2 * Copyright (c) 2008 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_ATOMIC);
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->dev->dev_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 */
186 mesh_mgmt_ies_add(skb, sdata);
189 /* Add Peer Link Management element */
209 pos = skb_put(skb, 2 + ie_len);
210 *pos++ = WLAN_EID_PEER_LINK;
212 memcpy(pos, meshpeeringproto, sizeof(meshpeeringproto));
214 memcpy(pos, &llid, 2);
217 memcpy(pos, &plid, 2);
219 if (action == PLINK_CLOSE) {
221 memcpy(pos, &reason, 2);
224 ieee80211_tx_skb(sdata, skb, 1);
228 void mesh_neighbour_update(u8 *hw_addr, u32 rates, struct ieee80211_sub_if_data *sdata,
229 bool peer_accepting_plinks)
231 struct ieee80211_local *local = sdata->local;
232 struct sta_info *sta;
236 sta = sta_info_get(local, hw_addr);
238 sta = mesh_plink_alloc(sdata, hw_addr, rates);
243 if (sta_info_insert(sta)) {
249 sta->last_rx = jiffies;
250 sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
251 if (peer_accepting_plinks && sta->plink_state == PLINK_LISTEN &&
252 sdata->u.mesh.accepting_plinks &&
253 sdata->u.mesh.mshcfg.auto_open_plinks)
254 mesh_plink_open(sta);
259 static void mesh_plink_timer(unsigned long data)
261 struct sta_info *sta;
262 __le16 llid, plid, reason;
263 struct ieee80211_sub_if_data *sdata;
266 * This STA is valid because sta_info_destroy() will
267 * del_timer_sync() this timer after having made sure
268 * it cannot be readded (by deleting the plink.)
270 sta = (struct sta_info *) data;
272 if (sta->sdata->local->quiescing) {
273 sta->plink_timer_was_running = true;
277 spin_lock_bh(&sta->lock);
278 if (sta->ignore_plink_timer) {
279 sta->ignore_plink_timer = false;
280 spin_unlock_bh(&sta->lock);
283 mpl_dbg("Mesh plink timer for %pM fired on state %d\n",
284 sta->sta.addr, sta->plink_state);
290 switch (sta->plink_state) {
294 if (sta->plink_retries < dot11MeshMaxRetries(sdata)) {
296 mpl_dbg("Mesh plink for %pM (retry, timeout): %d %d\n",
297 sta->sta.addr, sta->plink_retries,
299 get_random_bytes(&rand, sizeof(u32));
300 sta->plink_timeout = sta->plink_timeout +
301 rand % sta->plink_timeout;
302 ++sta->plink_retries;
303 mod_plink_timer(sta, sta->plink_timeout);
304 spin_unlock_bh(&sta->lock);
305 mesh_plink_frame_tx(sdata, PLINK_OPEN, sta->sta.addr, llid,
309 reason = cpu_to_le16(MESH_MAX_RETRIES);
310 /* fall through on else */
314 reason = cpu_to_le16(MESH_CONFIRM_TIMEOUT);
315 sta->plink_state = PLINK_HOLDING;
316 mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
317 spin_unlock_bh(&sta->lock);
318 mesh_plink_frame_tx(sdata, PLINK_CLOSE, sta->sta.addr, llid, plid,
323 del_timer(&sta->plink_timer);
324 mesh_plink_fsm_restart(sta);
325 spin_unlock_bh(&sta->lock);
328 spin_unlock_bh(&sta->lock);
334 void mesh_plink_quiesce(struct sta_info *sta)
336 if (del_timer_sync(&sta->plink_timer))
337 sta->plink_timer_was_running = true;
340 void mesh_plink_restart(struct sta_info *sta)
342 if (sta->plink_timer_was_running) {
343 add_timer(&sta->plink_timer);
344 sta->plink_timer_was_running = false;
349 static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout)
351 sta->plink_timer.expires = jiffies + (HZ * timeout / 1000);
352 sta->plink_timer.data = (unsigned long) sta;
353 sta->plink_timer.function = mesh_plink_timer;
354 sta->plink_timeout = timeout;
355 add_timer(&sta->plink_timer);
358 int mesh_plink_open(struct sta_info *sta)
361 struct ieee80211_sub_if_data *sdata = sta->sdata;
363 spin_lock_bh(&sta->lock);
364 get_random_bytes(&llid, 2);
366 if (sta->plink_state != PLINK_LISTEN) {
367 spin_unlock_bh(&sta->lock);
370 sta->plink_state = PLINK_OPN_SNT;
371 mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
372 spin_unlock_bh(&sta->lock);
373 mpl_dbg("Mesh plink: starting establishment with %pM\n",
376 return mesh_plink_frame_tx(sdata, PLINK_OPEN,
377 sta->sta.addr, llid, 0, 0);
380 void mesh_plink_block(struct sta_info *sta)
382 spin_lock_bh(&sta->lock);
383 __mesh_plink_deactivate(sta);
384 sta->plink_state = PLINK_BLOCKED;
385 spin_unlock_bh(&sta->lock);
389 void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata, struct ieee80211_mgmt *mgmt,
390 size_t len, struct ieee80211_rx_status *rx_status)
392 struct ieee80211_local *local = sdata->local;
393 struct ieee802_11_elems elems;
394 struct sta_info *sta;
395 enum plink_event event;
396 enum plink_frame_type ftype;
400 __le16 plid, llid, reason;
401 #ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
402 static const char *mplstates[] = {
403 [PLINK_LISTEN] = "LISTEN",
404 [PLINK_OPN_SNT] = "OPN-SNT",
405 [PLINK_OPN_RCVD] = "OPN-RCVD",
406 [PLINK_CNF_RCVD] = "CNF_RCVD",
407 [PLINK_ESTAB] = "ESTAB",
408 [PLINK_HOLDING] = "HOLDING",
409 [PLINK_BLOCKED] = "BLOCKED"
413 /* need action_code, aux */
414 if (len < IEEE80211_MIN_ACTION_SIZE + 3)
417 if (is_multicast_ether_addr(mgmt->da)) {
418 mpl_dbg("Mesh plink: ignore frame from multicast address");
422 baseaddr = mgmt->u.action.u.plink_action.variable;
423 baselen = (u8 *) mgmt->u.action.u.plink_action.variable - (u8 *) mgmt;
424 if (mgmt->u.action.u.plink_action.action_code == PLINK_CONFIRM) {
428 ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
429 if (!elems.peer_link) {
430 mpl_dbg("Mesh plink: missing necessary peer link ie\n");
434 ftype = mgmt->u.action.u.plink_action.action_code;
435 ie_len = elems.peer_link_len;
436 if ((ftype == PLINK_OPEN && ie_len != 6) ||
437 (ftype == PLINK_CONFIRM && ie_len != 8) ||
438 (ftype == PLINK_CLOSE && ie_len != 8 && ie_len != 10)) {
439 mpl_dbg("Mesh plink: incorrect plink ie length %d %d\n",
444 if (ftype != PLINK_CLOSE && (!elems.mesh_id || !elems.mesh_config)) {
445 mpl_dbg("Mesh plink: missing necessary ie\n");
448 /* Note the lines below are correct, the llid in the frame is the plid
449 * from the point of view of this host.
451 memcpy(&plid, PLINK_GET_LLID(elems.peer_link), 2);
452 if (ftype == PLINK_CONFIRM || (ftype == PLINK_CLOSE && ie_len == 10))
453 memcpy(&llid, PLINK_GET_PLID(elems.peer_link), 2);
457 sta = sta_info_get(local, mgmt->sa);
458 if (!sta && ftype != PLINK_OPEN) {
459 mpl_dbg("Mesh plink: cls or cnf from unknown peer\n");
464 if (sta && sta->plink_state == PLINK_BLOCKED) {
469 /* Now we will figure out the appropriate event... */
470 event = PLINK_UNDEFINED;
471 if (ftype != PLINK_CLOSE && (!mesh_matches_local(&elems, sdata))) {
483 spin_lock_bh(&sta->lock);
485 /* ftype == PLINK_OPEN */
487 if (!mesh_plink_free_count(sdata)) {
488 mpl_dbg("Mesh plink error: no more free plinks\n");
493 rates = ieee80211_sta_get_rates(local, &elems, rx_status->band);
494 sta = mesh_plink_alloc(sdata, mgmt->sa, rates);
496 mpl_dbg("Mesh plink error: plink table full\n");
500 if (sta_info_insert(sta)) {
505 spin_lock_bh(&sta->lock);
507 spin_lock_bh(&sta->lock);
510 if (!mesh_plink_free_count(sdata) ||
511 (sta->plid && sta->plid != plid))
517 if (!mesh_plink_free_count(sdata) ||
518 (sta->llid != llid || sta->plid != plid))
524 if (sta->plink_state == PLINK_ESTAB)
525 /* Do not check for llid or plid. This does not
526 * follow the standard but since multiple plinks
527 * per sta are not supported, it is necessary in
528 * order to avoid a livelock when MP A sees an
529 * establish peer link to MP B but MP B does not
530 * see it. This can be caused by a timeout in
531 * B's peer link establishment or B beign
535 else if (sta->plid != plid)
537 else if (ie_len == 7 && sta->llid != llid)
543 mpl_dbg("Mesh plink: unknown frame subtype\n");
544 spin_unlock_bh(&sta->lock);
550 mpl_dbg("Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
551 mgmt->sa, mplstates[sta->plink_state],
552 le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
555 switch (sta->plink_state) {
556 /* spin_unlock as soon as state is updated at each case */
560 mesh_plink_fsm_restart(sta);
561 spin_unlock_bh(&sta->lock);
564 sta->plink_state = PLINK_OPN_RCVD;
566 get_random_bytes(&llid, 2);
568 mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
569 spin_unlock_bh(&sta->lock);
570 mesh_plink_frame_tx(sdata, PLINK_OPEN, sta->sta.addr, llid,
572 mesh_plink_frame_tx(sdata, PLINK_CONFIRM, sta->sta.addr,
576 spin_unlock_bh(&sta->lock);
585 reason = cpu_to_le16(MESH_CAPABILITY_POLICY_VIOLATION);
588 reason = cpu_to_le16(MESH_CLOSE_RCVD);
589 sta->reason = reason;
590 sta->plink_state = PLINK_HOLDING;
591 if (!mod_plink_timer(sta,
592 dot11MeshHoldingTimeout(sdata)))
593 sta->ignore_plink_timer = true;
596 spin_unlock_bh(&sta->lock);
597 mesh_plink_frame_tx(sdata, PLINK_CLOSE, sta->sta.addr, llid,
601 /* retry timer is left untouched */
602 sta->plink_state = PLINK_OPN_RCVD;
605 spin_unlock_bh(&sta->lock);
606 mesh_plink_frame_tx(sdata, PLINK_CONFIRM, sta->sta.addr, llid,
610 sta->plink_state = PLINK_CNF_RCVD;
611 if (!mod_plink_timer(sta,
612 dot11MeshConfirmTimeout(sdata)))
613 sta->ignore_plink_timer = true;
615 spin_unlock_bh(&sta->lock);
618 spin_unlock_bh(&sta->lock);
627 reason = cpu_to_le16(MESH_CAPABILITY_POLICY_VIOLATION);
630 reason = cpu_to_le16(MESH_CLOSE_RCVD);
631 sta->reason = reason;
632 sta->plink_state = PLINK_HOLDING;
633 if (!mod_plink_timer(sta,
634 dot11MeshHoldingTimeout(sdata)))
635 sta->ignore_plink_timer = true;
638 spin_unlock_bh(&sta->lock);
639 mesh_plink_frame_tx(sdata, PLINK_CLOSE, sta->sta.addr, llid,
644 spin_unlock_bh(&sta->lock);
645 mesh_plink_frame_tx(sdata, PLINK_CONFIRM, sta->sta.addr, llid,
649 del_timer(&sta->plink_timer);
650 sta->plink_state = PLINK_ESTAB;
651 mesh_plink_inc_estab_count(sdata);
652 spin_unlock_bh(&sta->lock);
653 mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
657 spin_unlock_bh(&sta->lock);
666 reason = cpu_to_le16(MESH_CAPABILITY_POLICY_VIOLATION);
669 reason = cpu_to_le16(MESH_CLOSE_RCVD);
670 sta->reason = reason;
671 sta->plink_state = PLINK_HOLDING;
672 if (!mod_plink_timer(sta,
673 dot11MeshHoldingTimeout(sdata)))
674 sta->ignore_plink_timer = true;
677 spin_unlock_bh(&sta->lock);
678 mesh_plink_frame_tx(sdata, PLINK_CLOSE, sta->sta.addr, llid,
682 del_timer(&sta->plink_timer);
683 sta->plink_state = PLINK_ESTAB;
684 mesh_plink_inc_estab_count(sdata);
685 spin_unlock_bh(&sta->lock);
686 mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
688 mesh_plink_frame_tx(sdata, PLINK_CONFIRM, sta->sta.addr, llid,
692 spin_unlock_bh(&sta->lock);
700 reason = cpu_to_le16(MESH_CLOSE_RCVD);
701 sta->reason = reason;
702 __mesh_plink_deactivate(sta);
703 sta->plink_state = PLINK_HOLDING;
705 mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
706 spin_unlock_bh(&sta->lock);
707 mesh_plink_frame_tx(sdata, PLINK_CLOSE, sta->sta.addr, llid,
712 spin_unlock_bh(&sta->lock);
713 mesh_plink_frame_tx(sdata, PLINK_CONFIRM, sta->sta.addr, llid,
717 spin_unlock_bh(&sta->lock);
724 if (del_timer(&sta->plink_timer))
725 sta->ignore_plink_timer = 1;
726 mesh_plink_fsm_restart(sta);
727 spin_unlock_bh(&sta->lock);
734 reason = sta->reason;
735 spin_unlock_bh(&sta->lock);
736 mesh_plink_frame_tx(sdata, PLINK_CLOSE, sta->sta.addr,
740 spin_unlock_bh(&sta->lock);
744 /* should not get here, PLINK_BLOCKED is dealt with at the
745 * beggining of the function
747 spin_unlock_bh(&sta->lock);