mac80211: don't request ack for peering close
[linux-2.6-block.git] / net / mac80211 / mesh_plink.c
CommitLineData
c3896d2c 1/*
264d9b7d 2 * Copyright (c) 2008, 2009 open80211s Ltd.
c3896d2c
LCC
3 * 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 */
5a0e3ad6 9#include <linux/gfp.h>
902acc78
JB
10#include <linux/kernel.h>
11#include <linux/random.h>
c3896d2c 12#include "ieee80211_i.h"
2c8dccc7 13#include "rate.h"
c3896d2c 14#include "mesh.h"
c3896d2c 15
8db09850
TP
16#define PLINK_GET_LLID(p) (p + 2)
17#define PLINK_GET_PLID(p) (p + 4)
c3896d2c
LCC
18
19#define mod_plink_timer(s, t) (mod_timer(&s->plink_timer, \
20 jiffies + HZ * t / 1000))
21
472dbc45
JB
22#define dot11MeshMaxRetries(s) (s->u.mesh.mshcfg.dot11MeshMaxRetries)
23#define dot11MeshRetryTimeout(s) (s->u.mesh.mshcfg.dot11MeshRetryTimeout)
24#define dot11MeshConfirmTimeout(s) (s->u.mesh.mshcfg.dot11MeshConfirmTimeout)
25#define dot11MeshHoldingTimeout(s) (s->u.mesh.mshcfg.dot11MeshHoldingTimeout)
26#define dot11MeshMaxPeerLinks(s) (s->u.mesh.mshcfg.dot11MeshMaxPeerLinks)
c3896d2c 27
3d4f9699
AN
28/* We only need a valid sta if user configured a minimum rssi_threshold. */
29#define rssi_threshold_check(sta, sdata) \
55335137 30 (sdata->u.mesh.mshcfg.rssi_threshold == 0 ||\
3d4f9699
AN
31 (sta && (s8) -ewma_read(&sta->avg_signal) > \
32 sdata->u.mesh.mshcfg.rssi_threshold))
55335137 33
c3896d2c
LCC
34enum plink_event {
35 PLINK_UNDEFINED,
36 OPN_ACPT,
37 OPN_RJCT,
38 OPN_IGNR,
39 CNF_ACPT,
40 CNF_RJCT,
41 CNF_IGNR,
42 CLS_ACPT,
43 CLS_IGNR
44};
45
ba4a14e1
TP
46static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
47 enum ieee80211_self_protected_actioncode action,
48 u8 *da, __le16 llid, __le16 plid, __le16 reason);
49
c3896d2c
LCC
50static inline
51void mesh_plink_inc_estab_count(struct ieee80211_sub_if_data *sdata)
52{
472dbc45 53 atomic_inc(&sdata->u.mesh.mshstats.estab_plinks);
d0709a65 54 mesh_accept_plinks_update(sdata);
c3896d2c
LCC
55}
56
57static inline
58void mesh_plink_dec_estab_count(struct ieee80211_sub_if_data *sdata)
59{
472dbc45 60 atomic_dec(&sdata->u.mesh.mshstats.estab_plinks);
d0709a65 61 mesh_accept_plinks_update(sdata);
c3896d2c
LCC
62}
63
64/**
65 * mesh_plink_fsm_restart - restart a mesh peer link finite state machine
66 *
23c7a29c 67 * @sta: mesh peer link to restart
c3896d2c 68 *
07346f81 69 * Locking: this function must be called holding sta->lock
c3896d2c
LCC
70 */
71static inline void mesh_plink_fsm_restart(struct sta_info *sta)
72{
57cf8043 73 sta->plink_state = NL80211_PLINK_LISTEN;
37659ff8
LCC
74 sta->llid = sta->plid = sta->reason = 0;
75 sta->plink_retries = 0;
c3896d2c
LCC
76}
77
93e5deb1 78/*
54ab1ffb 79 * Allocate mesh sta entry and insert into station table
93e5deb1 80 */
03e4497e 81static struct sta_info *mesh_plink_alloc(struct ieee80211_sub_if_data *sdata,
54ab1ffb 82 u8 *hw_addr)
c3896d2c 83{
c3896d2c
LCC
84 struct sta_info *sta;
85
54ab1ffb 86 if (sdata->local->num_sta >= MESH_MAX_PLINKS)
73651ee6 87 return NULL;
c3896d2c 88
34e89507 89 sta = sta_info_alloc(sdata, hw_addr, GFP_KERNEL);
73651ee6
JB
90 if (!sta)
91 return NULL;
c3896d2c 92
83d5cc01
JB
93 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
94 sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
95 sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
d9a7ddb0 96
c2c98fde 97 set_sta_flag(sta, WLAN_STA_WME);
d9a7ddb0 98
c3896d2c
LCC
99 return sta;
100}
101
2c53040f 102/**
cbf9322e 103 * mesh_set_ht_prot_mode - set correct HT protection mode
57aac7c5 104 *
cbf9322e
AN
105 * Section 9.23.3.5 of IEEE 80211-2012 describes the protection rules for HT
106 * mesh STA in a MBSS. Three HT protection modes are supported for now, non-HT
107 * mixed mode, 20MHz-protection and no-protection mode. non-HT mixed mode is
108 * selected if any non-HT peers are present in our MBSS. 20MHz-protection mode
109 * is selected if all peers in our 20/40MHz MBSS support HT and atleast one
110 * HT20 peer is present. Otherwise no-protection mode is selected.
57aac7c5
AN
111 */
112static u32 mesh_set_ht_prot_mode(struct ieee80211_sub_if_data *sdata)
113{
114 struct ieee80211_local *local = sdata->local;
115 struct sta_info *sta;
116 u32 changed = 0;
117 u16 ht_opmode;
118 bool non_ht_sta = false, ht20_sta = false;
119
120 if (local->_oper_channel_type == NL80211_CHAN_NO_HT)
121 return 0;
122
123 rcu_read_lock();
124 list_for_each_entry_rcu(sta, &local->sta_list, list) {
cbf9322e
AN
125 if (sdata != sta->sdata ||
126 sta->plink_state != NL80211_PLINK_ESTAB)
127 continue;
128
129 switch (sta->ch_type) {
130 case NL80211_CHAN_NO_HT:
bdcbd8e0
JB
131 mpl_dbg(sdata,
132 "mesh_plink %pM: nonHT sta (%pM) is present\n",
cbf9322e
AN
133 sdata->vif.addr, sta->sta.addr);
134 non_ht_sta = true;
135 goto out;
136 case NL80211_CHAN_HT20:
bdcbd8e0
JB
137 mpl_dbg(sdata,
138 "mesh_plink %pM: HT20 sta (%pM) is present\n",
cbf9322e
AN
139 sdata->vif.addr, sta->sta.addr);
140 ht20_sta = true;
141 default:
142 break;
57aac7c5
AN
143 }
144 }
145out:
146 rcu_read_unlock();
147
148 if (non_ht_sta)
149 ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED;
150 else if (ht20_sta && local->_oper_channel_type > NL80211_CHAN_HT20)
151 ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_20MHZ;
152 else
153 ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
154
155 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
156 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
70c33eaa 157 sdata->u.mesh.mshcfg.ht_opmode = ht_opmode;
57aac7c5 158 changed = BSS_CHANGED_HT;
bdcbd8e0
JB
159 mpl_dbg(sdata,
160 "mesh_plink %pM: protection mode changed to %d\n",
57aac7c5
AN
161 sdata->vif.addr, ht_opmode);
162 }
163
164 return changed;
165}
166
c3896d2c 167/**
c9370197 168 * __mesh_plink_deactivate - deactivate mesh peer link
c3896d2c
LCC
169 *
170 * @sta: mesh peer link to deactivate
171 *
172 * All mesh paths with this peer as next hop will be flushed
173 *
07346f81 174 * Locking: the caller must hold sta->lock
c3896d2c 175 */
c9370197 176static bool __mesh_plink_deactivate(struct sta_info *sta)
c3896d2c 177{
d0709a65 178 struct ieee80211_sub_if_data *sdata = sta->sdata;
c9370197 179 bool deactivated = false;
d0709a65 180
57cf8043 181 if (sta->plink_state == NL80211_PLINK_ESTAB) {
c3896d2c 182 mesh_plink_dec_estab_count(sdata);
c9370197
JL
183 deactivated = true;
184 }
57cf8043 185 sta->plink_state = NL80211_PLINK_BLOCKED;
c3896d2c 186 mesh_path_flush_by_nexthop(sta);
c9370197
JL
187
188 return deactivated;
c3896d2c
LCC
189}
190
902acc78 191/**
c9370197 192 * mesh_plink_deactivate - deactivate mesh peer link
902acc78
JB
193 *
194 * @sta: mesh peer link to deactivate
195 *
196 * All mesh paths with this peer as next hop will be flushed
197 */
198void mesh_plink_deactivate(struct sta_info *sta)
199{
c9370197
JL
200 struct ieee80211_sub_if_data *sdata = sta->sdata;
201 bool deactivated;
202
07346f81 203 spin_lock_bh(&sta->lock);
c9370197 204 deactivated = __mesh_plink_deactivate(sta);
ba4a14e1
TP
205 sta->reason = cpu_to_le16(WLAN_REASON_MESH_PEER_CANCELED);
206 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
207 sta->sta.addr, sta->llid, sta->plid,
208 sta->reason);
07346f81 209 spin_unlock_bh(&sta->lock);
c9370197
JL
210
211 if (deactivated)
212 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
902acc78
JB
213}
214
f698d856 215static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
54ef656b
TP
216 enum ieee80211_self_protected_actioncode action,
217 u8 *da, __le16 llid, __le16 plid, __le16 reason) {
f698d856 218 struct ieee80211_local *local = sdata->local;
3b69a9c5 219 struct sk_buff *skb;
e7570dfb 220 struct ieee80211_tx_info *info;
c3896d2c
LCC
221 struct ieee80211_mgmt *mgmt;
222 bool include_plid = false;
8db09850 223 u16 peering_proto = 0;
3b69a9c5
TP
224 u8 *pos, ie_len = 4;
225 int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.self_prot) +
226 sizeof(mgmt->u.action.u.self_prot);
227
65e8b0cc 228 skb = dev_alloc_skb(local->tx_headroom +
3b69a9c5
TP
229 hdr_len +
230 2 + /* capability info */
231 2 + /* AID */
232 2 + 8 + /* supported rates */
233 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
234 2 + sdata->u.mesh.mesh_id_len +
235 2 + sizeof(struct ieee80211_meshconf_ie) +
176f3608 236 2 + sizeof(struct ieee80211_ht_cap) +
074d46d1 237 2 + sizeof(struct ieee80211_ht_operation) +
3b69a9c5
TP
238 2 + 8 + /* peering IE */
239 sdata->u.mesh.ie_len);
c3896d2c
LCC
240 if (!skb)
241 return -1;
e7570dfb 242 info = IEEE80211_SKB_CB(skb);
65e8b0cc 243 skb_reserve(skb, local->tx_headroom);
3b69a9c5
TP
244 mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
245 memset(mgmt, 0, hdr_len);
e7827a70
HH
246 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
247 IEEE80211_STYPE_ACTION);
c3896d2c 248 memcpy(mgmt->da, da, ETH_ALEN);
47846c9b 249 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
915b5c50 250 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
8db09850
TP
251 mgmt->u.action.category = WLAN_CATEGORY_SELF_PROTECTED;
252 mgmt->u.action.u.self_prot.action_code = action;
c3896d2c 253
8db09850
TP
254 if (action != WLAN_SP_MESH_PEERING_CLOSE) {
255 /* capability info */
256 pos = skb_put(skb, 2);
257 memset(pos, 0, 2);
54ef656b 258 if (action == WLAN_SP_MESH_PEERING_CONFIRM) {
8db09850
TP
259 /* AID */
260 pos = skb_put(skb, 2);
77fa76bb 261 memcpy(pos + 2, &plid, 2);
c3896d2c 262 }
6b77863b
JB
263 if (ieee80211_add_srates_ie(sdata, skb, true,
264 local->oper_channel->band) ||
265 ieee80211_add_ext_srates_ie(sdata, skb, true,
266 local->oper_channel->band) ||
082ebb0c
TP
267 mesh_add_rsn_ie(skb, sdata) ||
268 mesh_add_meshid_ie(skb, sdata) ||
269 mesh_add_meshconf_ie(skb, sdata))
270 return -1;
8db09850 271 } else { /* WLAN_SP_MESH_PEERING_CLOSE */
e7570dfb 272 info->flags |= IEEE80211_TX_CTL_NO_ACK;
8db09850
TP
273 if (mesh_add_meshid_ie(skb, sdata))
274 return -1;
c3896d2c
LCC
275 }
276
8db09850 277 /* Add Mesh Peering Management element */
c3896d2c 278 switch (action) {
54ef656b 279 case WLAN_SP_MESH_PEERING_OPEN:
c3896d2c 280 break;
54ef656b 281 case WLAN_SP_MESH_PEERING_CONFIRM:
8db09850 282 ie_len += 2;
c3896d2c
LCC
283 include_plid = true;
284 break;
54ef656b 285 case WLAN_SP_MESH_PEERING_CLOSE:
8db09850
TP
286 if (plid) {
287 ie_len += 2;
c3896d2c
LCC
288 include_plid = true;
289 }
8db09850 290 ie_len += 2; /* reason code */
c3896d2c 291 break;
8db09850
TP
292 default:
293 return -EINVAL;
c3896d2c
LCC
294 }
295
8db09850
TP
296 if (WARN_ON(skb_tailroom(skb) < 2 + ie_len))
297 return -ENOMEM;
298
c3896d2c 299 pos = skb_put(skb, 2 + ie_len);
8db09850 300 *pos++ = WLAN_EID_PEER_MGMT;
c3896d2c 301 *pos++ = ie_len;
8db09850
TP
302 memcpy(pos, &peering_proto, 2);
303 pos += 2;
c3896d2c 304 memcpy(pos, &llid, 2);
8db09850 305 pos += 2;
c3896d2c 306 if (include_plid) {
c3896d2c 307 memcpy(pos, &plid, 2);
8db09850 308 pos += 2;
c3896d2c 309 }
54ef656b 310 if (action == WLAN_SP_MESH_PEERING_CLOSE) {
c3896d2c 311 memcpy(pos, &reason, 2);
8db09850 312 pos += 2;
c3896d2c 313 }
176f3608
TP
314
315 if (action != WLAN_SP_MESH_PEERING_CLOSE) {
316 if (mesh_add_ht_cap_ie(skb, sdata) ||
074d46d1 317 mesh_add_ht_oper_ie(skb, sdata))
176f3608
TP
318 return -1;
319 }
320
8db09850
TP
321 if (mesh_add_vendor_ies(skb, sdata))
322 return -1;
c3896d2c 323
62ae67be 324 ieee80211_tx_skb(sdata, skb);
c3896d2c
LCC
325 return 0;
326}
327
2c53040f
BH
328/**
329 * mesh_peer_init - initialize new mesh peer and return resulting sta_info
54ab1ffb
TP
330 *
331 * @sdata: local meshif
332 * @addr: peer's address
54ab1ffb
TP
333 * @elems: IEs from beacon or mesh peering frame
334 *
335 * call under RCU
336 */
337static struct sta_info *mesh_peer_init(struct ieee80211_sub_if_data *sdata,
f743ff49 338 u8 *addr,
54ab1ffb 339 struct ieee802_11_elems *elems)
c3896d2c 340{
f698d856 341 struct ieee80211_local *local = sdata->local;
f743ff49 342 enum ieee80211_band band = local->oper_channel->band;
54ab1ffb 343 struct ieee80211_supported_band *sband;
f743ff49 344 u32 rates, basic_rates = 0;
c3896d2c 345 struct sta_info *sta;
e87278e7 346 bool insert = false;
c3896d2c 347
f743ff49
TP
348 sband = local->hw.wiphy->bands[band];
349 rates = ieee80211_sta_get_rates(local, elems, band, &basic_rates);
d0709a65 350
54ab1ffb 351 sta = sta_info_get(sdata, addr);
c3896d2c 352 if (!sta) {
f5c56814
TP
353 /* Userspace handles peer allocation when security is enabled */
354 if (sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED) {
355 cfg80211_notify_new_peer_candidate(sdata->dev, addr,
356 elems->ie_start,
357 elems->total_len,
358 GFP_ATOMIC);
359 return NULL;
360 }
361
54ab1ffb 362 sta = mesh_plink_alloc(sdata, addr);
34e89507 363 if (!sta)
54ab1ffb 364 return NULL;
e87278e7 365 insert = true;
c3896d2c
LCC
366 }
367
54ab1ffb 368 spin_lock_bh(&sta->lock);
c3896d2c 369 sta->last_rx = jiffies;
bae35d92
CYY
370 if (sta->plink_state == NL80211_PLINK_ESTAB) {
371 spin_unlock_bh(&sta->lock);
372 return sta;
373 }
374
f743ff49 375 sta->sta.supp_rates[band] = rates;
e76781e4
TP
376 if (elems->ht_cap_elem &&
377 sdata->local->_oper_channel_type != NL80211_CHAN_NO_HT)
54ab1ffb
TP
378 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
379 elems->ht_cap_elem,
380 &sta->sta.ht_cap);
381 else
382 memset(&sta->sta.ht_cap, 0, sizeof(sta->sta.ht_cap));
383
57aac7c5 384 if (elems->ht_operation) {
c7d25828
TP
385 if (!(elems->ht_operation->ht_param &
386 IEEE80211_HT_PARAM_CHAN_WIDTH_ANY))
387 sta->sta.ht_cap.cap &=
388 ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
57aac7c5
AN
389 sta->ch_type =
390 ieee80211_ht_oper_to_channel_type(elems->ht_operation);
391 }
c7d25828 392
54ab1ffb
TP
393 rate_control_rate_init(sta);
394 spin_unlock_bh(&sta->lock);
395
e87278e7
TP
396 if (insert && sta_info_insert(sta))
397 return NULL;
398
54ab1ffb
TP
399 return sta;
400}
401
f743ff49
TP
402void mesh_neighbour_update(struct ieee80211_sub_if_data *sdata,
403 u8 *hw_addr,
54ab1ffb
TP
404 struct ieee802_11_elems *elems)
405{
406 struct sta_info *sta;
407
54ab1ffb 408 rcu_read_lock();
f743ff49 409 sta = mesh_peer_init(sdata, hw_addr, elems);
54ab1ffb
TP
410 if (!sta)
411 goto out;
412
1570ca59 413 if (mesh_peer_accepts_plinks(elems) &&
54ab1ffb
TP
414 sta->plink_state == NL80211_PLINK_LISTEN &&
415 sdata->u.mesh.accepting_plinks &&
416 sdata->u.mesh.mshcfg.auto_open_plinks &&
417 rssi_threshold_check(sta, sdata))
c3896d2c
LCC
418 mesh_plink_open(sta);
419
54ab1ffb 420out:
d0709a65 421 rcu_read_unlock();
c3896d2c
LCC
422}
423
424static void mesh_plink_timer(unsigned long data)
425{
426 struct sta_info *sta;
427 __le16 llid, plid, reason;
c3896d2c 428 struct ieee80211_sub_if_data *sdata;
c3896d2c 429
d0709a65
JB
430 /*
431 * This STA is valid because sta_info_destroy() will
432 * del_timer_sync() this timer after having made sure
433 * it cannot be readded (by deleting the plink.)
434 */
c3896d2c
LCC
435 sta = (struct sta_info *) data;
436
5bb644a0
JB
437 if (sta->sdata->local->quiescing) {
438 sta->plink_timer_was_running = true;
439 return;
440 }
441
07346f81 442 spin_lock_bh(&sta->lock);
c3896d2c
LCC
443 if (sta->ignore_plink_timer) {
444 sta->ignore_plink_timer = false;
07346f81 445 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
446 return;
447 }
bdcbd8e0
JB
448 mpl_dbg(sta->sdata,
449 "Mesh plink timer for %pM fired on state %d\n",
0c68ae26 450 sta->sta.addr, sta->plink_state);
c3896d2c
LCC
451 reason = 0;
452 llid = sta->llid;
453 plid = sta->plid;
d0709a65 454 sdata = sta->sdata;
c3896d2c
LCC
455
456 switch (sta->plink_state) {
57cf8043
JC
457 case NL80211_PLINK_OPN_RCVD:
458 case NL80211_PLINK_OPN_SNT:
c3896d2c
LCC
459 /* retry timer */
460 if (sta->plink_retries < dot11MeshMaxRetries(sdata)) {
461 u32 rand;
bdcbd8e0
JB
462 mpl_dbg(sta->sdata,
463 "Mesh plink for %pM (retry, timeout): %d %d\n",
0c68ae26
JB
464 sta->sta.addr, sta->plink_retries,
465 sta->plink_timeout);
c3896d2c
LCC
466 get_random_bytes(&rand, sizeof(u32));
467 sta->plink_timeout = sta->plink_timeout +
468 rand % sta->plink_timeout;
469 ++sta->plink_retries;
d0709a65 470 mod_plink_timer(sta, sta->plink_timeout);
07346f81 471 spin_unlock_bh(&sta->lock);
54ef656b
TP
472 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
473 sta->sta.addr, llid, 0, 0);
c3896d2c
LCC
474 break;
475 }
54ef656b 476 reason = cpu_to_le16(WLAN_REASON_MESH_MAX_RETRIES);
c3896d2c 477 /* fall through on else */
57cf8043 478 case NL80211_PLINK_CNF_RCVD:
c3896d2c
LCC
479 /* confirm timer */
480 if (!reason)
54ef656b 481 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIRM_TIMEOUT);
57cf8043 482 sta->plink_state = NL80211_PLINK_HOLDING;
d0709a65 483 mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
07346f81 484 spin_unlock_bh(&sta->lock);
54ef656b
TP
485 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
486 sta->sta.addr, llid, plid, reason);
c3896d2c 487 break;
57cf8043 488 case NL80211_PLINK_HOLDING:
c3896d2c 489 /* holding timer */
d0709a65 490 del_timer(&sta->plink_timer);
c3896d2c 491 mesh_plink_fsm_restart(sta);
07346f81 492 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
493 break;
494 default:
07346f81 495 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
496 break;
497 }
c3896d2c
LCC
498}
499
5bb644a0
JB
500#ifdef CONFIG_PM
501void mesh_plink_quiesce(struct sta_info *sta)
502{
503 if (del_timer_sync(&sta->plink_timer))
504 sta->plink_timer_was_running = true;
505}
506
507void mesh_plink_restart(struct sta_info *sta)
508{
509 if (sta->plink_timer_was_running) {
510 add_timer(&sta->plink_timer);
511 sta->plink_timer_was_running = false;
512 }
513}
514#endif
515
c3896d2c
LCC
516static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout)
517{
518 sta->plink_timer.expires = jiffies + (HZ * timeout / 1000);
519 sta->plink_timer.data = (unsigned long) sta;
520 sta->plink_timer.function = mesh_plink_timer;
521 sta->plink_timeout = timeout;
c3896d2c
LCC
522 add_timer(&sta->plink_timer);
523}
524
525int mesh_plink_open(struct sta_info *sta)
526{
527 __le16 llid;
d0709a65 528 struct ieee80211_sub_if_data *sdata = sta->sdata;
c3896d2c 529
c2c98fde 530 if (!test_sta_flag(sta, WLAN_STA_AUTH))
53e80511
JC
531 return -EPERM;
532
07346f81 533 spin_lock_bh(&sta->lock);
c3896d2c
LCC
534 get_random_bytes(&llid, 2);
535 sta->llid = llid;
57cf8043 536 if (sta->plink_state != NL80211_PLINK_LISTEN) {
07346f81 537 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
538 return -EBUSY;
539 }
57cf8043 540 sta->plink_state = NL80211_PLINK_OPN_SNT;
c3896d2c 541 mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
07346f81 542 spin_unlock_bh(&sta->lock);
bdcbd8e0
JB
543 mpl_dbg(sdata,
544 "Mesh plink: starting establishment with %pM\n",
0c68ae26 545 sta->sta.addr);
c3896d2c 546
54ef656b 547 return mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
17741cdc 548 sta->sta.addr, llid, 0, 0);
c3896d2c
LCC
549}
550
551void mesh_plink_block(struct sta_info *sta)
552{
c9370197
JL
553 struct ieee80211_sub_if_data *sdata = sta->sdata;
554 bool deactivated;
555
07346f81 556 spin_lock_bh(&sta->lock);
c9370197 557 deactivated = __mesh_plink_deactivate(sta);
57cf8043 558 sta->plink_state = NL80211_PLINK_BLOCKED;
07346f81 559 spin_unlock_bh(&sta->lock);
c9370197
JL
560
561 if (deactivated)
562 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
c3896d2c
LCC
563}
564
c3896d2c 565
f698d856 566void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata, struct ieee80211_mgmt *mgmt,
c3896d2c
LCC
567 size_t len, struct ieee80211_rx_status *rx_status)
568{
c3896d2c
LCC
569 struct ieee802_11_elems elems;
570 struct sta_info *sta;
571 enum plink_event event;
54ef656b 572 enum ieee80211_self_protected_actioncode ftype;
c3896d2c 573 size_t baselen;
57aac7c5 574 bool matches_local = true;
c3896d2c
LCC
575 u8 ie_len;
576 u8 *baseaddr;
57aac7c5 577 u32 changed = 0;
c3896d2c 578 __le16 plid, llid, reason;
1460dd15 579 static const char *mplstates[] = {
57cf8043
JC
580 [NL80211_PLINK_LISTEN] = "LISTEN",
581 [NL80211_PLINK_OPN_SNT] = "OPN-SNT",
582 [NL80211_PLINK_OPN_RCVD] = "OPN-RCVD",
583 [NL80211_PLINK_CNF_RCVD] = "CNF_RCVD",
584 [NL80211_PLINK_ESTAB] = "ESTAB",
585 [NL80211_PLINK_HOLDING] = "HOLDING",
586 [NL80211_PLINK_BLOCKED] = "BLOCKED"
1460dd15 587 };
c3896d2c 588
9c80d3dc
JB
589 /* need action_code, aux */
590 if (len < IEEE80211_MIN_ACTION_SIZE + 3)
591 return;
592
c3896d2c 593 if (is_multicast_ether_addr(mgmt->da)) {
bdcbd8e0
JB
594 mpl_dbg(sdata,
595 "Mesh plink: ignore frame from multicast address\n");
c3896d2c
LCC
596 return;
597 }
598
8db09850
TP
599 baseaddr = mgmt->u.action.u.self_prot.variable;
600 baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
601 if (mgmt->u.action.u.self_prot.action_code ==
54ef656b 602 WLAN_SP_MESH_PEERING_CONFIRM) {
c3896d2c 603 baseaddr += 4;
70bdb6b2 604 baselen += 4;
c3896d2c
LCC
605 }
606 ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
8db09850 607 if (!elems.peering) {
bdcbd8e0
JB
608 mpl_dbg(sdata,
609 "Mesh plink: missing necessary peer link ie\n");
c3896d2c
LCC
610 return;
611 }
b130e5ce
JC
612 if (elems.rsn_len &&
613 sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
bdcbd8e0
JB
614 mpl_dbg(sdata,
615 "Mesh plink: can't establish link with secure peer\n");
5cff5e01
JC
616 return;
617 }
c3896d2c 618
8db09850
TP
619 ftype = mgmt->u.action.u.self_prot.action_code;
620 ie_len = elems.peering_len;
621 if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
622 (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
623 (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
624 && ie_len != 8)) {
bdcbd8e0
JB
625 mpl_dbg(sdata,
626 "Mesh plink: incorrect plink ie length %d %d\n",
627 ftype, ie_len);
c3896d2c
LCC
628 return;
629 }
630
54ef656b
TP
631 if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
632 (!elems.mesh_id || !elems.mesh_config)) {
bdcbd8e0 633 mpl_dbg(sdata, "Mesh plink: missing necessary ie\n");
c3896d2c
LCC
634 return;
635 }
636 /* Note the lines below are correct, the llid in the frame is the plid
637 * from the point of view of this host.
638 */
8db09850 639 memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
54ef656b 640 if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
8db09850
TP
641 (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
642 memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
c3896d2c 643
d0709a65
JB
644 rcu_read_lock();
645
abe60632 646 sta = sta_info_get(sdata, mgmt->sa);
54ef656b 647 if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
bdcbd8e0 648 mpl_dbg(sdata, "Mesh plink: cls or cnf from unknown peer\n");
d0709a65 649 rcu_read_unlock();
c3896d2c
LCC
650 return;
651 }
652
55335137 653 if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
3d4f9699 654 !rssi_threshold_check(sta, sdata)) {
bdcbd8e0 655 mpl_dbg(sdata, "Mesh plink: %pM does not meet rssi threshold\n",
3d4f9699 656 mgmt->sa);
55335137
AN
657 rcu_read_unlock();
658 return;
659 }
660
c2c98fde 661 if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
bdcbd8e0 662 mpl_dbg(sdata, "Mesh plink: Action frame from non-authed peer\n");
53e80511
JC
663 rcu_read_unlock();
664 return;
665 }
666
57cf8043 667 if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
d0709a65 668 rcu_read_unlock();
c3896d2c
LCC
669 return;
670 }
671
672 /* Now we will figure out the appropriate event... */
673 event = PLINK_UNDEFINED;
54ef656b 674 if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
f743ff49 675 !mesh_matches_local(sdata, &elems)) {
d12c7452 676 matches_local = false;
c3896d2c 677 switch (ftype) {
54ef656b 678 case WLAN_SP_MESH_PEERING_OPEN:
c3896d2c
LCC
679 event = OPN_RJCT;
680 break;
54ef656b 681 case WLAN_SP_MESH_PEERING_CONFIRM:
c3896d2c
LCC
682 event = CNF_RJCT;
683 break;
54ef656b 684 default:
c3896d2c
LCC
685 break;
686 }
d12c7452
CL
687 }
688
689 if (!sta && !matches_local) {
690 rcu_read_unlock();
54ef656b 691 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
d12c7452 692 llid = 0;
54ef656b
TP
693 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
694 mgmt->sa, llid, plid, reason);
d12c7452 695 return;
c3896d2c 696 } else if (!sta) {
54ef656b 697 /* ftype == WLAN_SP_MESH_PEERING_OPEN */
c3896d2c 698 if (!mesh_plink_free_count(sdata)) {
bdcbd8e0 699 mpl_dbg(sdata, "Mesh plink error: no more free plinks\n");
73651ee6
JB
700 rcu_read_unlock();
701 return;
702 }
c3896d2c 703 event = OPN_ACPT;
d12c7452 704 } else if (matches_local) {
c3896d2c 705 switch (ftype) {
54ef656b 706 case WLAN_SP_MESH_PEERING_OPEN:
c3896d2c 707 if (!mesh_plink_free_count(sdata) ||
d0709a65 708 (sta->plid && sta->plid != plid))
c3896d2c
LCC
709 event = OPN_IGNR;
710 else
711 event = OPN_ACPT;
712 break;
54ef656b 713 case WLAN_SP_MESH_PEERING_CONFIRM:
c3896d2c 714 if (!mesh_plink_free_count(sdata) ||
d0709a65 715 (sta->llid != llid || sta->plid != plid))
c3896d2c
LCC
716 event = CNF_IGNR;
717 else
718 event = CNF_ACPT;
719 break;
54ef656b 720 case WLAN_SP_MESH_PEERING_CLOSE:
57cf8043 721 if (sta->plink_state == NL80211_PLINK_ESTAB)
c3896d2c
LCC
722 /* Do not check for llid or plid. This does not
723 * follow the standard but since multiple plinks
724 * per sta are not supported, it is necessary in
725 * order to avoid a livelock when MP A sees an
726 * establish peer link to MP B but MP B does not
727 * see it. This can be caused by a timeout in
728 * B's peer link establishment or B beign
729 * restarted.
730 */
731 event = CLS_ACPT;
732 else if (sta->plid != plid)
733 event = CLS_IGNR;
734 else if (ie_len == 7 && sta->llid != llid)
735 event = CLS_IGNR;
736 else
737 event = CLS_ACPT;
738 break;
739 default:
bdcbd8e0 740 mpl_dbg(sdata, "Mesh plink: unknown frame subtype\n");
d0709a65 741 rcu_read_unlock();
c3896d2c
LCC
742 return;
743 }
54ab1ffb
TP
744 }
745
746 if (event == OPN_ACPT) {
747 /* allocate sta entry if necessary and update info */
f743ff49 748 sta = mesh_peer_init(sdata, mgmt->sa, &elems);
54ab1ffb 749 if (!sta) {
bdcbd8e0 750 mpl_dbg(sdata, "Mesh plink: failed to init peer!\n");
54ab1ffb
TP
751 rcu_read_unlock();
752 return;
753 }
c3896d2c
LCC
754 }
755
bdcbd8e0
JB
756 mpl_dbg(sdata,
757 "Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
1460dd15 758 mgmt->sa, mplstates[sta->plink_state],
0c68ae26
JB
759 le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
760 event);
c3896d2c 761 reason = 0;
54ab1ffb 762 spin_lock_bh(&sta->lock);
c3896d2c
LCC
763 switch (sta->plink_state) {
764 /* spin_unlock as soon as state is updated at each case */
57cf8043 765 case NL80211_PLINK_LISTEN:
c3896d2c
LCC
766 switch (event) {
767 case CLS_ACPT:
768 mesh_plink_fsm_restart(sta);
07346f81 769 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
770 break;
771 case OPN_ACPT:
57cf8043 772 sta->plink_state = NL80211_PLINK_OPN_RCVD;
c3896d2c
LCC
773 sta->plid = plid;
774 get_random_bytes(&llid, 2);
775 sta->llid = llid;
776 mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
07346f81 777 spin_unlock_bh(&sta->lock);
54ef656b
TP
778 mesh_plink_frame_tx(sdata,
779 WLAN_SP_MESH_PEERING_OPEN,
780 sta->sta.addr, llid, 0, 0);
781 mesh_plink_frame_tx(sdata,
782 WLAN_SP_MESH_PEERING_CONFIRM,
783 sta->sta.addr, llid, plid, 0);
c3896d2c
LCC
784 break;
785 default:
07346f81 786 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
787 break;
788 }
789 break;
790
57cf8043 791 case NL80211_PLINK_OPN_SNT:
c3896d2c
LCC
792 switch (event) {
793 case OPN_RJCT:
794 case CNF_RJCT:
54ef656b 795 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
c3896d2c
LCC
796 case CLS_ACPT:
797 if (!reason)
54ef656b 798 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
c3896d2c 799 sta->reason = reason;
57cf8043 800 sta->plink_state = NL80211_PLINK_HOLDING;
c3896d2c
LCC
801 if (!mod_plink_timer(sta,
802 dot11MeshHoldingTimeout(sdata)))
803 sta->ignore_plink_timer = true;
804
805 llid = sta->llid;
07346f81 806 spin_unlock_bh(&sta->lock);
54ef656b
TP
807 mesh_plink_frame_tx(sdata,
808 WLAN_SP_MESH_PEERING_CLOSE,
809 sta->sta.addr, llid, plid, reason);
c3896d2c
LCC
810 break;
811 case OPN_ACPT:
812 /* retry timer is left untouched */
57cf8043 813 sta->plink_state = NL80211_PLINK_OPN_RCVD;
c3896d2c
LCC
814 sta->plid = plid;
815 llid = sta->llid;
07346f81 816 spin_unlock_bh(&sta->lock);
54ef656b
TP
817 mesh_plink_frame_tx(sdata,
818 WLAN_SP_MESH_PEERING_CONFIRM,
819 sta->sta.addr, llid, plid, 0);
c3896d2c
LCC
820 break;
821 case CNF_ACPT:
57cf8043 822 sta->plink_state = NL80211_PLINK_CNF_RCVD;
c3896d2c
LCC
823 if (!mod_plink_timer(sta,
824 dot11MeshConfirmTimeout(sdata)))
825 sta->ignore_plink_timer = true;
826
07346f81 827 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
828 break;
829 default:
07346f81 830 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
831 break;
832 }
833 break;
834
57cf8043 835 case NL80211_PLINK_OPN_RCVD:
c3896d2c
LCC
836 switch (event) {
837 case OPN_RJCT:
838 case CNF_RJCT:
54ef656b 839 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
c3896d2c
LCC
840 case CLS_ACPT:
841 if (!reason)
54ef656b 842 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
c3896d2c 843 sta->reason = reason;
57cf8043 844 sta->plink_state = NL80211_PLINK_HOLDING;
c3896d2c
LCC
845 if (!mod_plink_timer(sta,
846 dot11MeshHoldingTimeout(sdata)))
847 sta->ignore_plink_timer = true;
848
849 llid = sta->llid;
07346f81 850 spin_unlock_bh(&sta->lock);
54ef656b
TP
851 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
852 sta->sta.addr, llid, plid, reason);
c3896d2c
LCC
853 break;
854 case OPN_ACPT:
855 llid = sta->llid;
07346f81 856 spin_unlock_bh(&sta->lock);
54ef656b
TP
857 mesh_plink_frame_tx(sdata,
858 WLAN_SP_MESH_PEERING_CONFIRM,
859 sta->sta.addr, llid, plid, 0);
c3896d2c
LCC
860 break;
861 case CNF_ACPT:
d0709a65 862 del_timer(&sta->plink_timer);
57cf8043 863 sta->plink_state = NL80211_PLINK_ESTAB;
07346f81 864 spin_unlock_bh(&sta->lock);
c9370197 865 mesh_plink_inc_estab_count(sdata);
57aac7c5
AN
866 changed |= mesh_set_ht_prot_mode(sdata);
867 changed |= BSS_CHANGED_BEACON;
bdcbd8e0 868 mpl_dbg(sdata, "Mesh plink with %pM ESTABLISHED\n",
0c68ae26 869 sta->sta.addr);
c3896d2c
LCC
870 break;
871 default:
07346f81 872 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
873 break;
874 }
875 break;
876
57cf8043 877 case NL80211_PLINK_CNF_RCVD:
c3896d2c
LCC
878 switch (event) {
879 case OPN_RJCT:
880 case CNF_RJCT:
54ef656b 881 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
c3896d2c
LCC
882 case CLS_ACPT:
883 if (!reason)
54ef656b 884 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
c3896d2c 885 sta->reason = reason;
57cf8043 886 sta->plink_state = NL80211_PLINK_HOLDING;
c3896d2c
LCC
887 if (!mod_plink_timer(sta,
888 dot11MeshHoldingTimeout(sdata)))
889 sta->ignore_plink_timer = true;
890
891 llid = sta->llid;
07346f81 892 spin_unlock_bh(&sta->lock);
54ef656b
TP
893 mesh_plink_frame_tx(sdata,
894 WLAN_SP_MESH_PEERING_CLOSE,
895 sta->sta.addr, llid, plid, reason);
ff59dc76 896 break;
c3896d2c 897 case OPN_ACPT:
d0709a65 898 del_timer(&sta->plink_timer);
57cf8043 899 sta->plink_state = NL80211_PLINK_ESTAB;
07346f81 900 spin_unlock_bh(&sta->lock);
c9370197 901 mesh_plink_inc_estab_count(sdata);
57aac7c5
AN
902 changed |= mesh_set_ht_prot_mode(sdata);
903 changed |= BSS_CHANGED_BEACON;
bdcbd8e0 904 mpl_dbg(sdata, "Mesh plink with %pM ESTABLISHED\n",
0c68ae26 905 sta->sta.addr);
54ef656b
TP
906 mesh_plink_frame_tx(sdata,
907 WLAN_SP_MESH_PEERING_CONFIRM,
908 sta->sta.addr, llid, plid, 0);
c3896d2c
LCC
909 break;
910 default:
07346f81 911 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
912 break;
913 }
914 break;
915
57cf8043 916 case NL80211_PLINK_ESTAB:
c3896d2c
LCC
917 switch (event) {
918 case CLS_ACPT:
54ef656b 919 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
c3896d2c 920 sta->reason = reason;
57aac7c5 921 __mesh_plink_deactivate(sta);
57cf8043 922 sta->plink_state = NL80211_PLINK_HOLDING;
c3896d2c 923 llid = sta->llid;
d0709a65 924 mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
07346f81 925 spin_unlock_bh(&sta->lock);
57aac7c5
AN
926 changed |= mesh_set_ht_prot_mode(sdata);
927 changed |= BSS_CHANGED_BEACON;
54ef656b
TP
928 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
929 sta->sta.addr, llid, plid, reason);
c3896d2c
LCC
930 break;
931 case OPN_ACPT:
932 llid = sta->llid;
07346f81 933 spin_unlock_bh(&sta->lock);
54ef656b
TP
934 mesh_plink_frame_tx(sdata,
935 WLAN_SP_MESH_PEERING_CONFIRM,
936 sta->sta.addr, llid, plid, 0);
c3896d2c
LCC
937 break;
938 default:
07346f81 939 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
940 break;
941 }
942 break;
57cf8043 943 case NL80211_PLINK_HOLDING:
c3896d2c
LCC
944 switch (event) {
945 case CLS_ACPT:
d0709a65 946 if (del_timer(&sta->plink_timer))
c3896d2c 947 sta->ignore_plink_timer = 1;
c3896d2c 948 mesh_plink_fsm_restart(sta);
07346f81 949 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
950 break;
951 case OPN_ACPT:
952 case CNF_ACPT:
953 case OPN_RJCT:
954 case CNF_RJCT:
955 llid = sta->llid;
956 reason = sta->reason;
07346f81 957 spin_unlock_bh(&sta->lock);
54ef656b
TP
958 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
959 sta->sta.addr, llid, plid, reason);
c3896d2c
LCC
960 break;
961 default:
07346f81 962 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
963 }
964 break;
965 default:
b4e08ea1 966 /* should not get here, PLINK_BLOCKED is dealt with at the
3ad2f3fb 967 * beginning of the function
c3896d2c 968 */
07346f81 969 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
970 break;
971 }
d0709a65
JB
972
973 rcu_read_unlock();
57aac7c5
AN
974
975 if (changed)
976 ieee80211_bss_info_change_notify(sdata, changed);
c3896d2c 977}