mesh_plink.c 23.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
/* We only need a valid sta if user configured a minimum rssi_threshold. */
#define rssi_threshold_check(sta, sdata) \
36
		(sdata->u.mesh.mshcfg.rssi_threshold == 0 ||\
37 38
		(sta && (s8) -ewma_read(&sta->avg_signal) > \
		sdata->u.mesh.mshcfg.rssi_threshold))
39

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

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

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

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

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

84
/*
85
 * Allocate mesh sta entry and insert into station table
86
 */
87
static struct sta_info *mesh_plink_alloc(struct ieee80211_sub_if_data *sdata,
88
					 u8 *hw_addr)
89 90 91
{
	struct sta_info *sta;

92
	if (sdata->local->num_sta >= MESH_MAX_PLINKS)
J
Johannes Berg 已提交
93
		return NULL;
94

95
	sta = sta_info_alloc(sdata, hw_addr, GFP_KERNEL);
J
Johannes Berg 已提交
96 97
	if (!sta)
		return NULL;
98

99 100 101
	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);
102

J
Johannes Berg 已提交
103
	set_sta_flag(sta, WLAN_STA_WME);
104

105 106
	if (sta_info_insert(sta))
		return NULL;
107 108 109 110 111

	return sta;
}

/**
112
 * __mesh_plink_deactivate - deactivate mesh peer link
113 114 115 116 117
 *
 * @sta: mesh peer link to deactivate
 *
 * All mesh paths with this peer as next hop will be flushed
 *
118
 * Locking: the caller must hold sta->lock
119
 */
120
static bool __mesh_plink_deactivate(struct sta_info *sta)
121
{
122
	struct ieee80211_sub_if_data *sdata = sta->sdata;
123
	bool deactivated = false;
124

125
	if (sta->plink_state == NL80211_PLINK_ESTAB) {
126
		mesh_plink_dec_estab_count(sdata);
127 128
		deactivated = true;
	}
129
	sta->plink_state = NL80211_PLINK_BLOCKED;
130
	mesh_path_flush_by_nexthop(sta);
131 132

	return deactivated;
133 134
}

J
Johannes Berg 已提交
135
/**
136
 * mesh_plink_deactivate - deactivate mesh peer link
J
Johannes Berg 已提交
137 138 139 140 141 142 143
 *
 * @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)
{
144 145 146
	struct ieee80211_sub_if_data *sdata = sta->sdata;
	bool deactivated;

147
	spin_lock_bh(&sta->lock);
148
	deactivated = __mesh_plink_deactivate(sta);
149 150 151 152
	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);
153
	spin_unlock_bh(&sta->lock);
154 155 156

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

159
static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
160 161
		enum ieee80211_self_protected_actioncode action,
		u8 *da, __le16 llid, __le16 plid, __le16 reason) {
162
	struct ieee80211_local *local = sdata->local;
163
	struct sk_buff *skb;
164 165
	struct ieee80211_mgmt *mgmt;
	bool include_plid = false;
166
	u16 peering_proto = 0;
167 168 169 170
	u8 *pos, ie_len = 4;
	int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.self_prot) +
		      sizeof(mgmt->u.action.u.self_prot);

171
	skb = dev_alloc_skb(local->tx_headroom +
172 173 174 175 176 177 178
			    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) +
179
			    2 + sizeof(struct ieee80211_ht_cap) +
180
			    2 + sizeof(struct ieee80211_ht_operation) +
181 182
			    2 + 8 + /* peering IE */
			    sdata->u.mesh.ie_len);
183 184
	if (!skb)
		return -1;
185
	skb_reserve(skb, local->tx_headroom);
186 187
	mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
	memset(mgmt, 0, hdr_len);
188 189
	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
					  IEEE80211_STYPE_ACTION);
190
	memcpy(mgmt->da, da, ETH_ALEN);
191
	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
192
	memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
193 194
	mgmt->u.action.category = WLAN_CATEGORY_SELF_PROTECTED;
	mgmt->u.action.u.self_prot.action_code = action;
195

