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_iface.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  * ng_iface.c
    3  */
    4 
    5 /*-
    6  * Copyright (c) 1996-1999 Whistle Communications, Inc.
    7  * All rights reserved.
    8  * 
    9  * Subject to the following obligations and disclaimer of warranty, use and
   10  * redistribution of this software, in source or object code forms, with or
   11  * without modifications are expressly permitted by Whistle Communications;
   12  * provided, however, that:
   13  * 1. Any and all reproductions of the source or object code must include the
   14  *    copyright notice above and the following disclaimer of warranties; and
   15  * 2. No rights are granted, in any manner or form, to use Whistle
   16  *    Communications, Inc. trademarks, including the mark "WHISTLE
   17  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
   18  *    such appears in the above copyright notice or in the software.
   19  * 
   20  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
   21  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
   22  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
   23  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
   24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
   25  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
   26  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
   27  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
   28  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
   29  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
   30  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
   31  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
   32  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
   33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   35  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
   36  * OF SUCH DAMAGE.
   37  *
   38  * Author: Archie Cobbs <archie@freebsd.org>
   39  *
   40  * $FreeBSD: releng/6.0/sys/netgraph/ng_iface.c 149443 2005-08-25 05:01:24Z rwatson $
   41  * $Whistle: ng_iface.c,v 1.33 1999/11/01 09:24:51 julian Exp $
   42  */
   43 
   44 /*
   45  * This node is also a system networking interface. It has
   46  * a hook for each protocol (IP, AppleTalk, IPX, etc). Packets
   47  * are simply relayed between the interface and the hooks.
   48  *
   49  * Interfaces are named ng0, ng1, etc.  New nodes take the
   50  * first available interface name.
   51  *
   52  * This node also includes Berkeley packet filter support.
   53  */
   54 
   55 #include "opt_atalk.h"
   56 #include "opt_inet.h"
   57 #include "opt_inet6.h"
   58 #include "opt_ipx.h"
   59 
   60 #include <sys/param.h>
   61 #include <sys/systm.h>
   62 #include <sys/errno.h>
   63 #include <sys/kernel.h>
   64 #include <sys/malloc.h>
   65 #include <sys/mbuf.h>
   66 #include <sys/errno.h>
   67 #include <sys/random.h>
   68 #include <sys/sockio.h>
   69 #include <sys/socket.h>
   70 #include <sys/syslog.h>
   71 #include <sys/libkern.h>
   72 
   73 #include <net/if.h>
   74 #include <net/if_types.h>
   75 #include <net/bpf.h>
   76 #include <net/netisr.h>
   77 
   78 #include <netinet/in.h>
   79 
   80 #include <netgraph/ng_message.h>
   81 #include <netgraph/netgraph.h>
   82 #include <netgraph/ng_parse.h>
   83 #include <netgraph/ng_iface.h>
   84 #include <netgraph/ng_cisco.h>
   85 
   86 #ifdef NG_SEPARATE_MALLOC
   87 MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node ");
   88 #else
   89 #define M_NETGRAPH_IFACE M_NETGRAPH
   90 #endif
   91 
   92 /* This struct describes one address family */
   93 struct iffam {
   94         sa_family_t     family;         /* Address family */
   95         const char      *hookname;      /* Name for hook */
   96 };
   97 typedef const struct iffam *iffam_p;
   98 
   99 /* List of address families supported by our interface */
  100 const static struct iffam gFamilies[] = {
  101         { AF_INET,      NG_IFACE_HOOK_INET      },
  102         { AF_INET6,     NG_IFACE_HOOK_INET6     },
  103         { AF_APPLETALK, NG_IFACE_HOOK_ATALK     },
  104         { AF_IPX,       NG_IFACE_HOOK_IPX       },
  105         { AF_ATM,       NG_IFACE_HOOK_ATM       },
  106         { AF_NATM,      NG_IFACE_HOOK_NATM      },
  107 };
  108 #define NUM_FAMILIES            (sizeof(gFamilies) / sizeof(*gFamilies))
  109 
  110 /* Node private data */
  111 struct ng_iface_private {
  112         struct  ifnet *ifp;             /* Our interface */
  113         int     unit;                   /* Interface unit number */
  114         node_p  node;                   /* Our netgraph node */
  115         hook_p  hooks[NUM_FAMILIES];    /* Hook for each address family */
  116 };
  117 typedef struct ng_iface_private *priv_p;
  118 
  119 /* Interface methods */
  120 static void     ng_iface_start(struct ifnet *ifp);
  121 static int      ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
  122 static int      ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
  123                         struct sockaddr *dst, struct rtentry *rt0);
  124 static void     ng_iface_bpftap(struct ifnet *ifp,
  125                         struct mbuf *m, sa_family_t family);
  126 #ifdef DEBUG
  127 static void     ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
  128 #endif
  129 
  130 /* Netgraph methods */
  131 static int              ng_iface_mod_event(module_t, int, void *);
  132 static ng_constructor_t ng_iface_constructor;
  133 static ng_rcvmsg_t      ng_iface_rcvmsg;
  134 static ng_shutdown_t    ng_iface_shutdown;
  135 static ng_newhook_t     ng_iface_newhook;
  136 static ng_rcvdata_t     ng_iface_rcvdata;
  137 static ng_disconnect_t  ng_iface_disconnect;
  138 
  139 /* Helper stuff */
  140 static iffam_p  get_iffam_from_af(sa_family_t family);
  141 static iffam_p  get_iffam_from_hook(priv_p priv, hook_p hook);
  142 static iffam_p  get_iffam_from_name(const char *name);
  143 static hook_p  *get_hook_from_iffam(priv_p priv, iffam_p iffam);
  144 
  145 /* Parse type for struct ng_cisco_ipaddr */
  146 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
  147         = NG_CISCO_IPADDR_TYPE_INFO;
  148 static const struct ng_parse_type ng_cisco_ipaddr_type = {
  149         &ng_parse_struct_type,
  150         &ng_cisco_ipaddr_type_fields
  151 };
  152 
  153 /* List of commands and how to convert arguments to/from ASCII */
  154 static const struct ng_cmdlist ng_iface_cmds[] = {
  155         {
  156           NGM_IFACE_COOKIE,
  157           NGM_IFACE_GET_IFNAME,
  158           "getifname",
  159           NULL,
  160           &ng_parse_string_type
  161         },
  162         {
  163           NGM_IFACE_COOKIE,
  164           NGM_IFACE_POINT2POINT,
  165           "point2point",
  166           NULL,
  167           NULL
  168         },
  169         {
  170           NGM_IFACE_COOKIE,
  171           NGM_IFACE_BROADCAST,
  172           "broadcast",
  173           NULL,
  174           NULL
  175         },
  176         {
  177           NGM_CISCO_COOKIE,
  178           NGM_CISCO_GET_IPADDR,
  179           "getipaddr",
  180           NULL,
  181           &ng_cisco_ipaddr_type
  182         },
  183         {
  184           NGM_IFACE_COOKIE,
  185           NGM_IFACE_GET_IFINDEX,
  186           "getifindex",
  187           NULL,
  188           &ng_parse_uint32_type
  189         },
  190         { 0 }
  191 };
  192 
  193 /* Node type descriptor */
  194 static struct ng_type typestruct = {
  195         .version =      NG_ABI_VERSION,
  196         .name =         NG_IFACE_NODE_TYPE,
  197         .mod_event =    ng_iface_mod_event,
  198         .constructor =  ng_iface_constructor,
  199         .rcvmsg =       ng_iface_rcvmsg,
  200         .shutdown =     ng_iface_shutdown,
  201         .newhook =      ng_iface_newhook,
  202         .rcvdata =      ng_iface_rcvdata,
  203         .disconnect =   ng_iface_disconnect,
  204         .cmdlist =      ng_iface_cmds,
  205 };
  206 NETGRAPH_INIT(iface, &typestruct);
  207 
  208 static struct unrhdr    *ng_iface_unit;
  209 
  210 /************************************************************************
  211                         HELPER STUFF
  212  ************************************************************************/
  213 
  214 /*
  215  * Get the family descriptor from the family ID
  216  */
  217 static __inline iffam_p
  218 get_iffam_from_af(sa_family_t family)
  219 {
  220         iffam_p iffam;
  221         int k;
  222 
  223         for (k = 0; k < NUM_FAMILIES; k++) {
  224                 iffam = &gFamilies[k];
  225                 if (iffam->family == family)
  226                         return (iffam);
  227         }
  228         return (NULL);
  229 }
  230 
  231 /*
  232  * Get the family descriptor from the hook
  233  */
  234 static __inline iffam_p
  235 get_iffam_from_hook(priv_p priv, hook_p hook)
  236 {
  237         int k;
  238 
  239         for (k = 0; k < NUM_FAMILIES; k++)
  240                 if (priv->hooks[k] == hook)
  241                         return (&gFamilies[k]);
  242         return (NULL);
  243 }
  244 
  245 /*
  246  * Get the hook from the iffam descriptor
  247  */
  248 
  249 static __inline hook_p *
  250 get_hook_from_iffam(priv_p priv, iffam_p iffam)
  251 {
  252         return (&priv->hooks[iffam - gFamilies]);
  253 }
  254 
  255 /*
  256  * Get the iffam descriptor from the name
  257  */
  258 static __inline iffam_p
  259 get_iffam_from_name(const char *name)
  260 {
  261         iffam_p iffam;
  262         int k;
  263 
  264         for (k = 0; k < NUM_FAMILIES; k++) {
  265                 iffam = &gFamilies[k];
  266                 if (!strcmp(iffam->hookname, name))
  267                         return (iffam);
  268         }
  269         return (NULL);
  270 }
  271 
  272 /************************************************************************
  273                         INTERFACE STUFF
  274  ************************************************************************/
  275 
  276 /*
  277  * Process an ioctl for the virtual interface
  278  */
  279 static int
  280 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
  281 {
  282         struct ifreq *const ifr = (struct ifreq *) data;
  283         int s, error = 0;
  284 
  285 #ifdef DEBUG
  286         ng_iface_print_ioctl(ifp, command, data);
  287 #endif
  288         s = splimp();
  289         switch (command) {
  290 
  291         /* These two are mostly handled at a higher layer */
  292         case SIOCSIFADDR:
  293                 ifp->if_flags |= IFF_UP;
  294                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
  295                 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
  296                 break;
  297         case SIOCGIFADDR:
  298                 break;
  299 
  300         /* Set flags */
  301         case SIOCSIFFLAGS:
  302                 /*
  303                  * If the interface is marked up and stopped, then start it.
  304                  * If it is marked down and running, then stop it.
  305                  */
  306                 if (ifr->ifr_flags & IFF_UP) {
  307                         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
  308                                 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
  309                                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
  310                         }
  311                 } else {
  312                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
  313                                 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
  314                                     IFF_DRV_OACTIVE);
  315                 }
  316                 break;
  317 
  318         /* Set the interface MTU */
  319         case SIOCSIFMTU:
  320                 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
  321                     || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
  322                         error = EINVAL;
  323                 else
  324                         ifp->if_mtu = ifr->ifr_mtu;
  325                 break;
  326 
  327         /* Stuff that's not supported */
  328         case SIOCADDMULTI:
  329         case SIOCDELMULTI:
  330                 error = 0;
  331                 break;
  332         case SIOCSIFPHYS:
  333                 error = EOPNOTSUPP;
  334                 break;
  335 
  336         default:
  337                 error = EINVAL;
  338                 break;
  339         }
  340         (void) splx(s);
  341         return (error);
  342 }
  343 
  344 /*
  345  * This routine is called to deliver a packet out the interface.
  346  * We simply look at the address family and relay the packet to
  347  * the corresponding hook, if it exists and is connected.
  348  */
  349 
  350 static int
  351 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
  352                 struct sockaddr *dst, struct rtentry *rt0)
  353 {
  354         const priv_p priv = (priv_p) ifp->if_softc;
  355         const iffam_p iffam = get_iffam_from_af(dst->sa_family);
  356         int len, error = 0;
  357         u_int32_t af;
  358 
  359         /* Check interface flags */
  360         if (!((ifp->if_flags & IFF_UP) &&
  361             (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
  362                 m_freem(m);
  363                 return (ENETDOWN);
  364         }
  365 
  366         /* BPF writes need to be handled specially. */
  367         if (dst->sa_family == AF_UNSPEC) {
  368                 bcopy(dst->sa_data, &af, sizeof(af));
  369                 dst->sa_family = af;
  370         }
  371 
  372         /* Berkeley packet filter */
  373         ng_iface_bpftap(ifp, m, dst->sa_family);
  374 
  375         /* Check address family to determine hook (if known) */
  376         if (iffam == NULL) {
  377                 m_freem(m);
  378                 log(LOG_WARNING, "%s: can't handle af%d\n",
  379                        ifp->if_xname, (int)dst->sa_family);
  380                 return (EAFNOSUPPORT);
  381         }
  382 
  383         /* Copy length before the mbuf gets invalidated */
  384         len = m->m_pkthdr.len;
  385 
  386         /* Send packet; if hook is not connected, mbuf will get freed. */
  387         NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m);
  388 
  389         /* Update stats */
  390         if (error == 0) {
  391                 ifp->if_obytes += len;
  392                 ifp->if_opackets++;
  393         }
  394         return (error);
  395 }
  396 
  397 /*
  398  * This routine should never be called
  399  */
  400 
  401 static void
  402 ng_iface_start(struct ifnet *ifp)
  403 {
  404         if_printf(ifp, "%s called?", __func__);
  405 }
  406 
  407 /*
  408  * Flash a packet by the BPF (requires prepending 4 byte AF header)
  409  * Note the phoney mbuf; this is OK because BPF treats it read-only.
  410  */
  411 static void
  412 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
  413 {
  414         KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
  415         if (ifp->if_bpf != NULL) {
  416                 int32_t family4 = (int32_t)family;
  417                 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
  418         }
  419 }
  420 
  421 #ifdef DEBUG
  422 /*
  423  * Display an ioctl to the virtual interface
  424  */
  425 
  426 static void
  427 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
  428 {
  429         char   *str;
  430 
  431         switch (command & IOC_DIRMASK) {
  432         case IOC_VOID:
  433                 str = "IO";
  434                 break;
  435         case IOC_OUT:
  436                 str = "IOR";
  437                 break;
  438         case IOC_IN:
  439                 str = "IOW";
  440                 break;
  441         case IOC_INOUT:
  442                 str = "IORW";
  443                 break;
  444         default:
  445                 str = "IO??";
  446         }
  447         log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
  448                ifp->if_xname,
  449                str,
  450                IOCGROUP(command),
  451                command & 0xff,
  452                IOCPARM_LEN(command));
  453 }
  454 #endif /* DEBUG */
  455 
  456 /************************************************************************
  457                         NETGRAPH NODE STUFF
  458  ************************************************************************/
  459 
  460 /*
  461  * Constructor for a node
  462  */
  463 static int
  464 ng_iface_constructor(node_p node)
  465 {
  466         struct ifnet *ifp;
  467         priv_p priv;
  468 
  469         /* Allocate node and interface private structures */
  470         MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_IFACE, M_NOWAIT|M_ZERO);
  471         if (priv == NULL)
  472                 return (ENOMEM);
  473         ifp = if_alloc(IFT_PROPVIRTUAL);
  474         if (ifp == NULL) {
  475                 FREE(priv, M_NETGRAPH_IFACE);
  476                 return (ENOMEM);
  477         }
  478 
  479         /* Link them together */
  480         ifp->if_softc = priv;
  481         priv->ifp = ifp;
  482 
  483         /* Get an interface unit number */
  484         priv->unit = alloc_unr(ng_iface_unit);
  485 
  486         /* Link together node and private info */
  487         NG_NODE_SET_PRIVATE(node, priv);
  488         priv->node = node;
  489 
  490         /* Initialize interface structure */
  491         if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
  492         ifp->if_output = ng_iface_output;
  493         ifp->if_start = ng_iface_start;
  494         ifp->if_ioctl = ng_iface_ioctl;
  495         ifp->if_watchdog = NULL;
  496         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
  497         ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
  498         ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
  499         ifp->if_type = IFT_PROPVIRTUAL;         /* XXX */
  500         ifp->if_addrlen = 0;                    /* XXX */
  501         ifp->if_hdrlen = 0;                     /* XXX */
  502         ifp->if_baudrate = 64000;               /* XXX */
  503 
  504         /* Give this node the same name as the interface (if possible) */
  505         if (ng_name_node(node, ifp->if_xname) != 0)
  506                 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
  507                     ifp->if_xname);
  508 
  509         /* Attach the interface */
  510         if_attach(ifp);
  511         bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
  512 
  513         /* Done */
  514         return (0);
  515 }
  516 
  517 /*
  518  * Give our ok for a hook to be added
  519  */
  520 static int
  521 ng_iface_newhook(node_p node, hook_p hook, const char *name)
  522 {
  523         const iffam_p iffam = get_iffam_from_name(name);
  524         hook_p *hookptr;
  525 
  526         if (iffam == NULL)
  527                 return (EPFNOSUPPORT);
  528         hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
  529         if (*hookptr != NULL)
  530                 return (EISCONN);
  531         *hookptr = hook;
  532         return (0);
  533 }
  534 
  535 /*
  536  * Receive a control message
  537  */
  538 static int
  539 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
  540 {
  541         const priv_p priv = NG_NODE_PRIVATE(node);
  542         struct ifnet *const ifp = priv->ifp;
  543         struct ng_mesg *resp = NULL;
  544         int error = 0;
  545         struct ng_mesg *msg;
  546 
  547         NGI_GET_MSG(item, msg);
  548         switch (msg->header.typecookie) {
  549         case NGM_IFACE_COOKIE:
  550                 switch (msg->header.cmd) {
  551                 case NGM_IFACE_GET_IFNAME:
  552                         NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
  553                         if (resp == NULL) {
  554                                 error = ENOMEM;
  555                                 break;
  556                         }
  557                         strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
  558                         break;
  559 
  560                 case NGM_IFACE_POINT2POINT:
  561                 case NGM_IFACE_BROADCAST:
  562                     {
  563 
  564                         /* Deny request if interface is UP */
  565                         if ((ifp->if_flags & IFF_UP) != 0)
  566                                 return (EBUSY);
  567 
  568                         /* Change flags */
  569                         switch (msg->header.cmd) {
  570                         case NGM_IFACE_POINT2POINT:
  571                                 ifp->if_flags |= IFF_POINTOPOINT;
  572                                 ifp->if_flags &= ~IFF_BROADCAST;
  573                                 break;
  574                         case NGM_IFACE_BROADCAST:
  575                                 ifp->if_flags &= ~IFF_POINTOPOINT;
  576                                 ifp->if_flags |= IFF_BROADCAST;
  577                                 break;
  578                         }
  579                         break;
  580                     }
  581 
  582                 case NGM_IFACE_GET_IFINDEX:
  583                         NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
  584                         if (resp == NULL) {
  585                                 error = ENOMEM;
  586                                 break;
  587                         }
  588                         *((uint32_t *)resp->data) = priv->ifp->if_index;
  589                         break;
  590 
  591                 default:
  592                         error = EINVAL;
  593                         break;
  594                 }
  595                 break;
  596         case NGM_CISCO_COOKIE:
  597                 switch (msg->header.cmd) {
  598                 case NGM_CISCO_GET_IPADDR:      /* we understand this too */
  599                     {
  600                         struct ifaddr *ifa;
  601 
  602                         /* Return the first configured IP address */
  603                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  604                                 struct ng_cisco_ipaddr *ips;
  605 
  606                                 if (ifa->ifa_addr->sa_family != AF_INET)
  607                                         continue;
  608                                 NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
  609                                 if (resp == NULL) {
  610                                         error = ENOMEM;
  611                                         break;
  612                                 }
  613                                 ips = (struct ng_cisco_ipaddr *)resp->data;
  614                                 ips->ipaddr = ((struct sockaddr_in *)
  615                                                 ifa->ifa_addr)->sin_addr;
  616                                 ips->netmask = ((struct sockaddr_in *)
  617                                                 ifa->ifa_netmask)->sin_addr;
  618                                 break;
  619                         }
  620 
  621                         /* No IP addresses on this interface? */
  622                         if (ifa == NULL)
  623                                 error = EADDRNOTAVAIL;
  624                         break;
  625                     }
  626                 default:
  627                         error = EINVAL;
  628                         break;
  629                 }
  630                 break;
  631         case NGM_FLOW_COOKIE:
  632                 switch (msg->header.cmd) {
  633                 case NGM_LINK_IS_UP:
  634                         ifp->if_drv_flags |= IFF_DRV_RUNNING;
  635                         break;
  636                 case NGM_LINK_IS_DOWN:
  637                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
  638                         break;
  639                 default:
  640                         break;
  641                 }
  642                 break;
  643         default:
  644                 error = EINVAL;
  645                 break;
  646         }
  647         NG_RESPOND_MSG(error, node, item, resp);
  648         NG_FREE_MSG(msg);
  649         return (error);
  650 }
  651 
  652 /*
  653  * Recive data from a hook. Pass the packet to the correct input routine.
  654  */
  655 static int
  656 ng_iface_rcvdata(hook_p hook, item_p item)
  657 {
  658         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
  659         const iffam_p iffam = get_iffam_from_hook(priv, hook);
  660         struct ifnet *const ifp = priv->ifp;
  661         struct mbuf *m;
  662         int isr;
  663 
  664         NGI_GET_M(item, m);
  665         NG_FREE_ITEM(item);
  666         /* Sanity checks */
  667         KASSERT(iffam != NULL, ("%s: iffam", __func__));
  668         M_ASSERTPKTHDR(m);
  669         if ((ifp->if_flags & IFF_UP) == 0) {
  670                 NG_FREE_M(m);
  671                 return (ENETDOWN);
  672         }
  673 
  674         /* Update interface stats */
  675         ifp->if_ipackets++;
  676         ifp->if_ibytes += m->m_pkthdr.len;
  677 
  678         /* Note receiving interface */
  679         m->m_pkthdr.rcvif = ifp;
  680 
  681         /* Berkeley packet filter */
  682         ng_iface_bpftap(ifp, m, iffam->family);
  683 
  684         /* Send packet */
  685         switch (iffam->family) {
  686 #ifdef INET
  687         case AF_INET:
  688                 isr = NETISR_IP;
  689                 break;
  690 #endif
  691 #ifdef INET6
  692         case AF_INET6:
  693                 isr = NETISR_IPV6;
  694                 break;
  695 #endif
  696 #ifdef IPX
  697         case AF_IPX:
  698                 isr = NETISR_IPX;
  699                 break;
  700 #endif
  701 #ifdef NETATALK
  702         case AF_APPLETALK:
  703                 isr = NETISR_ATALK2;
  704                 break;
  705 #endif
  706         default:
  707                 m_freem(m);
  708                 return (EAFNOSUPPORT);
  709         }
  710         /* First chunk of an mbuf contains good junk */
  711         if (harvest.point_to_point)
  712                 random_harvest(m, 16, 3, 0, RANDOM_NET);
  713         netisr_dispatch(isr, m);
  714         return (0);
  715 }
  716 
  717 /*
  718  * Shutdown and remove the node and its associated interface.
  719  */
  720 static int
  721 ng_iface_shutdown(node_p node)
  722 {
  723         const priv_p priv = NG_NODE_PRIVATE(node);
  724 
  725         bpfdetach(priv->ifp);
  726         if_detach(priv->ifp);
  727         if_free(priv->ifp);
  728         priv->ifp = NULL;
  729         free_unr(ng_iface_unit, priv->unit);
  730         FREE(priv, M_NETGRAPH_IFACE);
  731         NG_NODE_SET_PRIVATE(node, NULL);
  732         NG_NODE_UNREF(node);
  733         return (0);
  734 }
  735 
  736 /*
  737  * Hook disconnection. Note that we do *not* shutdown when all
  738  * hooks have been disconnected.
  739  */
  740 static int
  741 ng_iface_disconnect(hook_p hook)
  742 {
  743         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
  744         const iffam_p iffam = get_iffam_from_hook(priv, hook);
  745 
  746         if (iffam == NULL)
  747                 panic(__func__);
  748         *get_hook_from_iffam(priv, iffam) = NULL;
  749         return (0);
  750 }
  751 
  752 /*
  753  * Handle loading and unloading for this node type.
  754  */
  755 static int
  756 ng_iface_mod_event(module_t mod, int event, void *data)
  757 {
  758         int error = 0;
  759 
  760         switch (event) {
  761         case MOD_LOAD:
  762                 ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
  763                 break;
  764         case MOD_UNLOAD:
  765                 delete_unrhdr(ng_iface_unit);
  766                 break;
  767         default:
  768                 error = EOPNOTSUPP;
  769                 break;
  770         }
  771         return (error);
  772 }

Cache object: 8800e79a75fda2b87b17653ab00a87a0


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