mesh_plink.c 21.1 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 46 47 48

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

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

static inline
void mesh_plink_dec_estab_count(struct ieee80211_sub_if_data *sdata)
{
56
	atomic_dec(&sdata->u.mesh.mshstats.estab_plinks);
57
	mesh_accept_plinks_update(sdata);
58 59 60 61 62
}

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

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

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

87
	sta = sta_info_alloc(sdata, hw_addr, GFP_KERNEL);
J
Johannes Berg 已提交
88 89
	if (!sta)
		return NULL;
90

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

	return sta;
}

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

112
	if (sta->plink_state == NL80211_PLINK_ESTAB) {
113
		mesh_plink_dec_estab_count(sdata);
114 115
		deactivated = true;
	}
116
	sta->plink_state = NL80211_PLINK_BLOCKED;
117
	mesh_path_flush_by_nexthop(sta);
118 119

	return deactivated;
120 121
}

J
Johannes Berg 已提交
122
/**
123
 * mesh_plink_deactivate - deactivate mesh peer link
J
Johannes Berg 已提交
124 125 126 127 128 129 130
 *
 * @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)
{
131 132 133
	struct ieee80211_sub_if_data *sdata = sta->sdata;
	bool deactivated;

134
	spin_lock_bh(&sta->lock);
135
	deactivated = __mesh_plink_deactivate(sta);
136
	spin_unlock_bh(&sta->lock);
137 138 139

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

142
static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
143 144
		enum ieee80211_self_protected_actioncode action,
		u8 *da, __le16 llid, __le16 plid, __le16 reason) {
145
	struct ieee80211_local *local = sdata->local;
146
	struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400 +
147
			sdata->u.mesh.ie_len);
148 149
	struct ieee80211_mgmt *mgmt;
	bool include_plid = false;
150 151
	int ie_len = 4;
	u16 peering_proto = 0;
152 153 154 155 156 157 158 159 160
	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 *)
161 162
		skb_put(skb, 25 + sizeof(mgmt->u.action.u.self_prot));
	memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.self_prot));
163 164
	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
					  IEEE80211_STYPE_ACTION);
165
	memcpy(mgmt->da, da, ETH_ALEN);
166
	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
167
	memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
168 169
	mgmt->u.action.category = WLAN_CATEGORY_SELF_PROTECTED;
	mgmt->u.action.u.self_prot.action_code = action;
170

171 172 173 174
	if (action != WLAN_SP_MESH_PEERING_CLOSE) {
		/* capability info */
		pos = skb_put(skb, 2);
		memset(pos, 0, 2);
175
		if (action == WLAN_SP_MESH_PEERING_CONFIRM) {
176 177
			/* AID */
			pos = skb_put(skb, 2);
178
			memcpy(pos + 2, &plid, 2);
179
		}
180 181 182 183 184 185
		if (mesh_add_srates_ie(skb, sdata) ||
		    mesh_add_ext_srates_ie(skb, sdata) ||
		    mesh_add_rsn_ie(skb, sdata) ||
		    mesh_add_meshid_ie(skb, sdata) ||
		    mesh_add_meshconf_ie(skb, sdata))
			return -1;
186 187 188
	} else {	/* WLAN_SP_MESH_PEERING_CLOSE */
		if (mesh_add_meshid_ie(skb, sdata))
			return -1;
189 190
	}

191
	/* Add Mesh Peering Management element */
192
	switch (action) {
193
	case WLAN_SP_MESH_PEERING_OPEN:
194
		break;
195
	case WLAN_SP_MESH_PEERING_CONFIRM:
196
		ie_len += 2;
197 198
		include_plid = true;
		break;
199
	case WLAN_SP_MESH_PEERING_CLOSE:
200 201
		if (plid) {
			ie_len += 2;
202 203
			include_plid = true;
		}
204
		ie_len += 2;	/* reason code */
205
		break;
206 207
	default:
		return -EINVAL;
208 209
	}

210 211 212
	if (WARN_ON(skb_tailroom(skb) < 2 + ie_len))
		return -ENOMEM;

213
	pos = skb_put(skb, 2 + ie_len);
214
	*pos++ = WLAN_EID_PEER_MGMT;
215
	*pos++ = ie_len;
216 217
	memcpy(pos, &peering_proto, 2);
	pos += 2;
218
	memcpy(pos, &llid, 2);
219
	pos += 2;
220 221
	if (include_plid) {
		memcpy(pos, &plid, 2);
222
		pos += 2;
223
	}
