iface.c 63 KB

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