196 197 198 199
	if (action != WLAN_SP_MESH_PEERING_CLOSE) {
		/* capability info */
		pos = skb_put(skb, 2);
		memset(pos, 0, 2);
200
		if (action == WLAN_SP_MESH_PEERING_CONFIRM) {
201 202
			/* AID */
			pos = skb_put(skb, 2);
203
			memcpy(pos + 2, &plid, 2);
204
		}
205 206
		if (ieee80211_add_srates_ie(&sdata->vif, skb, true) ||
		    ieee80211_add_ext_srates_ie(&sdata->vif, skb, true) ||
207 208 209 210
		    mesh_add_rsn_ie(skb, sdata) ||
		    mesh_add_meshid_ie(skb, sdata) ||
		    mesh_add_meshconf_ie(skb, sdata))
			return -1;
211 212 213
	} else {	/* WLAN_SP_MESH_PEERING_CLOSE */
		if (mesh_add_meshid_ie(skb, sdata))
			return -1;
214 215
	}

216
	/* Add Mesh Peering Management element */
217
	switch (action) {
218
	case WLAN_SP_MESH_PEERING_OPEN:
219
		break;
220
	case WLAN_SP_MESH_PEERING_CONFIRM:
221
		ie_len += 2;
222 223
		include_plid = true;
		break;
224
	case WLAN_SP_MESH_PEERING_CLOSE:
225 226
		if (plid) {
			ie_len += 2;
227 228
			include_plid = true;
		}
229
		ie_len += 2;	/* reason code */
230
		break;
231 232
	default:
		return -EINVAL;
233 234
	}

235 236 237
	if (WARN_ON(skb_tailroom(skb) < 2 + ie_len))
		return -ENOMEM;

238
	pos = skb_put(skb, 2 + ie_len);
239
	*pos++ = WLAN_EID_PEER_MGMT;
240
	*pos++ = ie_len;
241 242
	memcpy(pos, &peering_proto, 2);
	pos += 2;
243
	memcpy(pos, &llid, 2);
244
	pos += 2;
245 246
	if (include_plid) {
		memcpy(pos, &plid, 2);
247
		pos += 2;
248
	}
249
	if (action == WLAN_SP_MESH_PEERING_CLOSE) {
250
		memcpy(pos, &reason, 2);
251
		pos += 2;
252
	}
253 254 255

	if (action != WLAN_SP_MESH_PEERING_CLOSE) {
		if (mesh_add_ht_cap_ie(skb, sdata) ||
256
		    mesh_add_ht_oper_ie(skb, sdata))
257 258 259
			return -1;
	}

260 261
	if (mesh_add_vendor_ies(skb, sdata))
		return -1;
262

263
	ieee80211_tx_skb(sdata, skb);
264 265 266
	return 0;
}

267 268 269 270 271 272 273 274 275
/* mesh_peer_init - initialize new mesh peer and return resulting sta_info
 *
 * @sdata: local meshif
 * @addr: peer's address
 * @elems: IEs from beacon or mesh peering frame
 *
 * call under RCU
 */
static struct sta_info *mesh_peer_init(struct ieee80211_sub_if_data *sdata,
276
				       u8 *addr,
277
				       struct ieee802_11_elems *elems)
278
{
279
	struct ieee80211_local *local = sdata->local;
280
	enum ieee80211_band band = local->oper_channel->band;
281
	struct ieee80211_supported_band *sband;
282
	u32 rates, basic_rates = 0;
283 284
	struct sta_info *sta;

285 286
	sband = local->hw.wiphy->bands[band];
	rates = ieee80211_sta_get_rates(local, elems, band, &basic_rates);
287

288
	sta = sta_info_get(sdata, addr);
289
	if (!sta) {
290
		sta = mesh_plink_alloc(sdata, addr);
291
		if (!sta)
292
			return NULL;
293 294
	}

295
	spin_lock_bh(&sta->lock);
296
	sta->last_rx = jiffies;
297
	sta->sta.supp_rates[band] = rates;
298 299
	if (elems->ht_cap_elem &&
	    sdata->local->_oper_channel_type != NL80211_CHAN_NO_HT)
300 301 302 303 304 305 306 307 308 309 310 311
		ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
						  elems->ht_cap_elem,
						  &sta->sta.ht_cap);
	else
		memset(&sta->sta.ht_cap, 0, sizeof(sta->sta.ht_cap));

	rate_control_rate_init(sta);
	spin_unlock_bh(&sta->lock);

	return sta;
}