224
	if (action == WLAN_SP_MESH_PEERING_CLOSE) {
225
		memcpy(pos, &reason, 2);
226
		pos += 2;
227
	}
228 229
	if (mesh_add_vendor_ies(skb, sdata))
		return -1;
230

231
	ieee80211_tx_skb(sdata, skb);
232 233 234
	return 0;
}

235 236 237
void mesh_neighbour_update(u8 *hw_addr, u32 rates,
		struct ieee80211_sub_if_data *sdata,
		struct ieee802_11_elems *elems)
238
{
239
	struct ieee80211_local *local = sdata->local;
240 241
	struct sta_info *sta;

242 243
	rcu_read_lock();

244
	sta = sta_info_get(sdata, hw_addr);
245
	if (!sta) {
246
		rcu_read_unlock();
247 248
		/* Userspace handles peer allocation when security is enabled
		 * */
249
		if (sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED)
250 251 252 253 254
			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);
255
		if (!sta)
J
Johannes Berg 已提交
256
			return;
257
		if (sta_info_insert_rcu(sta)) {
258
			rcu_read_unlock();
259
			return;
260
		}
261 262 263
	}

	sta->last_rx = jiffies;
J
Johannes Berg 已提交
264
	sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
265
	if (mesh_peer_accepts_plinks(elems) &&
266
			sta->plink_state == NL80211_PLINK_LISTEN &&
267 268
			sdata->u.mesh.accepting_plinks &&
			sdata->u.mesh.mshcfg.auto_open_plinks)
269 270
		mesh_plink_open(sta);

271
	rcu_read_unlock();
272 273 274 275 276 277 278 279
}

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

280 281 282 283 284
	/*
	 * 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.)
	 */
285 286
	sta = (struct sta_info *) data;

287 288 289 290 291
	if (sta->sdata->local->quiescing) {
		sta->plink_timer_was_running = true;
		return;
	}

292
	spin_lock_bh(&sta->lock);
293 294
	if (sta->ignore_plink_timer) {
		sta->ignore_plink_timer = false;
295
		spin_unlock_bh(&sta->lock);
296 297
		return;
	}
298 299
	mpl_dbg("Mesh plink timer for %pM fired on state %d\n",
		sta->sta.addr, sta->plink_state);
300 301 302
	reason = 0;
	llid = sta->llid;
	plid = sta->plid;
303
	sdata = sta->sdata;
304 305

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

348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
#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

364 365 366 367 368 369 370 371 372 373 374 375
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;
376
	struct ieee80211_sub_if_data *sdata = sta->sdata;
377

378 379 380
	if (!test_sta_flags(sta, WLAN_STA_AUTH))
		return -EPERM;

381
	spin_lock_bh(&sta->lock);
382 383
	get_random_bytes(&llid, 2);
	sta->llid = llid;
384
	if (sta->plink_state != NL80211_PLINK_LISTEN) {
385
		spin_unlock_bh(&sta->lock);
386 387
		return -EBUSY;
	}
388
	sta->plink_state = NL80211_PLINK_OPN_SNT;
389
	mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
390
	spin_unlock_bh(&sta->lock);
391 392
	mpl_dbg("Mesh plink: starting establishment with %pM\n",
		sta->sta.addr);
393

394
	return mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
395
				   sta->sta.addr, llid, 0, 0);
396 397 398 399
}

void mesh_plink_block(struct sta_info *sta)
{
400 401 402
	struct ieee80211_sub_if_data *sdata = sta->sdata;
	bool deactivated;

403
	spin_lock_bh(&sta->lock);
404
	deactivated = __mesh_plink_deactivate(sta);
405
	sta->plink_state = NL80211_PLINK_BLOCKED;
406
	spin_unlock_bh(&sta->lock);
407 408 409

	if (deactivated)
		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
410 411 412
}


