Add a new macro IF_LLSOCKADDR which maps a ifnet pointer to the
[dragonfly.git] / sys / netgraph / fec / ng_fec.c
1 /*
2  * ng_fec.c
3  *
4  * Copyright (c) 2001 Berkeley Software Design, Inc.
5  * Copyright (c) 2000, 2001
6  *      Bill Paul <wpaul@osd.bsdi.com>.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Bill Paul.
19  * 4. Neither the name of the author nor the names of any co-contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33  * THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  * $FreeBSD: src/sys/netgraph/ng_fec.c,v 1.1.2.1 2002/11/01 21:39:31 julian Exp $
36  * $DragonFly: src/sys/netgraph/fec/ng_fec.c,v 1.14 2005/06/03 23:23:03 joerg Exp $
37  */
38 /*
39  * Copyright (c) 1996-1999 Whistle Communications, Inc.
40  * All rights reserved.
41  * 
42  * Subject to the following obligations and disclaimer of warranty, use and
43  * redistribution of this software, in source or object code forms, with or
44  * without modifications are expressly permitted by Whistle Communications;
45  * provided, however, that:
46  * 1. Any and all reproductions of the source or object code must include the
47  *    copyright notice above and the following disclaimer of warranties; and
48  * 2. No rights are granted, in any manner or form, to use Whistle
49  *    Communications, Inc. trademarks, including the mark "WHISTLE
50  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
51  *    such appears in the above copyright notice or in the software.
52  * 
53  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
54  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
55  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
56  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
57  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
58  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
59  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
60  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
61  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
62  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
63  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
64  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
65  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
66  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
69  * OF SUCH DAMAGE.
70  *
71  * Author: Archie Cobbs <archie@freebsd.org>
72  *
73  * $Whistle: ng_fec.c,v 1.33 1999/11/01 09:24:51 julian Exp $
74  */
75
76 /*
77  * This module implements ethernet channel bonding using the Cisco
78  * Fast EtherChannel mechanism. Two or four ports may be combined
79  * into a single aggregate interface.
80  *
81  * Interfaces are named fec0, fec1, etc.  New nodes take the
82  * first available interface name.
83  *
84  * This node also includes Berkeley packet filter support.
85  *
86  * Note that this node doesn't need to connect to any other
87  * netgraph nodes in order to do its work.
88  */
89
90 #include <sys/param.h>
91 #include <sys/systm.h>
92 #include <sys/errno.h>
93 #include <sys/kernel.h>
94 #include <sys/malloc.h>
95 #include <sys/mbuf.h>
96 #include <sys/errno.h>
97 #include <sys/sockio.h>
98 #include <sys/socket.h>
99 #include <sys/syslog.h>
100 #include <sys/libkern.h>
101 #include <sys/queue.h>
102 #include <sys/thread2.h>
103
104 #include <net/if.h>
105 #include <net/if_types.h>
106 #include <net/if_arp.h>
107 #include <net/if_dl.h>
108 #include <net/if_media.h>
109 #include <net/intrq.h>
110 #include <net/bpf.h>
111 #include <net/ethernet.h>
112
113 #include "opt_inet.h"
114 #include "opt_inet6.h"
115
116 #include <netinet/in.h>
117 #ifdef INET
118 #include <netinet/in_systm.h>
119 #include <netinet/ip.h>
120 #endif
121
122 #ifdef INET6
123 #include <netinet/ip6.h>
124 #endif
125
126 #include <netgraph/ng_message.h>
127 #include <netgraph/netgraph.h>
128 #include <netgraph/ng_parse.h>
129 #include "ng_fec.h"
130
131 #define IFP2NG(ifp)  ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph)
132 #define FEC_INC(x, y)   (x) = (x + 1) % y
133
134 /*
135  * Current fast etherchannel implementations use either 2 or 4
136  * ports, so for now we limit the maximum bundle size to 4 interfaces.
137  */
138 #define FEC_BUNDLESIZ   4
139
140 struct ng_fec_portlist {
141         struct ifnet            *fec_if;
142         int                     fec_idx;
143         int                     fec_ifstat;
144         struct ether_addr       fec_mac;
145         TAILQ_ENTRY(ng_fec_portlist) fec_list;
146 };
147
148 struct ng_fec_bundle {
149         TAILQ_HEAD(,ng_fec_portlist) ng_fec_ports;
150         int                     fec_ifcnt;
151         int                     fec_btype;
152 };
153
154 #define FEC_BTYPE_MAC           0x01
155 #define FEC_BTYPE_INET          0x02
156 #define FEC_BTYPE_INET6         0x03
157
158 /* Node private data */
159 struct ng_fec_private {
160         struct arpcom arpcom;
161         struct ifmedia ifmedia;
162         int     if_flags;
163         int     if_error;               /* XXX */
164         int     unit;                   /* Interface unit number */
165         node_p  node;                   /* Our netgraph node */
166         struct ng_fec_bundle fec_bundle;/* Aggregate bundle */
167         struct callout fec_timeout;     /* callout for ticker */
168         int     (*real_if_output)(struct ifnet *, struct mbuf *,
169                                   struct sockaddr *, struct rtentry *);
170 };
171 typedef struct ng_fec_private *priv_p;
172
173 /* Interface methods */
174 static void     ng_fec_input(struct ifnet *, struct mbuf **,
175                         struct ether_header *);
176 static void     ng_fec_start(struct ifnet *ifp);
177 static int      ng_fec_choose_port(struct ng_fec_bundle *b,
178                         struct mbuf *m, struct ifnet **ifp);
179 static int      ng_fec_setport(struct ifnet *ifp, u_long cmd, caddr_t data);
180 static void     ng_fec_init(void *arg);
181 static void     ng_fec_stop(struct ifnet *ifp);
182 static int      ng_fec_ifmedia_upd(struct ifnet *ifp);
183 static void     ng_fec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
184 static int      ng_fec_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
185                              struct ucred *);
186 static int      ng_fec_output(struct ifnet *ifp, struct mbuf *m0,
187                         struct sockaddr *dst, struct rtentry *rt0);
188 static void     ng_fec_tick(void *arg);
189 static int      ng_fec_addport(struct ng_fec_private *priv, char *iface);
190 static int      ng_fec_delport(struct ng_fec_private *priv, char *iface);
191
192 #ifdef DEBUG
193 static void     ng_fec_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
194 #endif
195
196 /* Netgraph methods */
197 static ng_constructor_t ng_fec_constructor;
198 static ng_rcvmsg_t      ng_fec_rcvmsg;
199 static ng_shutdown_t    ng_fec_rmnode;
200
201 /* List of commands and how to convert arguments to/from ASCII */
202 static const struct ng_cmdlist ng_fec_cmds[] = {
203         {
204           NGM_FEC_COOKIE,
205           NGM_FEC_ADD_IFACE,
206           "add_iface",
207           &ng_parse_string_type,
208           NULL,
209         },
210         {
211           NGM_FEC_COOKIE,
212           NGM_FEC_DEL_IFACE,
213           "del_iface",
214           &ng_parse_string_type,
215           NULL,
216         },
217         {
218           NGM_FEC_COOKIE,
219           NGM_FEC_SET_MODE_MAC,
220           "set_mode_mac",
221           NULL,
222           NULL,
223         },
224         {
225           NGM_FEC_COOKIE,
226           NGM_FEC_SET_MODE_INET,
227           "set_mode_inet",
228           NULL,
229           NULL,
230         },
231         { 0 }
232 };
233
234 /* Node type descriptor */
235 static struct ng_type typestruct = {
236         NG_VERSION,
237         NG_FEC_NODE_TYPE,
238         NULL,
239         ng_fec_constructor,
240         ng_fec_rcvmsg,
241         ng_fec_rmnode,
242         NULL,
243         NULL,
244         NULL,
245         NULL,
246         NULL,
247         NULL,
248         ng_fec_cmds
249 };
250 NETGRAPH_INIT(fec, &typestruct);
251
252 /* We keep a bitmap indicating which unit numbers are free.
253    One means the unit number is free, zero means it's taken. */
254 static int      *ng_fec_units = NULL;
255 static int      ng_fec_units_len = 0;
256 static int      ng_units_in_use = 0;
257
258 #define UNITS_BITSPERWORD       (sizeof(*ng_fec_units) * NBBY)
259
260 /*
261  * Find the first free unit number for a new interface.
262  * Increase the size of the unit bitmap as necessary.
263  */
264 static __inline__ int
265 ng_fec_get_unit(int *unit)
266 {
267         int index, bit;
268
269         for (index = 0; index < ng_fec_units_len
270             && ng_fec_units[index] == 0; index++);
271         if (index == ng_fec_units_len) {                /* extend array */
272                 int i, *newarray, newlen;
273
274                 newlen = (2 * ng_fec_units_len) + 4;
275                 MALLOC(newarray, int *, newlen * sizeof(*ng_fec_units),
276                     M_NETGRAPH, M_NOWAIT);
277                 if (newarray == NULL)
278                         return (ENOMEM);
279                 bcopy(ng_fec_units, newarray,
280                     ng_fec_units_len * sizeof(*ng_fec_units));
281                 for (i = ng_fec_units_len; i < newlen; i++)
282                         newarray[i] = ~0;
283                 if (ng_fec_units != NULL)
284                         FREE(ng_fec_units, M_NETGRAPH);
285                 ng_fec_units = newarray;
286                 ng_fec_units_len = newlen;
287         }
288         bit = ffs(ng_fec_units[index]) - 1;
289         KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
290             ("%s: word=%d bit=%d", __func__, ng_fec_units[index], bit));
291         ng_fec_units[index] &= ~(1 << bit);
292         *unit = (index * UNITS_BITSPERWORD) + bit;
293         ng_units_in_use++;
294         return (0);
295 }
296
297 /*
298  * Free a no longer needed unit number.
299  */
300 static __inline__ void
301 ng_fec_free_unit(int unit)
302 {
303         int index, bit;
304
305         index = unit / UNITS_BITSPERWORD;
306         bit = unit % UNITS_BITSPERWORD;
307         KASSERT(index < ng_fec_units_len,
308             ("%s: unit=%d len=%d", __func__, unit, ng_fec_units_len));
309         KASSERT((ng_fec_units[index] & (1 << bit)) == 0,
310             ("%s: unit=%d is free", __func__, unit));
311         ng_fec_units[index] |= (1 << bit);
312         /*
313          * XXX We could think about reducing the size of ng_fec_units[]
314          * XXX here if the last portion is all ones
315          * XXX At least free it if no more units.
316          * Needed if we are eventually be able to unload.
317          */
318         ng_units_in_use++;
319         if (ng_units_in_use == 0) { /* XXX make SMP safe */
320                 FREE(ng_fec_units, M_NETGRAPH);
321                 ng_fec_units_len = 0;
322                 ng_fec_units = NULL;
323         }
324 }
325
326 /************************************************************************
327                         INTERFACE STUFF
328  ************************************************************************/
329
330 static int
331 ng_fec_addport(struct ng_fec_private *priv, char *iface)
332 {
333         struct ng_fec_bundle    *b;
334         struct ifnet            *ifp, *bifp;
335         struct arpcom           *ac;
336         struct ifaddr           *ifa;
337         struct sockaddr_dl      *sdl;
338         struct ng_fec_portlist  *p, *new;
339
340         if (priv == NULL || iface == NULL)
341                 return(EINVAL);
342
343         b = &priv->fec_bundle;
344         ifp = &priv->arpcom.ac_if;
345
346         /* Find the interface */
347         bifp = ifunit(iface);
348         if (bifp == NULL) {
349                 printf("fec%d: tried to add iface %s, which "
350                     "doesn't seem to exist\n", priv->unit, iface);
351                 return(ENOENT);
352         }
353
354         /* See if we have room in the bundle */
355         if (b->fec_ifcnt == FEC_BUNDLESIZ) {
356                 printf("fec%d: can't add new iface; bundle is full\n",
357                     priv->unit);
358                 return(ENOSPC);
359         }
360
361         /* See if the interface is already in the bundle */
362         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
363                 if (p->fec_if == bifp) {
364                         printf("fec%d: iface %s is already in this "
365                             "bundle\n", priv->unit, iface);
366                         return(EINVAL);
367                 }
368         }
369
370         /* Allocate new list entry. */
371         MALLOC(new, struct ng_fec_portlist *,
372             sizeof(struct ng_fec_portlist), M_NETGRAPH, M_NOWAIT);
373         if (new == NULL)
374                 return(ENOMEM);
375
376         ac = (struct arpcom *)bifp;
377         ac->ac_netgraph = priv->node;
378
379         /*
380          * If this is the first interface added to the bundle,
381          * use its MAC address for the virtual interface (and,
382          * by extension, all the other ports in the bundle).
383          */
384         if (b->fec_ifcnt == 0) {
385                 ifa = ifnet_addrs[ifp->if_index - 1];
386                 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
387                 bcopy((char *)ac->ac_enaddr,
388                     priv->arpcom.ac_enaddr, ETHER_ADDR_LEN);
389                 bcopy((char *)ac->ac_enaddr,
390                     LLADDR(sdl), ETHER_ADDR_LEN);
391         }
392
393         b->fec_btype = FEC_BTYPE_MAC;
394         new->fec_idx = b->fec_ifcnt;
395         b->fec_ifcnt++;
396
397         /* Save the real MAC address. */
398         bcopy((char *)ac->ac_enaddr,
399             (char *)&new->fec_mac, ETHER_ADDR_LEN);
400
401         /* Set up phony MAC address. */
402         sdl = IF_LLSOCKADDR(bifp);
403         bcopy(priv->arpcom.ac_enaddr, ac->ac_enaddr, ETHER_ADDR_LEN);
404         bcopy(priv->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
405
406         /* Add to the queue */
407         new->fec_if = bifp;
408         TAILQ_INSERT_TAIL(&b->ng_fec_ports, new, fec_list);
409
410         return(0);
411 }
412
413 static int
414 ng_fec_delport(struct ng_fec_private *priv, char *iface)
415 {
416         struct ng_fec_bundle    *b;
417         struct ifnet            *ifp, *bifp;
418         struct arpcom           *ac;
419         struct sockaddr_dl      *sdl;
420         struct ng_fec_portlist  *p;
421
422         if (priv == NULL || iface == NULL)
423                 return(EINVAL);
424
425         b = &priv->fec_bundle;
426         ifp = &priv->arpcom.ac_if;
427
428         /* Find the interface */
429         bifp = ifunit(iface);
430         if (bifp == NULL) {
431                 printf("fec%d: tried to remove iface %s, which "
432                     "doesn't seem to exist\n", priv->unit, iface);
433                 return(ENOENT);
434         }
435
436         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
437                 if (p->fec_if == bifp)
438                         break;
439         }
440
441         if (p == NULL) {
442                 printf("fec%d: tried to remove iface %s which "
443                     "is not in our bundle\n", priv->unit, iface);
444                 return(EINVAL);
445         }
446
447         /* Stop interface */
448         bifp->if_flags &= ~IFF_UP;
449         (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL, NULL);
450
451         /* Restore MAC address. */
452         ac = (struct arpcom *)bifp;
453         sdl = IF_LLSOCKADDR(bifp);
454         bcopy((char *)&p->fec_mac, ac->ac_enaddr, ETHER_ADDR_LEN);
455         bcopy((char *)&p->fec_mac, LLADDR(sdl), ETHER_ADDR_LEN);
456
457         /* Delete port */
458         TAILQ_REMOVE(&b->ng_fec_ports, p, fec_list);
459         FREE(p, M_NETGRAPH);
460         b->fec_ifcnt--;
461
462         return(0);
463 }
464
465 /*
466  * Pass an ioctl command down to all the underyling interfaces in a
467  * bundle. Used for setting multicast filters and flags.
468  */
469
470 static int 
471 ng_fec_setport(struct ifnet *ifp, u_long command, caddr_t data)
472 {
473         struct ng_fec_private   *priv;
474         struct ng_fec_bundle    *b;
475         struct ifnet            *oifp;
476         struct ng_fec_portlist  *p;
477
478         priv = ifp->if_softc;
479         b = &priv->fec_bundle;
480
481         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
482                 oifp = p->fec_if;
483                 if (oifp != NULL)
484                         (*oifp->if_ioctl)(oifp, command, data, NULL);
485         }
486
487         return(0);
488 }
489
490 static void
491 ng_fec_init(void *arg)
492 {
493         struct ng_fec_private   *priv;
494         struct ng_fec_bundle    *b;
495         struct ifnet            *ifp, *bifp;
496         struct ng_fec_portlist  *p;
497
498         ifp = arg;
499         priv = ifp->if_softc;
500         b = &priv->fec_bundle;
501
502         if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
503                 printf("fec%d: invalid bundle "
504                     "size: %d\n", priv->unit,
505                     b->fec_ifcnt);
506                 return;
507         }
508
509         ng_fec_stop(ifp);
510
511         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
512                 bifp = p->fec_if;
513                 bifp->if_flags |= IFF_UP;
514                 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL, NULL);
515                 /* mark iface as up and let the monitor check it */
516                 p->fec_ifstat = -1;
517         }
518
519         callout_reset(&priv->fec_timeout, hz, ng_fec_tick, priv);
520 }
521
522 static void
523 ng_fec_stop(struct ifnet *ifp)
524 {
525         struct ng_fec_private   *priv;
526         struct ng_fec_bundle    *b;
527         struct ifnet            *bifp;
528         struct ng_fec_portlist  *p;
529
530         priv = ifp->if_softc;
531         b = &priv->fec_bundle;
532
533         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
534                 bifp = p->fec_if;
535                 bifp->if_flags &= ~IFF_UP;
536                 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL, NULL);
537         }
538
539         callout_stop(&priv->fec_timeout);
540 }
541
542 static void
543 ng_fec_tick(void *arg)
544 {
545         struct ng_fec_private   *priv;
546         struct ng_fec_bundle    *b;
547         struct ifmediareq       ifmr;
548         struct ifnet            *ifp;
549         struct ng_fec_portlist  *p;
550         int                     error = 0;
551
552         priv = arg;
553         b = &priv->fec_bundle;
554
555         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
556                 bzero((char *)&ifmr, sizeof(ifmr));
557                 ifp = p->fec_if;
558                 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr,
559                                          NULL);
560                 if (error) {
561                         printf("fec%d: failed to check status "
562                             "of link %s\n", priv->unit, ifp->if_xname);
563                         continue;
564                 }
565
566                 if (ifmr.ifm_status & IFM_AVALID &&
567                     IFM_TYPE(ifmr.ifm_active) == IFM_ETHER) {
568                         if (ifmr.ifm_status & IFM_ACTIVE) {
569                                 if (p->fec_ifstat == -1 ||
570                                     p->fec_ifstat == 0) {
571                                         p->fec_ifstat = 1;
572                                         printf("fec%d: port %s in bundle "
573                                             "is up\n", priv->unit,
574                                             ifp->if_xname);
575                                 }
576                         } else {
577                                 if (p->fec_ifstat == -1 ||
578                                     p->fec_ifstat == 1) {
579                                         p->fec_ifstat = 0;
580                                         printf("fec%d: port %s in bundle "
581                                             "is down\n", priv->unit,
582                                             ifp->if_xname);
583                                 }
584                         }
585                 }
586         }
587
588         ifp = &priv->arpcom.ac_if;
589         if (ifp->if_flags & IFF_RUNNING)
590                 callout_reset(&priv->fec_timeout, hz, ng_fec_tick, priv);
591 }
592
593 static int
594 ng_fec_ifmedia_upd(struct ifnet *ifp)
595 {
596         return(0);
597 }
598
599 static void ng_fec_ifmedia_sts(struct ifnet *ifp,
600         struct ifmediareq *ifmr)
601 {
602         struct ng_fec_private   *priv;
603         struct ng_fec_bundle    *b;
604         struct ng_fec_portlist  *p;
605
606         priv = ifp->if_softc;
607         b = &priv->fec_bundle;
608
609         ifmr->ifm_status = IFM_AVALID;
610         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
611                 if (p->fec_ifstat) {
612                         ifmr->ifm_status |= IFM_ACTIVE;
613                         break;
614                 }
615         }
616 }
617
618 /*
619  * Process an ioctl for the virtual interface
620  */
621 static int
622 ng_fec_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
623 {
624         struct ifreq *const ifr = (struct ifreq *) data;
625         int error = 0;
626         struct ng_fec_private   *priv;
627         struct ng_fec_bundle    *b;
628
629         priv = ifp->if_softc;
630         b = &priv->fec_bundle;
631
632 #ifdef DEBUG
633         ng_fec_print_ioctl(ifp, command, data);
634 #endif
635         crit_enter();
636         switch (command) {
637
638         /* These two are mostly handled at a higher layer */
639         case SIOCSIFADDR:
640         case SIOCGIFADDR:
641         case SIOCSIFMTU:
642                 error = ether_ioctl(ifp, command, data);
643                 break;
644
645         /* Set flags */
646         case SIOCSIFFLAGS:
647                 /*
648                  * If the interface is marked up and stopped, then start it.
649                  * If it is marked down and running, then stop it.
650                  */
651                 if (ifr->ifr_flags & IFF_UP) {
652                         if (!(ifp->if_flags & IFF_RUNNING)) {
653                                 /* Sanity. */
654                                 if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
655                                         printf("fec%d: invalid bundle "
656                                             "size: %d\n", priv->unit,
657                                             b->fec_ifcnt);
658                                         error = EINVAL;
659                                         break;
660                                 }
661                                 ifp->if_flags &= ~(IFF_OACTIVE);
662                                 ifp->if_flags |= IFF_RUNNING;
663                                 ng_fec_init(ifp);
664                         }
665                         /*
666                          * Bubble down changes in promisc mode to
667                          * underlying interfaces.
668                          */
669                         if ((ifp->if_flags & IFF_PROMISC) !=
670                             (priv->if_flags & IFF_PROMISC)) {
671                                 ng_fec_setport(ifp, command, data);
672                                 priv->if_flags = ifp->if_flags;
673                         }
674                 } else {
675                         if (ifp->if_flags & IFF_RUNNING)
676                                 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
677                         ng_fec_stop(ifp);
678                 }
679                 break;
680
681         case SIOCADDMULTI:
682         case SIOCDELMULTI:
683                 ng_fec_setport(ifp, command, data);
684                 error = 0;
685                 break;
686         case SIOCGIFMEDIA:
687         case SIOCSIFMEDIA:
688                 error = ifmedia_ioctl(ifp, ifr, &priv->ifmedia, command);
689                 break;
690         /* Stuff that's not supported */
691         case SIOCSIFPHYS:
692                 error = EOPNOTSUPP;
693                 break;
694
695         default:
696                 error = EINVAL;
697                 break;
698         }
699         crit_exit();
700         return (error);
701 }
702
703 /*
704  * This routine spies on mbufs passing through ether_input(). If
705  * they come from one of the interfaces that are aggregated into
706  * our bundle, we fix up the ifnet pointer and increment our
707  * packet counters so that it looks like the frames are actually
708  * coming from us.
709  */
710 static void 
711 ng_fec_input(struct ifnet *ifp, struct mbuf **m0,
712                 struct ether_header *eh)
713 {
714         struct ng_node          *node;
715         struct ng_fec_private   *priv;
716         struct ng_fec_bundle    *b;
717         struct mbuf             *m;
718         struct ifnet            *bifp;
719         struct ng_fec_portlist  *p;
720
721         /* Sanity check */
722         if (ifp == NULL || m0 == NULL || eh == NULL)
723                 return;
724
725         node = IFP2NG(ifp);
726
727         /* Sanity check part II */
728         if (node == NULL)
729                 return;
730
731         priv = node->private;
732         b = &priv->fec_bundle;
733         bifp = &priv->arpcom.ac_if;
734
735         m = *m0;
736         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
737                 if (p->fec_if == m->m_pkthdr.rcvif)
738                         break;
739         }
740
741         /* Wasn't meant for us; leave this frame alone. */
742         if (p == NULL)
743                 return;
744
745         /* Pretend this is our frame. */
746         m->m_pkthdr.rcvif = bifp;
747         bifp->if_ipackets++;
748         bifp->if_ibytes += m->m_pkthdr.len + sizeof(struct ether_header);
749
750         if (bifp->if_bpf)
751                 bpf_ptap(bifp->if_bpf, m, eh, ETHER_HDR_LEN);
752 }
753
754 /*
755  * Take a quick peek at the packet and see if it's ok for us to use
756  * the inet or inet6 hash methods on it, if they're enabled. We do
757  * this by setting flags in the mbuf header. Once we've made up our
758  * mind what to do, we pass the frame to ether_output() for further
759  * processing.
760  */
761
762 static int
763 ng_fec_output(struct ifnet *ifp, struct mbuf *m,
764                 struct sockaddr *dst, struct rtentry *rt0)
765 {
766         const priv_p priv = (priv_p) ifp->if_softc;
767         struct ng_fec_bundle *b;
768         int error;
769
770         /* Check interface flags */
771         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
772                 m_freem(m);
773                 return (ENETDOWN);
774         }
775
776         b = &priv->fec_bundle;
777
778         switch (b->fec_btype) {
779         case FEC_BTYPE_MAC:
780                 m->m_flags |= M_FEC_MAC;
781                 break;
782 #ifdef INET
783         case FEC_BTYPE_INET:
784                 /*
785                  * We can't use the INET address port selection
786                  * scheme if this isn't an INET packet.
787                  */
788                 if (dst->sa_family == AF_INET)
789                         m->m_flags |= M_FEC_INET;
790 #ifdef INET6
791                 else if (dst->sa_family == AF_INET6)
792                         m->m_flags |= M_FEC_INET6;
793 #endif
794                 else {
795 #ifdef DEBUG
796                         printf("%s: can't do inet aggregation of non "
797                             "inet packet\n", ifp->if_xname);
798 #endif
799                         m->m_flags |= M_FEC_MAC;
800                 }
801                 break;
802 #endif
803         default:
804                 printf("%s: bogus hash type: %d\n", ifp->if_xname,
805                     b->fec_btype);
806                 m_freem(m);
807                 return(EINVAL);
808                 break;
809         }
810
811         /*
812          * Pass the frame to ether_output() for all the protocol
813          * handling. This will put the ethernet header on the packet
814          * for us.
815          */
816         priv->if_error = 0;
817         error = priv->real_if_output(ifp, m, dst, rt0);
818         if (priv->if_error && !error)
819                 error = priv->if_error;
820
821         return(error);
822 }
823
824 /*
825  * Apply a hash to the source and destination addresses in the packet
826  * in order to select an interface. Also check link status and handle
827  * dead links accordingly.
828  */
829
830 static int
831 ng_fec_choose_port(struct ng_fec_bundle *b,
832         struct mbuf *m, struct ifnet **ifp)
833 {
834         struct ether_header     *eh;
835         struct mbuf             *m0;
836 #ifdef INET
837         struct ip               *ip;
838 #ifdef INET6
839         struct ip6_hdr          *ip6;
840 #endif
841 #endif
842
843         struct ng_fec_portlist  *p;
844         int                     port = 0, mask;
845
846         /*
847          * If there are only two ports, mask off all but the
848          * last bit for XORing. If there are 4, mask off all
849          * but the last 2 bits.
850          */
851         mask = b->fec_ifcnt == 2 ? 0x1 : 0x3;
852         eh = mtod(m, struct ether_header *);
853 #ifdef INET
854         ip = (struct ip *)(mtod(m, char *) +
855             sizeof(struct ether_header));
856 #ifdef INET6
857         ip6 = (struct ip6_hdr *)(mtod(m, char *) +
858             sizeof(struct ether_header));
859 #endif
860 #endif
861
862         /*
863          * The fg_fec_output() routine is supposed to leave a
864          * flag for us in the mbuf that tells us what hash to
865          * use, but sometimes a new mbuf is prepended to the
866          * chain, so we have to search every mbuf in the chain
867          * to find the flags.
868          */
869         m0 = m;
870         while (m0) {
871                 if (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6))
872                         break;
873                 m0 = m0->m_next;
874         }
875         if (m0 == NULL)
876                 return(EINVAL);
877
878         switch (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6)) {
879         case M_FEC_MAC:
880                 port = (eh->ether_dhost[5] ^
881                     eh->ether_shost[5]) & mask;
882                 break;
883 #ifdef INET
884         case M_FEC_INET:
885                 port = (ntohl(ip->ip_dst.s_addr) ^
886                     ntohl(ip->ip_src.s_addr)) & mask;
887                 break;
888 #ifdef INET6
889         case M_FEC_INET6:
890                 port = (ip6->ip6_dst.s6_addr[15] ^
891                     ip6->ip6_dst.s6_addr[15]) & mask;
892                 break;
893 #endif
894 #endif
895         default:
896                 return(EINVAL);
897                         break;
898         }
899
900         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
901                 if (port == p->fec_idx)
902                         break;
903         }
904
905         /*
906          * Now that we've chosen a port, make sure it's
907          * alive. If it's not alive, cycle through the bundle
908          * looking for a port that is alive. If we don't find
909          * any, return an error.
910          */
911         if (p->fec_ifstat != 1) {
912                 struct ng_fec_portlist  *n = NULL;
913
914                 n = TAILQ_NEXT(p, fec_list);
915                 if (n == NULL)
916                         n = TAILQ_FIRST(&b->ng_fec_ports);
917                 while (n != p) {
918                         if (n->fec_ifstat == 1)
919                                 break;
920                         n = TAILQ_NEXT(n, fec_list);
921                         if (n == NULL)
922                                 n = TAILQ_FIRST(&b->ng_fec_ports);
923                 }
924                 if (n == p)
925                         return(EAGAIN);
926                 p = n;
927         }
928
929         *ifp = p->fec_if;
930
931         return(0);
932 }
933
934 /*
935  * Now that the packet has been run through ether_output(), yank it
936  * off our own send queue and stick it on the queue for the appropriate
937  * underlying physical interface. Note that if the interface's send
938  * queue is full, we save an error status in our private netgraph
939  * space which will eventually be handed up to ng_fec_output(), which
940  * will return it to the rest of the IP stack. We need to do this
941  * in order to duplicate the effect of ether_output() returning ENOBUFS
942  * when it detects that an interface's send queue is full. There's no
943  * other way to signal the error status from here since the if_start()
944  * routine is spec'ed to return void.
945  *
946  * Once the frame is queued, we call ether_output_frame() to initiate
947  * transmission.
948  */
949 static void
950 ng_fec_start(struct ifnet *ifp)
951 {
952         struct ng_fec_private   *priv;
953         struct ng_fec_bundle    *b;
954         struct ifnet            *oifp = NULL;
955         struct mbuf             *m0;
956         int                     error;
957
958         priv = ifp->if_softc;
959         b = &priv->fec_bundle;
960
961         IF_DEQUEUE(&ifp->if_snd, m0);
962         if (m0 == NULL)
963                 return;
964
965         BPF_MTAP(ifp, m0);
966
967         /* Queue up packet on the proper port. */
968         error = ng_fec_choose_port(b, m0, &oifp);
969         if (error) {
970                 ifp->if_ierrors++;
971                 m_freem(m0);
972                 priv->if_error = ENOBUFS;
973                 return;
974         }
975         ifp->if_opackets++;
976
977         priv->if_error = ether_output_frame(oifp, m0);
978 }
979
980 #ifdef DEBUG
981 /*
982  * Display an ioctl to the virtual interface
983  */
984
985 static void
986 ng_fec_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
987 {
988         char   *str;
989
990         switch (command & IOC_DIRMASK) {
991         case IOC_VOID:
992                 str = "IO";
993                 break;
994         case IOC_OUT:
995                 str = "IOR";
996                 break;
997         case IOC_IN:
998                 str = "IOW";
999                 break;
1000         case IOC_INOUT:
1001                 str = "IORW";
1002                 break;
1003         default:
1004                 str = "IO??";
1005         }
1006         log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
1007                ifp->if_xname,
1008                str,
1009                IOCGROUP(command),
1010                command & 0xff,
1011                IOCPARM_LEN(command));
1012 }
1013 #endif /* DEBUG */
1014
1015 /************************************************************************
1016                         NETGRAPH NODE STUFF
1017  ************************************************************************/
1018
1019 /*
1020  * Constructor for a node
1021  */
1022 static int
1023 ng_fec_constructor(node_p *nodep)
1024 {
1025         char ifname[NG_FEC_FEC_NAME_MAX + 1];
1026         struct ifnet *ifp;
1027         node_p node;
1028         priv_p priv;
1029         struct ng_fec_bundle *b;
1030         int error = 0;
1031
1032         /* Allocate node and interface private structures */
1033         MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT);
1034         if (priv == NULL)
1035                 return (ENOMEM);
1036         bzero(priv, sizeof(*priv));
1037
1038         ifp = &priv->arpcom.ac_if;
1039         b = &priv->fec_bundle;
1040
1041         /* Link them together */
1042         ifp->if_softc = priv;
1043
1044         /* Get an interface unit number */
1045         if ((error = ng_fec_get_unit(&priv->unit)) != 0) {
1046                 FREE(ifp, M_NETGRAPH);
1047                 FREE(priv, M_NETGRAPH);
1048                 return (error);
1049         }
1050
1051         /* Call generic node constructor */
1052         if ((error = ng_make_node_common(&typestruct, nodep)) != 0) {
1053                 ng_fec_free_unit(priv->unit);
1054                 FREE(ifp, M_NETGRAPH);
1055                 FREE(priv, M_NETGRAPH);
1056                 return (error);
1057         }
1058         node = *nodep;
1059
1060         /* Link together node and private info */
1061         node->private = priv;
1062         priv->node = node;
1063         priv->arpcom.ac_netgraph = node;
1064
1065         /* Initialize interface structure */
1066         if_initname(ifp, NG_FEC_FEC_NAME, priv->unit);
1067         ifp->if_start = ng_fec_start;
1068         ifp->if_ioctl = ng_fec_ioctl;
1069         ifp->if_init = ng_fec_init;
1070         ifp->if_watchdog = NULL;
1071         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
1072         ifp->if_mtu = NG_FEC_MTU_DEFAULT;
1073         ifp->if_flags = (IFF_SIMPLEX|IFF_BROADCAST|IFF_MULTICAST);
1074         ifp->if_type = IFT_PROPVIRTUAL;         /* XXX */
1075         ifp->if_addrlen = 0;                    /* XXX */
1076         ifp->if_hdrlen = 0;                     /* XXX */
1077         ifp->if_baudrate = 100000000;           /* XXX */
1078         TAILQ_INIT(&ifp->if_addrhead);
1079
1080         /* Give this node the same name as the interface (if possible) */
1081         bzero(ifname, sizeof(ifname));
1082         strlcpy(ifname, ifp->if_xname, sizeof(ifname));
1083         if (ng_name_node(node, ifname) != 0)
1084                 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
1085
1086         /* Grab hold of the ether_input pipe. */
1087         if (ng_ether_input_p == NULL)
1088                 ng_ether_input_p = ng_fec_input;
1089
1090         /* Attach the interface */
1091         ether_ifattach(ifp, priv->arpcom.ac_enaddr);
1092         priv->real_if_output = ifp->if_output;
1093         ifp->if_output = ng_fec_output;
1094         callout_init(&priv->fec_timeout);
1095
1096         TAILQ_INIT(&b->ng_fec_ports);
1097         b->fec_ifcnt = 0;
1098
1099         ifmedia_init(&priv->ifmedia, 0,
1100             ng_fec_ifmedia_upd, ng_fec_ifmedia_sts);
1101         ifmedia_add(&priv->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
1102         ifmedia_set(&priv->ifmedia, IFM_ETHER|IFM_NONE);
1103
1104         /* Done */
1105         return (0);
1106 }
1107
1108 /*
1109  * Receive a control message
1110  */
1111 static int
1112 ng_fec_rcvmsg(node_p node, struct ng_mesg *msg,
1113                 const char *retaddr, struct ng_mesg **rptr)
1114 {
1115         const priv_p priv = node->private;
1116         struct ng_fec_bundle    *b;
1117         struct ng_mesg *resp = NULL;
1118         char *ifname;
1119         int error = 0;
1120
1121         b = &priv->fec_bundle;
1122
1123         switch (msg->header.typecookie) {
1124         case NGM_FEC_COOKIE:
1125                 switch (msg->header.cmd) {
1126                 case NGM_FEC_ADD_IFACE:
1127                         ifname = msg->data;
1128                         error = ng_fec_addport(priv, ifname);
1129                         break;
1130                 case NGM_FEC_DEL_IFACE:
1131                         ifname = msg->data;
1132                         error = ng_fec_delport(priv, ifname);
1133                         break;
1134                 case NGM_FEC_SET_MODE_MAC:
1135                         b->fec_btype = FEC_BTYPE_MAC;
1136                         break;
1137 #ifdef INET
1138                 case NGM_FEC_SET_MODE_INET:
1139                         b->fec_btype = FEC_BTYPE_INET;
1140                         break;
1141 #ifdef INET6
1142                 case NGM_FEC_SET_MODE_INET6:
1143                         b->fec_btype = FEC_BTYPE_INET6;
1144                         break;
1145 #endif
1146 #endif
1147                 default:
1148                         error = EINVAL;
1149                         break;
1150                 }
1151                 break;
1152         default:
1153                 error = EINVAL;
1154                 break;
1155         }
1156         if (rptr)
1157                 *rptr = resp;
1158         else if (resp)
1159                 FREE(resp, M_NETGRAPH);
1160         FREE(msg, M_NETGRAPH);
1161         return (error);
1162 }
1163
1164 /*
1165  * Shutdown and remove the node and its associated interface.
1166  */
1167 static int
1168 ng_fec_rmnode(node_p node)
1169 {
1170         const priv_p priv = node->private;
1171         struct ng_fec_bundle *b;
1172         struct ng_fec_portlist  *p;
1173         char ifname[IFNAMSIZ];
1174
1175         b = &priv->fec_bundle;
1176         ng_fec_stop(&priv->arpcom.ac_if);
1177
1178         while (!TAILQ_EMPTY(&b->ng_fec_ports)) {
1179                 p = TAILQ_FIRST(&b->ng_fec_ports);
1180                 sprintf(ifname, "%s",
1181                     p->fec_if->if_xname); /* XXX: strings */
1182                 ng_fec_delport(priv, ifname);
1183         }
1184
1185         ng_cutlinks(node);
1186         ng_unname(node);
1187         if (ng_ether_input_p != NULL)
1188                 ng_ether_input_p = NULL;
1189         ether_ifdetach(&priv->arpcom.ac_if);
1190         ifmedia_removeall(&priv->ifmedia);
1191         ng_fec_free_unit(priv->unit);
1192         FREE(priv, M_NETGRAPH);
1193         node->private = NULL;
1194         ng_unref(node);
1195         return (0);
1196 }