iface.c 36 KB

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