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