mesh_plink.c 21.5 KB
Newer Older
1
/*
R
Rui Paulo 已提交
2
 * Copyright (c) 2008, 2009 open80211s Ltd.
3 4 5 6 7 8
 * Author:     Luis Carlos Cobo <luisca@cozybit.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
9
#include <linux/gfp.h>
J
Johannes Berg 已提交
10 11
#include <linux/kernel.h>
#include <linux/random.h>
12
#include "ieee80211_i.h"
J
Johannes Berg 已提交
13
#include "rate.h"
14 15 16 17 18 19 20 21
#include "mesh.h"

#ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
#define mpl_dbg(fmt, args...)	printk(KERN_DEBUG fmt, ##args)
#else
#define mpl_dbg(fmt, args...)	do { (void)(0); } while (0)
#endif

22 23
#define PLINK_GET_LLID(p) (p + 2)
#define PLINK_GET_PLID(p) (p + 4)
24 25 26 27

#define mod_plink_timer(s, t) (mod_timer(&s->plink_timer, \
				jiffies + HZ * t / 1000))

28 29 30 31 32
#define dot11MeshMaxRetries(s) (s->u.mesh.mshcfg.dot11MeshMaxRetries)
#define dot11MeshRetryTimeout(s) (s->u.mesh.mshcfg.dot11MeshRetryTimeout)
#define dot11MeshConfirmTimeout(s) (s->u.mesh.mshcfg.dot11MeshConfirmTimeout)
#define dot11MeshHoldingTimeout(s) (s->u.mesh.mshcfg.dot11MeshHoldingTimeout)
#define dot11MeshMaxPeerLinks(s) (s->u.mesh.mshcfg.dot11MeshMaxPeerLinks)
33 34 35 36 37 38 39 40 41 42 43 44 45

enum plink_event {
	PLINK_UNDEFINED,
	OPN_ACPT,
	OPN_RJCT,
	OPN_IGNR,
	CNF_ACPT,
	CNF_RJCT,
	CNF_IGNR,
	CLS_ACPT,
	CLS_IGNR
};

46 47 48 49
static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
		enum ieee80211_self_protected_actioncode action,
		u8 *da, __le16 llid, __le16 plid, __le16 reason);

50 51 52
static inline
void mesh_plink_inc_estab_count(struct ieee80211_sub_if_data *sdata)
{
53
	atomic_inc(&sdata->u.mesh.mshstats.estab_plinks);
54
	mesh_accept_plinks_update(sdata);
55 56 57 58 59
}

static inline
void mesh_plink_dec_estab_count(struct ieee80211_sub_if_data *sdata)
{
60
	atomic_dec(&sdata->u.mesh.mshstats.estab_plinks);
61
	mesh_accept_plinks_update(sdata);
62 63 64 65 66
}

/**
 * mesh_plink_fsm_restart - restart a mesh peer link finite state machine
 *
R
Rui Paulo 已提交
67
 * @sta: mesh peer link to restart
68
 *
69
 * Locking: this function must be called holding sta->lock
70 71 72
 */
static inline void mesh_plink_fsm_restart(struct sta_info *sta)
{
73
	sta->plink_state = NL80211_PLINK_LISTEN;
74 75
	sta->llid = sta->plid = sta->reason = 0;
	sta->plink_retries = 0;
76 77
}

78 79 80 81
/*
 * NOTE: This is just an alias for sta_info_alloc(), see notes
 *       on it in the lifecycle management section!
 */
82
static struct sta_info *mesh_plink_alloc(struct ieee80211_sub_if_data *sdata,
83
					 u8 *hw_addr, u32 rates)
84
{
85
	struct ieee80211_local *local = sdata->local;
86 87 88
	struct sta_info *sta;

	if (local->num_sta >= MESH_MAX_PLINKS)
J
Johannes Berg 已提交
89
		return NULL;
90

91
	sta = sta_info_alloc(sdata, hw_addr, GFP_KERNEL);
J
Johannes Berg 已提交
92 93
	if (!sta)
		return NULL;
94

95
	sta->flags = WLAN_STA_AUTHORIZED | WLAN_STA_AUTH | WLAN_STA_WME;
J
Johannes Berg 已提交
96
	sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
97
	rate_control_rate_init(sta);
98 99 100 101 102

	return sta;
}

