hostapd.c 52 KB

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