ath9k_hw: Updated AR9462 initval table to improve rx performance
[linux-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
LCC
15
16#ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
17#define mpl_dbg(fmt, args...) printk(KERN_DEBUG fmt, ##args)
18#else
19#define mpl_dbg(fmt, args...) do { (void)(0); } while (0)
20#endif
21
8db09850
TP
22#define PLINK_GET_LLID(p) (p + 2)
23#define PLINK_GET_PLID(p) (p + 4)
c3896d2c
LCC
24
25#define mod_plink_timer(s, t) (mod_timer(&s->plink_timer, \
26 jiffies + HZ * t / 1000))
27
472dbc45
JB
28#define dot11MeshMaxRetries(s) (s->u.mesh.mshcfg.dot11MeshMaxRetries)
29#define dot11MeshRetryTimeout(s) (s->u.mesh.mshcfg.dot11MeshRetryTimeout)
30#define dot11MeshConfirmTimeout(s) (s->u.mesh.mshcfg.dot11MeshConfirmTimeout)
31#define dot11MeshHoldingTimeout(s) (s->u.mesh.mshcfg.dot11MeshHoldingTimeout)
32#define dot11MeshMaxPeerLinks(s) (s->u.mesh.mshcfg.dot11MeshMaxPeerLinks)
c3896d2c 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
JB
78/*
79 * NOTE: This is just an alias for sta_info_alloc(), see notes
80 * on it in the lifecycle management section!
81 */
03e4497e 82static struct sta_info *mesh_plink_alloc(struct ieee80211_sub_if_data *sdata,
881d948c 83 u8 *hw_addr, u32 rates)
c3896d2c 84{
d0709a65 85 struct ieee80211_local *local = sdata->local;
c3896d2c
LCC
86 struct sta_info *sta;
87
c3896d2c 88 if (local->num_sta >= MESH_MAX_PLINKS)
73651ee6 89 return NULL;
c3896d2c 90
34e89507 91 sta = sta_info_alloc(sdata, hw_addr, GFP_KERNEL);
73651ee6
JB
92 if (!sta)
93 return NULL;
c3896d2c 94
c2c98fde
JB
95 set_sta_flag(sta, WLAN_STA_AUTH);
96 set_sta_flag(sta, WLAN_STA_AUTHORIZED);
97 set_sta_flag(sta, WLAN_STA_WME);
323ce79a 98 sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
b973c31a 99 rate_control_rate_init(sta);
c3896d2c
LCC
100
101 return sta;
102}
103
104/**
c9370197 105 * __mesh_plink_deactivate - deactivate mesh peer link
c3896d2c
LCC
106 *
107 * @sta: mesh peer link to deactivate
108 *
109 * All mesh paths with this peer as next hop will be flushed
110 *
07346f81 111 * Locking: the caller must hold sta->lock
c3896d2c 112 */
c9370197 113static bool __mesh_plink_deactivate(struct sta_info *sta)
c3896d2c 114{
d0709a65 115 struct ieee80211_sub_if_data *sdata = sta->sdata;
c9370197 116 bool deactivated = false;
d0709a65 117
57cf8043 118 if (sta->plink_state == NL80211_PLINK_ESTAB) {
c3896d2c 119 mesh_plink_dec_estab_count(sdata);
c9370197
JL
120 deactivated = true;
121 }
57cf8043 122 sta->plink_state = NL80211_PLINK_BLOCKED;
c3896d2c 123 mesh_path_flush_by_nexthop(sta);
c9370197
JL
124
125 return deactivated;
c3896d2c
LCC
126}
127
902acc78 128/**
c9370197 129 * mesh_plink_deactivate - deactivate mesh peer link
902acc78
JB
130 *
131 * @sta: mesh peer link to deactivate
132 *
133 * All mesh paths with this peer as next hop will be flushed
134 */
135void mesh_plink_deactivate(struct sta_info *sta)
136{
c9370197
JL
137 struct ieee80211_sub_if_data *sdata = sta->sdata;
138 bool deactivated;
139
07346f81 140 spin_lock_bh(&sta->lock);
c9370197 141 deactivated = __mesh_plink_deactivate(sta);
ba4a14e1
TP
142 sta->reason = cpu_to_le16(WLAN_REASON_MESH_PEER_CANCELED);
143 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
144 sta->sta.addr, sta->llid, sta->plid,
145 sta->reason);
07346f81 146 spin_unlock_bh(&sta->lock);
c9370197
JL
147
148 if (deactivated)
149 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
902acc78
JB
150}
151
f698d856 152static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
54ef656b
TP
153 enum ieee80211_self_protected_actioncode action,
154 u8 *da, __le16 llid, __le16 plid, __le16 reason) {
f698d856 155 struct ieee80211_local *local = sdata->local;
c80d545d 156 struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400 +
581a8b0f 157 sdata->u.mesh.ie_len);
c3896d2c
LCC
158 struct ieee80211_mgmt *mgmt;
159 bool include_plid = false;
8db09850
TP
160 int ie_len = 4;
161 u16 peering_proto = 0;
c3896d2c 162 u8 *pos;
c3896d2c
LCC
163
164 if (!skb)
165 return -1;
166 skb_reserve(skb, local->hw.extra_tx_headroom);
167 /* 25 is the size of the common mgmt part (24) plus the size of the
168 * common action part (1)
169 */
170 mgmt = (struct ieee80211_mgmt *)
8db09850
TP
171 skb_put(skb, 25 + sizeof(mgmt->u.action.u.self_prot));
172 memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.self_prot));
e7827a70
HH
173 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
174 IEEE80211_STYPE_ACTION);
c3896d2c 175 memcpy(mgmt->da, da, ETH_ALEN);
47846c9b 176 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
915b5c50 177 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
8db09850
TP
178 mgmt->u.action.category = WLAN_CATEGORY_SELF_PROTECTED;
179 mgmt->u.action.u.self_prot.action_code = action;
c3896d2c 180
8db09850
TP
181 if (action != WLAN_SP_MESH_PEERING_CLOSE) {
182 /* capability info */
183 pos = skb_put(skb, 2);
184 memset(pos, 0, 2);
54ef656b 185 if (action == WLAN_SP_MESH_PEERING_CONFIRM) {
8db09850
TP
186 /* AID */
187 pos = skb_put(skb, 2);
77fa76bb 188 memcpy(pos + 2, &plid, 2);
c3896d2c 189 }
768db343
AN
190 if (ieee80211_add_srates_ie(&sdata->vif, skb) ||
191 ieee80211_add_ext_srates_ie(&sdata->vif, skb) ||
082ebb0c
TP
192 mesh_add_rsn_ie(skb, sdata) ||
193 mesh_add_meshid_ie(skb, sdata) ||
194 mesh_add_meshconf_ie(skb, sdata))
195 return -1;
8db09850
TP
196 } else { /* WLAN_SP_MESH_PEERING_CLOSE */
197 if (mesh_add_meshid_ie(skb, sdata))
198 return -1;
c3896d2c
LCC
199 }
200
8db09850 201 /* Add Mesh Peering Management element */
c3896d2c 202 switch (action) {
54ef656b 203 case WLAN_SP_MESH_PEERING_OPEN:
c3896d2c 204 break;
54ef656b 205 case WLAN_SP_MESH_PEERING_CONFIRM:
8db09850 206 ie_len += 2;
c3896d2c
LCC
207 include_plid = true;
208 break;
54ef656b 209 case WLAN_SP_MESH_PEERING_CLOSE:
8db09850
TP
210 if (plid) {
211 ie_len += 2;
c3896d2c
LCC
212 include_plid = true;
213 }
8db09850 214 ie_len += 2; /* reason code */
c3896d2c 215 break;
8db09850
TP
216 default:
217 return -EINVAL;
c3896d2c
LCC
218 }
219
8db09850
TP
220 if (WARN_ON(skb_tailroom(skb) < 2 + ie_len))
221 return -ENOMEM;
222
c3896d2c 223 pos = skb_put(skb, 2 + ie_len);
8db09850 224 *pos++ = WLAN_EID_PEER_MGMT;
c3896d2c 225 *pos++ = ie_len;
8db09850
TP
226 memcpy(pos, &peering_proto, 2);
227 pos += 2;
c3896d2c 228 memcpy(pos, &llid, 2);
8db09850 229 pos += 2;
c3896d2c 230 if (include_plid) {
c3896d2c 231 memcpy(pos, &plid, 2);
8db09850 232 pos += 2;
c3896d2c 233 }
54ef656b 234 if (action == WLAN_SP_MESH_PEERING_CLOSE) {
c3896d2c 235 memcpy(pos, &reason, 2);
8db09850 236 pos += 2;
c3896d2c 237 }
8db09850
TP
238 if (mesh_add_vendor_ies(skb, sdata))
239 return -1;
c3896d2c 240
62ae67be 241 ieee80211_tx_skb(sdata, skb);
c3896d2c
LCC
242 return 0;
243}
244
1570ca59
JC
245void mesh_neighbour_update(u8 *hw_addr, u32 rates,
246 struct ieee80211_sub_if_data *sdata,
247 struct ieee802_11_elems *elems)
c3896d2c 248{
f698d856 249 struct ieee80211_local *local = sdata->local;
c3896d2c
LCC
250 struct sta_info *sta;
251
d0709a65
JB
252 rcu_read_lock();
253
abe60632 254 sta = sta_info_get(sdata, hw_addr);
c3896d2c 255 if (!sta) {
34e89507 256 rcu_read_unlock();
1570ca59
JC
257 /* Userspace handles peer allocation when security is enabled
258 * */
b130e5ce 259 if (sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED)
1570ca59
JC
260 cfg80211_notify_new_peer_candidate(sdata->dev, hw_addr,
261 elems->ie_start, elems->total_len,
262 GFP_KERNEL);
263 else
264 sta = mesh_plink_alloc(sdata, hw_addr, rates);
34e89507 265 if (!sta)
73651ee6 266 return;
34e89507 267 if (sta_info_insert_rcu(sta)) {
d0709a65 268 rcu_read_unlock();
c3896d2c 269 return;
d0709a65 270 }
c3896d2c
LCC
271 }
272
273 sta->last_rx = jiffies;
323ce79a 274 sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
1570ca59 275 if (mesh_peer_accepts_plinks(elems) &&
57cf8043 276 sta->plink_state == NL80211_PLINK_LISTEN &&
472dbc45
JB
277 sdata->u.mesh.accepting_plinks &&
278 sdata->u.mesh.mshcfg.auto_open_plinks)
c3896d2c
LCC
279 mesh_plink_open(sta);
280
d0709a65 281 rcu_read_unlock();
c3896d2c
LCC
282}
283
284static void mesh_plink_timer(unsigned long data)
285{
286 struct sta_info *sta;
287 __le16 llid, plid, reason;
c3896d2c 288 struct ieee80211_sub_if_data *sdata;
c3896d2c 289
d0709a65
JB
290 /*
291 * This STA is valid because sta_info_destroy() will
292 * del_timer_sync() this timer after having made sure
293 * it cannot be readded (by deleting the plink.)
294 */
c3896d2c
LCC
295 sta = (struct sta_info *) data;
296
5bb644a0
JB
297 if (sta->sdata->local->quiescing) {
298 sta->plink_timer_was_running = true;
299 return;
300 }
301
07346f81 302 spin_lock_bh(&sta->lock);
c3896d2c
LCC
303 if (sta->ignore_plink_timer) {
304 sta->ignore_plink_timer = false;
07346f81 305 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
306 return;
307 }
0c68ae26
JB
308 mpl_dbg("Mesh plink timer for %pM fired on state %d\n",
309 sta->sta.addr, sta->plink_state);
c3896d2c
LCC
310 reason = 0;
311 llid = sta->llid;
312 plid = sta->plid;
d0709a65 313 sdata = sta->sdata;
c3896d2c
LCC
314
315 switch (sta->plink_state) {
57cf8043
JC
316 case NL80211_PLINK_OPN_RCVD:
317 case NL80211_PLINK_OPN_SNT:
c3896d2c
LCC
318 /* retry timer */
319 if (sta->plink_retries < dot11MeshMaxRetries(sdata)) {
320 u32 rand;
0c68ae26
JB
321 mpl_dbg("Mesh plink for %pM (retry, timeout): %d %d\n",
322 sta->sta.addr, sta->plink_retries,
323 sta->plink_timeout);
c3896d2c
LCC
324 get_random_bytes(&rand, sizeof(u32));
325 sta->plink_timeout = sta->plink_timeout +
326 rand % sta->plink_timeout;
327 ++sta->plink_retries;
d0709a65 328 mod_plink_timer(sta, sta->plink_timeout);
07346f81 329 spin_unlock_bh(&sta->lock);
54ef656b
TP
330 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
331 sta->sta.addr, llid, 0, 0);
c3896d2c
LCC
332 break;
333 }
54ef656b 334 reason = cpu_to_le16(WLAN_REASON_MESH_MAX_RETRIES);
c3896d2c 335 /* fall through on else */
57cf8043 336 case NL80211_PLINK_CNF_RCVD:
c3896d2c
LCC
337 /* confirm timer */
338 if (!reason)
54ef656b 339 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIRM_TIMEOUT);
57cf8043 340 sta->plink_state = NL80211_PLINK_HOLDING;
d0709a65 341 mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
07346f81 342 spin_unlock_bh(&sta->lock);
54ef656b
TP
343 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
344 sta->sta.addr, llid, plid, reason);
c3896d2c 345 break;
57cf8043 346 case NL80211_PLINK_HOLDING:
c3896d2c 347 /* holding timer */
d0709a65 348 del_timer(&sta->plink_timer);
c3896d2c 349 mesh_plink_fsm_restart(sta);
07346f81 350 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
351 break;
352 default:
07346f81 353 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
354 break;
355 }
c3896d2c
LCC
356}
357
5bb644a0
JB
358#ifdef CONFIG_PM
359void mesh_plink_quiesce(struct sta_info *sta)
360{
361 if (del_timer_sync(&sta->plink_timer))
362 sta->plink_timer_was_running = true;
363}
364
365void mesh_plink_restart(struct sta_info *sta)
366{
367 if (sta->plink_timer_was_running) {
368 add_timer(&sta->plink_timer);
369 sta->plink_timer_was_running = false;
370 }
371}
372#endif
373
c3896d2c
LCC
374static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout)
375{
376 sta->plink_timer.expires = jiffies + (HZ * timeout / 1000);
377 sta->plink_timer.data = (unsigned long) sta;
378 sta->plink_timer.function = mesh_plink_timer;
379 sta->plink_timeout = timeout;
c3896d2c
LCC
380 add_timer(&sta->plink_timer);
381}
382
383int mesh_plink_open(struct sta_info *sta)
384{
385 __le16 llid;
d0709a65 386 struct ieee80211_sub_if_data *sdata = sta->sdata;
c3896d2c 387
c2c98fde 388 if (!test_sta_flag(sta, WLAN_STA_AUTH))
53e80511
JC
389 return -EPERM;
390
07346f81 391 spin_lock_bh(&sta->lock);
c3896d2c
LCC
392 get_random_bytes(&llid, 2);
393 sta->llid = llid;
57cf8043 394 if (sta->plink_state != NL80211_PLINK_LISTEN) {
07346f81 395 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
396 return -EBUSY;
397 }
57cf8043 398 sta->plink_state = NL80211_PLINK_OPN_SNT;
c3896d2c 399 mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
07346f81 400 spin_unlock_bh(&sta->lock);
0c68ae26
JB
401 mpl_dbg("Mesh plink: starting establishment with %pM\n",
402 sta->sta.addr);
c3896d2c 403
54ef656b 404 return mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
17741cdc 405 sta->sta.addr, llid, 0, 0);
c3896d2c
LCC
406}
407
408void mesh_plink_block(struct sta_info *sta)
409{
c9370197
JL
410 struct ieee80211_sub_if_data *sdata = sta->sdata;
411 bool deactivated;
412
07346f81 413 spin_lock_bh(&sta->lock);
c9370197 414 deactivated = __mesh_plink_deactivate(sta);
57cf8043 415 sta->plink_state = NL80211_PLINK_BLOCKED;
07346f81 416 spin_unlock_bh(&sta->lock);
c9370197
JL
417
418 if (deactivated)
419 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
c3896d2c
LCC
420}
421
c3896d2c 422
f698d856 423void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata, struct ieee80211_mgmt *mgmt,
c3896d2c
LCC
424 size_t len, struct ieee80211_rx_status *rx_status)
425{
d0709a65 426 struct ieee80211_local *local = sdata->local;
c3896d2c
LCC
427 struct ieee802_11_elems elems;
428 struct sta_info *sta;
429 enum plink_event event;
54ef656b 430 enum ieee80211_self_protected_actioncode ftype;
c3896d2c 431 size_t baselen;
d12c7452 432 bool deactivated, matches_local = true;
c3896d2c
LCC
433 u8 ie_len;
434 u8 *baseaddr;
435 __le16 plid, llid, reason;
1460dd15
RP
436#ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
437 static const char *mplstates[] = {
57cf8043
JC
438 [NL80211_PLINK_LISTEN] = "LISTEN",
439 [NL80211_PLINK_OPN_SNT] = "OPN-SNT",
440 [NL80211_PLINK_OPN_RCVD] = "OPN-RCVD",
441 [NL80211_PLINK_CNF_RCVD] = "CNF_RCVD",
442 [NL80211_PLINK_ESTAB] = "ESTAB",
443 [NL80211_PLINK_HOLDING] = "HOLDING",
444 [NL80211_PLINK_BLOCKED] = "BLOCKED"
1460dd15
RP
445 };
446#endif
c3896d2c 447
9c80d3dc
JB
448 /* need action_code, aux */
449 if (len < IEEE80211_MIN_ACTION_SIZE + 3)
450 return;
451
c3896d2c
LCC
452 if (is_multicast_ether_addr(mgmt->da)) {
453 mpl_dbg("Mesh plink: ignore frame from multicast address");
454 return;
455 }
456
8db09850
TP
457 baseaddr = mgmt->u.action.u.self_prot.variable;
458 baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
459 if (mgmt->u.action.u.self_prot.action_code ==
54ef656b 460 WLAN_SP_MESH_PEERING_CONFIRM) {
c3896d2c 461 baseaddr += 4;
70bdb6b2 462 baselen += 4;
c3896d2c
LCC
463 }
464 ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
8db09850 465 if (!elems.peering) {
c3896d2c
LCC
466 mpl_dbg("Mesh plink: missing necessary peer link ie\n");
467 return;
468 }
b130e5ce
JC
469 if (elems.rsn_len &&
470 sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
5cff5e01
JC
471 mpl_dbg("Mesh plink: can't establish link with secure peer\n");
472 return;
473 }
c3896d2c 474
8db09850
TP
475 ftype = mgmt->u.action.u.self_prot.action_code;
476 ie_len = elems.peering_len;
477 if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
478 (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
479 (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
480 && ie_len != 8)) {
0938393f
RP
481 mpl_dbg("Mesh plink: incorrect plink ie length %d %d\n",
482 ftype, ie_len);
c3896d2c
LCC
483 return;
484 }
485
54ef656b
TP
486 if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
487 (!elems.mesh_id || !elems.mesh_config)) {
c3896d2c
LCC
488 mpl_dbg("Mesh plink: missing necessary ie\n");
489 return;
490 }
491 /* Note the lines below are correct, the llid in the frame is the plid
492 * from the point of view of this host.
493 */
8db09850 494 memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
54ef656b 495 if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
8db09850
TP
496 (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
497 memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
c3896d2c 498
d0709a65
JB
499 rcu_read_lock();
500
abe60632 501 sta = sta_info_get(sdata, mgmt->sa);
54ef656b 502 if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
c3896d2c 503 mpl_dbg("Mesh plink: cls or cnf from unknown peer\n");
d0709a65 504 rcu_read_unlock();
c3896d2c
LCC
505 return;
506 }
507
c2c98fde 508 if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
53e80511
JC
509 mpl_dbg("Mesh plink: Action frame from non-authed peer\n");
510 rcu_read_unlock();
511 return;
512 }
513
57cf8043 514 if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
d0709a65 515 rcu_read_unlock();
c3896d2c
LCC
516 return;
517 }
518
519 /* Now we will figure out the appropriate event... */
520 event = PLINK_UNDEFINED;
54ef656b
TP
521 if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
522 (!mesh_matches_local(&elems, sdata))) {
d12c7452 523 matches_local = false;
c3896d2c 524 switch (ftype) {
54ef656b 525 case WLAN_SP_MESH_PEERING_OPEN:
c3896d2c
LCC
526 event = OPN_RJCT;
527 break;
54ef656b 528 case WLAN_SP_MESH_PEERING_CONFIRM:
c3896d2c
LCC
529 event = CNF_RJCT;
530 break;
54ef656b 531 default:
c3896d2c
LCC
532 break;
533 }
d12c7452
CL
534 }
535
536 if (!sta && !matches_local) {
537 rcu_read_unlock();
54ef656b 538 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
d12c7452 539 llid = 0;
54ef656b
TP
540 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
541 mgmt->sa, llid, plid, reason);
d12c7452 542 return;
c3896d2c 543 } else if (!sta) {
54ef656b 544 /* ftype == WLAN_SP_MESH_PEERING_OPEN */
881d948c 545 u32 rates;
34e89507
JB
546
547 rcu_read_unlock();
548
c3896d2c
LCC
549 if (!mesh_plink_free_count(sdata)) {
550 mpl_dbg("Mesh plink error: no more free plinks\n");
551 return;
552 }
553
554 rates = ieee80211_sta_get_rates(local, &elems, rx_status->band);
03e4497e 555 sta = mesh_plink_alloc(sdata, mgmt->sa, rates);
73651ee6 556 if (!sta) {
c3896d2c
LCC
557 mpl_dbg("Mesh plink error: plink table full\n");
558 return;
559 }
34e89507 560 if (sta_info_insert_rcu(sta)) {
73651ee6
JB
561 rcu_read_unlock();
562 return;
563 }
c3896d2c 564 event = OPN_ACPT;
07346f81 565 spin_lock_bh(&sta->lock);
d12c7452 566 } else if (matches_local) {
07346f81 567 spin_lock_bh(&sta->lock);
c3896d2c 568 switch (ftype) {
54ef656b 569 case WLAN_SP_MESH_PEERING_OPEN:
c3896d2c 570 if (!mesh_plink_free_count(sdata) ||
d0709a65 571 (sta->plid && sta->plid != plid))
c3896d2c
LCC
572 event = OPN_IGNR;
573 else
574 event = OPN_ACPT;
575 break;
54ef656b 576 case WLAN_SP_MESH_PEERING_CONFIRM:
c3896d2c 577 if (!mesh_plink_free_count(sdata) ||
d0709a65 578 (sta->llid != llid || sta->plid != plid))
c3896d2c
LCC
579 event = CNF_IGNR;
580 else
581 event = CNF_ACPT;
582 break;
54ef656b 583 case WLAN_SP_MESH_PEERING_CLOSE:
57cf8043 584 if (sta->plink_state == NL80211_PLINK_ESTAB)
c3896d2c
LCC
585 /* Do not check for llid or plid. This does not
586 * follow the standard but since multiple plinks
587 * per sta are not supported, it is necessary in
588 * order to avoid a livelock when MP A sees an
589 * establish peer link to MP B but MP B does not
590 * see it. This can be caused by a timeout in
591 * B's peer link establishment or B beign
592 * restarted.
593 */
594 event = CLS_ACPT;
595 else if (sta->plid != plid)
596 event = CLS_IGNR;
597 else if (ie_len == 7 && sta->llid != llid)
598 event = CLS_IGNR;
599 else
600 event = CLS_ACPT;
601 break;
602 default:
603 mpl_dbg("Mesh plink: unknown frame subtype\n");
07346f81 604 spin_unlock_bh(&sta->lock);
d0709a65 605 rcu_read_unlock();
c3896d2c
LCC
606 return;
607 }
d12c7452
CL
608 } else {
609 spin_lock_bh(&sta->lock);
c3896d2c
LCC
610 }
611
1460dd15
RP
612 mpl_dbg("Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
613 mgmt->sa, mplstates[sta->plink_state],
0c68ae26
JB
614 le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
615 event);
c3896d2c
LCC
616 reason = 0;
617 switch (sta->plink_state) {
618 /* spin_unlock as soon as state is updated at each case */
57cf8043 619 case NL80211_PLINK_LISTEN:
c3896d2c
LCC
620 switch (event) {
621 case CLS_ACPT:
622 mesh_plink_fsm_restart(sta);
07346f81 623 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
624 break;
625 case OPN_ACPT:
57cf8043 626 sta->plink_state = NL80211_PLINK_OPN_RCVD;
c3896d2c
LCC
627 sta->plid = plid;
628 get_random_bytes(&llid, 2);
629 sta->llid = llid;
630 mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
07346f81 631 spin_unlock_bh(&sta->lock);
54ef656b
TP
632 mesh_plink_frame_tx(sdata,
633 WLAN_SP_MESH_PEERING_OPEN,
634 sta->sta.addr, llid, 0, 0);
635 mesh_plink_frame_tx(sdata,
636 WLAN_SP_MESH_PEERING_CONFIRM,
637 sta->sta.addr, llid, plid, 0);
c3896d2c
LCC
638 break;
639 default:
07346f81 640 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
641 break;
642 }
643 break;
644
57cf8043 645 case NL80211_PLINK_OPN_SNT:
c3896d2c
LCC
646 switch (event) {
647 case OPN_RJCT:
648 case CNF_RJCT:
54ef656b 649 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
c3896d2c
LCC
650 case CLS_ACPT:
651 if (!reason)
54ef656b 652 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
c3896d2c 653 sta->reason = reason;
57cf8043 654 sta->plink_state = NL80211_PLINK_HOLDING;
c3896d2c
LCC
655 if (!mod_plink_timer(sta,
656 dot11MeshHoldingTimeout(sdata)))
657 sta->ignore_plink_timer = true;
658
659 llid = sta->llid;
07346f81 660 spin_unlock_bh(&sta->lock);
54ef656b
TP
661 mesh_plink_frame_tx(sdata,
662 WLAN_SP_MESH_PEERING_CLOSE,
663 sta->sta.addr, llid, plid, reason);
c3896d2c
LCC
664 break;
665 case OPN_ACPT:
666 /* retry timer is left untouched */
57cf8043 667 sta->plink_state = NL80211_PLINK_OPN_RCVD;
c3896d2c
LCC
668 sta->plid = plid;
669 llid = sta->llid;
07346f81 670 spin_unlock_bh(&sta->lock);
54ef656b
TP
671 mesh_plink_frame_tx(sdata,
672 WLAN_SP_MESH_PEERING_CONFIRM,
673 sta->sta.addr, llid, plid, 0);
c3896d2c
LCC
674 break;
675 case CNF_ACPT:
57cf8043 676 sta->plink_state = NL80211_PLINK_CNF_RCVD;
c3896d2c
LCC
677 if (!mod_plink_timer(sta,
678 dot11MeshConfirmTimeout(sdata)))
679 sta->ignore_plink_timer = true;
680
07346f81 681 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
682 break;
683 default:
07346f81 684 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
685 break;
686 }
687 break;
688
57cf8043 689 case NL80211_PLINK_OPN_RCVD:
c3896d2c
LCC
690 switch (event) {
691 case OPN_RJCT:
692 case CNF_RJCT:
54ef656b 693 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
c3896d2c
LCC
694 case CLS_ACPT:
695 if (!reason)
54ef656b 696 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
c3896d2c 697 sta->reason = reason;
57cf8043 698 sta->plink_state = NL80211_PLINK_HOLDING;
c3896d2c
LCC
699 if (!mod_plink_timer(sta,
700 dot11MeshHoldingTimeout(sdata)))
701 sta->ignore_plink_timer = true;
702
703 llid = sta->llid;
07346f81 704 spin_unlock_bh(&sta->lock);
54ef656b
TP
705 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
706 sta->sta.addr, llid, plid, reason);
c3896d2c
LCC
707 break;
708 case OPN_ACPT:
709 llid = sta->llid;
07346f81 710 spin_unlock_bh(&sta->lock);
54ef656b
TP
711 mesh_plink_frame_tx(sdata,
712 WLAN_SP_MESH_PEERING_CONFIRM,
713 sta->sta.addr, llid, plid, 0);
c3896d2c
LCC
714 break;
715 case CNF_ACPT:
d0709a65 716 del_timer(&sta->plink_timer);
57cf8043 717 sta->plink_state = NL80211_PLINK_ESTAB;
07346f81 718 spin_unlock_bh(&sta->lock);
c9370197
JL
719 mesh_plink_inc_estab_count(sdata);
720 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
0c68ae26
JB
721 mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
722 sta->sta.addr);
c3896d2c
LCC
723 break;
724 default:
07346f81 725 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
726 break;
727 }
728 break;
729
57cf8043 730 case NL80211_PLINK_CNF_RCVD:
c3896d2c
LCC
731 switch (event) {
732 case OPN_RJCT:
733 case CNF_RJCT:
54ef656b 734 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
c3896d2c
LCC
735 case CLS_ACPT:
736 if (!reason)
54ef656b 737 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
c3896d2c 738 sta->reason = reason;
57cf8043 739 sta->plink_state = NL80211_PLINK_HOLDING;
c3896d2c
LCC
740 if (!mod_plink_timer(sta,
741 dot11MeshHoldingTimeout(sdata)))
742 sta->ignore_plink_timer = true;
743
744 llid = sta->llid;
07346f81 745 spin_unlock_bh(&sta->lock);
54ef656b
TP
746 mesh_plink_frame_tx(sdata,
747 WLAN_SP_MESH_PEERING_CLOSE,
748 sta->sta.addr, llid, plid, reason);
ff59dc76 749 break;
c3896d2c 750 case OPN_ACPT:
d0709a65 751 del_timer(&sta->plink_timer);
57cf8043 752 sta->plink_state = NL80211_PLINK_ESTAB;
07346f81 753 spin_unlock_bh(&sta->lock);
c9370197
JL
754 mesh_plink_inc_estab_count(sdata);
755 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
0c68ae26
JB
756 mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
757 sta->sta.addr);
54ef656b
TP
758 mesh_plink_frame_tx(sdata,
759 WLAN_SP_MESH_PEERING_CONFIRM,
760 sta->sta.addr, llid, plid, 0);
c3896d2c
LCC
761 break;
762 default:
07346f81 763 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
764 break;
765 }
766 break;
767
57cf8043 768 case NL80211_PLINK_ESTAB:
c3896d2c
LCC
769 switch (event) {
770 case CLS_ACPT:
54ef656b 771 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
c3896d2c 772 sta->reason = reason;
c9370197 773 deactivated = __mesh_plink_deactivate(sta);
57cf8043 774 sta->plink_state = NL80211_PLINK_HOLDING;
c3896d2c 775 llid = sta->llid;
d0709a65 776 mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
07346f81 777 spin_unlock_bh(&sta->lock);
c9370197
JL
778 if (deactivated)
779 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
54ef656b
TP
780 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
781 sta->sta.addr, llid, plid, reason);
c3896d2c
LCC
782 break;
783 case OPN_ACPT:
784 llid = sta->llid;
07346f81 785 spin_unlock_bh(&sta->lock);
54ef656b
TP
786 mesh_plink_frame_tx(sdata,
787 WLAN_SP_MESH_PEERING_CONFIRM,
788 sta->sta.addr, llid, plid, 0);
c3896d2c
LCC
789 break;
790 default:
07346f81 791 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
792 break;
793 }
794 break;
57cf8043 795 case NL80211_PLINK_HOLDING:
c3896d2c
LCC
796 switch (event) {
797 case CLS_ACPT:
d0709a65 798 if (del_timer(&sta->plink_timer))
c3896d2c 799 sta->ignore_plink_timer = 1;
c3896d2c 800 mesh_plink_fsm_restart(sta);
07346f81 801 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
802 break;
803 case OPN_ACPT:
804 case CNF_ACPT:
805 case OPN_RJCT:
806 case CNF_RJCT:
807 llid = sta->llid;
808 reason = sta->reason;
07346f81 809 spin_unlock_bh(&sta->lock);
54ef656b
TP
810 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
811 sta->sta.addr, llid, plid, reason);
c3896d2c
LCC
812 break;
813 default:
07346f81 814 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
815 }
816 break;
817 default:
b4e08ea1 818 /* should not get here, PLINK_BLOCKED is dealt with at the
3ad2f3fb 819 * beginning of the function
c3896d2c 820 */
07346f81 821 spin_unlock_bh(&sta->lock);
c3896d2c
LCC
822 break;
823 }
d0709a65
JB
824
825 rcu_read_unlock();
c3896d2c 826}