hostapd.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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. void hostapd_interface_deinit_free(struct hostapd_iface *iface)
  959. {
  960. const struct wpa_driver_ops *driver;
  961. void *drv_priv;
  962. if (iface == NULL)
  963. return;
  964. driver = iface->bss[0]->driver;
  965. drv_priv = iface->bss[0]->drv_priv;
  966. hostapd_interface_deinit(iface);
  967. if (driver && driver->hapd_deinit && drv_priv)
  968. driver->hapd_deinit(drv_priv);
  969. hostapd_interface_free(iface);
  970. }
  971. int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
  972. {
  973. if (hapd_iface->bss[0]->drv_priv != NULL) {
  974. wpa_printf(MSG_ERROR, "Interface %s already enabled",
  975. hapd_iface->conf->bss[0]->iface);
  976. return -1;
  977. }
  978. wpa_printf(MSG_DEBUG, "Enable interface %s",
  979. hapd_iface->conf->bss[0]->iface);
  980. if (hapd_iface->interfaces == NULL ||
  981. hapd_iface->interfaces->driver_init == NULL ||
  982. hapd_iface->interfaces->driver_init(hapd_iface) ||
  983. hostapd_setup_interface(hapd_iface)) {
  984. hostapd_interface_deinit_free(hapd_iface);
  985. return -1;
  986. }
  987. return 0;
  988. }
  989. int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
  990. {
  991. size_t j;
  992. wpa_printf(MSG_DEBUG, "Reload interface %s",
  993. hapd_iface->conf->bss[0]->iface);
  994. for (j = 0; j < hapd_iface->num_bss; j++)
  995. hostapd_set_security_params(hapd_iface->conf->bss[j]);
  996. if (hostapd_config_check(hapd_iface->conf) < 0) {
  997. wpa_printf(MSG_ERROR, "Updated configuration is invalid");
  998. return -1;
  999. }
  1000. hostapd_clear_old(hapd_iface);
  1001. for (j = 0; j < hapd_iface->num_bss; j++)
  1002. hostapd_reload_bss(hapd_iface->bss[j]);
  1003. return 0;
  1004. }
  1005. int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
  1006. {
  1007. size_t j;
  1008. const struct wpa_driver_ops *driver;
  1009. void *drv_priv;
  1010. if (hapd_iface == NULL)
  1011. return -1;
  1012. driver = hapd_iface->bss[0]->driver;
  1013. drv_priv = hapd_iface->bss[0]->drv_priv;
  1014. /* whatever hostapd_interface_deinit does */
  1015. for (j = 0; j < hapd_iface->num_bss; j++) {
  1016. struct hostapd_data *hapd = hapd_iface->bss[j];
  1017. hostapd_free_stas(hapd);
  1018. hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
  1019. hostapd_clear_wep(hapd);
  1020. hostapd_free_hapd_data(hapd);
  1021. }
  1022. if (driver && driver->hapd_deinit && drv_priv) {
  1023. driver->hapd_deinit(drv_priv);
  1024. hapd_iface->bss[0]->drv_priv = NULL;
  1025. }
  1026. /* From hostapd_cleanup_iface: These were initialized in
  1027. * hostapd_setup_interface and hostapd_setup_interface_complete
  1028. */
  1029. hostapd_cleanup_iface_partial(hapd_iface);
  1030. wpa_printf(MSG_DEBUG, "Interface %s disabled",
  1031. hapd_iface->bss[0]->conf->iface);
  1032. return 0;
  1033. }
  1034. static struct hostapd_iface *
  1035. hostapd_iface_alloc(struct hapd_interfaces *interfaces)
  1036. {
  1037. struct hostapd_iface **iface, *hapd_iface;
  1038. iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
  1039. sizeof(struct hostapd_iface *));
  1040. if (iface == NULL)
  1041. return NULL;
  1042. interfaces->iface = iface;
  1043. hapd_iface = interfaces->iface[interfaces->count] =
  1044. os_zalloc(sizeof(*hapd_iface));
  1045. if (hapd_iface == NULL) {
  1046. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
  1047. "the interface", __func__);
  1048. return NULL;
  1049. }
  1050. interfaces->count++;
  1051. hapd_iface->interfaces = interfaces;
  1052. return hapd_iface;
  1053. }
  1054. static struct hostapd_config *
  1055. hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
  1056. const char *ctrl_iface)
  1057. {
  1058. struct hostapd_bss_config *bss;
  1059. struct hostapd_config *conf;
  1060. /* Allocates memory for bss and conf */
  1061. conf = hostapd_config_defaults();
  1062. if (conf == NULL) {
  1063. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
  1064. "configuration", __func__);
  1065. return NULL;
  1066. }
  1067. conf->driver = wpa_drivers[0];
  1068. if (conf->driver == NULL) {
  1069. wpa_printf(MSG_ERROR, "No driver wrappers registered!");
  1070. hostapd_config_free(conf);
  1071. return NULL;
  1072. }
  1073. bss = conf->last_bss = conf->bss[0];
  1074. os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
  1075. bss->ctrl_interface = os_strdup(ctrl_iface);
  1076. if (bss->ctrl_interface == NULL) {
  1077. hostapd_config_free(conf);
  1078. return NULL;
  1079. }
  1080. /* Reading configuration file skipped, will be done in SET!
  1081. * From reading the configuration till the end has to be done in
  1082. * SET
  1083. */
  1084. return conf;
  1085. }
  1086. static struct hostapd_iface * hostapd_data_alloc(
  1087. struct hapd_interfaces *interfaces, struct hostapd_config *conf)
  1088. {
  1089. size_t i;
  1090. struct hostapd_iface *hapd_iface =
  1091. interfaces->iface[interfaces->count - 1];
  1092. struct hostapd_data *hapd;
  1093. hapd_iface->conf = conf;
  1094. hapd_iface->num_bss = conf->num_bss;
  1095. hapd_iface->bss = os_zalloc(conf->num_bss *
  1096. sizeof(struct hostapd_data *));
  1097. if (hapd_iface->bss == NULL)
  1098. return NULL;
  1099. for (i = 0; i < conf->num_bss; i++) {
  1100. hapd = hapd_iface->bss[i] =
  1101. hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
  1102. if (hapd == NULL)
  1103. return NULL;
  1104. hapd->msg_ctx = hapd;
  1105. }
  1106. hapd_iface->interfaces = interfaces;
  1107. return hapd_iface;
  1108. }
  1109. int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
  1110. {
  1111. struct hostapd_config *conf = NULL;
  1112. struct hostapd_iface *hapd_iface = NULL;
  1113. char *ptr;
  1114. size_t i;
  1115. const char *conf_file = NULL;
  1116. ptr = os_strchr(buf, ' ');
  1117. if (ptr == NULL)
  1118. return -1;
  1119. *ptr++ = '\0';
  1120. if (os_strncmp(ptr, "config=", 7) == 0)
  1121. conf_file = ptr + 7;
  1122. for (i = 0; i < interfaces->count; i++) {
  1123. if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
  1124. buf)) {
  1125. wpa_printf(MSG_INFO, "Cannot add interface - it "
  1126. "already exists");
  1127. return -1;
  1128. }
  1129. }
  1130. hapd_iface = hostapd_iface_alloc(interfaces);
  1131. if (hapd_iface == NULL) {
  1132. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
  1133. "for interface", __func__);
  1134. goto fail;
  1135. }
  1136. if (conf_file && interfaces->config_read_cb) {
  1137. conf = interfaces->config_read_cb(conf_file);
  1138. if (conf && conf->bss)
  1139. os_strlcpy(conf->bss[0]->iface, buf,
  1140. sizeof(conf->bss[0]->iface));
  1141. } else
  1142. conf = hostapd_config_alloc(interfaces, buf, ptr);
  1143. if (conf == NULL || conf->bss == NULL) {
  1144. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
  1145. "for configuration", __func__);
  1146. goto fail;
  1147. }
  1148. hapd_iface = hostapd_data_alloc(interfaces, conf);
  1149. if (hapd_iface == NULL) {
  1150. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
  1151. "for hostapd", __func__);
  1152. goto fail;
  1153. }
  1154. if (hapd_iface->interfaces &&
  1155. hapd_iface->interfaces->ctrl_iface_init &&
  1156. hapd_iface->interfaces->ctrl_iface_init(hapd_iface->bss[0])) {
  1157. wpa_printf(MSG_ERROR, "%s: Failed to setup control "
  1158. "interface", __func__);
  1159. goto fail;
  1160. }
  1161. wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0]->iface);
  1162. return 0;
  1163. fail:
  1164. if (conf)
  1165. hostapd_config_free(conf);
  1166. if (hapd_iface) {
  1167. os_free(hapd_iface->bss[interfaces->count]);
  1168. os_free(hapd_iface);
  1169. }
  1170. return -1;
  1171. }
  1172. int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
  1173. {
  1174. struct hostapd_iface *hapd_iface;
  1175. size_t i, k = 0;
  1176. for (i = 0; i < interfaces->count; i++) {
  1177. hapd_iface = interfaces->iface[i];
  1178. if (hapd_iface == NULL)
  1179. return -1;
  1180. if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
  1181. wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
  1182. hostapd_interface_deinit_free(hapd_iface);
  1183. k = i;
  1184. while (k < (interfaces->count - 1)) {
  1185. interfaces->iface[k] =
  1186. interfaces->iface[k + 1];
  1187. k++;
  1188. }
  1189. interfaces->count--;
  1190. return 0;
  1191. }
  1192. }
  1193. return -1;
  1194. }
  1195. /**
  1196. * hostapd_new_assoc_sta - Notify that a new station associated with the AP
  1197. * @hapd: Pointer to BSS data
  1198. * @sta: Pointer to the associated STA data
  1199. * @reassoc: 1 to indicate this was a re-association; 0 = first association
  1200. *
  1201. * This function will be called whenever a station associates with the AP. It
  1202. * can be called from ieee802_11.c for drivers that export MLME to hostapd and
  1203. * from drv_callbacks.c based on driver events for drivers that take care of
  1204. * management frames (IEEE 802.11 authentication and association) internally.
  1205. */
  1206. void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
  1207. int reassoc)
  1208. {
  1209. if (hapd->tkip_countermeasures) {
  1210. hostapd_drv_sta_deauth(hapd, sta->addr,
  1211. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1212. return;
  1213. }
  1214. hostapd_prune_associations(hapd, sta->addr);
  1215. /* IEEE 802.11F (IAPP) */
  1216. if (hapd->conf->ieee802_11f)
  1217. iapp_new_station(hapd->iapp, sta);
  1218. #ifdef CONFIG_P2P
  1219. if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
  1220. sta->no_p2p_set = 1;
  1221. hapd->num_sta_no_p2p++;
  1222. if (hapd->num_sta_no_p2p == 1)
  1223. hostapd_p2p_non_p2p_sta_connected(hapd);
  1224. }
  1225. #endif /* CONFIG_P2P */
  1226. /* Start accounting here, if IEEE 802.1X and WPA are not used.
  1227. * IEEE 802.1X/WPA code will start accounting after the station has
  1228. * been authorized. */
  1229. if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
  1230. os_get_time(&sta->connected_time);
  1231. accounting_sta_start(hapd, sta);
  1232. }
  1233. /* Start IEEE 802.1X authentication process for new stations */
  1234. ieee802_1x_new_station(hapd, sta);
  1235. if (reassoc) {
  1236. if (sta->auth_alg != WLAN_AUTH_FT &&
  1237. !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
  1238. wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
  1239. } else
  1240. wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
  1241. wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
  1242. "for " MACSTR " (%d seconds - ap_max_inactivity)",
  1243. __func__, MAC2STR(sta->addr),
  1244. hapd->conf->ap_max_inactivity);
  1245. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  1246. eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
  1247. ap_handle_timer, hapd, sta);
  1248. }