2 * mac80211 configuration hooks for cfg80211
4 * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net>
6 * This file is GPLv2 as found in COPYING.
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"
17 #include "ieee80211_rate.h"
19 static enum ieee80211_if_types
20 nl80211_type_to_mac80211_type(enum nl80211_iftype type
)
23 case NL80211_IFTYPE_UNSPECIFIED
:
24 return IEEE80211_IF_TYPE_STA
;
25 case NL80211_IFTYPE_ADHOC
:
26 return IEEE80211_IF_TYPE_IBSS
;
27 case NL80211_IFTYPE_STATION
:
28 return IEEE80211_IF_TYPE_STA
;
29 case NL80211_IFTYPE_MONITOR
:
30 return IEEE80211_IF_TYPE_MNTR
;
32 return IEEE80211_IF_TYPE_INVALID
;
36 static int ieee80211_add_iface(struct wiphy
*wiphy
, char *name
,
37 enum nl80211_iftype type
, u32
*flags
)
39 struct ieee80211_local
*local
= wiphy_priv(wiphy
);
40 enum ieee80211_if_types itype
;
41 struct net_device
*dev
;
42 struct ieee80211_sub_if_data
*sdata
;
45 if (unlikely(local
->reg_state
!= IEEE80211_DEV_REGISTERED
))
48 itype
= nl80211_type_to_mac80211_type(type
);
49 if (itype
== IEEE80211_IF_TYPE_INVALID
)
52 err
= ieee80211_if_add(local
->mdev
, name
, &dev
, itype
);
53 if (err
|| itype
!= IEEE80211_IF_TYPE_MNTR
|| !flags
)
56 sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
57 sdata
->u
.mntr_flags
= *flags
;
61 static int ieee80211_del_iface(struct wiphy
*wiphy
, int ifindex
)
63 struct ieee80211_local
*local
= wiphy_priv(wiphy
);
64 struct net_device
*dev
;
67 if (unlikely(local
->reg_state
!= IEEE80211_DEV_REGISTERED
))
70 /* we're under RTNL */
71 dev
= __dev_get_by_index(&init_net
, ifindex
);
77 return ieee80211_if_remove(local
->mdev
, name
, -1);
80 static int ieee80211_change_iface(struct wiphy
*wiphy
, int ifindex
,
81 enum nl80211_iftype type
, u32
*flags
)
83 struct ieee80211_local
*local
= wiphy_priv(wiphy
);
84 struct net_device
*dev
;
85 enum ieee80211_if_types itype
;
86 struct ieee80211_sub_if_data
*sdata
;
88 if (unlikely(local
->reg_state
!= IEEE80211_DEV_REGISTERED
))
91 /* we're under RTNL */
92 dev
= __dev_get_by_index(&init_net
, ifindex
);
96 if (netif_running(dev
))
99 itype
= nl80211_type_to_mac80211_type(type
);
100 if (itype
== IEEE80211_IF_TYPE_INVALID
)
103 sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
105 if (sdata
->vif
.type
== IEEE80211_IF_TYPE_VLAN
)
108 ieee80211_if_reinit(dev
);
109 ieee80211_if_set_type(dev
, itype
);
111 if (sdata
->vif
.type
!= IEEE80211_IF_TYPE_MNTR
|| !flags
)
114 sdata
->u
.mntr_flags
= *flags
;
118 static int ieee80211_add_key(struct wiphy
*wiphy
, struct net_device
*dev
,
119 u8 key_idx
, u8
*mac_addr
,
120 struct key_params
*params
)
122 struct ieee80211_sub_if_data
*sdata
;
123 struct sta_info
*sta
= NULL
;
124 enum ieee80211_key_alg alg
;
127 sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
129 switch (params
->cipher
) {
130 case WLAN_CIPHER_SUITE_WEP40
:
131 case WLAN_CIPHER_SUITE_WEP104
:
134 case WLAN_CIPHER_SUITE_TKIP
:
137 case WLAN_CIPHER_SUITE_CCMP
:
145 sta
= sta_info_get(sdata
->local
, mac_addr
);
151 if (!ieee80211_key_alloc(sdata
, sta
, alg
, key_idx
,
152 params
->key_len
, params
->key
))
161 static int ieee80211_del_key(struct wiphy
*wiphy
, struct net_device
*dev
,
162 u8 key_idx
, u8
*mac_addr
)
164 struct ieee80211_sub_if_data
*sdata
;
165 struct sta_info
*sta
;
168 sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
171 sta
= sta_info_get(sdata
->local
, mac_addr
);
177 ieee80211_key_free(sta
->key
);
185 if (!sdata
->keys
[key_idx
])
188 ieee80211_key_free(sdata
->keys
[key_idx
]);
193 static int ieee80211_get_key(struct wiphy
*wiphy
, struct net_device
*dev
,
194 u8 key_idx
, u8
*mac_addr
, void *cookie
,
195 void (*callback
)(void *cookie
,
196 struct key_params
*params
))
198 struct ieee80211_sub_if_data
*sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
199 struct sta_info
*sta
= NULL
;
201 struct key_params params
;
202 struct ieee80211_key
*key
;
208 sta
= sta_info_get(sdata
->local
, mac_addr
);
214 key
= sdata
->keys
[key_idx
];
219 memset(¶ms
, 0, sizeof(params
));
221 switch (key
->conf
.alg
) {
223 params
.cipher
= WLAN_CIPHER_SUITE_TKIP
;
225 iv32
= key
->u
.tkip
.iv32
;
226 iv16
= key
->u
.tkip
.iv16
;
228 if (key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
&&
229 sdata
->local
->ops
->get_tkip_seq
)
230 sdata
->local
->ops
->get_tkip_seq(
231 local_to_hw(sdata
->local
),
232 key
->conf
.hw_key_idx
,
235 seq
[0] = iv16
& 0xff;
236 seq
[1] = (iv16
>> 8) & 0xff;
237 seq
[2] = iv32
& 0xff;
238 seq
[3] = (iv32
>> 8) & 0xff;
239 seq
[4] = (iv32
>> 16) & 0xff;
240 seq
[5] = (iv32
>> 24) & 0xff;
245 params
.cipher
= WLAN_CIPHER_SUITE_CCMP
;
246 seq
[0] = key
->u
.ccmp
.tx_pn
[5];
247 seq
[1] = key
->u
.ccmp
.tx_pn
[4];
248 seq
[2] = key
->u
.ccmp
.tx_pn
[3];
249 seq
[3] = key
->u
.ccmp
.tx_pn
[2];
250 seq
[4] = key
->u
.ccmp
.tx_pn
[1];
251 seq
[5] = key
->u
.ccmp
.tx_pn
[0];
256 if (key
->conf
.keylen
== 5)
257 params
.cipher
= WLAN_CIPHER_SUITE_WEP40
;
259 params
.cipher
= WLAN_CIPHER_SUITE_WEP104
;
263 params
.key
= key
->conf
.key
;
264 params
.key_len
= key
->conf
.keylen
;
266 callback(cookie
, ¶ms
);
275 static int ieee80211_config_default_key(struct wiphy
*wiphy
,
276 struct net_device
*dev
,
279 struct ieee80211_sub_if_data
*sdata
;
281 sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
282 ieee80211_set_default_key(sdata
, key_idx
);
287 static int ieee80211_get_station(struct wiphy
*wiphy
, struct net_device
*dev
,
288 u8
*mac
, struct station_stats
*stats
)
290 struct ieee80211_local
*local
= wdev_priv(dev
->ieee80211_ptr
);
291 struct sta_info
*sta
;
293 sta
= sta_info_get(local
, mac
);
297 /* XXX: verify sta->dev == dev */
299 stats
->filled
= STATION_STAT_INACTIVE_TIME
|
300 STATION_STAT_RX_BYTES
|
301 STATION_STAT_TX_BYTES
;
303 stats
->inactive_time
= jiffies_to_msecs(jiffies
- sta
->last_rx
);
304 stats
->rx_bytes
= sta
->rx_bytes
;
305 stats
->tx_bytes
= sta
->tx_bytes
;
313 * This handles both adding a beacon and setting new beacon info
315 static int ieee80211_config_beacon(struct ieee80211_sub_if_data
*sdata
,
316 struct beacon_parameters
*params
)
318 struct beacon_data
*new, *old
;
319 int new_head_len
, new_tail_len
;
323 old
= sdata
->u
.ap
.beacon
;
325 /* head must not be zero-length */
326 if (params
->head
&& !params
->head_len
)
330 * This is a kludge. beacon interval should really be part
331 * of the beacon information.
333 if (params
->interval
) {
334 sdata
->local
->hw
.conf
.beacon_int
= params
->interval
;
335 if (ieee80211_hw_config(sdata
->local
))
338 * We updated some parameter so if below bails out
344 /* Need to have a beacon head if we don't have one yet */
345 if (!params
->head
&& !old
)
348 /* sorry, no way to start beaconing without dtim period */
349 if (!params
->dtim_period
&& !old
)
352 /* new or old head? */
354 new_head_len
= params
->head_len
;
356 new_head_len
= old
->head_len
;
358 /* new or old tail? */
359 if (params
->tail
|| !old
)
360 /* params->tail_len will be zero for !params->tail */
361 new_tail_len
= params
->tail_len
;
363 new_tail_len
= old
->tail_len
;
365 size
= sizeof(*new) + new_head_len
+ new_tail_len
;
367 new = kzalloc(size
, GFP_KERNEL
);
371 /* start filling the new info now */
373 /* new or old dtim period? */
374 if (params
->dtim_period
)
375 new->dtim_period
= params
->dtim_period
;
377 new->dtim_period
= old
->dtim_period
;
380 * pointers go into the block we allocated,
381 * memory is | beacon_data | head | tail |
383 new->head
= ((u8
*) new) + sizeof(*new);
384 new->tail
= new->head
+ new_head_len
;
385 new->head_len
= new_head_len
;
386 new->tail_len
= new_tail_len
;
390 memcpy(new->head
, params
->head
, new_head_len
);
392 memcpy(new->head
, old
->head
, new_head_len
);
394 /* copy in optional tail */
396 memcpy(new->tail
, params
->tail
, new_tail_len
);
399 memcpy(new->tail
, old
->tail
, new_tail_len
);
401 rcu_assign_pointer(sdata
->u
.ap
.beacon
, new);
407 return ieee80211_if_config_beacon(sdata
->dev
);
410 static int ieee80211_add_beacon(struct wiphy
*wiphy
, struct net_device
*dev
,
411 struct beacon_parameters
*params
)
413 struct ieee80211_sub_if_data
*sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
414 struct beacon_data
*old
;
416 if (sdata
->vif
.type
!= IEEE80211_IF_TYPE_AP
)
419 old
= sdata
->u
.ap
.beacon
;
424 return ieee80211_config_beacon(sdata
, params
);
427 static int ieee80211_set_beacon(struct wiphy
*wiphy
, struct net_device
*dev
,
428 struct beacon_parameters
*params
)
430 struct ieee80211_sub_if_data
*sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
431 struct beacon_data
*old
;
433 if (sdata
->vif
.type
!= IEEE80211_IF_TYPE_AP
)
436 old
= sdata
->u
.ap
.beacon
;
441 return ieee80211_config_beacon(sdata
, params
);
444 static int ieee80211_del_beacon(struct wiphy
*wiphy
, struct net_device
*dev
)
446 struct ieee80211_sub_if_data
*sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
447 struct beacon_data
*old
;
449 if (sdata
->vif
.type
!= IEEE80211_IF_TYPE_AP
)
452 old
= sdata
->u
.ap
.beacon
;
457 rcu_assign_pointer(sdata
->u
.ap
.beacon
, NULL
);
461 return ieee80211_if_config_beacon(dev
);
464 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
465 struct iapp_layer2_update
{
466 u8 da
[ETH_ALEN
]; /* broadcast */
467 u8 sa
[ETH_ALEN
]; /* STA addr */
473 } __attribute__ ((packed
));
475 static void ieee80211_send_layer2_update(struct sta_info
*sta
)
477 struct iapp_layer2_update
*msg
;
480 /* Send Level 2 Update Frame to update forwarding tables in layer 2
483 skb
= dev_alloc_skb(sizeof(*msg
));
486 msg
= (struct iapp_layer2_update
*)skb_put(skb
, sizeof(*msg
));
488 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
489 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
491 memset(msg
->da
, 0xff, ETH_ALEN
);
492 memcpy(msg
->sa
, sta
->addr
, ETH_ALEN
);
495 msg
->ssap
= 0x01; /* NULL LSAP, CR Bit: Response */
496 msg
->control
= 0xaf; /* XID response lsb.1111F101.
497 * F=0 (no poll command; unsolicited frame) */
498 msg
->xid_info
[0] = 0x81; /* XID format identifier */
499 msg
->xid_info
[1] = 1; /* LLC types/classes: Type 1 LLC */
500 msg
->xid_info
[2] = 0; /* XID sender's receive window size (RW) */
503 skb
->protocol
= eth_type_trans(skb
, sta
->dev
);
504 memset(skb
->cb
, 0, sizeof(skb
->cb
));
508 static void sta_apply_parameters(struct ieee80211_local
*local
,
509 struct sta_info
*sta
,
510 struct station_parameters
*params
)
514 struct ieee80211_supported_band
*sband
;
516 if (params
->station_flags
& STATION_FLAG_CHANGED
) {
517 sta
->flags
&= ~WLAN_STA_AUTHORIZED
;
518 if (params
->station_flags
& STATION_FLAG_AUTHORIZED
)
519 sta
->flags
|= WLAN_STA_AUTHORIZED
;
521 sta
->flags
&= ~WLAN_STA_SHORT_PREAMBLE
;
522 if (params
->station_flags
& STATION_FLAG_SHORT_PREAMBLE
)
523 sta
->flags
|= WLAN_STA_SHORT_PREAMBLE
;
525 sta
->flags
&= ~WLAN_STA_WME
;
526 if (params
->station_flags
& STATION_FLAG_WME
)
527 sta
->flags
|= WLAN_STA_WME
;
531 sta
->aid
= params
->aid
;
532 if (sta
->aid
> IEEE80211_MAX_AID
)
533 sta
->aid
= 0; /* XXX: should this be an error? */
536 if (params
->listen_interval
>= 0)
537 sta
->listen_interval
= params
->listen_interval
;
539 if (params
->supported_rates
) {
541 sband
= local
->hw
.wiphy
->bands
[local
->oper_channel
->band
];
543 for (i
= 0; i
< params
->supported_rates_len
; i
++) {
544 int rate
= (params
->supported_rates
[i
] & 0x7f) * 5;
545 for (j
= 0; j
< sband
->n_bitrates
; j
++) {
546 if (sband
->bitrates
[j
].bitrate
== rate
)
550 sta
->supp_rates
[local
->oper_channel
->band
] = rates
;
554 static int ieee80211_add_station(struct wiphy
*wiphy
, struct net_device
*dev
,
555 u8
*mac
, struct station_parameters
*params
)
557 struct ieee80211_local
*local
= wdev_priv(dev
->ieee80211_ptr
);
558 struct sta_info
*sta
;
559 struct ieee80211_sub_if_data
*sdata
;
561 /* Prevent a race with changing the rate control algorithm */
562 if (!netif_running(dev
))
565 /* XXX: get sta belonging to dev */
566 sta
= sta_info_get(local
, mac
);
573 sdata
= IEEE80211_DEV_TO_SUB_IF(params
->vlan
);
575 if (sdata
->vif
.type
!= IEEE80211_IF_TYPE_VLAN
||
576 sdata
->vif
.type
!= IEEE80211_IF_TYPE_AP
)
579 sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
581 sta
= sta_info_add(local
, dev
, mac
, GFP_KERNEL
);
585 sta
->dev
= sdata
->dev
;
586 if (sdata
->vif
.type
== IEEE80211_IF_TYPE_VLAN
||
587 sdata
->vif
.type
== IEEE80211_IF_TYPE_AP
)
588 ieee80211_send_layer2_update(sta
);
590 sta
->flags
= WLAN_STA_AUTH
| WLAN_STA_ASSOC
;
592 sta_apply_parameters(local
, sta
, params
);
594 rate_control_rate_init(sta
, local
);
601 static int ieee80211_del_station(struct wiphy
*wiphy
, struct net_device
*dev
,
604 struct ieee80211_local
*local
= wdev_priv(dev
->ieee80211_ptr
);
605 struct sta_info
*sta
;
608 /* XXX: get sta belonging to dev */
609 sta
= sta_info_get(local
, mac
);
616 sta_info_flush(local
, dev
);
621 static int ieee80211_change_station(struct wiphy
*wiphy
,
622 struct net_device
*dev
,
624 struct station_parameters
*params
)
626 struct ieee80211_local
*local
= wdev_priv(dev
->ieee80211_ptr
);
627 struct sta_info
*sta
;
628 struct ieee80211_sub_if_data
*vlansdata
;
630 /* XXX: get sta belonging to dev */
631 sta
= sta_info_get(local
, mac
);
635 if (params
->vlan
&& params
->vlan
!= sta
->dev
) {
636 vlansdata
= IEEE80211_DEV_TO_SUB_IF(params
->vlan
);
638 if (vlansdata
->vif
.type
!= IEEE80211_IF_TYPE_VLAN
||
639 vlansdata
->vif
.type
!= IEEE80211_IF_TYPE_AP
)
642 sta
->dev
= params
->vlan
;
643 ieee80211_send_layer2_update(sta
);
646 sta_apply_parameters(local
, sta
, params
);
653 struct cfg80211_ops mac80211_config_ops
= {
654 .add_virtual_intf
= ieee80211_add_iface
,
655 .del_virtual_intf
= ieee80211_del_iface
,
656 .change_virtual_intf
= ieee80211_change_iface
,
657 .add_key
= ieee80211_add_key
,
658 .del_key
= ieee80211_del_key
,
659 .get_key
= ieee80211_get_key
,
660 .set_default_key
= ieee80211_config_default_key
,
661 .add_beacon
= ieee80211_add_beacon
,
662 .set_beacon
= ieee80211_set_beacon
,
663 .del_beacon
= ieee80211_del_beacon
,
664 .add_station
= ieee80211_add_station
,
665 .del_station
= ieee80211_del_station
,
666 .change_station
= ieee80211_change_station
,
667 .get_station
= ieee80211_get_station
,