iface.c 8.1 KB

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