mesh_plink.c 22.7 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
#define sta_meets_rssi_threshold(sta, sdata) \
		(sdata->u.mesh.mshcfg.rssi_threshold == 0 ||\
		(s8) -ewma_read(&sta->avg_signal) > \
		sdata->u.mesh.mshcfg.rssi_threshold)

39 40 41 42 43 44 45 46 47 48 49 50
enum plink_event {
	PLINK_UNDEFINED,
	OPN_ACPT,
	OPN_RJCT,
	OPN_IGNR,
	CNF_ACPT,
	CNF_RJCT,
	CNF_IGNR,
	CLS_ACPT,
	CLS_IGNR
};

51 52 53 54
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);

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

static inline
void mesh_plink_dec_estab_count(struct ieee80211_sub_if_data *sdata)
{
65
	atomic_dec(&sdata->u.mesh.mshstats.estab_plinks);
66
	mesh_accept_plinks_update(sdata);
67 68 69 70 71
}

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

83 84 85 86
/*
 * NOTE: This is just an alias for sta_info_alloc(), see notes
 *       on it in the lifecycle management section!
 */
87
static struct sta_info *mesh_plink_alloc(struct ieee80211_sub_if_data *sdata,
88 89
					 u8 *hw_addr, u32 rates,
					 struct ieee802_11_elems *elems)
90
{
91
	struct ieee80211_local *local = sdata->local;
92
	struct ieee80211_supported_band *sband;
93 94
	struct sta_info *sta;

95 96
	sband = local->hw.wiphy->bands[local->oper_channel->band];

97
	if (local->num_sta >= MESH_MAX_PLINKS)
J
Johannes Berg 已提交
98
		return NULL;
99

100
	sta = sta_info_alloc(sdata, hw_addr, GFP_KERNEL);
J
Johannes Berg 已提交
101 102
	if (!sta)
		return NULL;
103

104 105 106
	sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
	sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
	sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
107

J
Johannes Berg 已提交
108
	set_sta_flag(sta, WLAN_STA_WME);
109

J
Johannes Berg 已提交
110
	sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
111
	if (elems->ht_cap_elem)
B
Ben Greear 已提交
112 113
		ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
						  elems->ht_cap_elem,
114
						  &sta->sta.ht_cap);
115
	rate_control_rate_init(sta);
116 117 118 119 120

	return sta;
}

/**
121
 * __mesh_plink_deactivate - deactivate mesh peer link
122 123 124 125 126
 *
 * @sta: mesh peer link to deactivate
 *
 * All mesh paths with this peer as next hop will be flushed
 *
127
 * Locking: the caller must hold sta->lock
128
 */
129
static bool __mesh_plink_deactivate(struct sta_info *sta)
130
{
131
	struct ieee80211_sub_if_data *sdata = sta->sdata;
132
	bool deactivated = false;
133

134
	if (sta->plink_state == NL80211_PLINK_ESTAB) {
135
		mesh_plink_dec_estab_count(sdata);
136 137
		deactivated = true;
	}
138
	sta->plink_state = NL80211_PLINK_BLOCKED;
139
	mesh_path_flush_by_nexthop(sta);
140 141

	return deactivated;
142 143
}

J
Johannes Berg 已提交
144
/**
145
 * mesh_plink_deactivate - deactivate mesh peer link
J
Johannes Berg 已提交
146 147 148 149 150 151 152
 *
 * @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)
{
153 154 155
	struct ieee80211_sub_if_data *sdata = sta->sdata;
	bool deactivated;

156
	spin_lock_bh(&sta->lock);
157
	deactivated = __mesh_plink_deactivate(sta);
158 159 160 161
	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);
162
	spin_unlock_bh(&sta->lock);
163 164 165

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

168
static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
169 170
		enum ieee80211_self_protected_actioncode action,
		u8 *da, __le16 llid, __le16 plid, __le16 reason) {
171
	struct ieee80211_local *local = sdata->local;
172
	struct sk_buff *skb;
173 174
	struct ieee80211_mgmt *mgmt;
	bool include_plid = false;
175
	u16 peering_proto = 0;
176 177 178 179
	u8 *pos, ie_len = 4;
	int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.self_prot) +
		      sizeof(mgmt->u.action.u.self_prot);

180
	skb = dev_alloc_skb(local->tx_headroom +
181 182 183 184 185 186 187
			    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) +
188 189
			    2 + sizeof(struct ieee80211_ht_cap) +
			    2 + sizeof(struct ieee80211_ht_info) +
190 191
			    2 + 8 + /* peering IE */
			    sdata->u.mesh.ie_len);
