iface.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/if_arp.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/rtnetlink.h>
  15. #include <net/mac80211.h>
  16. #include "ieee80211_i.h"
  17. #include "sta_info.h"
  18. #include "debugfs_netdev.h"
  19. #include "mesh.h"
  20. /*
  21. * Called when the netdev is removed or, by the code below, before
  22. * the interface type changes.
  23. */
  24. static void ieee80211_teardown_sdata(struct net_device *dev)
  25. {
  26. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  27. struct ieee80211_local *local = sdata->local;
  28. struct beacon_data *beacon;
  29. struct sk_buff *skb;
  30. int flushed;
  31. int i;
  32. ieee80211_debugfs_remove_netdev(sdata);
  33. /* free extra data */
  34. ieee80211_free_keys(sdata);
  35. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  36. __skb_queue_purge(&sdata->fragments[i].skb_list);
  37. sdata->fragment_next = 0;
  38. switch (sdata->vif.type) {
  39. case IEEE80211_IF_TYPE_AP:
  40. beacon = sdata->u.ap.beacon;
  41. rcu_assign_pointer(sdata->u.ap.beacon, NULL);
  42. synchronize_rcu();
  43. kfree(beacon);
  44. while ((skb = skb_dequeue(&sdata->u.ap.ps_bc_buf))) {
  45. local->total_ps_buffered--;
  46. dev_kfree_skb(skb);
  47. }
  48. break;
  49. case IEEE80211_IF_TYPE_MESH_POINT:
  50. /* Allow compiler to elide mesh_rmc_free call. */
  51. if (ieee80211_vif_is_mesh(&sdata->vif))
  52. mesh_rmc_free(dev);
  53. /* fall through */
  54. case IEEE80211_IF_TYPE_STA:
  55. case IEEE80211_IF_TYPE_IBSS:
  56. kfree(sdata->u.sta.extra_ie);
  57. kfree(sdata->u.sta.assocreq_ies);
  58. kfree(sdata->u.sta.assocresp_ies);
  59. kfree_skb(sdata->u.sta.probe_resp);
  60. break;
  61. case IEEE80211_IF_TYPE_WDS:
  62. case IEEE80211_IF_TYPE_VLAN:
  63. case IEEE80211_IF_TYPE_MNTR:
  64. break;
  65. case IEEE80211_IF_TYPE_INVALID:
  66. BUG();
  67. break;
  68. }
  69. flushed = sta_info_flush(local, sdata);
  70. WARN_ON(flushed);
  71. }
  72. /*
  73. * Helper function to initialise an interface to a specific type.
  74. */
  75. static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
  76. enum ieee80211_if_types type)
  77. {
  78. struct ieee80211_if_sta *ifsta;
  79. /* clear type-dependent union */
  80. memset(&sdata->u, 0, sizeof(sdata->u));
  81. /* and set some type-dependent values */
  82. sdata->vif.type = type;
  83. /* only monitor differs */
  84. sdata->dev->type = ARPHRD_ETHER;
  85. switch (type) {
  86. case IEEE80211_IF_TYPE_AP:
  87. skb_queue_head_init(&sdata->u.ap.ps_bc_buf);
  88. INIT_LIST_HEAD(&sdata->u.ap.vlans);
  89. break;
  90. case IEEE80211_IF_TYPE_MESH_POINT:
  91. case IEEE80211_IF_TYPE_STA:
  92. case IEEE80211_IF_TYPE_IBSS:
  93. ifsta = &sdata->u.sta;
  94. INIT_WORK(&ifsta->work, ieee80211_sta_work);
  95. setup_timer(&ifsta->timer, ieee80211_sta_timer,
  96. (unsigned long) sdata);
  97. skb_queue_head_init(&ifsta->skb_queue);
  98. ifsta->capab = WLAN_CAPABILITY_ESS;
  99. ifsta->auth_algs = IEEE80211_AUTH_ALG_OPEN |
  100. IEEE80211_AUTH_ALG_SHARED_KEY;
  101. ifsta->flags |= IEEE80211_STA_CREATE_IBSS |
  102. IEEE80211_STA_AUTO_BSSID_SEL |
  103. IEEE80211_STA_AUTO_CHANNEL_SEL;
  104. if (ieee80211_num_regular_queues(&sdata->local->hw) >= 4)
  105. ifsta->flags |= IEEE80211_STA_WMM_ENABLED;
  106. if (ieee80211_vif_is_mesh(&sdata->vif))
  107. ieee80211_mesh_init_sdata(sdata);
  108. break;
  109. case IEEE80211_IF_TYPE_MNTR:
  110. sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
  111. sdata->dev->hard_start_xmit = ieee80211_monitor_start_xmit;
  112. sdata->u.mntr_flags = MONITOR_FLAG_CONTROL |
  113. MONITOR_FLAG_OTHER_BSS;
  114. break;
  115. case IEEE80211_IF_TYPE_WDS:
  116. case IEEE80211_IF_TYPE_VLAN:
  117. break;
  118. case IEEE80211_IF_TYPE_INVALID:
  119. BUG();
  120. break;
  121. }
  122. ieee80211_debugfs_add_netdev(sdata);
  123. }
  124. int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
  125. enum ieee80211_if_types type)
  126. {
  127. ASSERT_RTNL();
  128. if (type == sdata->vif.type)
  129. return 0;
  130. /*
  131. * We could, here, on changes between IBSS/STA/MESH modes,
  132. * invoke an MLME function instead that disassociates etc.
  133. * and goes into the requested mode.
  134. */
  135. if (netif_running(sdata->dev))
  136. return -EBUSY;
  137. /* Purge and reset type-dependent state. */
  138. ieee80211_teardown_sdata(sdata->dev);
  139. ieee80211_setup_sdata(sdata, type);
  140. /* reset some values that shouldn't be kept across type changes */
  141. sdata->basic_rates = 0;
  142. sdata->drop_unencrypted = 0;
  143. return 0;
  144. }
  145. int ieee80211_if_add(struct ieee80211_local *local, const char *name,
  146. struct net_device **new_dev, enum ieee80211_if_types type,
  147. struct vif_params *params)
  148. {
  149. struct net_device *ndev;
  150. struct ieee80211_sub_if_data *sdata = NULL;
  151. int ret, i;
  152. ASSERT_RTNL();
  153. ndev = alloc_netdev(sizeof(*sdata) + local->hw.vif_data_size,
  154. name, ieee80211_if_setup);
  155. if (!ndev)
  156. return -ENOMEM;
  157. ndev->needed_headroom = local->tx_headroom +
  158. 4*6 /* four MAC addresses */
  159. + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
  160. + 6 /* mesh */
  161. + 8 /* rfc1042/bridge tunnel */
  162. - ETH_HLEN /* ethernet hard_header_len */
  163. + IEEE80211_ENCRYPT_HEADROOM;
  164. ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
  165. ret = dev_alloc_name(ndev, ndev->name);
  166. if (ret < 0)
  167. goto fail;
  168. memcpy(ndev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  169. SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
  170. /* don't use IEEE80211_DEV_TO_SUB_IF because it checks too much */
  171. sdata = netdev_priv(ndev);
  172. ndev->ieee80211_ptr = &sdata->wdev;
  173. /* initialise type-independent data */
  174. sdata->wdev.wiphy = local->hw.wiphy;
  175. sdata->local = local;
  176. sdata->dev = ndev;
  177. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  178. skb_queue_head_init(&sdata->fragments[i].skb_list);
  179. INIT_LIST_HEAD(&sdata->key_list);
  180. sdata->force_unicast_rateidx = -1;
  181. sdata->max_ratectrl_rateidx = -1;
  182. /* setup type-dependent data */
  183. ieee80211_setup_sdata(sdata, type);
  184. ret = register_netdevice(ndev);
  185. if (ret)
  186. goto fail;
  187. ndev->uninit = ieee80211_teardown_sdata;
  188. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  189. params && params->mesh_id_len)
  190. ieee80211_if_sta_set_mesh_id(&sdata->u.sta,
  191. params->mesh_id_len,
  192. params->mesh_id);
  193. list_add_tail_rcu(&sdata->list, &local->interfaces);
  194. if (new_dev)
  195. *new_dev = ndev;
  196. return 0;
  197. fail:
  198. free_netdev(ndev);
  199. return ret;
  200. }
  201. void ieee80211_if_remove(struct net_device *dev)
  202. {
  203. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  204. ASSERT_RTNL();
  205. list_del_rcu(&sdata->list);
  206. synchronize_rcu();
  207. unregister_netdevice(dev);
  208. }
  209. /*
  210. * Remove all interfaces, may only be called at hardware unregistration
  211. * time because it doesn't do RCU-safe list removals.
  212. */
  213. void ieee80211_remove_interfaces(struct ieee80211_local *local)
  214. {
  215. struct ieee80211_sub_if_data *sdata, *tmp;
  216. ASSERT_RTNL();
  217. list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
  218. list_del(&sdata->list);
  219. unregister_netdevice(sdata->dev);
  220. }
  221. }