blob: 8af576c1d2f135f07d2db86c9edca749a9549e94 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * mac80211 configuration hooks for cfg80211
3 *
Johannes Berg62da92f2007-12-19 02:03:31 +01004 * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net>
Jiri Bencf0706e82007-05-05 11:45:53 -07005 *
6 * This file is GPLv2 as found in COPYING.
7 */
8
Johannes Berge8cbb4c2007-12-19 02:03:30 +01009#include <linux/ieee80211.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070010#include <linux/nl80211.h>
11#include <linux/rtnetlink.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070012#include <net/net_namespace.h>
Johannes Berg5dfdaf52007-12-19 02:03:33 +010013#include <linux/rcupdate.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070014#include <net/cfg80211.h>
15#include "ieee80211_i.h"
Michael Wue0eb6852007-09-18 17:29:21 -040016#include "cfg.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040017#include "rate.h"
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +010018#include "mesh.h"
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +010019
Johannes Berg42613db2007-09-28 21:52:27 +020020static enum ieee80211_if_types
21nl80211_type_to_mac80211_type(enum nl80211_iftype type)
22{
23 switch (type) {
24 case NL80211_IFTYPE_UNSPECIFIED:
25 return IEEE80211_IF_TYPE_STA;
26 case NL80211_IFTYPE_ADHOC:
27 return IEEE80211_IF_TYPE_IBSS;
28 case NL80211_IFTYPE_STATION:
29 return IEEE80211_IF_TYPE_STA;
30 case NL80211_IFTYPE_MONITOR:
31 return IEEE80211_IF_TYPE_MNTR;
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +010032#ifdef CONFIG_MAC80211_MESH
33 case NL80211_IFTYPE_MESH_POINT:
34 return IEEE80211_IF_TYPE_MESH_POINT;
35#endif
Johannes Berg42613db2007-09-28 21:52:27 +020036 default:
37 return IEEE80211_IF_TYPE_INVALID;
38 }
39}
40
Jiri Bencf0706e82007-05-05 11:45:53 -070041static int ieee80211_add_iface(struct wiphy *wiphy, char *name,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010042 enum nl80211_iftype type, u32 *flags,
43 struct vif_params *params)
Jiri Bencf0706e82007-05-05 11:45:53 -070044{
45 struct ieee80211_local *local = wiphy_priv(wiphy);
Johannes Berg42613db2007-09-28 21:52:27 +020046 enum ieee80211_if_types itype;
Michael Wu8cc9a732008-01-31 19:48:23 +010047 struct net_device *dev;
48 struct ieee80211_sub_if_data *sdata;
49 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -070050
51 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED))
52 return -ENODEV;
53
Johannes Berg42613db2007-09-28 21:52:27 +020054 itype = nl80211_type_to_mac80211_type(type);
55 if (itype == IEEE80211_IF_TYPE_INVALID)
Jiri Bencf0706e82007-05-05 11:45:53 -070056 return -EINVAL;
Jiri Bencf0706e82007-05-05 11:45:53 -070057
Luis Carlos Coboee385852008-02-23 15:17:11 +010058 err = ieee80211_if_add(local->mdev, name, &dev, itype, params);
Michael Wu8cc9a732008-01-31 19:48:23 +010059 if (err || itype != IEEE80211_IF_TYPE_MNTR || !flags)
60 return err;
61
62 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
63 sdata->u.mntr_flags = *flags;
64 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -070065}
66
67static int ieee80211_del_iface(struct wiphy *wiphy, int ifindex)
68{
69 struct ieee80211_local *local = wiphy_priv(wiphy);
70 struct net_device *dev;
71 char *name;
72
73 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED))
74 return -ENODEV;
75
Johannes Berg42613db2007-09-28 21:52:27 +020076 /* we're under RTNL */
77 dev = __dev_get_by_index(&init_net, ifindex);
Jiri Bencf0706e82007-05-05 11:45:53 -070078 if (!dev)
79 return 0;
80
81 name = dev->name;
Jiri Bencf0706e82007-05-05 11:45:53 -070082
83 return ieee80211_if_remove(local->mdev, name, -1);
84}
85
Johannes Berg42613db2007-09-28 21:52:27 +020086static int ieee80211_change_iface(struct wiphy *wiphy, int ifindex,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010087 enum nl80211_iftype type, u32 *flags,
88 struct vif_params *params)
Johannes Berg42613db2007-09-28 21:52:27 +020089{
90 struct ieee80211_local *local = wiphy_priv(wiphy);
91 struct net_device *dev;
92 enum ieee80211_if_types itype;
93 struct ieee80211_sub_if_data *sdata;
94
95 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED))
96 return -ENODEV;
97
98 /* we're under RTNL */
99 dev = __dev_get_by_index(&init_net, ifindex);
100 if (!dev)
101 return -ENODEV;
102
103 if (netif_running(dev))
104 return -EBUSY;
105
106 itype = nl80211_type_to_mac80211_type(type);
107 if (itype == IEEE80211_IF_TYPE_INVALID)
108 return -EINVAL;
109
110 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
111
Johannes Berg51fb61e2007-12-19 01:31:27 +0100112 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
Johannes Berg42613db2007-09-28 21:52:27 +0200113 return -EOPNOTSUPP;
114
115 ieee80211_if_reinit(dev);
116 ieee80211_if_set_type(dev, itype);
117
Johannes Berg902acc72008-02-23 15:17:19 +0100118 if (ieee80211_vif_is_mesh(&sdata->vif) && params->mesh_id_len)
119 ieee80211_if_sta_set_mesh_id(&sdata->u.sta,
120 params->mesh_id_len,
121 params->mesh_id);
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100122
Michael Wu8cc9a732008-01-31 19:48:23 +0100123 if (sdata->vif.type != IEEE80211_IF_TYPE_MNTR || !flags)
124 return 0;
125
126 sdata->u.mntr_flags = *flags;
Johannes Berg42613db2007-09-28 21:52:27 +0200127 return 0;
128}
129
Johannes Berge8cbb4c2007-12-19 02:03:30 +0100130static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev,
131 u8 key_idx, u8 *mac_addr,
132 struct key_params *params)
133{
134 struct ieee80211_sub_if_data *sdata;
135 struct sta_info *sta = NULL;
136 enum ieee80211_key_alg alg;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100137 struct ieee80211_key *key;
Johannes Berg3b967662008-04-08 17:56:52 +0200138 int err;
Johannes Berge8cbb4c2007-12-19 02:03:30 +0100139
140 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
141
142 switch (params->cipher) {
143 case WLAN_CIPHER_SUITE_WEP40:
144 case WLAN_CIPHER_SUITE_WEP104:
145 alg = ALG_WEP;
146 break;
147 case WLAN_CIPHER_SUITE_TKIP:
148 alg = ALG_TKIP;
149 break;
150 case WLAN_CIPHER_SUITE_CCMP:
151 alg = ALG_CCMP;
152 break;
153 default:
154 return -EINVAL;
155 }
156
Johannes Bergdb4d1162008-02-25 16:27:45 +0100157 key = ieee80211_key_alloc(alg, key_idx, params->key_len, params->key);
158 if (!key)
159 return -ENOMEM;
160
Johannes Berg3b967662008-04-08 17:56:52 +0200161 rcu_read_lock();
162
Johannes Berge8cbb4c2007-12-19 02:03:30 +0100163 if (mac_addr) {
164 sta = sta_info_get(sdata->local, mac_addr);
Johannes Bergdb4d1162008-02-25 16:27:45 +0100165 if (!sta) {
166 ieee80211_key_free(key);
Johannes Berg3b967662008-04-08 17:56:52 +0200167 err = -ENOENT;
168 goto out_unlock;
Johannes Bergdb4d1162008-02-25 16:27:45 +0100169 }
Johannes Berge8cbb4c2007-12-19 02:03:30 +0100170 }
171
Johannes Bergdb4d1162008-02-25 16:27:45 +0100172 ieee80211_key_link(key, sdata, sta);
173
Johannes Berg3b967662008-04-08 17:56:52 +0200174 err = 0;
175 out_unlock:
176 rcu_read_unlock();
177
178 return err;
Johannes Berge8cbb4c2007-12-19 02:03:30 +0100179}
180
181static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev,
182 u8 key_idx, u8 *mac_addr)
183{
184 struct ieee80211_sub_if_data *sdata;
185 struct sta_info *sta;
186 int ret;
187
188 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
189
Johannes Berg3b967662008-04-08 17:56:52 +0200190 rcu_read_lock();
191
Johannes Berge8cbb4c2007-12-19 02:03:30 +0100192 if (mac_addr) {
Johannes Berg3b967662008-04-08 17:56:52 +0200193 ret = -ENOENT;
194
Johannes Berge8cbb4c2007-12-19 02:03:30 +0100195 sta = sta_info_get(sdata->local, mac_addr);
196 if (!sta)
Johannes Berg3b967662008-04-08 17:56:52 +0200197 goto out_unlock;
Johannes Berge8cbb4c2007-12-19 02:03:30 +0100198
Johannes Bergdb4d1162008-02-25 16:27:45 +0100199 if (sta->key) {
Johannes Bergd0709a62008-02-25 16:27:46 +0100200 ieee80211_key_free(sta->key);
Johannes Bergdb4d1162008-02-25 16:27:45 +0100201 WARN_ON(sta->key);
Johannes Berg3b967662008-04-08 17:56:52 +0200202 ret = 0;
203 }
Johannes Berge8cbb4c2007-12-19 02:03:30 +0100204
Johannes Berg3b967662008-04-08 17:56:52 +0200205 goto out_unlock;
Johannes Berge8cbb4c2007-12-19 02:03:30 +0100206 }
207
Johannes Berg3b967662008-04-08 17:56:52 +0200208 if (!sdata->keys[key_idx]) {
209 ret = -ENOENT;
210 goto out_unlock;
211 }
Johannes Berge8cbb4c2007-12-19 02:03:30 +0100212
Johannes Bergd0709a62008-02-25 16:27:46 +0100213 ieee80211_key_free(sdata->keys[key_idx]);
Johannes Bergdb4d1162008-02-25 16:27:45 +0100214 WARN_ON(sdata->keys[key_idx]);
Johannes Berge8cbb4c2007-12-19 02:03:30 +0100215
Johannes Berg3b967662008-04-08 17:56:52 +0200216 ret = 0;
217 out_unlock:
218 rcu_read_unlock();
219
220 return ret;
Johannes Berge8cbb4c2007-12-19 02:03:30 +0100221}
222
Johannes Berg62da92f2007-12-19 02:03:31 +0100223static int ieee80211_get_key(struct wiphy *wiphy, struct net_device *dev,
224 u8 key_idx, u8 *mac_addr, void *cookie,
225 void (*callback)(void *cookie,
226 struct key_params *params))
227{
228 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
229 struct sta_info *sta = NULL;
230 u8 seq[6] = {0};
231 struct key_params params;
232 struct ieee80211_key *key;
233 u32 iv32;
234 u16 iv16;
235 int err = -ENOENT;
236
Johannes Berg3b967662008-04-08 17:56:52 +0200237 rcu_read_lock();
238
Johannes Berg62da92f2007-12-19 02:03:31 +0100239 if (mac_addr) {
240 sta = sta_info_get(sdata->local, mac_addr);
241 if (!sta)
242 goto out;
243
244 key = sta->key;
245 } else
246 key = sdata->keys[key_idx];
247
248 if (!key)
249 goto out;
250
251 memset(&params, 0, sizeof(params));
252
253 switch (key->conf.alg) {
254 case ALG_TKIP:
255 params.cipher = WLAN_CIPHER_SUITE_TKIP;
256
257 iv32 = key->u.tkip.iv32;
258 iv16 = key->u.tkip.iv16;
259
260 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE &&
261 sdata->local->ops->get_tkip_seq)
262 sdata->local->ops->get_tkip_seq(
263 local_to_hw(sdata->local),
264 key->conf.hw_key_idx,
265 &iv32, &iv16);
266
267 seq[0] = iv16 & 0xff;
268 seq[1] = (iv16 >> 8) & 0xff;
269 seq[2] = iv32 & 0xff;
270 seq[3] = (iv32 >> 8) & 0xff;
271 seq[4] = (iv32 >> 16) & 0xff;
272 seq[5] = (iv32 >> 24) & 0xff;
273 params.seq = seq;
274 params.seq_len = 6;
275 break;
276 case ALG_CCMP:
277 params.cipher = WLAN_CIPHER_SUITE_CCMP;
278 seq[0] = key->u.ccmp.tx_pn[5];
279 seq[1] = key->u.ccmp.tx_pn[4];
280 seq[2] = key->u.ccmp.tx_pn[3];
281 seq[3] = key->u.ccmp.tx_pn[2];
282 seq[4] = key->u.ccmp.tx_pn[1];
283 seq[5] = key->u.ccmp.tx_pn[0];
284 params.seq = seq;
285 params.seq_len = 6;
286 break;
287 case ALG_WEP:
288 if (key->conf.keylen == 5)
289 params.cipher = WLAN_CIPHER_SUITE_WEP40;
290 else
291 params.cipher = WLAN_CIPHER_SUITE_WEP104;
292 break;
293 }
294
295 params.key = key->conf.key;
296 params.key_len = key->conf.keylen;
297
298 callback(cookie, &params);
299 err = 0;
300
301 out:
Johannes Berg3b967662008-04-08 17:56:52 +0200302 rcu_read_unlock();
Johannes Berg62da92f2007-12-19 02:03:31 +0100303 return err;
304}
305
Johannes Berge8cbb4c2007-12-19 02:03:30 +0100306static int ieee80211_config_default_key(struct wiphy *wiphy,
307 struct net_device *dev,
308 u8 key_idx)
309{
310 struct ieee80211_sub_if_data *sdata;
311
Johannes Berg3b967662008-04-08 17:56:52 +0200312 rcu_read_lock();
313
Johannes Berge8cbb4c2007-12-19 02:03:30 +0100314 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
315 ieee80211_set_default_key(sdata, key_idx);
316
Johannes Berg3b967662008-04-08 17:56:52 +0200317 rcu_read_unlock();
318
Johannes Berge8cbb4c2007-12-19 02:03:30 +0100319 return 0;
320}
321
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100322static void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
323{
Johannes Bergd0709a62008-02-25 16:27:46 +0100324 struct ieee80211_sub_if_data *sdata = sta->sdata;
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100325
326 sinfo->filled = STATION_INFO_INACTIVE_TIME |
327 STATION_INFO_RX_BYTES |
328 STATION_INFO_TX_BYTES;
329
330 sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
331 sinfo->rx_bytes = sta->rx_bytes;
332 sinfo->tx_bytes = sta->tx_bytes;
333
Johannes Berg902acc72008-02-23 15:17:19 +0100334 if (ieee80211_vif_is_mesh(&sdata->vif)) {
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100335#ifdef CONFIG_MAC80211_MESH
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100336 sinfo->filled |= STATION_INFO_LLID |
337 STATION_INFO_PLID |
338 STATION_INFO_PLINK_STATE;
339
340 sinfo->llid = le16_to_cpu(sta->llid);
341 sinfo->plid = le16_to_cpu(sta->plid);
342 sinfo->plink_state = sta->plink_state;
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100343#endif
Johannes Berg902acc72008-02-23 15:17:19 +0100344 }
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100345}
346
347
348static int ieee80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
349 int idx, u8 *mac, struct station_info *sinfo)
350{
351 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
352 struct sta_info *sta;
Johannes Bergd0709a62008-02-25 16:27:46 +0100353 int ret = -ENOENT;
354
355 rcu_read_lock();
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100356
357 sta = sta_info_get_by_idx(local, idx, dev);
Johannes Bergd0709a62008-02-25 16:27:46 +0100358 if (sta) {
359 ret = 0;
360 memcpy(mac, sta->addr, ETH_ALEN);
361 sta_set_sinfo(sta, sinfo);
362 }
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100363
Johannes Bergd0709a62008-02-25 16:27:46 +0100364 rcu_read_unlock();
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100365
Johannes Bergd0709a62008-02-25 16:27:46 +0100366 return ret;
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100367}
368
Johannes Berg7bbdd2d2007-12-19 02:03:37 +0100369static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100370 u8 *mac, struct station_info *sinfo)
Johannes Berg7bbdd2d2007-12-19 02:03:37 +0100371{
372 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
373 struct sta_info *sta;
Johannes Bergd0709a62008-02-25 16:27:46 +0100374 int ret = -ENOENT;
Johannes Berg7bbdd2d2007-12-19 02:03:37 +0100375
Johannes Bergd0709a62008-02-25 16:27:46 +0100376 rcu_read_lock();
Johannes Berg7bbdd2d2007-12-19 02:03:37 +0100377
378 /* XXX: verify sta->dev == dev */
Johannes Berg7bbdd2d2007-12-19 02:03:37 +0100379
Johannes Bergd0709a62008-02-25 16:27:46 +0100380 sta = sta_info_get(local, mac);
381 if (sta) {
382 ret = 0;
383 sta_set_sinfo(sta, sinfo);
384 }
385
386 rcu_read_unlock();
387
388 return ret;
Johannes Berg7bbdd2d2007-12-19 02:03:37 +0100389}
390
Johannes Berg5dfdaf52007-12-19 02:03:33 +0100391/*
392 * This handles both adding a beacon and setting new beacon info
393 */
394static int ieee80211_config_beacon(struct ieee80211_sub_if_data *sdata,
395 struct beacon_parameters *params)
396{
397 struct beacon_data *new, *old;
398 int new_head_len, new_tail_len;
399 int size;
400 int err = -EINVAL;
401
402 old = sdata->u.ap.beacon;
403
404 /* head must not be zero-length */
405 if (params->head && !params->head_len)
406 return -EINVAL;
407
408 /*
409 * This is a kludge. beacon interval should really be part
410 * of the beacon information.
411 */
412 if (params->interval) {
413 sdata->local->hw.conf.beacon_int = params->interval;
414 if (ieee80211_hw_config(sdata->local))
415 return -EINVAL;
416 /*
417 * We updated some parameter so if below bails out
418 * it's not an error.
419 */
420 err = 0;
421 }
422
423 /* Need to have a beacon head if we don't have one yet */
424 if (!params->head && !old)
425 return err;
426
427 /* sorry, no way to start beaconing without dtim period */
428 if (!params->dtim_period && !old)
429 return err;
430
431 /* new or old head? */
432 if (params->head)
433 new_head_len = params->head_len;
434 else
435 new_head_len = old->head_len;
436
437 /* new or old tail? */
438 if (params->tail || !old)
439 /* params->tail_len will be zero for !params->tail */
440 new_tail_len = params->tail_len;
441 else
442 new_tail_len = old->tail_len;
443
444 size = sizeof(*new) + new_head_len + new_tail_len;
445
446 new = kzalloc(size, GFP_KERNEL);
447 if (!new)
448 return -ENOMEM;
449
450 /* start filling the new info now */
451
452 /* new or old dtim period? */
453 if (params->dtim_period)
454 new->dtim_period = params->dtim_period;
455 else
456 new->dtim_period = old->dtim_period;
457
458 /*
459 * pointers go into the block we allocated,
460 * memory is | beacon_data | head | tail |
461 */
462 new->head = ((u8 *) new) + sizeof(*new);
463 new->tail = new->head + new_head_len;
464 new->head_len = new_head_len;
465 new->tail_len = new_tail_len;
466
467 /* copy in head */
468 if (params->head)
469 memcpy(new->head, params->head, new_head_len);
470 else
471 memcpy(new->head, old->head, new_head_len);
472
473 /* copy in optional tail */
474 if (params->tail)
475 memcpy(new->tail, params->tail, new_tail_len);
476 else
477 if (old)
478 memcpy(new->tail, old->tail, new_tail_len);
479
480 rcu_assign_pointer(sdata->u.ap.beacon, new);
481
482 synchronize_rcu();
483
484 kfree(old);
485
486 return ieee80211_if_config_beacon(sdata->dev);
487}
488
489static int ieee80211_add_beacon(struct wiphy *wiphy, struct net_device *dev,
490 struct beacon_parameters *params)
491{
492 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
493 struct beacon_data *old;
494
495 if (sdata->vif.type != IEEE80211_IF_TYPE_AP)
496 return -EINVAL;
497
498 old = sdata->u.ap.beacon;
499
500 if (old)
501 return -EALREADY;
502
503 return ieee80211_config_beacon(sdata, params);
504}
505
506static int ieee80211_set_beacon(struct wiphy *wiphy, struct net_device *dev,
507 struct beacon_parameters *params)
508{
509 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
510 struct beacon_data *old;
511
512 if (sdata->vif.type != IEEE80211_IF_TYPE_AP)
513 return -EINVAL;
514
515 old = sdata->u.ap.beacon;
516
517 if (!old)
518 return -ENOENT;
519
520 return ieee80211_config_beacon(sdata, params);
521}
522
523static int ieee80211_del_beacon(struct wiphy *wiphy, struct net_device *dev)
524{
525 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
526 struct beacon_data *old;
527
528 if (sdata->vif.type != IEEE80211_IF_TYPE_AP)
529 return -EINVAL;
530
531 old = sdata->u.ap.beacon;
532
533 if (!old)
534 return -ENOENT;
535
536 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
537 synchronize_rcu();
538 kfree(old);
539
540 return ieee80211_if_config_beacon(dev);
541}
542
Johannes Berg4fd69312007-12-19 02:03:35 +0100543/* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
544struct iapp_layer2_update {
545 u8 da[ETH_ALEN]; /* broadcast */
546 u8 sa[ETH_ALEN]; /* STA addr */
547 __be16 len; /* 6 */
548 u8 dsap; /* 0 */
549 u8 ssap; /* 0 */
550 u8 control;
551 u8 xid_info[3];
552} __attribute__ ((packed));
553
554static void ieee80211_send_layer2_update(struct sta_info *sta)
555{
556 struct iapp_layer2_update *msg;
557 struct sk_buff *skb;
558
559 /* Send Level 2 Update Frame to update forwarding tables in layer 2
560 * bridge devices */
561
562 skb = dev_alloc_skb(sizeof(*msg));
563 if (!skb)
564 return;
565 msg = (struct iapp_layer2_update *)skb_put(skb, sizeof(*msg));
566
567 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
568 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
569
570 memset(msg->da, 0xff, ETH_ALEN);
571 memcpy(msg->sa, sta->addr, ETH_ALEN);
572 msg->len = htons(6);
573 msg->dsap = 0;
574 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
575 msg->control = 0xaf; /* XID response lsb.1111F101.
576 * F=0 (no poll command; unsolicited frame) */
577 msg->xid_info[0] = 0x81; /* XID format identifier */
578 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
579 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
580
Johannes Bergd0709a62008-02-25 16:27:46 +0100581 skb->dev = sta->sdata->dev;
582 skb->protocol = eth_type_trans(skb, sta->sdata->dev);
Johannes Berg4fd69312007-12-19 02:03:35 +0100583 memset(skb->cb, 0, sizeof(skb->cb));
584 netif_rx(skb);
585}
586
587static void sta_apply_parameters(struct ieee80211_local *local,
588 struct sta_info *sta,
589 struct station_parameters *params)
590{
591 u32 rates;
592 int i, j;
Johannes Berg8318d782008-01-24 19:38:38 +0100593 struct ieee80211_supported_band *sband;
Johannes Bergd0709a62008-02-25 16:27:46 +0100594 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg4fd69312007-12-19 02:03:35 +0100595
Johannes Berg73651ee2008-02-25 16:27:47 +0100596 /*
597 * FIXME: updating the flags is racy when this function is
598 * called from ieee80211_change_station(), this will
599 * be resolved in a future patch.
600 */
601
Johannes Berg4fd69312007-12-19 02:03:35 +0100602 if (params->station_flags & STATION_FLAG_CHANGED) {
603 sta->flags &= ~WLAN_STA_AUTHORIZED;
604 if (params->station_flags & STATION_FLAG_AUTHORIZED)
605 sta->flags |= WLAN_STA_AUTHORIZED;
606
607 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
608 if (params->station_flags & STATION_FLAG_SHORT_PREAMBLE)
609 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
610
611 sta->flags &= ~WLAN_STA_WME;
612 if (params->station_flags & STATION_FLAG_WME)
613 sta->flags |= WLAN_STA_WME;
614 }
615
Johannes Berg73651ee2008-02-25 16:27:47 +0100616 /*
617 * FIXME: updating the following information is racy when this
618 * function is called from ieee80211_change_station().
619 * However, all this information should be static so
620 * maybe we should just reject attemps to change it.
621 */
622
Johannes Berg4fd69312007-12-19 02:03:35 +0100623 if (params->aid) {
624 sta->aid = params->aid;
625 if (sta->aid > IEEE80211_MAX_AID)
626 sta->aid = 0; /* XXX: should this be an error? */
627 }
628
629 if (params->listen_interval >= 0)
630 sta->listen_interval = params->listen_interval;
631
632 if (params->supported_rates) {
633 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100634 sband = local->hw.wiphy->bands[local->oper_channel->band];
635
Johannes Berg4fd69312007-12-19 02:03:35 +0100636 for (i = 0; i < params->supported_rates_len; i++) {
637 int rate = (params->supported_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +0100638 for (j = 0; j < sband->n_bitrates; j++) {
639 if (sband->bitrates[j].bitrate == rate)
Johannes Berg4fd69312007-12-19 02:03:35 +0100640 rates |= BIT(j);
641 }
642 }
Johannes Berg8318d782008-01-24 19:38:38 +0100643 sta->supp_rates[local->oper_channel->band] = rates;
Johannes Berg4fd69312007-12-19 02:03:35 +0100644 }
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100645
Johannes Berg902acc72008-02-23 15:17:19 +0100646 if (ieee80211_vif_is_mesh(&sdata->vif) && params->plink_action) {
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100647 switch (params->plink_action) {
648 case PLINK_ACTION_OPEN:
649 mesh_plink_open(sta);
650 break;
651 case PLINK_ACTION_BLOCK:
652 mesh_plink_block(sta);
653 break;
654 }
Johannes Berg902acc72008-02-23 15:17:19 +0100655 }
Johannes Berg4fd69312007-12-19 02:03:35 +0100656}
657
658static int ieee80211_add_station(struct wiphy *wiphy, struct net_device *dev,
659 u8 *mac, struct station_parameters *params)
660{
661 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
662 struct sta_info *sta;
663 struct ieee80211_sub_if_data *sdata;
Johannes Berg73651ee2008-02-25 16:27:47 +0100664 int err;
Johannes Berg4fd69312007-12-19 02:03:35 +0100665
666 /* Prevent a race with changing the rate control algorithm */
667 if (!netif_running(dev))
668 return -ENETDOWN;
669
Johannes Berg4fd69312007-12-19 02:03:35 +0100670 if (params->vlan) {
671 sdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
672
673 if (sdata->vif.type != IEEE80211_IF_TYPE_VLAN ||
674 sdata->vif.type != IEEE80211_IF_TYPE_AP)
675 return -EINVAL;
676 } else
677 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
678
Johannes Berg03e44972008-02-27 09:56:40 +0100679 if (compare_ether_addr(mac, dev->dev_addr) == 0)
680 return -EINVAL;
681
682 if (is_multicast_ether_addr(mac))
683 return -EINVAL;
684
685 sta = sta_info_alloc(sdata, mac, GFP_KERNEL);
Johannes Berg73651ee2008-02-25 16:27:47 +0100686 if (!sta)
687 return -ENOMEM;
Johannes Berg4fd69312007-12-19 02:03:35 +0100688
689 sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
690
691 sta_apply_parameters(local, sta, params);
692
693 rate_control_rate_init(sta, local);
694
Johannes Berg73651ee2008-02-25 16:27:47 +0100695 rcu_read_lock();
696
697 err = sta_info_insert(sta);
698 if (err) {
Johannes Berg93e5deb2008-04-01 15:21:00 +0200699 /* STA has been freed */
Johannes Berg73651ee2008-02-25 16:27:47 +0100700 rcu_read_unlock();
701 return err;
702 }
703
704 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN ||
705 sdata->vif.type == IEEE80211_IF_TYPE_AP)
706 ieee80211_send_layer2_update(sta);
707
708 rcu_read_unlock();
709
Johannes Berg4fd69312007-12-19 02:03:35 +0100710 return 0;
711}
712
713static int ieee80211_del_station(struct wiphy *wiphy, struct net_device *dev,
714 u8 *mac)
715{
Johannes Bergd0709a62008-02-25 16:27:46 +0100716 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
717 struct ieee80211_local *local = sdata->local;
Johannes Berg4fd69312007-12-19 02:03:35 +0100718 struct sta_info *sta;
719
720 if (mac) {
721 /* XXX: get sta belonging to dev */
722 sta = sta_info_get(local, mac);
723 if (!sta)
724 return -ENOENT;
725
Johannes Bergd0709a62008-02-25 16:27:46 +0100726 sta_info_unlink(&sta);
Johannes Berg4f6fab42008-03-31 19:23:02 +0200727 sta_info_destroy(sta);
Johannes Berg4fd69312007-12-19 02:03:35 +0100728 } else
Johannes Bergd0709a62008-02-25 16:27:46 +0100729 sta_info_flush(local, sdata);
Johannes Berg4fd69312007-12-19 02:03:35 +0100730
731 return 0;
732}
733
734static int ieee80211_change_station(struct wiphy *wiphy,
735 struct net_device *dev,
736 u8 *mac,
737 struct station_parameters *params)
738{
739 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
740 struct sta_info *sta;
741 struct ieee80211_sub_if_data *vlansdata;
742
743 /* XXX: get sta belonging to dev */
744 sta = sta_info_get(local, mac);
745 if (!sta)
746 return -ENOENT;
747
Johannes Bergd0709a62008-02-25 16:27:46 +0100748 if (params->vlan && params->vlan != sta->sdata->dev) {
Johannes Berg4fd69312007-12-19 02:03:35 +0100749 vlansdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
750
751 if (vlansdata->vif.type != IEEE80211_IF_TYPE_VLAN ||
752 vlansdata->vif.type != IEEE80211_IF_TYPE_AP)
753 return -EINVAL;
754
Johannes Bergd0709a62008-02-25 16:27:46 +0100755 sta->sdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
Johannes Berg4fd69312007-12-19 02:03:35 +0100756 ieee80211_send_layer2_update(sta);
757 }
758
759 sta_apply_parameters(local, sta, params);
760
Johannes Berg4fd69312007-12-19 02:03:35 +0100761 return 0;
762}
763
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100764#ifdef CONFIG_MAC80211_MESH
765static int ieee80211_add_mpath(struct wiphy *wiphy, struct net_device *dev,
766 u8 *dst, u8 *next_hop)
767{
768 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
769 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
770 struct mesh_path *mpath;
771 struct sta_info *sta;
772 int err;
773
774 if (!netif_running(dev))
775 return -ENETDOWN;
776
777 if (sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT)
778 return -ENOTSUPP;
779
Johannes Bergd0709a62008-02-25 16:27:46 +0100780 rcu_read_lock();
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100781 sta = sta_info_get(local, next_hop);
Johannes Bergd0709a62008-02-25 16:27:46 +0100782 if (!sta) {
783 rcu_read_unlock();
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100784 return -ENOENT;
Johannes Bergd0709a62008-02-25 16:27:46 +0100785 }
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100786
787 err = mesh_path_add(dst, dev);
Johannes Bergd0709a62008-02-25 16:27:46 +0100788 if (err) {
789 rcu_read_unlock();
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100790 return err;
Johannes Bergd0709a62008-02-25 16:27:46 +0100791 }
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100792
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100793 mpath = mesh_path_lookup(dst, dev);
794 if (!mpath) {
795 rcu_read_unlock();
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100796 return -ENXIO;
797 }
798 mesh_path_fix_nexthop(mpath, sta);
Johannes Bergd0709a62008-02-25 16:27:46 +0100799
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100800 rcu_read_unlock();
801 return 0;
802}
803
804static int ieee80211_del_mpath(struct wiphy *wiphy, struct net_device *dev,
805 u8 *dst)
806{
807 if (dst)
Luis Carlos Cobocfa22c72008-02-29 15:04:13 -0800808 return mesh_path_del(dst, dev);
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100809
810 mesh_path_flush(dev);
811 return 0;
812}
813
814static int ieee80211_change_mpath(struct wiphy *wiphy,
815 struct net_device *dev,
816 u8 *dst, u8 *next_hop)
817{
818 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
819 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
820 struct mesh_path *mpath;
821 struct sta_info *sta;
822
823 if (!netif_running(dev))
824 return -ENETDOWN;
825
826 if (sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT)
827 return -ENOTSUPP;
828
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100829 rcu_read_lock();
Johannes Bergd0709a62008-02-25 16:27:46 +0100830
831 sta = sta_info_get(local, next_hop);
832 if (!sta) {
833 rcu_read_unlock();
834 return -ENOENT;
835 }
836
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100837 mpath = mesh_path_lookup(dst, dev);
838 if (!mpath) {
839 rcu_read_unlock();
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100840 return -ENOENT;
841 }
842
843 mesh_path_fix_nexthop(mpath, sta);
Johannes Bergd0709a62008-02-25 16:27:46 +0100844
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100845 rcu_read_unlock();
846 return 0;
847}
848
849static void mpath_set_pinfo(struct mesh_path *mpath, u8 *next_hop,
850 struct mpath_info *pinfo)
851{
852 if (mpath->next_hop)
853 memcpy(next_hop, mpath->next_hop->addr, ETH_ALEN);
854 else
855 memset(next_hop, 0, ETH_ALEN);
856
857 pinfo->filled = MPATH_INFO_FRAME_QLEN |
858 MPATH_INFO_DSN |
859 MPATH_INFO_METRIC |
860 MPATH_INFO_EXPTIME |
861 MPATH_INFO_DISCOVERY_TIMEOUT |
862 MPATH_INFO_DISCOVERY_RETRIES |
863 MPATH_INFO_FLAGS;
864
865 pinfo->frame_qlen = mpath->frame_queue.qlen;
866 pinfo->dsn = mpath->dsn;
867 pinfo->metric = mpath->metric;
868 if (time_before(jiffies, mpath->exp_time))
869 pinfo->exptime = jiffies_to_msecs(mpath->exp_time - jiffies);
870 pinfo->discovery_timeout =
871 jiffies_to_msecs(mpath->discovery_timeout);
872 pinfo->discovery_retries = mpath->discovery_retries;
873 pinfo->flags = 0;
874 if (mpath->flags & MESH_PATH_ACTIVE)
875 pinfo->flags |= NL80211_MPATH_FLAG_ACTIVE;
876 if (mpath->flags & MESH_PATH_RESOLVING)
877 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
878 if (mpath->flags & MESH_PATH_DSN_VALID)
879 pinfo->flags |= NL80211_MPATH_FLAG_DSN_VALID;
880 if (mpath->flags & MESH_PATH_FIXED)
881 pinfo->flags |= NL80211_MPATH_FLAG_FIXED;
882 if (mpath->flags & MESH_PATH_RESOLVING)
883 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
884
885 pinfo->flags = mpath->flags;
886}
887
888static int ieee80211_get_mpath(struct wiphy *wiphy, struct net_device *dev,
889 u8 *dst, u8 *next_hop, struct mpath_info *pinfo)
890
891{
892 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
893 struct mesh_path *mpath;
894
895 if (sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT)
896 return -ENOTSUPP;
897
898 rcu_read_lock();
899 mpath = mesh_path_lookup(dst, dev);
900 if (!mpath) {
901 rcu_read_unlock();
902 return -ENOENT;
903 }
904 memcpy(dst, mpath->dst, ETH_ALEN);
905 mpath_set_pinfo(mpath, next_hop, pinfo);
906 rcu_read_unlock();
907 return 0;
908}
909
910static int ieee80211_dump_mpath(struct wiphy *wiphy, struct net_device *dev,
911 int idx, u8 *dst, u8 *next_hop,
912 struct mpath_info *pinfo)
913{
914 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
915 struct mesh_path *mpath;
916
917 if (sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT)
918 return -ENOTSUPP;
919
920 rcu_read_lock();
921 mpath = mesh_path_lookup_by_idx(idx, dev);
922 if (!mpath) {
923 rcu_read_unlock();
924 return -ENOENT;
925 }
926 memcpy(dst, mpath->dst, ETH_ALEN);
927 mpath_set_pinfo(mpath, next_hop, pinfo);
928 rcu_read_unlock();
929 return 0;
930}
931#endif
932
Jiri Bencf0706e82007-05-05 11:45:53 -0700933struct cfg80211_ops mac80211_config_ops = {
934 .add_virtual_intf = ieee80211_add_iface,
935 .del_virtual_intf = ieee80211_del_iface,
Johannes Berg42613db2007-09-28 21:52:27 +0200936 .change_virtual_intf = ieee80211_change_iface,
Johannes Berge8cbb4c2007-12-19 02:03:30 +0100937 .add_key = ieee80211_add_key,
938 .del_key = ieee80211_del_key,
Johannes Berg62da92f2007-12-19 02:03:31 +0100939 .get_key = ieee80211_get_key,
Johannes Berge8cbb4c2007-12-19 02:03:30 +0100940 .set_default_key = ieee80211_config_default_key,
Johannes Berg5dfdaf52007-12-19 02:03:33 +0100941 .add_beacon = ieee80211_add_beacon,
942 .set_beacon = ieee80211_set_beacon,
943 .del_beacon = ieee80211_del_beacon,
Johannes Berg4fd69312007-12-19 02:03:35 +0100944 .add_station = ieee80211_add_station,
945 .del_station = ieee80211_del_station,
946 .change_station = ieee80211_change_station,
Johannes Berg7bbdd2d2007-12-19 02:03:37 +0100947 .get_station = ieee80211_get_station,
Luis Carlos Coboc5dd9c22008-02-23 15:17:17 +0100948 .dump_station = ieee80211_dump_station,
949#ifdef CONFIG_MAC80211_MESH
950 .add_mpath = ieee80211_add_mpath,
951 .del_mpath = ieee80211_del_mpath,
952 .change_mpath = ieee80211_change_mpath,
953 .get_mpath = ieee80211_get_mpath,
954 .dump_mpath = ieee80211_dump_mpath,
955#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700956};