mesh_plink.c 25.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
#include "mesh.h"

16 17
#define PLINK_GET_LLID(p) (p + 2)
#define PLINK_GET_PLID(p) (p + 4)
18 19 20 21

#define mod_plink_timer(s, t) (mod_timer(&s->plink_timer, \
				jiffies + HZ * t / 1000))

22 23 24 25 26
#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)
27

28 29
/* We only need a valid sta if user configured a minimum rssi_threshold. */
#define rssi_threshold_check(sta, sdata) \
30
		(sdata->u.mesh.mshcfg.rssi_threshold == 0 ||\
31 32
		(sta && (s8) -ewma_read(&sta->avg_signal) > \
		sdata->u.mesh.mshcfg.rssi_threshold))
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
 * Allocate mesh sta entry and insert into station table
80
 */
81
static struct sta_info *mesh_plink_alloc(struct ieee80211_sub_if_data *sdata,
82
					 u8 *hw_addr)
83 84 85
{
	struct sta_info *sta;

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

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

93 94 95
	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);
96

J
Johannes Berg 已提交
97
	set_sta_flag(sta, WLAN_STA_WME);
98

99 100 101
	return sta;
}

102
/**
103
 * mesh_set_ht_prot_mode - set correct HT protection mode
104
 *
105 106 107 108 109 110
 * Section 9.23.3.5 of IEEE 80211-2012 describes the protection rules for HT
 * mesh STA in a MBSS. Three HT protection modes are supported for now, non-HT
 * mixed mode, 20MHz-protection and no-protection mode. non-HT mixed mode is
 * selected if any non-HT peers are present in our MBSS.  20MHz-protection mode
 * is selected if all peers in our 20/40MHz MBSS support HT and atleast one
 * HT20 peer is present. Otherwise no-protection mode is selected.
111 112 113 114 115 116 117 118 119 120 121 122 123 124
 */
static u32 mesh_set_ht_prot_mode(struct ieee80211_sub_if_data *sdata)
{
	struct ieee80211_local *local = sdata->local;
	struct sta_info *sta;
	u32 changed = 0;
	u16 ht_opmode;
	bool non_ht_sta = false, ht20_sta = false;

	if (local->_oper_channel_type == NL80211_CHAN_NO_HT)
		return 0;

	rcu_read_lock();
	list_for_each_entry_rcu(sta, &local->sta_list, list) {
125 126 127 128 129 130
		if (sdata != sta->sdata ||
		    sta->plink_state != NL80211_PLINK_ESTAB)
			continue;

		switch (sta->ch_type) {
		case NL80211_CHAN_NO_HT:
J
Johannes Berg 已提交
131 132
			mpl_dbg(sdata,
				"mesh_plink %pM: nonHT sta (%pM) is present\n",
133 134 135 136
				sdata->vif.addr, sta->sta.addr);
			non_ht_sta = true;
			goto out;
		case NL80211_CHAN_HT20:
J
Johannes Berg 已提交
137 138
			mpl_dbg(sdata,
				"mesh_plink %pM: HT20 sta (%pM) is present\n",
139 140 141 142
				sdata->vif.addr, sta->sta.addr);
			ht20_sta = true;
		default:
			break;
143 144 145 146 147 148 149 150 151 152 153 154 155 156
		}
	}
out:
	rcu_read_unlock();

	if (non_ht_sta)
		ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED;
	else if (ht20_sta && local->_oper_channel_type > NL80211_CHAN_HT20)
		ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_20MHZ;
	else
		ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;

	if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
		sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
157
		sdata->u.mesh.mshcfg.ht_opmode = ht_opmode;
158
		changed = BSS_CHANGED_HT;
J
Johannes Berg 已提交
159 160
		mpl_dbg(sdata,
			"mesh_plink %pM: protection mode changed to %d\n",
161 162 163 164 165 166
			sdata->vif.addr, ht_opmode);
	}

	return changed;
}

167
/**
168
 * __mesh_plink_deactivate - deactivate mesh peer link
169 170 171 172 173
 *
 * @sta: mesh peer link to deactivate
 *
 * All mesh paths with this peer as next hop will be flushed
 *
174
 * Locking: the caller must hold sta->lock
175
 */
