hostapd.c 51 KB

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