iface.c 40 KB

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