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