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