xref: /freebsd/sys/net/if_lagg.c (revision 3e0efd2ec4fcb4cd68fb8ccf8aea6fc6151c454b)
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  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
22 
23 #include "opt_inet.h"
24 #include "opt_inet6.h"
25 
26 #include <sys/param.h>
27 #include <sys/kernel.h>
28 #include <sys/malloc.h>
29 #include <sys/mbuf.h>
30 #include <sys/queue.h>
31 #include <sys/socket.h>
32 #include <sys/sockio.h>
33 #include <sys/sysctl.h>
34 #include <sys/module.h>
35 #include <sys/priv.h>
36 #include <sys/systm.h>
37 #include <sys/proc.h>
38 #include <sys/hash.h>
39 #include <sys/lock.h>
40 #include <sys/rwlock.h>
41 #include <sys/taskqueue.h>
42 #include <sys/eventhandler.h>
43 
44 #include <net/ethernet.h>
45 #include <net/if.h>
46 #include <net/if_clone.h>
47 #include <net/if_arp.h>
48 #include <net/if_dl.h>
49 #include <net/if_llc.h>
50 #include <net/if_media.h>
51 #include <net/if_types.h>
52 #include <net/if_var.h>
53 #include <net/bpf.h>
54 
55 #if defined(INET) || defined(INET6)
56 #include <netinet/in.h>
57 #endif
58 #ifdef INET
59 #include <netinet/in_systm.h>
60 #include <netinet/if_ether.h>
61 #include <netinet/ip.h>
62 #endif
63 
64 #ifdef INET6
65 #include <netinet/ip6.h>
66 #endif
67 
68 #include <net/if_vlan_var.h>
69 #include <net/if_lagg.h>
70 #include <net/ieee8023ad_lacp.h>
71 
72 /* Special flags we should propagate to the lagg ports. */
73 static struct {
74 	int flag;
75 	int (*func)(struct ifnet *, int);
76 } lagg_pflags[] = {
77 	{IFF_PROMISC, ifpromisc},
78 	{IFF_ALLMULTI, if_allmulti},
79 	{0, NULL}
80 };
81 
82 SLIST_HEAD(__trhead, lagg_softc) lagg_list;	/* list of laggs */
83 static struct mtx	lagg_list_mtx;
84 eventhandler_tag	lagg_detach_cookie = NULL;
85 
86 static int	lagg_clone_create(struct if_clone *, int, caddr_t);
87 static void	lagg_clone_destroy(struct ifnet *);
88 static void	lagg_lladdr(struct lagg_softc *, uint8_t *);
89 static void	lagg_capabilities(struct lagg_softc *);
90 static void	lagg_port_lladdr(struct lagg_port *, uint8_t *);
91 static void	lagg_port_setlladdr(void *, int);
92 static int	lagg_port_create(struct lagg_softc *, struct ifnet *);
93 static int	lagg_port_destroy(struct lagg_port *, int);
94 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
95 static void	lagg_linkstate(struct lagg_softc *);
96 static void	lagg_port_state(struct ifnet *, int);
97 static int	lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
98 static int	lagg_port_output(struct ifnet *, struct mbuf *,
99 		    struct sockaddr *, struct route *);
100 static void	lagg_port_ifdetach(void *arg __unused, struct ifnet *);
101 #ifdef LAGG_PORT_STACKING
102 static int	lagg_port_checkstacking(struct lagg_softc *);
103 #endif
104 static void	lagg_port2req(struct lagg_port *, struct lagg_reqport *);
105 static void	lagg_init(void *);
106 static void	lagg_stop(struct lagg_softc *);
107 static int	lagg_ioctl(struct ifnet *, u_long, caddr_t);
108 static int	lagg_ether_setmulti(struct lagg_softc *);
109 static int	lagg_ether_cmdmulti(struct lagg_port *, int);
110 static	int	lagg_setflag(struct lagg_port *, int, int,
111 		    int (*func)(struct ifnet *, int));
112 static	int	lagg_setflags(struct lagg_port *, int status);
113 static void	lagg_start(struct ifnet *);
114 static int	lagg_media_change(struct ifnet *);
115 static void	lagg_media_status(struct ifnet *, struct ifmediareq *);
116 static struct lagg_port *lagg_link_active(struct lagg_softc *,
117 	    struct lagg_port *);
118 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
119 
120 IFC_SIMPLE_DECLARE(lagg, 0);
121 
122 /* Simple round robin */
123 static int	lagg_rr_attach(struct lagg_softc *);
124 static int	lagg_rr_detach(struct lagg_softc *);
125 static int	lagg_rr_start(struct lagg_softc *, struct mbuf *);
126 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
127 		    struct mbuf *);
128 
129 /* Active failover */
130 static int	lagg_fail_attach(struct lagg_softc *);
131 static int	lagg_fail_detach(struct lagg_softc *);
132 static int	lagg_fail_start(struct lagg_softc *, struct mbuf *);
133 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
134 		    struct mbuf *);
135 
136 /* Loadbalancing */
137 static int	lagg_lb_attach(struct lagg_softc *);
138 static int	lagg_lb_detach(struct lagg_softc *);
139 static int	lagg_lb_port_create(struct lagg_port *);
140 static void	lagg_lb_port_destroy(struct lagg_port *);
141 static int	lagg_lb_start(struct lagg_softc *, struct mbuf *);
142 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
143 		    struct mbuf *);
144 static int	lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
145 
146 /* 802.3ad LACP */
147 static int	lagg_lacp_attach(struct lagg_softc *);
148 static int	lagg_lacp_detach(struct lagg_softc *);
149 static int	lagg_lacp_start(struct lagg_softc *, struct mbuf *);
150 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
151 		    struct mbuf *);
152 static void	lagg_lacp_lladdr(struct lagg_softc *);
153 
154 /* lagg protocol table */
155 static const struct {
156 	int			ti_proto;
157 	int			(*ti_attach)(struct lagg_softc *);
158 } lagg_protos[] = {
159 	{ LAGG_PROTO_ROUNDROBIN,	lagg_rr_attach },
160 	{ LAGG_PROTO_FAILOVER,		lagg_fail_attach },
161 	{ LAGG_PROTO_LOADBALANCE,	lagg_lb_attach },
162 	{ LAGG_PROTO_ETHERCHANNEL,	lagg_lb_attach },
163 	{ LAGG_PROTO_LACP,		lagg_lacp_attach },
164 	{ LAGG_PROTO_NONE,		NULL }
165 };
166 
167 SYSCTL_DECL(_net_link);
168 static SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
169     "Link Aggregation");
170 
171 static int lagg_failover_rx_all = 0; /* Allow input on any failover links */
172 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW,
173     &lagg_failover_rx_all, 0,
174     "Accept input from any interface in a failover lagg");
175 static int def_use_flowid = 1; /* Default value for using M_FLOWID */
176 TUNABLE_INT("net.link.lagg.default_use_flowid", &def_use_flowid);
177 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RW,
178     &def_use_flowid, 0,
179     "Default setting for using flow id for load sharing");
180 
181 static int
182 lagg_modevent(module_t mod, int type, void *data)
183 {
184 
185 	switch (type) {
186 	case MOD_LOAD:
187 		mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
188 		SLIST_INIT(&lagg_list);
189 		if_clone_attach(&lagg_cloner);
190 		lagg_input_p = lagg_input;
191 		lagg_linkstate_p = lagg_port_state;
192 		lagg_detach_cookie = EVENTHANDLER_REGISTER(
193 		    ifnet_departure_event, lagg_port_ifdetach, NULL,
194 		    EVENTHANDLER_PRI_ANY);
195 		break;
196 	case MOD_UNLOAD:
197 		EVENTHANDLER_DEREGISTER(ifnet_departure_event,
198 		    lagg_detach_cookie);
199 		if_clone_detach(&lagg_cloner);
200 		lagg_input_p = NULL;
201 		lagg_linkstate_p = NULL;
202 		mtx_destroy(&lagg_list_mtx);
203 		break;
204 	default:
205 		return (EOPNOTSUPP);
206 	}
207 	return (0);
208 }
209 
210 static moduledata_t lagg_mod = {
211 	"if_lagg",
212 	lagg_modevent,
213 	0
214 };
215 
216 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
217 MODULE_VERSION(if_lagg, 1);
218 
219 #if __FreeBSD_version >= 800000
220 /*
221  * This routine is run via an vlan
222  * config EVENT
223  */
224 static void
225 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
226 {
227         struct lagg_softc       *sc = ifp->if_softc;
228         struct lagg_port        *lp;
229 
230         if (ifp->if_softc !=  arg)   /* Not our event */
231                 return;
232 
233         LAGG_RLOCK(sc);
234         if (!SLIST_EMPTY(&sc->sc_ports)) {
235                 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
236                         EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
237         }
238         LAGG_RUNLOCK(sc);
239 }
240 
241 /*
242  * This routine is run via an vlan
243  * unconfig EVENT
244  */
245 static void
246 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
247 {
248         struct lagg_softc       *sc = ifp->if_softc;
249         struct lagg_port        *lp;
250 
251         if (ifp->if_softc !=  arg)   /* Not our event */
252                 return;
253 
254         LAGG_RLOCK(sc);
255         if (!SLIST_EMPTY(&sc->sc_ports)) {
256                 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
257                         EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
258         }
259         LAGG_RUNLOCK(sc);
260 }
261 #endif
262 
263 static int
264 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
265 {
266 	struct lagg_softc *sc;
267 	struct ifnet *ifp;
268 	int i, error = 0;
269 	static const u_char eaddr[6];	/* 00:00:00:00:00:00 */
270 	struct sysctl_oid *oid;
271 	char num[14];			/* sufficient for 32 bits */
272 
273 	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
274 	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
275 	if (ifp == NULL) {
276 		free(sc, M_DEVBUF);
277 		return (ENOSPC);
278 	}
279 
280 	sysctl_ctx_init(&sc->ctx);
281 	snprintf(num, sizeof(num), "%u", unit);
282 	sc->use_flowid = def_use_flowid;
283 	oid = SYSCTL_ADD_NODE(&sc->ctx, &SYSCTL_NODE_CHILDREN(_net_link, lagg),
284 		OID_AUTO, num, CTLFLAG_RD, NULL, "");
285 	SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
286 		"use_flowid", CTLTYPE_INT|CTLFLAG_RW, &sc->use_flowid, sc->use_flowid,
287 		"Use flow id for load sharing");
288 	/* Hash all layers by default */
289 	sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
290 
291 	sc->sc_proto = LAGG_PROTO_NONE;
292 	for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
293 		if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
294 			sc->sc_proto = lagg_protos[i].ti_proto;
295 			if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
296 				if_free(ifp);
297 				free(sc, M_DEVBUF);
298 				return (error);
299 			}
300 			break;
301 		}
302 	}
303 	LAGG_LOCK_INIT(sc);
304 	SLIST_INIT(&sc->sc_ports);
305 	TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
306 
307 	/* Initialise pseudo media types */
308 	ifmedia_init(&sc->sc_media, 0, lagg_media_change,
309 	    lagg_media_status);
310 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
311 	ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
312 
313 	if_initname(ifp, ifc->ifc_name, unit);
314 	ifp->if_softc = sc;
315 	ifp->if_start = lagg_start;
316 	ifp->if_init = lagg_init;
317 	ifp->if_ioctl = lagg_ioctl;
318 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
319 
320 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
321 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
322 	IFQ_SET_READY(&ifp->if_snd);
323 
324 	/*
325 	 * Attach as an ordinary ethernet device, children will be attached
326 	 * as special device IFT_IEEE8023ADLAG.
327 	 */
328 	ether_ifattach(ifp, eaddr);
329 
330 #if __FreeBSD_version >= 800000
331 	sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
332 		lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
333 	sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
334 		lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
335 #endif
336 
337 	/* Insert into the global list of laggs */
338 	mtx_lock(&lagg_list_mtx);
339 	SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
340 	mtx_unlock(&lagg_list_mtx);
341 
342 	return (0);
343 }
344 
345 static void
346 lagg_clone_destroy(struct ifnet *ifp)
347 {
348 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
349 	struct lagg_port *lp;
350 
351 	LAGG_WLOCK(sc);
352 
353 	lagg_stop(sc);
354 	ifp->if_flags &= ~IFF_UP;
355 
356 #if __FreeBSD_version >= 800000
357 	EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
358 	EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
359 #endif
360 
361 	/* Shutdown and remove lagg ports */
362 	while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
363 		lagg_port_destroy(lp, 1);
364 	/* Unhook the aggregation protocol */
365 	if (sc->sc_detach != NULL)
366 		(*sc->sc_detach)(sc);
367 
368 	LAGG_WUNLOCK(sc);
369 
370 	sysctl_ctx_free(&sc->ctx);
371 	ifmedia_removeall(&sc->sc_media);
372 	ether_ifdetach(ifp);
373 	if_free(ifp);
374 
375 	mtx_lock(&lagg_list_mtx);
376 	SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
377 	mtx_unlock(&lagg_list_mtx);
378 
379 	taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
380 	LAGG_LOCK_DESTROY(sc);
381 	free(sc, M_DEVBUF);
382 }
383 
384 static void
385 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
386 {
387 	struct ifnet *ifp = sc->sc_ifp;
388 
389 	if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
390 		return;
391 
392 	bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
393 	/* Let the protocol know the MAC has changed */
394 	if (sc->sc_lladdr != NULL)
395 		(*sc->sc_lladdr)(sc);
396 	EVENTHANDLER_INVOKE(iflladdr_event, ifp);
397 }
398 
399 static void
400 lagg_capabilities(struct lagg_softc *sc)
401 {
402 	struct lagg_port *lp;
403 	int cap = ~0, ena = ~0;
404 	u_long hwa = ~0UL;
405 
406 	LAGG_WLOCK_ASSERT(sc);
407 
408 	/* Get capabilities from the lagg ports */
409 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
410 		cap &= lp->lp_ifp->if_capabilities;
411 		ena &= lp->lp_ifp->if_capenable;
412 		hwa &= lp->lp_ifp->if_hwassist;
413 	}
414 	cap = (cap == ~0 ? 0 : cap);
415 	ena = (ena == ~0 ? 0 : ena);
416 	hwa = (hwa == ~0 ? 0 : hwa);
417 
418 	if (sc->sc_ifp->if_capabilities != cap ||
419 	    sc->sc_ifp->if_capenable != ena ||
420 	    sc->sc_ifp->if_hwassist != hwa) {
421 		sc->sc_ifp->if_capabilities = cap;
422 		sc->sc_ifp->if_capenable = ena;
423 		sc->sc_ifp->if_hwassist = hwa;
424 		getmicrotime(&sc->sc_ifp->if_lastchange);
425 
426 		if (sc->sc_ifflags & IFF_DEBUG)
427 			if_printf(sc->sc_ifp,
428 			    "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
429 	}
430 }
431 
432 static void
433 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
434 {
435 	struct lagg_softc *sc = lp->lp_softc;
436 	struct ifnet *ifp = lp->lp_ifp;
437 	struct lagg_llq *llq;
438 	int pending = 0;
439 
440 	LAGG_WLOCK_ASSERT(sc);
441 
442 	if (lp->lp_detaching ||
443 	    memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
444 		return;
445 
446 	/* Check to make sure its not already queued to be changed */
447 	SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
448 		if (llq->llq_ifp == ifp) {
449 			pending = 1;
450 			break;
451 		}
452 	}
453 
454 	if (!pending) {
455 		llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
456 		if (llq == NULL)	/* XXX what to do */
457 			return;
458 	}
459 
460 	/* Update the lladdr even if pending, it may have changed */
461 	llq->llq_ifp = ifp;
462 	bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
463 
464 	if (!pending)
465 		SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
466 
467 	taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
468 }
469 
470 /*
471  * Set the interface MAC address from a taskqueue to avoid a LOR.
472  */
473 static void
474 lagg_port_setlladdr(void *arg, int pending)
475 {
476 	struct lagg_softc *sc = (struct lagg_softc *)arg;
477 	struct lagg_llq *llq, *head;
478 	struct ifnet *ifp;
479 	int error;
480 
481 	/* Grab a local reference of the queue and remove it from the softc */
482 	LAGG_WLOCK(sc);
483 	head = SLIST_FIRST(&sc->sc_llq_head);
484 	SLIST_FIRST(&sc->sc_llq_head) = NULL;
485 	LAGG_WUNLOCK(sc);
486 
487 	/*
488 	 * Traverse the queue and set the lladdr on each ifp. It is safe to do
489 	 * unlocked as we have the only reference to it.
490 	 */
491 	for (llq = head; llq != NULL; llq = head) {
492 		ifp = llq->llq_ifp;
493 
494 		/* Set the link layer address */
495 		error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
496 		if (error)
497 			printf("%s: setlladdr failed on %s\n", __func__,
498 			    ifp->if_xname);
499 
500 		head = SLIST_NEXT(llq, llq_entries);
501 		free(llq, M_DEVBUF);
502 	}
503 }
504 
505 static int
506 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
507 {
508 	struct lagg_softc *sc_ptr;
509 	struct lagg_port *lp;
510 	int error = 0;
511 
512 	LAGG_WLOCK_ASSERT(sc);
513 
514 	/* Limit the maximal number of lagg ports */
515 	if (sc->sc_count >= LAGG_MAX_PORTS)
516 		return (ENOSPC);
517 
518 	/* Check if port has already been associated to a lagg */
519 	if (ifp->if_lagg != NULL) {
520 		/* Port is already in the current lagg? */
521 		lp = (struct lagg_port *)ifp->if_lagg;
522 		if (lp->lp_softc == sc)
523 			return (EEXIST);
524 		return (EBUSY);
525 	}
526 
527 	/* XXX Disallow non-ethernet interfaces (this should be any of 802) */
528 	if (ifp->if_type != IFT_ETHER)
529 		return (EPROTONOSUPPORT);
530 
531 	/* Allow the first Ethernet member to define the MTU */
532 	if (SLIST_EMPTY(&sc->sc_ports))
533 		sc->sc_ifp->if_mtu = ifp->if_mtu;
534 	else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
535 		if_printf(sc->sc_ifp, "invalid MTU for %s\n",
536 		    ifp->if_xname);
537 		return (EINVAL);
538 	}
539 
540 	if ((lp = malloc(sizeof(struct lagg_port),
541 	    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
542 		return (ENOMEM);
543 
544 	/* Check if port is a stacked lagg */
545 	mtx_lock(&lagg_list_mtx);
546 	SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
547 		if (ifp == sc_ptr->sc_ifp) {
548 			mtx_unlock(&lagg_list_mtx);
549 			free(lp, M_DEVBUF);
550 			return (EINVAL);
551 			/* XXX disable stacking for the moment, its untested */
552 #ifdef LAGG_PORT_STACKING
553 			lp->lp_flags |= LAGG_PORT_STACK;
554 			if (lagg_port_checkstacking(sc_ptr) >=
555 			    LAGG_MAX_STACKING) {
556 				mtx_unlock(&lagg_list_mtx);
557 				free(lp, M_DEVBUF);
558 				return (E2BIG);
559 			}
560 #endif
561 		}
562 	}
563 	mtx_unlock(&lagg_list_mtx);
564 
565 	/* Change the interface type */
566 	lp->lp_iftype = ifp->if_type;
567 	ifp->if_type = IFT_IEEE8023ADLAG;
568 	ifp->if_lagg = lp;
569 	lp->lp_ioctl = ifp->if_ioctl;
570 	ifp->if_ioctl = lagg_port_ioctl;
571 	lp->lp_output = ifp->if_output;
572 	ifp->if_output = lagg_port_output;
573 
574 	lp->lp_ifp = ifp;
575 	lp->lp_softc = sc;
576 
577 	/* Save port link layer address */
578 	bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
579 
580 	if (SLIST_EMPTY(&sc->sc_ports)) {
581 		sc->sc_primary = lp;
582 		lagg_lladdr(sc, IF_LLADDR(ifp));
583 	} else {
584 		/* Update link layer address for this port */
585 		lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
586 	}
587 
588 	/* Insert into the list of ports */
589 	SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
590 	sc->sc_count++;
591 
592 	/* Update lagg capabilities */
593 	lagg_capabilities(sc);
594 	lagg_linkstate(sc);
595 
596 	/* Add multicast addresses and interface flags to this port */
597 	lagg_ether_cmdmulti(lp, 1);
598 	lagg_setflags(lp, 1);
599 
600 	if (sc->sc_port_create != NULL)
601 		error = (*sc->sc_port_create)(lp);
602 	if (error) {
603 		/* remove the port again, without calling sc_port_destroy */
604 		lagg_port_destroy(lp, 0);
605 		return (error);
606 	}
607 
608 	return (error);
609 }
610 
611 #ifdef LAGG_PORT_STACKING
612 static int
613 lagg_port_checkstacking(struct lagg_softc *sc)
614 {
615 	struct lagg_softc *sc_ptr;
616 	struct lagg_port *lp;
617 	int m = 0;
618 
619 	LAGG_WLOCK_ASSERT(sc);
620 
621 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
622 		if (lp->lp_flags & LAGG_PORT_STACK) {
623 			sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
624 			m = MAX(m, lagg_port_checkstacking(sc_ptr));
625 		}
626 	}
627 
628 	return (m + 1);
629 }
630 #endif
631 
632 static int
633 lagg_port_destroy(struct lagg_port *lp, int runpd)
634 {
635 	struct lagg_softc *sc = lp->lp_softc;
636 	struct lagg_port *lp_ptr;
637 	struct lagg_llq *llq;
638 	struct ifnet *ifp = lp->lp_ifp;
639 
640 	LAGG_WLOCK_ASSERT(sc);
641 
642 	if (runpd && sc->sc_port_destroy != NULL)
643 		(*sc->sc_port_destroy)(lp);
644 
645 	/*
646 	 * Remove multicast addresses and interface flags from this port and
647 	 * reset the MAC address, skip if the interface is being detached.
648 	 */
649 	if (!lp->lp_detaching) {
650 		lagg_ether_cmdmulti(lp, 0);
651 		lagg_setflags(lp, 0);
652 		lagg_port_lladdr(lp, lp->lp_lladdr);
653 	}
654 
655 	/* Restore interface */
656 	ifp->if_type = lp->lp_iftype;
657 	ifp->if_ioctl = lp->lp_ioctl;
658 	ifp->if_output = lp->lp_output;
659 	ifp->if_lagg = NULL;
660 
661 	/* Finally, remove the port from the lagg */
662 	SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
663 	sc->sc_count--;
664 
665 	/* Update the primary interface */
666 	if (lp == sc->sc_primary) {
667 		uint8_t lladdr[ETHER_ADDR_LEN];
668 
669 		if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
670 			bzero(&lladdr, ETHER_ADDR_LEN);
671 		} else {
672 			bcopy(lp_ptr->lp_lladdr,
673 			    lladdr, ETHER_ADDR_LEN);
674 		}
675 		lagg_lladdr(sc, lladdr);
676 		sc->sc_primary = lp_ptr;
677 
678 		/* Update link layer address for each port */
679 		SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
680 			lagg_port_lladdr(lp_ptr, lladdr);
681 	}
682 
683 	/* Remove any pending lladdr changes from the queue */
684 	if (lp->lp_detaching) {
685 		SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
686 			if (llq->llq_ifp == ifp) {
687 				SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
688 				    llq_entries);
689 				free(llq, M_DEVBUF);
690 				break;	/* Only appears once */
691 			}
692 		}
693 	}
694 
695 	if (lp->lp_ifflags)
696 		if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
697 
698 	free(lp, M_DEVBUF);
699 
700 	/* Update lagg capabilities */
701 	lagg_capabilities(sc);
702 	lagg_linkstate(sc);
703 
704 	return (0);
705 }
706 
707 static int
708 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
709 {
710 	struct lagg_reqport *rp = (struct lagg_reqport *)data;
711 	struct lagg_softc *sc;
712 	struct lagg_port *lp = NULL;
713 	int error = 0;
714 
715 	/* Should be checked by the caller */
716 	if (ifp->if_type != IFT_IEEE8023ADLAG ||
717 	    (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
718 		goto fallback;
719 
720 	switch (cmd) {
721 	case SIOCGLAGGPORT:
722 		if (rp->rp_portname[0] == '\0' ||
723 		    ifunit(rp->rp_portname) != ifp) {
724 			error = EINVAL;
725 			break;
726 		}
727 
728 		LAGG_RLOCK(sc);
729 		if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
730 			error = ENOENT;
731 			LAGG_RUNLOCK(sc);
732 			break;
733 		}
734 
735 		lagg_port2req(lp, rp);
736 		LAGG_RUNLOCK(sc);
737 		break;
738 
739 	case SIOCSIFCAP:
740 		if (lp->lp_ioctl == NULL) {
741 			error = EINVAL;
742 			break;
743 		}
744 		error = (*lp->lp_ioctl)(ifp, cmd, data);
745 		if (error)
746 			break;
747 
748 		/* Update lagg interface capabilities */
749 		LAGG_WLOCK(sc);
750 		lagg_capabilities(sc);
751 		LAGG_WUNLOCK(sc);
752 		break;
753 
754 	case SIOCSIFMTU:
755 		/* Do not allow the MTU to be changed once joined */
756 		error = EINVAL;
757 		break;
758 
759 	default:
760 		goto fallback;
761 	}
762 
763 	return (error);
764 
765 fallback:
766 	if (lp->lp_ioctl != NULL)
767 		return ((*lp->lp_ioctl)(ifp, cmd, data));
768 
769 	return (EINVAL);
770 }
771 
772 /*
773  * For direct output to child ports.
774  */
775 static int
776 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
777 	struct sockaddr *dst, struct route *ro)
778 {
779 	struct lagg_port *lp = ifp->if_lagg;
780 
781 	switch (dst->sa_family) {
782 		case pseudo_AF_HDRCMPLT:
783 		case AF_UNSPEC:
784 			return ((*lp->lp_output)(ifp, m, dst, ro));
785 	}
786 
787 	/* drop any other frames */
788 	m_freem(m);
789 	return (EBUSY);
790 }
791 
792 static void
793 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
794 {
795 	struct lagg_port *lp;
796 	struct lagg_softc *sc;
797 
798 	if ((lp = ifp->if_lagg) == NULL)
799 		return;
800 
801 	sc = lp->lp_softc;
802 
803 	LAGG_WLOCK(sc);
804 	lp->lp_detaching = 1;
805 	lagg_port_destroy(lp, 1);
806 	LAGG_WUNLOCK(sc);
807 }
808 
809 static void
810 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
811 {
812 	struct lagg_softc *sc = lp->lp_softc;
813 
814 	strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
815 	strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
816 	rp->rp_prio = lp->lp_prio;
817 	rp->rp_flags = lp->lp_flags;
818 	if (sc->sc_portreq != NULL)
819 		(*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
820 
821 	/* Add protocol specific flags */
822 	switch (sc->sc_proto) {
823 		case LAGG_PROTO_FAILOVER:
824 			if (lp == sc->sc_primary)
825 				rp->rp_flags |= LAGG_PORT_MASTER;
826 			if (lp == lagg_link_active(sc, sc->sc_primary))
827 				rp->rp_flags |= LAGG_PORT_ACTIVE;
828 			break;
829 
830 		case LAGG_PROTO_ROUNDROBIN:
831 		case LAGG_PROTO_LOADBALANCE:
832 		case LAGG_PROTO_ETHERCHANNEL:
833 			if (LAGG_PORTACTIVE(lp))
834 				rp->rp_flags |= LAGG_PORT_ACTIVE;
835 			break;
836 
837 		case LAGG_PROTO_LACP:
838 			/* LACP has a different definition of active */
839 			if (lacp_isactive(lp))
840 				rp->rp_flags |= LAGG_PORT_ACTIVE;
841 			if (lacp_iscollecting(lp))
842 				rp->rp_flags |= LAGG_PORT_COLLECTING;
843 			if (lacp_isdistributing(lp))
844 				rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
845 			break;
846 	}
847 
848 }
849 
850 static void
851 lagg_init(void *xsc)
852 {
853 	struct lagg_softc *sc = (struct lagg_softc *)xsc;
854 	struct lagg_port *lp;
855 	struct ifnet *ifp = sc->sc_ifp;
856 
857 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
858 		return;
859 
860 	LAGG_WLOCK(sc);
861 
862 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
863 	/* Update the port lladdrs */
864 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
865 		lagg_port_lladdr(lp, IF_LLADDR(ifp));
866 
867 	if (sc->sc_init != NULL)
868 		(*sc->sc_init)(sc);
869 
870 	LAGG_WUNLOCK(sc);
871 }
872 
873 static void
874 lagg_stop(struct lagg_softc *sc)
875 {
876 	struct ifnet *ifp = sc->sc_ifp;
877 
878 	LAGG_WLOCK_ASSERT(sc);
879 
880 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
881 		return;
882 
883 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
884 
885 	if (sc->sc_stop != NULL)
886 		(*sc->sc_stop)(sc);
887 }
888 
889 static int
890 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
891 {
892 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
893 	struct lagg_reqall *ra = (struct lagg_reqall *)data;
894 	struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
895 	struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
896 	struct ifreq *ifr = (struct ifreq *)data;
897 	struct lagg_port *lp;
898 	struct ifnet *tpif;
899 	struct thread *td = curthread;
900 	char *buf, *outbuf;
901 	int count, buflen, len, error = 0;
902 
903 	bzero(&rpbuf, sizeof(rpbuf));
904 
905 	switch (cmd) {
906 	case SIOCGLAGG:
907 		LAGG_RLOCK(sc);
908 		count = 0;
909 		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
910 			count++;
911 		buflen = count * sizeof(struct lagg_reqport);
912 		LAGG_RUNLOCK(sc);
913 
914 		outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
915 
916 		LAGG_RLOCK(sc);
917 		ra->ra_proto = sc->sc_proto;
918 		if (sc->sc_req != NULL)
919 			(*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
920 
921 		count = 0;
922 		buf = outbuf;
923 		len = min(ra->ra_size, buflen);
924 		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
925 			if (len < sizeof(rpbuf))
926 				break;
927 
928 			lagg_port2req(lp, &rpbuf);
929 			memcpy(buf, &rpbuf, sizeof(rpbuf));
930 			count++;
931 			buf += sizeof(rpbuf);
932 			len -= sizeof(rpbuf);
933 		}
934 		LAGG_RUNLOCK(sc);
935 		ra->ra_ports = count;
936 		ra->ra_size = count * sizeof(rpbuf);
937 		error = copyout(outbuf, ra->ra_port, ra->ra_size);
938 		free(outbuf, M_TEMP);
939 		break;
940 	case SIOCSLAGG:
941 		error = priv_check(td, PRIV_NET_LAGG);
942 		if (error)
943 			break;
944 		if (ra->ra_proto >= LAGG_PROTO_MAX) {
945 			error = EPROTONOSUPPORT;
946 			break;
947 		}
948 		LAGG_WLOCK(sc);
949 		if (sc->sc_proto != LAGG_PROTO_NONE) {
950 			/* Reset protocol first in case detach unlocks */
951 			sc->sc_proto = LAGG_PROTO_NONE;
952 			error = sc->sc_detach(sc);
953 			sc->sc_detach = NULL;
954 			sc->sc_start = NULL;
955 			sc->sc_input = NULL;
956 			sc->sc_port_create = NULL;
957 			sc->sc_port_destroy = NULL;
958 			sc->sc_linkstate = NULL;
959 			sc->sc_init = NULL;
960 			sc->sc_stop = NULL;
961 			sc->sc_lladdr = NULL;
962 			sc->sc_req = NULL;
963 			sc->sc_portreq = NULL;
964 		} else if (sc->sc_input != NULL) {
965 			/* Still detaching */
966 			error = EBUSY;
967 		}
968 		if (error != 0) {
969 			LAGG_WUNLOCK(sc);
970 			break;
971 		}
972 		for (int i = 0; i < (sizeof(lagg_protos) /
973 		    sizeof(lagg_protos[0])); i++) {
974 			if (lagg_protos[i].ti_proto == ra->ra_proto) {
975 				if (sc->sc_ifflags & IFF_DEBUG)
976 					printf("%s: using proto %u\n",
977 					    sc->sc_ifname,
978 					    lagg_protos[i].ti_proto);
979 				sc->sc_proto = lagg_protos[i].ti_proto;
980 				if (sc->sc_proto != LAGG_PROTO_NONE)
981 					error = lagg_protos[i].ti_attach(sc);
982 				LAGG_WUNLOCK(sc);
983 				return (error);
984 			}
985 		}
986 		LAGG_WUNLOCK(sc);
987 		error = EPROTONOSUPPORT;
988 		break;
989 	case SIOCGLAGGFLAGS:
990 		rf->rf_flags = sc->sc_flags;
991 		break;
992 	case SIOCSLAGGHASH:
993 		error = priv_check(td, PRIV_NET_LAGG);
994 		if (error)
995 			break;
996 		if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
997 			error = EINVAL;
998 			break;
999 		}
1000 		LAGG_WLOCK(sc);
1001 		sc->sc_flags &= ~LAGG_F_HASHMASK;
1002 		sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1003 		LAGG_WUNLOCK(sc);
1004 		break;
1005 	case SIOCGLAGGPORT:
1006 		if (rp->rp_portname[0] == '\0' ||
1007 		    (tpif = ifunit(rp->rp_portname)) == NULL) {
1008 			error = EINVAL;
1009 			break;
1010 		}
1011 
1012 		LAGG_RLOCK(sc);
1013 		if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1014 		    lp->lp_softc != sc) {
1015 			error = ENOENT;
1016 			LAGG_RUNLOCK(sc);
1017 			break;
1018 		}
1019 
1020 		lagg_port2req(lp, rp);
1021 		LAGG_RUNLOCK(sc);
1022 		break;
1023 	case SIOCSLAGGPORT:
1024 		error = priv_check(td, PRIV_NET_LAGG);
1025 		if (error)
1026 			break;
1027 		if (rp->rp_portname[0] == '\0' ||
1028 		    (tpif = ifunit(rp->rp_portname)) == NULL) {
1029 			error = EINVAL;
1030 			break;
1031 		}
1032 		LAGG_WLOCK(sc);
1033 		error = lagg_port_create(sc, tpif);
1034 		LAGG_WUNLOCK(sc);
1035 		break;
1036 	case SIOCSLAGGDELPORT:
1037 		error = priv_check(td, PRIV_NET_LAGG);
1038 		if (error)
1039 			break;
1040 		if (rp->rp_portname[0] == '\0' ||
1041 		    (tpif = ifunit(rp->rp_portname)) == NULL) {
1042 			error = EINVAL;
1043 			break;
1044 		}
1045 
1046 		LAGG_WLOCK(sc);
1047 		if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1048 		    lp->lp_softc != sc) {
1049 			error = ENOENT;
1050 			LAGG_WUNLOCK(sc);
1051 			break;
1052 		}
1053 
1054 		error = lagg_port_destroy(lp, 1);
1055 		LAGG_WUNLOCK(sc);
1056 		break;
1057 	case SIOCSIFFLAGS:
1058 		/* Set flags on ports too */
1059 		LAGG_WLOCK(sc);
1060 		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1061 			lagg_setflags(lp, 1);
1062 		}
1063 		LAGG_WUNLOCK(sc);
1064 
1065 		if (!(ifp->if_flags & IFF_UP) &&
1066 		    (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1067 			/*
1068 			 * If interface is marked down and it is running,
1069 			 * then stop and disable it.
1070 			 */
1071 			LAGG_WLOCK(sc);
1072 			lagg_stop(sc);
1073 			LAGG_WUNLOCK(sc);
1074 		} else if ((ifp->if_flags & IFF_UP) &&
1075 		    !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1076 			/*
1077 			 * If interface is marked up and it is stopped, then
1078 			 * start it.
1079 			 */
1080 			(*ifp->if_init)(sc);
1081 		}
1082 		break;
1083 	case SIOCADDMULTI:
1084 	case SIOCDELMULTI:
1085 		LAGG_WLOCK(sc);
1086 		error = lagg_ether_setmulti(sc);
1087 		LAGG_WUNLOCK(sc);
1088 		break;
1089 	case SIOCSIFMEDIA:
1090 	case SIOCGIFMEDIA:
1091 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1092 		break;
1093 
1094 	case SIOCSIFCAP:
1095 	case SIOCSIFMTU:
1096 		/* Do not allow the MTU or caps to be directly changed */
1097 		error = EINVAL;
1098 		break;
1099 
1100 	default:
1101 		error = ether_ioctl(ifp, cmd, data);
1102 		break;
1103 	}
1104 	return (error);
1105 }
1106 
1107 static int
1108 lagg_ether_setmulti(struct lagg_softc *sc)
1109 {
1110 	struct lagg_port *lp;
1111 
1112 	LAGG_WLOCK_ASSERT(sc);
1113 
1114 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1115 		/* First, remove any existing filter entries. */
1116 		lagg_ether_cmdmulti(lp, 0);
1117 		/* copy all addresses from the lagg interface to the port */
1118 		lagg_ether_cmdmulti(lp, 1);
1119 	}
1120 	return (0);
1121 }
1122 
1123 static int
1124 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1125 {
1126 	struct lagg_softc *sc = lp->lp_softc;
1127 	struct ifnet *ifp = lp->lp_ifp;
1128 	struct ifnet *scifp = sc->sc_ifp;
1129 	struct lagg_mc *mc;
1130 	struct ifmultiaddr *ifma, *rifma = NULL;
1131 	struct sockaddr_dl sdl;
1132 	int error;
1133 
1134 	LAGG_WLOCK_ASSERT(sc);
1135 
1136 	bzero((char *)&sdl, sizeof(sdl));
1137 	sdl.sdl_len = sizeof(sdl);
1138 	sdl.sdl_family = AF_LINK;
1139 	sdl.sdl_type = IFT_ETHER;
1140 	sdl.sdl_alen = ETHER_ADDR_LEN;
1141 	sdl.sdl_index = ifp->if_index;
1142 
1143 	if (set) {
1144 		TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1145 			if (ifma->ifma_addr->sa_family != AF_LINK)
1146 				continue;
1147 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1148 			    LLADDR(&sdl), ETHER_ADDR_LEN);
1149 
1150 			error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1151 			if (error)
1152 				return (error);
1153 			mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1154 			if (mc == NULL)
1155 				return (ENOMEM);
1156 			mc->mc_ifma = rifma;
1157 			SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1158 		}
1159 	} else {
1160 		while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1161 			SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1162 			if_delmulti_ifma(mc->mc_ifma);
1163 			free(mc, M_DEVBUF);
1164 		}
1165 	}
1166 	return (0);
1167 }
1168 
1169 /* Handle a ref counted flag that should be set on the lagg port as well */
1170 static int
1171 lagg_setflag(struct lagg_port *lp, int flag, int status,
1172 	     int (*func)(struct ifnet *, int))
1173 {
1174 	struct lagg_softc *sc = lp->lp_softc;
1175 	struct ifnet *scifp = sc->sc_ifp;
1176 	struct ifnet *ifp = lp->lp_ifp;
1177 	int error;
1178 
1179 	LAGG_WLOCK_ASSERT(sc);
1180 
1181 	status = status ? (scifp->if_flags & flag) : 0;
1182 	/* Now "status" contains the flag value or 0 */
1183 
1184 	/*
1185 	 * See if recorded ports status is different from what
1186 	 * we want it to be.  If it is, flip it.  We record ports
1187 	 * status in lp_ifflags so that we won't clear ports flag
1188 	 * we haven't set.  In fact, we don't clear or set ports
1189 	 * flags directly, but get or release references to them.
1190 	 * That's why we can be sure that recorded flags still are
1191 	 * in accord with actual ports flags.
1192 	 */
1193 	if (status != (lp->lp_ifflags & flag)) {
1194 		error = (*func)(ifp, status);
1195 		if (error)
1196 			return (error);
1197 		lp->lp_ifflags &= ~flag;
1198 		lp->lp_ifflags |= status;
1199 	}
1200 	return (0);
1201 }
1202 
1203 /*
1204  * Handle IFF_* flags that require certain changes on the lagg port
1205  * if "status" is true, update ports flags respective to the lagg
1206  * if "status" is false, forcedly clear the flags set on port.
1207  */
1208 static int
1209 lagg_setflags(struct lagg_port *lp, int status)
1210 {
1211 	int error, i;
1212 
1213 	for (i = 0; lagg_pflags[i].flag; i++) {
1214 		error = lagg_setflag(lp, lagg_pflags[i].flag,
1215 		    status, lagg_pflags[i].func);
1216 		if (error)
1217 			return (error);
1218 	}
1219 	return (0);
1220 }
1221 
1222 static void
1223 lagg_start(struct ifnet *ifp)
1224 {
1225 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1226 	struct mbuf *m;
1227 	int error = 0;
1228 
1229 	LAGG_RLOCK(sc);
1230 	/* We need a Tx algorithm and at least one port */
1231 	if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1232 		IF_DRAIN(&ifp->if_snd);
1233 		LAGG_RUNLOCK(sc);
1234 		return;
1235 	}
1236 
1237 	for (;; error = 0) {
1238 		IFQ_DEQUEUE(&ifp->if_snd, m);
1239 		if (m == NULL)
1240 			break;
1241 
1242 		ETHER_BPF_MTAP(ifp, m);
1243 
1244 		error = (*sc->sc_start)(sc, m);
1245 		if (error == 0)
1246 			ifp->if_opackets++;
1247 		else
1248 			ifp->if_oerrors++;
1249 	}
1250 	LAGG_RUNLOCK(sc);
1251 }
1252 
1253 static struct mbuf *
1254 lagg_input(struct ifnet *ifp, struct mbuf *m)
1255 {
1256 	struct lagg_port *lp = ifp->if_lagg;
1257 	struct lagg_softc *sc = lp->lp_softc;
1258 	struct ifnet *scifp = sc->sc_ifp;
1259 
1260 	LAGG_RLOCK(sc);
1261 	if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1262 	    (lp->lp_flags & LAGG_PORT_DISABLED) ||
1263 	    sc->sc_proto == LAGG_PROTO_NONE) {
1264 		LAGG_RUNLOCK(sc);
1265 		m_freem(m);
1266 		return (NULL);
1267 	}
1268 
1269 	ETHER_BPF_MTAP(scifp, m);
1270 
1271 	m = (*sc->sc_input)(sc, lp, m);
1272 
1273 	if (m != NULL) {
1274 		scifp->if_ipackets++;
1275 		scifp->if_ibytes += m->m_pkthdr.len;
1276 
1277 		if (scifp->if_flags & IFF_MONITOR) {
1278 			m_freem(m);
1279 			m = NULL;
1280 		}
1281 	}
1282 
1283 	LAGG_RUNLOCK(sc);
1284 	return (m);
1285 }
1286 
1287 static int
1288 lagg_media_change(struct ifnet *ifp)
1289 {
1290 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1291 
1292 	if (sc->sc_ifflags & IFF_DEBUG)
1293 		printf("%s\n", __func__);
1294 
1295 	/* Ignore */
1296 	return (0);
1297 }
1298 
1299 static void
1300 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1301 {
1302 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1303 	struct lagg_port *lp;
1304 
1305 	imr->ifm_status = IFM_AVALID;
1306 	imr->ifm_active = IFM_ETHER | IFM_AUTO;
1307 
1308 	LAGG_RLOCK(sc);
1309 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1310 		if (LAGG_PORTACTIVE(lp))
1311 			imr->ifm_status |= IFM_ACTIVE;
1312 	}
1313 	LAGG_RUNLOCK(sc);
1314 }
1315 
1316 static void
1317 lagg_linkstate(struct lagg_softc *sc)
1318 {
1319 	struct lagg_port *lp;
1320 	int new_link = LINK_STATE_DOWN;
1321 	uint64_t speed;
1322 
1323 	/* Our link is considered up if at least one of our ports is active */
1324 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1325 		if (lp->lp_link_state == LINK_STATE_UP) {
1326 			new_link = LINK_STATE_UP;
1327 			break;
1328 		}
1329 	}
1330 	if_link_state_change(sc->sc_ifp, new_link);
1331 
1332 	/* Update if_baudrate to reflect the max possible speed */
1333 	switch (sc->sc_proto) {
1334 		case LAGG_PROTO_FAILOVER:
1335 			sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1336 			    sc->sc_primary->lp_ifp->if_baudrate : 0;
1337 			break;
1338 		case LAGG_PROTO_ROUNDROBIN:
1339 		case LAGG_PROTO_LOADBALANCE:
1340 		case LAGG_PROTO_ETHERCHANNEL:
1341 			speed = 0;
1342 			SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1343 				speed += lp->lp_ifp->if_baudrate;
1344 			sc->sc_ifp->if_baudrate = speed;
1345 			break;
1346 		case LAGG_PROTO_LACP:
1347 			/* LACP updates if_baudrate itself */
1348 			break;
1349 	}
1350 }
1351 
1352 static void
1353 lagg_port_state(struct ifnet *ifp, int state)
1354 {
1355 	struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1356 	struct lagg_softc *sc = NULL;
1357 
1358 	if (lp != NULL)
1359 		sc = lp->lp_softc;
1360 	if (sc == NULL)
1361 		return;
1362 
1363 	LAGG_WLOCK(sc);
1364 	lagg_linkstate(sc);
1365 	if (sc->sc_linkstate != NULL)
1366 		(*sc->sc_linkstate)(lp);
1367 	LAGG_WUNLOCK(sc);
1368 }
1369 
1370 struct lagg_port *
1371 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1372 {
1373 	struct lagg_port *lp_next, *rval = NULL;
1374 	// int new_link = LINK_STATE_DOWN;
1375 
1376 	LAGG_RLOCK_ASSERT(sc);
1377 	/*
1378 	 * Search a port which reports an active link state.
1379 	 */
1380 
1381 	if (lp == NULL)
1382 		goto search;
1383 	if (LAGG_PORTACTIVE(lp)) {
1384 		rval = lp;
1385 		goto found;
1386 	}
1387 	if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1388 	    LAGG_PORTACTIVE(lp_next)) {
1389 		rval = lp_next;
1390 		goto found;
1391 	}
1392 
1393 search:
1394 	SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1395 		if (LAGG_PORTACTIVE(lp_next)) {
1396 			rval = lp_next;
1397 			goto found;
1398 		}
1399 	}
1400 
1401 found:
1402 	if (rval != NULL) {
1403 		/*
1404 		 * The IEEE 802.1D standard assumes that a lagg with
1405 		 * multiple ports is always full duplex. This is valid
1406 		 * for load sharing laggs and if at least two links
1407 		 * are active. Unfortunately, checking the latter would
1408 		 * be too expensive at this point.
1409 		 XXX
1410 		if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1411 		    (sc->sc_count > 1))
1412 			new_link = LINK_STATE_FULL_DUPLEX;
1413 		else
1414 			new_link = rval->lp_link_state;
1415 		 */
1416 	}
1417 
1418 	return (rval);
1419 }
1420 
1421 static const void *
1422 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1423 {
1424 	if (m->m_pkthdr.len < (off + len)) {
1425 		return (NULL);
1426 	} else if (m->m_len < (off + len)) {
1427 		m_copydata(m, off, len, buf);
1428 		return (buf);
1429 	}
1430 	return (mtod(m, char *) + off);
1431 }
1432 
1433 uint32_t
1434 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1435 {
1436 	uint16_t etype;
1437 	uint32_t p = key;
1438 	int off;
1439 	struct ether_header *eh;
1440 	const struct ether_vlan_header *vlan;
1441 #ifdef INET
1442 	const struct ip *ip;
1443 	const uint32_t *ports;
1444 	int iphlen;
1445 #endif
1446 #ifdef INET6
1447 	const struct ip6_hdr *ip6;
1448 	uint32_t flow;
1449 #endif
1450 	union {
1451 #ifdef INET
1452 		struct ip ip;
1453 #endif
1454 #ifdef INET6
1455 		struct ip6_hdr ip6;
1456 #endif
1457 		struct ether_vlan_header vlan;
1458 		uint32_t port;
1459 	} buf;
1460 
1461 
1462 	off = sizeof(*eh);
1463 	if (m->m_len < off)
1464 		goto out;
1465 	eh = mtod(m, struct ether_header *);
1466 	etype = ntohs(eh->ether_type);
1467 	if (sc->sc_flags & LAGG_F_HASHL2) {
1468 		p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1469 		p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1470 	}
1471 
1472 	/* Special handling for encapsulating VLAN frames */
1473 	if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1474 		p = hash32_buf(&m->m_pkthdr.ether_vtag,
1475 		    sizeof(m->m_pkthdr.ether_vtag), p);
1476 	} else if (etype == ETHERTYPE_VLAN) {
1477 		vlan = lagg_gethdr(m, off,  sizeof(*vlan), &buf);
1478 		if (vlan == NULL)
1479 			goto out;
1480 
1481 		if (sc->sc_flags & LAGG_F_HASHL2)
1482 			p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1483 		etype = ntohs(vlan->evl_proto);
1484 		off += sizeof(*vlan) - sizeof(*eh);
1485 	}
1486 
1487 	switch (etype) {
1488 #ifdef INET
1489 	case ETHERTYPE_IP:
1490 		ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1491 		if (ip == NULL)
1492 			goto out;
1493 
1494 		if (sc->sc_flags & LAGG_F_HASHL3) {
1495 			p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1496 			p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1497 		}
1498 		if (!(sc->sc_flags & LAGG_F_HASHL4))
1499 			break;
1500 		switch (ip->ip_p) {
1501 			case IPPROTO_TCP:
1502 			case IPPROTO_UDP:
1503 			case IPPROTO_SCTP:
1504 				iphlen = ip->ip_hl << 2;
1505 				if (iphlen < sizeof(*ip))
1506 					break;
1507 				off += iphlen;
1508 				ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1509 				if (ports == NULL)
1510 					break;
1511 				p = hash32_buf(ports, sizeof(*ports), p);
1512 				break;
1513 		}
1514 		break;
1515 #endif
1516 #ifdef INET6
1517 	case ETHERTYPE_IPV6:
1518 		if (!(sc->sc_flags & LAGG_F_HASHL3))
1519 			break;
1520 		ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1521 		if (ip6 == NULL)
1522 			goto out;
1523 
1524 		p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1525 		p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1526 		flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1527 		p = hash32_buf(&flow, sizeof(flow), p);	/* IPv6 flow label */
1528 		break;
1529 #endif
1530 	}
1531 out:
1532 	return (p);
1533 }
1534 
1535 int
1536 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1537 {
1538 
1539 	return (ifp->if_transmit)(ifp, m);
1540 }
1541 
1542 /*
1543  * Simple round robin aggregation
1544  */
1545 
1546 static int
1547 lagg_rr_attach(struct lagg_softc *sc)
1548 {
1549 	sc->sc_detach = lagg_rr_detach;
1550 	sc->sc_start = lagg_rr_start;
1551 	sc->sc_input = lagg_rr_input;
1552 	sc->sc_port_create = NULL;
1553 	sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1554 	sc->sc_seq = 0;
1555 
1556 	return (0);
1557 }
1558 
1559 static int
1560 lagg_rr_detach(struct lagg_softc *sc)
1561 {
1562 	return (0);
1563 }
1564 
1565 static int
1566 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1567 {
1568 	struct lagg_port *lp;
1569 	uint32_t p;
1570 
1571 	p = atomic_fetchadd_32(&sc->sc_seq, 1);
1572 	p %= sc->sc_count;
1573 	lp = SLIST_FIRST(&sc->sc_ports);
1574 	while (p--)
1575 		lp = SLIST_NEXT(lp, lp_entries);
1576 
1577 	/*
1578 	 * Check the port's link state. This will return the next active
1579 	 * port if the link is down or the port is NULL.
1580 	 */
1581 	if ((lp = lagg_link_active(sc, lp)) == NULL) {
1582 		m_freem(m);
1583 		return (ENOENT);
1584 	}
1585 
1586 	/* Send mbuf */
1587 	return (lagg_enqueue(lp->lp_ifp, m));
1588 }
1589 
1590 static struct mbuf *
1591 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1592 {
1593 	struct ifnet *ifp = sc->sc_ifp;
1594 
1595 	/* Just pass in the packet to our lagg device */
1596 	m->m_pkthdr.rcvif = ifp;
1597 
1598 	return (m);
1599 }
1600 
1601 /*
1602  * Active failover
1603  */
1604 
1605 static int
1606 lagg_fail_attach(struct lagg_softc *sc)
1607 {
1608 	sc->sc_detach = lagg_fail_detach;
1609 	sc->sc_start = lagg_fail_start;
1610 	sc->sc_input = lagg_fail_input;
1611 	sc->sc_port_create = NULL;
1612 	sc->sc_port_destroy = NULL;
1613 
1614 	return (0);
1615 }
1616 
1617 static int
1618 lagg_fail_detach(struct lagg_softc *sc)
1619 {
1620 	return (0);
1621 }
1622 
1623 static int
1624 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1625 {
1626 	struct lagg_port *lp;
1627 
1628 	/* Use the master port if active or the next available port */
1629 	if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1630 		m_freem(m);
1631 		return (ENOENT);
1632 	}
1633 
1634 	/* Send mbuf */
1635 	return (lagg_enqueue(lp->lp_ifp, m));
1636 }
1637 
1638 static struct mbuf *
1639 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1640 {
1641 	struct ifnet *ifp = sc->sc_ifp;
1642 	struct lagg_port *tmp_tp;
1643 
1644 	if (lp == sc->sc_primary || lagg_failover_rx_all) {
1645 		m->m_pkthdr.rcvif = ifp;
1646 		return (m);
1647 	}
1648 
1649 	if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1650 		tmp_tp = lagg_link_active(sc, sc->sc_primary);
1651 		/*
1652 		 * If tmp_tp is null, we've recieved a packet when all
1653 		 * our links are down. Weird, but process it anyways.
1654 		 */
1655 		if ((tmp_tp == NULL || tmp_tp == lp)) {
1656 			m->m_pkthdr.rcvif = ifp;
1657 			return (m);
1658 		}
1659 	}
1660 
1661 	m_freem(m);
1662 	return (NULL);
1663 }
1664 
1665 /*
1666  * Loadbalancing
1667  */
1668 
1669 static int
1670 lagg_lb_attach(struct lagg_softc *sc)
1671 {
1672 	struct lagg_port *lp;
1673 	struct lagg_lb *lb;
1674 
1675 	if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1676 	    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1677 		return (ENOMEM);
1678 
1679 	sc->sc_detach = lagg_lb_detach;
1680 	sc->sc_start = lagg_lb_start;
1681 	sc->sc_input = lagg_lb_input;
1682 	sc->sc_port_create = lagg_lb_port_create;
1683 	sc->sc_port_destroy = lagg_lb_port_destroy;
1684 	sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1685 
1686 	lb->lb_key = arc4random();
1687 	sc->sc_psc = (caddr_t)lb;
1688 
1689 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1690 		lagg_lb_port_create(lp);
1691 
1692 	return (0);
1693 }
1694 
1695 static int
1696 lagg_lb_detach(struct lagg_softc *sc)
1697 {
1698 	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1699 	if (lb != NULL)
1700 		free(lb, M_DEVBUF);
1701 	return (0);
1702 }
1703 
1704 static int
1705 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1706 {
1707 	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1708 	struct lagg_port *lp_next;
1709 	int i = 0;
1710 
1711 	bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1712 	SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1713 		if (lp_next == lp)
1714 			continue;
1715 		if (i >= LAGG_MAX_PORTS)
1716 			return (EINVAL);
1717 		if (sc->sc_ifflags & IFF_DEBUG)
1718 			printf("%s: port %s at index %d\n",
1719 			    sc->sc_ifname, lp_next->lp_ifname, i);
1720 		lb->lb_ports[i++] = lp_next;
1721 	}
1722 
1723 	return (0);
1724 }
1725 
1726 static int
1727 lagg_lb_port_create(struct lagg_port *lp)
1728 {
1729 	struct lagg_softc *sc = lp->lp_softc;
1730 	return (lagg_lb_porttable(sc, NULL));
1731 }
1732 
1733 static void
1734 lagg_lb_port_destroy(struct lagg_port *lp)
1735 {
1736 	struct lagg_softc *sc = lp->lp_softc;
1737 	lagg_lb_porttable(sc, lp);
1738 }
1739 
1740 static int
1741 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1742 {
1743 	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1744 	struct lagg_port *lp = NULL;
1745 	uint32_t p = 0;
1746 
1747 	if (sc->use_flowid && (m->m_flags & M_FLOWID))
1748 		p = m->m_pkthdr.flowid;
1749 	else
1750 		p = lagg_hashmbuf(sc, m, lb->lb_key);
1751 	p %= sc->sc_count;
1752 	lp = lb->lb_ports[p];
1753 
1754 	/*
1755 	 * Check the port's link state. This will return the next active
1756 	 * port if the link is down or the port is NULL.
1757 	 */
1758 	if ((lp = lagg_link_active(sc, lp)) == NULL) {
1759 		m_freem(m);
1760 		return (ENOENT);
1761 	}
1762 
1763 	/* Send mbuf */
1764 	return (lagg_enqueue(lp->lp_ifp, m));
1765 }
1766 
1767 static struct mbuf *
1768 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1769 {
1770 	struct ifnet *ifp = sc->sc_ifp;
1771 
1772 	/* Just pass in the packet to our lagg device */
1773 	m->m_pkthdr.rcvif = ifp;
1774 
1775 	return (m);
1776 }
1777 
1778 /*
1779  * 802.3ad LACP
1780  */
1781 
1782 static int
1783 lagg_lacp_attach(struct lagg_softc *sc)
1784 {
1785 	struct lagg_port *lp;
1786 	int error;
1787 
1788 	sc->sc_detach = lagg_lacp_detach;
1789 	sc->sc_port_create = lacp_port_create;
1790 	sc->sc_port_destroy = lacp_port_destroy;
1791 	sc->sc_linkstate = lacp_linkstate;
1792 	sc->sc_start = lagg_lacp_start;
1793 	sc->sc_input = lagg_lacp_input;
1794 	sc->sc_init = lacp_init;
1795 	sc->sc_stop = lacp_stop;
1796 	sc->sc_lladdr = lagg_lacp_lladdr;
1797 	sc->sc_req = lacp_req;
1798 	sc->sc_portreq = lacp_portreq;
1799 
1800 	error = lacp_attach(sc);
1801 	if (error)
1802 		return (error);
1803 
1804 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1805 		lacp_port_create(lp);
1806 
1807 	return (error);
1808 }
1809 
1810 static int
1811 lagg_lacp_detach(struct lagg_softc *sc)
1812 {
1813 	struct lagg_port *lp;
1814 	int error;
1815 
1816 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1817 		lacp_port_destroy(lp);
1818 
1819 	/* unlocking is safe here */
1820 	LAGG_WUNLOCK(sc);
1821 	error = lacp_detach(sc);
1822 	LAGG_WLOCK(sc);
1823 
1824 	return (error);
1825 }
1826 
1827 static void
1828 lagg_lacp_lladdr(struct lagg_softc *sc)
1829 {
1830 	struct lagg_port *lp;
1831 
1832 	/* purge all the lacp ports */
1833 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1834 		lacp_port_destroy(lp);
1835 
1836 	/* add them back in */
1837 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1838 		lacp_port_create(lp);
1839 }
1840 
1841 static int
1842 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1843 {
1844 	struct lagg_port *lp;
1845 
1846 	lp = lacp_select_tx_port(sc, m);
1847 	if (lp == NULL) {
1848 		m_freem(m);
1849 		return (EBUSY);
1850 	}
1851 
1852 	/* Send mbuf */
1853 	return (lagg_enqueue(lp->lp_ifp, m));
1854 }
1855 
1856 static struct mbuf *
1857 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1858 {
1859 	struct ifnet *ifp = sc->sc_ifp;
1860 	struct ether_header *eh;
1861 	u_short etype;
1862 
1863 	eh = mtod(m, struct ether_header *);
1864 	etype = ntohs(eh->ether_type);
1865 
1866 	/* Tap off LACP control messages */
1867 	if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
1868 		m = lacp_input(lp, m);
1869 		if (m == NULL)
1870 			return (NULL);
1871 	}
1872 
1873 	/*
1874 	 * If the port is not collecting or not in the active aggregator then
1875 	 * free and return.
1876 	 */
1877 	if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1878 		m_freem(m);
1879 		return (NULL);
1880 	}
1881 
1882 	m->m_pkthdr.rcvif = ifp;
1883 	return (m);
1884 }
1885