2 * Copyright (c) 2008, 2009 open80211s Ltd.
3 * Author: Luis Carlos Cobo <luisca@cozybit.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 #include <linux/kernel.h>
10 #include <linux/random.h>
11 #include "ieee80211_i.h"
15 #ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
16 #define mpl_dbg(fmt, args...) printk(KERN_DEBUG fmt, ##args)
18 #define mpl_dbg(fmt, args...) do { (void)(0); } while (0)
21 #define PLINK_GET_LLID(p) (p + 4)
22 #define PLINK_GET_PLID(p) (p + 6)
24 #define mod_plink_timer(s, t) (mod_timer(&s->plink_timer, \
25 jiffies + HZ * t / 1000))
27 /* Peer link cancel reasons, all subject to ANA approval */
28 #define MESH_LINK_CANCELLED 2
29 #define MESH_MAX_NEIGHBORS 3
30 #define MESH_CAPABILITY_POLICY_VIOLATION 4
31 #define MESH_CLOSE_RCVD 5
32 #define MESH_MAX_RETRIES 6
33 #define MESH_CONFIRM_TIMEOUT 7
34 #define MESH_SECURITY_ROLE_NEGOTIATION_DIFFERS 8
35 #define MESH_SECURITY_AUTHENTICATION_IMPOSSIBLE 9
36 #define MESH_SECURITY_FAILED_VERIFICATION 10
38 #define dot11MeshMaxRetries(s) (s->u.mesh.mshcfg.dot11MeshMaxRetries)
39 #define dot11MeshRetryTimeout(s) (s->u.mesh.mshcfg.dot11MeshRetryTimeout)
40 #define dot11MeshConfirmTimeout(s) (s->u.mesh.mshcfg.dot11MeshConfirmTimeout)
41 #define dot11MeshHoldingTimeout(s) (s->u.mesh.mshcfg.dot11MeshHoldingTimeout)
42 #define dot11MeshMaxPeerLinks(s) (s->u.mesh.mshcfg.dot11MeshMaxPeerLinks)
44 enum plink_frame_type
{
63 void mesh_plink_inc_estab_count(struct ieee80211_sub_if_data
*sdata
)
65 atomic_inc(&sdata
->u
.mesh
.mshstats
.estab_plinks
);
66 mesh_accept_plinks_update(sdata
);
67 ieee80211_bss_info_change_notify(sdata
, BSS_CHANGED_BEACON
);
71 void mesh_plink_dec_estab_count(struct ieee80211_sub_if_data
*sdata
)
73 atomic_dec(&sdata
->u
.mesh
.mshstats
.estab_plinks
);
74 mesh_accept_plinks_update(sdata
);
75 ieee80211_bss_info_change_notify(sdata
, BSS_CHANGED_BEACON
);
79 * mesh_plink_fsm_restart - restart a mesh peer link finite state machine
81 * @sta: mesh peer link to restart
83 * Locking: this function must be called holding sta->lock
85 static inline void mesh_plink_fsm_restart(struct sta_info
*sta
)
87 sta
->plink_state
= PLINK_LISTEN
;
88 sta
->llid
= sta
->plid
= sta
->reason
= 0;
89 sta
->plink_retries
= 0;
93 * NOTE: This is just an alias for sta_info_alloc(), see notes
94 * on it in the lifecycle management section!
96 static struct sta_info
*mesh_plink_alloc(struct ieee80211_sub_if_data
*sdata
,
97 u8
*hw_addr
, u32 rates
)
99 struct ieee80211_local
*local
= sdata
->local
;
100 struct sta_info
*sta
;
102 if (local
->num_sta
>= MESH_MAX_PLINKS
)
105 sta
= sta_info_alloc(sdata
, hw_addr
, GFP_KERNEL
);
109 sta
->flags
= WLAN_STA_AUTHORIZED
;
110 sta
->sta
.supp_rates
[local
->hw
.conf
.channel
->band
] = rates
;
111 rate_control_rate_init(sta
);
117 * mesh_plink_deactivate - deactivate mesh peer link
119 * @sta: mesh peer link to deactivate
121 * All mesh paths with this peer as next hop will be flushed
123 * Locking: the caller must hold sta->lock
125 static void __mesh_plink_deactivate(struct sta_info
*sta
)
127 struct ieee80211_sub_if_data
*sdata
= sta
->sdata
;
129 if (sta
->plink_state
== PLINK_ESTAB
)
130 mesh_plink_dec_estab_count(sdata
);
131 sta
->plink_state
= PLINK_BLOCKED
;
132 mesh_path_flush_by_nexthop(sta
);
136 * __mesh_plink_deactivate - deactivate mesh peer link
138 * @sta: mesh peer link to deactivate
140 * All mesh paths with this peer as next hop will be flushed
142 void mesh_plink_deactivate(struct sta_info
*sta
)
144 spin_lock_bh(&sta
->lock
);
145 __mesh_plink_deactivate(sta
);
146 spin_unlock_bh(&sta
->lock
);
149 static int mesh_plink_frame_tx(struct ieee80211_sub_if_data
*sdata
,
150 enum plink_frame_type action
, u8
*da
, __le16 llid
, __le16 plid
,
152 struct ieee80211_local
*local
= sdata
->local
;
153 struct sk_buff
*skb
= dev_alloc_skb(local
->hw
.extra_tx_headroom
+ 400);
154 struct ieee80211_mgmt
*mgmt
;
155 bool include_plid
= false;
156 static const u8 meshpeeringproto
[] = { 0x00, 0x0F, 0xAC, 0x2A };
162 skb_reserve(skb
, local
->hw
.extra_tx_headroom
);
163 /* 25 is the size of the common mgmt part (24) plus the size of the
164 * common action part (1)
166 mgmt
= (struct ieee80211_mgmt
*)
167 skb_put(skb
, 25 + sizeof(mgmt
->u
.action
.u
.plink_action
));
168 memset(mgmt
, 0, 25 + sizeof(mgmt
->u
.action
.u
.plink_action
));
169 mgmt
->frame_control
= cpu_to_le16(IEEE80211_FTYPE_MGMT
|
170 IEEE80211_STYPE_ACTION
);
171 memcpy(mgmt
->da
, da
, ETH_ALEN
);
172 memcpy(mgmt
->sa
, sdata
->vif
.addr
, ETH_ALEN
);
173 /* BSSID is left zeroed, wildcard value */
174 mgmt
->u
.action
.category
= MESH_PLINK_CATEGORY
;
175 mgmt
->u
.action
.u
.plink_action
.action_code
= action
;
177 if (action
== PLINK_CLOSE
)
178 mgmt
->u
.action
.u
.plink_action
.aux
= reason
;
180 mgmt
->u
.action
.u
.plink_action
.aux
= cpu_to_le16(0x0);
181 if (action
== PLINK_CONFIRM
) {
182 pos
= skb_put(skb
, 4);
183 /* two-byte status code followed by two-byte AID */
185 memcpy(pos
+ 2, &plid
, 2);
187 mesh_mgmt_ies_add(skb
, sdata
);
190 /* Add Peer Link Management element */
210 pos
= skb_put(skb
, 2 + ie_len
);
211 *pos
++ = WLAN_EID_PEER_LINK
;
213 memcpy(pos
, meshpeeringproto
, sizeof(meshpeeringproto
));
215 memcpy(pos
, &llid
, 2);
218 memcpy(pos
, &plid
, 2);
220 if (action
== PLINK_CLOSE
) {
222 memcpy(pos
, &reason
, 2);
225 ieee80211_tx_skb(sdata
, skb
);
229 void mesh_neighbour_update(u8
*hw_addr
, u32 rates
, struct ieee80211_sub_if_data
*sdata
,
230 bool peer_accepting_plinks
)
232 struct ieee80211_local
*local
= sdata
->local
;
233 struct sta_info
*sta
;
237 sta
= sta_info_get(sdata
, hw_addr
);
241 sta
= mesh_plink_alloc(sdata
, hw_addr
, rates
);
244 if (sta_info_insert_rcu(sta
)) {
250 sta
->last_rx
= jiffies
;
251 sta
->sta
.supp_rates
[local
->hw
.conf
.channel
->band
] = rates
;
252 if (peer_accepting_plinks
&& sta
->plink_state
== PLINK_LISTEN
&&
253 sdata
->u
.mesh
.accepting_plinks
&&
254 sdata
->u
.mesh
.mshcfg
.auto_open_plinks
)
255 mesh_plink_open(sta
);
260 static void mesh_plink_timer(unsigned long data
)
262 struct sta_info
*sta
;
263 __le16 llid
, plid
, reason
;
264 struct ieee80211_sub_if_data
*sdata
;
267 * This STA is valid because sta_info_destroy() will
268 * del_timer_sync() this timer after having made sure
269 * it cannot be readded (by deleting the plink.)
271 sta
= (struct sta_info
*) data
;
273 if (sta
->sdata
->local
->quiescing
) {
274 sta
->plink_timer_was_running
= true;
278 spin_lock_bh(&sta
->lock
);
279 if (sta
->ignore_plink_timer
) {
280 sta
->ignore_plink_timer
= false;
281 spin_unlock_bh(&sta
->lock
);
284 mpl_dbg("Mesh plink timer for %pM fired on state %d\n",
285 sta
->sta
.addr
, sta
->plink_state
);
291 switch (sta
->plink_state
) {
295 if (sta
->plink_retries
< dot11MeshMaxRetries(sdata
)) {
297 mpl_dbg("Mesh plink for %pM (retry, timeout): %d %d\n",
298 sta
->sta
.addr
, sta
->plink_retries
,
300 get_random_bytes(&rand
, sizeof(u32
));
301 sta
->plink_timeout
= sta
->plink_timeout
+
302 rand
% sta
->plink_timeout
;
303 ++sta
->plink_retries
;
304 mod_plink_timer(sta
, sta
->plink_timeout
);
305 spin_unlock_bh(&sta
->lock
);
306 mesh_plink_frame_tx(sdata
, PLINK_OPEN
, sta
->sta
.addr
, llid
,
310 reason
= cpu_to_le16(MESH_MAX_RETRIES
);
311 /* fall through on else */
315 reason
= cpu_to_le16(MESH_CONFIRM_TIMEOUT
);
316 sta
->plink_state
= PLINK_HOLDING
;
317 mod_plink_timer(sta
, dot11MeshHoldingTimeout(sdata
));
318 spin_unlock_bh(&sta
->lock
);
319 mesh_plink_frame_tx(sdata
, PLINK_CLOSE
, sta
->sta
.addr
, llid
, plid
,
324 del_timer(&sta
->plink_timer
);
325 mesh_plink_fsm_restart(sta
);
326 spin_unlock_bh(&sta
->lock
);
329 spin_unlock_bh(&sta
->lock
);
335 void mesh_plink_quiesce(struct sta_info
*sta
)
337 if (del_timer_sync(&sta
->plink_timer
))
338 sta
->plink_timer_was_running
= true;
341 void mesh_plink_restart(struct sta_info
*sta
)
343 if (sta
->plink_timer_was_running
) {
344 add_timer(&sta
->plink_timer
);
345 sta
->plink_timer_was_running
= false;
350 static inline void mesh_plink_timer_set(struct sta_info
*sta
, int timeout
)
352 sta
->plink_timer
.expires
= jiffies
+ (HZ
* timeout
/ 1000);
353 sta
->plink_timer
.data
= (unsigned long) sta
;
354 sta
->plink_timer
.function
= mesh_plink_timer
;
355 sta
->plink_timeout
= timeout
;
356 add_timer(&sta
->plink_timer
);
359 int mesh_plink_open(struct sta_info
*sta
)
362 struct ieee80211_sub_if_data
*sdata
= sta
->sdata
;
364 spin_lock_bh(&sta
->lock
);
365 get_random_bytes(&llid
, 2);
367 if (sta
->plink_state
!= PLINK_LISTEN
) {
368 spin_unlock_bh(&sta
->lock
);
371 sta
->plink_state
= PLINK_OPN_SNT
;
372 mesh_plink_timer_set(sta
, dot11MeshRetryTimeout(sdata
));
373 spin_unlock_bh(&sta
->lock
);
374 mpl_dbg("Mesh plink: starting establishment with %pM\n",
377 return mesh_plink_frame_tx(sdata
, PLINK_OPEN
,
378 sta
->sta
.addr
, llid
, 0, 0);
381 void mesh_plink_block(struct sta_info
*sta
)
383 spin_lock_bh(&sta
->lock
);
384 __mesh_plink_deactivate(sta
);
385 sta
->plink_state
= PLINK_BLOCKED
;
386 spin_unlock_bh(&sta
->lock
);
390 void mesh_rx_plink_frame(struct ieee80211_sub_if_data
*sdata
, struct ieee80211_mgmt
*mgmt
,
391 size_t len
, struct ieee80211_rx_status
*rx_status
)
393 struct ieee80211_local
*local
= sdata
->local
;
394 struct ieee802_11_elems elems
;
395 struct sta_info
*sta
;
396 enum plink_event event
;
397 enum plink_frame_type ftype
;
401 __le16 plid
, llid
, reason
;
402 #ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
403 static const char *mplstates
[] = {
404 [PLINK_LISTEN
] = "LISTEN",
405 [PLINK_OPN_SNT
] = "OPN-SNT",
406 [PLINK_OPN_RCVD
] = "OPN-RCVD",
407 [PLINK_CNF_RCVD
] = "CNF_RCVD",
408 [PLINK_ESTAB
] = "ESTAB",
409 [PLINK_HOLDING
] = "HOLDING",
410 [PLINK_BLOCKED
] = "BLOCKED"
414 /* need action_code, aux */
415 if (len
< IEEE80211_MIN_ACTION_SIZE
+ 3)
418 if (is_multicast_ether_addr(mgmt
->da
)) {
419 mpl_dbg("Mesh plink: ignore frame from multicast address");
423 baseaddr
= mgmt
->u
.action
.u
.plink_action
.variable
;
424 baselen
= (u8
*) mgmt
->u
.action
.u
.plink_action
.variable
- (u8
*) mgmt
;
425 if (mgmt
->u
.action
.u
.plink_action
.action_code
== PLINK_CONFIRM
) {
429 ieee802_11_parse_elems(baseaddr
, len
- baselen
, &elems
);
430 if (!elems
.peer_link
) {
431 mpl_dbg("Mesh plink: missing necessary peer link ie\n");
435 ftype
= mgmt
->u
.action
.u
.plink_action
.action_code
;
436 ie_len
= elems
.peer_link_len
;
437 if ((ftype
== PLINK_OPEN
&& ie_len
!= 6) ||
438 (ftype
== PLINK_CONFIRM
&& ie_len
!= 8) ||
439 (ftype
== PLINK_CLOSE
&& ie_len
!= 8 && ie_len
!= 10)) {
440 mpl_dbg("Mesh plink: incorrect plink ie length %d %d\n",
445 if (ftype
!= PLINK_CLOSE
&& (!elems
.mesh_id
|| !elems
.mesh_config
)) {
446 mpl_dbg("Mesh plink: missing necessary ie\n");
449 /* Note the lines below are correct, the llid in the frame is the plid
450 * from the point of view of this host.
452 memcpy(&plid
, PLINK_GET_LLID(elems
.peer_link
), 2);
453 if (ftype
== PLINK_CONFIRM
|| (ftype
== PLINK_CLOSE
&& ie_len
== 10))
454 memcpy(&llid
, PLINK_GET_PLID(elems
.peer_link
), 2);
458 sta
= sta_info_get(sdata
, mgmt
->sa
);
459 if (!sta
&& ftype
!= PLINK_OPEN
) {
460 mpl_dbg("Mesh plink: cls or cnf from unknown peer\n");
465 if (sta
&& sta
->plink_state
== PLINK_BLOCKED
) {
470 /* Now we will figure out the appropriate event... */
471 event
= PLINK_UNDEFINED
;
472 if (ftype
!= PLINK_CLOSE
&& (!mesh_matches_local(&elems
, sdata
))) {
484 spin_lock_bh(&sta
->lock
);
486 /* ftype == PLINK_OPEN */
491 if (!mesh_plink_free_count(sdata
)) {
492 mpl_dbg("Mesh plink error: no more free plinks\n");
496 rates
= ieee80211_sta_get_rates(local
, &elems
, rx_status
->band
);
497 sta
= mesh_plink_alloc(sdata
, mgmt
->sa
, rates
);
499 mpl_dbg("Mesh plink error: plink table full\n");
502 if (sta_info_insert_rcu(sta
)) {
507 spin_lock_bh(&sta
->lock
);
509 spin_lock_bh(&sta
->lock
);
512 if (!mesh_plink_free_count(sdata
) ||
513 (sta
->plid
&& sta
->plid
!= plid
))
519 if (!mesh_plink_free_count(sdata
) ||
520 (sta
->llid
!= llid
|| sta
->plid
!= plid
))
526 if (sta
->plink_state
== PLINK_ESTAB
)
527 /* Do not check for llid or plid. This does not
528 * follow the standard but since multiple plinks
529 * per sta are not supported, it is necessary in
530 * order to avoid a livelock when MP A sees an
531 * establish peer link to MP B but MP B does not
532 * see it. This can be caused by a timeout in
533 * B's peer link establishment or B beign
537 else if (sta
->plid
!= plid
)
539 else if (ie_len
== 7 && sta
->llid
!= llid
)
545 mpl_dbg("Mesh plink: unknown frame subtype\n");
546 spin_unlock_bh(&sta
->lock
);
552 mpl_dbg("Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
553 mgmt
->sa
, mplstates
[sta
->plink_state
],
554 le16_to_cpu(sta
->llid
), le16_to_cpu(sta
->plid
),
557 switch (sta
->plink_state
) {
558 /* spin_unlock as soon as state is updated at each case */
562 mesh_plink_fsm_restart(sta
);
563 spin_unlock_bh(&sta
->lock
);
566 sta
->plink_state
= PLINK_OPN_RCVD
;
568 get_random_bytes(&llid
, 2);
570 mesh_plink_timer_set(sta
, dot11MeshRetryTimeout(sdata
));
571 spin_unlock_bh(&sta
->lock
);
572 mesh_plink_frame_tx(sdata
, PLINK_OPEN
, sta
->sta
.addr
, llid
,
574 mesh_plink_frame_tx(sdata
, PLINK_CONFIRM
, sta
->sta
.addr
,
578 spin_unlock_bh(&sta
->lock
);
587 reason
= cpu_to_le16(MESH_CAPABILITY_POLICY_VIOLATION
);
590 reason
= cpu_to_le16(MESH_CLOSE_RCVD
);
591 sta
->reason
= reason
;
592 sta
->plink_state
= PLINK_HOLDING
;
593 if (!mod_plink_timer(sta
,
594 dot11MeshHoldingTimeout(sdata
)))
595 sta
->ignore_plink_timer
= true;
598 spin_unlock_bh(&sta
->lock
);
599 mesh_plink_frame_tx(sdata
, PLINK_CLOSE
, sta
->sta
.addr
, llid
,
603 /* retry timer is left untouched */
604 sta
->plink_state
= PLINK_OPN_RCVD
;
607 spin_unlock_bh(&sta
->lock
);
608 mesh_plink_frame_tx(sdata
, PLINK_CONFIRM
, sta
->sta
.addr
, llid
,
612 sta
->plink_state
= PLINK_CNF_RCVD
;
613 if (!mod_plink_timer(sta
,
614 dot11MeshConfirmTimeout(sdata
)))
615 sta
->ignore_plink_timer
= true;
617 spin_unlock_bh(&sta
->lock
);
620 spin_unlock_bh(&sta
->lock
);
629 reason
= cpu_to_le16(MESH_CAPABILITY_POLICY_VIOLATION
);
632 reason
= cpu_to_le16(MESH_CLOSE_RCVD
);
633 sta
->reason
= reason
;
634 sta
->plink_state
= PLINK_HOLDING
;
635 if (!mod_plink_timer(sta
,
636 dot11MeshHoldingTimeout(sdata
)))
637 sta
->ignore_plink_timer
= true;
640 spin_unlock_bh(&sta
->lock
);
641 mesh_plink_frame_tx(sdata
, PLINK_CLOSE
, sta
->sta
.addr
, llid
,
646 spin_unlock_bh(&sta
->lock
);
647 mesh_plink_frame_tx(sdata
, PLINK_CONFIRM
, sta
->sta
.addr
, llid
,
651 del_timer(&sta
->plink_timer
);
652 sta
->plink_state
= PLINK_ESTAB
;
653 mesh_plink_inc_estab_count(sdata
);
654 spin_unlock_bh(&sta
->lock
);
655 mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
659 spin_unlock_bh(&sta
->lock
);
668 reason
= cpu_to_le16(MESH_CAPABILITY_POLICY_VIOLATION
);
671 reason
= cpu_to_le16(MESH_CLOSE_RCVD
);
672 sta
->reason
= reason
;
673 sta
->plink_state
= PLINK_HOLDING
;
674 if (!mod_plink_timer(sta
,
675 dot11MeshHoldingTimeout(sdata
)))
676 sta
->ignore_plink_timer
= true;
679 spin_unlock_bh(&sta
->lock
);
680 mesh_plink_frame_tx(sdata
, PLINK_CLOSE
, sta
->sta
.addr
, llid
,
684 del_timer(&sta
->plink_timer
);
685 sta
->plink_state
= PLINK_ESTAB
;
686 mesh_plink_inc_estab_count(sdata
);
687 spin_unlock_bh(&sta
->lock
);
688 mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
690 mesh_plink_frame_tx(sdata
, PLINK_CONFIRM
, sta
->sta
.addr
, llid
,
694 spin_unlock_bh(&sta
->lock
);
702 reason
= cpu_to_le16(MESH_CLOSE_RCVD
);
703 sta
->reason
= reason
;
704 __mesh_plink_deactivate(sta
);
705 sta
->plink_state
= PLINK_HOLDING
;
707 mod_plink_timer(sta
, dot11MeshHoldingTimeout(sdata
));
708 spin_unlock_bh(&sta
->lock
);
709 mesh_plink_frame_tx(sdata
, PLINK_CLOSE
, sta
->sta
.addr
, llid
,
714 spin_unlock_bh(&sta
->lock
);
715 mesh_plink_frame_tx(sdata
, PLINK_CONFIRM
, sta
->sta
.addr
, llid
,
719 spin_unlock_bh(&sta
->lock
);
726 if (del_timer(&sta
->plink_timer
))
727 sta
->ignore_plink_timer
= 1;
728 mesh_plink_fsm_restart(sta
);
729 spin_unlock_bh(&sta
->lock
);
736 reason
= sta
->reason
;
737 spin_unlock_bh(&sta
->lock
);
738 mesh_plink_frame_tx(sdata
, PLINK_CLOSE
, sta
->sta
.addr
,
742 spin_unlock_bh(&sta
->lock
);
746 /* should not get here, PLINK_BLOCKED is dealt with at the
747 * beggining of the function
749 spin_unlock_bh(&sta
->lock
);