xref: /freebsd/sys/net80211/ieee80211_hwmp.c (revision a5ff72cb0e51a7675d4e2b5810a2b6dad5b91960)
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$");
32 #endif
33 
34 /*
35  * IEEE 802.11s Hybrid Wireless Mesh Protocol, HWMP.
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 <net/bpf.h>
61 
62 #include <net80211/ieee80211_var.h>
63 #include <net80211/ieee80211_action.h>
64 #include <net80211/ieee80211_input.h>
65 #include <net80211/ieee80211_mesh.h>
66 
67 static void	hwmp_vattach(struct ieee80211vap *);
68 static void	hwmp_vdetach(struct ieee80211vap *);
69 static int	hwmp_newstate(struct ieee80211vap *,
70 		    enum ieee80211_state, int);
71 static int	hwmp_send_action(struct ieee80211vap *,
72 		    const uint8_t [IEEE80211_ADDR_LEN],
73 		    uint8_t *, size_t);
74 static uint8_t * hwmp_add_meshpreq(uint8_t *,
75 		    const struct ieee80211_meshpreq_ie *);
76 static uint8_t * hwmp_add_meshprep(uint8_t *,
77 		    const struct ieee80211_meshprep_ie *);
78 static uint8_t * hwmp_add_meshperr(uint8_t *,
79 		    const struct ieee80211_meshperr_ie *);
80 static uint8_t * hwmp_add_meshrann(uint8_t *,
81 		    const struct ieee80211_meshrann_ie *);
82 static void	hwmp_rootmode_setup(struct ieee80211vap *);
83 static void	hwmp_rootmode_cb(void *);
84 static void	hwmp_rootmode_rann_cb(void *);
85 static void	hwmp_recv_preq(struct ieee80211vap *, struct ieee80211_node *,
86 		    const struct ieee80211_frame *,
87 		    const struct ieee80211_meshpreq_ie *);
88 static int	hwmp_send_preq(struct ieee80211vap *,
89 		    const uint8_t [IEEE80211_ADDR_LEN],
90 		    struct ieee80211_meshpreq_ie *,
91 		    struct timeval *, struct timeval *);
92 static void	hwmp_recv_prep(struct ieee80211vap *, struct ieee80211_node *,
93 		    const struct ieee80211_frame *,
94 		    const struct ieee80211_meshprep_ie *);
95 static int	hwmp_send_prep(struct ieee80211vap *,
96 		    const uint8_t [IEEE80211_ADDR_LEN],
97 		    struct ieee80211_meshprep_ie *);
98 static void	hwmp_recv_perr(struct ieee80211vap *, struct ieee80211_node *,
99 		    const struct ieee80211_frame *,
100 		    const struct ieee80211_meshperr_ie *);
101 static int	hwmp_send_perr(struct ieee80211vap *,
102 		    const uint8_t [IEEE80211_ADDR_LEN],
103 		    struct ieee80211_meshperr_ie *);
104 static void	hwmp_senderror(struct ieee80211vap *,
105 		    const uint8_t [IEEE80211_ADDR_LEN],
106 		    struct ieee80211_mesh_route *, int);
107 static void	hwmp_recv_rann(struct ieee80211vap *, struct ieee80211_node *,
108 		   const struct ieee80211_frame *,
109 		   const struct ieee80211_meshrann_ie *);
110 static int	hwmp_send_rann(struct ieee80211vap *,
111 		    const uint8_t [IEEE80211_ADDR_LEN],
112 		    struct ieee80211_meshrann_ie *);
113 static struct ieee80211_node *
114 		hwmp_discover(struct ieee80211vap *,
115 		    const uint8_t [IEEE80211_ADDR_LEN], struct mbuf *);
116 static void	hwmp_peerdown(struct ieee80211_node *);
117 
118 static struct timeval ieee80211_hwmp_preqminint = { 0, 100000 };
119 static struct timeval ieee80211_hwmp_perrminint = { 0, 100000 };
120 
121 
122 /* NB: the Target Address set in a Proactive PREQ is the broadcast address. */
123 static const uint8_t	broadcastaddr[IEEE80211_ADDR_LEN] =
124 	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
125 
126 typedef uint32_t ieee80211_hwmp_seq;
127 #define	HWMP_SEQ_LT(a, b)	((int32_t)((a)-(b)) < 0)
128 #define	HWMP_SEQ_LEQ(a, b)	((int32_t)((a)-(b)) <= 0)
129 #define	HWMP_SEQ_EQ(a, b)	((int32_t)((a)-(b)) == 0)
130 #define	HWMP_SEQ_GT(a, b)	((int32_t)((a)-(b)) > 0)
131 #define	HWMP_SEQ_GEQ(a, b)	((int32_t)((a)-(b)) >= 0)
132 
133 #define HWMP_SEQ_MAX(a, b)	(a > b ? a : b)
134 
135 /*
136  * Private extension of ieee80211_mesh_route.
137  */
138 struct ieee80211_hwmp_route {
139 	ieee80211_hwmp_seq	hr_seq;		/* last HWMP seq seen from dst*/
140 	ieee80211_hwmp_seq	hr_preqid;	/* last PREQ ID seen from dst */
141 	ieee80211_hwmp_seq	hr_origseq;	/* seq. no. on our latest PREQ*/
142 	struct timeval		hr_lastpreq;	/* last time we sent a PREQ */
143 	struct timeval		hr_lastrootconf; /* last sent PREQ root conf */
144 	int			hr_preqretries;	/* number of discoveries */
145 	int			hr_lastdiscovery; /* last discovery in ticks */
146 };
147 struct ieee80211_hwmp_state {
148 	ieee80211_hwmp_seq	hs_seq;		/* next seq to be used */
149 	ieee80211_hwmp_seq	hs_preqid;	/* next PREQ ID to be used */
150 	int			hs_rootmode;	/* proactive HWMP */
151 	struct timeval		hs_lastperr;	/* last time we sent a PERR */
152 	struct callout		hs_roottimer;
153 	uint8_t			hs_maxhops;	/* max hop count */
154 };
155 
156 static SYSCTL_NODE(_net_wlan, OID_AUTO, hwmp, CTLFLAG_RD, 0,
157     "IEEE 802.11s HWMP parameters");
158 static int	ieee80211_hwmp_targetonly = 0;
159 SYSCTL_INT(_net_wlan_hwmp, OID_AUTO, targetonly, CTLFLAG_RW,
160     &ieee80211_hwmp_targetonly, 0, "Set TO bit on generated PREQs");
161 static int	ieee80211_hwmp_pathtimeout = -1;
162 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, pathlifetime, CTLTYPE_INT | CTLFLAG_RW,
163     &ieee80211_hwmp_pathtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
164     "path entry lifetime (ms)");
165 static int	ieee80211_hwmp_maxpreq_retries = -1;
166 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, maxpreq_retries, CTLTYPE_INT | CTLFLAG_RW,
167     &ieee80211_hwmp_maxpreq_retries, 0, ieee80211_sysctl_msecs_ticks, "I",
168     "maximum number of preq retries");
169 static int	ieee80211_hwmp_net_diameter_traversaltime = -1;
170 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, net_diameter_traversal_time,
171     CTLTYPE_INT | CTLFLAG_RW, &ieee80211_hwmp_net_diameter_traversaltime, 0,
172     ieee80211_sysctl_msecs_ticks, "I",
173     "estimate travelse time across the MBSS (ms)");
174 static int	ieee80211_hwmp_roottimeout = -1;
175 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, roottimeout, CTLTYPE_INT | CTLFLAG_RW,
176     &ieee80211_hwmp_roottimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
177     "root PREQ timeout (ms)");
178 static int	ieee80211_hwmp_rootint = -1;
179 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, rootint, CTLTYPE_INT | CTLFLAG_RW,
180     &ieee80211_hwmp_rootint, 0, ieee80211_sysctl_msecs_ticks, "I",
181     "root interval (ms)");
182 static int	ieee80211_hwmp_rannint = -1;
183 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, rannint, CTLTYPE_INT | CTLFLAG_RW,
184     &ieee80211_hwmp_rannint, 0, ieee80211_sysctl_msecs_ticks, "I",
185     "root announcement interval (ms)");
186 static struct timeval ieee80211_hwmp_rootconfint = { 0, 0 };
187 static int	ieee80211_hwmp_rootconfint_internal = -1;
188 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, rootconfint, CTLTYPE_INT | CTLFLAG_RD,
189     &ieee80211_hwmp_rootconfint_internal, 0, ieee80211_sysctl_msecs_ticks, "I",
190     "root confirmation interval (ms) (read-only)");
191 
192 #define	IEEE80211_HWMP_DEFAULT_MAXHOPS	31
193 
194 static	ieee80211_recv_action_func hwmp_recv_action_meshpath;
195 
196 static struct ieee80211_mesh_proto_path mesh_proto_hwmp = {
197 	.mpp_descr	= "HWMP",
198 	.mpp_ie		= IEEE80211_MESHCONF_PATH_HWMP,
199 	.mpp_discover	= hwmp_discover,
200 	.mpp_peerdown	= hwmp_peerdown,
201 	.mpp_senderror	= hwmp_senderror,
202 	.mpp_vattach	= hwmp_vattach,
203 	.mpp_vdetach	= hwmp_vdetach,
204 	.mpp_newstate	= hwmp_newstate,
205 	.mpp_privlen	= sizeof(struct ieee80211_hwmp_route),
206 };
207 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, inact, CTLTYPE_INT | CTLFLAG_RW,
208 	&mesh_proto_hwmp.mpp_inact, 0, ieee80211_sysctl_msecs_ticks, "I",
209 	"mesh route inactivity timeout (ms)");
210 
211 
212 static void
213 ieee80211_hwmp_init(void)
214 {
215 	/* Default values as per amendment */
216 	ieee80211_hwmp_pathtimeout = msecs_to_ticks(5*1000);
217 	ieee80211_hwmp_roottimeout = msecs_to_ticks(5*1000);
218 	ieee80211_hwmp_rootint = msecs_to_ticks(2*1000);
219 	ieee80211_hwmp_rannint = msecs_to_ticks(1*1000);
220 	ieee80211_hwmp_rootconfint_internal = msecs_to_ticks(2*1000);
221 	ieee80211_hwmp_maxpreq_retries = 3;
222 	/*
223 	 * (TU): A measurement of time equal to 1024 μs,
224 	 * 500 TU is 512 ms.
225 	 */
226 	ieee80211_hwmp_net_diameter_traversaltime = msecs_to_ticks(512);
227 
228 	/*
229 	 * NB: I dont know how to make SYSCTL_PROC that calls ms to ticks
230 	 * and return a struct timeval...
231 	 */
232 	ieee80211_hwmp_rootconfint.tv_usec =
233 	    ieee80211_hwmp_rootconfint_internal * 1000;
234 
235 	/*
236 	 * Register action frame handler.
237 	 */
238 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
239 	    IEEE80211_ACTION_MESH_HWMP, hwmp_recv_action_meshpath);
240 
241 	/* NB: default is 5 secs per spec */
242 	mesh_proto_hwmp.mpp_inact = msecs_to_ticks(5*1000);
243 
244 	/*
245 	 * Register HWMP.
246 	 */
247 	ieee80211_mesh_register_proto_path(&mesh_proto_hwmp);
248 }
249 SYSINIT(wlan_hwmp, SI_SUB_DRIVERS, SI_ORDER_SECOND, ieee80211_hwmp_init, NULL);
250 
251 static void
252 hwmp_vattach(struct ieee80211vap *vap)
253 {
254 	struct ieee80211_hwmp_state *hs;
255 
256 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS,
257 	    ("not a mesh vap, opmode %d", vap->iv_opmode));
258 
259 	hs = IEEE80211_MALLOC(sizeof(struct ieee80211_hwmp_state), M_80211_VAP,
260 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
261 	if (hs == NULL) {
262 		printf("%s: couldn't alloc HWMP state\n", __func__);
263 		return;
264 	}
265 	hs->hs_maxhops = IEEE80211_HWMP_DEFAULT_MAXHOPS;
266 	callout_init(&hs->hs_roottimer, 1);
267 	vap->iv_hwmp = hs;
268 }
269 
270 static void
271 hwmp_vdetach(struct ieee80211vap *vap)
272 {
273 	struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
274 
275 	callout_drain(&hs->hs_roottimer);
276 	IEEE80211_FREE(vap->iv_hwmp, M_80211_VAP);
277 	vap->iv_hwmp = NULL;
278 }
279 
280 static int
281 hwmp_newstate(struct ieee80211vap *vap, enum ieee80211_state ostate, int arg)
282 {
283 	enum ieee80211_state nstate = vap->iv_state;
284 	struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
285 
286 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
287 	    __func__, ieee80211_state_name[ostate],
288 	    ieee80211_state_name[nstate], arg);
289 
290 	if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN)
291 		callout_drain(&hs->hs_roottimer);
292 	if (nstate == IEEE80211_S_RUN)
293 		hwmp_rootmode_setup(vap);
294 	return 0;
295 }
296 
297 /*
298  * Verify the length of an HWMP PREQ and return the number
299  * of destinations >= 1, if verification fails -1 is returned.
300  */
301 static int
302 verify_mesh_preq_len(struct ieee80211vap *vap,
303     const struct ieee80211_frame *wh, const uint8_t *iefrm)
304 {
305 	int alloc_sz = -1;
306 	int ndest = -1;
307 	if (iefrm[2] & IEEE80211_MESHPREQ_FLAGS_AE) {
308 		/* Originator External Address  present */
309 		alloc_sz =  IEEE80211_MESHPREQ_BASE_SZ_AE;
310 		ndest = iefrm[IEEE80211_MESHPREQ_TCNT_OFFSET_AE];
311 	} else {
312 		/* w/o Originator External Address */
313 		alloc_sz =  IEEE80211_MESHPREQ_BASE_SZ;
314 		ndest = iefrm[IEEE80211_MESHPREQ_TCNT_OFFSET];
315 	}
316 	alloc_sz += ndest * IEEE80211_MESHPREQ_TRGT_SZ;
317 
318 	if(iefrm[1] != (alloc_sz)) {
319 		IEEE80211_DISCARD(vap,
320 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP,
321 		    wh, NULL, "PREQ (AE=%s) with wrong len",
322 		    iefrm[2] & IEEE80211_MESHPREQ_FLAGS_AE ? "1" : "0");
323 		return (-1);
324 	}
325 	return ndest;
326 }
327 
328 /*
329  * Verify the length of an HWMP PREP and returns 1 on success,
330  * otherwise -1.
331  */
332 static int
333 verify_mesh_prep_len(struct ieee80211vap *vap,
334     const struct ieee80211_frame *wh, const uint8_t *iefrm)
335 {
336 	int alloc_sz = -1;
337 	if (iefrm[2] & IEEE80211_MESHPREP_FLAGS_AE) {
338 		if (iefrm[1] == IEEE80211_MESHPREP_BASE_SZ_AE)
339 			alloc_sz = IEEE80211_MESHPREP_BASE_SZ_AE;
340 	} else if (iefrm[1] == IEEE80211_MESHPREP_BASE_SZ)
341 		alloc_sz = IEEE80211_MESHPREP_BASE_SZ;
342 	if(alloc_sz < 0) {
343 		IEEE80211_DISCARD(vap,
344 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP,
345 		    wh, NULL, "PREP (AE=%s) with wrong len",
346 		    iefrm[2] & IEEE80211_MESHPREP_FLAGS_AE ? "1" : "0");
347 		return (-1);
348 	}
349 	return (1);
350 }
351 
352 /*
353  * Verify the length of an HWMP PERR and return the number
354  * of destinations >= 1, if verification fails -1 is returned.
355  */
356 static int
357 verify_mesh_perr_len(struct ieee80211vap *vap,
358     const struct ieee80211_frame *wh, const uint8_t *iefrm)
359 {
360 	int alloc_sz = -1;
361 	const uint8_t *iefrm_t = iefrm;
362 	uint8_t ndest = iefrm_t[IEEE80211_MESHPERR_NDEST_OFFSET];
363 	int i;
364 
365 	if(ndest > IEEE80211_MESHPERR_MAXDEST) {
366 		IEEE80211_DISCARD(vap,
367 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP,
368 		    wh, NULL, "PERR with wrong number of destionat (>19), %u",
369 		    ndest);
370 		return (-1);
371 	}
372 
373 	iefrm_t += IEEE80211_MESHPERR_NDEST_OFFSET + 1; /* flag is next field */
374 	/* We need to check each destionation flag to know size */
375 	for(i = 0; i<ndest; i++) {
376 		if ((*iefrm_t) & IEEE80211_MESHPERR_FLAGS_AE)
377 			iefrm_t += IEEE80211_MESHPERR_DEST_SZ_AE;
378 		else
379 			iefrm_t += IEEE80211_MESHPERR_DEST_SZ;
380 	}
381 
382 	alloc_sz = (iefrm_t - iefrm) - 2; /* action + code */
383 	if(alloc_sz !=  iefrm[1]) {
384 		IEEE80211_DISCARD(vap,
385 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP,
386 		    wh, NULL, "%s", "PERR with wrong len");
387 		return (-1);
388 	}
389 	return ndest;
390 }
391 
392 static int
393 hwmp_recv_action_meshpath(struct ieee80211_node *ni,
394 	const struct ieee80211_frame *wh,
395 	const uint8_t *frm, const uint8_t *efrm)
396 {
397 	struct ieee80211vap *vap = ni->ni_vap;
398 	struct ieee80211_meshpreq_ie *preq;
399 	struct ieee80211_meshprep_ie *prep;
400 	struct ieee80211_meshperr_ie *perr;
401 	struct ieee80211_meshrann_ie rann;
402 	const uint8_t *iefrm = frm + 2; /* action + code */
403 	const uint8_t *iefrm_t = iefrm; /* temporary pointer */
404 	int ndest = -1;
405 	int found = 0;
406 
407 	while (efrm - iefrm > 1) {
408 		IEEE80211_VERIFY_LENGTH(efrm - iefrm, iefrm[1] + 2, return 0);
409 		switch (*iefrm) {
410 		case IEEE80211_ELEMID_MESHPREQ:
411 		{
412 			int i = 0;
413 
414 			iefrm_t = iefrm;
415 			ndest = verify_mesh_preq_len(vap, wh, iefrm_t);
416 			if (ndest < 0) {
417 				vap->iv_stats.is_rx_mgtdiscard++;
418 				break;
419 			}
420 			preq = IEEE80211_MALLOC(sizeof(*preq) +
421 			    (ndest - 1) * sizeof(*preq->preq_targets),
422 			    M_80211_MESH_PREQ,
423 			    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
424 			KASSERT(preq != NULL, ("preq == NULL"));
425 
426 			preq->preq_ie = *iefrm_t++;
427 			preq->preq_len = *iefrm_t++;
428 			preq->preq_flags = *iefrm_t++;
429 			preq->preq_hopcount = *iefrm_t++;
430 			preq->preq_ttl = *iefrm_t++;
431 			preq->preq_id = le32dec(iefrm_t); iefrm_t += 4;
432 			IEEE80211_ADDR_COPY(preq->preq_origaddr, iefrm_t);
433 			iefrm_t += 6;
434 			preq->preq_origseq = le32dec(iefrm_t); iefrm_t += 4;
435 			/* NB: may have Originator Proxied Address */
436 			if (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AE)  {
437 				IEEE80211_ADDR_COPY(
438 				    preq->preq_orig_ext_addr, iefrm_t);
439 				iefrm_t += 6;
440 			}
441 			preq->preq_lifetime = le32dec(iefrm_t); iefrm_t += 4;
442 			preq->preq_metric = le32dec(iefrm_t); iefrm_t += 4;
443 			preq->preq_tcount = *iefrm_t++;
444 
445 			for (i = 0; i < preq->preq_tcount; i++) {
446 				preq->preq_targets[i].target_flags = *iefrm_t++;
447 				IEEE80211_ADDR_COPY(
448 				    preq->preq_targets[i].target_addr, iefrm_t);
449 				iefrm_t += 6;
450 				preq->preq_targets[i].target_seq =
451 				    le32dec(iefrm_t);
452 				iefrm_t += 4;
453 			}
454 
455 			hwmp_recv_preq(vap, ni, wh, preq);
456 			IEEE80211_FREE(preq, M_80211_MESH_PREQ);
457 			found++;
458 			break;
459 		}
460 		case IEEE80211_ELEMID_MESHPREP:
461 		{
462 			iefrm_t = iefrm;
463 			ndest = verify_mesh_prep_len(vap, wh, iefrm_t);
464 			if (ndest < 0) {
465 				vap->iv_stats.is_rx_mgtdiscard++;
466 				break;
467 			}
468 			prep = IEEE80211_MALLOC(sizeof(*prep),
469 			    M_80211_MESH_PREP,
470 			    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
471 			KASSERT(prep != NULL, ("prep == NULL"));
472 
473 			prep->prep_ie = *iefrm_t++;
474 			prep->prep_len = *iefrm_t++;
475 			prep->prep_flags = *iefrm_t++;
476 			prep->prep_hopcount = *iefrm_t++;
477 			prep->prep_ttl = *iefrm_t++;
478 			IEEE80211_ADDR_COPY(prep->prep_targetaddr, iefrm_t);
479 			iefrm_t += 6;
480 			prep->prep_targetseq = le32dec(iefrm_t); iefrm_t += 4;
481 			/* NB: May have Target Proxied Address */
482 			if (prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE)  {
483 				IEEE80211_ADDR_COPY(
484 				    prep->prep_target_ext_addr, iefrm_t);
485 				iefrm_t += 6;
486 			}
487 			prep->prep_lifetime = le32dec(iefrm_t); iefrm_t += 4;
488 			prep->prep_metric = le32dec(iefrm_t); iefrm_t += 4;
489 			IEEE80211_ADDR_COPY(prep->prep_origaddr, iefrm_t);
490 			iefrm_t += 6;
491 			prep->prep_origseq = le32dec(iefrm_t); iefrm_t += 4;
492 
493 			hwmp_recv_prep(vap, ni, wh, prep);
494 			IEEE80211_FREE(prep, M_80211_MESH_PREP);
495 			found++;
496 			break;
497 		}
498 		case IEEE80211_ELEMID_MESHPERR:
499 		{
500 			int i = 0;
501 
502 			iefrm_t = iefrm;
503 			ndest = verify_mesh_perr_len(vap, wh, iefrm_t);
504 			if (ndest < 0) {
505 				vap->iv_stats.is_rx_mgtdiscard++;
506 				break;
507 			}
508 			perr = IEEE80211_MALLOC(sizeof(*perr) +
509 			    (ndest - 1) * sizeof(*perr->perr_dests),
510 			    M_80211_MESH_PERR,
511 			    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
512 			KASSERT(perr != NULL, ("perr == NULL"));
513 
514 			perr->perr_ie = *iefrm_t++;
515 			perr->perr_len = *iefrm_t++;
516 			perr->perr_ttl = *iefrm_t++;
517 			perr->perr_ndests = *iefrm_t++;
518 
519 			for (i = 0; i<perr->perr_ndests; i++) {
520 				perr->perr_dests[i].dest_flags = *iefrm_t++;
521 				IEEE80211_ADDR_COPY(
522 				    perr->perr_dests[i].dest_addr, iefrm_t);
523 				iefrm_t += 6;
524 				perr->perr_dests[i].dest_seq = le32dec(iefrm_t);
525 				iefrm_t += 4;
526 				/* NB: May have Target Proxied Address */
527 				if (perr->perr_dests[i].dest_flags &
528 				    IEEE80211_MESHPERR_FLAGS_AE) {
529 					IEEE80211_ADDR_COPY(
530 					    perr->perr_dests[i].dest_ext_addr,
531 					    iefrm_t);
532 					iefrm_t += 6;
533 				}
534 				perr->perr_dests[i].dest_rcode =
535 				    le16dec(iefrm_t);
536 				iefrm_t += 2;
537 			}
538 
539 			hwmp_recv_perr(vap, ni, wh, perr);
540 			IEEE80211_FREE(perr, M_80211_MESH_PERR);
541 			found++;
542 			break;
543 		}
544 		case IEEE80211_ELEMID_MESHRANN:
545 		{
546 			const struct ieee80211_meshrann_ie *mrann =
547 			    (const struct ieee80211_meshrann_ie *) iefrm;
548 			if (mrann->rann_len !=
549 			    sizeof(struct ieee80211_meshrann_ie) - 2) {
550 				IEEE80211_DISCARD(vap,
551 				    IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP,
552 				    wh, NULL, "%s", "RAN with wrong len");
553 				    vap->iv_stats.is_rx_mgtdiscard++;
554 				return 1;
555 			}
556 			memcpy(&rann, mrann, sizeof(rann));
557 			rann.rann_seq = le32dec(&mrann->rann_seq);
558 			rann.rann_interval = le32dec(&mrann->rann_interval);
559 			rann.rann_metric = le32dec(&mrann->rann_metric);
560 			hwmp_recv_rann(vap, ni, wh, &rann);
561 			found++;
562 			break;
563 		}
564 		}
565 		iefrm += iefrm[1] + 2;
566 	}
567 	if (!found) {
568 		IEEE80211_DISCARD(vap,
569 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP,
570 		    wh, NULL, "%s", "PATH SEL action without IE");
571 		vap->iv_stats.is_rx_mgtdiscard++;
572 	}
573 	return 0;
574 }
575 
576 static int
577 hwmp_send_action(struct ieee80211vap *vap,
578     const uint8_t da[IEEE80211_ADDR_LEN],
579     uint8_t *ie, size_t len)
580 {
581 	struct ieee80211_node *ni;
582 	struct ieee80211com *ic;
583 	struct ieee80211_bpf_params params;
584 	struct mbuf *m;
585 	uint8_t *frm;
586 	int ret;
587 
588 	if (IEEE80211_IS_MULTICAST(da)) {
589 		ni = ieee80211_ref_node(vap->iv_bss);
590 #ifdef IEEE80211_DEBUG_REFCNT
591 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
592 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
593 		__func__, __LINE__,
594 		ni, ether_sprintf(ni->ni_macaddr),
595 		ieee80211_node_refcnt(ni)+1);
596 #endif
597 		ieee80211_ref_node(ni);
598 	}
599 	else
600 		ni = ieee80211_mesh_find_txnode(vap, da);
601 
602 	if (vap->iv_state == IEEE80211_S_CAC) {
603 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
604 		    "block %s frame in CAC state", "HWMP action");
605 		vap->iv_stats.is_tx_badstate++;
606 		return EIO;	/* XXX */
607 	}
608 
609 	KASSERT(ni != NULL, ("null node"));
610 	ic = ni->ni_ic;
611 
612 	m = ieee80211_getmgtframe(&frm,
613 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
614 	    sizeof(struct ieee80211_action) + len
615 	);
616 	if (m == NULL) {
617 		ieee80211_free_node(ni);
618 		vap->iv_stats.is_tx_nobuf++;
619 		return ENOMEM;
620 	}
621 	*frm++ = IEEE80211_ACTION_CAT_MESH;
622 	*frm++ = IEEE80211_ACTION_MESH_HWMP;
623 	switch (*ie) {
624 	case IEEE80211_ELEMID_MESHPREQ:
625 		frm = hwmp_add_meshpreq(frm,
626 		    (struct ieee80211_meshpreq_ie *)ie);
627 		break;
628 	case IEEE80211_ELEMID_MESHPREP:
629 		frm = hwmp_add_meshprep(frm,
630 		    (struct ieee80211_meshprep_ie *)ie);
631 		break;
632 	case IEEE80211_ELEMID_MESHPERR:
633 		frm = hwmp_add_meshperr(frm,
634 		    (struct ieee80211_meshperr_ie *)ie);
635 		break;
636 	case IEEE80211_ELEMID_MESHRANN:
637 		frm = hwmp_add_meshrann(frm,
638 		    (struct ieee80211_meshrann_ie *)ie);
639 		break;
640 	}
641 
642 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
643 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
644 	if (m == NULL) {
645 		ieee80211_free_node(ni);
646 		vap->iv_stats.is_tx_nobuf++;
647 		return ENOMEM;
648 	}
649 
650 	IEEE80211_TX_LOCK(ic);
651 
652 	ieee80211_send_setup(ni, m,
653 	    IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_ACTION,
654 	    IEEE80211_NONQOS_TID, vap->iv_myaddr, da, vap->iv_myaddr);
655 
656 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
657 	IEEE80211_NODE_STAT(ni, tx_mgmt);
658 
659 	memset(&params, 0, sizeof(params));
660 	params.ibp_pri = WME_AC_VO;
661 	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
662 	if (IEEE80211_IS_MULTICAST(da))
663 		params.ibp_try0 = 1;
664 	else
665 		params.ibp_try0 = ni->ni_txparms->maxretry;
666 	params.ibp_power = ni->ni_txpower;
667 	ret = ieee80211_raw_output(vap, ni, m, &params);
668 	IEEE80211_TX_UNLOCK(ic);
669 	return (ret);
670 }
671 
672 #define ADDSHORT(frm, v) do {		\
673 	le16enc(frm, v);		\
674 	frm += 2;			\
675 } while (0)
676 #define ADDWORD(frm, v) do {		\
677 	le32enc(frm, v);		\
678 	frm += 4;			\
679 } while (0)
680 /*
681  * Add a Mesh Path Request IE to a frame.
682  */
683 #define	PREQ_TFLAGS(n)	preq->preq_targets[n].target_flags
684 #define	PREQ_TADDR(n)	preq->preq_targets[n].target_addr
685 #define	PREQ_TSEQ(n)	preq->preq_targets[n].target_seq
686 static uint8_t *
687 hwmp_add_meshpreq(uint8_t *frm, const struct ieee80211_meshpreq_ie *preq)
688 {
689 	int i;
690 
691 	*frm++ = IEEE80211_ELEMID_MESHPREQ;
692 	*frm++ = preq->preq_len;	/* len already calculated */
693 	*frm++ = preq->preq_flags;
694 	*frm++ = preq->preq_hopcount;
695 	*frm++ = preq->preq_ttl;
696 	ADDWORD(frm, preq->preq_id);
697 	IEEE80211_ADDR_COPY(frm, preq->preq_origaddr); frm += 6;
698 	ADDWORD(frm, preq->preq_origseq);
699 	if (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AE) {
700 		IEEE80211_ADDR_COPY(frm, preq->preq_orig_ext_addr);
701 		frm += 6;
702 	}
703 	ADDWORD(frm, preq->preq_lifetime);
704 	ADDWORD(frm, preq->preq_metric);
705 	*frm++ = preq->preq_tcount;
706 	for (i = 0; i < preq->preq_tcount; i++) {
707 		*frm++ = PREQ_TFLAGS(i);
708 		IEEE80211_ADDR_COPY(frm, PREQ_TADDR(i));
709 		frm += 6;
710 		ADDWORD(frm, PREQ_TSEQ(i));
711 	}
712 	return frm;
713 }
714 #undef	PREQ_TFLAGS
715 #undef	PREQ_TADDR
716 #undef	PREQ_TSEQ
717 
718 /*
719  * Add a Mesh Path Reply IE to a frame.
720  */
721 static uint8_t *
722 hwmp_add_meshprep(uint8_t *frm, const struct ieee80211_meshprep_ie *prep)
723 {
724 	*frm++ = IEEE80211_ELEMID_MESHPREP;
725 	*frm++ = prep->prep_len;	/* len already calculated */
726 	*frm++ = prep->prep_flags;
727 	*frm++ = prep->prep_hopcount;
728 	*frm++ = prep->prep_ttl;
729 	IEEE80211_ADDR_COPY(frm, prep->prep_targetaddr); frm += 6;
730 	ADDWORD(frm, prep->prep_targetseq);
731 	if (prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE) {
732 		IEEE80211_ADDR_COPY(frm, prep->prep_target_ext_addr);
733 		frm += 6;
734 	}
735 	ADDWORD(frm, prep->prep_lifetime);
736 	ADDWORD(frm, prep->prep_metric);
737 	IEEE80211_ADDR_COPY(frm, prep->prep_origaddr); frm += 6;
738 	ADDWORD(frm, prep->prep_origseq);
739 	return frm;
740 }
741 
742 /*
743  * Add a Mesh Path Error IE to a frame.
744  */
745 #define	PERR_DFLAGS(n)	perr->perr_dests[n].dest_flags
746 #define	PERR_DADDR(n)	perr->perr_dests[n].dest_addr
747 #define	PERR_DSEQ(n)	perr->perr_dests[n].dest_seq
748 #define	PERR_EXTADDR(n)	perr->perr_dests[n].dest_ext_addr
749 #define	PERR_DRCODE(n)	perr->perr_dests[n].dest_rcode
750 static uint8_t *
751 hwmp_add_meshperr(uint8_t *frm, const struct ieee80211_meshperr_ie *perr)
752 {
753 	int i;
754 
755 	*frm++ = IEEE80211_ELEMID_MESHPERR;
756 	*frm++ = perr->perr_len;	/* len already calculated */
757 	*frm++ = perr->perr_ttl;
758 	*frm++ = perr->perr_ndests;
759 	for (i = 0; i < perr->perr_ndests; i++) {
760 		*frm++ = PERR_DFLAGS(i);
761 		IEEE80211_ADDR_COPY(frm, PERR_DADDR(i));
762 		frm += 6;
763 		ADDWORD(frm, PERR_DSEQ(i));
764 		if (PERR_DFLAGS(i) & IEEE80211_MESHPERR_FLAGS_AE) {
765 			IEEE80211_ADDR_COPY(frm, PERR_EXTADDR(i));
766 			frm += 6;
767 		}
768 		ADDSHORT(frm, PERR_DRCODE(i));
769 	}
770 	return frm;
771 }
772 #undef	PERR_DFLAGS
773 #undef	PERR_DADDR
774 #undef	PERR_DSEQ
775 #undef	PERR_EXTADDR
776 #undef	PERR_DRCODE
777 
778 /*
779  * Add a Root Annoucement IE to a frame.
780  */
781 static uint8_t *
782 hwmp_add_meshrann(uint8_t *frm, const struct ieee80211_meshrann_ie *rann)
783 {
784 	*frm++ = IEEE80211_ELEMID_MESHRANN;
785 	*frm++ = rann->rann_len;
786 	*frm++ = rann->rann_flags;
787 	*frm++ = rann->rann_hopcount;
788 	*frm++ = rann->rann_ttl;
789 	IEEE80211_ADDR_COPY(frm, rann->rann_addr); frm += 6;
790 	ADDWORD(frm, rann->rann_seq);
791 	ADDWORD(frm, rann->rann_interval);
792 	ADDWORD(frm, rann->rann_metric);
793 	return frm;
794 }
795 
796 static void
797 hwmp_rootmode_setup(struct ieee80211vap *vap)
798 {
799 	struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
800 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
801 
802 	switch (hs->hs_rootmode) {
803 	case IEEE80211_HWMP_ROOTMODE_DISABLED:
804 		callout_drain(&hs->hs_roottimer);
805 		ms->ms_flags &= ~IEEE80211_MESHFLAGS_ROOT;
806 		break;
807 	case IEEE80211_HWMP_ROOTMODE_NORMAL:
808 	case IEEE80211_HWMP_ROOTMODE_PROACTIVE:
809 		callout_reset(&hs->hs_roottimer, ieee80211_hwmp_rootint,
810 		    hwmp_rootmode_cb, vap);
811 		ms->ms_flags |= IEEE80211_MESHFLAGS_ROOT;
812 		break;
813 	case IEEE80211_HWMP_ROOTMODE_RANN:
814 		callout_reset(&hs->hs_roottimer, ieee80211_hwmp_rannint,
815 		    hwmp_rootmode_rann_cb, vap);
816 		ms->ms_flags |= IEEE80211_MESHFLAGS_ROOT;
817 		break;
818 	}
819 }
820 
821 /*
822  * Send a broadcast Path Request to find all nodes on the mesh. We are
823  * called when the vap is configured as a HWMP root node.
824  */
825 #define	PREQ_TFLAGS(n)	preq.preq_targets[n].target_flags
826 #define	PREQ_TADDR(n)	preq.preq_targets[n].target_addr
827 #define	PREQ_TSEQ(n)	preq.preq_targets[n].target_seq
828 static void
829 hwmp_rootmode_cb(void *arg)
830 {
831 	struct ieee80211vap *vap = (struct ieee80211vap *)arg;
832 	struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
833 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
834 	struct ieee80211_meshpreq_ie preq;
835 
836 	IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, vap->iv_bss,
837 	    "%s", "send broadcast PREQ");
838 
839 	preq.preq_flags = 0;
840 	if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE)
841 		preq.preq_flags |= IEEE80211_MESHPREQ_FLAGS_GATE;
842 	if (hs->hs_rootmode == IEEE80211_HWMP_ROOTMODE_PROACTIVE)
843 		preq.preq_flags |= IEEE80211_MESHPREQ_FLAGS_PP;
844 	preq.preq_hopcount = 0;
845 	preq.preq_ttl = ms->ms_ttl;
846 	preq.preq_id = ++hs->hs_preqid;
847 	IEEE80211_ADDR_COPY(preq.preq_origaddr, vap->iv_myaddr);
848 	preq.preq_origseq = ++hs->hs_seq;
849 	preq.preq_lifetime = ticks_to_msecs(ieee80211_hwmp_roottimeout);
850 	preq.preq_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
851 	preq.preq_tcount = 1;
852 	IEEE80211_ADDR_COPY(PREQ_TADDR(0), broadcastaddr);
853 	PREQ_TFLAGS(0) = IEEE80211_MESHPREQ_TFLAGS_TO |
854 	    IEEE80211_MESHPREQ_TFLAGS_USN;
855 	PREQ_TSEQ(0) = 0;
856 	vap->iv_stats.is_hwmp_rootreqs++;
857 	/* NB: we enforce rate check ourself */
858 	hwmp_send_preq(vap, broadcastaddr, &preq, NULL, NULL);
859 	hwmp_rootmode_setup(vap);
860 }
861 #undef	PREQ_TFLAGS
862 #undef	PREQ_TADDR
863 #undef	PREQ_TSEQ
864 
865 /*
866  * Send a Root Annoucement (RANN) to find all the nodes on the mesh. We are
867  * called when the vap is configured as a HWMP RANN root node.
868  */
869 static void
870 hwmp_rootmode_rann_cb(void *arg)
871 {
872 	struct ieee80211vap *vap = (struct ieee80211vap *)arg;
873 	struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
874 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
875 	struct ieee80211_meshrann_ie rann;
876 
877 	IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, vap->iv_bss,
878 	    "%s", "send broadcast RANN");
879 
880 	rann.rann_flags = 0;
881 	if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE)
882 		rann.rann_flags |= IEEE80211_MESHFLAGS_GATE;
883 	rann.rann_hopcount = 0;
884 	rann.rann_ttl = ms->ms_ttl;
885 	IEEE80211_ADDR_COPY(rann.rann_addr, vap->iv_myaddr);
886 	rann.rann_seq = ++hs->hs_seq;
887 	rann.rann_interval = ieee80211_hwmp_rannint;
888 	rann.rann_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
889 
890 	vap->iv_stats.is_hwmp_rootrann++;
891 	hwmp_send_rann(vap, broadcastaddr, &rann);
892 	hwmp_rootmode_setup(vap);
893 }
894 
895 /*
896  * Update forwarding information to TA if metric improves.
897  */
898 static void
899 hwmp_update_transmitter(struct ieee80211vap *vap, struct ieee80211_node *ni,
900     const char *hwmp_frame)
901 {
902 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
903 	struct ieee80211_mesh_route *rttran = NULL;	/* Transmitter */
904 	int metric = 0;
905 
906 	rttran = ieee80211_mesh_rt_find(vap, ni->ni_macaddr);
907 	if (rttran == NULL) {
908 		rttran = ieee80211_mesh_rt_add(vap, ni->ni_macaddr);
909 		if (rttran == NULL) {
910 			IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
911 			    "unable to add path to transmitter %6D of %s",
912 			    ni->ni_macaddr, ":", hwmp_frame);
913 			vap->iv_stats.is_mesh_rtaddfailed++;
914 			return;
915 		}
916 	}
917 	metric = ms->ms_pmetric->mpm_metric(ni);
918 	if (!(rttran->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) ||
919 	    rttran->rt_metric > metric)
920 	{
921 		IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
922 		    "%s path to transmiter %6D of %s, metric %d:%d",
923 		    rttran->rt_flags & IEEE80211_MESHRT_FLAGS_VALID ?
924 		    "prefer" : "update", ni->ni_macaddr, ":", hwmp_frame,
925 		    rttran->rt_metric, metric);
926 		IEEE80211_ADDR_COPY(rttran->rt_nexthop, ni->ni_macaddr);
927 		rttran->rt_metric = metric;
928 		rttran->rt_nhops  = 1;
929 		ieee80211_mesh_rt_update(rttran, ms->ms_ppath->mpp_inact);
930 		rttran->rt_flags = IEEE80211_MESHRT_FLAGS_VALID;
931 	}
932 }
933 
934 #define	PREQ_TFLAGS(n)	preq->preq_targets[n].target_flags
935 #define	PREQ_TADDR(n)	preq->preq_targets[n].target_addr
936 #define	PREQ_TSEQ(n)	preq->preq_targets[n].target_seq
937 static void
938 hwmp_recv_preq(struct ieee80211vap *vap, struct ieee80211_node *ni,
939     const struct ieee80211_frame *wh, const struct ieee80211_meshpreq_ie *preq)
940 {
941 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
942 	struct ieee80211_mesh_route *rtorig = NULL;
943 	struct ieee80211_mesh_route *rtorig_ext = NULL;
944 	struct ieee80211_mesh_route *rttarg = NULL;
945 	struct ieee80211_hwmp_route *hrorig = NULL;
946 	struct ieee80211_hwmp_route *hrtarg = NULL;
947 	struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
948 	struct ieee80211_meshprep_ie prep;
949 	ieee80211_hwmp_seq preqid;	/* last seen preqid for orig */
950 	uint32_t metric = 0;
951 
952 	/*
953 	 * Ignore PREQs from us. Could happen because someone forward it
954 	 * back to us.
955 	 */
956 	if (IEEE80211_ADDR_EQ(vap->iv_myaddr, preq->preq_origaddr))
957 		return;
958 
959 	IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
960 	    "received PREQ, orig %6D, targ(0) %6D", preq->preq_origaddr, ":",
961 	    PREQ_TADDR(0), ":");
962 
963 	/*
964 	 * Acceptance criteria: (if the PREQ is not for us or not broadcast,
965 	 * or an external mac address not proxied by us),
966 	 * AND forwarding is disabled, discard this PREQ.
967 	 */
968 	rttarg = ieee80211_mesh_rt_find(vap, PREQ_TADDR(0));
969 	if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD) &&
970 	    (!IEEE80211_ADDR_EQ(vap->iv_myaddr, PREQ_TADDR(0)) ||
971 	    !IEEE80211_IS_MULTICAST(PREQ_TADDR(0)) ||
972 	    (rttarg != NULL &&
973 	    rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY &&
974 	    IEEE80211_ADDR_EQ(vap->iv_myaddr, rttarg->rt_mesh_gate)))) {
975 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_HWMP,
976 		    preq->preq_origaddr, NULL, "%s", "not accepting PREQ");
977 		return;
978 	}
979 	/*
980 	 * Acceptance criteria: if unicast addressed
981 	 * AND no valid forwarding for Target of PREQ, discard this PREQ.
982 	 */
983 	if(rttarg != NULL)
984 		hrtarg = IEEE80211_MESH_ROUTE_PRIV(rttarg,
985 		    struct ieee80211_hwmp_route);
986 	/* Address mode: ucast */
987 	if(preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AM &&
988 	    rttarg == NULL &&
989 	    !IEEE80211_ADDR_EQ(vap->iv_myaddr, PREQ_TADDR(0))) {
990 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_HWMP,
991 		    preq->preq_origaddr, NULL,
992 		    "unicast addressed PREQ of unknown target %6D",
993 		    PREQ_TADDR(0), ":");
994 		return;
995 	}
996 
997 	/* PREQ ACCEPTED */
998 
999 	rtorig = ieee80211_mesh_rt_find(vap, preq->preq_origaddr);
1000 	if (rtorig == NULL) {
1001 		rtorig = ieee80211_mesh_rt_add(vap, preq->preq_origaddr);
1002 		if (rtorig == NULL) {
1003 			IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1004 			    "unable to add orig path to %6D",
1005 			    preq->preq_origaddr, ":");
1006 			vap->iv_stats.is_mesh_rtaddfailed++;
1007 			return;
1008 		}
1009 		IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1010 		    "adding originator %6D", preq->preq_origaddr, ":");
1011 	}
1012 	hrorig = IEEE80211_MESH_ROUTE_PRIV(rtorig, struct ieee80211_hwmp_route);
1013 
1014 	/* record last seen preqid */
1015 	preqid = hrorig->hr_preqid;
1016 	hrorig->hr_preqid = HWMP_SEQ_MAX(hrorig->hr_preqid, preq->preq_id);
1017 
1018 	/* Data creation and update of forwarding information
1019 	 * according to Table 11C-8 for originator mesh STA.
1020 	 */
1021 	metric = preq->preq_metric + ms->ms_pmetric->mpm_metric(ni);
1022 	if (HWMP_SEQ_GT(preq->preq_origseq, hrorig->hr_seq) ||
1023 	    (HWMP_SEQ_EQ(preq->preq_origseq, hrorig->hr_seq) &&
1024 	    metric < rtorig->rt_metric)) {
1025 		hrorig->hr_seq = preq->preq_origseq;
1026 		IEEE80211_ADDR_COPY(rtorig->rt_nexthop, wh->i_addr2);
1027 		rtorig->rt_metric = metric;
1028 		rtorig->rt_nhops  = preq->preq_hopcount + 1;
1029 		ieee80211_mesh_rt_update(rtorig, preq->preq_lifetime);
1030 		/* Path to orig is valid now.
1031 		 * NB: we know it can't be Proxy, and if it is GATE
1032 		 * it will be marked below.
1033 		 */
1034 		rtorig->rt_flags = IEEE80211_MESHRT_FLAGS_VALID;
1035 	} else if ((hrtarg != NULL &&
1036 	    !HWMP_SEQ_EQ(hrtarg->hr_seq, PREQ_TSEQ(0))) ||
1037 	    (rtorig->rt_flags & IEEE80211_MESHRT_FLAGS_VALID &&
1038 	    preqid >= preq->preq_id)) {
1039 		IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1040 		    "discard PREQ from %6D, old seqno %u <= %u,"
1041 		    " or old preqid %u < %u",
1042 		    preq->preq_origaddr, ":",
1043 		    preq->preq_origseq, hrorig->hr_seq,
1044 		    preq->preq_id, preqid);
1045 		return;
1046 	}
1047 
1048 	/* Update forwarding information to TA if metric improves. */
1049 	hwmp_update_transmitter(vap, ni, "PREQ");
1050 
1051 	/*
1052 	 * Check if the PREQ is addressed to us.
1053 	 * or a Proxy currently gated by us.
1054 	 */
1055 	if (IEEE80211_ADDR_EQ(vap->iv_myaddr, PREQ_TADDR(0)) ||
1056 	    (ms->ms_flags & IEEE80211_MESHFLAGS_GATE &&
1057 	    rttarg != NULL &&
1058 	    IEEE80211_ADDR_EQ(vap->iv_myaddr, rttarg->rt_mesh_gate) &&
1059 	    rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY &&
1060 	    rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_VALID)) {
1061 		/*
1062 		 * When we are the target we shall update our own HWMP seq
1063 		 * number with max of (current and preq->seq) + 1
1064 		 */
1065 		hs->hs_seq = HWMP_SEQ_MAX(hs->hs_seq, PREQ_TSEQ(0)) + 1;
1066 
1067 		prep.prep_flags = 0;
1068 		prep.prep_hopcount = 0;
1069 		prep.prep_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
1070 		IEEE80211_ADDR_COPY(prep.prep_targetaddr, vap->iv_myaddr);
1071 		if (rttarg != NULL && /* if NULL it means we are the target */
1072 		    rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1073 			IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1074 			    "reply for proxy %6D", rttarg->rt_dest, ":");
1075 			prep.prep_flags |= IEEE80211_MESHPREP_FLAGS_AE;
1076 			IEEE80211_ADDR_COPY(prep.prep_target_ext_addr,
1077 			    rttarg->rt_dest);
1078 			/* update proxy seqno to HWMP seqno */
1079 			rttarg->rt_ext_seq = hs->hs_seq;
1080 			prep.prep_hopcount = rttarg->rt_nhops;
1081 			prep.prep_metric = rttarg->rt_metric;
1082 			IEEE80211_ADDR_COPY(prep.prep_targetaddr, rttarg->rt_mesh_gate);
1083 		}
1084 		/*
1085 		 * Build and send a PREP frame.
1086 		 */
1087 		prep.prep_ttl = ms->ms_ttl;
1088 		prep.prep_targetseq = hs->hs_seq;
1089 		prep.prep_lifetime = preq->preq_lifetime;
1090 		IEEE80211_ADDR_COPY(prep.prep_origaddr, preq->preq_origaddr);
1091 		prep.prep_origseq = preq->preq_origseq;
1092 
1093 		IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1094 		    "reply to %6D", preq->preq_origaddr, ":");
1095 		hwmp_send_prep(vap, wh->i_addr2, &prep);
1096 		return;
1097 	}
1098 	/* we may update our proxy information for the orig external */
1099 	else if (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AE) {
1100 		rtorig_ext =
1101 		    ieee80211_mesh_rt_find(vap, preq->preq_orig_ext_addr);
1102 		if (rtorig_ext == NULL) {
1103 			rtorig_ext = ieee80211_mesh_rt_add(vap,
1104 			    preq->preq_orig_ext_addr);
1105 			if (rtorig_ext == NULL) {
1106 				IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1107 				    "unable to add orig ext proxy to %6D",
1108 				    preq->preq_orig_ext_addr, ":");
1109 				vap->iv_stats.is_mesh_rtaddfailed++;
1110 				return;
1111 			}
1112 			IEEE80211_ADDR_COPY(rtorig_ext->rt_mesh_gate,
1113 			    preq->preq_origaddr);
1114 		}
1115 		rtorig_ext->rt_ext_seq = preq->preq_origseq;
1116 		ieee80211_mesh_rt_update(rtorig_ext, preq->preq_lifetime);
1117 	}
1118 	/*
1119 	 * Proactive PREQ: reply with a proactive PREP to the
1120 	 * root STA if requested.
1121 	 */
1122 	if (IEEE80211_ADDR_EQ(PREQ_TADDR(0), broadcastaddr) &&
1123 	    (PREQ_TFLAGS(0) & IEEE80211_MESHPREQ_TFLAGS_TO)) {
1124 		IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1125 		    "root mesh station @ %6D", preq->preq_origaddr, ":");
1126 
1127 		/* Check if root is a mesh gate, mark it */
1128 		if (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_GATE) {
1129 			struct ieee80211_mesh_gate_route *gr;
1130 
1131 			rtorig->rt_flags |= IEEE80211_MESHRT_FLAGS_GATE;
1132 			gr = ieee80211_mesh_mark_gate(vap, preq->preq_origaddr,
1133 			    rtorig);
1134 			gr->gr_lastseq = 0; /* NOT GANN */
1135 		}
1136 
1137 		/*
1138 		 * Reply with a PREP if we don't have a path to the root
1139 		 * or if the root sent us a proactive PREQ.
1140 		 */
1141 		if ((rtorig->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 ||
1142 		    (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_PP)) {
1143 			prep.prep_flags = 0;
1144 			prep.prep_hopcount = 0;
1145 			prep.prep_ttl = ms->ms_ttl;
1146 			IEEE80211_ADDR_COPY(prep.prep_origaddr,
1147 			    preq->preq_origaddr);
1148 			prep.prep_origseq = preq->preq_origseq;
1149 			prep.prep_lifetime = preq->preq_lifetime;
1150 			prep.prep_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
1151 			IEEE80211_ADDR_COPY(prep.prep_targetaddr,
1152 			    vap->iv_myaddr);
1153 			prep.prep_targetseq = ++hs->hs_seq;
1154 			hwmp_send_prep(vap, rtorig->rt_nexthop, &prep);
1155 		}
1156 	}
1157 
1158 	/*
1159 	 * Forwarding and Intermediate reply for PREQs with 1 target.
1160 	 */
1161 	if ((preq->preq_tcount == 1) && (preq->preq_ttl > 1) &&
1162 	    (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
1163 		struct ieee80211_meshpreq_ie ppreq; /* propagated PREQ */
1164 
1165 		memcpy(&ppreq, preq, sizeof(ppreq));
1166 
1167 		/*
1168 		 * We have a valid route to this node.
1169 		 * NB: if target is proxy dont reply.
1170 		 */
1171 		if (rttarg != NULL &&
1172 		    rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_VALID &&
1173 		    !(rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY)) {
1174 			/*
1175 			 * Check if we can send an intermediate Path Reply,
1176 			 * i.e., Target Only bit is not set and target is not
1177 			 * the MAC broadcast address.
1178 			 */
1179 			if (!(PREQ_TFLAGS(0) & IEEE80211_MESHPREQ_TFLAGS_TO) &&
1180 			    !IEEE80211_ADDR_EQ(PREQ_TADDR(0), broadcastaddr)) {
1181 				struct ieee80211_meshprep_ie prep;
1182 
1183 				IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1184 				    "intermediate reply for PREQ from %6D",
1185 				    preq->preq_origaddr, ":");
1186 				prep.prep_flags = 0;
1187 				prep.prep_hopcount = rttarg->rt_nhops;
1188 				prep.prep_ttl = ms->ms_ttl;
1189 				IEEE80211_ADDR_COPY(&prep.prep_targetaddr,
1190 				    PREQ_TADDR(0));
1191 				prep.prep_targetseq = hrtarg->hr_seq;
1192 				prep.prep_lifetime = preq->preq_lifetime;
1193 				prep.prep_metric =rttarg->rt_metric;
1194 				IEEE80211_ADDR_COPY(&prep.prep_origaddr,
1195 				    preq->preq_origaddr);
1196 				prep.prep_origseq = hrorig->hr_seq;
1197 				hwmp_send_prep(vap, rtorig->rt_nexthop, &prep);
1198 
1199 				/*
1200 				 * Set TO and unset RF bits because we have
1201 				 * sent a PREP.
1202 				 */
1203 				ppreq.preq_targets[0].target_flags |=
1204 				    IEEE80211_MESHPREQ_TFLAGS_TO;
1205 			}
1206 		}
1207 
1208 		IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1209 		    "forward PREQ from %6D",
1210 		    preq->preq_origaddr, ":");
1211 		ppreq.preq_hopcount += 1;
1212 		ppreq.preq_ttl -= 1;
1213 		ppreq.preq_metric += ms->ms_pmetric->mpm_metric(ni);
1214 
1215 		/* don't do PREQ ratecheck when we propagate */
1216 		hwmp_send_preq(vap, broadcastaddr, &ppreq, NULL, NULL);
1217 	}
1218 }
1219 #undef	PREQ_TFLAGS
1220 #undef	PREQ_TADDR
1221 #undef	PREQ_TSEQ
1222 
1223 static int
1224 hwmp_send_preq(struct ieee80211vap *vap,
1225     const uint8_t da[IEEE80211_ADDR_LEN],
1226     struct ieee80211_meshpreq_ie *preq,
1227     struct timeval *last, struct timeval *minint)
1228 {
1229 
1230 	/*
1231 	 * Enforce PREQ interval.
1232 	 * NB: Proactive ROOT PREQs rate is handled by cb task.
1233 	 */
1234 	if (last != NULL && minint != NULL) {
1235 		if (ratecheck(last, minint) == 0)
1236 			return EALREADY; /* XXX: we should postpone */
1237 		getmicrouptime(last);
1238 	}
1239 
1240 	/*
1241 	 * mesh preq action frame format
1242 	 *     [6] da
1243 	 *     [6] sa
1244 	 *     [6] addr3 = sa
1245 	 *     [1] action
1246 	 *     [1] category
1247 	 *     [tlv] mesh path request
1248 	 */
1249 	preq->preq_ie = IEEE80211_ELEMID_MESHPREQ;
1250 	preq->preq_len = (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AE ?
1251 	    IEEE80211_MESHPREQ_BASE_SZ_AE : IEEE80211_MESHPREQ_BASE_SZ) +
1252 	    preq->preq_tcount * IEEE80211_MESHPREQ_TRGT_SZ;
1253 	return hwmp_send_action(vap, da, (uint8_t *)preq, preq->preq_len+2);
1254 }
1255 
1256 static void
1257 hwmp_recv_prep(struct ieee80211vap *vap, struct ieee80211_node *ni,
1258     const struct ieee80211_frame *wh, const struct ieee80211_meshprep_ie *prep)
1259 {
1260 #define	IS_PROXY(rt)	(rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY)
1261 #define	PROXIED_BY_US(rt)		\
1262     (IEEE80211_ADDR_EQ(vap->iv_myaddr, rt->rt_mesh_gate))
1263 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1264 	struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
1265 	struct ieee80211_mesh_route *rt = NULL;
1266 	struct ieee80211_mesh_route *rtorig = NULL;
1267 	struct ieee80211_mesh_route *rtext = NULL;
1268 	struct ieee80211_hwmp_route *hr;
1269 	struct ieee80211com *ic = vap->iv_ic;
1270 	struct mbuf *m, *next;
1271 	uint32_t metric = 0;
1272 	const uint8_t *addr;
1273 
1274 	IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1275 	    "received PREP, orig %6D, targ %6D", prep->prep_origaddr, ":",
1276 	    prep->prep_targetaddr, ":");
1277 
1278 	/*
1279 	 * Acceptance criteria: (If the corresponding PREP was not generated
1280 	 * by us OR not generated by an external mac that is not proxied by us)
1281 	 * AND forwarding is disabled, discard this PREP.
1282 	 */
1283 	rtorig = ieee80211_mesh_rt_find(vap, prep->prep_origaddr);
1284 	if ((!IEEE80211_ADDR_EQ(vap->iv_myaddr, prep->prep_origaddr) ||
1285 	    (rtorig != NULL && IS_PROXY(rtorig) && !PROXIED_BY_US(rtorig))) &&
1286 	    !(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)){
1287 		IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1288 		    "discard PREP, orig(%6D) not proxied or generated by us",
1289 		    prep->prep_origaddr, ":");
1290 		return;
1291 	}
1292 
1293 	/* PREP ACCEPTED */
1294 
1295 	/*
1296 	 * If accepted shall create or update the active forwarding information
1297 	 * it maintains for the target mesh STA of the PREP (according to the
1298 	 * rules defined in 13.10.8.4). If the conditions for creating or
1299 	 * updating the forwarding information have not been met in those
1300 	 * rules, no further steps are applied to the PREP.
1301 	 */
1302 	rt = ieee80211_mesh_rt_find(vap, prep->prep_targetaddr);
1303 	if (rt == NULL) {
1304 		rt = ieee80211_mesh_rt_add(vap, prep->prep_targetaddr);
1305 		if (rt == NULL) {
1306 			IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1307 			    "unable to add PREP path to %6D",
1308 			    prep->prep_targetaddr, ":");
1309 			vap->iv_stats.is_mesh_rtaddfailed++;
1310 			return;
1311 		}
1312 		IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1313 		    "adding target %6D", prep->prep_targetaddr, ":");
1314 	}
1315 	hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route);
1316 	/* update path metric */
1317 	metric = prep->prep_metric + ms->ms_pmetric->mpm_metric(ni);
1318 	if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID)) {
1319 		if (HWMP_SEQ_LT(prep->prep_targetseq, hr->hr_seq)) {
1320 			IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1321 			    "discard PREP from %6D, old seq no %u < %u",
1322 			    prep->prep_targetaddr, ":",
1323 			    prep->prep_targetseq, hr->hr_seq);
1324 			return;
1325 		} else if (HWMP_SEQ_LEQ(prep->prep_targetseq, hr->hr_seq) &&
1326 		    metric > rt->rt_metric) {
1327 			IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1328 			    "discard PREP from %6D, new metric %u > %u",
1329 			    prep->prep_targetaddr, ":",
1330 			    metric, rt->rt_metric);
1331 			return;
1332 		}
1333 	}
1334 
1335 	IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1336 	    "%s path to %6D, hopcount %d:%d metric %d:%d",
1337 	    rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID ?
1338 	    "prefer" : "update",
1339 	    prep->prep_targetaddr, ":",
1340 	    rt->rt_nhops, prep->prep_hopcount + 1,
1341 	    rt->rt_metric, metric);
1342 
1343 	hr->hr_seq = prep->prep_targetseq;
1344 	hr->hr_preqretries = 0;
1345 	IEEE80211_ADDR_COPY(rt->rt_nexthop, ni->ni_macaddr);
1346 	rt->rt_metric = metric;
1347 	rt->rt_nhops = prep->prep_hopcount + 1;
1348 	ieee80211_mesh_rt_update(rt, prep->prep_lifetime);
1349 	if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_DISCOVER) {
1350 		/* discovery complete */
1351 		rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_DISCOVER;
1352 	}
1353 	rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID; /* mark valid */
1354 
1355 	/* Update forwarding information to TA if metric improves */
1356 	hwmp_update_transmitter(vap, ni, "PREP");
1357 
1358 	/*
1359 	 * If it's NOT for us, propagate the PREP
1360 	 */
1361 	if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, prep->prep_origaddr) &&
1362 	    prep->prep_ttl > 1 &&
1363 	    prep->prep_hopcount < hs->hs_maxhops) {
1364 		struct ieee80211_meshprep_ie pprep; /* propagated PREP */
1365 		/*
1366 		 * NB: We should already have setup the path to orig
1367 		 * mesh STA when we propagated PREQ to target mesh STA,
1368 		 * no PREP is generated without a corresponding PREQ.
1369 		 * XXX: for now just ignore.
1370 		 */
1371 		if (rtorig == NULL) {
1372 			IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1373 			    "received PREP for an unknown orig(%6D)",
1374 			    prep->prep_origaddr, ":");
1375 			return;
1376 		}
1377 
1378 		IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1379 		    "propagate PREP from %6D",
1380 		    prep->prep_targetaddr, ":");
1381 
1382 		memcpy(&pprep, prep, sizeof(pprep));
1383 		pprep.prep_hopcount += 1;
1384 		pprep.prep_ttl -= 1;
1385 		pprep.prep_metric += ms->ms_pmetric->mpm_metric(ni);
1386 		hwmp_send_prep(vap, rtorig->rt_nexthop, &pprep);
1387 
1388 		/* precursor list for the Target Mesh STA Address is updated */
1389 	}
1390 
1391 	/*
1392 	 * Check if we received a PREP w/ AE and store target external address.
1393 	 * We may store target external address if recevied PREP w/ AE
1394 	 * and we are not final destination
1395 	 */
1396 	if (prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE) {
1397 		rtext = ieee80211_mesh_rt_find(vap,
1398 			prep->prep_target_ext_addr);
1399 		if (rtext == NULL) {
1400 			rtext = ieee80211_mesh_rt_add(vap,
1401 				prep->prep_target_ext_addr);
1402 			if (rtext == NULL) {
1403 				IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1404 				    "unable to add PREP path to proxy %6D",
1405 				    prep->prep_targetaddr, ":");
1406 				vap->iv_stats.is_mesh_rtaddfailed++;
1407 				return;
1408 			}
1409 		}
1410 		IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1411 		    "%s path to %6D, hopcount %d:%d metric %d:%d",
1412 		    rtext->rt_flags & IEEE80211_MESHRT_FLAGS_VALID ?
1413 		    "prefer" : "update",
1414 		    prep->prep_target_ext_addr, ":",
1415 		    rtext->rt_nhops, prep->prep_hopcount + 1,
1416 		    rtext->rt_metric, metric);
1417 
1418 		rtext->rt_flags = IEEE80211_MESHRT_FLAGS_PROXY |
1419 			IEEE80211_MESHRT_FLAGS_VALID;
1420 		IEEE80211_ADDR_COPY(rtext->rt_dest,
1421 		    prep->prep_target_ext_addr);
1422 		IEEE80211_ADDR_COPY(rtext->rt_mesh_gate,
1423 		    prep->prep_targetaddr);
1424 		IEEE80211_ADDR_COPY(rtext->rt_nexthop, wh->i_addr2);
1425 		rtext->rt_metric = metric;
1426 		rtext->rt_lifetime = prep->prep_lifetime;
1427 		rtext->rt_nhops = prep->prep_hopcount + 1;
1428 		rtext->rt_ext_seq = prep->prep_origseq; /* new proxy seq */
1429 		/*
1430 		 * XXX: proxy entries have no HWMP priv data,
1431 		 * nullify them to be sure?
1432 		 */
1433 	}
1434 	/*
1435 	 * Check for frames queued awaiting path discovery.
1436 	 * XXX probably can tell exactly and avoid remove call
1437 	 * NB: hash may have false matches, if so they will get
1438 	 *     stuck back on the stageq because there won't be
1439 	 *     a path.
1440 	 */
1441 	addr = prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE ?
1442 	    prep->prep_target_ext_addr : prep->prep_targetaddr;
1443 	m = ieee80211_ageq_remove(&ic->ic_stageq,
1444 	    (struct ieee80211_node *)(uintptr_t)
1445 	    ieee80211_mac_hash(ic, addr)); /* either dest or ext_dest */
1446 
1447 	/*
1448 	 * All frames in the stageq here should be non-M_ENCAP; or things
1449 	 * will get very unhappy.
1450 	 */
1451 	for (; m != NULL; m = next) {
1452 		next = m->m_nextpkt;
1453 		m->m_nextpkt = NULL;
1454 		IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1455 		    "flush queued frame %p len %d", m, m->m_pkthdr.len);
1456 		/*
1457 		 * If the mbuf has M_ENCAP set, ensure we free it.
1458 		 * Note that after if_transmit() is called, m is invalid.
1459 		 */
1460 		(void) ieee80211_vap_xmitpkt(vap, m);
1461 	}
1462 #undef	IS_PROXY
1463 #undef	PROXIED_BY_US
1464 }
1465 
1466 static int
1467 hwmp_send_prep(struct ieee80211vap *vap,
1468     const uint8_t da[IEEE80211_ADDR_LEN],
1469     struct ieee80211_meshprep_ie *prep)
1470 {
1471 	/* NB: there's no PREP minimum interval. */
1472 
1473 	/*
1474 	 * mesh prep action frame format
1475 	 *     [6] da
1476 	 *     [6] sa
1477 	 *     [6] addr3 = sa
1478 	 *     [1] action
1479 	 *     [1] category
1480 	 *     [tlv] mesh path reply
1481 	 */
1482 	prep->prep_ie = IEEE80211_ELEMID_MESHPREP;
1483 	prep->prep_len = prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE ?
1484 	    IEEE80211_MESHPREP_BASE_SZ_AE : IEEE80211_MESHPREP_BASE_SZ;
1485 	return hwmp_send_action(vap, da, (uint8_t *)prep, prep->prep_len + 2);
1486 }
1487 
1488 #define	PERR_DFLAGS(n)	perr.perr_dests[n].dest_flags
1489 #define	PERR_DADDR(n)	perr.perr_dests[n].dest_addr
1490 #define	PERR_DSEQ(n)	perr.perr_dests[n].dest_seq
1491 #define	PERR_DRCODE(n)	perr.perr_dests[n].dest_rcode
1492 static void
1493 hwmp_peerdown(struct ieee80211_node *ni)
1494 {
1495 	struct ieee80211vap *vap = ni->ni_vap;
1496 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1497 	struct ieee80211_meshperr_ie perr;
1498 	struct ieee80211_mesh_route *rt;
1499 	struct ieee80211_hwmp_route *hr;
1500 
1501 	rt = ieee80211_mesh_rt_find(vap, ni->ni_macaddr);
1502 	if (rt == NULL)
1503 		return;
1504 	hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route);
1505 	IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1506 	    "%s", "delete route entry");
1507 	perr.perr_ttl = ms->ms_ttl;
1508 	perr.perr_ndests = 1;
1509 	PERR_DFLAGS(0) = 0;
1510 	if (hr->hr_seq == 0)
1511 		PERR_DFLAGS(0) |= IEEE80211_MESHPERR_DFLAGS_USN;
1512 	PERR_DFLAGS(0) |= IEEE80211_MESHPERR_DFLAGS_RC;
1513 	IEEE80211_ADDR_COPY(PERR_DADDR(0), rt->rt_dest);
1514 	PERR_DSEQ(0) = ++hr->hr_seq;
1515 	PERR_DRCODE(0) = IEEE80211_REASON_MESH_PERR_DEST_UNREACH;
1516 	/* NB: flush everything passing through peer */
1517 	ieee80211_mesh_rt_flush_peer(vap, ni->ni_macaddr);
1518 	hwmp_send_perr(vap, broadcastaddr, &perr);
1519 }
1520 #undef	PERR_DFLAGS
1521 #undef	PERR_DADDR
1522 #undef	PERR_DSEQ
1523 #undef	PERR_DRCODE
1524 
1525 #define	PERR_DFLAGS(n)		perr->perr_dests[n].dest_flags
1526 #define	PERR_DADDR(n)		perr->perr_dests[n].dest_addr
1527 #define	PERR_DSEQ(n)		perr->perr_dests[n].dest_seq
1528 #define	PERR_DEXTADDR(n)	perr->perr_dests[n].dest_ext_addr
1529 #define	PERR_DRCODE(n)		perr->perr_dests[n].dest_rcode
1530 static void
1531 hwmp_recv_perr(struct ieee80211vap *vap, struct ieee80211_node *ni,
1532     const struct ieee80211_frame *wh, const struct ieee80211_meshperr_ie *perr)
1533 {
1534 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1535 	struct ieee80211_mesh_route *rt = NULL;
1536 	struct ieee80211_mesh_route *rt_ext = NULL;
1537 	struct ieee80211_hwmp_route *hr;
1538 	struct ieee80211_meshperr_ie *pperr = NULL;
1539 	int i, j = 0, forward = 0;
1540 
1541 	IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1542 	    "received PERR from %6D", wh->i_addr2, ":");
1543 
1544 	/*
1545 	 * if forwarding is true, prepare pperr
1546 	 */
1547 	if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) {
1548 		forward = 1;
1549 		pperr = IEEE80211_MALLOC(sizeof(*perr) + 31*sizeof(*perr->perr_dests),
1550 		    M_80211_MESH_PERR, IEEE80211_M_NOWAIT); /* XXX: magic number, 32 err dests */
1551 	}
1552 
1553 	/*
1554 	 * Acceptance criteria: check if we have forwarding information
1555 	 * stored about destination, and that nexthop == TA of this PERR.
1556 	 * NB: we also build a new PERR to propagate in case we should forward.
1557 	 */
1558 	for (i = 0; i < perr->perr_ndests; i++) {
1559 		rt = ieee80211_mesh_rt_find(vap, PERR_DADDR(i));
1560 		if (rt == NULL)
1561 			continue;
1562 		if (!IEEE80211_ADDR_EQ(rt->rt_nexthop, wh->i_addr2))
1563 			continue;
1564 
1565 		/* found and accepted a PERR ndest element, process it... */
1566 		if (forward)
1567 			memcpy(&pperr->perr_dests[j], &perr->perr_dests[i],
1568 			    sizeof(*perr->perr_dests));
1569 		hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route);
1570 		switch(PERR_DFLAGS(i)) {
1571 		case (IEEE80211_REASON_MESH_PERR_NO_FI):
1572 			if (PERR_DSEQ(i) == 0) {
1573 				hr->hr_seq++;
1574 				if (forward) {
1575 					pperr->perr_dests[j].dest_seq =
1576 					    hr->hr_seq;
1577 				}
1578 			} else {
1579 				hr->hr_seq = PERR_DSEQ(i);
1580 			}
1581 			rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID;
1582 			j++;
1583 			break;
1584 		case (IEEE80211_REASON_MESH_PERR_DEST_UNREACH):
1585 			if(HWMP_SEQ_GT(PERR_DSEQ(i), hr->hr_seq)) {
1586 				hr->hr_seq = PERR_DSEQ(i);
1587 				rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID;
1588 				j++;
1589 			}
1590 			break;
1591 		case (IEEE80211_REASON_MESH_PERR_NO_PROXY):
1592 			rt_ext = ieee80211_mesh_rt_find(vap, PERR_DEXTADDR(i));
1593 			if (rt_ext != NULL) {
1594 				rt_ext->rt_flags &=
1595 				    ~IEEE80211_MESHRT_FLAGS_VALID;
1596 				j++;
1597 			}
1598 			break;
1599 		default:
1600 			IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL,
1601 			    "PERR, unknown reason code %u\n", PERR_DFLAGS(i));
1602 			goto done; /* XXX: stats?? */
1603 		}
1604 		ieee80211_mesh_rt_flush_peer(vap, PERR_DADDR(i));
1605 		KASSERT(j < 32, ("PERR, error ndest >= 32 (%u)", j));
1606 	}
1607 	if (j == 0) {
1608 		IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL, "%s",
1609 		    "PERR not accepted");
1610 		goto done; /* XXX: stats?? */
1611 	}
1612 
1613 	/*
1614 	 * Propagate the PERR if we previously found it on our routing table.
1615 	 */
1616 	if (forward && perr->perr_ttl > 1) {
1617 		IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1618 		    "propagate PERR from %6D", wh->i_addr2, ":");
1619 		pperr->perr_ndests = j;
1620 		pperr->perr_ttl--;
1621 		hwmp_send_perr(vap, broadcastaddr, pperr);
1622 	}
1623 done:
1624 	if (pperr != NULL)
1625 		IEEE80211_FREE(pperr, M_80211_MESH_PERR);
1626 }
1627 #undef	PERR_DFLAGS
1628 #undef	PERR_DADDR
1629 #undef	PERR_DSEQ
1630 #undef	PERR_DEXTADDR
1631 #undef	PERR_DRCODE
1632 
1633 static int
1634 hwmp_send_perr(struct ieee80211vap *vap,
1635     const uint8_t da[IEEE80211_ADDR_LEN],
1636     struct ieee80211_meshperr_ie *perr)
1637 {
1638 	struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
1639 	int i;
1640 	uint8_t length = 0;
1641 
1642 	/*
1643 	 * Enforce PERR interval.
1644 	 */
1645 	if (ratecheck(&hs->hs_lastperr, &ieee80211_hwmp_perrminint) == 0)
1646 		return EALREADY;
1647 	getmicrouptime(&hs->hs_lastperr);
1648 
1649 	/*
1650 	 * mesh perr action frame format
1651 	 *     [6] da
1652 	 *     [6] sa
1653 	 *     [6] addr3 = sa
1654 	 *     [1] action
1655 	 *     [1] category
1656 	 *     [tlv] mesh path error
1657 	 */
1658 	perr->perr_ie = IEEE80211_ELEMID_MESHPERR;
1659 	length = IEEE80211_MESHPERR_BASE_SZ;
1660 	for (i = 0; i<perr->perr_ndests; i++) {
1661 		if (perr->perr_dests[i].dest_flags &
1662 		    IEEE80211_MESHPERR_FLAGS_AE) {
1663 			length += IEEE80211_MESHPERR_DEST_SZ_AE;
1664 			continue ;
1665 		}
1666 		length += IEEE80211_MESHPERR_DEST_SZ;
1667 	}
1668 	perr->perr_len =length;
1669 	return hwmp_send_action(vap, da, (uint8_t *)perr, perr->perr_len+2);
1670 }
1671 
1672 /*
1673  * Called from the rest of the net80211 code (mesh code for example).
1674  * NB: IEEE80211_REASON_MESH_PERR_DEST_UNREACH can be trigger by the fact that
1675  * a mesh STA is unable to forward an MSDU/MMPDU to a next-hop mesh STA.
1676  */
1677 #define	PERR_DFLAGS(n)		perr.perr_dests[n].dest_flags
1678 #define	PERR_DADDR(n)		perr.perr_dests[n].dest_addr
1679 #define	PERR_DSEQ(n)		perr.perr_dests[n].dest_seq
1680 #define	PERR_DEXTADDR(n)	perr.perr_dests[n].dest_ext_addr
1681 #define	PERR_DRCODE(n)		perr.perr_dests[n].dest_rcode
1682 static void
1683 hwmp_senderror(struct ieee80211vap *vap,
1684     const uint8_t addr[IEEE80211_ADDR_LEN],
1685     struct ieee80211_mesh_route *rt, int rcode)
1686 {
1687 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1688 	struct ieee80211_hwmp_route *hr = NULL;
1689 	struct ieee80211_meshperr_ie perr;
1690 
1691 	if (rt != NULL)
1692 		hr = IEEE80211_MESH_ROUTE_PRIV(rt,
1693 		    struct ieee80211_hwmp_route);
1694 
1695 	perr.perr_ndests = 1;
1696 	perr.perr_ttl = ms->ms_ttl;
1697 	PERR_DFLAGS(0) = 0;
1698 	PERR_DRCODE(0) = rcode;
1699 
1700 	switch (rcode) {
1701 	case IEEE80211_REASON_MESH_PERR_NO_FI:
1702 		IEEE80211_ADDR_COPY(PERR_DADDR(0), addr);
1703 		PERR_DSEQ(0) = 0; /* reserved */
1704 		break;
1705 	case IEEE80211_REASON_MESH_PERR_NO_PROXY:
1706 		KASSERT(rt != NULL, ("no proxy info for sending PERR"));
1707 		KASSERT(rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY,
1708 		    ("route is not marked proxy"));
1709 		PERR_DFLAGS(0) |= IEEE80211_MESHPERR_FLAGS_AE;
1710 		IEEE80211_ADDR_COPY(PERR_DADDR(0), vap->iv_myaddr);
1711 		PERR_DSEQ(0) = rt->rt_ext_seq;
1712 		IEEE80211_ADDR_COPY(PERR_DEXTADDR(0), addr);
1713 		break;
1714 	case IEEE80211_REASON_MESH_PERR_DEST_UNREACH:
1715 		KASSERT(rt != NULL, ("no route info for sending PERR"));
1716 		IEEE80211_ADDR_COPY(PERR_DADDR(0), addr);
1717 		PERR_DSEQ(0) = hr->hr_seq;
1718 		break;
1719 	default:
1720 		KASSERT(0, ("unknown reason code for HWMP PERR (%u)", rcode));
1721 	}
1722 	hwmp_send_perr(vap, broadcastaddr, &perr);
1723 }
1724 #undef	PERR_DFLAGS
1725 #undef	PEER_DADDR
1726 #undef	PERR_DSEQ
1727 #undef	PERR_DEXTADDR
1728 #undef	PERR_DRCODE
1729 
1730 static void
1731 hwmp_recv_rann(struct ieee80211vap *vap, struct ieee80211_node *ni,
1732     const struct ieee80211_frame *wh, const struct ieee80211_meshrann_ie *rann)
1733 {
1734 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1735 	struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
1736 	struct ieee80211_mesh_route *rt = NULL;
1737 	struct ieee80211_hwmp_route *hr;
1738 	struct ieee80211_meshpreq_ie preq;
1739 	struct ieee80211_meshrann_ie prann;
1740 	uint32_t metric = 0;
1741 
1742 	if (IEEE80211_ADDR_EQ(rann->rann_addr, vap->iv_myaddr))
1743 		return;
1744 
1745 	rt = ieee80211_mesh_rt_find(vap, rann->rann_addr);
1746 	if (rt != NULL && rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) {
1747 		hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route);
1748 
1749 		/* Acceptance criteria: if RANN.seq < stored seq, discard RANN */
1750 		if (HWMP_SEQ_LT(rann->rann_seq, hr->hr_seq)) {
1751 			IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL,
1752 			"RANN seq %u < %u", rann->rann_seq, hr->hr_seq);
1753 			return;
1754 		}
1755 
1756 		/* Acceptance criteria: if RANN.seq == stored seq AND
1757 		* RANN.metric > stored metric, discard RANN */
1758 		if (HWMP_SEQ_EQ(rann->rann_seq, hr->hr_seq) &&
1759 		rann->rann_metric > rt->rt_metric) {
1760 			IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL,
1761 			"RANN metric %u > %u", rann->rann_metric, rt->rt_metric);
1762 			return;
1763 		}
1764 	}
1765 
1766 	/* RANN ACCEPTED */
1767 
1768 	ieee80211_hwmp_rannint = rann->rann_interval; /* XXX: mtx lock? */
1769 	metric = rann->rann_metric + ms->ms_pmetric->mpm_metric(ni);
1770 
1771 	if (rt == NULL) {
1772 		rt = ieee80211_mesh_rt_add(vap, rann->rann_addr);
1773 		if (rt == NULL) {
1774 			IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL,
1775 			    "unable to add mac for RANN root %6D",
1776 			    rann->rann_addr, ":");
1777 			    vap->iv_stats.is_mesh_rtaddfailed++;
1778 			return;
1779 		}
1780 	}
1781 	hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route);
1782 	/* Check if root is a mesh gate, mark it */
1783 	if (rann->rann_flags & IEEE80211_MESHRANN_FLAGS_GATE) {
1784 		struct ieee80211_mesh_gate_route *gr;
1785 
1786 		rt->rt_flags |= IEEE80211_MESHRT_FLAGS_GATE;
1787 		gr = ieee80211_mesh_mark_gate(vap, rann->rann_addr,
1788 			rt);
1789 		gr->gr_lastseq = 0; /* NOT GANN */
1790 	}
1791 	/* discovery timeout */
1792 	ieee80211_mesh_rt_update(rt,
1793 	    ticks_to_msecs(ieee80211_hwmp_roottimeout));
1794 
1795 	preq.preq_flags = IEEE80211_MESHPREQ_FLAGS_AM;
1796 	preq.preq_hopcount = 0;
1797 	preq.preq_ttl = ms->ms_ttl;
1798 	preq.preq_id = 0; /* reserved */
1799 	IEEE80211_ADDR_COPY(preq.preq_origaddr, vap->iv_myaddr);
1800 	preq.preq_origseq = ++hs->hs_seq;
1801 	preq.preq_lifetime = ieee80211_hwmp_roottimeout;
1802 	preq.preq_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
1803 	preq.preq_tcount = 1;
1804 	preq.preq_targets[0].target_flags = IEEE80211_MESHPREQ_TFLAGS_TO;
1805 	/* NB: IEEE80211_MESHPREQ_TFLAGS_USN = 0 implicitly implied */
1806 	IEEE80211_ADDR_COPY(preq.preq_targets[0].target_addr, rann->rann_addr);
1807 	preq.preq_targets[0].target_seq = rann->rann_seq;
1808 	/* XXX: if rootconfint have not passed, we built this preq in vain */
1809 	hwmp_send_preq(vap, wh->i_addr2, &preq, &hr->hr_lastrootconf,
1810 	    &ieee80211_hwmp_rootconfint);
1811 
1812 	/* propagate a RANN */
1813 	if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID &&
1814 	    rann->rann_ttl > 1 &&
1815 	    ms->ms_flags & IEEE80211_MESHFLAGS_FWD) {
1816 		hr->hr_seq = rann->rann_seq;
1817 		memcpy(&prann, rann, sizeof(prann));
1818 		prann.rann_hopcount += 1;
1819 		prann.rann_ttl -= 1;
1820 		prann.rann_metric += ms->ms_pmetric->mpm_metric(ni);
1821 		hwmp_send_rann(vap, broadcastaddr, &prann);
1822 	}
1823 }
1824 
1825 static int
1826 hwmp_send_rann(struct ieee80211vap *vap,
1827     const uint8_t da[IEEE80211_ADDR_LEN],
1828     struct ieee80211_meshrann_ie *rann)
1829 {
1830 	/*
1831 	 * mesh rann action frame format
1832 	 *     [6] da
1833 	 *     [6] sa
1834 	 *     [6] addr3 = sa
1835 	 *     [1] action
1836 	 *     [1] category
1837 	 *     [tlv] root annoucement
1838 	 */
1839 	rann->rann_ie = IEEE80211_ELEMID_MESHRANN;
1840 	rann->rann_len = IEEE80211_MESHRANN_BASE_SZ;
1841 	return hwmp_send_action(vap, da, (uint8_t *)rann, rann->rann_len + 2);
1842 }
1843 
1844 #define	PREQ_TFLAGS(n)	preq.preq_targets[n].target_flags
1845 #define	PREQ_TADDR(n)	preq.preq_targets[n].target_addr
1846 #define	PREQ_TSEQ(n)	preq.preq_targets[n].target_seq
1847 static void
1848 hwmp_rediscover_cb(void *arg)
1849 {
1850 	struct ieee80211_mesh_route *rt = arg;
1851 	struct ieee80211vap *vap = rt->rt_vap;
1852 	struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
1853 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1854 	struct ieee80211_hwmp_route *hr;
1855 	struct ieee80211_meshpreq_ie preq; /* Optimize: storing first preq? */
1856 
1857 	if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID))
1858 		return ; /* nothing to do */
1859 
1860 	hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route);
1861 	if (hr->hr_preqretries >=
1862 		ieee80211_hwmp_maxpreq_retries) {
1863 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY,
1864 			rt->rt_dest, "%s",
1865 			"max number of discovery, send queued frames to GATE");
1866 		ieee80211_mesh_forward_to_gates(vap, rt);
1867 		vap->iv_stats.is_mesh_fwd_nopath++;
1868 		return ; /* XXX: flush queue? */
1869 	}
1870 
1871 	hr->hr_preqretries++;
1872 
1873 
1874 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP, rt->rt_dest,
1875 	    "start path rediscovery , target seq %u", hr->hr_seq);
1876 	/*
1877 	 * Try to discover the path for this node.
1878 	 * Group addressed PREQ Case A
1879 	 */
1880 	preq.preq_flags = 0;
1881 	preq.preq_hopcount = 0;
1882 	preq.preq_ttl = ms->ms_ttl;
1883 	preq.preq_id = ++hs->hs_preqid;
1884 	IEEE80211_ADDR_COPY(preq.preq_origaddr, vap->iv_myaddr);
1885 	preq.preq_origseq = hr->hr_origseq;
1886 	preq.preq_lifetime = ticks_to_msecs(ieee80211_hwmp_pathtimeout);
1887 	preq.preq_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
1888 	preq.preq_tcount = 1;
1889 	IEEE80211_ADDR_COPY(PREQ_TADDR(0), rt->rt_dest);
1890 	PREQ_TFLAGS(0) = 0;
1891 	if (ieee80211_hwmp_targetonly)
1892 		PREQ_TFLAGS(0) |= IEEE80211_MESHPREQ_TFLAGS_TO;
1893 	PREQ_TFLAGS(0) |= IEEE80211_MESHPREQ_TFLAGS_USN;
1894 	PREQ_TSEQ(0) = 0; /* RESERVED when USN flag is set */
1895 	/* XXX check return value */
1896 	hwmp_send_preq(vap, broadcastaddr, &preq, &hr->hr_lastpreq,
1897 	    &ieee80211_hwmp_preqminint);
1898 	callout_reset(&rt->rt_discovery,
1899 		ieee80211_hwmp_net_diameter_traversaltime * 2,
1900 		hwmp_rediscover_cb, rt);
1901 }
1902 
1903 static struct ieee80211_node *
1904 hwmp_discover(struct ieee80211vap *vap,
1905     const uint8_t dest[IEEE80211_ADDR_LEN], struct mbuf *m)
1906 {
1907 	struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
1908 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1909 	struct ieee80211_mesh_route *rt = NULL;
1910 	struct ieee80211_hwmp_route *hr;
1911 	struct ieee80211_meshpreq_ie preq;
1912 	struct ieee80211_node *ni;
1913 	int sendpreq = 0;
1914 
1915 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS,
1916 	    ("not a mesh vap, opmode %d", vap->iv_opmode));
1917 
1918 	KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
1919 	    ("%s: discovering self!", __func__));
1920 
1921 	ni = NULL;
1922 	if (!IEEE80211_IS_MULTICAST(dest)) {
1923 		rt = ieee80211_mesh_rt_find(vap, dest);
1924 		if (rt == NULL) {
1925 			rt = ieee80211_mesh_rt_add(vap, dest);
1926 			if (rt == NULL) {
1927 				IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP,
1928 				    ni, "unable to add discovery path to %6D",
1929 				    dest, ":");
1930 				vap->iv_stats.is_mesh_rtaddfailed++;
1931 				goto done;
1932 			}
1933 		}
1934 		hr = IEEE80211_MESH_ROUTE_PRIV(rt,
1935 		    struct ieee80211_hwmp_route);
1936 		if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_DISCOVER) {
1937 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP, dest,
1938 			    "%s", "already discovering queue frame until path found");
1939 			sendpreq = 1;
1940 			goto done;
1941 		}
1942 		if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
1943 			if (hr->hr_lastdiscovery != 0 &&
1944 			    (ticks - hr->hr_lastdiscovery <
1945 			    (ieee80211_hwmp_net_diameter_traversaltime * 2))) {
1946 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1947 			            dest, NULL, "%s",
1948 				    "too frequent discovery requeust");
1949 				sendpreq = 1;
1950 				goto done;
1951 			}
1952 			hr->hr_lastdiscovery = ticks;
1953 			if (hr->hr_preqretries >=
1954 			    ieee80211_hwmp_maxpreq_retries) {
1955 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1956 			            dest, NULL, "%s",
1957 				    "no valid path , max number of discovery");
1958 				vap->iv_stats.is_mesh_fwd_nopath++;
1959 				goto done;
1960 			}
1961 			rt->rt_flags = IEEE80211_MESHRT_FLAGS_DISCOVER;
1962 			hr->hr_preqretries++;
1963 			if (hr->hr_origseq == 0)
1964 				hr->hr_origseq = ++hs->hs_seq;
1965 			rt->rt_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
1966 			sendpreq = 1;
1967 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP, dest,
1968 			    "start path discovery (src %s), target seq %u",
1969 			    m == NULL ? "<none>" : ether_sprintf(
1970 			    mtod(m, struct ether_header *)->ether_shost),
1971 			    hr->hr_seq);
1972 			/*
1973 			 * Try to discover the path for this node.
1974 			 * Group addressed PREQ Case A
1975 			 */
1976 			preq.preq_flags = 0;
1977 			preq.preq_hopcount = 0;
1978 			preq.preq_ttl = ms->ms_ttl;
1979 			preq.preq_id = ++hs->hs_preqid;
1980 			IEEE80211_ADDR_COPY(preq.preq_origaddr, vap->iv_myaddr);
1981 			preq.preq_origseq = hr->hr_origseq;
1982 			preq.preq_lifetime =
1983 			    ticks_to_msecs(ieee80211_hwmp_pathtimeout);
1984 			preq.preq_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
1985 			preq.preq_tcount = 1;
1986 			IEEE80211_ADDR_COPY(PREQ_TADDR(0), dest);
1987 			PREQ_TFLAGS(0) = 0;
1988 			if (ieee80211_hwmp_targetonly)
1989 				PREQ_TFLAGS(0) |= IEEE80211_MESHPREQ_TFLAGS_TO;
1990 			PREQ_TFLAGS(0) |= IEEE80211_MESHPREQ_TFLAGS_USN;
1991 			PREQ_TSEQ(0) = 0; /* RESERVED when USN flag is set */
1992 			/* XXX check return value */
1993 			hwmp_send_preq(vap, broadcastaddr, &preq,
1994 			    &hr->hr_lastpreq, &ieee80211_hwmp_preqminint);
1995 			callout_reset(&rt->rt_discovery,
1996 			    ieee80211_hwmp_net_diameter_traversaltime * 2,
1997 			    hwmp_rediscover_cb, rt);
1998 		}
1999 		if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID)
2000 			ni = ieee80211_find_txnode(vap, rt->rt_nexthop);
2001 	} else {
2002 		ni = ieee80211_find_txnode(vap, dest);
2003 		/* NB: if null then we leak mbuf */
2004 		KASSERT(ni != NULL, ("leak mcast frame"));
2005 		return ni;
2006 	}
2007 done:
2008 	if (ni == NULL && m != NULL) {
2009 		if (sendpreq) {
2010 			struct ieee80211com *ic = vap->iv_ic;
2011 			/*
2012 			 * Queue packet for transmit when path discovery
2013 			 * completes.  If discovery never completes the
2014 			 * frame will be flushed by way of the aging timer.
2015 			 */
2016 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP, dest,
2017 			    "%s", "queue frame until path found");
2018 			m->m_pkthdr.rcvif = (void *)(uintptr_t)
2019 			    ieee80211_mac_hash(ic, dest);
2020 			/* XXX age chosen randomly */
2021 			ieee80211_ageq_append(&ic->ic_stageq, m,
2022 			    IEEE80211_INACT_WAIT);
2023 		} else {
2024 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_HWMP,
2025 			    dest, NULL, "%s", "no valid path to this node");
2026 			m_freem(m);
2027 		}
2028 	}
2029 	return ni;
2030 }
2031 #undef	PREQ_TFLAGS
2032 #undef	PREQ_TADDR
2033 #undef	PREQ_TSEQ
2034 
2035 static int
2036 hwmp_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
2037 {
2038 	struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
2039 	int error;
2040 
2041 	if (vap->iv_opmode != IEEE80211_M_MBSS)
2042 		return ENOSYS;
2043 	error = 0;
2044 	switch (ireq->i_type) {
2045 	case IEEE80211_IOC_HWMP_ROOTMODE:
2046 		ireq->i_val = hs->hs_rootmode;
2047 		break;
2048 	case IEEE80211_IOC_HWMP_MAXHOPS:
2049 		ireq->i_val = hs->hs_maxhops;
2050 		break;
2051 	default:
2052 		return ENOSYS;
2053 	}
2054 	return error;
2055 }
2056 IEEE80211_IOCTL_GET(hwmp, hwmp_ioctl_get80211);
2057 
2058 static int
2059 hwmp_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
2060 {
2061 	struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
2062 	int error;
2063 
2064 	if (vap->iv_opmode != IEEE80211_M_MBSS)
2065 		return ENOSYS;
2066 	error = 0;
2067 	switch (ireq->i_type) {
2068 	case IEEE80211_IOC_HWMP_ROOTMODE:
2069 		if (ireq->i_val < 0 || ireq->i_val > 3)
2070 			return EINVAL;
2071 		hs->hs_rootmode = ireq->i_val;
2072 		hwmp_rootmode_setup(vap);
2073 		break;
2074 	case IEEE80211_IOC_HWMP_MAXHOPS:
2075 		if (ireq->i_val <= 0 || ireq->i_val > 255)
2076 			return EINVAL;
2077 		hs->hs_maxhops = ireq->i_val;
2078 		break;
2079 	default:
2080 		return ENOSYS;
2081 	}
2082 	return error;
2083 }
2084 IEEE80211_IOCTL_SET(hwmp, hwmp_ioctl_set80211);
2085