[PATCH] mac80211: fix virtual interface locking
[safe/jmp/linux-2.6] / net / mac80211 / 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/mac80211.h>
15 #include "ieee80211_i.h"
16 #include "sta_info.h"
17 #include "debugfs_netdev.h"
18
19 void 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);
28
29         INIT_LIST_HEAD(&sdata->key_list);
30 }
31
32 static 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. */
42 int 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
79         ieee80211_debugfs_add_netdev(sdata);
80         ieee80211_if_set_type(ndev, type);
81
82         /* we're under RTNL so all this is fine */
83         if (unlikely(local->reg_state == IEEE80211_DEV_UNREGISTERED)) {
84                 __ieee80211_if_del(local, sdata);
85                 return -ENODEV;
86         }
87         list_add_tail_rcu(&sdata->list, &local->interfaces);
88
89         if (new_dev)
90                 *new_dev = ndev;
91
92         return 0;
93
94 fail:
95         free_netdev(ndev);
96         return ret;
97 }
98
99 int 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
130         ieee80211_debugfs_add_netdev(nsdata);
131
132         if (local->open_count > 0)
133                 dev_open(ndev);
134         local->apdev = ndev;
135         return 0;
136
137 fail:
138         free_netdev(ndev);
139         return ret;
140 }
141
142 void ieee80211_if_del_mgmt(struct ieee80211_local *local)
143 {
144         struct net_device *apdev;
145
146         ASSERT_RTNL();
147         apdev = local->apdev;
148         ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(apdev));
149         local->apdev = NULL;
150         unregister_netdevice(apdev);
151 }
152
153 void ieee80211_if_set_type(struct net_device *dev, int type)
154 {
155         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
156         int oldtype = sdata->type;
157
158         dev->hard_start_xmit = ieee80211_subif_start_xmit;
159
160         sdata->type = type;
161         switch (type) {
162         case IEEE80211_IF_TYPE_WDS:
163                 sdata->bss = NULL;
164                 break;
165         case IEEE80211_IF_TYPE_VLAN:
166                 sdata->u.vlan.ap = NULL;
167                 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;
174                 INIT_LIST_HEAD(&sdata->u.ap.vlans);
175                 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;
190                 ifsta->flags |= IEEE80211_STA_CREATE_IBSS |
191                         IEEE80211_STA_WMM_ENABLED |
192                         IEEE80211_STA_AUTO_BSSID_SEL |
193                         IEEE80211_STA_AUTO_CHANNEL_SEL;
194
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;
201                 dev->hard_start_xmit = ieee80211_monitor_start_xmit;
202                 break;
203         default:
204                 printk(KERN_WARNING "%s: %s: Unknown interface type 0x%x",
205                        dev->name, __FUNCTION__, type);
206         }
207         ieee80211_debugfs_change_if_type(sdata, oldtype);
208 }
209
210 /* Must be called with rtnl lock held. */
211 void 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;
216
217         ASSERT_RTNL();
218
219         ieee80211_free_keys(sdata);
220
221         ieee80211_if_sdata_deinit(sdata);
222
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;
228
229                 list_for_each_entry_safe(tsdata, n, &local->interfaces, list) {
230                         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);
235                                 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);
242                         }
243                 }
244
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) {
262                         sta_info_free(sta);
263                         sta_info_put(sta);
264                 } 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;
288         case IEEE80211_IF_TYPE_VLAN:
289                 sdata->u.vlan.ap = NULL;
290                 break;
291         }
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. */
301 void __ieee80211_if_del(struct ieee80211_local *local,
302                         struct ieee80211_sub_if_data *sdata)
303 {
304         struct net_device *dev = sdata->dev;
305
306         ieee80211_debugfs_remove_netdev(sdata);
307         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. */
313 int 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
320         list_for_each_entry_safe(sdata, n, &local->interfaces, list) {
321                 if ((sdata->type == id || id == -1) &&
322                     strcmp(name, sdata->dev->name) == 0 &&
323                     sdata->dev != local->mdev) {
324                         list_del_rcu(&sdata->list);
325                         synchronize_rcu();
326                         __ieee80211_if_del(local, sdata);
327                         return 0;
328                 }
329         }
330         return -ENODEV;
331 }
332
333 void 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 }