blob: a5aa0345dd7e17dbf6f8dbd3efb60be717d80988 [file] [log] [blame]
Luis Carlos Coboc3896d22008-02-23 15:17:13 +01001/*
Rui Paulo264d9b72009-11-09 23:46:58 +00002 * Copyright (c) 2008, 2009 open80211s Ltd.
Luis Carlos Coboc3896d22008-02-23 15:17:13 +01003 * Author: Luis Carlos Cobo <luisca@cozybit.com>
4 *
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.
8 */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/gfp.h>
Johannes Berg902acc72008-02-23 15:17:19 +010010#include <linux/kernel.h>
11#include <linux/random.h>
Luis Carlos Coboc3896d22008-02-23 15:17:13 +010012#include "ieee80211_i.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040013#include "rate.h"
Luis Carlos Coboc3896d22008-02-23 15:17:13 +010014#include "mesh.h"
Luis Carlos Coboc3896d22008-02-23 15:17:13 +010015
Thomas Pedersen8db09852011-08-12 20:01:00 -070016#define PLINK_GET_LLID(p) (p + 2)
17#define PLINK_GET_PLID(p) (p + 4)
Luis Carlos Coboc3896d22008-02-23 15:17:13 +010018
Johannes Berg433f5bc2015-06-17 10:31:00 +020019#define mod_plink_timer(s, t) (mod_timer(&s->mesh->plink_timer, \
Nicholas Mc Guirecc57ac52015-03-02 04:54:13 -050020 jiffies + msecs_to_jiffies(t)))
Luis Carlos Coboc3896d22008-02-23 15:17:13 +010021
Luis Carlos Coboc3896d22008-02-23 15:17:13 +010022enum plink_event {
23 PLINK_UNDEFINED,
24 OPN_ACPT,
25 OPN_RJCT,
26 OPN_IGNR,
27 CNF_ACPT,
28 CNF_RJCT,
29 CNF_IGNR,
30 CLS_ACPT,
31 CLS_IGNR
32};
33
Thomas Pedersen95e48ad2013-02-16 10:41:05 -080034static const char * const mplstates[] = {
35 [NL80211_PLINK_LISTEN] = "LISTEN",
36 [NL80211_PLINK_OPN_SNT] = "OPN-SNT",
37 [NL80211_PLINK_OPN_RCVD] = "OPN-RCVD",
38 [NL80211_PLINK_CNF_RCVD] = "CNF_RCVD",
39 [NL80211_PLINK_ESTAB] = "ESTAB",
40 [NL80211_PLINK_HOLDING] = "HOLDING",
41 [NL80211_PLINK_BLOCKED] = "BLOCKED"
42};
43
44static const char * const mplevents[] = {
45 [PLINK_UNDEFINED] = "NONE",
46 [OPN_ACPT] = "OPN_ACPT",
47 [OPN_RJCT] = "OPN_RJCT",
48 [OPN_IGNR] = "OPN_IGNR",
49 [CNF_ACPT] = "CNF_ACPT",
50 [CNF_RJCT] = "CNF_RJCT",
51 [CNF_IGNR] = "CNF_IGNR",
52 [CLS_ACPT] = "CLS_ACPT",
53 [CLS_IGNR] = "CLS_IGNR"
54};
55
Bob Copeland36c9bb22013-11-05 11:16:54 -080056/* We only need a valid sta if user configured a minimum rssi_threshold. */
57static bool rssi_threshold_check(struct ieee80211_sub_if_data *sdata,
58 struct sta_info *sta)
59{
60 s32 rssi_threshold = sdata->u.mesh.mshcfg.rssi_threshold;
61 return rssi_threshold == 0 ||
62 (sta && (s8) -ewma_read(&sta->avg_signal) > rssi_threshold);
63}
64
Luis Carlos Coboc3896d22008-02-23 15:17:13 +010065/**
66 * mesh_plink_fsm_restart - restart a mesh peer link finite state machine
67 *
Rui Paulo23c7a292009-11-09 23:46:42 +000068 * @sta: mesh peer link to restart
Luis Carlos Coboc3896d22008-02-23 15:17:13 +010069 *
Johannes Berg433f5bc2015-06-17 10:31:00 +020070 * Locking: this function must be called holding sta->mesh->plink_lock
Luis Carlos Coboc3896d22008-02-23 15:17:13 +010071 */
72static inline void mesh_plink_fsm_restart(struct sta_info *sta)
73{
Johannes Berg433f5bc2015-06-17 10:31:00 +020074 lockdep_assert_held(&sta->mesh->plink_lock);
75 sta->mesh->plink_state = NL80211_PLINK_LISTEN;
76 sta->mesh->llid = sta->mesh->plid = sta->mesh->reason = 0;
77 sta->mesh->plink_retries = 0;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +010078}
79
Thomas Pedersen3b144652013-01-26 16:39:55 -080080/*
81 * mesh_set_short_slot_time - enable / disable ERP short slot time.
82 *
83 * The standard indirectly mandates mesh STAs to turn off short slot time by
84 * disallowing advertising this (802.11-2012 8.4.1.4), but that doesn't mean we
85 * can't be sneaky about it. Enable short slot time if all mesh STAs in the
86 * MBSS support ERP rates.
87 *
88 * Returns BSS_CHANGED_ERP_SLOT or 0 for no change.
89 */
90static u32 mesh_set_short_slot_time(struct ieee80211_sub_if_data *sdata)
91{
92 struct ieee80211_local *local = sdata->local;
93 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
94 struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
95 struct sta_info *sta;
96 u32 erp_rates = 0, changed = 0;
97 int i;
98 bool short_slot = false;
99
100 if (band == IEEE80211_BAND_5GHZ) {
101 /* (IEEE 802.11-2012 19.4.5) */
102 short_slot = true;
103 goto out;
Johannes Bergea1b2b452015-06-02 20:15:49 +0200104 } else if (band != IEEE80211_BAND_2GHZ)
Thomas Pedersen3b144652013-01-26 16:39:55 -0800105 goto out;
106
107 for (i = 0; i < sband->n_bitrates; i++)
108 if (sband->bitrates[i].flags & IEEE80211_RATE_ERP_G)
109 erp_rates |= BIT(i);
110
111 if (!erp_rates)
112 goto out;
113
114 rcu_read_lock();
115 list_for_each_entry_rcu(sta, &local->sta_list, list) {
116 if (sdata != sta->sdata ||
Johannes Berg433f5bc2015-06-17 10:31:00 +0200117 sta->mesh->plink_state != NL80211_PLINK_ESTAB)
Thomas Pedersen3b144652013-01-26 16:39:55 -0800118 continue;
119
120 short_slot = false;
121 if (erp_rates & sta->sta.supp_rates[band])
122 short_slot = true;
123 else
124 break;
125 }
126 rcu_read_unlock();
127
128out:
129 if (sdata->vif.bss_conf.use_short_slot != short_slot) {
130 sdata->vif.bss_conf.use_short_slot = short_slot;
131 changed = BSS_CHANGED_ERP_SLOT;
132 mpl_dbg(sdata, "mesh_plink %pM: ERP short slot time %d\n",
133 sdata->vif.addr, short_slot);
134 }
135 return changed;
136}
137
Ben Hutchings2c530402012-07-10 10:55:09 +0000138/**
Ashok Nagarajancbf93222012-05-07 21:00:31 -0700139 * mesh_set_ht_prot_mode - set correct HT protection mode
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700140 *
Ashok Nagarajancbf93222012-05-07 21:00:31 -0700141 * Section 9.23.3.5 of IEEE 80211-2012 describes the protection rules for HT
142 * mesh STA in a MBSS. Three HT protection modes are supported for now, non-HT
143 * mixed mode, 20MHz-protection and no-protection mode. non-HT mixed mode is
144 * selected if any non-HT peers are present in our MBSS. 20MHz-protection mode
145 * is selected if all peers in our 20/40MHz MBSS support HT and atleast one
146 * HT20 peer is present. Otherwise no-protection mode is selected.
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700147 */
148static u32 mesh_set_ht_prot_mode(struct ieee80211_sub_if_data *sdata)
149{
150 struct ieee80211_local *local = sdata->local;
151 struct sta_info *sta;
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700152 u16 ht_opmode;
153 bool non_ht_sta = false, ht20_sta = false;
154
Simon Wunderlich0418a442013-05-16 13:00:31 +0200155 switch (sdata->vif.bss_conf.chandef.width) {
156 case NL80211_CHAN_WIDTH_20_NOHT:
157 case NL80211_CHAN_WIDTH_5:
158 case NL80211_CHAN_WIDTH_10:
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700159 return 0;
Simon Wunderlich0418a442013-05-16 13:00:31 +0200160 default:
161 break;
162 }
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700163
164 rcu_read_lock();
165 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Ashok Nagarajancbf93222012-05-07 21:00:31 -0700166 if (sdata != sta->sdata ||
Johannes Berg433f5bc2015-06-17 10:31:00 +0200167 sta->mesh->plink_state != NL80211_PLINK_ESTAB)
Ashok Nagarajancbf93222012-05-07 21:00:31 -0700168 continue;
169
Thomas Pedersen52ac8c42013-02-16 10:41:04 -0800170 if (sta->sta.bandwidth > IEEE80211_STA_RX_BW_20)
171 continue;
172
173 if (!sta->sta.ht_cap.ht_supported) {
174 mpl_dbg(sdata, "nonHT sta (%pM) is present\n",
175 sta->sta.addr);
Ashok Nagarajancbf93222012-05-07 21:00:31 -0700176 non_ht_sta = true;
Ashok Nagarajancbf93222012-05-07 21:00:31 -0700177 break;
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700178 }
Thomas Pedersen52ac8c42013-02-16 10:41:04 -0800179
180 mpl_dbg(sdata, "HT20 sta (%pM) is present\n", sta->sta.addr);
181 ht20_sta = true;
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700182 }
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700183 rcu_read_unlock();
184
185 if (non_ht_sta)
186 ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED;
Johannes Berg466f3102012-07-25 13:51:49 +0200187 else if (ht20_sta &&
Johannes Berg4bf88532012-11-09 11:39:59 +0100188 sdata->vif.bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700189 ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_20MHZ;
190 else
191 ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
192
Thomas Pedersen52ac8c42013-02-16 10:41:04 -0800193 if (sdata->vif.bss_conf.ht_operation_mode == ht_opmode)
194 return 0;
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700195
Thomas Pedersen52ac8c42013-02-16 10:41:04 -0800196 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
197 sdata->u.mesh.mshcfg.ht_opmode = ht_opmode;
198 mpl_dbg(sdata, "selected new HT protection mode %d\n", ht_opmode);
199 return BSS_CHANGED_HT;
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700200}
201
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200202static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
Johannes Bergbf7cd942013-02-15 14:40:31 +0100203 enum ieee80211_self_protected_actioncode action,
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +0800204 u8 *da, u16 llid, u16 plid, u16 reason)
Johannes Bergbf7cd942013-02-15 14:40:31 +0100205{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200206 struct ieee80211_local *local = sdata->local;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700207 struct sk_buff *skb;
Thomas Pedersene7570df2012-08-03 12:21:34 -0700208 struct ieee80211_tx_info *info;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100209 struct ieee80211_mgmt *mgmt;
210 bool include_plid = false;
Thomas Pedersen8db09852011-08-12 20:01:00 -0700211 u16 peering_proto = 0;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700212 u8 *pos, ie_len = 4;
213 int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.self_prot) +
214 sizeof(mgmt->u.action.u.self_prot);
Thomas Pedersenf609a432012-08-03 12:21:35 -0700215 int err = -ENOMEM;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100216
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800217 skb = dev_alloc_skb(local->tx_headroom +
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700218 hdr_len +
219 2 + /* capability info */
220 2 + /* AID */
221 2 + 8 + /* supported rates */
222 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
223 2 + sdata->u.mesh.mesh_id_len +
224 2 + sizeof(struct ieee80211_meshconf_ie) +
Thomas Pedersen176f3602011-10-26 14:47:27 -0700225 2 + sizeof(struct ieee80211_ht_cap) +
Johannes Berg074d46d2012-03-15 19:45:16 +0100226 2 + sizeof(struct ieee80211_ht_operation) +
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700227 2 + 8 + /* peering IE */
228 sdata->u.mesh.ie_len);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100229 if (!skb)
Bob Copeland87d84c42013-11-05 11:16:57 -0800230 return err;
Thomas Pedersene7570df2012-08-03 12:21:34 -0700231 info = IEEE80211_SKB_CB(skb);
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800232 skb_reserve(skb, local->tx_headroom);
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700233 mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
234 memset(mgmt, 0, hdr_len);
Harvey Harrisone7827a72008-07-15 18:44:13 -0700235 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
236 IEEE80211_STYPE_ACTION);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100237 memcpy(mgmt->da, da, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100238 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Javier Cardona915b5c52011-05-03 16:57:10 -0700239 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700240 mgmt->u.action.category = WLAN_CATEGORY_SELF_PROTECTED;
241 mgmt->u.action.u.self_prot.action_code = action;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100242
Thomas Pedersen8db09852011-08-12 20:01:00 -0700243 if (action != WLAN_SP_MESH_PEERING_CLOSE) {
Johannes Berg55de9082012-07-26 17:24:39 +0200244 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
245
Thomas Pedersen8db09852011-08-12 20:01:00 -0700246 /* capability info */
247 pos = skb_put(skb, 2);
248 memset(pos, 0, 2);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700249 if (action == WLAN_SP_MESH_PEERING_CONFIRM) {
Thomas Pedersen8db09852011-08-12 20:01:00 -0700250 /* AID */
251 pos = skb_put(skb, 2);
Bob Copeland2ea752c2015-07-14 08:31:55 -0400252 put_unaligned_le16(plid, pos);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100253 }
Johannes Berg55de9082012-07-26 17:24:39 +0200254 if (ieee80211_add_srates_ie(sdata, skb, true, band) ||
255 ieee80211_add_ext_srates_ie(sdata, skb, true, band) ||
Johannes Bergbf7cd942013-02-15 14:40:31 +0100256 mesh_add_rsn_ie(sdata, skb) ||
257 mesh_add_meshid_ie(sdata, skb) ||
258 mesh_add_meshconf_ie(sdata, skb))
Thomas Pedersenf609a432012-08-03 12:21:35 -0700259 goto free;
Thomas Pedersen8db09852011-08-12 20:01:00 -0700260 } else { /* WLAN_SP_MESH_PEERING_CLOSE */
Thomas Pedersene7570df2012-08-03 12:21:34 -0700261 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Johannes Bergbf7cd942013-02-15 14:40:31 +0100262 if (mesh_add_meshid_ie(sdata, skb))
Thomas Pedersenf609a432012-08-03 12:21:35 -0700263 goto free;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100264 }
265
Thomas Pedersen8db09852011-08-12 20:01:00 -0700266 /* Add Mesh Peering Management element */
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100267 switch (action) {
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700268 case WLAN_SP_MESH_PEERING_OPEN:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100269 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700270 case WLAN_SP_MESH_PEERING_CONFIRM:
Thomas Pedersen8db09852011-08-12 20:01:00 -0700271 ie_len += 2;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100272 include_plid = true;
273 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700274 case WLAN_SP_MESH_PEERING_CLOSE:
Thomas Pedersen8db09852011-08-12 20:01:00 -0700275 if (plid) {
276 ie_len += 2;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100277 include_plid = true;
278 }
Thomas Pedersen8db09852011-08-12 20:01:00 -0700279 ie_len += 2; /* reason code */
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100280 break;
Thomas Pedersen8db09852011-08-12 20:01:00 -0700281 default:
Thomas Pedersenf609a432012-08-03 12:21:35 -0700282 err = -EINVAL;
283 goto free;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100284 }
285
Thomas Pedersen8db09852011-08-12 20:01:00 -0700286 if (WARN_ON(skb_tailroom(skb) < 2 + ie_len))
Thomas Pedersenf609a432012-08-03 12:21:35 -0700287 goto free;
Thomas Pedersen8db09852011-08-12 20:01:00 -0700288
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100289 pos = skb_put(skb, 2 + ie_len);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700290 *pos++ = WLAN_EID_PEER_MGMT;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100291 *pos++ = ie_len;
Thomas Pedersen8db09852011-08-12 20:01:00 -0700292 memcpy(pos, &peering_proto, 2);
293 pos += 2;
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +0800294 put_unaligned_le16(llid, pos);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700295 pos += 2;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100296 if (include_plid) {
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +0800297 put_unaligned_le16(plid, pos);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700298 pos += 2;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100299 }
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700300 if (action == WLAN_SP_MESH_PEERING_CLOSE) {
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +0800301 put_unaligned_le16(reason, pos);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700302 pos += 2;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100303 }
Thomas Pedersen176f3602011-10-26 14:47:27 -0700304
305 if (action != WLAN_SP_MESH_PEERING_CLOSE) {
Johannes Bergbf7cd942013-02-15 14:40:31 +0100306 if (mesh_add_ht_cap_ie(sdata, skb) ||
307 mesh_add_ht_oper_ie(sdata, skb))
Thomas Pedersenf609a432012-08-03 12:21:35 -0700308 goto free;
Thomas Pedersen176f3602011-10-26 14:47:27 -0700309 }
310
Johannes Bergbf7cd942013-02-15 14:40:31 +0100311 if (mesh_add_vendor_ies(sdata, skb))
Thomas Pedersenf609a432012-08-03 12:21:35 -0700312 goto free;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100313
Johannes Berg62ae67b2009-11-18 18:42:05 +0100314 ieee80211_tx_skb(sdata, skb);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100315 return 0;
Thomas Pedersenf609a432012-08-03 12:21:35 -0700316free:
317 kfree_skb(skb);
318 return err;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100319}
320
Bob Copelandfa87a652015-07-14 08:31:57 -0400321/**
322 * __mesh_plink_deactivate - deactivate mesh peer link
323 *
324 * @sta: mesh peer link to deactivate
325 *
326 * All mesh paths with this peer as next hop will be flushed
327 * Returns beacon changed flag if the beacon content changed.
328 *
329 * Locking: the caller must hold sta->mesh->plink_lock
330 */
331static u32 __mesh_plink_deactivate(struct sta_info *sta)
332{
333 struct ieee80211_sub_if_data *sdata = sta->sdata;
334 u32 changed = 0;
335
336 lockdep_assert_held(&sta->mesh->plink_lock);
337
338 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
339 changed = mesh_plink_dec_estab_count(sdata);
340 sta->mesh->plink_state = NL80211_PLINK_BLOCKED;
341 mesh_path_flush_by_nexthop(sta);
342
343 ieee80211_mps_sta_status_update(sta);
344 changed |= ieee80211_mps_set_sta_local_pm(sta,
345 NL80211_MESH_POWER_UNKNOWN);
346
347 return changed;
348}
349
350/**
351 * mesh_plink_deactivate - deactivate mesh peer link
352 *
353 * @sta: mesh peer link to deactivate
354 *
355 * All mesh paths with this peer as next hop will be flushed
356 */
357u32 mesh_plink_deactivate(struct sta_info *sta)
358{
359 struct ieee80211_sub_if_data *sdata = sta->sdata;
360 u32 changed;
361
362 spin_lock_bh(&sta->mesh->plink_lock);
363 changed = __mesh_plink_deactivate(sta);
364 sta->mesh->reason = WLAN_REASON_MESH_PEER_CANCELED;
365 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
366 sta->sta.addr, sta->mesh->llid, sta->mesh->plid,
367 sta->mesh->reason);
368 spin_unlock_bh(&sta->mesh->plink_lock);
369
370 return changed;
371}
372
Thomas Pedersen296fcba32013-01-23 12:18:12 -0800373static void mesh_sta_info_init(struct ieee80211_sub_if_data *sdata,
374 struct sta_info *sta,
375 struct ieee802_11_elems *elems, bool insert)
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100376{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200377 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +0200378 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700379 struct ieee80211_supported_band *sband;
Thomas Pedersenf68d7762013-01-23 12:18:13 -0800380 u32 rates, basic_rates = 0, changed = 0;
Janusz.Dziedzic@tieto.comabcff6e2015-03-20 06:37:01 +0100381 enum ieee80211_sta_rx_bandwidth bw = sta->sta.bandwidth;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100382
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700383 sband = local->hw.wiphy->bands[band];
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200384 rates = ieee80211_sta_get_rates(sdata, elems, band, &basic_rates);
Johannes Bergd0709a62008-02-25 16:27:46 +0100385
Johannes Berg433f5bc2015-06-17 10:31:00 +0200386 spin_lock_bh(&sta->mesh->plink_lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100387 sta->last_rx = jiffies;
Thomas Pedersen296fcba32013-01-23 12:18:12 -0800388
389 /* rates and capabilities don't change during peering */
Johannes Berg433f5bc2015-06-17 10:31:00 +0200390 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB &&
391 sta->mesh->processed_beacon)
Thomas Pedersen296fcba32013-01-23 12:18:12 -0800392 goto out;
Johannes Berg433f5bc2015-06-17 10:31:00 +0200393 sta->mesh->processed_beacon = true;
Chun-Yeow Yeohbae35d92012-07-24 11:52:35 +0800394
Thomas Pedersenf68d7762013-01-23 12:18:13 -0800395 if (sta->sta.supp_rates[band] != rates)
396 changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700397 sta->sta.supp_rates[band] = rates;
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700398
Thomas Pedersen52ac8c42013-02-16 10:41:04 -0800399 if (ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
400 elems->ht_cap_elem, sta))
401 changed |= IEEE80211_RC_BW_CHANGED;
Johannes Berg4bf88532012-11-09 11:39:59 +0100402
Janusz.Dziedzic@tieto.comabcff6e2015-03-20 06:37:01 +0100403 if (bw != sta->sta.bandwidth)
404 changed |= IEEE80211_RC_BW_CHANGED;
405
Thomas Pedersen52ac8c42013-02-16 10:41:04 -0800406 /* HT peer is operating 20MHz-only */
407 if (elems->ht_operation &&
408 !(elems->ht_operation->ht_param &
409 IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
410 if (sta->sta.bandwidth != IEEE80211_STA_RX_BW_20)
Thomas Pedersenf68d7762013-01-23 12:18:13 -0800411 changed |= IEEE80211_RC_BW_CHANGED;
Thomas Pedersen52ac8c42013-02-16 10:41:04 -0800412 sta->sta.bandwidth = IEEE80211_STA_RX_BW_20;
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700413 }
Thomas Pedersenc7d25822012-04-26 15:01:07 -0700414
Helmut Schaa59cf1d62012-11-27 18:03:13 +0100415 if (insert)
416 rate_control_rate_init(sta);
Thomas Pedersenf68d7762013-01-23 12:18:13 -0800417 else
418 rate_control_rate_update(local, sband, sta, changed);
Thomas Pedersen296fcba32013-01-23 12:18:12 -0800419out:
Johannes Berg433f5bc2015-06-17 10:31:00 +0200420 spin_unlock_bh(&sta->mesh->plink_lock);
Thomas Pedersen296fcba32013-01-23 12:18:12 -0800421}
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700422
Thomas Pedersen296fcba32013-01-23 12:18:12 -0800423static struct sta_info *
424__mesh_sta_info_alloc(struct ieee80211_sub_if_data *sdata, u8 *hw_addr)
425{
426 struct sta_info *sta;
427
428 if (sdata->local->num_sta >= MESH_MAX_PLINKS)
Thomas Pedersene87278e2012-04-26 15:01:06 -0700429 return NULL;
430
Thomas Pedersen296fcba32013-01-23 12:18:12 -0800431 sta = sta_info_alloc(sdata, hw_addr, GFP_KERNEL);
432 if (!sta)
433 return NULL;
434
Johannes Berg433f5bc2015-06-17 10:31:00 +0200435 sta->mesh->plink_state = NL80211_PLINK_LISTEN;
Johannes Berga74a8c82014-07-22 14:50:47 +0200436 sta->sta.wme = true;
Thomas Pedersen296fcba32013-01-23 12:18:12 -0800437
438 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
439 sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
440 sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
441
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700442 return sta;
443}
444
Thomas Pedersen296fcba32013-01-23 12:18:12 -0800445static struct sta_info *
446mesh_sta_info_alloc(struct ieee80211_sub_if_data *sdata, u8 *addr,
447 struct ieee802_11_elems *elems)
448{
449 struct sta_info *sta = NULL;
450
Thomas Pedersena6dad6a2013-03-04 13:06:12 -0800451 /* Userspace handles station allocation */
452 if (sdata->u.mesh.user_mpm ||
453 sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED)
Thomas Pedersen296fcba32013-01-23 12:18:12 -0800454 cfg80211_notify_new_peer_candidate(sdata->dev, addr,
455 elems->ie_start,
456 elems->total_len,
457 GFP_KERNEL);
458 else
459 sta = __mesh_sta_info_alloc(sdata, addr);
460
461 return sta;
462}
463
464/*
465 * mesh_sta_info_get - return mesh sta info entry for @addr.
466 *
467 * @sdata: local meshif
468 * @addr: peer's address
469 * @elems: IEs from beacon or mesh peering frame.
470 *
471 * Return existing or newly allocated sta_info under RCU read lock.
472 * (re)initialize with given IEs.
473 */
474static struct sta_info *
475mesh_sta_info_get(struct ieee80211_sub_if_data *sdata,
476 u8 *addr, struct ieee802_11_elems *elems) __acquires(RCU)
477{
478 struct sta_info *sta = NULL;
479
480 rcu_read_lock();
481 sta = sta_info_get(sdata, addr);
482 if (sta) {
483 mesh_sta_info_init(sdata, sta, elems, false);
484 } else {
485 rcu_read_unlock();
486 /* can't run atomic */
487 sta = mesh_sta_info_alloc(sdata, addr, elems);
488 if (!sta) {
489 rcu_read_lock();
490 return NULL;
491 }
492
Thomas Pedersen3b4797b2013-01-26 17:00:02 -0800493 mesh_sta_info_init(sdata, sta, elems, true);
494
Thomas Pedersen296fcba32013-01-23 12:18:12 -0800495 if (sta_info_insert_rcu(sta))
496 return NULL;
497 }
498
499 return sta;
500}
501
502/*
503 * mesh_neighbour_update - update or initialize new mesh neighbor.
504 *
505 * @sdata: local meshif
506 * @addr: peer's address
507 * @elems: IEs from beacon or mesh peering frame
508 *
509 * Initiates peering if appropriate.
510 */
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700511void mesh_neighbour_update(struct ieee80211_sub_if_data *sdata,
512 u8 *hw_addr,
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700513 struct ieee802_11_elems *elems)
514{
515 struct sta_info *sta;
Thomas Pedersen39886b62013-02-13 12:14:19 -0800516 u32 changed = 0;
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700517
Thomas Pedersen296fcba32013-01-23 12:18:12 -0800518 sta = mesh_sta_info_get(sdata, hw_addr, elems);
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700519 if (!sta)
520 goto out;
521
Javier Cardona1570ca52011-04-07 15:08:35 -0700522 if (mesh_peer_accepts_plinks(elems) &&
Johannes Berg433f5bc2015-06-17 10:31:00 +0200523 sta->mesh->plink_state == NL80211_PLINK_LISTEN &&
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700524 sdata->u.mesh.accepting_plinks &&
525 sdata->u.mesh.mshcfg.auto_open_plinks &&
Bob Copeland36c9bb22013-11-05 11:16:54 -0800526 rssi_threshold_check(sdata, sta))
Thomas Pedersen39886b62013-02-13 12:14:19 -0800527 changed = mesh_plink_open(sta);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100528
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100529 ieee80211_mps_frame_release(sta, elems);
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700530out:
Johannes Bergd0709a62008-02-25 16:27:46 +0100531 rcu_read_unlock();
Thomas Pedersen2b5e1962013-02-14 11:20:13 -0800532 ieee80211_mbss_info_change_notify(sdata, changed);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100533}
534
535static void mesh_plink_timer(unsigned long data)
536{
537 struct sta_info *sta;
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +0800538 u16 reason = 0;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100539 struct ieee80211_sub_if_data *sdata;
Marco Porsch453e66f22012-11-21 18:40:32 -0800540 struct mesh_config *mshcfg;
Thomas Pedersen4efec45132013-11-05 11:16:50 -0800541 enum ieee80211_self_protected_actioncode action = 0;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100542
Johannes Bergd0709a62008-02-25 16:27:46 +0100543 /*
544 * This STA is valid because sta_info_destroy() will
545 * del_timer_sync() this timer after having made sure
546 * it cannot be readded (by deleting the plink.)
547 */
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100548 sta = (struct sta_info *) data;
549
Stanislaw Gruszka690205f2013-02-28 10:55:29 +0100550 if (sta->sdata->local->quiescing)
Johannes Berg5bb644a2009-05-17 11:40:42 +0200551 return;
Johannes Berg5bb644a2009-05-17 11:40:42 +0200552
Johannes Berg433f5bc2015-06-17 10:31:00 +0200553 spin_lock_bh(&sta->mesh->plink_lock);
Bob Copeland2b470c32014-06-04 09:27:31 -0400554
555 /* If a timer fires just before a state transition on another CPU,
556 * we may have already extended the timeout and changed state by the
557 * time we've acquired the lock and arrived here. In that case,
558 * skip this timer and wait for the new one.
559 */
Johannes Berg433f5bc2015-06-17 10:31:00 +0200560 if (time_before(jiffies, sta->mesh->plink_timer.expires)) {
Bob Copeland2b470c32014-06-04 09:27:31 -0400561 mpl_dbg(sta->sdata,
562 "Ignoring timer for %pM in state %s (timer adjusted)",
Johannes Berg433f5bc2015-06-17 10:31:00 +0200563 sta->sta.addr, mplstates[sta->mesh->plink_state]);
564 spin_unlock_bh(&sta->mesh->plink_lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100565 return;
566 }
Bob Copeland2b470c32014-06-04 09:27:31 -0400567
568 /* del_timer() and handler may race when entering these states */
Johannes Berg433f5bc2015-06-17 10:31:00 +0200569 if (sta->mesh->plink_state == NL80211_PLINK_LISTEN ||
570 sta->mesh->plink_state == NL80211_PLINK_ESTAB) {
Bob Copeland2b470c32014-06-04 09:27:31 -0400571 mpl_dbg(sta->sdata,
572 "Ignoring timer for %pM in state %s (timer deleted)",
Johannes Berg433f5bc2015-06-17 10:31:00 +0200573 sta->sta.addr, mplstates[sta->mesh->plink_state]);
574 spin_unlock_bh(&sta->mesh->plink_lock);
Bob Copeland2b470c32014-06-04 09:27:31 -0400575 return;
576 }
577
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200578 mpl_dbg(sta->sdata,
Thomas Pedersen3088f7d2013-04-08 11:06:16 -0700579 "Mesh plink timer for %pM fired on state %s\n",
Johannes Berg433f5bc2015-06-17 10:31:00 +0200580 sta->sta.addr, mplstates[sta->mesh->plink_state]);
Johannes Bergd0709a62008-02-25 16:27:46 +0100581 sdata = sta->sdata;
Marco Porsch453e66f22012-11-21 18:40:32 -0800582 mshcfg = &sdata->u.mesh.mshcfg;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100583
Johannes Berg433f5bc2015-06-17 10:31:00 +0200584 switch (sta->mesh->plink_state) {
Javier Cardona57cf8042011-05-13 10:45:43 -0700585 case NL80211_PLINK_OPN_RCVD:
586 case NL80211_PLINK_OPN_SNT:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100587 /* retry timer */
Johannes Berg433f5bc2015-06-17 10:31:00 +0200588 if (sta->mesh->plink_retries < mshcfg->dot11MeshMaxRetries) {
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100589 u32 rand;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200590 mpl_dbg(sta->sdata,
591 "Mesh plink for %pM (retry, timeout): %d %d\n",
Johannes Berg433f5bc2015-06-17 10:31:00 +0200592 sta->sta.addr, sta->mesh->plink_retries,
593 sta->mesh->plink_timeout);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100594 get_random_bytes(&rand, sizeof(u32));
Johannes Berg433f5bc2015-06-17 10:31:00 +0200595 sta->mesh->plink_timeout = sta->mesh->plink_timeout +
596 rand % sta->mesh->plink_timeout;
597 ++sta->mesh->plink_retries;
598 mod_plink_timer(sta, sta->mesh->plink_timeout);
Thomas Pedersen4efec45132013-11-05 11:16:50 -0800599 action = WLAN_SP_MESH_PEERING_OPEN;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100600 break;
601 }
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +0800602 reason = WLAN_REASON_MESH_MAX_RETRIES;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100603 /* fall through on else */
Javier Cardona57cf8042011-05-13 10:45:43 -0700604 case NL80211_PLINK_CNF_RCVD:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100605 /* confirm timer */
606 if (!reason)
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +0800607 reason = WLAN_REASON_MESH_CONFIRM_TIMEOUT;
Johannes Berg433f5bc2015-06-17 10:31:00 +0200608 sta->mesh->plink_state = NL80211_PLINK_HOLDING;
Marco Porsch453e66f22012-11-21 18:40:32 -0800609 mod_plink_timer(sta, mshcfg->dot11MeshHoldingTimeout);
Thomas Pedersen4efec45132013-11-05 11:16:50 -0800610 action = WLAN_SP_MESH_PEERING_CLOSE;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100611 break;
Javier Cardona57cf8042011-05-13 10:45:43 -0700612 case NL80211_PLINK_HOLDING:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100613 /* holding timer */
Johannes Berg433f5bc2015-06-17 10:31:00 +0200614 del_timer(&sta->mesh->plink_timer);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100615 mesh_plink_fsm_restart(sta);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100616 break;
617 default:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100618 break;
619 }
Johannes Berg433f5bc2015-06-17 10:31:00 +0200620 spin_unlock_bh(&sta->mesh->plink_lock);
Thomas Pedersen4efec45132013-11-05 11:16:50 -0800621 if (action)
622 mesh_plink_frame_tx(sdata, action, sta->sta.addr,
Johannes Berg433f5bc2015-06-17 10:31:00 +0200623 sta->mesh->llid, sta->mesh->plid, reason);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100624}
625
Nicholas Mc Guire0df2f6c2015-03-02 04:54:14 -0500626static inline void mesh_plink_timer_set(struct sta_info *sta, u32 timeout)
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100627{
Johannes Berg433f5bc2015-06-17 10:31:00 +0200628 sta->mesh->plink_timer.expires = jiffies + msecs_to_jiffies(timeout);
629 sta->mesh->plink_timer.data = (unsigned long) sta;
630 sta->mesh->plink_timer.function = mesh_plink_timer;
631 sta->mesh->plink_timeout = timeout;
632 add_timer(&sta->mesh->plink_timer);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100633}
634
Thomas Pedersen204d1302013-11-05 11:17:05 -0800635static bool llid_in_use(struct ieee80211_sub_if_data *sdata,
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +0800636 u16 llid)
Thomas Pedersen204d1302013-11-05 11:17:05 -0800637{
638 struct ieee80211_local *local = sdata->local;
639 bool in_use = false;
640 struct sta_info *sta;
641
642 rcu_read_lock();
643 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg433f5bc2015-06-17 10:31:00 +0200644 if (!memcmp(&sta->mesh->llid, &llid, sizeof(llid))) {
Thomas Pedersen204d1302013-11-05 11:17:05 -0800645 in_use = true;
646 break;
647 }
648 }
649 rcu_read_unlock();
650
651 return in_use;
652}
653
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +0800654static u16 mesh_get_new_llid(struct ieee80211_sub_if_data *sdata)
Thomas Pedersen204d1302013-11-05 11:17:05 -0800655{
656 u16 llid;
657
658 do {
659 get_random_bytes(&llid, sizeof(llid));
660 /* for mesh PS we still only have the AID range for TIM bits */
661 llid = (llid % IEEE80211_MAX_AID) + 1;
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +0800662 } while (llid_in_use(sdata, llid));
Thomas Pedersen204d1302013-11-05 11:17:05 -0800663
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +0800664 return llid;
Thomas Pedersen204d1302013-11-05 11:17:05 -0800665}
666
Thomas Pedersen39886b62013-02-13 12:14:19 -0800667u32 mesh_plink_open(struct sta_info *sta)
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100668{
Johannes Bergd0709a62008-02-25 16:27:46 +0100669 struct ieee80211_sub_if_data *sdata = sta->sdata;
Thomas Pedersen39886b62013-02-13 12:14:19 -0800670 u32 changed;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100671
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200672 if (!test_sta_flag(sta, WLAN_STA_AUTH))
Thomas Pedersen39886b62013-02-13 12:14:19 -0800673 return 0;
Javier Cardona53e80512011-04-07 15:08:32 -0700674
Johannes Berg433f5bc2015-06-17 10:31:00 +0200675 spin_lock_bh(&sta->mesh->plink_lock);
676 sta->mesh->llid = mesh_get_new_llid(sdata);
677 if (sta->mesh->plink_state != NL80211_PLINK_LISTEN &&
678 sta->mesh->plink_state != NL80211_PLINK_BLOCKED) {
679 spin_unlock_bh(&sta->mesh->plink_lock);
Thomas Pedersen39886b62013-02-13 12:14:19 -0800680 return 0;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100681 }
Johannes Berg433f5bc2015-06-17 10:31:00 +0200682 sta->mesh->plink_state = NL80211_PLINK_OPN_SNT;
Marco Porsch453e66f22012-11-21 18:40:32 -0800683 mesh_plink_timer_set(sta, sdata->u.mesh.mshcfg.dot11MeshRetryTimeout);
Johannes Berg433f5bc2015-06-17 10:31:00 +0200684 spin_unlock_bh(&sta->mesh->plink_lock);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200685 mpl_dbg(sdata,
686 "Mesh plink: starting establishment with %pM\n",
Johannes Berg0c68ae262008-10-27 15:56:10 -0700687 sta->sta.addr);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100688
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100689 /* set the non-peer mode to active during peering */
Thomas Pedersen39886b62013-02-13 12:14:19 -0800690 changed = ieee80211_mps_local_status_update(sdata);
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100691
Thomas Pedersen39886b62013-02-13 12:14:19 -0800692 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
Johannes Berg433f5bc2015-06-17 10:31:00 +0200693 sta->sta.addr, sta->mesh->llid, 0, 0);
Thomas Pedersen39886b62013-02-13 12:14:19 -0800694 return changed;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100695}
696
Thomas Pedersen39886b62013-02-13 12:14:19 -0800697u32 mesh_plink_block(struct sta_info *sta)
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100698{
Marco Porschdf323812012-08-08 07:58:43 +0200699 u32 changed;
John W. Linvillec9370192010-06-21 17:14:07 -0400700
Johannes Berg433f5bc2015-06-17 10:31:00 +0200701 spin_lock_bh(&sta->mesh->plink_lock);
Marco Porschdf323812012-08-08 07:58:43 +0200702 changed = __mesh_plink_deactivate(sta);
Johannes Berg433f5bc2015-06-17 10:31:00 +0200703 sta->mesh->plink_state = NL80211_PLINK_BLOCKED;
704 spin_unlock_bh(&sta->mesh->plink_lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400705
Thomas Pedersen39886b62013-02-13 12:14:19 -0800706 return changed;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100707}
708
Bob Copelande76d67f2013-11-05 11:16:52 -0800709static void mesh_plink_close(struct ieee80211_sub_if_data *sdata,
710 struct sta_info *sta,
711 enum plink_event event)
712{
713 struct mesh_config *mshcfg = &sdata->u.mesh.mshcfg;
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +0800714 u16 reason = (event == CLS_ACPT) ?
715 WLAN_REASON_MESH_CLOSE : WLAN_REASON_MESH_CONFIG;
Bob Copelande76d67f2013-11-05 11:16:52 -0800716
Johannes Berg433f5bc2015-06-17 10:31:00 +0200717 sta->mesh->reason = reason;
718 sta->mesh->plink_state = NL80211_PLINK_HOLDING;
Bob Copeland272a9e22013-11-05 11:16:56 -0800719 mod_plink_timer(sta, mshcfg->dot11MeshHoldingTimeout);
Bob Copelande76d67f2013-11-05 11:16:52 -0800720}
721
722static u32 mesh_plink_establish(struct ieee80211_sub_if_data *sdata,
723 struct sta_info *sta)
724{
725 struct mesh_config *mshcfg = &sdata->u.mesh.mshcfg;
726 u32 changed = 0;
727
Johannes Berg433f5bc2015-06-17 10:31:00 +0200728 del_timer(&sta->mesh->plink_timer);
729 sta->mesh->plink_state = NL80211_PLINK_ESTAB;
Bob Copelande76d67f2013-11-05 11:16:52 -0800730 changed |= mesh_plink_inc_estab_count(sdata);
731 changed |= mesh_set_ht_prot_mode(sdata);
732 changed |= mesh_set_short_slot_time(sdata);
733 mpl_dbg(sdata, "Mesh plink with %pM ESTABLISHED\n", sta->sta.addr);
734 ieee80211_mps_sta_status_update(sta);
735 changed |= ieee80211_mps_set_sta_local_pm(sta, mshcfg->power_mode);
736 return changed;
737}
738
Thomas Pedersenc7e67812013-11-05 11:17:02 -0800739/**
740 * mesh_plink_fsm - step @sta MPM based on @event
741 *
742 * @sdata: interface
743 * @sta: mesh neighbor
744 * @event: peering event
745 *
746 * Return: changed MBSS flags
747 */
748static u32 mesh_plink_fsm(struct ieee80211_sub_if_data *sdata,
749 struct sta_info *sta, enum plink_event event)
750{
751 struct mesh_config *mshcfg = &sdata->u.mesh.mshcfg;
752 enum ieee80211_self_protected_actioncode action = 0;
753 u32 changed = 0;
754
755 mpl_dbg(sdata, "peer %pM in state %s got event %s\n", sta->sta.addr,
Johannes Berg433f5bc2015-06-17 10:31:00 +0200756 mplstates[sta->mesh->plink_state], mplevents[event]);
Thomas Pedersenc7e67812013-11-05 11:17:02 -0800757
Johannes Berg433f5bc2015-06-17 10:31:00 +0200758 spin_lock_bh(&sta->mesh->plink_lock);
759 switch (sta->mesh->plink_state) {
Thomas Pedersenc7e67812013-11-05 11:17:02 -0800760 case NL80211_PLINK_LISTEN:
761 switch (event) {
762 case CLS_ACPT:
763 mesh_plink_fsm_restart(sta);
764 break;
765 case OPN_ACPT:
Johannes Berg433f5bc2015-06-17 10:31:00 +0200766 sta->mesh->plink_state = NL80211_PLINK_OPN_RCVD;
767 sta->mesh->llid = mesh_get_new_llid(sdata);
Thomas Pedersenc7e67812013-11-05 11:17:02 -0800768 mesh_plink_timer_set(sta,
769 mshcfg->dot11MeshRetryTimeout);
770
771 /* set the non-peer mode to active during peering */
772 changed |= ieee80211_mps_local_status_update(sdata);
773 action = WLAN_SP_MESH_PEERING_OPEN;
774 break;
775 default:
776 break;
777 }
778 break;
779 case NL80211_PLINK_OPN_SNT:
780 switch (event) {
781 case OPN_RJCT:
782 case CNF_RJCT:
783 case CLS_ACPT:
784 mesh_plink_close(sdata, sta, event);
785 action = WLAN_SP_MESH_PEERING_CLOSE;
786 break;
787 case OPN_ACPT:
788 /* retry timer is left untouched */
Johannes Berg433f5bc2015-06-17 10:31:00 +0200789 sta->mesh->plink_state = NL80211_PLINK_OPN_RCVD;
Thomas Pedersenc7e67812013-11-05 11:17:02 -0800790 action = WLAN_SP_MESH_PEERING_CONFIRM;
791 break;
792 case CNF_ACPT:
Johannes Berg433f5bc2015-06-17 10:31:00 +0200793 sta->mesh->plink_state = NL80211_PLINK_CNF_RCVD;
Bob Copeland2b470c32014-06-04 09:27:31 -0400794 mod_plink_timer(sta, mshcfg->dot11MeshConfirmTimeout);
Thomas Pedersenc7e67812013-11-05 11:17:02 -0800795 break;
796 default:
797 break;
798 }
799 break;
800 case NL80211_PLINK_OPN_RCVD:
801 switch (event) {
802 case OPN_RJCT:
803 case CNF_RJCT:
804 case CLS_ACPT:
805 mesh_plink_close(sdata, sta, event);
806 action = WLAN_SP_MESH_PEERING_CLOSE;
807 break;
808 case OPN_ACPT:
809 action = WLAN_SP_MESH_PEERING_CONFIRM;
810 break;
811 case CNF_ACPT:
812 changed |= mesh_plink_establish(sdata, sta);
813 break;
814 default:
815 break;
816 }
817 break;
818 case NL80211_PLINK_CNF_RCVD:
819 switch (event) {
820 case OPN_RJCT:
821 case CNF_RJCT:
822 case CLS_ACPT:
823 mesh_plink_close(sdata, sta, event);
824 action = WLAN_SP_MESH_PEERING_CLOSE;
825 break;
826 case OPN_ACPT:
827 changed |= mesh_plink_establish(sdata, sta);
828 action = WLAN_SP_MESH_PEERING_CONFIRM;
829 break;
830 default:
831 break;
832 }
833 break;
834 case NL80211_PLINK_ESTAB:
835 switch (event) {
836 case CLS_ACPT:
837 changed |= __mesh_plink_deactivate(sta);
838 changed |= mesh_set_ht_prot_mode(sdata);
839 changed |= mesh_set_short_slot_time(sdata);
840 mesh_plink_close(sdata, sta, event);
841 action = WLAN_SP_MESH_PEERING_CLOSE;
842 break;
843 case OPN_ACPT:
844 action = WLAN_SP_MESH_PEERING_CONFIRM;
845 break;
846 default:
847 break;
848 }
849 break;
850 case NL80211_PLINK_HOLDING:
851 switch (event) {
852 case CLS_ACPT:
Johannes Berg433f5bc2015-06-17 10:31:00 +0200853 del_timer(&sta->mesh->plink_timer);
Thomas Pedersenc7e67812013-11-05 11:17:02 -0800854 mesh_plink_fsm_restart(sta);
855 break;
856 case OPN_ACPT:
857 case CNF_ACPT:
858 case OPN_RJCT:
859 case CNF_RJCT:
860 action = WLAN_SP_MESH_PEERING_CLOSE;
861 break;
862 default:
863 break;
864 }
865 break;
866 default:
867 /* should not get here, PLINK_BLOCKED is dealt with at the
868 * beginning of the function
869 */
870 break;
871 }
Johannes Berg433f5bc2015-06-17 10:31:00 +0200872 spin_unlock_bh(&sta->mesh->plink_lock);
Thomas Pedersenc7e67812013-11-05 11:17:02 -0800873 if (action) {
874 mesh_plink_frame_tx(sdata, action, sta->sta.addr,
Johannes Berg433f5bc2015-06-17 10:31:00 +0200875 sta->mesh->llid, sta->mesh->plid,
876 sta->mesh->reason);
Thomas Pedersenc7e67812013-11-05 11:17:02 -0800877
878 /* also send confirm in open case */
879 if (action == WLAN_SP_MESH_PEERING_OPEN) {
880 mesh_plink_frame_tx(sdata,
881 WLAN_SP_MESH_PEERING_CONFIRM,
Johannes Berg433f5bc2015-06-17 10:31:00 +0200882 sta->sta.addr, sta->mesh->llid,
883 sta->mesh->plid, 0);
Thomas Pedersenc7e67812013-11-05 11:17:02 -0800884 }
885 }
886
887 return changed;
888}
889
Thomas Pedersenc99a89e2013-11-05 11:17:03 -0800890/*
891 * mesh_plink_get_event - get correct MPM event
892 *
893 * @sdata: interface
894 * @sta: peer, leave NULL if processing a frame from a new suitable peer
895 * @elems: peering management IEs
896 * @ftype: frame type
897 * @llid: peer's peer link ID
898 * @plid: peer's local link ID
899 *
900 * Return: new peering event for @sta, but PLINK_UNDEFINED should be treated as
901 * an error.
902 */
903static enum plink_event
904mesh_plink_get_event(struct ieee80211_sub_if_data *sdata,
905 struct sta_info *sta,
906 struct ieee802_11_elems *elems,
907 enum ieee80211_self_protected_actioncode ftype,
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +0800908 u16 llid, u16 plid)
Thomas Pedersenc99a89e2013-11-05 11:17:03 -0800909{
910 enum plink_event event = PLINK_UNDEFINED;
911 u8 ie_len = elems->peering_len;
912 bool matches_local;
913
914 matches_local = (ftype == WLAN_SP_MESH_PEERING_CLOSE ||
915 mesh_matches_local(sdata, elems));
916
917 /* deny open request from non-matching peer */
918 if (!matches_local && !sta) {
919 event = OPN_RJCT;
920 goto out;
921 }
922
923 if (!sta) {
924 if (ftype != WLAN_SP_MESH_PEERING_OPEN) {
925 mpl_dbg(sdata, "Mesh plink: cls or cnf from unknown peer\n");
926 goto out;
927 }
928 /* ftype == WLAN_SP_MESH_PEERING_OPEN */
929 if (!mesh_plink_free_count(sdata)) {
930 mpl_dbg(sdata, "Mesh plink error: no more free plinks\n");
931 goto out;
932 }
933 } else {
934 if (!test_sta_flag(sta, WLAN_STA_AUTH)) {
935 mpl_dbg(sdata, "Mesh plink: Action frame from non-authed peer\n");
936 goto out;
937 }
Johannes Berg433f5bc2015-06-17 10:31:00 +0200938 if (sta->mesh->plink_state == NL80211_PLINK_BLOCKED)
Thomas Pedersenc99a89e2013-11-05 11:17:03 -0800939 goto out;
940 }
941
942 /* new matching peer */
943 if (!sta) {
944 event = OPN_ACPT;
945 goto out;
946 }
947
948 switch (ftype) {
949 case WLAN_SP_MESH_PEERING_OPEN:
950 if (!matches_local)
951 event = OPN_RJCT;
952 if (!mesh_plink_free_count(sdata) ||
Johannes Berg433f5bc2015-06-17 10:31:00 +0200953 (sta->mesh->plid && sta->mesh->plid != plid))
Thomas Pedersenc99a89e2013-11-05 11:17:03 -0800954 event = OPN_IGNR;
955 else
956 event = OPN_ACPT;
957 break;
958 case WLAN_SP_MESH_PEERING_CONFIRM:
959 if (!matches_local)
960 event = CNF_RJCT;
961 if (!mesh_plink_free_count(sdata) ||
Johannes Berg433f5bc2015-06-17 10:31:00 +0200962 sta->mesh->llid != llid ||
963 (sta->mesh->plid && sta->mesh->plid != plid))
Thomas Pedersenc99a89e2013-11-05 11:17:03 -0800964 event = CNF_IGNR;
965 else
966 event = CNF_ACPT;
967 break;
968 case WLAN_SP_MESH_PEERING_CLOSE:
Johannes Berg433f5bc2015-06-17 10:31:00 +0200969 if (sta->mesh->plink_state == NL80211_PLINK_ESTAB)
Thomas Pedersenc99a89e2013-11-05 11:17:03 -0800970 /* Do not check for llid or plid. This does not
971 * follow the standard but since multiple plinks
972 * per sta are not supported, it is necessary in
973 * order to avoid a livelock when MP A sees an
974 * establish peer link to MP B but MP B does not
975 * see it. This can be caused by a timeout in
976 * B's peer link establishment or B beign
977 * restarted.
978 */
979 event = CLS_ACPT;
Johannes Berg433f5bc2015-06-17 10:31:00 +0200980 else if (sta->mesh->plid != plid)
Thomas Pedersenc99a89e2013-11-05 11:17:03 -0800981 event = CLS_IGNR;
Johannes Berg433f5bc2015-06-17 10:31:00 +0200982 else if (ie_len == 8 && sta->mesh->llid != llid)
Thomas Pedersenc99a89e2013-11-05 11:17:03 -0800983 event = CLS_IGNR;
984 else
985 event = CLS_ACPT;
986 break;
987 default:
988 mpl_dbg(sdata, "Mesh plink: unknown frame subtype\n");
989 break;
990 }
991
992out:
993 return event;
994}
995
Thomas Pedersen05a23ae2013-11-05 11:16:59 -0800996static void
997mesh_process_plink_frame(struct ieee80211_sub_if_data *sdata,
998 struct ieee80211_mgmt *mgmt,
999 struct ieee802_11_elems *elems)
Luis Carlos Coboc3896d22008-02-23 15:17:13 +01001000{
Thomas Pedersen05a23ae2013-11-05 11:16:59 -08001001
Luis Carlos Coboc3896d22008-02-23 15:17:13 +01001002 struct sta_info *sta;
1003 enum plink_event event;
Thomas Pedersen54ef6562011-08-11 19:35:12 -07001004 enum ieee80211_self_protected_actioncode ftype;
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -07001005 u32 changed = 0;
Thomas Pedersenc99a89e2013-11-05 11:17:03 -08001006 u8 ie_len = elems->peering_len;
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +08001007 u16 plid, llid = 0;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +01001008
Thomas Pedersen05a23ae2013-11-05 11:16:59 -08001009 if (!elems->peering) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001010 mpl_dbg(sdata,
1011 "Mesh plink: missing necessary peer link ie\n");
Luis Carlos Coboc3896d22008-02-23 15:17:13 +01001012 return;
1013 }
Johannes Bergbf7cd942013-02-15 14:40:31 +01001014
Thomas Pedersen05a23ae2013-11-05 11:16:59 -08001015 if (elems->rsn_len &&
Johannes Bergbf7cd942013-02-15 14:40:31 +01001016 sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001017 mpl_dbg(sdata,
1018 "Mesh plink: can't establish link with secure peer\n");
Javier Cardona5cff5e02011-04-07 15:08:29 -07001019 return;
1020 }
Luis Carlos Coboc3896d22008-02-23 15:17:13 +01001021
Thomas Pedersen8db09852011-08-12 20:01:00 -07001022 ftype = mgmt->u.action.u.self_prot.action_code;
Thomas Pedersen8db09852011-08-12 20:01:00 -07001023 if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
1024 (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
1025 (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
1026 && ie_len != 8)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001027 mpl_dbg(sdata,
1028 "Mesh plink: incorrect plink ie length %d %d\n",
1029 ftype, ie_len);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +01001030 return;
1031 }
1032
Thomas Pedersen54ef6562011-08-11 19:35:12 -07001033 if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
Thomas Pedersen05a23ae2013-11-05 11:16:59 -08001034 (!elems->mesh_id || !elems->mesh_config)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001035 mpl_dbg(sdata, "Mesh plink: missing necessary ie\n");
Luis Carlos Coboc3896d22008-02-23 15:17:13 +01001036 return;
1037 }
1038 /* Note the lines below are correct, the llid in the frame is the plid
1039 * from the point of view of this host.
1040 */
Bob Copelandf8134fe2014-06-28 16:35:26 -04001041 plid = get_unaligned_le16(PLINK_GET_LLID(elems->peering));
Thomas Pedersen54ef6562011-08-11 19:35:12 -07001042 if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
Bob Copelandf8134fe2014-06-28 16:35:26 -04001043 (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
1044 llid = get_unaligned_le16(PLINK_GET_PLID(elems->peering));
Luis Carlos Coboc3896d22008-02-23 15:17:13 +01001045
Thomas Pedersen296fcba32013-01-23 12:18:12 -08001046 /* WARNING: Only for sta pointer, is dropped & re-acquired */
Johannes Bergd0709a62008-02-25 16:27:46 +01001047 rcu_read_lock();
1048
Johannes Bergabe60632009-11-25 17:46:18 +01001049 sta = sta_info_get(sdata, mgmt->sa);
Bob Copeland32cb05b2013-11-05 11:16:53 -08001050
Ashok Nagarajan55335132012-02-28 17:04:08 -08001051 if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
Bob Copeland36c9bb22013-11-05 11:16:54 -08001052 !rssi_threshold_check(sdata, sta)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001053 mpl_dbg(sdata, "Mesh plink: %pM does not meet rssi threshold\n",
Ashok Nagarajan3d4f96992012-03-06 12:48:30 -08001054 mgmt->sa);
Thomas Pedersenfc103022013-11-05 11:17:00 -08001055 goto unlock_rcu;
Ashok Nagarajan55335132012-02-28 17:04:08 -08001056 }
1057
Luis Carlos Coboc3896d22008-02-23 15:17:13 +01001058 /* Now we will figure out the appropriate event... */
Thomas Pedersenc99a89e2013-11-05 11:17:03 -08001059 event = mesh_plink_get_event(sdata, sta, elems, ftype, llid, plid);
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -07001060
1061 if (event == OPN_ACPT) {
Thomas Pedersen296fcba32013-01-23 12:18:12 -08001062 rcu_read_unlock();
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -07001063 /* allocate sta entry if necessary and update info */
Thomas Pedersen05a23ae2013-11-05 11:16:59 -08001064 sta = mesh_sta_info_get(sdata, mgmt->sa, elems);
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -07001065 if (!sta) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001066 mpl_dbg(sdata, "Mesh plink: failed to init peer!\n");
Thomas Pedersenfc103022013-11-05 11:17:00 -08001067 goto unlock_rcu;
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -07001068 }
Johannes Berg433f5bc2015-06-17 10:31:00 +02001069 sta->mesh->plid = plid;
Thomas Pedersenc99a89e2013-11-05 11:17:03 -08001070 } else if (!sta && event == OPN_RJCT) {
1071 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
1072 mgmt->sa, 0, plid,
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +08001073 WLAN_REASON_MESH_CONFIG);
Thomas Pedersenc99a89e2013-11-05 11:17:03 -08001074 goto unlock_rcu;
1075 } else if (!sta || event == PLINK_UNDEFINED) {
1076 /* something went wrong */
1077 goto unlock_rcu;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +01001078 }
1079
Bob Copeland6c6fa492014-06-28 16:35:25 -04001080 /* 802.11-2012 13.3.7.2 - update plid on CNF if not set */
Johannes Berg433f5bc2015-06-17 10:31:00 +02001081 if (!sta->mesh->plid && event == CNF_ACPT)
1082 sta->mesh->plid = plid;
Bob Copeland6c6fa492014-06-28 16:35:25 -04001083
Thomas Pedersenc7e67812013-11-05 11:17:02 -08001084 changed |= mesh_plink_fsm(sdata, sta, event);
Johannes Bergd0709a62008-02-25 16:27:46 +01001085
Thomas Pedersenfc103022013-11-05 11:17:00 -08001086unlock_rcu:
Johannes Bergd0709a62008-02-25 16:27:46 +01001087 rcu_read_unlock();
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -07001088
Thomas Pedersenecccd072013-06-10 13:17:21 -07001089 if (changed)
Thomas Pedersen2b5e1962013-02-14 11:20:13 -08001090 ieee80211_mbss_info_change_notify(sdata, changed);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +01001091}
Thomas Pedersen05a23ae2013-11-05 11:16:59 -08001092
1093void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata,
1094 struct ieee80211_mgmt *mgmt, size_t len,
1095 struct ieee80211_rx_status *rx_status)
1096{
1097 struct ieee802_11_elems elems;
1098 size_t baselen;
1099 u8 *baseaddr;
1100
1101 /* need action_code, aux */
1102 if (len < IEEE80211_MIN_ACTION_SIZE + 3)
1103 return;
1104
1105 if (sdata->u.mesh.user_mpm)
1106 /* userspace must register for these */
1107 return;
1108
1109 if (is_multicast_ether_addr(mgmt->da)) {
1110 mpl_dbg(sdata,
1111 "Mesh plink: ignore frame from multicast address\n");
1112 return;
1113 }
1114
1115 baseaddr = mgmt->u.action.u.self_prot.variable;
1116 baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
1117 if (mgmt->u.action.u.self_prot.action_code ==
1118 WLAN_SP_MESH_PEERING_CONFIRM) {
1119 baseaddr += 4;
1120 baselen += 4;
Bob Copelandb3e7de82015-07-14 08:31:56 -04001121
1122 if (baselen > len)
1123 return;
Thomas Pedersen05a23ae2013-11-05 11:16:59 -08001124 }
1125 ieee802_11_parse_elems(baseaddr, len - baselen, true, &elems);
1126 mesh_process_plink_frame(sdata, mgmt, &elems);
1127}