176
static bool __mesh_plink_deactivate(struct sta_info *sta)
177
{
178
	struct ieee80211_sub_if_data *sdata = sta->sdata;
179
	bool deactivated = false;
180

181
	if (sta->plink_state == NL80211_PLINK_ESTAB) {
182
		mesh_plink_dec_estab_count(sdata);
183 184
		deactivated = true;
	}
185
	sta->plink_state = NL80211_PLINK_BLOCKED;
186
	mesh_path_flush_by_nexthop(sta);
187 188

	return deactivated;
189 190
}

J
Johannes Berg 已提交
191
/**
192
 * mesh_plink_deactivate - deactivate mesh peer link
J
Johannes Berg 已提交
193 194 195 196 197 198 199
 *
 * @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)
{
200 201 202
	struct ieee80211_sub_if_data *sdata = sta->sdata;
	bool deactivated;

203
	spin_lock_bh(&sta->lock);
204
	deactivated = __mesh_plink_deactivate(sta);
205 206 207 208
	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);
209
	spin_unlock_bh(&sta->lock);
210 211 212

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

215
static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
216 217
		enum ieee80211_self_protected_actioncode action,
		u8 *da, __le16 llid, __le16 plid, __le16 reason) {
218
	struct ieee80211_local *local = sdata->local;
219
	struct sk_buff *skb;
220 221
	struct ieee80211_mgmt *mgmt;
	bool include_plid = false;
222
	u16 peering_proto = 0;
223 224 225 226
	u8 *pos, ie_len = 4;
	int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.self_prot) +
		      sizeof(mgmt->u.action.u.self_prot);

227
	skb = dev_alloc_skb(local->tx_headroom +
228 229 230 231 232 233 234
			    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) +
235
			    2 + sizeof(struct ieee80211_ht_cap) +
236
			    2 + sizeof(struct ieee80211_ht_operation) +
237 238
			    2 + 8 + /* peering IE */
			    sdata->u.mesh.ie_len);
239 240
	if (!skb)
		return -1;
241
	skb_reserve(skb, local->tx_headroom);
242 243
	mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
	memset(mgmt, 0, hdr_len);
244 245
	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
					  IEEE80211_STYPE_ACTION);
246
	memcpy(mgmt->da, da, ETH_ALEN);
247
	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
248
	memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
249 250
	mgmt->u.action.category = WLAN_CATEGORY_SELF_PROTECTED;
	mgmt->u.action.u.self_prot.action_code = action;
251

252 253 254 255
	if (action != WLAN_SP_MESH_PEERING_CLOSE) {
		/* capability info */
		pos = skb_put(skb, 2);
		memset(pos, 0, 2);
256
		if (action == WLAN_SP_MESH_PEERING_CONFIRM) {
257 258
			/* AID */
			pos = skb_put(skb, 2);
259
			memcpy(pos + 2, &plid, 2);
260
		}
261 262 263 264
		if (ieee80211_add_srates_ie(sdata, skb, true,
					    local->oper_channel->band) ||
		    ieee80211_add_ext_srates_ie(sdata, skb, true,
						local->oper_channel->band) ||
265 266 267 268
		    mesh_add_rsn_ie(skb, sdata) ||
		    mesh_add_meshid_ie(skb, sdata) ||
		    mesh_add_meshconf_ie(skb, sdata))
			return -1;
269 270 271
	} else {	/* WLAN_SP_MESH_PEERING_CLOSE */
		if (mesh_add_meshid_ie(skb, sdata))
			return -1;
272 273
	}

274
	/* Add Mesh Peering Management element */
275
	switch (action) {
276
	case WLAN_SP_MESH_PEERING_OPEN:
277
		break;
278
	case WLAN_SP_MESH_PEERING_CONFIRM:
279
		ie_len += 2;
280 281
		include_plid = true;
		break;
282
	case WLAN_SP_MESH_PEERING_CLOSE:
283 284
		if (plid) {
			ie_len += 2;
285 286
			include_plid = true;
		}
287
		ie_len += 2;	/* reason code */
288
		break;
289 290
	default:
		return -EINVAL;
291 292
	}

293 294 295
	if (WARN_ON(skb_tailroom(skb) < 2 + ie_len))
		return -ENOMEM;

296
	pos = skb_put(skb, 2 + ie_len);
