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