/**
103
 * __mesh_plink_deactivate - deactivate mesh peer link
104 105 106 107 108
 *
 * @sta: mesh peer link to deactivate
 *
 * All mesh paths with this peer as next hop will be flushed
 *
109
 * Locking: the caller must hold sta->lock
110
 */
111
static bool __mesh_plink_deactivate(struct sta_info *sta)
112
{
113
	struct ieee80211_sub_if_data *sdata = sta->sdata;
114
	bool deactivated = false;
115

116
	if (sta->plink_state == NL80211_PLINK_ESTAB) {
117
		mesh_plink_dec_estab_count(sdata);
118 119
		deactivated = true;
	}
120
	sta->plink_state = NL80211_PLINK_BLOCKED;
121
	mesh_path_flush_by_nexthop(sta);
122 123

	return deactivated;
124 125
}

J
Johannes Berg 已提交
126
/**
127
 * mesh_plink_deactivate - deactivate mesh peer link
J
Johannes Berg 已提交
128 129 130 131 132 133 134
 *
 * @sta: mesh peer link to deactivate
 *
 * All mesh paths with this peer as next hop will be flushed
 */
void mesh_plink_deactivate(struct sta_info *sta)
{
135 136 137
	struct ieee80211_sub_if_data *sdata = sta->sdata;
	bool deactivated;

138
	spin_lock_bh(&sta->lock);
139
	deactivated = __mesh_plink_deactivate(sta);
140 141 142 143
	sta->reason = cpu_to_le16(WLAN_REASON_MESH_PEER_CANCELED);
	mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
			    sta->sta.addr, sta->llid, sta->plid,
			    sta->reason);
144
	spin_unlock_bh(&sta->lock);
145 146 147

	if (deactivated)
		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
J
Johannes Berg 已提交
148 149
}

150
static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
151 152
		enum ieee80211_self_protected_actioncode action,
		u8 *da, __le16 llid, __le16 plid, __le16 reason) {
153
	struct ieee80211_local *local = sdata->local;
154
	struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400 +
155
			sdata->u.mesh.ie_len);
156 157
	struct ieee80211_mgmt *mgmt;
	bool include_plid = false;
158 159
	int ie_len = 4;
	u16 peering_proto = 0;
160 161 162 163 164 165 166 167 168
	u8 *pos;

	if (!skb)
		return -1;
	skb_reserve(skb, local->hw.extra_tx_headroom);
	/* 25 is the size of the common mgmt part (24) plus the size of the
	 * common action part (1)
	 */
	mgmt = (struct ieee80211_mgmt *)
169 170
		skb_put(skb, 25 + sizeof(mgmt->u.action.u.self_prot));
	memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.self_prot));
171 172
	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
					  IEEE80211_STYPE_ACTION);
173
	memcpy(mgmt->da, da, ETH_ALEN);
174
	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
175
	memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
176 177
	mgmt->u.action.category = WLAN_CATEGORY_SELF_PROTECTED;
	mgmt->u.action.u.self_prot.action_code = action;
178

179 180 181 182
	if (action != WLAN_SP_MESH_PEERING_CLOSE) {
		/* capability info */
		pos = skb_put(skb, 2);
		memset(pos, 0, 2);
183
		if (action == WLAN_SP_MESH_PEERING_CONFIRM) {
184 185
			/* AID */
			pos = skb_put(skb, 2);
186
			memcpy(pos + 2, &plid, 2);
187
		}
188 189
		if (ieee80211_add_srates_ie(&sdata->vif, skb) ||
		    ieee80211_add_ext_srates_ie(&sdata->vif, skb) ||
190 191 192 193
		    mesh_add_rsn_ie(skb, sdata) ||
		    mesh_add_meshid_ie(skb, sdata) ||
		    mesh_add_meshconf_ie(skb, sdata))
			return -1;
194 195 196
	} else {	/* WLAN_SP_MESH_PEERING_CLOSE */
		if (mesh_add_meshid_ie(skb, sdata))
			return -1;
197 198
	}

199
	/* Add Mesh Peering Management element */
