ieee80211_mesh.c revision 195618
1/*-
2 * Copyright (c) 2009 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Rui Paulo under sponsorship from the
6 * FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29#include <sys/cdefs.h>
30#ifdef __FreeBSD__
31__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_mesh.c 195618 2009-07-11 15:02:45Z rpaulo $");
32#endif
33
34/*
35 * IEEE 802.11s Mesh Point (MBSS) support.
36 *
37 * Based on March 2009, D3.0 802.11s draft spec.
38 */
39#include "opt_inet.h"
40#include "opt_wlan.h"
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/mbuf.h>
45#include <sys/malloc.h>
46#include <sys/kernel.h>
47
48#include <sys/socket.h>
49#include <sys/sockio.h>
50#include <sys/endian.h>
51#include <sys/errno.h>
52#include <sys/proc.h>
53#include <sys/sysctl.h>
54
55#include <net/if.h>
56#include <net/if_media.h>
57#include <net/if_llc.h>
58#include <net/ethernet.h>
59
60#include <net80211/ieee80211_var.h>
61#include <net80211/ieee80211_action.h>
62#include <net80211/ieee80211_input.h>
63#include <net80211/ieee80211_mesh.h>
64
65static int	mesh_select_proto_path(struct ieee80211vap *, const char *);
66static int	mesh_select_proto_metric(struct ieee80211vap *, const char *);
67static void	mesh_vattach(struct ieee80211vap *);
68static int	mesh_newstate(struct ieee80211vap *, enum ieee80211_state, int);
69static __inline void
70		mesh_linkchange(struct ieee80211_node *,
71			    enum ieee80211_mesh_mlstate);
72static void	mesh_checkid(void *, struct ieee80211_node *);
73static uint32_t	mesh_generateid(struct ieee80211vap *);
74static int	mesh_checkpseq(struct ieee80211vap *,
75		    const uint8_t [IEEE80211_ADDR_LEN], uint32_t);
76static struct ieee80211_node *
77		mesh_find_txnode(struct ieee80211vap *,
78		    const uint8_t [IEEE80211_ADDR_LEN]);
79static void	mesh_forward(struct ieee80211vap *, struct mbuf *,
80		    const struct ieee80211_meshcntl *);
81static int	mesh_input(struct ieee80211_node *, struct mbuf *, int, int);
82static void	mesh_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
83		    int, int);
84static void	mesh_peer_timeout_setup(struct ieee80211_node *);
85static void	mesh_peer_timeout_backoff(struct ieee80211_node *);
86static void	mesh_peer_timeout_cb(void *);
87static __inline void
88		mesh_peer_timeout_stop(struct ieee80211_node *);
89static int	mesh_verify_meshpeerver(struct ieee80211vap *, const uint8_t *);
90static int	mesh_verify_meshid(struct ieee80211vap *, const uint8_t *);
91static int	mesh_verify_meshconf(struct ieee80211vap *, const uint8_t *);
92static int	mesh_verify_meshpeer(struct ieee80211vap *, const uint8_t *);
93uint32_t	mesh_airtime_calc(struct ieee80211_node *);
94
95/*
96 * Timeout values come from the specification and are in milliseconds.
97 */
98SYSCTL_NODE(_net_wlan, OID_AUTO, mesh, CTLFLAG_RD, 0,
99    "IEEE 802.11s parameters");
100static int ieee80211_mesh_retrytimeout = -1;
101SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, retrytimeout, CTLTYPE_INT | CTLFLAG_RW,
102    &ieee80211_mesh_retrytimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
103    "Retry timeout (msec)");
104static int ieee80211_mesh_holdingtimeout = -1;
105SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, holdingtimeout, CTLTYPE_INT | CTLFLAG_RW,
106    &ieee80211_mesh_holdingtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
107    "Holding state timeout (msec)");
108static int ieee80211_mesh_confirmtimeout = -1;
109SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, confirmtimeout, CTLTYPE_INT | CTLFLAG_RW,
110    &ieee80211_mesh_confirmtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
111    "Confirm state timeout (msec)");
112static int ieee80211_mesh_maxretries = 2;
113SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLTYPE_INT | CTLFLAG_RW,
114    &ieee80211_mesh_maxretries, 0,
115    "Maximum retries during peer link establishment");
116
117static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] =
118	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
119
120static	ieee80211_recv_action_func mesh_recv_action_meshpeering_open;
121static	ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm;
122static	ieee80211_recv_action_func mesh_recv_action_meshpeering_close;
123static	ieee80211_recv_action_func mesh_recv_action_meshlmetric_req;
124static	ieee80211_recv_action_func mesh_recv_action_meshlmetric_rep;
125
126static	ieee80211_send_action_func mesh_send_action_meshpeering_open;
127static	ieee80211_send_action_func mesh_send_action_meshpeering_confirm;
128static	ieee80211_send_action_func mesh_send_action_meshpeering_close;
129static	ieee80211_send_action_func mesh_send_action_meshlink_request;
130static	ieee80211_send_action_func mesh_send_action_meshlink_reply;
131
132static const struct ieee80211_mesh_proto_metric mesh_metric_airtime = {
133	.mpm_descr	= "AIRTIME",
134	.mpm_ie		= IEEE80211_MESHCONF_AIRTIME,
135	.mpm_metric	= mesh_airtime_calc,
136};
137
138static struct ieee80211_mesh_proto_path		mesh_proto_paths[4];
139static struct ieee80211_mesh_proto_metric	mesh_proto_metrics[4];
140
141#define	MESH_RT_LOCK(ms)	mtx_lock(&(ms)->ms_rt_lock)
142#define	MESH_RT_UNLOCK(ms)	mtx_unlock(&(ms)->ms_rt_lock)
143
144MALLOC_DEFINE(M_80211_MESH_RT, "80211mesh", "802.11s routing table");
145
146/*
147 * Helper functions to manipulate the Mesh routing table.
148 */
149struct ieee80211_mesh_route *
150ieee80211_mesh_rt_find(struct ieee80211vap *vap,
151    const uint8_t dest[IEEE80211_ADDR_LEN])
152{
153	struct ieee80211_mesh_state *ms = vap->iv_mesh;
154	struct ieee80211_mesh_route *rt;
155
156	MESH_RT_LOCK(ms);
157	TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
158		if (IEEE80211_ADDR_EQ(dest, rt->rt_dest)) {
159			MESH_RT_UNLOCK(ms);
160			return rt;
161		}
162	}
163	MESH_RT_UNLOCK(ms);
164	return NULL;
165}
166
167struct ieee80211_mesh_route *
168ieee80211_mesh_rt_add(struct ieee80211vap *vap,
169    const uint8_t dest[IEEE80211_ADDR_LEN])
170{
171	struct ieee80211_mesh_state *ms = vap->iv_mesh;
172	struct ieee80211_mesh_route *rt;
173
174	KASSERT(ieee80211_mesh_rt_find(vap, dest) == NULL,
175	    ("%s: duplicate entry in the routing table", __func__));
176	KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
177	    ("%s: adding self to the routing table", __func__));
178	KASSERT(!IEEE80211_ADDR_EQ(broadcastaddr, dest),
179	    ("%s: adding broadcast to the routing table", __func__));
180
181	rt = malloc(sizeof(struct ieee80211_mesh_route), M_80211_MESH_RT,
182	    M_NOWAIT | M_ZERO);
183	IEEE80211_ADDR_COPY(rt->rt_dest, dest);
184	rt->rt_priv = malloc(ms->ms_ppath->mpp_privlen, M_80211_MESH_RT,
185	    M_NOWAIT | M_ZERO);
186	MESH_RT_LOCK(ms);
187	TAILQ_INSERT_TAIL(&ms->ms_routes, rt, rt_next);
188	MESH_RT_UNLOCK(ms);
189	return rt;
190}
191
192void
193ieee80211_mesh_rt_del(struct ieee80211vap *vap,
194    const uint8_t dest[IEEE80211_ADDR_LEN])
195{
196	struct ieee80211_mesh_state *ms = vap->iv_mesh;
197	struct ieee80211_mesh_route *rt, *next;
198
199	MESH_RT_LOCK(ms);
200	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
201		if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) {
202			TAILQ_REMOVE(&ms->ms_routes, rt, rt_next);
203			free(rt->rt_priv, M_80211_MESH_RT);
204			free(rt, M_80211_MESH_RT);
205			MESH_RT_UNLOCK(ms);
206			return;
207		}
208	}
209	MESH_RT_UNLOCK(ms);
210}
211
212void
213ieee80211_mesh_rt_flush(struct ieee80211vap *vap)
214{
215	struct ieee80211_mesh_state *ms = vap->iv_mesh;
216	struct ieee80211_mesh_route *rt, *next;
217
218	if (ms == NULL)
219		return;
220	MESH_RT_LOCK(ms);
221	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
222		TAILQ_REMOVE(&ms->ms_routes, rt, rt_next);
223		free(rt->rt_priv, M_80211_MESH_RT);
224		free(rt, M_80211_MESH_RT);
225	}
226	MESH_RT_UNLOCK(ms);
227}
228
229#define	N(a)	(sizeof(a) / sizeof(a[0]))
230int
231ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp)
232{
233	int i, firstempty = -1;
234	static const uint8_t emptyie[4] = { 0, 0, 0, 0 };
235
236	for (i = 0; i < N(mesh_proto_paths); i++) {
237		if (memcmp(mpp->mpp_ie, mesh_proto_paths[i].mpp_ie, 4) == 0)
238			return EEXIST;
239		if (memcmp(mesh_proto_paths[i].mpp_ie, emptyie, 4) == 0 &&
240		    firstempty == -1)
241			firstempty = i;
242	}
243	if (firstempty < 0)
244		return ENOSPC;
245	memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp));
246	return 0;
247}
248
249int
250ieee80211_mesh_register_proto_metric(const struct
251    ieee80211_mesh_proto_metric *mpm)
252{
253	int i, firstempty = -1;
254	static const uint8_t emptyie[4] = { 0, 0, 0, 0 };
255
256	for (i = 0; i < N(mesh_proto_metrics); i++) {
257		if (memcmp(mpm->mpm_ie, mesh_proto_metrics[i].mpm_ie, 4) == 0)
258			return EEXIST;
259		if (memcmp(mesh_proto_metrics[i].mpm_ie, emptyie, 4) == 0 &&
260		    firstempty == -1)
261			firstempty = i;
262	}
263	if (firstempty < 0)
264		return ENOSPC;
265	memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm));
266	return 0;
267}
268
269static int
270mesh_select_proto_path(struct ieee80211vap *vap, const char *name)
271{
272	struct ieee80211_mesh_state *ms = vap->iv_mesh;
273	int i;
274
275	for (i = 0; i < N(mesh_proto_paths); i++) {
276		if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) {
277			ms->ms_ppath = &mesh_proto_paths[i];
278			if (vap->iv_state == IEEE80211_S_RUN)
279				vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
280			return 0;
281		}
282	}
283	return ENOENT;
284}
285
286static int
287mesh_select_proto_metric(struct ieee80211vap *vap, const char *name)
288{
289	struct ieee80211_mesh_state *ms = vap->iv_mesh;
290	int i;
291
292	for (i = 0; i < N(mesh_proto_metrics); i++) {
293		if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) {
294			ms->ms_pmetric = &mesh_proto_metrics[i];
295			if (vap->iv_state == IEEE80211_S_RUN)
296				vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
297			return 0;
298		}
299	}
300	return ENOENT;
301}
302#undef	N
303
304static void
305ieee80211_mesh_init(void)
306{
307
308	memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths));
309	memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics));
310
311	/*
312	 * Setup mesh parameters that depends on the clock frequency.
313	 */
314	ieee80211_mesh_retrytimeout = msecs_to_ticks(40);
315	ieee80211_mesh_holdingtimeout = msecs_to_ticks(40);
316	ieee80211_mesh_confirmtimeout = msecs_to_ticks(40);
317
318	/*
319	 * Register action frame handlers.
320	 */
321	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
322	    IEEE80211_ACTION_MESHPEERING_OPEN,
323	    mesh_recv_action_meshpeering_open);
324	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
325	    IEEE80211_ACTION_MESHPEERING_CONFIRM,
326	    mesh_recv_action_meshpeering_confirm);
327	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
328	    IEEE80211_ACTION_MESHPEERING_CLOSE,
329	    mesh_recv_action_meshpeering_close);
330	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC,
331	    IEEE80211_ACTION_MESHLMETRIC_REQ, mesh_recv_action_meshlmetric_req);
332	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC,
333	    IEEE80211_ACTION_MESHLMETRIC_REP, mesh_recv_action_meshlmetric_rep);
334
335	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
336	    IEEE80211_ACTION_MESHPEERING_OPEN,
337	    mesh_send_action_meshpeering_open);
338	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
339	    IEEE80211_ACTION_MESHPEERING_CONFIRM,
340	    mesh_send_action_meshpeering_confirm);
341	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
342	    IEEE80211_ACTION_MESHPEERING_CLOSE,
343	    mesh_send_action_meshpeering_close);
344	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC,
345	    IEEE80211_ACTION_MESHLMETRIC_REQ,
346	    mesh_send_action_meshlink_request);
347	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC,
348	    IEEE80211_ACTION_MESHLMETRIC_REP,
349	    mesh_send_action_meshlink_reply);
350
351	/*
352	 * Register Airtime Link Metric.
353	 */
354	ieee80211_mesh_register_proto_metric(&mesh_metric_airtime);
355
356}
357SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL);
358
359void
360ieee80211_mesh_attach(struct ieee80211com *ic)
361{
362	ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach;
363}
364
365void
366ieee80211_mesh_detach(struct ieee80211com *ic)
367{
368}
369
370static void
371mesh_vdetach_peers(void *arg, struct ieee80211_node *ni)
372{
373	struct ieee80211com *ic = ni->ni_ic;
374	uint16_t args[3];
375
376	if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) {
377		args[0] = ni->ni_mlpid;
378		args[1] = ni->ni_mllid;
379		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
380		ieee80211_send_action(ni,
381		    IEEE80211_ACTION_CAT_MESHPEERING,
382		    IEEE80211_ACTION_MESHPEERING_CLOSE,
383		    args);
384	}
385	callout_stop(&ni->ni_mltimer);
386	/* XXX belongs in hwmp */
387	ieee80211_ageq_drain_node(&ic->ic_stageq,
388	   (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
389}
390
391static void
392mesh_vdetach(struct ieee80211vap *vap)
393{
394	struct ieee80211_mesh_state *ms = vap->iv_mesh;
395
396	ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers,
397	    NULL);
398	ieee80211_mesh_rt_flush(vap);
399	mtx_destroy(&ms->ms_rt_lock);
400	ms->ms_ppath->mpp_vdetach(vap);
401	free(vap->iv_mesh, M_80211_VAP);
402	vap->iv_mesh = NULL;
403}
404
405static void
406mesh_vattach(struct ieee80211vap *vap)
407{
408	struct ieee80211_mesh_state *ms;
409	vap->iv_newstate = mesh_newstate;
410	vap->iv_input = mesh_input;
411	vap->iv_opdetach = mesh_vdetach;
412	vap->iv_recv_mgmt = mesh_recv_mgmt;
413	ms = malloc(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
414	    M_NOWAIT | M_ZERO);
415	if (ms == NULL) {
416		printf("%s: couldn't alloc MBSS state\n", __func__);
417	return;
418	}
419	vap->iv_mesh = ms;
420	ms->ms_seq = 0;
421	ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
422	ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL;
423	TAILQ_INIT(&ms->ms_routes);
424	mtx_init(&ms->ms_rt_lock, "MBSS", "802.11s routing table", MTX_DEF);
425	mesh_select_proto_metric(vap, "AIRTIME");
426	KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL"));
427	mesh_select_proto_path(vap, "HWMP");
428	KASSERT(ms->ms_ppath, ("ms_ppath == NULL"));
429	ms->ms_ppath->mpp_vattach(vap);
430}
431
432/*
433 * IEEE80211_M_MBSS vap state machine handler.
434 */
435static int
436mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
437{
438	struct ieee80211_mesh_state *ms = vap->iv_mesh;
439	struct ieee80211com *ic = vap->iv_ic;
440	struct ieee80211_node *ni;
441	enum ieee80211_state ostate;
442
443	IEEE80211_LOCK_ASSERT(ic);
444
445	ostate = vap->iv_state;
446	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
447	    __func__, ieee80211_state_name[ostate],
448	    ieee80211_state_name[nstate], arg);
449	vap->iv_state = nstate;		/* state transition */
450	if (ostate != IEEE80211_S_SCAN)
451		ieee80211_cancel_scan(vap);	/* background scan */
452	ni = vap->iv_bss;			/* NB: no reference held */
453	/* Flush the routing table */
454	if (nstate != IEEE80211_S_INIT && ostate == IEEE80211_S_INIT)
455		ieee80211_mesh_rt_flush(vap);
456	switch (nstate) {
457	case IEEE80211_S_INIT:
458		switch (ostate) {
459		case IEEE80211_S_SCAN:
460			ieee80211_cancel_scan(vap);
461			break;
462		case IEEE80211_S_CAC:
463			ieee80211_dfs_cac_stop(vap);
464			break;
465		case IEEE80211_S_RUN:
466			ieee80211_iterate_nodes(&ic->ic_sta,
467			    mesh_vdetach_peers, NULL);
468			break;
469		default:
470			break;
471		}
472		if (ostate != IEEE80211_S_INIT) {
473			/* NB: optimize INIT -> INIT case */
474			ieee80211_reset_bss(vap);
475		}
476		break;
477	case IEEE80211_S_SCAN:
478		switch (ostate) {
479		case IEEE80211_S_INIT:
480			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
481			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) &&
482			    ms->ms_idlen != 0) {
483				/*
484				 * Already have a channel and a mesh ID; bypass
485				 * the scan and startup immediately.
486				 */
487				ieee80211_create_ibss(vap, vap->iv_des_chan);
488				break;
489			}
490			/*
491			 * Initiate a scan.  We can come here as a result
492			 * of an IEEE80211_IOC_SCAN_REQ too in which case
493			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
494			 * and the scan request parameters will be present
495			 * in iv_scanreq.  Otherwise we do the default.
496			*/
497			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
498				ieee80211_check_scan(vap,
499				    vap->iv_scanreq_flags,
500				    vap->iv_scanreq_duration,
501				    vap->iv_scanreq_mindwell,
502				    vap->iv_scanreq_maxdwell,
503				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
504				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
505			} else
506				ieee80211_check_scan_current(vap);
507			break;
508		default:
509			break;
510		}
511		break;
512	case IEEE80211_S_CAC:
513		/*
514		 * Start CAC on a DFS channel.  We come here when starting
515		 * a bss on a DFS channel (see ieee80211_create_ibss).
516		 */
517		ieee80211_dfs_cac_start(vap);
518		break;
519	case IEEE80211_S_RUN:
520		switch (ostate) {
521		case IEEE80211_S_INIT:
522			/*
523			 * Already have a channel; bypass the
524			 * scan and startup immediately.
525			 * Note that ieee80211_create_ibss will call
526			 * back to do a RUN->RUN state change.
527			 */
528			ieee80211_create_ibss(vap,
529			    ieee80211_ht_adjust_channel(ic,
530				ic->ic_curchan, vap->iv_flags_ht));
531			/* NB: iv_bss is changed on return */
532			break;
533		case IEEE80211_S_CAC:
534			/*
535			 * NB: This is the normal state change when CAC
536			 * expires and no radar was detected; no need to
537			 * clear the CAC timer as it's already expired.
538			 */
539			/* fall thru... */
540		case IEEE80211_S_CSA:
541#if 0
542			/*
543			 * Shorten inactivity timer of associated stations
544			 * to weed out sta's that don't follow a CSA.
545			 */
546			ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
547#endif
548			/*
549			 * Update bss node channel to reflect where
550			 * we landed after CSA.
551			 */
552			ieee80211_node_set_chan(vap->iv_bss,
553			    ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
554				ieee80211_htchanflags(vap->iv_bss->ni_chan)));
555			/* XXX bypass debug msgs */
556			break;
557		case IEEE80211_S_SCAN:
558		case IEEE80211_S_RUN:
559#ifdef IEEE80211_DEBUG
560			if (ieee80211_msg_debug(vap)) {
561				struct ieee80211_node *ni = vap->iv_bss;
562				ieee80211_note(vap,
563				    "synchronized with %s meshid ",
564				    ether_sprintf(ni->ni_meshid));
565				ieee80211_print_essid(ni->ni_meshid,
566				    ni->ni_meshidlen);
567				/* XXX MCS/HT */
568				printf(" channel %d\n",
569				    ieee80211_chan2ieee(ic, ic->ic_curchan));
570			}
571#endif
572			break;
573		default:
574			break;
575		}
576		ieee80211_node_authorize(vap->iv_bss);
577		break;
578	default:
579		break;
580	}
581
582	/* NB: ostate not nstate */
583	ms->ms_ppath->mpp_newstate(vap, ostate, arg);
584
585	return 0;
586}
587
588/*
589 * Helper function to note the Mesh Peer Link FSM change.
590 */
591static void
592mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state)
593{
594	struct ieee80211vap *vap = ni->ni_vap;
595	struct ieee80211_mesh_state *ms = vap->iv_mesh;
596#ifdef IEEE80211_DEBUG
597	static const char *meshlinkstates[] = {
598		[IEEE80211_NODE_MESH_IDLE]		= "IDLE",
599		[IEEE80211_NODE_MESH_OPENSNT]		= "OPEN SENT",
600		[IEEE80211_NODE_MESH_OPENRCV]		= "OPEN RECEIVED",
601		[IEEE80211_NODE_MESH_CONFIRMRCV]	= "CONFIRM RECEIVED",
602		[IEEE80211_NODE_MESH_ESTABLISHED]	= "ESTABLISHED",
603		[IEEE80211_NODE_MESH_HOLDING]		= "HOLDING"
604	};
605#endif
606	IEEE80211_NOTE(vap, IEEE80211_MSG_MESH,
607	    ni, "peer link: %s -> %s",
608	    meshlinkstates[ni->ni_mlstate], meshlinkstates[state]);
609
610	/* track neighbor count */
611	if (state == IEEE80211_NODE_MESH_ESTABLISHED &&
612	    ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
613		KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow"));
614		ms->ms_neighbors++;
615	} else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED &&
616	    state != IEEE80211_NODE_MESH_ESTABLISHED) {
617		KASSERT(ms->ms_neighbors > 0, ("neighbor count 0"));
618		ms->ms_neighbors--;
619	}
620	ni->ni_mlstate = state;
621	if (state == IEEE80211_NODE_MESH_HOLDING)
622		ms->ms_ppath->mpp_peerdown(ni);
623}
624
625/*
626 * Helper function to generate a unique local ID required for mesh
627 * peer establishment.
628 */
629static void
630mesh_checkid(void *arg, struct ieee80211_node *ni)
631{
632	uint16_t *r = arg;
633
634	if (*r == ni->ni_mllid)
635		*(uint16_t *)arg = 0;
636}
637
638static uint32_t
639mesh_generateid(struct ieee80211vap *vap)
640{
641	int maxiter = 4;
642	uint16_t r;
643
644	do {
645		get_random_bytes(&r, 2);
646		ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_checkid, &r);
647		maxiter--;
648	} while (r == 0 && maxiter > 0);
649	return r;
650}
651
652/*
653 * Verifies if we already received this packet by checking its
654 * sequence number.
655 */
656static int
657mesh_checkpseq(struct ieee80211vap *vap,
658    const uint8_t source[IEEE80211_ADDR_LEN], uint32_t seq)
659{
660	struct ieee80211_mesh_route *rt;
661
662	rt = ieee80211_mesh_rt_find(vap, source);
663	if (rt == NULL) {
664		rt = ieee80211_mesh_rt_add(vap, source);
665		rt->rt_lastmseq = seq;
666		return 0;
667	}
668	if (IEEE80211_MESH_SEQ_GEQ(rt->rt_lastmseq, seq)) {
669		return 1;
670	} else {
671		rt->rt_lastmseq = seq;
672		return 0;
673	}
674}
675
676/*
677 * Iterate the routing table and locate the next hop.
678 */
679static struct ieee80211_node *
680mesh_find_txnode(struct ieee80211vap *vap,
681    const uint8_t dest[IEEE80211_ADDR_LEN])
682{
683	struct ieee80211_mesh_route *rt;
684
685	rt = ieee80211_mesh_rt_find(vap, dest);
686	if (rt == NULL)
687		return NULL;
688	return ieee80211_find_txnode(vap, rt->rt_nexthop);
689}
690
691/*
692 * Forward the specified frame.
693 * Decrement the TTL and set TA to our MAC address.
694 */
695static void
696mesh_forward(struct ieee80211vap *vap, struct mbuf *m,
697    const struct ieee80211_meshcntl *mc)
698{
699	struct ieee80211com *ic = vap->iv_ic;
700	struct ieee80211_mesh_state *ms = vap->iv_mesh;
701	struct ifnet *ifp = vap->iv_ifp;
702	struct ifnet *parent = ic->ic_ifp;
703	const struct ieee80211_frame *wh =
704	    mtod(m, const struct ieee80211_frame *);
705	struct mbuf *mcopy;
706	struct ieee80211_meshcntl *mccopy;
707	struct ieee80211_frame *whcopy;
708	struct ieee80211_node *ni;
709	int err;
710
711	if (mc->mc_ttl == 0) {
712		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
713		    "%s", "frame not fwd'd, ttl 0");
714		vap->iv_stats.is_mesh_fwd_ttl++;
715		return;
716	}
717	if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
718		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
719		    "%s", "frame not fwd'd, fwding disabled");
720		vap->iv_stats.is_mesh_fwd_disabled++;
721		return;
722	}
723	mcopy = m_dup(m, M_DONTWAIT);
724	if (mcopy == NULL) {
725		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
726		    "%s", "frame not fwd'd, cannot dup");
727		vap->iv_stats.is_mesh_fwd_nobuf++;
728		ifp->if_oerrors++;
729		return;
730	}
731	mcopy = m_pullup(mcopy, ieee80211_hdrspace(ic, wh) +
732	    sizeof(struct ieee80211_meshcntl));
733	if (mcopy == NULL) {
734		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
735		    "%s", "frame not fwd'd, too short");
736		vap->iv_stats.is_mesh_fwd_tooshort++;
737		ifp->if_oerrors++;
738		m_freem(mcopy);
739		return;
740	}
741	whcopy = mtod(mcopy, struct ieee80211_frame *);
742	mccopy = (struct ieee80211_meshcntl *)
743	    (mtod(mcopy, uint8_t *) + ieee80211_hdrspace(ic, wh));
744	/* XXX clear other bits? */
745	whcopy->i_fc[1] &= ~IEEE80211_FC1_RETRY;
746	IEEE80211_ADDR_COPY(whcopy->i_addr2, vap->iv_myaddr);
747	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
748		ni = ieee80211_ref_node(vap->iv_bss);
749		mcopy->m_flags |= M_MCAST;
750	} else {
751		ni = mesh_find_txnode(vap, whcopy->i_addr3);
752		if (ni == NULL) {
753			IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
754			    "%s", "frame not fwd'd, no path");
755			vap->iv_stats.is_mesh_fwd_nopath++;
756			m_freem(mcopy);
757			return;
758		}
759		IEEE80211_ADDR_COPY(whcopy->i_addr1, ni->ni_macaddr);
760	}
761	IEEE80211_NOTE(vap, IEEE80211_MSG_MESH, ni,
762	    "fwd %s frame from %s ttl %d",
763	    IEEE80211_IS_MULTICAST(wh->i_addr1) ?  "mcast" : "ucast",
764	    ether_sprintf(wh->i_addr3), mccopy->mc_ttl);
765
766	KASSERT(mccopy->mc_ttl > 0, ("%s called with wrong ttl", __func__));
767	mccopy->mc_ttl--;
768
769	/* XXX calculate priority so drivers can find the tx queue */
770	M_WME_SETAC(mcopy, WME_AC_BE);
771
772	/* XXX do we know m_nextpkt is NULL? */
773	mcopy->m_pkthdr.rcvif = (void *) ni;
774	err = parent->if_transmit(parent, mcopy);
775	if (err != 0) {
776		/* NB: IFQ_HANDOFF reclaims mbuf */
777		ieee80211_free_node(ni);
778	} else {
779		ifp->if_opackets++;
780	}
781}
782
783static int
784mesh_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
785{
786#define	SEQ_LEQ(a,b)	((int)((a)-(b)) <= 0)
787#define	HAS_SEQ(type)	((type & 0x4) == 0)
788	struct ieee80211vap *vap = ni->ni_vap;
789	struct ieee80211com *ic = ni->ni_ic;
790	struct ifnet *ifp = vap->iv_ifp;
791	struct ieee80211_frame *wh;
792	const struct ieee80211_meshcntl *mc;
793	int hdrspace, need_tap;
794	uint8_t dir, type, subtype, qos;
795	uint32_t seq;
796	uint8_t *addr;
797	ieee80211_seq rxseq;
798
799	KASSERT(ni != NULL, ("null node"));
800	ni->ni_inact = ni->ni_inact_reload;
801
802	need_tap = 1;			/* mbuf need to be tapped. */
803	type = -1;			/* undefined */
804
805	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
806		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
807		    ni->ni_macaddr, NULL,
808		    "too short (1): len %u", m->m_pkthdr.len);
809		vap->iv_stats.is_rx_tooshort++;
810		goto out;
811	}
812	/*
813	 * Bit of a cheat here, we use a pointer for a 3-address
814	 * frame format but don't reference fields past outside
815	 * ieee80211_frame_min w/o first validating the data is
816	 * present.
817	*/
818	wh = mtod(m, struct ieee80211_frame *);
819
820	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
821	    IEEE80211_FC0_VERSION_0) {
822		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
823		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
824		vap->iv_stats.is_rx_badversion++;
825		goto err;
826	}
827	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
828	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
829	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
830	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
831		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
832		ni->ni_noise = nf;
833		if (HAS_SEQ(type)) {
834			uint8_t tid = ieee80211_gettid(wh);
835
836			if (IEEE80211_QOS_HAS_SEQ(wh) &&
837			    TID_TO_WME_AC(tid) >= WME_AC_VI)
838				ic->ic_wme.wme_hipri_traffic++;
839			rxseq = le16toh(*(uint16_t *)wh->i_seq);
840			if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
841			    (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
842			    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
843				/* duplicate, discard */
844				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
845				    wh->i_addr1, "duplicate",
846				    "seqno <%u,%u> fragno <%u,%u> tid %u",
847				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
848				    ni->ni_rxseqs[tid] >>
849				    IEEE80211_SEQ_SEQ_SHIFT,
850				    rxseq & IEEE80211_SEQ_FRAG_MASK,
851				    ni->ni_rxseqs[tid] &
852				    IEEE80211_SEQ_FRAG_MASK,
853				    tid);
854				vap->iv_stats.is_rx_dup++;
855				IEEE80211_NODE_STAT(ni, rx_dup);
856				goto out;
857			}
858			ni->ni_rxseqs[tid] = rxseq;
859		}
860	}
861#ifdef IEEE80211_DEBUG
862	/*
863	 * It's easier, but too expensive, to simulate different mesh
864	 * topologies by consulting the ACL policy very early, so do this
865	 * only under DEBUG.
866	 *
867	 * NB: this check is also done upon peering link initiation.
868	 */
869	if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh->i_addr2)) {
870		IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
871		    wh, NULL, "%s", "disallowed by ACL");
872		vap->iv_stats.is_rx_acl++;
873		goto out;
874	}
875#endif
876	switch (type) {
877	case IEEE80211_FC0_TYPE_DATA:
878		if (ni == vap->iv_bss)
879			goto out;
880		if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
881			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
882			    ni->ni_macaddr, NULL,
883			    "peer link not yet established (%d)",
884			    ni->ni_mlstate);
885			vap->iv_stats.is_mesh_nolink++;
886			goto out;
887		}
888		if (dir != IEEE80211_FC1_DIR_FROMDS &&
889		    dir != IEEE80211_FC1_DIR_DSTODS) {
890			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
891			    wh, "data", "incorrect dir 0x%x", dir);
892			vap->iv_stats.is_rx_wrongdir++;
893			goto err;
894		}
895		/* pull up enough to get to the mesh control */
896		hdrspace = ieee80211_hdrspace(ic, wh);
897		if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) &&
898		    (m = m_pullup(m, hdrspace +
899		        sizeof(struct ieee80211_meshcntl))) == NULL) {
900			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
901			    ni->ni_macaddr, NULL,
902			    "data too short: expecting %u", hdrspace);
903			vap->iv_stats.is_rx_tooshort++;
904			goto out;		/* XXX */
905		}
906		/*
907		 * Now calculate the full extent of the headers. Note
908		 * ieee80211_decap will pull up anything we didn't get
909		 * above when it strips the 802.11 headers.
910		 */
911		mc = (const struct ieee80211_meshcntl *)
912		    (mtod(m, const uint8_t *) + hdrspace);
913		hdrspace += sizeof(struct ieee80211_meshcntl) +
914		    (mc->mc_flags & 3) * IEEE80211_ADDR_LEN;
915		seq = LE_READ_4(mc->mc_seq);
916		if (IEEE80211_IS_MULTICAST(wh->i_addr1))
917			addr = wh->i_addr3;
918		else
919			addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4;
920		if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) {
921			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
922			    addr, "data", "%s", "not to me");
923			vap->iv_stats.is_rx_wrongbss++;	/* XXX kinda */
924			goto out;
925		}
926		if (mesh_checkpseq(vap, addr, seq) != 0) {
927			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
928			    addr, "data", "duplicate mesh seqno %u ttl %u",
929			    seq, mc->mc_ttl);
930			vap->iv_stats.is_rx_dup++;
931			goto out;
932		}
933
934		/*
935		 * Potentially forward packet.  See table s36 (p140)
936		 * for the rules.  XXX tap fwd'd packets not for us?
937		 */
938		if (dir == IEEE80211_FC1_DIR_FROMDS ||
939		    !IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr)) {
940			mesh_forward(vap, m, mc);
941			if (dir == IEEE80211_FC1_DIR_DSTODS)
942				goto out;
943			/* NB: fall thru to deliver mcast frames locally */
944		}
945
946		/*
947		 * Save QoS bits for use below--before we strip the header.
948		 */
949		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
950			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
951			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
952			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
953		} else
954			qos = 0;
955		/*
956		 * Next up, any fragmentation.
957		 */
958		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
959			m = ieee80211_defrag(ni, m, hdrspace);
960			if (m == NULL) {
961				/* Fragment dropped or frame not complete yet */
962				goto out;
963			}
964		}
965		wh = NULL;		/* no longer valid, catch any uses */
966
967		if (ieee80211_radiotap_active_vap(vap))
968			ieee80211_radiotap_rx(vap, m);
969		need_tap = 0;
970
971		/*
972		 * Finally, strip the 802.11 header.
973		 */
974		m = ieee80211_decap(vap, m, hdrspace);
975		if (m == NULL) {
976			/* XXX mask bit to check for both */
977			/* don't count Null data frames as errors */
978			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
979			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
980				goto out;
981			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
982			    ni->ni_macaddr, "data", "%s", "decap error");
983			vap->iv_stats.is_rx_decap++;
984			IEEE80211_NODE_STAT(ni, rx_decap);
985			goto err;
986		}
987		if (qos & IEEE80211_QOS_AMSDU) {
988			m = ieee80211_decap_amsdu(ni, m);
989			if (m == NULL)
990				return IEEE80211_FC0_TYPE_DATA;
991		} else {
992#ifdef IEEE80211_SUPPORT_SUPERG
993			m = ieee80211_decap_fastframe(vap, ni, m);
994			if (m == NULL)
995				return IEEE80211_FC0_TYPE_DATA;
996#endif
997		}
998		ieee80211_deliver_data(vap, ni, m);
999		return type;
1000	case IEEE80211_FC0_TYPE_MGT:
1001		vap->iv_stats.is_rx_mgmt++;
1002		IEEE80211_NODE_STAT(ni, rx_mgmt);
1003		if (dir != IEEE80211_FC1_DIR_NODS) {
1004			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1005			    wh, "mgt", "incorrect dir 0x%x", dir);
1006			vap->iv_stats.is_rx_wrongdir++;
1007			goto err;
1008		}
1009		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
1010			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1011			    ni->ni_macaddr, "mgt", "too short: len %u",
1012			    m->m_pkthdr.len);
1013			vap->iv_stats.is_rx_tooshort++;
1014			goto out;
1015		}
1016#ifdef IEEE80211_DEBUG
1017		if ((ieee80211_msg_debug(vap) &&
1018		    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) ||
1019		    ieee80211_msg_dumppkts(vap)) {
1020			if_printf(ifp, "received %s from %s rssi %d\n",
1021			    ieee80211_mgt_subtype_name[subtype >>
1022			    IEEE80211_FC0_SUBTYPE_SHIFT],
1023			    ether_sprintf(wh->i_addr2), rssi);
1024		}
1025#endif
1026		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1027			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1028			    wh, NULL, "%s", "WEP set but not permitted");
1029			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
1030			goto out;
1031		}
1032		vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
1033		goto out;
1034	case IEEE80211_FC0_TYPE_CTL:
1035		vap->iv_stats.is_rx_ctl++;
1036		IEEE80211_NODE_STAT(ni, rx_ctrl);
1037		goto out;
1038	default:
1039		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1040		    wh, "bad", "frame type 0x%x", type);
1041		/* should not come here */
1042		break;
1043	}
1044err:
1045	ifp->if_ierrors++;
1046out:
1047	if (m != NULL) {
1048		if (need_tap && ieee80211_radiotap_active_vap(vap))
1049			ieee80211_radiotap_rx(vap, m);
1050		m_freem(m);
1051	}
1052	return type;
1053}
1054
1055static void
1056mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1057    int rssi, int nf)
1058{
1059	struct ieee80211vap *vap = ni->ni_vap;
1060	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1061	struct ieee80211com *ic = ni->ni_ic;
1062	struct ieee80211_frame *wh;
1063	uint8_t *frm, *efrm;
1064
1065	wh = mtod(m0, struct ieee80211_frame *);
1066	frm = (uint8_t *)&wh[1];
1067	efrm = mtod(m0, uint8_t *) + m0->m_len;
1068	switch (subtype) {
1069	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1070	case IEEE80211_FC0_SUBTYPE_BEACON:
1071	{
1072		struct ieee80211_scanparams scan;
1073		/*
1074		 * We process beacon/probe response
1075		 * frames to discover neighbors.
1076		 */
1077		if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
1078			return;
1079		/*
1080		 * Count frame now that we know it's to be processed.
1081		 */
1082		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1083			vap->iv_stats.is_rx_beacon++;	/* XXX remove */
1084			IEEE80211_NODE_STAT(ni, rx_beacons);
1085		} else
1086			IEEE80211_NODE_STAT(ni, rx_proberesp);
1087		/*
1088		 * If scanning, just pass information to the scan module.
1089		 */
1090		if (ic->ic_flags & IEEE80211_F_SCAN) {
1091			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1092				/*
1093				 * Actively scanning a channel marked passive;
1094				 * send a probe request now that we know there
1095				 * is 802.11 traffic present.
1096				 *
1097				 * XXX check if the beacon we recv'd gives
1098				 * us what we need and suppress the probe req
1099				 */
1100				ieee80211_probe_curchan(vap, 1);
1101				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1102			}
1103			ieee80211_add_scan(vap, &scan, wh,
1104			    subtype, rssi, nf);
1105			return;
1106		}
1107
1108		/* The rest of this code assumes we are running */
1109		if (vap->iv_state != IEEE80211_S_RUN)
1110			return;
1111		/*
1112		 * Ignore non-mesh STAs.
1113		 */
1114		if ((scan.capinfo &
1115		     (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) ||
1116		    scan.meshid == NULL || scan.meshconf == NULL) {
1117			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1118			    wh, "beacon", "%s", "not a mesh sta");
1119			vap->iv_stats.is_mesh_wrongmesh++;
1120			return;
1121		}
1122		/*
1123		 * Ignore STAs for other mesh networks.
1124		 */
1125		if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 ||
1126		    mesh_verify_meshconf(vap, scan.meshconf)) {
1127			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1128			    wh, "beacon", "%s", "not for our mesh");
1129			vap->iv_stats.is_mesh_wrongmesh++;
1130			return;
1131		}
1132		/*
1133		 * Peer only based on the current ACL policy.
1134		 */
1135		if (vap->iv_acl != NULL &&
1136		    !vap->iv_acl->iac_check(vap, wh->i_addr2)) {
1137			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1138			    wh, NULL, "%s", "disallowed by ACL");
1139			vap->iv_stats.is_rx_acl++;
1140			return;
1141		}
1142		/*
1143		 * Do neighbor discovery.
1144		 */
1145		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
1146			/*
1147			 * Create a new entry in the neighbor table.
1148			 */
1149			ni = ieee80211_add_neighbor(vap, wh, &scan);
1150		}
1151		/*
1152		 * Automatically peer with discovered nodes if possible.
1153		 * XXX backoff on repeated failure
1154		 */
1155		if (ni != vap->iv_bss &&
1156		    (ms->ms_flags & IEEE80211_MESHFLAGS_AP) &&
1157		    ni->ni_mlstate == IEEE80211_NODE_MESH_IDLE) {
1158			uint16_t args[1];
1159
1160			ni->ni_mlpid = mesh_generateid(vap);
1161			if (ni->ni_mlpid == 0)
1162				return;
1163			mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT);
1164			args[0] = ni->ni_mlpid;
1165			ieee80211_send_action(ni,
1166			    IEEE80211_ACTION_CAT_MESHPEERING,
1167			    IEEE80211_ACTION_MESHPEERING_OPEN, args);
1168			ni->ni_mlrcnt = 0;
1169			mesh_peer_timeout_setup(ni);
1170		}
1171		break;
1172	}
1173	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1174	{
1175		uint8_t *ssid, *meshid, *rates, *xrates;
1176		uint8_t *sfrm;
1177
1178		if (vap->iv_state != IEEE80211_S_RUN) {
1179			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1180			    wh, NULL, "wrong state %s",
1181			    ieee80211_state_name[vap->iv_state]);
1182			vap->iv_stats.is_rx_mgtdiscard++;
1183			return;
1184		}
1185		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
1186			/* frame must be directed */
1187			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1188			    wh, NULL, "%s", "not unicast");
1189			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
1190			return;
1191		}
1192		/*
1193		 * prreq frame format
1194		 *      [tlv] ssid
1195		 *      [tlv] supported rates
1196		 *      [tlv] extended supported rates
1197		 *	[tlv] mesh id
1198		 */
1199		ssid = meshid = rates = xrates = NULL;
1200		sfrm = frm;
1201		while (efrm - frm > 1) {
1202			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1203			switch (*frm) {
1204			case IEEE80211_ELEMID_SSID:
1205				ssid = frm;
1206				break;
1207			case IEEE80211_ELEMID_RATES:
1208				rates = frm;
1209				break;
1210			case IEEE80211_ELEMID_XRATES:
1211				xrates = frm;
1212				break;
1213			case IEEE80211_ELEMID_MESHID:
1214				meshid = frm;
1215				break;
1216			}
1217			frm += frm[2] + 2;
1218		}
1219		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1220		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1221		if (xrates != NULL)
1222			IEEE80211_VERIFY_ELEMENT(xrates,
1223			    IEEE80211_RATE_MAXSIZE - rates[1], return);
1224		if (meshid != NULL)
1225			IEEE80211_VERIFY_ELEMENT(meshid,
1226			    IEEE80211_MESHID_LEN, return);
1227		/* NB: meshid, not ssid */
1228		IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return);
1229
1230		/* XXX find a better class or define it's own */
1231		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1232		    "%s", "recv probe req");
1233		/*
1234		 * Some legacy 11b clients cannot hack a complete
1235		 * probe response frame.  When the request includes
1236		 * only a bare-bones rate set, communicate this to
1237		 * the transmit side.
1238		 */
1239		ieee80211_send_proberesp(vap, wh->i_addr2, 0);
1240		break;
1241	}
1242	case IEEE80211_FC0_SUBTYPE_ACTION:
1243		if (vap->iv_state != IEEE80211_S_RUN) {
1244			vap->iv_stats.is_rx_mgtdiscard++;
1245			break;
1246		}
1247		/*
1248		 * We received an action for an unknown neighbor.
1249		 * XXX: wait for it to beacon or create ieee80211_node?
1250		 */
1251		if (ni == vap->iv_bss) {
1252			IEEE80211_DISCARD(vap, IEEE80211_MSG_MESH,
1253			    wh, NULL, "%s", "unknown node");
1254			vap->iv_stats.is_rx_mgtdiscard++;
1255			break;
1256		}
1257		/*
1258		 * Discard if not for us.
1259		 */
1260		if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
1261		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1262			IEEE80211_DISCARD(vap, IEEE80211_MSG_MESH,
1263			    wh, NULL, "%s", "not for me");
1264			vap->iv_stats.is_rx_mgtdiscard++;
1265			break;
1266		}
1267		/* XXX parse_action is a bit useless now */
1268		if (ieee80211_parse_action(ni, m0) == 0)
1269			ic->ic_recv_action(ni, wh, frm, efrm);
1270		break;
1271	case IEEE80211_FC0_SUBTYPE_AUTH:
1272	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1273	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1274	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1275	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1276	case IEEE80211_FC0_SUBTYPE_DEAUTH:
1277	case IEEE80211_FC0_SUBTYPE_DISASSOC:
1278		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1279		    wh, NULL, "%s", "not handled");
1280		vap->iv_stats.is_rx_mgtdiscard++;
1281		return;
1282	default:
1283		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1284		    wh, "mgt", "subtype 0x%x not handled", subtype);
1285		vap->iv_stats.is_rx_badsubtype++;
1286		break;
1287	}
1288}
1289
1290/*
1291 * Parse meshpeering action ie's for open+confirm frames; the
1292 * important bits are returned in the supplied structure.
1293 */
1294static const struct ieee80211_meshpeer_ie *
1295mesh_parse_meshpeering_action(struct ieee80211_node *ni,
1296	const struct ieee80211_frame *wh,	/* XXX for VERIFY_LENGTH */
1297	const uint8_t *frm, const uint8_t *efrm,
1298	struct ieee80211_meshpeer_ie *mp)
1299{
1300	struct ieee80211vap *vap = ni->ni_vap;
1301	const struct ieee80211_meshpeer_ie *mpie;
1302	const uint8_t *meshid, *meshconf, *meshpeerver, *meshpeer;
1303
1304	meshid = meshconf = meshpeerver = meshpeer = NULL;
1305	while (efrm - frm > 1) {
1306		IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
1307		switch (*frm) {
1308		case IEEE80211_ELEMID_MESHPEERVER:
1309			meshpeerver = frm;
1310			break;
1311		case IEEE80211_ELEMID_MESHID:
1312			meshid = frm;
1313			break;
1314		case IEEE80211_ELEMID_MESHCONF:
1315			meshconf = frm;
1316			break;
1317		case IEEE80211_ELEMID_MESHPEER:
1318			meshpeer = frm;
1319			mpie = (const struct ieee80211_meshpeer_ie *) frm;
1320			memset(mp, 0, sizeof(*mp));
1321			mp->peer_subtype = mpie->peer_subtype;
1322			mp->peer_llinkid = LE_READ_2(&mpie->peer_llinkid);
1323			/* NB: peer link ID is optional on these frames */
1324			if (mpie->peer_subtype ==
1325			    IEEE80211_MESH_PEER_LINK_CLOSE &&
1326			    mpie->peer_len == 5) {
1327				mp->peer_linkid = 0;
1328				mp->peer_rcode = LE_READ_2(&mpie->peer_linkid);
1329			} else {
1330				mp->peer_linkid = LE_READ_2(&mpie->peer_linkid);
1331				mp->peer_rcode = LE_READ_2(&mpie->peer_rcode);
1332			}
1333			break;
1334		}
1335		frm += frm[1] + 2;
1336	}
1337
1338	/*
1339	 * Verify the contents of the frame. Action frames with
1340	 * close subtype don't have a Mesh Configuration IE.
1341	 * If if fails validation, close the peer link.
1342	 */
1343	KASSERT(meshpeer != NULL && mp->peer_subtype !=
1344	    IEEE80211_ACTION_MESHPEERING_CLOSE, ("parsing close action"));
1345
1346	if (mesh_verify_meshpeerver(vap, meshpeerver) ||
1347	    mesh_verify_meshid(vap, meshid) ||
1348	    mesh_verify_meshpeer(vap, meshpeer) ||
1349	    mesh_verify_meshconf(vap, meshconf)) {
1350		uint16_t args[3];
1351
1352		IEEE80211_DISCARD(vap,
1353		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
1354		    wh, NULL, "%s", "not for our mesh");
1355		vap->iv_stats.is_rx_mgtdiscard++;
1356		switch (ni->ni_mlstate) {
1357		case IEEE80211_NODE_MESH_IDLE:
1358		case IEEE80211_NODE_MESH_ESTABLISHED:
1359		case IEEE80211_NODE_MESH_HOLDING:
1360			/* ignore */
1361			break;
1362		case IEEE80211_NODE_MESH_OPENSNT:
1363		case IEEE80211_NODE_MESH_OPENRCV:
1364		case IEEE80211_NODE_MESH_CONFIRMRCV:
1365			args[0] = ni->ni_mlpid;
1366			args[1] = ni->ni_mllid;
1367			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1368			ieee80211_send_action(ni,
1369			    IEEE80211_ACTION_CAT_MESHPEERING,
1370			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1371			    args);
1372			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1373			mesh_peer_timeout_setup(ni);
1374			break;
1375		}
1376		return NULL;
1377	}
1378	return (const struct ieee80211_meshpeer_ie *) mp;
1379}
1380
1381static int
1382mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
1383	const struct ieee80211_frame *wh,
1384	const uint8_t *frm, const uint8_t *efrm)
1385{
1386	struct ieee80211vap *vap = ni->ni_vap;
1387	struct ieee80211_meshpeer_ie ie;
1388	const struct ieee80211_meshpeer_ie *meshpeer;
1389	uint16_t args[3];
1390
1391	/* +2+2 for action + code + capabilites */
1392	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie);
1393	if (meshpeer == NULL) {
1394		return 0;
1395	}
1396
1397	/* XXX move up */
1398	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1399	    "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
1400
1401	switch (ni->ni_mlstate) {
1402	case IEEE80211_NODE_MESH_IDLE:
1403		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
1404		ni->ni_mllid = meshpeer->peer_llinkid;
1405		ni->ni_mlpid = mesh_generateid(vap);
1406		if (ni->ni_mlpid == 0)
1407			return 0;		/* XXX */
1408		args[0] = ni->ni_mlpid;
1409		/* Announce we're open too... */
1410		ieee80211_send_action(ni,
1411		    IEEE80211_ACTION_CAT_MESHPEERING,
1412		    IEEE80211_ACTION_MESHPEERING_OPEN, args);
1413		/* ...and confirm the link. */
1414		args[0] = ni->ni_mlpid;
1415		args[1] = ni->ni_mllid;
1416		ieee80211_send_action(ni,
1417		    IEEE80211_ACTION_CAT_MESHPEERING,
1418		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1419		    args);
1420		mesh_peer_timeout_setup(ni);
1421		break;
1422	case IEEE80211_NODE_MESH_OPENRCV:
1423		/* Wrong Link ID */
1424		if (ni->ni_mllid != meshpeer->peer_llinkid) {
1425			args[0] = ni->ni_mllid;
1426			args[1] = ni->ni_mlpid;
1427			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1428			ieee80211_send_action(ni,
1429			    IEEE80211_ACTION_CAT_MESHPEERING,
1430			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1431			    args);
1432			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1433			mesh_peer_timeout_setup(ni);
1434			break;
1435		}
1436		/* Duplicate open, confirm again. */
1437		args[0] = ni->ni_mlpid;
1438		args[1] = ni->ni_mllid;
1439		ieee80211_send_action(ni,
1440		    IEEE80211_ACTION_CAT_MESHPEERING,
1441		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1442		    args);
1443		break;
1444	case IEEE80211_NODE_MESH_OPENSNT:
1445		ni->ni_mllid = meshpeer->peer_llinkid;
1446		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
1447		args[0] = ni->ni_mlpid;
1448		args[1] = ni->ni_mllid;
1449		ieee80211_send_action(ni,
1450		    IEEE80211_ACTION_CAT_MESHPEERING,
1451		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1452		    args);
1453		/* NB: don't setup/clear any timeout */
1454		break;
1455	case IEEE80211_NODE_MESH_CONFIRMRCV:
1456		if (ni->ni_mlpid != meshpeer->peer_linkid ||
1457		    ni->ni_mllid != meshpeer->peer_llinkid) {
1458			args[0] = ni->ni_mlpid;
1459			args[1] = ni->ni_mllid;
1460			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1461			ieee80211_send_action(ni,
1462			    IEEE80211_ACTION_CAT_MESHPEERING,
1463			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1464			    args);
1465			mesh_linkchange(ni,
1466			    IEEE80211_NODE_MESH_HOLDING);
1467			mesh_peer_timeout_setup(ni);
1468			break;
1469		}
1470		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
1471		ni->ni_mllid = meshpeer->peer_llinkid;
1472		args[0] = ni->ni_mlpid;
1473		args[1] = ni->ni_mllid;
1474		ieee80211_send_action(ni,
1475		    IEEE80211_ACTION_CAT_MESHPEERING,
1476		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1477		    args);
1478		mesh_peer_timeout_stop(ni);
1479		break;
1480	case IEEE80211_NODE_MESH_ESTABLISHED:
1481		if (ni->ni_mllid != meshpeer->peer_llinkid) {
1482			args[0] = ni->ni_mllid;
1483			args[1] = ni->ni_mlpid;
1484			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1485			ieee80211_send_action(ni,
1486			    IEEE80211_ACTION_CAT_MESHPEERING,
1487			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1488			    args);
1489			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1490			mesh_peer_timeout_setup(ni);
1491			break;
1492		}
1493		args[0] = ni->ni_mlpid;
1494		args[1] = ni->ni_mllid;
1495		ieee80211_send_action(ni,
1496		    IEEE80211_ACTION_CAT_MESHPEERING,
1497		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1498		    args);
1499		break;
1500	case IEEE80211_NODE_MESH_HOLDING:
1501		args[0] = ni->ni_mlpid;
1502		args[1] = meshpeer->peer_llinkid;
1503		args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
1504		ieee80211_send_action(ni,
1505		    IEEE80211_ACTION_CAT_MESHPEERING,
1506		    IEEE80211_ACTION_MESHPEERING_CLOSE,
1507		    args);
1508		break;
1509	}
1510	return 0;
1511}
1512
1513static int
1514mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
1515	const struct ieee80211_frame *wh,
1516	const uint8_t *frm, const uint8_t *efrm)
1517{
1518	struct ieee80211vap *vap = ni->ni_vap;
1519	struct ieee80211_meshpeer_ie ie;
1520	const struct ieee80211_meshpeer_ie *meshpeer;
1521	uint16_t args[3];
1522
1523	/* +2+2+2+2 for action + code + capabilites + status code + AID */
1524	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie);
1525	if (meshpeer == NULL) {
1526		return 0;
1527	}
1528
1529	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1530	    "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
1531	    meshpeer->peer_llinkid, meshpeer->peer_linkid);
1532
1533	switch (ni->ni_mlstate) {
1534	case IEEE80211_NODE_MESH_OPENRCV:
1535		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
1536		mesh_peer_timeout_stop(ni);
1537		break;
1538	case IEEE80211_NODE_MESH_OPENSNT:
1539		mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
1540		break;
1541	case IEEE80211_NODE_MESH_HOLDING:
1542		args[0] = ni->ni_mlpid;
1543		args[1] = meshpeer->peer_llinkid;
1544		args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
1545		ieee80211_send_action(ni,
1546		    IEEE80211_ACTION_CAT_MESHPEERING,
1547		    IEEE80211_ACTION_MESHPEERING_CLOSE,
1548		    args);
1549		break;
1550	case IEEE80211_NODE_MESH_CONFIRMRCV:
1551		if (ni->ni_mllid != meshpeer->peer_llinkid) {
1552			args[0] = ni->ni_mlpid;
1553			args[1] = ni->ni_mllid;
1554			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1555			ieee80211_send_action(ni,
1556			    IEEE80211_ACTION_CAT_MESHPEERING,
1557			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1558			    args);
1559			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1560			mesh_peer_timeout_setup(ni);
1561		}
1562		break;
1563	default:
1564		IEEE80211_DISCARD(vap,
1565		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
1566		    wh, NULL, "received confirm in invalid state %d",
1567		    ni->ni_mlstate);
1568		vap->iv_stats.is_rx_mgtdiscard++;
1569		break;
1570	}
1571	return 0;
1572}
1573
1574static int
1575mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
1576	const struct ieee80211_frame *wh,
1577	const uint8_t *frm, const uint8_t *efrm)
1578{
1579	uint16_t args[3];
1580
1581	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
1582	    ni, "%s", "recv PEER CLOSE");
1583
1584	switch (ni->ni_mlstate) {
1585	case IEEE80211_NODE_MESH_IDLE:
1586		/* ignore */
1587		break;
1588	case IEEE80211_NODE_MESH_OPENRCV:
1589	case IEEE80211_NODE_MESH_OPENSNT:
1590	case IEEE80211_NODE_MESH_CONFIRMRCV:
1591	case IEEE80211_NODE_MESH_ESTABLISHED:
1592		args[0] = ni->ni_mlpid;
1593		args[1] = ni->ni_mllid;
1594		args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
1595		ieee80211_send_action(ni,
1596		    IEEE80211_ACTION_CAT_MESHPEERING,
1597		    IEEE80211_ACTION_MESHPEERING_CLOSE,
1598		    args);
1599		mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1600		mesh_peer_timeout_setup(ni);
1601		break;
1602	case IEEE80211_NODE_MESH_HOLDING:
1603		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
1604		mesh_peer_timeout_setup(ni);
1605		break;
1606	}
1607	return 0;
1608}
1609
1610/*
1611 * Link Metric handling.
1612 */
1613static int
1614mesh_recv_action_meshlmetric_req(struct ieee80211_node *ni,
1615	const struct ieee80211_frame *wh,
1616	const uint8_t *frm, const uint8_t *efrm)
1617{
1618	uint32_t metric;
1619
1620	metric = mesh_airtime_calc(ni);
1621	ieee80211_send_action(ni,
1622	    IEEE80211_ACTION_CAT_MESHLMETRIC,
1623	    IEEE80211_ACTION_MESHLMETRIC_REP,
1624	    &metric);
1625	return 0;
1626}
1627
1628static int
1629mesh_recv_action_meshlmetric_rep(struct ieee80211_node *ni,
1630	const struct ieee80211_frame *wh,
1631	const uint8_t *frm, const uint8_t *efrm)
1632{
1633	return 0;
1634}
1635
1636static int
1637mesh_send_action(struct ieee80211_node *ni, struct mbuf *m)
1638{
1639	struct ieee80211_bpf_params params;
1640
1641	memset(&params, 0, sizeof(params));
1642	params.ibp_pri = WME_AC_VO;
1643	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
1644	/* XXX ucast/mcast */
1645	params.ibp_try0 = ni->ni_txparms->maxretry;
1646	params.ibp_power = ni->ni_txpower;
1647	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
1648	     &params);
1649}
1650
1651#define	ADDSHORT(frm, v) do {			\
1652	frm[0] = (v) & 0xff;			\
1653	frm[1] = (v) >> 8;			\
1654	frm += 2;				\
1655} while (0)
1656#define	ADDWORD(frm, v) do {			\
1657	frm[0] = (v) & 0xff;			\
1658	frm[1] = ((v) >> 8) & 0xff;		\
1659	frm[2] = ((v) >> 16) & 0xff;		\
1660	frm[3] = ((v) >> 24) & 0xff;		\
1661	frm += 4;				\
1662} while (0)
1663
1664static int
1665mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
1666	int category, int action, void *args0)
1667{
1668	struct ieee80211vap *vap = ni->ni_vap;
1669	struct ieee80211com *ic = ni->ni_ic;
1670	uint16_t *args = args0;
1671	const struct ieee80211_rateset *rs;
1672	struct mbuf *m;
1673	uint8_t *frm;
1674
1675	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1676	    "send PEER OPEN action: localid 0x%x", args[0]);
1677
1678	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1679	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
1680	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
1681	ieee80211_ref_node(ni);
1682
1683	m = ieee80211_getmgtframe(&frm,
1684	    ic->ic_headroom + sizeof(struct ieee80211_frame),
1685	    sizeof(uint16_t)	/* action+category */
1686	    + sizeof(uint16_t)	/* capabilites */
1687	    + sizeof(struct ieee80211_meshpeerver_ie)
1688	    + 2 + IEEE80211_RATE_SIZE
1689	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1690	    + 2 + IEEE80211_MESHID_LEN
1691	    + sizeof(struct ieee80211_meshconf_ie)
1692	    + sizeof(struct ieee80211_meshpeer_ie)
1693	);
1694	if (m != NULL) {
1695		/*
1696		 * mesh peer open action frame format:
1697		 *   [1] category
1698		 *   [1] action
1699		 *   [2] capabilities
1700		 *   [tlv] mesh peer protocol version
1701		 *   [tlv] rates
1702		 *   [tlv] xrates
1703		 *   [tlv] mesh id
1704		 *   [tlv] mesh conf
1705		 *   [tlv] mesh peer link mgmt
1706		 */
1707		*frm++ = category;
1708		*frm++ = action;
1709		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
1710		frm = ieee80211_add_meshpeerver(frm, vap);
1711		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
1712		frm = ieee80211_add_rates(frm, rs);
1713		frm = ieee80211_add_xrates(frm, rs);
1714		frm = ieee80211_add_meshid(frm, vap);
1715		frm = ieee80211_add_meshconf(frm, vap);
1716		frm = ieee80211_add_meshpeer(frm, IEEE80211_MESH_PEER_LINK_OPEN,
1717		    args[0], 0, 0);
1718		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1719		return mesh_send_action(ni, m);
1720	} else {
1721		vap->iv_stats.is_tx_nobuf++;
1722		ieee80211_free_node(ni);
1723		return ENOMEM;
1724	}
1725}
1726
1727static int
1728mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
1729	int category, int action, void *args0)
1730{
1731	struct ieee80211vap *vap = ni->ni_vap;
1732	struct ieee80211com *ic = ni->ni_ic;
1733	uint16_t *args = args0;
1734	const struct ieee80211_rateset *rs;
1735	struct mbuf *m;
1736	uint8_t *frm;
1737
1738	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1739	    "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
1740	    args[0], args[1]);
1741
1742	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1743	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
1744	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
1745	ieee80211_ref_node(ni);
1746
1747	m = ieee80211_getmgtframe(&frm,
1748	    ic->ic_headroom + sizeof(struct ieee80211_frame),
1749	    sizeof(uint16_t)	/* action+category */
1750	    + sizeof(uint16_t)	/* capabilites */
1751	    + sizeof(uint16_t)	/* status code */
1752	    + sizeof(uint16_t)	/* AID */
1753	    + sizeof(struct ieee80211_meshpeerver_ie)
1754	    + 2 + IEEE80211_RATE_SIZE
1755	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1756	    + 2 + IEEE80211_MESHID_LEN
1757	    + sizeof(struct ieee80211_meshconf_ie)
1758	    + sizeof(struct ieee80211_meshpeer_ie)
1759	);
1760	if (m != NULL) {
1761		/*
1762		 * mesh peer confirm action frame format:
1763		 *   [1] category
1764		 *   [1] action
1765		 *   [2] capabilities
1766		 *   [2] status code
1767		 *   [2] association id (peer ID)
1768		 *   [tlv] mesh peer protocol version
1769		 *   [tlv] rates
1770		 *   [tlv] xrates
1771		 *   [tlv] mesh id
1772		 *   [tlv] mesh conf
1773		 *   [tlv] mesh peer link mgmt
1774		 */
1775		*frm++ = category;
1776		*frm++ = action;
1777		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
1778		ADDSHORT(frm, 0);		/* status code */
1779		ADDSHORT(frm, args[1]);		/* AID */
1780		frm = ieee80211_add_meshpeerver(frm, vap);
1781		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
1782		frm = ieee80211_add_rates(frm, rs);
1783		frm = ieee80211_add_xrates(frm, rs);
1784		frm = ieee80211_add_meshid(frm, vap);
1785		frm = ieee80211_add_meshconf(frm, vap);
1786		frm = ieee80211_add_meshpeer(frm,
1787		    IEEE80211_MESH_PEER_LINK_CONFIRM,
1788		    args[0], args[1], 0);
1789		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1790		return mesh_send_action(ni, m);
1791	} else {
1792		vap->iv_stats.is_tx_nobuf++;
1793		ieee80211_free_node(ni);
1794		return ENOMEM;
1795	}
1796}
1797
1798static int
1799mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
1800	int category, int action, void *args0)
1801{
1802	struct ieee80211vap *vap = ni->ni_vap;
1803	struct ieee80211com *ic = ni->ni_ic;
1804	uint16_t *args = args0;
1805	struct mbuf *m;
1806	uint8_t *frm;
1807
1808	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1809	    "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d",
1810	    args[0], args[1], args[2]);
1811
1812	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1813	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
1814	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
1815	ieee80211_ref_node(ni);
1816
1817	m = ieee80211_getmgtframe(&frm,
1818	    ic->ic_headroom + sizeof(struct ieee80211_frame),
1819	    sizeof(uint16_t)	/* action+category */
1820	    + sizeof(uint16_t)	/* reason code */
1821	    + sizeof(struct ieee80211_meshpeerver_ie)
1822	    + 2 + IEEE80211_MESHID_LEN
1823	    + sizeof(struct ieee80211_meshpeer_ie)
1824	);
1825	if (m != NULL) {
1826		/*
1827		 * mesh peer close action frame format:
1828		 *   [1] category
1829		 *   [1] action
1830		 *   [2] reason code
1831		 *   [tlv] mesh peer protocol version
1832		 *   [tlv] mesh id
1833		 *   [tlv] mesh peer link mgmt
1834		 */
1835		*frm++ = category;
1836		*frm++ = action;
1837		ADDSHORT(frm, args[2]);		/* reason code */
1838		frm = ieee80211_add_meshpeerver(frm, vap);
1839		frm = ieee80211_add_meshid(frm, vap);
1840		frm = ieee80211_add_meshpeer(frm,
1841		    IEEE80211_MESH_PEER_LINK_CLOSE,
1842		    args[0], args[1], args[2]);
1843		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1844		return mesh_send_action(ni, m);
1845	} else {
1846		vap->iv_stats.is_tx_nobuf++;
1847		ieee80211_free_node(ni);
1848		return ENOMEM;
1849	}
1850}
1851
1852static int
1853mesh_send_action_meshlink_request(struct ieee80211_node *ni,
1854	int category, int action, void *arg0)
1855{
1856	struct ieee80211vap *vap = ni->ni_vap;
1857	struct ieee80211com *ic = ni->ni_ic;
1858	struct mbuf *m;
1859	uint8_t *frm;
1860
1861	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1862	    "%s", "send LINK METRIC REQUEST action");
1863
1864	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1865	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
1866	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
1867	ieee80211_ref_node(ni);
1868
1869	m = ieee80211_getmgtframe(&frm,
1870	    ic->ic_headroom + sizeof(struct ieee80211_frame),
1871	    sizeof(uint16_t)	/* action+category */
1872	);
1873	if (m != NULL) {
1874		/*
1875		 * mesh link metric request
1876		 *   [1] category
1877		 *   [1] action
1878		 */
1879		*frm++ = category;
1880		*frm++ = action;
1881		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1882		return mesh_send_action(ni, m);
1883	} else {
1884		vap->iv_stats.is_tx_nobuf++;
1885		ieee80211_free_node(ni);
1886		return ENOMEM;
1887	}
1888}
1889
1890static int
1891mesh_send_action_meshlink_reply(struct ieee80211_node *ni,
1892	int category, int action, void *args0)
1893{
1894	struct ieee80211vap *vap = ni->ni_vap;
1895	struct ieee80211com *ic = ni->ni_ic;
1896	uint32_t *metric = args0;
1897	struct mbuf *m;
1898	uint8_t *frm;
1899
1900	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1901	    "send LINK METRIC REPLY action: metric 0x%x", *metric);
1902
1903	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1904	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
1905	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
1906	ieee80211_ref_node(ni);
1907
1908	m = ieee80211_getmgtframe(&frm,
1909	    ic->ic_headroom + sizeof(struct ieee80211_frame),
1910	    sizeof(uint16_t)	/* action+category */
1911	    + sizeof(struct ieee80211_meshlmetric_ie)
1912	);
1913	if (m != NULL) {
1914		/*
1915		 * mesh link metric reply
1916		 *   [1] category
1917		 *   [1] action
1918		 *   [tlv] mesh link metric
1919		 */
1920		*frm++ = category;
1921		*frm++ = action;
1922		frm = ieee80211_add_meshlmetric(frm, *metric);
1923		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1924		return mesh_send_action(ni, m);
1925	} else {
1926		vap->iv_stats.is_tx_nobuf++;
1927		ieee80211_free_node(ni);
1928		return ENOMEM;
1929	}
1930}
1931
1932static void
1933mesh_peer_timeout_setup(struct ieee80211_node *ni)
1934{
1935	switch (ni->ni_mlstate) {
1936	case IEEE80211_NODE_MESH_HOLDING:
1937		ni->ni_mltval = ieee80211_mesh_holdingtimeout;
1938		break;
1939	case IEEE80211_NODE_MESH_CONFIRMRCV:
1940		ni->ni_mltval = ieee80211_mesh_confirmtimeout;
1941		break;
1942	case IEEE80211_NODE_MESH_IDLE:
1943		ni->ni_mltval = 0;
1944		break;
1945	default:
1946		ni->ni_mltval = ieee80211_mesh_retrytimeout;
1947		break;
1948	}
1949	if (ni->ni_mltval)
1950		callout_reset(&ni->ni_mltimer, ni->ni_mltval,
1951		    mesh_peer_timeout_cb, ni);
1952}
1953
1954/*
1955 * Same as above but backoffs timer statisically 50%.
1956 */
1957static void
1958mesh_peer_timeout_backoff(struct ieee80211_node *ni)
1959{
1960	uint32_t r;
1961
1962	r = arc4random();
1963	ni->ni_mltval += r % ni->ni_mltval;
1964	callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
1965	    ni);
1966}
1967
1968static __inline void
1969mesh_peer_timeout_stop(struct ieee80211_node *ni)
1970{
1971	callout_stop(&ni->ni_mltimer);
1972}
1973
1974/*
1975 * Mesh Peer Link Management FSM timeout handling.
1976 */
1977static void
1978mesh_peer_timeout_cb(void *arg)
1979{
1980	struct ieee80211_node *ni = (struct ieee80211_node *)arg;
1981	uint16_t args[3];
1982
1983	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
1984	    ni, "mesh link timeout, state %d, retry counter %d",
1985	    ni->ni_mlstate, ni->ni_mlrcnt);
1986
1987	switch (ni->ni_mlstate) {
1988	case IEEE80211_NODE_MESH_IDLE:
1989	case IEEE80211_NODE_MESH_ESTABLISHED:
1990		break;
1991	case IEEE80211_NODE_MESH_OPENSNT:
1992	case IEEE80211_NODE_MESH_OPENRCV:
1993		if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
1994			args[0] = ni->ni_mlpid;
1995			args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
1996			ieee80211_send_action(ni,
1997			    IEEE80211_ACTION_CAT_MESHPEERING,
1998			    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
1999			ni->ni_mlrcnt = 0;
2000			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2001			mesh_peer_timeout_setup(ni);
2002		} else {
2003			args[0] = ni->ni_mlpid;
2004			ieee80211_send_action(ni,
2005			    IEEE80211_ACTION_CAT_MESHPEERING,
2006			    IEEE80211_ACTION_MESHPEERING_OPEN, args);
2007			ni->ni_mlrcnt++;
2008			mesh_peer_timeout_backoff(ni);
2009		}
2010		break;
2011	case IEEE80211_NODE_MESH_CONFIRMRCV:
2012		if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
2013			args[0] = ni->ni_mlpid;
2014			args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
2015			ieee80211_send_action(ni,
2016			    IEEE80211_ACTION_CAT_MESHPEERING,
2017			    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
2018			ni->ni_mlrcnt = 0;
2019			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2020			mesh_peer_timeout_setup(ni);
2021		} else {
2022			ni->ni_mlrcnt++;
2023			mesh_peer_timeout_setup(ni);
2024		}
2025		break;
2026	case IEEE80211_NODE_MESH_HOLDING:
2027		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
2028		break;
2029	}
2030}
2031
2032static __inline int
2033mesh_verify_meshpeerver(struct ieee80211vap *vap, const uint8_t *ie)
2034{
2035	static const uint8_t peer[4] = IEEE80211_MESHPEERVER_PEER;
2036	const struct ieee80211_meshpeerver_ie *meshpeerver =
2037	    (const struct ieee80211_meshpeerver_ie *) ie;
2038
2039	if (meshpeerver->peerver_len !=
2040	    sizeof(struct ieee80211_meshpeerver_ie) - 2)
2041		return 1;
2042	return memcmp(meshpeerver->peerver_proto, peer, 4);
2043}
2044
2045static __inline int
2046mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
2047{
2048	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2049
2050	if (ie == NULL || ie[1] != ms->ms_idlen)
2051		return 1;
2052	return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
2053}
2054
2055/*
2056 * Check if we are using the same algorithms for this mesh.
2057 */
2058static int
2059mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
2060{
2061	static const uint8_t null[4] = IEEE80211_MESHCONF_NULL;
2062	const struct ieee80211_meshconf_ie *meshconf =
2063	    (const struct ieee80211_meshconf_ie *) ie;
2064	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
2065
2066	if (meshconf == NULL)
2067		return 1;
2068	if (meshconf->conf_ver != IEEE80211_MESHCONF_VERSION) {
2069		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2070		    "wrong mesh conf version: %d\n", meshconf->conf_ver);
2071		return 1;
2072	}
2073	if (memcmp(meshconf->conf_pselid, ms->ms_ppath->mpp_ie, 4) != 0) {
2074		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2075		    "unknown path selection algorithm: 0x%x%x%x%x\n",
2076		    meshconf->conf_pselid[0], meshconf->conf_pselid[1],
2077		    meshconf->conf_pselid[2], meshconf->conf_pselid[3]);
2078		return 1;
2079	}
2080	if (memcmp(meshconf->conf_pmetid, ms->ms_pmetric->mpm_ie, 4) != 0) {
2081		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2082		    "unknown path metric algorithm: 0x%x%x%x%x\n",
2083		    meshconf->conf_pmetid[0], meshconf->conf_pmetid[1],
2084		    meshconf->conf_pmetid[2], meshconf->conf_pmetid[3]);
2085		return 1;
2086	}
2087	if (memcmp(meshconf->conf_ccid, null, 4) != 0) {
2088		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2089		    "unknown congestion sig algorithm: 0x%x%x%x%x\n",
2090		    meshconf->conf_ccid[0], meshconf->conf_ccid[1],
2091		    meshconf->conf_ccid[2], meshconf->conf_ccid[3]);
2092		return 1;
2093	}
2094	if (memcmp(meshconf->conf_syncid, null, 4) != 0) {
2095		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2096		    "unknown sync algorithm: 0x%x%x%x%x\n",
2097		    meshconf->conf_syncid[0], meshconf->conf_syncid[1],
2098		    meshconf->conf_syncid[2], meshconf->conf_syncid[3]);
2099		return 1;
2100	}
2101	if (memcmp(meshconf->conf_authid, null, 4) != 0) {
2102		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2103		    "unknown auth auth algorithm: 0x%x%x%x%x\n",
2104		    meshconf->conf_pselid[0], meshconf->conf_pselid[1],
2105		    meshconf->conf_pselid[2], meshconf->conf_pselid[3]);
2106		return 1;
2107	}
2108	/* Not accepting peers */
2109	if (!(meshconf->conf_cap & IEEE80211_MESHCONF_CAP_AP)) {
2110		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2111		    "not accepting peers: 0x%x\n", meshconf->conf_cap);
2112		return 1;
2113	}
2114	return 0;
2115}
2116
2117static int
2118mesh_verify_meshpeer(struct ieee80211vap *vap, const uint8_t *ie)
2119{
2120	const struct ieee80211_meshpeer_ie *meshpeer =
2121	    (const struct ieee80211_meshpeer_ie *) ie;
2122
2123	if (meshpeer == NULL)
2124		return 1;
2125	switch (meshpeer->peer_subtype) {
2126	case IEEE80211_MESH_PEER_LINK_OPEN:
2127		if (meshpeer->peer_len != 3)
2128			return 1;
2129		break;
2130	case IEEE80211_MESH_PEER_LINK_CONFIRM:
2131		if (meshpeer->peer_len != 5)
2132			return 1;
2133		break;
2134	case IEEE80211_MESH_PEER_LINK_CLOSE:
2135		if (meshpeer->peer_len < 5)
2136			return 1;
2137		if (meshpeer->peer_len == 5 && meshpeer->peer_linkid != 0)
2138			return 1;
2139		if (meshpeer->peer_rcode == 0)
2140			return 1;
2141		break;
2142	}
2143	return 0;
2144}
2145
2146/*
2147 * Add a Mesh ID IE to a frame.
2148 */
2149uint8_t *
2150ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
2151{
2152	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2153
2154	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
2155
2156	*frm++ = IEEE80211_ELEMID_MESHID;
2157	*frm++ = ms->ms_idlen;
2158	memcpy(frm, ms->ms_id, ms->ms_idlen);
2159	return frm + ms->ms_idlen;
2160}
2161
2162/*
2163 * Add a Mesh Configuration IE to a frame.
2164 * For now just use HWMP routing, Airtime link metric, Null Congestion
2165 * Signaling, Null Sync Protocol and Null Authentication.
2166 */
2167uint8_t *
2168ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
2169{
2170	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
2171	static const uint8_t null[4] = IEEE80211_MESHCONF_NULL;
2172
2173	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
2174
2175	*frm++ = IEEE80211_ELEMID_MESHCONF;
2176	*frm++ = sizeof(struct ieee80211_meshconf_ie) - 2;
2177	*frm++ = IEEE80211_MESHCONF_VERSION;
2178	memcpy(frm, ms->ms_ppath->mpp_ie, 4);	/* path selection */
2179	frm += 4;
2180	memcpy(frm, ms->ms_pmetric->mpm_ie, 4);	/* link metric */
2181	frm += 4;
2182	/* XXX null for now */
2183	memcpy(frm, null, 4);			/* congestion control */
2184	frm += 4;
2185	memcpy(frm, null, 4);			/* sync */
2186	frm += 4;
2187	memcpy(frm, null, 4);			/* auth */
2188	frm += 4;
2189	/* NB: set the number of neighbors before the rest */
2190	*frm = (ms->ms_neighbors > 15 ? 15 : ms->ms_neighbors) << 1;
2191	if (ms->ms_flags & IEEE80211_MESHFLAGS_PORTAL)
2192		*frm |= IEEE80211_MESHCONF_FORM_MP;
2193	frm += 1;
2194	if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
2195		*frm |= IEEE80211_MESHCONF_CAP_AP;
2196	if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
2197		*frm |= IEEE80211_MESHCONF_CAP_FWRD;
2198	frm += 1;
2199	return frm;
2200}
2201
2202/*
2203 * Add a Mesh Peer Protocol IE to a frame.
2204 * XXX: needs to grow support for Abbreviated Handshake
2205 */
2206uint8_t *
2207ieee80211_add_meshpeerver(uint8_t *frm, struct ieee80211vap *vap)
2208{
2209	static struct ieee80211_meshpeerver_ie ie = {
2210		.peerver_ie 	= IEEE80211_ELEMID_MESHPEERVER,
2211		.peerver_len 	= 4,
2212		.peerver_proto	= IEEE80211_MESHPEERVER_PEER,
2213	};
2214
2215	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
2216
2217	memcpy(frm, &ie, sizeof(ie));
2218	return frm + sizeof(ie);
2219}
2220
2221/*
2222 * Add a Mesh Peer Management IE to a frame.
2223 */
2224uint8_t *
2225ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
2226    uint16_t peerid, uint16_t reason)
2227{
2228	KASSERT(localid != 0, ("localid == 0"));
2229
2230	*frm++ = IEEE80211_ELEMID_MESHPEER;
2231	switch (subtype) {
2232	case IEEE80211_MESH_PEER_LINK_OPEN:
2233		*frm++ = 3;		/* length */
2234		*frm++ = subtype;
2235		ADDSHORT(frm, localid);	/* local ID */
2236		break;
2237	case IEEE80211_MESH_PEER_LINK_CONFIRM:
2238		KASSERT(peerid != 0, ("sending peer confirm without peer id"));
2239		*frm++ = 5;		/* length */
2240		*frm++ = subtype;
2241		ADDSHORT(frm, localid);	/* local ID */
2242		ADDSHORT(frm, peerid);	/* peer ID */
2243		break;
2244	case IEEE80211_MESH_PEER_LINK_CLOSE:
2245		if (peerid)
2246			*frm++ = 7;	/* length */
2247		else
2248			*frm++ = 5;	/* length */
2249		*frm++ = subtype;
2250		ADDSHORT(frm, localid);	/* local ID */
2251		if (peerid)
2252			ADDSHORT(frm, peerid);	/* peer ID */
2253		ADDSHORT(frm, reason);
2254		break;
2255	}
2256	return frm;
2257}
2258
2259/*
2260 * Compute an Airtime Link Metric for the link with this node.
2261 *
2262 * Based on Draft 3.0 spec (11B.10, p.149).
2263 */
2264/*
2265 * Max 802.11s overhead.
2266 */
2267#define IEEE80211_MESH_MAXOVERHEAD \
2268	(sizeof(struct ieee80211_qosframe_addr4) \
2269	 + sizeof(struct ieee80211_meshcntl_ae11) \
2270	+ sizeof(struct llc) \
2271	+ IEEE80211_ADDR_LEN \
2272	+ IEEE80211_WEP_IVLEN \
2273	+ IEEE80211_WEP_KIDLEN \
2274	+ IEEE80211_WEP_CRCLEN \
2275	+ IEEE80211_WEP_MICLEN \
2276	+ IEEE80211_CRC_LEN)
2277uint32_t
2278mesh_airtime_calc(struct ieee80211_node *ni)
2279{
2280#define M_BITS 8
2281#define S_FACTOR (2 * M_BITS)
2282	struct ieee80211com *ic = ni->ni_ic;
2283	struct ifnet *ifp = ni->ni_vap->iv_ifp;
2284	const static int nbits = 8192 << M_BITS;
2285	uint32_t overhead, rate, errrate;
2286	uint64_t res;
2287
2288	/* Time to transmit a frame */
2289	rate = ni->ni_txrate;
2290	overhead = ieee80211_compute_duration(ic->ic_rt,
2291	    ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
2292	/* Error rate in percentage */
2293	/* XXX assuming small failures are ok */
2294	errrate = (((ifp->if_oerrors +
2295	    ifp->if_ierrors) / 100) << M_BITS) / 100;
2296	res = (overhead + (nbits / rate)) *
2297	    ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
2298
2299	return (uint32_t)(res >> S_FACTOR);
2300#undef M_BITS
2301#undef S_FACTOR
2302}
2303
2304/*
2305 * Add a Mesh Link Metric report IE to a frame.
2306 */
2307uint8_t *
2308ieee80211_add_meshlmetric(uint8_t *frm, uint32_t metric)
2309{
2310	*frm++ = IEEE80211_ELEMID_MESHLINK;
2311	*frm++ = 4;
2312	ADDWORD(frm, metric);
2313	return frm;
2314}
2315#undef ADDSHORT
2316#undef ADDWORD
2317
2318/*
2319 * Initialize any mesh-specific node state.
2320 */
2321void
2322ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
2323{
2324	ni->ni_flags |= IEEE80211_NODE_QOS;
2325	callout_init(&ni->ni_mltimer, CALLOUT_MPSAFE);
2326}
2327
2328/*
2329 * Cleanup any mesh-specific node state.
2330 */
2331void
2332ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
2333{
2334	struct ieee80211vap *vap = ni->ni_vap;
2335	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2336
2337	callout_drain(&ni->ni_mltimer);
2338	/* NB: short-circuit callbacks after mesh_vdetach */
2339	if (vap->iv_mesh != NULL)
2340		ms->ms_ppath->mpp_peerdown(ni);
2341}
2342
2343void
2344ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
2345{
2346	ni->ni_meshidlen = ie[1];
2347	memcpy(ni->ni_meshid, ie + 2, ie[1]);
2348}
2349
2350/*
2351 * Setup mesh-specific node state on neighbor discovery.
2352 */
2353void
2354ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
2355	const struct ieee80211_frame *wh,
2356	const struct ieee80211_scanparams *sp)
2357{
2358	ieee80211_parse_meshid(ni, sp->meshid);
2359}
2360
2361static int
2362mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
2363{
2364	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2365	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
2366	struct ieee80211_mesh_route *rt;
2367	struct ieee80211req_mesh_route *imr;
2368	size_t len, off;
2369	uint8_t *p;
2370	int error;
2371
2372	if (vap->iv_opmode != IEEE80211_M_MBSS)
2373		return ENOSYS;
2374
2375	error = 0;
2376	switch (ireq->i_type) {
2377	case IEEE80211_IOC_MESH_ID:
2378		ireq->i_len = ms->ms_idlen;
2379		memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
2380		error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
2381		break;
2382	case IEEE80211_IOC_MESH_AP:
2383		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
2384		break;
2385	case IEEE80211_IOC_MESH_FWRD:
2386		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
2387		break;
2388	case IEEE80211_IOC_MESH_TTL:
2389		ireq->i_val = ms->ms_ttl;
2390		break;
2391	case IEEE80211_IOC_MESH_RTCMD:
2392		switch (ireq->i_val) {
2393		case IEEE80211_MESH_RTCMD_LIST:
2394			len = 0;
2395			MESH_RT_LOCK(ms);
2396			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
2397				len += sizeof(*imr);
2398			}
2399			MESH_RT_UNLOCK(ms);
2400			if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
2401				ireq->i_len = len;
2402				return ENOMEM;
2403			}
2404			ireq->i_len = len;
2405			p = malloc(len, M_TEMP, M_NOWAIT | M_ZERO);
2406			if (p == NULL)
2407				return ENOMEM;
2408			off = 0;
2409			MESH_RT_LOCK(ms);
2410			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
2411				if (off >= len)
2412					break;
2413				imr = (struct ieee80211req_mesh_route *)
2414				    (p + off);
2415				IEEE80211_ADDR_COPY(imr->imr_dest,
2416				    rt->rt_dest);
2417				IEEE80211_ADDR_COPY(imr->imr_nexthop,
2418				    rt->rt_nexthop);
2419				imr->imr_metric = rt->rt_metric;
2420				imr->imr_nhops = rt->rt_nhops;
2421				imr->imr_lifetime = rt->rt_lifetime;
2422				off += sizeof(*imr);
2423			}
2424			MESH_RT_UNLOCK(ms);
2425			error = copyout(p, (uint8_t *)ireq->i_data,
2426			    ireq->i_len);
2427			free(p, M_TEMP);
2428			break;
2429		case IEEE80211_MESH_RTCMD_FLUSH:
2430		case IEEE80211_MESH_RTCMD_ADD:
2431		case IEEE80211_MESH_RTCMD_DELETE:
2432			return EINVAL;
2433		default:
2434			return ENOSYS;
2435		}
2436		break;
2437	case IEEE80211_IOC_MESH_PR_METRIC:
2438		len = strlen(ms->ms_pmetric->mpm_descr);
2439		if (ireq->i_len < len)
2440			return EINVAL;
2441		ireq->i_len = len;
2442		error = copyout(ms->ms_pmetric->mpm_descr,
2443		    (uint8_t *)ireq->i_data, len);
2444		break;
2445	case IEEE80211_IOC_MESH_PR_PATH:
2446		len = strlen(ms->ms_ppath->mpp_descr);
2447		if (ireq->i_len < len)
2448			return EINVAL;
2449		ireq->i_len = len;
2450		error = copyout(ms->ms_ppath->mpp_descr,
2451		    (uint8_t *)ireq->i_data, len);
2452		break;
2453	default:
2454		return ENOSYS;
2455	}
2456
2457	return error;
2458}
2459IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
2460
2461static int
2462mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
2463{
2464	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2465	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
2466	uint8_t tmpaddr[IEEE80211_ADDR_LEN];
2467	char tmpproto[IEEE80211_MESH_PROTO_DSZ];
2468	int error;
2469
2470	if (vap->iv_opmode != IEEE80211_M_MBSS)
2471		return ENOSYS;
2472
2473	error = 0;
2474	switch (ireq->i_type) {
2475	case IEEE80211_IOC_MESH_ID:
2476		if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
2477			return EINVAL;
2478		error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
2479		if (error)
2480			break;
2481		memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
2482		ms->ms_idlen = ireq->i_len;
2483		memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
2484		break;
2485	case IEEE80211_IOC_MESH_AP:
2486		if (ireq->i_val)
2487			ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
2488		else
2489			ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
2490		break;
2491	case IEEE80211_IOC_MESH_FWRD:
2492		if (ireq->i_val)
2493			ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
2494		else
2495			ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
2496		break;
2497	case IEEE80211_IOC_MESH_TTL:
2498		ms->ms_ttl = (uint8_t) ireq->i_val;
2499		break;
2500	case IEEE80211_IOC_MESH_RTCMD:
2501		switch (ireq->i_val) {
2502		case IEEE80211_MESH_RTCMD_LIST:
2503			return EINVAL;
2504		case IEEE80211_MESH_RTCMD_FLUSH:
2505			ieee80211_mesh_rt_flush(vap);
2506			break;
2507		case IEEE80211_MESH_RTCMD_ADD:
2508			if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) ||
2509			    IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data))
2510				return EINVAL;
2511			error = copyin(ireq->i_data, &tmpaddr,
2512			    IEEE80211_ADDR_LEN);
2513			if (!error)
2514				ieee80211_mesh_discover(vap, tmpaddr, NULL);
2515			break;
2516		case IEEE80211_MESH_RTCMD_DELETE:
2517			ieee80211_mesh_rt_del(vap, ireq->i_data);
2518			break;
2519		default:
2520			return ENOSYS;
2521		}
2522		break;
2523	case IEEE80211_IOC_MESH_PR_METRIC:
2524		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
2525		if (!error)
2526			return mesh_select_proto_metric(vap, tmpproto);
2527		break;
2528	case IEEE80211_IOC_MESH_PR_PATH:
2529		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
2530		if (!error)
2531			return mesh_select_proto_path(vap, tmpproto);
2532		break;
2533	default:
2534		return ENOSYS;
2535	}
2536	return error;
2537}
2538IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);
2539