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_ether.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 
    2 /*
    3  * ng_ether.c
    4  */
    5 
    6 /*-
    7  * Copyright (c) 1996-2000 Whistle Communications, Inc.
    8  * All rights reserved.
    9  * 
   10  * Subject to the following obligations and disclaimer of warranty, use and
   11  * redistribution of this software, in source or object code forms, with or
   12  * without modifications are expressly permitted by Whistle Communications;
   13  * provided, however, that:
   14  * 1. Any and all reproductions of the source or object code must include the
   15  *    copyright notice above and the following disclaimer of warranties; and
   16  * 2. No rights are granted, in any manner or form, to use Whistle
   17  *    Communications, Inc. trademarks, including the mark "WHISTLE
   18  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
   19  *    such appears in the above copyright notice or in the software.
   20  * 
   21  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
   22  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
   23  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
   24  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
   25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
   26  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
   27  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
   28  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
   29  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
   30  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
   31  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
   32  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
   33  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
   34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   36  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
   37  * OF SUCH DAMAGE.
   38  *
   39  * Authors: Archie Cobbs <archie@freebsd.org>
   40  *          Julian Elischer <julian@freebsd.org>
   41  *
   42  * $FreeBSD: src/sys/netgraph/ng_ether.c,v 1.38.2.2 2005/01/31 23:26:28 imp Exp $
   43  */
   44 
   45 /*
   46  * ng_ether(4) netgraph node type
   47  */
   48 
   49 #include <sys/param.h>
   50 #include <sys/systm.h>
   51 #include <sys/kernel.h>
   52 #include <sys/malloc.h>
   53 #include <sys/mbuf.h>
   54 #include <sys/errno.h>
   55 #include <sys/syslog.h>
   56 #include <sys/socket.h>
   57 
   58 #include <net/if.h>
   59 #include <net/if_types.h>
   60 #include <net/if_arp.h>
   61 #include <net/if_var.h>
   62 #include <net/ethernet.h>
   63 
   64 #include <netgraph/ng_message.h>
   65 #include <netgraph/netgraph.h>
   66 #include <netgraph/ng_parse.h>
   67 #include <netgraph/ng_ether.h>
   68 
   69 #define IFP2NG(ifp)  ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph)
   70 #define IFP2NG_SET(ifp, val)    (((struct arpcom *)(ifp))->ac_netgraph = (val))
   71 
   72 /* Per-node private data */
   73 struct private {
   74         struct ifnet    *ifp;           /* associated interface */
   75         hook_p          upper;          /* upper hook connection */
   76         hook_p          lower;          /* lower hook connection */
   77         hook_p          orphan;         /* orphan hook connection */
   78         u_char          autoSrcAddr;    /* always overwrite source address */
   79         u_char          promisc;        /* promiscuous mode enabled */
   80         u_long          hwassist;       /* hardware checksum capabilities */
   81         u_int           flags;          /* flags e.g. really die */
   82 };
   83 typedef struct private *priv_p;
   84 
   85 /* Hook pointers used by if_ethersubr.c to callback to netgraph */
   86 extern  void    (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
   87 extern  void    (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
   88 extern  int     (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
   89 extern  void    (*ng_ether_attach_p)(struct ifnet *ifp);
   90 extern  void    (*ng_ether_detach_p)(struct ifnet *ifp);
   91 
   92 /* Functional hooks called from if_ethersubr.c */
   93 static void     ng_ether_input(struct ifnet *ifp, struct mbuf **mp);
   94 static void     ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m);
   95 static int      ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
   96 static void     ng_ether_attach(struct ifnet *ifp);
   97 static void     ng_ether_detach(struct ifnet *ifp); 
   98 
   99 /* Other functions */
  100 static int      ng_ether_rcv_lower(node_p node, struct mbuf *m);
  101 static int      ng_ether_rcv_upper(node_p node, struct mbuf *m);
  102 
  103 /* Netgraph node methods */
  104 static ng_constructor_t ng_ether_constructor;
  105 static ng_rcvmsg_t      ng_ether_rcvmsg;
  106 static ng_shutdown_t    ng_ether_shutdown;
  107 static ng_newhook_t     ng_ether_newhook;
  108 static ng_connect_t     ng_ether_connect;
  109 static ng_rcvdata_t     ng_ether_rcvdata;
  110 static ng_disconnect_t  ng_ether_disconnect;
  111 static int              ng_ether_mod_event(module_t mod, int event, void *data);
  112 
  113 /* List of commands and how to convert arguments to/from ASCII */
  114 static const struct ng_cmdlist ng_ether_cmdlist[] = {
  115         {
  116           NGM_ETHER_COOKIE,
  117           NGM_ETHER_GET_IFNAME,
  118           "getifname",
  119           NULL,
  120           &ng_parse_string_type
  121         },
  122         {
  123           NGM_ETHER_COOKIE,
  124           NGM_ETHER_GET_IFINDEX,
  125           "getifindex",
  126           NULL,
  127           &ng_parse_int32_type
  128         },
  129         {
  130           NGM_ETHER_COOKIE,
  131           NGM_ETHER_GET_ENADDR,
  132           "getenaddr",
  133           NULL,
  134           &ng_parse_enaddr_type
  135         },
  136         {
  137           NGM_ETHER_COOKIE,
  138           NGM_ETHER_SET_ENADDR,
  139           "setenaddr",
  140           &ng_parse_enaddr_type,
  141           NULL
  142         },
  143         {
  144           NGM_ETHER_COOKIE,
  145           NGM_ETHER_GET_PROMISC,
  146           "getpromisc",
  147           NULL,
  148           &ng_parse_int32_type
  149         },
  150         {
  151           NGM_ETHER_COOKIE,
  152           NGM_ETHER_SET_PROMISC,
  153           "setpromisc",
  154           &ng_parse_int32_type,
  155           NULL
  156         },
  157         {
  158           NGM_ETHER_COOKIE,
  159           NGM_ETHER_GET_AUTOSRC,
  160           "getautosrc",
  161           NULL,
  162           &ng_parse_int32_type
  163         },
  164         {
  165           NGM_ETHER_COOKIE,
  166           NGM_ETHER_SET_AUTOSRC,
  167           "setautosrc",
  168           &ng_parse_int32_type,
  169           NULL
  170         },
  171         { 0 }
  172 };
  173 
  174 static struct ng_type ng_ether_typestruct = {
  175         .version =      NG_ABI_VERSION,
  176         .name =         NG_ETHER_NODE_TYPE,
  177         .mod_event =    ng_ether_mod_event,
  178         .constructor =  ng_ether_constructor,
  179         .rcvmsg =       ng_ether_rcvmsg,
  180         .shutdown =     ng_ether_shutdown,
  181         .newhook =      ng_ether_newhook,
  182         .connect =      ng_ether_connect,
  183         .rcvdata =      ng_ether_rcvdata,
  184         .disconnect =   ng_ether_disconnect,
  185         .cmdlist =      ng_ether_cmdlist,
  186 };
  187 MODULE_VERSION(ng_ether, 1);
  188 NETGRAPH_INIT(ether, &ng_ether_typestruct);
  189 
  190 /******************************************************************
  191                     ETHERNET FUNCTION HOOKS
  192 ******************************************************************/
  193 
  194 /*
  195  * Handle a packet that has come in on an interface. We get to
  196  * look at it here before any upper layer protocols do.
  197  *
  198  * NOTE: this function will get called at splimp()
  199  */
  200 static void
  201 ng_ether_input(struct ifnet *ifp, struct mbuf **mp)
  202 {
  203         const node_p node = IFP2NG(ifp);
  204         const priv_p priv = NG_NODE_PRIVATE(node);
  205         int error;
  206 
  207         /* If "lower" hook not connected, let packet continue */
  208         if (priv->lower == NULL)
  209                 return;
  210         NG_SEND_DATA_ONLY(error, priv->lower, *mp);     /* sets *mp = NULL */
  211 }
  212 
  213 /*
  214  * Handle a packet that has come in on an interface, and which
  215  * does not match any of our known protocols (an ``orphan'').
  216  *
  217  * NOTE: this function will get called at splimp()
  218  */
  219 static void
  220 ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m)
  221 {
  222         const node_p node = IFP2NG(ifp);
  223         const priv_p priv = NG_NODE_PRIVATE(node);
  224         int error;
  225 
  226         /* If "orphan" hook not connected, discard packet */
  227         if (priv->orphan == NULL) {
  228                 m_freem(m);
  229                 return;
  230         }
  231         NG_SEND_DATA_ONLY(error, priv->orphan, m);
  232 }
  233 
  234 /*
  235  * Handle a packet that is going out on an interface.
  236  * The Ethernet header is already attached to the mbuf.
  237  */
  238 static int
  239 ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
  240 {
  241         const node_p node = IFP2NG(ifp);
  242         const priv_p priv = NG_NODE_PRIVATE(node);
  243         int error = 0;
  244 
  245         /* If "upper" hook not connected, let packet continue */
  246         if (priv->upper == NULL)
  247                 return (0);
  248 
  249         /* Send it out "upper" hook */
  250         NG_SEND_DATA_ONLY(error, priv->upper, *mp);
  251         return (error);
  252 }
  253 
  254 /*
  255  * A new Ethernet interface has been attached.
  256  * Create a new node for it, etc.
  257  */
  258 static void
  259 ng_ether_attach(struct ifnet *ifp)
  260 {
  261         priv_p priv;
  262         node_p node;
  263 
  264         /* Create node */
  265         KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
  266         if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
  267                 log(LOG_ERR, "%s: can't %s for %s\n",
  268                     __func__, "create node", ifp->if_xname);
  269                 return;
  270         }
  271 
  272         /* Allocate private data */
  273         MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
  274         if (priv == NULL) {
  275                 log(LOG_ERR, "%s: can't %s for %s\n",
  276                     __func__, "allocate memory", ifp->if_xname);
  277                 NG_NODE_UNREF(node);
  278                 return;
  279         }
  280         NG_NODE_SET_PRIVATE(node, priv);
  281         priv->ifp = ifp;
  282         IFP2NG_SET(ifp, node);
  283         priv->autoSrcAddr = 1;
  284         priv->hwassist = ifp->if_hwassist;
  285 
  286         /* Try to give the node the same name as the interface */
  287         if (ng_name_node(node, ifp->if_xname) != 0) {
  288                 log(LOG_WARNING, "%s: can't name node %s\n",
  289                     __func__, ifp->if_xname);
  290         }
  291 }
  292 
  293 /*
  294  * An Ethernet interface is being detached.
  295  * REALLY Destroy its node.
  296  */
  297 static void
  298 ng_ether_detach(struct ifnet *ifp)
  299 {
  300         const node_p node = IFP2NG(ifp);
  301         const priv_p priv = NG_NODE_PRIVATE(node);
  302 
  303         NG_NODE_REALLY_DIE(node);       /* Force real removal of node */
  304         /*
  305          * We can't assume the ifnet is still around when we run shutdown
  306          * So zap it now. XXX We HOPE that anything running at this time
  307          * handles it (as it should in the non netgraph case).
  308          */
  309         IFP2NG_SET(ifp, NULL);
  310         priv->ifp = NULL;       /* XXX race if interrupted an output packet */
  311         ng_rmnode_self(node);           /* remove all netgraph parts */
  312 }
  313 
  314 /******************************************************************
  315                     NETGRAPH NODE METHODS
  316 ******************************************************************/
  317 
  318 /*
  319  * It is not possible or allowable to create a node of this type.
  320  * Nodes get created when the interface is attached (or, when
  321  * this node type's KLD is loaded).
  322  */
  323 static int
  324 ng_ether_constructor(node_p node)
  325 {
  326         return (EINVAL);
  327 }
  328 
  329 /*
  330  * Check for attaching a new hook.
  331  */
  332 static  int
  333 ng_ether_newhook(node_p node, hook_p hook, const char *name)
  334 {
  335         const priv_p priv = NG_NODE_PRIVATE(node);
  336         hook_p *hookptr;
  337 
  338         /* Divert hook is an alias for lower */
  339         if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
  340                 name = NG_ETHER_HOOK_LOWER;
  341 
  342         /* Which hook? */
  343         if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
  344                 hookptr = &priv->upper;
  345         else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0)
  346                 hookptr = &priv->lower;
  347         else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0)
  348                 hookptr = &priv->orphan;
  349         else
  350                 return (EINVAL);
  351 
  352         /* Check if already connected (shouldn't be, but doesn't hurt) */
  353         if (*hookptr != NULL)
  354                 return (EISCONN);
  355 
  356         /* Disable hardware checksums while 'upper' hook is connected */
  357         if (hookptr == &priv->upper)
  358                 priv->ifp->if_hwassist = 0;
  359 
  360         /* OK */
  361         *hookptr = hook;
  362         return (0);
  363 }
  364 
  365 /*
  366  * Hooks are attached, adjust to force queueing.
  367  * We don't really care which hook it is.
  368  * they should all be queuing for outgoing data.
  369  */
  370 static  int
  371 ng_ether_connect(hook_p hook)
  372 {
  373         NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
  374         return (0);
  375 }
  376 
  377 /*
  378  * Receive an incoming control message.
  379  */
  380 static int
  381 ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
  382 {
  383         const priv_p priv = NG_NODE_PRIVATE(node);
  384         struct ng_mesg *resp = NULL;
  385         int error = 0;
  386         struct ng_mesg *msg;
  387 
  388         NGI_GET_MSG(item, msg);
  389         switch (msg->header.typecookie) {
  390         case NGM_ETHER_COOKIE:
  391                 switch (msg->header.cmd) {
  392                 case NGM_ETHER_GET_IFNAME:
  393                         NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT);
  394                         if (resp == NULL) {
  395                                 error = ENOMEM;
  396                                 break;
  397                         }
  398                         strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ + 1);
  399                         break;
  400                 case NGM_ETHER_GET_IFINDEX:
  401                         NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
  402                         if (resp == NULL) {
  403                                 error = ENOMEM;
  404                                 break;
  405                         }
  406                         *((u_int32_t *)resp->data) = priv->ifp->if_index;
  407                         break;
  408                 case NGM_ETHER_GET_ENADDR:
  409                         NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
  410                         if (resp == NULL) {
  411                                 error = ENOMEM;
  412                                 break;
  413                         }
  414                         bcopy((IFP2AC(priv->ifp))->ac_enaddr,
  415                             resp->data, ETHER_ADDR_LEN);
  416                         break;
  417                 case NGM_ETHER_SET_ENADDR:
  418                     {
  419                         if (msg->header.arglen != ETHER_ADDR_LEN) {
  420                                 error = EINVAL;
  421                                 break;
  422                         }
  423                         error = if_setlladdr(priv->ifp,
  424                             (u_char *)msg->data, ETHER_ADDR_LEN);
  425                         break;
  426                     }
  427                 case NGM_ETHER_GET_PROMISC:
  428                         NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
  429                         if (resp == NULL) {
  430                                 error = ENOMEM;
  431                                 break;
  432                         }
  433                         *((u_int32_t *)resp->data) = priv->promisc;
  434                         break;
  435                 case NGM_ETHER_SET_PROMISC:
  436                     {
  437                         u_char want;
  438 
  439                         if (msg->header.arglen != sizeof(u_int32_t)) {
  440                                 error = EINVAL;
  441                                 break;
  442                         }
  443                         want = !!*((u_int32_t *)msg->data);
  444                         if (want ^ priv->promisc) {
  445                                 if ((error = ifpromisc(priv->ifp, want)) != 0)
  446                                         break;
  447                                 priv->promisc = want;
  448                         }
  449                         break;
  450                     }
  451                 case NGM_ETHER_GET_AUTOSRC:
  452                         NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
  453                         if (resp == NULL) {
  454                                 error = ENOMEM;
  455                                 break;
  456                         }
  457                         *((u_int32_t *)resp->data) = priv->autoSrcAddr;
  458                         break;
  459                 case NGM_ETHER_SET_AUTOSRC:
  460                         if (msg->header.arglen != sizeof(u_int32_t)) {
  461                                 error = EINVAL;
  462                                 break;
  463                         }
  464                         priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
  465                         break;
  466                 default:
  467                         error = EINVAL;
  468                         break;
  469                 }
  470                 break;
  471         default:
  472                 error = EINVAL;
  473                 break;
  474         }
  475         NG_RESPOND_MSG(error, node, item, resp);
  476         NG_FREE_MSG(msg);
  477         return (error);
  478 }
  479 
  480 /*
  481  * Receive data on a hook.
  482  */
  483 static int
  484 ng_ether_rcvdata(hook_p hook, item_p item)
  485 {
  486         const node_p node = NG_HOOK_NODE(hook);
  487         const priv_p priv = NG_NODE_PRIVATE(node);
  488         struct mbuf *m;
  489 
  490         NGI_GET_M(item, m);
  491         NG_FREE_ITEM(item);
  492 
  493         if (hook == priv->lower || hook == priv->orphan)
  494                 return ng_ether_rcv_lower(node, m);
  495         if (hook == priv->upper)
  496                 return ng_ether_rcv_upper(node, m);
  497         panic("%s: weird hook", __func__);
  498 #ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */
  499         return (0);
  500 #endif
  501 }
  502 
  503 /*
  504  * Handle an mbuf received on the "lower" or "orphan" hook.
  505  */
  506 static int
  507 ng_ether_rcv_lower(node_p node, struct mbuf *m)
  508 {
  509         const priv_p priv = NG_NODE_PRIVATE(node);
  510         struct ifnet *const ifp = priv->ifp;
  511 
  512         /* Check whether interface is ready for packets */
  513         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
  514                 NG_FREE_M(m);
  515                 return (ENETDOWN);
  516         }
  517 
  518         /* Make sure header is fully pulled up */
  519         if (m->m_pkthdr.len < sizeof(struct ether_header)) {
  520                 NG_FREE_M(m);
  521                 return (EINVAL);
  522         }
  523         if (m->m_len < sizeof(struct ether_header)
  524             && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
  525                 return (ENOBUFS);
  526 
  527         /* Drop in the MAC address if desired */
  528         if (priv->autoSrcAddr) {
  529 
  530                 /* Make the mbuf writable if it's not already */
  531                 if (!M_WRITABLE(m)
  532                     && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
  533                         return (ENOBUFS);
  534 
  535                 /* Overwrite source MAC address */
  536                 bcopy((IFP2AC(ifp))->ac_enaddr,
  537                     mtod(m, struct ether_header *)->ether_shost,
  538                     ETHER_ADDR_LEN);
  539         }
  540 
  541         /* Send it on its way */
  542         return ether_output_frame(ifp, m);
  543 }
  544 
  545 /*
  546  * Handle an mbuf received on the "upper" hook.
  547  */
  548 static int
  549 ng_ether_rcv_upper(node_p node, struct mbuf *m)
  550 {
  551         const priv_p priv = NG_NODE_PRIVATE(node);
  552 
  553         m->m_pkthdr.rcvif = priv->ifp;
  554 
  555         /* Route packet back in */
  556         ether_demux(priv->ifp, m);
  557         return (0);
  558 }
  559 
  560 /*
  561  * Shutdown node. This resets the node but does not remove it
  562  * unless the REALLY_DIE flag is set.
  563  */
  564 static int
  565 ng_ether_shutdown(node_p node)
  566 {
  567         const priv_p priv = NG_NODE_PRIVATE(node);
  568 
  569         if (node->nd_flags & NGF_REALLY_DIE) {
  570                 /*
  571                  * WE came here because the ethernet card is being unloaded,
  572                  * so stop being persistant.
  573                  * Actually undo all the things we did on creation.
  574                  * Assume the ifp has already been freed.
  575                  */
  576                 NG_NODE_SET_PRIVATE(node, NULL);
  577                 FREE(priv, M_NETGRAPH);         
  578                 NG_NODE_UNREF(node);    /* free node itself */
  579                 return (0);
  580         }
  581         if (priv->promisc) {            /* disable promiscuous mode */
  582                 (void)ifpromisc(priv->ifp, 0);
  583                 priv->promisc = 0;
  584         }
  585         priv->autoSrcAddr = 1;          /* reset auto-src-addr flag */
  586         NG_NODE_REVIVE(node);           /* Signal ng_rmnode we are persisant */
  587 
  588         return (0);
  589 }
  590 
  591 /*
  592  * Hook disconnection.
  593  */
  594 static int
  595 ng_ether_disconnect(hook_p hook)
  596 {
  597         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
  598 
  599         if (hook == priv->upper) {
  600                 priv->upper = NULL;
  601                 if (priv->ifp != NULL)          /* restore h/w csum */
  602                         priv->ifp->if_hwassist = priv->hwassist;
  603         } else if (hook == priv->lower)
  604                 priv->lower = NULL;
  605         else if (hook == priv->orphan)
  606                 priv->orphan = NULL;
  607         else
  608                 panic("%s: weird hook", __func__);
  609         if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
  610         && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
  611                 ng_rmnode_self(NG_HOOK_NODE(hook));     /* reset node */
  612         return (0);
  613 }
  614 
  615 /******************************************************************
  616                         INITIALIZATION
  617 ******************************************************************/
  618 
  619 /*
  620  * Handle loading and unloading for this node type.
  621  */
  622 static int
  623 ng_ether_mod_event(module_t mod, int event, void *data)
  624 {
  625         struct ifnet *ifp;
  626         int error = 0;
  627         int s;
  628 
  629         s = splnet();
  630         switch (event) {
  631         case MOD_LOAD:
  632 
  633                 /* Register function hooks */
  634                 if (ng_ether_attach_p != NULL) {
  635                         error = EEXIST;
  636                         break;
  637                 }
  638                 ng_ether_attach_p = ng_ether_attach;
  639                 ng_ether_detach_p = ng_ether_detach;
  640                 ng_ether_output_p = ng_ether_output;
  641                 ng_ether_input_p = ng_ether_input;
  642                 ng_ether_input_orphan_p = ng_ether_input_orphan;
  643 
  644                 /* Create nodes for any already-existing Ethernet interfaces */
  645                 IFNET_RLOCK();
  646                 TAILQ_FOREACH(ifp, &ifnet, if_link) {
  647                         if (ifp->if_type == IFT_ETHER
  648                             || ifp->if_type == IFT_L2VLAN)
  649                                 ng_ether_attach(ifp);
  650                 }
  651                 IFNET_RUNLOCK();
  652                 break;
  653 
  654         case MOD_UNLOAD:
  655 
  656                 /*
  657                  * Note that the base code won't try to unload us until
  658                  * all nodes have been removed, and that can't happen
  659                  * until all Ethernet interfaces are removed. In any
  660                  * case, we know there are no nodes left if the action
  661                  * is MOD_UNLOAD, so there's no need to detach any nodes.
  662                  */
  663 
  664                 /* Unregister function hooks */
  665                 ng_ether_attach_p = NULL;
  666                 ng_ether_detach_p = NULL;
  667                 ng_ether_output_p = NULL;
  668                 ng_ether_input_p = NULL;
  669                 ng_ether_input_orphan_p = NULL;
  670                 break;
  671 
  672         default:
  673                 error = EOPNOTSUPP;
  674                 break;
  675         }
  676         splx(s);
  677         return (error);
  678 }
  679 

Cache object: f5811d288c845c7706618d68279a574c


[ 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.