blob: 2ba24ef319dab534dcf97c1b657b0b462148dac3 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
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"
Jiri Bence9f207f2007-05-05 11:46:38 -070017#include "debugfs_netdev.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070018
19void ieee80211_if_sdata_init(struct ieee80211_sub_if_data *sdata)
20{
21 int i;
22
23 /* Default values for sub-interface parameters */
24 sdata->drop_unencrypted = 0;
25 sdata->eapol = 1;
26 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
27 skb_queue_head_init(&sdata->fragments[i].skb_list);
Johannes Berg11a843b2007-08-28 17:01:55 -040028
29 INIT_LIST_HEAD(&sdata->key_list);
Jiri Bencf0706e82007-05-05 11:45:53 -070030}
31
32static void ieee80211_if_sdata_deinit(struct ieee80211_sub_if_data *sdata)
33{
34 int i;
35
36 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
37 __skb_queue_purge(&sdata->fragments[i].skb_list);
38 }
39}
40
41/* Must be called with rtnl lock held. */
42int ieee80211_if_add(struct net_device *dev, const char *name,
43 struct net_device **new_dev, int type)
44{
45 struct net_device *ndev;
46 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
47 struct ieee80211_sub_if_data *sdata = NULL;
48 int ret;
49
50 ASSERT_RTNL();
51 ndev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
52 name, ieee80211_if_setup);
53 if (!ndev)
54 return -ENOMEM;
55
56 ret = dev_alloc_name(ndev, ndev->name);
57 if (ret < 0)
58 goto fail;
59
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
67 sdata = IEEE80211_DEV_TO_SUB_IF(ndev);
68 ndev->ieee80211_ptr = &sdata->wdev;
69 sdata->wdev.wiphy = local->hw.wiphy;
70 sdata->type = IEEE80211_IF_TYPE_AP;
71 sdata->dev = ndev;
72 sdata->local = local;
73 ieee80211_if_sdata_init(sdata);
74
75 ret = register_netdevice(ndev);
76 if (ret)
77 goto fail;
78
Jiri Bence9f207f2007-05-05 11:46:38 -070079 ieee80211_debugfs_add_netdev(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -070080 ieee80211_if_set_type(ndev, type);
81
Johannes Berg79010422007-09-18 17:29:21 -040082 /* we're under RTNL so all this is fine */
Jiri Bencf0706e82007-05-05 11:45:53 -070083 if (unlikely(local->reg_state == IEEE80211_DEV_UNREGISTERED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -070084 __ieee80211_if_del(local, sdata);
85 return -ENODEV;
86 }
Johannes Berg79010422007-09-18 17:29:21 -040087 list_add_tail_rcu(&sdata->list, &local->interfaces);
88
Jiri Bencf0706e82007-05-05 11:45:53 -070089 if (new_dev)
90 *new_dev = ndev;
Jiri Bencf0706e82007-05-05 11:45:53 -070091
Jiri Bencf0706e82007-05-05 11:45:53 -070092 return 0;
93
94fail:
95 free_netdev(ndev);
96 return ret;
97}
98
99int ieee80211_if_add_mgmt(struct ieee80211_local *local)
100{
101 struct net_device *ndev;
102 struct ieee80211_sub_if_data *nsdata;
103 int ret;
104
105 ASSERT_RTNL();
106
107 ndev = alloc_netdev(sizeof(struct ieee80211_sub_if_data), "wmgmt%d",
108 ieee80211_if_mgmt_setup);
109 if (!ndev)
110 return -ENOMEM;
111 ret = dev_alloc_name(ndev, ndev->name);
112 if (ret < 0)
113 goto fail;
114
115 memcpy(ndev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
116 SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
117
118 nsdata = IEEE80211_DEV_TO_SUB_IF(ndev);
119 ndev->ieee80211_ptr = &nsdata->wdev;
120 nsdata->wdev.wiphy = local->hw.wiphy;
121 nsdata->type = IEEE80211_IF_TYPE_MGMT;
122 nsdata->dev = ndev;
123 nsdata->local = local;
124 ieee80211_if_sdata_init(nsdata);
125
126 ret = register_netdevice(ndev);
127 if (ret)
128 goto fail;
129
Jiri Bence9f207f2007-05-05 11:46:38 -0700130 ieee80211_debugfs_add_netdev(nsdata);
131
Jiri Bencf0706e82007-05-05 11:45:53 -0700132 if (local->open_count > 0)
133 dev_open(ndev);
134 local->apdev = ndev;
135 return 0;
136
137fail:
138 free_netdev(ndev);
139 return ret;
140}
141
142void ieee80211_if_del_mgmt(struct ieee80211_local *local)
143{
144 struct net_device *apdev;
145
146 ASSERT_RTNL();
147 apdev = local->apdev;
Jiri Bence9f207f2007-05-05 11:46:38 -0700148 ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(apdev));
Jiri Bencf0706e82007-05-05 11:45:53 -0700149 local->apdev = NULL;
150 unregister_netdevice(apdev);
151}
152
153void ieee80211_if_set_type(struct net_device *dev, int type)
154{
155 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bence9f207f2007-05-05 11:46:38 -0700156 int oldtype = sdata->type;
Jiri Bencf0706e82007-05-05 11:45:53 -0700157
Johannes Berg40f7cac2007-07-10 19:32:08 +0200158 dev->hard_start_xmit = ieee80211_subif_start_xmit;
159
Jiri Bencf0706e82007-05-05 11:45:53 -0700160 sdata->type = type;
161 switch (type) {
162 case IEEE80211_IF_TYPE_WDS:
163 sdata->bss = NULL;
164 break;
165 case IEEE80211_IF_TYPE_VLAN:
Johannes Berg0ec3ca442007-09-17 01:29:24 -0400166 sdata->u.vlan.ap = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700167 break;
168 case IEEE80211_IF_TYPE_AP:
169 sdata->u.ap.dtim_period = 2;
170 sdata->u.ap.force_unicast_rateidx = -1;
171 sdata->u.ap.max_ratectrl_rateidx = -1;
172 skb_queue_head_init(&sdata->u.ap.ps_bc_buf);
173 sdata->bss = &sdata->u.ap;
Johannes Berg0ec3ca442007-09-17 01:29:24 -0400174 INIT_LIST_HEAD(&sdata->u.ap.vlans);
Jiri Bencf0706e82007-05-05 11:45:53 -0700175 break;
176 case IEEE80211_IF_TYPE_STA:
177 case IEEE80211_IF_TYPE_IBSS: {
178 struct ieee80211_sub_if_data *msdata;
179 struct ieee80211_if_sta *ifsta;
180
181 ifsta = &sdata->u.sta;
182 INIT_WORK(&ifsta->work, ieee80211_sta_work);
183 setup_timer(&ifsta->timer, ieee80211_sta_timer,
184 (unsigned long) sdata);
185 skb_queue_head_init(&ifsta->skb_queue);
186
187 ifsta->capab = WLAN_CAPABILITY_ESS;
188 ifsta->auth_algs = IEEE80211_AUTH_ALG_OPEN |
189 IEEE80211_AUTH_ALG_SHARED_KEY;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400190 ifsta->flags |= IEEE80211_STA_CREATE_IBSS |
191 IEEE80211_STA_WMM_ENABLED |
192 IEEE80211_STA_AUTO_BSSID_SEL |
193 IEEE80211_STA_AUTO_CHANNEL_SEL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700194
195 msdata = IEEE80211_DEV_TO_SUB_IF(sdata->local->mdev);
196 sdata->bss = &msdata->u.ap;
197 break;
198 }
199 case IEEE80211_IF_TYPE_MNTR:
200 dev->type = ARPHRD_IEEE80211_RADIOTAP;
Johannes Berg40f7cac2007-07-10 19:32:08 +0200201 dev->hard_start_xmit = ieee80211_monitor_start_xmit;
Jiri Bencf0706e82007-05-05 11:45:53 -0700202 break;
203 default:
204 printk(KERN_WARNING "%s: %s: Unknown interface type 0x%x",
205 dev->name, __FUNCTION__, type);
206 }
Jiri Bence9f207f2007-05-05 11:46:38 -0700207 ieee80211_debugfs_change_if_type(sdata, oldtype);
Jiri Bencf0706e82007-05-05 11:45:53 -0700208}
209
210/* Must be called with rtnl lock held. */
211void ieee80211_if_reinit(struct net_device *dev)
212{
213 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
214 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
215 struct sta_info *sta;
Jiri Bencf0706e82007-05-05 11:45:53 -0700216
217 ASSERT_RTNL();
Johannes Berg11a843b2007-08-28 17:01:55 -0400218
219 ieee80211_free_keys(sdata);
220
Jiri Bencf0706e82007-05-05 11:45:53 -0700221 ieee80211_if_sdata_deinit(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -0700222
223 switch (sdata->type) {
224 case IEEE80211_IF_TYPE_AP: {
225 /* Remove all virtual interfaces that use this BSS
226 * as their sdata->bss */
227 struct ieee80211_sub_if_data *tsdata, *n;
Jiri Bencf0706e82007-05-05 11:45:53 -0700228
Johannes Berg79010422007-09-18 17:29:21 -0400229 list_for_each_entry_safe(tsdata, n, &local->interfaces, list) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700230 if (tsdata != sdata && tsdata->bss == &sdata->u.ap) {
231 printk(KERN_DEBUG "%s: removing virtual "
232 "interface %s because its BSS interface"
233 " is being removed\n",
234 sdata->dev->name, tsdata->dev->name);
Johannes Berg79010422007-09-18 17:29:21 -0400235 list_del_rcu(&tsdata->list);
236 /*
237 * We have lots of time and can afford
238 * to sync for each interface
239 */
240 synchronize_rcu();
241 __ieee80211_if_del(local, tsdata);
Jiri Bencf0706e82007-05-05 11:45:53 -0700242 }
243 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700244
245 kfree(sdata->u.ap.beacon_head);
246 kfree(sdata->u.ap.beacon_tail);
247 kfree(sdata->u.ap.generic_elem);
248
249 if (dev != local->mdev) {
250 struct sk_buff *skb;
251 while ((skb = skb_dequeue(&sdata->u.ap.ps_bc_buf))) {
252 local->total_ps_buffered--;
253 dev_kfree_skb(skb);
254 }
255 }
256
257 break;
258 }
259 case IEEE80211_IF_TYPE_WDS:
260 sta = sta_info_get(local, sdata->u.wds.remote_addr);
261 if (sta) {
Michael Wube8755e2007-07-27 15:43:23 +0200262 sta_info_free(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700263 sta_info_put(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700264 } else {
265#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
266 printk(KERN_DEBUG "%s: Someone had deleted my STA "
267 "entry for the WDS link\n", dev->name);
268#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
269 }
270 break;
271 case IEEE80211_IF_TYPE_STA:
272 case IEEE80211_IF_TYPE_IBSS:
273 kfree(sdata->u.sta.extra_ie);
274 sdata->u.sta.extra_ie = NULL;
275 kfree(sdata->u.sta.assocreq_ies);
276 sdata->u.sta.assocreq_ies = NULL;
277 kfree(sdata->u.sta.assocresp_ies);
278 sdata->u.sta.assocresp_ies = NULL;
279 if (sdata->u.sta.probe_resp) {
280 dev_kfree_skb(sdata->u.sta.probe_resp);
281 sdata->u.sta.probe_resp = NULL;
282 }
283
284 break;
285 case IEEE80211_IF_TYPE_MNTR:
286 dev->type = ARPHRD_ETHER;
287 break;
Johannes Berg0ec3ca442007-09-17 01:29:24 -0400288 case IEEE80211_IF_TYPE_VLAN:
289 sdata->u.vlan.ap = NULL;
290 break;
Jiri Bencf0706e82007-05-05 11:45:53 -0700291 }
292
293 /* remove all STAs that are bound to this virtual interface */
294 sta_info_flush(local, dev);
295
296 memset(&sdata->u, 0, sizeof(sdata->u));
297 ieee80211_if_sdata_init(sdata);
298}
299
300/* Must be called with rtnl lock held. */
301void __ieee80211_if_del(struct ieee80211_local *local,
302 struct ieee80211_sub_if_data *sdata)
303{
304 struct net_device *dev = sdata->dev;
305
Jiri Bence9f207f2007-05-05 11:46:38 -0700306 ieee80211_debugfs_remove_netdev(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -0700307 unregister_netdevice(dev);
308 /* Except master interface, the net_device will be freed by
309 * net_device->destructor (i. e. ieee80211_if_free). */
310}
311
312/* Must be called with rtnl lock held. */
313int ieee80211_if_remove(struct net_device *dev, const char *name, int id)
314{
315 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
316 struct ieee80211_sub_if_data *sdata, *n;
317
318 ASSERT_RTNL();
319
Johannes Berg79010422007-09-18 17:29:21 -0400320 list_for_each_entry_safe(sdata, n, &local->interfaces, list) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700321 if ((sdata->type == id || id == -1) &&
322 strcmp(name, sdata->dev->name) == 0 &&
323 sdata->dev != local->mdev) {
Johannes Berg79010422007-09-18 17:29:21 -0400324 list_del_rcu(&sdata->list);
325 synchronize_rcu();
Jiri Bencf0706e82007-05-05 11:45:53 -0700326 __ieee80211_if_del(local, sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -0700327 return 0;
328 }
329 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700330 return -ENODEV;
331}
332
333void ieee80211_if_free(struct net_device *dev)
334{
335 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
336 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
337
338 /* local->apdev must be NULL when freeing management interface */
339 BUG_ON(dev == local->apdev);
340 ieee80211_if_sdata_deinit(sdata);
341 free_netdev(dev);
342}