hostapd.c 52 KB

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