ieee80211_iface.c 7.8 KB

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