iface.c 45 KB

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