hostapd.c 53 KB

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