iface.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Interface handling
  4. *
  5. * Copyright 2002-2005, Instant802 Networks, Inc.
  6. * Copyright 2005-2006, Devicescape Software, Inc.
  7. * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
  8. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  9. * Copyright 2013-2014 Intel Mobile Communications GmbH
  10. * Copyright (c) 2016 Intel Deutschland GmbH
  11. * Copyright (C) 2018-2025 Intel Corporation
  12. */
  13. #include <linux/slab.h>
  14. #include <linux/kernel.h>
  15. #include <linux/if_arp.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/rtnetlink.h>
  18. #include <linux/kcov.h>
  19. #include <net/mac80211.h>
  20. #include <net/ieee80211_radiotap.h>
  21. #include "ieee80211_i.h"
  22. #include "sta_info.h"
  23. #include "debugfs_netdev.h"
  24. #include "mesh.h"
  25. #include "led.h"
  26. #include "driver-ops.h"
  27. #include "wme.h"
  28. #include "rate.h"
  29. /**
  30. * DOC: Interface list locking
  31. *
  32. * The interface list in each struct ieee80211_local is protected
  33. * three-fold:
  34. *
  35. * (1) modifications may only be done under the RTNL *and* wiphy mutex
  36. * *and* iflist_mtx
  37. * (2) modifications are done in an RCU manner so atomic readers
  38. * can traverse the list in RCU-safe blocks.
  39. *
  40. * As a consequence, reads (traversals) of the list can be protected
  41. * by either the RTNL, the wiphy mutex, the iflist_mtx or RCU.
  42. */
  43. static void ieee80211_iface_work(struct wiphy *wiphy, struct wiphy_work *work);
  44. bool __ieee80211_recalc_txpower(struct ieee80211_sub_if_data *sdata)
  45. {
  46. struct ieee80211_chanctx_conf *chanctx_conf;
  47. int power;
  48. rcu_read_lock();
  49. chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
  50. if (!chanctx_conf) {
  51. rcu_read_unlock();
  52. return false;
  53. }
  54. power = ieee80211_chandef_max_power(&chanctx_conf->def);
  55. rcu_read_unlock();
  56. if (sdata->deflink.user_power_level != IEEE80211_UNSET_POWER_LEVEL)
  57. power = min(power, sdata->deflink.user_power_level);
  58. if (sdata->deflink.ap_power_level != IEEE80211_UNSET_POWER_LEVEL)
  59. power = min(power, sdata->deflink.ap_power_level);
  60. if (power != sdata->vif.bss_conf.txpower) {
  61. sdata->vif.bss_conf.txpower = power;
  62. ieee80211_hw_config(sdata->local, 0);
  63. return true;
  64. }
  65. return false;
  66. }
  67. void ieee80211_recalc_txpower(struct ieee80211_sub_if_data *sdata,
  68. bool update_bss)
  69. {
  70. if (__ieee80211_recalc_txpower(sdata) ||
  71. (update_bss && ieee80211_sdata_running(sdata)))
  72. ieee80211_link_info_change_notify(sdata, &sdata->deflink,
  73. BSS_CHANGED_TXPOWER);
  74. }
  75. static u32 __ieee80211_idle_off(struct ieee80211_local *local)
  76. {
  77. if (!(local->hw.conf.flags & IEEE80211_CONF_IDLE))
  78. return 0;
  79. local->hw.conf.flags &= ~IEEE80211_CONF_IDLE;
  80. return IEEE80211_CONF_CHANGE_IDLE;
  81. }
  82. static u32 __ieee80211_idle_on(struct ieee80211_local *local)
  83. {
  84. if (local->hw.conf.flags & IEEE80211_CONF_IDLE)
  85. return 0;
  86. ieee80211_flush_queues(local, NULL, false);
  87. local->hw.conf.flags |= IEEE80211_CONF_IDLE;
  88. return IEEE80211_CONF_CHANGE_IDLE;
  89. }
  90. static u32 __ieee80211_recalc_idle(struct ieee80211_local *local,
  91. bool force_active)
  92. {
  93. bool working, scanning, active;
  94. unsigned int led_trig_start = 0, led_trig_stop = 0;
  95. lockdep_assert_wiphy(local->hw.wiphy);
  96. active = force_active ||
  97. !list_empty(&local->chanctx_list) ||
  98. local->monitors;
  99. working = !local->ops->remain_on_channel &&
  100. !list_empty(&local->roc_list);
  101. scanning = test_bit(SCAN_SW_SCANNING, &local->scanning) ||
  102. test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
  103. if (working || scanning)
  104. led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_WORK;
  105. else
  106. led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_WORK;
  107. if (active)
  108. led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED;
  109. else
  110. led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED;
  111. ieee80211_mod_tpt_led_trig(local, led_trig_start, led_trig_stop);
  112. if (working || scanning || active)
  113. return __ieee80211_idle_off(local);
  114. return __ieee80211_idle_on(local);
  115. }
  116. u32 ieee80211_idle_off(struct ieee80211_local *local)
  117. {
  118. return __ieee80211_recalc_idle(local, true);
  119. }
  120. void ieee80211_recalc_idle(struct ieee80211_local *local)
  121. {
  122. u32 change = __ieee80211_recalc_idle(local, false);
  123. if (change)
  124. ieee80211_hw_config(local, change);
  125. }
  126. static int ieee80211_verify_mac(struct ieee80211_sub_if_data *sdata, u8 *addr,
  127. bool check_dup)
  128. {
  129. struct ieee80211_local *local = sdata->local;
  130. struct ieee80211_sub_if_data *iter;
  131. u64 new, mask, tmp;
  132. u8 *m;
  133. int ret = 0;
  134. lockdep_assert_wiphy(local->hw.wiphy);
  135. if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
  136. return 0;
  137. m = addr;
  138. new = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  139. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  140. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  141. m = local->hw.wiphy->addr_mask;
  142. mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  143. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  144. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  145. if (!check_dup)
  146. return ret;
  147. list_for_each_entry(iter, &local->interfaces, list) {
  148. if (iter == sdata)
  149. continue;
  150. if (iter->vif.type == NL80211_IFTYPE_MONITOR &&
  151. !(iter->u.mntr.flags & MONITOR_FLAG_ACTIVE))
  152. continue;
  153. m = iter->vif.addr;
  154. tmp = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  155. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  156. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  157. if ((new & ~mask) != (tmp & ~mask)) {
  158. ret = -EINVAL;
  159. break;
  160. }
  161. }
  162. return ret;
  163. }
  164. static int ieee80211_can_powered_addr_change(struct ieee80211_sub_if_data *sdata)
  165. {
  166. struct ieee80211_roc_work *roc;
  167. struct ieee80211_local *local = sdata->local;
  168. struct ieee80211_sub_if_data *scan_sdata;
  169. int ret = 0;
  170. lockdep_assert_wiphy(local->hw.wiphy);
  171. /* To be the most flexible here we want to only limit changing the
  172. * address if the specific interface is doing offchannel work or
  173. * scanning.
  174. */
  175. if (netif_carrier_ok(sdata->dev))
  176. return -EBUSY;
  177. /* First check no ROC work is happening on this iface */
  178. list_for_each_entry(roc, &local->roc_list, list) {
  179. if (roc->sdata != sdata)
  180. continue;
  181. if (roc->started) {
  182. ret = -EBUSY;
  183. goto unlock;
  184. }
  185. }
  186. /* And if this iface is scanning */
  187. if (local->scanning) {
  188. scan_sdata = rcu_dereference_protected(local->scan_sdata,
  189. lockdep_is_held(&local->hw.wiphy->mtx));
  190. if (sdata == scan_sdata)
  191. ret = -EBUSY;
  192. }
  193. switch (sdata->vif.type) {
  194. case NL80211_IFTYPE_STATION:
  195. case NL80211_IFTYPE_P2P_CLIENT:
  196. /* More interface types could be added here but changing the
  197. * address while powered makes the most sense in client modes.
  198. */
  199. break;
  200. default:
  201. ret = -EOPNOTSUPP;
  202. }
  203. unlock:
  204. return ret;
  205. }
  206. static int _ieee80211_change_mac(struct ieee80211_sub_if_data *sdata,
  207. void *addr)
  208. {
  209. struct ieee80211_local *local = sdata->local;
  210. struct sockaddr *sa = addr;
  211. bool check_dup = true;
  212. bool live = false;
  213. int ret;
  214. if (ieee80211_sdata_running(sdata)) {
  215. ret = ieee80211_can_powered_addr_change(sdata);
  216. if (ret)
  217. return ret;
  218. live = true;
  219. }
  220. if (sdata->vif.type == NL80211_IFTYPE_MONITOR &&
  221. !(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
  222. check_dup = false;
  223. ret = ieee80211_verify_mac(sdata, sa->sa_data, check_dup);
  224. if (ret)
  225. return ret;
  226. if (live)
  227. drv_remove_interface(local, sdata);
  228. ret = eth_mac_addr(sdata->dev, sa);
  229. if (ret == 0) {
  230. memcpy(sdata->vif.addr, sa->sa_data, ETH_ALEN);
  231. ether_addr_copy(sdata->vif.bss_conf.addr, sdata->vif.addr);
  232. }
  233. /* Regardless of eth_mac_addr() return we still want to add the
  234. * interface back. This should not fail...
  235. */
  236. if (live)
  237. WARN_ON(drv_add_interface(local, sdata));
  238. return ret;
  239. }
  240. static int ieee80211_change_mac(struct net_device *dev, void *addr)
  241. {
  242. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  243. struct ieee80211_local *local = sdata->local;
  244. int ret;
  245. /*
  246. * This happens during unregistration if there's a bond device
  247. * active (maybe other cases?) and we must get removed from it.
  248. * But we really don't care anymore if it's not registered now.
  249. */
  250. if (!dev->ieee80211_ptr->registered)
  251. return 0;
  252. wiphy_lock(local->hw.wiphy);
  253. ret = _ieee80211_change_mac(sdata, addr);
  254. wiphy_unlock(local->hw.wiphy);
  255. return ret;
  256. }
  257. static inline int identical_mac_addr_allowed(int type1, int type2)
  258. {
  259. return type1 == NL80211_IFTYPE_MONITOR ||
  260. type2 == NL80211_IFTYPE_MONITOR ||
  261. type1 == NL80211_IFTYPE_P2P_DEVICE ||
  262. type2 == NL80211_IFTYPE_P2P_DEVICE ||
  263. (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_AP_VLAN) ||
  264. (type1 == NL80211_IFTYPE_AP_VLAN &&
  265. (type2 == NL80211_IFTYPE_AP ||
  266. type2 == NL80211_IFTYPE_AP_VLAN));
  267. }
  268. static int ieee80211_check_concurrent_iface(struct ieee80211_sub_if_data *sdata,
  269. enum nl80211_iftype iftype)
  270. {
  271. struct ieee80211_local *local = sdata->local;
  272. struct ieee80211_sub_if_data *nsdata;
  273. ASSERT_RTNL();
  274. lockdep_assert_wiphy(local->hw.wiphy);
  275. /* we hold the RTNL here so can safely walk the list */
  276. list_for_each_entry(nsdata, &local->interfaces, list) {
  277. if (nsdata != sdata && ieee80211_sdata_running(nsdata)) {
  278. /*
  279. * Only OCB and monitor mode may coexist
  280. */
  281. if ((sdata->vif.type == NL80211_IFTYPE_OCB &&
  282. nsdata->vif.type != NL80211_IFTYPE_MONITOR) ||
  283. (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  284. nsdata->vif.type == NL80211_IFTYPE_OCB))
  285. return -EBUSY;
  286. /*
  287. * Allow only a single IBSS interface to be up at any
  288. * time. This is restricted because beacon distribution
  289. * cannot work properly if both are in the same IBSS.
  290. *
  291. * To remove this restriction we'd have to disallow them
  292. * from setting the same SSID on different IBSS interfaces
  293. * belonging to the same hardware. Then, however, we're
  294. * faced with having to adopt two different TSF timers...
  295. */
  296. if (iftype == NL80211_IFTYPE_ADHOC &&
  297. nsdata->vif.type == NL80211_IFTYPE_ADHOC)
  298. return -EBUSY;
  299. /*
  300. * will not add another interface while any channel
  301. * switch is active.
  302. */
  303. if (nsdata->vif.bss_conf.csa_active)
  304. return -EBUSY;
  305. /*
  306. * The remaining checks are only performed for interfaces
  307. * with the same MAC address.
  308. */
  309. if (!ether_addr_equal(sdata->vif.addr,
  310. nsdata->vif.addr))
  311. continue;
  312. /*
  313. * check whether it may have the same address
  314. */
  315. if (!identical_mac_addr_allowed(iftype,
  316. nsdata->vif.type))
  317. return -ENOTUNIQ;
  318. /* No support for VLAN with MLO yet */
  319. if (iftype == NL80211_IFTYPE_AP_VLAN &&
  320. sdata->wdev.use_4addr &&
  321. nsdata->vif.type == NL80211_IFTYPE_AP &&
  322. nsdata->vif.valid_links)
  323. return -EOPNOTSUPP;
  324. /*
  325. * can only add VLANs to enabled APs
  326. */
  327. if (iftype == NL80211_IFTYPE_AP_VLAN &&
  328. nsdata->vif.type == NL80211_IFTYPE_AP)
  329. sdata->bss = &nsdata->u.ap;
  330. }
  331. }
  332. return ieee80211_check_combinations(sdata, NULL, 0, 0, -1);
  333. }
  334. static int ieee80211_check_queues(struct ieee80211_sub_if_data *sdata,
  335. enum nl80211_iftype iftype)
  336. {
  337. int n_queues = sdata->local->hw.queues;
  338. int i;
  339. if (iftype == NL80211_IFTYPE_NAN)
  340. return 0;
  341. if (iftype != NL80211_IFTYPE_P2P_DEVICE) {
  342. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  343. if (WARN_ON_ONCE(sdata->vif.hw_queue[i] ==
  344. IEEE80211_INVAL_HW_QUEUE))
  345. return -EINVAL;
  346. if (WARN_ON_ONCE(sdata->vif.hw_queue[i] >=
  347. n_queues))
  348. return -EINVAL;
  349. }
  350. }
  351. if ((iftype != NL80211_IFTYPE_AP &&
  352. iftype != NL80211_IFTYPE_P2P_GO &&
  353. iftype != NL80211_IFTYPE_MESH_POINT) ||
  354. !ieee80211_hw_check(&sdata->local->hw, QUEUE_CONTROL)) {
  355. sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE;
  356. return 0;
  357. }
  358. if (WARN_ON_ONCE(sdata->vif.cab_queue == IEEE80211_INVAL_HW_QUEUE))
  359. return -EINVAL;
  360. if (WARN_ON_ONCE(sdata->vif.cab_queue >= n_queues))
  361. return -EINVAL;
  362. return 0;
  363. }
  364. static int ieee80211_open(struct net_device *dev)
  365. {
  366. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  367. int err;
  368. /* fail early if user set an invalid address */
  369. if (!is_valid_ether_addr(dev->dev_addr))
  370. return -EADDRNOTAVAIL;
  371. wiphy_lock(sdata->local->hw.wiphy);
  372. err = ieee80211_check_concurrent_iface(sdata, sdata->vif.type);
  373. if (err)
  374. goto out;
  375. err = ieee80211_do_open(&sdata->wdev, true);
  376. out:
  377. wiphy_unlock(sdata->local->hw.wiphy);
  378. return err;
  379. }
  380. static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata, bool going_down)
  381. {
  382. struct ieee80211_local *local = sdata->local;
  383. unsigned long flags;
  384. struct sk_buff_head freeq;
  385. struct sk_buff *skb, *tmp;
  386. u32 hw_reconf_flags = 0;
  387. int i, flushed;
  388. struct ps_data *ps;
  389. struct cfg80211_chan_def chandef;
  390. bool cancel_scan;
  391. struct cfg80211_nan_func *func;
  392. lockdep_assert_wiphy(local->hw.wiphy);
  393. clear_bit(SDATA_STATE_RUNNING, &sdata->state);
  394. synchronize_rcu(); /* flush _ieee80211_wake_txqs() */
  395. cancel_scan = rcu_access_pointer(local->scan_sdata) == sdata;
  396. if (cancel_scan)
  397. ieee80211_scan_cancel(local);
  398. ieee80211_roc_purge(local, sdata);
  399. switch (sdata->vif.type) {
  400. case NL80211_IFTYPE_STATION:
  401. ieee80211_mgd_stop(sdata);
  402. break;
  403. case NL80211_IFTYPE_ADHOC:
  404. ieee80211_ibss_stop(sdata);
  405. break;
  406. case NL80211_IFTYPE_MONITOR:
  407. if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES)
  408. break;
  409. list_del_rcu(&sdata->u.mntr.list);
  410. break;
  411. case NL80211_IFTYPE_AP_VLAN:
  412. ieee80211_apvlan_link_clear(sdata);
  413. break;
  414. default:
  415. break;
  416. }
  417. /*
  418. * Remove all stations associated with this interface.
  419. *
  420. * This must be done before calling ops->remove_interface()
  421. * because otherwise we can later invoke ops->sta_notify()
  422. * whenever the STAs are removed, and that invalidates driver
  423. * assumptions about always getting a vif pointer that is valid
  424. * (because if we remove a STA after ops->remove_interface()
  425. * the driver will have removed the vif info already!)
  426. *
  427. * For AP_VLANs stations may exist since there's nothing else that
  428. * would have removed them, but in other modes there shouldn't
  429. * be any stations.
  430. */
  431. flushed = sta_info_flush(sdata, -1);
  432. WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_AP_VLAN && flushed > 0);
  433. /* don't count this interface for allmulti while it is down */
  434. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  435. atomic_dec(&local->iff_allmultis);
  436. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  437. local->fif_pspoll--;
  438. local->fif_probe_req--;
  439. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  440. local->fif_probe_req--;
  441. }
  442. if (sdata->dev) {
  443. netif_addr_lock_bh(sdata->dev);
  444. spin_lock_bh(&local->filter_lock);
  445. __hw_addr_unsync(&local->mc_list, &sdata->dev->mc,
  446. sdata->dev->addr_len);
  447. spin_unlock_bh(&local->filter_lock);
  448. netif_addr_unlock_bh(sdata->dev);
  449. }
  450. del_timer_sync(&local->dynamic_ps_timer);
  451. wiphy_work_cancel(local->hw.wiphy, &local->dynamic_ps_enable_work);
  452. WARN(ieee80211_vif_is_mld(&sdata->vif),
  453. "destroying interface with valid links 0x%04x\n",
  454. sdata->vif.valid_links);
  455. sdata->vif.bss_conf.csa_active = false;
  456. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  457. sdata->deflink.u.mgd.csa.waiting_bcn = false;
  458. ieee80211_vif_unblock_queues_csa(sdata);
  459. wiphy_work_cancel(local->hw.wiphy, &sdata->deflink.csa.finalize_work);
  460. wiphy_work_cancel(local->hw.wiphy,
  461. &sdata->deflink.color_change_finalize_work);
  462. wiphy_delayed_work_cancel(local->hw.wiphy,
  463. &sdata->deflink.dfs_cac_timer_work);
  464. if (sdata->wdev.links[0].cac_started) {
  465. chandef = sdata->vif.bss_conf.chanreq.oper;
  466. WARN_ON(local->suspended);
  467. ieee80211_link_release_channel(&sdata->deflink);
  468. cfg80211_cac_event(sdata->dev, &chandef,
  469. NL80211_RADAR_CAC_ABORTED,
  470. GFP_KERNEL, 0);
  471. }
  472. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  473. WARN_ON(!list_empty(&sdata->u.ap.vlans));
  474. } else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  475. /* remove all packets in parent bc_buf pointing to this dev */
  476. ps = &sdata->bss->ps;
  477. spin_lock_irqsave(&ps->bc_buf.lock, flags);
  478. skb_queue_walk_safe(&ps->bc_buf, skb, tmp) {
  479. if (skb->dev == sdata->dev) {
  480. __skb_unlink(skb, &ps->bc_buf);
  481. local->total_ps_buffered--;
  482. ieee80211_free_txskb(&local->hw, skb);
  483. }
  484. }
  485. spin_unlock_irqrestore(&ps->bc_buf.lock, flags);
  486. }
  487. if (going_down)
  488. local->open_count--;
  489. switch (sdata->vif.type) {
  490. case NL80211_IFTYPE_AP_VLAN:
  491. list_del(&sdata->u.vlan.list);
  492. RCU_INIT_POINTER(sdata->vif.bss_conf.chanctx_conf, NULL);
  493. /* see comment in the default case below */
  494. ieee80211_free_keys(sdata, true);
  495. /* no need to tell driver */
  496. break;
  497. case NL80211_IFTYPE_MONITOR:
  498. if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES) {
  499. local->cooked_mntrs--;
  500. break;
  501. }
  502. local->monitors--;
  503. if (local->monitors == 0) {
  504. local->hw.conf.flags &= ~IEEE80211_CONF_MONITOR;
  505. hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
  506. }
  507. ieee80211_adjust_monitor_flags(sdata, -1);
  508. break;
  509. case NL80211_IFTYPE_NAN:
  510. /* clean all the functions */
  511. spin_lock_bh(&sdata->u.nan.func_lock);
  512. idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, i) {
  513. idr_remove(&sdata->u.nan.function_inst_ids, i);
  514. cfg80211_free_nan_func(func);
  515. }
  516. idr_destroy(&sdata->u.nan.function_inst_ids);
  517. spin_unlock_bh(&sdata->u.nan.func_lock);
  518. break;
  519. case NL80211_IFTYPE_P2P_DEVICE:
  520. /* relies on synchronize_rcu() below */
  521. RCU_INIT_POINTER(local->p2p_sdata, NULL);
  522. fallthrough;
  523. default:
  524. wiphy_work_cancel(sdata->local->hw.wiphy, &sdata->work);
  525. /*
  526. * When we get here, the interface is marked down.
  527. * Free the remaining keys, if there are any
  528. * (which can happen in AP mode if userspace sets
  529. * keys before the interface is operating)
  530. *
  531. * Force the key freeing to always synchronize_net()
  532. * to wait for the RX path in case it is using this
  533. * interface enqueuing frames at this very time on
  534. * another CPU.
  535. */
  536. ieee80211_free_keys(sdata, true);
  537. skb_queue_purge(&sdata->skb_queue);
  538. skb_queue_purge(&sdata->status_queue);
  539. }
  540. /*
  541. * Since ieee80211_free_txskb() may issue __dev_queue_xmit()
  542. * which should be called with interrupts enabled, reclamation
  543. * is done in two phases:
  544. */
  545. __skb_queue_head_init(&freeq);
  546. /* unlink from local queues... */
  547. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  548. for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
  549. skb_queue_walk_safe(&local->pending[i], skb, tmp) {
  550. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  551. if (info->control.vif == &sdata->vif) {
  552. __skb_unlink(skb, &local->pending[i]);
  553. __skb_queue_tail(&freeq, skb);
  554. }
  555. }
  556. }
  557. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  558. /* ... and perform actual reclamation with interrupts enabled. */
  559. skb_queue_walk_safe(&freeq, skb, tmp) {
  560. __skb_unlink(skb, &freeq);
  561. ieee80211_free_txskb(&local->hw, skb);
  562. }
  563. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  564. ieee80211_txq_remove_vlan(local, sdata);
  565. if (sdata->vif.txq)
  566. ieee80211_txq_purge(sdata->local, to_txq_info(sdata->vif.txq));
  567. sdata->bss = NULL;
  568. if (local->open_count == 0)
  569. ieee80211_clear_tx_pending(local);
  570. sdata->vif.bss_conf.beacon_int = 0;
  571. /*
  572. * If the interface goes down while suspended, presumably because
  573. * the device was unplugged and that happens before our resume,
  574. * then the driver is already unconfigured and the remainder of
  575. * this function isn't needed.
  576. * XXX: what about WoWLAN? If the device has software state, e.g.
  577. * memory allocated, it might expect teardown commands from
  578. * mac80211 here?
  579. */
  580. if (local->suspended) {
  581. WARN_ON(local->wowlan);
  582. WARN_ON(rcu_access_pointer(local->monitor_sdata));
  583. return;
  584. }
  585. switch (sdata->vif.type) {
  586. case NL80211_IFTYPE_AP_VLAN:
  587. break;
  588. case NL80211_IFTYPE_MONITOR:
  589. if (local->monitors == 0)
  590. ieee80211_del_virtual_monitor(local);
  591. ieee80211_recalc_idle(local);
  592. ieee80211_recalc_offload(local);
  593. if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
  594. break;
  595. fallthrough;
  596. default:
  597. if (!going_down)
  598. break;
  599. drv_remove_interface(local, sdata);
  600. /* Clear private driver data to prevent reuse */
  601. memset(sdata->vif.drv_priv, 0, local->hw.vif_data_size);
  602. }
  603. ieee80211_recalc_ps(local);
  604. if (cancel_scan)
  605. wiphy_delayed_work_flush(local->hw.wiphy, &local->scan_work);
  606. if (local->open_count == 0) {
  607. ieee80211_stop_device(local, false);
  608. /* no reconfiguring after stop! */
  609. return;
  610. }
  611. /* do after stop to avoid reconfiguring when we stop anyway */
  612. ieee80211_configure_filter(local);
  613. ieee80211_hw_config(local, hw_reconf_flags);
  614. if (local->monitors == local->open_count)
  615. ieee80211_add_virtual_monitor(local);
  616. }
  617. static void ieee80211_stop_mbssid(struct ieee80211_sub_if_data *sdata)
  618. {
  619. struct ieee80211_sub_if_data *tx_sdata, *non_tx_sdata, *tmp_sdata;
  620. struct ieee80211_vif *tx_vif = sdata->vif.mbssid_tx_vif;
  621. if (!tx_vif)
  622. return;
  623. tx_sdata = vif_to_sdata(tx_vif);
  624. sdata->vif.mbssid_tx_vif = NULL;
  625. list_for_each_entry_safe(non_tx_sdata, tmp_sdata,
  626. &tx_sdata->local->interfaces, list) {
  627. if (non_tx_sdata != sdata && non_tx_sdata != tx_sdata &&
  628. non_tx_sdata->vif.mbssid_tx_vif == tx_vif &&
  629. ieee80211_sdata_running(non_tx_sdata)) {
  630. non_tx_sdata->vif.mbssid_tx_vif = NULL;
  631. dev_close(non_tx_sdata->wdev.netdev);
  632. }
  633. }
  634. if (sdata != tx_sdata && ieee80211_sdata_running(tx_sdata)) {
  635. tx_sdata->vif.mbssid_tx_vif = NULL;
  636. dev_close(tx_sdata->wdev.netdev);
  637. }
  638. }
  639. static int ieee80211_stop(struct net_device *dev)
  640. {
  641. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  642. /* close dependent VLAN and MBSSID interfaces before locking wiphy */
  643. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  644. struct ieee80211_sub_if_data *vlan, *tmpsdata;
  645. list_for_each_entry_safe(vlan, tmpsdata, &sdata->u.ap.vlans,
  646. u.vlan.list)
  647. dev_close(vlan->dev);
  648. ieee80211_stop_mbssid(sdata);
  649. }
  650. wiphy_lock(sdata->local->hw.wiphy);
  651. wiphy_work_cancel(sdata->local->hw.wiphy, &sdata->activate_links_work);
  652. ieee80211_do_stop(sdata, true);
  653. wiphy_unlock(sdata->local->hw.wiphy);
  654. return 0;
  655. }
  656. static void ieee80211_set_multicast_list(struct net_device *dev)
  657. {
  658. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  659. struct ieee80211_local *local = sdata->local;
  660. int allmulti, sdata_allmulti;
  661. allmulti = !!(dev->flags & IFF_ALLMULTI);
  662. sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
  663. if (allmulti != sdata_allmulti) {
  664. if (dev->flags & IFF_ALLMULTI)
  665. atomic_inc(&local->iff_allmultis);
  666. else
  667. atomic_dec(&local->iff_allmultis);
  668. sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
  669. }
  670. spin_lock_bh(&local->filter_lock);
  671. __hw_addr_sync(&local->mc_list, &dev->mc, dev->addr_len);
  672. spin_unlock_bh(&local->filter_lock);
  673. wiphy_work_queue(local->hw.wiphy, &local->reconfig_filter);
  674. }
  675. /*
  676. * Called when the netdev is removed or, by the code below, before
  677. * the interface type changes.
  678. */
  679. static void ieee80211_teardown_sdata(struct ieee80211_sub_if_data *sdata)
  680. {
  681. if (WARN_ON(!list_empty(&sdata->work.entry)))
  682. wiphy_work_cancel(sdata->local->hw.wiphy, &sdata->work);
  683. /* free extra data */
  684. ieee80211_free_keys(sdata, false);
  685. ieee80211_debugfs_remove_netdev(sdata);
  686. ieee80211_destroy_frag_cache(&sdata->frags);
  687. if (ieee80211_vif_is_mesh(&sdata->vif))
  688. ieee80211_mesh_teardown_sdata(sdata);
  689. ieee80211_vif_clear_links(sdata);
  690. ieee80211_link_stop(&sdata->deflink);
  691. }
  692. static void ieee80211_uninit(struct net_device *dev)
  693. {
  694. ieee80211_teardown_sdata(IEEE80211_DEV_TO_SUB_IF(dev));
  695. }
  696. static int ieee80211_netdev_setup_tc(struct net_device *dev,
  697. enum tc_setup_type type, void *type_data)
  698. {
  699. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  700. struct ieee80211_local *local = sdata->local;
  701. return drv_net_setup_tc(local, sdata, dev, type, type_data);
  702. }
  703. static const struct net_device_ops ieee80211_dataif_ops = {
  704. .ndo_open = ieee80211_open,
  705. .ndo_stop = ieee80211_stop,
  706. .ndo_uninit = ieee80211_uninit,
  707. .ndo_start_xmit = ieee80211_subif_start_xmit,
  708. .ndo_set_rx_mode = ieee80211_set_multicast_list,
  709. .ndo_set_mac_address = ieee80211_change_mac,
  710. .ndo_setup_tc = ieee80211_netdev_setup_tc,
  711. };
  712. static u16 ieee80211_monitor_select_queue(struct net_device *dev,
  713. struct sk_buff *skb,
  714. struct net_device *sb_dev)
  715. {
  716. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  717. struct ieee80211_local *local = sdata->local;
  718. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  719. struct ieee80211_hdr *hdr;
  720. int len_rthdr;
  721. if (local->hw.queues < IEEE80211_NUM_ACS)
  722. return 0;
  723. /* reset flags and info before parsing radiotap header */
  724. memset(info, 0, sizeof(*info));
  725. if (!ieee80211_parse_tx_radiotap(skb, dev))
  726. return 0; /* doesn't matter, frame will be dropped */
  727. len_rthdr = ieee80211_get_radiotap_len(skb->data);
  728. hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
  729. if (skb->len < len_rthdr + 2 ||
  730. skb->len < len_rthdr + ieee80211_hdrlen(hdr->frame_control))
  731. return 0; /* doesn't matter, frame will be dropped */
  732. return ieee80211_select_queue_80211(sdata, skb, hdr);
  733. }
  734. static const struct net_device_ops ieee80211_monitorif_ops = {
  735. .ndo_open = ieee80211_open,
  736. .ndo_stop = ieee80211_stop,
  737. .ndo_uninit = ieee80211_uninit,
  738. .ndo_start_xmit = ieee80211_monitor_start_xmit,
  739. .ndo_set_rx_mode = ieee80211_set_multicast_list,
  740. .ndo_set_mac_address = ieee80211_change_mac,
  741. .ndo_select_queue = ieee80211_monitor_select_queue,
  742. };
  743. static int ieee80211_netdev_fill_forward_path(struct net_device_path_ctx *ctx,
  744. struct net_device_path *path)
  745. {
  746. struct ieee80211_sub_if_data *sdata;
  747. struct ieee80211_local *local;
  748. struct sta_info *sta;
  749. int ret = -ENOENT;
  750. sdata = IEEE80211_DEV_TO_SUB_IF(ctx->dev);
  751. local = sdata->local;
  752. if (!local->ops->net_fill_forward_path)
  753. return -EOPNOTSUPP;
  754. rcu_read_lock();
  755. switch (sdata->vif.type) {
  756. case NL80211_IFTYPE_AP_VLAN:
  757. sta = rcu_dereference(sdata->u.vlan.sta);
  758. if (sta)
  759. break;
  760. if (sdata->wdev.use_4addr)
  761. goto out;
  762. if (is_multicast_ether_addr(ctx->daddr))
  763. goto out;
  764. sta = sta_info_get_bss(sdata, ctx->daddr);
  765. break;
  766. case NL80211_IFTYPE_AP:
  767. if (is_multicast_ether_addr(ctx->daddr))
  768. goto out;
  769. sta = sta_info_get(sdata, ctx->daddr);
  770. break;
  771. case NL80211_IFTYPE_STATION:
  772. if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
  773. sta = sta_info_get(sdata, ctx->daddr);
  774. if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
  775. if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH))
  776. goto out;
  777. break;
  778. }
  779. }
  780. sta = sta_info_get(sdata, sdata->deflink.u.mgd.bssid);
  781. break;
  782. default:
  783. goto out;
  784. }
  785. if (!sta)
  786. goto out;
  787. ret = drv_net_fill_forward_path(local, sdata, &sta->sta, ctx, path);
  788. out:
  789. rcu_read_unlock();
  790. return ret;
  791. }
  792. static const struct net_device_ops ieee80211_dataif_8023_ops = {
  793. .ndo_open = ieee80211_open,
  794. .ndo_stop = ieee80211_stop,
  795. .ndo_uninit = ieee80211_uninit,
  796. .ndo_start_xmit = ieee80211_subif_start_xmit_8023,
  797. .ndo_set_rx_mode = ieee80211_set_multicast_list,
  798. .ndo_set_mac_address = ieee80211_change_mac,
  799. .ndo_fill_forward_path = ieee80211_netdev_fill_forward_path,
  800. .ndo_setup_tc = ieee80211_netdev_setup_tc,
  801. };
  802. static bool ieee80211_iftype_supports_hdr_offload(enum nl80211_iftype iftype)
  803. {
  804. switch (iftype) {
  805. /* P2P GO and client are mapped to AP/STATION types */
  806. case NL80211_IFTYPE_AP:
  807. case NL80211_IFTYPE_STATION:
  808. return true;
  809. default:
  810. return false;
  811. }
  812. }
  813. static bool ieee80211_set_sdata_offload_flags(struct ieee80211_sub_if_data *sdata)
  814. {
  815. struct ieee80211_local *local = sdata->local;
  816. u32 flags;
  817. flags = sdata->vif.offload_flags;
  818. if (ieee80211_hw_check(&local->hw, SUPPORTS_TX_ENCAP_OFFLOAD) &&
  819. ieee80211_iftype_supports_hdr_offload(sdata->vif.type)) {
  820. flags |= IEEE80211_OFFLOAD_ENCAP_ENABLED;
  821. if (!ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG) &&
  822. local->hw.wiphy->frag_threshold != (u32)-1)
  823. flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
  824. if (local->monitors)
  825. flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
  826. } else {
  827. flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
  828. }
  829. if (ieee80211_hw_check(&local->hw, SUPPORTS_RX_DECAP_OFFLOAD) &&
  830. ieee80211_iftype_supports_hdr_offload(sdata->vif.type)) {
  831. flags |= IEEE80211_OFFLOAD_DECAP_ENABLED;
  832. if (local->monitors &&
  833. !ieee80211_hw_check(&local->hw, SUPPORTS_CONC_MON_RX_DECAP))
  834. flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
  835. } else {
  836. flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
  837. }
  838. if (sdata->vif.offload_flags == flags)
  839. return false;
  840. sdata->vif.offload_flags = flags;
  841. ieee80211_check_fast_rx_iface(sdata);
  842. return true;
  843. }
  844. static void ieee80211_set_vif_encap_ops(struct ieee80211_sub_if_data *sdata)
  845. {
  846. struct ieee80211_local *local = sdata->local;
  847. struct ieee80211_sub_if_data *bss = sdata;
  848. bool enabled;
  849. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  850. if (!sdata->bss)
  851. return;
  852. bss = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
  853. }
  854. if (!ieee80211_hw_check(&local->hw, SUPPORTS_TX_ENCAP_OFFLOAD) ||
  855. !ieee80211_iftype_supports_hdr_offload(bss->vif.type))
  856. return;
  857. enabled = bss->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED;
  858. if (sdata->wdev.use_4addr &&
  859. !(bss->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_4ADDR))
  860. enabled = false;
  861. sdata->dev->netdev_ops = enabled ? &ieee80211_dataif_8023_ops :
  862. &ieee80211_dataif_ops;
  863. }
  864. static void ieee80211_recalc_sdata_offload(struct ieee80211_sub_if_data *sdata)
  865. {
  866. struct ieee80211_local *local = sdata->local;
  867. struct ieee80211_sub_if_data *vsdata;
  868. if (ieee80211_set_sdata_offload_flags(sdata)) {
  869. drv_update_vif_offload(local, sdata);
  870. ieee80211_set_vif_encap_ops(sdata);
  871. }
  872. list_for_each_entry(vsdata, &local->interfaces, list) {
  873. if (vsdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
  874. vsdata->bss != &sdata->u.ap)
  875. continue;
  876. ieee80211_set_vif_encap_ops(vsdata);
  877. }
  878. }
  879. void ieee80211_recalc_offload(struct ieee80211_local *local)
  880. {
  881. struct ieee80211_sub_if_data *sdata;
  882. if (!ieee80211_hw_check(&local->hw, SUPPORTS_TX_ENCAP_OFFLOAD))
  883. return;
  884. lockdep_assert_wiphy(local->hw.wiphy);
  885. list_for_each_entry(sdata, &local->interfaces, list) {
  886. if (!ieee80211_sdata_running(sdata))
  887. continue;
  888. ieee80211_recalc_sdata_offload(sdata);
  889. }
  890. }
  891. void ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data *sdata,
  892. const int offset)
  893. {
  894. struct ieee80211_local *local = sdata->local;
  895. u32 flags = sdata->u.mntr.flags;
  896. #define ADJUST(_f, _s) do { \
  897. if (flags & MONITOR_FLAG_##_f) \
  898. local->fif_##_s += offset; \
  899. } while (0)
  900. ADJUST(FCSFAIL, fcsfail);
  901. ADJUST(PLCPFAIL, plcpfail);
  902. ADJUST(CONTROL, control);
  903. ADJUST(CONTROL, pspoll);
  904. ADJUST(OTHER_BSS, other_bss);
  905. #undef ADJUST
  906. }
  907. static void ieee80211_set_default_queues(struct ieee80211_sub_if_data *sdata)
  908. {
  909. struct ieee80211_local *local = sdata->local;
  910. int i;
  911. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  912. if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
  913. sdata->vif.hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
  914. else if (local->hw.queues >= IEEE80211_NUM_ACS)
  915. sdata->vif.hw_queue[i] = i;
  916. else
  917. sdata->vif.hw_queue[i] = 0;
  918. }
  919. sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE;
  920. }
  921. static void ieee80211_sdata_init(struct ieee80211_local *local,
  922. struct ieee80211_sub_if_data *sdata)
  923. {
  924. sdata->local = local;
  925. /*
  926. * Initialize the default link, so we can use link_id 0 for non-MLD,
  927. * and that continues to work for non-MLD-aware drivers that use just
  928. * vif.bss_conf instead of vif.link_conf.
  929. *
  930. * Note that we never change this, so if link ID 0 isn't used in an
  931. * MLD connection, we get a separate allocation for it.
  932. */
  933. ieee80211_link_init(sdata, -1, &sdata->deflink, &sdata->vif.bss_conf);
  934. }
  935. int ieee80211_add_virtual_monitor(struct ieee80211_local *local)
  936. {
  937. struct ieee80211_sub_if_data *sdata;
  938. int ret;
  939. ASSERT_RTNL();
  940. lockdep_assert_wiphy(local->hw.wiphy);
  941. if (local->monitor_sdata)
  942. return 0;
  943. sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size, GFP_KERNEL);
  944. if (!sdata)
  945. return -ENOMEM;
  946. /* set up data */
  947. sdata->vif.type = NL80211_IFTYPE_MONITOR;
  948. snprintf(sdata->name, IFNAMSIZ, "%s-monitor",
  949. wiphy_name(local->hw.wiphy));
  950. sdata->wdev.iftype = NL80211_IFTYPE_MONITOR;
  951. sdata->wdev.wiphy = local->hw.wiphy;
  952. ieee80211_sdata_init(local, sdata);
  953. ieee80211_set_default_queues(sdata);
  954. if (ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF)) {
  955. ret = drv_add_interface(local, sdata);
  956. if (WARN_ON(ret)) {
  957. /* ok .. stupid driver, it asked for this! */
  958. kfree(sdata);
  959. return ret;
  960. }
  961. }
  962. set_bit(SDATA_STATE_RUNNING, &sdata->state);
  963. ret = ieee80211_check_queues(sdata, NL80211_IFTYPE_MONITOR);
  964. if (ret) {
  965. kfree(sdata);
  966. return ret;
  967. }
  968. mutex_lock(&local->iflist_mtx);
  969. rcu_assign_pointer(local->monitor_sdata, sdata);
  970. mutex_unlock(&local->iflist_mtx);
  971. ret = ieee80211_link_use_channel(&sdata->deflink, &local->monitor_chanreq,
  972. IEEE80211_CHANCTX_EXCLUSIVE);
  973. if (ret) {
  974. mutex_lock(&local->iflist_mtx);
  975. RCU_INIT_POINTER(local->monitor_sdata, NULL);
  976. mutex_unlock(&local->iflist_mtx);
  977. synchronize_net();
  978. drv_remove_interface(local, sdata);
  979. kfree(sdata);
  980. return ret;
  981. }
  982. skb_queue_head_init(&sdata->skb_queue);
  983. skb_queue_head_init(&sdata->status_queue);
  984. wiphy_work_init(&sdata->work, ieee80211_iface_work);
  985. return 0;
  986. }
  987. void ieee80211_del_virtual_monitor(struct ieee80211_local *local)
  988. {
  989. struct ieee80211_sub_if_data *sdata;
  990. ASSERT_RTNL();
  991. lockdep_assert_wiphy(local->hw.wiphy);
  992. mutex_lock(&local->iflist_mtx);
  993. sdata = rcu_dereference_protected(local->monitor_sdata,
  994. lockdep_is_held(&local->iflist_mtx));
  995. if (!sdata) {
  996. mutex_unlock(&local->iflist_mtx);
  997. return;
  998. }
  999. clear_bit(SDATA_STATE_RUNNING, &sdata->state);
  1000. ieee80211_link_release_channel(&sdata->deflink);
  1001. if (ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF))
  1002. drv_remove_interface(local, sdata);
  1003. RCU_INIT_POINTER(local->monitor_sdata, NULL);
  1004. mutex_unlock(&local->iflist_mtx);
  1005. synchronize_net();
  1006. kfree(sdata);
  1007. }
  1008. /*
  1009. * NOTE: Be very careful when changing this function, it must NOT return
  1010. * an error on interface type changes that have been pre-checked, so most
  1011. * checks should be in ieee80211_check_concurrent_iface.
  1012. */
  1013. int ieee80211_do_open(struct wireless_dev *wdev, bool coming_up)
  1014. {
  1015. struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
  1016. struct net_device *dev = wdev->netdev;
  1017. struct ieee80211_local *local = sdata->local;
  1018. u64 changed = 0;
  1019. int res;
  1020. u32 hw_reconf_flags = 0;
  1021. lockdep_assert_wiphy(local->hw.wiphy);
  1022. switch (sdata->vif.type) {
  1023. case NL80211_IFTYPE_AP_VLAN: {
  1024. struct ieee80211_sub_if_data *master;
  1025. if (!sdata->bss)
  1026. return -ENOLINK;
  1027. list_add(&sdata->u.vlan.list, &sdata->bss->vlans);
  1028. master = container_of(sdata->bss,
  1029. struct ieee80211_sub_if_data, u.ap);
  1030. sdata->control_port_protocol =
  1031. master->control_port_protocol;
  1032. sdata->control_port_no_encrypt =
  1033. master->control_port_no_encrypt;
  1034. sdata->control_port_over_nl80211 =
  1035. master->control_port_over_nl80211;
  1036. sdata->control_port_no_preauth =
  1037. master->control_port_no_preauth;
  1038. sdata->vif.cab_queue = master->vif.cab_queue;
  1039. memcpy(sdata->vif.hw_queue, master->vif.hw_queue,
  1040. sizeof(sdata->vif.hw_queue));
  1041. sdata->vif.bss_conf.chanreq = master->vif.bss_conf.chanreq;
  1042. sdata->crypto_tx_tailroom_needed_cnt +=
  1043. master->crypto_tx_tailroom_needed_cnt;
  1044. ieee80211_apvlan_link_setup(sdata);
  1045. break;
  1046. }
  1047. case NL80211_IFTYPE_AP:
  1048. sdata->bss = &sdata->u.ap;
  1049. break;
  1050. case NL80211_IFTYPE_MESH_POINT:
  1051. case NL80211_IFTYPE_STATION:
  1052. case NL80211_IFTYPE_MONITOR:
  1053. case NL80211_IFTYPE_ADHOC:
  1054. case NL80211_IFTYPE_P2P_DEVICE:
  1055. case NL80211_IFTYPE_OCB:
  1056. case NL80211_IFTYPE_NAN:
  1057. /* no special treatment */
  1058. break;
  1059. case NL80211_IFTYPE_UNSPECIFIED:
  1060. case NUM_NL80211_IFTYPES:
  1061. case NL80211_IFTYPE_P2P_CLIENT:
  1062. case NL80211_IFTYPE_P2P_GO:
  1063. case NL80211_IFTYPE_WDS:
  1064. /* cannot happen */
  1065. WARN_ON(1);
  1066. break;
  1067. }
  1068. if (local->open_count == 0) {
  1069. /* here we can consider everything in good order (again) */
  1070. local->reconfig_failure = false;
  1071. res = drv_start(local);
  1072. if (res)
  1073. goto err_del_bss;
  1074. ieee80211_led_radio(local, true);
  1075. ieee80211_mod_tpt_led_trig(local,
  1076. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  1077. }
  1078. /*
  1079. * Copy the hopefully now-present MAC address to
  1080. * this interface, if it has the special null one.
  1081. */
  1082. if (dev && is_zero_ether_addr(dev->dev_addr)) {
  1083. eth_hw_addr_set(dev, local->hw.wiphy->perm_addr);
  1084. memcpy(dev->perm_addr, dev->dev_addr, ETH_ALEN);
  1085. if (!is_valid_ether_addr(dev->dev_addr)) {
  1086. res = -EADDRNOTAVAIL;
  1087. goto err_stop;
  1088. }
  1089. }
  1090. switch (sdata->vif.type) {
  1091. case NL80211_IFTYPE_AP_VLAN:
  1092. /* no need to tell driver, but set carrier and chanctx */
  1093. if (sdata->bss->active) {
  1094. struct ieee80211_link_data *link;
  1095. for_each_link_data(sdata, link) {
  1096. ieee80211_link_vlan_copy_chanctx(link);
  1097. }
  1098. netif_carrier_on(dev);
  1099. ieee80211_set_vif_encap_ops(sdata);
  1100. } else {
  1101. netif_carrier_off(dev);
  1102. }
  1103. break;
  1104. case NL80211_IFTYPE_MONITOR:
  1105. if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES) {
  1106. local->cooked_mntrs++;
  1107. break;
  1108. }
  1109. if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
  1110. res = drv_add_interface(local, sdata);
  1111. if (res)
  1112. goto err_stop;
  1113. } else if (local->monitors == 0 && local->open_count == 0) {
  1114. res = ieee80211_add_virtual_monitor(local);
  1115. if (res)
  1116. goto err_stop;
  1117. }
  1118. /* must be before the call to ieee80211_configure_filter */
  1119. local->monitors++;
  1120. if (local->monitors == 1) {
  1121. local->hw.conf.flags |= IEEE80211_CONF_MONITOR;
  1122. hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
  1123. }
  1124. ieee80211_adjust_monitor_flags(sdata, 1);
  1125. ieee80211_configure_filter(local);
  1126. ieee80211_recalc_offload(local);
  1127. ieee80211_recalc_idle(local);
  1128. netif_carrier_on(dev);
  1129. break;
  1130. default:
  1131. if (coming_up) {
  1132. ieee80211_del_virtual_monitor(local);
  1133. ieee80211_set_sdata_offload_flags(sdata);
  1134. res = drv_add_interface(local, sdata);
  1135. if (res)
  1136. goto err_stop;
  1137. ieee80211_set_vif_encap_ops(sdata);
  1138. res = ieee80211_check_queues(sdata,
  1139. ieee80211_vif_type_p2p(&sdata->vif));
  1140. if (res)
  1141. goto err_del_interface;
  1142. }
  1143. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  1144. local->fif_pspoll++;
  1145. local->fif_probe_req++;
  1146. ieee80211_configure_filter(local);
  1147. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  1148. local->fif_probe_req++;
  1149. }
  1150. if (sdata->vif.probe_req_reg)
  1151. drv_config_iface_filter(local, sdata,
  1152. FIF_PROBE_REQ,
  1153. FIF_PROBE_REQ);
  1154. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
  1155. sdata->vif.type != NL80211_IFTYPE_NAN)
  1156. changed |= ieee80211_reset_erp_info(sdata);
  1157. ieee80211_link_info_change_notify(sdata, &sdata->deflink,
  1158. changed);
  1159. switch (sdata->vif.type) {
  1160. case NL80211_IFTYPE_STATION:
  1161. case NL80211_IFTYPE_ADHOC:
  1162. case NL80211_IFTYPE_AP:
  1163. case NL80211_IFTYPE_MESH_POINT:
  1164. case NL80211_IFTYPE_OCB:
  1165. netif_carrier_off(dev);
  1166. break;
  1167. case NL80211_IFTYPE_P2P_DEVICE:
  1168. case NL80211_IFTYPE_NAN:
  1169. break;
  1170. default:
  1171. /* not reached */
  1172. WARN_ON(1);
  1173. }
  1174. /*
  1175. * Set default queue parameters so drivers don't
  1176. * need to initialise the hardware if the hardware
  1177. * doesn't start up with sane defaults.
  1178. * Enable QoS for anything but station interfaces.
  1179. */
  1180. ieee80211_set_wmm_default(&sdata->deflink, true,
  1181. sdata->vif.type != NL80211_IFTYPE_STATION);
  1182. }
  1183. switch (sdata->vif.type) {
  1184. case NL80211_IFTYPE_P2P_DEVICE:
  1185. rcu_assign_pointer(local->p2p_sdata, sdata);
  1186. break;
  1187. case NL80211_IFTYPE_MONITOR:
  1188. if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES)
  1189. break;
  1190. list_add_tail_rcu(&sdata->u.mntr.list, &local->mon_list);
  1191. break;
  1192. default:
  1193. break;
  1194. }
  1195. /*
  1196. * set_multicast_list will be invoked by the networking core
  1197. * which will check whether any increments here were done in
  1198. * error and sync them down to the hardware as filter flags.
  1199. */
  1200. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  1201. atomic_inc(&local->iff_allmultis);
  1202. if (coming_up)
  1203. local->open_count++;
  1204. if (local->open_count == 1)
  1205. ieee80211_hw_conf_init(local);
  1206. else if (hw_reconf_flags)
  1207. ieee80211_hw_config(local, hw_reconf_flags);
  1208. ieee80211_recalc_ps(local);
  1209. set_bit(SDATA_STATE_RUNNING, &sdata->state);
  1210. return 0;
  1211. err_del_interface:
  1212. drv_remove_interface(local, sdata);
  1213. err_stop:
  1214. if (!local->open_count)
  1215. drv_stop(local, false);
  1216. err_del_bss:
  1217. sdata->bss = NULL;
  1218. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1219. list_del(&sdata->u.vlan.list);
  1220. /* might already be clear but that doesn't matter */
  1221. clear_bit(SDATA_STATE_RUNNING, &sdata->state);
  1222. return res;
  1223. }
  1224. static void ieee80211_if_setup(struct net_device *dev)
  1225. {
  1226. ether_setup(dev);
  1227. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  1228. dev->priv_flags |= IFF_NO_QUEUE;
  1229. dev->netdev_ops = &ieee80211_dataif_ops;
  1230. dev->needs_free_netdev = true;
  1231. }
  1232. static void ieee80211_iface_process_skb(struct ieee80211_local *local,
  1233. struct ieee80211_sub_if_data *sdata,
  1234. struct sk_buff *skb)
  1235. {
  1236. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1237. lockdep_assert_wiphy(local->hw.wiphy);
  1238. if (ieee80211_is_action(mgmt->frame_control) &&
  1239. mgmt->u.action.category == WLAN_CATEGORY_BACK) {
  1240. struct sta_info *sta;
  1241. int len = skb->len;
  1242. sta = sta_info_get_bss(sdata, mgmt->sa);
  1243. if (sta) {
  1244. switch (mgmt->u.action.u.addba_req.action_code) {
  1245. case WLAN_ACTION_ADDBA_REQ:
  1246. ieee80211_process_addba_request(local, sta,
  1247. mgmt, len);
  1248. break;
  1249. case WLAN_ACTION_ADDBA_RESP:
  1250. ieee80211_process_addba_resp(local, sta,
  1251. mgmt, len);
  1252. break;
  1253. case WLAN_ACTION_DELBA:
  1254. ieee80211_process_delba(sdata, sta,
  1255. mgmt, len);
  1256. break;
  1257. default:
  1258. WARN_ON(1);
  1259. break;
  1260. }
  1261. }
  1262. } else if (ieee80211_is_action(mgmt->frame_control) &&
  1263. mgmt->u.action.category == WLAN_CATEGORY_VHT) {
  1264. switch (mgmt->u.action.u.vht_group_notif.action_code) {
  1265. case WLAN_VHT_ACTION_OPMODE_NOTIF: {
  1266. struct ieee80211_rx_status *status;
  1267. enum nl80211_band band;
  1268. struct sta_info *sta;
  1269. u8 opmode;
  1270. status = IEEE80211_SKB_RXCB(skb);
  1271. band = status->band;
  1272. opmode = mgmt->u.action.u.vht_opmode_notif.operating_mode;
  1273. sta = sta_info_get_bss(sdata, mgmt->sa);
  1274. if (sta)
  1275. ieee80211_vht_handle_opmode(sdata,
  1276. &sta->deflink,
  1277. opmode, band);
  1278. break;
  1279. }
  1280. case WLAN_VHT_ACTION_GROUPID_MGMT:
  1281. ieee80211_process_mu_groups(sdata, &sdata->deflink,
  1282. mgmt);
  1283. break;
  1284. default:
  1285. WARN_ON(1);
  1286. break;
  1287. }
  1288. } else if (ieee80211_is_action(mgmt->frame_control) &&
  1289. mgmt->u.action.category == WLAN_CATEGORY_S1G) {
  1290. switch (mgmt->u.action.u.s1g.action_code) {
  1291. case WLAN_S1G_TWT_TEARDOWN:
  1292. case WLAN_S1G_TWT_SETUP:
  1293. ieee80211_s1g_rx_twt_action(sdata, skb);
  1294. break;
  1295. default:
  1296. break;
  1297. }
  1298. } else if (ieee80211_is_action(mgmt->frame_control) &&
  1299. mgmt->u.action.category == WLAN_CATEGORY_PROTECTED_EHT) {
  1300. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  1301. switch (mgmt->u.action.u.ttlm_req.action_code) {
  1302. case WLAN_PROTECTED_EHT_ACTION_TTLM_REQ:
  1303. ieee80211_process_neg_ttlm_req(sdata, mgmt,
  1304. skb->len);
  1305. break;
  1306. case WLAN_PROTECTED_EHT_ACTION_TTLM_RES:
  1307. ieee80211_process_neg_ttlm_res(sdata, mgmt,
  1308. skb->len);
  1309. break;
  1310. default:
  1311. break;
  1312. }
  1313. }
  1314. } else if (ieee80211_is_ext(mgmt->frame_control)) {
  1315. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  1316. ieee80211_sta_rx_queued_ext(sdata, skb);
  1317. else
  1318. WARN_ON(1);
  1319. } else if (ieee80211_is_data_qos(mgmt->frame_control)) {
  1320. struct ieee80211_hdr *hdr = (void *)mgmt;
  1321. struct sta_info *sta;
  1322. /*
  1323. * So the frame isn't mgmt, but frame_control
  1324. * is at the right place anyway, of course, so
  1325. * the if statement is correct.
  1326. *
  1327. * Warn if we have other data frame types here,
  1328. * they must not get here.
  1329. */
  1330. WARN_ON(hdr->frame_control &
  1331. cpu_to_le16(IEEE80211_STYPE_NULLFUNC));
  1332. WARN_ON(!(hdr->seq_ctrl &
  1333. cpu_to_le16(IEEE80211_SCTL_FRAG)));
  1334. /*
  1335. * This was a fragment of a frame, received while
  1336. * a block-ack session was active. That cannot be
  1337. * right, so terminate the session.
  1338. */
  1339. sta = sta_info_get_bss(sdata, mgmt->sa);
  1340. if (sta) {
  1341. u16 tid = ieee80211_get_tid(hdr);
  1342. __ieee80211_stop_rx_ba_session(
  1343. sta, tid, WLAN_BACK_RECIPIENT,
  1344. WLAN_REASON_QSTA_REQUIRE_SETUP,
  1345. true);
  1346. }
  1347. } else switch (sdata->vif.type) {
  1348. case NL80211_IFTYPE_STATION:
  1349. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  1350. break;
  1351. case NL80211_IFTYPE_ADHOC:
  1352. ieee80211_ibss_rx_queued_mgmt(sdata, skb);
  1353. break;
  1354. case NL80211_IFTYPE_MESH_POINT:
  1355. if (!ieee80211_vif_is_mesh(&sdata->vif))
  1356. break;
  1357. ieee80211_mesh_rx_queued_mgmt(sdata, skb);
  1358. break;
  1359. default:
  1360. WARN(1, "frame for unexpected interface type");
  1361. break;
  1362. }
  1363. }
  1364. static void ieee80211_iface_process_status(struct ieee80211_sub_if_data *sdata,
  1365. struct sk_buff *skb)
  1366. {
  1367. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1368. if (ieee80211_is_action(mgmt->frame_control) &&
  1369. mgmt->u.action.category == WLAN_CATEGORY_S1G) {
  1370. switch (mgmt->u.action.u.s1g.action_code) {
  1371. case WLAN_S1G_TWT_TEARDOWN:
  1372. case WLAN_S1G_TWT_SETUP:
  1373. ieee80211_s1g_status_twt_action(sdata, skb);
  1374. break;
  1375. default:
  1376. break;
  1377. }
  1378. }
  1379. }
  1380. static void ieee80211_iface_work(struct wiphy *wiphy, struct wiphy_work *work)
  1381. {
  1382. struct ieee80211_sub_if_data *sdata =
  1383. container_of(work, struct ieee80211_sub_if_data, work);
  1384. struct ieee80211_local *local = sdata->local;
  1385. struct sk_buff *skb;
  1386. if (!ieee80211_sdata_running(sdata))
  1387. return;
  1388. if (test_bit(SCAN_SW_SCANNING, &local->scanning))
  1389. return;
  1390. if (!ieee80211_can_run_worker(local))
  1391. return;
  1392. /* first process frames */
  1393. while ((skb = skb_dequeue(&sdata->skb_queue))) {
  1394. kcov_remote_start_common(skb_get_kcov_handle(skb));
  1395. if (skb->protocol == cpu_to_be16(ETH_P_TDLS))
  1396. ieee80211_process_tdls_channel_switch(sdata, skb);
  1397. else
  1398. ieee80211_iface_process_skb(local, sdata, skb);
  1399. kfree_skb(skb);
  1400. kcov_remote_stop();
  1401. }
  1402. /* process status queue */
  1403. while ((skb = skb_dequeue(&sdata->status_queue))) {
  1404. kcov_remote_start_common(skb_get_kcov_handle(skb));
  1405. ieee80211_iface_process_status(sdata, skb);
  1406. kfree_skb(skb);
  1407. kcov_remote_stop();
  1408. }
  1409. /* then other type-dependent work */
  1410. switch (sdata->vif.type) {
  1411. case NL80211_IFTYPE_STATION:
  1412. ieee80211_sta_work(sdata);
  1413. break;
  1414. case NL80211_IFTYPE_ADHOC:
  1415. ieee80211_ibss_work(sdata);
  1416. break;
  1417. case NL80211_IFTYPE_MESH_POINT:
  1418. if (!ieee80211_vif_is_mesh(&sdata->vif))
  1419. break;
  1420. ieee80211_mesh_work(sdata);
  1421. break;
  1422. case NL80211_IFTYPE_OCB:
  1423. ieee80211_ocb_work(sdata);
  1424. break;
  1425. default:
  1426. break;
  1427. }
  1428. }
  1429. static void ieee80211_activate_links_work(struct wiphy *wiphy,
  1430. struct wiphy_work *work)
  1431. {
  1432. struct ieee80211_sub_if_data *sdata =
  1433. container_of(work, struct ieee80211_sub_if_data,
  1434. activate_links_work);
  1435. struct ieee80211_local *local = wiphy_priv(wiphy);
  1436. if (local->in_reconfig)
  1437. return;
  1438. ieee80211_set_active_links(&sdata->vif, sdata->desired_active_links);
  1439. sdata->desired_active_links = 0;
  1440. }
  1441. /*
  1442. * Helper function to initialise an interface to a specific type.
  1443. */
  1444. static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
  1445. enum nl80211_iftype type)
  1446. {
  1447. static const u8 bssid_wildcard[ETH_ALEN] = {0xff, 0xff, 0xff,
  1448. 0xff, 0xff, 0xff};
  1449. /* clear type-dependent unions */
  1450. memset(&sdata->u, 0, sizeof(sdata->u));
  1451. memset(&sdata->deflink.u, 0, sizeof(sdata->deflink.u));
  1452. /* and set some type-dependent values */
  1453. sdata->vif.type = type;
  1454. sdata->vif.p2p = false;
  1455. sdata->wdev.iftype = type;
  1456. sdata->control_port_protocol = cpu_to_be16(ETH_P_PAE);
  1457. sdata->control_port_no_encrypt = false;
  1458. sdata->control_port_over_nl80211 = false;
  1459. sdata->control_port_no_preauth = false;
  1460. sdata->vif.cfg.idle = true;
  1461. sdata->vif.bss_conf.txpower = INT_MIN; /* unset */
  1462. sdata->noack_map = 0;
  1463. /* only monitor/p2p-device differ */
  1464. if (sdata->dev) {
  1465. sdata->dev->netdev_ops = &ieee80211_dataif_ops;
  1466. sdata->dev->type = ARPHRD_ETHER;
  1467. }
  1468. skb_queue_head_init(&sdata->skb_queue);
  1469. skb_queue_head_init(&sdata->status_queue);
  1470. wiphy_work_init(&sdata->work, ieee80211_iface_work);
  1471. wiphy_work_init(&sdata->activate_links_work,
  1472. ieee80211_activate_links_work);
  1473. switch (type) {
  1474. case NL80211_IFTYPE_P2P_GO:
  1475. type = NL80211_IFTYPE_AP;
  1476. sdata->vif.type = type;
  1477. sdata->vif.p2p = true;
  1478. fallthrough;
  1479. case NL80211_IFTYPE_AP:
  1480. skb_queue_head_init(&sdata->u.ap.ps.bc_buf);
  1481. INIT_LIST_HEAD(&sdata->u.ap.vlans);
  1482. sdata->vif.bss_conf.bssid = sdata->vif.addr;
  1483. break;
  1484. case NL80211_IFTYPE_P2P_CLIENT:
  1485. type = NL80211_IFTYPE_STATION;
  1486. sdata->vif.type = type;
  1487. sdata->vif.p2p = true;
  1488. fallthrough;
  1489. case NL80211_IFTYPE_STATION:
  1490. sdata->vif.bss_conf.bssid = sdata->deflink.u.mgd.bssid;
  1491. ieee80211_sta_setup_sdata(sdata);
  1492. break;
  1493. case NL80211_IFTYPE_OCB:
  1494. sdata->vif.bss_conf.bssid = bssid_wildcard;
  1495. ieee80211_ocb_setup_sdata(sdata);
  1496. break;
  1497. case NL80211_IFTYPE_ADHOC:
  1498. sdata->vif.bss_conf.bssid = sdata->u.ibss.bssid;
  1499. ieee80211_ibss_setup_sdata(sdata);
  1500. break;
  1501. case NL80211_IFTYPE_MESH_POINT:
  1502. if (ieee80211_vif_is_mesh(&sdata->vif))
  1503. ieee80211_mesh_init_sdata(sdata);
  1504. break;
  1505. case NL80211_IFTYPE_MONITOR:
  1506. sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1507. sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
  1508. sdata->u.mntr.flags = MONITOR_FLAG_CONTROL |
  1509. MONITOR_FLAG_OTHER_BSS;
  1510. break;
  1511. case NL80211_IFTYPE_NAN:
  1512. idr_init(&sdata->u.nan.function_inst_ids);
  1513. spin_lock_init(&sdata->u.nan.func_lock);
  1514. sdata->vif.bss_conf.bssid = sdata->vif.addr;
  1515. break;
  1516. case NL80211_IFTYPE_AP_VLAN:
  1517. case NL80211_IFTYPE_P2P_DEVICE:
  1518. sdata->vif.bss_conf.bssid = sdata->vif.addr;
  1519. break;
  1520. case NL80211_IFTYPE_UNSPECIFIED:
  1521. case NL80211_IFTYPE_WDS:
  1522. case NUM_NL80211_IFTYPES:
  1523. WARN_ON(1);
  1524. break;
  1525. }
  1526. /* need to do this after the switch so vif.type is correct */
  1527. ieee80211_link_setup(&sdata->deflink);
  1528. ieee80211_debugfs_recreate_netdev(sdata, false);
  1529. }
  1530. static int ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data *sdata,
  1531. enum nl80211_iftype type)
  1532. {
  1533. struct ieee80211_local *local = sdata->local;
  1534. int ret, err;
  1535. enum nl80211_iftype internal_type = type;
  1536. bool p2p = false;
  1537. ASSERT_RTNL();
  1538. if (!local->ops->change_interface)
  1539. return -EBUSY;
  1540. /* for now, don't support changing while links exist */
  1541. if (ieee80211_vif_is_mld(&sdata->vif))
  1542. return -EBUSY;
  1543. switch (sdata->vif.type) {
  1544. case NL80211_IFTYPE_AP:
  1545. if (!list_empty(&sdata->u.ap.vlans))
  1546. return -EBUSY;
  1547. break;
  1548. case NL80211_IFTYPE_STATION:
  1549. case NL80211_IFTYPE_ADHOC:
  1550. case NL80211_IFTYPE_OCB:
  1551. /*
  1552. * Could maybe also all others here?
  1553. * Just not sure how that interacts
  1554. * with the RX/config path e.g. for
  1555. * mesh.
  1556. */
  1557. break;
  1558. default:
  1559. return -EBUSY;
  1560. }
  1561. switch (type) {
  1562. case NL80211_IFTYPE_AP:
  1563. case NL80211_IFTYPE_STATION:
  1564. case NL80211_IFTYPE_ADHOC:
  1565. case NL80211_IFTYPE_OCB:
  1566. /*
  1567. * Could probably support everything
  1568. * but here.
  1569. */
  1570. break;
  1571. case NL80211_IFTYPE_P2P_CLIENT:
  1572. p2p = true;
  1573. internal_type = NL80211_IFTYPE_STATION;
  1574. break;
  1575. case NL80211_IFTYPE_P2P_GO:
  1576. p2p = true;
  1577. internal_type = NL80211_IFTYPE_AP;
  1578. break;
  1579. default:
  1580. return -EBUSY;
  1581. }
  1582. ret = ieee80211_check_concurrent_iface(sdata, internal_type);
  1583. if (ret)
  1584. return ret;
  1585. ieee80211_stop_vif_queues(local, sdata,
  1586. IEEE80211_QUEUE_STOP_REASON_IFTYPE_CHANGE);
  1587. /* do_stop will synchronize_rcu() first thing */
  1588. ieee80211_do_stop(sdata, false);
  1589. ieee80211_teardown_sdata(sdata);
  1590. ieee80211_set_sdata_offload_flags(sdata);
  1591. ret = drv_change_interface(local, sdata, internal_type, p2p);
  1592. if (ret)
  1593. type = ieee80211_vif_type_p2p(&sdata->vif);
  1594. /*
  1595. * Ignore return value here, there's not much we can do since
  1596. * the driver changed the interface type internally already.
  1597. * The warnings will hopefully make driver authors fix it :-)
  1598. */
  1599. ieee80211_check_queues(sdata, type);
  1600. ieee80211_setup_sdata(sdata, type);
  1601. ieee80211_set_vif_encap_ops(sdata);
  1602. err = ieee80211_do_open(&sdata->wdev, false);
  1603. WARN(err, "type change: do_open returned %d", err);
  1604. ieee80211_wake_vif_queues(local, sdata,
  1605. IEEE80211_QUEUE_STOP_REASON_IFTYPE_CHANGE);
  1606. return ret;
  1607. }
  1608. int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
  1609. enum nl80211_iftype type)
  1610. {
  1611. int ret;
  1612. ASSERT_RTNL();
  1613. if (type == ieee80211_vif_type_p2p(&sdata->vif))
  1614. return 0;
  1615. if (ieee80211_sdata_running(sdata)) {
  1616. ret = ieee80211_runtime_change_iftype(sdata, type);
  1617. if (ret)
  1618. return ret;
  1619. } else {
  1620. /* Purge and reset type-dependent state. */
  1621. ieee80211_teardown_sdata(sdata);
  1622. ieee80211_setup_sdata(sdata, type);
  1623. }
  1624. /* reset some values that shouldn't be kept across type changes */
  1625. if (type == NL80211_IFTYPE_STATION)
  1626. sdata->u.mgd.use_4addr = false;
  1627. return 0;
  1628. }
  1629. static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
  1630. u8 *perm_addr, enum nl80211_iftype type)
  1631. {
  1632. struct ieee80211_sub_if_data *sdata;
  1633. u64 mask, start, addr, val, inc;
  1634. u8 *m;
  1635. u8 tmp_addr[ETH_ALEN];
  1636. int i;
  1637. lockdep_assert_wiphy(local->hw.wiphy);
  1638. /* default ... something at least */
  1639. memcpy(perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  1640. if (is_zero_ether_addr(local->hw.wiphy->addr_mask) &&
  1641. local->hw.wiphy->n_addresses <= 1)
  1642. return;
  1643. switch (type) {
  1644. case NL80211_IFTYPE_MONITOR:
  1645. /* doesn't matter */
  1646. break;
  1647. case NL80211_IFTYPE_AP_VLAN:
  1648. /* match up with an AP interface */
  1649. list_for_each_entry(sdata, &local->interfaces, list) {
  1650. if (sdata->vif.type != NL80211_IFTYPE_AP)
  1651. continue;
  1652. memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
  1653. break;
  1654. }
  1655. /* keep default if no AP interface present */
  1656. break;
  1657. case NL80211_IFTYPE_P2P_CLIENT:
  1658. case NL80211_IFTYPE_P2P_GO:
  1659. if (ieee80211_hw_check(&local->hw, P2P_DEV_ADDR_FOR_INTF)) {
  1660. list_for_each_entry(sdata, &local->interfaces, list) {
  1661. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE)
  1662. continue;
  1663. if (!ieee80211_sdata_running(sdata))
  1664. continue;
  1665. memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
  1666. return;
  1667. }
  1668. }
  1669. fallthrough;
  1670. default:
  1671. /* assign a new address if possible -- try n_addresses first */
  1672. for (i = 0; i < local->hw.wiphy->n_addresses; i++) {
  1673. bool used = false;
  1674. list_for_each_entry(sdata, &local->interfaces, list) {
  1675. if (ether_addr_equal(local->hw.wiphy->addresses[i].addr,
  1676. sdata->vif.addr)) {
  1677. used = true;
  1678. break;
  1679. }
  1680. }
  1681. if (!used) {
  1682. memcpy(perm_addr,
  1683. local->hw.wiphy->addresses[i].addr,
  1684. ETH_ALEN);
  1685. break;
  1686. }
  1687. }
  1688. /* try mask if available */
  1689. if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
  1690. break;
  1691. m = local->hw.wiphy->addr_mask;
  1692. mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  1693. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  1694. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  1695. if (__ffs64(mask) + hweight64(mask) != fls64(mask)) {
  1696. /* not a contiguous mask ... not handled now! */
  1697. pr_info("not contiguous\n");
  1698. break;
  1699. }
  1700. /*
  1701. * Pick address of existing interface in case user changed
  1702. * MAC address manually, default to perm_addr.
  1703. */
  1704. m = local->hw.wiphy->perm_addr;
  1705. list_for_each_entry(sdata, &local->interfaces, list) {
  1706. if (sdata->vif.type == NL80211_IFTYPE_MONITOR)
  1707. continue;
  1708. m = sdata->vif.addr;
  1709. break;
  1710. }
  1711. start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  1712. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  1713. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  1714. inc = 1ULL<<__ffs64(mask);
  1715. val = (start & mask);
  1716. addr = (start & ~mask) | (val & mask);
  1717. do {
  1718. bool used = false;
  1719. tmp_addr[5] = addr >> 0*8;
  1720. tmp_addr[4] = addr >> 1*8;
  1721. tmp_addr[3] = addr >> 2*8;
  1722. tmp_addr[2] = addr >> 3*8;
  1723. tmp_addr[1] = addr >> 4*8;
  1724. tmp_addr[0] = addr >> 5*8;
  1725. val += inc;
  1726. list_for_each_entry(sdata, &local->interfaces, list) {
  1727. if (ether_addr_equal(tmp_addr, sdata->vif.addr)) {
  1728. used = true;
  1729. break;
  1730. }
  1731. }
  1732. if (!used) {
  1733. memcpy(perm_addr, tmp_addr, ETH_ALEN);
  1734. break;
  1735. }
  1736. addr = (start & ~mask) | (val & mask);
  1737. } while (addr != start);
  1738. break;
  1739. }
  1740. }
  1741. int ieee80211_if_add(struct ieee80211_local *local, const char *name,
  1742. unsigned char name_assign_type,
  1743. struct wireless_dev **new_wdev, enum nl80211_iftype type,
  1744. struct vif_params *params)
  1745. {
  1746. struct net_device *ndev = NULL;
  1747. struct ieee80211_sub_if_data *sdata = NULL;
  1748. struct txq_info *txqi;
  1749. int ret, i;
  1750. ASSERT_RTNL();
  1751. lockdep_assert_wiphy(local->hw.wiphy);
  1752. if (type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN) {
  1753. struct wireless_dev *wdev;
  1754. sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size,
  1755. GFP_KERNEL);
  1756. if (!sdata)
  1757. return -ENOMEM;
  1758. wdev = &sdata->wdev;
  1759. sdata->dev = NULL;
  1760. strscpy(sdata->name, name, IFNAMSIZ);
  1761. ieee80211_assign_perm_addr(local, wdev->address, type);
  1762. memcpy(sdata->vif.addr, wdev->address, ETH_ALEN);
  1763. ether_addr_copy(sdata->vif.bss_conf.addr, sdata->vif.addr);
  1764. } else {
  1765. int size = ALIGN(sizeof(*sdata) + local->hw.vif_data_size,
  1766. sizeof(void *));
  1767. int txq_size = 0;
  1768. if (type != NL80211_IFTYPE_AP_VLAN &&
  1769. (type != NL80211_IFTYPE_MONITOR ||
  1770. (params->flags & MONITOR_FLAG_ACTIVE)))
  1771. txq_size += sizeof(struct txq_info) +
  1772. local->hw.txq_data_size;
  1773. ndev = alloc_netdev_mqs(size + txq_size,
  1774. name, name_assign_type,
  1775. ieee80211_if_setup, 1, 1);
  1776. if (!ndev)
  1777. return -ENOMEM;
  1778. dev_net_set(ndev, wiphy_net(local->hw.wiphy));
  1779. ndev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
  1780. ndev->needed_headroom = local->tx_headroom +
  1781. 4*6 /* four MAC addresses */
  1782. + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
  1783. + 6 /* mesh */
  1784. + 8 /* rfc1042/bridge tunnel */
  1785. - ETH_HLEN /* ethernet hard_header_len */
  1786. + IEEE80211_ENCRYPT_HEADROOM;
  1787. ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
  1788. ret = dev_alloc_name(ndev, ndev->name);
  1789. if (ret < 0) {
  1790. free_netdev(ndev);
  1791. return ret;
  1792. }
  1793. ieee80211_assign_perm_addr(local, ndev->perm_addr, type);
  1794. if (is_valid_ether_addr(params->macaddr))
  1795. eth_hw_addr_set(ndev, params->macaddr);
  1796. else
  1797. eth_hw_addr_set(ndev, ndev->perm_addr);
  1798. SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
  1799. /* don't use IEEE80211_DEV_TO_SUB_IF -- it checks too much */
  1800. sdata = netdev_priv(ndev);
  1801. ndev->ieee80211_ptr = &sdata->wdev;
  1802. memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
  1803. ether_addr_copy(sdata->vif.bss_conf.addr, sdata->vif.addr);
  1804. memcpy(sdata->name, ndev->name, IFNAMSIZ);
  1805. if (txq_size) {
  1806. txqi = netdev_priv(ndev) + size;
  1807. ieee80211_txq_init(sdata, NULL, txqi, 0);
  1808. }
  1809. sdata->dev = ndev;
  1810. }
  1811. /* initialise type-independent data */
  1812. sdata->wdev.wiphy = local->hw.wiphy;
  1813. ieee80211_sdata_init(local, sdata);
  1814. ieee80211_init_frag_cache(&sdata->frags);
  1815. INIT_LIST_HEAD(&sdata->key_list);
  1816. wiphy_delayed_work_init(&sdata->dec_tailroom_needed_wk,
  1817. ieee80211_delayed_tailroom_dec);
  1818. for (i = 0; i < NUM_NL80211_BANDS; i++) {
  1819. struct ieee80211_supported_band *sband;
  1820. sband = local->hw.wiphy->bands[i];
  1821. sdata->rc_rateidx_mask[i] =
  1822. sband ? (1 << sband->n_bitrates) - 1 : 0;
  1823. if (sband) {
  1824. __le16 cap;
  1825. u16 *vht_rate_mask;
  1826. memcpy(sdata->rc_rateidx_mcs_mask[i],
  1827. sband->ht_cap.mcs.rx_mask,
  1828. sizeof(sdata->rc_rateidx_mcs_mask[i]));
  1829. cap = sband->vht_cap.vht_mcs.rx_mcs_map;
  1830. vht_rate_mask = sdata->rc_rateidx_vht_mcs_mask[i];
  1831. ieee80211_get_vht_mask_from_cap(cap, vht_rate_mask);
  1832. } else {
  1833. memset(sdata->rc_rateidx_mcs_mask[i], 0,
  1834. sizeof(sdata->rc_rateidx_mcs_mask[i]));
  1835. memset(sdata->rc_rateidx_vht_mcs_mask[i], 0,
  1836. sizeof(sdata->rc_rateidx_vht_mcs_mask[i]));
  1837. }
  1838. }
  1839. ieee80211_set_default_queues(sdata);
  1840. sdata->deflink.ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
  1841. sdata->deflink.user_power_level = local->user_power_level;
  1842. /* setup type-dependent data */
  1843. ieee80211_setup_sdata(sdata, type);
  1844. if (ndev) {
  1845. ndev->ieee80211_ptr->use_4addr = params->use_4addr;
  1846. if (type == NL80211_IFTYPE_STATION)
  1847. sdata->u.mgd.use_4addr = params->use_4addr;
  1848. ndev->features |= local->hw.netdev_features;
  1849. ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1850. ndev->hw_features |= ndev->features &
  1851. MAC80211_SUPPORTED_FEATURES_TX;
  1852. sdata->vif.netdev_features = local->hw.netdev_features;
  1853. netdev_set_default_ethtool_ops(ndev, &ieee80211_ethtool_ops);
  1854. /* MTU range is normally 256 - 2304, where the upper limit is
  1855. * the maximum MSDU size. Monitor interfaces send and receive
  1856. * MPDU and A-MSDU frames which may be much larger so we do
  1857. * not impose an upper limit in that case.
  1858. */
  1859. ndev->min_mtu = 256;
  1860. if (type == NL80211_IFTYPE_MONITOR)
  1861. ndev->max_mtu = 0;
  1862. else
  1863. ndev->max_mtu = local->hw.max_mtu;
  1864. ret = cfg80211_register_netdevice(ndev);
  1865. if (ret) {
  1866. free_netdev(ndev);
  1867. return ret;
  1868. }
  1869. }
  1870. mutex_lock(&local->iflist_mtx);
  1871. list_add_tail_rcu(&sdata->list, &local->interfaces);
  1872. mutex_unlock(&local->iflist_mtx);
  1873. if (new_wdev)
  1874. *new_wdev = &sdata->wdev;
  1875. return 0;
  1876. }
  1877. void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
  1878. {
  1879. ASSERT_RTNL();
  1880. lockdep_assert_wiphy(sdata->local->hw.wiphy);
  1881. mutex_lock(&sdata->local->iflist_mtx);
  1882. list_del_rcu(&sdata->list);
  1883. mutex_unlock(&sdata->local->iflist_mtx);
  1884. if (sdata->vif.txq)
  1885. ieee80211_txq_purge(sdata->local, to_txq_info(sdata->vif.txq));
  1886. synchronize_rcu();
  1887. cfg80211_unregister_wdev(&sdata->wdev);
  1888. if (!sdata->dev) {
  1889. ieee80211_teardown_sdata(sdata);
  1890. kfree(sdata);
  1891. }
  1892. }
  1893. void ieee80211_sdata_stop(struct ieee80211_sub_if_data *sdata)
  1894. {
  1895. if (WARN_ON_ONCE(!test_bit(SDATA_STATE_RUNNING, &sdata->state)))
  1896. return;
  1897. ieee80211_do_stop(sdata, true);
  1898. }
  1899. void ieee80211_remove_interfaces(struct ieee80211_local *local)
  1900. {
  1901. struct ieee80211_sub_if_data *sdata, *tmp;
  1902. LIST_HEAD(unreg_list);
  1903. ASSERT_RTNL();
  1904. /* Before destroying the interfaces, make sure they're all stopped so
  1905. * that the hardware is stopped. Otherwise, the driver might still be
  1906. * iterating the interfaces during the shutdown, e.g. from a worker
  1907. * or from RX processing or similar, and if it does so (using atomic
  1908. * iteration) while we're manipulating the list, the iteration will
  1909. * crash.
  1910. *
  1911. * After this, the hardware should be stopped and the driver should
  1912. * have stopped all of its activities, so that we can do RCU-unaware
  1913. * manipulations of the interface list below.
  1914. */
  1915. cfg80211_shutdown_all_interfaces(local->hw.wiphy);
  1916. wiphy_lock(local->hw.wiphy);
  1917. WARN(local->open_count, "%s: open count remains %d\n",
  1918. wiphy_name(local->hw.wiphy), local->open_count);
  1919. mutex_lock(&local->iflist_mtx);
  1920. list_splice_init(&local->interfaces, &unreg_list);
  1921. mutex_unlock(&local->iflist_mtx);
  1922. list_for_each_entry_safe(sdata, tmp, &unreg_list, list) {
  1923. bool netdev = sdata->dev;
  1924. /*
  1925. * Remove IP addresses explicitly, since the notifier will
  1926. * skip the callbacks if wdev->registered is false, since
  1927. * we can't acquire the wiphy_lock() again there if already
  1928. * inside this locked section.
  1929. */
  1930. sdata->vif.cfg.arp_addr_cnt = 0;
  1931. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  1932. sdata->u.mgd.associated)
  1933. ieee80211_vif_cfg_change_notify(sdata,
  1934. BSS_CHANGED_ARP_FILTER);
  1935. list_del(&sdata->list);
  1936. cfg80211_unregister_wdev(&sdata->wdev);
  1937. if (!netdev)
  1938. kfree(sdata);
  1939. }
  1940. wiphy_unlock(local->hw.wiphy);
  1941. }
  1942. static int netdev_notify(struct notifier_block *nb,
  1943. unsigned long state, void *ptr)
  1944. {
  1945. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1946. struct ieee80211_sub_if_data *sdata;
  1947. if (state != NETDEV_CHANGENAME)
  1948. return NOTIFY_DONE;
  1949. if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
  1950. return NOTIFY_DONE;
  1951. if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
  1952. return NOTIFY_DONE;
  1953. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1954. memcpy(sdata->name, dev->name, IFNAMSIZ);
  1955. ieee80211_debugfs_rename_netdev(sdata);
  1956. return NOTIFY_OK;
  1957. }
  1958. static struct notifier_block mac80211_netdev_notifier = {
  1959. .notifier_call = netdev_notify,
  1960. };
  1961. int ieee80211_iface_init(void)
  1962. {
  1963. return register_netdevice_notifier(&mac80211_netdev_notifier);
  1964. }
  1965. void ieee80211_iface_exit(void)
  1966. {
  1967. unregister_netdevice_notifier(&mac80211_netdev_notifier);
  1968. }
  1969. void ieee80211_vif_inc_num_mcast(struct ieee80211_sub_if_data *sdata)
  1970. {
  1971. if (sdata->vif.type == NL80211_IFTYPE_AP)
  1972. atomic_inc(&sdata->u.ap.num_mcast_sta);
  1973. else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1974. atomic_inc(&sdata->u.vlan.num_mcast_sta);
  1975. }
  1976. void ieee80211_vif_dec_num_mcast(struct ieee80211_sub_if_data *sdata)
  1977. {
  1978. if (sdata->vif.type == NL80211_IFTYPE_AP)
  1979. atomic_dec(&sdata->u.ap.num_mcast_sta);
  1980. else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1981. atomic_dec(&sdata->u.vlan.num_mcast_sta);
  1982. }
  1983. void ieee80211_vif_block_queues_csa(struct ieee80211_sub_if_data *sdata)
  1984. {
  1985. struct ieee80211_local *local = sdata->local;
  1986. if (ieee80211_hw_check(&local->hw, HANDLES_QUIET_CSA))
  1987. return;
  1988. ieee80211_stop_vif_queues_norefcount(local, sdata,
  1989. IEEE80211_QUEUE_STOP_REASON_CSA);
  1990. }
  1991. void ieee80211_vif_unblock_queues_csa(struct ieee80211_sub_if_data *sdata)
  1992. {
  1993. struct ieee80211_local *local = sdata->local;
  1994. ieee80211_wake_vif_queues_norefcount(local, sdata,
  1995. IEEE80211_QUEUE_STOP_REASON_CSA);
  1996. }