ieee80211_iface.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/if_arp.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/rtnetlink.h>
  14. #include <net/mac80211.h>
  15. #include "ieee80211_i.h"
  16. #include "sta_info.h"
  17. #include "debugfs_netdev.h"
  18. void ieee80211_if_sdata_init(struct ieee80211_sub_if_data *sdata)
  19. {
  20. int i;
  21. /* Default values for sub-interface parameters */
  22. sdata->drop_unencrypted = 0;
  23. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  24. skb_queue_head_init(&sdata->fragments[i].skb_list);
  25. INIT_LIST_HEAD(&sdata->key_list);
  26. }
  27. static void ieee80211_if_sdata_deinit(struct ieee80211_sub_if_data *sdata)
  28. {
  29. int i;
  30. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
  31. __skb_queue_purge(&sdata->fragments[i].skb_list);
  32. }
  33. }
  34. /* Must be called with rtnl lock held. */
  35. int ieee80211_if_add(struct net_device *dev, const char *name,
  36. struct net_device **new_dev, int type)
  37. {
  38. struct net_device *ndev;
  39. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  40. struct ieee80211_sub_if_data *sdata = NULL;
  41. int ret;
  42. ASSERT_RTNL();
  43. ndev = alloc_netdev(sizeof(*sdata) + local->hw.vif_data_size,
  44. name, ieee80211_if_setup);
  45. if (!ndev)
  46. return -ENOMEM;
  47. ret = dev_alloc_name(ndev, ndev->name);
  48. if (ret < 0)
  49. goto fail;
  50. memcpy(ndev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  51. ndev->base_addr = dev->base_addr;
  52. ndev->irq = dev->irq;
  53. ndev->mem_start = dev->mem_start;
  54. ndev->mem_end = dev->mem_end;
  55. SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
  56. sdata = IEEE80211_DEV_TO_SUB_IF(ndev);
  57. ndev->ieee80211_ptr = &sdata->wdev;
  58. sdata->wdev.wiphy = local->hw.wiphy;
  59. sdata->vif.type = IEEE80211_IF_TYPE_AP;
  60. sdata->dev = ndev;
  61. sdata->local = local;
  62. ieee80211_if_sdata_init(sdata);
  63. ret = register_netdevice(ndev);
  64. if (ret)
  65. goto fail;
  66. ieee80211_debugfs_add_netdev(sdata);
  67. ieee80211_if_set_type(ndev, type);
  68. /* we're under RTNL so all this is fine */
  69. if (unlikely(local->reg_state == IEEE80211_DEV_UNREGISTERED)) {
  70. __ieee80211_if_del(local, sdata);
  71. return -ENODEV;
  72. }
  73. list_add_tail_rcu(&sdata->list, &local->interfaces);
  74. if (new_dev)
  75. *new_dev = ndev;
  76. return 0;
  77. fail:
  78. free_netdev(ndev);
  79. return ret;
  80. }
  81. void ieee80211_if_set_type(struct net_device *dev, int type)
  82. {
  83. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  84. int oldtype = sdata->vif.type;
  85. /*
  86. * We need to call this function on the master interface
  87. * which already has a hard_start_xmit routine assigned
  88. * which must not be changed.
  89. */
  90. if (dev != sdata->local->mdev)
  91. dev->hard_start_xmit = ieee80211_subif_start_xmit;
  92. /*
  93. * Called even when register_netdevice fails, it would
  94. * oops if assigned before initialising the rest.
  95. */
  96. dev->uninit = ieee80211_if_reinit;
  97. /* most have no BSS pointer */
  98. sdata->bss = NULL;
  99. sdata->vif.type = type;
  100. switch (type) {
  101. case IEEE80211_IF_TYPE_WDS:
  102. /* nothing special */
  103. break;
  104. case IEEE80211_IF_TYPE_VLAN:
  105. sdata->u.vlan.ap = NULL;
  106. break;
  107. case IEEE80211_IF_TYPE_AP:
  108. sdata->u.ap.force_unicast_rateidx = -1;
  109. sdata->u.ap.max_ratectrl_rateidx = -1;
  110. skb_queue_head_init(&sdata->u.ap.ps_bc_buf);
  111. sdata->bss = &sdata->u.ap;
  112. INIT_LIST_HEAD(&sdata->u.ap.vlans);
  113. break;
  114. case IEEE80211_IF_TYPE_STA:
  115. case IEEE80211_IF_TYPE_IBSS: {
  116. struct ieee80211_sub_if_data *msdata;
  117. struct ieee80211_if_sta *ifsta;
  118. ifsta = &sdata->u.sta;
  119. INIT_WORK(&ifsta->work, ieee80211_sta_work);
  120. setup_timer(&ifsta->timer, ieee80211_sta_timer,
  121. (unsigned long) sdata);
  122. skb_queue_head_init(&ifsta->skb_queue);
  123. ifsta->capab = WLAN_CAPABILITY_ESS;
  124. ifsta->auth_algs = IEEE80211_AUTH_ALG_OPEN |
  125. IEEE80211_AUTH_ALG_SHARED_KEY;
  126. ifsta->flags |= IEEE80211_STA_CREATE_IBSS |
  127. IEEE80211_STA_WMM_ENABLED |
  128. IEEE80211_STA_AUTO_BSSID_SEL |
  129. IEEE80211_STA_AUTO_CHANNEL_SEL;
  130. msdata = IEEE80211_DEV_TO_SUB_IF(sdata->local->mdev);
  131. sdata->bss = &msdata->u.ap;
  132. break;
  133. }
  134. case IEEE80211_IF_TYPE_MNTR:
  135. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  136. dev->hard_start_xmit = ieee80211_monitor_start_xmit;
  137. break;
  138. default:
  139. printk(KERN_WARNING "%s: %s: Unknown interface type 0x%x",
  140. dev->name, __FUNCTION__, type);
  141. }
  142. ieee80211_debugfs_change_if_type(sdata, oldtype);
  143. }
  144. /* Must be called with rtnl lock held. */
  145. void ieee80211_if_reinit(struct net_device *dev)
  146. {
  147. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  148. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  149. struct sta_info *sta;
  150. struct sk_buff *skb;
  151. ASSERT_RTNL();
  152. ieee80211_free_keys(sdata);
  153. ieee80211_if_sdata_deinit(sdata);
  154. switch (sdata->vif.type) {
  155. case IEEE80211_IF_TYPE_INVALID:
  156. /* cannot happen */
  157. WARN_ON(1);
  158. break;
  159. case IEEE80211_IF_TYPE_AP: {
  160. /* Remove all virtual interfaces that use this BSS
  161. * as their sdata->bss */
  162. struct ieee80211_sub_if_data *tsdata, *n;
  163. list_for_each_entry_safe(tsdata, n, &local->interfaces, list) {
  164. if (tsdata != sdata && tsdata->bss == &sdata->u.ap) {
  165. printk(KERN_DEBUG "%s: removing virtual "
  166. "interface %s because its BSS interface"
  167. " is being removed\n",
  168. sdata->dev->name, tsdata->dev->name);
  169. list_del_rcu(&tsdata->list);
  170. /*
  171. * We have lots of time and can afford
  172. * to sync for each interface
  173. */
  174. synchronize_rcu();
  175. __ieee80211_if_del(local, tsdata);
  176. }
  177. }
  178. kfree(sdata->u.ap.beacon);
  179. while ((skb = skb_dequeue(&sdata->u.ap.ps_bc_buf))) {
  180. local->total_ps_buffered--;
  181. dev_kfree_skb(skb);
  182. }
  183. break;
  184. }
  185. case IEEE80211_IF_TYPE_WDS:
  186. sta = sta_info_get(local, sdata->u.wds.remote_addr);
  187. if (sta) {
  188. sta_info_free(sta);
  189. sta_info_put(sta);
  190. } else {
  191. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  192. printk(KERN_DEBUG "%s: Someone had deleted my STA "
  193. "entry for the WDS link\n", dev->name);
  194. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  195. }
  196. break;
  197. case IEEE80211_IF_TYPE_STA:
  198. case IEEE80211_IF_TYPE_IBSS:
  199. kfree(sdata->u.sta.extra_ie);
  200. sdata->u.sta.extra_ie = NULL;
  201. kfree(sdata->u.sta.assocreq_ies);
  202. sdata->u.sta.assocreq_ies = NULL;
  203. kfree(sdata->u.sta.assocresp_ies);
  204. sdata->u.sta.assocresp_ies = NULL;
  205. if (sdata->u.sta.probe_resp) {
  206. dev_kfree_skb(sdata->u.sta.probe_resp);
  207. sdata->u.sta.probe_resp = NULL;
  208. }
  209. break;
  210. case IEEE80211_IF_TYPE_MNTR:
  211. dev->type = ARPHRD_ETHER;
  212. break;
  213. case IEEE80211_IF_TYPE_VLAN:
  214. sdata->u.vlan.ap = NULL;
  215. break;
  216. }
  217. /* remove all STAs that are bound to this virtual interface */
  218. sta_info_flush(local, dev);
  219. memset(&sdata->u, 0, sizeof(sdata->u));
  220. ieee80211_if_sdata_init(sdata);
  221. }
  222. /* Must be called with rtnl lock held. */
  223. void __ieee80211_if_del(struct ieee80211_local *local,
  224. struct ieee80211_sub_if_data *sdata)
  225. {
  226. struct net_device *dev = sdata->dev;
  227. ieee80211_debugfs_remove_netdev(sdata);
  228. unregister_netdevice(dev);
  229. /* Except master interface, the net_device will be freed by
  230. * net_device->destructor (i. e. ieee80211_if_free). */
  231. }
  232. /* Must be called with rtnl lock held. */
  233. int ieee80211_if_remove(struct net_device *dev, const char *name, int id)
  234. {
  235. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  236. struct ieee80211_sub_if_data *sdata, *n;
  237. ASSERT_RTNL();
  238. list_for_each_entry_safe(sdata, n, &local->interfaces, list) {
  239. if ((sdata->vif.type == id || id == -1) &&
  240. strcmp(name, sdata->dev->name) == 0 &&
  241. sdata->dev != local->mdev) {
  242. list_del_rcu(&sdata->list);
  243. synchronize_rcu();
  244. __ieee80211_if_del(local, sdata);
  245. return 0;
  246. }
  247. }
  248. return -ENODEV;
  249. }
  250. void ieee80211_if_free(struct net_device *dev)
  251. {
  252. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  253. ieee80211_if_sdata_deinit(sdata);
  254. free_netdev(dev);
  255. }