iface.c 8.4 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. }
  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. ndev->needed_headroom = local->tx_headroom +
  50. 4*6 /* four MAC addresses */
  51. + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
  52. + 6 /* mesh */
  53. + 8 /* rfc1042/bridge tunnel */
  54. - ETH_HLEN /* ethernet hard_header_len */
  55. + IEEE80211_ENCRYPT_HEADROOM;
  56. ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
  57. ret = dev_alloc_name(ndev, ndev->name);
  58. if (ret < 0)
  59. goto fail;
  60. memcpy(ndev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  61. ndev->base_addr = dev->base_addr;
  62. ndev->irq = dev->irq;
  63. ndev->mem_start = dev->mem_start;
  64. ndev->mem_end = dev->mem_end;
  65. SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
  66. sdata = IEEE80211_DEV_TO_SUB_IF(ndev);
  67. ndev->ieee80211_ptr = &sdata->wdev;
  68. sdata->wdev.wiphy = local->hw.wiphy;
  69. sdata->vif.type = IEEE80211_IF_TYPE_AP;
  70. sdata->dev = ndev;
  71. sdata->local = local;
  72. ieee80211_if_sdata_init(sdata);
  73. ret = register_netdevice(ndev);
  74. if (ret)
  75. goto fail;
  76. ieee80211_debugfs_add_netdev(sdata);
  77. ieee80211_if_set_type(ndev, type);
  78. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  79. params && params->mesh_id_len)
  80. ieee80211_if_sta_set_mesh_id(&sdata->u.sta,
  81. params->mesh_id_len,
  82. params->mesh_id);
  83. /* we're under RTNL so all this is fine */
  84. if (unlikely(local->reg_state == IEEE80211_DEV_UNREGISTERED)) {
  85. __ieee80211_if_del(local, sdata);
  86. return -ENODEV;
  87. }
  88. list_add_tail_rcu(&sdata->list, &local->interfaces);
  89. if (new_dev)
  90. *new_dev = ndev;
  91. return 0;
  92. fail:
  93. free_netdev(ndev);
  94. return ret;
  95. }
  96. void ieee80211_if_set_type(struct net_device *dev, int type)
  97. {
  98. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  99. int oldtype = sdata->vif.type;
  100. /*
  101. * We need to call this function on the master interface
  102. * which already has a hard_start_xmit routine assigned
  103. * which must not be changed.
  104. */
  105. if (dev != sdata->local->mdev)
  106. dev->hard_start_xmit = ieee80211_subif_start_xmit;
  107. /*
  108. * Called even when register_netdevice fails, it would
  109. * oops if assigned before initialising the rest.
  110. */
  111. dev->uninit = ieee80211_if_reinit;
  112. /* most have no BSS pointer */
  113. sdata->bss = NULL;
  114. sdata->vif.type = type;
  115. sdata->basic_rates = 0;
  116. switch (type) {
  117. case IEEE80211_IF_TYPE_WDS:
  118. /* nothing special */
  119. break;
  120. case IEEE80211_IF_TYPE_VLAN:
  121. sdata->u.vlan.ap = NULL;
  122. break;
  123. case IEEE80211_IF_TYPE_AP:
  124. sdata->u.ap.force_unicast_rateidx = -1;
  125. sdata->u.ap.max_ratectrl_rateidx = -1;
  126. skb_queue_head_init(&sdata->u.ap.ps_bc_buf);
  127. sdata->bss = &sdata->u.ap;
  128. INIT_LIST_HEAD(&sdata->u.ap.vlans);
  129. break;
  130. case IEEE80211_IF_TYPE_MESH_POINT:
  131. case IEEE80211_IF_TYPE_STA:
  132. case IEEE80211_IF_TYPE_IBSS: {
  133. struct ieee80211_sub_if_data *msdata;
  134. struct ieee80211_if_sta *ifsta;
  135. ifsta = &sdata->u.sta;
  136. INIT_WORK(&ifsta->work, ieee80211_sta_work);
  137. setup_timer(&ifsta->timer, ieee80211_sta_timer,
  138. (unsigned long) sdata);
  139. skb_queue_head_init(&ifsta->skb_queue);
  140. ifsta->capab = WLAN_CAPABILITY_ESS;
  141. ifsta->auth_algs = IEEE80211_AUTH_ALG_OPEN |
  142. IEEE80211_AUTH_ALG_SHARED_KEY;
  143. ifsta->flags |= IEEE80211_STA_CREATE_IBSS |
  144. IEEE80211_STA_WMM_ENABLED |
  145. IEEE80211_STA_AUTO_BSSID_SEL |
  146. IEEE80211_STA_AUTO_CHANNEL_SEL;
  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. }