mesh_plink.c 21.8 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;
157 158
	struct ieee80211_mgmt *mgmt;
	bool include_plid = false;
159
	u16 peering_proto = 0;
160 161 162 163 164 165 166 167 168 169 170 171 172 173
	u8 *pos, ie_len = 4;
	int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.self_prot) +
		      sizeof(mgmt->u.action.u.self_prot);

	skb = dev_alloc_skb(local->hw.extra_tx_headroom +
			    hdr_len +
			    2 + /* capability info */
			    2 + /* AID */
			    2 + 8 + /* supported rates */
			    2 + (IEEE80211_MAX_SUPP_RATES - 8) +
			    2 + sdata->u.mesh.mesh_id_len +
			    2 + sizeof(struct ieee80211_meshconf_ie) +
			    2 + 8 + /* peering IE */
			    sdata->u.mesh.ie_len);
174 175 176
	if (!skb)
		return -1;
	skb_reserve(skb, local->hw.extra_tx_headroom);
177 178
	mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
	memset(mgmt, 0, hdr_len);
179 180
	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
					  IEEE80211_STYPE_ACTION);
181
	memcpy(mgmt->da, da, ETH_ALEN);
182
	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
183
	memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
184 185
	mgmt->u.action.category = WLAN_CATEGORY_SELF_PROTECTED;
	mgmt->u.action.u.self_prot.action_code = action;
186

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

207
	/* Add Mesh Peering Management element */
208
	switch (action) {
209
	case WLAN_SP_MESH_PEERING_OPEN:
210
		break;
211
	case WLAN_SP_MESH_PEERING_CONFIRM:
212
		ie_len += 2;
213 214
		include_plid = true;
		break;
215
	case WLAN_SP_MESH_PEERING_CLOSE:
216 217
		if (plid) {
			ie_len += 2;
218 219
			include_plid = true;
		}
220
		ie_len += 2;	/* reason code */
221
		break;
222 223
	default:
		return -EINVAL;
224 225
	}

226 227 228
	if (WARN_ON(skb_tailroom(skb) < 2 + ie_len))
		return -ENOMEM;

229
	pos = skb_put(skb, 2 + ie_len);
230
	*pos++ = WLAN_EID_PEER_MGMT;
231
	*pos++ = ie_len;
232 233
	memcpy(pos, &peering_proto, 2);
	pos += 2;
234
	memcpy(pos, &llid, 2);
235
	pos += 2;
236 237
	if (include_plid) {
		memcpy(pos, &plid, 2);
238
		pos += 2;
239
	}
240
	if (action == WLAN_SP_MESH_PEERING_CLOSE) {
241
		memcpy(pos, &reason, 2);
242
		pos += 2;
243
	}
244 245
	if (mesh_add_vendor_ies(skb, sdata))
		return -1;
246

247
	ieee80211_tx_skb(sdata, skb);
248 249 250
	return 0;
}

251 252 253
void mesh_neighbour_update(u8 *hw_addr, u32 rates,
		struct ieee80211_sub_if_data *sdata,
		struct ieee802_11_elems *elems)
254
{
255
	struct ieee80211_local *local = sdata->local;
256 257
	struct sta_info *sta;

258 259
	rcu_read_lock();

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

	sta->last_rx = jiffies;
J
Johannes Berg 已提交
280
	sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
281
	if (mesh_peer_accepts_plinks(elems) &&
282
			sta->plink_state == NL80211_PLINK_LISTEN &&
283 284
			sdata->u.mesh.accepting_plinks &&
			sdata->u.mesh.mshcfg.auto_open_plinks)
285 286
		mesh_plink_open(sta);

287
	rcu_read_unlock();
288 289 290 291 292 293 294 295
}

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

296 297 298 299 300
	/*
	 * 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.)
	 */
301 302
	sta = (struct sta_info *) data;

303 304 305 306 307
	if (sta->sdata->local->quiescing) {
		sta->plink_timer_was_running = true;
		return;
	}

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

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

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
#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

380 381 382 383 384 385 386 387 388 389 390 391
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;
392
	struct ieee80211_sub_if_data *sdata = sta->sdata;
393

