iface.c 35 KB

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