iface.c 40 KB

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