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