hostapd.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160
  1. /*
  2. * hostapd / Initialization and configuration
  3. * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #ifndef CONFIG_NATIVE_WINDOWS
  16. #include <syslog.h>
  17. #endif /* CONFIG_NATIVE_WINDOWS */
  18. #include "eloop.h"
  19. #include "hostapd.h"
  20. #include "ieee802_1x.h"
  21. #include "ieee802_11.h"
  22. #include "beacon.h"
  23. #include "hw_features.h"
  24. #include "accounting.h"
  25. #include "eapol_sm.h"
  26. #include "iapp.h"
  27. #include "ap.h"
  28. #include "ieee802_11_auth.h"
  29. #include "ap_list.h"
  30. #include "sta_info.h"
  31. #include "driver_i.h"
  32. #include "radius/radius_client.h"
  33. #include "radius/radius_server.h"
  34. #include "radius/radius.h"
  35. #include "wpa.h"
  36. #include "preauth.h"
  37. #include "wme.h"
  38. #include "vlan_init.h"
  39. #include "ctrl_iface.h"
  40. #include "tls.h"
  41. #include "eap_server/eap_sim_db.h"
  42. #include "eap_server/eap.h"
  43. #include "eap_server/tncs.h"
  44. #include "version.h"
  45. #include "l2_packet/l2_packet.h"
  46. #include "wps_hostapd.h"
  47. static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
  48. size_t identity_len, int phase2,
  49. struct eap_user *user);
  50. static int hostapd_flush_old_stations(struct hostapd_data *hapd);
  51. static int hostapd_setup_wpa(struct hostapd_data *hapd);
  52. static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
  53. struct hapd_interfaces {
  54. size_t count;
  55. struct hostapd_iface **iface;
  56. };
  57. extern int wpa_debug_level;
  58. extern int wpa_debug_show_keys;
  59. extern int wpa_debug_timestamp;
  60. static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
  61. int level, const char *txt, size_t len)
  62. {
  63. struct hostapd_data *hapd = ctx;
  64. char *format, *module_str;
  65. int maxlen;
  66. int conf_syslog_level, conf_stdout_level;
  67. unsigned int conf_syslog, conf_stdout;
  68. maxlen = len + 100;
  69. format = os_malloc(maxlen);
  70. if (!format)
  71. return;
  72. if (hapd && hapd->conf) {
  73. conf_syslog_level = hapd->conf->logger_syslog_level;
  74. conf_stdout_level = hapd->conf->logger_stdout_level;
  75. conf_syslog = hapd->conf->logger_syslog;
  76. conf_stdout = hapd->conf->logger_stdout;
  77. } else {
  78. conf_syslog_level = conf_stdout_level = 0;
  79. conf_syslog = conf_stdout = (unsigned int) -1;
  80. }
  81. switch (module) {
  82. case HOSTAPD_MODULE_IEEE80211:
  83. module_str = "IEEE 802.11";
  84. break;
  85. case HOSTAPD_MODULE_IEEE8021X:
  86. module_str = "IEEE 802.1X";
  87. break;
  88. case HOSTAPD_MODULE_RADIUS:
  89. module_str = "RADIUS";
  90. break;
  91. case HOSTAPD_MODULE_WPA:
  92. module_str = "WPA";
  93. break;
  94. case HOSTAPD_MODULE_DRIVER:
  95. module_str = "DRIVER";
  96. break;
  97. case HOSTAPD_MODULE_IAPP:
  98. module_str = "IAPP";
  99. break;
  100. case HOSTAPD_MODULE_MLME:
  101. module_str = "MLME";
  102. break;
  103. default:
  104. module_str = NULL;
  105. break;
  106. }
  107. if (hapd && hapd->conf && addr)
  108. os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
  109. hapd->conf->iface, MAC2STR(addr),
  110. module_str ? " " : "", module_str, txt);
  111. else if (hapd && hapd->conf)
  112. os_snprintf(format, maxlen, "%s:%s%s %s",
  113. hapd->conf->iface, module_str ? " " : "",
  114. module_str, txt);
  115. else if (addr)
  116. os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
  117. MAC2STR(addr), module_str ? " " : "",
  118. module_str, txt);
  119. else
  120. os_snprintf(format, maxlen, "%s%s%s",
  121. module_str, module_str ? ": " : "", txt);
  122. if ((conf_stdout & module) && level >= conf_stdout_level) {
  123. wpa_debug_print_timestamp();
  124. printf("%s\n", format);
  125. }
  126. #ifndef CONFIG_NATIVE_WINDOWS
  127. if ((conf_syslog & module) && level >= conf_syslog_level) {
  128. int priority;
  129. switch (level) {
  130. case HOSTAPD_LEVEL_DEBUG_VERBOSE:
  131. case HOSTAPD_LEVEL_DEBUG:
  132. priority = LOG_DEBUG;
  133. break;
  134. case HOSTAPD_LEVEL_INFO:
  135. priority = LOG_INFO;
  136. break;
  137. case HOSTAPD_LEVEL_NOTICE:
  138. priority = LOG_NOTICE;
  139. break;
  140. case HOSTAPD_LEVEL_WARNING:
  141. priority = LOG_WARNING;
  142. break;
  143. default:
  144. priority = LOG_INFO;
  145. break;
  146. }
  147. syslog(priority, "%s", format);
  148. }
  149. #endif /* CONFIG_NATIVE_WINDOWS */
  150. os_free(format);
  151. }
  152. static void hostapd_deauth_all_stas(struct hostapd_data *hapd)
  153. {
  154. u8 addr[ETH_ALEN];
  155. /* New Prism2.5/3 STA firmware versions seem to have issues with this
  156. * broadcast deauth frame. This gets the firmware in odd state where
  157. * nothing works correctly, so let's skip sending this for the hostap
  158. * driver. */
  159. if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
  160. os_memset(addr, 0xff, ETH_ALEN);
  161. hostapd_sta_deauth(hapd, addr,
  162. WLAN_REASON_PREV_AUTH_NOT_VALID);
  163. }
  164. }
  165. /**
  166. * hostapd_prune_associations - Remove extraneous associations
  167. * @hapd: Pointer to BSS data for the most recent association
  168. * @sta: Pointer to the associated STA data
  169. *
  170. * This function looks through all radios and BSS's for previous
  171. * (stale) associations of STA. If any are found they are removed.
  172. */
  173. static void hostapd_prune_associations(struct hostapd_data *hapd,
  174. struct sta_info *sta)
  175. {
  176. struct sta_info *osta;
  177. struct hostapd_data *ohapd;
  178. size_t i, j;
  179. struct hapd_interfaces *interfaces = eloop_get_user_data();
  180. for (i = 0; i < interfaces->count; i++) {
  181. for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
  182. ohapd = interfaces->iface[i]->bss[j];
  183. if (ohapd == hapd)
  184. continue;
  185. osta = ap_get_sta(ohapd, sta->addr);
  186. if (!osta)
  187. continue;
  188. ap_sta_disassociate(ohapd, osta,
  189. WLAN_REASON_UNSPECIFIED);
  190. }
  191. }
  192. }
  193. /**
  194. * hostapd_new_assoc_sta - Notify that a new station associated with the AP
  195. * @hapd: Pointer to BSS data
  196. * @sta: Pointer to the associated STA data
  197. * @reassoc: 1 to indicate this was a re-association; 0 = first association
  198. *
  199. * This function will be called whenever a station associates with the AP. It
  200. * can be called for ieee802_11.c for drivers that export MLME to hostapd and
  201. * from driver_*.c for drivers that take care of management frames (IEEE 802.11
  202. * authentication and association) internally.
  203. */
  204. void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
  205. int reassoc)
  206. {
  207. if (hapd->tkip_countermeasures) {
  208. hostapd_sta_deauth(hapd, sta->addr,
  209. WLAN_REASON_MICHAEL_MIC_FAILURE);
  210. return;
  211. }
  212. hostapd_prune_associations(hapd, sta);
  213. /* IEEE 802.11F (IAPP) */
  214. if (hapd->conf->ieee802_11f)
  215. iapp_new_station(hapd->iapp, sta);
  216. /* Start accounting here, if IEEE 802.1X and WPA are not used.
  217. * IEEE 802.1X/WPA code will start accounting after the station has
  218. * been authorized. */
  219. if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
  220. accounting_sta_start(hapd, sta);
  221. hostapd_wme_sta_config(hapd, sta);
  222. /* Start IEEE 802.1X authentication process for new stations */
  223. ieee802_1x_new_station(hapd, sta);
  224. if (reassoc) {
  225. if (sta->auth_alg != WLAN_AUTH_FT &&
  226. !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
  227. wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
  228. } else
  229. wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
  230. }
  231. void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
  232. const u8 *buf, size_t len, int ack)
  233. {
  234. struct sta_info *sta;
  235. sta = ap_get_sta(hapd, addr);
  236. if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
  237. wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
  238. "activity poll", MAC2STR(sta->addr),
  239. ack ? "ACKed" : "did not ACK");
  240. if (ack)
  241. sta->flags &= ~WLAN_STA_PENDING_POLL;
  242. }
  243. if (sta)
  244. ieee802_1x_tx_status(hapd, sta, buf, len, ack);
  245. }
  246. void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd, const u8 *addr)
  247. {
  248. struct sta_info *sta;
  249. sta = ap_get_sta(hapd, addr);
  250. if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
  251. wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated "
  252. "STA " MACSTR, MAC2STR(addr));
  253. if (sta && (sta->flags & WLAN_STA_AUTH))
  254. hostapd_sta_disassoc(
  255. hapd, addr,
  256. WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
  257. else
  258. hostapd_sta_deauth(
  259. hapd, addr,
  260. WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
  261. }
  262. }
  263. int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
  264. const u8 *ie, size_t ielen)
  265. {
  266. struct sta_info *sta;
  267. int new_assoc, res;
  268. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  269. HOSTAPD_LEVEL_INFO, "associated");
  270. sta = ap_get_sta(hapd, addr);
  271. if (sta) {
  272. accounting_sta_stop(hapd, sta);
  273. } else {
  274. sta = ap_sta_add(hapd, addr);
  275. if (sta == NULL)
  276. return -1;
  277. }
  278. sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
  279. if (hapd->conf->wpa) {
  280. if (ie == NULL || ielen == 0) {
  281. if (hapd->conf->wps_state) {
  282. wpa_printf(MSG_DEBUG, "STA did not include "
  283. "WPA/RSN IE in (Re)Association "
  284. "Request - possible WPS use");
  285. sta->flags |= WLAN_STA_MAYBE_WPS;
  286. goto skip_wpa_check;
  287. }
  288. wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
  289. return -1;
  290. }
  291. if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
  292. os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
  293. sta->flags |= WLAN_STA_WPS;
  294. goto skip_wpa_check;
  295. }
  296. if (sta->wpa_sm == NULL)
  297. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  298. sta->addr);
  299. if (sta->wpa_sm == NULL) {
  300. wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
  301. "machine");
  302. return -1;
  303. }
  304. res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
  305. ie, ielen, NULL, 0);
  306. if (res != WPA_IE_OK) {
  307. wpa_printf(MSG_DEBUG, "WPA/RSN information element "
  308. "rejected? (res %u)", res);
  309. wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
  310. return -1;
  311. }
  312. }
  313. skip_wpa_check:
  314. new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
  315. sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
  316. wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
  317. hostapd_new_assoc_sta(hapd, sta, !new_assoc);
  318. ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
  319. return 0;
  320. }
  321. void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
  322. {
  323. struct sta_info *sta;
  324. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  325. HOSTAPD_LEVEL_INFO, "disassociated");
  326. sta = ap_get_sta(hapd, addr);
  327. if (sta == NULL) {
  328. wpa_printf(MSG_DEBUG, "Disassociation notification for "
  329. "unknown STA " MACSTR, MAC2STR(addr));
  330. return;
  331. }
  332. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
  333. wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
  334. sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
  335. ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
  336. ap_free_sta(hapd, sta);
  337. }
  338. void hostapd_eapol_receive(struct hostapd_data *hapd, const u8 *sa,
  339. const u8 *buf, size_t len)
  340. {
  341. ieee802_1x_receive(hapd, sa, buf, len);
  342. }
  343. #ifdef EAP_SERVER
  344. static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
  345. struct sta_info *sta, void *ctx)
  346. {
  347. if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
  348. return 1;
  349. return 0;
  350. }
  351. static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
  352. {
  353. struct hostapd_data *hapd = ctx;
  354. if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
  355. radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
  356. }
  357. #endif /* EAP_SERVER */
  358. /**
  359. * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
  360. */
  361. static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
  362. {
  363. wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
  364. eloop_terminate();
  365. }
  366. static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
  367. struct wpa_auth_config *wconf)
  368. {
  369. wconf->wpa = conf->wpa;
  370. wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
  371. wconf->wpa_pairwise = conf->wpa_pairwise;
  372. wconf->wpa_group = conf->wpa_group;
  373. wconf->wpa_group_rekey = conf->wpa_group_rekey;
  374. wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
  375. wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
  376. wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
  377. wconf->rsn_pairwise = conf->rsn_pairwise;
  378. wconf->rsn_preauth = conf->rsn_preauth;
  379. wconf->eapol_version = conf->eapol_version;
  380. wconf->peerkey = conf->peerkey;
  381. wconf->wme_enabled = conf->wme_enabled;
  382. wconf->okc = conf->okc;
  383. #ifdef CONFIG_IEEE80211W
  384. wconf->ieee80211w = conf->ieee80211w;
  385. #endif /* CONFIG_IEEE80211W */
  386. #ifdef CONFIG_IEEE80211R
  387. wconf->ssid_len = conf->ssid.ssid_len;
  388. if (wconf->ssid_len > SSID_LEN)
  389. wconf->ssid_len = SSID_LEN;
  390. os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
  391. os_memcpy(wconf->mobility_domain, conf->mobility_domain,
  392. MOBILITY_DOMAIN_ID_LEN);
  393. if (conf->nas_identifier &&
  394. os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
  395. wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
  396. os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
  397. wconf->r0_key_holder_len);
  398. }
  399. os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
  400. wconf->r0_key_lifetime = conf->r0_key_lifetime;
  401. wconf->reassociation_deadline = conf->reassociation_deadline;
  402. wconf->r0kh_list = conf->r0kh_list;
  403. wconf->r1kh_list = conf->r1kh_list;
  404. wconf->pmk_r1_push = conf->pmk_r1_push;
  405. #endif /* CONFIG_IEEE80211R */
  406. }
  407. int hostapd_reload_config(struct hostapd_iface *iface)
  408. {
  409. struct hostapd_data *hapd = iface->bss[0];
  410. struct hostapd_config *newconf, *oldconf;
  411. struct wpa_auth_config wpa_auth_conf;
  412. newconf = hostapd_config_read(iface->config_fname);
  413. if (newconf == NULL)
  414. return -1;
  415. /*
  416. * Deauthenticate all stations since the new configuration may not
  417. * allow them to use the BSS anymore.
  418. */
  419. hostapd_flush_old_stations(hapd);
  420. /* TODO: update dynamic data based on changed configuration
  421. * items (e.g., open/close sockets, etc.) */
  422. radius_client_flush(hapd->radius, 0);
  423. oldconf = hapd->iconf;
  424. hapd->iconf = newconf;
  425. hapd->conf = &newconf->bss[0];
  426. iface->conf = newconf;
  427. if (hostapd_setup_wpa_psk(hapd->conf)) {
  428. wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
  429. "after reloading configuration");
  430. }
  431. if (hapd->conf->wpa && hapd->wpa_auth == NULL)
  432. hostapd_setup_wpa(hapd);
  433. else if (hapd->conf->wpa) {
  434. hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
  435. wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
  436. } else if (hapd->wpa_auth) {
  437. wpa_deinit(hapd->wpa_auth);
  438. hapd->wpa_auth = NULL;
  439. hostapd_set_privacy(hapd, 0);
  440. hostapd_setup_encryption(hapd->conf->iface, hapd);
  441. }
  442. ieee802_11_set_beacon(hapd);
  443. hostapd_config_free(oldconf);
  444. wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
  445. return 0;
  446. }
  447. #ifndef CONFIG_NATIVE_WINDOWS
  448. /**
  449. * handle_reload - SIGHUP handler to reload configuration
  450. */
  451. static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
  452. {
  453. struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
  454. size_t i;
  455. wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
  456. sig);
  457. for (i = 0; i < hapds->count; i++) {
  458. if (hostapd_reload_config(hapds->iface[i]) < 0) {
  459. wpa_printf(MSG_WARNING, "Failed to read new "
  460. "configuration file - continuing with "
  461. "old.");
  462. continue;
  463. }
  464. }
  465. }
  466. #ifdef HOSTAPD_DUMP_STATE
  467. /**
  468. * hostapd_dump_state - SIGUSR1 handler to dump hostapd state to a text file
  469. */
  470. static void hostapd_dump_state(struct hostapd_data *hapd)
  471. {
  472. FILE *f;
  473. time_t now;
  474. struct sta_info *sta;
  475. int i;
  476. char *buf;
  477. if (!hapd->conf->dump_log_name) {
  478. wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
  479. "request");
  480. return;
  481. }
  482. wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
  483. hapd->conf->dump_log_name);
  484. f = fopen(hapd->conf->dump_log_name, "w");
  485. if (f == NULL) {
  486. wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
  487. "writing.", hapd->conf->dump_log_name);
  488. return;
  489. }
  490. time(&now);
  491. fprintf(f, "hostapd state dump - %s", ctime(&now));
  492. fprintf(f, "num_sta=%d num_sta_non_erp=%d "
  493. "num_sta_no_short_slot_time=%d\n"
  494. "num_sta_no_short_preamble=%d\n",
  495. hapd->num_sta, hapd->iface->num_sta_non_erp,
  496. hapd->iface->num_sta_no_short_slot_time,
  497. hapd->iface->num_sta_no_short_preamble);
  498. for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
  499. fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
  500. fprintf(f,
  501. " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
  502. " capability=0x%x listen_interval=%d\n",
  503. sta->aid,
  504. sta->flags,
  505. (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
  506. (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
  507. (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
  508. (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
  509. (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
  510. (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
  511. ""),
  512. (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
  513. ""),
  514. (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
  515. "[SHORT_PREAMBLE]" : ""),
  516. (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
  517. (sta->flags & WLAN_STA_WME ? "[WME]" : ""),
  518. (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
  519. (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
  520. (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
  521. (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
  522. sta->capability,
  523. sta->listen_interval);
  524. fprintf(f, " supported_rates=");
  525. for (i = 0; i < sta->supported_rates_len; i++)
  526. fprintf(f, "%02x ", sta->supported_rates[i]);
  527. fprintf(f, "\n");
  528. fprintf(f,
  529. " timeout_next=%s\n",
  530. (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
  531. (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
  532. "DEAUTH")));
  533. ieee802_1x_dump_state(f, " ", sta);
  534. }
  535. buf = os_malloc(4096);
  536. if (buf) {
  537. int count = radius_client_get_mib(hapd->radius, buf, 4096);
  538. if (count < 0)
  539. count = 0;
  540. else if (count > 4095)
  541. count = 4095;
  542. buf[count] = '\0';
  543. fprintf(f, "%s", buf);
  544. count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
  545. if (count < 0)
  546. count = 0;
  547. else if (count > 4095)
  548. count = 4095;
  549. buf[count] = '\0';
  550. fprintf(f, "%s", buf);
  551. os_free(buf);
  552. }
  553. fclose(f);
  554. }
  555. #endif /* HOSTAPD_DUMP_STATE */
  556. static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
  557. {
  558. #ifdef HOSTAPD_DUMP_STATE
  559. struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
  560. size_t i, j;
  561. for (i = 0; i < hapds->count; i++) {
  562. struct hostapd_iface *hapd_iface = hapds->iface[i];
  563. for (j = 0; j < hapd_iface->num_bss; j++)
  564. hostapd_dump_state(hapd_iface->bss[j]);
  565. }
  566. #endif /* HOSTAPD_DUMP_STATE */
  567. }
  568. #endif /* CONFIG_NATIVE_WINDOWS */
  569. static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
  570. char *ifname)
  571. {
  572. int i;
  573. for (i = 0; i < NUM_WEP_KEYS; i++) {
  574. if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
  575. 0, i == 0 ? 1 : 0)) {
  576. wpa_printf(MSG_DEBUG, "Failed to clear default "
  577. "encryption keys (ifname=%s keyidx=%d)",
  578. ifname, i);
  579. }
  580. }
  581. #ifdef CONFIG_IEEE80211W
  582. if (hapd->conf->ieee80211w) {
  583. for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
  584. if (hostapd_set_encryption(ifname, hapd, "none", NULL,
  585. i, NULL, 0,
  586. i == 0 ? 1 : 0)) {
  587. wpa_printf(MSG_DEBUG, "Failed to clear "
  588. "default mgmt encryption keys "
  589. "(ifname=%s keyidx=%d)", ifname, i);
  590. }
  591. }
  592. }
  593. #endif /* CONFIG_IEEE80211W */
  594. }
  595. static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
  596. {
  597. hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
  598. return 0;
  599. }
  600. static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
  601. {
  602. int errors = 0, idx;
  603. struct hostapd_ssid *ssid = &hapd->conf->ssid;
  604. idx = ssid->wep.idx;
  605. if (ssid->wep.default_len &&
  606. hostapd_set_encryption(hapd->conf->iface,
  607. hapd, "WEP", NULL, idx,
  608. ssid->wep.key[idx],
  609. ssid->wep.len[idx],
  610. idx == ssid->wep.idx)) {
  611. wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
  612. errors++;
  613. }
  614. if (ssid->dyn_vlan_keys) {
  615. size_t i;
  616. for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
  617. const char *ifname;
  618. struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
  619. if (key == NULL)
  620. continue;
  621. ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
  622. i);
  623. if (ifname == NULL)
  624. continue;
  625. idx = key->idx;
  626. if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
  627. idx, key->key[idx],
  628. key->len[idx],
  629. idx == key->idx)) {
  630. wpa_printf(MSG_WARNING, "Could not set "
  631. "dynamic VLAN WEP encryption.");
  632. errors++;
  633. }
  634. }
  635. }
  636. return errors;
  637. }
  638. /**
  639. * hostapd_cleanup - Per-BSS cleanup (deinitialization)
  640. * @hapd: Pointer to BSS data
  641. *
  642. * This function is used to free all per-BSS data structures and resources.
  643. * This gets called in a loop for each BSS between calls to
  644. * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
  645. * is deinitialized. Most of the modules that are initialized in
  646. * hostapd_setup_bss() are deinitialized here.
  647. */
  648. static void hostapd_cleanup(struct hostapd_data *hapd)
  649. {
  650. hostapd_ctrl_iface_deinit(hapd);
  651. os_free(hapd->default_wep_key);
  652. hapd->default_wep_key = NULL;
  653. iapp_deinit(hapd->iapp);
  654. hapd->iapp = NULL;
  655. accounting_deinit(hapd);
  656. rsn_preauth_iface_deinit(hapd);
  657. if (hapd->wpa_auth) {
  658. wpa_deinit(hapd->wpa_auth);
  659. hapd->wpa_auth = NULL;
  660. if (hostapd_set_privacy(hapd, 0)) {
  661. wpa_printf(MSG_DEBUG, "Could not disable "
  662. "PrivacyInvoked for interface %s",
  663. hapd->conf->iface);
  664. }
  665. if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
  666. wpa_printf(MSG_DEBUG, "Could not remove generic "
  667. "information element from interface %s",
  668. hapd->conf->iface);
  669. }
  670. }
  671. ieee802_1x_deinit(hapd);
  672. vlan_deinit(hapd);
  673. hostapd_acl_deinit(hapd);
  674. radius_client_deinit(hapd->radius);
  675. hapd->radius = NULL;
  676. radius_server_deinit(hapd->radius_srv);
  677. hapd->radius_srv = NULL;
  678. #ifdef CONFIG_IEEE80211R
  679. l2_packet_deinit(hapd->l2);
  680. #endif /* CONFIG_IEEE80211R */
  681. hostapd_deinit_wps(hapd);
  682. hostapd_wireless_event_deinit(hapd);
  683. #ifdef EAP_TLS_FUNCS
  684. if (hapd->ssl_ctx) {
  685. tls_deinit(hapd->ssl_ctx);
  686. hapd->ssl_ctx = NULL;
  687. }
  688. #endif /* EAP_TLS_FUNCS */
  689. #ifdef EAP_SERVER
  690. if (hapd->eap_sim_db_priv) {
  691. eap_sim_db_deinit(hapd->eap_sim_db_priv);
  692. hapd->eap_sim_db_priv = NULL;
  693. }
  694. #endif /* EAP_SERVER */
  695. if (hapd->interface_added &&
  696. hostapd_bss_remove(hapd, hapd->conf->iface)) {
  697. wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
  698. hapd->conf->iface);
  699. }
  700. }
  701. /**
  702. * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
  703. * @iface: Pointer to interface data
  704. *
  705. * This function is called before per-BSS data structures are deinitialized
  706. * with hostapd_cleanup().
  707. */
  708. static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
  709. {
  710. }
  711. /**
  712. * hostapd_cleanup_iface - Complete per-interface cleanup
  713. * @iface: Pointer to interface data
  714. *
  715. * This function is called after per-BSS data structures are deinitialized
  716. * with hostapd_cleanup().
  717. */
  718. static void hostapd_cleanup_iface(struct hostapd_iface *iface)
  719. {
  720. hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
  721. iface->hw_features = NULL;
  722. os_free(iface->current_rates);
  723. iface->current_rates = NULL;
  724. ap_list_deinit(iface);
  725. hostapd_config_free(iface->conf);
  726. iface->conf = NULL;
  727. os_free(iface->config_fname);
  728. os_free(iface->bss);
  729. os_free(iface);
  730. }
  731. static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
  732. {
  733. int i;
  734. hostapd_broadcast_wep_set(hapd);
  735. if (hapd->conf->ssid.wep.default_len)
  736. return 0;
  737. for (i = 0; i < 4; i++) {
  738. if (hapd->conf->ssid.wep.key[i] &&
  739. hostapd_set_encryption(iface, hapd, "WEP", NULL,
  740. i, hapd->conf->ssid.wep.key[i],
  741. hapd->conf->ssid.wep.len[i],
  742. i == hapd->conf->ssid.wep.idx)) {
  743. wpa_printf(MSG_WARNING, "Could not set WEP "
  744. "encryption.");
  745. return -1;
  746. }
  747. if (hapd->conf->ssid.wep.key[i] &&
  748. i == hapd->conf->ssid.wep.idx)
  749. hostapd_set_privacy(hapd, 1);
  750. }
  751. return 0;
  752. }
  753. static int hostapd_flush_old_stations(struct hostapd_data *hapd)
  754. {
  755. int ret = 0;
  756. if (hostapd_drv_none(hapd))
  757. return 0;
  758. wpa_printf(MSG_DEBUG, "Flushing old station entries");
  759. if (hostapd_flush(hapd)) {
  760. wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
  761. ret = -1;
  762. }
  763. wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
  764. hostapd_deauth_all_stas(hapd);
  765. return ret;
  766. }
  767. static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
  768. logger_level level, const char *txt)
  769. {
  770. struct hostapd_data *hapd = ctx;
  771. int hlevel;
  772. switch (level) {
  773. case LOGGER_WARNING:
  774. hlevel = HOSTAPD_LEVEL_WARNING;
  775. break;
  776. case LOGGER_INFO:
  777. hlevel = HOSTAPD_LEVEL_INFO;
  778. break;
  779. case LOGGER_DEBUG:
  780. default:
  781. hlevel = HOSTAPD_LEVEL_DEBUG;
  782. break;
  783. }
  784. hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
  785. }
  786. static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
  787. u16 reason)
  788. {
  789. struct hostapd_data *hapd = ctx;
  790. struct sta_info *sta;
  791. wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
  792. "STA " MACSTR " reason %d",
  793. __func__, MAC2STR(addr), reason);
  794. sta = ap_get_sta(hapd, addr);
  795. hostapd_sta_deauth(hapd, addr, reason);
  796. if (sta == NULL)
  797. return;
  798. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
  799. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  800. eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
  801. sta->timeout_next = STA_REMOVE;
  802. }
  803. static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
  804. {
  805. struct hostapd_data *hapd = ctx;
  806. ieee80211_michael_mic_failure(hapd, addr, 0);
  807. }
  808. static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
  809. wpa_eapol_variable var, int value)
  810. {
  811. struct hostapd_data *hapd = ctx;
  812. struct sta_info *sta = ap_get_sta(hapd, addr);
  813. if (sta == NULL)
  814. return;
  815. switch (var) {
  816. case WPA_EAPOL_portEnabled:
  817. ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
  818. break;
  819. case WPA_EAPOL_portValid:
  820. ieee802_1x_notify_port_valid(sta->eapol_sm, value);
  821. break;
  822. case WPA_EAPOL_authorized:
  823. ieee802_1x_set_sta_authorized(hapd, sta, value);
  824. break;
  825. case WPA_EAPOL_portControl_Auto:
  826. if (sta->eapol_sm)
  827. sta->eapol_sm->portControl = Auto;
  828. break;
  829. case WPA_EAPOL_keyRun:
  830. if (sta->eapol_sm)
  831. sta->eapol_sm->keyRun = value ? TRUE : FALSE;
  832. break;
  833. case WPA_EAPOL_keyAvailable:
  834. if (sta->eapol_sm)
  835. sta->eapol_sm->eap_if->eapKeyAvailable =
  836. value ? TRUE : FALSE;
  837. break;
  838. case WPA_EAPOL_keyDone:
  839. if (sta->eapol_sm)
  840. sta->eapol_sm->keyDone = value ? TRUE : FALSE;
  841. break;
  842. case WPA_EAPOL_inc_EapolFramesTx:
  843. if (sta->eapol_sm)
  844. sta->eapol_sm->dot1xAuthEapolFramesTx++;
  845. break;
  846. }
  847. }
  848. static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
  849. wpa_eapol_variable var)
  850. {
  851. struct hostapd_data *hapd = ctx;
  852. struct sta_info *sta = ap_get_sta(hapd, addr);
  853. if (sta == NULL || sta->eapol_sm == NULL)
  854. return -1;
  855. switch (var) {
  856. case WPA_EAPOL_keyRun:
  857. return sta->eapol_sm->keyRun;
  858. case WPA_EAPOL_keyAvailable:
  859. return sta->eapol_sm->eap_if->eapKeyAvailable;
  860. default:
  861. return -1;
  862. }
  863. }
  864. static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
  865. const u8 *prev_psk)
  866. {
  867. struct hostapd_data *hapd = ctx;
  868. return hostapd_get_psk(hapd->conf, addr, prev_psk);
  869. }
  870. static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
  871. size_t *len)
  872. {
  873. struct hostapd_data *hapd = ctx;
  874. const u8 *key;
  875. size_t keylen;
  876. struct sta_info *sta;
  877. sta = ap_get_sta(hapd, addr);
  878. if (sta == NULL)
  879. return -1;
  880. key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
  881. if (key == NULL)
  882. return -1;
  883. if (keylen > *len)
  884. keylen = *len;
  885. os_memcpy(msk, key, keylen);
  886. *len = keylen;
  887. return 0;
  888. }
  889. static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
  890. const u8 *addr, int idx, u8 *key,
  891. size_t key_len)
  892. {
  893. struct hostapd_data *hapd = ctx;
  894. const char *ifname = hapd->conf->iface;
  895. if (vlan_id > 0) {
  896. ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
  897. if (ifname == NULL)
  898. return -1;
  899. }
  900. return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
  901. key, key_len, 1);
  902. }
  903. static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
  904. u8 *seq)
  905. {
  906. struct hostapd_data *hapd = ctx;
  907. return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
  908. }
  909. static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
  910. u8 *seq)
  911. {
  912. struct hostapd_data *hapd = ctx;
  913. return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
  914. seq);
  915. }
  916. static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
  917. const u8 *data, size_t data_len,
  918. int encrypt)
  919. {
  920. struct hostapd_data *hapd = ctx;
  921. return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
  922. }
  923. static int hostapd_wpa_auth_for_each_sta(
  924. void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
  925. void *cb_ctx)
  926. {
  927. struct hostapd_data *hapd = ctx;
  928. struct sta_info *sta;
  929. for (sta = hapd->sta_list; sta; sta = sta->next) {
  930. if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
  931. return 1;
  932. }
  933. return 0;
  934. }
  935. static int hostapd_wpa_auth_for_each_auth(
  936. void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
  937. void *cb_ctx)
  938. {
  939. struct hostapd_data *ohapd;
  940. size_t i, j;
  941. struct hapd_interfaces *interfaces = eloop_get_user_data();
  942. for (i = 0; i < interfaces->count; i++) {
  943. for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
  944. ohapd = interfaces->iface[i]->bss[j];
  945. if (cb(ohapd->wpa_auth, cb_ctx))
  946. return 1;
  947. }
  948. }
  949. return 0;
  950. }
  951. static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
  952. const u8 *data, size_t data_len)
  953. {
  954. struct hostapd_data *hapd = ctx;
  955. if (hapd->driver && hapd->driver->send_ether)
  956. return hapd->driver->send_ether(hapd->drv_priv, dst,
  957. hapd->own_addr, proto,
  958. data, data_len);
  959. if (hapd->l2 == NULL)
  960. return -1;
  961. return l2_packet_send(hapd->l2, dst, proto, data, data_len);
  962. }
  963. #ifdef CONFIG_IEEE80211R
  964. static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
  965. const u8 *data, size_t data_len)
  966. {
  967. struct hostapd_data *hapd = ctx;
  968. int res;
  969. struct ieee80211_mgmt *m;
  970. size_t mlen;
  971. struct sta_info *sta;
  972. sta = ap_get_sta(hapd, dst);
  973. if (sta == NULL || sta->wpa_sm == NULL)
  974. return -1;
  975. m = os_zalloc(sizeof(*m) + data_len);
  976. if (m == NULL)
  977. return -1;
  978. mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
  979. m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  980. WLAN_FC_STYPE_ACTION);
  981. os_memcpy(m->da, dst, ETH_ALEN);
  982. os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
  983. os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
  984. os_memcpy(&m->u, data, data_len);
  985. res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
  986. os_free(m);
  987. return res;
  988. }
  989. static struct wpa_state_machine *
  990. hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
  991. {
  992. struct hostapd_data *hapd = ctx;
  993. struct sta_info *sta;
  994. sta = ap_sta_add(hapd, sta_addr);
  995. if (sta == NULL)
  996. return NULL;
  997. if (sta->wpa_sm)
  998. return sta->wpa_sm;
  999. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
  1000. if (sta->wpa_sm == NULL) {
  1001. ap_free_sta(hapd, sta);
  1002. return NULL;
  1003. }
  1004. sta->auth_alg = WLAN_AUTH_FT;
  1005. return sta->wpa_sm;
  1006. }
  1007. static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
  1008. size_t len)
  1009. {
  1010. struct hostapd_data *hapd = ctx;
  1011. wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
  1012. }
  1013. #endif /* CONFIG_IEEE80211R */
  1014. /**
  1015. * hostapd_validate_bssid_configuration - Validate BSSID configuration
  1016. * @iface: Pointer to interface data
  1017. * Returns: 0 on success, -1 on failure
  1018. *
  1019. * This function is used to validate that the configured BSSIDs are valid.
  1020. */
  1021. static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
  1022. {
  1023. u8 mask[ETH_ALEN] = { 0 };
  1024. struct hostapd_data *hapd = iface->bss[0];
  1025. unsigned int i = iface->conf->num_bss, bits = 0, j;
  1026. int res;
  1027. if (hostapd_drv_none(hapd))
  1028. return 0;
  1029. /* Generate BSSID mask that is large enough to cover the BSSIDs. */
  1030. /* Determine the bits necessary to cover the number of BSSIDs. */
  1031. for (i--; i; i >>= 1)
  1032. bits++;
  1033. /* Determine the bits necessary to any configured BSSIDs,
  1034. if they are higher than the number of BSSIDs. */
  1035. for (j = 0; j < iface->conf->num_bss; j++) {
  1036. if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
  1037. continue;
  1038. for (i = 0; i < ETH_ALEN; i++) {
  1039. mask[i] |=
  1040. iface->conf->bss[j].bssid[i] ^
  1041. hapd->own_addr[i];
  1042. }
  1043. }
  1044. for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
  1045. ;
  1046. j = 0;
  1047. if (i < ETH_ALEN) {
  1048. j = (5 - i) * 8;
  1049. while (mask[i] != 0) {
  1050. mask[i] >>= 1;
  1051. j++;
  1052. }
  1053. }
  1054. if (bits < j)
  1055. bits = j;
  1056. if (bits > 40)
  1057. return -1;
  1058. os_memset(mask, 0xff, ETH_ALEN);
  1059. j = bits / 8;
  1060. for (i = 5; i > 5 - j; i--)
  1061. mask[i] = 0;
  1062. j = bits % 8;
  1063. while (j--)
  1064. mask[i] <<= 1;
  1065. wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
  1066. (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
  1067. res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
  1068. if (res == 0)
  1069. return 0;
  1070. if (res < 0) {
  1071. wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
  1072. MACSTR " for start address " MACSTR ".",
  1073. MAC2STR(mask), MAC2STR(hapd->own_addr));
  1074. return -1;
  1075. }
  1076. for (i = 0; i < ETH_ALEN; i++) {
  1077. if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
  1078. wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
  1079. " for start address " MACSTR ".",
  1080. MAC2STR(mask), MAC2STR(hapd->own_addr));
  1081. wpa_printf(MSG_ERROR, "Start address must be the "
  1082. "first address in the block (i.e., addr "
  1083. "AND mask == addr).");
  1084. return -1;
  1085. }
  1086. }
  1087. return 0;
  1088. }
  1089. static int mac_in_conf(struct hostapd_config *conf, const void *a)
  1090. {
  1091. size_t i;
  1092. for (i = 0; i < conf->num_bss; i++) {
  1093. if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
  1094. return 1;
  1095. }
  1096. }
  1097. return 0;
  1098. }
  1099. static int hostapd_setup_wpa(struct hostapd_data *hapd)
  1100. {
  1101. struct wpa_auth_config _conf;
  1102. struct wpa_auth_callbacks cb;
  1103. const u8 *wpa_ie;
  1104. size_t wpa_ie_len;
  1105. hostapd_wpa_auth_conf(hapd->conf, &_conf);
  1106. os_memset(&cb, 0, sizeof(cb));
  1107. cb.ctx = hapd;
  1108. cb.logger = hostapd_wpa_auth_logger;
  1109. cb.disconnect = hostapd_wpa_auth_disconnect;
  1110. cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
  1111. cb.set_eapol = hostapd_wpa_auth_set_eapol;
  1112. cb.get_eapol = hostapd_wpa_auth_get_eapol;
  1113. cb.get_psk = hostapd_wpa_auth_get_psk;
  1114. cb.get_msk = hostapd_wpa_auth_get_msk;
  1115. cb.set_key = hostapd_wpa_auth_set_key;
  1116. cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
  1117. cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
  1118. cb.send_eapol = hostapd_wpa_auth_send_eapol;
  1119. cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
  1120. cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
  1121. cb.send_ether = hostapd_wpa_auth_send_ether;
  1122. #ifdef CONFIG_IEEE80211R
  1123. cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
  1124. cb.add_sta = hostapd_wpa_auth_add_sta;
  1125. #endif /* CONFIG_IEEE80211R */
  1126. hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
  1127. if (hapd->wpa_auth == NULL) {
  1128. wpa_printf(MSG_ERROR, "WPA initialization failed.");
  1129. return -1;
  1130. }
  1131. if (hostapd_set_privacy(hapd, 1)) {
  1132. wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
  1133. "for interface %s", hapd->conf->iface);
  1134. return -1;
  1135. }
  1136. wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
  1137. if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
  1138. wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
  1139. "the kernel driver.");
  1140. return -1;
  1141. }
  1142. if (rsn_preauth_iface_init(hapd)) {
  1143. wpa_printf(MSG_ERROR, "Initialization of RSN "
  1144. "pre-authentication failed.");
  1145. return -1;
  1146. }
  1147. return 0;
  1148. }
  1149. static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
  1150. struct hostapd_bss_config *conf)
  1151. {
  1152. struct radius_server_conf srv;
  1153. os_memset(&srv, 0, sizeof(srv));
  1154. srv.client_file = conf->radius_server_clients;
  1155. srv.auth_port = conf->radius_server_auth_port;
  1156. srv.conf_ctx = conf;
  1157. srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
  1158. srv.ssl_ctx = hapd->ssl_ctx;
  1159. srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
  1160. srv.eap_fast_a_id = conf->eap_fast_a_id;
  1161. srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
  1162. srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
  1163. srv.eap_fast_prov = conf->eap_fast_prov;
  1164. srv.pac_key_lifetime = conf->pac_key_lifetime;
  1165. srv.pac_key_refresh_time = conf->pac_key_refresh_time;
  1166. srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
  1167. srv.tnc = conf->tnc;
  1168. srv.wps = hapd->wps;
  1169. srv.ipv6 = conf->radius_server_ipv6;
  1170. srv.get_eap_user = hostapd_radius_get_eap_user;
  1171. srv.eap_req_id_text = conf->eap_req_id_text;
  1172. srv.eap_req_id_text_len = conf->eap_req_id_text_len;
  1173. hapd->radius_srv = radius_server_init(&srv);
  1174. if (hapd->radius_srv == NULL) {
  1175. wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
  1176. return -1;
  1177. }
  1178. return 0;
  1179. }
  1180. /**
  1181. * hostapd_setup_bss - Per-BSS setup (initialization)
  1182. * @hapd: Pointer to BSS data
  1183. * @first: Whether this BSS is the first BSS of an interface
  1184. *
  1185. * This function is used to initialize all per-BSS data structures and
  1186. * resources. This gets called in a loop for each BSS when an interface is
  1187. * initialized. Most of the modules that are initialized here will be
  1188. * deinitialized in hostapd_cleanup().
  1189. */
  1190. static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
  1191. {
  1192. struct hostapd_bss_config *conf = hapd->conf;
  1193. u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
  1194. int ssid_len, set_ssid;
  1195. if (!first) {
  1196. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
  1197. /* Allocate the next available BSSID. */
  1198. do {
  1199. inc_byte_array(hapd->own_addr, ETH_ALEN);
  1200. } while (mac_in_conf(hapd->iconf, hapd->own_addr));
  1201. } else {
  1202. /* Allocate the configured BSSID. */
  1203. os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
  1204. if (hostapd_mac_comp(hapd->own_addr,
  1205. hapd->iface->bss[0]->own_addr) ==
  1206. 0) {
  1207. wpa_printf(MSG_ERROR, "BSS '%s' may not have "
  1208. "BSSID set to the MAC address of "
  1209. "the radio", hapd->conf->iface);
  1210. return -1;
  1211. }
  1212. }
  1213. hapd->interface_added = 1;
  1214. if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
  1215. hapd->own_addr)) {
  1216. wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
  1217. MACSTR ")", MAC2STR(hapd->own_addr));
  1218. return -1;
  1219. }
  1220. }
  1221. /*
  1222. * Fetch the SSID from the system and use it or,
  1223. * if one was specified in the config file, verify they
  1224. * match.
  1225. */
  1226. ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
  1227. if (ssid_len < 0) {
  1228. wpa_printf(MSG_ERROR, "Could not read SSID from system");
  1229. return -1;
  1230. }
  1231. if (conf->ssid.ssid_set) {
  1232. /*
  1233. * If SSID is specified in the config file and it differs
  1234. * from what is being used then force installation of the
  1235. * new SSID.
  1236. */
  1237. set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
  1238. os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
  1239. } else {
  1240. /*
  1241. * No SSID in the config file; just use the one we got
  1242. * from the system.
  1243. */
  1244. set_ssid = 0;
  1245. conf->ssid.ssid_len = ssid_len;
  1246. os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
  1247. conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
  1248. }
  1249. if (!hostapd_drv_none(hapd)) {
  1250. wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
  1251. " and ssid '%s'",
  1252. hapd->conf->iface, MAC2STR(hapd->own_addr),
  1253. hapd->conf->ssid.ssid);
  1254. }
  1255. if (hostapd_setup_wpa_psk(conf)) {
  1256. wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
  1257. return -1;
  1258. }
  1259. /* Set flag for whether SSID is broadcast in beacons */
  1260. if (hostapd_set_broadcast_ssid(hapd,
  1261. !!hapd->conf->ignore_broadcast_ssid)) {
  1262. wpa_printf(MSG_ERROR, "Could not set broadcast SSID flag for "
  1263. "kernel driver");
  1264. return -1;
  1265. }
  1266. if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
  1267. wpa_printf(MSG_ERROR, "Could not set DTIM period for kernel "
  1268. "driver");
  1269. return -1;
  1270. }
  1271. /* Set SSID for the kernel driver (to be used in beacon and probe
  1272. * response frames) */
  1273. if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
  1274. conf->ssid.ssid_len)) {
  1275. wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
  1276. return -1;
  1277. }
  1278. if (wpa_debug_level == MSG_MSGDUMP)
  1279. conf->radius->msg_dumps = 1;
  1280. hapd->radius = radius_client_init(hapd, conf->radius);
  1281. if (hapd->radius == NULL) {
  1282. wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
  1283. return -1;
  1284. }
  1285. if (hostapd_acl_init(hapd)) {
  1286. wpa_printf(MSG_ERROR, "ACL initialization failed.");
  1287. return -1;
  1288. }
  1289. if (hostapd_init_wps(hapd, conf))
  1290. return -1;
  1291. if (ieee802_1x_init(hapd)) {
  1292. wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
  1293. return -1;
  1294. }
  1295. if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
  1296. return -1;
  1297. if (accounting_init(hapd)) {
  1298. wpa_printf(MSG_ERROR, "Accounting initialization failed.");
  1299. return -1;
  1300. }
  1301. if (hapd->conf->ieee802_11f &&
  1302. (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
  1303. wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
  1304. "failed.");
  1305. return -1;
  1306. }
  1307. if (hostapd_ctrl_iface_init(hapd)) {
  1308. wpa_printf(MSG_ERROR, "Failed to setup control interface");
  1309. return -1;
  1310. }
  1311. if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
  1312. wpa_printf(MSG_ERROR, "VLAN initialization failed.");
  1313. return -1;
  1314. }
  1315. #ifdef CONFIG_IEEE80211R
  1316. if (!hostapd_drv_none(hapd)) {
  1317. hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
  1318. hostapd_rrb_receive, hapd, 0);
  1319. if (hapd->l2 == NULL &&
  1320. (hapd->driver == NULL ||
  1321. hapd->driver->send_ether == NULL)) {
  1322. wpa_printf(MSG_ERROR, "Failed to open l2_packet "
  1323. "interface");
  1324. return -1;
  1325. }
  1326. }
  1327. #endif /* CONFIG_IEEE80211R */
  1328. ieee802_11_set_beacon(hapd);
  1329. if (conf->radius_server_clients &&
  1330. hostapd_setup_radius_srv(hapd, conf))
  1331. return -1;
  1332. return 0;
  1333. }
  1334. static void hostapd_tx_queue_params(struct hostapd_iface *iface)
  1335. {
  1336. struct hostapd_data *hapd = iface->bss[0];
  1337. int i;
  1338. struct hostapd_tx_queue_params *p;
  1339. for (i = 0; i < NUM_TX_QUEUES; i++) {
  1340. p = &iface->conf->tx_queue[i];
  1341. if (!p->configured)
  1342. continue;
  1343. if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
  1344. p->cwmax, p->burst)) {
  1345. wpa_printf(MSG_DEBUG, "Failed to set TX queue "
  1346. "parameters for queue %d.", i);
  1347. /* Continue anyway */
  1348. }
  1349. }
  1350. }
  1351. static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
  1352. size_t identity_len, int phase2,
  1353. struct eap_user *user)
  1354. {
  1355. const struct hostapd_eap_user *eap_user;
  1356. int i, count;
  1357. eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
  1358. if (eap_user == NULL)
  1359. return -1;
  1360. if (user == NULL)
  1361. return 0;
  1362. os_memset(user, 0, sizeof(*user));
  1363. count = EAP_USER_MAX_METHODS;
  1364. if (count > EAP_MAX_METHODS)
  1365. count = EAP_MAX_METHODS;
  1366. for (i = 0; i < count; i++) {
  1367. user->methods[i].vendor = eap_user->methods[i].vendor;
  1368. user->methods[i].method = eap_user->methods[i].method;
  1369. }
  1370. if (eap_user->password) {
  1371. user->password = os_malloc(eap_user->password_len);
  1372. if (user->password == NULL)
  1373. return -1;
  1374. os_memcpy(user->password, eap_user->password,
  1375. eap_user->password_len);
  1376. user->password_len = eap_user->password_len;
  1377. user->password_hash = eap_user->password_hash;
  1378. }
  1379. user->force_version = eap_user->force_version;
  1380. user->ttls_auth = eap_user->ttls_auth;
  1381. return 0;
  1382. }
  1383. static int setup_interface(struct hostapd_iface *iface)
  1384. {
  1385. struct hostapd_data *hapd = iface->bss[0];
  1386. struct hostapd_bss_config *conf = hapd->conf;
  1387. size_t i;
  1388. char country[4];
  1389. u8 *b = conf->bssid;
  1390. int freq;
  1391. size_t j;
  1392. int ret = 0;
  1393. u8 *prev_addr;
  1394. /*
  1395. * Initialize the driver interface and make sure that all BSSes get
  1396. * configured with a pointer to this driver interface.
  1397. */
  1398. if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
  1399. hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
  1400. } else {
  1401. hapd->drv_priv = hostapd_driver_init(hapd);
  1402. }
  1403. if (hapd->drv_priv == NULL) {
  1404. wpa_printf(MSG_ERROR, "%s driver initialization failed.",
  1405. hapd->driver ? hapd->driver->name : "Unknown");
  1406. hapd->driver = NULL;
  1407. return -1;
  1408. }
  1409. for (i = 0; i < iface->num_bss; i++) {
  1410. iface->bss[i]->driver = hapd->driver;
  1411. iface->bss[i]->drv_priv = hapd->drv_priv;
  1412. }
  1413. if (hostapd_validate_bssid_configuration(iface))
  1414. return -1;
  1415. #ifdef CONFIG_IEEE80211N
  1416. SET_2BIT_LE16(&iface->ht_op_mode,
  1417. HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
  1418. OP_MODE_PURE);
  1419. #endif /* CONFIG_IEEE80211N */
  1420. os_memcpy(country, hapd->iconf->country, 3);
  1421. country[3] = '\0';
  1422. if (hostapd_set_country(hapd, country) < 0) {
  1423. wpa_printf(MSG_ERROR, "Failed to set country code");
  1424. return -1;
  1425. }
  1426. if (hapd->iconf->ieee80211d &&
  1427. hostapd_set_ieee80211d(hapd, 1) < 0) {
  1428. wpa_printf(MSG_ERROR, "Failed to set ieee80211d (%d)",
  1429. hapd->iconf->ieee80211d);
  1430. return -1;
  1431. }
  1432. if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
  1433. hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
  1434. wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
  1435. "kernel driver");
  1436. return -1;
  1437. }
  1438. /* TODO: merge with hostapd_driver_init() ? */
  1439. if (hostapd_wireless_event_init(hapd) < 0)
  1440. return -1;
  1441. if (hostapd_get_hw_features(iface)) {
  1442. /* Not all drivers support this yet, so continue without hw
  1443. * feature data. */
  1444. } else {
  1445. int ret = hostapd_select_hw_mode(iface);
  1446. if (ret < 0) {
  1447. wpa_printf(MSG_ERROR, "Could not select hw_mode and "
  1448. "channel. (%d)", ret);
  1449. return -1;
  1450. }
  1451. }
  1452. hostapd_flush_old_stations(hapd);
  1453. hostapd_set_privacy(hapd, 0);
  1454. if (hapd->iconf->channel) {
  1455. freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
  1456. wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
  1457. "Frequency: %d MHz",
  1458. hostapd_hw_mode_txt(hapd->iconf->hw_mode),
  1459. hapd->iconf->channel, freq);
  1460. if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
  1461. hapd->iconf->ieee80211n,
  1462. hapd->iconf->secondary_channel)) {
  1463. wpa_printf(MSG_ERROR, "Could not set channel for "
  1464. "kernel driver");
  1465. return -1;
  1466. }
  1467. }
  1468. hostapd_broadcast_wep_clear(hapd);
  1469. if (hostapd_setup_encryption(hapd->conf->iface, hapd))
  1470. return -1;
  1471. hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
  1472. ieee802_11_set_beacon(hapd);
  1473. if (hapd->iconf->rts_threshold > -1 &&
  1474. hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
  1475. wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
  1476. "kernel driver");
  1477. return -1;
  1478. }
  1479. if (hapd->iconf->fragm_threshold > -1 &&
  1480. hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
  1481. wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
  1482. "for kernel driver");
  1483. return -1;
  1484. }
  1485. prev_addr = hapd->own_addr;
  1486. for (j = 0; j < iface->num_bss; j++) {
  1487. hapd = iface->bss[j];
  1488. if (j)
  1489. os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
  1490. if (hostapd_setup_bss(hapd, j == 0))
  1491. return -1;
  1492. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
  1493. prev_addr = hapd->own_addr;
  1494. }
  1495. hostapd_tx_queue_params(iface);
  1496. ap_list_init(iface);
  1497. if (hostapd_driver_commit(hapd) < 0) {
  1498. wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
  1499. "configuration", __func__);
  1500. return -1;
  1501. }
  1502. return ret;
  1503. }
  1504. /**
  1505. * hostapd_setup_interface - Setup of an interface
  1506. * @iface: Pointer to interface data.
  1507. * Returns: 0 on success, -1 on failure
  1508. *
  1509. * Initializes the driver interface, validates the configuration,
  1510. * and sets driver parameters based on the configuration.
  1511. * Flushes old stations, sets the channel, encryption,
  1512. * beacons, and WDS links based on the configuration.
  1513. */
  1514. static int hostapd_setup_interface(struct hostapd_iface *iface)
  1515. {
  1516. int ret;
  1517. ret = setup_interface(iface);
  1518. if (ret) {
  1519. wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
  1520. iface->bss[0]->conf->iface);
  1521. eloop_terminate();
  1522. return -1;
  1523. } else if (!hostapd_drv_none(iface->bss[0])) {
  1524. wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
  1525. iface->bss[0]->conf->iface);
  1526. }
  1527. return 0;
  1528. }
  1529. static void show_version(void)
  1530. {
  1531. fprintf(stderr,
  1532. "hostapd v" VERSION_STR "\n"
  1533. "User space daemon for IEEE 802.11 AP management,\n"
  1534. "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
  1535. "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
  1536. "and contributors\n");
  1537. }
  1538. static void usage(void)
  1539. {
  1540. show_version();
  1541. fprintf(stderr,
  1542. "\n"
  1543. "usage: hostapd [-hdBKtv] [-P <PID file>] "
  1544. "<configuration file(s)>\n"
  1545. "\n"
  1546. "options:\n"
  1547. " -h show this usage\n"
  1548. " -d show more debug messages (-dd for even more)\n"
  1549. " -B run daemon in the background\n"
  1550. " -P PID file\n"
  1551. " -K include key data in debug messages\n"
  1552. " -t include timestamps in some debug messages\n"
  1553. " -v show hostapd version\n");
  1554. exit(1);
  1555. }
  1556. /**
  1557. * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
  1558. * @hapd_iface: Pointer to interface data
  1559. * @conf: Pointer to per-interface configuration
  1560. * @bss: Pointer to per-BSS configuration for this BSS
  1561. * Returns: Pointer to allocated BSS data
  1562. *
  1563. * This function is used to allocate per-BSS data structure. This data will be
  1564. * freed after hostapd_cleanup() is called for it during interface
  1565. * deinitialization.
  1566. */
  1567. static struct hostapd_data *
  1568. hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
  1569. struct hostapd_config *conf,
  1570. struct hostapd_bss_config *bss)
  1571. {
  1572. struct hostapd_data *hapd;
  1573. hapd = os_zalloc(sizeof(*hapd));
  1574. if (hapd == NULL)
  1575. return NULL;
  1576. hapd->iconf = conf;
  1577. hapd->conf = bss;
  1578. hapd->iface = hapd_iface;
  1579. if (hapd->conf->individual_wep_key_len > 0) {
  1580. /* use key0 in individual key and key1 in broadcast key */
  1581. hapd->default_wep_key_idx = 1;
  1582. }
  1583. #ifdef EAP_TLS_FUNCS
  1584. if (hapd->conf->eap_server &&
  1585. (hapd->conf->ca_cert || hapd->conf->server_cert ||
  1586. hapd->conf->dh_file)) {
  1587. struct tls_connection_params params;
  1588. hapd->ssl_ctx = tls_init(NULL);
  1589. if (hapd->ssl_ctx == NULL) {
  1590. wpa_printf(MSG_ERROR, "Failed to initialize TLS");
  1591. goto fail;
  1592. }
  1593. os_memset(&params, 0, sizeof(params));
  1594. params.ca_cert = hapd->conf->ca_cert;
  1595. params.client_cert = hapd->conf->server_cert;
  1596. params.private_key = hapd->conf->private_key;
  1597. params.private_key_passwd = hapd->conf->private_key_passwd;
  1598. params.dh_file = hapd->conf->dh_file;
  1599. if (tls_global_set_params(hapd->ssl_ctx, &params)) {
  1600. wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
  1601. goto fail;
  1602. }
  1603. if (tls_global_set_verify(hapd->ssl_ctx,
  1604. hapd->conf->check_crl)) {
  1605. wpa_printf(MSG_ERROR, "Failed to enable check_crl");
  1606. goto fail;
  1607. }
  1608. }
  1609. #endif /* EAP_TLS_FUNCS */
  1610. #ifdef EAP_SERVER
  1611. if (hapd->conf->eap_sim_db) {
  1612. hapd->eap_sim_db_priv =
  1613. eap_sim_db_init(hapd->conf->eap_sim_db,
  1614. hostapd_sim_db_cb, hapd);
  1615. if (hapd->eap_sim_db_priv == NULL) {
  1616. wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
  1617. "database interface");
  1618. goto fail;
  1619. }
  1620. }
  1621. #endif /* EAP_SERVER */
  1622. hapd->driver = hapd->iconf->driver;
  1623. return hapd;
  1624. #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
  1625. fail:
  1626. #endif
  1627. /* TODO: cleanup allocated resources(?) */
  1628. os_free(hapd);
  1629. return NULL;
  1630. }
  1631. /**
  1632. * hostapd_init - Allocate and initialize per-interface data
  1633. * @config_file: Path to the configuration file
  1634. * Returns: Pointer to the allocated interface data or %NULL on failure
  1635. *
  1636. * This function is used to allocate main data structures for per-interface
  1637. * data. The allocated data buffer will be freed by calling
  1638. * hostapd_cleanup_iface().
  1639. */
  1640. static struct hostapd_iface * hostapd_init(const char *config_file)
  1641. {
  1642. struct hostapd_iface *hapd_iface = NULL;
  1643. struct hostapd_config *conf = NULL;
  1644. struct hostapd_data *hapd;
  1645. size_t i;
  1646. hapd_iface = os_zalloc(sizeof(*hapd_iface));
  1647. if (hapd_iface == NULL)
  1648. goto fail;
  1649. hapd_iface->config_fname = os_strdup(config_file);
  1650. if (hapd_iface->config_fname == NULL)
  1651. goto fail;
  1652. conf = hostapd_config_read(hapd_iface->config_fname);
  1653. if (conf == NULL)
  1654. goto fail;
  1655. hapd_iface->conf = conf;
  1656. hapd_iface->num_bss = conf->num_bss;
  1657. hapd_iface->bss = os_zalloc(conf->num_bss *
  1658. sizeof(struct hostapd_data *));
  1659. if (hapd_iface->bss == NULL)
  1660. goto fail;
  1661. for (i = 0; i < conf->num_bss; i++) {
  1662. hapd = hapd_iface->bss[i] =
  1663. hostapd_alloc_bss_data(hapd_iface, conf,
  1664. &conf->bss[i]);
  1665. if (hapd == NULL)
  1666. goto fail;
  1667. }
  1668. return hapd_iface;
  1669. fail:
  1670. if (conf)
  1671. hostapd_config_free(conf);
  1672. if (hapd_iface) {
  1673. for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
  1674. hapd = hapd_iface->bss[i];
  1675. if (hapd && hapd->ssl_ctx)
  1676. tls_deinit(hapd->ssl_ctx);
  1677. }
  1678. os_free(hapd_iface->config_fname);
  1679. os_free(hapd_iface->bss);
  1680. os_free(hapd_iface);
  1681. }
  1682. return NULL;
  1683. }
  1684. int main(int argc, char *argv[])
  1685. {
  1686. struct hapd_interfaces interfaces;
  1687. int ret = 1, k;
  1688. size_t i, j;
  1689. int c, debug = 0, daemonize = 0, tnc = 0;
  1690. const char *pid_file = NULL;
  1691. hostapd_logger_register_cb(hostapd_logger_cb);
  1692. for (;;) {
  1693. c = getopt(argc, argv, "BdhKP:tv");
  1694. if (c < 0)
  1695. break;
  1696. switch (c) {
  1697. case 'h':
  1698. usage();
  1699. break;
  1700. case 'd':
  1701. debug++;
  1702. if (wpa_debug_level > 0)
  1703. wpa_debug_level--;
  1704. break;
  1705. case 'B':
  1706. daemonize++;
  1707. break;
  1708. case 'K':
  1709. wpa_debug_show_keys++;
  1710. break;
  1711. case 'P':
  1712. pid_file = optarg;
  1713. break;
  1714. case 't':
  1715. wpa_debug_timestamp++;
  1716. break;
  1717. case 'v':
  1718. show_version();
  1719. exit(1);
  1720. break;
  1721. default:
  1722. usage();
  1723. break;
  1724. }
  1725. }
  1726. if (optind == argc)
  1727. usage();
  1728. if (eap_server_register_methods()) {
  1729. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  1730. return -1;
  1731. }
  1732. interfaces.count = argc - optind;
  1733. interfaces.iface = os_malloc(interfaces.count *
  1734. sizeof(struct hostapd_iface *));
  1735. if (interfaces.iface == NULL) {
  1736. wpa_printf(MSG_ERROR, "malloc failed\n");
  1737. return -1;
  1738. }
  1739. if (eloop_init(&interfaces)) {
  1740. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  1741. return -1;
  1742. }
  1743. #ifndef CONFIG_NATIVE_WINDOWS
  1744. eloop_register_signal(SIGHUP, handle_reload, NULL);
  1745. eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
  1746. #endif /* CONFIG_NATIVE_WINDOWS */
  1747. eloop_register_signal_terminate(handle_term, NULL);
  1748. /* Initialize interfaces */
  1749. for (i = 0; i < interfaces.count; i++) {
  1750. wpa_printf(MSG_ERROR, "Configuration file: %s",
  1751. argv[optind + i]);
  1752. interfaces.iface[i] = hostapd_init(argv[optind + i]);
  1753. if (!interfaces.iface[i])
  1754. goto out;
  1755. for (k = 0; k < debug; k++) {
  1756. if (interfaces.iface[i]->bss[0]->conf->
  1757. logger_stdout_level > 0)
  1758. interfaces.iface[i]->bss[0]->conf->
  1759. logger_stdout_level--;
  1760. }
  1761. ret = hostapd_setup_interface(interfaces.iface[i]);
  1762. if (ret)
  1763. goto out;
  1764. for (k = 0; k < (int) interfaces.iface[i]->num_bss; k++) {
  1765. if (interfaces.iface[i]->bss[0]->conf->tnc)
  1766. tnc++;
  1767. }
  1768. }
  1769. #ifdef EAP_TNC
  1770. if (tnc && tncs_global_init() < 0) {
  1771. wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
  1772. goto out;
  1773. }
  1774. #endif /* EAP_TNC */
  1775. if (daemonize && os_daemonize(pid_file)) {
  1776. perror("daemon");
  1777. goto out;
  1778. }
  1779. #ifndef CONFIG_NATIVE_WINDOWS
  1780. openlog("hostapd", 0, LOG_DAEMON);
  1781. #endif /* CONFIG_NATIVE_WINDOWS */
  1782. eloop_run();
  1783. /* Disconnect associated stations from all interfaces and BSSes */
  1784. for (i = 0; i < interfaces.count; i++) {
  1785. for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
  1786. struct hostapd_data *hapd =
  1787. interfaces.iface[i]->bss[j];
  1788. hostapd_free_stas(hapd);
  1789. hostapd_flush_old_stations(hapd);
  1790. }
  1791. }
  1792. ret = 0;
  1793. out:
  1794. /* Deinitialize all interfaces */
  1795. for (i = 0; i < interfaces.count; i++) {
  1796. if (!interfaces.iface[i])
  1797. continue;
  1798. hostapd_cleanup_iface_pre(interfaces.iface[i]);
  1799. for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
  1800. struct hostapd_data *hapd =
  1801. interfaces.iface[i]->bss[j];
  1802. hostapd_cleanup(hapd);
  1803. if (j == interfaces.iface[i]->num_bss - 1 &&
  1804. hapd->driver)
  1805. hostapd_driver_deinit(hapd);
  1806. }
  1807. for (j = 0; j < interfaces.iface[i]->num_bss; j++)
  1808. os_free(interfaces.iface[i]->bss[j]);
  1809. hostapd_cleanup_iface(interfaces.iface[i]);
  1810. }
  1811. os_free(interfaces.iface);
  1812. #ifdef EAP_TNC
  1813. tncs_global_deinit();
  1814. #endif /* EAP_TNC */
  1815. eloop_destroy();
  1816. #ifndef CONFIG_NATIVE_WINDOWS
  1817. closelog();
  1818. #endif /* CONFIG_NATIVE_WINDOWS */
  1819. eap_server_unregister_methods();
  1820. os_daemonize_terminate(pid_file);
  1821. return ret;
  1822. }