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