iface.c 26 KB

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