hostapd.c 50 KB

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