iface.c 25 KB

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