192 193
	if (!skb)
		return -1;
194
	skb_reserve(skb, local->tx_headroom);
195 196
	mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
	memset(mgmt, 0, hdr_len);
197 198
	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
					  IEEE80211_STYPE_ACTION);
199
	memcpy(mgmt->da, da, ETH_ALEN);
200
	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
201
	memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
202 203
	mgmt->u.action.category = WLAN_CATEGORY_SELF_PROTECTED;
	mgmt->u.action.u.self_prot.action_code = action;
204

205 206 207 208
	if (action != WLAN_SP_MESH_PEERING_CLOSE) {
		/* capability info */
		pos = skb_put(skb, 2);
		memset(pos, 0, 2);
209
		if (action == WLAN_SP_MESH_PEERING_CONFIRM) {
210 211
			/* AID */
			pos = skb_put(skb, 2);
212
			memcpy(pos + 2, &plid, 2);
213
		}
214 215
		if (ieee80211_add_srates_ie(&sdata->vif, skb) ||
		    ieee80211_add_ext_srates_ie(&sdata->vif, skb) ||
216 217 218 219
		    mesh_add_rsn_ie(skb, sdata) ||
		    mesh_add_meshid_ie(skb, sdata) ||
		    mesh_add_meshconf_ie(skb, sdata))
			return -1;
220 221 222
	} else {	/* WLAN_SP_MESH_PEERING_CLOSE */
		if (mesh_add_meshid_ie(skb, sdata))
			return -1;
223 224
	}

225
	/* Add Mesh Peering Management element */
226
	switch (action) {
227
	case WLAN_SP_MESH_PEERING_OPEN:
228
		break;
229
	case WLAN_SP_MESH_PEERING_CONFIRM:
230
		ie_len += 2;
231 232
		include_plid = true;
		break;
233
	case WLAN_SP_MESH_PEERING_CLOSE:
234 235
		if (plid) {
			ie_len += 2;
236 237
			include_plid = true;
		}
238
		ie_len += 2;	/* reason code */
239
		break;
240 241
	default:
		return -EINVAL;
242 243
	}

244 245 246
	if (WARN_ON(skb_tailroom(skb) < 2 + ie_len))
		return -ENOMEM;

247
	pos = skb_put(skb, 2 + ie_len);
248
	*pos++ = WLAN_EID_PEER_MGMT;
249
	*pos++ = ie_len;
250 251
	memcpy(pos, &peering_proto, 2);
	pos += 2;
252
	memcpy(pos, &llid, 2);
253
	pos += 2;
254 255
	if (include_plid) {
		memcpy(pos, &plid, 2);
256
		pos += 2;
257
	}
258
	if (action == WLAN_SP_MESH_PEERING_CLOSE) {
259
		memcpy(pos, &reason, 2);
260
		pos += 2;
261
	}
262 263 264 265 266 267 268

	if (action != WLAN_SP_MESH_PEERING_CLOSE) {
		if (mesh_add_ht_cap_ie(skb, sdata) ||
		    mesh_add_ht_info_ie(skb, sdata))
			return -1;
	}

269 270
	if (mesh_add_vendor_ies(skb, sdata))
		return -1;
271

272
	ieee80211_tx_skb(sdata, skb);
