hostapd.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  1. /*
  2. * hostapd / Initialization and configuration
  3. * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "common/ieee802_11_defs.h"
  12. #include "radius/radius_client.h"
  13. #include "radius/radius_das.h"
  14. #include "drivers/driver.h"
  15. #include "hostapd.h"
  16. #include "authsrv.h"
  17. #include "sta_info.h"
  18. #include "accounting.h"
  19. #include "ap_list.h"
  20. #include "beacon.h"
  21. #include "iapp.h"
  22. #include "ieee802_1x.h"
  23. #include "ieee802_11_auth.h"
  24. #include "vlan_init.h"
  25. #include "wpa_auth.h"
  26. #include "wps_hostapd.h"
  27. #include "hw_features.h"
  28. #include "wpa_auth_glue.h"
  29. #include "ap_drv_ops.h"
  30. #include "ap_config.h"
  31. #include "p2p_hostapd.h"
  32. #include "gas_serv.h"
  33. #include "dfs.h"
  34. static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
  35. static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
  36. static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
  37. extern int wpa_debug_level;
  38. extern struct wpa_driver_ops *wpa_drivers[];
  39. int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
  40. int (*cb)(struct hostapd_iface *iface,
  41. void *ctx), void *ctx)
  42. {
  43. size_t i;
  44. int ret;
  45. for (i = 0; i < interfaces->count; i++) {
  46. ret = cb(interfaces->iface[i], ctx);
  47. if (ret)
  48. return ret;
  49. }
  50. return 0;
  51. }
  52. static void hostapd_reload_bss(struct hostapd_data *hapd)
  53. {
  54. struct hostapd_ssid *ssid;
  55. #ifndef CONFIG_NO_RADIUS
  56. radius_client_reconfig(hapd->radius, hapd->conf->radius);
  57. #endif /* CONFIG_NO_RADIUS */
  58. ssid = &hapd->conf->ssid;
  59. if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
  60. ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
  61. /*
  62. * Force PSK to be derived again since SSID or passphrase may
  63. * have changed.
  64. */
  65. os_free(ssid->wpa_psk);
  66. ssid->wpa_psk = NULL;
  67. }
  68. if (hostapd_setup_wpa_psk(hapd->conf)) {
  69. wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
  70. "after reloading configuration");
  71. }
  72. if (hapd->conf->ieee802_1x || hapd->conf->wpa)
  73. hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
  74. else
  75. hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
  76. if (hapd->conf->wpa && hapd->wpa_auth == NULL) {
  77. hostapd_setup_wpa(hapd);
  78. if (hapd->wpa_auth)
  79. wpa_init_keys(hapd->wpa_auth);
  80. } else if (hapd->conf->wpa) {
  81. const u8 *wpa_ie;
  82. size_t wpa_ie_len;
  83. hostapd_reconfig_wpa(hapd);
  84. wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
  85. if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
  86. wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
  87. "the kernel driver.");
  88. } else if (hapd->wpa_auth) {
  89. wpa_deinit(hapd->wpa_auth);
  90. hapd->wpa_auth = NULL;
  91. hostapd_set_privacy(hapd, 0);
  92. hostapd_setup_encryption(hapd->conf->iface, hapd);
  93. hostapd_set_generic_elem(hapd, (u8 *) "", 0);
  94. }
  95. ieee802_11_set_beacon(hapd);
  96. hostapd_update_wps(hapd);
  97. if (hapd->conf->ssid.ssid_set &&
  98. hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
  99. hapd->conf->ssid.ssid_len)) {
  100. wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
  101. /* try to continue */
  102. }
  103. wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
  104. }
  105. static void hostapd_clear_old(struct hostapd_iface *iface)
  106. {
  107. size_t j;
  108. /*
  109. * Deauthenticate all stations since the new configuration may not
  110. * allow them to use the BSS anymore.
  111. */
  112. for (j = 0; j < iface->num_bss; j++) {
  113. hostapd_flush_old_stations(iface->bss[j],
  114. WLAN_REASON_PREV_AUTH_NOT_VALID);
  115. hostapd_broadcast_wep_clear(iface->bss[j]);
  116. #ifndef CONFIG_NO_RADIUS
  117. /* TODO: update dynamic data based on changed configuration
  118. * items (e.g., open/close sockets, etc.) */
  119. radius_client_flush(iface->bss[j]->radius, 0);
  120. #endif /* CONFIG_NO_RADIUS */
  121. }
  122. }
  123. int hostapd_reload_config(struct hostapd_iface *iface)
  124. {
  125. struct hostapd_data *hapd = iface->bss[0];
  126. struct hostapd_config *newconf, *oldconf;
  127. size_t j;
  128. if (iface->config_fname == NULL) {
  129. /* Only in-memory config in use - assume it has been updated */
  130. hostapd_clear_old(iface);
  131. for (j = 0; j < iface->num_bss; j++)
  132. hostapd_reload_bss(iface->bss[j]);
  133. return 0;
  134. }
  135. if (iface->interfaces == NULL ||
  136. iface->interfaces->config_read_cb == NULL)
  137. return -1;
  138. newconf = iface->interfaces->config_read_cb(iface->config_fname);
  139. if (newconf == NULL)
  140. return -1;
  141. hostapd_clear_old(iface);
  142. oldconf = hapd->iconf;
  143. iface->conf = newconf;
  144. for (j = 0; j < iface->num_bss; j++) {
  145. hapd = iface->bss[j];
  146. hapd->iconf = newconf;
  147. hapd->conf = newconf->bss[j];
  148. hostapd_reload_bss(hapd);
  149. }
  150. hostapd_config_free(oldconf);
  151. return 0;
  152. }
  153. static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
  154. char *ifname)
  155. {
  156. int i;
  157. for (i = 0; i < NUM_WEP_KEYS; i++) {
  158. if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
  159. 0, NULL, 0, NULL, 0)) {
  160. wpa_printf(MSG_DEBUG, "Failed to clear default "
  161. "encryption keys (ifname=%s keyidx=%d)",
  162. ifname, i);
  163. }
  164. }
  165. #ifdef CONFIG_IEEE80211W
  166. if (hapd->conf->ieee80211w) {
  167. for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
  168. if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
  169. NULL, i, 0, NULL,
  170. 0, NULL, 0)) {
  171. wpa_printf(MSG_DEBUG, "Failed to clear "
  172. "default mgmt encryption keys "
  173. "(ifname=%s keyidx=%d)", ifname, i);
  174. }
  175. }
  176. }
  177. #endif /* CONFIG_IEEE80211W */
  178. }
  179. static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
  180. {
  181. hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
  182. return 0;
  183. }
  184. static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
  185. {
  186. int errors = 0, idx;
  187. struct hostapd_ssid *ssid = &hapd->conf->ssid;
  188. idx = ssid->wep.idx;
  189. if (ssid->wep.default_len &&
  190. hostapd_drv_set_key(hapd->conf->iface,
  191. hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
  192. 1, NULL, 0, ssid->wep.key[idx],
  193. ssid->wep.len[idx])) {
  194. wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
  195. errors++;
  196. }
  197. return errors;
  198. }
  199. static void hostapd_free_hapd_data(struct hostapd_data *hapd)
  200. {
  201. iapp_deinit(hapd->iapp);
  202. hapd->iapp = NULL;
  203. accounting_deinit(hapd);
  204. hostapd_deinit_wpa(hapd);
  205. vlan_deinit(hapd);
  206. hostapd_acl_deinit(hapd);
  207. #ifndef CONFIG_NO_RADIUS
  208. radius_client_deinit(hapd->radius);
  209. hapd->radius = NULL;
  210. radius_das_deinit(hapd->radius_das);
  211. hapd->radius_das = NULL;
  212. #endif /* CONFIG_NO_RADIUS */
  213. hostapd_deinit_wps(hapd);
  214. authsrv_deinit(hapd);
  215. if (hapd->interface_added &&
  216. hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
  217. wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
  218. hapd->conf->iface);
  219. }
  220. os_free(hapd->probereq_cb);
  221. hapd->probereq_cb = NULL;
  222. #ifdef CONFIG_P2P
  223. wpabuf_free(hapd->p2p_beacon_ie);
  224. hapd->p2p_beacon_ie = NULL;
  225. wpabuf_free(hapd->p2p_probe_resp_ie);
  226. hapd->p2p_probe_resp_ie = NULL;
  227. #endif /* CONFIG_P2P */
  228. wpabuf_free(hapd->time_adv);
  229. #ifdef CONFIG_INTERWORKING
  230. gas_serv_deinit(hapd);
  231. #endif /* CONFIG_INTERWORKING */
  232. #ifdef CONFIG_SQLITE
  233. os_free(hapd->tmp_eap_user.identity);
  234. os_free(hapd->tmp_eap_user.password);
  235. #endif /* CONFIG_SQLITE */
  236. }
  237. /**
  238. * hostapd_cleanup - Per-BSS cleanup (deinitialization)
  239. * @hapd: Pointer to BSS data
  240. *
  241. * This function is used to free all per-BSS data structures and resources.
  242. * This gets called in a loop for each BSS between calls to
  243. * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
  244. * is deinitialized. Most of the modules that are initialized in
  245. * hostapd_setup_bss() are deinitialized here.
  246. */
  247. static void hostapd_cleanup(struct hostapd_data *hapd)
  248. {
  249. if (hapd->iface->interfaces &&
  250. hapd->iface->interfaces->ctrl_iface_deinit)
  251. hapd->iface->interfaces->ctrl_iface_deinit(hapd);
  252. hostapd_free_hapd_data(hapd);
  253. }
  254. /**
  255. * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
  256. * @iface: Pointer to interface data
  257. *
  258. * This function is called before per-BSS data structures are deinitialized
  259. * with hostapd_cleanup().
  260. */
  261. static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
  262. {
  263. }
  264. static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
  265. {
  266. hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
  267. iface->hw_features = NULL;
  268. os_free(iface->current_rates);
  269. iface->current_rates = NULL;
  270. os_free(iface->basic_rates);
  271. iface->basic_rates = NULL;
  272. ap_list_deinit(iface);
  273. }
  274. /**
  275. * hostapd_cleanup_iface - Complete per-interface cleanup
  276. * @iface: Pointer to interface data
  277. *
  278. * This function is called after per-BSS data structures are deinitialized
  279. * with hostapd_cleanup().
  280. */
  281. static void hostapd_cleanup_iface(struct hostapd_iface *iface)
  282. {
  283. hostapd_cleanup_iface_partial(iface);
  284. hostapd_config_free(iface->conf);
  285. iface->conf = NULL;
  286. os_free(iface->config_fname);
  287. os_free(iface->bss);
  288. os_free(iface);
  289. }
  290. static void hostapd_clear_wep(struct hostapd_data *hapd)
  291. {
  292. if (hapd->drv_priv) {
  293. hostapd_set_privacy(hapd, 0);
  294. hostapd_broadcast_wep_clear(hapd);
  295. }
  296. }
  297. static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
  298. {
  299. int i;
  300. hostapd_broadcast_wep_set(hapd);
  301. if (hapd->conf->ssid.wep.default_len) {
  302. hostapd_set_privacy(hapd, 1);
  303. return 0;
  304. }
  305. /*
  306. * When IEEE 802.1X is not enabled, the driver may need to know how to
  307. * set authentication algorithms for static WEP.
  308. */
  309. hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
  310. for (i = 0; i < 4; i++) {
  311. if (hapd->conf->ssid.wep.key[i] &&
  312. hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
  313. i == hapd->conf->ssid.wep.idx, NULL, 0,
  314. hapd->conf->ssid.wep.key[i],
  315. hapd->conf->ssid.wep.len[i])) {
  316. wpa_printf(MSG_WARNING, "Could not set WEP "
  317. "encryption.");
  318. return -1;
  319. }
  320. if (hapd->conf->ssid.wep.key[i] &&
  321. i == hapd->conf->ssid.wep.idx)
  322. hostapd_set_privacy(hapd, 1);
  323. }
  324. return 0;
  325. }
  326. static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
  327. {
  328. int ret = 0;
  329. u8 addr[ETH_ALEN];
  330. if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
  331. return 0;
  332. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Flushing old station entries");
  333. if (hostapd_flush(hapd)) {
  334. wpa_msg(hapd->msg_ctx, MSG_WARNING, "Could not connect to "
  335. "kernel driver");
  336. ret = -1;
  337. }
  338. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
  339. os_memset(addr, 0xff, ETH_ALEN);
  340. hostapd_drv_sta_deauth(hapd, addr, reason);
  341. hostapd_free_stas(hapd);
  342. return ret;
  343. }
  344. /**
  345. * hostapd_validate_bssid_configuration - Validate BSSID configuration
  346. * @iface: Pointer to interface data
  347. * Returns: 0 on success, -1 on failure
  348. *
  349. * This function is used to validate that the configured BSSIDs are valid.
  350. */
  351. static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
  352. {
  353. u8 mask[ETH_ALEN] = { 0 };
  354. struct hostapd_data *hapd = iface->bss[0];
  355. unsigned int i = iface->conf->num_bss, bits = 0, j;
  356. int auto_addr = 0;
  357. if (hostapd_drv_none(hapd))
  358. return 0;
  359. /* Generate BSSID mask that is large enough to cover the BSSIDs. */
  360. /* Determine the bits necessary to cover the number of BSSIDs. */
  361. for (i--; i; i >>= 1)
  362. bits++;
  363. /* Determine the bits necessary to any configured BSSIDs,
  364. if they are higher than the number of BSSIDs. */
  365. for (j = 0; j < iface->conf->num_bss; j++) {
  366. if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
  367. if (j)
  368. auto_addr++;
  369. continue;
  370. }
  371. for (i = 0; i < ETH_ALEN; i++) {
  372. mask[i] |=
  373. iface->conf->bss[j]->bssid[i] ^
  374. hapd->own_addr[i];
  375. }
  376. }
  377. if (!auto_addr)
  378. goto skip_mask_ext;
  379. for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
  380. ;
  381. j = 0;
  382. if (i < ETH_ALEN) {
  383. j = (5 - i) * 8;
  384. while (mask[i] != 0) {
  385. mask[i] >>= 1;
  386. j++;
  387. }
  388. }
  389. if (bits < j)
  390. bits = j;
  391. if (bits > 40) {
  392. wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
  393. bits);
  394. return -1;
  395. }
  396. os_memset(mask, 0xff, ETH_ALEN);
  397. j = bits / 8;
  398. for (i = 5; i > 5 - j; i--)
  399. mask[i] = 0;
  400. j = bits % 8;
  401. while (j--)
  402. mask[i] <<= 1;
  403. skip_mask_ext:
  404. wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
  405. (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
  406. if (!auto_addr)
  407. return 0;
  408. for (i = 0; i < ETH_ALEN; i++) {
  409. if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
  410. wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
  411. " for start address " MACSTR ".",
  412. MAC2STR(mask), MAC2STR(hapd->own_addr));
  413. wpa_printf(MSG_ERROR, "Start address must be the "
  414. "first address in the block (i.e., addr "
  415. "AND mask == addr).");
  416. return -1;
  417. }
  418. }
  419. return 0;
  420. }
  421. static int mac_in_conf(struct hostapd_config *conf, const void *a)
  422. {
  423. size_t i;
  424. for (i = 0; i < conf->num_bss; i++) {
  425. if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
  426. return 1;
  427. }
  428. }
  429. return 0;
  430. }
  431. #ifndef CONFIG_NO_RADIUS
  432. static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
  433. struct radius_das_attrs *attr)
  434. {
  435. /* TODO */
  436. return 0;
  437. }
  438. static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
  439. struct radius_das_attrs *attr)
  440. {
  441. struct sta_info *sta = NULL;
  442. char buf[128];
  443. if (attr->sta_addr)
  444. sta = ap_get_sta(hapd, attr->sta_addr);
  445. if (sta == NULL && attr->acct_session_id &&
  446. attr->acct_session_id_len == 17) {
  447. for (sta = hapd->sta_list; sta; sta = sta->next) {
  448. os_snprintf(buf, sizeof(buf), "%08X-%08X",
  449. sta->acct_session_id_hi,
  450. sta->acct_session_id_lo);
  451. if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
  452. break;
  453. }
  454. }
  455. if (sta == NULL && attr->cui) {
  456. for (sta = hapd->sta_list; sta; sta = sta->next) {
  457. struct wpabuf *cui;
  458. cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
  459. if (cui && wpabuf_len(cui) == attr->cui_len &&
  460. os_memcmp(wpabuf_head(cui), attr->cui,
  461. attr->cui_len) == 0)
  462. break;
  463. }
  464. }
  465. if (sta == NULL && attr->user_name) {
  466. for (sta = hapd->sta_list; sta; sta = sta->next) {
  467. u8 *identity;
  468. size_t identity_len;
  469. identity = ieee802_1x_get_identity(sta->eapol_sm,
  470. &identity_len);
  471. if (identity &&
  472. identity_len == attr->user_name_len &&
  473. os_memcmp(identity, attr->user_name, identity_len)
  474. == 0)
  475. break;
  476. }
  477. }
  478. return sta;
  479. }
  480. static enum radius_das_res
  481. hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
  482. {
  483. struct hostapd_data *hapd = ctx;
  484. struct sta_info *sta;
  485. if (hostapd_das_nas_mismatch(hapd, attr))
  486. return RADIUS_DAS_NAS_MISMATCH;
  487. sta = hostapd_das_find_sta(hapd, attr);
  488. if (sta == NULL)
  489. return RADIUS_DAS_SESSION_NOT_FOUND;
  490. hostapd_drv_sta_deauth(hapd, sta->addr,
  491. WLAN_REASON_PREV_AUTH_NOT_VALID);
  492. ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
  493. return RADIUS_DAS_SUCCESS;
  494. }
  495. #endif /* CONFIG_NO_RADIUS */
  496. /**
  497. * hostapd_setup_bss - Per-BSS setup (initialization)
  498. * @hapd: Pointer to BSS data
  499. * @first: Whether this BSS is the first BSS of an interface
  500. *
  501. * This function is used to initialize all per-BSS data structures and
  502. * resources. This gets called in a loop for each BSS when an interface is
  503. * initialized. Most of the modules that are initialized here will be
  504. * deinitialized in hostapd_cleanup().
  505. */
  506. static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
  507. {
  508. struct hostapd_bss_config *conf = hapd->conf;
  509. u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
  510. int ssid_len, set_ssid;
  511. char force_ifname[IFNAMSIZ];
  512. u8 if_addr[ETH_ALEN];
  513. if (!first) {
  514. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
  515. /* Allocate the next available BSSID. */
  516. do {
  517. inc_byte_array(hapd->own_addr, ETH_ALEN);
  518. } while (mac_in_conf(hapd->iconf, hapd->own_addr));
  519. } else {
  520. /* Allocate the configured BSSID. */
  521. os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
  522. if (hostapd_mac_comp(hapd->own_addr,
  523. hapd->iface->bss[0]->own_addr) ==
  524. 0) {
  525. wpa_printf(MSG_ERROR, "BSS '%s' may not have "
  526. "BSSID set to the MAC address of "
  527. "the radio", hapd->conf->iface);
  528. return -1;
  529. }
  530. }
  531. hapd->interface_added = 1;
  532. if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
  533. hapd->conf->iface, hapd->own_addr, hapd,
  534. &hapd->drv_priv, force_ifname, if_addr,
  535. hapd->conf->bridge[0] ? hapd->conf->bridge :
  536. NULL)) {
  537. wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
  538. MACSTR ")", MAC2STR(hapd->own_addr));
  539. return -1;
  540. }
  541. }
  542. if (conf->wmm_enabled < 0)
  543. conf->wmm_enabled = hapd->iconf->ieee80211n;
  544. hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
  545. hostapd_set_privacy(hapd, 0);
  546. hostapd_broadcast_wep_clear(hapd);
  547. if (hostapd_setup_encryption(hapd->conf->iface, hapd))
  548. return -1;
  549. /*
  550. * Fetch the SSID from the system and use it or,
  551. * if one was specified in the config file, verify they
  552. * match.
  553. */
  554. ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
  555. if (ssid_len < 0) {
  556. wpa_printf(MSG_ERROR, "Could not read SSID from system");
  557. return -1;
  558. }
  559. if (conf->ssid.ssid_set) {
  560. /*
  561. * If SSID is specified in the config file and it differs
  562. * from what is being used then force installation of the
  563. * new SSID.
  564. */
  565. set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
  566. os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
  567. } else {
  568. /*
  569. * No SSID in the config file; just use the one we got
  570. * from the system.
  571. */
  572. set_ssid = 0;
  573. conf->ssid.ssid_len = ssid_len;
  574. os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
  575. }
  576. if (!hostapd_drv_none(hapd)) {
  577. wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
  578. " and ssid \"%s\"",
  579. hapd->conf->iface, MAC2STR(hapd->own_addr),
  580. wpa_ssid_txt(hapd->conf->ssid.ssid,
  581. hapd->conf->ssid.ssid_len));
  582. }
  583. if (hostapd_setup_wpa_psk(conf)) {
  584. wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
  585. return -1;
  586. }
  587. /* Set SSID for the kernel driver (to be used in beacon and probe
  588. * response frames) */
  589. if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
  590. conf->ssid.ssid_len)) {
  591. wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
  592. return -1;
  593. }
  594. if (wpa_debug_level == MSG_MSGDUMP)
  595. conf->radius->msg_dumps = 1;
  596. #ifndef CONFIG_NO_RADIUS
  597. hapd->radius = radius_client_init(hapd, conf->radius);
  598. if (hapd->radius == NULL) {
  599. wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
  600. return -1;
  601. }
  602. if (hapd->conf->radius_das_port) {
  603. struct radius_das_conf das_conf;
  604. os_memset(&das_conf, 0, sizeof(das_conf));
  605. das_conf.port = hapd->conf->radius_das_port;
  606. das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
  607. das_conf.shared_secret_len =
  608. hapd->conf->radius_das_shared_secret_len;
  609. das_conf.client_addr = &hapd->conf->radius_das_client_addr;
  610. das_conf.time_window = hapd->conf->radius_das_time_window;
  611. das_conf.require_event_timestamp =
  612. hapd->conf->radius_das_require_event_timestamp;
  613. das_conf.ctx = hapd;
  614. das_conf.disconnect = hostapd_das_disconnect;
  615. hapd->radius_das = radius_das_init(&das_conf);
  616. if (hapd->radius_das == NULL) {
  617. wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
  618. "failed.");
  619. return -1;
  620. }
  621. }
  622. #endif /* CONFIG_NO_RADIUS */
  623. if (hostapd_acl_init(hapd)) {
  624. wpa_printf(MSG_ERROR, "ACL initialization failed.");
  625. return -1;
  626. }
  627. if (hostapd_init_wps(hapd, conf))
  628. return -1;
  629. if (authsrv_init(hapd) < 0)
  630. return -1;
  631. if (ieee802_1x_init(hapd)) {
  632. wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
  633. return -1;
  634. }
  635. if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
  636. return -1;
  637. if (accounting_init(hapd)) {
  638. wpa_printf(MSG_ERROR, "Accounting initialization failed.");
  639. return -1;
  640. }
  641. if (hapd->conf->ieee802_11f &&
  642. (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
  643. wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
  644. "failed.");
  645. return -1;
  646. }
  647. #ifdef CONFIG_INTERWORKING
  648. if (gas_serv_init(hapd)) {
  649. wpa_printf(MSG_ERROR, "GAS server initialization failed");
  650. return -1;
  651. }
  652. #endif /* CONFIG_INTERWORKING */
  653. if (hapd->iface->interfaces &&
  654. hapd->iface->interfaces->ctrl_iface_init &&
  655. hapd->iface->interfaces->ctrl_iface_init(hapd)) {
  656. wpa_printf(MSG_ERROR, "Failed to setup control interface");
  657. return -1;
  658. }
  659. if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
  660. wpa_printf(MSG_ERROR, "VLAN initialization failed.");
  661. return -1;
  662. }
  663. if (!hapd->conf->start_disabled)
  664. ieee802_11_set_beacon(hapd);
  665. if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
  666. return -1;
  667. if (hapd->driver && hapd->driver->set_operstate)
  668. hapd->driver->set_operstate(hapd->drv_priv, 1);
  669. return 0;
  670. }
  671. static void hostapd_tx_queue_params(struct hostapd_iface *iface)
  672. {
  673. struct hostapd_data *hapd = iface->bss[0];
  674. int i;
  675. struct hostapd_tx_queue_params *p;
  676. for (i = 0; i < NUM_TX_QUEUES; i++) {
  677. p = &iface->conf->tx_queue[i];
  678. if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
  679. p->cwmax, p->burst)) {
  680. wpa_printf(MSG_DEBUG, "Failed to set TX queue "
  681. "parameters for queue %d.", i);
  682. /* Continue anyway */
  683. }
  684. }
  685. }
  686. static int hostapd_set_acl_list(struct hostapd_data *hapd,
  687. struct mac_acl_entry *mac_acl,
  688. int n_entries, u8 accept_acl)
  689. {
  690. struct hostapd_acl_params *acl_params;
  691. int i, err;
  692. acl_params = os_zalloc(sizeof(*acl_params) +
  693. (n_entries * sizeof(acl_params->mac_acl[0])));
  694. if (!acl_params)
  695. return -ENOMEM;
  696. for (i = 0; i < n_entries; i++)
  697. os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
  698. ETH_ALEN);
  699. acl_params->acl_policy = accept_acl;
  700. acl_params->num_mac_acl = n_entries;
  701. err = hostapd_drv_set_acl(hapd, acl_params);
  702. os_free(acl_params);
  703. return err;
  704. }
  705. static void hostapd_set_acl(struct hostapd_data *hapd)
  706. {
  707. struct hostapd_config *conf = hapd->iconf;
  708. int err;
  709. u8 accept_acl;
  710. if (hapd->iface->drv_max_acl_mac_addrs == 0)
  711. return;
  712. if (!(conf->bss[0]->num_accept_mac || conf->bss[0]->num_deny_mac))
  713. return;
  714. if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
  715. if (conf->bss[0]->num_accept_mac) {
  716. accept_acl = 1;
  717. err = hostapd_set_acl_list(hapd,
  718. conf->bss[0]->accept_mac,
  719. conf->bss[0]->num_accept_mac,
  720. accept_acl);
  721. if (err) {
  722. wpa_printf(MSG_DEBUG, "Failed to set accept acl");
  723. return;
  724. }
  725. } else {
  726. wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
  727. }
  728. } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
  729. if (conf->bss[0]->num_deny_mac) {
  730. accept_acl = 0;
  731. err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
  732. conf->bss[0]->num_deny_mac,
  733. accept_acl);
  734. if (err) {
  735. wpa_printf(MSG_DEBUG, "Failed to set deny acl");
  736. return;
  737. }
  738. } else {
  739. wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
  740. }
  741. }
  742. }
  743. static int setup_interface(struct hostapd_iface *iface)
  744. {
  745. struct hostapd_data *hapd = iface->bss[0];
  746. size_t i;
  747. char country[4];
  748. /*
  749. * Make sure that all BSSes get configured with a pointer to the same
  750. * driver interface.
  751. */
  752. for (i = 1; i < iface->num_bss; i++) {
  753. iface->bss[i]->driver = hapd->driver;
  754. iface->bss[i]->drv_priv = hapd->drv_priv;
  755. }
  756. if (hostapd_validate_bssid_configuration(iface))
  757. return -1;
  758. if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
  759. os_memcpy(country, hapd->iconf->country, 3);
  760. country[3] = '\0';
  761. if (hostapd_set_country(hapd, country) < 0) {
  762. wpa_printf(MSG_ERROR, "Failed to set country code");
  763. return -1;
  764. }
  765. }
  766. if (hostapd_get_hw_features(iface)) {
  767. /* Not all drivers support this yet, so continue without hw
  768. * feature data. */
  769. } else {
  770. int ret = hostapd_select_hw_mode(iface);
  771. if (ret < 0) {
  772. wpa_printf(MSG_ERROR, "Could not select hw_mode and "
  773. "channel. (%d)", ret);
  774. return -1;
  775. }
  776. if (ret == 1) {
  777. wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
  778. return 0;
  779. }
  780. ret = hostapd_check_ht_capab(iface);
  781. if (ret < 0)
  782. return -1;
  783. if (ret == 1) {
  784. wpa_printf(MSG_DEBUG, "Interface initialization will "
  785. "be completed in a callback");
  786. return 0;
  787. }
  788. if (iface->conf->ieee80211h)
  789. wpa_printf(MSG_DEBUG, "DFS support is enabled");
  790. }
  791. return hostapd_setup_interface_complete(iface, 0);
  792. }
  793. int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
  794. {
  795. struct hostapd_data *hapd = iface->bss[0];
  796. size_t j;
  797. u8 *prev_addr;
  798. if (err) {
  799. wpa_printf(MSG_ERROR, "Interface initialization failed");
  800. eloop_terminate();
  801. return -1;
  802. }
  803. wpa_printf(MSG_DEBUG, "Completing interface initialization");
  804. if (hapd->iconf->channel) {
  805. #ifdef NEED_AP_MLME
  806. int res;
  807. #endif /* NEED_AP_MLME */
  808. iface->freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
  809. wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
  810. "Frequency: %d MHz",
  811. hostapd_hw_mode_txt(hapd->iconf->hw_mode),
  812. hapd->iconf->channel, iface->freq);
  813. #ifdef NEED_AP_MLME
  814. /* Check DFS */
  815. res = hostapd_handle_dfs(hapd);
  816. if (res <= 0)
  817. return res;
  818. #endif /* NEED_AP_MLME */
  819. if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
  820. hapd->iconf->channel,
  821. hapd->iconf->ieee80211n,
  822. hapd->iconf->ieee80211ac,
  823. hapd->iconf->secondary_channel,
  824. hapd->iconf->vht_oper_chwidth,
  825. hapd->iconf->vht_oper_centr_freq_seg0_idx,
  826. hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
  827. wpa_printf(MSG_ERROR, "Could not set channel for "
  828. "kernel driver");
  829. return -1;
  830. }
  831. }
  832. if (iface->current_mode) {
  833. if (hostapd_prepare_rates(iface, iface->current_mode)) {
  834. wpa_printf(MSG_ERROR, "Failed to prepare rates "
  835. "table.");
  836. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
  837. HOSTAPD_LEVEL_WARNING,
  838. "Failed to prepare rates table.");
  839. return -1;
  840. }
  841. }
  842. if (hapd->iconf->rts_threshold > -1 &&
  843. hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
  844. wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
  845. "kernel driver");
  846. return -1;
  847. }
  848. if (hapd->iconf->fragm_threshold > -1 &&
  849. hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
  850. wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
  851. "for kernel driver");
  852. return -1;
  853. }
  854. prev_addr = hapd->own_addr;
  855. for (j = 0; j < iface->num_bss; j++) {
  856. hapd = iface->bss[j];
  857. if (j)
  858. os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
  859. if (hostapd_setup_bss(hapd, j == 0))
  860. return -1;
  861. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
  862. prev_addr = hapd->own_addr;
  863. }
  864. hostapd_tx_queue_params(iface);
  865. ap_list_init(iface);
  866. hostapd_set_acl(hapd);
  867. if (hostapd_driver_commit(hapd) < 0) {
  868. wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
  869. "configuration", __func__);
  870. return -1;
  871. }
  872. /*
  873. * WPS UPnP module can be initialized only when the "upnp_iface" is up.
  874. * If "interface" and "upnp_iface" are the same (e.g., non-bridge
  875. * mode), the interface is up only after driver_commit, so initialize
  876. * WPS after driver_commit.
  877. */
  878. for (j = 0; j < iface->num_bss; j++) {
  879. if (hostapd_init_wps_complete(iface->bss[j]))
  880. return -1;
  881. }
  882. if (hapd->setup_complete_cb)
  883. hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
  884. wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
  885. iface->bss[0]->conf->iface);
  886. return 0;
  887. }
  888. /**
  889. * hostapd_setup_interface - Setup of an interface
  890. * @iface: Pointer to interface data.
  891. * Returns: 0 on success, -1 on failure
  892. *
  893. * Initializes the driver interface, validates the configuration,
  894. * and sets driver parameters based on the configuration.
  895. * Flushes old stations, sets the channel, encryption,
  896. * beacons, and WDS links based on the configuration.
  897. */
  898. int hostapd_setup_interface(struct hostapd_iface *iface)
  899. {
  900. int ret;
  901. ret = setup_interface(iface);
  902. if (ret) {
  903. wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
  904. iface->bss[0]->conf->iface);
  905. return -1;
  906. }
  907. return 0;
  908. }
  909. /**
  910. * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
  911. * @hapd_iface: Pointer to interface data
  912. * @conf: Pointer to per-interface configuration
  913. * @bss: Pointer to per-BSS configuration for this BSS
  914. * Returns: Pointer to allocated BSS data
  915. *
  916. * This function is used to allocate per-BSS data structure. This data will be
  917. * freed after hostapd_cleanup() is called for it during interface
  918. * deinitialization.
  919. */
  920. struct hostapd_data *
  921. hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
  922. struct hostapd_config *conf,
  923. struct hostapd_bss_config *bss)
  924. {
  925. struct hostapd_data *hapd;
  926. hapd = os_zalloc(sizeof(*hapd));
  927. if (hapd == NULL)
  928. return NULL;
  929. hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
  930. hapd->iconf = conf;
  931. hapd->conf = bss;
  932. hapd->iface = hapd_iface;
  933. hapd->driver = hapd->iconf->driver;
  934. hapd->ctrl_sock = -1;
  935. return hapd;
  936. }
  937. void hostapd_interface_deinit(struct hostapd_iface *iface)
  938. {
  939. size_t j;
  940. if (iface == NULL)
  941. return;
  942. hostapd_cleanup_iface_pre(iface);
  943. for (j = 0; j < iface->num_bss; j++) {
  944. struct hostapd_data *hapd = iface->bss[j];
  945. hostapd_free_stas(hapd);
  946. hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
  947. hostapd_clear_wep(hapd);
  948. hostapd_cleanup(hapd);
  949. }
  950. }
  951. void hostapd_interface_free(struct hostapd_iface *iface)
  952. {
  953. size_t j;
  954. for (j = 0; j < iface->num_bss; j++)
  955. os_free(iface->bss[j]);
  956. hostapd_cleanup_iface(iface);
  957. }
  958. #ifdef HOSTAPD
  959. void hostapd_interface_deinit_free(struct hostapd_iface *iface)
  960. {
  961. const struct wpa_driver_ops *driver;
  962. void *drv_priv;
  963. if (iface == NULL)
  964. return;
  965. driver = iface->bss[0]->driver;
  966. drv_priv = iface->bss[0]->drv_priv;
  967. hostapd_interface_deinit(iface);
  968. if (driver && driver->hapd_deinit && drv_priv)
  969. driver->hapd_deinit(drv_priv);
  970. hostapd_interface_free(iface);
  971. }
  972. int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
  973. {
  974. if (hapd_iface->bss[0]->drv_priv != NULL) {
  975. wpa_printf(MSG_ERROR, "Interface %s already enabled",
  976. hapd_iface->conf->bss[0]->iface);
  977. return -1;
  978. }
  979. wpa_printf(MSG_DEBUG, "Enable interface %s",
  980. hapd_iface->conf->bss[0]->iface);
  981. if (hapd_iface->interfaces == NULL ||
  982. hapd_iface->interfaces->driver_init == NULL ||
  983. hapd_iface->interfaces->driver_init(hapd_iface) ||
  984. hostapd_setup_interface(hapd_iface)) {
  985. hostapd_interface_deinit_free(hapd_iface);
  986. return -1;
  987. }
  988. return 0;
  989. }
  990. int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
  991. {
  992. size_t j;
  993. wpa_printf(MSG_DEBUG, "Reload interface %s",
  994. hapd_iface->conf->bss[0]->iface);
  995. for (j = 0; j < hapd_iface->num_bss; j++)
  996. hostapd_set_security_params(hapd_iface->conf->bss[j]);
  997. if (hostapd_config_check(hapd_iface->conf) < 0) {
  998. wpa_printf(MSG_ERROR, "Updated configuration is invalid");
  999. return -1;
  1000. }
  1001. hostapd_clear_old(hapd_iface);
  1002. for (j = 0; j < hapd_iface->num_bss; j++)
  1003. hostapd_reload_bss(hapd_iface->bss[j]);
  1004. return 0;
  1005. }
  1006. int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
  1007. {
  1008. size_t j;
  1009. const struct wpa_driver_ops *driver;
  1010. void *drv_priv;
  1011. if (hapd_iface == NULL)
  1012. return -1;
  1013. driver = hapd_iface->bss[0]->driver;
  1014. drv_priv = hapd_iface->bss[0]->drv_priv;
  1015. /* whatever hostapd_interface_deinit does */
  1016. for (j = 0; j < hapd_iface->num_bss; j++) {
  1017. struct hostapd_data *hapd = hapd_iface->bss[j];
  1018. hostapd_free_stas(hapd);
  1019. hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
  1020. hostapd_clear_wep(hapd);
  1021. hostapd_free_hapd_data(hapd);
  1022. }
  1023. if (driver && driver->hapd_deinit && drv_priv) {
  1024. driver->hapd_deinit(drv_priv);
  1025. hapd_iface->bss[0]->drv_priv = NULL;
  1026. }
  1027. /* From hostapd_cleanup_iface: These were initialized in
  1028. * hostapd_setup_interface and hostapd_setup_interface_complete
  1029. */
  1030. hostapd_cleanup_iface_partial(hapd_iface);
  1031. wpa_printf(MSG_DEBUG, "Interface %s disabled",
  1032. hapd_iface->bss[0]->conf->iface);
  1033. return 0;
  1034. }
  1035. static struct hostapd_iface *
  1036. hostapd_iface_alloc(struct hapd_interfaces *interfaces)
  1037. {
  1038. struct hostapd_iface **iface, *hapd_iface;
  1039. iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
  1040. sizeof(struct hostapd_iface *));
  1041. if (iface == NULL)
  1042. return NULL;
  1043. interfaces->iface = iface;
  1044. hapd_iface = interfaces->iface[interfaces->count] =
  1045. os_zalloc(sizeof(*hapd_iface));
  1046. if (hapd_iface == NULL) {
  1047. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
  1048. "the interface", __func__);
  1049. return NULL;
  1050. }
  1051. interfaces->count++;
  1052. hapd_iface->interfaces = interfaces;
  1053. return hapd_iface;
  1054. }
  1055. static struct hostapd_config *
  1056. hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
  1057. const char *ctrl_iface)
  1058. {
  1059. struct hostapd_bss_config *bss;
  1060. struct hostapd_config *conf;
  1061. /* Allocates memory for bss and conf */
  1062. conf = hostapd_config_defaults();
  1063. if (conf == NULL) {
  1064. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
  1065. "configuration", __func__);
  1066. return NULL;
  1067. }
  1068. conf->driver = wpa_drivers[0];
  1069. if (conf->driver == NULL) {
  1070. wpa_printf(MSG_ERROR, "No driver wrappers registered!");
  1071. hostapd_config_free(conf);
  1072. return NULL;
  1073. }
  1074. bss = conf->last_bss = conf->bss[0];
  1075. os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
  1076. bss->ctrl_interface = os_strdup(ctrl_iface);
  1077. if (bss->ctrl_interface == NULL) {
  1078. hostapd_config_free(conf);
  1079. return NULL;
  1080. }
  1081. /* Reading configuration file skipped, will be done in SET!
  1082. * From reading the configuration till the end has to be done in
  1083. * SET
  1084. */
  1085. return conf;
  1086. }
  1087. static struct hostapd_iface * hostapd_data_alloc(
  1088. struct hapd_interfaces *interfaces, struct hostapd_config *conf)
  1089. {
  1090. size_t i;
  1091. struct hostapd_iface *hapd_iface =
  1092. interfaces->iface[interfaces->count - 1];
  1093. struct hostapd_data *hapd;
  1094. hapd_iface->conf = conf;
  1095. hapd_iface->num_bss = conf->num_bss;
  1096. hapd_iface->bss = os_zalloc(conf->num_bss *
  1097. sizeof(struct hostapd_data *));
  1098. if (hapd_iface->bss == NULL)
  1099. return NULL;
  1100. for (i = 0; i < conf->num_bss; i++) {
  1101. hapd = hapd_iface->bss[i] =
  1102. hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
  1103. if (hapd == NULL)
  1104. return NULL;
  1105. hapd->msg_ctx = hapd;
  1106. }
  1107. hapd_iface->interfaces = interfaces;
  1108. return hapd_iface;
  1109. }
  1110. int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
  1111. {
  1112. struct hostapd_config *conf = NULL;
  1113. struct hostapd_iface *hapd_iface = NULL;
  1114. char *ptr;
  1115. size_t i;
  1116. const char *conf_file = NULL;
  1117. ptr = os_strchr(buf, ' ');
  1118. if (ptr == NULL)
  1119. return -1;
  1120. *ptr++ = '\0';
  1121. if (os_strncmp(ptr, "config=", 7) == 0)
  1122. conf_file = ptr + 7;
  1123. for (i = 0; i < interfaces->count; i++) {
  1124. if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
  1125. buf)) {
  1126. wpa_printf(MSG_INFO, "Cannot add interface - it "
  1127. "already exists");
  1128. return -1;
  1129. }
  1130. }
  1131. hapd_iface = hostapd_iface_alloc(interfaces);
  1132. if (hapd_iface == NULL) {
  1133. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
  1134. "for interface", __func__);
  1135. goto fail;
  1136. }
  1137. if (conf_file && interfaces->config_read_cb) {
  1138. conf = interfaces->config_read_cb(conf_file);
  1139. if (conf && conf->bss)
  1140. os_strlcpy(conf->bss[0]->iface, buf,
  1141. sizeof(conf->bss[0]->iface));
  1142. } else
  1143. conf = hostapd_config_alloc(interfaces, buf, ptr);
  1144. if (conf == NULL || conf->bss == NULL) {
  1145. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
  1146. "for configuration", __func__);
  1147. goto fail;
  1148. }
  1149. hapd_iface = hostapd_data_alloc(interfaces, conf);
  1150. if (hapd_iface == NULL) {
  1151. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
  1152. "for hostapd", __func__);
  1153. goto fail;
  1154. }
  1155. if (hapd_iface->interfaces &&
  1156. hapd_iface->interfaces->ctrl_iface_init &&
  1157. hapd_iface->interfaces->ctrl_iface_init(hapd_iface->bss[0])) {
  1158. wpa_printf(MSG_ERROR, "%s: Failed to setup control "
  1159. "interface", __func__);
  1160. goto fail;
  1161. }
  1162. wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0]->iface);
  1163. return 0;
  1164. fail:
  1165. if (conf)
  1166. hostapd_config_free(conf);
  1167. if (hapd_iface) {
  1168. os_free(hapd_iface->bss[interfaces->count]);
  1169. os_free(hapd_iface);
  1170. }
  1171. return -1;
  1172. }
  1173. int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
  1174. {
  1175. struct hostapd_iface *hapd_iface;
  1176. size_t i, k = 0;
  1177. for (i = 0; i < interfaces->count; i++) {
  1178. hapd_iface = interfaces->iface[i];
  1179. if (hapd_iface == NULL)
  1180. return -1;
  1181. if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
  1182. wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
  1183. hostapd_interface_deinit_free(hapd_iface);
  1184. k = i;
  1185. while (k < (interfaces->count - 1)) {
  1186. interfaces->iface[k] =
  1187. interfaces->iface[k + 1];
  1188. k++;
  1189. }
  1190. interfaces->count--;
  1191. return 0;
  1192. }
  1193. }
  1194. return -1;
  1195. }
  1196. #endif /* HOSTAPD */
  1197. /**
  1198. * hostapd_new_assoc_sta - Notify that a new station associated with the AP
  1199. * @hapd: Pointer to BSS data
  1200. * @sta: Pointer to the associated STA data
  1201. * @reassoc: 1 to indicate this was a re-association; 0 = first association
  1202. *
  1203. * This function will be called whenever a station associates with the AP. It
  1204. * can be called from ieee802_11.c for drivers that export MLME to hostapd and
  1205. * from drv_callbacks.c based on driver events for drivers that take care of
  1206. * management frames (IEEE 802.11 authentication and association) internally.
  1207. */
  1208. void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
  1209. int reassoc)
  1210. {
  1211. if (hapd->tkip_countermeasures) {
  1212. hostapd_drv_sta_deauth(hapd, sta->addr,
  1213. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1214. return;
  1215. }
  1216. hostapd_prune_associations(hapd, sta->addr);
  1217. /* IEEE 802.11F (IAPP) */
  1218. if (hapd->conf->ieee802_11f)
  1219. iapp_new_station(hapd->iapp, sta);
  1220. #ifdef CONFIG_P2P
  1221. if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
  1222. sta->no_p2p_set = 1;
  1223. hapd->num_sta_no_p2p++;
  1224. if (hapd->num_sta_no_p2p == 1)
  1225. hostapd_p2p_non_p2p_sta_connected(hapd);
  1226. }
  1227. #endif /* CONFIG_P2P */
  1228. /* Start accounting here, if IEEE 802.1X and WPA are not used.
  1229. * IEEE 802.1X/WPA code will start accounting after the station has
  1230. * been authorized. */
  1231. if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
  1232. os_get_time(&sta->connected_time);
  1233. accounting_sta_start(hapd, sta);
  1234. }
  1235. /* Start IEEE 802.1X authentication process for new stations */
  1236. ieee802_1x_new_station(hapd, sta);
  1237. if (reassoc) {
  1238. if (sta->auth_alg != WLAN_AUTH_FT &&
  1239. !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
  1240. wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
  1241. } else
  1242. wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
  1243. wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
  1244. "for " MACSTR " (%d seconds - ap_max_inactivity)",
  1245. __func__, MAC2STR(sta->addr),
  1246. hapd->conf->ap_max_inactivity);
  1247. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  1248. eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
  1249. ap_handle_timer, hapd, sta);
  1250. }