200
	switch (action) {
201
	case WLAN_SP_MESH_PEERING_OPEN:
202
		break;
203
	case WLAN_SP_MESH_PEERING_CONFIRM:
204
		ie_len += 2;
205 206
		include_plid = true;
		break;
207
	case WLAN_SP_MESH_PEERING_CLOSE:
208 209
		if (plid) {
			ie_len += 2;
210 211
			include_plid = true;
		}
212
		ie_len += 2;	/* reason code */
213
		break;
214 215
	default:
		return -EINVAL;
216 217
	}

218 219 220
	if (WARN_ON(skb_tailroom(skb) < 2 + ie_len))
		return -ENOMEM;

221
	pos = skb_put(skb, 2 + ie_len);
222
	*pos++ = WLAN_EID_PEER_MGMT;
223
	*pos++ = ie_len;
224 225
	memcpy(pos, &peering_proto, 2);
	pos += 2;
226
	memcpy(pos, &llid, 2);
227
	pos += 2;
228 229
	if (include_plid) {
		memcpy(pos, &plid, 2);
230
		pos += 2;
231
	}
232
	if (action == WLAN_SP_MESH_PEERING_CLOSE) {
233
		memcpy(pos, &reason, 2);
234
		pos += 2;
235
	}
236 237
	if (mesh_add_vendor_ies(skb, sdata))
		return -1;
238

239
	ieee80211_tx_skb(sdata, skb);
240 241 242
	return 0;
}

243 244 245
void mesh_neighbour_update(u8 *hw_addr, u32 rates,
		struct ieee80211_sub_if_data *sdata,
		struct ieee802_11_elems *elems)
246
{
247
	struct ieee80211_local *local = sdata->local;
248 249
	struct sta_info *sta;

250 251
	rcu_read_lock();

252
	sta = sta_info_get(sdata, hw_addr);
253
	if (!sta) {
254
		rcu_read_unlock();
255 256
		/* Userspace handles peer allocation when security is enabled
		 * */
257
		if (sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED)
258 259 260 261 262
			cfg80211_notify_new_peer_candidate(sdata->dev, hw_addr,
					elems->ie_start, elems->total_len,
					GFP_KERNEL);
		else
			sta = mesh_plink_alloc(sdata, hw_addr, rates);
263
		if (!sta)
J
Johannes Berg 已提交
264
			return;
265
		if (sta_info_insert_rcu(sta)) {
266
			rcu_read_unlock();
267
			return;
268
		}
269 270 271
	}

	sta->last_rx = jiffies;
J
Johannes Berg 已提交
272
	sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
273
	if (mesh_peer_accepts_plinks(elems) &&
274
			sta->plink_state == NL80211_PLINK_LISTEN &&
275 276
			sdata->u.mesh.accepting_plinks &&
			sdata->u.mesh.mshcfg.auto_open_plinks)
277 278
		mesh_plink_open(sta);

279
	rcu_read_unlock();
280 281 282 283 284 285 286 287
}

static void mesh_plink_timer(unsigned long data)
{
	struct sta_info *sta;
	__le16 llid, plid, reason;
	struct ieee80211_sub_if_data *sdata;

288 289 290 291 292
	/*
	 * This STA is valid because sta_info_destroy() will
	 * del_timer_sync() this timer after having made sure
	 * it cannot be readded (by deleting the plink.)
	 */
293 294
	sta = (struct sta_info *) data;

295 296 297 298 299
	if (sta->sdata->local->quiescing) {
		sta->plink_timer_was_running = true;
		return;
	}

300
	spin_lock_bh(&sta->lock);
301 302
	if (sta->ignore_plink_timer) {
		sta->ignore_plink_timer = false;
303
		spin_unlock_bh(&sta->lock);
304 305
		return;
	}
306 307
	mpl_dbg("Mesh plink timer for %pM fired on state %d\n",
		sta->sta.addr, sta->plink_state);
308 309 310
	reason = 0;
	llid = sta->llid;
	plid = sta->plid;
311
	sdata = sta->sdata;
312 313

	switch (sta->plink_state) {
314 315
	case NL80211_PLINK_OPN_RCVD:
	case NL80211_PLINK_OPN_SNT:
316 317 318
		/* retry timer */
		if (sta->plink_retries < dot11MeshMaxRetries(sdata)) {
			u32 rand;
319 320 321
			mpl_dbg("Mesh plink for %pM (retry, timeout): %d %d\n",
				sta->sta.addr, sta->plink_retries,
				sta->plink_timeout);
322 323 324 325
			get_random_bytes(&rand, sizeof(u32));
			sta->plink_timeout = sta->plink_timeout +
					     rand % sta->plink_timeout;
			++sta->plink_retries;
326
			mod_plink_timer(sta, sta->plink_timeout);
327
			spin_unlock_bh(&sta->lock);
328 329
			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
					    sta->sta.addr, llid, 0, 0);
330 331
			break;
		}
332
		reason = cpu_to_le16(WLAN_REASON_MESH_MAX_RETRIES);
333
		/* fall through on else */
334
	case NL80211_PLINK_CNF_RCVD:
335 336
		/* confirm timer */
		if (!reason)
337
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIRM_TIMEOUT);
338
		sta->plink_state = NL80211_PLINK_HOLDING;
