iface.c 53 KB

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