hostapd.c 73 KB

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