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