iface.c 35 KB

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