blob: f2aefd4b86372b1f8e54bdc70b3e2ccdd8d6fef2 [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#include "mesh.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070019
20void ieee80211_if_sdata_init(struct ieee80211_sub_if_data *sdata)
21{
22 int i;
23
24 /* Default values for sub-interface parameters */
25 sdata->drop_unencrypted = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -070026 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
27 skb_queue_head_init(&sdata->fragments[i].skb_list);
Johannes Berg11a843b2007-08-28 17:01:55 -040028
29 INIT_LIST_HEAD(&sdata->key_list);
Johannes Berg3e122be2008-07-09 14:40:34 +020030
31 sdata->force_unicast_rateidx = -1;
32 sdata->max_ratectrl_rateidx = -1;
Jiri Bencf0706e82007-05-05 11:45:53 -070033}
34
35static void ieee80211_if_sdata_deinit(struct ieee80211_sub_if_data *sdata)
36{
37 int i;
38
Johannes Berg988c0f72008-04-17 19:21:22 +020039 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
Jiri Bencf0706e82007-05-05 11:45:53 -070040 __skb_queue_purge(&sdata->fragments[i].skb_list);
Jiri Bencf0706e82007-05-05 11:45:53 -070041}
42
43/* Must be called with rtnl lock held. */
Johannes Berg3e122be2008-07-09 14:40:34 +020044int ieee80211_if_add(struct ieee80211_local *local, 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;
Jiri Bencf0706e82007-05-05 11:45:53 -070049 struct ieee80211_sub_if_data *sdata = NULL;
50 int ret;
51
52 ASSERT_RTNL();
Johannes Berg32bfd352007-12-19 01:31:26 +010053 ndev = alloc_netdev(sizeof(*sdata) + local->hw.vif_data_size,
Jiri Bencf0706e82007-05-05 11:45:53 -070054 name, ieee80211_if_setup);
55 if (!ndev)
56 return -ENOMEM;
57
Johannes Bergf3994ec2008-05-12 20:51:44 -070058 ndev->needed_headroom = local->tx_headroom +
59 4*6 /* four MAC addresses */
60 + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
61 + 6 /* mesh */
62 + 8 /* rfc1042/bridge tunnel */
63 - ETH_HLEN /* ethernet hard_header_len */
64 + IEEE80211_ENCRYPT_HEADROOM;
65 ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
66
Jiri Bencf0706e82007-05-05 11:45:53 -070067 ret = dev_alloc_name(ndev, ndev->name);
68 if (ret < 0)
69 goto fail;
70
71 memcpy(ndev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
Jiri Bencf0706e82007-05-05 11:45:53 -070072 SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
73
Johannes Berg3e122be2008-07-09 14:40:34 +020074 /* don't use IEEE80211_DEV_TO_SUB_IF because it checks too much */
75 sdata = netdev_priv(ndev);
Jiri Bencf0706e82007-05-05 11:45:53 -070076 ndev->ieee80211_ptr = &sdata->wdev;
77 sdata->wdev.wiphy = local->hw.wiphy;
Johannes Berg51fb61e2007-12-19 01:31:27 +010078 sdata->vif.type = IEEE80211_IF_TYPE_AP;
Jiri Bencf0706e82007-05-05 11:45:53 -070079 sdata->dev = ndev;
80 sdata->local = local;
81 ieee80211_if_sdata_init(sdata);
82
83 ret = register_netdevice(ndev);
84 if (ret)
85 goto fail;
86
Jiri Bence9f207f2007-05-05 11:46:38 -070087 ieee80211_debugfs_add_netdev(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -070088 ieee80211_if_set_type(ndev, type);
89
Johannes Berg902acc72008-02-23 15:17:19 +010090 if (ieee80211_vif_is_mesh(&sdata->vif) &&
91 params && params->mesh_id_len)
92 ieee80211_if_sta_set_mesh_id(&sdata->u.sta,
93 params->mesh_id_len,
94 params->mesh_id);
Luis Carlos Coboee385852008-02-23 15:17:11 +010095
Johannes Berg79010422007-09-18 17:29:21 -040096 /* we're under RTNL so all this is fine */
Jiri Bencf0706e82007-05-05 11:45:53 -070097 if (unlikely(local->reg_state == IEEE80211_DEV_UNREGISTERED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -070098 __ieee80211_if_del(local, sdata);
99 return -ENODEV;
100 }
Johannes Berg79010422007-09-18 17:29:21 -0400101 list_add_tail_rcu(&sdata->list, &local->interfaces);
102
Jiri Bencf0706e82007-05-05 11:45:53 -0700103 if (new_dev)
104 *new_dev = ndev;
Jiri Bencf0706e82007-05-05 11:45:53 -0700105
Jiri Bencf0706e82007-05-05 11:45:53 -0700106 return 0;
107
108fail:
109 free_netdev(ndev);
110 return ret;
111}
112
Jiri Bencf0706e82007-05-05 11:45:53 -0700113void ieee80211_if_set_type(struct net_device *dev, int type)
114{
115 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +0100116 int oldtype = sdata->vif.type;
Jiri Bencf0706e82007-05-05 11:45:53 -0700117
Johannes Berg5b2812e2007-09-26 14:27:23 +0200118 /*
Johannes Berg5b2812e2007-09-26 14:27:23 +0200119 * Called even when register_netdevice fails, it would
120 * oops if assigned before initialising the rest.
121 */
122 dev->uninit = ieee80211_if_reinit;
123
124 /* most have no BSS pointer */
125 sdata->bss = NULL;
Johannes Berg51fb61e2007-12-19 01:31:27 +0100126 sdata->vif.type = type;
Johannes Berg5b2812e2007-09-26 14:27:23 +0200127
Johannes Berg8318d782008-01-24 19:38:38 +0100128 sdata->basic_rates = 0;
129
Jiri Bencf0706e82007-05-05 11:45:53 -0700130 switch (type) {
131 case IEEE80211_IF_TYPE_WDS:
Johannes Berg3e122be2008-07-09 14:40:34 +0200132 case IEEE80211_IF_TYPE_VLAN:
Johannes Berg5b2812e2007-09-26 14:27:23 +0200133 /* nothing special */
Jiri Bencf0706e82007-05-05 11:45:53 -0700134 break;
Jiri Bencf0706e82007-05-05 11:45:53 -0700135 case IEEE80211_IF_TYPE_AP:
Jiri Bencf0706e82007-05-05 11:45:53 -0700136 skb_queue_head_init(&sdata->u.ap.ps_bc_buf);
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400137 INIT_LIST_HEAD(&sdata->u.ap.vlans);
Jiri Bencf0706e82007-05-05 11:45:53 -0700138 break;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100139 case IEEE80211_IF_TYPE_MESH_POINT:
Jiri Bencf0706e82007-05-05 11:45:53 -0700140 case IEEE80211_IF_TYPE_STA:
141 case IEEE80211_IF_TYPE_IBSS: {
Jiri Bencf0706e82007-05-05 11:45:53 -0700142 struct ieee80211_if_sta *ifsta;
143
144 ifsta = &sdata->u.sta;
145 INIT_WORK(&ifsta->work, ieee80211_sta_work);
146 setup_timer(&ifsta->timer, ieee80211_sta_timer,
147 (unsigned long) sdata);
148 skb_queue_head_init(&ifsta->skb_queue);
149
150 ifsta->capab = WLAN_CAPABILITY_ESS;
151 ifsta->auth_algs = IEEE80211_AUTH_ALG_OPEN |
152 IEEE80211_AUTH_ALG_SHARED_KEY;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400153 ifsta->flags |= IEEE80211_STA_CREATE_IBSS |
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400154 IEEE80211_STA_AUTO_BSSID_SEL |
155 IEEE80211_STA_AUTO_CHANNEL_SEL;
Johannes Berge2530082008-05-17 00:57:14 +0200156 if (ieee80211_num_regular_queues(&sdata->local->hw) >= 4)
Johannes Berg3434fbd2008-05-03 00:59:37 +0200157 ifsta->flags |= IEEE80211_STA_WMM_ENABLED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700158
Johannes Berg902acc72008-02-23 15:17:19 +0100159 if (ieee80211_vif_is_mesh(&sdata->vif))
160 ieee80211_mesh_init_sdata(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -0700161 break;
162 }
163 case IEEE80211_IF_TYPE_MNTR:
164 dev->type = ARPHRD_IEEE80211_RADIOTAP;
Johannes Berg40f7cac2007-07-10 19:32:08 +0200165 dev->hard_start_xmit = ieee80211_monitor_start_xmit;
Michael Wu8cc9a732008-01-31 19:48:23 +0100166 sdata->u.mntr_flags = MONITOR_FLAG_CONTROL |
167 MONITOR_FLAG_OTHER_BSS;
Jiri Bencf0706e82007-05-05 11:45:53 -0700168 break;
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200169 case IEEE80211_IF_TYPE_INVALID:
170 BUG();
171 break;
Jiri Bencf0706e82007-05-05 11:45:53 -0700172 }
Jiri Bence9f207f2007-05-05 11:46:38 -0700173 ieee80211_debugfs_change_if_type(sdata, oldtype);
Jiri Bencf0706e82007-05-05 11:45:53 -0700174}
175
176/* Must be called with rtnl lock held. */
177void ieee80211_if_reinit(struct net_device *dev)
178{
179 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
180 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg5b2812e2007-09-26 14:27:23 +0200181 struct sk_buff *skb;
Johannes Berg44213b52008-02-25 16:27:49 +0100182 int flushed;
Jiri Bencf0706e82007-05-05 11:45:53 -0700183
184 ASSERT_RTNL();
Johannes Berg11a843b2007-08-28 17:01:55 -0400185
186 ieee80211_free_keys(sdata);
187
Jiri Bencf0706e82007-05-05 11:45:53 -0700188 ieee80211_if_sdata_deinit(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -0700189
Johannes Berg902acc72008-02-23 15:17:19 +0100190 /* Need to handle mesh specially to allow eliding the function call */
191 if (ieee80211_vif_is_mesh(&sdata->vif))
192 mesh_rmc_free(dev);
193
Johannes Berg51fb61e2007-12-19 01:31:27 +0100194 switch (sdata->vif.type) {
Johannes Berga2897552007-09-28 14:01:25 +0200195 case IEEE80211_IF_TYPE_INVALID:
196 /* cannot happen */
197 WARN_ON(1);
198 break;
Jiri Bencf0706e82007-05-05 11:45:53 -0700199 case IEEE80211_IF_TYPE_AP: {
Johannes Berge6a5ddf2008-02-25 16:27:42 +0100200 struct beacon_data *beacon;
Jiri Bencf0706e82007-05-05 11:45:53 -0700201
Johannes Berge6a5ddf2008-02-25 16:27:42 +0100202 beacon = sdata->u.ap.beacon;
203 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
204 synchronize_rcu();
205 kfree(beacon);
Jiri Bencf0706e82007-05-05 11:45:53 -0700206
Johannes Berg5b2812e2007-09-26 14:27:23 +0200207 while ((skb = skb_dequeue(&sdata->u.ap.ps_bc_buf))) {
208 local->total_ps_buffered--;
209 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700210 }
211
212 break;
213 }
214 case IEEE80211_IF_TYPE_WDS:
Johannes Berg3e122be2008-07-09 14:40:34 +0200215 case IEEE80211_IF_TYPE_VLAN:
Johannes Berg44213b52008-02-25 16:27:49 +0100216 /* nothing to do */
Jiri Bencf0706e82007-05-05 11:45:53 -0700217 break;
Johannes Berg6032f932008-02-23 15:17:07 +0100218 case IEEE80211_IF_TYPE_MESH_POINT:
Jiri Bencf0706e82007-05-05 11:45:53 -0700219 case IEEE80211_IF_TYPE_STA:
220 case IEEE80211_IF_TYPE_IBSS:
221 kfree(sdata->u.sta.extra_ie);
222 sdata->u.sta.extra_ie = NULL;
223 kfree(sdata->u.sta.assocreq_ies);
224 sdata->u.sta.assocreq_ies = NULL;
225 kfree(sdata->u.sta.assocresp_ies);
226 sdata->u.sta.assocresp_ies = NULL;
227 if (sdata->u.sta.probe_resp) {
228 dev_kfree_skb(sdata->u.sta.probe_resp);
229 sdata->u.sta.probe_resp = NULL;
230 }
231
232 break;
233 case IEEE80211_IF_TYPE_MNTR:
234 dev->type = ARPHRD_ETHER;
235 break;
236 }
237
Johannes Berg44213b52008-02-25 16:27:49 +0100238 flushed = sta_info_flush(local, sdata);
239 WARN_ON(flushed);
Jiri Bencf0706e82007-05-05 11:45:53 -0700240
241 memset(&sdata->u, 0, sizeof(sdata->u));
242 ieee80211_if_sdata_init(sdata);
243}
244
245/* Must be called with rtnl lock held. */
246void __ieee80211_if_del(struct ieee80211_local *local,
247 struct ieee80211_sub_if_data *sdata)
248{
249 struct net_device *dev = sdata->dev;
250
Jiri Bence9f207f2007-05-05 11:46:38 -0700251 ieee80211_debugfs_remove_netdev(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -0700252 unregister_netdevice(dev);
Johannes Berg3e122be2008-07-09 14:40:34 +0200253 /*
254 * The net_device will be freed by its destructor,
255 * i.e. ieee80211_if_free.
256 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700257}
258
259/* Must be called with rtnl lock held. */
260int ieee80211_if_remove(struct net_device *dev, const char *name, int id)
261{
262 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
263 struct ieee80211_sub_if_data *sdata, *n;
264
265 ASSERT_RTNL();
266
Johannes Berg79010422007-09-18 17:29:21 -0400267 list_for_each_entry_safe(sdata, n, &local->interfaces, list) {
Johannes Berg51fb61e2007-12-19 01:31:27 +0100268 if ((sdata->vif.type == id || id == -1) &&
Johannes Berg3e122be2008-07-09 14:40:34 +0200269 strcmp(name, sdata->dev->name) == 0) {
Johannes Berg79010422007-09-18 17:29:21 -0400270 list_del_rcu(&sdata->list);
271 synchronize_rcu();
Jiri Bencf0706e82007-05-05 11:45:53 -0700272 __ieee80211_if_del(local, sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -0700273 return 0;
274 }
275 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700276 return -ENODEV;
277}
278
279void ieee80211_if_free(struct net_device *dev)
280{
Jiri Bencf0706e82007-05-05 11:45:53 -0700281 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
282
Jiri Bencf0706e82007-05-05 11:45:53 -0700283 ieee80211_if_sdata_deinit(sdata);
284 free_netdev(dev);
285}