273 274 275
	return 0;
}

276 277 278
void mesh_neighbour_update(u8 *hw_addr, u32 rates,
		struct ieee80211_sub_if_data *sdata,
		struct ieee802_11_elems *elems)
279
{
280
	struct ieee80211_local *local = sdata->local;
281 282
	struct sta_info *sta;

283 284
	rcu_read_lock();

285
	sta = sta_info_get(sdata, hw_addr);
286
	if (!sta) {
287
		rcu_read_unlock();
288 289
		/* Userspace handles peer allocation when security is enabled
		 * */
290
		if (sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED)
291 292 293 294
			cfg80211_notify_new_peer_candidate(sdata->dev, hw_addr,
					elems->ie_start, elems->total_len,
					GFP_KERNEL);
		else
295
			sta = mesh_plink_alloc(sdata, hw_addr, rates, elems);
296
		if (!sta)
J
Johannes Berg 已提交
297
			return;
298
		if (sta_info_insert_rcu(sta)) {
299
			rcu_read_unlock();
300
			return;
301
		}
302 303 304
	}

	sta->last_rx = jiffies;
J
Johannes Berg 已提交
305
	sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
306
	if (mesh_peer_accepts_plinks(elems) &&
307
			sta->plink_state == NL80211_PLINK_LISTEN &&
308
			sdata->u.mesh.accepting_plinks &&
309 310
			sdata->u.mesh.mshcfg.auto_open_plinks &&
			sta_meets_rssi_threshold(sta, sdata))
311 312
		mesh_plink_open(sta);

313
	rcu_read_unlock();
314 315 316 317 318 319 320 321
}

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

322 323 324 325 326
	/*
	 * 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.)
	 */
327 328
	sta = (struct sta_info *) data;

329 330 331 332 333
	if (sta->sdata->local->quiescing) {
		sta->plink_timer_was_running = true;
		return;
	}

334
	spin_lock_bh(&sta->lock);
335 336
	if (sta->ignore_plink_timer) {
		sta->ignore_plink_timer = false;
337
		spin_unlock_bh(&sta->lock);
338 339
		return;
	}
340 341
	mpl_dbg("Mesh plink timer for %pM fired on state %d\n",
		sta->sta.addr, sta->plink_state);
342 343 344
	reason = 0;
	llid = sta->llid;
	plid = sta->plid;
345
	sdata = sta->sdata;
346 347

	switch (sta->plink_state) {
348 349
	case NL80211_PLINK_OPN_RCVD:
	case NL80211_PLINK_OPN_SNT:
350 351 352
		/* retry timer */
		if (sta->plink_retries < dot11MeshMaxRetries(sdata)) {
			u32 rand;
353 354 355
			mpl_dbg("Mesh plink for %pM (retry, timeout): %d %d\n",
				sta->sta.addr, sta->plink_retries,
				sta->plink_timeout);
356 357 358 359
			get_random_bytes(&rand, sizeof(u32));
			sta->plink_timeout = sta->plink_timeout +
					     rand % sta->plink_timeout;
			++sta->plink_retries;
360
			mod_plink_timer(sta, sta->plink_timeout);
361
			spin_unlock_bh(&sta->lock);
362 363
			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
					    sta->sta.addr, llid, 0, 0);
364 365
			break;
		}
366
		reason = cpu_to_le16(WLAN_REASON_MESH_MAX_RETRIES);
367
		/* fall through on else */
368
	case NL80211_PLINK_CNF_RCVD:
369 370
		/* confirm timer */
		if (!reason)
371
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIRM_TIMEOUT);
372
		sta->plink_state = NL80211_PLINK_HOLDING;
373
		mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
374
		spin_unlock_bh(&sta->lock);
375 376
		mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
				    sta->sta.addr, llid, plid, reason);
377
		break;
378
	case NL80211_PLINK_HOLDING:
379
		/* holding timer */
