hostapd.c 51 KB

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