hostapd.c 49 KB

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