hostapd.c 44 KB

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