2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
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.
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 <net/d80211_mgmt.h>
16 #include "ieee80211_i.h"
19 void ieee80211_if_sdata_init(struct ieee80211_sub_if_data
*sdata
)
23 /* Default values for sub-interface parameters */
24 sdata
->drop_unencrypted
= 0;
26 for (i
= 0; i
< IEEE80211_FRAGMENT_MAX
; i
++)
27 skb_queue_head_init(&sdata
->fragments
[i
].skb_list
);
30 static void ieee80211_if_sdata_deinit(struct ieee80211_sub_if_data
*sdata
)
34 for (i
= 0; i
< IEEE80211_FRAGMENT_MAX
; i
++) {
35 __skb_queue_purge(&sdata
->fragments
[i
].skb_list
);
39 /* Must be called with rtnl lock held. */
40 int ieee80211_if_add(struct net_device
*dev
, const char *name
,
41 int format
, struct net_device
**new_dev
)
43 struct net_device
*ndev
, *tmp_dev
;
44 struct ieee80211_local
*local
= dev
->ieee80211_ptr
;
45 struct ieee80211_sub_if_data
*sdata
= NULL
, *sdata_parent
;
50 ndev
= *new_dev
= alloc_netdev(sizeof(struct ieee80211_sub_if_data
),
51 "", ieee80211_if_setup
);
55 ndev
->ieee80211_ptr
= local
;
56 if (strlen(name
) == 0) {
59 sprintf(ndev
->name
, "%s.%d", dev
->name
, i
++);
60 tmp_dev
= dev_get_by_name(ndev
->name
);
66 ret
= dev_alloc_name(ndev
, name
);
70 snprintf(ndev
->name
, IFNAMSIZ
, "%s", name
);
73 memcpy(ndev
->dev_addr
, local
->hw
.perm_addr
, ETH_ALEN
);
74 ndev
->base_addr
= dev
->base_addr
;
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
);
81 sdata
= IEEE80211_DEV_TO_SUB_IF(ndev
);
82 sdata
->type
= IEEE80211_IF_TYPE_AP
;
85 sdata_parent
= IEEE80211_DEV_TO_SUB_IF(dev
);
86 ieee80211_if_sdata_init(sdata
);
88 ret
= register_netdevice(ndev
);
91 ret
= ieee80211_sysfs_add_netdevice(ndev
);
93 /* ndev will be freed by ndev->destructor */
94 unregister_netdevice(ndev
);
99 list_add(&sdata
->list
, &local
->sub_if_list
);
109 int ieee80211_if_add_mgmt(struct ieee80211_local
*local
)
111 struct net_device
*ndev
;
112 struct ieee80211_sub_if_data
*nsdata
;
117 ndev
= alloc_netdev(sizeof(struct ieee80211_sub_if_data
), "",
118 ieee80211_if_mgmt_setup
);
121 ret
= dev_alloc_name(ndev
, "wmgmt%d");
125 ndev
->ieee80211_ptr
= local
;
126 memcpy(ndev
->dev_addr
, local
->hw
.perm_addr
, ETH_ALEN
);
127 SET_NETDEV_DEV(ndev
, local
->hw
.dev
);
129 nsdata
= IEEE80211_DEV_TO_SUB_IF(ndev
);
130 nsdata
->type
= IEEE80211_IF_TYPE_MGMT
;
132 nsdata
->local
= local
;
133 ieee80211_if_sdata_init(nsdata
);
135 ret
= register_netdevice(ndev
);
138 ret
= ieee80211_sysfs_add_netdevice(ndev
);
141 if (local
->open_count
> 0)
147 unregister_netdevice(ndev
);
153 void ieee80211_if_del_mgmt(struct ieee80211_local
*local
)
155 struct net_device
*apdev
;
158 apdev
= local
->apdev
;
159 ieee80211_sysfs_remove_netdevice(apdev
);
161 unregister_netdevice(apdev
);
164 void ieee80211_if_set_type(struct net_device
*dev
, int type
)
166 struct ieee80211_sub_if_data
*sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
170 case IEEE80211_IF_TYPE_WDS
:
173 case IEEE80211_IF_TYPE_VLAN
:
175 case IEEE80211_IF_TYPE_AP
:
176 sdata
->u
.ap
.dtim_period
= 2;
177 sdata
->u
.ap
.force_unicast_rateidx
= -1;
178 sdata
->u
.ap
.max_ratectrl_rateidx
= -1;
179 skb_queue_head_init(&sdata
->u
.ap
.ps_bc_buf
);
180 sdata
->bss
= &sdata
->u
.ap
;
182 case IEEE80211_IF_TYPE_STA
:
183 case IEEE80211_IF_TYPE_IBSS
: {
184 struct ieee80211_sub_if_data
*msdata
;
185 struct ieee80211_if_sta
*ifsta
;
187 ifsta
= &sdata
->u
.sta
;
188 INIT_WORK(&ifsta
->work
, ieee80211_sta_work
, dev
);
190 ifsta
->capab
= WLAN_CAPABILITY_ESS
;
191 ifsta
->auth_algs
= IEEE80211_AUTH_ALG_OPEN
|
192 IEEE80211_AUTH_ALG_SHARED_KEY
;
193 ifsta
->create_ibss
= 1;
194 ifsta
->wmm_enabled
= 1;
196 msdata
= IEEE80211_DEV_TO_SUB_IF(sdata
->local
->mdev
);
197 sdata
->bss
= &msdata
->u
.ap
;
200 case IEEE80211_IF_TYPE_MNTR
:
201 dev
->type
= ARPHRD_IEEE80211_PRISM
;
204 printk(KERN_WARNING
"%s: %s: Unknown interface type 0x%x",
205 dev
->name
, __FUNCTION__
, type
);
207 ieee80211_sysfs_change_if_type(dev
);
210 /* Must be called with rtnl lock held. */
211 void ieee80211_if_reinit(struct net_device
*dev
)
213 struct ieee80211_local
*local
= dev
->ieee80211_ptr
;
214 struct ieee80211_sub_if_data
*sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
215 struct sta_info
*sta
;
219 ieee80211_if_sdata_deinit(sdata
);
220 for (i
= 0; i
< NUM_DEFAULT_KEYS
; i
++) {
224 /* The interface is down at the moment, so there is not
225 * really much point in disabling the keys at this point. */
226 memset(addr
, 0xff, ETH_ALEN
);
227 if (local
->ops
->set_key
)
228 local
->ops
->set_key(dev
, DISABLE_KEY
, addr
,
231 ieee80211_key_free(sdata
->keys
[i
]);
234 /* Shouldn't be necessary but won't hurt */
235 ieee80211_if_shutdown(dev
);
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
;
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
);
253 kfree(sdata
->u
.ap
.beacon_head
);
254 kfree(sdata
->u
.ap
.beacon_tail
);
255 kfree(sdata
->u
.ap
.generic_elem
);
257 if (dev
!= local
->mdev
) {
259 while ((skb
= skb_dequeue(&sdata
->u
.ap
.ps_bc_buf
))) {
260 local
->total_ps_buffered
--;
267 case IEEE80211_IF_TYPE_WDS
:
268 sta
= sta_info_get(local
, sdata
->u
.wds
.remote_addr
);
271 sta_info_free(sta
, 0);
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 */
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
;
293 case IEEE80211_IF_TYPE_MNTR
:
294 dev
->type
= ARPHRD_ETHER
;
298 /* remove all STAs that are bound to this virtual interface */
299 sta_info_flush(local
, dev
);
301 memset(&sdata
->u
, 0, sizeof(sdata
->u
));
302 ieee80211_if_sdata_init(sdata
);
305 /* Must be called with rtnl lock held. */
306 void __ieee80211_if_del(struct ieee80211_local
*local
,
307 struct ieee80211_sub_if_data
*sdata
)
309 struct net_device
*dev
= sdata
->dev
;
311 ieee80211_if_reinit(dev
);
312 list_del(&sdata
->list
);
313 ieee80211_sysfs_remove_netdevice(dev
);
314 unregister_netdevice(dev
);
315 /* Except master interface, the net_device will be freed by
316 * net_device->destructor (i. e. ieee80211_if_free). */
319 /* Must be called with rtnl lock held. */
320 int ieee80211_if_remove(struct net_device
*dev
, const char *name
, int id
)
322 struct ieee80211_local
*local
= dev
->ieee80211_ptr
;
323 struct ieee80211_sub_if_data
*sdata
, *n
;
327 list_for_each_entry_safe(sdata
, n
, &local
->sub_if_list
, list
) {
328 if ((sdata
->type
== id
|| id
== -1) &&
329 strcmp(name
, sdata
->dev
->name
) == 0 &&
330 sdata
->dev
!= local
->mdev
) {
331 __ieee80211_if_del(local
, sdata
);
338 void ieee80211_if_free(struct net_device
*dev
)
340 struct ieee80211_local
*local
= dev
->ieee80211_ptr
;
341 struct ieee80211_sub_if_data
*sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
343 /* local->apdev must be NULL when freeing management interface */
344 BUG_ON(dev
== local
->apdev
);
345 ieee80211_if_sdata_deinit(sdata
);
349 /* Must be called with rtnl lock held. */
350 void ieee80211_if_flush(struct net_device
*dev
)
352 struct ieee80211_local
*local
= dev
->ieee80211_ptr
;
353 struct ieee80211_sub_if_data
*sdata
, *n
;
356 list_for_each_entry_safe(sdata
, n
, &local
->sub_if_list
, list
) {
357 __ieee80211_if_del(local
, sdata
);
361 void ieee80211_if_del(struct net_device
*dev
)
363 struct ieee80211_local
*local
= dev
->ieee80211_ptr
;
364 struct ieee80211_sub_if_data
*sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
367 if (sdata
->type
== IEEE80211_IF_TYPE_MGMT
)
368 ieee80211_if_del_mgmt(local
);
370 __ieee80211_if_del(local
, sdata
);