339
		mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
340
		spin_unlock_bh(&sta->lock);
341 342
		mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
				    sta->sta.addr, llid, plid, reason);
343
		break;
344
	case NL80211_PLINK_HOLDING:
345
		/* holding timer */
346
		del_timer(&sta->plink_timer);
347
		mesh_plink_fsm_restart(sta);
348
		spin_unlock_bh(&sta->lock);
349 350
		break;
	default:
351
		spin_unlock_bh(&sta->lock);
352 353 354 355
		break;
	}
}

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
#ifdef CONFIG_PM
void mesh_plink_quiesce(struct sta_info *sta)
{
	if (del_timer_sync(&sta->plink_timer))
		sta->plink_timer_was_running = true;
}

void mesh_plink_restart(struct sta_info *sta)
{
	if (sta->plink_timer_was_running) {
		add_timer(&sta->plink_timer);
		sta->plink_timer_was_running = false;
	}
}
#endif

372 373 374 375 376 377 378 379 380 381 382 383
static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout)
{
	sta->plink_timer.expires = jiffies + (HZ * timeout / 1000);
	sta->plink_timer.data = (unsigned long) sta;
	sta->plink_timer.function = mesh_plink_timer;
	sta->plink_timeout = timeout;
	add_timer(&sta->plink_timer);
}

int mesh_plink_open(struct sta_info *sta)
{
	__le16 llid;
384
	struct ieee80211_sub_if_data *sdata = sta->sdata;
385

386 387 388
	if (!test_sta_flags(sta, WLAN_STA_AUTH))
		return -EPERM;

389
	spin_lock_bh(&sta->lock);
390 391
	get_random_bytes(&llid, 2);
	sta->llid = llid;
392
	if (sta->plink_state != NL80211_PLINK_LISTEN) {
393
		spin_unlock_bh(&sta->lock);
394 395
		return -EBUSY;
	}
396
	sta->plink_state = NL80211_PLINK_OPN_SNT;
397
	mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
398
	spin_unlock_bh(&sta->lock);
399 400
	mpl_dbg("Mesh plink: starting establishment with %pM\n",
		sta->sta.addr);
401

402
	return mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
403
				   sta->sta.addr, llid, 0, 0);
404 405 406 407
}

void mesh_plink_block(struct sta_info *sta)
{
408 409 410
	struct ieee80211_sub_if_data *sdata = sta->sdata;
	bool deactivated;

411
	spin_lock_bh(&sta->lock);
412
	deactivated = __mesh_plink_deactivate(sta);
413
	sta->plink_state = NL80211_PLINK_BLOCKED;
414
	spin_unlock_bh(&sta->lock);
415 416 417

	if (deactivated)
		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
418 419 420
}


