xref: /freebsd/sys/net/if_lagg.c (revision 8bcb0991864975618c09697b1aca10683346d9f0)
1 /*	$OpenBSD: if_trunk.c,v 1.30 2007/01/31 06:20:19 reyk Exp $	*/
2 
3 /*
4  * Copyright (c) 2005, 2006 Reyk Floeter <reyk@openbsd.org>
5  * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
6  * Copyright (c) 2014, 2016 Marcelo Araujo <araujo@FreeBSD.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23 
24 #include "opt_inet.h"
25 #include "opt_inet6.h"
26 #include "opt_kern_tls.h"
27 #include "opt_ratelimit.h"
28 
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/malloc.h>
32 #include <sys/mbuf.h>
33 #include <sys/queue.h>
34 #include <sys/socket.h>
35 #include <sys/sockio.h>
36 #include <sys/sysctl.h>
37 #include <sys/module.h>
38 #include <sys/priv.h>
39 #include <sys/systm.h>
40 #include <sys/proc.h>
41 #include <sys/lock.h>
42 #include <sys/rmlock.h>
43 #include <sys/sx.h>
44 #include <sys/taskqueue.h>
45 #include <sys/eventhandler.h>
46 
47 #include <net/ethernet.h>
48 #include <net/if.h>
49 #include <net/if_clone.h>
50 #include <net/if_arp.h>
51 #include <net/if_dl.h>
52 #include <net/if_media.h>
53 #include <net/if_types.h>
54 #include <net/if_var.h>
55 #include <net/bpf.h>
56 #include <net/vnet.h>
57 
58 #if defined(INET) || defined(INET6)
59 #include <netinet/in.h>
60 #include <netinet/ip.h>
61 #endif
62 #ifdef INET
63 #include <netinet/in_systm.h>
64 #include <netinet/if_ether.h>
65 #endif
66 
67 #ifdef INET6
68 #include <netinet/ip6.h>
69 #include <netinet6/in6_var.h>
70 #include <netinet6/in6_ifattach.h>
71 #endif
72 
73 #include <net/if_vlan_var.h>
74 #include <net/if_lagg.h>
75 #include <net/ieee8023ad_lacp.h>
76 
77 #define	LAGG_RLOCK()	struct epoch_tracker lagg_et; epoch_enter_preempt(net_epoch_preempt, &lagg_et)
78 #define	LAGG_RUNLOCK()	epoch_exit_preempt(net_epoch_preempt, &lagg_et)
79 #define	LAGG_RLOCK_ASSERT()	NET_EPOCH_ASSERT()
80 #define	LAGG_UNLOCK_ASSERT()	MPASS(!in_epoch(net_epoch_preempt))
81 
82 #define	LAGG_SX_INIT(_sc)	sx_init(&(_sc)->sc_sx, "if_lagg sx")
83 #define	LAGG_SX_DESTROY(_sc)	sx_destroy(&(_sc)->sc_sx)
84 #define	LAGG_XLOCK(_sc)		sx_xlock(&(_sc)->sc_sx)
85 #define	LAGG_XUNLOCK(_sc)	sx_xunlock(&(_sc)->sc_sx)
86 #define	LAGG_SXLOCK_ASSERT(_sc)	sx_assert(&(_sc)->sc_sx, SA_LOCKED)
87 #define	LAGG_XLOCK_ASSERT(_sc)	sx_assert(&(_sc)->sc_sx, SA_XLOCKED)
88 
89 /* Special flags we should propagate to the lagg ports. */
90 static struct {
91 	int flag;
92 	int (*func)(struct ifnet *, int);
93 } lagg_pflags[] = {
94 	{IFF_PROMISC, ifpromisc},
95 	{IFF_ALLMULTI, if_allmulti},
96 	{0, NULL}
97 };
98 
99 struct lagg_snd_tag {
100 	struct m_snd_tag com;
101 	struct m_snd_tag *tag;
102 };
103 
104 VNET_DEFINE(SLIST_HEAD(__trhead, lagg_softc), lagg_list); /* list of laggs */
105 #define	V_lagg_list	VNET(lagg_list)
106 VNET_DEFINE_STATIC(struct mtx, lagg_list_mtx);
107 #define	V_lagg_list_mtx	VNET(lagg_list_mtx)
108 #define	LAGG_LIST_LOCK_INIT(x)		mtx_init(&V_lagg_list_mtx, \
109 					"if_lagg list", NULL, MTX_DEF)
110 #define	LAGG_LIST_LOCK_DESTROY(x)	mtx_destroy(&V_lagg_list_mtx)
111 #define	LAGG_LIST_LOCK(x)		mtx_lock(&V_lagg_list_mtx)
112 #define	LAGG_LIST_UNLOCK(x)		mtx_unlock(&V_lagg_list_mtx)
113 eventhandler_tag	lagg_detach_cookie = NULL;
114 
115 static int	lagg_clone_create(struct if_clone *, int, caddr_t);
116 static void	lagg_clone_destroy(struct ifnet *);
117 VNET_DEFINE_STATIC(struct if_clone *, lagg_cloner);
118 #define	V_lagg_cloner	VNET(lagg_cloner)
119 static const char laggname[] = "lagg";
120 static MALLOC_DEFINE(M_LAGG, laggname, "802.3AD Link Aggregation Interface");
121 
122 static void	lagg_capabilities(struct lagg_softc *);
123 static int	lagg_port_create(struct lagg_softc *, struct ifnet *);
124 static int	lagg_port_destroy(struct lagg_port *, int);
125 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
126 static void	lagg_linkstate(struct lagg_softc *);
127 static void	lagg_port_state(struct ifnet *, int);
128 static int	lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
129 static int	lagg_port_output(struct ifnet *, struct mbuf *,
130 		    const struct sockaddr *, struct route *);
131 static void	lagg_port_ifdetach(void *arg __unused, struct ifnet *);
132 #ifdef LAGG_PORT_STACKING
133 static int	lagg_port_checkstacking(struct lagg_softc *);
134 #endif
135 static void	lagg_port2req(struct lagg_port *, struct lagg_reqport *);
136 static void	lagg_init(void *);
137 static void	lagg_stop(struct lagg_softc *);
138 static int	lagg_ioctl(struct ifnet *, u_long, caddr_t);
139 #if defined(KERN_TLS) || defined(RATELIMIT)
140 static int	lagg_snd_tag_alloc(struct ifnet *,
141 		    union if_snd_tag_alloc_params *,
142 		    struct m_snd_tag **);
143 static int	lagg_snd_tag_modify(struct m_snd_tag *,
144 		    union if_snd_tag_modify_params *);
145 static int	lagg_snd_tag_query(struct m_snd_tag *,
146 		    union if_snd_tag_query_params *);
147 static void	lagg_snd_tag_free(struct m_snd_tag *);
148 static void     lagg_ratelimit_query(struct ifnet *,
149 		    struct if_ratelimit_query_results *);
150 #endif
151 static int	lagg_setmulti(struct lagg_port *);
152 static int	lagg_clrmulti(struct lagg_port *);
153 static	int	lagg_setcaps(struct lagg_port *, int cap);
154 static	int	lagg_setflag(struct lagg_port *, int, int,
155 		    int (*func)(struct ifnet *, int));
156 static	int	lagg_setflags(struct lagg_port *, int status);
157 static uint64_t lagg_get_counter(struct ifnet *ifp, ift_counter cnt);
158 static int	lagg_transmit(struct ifnet *, struct mbuf *);
159 static void	lagg_qflush(struct ifnet *);
160 static int	lagg_media_change(struct ifnet *);
161 static void	lagg_media_status(struct ifnet *, struct ifmediareq *);
162 static struct lagg_port *lagg_link_active(struct lagg_softc *,
163 	    struct lagg_port *);
164 
165 /* Simple round robin */
166 static void	lagg_rr_attach(struct lagg_softc *);
167 static int	lagg_rr_start(struct lagg_softc *, struct mbuf *);
168 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
169 		    struct mbuf *);
170 
171 /* Active failover */
172 static int	lagg_fail_start(struct lagg_softc *, struct mbuf *);
173 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
174 		    struct mbuf *);
175 
176 /* Loadbalancing */
177 static void	lagg_lb_attach(struct lagg_softc *);
178 static void	lagg_lb_detach(struct lagg_softc *);
179 static int	lagg_lb_port_create(struct lagg_port *);
180 static void	lagg_lb_port_destroy(struct lagg_port *);
181 static int	lagg_lb_start(struct lagg_softc *, struct mbuf *);
182 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
183 		    struct mbuf *);
184 static int	lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
185 
186 /* Broadcast */
187 static int    lagg_bcast_start(struct lagg_softc *, struct mbuf *);
188 static struct mbuf *lagg_bcast_input(struct lagg_softc *, struct lagg_port *,
189 		    struct mbuf *);
190 
191 /* 802.3ad LACP */
192 static void	lagg_lacp_attach(struct lagg_softc *);
193 static void	lagg_lacp_detach(struct lagg_softc *);
194 static int	lagg_lacp_start(struct lagg_softc *, struct mbuf *);
195 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
196 		    struct mbuf *);
197 static void	lagg_lacp_lladdr(struct lagg_softc *);
198 
199 /* lagg protocol table */
200 static const struct lagg_proto {
201 	lagg_proto	pr_num;
202 	void		(*pr_attach)(struct lagg_softc *);
203 	void		(*pr_detach)(struct lagg_softc *);
204 	int		(*pr_start)(struct lagg_softc *, struct mbuf *);
205 	struct mbuf *	(*pr_input)(struct lagg_softc *, struct lagg_port *,
206 			    struct mbuf *);
207 	int		(*pr_addport)(struct lagg_port *);
208 	void		(*pr_delport)(struct lagg_port *);
209 	void		(*pr_linkstate)(struct lagg_port *);
210 	void 		(*pr_init)(struct lagg_softc *);
211 	void 		(*pr_stop)(struct lagg_softc *);
212 	void 		(*pr_lladdr)(struct lagg_softc *);
213 	void		(*pr_request)(struct lagg_softc *, void *);
214 	void		(*pr_portreq)(struct lagg_port *, void *);
215 } lagg_protos[] = {
216     {
217 	.pr_num = LAGG_PROTO_NONE
218     },
219     {
220 	.pr_num = LAGG_PROTO_ROUNDROBIN,
221 	.pr_attach = lagg_rr_attach,
222 	.pr_start = lagg_rr_start,
223 	.pr_input = lagg_rr_input,
224     },
225     {
226 	.pr_num = LAGG_PROTO_FAILOVER,
227 	.pr_start = lagg_fail_start,
228 	.pr_input = lagg_fail_input,
229     },
230     {
231 	.pr_num = LAGG_PROTO_LOADBALANCE,
232 	.pr_attach = lagg_lb_attach,
233 	.pr_detach = lagg_lb_detach,
234 	.pr_start = lagg_lb_start,
235 	.pr_input = lagg_lb_input,
236 	.pr_addport = lagg_lb_port_create,
237 	.pr_delport = lagg_lb_port_destroy,
238     },
239     {
240 	.pr_num = LAGG_PROTO_LACP,
241 	.pr_attach = lagg_lacp_attach,
242 	.pr_detach = lagg_lacp_detach,
243 	.pr_start = lagg_lacp_start,
244 	.pr_input = lagg_lacp_input,
245 	.pr_addport = lacp_port_create,
246 	.pr_delport = lacp_port_destroy,
247 	.pr_linkstate = lacp_linkstate,
248 	.pr_init = lacp_init,
249 	.pr_stop = lacp_stop,
250 	.pr_lladdr = lagg_lacp_lladdr,
251 	.pr_request = lacp_req,
252 	.pr_portreq = lacp_portreq,
253     },
254     {
255 	.pr_num = LAGG_PROTO_BROADCAST,
256 	.pr_start = lagg_bcast_start,
257 	.pr_input = lagg_bcast_input,
258     },
259 };
260 
261 SYSCTL_DECL(_net_link);
262 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
263     "Link Aggregation");
264 
265 /* Allow input on any failover links */
266 VNET_DEFINE_STATIC(int, lagg_failover_rx_all);
267 #define	V_lagg_failover_rx_all	VNET(lagg_failover_rx_all)
268 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW | CTLFLAG_VNET,
269     &VNET_NAME(lagg_failover_rx_all), 0,
270     "Accept input from any interface in a failover lagg");
271 
272 /* Default value for using flowid */
273 VNET_DEFINE_STATIC(int, def_use_flowid) = 0;
274 #define	V_def_use_flowid	VNET(def_use_flowid)
275 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RWTUN,
276     &VNET_NAME(def_use_flowid), 0,
277     "Default setting for using flow id for load sharing");
278 
279 /* Default value for using numa */
280 VNET_DEFINE_STATIC(int, def_use_numa) = 1;
281 #define	V_def_use_numa	VNET(def_use_numa)
282 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_numa, CTLFLAG_RWTUN,
283     &VNET_NAME(def_use_numa), 0,
284     "Use numa to steer flows");
285 
286 /* Default value for flowid shift */
287 VNET_DEFINE_STATIC(int, def_flowid_shift) = 16;
288 #define	V_def_flowid_shift	VNET(def_flowid_shift)
289 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
290     &VNET_NAME(def_flowid_shift), 0,
291     "Default setting for flowid shift for load sharing");
292 
293 static void
294 vnet_lagg_init(const void *unused __unused)
295 {
296 
297 	LAGG_LIST_LOCK_INIT();
298 	SLIST_INIT(&V_lagg_list);
299 	V_lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
300 	    lagg_clone_destroy, 0);
301 }
302 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
303     vnet_lagg_init, NULL);
304 
305 static void
306 vnet_lagg_uninit(const void *unused __unused)
307 {
308 
309 	if_clone_detach(V_lagg_cloner);
310 	LAGG_LIST_LOCK_DESTROY();
311 }
312 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
313     vnet_lagg_uninit, NULL);
314 
315 static int
316 lagg_modevent(module_t mod, int type, void *data)
317 {
318 
319 	switch (type) {
320 	case MOD_LOAD:
321 		lagg_input_p = lagg_input;
322 		lagg_linkstate_p = lagg_port_state;
323 		lagg_detach_cookie = EVENTHANDLER_REGISTER(
324 		    ifnet_departure_event, lagg_port_ifdetach, NULL,
325 		    EVENTHANDLER_PRI_ANY);
326 		break;
327 	case MOD_UNLOAD:
328 		EVENTHANDLER_DEREGISTER(ifnet_departure_event,
329 		    lagg_detach_cookie);
330 		lagg_input_p = NULL;
331 		lagg_linkstate_p = NULL;
332 		break;
333 	default:
334 		return (EOPNOTSUPP);
335 	}
336 	return (0);
337 }
338 
339 static moduledata_t lagg_mod = {
340 	"if_lagg",
341 	lagg_modevent,
342 	0
343 };
344 
345 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
346 MODULE_VERSION(if_lagg, 1);
347 
348 static void
349 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr)
350 {
351 
352 	LAGG_XLOCK_ASSERT(sc);
353 	KASSERT(sc->sc_proto == LAGG_PROTO_NONE, ("%s: sc %p has proto",
354 	    __func__, sc));
355 
356 	if (sc->sc_ifflags & IFF_DEBUG)
357 		if_printf(sc->sc_ifp, "using proto %u\n", pr);
358 
359 	if (lagg_protos[pr].pr_attach != NULL)
360 		lagg_protos[pr].pr_attach(sc);
361 	sc->sc_proto = pr;
362 }
363 
364 static void
365 lagg_proto_detach(struct lagg_softc *sc)
366 {
367 	lagg_proto pr;
368 
369 	LAGG_XLOCK_ASSERT(sc);
370 	pr = sc->sc_proto;
371 	sc->sc_proto = LAGG_PROTO_NONE;
372 
373 	if (lagg_protos[pr].pr_detach != NULL)
374 		lagg_protos[pr].pr_detach(sc);
375 }
376 
377 static int
378 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m)
379 {
380 
381 	return (lagg_protos[sc->sc_proto].pr_start(sc, m));
382 }
383 
384 static struct mbuf *
385 lagg_proto_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
386 {
387 
388 	return (lagg_protos[sc->sc_proto].pr_input(sc, lp, m));
389 }
390 
391 static int
392 lagg_proto_addport(struct lagg_softc *sc, struct lagg_port *lp)
393 {
394 
395 	if (lagg_protos[sc->sc_proto].pr_addport == NULL)
396 		return (0);
397 	else
398 		return (lagg_protos[sc->sc_proto].pr_addport(lp));
399 }
400 
401 static void
402 lagg_proto_delport(struct lagg_softc *sc, struct lagg_port *lp)
403 {
404 
405 	if (lagg_protos[sc->sc_proto].pr_delport != NULL)
406 		lagg_protos[sc->sc_proto].pr_delport(lp);
407 }
408 
409 static void
410 lagg_proto_linkstate(struct lagg_softc *sc, struct lagg_port *lp)
411 {
412 
413 	if (lagg_protos[sc->sc_proto].pr_linkstate != NULL)
414 		lagg_protos[sc->sc_proto].pr_linkstate(lp);
415 }
416 
417 static void
418 lagg_proto_init(struct lagg_softc *sc)
419 {
420 
421 	if (lagg_protos[sc->sc_proto].pr_init != NULL)
422 		lagg_protos[sc->sc_proto].pr_init(sc);
423 }
424 
425 static void
426 lagg_proto_stop(struct lagg_softc *sc)
427 {
428 
429 	if (lagg_protos[sc->sc_proto].pr_stop != NULL)
430 		lagg_protos[sc->sc_proto].pr_stop(sc);
431 }
432 
433 static void
434 lagg_proto_lladdr(struct lagg_softc *sc)
435 {
436 
437 	if (lagg_protos[sc->sc_proto].pr_lladdr != NULL)
438 		lagg_protos[sc->sc_proto].pr_lladdr(sc);
439 }
440 
441 static void
442 lagg_proto_request(struct lagg_softc *sc, void *v)
443 {
444 
445 	if (lagg_protos[sc->sc_proto].pr_request != NULL)
446 		lagg_protos[sc->sc_proto].pr_request(sc, v);
447 }
448 
449 static void
450 lagg_proto_portreq(struct lagg_softc *sc, struct lagg_port *lp, void *v)
451 {
452 
453 	if (lagg_protos[sc->sc_proto].pr_portreq != NULL)
454 		lagg_protos[sc->sc_proto].pr_portreq(lp, v);
455 }
456 
457 /*
458  * This routine is run via an vlan
459  * config EVENT
460  */
461 static void
462 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
463 {
464 	struct lagg_softc *sc = ifp->if_softc;
465 	struct lagg_port *lp;
466 
467 	if (ifp->if_softc !=  arg)   /* Not our event */
468 		return;
469 
470 	LAGG_RLOCK();
471 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
472 		EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
473 	LAGG_RUNLOCK();
474 }
475 
476 /*
477  * This routine is run via an vlan
478  * unconfig EVENT
479  */
480 static void
481 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
482 {
483 	struct lagg_softc *sc = ifp->if_softc;
484 	struct lagg_port *lp;
485 
486 	if (ifp->if_softc !=  arg)   /* Not our event */
487 		return;
488 
489 	LAGG_RLOCK();
490 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
491 		EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
492 	LAGG_RUNLOCK();
493 }
494 
495 static int
496 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
497 {
498 	struct lagg_softc *sc;
499 	struct ifnet *ifp;
500 	static const u_char eaddr[6];	/* 00:00:00:00:00:00 */
501 
502 	sc = malloc(sizeof(*sc), M_LAGG, M_WAITOK|M_ZERO);
503 	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
504 	if (ifp == NULL) {
505 		free(sc, M_LAGG);
506 		return (ENOSPC);
507 	}
508 	LAGG_SX_INIT(sc);
509 
510 	LAGG_XLOCK(sc);
511 	if (V_def_use_flowid)
512 		sc->sc_opts |= LAGG_OPT_USE_FLOWID;
513 	if (V_def_use_numa)
514 		sc->sc_opts |= LAGG_OPT_USE_NUMA;
515 	sc->flowid_shift = V_def_flowid_shift;
516 
517 	/* Hash all layers by default */
518 	sc->sc_flags = MBUF_HASHFLAG_L2|MBUF_HASHFLAG_L3|MBUF_HASHFLAG_L4;
519 
520 	lagg_proto_attach(sc, LAGG_PROTO_DEFAULT);
521 
522 	CK_SLIST_INIT(&sc->sc_ports);
523 
524 	/* Initialise pseudo media types */
525 	ifmedia_init(&sc->sc_media, 0, lagg_media_change,
526 	    lagg_media_status);
527 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
528 	ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
529 
530 	if_initname(ifp, laggname, unit);
531 	ifp->if_softc = sc;
532 	ifp->if_transmit = lagg_transmit;
533 	ifp->if_qflush = lagg_qflush;
534 	ifp->if_init = lagg_init;
535 	ifp->if_ioctl = lagg_ioctl;
536 	ifp->if_get_counter = lagg_get_counter;
537 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
538 #if defined(KERN_TLS) || defined(RATELIMIT)
539 	ifp->if_snd_tag_alloc = lagg_snd_tag_alloc;
540 	ifp->if_snd_tag_modify = lagg_snd_tag_modify;
541 	ifp->if_snd_tag_query = lagg_snd_tag_query;
542 	ifp->if_snd_tag_free = lagg_snd_tag_free;
543 	ifp->if_ratelimit_query = lagg_ratelimit_query;
544 #endif
545 	ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
546 
547 	/*
548 	 * Attach as an ordinary ethernet device, children will be attached
549 	 * as special device IFT_IEEE8023ADLAG.
550 	 */
551 	ether_ifattach(ifp, eaddr);
552 
553 	sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
554 		lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
555 	sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
556 		lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
557 
558 	/* Insert into the global list of laggs */
559 	LAGG_LIST_LOCK();
560 	SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries);
561 	LAGG_LIST_UNLOCK();
562 	LAGG_XUNLOCK(sc);
563 
564 	return (0);
565 }
566 
567 static void
568 lagg_clone_destroy(struct ifnet *ifp)
569 {
570 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
571 	struct lagg_port *lp;
572 
573 	LAGG_XLOCK(sc);
574 	sc->sc_destroying = 1;
575 	lagg_stop(sc);
576 	ifp->if_flags &= ~IFF_UP;
577 
578 	EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
579 	EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
580 
581 	/* Shutdown and remove lagg ports */
582 	while ((lp = CK_SLIST_FIRST(&sc->sc_ports)) != NULL)
583 		lagg_port_destroy(lp, 1);
584 
585 	/* Unhook the aggregation protocol */
586 	lagg_proto_detach(sc);
587 	LAGG_XUNLOCK(sc);
588 
589 	ifmedia_removeall(&sc->sc_media);
590 	ether_ifdetach(ifp);
591 	if_free(ifp);
592 
593 	LAGG_LIST_LOCK();
594 	SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
595 	LAGG_LIST_UNLOCK();
596 
597 	LAGG_SX_DESTROY(sc);
598 	free(sc, M_LAGG);
599 }
600 
601 static void
602 lagg_capabilities(struct lagg_softc *sc)
603 {
604 	struct lagg_port *lp;
605 	int cap, ena, pena;
606 	uint64_t hwa;
607 	struct ifnet_hw_tsomax hw_tsomax;
608 
609 	LAGG_XLOCK_ASSERT(sc);
610 
611 	/* Get common enabled capabilities for the lagg ports */
612 	ena = ~0;
613 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
614 		ena &= lp->lp_ifp->if_capenable;
615 	ena = (ena == ~0 ? 0 : ena);
616 
617 	/*
618 	 * Apply common enabled capabilities back to the lagg ports.
619 	 * May require several iterations if they are dependent.
620 	 */
621 	do {
622 		pena = ena;
623 		CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
624 			lagg_setcaps(lp, ena);
625 			ena &= lp->lp_ifp->if_capenable;
626 		}
627 	} while (pena != ena);
628 
629 	/* Get other capabilities from the lagg ports */
630 	cap = ~0;
631 	hwa = ~(uint64_t)0;
632 	memset(&hw_tsomax, 0, sizeof(hw_tsomax));
633 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
634 		cap &= lp->lp_ifp->if_capabilities;
635 		hwa &= lp->lp_ifp->if_hwassist;
636 		if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
637 	}
638 	cap = (cap == ~0 ? 0 : cap);
639 	hwa = (hwa == ~(uint64_t)0 ? 0 : hwa);
640 
641 	if (sc->sc_ifp->if_capabilities != cap ||
642 	    sc->sc_ifp->if_capenable != ena ||
643 	    sc->sc_ifp->if_hwassist != hwa ||
644 	    if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
645 		sc->sc_ifp->if_capabilities = cap;
646 		sc->sc_ifp->if_capenable = ena;
647 		sc->sc_ifp->if_hwassist = hwa;
648 		getmicrotime(&sc->sc_ifp->if_lastchange);
649 
650 		if (sc->sc_ifflags & IFF_DEBUG)
651 			if_printf(sc->sc_ifp,
652 			    "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
653 	}
654 }
655 
656 static int
657 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
658 {
659 	struct lagg_softc *sc_ptr;
660 	struct lagg_port *lp, *tlp;
661 	struct ifreq ifr;
662 	int error, i, oldmtu;
663 	uint64_t *pval;
664 
665 	LAGG_XLOCK_ASSERT(sc);
666 
667 	if (sc->sc_ifp == ifp) {
668 		if_printf(sc->sc_ifp,
669 		    "cannot add a lagg to itself as a port\n");
670 		return (EINVAL);
671 	}
672 
673 	/* Limit the maximal number of lagg ports */
674 	if (sc->sc_count >= LAGG_MAX_PORTS)
675 		return (ENOSPC);
676 
677 	/* Check if port has already been associated to a lagg */
678 	if (ifp->if_lagg != NULL) {
679 		/* Port is already in the current lagg? */
680 		lp = (struct lagg_port *)ifp->if_lagg;
681 		if (lp->lp_softc == sc)
682 			return (EEXIST);
683 		return (EBUSY);
684 	}
685 
686 	/* XXX Disallow non-ethernet interfaces (this should be any of 802) */
687 	if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN)
688 		return (EPROTONOSUPPORT);
689 
690 	/* Allow the first Ethernet member to define the MTU */
691 	oldmtu = -1;
692 	if (CK_SLIST_EMPTY(&sc->sc_ports)) {
693 		sc->sc_ifp->if_mtu = ifp->if_mtu;
694 	} else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
695 		if (ifp->if_ioctl == NULL) {
696 			if_printf(sc->sc_ifp, "cannot change MTU for %s\n",
697 			    ifp->if_xname);
698 			return (EINVAL);
699 		}
700 		oldmtu = ifp->if_mtu;
701 		strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name));
702 		ifr.ifr_mtu = sc->sc_ifp->if_mtu;
703 		error = (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
704 		if (error != 0) {
705 			if_printf(sc->sc_ifp, "invalid MTU for %s\n",
706 			    ifp->if_xname);
707 			return (error);
708 		}
709 		ifr.ifr_mtu = oldmtu;
710 	}
711 
712 	lp = malloc(sizeof(struct lagg_port), M_LAGG, M_WAITOK|M_ZERO);
713 	lp->lp_softc = sc;
714 
715 	/* Check if port is a stacked lagg */
716 	LAGG_LIST_LOCK();
717 	SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
718 		if (ifp == sc_ptr->sc_ifp) {
719 			LAGG_LIST_UNLOCK();
720 			free(lp, M_LAGG);
721 			if (oldmtu != -1)
722 				(*ifp->if_ioctl)(ifp, SIOCSIFMTU,
723 				    (caddr_t)&ifr);
724 			return (EINVAL);
725 			/* XXX disable stacking for the moment, its untested */
726 #ifdef LAGG_PORT_STACKING
727 			lp->lp_flags |= LAGG_PORT_STACK;
728 			if (lagg_port_checkstacking(sc_ptr) >=
729 			    LAGG_MAX_STACKING) {
730 				LAGG_LIST_UNLOCK();
731 				free(lp, M_LAGG);
732 				if (oldmtu != -1)
733 					(*ifp->if_ioctl)(ifp, SIOCSIFMTU,
734 					    (caddr_t)&ifr);
735 				return (E2BIG);
736 			}
737 #endif
738 		}
739 	}
740 	LAGG_LIST_UNLOCK();
741 
742 	if_ref(ifp);
743 	lp->lp_ifp = ifp;
744 
745 	bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
746 	lp->lp_ifcapenable = ifp->if_capenable;
747 	if (CK_SLIST_EMPTY(&sc->sc_ports)) {
748 		bcopy(IF_LLADDR(ifp), IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
749 		lagg_proto_lladdr(sc);
750 		EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
751 	} else {
752 		if_setlladdr(ifp, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
753 	}
754 	lagg_setflags(lp, 1);
755 
756 	if (CK_SLIST_EMPTY(&sc->sc_ports))
757 		sc->sc_primary = lp;
758 
759 	/* Change the interface type */
760 	lp->lp_iftype = ifp->if_type;
761 	ifp->if_type = IFT_IEEE8023ADLAG;
762 	ifp->if_lagg = lp;
763 	lp->lp_ioctl = ifp->if_ioctl;
764 	ifp->if_ioctl = lagg_port_ioctl;
765 	lp->lp_output = ifp->if_output;
766 	ifp->if_output = lagg_port_output;
767 
768 	/* Read port counters */
769 	pval = lp->port_counters.val;
770 	for (i = 0; i < IFCOUNTERS; i++, pval++)
771 		*pval = ifp->if_get_counter(ifp, i);
772 
773 	/*
774 	 * Insert into the list of ports.
775 	 * Keep ports sorted by if_index. It is handy, when configuration
776 	 * is predictable and `ifconfig laggN create ...` command
777 	 * will lead to the same result each time.
778 	 */
779 	CK_SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
780 		if (tlp->lp_ifp->if_index < ifp->if_index && (
781 		    CK_SLIST_NEXT(tlp, lp_entries) == NULL ||
782 		    ((struct  lagg_port*)CK_SLIST_NEXT(tlp, lp_entries))->lp_ifp->if_index >
783 		    ifp->if_index))
784 			break;
785 	}
786 	if (tlp != NULL)
787 		CK_SLIST_INSERT_AFTER(tlp, lp, lp_entries);
788 	else
789 		CK_SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
790 	sc->sc_count++;
791 
792 	lagg_setmulti(lp);
793 
794 
795 	if ((error = lagg_proto_addport(sc, lp)) != 0) {
796 		/* Remove the port, without calling pr_delport. */
797 		lagg_port_destroy(lp, 0);
798 		if (oldmtu != -1)
799 			(*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
800 		return (error);
801 	}
802 
803 	/* Update lagg capabilities */
804 	lagg_capabilities(sc);
805 	lagg_linkstate(sc);
806 
807 	return (0);
808 }
809 
810 #ifdef LAGG_PORT_STACKING
811 static int
812 lagg_port_checkstacking(struct lagg_softc *sc)
813 {
814 	struct lagg_softc *sc_ptr;
815 	struct lagg_port *lp;
816 	int m = 0;
817 
818 	LAGG_SXLOCK_ASSERT(sc);
819 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
820 		if (lp->lp_flags & LAGG_PORT_STACK) {
821 			sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
822 			m = MAX(m, lagg_port_checkstacking(sc_ptr));
823 		}
824 	}
825 
826 	return (m + 1);
827 }
828 #endif
829 
830 static void
831 lagg_port_destroy_cb(epoch_context_t ec)
832 {
833 	struct lagg_port *lp;
834 	struct ifnet *ifp;
835 
836 	lp = __containerof(ec, struct lagg_port, lp_epoch_ctx);
837 	ifp = lp->lp_ifp;
838 
839 	if_rele(ifp);
840 	free(lp, M_LAGG);
841 }
842 
843 static int
844 lagg_port_destroy(struct lagg_port *lp, int rundelport)
845 {
846 	struct lagg_softc *sc = lp->lp_softc;
847 	struct lagg_port *lp_ptr, *lp0;
848 	struct ifnet *ifp = lp->lp_ifp;
849 	uint64_t *pval, vdiff;
850 	int i;
851 
852 	LAGG_XLOCK_ASSERT(sc);
853 
854 	if (rundelport)
855 		lagg_proto_delport(sc, lp);
856 
857 	if (lp->lp_detaching == 0)
858 		lagg_clrmulti(lp);
859 
860 	/* Restore interface */
861 	ifp->if_type = lp->lp_iftype;
862 	ifp->if_ioctl = lp->lp_ioctl;
863 	ifp->if_output = lp->lp_output;
864 	ifp->if_lagg = NULL;
865 
866 	/* Update detached port counters */
867 	pval = lp->port_counters.val;
868 	for (i = 0; i < IFCOUNTERS; i++, pval++) {
869 		vdiff = ifp->if_get_counter(ifp, i) - *pval;
870 		sc->detached_counters.val[i] += vdiff;
871 	}
872 
873 	/* Finally, remove the port from the lagg */
874 	CK_SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
875 	sc->sc_count--;
876 
877 	/* Update the primary interface */
878 	if (lp == sc->sc_primary) {
879 		uint8_t lladdr[ETHER_ADDR_LEN];
880 
881 		if ((lp0 = CK_SLIST_FIRST(&sc->sc_ports)) == NULL)
882 			bzero(&lladdr, ETHER_ADDR_LEN);
883 		else
884 			bcopy(lp0->lp_lladdr, lladdr, ETHER_ADDR_LEN);
885 		sc->sc_primary = lp0;
886 		if (sc->sc_destroying == 0) {
887 			bcopy(lladdr, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
888 			lagg_proto_lladdr(sc);
889 			EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
890 		}
891 
892 		/*
893 		 * Update lladdr for each port (new primary needs update
894 		 * as well, to switch from old lladdr to its 'real' one)
895 		 */
896 		CK_SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
897 			if_setlladdr(lp_ptr->lp_ifp, lladdr, ETHER_ADDR_LEN);
898 	}
899 
900 	if (lp->lp_ifflags)
901 		if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
902 
903 	if (lp->lp_detaching == 0) {
904 		lagg_setflags(lp, 0);
905 		lagg_setcaps(lp, lp->lp_ifcapenable);
906 		if_setlladdr(ifp, lp->lp_lladdr, ETHER_ADDR_LEN);
907 	}
908 
909 	/*
910 	 * free port and release it's ifnet reference after a grace period has
911 	 * elapsed.
912 	 */
913 	NET_EPOCH_CALL(lagg_port_destroy_cb, &lp->lp_epoch_ctx);
914 	/* Update lagg capabilities */
915 	lagg_capabilities(sc);
916 	lagg_linkstate(sc);
917 
918 	return (0);
919 }
920 
921 static int
922 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
923 {
924 	struct lagg_reqport *rp = (struct lagg_reqport *)data;
925 	struct lagg_softc *sc;
926 	struct lagg_port *lp = NULL;
927 	int error = 0;
928 
929 	/* Should be checked by the caller */
930 	if (ifp->if_type != IFT_IEEE8023ADLAG ||
931 	    (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
932 		goto fallback;
933 
934 	switch (cmd) {
935 	case SIOCGLAGGPORT:
936 		if (rp->rp_portname[0] == '\0' ||
937 		    ifunit(rp->rp_portname) != ifp) {
938 			error = EINVAL;
939 			break;
940 		}
941 
942 		LAGG_RLOCK();
943 		if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
944 			error = ENOENT;
945 			LAGG_RUNLOCK();
946 			break;
947 		}
948 
949 		lagg_port2req(lp, rp);
950 		LAGG_RUNLOCK();
951 		break;
952 
953 	case SIOCSIFCAP:
954 		if (lp->lp_ioctl == NULL) {
955 			error = EINVAL;
956 			break;
957 		}
958 		error = (*lp->lp_ioctl)(ifp, cmd, data);
959 		if (error)
960 			break;
961 
962 		/* Update lagg interface capabilities */
963 		LAGG_XLOCK(sc);
964 		lagg_capabilities(sc);
965 		LAGG_XUNLOCK(sc);
966 		VLAN_CAPABILITIES(sc->sc_ifp);
967 		break;
968 
969 	case SIOCSIFMTU:
970 		/* Do not allow the MTU to be changed once joined */
971 		error = EINVAL;
972 		break;
973 
974 	default:
975 		goto fallback;
976 	}
977 
978 	return (error);
979 
980 fallback:
981 	if (lp != NULL && lp->lp_ioctl != NULL)
982 		return ((*lp->lp_ioctl)(ifp, cmd, data));
983 
984 	return (EINVAL);
985 }
986 
987 /*
988  * Requests counter @cnt data.
989  *
990  * Counter value is calculated the following way:
991  * 1) for each port, sum  difference between current and "initial" measurements.
992  * 2) add lagg logical interface counters.
993  * 3) add data from detached_counters array.
994  *
995  * We also do the following things on ports attach/detach:
996  * 1) On port attach we store all counters it has into port_counter array.
997  * 2) On port detach we add the different between "initial" and
998  *   current counters data to detached_counters array.
999  */
1000 static uint64_t
1001 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
1002 {
1003 	struct lagg_softc *sc;
1004 	struct lagg_port *lp;
1005 	struct ifnet *lpifp;
1006 	uint64_t newval, oldval, vsum;
1007 
1008 	/* Revise this when we've got non-generic counters. */
1009 	KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1010 
1011 	sc = (struct lagg_softc *)ifp->if_softc;
1012 
1013 	vsum = 0;
1014 	LAGG_RLOCK();
1015 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1016 		/* Saved attached value */
1017 		oldval = lp->port_counters.val[cnt];
1018 		/* current value */
1019 		lpifp = lp->lp_ifp;
1020 		newval = lpifp->if_get_counter(lpifp, cnt);
1021 		/* Calculate diff and save new */
1022 		vsum += newval - oldval;
1023 	}
1024 	LAGG_RUNLOCK();
1025 
1026 	/*
1027 	 * Add counter data which might be added by upper
1028 	 * layer protocols operating on logical interface.
1029 	 */
1030 	vsum += if_get_counter_default(ifp, cnt);
1031 
1032 	/*
1033 	 * Add counter data from detached ports counters
1034 	 */
1035 	vsum += sc->detached_counters.val[cnt];
1036 
1037 
1038 	return (vsum);
1039 }
1040 
1041 /*
1042  * For direct output to child ports.
1043  */
1044 static int
1045 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1046 	const struct sockaddr *dst, struct route *ro)
1047 {
1048 	struct lagg_port *lp = ifp->if_lagg;
1049 
1050 	switch (dst->sa_family) {
1051 		case pseudo_AF_HDRCMPLT:
1052 		case AF_UNSPEC:
1053 			return ((*lp->lp_output)(ifp, m, dst, ro));
1054 	}
1055 
1056 	/* drop any other frames */
1057 	m_freem(m);
1058 	return (ENETDOWN);
1059 }
1060 
1061 static void
1062 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1063 {
1064 	struct lagg_port *lp;
1065 	struct lagg_softc *sc;
1066 
1067 	if ((lp = ifp->if_lagg) == NULL)
1068 		return;
1069 	/* If the ifnet is just being renamed, don't do anything. */
1070 	if (ifp->if_flags & IFF_RENAMING)
1071 		return;
1072 
1073 	sc = lp->lp_softc;
1074 
1075 	LAGG_XLOCK(sc);
1076 	lp->lp_detaching = 1;
1077 	lagg_port_destroy(lp, 1);
1078 	LAGG_XUNLOCK(sc);
1079 	VLAN_CAPABILITIES(sc->sc_ifp);
1080 }
1081 
1082 static void
1083 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1084 {
1085 	struct lagg_softc *sc = lp->lp_softc;
1086 
1087 	strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1088 	strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1089 	rp->rp_prio = lp->lp_prio;
1090 	rp->rp_flags = lp->lp_flags;
1091 	lagg_proto_portreq(sc, lp, &rp->rp_psc);
1092 
1093 	/* Add protocol specific flags */
1094 	switch (sc->sc_proto) {
1095 		case LAGG_PROTO_FAILOVER:
1096 			if (lp == sc->sc_primary)
1097 				rp->rp_flags |= LAGG_PORT_MASTER;
1098 			if (lp == lagg_link_active(sc, sc->sc_primary))
1099 				rp->rp_flags |= LAGG_PORT_ACTIVE;
1100 			break;
1101 
1102 		case LAGG_PROTO_ROUNDROBIN:
1103 		case LAGG_PROTO_LOADBALANCE:
1104 		case LAGG_PROTO_BROADCAST:
1105 			if (LAGG_PORTACTIVE(lp))
1106 				rp->rp_flags |= LAGG_PORT_ACTIVE;
1107 			break;
1108 
1109 		case LAGG_PROTO_LACP:
1110 			/* LACP has a different definition of active */
1111 			if (lacp_isactive(lp))
1112 				rp->rp_flags |= LAGG_PORT_ACTIVE;
1113 			if (lacp_iscollecting(lp))
1114 				rp->rp_flags |= LAGG_PORT_COLLECTING;
1115 			if (lacp_isdistributing(lp))
1116 				rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1117 			break;
1118 	}
1119 
1120 }
1121 
1122 static void
1123 lagg_init(void *xsc)
1124 {
1125 	struct lagg_softc *sc = (struct lagg_softc *)xsc;
1126 	struct ifnet *ifp = sc->sc_ifp;
1127 	struct lagg_port *lp;
1128 
1129 	LAGG_XLOCK(sc);
1130 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1131 		LAGG_XUNLOCK(sc);
1132 		return;
1133 	}
1134 
1135 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1136 
1137 	/*
1138 	 * Update the port lladdrs if needed.
1139 	 * This might be if_setlladdr() notification
1140 	 * that lladdr has been changed.
1141 	 */
1142 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1143 		if (memcmp(IF_LLADDR(ifp), IF_LLADDR(lp->lp_ifp),
1144 		    ETHER_ADDR_LEN) != 0)
1145 			if_setlladdr(lp->lp_ifp, IF_LLADDR(ifp), ETHER_ADDR_LEN);
1146 	}
1147 
1148 	lagg_proto_init(sc);
1149 
1150 	LAGG_XUNLOCK(sc);
1151 }
1152 
1153 static void
1154 lagg_stop(struct lagg_softc *sc)
1155 {
1156 	struct ifnet *ifp = sc->sc_ifp;
1157 
1158 	LAGG_XLOCK_ASSERT(sc);
1159 
1160 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1161 		return;
1162 
1163 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1164 
1165 	lagg_proto_stop(sc);
1166 }
1167 
1168 static int
1169 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1170 {
1171 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1172 	struct lagg_reqall *ra = (struct lagg_reqall *)data;
1173 	struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1174 	struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1175 	struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1176 	struct ifreq *ifr = (struct ifreq *)data;
1177 	struct lagg_port *lp;
1178 	struct ifnet *tpif;
1179 	struct thread *td = curthread;
1180 	char *buf, *outbuf;
1181 	int count, buflen, len, error = 0;
1182 
1183 	bzero(&rpbuf, sizeof(rpbuf));
1184 
1185 	switch (cmd) {
1186 	case SIOCGLAGG:
1187 		LAGG_XLOCK(sc);
1188 		buflen = sc->sc_count * sizeof(struct lagg_reqport);
1189 		outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1190 		ra->ra_proto = sc->sc_proto;
1191 		lagg_proto_request(sc, &ra->ra_psc);
1192 		count = 0;
1193 		buf = outbuf;
1194 		len = min(ra->ra_size, buflen);
1195 		CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1196 			if (len < sizeof(rpbuf))
1197 				break;
1198 
1199 			lagg_port2req(lp, &rpbuf);
1200 			memcpy(buf, &rpbuf, sizeof(rpbuf));
1201 			count++;
1202 			buf += sizeof(rpbuf);
1203 			len -= sizeof(rpbuf);
1204 		}
1205 		LAGG_XUNLOCK(sc);
1206 		ra->ra_ports = count;
1207 		ra->ra_size = count * sizeof(rpbuf);
1208 		error = copyout(outbuf, ra->ra_port, ra->ra_size);
1209 		free(outbuf, M_TEMP);
1210 		break;
1211 	case SIOCSLAGG:
1212 		error = priv_check(td, PRIV_NET_LAGG);
1213 		if (error)
1214 			break;
1215 		if (ra->ra_proto >= LAGG_PROTO_MAX) {
1216 			error = EPROTONOSUPPORT;
1217 			break;
1218 		}
1219 
1220 		LAGG_XLOCK(sc);
1221 		lagg_proto_detach(sc);
1222 		LAGG_UNLOCK_ASSERT();
1223 		lagg_proto_attach(sc, ra->ra_proto);
1224 		LAGG_XUNLOCK(sc);
1225 		break;
1226 	case SIOCGLAGGOPTS:
1227 		LAGG_XLOCK(sc);
1228 		ro->ro_opts = sc->sc_opts;
1229 		if (sc->sc_proto == LAGG_PROTO_LACP) {
1230 			struct lacp_softc *lsc;
1231 
1232 			lsc = (struct lacp_softc *)sc->sc_psc;
1233 			if (lsc->lsc_debug.lsc_tx_test != 0)
1234 				ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1235 			if (lsc->lsc_debug.lsc_rx_test != 0)
1236 				ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1237 			if (lsc->lsc_strict_mode != 0)
1238 				ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1239 			if (lsc->lsc_fast_timeout != 0)
1240 				ro->ro_opts |= LAGG_OPT_LACP_TIMEOUT;
1241 
1242 			ro->ro_active = sc->sc_active;
1243 		} else {
1244 			ro->ro_active = 0;
1245 			CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1246 				ro->ro_active += LAGG_PORTACTIVE(lp);
1247 		}
1248 		ro->ro_bkt = sc->sc_stride;
1249 		ro->ro_flapping = sc->sc_flapping;
1250 		ro->ro_flowid_shift = sc->flowid_shift;
1251 		LAGG_XUNLOCK(sc);
1252 		break;
1253 	case SIOCSLAGGOPTS:
1254 		error = priv_check(td, PRIV_NET_LAGG);
1255 		if (error)
1256 			break;
1257 
1258 		/*
1259 		 * The stride option was added without defining a corresponding
1260 		 * LAGG_OPT flag, so handle a non-zero value before checking
1261 		 * anything else to preserve compatibility.
1262 		 */
1263 		LAGG_XLOCK(sc);
1264 		if (ro->ro_opts == 0 && ro->ro_bkt != 0) {
1265 			if (sc->sc_proto != LAGG_PROTO_ROUNDROBIN) {
1266 				LAGG_XUNLOCK(sc);
1267 				error = EINVAL;
1268 				break;
1269 			}
1270 			sc->sc_stride = ro->ro_bkt;
1271 		}
1272 		if (ro->ro_opts == 0) {
1273 			LAGG_XUNLOCK(sc);
1274 			break;
1275 		}
1276 
1277 		/*
1278 		 * Set options.  LACP options are stored in sc->sc_psc,
1279 		 * not in sc_opts.
1280 		 */
1281 		int valid, lacp;
1282 
1283 		switch (ro->ro_opts) {
1284 		case LAGG_OPT_USE_FLOWID:
1285 		case -LAGG_OPT_USE_FLOWID:
1286 		case LAGG_OPT_USE_NUMA:
1287 		case -LAGG_OPT_USE_NUMA:
1288 		case LAGG_OPT_FLOWIDSHIFT:
1289 		case LAGG_OPT_RR_LIMIT:
1290 			valid = 1;
1291 			lacp = 0;
1292 			break;
1293 		case LAGG_OPT_LACP_TXTEST:
1294 		case -LAGG_OPT_LACP_TXTEST:
1295 		case LAGG_OPT_LACP_RXTEST:
1296 		case -LAGG_OPT_LACP_RXTEST:
1297 		case LAGG_OPT_LACP_STRICT:
1298 		case -LAGG_OPT_LACP_STRICT:
1299 		case LAGG_OPT_LACP_TIMEOUT:
1300 		case -LAGG_OPT_LACP_TIMEOUT:
1301 			valid = lacp = 1;
1302 			break;
1303 		default:
1304 			valid = lacp = 0;
1305 			break;
1306 		}
1307 
1308 		if (valid == 0 ||
1309 		    (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1310 			/* Invalid combination of options specified. */
1311 			error = EINVAL;
1312 			LAGG_XUNLOCK(sc);
1313 			break;	/* Return from SIOCSLAGGOPTS. */
1314 		}
1315 
1316 		/*
1317 		 * Store new options into sc->sc_opts except for
1318 		 * FLOWIDSHIFT, RR and LACP options.
1319 		 */
1320 		if (lacp == 0) {
1321 			if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1322 				sc->flowid_shift = ro->ro_flowid_shift;
1323 			else if (ro->ro_opts == LAGG_OPT_RR_LIMIT) {
1324 				if (sc->sc_proto != LAGG_PROTO_ROUNDROBIN ||
1325 				    ro->ro_bkt == 0) {
1326 					error = EINVAL;
1327 					LAGG_XUNLOCK(sc);
1328 					break;
1329 				}
1330 				sc->sc_stride = ro->ro_bkt;
1331 			} else if (ro->ro_opts > 0)
1332 				sc->sc_opts |= ro->ro_opts;
1333 			else
1334 				sc->sc_opts &= ~ro->ro_opts;
1335 		} else {
1336 			struct lacp_softc *lsc;
1337 			struct lacp_port *lp;
1338 
1339 			lsc = (struct lacp_softc *)sc->sc_psc;
1340 
1341 			switch (ro->ro_opts) {
1342 			case LAGG_OPT_LACP_TXTEST:
1343 				lsc->lsc_debug.lsc_tx_test = 1;
1344 				break;
1345 			case -LAGG_OPT_LACP_TXTEST:
1346 				lsc->lsc_debug.lsc_tx_test = 0;
1347 				break;
1348 			case LAGG_OPT_LACP_RXTEST:
1349 				lsc->lsc_debug.lsc_rx_test = 1;
1350 				break;
1351 			case -LAGG_OPT_LACP_RXTEST:
1352 				lsc->lsc_debug.lsc_rx_test = 0;
1353 				break;
1354 			case LAGG_OPT_LACP_STRICT:
1355 				lsc->lsc_strict_mode = 1;
1356 				break;
1357 			case -LAGG_OPT_LACP_STRICT:
1358 				lsc->lsc_strict_mode = 0;
1359 				break;
1360 			case LAGG_OPT_LACP_TIMEOUT:
1361 				LACP_LOCK(lsc);
1362         			LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1363                         		lp->lp_state |= LACP_STATE_TIMEOUT;
1364 				LACP_UNLOCK(lsc);
1365 				lsc->lsc_fast_timeout = 1;
1366 				break;
1367 			case -LAGG_OPT_LACP_TIMEOUT:
1368 				LACP_LOCK(lsc);
1369         			LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1370                         		lp->lp_state &= ~LACP_STATE_TIMEOUT;
1371 				LACP_UNLOCK(lsc);
1372 				lsc->lsc_fast_timeout = 0;
1373 				break;
1374 			}
1375 		}
1376 		LAGG_XUNLOCK(sc);
1377 		break;
1378 	case SIOCGLAGGFLAGS:
1379 		rf->rf_flags = 0;
1380 		LAGG_XLOCK(sc);
1381 		if (sc->sc_flags & MBUF_HASHFLAG_L2)
1382 			rf->rf_flags |= LAGG_F_HASHL2;
1383 		if (sc->sc_flags & MBUF_HASHFLAG_L3)
1384 			rf->rf_flags |= LAGG_F_HASHL3;
1385 		if (sc->sc_flags & MBUF_HASHFLAG_L4)
1386 			rf->rf_flags |= LAGG_F_HASHL4;
1387 		LAGG_XUNLOCK(sc);
1388 		break;
1389 	case SIOCSLAGGHASH:
1390 		error = priv_check(td, PRIV_NET_LAGG);
1391 		if (error)
1392 			break;
1393 		if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1394 			error = EINVAL;
1395 			break;
1396 		}
1397 		LAGG_XLOCK(sc);
1398 		sc->sc_flags = 0;
1399 		if (rf->rf_flags & LAGG_F_HASHL2)
1400 			sc->sc_flags |= MBUF_HASHFLAG_L2;
1401 		if (rf->rf_flags & LAGG_F_HASHL3)
1402 			sc->sc_flags |= MBUF_HASHFLAG_L3;
1403 		if (rf->rf_flags & LAGG_F_HASHL4)
1404 			sc->sc_flags |= MBUF_HASHFLAG_L4;
1405 		LAGG_XUNLOCK(sc);
1406 		break;
1407 	case SIOCGLAGGPORT:
1408 		if (rp->rp_portname[0] == '\0' ||
1409 		    (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1410 			error = EINVAL;
1411 			break;
1412 		}
1413 
1414 		LAGG_RLOCK();
1415 		if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1416 		    lp->lp_softc != sc) {
1417 			error = ENOENT;
1418 			LAGG_RUNLOCK();
1419 			if_rele(tpif);
1420 			break;
1421 		}
1422 
1423 		lagg_port2req(lp, rp);
1424 		LAGG_RUNLOCK();
1425 		if_rele(tpif);
1426 		break;
1427 	case SIOCSLAGGPORT:
1428 		error = priv_check(td, PRIV_NET_LAGG);
1429 		if (error)
1430 			break;
1431 		if (rp->rp_portname[0] == '\0' ||
1432 		    (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1433 			error = EINVAL;
1434 			break;
1435 		}
1436 #ifdef INET6
1437 		/*
1438 		 * A laggport interface should not have inet6 address
1439 		 * because two interfaces with a valid link-local
1440 		 * scope zone must not be merged in any form.  This
1441 		 * restriction is needed to prevent violation of
1442 		 * link-local scope zone.  Attempts to add a laggport
1443 		 * interface which has inet6 addresses triggers
1444 		 * removal of all inet6 addresses on the member
1445 		 * interface.
1446 		 */
1447 		if (in6ifa_llaonifp(tpif)) {
1448 			in6_ifdetach(tpif);
1449 				if_printf(sc->sc_ifp,
1450 				    "IPv6 addresses on %s have been removed "
1451 				    "before adding it as a member to prevent "
1452 				    "IPv6 address scope violation.\n",
1453 				    tpif->if_xname);
1454 		}
1455 #endif
1456 		LAGG_XLOCK(sc);
1457 		error = lagg_port_create(sc, tpif);
1458 		LAGG_XUNLOCK(sc);
1459 		if_rele(tpif);
1460 		VLAN_CAPABILITIES(ifp);
1461 		break;
1462 	case SIOCSLAGGDELPORT:
1463 		error = priv_check(td, PRIV_NET_LAGG);
1464 		if (error)
1465 			break;
1466 		if (rp->rp_portname[0] == '\0' ||
1467 		    (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1468 			error = EINVAL;
1469 			break;
1470 		}
1471 
1472 		LAGG_XLOCK(sc);
1473 		if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1474 		    lp->lp_softc != sc) {
1475 			error = ENOENT;
1476 			LAGG_XUNLOCK(sc);
1477 			if_rele(tpif);
1478 			break;
1479 		}
1480 
1481 		error = lagg_port_destroy(lp, 1);
1482 		LAGG_XUNLOCK(sc);
1483 		if_rele(tpif);
1484 		VLAN_CAPABILITIES(ifp);
1485 		break;
1486 	case SIOCSIFFLAGS:
1487 		/* Set flags on ports too */
1488 		LAGG_XLOCK(sc);
1489 		CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1490 			lagg_setflags(lp, 1);
1491 		}
1492 
1493 		if (!(ifp->if_flags & IFF_UP) &&
1494 		    (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1495 			/*
1496 			 * If interface is marked down and it is running,
1497 			 * then stop and disable it.
1498 			 */
1499 			lagg_stop(sc);
1500 			LAGG_XUNLOCK(sc);
1501 		} else if ((ifp->if_flags & IFF_UP) &&
1502 		    !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1503 			/*
1504 			 * If interface is marked up and it is stopped, then
1505 			 * start it.
1506 			 */
1507 			LAGG_XUNLOCK(sc);
1508 			(*ifp->if_init)(sc);
1509 		} else
1510 			LAGG_XUNLOCK(sc);
1511 		break;
1512 	case SIOCADDMULTI:
1513 	case SIOCDELMULTI:
1514 		LAGG_XLOCK(sc);
1515 		CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1516 			lagg_clrmulti(lp);
1517 			lagg_setmulti(lp);
1518 		}
1519 		LAGG_XUNLOCK(sc);
1520 		error = 0;
1521 		break;
1522 	case SIOCSIFMEDIA:
1523 	case SIOCGIFMEDIA:
1524 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1525 		break;
1526 
1527 	case SIOCSIFCAP:
1528 		LAGG_XLOCK(sc);
1529 		CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1530 			if (lp->lp_ioctl != NULL)
1531 				(*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1532 		}
1533 		lagg_capabilities(sc);
1534 		LAGG_XUNLOCK(sc);
1535 		VLAN_CAPABILITIES(ifp);
1536 		error = 0;
1537 		break;
1538 
1539 	case SIOCSIFMTU:
1540 		LAGG_XLOCK(sc);
1541 		CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1542 			if (lp->lp_ioctl != NULL)
1543 				error = (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1544 			else
1545 				error = EINVAL;
1546 			if (error != 0) {
1547 				if_printf(ifp,
1548 				    "failed to change MTU to %d on port %s, "
1549 				    "reverting all ports to original MTU (%d)\n",
1550 				    ifr->ifr_mtu, lp->lp_ifp->if_xname, ifp->if_mtu);
1551 				break;
1552 			}
1553 		}
1554 		if (error == 0) {
1555 			ifp->if_mtu = ifr->ifr_mtu;
1556 		} else {
1557 			/* set every port back to the original MTU */
1558 			ifr->ifr_mtu = ifp->if_mtu;
1559 			CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1560 				if (lp->lp_ioctl != NULL)
1561 					(*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1562 			}
1563 		}
1564 		LAGG_XUNLOCK(sc);
1565 		break;
1566 
1567 	default:
1568 		error = ether_ioctl(ifp, cmd, data);
1569 		break;
1570 	}
1571 	return (error);
1572 }
1573 
1574 #if defined(KERN_TLS) || defined(RATELIMIT)
1575 static inline struct lagg_snd_tag *
1576 mst_to_lst(struct m_snd_tag *mst)
1577 {
1578 
1579 	return (__containerof(mst, struct lagg_snd_tag, com));
1580 }
1581 
1582 /*
1583  * Look up the port used by a specific flow.  This only works for lagg
1584  * protocols with deterministic port mappings (e.g. not roundrobin).
1585  * In addition protocols which use a hash to map flows to ports must
1586  * be configured to use the mbuf flowid rather than hashing packet
1587  * contents.
1588  */
1589 static struct lagg_port *
1590 lookup_snd_tag_port(struct ifnet *ifp, uint32_t flowid, uint32_t flowtype)
1591 {
1592 	struct lagg_softc *sc;
1593 	struct lagg_port *lp;
1594 	struct lagg_lb *lb;
1595 	uint32_t p;
1596 
1597 	sc = ifp->if_softc;
1598 
1599 	switch (sc->sc_proto) {
1600 	case LAGG_PROTO_FAILOVER:
1601 		return (lagg_link_active(sc, sc->sc_primary));
1602 	case LAGG_PROTO_LOADBALANCE:
1603 		if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1604 		    flowtype == M_HASHTYPE_NONE)
1605 			return (NULL);
1606 		p = flowid >> sc->flowid_shift;
1607 		p %= sc->sc_count;
1608 		lb = (struct lagg_lb *)sc->sc_psc;
1609 		lp = lb->lb_ports[p];
1610 		return (lagg_link_active(sc, lp));
1611 	case LAGG_PROTO_LACP:
1612 		if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1613 		    flowtype == M_HASHTYPE_NONE)
1614 			return (NULL);
1615 		return (lacp_select_tx_port_by_hash(sc, flowid));
1616 	default:
1617 		return (NULL);
1618 	}
1619 }
1620 
1621 static int
1622 lagg_snd_tag_alloc(struct ifnet *ifp,
1623     union if_snd_tag_alloc_params *params,
1624     struct m_snd_tag **ppmt)
1625 {
1626 	struct lagg_snd_tag *lst;
1627 	struct lagg_softc *sc;
1628 	struct lagg_port *lp;
1629 	struct ifnet *lp_ifp;
1630 	int error;
1631 
1632 	sc = ifp->if_softc;
1633 
1634 	LAGG_RLOCK();
1635 	lp = lookup_snd_tag_port(ifp, params->hdr.flowid, params->hdr.flowtype);
1636 	if (lp == NULL) {
1637 		LAGG_RUNLOCK();
1638 		return (EOPNOTSUPP);
1639 	}
1640 	if (lp->lp_ifp == NULL || lp->lp_ifp->if_snd_tag_alloc == NULL) {
1641 		LAGG_RUNLOCK();
1642 		return (EOPNOTSUPP);
1643 	}
1644 	lp_ifp = lp->lp_ifp;
1645 	if_ref(lp_ifp);
1646 	LAGG_RUNLOCK();
1647 
1648 	lst = malloc(sizeof(*lst), M_LAGG, M_NOWAIT);
1649 	if (lst == NULL) {
1650 		if_rele(lp_ifp);
1651 		return (ENOMEM);
1652 	}
1653 
1654 	error = lp_ifp->if_snd_tag_alloc(lp_ifp, params, &lst->tag);
1655 	if_rele(lp_ifp);
1656 	if (error) {
1657 		free(lst, M_LAGG);
1658 		return (error);
1659 	}
1660 
1661 	m_snd_tag_init(&lst->com, ifp);
1662 
1663 	*ppmt = &lst->com;
1664 	return (0);
1665 }
1666 
1667 static int
1668 lagg_snd_tag_modify(struct m_snd_tag *mst,
1669     union if_snd_tag_modify_params *params)
1670 {
1671 	struct lagg_snd_tag *lst;
1672 
1673 	lst = mst_to_lst(mst);
1674 	return (lst->tag->ifp->if_snd_tag_modify(lst->tag, params));
1675 }
1676 
1677 static int
1678 lagg_snd_tag_query(struct m_snd_tag *mst,
1679     union if_snd_tag_query_params *params)
1680 {
1681 	struct lagg_snd_tag *lst;
1682 
1683 	lst = mst_to_lst(mst);
1684 	return (lst->tag->ifp->if_snd_tag_query(lst->tag, params));
1685 }
1686 
1687 static void
1688 lagg_snd_tag_free(struct m_snd_tag *mst)
1689 {
1690 	struct lagg_snd_tag *lst;
1691 
1692 	lst = mst_to_lst(mst);
1693 	m_snd_tag_rele(lst->tag);
1694 	free(lst, M_LAGG);
1695 }
1696 
1697 static void
1698 lagg_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q)
1699 {
1700 	/*
1701 	 * For lagg, we have an indirect
1702 	 * interface. The caller needs to
1703 	 * get a ratelimit tag on the actual
1704 	 * interface the flow will go on.
1705 	 */
1706 	q->rate_table = NULL;
1707 	q->flags = RT_IS_INDIRECT;
1708 	q->max_flows = 0;
1709 	q->number_of_rates = 0;
1710 }
1711 #endif
1712 
1713 static int
1714 lagg_setmulti(struct lagg_port *lp)
1715 {
1716 	struct lagg_softc *sc = lp->lp_softc;
1717 	struct ifnet *ifp = lp->lp_ifp;
1718 	struct ifnet *scifp = sc->sc_ifp;
1719 	struct lagg_mc *mc;
1720 	struct ifmultiaddr *ifma;
1721 	int error;
1722 
1723 	IF_ADDR_WLOCK(scifp);
1724 	CK_STAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1725 		if (ifma->ifma_addr->sa_family != AF_LINK)
1726 			continue;
1727 		mc = malloc(sizeof(struct lagg_mc), M_LAGG, M_NOWAIT);
1728 		if (mc == NULL) {
1729 			IF_ADDR_WUNLOCK(scifp);
1730 			return (ENOMEM);
1731 		}
1732 		bcopy(ifma->ifma_addr, &mc->mc_addr,
1733 		    ifma->ifma_addr->sa_len);
1734 		mc->mc_addr.sdl_index = ifp->if_index;
1735 		mc->mc_ifma = NULL;
1736 		SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1737 	}
1738 	IF_ADDR_WUNLOCK(scifp);
1739 	SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1740 		error = if_addmulti(ifp,
1741 		    (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1742 		if (error)
1743 			return (error);
1744 	}
1745 	return (0);
1746 }
1747 
1748 static int
1749 lagg_clrmulti(struct lagg_port *lp)
1750 {
1751 	struct lagg_mc *mc;
1752 
1753 	LAGG_XLOCK_ASSERT(lp->lp_softc);
1754 	while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1755 		SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1756 		if (mc->mc_ifma && lp->lp_detaching == 0)
1757 			if_delmulti_ifma(mc->mc_ifma);
1758 		free(mc, M_LAGG);
1759 	}
1760 	return (0);
1761 }
1762 
1763 static int
1764 lagg_setcaps(struct lagg_port *lp, int cap)
1765 {
1766 	struct ifreq ifr;
1767 
1768 	if (lp->lp_ifp->if_capenable == cap)
1769 		return (0);
1770 	if (lp->lp_ioctl == NULL)
1771 		return (ENXIO);
1772 	ifr.ifr_reqcap = cap;
1773 	return ((*lp->lp_ioctl)(lp->lp_ifp, SIOCSIFCAP, (caddr_t)&ifr));
1774 }
1775 
1776 /* Handle a ref counted flag that should be set on the lagg port as well */
1777 static int
1778 lagg_setflag(struct lagg_port *lp, int flag, int status,
1779     int (*func)(struct ifnet *, int))
1780 {
1781 	struct lagg_softc *sc = lp->lp_softc;
1782 	struct ifnet *scifp = sc->sc_ifp;
1783 	struct ifnet *ifp = lp->lp_ifp;
1784 	int error;
1785 
1786 	LAGG_XLOCK_ASSERT(sc);
1787 
1788 	status = status ? (scifp->if_flags & flag) : 0;
1789 	/* Now "status" contains the flag value or 0 */
1790 
1791 	/*
1792 	 * See if recorded ports status is different from what
1793 	 * we want it to be.  If it is, flip it.  We record ports
1794 	 * status in lp_ifflags so that we won't clear ports flag
1795 	 * we haven't set.  In fact, we don't clear or set ports
1796 	 * flags directly, but get or release references to them.
1797 	 * That's why we can be sure that recorded flags still are
1798 	 * in accord with actual ports flags.
1799 	 */
1800 	if (status != (lp->lp_ifflags & flag)) {
1801 		error = (*func)(ifp, status);
1802 		if (error)
1803 			return (error);
1804 		lp->lp_ifflags &= ~flag;
1805 		lp->lp_ifflags |= status;
1806 	}
1807 	return (0);
1808 }
1809 
1810 /*
1811  * Handle IFF_* flags that require certain changes on the lagg port
1812  * if "status" is true, update ports flags respective to the lagg
1813  * if "status" is false, forcedly clear the flags set on port.
1814  */
1815 static int
1816 lagg_setflags(struct lagg_port *lp, int status)
1817 {
1818 	int error, i;
1819 
1820 	for (i = 0; lagg_pflags[i].flag; i++) {
1821 		error = lagg_setflag(lp, lagg_pflags[i].flag,
1822 		    status, lagg_pflags[i].func);
1823 		if (error)
1824 			return (error);
1825 	}
1826 	return (0);
1827 }
1828 
1829 static int
1830 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1831 {
1832 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1833 	int error;
1834 
1835 #if defined(KERN_TLS) || defined(RATELIMIT)
1836 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
1837 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
1838 #endif
1839 	LAGG_RLOCK();
1840 	/* We need a Tx algorithm and at least one port */
1841 	if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1842 		LAGG_RUNLOCK();
1843 		m_freem(m);
1844 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1845 		return (ENXIO);
1846 	}
1847 
1848 	ETHER_BPF_MTAP(ifp, m);
1849 
1850 	error = lagg_proto_start(sc, m);
1851 	LAGG_RUNLOCK();
1852 
1853 	if (error != 0)
1854 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1855 
1856 	return (error);
1857 }
1858 
1859 /*
1860  * The ifp->if_qflush entry point for lagg(4) is no-op.
1861  */
1862 static void
1863 lagg_qflush(struct ifnet *ifp __unused)
1864 {
1865 }
1866 
1867 static struct mbuf *
1868 lagg_input(struct ifnet *ifp, struct mbuf *m)
1869 {
1870 	struct lagg_port *lp = ifp->if_lagg;
1871 	struct lagg_softc *sc = lp->lp_softc;
1872 	struct ifnet *scifp = sc->sc_ifp;
1873 
1874 	LAGG_RLOCK();
1875 	if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1876 	    lp->lp_detaching != 0 ||
1877 	    sc->sc_proto == LAGG_PROTO_NONE) {
1878 		LAGG_RUNLOCK();
1879 		m_freem(m);
1880 		return (NULL);
1881 	}
1882 
1883 	ETHER_BPF_MTAP(scifp, m);
1884 
1885 	m = lagg_proto_input(sc, lp, m);
1886 	if (m != NULL && (scifp->if_flags & IFF_MONITOR) != 0) {
1887 		m_freem(m);
1888 		m = NULL;
1889 	}
1890 
1891 	LAGG_RUNLOCK();
1892 	return (m);
1893 }
1894 
1895 static int
1896 lagg_media_change(struct ifnet *ifp)
1897 {
1898 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1899 
1900 	if (sc->sc_ifflags & IFF_DEBUG)
1901 		printf("%s\n", __func__);
1902 
1903 	/* Ignore */
1904 	return (0);
1905 }
1906 
1907 static void
1908 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1909 {
1910 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1911 	struct lagg_port *lp;
1912 
1913 	imr->ifm_status = IFM_AVALID;
1914 	imr->ifm_active = IFM_ETHER | IFM_AUTO;
1915 
1916 	LAGG_RLOCK();
1917 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1918 		if (LAGG_PORTACTIVE(lp))
1919 			imr->ifm_status |= IFM_ACTIVE;
1920 	}
1921 	LAGG_RUNLOCK();
1922 }
1923 
1924 static void
1925 lagg_linkstate(struct lagg_softc *sc)
1926 {
1927 	struct lagg_port *lp;
1928 	int new_link = LINK_STATE_DOWN;
1929 	uint64_t speed;
1930 
1931 	LAGG_XLOCK_ASSERT(sc);
1932 
1933 	/* LACP handles link state itself */
1934 	if (sc->sc_proto == LAGG_PROTO_LACP)
1935 		return;
1936 
1937 	/* Our link is considered up if at least one of our ports is active */
1938 	LAGG_RLOCK();
1939 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1940 		if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
1941 			new_link = LINK_STATE_UP;
1942 			break;
1943 		}
1944 	}
1945 	LAGG_RUNLOCK();
1946 	if_link_state_change(sc->sc_ifp, new_link);
1947 
1948 	/* Update if_baudrate to reflect the max possible speed */
1949 	switch (sc->sc_proto) {
1950 		case LAGG_PROTO_FAILOVER:
1951 			sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1952 			    sc->sc_primary->lp_ifp->if_baudrate : 0;
1953 			break;
1954 		case LAGG_PROTO_ROUNDROBIN:
1955 		case LAGG_PROTO_LOADBALANCE:
1956 		case LAGG_PROTO_BROADCAST:
1957 			speed = 0;
1958 			LAGG_RLOCK();
1959 			CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1960 				speed += lp->lp_ifp->if_baudrate;
1961 			LAGG_RUNLOCK();
1962 			sc->sc_ifp->if_baudrate = speed;
1963 			break;
1964 		case LAGG_PROTO_LACP:
1965 			/* LACP updates if_baudrate itself */
1966 			break;
1967 	}
1968 }
1969 
1970 static void
1971 lagg_port_state(struct ifnet *ifp, int state)
1972 {
1973 	struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1974 	struct lagg_softc *sc = NULL;
1975 
1976 	if (lp != NULL)
1977 		sc = lp->lp_softc;
1978 	if (sc == NULL)
1979 		return;
1980 
1981 	LAGG_XLOCK(sc);
1982 	lagg_linkstate(sc);
1983 	lagg_proto_linkstate(sc, lp);
1984 	LAGG_XUNLOCK(sc);
1985 }
1986 
1987 struct lagg_port *
1988 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1989 {
1990 	struct lagg_port *lp_next, *rval = NULL;
1991 
1992 	/*
1993 	 * Search a port which reports an active link state.
1994 	 */
1995 
1996 #ifdef INVARIANTS
1997 	/*
1998 	 * This is called with either LAGG_RLOCK() held or
1999 	 * LAGG_XLOCK(sc) held.
2000 	 */
2001 	if (!in_epoch(net_epoch_preempt))
2002 		LAGG_XLOCK_ASSERT(sc);
2003 #endif
2004 
2005 	if (lp == NULL)
2006 		goto search;
2007 	if (LAGG_PORTACTIVE(lp)) {
2008 		rval = lp;
2009 		goto found;
2010 	}
2011 	if ((lp_next = CK_SLIST_NEXT(lp, lp_entries)) != NULL &&
2012 	    LAGG_PORTACTIVE(lp_next)) {
2013 		rval = lp_next;
2014 		goto found;
2015 	}
2016 
2017 search:
2018 	CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2019 		if (LAGG_PORTACTIVE(lp_next)) {
2020 			return (lp_next);
2021 		}
2022 	}
2023 found:
2024 	return (rval);
2025 }
2026 
2027 int
2028 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
2029 {
2030 
2031 #if defined(KERN_TLS) || defined(RATELIMIT)
2032 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
2033 		struct lagg_snd_tag *lst;
2034 		struct m_snd_tag *mst;
2035 
2036 		mst = m->m_pkthdr.snd_tag;
2037 		lst = mst_to_lst(mst);
2038 		if (lst->tag->ifp != ifp) {
2039 			m_freem(m);
2040 			return (EAGAIN);
2041 		}
2042 		m->m_pkthdr.snd_tag = m_snd_tag_ref(lst->tag);
2043 		m_snd_tag_rele(mst);
2044 	}
2045 #endif
2046 	return (ifp->if_transmit)(ifp, m);
2047 }
2048 
2049 /*
2050  * Simple round robin aggregation
2051  */
2052 static void
2053 lagg_rr_attach(struct lagg_softc *sc)
2054 {
2055 	sc->sc_seq = 0;
2056 	sc->sc_stride = 1;
2057 }
2058 
2059 static int
2060 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
2061 {
2062 	struct lagg_port *lp;
2063 	uint32_t p;
2064 
2065 	p = atomic_fetchadd_32(&sc->sc_seq, 1);
2066 	p /= sc->sc_stride;
2067 	p %= sc->sc_count;
2068 	lp = CK_SLIST_FIRST(&sc->sc_ports);
2069 
2070 	while (p--)
2071 		lp = CK_SLIST_NEXT(lp, lp_entries);
2072 
2073 	/*
2074 	 * Check the port's link state. This will return the next active
2075 	 * port if the link is down or the port is NULL.
2076 	 */
2077 	if ((lp = lagg_link_active(sc, lp)) == NULL) {
2078 		m_freem(m);
2079 		return (ENETDOWN);
2080 	}
2081 
2082 	/* Send mbuf */
2083 	return (lagg_enqueue(lp->lp_ifp, m));
2084 }
2085 
2086 static struct mbuf *
2087 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2088 {
2089 	struct ifnet *ifp = sc->sc_ifp;
2090 
2091 	/* Just pass in the packet to our lagg device */
2092 	m->m_pkthdr.rcvif = ifp;
2093 
2094 	return (m);
2095 }
2096 
2097 /*
2098  * Broadcast mode
2099  */
2100 static int
2101 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
2102 {
2103 	int active_ports = 0;
2104 	int errors = 0;
2105 	int ret;
2106 	struct lagg_port *lp, *last = NULL;
2107 	struct mbuf *m0;
2108 
2109 	LAGG_RLOCK_ASSERT();
2110 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2111 		if (!LAGG_PORTACTIVE(lp))
2112 			continue;
2113 
2114 		active_ports++;
2115 
2116 		if (last != NULL) {
2117 			m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
2118 			if (m0 == NULL) {
2119 				ret = ENOBUFS;
2120 				errors++;
2121 				break;
2122 			}
2123 
2124 			ret = lagg_enqueue(last->lp_ifp, m0);
2125 			if (ret != 0)
2126 				errors++;
2127 		}
2128 		last = lp;
2129 	}
2130 
2131 	if (last == NULL) {
2132 		m_freem(m);
2133 		return (ENOENT);
2134 	}
2135 	if ((last = lagg_link_active(sc, last)) == NULL) {
2136 		m_freem(m);
2137 		return (ENETDOWN);
2138 	}
2139 
2140 	ret = lagg_enqueue(last->lp_ifp, m);
2141 	if (ret != 0)
2142 		errors++;
2143 
2144 	if (errors == 0)
2145 		return (ret);
2146 
2147 	return (0);
2148 }
2149 
2150 static struct mbuf*
2151 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2152 {
2153 	struct ifnet *ifp = sc->sc_ifp;
2154 
2155 	/* Just pass in the packet to our lagg device */
2156 	m->m_pkthdr.rcvif = ifp;
2157 	return (m);
2158 }
2159 
2160 /*
2161  * Active failover
2162  */
2163 static int
2164 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
2165 {
2166 	struct lagg_port *lp;
2167 
2168 	/* Use the master port if active or the next available port */
2169 	if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
2170 		m_freem(m);
2171 		return (ENETDOWN);
2172 	}
2173 
2174 	/* Send mbuf */
2175 	return (lagg_enqueue(lp->lp_ifp, m));
2176 }
2177 
2178 static struct mbuf *
2179 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2180 {
2181 	struct ifnet *ifp = sc->sc_ifp;
2182 	struct lagg_port *tmp_tp;
2183 
2184 	if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
2185 		m->m_pkthdr.rcvif = ifp;
2186 		return (m);
2187 	}
2188 
2189 	if (!LAGG_PORTACTIVE(sc->sc_primary)) {
2190 		tmp_tp = lagg_link_active(sc, sc->sc_primary);
2191 		/*
2192 		 * If tmp_tp is null, we've received a packet when all
2193 		 * our links are down. Weird, but process it anyways.
2194 		 */
2195 		if ((tmp_tp == NULL || tmp_tp == lp)) {
2196 			m->m_pkthdr.rcvif = ifp;
2197 			return (m);
2198 		}
2199 	}
2200 
2201 	m_freem(m);
2202 	return (NULL);
2203 }
2204 
2205 /*
2206  * Loadbalancing
2207  */
2208 static void
2209 lagg_lb_attach(struct lagg_softc *sc)
2210 {
2211 	struct lagg_port *lp;
2212 	struct lagg_lb *lb;
2213 
2214 	LAGG_XLOCK_ASSERT(sc);
2215 	lb = malloc(sizeof(struct lagg_lb), M_LAGG, M_WAITOK | M_ZERO);
2216 	lb->lb_key = m_ether_tcpip_hash_init();
2217 	sc->sc_psc = lb;
2218 
2219 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2220 		lagg_lb_port_create(lp);
2221 }
2222 
2223 static void
2224 lagg_lb_detach(struct lagg_softc *sc)
2225 {
2226 	struct lagg_lb *lb;
2227 
2228 	lb = (struct lagg_lb *)sc->sc_psc;
2229 	if (lb != NULL)
2230 		free(lb, M_LAGG);
2231 }
2232 
2233 static int
2234 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2235 {
2236 	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2237 	struct lagg_port *lp_next;
2238 	int i = 0, rv;
2239 
2240 	rv = 0;
2241 	bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2242 	LAGG_XLOCK_ASSERT(sc);
2243 	CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2244 		if (lp_next == lp)
2245 			continue;
2246 		if (i >= LAGG_MAX_PORTS) {
2247 			rv = EINVAL;
2248 			break;
2249 		}
2250 		if (sc->sc_ifflags & IFF_DEBUG)
2251 			printf("%s: port %s at index %d\n",
2252 			    sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2253 		lb->lb_ports[i++] = lp_next;
2254 	}
2255 
2256 	return (rv);
2257 }
2258 
2259 static int
2260 lagg_lb_port_create(struct lagg_port *lp)
2261 {
2262 	struct lagg_softc *sc = lp->lp_softc;
2263 	return (lagg_lb_porttable(sc, NULL));
2264 }
2265 
2266 static void
2267 lagg_lb_port_destroy(struct lagg_port *lp)
2268 {
2269 	struct lagg_softc *sc = lp->lp_softc;
2270 	lagg_lb_porttable(sc, lp);
2271 }
2272 
2273 static int
2274 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2275 {
2276 	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2277 	struct lagg_port *lp = NULL;
2278 	uint32_t p = 0;
2279 
2280 	if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
2281 	    M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2282 		p = m->m_pkthdr.flowid >> sc->flowid_shift;
2283 	else
2284 		p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key);
2285 	p %= sc->sc_count;
2286 	lp = lb->lb_ports[p];
2287 
2288 	/*
2289 	 * Check the port's link state. This will return the next active
2290 	 * port if the link is down or the port is NULL.
2291 	 */
2292 	if ((lp = lagg_link_active(sc, lp)) == NULL) {
2293 		m_freem(m);
2294 		return (ENETDOWN);
2295 	}
2296 
2297 	/* Send mbuf */
2298 	return (lagg_enqueue(lp->lp_ifp, m));
2299 }
2300 
2301 static struct mbuf *
2302 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2303 {
2304 	struct ifnet *ifp = sc->sc_ifp;
2305 
2306 	/* Just pass in the packet to our lagg device */
2307 	m->m_pkthdr.rcvif = ifp;
2308 
2309 	return (m);
2310 }
2311 
2312 /*
2313  * 802.3ad LACP
2314  */
2315 static void
2316 lagg_lacp_attach(struct lagg_softc *sc)
2317 {
2318 	struct lagg_port *lp;
2319 
2320 	lacp_attach(sc);
2321 	LAGG_XLOCK_ASSERT(sc);
2322 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2323 		lacp_port_create(lp);
2324 }
2325 
2326 static void
2327 lagg_lacp_detach(struct lagg_softc *sc)
2328 {
2329 	struct lagg_port *lp;
2330 	void *psc;
2331 
2332 	LAGG_XLOCK_ASSERT(sc);
2333 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2334 		lacp_port_destroy(lp);
2335 
2336 	psc = sc->sc_psc;
2337 	sc->sc_psc = NULL;
2338 	lacp_detach(psc);
2339 }
2340 
2341 static void
2342 lagg_lacp_lladdr(struct lagg_softc *sc)
2343 {
2344 	struct lagg_port *lp;
2345 
2346 	LAGG_SXLOCK_ASSERT(sc);
2347 
2348 	/* purge all the lacp ports */
2349 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2350 		lacp_port_destroy(lp);
2351 
2352 	/* add them back in */
2353 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2354 		lacp_port_create(lp);
2355 }
2356 
2357 static int
2358 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2359 {
2360 	struct lagg_port *lp;
2361 
2362 	lp = lacp_select_tx_port(sc, m);
2363 	if (lp == NULL) {
2364 		m_freem(m);
2365 		return (ENETDOWN);
2366 	}
2367 
2368 	/* Send mbuf */
2369 	return (lagg_enqueue(lp->lp_ifp, m));
2370 }
2371 
2372 static struct mbuf *
2373 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2374 {
2375 	struct ifnet *ifp = sc->sc_ifp;
2376 	struct ether_header *eh;
2377 	u_short etype;
2378 
2379 	eh = mtod(m, struct ether_header *);
2380 	etype = ntohs(eh->ether_type);
2381 
2382 	/* Tap off LACP control messages */
2383 	if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2384 		m = lacp_input(lp, m);
2385 		if (m == NULL)
2386 			return (NULL);
2387 	}
2388 
2389 	/*
2390 	 * If the port is not collecting or not in the active aggregator then
2391 	 * free and return.
2392 	 */
2393 	if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2394 		m_freem(m);
2395 		return (NULL);
2396 	}
2397 
2398 	m->m_pkthdr.rcvif = ifp;
2399 	return (m);
2400 }
2401 
2402