hostapd.c 35 KB

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