hostapd.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  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. #include "eloop.h"
  16. #include "hostapd.h"
  17. #include "ieee802_1x.h"
  18. #include "beacon.h"
  19. #include "hw_features.h"
  20. #include "accounting.h"
  21. #include "eapol_sm.h"
  22. #include "iapp.h"
  23. #include "ieee802_11_defs.h"
  24. #include "ieee802_11_auth.h"
  25. #include "sta_info.h"
  26. #include "ap_list.h"
  27. #include "driver_i.h"
  28. #include "radius/radius_client.h"
  29. #include "radius/radius_server.h"
  30. #include "wpa.h"
  31. #include "preauth.h"
  32. #include "vlan_init.h"
  33. #include "ctrl_iface.h"
  34. #include "tls.h"
  35. #include "eap_server/eap_sim_db.h"
  36. #include "eap_server/eap.h"
  37. #include "eap_server/tncs.h"
  38. #include "version.h"
  39. #include "l2_packet/l2_packet.h"
  40. #include "wps_hostapd.h"
  41. #include "tkip_countermeasures.h"
  42. static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
  43. size_t identity_len, int phase2,
  44. struct eap_user *user);
  45. static int hostapd_flush_old_stations(struct hostapd_data *hapd);
  46. static int hostapd_setup_wpa(struct hostapd_data *hapd);
  47. static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
  48. extern int wpa_debug_level;
  49. #ifdef EAP_SERVER
  50. static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
  51. struct sta_info *sta, void *ctx)
  52. {
  53. if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
  54. return 1;
  55. return 0;
  56. }
  57. static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
  58. {
  59. struct hostapd_data *hapd = ctx;
  60. if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
  61. radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
  62. }
  63. #endif /* EAP_SERVER */
  64. static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
  65. struct wpa_auth_config *wconf)
  66. {
  67. wconf->wpa = conf->wpa;
  68. wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
  69. wconf->wpa_pairwise = conf->wpa_pairwise;
  70. wconf->wpa_group = conf->wpa_group;
  71. wconf->wpa_group_rekey = conf->wpa_group_rekey;
  72. wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
  73. wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
  74. wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
  75. wconf->rsn_pairwise = conf->rsn_pairwise;
  76. wconf->rsn_preauth = conf->rsn_preauth;
  77. wconf->eapol_version = conf->eapol_version;
  78. wconf->peerkey = conf->peerkey;
  79. wconf->wmm_enabled = conf->wmm_enabled;
  80. wconf->okc = conf->okc;
  81. #ifdef CONFIG_IEEE80211W
  82. wconf->ieee80211w = conf->ieee80211w;
  83. #endif /* CONFIG_IEEE80211W */
  84. #ifdef CONFIG_IEEE80211R
  85. wconf->ssid_len = conf->ssid.ssid_len;
  86. if (wconf->ssid_len > SSID_LEN)
  87. wconf->ssid_len = SSID_LEN;
  88. os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
  89. os_memcpy(wconf->mobility_domain, conf->mobility_domain,
  90. MOBILITY_DOMAIN_ID_LEN);
  91. if (conf->nas_identifier &&
  92. os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
  93. wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
  94. os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
  95. wconf->r0_key_holder_len);
  96. }
  97. os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
  98. wconf->r0_key_lifetime = conf->r0_key_lifetime;
  99. wconf->reassociation_deadline = conf->reassociation_deadline;
  100. wconf->r0kh_list = conf->r0kh_list;
  101. wconf->r1kh_list = conf->r1kh_list;
  102. wconf->pmk_r1_push = conf->pmk_r1_push;
  103. #endif /* CONFIG_IEEE80211R */
  104. }
  105. int hostapd_reload_config(struct hostapd_iface *iface)
  106. {
  107. struct hostapd_data *hapd = iface->bss[0];
  108. struct hostapd_config *newconf, *oldconf;
  109. struct wpa_auth_config wpa_auth_conf;
  110. size_t j;
  111. newconf = hostapd_config_read(iface->config_fname);
  112. if (newconf == NULL)
  113. return -1;
  114. /*
  115. * Deauthenticate all stations since the new configuration may not
  116. * allow them to use the BSS anymore.
  117. */
  118. for (j = 0; j < iface->num_bss; j++)
  119. hostapd_flush_old_stations(iface->bss[j]);
  120. /* TODO: update dynamic data based on changed configuration
  121. * items (e.g., open/close sockets, etc.) */
  122. radius_client_flush(hapd->radius, 0);
  123. oldconf = hapd->iconf;
  124. hapd->iconf = newconf;
  125. hapd->conf = &newconf->bss[0];
  126. iface->conf = newconf;
  127. if (hostapd_setup_wpa_psk(hapd->conf)) {
  128. wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
  129. "after reloading configuration");
  130. }
  131. if (hapd->conf->wpa && hapd->wpa_auth == NULL)
  132. hostapd_setup_wpa(hapd);
  133. else if (hapd->conf->wpa) {
  134. hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
  135. wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
  136. } else if (hapd->wpa_auth) {
  137. wpa_deinit(hapd->wpa_auth);
  138. hapd->wpa_auth = NULL;
  139. hostapd_set_privacy(hapd, 0);
  140. hostapd_setup_encryption(hapd->conf->iface, hapd);
  141. }
  142. ieee802_11_set_beacon(hapd);
  143. if (hapd->conf->ssid.ssid_set &&
  144. hostapd_set_ssid(hapd, (u8 *) hapd->conf->ssid.ssid,
  145. hapd->conf->ssid.ssid_len)) {
  146. wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
  147. /* try to continue */
  148. }
  149. if (hapd->conf->ieee802_1x || hapd->conf->wpa)
  150. hostapd_set_ieee8021x(hapd->conf->iface, hapd, 1);
  151. hostapd_config_free(oldconf);
  152. wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
  153. return 0;
  154. }
  155. int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
  156. {
  157. if (hostapd_reload_config(iface) < 0) {
  158. wpa_printf(MSG_WARNING, "Failed to read new configuration "
  159. "file - continuing with old.");
  160. }
  161. return 0;
  162. }
  163. #ifdef HOSTAPD_DUMP_STATE
  164. /**
  165. * hostapd_dump_state - SIGUSR1 handler to dump hostapd state to a text file
  166. */
  167. static void hostapd_dump_state(struct hostapd_data *hapd)
  168. {
  169. FILE *f;
  170. time_t now;
  171. struct sta_info *sta;
  172. int i;
  173. char *buf;
  174. if (!hapd->conf->dump_log_name) {
  175. wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
  176. "request");
  177. return;
  178. }
  179. wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
  180. hapd->conf->dump_log_name);
  181. f = fopen(hapd->conf->dump_log_name, "w");
  182. if (f == NULL) {
  183. wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
  184. "writing.", hapd->conf->dump_log_name);
  185. return;
  186. }
  187. time(&now);
  188. fprintf(f, "hostapd state dump - %s", ctime(&now));
  189. fprintf(f, "num_sta=%d num_sta_non_erp=%d "
  190. "num_sta_no_short_slot_time=%d\n"
  191. "num_sta_no_short_preamble=%d\n",
  192. hapd->num_sta, hapd->iface->num_sta_non_erp,
  193. hapd->iface->num_sta_no_short_slot_time,
  194. hapd->iface->num_sta_no_short_preamble);
  195. for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
  196. fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
  197. fprintf(f,
  198. " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
  199. " capability=0x%x listen_interval=%d\n",
  200. sta->aid,
  201. sta->flags,
  202. (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
  203. (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
  204. (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
  205. (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
  206. (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
  207. (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
  208. ""),
  209. (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
  210. ""),
  211. (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
  212. "[SHORT_PREAMBLE]" : ""),
  213. (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
  214. (sta->flags & WLAN_STA_WMM ? "[WMM]" : ""),
  215. (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
  216. (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
  217. (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
  218. (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
  219. sta->capability,
  220. sta->listen_interval);
  221. fprintf(f, " supported_rates=");
  222. for (i = 0; i < sta->supported_rates_len; i++)
  223. fprintf(f, "%02x ", sta->supported_rates[i]);
  224. fprintf(f, "\n");
  225. fprintf(f,
  226. " timeout_next=%s\n",
  227. (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
  228. (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
  229. "DEAUTH")));
  230. ieee802_1x_dump_state(f, " ", sta);
  231. }
  232. buf = os_malloc(4096);
  233. if (buf) {
  234. int count = radius_client_get_mib(hapd->radius, buf, 4096);
  235. if (count < 0)
  236. count = 0;
  237. else if (count > 4095)
  238. count = 4095;
  239. buf[count] = '\0';
  240. fprintf(f, "%s", buf);
  241. count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
  242. if (count < 0)
  243. count = 0;
  244. else if (count > 4095)
  245. count = 4095;
  246. buf[count] = '\0';
  247. fprintf(f, "%s", buf);
  248. os_free(buf);
  249. }
  250. fclose(f);
  251. }
  252. int handle_dump_state_iface(struct hostapd_iface *iface, void *ctx)
  253. {
  254. size_t i;
  255. for (i = 0; i < iface->num_bss; i++)
  256. hostapd_dump_state(iface->bss[i]);
  257. return 0;
  258. }
  259. #endif /* HOSTAPD_DUMP_STATE */
  260. static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
  261. char *ifname)
  262. {
  263. int i;
  264. for (i = 0; i < NUM_WEP_KEYS; i++) {
  265. if (hostapd_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
  266. i == 0 ? 1 : 0, NULL, 0, NULL, 0)) {
  267. wpa_printf(MSG_DEBUG, "Failed to clear default "
  268. "encryption keys (ifname=%s keyidx=%d)",
  269. ifname, i);
  270. }
  271. }
  272. #ifdef CONFIG_IEEE80211W
  273. if (hapd->conf->ieee80211w) {
  274. for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
  275. if (hostapd_set_key(ifname, hapd, WPA_ALG_NONE, NULL,
  276. i, i == 0 ? 1 : 0, NULL, 0,
  277. NULL, 0)) {
  278. wpa_printf(MSG_DEBUG, "Failed to clear "
  279. "default mgmt encryption keys "
  280. "(ifname=%s keyidx=%d)", ifname, i);
  281. }
  282. }
  283. }
  284. #endif /* CONFIG_IEEE80211W */
  285. }
  286. static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
  287. {
  288. hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
  289. return 0;
  290. }
  291. static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
  292. {
  293. int errors = 0, idx;
  294. struct hostapd_ssid *ssid = &hapd->conf->ssid;
  295. idx = ssid->wep.idx;
  296. if (ssid->wep.default_len &&
  297. hostapd_set_key(hapd->conf->iface,
  298. hapd, WPA_ALG_WEP, NULL, idx, idx == ssid->wep.idx,
  299. NULL, 0, ssid->wep.key[idx], ssid->wep.len[idx])) {
  300. wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
  301. errors++;
  302. }
  303. if (ssid->dyn_vlan_keys) {
  304. size_t i;
  305. for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
  306. const char *ifname;
  307. struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
  308. if (key == NULL)
  309. continue;
  310. ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
  311. i);
  312. if (ifname == NULL)
  313. continue;
  314. idx = key->idx;
  315. if (hostapd_set_key(ifname, hapd, WPA_ALG_WEP, NULL,
  316. idx, idx == key->idx, NULL, 0,
  317. key->key[idx], key->len[idx])) {
  318. wpa_printf(MSG_WARNING, "Could not set "
  319. "dynamic VLAN WEP encryption.");
  320. errors++;
  321. }
  322. }
  323. }
  324. return errors;
  325. }
  326. /**
  327. * hostapd_cleanup - Per-BSS cleanup (deinitialization)
  328. * @hapd: Pointer to BSS data
  329. *
  330. * This function is used to free all per-BSS data structures and resources.
  331. * This gets called in a loop for each BSS between calls to
  332. * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
  333. * is deinitialized. Most of the modules that are initialized in
  334. * hostapd_setup_bss() are deinitialized here.
  335. */
  336. static void hostapd_cleanup(struct hostapd_data *hapd)
  337. {
  338. hostapd_ctrl_iface_deinit(hapd);
  339. iapp_deinit(hapd->iapp);
  340. hapd->iapp = NULL;
  341. accounting_deinit(hapd);
  342. rsn_preauth_iface_deinit(hapd);
  343. if (hapd->wpa_auth) {
  344. wpa_deinit(hapd->wpa_auth);
  345. hapd->wpa_auth = NULL;
  346. if (hostapd_set_privacy(hapd, 0)) {
  347. wpa_printf(MSG_DEBUG, "Could not disable "
  348. "PrivacyInvoked for interface %s",
  349. hapd->conf->iface);
  350. }
  351. if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
  352. wpa_printf(MSG_DEBUG, "Could not remove generic "
  353. "information element from interface %s",
  354. hapd->conf->iface);
  355. }
  356. }
  357. ieee802_1x_deinit(hapd);
  358. vlan_deinit(hapd);
  359. hostapd_acl_deinit(hapd);
  360. radius_client_deinit(hapd->radius);
  361. hapd->radius = NULL;
  362. radius_server_deinit(hapd->radius_srv);
  363. hapd->radius_srv = NULL;
  364. #ifdef CONFIG_IEEE80211R
  365. l2_packet_deinit(hapd->l2);
  366. #endif /* CONFIG_IEEE80211R */
  367. hostapd_deinit_wps(hapd);
  368. #ifdef EAP_TLS_FUNCS
  369. if (hapd->ssl_ctx) {
  370. tls_deinit(hapd->ssl_ctx);
  371. hapd->ssl_ctx = NULL;
  372. }
  373. #endif /* EAP_TLS_FUNCS */
  374. #ifdef EAP_SERVER
  375. if (hapd->eap_sim_db_priv) {
  376. eap_sim_db_deinit(hapd->eap_sim_db_priv);
  377. hapd->eap_sim_db_priv = NULL;
  378. }
  379. #endif /* EAP_SERVER */
  380. if (hapd->interface_added &&
  381. hostapd_bss_remove(hapd, hapd->conf->iface)) {
  382. wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
  383. hapd->conf->iface);
  384. }
  385. os_free(hapd->probereq_cb);
  386. hapd->probereq_cb = NULL;
  387. }
  388. /**
  389. * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
  390. * @iface: Pointer to interface data
  391. *
  392. * This function is called before per-BSS data structures are deinitialized
  393. * with hostapd_cleanup().
  394. */
  395. static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
  396. {
  397. }
  398. /**
  399. * hostapd_cleanup_iface - Complete per-interface cleanup
  400. * @iface: Pointer to interface data
  401. *
  402. * This function is called after per-BSS data structures are deinitialized
  403. * with hostapd_cleanup().
  404. */
  405. static void hostapd_cleanup_iface(struct hostapd_iface *iface)
  406. {
  407. hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
  408. iface->hw_features = NULL;
  409. os_free(iface->current_rates);
  410. iface->current_rates = NULL;
  411. ap_list_deinit(iface);
  412. hostapd_config_free(iface->conf);
  413. iface->conf = NULL;
  414. os_free(iface->config_fname);
  415. os_free(iface->bss);
  416. os_free(iface);
  417. }
  418. static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
  419. {
  420. int i;
  421. hostapd_broadcast_wep_set(hapd);
  422. if (hapd->conf->ssid.wep.default_len)
  423. return 0;
  424. for (i = 0; i < 4; i++) {
  425. if (hapd->conf->ssid.wep.key[i] &&
  426. hostapd_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
  427. i == hapd->conf->ssid.wep.idx, NULL, 0,
  428. hapd->conf->ssid.wep.key[i],
  429. hapd->conf->ssid.wep.len[i])) {
  430. wpa_printf(MSG_WARNING, "Could not set WEP "
  431. "encryption.");
  432. return -1;
  433. }
  434. if (hapd->conf->ssid.wep.key[i] &&
  435. i == hapd->conf->ssid.wep.idx)
  436. hostapd_set_privacy(hapd, 1);
  437. }
  438. return 0;
  439. }
  440. static int hostapd_flush_old_stations(struct hostapd_data *hapd)
  441. {
  442. int ret = 0;
  443. if (hostapd_drv_none(hapd))
  444. return 0;
  445. wpa_printf(MSG_DEBUG, "Flushing old station entries");
  446. if (hostapd_flush(hapd)) {
  447. wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
  448. ret = -1;
  449. }
  450. wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
  451. /* New Prism2.5/3 STA firmware versions seem to have issues with this
  452. * broadcast deauth frame. This gets the firmware in odd state where
  453. * nothing works correctly, so let's skip sending this for the hostap
  454. * driver. */
  455. if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
  456. u8 addr[ETH_ALEN];
  457. os_memset(addr, 0xff, ETH_ALEN);
  458. hostapd_sta_deauth(hapd, addr,
  459. WLAN_REASON_PREV_AUTH_NOT_VALID);
  460. }
  461. return ret;
  462. }
  463. static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
  464. logger_level level, const char *txt)
  465. {
  466. #ifndef CONFIG_NO_HOSTAPD_LOGGER
  467. struct hostapd_data *hapd = ctx;
  468. int hlevel;
  469. switch (level) {
  470. case LOGGER_WARNING:
  471. hlevel = HOSTAPD_LEVEL_WARNING;
  472. break;
  473. case LOGGER_INFO:
  474. hlevel = HOSTAPD_LEVEL_INFO;
  475. break;
  476. case LOGGER_DEBUG:
  477. default:
  478. hlevel = HOSTAPD_LEVEL_DEBUG;
  479. break;
  480. }
  481. hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
  482. #endif /* CONFIG_NO_HOSTAPD_LOGGER */
  483. }
  484. static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
  485. u16 reason)
  486. {
  487. struct hostapd_data *hapd = ctx;
  488. struct sta_info *sta;
  489. wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
  490. "STA " MACSTR " reason %d",
  491. __func__, MAC2STR(addr), reason);
  492. sta = ap_get_sta(hapd, addr);
  493. hostapd_sta_deauth(hapd, addr, reason);
  494. if (sta == NULL)
  495. return;
  496. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
  497. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  498. eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
  499. sta->timeout_next = STA_REMOVE;
  500. }
  501. static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
  502. {
  503. struct hostapd_data *hapd = ctx;
  504. michael_mic_failure(hapd, addr, 0);
  505. }
  506. static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
  507. wpa_eapol_variable var, int value)
  508. {
  509. struct hostapd_data *hapd = ctx;
  510. struct sta_info *sta = ap_get_sta(hapd, addr);
  511. if (sta == NULL)
  512. return;
  513. switch (var) {
  514. case WPA_EAPOL_portEnabled:
  515. ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
  516. break;
  517. case WPA_EAPOL_portValid:
  518. ieee802_1x_notify_port_valid(sta->eapol_sm, value);
  519. break;
  520. case WPA_EAPOL_authorized:
  521. ieee802_1x_set_sta_authorized(hapd, sta, value);
  522. break;
  523. case WPA_EAPOL_portControl_Auto:
  524. if (sta->eapol_sm)
  525. sta->eapol_sm->portControl = Auto;
  526. break;
  527. case WPA_EAPOL_keyRun:
  528. if (sta->eapol_sm)
  529. sta->eapol_sm->keyRun = value ? TRUE : FALSE;
  530. break;
  531. case WPA_EAPOL_keyAvailable:
  532. if (sta->eapol_sm)
  533. sta->eapol_sm->eap_if->eapKeyAvailable =
  534. value ? TRUE : FALSE;
  535. break;
  536. case WPA_EAPOL_keyDone:
  537. if (sta->eapol_sm)
  538. sta->eapol_sm->keyDone = value ? TRUE : FALSE;
  539. break;
  540. case WPA_EAPOL_inc_EapolFramesTx:
  541. if (sta->eapol_sm)
  542. sta->eapol_sm->dot1xAuthEapolFramesTx++;
  543. break;
  544. }
  545. }
  546. static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
  547. wpa_eapol_variable var)
  548. {
  549. struct hostapd_data *hapd = ctx;
  550. struct sta_info *sta = ap_get_sta(hapd, addr);
  551. if (sta == NULL || sta->eapol_sm == NULL)
  552. return -1;
  553. switch (var) {
  554. case WPA_EAPOL_keyRun:
  555. return sta->eapol_sm->keyRun;
  556. case WPA_EAPOL_keyAvailable:
  557. return sta->eapol_sm->eap_if->eapKeyAvailable;
  558. default:
  559. return -1;
  560. }
  561. }
  562. static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
  563. const u8 *prev_psk)
  564. {
  565. struct hostapd_data *hapd = ctx;
  566. return hostapd_get_psk(hapd->conf, addr, prev_psk);
  567. }
  568. static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
  569. size_t *len)
  570. {
  571. struct hostapd_data *hapd = ctx;
  572. const u8 *key;
  573. size_t keylen;
  574. struct sta_info *sta;
  575. sta = ap_get_sta(hapd, addr);
  576. if (sta == NULL)
  577. return -1;
  578. key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
  579. if (key == NULL)
  580. return -1;
  581. if (keylen > *len)
  582. keylen = *len;
  583. os_memcpy(msk, key, keylen);
  584. *len = keylen;
  585. return 0;
  586. }
  587. static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, wpa_alg alg,
  588. const u8 *addr, int idx, u8 *key,
  589. size_t key_len)
  590. {
  591. struct hostapd_data *hapd = ctx;
  592. const char *ifname = hapd->conf->iface;
  593. if (vlan_id > 0) {
  594. ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
  595. if (ifname == NULL)
  596. return -1;
  597. }
  598. return hostapd_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
  599. key, key_len);
  600. }
  601. static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
  602. u8 *seq)
  603. {
  604. struct hostapd_data *hapd = ctx;
  605. return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
  606. }
  607. static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
  608. u8 *seq)
  609. {
  610. struct hostapd_data *hapd = ctx;
  611. return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
  612. seq);
  613. }
  614. static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
  615. const u8 *data, size_t data_len,
  616. int encrypt)
  617. {
  618. struct hostapd_data *hapd = ctx;
  619. return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
  620. }
  621. static int hostapd_wpa_auth_for_each_sta(
  622. void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
  623. void *cb_ctx)
  624. {
  625. struct hostapd_data *hapd = ctx;
  626. struct sta_info *sta;
  627. for (sta = hapd->sta_list; sta; sta = sta->next) {
  628. if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
  629. return 1;
  630. }
  631. return 0;
  632. }
  633. struct wpa_auth_iface_iter_data {
  634. int (*cb)(struct wpa_authenticator *sm, void *ctx);
  635. void *cb_ctx;
  636. };
  637. static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
  638. {
  639. struct wpa_auth_iface_iter_data *data = ctx;
  640. size_t i;
  641. for (i = 0; i < iface->num_bss; i++) {
  642. if (data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
  643. return 1;
  644. }
  645. return 0;
  646. }
  647. static int hostapd_wpa_auth_for_each_auth(
  648. void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
  649. void *cb_ctx)
  650. {
  651. struct wpa_auth_iface_iter_data data;
  652. data.cb = cb;
  653. data.cb_ctx = cb_ctx;
  654. return hostapd_for_each_interface(wpa_auth_iface_iter, &data);
  655. }
  656. static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
  657. const u8 *data, size_t data_len)
  658. {
  659. struct hostapd_data *hapd = ctx;
  660. if (hapd->driver && hapd->driver->send_ether)
  661. return hapd->driver->send_ether(hapd->drv_priv, dst,
  662. hapd->own_addr, proto,
  663. data, data_len);
  664. if (hapd->l2 == NULL)
  665. return -1;
  666. return l2_packet_send(hapd->l2, dst, proto, data, data_len);
  667. }
  668. #ifdef CONFIG_IEEE80211R
  669. static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
  670. const u8 *data, size_t data_len)
  671. {
  672. struct hostapd_data *hapd = ctx;
  673. int res;
  674. struct ieee80211_mgmt *m;
  675. size_t mlen;
  676. struct sta_info *sta;
  677. sta = ap_get_sta(hapd, dst);
  678. if (sta == NULL || sta->wpa_sm == NULL)
  679. return -1;
  680. m = os_zalloc(sizeof(*m) + data_len);
  681. if (m == NULL)
  682. return -1;
  683. mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
  684. m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  685. WLAN_FC_STYPE_ACTION);
  686. os_memcpy(m->da, dst, ETH_ALEN);
  687. os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
  688. os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
  689. os_memcpy(&m->u, data, data_len);
  690. res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen);
  691. os_free(m);
  692. return res;
  693. }
  694. static struct wpa_state_machine *
  695. hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
  696. {
  697. struct hostapd_data *hapd = ctx;
  698. struct sta_info *sta;
  699. sta = ap_sta_add(hapd, sta_addr);
  700. if (sta == NULL)
  701. return NULL;
  702. if (sta->wpa_sm)
  703. return sta->wpa_sm;
  704. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
  705. if (sta->wpa_sm == NULL) {
  706. ap_free_sta(hapd, sta);
  707. return NULL;
  708. }
  709. sta->auth_alg = WLAN_AUTH_FT;
  710. return sta->wpa_sm;
  711. }
  712. static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
  713. size_t len)
  714. {
  715. struct hostapd_data *hapd = ctx;
  716. wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
  717. }
  718. #endif /* CONFIG_IEEE80211R */
  719. /**
  720. * hostapd_validate_bssid_configuration - Validate BSSID configuration
  721. * @iface: Pointer to interface data
  722. * Returns: 0 on success, -1 on failure
  723. *
  724. * This function is used to validate that the configured BSSIDs are valid.
  725. */
  726. static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
  727. {
  728. u8 mask[ETH_ALEN] = { 0 };
  729. struct hostapd_data *hapd = iface->bss[0];
  730. unsigned int i = iface->conf->num_bss, bits = 0, j;
  731. int res;
  732. int auto_addr = 0;
  733. if (hostapd_drv_none(hapd))
  734. return 0;
  735. /* Generate BSSID mask that is large enough to cover the BSSIDs. */
  736. /* Determine the bits necessary to cover the number of BSSIDs. */
  737. for (i--; i; i >>= 1)
  738. bits++;
  739. /* Determine the bits necessary to any configured BSSIDs,
  740. if they are higher than the number of BSSIDs. */
  741. for (j = 0; j < iface->conf->num_bss; j++) {
  742. if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
  743. if (j)
  744. auto_addr++;
  745. continue;
  746. }
  747. for (i = 0; i < ETH_ALEN; i++) {
  748. mask[i] |=
  749. iface->conf->bss[j].bssid[i] ^
  750. hapd->own_addr[i];
  751. }
  752. }
  753. if (!auto_addr)
  754. goto skip_mask_ext;
  755. for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
  756. ;
  757. j = 0;
  758. if (i < ETH_ALEN) {
  759. j = (5 - i) * 8;
  760. while (mask[i] != 0) {
  761. mask[i] >>= 1;
  762. j++;
  763. }
  764. }
  765. if (bits < j)
  766. bits = j;
  767. if (bits > 40) {
  768. wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
  769. bits);
  770. return -1;
  771. }
  772. os_memset(mask, 0xff, ETH_ALEN);
  773. j = bits / 8;
  774. for (i = 5; i > 5 - j; i--)
  775. mask[i] = 0;
  776. j = bits % 8;
  777. while (j--)
  778. mask[i] <<= 1;
  779. skip_mask_ext:
  780. wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
  781. (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
  782. res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
  783. if (res == 0)
  784. return 0;
  785. if (res < 0) {
  786. wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
  787. MACSTR " for start address " MACSTR ".",
  788. MAC2STR(mask), MAC2STR(hapd->own_addr));
  789. return -1;
  790. }
  791. if (!auto_addr)
  792. return 0;
  793. for (i = 0; i < ETH_ALEN; i++) {
  794. if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
  795. wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
  796. " for start address " MACSTR ".",
  797. MAC2STR(mask), MAC2STR(hapd->own_addr));
  798. wpa_printf(MSG_ERROR, "Start address must be the "
  799. "first address in the block (i.e., addr "
  800. "AND mask == addr).");
  801. return -1;
  802. }
  803. }
  804. return 0;
  805. }
  806. static int mac_in_conf(struct hostapd_config *conf, const void *a)
  807. {
  808. size_t i;
  809. for (i = 0; i < conf->num_bss; i++) {
  810. if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
  811. return 1;
  812. }
  813. }
  814. return 0;
  815. }
  816. static int hostapd_setup_wpa(struct hostapd_data *hapd)
  817. {
  818. struct wpa_auth_config _conf;
  819. struct wpa_auth_callbacks cb;
  820. const u8 *wpa_ie;
  821. size_t wpa_ie_len;
  822. hostapd_wpa_auth_conf(hapd->conf, &_conf);
  823. os_memset(&cb, 0, sizeof(cb));
  824. cb.ctx = hapd;
  825. cb.logger = hostapd_wpa_auth_logger;
  826. cb.disconnect = hostapd_wpa_auth_disconnect;
  827. cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
  828. cb.set_eapol = hostapd_wpa_auth_set_eapol;
  829. cb.get_eapol = hostapd_wpa_auth_get_eapol;
  830. cb.get_psk = hostapd_wpa_auth_get_psk;
  831. cb.get_msk = hostapd_wpa_auth_get_msk;
  832. cb.set_key = hostapd_wpa_auth_set_key;
  833. cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
  834. cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
  835. cb.send_eapol = hostapd_wpa_auth_send_eapol;
  836. cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
  837. cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
  838. cb.send_ether = hostapd_wpa_auth_send_ether;
  839. #ifdef CONFIG_IEEE80211R
  840. cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
  841. cb.add_sta = hostapd_wpa_auth_add_sta;
  842. #endif /* CONFIG_IEEE80211R */
  843. hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
  844. if (hapd->wpa_auth == NULL) {
  845. wpa_printf(MSG_ERROR, "WPA initialization failed.");
  846. return -1;
  847. }
  848. if (hostapd_set_privacy(hapd, 1)) {
  849. wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
  850. "for interface %s", hapd->conf->iface);
  851. return -1;
  852. }
  853. wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
  854. if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
  855. wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
  856. "the kernel driver.");
  857. return -1;
  858. }
  859. if (rsn_preauth_iface_init(hapd)) {
  860. wpa_printf(MSG_ERROR, "Initialization of RSN "
  861. "pre-authentication failed.");
  862. return -1;
  863. }
  864. return 0;
  865. }
  866. static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
  867. struct hostapd_bss_config *conf)
  868. {
  869. struct radius_server_conf srv;
  870. os_memset(&srv, 0, sizeof(srv));
  871. srv.client_file = conf->radius_server_clients;
  872. srv.auth_port = conf->radius_server_auth_port;
  873. srv.conf_ctx = conf;
  874. srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
  875. srv.ssl_ctx = hapd->ssl_ctx;
  876. srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
  877. srv.eap_fast_a_id = conf->eap_fast_a_id;
  878. srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
  879. srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
  880. srv.eap_fast_prov = conf->eap_fast_prov;
  881. srv.pac_key_lifetime = conf->pac_key_lifetime;
  882. srv.pac_key_refresh_time = conf->pac_key_refresh_time;
  883. srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
  884. srv.tnc = conf->tnc;
  885. srv.wps = hapd->wps;
  886. srv.ipv6 = conf->radius_server_ipv6;
  887. srv.get_eap_user = hostapd_radius_get_eap_user;
  888. srv.eap_req_id_text = conf->eap_req_id_text;
  889. srv.eap_req_id_text_len = conf->eap_req_id_text_len;
  890. hapd->radius_srv = radius_server_init(&srv);
  891. if (hapd->radius_srv == NULL) {
  892. wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
  893. return -1;
  894. }
  895. return 0;
  896. }
  897. /**
  898. * hostapd_setup_bss - Per-BSS setup (initialization)
  899. * @hapd: Pointer to BSS data
  900. * @first: Whether this BSS is the first BSS of an interface
  901. *
  902. * This function is used to initialize all per-BSS data structures and
  903. * resources. This gets called in a loop for each BSS when an interface is
  904. * initialized. Most of the modules that are initialized here will be
  905. * deinitialized in hostapd_cleanup().
  906. */
  907. static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
  908. {
  909. struct hostapd_bss_config *conf = hapd->conf;
  910. u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
  911. int ssid_len, set_ssid;
  912. if (!first) {
  913. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
  914. /* Allocate the next available BSSID. */
  915. do {
  916. inc_byte_array(hapd->own_addr, ETH_ALEN);
  917. } while (mac_in_conf(hapd->iconf, hapd->own_addr));
  918. } else {
  919. /* Allocate the configured BSSID. */
  920. os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
  921. if (hostapd_mac_comp(hapd->own_addr,
  922. hapd->iface->bss[0]->own_addr) ==
  923. 0) {
  924. wpa_printf(MSG_ERROR, "BSS '%s' may not have "
  925. "BSSID set to the MAC address of "
  926. "the radio", hapd->conf->iface);
  927. return -1;
  928. }
  929. }
  930. hapd->interface_added = 1;
  931. if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
  932. hapd->own_addr)) {
  933. wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
  934. MACSTR ")", MAC2STR(hapd->own_addr));
  935. return -1;
  936. }
  937. }
  938. hostapd_flush_old_stations(hapd);
  939. hostapd_set_privacy(hapd, 0);
  940. hostapd_broadcast_wep_clear(hapd);
  941. if (hostapd_setup_encryption(hapd->conf->iface, hapd))
  942. return -1;
  943. /*
  944. * Fetch the SSID from the system and use it or,
  945. * if one was specified in the config file, verify they
  946. * match.
  947. */
  948. ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
  949. if (ssid_len < 0) {
  950. wpa_printf(MSG_ERROR, "Could not read SSID from system");
  951. return -1;
  952. }
  953. if (conf->ssid.ssid_set) {
  954. /*
  955. * If SSID is specified in the config file and it differs
  956. * from what is being used then force installation of the
  957. * new SSID.
  958. */
  959. set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
  960. os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
  961. } else {
  962. /*
  963. * No SSID in the config file; just use the one we got
  964. * from the system.
  965. */
  966. set_ssid = 0;
  967. conf->ssid.ssid_len = ssid_len;
  968. os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
  969. conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
  970. }
  971. if (!hostapd_drv_none(hapd)) {
  972. wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
  973. " and ssid '%s'",
  974. hapd->conf->iface, MAC2STR(hapd->own_addr),
  975. hapd->conf->ssid.ssid);
  976. }
  977. if (hostapd_setup_wpa_psk(conf)) {
  978. wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
  979. return -1;
  980. }
  981. /* Set SSID for the kernel driver (to be used in beacon and probe
  982. * response frames) */
  983. if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
  984. conf->ssid.ssid_len)) {
  985. wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
  986. return -1;
  987. }
  988. if (wpa_debug_level == MSG_MSGDUMP)
  989. conf->radius->msg_dumps = 1;
  990. hapd->radius = radius_client_init(hapd, conf->radius);
  991. if (hapd->radius == NULL) {
  992. wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
  993. return -1;
  994. }
  995. if (hostapd_acl_init(hapd)) {
  996. wpa_printf(MSG_ERROR, "ACL initialization failed.");
  997. return -1;
  998. }
  999. if (hostapd_init_wps(hapd, conf))
  1000. return -1;
  1001. if (ieee802_1x_init(hapd)) {
  1002. wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
  1003. return -1;
  1004. }
  1005. if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
  1006. return -1;
  1007. if (accounting_init(hapd)) {
  1008. wpa_printf(MSG_ERROR, "Accounting initialization failed.");
  1009. return -1;
  1010. }
  1011. if (hapd->conf->ieee802_11f &&
  1012. (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
  1013. wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
  1014. "failed.");
  1015. return -1;
  1016. }
  1017. if (hostapd_ctrl_iface_init(hapd)) {
  1018. wpa_printf(MSG_ERROR, "Failed to setup control interface");
  1019. return -1;
  1020. }
  1021. if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
  1022. wpa_printf(MSG_ERROR, "VLAN initialization failed.");
  1023. return -1;
  1024. }
  1025. #ifdef CONFIG_IEEE80211R
  1026. if (!hostapd_drv_none(hapd)) {
  1027. hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
  1028. hostapd_rrb_receive, hapd, 0);
  1029. if (hapd->l2 == NULL &&
  1030. (hapd->driver == NULL ||
  1031. hapd->driver->send_ether == NULL)) {
  1032. wpa_printf(MSG_ERROR, "Failed to open l2_packet "
  1033. "interface");
  1034. return -1;
  1035. }
  1036. }
  1037. #endif /* CONFIG_IEEE80211R */
  1038. ieee802_11_set_beacon(hapd);
  1039. if (conf->radius_server_clients &&
  1040. hostapd_setup_radius_srv(hapd, conf))
  1041. return -1;
  1042. return 0;
  1043. }
  1044. static void hostapd_tx_queue_params(struct hostapd_iface *iface)
  1045. {
  1046. struct hostapd_data *hapd = iface->bss[0];
  1047. int i;
  1048. struct hostapd_tx_queue_params *p;
  1049. for (i = 0; i < NUM_TX_QUEUES; i++) {
  1050. p = &iface->conf->tx_queue[i];
  1051. if (!p->configured)
  1052. continue;
  1053. if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
  1054. p->cwmax, p->burst)) {
  1055. wpa_printf(MSG_DEBUG, "Failed to set TX queue "
  1056. "parameters for queue %d.", i);
  1057. /* Continue anyway */
  1058. }
  1059. }
  1060. }
  1061. static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
  1062. size_t identity_len, int phase2,
  1063. struct eap_user *user)
  1064. {
  1065. const struct hostapd_eap_user *eap_user;
  1066. int i, count;
  1067. eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
  1068. if (eap_user == NULL)
  1069. return -1;
  1070. if (user == NULL)
  1071. return 0;
  1072. os_memset(user, 0, sizeof(*user));
  1073. count = EAP_USER_MAX_METHODS;
  1074. if (count > EAP_MAX_METHODS)
  1075. count = EAP_MAX_METHODS;
  1076. for (i = 0; i < count; i++) {
  1077. user->methods[i].vendor = eap_user->methods[i].vendor;
  1078. user->methods[i].method = eap_user->methods[i].method;
  1079. }
  1080. if (eap_user->password) {
  1081. user->password = os_malloc(eap_user->password_len);
  1082. if (user->password == NULL)
  1083. return -1;
  1084. os_memcpy(user->password, eap_user->password,
  1085. eap_user->password_len);
  1086. user->password_len = eap_user->password_len;
  1087. user->password_hash = eap_user->password_hash;
  1088. }
  1089. user->force_version = eap_user->force_version;
  1090. user->ttls_auth = eap_user->ttls_auth;
  1091. return 0;
  1092. }
  1093. static int setup_interface(struct hostapd_iface *iface)
  1094. {
  1095. struct hostapd_data *hapd = iface->bss[0];
  1096. struct hostapd_bss_config *conf = hapd->conf;
  1097. size_t i;
  1098. char country[4];
  1099. u8 *b = conf->bssid;
  1100. /*
  1101. * Initialize the driver interface and make sure that all BSSes get
  1102. * configured with a pointer to this driver interface.
  1103. */
  1104. if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
  1105. b = NULL;
  1106. hapd->drv_priv = hostapd_driver_init(hapd, b);
  1107. if (hapd->drv_priv == NULL) {
  1108. wpa_printf(MSG_ERROR, "%s driver initialization failed.",
  1109. hapd->driver ? hapd->driver->name : "Unknown");
  1110. hapd->driver = NULL;
  1111. return -1;
  1112. }
  1113. for (i = 0; i < iface->num_bss; i++) {
  1114. iface->bss[i]->driver = hapd->driver;
  1115. iface->bss[i]->drv_priv = hapd->drv_priv;
  1116. }
  1117. if (hostapd_validate_bssid_configuration(iface))
  1118. return -1;
  1119. #ifdef CONFIG_IEEE80211N
  1120. SET_2BIT_LE16(&iface->ht_op_mode,
  1121. HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
  1122. OP_MODE_PURE);
  1123. #endif /* CONFIG_IEEE80211N */
  1124. if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
  1125. os_memcpy(country, hapd->iconf->country, 3);
  1126. country[3] = '\0';
  1127. if (hostapd_set_country(hapd, country) < 0) {
  1128. wpa_printf(MSG_ERROR, "Failed to set country code");
  1129. return -1;
  1130. }
  1131. }
  1132. if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
  1133. hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
  1134. wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
  1135. "kernel driver");
  1136. return -1;
  1137. }
  1138. if (hostapd_get_hw_features(iface)) {
  1139. /* Not all drivers support this yet, so continue without hw
  1140. * feature data. */
  1141. } else {
  1142. int ret = hostapd_select_hw_mode(iface);
  1143. if (ret < 0) {
  1144. wpa_printf(MSG_ERROR, "Could not select hw_mode and "
  1145. "channel. (%d)", ret);
  1146. return -1;
  1147. }
  1148. ret = hostapd_check_ht_capab(iface);
  1149. if (ret < 0)
  1150. return -1;
  1151. if (ret == 1) {
  1152. wpa_printf(MSG_DEBUG, "Interface initialization will "
  1153. "be completed in a callback");
  1154. return 0;
  1155. }
  1156. }
  1157. return hostapd_setup_interface_complete(iface, 0);
  1158. }
  1159. int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
  1160. {
  1161. struct hostapd_data *hapd = iface->bss[0];
  1162. int freq;
  1163. size_t j;
  1164. u8 *prev_addr;
  1165. if (err) {
  1166. wpa_printf(MSG_ERROR, "Interface initialization failed");
  1167. eloop_terminate();
  1168. return -1;
  1169. }
  1170. wpa_printf(MSG_DEBUG, "Completing interface initialization");
  1171. if (hapd->iconf->channel) {
  1172. freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
  1173. wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
  1174. "Frequency: %d MHz",
  1175. hostapd_hw_mode_txt(hapd->iconf->hw_mode),
  1176. hapd->iconf->channel, freq);
  1177. if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
  1178. hapd->iconf->channel,
  1179. hapd->iconf->ieee80211n,
  1180. hapd->iconf->secondary_channel)) {
  1181. wpa_printf(MSG_ERROR, "Could not set channel for "
  1182. "kernel driver");
  1183. return -1;
  1184. }
  1185. }
  1186. if (hapd->iconf->rts_threshold > -1 &&
  1187. hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
  1188. wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
  1189. "kernel driver");
  1190. return -1;
  1191. }
  1192. if (hapd->iconf->fragm_threshold > -1 &&
  1193. hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
  1194. wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
  1195. "for kernel driver");
  1196. return -1;
  1197. }
  1198. prev_addr = hapd->own_addr;
  1199. for (j = 0; j < iface->num_bss; j++) {
  1200. hapd = iface->bss[j];
  1201. if (j)
  1202. os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
  1203. if (hostapd_setup_bss(hapd, j == 0))
  1204. return -1;
  1205. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
  1206. prev_addr = hapd->own_addr;
  1207. }
  1208. hostapd_tx_queue_params(iface);
  1209. ap_list_init(iface);
  1210. if (hostapd_driver_commit(hapd) < 0) {
  1211. wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
  1212. "configuration", __func__);
  1213. return -1;
  1214. }
  1215. wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
  1216. iface->bss[0]->conf->iface);
  1217. return 0;
  1218. }
  1219. /**
  1220. * hostapd_setup_interface - Setup of an interface
  1221. * @iface: Pointer to interface data.
  1222. * Returns: 0 on success, -1 on failure
  1223. *
  1224. * Initializes the driver interface, validates the configuration,
  1225. * and sets driver parameters based on the configuration.
  1226. * Flushes old stations, sets the channel, encryption,
  1227. * beacons, and WDS links based on the configuration.
  1228. */
  1229. int hostapd_setup_interface(struct hostapd_iface *iface)
  1230. {
  1231. int ret;
  1232. ret = setup_interface(iface);
  1233. if (ret) {
  1234. wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
  1235. iface->bss[0]->conf->iface);
  1236. eloop_terminate();
  1237. return -1;
  1238. }
  1239. return 0;
  1240. }
  1241. /**
  1242. * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
  1243. * @hapd_iface: Pointer to interface data
  1244. * @conf: Pointer to per-interface configuration
  1245. * @bss: Pointer to per-BSS configuration for this BSS
  1246. * Returns: Pointer to allocated BSS data
  1247. *
  1248. * This function is used to allocate per-BSS data structure. This data will be
  1249. * freed after hostapd_cleanup() is called for it during interface
  1250. * deinitialization.
  1251. */
  1252. struct hostapd_data *
  1253. hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
  1254. struct hostapd_config *conf,
  1255. struct hostapd_bss_config *bss)
  1256. {
  1257. struct hostapd_data *hapd;
  1258. hapd = os_zalloc(sizeof(*hapd));
  1259. if (hapd == NULL)
  1260. return NULL;
  1261. hapd->iconf = conf;
  1262. hapd->conf = bss;
  1263. hapd->iface = hapd_iface;
  1264. #ifdef EAP_TLS_FUNCS
  1265. if (hapd->conf->eap_server &&
  1266. (hapd->conf->ca_cert || hapd->conf->server_cert ||
  1267. hapd->conf->dh_file)) {
  1268. struct tls_connection_params params;
  1269. hapd->ssl_ctx = tls_init(NULL);
  1270. if (hapd->ssl_ctx == NULL) {
  1271. wpa_printf(MSG_ERROR, "Failed to initialize TLS");
  1272. goto fail;
  1273. }
  1274. os_memset(&params, 0, sizeof(params));
  1275. params.ca_cert = hapd->conf->ca_cert;
  1276. params.client_cert = hapd->conf->server_cert;
  1277. params.private_key = hapd->conf->private_key;
  1278. params.private_key_passwd = hapd->conf->private_key_passwd;
  1279. params.dh_file = hapd->conf->dh_file;
  1280. if (tls_global_set_params(hapd->ssl_ctx, &params)) {
  1281. wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
  1282. goto fail;
  1283. }
  1284. if (tls_global_set_verify(hapd->ssl_ctx,
  1285. hapd->conf->check_crl)) {
  1286. wpa_printf(MSG_ERROR, "Failed to enable check_crl");
  1287. goto fail;
  1288. }
  1289. }
  1290. #endif /* EAP_TLS_FUNCS */
  1291. #ifdef EAP_SERVER
  1292. if (hapd->conf->eap_sim_db) {
  1293. hapd->eap_sim_db_priv =
  1294. eap_sim_db_init(hapd->conf->eap_sim_db,
  1295. hostapd_sim_db_cb, hapd);
  1296. if (hapd->eap_sim_db_priv == NULL) {
  1297. wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
  1298. "database interface");
  1299. goto fail;
  1300. }
  1301. }
  1302. #endif /* EAP_SERVER */
  1303. hapd->driver = hapd->iconf->driver;
  1304. return hapd;
  1305. #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
  1306. fail:
  1307. #endif
  1308. /* TODO: cleanup allocated resources(?) */
  1309. os_free(hapd);
  1310. return NULL;
  1311. }
  1312. void hostapd_interface_deinit(struct hostapd_iface *iface)
  1313. {
  1314. size_t j;
  1315. if (iface == NULL)
  1316. return;
  1317. hostapd_cleanup_iface_pre(iface);
  1318. for (j = 0; j < iface->num_bss; j++) {
  1319. struct hostapd_data *hapd = iface->bss[j];
  1320. hostapd_free_stas(hapd);
  1321. hostapd_flush_old_stations(hapd);
  1322. hostapd_cleanup(hapd);
  1323. if (j == iface->num_bss - 1 && hapd->driver)
  1324. hostapd_driver_deinit(hapd);
  1325. }
  1326. for (j = 0; j < iface->num_bss; j++)
  1327. os_free(iface->bss[j]);
  1328. hostapd_cleanup_iface(iface);
  1329. }
  1330. int hostapd_register_probereq_cb(struct hostapd_data *hapd,
  1331. void (*cb)(void *ctx, const u8 *sa,
  1332. const u8 *ie, size_t ie_len),
  1333. void *ctx)
  1334. {
  1335. struct hostapd_probereq_cb *n;
  1336. n = os_realloc(hapd->probereq_cb, (hapd->num_probereq_cb + 1) *
  1337. sizeof(struct hostapd_probereq_cb));
  1338. if (n == NULL)
  1339. return -1;
  1340. hapd->probereq_cb = n;
  1341. n = &hapd->probereq_cb[hapd->num_probereq_cb];
  1342. hapd->num_probereq_cb++;
  1343. n->cb = cb;
  1344. n->ctx = ctx;
  1345. return 0;
  1346. }