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