hostapd.c 50 KB

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