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