iface.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. /*
  2. * Interface handling (except master interface)
  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. static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
  44. {
  45. int meshhdrlen;
  46. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  47. meshhdrlen = (sdata->vif.type == NL80211_IFTYPE_MESH_POINT) ? 5 : 0;
  48. /* FIX: what would be proper limits for MTU?
  49. * This interface uses 802.3 frames. */
  50. if (new_mtu < 256 ||
  51. new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6 - meshhdrlen) {
  52. return -EINVAL;
  53. }
  54. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  55. printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
  56. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  57. dev->mtu = new_mtu;
  58. return 0;
  59. }
  60. static int ieee80211_change_mac(struct net_device *dev, void *addr)
  61. {
  62. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  63. struct sockaddr *sa = addr;
  64. int ret;
  65. if (ieee80211_sdata_running(sdata))
  66. return -EBUSY;
  67. ret = eth_mac_addr(dev, sa);
  68. if (ret == 0)
  69. memcpy(sdata->vif.addr, sa->sa_data, ETH_ALEN);
  70. return ret;
  71. }
  72. static inline int identical_mac_addr_allowed(int type1, int type2)
  73. {
  74. return type1 == NL80211_IFTYPE_MONITOR ||
  75. type2 == NL80211_IFTYPE_MONITOR ||
  76. (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_WDS) ||
  77. (type1 == NL80211_IFTYPE_WDS &&
  78. (type2 == NL80211_IFTYPE_WDS ||
  79. type2 == NL80211_IFTYPE_AP)) ||
  80. (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_AP_VLAN) ||
  81. (type1 == NL80211_IFTYPE_AP_VLAN &&
  82. (type2 == NL80211_IFTYPE_AP ||
  83. type2 == NL80211_IFTYPE_AP_VLAN));
  84. }
  85. static int ieee80211_check_concurrent_iface(struct ieee80211_sub_if_data *sdata,
  86. enum nl80211_iftype iftype)
  87. {
  88. struct ieee80211_local *local = sdata->local;
  89. struct ieee80211_sub_if_data *nsdata;
  90. struct net_device *dev = sdata->dev;
  91. ASSERT_RTNL();
  92. /* we hold the RTNL here so can safely walk the list */
  93. list_for_each_entry(nsdata, &local->interfaces, list) {
  94. struct net_device *ndev = nsdata->dev;
  95. if (ndev != dev && ieee80211_sdata_running(nsdata)) {
  96. /*
  97. * Allow only a single IBSS interface to be up at any
  98. * time. This is restricted because beacon distribution
  99. * cannot work properly if both are in the same IBSS.
  100. *
  101. * To remove this restriction we'd have to disallow them
  102. * from setting the same SSID on different IBSS interfaces
  103. * belonging to the same hardware. Then, however, we're
  104. * faced with having to adopt two different TSF timers...
  105. */
  106. if (iftype == NL80211_IFTYPE_ADHOC &&
  107. nsdata->vif.type == NL80211_IFTYPE_ADHOC)
  108. return -EBUSY;
  109. /*
  110. * The remaining checks are only performed for interfaces
  111. * with the same MAC address.
  112. */
  113. if (compare_ether_addr(dev->dev_addr, ndev->dev_addr))
  114. continue;
  115. /*
  116. * check whether it may have the same address
  117. */
  118. if (!identical_mac_addr_allowed(iftype,
  119. nsdata->vif.type))
  120. return -ENOTUNIQ;
  121. /*
  122. * can only add VLANs to enabled APs
  123. */
  124. if (iftype == NL80211_IFTYPE_AP_VLAN &&
  125. nsdata->vif.type == NL80211_IFTYPE_AP)
  126. sdata->bss = &nsdata->u.ap;
  127. }
  128. }
  129. return 0;
  130. }
  131. void ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data *sdata,
  132. const int offset)
  133. {
  134. struct ieee80211_local *local = sdata->local;
  135. u32 flags = sdata->u.mntr_flags;
  136. #define ADJUST(_f, _s) do { \
  137. if (flags & MONITOR_FLAG_##_f) \
  138. local->fif_##_s += offset; \
  139. } while (0)
  140. ADJUST(FCSFAIL, fcsfail);
  141. ADJUST(PLCPFAIL, plcpfail);
  142. ADJUST(CONTROL, control);
  143. ADJUST(CONTROL, pspoll);
  144. ADJUST(OTHER_BSS, other_bss);
  145. #undef ADJUST
  146. }
  147. /*
  148. * NOTE: Be very careful when changing this function, it must NOT return
  149. * an error on interface type changes that have been pre-checked, so most
  150. * checks should be in ieee80211_check_concurrent_iface.
  151. */
  152. static int ieee80211_do_open(struct net_device *dev, bool coming_up)
  153. {
  154. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  155. struct ieee80211_local *local = sdata->local;
  156. struct sta_info *sta;
  157. u32 changed = 0;
  158. int res;
  159. u32 hw_reconf_flags = 0;
  160. switch (sdata->vif.type) {
  161. case NL80211_IFTYPE_WDS:
  162. if (!is_valid_ether_addr(sdata->u.wds.remote_addr))
  163. return -ENOLINK;
  164. break;
  165. case NL80211_IFTYPE_AP_VLAN:
  166. if (!sdata->bss)
  167. return -ENOLINK;
  168. list_add(&sdata->u.vlan.list, &sdata->bss->vlans);
  169. break;
  170. case NL80211_IFTYPE_AP:
  171. sdata->bss = &sdata->u.ap;
  172. break;
  173. case NL80211_IFTYPE_MESH_POINT:
  174. case NL80211_IFTYPE_STATION:
  175. case NL80211_IFTYPE_MONITOR:
  176. case NL80211_IFTYPE_ADHOC:
  177. /* no special treatment */
  178. break;
  179. case NL80211_IFTYPE_UNSPECIFIED:
  180. case NUM_NL80211_IFTYPES:
  181. case NL80211_IFTYPE_P2P_CLIENT:
  182. case NL80211_IFTYPE_P2P_GO:
  183. /* cannot happen */
  184. WARN_ON(1);
  185. break;
  186. }
  187. if (local->open_count == 0) {
  188. res = drv_start(local);
  189. if (res)
  190. goto err_del_bss;
  191. if (local->ops->napi_poll)
  192. napi_enable(&local->napi);
  193. /* we're brought up, everything changes */
  194. hw_reconf_flags = ~0;
  195. ieee80211_led_radio(local, true);
  196. }
  197. /*
  198. * Copy the hopefully now-present MAC address to
  199. * this interface, if it has the special null one.
  200. */
  201. if (is_zero_ether_addr(dev->dev_addr)) {
  202. memcpy(dev->dev_addr,
  203. local->hw.wiphy->perm_addr,
  204. ETH_ALEN);
  205. memcpy(dev->perm_addr, dev->dev_addr, ETH_ALEN);
  206. if (!is_valid_ether_addr(dev->dev_addr)) {
  207. if (!local->open_count)
  208. drv_stop(local);
  209. return -EADDRNOTAVAIL;
  210. }
  211. }
  212. switch (sdata->vif.type) {
  213. case NL80211_IFTYPE_AP_VLAN:
  214. /* no need to tell driver */
  215. break;
  216. case NL80211_IFTYPE_MONITOR:
  217. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  218. local->cooked_mntrs++;
  219. break;
  220. }
  221. /* must be before the call to ieee80211_configure_filter */
  222. local->monitors++;
  223. if (local->monitors == 1) {
  224. local->hw.conf.flags |= IEEE80211_CONF_MONITOR;
  225. hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
  226. }
  227. ieee80211_adjust_monitor_flags(sdata, 1);
  228. ieee80211_configure_filter(local);
  229. netif_carrier_on(dev);
  230. break;
  231. default:
  232. if (coming_up) {
  233. res = drv_add_interface(local, &sdata->vif);
  234. if (res)
  235. goto err_stop;
  236. }
  237. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  238. local->fif_pspoll++;
  239. local->fif_probe_req++;
  240. ieee80211_configure_filter(local);
  241. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  242. local->fif_probe_req++;
  243. }
  244. changed |= ieee80211_reset_erp_info(sdata);
  245. ieee80211_bss_info_change_notify(sdata, changed);
  246. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  247. netif_carrier_off(dev);
  248. else
  249. netif_carrier_on(dev);
  250. }
  251. set_bit(SDATA_STATE_RUNNING, &sdata->state);
  252. if (sdata->vif.type == NL80211_IFTYPE_WDS) {
  253. /* Create STA entry for the WDS peer */
  254. sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
  255. GFP_KERNEL);
  256. if (!sta) {
  257. res = -ENOMEM;
  258. goto err_del_interface;
  259. }
  260. /* no locking required since STA is not live yet */
  261. sta->flags |= WLAN_STA_AUTHORIZED;
  262. res = sta_info_insert(sta);
  263. if (res) {
  264. /* STA has been freed */
  265. goto err_del_interface;
  266. }
  267. rate_control_rate_init(sta);
  268. }
  269. /*
  270. * set_multicast_list will be invoked by the networking core
  271. * which will check whether any increments here were done in
  272. * error and sync them down to the hardware as filter flags.
  273. */
  274. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  275. atomic_inc(&local->iff_allmultis);
  276. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  277. atomic_inc(&local->iff_promiscs);
  278. mutex_lock(&local->mtx);
  279. hw_reconf_flags |= __ieee80211_recalc_idle(local);
  280. mutex_unlock(&local->mtx);
  281. if (coming_up)
  282. local->open_count++;
  283. if (hw_reconf_flags) {
  284. ieee80211_hw_config(local, hw_reconf_flags);
  285. /*
  286. * set default queue parameters so drivers don't
  287. * need to initialise the hardware if the hardware
  288. * doesn't start up with sane defaults
  289. */
  290. ieee80211_set_wmm_default(sdata);
  291. }
  292. ieee80211_recalc_ps(local, -1);
  293. netif_tx_start_all_queues(dev);
  294. return 0;
  295. err_del_interface:
  296. drv_remove_interface(local, &sdata->vif);
  297. err_stop:
  298. if (!local->open_count)
  299. drv_stop(local);
  300. err_del_bss:
  301. sdata->bss = NULL;
  302. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  303. list_del(&sdata->u.vlan.list);
  304. clear_bit(SDATA_STATE_RUNNING, &sdata->state);
  305. return res;
  306. }
  307. static int ieee80211_open(struct net_device *dev)
  308. {
  309. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  310. int err;
  311. /* fail early if user set an invalid address */
  312. if (!is_zero_ether_addr(dev->dev_addr) &&
  313. !is_valid_ether_addr(dev->dev_addr))
  314. return -EADDRNOTAVAIL;
  315. err = ieee80211_check_concurrent_iface(sdata, sdata->vif.type);
  316. if (err)
  317. return err;
  318. return ieee80211_do_open(dev, true);
  319. }
  320. static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
  321. bool going_down)
  322. {
  323. struct ieee80211_local *local = sdata->local;
  324. unsigned long flags;
  325. struct sk_buff *skb, *tmp;
  326. u32 hw_reconf_flags = 0;
  327. int i;
  328. if (local->scan_sdata == sdata)
  329. ieee80211_scan_cancel(local);
  330. clear_bit(SDATA_STATE_RUNNING, &sdata->state);
  331. /*
  332. * Stop TX on this interface first.
  333. */
  334. netif_tx_stop_all_queues(sdata->dev);
  335. /*
  336. * Purge work for this interface.
  337. */
  338. ieee80211_work_purge(sdata);
  339. /*
  340. * Remove all stations associated with this interface.
  341. *
  342. * This must be done before calling ops->remove_interface()
  343. * because otherwise we can later invoke ops->sta_notify()
  344. * whenever the STAs are removed, and that invalidates driver
  345. * assumptions about always getting a vif pointer that is valid
  346. * (because if we remove a STA after ops->remove_interface()
  347. * the driver will have removed the vif info already!)
  348. *
  349. * This is relevant only in AP, WDS and mesh modes, since in
  350. * all other modes we've already removed all stations when
  351. * disconnecting etc.
  352. */
  353. sta_info_flush(local, sdata);
  354. /*
  355. * Don't count this interface for promisc/allmulti while it
  356. * is down. dev_mc_unsync() will invoke set_multicast_list
  357. * on the master interface which will sync these down to the
  358. * hardware as filter flags.
  359. */
  360. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  361. atomic_dec(&local->iff_allmultis);
  362. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  363. atomic_dec(&local->iff_promiscs);
  364. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  365. local->fif_pspoll--;
  366. local->fif_probe_req--;
  367. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  368. local->fif_probe_req--;
  369. }
  370. netif_addr_lock_bh(sdata->dev);
  371. spin_lock_bh(&local->filter_lock);
  372. __hw_addr_unsync(&local->mc_list, &sdata->dev->mc,
  373. sdata->dev->addr_len);
  374. spin_unlock_bh(&local->filter_lock);
  375. netif_addr_unlock_bh(sdata->dev);
  376. ieee80211_configure_filter(local);
  377. del_timer_sync(&local->dynamic_ps_timer);
  378. cancel_work_sync(&local->dynamic_ps_enable_work);
  379. /* APs need special treatment */
  380. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  381. struct ieee80211_sub_if_data *vlan, *tmpsdata;
  382. struct beacon_data *old_beacon = sdata->u.ap.beacon;
  383. /* sdata_running will return false, so this will disable */
  384. ieee80211_bss_info_change_notify(sdata,
  385. BSS_CHANGED_BEACON_ENABLED);
  386. /* remove beacon */
  387. rcu_assign_pointer(sdata->u.ap.beacon, NULL);
  388. synchronize_rcu();
  389. kfree(old_beacon);
  390. /* free all potentially still buffered bcast frames */
  391. while ((skb = skb_dequeue(&sdata->u.ap.ps_bc_buf))) {
  392. local->total_ps_buffered--;
  393. dev_kfree_skb(skb);
  394. }
  395. /* down all dependent devices, that is VLANs */
  396. list_for_each_entry_safe(vlan, tmpsdata, &sdata->u.ap.vlans,
  397. u.vlan.list)
  398. dev_close(vlan->dev);
  399. WARN_ON(!list_empty(&sdata->u.ap.vlans));
  400. }
  401. if (going_down)
  402. local->open_count--;
  403. switch (sdata->vif.type) {
  404. case NL80211_IFTYPE_AP_VLAN:
  405. list_del(&sdata->u.vlan.list);
  406. /* no need to tell driver */
  407. break;
  408. case NL80211_IFTYPE_MONITOR:
  409. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  410. local->cooked_mntrs--;
  411. break;
  412. }
  413. local->monitors--;
  414. if (local->monitors == 0) {
  415. local->hw.conf.flags &= ~IEEE80211_CONF_MONITOR;
  416. hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
  417. }
  418. ieee80211_adjust_monitor_flags(sdata, -1);
  419. ieee80211_configure_filter(local);
  420. break;
  421. default:
  422. flush_work(&sdata->work);
  423. /*
  424. * When we get here, the interface is marked down.
  425. * Call synchronize_rcu() to wait for the RX path
  426. * should it be using the interface and enqueuing
  427. * frames at this very time on another CPU.
  428. */
  429. synchronize_rcu();
  430. skb_queue_purge(&sdata->skb_queue);
  431. /*
  432. * Disable beaconing here for mesh only, AP and IBSS
  433. * are already taken care of.
  434. */
  435. if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  436. ieee80211_bss_info_change_notify(sdata,
  437. BSS_CHANGED_BEACON_ENABLED);
  438. /*
  439. * Free all remaining keys, there shouldn't be any,
  440. * except maybe group keys in AP more or WDS?
  441. */
  442. ieee80211_free_keys(sdata);
  443. if (going_down)
  444. drv_remove_interface(local, &sdata->vif);
  445. }
  446. sdata->bss = NULL;
  447. mutex_lock(&local->mtx);
  448. hw_reconf_flags |= __ieee80211_recalc_idle(local);
  449. mutex_unlock(&local->mtx);
  450. ieee80211_recalc_ps(local, -1);
  451. if (local->open_count == 0) {
  452. if (local->ops->napi_poll)
  453. napi_disable(&local->napi);
  454. ieee80211_clear_tx_pending(local);
  455. ieee80211_stop_device(local);
  456. /* no reconfiguring after stop! */
  457. hw_reconf_flags = 0;
  458. }
  459. /* do after stop to avoid reconfiguring when we stop anyway */
  460. if (hw_reconf_flags)
  461. ieee80211_hw_config(local, hw_reconf_flags);
  462. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  463. for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
  464. skb_queue_walk_safe(&local->pending[i], skb, tmp) {
  465. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  466. if (info->control.vif == &sdata->vif) {
  467. __skb_unlink(skb, &local->pending[i]);
  468. dev_kfree_skb_irq(skb);
  469. }
  470. }
  471. }
  472. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  473. }
  474. static int ieee80211_stop(struct net_device *dev)
  475. {
  476. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  477. ieee80211_do_stop(sdata, true);
  478. return 0;
  479. }
  480. static void ieee80211_set_multicast_list(struct net_device *dev)
  481. {
  482. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  483. struct ieee80211_local *local = sdata->local;
  484. int allmulti, promisc, sdata_allmulti, sdata_promisc;
  485. allmulti = !!(dev->flags & IFF_ALLMULTI);
  486. promisc = !!(dev->flags & IFF_PROMISC);
  487. sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
  488. sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
  489. if (allmulti != sdata_allmulti) {
  490. if (dev->flags & IFF_ALLMULTI)
  491. atomic_inc(&local->iff_allmultis);
  492. else
  493. atomic_dec(&local->iff_allmultis);
  494. sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
  495. }
  496. if (promisc != sdata_promisc) {
  497. if (dev->flags & IFF_PROMISC)
  498. atomic_inc(&local->iff_promiscs);
  499. else
  500. atomic_dec(&local->iff_promiscs);
  501. sdata->flags ^= IEEE80211_SDATA_PROMISC;
  502. }
  503. spin_lock_bh(&local->filter_lock);
  504. __hw_addr_sync(&local->mc_list, &dev->mc, dev->addr_len);
  505. spin_unlock_bh(&local->filter_lock);
  506. ieee80211_queue_work(&local->hw, &local->reconfig_filter);
  507. }
  508. /*
  509. * Called when the netdev is removed or, by the code below, before
  510. * the interface type changes.
  511. */
  512. static void ieee80211_teardown_sdata(struct net_device *dev)
  513. {
  514. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  515. struct ieee80211_local *local = sdata->local;
  516. int flushed;
  517. int i;
  518. /* free extra data */
  519. ieee80211_free_keys(sdata);
  520. ieee80211_debugfs_remove_netdev(sdata);
  521. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  522. __skb_queue_purge(&sdata->fragments[i].skb_list);
  523. sdata->fragment_next = 0;
  524. if (ieee80211_vif_is_mesh(&sdata->vif))
  525. mesh_rmc_free(sdata);
  526. flushed = sta_info_flush(local, sdata);
  527. WARN_ON(flushed);
  528. }
  529. static u16 ieee80211_netdev_select_queue(struct net_device *dev,
  530. struct sk_buff *skb)
  531. {
  532. return ieee80211_select_queue(IEEE80211_DEV_TO_SUB_IF(dev), skb);
  533. }
  534. static const struct net_device_ops ieee80211_dataif_ops = {
  535. .ndo_open = ieee80211_open,
  536. .ndo_stop = ieee80211_stop,
  537. .ndo_uninit = ieee80211_teardown_sdata,
  538. .ndo_start_xmit = ieee80211_subif_start_xmit,
  539. .ndo_set_multicast_list = ieee80211_set_multicast_list,
  540. .ndo_change_mtu = ieee80211_change_mtu,
  541. .ndo_set_mac_address = ieee80211_change_mac,
  542. .ndo_select_queue = ieee80211_netdev_select_queue,
  543. };
  544. static u16 ieee80211_monitor_select_queue(struct net_device *dev,
  545. struct sk_buff *skb)
  546. {
  547. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  548. struct ieee80211_local *local = sdata->local;
  549. struct ieee80211_hdr *hdr;
  550. struct ieee80211_radiotap_header *rtap = (void *)skb->data;
  551. u8 *p;
  552. if (local->hw.queues < 4)
  553. return 0;
  554. if (skb->len < 4 ||
  555. skb->len < le16_to_cpu(rtap->it_len) + 2 /* frame control */)
  556. return 0; /* doesn't matter, frame will be dropped */
  557. hdr = (void *)((u8 *)skb->data + le16_to_cpu(rtap->it_len));
  558. if (!ieee80211_is_data(hdr->frame_control)) {
  559. skb->priority = 7;
  560. return ieee802_1d_to_ac[skb->priority];
  561. }
  562. if (!ieee80211_is_data_qos(hdr->frame_control)) {
  563. skb->priority = 0;
  564. return ieee802_1d_to_ac[skb->priority];
  565. }
  566. p = ieee80211_get_qos_ctl(hdr);
  567. skb->priority = *p & IEEE80211_QOS_CTL_TAG1D_MASK;
  568. return ieee80211_downgrade_queue(local, skb);
  569. }
  570. static const struct net_device_ops ieee80211_monitorif_ops = {
  571. .ndo_open = ieee80211_open,
  572. .ndo_stop = ieee80211_stop,
  573. .ndo_uninit = ieee80211_teardown_sdata,
  574. .ndo_start_xmit = ieee80211_monitor_start_xmit,
  575. .ndo_set_multicast_list = ieee80211_set_multicast_list,
  576. .ndo_change_mtu = ieee80211_change_mtu,
  577. .ndo_set_mac_address = eth_mac_addr,
  578. .ndo_select_queue = ieee80211_monitor_select_queue,
  579. };
  580. static void ieee80211_if_setup(struct net_device *dev)
  581. {
  582. ether_setup(dev);
  583. dev->netdev_ops = &ieee80211_dataif_ops;
  584. dev->destructor = free_netdev;
  585. }
  586. static void ieee80211_iface_work(struct work_struct *work)
  587. {
  588. struct ieee80211_sub_if_data *sdata =
  589. container_of(work, struct ieee80211_sub_if_data, work);
  590. struct ieee80211_local *local = sdata->local;
  591. struct sk_buff *skb;
  592. struct sta_info *sta;
  593. struct ieee80211_ra_tid *ra_tid;
  594. if (!ieee80211_sdata_running(sdata))
  595. return;
  596. if (local->scanning)
  597. return;
  598. /*
  599. * ieee80211_queue_work() should have picked up most cases,
  600. * here we'll pick the rest.
  601. */
  602. if (WARN(local->suspended,
  603. "interface work scheduled while going to suspend\n"))
  604. return;
  605. /* first process frames */
  606. while ((skb = skb_dequeue(&sdata->skb_queue))) {
  607. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  608. if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_START) {
  609. ra_tid = (void *)&skb->cb;
  610. ieee80211_start_tx_ba_cb(&sdata->vif, ra_tid->ra,
  611. ra_tid->tid);
  612. } else if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_STOP) {
  613. ra_tid = (void *)&skb->cb;
  614. ieee80211_stop_tx_ba_cb(&sdata->vif, ra_tid->ra,
  615. ra_tid->tid);
  616. } else if (ieee80211_is_action(mgmt->frame_control) &&
  617. mgmt->u.action.category == WLAN_CATEGORY_BACK) {
  618. int len = skb->len;
  619. mutex_lock(&local->sta_mtx);
  620. sta = sta_info_get_bss(sdata, mgmt->sa);
  621. if (sta) {
  622. switch (mgmt->u.action.u.addba_req.action_code) {
  623. case WLAN_ACTION_ADDBA_REQ:
  624. ieee80211_process_addba_request(
  625. local, sta, mgmt, len);
  626. break;
  627. case WLAN_ACTION_ADDBA_RESP:
  628. ieee80211_process_addba_resp(local, sta,
  629. mgmt, len);
  630. break;
  631. case WLAN_ACTION_DELBA:
  632. ieee80211_process_delba(sdata, sta,
  633. mgmt, len);
  634. break;
  635. default:
  636. WARN_ON(1);
  637. break;
  638. }
  639. }
  640. mutex_unlock(&local->sta_mtx);
  641. } else if (ieee80211_is_data_qos(mgmt->frame_control)) {
  642. struct ieee80211_hdr *hdr = (void *)mgmt;
  643. /*
  644. * So the frame isn't mgmt, but frame_control
  645. * is at the right place anyway, of course, so
  646. * the if statement is correct.
  647. *
  648. * Warn if we have other data frame types here,
  649. * they must not get here.
  650. */
  651. WARN_ON(hdr->frame_control &
  652. cpu_to_le16(IEEE80211_STYPE_NULLFUNC));
  653. WARN_ON(!(hdr->seq_ctrl &
  654. cpu_to_le16(IEEE80211_SCTL_FRAG)));
  655. /*
  656. * This was a fragment of a frame, received while
  657. * a block-ack session was active. That cannot be
  658. * right, so terminate the session.
  659. */
  660. mutex_lock(&local->sta_mtx);
  661. sta = sta_info_get_bss(sdata, mgmt->sa);
  662. if (sta) {
  663. u16 tid = *ieee80211_get_qos_ctl(hdr) &
  664. IEEE80211_QOS_CTL_TID_MASK;
  665. __ieee80211_stop_rx_ba_session(
  666. sta, tid, WLAN_BACK_RECIPIENT,
  667. WLAN_REASON_QSTA_REQUIRE_SETUP,
  668. true);
  669. }
  670. mutex_unlock(&local->sta_mtx);
  671. } else switch (sdata->vif.type) {
  672. case NL80211_IFTYPE_STATION:
  673. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  674. break;
  675. case NL80211_IFTYPE_ADHOC:
  676. ieee80211_ibss_rx_queued_mgmt(sdata, skb);
  677. break;
  678. case NL80211_IFTYPE_MESH_POINT:
  679. if (!ieee80211_vif_is_mesh(&sdata->vif))
  680. break;
  681. ieee80211_mesh_rx_queued_mgmt(sdata, skb);
  682. break;
  683. default:
  684. WARN(1, "frame for unexpected interface type");
  685. break;
  686. }
  687. kfree_skb(skb);
  688. }
  689. /* then other type-dependent work */
  690. switch (sdata->vif.type) {
  691. case NL80211_IFTYPE_STATION:
  692. ieee80211_sta_work(sdata);
  693. break;
  694. case NL80211_IFTYPE_ADHOC:
  695. ieee80211_ibss_work(sdata);
  696. break;
  697. case NL80211_IFTYPE_MESH_POINT:
  698. if (!ieee80211_vif_is_mesh(&sdata->vif))
  699. break;
  700. ieee80211_mesh_work(sdata);
  701. break;
  702. default:
  703. break;
  704. }
  705. }
  706. /*
  707. * Helper function to initialise an interface to a specific type.
  708. */
  709. static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
  710. enum nl80211_iftype type)
  711. {
  712. /* clear type-dependent union */
  713. memset(&sdata->u, 0, sizeof(sdata->u));
  714. /* and set some type-dependent values */
  715. sdata->vif.type = type;
  716. sdata->vif.p2p = false;
  717. sdata->dev->netdev_ops = &ieee80211_dataif_ops;
  718. sdata->wdev.iftype = type;
  719. sdata->control_port_protocol = cpu_to_be16(ETH_P_PAE);
  720. sdata->control_port_no_encrypt = false;
  721. /* only monitor differs */
  722. sdata->dev->type = ARPHRD_ETHER;
  723. skb_queue_head_init(&sdata->skb_queue);
  724. INIT_WORK(&sdata->work, ieee80211_iface_work);
  725. switch (type) {
  726. case NL80211_IFTYPE_P2P_GO:
  727. type = NL80211_IFTYPE_AP;
  728. sdata->vif.type = type;
  729. sdata->vif.p2p = true;
  730. /* fall through */
  731. case NL80211_IFTYPE_AP:
  732. skb_queue_head_init(&sdata->u.ap.ps_bc_buf);
  733. INIT_LIST_HEAD(&sdata->u.ap.vlans);
  734. break;
  735. case NL80211_IFTYPE_P2P_CLIENT:
  736. type = NL80211_IFTYPE_STATION;
  737. sdata->vif.type = type;
  738. sdata->vif.p2p = true;
  739. /* fall through */
  740. case NL80211_IFTYPE_STATION:
  741. ieee80211_sta_setup_sdata(sdata);
  742. break;
  743. case NL80211_IFTYPE_ADHOC:
  744. ieee80211_ibss_setup_sdata(sdata);
  745. break;
  746. case NL80211_IFTYPE_MESH_POINT:
  747. if (ieee80211_vif_is_mesh(&sdata->vif))
  748. ieee80211_mesh_init_sdata(sdata);
  749. break;
  750. case NL80211_IFTYPE_MONITOR:
  751. sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
  752. sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
  753. sdata->u.mntr_flags = MONITOR_FLAG_CONTROL |
  754. MONITOR_FLAG_OTHER_BSS;
  755. break;
  756. case NL80211_IFTYPE_WDS:
  757. case NL80211_IFTYPE_AP_VLAN:
  758. break;
  759. case NL80211_IFTYPE_UNSPECIFIED:
  760. case NUM_NL80211_IFTYPES:
  761. BUG();
  762. break;
  763. }
  764. ieee80211_debugfs_add_netdev(sdata);
  765. }
  766. static int ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data *sdata,
  767. enum nl80211_iftype type)
  768. {
  769. struct ieee80211_local *local = sdata->local;
  770. int ret, err;
  771. enum nl80211_iftype internal_type = type;
  772. bool p2p = false;
  773. ASSERT_RTNL();
  774. if (!local->ops->change_interface)
  775. return -EBUSY;
  776. switch (sdata->vif.type) {
  777. case NL80211_IFTYPE_AP:
  778. case NL80211_IFTYPE_STATION:
  779. case NL80211_IFTYPE_ADHOC:
  780. /*
  781. * Could maybe also all others here?
  782. * Just not sure how that interacts
  783. * with the RX/config path e.g. for
  784. * mesh.
  785. */
  786. break;
  787. default:
  788. return -EBUSY;
  789. }
  790. switch (type) {
  791. case NL80211_IFTYPE_AP:
  792. case NL80211_IFTYPE_STATION:
  793. case NL80211_IFTYPE_ADHOC:
  794. /*
  795. * Could probably support everything
  796. * but WDS here (WDS do_open can fail
  797. * under memory pressure, which this
  798. * code isn't prepared to handle).
  799. */
  800. break;
  801. case NL80211_IFTYPE_P2P_CLIENT:
  802. p2p = true;
  803. internal_type = NL80211_IFTYPE_STATION;
  804. break;
  805. case NL80211_IFTYPE_P2P_GO:
  806. p2p = true;
  807. internal_type = NL80211_IFTYPE_AP;
  808. break;
  809. default:
  810. return -EBUSY;
  811. }
  812. ret = ieee80211_check_concurrent_iface(sdata, internal_type);
  813. if (ret)
  814. return ret;
  815. ieee80211_do_stop(sdata, false);
  816. ieee80211_teardown_sdata(sdata->dev);
  817. ret = drv_change_interface(local, sdata, internal_type, p2p);
  818. if (ret)
  819. type = sdata->vif.type;
  820. ieee80211_setup_sdata(sdata, type);
  821. err = ieee80211_do_open(sdata->dev, false);
  822. WARN(err, "type change: do_open returned %d", err);
  823. return ret;
  824. }
  825. int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
  826. enum nl80211_iftype type)
  827. {
  828. int ret;
  829. ASSERT_RTNL();
  830. if (type == ieee80211_vif_type_p2p(&sdata->vif))
  831. return 0;
  832. /* Setting ad-hoc mode on non-IBSS channel is not supported. */
  833. if (sdata->local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS &&
  834. type == NL80211_IFTYPE_ADHOC)
  835. return -EOPNOTSUPP;
  836. if (ieee80211_sdata_running(sdata)) {
  837. ret = ieee80211_runtime_change_iftype(sdata, type);
  838. if (ret)
  839. return ret;
  840. } else {
  841. /* Purge and reset type-dependent state. */
  842. ieee80211_teardown_sdata(sdata->dev);
  843. ieee80211_setup_sdata(sdata, type);
  844. }
  845. /* reset some values that shouldn't be kept across type changes */
  846. sdata->vif.bss_conf.basic_rates =
  847. ieee80211_mandatory_rates(sdata->local,
  848. sdata->local->hw.conf.channel->band);
  849. sdata->drop_unencrypted = 0;
  850. if (type == NL80211_IFTYPE_STATION)
  851. sdata->u.mgd.use_4addr = false;
  852. return 0;
  853. }
  854. static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
  855. struct net_device *dev,
  856. enum nl80211_iftype type)
  857. {
  858. struct ieee80211_sub_if_data *sdata;
  859. u64 mask, start, addr, val, inc;
  860. u8 *m;
  861. u8 tmp_addr[ETH_ALEN];
  862. int i;
  863. /* default ... something at least */
  864. memcpy(dev->perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  865. if (is_zero_ether_addr(local->hw.wiphy->addr_mask) &&
  866. local->hw.wiphy->n_addresses <= 1)
  867. return;
  868. mutex_lock(&local->iflist_mtx);
  869. switch (type) {
  870. case NL80211_IFTYPE_MONITOR:
  871. /* doesn't matter */
  872. break;
  873. case NL80211_IFTYPE_WDS:
  874. case NL80211_IFTYPE_AP_VLAN:
  875. /* match up with an AP interface */
  876. list_for_each_entry(sdata, &local->interfaces, list) {
  877. if (sdata->vif.type != NL80211_IFTYPE_AP)
  878. continue;
  879. memcpy(dev->perm_addr, sdata->vif.addr, ETH_ALEN);
  880. break;
  881. }
  882. /* keep default if no AP interface present */
  883. break;
  884. default:
  885. /* assign a new address if possible -- try n_addresses first */
  886. for (i = 0; i < local->hw.wiphy->n_addresses; i++) {
  887. bool used = false;
  888. list_for_each_entry(sdata, &local->interfaces, list) {
  889. if (memcmp(local->hw.wiphy->addresses[i].addr,
  890. sdata->vif.addr, ETH_ALEN) == 0) {
  891. used = true;
  892. break;
  893. }
  894. }
  895. if (!used) {
  896. memcpy(dev->perm_addr,
  897. local->hw.wiphy->addresses[i].addr,
  898. ETH_ALEN);
  899. break;
  900. }
  901. }
  902. /* try mask if available */
  903. if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
  904. break;
  905. m = local->hw.wiphy->addr_mask;
  906. mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  907. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  908. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  909. if (__ffs64(mask) + hweight64(mask) != fls64(mask)) {
  910. /* not a contiguous mask ... not handled now! */
  911. printk(KERN_DEBUG "not contiguous\n");
  912. break;
  913. }
  914. m = local->hw.wiphy->perm_addr;
  915. start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  916. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  917. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  918. inc = 1ULL<<__ffs64(mask);
  919. val = (start & mask);
  920. addr = (start & ~mask) | (val & mask);
  921. do {
  922. bool used = false;
  923. tmp_addr[5] = addr >> 0*8;
  924. tmp_addr[4] = addr >> 1*8;
  925. tmp_addr[3] = addr >> 2*8;
  926. tmp_addr[2] = addr >> 3*8;
  927. tmp_addr[1] = addr >> 4*8;
  928. tmp_addr[0] = addr >> 5*8;
  929. val += inc;
  930. list_for_each_entry(sdata, &local->interfaces, list) {
  931. if (memcmp(tmp_addr, sdata->vif.addr,
  932. ETH_ALEN) == 0) {
  933. used = true;
  934. break;
  935. }
  936. }
  937. if (!used) {
  938. memcpy(dev->perm_addr, tmp_addr, ETH_ALEN);
  939. break;
  940. }
  941. addr = (start & ~mask) | (val & mask);
  942. } while (addr != start);
  943. break;
  944. }
  945. mutex_unlock(&local->iflist_mtx);
  946. }
  947. int ieee80211_if_add(struct ieee80211_local *local, const char *name,
  948. struct net_device **new_dev, enum nl80211_iftype type,
  949. struct vif_params *params)
  950. {
  951. struct net_device *ndev;
  952. struct ieee80211_sub_if_data *sdata = NULL;
  953. int ret, i;
  954. ASSERT_RTNL();
  955. ndev = alloc_netdev_mq(sizeof(*sdata) + local->hw.vif_data_size,
  956. name, ieee80211_if_setup, local->hw.queues);
  957. if (!ndev)
  958. return -ENOMEM;
  959. dev_net_set(ndev, wiphy_net(local->hw.wiphy));
  960. ndev->needed_headroom = local->tx_headroom +
  961. 4*6 /* four MAC addresses */
  962. + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
  963. + 6 /* mesh */
  964. + 8 /* rfc1042/bridge tunnel */
  965. - ETH_HLEN /* ethernet hard_header_len */
  966. + IEEE80211_ENCRYPT_HEADROOM;
  967. ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
  968. ret = dev_alloc_name(ndev, ndev->name);
  969. if (ret < 0)
  970. goto fail;
  971. ieee80211_assign_perm_addr(local, ndev, type);
  972. memcpy(ndev->dev_addr, ndev->perm_addr, ETH_ALEN);
  973. SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
  974. /* don't use IEEE80211_DEV_TO_SUB_IF because it checks too much */
  975. sdata = netdev_priv(ndev);
  976. ndev->ieee80211_ptr = &sdata->wdev;
  977. memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
  978. memcpy(sdata->name, ndev->name, IFNAMSIZ);
  979. /* initialise type-independent data */
  980. sdata->wdev.wiphy = local->hw.wiphy;
  981. sdata->local = local;
  982. sdata->dev = ndev;
  983. #ifdef CONFIG_INET
  984. sdata->arp_filter_state = true;
  985. #endif
  986. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  987. skb_queue_head_init(&sdata->fragments[i].skb_list);
  988. INIT_LIST_HEAD(&sdata->key_list);
  989. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  990. struct ieee80211_supported_band *sband;
  991. sband = local->hw.wiphy->bands[i];
  992. sdata->rc_rateidx_mask[i] =
  993. sband ? (1 << sband->n_bitrates) - 1 : 0;
  994. }
  995. /* setup type-dependent data */
  996. ieee80211_setup_sdata(sdata, type);
  997. if (params) {
  998. ndev->ieee80211_ptr->use_4addr = params->use_4addr;
  999. if (type == NL80211_IFTYPE_STATION)
  1000. sdata->u.mgd.use_4addr = params->use_4addr;
  1001. }
  1002. ret = register_netdevice(ndev);
  1003. if (ret)
  1004. goto fail;
  1005. mutex_lock(&local->iflist_mtx);
  1006. list_add_tail_rcu(&sdata->list, &local->interfaces);
  1007. mutex_unlock(&local->iflist_mtx);
  1008. if (new_dev)
  1009. *new_dev = ndev;
  1010. return 0;
  1011. fail:
  1012. free_netdev(ndev);
  1013. return ret;
  1014. }
  1015. void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
  1016. {
  1017. ASSERT_RTNL();
  1018. mutex_lock(&sdata->local->iflist_mtx);
  1019. list_del_rcu(&sdata->list);
  1020. mutex_unlock(&sdata->local->iflist_mtx);
  1021. synchronize_rcu();
  1022. unregister_netdevice(sdata->dev);
  1023. }
  1024. /*
  1025. * Remove all interfaces, may only be called at hardware unregistration
  1026. * time because it doesn't do RCU-safe list removals.
  1027. */
  1028. void ieee80211_remove_interfaces(struct ieee80211_local *local)
  1029. {
  1030. struct ieee80211_sub_if_data *sdata, *tmp;
  1031. LIST_HEAD(unreg_list);
  1032. ASSERT_RTNL();
  1033. mutex_lock(&local->iflist_mtx);
  1034. list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
  1035. list_del(&sdata->list);
  1036. unregister_netdevice_queue(sdata->dev, &unreg_list);
  1037. }
  1038. mutex_unlock(&local->iflist_mtx);
  1039. unregister_netdevice_many(&unreg_list);
  1040. }
  1041. static u32 ieee80211_idle_off(struct ieee80211_local *local,
  1042. const char *reason)
  1043. {
  1044. if (!(local->hw.conf.flags & IEEE80211_CONF_IDLE))
  1045. return 0;
  1046. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1047. wiphy_debug(local->hw.wiphy, "device no longer idle - %s\n", reason);
  1048. #endif
  1049. local->hw.conf.flags &= ~IEEE80211_CONF_IDLE;
  1050. return IEEE80211_CONF_CHANGE_IDLE;
  1051. }
  1052. static u32 ieee80211_idle_on(struct ieee80211_local *local)
  1053. {
  1054. if (local->hw.conf.flags & IEEE80211_CONF_IDLE)
  1055. return 0;
  1056. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1057. wiphy_debug(local->hw.wiphy, "device now idle\n");
  1058. #endif
  1059. drv_flush(local, false);
  1060. local->hw.conf.flags |= IEEE80211_CONF_IDLE;
  1061. return IEEE80211_CONF_CHANGE_IDLE;
  1062. }
  1063. u32 __ieee80211_recalc_idle(struct ieee80211_local *local)
  1064. {
  1065. struct ieee80211_sub_if_data *sdata;
  1066. int count = 0;
  1067. bool working = false, scanning = false;
  1068. struct ieee80211_work *wk;
  1069. #ifdef CONFIG_PROVE_LOCKING
  1070. WARN_ON(debug_locks && !lockdep_rtnl_is_held() &&
  1071. !lockdep_is_held(&local->iflist_mtx));
  1072. #endif
  1073. lockdep_assert_held(&local->mtx);
  1074. list_for_each_entry(sdata, &local->interfaces, list) {
  1075. if (!ieee80211_sdata_running(sdata)) {
  1076. sdata->vif.bss_conf.idle = true;
  1077. continue;
  1078. }
  1079. sdata->old_idle = sdata->vif.bss_conf.idle;
  1080. /* do not count disabled managed interfaces */
  1081. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  1082. !sdata->u.mgd.associated) {
  1083. sdata->vif.bss_conf.idle = true;
  1084. continue;
  1085. }
  1086. /* do not count unused IBSS interfaces */
  1087. if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  1088. !sdata->u.ibss.ssid_len) {
  1089. sdata->vif.bss_conf.idle = true;
  1090. continue;
  1091. }
  1092. /* count everything else */
  1093. count++;
  1094. }
  1095. list_for_each_entry(wk, &local->work_list, list) {
  1096. working = true;
  1097. wk->sdata->vif.bss_conf.idle = false;
  1098. }
  1099. if (local->scan_sdata) {
  1100. scanning = true;
  1101. local->scan_sdata->vif.bss_conf.idle = false;
  1102. }
  1103. list_for_each_entry(sdata, &local->interfaces, list) {
  1104. if (sdata->old_idle == sdata->vif.bss_conf.idle)
  1105. continue;
  1106. if (!ieee80211_sdata_running(sdata))
  1107. continue;
  1108. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
  1109. }
  1110. if (working)
  1111. return ieee80211_idle_off(local, "working");
  1112. if (scanning)
  1113. return ieee80211_idle_off(local, "scanning");
  1114. if (!count)
  1115. return ieee80211_idle_on(local);
  1116. else
  1117. return ieee80211_idle_off(local, "in use");
  1118. return 0;
  1119. }
  1120. void ieee80211_recalc_idle(struct ieee80211_local *local)
  1121. {
  1122. u32 chg;
  1123. mutex_lock(&local->iflist_mtx);
  1124. chg = __ieee80211_recalc_idle(local);
  1125. mutex_unlock(&local->iflist_mtx);
  1126. if (chg)
  1127. ieee80211_hw_config(local, chg);
  1128. }
  1129. static int netdev_notify(struct notifier_block *nb,
  1130. unsigned long state,
  1131. void *ndev)
  1132. {
  1133. struct net_device *dev = ndev;
  1134. struct ieee80211_sub_if_data *sdata;
  1135. if (state != NETDEV_CHANGENAME)
  1136. return 0;
  1137. if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
  1138. return 0;
  1139. if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
  1140. return 0;
  1141. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1142. memcpy(sdata->name, dev->name, IFNAMSIZ);
  1143. ieee80211_debugfs_rename_netdev(sdata);
  1144. return 0;
  1145. }
  1146. static struct notifier_block mac80211_netdev_notifier = {
  1147. .notifier_call = netdev_notify,
  1148. };
  1149. int ieee80211_iface_init(void)
  1150. {
  1151. return register_netdevice_notifier(&mac80211_netdev_notifier);
  1152. }
  1153. void ieee80211_iface_exit(void)
  1154. {
  1155. unregister_netdevice_notifier(&mac80211_netdev_notifier);
  1156. }