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"
12 #include "ieee80211_rate.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 IEEE80211_FC(type, stype) cpu_to_le16(type | stype)
22 #define PLINK_GET_FRAME_SUBTYPE(p) (p)
23 #define PLINK_GET_LLID(p) (p + 1)
24 #define PLINK_GET_PLID(p) (p + 3)
26 #define mod_plink_timer(s, t) (mod_timer(&s->plink_timer, \
27 jiffies + HZ * t / 1000))
29 /* Peer link cancel reasons, all subject to ANA approval */
30 #define MESH_LINK_CANCELLED 2
31 #define MESH_MAX_NEIGHBORS 3
32 #define MESH_CAPABILITY_POLICY_VIOLATION 4
33 #define MESH_CLOSE_RCVD 5
34 #define MESH_MAX_RETRIES 6
35 #define MESH_CONFIRM_TIMEOUT 7
36 #define MESH_SECURITY_ROLE_NEGOTIATION_DIFFERS 8
37 #define MESH_SECURITY_AUTHENTICATION_IMPOSSIBLE 9
38 #define MESH_SECURITY_FAILED_VERIFICATION 10
40 #define dot11MeshMaxRetries(s) (s->u.sta.mshcfg.dot11MeshMaxRetries)
41 #define dot11MeshRetryTimeout(s) (s->u.sta.mshcfg.dot11MeshRetryTimeout)
42 #define dot11MeshConfirmTimeout(s) (s->u.sta.mshcfg.dot11MeshConfirmTimeout)
43 #define dot11MeshHoldingTimeout(s) (s->u.sta.mshcfg.dot11MeshHoldingTimeout)
44 #define dot11MeshMaxPeerLinks(s) (s->u.sta.mshcfg.dot11MeshMaxPeerLinks)
46 enum plink_frame_type {
65 void mesh_plink_inc_estab_count(struct ieee80211_sub_if_data *sdata)
67 atomic_inc(&sdata->u.sta.mshstats.estab_plinks);
68 mesh_accept_plinks_update(sdata);
72 void mesh_plink_dec_estab_count(struct ieee80211_sub_if_data *sdata)
74 atomic_dec(&sdata->u.sta.mshstats.estab_plinks);
75 mesh_accept_plinks_update(sdata);
79 * mesh_plink_fsm_restart - restart a mesh peer link finite state machine
81 * @sta: mes peer link to restart
83 * Locking: this function must be called holding sta->plink_lock
85 static inline void mesh_plink_fsm_restart(struct sta_info *sta)
87 sta->plink_state = LISTEN;
88 sta->llid = sta->plid = sta->reason = 0;
89 sta->plink_retries = 0;
92 static struct sta_info *mesh_plink_alloc(struct ieee80211_sub_if_data *sdata,
93 u8 *hw_addr, u64 rates)
95 struct ieee80211_local *local = sdata->local;
98 if (local->num_sta >= MESH_MAX_PLINKS)
101 sta = sta_info_alloc(sdata, hw_addr, GFP_ATOMIC);
105 sta->flags |= WLAN_STA_AUTHORIZED;
106 sta->supp_rates[local->hw.conf.channel->band] = rates;
112 * mesh_plink_deactivate - deactivate mesh peer link
114 * @sta: mesh peer link to deactivate
116 * All mesh paths with this peer as next hop will be flushed
118 * Locking: the caller must hold sta->plink_lock
120 static void __mesh_plink_deactivate(struct sta_info *sta)
122 struct ieee80211_sub_if_data *sdata = sta->sdata;
124 if (sta->plink_state == ESTAB)
125 mesh_plink_dec_estab_count(sdata);
126 sta->plink_state = BLOCKED;
127 mesh_path_flush_by_nexthop(sta);
131 * __mesh_plink_deactivate - deactivate mesh peer link
133 * @sta: mesh peer link to deactivate
135 * All mesh paths with this peer as next hop will be flushed
137 void mesh_plink_deactivate(struct sta_info *sta)
139 spin_lock_bh(&sta->plink_lock);
140 __mesh_plink_deactivate(sta);
141 spin_unlock_bh(&sta->plink_lock);
144 static int mesh_plink_frame_tx(struct net_device *dev,
145 enum plink_frame_type action, u8 *da, __le16 llid, __le16 plid,
147 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
148 struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
149 struct ieee80211_mgmt *mgmt;
150 bool include_plid = false;
156 skb_reserve(skb, local->hw.extra_tx_headroom);
157 /* 25 is the size of the common mgmt part (24) plus the size of the
158 * common action part (1)
160 mgmt = (struct ieee80211_mgmt *)
161 skb_put(skb, 25 + sizeof(mgmt->u.action.u.plink_action));
162 memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.plink_action));
163 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
164 IEEE80211_STYPE_ACTION);
165 memcpy(mgmt->da, da, ETH_ALEN);
166 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
167 /* BSSID is left zeroed, wildcard value */
168 mgmt->u.action.category = PLINK_CATEGORY;
169 mgmt->u.action.u.plink_action.action_code = action;
171 if (action == PLINK_CLOSE)
172 mgmt->u.action.u.plink_action.aux = reason;
174 mgmt->u.action.u.plink_action.aux = cpu_to_le16(0x0);
175 if (action == PLINK_CONFIRM) {
176 pos = skb_put(skb, 4);
177 /* two-byte status code followed by two-byte AID */
180 mesh_mgmt_ies_add(skb, dev);
183 /* Add Peer Link Management element */
203 pos = skb_put(skb, 2 + ie_len);
204 *pos++ = WLAN_EID_PEER_LINK;
207 memcpy(pos, &llid, 2);
210 memcpy(pos, &plid, 2);
212 if (action == PLINK_CLOSE) {
214 memcpy(pos, &reason, 2);
217 ieee80211_sta_tx(dev, skb, 0);
221 void mesh_neighbour_update(u8 *hw_addr, u64 rates, struct net_device *dev,
222 bool peer_accepting_plinks)
224 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
225 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
226 struct sta_info *sta;
230 sta = sta_info_get(local, hw_addr);
232 sta = mesh_plink_alloc(sdata, hw_addr, rates);
237 if (sta_info_insert(sta)) {
238 sta_info_destroy(sta);
244 sta->last_rx = jiffies;
245 sta->supp_rates[local->hw.conf.channel->band] = rates;
246 if (peer_accepting_plinks && sta->plink_state == LISTEN &&
247 sdata->u.sta.accepting_plinks &&
248 sdata->u.sta.mshcfg.auto_open_plinks)
249 mesh_plink_open(sta);
254 static void mesh_plink_timer(unsigned long data)
256 struct sta_info *sta;
257 __le16 llid, plid, reason;
258 struct net_device *dev = NULL;
259 struct ieee80211_sub_if_data *sdata;
260 #ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
261 DECLARE_MAC_BUF(mac);
265 * This STA is valid because sta_info_destroy() will
266 * del_timer_sync() this timer after having made sure
267 * it cannot be readded (by deleting the plink.)
269 sta = (struct sta_info *) data;
271 spin_lock_bh(&sta->plink_lock);
272 if (sta->ignore_plink_timer) {
273 sta->ignore_plink_timer = false;
274 spin_unlock_bh(&sta->plink_lock);
277 mpl_dbg("Mesh plink timer for %s fired on state %d\n",
278 print_mac(mac, sta->addr), sta->plink_state);
285 switch (sta->plink_state) {
289 if (sta->plink_retries < dot11MeshMaxRetries(sdata)) {
291 mpl_dbg("Mesh plink for %s (retry, timeout): %d %d\n",
292 print_mac(mac, sta->addr),
293 sta->plink_retries, sta->plink_timeout);
294 get_random_bytes(&rand, sizeof(u32));
295 sta->plink_timeout = sta->plink_timeout +
296 rand % sta->plink_timeout;
297 ++sta->plink_retries;
298 mod_plink_timer(sta, sta->plink_timeout);
299 spin_unlock_bh(&sta->plink_lock);
300 mesh_plink_frame_tx(dev, PLINK_OPEN, sta->addr, llid,
304 reason = cpu_to_le16(MESH_MAX_RETRIES);
305 /* fall through on else */
309 reason = cpu_to_le16(MESH_CONFIRM_TIMEOUT);
310 sta->plink_state = HOLDING;
311 mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
312 spin_unlock_bh(&sta->plink_lock);
313 mesh_plink_frame_tx(dev, PLINK_CLOSE, sta->addr, llid, plid,
318 del_timer(&sta->plink_timer);
319 mesh_plink_fsm_restart(sta);
320 spin_unlock_bh(&sta->plink_lock);
323 spin_unlock_bh(&sta->plink_lock);
328 static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout)
330 sta->plink_timer.expires = jiffies + (HZ * timeout / 1000);
331 sta->plink_timer.data = (unsigned long) sta;
332 sta->plink_timer.function = mesh_plink_timer;
333 sta->plink_timeout = timeout;
334 add_timer(&sta->plink_timer);
337 int mesh_plink_open(struct sta_info *sta)
340 struct ieee80211_sub_if_data *sdata = sta->sdata;
341 #ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
342 DECLARE_MAC_BUF(mac);
345 spin_lock_bh(&sta->plink_lock);
346 get_random_bytes(&llid, 2);
348 if (sta->plink_state != LISTEN) {
349 spin_unlock_bh(&sta->plink_lock);
352 sta->plink_state = OPN_SNT;
353 mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
354 spin_unlock_bh(&sta->plink_lock);
355 mpl_dbg("Mesh plink: starting establishment with %s\n",
356 print_mac(mac, sta->addr));
358 return mesh_plink_frame_tx(sdata->dev, PLINK_OPEN,
359 sta->addr, llid, 0, 0);
362 void mesh_plink_block(struct sta_info *sta)
364 #ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
365 DECLARE_MAC_BUF(mac);
368 spin_lock_bh(&sta->plink_lock);
369 __mesh_plink_deactivate(sta);
370 sta->plink_state = BLOCKED;
371 spin_unlock_bh(&sta->plink_lock);
374 int mesh_plink_close(struct sta_info *sta)
376 struct ieee80211_sub_if_data *sdata = sta->sdata;
377 __le16 llid, plid, reason;
378 #ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
379 DECLARE_MAC_BUF(mac);
382 mpl_dbg("Mesh plink: closing link with %s\n",
383 print_mac(mac, sta->addr));
384 spin_lock_bh(&sta->plink_lock);
385 sta->reason = cpu_to_le16(MESH_LINK_CANCELLED);
386 reason = sta->reason;
388 if (sta->plink_state == LISTEN || sta->plink_state == BLOCKED) {
389 mesh_plink_fsm_restart(sta);
390 spin_unlock_bh(&sta->plink_lock);
392 } else if (sta->plink_state == ESTAB) {
393 __mesh_plink_deactivate(sta);
394 /* The timer should not be running */
395 mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
396 } else if (!mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata)))
397 sta->ignore_plink_timer = true;
399 sta->plink_state = HOLDING;
402 spin_unlock_bh(&sta->plink_lock);
403 mesh_plink_frame_tx(sta->sdata->dev, PLINK_CLOSE, sta->addr, llid,
408 void mesh_rx_plink_frame(struct net_device *dev, struct ieee80211_mgmt *mgmt,
409 size_t len, struct ieee80211_rx_status *rx_status)
411 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
412 struct ieee80211_local *local = sdata->local;
413 struct ieee802_11_elems elems;
414 struct sta_info *sta;
415 enum plink_event event;
416 enum plink_frame_type ftype;
420 __le16 plid, llid, reason;
421 #ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
422 DECLARE_MAC_BUF(mac);
425 if (is_multicast_ether_addr(mgmt->da)) {
426 mpl_dbg("Mesh plink: ignore frame from multicast address");
430 baseaddr = mgmt->u.action.u.plink_action.variable;
431 baselen = (u8 *) mgmt->u.action.u.plink_action.variable - (u8 *) mgmt;
432 if (mgmt->u.action.u.plink_action.action_code == PLINK_CONFIRM) {
436 ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
437 if (!elems.peer_link) {
438 mpl_dbg("Mesh plink: missing necessary peer link ie\n");
442 ftype = *((u8 *)PLINK_GET_FRAME_SUBTYPE(elems.peer_link));
443 ie_len = elems.peer_link_len;
444 if ((ftype == PLINK_OPEN && ie_len != 3) ||
445 (ftype == PLINK_CONFIRM && ie_len != 5) ||
446 (ftype == PLINK_CLOSE && ie_len != 5 && ie_len != 7)) {
447 mpl_dbg("Mesh plink: incorrect plink ie length\n");
451 if (ftype != PLINK_CLOSE && (!elems.mesh_id || !elems.mesh_config)) {
452 mpl_dbg("Mesh plink: missing necessary ie\n");
455 /* Note the lines below are correct, the llid in the frame is the plid
456 * from the point of view of this host.
458 memcpy(&plid, PLINK_GET_LLID(elems.peer_link), 2);
459 if (ftype == PLINK_CONFIRM || (ftype == PLINK_CLOSE && ie_len == 7))
460 memcpy(&llid, PLINK_GET_PLID(elems.peer_link), 2);
464 sta = sta_info_get(local, mgmt->sa);
465 if (!sta && ftype != PLINK_OPEN) {
466 mpl_dbg("Mesh plink: cls or cnf from unknown peer\n");
471 if (sta && sta->plink_state == BLOCKED) {
476 /* Now we will figure out the appropriate event... */
477 event = PLINK_UNDEFINED;
478 if (ftype != PLINK_CLOSE && (!mesh_matches_local(&elems, dev))) {
490 spin_lock_bh(&sta->plink_lock);
492 /* ftype == PLINK_OPEN */
494 if (!mesh_plink_free_count(sdata)) {
495 mpl_dbg("Mesh plink error: no more free plinks\n");
500 rates = ieee80211_sta_get_rates(local, &elems, rx_status->band);
501 sta = mesh_plink_alloc(sdata, mgmt->sa, rates);
503 mpl_dbg("Mesh plink error: plink table full\n");
507 if (sta_info_insert(sta)) {
508 sta_info_destroy(sta);
513 spin_lock_bh(&sta->plink_lock);
515 spin_lock_bh(&sta->plink_lock);
518 if (!mesh_plink_free_count(sdata) ||
519 (sta->plid && sta->plid != plid))
525 if (!mesh_plink_free_count(sdata) ||
526 (sta->llid != llid || sta->plid != plid))
532 if (sta->plink_state == ESTAB)
533 /* Do not check for llid or plid. This does not
534 * follow the standard but since multiple plinks
535 * per sta are not supported, it is necessary in
536 * order to avoid a livelock when MP A sees an
537 * establish peer link to MP B but MP B does not
538 * see it. This can be caused by a timeout in
539 * B's peer link establishment or B beign
543 else if (sta->plid != plid)
545 else if (ie_len == 7 && sta->llid != llid)
551 mpl_dbg("Mesh plink: unknown frame subtype\n");
552 spin_unlock_bh(&sta->plink_lock);
558 mpl_dbg("Mesh plink (peer, state, llid, plid, event): %s %d %d %d %d\n",
559 print_mac(mac, mgmt->sa), sta->plink_state,
560 __le16_to_cpu(sta->llid), __le16_to_cpu(sta->plid),
563 switch (sta->plink_state) {
564 /* spin_unlock as soon as state is updated at each case */
568 mesh_plink_fsm_restart(sta);
569 spin_unlock_bh(&sta->plink_lock);
572 sta->plink_state = OPN_RCVD;
574 get_random_bytes(&llid, 2);
576 mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
577 spin_unlock_bh(&sta->plink_lock);
578 mesh_plink_frame_tx(dev, PLINK_OPEN, sta->addr, llid,
580 mesh_plink_frame_tx(dev, PLINK_CONFIRM, sta->addr,
584 spin_unlock_bh(&sta->plink_lock);
593 reason = cpu_to_le16(MESH_CAPABILITY_POLICY_VIOLATION);
596 reason = cpu_to_le16(MESH_CLOSE_RCVD);
597 sta->reason = reason;
598 sta->plink_state = HOLDING;
599 if (!mod_plink_timer(sta,
600 dot11MeshHoldingTimeout(sdata)))
601 sta->ignore_plink_timer = true;
604 spin_unlock_bh(&sta->plink_lock);
605 mesh_plink_frame_tx(dev, PLINK_CLOSE, sta->addr, llid,
609 /* retry timer is left untouched */
610 sta->plink_state = OPN_RCVD;
613 spin_unlock_bh(&sta->plink_lock);
614 mesh_plink_frame_tx(dev, PLINK_CONFIRM, sta->addr, llid,
618 sta->plink_state = CNF_RCVD;
619 if (!mod_plink_timer(sta,
620 dot11MeshConfirmTimeout(sdata)))
621 sta->ignore_plink_timer = true;
623 spin_unlock_bh(&sta->plink_lock);
626 spin_unlock_bh(&sta->plink_lock);
635 reason = cpu_to_le16(MESH_CAPABILITY_POLICY_VIOLATION);
638 reason = cpu_to_le16(MESH_CLOSE_RCVD);
639 sta->reason = reason;
640 sta->plink_state = HOLDING;
641 if (!mod_plink_timer(sta,
642 dot11MeshHoldingTimeout(sdata)))
643 sta->ignore_plink_timer = true;
646 spin_unlock_bh(&sta->plink_lock);
647 mesh_plink_frame_tx(dev, PLINK_CLOSE, sta->addr, llid,
652 spin_unlock_bh(&sta->plink_lock);
653 mesh_plink_frame_tx(dev, PLINK_CONFIRM, sta->addr, llid,
657 del_timer(&sta->plink_timer);
658 sta->plink_state = ESTAB;
659 mesh_plink_inc_estab_count(sdata);
660 spin_unlock_bh(&sta->plink_lock);
661 mpl_dbg("Mesh plink with %s ESTABLISHED\n",
662 print_mac(mac, sta->addr));
665 spin_unlock_bh(&sta->plink_lock);
674 reason = cpu_to_le16(MESH_CAPABILITY_POLICY_VIOLATION);
677 reason = cpu_to_le16(MESH_CLOSE_RCVD);
678 sta->reason = reason;
679 sta->plink_state = HOLDING;
680 if (!mod_plink_timer(sta,
681 dot11MeshHoldingTimeout(sdata)))
682 sta->ignore_plink_timer = true;
685 spin_unlock_bh(&sta->plink_lock);
686 mesh_plink_frame_tx(dev, PLINK_CLOSE, sta->addr, llid,
690 del_timer(&sta->plink_timer);
691 sta->plink_state = ESTAB;
692 mesh_plink_inc_estab_count(sdata);
693 spin_unlock_bh(&sta->plink_lock);
694 mpl_dbg("Mesh plink with %s ESTABLISHED\n",
695 print_mac(mac, sta->addr));
696 mesh_plink_frame_tx(dev, PLINK_CONFIRM, sta->addr, llid,
700 spin_unlock_bh(&sta->plink_lock);
708 reason = cpu_to_le16(MESH_CLOSE_RCVD);
709 sta->reason = reason;
710 __mesh_plink_deactivate(sta);
711 sta->plink_state = HOLDING;
713 mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
714 spin_unlock_bh(&sta->plink_lock);
715 mesh_plink_frame_tx(dev, PLINK_CLOSE, sta->addr, llid,
720 spin_unlock_bh(&sta->plink_lock);
721 mesh_plink_frame_tx(dev, PLINK_CONFIRM, sta->addr, llid,
725 spin_unlock_bh(&sta->plink_lock);
732 if (del_timer(&sta->plink_timer))
733 sta->ignore_plink_timer = 1;
734 mesh_plink_fsm_restart(sta);
735 spin_unlock_bh(&sta->plink_lock);
742 reason = sta->reason;
743 spin_unlock_bh(&sta->plink_lock);
744 mesh_plink_frame_tx(dev, PLINK_CLOSE, sta->addr, llid,
748 spin_unlock_bh(&sta->plink_lock);
752 /* should not get here, BLOCKED is dealt with at the beggining
755 spin_unlock_bh(&sta->plink_lock);