hostapd.c 49 KB

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