fix typo in UnistallDev
[openwrt.git] / package / d80211 / src / ieee80211_iface.c
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/d80211.h>
15 #include "ieee80211_i.h"
16 #include "sta_info.h"
17
18 void ieee80211_if_sdata_init(struct ieee80211_sub_if_data *sdata)
19 {
20 int i;
21
22 /* Default values for sub-interface parameters */
23 sdata->drop_unencrypted = 0;
24 sdata->eapol = 1;
25 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
26 skb_queue_head_init(&sdata->fragments[i].skb_list);
27 }
28
29 static void ieee80211_if_sdata_deinit(struct ieee80211_sub_if_data *sdata)
30 {
31 int i;
32
33 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
34 __skb_queue_purge(&sdata->fragments[i].skb_list);
35 }
36 }
37
38 /* Must be called with rtnl lock held. */
39 int ieee80211_if_add(struct net_device *dev, const char *name,
40 int format, struct net_device **new_dev)
41 {
42 struct net_device *ndev, *tmp_dev;
43 struct ieee80211_local *local = dev->ieee80211_ptr;
44 struct ieee80211_sub_if_data *sdata = NULL;
45 int ret;
46 int i;
47
48 ASSERT_RTNL();
49 ndev = *new_dev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
50 "", ieee80211_if_setup);
51 if (!ndev)
52 return -ENOMEM;
53
54 ndev->ieee80211_ptr = local;
55 if (strlen(name) == 0) {
56 i = 0;
57 do {
58 snprintf(ndev->name, sizeof(ndev->name), "%s.%d",
59 dev->name, i++);
60 tmp_dev = dev_get_by_name(ndev->name);
61 if (!tmp_dev)
62 break;
63 dev_put(tmp_dev);
64 } while (i < 10000);
65 } else if (format) {
66 ret = dev_alloc_name(ndev, name);
67 if (ret < 0)
68 goto fail;
69 } else {
70 snprintf(ndev->name, IFNAMSIZ, "%s", name);
71 }
72
73 memcpy(ndev->dev_addr, local->hw.perm_addr, ETH_ALEN);
74 ndev->base_addr = dev->base_addr;
75 ndev->irq = dev->irq;
76 ndev->mem_start = dev->mem_start;
77 ndev->mem_end = dev->mem_end;
78 ndev->flags = dev->flags & IFF_MULTICAST;
79 SET_NETDEV_DEV(ndev, local->hw.dev);
80
81 sdata = IEEE80211_DEV_TO_SUB_IF(ndev);
82 sdata->type = IEEE80211_IF_TYPE_AP;
83 sdata->dev = ndev;
84 sdata->local = local;
85 ieee80211_if_sdata_init(sdata);
86
87 ret = register_netdevice(ndev);
88 if (ret)
89 goto fail;
90 ret = ieee80211_sysfs_add_netdevice(ndev);
91 if (ret) {
92 /* ndev will be freed by ndev->destructor */
93 unregister_netdevice(ndev);
94 *new_dev = NULL;
95 return ret;
96 }
97
98 list_add(&sdata->list, &local->sub_if_list);
99 ieee80211_update_default_wep_only(local);
100
101 return 0;
102
103 fail:
104 free_netdev(ndev);
105 *new_dev = NULL;
106 return ret;
107 }
108
109 int ieee80211_if_add_mgmt(struct ieee80211_local *local)
110 {
111 struct net_device *ndev;
112 struct ieee80211_sub_if_data *nsdata;
113 int ret;
114
115 ASSERT_RTNL();
116
117 ndev = alloc_netdev(sizeof(struct ieee80211_sub_if_data), "",
118 ieee80211_if_mgmt_setup);
119 if (!ndev)
120 return -ENOMEM;
121 ret = dev_alloc_name(ndev, "wmgmt%d");
122 if (ret < 0)
123 goto fail;
124
125 ndev->ieee80211_ptr = local;
126 memcpy(ndev->dev_addr, local->hw.perm_addr, ETH_ALEN);
127 SET_NETDEV_DEV(ndev, local->hw.dev);
128
129 nsdata = IEEE80211_DEV_TO_SUB_IF(ndev);
130 nsdata->type = IEEE80211_IF_TYPE_MGMT;
131 nsdata->dev = ndev;
132 nsdata->local = local;
133 ieee80211_if_sdata_init(nsdata);
134
135 ret = register_netdevice(ndev);
136 if (ret)
137 goto fail;
138 ret = ieee80211_sysfs_add_netdevice(ndev);
139 if (ret)
140 goto fail_sysfs;
141 if (local->open_count > 0)
142 dev_open(ndev);
143 local->apdev = ndev;
144 return 0;
145
146 fail_sysfs:
147 unregister_netdevice(ndev);
148 fail:
149 free_netdev(ndev);
150 return ret;
151 }
152
153 void ieee80211_if_del_mgmt(struct ieee80211_local *local)
154 {
155 struct net_device *apdev;
156
157 ASSERT_RTNL();
158 apdev = local->apdev;
159 ieee80211_sysfs_remove_netdevice(apdev);
160 local->apdev = NULL;
161 unregister_netdevice(apdev);
162 }
163
164 void ieee80211_if_set_type(struct net_device *dev, int type)
165 {
166 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
167 struct ieee80211_local *local = dev->ieee80211_ptr;
168
169 sdata->type = type;
170 switch (type) {
171 case IEEE80211_IF_TYPE_WDS:
172 sdata->bss = NULL;
173 break;
174 case IEEE80211_IF_TYPE_VLAN:
175 break;
176 case IEEE80211_IF_TYPE_AP:
177 sdata->u.ap.dtim_period = 2;
178 sdata->u.ap.force_unicast_rateidx = -1;
179 sdata->u.ap.max_ratectrl_rateidx = -1;
180 skb_queue_head_init(&sdata->u.ap.ps_bc_buf);
181 sdata->bss = &sdata->u.ap;
182 break;
183 case IEEE80211_IF_TYPE_STA:
184 case IEEE80211_IF_TYPE_IBSS: {
185 struct ieee80211_sub_if_data *msdata;
186 struct ieee80211_if_sta *ifsta;
187
188 ifsta = &sdata->u.sta;
189 INIT_DELAYED_WORK(&ifsta->work, ieee80211_sta_work);
190
191 ifsta->capab = WLAN_CAPABILITY_ESS;
192 ifsta->auth_algs = IEEE80211_AUTH_ALG_OPEN |
193 IEEE80211_AUTH_ALG_SHARED_KEY;
194 ifsta->create_ibss = 1;
195 ifsta->wmm_enabled = 1;
196
197 msdata = IEEE80211_DEV_TO_SUB_IF(sdata->local->mdev);
198 sdata->bss = &msdata->u.ap;
199 break;
200 }
201 case IEEE80211_IF_TYPE_MNTR:
202 dev->type = ARPHRD_IEEE80211_PRISM;
203 break;
204 default:
205 printk(KERN_WARNING "%s: %s: Unknown interface type 0x%x",
206 dev->name, __FUNCTION__, type);
207 }
208 ieee80211_sysfs_change_if_type(dev);
209 ieee80211_update_default_wep_only(local);
210 }
211
212 /* Must be called with rtnl lock held. */
213 void ieee80211_if_reinit(struct net_device *dev)
214 {
215 struct ieee80211_local *local = dev->ieee80211_ptr;
216 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
217 struct sta_info *sta;
218 int i;
219
220 ASSERT_RTNL();
221 ieee80211_if_sdata_deinit(sdata);
222 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
223 if (!sdata->keys[i])
224 continue;
225 #if 0
226 /* The interface is down at the moment, so there is not
227 * really much point in disabling the keys at this point. */
228 memset(addr, 0xff, ETH_ALEN);
229 if (local->ops->set_key)
230 local->ops->set_key(local_to_hw(local), DISABLE_KEY, addr,
231 local->keys[i], 0);
232 #endif
233 ieee80211_key_free(sdata->keys[i]);
234 sdata->keys[i] = NULL;
235 }
236
237 switch (sdata->type) {
238 case IEEE80211_IF_TYPE_AP: {
239 /* Remove all virtual interfaces that use this BSS
240 * as their sdata->bss */
241 struct ieee80211_sub_if_data *tsdata, *n;
242
243 list_for_each_entry_safe(tsdata, n, &local->sub_if_list, list) {
244 if (tsdata != sdata && tsdata->bss == &sdata->u.ap) {
245 printk(KERN_DEBUG "%s: removing virtual "
246 "interface %s because its BSS interface"
247 " is being removed\n",
248 sdata->dev->name, tsdata->dev->name);
249 __ieee80211_if_del(local, tsdata);
250 }
251 }
252
253 kfree(sdata->u.ap.beacon_head);
254 kfree(sdata->u.ap.beacon_tail);
255 kfree(sdata->u.ap.generic_elem);
256
257 if (dev != local->mdev) {
258 struct sk_buff *skb;
259 while ((skb = skb_dequeue(&sdata->u.ap.ps_bc_buf))) {
260 local->total_ps_buffered--;
261 dev_kfree_skb(skb);
262 }
263 }
264
265 break;
266 }
267 case IEEE80211_IF_TYPE_WDS:
268 sta = sta_info_get(local, sdata->u.wds.remote_addr);
269 if (sta) {
270 sta_info_put(sta);
271 sta_info_free(sta, 0);
272 } else {
273 #ifdef CONFIG_D80211_VERBOSE_DEBUG
274 printk(KERN_DEBUG "%s: Someone had deleted my STA "
275 "entry for the WDS link\n", dev->name);
276 #endif /* CONFIG_D80211_VERBOSE_DEBUG */
277 }
278 break;
279 case IEEE80211_IF_TYPE_STA:
280 case IEEE80211_IF_TYPE_IBSS:
281 kfree(sdata->u.sta.extra_ie);
282 sdata->u.sta.extra_ie = NULL;
283 kfree(sdata->u.sta.assocreq_ies);
284 sdata->u.sta.assocreq_ies = NULL;
285 kfree(sdata->u.sta.assocresp_ies);
286 sdata->u.sta.assocresp_ies = NULL;
287 if (sdata->u.sta.probe_resp) {
288 dev_kfree_skb(sdata->u.sta.probe_resp);
289 sdata->u.sta.probe_resp = NULL;
290 }
291
292 break;
293 case IEEE80211_IF_TYPE_MNTR:
294 dev->type = ARPHRD_ETHER;
295 break;
296 }
297
298 /* remove all STAs that are bound to this virtual interface */
299 sta_info_flush(local, dev);
300
301 memset(&sdata->u, 0, sizeof(sdata->u));
302 ieee80211_if_sdata_init(sdata);
303 }
304
305 /* Must be called with rtnl lock held. */
306 void __ieee80211_if_del(struct ieee80211_local *local,
307 struct ieee80211_sub_if_data *sdata)
308 {
309 struct net_device *dev = sdata->dev;
310
311 list_del(&sdata->list);
312 ieee80211_sysfs_remove_netdevice(dev);
313 unregister_netdevice(dev);
314 /* Except master interface, the net_device will be freed by
315 * net_device->destructor (i. e. ieee80211_if_free). */
316 }
317
318 /* Must be called with rtnl lock held. */
319 int ieee80211_if_remove(struct net_device *dev, const char *name, int id)
320 {
321 struct ieee80211_local *local = dev->ieee80211_ptr;
322 struct ieee80211_sub_if_data *sdata, *n;
323
324 ASSERT_RTNL();
325
326 list_for_each_entry_safe(sdata, n, &local->sub_if_list, list) {
327 if ((sdata->type == id || id == -1) &&
328 strcmp(name, sdata->dev->name) == 0 &&
329 sdata->dev != local->mdev) {
330 __ieee80211_if_del(local, sdata);
331 ieee80211_update_default_wep_only(local);
332 return 0;
333 }
334 }
335 return -ENODEV;
336 }
337
338 void ieee80211_if_free(struct net_device *dev)
339 {
340 struct ieee80211_local *local = dev->ieee80211_ptr;
341 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
342
343 /* local->apdev must be NULL when freeing management interface */
344 BUG_ON(dev == local->apdev);
345 ieee80211_if_sdata_deinit(sdata);
346 free_netdev(dev);
347 }
348
349 /* Must be called with rtnl lock held. */
350 void ieee80211_if_flush(struct net_device *dev)
351 {
352 struct ieee80211_local *local = dev->ieee80211_ptr;
353 struct ieee80211_sub_if_data *sdata, *n;
354
355 ASSERT_RTNL();
356 list_for_each_entry_safe(sdata, n, &local->sub_if_list, list) {
357 __ieee80211_if_del(local, sdata);
358 }
359 }
360
361 void ieee80211_if_del(struct net_device *dev)
362 {
363 struct ieee80211_local *local = dev->ieee80211_ptr;
364 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
365
366 rtnl_lock();
367 if (sdata->type == IEEE80211_IF_TYPE_MGMT)
368 ieee80211_if_del_mgmt(local);
369 else
370 __ieee80211_if_del(local, sdata);
371 rtnl_unlock();
372 }
This page took 0.057638 seconds and 5 git commands to generate.