hostapd.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983
  1. /*
  2. * hostapd / Initialization and configuration
  3. * Copyright (c) 2002-2013, 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 "common/wpa_ctrl.h"
  13. #include "radius/radius_client.h"
  14. #include "radius/radius_das.h"
  15. #include "drivers/driver.h"
  16. #include "hostapd.h"
  17. #include "authsrv.h"
  18. #include "sta_info.h"
  19. #include "accounting.h"
  20. #include "ap_list.h"
  21. #include "beacon.h"
  22. #include "iapp.h"
  23. #include "ieee802_1x.h"
  24. #include "ieee802_11_auth.h"
  25. #include "vlan_init.h"
  26. #include "wpa_auth.h"
  27. #include "wps_hostapd.h"
  28. #include "hw_features.h"
  29. #include "wpa_auth_glue.h"
  30. #include "ap_drv_ops.h"
  31. #include "ap_config.h"
  32. #include "p2p_hostapd.h"
  33. #include "gas_serv.h"
  34. #include "dfs.h"
  35. static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
  36. static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
  37. static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
  38. static int setup_interface2(struct hostapd_iface *iface);
  39. static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
  40. extern int wpa_debug_level;
  41. extern struct wpa_driver_ops *wpa_drivers[];
  42. int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
  43. int (*cb)(struct hostapd_iface *iface,
  44. void *ctx), void *ctx)
  45. {
  46. size_t i;
  47. int ret;
  48. for (i = 0; i < interfaces->count; i++) {
  49. ret = cb(interfaces->iface[i], ctx);
  50. if (ret)
  51. return ret;
  52. }
  53. return 0;
  54. }
  55. static void hostapd_reload_bss(struct hostapd_data *hapd)
  56. {
  57. struct hostapd_ssid *ssid;
  58. #ifndef CONFIG_NO_RADIUS
  59. radius_client_reconfig(hapd->radius, hapd->conf->radius);
  60. #endif /* CONFIG_NO_RADIUS */
  61. ssid = &hapd->conf->ssid;
  62. if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
  63. ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
  64. /*
  65. * Force PSK to be derived again since SSID or passphrase may
  66. * have changed.
  67. */
  68. os_free(ssid->wpa_psk);
  69. ssid->wpa_psk = NULL;
  70. }
  71. if (hostapd_setup_wpa_psk(hapd->conf)) {
  72. wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
  73. "after reloading configuration");
  74. }
  75. if (hapd->conf->ieee802_1x || hapd->conf->wpa)
  76. hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
  77. else
  78. hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
  79. if (hapd->conf->wpa && hapd->wpa_auth == NULL) {
  80. hostapd_setup_wpa(hapd);
  81. if (hapd->wpa_auth)
  82. wpa_init_keys(hapd->wpa_auth);
  83. } else if (hapd->conf->wpa) {
  84. const u8 *wpa_ie;
  85. size_t wpa_ie_len;
  86. hostapd_reconfig_wpa(hapd);
  87. wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
  88. if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
  89. wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
  90. "the kernel driver.");
  91. } else if (hapd->wpa_auth) {
  92. wpa_deinit(hapd->wpa_auth);
  93. hapd->wpa_auth = NULL;
  94. hostapd_set_privacy(hapd, 0);
  95. hostapd_setup_encryption(hapd->conf->iface, hapd);
  96. hostapd_set_generic_elem(hapd, (u8 *) "", 0);
  97. }
  98. ieee802_11_set_beacon(hapd);
  99. hostapd_update_wps(hapd);
  100. if (hapd->conf->ssid.ssid_set &&
  101. hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
  102. hapd->conf->ssid.ssid_len)) {
  103. wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
  104. /* try to continue */
  105. }
  106. wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
  107. }
  108. static void hostapd_clear_old(struct hostapd_iface *iface)
  109. {
  110. size_t j;
  111. /*
  112. * Deauthenticate all stations since the new configuration may not
  113. * allow them to use the BSS anymore.
  114. */
  115. for (j = 0; j < iface->num_bss; j++) {
  116. hostapd_flush_old_stations(iface->bss[j],
  117. WLAN_REASON_PREV_AUTH_NOT_VALID);
  118. hostapd_broadcast_wep_clear(iface->bss[j]);
  119. #ifndef CONFIG_NO_RADIUS
  120. /* TODO: update dynamic data based on changed configuration
  121. * items (e.g., open/close sockets, etc.) */
  122. radius_client_flush(iface->bss[j]->radius, 0);
  123. #endif /* CONFIG_NO_RADIUS */
  124. }
  125. }
  126. int hostapd_reload_config(struct hostapd_iface *iface)
  127. {
  128. struct hostapd_data *hapd = iface->bss[0];
  129. struct hostapd_config *newconf, *oldconf;
  130. size_t j;
  131. if (iface->config_fname == NULL) {
  132. /* Only in-memory config in use - assume it has been updated */
  133. hostapd_clear_old(iface);
  134. for (j = 0; j < iface->num_bss; j++)
  135. hostapd_reload_bss(iface->bss[j]);
  136. return 0;
  137. }
  138. if (iface->interfaces == NULL ||
  139. iface->interfaces->config_read_cb == NULL)
  140. return -1;
  141. newconf = iface->interfaces->config_read_cb(iface->config_fname);
  142. if (newconf == NULL)
  143. return -1;
  144. hostapd_clear_old(iface);
  145. oldconf = hapd->iconf;
  146. iface->conf = newconf;
  147. for (j = 0; j < iface->num_bss; j++) {
  148. hapd = iface->bss[j];
  149. hapd->iconf = newconf;
  150. hapd->conf = newconf->bss[j];
  151. hostapd_reload_bss(hapd);
  152. }
  153. hostapd_config_free(oldconf);
  154. return 0;
  155. }
  156. static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
  157. char *ifname)
  158. {
  159. int i;
  160. for (i = 0; i < NUM_WEP_KEYS; i++) {
  161. if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
  162. 0, NULL, 0, NULL, 0)) {
  163. wpa_printf(MSG_DEBUG, "Failed to clear default "
  164. "encryption keys (ifname=%s keyidx=%d)",
  165. ifname, i);
  166. }
  167. }
  168. #ifdef CONFIG_IEEE80211W
  169. if (hapd->conf->ieee80211w) {
  170. for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
  171. if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
  172. NULL, i, 0, NULL,
  173. 0, NULL, 0)) {
  174. wpa_printf(MSG_DEBUG, "Failed to clear "
  175. "default mgmt encryption keys "
  176. "(ifname=%s keyidx=%d)", ifname, i);
  177. }
  178. }
  179. }
  180. #endif /* CONFIG_IEEE80211W */
  181. }
  182. static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
  183. {
  184. hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
  185. return 0;
  186. }
  187. static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
  188. {
  189. int errors = 0, idx;
  190. struct hostapd_ssid *ssid = &hapd->conf->ssid;
  191. idx = ssid->wep.idx;
  192. if (ssid->wep.default_len &&
  193. hostapd_drv_set_key(hapd->conf->iface,
  194. hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
  195. 1, NULL, 0, ssid->wep.key[idx],
  196. ssid->wep.len[idx])) {
  197. wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
  198. errors++;
  199. }
  200. return errors;
  201. }
  202. static void hostapd_free_hapd_data(struct hostapd_data *hapd)
  203. {
  204. iapp_deinit(hapd->iapp);
  205. hapd->iapp = NULL;
  206. accounting_deinit(hapd);
  207. hostapd_deinit_wpa(hapd);
  208. vlan_deinit(hapd);
  209. hostapd_acl_deinit(hapd);
  210. #ifndef CONFIG_NO_RADIUS
  211. radius_client_deinit(hapd->radius);
  212. hapd->radius = NULL;
  213. radius_das_deinit(hapd->radius_das);
  214. hapd->radius_das = NULL;
  215. #endif /* CONFIG_NO_RADIUS */
  216. hostapd_deinit_wps(hapd);
  217. authsrv_deinit(hapd);
  218. if (hapd->interface_added &&
  219. hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
  220. wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
  221. hapd->conf->iface);
  222. }
  223. os_free(hapd->probereq_cb);
  224. hapd->probereq_cb = NULL;
  225. #ifdef CONFIG_P2P
  226. wpabuf_free(hapd->p2p_beacon_ie);
  227. hapd->p2p_beacon_ie = NULL;
  228. wpabuf_free(hapd->p2p_probe_resp_ie);
  229. hapd->p2p_probe_resp_ie = NULL;
  230. #endif /* CONFIG_P2P */
  231. wpabuf_free(hapd->time_adv);
  232. #ifdef CONFIG_INTERWORKING
  233. gas_serv_deinit(hapd);
  234. #endif /* CONFIG_INTERWORKING */
  235. #ifdef CONFIG_SQLITE
  236. os_free(hapd->tmp_eap_user.identity);
  237. os_free(hapd->tmp_eap_user.password);
  238. #endif /* CONFIG_SQLITE */
  239. }
  240. /**
  241. * hostapd_cleanup - Per-BSS cleanup (deinitialization)
  242. * @hapd: Pointer to BSS data
  243. *
  244. * This function is used to free all per-BSS data structures and resources.
  245. * This gets called in a loop for each BSS between calls to
  246. * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
  247. * is deinitialized. Most of the modules that are initialized in
  248. * hostapd_setup_bss() are deinitialized here.
  249. */
  250. static void hostapd_cleanup(struct hostapd_data *hapd)
  251. {
  252. if (hapd->iface->interfaces &&
  253. hapd->iface->interfaces->ctrl_iface_deinit)
  254. hapd->iface->interfaces->ctrl_iface_deinit(hapd);
  255. hostapd_free_hapd_data(hapd);
  256. }
  257. /**
  258. * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
  259. * @iface: Pointer to interface data
  260. *
  261. * This function is called before per-BSS data structures are deinitialized
  262. * with hostapd_cleanup().
  263. */
  264. static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
  265. {
  266. }
  267. static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
  268. {
  269. hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
  270. iface->hw_features = NULL;
  271. os_free(iface->current_rates);
  272. iface->current_rates = NULL;
  273. os_free(iface->basic_rates);
  274. iface->basic_rates = NULL;
  275. ap_list_deinit(iface);
  276. }
  277. /**
  278. * hostapd_cleanup_iface - Complete per-interface cleanup
  279. * @iface: Pointer to interface data
  280. *
  281. * This function is called after per-BSS data structures are deinitialized
  282. * with hostapd_cleanup().
  283. */
  284. static void hostapd_cleanup_iface(struct hostapd_iface *iface)
  285. {
  286. eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
  287. hostapd_cleanup_iface_partial(iface);
  288. hostapd_config_free(iface->conf);
  289. iface->conf = NULL;
  290. os_free(iface->config_fname);
  291. os_free(iface->bss);
  292. os_free(iface);
  293. }
  294. static void hostapd_clear_wep(struct hostapd_data *hapd)
  295. {
  296. if (hapd->drv_priv) {
  297. hostapd_set_privacy(hapd, 0);
  298. hostapd_broadcast_wep_clear(hapd);
  299. }
  300. }
  301. static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
  302. {
  303. int i;
  304. hostapd_broadcast_wep_set(hapd);
  305. if (hapd->conf->ssid.wep.default_len) {
  306. hostapd_set_privacy(hapd, 1);
  307. return 0;
  308. }
  309. /*
  310. * When IEEE 802.1X is not enabled, the driver may need to know how to
  311. * set authentication algorithms for static WEP.
  312. */
  313. hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
  314. for (i = 0; i < 4; i++) {
  315. if (hapd->conf->ssid.wep.key[i] &&
  316. hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
  317. i == hapd->conf->ssid.wep.idx, NULL, 0,
  318. hapd->conf->ssid.wep.key[i],
  319. hapd->conf->ssid.wep.len[i])) {
  320. wpa_printf(MSG_WARNING, "Could not set WEP "
  321. "encryption.");
  322. return -1;
  323. }
  324. if (hapd->conf->ssid.wep.key[i] &&
  325. i == hapd->conf->ssid.wep.idx)
  326. hostapd_set_privacy(hapd, 1);
  327. }
  328. return 0;
  329. }
  330. static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
  331. {
  332. int ret = 0;
  333. u8 addr[ETH_ALEN];
  334. if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
  335. return 0;
  336. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Flushing old station entries");
  337. if (hostapd_flush(hapd)) {
  338. wpa_msg(hapd->msg_ctx, MSG_WARNING, "Could not connect to "
  339. "kernel driver");
  340. ret = -1;
  341. }
  342. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
  343. os_memset(addr, 0xff, ETH_ALEN);
  344. hostapd_drv_sta_deauth(hapd, addr, reason);
  345. hostapd_free_stas(hapd);
  346. return ret;
  347. }
  348. /**
  349. * hostapd_validate_bssid_configuration - Validate BSSID configuration
  350. * @iface: Pointer to interface data
  351. * Returns: 0 on success, -1 on failure
  352. *
  353. * This function is used to validate that the configured BSSIDs are valid.
  354. */
  355. static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
  356. {
  357. u8 mask[ETH_ALEN] = { 0 };
  358. struct hostapd_data *hapd = iface->bss[0];
  359. unsigned int i = iface->conf->num_bss, bits = 0, j;
  360. int auto_addr = 0;
  361. if (hostapd_drv_none(hapd))
  362. return 0;
  363. /* Generate BSSID mask that is large enough to cover the BSSIDs. */
  364. /* Determine the bits necessary to cover the number of BSSIDs. */
  365. for (i--; i; i >>= 1)
  366. bits++;
  367. /* Determine the bits necessary to any configured BSSIDs,
  368. if they are higher than the number of BSSIDs. */
  369. for (j = 0; j < iface->conf->num_bss; j++) {
  370. if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
  371. if (j)
  372. auto_addr++;
  373. continue;
  374. }
  375. for (i = 0; i < ETH_ALEN; i++) {
  376. mask[i] |=
  377. iface->conf->bss[j]->bssid[i] ^
  378. hapd->own_addr[i];
  379. }
  380. }
  381. if (!auto_addr)
  382. goto skip_mask_ext;
  383. for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
  384. ;
  385. j = 0;
  386. if (i < ETH_ALEN) {
  387. j = (5 - i) * 8;
  388. while (mask[i] != 0) {
  389. mask[i] >>= 1;
  390. j++;
  391. }
  392. }
  393. if (bits < j)
  394. bits = j;
  395. if (bits > 40) {
  396. wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
  397. bits);
  398. return -1;
  399. }
  400. os_memset(mask, 0xff, ETH_ALEN);
  401. j = bits / 8;
  402. for (i = 5; i > 5 - j; i--)
  403. mask[i] = 0;
  404. j = bits % 8;
  405. while (j--)
  406. mask[i] <<= 1;
  407. skip_mask_ext:
  408. wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
  409. (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
  410. if (!auto_addr)
  411. return 0;
  412. for (i = 0; i < ETH_ALEN; i++) {
  413. if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
  414. wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
  415. " for start address " MACSTR ".",
  416. MAC2STR(mask), MAC2STR(hapd->own_addr));
  417. wpa_printf(MSG_ERROR, "Start address must be the "
  418. "first address in the block (i.e., addr "
  419. "AND mask == addr).");
  420. return -1;
  421. }
  422. }
  423. return 0;
  424. }
  425. static int mac_in_conf(struct hostapd_config *conf, const void *a)
  426. {
  427. size_t i;
  428. for (i = 0; i < conf->num_bss; i++) {
  429. if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
  430. return 1;
  431. }
  432. }
  433. return 0;
  434. }
  435. #ifndef CONFIG_NO_RADIUS
  436. static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
  437. struct radius_das_attrs *attr)
  438. {
  439. /* TODO */
  440. return 0;
  441. }
  442. static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
  443. struct radius_das_attrs *attr)
  444. {
  445. struct sta_info *sta = NULL;
  446. char buf[128];
  447. if (attr->sta_addr)
  448. sta = ap_get_sta(hapd, attr->sta_addr);
  449. if (sta == NULL && attr->acct_session_id &&
  450. attr->acct_session_id_len == 17) {
  451. for (sta = hapd->sta_list; sta; sta = sta->next) {
  452. os_snprintf(buf, sizeof(buf), "%08X-%08X",
  453. sta->acct_session_id_hi,
  454. sta->acct_session_id_lo);
  455. if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
  456. break;
  457. }
  458. }
  459. if (sta == NULL && attr->cui) {
  460. for (sta = hapd->sta_list; sta; sta = sta->next) {
  461. struct wpabuf *cui;
  462. cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
  463. if (cui && wpabuf_len(cui) == attr->cui_len &&
  464. os_memcmp(wpabuf_head(cui), attr->cui,
  465. attr->cui_len) == 0)
  466. break;
  467. }
  468. }
  469. if (sta == NULL && attr->user_name) {
  470. for (sta = hapd->sta_list; sta; sta = sta->next) {
  471. u8 *identity;
  472. size_t identity_len;
  473. identity = ieee802_1x_get_identity(sta->eapol_sm,
  474. &identity_len);
  475. if (identity &&
  476. identity_len == attr->user_name_len &&
  477. os_memcmp(identity, attr->user_name, identity_len)
  478. == 0)
  479. break;
  480. }
  481. }
  482. return sta;
  483. }
  484. static enum radius_das_res
  485. hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
  486. {
  487. struct hostapd_data *hapd = ctx;
  488. struct sta_info *sta;
  489. if (hostapd_das_nas_mismatch(hapd, attr))
  490. return RADIUS_DAS_NAS_MISMATCH;
  491. sta = hostapd_das_find_sta(hapd, attr);
  492. if (sta == NULL)
  493. return RADIUS_DAS_SESSION_NOT_FOUND;
  494. hostapd_drv_sta_deauth(hapd, sta->addr,
  495. WLAN_REASON_PREV_AUTH_NOT_VALID);
  496. ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
  497. return RADIUS_DAS_SUCCESS;
  498. }
  499. #endif /* CONFIG_NO_RADIUS */
  500. /**
  501. * hostapd_setup_bss - Per-BSS setup (initialization)
  502. * @hapd: Pointer to BSS data
  503. * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
  504. * but interface may exist
  505. *
  506. * This function is used to initialize all per-BSS data structures and
  507. * resources. This gets called in a loop for each BSS when an interface is
  508. * initialized. Most of the modules that are initialized here will be
  509. * deinitialized in hostapd_cleanup().
  510. */
  511. static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
  512. {
  513. struct hostapd_bss_config *conf = hapd->conf;
  514. u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
  515. int ssid_len, set_ssid;
  516. char force_ifname[IFNAMSIZ];
  517. u8 if_addr[ETH_ALEN];
  518. if (!first || first == -1) {
  519. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
  520. /* Allocate the next available BSSID. */
  521. do {
  522. inc_byte_array(hapd->own_addr, ETH_ALEN);
  523. } while (mac_in_conf(hapd->iconf, hapd->own_addr));
  524. } else {
  525. /* Allocate the configured BSSID. */
  526. os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
  527. if (hostapd_mac_comp(hapd->own_addr,
  528. hapd->iface->bss[0]->own_addr) ==
  529. 0) {
  530. wpa_printf(MSG_ERROR, "BSS '%s' may not have "
  531. "BSSID set to the MAC address of "
  532. "the radio", hapd->conf->iface);
  533. return -1;
  534. }
  535. }
  536. hapd->interface_added = 1;
  537. if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
  538. hapd->conf->iface, hapd->own_addr, hapd,
  539. &hapd->drv_priv, force_ifname, if_addr,
  540. hapd->conf->bridge[0] ? hapd->conf->bridge :
  541. NULL, first == -1)) {
  542. wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
  543. MACSTR ")", MAC2STR(hapd->own_addr));
  544. return -1;
  545. }
  546. }
  547. if (conf->wmm_enabled < 0)
  548. conf->wmm_enabled = hapd->iconf->ieee80211n;
  549. hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
  550. hostapd_set_privacy(hapd, 0);
  551. hostapd_broadcast_wep_clear(hapd);
  552. if (hostapd_setup_encryption(hapd->conf->iface, hapd))
  553. return -1;
  554. /*
  555. * Fetch the SSID from the system and use it or,
  556. * if one was specified in the config file, verify they
  557. * match.
  558. */
  559. ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
  560. if (ssid_len < 0) {
  561. wpa_printf(MSG_ERROR, "Could not read SSID from system");
  562. return -1;
  563. }
  564. if (conf->ssid.ssid_set) {
  565. /*
  566. * If SSID is specified in the config file and it differs
  567. * from what is being used then force installation of the
  568. * new SSID.
  569. */
  570. set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
  571. os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
  572. } else {
  573. /*
  574. * No SSID in the config file; just use the one we got
  575. * from the system.
  576. */
  577. set_ssid = 0;
  578. conf->ssid.ssid_len = ssid_len;
  579. os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
  580. }
  581. if (!hostapd_drv_none(hapd)) {
  582. wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
  583. " and ssid \"%s\"",
  584. hapd->conf->iface, MAC2STR(hapd->own_addr),
  585. wpa_ssid_txt(hapd->conf->ssid.ssid,
  586. hapd->conf->ssid.ssid_len));
  587. }
  588. if (hostapd_setup_wpa_psk(conf)) {
  589. wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
  590. return -1;
  591. }
  592. /* Set SSID for the kernel driver (to be used in beacon and probe
  593. * response frames) */
  594. if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
  595. conf->ssid.ssid_len)) {
  596. wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
  597. return -1;
  598. }
  599. if (wpa_debug_level == MSG_MSGDUMP)
  600. conf->radius->msg_dumps = 1;
  601. #ifndef CONFIG_NO_RADIUS
  602. hapd->radius = radius_client_init(hapd, conf->radius);
  603. if (hapd->radius == NULL) {
  604. wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
  605. return -1;
  606. }
  607. if (hapd->conf->radius_das_port) {
  608. struct radius_das_conf das_conf;
  609. os_memset(&das_conf, 0, sizeof(das_conf));
  610. das_conf.port = hapd->conf->radius_das_port;
  611. das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
  612. das_conf.shared_secret_len =
  613. hapd->conf->radius_das_shared_secret_len;
  614. das_conf.client_addr = &hapd->conf->radius_das_client_addr;
  615. das_conf.time_window = hapd->conf->radius_das_time_window;
  616. das_conf.require_event_timestamp =
  617. hapd->conf->radius_das_require_event_timestamp;
  618. das_conf.ctx = hapd;
  619. das_conf.disconnect = hostapd_das_disconnect;
  620. hapd->radius_das = radius_das_init(&das_conf);
  621. if (hapd->radius_das == NULL) {
  622. wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
  623. "failed.");
  624. return -1;
  625. }
  626. }
  627. #endif /* CONFIG_NO_RADIUS */
  628. if (hostapd_acl_init(hapd)) {
  629. wpa_printf(MSG_ERROR, "ACL initialization failed.");
  630. return -1;
  631. }
  632. if (hostapd_init_wps(hapd, conf))
  633. return -1;
  634. if (authsrv_init(hapd) < 0)
  635. return -1;
  636. if (ieee802_1x_init(hapd)) {
  637. wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
  638. return -1;
  639. }
  640. if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
  641. return -1;
  642. if (accounting_init(hapd)) {
  643. wpa_printf(MSG_ERROR, "Accounting initialization failed.");
  644. return -1;
  645. }
  646. if (hapd->conf->ieee802_11f &&
  647. (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
  648. wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
  649. "failed.");
  650. return -1;
  651. }
  652. #ifdef CONFIG_INTERWORKING
  653. if (gas_serv_init(hapd)) {
  654. wpa_printf(MSG_ERROR, "GAS server initialization failed");
  655. return -1;
  656. }
  657. if (conf->qos_map_set_len &&
  658. hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
  659. conf->qos_map_set_len)) {
  660. wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
  661. return -1;
  662. }
  663. #endif /* CONFIG_INTERWORKING */
  664. if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
  665. wpa_printf(MSG_ERROR, "VLAN initialization failed.");
  666. return -1;
  667. }
  668. if (!hapd->conf->start_disabled)
  669. ieee802_11_set_beacon(hapd);
  670. if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
  671. return -1;
  672. if (hapd->driver && hapd->driver->set_operstate)
  673. hapd->driver->set_operstate(hapd->drv_priv, 1);
  674. return 0;
  675. }
  676. static void hostapd_tx_queue_params(struct hostapd_iface *iface)
  677. {
  678. struct hostapd_data *hapd = iface->bss[0];
  679. int i;
  680. struct hostapd_tx_queue_params *p;
  681. for (i = 0; i < NUM_TX_QUEUES; i++) {
  682. p = &iface->conf->tx_queue[i];
  683. if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
  684. p->cwmax, p->burst)) {
  685. wpa_printf(MSG_DEBUG, "Failed to set TX queue "
  686. "parameters for queue %d.", i);
  687. /* Continue anyway */
  688. }
  689. }
  690. }
  691. static int hostapd_set_acl_list(struct hostapd_data *hapd,
  692. struct mac_acl_entry *mac_acl,
  693. int n_entries, u8 accept_acl)
  694. {
  695. struct hostapd_acl_params *acl_params;
  696. int i, err;
  697. acl_params = os_zalloc(sizeof(*acl_params) +
  698. (n_entries * sizeof(acl_params->mac_acl[0])));
  699. if (!acl_params)
  700. return -ENOMEM;
  701. for (i = 0; i < n_entries; i++)
  702. os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
  703. ETH_ALEN);
  704. acl_params->acl_policy = accept_acl;
  705. acl_params->num_mac_acl = n_entries;
  706. err = hostapd_drv_set_acl(hapd, acl_params);
  707. os_free(acl_params);
  708. return err;
  709. }
  710. static void hostapd_set_acl(struct hostapd_data *hapd)
  711. {
  712. struct hostapd_config *conf = hapd->iconf;
  713. int err;
  714. u8 accept_acl;
  715. if (hapd->iface->drv_max_acl_mac_addrs == 0)
  716. return;
  717. if (!(conf->bss[0]->num_accept_mac || conf->bss[0]->num_deny_mac))
  718. return;
  719. if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
  720. if (conf->bss[0]->num_accept_mac) {
  721. accept_acl = 1;
  722. err = hostapd_set_acl_list(hapd,
  723. conf->bss[0]->accept_mac,
  724. conf->bss[0]->num_accept_mac,
  725. accept_acl);
  726. if (err) {
  727. wpa_printf(MSG_DEBUG, "Failed to set accept acl");
  728. return;
  729. }
  730. } else {
  731. wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
  732. }
  733. } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
  734. if (conf->bss[0]->num_deny_mac) {
  735. accept_acl = 0;
  736. err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
  737. conf->bss[0]->num_deny_mac,
  738. accept_acl);
  739. if (err) {
  740. wpa_printf(MSG_DEBUG, "Failed to set deny acl");
  741. return;
  742. }
  743. } else {
  744. wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
  745. }
  746. }
  747. }
  748. static int start_ctrl_iface_bss(struct hostapd_data *hapd)
  749. {
  750. if (!hapd->iface->interfaces ||
  751. !hapd->iface->interfaces->ctrl_iface_init)
  752. return 0;
  753. if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
  754. wpa_printf(MSG_ERROR,
  755. "Failed to setup control interface for %s",
  756. hapd->conf->iface);
  757. return -1;
  758. }
  759. return 0;
  760. }
  761. static int start_ctrl_iface(struct hostapd_iface *iface)
  762. {
  763. size_t i;
  764. if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
  765. return 0;
  766. for (i = 0; i < iface->num_bss; i++) {
  767. struct hostapd_data *hapd = iface->bss[i];
  768. if (iface->interfaces->ctrl_iface_init(hapd)) {
  769. wpa_printf(MSG_ERROR,
  770. "Failed to setup control interface for %s",
  771. hapd->conf->iface);
  772. return -1;
  773. }
  774. }
  775. return 0;
  776. }
  777. static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
  778. {
  779. struct hostapd_iface *iface = eloop_ctx;
  780. if (!iface->wait_channel_update) {
  781. wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
  782. return;
  783. }
  784. /*
  785. * It is possible that the existing channel list is acceptable, so try
  786. * to proceed.
  787. */
  788. wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
  789. setup_interface2(iface);
  790. }
  791. void hostapd_channel_list_updated(struct hostapd_iface *iface)
  792. {
  793. if (!iface->wait_channel_update)
  794. return;
  795. wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
  796. eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
  797. setup_interface2(iface);
  798. }
  799. static int setup_interface(struct hostapd_iface *iface)
  800. {
  801. struct hostapd_data *hapd = iface->bss[0];
  802. size_t i;
  803. if (!iface->phy[0]) {
  804. const char *phy = hostapd_drv_get_radio_name(hapd);
  805. if (phy) {
  806. wpa_printf(MSG_DEBUG, "phy: %s", phy);
  807. os_strlcpy(iface->phy, phy, sizeof(iface->phy));
  808. }
  809. }
  810. /*
  811. * Make sure that all BSSes get configured with a pointer to the same
  812. * driver interface.
  813. */
  814. for (i = 1; i < iface->num_bss; i++) {
  815. iface->bss[i]->driver = hapd->driver;
  816. iface->bss[i]->drv_priv = hapd->drv_priv;
  817. }
  818. if (hostapd_validate_bssid_configuration(iface))
  819. return -1;
  820. /*
  821. * Initialize control interfaces early to allow external monitoring of
  822. * channel setup operations that may take considerable amount of time
  823. * especially for DFS cases.
  824. */
  825. if (start_ctrl_iface(iface))
  826. return -1;
  827. if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
  828. char country[4], previous_country[4];
  829. hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
  830. if (hostapd_get_country(hapd, previous_country) < 0)
  831. previous_country[0] = '\0';
  832. os_memcpy(country, hapd->iconf->country, 3);
  833. country[3] = '\0';
  834. if (hostapd_set_country(hapd, country) < 0) {
  835. wpa_printf(MSG_ERROR, "Failed to set country code");
  836. return -1;
  837. }
  838. wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
  839. previous_country, country);
  840. if (os_strncmp(previous_country, country, 2) != 0) {
  841. wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
  842. iface->wait_channel_update = 1;
  843. eloop_register_timeout(1, 0,
  844. channel_list_update_timeout,
  845. iface, NULL);
  846. return 0;
  847. }
  848. }
  849. return setup_interface2(iface);
  850. }
  851. static int setup_interface2(struct hostapd_iface *iface)
  852. {
  853. iface->wait_channel_update = 0;
  854. if (hostapd_get_hw_features(iface)) {
  855. /* Not all drivers support this yet, so continue without hw
  856. * feature data. */
  857. } else {
  858. int ret = hostapd_select_hw_mode(iface);
  859. if (ret < 0) {
  860. wpa_printf(MSG_ERROR, "Could not select hw_mode and "
  861. "channel. (%d)", ret);
  862. return -1;
  863. }
  864. if (ret == 1) {
  865. wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
  866. return 0;
  867. }
  868. ret = hostapd_check_ht_capab(iface);
  869. if (ret < 0)
  870. return -1;
  871. if (ret == 1) {
  872. wpa_printf(MSG_DEBUG, "Interface initialization will "
  873. "be completed in a callback");
  874. return 0;
  875. }
  876. if (iface->conf->ieee80211h)
  877. wpa_printf(MSG_DEBUG, "DFS support is enabled");
  878. }
  879. return hostapd_setup_interface_complete(iface, 0);
  880. }
  881. /**
  882. * hostapd_setup_interface_complete - Complete interface setup
  883. *
  884. * This function is called when previous steps in the interface setup has been
  885. * completed. This can also start operations, e.g., DFS, that will require
  886. * additional processing before interface is ready to be enabled. Such
  887. * operations will call this function from eloop callbacks when finished.
  888. */
  889. int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
  890. {
  891. struct hostapd_data *hapd = iface->bss[0];
  892. size_t j;
  893. u8 *prev_addr;
  894. if (err) {
  895. wpa_printf(MSG_ERROR, "Interface initialization failed");
  896. hostapd_set_state(iface, HAPD_IFACE_DISABLED);
  897. if (iface->interfaces && iface->interfaces->terminate_on_error)
  898. eloop_terminate();
  899. return -1;
  900. }
  901. wpa_printf(MSG_DEBUG, "Completing interface initialization");
  902. if (iface->conf->channel) {
  903. #ifdef NEED_AP_MLME
  904. int res;
  905. #endif /* NEED_AP_MLME */
  906. iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
  907. wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
  908. "Frequency: %d MHz",
  909. hostapd_hw_mode_txt(iface->conf->hw_mode),
  910. iface->conf->channel, iface->freq);
  911. #ifdef NEED_AP_MLME
  912. /* Check DFS */
  913. res = hostapd_handle_dfs(iface);
  914. if (res <= 0)
  915. return res;
  916. #endif /* NEED_AP_MLME */
  917. if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
  918. hapd->iconf->channel,
  919. hapd->iconf->ieee80211n,
  920. hapd->iconf->ieee80211ac,
  921. hapd->iconf->secondary_channel,
  922. hapd->iconf->vht_oper_chwidth,
  923. hapd->iconf->vht_oper_centr_freq_seg0_idx,
  924. hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
  925. wpa_printf(MSG_ERROR, "Could not set channel for "
  926. "kernel driver");
  927. return -1;
  928. }
  929. }
  930. if (iface->current_mode) {
  931. if (hostapd_prepare_rates(iface, iface->current_mode)) {
  932. wpa_printf(MSG_ERROR, "Failed to prepare rates "
  933. "table.");
  934. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
  935. HOSTAPD_LEVEL_WARNING,
  936. "Failed to prepare rates table.");
  937. return -1;
  938. }
  939. }
  940. if (hapd->iconf->rts_threshold > -1 &&
  941. hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
  942. wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
  943. "kernel driver");
  944. return -1;
  945. }
  946. if (hapd->iconf->fragm_threshold > -1 &&
  947. hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
  948. wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
  949. "for kernel driver");
  950. return -1;
  951. }
  952. prev_addr = hapd->own_addr;
  953. for (j = 0; j < iface->num_bss; j++) {
  954. hapd = iface->bss[j];
  955. if (j)
  956. os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
  957. if (hostapd_setup_bss(hapd, j == 0))
  958. return -1;
  959. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
  960. prev_addr = hapd->own_addr;
  961. }
  962. hapd = iface->bss[0];
  963. hostapd_tx_queue_params(iface);
  964. ap_list_init(iface);
  965. hostapd_set_acl(hapd);
  966. if (hostapd_driver_commit(hapd) < 0) {
  967. wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
  968. "configuration", __func__);
  969. return -1;
  970. }
  971. /*
  972. * WPS UPnP module can be initialized only when the "upnp_iface" is up.
  973. * If "interface" and "upnp_iface" are the same (e.g., non-bridge
  974. * mode), the interface is up only after driver_commit, so initialize
  975. * WPS after driver_commit.
  976. */
  977. for (j = 0; j < iface->num_bss; j++) {
  978. if (hostapd_init_wps_complete(iface->bss[j]))
  979. return -1;
  980. }
  981. hostapd_set_state(iface, HAPD_IFACE_ENABLED);
  982. wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
  983. if (hapd->setup_complete_cb)
  984. hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
  985. wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
  986. iface->bss[0]->conf->iface);
  987. if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
  988. iface->interfaces->terminate_on_error--;
  989. return 0;
  990. }
  991. /**
  992. * hostapd_setup_interface - Setup of an interface
  993. * @iface: Pointer to interface data.
  994. * Returns: 0 on success, -1 on failure
  995. *
  996. * Initializes the driver interface, validates the configuration,
  997. * and sets driver parameters based on the configuration.
  998. * Flushes old stations, sets the channel, encryption,
  999. * beacons, and WDS links based on the configuration.
  1000. *
  1001. * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
  1002. * or DFS operations, this function returns 0 before such operations have been
  1003. * completed. The pending operations are registered into eloop and will be
  1004. * completed from eloop callbacks. Those callbacks end up calling
  1005. * hostapd_setup_interface_complete() once setup has been completed.
  1006. */
  1007. int hostapd_setup_interface(struct hostapd_iface *iface)
  1008. {
  1009. int ret;
  1010. ret = setup_interface(iface);
  1011. if (ret) {
  1012. wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
  1013. iface->bss[0]->conf->iface);
  1014. return -1;
  1015. }
  1016. return 0;
  1017. }
  1018. /**
  1019. * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
  1020. * @hapd_iface: Pointer to interface data
  1021. * @conf: Pointer to per-interface configuration
  1022. * @bss: Pointer to per-BSS configuration for this BSS
  1023. * Returns: Pointer to allocated BSS data
  1024. *
  1025. * This function is used to allocate per-BSS data structure. This data will be
  1026. * freed after hostapd_cleanup() is called for it during interface
  1027. * deinitialization.
  1028. */
  1029. struct hostapd_data *
  1030. hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
  1031. struct hostapd_config *conf,
  1032. struct hostapd_bss_config *bss)
  1033. {
  1034. struct hostapd_data *hapd;
  1035. hapd = os_zalloc(sizeof(*hapd));
  1036. if (hapd == NULL)
  1037. return NULL;
  1038. hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
  1039. hapd->iconf = conf;
  1040. hapd->conf = bss;
  1041. hapd->iface = hapd_iface;
  1042. hapd->driver = hapd->iconf->driver;
  1043. hapd->ctrl_sock = -1;
  1044. return hapd;
  1045. }
  1046. void hostapd_interface_deinit(struct hostapd_iface *iface)
  1047. {
  1048. int j;
  1049. if (iface == NULL)
  1050. return;
  1051. eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
  1052. iface->wait_channel_update = 0;
  1053. hostapd_cleanup_iface_pre(iface);
  1054. for (j = iface->num_bss - 1; j >= 0; j--) {
  1055. struct hostapd_data *hapd = iface->bss[j];
  1056. hostapd_free_stas(hapd);
  1057. hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
  1058. hostapd_clear_wep(hapd);
  1059. hostapd_cleanup(hapd);
  1060. }
  1061. }
  1062. void hostapd_interface_free(struct hostapd_iface *iface)
  1063. {
  1064. size_t j;
  1065. for (j = 0; j < iface->num_bss; j++)
  1066. os_free(iface->bss[j]);
  1067. hostapd_cleanup_iface(iface);
  1068. }
  1069. /**
  1070. * hostapd_init - Allocate and initialize per-interface data
  1071. * @config_file: Path to the configuration file
  1072. * Returns: Pointer to the allocated interface data or %NULL on failure
  1073. *
  1074. * This function is used to allocate main data structures for per-interface
  1075. * data. The allocated data buffer will be freed by calling
  1076. * hostapd_cleanup_iface().
  1077. */
  1078. struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
  1079. const char *config_file)
  1080. {
  1081. struct hostapd_iface *hapd_iface = NULL;
  1082. struct hostapd_config *conf = NULL;
  1083. struct hostapd_data *hapd;
  1084. size_t i;
  1085. hapd_iface = os_zalloc(sizeof(*hapd_iface));
  1086. if (hapd_iface == NULL)
  1087. goto fail;
  1088. hapd_iface->config_fname = os_strdup(config_file);
  1089. if (hapd_iface->config_fname == NULL)
  1090. goto fail;
  1091. conf = interfaces->config_read_cb(hapd_iface->config_fname);
  1092. if (conf == NULL)
  1093. goto fail;
  1094. hapd_iface->conf = conf;
  1095. hapd_iface->num_bss = conf->num_bss;
  1096. hapd_iface->bss = os_calloc(conf->num_bss,
  1097. sizeof(struct hostapd_data *));
  1098. if (hapd_iface->bss == NULL)
  1099. goto fail;
  1100. for (i = 0; i < conf->num_bss; i++) {
  1101. hapd = hapd_iface->bss[i] =
  1102. hostapd_alloc_bss_data(hapd_iface, conf,
  1103. conf->bss[i]);
  1104. if (hapd == NULL)
  1105. goto fail;
  1106. hapd->msg_ctx = hapd;
  1107. }
  1108. return hapd_iface;
  1109. fail:
  1110. wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
  1111. config_file);
  1112. if (conf)
  1113. hostapd_config_free(conf);
  1114. if (hapd_iface) {
  1115. os_free(hapd_iface->config_fname);
  1116. os_free(hapd_iface->bss);
  1117. os_free(hapd_iface);
  1118. }
  1119. return NULL;
  1120. }
  1121. static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
  1122. {
  1123. size_t i, j;
  1124. for (i = 0; i < interfaces->count; i++) {
  1125. struct hostapd_iface *iface = interfaces->iface[i];
  1126. for (j = 0; j < iface->num_bss; j++) {
  1127. struct hostapd_data *hapd = iface->bss[j];
  1128. if (os_strcmp(ifname, hapd->conf->iface) == 0)
  1129. return 1;
  1130. }
  1131. }
  1132. return 0;
  1133. }
  1134. /**
  1135. * hostapd_interface_init_bss - Read configuration file and init BSS data
  1136. *
  1137. * This function is used to parse configuration file for a BSS. This BSS is
  1138. * added to an existing interface sharing the same radio (if any) or a new
  1139. * interface is created if this is the first interface on a radio. This
  1140. * allocate memory for the BSS. No actual driver operations are started.
  1141. *
  1142. * This is similar to hostapd_interface_init(), but for a case where the
  1143. * configuration is used to add a single BSS instead of all BSSes for a radio.
  1144. */
  1145. struct hostapd_iface *
  1146. hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
  1147. const char *config_fname, int debug)
  1148. {
  1149. struct hostapd_iface *new_iface = NULL, *iface = NULL;
  1150. struct hostapd_data *hapd;
  1151. int k;
  1152. size_t i, bss_idx;
  1153. if (!phy || !*phy)
  1154. return NULL;
  1155. for (i = 0; i < interfaces->count; i++) {
  1156. if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
  1157. iface = interfaces->iface[i];
  1158. break;
  1159. }
  1160. }
  1161. wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
  1162. config_fname, phy, iface ? "" : " --> new PHY");
  1163. if (iface) {
  1164. struct hostapd_config *conf;
  1165. struct hostapd_bss_config **tmp_conf;
  1166. struct hostapd_data **tmp_bss;
  1167. struct hostapd_bss_config *bss;
  1168. const char *ifname;
  1169. /* Add new BSS to existing iface */
  1170. conf = interfaces->config_read_cb(config_fname);
  1171. if (conf == NULL)
  1172. return NULL;
  1173. if (conf->num_bss > 1) {
  1174. wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
  1175. hostapd_config_free(conf);
  1176. return NULL;
  1177. }
  1178. ifname = conf->bss[0]->iface;
  1179. if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
  1180. wpa_printf(MSG_ERROR,
  1181. "Interface name %s already in use", ifname);
  1182. hostapd_config_free(conf);
  1183. return NULL;
  1184. }
  1185. tmp_conf = os_realloc_array(
  1186. iface->conf->bss, iface->conf->num_bss + 1,
  1187. sizeof(struct hostapd_bss_config *));
  1188. tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
  1189. sizeof(struct hostapd_data *));
  1190. if (tmp_bss)
  1191. iface->bss = tmp_bss;
  1192. if (tmp_conf) {
  1193. iface->conf->bss = tmp_conf;
  1194. iface->conf->last_bss = tmp_conf[0];
  1195. }
  1196. if (tmp_bss == NULL || tmp_conf == NULL) {
  1197. hostapd_config_free(conf);
  1198. return NULL;
  1199. }
  1200. bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
  1201. iface->conf->num_bss++;
  1202. hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
  1203. if (hapd == NULL) {
  1204. iface->conf->num_bss--;
  1205. hostapd_config_free(conf);
  1206. return NULL;
  1207. }
  1208. iface->conf->last_bss = bss;
  1209. iface->bss[iface->num_bss] = hapd;
  1210. hapd->msg_ctx = hapd;
  1211. bss_idx = iface->num_bss++;
  1212. conf->num_bss--;
  1213. conf->bss[0] = NULL;
  1214. hostapd_config_free(conf);
  1215. } else {
  1216. /* Add a new iface with the first BSS */
  1217. new_iface = iface = hostapd_init(interfaces, config_fname);
  1218. if (!iface)
  1219. return NULL;
  1220. os_strlcpy(iface->phy, phy, sizeof(iface->phy));
  1221. iface->interfaces = interfaces;
  1222. bss_idx = 0;
  1223. }
  1224. for (k = 0; k < debug; k++) {
  1225. if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
  1226. iface->bss[bss_idx]->conf->logger_stdout_level--;
  1227. }
  1228. if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
  1229. !hostapd_drv_none(iface->bss[bss_idx])) {
  1230. wpa_printf(MSG_ERROR, "Interface name not specified in %s",
  1231. config_fname);
  1232. if (new_iface)
  1233. hostapd_interface_deinit_free(new_iface);
  1234. return NULL;
  1235. }
  1236. return iface;
  1237. }
  1238. void hostapd_interface_deinit_free(struct hostapd_iface *iface)
  1239. {
  1240. const struct wpa_driver_ops *driver;
  1241. void *drv_priv;
  1242. if (iface == NULL)
  1243. return;
  1244. driver = iface->bss[0]->driver;
  1245. drv_priv = iface->bss[0]->drv_priv;
  1246. hostapd_interface_deinit(iface);
  1247. if (driver && driver->hapd_deinit && drv_priv)
  1248. driver->hapd_deinit(drv_priv);
  1249. hostapd_interface_free(iface);
  1250. }
  1251. int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
  1252. {
  1253. if (hapd_iface->bss[0]->drv_priv != NULL) {
  1254. wpa_printf(MSG_ERROR, "Interface %s already enabled",
  1255. hapd_iface->conf->bss[0]->iface);
  1256. return -1;
  1257. }
  1258. wpa_printf(MSG_DEBUG, "Enable interface %s",
  1259. hapd_iface->conf->bss[0]->iface);
  1260. if (hapd_iface->interfaces == NULL ||
  1261. hapd_iface->interfaces->driver_init == NULL ||
  1262. hapd_iface->interfaces->driver_init(hapd_iface))
  1263. return -1;
  1264. if (hostapd_setup_interface(hapd_iface)) {
  1265. const struct wpa_driver_ops *driver;
  1266. void *drv_priv;
  1267. driver = hapd_iface->bss[0]->driver;
  1268. drv_priv = hapd_iface->bss[0]->drv_priv;
  1269. if (driver && driver->hapd_deinit && drv_priv) {
  1270. driver->hapd_deinit(drv_priv);
  1271. hapd_iface->bss[0]->drv_priv = NULL;
  1272. }
  1273. return -1;
  1274. }
  1275. return 0;
  1276. }
  1277. int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
  1278. {
  1279. size_t j;
  1280. wpa_printf(MSG_DEBUG, "Reload interface %s",
  1281. hapd_iface->conf->bss[0]->iface);
  1282. for (j = 0; j < hapd_iface->num_bss; j++)
  1283. hostapd_set_security_params(hapd_iface->conf->bss[j]);
  1284. if (hostapd_config_check(hapd_iface->conf) < 0) {
  1285. wpa_printf(MSG_ERROR, "Updated configuration is invalid");
  1286. return -1;
  1287. }
  1288. hostapd_clear_old(hapd_iface);
  1289. for (j = 0; j < hapd_iface->num_bss; j++)
  1290. hostapd_reload_bss(hapd_iface->bss[j]);
  1291. return 0;
  1292. }
  1293. int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
  1294. {
  1295. size_t j;
  1296. const struct wpa_driver_ops *driver;
  1297. void *drv_priv;
  1298. if (hapd_iface == NULL)
  1299. return -1;
  1300. wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
  1301. driver = hapd_iface->bss[0]->driver;
  1302. drv_priv = hapd_iface->bss[0]->drv_priv;
  1303. /* whatever hostapd_interface_deinit does */
  1304. for (j = 0; j < hapd_iface->num_bss; j++) {
  1305. struct hostapd_data *hapd = hapd_iface->bss[j];
  1306. hostapd_free_stas(hapd);
  1307. hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
  1308. hostapd_clear_wep(hapd);
  1309. hostapd_free_hapd_data(hapd);
  1310. }
  1311. if (driver && driver->hapd_deinit && drv_priv) {
  1312. driver->hapd_deinit(drv_priv);
  1313. hapd_iface->bss[0]->drv_priv = NULL;
  1314. }
  1315. /* From hostapd_cleanup_iface: These were initialized in
  1316. * hostapd_setup_interface and hostapd_setup_interface_complete
  1317. */
  1318. hostapd_cleanup_iface_partial(hapd_iface);
  1319. wpa_printf(MSG_DEBUG, "Interface %s disabled",
  1320. hapd_iface->bss[0]->conf->iface);
  1321. hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
  1322. return 0;
  1323. }
  1324. static struct hostapd_iface *
  1325. hostapd_iface_alloc(struct hapd_interfaces *interfaces)
  1326. {
  1327. struct hostapd_iface **iface, *hapd_iface;
  1328. iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
  1329. sizeof(struct hostapd_iface *));
  1330. if (iface == NULL)
  1331. return NULL;
  1332. interfaces->iface = iface;
  1333. hapd_iface = interfaces->iface[interfaces->count] =
  1334. os_zalloc(sizeof(*hapd_iface));
  1335. if (hapd_iface == NULL) {
  1336. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
  1337. "the interface", __func__);
  1338. return NULL;
  1339. }
  1340. interfaces->count++;
  1341. hapd_iface->interfaces = interfaces;
  1342. return hapd_iface;
  1343. }
  1344. static struct hostapd_config *
  1345. hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
  1346. const char *ctrl_iface)
  1347. {
  1348. struct hostapd_bss_config *bss;
  1349. struct hostapd_config *conf;
  1350. /* Allocates memory for bss and conf */
  1351. conf = hostapd_config_defaults();
  1352. if (conf == NULL) {
  1353. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
  1354. "configuration", __func__);
  1355. return NULL;
  1356. }
  1357. conf->driver = wpa_drivers[0];
  1358. if (conf->driver == NULL) {
  1359. wpa_printf(MSG_ERROR, "No driver wrappers registered!");
  1360. hostapd_config_free(conf);
  1361. return NULL;
  1362. }
  1363. bss = conf->last_bss = conf->bss[0];
  1364. os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
  1365. bss->ctrl_interface = os_strdup(ctrl_iface);
  1366. if (bss->ctrl_interface == NULL) {
  1367. hostapd_config_free(conf);
  1368. return NULL;
  1369. }
  1370. /* Reading configuration file skipped, will be done in SET!
  1371. * From reading the configuration till the end has to be done in
  1372. * SET
  1373. */
  1374. return conf;
  1375. }
  1376. static struct hostapd_iface * hostapd_data_alloc(
  1377. struct hapd_interfaces *interfaces, struct hostapd_config *conf)
  1378. {
  1379. size_t i;
  1380. struct hostapd_iface *hapd_iface =
  1381. interfaces->iface[interfaces->count - 1];
  1382. struct hostapd_data *hapd;
  1383. hapd_iface->conf = conf;
  1384. hapd_iface->num_bss = conf->num_bss;
  1385. hapd_iface->bss = os_zalloc(conf->num_bss *
  1386. sizeof(struct hostapd_data *));
  1387. if (hapd_iface->bss == NULL)
  1388. return NULL;
  1389. for (i = 0; i < conf->num_bss; i++) {
  1390. hapd = hapd_iface->bss[i] =
  1391. hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
  1392. if (hapd == NULL)
  1393. return NULL;
  1394. hapd->msg_ctx = hapd;
  1395. }
  1396. hapd_iface->interfaces = interfaces;
  1397. return hapd_iface;
  1398. }
  1399. int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
  1400. {
  1401. struct hostapd_config *conf = NULL;
  1402. struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
  1403. struct hostapd_data *hapd;
  1404. char *ptr;
  1405. size_t i, j;
  1406. const char *conf_file = NULL, *phy_name = NULL;
  1407. if (os_strncmp(buf, "bss_config=", 11) == 0) {
  1408. char *pos;
  1409. phy_name = buf + 11;
  1410. pos = os_strchr(phy_name, ':');
  1411. if (!pos)
  1412. return -1;
  1413. *pos++ = '\0';
  1414. conf_file = pos;
  1415. if (!os_strlen(conf_file))
  1416. return -1;
  1417. hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
  1418. conf_file, 0);
  1419. if (!hapd_iface)
  1420. return -1;
  1421. for (j = 0; j < interfaces->count; j++) {
  1422. if (interfaces->iface[j] == hapd_iface)
  1423. break;
  1424. }
  1425. if (j == interfaces->count) {
  1426. struct hostapd_iface **tmp;
  1427. tmp = os_realloc_array(interfaces->iface,
  1428. interfaces->count + 1,
  1429. sizeof(struct hostapd_iface *));
  1430. if (!tmp) {
  1431. hostapd_interface_deinit_free(hapd_iface);
  1432. return -1;
  1433. }
  1434. interfaces->iface = tmp;
  1435. interfaces->iface[interfaces->count++] = hapd_iface;
  1436. new_iface = hapd_iface;
  1437. }
  1438. if (new_iface) {
  1439. if (interfaces->driver_init(hapd_iface) ||
  1440. hostapd_setup_interface(hapd_iface)) {
  1441. interfaces->count--;
  1442. goto fail;
  1443. }
  1444. } else {
  1445. /* Assign new BSS with bss[0]'s driver info */
  1446. hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
  1447. hapd->driver = hapd_iface->bss[0]->driver;
  1448. hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
  1449. os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
  1450. ETH_ALEN);
  1451. if (start_ctrl_iface_bss(hapd) < 0 ||
  1452. hostapd_setup_bss(hapd, -1)) {
  1453. hapd_iface->conf->num_bss--;
  1454. hapd_iface->num_bss--;
  1455. os_free(hapd);
  1456. return -1;
  1457. }
  1458. }
  1459. return 0;
  1460. }
  1461. ptr = os_strchr(buf, ' ');
  1462. if (ptr == NULL)
  1463. return -1;
  1464. *ptr++ = '\0';
  1465. if (os_strncmp(ptr, "config=", 7) == 0)
  1466. conf_file = ptr + 7;
  1467. for (i = 0; i < interfaces->count; i++) {
  1468. if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
  1469. buf)) {
  1470. wpa_printf(MSG_INFO, "Cannot add interface - it "
  1471. "already exists");
  1472. return -1;
  1473. }
  1474. }
  1475. hapd_iface = hostapd_iface_alloc(interfaces);
  1476. if (hapd_iface == NULL) {
  1477. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
  1478. "for interface", __func__);
  1479. goto fail;
  1480. }
  1481. if (conf_file && interfaces->config_read_cb) {
  1482. conf = interfaces->config_read_cb(conf_file);
  1483. if (conf && conf->bss)
  1484. os_strlcpy(conf->bss[0]->iface, buf,
  1485. sizeof(conf->bss[0]->iface));
  1486. } else
  1487. conf = hostapd_config_alloc(interfaces, buf, ptr);
  1488. if (conf == NULL || conf->bss == NULL) {
  1489. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
  1490. "for configuration", __func__);
  1491. goto fail;
  1492. }
  1493. hapd_iface = hostapd_data_alloc(interfaces, conf);
  1494. if (hapd_iface == NULL) {
  1495. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
  1496. "for hostapd", __func__);
  1497. goto fail;
  1498. }
  1499. if (start_ctrl_iface(hapd_iface) < 0)
  1500. goto fail;
  1501. wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0]->iface);
  1502. return 0;
  1503. fail:
  1504. if (conf)
  1505. hostapd_config_free(conf);
  1506. if (hapd_iface) {
  1507. if (hapd_iface->bss) {
  1508. for (i = 0; i < hapd_iface->num_bss; i++) {
  1509. hapd = hapd_iface->bss[i];
  1510. if (hapd && hapd_iface->interfaces &&
  1511. hapd_iface->interfaces->ctrl_iface_deinit)
  1512. hapd_iface->interfaces->
  1513. ctrl_iface_deinit(hapd);
  1514. os_free(hapd_iface->bss[i]);
  1515. }
  1516. os_free(hapd_iface->bss);
  1517. }
  1518. os_free(hapd_iface);
  1519. }
  1520. return -1;
  1521. }
  1522. static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
  1523. {
  1524. size_t i;
  1525. wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
  1526. /* Remove hostapd_data only if it has already been initialized */
  1527. if (idx < iface->num_bss) {
  1528. struct hostapd_data *hapd = iface->bss[idx];
  1529. hostapd_free_stas(hapd);
  1530. hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
  1531. hostapd_clear_wep(hapd);
  1532. hostapd_cleanup(hapd);
  1533. hostapd_config_free_bss(hapd->conf);
  1534. os_free(hapd);
  1535. iface->num_bss--;
  1536. for (i = idx; i < iface->num_bss; i++)
  1537. iface->bss[i] = iface->bss[i + 1];
  1538. } else {
  1539. hostapd_config_free_bss(iface->conf->bss[idx]);
  1540. iface->conf->bss[idx] = NULL;
  1541. }
  1542. iface->conf->num_bss--;
  1543. for (i = idx; i < iface->conf->num_bss; i++)
  1544. iface->conf->bss[i] = iface->conf->bss[i + 1];
  1545. return 0;
  1546. }
  1547. int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
  1548. {
  1549. struct hostapd_iface *hapd_iface;
  1550. size_t i, j, k = 0;
  1551. for (i = 0; i < interfaces->count; i++) {
  1552. hapd_iface = interfaces->iface[i];
  1553. if (hapd_iface == NULL)
  1554. return -1;
  1555. if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
  1556. wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
  1557. hostapd_interface_deinit_free(hapd_iface);
  1558. k = i;
  1559. while (k < (interfaces->count - 1)) {
  1560. interfaces->iface[k] =
  1561. interfaces->iface[k + 1];
  1562. k++;
  1563. }
  1564. interfaces->count--;
  1565. return 0;
  1566. }
  1567. for (j = 0; j < hapd_iface->conf->num_bss; j++) {
  1568. if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf))
  1569. return hostapd_remove_bss(hapd_iface, j);
  1570. }
  1571. }
  1572. return -1;
  1573. }
  1574. /**
  1575. * hostapd_new_assoc_sta - Notify that a new station associated with the AP
  1576. * @hapd: Pointer to BSS data
  1577. * @sta: Pointer to the associated STA data
  1578. * @reassoc: 1 to indicate this was a re-association; 0 = first association
  1579. *
  1580. * This function will be called whenever a station associates with the AP. It
  1581. * can be called from ieee802_11.c for drivers that export MLME to hostapd and
  1582. * from drv_callbacks.c based on driver events for drivers that take care of
  1583. * management frames (IEEE 802.11 authentication and association) internally.
  1584. */
  1585. void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
  1586. int reassoc)
  1587. {
  1588. if (hapd->tkip_countermeasures) {
  1589. hostapd_drv_sta_deauth(hapd, sta->addr,
  1590. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1591. return;
  1592. }
  1593. hostapd_prune_associations(hapd, sta->addr);
  1594. /* IEEE 802.11F (IAPP) */
  1595. if (hapd->conf->ieee802_11f)
  1596. iapp_new_station(hapd->iapp, sta);
  1597. #ifdef CONFIG_P2P
  1598. if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
  1599. sta->no_p2p_set = 1;
  1600. hapd->num_sta_no_p2p++;
  1601. if (hapd->num_sta_no_p2p == 1)
  1602. hostapd_p2p_non_p2p_sta_connected(hapd);
  1603. }
  1604. #endif /* CONFIG_P2P */
  1605. /* Start accounting here, if IEEE 802.1X and WPA are not used.
  1606. * IEEE 802.1X/WPA code will start accounting after the station has
  1607. * been authorized. */
  1608. if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
  1609. os_get_time(&sta->connected_time);
  1610. accounting_sta_start(hapd, sta);
  1611. }
  1612. /* Start IEEE 802.1X authentication process for new stations */
  1613. ieee802_1x_new_station(hapd, sta);
  1614. if (reassoc) {
  1615. if (sta->auth_alg != WLAN_AUTH_FT &&
  1616. !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
  1617. wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
  1618. } else
  1619. wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
  1620. wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
  1621. "for " MACSTR " (%d seconds - ap_max_inactivity)",
  1622. __func__, MAC2STR(sta->addr),
  1623. hapd->conf->ap_max_inactivity);
  1624. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  1625. eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
  1626. ap_handle_timer, hapd, sta);
  1627. }
  1628. const char * hostapd_state_text(enum hostapd_iface_state s)
  1629. {
  1630. switch (s) {
  1631. case HAPD_IFACE_UNINITIALIZED:
  1632. return "UNINITIALIZED";
  1633. case HAPD_IFACE_DISABLED:
  1634. return "DISABLED";
  1635. case HAPD_IFACE_COUNTRY_UPDATE:
  1636. return "COUNTRY_UPDATE";
  1637. case HAPD_IFACE_ACS:
  1638. return "ACS";
  1639. case HAPD_IFACE_HT_SCAN:
  1640. return "HT_SCAN";
  1641. case HAPD_IFACE_DFS:
  1642. return "DFS";
  1643. case HAPD_IFACE_ENABLED:
  1644. return "ENABLED";
  1645. }
  1646. return "UNKNOWN";
  1647. }
  1648. void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
  1649. {
  1650. wpa_printf(MSG_INFO, "%s: interface state %s->%s",
  1651. iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
  1652. hostapd_state_text(s));
  1653. iface->state = s;
  1654. }