iface.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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/kernel.h>
  14. #include <linux/if_arp.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/rtnetlink.h>
  17. #include <net/mac80211.h>
  18. #include "ieee80211_i.h"
  19. #include "sta_info.h"
  20. #include "debugfs_netdev.h"
  21. #include "mesh.h"
  22. #include "led.h"
  23. /**
  24. * DOC: Interface list locking
  25. *
  26. * The interface list in each struct ieee80211_local is protected
  27. * three-fold:
  28. *
  29. * (1) modifications may only be done under the RTNL
  30. * (2) modifications and readers are protected against each other by
  31. * the iflist_mtx.
  32. * (3) modifications are done in an RCU manner so atomic readers
  33. * can traverse the list in RCU-safe blocks.
  34. *
  35. * As a consequence, reads (traversals) of the list can be protected
  36. * by either the RTNL, the iflist_mtx or RCU.
  37. */
  38. static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
  39. {
  40. int meshhdrlen;
  41. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  42. meshhdrlen = (sdata->vif.type == NL80211_IFTYPE_MESH_POINT) ? 5 : 0;
  43. /* FIX: what would be proper limits for MTU?
  44. * This interface uses 802.3 frames. */
  45. if (new_mtu < 256 ||
  46. new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6 - meshhdrlen) {
  47. return -EINVAL;
  48. }
  49. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  50. printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
  51. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  52. dev->mtu = new_mtu;
  53. return 0;
  54. }
  55. static inline int identical_mac_addr_allowed(int type1, int type2)
  56. {
  57. return type1 == NL80211_IFTYPE_MONITOR ||
  58. type2 == NL80211_IFTYPE_MONITOR ||
  59. (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_WDS) ||
  60. (type1 == NL80211_IFTYPE_WDS &&
  61. (type2 == NL80211_IFTYPE_WDS ||
  62. type2 == NL80211_IFTYPE_AP)) ||
  63. (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_AP_VLAN) ||
  64. (type1 == NL80211_IFTYPE_AP_VLAN &&
  65. (type2 == NL80211_IFTYPE_AP ||
  66. type2 == NL80211_IFTYPE_AP_VLAN));
  67. }
  68. static int ieee80211_open(struct net_device *dev)
  69. {
  70. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  71. struct ieee80211_sub_if_data *nsdata;
  72. struct ieee80211_local *local = sdata->local;
  73. struct sta_info *sta;
  74. struct ieee80211_if_init_conf conf;
  75. u32 changed = 0;
  76. int res;
  77. u32 hw_reconf_flags = 0;
  78. u8 null_addr[ETH_ALEN] = {0};
  79. /* fail early if user set an invalid address */
  80. if (compare_ether_addr(dev->dev_addr, null_addr) &&
  81. !is_valid_ether_addr(dev->dev_addr))
  82. return -EADDRNOTAVAIL;
  83. /* we hold the RTNL here so can safely walk the list */
  84. list_for_each_entry(nsdata, &local->interfaces, list) {
  85. struct net_device *ndev = nsdata->dev;
  86. if (ndev != dev && netif_running(ndev)) {
  87. /*
  88. * Allow only a single IBSS interface to be up at any
  89. * time. This is restricted because beacon distribution
  90. * cannot work properly if both are in the same IBSS.
  91. *
  92. * To remove this restriction we'd have to disallow them
  93. * from setting the same SSID on different IBSS interfaces
  94. * belonging to the same hardware. Then, however, we're
  95. * faced with having to adopt two different TSF timers...
  96. */
  97. if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  98. nsdata->vif.type == NL80211_IFTYPE_ADHOC)
  99. return -EBUSY;
  100. /*
  101. * The remaining checks are only performed for interfaces
  102. * with the same MAC address.
  103. */
  104. if (compare_ether_addr(dev->dev_addr, ndev->dev_addr))
  105. continue;
  106. /*
  107. * check whether it may have the same address
  108. */
  109. if (!identical_mac_addr_allowed(sdata->vif.type,
  110. nsdata->vif.type))
  111. return -ENOTUNIQ;
  112. /*
  113. * can only add VLANs to enabled APs
  114. */
  115. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  116. nsdata->vif.type == NL80211_IFTYPE_AP)
  117. sdata->bss = &nsdata->u.ap;
  118. }
  119. }
  120. switch (sdata->vif.type) {
  121. case NL80211_IFTYPE_WDS:
  122. if (!is_valid_ether_addr(sdata->u.wds.remote_addr))
  123. return -ENOLINK;
  124. break;
  125. case NL80211_IFTYPE_AP_VLAN:
  126. if (!sdata->bss)
  127. return -ENOLINK;
  128. list_add(&sdata->u.vlan.list, &sdata->bss->vlans);
  129. break;
  130. case NL80211_IFTYPE_AP:
  131. sdata->bss = &sdata->u.ap;
  132. break;
  133. case NL80211_IFTYPE_MESH_POINT:
  134. if (!ieee80211_vif_is_mesh(&sdata->vif))
  135. break;
  136. /* mesh ifaces must set allmulti to forward mcast traffic */
  137. atomic_inc(&local->iff_allmultis);
  138. break;
  139. case NL80211_IFTYPE_STATION:
  140. case NL80211_IFTYPE_MONITOR:
  141. case NL80211_IFTYPE_ADHOC:
  142. /* no special treatment */
  143. break;
  144. case NL80211_IFTYPE_UNSPECIFIED:
  145. case __NL80211_IFTYPE_AFTER_LAST:
  146. /* cannot happen */
  147. WARN_ON(1);
  148. break;
  149. }
  150. if (local->open_count == 0) {
  151. res = 0;
  152. if (local->ops->start)
  153. res = local->ops->start(local_to_hw(local));
  154. if (res)
  155. goto err_del_bss;
  156. /* we're brought up, everything changes */
  157. hw_reconf_flags = ~0;
  158. ieee80211_led_radio(local, local->hw.conf.radio_enabled);
  159. }
  160. /*
  161. * Check all interfaces and copy the hopefully now-present
  162. * MAC address to those that have the special null one.
  163. */
  164. list_for_each_entry(nsdata, &local->interfaces, list) {
  165. struct net_device *ndev = nsdata->dev;
  166. /*
  167. * No need to check netif_running since we do not allow
  168. * it to start up with this invalid address.
  169. */
  170. if (compare_ether_addr(null_addr, ndev->dev_addr) == 0)
  171. memcpy(ndev->dev_addr,
  172. local->hw.wiphy->perm_addr,
  173. ETH_ALEN);
  174. }
  175. if (compare_ether_addr(null_addr, local->mdev->dev_addr) == 0)
  176. memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr,
  177. ETH_ALEN);
  178. /*
  179. * Validate the MAC address for this device.
  180. */
  181. if (!is_valid_ether_addr(dev->dev_addr)) {
  182. if (!local->open_count && local->ops->stop)
  183. local->ops->stop(local_to_hw(local));
  184. return -EADDRNOTAVAIL;
  185. }
  186. switch (sdata->vif.type) {
  187. case NL80211_IFTYPE_AP_VLAN:
  188. /* no need to tell driver */
  189. break;
  190. case NL80211_IFTYPE_MONITOR:
  191. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  192. local->cooked_mntrs++;
  193. break;
  194. }
  195. /* must be before the call to ieee80211_configure_filter */
  196. local->monitors++;
  197. if (local->monitors == 1) {
  198. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  199. hw_reconf_flags |= IEEE80211_CONF_CHANGE_RADIOTAP;
  200. }
  201. if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
  202. local->fif_fcsfail++;
  203. if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
  204. local->fif_plcpfail++;
  205. if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
  206. local->fif_control++;
  207. if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
  208. local->fif_other_bss++;
  209. netif_addr_lock_bh(local->mdev);
  210. ieee80211_configure_filter(local);
  211. netif_addr_unlock_bh(local->mdev);
  212. break;
  213. case NL80211_IFTYPE_STATION:
  214. case NL80211_IFTYPE_ADHOC:
  215. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  216. sdata->u.mgd.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
  217. else
  218. sdata->u.ibss.flags &= ~IEEE80211_IBSS_PREV_BSSID_SET;
  219. /* fall through */
  220. default:
  221. conf.vif = &sdata->vif;
  222. conf.type = sdata->vif.type;
  223. conf.mac_addr = dev->dev_addr;
  224. res = local->ops->add_interface(local_to_hw(local), &conf);
  225. if (res)
  226. goto err_stop;
  227. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  228. local->fif_other_bss++;
  229. netif_addr_lock_bh(local->mdev);
  230. ieee80211_configure_filter(local);
  231. netif_addr_unlock_bh(local->mdev);
  232. ieee80211_start_mesh(sdata);
  233. }
  234. changed |= ieee80211_reset_erp_info(sdata);
  235. ieee80211_bss_info_change_notify(sdata, changed);
  236. ieee80211_enable_keys(sdata);
  237. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  238. netif_carrier_off(dev);
  239. else
  240. netif_carrier_on(dev);
  241. }
  242. if (sdata->vif.type == NL80211_IFTYPE_WDS) {
  243. /* Create STA entry for the WDS peer */
  244. sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
  245. GFP_KERNEL);
  246. if (!sta) {
  247. res = -ENOMEM;
  248. goto err_del_interface;
  249. }
  250. /* no locking required since STA is not live yet */
  251. sta->flags |= WLAN_STA_AUTHORIZED;
  252. res = sta_info_insert(sta);
  253. if (res) {
  254. /* STA has been freed */
  255. goto err_del_interface;
  256. }
  257. }
  258. if (local->open_count == 0) {
  259. res = dev_open(local->mdev);
  260. WARN_ON(res);
  261. if (res)
  262. goto err_del_interface;
  263. tasklet_enable(&local->tx_pending_tasklet);
  264. tasklet_enable(&local->tasklet);
  265. }
  266. /*
  267. * set_multicast_list will be invoked by the networking core
  268. * which will check whether any increments here were done in
  269. * error and sync them down to the hardware as filter flags.
  270. */
  271. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  272. atomic_inc(&local->iff_allmultis);
  273. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  274. atomic_inc(&local->iff_promiscs);
  275. local->open_count++;
  276. if (hw_reconf_flags) {
  277. ieee80211_hw_config(local, hw_reconf_flags);
  278. /*
  279. * set default queue parameters so drivers don't
  280. * need to initialise the hardware if the hardware
  281. * doesn't start up with sane defaults
  282. */
  283. ieee80211_set_wmm_default(sdata);
  284. }
  285. ieee80211_recalc_ps(local);
  286. /*
  287. * ieee80211_sta_work is disabled while network interface
  288. * is down. Therefore, some configuration changes may not
  289. * yet be effective. Trigger execution of ieee80211_sta_work
  290. * to fix this.
  291. */
  292. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  293. queue_work(local->hw.workqueue, &sdata->u.mgd.work);
  294. else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  295. queue_work(local->hw.workqueue, &sdata->u.ibss.work);
  296. netif_tx_start_all_queues(dev);
  297. return 0;
  298. err_del_interface:
  299. local->ops->remove_interface(local_to_hw(local), &conf);
  300. err_stop:
  301. if (!local->open_count && local->ops->stop)
  302. local->ops->stop(local_to_hw(local));
  303. err_del_bss:
  304. sdata->bss = NULL;
  305. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  306. list_del(&sdata->u.vlan.list);
  307. return res;
  308. }
  309. static int ieee80211_stop(struct net_device *dev)
  310. {
  311. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  312. struct ieee80211_local *local = sdata->local;
  313. struct ieee80211_if_init_conf conf;
  314. struct sta_info *sta;
  315. u32 hw_reconf_flags = 0;
  316. /*
  317. * Stop TX on this interface first.
  318. */
  319. netif_tx_stop_all_queues(dev);
  320. /*
  321. * Now delete all active aggregation sessions.
  322. */
  323. rcu_read_lock();
  324. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  325. if (sta->sdata == sdata)
  326. ieee80211_sta_tear_down_BA_sessions(sta);
  327. }
  328. rcu_read_unlock();
  329. /*
  330. * Announce that we are leaving the network, in case we are a
  331. * station interface type. This must be done before removing
  332. * all stations associated with sta_info_flush, otherwise STA
  333. * information will be gone and no announce being done.
  334. */
  335. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  336. if (sdata->u.mgd.state != IEEE80211_STA_MLME_DISABLED)
  337. ieee80211_sta_deauthenticate(sdata,
  338. WLAN_REASON_DEAUTH_LEAVING);
  339. }
  340. /*
  341. * Remove all stations associated with this interface.
  342. *
  343. * This must be done before calling ops->remove_interface()
  344. * because otherwise we can later invoke ops->sta_notify()
  345. * whenever the STAs are removed, and that invalidates driver
  346. * assumptions about always getting a vif pointer that is valid
  347. * (because if we remove a STA after ops->remove_interface()
  348. * the driver will have removed the vif info already!)
  349. *
  350. * We could relax this and only unlink the stations from the
  351. * hash table and list but keep them on a per-sdata list that
  352. * will be inserted back again when the interface is brought
  353. * up again, but I don't currently see a use case for that,
  354. * except with WDS which gets a STA entry created when it is
  355. * brought up.
  356. */
  357. sta_info_flush(local, sdata);
  358. /*
  359. * Don't count this interface for promisc/allmulti while it
  360. * is down. dev_mc_unsync() will invoke set_multicast_list
  361. * on the master interface which will sync these down to the
  362. * hardware as filter flags.
  363. */
  364. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  365. atomic_dec(&local->iff_allmultis);
  366. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  367. atomic_dec(&local->iff_promiscs);
  368. dev_mc_unsync(local->mdev, dev);
  369. del_timer_sync(&local->dynamic_ps_timer);
  370. cancel_work_sync(&local->dynamic_ps_enable_work);
  371. /* APs need special treatment */
  372. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  373. struct ieee80211_sub_if_data *vlan, *tmp;
  374. struct beacon_data *old_beacon = sdata->u.ap.beacon;
  375. /* remove beacon */
  376. rcu_assign_pointer(sdata->u.ap.beacon, NULL);
  377. synchronize_rcu();
  378. kfree(old_beacon);
  379. /* down all dependent devices, that is VLANs */
  380. list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
  381. u.vlan.list)
  382. dev_close(vlan->dev);
  383. WARN_ON(!list_empty(&sdata->u.ap.vlans));
  384. }
  385. local->open_count--;
  386. switch (sdata->vif.type) {
  387. case NL80211_IFTYPE_AP_VLAN:
  388. list_del(&sdata->u.vlan.list);
  389. /* no need to tell driver */
  390. break;
  391. case NL80211_IFTYPE_MONITOR:
  392. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  393. local->cooked_mntrs--;
  394. break;
  395. }
  396. local->monitors--;
  397. if (local->monitors == 0) {
  398. local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
  399. hw_reconf_flags |= IEEE80211_CONF_CHANGE_RADIOTAP;
  400. }
  401. if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
  402. local->fif_fcsfail--;
  403. if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
  404. local->fif_plcpfail--;
  405. if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
  406. local->fif_control--;
  407. if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
  408. local->fif_other_bss--;
  409. netif_addr_lock_bh(local->mdev);
  410. ieee80211_configure_filter(local);
  411. netif_addr_unlock_bh(local->mdev);
  412. break;
  413. case NL80211_IFTYPE_STATION:
  414. memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
  415. del_timer_sync(&sdata->u.mgd.chswitch_timer);
  416. del_timer_sync(&sdata->u.mgd.timer);
  417. /*
  418. * If the timer fired while we waited for it, it will have
  419. * requeued the work. Now the work will be running again
  420. * but will not rearm the timer again because it checks
  421. * whether the interface is running, which, at this point,
  422. * it no longer is.
  423. */
  424. cancel_work_sync(&sdata->u.mgd.work);
  425. cancel_work_sync(&sdata->u.mgd.chswitch_work);
  426. cancel_work_sync(&sdata->u.mgd.beacon_loss_work);
  427. /*
  428. * When we get here, the interface is marked down.
  429. * Call synchronize_rcu() to wait for the RX path
  430. * should it be using the interface and enqueuing
  431. * frames at this very time on another CPU.
  432. */
  433. synchronize_rcu();
  434. skb_queue_purge(&sdata->u.mgd.skb_queue);
  435. sdata->u.mgd.flags &= ~(IEEE80211_STA_PRIVACY_INVOKED |
  436. IEEE80211_STA_TKIP_WEP_USED);
  437. kfree(sdata->u.mgd.extra_ie);
  438. sdata->u.mgd.extra_ie = NULL;
  439. sdata->u.mgd.extra_ie_len = 0;
  440. /* fall through */
  441. case NL80211_IFTYPE_ADHOC:
  442. if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  443. memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
  444. del_timer_sync(&sdata->u.ibss.timer);
  445. cancel_work_sync(&sdata->u.ibss.work);
  446. synchronize_rcu();
  447. skb_queue_purge(&sdata->u.ibss.skb_queue);
  448. }
  449. /* fall through */
  450. case NL80211_IFTYPE_MESH_POINT:
  451. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  452. /* other_bss and allmulti are always set on mesh
  453. * ifaces */
  454. local->fif_other_bss--;
  455. atomic_dec(&local->iff_allmultis);
  456. netif_addr_lock_bh(local->mdev);
  457. ieee80211_configure_filter(local);
  458. netif_addr_unlock_bh(local->mdev);
  459. ieee80211_stop_mesh(sdata);
  460. }
  461. /* fall through */
  462. default:
  463. if (local->scan_sdata == sdata) {
  464. if (!local->ops->hw_scan)
  465. cancel_delayed_work_sync(&local->scan_work);
  466. /*
  467. * The software scan can no longer run now, so we can
  468. * clear out the scan_sdata reference. However, the
  469. * hardware scan may still be running. The complete
  470. * function must be prepared to handle a NULL value.
  471. */
  472. local->scan_sdata = NULL;
  473. /*
  474. * The memory barrier guarantees that another CPU
  475. * that is hardware-scanning will now see the fact
  476. * that this interface is gone.
  477. */
  478. smp_mb();
  479. /*
  480. * If software scanning, complete the scan but since
  481. * the scan_sdata is NULL already don't send out a
  482. * scan event to userspace -- the scan is incomplete.
  483. */
  484. if (local->sw_scanning)
  485. ieee80211_scan_completed(&local->hw, true);
  486. }
  487. conf.vif = &sdata->vif;
  488. conf.type = sdata->vif.type;
  489. conf.mac_addr = dev->dev_addr;
  490. /* disable all keys for as long as this netdev is down */
  491. ieee80211_disable_keys(sdata);
  492. local->ops->remove_interface(local_to_hw(local), &conf);
  493. }
  494. sdata->bss = NULL;
  495. if (local->open_count == 0) {
  496. if (netif_running(local->mdev))
  497. dev_close(local->mdev);
  498. if (local->ops->stop)
  499. local->ops->stop(local_to_hw(local));
  500. ieee80211_led_radio(local, 0);
  501. flush_workqueue(local->hw.workqueue);
  502. tasklet_disable(&local->tx_pending_tasklet);
  503. tasklet_disable(&local->tasklet);
  504. /* no reconfiguring after stop! */
  505. hw_reconf_flags = 0;
  506. }
  507. ieee80211_recalc_ps(local);
  508. /* do after stop to avoid reconfiguring when we stop anyway */
  509. if (hw_reconf_flags)
  510. ieee80211_hw_config(local, hw_reconf_flags);
  511. return 0;
  512. }
  513. static void ieee80211_set_multicast_list(struct net_device *dev)
  514. {
  515. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  516. struct ieee80211_local *local = sdata->local;
  517. int allmulti, promisc, sdata_allmulti, sdata_promisc;
  518. allmulti = !!(dev->flags & IFF_ALLMULTI);
  519. promisc = !!(dev->flags & IFF_PROMISC);
  520. sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
  521. sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
  522. if (allmulti != sdata_allmulti) {
  523. if (dev->flags & IFF_ALLMULTI)
  524. atomic_inc(&local->iff_allmultis);
  525. else
  526. atomic_dec(&local->iff_allmultis);
  527. sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
  528. }
  529. if (promisc != sdata_promisc) {
  530. if (dev->flags & IFF_PROMISC)
  531. atomic_inc(&local->iff_promiscs);
  532. else
  533. atomic_dec(&local->iff_promiscs);
  534. sdata->flags ^= IEEE80211_SDATA_PROMISC;
  535. }
  536. dev_mc_sync(local->mdev, dev);
  537. }
  538. /*
  539. * Called when the netdev is removed or, by the code below, before
  540. * the interface type changes.
  541. */
  542. static void ieee80211_teardown_sdata(struct net_device *dev)
  543. {
  544. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  545. struct ieee80211_local *local = sdata->local;
  546. struct beacon_data *beacon;
  547. struct sk_buff *skb;
  548. int flushed;
  549. int i;
  550. /* free extra data */
  551. ieee80211_free_keys(sdata);
  552. ieee80211_debugfs_remove_netdev(sdata);
  553. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  554. __skb_queue_purge(&sdata->fragments[i].skb_list);
  555. sdata->fragment_next = 0;
  556. switch (sdata->vif.type) {
  557. case NL80211_IFTYPE_AP:
  558. beacon = sdata->u.ap.beacon;
  559. rcu_assign_pointer(sdata->u.ap.beacon, NULL);
  560. synchronize_rcu();
  561. kfree(beacon);
  562. while ((skb = skb_dequeue(&sdata->u.ap.ps_bc_buf))) {
  563. local->total_ps_buffered--;
  564. dev_kfree_skb(skb);
  565. }
  566. break;
  567. case NL80211_IFTYPE_MESH_POINT:
  568. if (ieee80211_vif_is_mesh(&sdata->vif))
  569. mesh_rmc_free(sdata);
  570. break;
  571. case NL80211_IFTYPE_ADHOC:
  572. kfree_skb(sdata->u.ibss.probe_resp);
  573. break;
  574. case NL80211_IFTYPE_STATION:
  575. kfree(sdata->u.mgd.extra_ie);
  576. kfree(sdata->u.mgd.assocreq_ies);
  577. kfree(sdata->u.mgd.assocresp_ies);
  578. kfree(sdata->u.mgd.sme_auth_ie);
  579. break;
  580. case NL80211_IFTYPE_WDS:
  581. case NL80211_IFTYPE_AP_VLAN:
  582. case NL80211_IFTYPE_MONITOR:
  583. break;
  584. case NL80211_IFTYPE_UNSPECIFIED:
  585. case __NL80211_IFTYPE_AFTER_LAST:
  586. BUG();
  587. break;
  588. }
  589. flushed = sta_info_flush(local, sdata);
  590. WARN_ON(flushed);
  591. }
  592. static const struct net_device_ops ieee80211_dataif_ops = {
  593. .ndo_open = ieee80211_open,
  594. .ndo_stop = ieee80211_stop,
  595. .ndo_uninit = ieee80211_teardown_sdata,
  596. .ndo_start_xmit = ieee80211_subif_start_xmit,
  597. .ndo_set_multicast_list = ieee80211_set_multicast_list,
  598. .ndo_change_mtu = ieee80211_change_mtu,
  599. .ndo_set_mac_address = eth_mac_addr,
  600. };
  601. static const struct net_device_ops ieee80211_monitorif_ops = {
  602. .ndo_open = ieee80211_open,
  603. .ndo_stop = ieee80211_stop,
  604. .ndo_uninit = ieee80211_teardown_sdata,
  605. .ndo_start_xmit = ieee80211_monitor_start_xmit,
  606. .ndo_set_multicast_list = ieee80211_set_multicast_list,
  607. .ndo_change_mtu = ieee80211_change_mtu,
  608. .ndo_set_mac_address = eth_mac_addr,
  609. };
  610. static void ieee80211_if_setup(struct net_device *dev)
  611. {
  612. ether_setup(dev);
  613. dev->netdev_ops = &ieee80211_dataif_ops;
  614. dev->wireless_handlers = &ieee80211_iw_handler_def;
  615. dev->destructor = free_netdev;
  616. }
  617. /*
  618. * Helper function to initialise an interface to a specific type.
  619. */
  620. static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
  621. enum nl80211_iftype type)
  622. {
  623. /* clear type-dependent union */
  624. memset(&sdata->u, 0, sizeof(sdata->u));
  625. /* and set some type-dependent values */
  626. sdata->vif.type = type;
  627. sdata->dev->netdev_ops = &ieee80211_dataif_ops;
  628. sdata->wdev.iftype = type;
  629. /* only monitor differs */
  630. sdata->dev->type = ARPHRD_ETHER;
  631. switch (type) {
  632. case NL80211_IFTYPE_AP:
  633. skb_queue_head_init(&sdata->u.ap.ps_bc_buf);
  634. INIT_LIST_HEAD(&sdata->u.ap.vlans);
  635. break;
  636. case NL80211_IFTYPE_STATION:
  637. ieee80211_sta_setup_sdata(sdata);
  638. break;
  639. case NL80211_IFTYPE_ADHOC:
  640. ieee80211_ibss_setup_sdata(sdata);
  641. break;
  642. case NL80211_IFTYPE_MESH_POINT:
  643. if (ieee80211_vif_is_mesh(&sdata->vif))
  644. ieee80211_mesh_init_sdata(sdata);
  645. break;
  646. case NL80211_IFTYPE_MONITOR:
  647. sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
  648. sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
  649. sdata->u.mntr_flags = MONITOR_FLAG_CONTROL |
  650. MONITOR_FLAG_OTHER_BSS;
  651. break;
  652. case NL80211_IFTYPE_WDS:
  653. case NL80211_IFTYPE_AP_VLAN:
  654. break;
  655. case NL80211_IFTYPE_UNSPECIFIED:
  656. case __NL80211_IFTYPE_AFTER_LAST:
  657. BUG();
  658. break;
  659. }
  660. ieee80211_debugfs_add_netdev(sdata);
  661. }
  662. int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
  663. enum nl80211_iftype type)
  664. {
  665. ASSERT_RTNL();
  666. if (type == sdata->vif.type)
  667. return 0;
  668. /* Setting ad-hoc mode on non-IBSS channel is not supported. */
  669. if (sdata->local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS &&
  670. type == NL80211_IFTYPE_ADHOC)
  671. return -EOPNOTSUPP;
  672. /*
  673. * We could, here, on changes between IBSS/STA/MESH modes,
  674. * invoke an MLME function instead that disassociates etc.
  675. * and goes into the requested mode.
  676. */
  677. if (netif_running(sdata->dev))
  678. return -EBUSY;
  679. /* Purge and reset type-dependent state. */
  680. ieee80211_teardown_sdata(sdata->dev);
  681. ieee80211_setup_sdata(sdata, type);
  682. /* reset some values that shouldn't be kept across type changes */
  683. sdata->vif.bss_conf.basic_rates =
  684. ieee80211_mandatory_rates(sdata->local,
  685. sdata->local->hw.conf.channel->band);
  686. sdata->drop_unencrypted = 0;
  687. return 0;
  688. }
  689. int ieee80211_if_add(struct ieee80211_local *local, const char *name,
  690. struct net_device **new_dev, enum nl80211_iftype type,
  691. struct vif_params *params)
  692. {
  693. struct net_device *ndev;
  694. struct ieee80211_sub_if_data *sdata = NULL;
  695. int ret, i;
  696. ASSERT_RTNL();
  697. ndev = alloc_netdev(sizeof(*sdata) + local->hw.vif_data_size,
  698. name, ieee80211_if_setup);
  699. if (!ndev)
  700. return -ENOMEM;
  701. ndev->needed_headroom = local->tx_headroom +
  702. 4*6 /* four MAC addresses */
  703. + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
  704. + 6 /* mesh */
  705. + 8 /* rfc1042/bridge tunnel */
  706. - ETH_HLEN /* ethernet hard_header_len */
  707. + IEEE80211_ENCRYPT_HEADROOM;
  708. ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
  709. ret = dev_alloc_name(ndev, ndev->name);
  710. if (ret < 0)
  711. goto fail;
  712. memcpy(ndev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  713. SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
  714. ndev->features |= NETIF_F_NETNS_LOCAL;
  715. /* don't use IEEE80211_DEV_TO_SUB_IF because it checks too much */
  716. sdata = netdev_priv(ndev);
  717. ndev->ieee80211_ptr = &sdata->wdev;
  718. /* initialise type-independent data */
  719. sdata->wdev.wiphy = local->hw.wiphy;
  720. sdata->local = local;
  721. sdata->dev = ndev;
  722. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  723. skb_queue_head_init(&sdata->fragments[i].skb_list);
  724. INIT_LIST_HEAD(&sdata->key_list);
  725. sdata->force_unicast_rateidx = -1;
  726. sdata->max_ratectrl_rateidx = -1;
  727. /* setup type-dependent data */
  728. ieee80211_setup_sdata(sdata, type);
  729. ret = register_netdevice(ndev);
  730. if (ret)
  731. goto fail;
  732. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  733. params && params->mesh_id_len)
  734. ieee80211_sdata_set_mesh_id(sdata,
  735. params->mesh_id_len,
  736. params->mesh_id);
  737. mutex_lock(&local->iflist_mtx);
  738. list_add_tail_rcu(&sdata->list, &local->interfaces);
  739. mutex_unlock(&local->iflist_mtx);
  740. if (new_dev)
  741. *new_dev = ndev;
  742. return 0;
  743. fail:
  744. free_netdev(ndev);
  745. return ret;
  746. }
  747. void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
  748. {
  749. ASSERT_RTNL();
  750. mutex_lock(&sdata->local->iflist_mtx);
  751. list_del_rcu(&sdata->list);
  752. mutex_unlock(&sdata->local->iflist_mtx);
  753. synchronize_rcu();
  754. unregister_netdevice(sdata->dev);
  755. }
  756. /*
  757. * Remove all interfaces, may only be called at hardware unregistration
  758. * time because it doesn't do RCU-safe list removals.
  759. */
  760. void ieee80211_remove_interfaces(struct ieee80211_local *local)
  761. {
  762. struct ieee80211_sub_if_data *sdata, *tmp;
  763. ASSERT_RTNL();
  764. list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
  765. /*
  766. * we cannot hold the iflist_mtx across unregister_netdevice,
  767. * but we only need to hold it for list modifications to lock
  768. * out readers since we're under the RTNL here as all other
  769. * writers.
  770. */
  771. mutex_lock(&local->iflist_mtx);
  772. list_del(&sdata->list);
  773. mutex_unlock(&local->iflist_mtx);
  774. unregister_netdevice(sdata->dev);
  775. }
  776. }