xref: /freebsd/sys/net/if_lagg.c (revision 3ef51c5fb9163f2aafb1c14729e06a8bf0c4d113)
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 		return (EBUSY);
521 
522 	/* XXX Disallow non-ethernet interfaces (this should be any of 802) */
523 	if (ifp->if_type != IFT_ETHER)
524 		return (EPROTONOSUPPORT);
525 
526 	/* Allow the first Ethernet member to define the MTU */
527 	if (SLIST_EMPTY(&sc->sc_ports))
528 		sc->sc_ifp->if_mtu = ifp->if_mtu;
529 	else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
530 		if_printf(sc->sc_ifp, "invalid MTU for %s\n",
531 		    ifp->if_xname);
532 		return (EINVAL);
533 	}
534 
535 	if ((lp = malloc(sizeof(struct lagg_port),
536 	    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
537 		return (ENOMEM);
538 
539 	/* Check if port is a stacked lagg */
540 	mtx_lock(&lagg_list_mtx);
541 	SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
542 		if (ifp == sc_ptr->sc_ifp) {
543 			mtx_unlock(&lagg_list_mtx);
544 			free(lp, M_DEVBUF);
545 			return (EINVAL);
546 			/* XXX disable stacking for the moment, its untested */
547 #ifdef LAGG_PORT_STACKING
548 			lp->lp_flags |= LAGG_PORT_STACK;
549 			if (lagg_port_checkstacking(sc_ptr) >=
550 			    LAGG_MAX_STACKING) {
551 				mtx_unlock(&lagg_list_mtx);
552 				free(lp, M_DEVBUF);
553 				return (E2BIG);
554 			}
555 #endif
556 		}
557 	}
558 	mtx_unlock(&lagg_list_mtx);
559 
560 	/* Change the interface type */
561 	lp->lp_iftype = ifp->if_type;
562 	ifp->if_type = IFT_IEEE8023ADLAG;
563 	ifp->if_lagg = lp;
564 	lp->lp_ioctl = ifp->if_ioctl;
565 	ifp->if_ioctl = lagg_port_ioctl;
566 	lp->lp_output = ifp->if_output;
567 	ifp->if_output = lagg_port_output;
568 
569 	lp->lp_ifp = ifp;
570 	lp->lp_softc = sc;
571 
572 	/* Save port link layer address */
573 	bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
574 
575 	if (SLIST_EMPTY(&sc->sc_ports)) {
576 		sc->sc_primary = lp;
577 		lagg_lladdr(sc, IF_LLADDR(ifp));
578 	} else {
579 		/* Update link layer address for this port */
580 		lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
581 	}
582 
583 	/* Insert into the list of ports */
584 	SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
585 	sc->sc_count++;
586 
587 	/* Update lagg capabilities */
588 	lagg_capabilities(sc);
589 	lagg_linkstate(sc);
590 
591 	/* Add multicast addresses and interface flags to this port */
592 	lagg_ether_cmdmulti(lp, 1);
593 	lagg_setflags(lp, 1);
594 
595 	if (sc->sc_port_create != NULL)
596 		error = (*sc->sc_port_create)(lp);
597 	if (error) {
598 		/* remove the port again, without calling sc_port_destroy */
599 		lagg_port_destroy(lp, 0);
600 		return (error);
601 	}
602 
603 	return (error);
604 }
605 
606 #ifdef LAGG_PORT_STACKING
607 static int
608 lagg_port_checkstacking(struct lagg_softc *sc)
609 {
610 	struct lagg_softc *sc_ptr;
611 	struct lagg_port *lp;
612 	int m = 0;
613 
614 	LAGG_WLOCK_ASSERT(sc);
615 
616 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
617 		if (lp->lp_flags & LAGG_PORT_STACK) {
618 			sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
619 			m = MAX(m, lagg_port_checkstacking(sc_ptr));
620 		}
621 	}
622 
623 	return (m + 1);
624 }
625 #endif
626 
627 static int
628 lagg_port_destroy(struct lagg_port *lp, int runpd)
629 {
630 	struct lagg_softc *sc = lp->lp_softc;
631 	struct lagg_port *lp_ptr;
632 	struct lagg_llq *llq;
633 	struct ifnet *ifp = lp->lp_ifp;
634 
635 	LAGG_WLOCK_ASSERT(sc);
636 
637 	if (runpd && sc->sc_port_destroy != NULL)
638 		(*sc->sc_port_destroy)(lp);
639 
640 	/*
641 	 * Remove multicast addresses and interface flags from this port and
642 	 * reset the MAC address, skip if the interface is being detached.
643 	 */
644 	if (!lp->lp_detaching) {
645 		lagg_ether_cmdmulti(lp, 0);
646 		lagg_setflags(lp, 0);
647 		lagg_port_lladdr(lp, lp->lp_lladdr);
648 	}
649 
650 	/* Restore interface */
651 	ifp->if_type = lp->lp_iftype;
652 	ifp->if_ioctl = lp->lp_ioctl;
653 	ifp->if_output = lp->lp_output;
654 	ifp->if_lagg = NULL;
655 
656 	/* Finally, remove the port from the lagg */
657 	SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
658 	sc->sc_count--;
659 
660 	/* Update the primary interface */
661 	if (lp == sc->sc_primary) {
662 		uint8_t lladdr[ETHER_ADDR_LEN];
663 
664 		if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
665 			bzero(&lladdr, ETHER_ADDR_LEN);
666 		} else {
667 			bcopy(lp_ptr->lp_lladdr,
668 			    lladdr, ETHER_ADDR_LEN);
669 		}
670 		lagg_lladdr(sc, lladdr);
671 		sc->sc_primary = lp_ptr;
672 
673 		/* Update link layer address for each port */
674 		SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
675 			lagg_port_lladdr(lp_ptr, lladdr);
676 	}
677 
678 	/* Remove any pending lladdr changes from the queue */
679 	if (lp->lp_detaching) {
680 		SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
681 			if (llq->llq_ifp == ifp) {
682 				SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
683 				    llq_entries);
684 				free(llq, M_DEVBUF);
685 				break;	/* Only appears once */
686 			}
687 		}
688 	}
689 
690 	if (lp->lp_ifflags)
691 		if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
692 
693 	free(lp, M_DEVBUF);
694 
695 	/* Update lagg capabilities */
696 	lagg_capabilities(sc);
697 	lagg_linkstate(sc);
698 
699 	return (0);
700 }
701 
702 static int
703 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
704 {
705 	struct lagg_reqport *rp = (struct lagg_reqport *)data;
706 	struct lagg_softc *sc;
707 	struct lagg_port *lp = NULL;
708 	int error = 0;
709 
710 	/* Should be checked by the caller */
711 	if (ifp->if_type != IFT_IEEE8023ADLAG ||
712 	    (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
713 		goto fallback;
714 
715 	switch (cmd) {
716 	case SIOCGLAGGPORT:
717 		if (rp->rp_portname[0] == '\0' ||
718 		    ifunit(rp->rp_portname) != ifp) {
719 			error = EINVAL;
720 			break;
721 		}
722 
723 		LAGG_RLOCK(sc);
724 		if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
725 			error = ENOENT;
726 			LAGG_RUNLOCK(sc);
727 			break;
728 		}
729 
730 		lagg_port2req(lp, rp);
731 		LAGG_RUNLOCK(sc);
732 		break;
733 
734 	case SIOCSIFCAP:
735 		if (lp->lp_ioctl == NULL) {
736 			error = EINVAL;
737 			break;
738 		}
739 		error = (*lp->lp_ioctl)(ifp, cmd, data);
740 		if (error)
741 			break;
742 
743 		/* Update lagg interface capabilities */
744 		LAGG_WLOCK(sc);
745 		lagg_capabilities(sc);
746 		LAGG_WUNLOCK(sc);
747 		break;
748 
749 	case SIOCSIFMTU:
750 		/* Do not allow the MTU to be changed once joined */
751 		error = EINVAL;
752 		break;
753 
754 	default:
755 		goto fallback;
756 	}
757 
758 	return (error);
759 
760 fallback:
761 	if (lp->lp_ioctl != NULL)
762 		return ((*lp->lp_ioctl)(ifp, cmd, data));
763 
764 	return (EINVAL);
765 }
766 
767 static int
768 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
769 	struct sockaddr *dst, struct route *ro)
770 {
771 	struct lagg_port *lp = ifp->if_lagg;
772 	struct ether_header *eh;
773 	short type = 0;
774 
775 	switch (dst->sa_family) {
776 		case pseudo_AF_HDRCMPLT:
777 		case AF_UNSPEC:
778 			eh = (struct ether_header *)dst->sa_data;
779 			type = eh->ether_type;
780 			break;
781 	}
782 
783 	/*
784 	 * Only allow ethernet types required to initiate or maintain the link,
785 	 * aggregated frames take a different path.
786 	 */
787 	switch (ntohs(type)) {
788 		case ETHERTYPE_PAE:	/* EAPOL PAE/802.1x */
789 			return ((*lp->lp_output)(ifp, m, dst, ro));
790 	}
791 
792 	/* drop any other frames */
793 	m_freem(m);
794 	return (EBUSY);
795 }
796 
797 static void
798 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
799 {
800 	struct lagg_port *lp;
801 	struct lagg_softc *sc;
802 
803 	if ((lp = ifp->if_lagg) == NULL)
804 		return;
805 
806 	sc = lp->lp_softc;
807 
808 	LAGG_WLOCK(sc);
809 	lp->lp_detaching = 1;
810 	lagg_port_destroy(lp, 1);
811 	LAGG_WUNLOCK(sc);
812 }
813 
814 static void
815 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
816 {
817 	struct lagg_softc *sc = lp->lp_softc;
818 
819 	strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
820 	strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
821 	rp->rp_prio = lp->lp_prio;
822 	rp->rp_flags = lp->lp_flags;
823 	if (sc->sc_portreq != NULL)
824 		(*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
825 
826 	/* Add protocol specific flags */
827 	switch (sc->sc_proto) {
828 		case LAGG_PROTO_FAILOVER:
829 			if (lp == sc->sc_primary)
830 				rp->rp_flags |= LAGG_PORT_MASTER;
831 			if (lp == lagg_link_active(sc, sc->sc_primary))
832 				rp->rp_flags |= LAGG_PORT_ACTIVE;
833 			break;
834 
835 		case LAGG_PROTO_ROUNDROBIN:
836 		case LAGG_PROTO_LOADBALANCE:
837 		case LAGG_PROTO_ETHERCHANNEL:
838 			if (LAGG_PORTACTIVE(lp))
839 				rp->rp_flags |= LAGG_PORT_ACTIVE;
840 			break;
841 
842 		case LAGG_PROTO_LACP:
843 			/* LACP has a different definition of active */
844 			if (lacp_isactive(lp))
845 				rp->rp_flags |= LAGG_PORT_ACTIVE;
846 			if (lacp_iscollecting(lp))
847 				rp->rp_flags |= LAGG_PORT_COLLECTING;
848 			if (lacp_isdistributing(lp))
849 				rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
850 			break;
851 	}
852 
853 }
854 
855 static void
856 lagg_init(void *xsc)
857 {
858 	struct lagg_softc *sc = (struct lagg_softc *)xsc;
859 	struct lagg_port *lp;
860 	struct ifnet *ifp = sc->sc_ifp;
861 
862 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
863 		return;
864 
865 	LAGG_WLOCK(sc);
866 
867 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
868 	/* Update the port lladdrs */
869 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
870 		lagg_port_lladdr(lp, IF_LLADDR(ifp));
871 
872 	if (sc->sc_init != NULL)
873 		(*sc->sc_init)(sc);
874 
875 	LAGG_WUNLOCK(sc);
876 }
877 
878 static void
879 lagg_stop(struct lagg_softc *sc)
880 {
881 	struct ifnet *ifp = sc->sc_ifp;
882 
883 	LAGG_WLOCK_ASSERT(sc);
884 
885 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
886 		return;
887 
888 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
889 
890 	if (sc->sc_stop != NULL)
891 		(*sc->sc_stop)(sc);
892 }
893 
894 static int
895 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
896 {
897 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
898 	struct lagg_reqall *ra = (struct lagg_reqall *)data;
899 	struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
900 	struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
901 	struct ifreq *ifr = (struct ifreq *)data;
902 	struct lagg_port *lp;
903 	struct ifnet *tpif;
904 	struct thread *td = curthread;
905 	char *buf, *outbuf;
906 	int count, buflen, len, error = 0;
907 
908 	bzero(&rpbuf, sizeof(rpbuf));
909 
910 	switch (cmd) {
911 	case SIOCGLAGG:
912 		LAGG_RLOCK(sc);
913 		count = 0;
914 		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
915 			count++;
916 		buflen = count * sizeof(struct lagg_reqport);
917 		LAGG_RUNLOCK(sc);
918 
919 		outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
920 
921 		LAGG_RLOCK(sc);
922 		ra->ra_proto = sc->sc_proto;
923 		if (sc->sc_req != NULL)
924 			(*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
925 
926 		count = 0;
927 		buf = outbuf;
928 		len = min(ra->ra_size, buflen);
929 		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
930 			if (len < sizeof(rpbuf))
931 				break;
932 
933 			lagg_port2req(lp, &rpbuf);
934 			memcpy(buf, &rpbuf, sizeof(rpbuf));
935 			count++;
936 			buf += sizeof(rpbuf);
937 			len -= sizeof(rpbuf);
938 		}
939 		LAGG_RUNLOCK(sc);
940 		ra->ra_ports = count;
941 		ra->ra_size = count * sizeof(rpbuf);
942 		error = copyout(outbuf, ra->ra_port, ra->ra_size);
943 		free(outbuf, M_TEMP);
944 		break;
945 	case SIOCSLAGG:
946 		error = priv_check(td, PRIV_NET_LAGG);
947 		if (error)
948 			break;
949 		if (ra->ra_proto >= LAGG_PROTO_MAX) {
950 			error = EPROTONOSUPPORT;
951 			break;
952 		}
953 		if (sc->sc_proto != LAGG_PROTO_NONE) {
954 			LAGG_WLOCK(sc);
955 			error = sc->sc_detach(sc);
956 			/* Reset protocol and pointers */
957 			sc->sc_proto = LAGG_PROTO_NONE;
958 			sc->sc_detach = NULL;
959 			sc->sc_start = NULL;
960 			sc->sc_input = NULL;
961 			sc->sc_port_create = NULL;
962 			sc->sc_port_destroy = NULL;
963 			sc->sc_linkstate = NULL;
964 			sc->sc_init = NULL;
965 			sc->sc_stop = NULL;
966 			sc->sc_lladdr = NULL;
967 			sc->sc_req = NULL;
968 			sc->sc_portreq = NULL;
969 			LAGG_WUNLOCK(sc);
970 		}
971 		if (error != 0)
972 			break;
973 		for (int i = 0; i < (sizeof(lagg_protos) /
974 		    sizeof(lagg_protos[0])); i++) {
975 			if (lagg_protos[i].ti_proto == ra->ra_proto) {
976 				if (sc->sc_ifflags & IFF_DEBUG)
977 					printf("%s: using proto %u\n",
978 					    sc->sc_ifname,
979 					    lagg_protos[i].ti_proto);
980 				LAGG_WLOCK(sc);
981 				sc->sc_proto = lagg_protos[i].ti_proto;
982 				if (sc->sc_proto != LAGG_PROTO_NONE)
983 					error = lagg_protos[i].ti_attach(sc);
984 				LAGG_WUNLOCK(sc);
985 				return (error);
986 			}
987 		}
988 		error = EPROTONOSUPPORT;
989 		break;
990 	case SIOCGLAGGFLAGS:
991 		rf->rf_flags = sc->sc_flags;
992 		break;
993 	case SIOCSLAGGHASH:
994 		error = priv_check(td, PRIV_NET_LAGG);
995 		if (error)
996 			break;
997 		if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
998 			error = EINVAL;
999 			break;
1000 		}
1001 		LAGG_WLOCK(sc);
1002 		sc->sc_flags &= ~LAGG_F_HASHMASK;
1003 		sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1004 		LAGG_WUNLOCK(sc);
1005 		break;
1006 	case SIOCGLAGGPORT:
1007 		if (rp->rp_portname[0] == '\0' ||
1008 		    (tpif = ifunit(rp->rp_portname)) == NULL) {
1009 			error = EINVAL;
1010 			break;
1011 		}
1012 
1013 		LAGG_RLOCK(sc);
1014 		if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1015 		    lp->lp_softc != sc) {
1016 			error = ENOENT;
1017 			LAGG_RUNLOCK(sc);
1018 			break;
1019 		}
1020 
1021 		lagg_port2req(lp, rp);
1022 		LAGG_RUNLOCK(sc);
1023 		break;
1024 	case SIOCSLAGGPORT:
1025 		error = priv_check(td, PRIV_NET_LAGG);
1026 		if (error)
1027 			break;
1028 		if (rp->rp_portname[0] == '\0' ||
1029 		    (tpif = ifunit(rp->rp_portname)) == NULL) {
1030 			error = EINVAL;
1031 			break;
1032 		}
1033 		LAGG_WLOCK(sc);
1034 		error = lagg_port_create(sc, tpif);
1035 		LAGG_WUNLOCK(sc);
1036 		break;
1037 	case SIOCSLAGGDELPORT:
1038 		error = priv_check(td, PRIV_NET_LAGG);
1039 		if (error)
1040 			break;
1041 		if (rp->rp_portname[0] == '\0' ||
1042 		    (tpif = ifunit(rp->rp_portname)) == NULL) {
1043 			error = EINVAL;
1044 			break;
1045 		}
1046 
1047 		LAGG_WLOCK(sc);
1048 		if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1049 		    lp->lp_softc != sc) {
1050 			error = ENOENT;
1051 			LAGG_WUNLOCK(sc);
1052 			break;
1053 		}
1054 
1055 		error = lagg_port_destroy(lp, 1);
1056 		LAGG_WUNLOCK(sc);
1057 		break;
1058 	case SIOCSIFFLAGS:
1059 		/* Set flags on ports too */
1060 		LAGG_WLOCK(sc);
1061 		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1062 			lagg_setflags(lp, 1);
1063 		}
1064 		LAGG_WUNLOCK(sc);
1065 
1066 		if (!(ifp->if_flags & IFF_UP) &&
1067 		    (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1068 			/*
1069 			 * If interface is marked down and it is running,
1070 			 * then stop and disable it.
1071 			 */
1072 			LAGG_WLOCK(sc);
1073 			lagg_stop(sc);
1074 			LAGG_WUNLOCK(sc);
1075 		} else if ((ifp->if_flags & IFF_UP) &&
1076 		    !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1077 			/*
1078 			 * If interface is marked up and it is stopped, then
1079 			 * start it.
1080 			 */
1081 			(*ifp->if_init)(sc);
1082 		}
1083 		break;
1084 	case SIOCADDMULTI:
1085 	case SIOCDELMULTI:
1086 		LAGG_WLOCK(sc);
1087 		error = lagg_ether_setmulti(sc);
1088 		LAGG_WUNLOCK(sc);
1089 		break;
1090 	case SIOCSIFMEDIA:
1091 	case SIOCGIFMEDIA:
1092 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1093 		break;
1094 
1095 	case SIOCSIFCAP:
1096 	case SIOCSIFMTU:
1097 		/* Do not allow the MTU or caps to be directly changed */
1098 		error = EINVAL;
1099 		break;
1100 
1101 	default:
1102 		error = ether_ioctl(ifp, cmd, data);
1103 		break;
1104 	}
1105 	return (error);
1106 }
1107 
1108 static int
1109 lagg_ether_setmulti(struct lagg_softc *sc)
1110 {
1111 	struct lagg_port *lp;
1112 
1113 	LAGG_WLOCK_ASSERT(sc);
1114 
1115 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1116 		/* First, remove any existing filter entries. */
1117 		lagg_ether_cmdmulti(lp, 0);
1118 		/* copy all addresses from the lagg interface to the port */
1119 		lagg_ether_cmdmulti(lp, 1);
1120 	}
1121 	return (0);
1122 }
1123 
1124 static int
1125 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1126 {
1127 	struct lagg_softc *sc = lp->lp_softc;
1128 	struct ifnet *ifp = lp->lp_ifp;
1129 	struct ifnet *scifp = sc->sc_ifp;
1130 	struct lagg_mc *mc;
1131 	struct ifmultiaddr *ifma, *rifma = NULL;
1132 	struct sockaddr_dl sdl;
1133 	int error;
1134 
1135 	LAGG_WLOCK_ASSERT(sc);
1136 
1137 	bzero((char *)&sdl, sizeof(sdl));
1138 	sdl.sdl_len = sizeof(sdl);
1139 	sdl.sdl_family = AF_LINK;
1140 	sdl.sdl_type = IFT_ETHER;
1141 	sdl.sdl_alen = ETHER_ADDR_LEN;
1142 	sdl.sdl_index = ifp->if_index;
1143 
1144 	if (set) {
1145 		TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1146 			if (ifma->ifma_addr->sa_family != AF_LINK)
1147 				continue;
1148 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1149 			    LLADDR(&sdl), ETHER_ADDR_LEN);
1150 
1151 			error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1152 			if (error)
1153 				return (error);
1154 			mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1155 			if (mc == NULL)
1156 				return (ENOMEM);
1157 			mc->mc_ifma = rifma;
1158 			SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1159 		}
1160 	} else {
1161 		while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1162 			SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1163 			if_delmulti_ifma(mc->mc_ifma);
1164 			free(mc, M_DEVBUF);
1165 		}
1166 	}
1167 	return (0);
1168 }
1169 
1170 /* Handle a ref counted flag that should be set on the lagg port as well */
1171 static int
1172 lagg_setflag(struct lagg_port *lp, int flag, int status,
1173 	     int (*func)(struct ifnet *, int))
1174 {
1175 	struct lagg_softc *sc = lp->lp_softc;
1176 	struct ifnet *scifp = sc->sc_ifp;
1177 	struct ifnet *ifp = lp->lp_ifp;
1178 	int error;
1179 
1180 	LAGG_WLOCK_ASSERT(sc);
1181 
1182 	status = status ? (scifp->if_flags & flag) : 0;
1183 	/* Now "status" contains the flag value or 0 */
1184 
1185 	/*
1186 	 * See if recorded ports status is different from what
1187 	 * we want it to be.  If it is, flip it.  We record ports
1188 	 * status in lp_ifflags so that we won't clear ports flag
1189 	 * we haven't set.  In fact, we don't clear or set ports
1190 	 * flags directly, but get or release references to them.
1191 	 * That's why we can be sure that recorded flags still are
1192 	 * in accord with actual ports flags.
1193 	 */
1194 	if (status != (lp->lp_ifflags & flag)) {
1195 		error = (*func)(ifp, status);
1196 		if (error)
1197 			return (error);
1198 		lp->lp_ifflags &= ~flag;
1199 		lp->lp_ifflags |= status;
1200 	}
1201 	return (0);
1202 }
1203 
1204 /*
1205  * Handle IFF_* flags that require certain changes on the lagg port
1206  * if "status" is true, update ports flags respective to the lagg
1207  * if "status" is false, forcedly clear the flags set on port.
1208  */
1209 static int
1210 lagg_setflags(struct lagg_port *lp, int status)
1211 {
1212 	int error, i;
1213 
1214 	for (i = 0; lagg_pflags[i].flag; i++) {
1215 		error = lagg_setflag(lp, lagg_pflags[i].flag,
1216 		    status, lagg_pflags[i].func);
1217 		if (error)
1218 			return (error);
1219 	}
1220 	return (0);
1221 }
1222 
1223 static void
1224 lagg_start(struct ifnet *ifp)
1225 {
1226 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1227 	struct mbuf *m;
1228 	int error = 0;
1229 
1230 	LAGG_RLOCK(sc);
1231 	/* We need a Tx algorithm and at least one port */
1232 	if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1233 		IF_DRAIN(&ifp->if_snd);
1234 		LAGG_RUNLOCK(sc);
1235 		return;
1236 	}
1237 
1238 	for (;; error = 0) {
1239 		IFQ_DEQUEUE(&ifp->if_snd, m);
1240 		if (m == NULL)
1241 			break;
1242 
1243 		ETHER_BPF_MTAP(ifp, m);
1244 
1245 		error = (*sc->sc_start)(sc, m);
1246 		if (error == 0)
1247 			ifp->if_opackets++;
1248 		else
1249 			ifp->if_oerrors++;
1250 	}
1251 	LAGG_RUNLOCK(sc);
1252 }
1253 
1254 static struct mbuf *
1255 lagg_input(struct ifnet *ifp, struct mbuf *m)
1256 {
1257 	struct lagg_port *lp = ifp->if_lagg;
1258 	struct lagg_softc *sc = lp->lp_softc;
1259 	struct ifnet *scifp = sc->sc_ifp;
1260 
1261 	LAGG_RLOCK(sc);
1262 	if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1263 	    (lp->lp_flags & LAGG_PORT_DISABLED) ||
1264 	    sc->sc_proto == LAGG_PROTO_NONE) {
1265 		LAGG_RUNLOCK(sc);
1266 		m_freem(m);
1267 		return (NULL);
1268 	}
1269 
1270 	ETHER_BPF_MTAP(scifp, m);
1271 
1272 	m = (*sc->sc_input)(sc, lp, m);
1273 
1274 	if (m != NULL) {
1275 		scifp->if_ipackets++;
1276 		scifp->if_ibytes += m->m_pkthdr.len;
1277 
1278 		if (scifp->if_flags & IFF_MONITOR) {
1279 			m_freem(m);
1280 			m = NULL;
1281 		}
1282 	}
1283 
1284 	LAGG_RUNLOCK(sc);
1285 	return (m);
1286 }
1287 
1288 static int
1289 lagg_media_change(struct ifnet *ifp)
1290 {
1291 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1292 
1293 	if (sc->sc_ifflags & IFF_DEBUG)
1294 		printf("%s\n", __func__);
1295 
1296 	/* Ignore */
1297 	return (0);
1298 }
1299 
1300 static void
1301 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1302 {
1303 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1304 	struct lagg_port *lp;
1305 
1306 	imr->ifm_status = IFM_AVALID;
1307 	imr->ifm_active = IFM_ETHER | IFM_AUTO;
1308 
1309 	LAGG_RLOCK(sc);
1310 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1311 		if (LAGG_PORTACTIVE(lp))
1312 			imr->ifm_status |= IFM_ACTIVE;
1313 	}
1314 	LAGG_RUNLOCK(sc);
1315 }
1316 
1317 static void
1318 lagg_linkstate(struct lagg_softc *sc)
1319 {
1320 	struct lagg_port *lp;
1321 	int new_link = LINK_STATE_DOWN;
1322 	uint64_t speed;
1323 
1324 	/* Our link is considered up if at least one of our ports is active */
1325 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1326 		if (lp->lp_link_state == LINK_STATE_UP) {
1327 			new_link = LINK_STATE_UP;
1328 			break;
1329 		}
1330 	}
1331 	if_link_state_change(sc->sc_ifp, new_link);
1332 
1333 	/* Update if_baudrate to reflect the max possible speed */
1334 	switch (sc->sc_proto) {
1335 		case LAGG_PROTO_FAILOVER:
1336 			sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1337 			    sc->sc_primary->lp_ifp->if_baudrate : 0;
1338 			break;
1339 		case LAGG_PROTO_ROUNDROBIN:
1340 		case LAGG_PROTO_LOADBALANCE:
1341 		case LAGG_PROTO_ETHERCHANNEL:
1342 			speed = 0;
1343 			SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1344 				speed += lp->lp_ifp->if_baudrate;
1345 			sc->sc_ifp->if_baudrate = speed;
1346 			break;
1347 		case LAGG_PROTO_LACP:
1348 			/* LACP updates if_baudrate itself */
1349 			break;
1350 	}
1351 }
1352 
1353 static void
1354 lagg_port_state(struct ifnet *ifp, int state)
1355 {
1356 	struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1357 	struct lagg_softc *sc = NULL;
1358 
1359 	if (lp != NULL)
1360 		sc = lp->lp_softc;
1361 	if (sc == NULL)
1362 		return;
1363 
1364 	LAGG_WLOCK(sc);
1365 	lagg_linkstate(sc);
1366 	if (sc->sc_linkstate != NULL)
1367 		(*sc->sc_linkstate)(lp);
1368 	LAGG_WUNLOCK(sc);
1369 }
1370 
1371 struct lagg_port *
1372 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1373 {
1374 	struct lagg_port *lp_next, *rval = NULL;
1375 	// int new_link = LINK_STATE_DOWN;
1376 
1377 	LAGG_RLOCK_ASSERT(sc);
1378 	/*
1379 	 * Search a port which reports an active link state.
1380 	 */
1381 
1382 	if (lp == NULL)
1383 		goto search;
1384 	if (LAGG_PORTACTIVE(lp)) {
1385 		rval = lp;
1386 		goto found;
1387 	}
1388 	if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1389 	    LAGG_PORTACTIVE(lp_next)) {
1390 		rval = lp_next;
1391 		goto found;
1392 	}
1393 
1394 search:
1395 	SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1396 		if (LAGG_PORTACTIVE(lp_next)) {
1397 			rval = lp_next;
1398 			goto found;
1399 		}
1400 	}
1401 
1402 found:
1403 	if (rval != NULL) {
1404 		/*
1405 		 * The IEEE 802.1D standard assumes that a lagg with
1406 		 * multiple ports is always full duplex. This is valid
1407 		 * for load sharing laggs and if at least two links
1408 		 * are active. Unfortunately, checking the latter would
1409 		 * be too expensive at this point.
1410 		 XXX
1411 		if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1412 		    (sc->sc_count > 1))
1413 			new_link = LINK_STATE_FULL_DUPLEX;
1414 		else
1415 			new_link = rval->lp_link_state;
1416 		 */
1417 	}
1418 
1419 	return (rval);
1420 }
1421 
1422 static const void *
1423 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1424 {
1425 	if (m->m_pkthdr.len < (off + len)) {
1426 		return (NULL);
1427 	} else if (m->m_len < (off + len)) {
1428 		m_copydata(m, off, len, buf);
1429 		return (buf);
1430 	}
1431 	return (mtod(m, char *) + off);
1432 }
1433 
1434 uint32_t
1435 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1436 {
1437 	uint16_t etype;
1438 	uint32_t p = key;
1439 	int off;
1440 	struct ether_header *eh;
1441 	const struct ether_vlan_header *vlan;
1442 #ifdef INET
1443 	const struct ip *ip;
1444 	const uint32_t *ports;
1445 	int iphlen;
1446 #endif
1447 #ifdef INET6
1448 	const struct ip6_hdr *ip6;
1449 	uint32_t flow;
1450 #endif
1451 	union {
1452 #ifdef INET
1453 		struct ip ip;
1454 #endif
1455 #ifdef INET6
1456 		struct ip6_hdr ip6;
1457 #endif
1458 		struct ether_vlan_header vlan;
1459 		uint32_t port;
1460 	} buf;
1461 
1462 
1463 	off = sizeof(*eh);
1464 	if (m->m_len < off)
1465 		goto out;
1466 	eh = mtod(m, struct ether_header *);
1467 	etype = ntohs(eh->ether_type);
1468 	if (sc->sc_flags & LAGG_F_HASHL2) {
1469 		p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1470 		p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1471 	}
1472 
1473 	/* Special handling for encapsulating VLAN frames */
1474 	if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1475 		p = hash32_buf(&m->m_pkthdr.ether_vtag,
1476 		    sizeof(m->m_pkthdr.ether_vtag), p);
1477 	} else if (etype == ETHERTYPE_VLAN) {
1478 		vlan = lagg_gethdr(m, off,  sizeof(*vlan), &buf);
1479 		if (vlan == NULL)
1480 			goto out;
1481 
1482 		if (sc->sc_flags & LAGG_F_HASHL2)
1483 			p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1484 		etype = ntohs(vlan->evl_proto);
1485 		off += sizeof(*vlan) - sizeof(*eh);
1486 	}
1487 
1488 	switch (etype) {
1489 #ifdef INET
1490 	case ETHERTYPE_IP:
1491 		ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1492 		if (ip == NULL)
1493 			goto out;
1494 
1495 		if (sc->sc_flags & LAGG_F_HASHL3) {
1496 			p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1497 			p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1498 		}
1499 		if (!(sc->sc_flags & LAGG_F_HASHL4))
1500 			break;
1501 		switch (ip->ip_p) {
1502 			case IPPROTO_TCP:
1503 			case IPPROTO_UDP:
1504 			case IPPROTO_SCTP:
1505 				iphlen = ip->ip_hl << 2;
1506 				if (iphlen < sizeof(*ip))
1507 					break;
1508 				off += iphlen;
1509 				ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1510 				if (ports == NULL)
1511 					break;
1512 				p = hash32_buf(ports, sizeof(*ports), p);
1513 				break;
1514 		}
1515 		break;
1516 #endif
1517 #ifdef INET6
1518 	case ETHERTYPE_IPV6:
1519 		if (!(sc->sc_flags & LAGG_F_HASHL3))
1520 			break;
1521 		ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1522 		if (ip6 == NULL)
1523 			goto out;
1524 
1525 		p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1526 		p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1527 		flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1528 		p = hash32_buf(&flow, sizeof(flow), p);	/* IPv6 flow label */
1529 		break;
1530 #endif
1531 	}
1532 out:
1533 	return (p);
1534 }
1535 
1536 int
1537 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1538 {
1539 
1540 	return (ifp->if_transmit)(ifp, m);
1541 }
1542 
1543 /*
1544  * Simple round robin aggregation
1545  */
1546 
1547 static int
1548 lagg_rr_attach(struct lagg_softc *sc)
1549 {
1550 	sc->sc_detach = lagg_rr_detach;
1551 	sc->sc_start = lagg_rr_start;
1552 	sc->sc_input = lagg_rr_input;
1553 	sc->sc_port_create = NULL;
1554 	sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1555 	sc->sc_seq = 0;
1556 
1557 	return (0);
1558 }
1559 
1560 static int
1561 lagg_rr_detach(struct lagg_softc *sc)
1562 {
1563 	return (0);
1564 }
1565 
1566 static int
1567 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1568 {
1569 	struct lagg_port *lp;
1570 	uint32_t p;
1571 
1572 	p = atomic_fetchadd_32(&sc->sc_seq, 1);
1573 	p %= sc->sc_count;
1574 	lp = SLIST_FIRST(&sc->sc_ports);
1575 	while (p--)
1576 		lp = SLIST_NEXT(lp, lp_entries);
1577 
1578 	/*
1579 	 * Check the port's link state. This will return the next active
1580 	 * port if the link is down or the port is NULL.
1581 	 */
1582 	if ((lp = lagg_link_active(sc, lp)) == NULL) {
1583 		m_freem(m);
1584 		return (ENOENT);
1585 	}
1586 
1587 	/* Send mbuf */
1588 	return (lagg_enqueue(lp->lp_ifp, m));
1589 }
1590 
1591 static struct mbuf *
1592 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1593 {
1594 	struct ifnet *ifp = sc->sc_ifp;
1595 
1596 	/* Just pass in the packet to our lagg device */
1597 	m->m_pkthdr.rcvif = ifp;
1598 
1599 	return (m);
1600 }
1601 
1602 /*
1603  * Active failover
1604  */
1605 
1606 static int
1607 lagg_fail_attach(struct lagg_softc *sc)
1608 {
1609 	sc->sc_detach = lagg_fail_detach;
1610 	sc->sc_start = lagg_fail_start;
1611 	sc->sc_input = lagg_fail_input;
1612 	sc->sc_port_create = NULL;
1613 	sc->sc_port_destroy = NULL;
1614 
1615 	return (0);
1616 }
1617 
1618 static int
1619 lagg_fail_detach(struct lagg_softc *sc)
1620 {
1621 	return (0);
1622 }
1623 
1624 static int
1625 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1626 {
1627 	struct lagg_port *lp;
1628 
1629 	/* Use the master port if active or the next available port */
1630 	if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1631 		m_freem(m);
1632 		return (ENOENT);
1633 	}
1634 
1635 	/* Send mbuf */
1636 	return (lagg_enqueue(lp->lp_ifp, m));
1637 }
1638 
1639 static struct mbuf *
1640 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1641 {
1642 	struct ifnet *ifp = sc->sc_ifp;
1643 	struct lagg_port *tmp_tp;
1644 
1645 	if (lp == sc->sc_primary || lagg_failover_rx_all) {
1646 		m->m_pkthdr.rcvif = ifp;
1647 		return (m);
1648 	}
1649 
1650 	if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1651 		tmp_tp = lagg_link_active(sc, sc->sc_primary);
1652 		/*
1653 		 * If tmp_tp is null, we've recieved a packet when all
1654 		 * our links are down. Weird, but process it anyways.
1655 		 */
1656 		if ((tmp_tp == NULL || tmp_tp == lp)) {
1657 			m->m_pkthdr.rcvif = ifp;
1658 			return (m);
1659 		}
1660 	}
1661 
1662 	m_freem(m);
1663 	return (NULL);
1664 }
1665 
1666 /*
1667  * Loadbalancing
1668  */
1669 
1670 static int
1671 lagg_lb_attach(struct lagg_softc *sc)
1672 {
1673 	struct lagg_port *lp;
1674 	struct lagg_lb *lb;
1675 
1676 	if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1677 	    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1678 		return (ENOMEM);
1679 
1680 	sc->sc_detach = lagg_lb_detach;
1681 	sc->sc_start = lagg_lb_start;
1682 	sc->sc_input = lagg_lb_input;
1683 	sc->sc_port_create = lagg_lb_port_create;
1684 	sc->sc_port_destroy = lagg_lb_port_destroy;
1685 	sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1686 
1687 	lb->lb_key = arc4random();
1688 	sc->sc_psc = (caddr_t)lb;
1689 
1690 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1691 		lagg_lb_port_create(lp);
1692 
1693 	return (0);
1694 }
1695 
1696 static int
1697 lagg_lb_detach(struct lagg_softc *sc)
1698 {
1699 	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1700 	if (lb != NULL)
1701 		free(lb, M_DEVBUF);
1702 	return (0);
1703 }
1704 
1705 static int
1706 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1707 {
1708 	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1709 	struct lagg_port *lp_next;
1710 	int i = 0;
1711 
1712 	bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1713 	SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1714 		if (lp_next == lp)
1715 			continue;
1716 		if (i >= LAGG_MAX_PORTS)
1717 			return (EINVAL);
1718 		if (sc->sc_ifflags & IFF_DEBUG)
1719 			printf("%s: port %s at index %d\n",
1720 			    sc->sc_ifname, lp_next->lp_ifname, i);
1721 		lb->lb_ports[i++] = lp_next;
1722 	}
1723 
1724 	return (0);
1725 }
1726 
1727 static int
1728 lagg_lb_port_create(struct lagg_port *lp)
1729 {
1730 	struct lagg_softc *sc = lp->lp_softc;
1731 	return (lagg_lb_porttable(sc, NULL));
1732 }
1733 
1734 static void
1735 lagg_lb_port_destroy(struct lagg_port *lp)
1736 {
1737 	struct lagg_softc *sc = lp->lp_softc;
1738 	lagg_lb_porttable(sc, lp);
1739 }
1740 
1741 static int
1742 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1743 {
1744 	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1745 	struct lagg_port *lp = NULL;
1746 	uint32_t p = 0;
1747 
1748 	if (sc->use_flowid && (m->m_flags & M_FLOWID))
1749 		p = m->m_pkthdr.flowid;
1750 	else
1751 		p = lagg_hashmbuf(sc, m, lb->lb_key);
1752 	p %= sc->sc_count;
1753 	lp = lb->lb_ports[p];
1754 
1755 	/*
1756 	 * Check the port's link state. This will return the next active
1757 	 * port if the link is down or the port is NULL.
1758 	 */
1759 	if ((lp = lagg_link_active(sc, lp)) == NULL) {
1760 		m_freem(m);
1761 		return (ENOENT);
1762 	}
1763 
1764 	/* Send mbuf */
1765 	return (lagg_enqueue(lp->lp_ifp, m));
1766 }
1767 
1768 static struct mbuf *
1769 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1770 {
1771 	struct ifnet *ifp = sc->sc_ifp;
1772 
1773 	/* Just pass in the packet to our lagg device */
1774 	m->m_pkthdr.rcvif = ifp;
1775 
1776 	return (m);
1777 }
1778 
1779 /*
1780  * 802.3ad LACP
1781  */
1782 
1783 static int
1784 lagg_lacp_attach(struct lagg_softc *sc)
1785 {
1786 	struct lagg_port *lp;
1787 	int error;
1788 
1789 	sc->sc_detach = lagg_lacp_detach;
1790 	sc->sc_port_create = lacp_port_create;
1791 	sc->sc_port_destroy = lacp_port_destroy;
1792 	sc->sc_linkstate = lacp_linkstate;
1793 	sc->sc_start = lagg_lacp_start;
1794 	sc->sc_input = lagg_lacp_input;
1795 	sc->sc_init = lacp_init;
1796 	sc->sc_stop = lacp_stop;
1797 	sc->sc_lladdr = lagg_lacp_lladdr;
1798 	sc->sc_req = lacp_req;
1799 	sc->sc_portreq = lacp_portreq;
1800 
1801 	error = lacp_attach(sc);
1802 	if (error)
1803 		return (error);
1804 
1805 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1806 		lacp_port_create(lp);
1807 
1808 	return (error);
1809 }
1810 
1811 static int
1812 lagg_lacp_detach(struct lagg_softc *sc)
1813 {
1814 	struct lagg_port *lp;
1815 	int error;
1816 
1817 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1818 		lacp_port_destroy(lp);
1819 
1820 	/* unlocking is safe here */
1821 	LAGG_WUNLOCK(sc);
1822 	error = lacp_detach(sc);
1823 	LAGG_WLOCK(sc);
1824 
1825 	return (error);
1826 }
1827 
1828 static void
1829 lagg_lacp_lladdr(struct lagg_softc *sc)
1830 {
1831 	struct lagg_port *lp;
1832 
1833 	/* purge all the lacp ports */
1834 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1835 		lacp_port_destroy(lp);
1836 
1837 	/* add them back in */
1838 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1839 		lacp_port_create(lp);
1840 }
1841 
1842 static int
1843 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1844 {
1845 	struct lagg_port *lp;
1846 
1847 	lp = lacp_select_tx_port(sc, m);
1848 	if (lp == NULL) {
1849 		m_freem(m);
1850 		return (EBUSY);
1851 	}
1852 
1853 	/* Send mbuf */
1854 	return (lagg_enqueue(lp->lp_ifp, m));
1855 }
1856 
1857 static struct mbuf *
1858 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1859 {
1860 	struct ifnet *ifp = sc->sc_ifp;
1861 	struct ether_header *eh;
1862 	u_short etype;
1863 
1864 	eh = mtod(m, struct ether_header *);
1865 	etype = ntohs(eh->ether_type);
1866 
1867 	/* Tap off LACP control messages */
1868 	if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
1869 		m = lacp_input(lp, m);
1870 		if (m == NULL)
1871 			return (NULL);
1872 	}
1873 
1874 	/*
1875 	 * If the port is not collecting or not in the active aggregator then
1876 	 * free and return.
1877 	 */
1878 	if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1879 		m_freem(m);
1880 		return (NULL);
1881 	}
1882 
1883 	m->m_pkthdr.rcvif = ifp;
1884 	return (m);
1885 }
1886