mesh_plink.c revision 52ac8c488774b7ac6d1b0ed4c323b5de90d39d6e
1/*
2 * Copyright (c) 2008, 2009 open80211s Ltd.
3 * Author:     Luis Carlos Cobo <luisca@cozybit.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9#include <linux/gfp.h>
10#include <linux/kernel.h>
11#include <linux/random.h>
12#include "ieee80211_i.h"
13#include "rate.h"
14#include "mesh.h"
15
16#define PLINK_GET_LLID(p) (p + 2)
17#define PLINK_GET_PLID(p) (p + 4)
18
19#define mod_plink_timer(s, t) (mod_timer(&s->plink_timer, \
20				jiffies + HZ * t / 1000))
21
22/* We only need a valid sta if user configured a minimum rssi_threshold. */
23#define rssi_threshold_check(sta, sdata) \
24		(sdata->u.mesh.mshcfg.rssi_threshold == 0 ||\
25		(sta && (s8) -ewma_read(&sta->avg_signal) > \
26		sdata->u.mesh.mshcfg.rssi_threshold))
27
28enum plink_event {
29	PLINK_UNDEFINED,
30	OPN_ACPT,
31	OPN_RJCT,
32	OPN_IGNR,
33	CNF_ACPT,
34	CNF_RJCT,
35	CNF_IGNR,
36	CLS_ACPT,
37	CLS_IGNR
38};
39
40static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
41			       enum ieee80211_self_protected_actioncode action,
42			       u8 *da, __le16 llid, __le16 plid, __le16 reason);
43
44/**
45 * mesh_plink_fsm_restart - restart a mesh peer link finite state machine
46 *
47 * @sta: mesh peer link to restart
48 *
49 * Locking: this function must be called holding sta->lock
50 */
51static inline void mesh_plink_fsm_restart(struct sta_info *sta)
52{
53	sta->plink_state = NL80211_PLINK_LISTEN;
54	sta->llid = sta->plid = sta->reason = 0;
55	sta->plink_retries = 0;
56}
57
58/*
59 * mesh_set_short_slot_time - enable / disable ERP short slot time.
60 *
61 * The standard indirectly mandates mesh STAs to turn off short slot time by
62 * disallowing advertising this (802.11-2012 8.4.1.4), but that doesn't mean we
63 * can't be sneaky about it. Enable short slot time if all mesh STAs in the
64 * MBSS support ERP rates.
65 *
66 * Returns BSS_CHANGED_ERP_SLOT or 0 for no change.
67 */
68static u32 mesh_set_short_slot_time(struct ieee80211_sub_if_data *sdata)
69{
70	struct ieee80211_local *local = sdata->local;
71	enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
72	struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
73	struct sta_info *sta;
74	u32 erp_rates = 0, changed = 0;
75	int i;
76	bool short_slot = false;
77
78	if (band == IEEE80211_BAND_5GHZ) {
79		/* (IEEE 802.11-2012 19.4.5) */
80		short_slot = true;
81		goto out;
82	} else if (band != IEEE80211_BAND_2GHZ ||
83		   (band == IEEE80211_BAND_2GHZ &&
84		    local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
85		goto out;
86
87	for (i = 0; i < sband->n_bitrates; i++)
88		if (sband->bitrates[i].flags & IEEE80211_RATE_ERP_G)
89			erp_rates |= BIT(i);
90
91	if (!erp_rates)
92		goto out;
93
94	rcu_read_lock();
95	list_for_each_entry_rcu(sta, &local->sta_list, list) {
96		if (sdata != sta->sdata ||
97		    sta->plink_state != NL80211_PLINK_ESTAB)
98			continue;
99
100		short_slot = false;
101		if (erp_rates & sta->sta.supp_rates[band])
102			short_slot = true;
103		 else
104			break;
105	}
106	rcu_read_unlock();
107
108out:
109	if (sdata->vif.bss_conf.use_short_slot != short_slot) {
110		sdata->vif.bss_conf.use_short_slot = short_slot;
111		changed = BSS_CHANGED_ERP_SLOT;
112		mpl_dbg(sdata, "mesh_plink %pM: ERP short slot time %d\n",
113			sdata->vif.addr, short_slot);
114	}
115	return changed;
116}
117
118/**
119 * mesh_set_ht_prot_mode - set correct HT protection mode
120 *
121 * Section 9.23.3.5 of IEEE 80211-2012 describes the protection rules for HT
122 * mesh STA in a MBSS. Three HT protection modes are supported for now, non-HT
123 * mixed mode, 20MHz-protection and no-protection mode. non-HT mixed mode is
124 * selected if any non-HT peers are present in our MBSS.  20MHz-protection mode
125 * is selected if all peers in our 20/40MHz MBSS support HT and atleast one
126 * HT20 peer is present. Otherwise no-protection mode is selected.
127 */
128static u32 mesh_set_ht_prot_mode(struct ieee80211_sub_if_data *sdata)
129{
130	struct ieee80211_local *local = sdata->local;
131	struct sta_info *sta;
132	u16 ht_opmode;
133	bool non_ht_sta = false, ht20_sta = false;
134
135	if (sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT)
136		return 0;
137
138	rcu_read_lock();
139	list_for_each_entry_rcu(sta, &local->sta_list, list) {
140		if (sdata != sta->sdata ||
141		    sta->plink_state != NL80211_PLINK_ESTAB)
142			continue;
143
144		if (sta->sta.bandwidth > IEEE80211_STA_RX_BW_20)
145			continue;
146
147		if (!sta->sta.ht_cap.ht_supported) {
148			mpl_dbg(sdata, "nonHT sta (%pM) is present\n",
149				       sta->sta.addr);
150			non_ht_sta = true;
151			break;
152		}
153
154		mpl_dbg(sdata, "HT20 sta (%pM) is present\n", sta->sta.addr);
155		ht20_sta = true;
156	}
157	rcu_read_unlock();
158
159	if (non_ht_sta)
160		ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED;
161	else if (ht20_sta &&
162		 sdata->vif.bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
163		ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_20MHZ;
164	else
165		ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
166
167	if (sdata->vif.bss_conf.ht_operation_mode == ht_opmode)
168		return 0;
169
170	sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
171	sdata->u.mesh.mshcfg.ht_opmode = ht_opmode;
172	mpl_dbg(sdata, "selected new HT protection mode %d\n", ht_opmode);
173	return BSS_CHANGED_HT;
174}
175
176/**
177 * __mesh_plink_deactivate - deactivate mesh peer link
178 *
179 * @sta: mesh peer link to deactivate
180 *
181 * All mesh paths with this peer as next hop will be flushed
182 * Returns beacon changed flag if the beacon content changed.
183 *
184 * Locking: the caller must hold sta->lock
185 */
186static u32 __mesh_plink_deactivate(struct sta_info *sta)
187{
188	struct ieee80211_sub_if_data *sdata = sta->sdata;
189	u32 changed = 0;
190
191	if (sta->plink_state == NL80211_PLINK_ESTAB)
192		changed = mesh_plink_dec_estab_count(sdata);
193	sta->plink_state = NL80211_PLINK_BLOCKED;
194	mesh_path_flush_by_nexthop(sta);
195
196	ieee80211_mps_sta_status_update(sta);
197	changed |= ieee80211_mps_local_status_update(sdata);
198
199	return changed;
200}
201
202/**
203 * mesh_plink_deactivate - deactivate mesh peer link
204 *
205 * @sta: mesh peer link to deactivate
206 *
207 * All mesh paths with this peer as next hop will be flushed
208 */
209u32 mesh_plink_deactivate(struct sta_info *sta)
210{
211	struct ieee80211_sub_if_data *sdata = sta->sdata;
212	u32 changed;
213
214	spin_lock_bh(&sta->lock);
215	changed = __mesh_plink_deactivate(sta);
216	sta->reason = cpu_to_le16(WLAN_REASON_MESH_PEER_CANCELED);
217	mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
218			    sta->sta.addr, sta->llid, sta->plid,
219			    sta->reason);
220	spin_unlock_bh(&sta->lock);
221
222	return changed;
223}
224
225static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
226			       enum ieee80211_self_protected_actioncode action,
227			       u8 *da, __le16 llid, __le16 plid, __le16 reason)
228{
229	struct ieee80211_local *local = sdata->local;
230	struct sk_buff *skb;
231	struct ieee80211_tx_info *info;
232	struct ieee80211_mgmt *mgmt;
233	bool include_plid = false;
234	u16 peering_proto = 0;
235	u8 *pos, ie_len = 4;
236	int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.self_prot) +
237		      sizeof(mgmt->u.action.u.self_prot);
238	int err = -ENOMEM;
239
240	skb = dev_alloc_skb(local->tx_headroom +
241			    hdr_len +
242			    2 + /* capability info */
243			    2 + /* AID */
244			    2 + 8 + /* supported rates */
245			    2 + (IEEE80211_MAX_SUPP_RATES - 8) +
246			    2 + sdata->u.mesh.mesh_id_len +
247			    2 + sizeof(struct ieee80211_meshconf_ie) +
248			    2 + sizeof(struct ieee80211_ht_cap) +
249			    2 + sizeof(struct ieee80211_ht_operation) +
250			    2 + 8 + /* peering IE */
251			    sdata->u.mesh.ie_len);
252	if (!skb)
253		return -1;
254	info = IEEE80211_SKB_CB(skb);
255	skb_reserve(skb, local->tx_headroom);
256	mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
257	memset(mgmt, 0, hdr_len);
258	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
259					  IEEE80211_STYPE_ACTION);
260	memcpy(mgmt->da, da, ETH_ALEN);
261	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
262	memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
263	mgmt->u.action.category = WLAN_CATEGORY_SELF_PROTECTED;
264	mgmt->u.action.u.self_prot.action_code = action;
265
266	if (action != WLAN_SP_MESH_PEERING_CLOSE) {
267		enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
268
269		/* capability info */
270		pos = skb_put(skb, 2);
271		memset(pos, 0, 2);
272		if (action == WLAN_SP_MESH_PEERING_CONFIRM) {
273			/* AID */
274			pos = skb_put(skb, 2);
275			memcpy(pos + 2, &plid, 2);
276		}
277		if (ieee80211_add_srates_ie(sdata, skb, true, band) ||
278		    ieee80211_add_ext_srates_ie(sdata, skb, true, band) ||
279		    mesh_add_rsn_ie(sdata, skb) ||
280		    mesh_add_meshid_ie(sdata, skb) ||
281		    mesh_add_meshconf_ie(sdata, skb))
282			goto free;
283	} else {	/* WLAN_SP_MESH_PEERING_CLOSE */
284		info->flags |= IEEE80211_TX_CTL_NO_ACK;
285		if (mesh_add_meshid_ie(sdata, skb))
286			goto free;
287	}
288
289	/* Add Mesh Peering Management element */
290	switch (action) {
291	case WLAN_SP_MESH_PEERING_OPEN:
292		break;
293	case WLAN_SP_MESH_PEERING_CONFIRM:
294		ie_len += 2;
295		include_plid = true;
296		break;
297	case WLAN_SP_MESH_PEERING_CLOSE:
298		if (plid) {
299			ie_len += 2;
300			include_plid = true;
301		}
302		ie_len += 2;	/* reason code */
303		break;
304	default:
305		err = -EINVAL;
306		goto free;
307	}
308
309	if (WARN_ON(skb_tailroom(skb) < 2 + ie_len))
310		goto free;
311
312	pos = skb_put(skb, 2 + ie_len);
313	*pos++ = WLAN_EID_PEER_MGMT;
314	*pos++ = ie_len;
315	memcpy(pos, &peering_proto, 2);
316	pos += 2;
317	memcpy(pos, &llid, 2);
318	pos += 2;
319	if (include_plid) {
320		memcpy(pos, &plid, 2);
321		pos += 2;
322	}
323	if (action == WLAN_SP_MESH_PEERING_CLOSE) {
324		memcpy(pos, &reason, 2);
325		pos += 2;
326	}
327
328	if (action != WLAN_SP_MESH_PEERING_CLOSE) {
329		if (mesh_add_ht_cap_ie(sdata, skb) ||
330		    mesh_add_ht_oper_ie(sdata, skb))
331			goto free;
332	}
333
334	if (mesh_add_vendor_ies(sdata, skb))
335		goto free;
336
337	ieee80211_tx_skb(sdata, skb);
338	return 0;
339free:
340	kfree_skb(skb);
341	return err;
342}
343
344static void mesh_sta_info_init(struct ieee80211_sub_if_data *sdata,
345			       struct sta_info *sta,
346			       struct ieee802_11_elems *elems, bool insert)
347{
348	struct ieee80211_local *local = sdata->local;
349	enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
350	struct ieee80211_supported_band *sband;
351	u32 rates, basic_rates = 0, changed = 0;
352
353	sband = local->hw.wiphy->bands[band];
354	rates = ieee80211_sta_get_rates(local, elems, band, &basic_rates);
355
356	spin_lock_bh(&sta->lock);
357	sta->last_rx = jiffies;
358
359	/* rates and capabilities don't change during peering */
360	if (sta->plink_state == NL80211_PLINK_ESTAB)
361		goto out;
362
363	if (sta->sta.supp_rates[band] != rates)
364		changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
365	sta->sta.supp_rates[band] = rates;
366
367	if (ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
368					      elems->ht_cap_elem, sta))
369		changed |= IEEE80211_RC_BW_CHANGED;
370
371	/* HT peer is operating 20MHz-only */
372	if (elems->ht_operation &&
373	    !(elems->ht_operation->ht_param &
374	      IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
375		if (sta->sta.bandwidth != IEEE80211_STA_RX_BW_20)
376			changed |= IEEE80211_RC_BW_CHANGED;
377		sta->sta.bandwidth = IEEE80211_STA_RX_BW_20;
378	}
379
380	if (insert)
381		rate_control_rate_init(sta);
382	else
383		rate_control_rate_update(local, sband, sta, changed);
384out:
385	spin_unlock_bh(&sta->lock);
386}
387
388static struct sta_info *
389__mesh_sta_info_alloc(struct ieee80211_sub_if_data *sdata, u8 *hw_addr)
390{
391	struct sta_info *sta;
392
393	if (sdata->local->num_sta >= MESH_MAX_PLINKS)
394		return NULL;
395
396	sta = sta_info_alloc(sdata, hw_addr, GFP_KERNEL);
397	if (!sta)
398		return NULL;
399
400	sta->plink_state = NL80211_PLINK_LISTEN;
401	init_timer(&sta->plink_timer);
402
403	sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
404	sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
405	sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
406
407	set_sta_flag(sta, WLAN_STA_WME);
408
409	return sta;
410}
411
412static struct sta_info *
413mesh_sta_info_alloc(struct ieee80211_sub_if_data *sdata, u8 *addr,
414		    struct ieee802_11_elems *elems)
415{
416	struct sta_info *sta = NULL;
417
418	/* Userspace handles peer allocation when security is enabled */
419	if (sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED)
420		cfg80211_notify_new_peer_candidate(sdata->dev, addr,
421						   elems->ie_start,
422						   elems->total_len,
423						   GFP_KERNEL);
424	else
425		sta = __mesh_sta_info_alloc(sdata, addr);
426
427	return sta;
428}
429
430/*
431 * mesh_sta_info_get - return mesh sta info entry for @addr.
432 *
433 * @sdata: local meshif
434 * @addr: peer's address
435 * @elems: IEs from beacon or mesh peering frame.
436 *
437 * Return existing or newly allocated sta_info under RCU read lock.
438 * (re)initialize with given IEs.
439 */
440static struct sta_info *
441mesh_sta_info_get(struct ieee80211_sub_if_data *sdata,
442		  u8 *addr, struct ieee802_11_elems *elems) __acquires(RCU)
443{
444	struct sta_info *sta = NULL;
445
446	rcu_read_lock();
447	sta = sta_info_get(sdata, addr);
448	if (sta) {
449		mesh_sta_info_init(sdata, sta, elems, false);
450	} else {
451		rcu_read_unlock();
452		/* can't run atomic */
453		sta = mesh_sta_info_alloc(sdata, addr, elems);
454		if (!sta) {
455			rcu_read_lock();
456			return NULL;
457		}
458
459		mesh_sta_info_init(sdata, sta, elems, true);
460
461		if (sta_info_insert_rcu(sta))
462			return NULL;
463	}
464
465	return sta;
466}
467
468/*
469 * mesh_neighbour_update - update or initialize new mesh neighbor.
470 *
471 * @sdata: local meshif
472 * @addr: peer's address
473 * @elems: IEs from beacon or mesh peering frame
474 *
475 * Initiates peering if appropriate.
476 */
477void mesh_neighbour_update(struct ieee80211_sub_if_data *sdata,
478			   u8 *hw_addr,
479			   struct ieee802_11_elems *elems)
480{
481	struct sta_info *sta;
482	u32 changed = 0;
483
484	sta = mesh_sta_info_get(sdata, hw_addr, elems);
485	if (!sta)
486		goto out;
487
488	if (mesh_peer_accepts_plinks(elems) &&
489	    sta->plink_state == NL80211_PLINK_LISTEN &&
490	    sdata->u.mesh.accepting_plinks &&
491	    sdata->u.mesh.mshcfg.auto_open_plinks &&
492	    rssi_threshold_check(sta, sdata))
493		changed = mesh_plink_open(sta);
494
495	ieee80211_mps_frame_release(sta, elems);
496out:
497	rcu_read_unlock();
498	ieee80211_mbss_info_change_notify(sdata, changed);
499}
500
501static void mesh_plink_timer(unsigned long data)
502{
503	struct sta_info *sta;
504	__le16 llid, plid, reason;
505	struct ieee80211_sub_if_data *sdata;
506	struct mesh_config *mshcfg;
507
508	/*
509	 * This STA is valid because sta_info_destroy() will
510	 * del_timer_sync() this timer after having made sure
511	 * it cannot be readded (by deleting the plink.)
512	 */
513	sta = (struct sta_info *) data;
514
515	if (sta->sdata->local->quiescing) {
516		sta->plink_timer_was_running = true;
517		return;
518	}
519
520	spin_lock_bh(&sta->lock);
521	if (sta->ignore_plink_timer) {
522		sta->ignore_plink_timer = false;
523		spin_unlock_bh(&sta->lock);
524		return;
525	}
526	mpl_dbg(sta->sdata,
527		"Mesh plink timer for %pM fired on state %d\n",
528		sta->sta.addr, sta->plink_state);
529	reason = 0;
530	llid = sta->llid;
531	plid = sta->plid;
532	sdata = sta->sdata;
533	mshcfg = &sdata->u.mesh.mshcfg;
534
535	switch (sta->plink_state) {
536	case NL80211_PLINK_OPN_RCVD:
537	case NL80211_PLINK_OPN_SNT:
538		/* retry timer */
539		if (sta->plink_retries < mshcfg->dot11MeshMaxRetries) {
540			u32 rand;
541			mpl_dbg(sta->sdata,
542				"Mesh plink for %pM (retry, timeout): %d %d\n",
543				sta->sta.addr, sta->plink_retries,
544				sta->plink_timeout);
545			get_random_bytes(&rand, sizeof(u32));
546			sta->plink_timeout = sta->plink_timeout +
547					     rand % sta->plink_timeout;
548			++sta->plink_retries;
549			mod_plink_timer(sta, sta->plink_timeout);
550			spin_unlock_bh(&sta->lock);
551			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
552					    sta->sta.addr, llid, 0, 0);
553			break;
554		}
555		reason = cpu_to_le16(WLAN_REASON_MESH_MAX_RETRIES);
556		/* fall through on else */
557	case NL80211_PLINK_CNF_RCVD:
558		/* confirm timer */
559		if (!reason)
560			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIRM_TIMEOUT);
561		sta->plink_state = NL80211_PLINK_HOLDING;
562		mod_plink_timer(sta, mshcfg->dot11MeshHoldingTimeout);
563		spin_unlock_bh(&sta->lock);
564		mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
565				    sta->sta.addr, llid, plid, reason);
566		break;
567	case NL80211_PLINK_HOLDING:
568		/* holding timer */
569		del_timer(&sta->plink_timer);
570		mesh_plink_fsm_restart(sta);
571		spin_unlock_bh(&sta->lock);
572		break;
573	default:
574		spin_unlock_bh(&sta->lock);
575		break;
576	}
577}
578
579#ifdef CONFIG_PM
580void mesh_plink_quiesce(struct sta_info *sta)
581{
582	if (!ieee80211_vif_is_mesh(&sta->sdata->vif))
583		return;
584
585	/* no kernel mesh sta timers have been initialized */
586	if (sta->sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE)
587		return;
588
589	if (del_timer_sync(&sta->plink_timer))
590		sta->plink_timer_was_running = true;
591}
592
593void mesh_plink_restart(struct sta_info *sta)
594{
595	if (sta->plink_timer_was_running) {
596		add_timer(&sta->plink_timer);
597		sta->plink_timer_was_running = false;
598	}
599}
600#endif
601
602static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout)
603{
604	sta->plink_timer.expires = jiffies + (HZ * timeout / 1000);
605	sta->plink_timer.data = (unsigned long) sta;
606	sta->plink_timer.function = mesh_plink_timer;
607	sta->plink_timeout = timeout;
608	add_timer(&sta->plink_timer);
609}
610
611u32 mesh_plink_open(struct sta_info *sta)
612{
613	__le16 llid;
614	struct ieee80211_sub_if_data *sdata = sta->sdata;
615	u32 changed;
616
617	if (!test_sta_flag(sta, WLAN_STA_AUTH))
618		return 0;
619
620	spin_lock_bh(&sta->lock);
621	get_random_bytes(&llid, 2);
622	sta->llid = llid;
623	if (sta->plink_state != NL80211_PLINK_LISTEN &&
624	    sta->plink_state != NL80211_PLINK_BLOCKED) {
625		spin_unlock_bh(&sta->lock);
626		return 0;
627	}
628	sta->plink_state = NL80211_PLINK_OPN_SNT;
629	mesh_plink_timer_set(sta, sdata->u.mesh.mshcfg.dot11MeshRetryTimeout);
630	spin_unlock_bh(&sta->lock);
631	mpl_dbg(sdata,
632		"Mesh plink: starting establishment with %pM\n",
633		sta->sta.addr);
634
635	/* set the non-peer mode to active during peering */
636	changed = ieee80211_mps_local_status_update(sdata);
637
638	mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
639			    sta->sta.addr, llid, 0, 0);
640	return changed;
641}
642
643u32 mesh_plink_block(struct sta_info *sta)
644{
645	u32 changed;
646
647	spin_lock_bh(&sta->lock);
648	changed = __mesh_plink_deactivate(sta);
649	sta->plink_state = NL80211_PLINK_BLOCKED;
650	spin_unlock_bh(&sta->lock);
651
652	return changed;
653}
654
655
656void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata,
657			 struct ieee80211_mgmt *mgmt, size_t len,
658			 struct ieee80211_rx_status *rx_status)
659{
660	struct mesh_config *mshcfg = &sdata->u.mesh.mshcfg;
661	struct ieee802_11_elems elems;
662	struct sta_info *sta;
663	enum plink_event event;
664	enum ieee80211_self_protected_actioncode ftype;
665	size_t baselen;
666	bool matches_local = true;
667	u8 ie_len;
668	u8 *baseaddr;
669	u32 changed = 0;
670	__le16 plid, llid, reason;
671	static const char * const mplstates[] = {
672		[NL80211_PLINK_LISTEN] = "LISTEN",
673		[NL80211_PLINK_OPN_SNT] = "OPN-SNT",
674		[NL80211_PLINK_OPN_RCVD] = "OPN-RCVD",
675		[NL80211_PLINK_CNF_RCVD] = "CNF_RCVD",
676		[NL80211_PLINK_ESTAB] = "ESTAB",
677		[NL80211_PLINK_HOLDING] = "HOLDING",
678		[NL80211_PLINK_BLOCKED] = "BLOCKED"
679	};
680
681	/* need action_code, aux */
682	if (len < IEEE80211_MIN_ACTION_SIZE + 3)
683		return;
684
685	if (is_multicast_ether_addr(mgmt->da)) {
686		mpl_dbg(sdata,
687			"Mesh plink: ignore frame from multicast address\n");
688		return;
689	}
690
691	baseaddr = mgmt->u.action.u.self_prot.variable;
692	baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
693	if (mgmt->u.action.u.self_prot.action_code ==
694						WLAN_SP_MESH_PEERING_CONFIRM) {
695		baseaddr += 4;
696		baselen += 4;
697	}
698	ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
699
700	if (!elems.peering) {
701		mpl_dbg(sdata,
702			"Mesh plink: missing necessary peer link ie\n");
703		return;
704	}
705
706	if (elems.rsn_len &&
707	    sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
708		mpl_dbg(sdata,
709			"Mesh plink: can't establish link with secure peer\n");
710		return;
711	}
712
713	ftype = mgmt->u.action.u.self_prot.action_code;
714	ie_len = elems.peering_len;
715	if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
716	    (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
717	    (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
718							&& ie_len != 8)) {
719		mpl_dbg(sdata,
720			"Mesh plink: incorrect plink ie length %d %d\n",
721			ftype, ie_len);
722		return;
723	}
724
725	if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
726	    (!elems.mesh_id || !elems.mesh_config)) {
727		mpl_dbg(sdata, "Mesh plink: missing necessary ie\n");
728		return;
729	}
730	/* Note the lines below are correct, the llid in the frame is the plid
731	 * from the point of view of this host.
732	 */
733	memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
734	if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
735	    (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
736		memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
737
738	/* WARNING: Only for sta pointer, is dropped & re-acquired */
739	rcu_read_lock();
740
741	sta = sta_info_get(sdata, mgmt->sa);
742	if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
743		mpl_dbg(sdata, "Mesh plink: cls or cnf from unknown peer\n");
744		rcu_read_unlock();
745		return;
746	}
747
748	if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
749	    !rssi_threshold_check(sta, sdata)) {
750		mpl_dbg(sdata, "Mesh plink: %pM does not meet rssi threshold\n",
751			mgmt->sa);
752		rcu_read_unlock();
753		return;
754	}
755
756	if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
757		mpl_dbg(sdata, "Mesh plink: Action frame from non-authed peer\n");
758		rcu_read_unlock();
759		return;
760	}
761
762	if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
763		rcu_read_unlock();
764		return;
765	}
766
767	/* Now we will figure out the appropriate event... */
768	event = PLINK_UNDEFINED;
769	if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
770	    !mesh_matches_local(sdata, &elems)) {
771		matches_local = false;
772		switch (ftype) {
773		case WLAN_SP_MESH_PEERING_OPEN:
774			event = OPN_RJCT;
775			break;
776		case WLAN_SP_MESH_PEERING_CONFIRM:
777			event = CNF_RJCT;
778			break;
779		default:
780			break;
781		}
782	}
783
784	if (!sta && !matches_local) {
785		rcu_read_unlock();
786		reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
787		llid = 0;
788		mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
789				    mgmt->sa, llid, plid, reason);
790		return;
791	} else if (!sta) {
792		/* ftype == WLAN_SP_MESH_PEERING_OPEN */
793		if (!mesh_plink_free_count(sdata)) {
794			mpl_dbg(sdata, "Mesh plink error: no more free plinks\n");
795			rcu_read_unlock();
796			return;
797		}
798		event = OPN_ACPT;
799	} else if (matches_local) {
800		switch (ftype) {
801		case WLAN_SP_MESH_PEERING_OPEN:
802			if (!mesh_plink_free_count(sdata) ||
803			    (sta->plid && sta->plid != plid))
804				event = OPN_IGNR;
805			else
806				event = OPN_ACPT;
807			break;
808		case WLAN_SP_MESH_PEERING_CONFIRM:
809			if (!mesh_plink_free_count(sdata) ||
810			    (sta->llid != llid || sta->plid != plid))
811				event = CNF_IGNR;
812			else
813				event = CNF_ACPT;
814			break;
815		case WLAN_SP_MESH_PEERING_CLOSE:
816			if (sta->plink_state == NL80211_PLINK_ESTAB)
817				/* Do not check for llid or plid. This does not
818				 * follow the standard but since multiple plinks
819				 * per sta are not supported, it is necessary in
820				 * order to avoid a livelock when MP A sees an
821				 * establish peer link to MP B but MP B does not
822				 * see it. This can be caused by a timeout in
823				 * B's peer link establishment or B beign
824				 * restarted.
825				 */
826				event = CLS_ACPT;
827			else if (sta->plid != plid)
828				event = CLS_IGNR;
829			else if (ie_len == 7 && sta->llid != llid)
830				event = CLS_IGNR;
831			else
832				event = CLS_ACPT;
833			break;
834		default:
835			mpl_dbg(sdata, "Mesh plink: unknown frame subtype\n");
836			rcu_read_unlock();
837			return;
838		}
839	}
840
841	if (event == OPN_ACPT) {
842		rcu_read_unlock();
843		/* allocate sta entry if necessary and update info */
844		sta = mesh_sta_info_get(sdata, mgmt->sa, &elems);
845		if (!sta) {
846			mpl_dbg(sdata, "Mesh plink: failed to init peer!\n");
847			rcu_read_unlock();
848			return;
849		}
850	}
851
852	mpl_dbg(sdata,
853		"Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
854		mgmt->sa, mplstates[sta->plink_state],
855		le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
856		event);
857	reason = 0;
858	spin_lock_bh(&sta->lock);
859	switch (sta->plink_state) {
860		/* spin_unlock as soon as state is updated at each case */
861	case NL80211_PLINK_LISTEN:
862		switch (event) {
863		case CLS_ACPT:
864			mesh_plink_fsm_restart(sta);
865			spin_unlock_bh(&sta->lock);
866			break;
867		case OPN_ACPT:
868			sta->plink_state = NL80211_PLINK_OPN_RCVD;
869			sta->plid = plid;
870			get_random_bytes(&llid, 2);
871			sta->llid = llid;
872			mesh_plink_timer_set(sta,
873					     mshcfg->dot11MeshRetryTimeout);
874
875			/* set the non-peer mode to active during peering */
876			changed |= ieee80211_mps_local_status_update(sdata);
877
878			spin_unlock_bh(&sta->lock);
879			mesh_plink_frame_tx(sdata,
880					    WLAN_SP_MESH_PEERING_OPEN,
881					    sta->sta.addr, llid, 0, 0);
882			mesh_plink_frame_tx(sdata,
883					    WLAN_SP_MESH_PEERING_CONFIRM,
884					    sta->sta.addr, llid, plid, 0);
885			break;
886		default:
887			spin_unlock_bh(&sta->lock);
888			break;
889		}
890		break;
891
892	case NL80211_PLINK_OPN_SNT:
893		switch (event) {
894		case OPN_RJCT:
895		case CNF_RJCT:
896			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
897		case CLS_ACPT:
898			if (!reason)
899				reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
900			sta->reason = reason;
901			sta->plink_state = NL80211_PLINK_HOLDING;
902			if (!mod_plink_timer(sta,
903					     mshcfg->dot11MeshHoldingTimeout))
904				sta->ignore_plink_timer = true;
905
906			llid = sta->llid;
907			spin_unlock_bh(&sta->lock);
908			mesh_plink_frame_tx(sdata,
909					    WLAN_SP_MESH_PEERING_CLOSE,
910					    sta->sta.addr, llid, plid, reason);
911			break;
912		case OPN_ACPT:
913			/* retry timer is left untouched */
914			sta->plink_state = NL80211_PLINK_OPN_RCVD;
915			sta->plid = plid;
916			llid = sta->llid;
917			spin_unlock_bh(&sta->lock);
918			mesh_plink_frame_tx(sdata,
919					    WLAN_SP_MESH_PEERING_CONFIRM,
920					    sta->sta.addr, llid, plid, 0);
921			break;
922		case CNF_ACPT:
923			sta->plink_state = NL80211_PLINK_CNF_RCVD;
924			if (!mod_plink_timer(sta,
925					     mshcfg->dot11MeshConfirmTimeout))
926				sta->ignore_plink_timer = true;
927
928			spin_unlock_bh(&sta->lock);
929			break;
930		default:
931			spin_unlock_bh(&sta->lock);
932			break;
933		}
934		break;
935
936	case NL80211_PLINK_OPN_RCVD:
937		switch (event) {
938		case OPN_RJCT:
939		case CNF_RJCT:
940			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
941		case CLS_ACPT:
942			if (!reason)
943				reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
944			sta->reason = reason;
945			sta->plink_state = NL80211_PLINK_HOLDING;
946			if (!mod_plink_timer(sta,
947					     mshcfg->dot11MeshHoldingTimeout))
948				sta->ignore_plink_timer = true;
949
950			llid = sta->llid;
951			spin_unlock_bh(&sta->lock);
952			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
953					    sta->sta.addr, llid, plid, reason);
954			break;
955		case OPN_ACPT:
956			llid = sta->llid;
957			spin_unlock_bh(&sta->lock);
958			mesh_plink_frame_tx(sdata,
959					    WLAN_SP_MESH_PEERING_CONFIRM,
960					    sta->sta.addr, llid, plid, 0);
961			break;
962		case CNF_ACPT:
963			del_timer(&sta->plink_timer);
964			sta->plink_state = NL80211_PLINK_ESTAB;
965			spin_unlock_bh(&sta->lock);
966			changed |= mesh_plink_inc_estab_count(sdata);
967			changed |= mesh_set_ht_prot_mode(sdata);
968			changed |= mesh_set_short_slot_time(sdata);
969			mpl_dbg(sdata, "Mesh plink with %pM ESTABLISHED\n",
970				sta->sta.addr);
971			ieee80211_mps_sta_status_update(sta);
972			changed |= ieee80211_mps_set_sta_local_pm(sta,
973						       mshcfg->power_mode);
974			break;
975		default:
976			spin_unlock_bh(&sta->lock);
977			break;
978		}
979		break;
980
981	case NL80211_PLINK_CNF_RCVD:
982		switch (event) {
983		case OPN_RJCT:
984		case CNF_RJCT:
985			reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
986		case CLS_ACPT:
987			if (!reason)
988				reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
989			sta->reason = reason;
990			sta->plink_state = NL80211_PLINK_HOLDING;
991			if (!mod_plink_timer(sta,
992					     mshcfg->dot11MeshHoldingTimeout))
993				sta->ignore_plink_timer = true;
994
995			llid = sta->llid;
996			spin_unlock_bh(&sta->lock);
997			mesh_plink_frame_tx(sdata,
998					    WLAN_SP_MESH_PEERING_CLOSE,
999					    sta->sta.addr, llid, plid, reason);
1000			break;
1001		case OPN_ACPT:
1002			del_timer(&sta->plink_timer);
1003			sta->plink_state = NL80211_PLINK_ESTAB;
1004			spin_unlock_bh(&sta->lock);
1005			changed |= mesh_plink_inc_estab_count(sdata);
1006			changed |= mesh_set_ht_prot_mode(sdata);
1007			changed |= mesh_set_short_slot_time(sdata);
1008			mpl_dbg(sdata, "Mesh plink with %pM ESTABLISHED\n",
1009				sta->sta.addr);
1010			mesh_plink_frame_tx(sdata,
1011					    WLAN_SP_MESH_PEERING_CONFIRM,
1012					    sta->sta.addr, llid, plid, 0);
1013			ieee80211_mps_sta_status_update(sta);
1014			changed |= ieee80211_mps_set_sta_local_pm(sta,
1015							mshcfg->power_mode);
1016			break;
1017		default:
1018			spin_unlock_bh(&sta->lock);
1019			break;
1020		}
1021		break;
1022
1023	case NL80211_PLINK_ESTAB:
1024		switch (event) {
1025		case CLS_ACPT:
1026			reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
1027			sta->reason = reason;
1028			changed |= __mesh_plink_deactivate(sta);
1029			sta->plink_state = NL80211_PLINK_HOLDING;
1030			llid = sta->llid;
1031			mod_plink_timer(sta, mshcfg->dot11MeshHoldingTimeout);
1032			spin_unlock_bh(&sta->lock);
1033			changed |= mesh_set_ht_prot_mode(sdata);
1034			changed |= mesh_set_short_slot_time(sdata);
1035			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
1036					    sta->sta.addr, llid, plid, reason);
1037			break;
1038		case OPN_ACPT:
1039			llid = sta->llid;
1040			spin_unlock_bh(&sta->lock);
1041			mesh_plink_frame_tx(sdata,
1042					    WLAN_SP_MESH_PEERING_CONFIRM,
1043					    sta->sta.addr, llid, plid, 0);
1044			break;
1045		default:
1046			spin_unlock_bh(&sta->lock);
1047			break;
1048		}
1049		break;
1050	case NL80211_PLINK_HOLDING:
1051		switch (event) {
1052		case CLS_ACPT:
1053			if (del_timer(&sta->plink_timer))
1054				sta->ignore_plink_timer = 1;
1055			mesh_plink_fsm_restart(sta);
1056			spin_unlock_bh(&sta->lock);
1057			break;
1058		case OPN_ACPT:
1059		case CNF_ACPT:
1060		case OPN_RJCT:
1061		case CNF_RJCT:
1062			llid = sta->llid;
1063			reason = sta->reason;
1064			spin_unlock_bh(&sta->lock);
1065			mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
1066					    sta->sta.addr, llid, plid, reason);
1067			break;
1068		default:
1069			spin_unlock_bh(&sta->lock);
1070		}
1071		break;
1072	default:
1073		/* should not get here, PLINK_BLOCKED is dealt with at the
1074		 * beginning of the function
1075		 */
1076		spin_unlock_bh(&sta->lock);
1077		break;
1078	}
1079
1080	rcu_read_unlock();
1081
1082	if (changed)
1083		ieee80211_mbss_info_change_notify(sdata, changed);
1084}
1085