iface.c 50 KB

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