iface.c 40 KB

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