mac80211: unify config_interface and bss_info_changed
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / mac80211 / cfg.c
blob648bac1c850e32ab15600fdc7442ef161b064046
1 /*
2 * mac80211 configuration hooks for cfg80211
4 * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net>
6 * This file is GPLv2 as found in COPYING.
7 */
9 #include <linux/ieee80211.h>
10 #include <linux/nl80211.h>
11 #include <linux/rtnetlink.h>
12 #include <net/net_namespace.h>
13 #include <linux/rcupdate.h>
14 #include <net/cfg80211.h>
15 #include "ieee80211_i.h"
16 #include "cfg.h"
17 #include "rate.h"
18 #include "mesh.h"
20 static bool nl80211_type_check(enum nl80211_iftype type)
22 switch (type) {
23 case NL80211_IFTYPE_ADHOC:
24 case NL80211_IFTYPE_STATION:
25 case NL80211_IFTYPE_MONITOR:
26 #ifdef CONFIG_MAC80211_MESH
27 case NL80211_IFTYPE_MESH_POINT:
28 #endif
29 case NL80211_IFTYPE_AP:
30 case NL80211_IFTYPE_AP_VLAN:
31 case NL80211_IFTYPE_WDS:
32 return true;
33 default:
34 return false;
38 static int ieee80211_add_iface(struct wiphy *wiphy, char *name,
39 enum nl80211_iftype type, u32 *flags,
40 struct vif_params *params)
42 struct ieee80211_local *local = wiphy_priv(wiphy);
43 struct net_device *dev;
44 struct ieee80211_sub_if_data *sdata;
45 int err;
47 if (!nl80211_type_check(type))
48 return -EINVAL;
50 err = ieee80211_if_add(local, name, &dev, type, params);
51 if (err || type != NL80211_IFTYPE_MONITOR || !flags)
52 return err;
54 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
55 sdata->u.mntr_flags = *flags;
56 return 0;
59 static int ieee80211_del_iface(struct wiphy *wiphy, int ifindex)
61 struct net_device *dev;
62 struct ieee80211_sub_if_data *sdata;
64 /* we're under RTNL */
65 dev = __dev_get_by_index(&init_net, ifindex);
66 if (!dev)
67 return -ENODEV;
69 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
71 ieee80211_if_remove(sdata);
73 return 0;
76 static int ieee80211_change_iface(struct wiphy *wiphy, int ifindex,
77 enum nl80211_iftype type, u32 *flags,
78 struct vif_params *params)
80 struct net_device *dev;
81 struct ieee80211_sub_if_data *sdata;
82 int ret;
84 /* we're under RTNL */
85 dev = __dev_get_by_index(&init_net, ifindex);
86 if (!dev)
87 return -ENODEV;
89 if (!nl80211_type_check(type))
90 return -EINVAL;
92 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
94 ret = ieee80211_if_change_type(sdata, type);
95 if (ret)
96 return ret;
98 if (netif_running(sdata->dev))
99 return -EBUSY;
101 if (ieee80211_vif_is_mesh(&sdata->vif) && params->mesh_id_len)
102 ieee80211_sdata_set_mesh_id(sdata,
103 params->mesh_id_len,
104 params->mesh_id);
106 if (sdata->vif.type != NL80211_IFTYPE_MONITOR || !flags)
107 return 0;
109 sdata->u.mntr_flags = *flags;
110 return 0;
113 static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev,
114 u8 key_idx, u8 *mac_addr,
115 struct key_params *params)
117 struct ieee80211_sub_if_data *sdata;
118 struct sta_info *sta = NULL;
119 enum ieee80211_key_alg alg;
120 struct ieee80211_key *key;
121 int err;
123 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
125 switch (params->cipher) {
126 case WLAN_CIPHER_SUITE_WEP40:
127 case WLAN_CIPHER_SUITE_WEP104:
128 alg = ALG_WEP;
129 break;
130 case WLAN_CIPHER_SUITE_TKIP:
131 alg = ALG_TKIP;
132 break;
133 case WLAN_CIPHER_SUITE_CCMP:
134 alg = ALG_CCMP;
135 break;
136 case WLAN_CIPHER_SUITE_AES_CMAC:
137 alg = ALG_AES_CMAC;
138 break;
139 default:
140 return -EINVAL;
143 key = ieee80211_key_alloc(alg, key_idx, params->key_len, params->key);
144 if (!key)
145 return -ENOMEM;
147 rcu_read_lock();
149 if (mac_addr) {
150 sta = sta_info_get(sdata->local, mac_addr);
151 if (!sta) {
152 ieee80211_key_free(key);
153 err = -ENOENT;
154 goto out_unlock;
158 ieee80211_key_link(key, sdata, sta);
160 err = 0;
161 out_unlock:
162 rcu_read_unlock();
164 return err;
167 static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev,
168 u8 key_idx, u8 *mac_addr)
170 struct ieee80211_sub_if_data *sdata;
171 struct sta_info *sta;
172 int ret;
174 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
176 rcu_read_lock();
178 if (mac_addr) {
179 ret = -ENOENT;
181 sta = sta_info_get(sdata->local, mac_addr);
182 if (!sta)
183 goto out_unlock;
185 if (sta->key) {
186 ieee80211_key_free(sta->key);
187 WARN_ON(sta->key);
188 ret = 0;
191 goto out_unlock;
194 if (!sdata->keys[key_idx]) {
195 ret = -ENOENT;
196 goto out_unlock;
199 ieee80211_key_free(sdata->keys[key_idx]);
200 WARN_ON(sdata->keys[key_idx]);
202 ret = 0;
203 out_unlock:
204 rcu_read_unlock();
206 return ret;
209 static int ieee80211_get_key(struct wiphy *wiphy, struct net_device *dev,
210 u8 key_idx, u8 *mac_addr, void *cookie,
211 void (*callback)(void *cookie,
212 struct key_params *params))
214 struct ieee80211_sub_if_data *sdata;
215 struct sta_info *sta = NULL;
216 u8 seq[6] = {0};
217 struct key_params params;
218 struct ieee80211_key *key;
219 u32 iv32;
220 u16 iv16;
221 int err = -ENOENT;
223 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
225 rcu_read_lock();
227 if (mac_addr) {
228 sta = sta_info_get(sdata->local, mac_addr);
229 if (!sta)
230 goto out;
232 key = sta->key;
233 } else
234 key = sdata->keys[key_idx];
236 if (!key)
237 goto out;
239 memset(&params, 0, sizeof(params));
241 switch (key->conf.alg) {
242 case ALG_TKIP:
243 params.cipher = WLAN_CIPHER_SUITE_TKIP;
245 iv32 = key->u.tkip.tx.iv32;
246 iv16 = key->u.tkip.tx.iv16;
248 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE &&
249 sdata->local->ops->get_tkip_seq)
250 sdata->local->ops->get_tkip_seq(
251 local_to_hw(sdata->local),
252 key->conf.hw_key_idx,
253 &iv32, &iv16);
255 seq[0] = iv16 & 0xff;
256 seq[1] = (iv16 >> 8) & 0xff;
257 seq[2] = iv32 & 0xff;
258 seq[3] = (iv32 >> 8) & 0xff;
259 seq[4] = (iv32 >> 16) & 0xff;
260 seq[5] = (iv32 >> 24) & 0xff;
261 params.seq = seq;
262 params.seq_len = 6;
263 break;
264 case ALG_CCMP:
265 params.cipher = WLAN_CIPHER_SUITE_CCMP;
266 seq[0] = key->u.ccmp.tx_pn[5];
267 seq[1] = key->u.ccmp.tx_pn[4];
268 seq[2] = key->u.ccmp.tx_pn[3];
269 seq[3] = key->u.ccmp.tx_pn[2];
270 seq[4] = key->u.ccmp.tx_pn[1];
271 seq[5] = key->u.ccmp.tx_pn[0];
272 params.seq = seq;
273 params.seq_len = 6;
274 break;
275 case ALG_WEP:
276 if (key->conf.keylen == 5)
277 params.cipher = WLAN_CIPHER_SUITE_WEP40;
278 else
279 params.cipher = WLAN_CIPHER_SUITE_WEP104;
280 break;
281 case ALG_AES_CMAC:
282 params.cipher = WLAN_CIPHER_SUITE_AES_CMAC;
283 seq[0] = key->u.aes_cmac.tx_pn[5];
284 seq[1] = key->u.aes_cmac.tx_pn[4];
285 seq[2] = key->u.aes_cmac.tx_pn[3];
286 seq[3] = key->u.aes_cmac.tx_pn[2];
287 seq[4] = key->u.aes_cmac.tx_pn[1];
288 seq[5] = key->u.aes_cmac.tx_pn[0];
289 params.seq = seq;
290 params.seq_len = 6;
291 break;
294 params.key = key->conf.key;
295 params.key_len = key->conf.keylen;
297 callback(cookie, &params);
298 err = 0;
300 out:
301 rcu_read_unlock();
302 return err;
305 static int ieee80211_config_default_key(struct wiphy *wiphy,
306 struct net_device *dev,
307 u8 key_idx)
309 struct ieee80211_sub_if_data *sdata;
311 rcu_read_lock();
313 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
314 ieee80211_set_default_key(sdata, key_idx);
316 rcu_read_unlock();
318 return 0;
321 static int ieee80211_config_default_mgmt_key(struct wiphy *wiphy,
322 struct net_device *dev,
323 u8 key_idx)
325 struct ieee80211_sub_if_data *sdata;
327 rcu_read_lock();
329 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
330 ieee80211_set_default_mgmt_key(sdata, key_idx);
332 rcu_read_unlock();
334 return 0;
337 static void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
339 struct ieee80211_sub_if_data *sdata = sta->sdata;
341 sinfo->filled = STATION_INFO_INACTIVE_TIME |
342 STATION_INFO_RX_BYTES |
343 STATION_INFO_TX_BYTES |
344 STATION_INFO_RX_PACKETS |
345 STATION_INFO_TX_PACKETS |
346 STATION_INFO_TX_BITRATE;
348 sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
349 sinfo->rx_bytes = sta->rx_bytes;
350 sinfo->tx_bytes = sta->tx_bytes;
351 sinfo->rx_packets = sta->rx_packets;
352 sinfo->tx_packets = sta->tx_packets;
354 if (sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
355 sinfo->filled |= STATION_INFO_SIGNAL;
356 sinfo->signal = (s8)sta->last_signal;
359 sinfo->txrate.flags = 0;
360 if (sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS)
361 sinfo->txrate.flags |= RATE_INFO_FLAGS_MCS;
362 if (sta->last_tx_rate.flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
363 sinfo->txrate.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
364 if (sta->last_tx_rate.flags & IEEE80211_TX_RC_SHORT_GI)
365 sinfo->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
367 if (!(sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS)) {
368 struct ieee80211_supported_band *sband;
369 sband = sta->local->hw.wiphy->bands[
370 sta->local->hw.conf.channel->band];
371 sinfo->txrate.legacy =
372 sband->bitrates[sta->last_tx_rate.idx].bitrate;
373 } else
374 sinfo->txrate.mcs = sta->last_tx_rate.idx;
376 if (ieee80211_vif_is_mesh(&sdata->vif)) {
377 #ifdef CONFIG_MAC80211_MESH
378 sinfo->filled |= STATION_INFO_LLID |
379 STATION_INFO_PLID |
380 STATION_INFO_PLINK_STATE;
382 sinfo->llid = le16_to_cpu(sta->llid);
383 sinfo->plid = le16_to_cpu(sta->plid);
384 sinfo->plink_state = sta->plink_state;
385 #endif
390 static int ieee80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
391 int idx, u8 *mac, struct station_info *sinfo)
393 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
394 struct sta_info *sta;
395 int ret = -ENOENT;
397 rcu_read_lock();
399 sta = sta_info_get_by_idx(local, idx, dev);
400 if (sta) {
401 ret = 0;
402 memcpy(mac, sta->sta.addr, ETH_ALEN);
403 sta_set_sinfo(sta, sinfo);
406 rcu_read_unlock();
408 return ret;
411 static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev,
412 u8 *mac, struct station_info *sinfo)
414 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
415 struct sta_info *sta;
416 int ret = -ENOENT;
418 rcu_read_lock();
420 /* XXX: verify sta->dev == dev */
422 sta = sta_info_get(local, mac);
423 if (sta) {
424 ret = 0;
425 sta_set_sinfo(sta, sinfo);
428 rcu_read_unlock();
430 return ret;
434 * This handles both adding a beacon and setting new beacon info
436 static int ieee80211_config_beacon(struct ieee80211_sub_if_data *sdata,
437 struct beacon_parameters *params)
439 struct beacon_data *new, *old;
440 int new_head_len, new_tail_len;
441 int size;
442 int err = -EINVAL;
444 old = sdata->u.ap.beacon;
446 /* head must not be zero-length */
447 if (params->head && !params->head_len)
448 return -EINVAL;
451 * This is a kludge. beacon interval should really be part
452 * of the beacon information.
454 if (params->interval &&
455 (sdata->vif.bss_conf.beacon_int != params->interval)) {
456 sdata->vif.bss_conf.beacon_int = params->interval;
457 ieee80211_bss_info_change_notify(sdata,
458 BSS_CHANGED_BEACON_INT);
461 /* Need to have a beacon head if we don't have one yet */
462 if (!params->head && !old)
463 return err;
465 /* sorry, no way to start beaconing without dtim period */
466 if (!params->dtim_period && !old)
467 return err;
469 /* new or old head? */
470 if (params->head)
471 new_head_len = params->head_len;
472 else
473 new_head_len = old->head_len;
475 /* new or old tail? */
476 if (params->tail || !old)
477 /* params->tail_len will be zero for !params->tail */
478 new_tail_len = params->tail_len;
479 else
480 new_tail_len = old->tail_len;
482 size = sizeof(*new) + new_head_len + new_tail_len;
484 new = kzalloc(size, GFP_KERNEL);
485 if (!new)
486 return -ENOMEM;
488 /* start filling the new info now */
490 /* new or old dtim period? */
491 if (params->dtim_period)
492 new->dtim_period = params->dtim_period;
493 else
494 new->dtim_period = old->dtim_period;
497 * pointers go into the block we allocated,
498 * memory is | beacon_data | head | tail |
500 new->head = ((u8 *) new) + sizeof(*new);
501 new->tail = new->head + new_head_len;
502 new->head_len = new_head_len;
503 new->tail_len = new_tail_len;
505 /* copy in head */
506 if (params->head)
507 memcpy(new->head, params->head, new_head_len);
508 else
509 memcpy(new->head, old->head, new_head_len);
511 /* copy in optional tail */
512 if (params->tail)
513 memcpy(new->tail, params->tail, new_tail_len);
514 else
515 if (old)
516 memcpy(new->tail, old->tail, new_tail_len);
518 rcu_assign_pointer(sdata->u.ap.beacon, new);
520 synchronize_rcu();
522 kfree(old);
524 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
525 BSS_CHANGED_BEACON);
526 return 0;
529 static int ieee80211_add_beacon(struct wiphy *wiphy, struct net_device *dev,
530 struct beacon_parameters *params)
532 struct ieee80211_sub_if_data *sdata;
533 struct beacon_data *old;
535 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
537 old = sdata->u.ap.beacon;
539 if (old)
540 return -EALREADY;
542 return ieee80211_config_beacon(sdata, params);
545 static int ieee80211_set_beacon(struct wiphy *wiphy, struct net_device *dev,
546 struct beacon_parameters *params)
548 struct ieee80211_sub_if_data *sdata;
549 struct beacon_data *old;
551 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
553 old = sdata->u.ap.beacon;
555 if (!old)
556 return -ENOENT;
558 return ieee80211_config_beacon(sdata, params);
561 static int ieee80211_del_beacon(struct wiphy *wiphy, struct net_device *dev)
563 struct ieee80211_sub_if_data *sdata;
564 struct beacon_data *old;
566 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
568 old = sdata->u.ap.beacon;
570 if (!old)
571 return -ENOENT;
573 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
574 synchronize_rcu();
575 kfree(old);
577 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
578 return 0;
581 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
582 struct iapp_layer2_update {
583 u8 da[ETH_ALEN]; /* broadcast */
584 u8 sa[ETH_ALEN]; /* STA addr */
585 __be16 len; /* 6 */
586 u8 dsap; /* 0 */
587 u8 ssap; /* 0 */
588 u8 control;
589 u8 xid_info[3];
590 } __attribute__ ((packed));
592 static void ieee80211_send_layer2_update(struct sta_info *sta)
594 struct iapp_layer2_update *msg;
595 struct sk_buff *skb;
597 /* Send Level 2 Update Frame to update forwarding tables in layer 2
598 * bridge devices */
600 skb = dev_alloc_skb(sizeof(*msg));
601 if (!skb)
602 return;
603 msg = (struct iapp_layer2_update *)skb_put(skb, sizeof(*msg));
605 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
606 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
608 memset(msg->da, 0xff, ETH_ALEN);
609 memcpy(msg->sa, sta->sta.addr, ETH_ALEN);
610 msg->len = htons(6);
611 msg->dsap = 0;
612 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
613 msg->control = 0xaf; /* XID response lsb.1111F101.
614 * F=0 (no poll command; unsolicited frame) */
615 msg->xid_info[0] = 0x81; /* XID format identifier */
616 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
617 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
619 skb->dev = sta->sdata->dev;
620 skb->protocol = eth_type_trans(skb, sta->sdata->dev);
621 memset(skb->cb, 0, sizeof(skb->cb));
622 netif_rx(skb);
625 static void sta_apply_parameters(struct ieee80211_local *local,
626 struct sta_info *sta,
627 struct station_parameters *params)
629 u32 rates;
630 int i, j;
631 struct ieee80211_supported_band *sband;
632 struct ieee80211_sub_if_data *sdata = sta->sdata;
634 sband = local->hw.wiphy->bands[local->oper_channel->band];
637 * FIXME: updating the flags is racy when this function is
638 * called from ieee80211_change_station(), this will
639 * be resolved in a future patch.
642 if (params->station_flags & STATION_FLAG_CHANGED) {
643 spin_lock_bh(&sta->lock);
644 sta->flags &= ~WLAN_STA_AUTHORIZED;
645 if (params->station_flags & STATION_FLAG_AUTHORIZED)
646 sta->flags |= WLAN_STA_AUTHORIZED;
648 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
649 if (params->station_flags & STATION_FLAG_SHORT_PREAMBLE)
650 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
652 sta->flags &= ~WLAN_STA_WME;
653 if (params->station_flags & STATION_FLAG_WME)
654 sta->flags |= WLAN_STA_WME;
656 sta->flags &= ~WLAN_STA_MFP;
657 if (params->station_flags & STATION_FLAG_MFP)
658 sta->flags |= WLAN_STA_MFP;
659 spin_unlock_bh(&sta->lock);
663 * FIXME: updating the following information is racy when this
664 * function is called from ieee80211_change_station().
665 * However, all this information should be static so
666 * maybe we should just reject attemps to change it.
669 if (params->aid) {
670 sta->sta.aid = params->aid;
671 if (sta->sta.aid > IEEE80211_MAX_AID)
672 sta->sta.aid = 0; /* XXX: should this be an error? */
675 if (params->listen_interval >= 0)
676 sta->listen_interval = params->listen_interval;
678 if (params->supported_rates) {
679 rates = 0;
681 for (i = 0; i < params->supported_rates_len; i++) {
682 int rate = (params->supported_rates[i] & 0x7f) * 5;
683 for (j = 0; j < sband->n_bitrates; j++) {
684 if (sband->bitrates[j].bitrate == rate)
685 rates |= BIT(j);
688 sta->sta.supp_rates[local->oper_channel->band] = rates;
691 if (params->ht_capa)
692 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
693 params->ht_capa,
694 &sta->sta.ht_cap);
696 if (ieee80211_vif_is_mesh(&sdata->vif) && params->plink_action) {
697 switch (params->plink_action) {
698 case PLINK_ACTION_OPEN:
699 mesh_plink_open(sta);
700 break;
701 case PLINK_ACTION_BLOCK:
702 mesh_plink_block(sta);
703 break;
708 static int ieee80211_add_station(struct wiphy *wiphy, struct net_device *dev,
709 u8 *mac, struct station_parameters *params)
711 struct ieee80211_local *local = wiphy_priv(wiphy);
712 struct sta_info *sta;
713 struct ieee80211_sub_if_data *sdata;
714 int err;
715 int layer2_update;
717 if (params->vlan) {
718 sdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
720 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
721 sdata->vif.type != NL80211_IFTYPE_AP)
722 return -EINVAL;
723 } else
724 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
726 if (compare_ether_addr(mac, dev->dev_addr) == 0)
727 return -EINVAL;
729 if (is_multicast_ether_addr(mac))
730 return -EINVAL;
732 sta = sta_info_alloc(sdata, mac, GFP_KERNEL);
733 if (!sta)
734 return -ENOMEM;
736 sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
738 sta_apply_parameters(local, sta, params);
740 rate_control_rate_init(sta);
742 layer2_update = sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
743 sdata->vif.type == NL80211_IFTYPE_AP;
745 rcu_read_lock();
747 err = sta_info_insert(sta);
748 if (err) {
749 /* STA has been freed */
750 if (err == -EEXIST && layer2_update) {
751 /* Need to update layer 2 devices on reassociation */
752 sta = sta_info_get(local, mac);
753 if (sta)
754 ieee80211_send_layer2_update(sta);
756 rcu_read_unlock();
757 return err;
760 if (layer2_update)
761 ieee80211_send_layer2_update(sta);
763 rcu_read_unlock();
765 return 0;
768 static int ieee80211_del_station(struct wiphy *wiphy, struct net_device *dev,
769 u8 *mac)
771 struct ieee80211_local *local = wiphy_priv(wiphy);
772 struct ieee80211_sub_if_data *sdata;
773 struct sta_info *sta;
775 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
777 if (mac) {
778 rcu_read_lock();
780 /* XXX: get sta belonging to dev */
781 sta = sta_info_get(local, mac);
782 if (!sta) {
783 rcu_read_unlock();
784 return -ENOENT;
787 sta_info_unlink(&sta);
788 rcu_read_unlock();
790 sta_info_destroy(sta);
791 } else
792 sta_info_flush(local, sdata);
794 return 0;
797 static int ieee80211_change_station(struct wiphy *wiphy,
798 struct net_device *dev,
799 u8 *mac,
800 struct station_parameters *params)
802 struct ieee80211_local *local = wiphy_priv(wiphy);
803 struct sta_info *sta;
804 struct ieee80211_sub_if_data *vlansdata;
806 rcu_read_lock();
808 /* XXX: get sta belonging to dev */
809 sta = sta_info_get(local, mac);
810 if (!sta) {
811 rcu_read_unlock();
812 return -ENOENT;
815 if (params->vlan && params->vlan != sta->sdata->dev) {
816 vlansdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
818 if (vlansdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
819 vlansdata->vif.type != NL80211_IFTYPE_AP) {
820 rcu_read_unlock();
821 return -EINVAL;
824 sta->sdata = vlansdata;
825 ieee80211_send_layer2_update(sta);
828 sta_apply_parameters(local, sta, params);
830 rcu_read_unlock();
832 return 0;
835 #ifdef CONFIG_MAC80211_MESH
836 static int ieee80211_add_mpath(struct wiphy *wiphy, struct net_device *dev,
837 u8 *dst, u8 *next_hop)
839 struct ieee80211_local *local = wiphy_priv(wiphy);
840 struct ieee80211_sub_if_data *sdata;
841 struct mesh_path *mpath;
842 struct sta_info *sta;
843 int err;
845 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
847 rcu_read_lock();
848 sta = sta_info_get(local, next_hop);
849 if (!sta) {
850 rcu_read_unlock();
851 return -ENOENT;
854 err = mesh_path_add(dst, sdata);
855 if (err) {
856 rcu_read_unlock();
857 return err;
860 mpath = mesh_path_lookup(dst, sdata);
861 if (!mpath) {
862 rcu_read_unlock();
863 return -ENXIO;
865 mesh_path_fix_nexthop(mpath, sta);
867 rcu_read_unlock();
868 return 0;
871 static int ieee80211_del_mpath(struct wiphy *wiphy, struct net_device *dev,
872 u8 *dst)
874 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
876 if (dst)
877 return mesh_path_del(dst, sdata);
879 mesh_path_flush(sdata);
880 return 0;
883 static int ieee80211_change_mpath(struct wiphy *wiphy,
884 struct net_device *dev,
885 u8 *dst, u8 *next_hop)
887 struct ieee80211_local *local = wiphy_priv(wiphy);
888 struct ieee80211_sub_if_data *sdata;
889 struct mesh_path *mpath;
890 struct sta_info *sta;
892 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
894 rcu_read_lock();
896 sta = sta_info_get(local, next_hop);
897 if (!sta) {
898 rcu_read_unlock();
899 return -ENOENT;
902 mpath = mesh_path_lookup(dst, sdata);
903 if (!mpath) {
904 rcu_read_unlock();
905 return -ENOENT;
908 mesh_path_fix_nexthop(mpath, sta);
910 rcu_read_unlock();
911 return 0;
914 static void mpath_set_pinfo(struct mesh_path *mpath, u8 *next_hop,
915 struct mpath_info *pinfo)
917 if (mpath->next_hop)
918 memcpy(next_hop, mpath->next_hop->sta.addr, ETH_ALEN);
919 else
920 memset(next_hop, 0, ETH_ALEN);
922 pinfo->filled = MPATH_INFO_FRAME_QLEN |
923 MPATH_INFO_DSN |
924 MPATH_INFO_METRIC |
925 MPATH_INFO_EXPTIME |
926 MPATH_INFO_DISCOVERY_TIMEOUT |
927 MPATH_INFO_DISCOVERY_RETRIES |
928 MPATH_INFO_FLAGS;
930 pinfo->frame_qlen = mpath->frame_queue.qlen;
931 pinfo->dsn = mpath->dsn;
932 pinfo->metric = mpath->metric;
933 if (time_before(jiffies, mpath->exp_time))
934 pinfo->exptime = jiffies_to_msecs(mpath->exp_time - jiffies);
935 pinfo->discovery_timeout =
936 jiffies_to_msecs(mpath->discovery_timeout);
937 pinfo->discovery_retries = mpath->discovery_retries;
938 pinfo->flags = 0;
939 if (mpath->flags & MESH_PATH_ACTIVE)
940 pinfo->flags |= NL80211_MPATH_FLAG_ACTIVE;
941 if (mpath->flags & MESH_PATH_RESOLVING)
942 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
943 if (mpath->flags & MESH_PATH_DSN_VALID)
944 pinfo->flags |= NL80211_MPATH_FLAG_DSN_VALID;
945 if (mpath->flags & MESH_PATH_FIXED)
946 pinfo->flags |= NL80211_MPATH_FLAG_FIXED;
947 if (mpath->flags & MESH_PATH_RESOLVING)
948 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
950 pinfo->flags = mpath->flags;
953 static int ieee80211_get_mpath(struct wiphy *wiphy, struct net_device *dev,
954 u8 *dst, u8 *next_hop, struct mpath_info *pinfo)
957 struct ieee80211_sub_if_data *sdata;
958 struct mesh_path *mpath;
960 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
962 rcu_read_lock();
963 mpath = mesh_path_lookup(dst, sdata);
964 if (!mpath) {
965 rcu_read_unlock();
966 return -ENOENT;
968 memcpy(dst, mpath->dst, ETH_ALEN);
969 mpath_set_pinfo(mpath, next_hop, pinfo);
970 rcu_read_unlock();
971 return 0;
974 static int ieee80211_dump_mpath(struct wiphy *wiphy, struct net_device *dev,
975 int idx, u8 *dst, u8 *next_hop,
976 struct mpath_info *pinfo)
978 struct ieee80211_sub_if_data *sdata;
979 struct mesh_path *mpath;
981 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
983 rcu_read_lock();
984 mpath = mesh_path_lookup_by_idx(idx, sdata);
985 if (!mpath) {
986 rcu_read_unlock();
987 return -ENOENT;
989 memcpy(dst, mpath->dst, ETH_ALEN);
990 mpath_set_pinfo(mpath, next_hop, pinfo);
991 rcu_read_unlock();
992 return 0;
995 static int ieee80211_get_mesh_params(struct wiphy *wiphy,
996 struct net_device *dev,
997 struct mesh_config *conf)
999 struct ieee80211_sub_if_data *sdata;
1000 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1002 memcpy(conf, &(sdata->u.mesh.mshcfg), sizeof(struct mesh_config));
1003 return 0;
1006 static inline bool _chg_mesh_attr(enum nl80211_meshconf_params parm, u32 mask)
1008 return (mask >> (parm-1)) & 0x1;
1011 static int ieee80211_set_mesh_params(struct wiphy *wiphy,
1012 struct net_device *dev,
1013 const struct mesh_config *nconf, u32 mask)
1015 struct mesh_config *conf;
1016 struct ieee80211_sub_if_data *sdata;
1017 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1019 /* Set the config options which we are interested in setting */
1020 conf = &(sdata->u.mesh.mshcfg);
1021 if (_chg_mesh_attr(NL80211_MESHCONF_RETRY_TIMEOUT, mask))
1022 conf->dot11MeshRetryTimeout = nconf->dot11MeshRetryTimeout;
1023 if (_chg_mesh_attr(NL80211_MESHCONF_CONFIRM_TIMEOUT, mask))
1024 conf->dot11MeshConfirmTimeout = nconf->dot11MeshConfirmTimeout;
1025 if (_chg_mesh_attr(NL80211_MESHCONF_HOLDING_TIMEOUT, mask))
1026 conf->dot11MeshHoldingTimeout = nconf->dot11MeshHoldingTimeout;
1027 if (_chg_mesh_attr(NL80211_MESHCONF_MAX_PEER_LINKS, mask))
1028 conf->dot11MeshMaxPeerLinks = nconf->dot11MeshMaxPeerLinks;
1029 if (_chg_mesh_attr(NL80211_MESHCONF_MAX_RETRIES, mask))
1030 conf->dot11MeshMaxRetries = nconf->dot11MeshMaxRetries;
1031 if (_chg_mesh_attr(NL80211_MESHCONF_TTL, mask))
1032 conf->dot11MeshTTL = nconf->dot11MeshTTL;
1033 if (_chg_mesh_attr(NL80211_MESHCONF_AUTO_OPEN_PLINKS, mask))
1034 conf->auto_open_plinks = nconf->auto_open_plinks;
1035 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES, mask))
1036 conf->dot11MeshHWMPmaxPREQretries =
1037 nconf->dot11MeshHWMPmaxPREQretries;
1038 if (_chg_mesh_attr(NL80211_MESHCONF_PATH_REFRESH_TIME, mask))
1039 conf->path_refresh_time = nconf->path_refresh_time;
1040 if (_chg_mesh_attr(NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT, mask))
1041 conf->min_discovery_timeout = nconf->min_discovery_timeout;
1042 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT, mask))
1043 conf->dot11MeshHWMPactivePathTimeout =
1044 nconf->dot11MeshHWMPactivePathTimeout;
1045 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL, mask))
1046 conf->dot11MeshHWMPpreqMinInterval =
1047 nconf->dot11MeshHWMPpreqMinInterval;
1048 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
1049 mask))
1050 conf->dot11MeshHWMPnetDiameterTraversalTime =
1051 nconf->dot11MeshHWMPnetDiameterTraversalTime;
1052 return 0;
1055 #endif
1057 static int ieee80211_change_bss(struct wiphy *wiphy,
1058 struct net_device *dev,
1059 struct bss_parameters *params)
1061 struct ieee80211_sub_if_data *sdata;
1062 u32 changed = 0;
1064 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1066 if (params->use_cts_prot >= 0) {
1067 sdata->vif.bss_conf.use_cts_prot = params->use_cts_prot;
1068 changed |= BSS_CHANGED_ERP_CTS_PROT;
1070 if (params->use_short_preamble >= 0) {
1071 sdata->vif.bss_conf.use_short_preamble =
1072 params->use_short_preamble;
1073 changed |= BSS_CHANGED_ERP_PREAMBLE;
1075 if (params->use_short_slot_time >= 0) {
1076 sdata->vif.bss_conf.use_short_slot =
1077 params->use_short_slot_time;
1078 changed |= BSS_CHANGED_ERP_SLOT;
1081 if (params->basic_rates) {
1082 int i, j;
1083 u32 rates = 0;
1084 struct ieee80211_local *local = wiphy_priv(wiphy);
1085 struct ieee80211_supported_band *sband =
1086 wiphy->bands[local->oper_channel->band];
1088 for (i = 0; i < params->basic_rates_len; i++) {
1089 int rate = (params->basic_rates[i] & 0x7f) * 5;
1090 for (j = 0; j < sband->n_bitrates; j++) {
1091 if (sband->bitrates[j].bitrate == rate)
1092 rates |= BIT(j);
1095 sdata->vif.bss_conf.basic_rates = rates;
1096 changed |= BSS_CHANGED_BASIC_RATES;
1099 ieee80211_bss_info_change_notify(sdata, changed);
1101 return 0;
1104 static int ieee80211_set_txq_params(struct wiphy *wiphy,
1105 struct ieee80211_txq_params *params)
1107 struct ieee80211_local *local = wiphy_priv(wiphy);
1108 struct ieee80211_tx_queue_params p;
1110 if (!local->ops->conf_tx)
1111 return -EOPNOTSUPP;
1113 memset(&p, 0, sizeof(p));
1114 p.aifs = params->aifs;
1115 p.cw_max = params->cwmax;
1116 p.cw_min = params->cwmin;
1117 p.txop = params->txop;
1118 if (local->ops->conf_tx(local_to_hw(local), params->queue, &p)) {
1119 printk(KERN_DEBUG "%s: failed to set TX queue "
1120 "parameters for queue %d\n", local->mdev->name,
1121 params->queue);
1122 return -EINVAL;
1125 return 0;
1128 static int ieee80211_set_channel(struct wiphy *wiphy,
1129 struct ieee80211_channel *chan,
1130 enum nl80211_channel_type channel_type)
1132 struct ieee80211_local *local = wiphy_priv(wiphy);
1134 local->oper_channel = chan;
1135 local->oper_channel_type = channel_type;
1137 return ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
1140 #ifdef CONFIG_PM
1141 static int ieee80211_suspend(struct wiphy *wiphy)
1143 return __ieee80211_suspend(wiphy_priv(wiphy));
1146 static int ieee80211_resume(struct wiphy *wiphy)
1148 return __ieee80211_resume(wiphy_priv(wiphy));
1150 #else
1151 #define ieee80211_suspend NULL
1152 #define ieee80211_resume NULL
1153 #endif
1155 static int ieee80211_scan(struct wiphy *wiphy,
1156 struct net_device *dev,
1157 struct cfg80211_scan_request *req)
1159 struct ieee80211_sub_if_data *sdata;
1161 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1163 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
1164 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
1165 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
1166 (sdata->vif.type != NL80211_IFTYPE_AP || sdata->u.ap.beacon))
1167 return -EOPNOTSUPP;
1169 return ieee80211_request_scan(sdata, req);
1172 static int ieee80211_auth(struct wiphy *wiphy, struct net_device *dev,
1173 struct cfg80211_auth_request *req)
1175 struct ieee80211_sub_if_data *sdata;
1177 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1179 switch (req->auth_type) {
1180 case NL80211_AUTHTYPE_OPEN_SYSTEM:
1181 sdata->u.mgd.auth_algs = IEEE80211_AUTH_ALG_OPEN;
1182 break;
1183 case NL80211_AUTHTYPE_SHARED_KEY:
1184 sdata->u.mgd.auth_algs = IEEE80211_AUTH_ALG_SHARED_KEY;
1185 break;
1186 case NL80211_AUTHTYPE_FT:
1187 sdata->u.mgd.auth_algs = IEEE80211_AUTH_ALG_FT;
1188 break;
1189 case NL80211_AUTHTYPE_NETWORK_EAP:
1190 sdata->u.mgd.auth_algs = IEEE80211_AUTH_ALG_LEAP;
1191 break;
1192 default:
1193 return -EOPNOTSUPP;
1196 memcpy(sdata->u.mgd.bssid, req->peer_addr, ETH_ALEN);
1197 sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
1198 sdata->u.mgd.flags |= IEEE80211_STA_BSSID_SET;
1200 /* TODO: req->chan */
1201 sdata->u.mgd.flags |= IEEE80211_STA_AUTO_CHANNEL_SEL;
1203 if (req->ssid) {
1204 sdata->u.mgd.flags |= IEEE80211_STA_SSID_SET;
1205 memcpy(sdata->u.mgd.ssid, req->ssid, req->ssid_len);
1206 sdata->u.mgd.ssid_len = req->ssid_len;
1207 sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_SSID_SEL;
1210 kfree(sdata->u.mgd.sme_auth_ie);
1211 sdata->u.mgd.sme_auth_ie = NULL;
1212 sdata->u.mgd.sme_auth_ie_len = 0;
1213 if (req->ie) {
1214 sdata->u.mgd.sme_auth_ie = kmalloc(req->ie_len, GFP_KERNEL);
1215 if (sdata->u.mgd.sme_auth_ie == NULL)
1216 return -ENOMEM;
1217 memcpy(sdata->u.mgd.sme_auth_ie, req->ie, req->ie_len);
1218 sdata->u.mgd.sme_auth_ie_len = req->ie_len;
1221 sdata->u.mgd.flags |= IEEE80211_STA_EXT_SME;
1222 sdata->u.mgd.state = IEEE80211_STA_MLME_DIRECT_PROBE;
1223 ieee80211_sta_req_auth(sdata);
1224 return 0;
1227 static int ieee80211_assoc(struct wiphy *wiphy, struct net_device *dev,
1228 struct cfg80211_assoc_request *req)
1230 struct ieee80211_sub_if_data *sdata;
1231 int ret;
1233 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1235 if (memcmp(sdata->u.mgd.bssid, req->peer_addr, ETH_ALEN) != 0 ||
1236 !(sdata->u.mgd.flags & IEEE80211_STA_AUTHENTICATED))
1237 return -ENOLINK; /* not authenticated */
1239 sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
1240 sdata->u.mgd.flags |= IEEE80211_STA_BSSID_SET;
1242 /* TODO: req->chan */
1243 sdata->u.mgd.flags |= IEEE80211_STA_AUTO_CHANNEL_SEL;
1245 if (req->ssid) {
1246 sdata->u.mgd.flags |= IEEE80211_STA_SSID_SET;
1247 memcpy(sdata->u.mgd.ssid, req->ssid, req->ssid_len);
1248 sdata->u.mgd.ssid_len = req->ssid_len;
1249 sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_SSID_SEL;
1250 } else
1251 sdata->u.mgd.flags |= IEEE80211_STA_AUTO_SSID_SEL;
1253 ret = ieee80211_sta_set_extra_ie(sdata, req->ie, req->ie_len);
1254 if (ret)
1255 return ret;
1257 sdata->u.mgd.flags |= IEEE80211_STA_EXT_SME;
1258 sdata->u.mgd.state = IEEE80211_STA_MLME_ASSOCIATE;
1259 ieee80211_sta_req_auth(sdata);
1260 return 0;
1263 static int ieee80211_deauth(struct wiphy *wiphy, struct net_device *dev,
1264 struct cfg80211_deauth_request *req)
1266 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1268 /* TODO: req->ie, req->peer_addr */
1269 return ieee80211_sta_deauthenticate(sdata, req->reason_code);
1272 static int ieee80211_disassoc(struct wiphy *wiphy, struct net_device *dev,
1273 struct cfg80211_disassoc_request *req)
1275 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1277 /* TODO: req->ie, req->peer_addr */
1278 return ieee80211_sta_disassociate(sdata, req->reason_code);
1281 static int ieee80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1282 struct cfg80211_ibss_params *params)
1284 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1286 return ieee80211_ibss_join(sdata, params);
1289 static int ieee80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
1291 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1293 return ieee80211_ibss_leave(sdata);
1296 static int ieee80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1298 struct ieee80211_local *local = wiphy_priv(wiphy);
1300 if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1301 int err;
1303 if (local->ops->set_rts_threshold) {
1304 err = local->ops->set_rts_threshold(
1305 local_to_hw(local), wiphy->rts_threshold);
1306 if (err)
1307 return err;
1311 if (changed & WIPHY_PARAM_RETRY_SHORT)
1312 local->hw.conf.short_frame_max_tx_count = wiphy->retry_short;
1313 if (changed & WIPHY_PARAM_RETRY_LONG)
1314 local->hw.conf.long_frame_max_tx_count = wiphy->retry_long;
1315 if (changed &
1316 (WIPHY_PARAM_RETRY_SHORT | WIPHY_PARAM_RETRY_LONG))
1317 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_RETRY_LIMITS);
1319 return 0;
1322 struct cfg80211_ops mac80211_config_ops = {
1323 .add_virtual_intf = ieee80211_add_iface,
1324 .del_virtual_intf = ieee80211_del_iface,
1325 .change_virtual_intf = ieee80211_change_iface,
1326 .add_key = ieee80211_add_key,
1327 .del_key = ieee80211_del_key,
1328 .get_key = ieee80211_get_key,
1329 .set_default_key = ieee80211_config_default_key,
1330 .set_default_mgmt_key = ieee80211_config_default_mgmt_key,
1331 .add_beacon = ieee80211_add_beacon,
1332 .set_beacon = ieee80211_set_beacon,
1333 .del_beacon = ieee80211_del_beacon,
1334 .add_station = ieee80211_add_station,
1335 .del_station = ieee80211_del_station,
1336 .change_station = ieee80211_change_station,
1337 .get_station = ieee80211_get_station,
1338 .dump_station = ieee80211_dump_station,
1339 #ifdef CONFIG_MAC80211_MESH
1340 .add_mpath = ieee80211_add_mpath,
1341 .del_mpath = ieee80211_del_mpath,
1342 .change_mpath = ieee80211_change_mpath,
1343 .get_mpath = ieee80211_get_mpath,
1344 .dump_mpath = ieee80211_dump_mpath,
1345 .set_mesh_params = ieee80211_set_mesh_params,
1346 .get_mesh_params = ieee80211_get_mesh_params,
1347 #endif
1348 .change_bss = ieee80211_change_bss,
1349 .set_txq_params = ieee80211_set_txq_params,
1350 .set_channel = ieee80211_set_channel,
1351 .suspend = ieee80211_suspend,
1352 .resume = ieee80211_resume,
1353 .scan = ieee80211_scan,
1354 .auth = ieee80211_auth,
1355 .assoc = ieee80211_assoc,
1356 .deauth = ieee80211_deauth,
1357 .disassoc = ieee80211_disassoc,
1358 .join_ibss = ieee80211_join_ibss,
1359 .leave_ibss = ieee80211_leave_ibss,
1360 .set_wiphy_params = ieee80211_set_wiphy_params,