380
		del_timer(&sta->plink_timer);
381
		mesh_plink_fsm_restart(sta);
382
		spin_unlock_bh(&sta->lock);
383 384
		break;
	default:
385
		spin_unlock_bh(&sta->lock);
386 387 388 389
		break;
	}
}

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
#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

406 407 408 409 410 411 412 413 414 415 416 417
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;
418
	struct ieee80211_sub_if_data *sdata = sta->sdata;
419

J
Johannes Berg 已提交
420
	if (!test_sta_flag(sta, WLAN_STA_AUTH))
421 422
		return -EPERM;

423
	spin_lock_bh(&sta->lock);
424 425
	get_random_bytes(&llid, 2);
	sta->llid = llid;
426
	if (sta->plink_state != NL80211_PLINK_LISTEN) {
427
		spin_unlock_bh(&sta->lock);
428 429
		return -EBUSY;
	}
430
	sta->plink_state = NL80211_PLINK_OPN_SNT;
431
	mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
432
	spin_unlock_bh(&sta->lock);
433 434
	mpl_dbg("Mesh plink: starting establishment with %pM\n",
		sta->sta.addr);
435

436
	return mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
437
				   sta->sta.addr, llid, 0, 0);
438 439 440 441
}

void mesh_plink_block(struct sta_info *sta)
{
442 443 444
	struct ieee80211_sub_if_data *sdata = sta->sdata;
	bool deactivated;

445
	spin_lock_bh(&sta->lock);
446
	deactivated = __mesh_plink_deactivate(sta);
447
	sta->plink_state = NL80211_PLINK_BLOCKED;
448
	spin_unlock_bh(&sta->lock);
449 450 451

	if (deactivated)
		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
452 453 454
}


455
void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata, struct ieee80211_mgmt *mgmt,
456 457
			 size_t len, struct ieee80211_rx_status *rx_status)
{
458
	struct ieee80211_local *local = sdata->local;
459 460 461
	struct ieee802_11_elems elems;
	struct sta_info *sta;
	enum plink_event event;
462
	enum ieee80211_self_protected_actioncode ftype;
463
	size_t baselen;
464
	bool deactivated, matches_local = true;
465 466 467
	u8 ie_len;
	u8 *baseaddr;
	__le16 plid, llid, reason;
468 469
#ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
	static const char *mplstates[] = {
470 471 472 473 474 475 476
		[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"
477 478
	};
#endif
479

480 481 482 483
	/* need action_code, aux */
	if (len < IEEE80211_MIN_ACTION_SIZE + 3)
		return;

484 485 486 487 488
	if (is_multicast_ether_addr(mgmt->da)) {
		mpl_dbg("Mesh plink: ignore frame from multicast address");
		return;
	}

489 490 491
	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 ==
492
						WLAN_SP_MESH_PEERING_CONFIRM) {
493
		baseaddr += 4;
494
		baselen += 4;
495 496
	}
	ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
497
	if (!elems.peering) {
498 499 500
		mpl_dbg("Mesh plink: missing necessary peer link ie\n");
		return;
	}
501 502
	if (elems.rsn_len &&
			sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
503 504 505
		mpl_dbg("Mesh plink: can't establish link with secure peer\n");
		return;
	}
506

507 508 509 510 511 512
	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)) {
513 514
		mpl_dbg("Mesh plink: incorrect plink ie length %d %d\n",
		    ftype, ie_len);
515 516 517
		return;
	}

518 519
	if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
				(!elems.mesh_id || !elems.mesh_config)) {
520 521 522 523 524 525
		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.
	 */
526
	memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
527
	if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
528 529
	    (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
		memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
530

531 532
	rcu_read_lock();

533
	sta = sta_info_get(sdata, mgmt->sa);
534
	if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
535
		mpl_dbg("Mesh plink: cls or cnf from unknown peer\n");
536
		rcu_read_unlock();
537 538 539
		return;
	}

540 541 542 543 544 545 546 547
	if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
	    !sta_meets_rssi_threshold(sta, sdata)) {
		mpl_dbg("Mesh plink: %pM does not meet rssi threshold\n",
			sta->sta.addr);
		rcu_read_unlock();
		return;
	}

