The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netgraph/ng_fec.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 6ced1f82cf07f0d6248de1959f58c8bc


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.