hostapd.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121
  1. /*
  2. * hostapd / Initialization and configuration
  3. * Copyright (c) 2002-2014, 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 "common/hw_features_common.h"
  14. #include "radius/radius_client.h"
  15. #include "radius/radius_das.h"
  16. #include "eap_server/tncs.h"
  17. #include "eapol_auth/eapol_auth_sm.h"
  18. #include "eapol_auth/eapol_auth_sm_i.h"
  19. #include "fst/fst.h"
  20. #include "hostapd.h"
  21. #include "authsrv.h"
  22. #include "sta_info.h"
  23. #include "accounting.h"
  24. #include "ap_list.h"
  25. #include "beacon.h"
  26. #include "iapp.h"
  27. #include "ieee802_1x.h"
  28. #include "ieee802_11_auth.h"
  29. #include "vlan_init.h"
  30. #include "wpa_auth.h"
  31. #include "wps_hostapd.h"
  32. #include "hw_features.h"
  33. #include "wpa_auth_glue.h"
  34. #include "ap_drv_ops.h"
  35. #include "ap_config.h"
  36. #include "p2p_hostapd.h"
  37. #include "gas_serv.h"
  38. #include "dfs.h"
  39. #include "ieee802_11.h"
  40. #include "bss_load.h"
  41. #include "x_snoop.h"
  42. #include "dhcp_snoop.h"
  43. #include "ndisc_snoop.h"
  44. static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
  45. static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
  46. static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
  47. static int setup_interface2(struct hostapd_iface *iface);
  48. static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
  49. int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
  50. int (*cb)(struct hostapd_iface *iface,
  51. void *ctx), void *ctx)
  52. {
  53. size_t i;
  54. int ret;
  55. for (i = 0; i < interfaces->count; i++) {
  56. ret = cb(interfaces->iface[i], ctx);
  57. if (ret)
  58. return ret;
  59. }
  60. return 0;
  61. }
  62. static void hostapd_reload_bss(struct hostapd_data *hapd)
  63. {
  64. struct hostapd_ssid *ssid;
  65. #ifndef CONFIG_NO_RADIUS
  66. radius_client_reconfig(hapd->radius, hapd->conf->radius);
  67. #endif /* CONFIG_NO_RADIUS */
  68. ssid = &hapd->conf->ssid;
  69. if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
  70. ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
  71. /*
  72. * Force PSK to be derived again since SSID or passphrase may
  73. * have changed.
  74. */
  75. hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
  76. }
  77. if (hostapd_setup_wpa_psk(hapd->conf)) {
  78. wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
  79. "after reloading configuration");
  80. }
  81. if (hapd->conf->ieee802_1x || hapd->conf->wpa)
  82. hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
  83. else
  84. hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
  85. if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
  86. hostapd_setup_wpa(hapd);
  87. if (hapd->wpa_auth)
  88. wpa_init_keys(hapd->wpa_auth);
  89. } else if (hapd->conf->wpa) {
  90. const u8 *wpa_ie;
  91. size_t wpa_ie_len;
  92. hostapd_reconfig_wpa(hapd);
  93. wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
  94. if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
  95. wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
  96. "the kernel driver.");
  97. } else if (hapd->wpa_auth) {
  98. wpa_deinit(hapd->wpa_auth);
  99. hapd->wpa_auth = NULL;
  100. hostapd_set_privacy(hapd, 0);
  101. hostapd_setup_encryption(hapd->conf->iface, hapd);
  102. hostapd_set_generic_elem(hapd, (u8 *) "", 0);
  103. }
  104. ieee802_11_set_beacon(hapd);
  105. hostapd_update_wps(hapd);
  106. if (hapd->conf->ssid.ssid_set &&
  107. hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
  108. hapd->conf->ssid.ssid_len)) {
  109. wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
  110. /* try to continue */
  111. }
  112. wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
  113. }
  114. static void hostapd_clear_old(struct hostapd_iface *iface)
  115. {
  116. size_t j;
  117. /*
  118. * Deauthenticate all stations since the new configuration may not
  119. * allow them to use the BSS anymore.
  120. */
  121. for (j = 0; j < iface->num_bss; j++) {
  122. hostapd_flush_old_stations(iface->bss[j],
  123. WLAN_REASON_PREV_AUTH_NOT_VALID);
  124. hostapd_broadcast_wep_clear(iface->bss[j]);
  125. #ifndef CONFIG_NO_RADIUS
  126. /* TODO: update dynamic data based on changed configuration
  127. * items (e.g., open/close sockets, etc.) */
  128. radius_client_flush(iface->bss[j]->radius, 0);
  129. #endif /* CONFIG_NO_RADIUS */
  130. }
  131. }
  132. int hostapd_reload_config(struct hostapd_iface *iface)
  133. {
  134. struct hostapd_data *hapd = iface->bss[0];
  135. struct hostapd_config *newconf, *oldconf;
  136. size_t j;
  137. if (iface->config_fname == NULL) {
  138. /* Only in-memory config in use - assume it has been updated */
  139. hostapd_clear_old(iface);
  140. for (j = 0; j < iface->num_bss; j++)
  141. hostapd_reload_bss(iface->bss[j]);
  142. return 0;
  143. }
  144. if (iface->interfaces == NULL ||
  145. iface->interfaces->config_read_cb == NULL)
  146. return -1;
  147. newconf = iface->interfaces->config_read_cb(iface->config_fname);
  148. if (newconf == NULL)
  149. return -1;
  150. hostapd_clear_old(iface);
  151. oldconf = hapd->iconf;
  152. iface->conf = newconf;
  153. for (j = 0; j < iface->num_bss; j++) {
  154. hapd = iface->bss[j];
  155. hapd->iconf = newconf;
  156. hapd->iconf->channel = oldconf->channel;
  157. hapd->iconf->acs = oldconf->acs;
  158. hapd->iconf->secondary_channel = oldconf->secondary_channel;
  159. hapd->iconf->ieee80211n = oldconf->ieee80211n;
  160. hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
  161. hapd->iconf->ht_capab = oldconf->ht_capab;
  162. hapd->iconf->vht_capab = oldconf->vht_capab;
  163. hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
  164. hapd->iconf->vht_oper_centr_freq_seg0_idx =
  165. oldconf->vht_oper_centr_freq_seg0_idx;
  166. hapd->iconf->vht_oper_centr_freq_seg1_idx =
  167. oldconf->vht_oper_centr_freq_seg1_idx;
  168. hapd->conf = newconf->bss[j];
  169. hostapd_reload_bss(hapd);
  170. }
  171. hostapd_config_free(oldconf);
  172. return 0;
  173. }
  174. static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
  175. char *ifname)
  176. {
  177. int i;
  178. for (i = 0; i < NUM_WEP_KEYS; i++) {
  179. if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
  180. 0, NULL, 0, NULL, 0)) {
  181. wpa_printf(MSG_DEBUG, "Failed to clear default "
  182. "encryption keys (ifname=%s keyidx=%d)",
  183. ifname, i);
  184. }
  185. }
  186. #ifdef CONFIG_IEEE80211W
  187. if (hapd->conf->ieee80211w) {
  188. for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
  189. if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
  190. NULL, i, 0, NULL,
  191. 0, NULL, 0)) {
  192. wpa_printf(MSG_DEBUG, "Failed to clear "
  193. "default mgmt encryption keys "
  194. "(ifname=%s keyidx=%d)", ifname, i);
  195. }
  196. }
  197. }
  198. #endif /* CONFIG_IEEE80211W */
  199. }
  200. static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
  201. {
  202. hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
  203. return 0;
  204. }
  205. static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
  206. {
  207. int errors = 0, idx;
  208. struct hostapd_ssid *ssid = &hapd->conf->ssid;
  209. idx = ssid->wep.idx;
  210. if (ssid->wep.default_len &&
  211. hostapd_drv_set_key(hapd->conf->iface,
  212. hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
  213. 1, NULL, 0, ssid->wep.key[idx],
  214. ssid->wep.len[idx])) {
  215. wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
  216. errors++;
  217. }
  218. return errors;
  219. }
  220. static void hostapd_free_hapd_data(struct hostapd_data *hapd)
  221. {
  222. os_free(hapd->probereq_cb);
  223. hapd->probereq_cb = NULL;
  224. hapd->num_probereq_cb = 0;
  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. if (!hapd->started) {
  232. wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
  233. __func__, hapd->conf->iface);
  234. return;
  235. }
  236. hapd->started = 0;
  237. wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
  238. iapp_deinit(hapd->iapp);
  239. hapd->iapp = NULL;
  240. accounting_deinit(hapd);
  241. hostapd_deinit_wpa(hapd);
  242. vlan_deinit(hapd);
  243. hostapd_acl_deinit(hapd);
  244. #ifndef CONFIG_NO_RADIUS
  245. radius_client_deinit(hapd->radius);
  246. hapd->radius = NULL;
  247. radius_das_deinit(hapd->radius_das);
  248. hapd->radius_das = NULL;
  249. #endif /* CONFIG_NO_RADIUS */
  250. hostapd_deinit_wps(hapd);
  251. authsrv_deinit(hapd);
  252. if (hapd->interface_added) {
  253. hapd->interface_added = 0;
  254. if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
  255. wpa_printf(MSG_WARNING,
  256. "Failed to remove BSS interface %s",
  257. hapd->conf->iface);
  258. hapd->interface_added = 1;
  259. } else {
  260. /*
  261. * Since this was a dynamically added interface, the
  262. * driver wrapper may have removed its internal instance
  263. * and hapd->drv_priv is not valid anymore.
  264. */
  265. hapd->drv_priv = NULL;
  266. }
  267. }
  268. wpabuf_free(hapd->time_adv);
  269. #ifdef CONFIG_INTERWORKING
  270. gas_serv_deinit(hapd);
  271. #endif /* CONFIG_INTERWORKING */
  272. bss_load_update_deinit(hapd);
  273. ndisc_snoop_deinit(hapd);
  274. dhcp_snoop_deinit(hapd);
  275. x_snoop_deinit(hapd);
  276. #ifdef CONFIG_SQLITE
  277. bin_clear_free(hapd->tmp_eap_user.identity,
  278. hapd->tmp_eap_user.identity_len);
  279. bin_clear_free(hapd->tmp_eap_user.password,
  280. hapd->tmp_eap_user.password_len);
  281. #endif /* CONFIG_SQLITE */
  282. #ifdef CONFIG_MESH
  283. wpabuf_free(hapd->mesh_pending_auth);
  284. hapd->mesh_pending_auth = NULL;
  285. #endif /* CONFIG_MESH */
  286. }
  287. /**
  288. * hostapd_cleanup - Per-BSS cleanup (deinitialization)
  289. * @hapd: Pointer to BSS data
  290. *
  291. * This function is used to free all per-BSS data structures and resources.
  292. * Most of the modules that are initialized in hostapd_setup_bss() are
  293. * deinitialized here.
  294. */
  295. static void hostapd_cleanup(struct hostapd_data *hapd)
  296. {
  297. wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
  298. hapd->conf->iface);
  299. if (hapd->iface->interfaces &&
  300. hapd->iface->interfaces->ctrl_iface_deinit)
  301. hapd->iface->interfaces->ctrl_iface_deinit(hapd);
  302. hostapd_free_hapd_data(hapd);
  303. }
  304. static void sta_track_deinit(struct hostapd_iface *iface)
  305. {
  306. struct hostapd_sta_info *info;
  307. if (!iface->num_sta_seen)
  308. return;
  309. while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
  310. list))) {
  311. dl_list_del(&info->list);
  312. iface->num_sta_seen--;
  313. os_free(info);
  314. }
  315. }
  316. static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
  317. {
  318. wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
  319. #ifdef CONFIG_IEEE80211N
  320. #ifdef NEED_AP_MLME
  321. hostapd_stop_setup_timers(iface);
  322. #endif /* NEED_AP_MLME */
  323. #endif /* CONFIG_IEEE80211N */
  324. hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
  325. iface->hw_features = NULL;
  326. os_free(iface->current_rates);
  327. iface->current_rates = NULL;
  328. os_free(iface->basic_rates);
  329. iface->basic_rates = NULL;
  330. ap_list_deinit(iface);
  331. sta_track_deinit(iface);
  332. }
  333. /**
  334. * hostapd_cleanup_iface - Complete per-interface cleanup
  335. * @iface: Pointer to interface data
  336. *
  337. * This function is called after per-BSS data structures are deinitialized
  338. * with hostapd_cleanup().
  339. */
  340. static void hostapd_cleanup_iface(struct hostapd_iface *iface)
  341. {
  342. wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
  343. eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
  344. hostapd_cleanup_iface_partial(iface);
  345. hostapd_config_free(iface->conf);
  346. iface->conf = NULL;
  347. os_free(iface->config_fname);
  348. os_free(iface->bss);
  349. wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
  350. os_free(iface);
  351. }
  352. static void hostapd_clear_wep(struct hostapd_data *hapd)
  353. {
  354. if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
  355. hostapd_set_privacy(hapd, 0);
  356. hostapd_broadcast_wep_clear(hapd);
  357. }
  358. }
  359. static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
  360. {
  361. int i;
  362. hostapd_broadcast_wep_set(hapd);
  363. if (hapd->conf->ssid.wep.default_len) {
  364. hostapd_set_privacy(hapd, 1);
  365. return 0;
  366. }
  367. /*
  368. * When IEEE 802.1X is not enabled, the driver may need to know how to
  369. * set authentication algorithms for static WEP.
  370. */
  371. hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
  372. for (i = 0; i < 4; i++) {
  373. if (hapd->conf->ssid.wep.key[i] &&
  374. hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
  375. i == hapd->conf->ssid.wep.idx, NULL, 0,
  376. hapd->conf->ssid.wep.key[i],
  377. hapd->conf->ssid.wep.len[i])) {
  378. wpa_printf(MSG_WARNING, "Could not set WEP "
  379. "encryption.");
  380. return -1;
  381. }
  382. if (hapd->conf->ssid.wep.key[i] &&
  383. i == hapd->conf->ssid.wep.idx)
  384. hostapd_set_privacy(hapd, 1);
  385. }
  386. return 0;
  387. }
  388. static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
  389. {
  390. int ret = 0;
  391. u8 addr[ETH_ALEN];
  392. if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
  393. return 0;
  394. if (!hapd->iface->driver_ap_teardown) {
  395. wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
  396. "Flushing old station entries");
  397. if (hostapd_flush(hapd)) {
  398. wpa_msg(hapd->msg_ctx, MSG_WARNING,
  399. "Could not connect to kernel driver");
  400. ret = -1;
  401. }
  402. }
  403. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
  404. os_memset(addr, 0xff, ETH_ALEN);
  405. hostapd_drv_sta_deauth(hapd, addr, reason);
  406. hostapd_free_stas(hapd);
  407. return ret;
  408. }
  409. static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
  410. {
  411. hostapd_free_stas(hapd);
  412. hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
  413. hostapd_clear_wep(hapd);
  414. }
  415. /**
  416. * hostapd_validate_bssid_configuration - Validate BSSID configuration
  417. * @iface: Pointer to interface data
  418. * Returns: 0 on success, -1 on failure
  419. *
  420. * This function is used to validate that the configured BSSIDs are valid.
  421. */
  422. static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
  423. {
  424. u8 mask[ETH_ALEN] = { 0 };
  425. struct hostapd_data *hapd = iface->bss[0];
  426. unsigned int i = iface->conf->num_bss, bits = 0, j;
  427. int auto_addr = 0;
  428. if (hostapd_drv_none(hapd))
  429. return 0;
  430. /* Generate BSSID mask that is large enough to cover the BSSIDs. */
  431. /* Determine the bits necessary to cover the number of BSSIDs. */
  432. for (i--; i; i >>= 1)
  433. bits++;
  434. /* Determine the bits necessary to any configured BSSIDs,
  435. if they are higher than the number of BSSIDs. */
  436. for (j = 0; j < iface->conf->num_bss; j++) {
  437. if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
  438. if (j)
  439. auto_addr++;
  440. continue;
  441. }
  442. for (i = 0; i < ETH_ALEN; i++) {
  443. mask[i] |=
  444. iface->conf->bss[j]->bssid[i] ^
  445. hapd->own_addr[i];
  446. }
  447. }
  448. if (!auto_addr)
  449. goto skip_mask_ext;
  450. for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
  451. ;
  452. j = 0;
  453. if (i < ETH_ALEN) {
  454. j = (5 - i) * 8;
  455. while (mask[i] != 0) {
  456. mask[i] >>= 1;
  457. j++;
  458. }
  459. }
  460. if (bits < j)
  461. bits = j;
  462. if (bits > 40) {
  463. wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
  464. bits);
  465. return -1;
  466. }
  467. os_memset(mask, 0xff, ETH_ALEN);
  468. j = bits / 8;
  469. for (i = 5; i > 5 - j; i--)
  470. mask[i] = 0;
  471. j = bits % 8;
  472. while (j--)
  473. mask[i] <<= 1;
  474. skip_mask_ext:
  475. wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
  476. (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
  477. if (!auto_addr)
  478. return 0;
  479. for (i = 0; i < ETH_ALEN; i++) {
  480. if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
  481. wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
  482. " for start address " MACSTR ".",
  483. MAC2STR(mask), MAC2STR(hapd->own_addr));
  484. wpa_printf(MSG_ERROR, "Start address must be the "
  485. "first address in the block (i.e., addr "
  486. "AND mask == addr).");
  487. return -1;
  488. }
  489. }
  490. return 0;
  491. }
  492. static int mac_in_conf(struct hostapd_config *conf, const void *a)
  493. {
  494. size_t i;
  495. for (i = 0; i < conf->num_bss; i++) {
  496. if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
  497. return 1;
  498. }
  499. }
  500. return 0;
  501. }
  502. #ifndef CONFIG_NO_RADIUS
  503. static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
  504. struct radius_das_attrs *attr)
  505. {
  506. if (attr->nas_identifier &&
  507. (!hapd->conf->nas_identifier ||
  508. os_strlen(hapd->conf->nas_identifier) !=
  509. attr->nas_identifier_len ||
  510. os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
  511. attr->nas_identifier_len) != 0)) {
  512. wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
  513. return 1;
  514. }
  515. if (attr->nas_ip_addr &&
  516. (hapd->conf->own_ip_addr.af != AF_INET ||
  517. os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
  518. 0)) {
  519. wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
  520. return 1;
  521. }
  522. #ifdef CONFIG_IPV6
  523. if (attr->nas_ipv6_addr &&
  524. (hapd->conf->own_ip_addr.af != AF_INET6 ||
  525. os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
  526. != 0)) {
  527. wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
  528. return 1;
  529. }
  530. #endif /* CONFIG_IPV6 */
  531. return 0;
  532. }
  533. static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
  534. struct radius_das_attrs *attr,
  535. int *multi)
  536. {
  537. struct sta_info *selected, *sta;
  538. char buf[128];
  539. int num_attr = 0;
  540. int count;
  541. *multi = 0;
  542. for (sta = hapd->sta_list; sta; sta = sta->next)
  543. sta->radius_das_match = 1;
  544. if (attr->sta_addr) {
  545. num_attr++;
  546. sta = ap_get_sta(hapd, attr->sta_addr);
  547. if (!sta) {
  548. wpa_printf(MSG_DEBUG,
  549. "RADIUS DAS: No Calling-Station-Id match");
  550. return NULL;
  551. }
  552. selected = sta;
  553. for (sta = hapd->sta_list; sta; sta = sta->next) {
  554. if (sta != selected)
  555. sta->radius_das_match = 0;
  556. }
  557. wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
  558. }
  559. if (attr->acct_session_id) {
  560. num_attr++;
  561. if (attr->acct_session_id_len != 16) {
  562. wpa_printf(MSG_DEBUG,
  563. "RADIUS DAS: Acct-Session-Id cannot match");
  564. return NULL;
  565. }
  566. count = 0;
  567. for (sta = hapd->sta_list; sta; sta = sta->next) {
  568. if (!sta->radius_das_match)
  569. continue;
  570. os_snprintf(buf, sizeof(buf), "%016llX",
  571. (unsigned long long) sta->acct_session_id);
  572. if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
  573. sta->radius_das_match = 0;
  574. else
  575. count++;
  576. }
  577. if (count == 0) {
  578. wpa_printf(MSG_DEBUG,
  579. "RADIUS DAS: No matches remaining after Acct-Session-Id check");
  580. return NULL;
  581. }
  582. wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
  583. }
  584. if (attr->acct_multi_session_id) {
  585. num_attr++;
  586. if (attr->acct_multi_session_id_len != 16) {
  587. wpa_printf(MSG_DEBUG,
  588. "RADIUS DAS: Acct-Multi-Session-Id cannot match");
  589. return NULL;
  590. }
  591. count = 0;
  592. for (sta = hapd->sta_list; sta; sta = sta->next) {
  593. if (!sta->radius_das_match)
  594. continue;
  595. if (!sta->eapol_sm ||
  596. !sta->eapol_sm->acct_multi_session_id) {
  597. sta->radius_das_match = 0;
  598. continue;
  599. }
  600. os_snprintf(buf, sizeof(buf), "%016llX",
  601. (unsigned long long)
  602. sta->eapol_sm->acct_multi_session_id);
  603. if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
  604. 0)
  605. sta->radius_das_match = 0;
  606. else
  607. count++;
  608. }
  609. if (count == 0) {
  610. wpa_printf(MSG_DEBUG,
  611. "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
  612. return NULL;
  613. }
  614. wpa_printf(MSG_DEBUG,
  615. "RADIUS DAS: Acct-Multi-Session-Id match");
  616. }
  617. if (attr->cui) {
  618. num_attr++;
  619. count = 0;
  620. for (sta = hapd->sta_list; sta; sta = sta->next) {
  621. struct wpabuf *cui;
  622. if (!sta->radius_das_match)
  623. continue;
  624. cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
  625. if (!cui || wpabuf_len(cui) != attr->cui_len ||
  626. os_memcmp(wpabuf_head(cui), attr->cui,
  627. attr->cui_len) != 0)
  628. sta->radius_das_match = 0;
  629. else
  630. count++;
  631. }
  632. if (count == 0) {
  633. wpa_printf(MSG_DEBUG,
  634. "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
  635. return NULL;
  636. }
  637. wpa_printf(MSG_DEBUG,
  638. "RADIUS DAS: Chargeable-User-Identity match");
  639. }
  640. if (attr->user_name) {
  641. num_attr++;
  642. count = 0;
  643. for (sta = hapd->sta_list; sta; sta = sta->next) {
  644. u8 *identity;
  645. size_t identity_len;
  646. if (!sta->radius_das_match)
  647. continue;
  648. identity = ieee802_1x_get_identity(sta->eapol_sm,
  649. &identity_len);
  650. if (!identity ||
  651. identity_len != attr->user_name_len ||
  652. os_memcmp(identity, attr->user_name, identity_len)
  653. != 0)
  654. sta->radius_das_match = 0;
  655. else
  656. count++;
  657. }
  658. if (count == 0) {
  659. wpa_printf(MSG_DEBUG,
  660. "RADIUS DAS: No matches remaining after User-Name check");
  661. return NULL;
  662. }
  663. wpa_printf(MSG_DEBUG,
  664. "RADIUS DAS: User-Name match");
  665. }
  666. if (num_attr == 0) {
  667. /*
  668. * In theory, we could match all current associations, but it
  669. * seems safer to just reject requests that do not include any
  670. * session identification attributes.
  671. */
  672. wpa_printf(MSG_DEBUG,
  673. "RADIUS DAS: No session identification attributes included");
  674. return NULL;
  675. }
  676. selected = NULL;
  677. for (sta = hapd->sta_list; sta; sta = sta->next) {
  678. if (sta->radius_das_match) {
  679. if (selected) {
  680. *multi = 1;
  681. return NULL;
  682. }
  683. selected = sta;
  684. }
  685. }
  686. return selected;
  687. }
  688. static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
  689. struct radius_das_attrs *attr)
  690. {
  691. if (!hapd->wpa_auth)
  692. return -1;
  693. return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
  694. }
  695. static enum radius_das_res
  696. hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
  697. {
  698. struct hostapd_data *hapd = ctx;
  699. struct sta_info *sta;
  700. int multi;
  701. if (hostapd_das_nas_mismatch(hapd, attr))
  702. return RADIUS_DAS_NAS_MISMATCH;
  703. sta = hostapd_das_find_sta(hapd, attr, &multi);
  704. if (sta == NULL) {
  705. if (multi) {
  706. wpa_printf(MSG_DEBUG,
  707. "RADIUS DAS: Multiple sessions match - not supported");
  708. return RADIUS_DAS_MULTI_SESSION_MATCH;
  709. }
  710. if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
  711. wpa_printf(MSG_DEBUG,
  712. "RADIUS DAS: PMKSA cache entry matched");
  713. return RADIUS_DAS_SUCCESS;
  714. }
  715. wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
  716. return RADIUS_DAS_SESSION_NOT_FOUND;
  717. }
  718. wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
  719. " - disconnecting", MAC2STR(sta->addr));
  720. wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
  721. hostapd_drv_sta_deauth(hapd, sta->addr,
  722. WLAN_REASON_PREV_AUTH_NOT_VALID);
  723. ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
  724. return RADIUS_DAS_SUCCESS;
  725. }
  726. #endif /* CONFIG_NO_RADIUS */
  727. /**
  728. * hostapd_setup_bss - Per-BSS setup (initialization)
  729. * @hapd: Pointer to BSS data
  730. * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
  731. * but interface may exist
  732. *
  733. * This function is used to initialize all per-BSS data structures and
  734. * resources. This gets called in a loop for each BSS when an interface is
  735. * initialized. Most of the modules that are initialized here will be
  736. * deinitialized in hostapd_cleanup().
  737. */
  738. static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
  739. {
  740. struct hostapd_bss_config *conf = hapd->conf;
  741. u8 ssid[SSID_MAX_LEN + 1];
  742. int ssid_len, set_ssid;
  743. char force_ifname[IFNAMSIZ];
  744. u8 if_addr[ETH_ALEN];
  745. int flush_old_stations = 1;
  746. wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
  747. __func__, hapd, conf->iface, first);
  748. #ifdef EAP_SERVER_TNC
  749. if (conf->tnc && tncs_global_init() < 0) {
  750. wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
  751. return -1;
  752. }
  753. #endif /* EAP_SERVER_TNC */
  754. if (hapd->started) {
  755. wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
  756. __func__, conf->iface);
  757. return -1;
  758. }
  759. hapd->started = 1;
  760. if (!first || first == -1) {
  761. if (is_zero_ether_addr(conf->bssid)) {
  762. /* Allocate the next available BSSID. */
  763. do {
  764. inc_byte_array(hapd->own_addr, ETH_ALEN);
  765. } while (mac_in_conf(hapd->iconf, hapd->own_addr));
  766. } else {
  767. /* Allocate the configured BSSID. */
  768. os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
  769. if (hostapd_mac_comp(hapd->own_addr,
  770. hapd->iface->bss[0]->own_addr) ==
  771. 0) {
  772. wpa_printf(MSG_ERROR, "BSS '%s' may not have "
  773. "BSSID set to the MAC address of "
  774. "the radio", conf->iface);
  775. return -1;
  776. }
  777. }
  778. hapd->interface_added = 1;
  779. if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
  780. conf->iface, hapd->own_addr, hapd,
  781. &hapd->drv_priv, force_ifname, if_addr,
  782. conf->bridge[0] ? conf->bridge : NULL,
  783. first == -1)) {
  784. wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
  785. MACSTR ")", MAC2STR(hapd->own_addr));
  786. hapd->interface_added = 0;
  787. return -1;
  788. }
  789. }
  790. if (conf->wmm_enabled < 0)
  791. conf->wmm_enabled = hapd->iconf->ieee80211n;
  792. #ifdef CONFIG_IEEE80211R
  793. if (is_zero_ether_addr(conf->r1_key_holder))
  794. os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
  795. #endif /* CONFIG_IEEE80211R */
  796. #ifdef CONFIG_MESH
  797. if (hapd->iface->mconf == NULL)
  798. flush_old_stations = 0;
  799. #endif /* CONFIG_MESH */
  800. if (flush_old_stations)
  801. hostapd_flush_old_stations(hapd,
  802. WLAN_REASON_PREV_AUTH_NOT_VALID);
  803. hostapd_set_privacy(hapd, 0);
  804. hostapd_broadcast_wep_clear(hapd);
  805. if (hostapd_setup_encryption(conf->iface, hapd))
  806. return -1;
  807. /*
  808. * Fetch the SSID from the system and use it or,
  809. * if one was specified in the config file, verify they
  810. * match.
  811. */
  812. ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
  813. if (ssid_len < 0) {
  814. wpa_printf(MSG_ERROR, "Could not read SSID from system");
  815. return -1;
  816. }
  817. if (conf->ssid.ssid_set) {
  818. /*
  819. * If SSID is specified in the config file and it differs
  820. * from what is being used then force installation of the
  821. * new SSID.
  822. */
  823. set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
  824. os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
  825. } else {
  826. /*
  827. * No SSID in the config file; just use the one we got
  828. * from the system.
  829. */
  830. set_ssid = 0;
  831. conf->ssid.ssid_len = ssid_len;
  832. os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
  833. }
  834. if (!hostapd_drv_none(hapd)) {
  835. wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
  836. " and ssid \"%s\"",
  837. conf->iface, MAC2STR(hapd->own_addr),
  838. wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
  839. }
  840. if (hostapd_setup_wpa_psk(conf)) {
  841. wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
  842. return -1;
  843. }
  844. /* Set SSID for the kernel driver (to be used in beacon and probe
  845. * response frames) */
  846. if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
  847. conf->ssid.ssid_len)) {
  848. wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
  849. return -1;
  850. }
  851. if (wpa_debug_level <= MSG_MSGDUMP)
  852. conf->radius->msg_dumps = 1;
  853. #ifndef CONFIG_NO_RADIUS
  854. hapd->radius = radius_client_init(hapd, conf->radius);
  855. if (hapd->radius == NULL) {
  856. wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
  857. return -1;
  858. }
  859. if (conf->radius_das_port) {
  860. struct radius_das_conf das_conf;
  861. os_memset(&das_conf, 0, sizeof(das_conf));
  862. das_conf.port = conf->radius_das_port;
  863. das_conf.shared_secret = conf->radius_das_shared_secret;
  864. das_conf.shared_secret_len =
  865. conf->radius_das_shared_secret_len;
  866. das_conf.client_addr = &conf->radius_das_client_addr;
  867. das_conf.time_window = conf->radius_das_time_window;
  868. das_conf.require_event_timestamp =
  869. conf->radius_das_require_event_timestamp;
  870. das_conf.ctx = hapd;
  871. das_conf.disconnect = hostapd_das_disconnect;
  872. hapd->radius_das = radius_das_init(&das_conf);
  873. if (hapd->radius_das == NULL) {
  874. wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
  875. "failed.");
  876. return -1;
  877. }
  878. }
  879. #endif /* CONFIG_NO_RADIUS */
  880. if (hostapd_acl_init(hapd)) {
  881. wpa_printf(MSG_ERROR, "ACL initialization failed.");
  882. return -1;
  883. }
  884. if (hostapd_init_wps(hapd, conf))
  885. return -1;
  886. if (authsrv_init(hapd) < 0)
  887. return -1;
  888. if (ieee802_1x_init(hapd)) {
  889. wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
  890. return -1;
  891. }
  892. if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
  893. return -1;
  894. if (accounting_init(hapd)) {
  895. wpa_printf(MSG_ERROR, "Accounting initialization failed.");
  896. return -1;
  897. }
  898. if (conf->ieee802_11f &&
  899. (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
  900. wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
  901. "failed.");
  902. return -1;
  903. }
  904. #ifdef CONFIG_INTERWORKING
  905. if (gas_serv_init(hapd)) {
  906. wpa_printf(MSG_ERROR, "GAS server initialization failed");
  907. return -1;
  908. }
  909. if (conf->qos_map_set_len &&
  910. hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
  911. conf->qos_map_set_len)) {
  912. wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
  913. return -1;
  914. }
  915. #endif /* CONFIG_INTERWORKING */
  916. if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
  917. wpa_printf(MSG_ERROR, "BSS Load initialization failed");
  918. return -1;
  919. }
  920. if (conf->proxy_arp) {
  921. if (x_snoop_init(hapd)) {
  922. wpa_printf(MSG_ERROR,
  923. "Generic snooping infrastructure initialization failed");
  924. return -1;
  925. }
  926. if (dhcp_snoop_init(hapd)) {
  927. wpa_printf(MSG_ERROR,
  928. "DHCP snooping initialization failed");
  929. return -1;
  930. }
  931. if (ndisc_snoop_init(hapd)) {
  932. wpa_printf(MSG_ERROR,
  933. "Neighbor Discovery snooping initialization failed");
  934. return -1;
  935. }
  936. }
  937. if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
  938. wpa_printf(MSG_ERROR, "VLAN initialization failed.");
  939. return -1;
  940. }
  941. if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
  942. return -1;
  943. if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
  944. return -1;
  945. if (hapd->driver && hapd->driver->set_operstate)
  946. hapd->driver->set_operstate(hapd->drv_priv, 1);
  947. return 0;
  948. }
  949. static void hostapd_tx_queue_params(struct hostapd_iface *iface)
  950. {
  951. struct hostapd_data *hapd = iface->bss[0];
  952. int i;
  953. struct hostapd_tx_queue_params *p;
  954. #ifdef CONFIG_MESH
  955. if (iface->mconf == NULL)
  956. return;
  957. #endif /* CONFIG_MESH */
  958. for (i = 0; i < NUM_TX_QUEUES; i++) {
  959. p = &iface->conf->tx_queue[i];
  960. if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
  961. p->cwmax, p->burst)) {
  962. wpa_printf(MSG_DEBUG, "Failed to set TX queue "
  963. "parameters for queue %d.", i);
  964. /* Continue anyway */
  965. }
  966. }
  967. }
  968. static int hostapd_set_acl_list(struct hostapd_data *hapd,
  969. struct mac_acl_entry *mac_acl,
  970. int n_entries, u8 accept_acl)
  971. {
  972. struct hostapd_acl_params *acl_params;
  973. int i, err;
  974. acl_params = os_zalloc(sizeof(*acl_params) +
  975. (n_entries * sizeof(acl_params->mac_acl[0])));
  976. if (!acl_params)
  977. return -ENOMEM;
  978. for (i = 0; i < n_entries; i++)
  979. os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
  980. ETH_ALEN);
  981. acl_params->acl_policy = accept_acl;
  982. acl_params->num_mac_acl = n_entries;
  983. err = hostapd_drv_set_acl(hapd, acl_params);
  984. os_free(acl_params);
  985. return err;
  986. }
  987. static void hostapd_set_acl(struct hostapd_data *hapd)
  988. {
  989. struct hostapd_config *conf = hapd->iconf;
  990. int err;
  991. u8 accept_acl;
  992. if (hapd->iface->drv_max_acl_mac_addrs == 0)
  993. return;
  994. if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
  995. accept_acl = 1;
  996. err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
  997. conf->bss[0]->num_accept_mac,
  998. accept_acl);
  999. if (err) {
  1000. wpa_printf(MSG_DEBUG, "Failed to set accept acl");
  1001. return;
  1002. }
  1003. } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
  1004. accept_acl = 0;
  1005. err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
  1006. conf->bss[0]->num_deny_mac,
  1007. accept_acl);
  1008. if (err) {
  1009. wpa_printf(MSG_DEBUG, "Failed to set deny acl");
  1010. return;
  1011. }
  1012. }
  1013. }
  1014. static int start_ctrl_iface_bss(struct hostapd_data *hapd)
  1015. {
  1016. if (!hapd->iface->interfaces ||
  1017. !hapd->iface->interfaces->ctrl_iface_init)
  1018. return 0;
  1019. if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
  1020. wpa_printf(MSG_ERROR,
  1021. "Failed to setup control interface for %s",
  1022. hapd->conf->iface);
  1023. return -1;
  1024. }
  1025. return 0;
  1026. }
  1027. static int start_ctrl_iface(struct hostapd_iface *iface)
  1028. {
  1029. size_t i;
  1030. if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
  1031. return 0;
  1032. for (i = 0; i < iface->num_bss; i++) {
  1033. struct hostapd_data *hapd = iface->bss[i];
  1034. if (iface->interfaces->ctrl_iface_init(hapd)) {
  1035. wpa_printf(MSG_ERROR,
  1036. "Failed to setup control interface for %s",
  1037. hapd->conf->iface);
  1038. return -1;
  1039. }
  1040. }
  1041. return 0;
  1042. }
  1043. static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
  1044. {
  1045. struct hostapd_iface *iface = eloop_ctx;
  1046. if (!iface->wait_channel_update) {
  1047. wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
  1048. return;
  1049. }
  1050. /*
  1051. * It is possible that the existing channel list is acceptable, so try
  1052. * to proceed.
  1053. */
  1054. wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
  1055. setup_interface2(iface);
  1056. }
  1057. void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
  1058. {
  1059. if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
  1060. return;
  1061. wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
  1062. eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
  1063. setup_interface2(iface);
  1064. }
  1065. static int setup_interface(struct hostapd_iface *iface)
  1066. {
  1067. struct hostapd_data *hapd = iface->bss[0];
  1068. size_t i;
  1069. /*
  1070. * It is possible that setup_interface() is called after the interface
  1071. * was disabled etc., in which case driver_ap_teardown is possibly set
  1072. * to 1. Clear it here so any other key/station deletion, which is not
  1073. * part of a teardown flow, would also call the relevant driver
  1074. * callbacks.
  1075. */
  1076. iface->driver_ap_teardown = 0;
  1077. if (!iface->phy[0]) {
  1078. const char *phy = hostapd_drv_get_radio_name(hapd);
  1079. if (phy) {
  1080. wpa_printf(MSG_DEBUG, "phy: %s", phy);
  1081. os_strlcpy(iface->phy, phy, sizeof(iface->phy));
  1082. }
  1083. }
  1084. /*
  1085. * Make sure that all BSSes get configured with a pointer to the same
  1086. * driver interface.
  1087. */
  1088. for (i = 1; i < iface->num_bss; i++) {
  1089. iface->bss[i]->driver = hapd->driver;
  1090. iface->bss[i]->drv_priv = hapd->drv_priv;
  1091. }
  1092. if (hostapd_validate_bssid_configuration(iface))
  1093. return -1;
  1094. /*
  1095. * Initialize control interfaces early to allow external monitoring of
  1096. * channel setup operations that may take considerable amount of time
  1097. * especially for DFS cases.
  1098. */
  1099. if (start_ctrl_iface(iface))
  1100. return -1;
  1101. if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
  1102. char country[4], previous_country[4];
  1103. hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
  1104. if (hostapd_get_country(hapd, previous_country) < 0)
  1105. previous_country[0] = '\0';
  1106. os_memcpy(country, hapd->iconf->country, 3);
  1107. country[3] = '\0';
  1108. if (hostapd_set_country(hapd, country) < 0) {
  1109. wpa_printf(MSG_ERROR, "Failed to set country code");
  1110. return -1;
  1111. }
  1112. wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
  1113. previous_country, country);
  1114. if (os_strncmp(previous_country, country, 2) != 0) {
  1115. wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
  1116. iface->wait_channel_update = 1;
  1117. eloop_register_timeout(5, 0,
  1118. channel_list_update_timeout,
  1119. iface, NULL);
  1120. return 0;
  1121. }
  1122. }
  1123. return setup_interface2(iface);
  1124. }
  1125. static int setup_interface2(struct hostapd_iface *iface)
  1126. {
  1127. iface->wait_channel_update = 0;
  1128. if (hostapd_get_hw_features(iface)) {
  1129. /* Not all drivers support this yet, so continue without hw
  1130. * feature data. */
  1131. } else {
  1132. int ret = hostapd_select_hw_mode(iface);
  1133. if (ret < 0) {
  1134. wpa_printf(MSG_ERROR, "Could not select hw_mode and "
  1135. "channel. (%d)", ret);
  1136. goto fail;
  1137. }
  1138. if (ret == 1) {
  1139. wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
  1140. return 0;
  1141. }
  1142. ret = hostapd_check_ht_capab(iface);
  1143. if (ret < 0)
  1144. goto fail;
  1145. if (ret == 1) {
  1146. wpa_printf(MSG_DEBUG, "Interface initialization will "
  1147. "be completed in a callback");
  1148. return 0;
  1149. }
  1150. if (iface->conf->ieee80211h)
  1151. wpa_printf(MSG_DEBUG, "DFS support is enabled");
  1152. }
  1153. return hostapd_setup_interface_complete(iface, 0);
  1154. fail:
  1155. hostapd_set_state(iface, HAPD_IFACE_DISABLED);
  1156. wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
  1157. if (iface->interfaces && iface->interfaces->terminate_on_error)
  1158. eloop_terminate();
  1159. return -1;
  1160. }
  1161. #ifdef CONFIG_FST
  1162. static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
  1163. {
  1164. struct hostapd_data *hapd = ctx;
  1165. return hapd->own_addr;
  1166. }
  1167. static void fst_hostapd_get_channel_info_cb(void *ctx,
  1168. enum hostapd_hw_mode *hw_mode,
  1169. u8 *channel)
  1170. {
  1171. struct hostapd_data *hapd = ctx;
  1172. *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
  1173. }
  1174. static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
  1175. {
  1176. struct hostapd_data *hapd = ctx;
  1177. if (hapd->iface->fst_ies != fst_ies) {
  1178. hapd->iface->fst_ies = fst_ies;
  1179. if (ieee802_11_set_beacon(hapd))
  1180. wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
  1181. }
  1182. }
  1183. static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
  1184. struct wpabuf *buf)
  1185. {
  1186. struct hostapd_data *hapd = ctx;
  1187. return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
  1188. wpabuf_head(buf), wpabuf_len(buf));
  1189. }
  1190. static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
  1191. {
  1192. struct hostapd_data *hapd = ctx;
  1193. struct sta_info *sta = ap_get_sta(hapd, addr);
  1194. return sta ? sta->mb_ies : NULL;
  1195. }
  1196. static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
  1197. const u8 *buf, size_t size)
  1198. {
  1199. struct hostapd_data *hapd = ctx;
  1200. struct sta_info *sta = ap_get_sta(hapd, addr);
  1201. if (sta) {
  1202. struct mb_ies_info info;
  1203. if (!mb_ies_info_by_ies(&info, buf, size)) {
  1204. wpabuf_free(sta->mb_ies);
  1205. sta->mb_ies = mb_ies_by_info(&info);
  1206. }
  1207. }
  1208. }
  1209. static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
  1210. Boolean mb_only)
  1211. {
  1212. struct sta_info *s = (struct sta_info *) *get_ctx;
  1213. if (mb_only) {
  1214. for (; s && !s->mb_ies; s = s->next)
  1215. ;
  1216. }
  1217. if (s) {
  1218. *get_ctx = (struct fst_get_peer_ctx *) s->next;
  1219. return s->addr;
  1220. }
  1221. *get_ctx = NULL;
  1222. return NULL;
  1223. }
  1224. static const u8 * fst_hostapd_get_peer_first(void *ctx,
  1225. struct fst_get_peer_ctx **get_ctx,
  1226. Boolean mb_only)
  1227. {
  1228. struct hostapd_data *hapd = ctx;
  1229. *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
  1230. return fst_hostapd_get_sta(get_ctx, mb_only);
  1231. }
  1232. static const u8 * fst_hostapd_get_peer_next(void *ctx,
  1233. struct fst_get_peer_ctx **get_ctx,
  1234. Boolean mb_only)
  1235. {
  1236. return fst_hostapd_get_sta(get_ctx, mb_only);
  1237. }
  1238. void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
  1239. struct fst_wpa_obj *iface_obj)
  1240. {
  1241. iface_obj->ctx = hapd;
  1242. iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
  1243. iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
  1244. iface_obj->set_ies = fst_hostapd_set_ies_cb;
  1245. iface_obj->send_action = fst_hostapd_send_action_cb;
  1246. iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
  1247. iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
  1248. iface_obj->get_peer_first = fst_hostapd_get_peer_first;
  1249. iface_obj->get_peer_next = fst_hostapd_get_peer_next;
  1250. }
  1251. #endif /* CONFIG_FST */
  1252. static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
  1253. int err)
  1254. {
  1255. struct hostapd_data *hapd = iface->bss[0];
  1256. size_t j;
  1257. u8 *prev_addr;
  1258. int delay_apply_cfg = 0;
  1259. int res_dfs_offload = 0;
  1260. if (err)
  1261. goto fail;
  1262. wpa_printf(MSG_DEBUG, "Completing interface initialization");
  1263. if (iface->conf->channel) {
  1264. #ifdef NEED_AP_MLME
  1265. int res;
  1266. #endif /* NEED_AP_MLME */
  1267. iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
  1268. wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
  1269. "Frequency: %d MHz",
  1270. hostapd_hw_mode_txt(iface->conf->hw_mode),
  1271. iface->conf->channel, iface->freq);
  1272. #ifdef NEED_AP_MLME
  1273. /* Handle DFS only if it is not offloaded to the driver */
  1274. if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
  1275. /* Check DFS */
  1276. res = hostapd_handle_dfs(iface);
  1277. if (res <= 0) {
  1278. if (res < 0)
  1279. goto fail;
  1280. return res;
  1281. }
  1282. } else {
  1283. /* If DFS is offloaded to the driver */
  1284. res_dfs_offload = hostapd_handle_dfs_offload(iface);
  1285. if (res_dfs_offload <= 0) {
  1286. if (res_dfs_offload < 0)
  1287. goto fail;
  1288. } else {
  1289. wpa_printf(MSG_DEBUG,
  1290. "Proceed with AP/channel setup");
  1291. /*
  1292. * If this is a DFS channel, move to completing
  1293. * AP setup.
  1294. */
  1295. if (res_dfs_offload == 1)
  1296. goto dfs_offload;
  1297. /* Otherwise fall through. */
  1298. }
  1299. }
  1300. #endif /* NEED_AP_MLME */
  1301. #ifdef CONFIG_MESH
  1302. if (iface->mconf != NULL) {
  1303. wpa_printf(MSG_DEBUG,
  1304. "%s: Mesh configuration will be applied while joining the mesh network",
  1305. iface->bss[0]->conf->iface);
  1306. delay_apply_cfg = 1;
  1307. }
  1308. #endif /* CONFIG_MESH */
  1309. if (!delay_apply_cfg &&
  1310. hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
  1311. hapd->iconf->channel,
  1312. hapd->iconf->ieee80211n,
  1313. hapd->iconf->ieee80211ac,
  1314. hapd->iconf->secondary_channel,
  1315. hapd->iconf->vht_oper_chwidth,
  1316. hapd->iconf->vht_oper_centr_freq_seg0_idx,
  1317. hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
  1318. wpa_printf(MSG_ERROR, "Could not set channel for "
  1319. "kernel driver");
  1320. goto fail;
  1321. }
  1322. }
  1323. if (iface->current_mode) {
  1324. if (hostapd_prepare_rates(iface, iface->current_mode)) {
  1325. wpa_printf(MSG_ERROR, "Failed to prepare rates "
  1326. "table.");
  1327. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
  1328. HOSTAPD_LEVEL_WARNING,
  1329. "Failed to prepare rates table.");
  1330. goto fail;
  1331. }
  1332. }
  1333. if (hapd->iconf->rts_threshold > -1 &&
  1334. hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
  1335. wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
  1336. "kernel driver");
  1337. goto fail;
  1338. }
  1339. if (hapd->iconf->fragm_threshold > -1 &&
  1340. hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
  1341. wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
  1342. "for kernel driver");
  1343. goto fail;
  1344. }
  1345. prev_addr = hapd->own_addr;
  1346. for (j = 0; j < iface->num_bss; j++) {
  1347. hapd = iface->bss[j];
  1348. if (j)
  1349. os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
  1350. if (hostapd_setup_bss(hapd, j == 0)) {
  1351. do {
  1352. hapd = iface->bss[j];
  1353. hostapd_bss_deinit_no_free(hapd);
  1354. hostapd_free_hapd_data(hapd);
  1355. } while (j-- > 0);
  1356. goto fail;
  1357. }
  1358. if (is_zero_ether_addr(hapd->conf->bssid))
  1359. prev_addr = hapd->own_addr;
  1360. }
  1361. hapd = iface->bss[0];
  1362. hostapd_tx_queue_params(iface);
  1363. ap_list_init(iface);
  1364. dl_list_init(&iface->sta_seen);
  1365. hostapd_set_acl(hapd);
  1366. if (hostapd_driver_commit(hapd) < 0) {
  1367. wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
  1368. "configuration", __func__);
  1369. goto fail;
  1370. }
  1371. /*
  1372. * WPS UPnP module can be initialized only when the "upnp_iface" is up.
  1373. * If "interface" and "upnp_iface" are the same (e.g., non-bridge
  1374. * mode), the interface is up only after driver_commit, so initialize
  1375. * WPS after driver_commit.
  1376. */
  1377. for (j = 0; j < iface->num_bss; j++) {
  1378. if (hostapd_init_wps_complete(iface->bss[j]))
  1379. goto fail;
  1380. }
  1381. if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
  1382. !res_dfs_offload) {
  1383. /*
  1384. * If freq is DFS, and DFS is offloaded to the driver, then wait
  1385. * for CAC to complete.
  1386. */
  1387. wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
  1388. return res_dfs_offload;
  1389. }
  1390. #ifdef NEED_AP_MLME
  1391. dfs_offload:
  1392. #endif /* NEED_AP_MLME */
  1393. #ifdef CONFIG_FST
  1394. if (hapd->iconf->fst_cfg.group_id[0]) {
  1395. struct fst_wpa_obj iface_obj;
  1396. fst_hostapd_fill_iface_obj(hapd, &iface_obj);
  1397. iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
  1398. &iface_obj, &hapd->iconf->fst_cfg);
  1399. if (!iface->fst) {
  1400. wpa_printf(MSG_ERROR, "Could not attach to FST %s",
  1401. hapd->iconf->fst_cfg.group_id);
  1402. goto fail;
  1403. }
  1404. }
  1405. #endif /* CONFIG_FST */
  1406. hostapd_set_state(iface, HAPD_IFACE_ENABLED);
  1407. wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
  1408. if (hapd->setup_complete_cb)
  1409. hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
  1410. wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
  1411. iface->bss[0]->conf->iface);
  1412. if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
  1413. iface->interfaces->terminate_on_error--;
  1414. return 0;
  1415. fail:
  1416. wpa_printf(MSG_ERROR, "Interface initialization failed");
  1417. hostapd_set_state(iface, HAPD_IFACE_DISABLED);
  1418. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
  1419. #ifdef CONFIG_FST
  1420. if (iface->fst) {
  1421. fst_detach(iface->fst);
  1422. iface->fst = NULL;
  1423. }
  1424. #endif /* CONFIG_FST */
  1425. if (iface->interfaces && iface->interfaces->terminate_on_error)
  1426. eloop_terminate();
  1427. return -1;
  1428. }
  1429. /**
  1430. * hostapd_setup_interface_complete - Complete interface setup
  1431. *
  1432. * This function is called when previous steps in the interface setup has been
  1433. * completed. This can also start operations, e.g., DFS, that will require
  1434. * additional processing before interface is ready to be enabled. Such
  1435. * operations will call this function from eloop callbacks when finished.
  1436. */
  1437. int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
  1438. {
  1439. struct hapd_interfaces *interfaces = iface->interfaces;
  1440. struct hostapd_data *hapd = iface->bss[0];
  1441. unsigned int i;
  1442. int not_ready_in_sync_ifaces = 0;
  1443. if (!iface->need_to_start_in_sync)
  1444. return hostapd_setup_interface_complete_sync(iface, err);
  1445. if (err) {
  1446. wpa_printf(MSG_ERROR, "Interface initialization failed");
  1447. hostapd_set_state(iface, HAPD_IFACE_DISABLED);
  1448. iface->need_to_start_in_sync = 0;
  1449. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
  1450. if (interfaces && interfaces->terminate_on_error)
  1451. eloop_terminate();
  1452. return -1;
  1453. }
  1454. if (iface->ready_to_start_in_sync) {
  1455. /* Already in ready and waiting. should never happpen */
  1456. return 0;
  1457. }
  1458. for (i = 0; i < interfaces->count; i++) {
  1459. if (interfaces->iface[i]->need_to_start_in_sync &&
  1460. !interfaces->iface[i]->ready_to_start_in_sync)
  1461. not_ready_in_sync_ifaces++;
  1462. }
  1463. /*
  1464. * Check if this is the last interface, if yes then start all the other
  1465. * waiting interfaces. If not, add this interface to the waiting list.
  1466. */
  1467. if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
  1468. /*
  1469. * If this interface went through CAC, do not synchronize, just
  1470. * start immediately.
  1471. */
  1472. iface->need_to_start_in_sync = 0;
  1473. wpa_printf(MSG_INFO,
  1474. "%s: Finished CAC - bypass sync and start interface",
  1475. iface->bss[0]->conf->iface);
  1476. return hostapd_setup_interface_complete_sync(iface, err);
  1477. }
  1478. if (not_ready_in_sync_ifaces > 1) {
  1479. /* need to wait as there are other interfaces still coming up */
  1480. iface->ready_to_start_in_sync = 1;
  1481. wpa_printf(MSG_INFO,
  1482. "%s: Interface waiting to sync with other interfaces",
  1483. iface->bss[0]->conf->iface);
  1484. return 0;
  1485. }
  1486. wpa_printf(MSG_INFO,
  1487. "%s: Last interface to sync - starting all interfaces",
  1488. iface->bss[0]->conf->iface);
  1489. iface->need_to_start_in_sync = 0;
  1490. hostapd_setup_interface_complete_sync(iface, err);
  1491. for (i = 0; i < interfaces->count; i++) {
  1492. if (interfaces->iface[i]->need_to_start_in_sync &&
  1493. interfaces->iface[i]->ready_to_start_in_sync) {
  1494. hostapd_setup_interface_complete_sync(
  1495. interfaces->iface[i], 0);
  1496. /* Only once the interfaces are sync started */
  1497. interfaces->iface[i]->need_to_start_in_sync = 0;
  1498. }
  1499. }
  1500. return 0;
  1501. }
  1502. /**
  1503. * hostapd_setup_interface - Setup of an interface
  1504. * @iface: Pointer to interface data.
  1505. * Returns: 0 on success, -1 on failure
  1506. *
  1507. * Initializes the driver interface, validates the configuration,
  1508. * and sets driver parameters based on the configuration.
  1509. * Flushes old stations, sets the channel, encryption,
  1510. * beacons, and WDS links based on the configuration.
  1511. *
  1512. * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
  1513. * or DFS operations, this function returns 0 before such operations have been
  1514. * completed. The pending operations are registered into eloop and will be
  1515. * completed from eloop callbacks. Those callbacks end up calling
  1516. * hostapd_setup_interface_complete() once setup has been completed.
  1517. */
  1518. int hostapd_setup_interface(struct hostapd_iface *iface)
  1519. {
  1520. int ret;
  1521. ret = setup_interface(iface);
  1522. if (ret) {
  1523. wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
  1524. iface->bss[0]->conf->iface);
  1525. return -1;
  1526. }
  1527. return 0;
  1528. }
  1529. /**
  1530. * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
  1531. * @hapd_iface: Pointer to interface data
  1532. * @conf: Pointer to per-interface configuration
  1533. * @bss: Pointer to per-BSS configuration for this BSS
  1534. * Returns: Pointer to allocated BSS data
  1535. *
  1536. * This function is used to allocate per-BSS data structure. This data will be
  1537. * freed after hostapd_cleanup() is called for it during interface
  1538. * deinitialization.
  1539. */
  1540. struct hostapd_data *
  1541. hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
  1542. struct hostapd_config *conf,
  1543. struct hostapd_bss_config *bss)
  1544. {
  1545. struct hostapd_data *hapd;
  1546. hapd = os_zalloc(sizeof(*hapd));
  1547. if (hapd == NULL)
  1548. return NULL;
  1549. hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
  1550. hapd->iconf = conf;
  1551. hapd->conf = bss;
  1552. hapd->iface = hapd_iface;
  1553. hapd->driver = hapd->iconf->driver;
  1554. hapd->ctrl_sock = -1;
  1555. return hapd;
  1556. }
  1557. static void hostapd_bss_deinit(struct hostapd_data *hapd)
  1558. {
  1559. wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
  1560. hapd->conf->iface);
  1561. hostapd_bss_deinit_no_free(hapd);
  1562. wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
  1563. hostapd_cleanup(hapd);
  1564. }
  1565. void hostapd_interface_deinit(struct hostapd_iface *iface)
  1566. {
  1567. int j;
  1568. wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
  1569. if (iface == NULL)
  1570. return;
  1571. hostapd_set_state(iface, HAPD_IFACE_DISABLED);
  1572. #ifdef CONFIG_IEEE80211N
  1573. #ifdef NEED_AP_MLME
  1574. hostapd_stop_setup_timers(iface);
  1575. eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
  1576. #endif /* NEED_AP_MLME */
  1577. #endif /* CONFIG_IEEE80211N */
  1578. eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
  1579. iface->wait_channel_update = 0;
  1580. #ifdef CONFIG_FST
  1581. if (iface->fst) {
  1582. fst_detach(iface->fst);
  1583. iface->fst = NULL;
  1584. }
  1585. #endif /* CONFIG_FST */
  1586. for (j = iface->num_bss - 1; j >= 0; j--)
  1587. hostapd_bss_deinit(iface->bss[j]);
  1588. }
  1589. void hostapd_interface_free(struct hostapd_iface *iface)
  1590. {
  1591. size_t j;
  1592. wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
  1593. for (j = 0; j < iface->num_bss; j++) {
  1594. wpa_printf(MSG_DEBUG, "%s: free hapd %p",
  1595. __func__, iface->bss[j]);
  1596. os_free(iface->bss[j]);
  1597. }
  1598. hostapd_cleanup_iface(iface);
  1599. }
  1600. /**
  1601. * hostapd_init - Allocate and initialize per-interface data
  1602. * @config_file: Path to the configuration file
  1603. * Returns: Pointer to the allocated interface data or %NULL on failure
  1604. *
  1605. * This function is used to allocate main data structures for per-interface
  1606. * data. The allocated data buffer will be freed by calling
  1607. * hostapd_cleanup_iface().
  1608. */
  1609. struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
  1610. const char *config_file)
  1611. {
  1612. struct hostapd_iface *hapd_iface = NULL;
  1613. struct hostapd_config *conf = NULL;
  1614. struct hostapd_data *hapd;
  1615. size_t i;
  1616. hapd_iface = os_zalloc(sizeof(*hapd_iface));
  1617. if (hapd_iface == NULL)
  1618. goto fail;
  1619. hapd_iface->config_fname = os_strdup(config_file);
  1620. if (hapd_iface->config_fname == NULL)
  1621. goto fail;
  1622. conf = interfaces->config_read_cb(hapd_iface->config_fname);
  1623. if (conf == NULL)
  1624. goto fail;
  1625. hapd_iface->conf = conf;
  1626. hapd_iface->num_bss = conf->num_bss;
  1627. hapd_iface->bss = os_calloc(conf->num_bss,
  1628. sizeof(struct hostapd_data *));
  1629. if (hapd_iface->bss == NULL)
  1630. goto fail;
  1631. for (i = 0; i < conf->num_bss; i++) {
  1632. hapd = hapd_iface->bss[i] =
  1633. hostapd_alloc_bss_data(hapd_iface, conf,
  1634. conf->bss[i]);
  1635. if (hapd == NULL)
  1636. goto fail;
  1637. hapd->msg_ctx = hapd;
  1638. }
  1639. return hapd_iface;
  1640. fail:
  1641. wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
  1642. config_file);
  1643. if (conf)
  1644. hostapd_config_free(conf);
  1645. if (hapd_iface) {
  1646. os_free(hapd_iface->config_fname);
  1647. os_free(hapd_iface->bss);
  1648. wpa_printf(MSG_DEBUG, "%s: free iface %p",
  1649. __func__, hapd_iface);
  1650. os_free(hapd_iface);
  1651. }
  1652. return NULL;
  1653. }
  1654. static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
  1655. {
  1656. size_t i, j;
  1657. for (i = 0; i < interfaces->count; i++) {
  1658. struct hostapd_iface *iface = interfaces->iface[i];
  1659. for (j = 0; j < iface->num_bss; j++) {
  1660. struct hostapd_data *hapd = iface->bss[j];
  1661. if (os_strcmp(ifname, hapd->conf->iface) == 0)
  1662. return 1;
  1663. }
  1664. }
  1665. return 0;
  1666. }
  1667. /**
  1668. * hostapd_interface_init_bss - Read configuration file and init BSS data
  1669. *
  1670. * This function is used to parse configuration file for a BSS. This BSS is
  1671. * added to an existing interface sharing the same radio (if any) or a new
  1672. * interface is created if this is the first interface on a radio. This
  1673. * allocate memory for the BSS. No actual driver operations are started.
  1674. *
  1675. * This is similar to hostapd_interface_init(), but for a case where the
  1676. * configuration is used to add a single BSS instead of all BSSes for a radio.
  1677. */
  1678. struct hostapd_iface *
  1679. hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
  1680. const char *config_fname, int debug)
  1681. {
  1682. struct hostapd_iface *new_iface = NULL, *iface = NULL;
  1683. struct hostapd_data *hapd;
  1684. int k;
  1685. size_t i, bss_idx;
  1686. if (!phy || !*phy)
  1687. return NULL;
  1688. for (i = 0; i < interfaces->count; i++) {
  1689. if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
  1690. iface = interfaces->iface[i];
  1691. break;
  1692. }
  1693. }
  1694. wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
  1695. config_fname, phy, iface ? "" : " --> new PHY");
  1696. if (iface) {
  1697. struct hostapd_config *conf;
  1698. struct hostapd_bss_config **tmp_conf;
  1699. struct hostapd_data **tmp_bss;
  1700. struct hostapd_bss_config *bss;
  1701. const char *ifname;
  1702. /* Add new BSS to existing iface */
  1703. conf = interfaces->config_read_cb(config_fname);
  1704. if (conf == NULL)
  1705. return NULL;
  1706. if (conf->num_bss > 1) {
  1707. wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
  1708. hostapd_config_free(conf);
  1709. return NULL;
  1710. }
  1711. ifname = conf->bss[0]->iface;
  1712. if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
  1713. wpa_printf(MSG_ERROR,
  1714. "Interface name %s already in use", ifname);
  1715. hostapd_config_free(conf);
  1716. return NULL;
  1717. }
  1718. tmp_conf = os_realloc_array(
  1719. iface->conf->bss, iface->conf->num_bss + 1,
  1720. sizeof(struct hostapd_bss_config *));
  1721. tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
  1722. sizeof(struct hostapd_data *));
  1723. if (tmp_bss)
  1724. iface->bss = tmp_bss;
  1725. if (tmp_conf) {
  1726. iface->conf->bss = tmp_conf;
  1727. iface->conf->last_bss = tmp_conf[0];
  1728. }
  1729. if (tmp_bss == NULL || tmp_conf == NULL) {
  1730. hostapd_config_free(conf);
  1731. return NULL;
  1732. }
  1733. bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
  1734. iface->conf->num_bss++;
  1735. hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
  1736. if (hapd == NULL) {
  1737. iface->conf->num_bss--;
  1738. hostapd_config_free(conf);
  1739. return NULL;
  1740. }
  1741. iface->conf->last_bss = bss;
  1742. iface->bss[iface->num_bss] = hapd;
  1743. hapd->msg_ctx = hapd;
  1744. bss_idx = iface->num_bss++;
  1745. conf->num_bss--;
  1746. conf->bss[0] = NULL;
  1747. hostapd_config_free(conf);
  1748. } else {
  1749. /* Add a new iface with the first BSS */
  1750. new_iface = iface = hostapd_init(interfaces, config_fname);
  1751. if (!iface)
  1752. return NULL;
  1753. os_strlcpy(iface->phy, phy, sizeof(iface->phy));
  1754. iface->interfaces = interfaces;
  1755. bss_idx = 0;
  1756. }
  1757. for (k = 0; k < debug; k++) {
  1758. if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
  1759. iface->bss[bss_idx]->conf->logger_stdout_level--;
  1760. }
  1761. if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
  1762. !hostapd_drv_none(iface->bss[bss_idx])) {
  1763. wpa_printf(MSG_ERROR, "Interface name not specified in %s",
  1764. config_fname);
  1765. if (new_iface)
  1766. hostapd_interface_deinit_free(new_iface);
  1767. return NULL;
  1768. }
  1769. return iface;
  1770. }
  1771. void hostapd_interface_deinit_free(struct hostapd_iface *iface)
  1772. {
  1773. const struct wpa_driver_ops *driver;
  1774. void *drv_priv;
  1775. wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
  1776. if (iface == NULL)
  1777. return;
  1778. wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
  1779. __func__, (unsigned int) iface->num_bss,
  1780. (unsigned int) iface->conf->num_bss);
  1781. driver = iface->bss[0]->driver;
  1782. drv_priv = iface->bss[0]->drv_priv;
  1783. hostapd_interface_deinit(iface);
  1784. wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
  1785. __func__, driver, drv_priv);
  1786. if (driver && driver->hapd_deinit && drv_priv) {
  1787. driver->hapd_deinit(drv_priv);
  1788. iface->bss[0]->drv_priv = NULL;
  1789. }
  1790. hostapd_interface_free(iface);
  1791. }
  1792. static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
  1793. void *drv_priv,
  1794. struct hostapd_iface *hapd_iface)
  1795. {
  1796. size_t j;
  1797. wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
  1798. __func__, driver, drv_priv);
  1799. if (driver && driver->hapd_deinit && drv_priv) {
  1800. driver->hapd_deinit(drv_priv);
  1801. for (j = 0; j < hapd_iface->num_bss; j++) {
  1802. wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
  1803. __func__, (int) j,
  1804. hapd_iface->bss[j]->drv_priv);
  1805. if (hapd_iface->bss[j]->drv_priv == drv_priv)
  1806. hapd_iface->bss[j]->drv_priv = NULL;
  1807. }
  1808. }
  1809. }
  1810. int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
  1811. {
  1812. size_t j;
  1813. if (hapd_iface->bss[0]->drv_priv != NULL) {
  1814. wpa_printf(MSG_ERROR, "Interface %s already enabled",
  1815. hapd_iface->conf->bss[0]->iface);
  1816. return -1;
  1817. }
  1818. wpa_printf(MSG_DEBUG, "Enable interface %s",
  1819. hapd_iface->conf->bss[0]->iface);
  1820. for (j = 0; j < hapd_iface->num_bss; j++)
  1821. hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
  1822. if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
  1823. wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
  1824. return -1;
  1825. }
  1826. if (hapd_iface->interfaces == NULL ||
  1827. hapd_iface->interfaces->driver_init == NULL ||
  1828. hapd_iface->interfaces->driver_init(hapd_iface))
  1829. return -1;
  1830. if (hostapd_setup_interface(hapd_iface)) {
  1831. hostapd_deinit_driver(hapd_iface->bss[0]->driver,
  1832. hapd_iface->bss[0]->drv_priv,
  1833. hapd_iface);
  1834. return -1;
  1835. }
  1836. return 0;
  1837. }
  1838. int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
  1839. {
  1840. size_t j;
  1841. wpa_printf(MSG_DEBUG, "Reload interface %s",
  1842. hapd_iface->conf->bss[0]->iface);
  1843. for (j = 0; j < hapd_iface->num_bss; j++)
  1844. hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
  1845. if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
  1846. wpa_printf(MSG_ERROR, "Updated configuration is invalid");
  1847. return -1;
  1848. }
  1849. hostapd_clear_old(hapd_iface);
  1850. for (j = 0; j < hapd_iface->num_bss; j++)
  1851. hostapd_reload_bss(hapd_iface->bss[j]);
  1852. return 0;
  1853. }
  1854. int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
  1855. {
  1856. size_t j;
  1857. const struct wpa_driver_ops *driver;
  1858. void *drv_priv;
  1859. if (hapd_iface == NULL)
  1860. return -1;
  1861. if (hapd_iface->bss[0]->drv_priv == NULL) {
  1862. wpa_printf(MSG_INFO, "Interface %s already disabled",
  1863. hapd_iface->conf->bss[0]->iface);
  1864. return -1;
  1865. }
  1866. wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
  1867. driver = hapd_iface->bss[0]->driver;
  1868. drv_priv = hapd_iface->bss[0]->drv_priv;
  1869. hapd_iface->driver_ap_teardown =
  1870. !!(hapd_iface->drv_flags &
  1871. WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
  1872. /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
  1873. for (j = 0; j < hapd_iface->num_bss; j++) {
  1874. struct hostapd_data *hapd = hapd_iface->bss[j];
  1875. hostapd_bss_deinit_no_free(hapd);
  1876. hostapd_free_hapd_data(hapd);
  1877. }
  1878. hostapd_deinit_driver(driver, drv_priv, hapd_iface);
  1879. /* From hostapd_cleanup_iface: These were initialized in
  1880. * hostapd_setup_interface and hostapd_setup_interface_complete
  1881. */
  1882. hostapd_cleanup_iface_partial(hapd_iface);
  1883. wpa_printf(MSG_DEBUG, "Interface %s disabled",
  1884. hapd_iface->bss[0]->conf->iface);
  1885. hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
  1886. return 0;
  1887. }
  1888. static struct hostapd_iface *
  1889. hostapd_iface_alloc(struct hapd_interfaces *interfaces)
  1890. {
  1891. struct hostapd_iface **iface, *hapd_iface;
  1892. iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
  1893. sizeof(struct hostapd_iface *));
  1894. if (iface == NULL)
  1895. return NULL;
  1896. interfaces->iface = iface;
  1897. hapd_iface = interfaces->iface[interfaces->count] =
  1898. os_zalloc(sizeof(*hapd_iface));
  1899. if (hapd_iface == NULL) {
  1900. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
  1901. "the interface", __func__);
  1902. return NULL;
  1903. }
  1904. interfaces->count++;
  1905. hapd_iface->interfaces = interfaces;
  1906. return hapd_iface;
  1907. }
  1908. static struct hostapd_config *
  1909. hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
  1910. const char *ctrl_iface, const char *driver)
  1911. {
  1912. struct hostapd_bss_config *bss;
  1913. struct hostapd_config *conf;
  1914. /* Allocates memory for bss and conf */
  1915. conf = hostapd_config_defaults();
  1916. if (conf == NULL) {
  1917. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
  1918. "configuration", __func__);
  1919. return NULL;
  1920. }
  1921. if (driver) {
  1922. int j;
  1923. for (j = 0; wpa_drivers[j]; j++) {
  1924. if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
  1925. conf->driver = wpa_drivers[j];
  1926. goto skip;
  1927. }
  1928. }
  1929. wpa_printf(MSG_ERROR,
  1930. "Invalid/unknown driver '%s' - registering the default driver",
  1931. driver);
  1932. }
  1933. conf->driver = wpa_drivers[0];
  1934. if (conf->driver == NULL) {
  1935. wpa_printf(MSG_ERROR, "No driver wrappers registered!");
  1936. hostapd_config_free(conf);
  1937. return NULL;
  1938. }
  1939. skip:
  1940. bss = conf->last_bss = conf->bss[0];
  1941. os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
  1942. bss->ctrl_interface = os_strdup(ctrl_iface);
  1943. if (bss->ctrl_interface == NULL) {
  1944. hostapd_config_free(conf);
  1945. return NULL;
  1946. }
  1947. /* Reading configuration file skipped, will be done in SET!
  1948. * From reading the configuration till the end has to be done in
  1949. * SET
  1950. */
  1951. return conf;
  1952. }
  1953. static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
  1954. struct hostapd_config *conf)
  1955. {
  1956. size_t i;
  1957. struct hostapd_data *hapd;
  1958. hapd_iface->bss = os_calloc(conf->num_bss,
  1959. sizeof(struct hostapd_data *));
  1960. if (hapd_iface->bss == NULL)
  1961. return -1;
  1962. for (i = 0; i < conf->num_bss; i++) {
  1963. hapd = hapd_iface->bss[i] =
  1964. hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
  1965. if (hapd == NULL) {
  1966. while (i > 0) {
  1967. i--;
  1968. os_free(hapd_iface->bss[i]);
  1969. hapd_iface->bss[i] = NULL;
  1970. }
  1971. os_free(hapd_iface->bss);
  1972. hapd_iface->bss = NULL;
  1973. return -1;
  1974. }
  1975. hapd->msg_ctx = hapd;
  1976. }
  1977. hapd_iface->conf = conf;
  1978. hapd_iface->num_bss = conf->num_bss;
  1979. return 0;
  1980. }
  1981. int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
  1982. {
  1983. struct hostapd_config *conf = NULL;
  1984. struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
  1985. struct hostapd_data *hapd;
  1986. char *ptr;
  1987. size_t i, j;
  1988. const char *conf_file = NULL, *phy_name = NULL;
  1989. if (os_strncmp(buf, "bss_config=", 11) == 0) {
  1990. char *pos;
  1991. phy_name = buf + 11;
  1992. pos = os_strchr(phy_name, ':');
  1993. if (!pos)
  1994. return -1;
  1995. *pos++ = '\0';
  1996. conf_file = pos;
  1997. if (!os_strlen(conf_file))
  1998. return -1;
  1999. hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
  2000. conf_file, 0);
  2001. if (!hapd_iface)
  2002. return -1;
  2003. for (j = 0; j < interfaces->count; j++) {
  2004. if (interfaces->iface[j] == hapd_iface)
  2005. break;
  2006. }
  2007. if (j == interfaces->count) {
  2008. struct hostapd_iface **tmp;
  2009. tmp = os_realloc_array(interfaces->iface,
  2010. interfaces->count + 1,
  2011. sizeof(struct hostapd_iface *));
  2012. if (!tmp) {
  2013. hostapd_interface_deinit_free(hapd_iface);
  2014. return -1;
  2015. }
  2016. interfaces->iface = tmp;
  2017. interfaces->iface[interfaces->count++] = hapd_iface;
  2018. new_iface = hapd_iface;
  2019. }
  2020. if (new_iface) {
  2021. if (interfaces->driver_init(hapd_iface))
  2022. goto fail;
  2023. if (hostapd_setup_interface(hapd_iface)) {
  2024. hostapd_deinit_driver(
  2025. hapd_iface->bss[0]->driver,
  2026. hapd_iface->bss[0]->drv_priv,
  2027. hapd_iface);
  2028. goto fail;
  2029. }
  2030. } else {
  2031. /* Assign new BSS with bss[0]'s driver info */
  2032. hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
  2033. hapd->driver = hapd_iface->bss[0]->driver;
  2034. hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
  2035. os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
  2036. ETH_ALEN);
  2037. if (start_ctrl_iface_bss(hapd) < 0 ||
  2038. (hapd_iface->state == HAPD_IFACE_ENABLED &&
  2039. hostapd_setup_bss(hapd, -1))) {
  2040. hostapd_cleanup(hapd);
  2041. hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
  2042. hapd_iface->conf->num_bss--;
  2043. hapd_iface->num_bss--;
  2044. wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
  2045. __func__, hapd, hapd->conf->iface);
  2046. hostapd_config_free_bss(hapd->conf);
  2047. hapd->conf = NULL;
  2048. os_free(hapd);
  2049. return -1;
  2050. }
  2051. }
  2052. return 0;
  2053. }
  2054. ptr = os_strchr(buf, ' ');
  2055. if (ptr == NULL)
  2056. return -1;
  2057. *ptr++ = '\0';
  2058. if (os_strncmp(ptr, "config=", 7) == 0)
  2059. conf_file = ptr + 7;
  2060. for (i = 0; i < interfaces->count; i++) {
  2061. if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
  2062. buf)) {
  2063. wpa_printf(MSG_INFO, "Cannot add interface - it "
  2064. "already exists");
  2065. return -1;
  2066. }
  2067. }
  2068. hapd_iface = hostapd_iface_alloc(interfaces);
  2069. if (hapd_iface == NULL) {
  2070. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
  2071. "for interface", __func__);
  2072. goto fail;
  2073. }
  2074. new_iface = hapd_iface;
  2075. if (conf_file && interfaces->config_read_cb) {
  2076. conf = interfaces->config_read_cb(conf_file);
  2077. if (conf && conf->bss)
  2078. os_strlcpy(conf->bss[0]->iface, buf,
  2079. sizeof(conf->bss[0]->iface));
  2080. } else {
  2081. char *driver = os_strchr(ptr, ' ');
  2082. if (driver)
  2083. *driver++ = '\0';
  2084. conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
  2085. }
  2086. if (conf == NULL || conf->bss == NULL) {
  2087. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
  2088. "for configuration", __func__);
  2089. goto fail;
  2090. }
  2091. if (hostapd_data_alloc(hapd_iface, conf) < 0) {
  2092. wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
  2093. "for hostapd", __func__);
  2094. goto fail;
  2095. }
  2096. conf = NULL;
  2097. if (start_ctrl_iface(hapd_iface) < 0)
  2098. goto fail;
  2099. wpa_printf(MSG_INFO, "Add interface '%s'",
  2100. hapd_iface->conf->bss[0]->iface);
  2101. return 0;
  2102. fail:
  2103. if (conf)
  2104. hostapd_config_free(conf);
  2105. if (hapd_iface) {
  2106. if (hapd_iface->bss) {
  2107. for (i = 0; i < hapd_iface->num_bss; i++) {
  2108. hapd = hapd_iface->bss[i];
  2109. if (!hapd)
  2110. continue;
  2111. if (hapd_iface->interfaces &&
  2112. hapd_iface->interfaces->ctrl_iface_deinit)
  2113. hapd_iface->interfaces->
  2114. ctrl_iface_deinit(hapd);
  2115. wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
  2116. __func__, hapd_iface->bss[i],
  2117. hapd->conf->iface);
  2118. hostapd_cleanup(hapd);
  2119. os_free(hapd);
  2120. hapd_iface->bss[i] = NULL;
  2121. }
  2122. os_free(hapd_iface->bss);
  2123. hapd_iface->bss = NULL;
  2124. }
  2125. if (new_iface) {
  2126. interfaces->count--;
  2127. interfaces->iface[interfaces->count] = NULL;
  2128. }
  2129. hostapd_cleanup_iface(hapd_iface);
  2130. }
  2131. return -1;
  2132. }
  2133. static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
  2134. {
  2135. size_t i;
  2136. wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
  2137. /* Remove hostapd_data only if it has already been initialized */
  2138. if (idx < iface->num_bss) {
  2139. struct hostapd_data *hapd = iface->bss[idx];
  2140. hostapd_bss_deinit(hapd);
  2141. wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
  2142. __func__, hapd, hapd->conf->iface);
  2143. hostapd_config_free_bss(hapd->conf);
  2144. hapd->conf = NULL;
  2145. os_free(hapd);
  2146. iface->num_bss--;
  2147. for (i = idx; i < iface->num_bss; i++)
  2148. iface->bss[i] = iface->bss[i + 1];
  2149. } else {
  2150. hostapd_config_free_bss(iface->conf->bss[idx]);
  2151. iface->conf->bss[idx] = NULL;
  2152. }
  2153. iface->conf->num_bss--;
  2154. for (i = idx; i < iface->conf->num_bss; i++)
  2155. iface->conf->bss[i] = iface->conf->bss[i + 1];
  2156. return 0;
  2157. }
  2158. int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
  2159. {
  2160. struct hostapd_iface *hapd_iface;
  2161. size_t i, j, k = 0;
  2162. for (i = 0; i < interfaces->count; i++) {
  2163. hapd_iface = interfaces->iface[i];
  2164. if (hapd_iface == NULL)
  2165. return -1;
  2166. if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
  2167. wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
  2168. hapd_iface->driver_ap_teardown =
  2169. !!(hapd_iface->drv_flags &
  2170. WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
  2171. hostapd_interface_deinit_free(hapd_iface);
  2172. k = i;
  2173. while (k < (interfaces->count - 1)) {
  2174. interfaces->iface[k] =
  2175. interfaces->iface[k + 1];
  2176. k++;
  2177. }
  2178. interfaces->count--;
  2179. return 0;
  2180. }
  2181. for (j = 0; j < hapd_iface->conf->num_bss; j++) {
  2182. if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
  2183. hapd_iface->driver_ap_teardown =
  2184. !(hapd_iface->drv_flags &
  2185. WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
  2186. return hostapd_remove_bss(hapd_iface, j);
  2187. }
  2188. }
  2189. }
  2190. return -1;
  2191. }
  2192. /**
  2193. * hostapd_new_assoc_sta - Notify that a new station associated with the AP
  2194. * @hapd: Pointer to BSS data
  2195. * @sta: Pointer to the associated STA data
  2196. * @reassoc: 1 to indicate this was a re-association; 0 = first association
  2197. *
  2198. * This function will be called whenever a station associates with the AP. It
  2199. * can be called from ieee802_11.c for drivers that export MLME to hostapd and
  2200. * from drv_callbacks.c based on driver events for drivers that take care of
  2201. * management frames (IEEE 802.11 authentication and association) internally.
  2202. */
  2203. void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
  2204. int reassoc)
  2205. {
  2206. if (hapd->tkip_countermeasures) {
  2207. hostapd_drv_sta_deauth(hapd, sta->addr,
  2208. WLAN_REASON_MICHAEL_MIC_FAILURE);
  2209. return;
  2210. }
  2211. hostapd_prune_associations(hapd, sta->addr);
  2212. ap_sta_clear_disconnect_timeouts(hapd, sta);
  2213. /* IEEE 802.11F (IAPP) */
  2214. if (hapd->conf->ieee802_11f)
  2215. iapp_new_station(hapd->iapp, sta);
  2216. #ifdef CONFIG_P2P
  2217. if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
  2218. sta->no_p2p_set = 1;
  2219. hapd->num_sta_no_p2p++;
  2220. if (hapd->num_sta_no_p2p == 1)
  2221. hostapd_p2p_non_p2p_sta_connected(hapd);
  2222. }
  2223. #endif /* CONFIG_P2P */
  2224. /* Start accounting here, if IEEE 802.1X and WPA are not used.
  2225. * IEEE 802.1X/WPA code will start accounting after the station has
  2226. * been authorized. */
  2227. if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
  2228. ap_sta_set_authorized(hapd, sta, 1);
  2229. os_get_reltime(&sta->connected_time);
  2230. accounting_sta_start(hapd, sta);
  2231. }
  2232. /* Start IEEE 802.1X authentication process for new stations */
  2233. ieee802_1x_new_station(hapd, sta);
  2234. if (reassoc) {
  2235. if (sta->auth_alg != WLAN_AUTH_FT &&
  2236. !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
  2237. wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
  2238. } else
  2239. wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
  2240. if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
  2241. wpa_printf(MSG_DEBUG,
  2242. "%s: %s: reschedule ap_handle_timer timeout for "
  2243. MACSTR " (%d seconds - ap_max_inactivity)",
  2244. hapd->conf->iface, __func__, MAC2STR(sta->addr),
  2245. hapd->conf->ap_max_inactivity);
  2246. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  2247. eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
  2248. ap_handle_timer, hapd, sta);
  2249. }
  2250. }
  2251. const char * hostapd_state_text(enum hostapd_iface_state s)
  2252. {
  2253. switch (s) {
  2254. case HAPD_IFACE_UNINITIALIZED:
  2255. return "UNINITIALIZED";
  2256. case HAPD_IFACE_DISABLED:
  2257. return "DISABLED";
  2258. case HAPD_IFACE_COUNTRY_UPDATE:
  2259. return "COUNTRY_UPDATE";
  2260. case HAPD_IFACE_ACS:
  2261. return "ACS";
  2262. case HAPD_IFACE_HT_SCAN:
  2263. return "HT_SCAN";
  2264. case HAPD_IFACE_DFS:
  2265. return "DFS";
  2266. case HAPD_IFACE_ENABLED:
  2267. return "ENABLED";
  2268. }
  2269. return "UNKNOWN";
  2270. }
  2271. void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
  2272. {
  2273. wpa_printf(MSG_INFO, "%s: interface state %s->%s",
  2274. iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
  2275. hostapd_state_text(s));
  2276. iface->state = s;
  2277. }
  2278. int hostapd_csa_in_progress(struct hostapd_iface *iface)
  2279. {
  2280. unsigned int i;
  2281. for (i = 0; i < iface->num_bss; i++)
  2282. if (iface->bss[i]->csa_in_progress)
  2283. return 1;
  2284. return 0;
  2285. }
  2286. #ifdef NEED_AP_MLME
  2287. static void free_beacon_data(struct beacon_data *beacon)
  2288. {
  2289. os_free(beacon->head);
  2290. beacon->head = NULL;
  2291. os_free(beacon->tail);
  2292. beacon->tail = NULL;
  2293. os_free(beacon->probe_resp);
  2294. beacon->probe_resp = NULL;
  2295. os_free(beacon->beacon_ies);
  2296. beacon->beacon_ies = NULL;
  2297. os_free(beacon->proberesp_ies);
  2298. beacon->proberesp_ies = NULL;
  2299. os_free(beacon->assocresp_ies);
  2300. beacon->assocresp_ies = NULL;
  2301. }
  2302. static int hostapd_build_beacon_data(struct hostapd_data *hapd,
  2303. struct beacon_data *beacon)
  2304. {
  2305. struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
  2306. struct wpa_driver_ap_params params;
  2307. int ret;
  2308. os_memset(beacon, 0, sizeof(*beacon));
  2309. ret = ieee802_11_build_ap_params(hapd, &params);
  2310. if (ret < 0)
  2311. return ret;
  2312. ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
  2313. &proberesp_extra,
  2314. &assocresp_extra);
  2315. if (ret)
  2316. goto free_ap_params;
  2317. ret = -1;
  2318. beacon->head = os_malloc(params.head_len);
  2319. if (!beacon->head)
  2320. goto free_ap_extra_ies;
  2321. os_memcpy(beacon->head, params.head, params.head_len);
  2322. beacon->head_len = params.head_len;
  2323. beacon->tail = os_malloc(params.tail_len);
  2324. if (!beacon->tail)
  2325. goto free_beacon;
  2326. os_memcpy(beacon->tail, params.tail, params.tail_len);
  2327. beacon->tail_len = params.tail_len;
  2328. if (params.proberesp != NULL) {
  2329. beacon->probe_resp = os_malloc(params.proberesp_len);
  2330. if (!beacon->probe_resp)
  2331. goto free_beacon;
  2332. os_memcpy(beacon->probe_resp, params.proberesp,
  2333. params.proberesp_len);
  2334. beacon->probe_resp_len = params.proberesp_len;
  2335. }
  2336. /* copy the extra ies */
  2337. if (beacon_extra) {
  2338. beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
  2339. if (!beacon->beacon_ies)
  2340. goto free_beacon;
  2341. os_memcpy(beacon->beacon_ies,
  2342. beacon_extra->buf, wpabuf_len(beacon_extra));
  2343. beacon->beacon_ies_len = wpabuf_len(beacon_extra);
  2344. }
  2345. if (proberesp_extra) {
  2346. beacon->proberesp_ies =
  2347. os_malloc(wpabuf_len(proberesp_extra));
  2348. if (!beacon->proberesp_ies)
  2349. goto free_beacon;
  2350. os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
  2351. wpabuf_len(proberesp_extra));
  2352. beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
  2353. }
  2354. if (assocresp_extra) {
  2355. beacon->assocresp_ies =
  2356. os_malloc(wpabuf_len(assocresp_extra));
  2357. if (!beacon->assocresp_ies)
  2358. goto free_beacon;
  2359. os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
  2360. wpabuf_len(assocresp_extra));
  2361. beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
  2362. }
  2363. ret = 0;
  2364. free_beacon:
  2365. /* if the function fails, the caller should not free beacon data */
  2366. if (ret)
  2367. free_beacon_data(beacon);
  2368. free_ap_extra_ies:
  2369. hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
  2370. assocresp_extra);
  2371. free_ap_params:
  2372. ieee802_11_free_ap_params(&params);
  2373. return ret;
  2374. }
  2375. /*
  2376. * TODO: This flow currently supports only changing channel and width within
  2377. * the same hw_mode. Any other changes to MAC parameters or provided settings
  2378. * are not supported.
  2379. */
  2380. static int hostapd_change_config_freq(struct hostapd_data *hapd,
  2381. struct hostapd_config *conf,
  2382. struct hostapd_freq_params *params,
  2383. struct hostapd_freq_params *old_params)
  2384. {
  2385. int channel;
  2386. if (!params->channel) {
  2387. /* check if the new channel is supported by hw */
  2388. params->channel = hostapd_hw_get_channel(hapd, params->freq);
  2389. }
  2390. channel = params->channel;
  2391. if (!channel)
  2392. return -1;
  2393. /* if a pointer to old_params is provided we save previous state */
  2394. if (old_params &&
  2395. hostapd_set_freq_params(old_params, conf->hw_mode,
  2396. hostapd_hw_get_freq(hapd, conf->channel),
  2397. conf->channel, conf->ieee80211n,
  2398. conf->ieee80211ac,
  2399. conf->secondary_channel,
  2400. conf->vht_oper_chwidth,
  2401. conf->vht_oper_centr_freq_seg0_idx,
  2402. conf->vht_oper_centr_freq_seg1_idx,
  2403. conf->vht_capab))
  2404. return -1;
  2405. switch (params->bandwidth) {
  2406. case 0:
  2407. case 20:
  2408. case 40:
  2409. conf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
  2410. break;
  2411. case 80:
  2412. if (params->center_freq2)
  2413. conf->vht_oper_chwidth = VHT_CHANWIDTH_80P80MHZ;
  2414. else
  2415. conf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
  2416. break;
  2417. case 160:
  2418. conf->vht_oper_chwidth = VHT_CHANWIDTH_160MHZ;
  2419. break;
  2420. default:
  2421. return -1;
  2422. }
  2423. conf->channel = channel;
  2424. conf->ieee80211n = params->ht_enabled;
  2425. conf->secondary_channel = params->sec_channel_offset;
  2426. ieee80211_freq_to_chan(params->center_freq1,
  2427. &conf->vht_oper_centr_freq_seg0_idx);
  2428. ieee80211_freq_to_chan(params->center_freq2,
  2429. &conf->vht_oper_centr_freq_seg1_idx);
  2430. /* TODO: maybe call here hostapd_config_check here? */
  2431. return 0;
  2432. }
  2433. static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
  2434. struct csa_settings *settings)
  2435. {
  2436. struct hostapd_iface *iface = hapd->iface;
  2437. struct hostapd_freq_params old_freq;
  2438. int ret;
  2439. u8 chan, vht_bandwidth;
  2440. os_memset(&old_freq, 0, sizeof(old_freq));
  2441. if (!iface || !iface->freq || hapd->csa_in_progress)
  2442. return -1;
  2443. switch (settings->freq_params.bandwidth) {
  2444. case 80:
  2445. if (settings->freq_params.center_freq2)
  2446. vht_bandwidth = VHT_CHANWIDTH_80P80MHZ;
  2447. else
  2448. vht_bandwidth = VHT_CHANWIDTH_80MHZ;
  2449. break;
  2450. case 160:
  2451. vht_bandwidth = VHT_CHANWIDTH_160MHZ;
  2452. break;
  2453. default:
  2454. vht_bandwidth = VHT_CHANWIDTH_USE_HT;
  2455. break;
  2456. }
  2457. if (ieee80211_freq_to_channel_ext(
  2458. settings->freq_params.freq,
  2459. settings->freq_params.sec_channel_offset,
  2460. vht_bandwidth,
  2461. &hapd->iface->cs_oper_class,
  2462. &chan) == NUM_HOSTAPD_MODES) {
  2463. wpa_printf(MSG_DEBUG,
  2464. "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d)",
  2465. settings->freq_params.freq,
  2466. settings->freq_params.sec_channel_offset,
  2467. settings->freq_params.vht_enabled);
  2468. return -1;
  2469. }
  2470. settings->freq_params.channel = chan;
  2471. ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
  2472. &settings->freq_params,
  2473. &old_freq);
  2474. if (ret)
  2475. return ret;
  2476. ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
  2477. /* change back the configuration */
  2478. hostapd_change_config_freq(iface->bss[0], iface->conf,
  2479. &old_freq, NULL);
  2480. if (ret)
  2481. return ret;
  2482. /* set channel switch parameters for csa ie */
  2483. hapd->cs_freq_params = settings->freq_params;
  2484. hapd->cs_count = settings->cs_count;
  2485. hapd->cs_block_tx = settings->block_tx;
  2486. ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
  2487. if (ret) {
  2488. free_beacon_data(&settings->beacon_after);
  2489. return ret;
  2490. }
  2491. settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
  2492. settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
  2493. settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
  2494. settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
  2495. return 0;
  2496. }
  2497. void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
  2498. {
  2499. os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
  2500. hapd->cs_count = 0;
  2501. hapd->cs_block_tx = 0;
  2502. hapd->cs_c_off_beacon = 0;
  2503. hapd->cs_c_off_proberesp = 0;
  2504. hapd->csa_in_progress = 0;
  2505. hapd->cs_c_off_ecsa_beacon = 0;
  2506. hapd->cs_c_off_ecsa_proberesp = 0;
  2507. }
  2508. int hostapd_switch_channel(struct hostapd_data *hapd,
  2509. struct csa_settings *settings)
  2510. {
  2511. int ret;
  2512. if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
  2513. wpa_printf(MSG_INFO, "CSA is not supported");
  2514. return -1;
  2515. }
  2516. ret = hostapd_fill_csa_settings(hapd, settings);
  2517. if (ret)
  2518. return ret;
  2519. ret = hostapd_drv_switch_channel(hapd, settings);
  2520. free_beacon_data(&settings->beacon_csa);
  2521. free_beacon_data(&settings->beacon_after);
  2522. if (ret) {
  2523. /* if we failed, clean cs parameters */
  2524. hostapd_cleanup_cs_params(hapd);
  2525. return ret;
  2526. }
  2527. hapd->csa_in_progress = 1;
  2528. return 0;
  2529. }
  2530. void
  2531. hostapd_switch_channel_fallback(struct hostapd_iface *iface,
  2532. const struct hostapd_freq_params *freq_params)
  2533. {
  2534. int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
  2535. unsigned int i;
  2536. wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
  2537. if (freq_params->center_freq1)
  2538. vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
  2539. if (freq_params->center_freq2)
  2540. vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
  2541. switch (freq_params->bandwidth) {
  2542. case 0:
  2543. case 20:
  2544. case 40:
  2545. vht_bw = VHT_CHANWIDTH_USE_HT;
  2546. break;
  2547. case 80:
  2548. if (freq_params->center_freq2)
  2549. vht_bw = VHT_CHANWIDTH_80P80MHZ;
  2550. else
  2551. vht_bw = VHT_CHANWIDTH_80MHZ;
  2552. break;
  2553. case 160:
  2554. vht_bw = VHT_CHANWIDTH_160MHZ;
  2555. break;
  2556. default:
  2557. wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
  2558. freq_params->bandwidth);
  2559. break;
  2560. }
  2561. iface->freq = freq_params->freq;
  2562. iface->conf->channel = freq_params->channel;
  2563. iface->conf->secondary_channel = freq_params->sec_channel_offset;
  2564. iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
  2565. iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
  2566. iface->conf->vht_oper_chwidth = vht_bw;
  2567. iface->conf->ieee80211n = freq_params->ht_enabled;
  2568. iface->conf->ieee80211ac = freq_params->vht_enabled;
  2569. /*
  2570. * cs_params must not be cleared earlier because the freq_params
  2571. * argument may actually point to one of these.
  2572. */
  2573. for (i = 0; i < iface->num_bss; i++)
  2574. hostapd_cleanup_cs_params(iface->bss[i]);
  2575. hostapd_disable_iface(iface);
  2576. hostapd_enable_iface(iface);
  2577. }
  2578. struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
  2579. const char *ifname)
  2580. {
  2581. size_t i, j;
  2582. for (i = 0; i < interfaces->count; i++) {
  2583. struct hostapd_iface *iface = interfaces->iface[i];
  2584. for (j = 0; j < iface->num_bss; j++) {
  2585. struct hostapd_data *hapd = iface->bss[j];
  2586. if (os_strcmp(ifname, hapd->conf->iface) == 0)
  2587. return hapd;
  2588. }
  2589. }
  2590. return NULL;
  2591. }
  2592. #endif /* NEED_AP_MLME */
  2593. void hostapd_periodic_iface(struct hostapd_iface *iface)
  2594. {
  2595. size_t i;
  2596. ap_list_timer(iface);
  2597. for (i = 0; i < iface->num_bss; i++) {
  2598. struct hostapd_data *hapd = iface->bss[i];
  2599. if (!hapd->started)
  2600. continue;
  2601. #ifndef CONFIG_NO_RADIUS
  2602. hostapd_acl_expire(hapd);
  2603. #endif /* CONFIG_NO_RADIUS */
  2604. }
  2605. }