J
Johannes Berg 已提交
548
	if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
549 550 551 552 553
		mpl_dbg("Mesh plink: Action frame from non-authed peer\n");
		rcu_read_unlock();
		return;
	}

554
	if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
555
		rcu_read_unlock();
556 557 558 559 560
		return;
	}

	/* Now we will figure out the appropriate event... */
	event = PLINK_UNDEFINED;
561 562
	if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
	    (!mesh_matches_local(&elems, sdata))) {
563
		matches_local = false;
564
		switch (ftype) {
565
		case WLAN_SP_MESH_PEERING_OPEN:
566 567
			event = OPN_RJCT;
			break;
568
		case WLAN_SP_MESH_PEERING_CONFIRM:
569 570
			event = CNF_RJCT;
			break;
571
		default:
572 573
			break;
		}
574 575 576 577
	}

	if (!sta && !matches_local) {
		rcu_read_unlock();
578
		reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
579
		llid = 0;
580 581
		mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
				    mgmt->sa, llid, plid, reason);
582
		return;
583
	} else if (!sta) {
584
		/* ftype == WLAN_SP_MESH_PEERING_OPEN */
585
		u32 rates;
586 587 588

		rcu_read_unlock();

589 590 591 592 593 594
		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);
595
		sta = mesh_plink_alloc(sdata, mgmt->sa, rates, &elems);
J
Johannes Berg 已提交
596
		if (!sta) {
597 598 599
			mpl_dbg("Mesh plink error: plink table full\n");
			return;
		}
600
		if (sta_info_insert_rcu(sta)) {
J
Johannes Berg 已提交
601 602 603
			rcu_read_unlock();
			return;
		}
604
		event = OPN_ACPT;
605
		spin_lock_bh(&sta->lock);
606
	} else if (matches_local) {
607
		spin_lock_bh(&sta->lock);
608
		switch (ftype) {
609
		case WLAN_SP_MESH_PEERING_OPEN:
610
			if (!mesh_plink_free_count(sdata) ||
611
			    (sta->plid && sta->plid != plid))
612 613 614 615
				event = OPN_IGNR;
			else
				event = OPN_ACPT;
			break;
616
		case WLAN_SP_MESH_PEERING_CONFIRM:
617
			if (!mesh_plink_free_count(sdata) ||
618
			    (sta->llid != llid || sta->plid != plid))
619 620 621 622
				event = CNF_IGNR;
			else
				event = CNF_ACPT;
			break;
623
		case WLAN_SP_MESH_PEERING_CLOSE:
624
			if (sta->plink_state == NL80211_PLINK_ESTAB)
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
				/* 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");
644
			spin_unlock_bh(&sta->lock);
645
			rcu_read_unlock();
646 647
			return;
		}
648 649
	} else {
		spin_lock_bh(&sta->lock);
650 651
	}

652 653
	mpl_dbg("Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
		mgmt->sa, mplstates[sta->plink_state],
654 655
		le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
		event);
656 657 658
	reason = 0;
	switch (sta->plink_state) {
		/* spin_unlock as soon as state is updated at each case */
659
	case NL80211_PLINK_LISTEN:
660 661 662
		switch (event) {
		case CLS_ACPT:
			mesh_plink_fsm_restart(sta);
663
			spin_unlock_bh(&sta->lock);
664 665
			break;
		case OPN_ACPT:
666
			sta->plink_state = NL80211_PLINK_OPN_RCVD;
667 668 669 670
			sta->plid = plid;
			get_random_bytes(&llid, 2);
			sta->llid = llid;
			mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
671
			spin_unlock_bh(&sta->lock);
672 673 674 675 676 677
			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);
678 679
			break;
		default:
680
			spin_unlock_bh(&sta->lock);
681 682 683 684
			break;
		}
		break;

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

			llid = sta->llid;
700
			spin_unlock_bh(&sta->lock);
701 702 703
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
704 705 706
			break;
		case OPN_ACPT:
			/* retry timer is left untouched */
707
			sta->plink_state = NL80211_PLINK_OPN_RCVD;
708 709
			sta->plid = plid;
			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
			sta->plink_state = NL80211_PLINK_CNF_RCVD;
717 718 719 720
			if (!mod_plink_timer(sta,
					     dot11MeshConfirmTimeout(sdata)))
				sta->ignore_plink_timer = true;

721
			spin_unlock_bh(&sta->lock);
722 723
			break;
		default:
724
			spin_unlock_bh(&sta->lock);
725 726 727 728
			break;
		}
		break;

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

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

