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