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

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

	return sta;
}

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

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

	return deactivated;
126 127
}

J
Johannes Berg 已提交
128
/**
129
 * mesh_plink_deactivate - deactivate mesh peer link
J
Johannes Berg 已提交
130 131 132 133 134 135 136
 *
 * @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)
{
137 138 139
	struct ieee80211_sub_if_data *sdata = sta->sdata;
	bool deactivated;

140
	spin_lock_bh(&sta->lock);
141
	deactivated = __mesh_plink_deactivate(sta);
142 143 144 145
	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);
146
	spin_unlock_bh(&sta->lock);
147 148 149

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

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

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

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

220 221 222
	if (WARN_ON(skb_tailroom(skb) < 2 + ie_len))
		return -ENOMEM;

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

241
	ieee80211_tx_skb(sdata, skb);
242 243 244
	return 0;
}

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

252 253
	rcu_read_lock();

254
	sta = sta_info_get(sdata, hw_addr);
255
	if (!sta) {
256
		rcu_read_unlock();
257 258
		/* Userspace handles peer allocation when security is enabled
		 * */
259
		if (sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED)
260 261 262 263 264
			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);
265
		if (!sta)
J
Johannes Berg 已提交
266
			return;
267
		if (sta_info_insert_rcu(sta)) {
268
			rcu_read_unlock();
269
			return;
270
		}
271 272 273
	}

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

281
	rcu_read_unlock();
282 283 284 285 286 287 288 289
}

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

290 291 292 293 294
	/*
	 * 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.)
	 */
295 296
	sta = (struct sta_info *) data;

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

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

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

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
#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

374 375 376 377 378 379 380 381 382 383 384 385
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;
386
	struct ieee80211_sub_if_data *sdata = sta->sdata;
387

J
Johannes Berg 已提交
388
	if (!test_sta_flag(sta, WLAN_STA_AUTH))
389 390
		return -EPERM;

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

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

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

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

	if (deactivated)
		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
420 421 422
}


423
void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata, struct ieee80211_mgmt *mgmt,
424 425
			 size_t len, struct ieee80211_rx_status *rx_status)
{
426
	struct ieee80211_local *local = sdata->local;
427 428 429
	struct ieee802_11_elems elems;
	struct sta_info *sta;
	enum plink_event event;
430
	enum ieee80211_self_protected_actioncode ftype;
431
	size_t baselen;
432
	bool deactivated, matches_local = true;
433 434 435
	u8 ie_len;
	u8 *baseaddr;
	__le16 plid, llid, reason;
436 437
#ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
	static const char *mplstates[] = {
438 439 440 441 442 443 444
		[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"
445 446
	};
#endif
447

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

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

457 458 459
	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 ==
460
						WLAN_SP_MESH_PEERING_CONFIRM) {
461
		baseaddr += 4;
462
		baselen += 4;
463 464
	}
	ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
465
	if (!elems.peering) {
466 467 468
		mpl_dbg("Mesh plink: missing necessary peer link ie\n");
		return;
	}
469 470
	if (elems.rsn_len &&
			sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
471 472 473
		mpl_dbg("Mesh plink: can't establish link with secure peer\n");
		return;
	}
474

475 476 477 478 479 480
	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)) {
481 482
		mpl_dbg("Mesh plink: incorrect plink ie length %d %d\n",
		    ftype, ie_len);
483 484 485
		return;
	}

486 487
	if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
				(!elems.mesh_id || !elems.mesh_config)) {
488 489 490 491 492 493
		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.
	 */
494
	memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
495
	if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
496 497
	    (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
		memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
498

499 500
	rcu_read_lock();

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

J
Johannes Berg 已提交
508
	if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
509 510 511 512 513
		mpl_dbg("Mesh plink: Action frame from non-authed peer\n");
		rcu_read_unlock();
		return;
	}

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

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

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

		rcu_read_unlock();

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

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

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

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

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

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

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

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

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

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

	rcu_read_unlock();
826
}