297
	*pos++ = WLAN_EID_PEER_MGMT;
298
	*pos++ = ie_len;
299 300
	memcpy(pos, &peering_proto, 2);
	pos += 2;
301
	memcpy(pos, &llid, 2);
302
	pos += 2;
303 304
	if (include_plid) {
		memcpy(pos, &plid, 2);
305
		pos += 2;
306
	}
307
	if (action == WLAN_SP_MESH_PEERING_CLOSE) {
308
		memcpy(pos, &reason, 2);
309
		pos += 2;
310
	}
311 312 313

	if (action != WLAN_SP_MESH_PEERING_CLOSE) {
		if (mesh_add_ht_cap_ie(skb, sdata) ||
314
		    mesh_add_ht_oper_ie(skb, sdata))
315 316 317
			return -1;
	}

318 319
	if (mesh_add_vendor_ies(skb, sdata))
		return -1;
320

321
	ieee80211_tx_skb(sdata, skb);
322 323 324
	return 0;
}

325 326
/**
 * mesh_peer_init - initialize new mesh peer and return resulting sta_info
327 328 329 330 331 332 333 334
 *
 * @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,
335
				       u8 *addr,
336
				       struct ieee802_11_elems *elems)
337
{
338
	struct ieee80211_local *local = sdata->local;
339
	enum ieee80211_band band = local->oper_channel->band;
340
	struct ieee80211_supported_band *sband;
341
	u32 rates, basic_rates = 0;
342
	struct sta_info *sta;
343
	bool insert = false;
344

345 346
	sband = local->hw.wiphy->bands[band];
	rates = ieee80211_sta_get_rates(local, elems, band, &basic_rates);
347

348
	sta = sta_info_get(sdata, addr);
349
	if (!sta) {
350 351 352 353 354 355 356 357 358
		/* Userspace handles peer allocation when security is enabled */
		if (sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED) {
			cfg80211_notify_new_peer_candidate(sdata->dev, addr,
							   elems->ie_start,
							   elems->total_len,
							   GFP_ATOMIC);
			return NULL;
		}

359
		sta = mesh_plink_alloc(sdata, addr);
360
		if (!sta)
361
			return NULL;
362
		insert = true;
363 364
	}

365
	spin_lock_bh(&sta->lock);
366
	sta->last_rx = jiffies;
367 368 369 370 371
	if (sta->plink_state == NL80211_PLINK_ESTAB) {
		spin_unlock_bh(&sta->lock);
		return sta;
	}

372
	sta->sta.supp_rates[band] = rates;
373 374
	if (elems->ht_cap_elem &&
	    sdata->local->_oper_channel_type != NL80211_CHAN_NO_HT)
375 376 377 378 379 380
		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));

381
	if (elems->ht_operation) {
382 383 384 385
		if (!(elems->ht_operation->ht_param &
		      IEEE80211_HT_PARAM_CHAN_WIDTH_ANY))
			sta->sta.ht_cap.cap &=
					    ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
386 387 388
		sta->ch_type =
			ieee80211_ht_oper_to_channel_type(elems->ht_operation);
	}
389

390 391 392
	rate_control_rate_init(sta);
	spin_unlock_bh(&sta->lock);

393 394 395
	if (insert && sta_info_insert(sta))
		return NULL;

396 397 398
	return sta;
}

399 400
void mesh_neighbour_update(struct ieee80211_sub_if_data *sdata,
			   u8 *hw_addr,
401 402 403 404 405
			   struct ieee802_11_elems *elems)
{
	struct sta_info *sta;

	rcu_read_lock();
406
	sta = mesh_peer_init(sdata, hw_addr, elems);
407 408 409
	if (!sta)
		goto out;

410
	if (mesh_peer_accepts_plinks(elems) &&
411 412 413 414
	    sta->plink_state == NL80211_PLINK_LISTEN &&
	    sdata->u.mesh.accepting_plinks &&
	    sdata->u.mesh.mshcfg.auto_open_plinks &&
	    rssi_threshold_check(sta, sdata))
415 416
		mesh_plink_open(sta);

417
out:
418
	rcu_read_unlock();
419 420 421 422 423 424 425 426
}

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

427 428 429 430 431
	/*
	 * 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.)
	 */