J
Johannes Berg 已提交
394
	if (!test_sta_flag(sta, WLAN_STA_AUTH))
395 396
		return -EPERM;

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

410
	return mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
411
				   sta->sta.addr, llid, 0, 0);
412 413 414 415
}

void mesh_plink_block(struct sta_info *sta)
{
416 417 418
	struct ieee80211_sub_if_data *sdata = sta->sdata;
	bool deactivated;

419
	spin_lock_bh(&sta->lock);
420
	deactivated = __mesh_plink_deactivate(sta);
421
	sta->plink_state = NL80211_PLINK_BLOCKED;
422
	spin_unlock_bh(&sta->lock);
423 424 425

	if (deactivated)
		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
426 427 428
}


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

454 455 456 457
	/* need action_code, aux */
	if (len < IEEE80211_MIN_ACTION_SIZE + 3)
		return;

458 459 460 461 462
	if (is_multicast_ether_addr(mgmt->da)) {
		mpl_dbg("Mesh plink: ignore frame from multicast address");
		return;
	}

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

481 482 483 484 485 486
	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)) {
487 488
		mpl_dbg("Mesh plink: incorrect plink ie length %d %d\n",
		    ftype, ie_len);
489 490 491
		return;
	}

492 493
	if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
				(!elems.mesh_id || !elems.mesh_config)) {
494 495 496 497 498 499
		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.
	 */
500
	memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
501
	if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
502 503
	    (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
		memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
504

505 506
	rcu_read_lock();

507
	sta = sta_info_get(sdata, mgmt->sa);
508
	if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
509
		mpl_dbg("Mesh plink: cls or cnf from unknown peer\n");
510
		rcu_read_unlock();
511 512 513
		return;
	}

J
Johannes Berg 已提交
514
	if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
515 516 517 518 519
		mpl_dbg("Mesh plink: Action frame from non-authed peer\n");
		rcu_read_unlock();
		return;
	}

520
	if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
521
		rcu_read_unlock();
522 523 524 525 526
		return;
	}

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

	if (!sta && !matches_local) {
		rcu_read_unlock();
544
		reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
545
		llid = 0;
546 547
		mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
				    mgmt->sa, llid, plid, reason);
548
		return;
549
	} else if (!sta) {
550
		/* ftype == WLAN_SP_MESH_PEERING_OPEN */
551
		u32 rates;
552 553 554

		rcu_read_unlock();

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

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

651
	case NL80211_PLINK_OPN_SNT:
652 653 654
		switch (event) {
		case OPN_RJCT:
		case CNF_RJCT:
655
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
656 657
		case CLS_ACPT:
			if (!reason)
658
				reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
659
			sta->reason = reason;
660
			sta->plink_state = NL80211_PLINK_HOLDING;
661 662 663 664 665
			if (!mod_plink_timer(sta,
					     dot11MeshHoldingTimeout(sdata)))
				sta->ignore_plink_timer = true;

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

687
			spin_unlock_bh(&sta->lock);
688 689
			break;
		default:
690
			spin_unlock_bh(&sta->lock);
691 692 693 694
			break;
		}
		break;

695
	case NL80211_PLINK_OPN_RCVD:
696 697 698
		switch (event) {
		case OPN_RJCT:
		case CNF_RJCT:
699
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
700 701
		case CLS_ACPT:
			if (!reason)
702
				reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
703
			sta->reason = reason;
704
			sta->plink_state = NL80211_PLINK_HOLDING;
705 706 707 708 709
			if (!mod_plink_timer(sta,
					     dot11MeshHoldingTimeout(sdata)))
				sta->ignore_plink_timer = true;

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

736
	case NL80211_PLINK_CNF_RCVD:
737 738 739
		switch (event) {
		case OPN_RJCT:
		case CNF_RJCT:
740
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
741 742
		case CLS_ACPT:
			if (!reason)
743
				reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
744
			sta->reason = reason;
745
			sta->plink_state = NL80211_PLINK_HOLDING;
746 747 748 749 750
			if (!mod_plink_timer(sta,
					     dot11MeshHoldingTimeout(sdata)))
				sta->ignore_plink_timer = true;

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

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

	rcu_read_unlock();
832
}