hostapd.c 51 KB

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