770
	case NL80211_PLINK_CNF_RCVD:
771 772 773
		switch (event) {
		case OPN_RJCT:
		case CNF_RJCT:
774
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
775 776
		case CLS_ACPT:
			if (!reason)
777
				reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
778
			sta->reason = reason;
779
			sta->plink_state = NL80211_PLINK_HOLDING;
780 781 782 783 784
			if (!mod_plink_timer(sta,
					     dot11MeshHoldingTimeout(sdata)))
				sta->ignore_plink_timer = true;

			llid = sta->llid;
785
			spin_unlock_bh(&sta->lock);
786 787 788
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
789
			break;
790
		case OPN_ACPT:
791
			del_timer(&sta->plink_timer);
792
			sta->plink_state = NL80211_PLINK_ESTAB;
793
			spin_unlock_bh(&sta->lock);
794 795
			mesh_plink_inc_estab_count(sdata);
			ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
796 797
			mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
				sta->sta.addr);
798 799 800
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CONFIRM,
					    sta->sta.addr, llid, plid, 0);
801 802
			break;
		default:
803
			spin_unlock_bh(&sta->lock);
804 805 806 807
			break;
		}
		break;

808
	case NL80211_PLINK_ESTAB:
809 810
		switch (event) {
		case CLS_ACPT:
811
			reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
812
			sta->reason = reason;
813
			deactivated = __mesh_plink_deactivate(sta);
814
			sta->plink_state = NL80211_PLINK_HOLDING;
815
			llid = sta->llid;
816
			mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
817
			spin_unlock_bh(&sta->lock);
818 819
			if (deactivated)
				ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
820 821
			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
822 823 824
			break;
		case OPN_ACPT:
			llid = sta->llid;
825
			spin_unlock_bh(&sta->lock);
826 827 828
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CONFIRM,
					    sta->sta.addr, llid, plid, 0);
829 830
			break;
		default:
831
			spin_unlock_bh(&sta->lock);
832 833 834
			break;
		}
		break;
835
	case NL80211_PLINK_HOLDING:
836 837
		switch (event) {
		case CLS_ACPT:
838
			if (del_timer(&sta->plink_timer))
839 840
				sta->ignore_plink_timer = 1;
			mesh_plink_fsm_restart(sta);
841
			spin_unlock_bh(&sta->lock);
842 843 844 845 846 847 848
			break;
		case OPN_ACPT:
		case CNF_ACPT:
		case OPN_RJCT:
		case CNF_RJCT:
			llid = sta->llid;
			reason = sta->reason;
849
			spin_unlock_bh(&sta->lock);
850 851
			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
852 853
			break;
		default:
854
			spin_unlock_bh(&sta->lock);
855 856 857
		}
		break;
	default:
858
		/* should not get here, PLINK_BLOCKED is dealt with at the
D
Daniel Mack 已提交
859
		 * beginning of the function
860
		 */
861
		spin_unlock_bh(&sta->lock);
862 863
		break;
	}
864 865

	rcu_read_unlock();
866
}