iface.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  1. /*
  2. * Interface handling
  3. *
  4. * Copyright 2002-2005, Instant802 Networks, Inc.
  5. * Copyright 2005-2006, Devicescape Software, Inc.
  6. * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
  7. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  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 <net/mac80211.h>
  19. #include <net/ieee80211_radiotap.h>
  20. #include "ieee80211_i.h"
  21. #include "sta_info.h"
  22. #include "debugfs_netdev.h"
  23. #include "mesh.h"
  24. #include "led.h"
  25. #include "driver-ops.h"
  26. #include "wme.h"
  27. #include "rate.h"
  28. /**
  29. * DOC: Interface list locking
  30. *
  31. * The interface list in each struct ieee80211_local is protected
  32. * three-fold:
  33. *
  34. * (1) modifications may only be done under the RTNL
  35. * (2) modifications and readers are protected against each other by
  36. * the iflist_mtx.
  37. * (3) 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 iflist_mtx or RCU.
  42. */
  43. bool __ieee80211_recalc_txpower(struct ieee80211_sub_if_data *sdata)
  44. {
  45. struct ieee80211_chanctx_conf *chanctx_conf;
  46. int power;
  47. rcu_read_lock();
  48. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  49. if (!chanctx_conf) {
  50. rcu_read_unlock();
  51. return false;
  52. }
  53. power = ieee80211_chandef_max_power(&chanctx_conf->def);
  54. rcu_read_unlock();
  55. if (sdata->user_power_level != IEEE80211_UNSET_POWER_LEVEL)
  56. power = min(power, sdata->user_power_level);
  57. if (sdata->ap_power_level != IEEE80211_UNSET_POWER_LEVEL)
  58. power = min(power, sdata->ap_power_level);
  59. if (power != sdata->vif.bss_conf.txpower) {
  60. sdata->vif.bss_conf.txpower = power;
  61. ieee80211_hw_config(sdata->local, 0);
  62. return true;
  63. }
  64. return false;
  65. }
  66. void ieee80211_recalc_txpower(struct ieee80211_sub_if_data *sdata)
  67. {
  68. if (__ieee80211_recalc_txpower(sdata))
  69. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_TXPOWER);
  70. }
  71. static u32 __ieee80211_idle_off(struct ieee80211_local *local)
  72. {
  73. if (!(local->hw.conf.flags & IEEE80211_CONF_IDLE))
  74. return 0;
  75. local->hw.conf.flags &= ~IEEE80211_CONF_IDLE;
  76. return IEEE80211_CONF_CHANGE_IDLE;
  77. }
  78. static u32 __ieee80211_idle_on(struct ieee80211_local *local)
  79. {
  80. if (local->hw.conf.flags & IEEE80211_CONF_IDLE)
  81. return 0;
  82. ieee80211_flush_queues(local, NULL);
  83. local->hw.conf.flags |= IEEE80211_CONF_IDLE;
  84. return IEEE80211_CONF_CHANGE_IDLE;
  85. }
  86. static u32 __ieee80211_recalc_idle(struct ieee80211_local *local,
  87. bool force_active)
  88. {
  89. bool working, scanning, active;
  90. unsigned int led_trig_start = 0, led_trig_stop = 0;
  91. lockdep_assert_held(&local->mtx);
  92. active = force_active ||
  93. !list_empty(&local->chanctx_list) ||
  94. local->monitors;
  95. working = !local->ops->remain_on_channel &&
  96. !list_empty(&local->roc_list);
  97. scanning = test_bit(SCAN_SW_SCANNING, &local->scanning) ||
  98. test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
  99. if (working || scanning)
  100. led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_WORK;
  101. else
  102. led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_WORK;
  103. if (active)
  104. led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED;
  105. else
  106. led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED;
  107. ieee80211_mod_tpt_led_trig(local, led_trig_start, led_trig_stop);
  108. if (working || scanning || active)
  109. return __ieee80211_idle_off(local);
  110. return __ieee80211_idle_on(local);
  111. }
  112. u32 ieee80211_idle_off(struct ieee80211_local *local)
  113. {
  114. return __ieee80211_recalc_idle(local, true);
  115. }
  116. void ieee80211_recalc_idle(struct ieee80211_local *local)
  117. {
  118. u32 change = __ieee80211_recalc_idle(local, false);
  119. if (change)
  120. ieee80211_hw_config(local, change);
  121. }
  122. static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
  123. {
  124. if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN)
  125. return -EINVAL;
  126. dev->mtu = new_mtu;
  127. return 0;
  128. }
  129. static int ieee80211_verify_mac(struct ieee80211_sub_if_data *sdata, u8 *addr,
  130. bool check_dup)
  131. {
  132. struct ieee80211_local *local = sdata->local;
  133. struct ieee80211_sub_if_data *iter;
  134. u64 new, mask, tmp;
  135. u8 *m;
  136. int ret = 0;
  137. if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
  138. return 0;
  139. m = addr;
  140. new = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  141. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  142. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  143. m = local->hw.wiphy->addr_mask;
  144. mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  145. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  146. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  147. if (!check_dup)
  148. return ret;
  149. mutex_lock(&local->iflist_mtx);
  150. list_for_each_entry(iter, &local->interfaces, list) {
  151. if (iter == sdata)
  152. continue;
  153. if (iter->vif.type == NL80211_IFTYPE_MONITOR &&
  154. !(iter->u.mntr_flags & MONITOR_FLAG_ACTIVE))
  155. continue;
  156. m = iter->vif.addr;
  157. tmp = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  158. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  159. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  160. if ((new & ~mask) != (tmp & ~mask)) {
  161. ret = -EINVAL;
  162. break;
  163. }
  164. }
  165. mutex_unlock(&local->iflist_mtx);
  166. return ret;
  167. }
  168. static int ieee80211_change_mac(struct net_device *dev, void *addr)
  169. {
  170. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  171. struct sockaddr *sa = addr;
  172. bool check_dup = true;
  173. int ret;
  174. if (ieee80211_sdata_running(sdata))
  175. return -EBUSY;
  176. if (sdata->vif.type == NL80211_IFTYPE_MONITOR &&
  177. !(sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE))
  178. check_dup = false;
  179. ret = ieee80211_verify_mac(sdata, sa->sa_data, check_dup);
  180. if (ret)
  181. return ret;
  182. ret = eth_mac_addr(dev, sa);
  183. if (ret == 0)
  184. memcpy(sdata->vif.addr, sa->sa_data, ETH_ALEN);
  185. return ret;
  186. }
  187. static inline int identical_mac_addr_allowed(int type1, int type2)
  188. {
  189. return type1 == NL80211_IFTYPE_MONITOR ||
  190. type2 == NL80211_IFTYPE_MONITOR ||
  191. type1 == NL80211_IFTYPE_P2P_DEVICE ||
  192. type2 == NL80211_IFTYPE_P2P_DEVICE ||
  193. (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_WDS) ||
  194. (type1 == NL80211_IFTYPE_WDS &&
  195. (type2 == NL80211_IFTYPE_WDS ||
  196. type2 == NL80211_IFTYPE_AP)) ||
  197. (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_AP_VLAN) ||
  198. (type1 == NL80211_IFTYPE_AP_VLAN &&
  199. (type2 == NL80211_IFTYPE_AP ||
  200. type2 == NL80211_IFTYPE_AP_VLAN));
  201. }
  202. static int ieee80211_check_concurrent_iface(struct ieee80211_sub_if_data *sdata,
  203. enum nl80211_iftype iftype)
  204. {
  205. struct ieee80211_local *local = sdata->local;
  206. struct ieee80211_sub_if_data *nsdata;
  207. int ret;
  208. ASSERT_RTNL();
  209. /* we hold the RTNL here so can safely walk the list */
  210. list_for_each_entry(nsdata, &local->interfaces, list) {
  211. if (nsdata != sdata && ieee80211_sdata_running(nsdata)) {
  212. /*
  213. * Allow only a single IBSS interface to be up at any
  214. * time. This is restricted because beacon distribution
  215. * cannot work properly if both are in the same IBSS.
  216. *
  217. * To remove this restriction we'd have to disallow them
  218. * from setting the same SSID on different IBSS interfaces
  219. * belonging to the same hardware. Then, however, we're
  220. * faced with having to adopt two different TSF timers...
  221. */
  222. if (iftype == NL80211_IFTYPE_ADHOC &&
  223. nsdata->vif.type == NL80211_IFTYPE_ADHOC)
  224. return -EBUSY;
  225. /*
  226. * will not add another interface while any channel
  227. * switch is active.
  228. */
  229. if (nsdata->vif.csa_active)
  230. return -EBUSY;
  231. /*
  232. * The remaining checks are only performed for interfaces
  233. * with the same MAC address.
  234. */
  235. if (!ether_addr_equal(sdata->vif.addr,
  236. nsdata->vif.addr))
  237. continue;
  238. /*
  239. * check whether it may have the same address
  240. */
  241. if (!identical_mac_addr_allowed(iftype,
  242. nsdata->vif.type))
  243. return -ENOTUNIQ;
  244. /*
  245. * can only add VLANs to enabled APs
  246. */
  247. if (iftype == NL80211_IFTYPE_AP_VLAN &&
  248. nsdata->vif.type == NL80211_IFTYPE_AP)
  249. sdata->bss = &nsdata->u.ap;
  250. }
  251. }
  252. mutex_lock(&local->chanctx_mtx);
  253. ret = ieee80211_check_combinations(sdata, NULL, 0, 0);
  254. mutex_unlock(&local->chanctx_mtx);
  255. return ret;
  256. }
  257. static int ieee80211_check_queues(struct ieee80211_sub_if_data *sdata,
  258. enum nl80211_iftype iftype)
  259. {
  260. int n_queues = sdata->local->hw.queues;
  261. int i;
  262. if (iftype != NL80211_IFTYPE_P2P_DEVICE) {
  263. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  264. if (WARN_ON_ONCE(sdata->vif.hw_queue[i] ==
  265. IEEE80211_INVAL_HW_QUEUE))
  266. return -EINVAL;
  267. if (WARN_ON_ONCE(sdata->vif.hw_queue[i] >=
  268. n_queues))
  269. return -EINVAL;
  270. }
  271. }
  272. if ((iftype != NL80211_IFTYPE_AP &&
  273. iftype != NL80211_IFTYPE_P2P_GO &&
  274. iftype != NL80211_IFTYPE_MESH_POINT) ||
  275. !(sdata->local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)) {
  276. sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE;
  277. return 0;
  278. }
  279. if (WARN_ON_ONCE(sdata->vif.cab_queue == IEEE80211_INVAL_HW_QUEUE))
  280. return -EINVAL;
  281. if (WARN_ON_ONCE(sdata->vif.cab_queue >= n_queues))
  282. return -EINVAL;
  283. return 0;
  284. }
  285. void ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data *sdata,
  286. const int offset)
  287. {
  288. struct ieee80211_local *local = sdata->local;
  289. u32 flags = sdata->u.mntr_flags;
  290. #define ADJUST(_f, _s) do { \
  291. if (flags & MONITOR_FLAG_##_f) \
  292. local->fif_##_s += offset; \
  293. } while (0)
  294. ADJUST(FCSFAIL, fcsfail);
  295. ADJUST(PLCPFAIL, plcpfail);
  296. ADJUST(CONTROL, control);
  297. ADJUST(CONTROL, pspoll);
  298. ADJUST(OTHER_BSS, other_bss);
  299. #undef ADJUST
  300. }
  301. static void ieee80211_set_default_queues(struct ieee80211_sub_if_data *sdata)
  302. {
  303. struct ieee80211_local *local = sdata->local;
  304. int i;
  305. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  306. if (local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)
  307. sdata->vif.hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
  308. else if (local->hw.queues >= IEEE80211_NUM_ACS)
  309. sdata->vif.hw_queue[i] = i;
  310. else
  311. sdata->vif.hw_queue[i] = 0;
  312. }
  313. sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE;
  314. }
  315. int ieee80211_add_virtual_monitor(struct ieee80211_local *local)
  316. {
  317. struct ieee80211_sub_if_data *sdata;
  318. int ret;
  319. if (!(local->hw.flags & IEEE80211_HW_WANT_MONITOR_VIF))
  320. return 0;
  321. ASSERT_RTNL();
  322. if (local->monitor_sdata)
  323. return 0;
  324. sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size, GFP_KERNEL);
  325. if (!sdata)
  326. return -ENOMEM;
  327. /* set up data */
  328. sdata->local = local;
  329. sdata->vif.type = NL80211_IFTYPE_MONITOR;
  330. snprintf(sdata->name, IFNAMSIZ, "%s-monitor",
  331. wiphy_name(local->hw.wiphy));
  332. sdata->wdev.iftype = NL80211_IFTYPE_MONITOR;
  333. sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
  334. ieee80211_set_default_queues(sdata);
  335. ret = drv_add_interface(local, sdata);
  336. if (WARN_ON(ret)) {
  337. /* ok .. stupid driver, it asked for this! */
  338. kfree(sdata);
  339. return ret;
  340. }
  341. ret = ieee80211_check_queues(sdata, NL80211_IFTYPE_MONITOR);
  342. if (ret) {
  343. kfree(sdata);
  344. return ret;
  345. }
  346. mutex_lock(&local->iflist_mtx);
  347. rcu_assign_pointer(local->monitor_sdata, sdata);
  348. mutex_unlock(&local->iflist_mtx);
  349. mutex_lock(&local->mtx);
  350. ret = ieee80211_vif_use_channel(sdata, &local->monitor_chandef,
  351. IEEE80211_CHANCTX_EXCLUSIVE);
  352. mutex_unlock(&local->mtx);
  353. if (ret) {
  354. mutex_lock(&local->iflist_mtx);
  355. RCU_INIT_POINTER(local->monitor_sdata, NULL);
  356. mutex_unlock(&local->iflist_mtx);
  357. synchronize_net();
  358. drv_remove_interface(local, sdata);
  359. kfree(sdata);
  360. return ret;
  361. }
  362. return 0;
  363. }
  364. void ieee80211_del_virtual_monitor(struct ieee80211_local *local)
  365. {
  366. struct ieee80211_sub_if_data *sdata;
  367. if (!(local->hw.flags & IEEE80211_HW_WANT_MONITOR_VIF))
  368. return;
  369. ASSERT_RTNL();
  370. mutex_lock(&local->iflist_mtx);
  371. sdata = rcu_dereference_protected(local->monitor_sdata,
  372. lockdep_is_held(&local->iflist_mtx));
  373. if (!sdata) {
  374. mutex_unlock(&local->iflist_mtx);
  375. return;
  376. }
  377. RCU_INIT_POINTER(local->monitor_sdata, NULL);
  378. mutex_unlock(&local->iflist_mtx);
  379. synchronize_net();
  380. mutex_lock(&local->mtx);
  381. ieee80211_vif_release_channel(sdata);
  382. mutex_unlock(&local->mtx);
  383. drv_remove_interface(local, sdata);
  384. kfree(sdata);
  385. }
  386. /*
  387. * NOTE: Be very careful when changing this function, it must NOT return
  388. * an error on interface type changes that have been pre-checked, so most
  389. * checks should be in ieee80211_check_concurrent_iface.
  390. */
  391. int ieee80211_do_open(struct wireless_dev *wdev, bool coming_up)
  392. {
  393. struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
  394. struct net_device *dev = wdev->netdev;
  395. struct ieee80211_local *local = sdata->local;
  396. struct sta_info *sta;
  397. u32 changed = 0;
  398. int res;
  399. u32 hw_reconf_flags = 0;
  400. switch (sdata->vif.type) {
  401. case NL80211_IFTYPE_WDS:
  402. if (!is_valid_ether_addr(sdata->u.wds.remote_addr))
  403. return -ENOLINK;
  404. break;
  405. case NL80211_IFTYPE_AP_VLAN: {
  406. struct ieee80211_sub_if_data *master;
  407. if (!sdata->bss)
  408. return -ENOLINK;
  409. mutex_lock(&local->mtx);
  410. list_add(&sdata->u.vlan.list, &sdata->bss->vlans);
  411. mutex_unlock(&local->mtx);
  412. master = container_of(sdata->bss,
  413. struct ieee80211_sub_if_data, u.ap);
  414. sdata->control_port_protocol =
  415. master->control_port_protocol;
  416. sdata->control_port_no_encrypt =
  417. master->control_port_no_encrypt;
  418. sdata->vif.cab_queue = master->vif.cab_queue;
  419. memcpy(sdata->vif.hw_queue, master->vif.hw_queue,
  420. sizeof(sdata->vif.hw_queue));
  421. break;
  422. }
  423. case NL80211_IFTYPE_AP:
  424. sdata->bss = &sdata->u.ap;
  425. break;
  426. case NL80211_IFTYPE_MESH_POINT:
  427. case NL80211_IFTYPE_STATION:
  428. case NL80211_IFTYPE_MONITOR:
  429. case NL80211_IFTYPE_ADHOC:
  430. case NL80211_IFTYPE_P2P_DEVICE:
  431. /* no special treatment */
  432. break;
  433. case NL80211_IFTYPE_UNSPECIFIED:
  434. case NUM_NL80211_IFTYPES:
  435. case NL80211_IFTYPE_P2P_CLIENT:
  436. case NL80211_IFTYPE_P2P_GO:
  437. /* cannot happen */
  438. WARN_ON(1);
  439. break;
  440. }
  441. if (local->open_count == 0) {
  442. res = drv_start(local);
  443. if (res)
  444. goto err_del_bss;
  445. /* we're brought up, everything changes */
  446. hw_reconf_flags = ~0;
  447. ieee80211_led_radio(local, true);
  448. ieee80211_mod_tpt_led_trig(local,
  449. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  450. }
  451. /*
  452. * Copy the hopefully now-present MAC address to
  453. * this interface, if it has the special null one.
  454. */
  455. if (dev && is_zero_ether_addr(dev->dev_addr)) {
  456. memcpy(dev->dev_addr,
  457. local->hw.wiphy->perm_addr,
  458. ETH_ALEN);
  459. memcpy(dev->perm_addr, dev->dev_addr, ETH_ALEN);
  460. if (!is_valid_ether_addr(dev->dev_addr)) {
  461. res = -EADDRNOTAVAIL;
  462. goto err_stop;
  463. }
  464. }
  465. switch (sdata->vif.type) {
  466. case NL80211_IFTYPE_AP_VLAN:
  467. /* no need to tell driver, but set carrier and chanctx */
  468. if (rtnl_dereference(sdata->bss->beacon)) {
  469. ieee80211_vif_vlan_copy_chanctx(sdata);
  470. netif_carrier_on(dev);
  471. } else {
  472. netif_carrier_off(dev);
  473. }
  474. break;
  475. case NL80211_IFTYPE_MONITOR:
  476. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  477. local->cooked_mntrs++;
  478. break;
  479. }
  480. if (sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE) {
  481. res = drv_add_interface(local, sdata);
  482. if (res)
  483. goto err_stop;
  484. } else if (local->monitors == 0 && local->open_count == 0) {
  485. res = ieee80211_add_virtual_monitor(local);
  486. if (res)
  487. goto err_stop;
  488. }
  489. /* must be before the call to ieee80211_configure_filter */
  490. local->monitors++;
  491. if (local->monitors == 1) {
  492. local->hw.conf.flags |= IEEE80211_CONF_MONITOR;
  493. hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
  494. }
  495. ieee80211_adjust_monitor_flags(sdata, 1);
  496. ieee80211_configure_filter(local);
  497. mutex_lock(&local->mtx);
  498. ieee80211_recalc_idle(local);
  499. mutex_unlock(&local->mtx);
  500. netif_carrier_on(dev);
  501. break;
  502. default:
  503. if (coming_up) {
  504. ieee80211_del_virtual_monitor(local);
  505. res = drv_add_interface(local, sdata);
  506. if (res)
  507. goto err_stop;
  508. res = ieee80211_check_queues(sdata,
  509. ieee80211_vif_type_p2p(&sdata->vif));
  510. if (res)
  511. goto err_del_interface;
  512. }
  513. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  514. local->fif_pspoll++;
  515. local->fif_probe_req++;
  516. ieee80211_configure_filter(local);
  517. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  518. local->fif_probe_req++;
  519. }
  520. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE)
  521. changed |= ieee80211_reset_erp_info(sdata);
  522. ieee80211_bss_info_change_notify(sdata, changed);
  523. switch (sdata->vif.type) {
  524. case NL80211_IFTYPE_STATION:
  525. case NL80211_IFTYPE_ADHOC:
  526. case NL80211_IFTYPE_AP:
  527. case NL80211_IFTYPE_MESH_POINT:
  528. netif_carrier_off(dev);
  529. break;
  530. case NL80211_IFTYPE_WDS:
  531. case NL80211_IFTYPE_P2P_DEVICE:
  532. break;
  533. default:
  534. /* not reached */
  535. WARN_ON(1);
  536. }
  537. /*
  538. * set default queue parameters so drivers don't
  539. * need to initialise the hardware if the hardware
  540. * doesn't start up with sane defaults
  541. */
  542. ieee80211_set_wmm_default(sdata, true);
  543. }
  544. set_bit(SDATA_STATE_RUNNING, &sdata->state);
  545. if (sdata->vif.type == NL80211_IFTYPE_WDS) {
  546. /* Create STA entry for the WDS peer */
  547. sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
  548. GFP_KERNEL);
  549. if (!sta) {
  550. res = -ENOMEM;
  551. goto err_del_interface;
  552. }
  553. sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
  554. sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
  555. sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
  556. res = sta_info_insert(sta);
  557. if (res) {
  558. /* STA has been freed */
  559. goto err_del_interface;
  560. }
  561. rate_control_rate_init(sta);
  562. netif_carrier_on(dev);
  563. } else if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
  564. rcu_assign_pointer(local->p2p_sdata, sdata);
  565. }
  566. /*
  567. * set_multicast_list will be invoked by the networking core
  568. * which will check whether any increments here were done in
  569. * error and sync them down to the hardware as filter flags.
  570. */
  571. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  572. atomic_inc(&local->iff_allmultis);
  573. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  574. atomic_inc(&local->iff_promiscs);
  575. if (coming_up)
  576. local->open_count++;
  577. if (hw_reconf_flags)
  578. ieee80211_hw_config(local, hw_reconf_flags);
  579. ieee80211_recalc_ps(local, -1);
  580. if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  581. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  582. /* XXX: for AP_VLAN, actually track AP queues */
  583. netif_tx_start_all_queues(dev);
  584. } else if (dev) {
  585. unsigned long flags;
  586. int n_acs = IEEE80211_NUM_ACS;
  587. int ac;
  588. if (local->hw.queues < IEEE80211_NUM_ACS)
  589. n_acs = 1;
  590. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  591. if (sdata->vif.cab_queue == IEEE80211_INVAL_HW_QUEUE ||
  592. (local->queue_stop_reasons[sdata->vif.cab_queue] == 0 &&
  593. skb_queue_empty(&local->pending[sdata->vif.cab_queue]))) {
  594. for (ac = 0; ac < n_acs; ac++) {
  595. int ac_queue = sdata->vif.hw_queue[ac];
  596. if (local->queue_stop_reasons[ac_queue] == 0 &&
  597. skb_queue_empty(&local->pending[ac_queue]))
  598. netif_start_subqueue(dev, ac);
  599. }
  600. }
  601. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  602. }
  603. return 0;
  604. err_del_interface:
  605. drv_remove_interface(local, sdata);
  606. err_stop:
  607. if (!local->open_count)
  608. drv_stop(local);
  609. err_del_bss:
  610. sdata->bss = NULL;
  611. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  612. mutex_lock(&local->mtx);
  613. list_del(&sdata->u.vlan.list);
  614. mutex_unlock(&local->mtx);
  615. }
  616. /* might already be clear but that doesn't matter */
  617. clear_bit(SDATA_STATE_RUNNING, &sdata->state);
  618. return res;
  619. }
  620. static int ieee80211_open(struct net_device *dev)
  621. {
  622. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  623. int err;
  624. /* fail early if user set an invalid address */
  625. if (!is_valid_ether_addr(dev->dev_addr))
  626. return -EADDRNOTAVAIL;
  627. err = ieee80211_check_concurrent_iface(sdata, sdata->vif.type);
  628. if (err)
  629. return err;
  630. return ieee80211_do_open(&sdata->wdev, true);
  631. }
  632. static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
  633. bool going_down)
  634. {
  635. struct ieee80211_local *local = sdata->local;
  636. unsigned long flags;
  637. struct sk_buff *skb, *tmp;
  638. u32 hw_reconf_flags = 0;
  639. int i, flushed;
  640. struct ps_data *ps;
  641. struct cfg80211_chan_def chandef;
  642. clear_bit(SDATA_STATE_RUNNING, &sdata->state);
  643. if (rcu_access_pointer(local->scan_sdata) == sdata)
  644. ieee80211_scan_cancel(local);
  645. /*
  646. * Stop TX on this interface first.
  647. */
  648. if (sdata->dev)
  649. netif_tx_stop_all_queues(sdata->dev);
  650. ieee80211_roc_purge(local, sdata);
  651. switch (sdata->vif.type) {
  652. case NL80211_IFTYPE_STATION:
  653. ieee80211_mgd_stop(sdata);
  654. break;
  655. case NL80211_IFTYPE_ADHOC:
  656. ieee80211_ibss_stop(sdata);
  657. break;
  658. case NL80211_IFTYPE_AP:
  659. cancel_work_sync(&sdata->u.ap.request_smps_work);
  660. break;
  661. default:
  662. break;
  663. }
  664. /*
  665. * Remove all stations associated with this interface.
  666. *
  667. * This must be done before calling ops->remove_interface()
  668. * because otherwise we can later invoke ops->sta_notify()
  669. * whenever the STAs are removed, and that invalidates driver
  670. * assumptions about always getting a vif pointer that is valid
  671. * (because if we remove a STA after ops->remove_interface()
  672. * the driver will have removed the vif info already!)
  673. *
  674. * This is relevant only in WDS mode, in all other modes we've
  675. * already removed all stations when disconnecting or similar,
  676. * so warn otherwise.
  677. */
  678. flushed = sta_info_flush(sdata);
  679. WARN_ON_ONCE((sdata->vif.type != NL80211_IFTYPE_WDS && flushed > 0) ||
  680. (sdata->vif.type == NL80211_IFTYPE_WDS && flushed != 1));
  681. /* don't count this interface for promisc/allmulti while it is down */
  682. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  683. atomic_dec(&local->iff_allmultis);
  684. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  685. atomic_dec(&local->iff_promiscs);
  686. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  687. local->fif_pspoll--;
  688. local->fif_probe_req--;
  689. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  690. local->fif_probe_req--;
  691. }
  692. if (sdata->dev) {
  693. netif_addr_lock_bh(sdata->dev);
  694. spin_lock_bh(&local->filter_lock);
  695. __hw_addr_unsync(&local->mc_list, &sdata->dev->mc,
  696. sdata->dev->addr_len);
  697. spin_unlock_bh(&local->filter_lock);
  698. netif_addr_unlock_bh(sdata->dev);
  699. }
  700. del_timer_sync(&local->dynamic_ps_timer);
  701. cancel_work_sync(&local->dynamic_ps_enable_work);
  702. cancel_work_sync(&sdata->recalc_smps);
  703. sdata_lock(sdata);
  704. mutex_lock(&local->mtx);
  705. sdata->vif.csa_active = false;
  706. if (!ieee80211_csa_needs_block_tx(local))
  707. ieee80211_wake_queues_by_reason(&local->hw,
  708. IEEE80211_MAX_QUEUE_MAP,
  709. IEEE80211_QUEUE_STOP_REASON_CSA);
  710. mutex_unlock(&local->mtx);
  711. sdata_unlock(sdata);
  712. cancel_work_sync(&sdata->csa_finalize_work);
  713. cancel_delayed_work_sync(&sdata->dfs_cac_timer_work);
  714. if (sdata->wdev.cac_started) {
  715. chandef = sdata->vif.bss_conf.chandef;
  716. WARN_ON(local->suspended);
  717. mutex_lock(&local->mtx);
  718. ieee80211_vif_release_channel(sdata);
  719. mutex_unlock(&local->mtx);
  720. cfg80211_cac_event(sdata->dev, &chandef,
  721. NL80211_RADAR_CAC_ABORTED,
  722. GFP_KERNEL);
  723. }
  724. /* APs need special treatment */
  725. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  726. struct ieee80211_sub_if_data *vlan, *tmpsdata;
  727. /* down all dependent devices, that is VLANs */
  728. list_for_each_entry_safe(vlan, tmpsdata, &sdata->u.ap.vlans,
  729. u.vlan.list)
  730. dev_close(vlan->dev);
  731. WARN_ON(!list_empty(&sdata->u.ap.vlans));
  732. } else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  733. /* remove all packets in parent bc_buf pointing to this dev */
  734. ps = &sdata->bss->ps;
  735. spin_lock_irqsave(&ps->bc_buf.lock, flags);
  736. skb_queue_walk_safe(&ps->bc_buf, skb, tmp) {
  737. if (skb->dev == sdata->dev) {
  738. __skb_unlink(skb, &ps->bc_buf);
  739. local->total_ps_buffered--;
  740. ieee80211_free_txskb(&local->hw, skb);
  741. }
  742. }
  743. spin_unlock_irqrestore(&ps->bc_buf.lock, flags);
  744. }
  745. if (going_down)
  746. local->open_count--;
  747. switch (sdata->vif.type) {
  748. case NL80211_IFTYPE_AP_VLAN:
  749. mutex_lock(&local->mtx);
  750. list_del(&sdata->u.vlan.list);
  751. mutex_unlock(&local->mtx);
  752. RCU_INIT_POINTER(sdata->vif.chanctx_conf, NULL);
  753. /* no need to tell driver */
  754. break;
  755. case NL80211_IFTYPE_MONITOR:
  756. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  757. local->cooked_mntrs--;
  758. break;
  759. }
  760. local->monitors--;
  761. if (local->monitors == 0) {
  762. local->hw.conf.flags &= ~IEEE80211_CONF_MONITOR;
  763. hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
  764. }
  765. ieee80211_adjust_monitor_flags(sdata, -1);
  766. break;
  767. case NL80211_IFTYPE_P2P_DEVICE:
  768. /* relies on synchronize_rcu() below */
  769. RCU_INIT_POINTER(local->p2p_sdata, NULL);
  770. /* fall through */
  771. default:
  772. cancel_work_sync(&sdata->work);
  773. /*
  774. * When we get here, the interface is marked down.
  775. * Free the remaining keys, if there are any
  776. * (shouldn't be, except maybe in WDS mode?)
  777. *
  778. * Force the key freeing to always synchronize_net()
  779. * to wait for the RX path in case it is using this
  780. * interface enqueuing frames * at this very time on
  781. * another CPU.
  782. */
  783. ieee80211_free_keys(sdata, true);
  784. /* fall through */
  785. case NL80211_IFTYPE_AP:
  786. skb_queue_purge(&sdata->skb_queue);
  787. }
  788. sdata->bss = NULL;
  789. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  790. for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
  791. skb_queue_walk_safe(&local->pending[i], skb, tmp) {
  792. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  793. if (info->control.vif == &sdata->vif) {
  794. __skb_unlink(skb, &local->pending[i]);
  795. ieee80211_free_txskb(&local->hw, skb);
  796. }
  797. }
  798. }
  799. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  800. if (local->open_count == 0)
  801. ieee80211_clear_tx_pending(local);
  802. /*
  803. * If the interface goes down while suspended, presumably because
  804. * the device was unplugged and that happens before our resume,
  805. * then the driver is already unconfigured and the remainder of
  806. * this function isn't needed.
  807. * XXX: what about WoWLAN? If the device has software state, e.g.
  808. * memory allocated, it might expect teardown commands from
  809. * mac80211 here?
  810. */
  811. if (local->suspended) {
  812. WARN_ON(local->wowlan);
  813. WARN_ON(rtnl_dereference(local->monitor_sdata));
  814. return;
  815. }
  816. switch (sdata->vif.type) {
  817. case NL80211_IFTYPE_AP_VLAN:
  818. break;
  819. case NL80211_IFTYPE_MONITOR:
  820. if (local->monitors == 0)
  821. ieee80211_del_virtual_monitor(local);
  822. mutex_lock(&local->mtx);
  823. ieee80211_recalc_idle(local);
  824. mutex_unlock(&local->mtx);
  825. if (!(sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE))
  826. break;
  827. /* fall through */
  828. default:
  829. if (going_down)
  830. drv_remove_interface(local, sdata);
  831. }
  832. ieee80211_recalc_ps(local, -1);
  833. if (local->open_count == 0) {
  834. ieee80211_stop_device(local);
  835. /* no reconfiguring after stop! */
  836. return;
  837. }
  838. /* do after stop to avoid reconfiguring when we stop anyway */
  839. ieee80211_configure_filter(local);
  840. ieee80211_hw_config(local, hw_reconf_flags);
  841. if (local->monitors == local->open_count)
  842. ieee80211_add_virtual_monitor(local);
  843. }
  844. static int ieee80211_stop(struct net_device *dev)
  845. {
  846. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  847. ieee80211_do_stop(sdata, true);
  848. return 0;
  849. }
  850. static void ieee80211_set_multicast_list(struct net_device *dev)
  851. {
  852. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  853. struct ieee80211_local *local = sdata->local;
  854. int allmulti, promisc, sdata_allmulti, sdata_promisc;
  855. allmulti = !!(dev->flags & IFF_ALLMULTI);
  856. promisc = !!(dev->flags & IFF_PROMISC);
  857. sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
  858. sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
  859. if (allmulti != sdata_allmulti) {
  860. if (dev->flags & IFF_ALLMULTI)
  861. atomic_inc(&local->iff_allmultis);
  862. else
  863. atomic_dec(&local->iff_allmultis);
  864. sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
  865. }
  866. if (promisc != sdata_promisc) {
  867. if (dev->flags & IFF_PROMISC)
  868. atomic_inc(&local->iff_promiscs);
  869. else
  870. atomic_dec(&local->iff_promiscs);
  871. sdata->flags ^= IEEE80211_SDATA_PROMISC;
  872. }
  873. spin_lock_bh(&local->filter_lock);
  874. __hw_addr_sync(&local->mc_list, &dev->mc, dev->addr_len);
  875. spin_unlock_bh(&local->filter_lock);
  876. ieee80211_queue_work(&local->hw, &local->reconfig_filter);
  877. }
  878. /*
  879. * Called when the netdev is removed or, by the code below, before
  880. * the interface type changes.
  881. */
  882. static void ieee80211_teardown_sdata(struct ieee80211_sub_if_data *sdata)
  883. {
  884. int i;
  885. /* free extra data */
  886. ieee80211_free_keys(sdata, false);
  887. ieee80211_debugfs_remove_netdev(sdata);
  888. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  889. __skb_queue_purge(&sdata->fragments[i].skb_list);
  890. sdata->fragment_next = 0;
  891. if (ieee80211_vif_is_mesh(&sdata->vif))
  892. mesh_rmc_free(sdata);
  893. }
  894. static void ieee80211_uninit(struct net_device *dev)
  895. {
  896. ieee80211_teardown_sdata(IEEE80211_DEV_TO_SUB_IF(dev));
  897. }
  898. static u16 ieee80211_netdev_select_queue(struct net_device *dev,
  899. struct sk_buff *skb,
  900. void *accel_priv,
  901. select_queue_fallback_t fallback)
  902. {
  903. return ieee80211_select_queue(IEEE80211_DEV_TO_SUB_IF(dev), skb);
  904. }
  905. static const struct net_device_ops ieee80211_dataif_ops = {
  906. .ndo_open = ieee80211_open,
  907. .ndo_stop = ieee80211_stop,
  908. .ndo_uninit = ieee80211_uninit,
  909. .ndo_start_xmit = ieee80211_subif_start_xmit,
  910. .ndo_set_rx_mode = ieee80211_set_multicast_list,
  911. .ndo_change_mtu = ieee80211_change_mtu,
  912. .ndo_set_mac_address = ieee80211_change_mac,
  913. .ndo_select_queue = ieee80211_netdev_select_queue,
  914. };
  915. static u16 ieee80211_monitor_select_queue(struct net_device *dev,
  916. struct sk_buff *skb,
  917. void *accel_priv,
  918. select_queue_fallback_t fallback)
  919. {
  920. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  921. struct ieee80211_local *local = sdata->local;
  922. struct ieee80211_hdr *hdr;
  923. struct ieee80211_radiotap_header *rtap = (void *)skb->data;
  924. if (local->hw.queues < IEEE80211_NUM_ACS)
  925. return 0;
  926. if (skb->len < 4 ||
  927. skb->len < le16_to_cpu(rtap->it_len) + 2 /* frame control */)
  928. return 0; /* doesn't matter, frame will be dropped */
  929. hdr = (void *)((u8 *)skb->data + le16_to_cpu(rtap->it_len));
  930. return ieee80211_select_queue_80211(sdata, skb, hdr);
  931. }
  932. static const struct net_device_ops ieee80211_monitorif_ops = {
  933. .ndo_open = ieee80211_open,
  934. .ndo_stop = ieee80211_stop,
  935. .ndo_uninit = ieee80211_uninit,
  936. .ndo_start_xmit = ieee80211_monitor_start_xmit,
  937. .ndo_set_rx_mode = ieee80211_set_multicast_list,
  938. .ndo_change_mtu = ieee80211_change_mtu,
  939. .ndo_set_mac_address = ieee80211_change_mac,
  940. .ndo_select_queue = ieee80211_monitor_select_queue,
  941. };
  942. static void ieee80211_if_setup(struct net_device *dev)
  943. {
  944. ether_setup(dev);
  945. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  946. dev->netdev_ops = &ieee80211_dataif_ops;
  947. dev->destructor = free_netdev;
  948. }
  949. static void ieee80211_iface_work(struct work_struct *work)
  950. {
  951. struct ieee80211_sub_if_data *sdata =
  952. container_of(work, struct ieee80211_sub_if_data, work);
  953. struct ieee80211_local *local = sdata->local;
  954. struct sk_buff *skb;
  955. struct sta_info *sta;
  956. struct ieee80211_ra_tid *ra_tid;
  957. if (!ieee80211_sdata_running(sdata))
  958. return;
  959. if (local->scanning)
  960. return;
  961. /*
  962. * ieee80211_queue_work() should have picked up most cases,
  963. * here we'll pick the rest.
  964. */
  965. if (WARN(local->suspended,
  966. "interface work scheduled while going to suspend\n"))
  967. return;
  968. /* first process frames */
  969. while ((skb = skb_dequeue(&sdata->skb_queue))) {
  970. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  971. if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_START) {
  972. ra_tid = (void *)&skb->cb;
  973. ieee80211_start_tx_ba_cb(&sdata->vif, ra_tid->ra,
  974. ra_tid->tid);
  975. } else if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_STOP) {
  976. ra_tid = (void *)&skb->cb;
  977. ieee80211_stop_tx_ba_cb(&sdata->vif, ra_tid->ra,
  978. ra_tid->tid);
  979. } else if (ieee80211_is_action(mgmt->frame_control) &&
  980. mgmt->u.action.category == WLAN_CATEGORY_BACK) {
  981. int len = skb->len;
  982. mutex_lock(&local->sta_mtx);
  983. sta = sta_info_get_bss(sdata, mgmt->sa);
  984. if (sta) {
  985. switch (mgmt->u.action.u.addba_req.action_code) {
  986. case WLAN_ACTION_ADDBA_REQ:
  987. ieee80211_process_addba_request(
  988. local, sta, mgmt, len);
  989. break;
  990. case WLAN_ACTION_ADDBA_RESP:
  991. ieee80211_process_addba_resp(local, sta,
  992. mgmt, len);
  993. break;
  994. case WLAN_ACTION_DELBA:
  995. ieee80211_process_delba(sdata, sta,
  996. mgmt, len);
  997. break;
  998. default:
  999. WARN_ON(1);
  1000. break;
  1001. }
  1002. }
  1003. mutex_unlock(&local->sta_mtx);
  1004. } else if (ieee80211_is_data_qos(mgmt->frame_control)) {
  1005. struct ieee80211_hdr *hdr = (void *)mgmt;
  1006. /*
  1007. * So the frame isn't mgmt, but frame_control
  1008. * is at the right place anyway, of course, so
  1009. * the if statement is correct.
  1010. *
  1011. * Warn if we have other data frame types here,
  1012. * they must not get here.
  1013. */
  1014. WARN_ON(hdr->frame_control &
  1015. cpu_to_le16(IEEE80211_STYPE_NULLFUNC));
  1016. WARN_ON(!(hdr->seq_ctrl &
  1017. cpu_to_le16(IEEE80211_SCTL_FRAG)));
  1018. /*
  1019. * This was a fragment of a frame, received while
  1020. * a block-ack session was active. That cannot be
  1021. * right, so terminate the session.
  1022. */
  1023. mutex_lock(&local->sta_mtx);
  1024. sta = sta_info_get_bss(sdata, mgmt->sa);
  1025. if (sta) {
  1026. u16 tid = *ieee80211_get_qos_ctl(hdr) &
  1027. IEEE80211_QOS_CTL_TID_MASK;
  1028. __ieee80211_stop_rx_ba_session(
  1029. sta, tid, WLAN_BACK_RECIPIENT,
  1030. WLAN_REASON_QSTA_REQUIRE_SETUP,
  1031. true);
  1032. }
  1033. mutex_unlock(&local->sta_mtx);
  1034. } else switch (sdata->vif.type) {
  1035. case NL80211_IFTYPE_STATION:
  1036. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  1037. break;
  1038. case NL80211_IFTYPE_ADHOC:
  1039. ieee80211_ibss_rx_queued_mgmt(sdata, skb);
  1040. break;
  1041. case NL80211_IFTYPE_MESH_POINT:
  1042. if (!ieee80211_vif_is_mesh(&sdata->vif))
  1043. break;
  1044. ieee80211_mesh_rx_queued_mgmt(sdata, skb);
  1045. break;
  1046. default:
  1047. WARN(1, "frame for unexpected interface type");
  1048. break;
  1049. }
  1050. kfree_skb(skb);
  1051. }
  1052. /* then other type-dependent work */
  1053. switch (sdata->vif.type) {
  1054. case NL80211_IFTYPE_STATION:
  1055. ieee80211_sta_work(sdata);
  1056. break;
  1057. case NL80211_IFTYPE_ADHOC:
  1058. ieee80211_ibss_work(sdata);
  1059. break;
  1060. case NL80211_IFTYPE_MESH_POINT:
  1061. if (!ieee80211_vif_is_mesh(&sdata->vif))
  1062. break;
  1063. ieee80211_mesh_work(sdata);
  1064. break;
  1065. default:
  1066. break;
  1067. }
  1068. }
  1069. static void ieee80211_recalc_smps_work(struct work_struct *work)
  1070. {
  1071. struct ieee80211_sub_if_data *sdata =
  1072. container_of(work, struct ieee80211_sub_if_data, recalc_smps);
  1073. ieee80211_recalc_smps(sdata);
  1074. }
  1075. /*
  1076. * Helper function to initialise an interface to a specific type.
  1077. */
  1078. static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
  1079. enum nl80211_iftype type)
  1080. {
  1081. /* clear type-dependent union */
  1082. memset(&sdata->u, 0, sizeof(sdata->u));
  1083. /* and set some type-dependent values */
  1084. sdata->vif.type = type;
  1085. sdata->vif.p2p = false;
  1086. sdata->wdev.iftype = type;
  1087. sdata->control_port_protocol = cpu_to_be16(ETH_P_PAE);
  1088. sdata->control_port_no_encrypt = false;
  1089. sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
  1090. sdata->vif.bss_conf.idle = true;
  1091. sdata->noack_map = 0;
  1092. /* only monitor/p2p-device differ */
  1093. if (sdata->dev) {
  1094. sdata->dev->netdev_ops = &ieee80211_dataif_ops;
  1095. sdata->dev->type = ARPHRD_ETHER;
  1096. }
  1097. skb_queue_head_init(&sdata->skb_queue);
  1098. INIT_WORK(&sdata->work, ieee80211_iface_work);
  1099. INIT_WORK(&sdata->recalc_smps, ieee80211_recalc_smps_work);
  1100. INIT_WORK(&sdata->csa_finalize_work, ieee80211_csa_finalize_work);
  1101. INIT_LIST_HEAD(&sdata->assigned_chanctx_list);
  1102. INIT_LIST_HEAD(&sdata->reserved_chanctx_list);
  1103. switch (type) {
  1104. case NL80211_IFTYPE_P2P_GO:
  1105. type = NL80211_IFTYPE_AP;
  1106. sdata->vif.type = type;
  1107. sdata->vif.p2p = true;
  1108. /* fall through */
  1109. case NL80211_IFTYPE_AP:
  1110. skb_queue_head_init(&sdata->u.ap.ps.bc_buf);
  1111. INIT_LIST_HEAD(&sdata->u.ap.vlans);
  1112. INIT_WORK(&sdata->u.ap.request_smps_work,
  1113. ieee80211_request_smps_ap_work);
  1114. sdata->vif.bss_conf.bssid = sdata->vif.addr;
  1115. sdata->u.ap.req_smps = IEEE80211_SMPS_OFF;
  1116. break;
  1117. case NL80211_IFTYPE_P2P_CLIENT:
  1118. type = NL80211_IFTYPE_STATION;
  1119. sdata->vif.type = type;
  1120. sdata->vif.p2p = true;
  1121. /* fall through */
  1122. case NL80211_IFTYPE_STATION:
  1123. sdata->vif.bss_conf.bssid = sdata->u.mgd.bssid;
  1124. ieee80211_sta_setup_sdata(sdata);
  1125. break;
  1126. case NL80211_IFTYPE_ADHOC:
  1127. sdata->vif.bss_conf.bssid = sdata->u.ibss.bssid;
  1128. ieee80211_ibss_setup_sdata(sdata);
  1129. break;
  1130. case NL80211_IFTYPE_MESH_POINT:
  1131. if (ieee80211_vif_is_mesh(&sdata->vif))
  1132. ieee80211_mesh_init_sdata(sdata);
  1133. break;
  1134. case NL80211_IFTYPE_MONITOR:
  1135. sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1136. sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
  1137. sdata->u.mntr_flags = MONITOR_FLAG_CONTROL |
  1138. MONITOR_FLAG_OTHER_BSS;
  1139. break;
  1140. case NL80211_IFTYPE_WDS:
  1141. sdata->vif.bss_conf.bssid = NULL;
  1142. break;
  1143. case NL80211_IFTYPE_AP_VLAN:
  1144. case NL80211_IFTYPE_P2P_DEVICE:
  1145. sdata->vif.bss_conf.bssid = sdata->vif.addr;
  1146. break;
  1147. case NL80211_IFTYPE_UNSPECIFIED:
  1148. case NUM_NL80211_IFTYPES:
  1149. BUG();
  1150. break;
  1151. }
  1152. ieee80211_debugfs_add_netdev(sdata);
  1153. }
  1154. static int ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data *sdata,
  1155. enum nl80211_iftype type)
  1156. {
  1157. struct ieee80211_local *local = sdata->local;
  1158. int ret, err;
  1159. enum nl80211_iftype internal_type = type;
  1160. bool p2p = false;
  1161. ASSERT_RTNL();
  1162. if (!local->ops->change_interface)
  1163. return -EBUSY;
  1164. switch (sdata->vif.type) {
  1165. case NL80211_IFTYPE_AP:
  1166. case NL80211_IFTYPE_STATION:
  1167. case NL80211_IFTYPE_ADHOC:
  1168. /*
  1169. * Could maybe also all others here?
  1170. * Just not sure how that interacts
  1171. * with the RX/config path e.g. for
  1172. * mesh.
  1173. */
  1174. break;
  1175. default:
  1176. return -EBUSY;
  1177. }
  1178. switch (type) {
  1179. case NL80211_IFTYPE_AP:
  1180. case NL80211_IFTYPE_STATION:
  1181. case NL80211_IFTYPE_ADHOC:
  1182. /*
  1183. * Could probably support everything
  1184. * but WDS here (WDS do_open can fail
  1185. * under memory pressure, which this
  1186. * code isn't prepared to handle).
  1187. */
  1188. break;
  1189. case NL80211_IFTYPE_P2P_CLIENT:
  1190. p2p = true;
  1191. internal_type = NL80211_IFTYPE_STATION;
  1192. break;
  1193. case NL80211_IFTYPE_P2P_GO:
  1194. p2p = true;
  1195. internal_type = NL80211_IFTYPE_AP;
  1196. break;
  1197. default:
  1198. return -EBUSY;
  1199. }
  1200. ret = ieee80211_check_concurrent_iface(sdata, internal_type);
  1201. if (ret)
  1202. return ret;
  1203. ieee80211_do_stop(sdata, false);
  1204. ieee80211_teardown_sdata(sdata);
  1205. ret = drv_change_interface(local, sdata, internal_type, p2p);
  1206. if (ret)
  1207. type = ieee80211_vif_type_p2p(&sdata->vif);
  1208. /*
  1209. * Ignore return value here, there's not much we can do since
  1210. * the driver changed the interface type internally already.
  1211. * The warnings will hopefully make driver authors fix it :-)
  1212. */
  1213. ieee80211_check_queues(sdata, type);
  1214. ieee80211_setup_sdata(sdata, type);
  1215. err = ieee80211_do_open(&sdata->wdev, false);
  1216. WARN(err, "type change: do_open returned %d", err);
  1217. return ret;
  1218. }
  1219. int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
  1220. enum nl80211_iftype type)
  1221. {
  1222. int ret;
  1223. ASSERT_RTNL();
  1224. if (type == ieee80211_vif_type_p2p(&sdata->vif))
  1225. return 0;
  1226. if (ieee80211_sdata_running(sdata)) {
  1227. ret = ieee80211_runtime_change_iftype(sdata, type);
  1228. if (ret)
  1229. return ret;
  1230. } else {
  1231. /* Purge and reset type-dependent state. */
  1232. ieee80211_teardown_sdata(sdata);
  1233. ieee80211_setup_sdata(sdata, type);
  1234. }
  1235. /* reset some values that shouldn't be kept across type changes */
  1236. sdata->drop_unencrypted = 0;
  1237. if (type == NL80211_IFTYPE_STATION)
  1238. sdata->u.mgd.use_4addr = false;
  1239. return 0;
  1240. }
  1241. static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
  1242. u8 *perm_addr, enum nl80211_iftype type)
  1243. {
  1244. struct ieee80211_sub_if_data *sdata;
  1245. u64 mask, start, addr, val, inc;
  1246. u8 *m;
  1247. u8 tmp_addr[ETH_ALEN];
  1248. int i;
  1249. /* default ... something at least */
  1250. memcpy(perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  1251. if (is_zero_ether_addr(local->hw.wiphy->addr_mask) &&
  1252. local->hw.wiphy->n_addresses <= 1)
  1253. return;
  1254. mutex_lock(&local->iflist_mtx);
  1255. switch (type) {
  1256. case NL80211_IFTYPE_MONITOR:
  1257. /* doesn't matter */
  1258. break;
  1259. case NL80211_IFTYPE_WDS:
  1260. case NL80211_IFTYPE_AP_VLAN:
  1261. /* match up with an AP interface */
  1262. list_for_each_entry(sdata, &local->interfaces, list) {
  1263. if (sdata->vif.type != NL80211_IFTYPE_AP)
  1264. continue;
  1265. memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
  1266. break;
  1267. }
  1268. /* keep default if no AP interface present */
  1269. break;
  1270. case NL80211_IFTYPE_P2P_CLIENT:
  1271. case NL80211_IFTYPE_P2P_GO:
  1272. if (local->hw.flags & IEEE80211_HW_P2P_DEV_ADDR_FOR_INTF) {
  1273. list_for_each_entry(sdata, &local->interfaces, list) {
  1274. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE)
  1275. continue;
  1276. if (!ieee80211_sdata_running(sdata))
  1277. continue;
  1278. memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
  1279. goto out_unlock;
  1280. }
  1281. }
  1282. /* otherwise fall through */
  1283. default:
  1284. /* assign a new address if possible -- try n_addresses first */
  1285. for (i = 0; i < local->hw.wiphy->n_addresses; i++) {
  1286. bool used = false;
  1287. list_for_each_entry(sdata, &local->interfaces, list) {
  1288. if (ether_addr_equal(local->hw.wiphy->addresses[i].addr,
  1289. sdata->vif.addr)) {
  1290. used = true;
  1291. break;
  1292. }
  1293. }
  1294. if (!used) {
  1295. memcpy(perm_addr,
  1296. local->hw.wiphy->addresses[i].addr,
  1297. ETH_ALEN);
  1298. break;
  1299. }
  1300. }
  1301. /* try mask if available */
  1302. if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
  1303. break;
  1304. m = local->hw.wiphy->addr_mask;
  1305. mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  1306. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  1307. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  1308. if (__ffs64(mask) + hweight64(mask) != fls64(mask)) {
  1309. /* not a contiguous mask ... not handled now! */
  1310. pr_info("not contiguous\n");
  1311. break;
  1312. }
  1313. /*
  1314. * Pick address of existing interface in case user changed
  1315. * MAC address manually, default to perm_addr.
  1316. */
  1317. m = local->hw.wiphy->perm_addr;
  1318. list_for_each_entry(sdata, &local->interfaces, list) {
  1319. if (sdata->vif.type == NL80211_IFTYPE_MONITOR)
  1320. continue;
  1321. m = sdata->vif.addr;
  1322. break;
  1323. }
  1324. start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  1325. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  1326. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  1327. inc = 1ULL<<__ffs64(mask);
  1328. val = (start & mask);
  1329. addr = (start & ~mask) | (val & mask);
  1330. do {
  1331. bool used = false;
  1332. tmp_addr[5] = addr >> 0*8;
  1333. tmp_addr[4] = addr >> 1*8;
  1334. tmp_addr[3] = addr >> 2*8;
  1335. tmp_addr[2] = addr >> 3*8;
  1336. tmp_addr[1] = addr >> 4*8;
  1337. tmp_addr[0] = addr >> 5*8;
  1338. val += inc;
  1339. list_for_each_entry(sdata, &local->interfaces, list) {
  1340. if (ether_addr_equal(tmp_addr, sdata->vif.addr)) {
  1341. used = true;
  1342. break;
  1343. }
  1344. }
  1345. if (!used) {
  1346. memcpy(perm_addr, tmp_addr, ETH_ALEN);
  1347. break;
  1348. }
  1349. addr = (start & ~mask) | (val & mask);
  1350. } while (addr != start);
  1351. break;
  1352. }
  1353. out_unlock:
  1354. mutex_unlock(&local->iflist_mtx);
  1355. }
  1356. int ieee80211_if_add(struct ieee80211_local *local, const char *name,
  1357. struct wireless_dev **new_wdev, enum nl80211_iftype type,
  1358. struct vif_params *params)
  1359. {
  1360. struct net_device *ndev = NULL;
  1361. struct ieee80211_sub_if_data *sdata = NULL;
  1362. int ret, i;
  1363. int txqs = 1;
  1364. ASSERT_RTNL();
  1365. if (type == NL80211_IFTYPE_P2P_DEVICE) {
  1366. struct wireless_dev *wdev;
  1367. sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size,
  1368. GFP_KERNEL);
  1369. if (!sdata)
  1370. return -ENOMEM;
  1371. wdev = &sdata->wdev;
  1372. sdata->dev = NULL;
  1373. strlcpy(sdata->name, name, IFNAMSIZ);
  1374. ieee80211_assign_perm_addr(local, wdev->address, type);
  1375. memcpy(sdata->vif.addr, wdev->address, ETH_ALEN);
  1376. } else {
  1377. if (local->hw.queues >= IEEE80211_NUM_ACS)
  1378. txqs = IEEE80211_NUM_ACS;
  1379. ndev = alloc_netdev_mqs(sizeof(*sdata) +
  1380. local->hw.vif_data_size,
  1381. name, ieee80211_if_setup, txqs, 1);
  1382. if (!ndev)
  1383. return -ENOMEM;
  1384. dev_net_set(ndev, wiphy_net(local->hw.wiphy));
  1385. ndev->needed_headroom = local->tx_headroom +
  1386. 4*6 /* four MAC addresses */
  1387. + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
  1388. + 6 /* mesh */
  1389. + 8 /* rfc1042/bridge tunnel */
  1390. - ETH_HLEN /* ethernet hard_header_len */
  1391. + IEEE80211_ENCRYPT_HEADROOM;
  1392. ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
  1393. ret = dev_alloc_name(ndev, ndev->name);
  1394. if (ret < 0) {
  1395. free_netdev(ndev);
  1396. return ret;
  1397. }
  1398. ieee80211_assign_perm_addr(local, ndev->perm_addr, type);
  1399. memcpy(ndev->dev_addr, ndev->perm_addr, ETH_ALEN);
  1400. SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
  1401. /* don't use IEEE80211_DEV_TO_SUB_IF -- it checks too much */
  1402. sdata = netdev_priv(ndev);
  1403. ndev->ieee80211_ptr = &sdata->wdev;
  1404. memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
  1405. memcpy(sdata->name, ndev->name, IFNAMSIZ);
  1406. sdata->dev = ndev;
  1407. }
  1408. /* initialise type-independent data */
  1409. sdata->wdev.wiphy = local->hw.wiphy;
  1410. sdata->local = local;
  1411. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  1412. skb_queue_head_init(&sdata->fragments[i].skb_list);
  1413. INIT_LIST_HEAD(&sdata->key_list);
  1414. INIT_DELAYED_WORK(&sdata->dfs_cac_timer_work,
  1415. ieee80211_dfs_cac_timer_work);
  1416. INIT_DELAYED_WORK(&sdata->dec_tailroom_needed_wk,
  1417. ieee80211_delayed_tailroom_dec);
  1418. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  1419. struct ieee80211_supported_band *sband;
  1420. sband = local->hw.wiphy->bands[i];
  1421. sdata->rc_rateidx_mask[i] =
  1422. sband ? (1 << sband->n_bitrates) - 1 : 0;
  1423. if (sband)
  1424. memcpy(sdata->rc_rateidx_mcs_mask[i],
  1425. sband->ht_cap.mcs.rx_mask,
  1426. sizeof(sdata->rc_rateidx_mcs_mask[i]));
  1427. else
  1428. memset(sdata->rc_rateidx_mcs_mask[i], 0,
  1429. sizeof(sdata->rc_rateidx_mcs_mask[i]));
  1430. }
  1431. ieee80211_set_default_queues(sdata);
  1432. sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
  1433. sdata->user_power_level = local->user_power_level;
  1434. sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
  1435. /* setup type-dependent data */
  1436. ieee80211_setup_sdata(sdata, type);
  1437. if (ndev) {
  1438. if (params) {
  1439. ndev->ieee80211_ptr->use_4addr = params->use_4addr;
  1440. if (type == NL80211_IFTYPE_STATION)
  1441. sdata->u.mgd.use_4addr = params->use_4addr;
  1442. }
  1443. ndev->features |= local->hw.netdev_features;
  1444. ret = register_netdevice(ndev);
  1445. if (ret) {
  1446. free_netdev(ndev);
  1447. return ret;
  1448. }
  1449. }
  1450. mutex_lock(&local->iflist_mtx);
  1451. list_add_tail_rcu(&sdata->list, &local->interfaces);
  1452. mutex_unlock(&local->iflist_mtx);
  1453. if (new_wdev)
  1454. *new_wdev = &sdata->wdev;
  1455. return 0;
  1456. }
  1457. void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
  1458. {
  1459. ASSERT_RTNL();
  1460. mutex_lock(&sdata->local->iflist_mtx);
  1461. list_del_rcu(&sdata->list);
  1462. mutex_unlock(&sdata->local->iflist_mtx);
  1463. synchronize_rcu();
  1464. if (sdata->dev) {
  1465. unregister_netdevice(sdata->dev);
  1466. } else {
  1467. cfg80211_unregister_wdev(&sdata->wdev);
  1468. kfree(sdata);
  1469. }
  1470. }
  1471. void ieee80211_sdata_stop(struct ieee80211_sub_if_data *sdata)
  1472. {
  1473. if (WARN_ON_ONCE(!test_bit(SDATA_STATE_RUNNING, &sdata->state)))
  1474. return;
  1475. ieee80211_do_stop(sdata, true);
  1476. ieee80211_teardown_sdata(sdata);
  1477. }
  1478. /*
  1479. * Remove all interfaces, may only be called at hardware unregistration
  1480. * time because it doesn't do RCU-safe list removals.
  1481. */
  1482. void ieee80211_remove_interfaces(struct ieee80211_local *local)
  1483. {
  1484. struct ieee80211_sub_if_data *sdata, *tmp;
  1485. LIST_HEAD(unreg_list);
  1486. LIST_HEAD(wdev_list);
  1487. ASSERT_RTNL();
  1488. /*
  1489. * Close all AP_VLAN interfaces first, as otherwise they
  1490. * might be closed while the AP interface they belong to
  1491. * is closed, causing unregister_netdevice_many() to crash.
  1492. */
  1493. list_for_each_entry(sdata, &local->interfaces, list)
  1494. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1495. dev_close(sdata->dev);
  1496. mutex_lock(&local->iflist_mtx);
  1497. list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
  1498. list_del(&sdata->list);
  1499. if (sdata->dev)
  1500. unregister_netdevice_queue(sdata->dev, &unreg_list);
  1501. else
  1502. list_add(&sdata->list, &wdev_list);
  1503. }
  1504. mutex_unlock(&local->iflist_mtx);
  1505. unregister_netdevice_many(&unreg_list);
  1506. list_for_each_entry_safe(sdata, tmp, &wdev_list, list) {
  1507. list_del(&sdata->list);
  1508. cfg80211_unregister_wdev(&sdata->wdev);
  1509. kfree(sdata);
  1510. }
  1511. }
  1512. static int netdev_notify(struct notifier_block *nb,
  1513. unsigned long state, void *ptr)
  1514. {
  1515. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1516. struct ieee80211_sub_if_data *sdata;
  1517. if (state != NETDEV_CHANGENAME)
  1518. return NOTIFY_DONE;
  1519. if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
  1520. return NOTIFY_DONE;
  1521. if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
  1522. return NOTIFY_DONE;
  1523. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1524. memcpy(sdata->name, dev->name, IFNAMSIZ);
  1525. ieee80211_debugfs_rename_netdev(sdata);
  1526. return NOTIFY_OK;
  1527. }
  1528. static struct notifier_block mac80211_netdev_notifier = {
  1529. .notifier_call = netdev_notify,
  1530. };
  1531. int ieee80211_iface_init(void)
  1532. {
  1533. return register_netdevice_notifier(&mac80211_netdev_notifier);
  1534. }
  1535. void ieee80211_iface_exit(void)
  1536. {
  1537. unregister_netdevice_notifier(&mac80211_netdev_notifier);
  1538. }