432 433
	sta = (struct sta_info *) data;

434 435 436 437 438
	if (sta->sdata->local->quiescing) {
		sta->plink_timer_was_running = true;
		return;
	}

439
	spin_lock_bh(&sta->lock);
440 441
	if (sta->ignore_plink_timer) {
		sta->ignore_plink_timer = false;
442
		spin_unlock_bh(&sta->lock);
443 444
		return;
	}
J
Johannes Berg 已提交
445 446
	mpl_dbg(sta->sdata,
		"Mesh plink timer for %pM fired on state %d\n",
447
		sta->sta.addr, sta->plink_state);
448 449 450
	reason = 0;
	llid = sta->llid;
	plid = sta->plid;
451
	sdata = sta->sdata;
452 453

	switch (sta->plink_state) {
454 455
	case NL80211_PLINK_OPN_RCVD:
	case NL80211_PLINK_OPN_SNT:
456 457 458
		/* retry timer */
		if (sta->plink_retries < dot11MeshMaxRetries(sdata)) {
			u32 rand;
J
Johannes Berg 已提交
459 460
			mpl_dbg(sta->sdata,
				"Mesh plink for %pM (retry, timeout): %d %d\n",
461 462
				sta->sta.addr, sta->plink_retries,
				sta->plink_timeout);
463 464 465 466
			get_random_bytes(&rand, sizeof(u32));
			sta->plink_timeout = sta->plink_timeout +
					     rand % sta->plink_timeout;
			++sta->plink_retries;
467
			mod_plink_timer(sta, sta->plink_timeout);
468
			spin_unlock_bh(&sta->lock);
469 470
			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
					    sta->sta.addr, llid, 0, 0);
471 472
			break;
		}
473
		reason = cpu_to_le16(WLAN_REASON_MESH_MAX_RETRIES);
474
		/* fall through on else */
475
	case NL80211_PLINK_CNF_RCVD:
476 477
		/* confirm timer */
		if (!reason)
478
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIRM_TIMEOUT);
479
		sta->plink_state = NL80211_PLINK_HOLDING;
480
		mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
481
		spin_unlock_bh(&sta->lock);
482 483
		mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
				    sta->sta.addr, llid, plid, reason);
484
		break;
485
	case NL80211_PLINK_HOLDING:
486
		/* holding timer */
487
		del_timer(&sta->plink_timer);
488
		mesh_plink_fsm_restart(sta);
489
		spin_unlock_bh(&sta->lock);
490 491
		break;
	default:
492
		spin_unlock_bh(&sta->lock);
493 494 495 496
		break;
	}
}

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
#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

513 514 515 516 517 518 519 520 521 522 523 524
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;
525
	struct ieee80211_sub_if_data *sdata = sta->sdata;
526

J
Johannes Berg 已提交
527
	if (!test_sta_flag(sta, WLAN_STA_AUTH))
528 529
		return -EPERM;

530
	spin_lock_bh(&sta->lock);
531 532
	get_random_bytes(&llid, 2);
	sta->llid = llid;
533
	if (sta->plink_state != NL80211_PLINK_LISTEN) {
534
		spin_unlock_bh(&sta->lock);
535 536
		return -EBUSY;
	}
537
	sta->plink_state = NL80211_PLINK_OPN_SNT;
538
	mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
539
	spin_unlock_bh(&sta->lock);
J
Johannes Berg 已提交
540 541
	mpl_dbg(sdata,
		"Mesh plink: starting establishment with %pM\n",
542
		sta->sta.addr);
543

544
	return mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
545
				   sta->sta.addr, llid, 0, 0);
546 547 548 549
}

void mesh_plink_block(struct sta_info *sta)
{
550 551 552
	struct ieee80211_sub_if_data *sdata = sta->sdata;
	bool deactivated;

553
	spin_lock_bh(&sta->lock);
554
	deactivated = __mesh_plink_deactivate(sta);
555
	sta->plink_state = NL80211_PLINK_BLOCKED;
556
	spin_unlock_bh(&sta->lock);
557 558 559

	if (deactivated)
		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
560 561 562
}


