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/mac80211.h>
15 #include "ieee80211_i.h"
18 void ieee80211_if_sdata_init(struct ieee80211_sub_if_data
*sdata
)
22 /* Default values for sub-interface parameters */
23 sdata
->drop_unencrypted
= 0;
25 for (i
= 0; i
< IEEE80211_FRAGMENT_MAX
; i
++)
26 skb_queue_head_init(&sdata
->fragments
[i
].skb_list
);
29 static void ieee80211_if_sdata_deinit(struct ieee80211_sub_if_data
*sdata
)
33 for (i
= 0; i
< IEEE80211_FRAGMENT_MAX
; i
++) {
34 __skb_queue_purge(&sdata
->fragments
[i
].skb_list
);
38 /* Must be called with rtnl lock held. */
39 int ieee80211_if_add(struct net_device
*dev
, const char *name
,
40 struct net_device
**new_dev
, int type
)
42 struct net_device
*ndev
;
43 struct ieee80211_local
*local
= wdev_priv(dev
->ieee80211_ptr
);
44 struct ieee80211_sub_if_data
*sdata
= NULL
;
48 ndev
= alloc_netdev(sizeof(struct ieee80211_sub_if_data
),
49 name
, ieee80211_if_setup
);
53 ret
= dev_alloc_name(ndev
, ndev
->name
);
57 memcpy(ndev
->dev_addr
, local
->hw
.wiphy
->perm_addr
, ETH_ALEN
);
58 ndev
->base_addr
= dev
->base_addr
;
60 ndev
->mem_start
= dev
->mem_start
;
61 ndev
->mem_end
= dev
->mem_end
;
62 SET_NETDEV_DEV(ndev
, wiphy_dev(local
->hw
.wiphy
));
64 sdata
= IEEE80211_DEV_TO_SUB_IF(ndev
);
65 ndev
->ieee80211_ptr
= &sdata
->wdev
;
66 sdata
->wdev
.wiphy
= local
->hw
.wiphy
;
67 sdata
->type
= IEEE80211_IF_TYPE_AP
;
70 ieee80211_if_sdata_init(sdata
);
72 ret
= register_netdevice(ndev
);
76 ieee80211_if_set_type(ndev
, type
);
78 write_lock_bh(&local
->sub_if_lock
);
79 if (unlikely(local
->reg_state
== IEEE80211_DEV_UNREGISTERED
)) {
80 write_unlock_bh(&local
->sub_if_lock
);
81 __ieee80211_if_del(local
, sdata
);
84 list_add(&sdata
->list
, &local
->sub_if_list
);
87 write_unlock_bh(&local
->sub_if_lock
);
89 ieee80211_update_default_wep_only(local
);
98 int ieee80211_if_add_mgmt(struct ieee80211_local
*local
)
100 struct net_device
*ndev
;
101 struct ieee80211_sub_if_data
*nsdata
;
106 ndev
= alloc_netdev(sizeof(struct ieee80211_sub_if_data
), "wmgmt%d",
107 ieee80211_if_mgmt_setup
);
110 ret
= dev_alloc_name(ndev
, ndev
->name
);
114 memcpy(ndev
->dev_addr
, local
->hw
.wiphy
->perm_addr
, ETH_ALEN
);
115 SET_NETDEV_DEV(ndev
, wiphy_dev(local
->hw
.wiphy
));
117 nsdata
= IEEE80211_DEV_TO_SUB_IF(ndev
);
118 ndev
->ieee80211_ptr
= &nsdata
->wdev
;
119 nsdata
->wdev
.wiphy
= local
->hw
.wiphy
;
120 nsdata
->type
= IEEE80211_IF_TYPE_MGMT
;
122 nsdata
->local
= local
;
123 ieee80211_if_sdata_init(nsdata
);
125 ret
= register_netdevice(ndev
);
129 if (local
->open_count
> 0)
139 void ieee80211_if_del_mgmt(struct ieee80211_local
*local
)
141 struct net_device
*apdev
;
144 apdev
= local
->apdev
;
146 unregister_netdevice(apdev
);
149 void ieee80211_if_set_type(struct net_device
*dev
, int type
)
151 struct ieee80211_sub_if_data
*sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
152 struct ieee80211_local
*local
= wdev_priv(dev
->ieee80211_ptr
);
156 case IEEE80211_IF_TYPE_WDS
:
159 case IEEE80211_IF_TYPE_VLAN
:
161 case IEEE80211_IF_TYPE_AP
:
162 sdata
->u
.ap
.dtim_period
= 2;
163 sdata
->u
.ap
.force_unicast_rateidx
= -1;
164 sdata
->u
.ap
.max_ratectrl_rateidx
= -1;
165 skb_queue_head_init(&sdata
->u
.ap
.ps_bc_buf
);
166 sdata
->bss
= &sdata
->u
.ap
;
168 case IEEE80211_IF_TYPE_STA
:
169 case IEEE80211_IF_TYPE_IBSS
: {
170 struct ieee80211_sub_if_data
*msdata
;
171 struct ieee80211_if_sta
*ifsta
;
173 ifsta
= &sdata
->u
.sta
;
174 INIT_WORK(&ifsta
->work
, ieee80211_sta_work
);
175 setup_timer(&ifsta
->timer
, ieee80211_sta_timer
,
176 (unsigned long) sdata
);
177 skb_queue_head_init(&ifsta
->skb_queue
);
179 ifsta
->capab
= WLAN_CAPABILITY_ESS
;
180 ifsta
->auth_algs
= IEEE80211_AUTH_ALG_OPEN
|
181 IEEE80211_AUTH_ALG_SHARED_KEY
;
182 ifsta
->create_ibss
= 1;
183 ifsta
->wmm_enabled
= 1;
184 ifsta
->auto_channel_sel
= 1;
185 ifsta
->auto_bssid_sel
= 1;
187 msdata
= IEEE80211_DEV_TO_SUB_IF(sdata
->local
->mdev
);
188 sdata
->bss
= &msdata
->u
.ap
;
191 case IEEE80211_IF_TYPE_MNTR
:
192 dev
->type
= ARPHRD_IEEE80211_RADIOTAP
;
195 printk(KERN_WARNING
"%s: %s: Unknown interface type 0x%x",
196 dev
->name
, __FUNCTION__
, type
);
198 ieee80211_update_default_wep_only(local
);
201 /* Must be called with rtnl lock held. */
202 void ieee80211_if_reinit(struct net_device
*dev
)
204 struct ieee80211_local
*local
= wdev_priv(dev
->ieee80211_ptr
);
205 struct ieee80211_sub_if_data
*sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
206 struct sta_info
*sta
;
210 ieee80211_if_sdata_deinit(sdata
);
211 for (i
= 0; i
< NUM_DEFAULT_KEYS
; i
++) {
215 /* The interface is down at the moment, so there is not
216 * really much point in disabling the keys at this point. */
217 memset(addr
, 0xff, ETH_ALEN
);
218 if (local
->ops
->set_key
)
219 local
->ops
->set_key(local_to_hw(local
), DISABLE_KEY
, addr
,
222 ieee80211_key_free(sdata
->keys
[i
]);
223 sdata
->keys
[i
] = NULL
;
226 switch (sdata
->type
) {
227 case IEEE80211_IF_TYPE_AP
: {
228 /* Remove all virtual interfaces that use this BSS
229 * as their sdata->bss */
230 struct ieee80211_sub_if_data
*tsdata
, *n
;
233 write_lock_bh(&local
->sub_if_lock
);
234 list_for_each_entry_safe(tsdata
, n
, &local
->sub_if_list
, list
) {
235 if (tsdata
!= sdata
&& tsdata
->bss
== &sdata
->u
.ap
) {
236 printk(KERN_DEBUG
"%s: removing virtual "
237 "interface %s because its BSS interface"
238 " is being removed\n",
239 sdata
->dev
->name
, tsdata
->dev
->name
);
240 list_move_tail(&tsdata
->list
, &tmp_list
);
243 write_unlock_bh(&local
->sub_if_lock
);
245 list_for_each_entry_safe(tsdata
, n
, &tmp_list
, list
)
246 __ieee80211_if_del(local
, tsdata
);
248 kfree(sdata
->u
.ap
.beacon_head
);
249 kfree(sdata
->u
.ap
.beacon_tail
);
250 kfree(sdata
->u
.ap
.generic_elem
);
252 if (dev
!= local
->mdev
) {
254 while ((skb
= skb_dequeue(&sdata
->u
.ap
.ps_bc_buf
))) {
255 local
->total_ps_buffered
--;
262 case IEEE80211_IF_TYPE_WDS
:
263 sta
= sta_info_get(local
, sdata
->u
.wds
.remote_addr
);
266 sta_info_free(sta
, 0);
268 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
269 printk(KERN_DEBUG
"%s: Someone had deleted my STA "
270 "entry for the WDS link\n", dev
->name
);
271 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
274 case IEEE80211_IF_TYPE_STA
:
275 case IEEE80211_IF_TYPE_IBSS
:
276 kfree(sdata
->u
.sta
.extra_ie
);
277 sdata
->u
.sta
.extra_ie
= NULL
;
278 kfree(sdata
->u
.sta
.assocreq_ies
);
279 sdata
->u
.sta
.assocreq_ies
= NULL
;
280 kfree(sdata
->u
.sta
.assocresp_ies
);
281 sdata
->u
.sta
.assocresp_ies
= NULL
;
282 if (sdata
->u
.sta
.probe_resp
) {
283 dev_kfree_skb(sdata
->u
.sta
.probe_resp
);
284 sdata
->u
.sta
.probe_resp
= NULL
;
288 case IEEE80211_IF_TYPE_MNTR
:
289 dev
->type
= ARPHRD_ETHER
;
293 /* remove all STAs that are bound to this virtual interface */
294 sta_info_flush(local
, dev
);
296 memset(&sdata
->u
, 0, sizeof(sdata
->u
));
297 ieee80211_if_sdata_init(sdata
);
300 /* Must be called with rtnl lock held. */
301 void __ieee80211_if_del(struct ieee80211_local
*local
,
302 struct ieee80211_sub_if_data
*sdata
)
304 struct net_device
*dev
= sdata
->dev
;
306 unregister_netdevice(dev
);
307 /* Except master interface, the net_device will be freed by
308 * net_device->destructor (i. e. ieee80211_if_free). */
311 /* Must be called with rtnl lock held. */
312 int ieee80211_if_remove(struct net_device
*dev
, const char *name
, int id
)
314 struct ieee80211_local
*local
= wdev_priv(dev
->ieee80211_ptr
);
315 struct ieee80211_sub_if_data
*sdata
, *n
;
319 write_lock_bh(&local
->sub_if_lock
);
320 list_for_each_entry_safe(sdata
, n
, &local
->sub_if_list
, list
) {
321 if ((sdata
->type
== id
|| id
== -1) &&
322 strcmp(name
, sdata
->dev
->name
) == 0 &&
323 sdata
->dev
!= local
->mdev
) {
324 list_del(&sdata
->list
);
325 write_unlock_bh(&local
->sub_if_lock
);
326 __ieee80211_if_del(local
, sdata
);
327 ieee80211_update_default_wep_only(local
);
331 write_unlock_bh(&local
->sub_if_lock
);
335 void ieee80211_if_free(struct net_device
*dev
)
337 struct ieee80211_local
*local
= wdev_priv(dev
->ieee80211_ptr
);
338 struct ieee80211_sub_if_data
*sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
340 /* local->apdev must be NULL when freeing management interface */
341 BUG_ON(dev
== local
->apdev
);
342 ieee80211_if_sdata_deinit(sdata
);