hostapd.c 56 KB

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