312 313
void mesh_neighbour_update(struct ieee80211_sub_if_data *sdata,
			   u8 *hw_addr,
314 315 316 317 318 319 320 321 322 323 324 325 326 327
			   struct ieee802_11_elems *elems)
{
	struct sta_info *sta;

	/* Userspace handles peer allocation when security is enabled */
	if (sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED) {
		cfg80211_notify_new_peer_candidate(sdata->dev, hw_addr,
						   elems->ie_start,
						   elems->total_len,
						   GFP_KERNEL);
		return;
	}

	rcu_read_lock();
328
	sta = mesh_peer_init(sdata, hw_addr, elems);
329 330 331
	if (!sta)
		goto out;

332
	if (mesh_peer_accepts_plinks(elems) &&
333 334 335 336
	    sta->plink_state == NL80211_PLINK_LISTEN &&
	    sdata->u.mesh.accepting_plinks &&
	    sdata->u.mesh.mshcfg.auto_open_plinks &&
	    rssi_threshold_check(sta, sdata))
337 338
		mesh_plink_open(sta);

339
out:
340
	rcu_read_unlock();
341 342 343 344 345 346 347 348
}

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

349 350 351 352 353
	/*
	 * 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.)
	 */
354 355
	sta = (struct sta_info *) data;

356 357 358 359 360
	if (sta->sdata->local->quiescing) {
		sta->plink_timer_was_running = true;
		return;
	}

361
	spin_lock_bh(&sta->lock);
362 363
	if (sta->ignore_plink_timer) {
		sta->ignore_plink_timer = false;
364
		spin_unlock_bh(&sta->lock);
365 366
		return;
	}
367 368
	mpl_dbg("Mesh plink timer for %pM fired on state %d\n",
		sta->sta.addr, sta->plink_state);
369 370 371
	reason = 0;
	llid = sta->llid;
	plid = sta->plid;
372
	sdata = sta->sdata;
373 374

	switch (sta->plink_state) {
375 376
	case NL80211_PLINK_OPN_RCVD:
	case NL80211_PLINK_OPN_SNT:
377 378 379
		/* retry timer */
		if (sta->plink_retries < dot11MeshMaxRetries(sdata)) {
			u32 rand;
380 381 382
			mpl_dbg("Mesh plink for %pM (retry, timeout): %d %d\n",
				sta->sta.addr, sta->plink_retries,
				sta->plink_timeout);
383 384 385 386
			get_random_bytes(&rand, sizeof(u32));
			sta->plink_timeout = sta->plink_timeout +
					     rand % sta->plink_timeout;
			++sta->plink_retries;
387
			mod_plink_timer(sta, sta->plink_timeout);
388
			spin_unlock_bh(&sta->lock);
389 390
			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
					    sta->sta.addr, llid, 0, 0);
391 392
			break;
		}
393
		reason = cpu_to_le16(WLAN_REASON_MESH_MAX_RETRIES);
394
		/* fall through on else */
395
	case NL80211_PLINK_CNF_RCVD:
396 397
		/* confirm timer */
		if (!reason)
398
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIRM_TIMEOUT);
399
		sta->plink_state = NL80211_PLINK_HOLDING;
400
		mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
401
		spin_unlock_bh(&sta->lock);
402 403
		mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
				    sta->sta.addr, llid, plid, reason);
404
		break;
405
	case NL80211_PLINK_HOLDING:
406
		/* holding timer */
407
		del_timer(&sta->plink_timer);
408
		mesh_plink_fsm_restart(sta);
409
		spin_unlock_bh(&sta->lock);
410 411
		break;
	default:
412
		spin_unlock_bh(&sta->lock);
413 414 415 416
		break;
	}
}

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
#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

433 434 435 436 437 438 439 440 441 442 443 444
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;
445
	struct ieee80211_sub_if_data *sdata = sta->sdata;
446

J
Johannes Berg 已提交
447
	if (!test_sta_flag(sta, WLAN_STA_AUTH))
448 449
		return -EPERM;

450
	spin_lock_bh(&sta->lock);
451 452
	get_random_bytes(&llid, 2);
	sta->llid = llid;
453
	if (sta->plink_state != NL80211_PLINK_LISTEN) {
454
		spin_unlock_bh(&sta->lock);
455 456
		return -EBUSY;
	}
