iface.c 21 KB

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