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