457
	sta->plink_state = NL80211_PLINK_OPN_SNT;
458
	mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
459
	spin_unlock_bh(&sta->lock);
460 461
	mpl_dbg("Mesh plink: starting establishment with %pM\n",
		sta->sta.addr);
462

463
	return mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
464
				   sta->sta.addr, llid, 0, 0);
465 466 467 468
}

void mesh_plink_block(struct sta_info *sta)
{
469 470 471
	struct ieee80211_sub_if_data *sdata = sta->sdata;
	bool deactivated;

472
	spin_lock_bh(&sta->lock);
473
	deactivated = __mesh_plink_deactivate(sta);
474
	sta->plink_state = NL80211_PLINK_BLOCKED;
475
	spin_unlock_bh(&sta->lock);
476 477 478

	if (deactivated)
		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
479 480 481
}


482
void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata, struct ieee80211_mgmt *mgmt,
483 484 485 486 487
			 size_t len, struct ieee80211_rx_status *rx_status)
{
	struct ieee802_11_elems elems;
	struct sta_info *sta;
	enum plink_event event;
488
	enum ieee80211_self_protected_actioncode ftype;
489
	size_t baselen;
490
	bool deactivated, matches_local = true;
491 492 493
	u8 ie_len;
	u8 *baseaddr;
	__le16 plid, llid, reason;
494 495
#ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
	static const char *mplstates[] = {
496 497 498 499 500 501 502
		[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"
503 504
	};
#endif
505

506 507 508 509
	/* need action_code, aux */
	if (len < IEEE80211_MIN_ACTION_SIZE + 3)
		return;

510 511 512 513 514
	if (is_multicast_ether_addr(mgmt->da)) {
		mpl_dbg("Mesh plink: ignore frame from multicast address");
		return;
	}

515 516 517
	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 ==
518
						WLAN_SP_MESH_PEERING_CONFIRM) {
519
		baseaddr += 4;
520
		baselen += 4;
521 522
	}
	ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
523
	if (!elems.peering) {
524 525 526
		mpl_dbg("Mesh plink: missing necessary peer link ie\n");
		return;
	}
527 528
	if (elems.rsn_len &&
			sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
529 530 531
		mpl_dbg("Mesh plink: can't establish link with secure peer\n");
		return;
	}
532

533 534 535 536 537 538
	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)) {
539 540
		mpl_dbg("Mesh plink: incorrect plink ie length %d %d\n",
		    ftype, ie_len);
541 542 543
		return;
	}

544 545
	if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
				(!elems.mesh_id || !elems.mesh_config)) {
546 547 548 549 550 551
		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.
	 */
552
	memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
553
	if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
554 555
	    (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
		memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
556

557 558
	rcu_read_lock();

559
	sta = sta_info_get(sdata, mgmt->sa);
560
	if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
561
		mpl_dbg("Mesh plink: cls or cnf from unknown peer\n");
562
		rcu_read_unlock();
563 564 565
		return;
	}

566
	if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
567
	    !rssi_threshold_check(sta, sdata)) {
568
		mpl_dbg("Mesh plink: %pM does not meet rssi threshold\n",
569
			mgmt->sa);
570 571 572 573
		rcu_read_unlock();
		return;
	}

J
Johannes Berg 已提交
574
	if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
575 576 577 578 579
		mpl_dbg("Mesh plink: Action frame from non-authed peer\n");
		rcu_read_unlock();
		return;
	}

580
	if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
581
		rcu_read_unlock();
582 583 584 585 586
		return;
	}

	/* Now we will figure out the appropriate event... */
	event = PLINK_UNDEFINED;
587
	if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
588
	    !mesh_matches_local(sdata, &elems)) {
589
		matches_local = false;
590
		switch (ftype) {
591
		case WLAN_SP_MESH_PEERING_OPEN:
592 593
			event = OPN_RJCT;
			break;
594
		case WLAN_SP_MESH_PEERING_CONFIRM:
595 596
			event = CNF_RJCT;
			break;
597
		default:
598 599
			break;
		}
600 601 602 603
	}

	if (!sta && !matches_local) {
		rcu_read_unlock();
604
		reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
605
		llid = 0;
606 607
		mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
				    mgmt->sa, llid, plid, reason);