563
void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata, struct ieee80211_mgmt *mgmt,
564 565 566 567 568
			 size_t len, struct ieee80211_rx_status *rx_status)
{
	struct ieee802_11_elems elems;
	struct sta_info *sta;
	enum plink_event event;
569
	enum ieee80211_self_protected_actioncode ftype;
570
	size_t baselen;
571
	bool matches_local = true;
572 573
	u8 ie_len;
	u8 *baseaddr;
574
	u32 changed = 0;
575
	__le16 plid, llid, reason;
576
	static const char *mplstates[] = {
577 578 579 580 581 582 583
		[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"
584
	};
585

586 587 588 589
	/* need action_code, aux */
	if (len < IEEE80211_MIN_ACTION_SIZE + 3)
		return;

590
	if (is_multicast_ether_addr(mgmt->da)) {
J
Johannes Berg 已提交
591 592
		mpl_dbg(sdata,
			"Mesh plink: ignore frame from multicast address\n");
593 594 595
		return;
	}

596 597 598
	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 ==
599
						WLAN_SP_MESH_PEERING_CONFIRM) {
600
		baseaddr += 4;
601
		baselen += 4;
602 603
	}
	ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
604
	if (!elems.peering) {
J
Johannes Berg 已提交
605 606
		mpl_dbg(sdata,
			"Mesh plink: missing necessary peer link ie\n");
607 608
		return;
	}
609 610
	if (elems.rsn_len &&
			sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
J
Johannes Berg 已提交
611 612
		mpl_dbg(sdata,
			"Mesh plink: can't establish link with secure peer\n");
613 614
		return;
	}
615

616 617 618 619 620 621
	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)) {
J
Johannes Berg 已提交
622 623 624
		mpl_dbg(sdata,
			"Mesh plink: incorrect plink ie length %d %d\n",
			ftype, ie_len);
625 626 627
		return;
	}

628 629
	if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
				(!elems.mesh_id || !elems.mesh_config)) {
J
Johannes Berg 已提交
630
		mpl_dbg(sdata, "Mesh plink: missing necessary ie\n");
631 632 633 634 635
		return;
	}
	/* Note the lines below are correct, the llid in the frame is the plid
	 * from the point of view of this host.
	 */
636
	memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
637
	if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
638 639
	    (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
		memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
640

641 642
	rcu_read_lock();

643
	sta = sta_info_get(sdata, mgmt->sa);
644
	if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
J
Johannes Berg 已提交
645
		mpl_dbg(sdata, "Mesh plink: cls or cnf from unknown peer\n");
646
		rcu_read_unlock();
647 648 649
		return;
	}

650
	if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
651
	    !rssi_threshold_check(sta, sdata)) {
J
Johannes Berg 已提交
652
		mpl_dbg(sdata, "Mesh plink: %pM does not meet rssi threshold\n",
653
			mgmt->sa);
654 655 656 657
		rcu_read_unlock();
		return;
	}

J
Johannes Berg 已提交
658
	if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
J
Johannes Berg 已提交
659
		mpl_dbg(sdata, "Mesh plink: Action frame from non-authed peer\n");
660 661 662 663
		rcu_read_unlock();
		return;
	}

664
	if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
665
		rcu_read_unlock();
666 667 668 669 670
		return;
	}

	/* Now we will figure out the appropriate event... */
	event = PLINK_UNDEFINED;
671
	if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
672
	    !mesh_matches_local(sdata, &elems)) {
673
		matches_local = false;
674
		switch (ftype) {
675
		case WLAN_SP_MESH_PEERING_OPEN:
676 677
			event = OPN_RJCT;
			break;
678
		case WLAN_SP_MESH_PEERING_CONFIRM:
679 680
			event = CNF_RJCT;
			break;
681
		default:
682 683
			break;
		}
684 685 686 687
	}

	if (!sta && !matches_local) {
		rcu_read_unlock();
688
		reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
689
		llid = 0;
690 691
		mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
				    mgmt->sa, llid, plid, reason);
692
		return;
