1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2008, 2009 open80211s Ltd.
4 * Copyright (C) 2019, 2021-2023 Intel Corporation
5 * Author: Luis Carlos Cobo <luisca@cozybit.com>
8 #include <linux/kernel.h>
9 #include <linux/random.h>
10 #include <linux/rculist.h>
12 #include "ieee80211_i.h"
16 #define PLINK_CNF_AID(mgmt) ((mgmt)->u.action.u.self_prot.variable + 2)
17 #define PLINK_GET_LLID(p) (p + 2)
18 #define PLINK_GET_PLID(p) (p + 4)
20 #define mod_plink_timer(s, t) (mod_timer(&s->mesh->plink_timer, \
21 jiffies + msecs_to_jiffies(t)))
35 static const char * const mplstates[] = {
36 [NL80211_PLINK_LISTEN] = "LISTEN",
37 [NL80211_PLINK_OPN_SNT] = "OPN-SNT",
38 [NL80211_PLINK_OPN_RCVD] = "OPN-RCVD",
39 [NL80211_PLINK_CNF_RCVD] = "CNF_RCVD",
40 [NL80211_PLINK_ESTAB] = "ESTAB",
41 [NL80211_PLINK_HOLDING] = "HOLDING",
42 [NL80211_PLINK_BLOCKED] = "BLOCKED"
45 static const char * const mplevents[] = {
46 [PLINK_UNDEFINED] = "NONE",
47 [OPN_ACPT] = "OPN_ACPT",
48 [OPN_RJCT] = "OPN_RJCT",
49 [OPN_IGNR] = "OPN_IGNR",
50 [CNF_ACPT] = "CNF_ACPT",
51 [CNF_RJCT] = "CNF_RJCT",
52 [CNF_IGNR] = "CNF_IGNR",
53 [CLS_ACPT] = "CLS_ACPT",
54 [CLS_IGNR] = "CLS_IGNR"
57 /* We only need a valid sta if user configured a minimum rssi_threshold. */
58 static bool rssi_threshold_check(struct ieee80211_sub_if_data *sdata,
61 s32 rssi_threshold = sdata->u.mesh.mshcfg.rssi_threshold;
62 return rssi_threshold == 0 ||
64 (s8)-ewma_signal_read(&sta->deflink.rx_stats_avg.signal) >
69 * mesh_plink_fsm_restart - restart a mesh peer link finite state machine
71 * @sta: mesh peer link to restart
73 * Locking: this function must be called holding sta->mesh->plink_lock
75 static inline void mesh_plink_fsm_restart(struct sta_info *sta)
77 lockdep_assert_held(&sta->mesh->plink_lock);
78 sta->mesh->plink_state = NL80211_PLINK_LISTEN;
79 sta->mesh->llid = sta->mesh->plid = sta->mesh->reason = 0;
80 sta->mesh->plink_retries = 0;
84 * mesh_set_short_slot_time - enable / disable ERP short slot time.
86 * The standard indirectly mandates mesh STAs to turn off short slot time by
87 * disallowing advertising this (802.11-2012 8.4.1.4), but that doesn't mean we
88 * can't be sneaky about it. Enable short slot time if all mesh STAs in the
89 * MBSS support ERP rates.
91 * Returns BSS_CHANGED_ERP_SLOT or 0 for no change.
93 static u64 mesh_set_short_slot_time(struct ieee80211_sub_if_data *sdata)
95 struct ieee80211_local *local = sdata->local;
96 struct ieee80211_supported_band *sband;
101 bool short_slot = false;
103 sband = ieee80211_get_sband(sdata);
107 if (sband->band == NL80211_BAND_5GHZ) {
108 /* (IEEE 802.11-2012 19.4.5) */
111 } else if (sband->band != NL80211_BAND_2GHZ) {
115 for (i = 0; i < sband->n_bitrates; i++)
116 if (sband->bitrates[i].flags & IEEE80211_RATE_ERP_G)
123 list_for_each_entry_rcu(sta, &local->sta_list, list) {
124 if (sdata != sta->sdata ||
125 sta->mesh->plink_state != NL80211_PLINK_ESTAB)
129 if (erp_rates & sta->sta.deflink.supp_rates[sband->band])
137 if (sdata->vif.bss_conf.use_short_slot != short_slot) {
138 sdata->vif.bss_conf.use_short_slot = short_slot;
139 changed = BSS_CHANGED_ERP_SLOT;
140 mpl_dbg(sdata, "mesh_plink %pM: ERP short slot time %d\n",
141 sdata->vif.addr, short_slot);
147 * mesh_set_ht_prot_mode - set correct HT protection mode
148 * @sdata: the (mesh) interface to handle
150 * Section 9.23.3.5 of IEEE 80211-2012 describes the protection rules for HT
151 * mesh STA in a MBSS. Three HT protection modes are supported for now, non-HT
152 * mixed mode, 20MHz-protection and no-protection mode. non-HT mixed mode is
153 * selected if any non-HT peers are present in our MBSS. 20MHz-protection mode
154 * is selected if all peers in our 20/40MHz MBSS support HT and at least one
155 * HT20 peer is present. Otherwise no-protection mode is selected.
157 static u64 mesh_set_ht_prot_mode(struct ieee80211_sub_if_data *sdata)
159 struct ieee80211_local *local = sdata->local;
160 struct sta_info *sta;
162 bool non_ht_sta = false, ht20_sta = false;
164 switch (sdata->vif.bss_conf.chandef.width) {
165 case NL80211_CHAN_WIDTH_20_NOHT:
166 case NL80211_CHAN_WIDTH_5:
167 case NL80211_CHAN_WIDTH_10:
174 list_for_each_entry_rcu(sta, &local->sta_list, list) {
175 if (sdata != sta->sdata ||
176 sta->mesh->plink_state != NL80211_PLINK_ESTAB)
179 if (sta->sta.deflink.bandwidth > IEEE80211_STA_RX_BW_20)
182 if (!sta->sta.deflink.ht_cap.ht_supported) {
183 mpl_dbg(sdata, "nonHT sta (%pM) is present\n",
189 mpl_dbg(sdata, "HT20 sta (%pM) is present\n", sta->sta.addr);
195 ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED;
197 sdata->vif.bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
198 ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_20MHZ;
200 ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
202 if (sdata->vif.bss_conf.ht_operation_mode == ht_opmode)
205 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
206 sdata->u.mesh.mshcfg.ht_opmode = ht_opmode;
207 mpl_dbg(sdata, "selected new HT protection mode %d\n", ht_opmode);
208 return BSS_CHANGED_HT;
211 static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
212 struct sta_info *sta,
213 enum ieee80211_self_protected_actioncode action,
214 u8 *da, u16 llid, u16 plid, u16 reason)
216 struct ieee80211_local *local = sdata->local;
218 struct ieee80211_tx_info *info;
219 struct ieee80211_mgmt *mgmt;
220 bool include_plid = false;
221 u16 peering_proto = 0;
223 u8 ie_len_he_cap, ie_len_eht_cap;
224 int hdr_len = offsetofend(struct ieee80211_mgmt, u.action.u.self_prot);
227 ie_len_he_cap = ieee80211_ie_len_he_cap(sdata,
228 NL80211_IFTYPE_MESH_POINT);
229 ie_len_eht_cap = ieee80211_ie_len_eht_cap(sdata,
230 NL80211_IFTYPE_MESH_POINT);
231 skb = dev_alloc_skb(local->tx_headroom +
233 2 + /* capability info */
235 2 + 8 + /* supported rates */
236 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
237 2 + sdata->u.mesh.mesh_id_len +
238 2 + sizeof(struct ieee80211_meshconf_ie) +
239 2 + sizeof(struct ieee80211_ht_cap) +
240 2 + sizeof(struct ieee80211_ht_operation) +
241 2 + sizeof(struct ieee80211_vht_cap) +
242 2 + sizeof(struct ieee80211_vht_operation) +
244 2 + 1 + sizeof(struct ieee80211_he_operation) +
245 sizeof(struct ieee80211_he_6ghz_oper) +
246 2 + 1 + sizeof(struct ieee80211_he_6ghz_capa) +
248 2 + 1 + offsetof(struct ieee80211_eht_operation, optional) +
249 offsetof(struct ieee80211_eht_operation_info, optional) +
250 2 + 8 + /* peering IE */
251 sdata->u.mesh.ie_len);
254 info = IEEE80211_SKB_CB(skb);
255 skb_reserve(skb, local->tx_headroom);
256 mgmt = skb_put_zero(skb, hdr_len);
257 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
258 IEEE80211_STYPE_ACTION);
259 memcpy(mgmt->da, da, ETH_ALEN);
260 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
261 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
262 mgmt->u.action.category = WLAN_CATEGORY_SELF_PROTECTED;
263 mgmt->u.action.u.self_prot.action_code = action;
265 if (action != WLAN_SP_MESH_PEERING_CLOSE) {
266 struct ieee80211_supported_band *sband;
267 enum nl80211_band band;
269 sband = ieee80211_get_sband(sdata);
276 /* capability info */
277 pos = skb_put_zero(skb, 2);
278 if (action == WLAN_SP_MESH_PEERING_CONFIRM) {
280 pos = skb_put(skb, 2);
281 put_unaligned_le16(sta->sta.aid, pos);
283 if (ieee80211_add_srates_ie(sdata, skb, true, band) ||
284 ieee80211_add_ext_srates_ie(sdata, skb, true, band) ||
285 mesh_add_rsn_ie(sdata, skb) ||
286 mesh_add_meshid_ie(sdata, skb) ||
287 mesh_add_meshconf_ie(sdata, skb))
289 } else { /* WLAN_SP_MESH_PEERING_CLOSE */
290 info->flags |= IEEE80211_TX_CTL_NO_ACK;
291 if (mesh_add_meshid_ie(sdata, skb))
295 /* Add Mesh Peering Management element */
297 case WLAN_SP_MESH_PEERING_OPEN:
299 case WLAN_SP_MESH_PEERING_CONFIRM:
303 case WLAN_SP_MESH_PEERING_CLOSE:
308 ie_len += 2; /* reason code */
315 if (WARN_ON(skb_tailroom(skb) < 2 + ie_len))
318 pos = skb_put(skb, 2 + ie_len);
319 *pos++ = WLAN_EID_PEER_MGMT;
321 memcpy(pos, &peering_proto, 2);
323 put_unaligned_le16(llid, pos);
326 put_unaligned_le16(plid, pos);
329 if (action == WLAN_SP_MESH_PEERING_CLOSE) {
330 put_unaligned_le16(reason, pos);
334 if (action != WLAN_SP_MESH_PEERING_CLOSE) {
335 if (mesh_add_ht_cap_ie(sdata, skb) ||
336 mesh_add_ht_oper_ie(sdata, skb) ||
337 mesh_add_vht_cap_ie(sdata, skb) ||
338 mesh_add_vht_oper_ie(sdata, skb) ||
339 mesh_add_he_cap_ie(sdata, skb, ie_len_he_cap) ||
340 mesh_add_he_oper_ie(sdata, skb) ||
341 mesh_add_he_6ghz_cap_ie(sdata, skb) ||
342 mesh_add_eht_cap_ie(sdata, skb, ie_len_eht_cap) ||
343 mesh_add_eht_oper_ie(sdata, skb))
347 if (mesh_add_vendor_ies(sdata, skb))
350 ieee80211_tx_skb(sdata, skb);
358 * __mesh_plink_deactivate - deactivate mesh peer link
360 * @sta: mesh peer link to deactivate
362 * Mesh paths with this peer as next hop should be flushed
363 * by the caller outside of plink_lock.
365 * Returns beacon changed flag if the beacon content changed.
367 * Locking: the caller must hold sta->mesh->plink_lock
369 static u64 __mesh_plink_deactivate(struct sta_info *sta)
371 struct ieee80211_sub_if_data *sdata = sta->sdata;
374 lockdep_assert_held(&sta->mesh->plink_lock);
376 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
377 changed = mesh_plink_dec_estab_count(sdata);
378 sta->mesh->plink_state = NL80211_PLINK_BLOCKED;
380 ieee80211_mps_sta_status_update(sta);
381 changed |= ieee80211_mps_set_sta_local_pm(sta,
382 NL80211_MESH_POWER_UNKNOWN);
388 * mesh_plink_deactivate - deactivate mesh peer link
390 * @sta: mesh peer link to deactivate
392 * All mesh paths with this peer as next hop will be flushed
394 u64 mesh_plink_deactivate(struct sta_info *sta)
396 struct ieee80211_sub_if_data *sdata = sta->sdata;
399 spin_lock_bh(&sta->mesh->plink_lock);
400 changed = __mesh_plink_deactivate(sta);
402 if (!sdata->u.mesh.user_mpm) {
403 sta->mesh->reason = WLAN_REASON_MESH_PEER_CANCELED;
404 mesh_plink_frame_tx(sdata, sta, WLAN_SP_MESH_PEERING_CLOSE,
405 sta->sta.addr, sta->mesh->llid,
406 sta->mesh->plid, sta->mesh->reason);
408 spin_unlock_bh(&sta->mesh->plink_lock);
409 if (!sdata->u.mesh.user_mpm)
410 del_timer_sync(&sta->mesh->plink_timer);
411 mesh_path_flush_by_nexthop(sta);
413 /* make sure no readers can access nexthop sta from here on */
419 static void mesh_sta_info_init(struct ieee80211_sub_if_data *sdata,
420 struct sta_info *sta,
421 struct ieee802_11_elems *elems)
423 struct ieee80211_local *local = sdata->local;
424 struct ieee80211_supported_band *sband;
425 u32 rates, basic_rates = 0, changed = 0;
426 enum ieee80211_sta_rx_bandwidth bw = sta->sta.deflink.bandwidth;
428 sband = ieee80211_get_sband(sdata);
432 rates = ieee80211_sta_get_rates(sdata, elems, sband->band,
435 spin_lock_bh(&sta->mesh->plink_lock);
436 sta->deflink.rx_stats.last_rx = jiffies;
438 /* rates and capabilities don't change during peering */
439 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB &&
440 sta->mesh->processed_beacon)
442 sta->mesh->processed_beacon = true;
444 if (sta->sta.deflink.supp_rates[sband->band] != rates)
445 changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
446 sta->sta.deflink.supp_rates[sband->band] = rates;
448 if (ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
451 changed |= IEEE80211_RC_BW_CHANGED;
453 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
454 elems->vht_cap_elem, NULL,
457 ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband, elems->he_cap,
462 ieee80211_eht_cap_ie_to_sta_eht_cap(sdata, sband, elems->he_cap,
464 elems->eht_cap, elems->eht_cap_len,
467 if (bw != sta->sta.deflink.bandwidth)
468 changed |= IEEE80211_RC_BW_CHANGED;
470 /* HT peer is operating 20MHz-only */
471 if (elems->ht_operation &&
472 !(elems->ht_operation->ht_param &
473 IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
474 if (sta->sta.deflink.bandwidth != IEEE80211_STA_RX_BW_20)
475 changed |= IEEE80211_RC_BW_CHANGED;
476 sta->sta.deflink.bandwidth = IEEE80211_STA_RX_BW_20;
479 if (!test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
480 rate_control_rate_init(sta);
482 rate_control_rate_update(local, sband, sta, 0, changed);
484 spin_unlock_bh(&sta->mesh->plink_lock);
487 static int mesh_allocate_aid(struct ieee80211_sub_if_data *sdata)
489 struct sta_info *sta;
490 unsigned long *aid_map;
493 aid_map = bitmap_zalloc(IEEE80211_MAX_AID + 1, GFP_KERNEL);
497 /* reserve aid 0 for mcast indication */
498 __set_bit(0, aid_map);
501 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list)
502 __set_bit(sta->sta.aid, aid_map);
505 aid = find_first_zero_bit(aid_map, IEEE80211_MAX_AID + 1);
506 bitmap_free(aid_map);
508 if (aid > IEEE80211_MAX_AID)
514 static struct sta_info *
515 __mesh_sta_info_alloc(struct ieee80211_sub_if_data *sdata, u8 *hw_addr)
517 struct sta_info *sta;
520 if (sdata->local->num_sta >= MESH_MAX_PLINKS)
523 aid = mesh_allocate_aid(sdata);
527 sta = sta_info_alloc(sdata, hw_addr, GFP_KERNEL);
531 sta->mesh->plink_state = NL80211_PLINK_LISTEN;
535 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
536 sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
537 sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
542 static struct sta_info *
543 mesh_sta_info_alloc(struct ieee80211_sub_if_data *sdata, u8 *addr,
544 struct ieee802_11_elems *elems,
545 struct ieee80211_rx_status *rx_status)
547 struct sta_info *sta = NULL;
549 /* Userspace handles station allocation */
550 if (sdata->u.mesh.user_mpm ||
551 sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED) {
552 if (mesh_peer_accepts_plinks(elems) &&
553 mesh_plink_availables(sdata)) {
556 if (ieee80211_hw_check(&sdata->local->hw, SIGNAL_DBM))
557 sig = rx_status->signal;
559 cfg80211_notify_new_peer_candidate(sdata->dev, addr,
565 sta = __mesh_sta_info_alloc(sdata, addr);
571 * mesh_sta_info_get - return mesh sta info entry for @addr.
573 * @sdata: local meshif
574 * @addr: peer's address
575 * @elems: IEs from beacon or mesh peering frame.
576 * @rx_status: rx status for the frame for signal reporting
578 * Return existing or newly allocated sta_info under RCU read lock.
579 * (re)initialize with given IEs.
581 static struct sta_info *
582 mesh_sta_info_get(struct ieee80211_sub_if_data *sdata,
583 u8 *addr, struct ieee802_11_elems *elems,
584 struct ieee80211_rx_status *rx_status) __acquires(RCU)
586 struct sta_info *sta = NULL;
589 sta = sta_info_get(sdata, addr);
591 mesh_sta_info_init(sdata, sta, elems);
594 /* can't run atomic */
595 sta = mesh_sta_info_alloc(sdata, addr, elems, rx_status);
601 mesh_sta_info_init(sdata, sta, elems);
603 if (sta_info_insert_rcu(sta))
611 * mesh_neighbour_update - update or initialize new mesh neighbor.
613 * @sdata: local meshif
614 * @addr: peer's address
615 * @elems: IEs from beacon or mesh peering frame
616 * @rx_status: rx status for the frame for signal reporting
618 * Initiates peering if appropriate.
620 void mesh_neighbour_update(struct ieee80211_sub_if_data *sdata,
622 struct ieee802_11_elems *elems,
623 struct ieee80211_rx_status *rx_status)
625 struct sta_info *sta;
628 sta = mesh_sta_info_get(sdata, hw_addr, elems, rx_status);
632 sta->mesh->connected_to_gate = elems->mesh_config->meshconf_form &
633 IEEE80211_MESHCONF_FORM_CONNECTED_TO_GATE;
635 if (mesh_peer_accepts_plinks(elems) &&
636 sta->mesh->plink_state == NL80211_PLINK_LISTEN &&
637 sdata->u.mesh.accepting_plinks &&
638 sdata->u.mesh.mshcfg.auto_open_plinks &&
639 rssi_threshold_check(sdata, sta))
640 changed = mesh_plink_open(sta);
642 ieee80211_mps_frame_release(sta, elems);
645 ieee80211_mbss_info_change_notify(sdata, changed);
648 void mesh_plink_timer(struct timer_list *t)
650 struct mesh_sta *mesh = from_timer(mesh, t, plink_timer);
651 struct sta_info *sta;
653 struct ieee80211_sub_if_data *sdata;
654 struct mesh_config *mshcfg;
655 enum ieee80211_self_protected_actioncode action = 0;
658 * This STA is valid because sta_info_destroy() will
659 * del_timer_sync() this timer after having made sure
660 * it cannot be readded (by deleting the plink.)
662 sta = mesh->plink_sta;
664 if (sta->sdata->local->quiescing)
667 spin_lock_bh(&sta->mesh->plink_lock);
669 /* If a timer fires just before a state transition on another CPU,
670 * we may have already extended the timeout and changed state by the
671 * time we've acquired the lock and arrived here. In that case,
672 * skip this timer and wait for the new one.
674 if (time_before(jiffies, sta->mesh->plink_timer.expires)) {
676 "Ignoring timer for %pM in state %s (timer adjusted)",
677 sta->sta.addr, mplstates[sta->mesh->plink_state]);
678 spin_unlock_bh(&sta->mesh->plink_lock);
682 /* del_timer() and handler may race when entering these states */
683 if (sta->mesh->plink_state == NL80211_PLINK_LISTEN ||
684 sta->mesh->plink_state == NL80211_PLINK_ESTAB) {
686 "Ignoring timer for %pM in state %s (timer deleted)",
687 sta->sta.addr, mplstates[sta->mesh->plink_state]);
688 spin_unlock_bh(&sta->mesh->plink_lock);
693 "Mesh plink timer for %pM fired on state %s\n",
694 sta->sta.addr, mplstates[sta->mesh->plink_state]);
696 mshcfg = &sdata->u.mesh.mshcfg;
698 switch (sta->mesh->plink_state) {
699 case NL80211_PLINK_OPN_RCVD:
700 case NL80211_PLINK_OPN_SNT:
702 if (sta->mesh->plink_retries < mshcfg->dot11MeshMaxRetries) {
705 "Mesh plink for %pM (retry, timeout): %d %d\n",
706 sta->sta.addr, sta->mesh->plink_retries,
707 sta->mesh->plink_timeout);
708 get_random_bytes(&rand, sizeof(u32));
709 sta->mesh->plink_timeout = sta->mesh->plink_timeout +
710 rand % sta->mesh->plink_timeout;
711 ++sta->mesh->plink_retries;
712 mod_plink_timer(sta, sta->mesh->plink_timeout);
713 action = WLAN_SP_MESH_PEERING_OPEN;
716 reason = WLAN_REASON_MESH_MAX_RETRIES;
718 case NL80211_PLINK_CNF_RCVD:
721 reason = WLAN_REASON_MESH_CONFIRM_TIMEOUT;
722 sta->mesh->plink_state = NL80211_PLINK_HOLDING;
723 mod_plink_timer(sta, mshcfg->dot11MeshHoldingTimeout);
724 action = WLAN_SP_MESH_PEERING_CLOSE;
726 case NL80211_PLINK_HOLDING:
728 del_timer(&sta->mesh->plink_timer);
729 mesh_plink_fsm_restart(sta);
734 spin_unlock_bh(&sta->mesh->plink_lock);
736 mesh_plink_frame_tx(sdata, sta, action, sta->sta.addr,
737 sta->mesh->llid, sta->mesh->plid, reason);
740 static inline void mesh_plink_timer_set(struct sta_info *sta, u32 timeout)
742 sta->mesh->plink_timeout = timeout;
743 mod_timer(&sta->mesh->plink_timer, jiffies + msecs_to_jiffies(timeout));
746 static bool llid_in_use(struct ieee80211_sub_if_data *sdata,
749 struct ieee80211_local *local = sdata->local;
751 struct sta_info *sta;
754 list_for_each_entry_rcu(sta, &local->sta_list, list) {
755 if (sdata != sta->sdata)
758 if (!memcmp(&sta->mesh->llid, &llid, sizeof(llid))) {
768 static u16 mesh_get_new_llid(struct ieee80211_sub_if_data *sdata)
773 get_random_bytes(&llid, sizeof(llid));
774 } while (llid_in_use(sdata, llid));
779 u64 mesh_plink_open(struct sta_info *sta)
781 struct ieee80211_sub_if_data *sdata = sta->sdata;
784 if (!test_sta_flag(sta, WLAN_STA_AUTH))
787 spin_lock_bh(&sta->mesh->plink_lock);
788 sta->mesh->llid = mesh_get_new_llid(sdata);
789 if (sta->mesh->plink_state != NL80211_PLINK_LISTEN &&
790 sta->mesh->plink_state != NL80211_PLINK_BLOCKED) {
791 spin_unlock_bh(&sta->mesh->plink_lock);
794 sta->mesh->plink_state = NL80211_PLINK_OPN_SNT;
795 mesh_plink_timer_set(sta, sdata->u.mesh.mshcfg.dot11MeshRetryTimeout);
796 spin_unlock_bh(&sta->mesh->plink_lock);
798 "Mesh plink: starting establishment with %pM\n",
801 /* set the non-peer mode to active during peering */
802 changed = ieee80211_mps_local_status_update(sdata);
804 mesh_plink_frame_tx(sdata, sta, WLAN_SP_MESH_PEERING_OPEN,
805 sta->sta.addr, sta->mesh->llid, 0, 0);
809 u64 mesh_plink_block(struct sta_info *sta)
813 spin_lock_bh(&sta->mesh->plink_lock);
814 changed = __mesh_plink_deactivate(sta);
815 sta->mesh->plink_state = NL80211_PLINK_BLOCKED;
816 spin_unlock_bh(&sta->mesh->plink_lock);
817 mesh_path_flush_by_nexthop(sta);
822 static void mesh_plink_close(struct ieee80211_sub_if_data *sdata,
823 struct sta_info *sta,
824 enum plink_event event)
826 struct mesh_config *mshcfg = &sdata->u.mesh.mshcfg;
827 u16 reason = (event == CLS_ACPT) ?
828 WLAN_REASON_MESH_CLOSE : WLAN_REASON_MESH_CONFIG;
830 sta->mesh->reason = reason;
831 sta->mesh->plink_state = NL80211_PLINK_HOLDING;
832 mod_plink_timer(sta, mshcfg->dot11MeshHoldingTimeout);
835 static u64 mesh_plink_establish(struct ieee80211_sub_if_data *sdata,
836 struct sta_info *sta)
838 struct mesh_config *mshcfg = &sdata->u.mesh.mshcfg;
841 del_timer(&sta->mesh->plink_timer);
842 sta->mesh->plink_state = NL80211_PLINK_ESTAB;
843 changed |= mesh_plink_inc_estab_count(sdata);
844 changed |= mesh_set_ht_prot_mode(sdata);
845 changed |= mesh_set_short_slot_time(sdata);
846 mpl_dbg(sdata, "Mesh plink with %pM ESTABLISHED\n", sta->sta.addr);
847 ieee80211_mps_sta_status_update(sta);
848 changed |= ieee80211_mps_set_sta_local_pm(sta, mshcfg->power_mode);
853 * mesh_plink_fsm - step @sta MPM based on @event
856 * @sta: mesh neighbor
857 * @event: peering event
859 * Return: changed MBSS flags
861 static u64 mesh_plink_fsm(struct ieee80211_sub_if_data *sdata,
862 struct sta_info *sta, enum plink_event event)
864 struct mesh_config *mshcfg = &sdata->u.mesh.mshcfg;
865 enum ieee80211_self_protected_actioncode action = 0;
869 mpl_dbg(sdata, "peer %pM in state %s got event %s\n", sta->sta.addr,
870 mplstates[sta->mesh->plink_state], mplevents[event]);
872 spin_lock_bh(&sta->mesh->plink_lock);
873 switch (sta->mesh->plink_state) {
874 case NL80211_PLINK_LISTEN:
877 mesh_plink_fsm_restart(sta);
880 sta->mesh->plink_state = NL80211_PLINK_OPN_RCVD;
881 sta->mesh->llid = mesh_get_new_llid(sdata);
882 mesh_plink_timer_set(sta,
883 mshcfg->dot11MeshRetryTimeout);
885 /* set the non-peer mode to active during peering */
886 changed |= ieee80211_mps_local_status_update(sdata);
887 action = WLAN_SP_MESH_PEERING_OPEN;
893 case NL80211_PLINK_OPN_SNT:
898 mesh_plink_close(sdata, sta, event);
899 action = WLAN_SP_MESH_PEERING_CLOSE;
902 /* retry timer is left untouched */
903 sta->mesh->plink_state = NL80211_PLINK_OPN_RCVD;
904 action = WLAN_SP_MESH_PEERING_CONFIRM;
907 sta->mesh->plink_state = NL80211_PLINK_CNF_RCVD;
908 mod_plink_timer(sta, mshcfg->dot11MeshConfirmTimeout);
914 case NL80211_PLINK_OPN_RCVD:
919 mesh_plink_close(sdata, sta, event);
920 action = WLAN_SP_MESH_PEERING_CLOSE;
923 action = WLAN_SP_MESH_PEERING_CONFIRM;
926 changed |= mesh_plink_establish(sdata, sta);
932 case NL80211_PLINK_CNF_RCVD:
937 mesh_plink_close(sdata, sta, event);
938 action = WLAN_SP_MESH_PEERING_CLOSE;
941 changed |= mesh_plink_establish(sdata, sta);
942 action = WLAN_SP_MESH_PEERING_CONFIRM;
948 case NL80211_PLINK_ESTAB:
951 changed |= __mesh_plink_deactivate(sta);
952 changed |= mesh_set_ht_prot_mode(sdata);
953 changed |= mesh_set_short_slot_time(sdata);
954 mesh_plink_close(sdata, sta, event);
955 action = WLAN_SP_MESH_PEERING_CLOSE;
959 action = WLAN_SP_MESH_PEERING_CONFIRM;
965 case NL80211_PLINK_HOLDING:
968 del_timer(&sta->mesh->plink_timer);
969 mesh_plink_fsm_restart(sta);
975 action = WLAN_SP_MESH_PEERING_CLOSE;
982 /* should not get here, PLINK_BLOCKED is dealt with at the
983 * beginning of the function
987 spin_unlock_bh(&sta->mesh->plink_lock);
989 mesh_path_flush_by_nexthop(sta);
991 mesh_plink_frame_tx(sdata, sta, action, sta->sta.addr,
992 sta->mesh->llid, sta->mesh->plid,
995 /* also send confirm in open case */
996 if (action == WLAN_SP_MESH_PEERING_OPEN) {
997 mesh_plink_frame_tx(sdata, sta,
998 WLAN_SP_MESH_PEERING_CONFIRM,
999 sta->sta.addr, sta->mesh->llid,
1000 sta->mesh->plid, 0);
1008 * mesh_plink_get_event - get correct MPM event
1011 * @sta: peer, leave NULL if processing a frame from a new suitable peer
1012 * @elems: peering management IEs
1013 * @ftype: frame type
1014 * @llid: peer's peer link ID
1015 * @plid: peer's local link ID
1017 * Return: new peering event for @sta, but PLINK_UNDEFINED should be treated as
1020 static enum plink_event
1021 mesh_plink_get_event(struct ieee80211_sub_if_data *sdata,
1022 struct sta_info *sta,
1023 struct ieee802_11_elems *elems,
1024 enum ieee80211_self_protected_actioncode ftype,
1027 enum plink_event event = PLINK_UNDEFINED;
1028 u8 ie_len = elems->peering_len;
1031 matches_local = (ftype == WLAN_SP_MESH_PEERING_CLOSE ||
1032 mesh_matches_local(sdata, elems));
1034 /* deny open request from non-matching peer */
1035 if (!matches_local && !sta) {
1041 if (ftype != WLAN_SP_MESH_PEERING_OPEN) {
1042 mpl_dbg(sdata, "Mesh plink: cls or cnf from unknown peer\n");
1045 /* ftype == WLAN_SP_MESH_PEERING_OPEN */
1046 if (!mesh_plink_free_count(sdata)) {
1047 mpl_dbg(sdata, "Mesh plink error: no more free plinks\n");
1051 /* new matching peer */
1055 if (!test_sta_flag(sta, WLAN_STA_AUTH)) {
1056 mpl_dbg(sdata, "Mesh plink: Action frame from non-authed peer\n");
1059 if (sta->mesh->plink_state == NL80211_PLINK_BLOCKED)
1064 case WLAN_SP_MESH_PEERING_OPEN:
1067 if (!mesh_plink_free_count(sdata) ||
1068 (sta->mesh->plid && sta->mesh->plid != plid))
1073 case WLAN_SP_MESH_PEERING_CONFIRM:
1076 if (!mesh_plink_free_count(sdata) ||
1077 sta->mesh->llid != llid ||
1078 (sta->mesh->plid && sta->mesh->plid != plid))
1083 case WLAN_SP_MESH_PEERING_CLOSE:
1084 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
1085 /* Do not check for llid or plid. This does not
1086 * follow the standard but since multiple plinks
1087 * per sta are not supported, it is necessary in
1088 * order to avoid a livelock when MP A sees an
1089 * establish peer link to MP B but MP B does not
1090 * see it. This can be caused by a timeout in
1091 * B's peer link establishment or B beign
1095 else if (sta->mesh->plid != plid)
1097 else if (ie_len == 8 && sta->mesh->llid != llid)
1103 mpl_dbg(sdata, "Mesh plink: unknown frame subtype\n");
1112 mesh_process_plink_frame(struct ieee80211_sub_if_data *sdata,
1113 struct ieee80211_mgmt *mgmt,
1114 struct ieee802_11_elems *elems,
1115 struct ieee80211_rx_status *rx_status)
1118 struct sta_info *sta;
1119 enum plink_event event;
1120 enum ieee80211_self_protected_actioncode ftype;
1122 u8 ie_len = elems->peering_len;
1125 if (!elems->peering) {
1127 "Mesh plink: missing necessary peer link ie\n");
1131 if (elems->rsn_len &&
1132 sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
1134 "Mesh plink: can't establish link with secure peer\n");
1138 ftype = mgmt->u.action.u.self_prot.action_code;
1139 if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
1140 (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
1141 (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
1144 "Mesh plink: incorrect plink ie length %d %d\n",
1149 if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
1150 (!elems->mesh_id || !elems->mesh_config)) {
1151 mpl_dbg(sdata, "Mesh plink: missing necessary ie\n");
1154 /* Note the lines below are correct, the llid in the frame is the plid
1155 * from the point of view of this host.
1157 plid = get_unaligned_le16(PLINK_GET_LLID(elems->peering));
1158 if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
1159 (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
1160 llid = get_unaligned_le16(PLINK_GET_PLID(elems->peering));
1162 /* WARNING: Only for sta pointer, is dropped & re-acquired */
1165 sta = sta_info_get(sdata, mgmt->sa);
1167 if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
1168 !rssi_threshold_check(sdata, sta)) {
1169 mpl_dbg(sdata, "Mesh plink: %pM does not meet rssi threshold\n",
1174 /* Now we will figure out the appropriate event... */
1175 event = mesh_plink_get_event(sdata, sta, elems, ftype, llid, plid);
1177 if (event == OPN_ACPT) {
1179 /* allocate sta entry if necessary and update info */
1180 sta = mesh_sta_info_get(sdata, mgmt->sa, elems, rx_status);
1182 mpl_dbg(sdata, "Mesh plink: failed to init peer!\n");
1185 sta->mesh->plid = plid;
1186 } else if (!sta && event == OPN_RJCT) {
1187 mesh_plink_frame_tx(sdata, NULL, WLAN_SP_MESH_PEERING_CLOSE,
1189 WLAN_REASON_MESH_CONFIG);
1191 } else if (!sta || event == PLINK_UNDEFINED) {
1192 /* something went wrong */
1196 if (event == CNF_ACPT) {
1197 /* 802.11-2012 13.3.7.2 - update plid on CNF if not set */
1198 if (!sta->mesh->plid)
1199 sta->mesh->plid = plid;
1201 sta->mesh->aid = get_unaligned_le16(PLINK_CNF_AID(mgmt));
1204 changed |= mesh_plink_fsm(sdata, sta, event);
1210 ieee80211_mbss_info_change_notify(sdata, changed);
1213 void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata,
1214 struct ieee80211_mgmt *mgmt, size_t len,
1215 struct ieee80211_rx_status *rx_status)
1217 struct ieee802_11_elems *elems;
1221 /* need action_code, aux */
1222 if (len < IEEE80211_MIN_ACTION_SIZE + 3)
1225 if (sdata->u.mesh.user_mpm)
1226 /* userspace must register for these */
1229 if (is_multicast_ether_addr(mgmt->da)) {
1231 "Mesh plink: ignore frame from multicast address\n");
1235 baseaddr = mgmt->u.action.u.self_prot.variable;
1236 baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
1237 if (mgmt->u.action.u.self_prot.action_code ==
1238 WLAN_SP_MESH_PEERING_CONFIRM) {
1245 elems = ieee802_11_parse_elems(baseaddr, len - baselen, true, NULL);
1246 mesh_process_plink_frame(sdata, mgmt, elems, rx_status);