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"
17 #include "debugfs_netdev.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);
29 INIT_LIST_HEAD(&sdata->key_list);
32 static void ieee80211_if_sdata_deinit(struct ieee80211_sub_if_data *sdata)
36 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
37 __skb_queue_purge(&sdata->fragments[i].skb_list);
41 /* Must be called with rtnl lock held. */
42 int ieee80211_if_add(struct net_device *dev, const char *name,
43 struct net_device **new_dev, int type)
45 struct net_device *ndev;
46 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
47 struct ieee80211_sub_if_data *sdata = NULL;
51 ndev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
52 name, ieee80211_if_setup);
56 ret = dev_alloc_name(ndev, ndev->name);
60 memcpy(ndev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
61 ndev->base_addr = dev->base_addr;
63 ndev->mem_start = dev->mem_start;
64 ndev->mem_end = dev->mem_end;
65 SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
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;
73 ieee80211_if_sdata_init(sdata);
75 ret = register_netdevice(ndev);
79 ieee80211_debugfs_add_netdev(sdata);
80 ieee80211_if_set_type(ndev, type);
82 write_lock_bh(&local->sub_if_lock);
83 if (unlikely(local->reg_state == IEEE80211_DEV_UNREGISTERED)) {
84 write_unlock_bh(&local->sub_if_lock);
85 __ieee80211_if_del(local, sdata);
88 list_add(&sdata->list, &local->sub_if_list);
91 write_unlock_bh(&local->sub_if_lock);
100 int ieee80211_if_add_mgmt(struct ieee80211_local *local)
102 struct net_device *ndev;
103 struct ieee80211_sub_if_data *nsdata;
108 ndev = alloc_netdev(sizeof(struct ieee80211_sub_if_data), "wmgmt%d",
109 ieee80211_if_mgmt_setup);
112 ret = dev_alloc_name(ndev, ndev->name);
116 memcpy(ndev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
117 SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
119 nsdata = IEEE80211_DEV_TO_SUB_IF(ndev);
120 ndev->ieee80211_ptr = &nsdata->wdev;
121 nsdata->wdev.wiphy = local->hw.wiphy;
122 nsdata->type = IEEE80211_IF_TYPE_MGMT;
124 nsdata->local = local;
125 ieee80211_if_sdata_init(nsdata);
127 ret = register_netdevice(ndev);
131 ieee80211_debugfs_add_netdev(nsdata);
133 if (local->open_count > 0)
143 void ieee80211_if_del_mgmt(struct ieee80211_local *local)
145 struct net_device *apdev;
148 apdev = local->apdev;
149 ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(apdev));
151 unregister_netdevice(apdev);
154 void ieee80211_if_set_type(struct net_device *dev, int type)
156 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
157 int oldtype = sdata->type;
159 dev->hard_start_xmit = ieee80211_subif_start_xmit;
163 case IEEE80211_IF_TYPE_WDS:
166 case IEEE80211_IF_TYPE_VLAN:
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;
175 case IEEE80211_IF_TYPE_STA:
176 case IEEE80211_IF_TYPE_IBSS: {
177 struct ieee80211_sub_if_data *msdata;
178 struct ieee80211_if_sta *ifsta;
180 ifsta = &sdata->u.sta;
181 INIT_WORK(&ifsta->work, ieee80211_sta_work);
182 setup_timer(&ifsta->timer, ieee80211_sta_timer,
183 (unsigned long) sdata);
184 skb_queue_head_init(&ifsta->skb_queue);
186 ifsta->capab = WLAN_CAPABILITY_ESS;
187 ifsta->auth_algs = IEEE80211_AUTH_ALG_OPEN |
188 IEEE80211_AUTH_ALG_SHARED_KEY;
189 ifsta->flags |= IEEE80211_STA_CREATE_IBSS |
190 IEEE80211_STA_WMM_ENABLED |
191 IEEE80211_STA_AUTO_BSSID_SEL |
192 IEEE80211_STA_AUTO_CHANNEL_SEL;
194 msdata = IEEE80211_DEV_TO_SUB_IF(sdata->local->mdev);
195 sdata->bss = &msdata->u.ap;
198 case IEEE80211_IF_TYPE_MNTR:
199 dev->type = ARPHRD_IEEE80211_RADIOTAP;
200 dev->hard_start_xmit = ieee80211_monitor_start_xmit;
203 printk(KERN_WARNING "%s: %s: Unknown interface type 0x%x",
204 dev->name, __FUNCTION__, type);
206 ieee80211_debugfs_change_if_type(sdata, oldtype);
209 /* Must be called with rtnl lock held. */
210 void ieee80211_if_reinit(struct net_device *dev)
212 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
213 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
214 struct sta_info *sta;
218 ieee80211_free_keys(sdata);
220 ieee80211_if_sdata_deinit(sdata);
222 switch (sdata->type) {
223 case IEEE80211_IF_TYPE_AP: {
224 /* Remove all virtual interfaces that use this BSS
225 * as their sdata->bss */
226 struct ieee80211_sub_if_data *tsdata, *n;
229 write_lock_bh(&local->sub_if_lock);
230 list_for_each_entry_safe(tsdata, n, &local->sub_if_list, list) {
231 if (tsdata != sdata && tsdata->bss == &sdata->u.ap) {
232 printk(KERN_DEBUG "%s: removing virtual "
233 "interface %s because its BSS interface"
234 " is being removed\n",
235 sdata->dev->name, tsdata->dev->name);
236 list_move_tail(&tsdata->list, &tmp_list);
239 write_unlock_bh(&local->sub_if_lock);
241 list_for_each_entry_safe(tsdata, n, &tmp_list, list)
242 __ieee80211_if_del(local, tsdata);
244 kfree(sdata->u.ap.beacon_head);
245 kfree(sdata->u.ap.beacon_tail);
246 kfree(sdata->u.ap.generic_elem);
248 if (dev != local->mdev) {
250 while ((skb = skb_dequeue(&sdata->u.ap.ps_bc_buf))) {
251 local->total_ps_buffered--;
258 case IEEE80211_IF_TYPE_WDS:
259 sta = sta_info_get(local, sdata->u.wds.remote_addr);
264 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
265 printk(KERN_DEBUG "%s: Someone had deleted my STA "
266 "entry for the WDS link\n", dev->name);
267 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
270 case IEEE80211_IF_TYPE_STA:
271 case IEEE80211_IF_TYPE_IBSS:
272 kfree(sdata->u.sta.extra_ie);
273 sdata->u.sta.extra_ie = NULL;
274 kfree(sdata->u.sta.assocreq_ies);
275 sdata->u.sta.assocreq_ies = NULL;
276 kfree(sdata->u.sta.assocresp_ies);
277 sdata->u.sta.assocresp_ies = NULL;
278 if (sdata->u.sta.probe_resp) {
279 dev_kfree_skb(sdata->u.sta.probe_resp);
280 sdata->u.sta.probe_resp = NULL;
284 case IEEE80211_IF_TYPE_MNTR:
285 dev->type = ARPHRD_ETHER;
289 /* remove all STAs that are bound to this virtual interface */
290 sta_info_flush(local, dev);
292 memset(&sdata->u, 0, sizeof(sdata->u));
293 ieee80211_if_sdata_init(sdata);
296 /* Must be called with rtnl lock held. */
297 void __ieee80211_if_del(struct ieee80211_local *local,
298 struct ieee80211_sub_if_data *sdata)
300 struct net_device *dev = sdata->dev;
302 ieee80211_debugfs_remove_netdev(sdata);
303 unregister_netdevice(dev);
304 /* Except master interface, the net_device will be freed by
305 * net_device->destructor (i. e. ieee80211_if_free). */
308 /* Must be called with rtnl lock held. */
309 int ieee80211_if_remove(struct net_device *dev, const char *name, int id)
311 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
312 struct ieee80211_sub_if_data *sdata, *n;
316 write_lock_bh(&local->sub_if_lock);
317 list_for_each_entry_safe(sdata, n, &local->sub_if_list, list) {
318 if ((sdata->type == id || id == -1) &&
319 strcmp(name, sdata->dev->name) == 0 &&
320 sdata->dev != local->mdev) {
321 list_del(&sdata->list);
322 write_unlock_bh(&local->sub_if_lock);
323 __ieee80211_if_del(local, sdata);
327 write_unlock_bh(&local->sub_if_lock);
331 void ieee80211_if_free(struct net_device *dev)
333 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
334 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
336 /* local->apdev must be NULL when freeing management interface */
337 BUG_ON(dev == local->apdev);
338 ieee80211_if_sdata_deinit(sdata);