hostapd.c 49 KB

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