iface.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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. u32 hw_reconf_flags = 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. /* we're brought up, everything changes */
  142. hw_reconf_flags = ~0;
  143. ieee80211_led_radio(local, local->hw.conf.radio_enabled);
  144. }
  145. /*
  146. * Check all interfaces and copy the hopefully now-present
  147. * MAC address to those that have the special null one.
  148. */
  149. list_for_each_entry(nsdata, &local->interfaces, list) {
  150. struct net_device *ndev = nsdata->dev;
  151. /*
  152. * No need to check netif_running since we do not allow
  153. * it to start up with this invalid address.
  154. */
  155. if (compare_ether_addr(null_addr, ndev->dev_addr) == 0)
  156. memcpy(ndev->dev_addr,
  157. local->hw.wiphy->perm_addr,
  158. ETH_ALEN);
  159. }
  160. if (compare_ether_addr(null_addr, local->mdev->dev_addr) == 0)
  161. memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr,
  162. ETH_ALEN);
  163. /*
  164. * Validate the MAC address for this device.
  165. */
  166. if (!is_valid_ether_addr(dev->dev_addr)) {
  167. if (!local->open_count && local->ops->stop)
  168. local->ops->stop(local_to_hw(local));
  169. return -EADDRNOTAVAIL;
  170. }
  171. switch (sdata->vif.type) {
  172. case NL80211_IFTYPE_AP_VLAN:
  173. /* no need to tell driver */
  174. break;
  175. case NL80211_IFTYPE_MONITOR:
  176. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  177. local->cooked_mntrs++;
  178. break;
  179. }
  180. /* must be before the call to ieee80211_configure_filter */
  181. local->monitors++;
  182. if (local->monitors == 1) {
  183. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  184. hw_reconf_flags |= IEEE80211_CONF_CHANGE_RADIOTAP;
  185. }
  186. if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
  187. local->fif_fcsfail++;
  188. if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
  189. local->fif_plcpfail++;
  190. if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
  191. local->fif_control++;
  192. if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
  193. local->fif_other_bss++;
  194. netif_addr_lock_bh(local->mdev);
  195. ieee80211_configure_filter(local);
  196. netif_addr_unlock_bh(local->mdev);
  197. break;
  198. case NL80211_IFTYPE_STATION:
  199. case NL80211_IFTYPE_ADHOC:
  200. sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
  201. /* fall through */
  202. default:
  203. conf.vif = &sdata->vif;
  204. conf.type = sdata->vif.type;
  205. conf.mac_addr = dev->dev_addr;
  206. res = local->ops->add_interface(local_to_hw(local), &conf);
  207. if (res)
  208. goto err_stop;
  209. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  210. local->fif_other_bss++;
  211. netif_addr_lock_bh(local->mdev);
  212. ieee80211_configure_filter(local);
  213. netif_addr_unlock_bh(local->mdev);
  214. ieee80211_start_mesh(sdata);
  215. }
  216. changed |= ieee80211_reset_erp_info(sdata);
  217. ieee80211_bss_info_change_notify(sdata, changed);
  218. ieee80211_enable_keys(sdata);
  219. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  220. !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
  221. netif_carrier_off(dev);
  222. else
  223. netif_carrier_on(dev);
  224. }
  225. if (sdata->vif.type == NL80211_IFTYPE_WDS) {
  226. /* Create STA entry for the WDS peer */
  227. sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
  228. GFP_KERNEL);
  229. if (!sta) {
  230. res = -ENOMEM;
  231. goto err_del_interface;
  232. }
  233. /* no locking required since STA is not live yet */
  234. sta->flags |= WLAN_STA_AUTHORIZED;
  235. res = sta_info_insert(sta);
  236. if (res) {
  237. /* STA has been freed */
  238. goto err_del_interface;
  239. }
  240. }
  241. if (local->open_count == 0) {
  242. res = dev_open(local->mdev);
  243. WARN_ON(res);
  244. if (res)
  245. goto err_del_interface;
  246. tasklet_enable(&local->tx_pending_tasklet);
  247. tasklet_enable(&local->tasklet);
  248. }
  249. /*
  250. * set_multicast_list will be invoked by the networking core
  251. * which will check whether any increments here were done in
  252. * error and sync them down to the hardware as filter flags.
  253. */
  254. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  255. atomic_inc(&local->iff_allmultis);
  256. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  257. atomic_inc(&local->iff_promiscs);
  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. /*
  269. * ieee80211_sta_work is disabled while network interface
  270. * is down. Therefore, some configuration changes may not
  271. * yet be effective. Trigger execution of ieee80211_sta_work
  272. * to fix this.
  273. */
  274. if (sdata->vif.type == NL80211_IFTYPE_STATION ||
  275. sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  276. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  277. queue_work(local->hw.workqueue, &ifsta->work);
  278. }
  279. netif_tx_start_all_queues(dev);
  280. return 0;
  281. err_del_interface:
  282. local->ops->remove_interface(local_to_hw(local), &conf);
  283. err_stop:
  284. if (!local->open_count && local->ops->stop)
  285. local->ops->stop(local_to_hw(local));
  286. err_del_bss:
  287. sdata->bss = NULL;
  288. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  289. list_del(&sdata->u.vlan.list);
  290. return res;
  291. }
  292. static int ieee80211_stop(struct net_device *dev)
  293. {
  294. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  295. struct ieee80211_local *local = sdata->local;
  296. struct ieee80211_if_init_conf conf;
  297. struct sta_info *sta;
  298. u32 hw_reconf_flags = 0;
  299. /*
  300. * Stop TX on this interface first.
  301. */
  302. netif_tx_stop_all_queues(dev);
  303. /*
  304. * Now delete all active aggregation sessions.
  305. */
  306. rcu_read_lock();
  307. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  308. if (sta->sdata == sdata)
  309. ieee80211_sta_tear_down_BA_sessions(sdata,
  310. sta->sta.addr);
  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. dev_mc_unsync(local->mdev, dev);
  342. /* APs need special treatment */
  343. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  344. struct ieee80211_sub_if_data *vlan, *tmp;
  345. struct beacon_data *old_beacon = sdata->u.ap.beacon;
  346. /* remove beacon */
  347. rcu_assign_pointer(sdata->u.ap.beacon, NULL);
  348. synchronize_rcu();
  349. kfree(old_beacon);
  350. /* down all dependent devices, that is VLANs */
  351. list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
  352. u.vlan.list)
  353. dev_close(vlan->dev);
  354. WARN_ON(!list_empty(&sdata->u.ap.vlans));
  355. }
  356. local->open_count--;
  357. switch (sdata->vif.type) {
  358. case NL80211_IFTYPE_AP_VLAN:
  359. list_del(&sdata->u.vlan.list);
  360. /* no need to tell driver */
  361. break;
  362. case NL80211_IFTYPE_MONITOR:
  363. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  364. local->cooked_mntrs--;
  365. break;
  366. }
  367. local->monitors--;
  368. if (local->monitors == 0) {
  369. local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
  370. hw_reconf_flags |= IEEE80211_CONF_CHANGE_RADIOTAP;
  371. }
  372. if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
  373. local->fif_fcsfail--;
  374. if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
  375. local->fif_plcpfail--;
  376. if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
  377. local->fif_control--;
  378. if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
  379. local->fif_other_bss--;
  380. netif_addr_lock_bh(local->mdev);
  381. ieee80211_configure_filter(local);
  382. netif_addr_unlock_bh(local->mdev);
  383. break;
  384. case NL80211_IFTYPE_STATION:
  385. case NL80211_IFTYPE_ADHOC:
  386. /* Announce that we are leaving the network. */
  387. if (sdata->u.sta.state != IEEE80211_STA_MLME_DISABLED)
  388. ieee80211_sta_deauthenticate(sdata,
  389. WLAN_REASON_DEAUTH_LEAVING);
  390. memset(sdata->u.sta.bssid, 0, ETH_ALEN);
  391. del_timer_sync(&sdata->u.sta.timer);
  392. /*
  393. * If the timer fired while we waited for it, it will have
  394. * requeued the work. Now the work will be running again
  395. * but will not rearm the timer again because it checks
  396. * whether the interface is running, which, at this point,
  397. * it no longer is.
  398. */
  399. cancel_work_sync(&sdata->u.sta.work);
  400. /*
  401. * When we get here, the interface is marked down.
  402. * Call synchronize_rcu() to wait for the RX path
  403. * should it be using the interface and enqueuing
  404. * frames at this very time on another CPU.
  405. */
  406. synchronize_rcu();
  407. skb_queue_purge(&sdata->u.sta.skb_queue);
  408. sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
  409. kfree(sdata->u.sta.extra_ie);
  410. sdata->u.sta.extra_ie = NULL;
  411. sdata->u.sta.extra_ie_len = 0;
  412. /* fall through */
  413. case NL80211_IFTYPE_MESH_POINT:
  414. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  415. /* other_bss and allmulti are always set on mesh
  416. * ifaces */
  417. local->fif_other_bss--;
  418. atomic_dec(&local->iff_allmultis);
  419. netif_addr_lock_bh(local->mdev);
  420. ieee80211_configure_filter(local);
  421. netif_addr_unlock_bh(local->mdev);
  422. ieee80211_stop_mesh(sdata);
  423. }
  424. /* fall through */
  425. default:
  426. if (local->scan_sdata == sdata) {
  427. if (!local->ops->hw_scan)
  428. cancel_delayed_work_sync(&local->scan_work);
  429. /*
  430. * The software scan can no longer run now, so we can
  431. * clear out the scan_sdata reference. However, the
  432. * hardware scan may still be running. The complete
  433. * function must be prepared to handle a NULL value.
  434. */
  435. local->scan_sdata = NULL;
  436. /*
  437. * The memory barrier guarantees that another CPU
  438. * that is hardware-scanning will now see the fact
  439. * that this interface is gone.
  440. */
  441. smp_mb();
  442. /*
  443. * If software scanning, complete the scan but since
  444. * the scan_sdata is NULL already don't send out a
  445. * scan event to userspace -- the scan is incomplete.
  446. */
  447. if (local->sw_scanning)
  448. ieee80211_scan_completed(&local->hw);
  449. }
  450. conf.vif = &sdata->vif;
  451. conf.type = sdata->vif.type;
  452. conf.mac_addr = dev->dev_addr;
  453. /* disable all keys for as long as this netdev is down */
  454. ieee80211_disable_keys(sdata);
  455. local->ops->remove_interface(local_to_hw(local), &conf);
  456. }
  457. sdata->bss = NULL;
  458. if (local->open_count == 0) {
  459. if (netif_running(local->mdev))
  460. dev_close(local->mdev);
  461. if (local->ops->stop)
  462. local->ops->stop(local_to_hw(local));
  463. ieee80211_led_radio(local, 0);
  464. flush_workqueue(local->hw.workqueue);
  465. tasklet_disable(&local->tx_pending_tasklet);
  466. tasklet_disable(&local->tasklet);
  467. /* no reconfiguring after stop! */
  468. hw_reconf_flags = 0;
  469. }
  470. /* do after stop to avoid reconfiguring when we stop anyway */
  471. if (hw_reconf_flags)
  472. ieee80211_hw_config(local, hw_reconf_flags);
  473. return 0;
  474. }
  475. static void ieee80211_set_multicast_list(struct net_device *dev)
  476. {
  477. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  478. struct ieee80211_local *local = sdata->local;
  479. int allmulti, promisc, sdata_allmulti, sdata_promisc;
  480. allmulti = !!(dev->flags & IFF_ALLMULTI);
  481. promisc = !!(dev->flags & IFF_PROMISC);
  482. sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
  483. sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
  484. if (allmulti != sdata_allmulti) {
  485. if (dev->flags & IFF_ALLMULTI)
  486. atomic_inc(&local->iff_allmultis);
  487. else
  488. atomic_dec(&local->iff_allmultis);
  489. sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
  490. }
  491. if (promisc != sdata_promisc) {
  492. if (dev->flags & IFF_PROMISC)
  493. atomic_inc(&local->iff_promiscs);
  494. else
  495. atomic_dec(&local->iff_promiscs);
  496. sdata->flags ^= IEEE80211_SDATA_PROMISC;
  497. }
  498. dev_mc_sync(local->mdev, dev);
  499. }
  500. static void ieee80211_if_setup(struct net_device *dev)
  501. {
  502. ether_setup(dev);
  503. dev->hard_start_xmit = ieee80211_subif_start_xmit;
  504. dev->wireless_handlers = &ieee80211_iw_handler_def;
  505. dev->set_multicast_list = ieee80211_set_multicast_list;
  506. dev->change_mtu = ieee80211_change_mtu;
  507. dev->open = ieee80211_open;
  508. dev->stop = ieee80211_stop;
  509. dev->destructor = free_netdev;
  510. /* we will validate the address ourselves in ->open */
  511. dev->validate_addr = NULL;
  512. }
  513. /*
  514. * Called when the netdev is removed or, by the code below, before
  515. * the interface type changes.
  516. */
  517. static void ieee80211_teardown_sdata(struct net_device *dev)
  518. {
  519. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  520. struct ieee80211_local *local = sdata->local;
  521. struct beacon_data *beacon;
  522. struct sk_buff *skb;
  523. int flushed;
  524. int i;
  525. /* free extra data */
  526. ieee80211_free_keys(sdata);
  527. ieee80211_debugfs_remove_netdev(sdata);
  528. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  529. __skb_queue_purge(&sdata->fragments[i].skb_list);
  530. sdata->fragment_next = 0;
  531. switch (sdata->vif.type) {
  532. case NL80211_IFTYPE_AP:
  533. beacon = sdata->u.ap.beacon;
  534. rcu_assign_pointer(sdata->u.ap.beacon, NULL);
  535. synchronize_rcu();
  536. kfree(beacon);
  537. while ((skb = skb_dequeue(&sdata->u.ap.ps_bc_buf))) {
  538. local->total_ps_buffered--;
  539. dev_kfree_skb(skb);
  540. }
  541. break;
  542. case NL80211_IFTYPE_MESH_POINT:
  543. if (ieee80211_vif_is_mesh(&sdata->vif))
  544. mesh_rmc_free(sdata);
  545. break;
  546. case NL80211_IFTYPE_STATION:
  547. case NL80211_IFTYPE_ADHOC:
  548. kfree(sdata->u.sta.extra_ie);
  549. kfree(sdata->u.sta.assocreq_ies);
  550. kfree(sdata->u.sta.assocresp_ies);
  551. kfree_skb(sdata->u.sta.probe_resp);
  552. break;
  553. case NL80211_IFTYPE_WDS:
  554. case NL80211_IFTYPE_AP_VLAN:
  555. case NL80211_IFTYPE_MONITOR:
  556. break;
  557. case NL80211_IFTYPE_UNSPECIFIED:
  558. case __NL80211_IFTYPE_AFTER_LAST:
  559. BUG();
  560. break;
  561. }
  562. flushed = sta_info_flush(local, sdata);
  563. WARN_ON(flushed);
  564. }
  565. /*
  566. * Helper function to initialise an interface to a specific type.
  567. */
  568. static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
  569. enum nl80211_iftype type)
  570. {
  571. /* clear type-dependent union */
  572. memset(&sdata->u, 0, sizeof(sdata->u));
  573. /* and set some type-dependent values */
  574. sdata->vif.type = type;
  575. sdata->dev->hard_start_xmit = ieee80211_subif_start_xmit;
  576. sdata->wdev.iftype = type;
  577. /* only monitor differs */
  578. sdata->dev->type = ARPHRD_ETHER;
  579. switch (type) {
  580. case NL80211_IFTYPE_AP:
  581. skb_queue_head_init(&sdata->u.ap.ps_bc_buf);
  582. INIT_LIST_HEAD(&sdata->u.ap.vlans);
  583. break;
  584. case NL80211_IFTYPE_STATION:
  585. case NL80211_IFTYPE_ADHOC:
  586. ieee80211_sta_setup_sdata(sdata);
  587. break;
  588. case NL80211_IFTYPE_MESH_POINT:
  589. if (ieee80211_vif_is_mesh(&sdata->vif))
  590. ieee80211_mesh_init_sdata(sdata);
  591. break;
  592. case NL80211_IFTYPE_MONITOR:
  593. sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
  594. sdata->dev->hard_start_xmit = ieee80211_monitor_start_xmit;
  595. sdata->u.mntr_flags = MONITOR_FLAG_CONTROL |
  596. MONITOR_FLAG_OTHER_BSS;
  597. break;
  598. case NL80211_IFTYPE_WDS:
  599. case NL80211_IFTYPE_AP_VLAN:
  600. break;
  601. case NL80211_IFTYPE_UNSPECIFIED:
  602. case __NL80211_IFTYPE_AFTER_LAST:
  603. BUG();
  604. break;
  605. }
  606. ieee80211_debugfs_add_netdev(sdata);
  607. }
  608. int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
  609. enum nl80211_iftype type)
  610. {
  611. ASSERT_RTNL();
  612. if (type == sdata->vif.type)
  613. return 0;
  614. /* Setting ad-hoc mode on non-IBSS channel is not supported. */
  615. if (sdata->local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)
  616. return -EOPNOTSUPP;
  617. /*
  618. * We could, here, on changes between IBSS/STA/MESH modes,
  619. * invoke an MLME function instead that disassociates etc.
  620. * and goes into the requested mode.
  621. */
  622. if (netif_running(sdata->dev))
  623. return -EBUSY;
  624. /* Purge and reset type-dependent state. */
  625. ieee80211_teardown_sdata(sdata->dev);
  626. ieee80211_setup_sdata(sdata, type);
  627. /* reset some values that shouldn't be kept across type changes */
  628. sdata->vif.bss_conf.basic_rates =
  629. ieee80211_mandatory_rates(sdata->local,
  630. sdata->local->hw.conf.channel->band);
  631. sdata->drop_unencrypted = 0;
  632. return 0;
  633. }
  634. int ieee80211_if_add(struct ieee80211_local *local, const char *name,
  635. struct net_device **new_dev, enum nl80211_iftype type,
  636. struct vif_params *params)
  637. {
  638. struct net_device *ndev;
  639. struct ieee80211_sub_if_data *sdata = NULL;
  640. int ret, i;
  641. ASSERT_RTNL();
  642. ndev = alloc_netdev(sizeof(*sdata) + local->hw.vif_data_size,
  643. name, ieee80211_if_setup);
  644. if (!ndev)
  645. return -ENOMEM;
  646. ndev->needed_headroom = local->tx_headroom +
  647. 4*6 /* four MAC addresses */
  648. + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
  649. + 6 /* mesh */
  650. + 8 /* rfc1042/bridge tunnel */
  651. - ETH_HLEN /* ethernet hard_header_len */
  652. + IEEE80211_ENCRYPT_HEADROOM;
  653. ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
  654. ret = dev_alloc_name(ndev, ndev->name);
  655. if (ret < 0)
  656. goto fail;
  657. memcpy(ndev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  658. SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
  659. /* don't use IEEE80211_DEV_TO_SUB_IF because it checks too much */
  660. sdata = netdev_priv(ndev);
  661. ndev->ieee80211_ptr = &sdata->wdev;
  662. /* initialise type-independent data */
  663. sdata->wdev.wiphy = local->hw.wiphy;
  664. sdata->local = local;
  665. sdata->dev = ndev;
  666. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  667. skb_queue_head_init(&sdata->fragments[i].skb_list);
  668. INIT_LIST_HEAD(&sdata->key_list);
  669. sdata->force_unicast_rateidx = -1;
  670. sdata->max_ratectrl_rateidx = -1;
  671. /* setup type-dependent data */
  672. ieee80211_setup_sdata(sdata, type);
  673. ret = register_netdevice(ndev);
  674. if (ret)
  675. goto fail;
  676. ndev->uninit = ieee80211_teardown_sdata;
  677. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  678. params && params->mesh_id_len)
  679. ieee80211_sdata_set_mesh_id(sdata,
  680. params->mesh_id_len,
  681. params->mesh_id);
  682. list_add_tail_rcu(&sdata->list, &local->interfaces);
  683. if (new_dev)
  684. *new_dev = ndev;
  685. return 0;
  686. fail:
  687. free_netdev(ndev);
  688. return ret;
  689. }
  690. void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
  691. {
  692. ASSERT_RTNL();
  693. list_del_rcu(&sdata->list);
  694. synchronize_rcu();
  695. unregister_netdevice(sdata->dev);
  696. }
  697. /*
  698. * Remove all interfaces, may only be called at hardware unregistration
  699. * time because it doesn't do RCU-safe list removals.
  700. */
  701. void ieee80211_remove_interfaces(struct ieee80211_local *local)
  702. {
  703. struct ieee80211_sub_if_data *sdata, *tmp;
  704. ASSERT_RTNL();
  705. list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
  706. list_del(&sdata->list);
  707. unregister_netdevice(sdata->dev);
  708. }
  709. }