693
	} else if (!sta) {
694
		/* ftype == WLAN_SP_MESH_PEERING_OPEN */
695
		if (!mesh_plink_free_count(sdata)) {
J
Johannes Berg 已提交
696
			mpl_dbg(sdata, "Mesh plink error: no more free plinks\n");
J
Johannes Berg 已提交
697 698 699
			rcu_read_unlock();
			return;
		}
700
		event = OPN_ACPT;
701
	} else if (matches_local) {
702
		switch (ftype) {
703
		case WLAN_SP_MESH_PEERING_OPEN:
704
			if (!mesh_plink_free_count(sdata) ||
705
			    (sta->plid && sta->plid != plid))
706 707 708 709
				event = OPN_IGNR;
			else
				event = OPN_ACPT;
			break;
710
		case WLAN_SP_MESH_PEERING_CONFIRM:
711
			if (!mesh_plink_free_count(sdata) ||
712
			    (sta->llid != llid || sta->plid != plid))
713 714 715 716
				event = CNF_IGNR;
			else
				event = CNF_ACPT;
			break;
717
		case WLAN_SP_MESH_PEERING_CLOSE:
718
			if (sta->plink_state == NL80211_PLINK_ESTAB)
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
				/* 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:
J
Johannes Berg 已提交
737
			mpl_dbg(sdata, "Mesh plink: unknown frame subtype\n");
738
			rcu_read_unlock();
739 740
			return;
		}
741 742 743 744
	}

	if (event == OPN_ACPT) {
		/* allocate sta entry if necessary and update info */
745
		sta = mesh_peer_init(sdata, mgmt->sa, &elems);
746
		if (!sta) {
J
Johannes Berg 已提交
747
			mpl_dbg(sdata, "Mesh plink: failed to init peer!\n");
748 749 750
			rcu_read_unlock();
			return;
		}
751 752
	}

J
Johannes Berg 已提交
753 754
	mpl_dbg(sdata,
		"Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
755
		mgmt->sa, mplstates[sta->plink_state],
756 757
		le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
		event);
758
	reason = 0;
759
	spin_lock_bh(&sta->lock);
760 761
	switch (sta->plink_state) {
		/* spin_unlock as soon as state is updated at each case */
762
	case NL80211_PLINK_LISTEN:
763 764 765
		switch (event) {
		case CLS_ACPT:
			mesh_plink_fsm_restart(sta);
766
			spin_unlock_bh(&sta->lock);
767 768
			break;
		case OPN_ACPT:
769
			sta->plink_state = NL80211_PLINK_OPN_RCVD;
770 771 772 773
			sta->plid = plid;
			get_random_bytes(&llid, 2);
			sta->llid = llid;
			mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
774
			spin_unlock_bh(&sta->lock);
775 776 777 778 779 780
			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);
781 782
			break;
		default:
783
			spin_unlock_bh(&sta->lock);
784 785 786 787
			break;
		}
		break;

788
	case NL80211_PLINK_OPN_SNT:
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 808 809
			break;
		case OPN_ACPT:
			/* retry timer is left untouched */
810
			sta->plink_state = NL80211_PLINK_OPN_RCVD;
811 812
			sta->plid = plid;
			llid = sta->llid;
813
			spin_unlock_bh(&sta->lock);
814 815 816
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CONFIRM,
					    sta->sta.addr, llid, plid, 0);
817 818
			break;
		case CNF_ACPT:
819
			sta->plink_state = NL80211_PLINK_CNF_RCVD;
820 821 822 823
			if (!mod_plink_timer(sta,
					     dot11MeshConfirmTimeout(sdata)))
				sta->ignore_plink_timer = true;

824
			spin_unlock_bh(&sta->lock);
825 826
			break;
		default:
827
			spin_unlock_bh(&sta->lock);
828 829 830 831
			break;
		}
		break;

832
	case NL80211_PLINK_OPN_RCVD:
833 834 835
		switch (event) {
		case OPN_RJCT:
		case CNF_RJCT:
836
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
837 838
		case CLS_ACPT:
			if (!reason)
839
				reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
840
			sta->reason = reason;
841
			sta->plink_state = NL80211_PLINK_HOLDING;
842 843 844 845 846
			if (!mod_plink_timer(sta,
					     dot11MeshHoldingTimeout(sdata)))
				sta->ignore_plink_timer = true;

			llid = sta->llid;
847
			spin_unlock_bh(&sta->lock);
848 849
			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
850 851 852
			break;
		case OPN_ACPT:
			llid = sta->llid;
853
			spin_unlock_bh(&sta->lock);
854 855 856
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CONFIRM,
					    sta->sta.addr, llid, plid, 0);
