hostapd.c 49 KB

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