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