hostapd.c 83 KB

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