iface.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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. /*
  175. * Validate the MAC address for this device.
  176. */
  177. if (!is_valid_ether_addr(dev->dev_addr)) {
  178. if (!local->open_count)
  179. drv_stop(local);
  180. return -EADDRNOTAVAIL;
  181. }
  182. switch (sdata->vif.type) {
  183. case NL80211_IFTYPE_AP_VLAN:
  184. /* no need to tell driver */
  185. break;
  186. case NL80211_IFTYPE_MONITOR:
  187. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  188. local->cooked_mntrs++;
  189. break;
  190. }
  191. /* must be before the call to ieee80211_configure_filter */
  192. local->monitors++;
  193. if (local->monitors == 1) {
  194. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  195. hw_reconf_flags |= IEEE80211_CONF_CHANGE_RADIOTAP;
  196. }
  197. if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
  198. local->fif_fcsfail++;
  199. if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
  200. local->fif_plcpfail++;
  201. if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL) {
  202. local->fif_control++;
  203. local->fif_pspoll++;
  204. }
  205. if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
  206. local->fif_other_bss++;
  207. ieee80211_configure_filter(local);
  208. break;
  209. default:
  210. conf.vif = &sdata->vif;
  211. conf.type = sdata->vif.type;
  212. conf.mac_addr = dev->dev_addr;
  213. res = drv_add_interface(local, &conf);
  214. if (res)
  215. goto err_stop;
  216. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  217. local->fif_other_bss++;
  218. ieee80211_configure_filter(local);
  219. ieee80211_start_mesh(sdata);
  220. } else if (sdata->vif.type == NL80211_IFTYPE_AP) {
  221. local->fif_pspoll++;
  222. ieee80211_configure_filter(local);
  223. }
  224. changed |= ieee80211_reset_erp_info(sdata);
  225. ieee80211_bss_info_change_notify(sdata, changed);
  226. ieee80211_enable_keys(sdata);
  227. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  228. netif_carrier_off(dev);
  229. else
  230. netif_carrier_on(dev);
  231. }
  232. if (sdata->vif.type == NL80211_IFTYPE_WDS) {
  233. /* Create STA entry for the WDS peer */
  234. sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
  235. GFP_KERNEL);
  236. if (!sta) {
  237. res = -ENOMEM;
  238. goto err_del_interface;
  239. }
  240. /* no locking required since STA is not live yet */
  241. sta->flags |= WLAN_STA_AUTHORIZED;
  242. res = sta_info_insert(sta);
  243. if (res) {
  244. /* STA has been freed */
  245. goto err_del_interface;
  246. }
  247. }
  248. /*
  249. * set_multicast_list will be invoked by the networking core
  250. * which will check whether any increments here were done in
  251. * error and sync them down to the hardware as filter flags.
  252. */
  253. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  254. atomic_inc(&local->iff_allmultis);
  255. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  256. atomic_inc(&local->iff_promiscs);
  257. hw_reconf_flags |= __ieee80211_recalc_idle(local);
  258. local->open_count++;
  259. if (hw_reconf_flags) {
  260. ieee80211_hw_config(local, hw_reconf_flags);
  261. /*
  262. * set default queue parameters so drivers don't
  263. * need to initialise the hardware if the hardware
  264. * doesn't start up with sane defaults
  265. */
  266. ieee80211_set_wmm_default(sdata);
  267. }
  268. ieee80211_recalc_ps(local, -1);
  269. /*
  270. * ieee80211_sta_work is disabled while network interface
  271. * is down. Therefore, some configuration changes may not
  272. * yet be effective. Trigger execution of ieee80211_sta_work
  273. * to fix this.
  274. */
  275. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  276. ieee80211_queue_work(&local->hw, &sdata->u.mgd.work);
  277. netif_tx_start_all_queues(dev);
  278. return 0;
  279. err_del_interface:
  280. drv_remove_interface(local, &conf);
  281. err_stop:
  282. if (!local->open_count)
  283. drv_stop(local);
  284. err_del_bss:
  285. sdata->bss = NULL;
  286. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  287. list_del(&sdata->u.vlan.list);
  288. return res;
  289. }
  290. static int ieee80211_stop(struct net_device *dev)
  291. {
  292. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  293. struct ieee80211_local *local = sdata->local;
  294. struct ieee80211_if_init_conf conf;
  295. struct sta_info *sta;
  296. unsigned long flags;
  297. struct sk_buff *skb, *tmp;
  298. u32 hw_reconf_flags = 0;
  299. int i;
  300. /*
  301. * Stop TX on this interface first.
  302. */
  303. netif_tx_stop_all_queues(dev);
  304. /*
  305. * Now delete all active aggregation sessions.
  306. */
  307. rcu_read_lock();
  308. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  309. if (sta->sdata == sdata)
  310. ieee80211_sta_tear_down_BA_sessions(sta);
  311. }
  312. rcu_read_unlock();
  313. /*
  314. * Remove all stations associated with this interface.
  315. *
  316. * This must be done before calling ops->remove_interface()
  317. * because otherwise we can later invoke ops->sta_notify()
  318. * whenever the STAs are removed, and that invalidates driver
  319. * assumptions about always getting a vif pointer that is valid
  320. * (because if we remove a STA after ops->remove_interface()
  321. * the driver will have removed the vif info already!)
  322. *
  323. * We could relax this and only unlink the stations from the
  324. * hash table and list but keep them on a per-sdata list that
  325. * will be inserted back again when the interface is brought
  326. * up again, but I don't currently see a use case for that,
  327. * except with WDS which gets a STA entry created when it is
  328. * brought up.
  329. */
  330. sta_info_flush(local, sdata);
  331. /*
  332. * Don't count this interface for promisc/allmulti while it
  333. * is down. dev_mc_unsync() will invoke set_multicast_list
  334. * on the master interface which will sync these down to the
  335. * hardware as filter flags.
  336. */
  337. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  338. atomic_dec(&local->iff_allmultis);
  339. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  340. atomic_dec(&local->iff_promiscs);
  341. if (sdata->vif.type == NL80211_IFTYPE_AP)
  342. local->fif_pspoll--;
  343. netif_addr_lock_bh(dev);
  344. spin_lock_bh(&local->filter_lock);
  345. __dev_addr_unsync(&local->mc_list, &local->mc_count,
  346. &dev->mc_list, &dev->mc_count);
  347. spin_unlock_bh(&local->filter_lock);
  348. netif_addr_unlock_bh(dev);
  349. ieee80211_configure_filter(local);
  350. del_timer_sync(&local->dynamic_ps_timer);
  351. cancel_work_sync(&local->dynamic_ps_enable_work);
  352. /* APs need special treatment */
  353. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  354. struct ieee80211_sub_if_data *vlan, *tmpsdata;
  355. struct beacon_data *old_beacon = sdata->u.ap.beacon;
  356. /* remove beacon */
  357. rcu_assign_pointer(sdata->u.ap.beacon, NULL);
  358. synchronize_rcu();
  359. kfree(old_beacon);
  360. /* down all dependent devices, that is VLANs */
  361. list_for_each_entry_safe(vlan, tmpsdata, &sdata->u.ap.vlans,
  362. u.vlan.list)
  363. dev_close(vlan->dev);
  364. WARN_ON(!list_empty(&sdata->u.ap.vlans));
  365. }
  366. local->open_count--;
  367. switch (sdata->vif.type) {
  368. case NL80211_IFTYPE_AP_VLAN:
  369. list_del(&sdata->u.vlan.list);
  370. /* no need to tell driver */
  371. break;
  372. case NL80211_IFTYPE_MONITOR:
  373. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  374. local->cooked_mntrs--;
  375. break;
  376. }
  377. local->monitors--;
  378. if (local->monitors == 0) {
  379. local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
  380. hw_reconf_flags |= IEEE80211_CONF_CHANGE_RADIOTAP;
  381. }
  382. if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
  383. local->fif_fcsfail--;
  384. if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
  385. local->fif_plcpfail--;
  386. if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL) {
  387. local->fif_pspoll--;
  388. local->fif_control--;
  389. }
  390. if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
  391. local->fif_other_bss--;
  392. ieee80211_configure_filter(local);
  393. break;
  394. case NL80211_IFTYPE_STATION:
  395. del_timer_sync(&sdata->u.mgd.chswitch_timer);
  396. del_timer_sync(&sdata->u.mgd.timer);
  397. del_timer_sync(&sdata->u.mgd.conn_mon_timer);
  398. del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
  399. /*
  400. * If any of the timers fired while we waited for it, it will
  401. * have queued its work. Now the work will be running again
  402. * but will not rearm the timer again because it checks
  403. * whether the interface is running, which, at this point,
  404. * it no longer is.
  405. */
  406. cancel_work_sync(&sdata->u.mgd.work);
  407. cancel_work_sync(&sdata->u.mgd.chswitch_work);
  408. cancel_work_sync(&sdata->u.mgd.monitor_work);
  409. cancel_work_sync(&sdata->u.mgd.beacon_loss_work);
  410. /*
  411. * When we get here, the interface is marked down.
  412. * Call synchronize_rcu() to wait for the RX path
  413. * should it be using the interface and enqueuing
  414. * frames at this very time on another CPU.
  415. */
  416. synchronize_rcu();
  417. skb_queue_purge(&sdata->u.mgd.skb_queue);
  418. /* fall through */
  419. case NL80211_IFTYPE_ADHOC:
  420. if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  421. del_timer_sync(&sdata->u.ibss.timer);
  422. cancel_work_sync(&sdata->u.ibss.work);
  423. synchronize_rcu();
  424. skb_queue_purge(&sdata->u.ibss.skb_queue);
  425. }
  426. /* fall through */
  427. case NL80211_IFTYPE_MESH_POINT:
  428. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  429. /* other_bss and allmulti are always set on mesh
  430. * ifaces */
  431. local->fif_other_bss--;
  432. atomic_dec(&local->iff_allmultis);
  433. ieee80211_configure_filter(local);
  434. ieee80211_stop_mesh(sdata);
  435. }
  436. /* fall through */
  437. default:
  438. if (local->scan_sdata == sdata) {
  439. if (!local->ops->hw_scan)
  440. cancel_delayed_work_sync(&local->scan_work);
  441. /*
  442. * The software scan can no longer run now, so we can
  443. * clear out the scan_sdata reference. However, the
  444. * hardware scan may still be running. The complete
  445. * function must be prepared to handle a NULL value.
  446. */
  447. local->scan_sdata = NULL;
  448. /*
  449. * The memory barrier guarantees that another CPU
  450. * that is hardware-scanning will now see the fact
  451. * that this interface is gone.
  452. */
  453. smp_mb();
  454. /*
  455. * If software scanning, complete the scan but since
  456. * the scan_sdata is NULL already don't send out a
  457. * scan event to userspace -- the scan is incomplete.
  458. */
  459. if (test_bit(SCAN_SW_SCANNING, &local->scanning))
  460. ieee80211_scan_completed(&local->hw, true);
  461. }
  462. /*
  463. * Disable beaconing for AP and mesh, IBSS can't
  464. * still be joined to a network at this point.
  465. */
  466. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  467. sdata->vif.type == NL80211_IFTYPE_MESH_POINT) {
  468. ieee80211_bss_info_change_notify(sdata,
  469. BSS_CHANGED_BEACON_ENABLED);
  470. }
  471. conf.vif = &sdata->vif;
  472. conf.type = sdata->vif.type;
  473. conf.mac_addr = dev->dev_addr;
  474. /* disable all keys for as long as this netdev is down */
  475. ieee80211_disable_keys(sdata);
  476. drv_remove_interface(local, &conf);
  477. }
  478. sdata->bss = NULL;
  479. hw_reconf_flags |= __ieee80211_recalc_idle(local);
  480. ieee80211_recalc_ps(local, -1);
  481. if (local->open_count == 0) {
  482. ieee80211_clear_tx_pending(local);
  483. ieee80211_stop_device(local);
  484. /* no reconfiguring after stop! */
  485. hw_reconf_flags = 0;
  486. }
  487. /* do after stop to avoid reconfiguring when we stop anyway */
  488. if (hw_reconf_flags)
  489. ieee80211_hw_config(local, hw_reconf_flags);
  490. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  491. for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
  492. skb_queue_walk_safe(&local->pending[i], skb, tmp) {
  493. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  494. if (info->control.vif == &sdata->vif) {
  495. __skb_unlink(skb, &local->pending[i]);
  496. dev_kfree_skb_irq(skb);
  497. }
  498. }
  499. }
  500. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  501. return 0;
  502. }
  503. static void ieee80211_set_multicast_list(struct net_device *dev)
  504. {
  505. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  506. struct ieee80211_local *local = sdata->local;
  507. int allmulti, promisc, sdata_allmulti, sdata_promisc;
  508. allmulti = !!(dev->flags & IFF_ALLMULTI);
  509. promisc = !!(dev->flags & IFF_PROMISC);
  510. sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
  511. sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
  512. if (allmulti != sdata_allmulti) {
  513. if (dev->flags & IFF_ALLMULTI)
  514. atomic_inc(&local->iff_allmultis);
  515. else
  516. atomic_dec(&local->iff_allmultis);
  517. sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
  518. }
  519. if (promisc != sdata_promisc) {
  520. if (dev->flags & IFF_PROMISC)
  521. atomic_inc(&local->iff_promiscs);
  522. else
  523. atomic_dec(&local->iff_promiscs);
  524. sdata->flags ^= IEEE80211_SDATA_PROMISC;
  525. }
  526. spin_lock_bh(&local->filter_lock);
  527. __dev_addr_sync(&local->mc_list, &local->mc_count,
  528. &dev->mc_list, &dev->mc_count);
  529. spin_unlock_bh(&local->filter_lock);
  530. ieee80211_queue_work(&local->hw, &local->reconfig_filter);
  531. }
  532. /*
  533. * Called when the netdev is removed or, by the code below, before
  534. * the interface type changes.
  535. */
  536. static void ieee80211_teardown_sdata(struct net_device *dev)
  537. {
  538. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  539. struct ieee80211_local *local = sdata->local;
  540. struct beacon_data *beacon;
  541. struct sk_buff *skb;
  542. int flushed;
  543. int i;
  544. /* free extra data */
  545. ieee80211_free_keys(sdata);
  546. ieee80211_debugfs_remove_netdev(sdata);
  547. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  548. __skb_queue_purge(&sdata->fragments[i].skb_list);
  549. sdata->fragment_next = 0;
  550. switch (sdata->vif.type) {
  551. case NL80211_IFTYPE_AP:
  552. beacon = sdata->u.ap.beacon;
  553. rcu_assign_pointer(sdata->u.ap.beacon, NULL);
  554. synchronize_rcu();
  555. kfree(beacon);
  556. while ((skb = skb_dequeue(&sdata->u.ap.ps_bc_buf))) {
  557. local->total_ps_buffered--;
  558. dev_kfree_skb(skb);
  559. }
  560. break;
  561. case NL80211_IFTYPE_MESH_POINT:
  562. if (ieee80211_vif_is_mesh(&sdata->vif))
  563. mesh_rmc_free(sdata);
  564. break;
  565. case NL80211_IFTYPE_ADHOC:
  566. if (WARN_ON(sdata->u.ibss.presp))
  567. kfree_skb(sdata->u.ibss.presp);
  568. break;
  569. case NL80211_IFTYPE_STATION:
  570. case NL80211_IFTYPE_WDS:
  571. case NL80211_IFTYPE_AP_VLAN:
  572. case NL80211_IFTYPE_MONITOR:
  573. break;
  574. case NL80211_IFTYPE_UNSPECIFIED:
  575. case __NL80211_IFTYPE_AFTER_LAST:
  576. BUG();
  577. break;
  578. }
  579. flushed = sta_info_flush(local, sdata);
  580. WARN_ON(flushed);
  581. }
  582. static const struct net_device_ops ieee80211_dataif_ops = {
  583. .ndo_open = ieee80211_open,
  584. .ndo_stop = ieee80211_stop,
  585. .ndo_uninit = ieee80211_teardown_sdata,
  586. .ndo_start_xmit = ieee80211_subif_start_xmit,
  587. .ndo_set_multicast_list = ieee80211_set_multicast_list,
  588. .ndo_change_mtu = ieee80211_change_mtu,
  589. .ndo_set_mac_address = eth_mac_addr,
  590. };
  591. static const struct net_device_ops ieee80211_monitorif_ops = {
  592. .ndo_open = ieee80211_open,
  593. .ndo_stop = ieee80211_stop,
  594. .ndo_uninit = ieee80211_teardown_sdata,
  595. .ndo_start_xmit = ieee80211_monitor_start_xmit,
  596. .ndo_set_multicast_list = ieee80211_set_multicast_list,
  597. .ndo_change_mtu = ieee80211_change_mtu,
  598. .ndo_set_mac_address = eth_mac_addr,
  599. };
  600. static void ieee80211_if_setup(struct net_device *dev)
  601. {
  602. ether_setup(dev);
  603. dev->netdev_ops = &ieee80211_dataif_ops;
  604. dev->destructor = free_netdev;
  605. }
  606. /*
  607. * Helper function to initialise an interface to a specific type.
  608. */
  609. static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
  610. enum nl80211_iftype type)
  611. {
  612. /* clear type-dependent union */
  613. memset(&sdata->u, 0, sizeof(sdata->u));
  614. /* and set some type-dependent values */
  615. sdata->vif.type = type;
  616. sdata->dev->netdev_ops = &ieee80211_dataif_ops;
  617. sdata->wdev.iftype = type;
  618. /* only monitor differs */
  619. sdata->dev->type = ARPHRD_ETHER;
  620. switch (type) {
  621. case NL80211_IFTYPE_AP:
  622. skb_queue_head_init(&sdata->u.ap.ps_bc_buf);
  623. INIT_LIST_HEAD(&sdata->u.ap.vlans);
  624. break;
  625. case NL80211_IFTYPE_STATION:
  626. ieee80211_sta_setup_sdata(sdata);
  627. break;
  628. case NL80211_IFTYPE_ADHOC:
  629. ieee80211_ibss_setup_sdata(sdata);
  630. break;
  631. case NL80211_IFTYPE_MESH_POINT:
  632. if (ieee80211_vif_is_mesh(&sdata->vif))
  633. ieee80211_mesh_init_sdata(sdata);
  634. break;
  635. case NL80211_IFTYPE_MONITOR:
  636. sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
  637. sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
  638. sdata->u.mntr_flags = MONITOR_FLAG_CONTROL |
  639. MONITOR_FLAG_OTHER_BSS;
  640. break;
  641. case NL80211_IFTYPE_WDS:
  642. case NL80211_IFTYPE_AP_VLAN:
  643. break;
  644. case NL80211_IFTYPE_UNSPECIFIED:
  645. case __NL80211_IFTYPE_AFTER_LAST:
  646. BUG();
  647. break;
  648. }
  649. ieee80211_debugfs_add_netdev(sdata);
  650. }
  651. int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
  652. enum nl80211_iftype type)
  653. {
  654. ASSERT_RTNL();
  655. if (type == sdata->vif.type)
  656. return 0;
  657. /* Setting ad-hoc mode on non-IBSS channel is not supported. */
  658. if (sdata->local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS &&
  659. type == NL80211_IFTYPE_ADHOC)
  660. return -EOPNOTSUPP;
  661. /*
  662. * We could, here, on changes between IBSS/STA/MESH modes,
  663. * invoke an MLME function instead that disassociates etc.
  664. * and goes into the requested mode.
  665. */
  666. if (netif_running(sdata->dev))
  667. return -EBUSY;
  668. /* Purge and reset type-dependent state. */
  669. ieee80211_teardown_sdata(sdata->dev);
  670. ieee80211_setup_sdata(sdata, type);
  671. /* reset some values that shouldn't be kept across type changes */
  672. sdata->vif.bss_conf.basic_rates =
  673. ieee80211_mandatory_rates(sdata->local,
  674. sdata->local->hw.conf.channel->band);
  675. sdata->drop_unencrypted = 0;
  676. return 0;
  677. }
  678. int ieee80211_if_add(struct ieee80211_local *local, const char *name,
  679. struct net_device **new_dev, enum nl80211_iftype type,
  680. struct vif_params *params)
  681. {
  682. struct net_device *ndev;
  683. struct ieee80211_sub_if_data *sdata = NULL;
  684. int ret, i;
  685. ASSERT_RTNL();
  686. ndev = alloc_netdev(sizeof(*sdata) + local->hw.vif_data_size,
  687. name, ieee80211_if_setup);
  688. if (!ndev)
  689. return -ENOMEM;
  690. dev_net_set(ndev, wiphy_net(local->hw.wiphy));
  691. ndev->needed_headroom = local->tx_headroom +
  692. 4*6 /* four MAC addresses */
  693. + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
  694. + 6 /* mesh */
  695. + 8 /* rfc1042/bridge tunnel */
  696. - ETH_HLEN /* ethernet hard_header_len */
  697. + IEEE80211_ENCRYPT_HEADROOM;
  698. ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
  699. ret = dev_alloc_name(ndev, ndev->name);
  700. if (ret < 0)
  701. goto fail;
  702. memcpy(ndev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  703. SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
  704. /* don't use IEEE80211_DEV_TO_SUB_IF because it checks too much */
  705. sdata = netdev_priv(ndev);
  706. ndev->ieee80211_ptr = &sdata->wdev;
  707. /* initialise type-independent data */
  708. sdata->wdev.wiphy = local->hw.wiphy;
  709. sdata->local = local;
  710. sdata->dev = ndev;
  711. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  712. skb_queue_head_init(&sdata->fragments[i].skb_list);
  713. INIT_LIST_HEAD(&sdata->key_list);
  714. sdata->force_unicast_rateidx = -1;
  715. sdata->max_ratectrl_rateidx = -1;
  716. /* setup type-dependent data */
  717. ieee80211_setup_sdata(sdata, type);
  718. ret = register_netdevice(ndev);
  719. if (ret)
  720. goto fail;
  721. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  722. params && params->mesh_id_len)
  723. ieee80211_sdata_set_mesh_id(sdata,
  724. params->mesh_id_len,
  725. params->mesh_id);
  726. mutex_lock(&local->iflist_mtx);
  727. list_add_tail_rcu(&sdata->list, &local->interfaces);
  728. mutex_unlock(&local->iflist_mtx);
  729. if (new_dev)
  730. *new_dev = ndev;
  731. return 0;
  732. fail:
  733. free_netdev(ndev);
  734. return ret;
  735. }
  736. void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
  737. {
  738. ASSERT_RTNL();
  739. mutex_lock(&sdata->local->iflist_mtx);
  740. list_del_rcu(&sdata->list);
  741. mutex_unlock(&sdata->local->iflist_mtx);
  742. synchronize_rcu();
  743. unregister_netdevice(sdata->dev);
  744. }
  745. /*
  746. * Remove all interfaces, may only be called at hardware unregistration
  747. * time because it doesn't do RCU-safe list removals.
  748. */
  749. void ieee80211_remove_interfaces(struct ieee80211_local *local)
  750. {
  751. struct ieee80211_sub_if_data *sdata, *tmp;
  752. ASSERT_RTNL();
  753. list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
  754. /*
  755. * we cannot hold the iflist_mtx across unregister_netdevice,
  756. * but we only need to hold it for list modifications to lock
  757. * out readers since we're under the RTNL here as all other
  758. * writers.
  759. */
  760. mutex_lock(&local->iflist_mtx);
  761. list_del(&sdata->list);
  762. mutex_unlock(&local->iflist_mtx);
  763. unregister_netdevice(sdata->dev);
  764. }
  765. }
  766. static u32 ieee80211_idle_off(struct ieee80211_local *local,
  767. const char *reason)
  768. {
  769. if (!(local->hw.conf.flags & IEEE80211_CONF_IDLE))
  770. return 0;
  771. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  772. printk(KERN_DEBUG "%s: device no longer idle - %s\n",
  773. wiphy_name(local->hw.wiphy), reason);
  774. #endif
  775. local->hw.conf.flags &= ~IEEE80211_CONF_IDLE;
  776. return IEEE80211_CONF_CHANGE_IDLE;
  777. }
  778. static u32 ieee80211_idle_on(struct ieee80211_local *local)
  779. {
  780. if (local->hw.conf.flags & IEEE80211_CONF_IDLE)
  781. return 0;
  782. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  783. printk(KERN_DEBUG "%s: device now idle\n",
  784. wiphy_name(local->hw.wiphy));
  785. #endif
  786. local->hw.conf.flags |= IEEE80211_CONF_IDLE;
  787. return IEEE80211_CONF_CHANGE_IDLE;
  788. }
  789. u32 __ieee80211_recalc_idle(struct ieee80211_local *local)
  790. {
  791. struct ieee80211_sub_if_data *sdata;
  792. int count = 0;
  793. if (local->scanning)
  794. return ieee80211_idle_off(local, "scanning");
  795. list_for_each_entry(sdata, &local->interfaces, list) {
  796. if (!netif_running(sdata->dev))
  797. continue;
  798. /* do not count disabled managed interfaces */
  799. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  800. !sdata->u.mgd.associated &&
  801. list_empty(&sdata->u.mgd.work_list))
  802. continue;
  803. /* do not count unused IBSS interfaces */
  804. if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  805. !sdata->u.ibss.ssid_len)
  806. continue;
  807. /* count everything else */
  808. count++;
  809. }
  810. if (!count)
  811. return ieee80211_idle_on(local);
  812. else
  813. return ieee80211_idle_off(local, "in use");
  814. return 0;
  815. }
  816. void ieee80211_recalc_idle(struct ieee80211_local *local)
  817. {
  818. u32 chg;
  819. mutex_lock(&local->iflist_mtx);
  820. chg = __ieee80211_recalc_idle(local);
  821. mutex_unlock(&local->iflist_mtx);
  822. if (chg)
  823. ieee80211_hw_config(local, chg);
  824. }