421
void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata, struct ieee80211_mgmt *mgmt,
422 423
			 size_t len, struct ieee80211_rx_status *rx_status)
{
424
	struct ieee80211_local *local = sdata->local;
425 426 427
	struct ieee802_11_elems elems;
	struct sta_info *sta;
	enum plink_event event;
428
	enum ieee80211_self_protected_actioncode ftype;
429
	size_t baselen;
430
	bool deactivated, matches_local = true;
431 432 433
	u8 ie_len;
	u8 *baseaddr;
	__le16 plid, llid, reason;
434 435
#ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
	static const char *mplstates[] = {
436 437 438 439 440 441 442
		[NL80211_PLINK_LISTEN] = "LISTEN",
		[NL80211_PLINK_OPN_SNT] = "OPN-SNT",
		[NL80211_PLINK_OPN_RCVD] = "OPN-RCVD",
		[NL80211_PLINK_CNF_RCVD] = "CNF_RCVD",
		[NL80211_PLINK_ESTAB] = "ESTAB",
		[NL80211_PLINK_HOLDING] = "HOLDING",
		[NL80211_PLINK_BLOCKED] = "BLOCKED"
443 444
	};
#endif
445

446 447 448 449
	/* need action_code, aux */
	if (len < IEEE80211_MIN_ACTION_SIZE + 3)
		return;

450 451 452 453 454
	if (is_multicast_ether_addr(mgmt->da)) {
		mpl_dbg("Mesh plink: ignore frame from multicast address");
		return;
	}

455 456 457
	baseaddr = mgmt->u.action.u.self_prot.variable;
	baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
	if (mgmt->u.action.u.self_prot.action_code ==
458
						WLAN_SP_MESH_PEERING_CONFIRM) {
459
		baseaddr += 4;
460
		baselen += 4;
461 462
	}
	ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
463
	if (!elems.peering) {
464 465 466
		mpl_dbg("Mesh plink: missing necessary peer link ie\n");
		return;
	}
467 468
	if (elems.rsn_len &&
			sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
469 470 471
		mpl_dbg("Mesh plink: can't establish link with secure peer\n");
		return;
	}
472

473 474 475 476 477 478
	ftype = mgmt->u.action.u.self_prot.action_code;
	ie_len = elems.peering_len;
	if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
	    (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
	    (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
							&& ie_len != 8)) {
479 480
		mpl_dbg("Mesh plink: incorrect plink ie length %d %d\n",
		    ftype, ie_len);
481 482 483
		return;
	}

484 485
	if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
				(!elems.mesh_id || !elems.mesh_config)) {
486 487 488 489 490 491
		mpl_dbg("Mesh plink: missing necessary ie\n");
		return;
	}
	/* Note the lines below are correct, the llid in the frame is the plid
	 * from the point of view of this host.
	 */
492
	memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
493
	if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
494 495
	    (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
		memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
496

497 498
	rcu_read_lock();

499
	sta = sta_info_get(sdata, mgmt->sa);
500
	if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
501
		mpl_dbg("Mesh plink: cls or cnf from unknown peer\n");
502
		rcu_read_unlock();
503 504 505
		return;
	}

506 507 508 509 510 511
	if (sta && !test_sta_flags(sta, WLAN_STA_AUTH)) {
		mpl_dbg("Mesh plink: Action frame from non-authed peer\n");
		rcu_read_unlock();
		return;
	}

512
	if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
513
		rcu_read_unlock();
514 515 516 517 518
		return;
	}

	/* Now we will figure out the appropriate event... */
	event = PLINK_UNDEFINED;
519 520
	if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
	    (!mesh_matches_local(&elems, sdata))) {
521
		matches_local = false;
522
		switch (ftype) {
523
		case WLAN_SP_MESH_PEERING_OPEN:
524 525
			event = OPN_RJCT;
			break;
526
		case WLAN_SP_MESH_PEERING_CONFIRM:
527 528
			event = CNF_RJCT;
			break;
529
		default:
530 531
			break;
		}
532 533 534 535
	}

	if (!sta && !matches_local) {
		rcu_read_unlock();
536
		reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
537
		llid = 0;
538 539
		mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
				    mgmt->sa, llid, plid, reason);
540
		return;
