iface.c 26 KB

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