iface.c 51 KB

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