541
	} else if (!sta) {
542
		/* ftype == WLAN_SP_MESH_PEERING_OPEN */
543
		u32 rates;
544 545 546

		rcu_read_unlock();

547 548 549 550 551 552
		if (!mesh_plink_free_count(sdata)) {
			mpl_dbg("Mesh plink error: no more free plinks\n");
			return;
		}

		rates = ieee80211_sta_get_rates(local, &elems, rx_status->band);
553
		sta = mesh_plink_alloc(sdata, mgmt->sa, rates);
J
Johannes Berg 已提交
554
		if (!sta) {
555 556 557
			mpl_dbg("Mesh plink error: plink table full\n");
			return;
		}
558
		if (sta_info_insert_rcu(sta)) {
J
Johannes Berg 已提交
559 560 561
			rcu_read_unlock();
			return;
		}
562
		event = OPN_ACPT;
563
		spin_lock_bh(&sta->lock);
564
	} else if (matches_local) {
565
		spin_lock_bh(&sta->lock);
566
		switch (ftype) {
567
		case WLAN_SP_MESH_PEERING_OPEN:
568
			if (!mesh_plink_free_count(sdata) ||
569
			    (sta->plid && sta->plid != plid))
570 571 572 573
				event = OPN_IGNR;
			else
				event = OPN_ACPT;
			break;
574
		case WLAN_SP_MESH_PEERING_CONFIRM:
575
			if (!mesh_plink_free_count(sdata) ||
576
			    (sta->llid != llid || sta->plid != plid))
577 578 579 580
				event = CNF_IGNR;
			else
				event = CNF_ACPT;
			break;
581
		case WLAN_SP_MESH_PEERING_CLOSE:
582
			if (sta->plink_state == NL80211_PLINK_ESTAB)
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
				/* Do not check for llid or plid. This does not
				 * follow the standard but since multiple plinks
				 * per sta are not supported, it is necessary in
				 * order to avoid a livelock when MP A sees an
				 * establish peer link to MP B but MP B does not
				 * see it. This can be caused by a timeout in
				 * B's peer link establishment or B beign
				 * restarted.
				 */
				event = CLS_ACPT;
			else if (sta->plid != plid)
				event = CLS_IGNR;
			else if (ie_len == 7 && sta->llid != llid)
				event = CLS_IGNR;
			else
				event = CLS_ACPT;
			break;
		default:
			mpl_dbg("Mesh plink: unknown frame subtype\n");
602
			spin_unlock_bh(&sta->lock);
603
			rcu_read_unlock();
604 605
			return;
		}
606 607
	} else {
		spin_lock_bh(&sta->lock);
608 609
	}

610 611
	mpl_dbg("Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
		mgmt->sa, mplstates[sta->plink_state],
612 613
		le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
		event);
614 615 616
	reason = 0;
	switch (sta->plink_state) {
		/* spin_unlock as soon as state is updated at each case */
617
	case NL80211_PLINK_LISTEN:
618 619 620
		switch (event) {
		case CLS_ACPT:
			mesh_plink_fsm_restart(sta);
621
			spin_unlock_bh(&sta->lock);
622 623
			break;
		case OPN_ACPT:
624
			sta->plink_state = NL80211_PLINK_OPN_RCVD;
625 626 627 628
			sta->plid = plid;
			get_random_bytes(&llid, 2);
			sta->llid = llid;
			mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
629
			spin_unlock_bh(&sta->lock);
630 631 632 633 634 635
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_OPEN,
					    sta->sta.addr, llid, 0, 0);
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CONFIRM,
					    sta->sta.addr, llid, plid, 0);
636 637
			break;
		default:
638
			spin_unlock_bh(&sta->lock);
639 640 641 642
			break;
		}
		break;

643
	case NL80211_PLINK_OPN_SNT:
644 645 646
		switch (event) {
		case OPN_RJCT:
		case CNF_RJCT:
647
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
648 649
		case CLS_ACPT:
			if (!reason)
650
				reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
651
			sta->reason = reason;
652
			sta->plink_state = NL80211_PLINK_HOLDING;
653 654 655 656 657
			if (!mod_plink_timer(sta,
					     dot11MeshHoldingTimeout(sdata)))
				sta->ignore_plink_timer = true;

			llid = sta->llid;
658
			spin_unlock_bh(&sta->lock);
659 660 661
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
662 663 664
			break;
		case OPN_ACPT:
			/* retry timer is left untouched */
665
			sta->plink_state = NL80211_PLINK_OPN_RCVD;
666 667
			sta->plid = plid;
			llid = sta->llid;
668
			spin_unlock_bh(&sta->lock);
669 670 671
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CONFIRM,
					    sta->sta.addr, llid, plid, 0);
672 673
			break;
		case CNF_ACPT:
674
			sta->plink_state = NL80211_PLINK_CNF_RCVD;
675 676 677 678
			if (!mod_plink_timer(sta,
					     dot11MeshConfirmTimeout(sdata)))
				sta->ignore_plink_timer = true;

679
			spin_unlock_bh(&sta->lock);
680 681
			break;
		default:
682
			spin_unlock_bh(&sta->lock);
683 684 685 686
			break;
		}
		break;

