iface.c 44 KB

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