413
void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata, struct ieee80211_mgmt *mgmt,
414 415
			 size_t len, struct ieee80211_rx_status *rx_status)
{
416
	struct ieee80211_local *local = sdata->local;
417 418 419
	struct ieee802_11_elems elems;
	struct sta_info *sta;
	enum plink_event event;
420
	enum ieee80211_self_protected_actioncode ftype;
421
	size_t baselen;
422
	bool deactivated, matches_local = true;
423 424 425
	u8 ie_len;
	u8 *baseaddr;
	__le16 plid, llid, reason;
426 427
#ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
	static const char *mplstates[] = {
428 429 430 431 432 433 434
		[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"
435 436
	};
#endif
437

438 439 440 441
	/* need action_code, aux */
	if (len < IEEE80211_MIN_ACTION_SIZE + 3)
		return;

442 443 444 445 446
	if (is_multicast_ether_addr(mgmt->da)) {
		mpl_dbg("Mesh plink: ignore frame from multicast address");
		return;
	}

447 448 449
	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 ==
450
						WLAN_SP_MESH_PEERING_CONFIRM) {
451
		baseaddr += 4;
452
		baselen += 4;
453 454
	}
	ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
455
	if (!elems.peering) {
456 457 458
		mpl_dbg("Mesh plink: missing necessary peer link ie\n");
		return;
	}
459 460
	if (elems.rsn_len &&
			sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
461 462 463
		mpl_dbg("Mesh plink: can't establish link with secure peer\n");
		return;
	}
464

465 466 467 468 469 470
	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)) {
471 472
		mpl_dbg("Mesh plink: incorrect plink ie length %d %d\n",
		    ftype, ie_len);
473 474 475
		return;
	}

476 477
	if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
				(!elems.mesh_id || !elems.mesh_config)) {
478 479 480 481 482 483
		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.
	 */
484
	memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
485
	if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
486 487
	    (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
		memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
488

489 490
	rcu_read_lock();

491
	sta = sta_info_get(sdata, mgmt->sa);
492
	if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
493
		mpl_dbg("Mesh plink: cls or cnf from unknown peer\n");
494
		rcu_read_unlock();
495 496 497
		return;
	}

498 499 500 501 502 503
	if (sta && !test_sta_flags(sta, WLAN_STA_AUTH)) {
		mpl_dbg("Mesh plink: Action frame from non-authed peer\n");
		rcu_read_unlock();
		return;
	}

504
	if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
505
		rcu_read_unlock();
506 507 508 509 510
		return;
	}

	/* Now we will figure out the appropriate event... */
	event = PLINK_UNDEFINED;
511 512
	if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
	    (!mesh_matches_local(&elems, sdata))) {
513
		matches_local = false;
514
		switch (ftype) {
515
		case WLAN_SP_MESH_PEERING_OPEN:
516 517
			event = OPN_RJCT;
			break;
518
		case WLAN_SP_MESH_PEERING_CONFIRM:
519 520
			event = CNF_RJCT;
			break;
521
		default:
522 523
			break;
		}
524 525 526 527
	}

	if (!sta && !matches_local) {
		rcu_read_unlock();
528
		reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
529
		llid = 0;
530 531
		mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
				    mgmt->sa, llid, plid, reason);
532
		return;
533
	} else if (!sta) {
534
		/* ftype == WLAN_SP_MESH_PEERING_OPEN */
535
		u32 rates;
536 537 538

		rcu_read_unlock();

539 540 541 542 543 544
		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);
545
		sta = mesh_plink_alloc(sdata, mgmt->sa, rates);
J
Johannes Berg 已提交
546
		if (!sta) {
547 548 549
			mpl_dbg("Mesh plink error: plink table full\n");
			return;
		}
550
		if (sta_info_insert_rcu(sta)) {
J
Johannes Berg 已提交
551 552 553
			rcu_read_unlock();
			return;
		}
554
		event = OPN_ACPT;
555
		spin_lock_bh(&sta->lock);
