iface.c 8.5 KB

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