857 858
			break;
		case CNF_ACPT:
859
			del_timer(&sta->plink_timer);
860
			sta->plink_state = NL80211_PLINK_ESTAB;
861
			spin_unlock_bh(&sta->lock);
862
			mesh_plink_inc_estab_count(sdata);
863 864
			changed |= mesh_set_ht_prot_mode(sdata);
			changed |= BSS_CHANGED_BEACON;
J
Johannes Berg 已提交
865
			mpl_dbg(sdata, "Mesh plink with %pM ESTABLISHED\n",
866
				sta->sta.addr);
867 868
			break;
		default:
869
			spin_unlock_bh(&sta->lock);
870 871 872 873
			break;
		}
		break;

874
	case NL80211_PLINK_CNF_RCVD:
875 876 877
		switch (event) {
		case OPN_RJCT:
		case CNF_RJCT:
878
			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
879 880
		case CLS_ACPT:
			if (!reason)
881
				reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
882
			sta->reason = reason;
883
			sta->plink_state = NL80211_PLINK_HOLDING;
884 885 886 887 888
			if (!mod_plink_timer(sta,
					     dot11MeshHoldingTimeout(sdata)))
				sta->ignore_plink_timer = true;

			llid = sta->llid;
889
			spin_unlock_bh(&sta->lock);
890 891 892
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
893
			break;
894
		case OPN_ACPT:
895
			del_timer(&sta->plink_timer);
896
			sta->plink_state = NL80211_PLINK_ESTAB;
897
			spin_unlock_bh(&sta->lock);
898
			mesh_plink_inc_estab_count(sdata);
899 900
			changed |= mesh_set_ht_prot_mode(sdata);
			changed |= BSS_CHANGED_BEACON;
J
Johannes Berg 已提交
901
			mpl_dbg(sdata, "Mesh plink with %pM ESTABLISHED\n",
902
				sta->sta.addr);
903 904 905
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CONFIRM,
					    sta->sta.addr, llid, plid, 0);
906 907
			break;
		default:
908
			spin_unlock_bh(&sta->lock);
909 910 911 912
			break;
		}
		break;

913
	case NL80211_PLINK_ESTAB:
914 915
		switch (event) {
		case CLS_ACPT:
916
			reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
917
			sta->reason = reason;
918
			__mesh_plink_deactivate(sta);
919
			sta->plink_state = NL80211_PLINK_HOLDING;
920
			llid = sta->llid;
921
			mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
922
			spin_unlock_bh(&sta->lock);
923 924
			changed |= mesh_set_ht_prot_mode(sdata);
			changed |= BSS_CHANGED_BEACON;
925 926
			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
927 928 929
			break;
		case OPN_ACPT:
			llid = sta->llid;
930
			spin_unlock_bh(&sta->lock);
931 932 933
			mesh_plink_frame_tx(sdata,
					    WLAN_SP_MESH_PEERING_CONFIRM,
					    sta->sta.addr, llid, plid, 0);
934 935
			break;
		default:
936
			spin_unlock_bh(&sta->lock);
937 938 939
			break;
		}
		break;
940
	case NL80211_PLINK_HOLDING:
941 942
		switch (event) {
		case CLS_ACPT:
943
			if (del_timer(&sta->plink_timer))
944 945
				sta->ignore_plink_timer = 1;
			mesh_plink_fsm_restart(sta);
946
			spin_unlock_bh(&sta->lock);
947 948 949 950 951 952 953
			break;
		case OPN_ACPT:
		case CNF_ACPT:
		case OPN_RJCT:
		case CNF_RJCT:
			llid = sta->llid;
			reason = sta->reason;
954
			spin_unlock_bh(&sta->lock);
955 956
			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
					    sta->sta.addr, llid, plid, reason);
957 958
			break;
		default:
959
			spin_unlock_bh(&sta->lock);
960 961 962
		}
		break;
	default:
963
		/* should not get here, PLINK_BLOCKED is dealt with at the
D
Daniel Mack 已提交
964
		 * beginning of the function
965
		 */
966
		spin_unlock_bh(&sta->lock);
967 968
		break;
	}
969 970

	rcu_read_unlock();
971 972 973

	if (changed)
		ieee80211_bss_info_change_notify(sdata, changed);
974
}