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