687
	case NL80211_PLINK_OPN_RCVD:
688 689 690
		switch (event) {
		case OPN_RJCT:
		case CNF_RJCT:
691
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
692 693
		case CLS_ACPT:
			if (!reason)
694
				reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
695
			sta->reason = reason;
696
			sta->plink_state = NL80211_PLINK_HOLDING;
697 698 699 700 701
			if (!mod_plink_timer(sta,
					     dot11MeshHoldingTimeout(sdata)))
				sta->ignore_plink_timer = true;

			llid = sta->llid;
702
			spin_unlock_bh(&sta->lock);
703 704
			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
705 706 707
			break;
		case OPN_ACPT:
			llid = sta->llid;
708
			spin_unlock_bh(&sta->lock);
709 710 711
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CONFIRM,
					    sta->sta.addr, llid, plid, 0);
712 713
			break;
		case CNF_ACPT:
714
			del_timer(&sta->plink_timer);
715
			sta->plink_state = NL80211_PLINK_ESTAB;
716
			spin_unlock_bh(&sta->lock);
717 718
			mesh_plink_inc_estab_count(sdata);
			ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
719 720
			mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
				sta->sta.addr);
721 722
			break;
		default:
723
			spin_unlock_bh(&sta->lock);
724 725 726 727
			break;
		}
		break;

728
	case NL80211_PLINK_CNF_RCVD:
729 730 731
		switch (event) {
		case OPN_RJCT:
		case CNF_RJCT:
732
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
733 734
		case CLS_ACPT:
			if (!reason)
735
				reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
736
			sta->reason = reason;
737
			sta->plink_state = NL80211_PLINK_HOLDING;
738 739 740 741 742
			if (!mod_plink_timer(sta,
					     dot11MeshHoldingTimeout(sdata)))
				sta->ignore_plink_timer = true;

			llid = sta->llid;
743
			spin_unlock_bh(&sta->lock);
744 745 746
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
747
			break;
748
		case OPN_ACPT:
749
			del_timer(&sta->plink_timer);
750
			sta->plink_state = NL80211_PLINK_ESTAB;
751
			spin_unlock_bh(&sta->lock);
752 753
			mesh_plink_inc_estab_count(sdata);
			ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
754 755
			mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
				sta->sta.addr);
756 757 758
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CONFIRM,
					    sta->sta.addr, llid, plid, 0);
759 760
			break;
		default:
761
			spin_unlock_bh(&sta->lock);
762 763 764 765
			break;
		}
		break;

766
	case NL80211_PLINK_ESTAB:
767 768
		switch (event) {
		case CLS_ACPT:
769
			reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
770
			sta->reason = reason;
771
			deactivated = __mesh_plink_deactivate(sta);
772
			sta->plink_state = NL80211_PLINK_HOLDING;
773
			llid = sta->llid;
774
			mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
775
			spin_unlock_bh(&sta->lock);
776 777
			if (deactivated)
				ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
778 779
			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
780 781 782
			break;
		case OPN_ACPT:
			llid = sta->llid;
783
			spin_unlock_bh(&sta->lock);
784 785 786
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CONFIRM,
					    sta->sta.addr, llid, plid, 0);
787 788
			break;
		default:
789
			spin_unlock_bh(&sta->lock);
790 791 792
			break;
		}
		break;
793
	case NL80211_PLINK_HOLDING:
794 795
		switch (event) {
		case CLS_ACPT:
796
			if (del_timer(&sta->plink_timer))
797 798
				sta->ignore_plink_timer = 1;
			mesh_plink_fsm_restart(sta);
799
			spin_unlock_bh(&sta->lock);
800 801 802 803 804 805 806
			break;
		case OPN_ACPT:
		case CNF_ACPT:
		case OPN_RJCT:
		case CNF_RJCT:
			llid = sta->llid;
			reason = sta->reason;
807
			spin_unlock_bh(&sta->lock);
808 809
			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
810 811
			break;
		default:
812
			spin_unlock_bh(&sta->lock);
813 814 815
		}
		break;
	default:
816
		/* should not get here, PLINK_BLOCKED is dealt with at the
D
Daniel Mack 已提交
817
		 * beginning of the function
818
		 */
819
		spin_unlock_bh(&sta->lock);
820 821
		break;
	}
822 823

	rcu_read_unlock();
824
}