iface.c 36 KB

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