blob: c2f92b78bfc913e890ef8646749e0db286ca9fbf [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
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"
Jiri Bence9f207f2007-05-05 11:46:38 -070017#include "debugfs_netdev.h"
Luis Carlos Coboee385852008-02-23 15:17:11 +010018#ifdef CONFIG_MAC80211_MESH
19#include "mesh.h"
20#endif
Jiri Bencf0706e82007-05-05 11:45:53 -070021
22void ieee80211_if_sdata_init(struct ieee80211_sub_if_data *sdata)
23{
24 int i;
25
26 /* Default values for sub-interface parameters */
27 sdata->drop_unencrypted = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -070028 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
29 skb_queue_head_init(&sdata->fragments[i].skb_list);
Johannes Berg11a843b2007-08-28 17:01:55 -040030
31 INIT_LIST_HEAD(&sdata->key_list);
Jiri Bencf0706e82007-05-05 11:45:53 -070032}
33
34static void ieee80211_if_sdata_deinit(struct ieee80211_sub_if_data *sdata)
35{
36 int i;
37
38 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
39 __skb_queue_purge(&sdata->fragments[i].skb_list);
40 }
41}
42
43/* Must be called with rtnl lock held. */
44int ieee80211_if_add(struct net_device *dev, const char *name,
Luis Carlos Coboee385852008-02-23 15:17:11 +010045 struct net_device **new_dev, int type,
46 struct vif_params *params)
Jiri Bencf0706e82007-05-05 11:45:53 -070047{
48 struct net_device *ndev;
49 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
50 struct ieee80211_sub_if_data *sdata = NULL;
51 int ret;
52
53 ASSERT_RTNL();
Johannes Berg32bfd352007-12-19 01:31:26 +010054 ndev = alloc_netdev(sizeof(*sdata) + local->hw.vif_data_size,
Jiri Bencf0706e82007-05-05 11:45:53 -070055 name, ieee80211_if_setup);
56 if (!ndev)
57 return -ENOMEM;
58
59 ret = dev_alloc_name(ndev, ndev->name);
60 if (ret < 0)
61 goto fail;
62
63 memcpy(ndev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
64 ndev->base_addr = dev->base_addr;
65 ndev->irq = dev->irq;
66 ndev->mem_start = dev->mem_start;
67 ndev->mem_end = dev->mem_end;
68 SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
69
70 sdata = IEEE80211_DEV_TO_SUB_IF(ndev);
71 ndev->ieee80211_ptr = &sdata->wdev;
72 sdata->wdev.wiphy = local->hw.wiphy;
Johannes Berg51fb61e2007-12-19 01:31:27 +010073 sdata->vif.type = IEEE80211_IF_TYPE_AP;
Jiri Bencf0706e82007-05-05 11:45:53 -070074 sdata->dev = ndev;
75 sdata->local = local;
76 ieee80211_if_sdata_init(sdata);
77
78 ret = register_netdevice(ndev);
79 if (ret)
80 goto fail;
81
Jiri Bence9f207f2007-05-05 11:46:38 -070082 ieee80211_debugfs_add_netdev(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -070083 ieee80211_if_set_type(ndev, type);
84
Luis Carlos Coboee385852008-02-23 15:17:11 +010085#ifdef CONFIG_MAC80211_MESH
86 if (sdata->vif.type == IEEE80211_IF_TYPE_MESH_POINT &&
87 params && params->mesh_id_len) {
88 sdata->u.sta.mesh_id_len = params->mesh_id_len;
89 memcpy(sdata->u.sta.mesh_id, params->mesh_id,
90 params->mesh_id_len);
91 }
92#endif
93
Johannes Berg79010422007-09-18 17:29:21 -040094 /* we're under RTNL so all this is fine */
Jiri Bencf0706e82007-05-05 11:45:53 -070095 if (unlikely(local->reg_state == IEEE80211_DEV_UNREGISTERED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -070096 __ieee80211_if_del(local, sdata);
97 return -ENODEV;
98 }
Johannes Berg79010422007-09-18 17:29:21 -040099 list_add_tail_rcu(&sdata->list, &local->interfaces);
100
Jiri Bencf0706e82007-05-05 11:45:53 -0700101 if (new_dev)
102 *new_dev = ndev;
Jiri Bencf0706e82007-05-05 11:45:53 -0700103
Jiri Bencf0706e82007-05-05 11:45:53 -0700104 return 0;
105
106fail:
107 free_netdev(ndev);
108 return ret;
109}
110
Jiri Bencf0706e82007-05-05 11:45:53 -0700111void ieee80211_if_set_type(struct net_device *dev, int type)
112{
113 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +0100114 int oldtype = sdata->vif.type;
Jiri Bencf0706e82007-05-05 11:45:53 -0700115
Johannes Berg5b2812e2007-09-26 14:27:23 +0200116 /*
117 * We need to call this function on the master interface
118 * which already has a hard_start_xmit routine assigned
119 * which must not be changed.
120 */
John W. Linville0654ff02007-10-04 14:04:40 -0400121 if (dev != sdata->local->mdev)
Johannes Berg5b2812e2007-09-26 14:27:23 +0200122 dev->hard_start_xmit = ieee80211_subif_start_xmit;
Johannes Berg40f7cac2007-07-10 19:32:08 +0200123
Johannes Berg5b2812e2007-09-26 14:27:23 +0200124 /*
125 * Called even when register_netdevice fails, it would
126 * oops if assigned before initialising the rest.
127 */
128 dev->uninit = ieee80211_if_reinit;
129
130 /* most have no BSS pointer */
131 sdata->bss = NULL;
Johannes Berg51fb61e2007-12-19 01:31:27 +0100132 sdata->vif.type = type;
Johannes Berg5b2812e2007-09-26 14:27:23 +0200133
Johannes Berg8318d782008-01-24 19:38:38 +0100134 sdata->basic_rates = 0;
135
Jiri Bencf0706e82007-05-05 11:45:53 -0700136 switch (type) {
137 case IEEE80211_IF_TYPE_WDS:
Johannes Berg5b2812e2007-09-26 14:27:23 +0200138 /* nothing special */
Jiri Bencf0706e82007-05-05 11:45:53 -0700139 break;
140 case IEEE80211_IF_TYPE_VLAN:
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400141 sdata->u.vlan.ap = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700142 break;
143 case IEEE80211_IF_TYPE_AP:
Jiri Bencf0706e82007-05-05 11:45:53 -0700144 sdata->u.ap.force_unicast_rateidx = -1;
145 sdata->u.ap.max_ratectrl_rateidx = -1;
146 skb_queue_head_init(&sdata->u.ap.ps_bc_buf);
147 sdata->bss = &sdata->u.ap;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400148 INIT_LIST_HEAD(&sdata->u.ap.vlans);
Jiri Bencf0706e82007-05-05 11:45:53 -0700149 break;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100150 case IEEE80211_IF_TYPE_MESH_POINT:
Jiri Bencf0706e82007-05-05 11:45:53 -0700151 case IEEE80211_IF_TYPE_STA:
152 case IEEE80211_IF_TYPE_IBSS: {
153 struct ieee80211_sub_if_data *msdata;
154 struct ieee80211_if_sta *ifsta;
155
156 ifsta = &sdata->u.sta;
157 INIT_WORK(&ifsta->work, ieee80211_sta_work);
158 setup_timer(&ifsta->timer, ieee80211_sta_timer,
159 (unsigned long) sdata);
160 skb_queue_head_init(&ifsta->skb_queue);
161
162 ifsta->capab = WLAN_CAPABILITY_ESS;
163 ifsta->auth_algs = IEEE80211_AUTH_ALG_OPEN |
164 IEEE80211_AUTH_ALG_SHARED_KEY;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400165 ifsta->flags |= IEEE80211_STA_CREATE_IBSS |
166 IEEE80211_STA_WMM_ENABLED |
167 IEEE80211_STA_AUTO_BSSID_SEL |
168 IEEE80211_STA_AUTO_CHANNEL_SEL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700169
170 msdata = IEEE80211_DEV_TO_SUB_IF(sdata->local->mdev);
171 sdata->bss = &msdata->u.ap;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100172
173#ifdef CONFIG_MAC80211_MESH
174 if (type == IEEE80211_IF_TYPE_MESH_POINT) {
175 ifsta->mshcfg.dot11MeshRetryTimeout = MESH_RET_T;
176 ifsta->mshcfg.dot11MeshConfirmTimeout = MESH_CONF_T;
177 ifsta->mshcfg.dot11MeshHoldingTimeout = MESH_HOLD_T;
178 ifsta->mshcfg.dot11MeshMaxRetries = MESH_MAX_RETR;
179 ifsta->mshcfg.dot11MeshTTL = MESH_TTL;
180 ifsta->mshcfg.auto_open_plinks = true;
181 ifsta->mshcfg.dot11MeshMaxPeerLinks =
182 MESH_MAX_ESTAB_PLINKS;
183 ifsta->mshcfg.dot11MeshHWMPactivePathTimeout =
184 MESH_PATH_TIMEOUT;
185 ifsta->mshcfg.dot11MeshHWMPpreqMinInterval =
186 MESH_PREQ_MIN_INT;
187 ifsta->mshcfg.dot11MeshHWMPnetDiameterTraversalTime =
188 MESH_DIAM_TRAVERSAL_TIME;
189 ifsta->mshcfg.dot11MeshHWMPmaxPREQretries =
190 MESH_MAX_PREQ_RETRIES;
191 ifsta->mshcfg.path_refresh_time =
192 MESH_PATH_REFRESH_TIME;
193 ifsta->mshcfg.min_discovery_timeout =
194 MESH_MIN_DISCOVERY_TIMEOUT;
195 ifsta->accepting_plinks = true;
196 ifsta->preq_id = 0;
197 ifsta->dsn = 0;
198 atomic_set(&ifsta->mpaths, 0);
199 mesh_rmc_init(dev);
200 ifsta->last_preq = jiffies;
201 /* Allocate all mesh structures when creating the first
202 * mesh interface.
203 */
204 if (!mesh_allocated)
205 ieee80211s_init();
206 mesh_ids_set_default(ifsta);
207 setup_timer(&ifsta->mesh_path_timer,
208 ieee80211_mesh_path_timer,
209 (unsigned long) sdata);
210 INIT_LIST_HEAD(&ifsta->preq_queue.list);
211 spin_lock_init(&ifsta->mesh_preq_queue_lock);
212 }
213#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700214 break;
215 }
216 case IEEE80211_IF_TYPE_MNTR:
217 dev->type = ARPHRD_IEEE80211_RADIOTAP;
Johannes Berg40f7cac2007-07-10 19:32:08 +0200218 dev->hard_start_xmit = ieee80211_monitor_start_xmit;
Michael Wu8cc9a732008-01-31 19:48:23 +0100219 sdata->u.mntr_flags = MONITOR_FLAG_CONTROL |
220 MONITOR_FLAG_OTHER_BSS;
Jiri Bencf0706e82007-05-05 11:45:53 -0700221 break;
222 default:
223 printk(KERN_WARNING "%s: %s: Unknown interface type 0x%x",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800224 dev->name, __func__, type);
Jiri Bencf0706e82007-05-05 11:45:53 -0700225 }
Jiri Bence9f207f2007-05-05 11:46:38 -0700226 ieee80211_debugfs_change_if_type(sdata, oldtype);
Jiri Bencf0706e82007-05-05 11:45:53 -0700227}
228
229/* Must be called with rtnl lock held. */
230void ieee80211_if_reinit(struct net_device *dev)
231{
232 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
233 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
234 struct sta_info *sta;
Johannes Berg5b2812e2007-09-26 14:27:23 +0200235 struct sk_buff *skb;
Jiri Bencf0706e82007-05-05 11:45:53 -0700236
237 ASSERT_RTNL();
Johannes Berg11a843b2007-08-28 17:01:55 -0400238
239 ieee80211_free_keys(sdata);
240
Jiri Bencf0706e82007-05-05 11:45:53 -0700241 ieee80211_if_sdata_deinit(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -0700242
Johannes Berg51fb61e2007-12-19 01:31:27 +0100243 switch (sdata->vif.type) {
Johannes Berga2897552007-09-28 14:01:25 +0200244 case IEEE80211_IF_TYPE_INVALID:
245 /* cannot happen */
246 WARN_ON(1);
247 break;
Jiri Bencf0706e82007-05-05 11:45:53 -0700248 case IEEE80211_IF_TYPE_AP: {
249 /* Remove all virtual interfaces that use this BSS
250 * as their sdata->bss */
251 struct ieee80211_sub_if_data *tsdata, *n;
Johannes Berge6a5ddf2008-02-25 16:27:42 +0100252 struct beacon_data *beacon;
Jiri Bencf0706e82007-05-05 11:45:53 -0700253
Johannes Berg79010422007-09-18 17:29:21 -0400254 list_for_each_entry_safe(tsdata, n, &local->interfaces, list) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700255 if (tsdata != sdata && tsdata->bss == &sdata->u.ap) {
256 printk(KERN_DEBUG "%s: removing virtual "
257 "interface %s because its BSS interface"
258 " is being removed\n",
259 sdata->dev->name, tsdata->dev->name);
Johannes Berg79010422007-09-18 17:29:21 -0400260 list_del_rcu(&tsdata->list);
261 /*
262 * We have lots of time and can afford
263 * to sync for each interface
264 */
265 synchronize_rcu();
266 __ieee80211_if_del(local, tsdata);
Jiri Bencf0706e82007-05-05 11:45:53 -0700267 }
268 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700269
Johannes Berge6a5ddf2008-02-25 16:27:42 +0100270 beacon = sdata->u.ap.beacon;
271 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
272 synchronize_rcu();
273 kfree(beacon);
Jiri Bencf0706e82007-05-05 11:45:53 -0700274
Johannes Berg5b2812e2007-09-26 14:27:23 +0200275 while ((skb = skb_dequeue(&sdata->u.ap.ps_bc_buf))) {
276 local->total_ps_buffered--;
277 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700278 }
279
280 break;
281 }
282 case IEEE80211_IF_TYPE_WDS:
283 sta = sta_info_get(local, sdata->u.wds.remote_addr);
284 if (sta) {
Michael Wube8755e2007-07-27 15:43:23 +0200285 sta_info_free(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700286 sta_info_put(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700287 } else {
288#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
289 printk(KERN_DEBUG "%s: Someone had deleted my STA "
290 "entry for the WDS link\n", dev->name);
291#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
292 }
293 break;
Johannes Berg6032f932008-02-23 15:17:07 +0100294 case IEEE80211_IF_TYPE_MESH_POINT:
Luis Carlos Coboee385852008-02-23 15:17:11 +0100295#ifdef CONFIG_MAC80211_MESH
296 mesh_rmc_free(dev);
297#endif
298 /* fall through */
Jiri Bencf0706e82007-05-05 11:45:53 -0700299 case IEEE80211_IF_TYPE_STA:
300 case IEEE80211_IF_TYPE_IBSS:
301 kfree(sdata->u.sta.extra_ie);
302 sdata->u.sta.extra_ie = NULL;
303 kfree(sdata->u.sta.assocreq_ies);
304 sdata->u.sta.assocreq_ies = NULL;
305 kfree(sdata->u.sta.assocresp_ies);
306 sdata->u.sta.assocresp_ies = NULL;
307 if (sdata->u.sta.probe_resp) {
308 dev_kfree_skb(sdata->u.sta.probe_resp);
309 sdata->u.sta.probe_resp = NULL;
310 }
311
312 break;
313 case IEEE80211_IF_TYPE_MNTR:
314 dev->type = ARPHRD_ETHER;
315 break;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400316 case IEEE80211_IF_TYPE_VLAN:
317 sdata->u.vlan.ap = NULL;
318 break;
Jiri Bencf0706e82007-05-05 11:45:53 -0700319 }
320
321 /* remove all STAs that are bound to this virtual interface */
322 sta_info_flush(local, dev);
323
324 memset(&sdata->u, 0, sizeof(sdata->u));
325 ieee80211_if_sdata_init(sdata);
326}
327
328/* Must be called with rtnl lock held. */
329void __ieee80211_if_del(struct ieee80211_local *local,
330 struct ieee80211_sub_if_data *sdata)
331{
332 struct net_device *dev = sdata->dev;
333
Jiri Bence9f207f2007-05-05 11:46:38 -0700334 ieee80211_debugfs_remove_netdev(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -0700335 unregister_netdevice(dev);
336 /* Except master interface, the net_device will be freed by
337 * net_device->destructor (i. e. ieee80211_if_free). */
338}
339
340/* Must be called with rtnl lock held. */
341int ieee80211_if_remove(struct net_device *dev, const char *name, int id)
342{
343 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
344 struct ieee80211_sub_if_data *sdata, *n;
345
346 ASSERT_RTNL();
347
Johannes Berg79010422007-09-18 17:29:21 -0400348 list_for_each_entry_safe(sdata, n, &local->interfaces, list) {
Johannes Berg51fb61e2007-12-19 01:31:27 +0100349 if ((sdata->vif.type == id || id == -1) &&
Jiri Bencf0706e82007-05-05 11:45:53 -0700350 strcmp(name, sdata->dev->name) == 0 &&
351 sdata->dev != local->mdev) {
Johannes Berg79010422007-09-18 17:29:21 -0400352 list_del_rcu(&sdata->list);
353 synchronize_rcu();
Jiri Bencf0706e82007-05-05 11:45:53 -0700354 __ieee80211_if_del(local, sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -0700355 return 0;
356 }
357 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700358 return -ENODEV;
359}
360
361void ieee80211_if_free(struct net_device *dev)
362{
Jiri Bencf0706e82007-05-05 11:45:53 -0700363 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
364
Jiri Bencf0706e82007-05-05 11:45:53 -0700365 ieee80211_if_sdata_deinit(sdata);
366 free_netdev(dev);
367}