608
		return;
609
	} else if (!sta) {
610
		/* ftype == WLAN_SP_MESH_PEERING_OPEN */
611 612
		if (!mesh_plink_free_count(sdata)) {
			mpl_dbg("Mesh plink error: no more free plinks\n");
J
Johannes Berg 已提交
613 614 615
			rcu_read_unlock();
			return;
		}
616
		event = OPN_ACPT;
617
	} else if (matches_local) {
618
		switch (ftype) {
619
		case WLAN_SP_MESH_PEERING_OPEN:
620
			if (!mesh_plink_free_count(sdata) ||
621
			    (sta->plid && sta->plid != plid))
622 623 624 625
				event = OPN_IGNR;
			else
				event = OPN_ACPT;
			break;
626
		case WLAN_SP_MESH_PEERING_CONFIRM:
627
			if (!mesh_plink_free_count(sdata) ||
628
			    (sta->llid != llid || sta->plid != plid))
629 630 631 632
				event = CNF_IGNR;
			else
				event = CNF_ACPT;
			break;
633
		case WLAN_SP_MESH_PEERING_CLOSE:
634
			if (sta->plink_state == NL80211_PLINK_ESTAB)
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
				/* 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");
654
			rcu_read_unlock();
655 656
			return;
		}
657 658 659 660
	}

	if (event == OPN_ACPT) {
		/* allocate sta entry if necessary and update info */
661
		sta = mesh_peer_init(sdata, mgmt->sa, &elems);
662 663 664 665 666
		if (!sta) {
			mpl_dbg("Mesh plink: failed to init peer!\n");
			rcu_read_unlock();
			return;
		}
667 668
	}

669 670
	mpl_dbg("Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
		mgmt->sa, mplstates[sta->plink_state],
671 672
		le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
		event);
673
	reason = 0;
674
	spin_lock_bh(&sta->lock);
675 676
	switch (sta->plink_state) {
		/* spin_unlock as soon as state is updated at each case */
677
	case NL80211_PLINK_LISTEN:
678 679 680
		switch (event) {
		case CLS_ACPT:
			mesh_plink_fsm_restart(sta);
681
			spin_unlock_bh(&sta->lock);
682 683
			break;
		case OPN_ACPT:
684
			sta->plink_state = NL80211_PLINK_OPN_RCVD;
685 686 687 688
			sta->plid = plid;
			get_random_bytes(&llid, 2);
			sta->llid = llid;
			mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
689
			spin_unlock_bh(&sta->lock);
690 691 692 693 694 695
			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);
696 697
			break;
		default:
698
			spin_unlock_bh(&sta->lock);
699 700 701 702
			break;
		}
		break;

703
	case NL80211_PLINK_OPN_SNT:
704 705 706
		switch (event) {
		case OPN_RJCT:
		case CNF_RJCT:
707
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
708 709
		case CLS_ACPT:
			if (!reason)
710
				reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
711
			sta->reason = reason;
712
			sta->plink_state = NL80211_PLINK_HOLDING;
713 714 715 716 717
			if (!mod_plink_timer(sta,
					     dot11MeshHoldingTimeout(sdata)))
				sta->ignore_plink_timer = true;

			llid = sta->llid;
718
			spin_unlock_bh(&sta->lock);
719 720 721
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
722 723 724
			break;
		case OPN_ACPT:
			/* retry timer is left untouched */
725
			sta->plink_state = NL80211_PLINK_OPN_RCVD;
726 727
			sta->plid = plid;
			llid = sta->llid;
728
			spin_unlock_bh(&sta->lock);
729 730 731
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CONFIRM,
					    sta->sta.addr, llid, plid, 0);
732 733
			break;
		case CNF_ACPT:
734
			sta->plink_state = NL80211_PLINK_CNF_RCVD;
735 736 737 738
			if (!mod_plink_timer(sta,
					     dot11MeshConfirmTimeout(sdata)))
				sta->ignore_plink_timer = true;

739
			spin_unlock_bh(&sta->lock);
740 741
			break;
		default:
742
			spin_unlock_bh(&sta->lock);
743 744 745 746
			break;
		}
		break;

747
	case NL80211_PLINK_OPN_RCVD:
748 749 750
		switch (event) {
		case OPN_RJCT:
		case CNF_RJCT:
751
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
752 753
		case CLS_ACPT:
			if (!reason)
754
				reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
755
			sta->reason = reason;
756
			sta->plink_state = NL80211_PLINK_HOLDING;
757 758 759 760 761
			if (!mod_plink_timer(sta,
					     dot11MeshHoldingTimeout(sdata)))
				sta->ignore_plink_timer = true;

			llid = sta->llid;
762
			spin_unlock_bh(&sta->lock);
763 764
			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
765 766 767
			break;
		case OPN_ACPT:
			llid = sta->llid;
768
			spin_unlock_bh(&sta->lock);
769 770 771
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CONFIRM,
					    sta->sta.addr, llid, plid, 0);
772 773
			break;
		case CNF_ACPT:
774
			del_timer(&sta->plink_timer);
775
			sta->plink_state = NL80211_PLINK_ESTAB;
776
			spin_unlock_bh(&sta->lock);
777 778
			mesh_plink_inc_estab_count(sdata);
			ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
779 780
			mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
				sta->sta.addr);
781 782
			break;
		default:
783
			spin_unlock_bh(&sta->lock);
784 785 786 787
			break;
		}
		break;

788
	case NL80211_PLINK_CNF_RCVD:
789 790 791
		switch (event) {
		case OPN_RJCT:
		case CNF_RJCT:
792
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
793 794
		case CLS_ACPT:
			if (!reason)
795
				reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
796
			sta->reason = reason;
797
			sta->plink_state = NL80211_PLINK_HOLDING;
798 799 800 801 802
			if (!mod_plink_timer(sta,
					     dot11MeshHoldingTimeout(sdata)))
				sta->ignore_plink_timer = true;

			llid = sta->llid;
803
			spin_unlock_bh(&sta->lock);
804 805 806
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
807
			break;
808
		case OPN_ACPT:
809
			del_timer(&sta->plink_timer);
810
			sta->plink_state = NL80211_PLINK_ESTAB;
811
			spin_unlock_bh(&sta->lock);
812 813
			mesh_plink_inc_estab_count(sdata);
			ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
814 815
			mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
				sta->sta.addr);
816 817 818
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CONFIRM,
					    sta->sta.addr, llid, plid, 0);
819 820
			break;
		default:
821
			spin_unlock_bh(&sta->lock);
822 823 824 825
			break;
		}
		break;

826
	case NL80211_PLINK_ESTAB:
827 828
		switch (event) {
		case CLS_ACPT:
829
			reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
830
			sta->reason = reason;
831
			deactivated = __mesh_plink_deactivate(sta);
832
			sta->plink_state = NL80211_PLINK_HOLDING;
833
			llid = sta->llid;
834
			mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
835
			spin_unlock_bh(&sta->lock);
836 837
			if (deactivated)
				ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
838 839
			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
840 841 842
			break;
		case OPN_ACPT:
			llid = sta->llid;
843
			spin_unlock_bh(&sta->lock);
844 845 846
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CONFIRM,
					    sta->sta.addr, llid, plid, 0);
847 848
			break;
		default:
849
			spin_unlock_bh(&sta->lock);
850 851 852
			break;
		}
		break;
853
	case NL80211_PLINK_HOLDING:
854 855
		switch (event) {
		case CLS_ACPT:
856
			if (del_timer(&sta->plink_timer))
857 858
				sta->ignore_plink_timer = 1;
			mesh_plink_fsm_restart(sta);
859
			spin_unlock_bh(&sta->lock);
860 861 862 863 864 865 866
			break;
		case OPN_ACPT:
		case CNF_ACPT:
		case OPN_RJCT:
		case CNF_RJCT:
			llid = sta->llid;
			reason = sta->reason;
867
			spin_unlock_bh(&sta->lock);
868 869
			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
870 871
			break;
		default:
872
			spin_unlock_bh(&sta->lock);
873 874 875
		}
		break;
	default:
876
		/* should not get here, PLINK_BLOCKED is dealt with at the
D
Daniel Mack 已提交
877
		 * beginning of the function
878
		 */
879
		spin_unlock_bh(&sta->lock);
880 881
		break;
	}
882 883

	rcu_read_unlock();
884
}