556
	} else if (matches_local) {
557
		spin_lock_bh(&sta->lock);
558
		switch (ftype) {
559
		case WLAN_SP_MESH_PEERING_OPEN:
560
			if (!mesh_plink_free_count(sdata) ||
561
			    (sta->plid && sta->plid != plid))
562 563 564 565
				event = OPN_IGNR;
			else
				event = OPN_ACPT;
			break;
566
		case WLAN_SP_MESH_PEERING_CONFIRM:
567
			if (!mesh_plink_free_count(sdata) ||
568
			    (sta->llid != llid || sta->plid != plid))
569 570 571 572
				event = CNF_IGNR;
			else
				event = CNF_ACPT;
			break;
573
		case WLAN_SP_MESH_PEERING_CLOSE:
574
			if (sta->plink_state == NL80211_PLINK_ESTAB)
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
				/* 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");
594
			spin_unlock_bh(&sta->lock);
595
			rcu_read_unlock();
596 597
			return;
		}
598 599
	} else {
		spin_lock_bh(&sta->lock);
600 601
	}

602 603
	mpl_dbg("Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
		mgmt->sa, mplstates[sta->plink_state],
604 605
		le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
		event);
606 607 608
	reason = 0;
	switch (sta->plink_state) {
		/* spin_unlock as soon as state is updated at each case */
609
	case NL80211_PLINK_LISTEN:
610 611 612
		switch (event) {
		case CLS_ACPT:
			mesh_plink_fsm_restart(sta);
613
			spin_unlock_bh(&sta->lock);
614 615
			break;
		case OPN_ACPT:
616
			sta->plink_state = NL80211_PLINK_OPN_RCVD;
617 618 619 620
			sta->plid = plid;
			get_random_bytes(&llid, 2);
			sta->llid = llid;
			mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
621
			spin_unlock_bh(&sta->lock);
622 623 624 625 626 627
			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);
628 629
			break;
		default:
630
			spin_unlock_bh(&sta->lock);
631 632 633 634
			break;
		}
		break;

635
	case NL80211_PLINK_OPN_SNT:
636 637 638
		switch (event) {
		case OPN_RJCT:
		case CNF_RJCT:
639
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
640 641
		case CLS_ACPT:
			if (!reason)
642
				reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
643
			sta->reason = reason;
644
			sta->plink_state = NL80211_PLINK_HOLDING;
645 646 647 648 649
			if (!mod_plink_timer(sta,
					     dot11MeshHoldingTimeout(sdata)))
				sta->ignore_plink_timer = true;

			llid = sta->llid;
650
			spin_unlock_bh(&sta->lock);
651 652 653
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
654 655 656
			break;
		case OPN_ACPT:
			/* retry timer is left untouched */
657
			sta->plink_state = NL80211_PLINK_OPN_RCVD;
658 659
			sta->plid = plid;
			llid = sta->llid;
660
			spin_unlock_bh(&sta->lock);
661 662 663
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CONFIRM,
					    sta->sta.addr, llid, plid, 0);
664 665
			break;
		case CNF_ACPT:
666
			sta->plink_state = NL80211_PLINK_CNF_RCVD;
667 668 669 670
			if (!mod_plink_timer(sta,
					     dot11MeshConfirmTimeout(sdata)))
				sta->ignore_plink_timer = true;

671
			spin_unlock_bh(&sta->lock);
672 673
			break;
		default:
674
			spin_unlock_bh(&sta->lock);
675 676 677 678
			break;
		}
		break;

679
	case NL80211_PLINK_OPN_RCVD:
680 681 682
		switch (event) {
		case OPN_RJCT:
		case CNF_RJCT:
683
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
684 685
		case CLS_ACPT:
			if (!reason)
686
				reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
687
			sta->reason = reason;
688
			sta->plink_state = NL80211_PLINK_HOLDING;
689 690 691 692 693
			if (!mod_plink_timer(sta,
					     dot11MeshHoldingTimeout(sdata)))
				sta->ignore_plink_timer = true;

			llid = sta->llid;
694
			spin_unlock_bh(&sta->lock);
695 696
			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
697 698 699
			break;
		case OPN_ACPT:
			llid = sta->llid;
700
			spin_unlock_bh(&sta->lock);
701 702 703
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CONFIRM,
					    sta->sta.addr, llid, plid, 0);
704 705
			break;
		case CNF_ACPT:
706
			del_timer(&sta->plink_timer);
707
			sta->plink_state = NL80211_PLINK_ESTAB;
708
			spin_unlock_bh(&sta->lock);
709 710
			mesh_plink_inc_estab_count(sdata);
			ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
711 712
			mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
				sta->sta.addr);
713 714
			break;
		default:
715
			spin_unlock_bh(&sta->lock);
716 717 718 719
			break;
		}
		break;

720
	case NL80211_PLINK_CNF_RCVD:
721 722 723
		switch (event) {
		case OPN_RJCT:
		case CNF_RJCT:
724
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
725 726
		case CLS_ACPT:
			if (!reason)
727
				reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
728
			sta->reason = reason;
729
			sta->plink_state = NL80211_PLINK_HOLDING;
730 731 732 733 734
			if (!mod_plink_timer(sta,
					     dot11MeshHoldingTimeout(sdata)))
				sta->ignore_plink_timer = true;

			llid = sta->llid;
735
			spin_unlock_bh(&sta->lock);
736 737 738
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
739
			break;
740
		case OPN_ACPT:
741
			del_timer(&sta->plink_timer);
742
			sta->plink_state = NL80211_PLINK_ESTAB;
743
			spin_unlock_bh(&sta->lock);
744 745
			mesh_plink_inc_estab_count(sdata);
			ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
746 747
			mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
				sta->sta.addr);
748 749 750
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CONFIRM,
					    sta->sta.addr, llid, plid, 0);
751 752
			break;
		default:
753
			spin_unlock_bh(&sta->lock);
754 755 756 757
			break;
		}
		break;

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

	rcu_read_unlock();
816
}