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-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_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$
   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, 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_inet.h"
   56 #include "opt_inet6.h"
   57 
   58 #include <sys/param.h>
   59 #include <sys/systm.h>
   60 #include <sys/errno.h>
   61 #include <sys/kernel.h>
   62 #include <sys/lock.h>
   63 #include <sys/malloc.h>
   64 #include <sys/mbuf.h>
   65 #include <sys/errno.h>
   66 #include <sys/proc.h>
   67 #include <sys/random.h>
   68 #include <sys/rmlock.h>
   69 #include <sys/sockio.h>
   70 #include <sys/socket.h>
   71 #include <sys/sysctl.h>
   72 #include <sys/syslog.h>
   73 #include <sys/libkern.h>
   74 
   75 #include <net/if.h>
   76 #include <net/if_var.h>
   77 #include <net/if_types.h>
   78 #include <net/bpf.h>
   79 #include <net/netisr.h>
   80 #include <net/route.h>
   81 #include <net/vnet.h>
   82 
   83 #include <netinet/in.h>
   84 
   85 #include <netgraph/ng_message.h>
   86 #include <netgraph/netgraph.h>
   87 #include <netgraph/ng_parse.h>
   88 #include <netgraph/ng_iface.h>
   89 
   90 #ifdef NG_SEPARATE_MALLOC
   91 static MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node");
   92 #else
   93 #define M_NETGRAPH_IFACE M_NETGRAPH
   94 #endif
   95 
   96 static SYSCTL_NODE(_net_graph, OID_AUTO, iface, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
   97     "Point to point netgraph interface");
   98 VNET_DEFINE_STATIC(int, ng_iface_max_nest) = 2;
   99 #define V_ng_iface_max_nest     VNET(ng_iface_max_nest)
  100 SYSCTL_INT(_net_graph_iface, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW,
  101     &VNET_NAME(ng_iface_max_nest), 0, "Max nested tunnels");
  102 
  103 /* This struct describes one address family */
  104 struct iffam {
  105         sa_family_t     family;         /* Address family */
  106         const char      *hookname;      /* Name for hook */
  107 };
  108 typedef const struct iffam *iffam_p;
  109 
  110 /* List of address families supported by our interface */
  111 const static struct iffam gFamilies[] = {
  112         { AF_INET,      NG_IFACE_HOOK_INET      },
  113         { AF_INET6,     NG_IFACE_HOOK_INET6     },
  114 };
  115 #define NUM_FAMILIES            nitems(gFamilies)
  116 
  117 /* Node private data */
  118 struct ng_iface_private {
  119         struct  ifnet *ifp;             /* Our interface */
  120         int     unit;                   /* Interface unit number */
  121         node_p  node;                   /* Our netgraph node */
  122         hook_p  hooks[NUM_FAMILIES];    /* Hook for each address family */
  123         struct rmlock   lock;           /* Protect private data changes */
  124 };
  125 typedef struct ng_iface_private *priv_p;
  126 
  127 #define PRIV_RLOCK(priv, t)     rm_rlock(&priv->lock, t)
  128 #define PRIV_RUNLOCK(priv, t)   rm_runlock(&priv->lock, t)
  129 #define PRIV_WLOCK(priv)        rm_wlock(&priv->lock)
  130 #define PRIV_WUNLOCK(priv)      rm_wunlock(&priv->lock)
  131 
  132 /* Interface methods */
  133 static void     ng_iface_start(struct ifnet *ifp);
  134 static int      ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
  135 static int      ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
  136                         const struct sockaddr *dst, struct route *ro);
  137 static void     ng_iface_bpftap(struct ifnet *ifp,
  138                         struct mbuf *m, sa_family_t family);
  139 static int      ng_iface_send(struct ifnet *ifp, struct mbuf *m,
  140                         sa_family_t sa);
  141 #ifdef DEBUG
  142 static void     ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
  143 #endif
  144 
  145 /* Netgraph methods */
  146 static int              ng_iface_mod_event(module_t, int, void *);
  147 static ng_constructor_t ng_iface_constructor;
  148 static ng_rcvmsg_t      ng_iface_rcvmsg;
  149 static ng_shutdown_t    ng_iface_shutdown;
  150 static ng_newhook_t     ng_iface_newhook;
  151 static ng_rcvdata_t     ng_iface_rcvdata;
  152 static ng_disconnect_t  ng_iface_disconnect;
  153 
  154 /* Helper stuff */
  155 static iffam_p  get_iffam_from_af(sa_family_t family);
  156 static iffam_p  get_iffam_from_hook(priv_p priv, hook_p hook);
  157 static iffam_p  get_iffam_from_name(const char *name);
  158 static hook_p  *get_hook_from_iffam(priv_p priv, iffam_p iffam);
  159 
  160 /* List of commands and how to convert arguments to/from ASCII */
  161 static const struct ng_cmdlist ng_iface_cmds[] = {
  162         {
  163           NGM_IFACE_COOKIE,
  164           NGM_IFACE_GET_IFNAME,
  165           "getifname",
  166           NULL,
  167           &ng_parse_string_type
  168         },
  169         {
  170           NGM_IFACE_COOKIE,
  171           NGM_IFACE_POINT2POINT,
  172           "point2point",
  173           NULL,
  174           NULL
  175         },
  176         {
  177           NGM_IFACE_COOKIE,
  178           NGM_IFACE_BROADCAST,
  179           "broadcast",
  180           NULL,
  181           NULL
  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 VNET_DEFINE_STATIC(struct unrhdr *, ng_iface_unit);
  209 #define V_ng_iface_unit                 VNET(ng_iface_unit)
  210 
  211 /************************************************************************
  212                         HELPER STUFF
  213  ************************************************************************/
  214 
  215 /*
  216  * Get the family descriptor from the family ID
  217  */
  218 static __inline iffam_p
  219 get_iffam_from_af(sa_family_t family)
  220 {
  221         iffam_p iffam;
  222         int k;
  223 
  224         for (k = 0; k < NUM_FAMILIES; k++) {
  225                 iffam = &gFamilies[k];
  226                 if (iffam->family == family)
  227                         return (iffam);
  228         }
  229         return (NULL);
  230 }
  231 
  232 /*
  233  * Get the family descriptor from the hook
  234  */
  235 static __inline iffam_p
  236 get_iffam_from_hook(priv_p priv, hook_p hook)
  237 {
  238         int k;
  239 
  240         for (k = 0; k < NUM_FAMILIES; k++)
  241                 if (priv->hooks[k] == hook)
  242                         return (&gFamilies[k]);
  243         return (NULL);
  244 }
  245 
  246 /*
  247  * Get the hook from the iffam descriptor
  248  */
  249 
  250 static __inline hook_p *
  251 get_hook_from_iffam(priv_p priv, iffam_p iffam)
  252 {
  253         return (&priv->hooks[iffam - gFamilies]);
  254 }
  255 
  256 /*
  257  * Get the iffam descriptor from the name
  258  */
  259 static __inline iffam_p
  260 get_iffam_from_name(const char *name)
  261 {
  262         iffam_p iffam;
  263         int k;
  264 
  265         for (k = 0; k < NUM_FAMILIES; k++) {
  266                 iffam = &gFamilies[k];
  267                 if (!strcmp(iffam->hookname, name))
  268                         return (iffam);
  269         }
  270         return (NULL);
  271 }
  272 
  273 /************************************************************************
  274                         INTERFACE STUFF
  275  ************************************************************************/
  276 
  277 /*
  278  * Process an ioctl for the virtual interface
  279  */
  280 static int
  281 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
  282 {
  283         struct ifreq *const ifr = (struct ifreq *) data;
  284         int error = 0;
  285 
  286 #ifdef DEBUG
  287         ng_iface_print_ioctl(ifp, command, data);
  288 #endif
  289         switch (command) {
  290         /* These two are mostly handled at a higher layer */
  291         case SIOCSIFADDR:
  292                 ifp->if_flags |= IFF_UP;
  293                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
  294                 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
  295                 break;
  296         case SIOCGIFADDR:
  297                 break;
  298 
  299         /* Set flags */
  300         case SIOCSIFFLAGS:
  301                 /*
  302                  * If the interface is marked up and stopped, then start it.
  303                  * If it is marked down and running, then stop it.
  304                  */
  305                 if (ifr->ifr_flags & IFF_UP) {
  306                         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
  307                                 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
  308                                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
  309                         }
  310                 } else {
  311                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
  312                                 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
  313                                     IFF_DRV_OACTIVE);
  314                 }
  315                 break;
  316 
  317         /* Set the interface MTU */
  318         case SIOCSIFMTU:
  319                 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
  320                     || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
  321                         error = EINVAL;
  322                 else
  323                         ifp->if_mtu = ifr->ifr_mtu;
  324                 break;
  325 
  326         /* Stuff that's not supported */
  327         case SIOCADDMULTI:
  328         case SIOCDELMULTI:
  329                 error = 0;
  330                 break;
  331         case SIOCSIFPHYS:
  332                 error = EOPNOTSUPP;
  333                 break;
  334 
  335         default:
  336                 error = EINVAL;
  337                 break;
  338         }
  339         return (error);
  340 }
  341 
  342 /*
  343  * This routine is called to deliver a packet out the interface.
  344  * We simply look at the address family and relay the packet to
  345  * the corresponding hook, if it exists and is connected.
  346  */
  347 
  348 static int
  349 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
  350         const struct sockaddr *dst, struct route *ro)
  351 {
  352         uint32_t af;
  353         int error;
  354 
  355         /* Check interface flags */
  356         if (!((ifp->if_flags & IFF_UP) &&
  357             (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
  358                 m_freem(m);
  359                 return (ENETDOWN);
  360         }
  361 
  362         /* Protect from deadly infinite recursion. */
  363         error = if_tunnel_check_nesting(ifp, m, NGM_IFACE_COOKIE,
  364             V_ng_iface_max_nest);
  365         if (error) {
  366                 m_freem(m);
  367                 return (error);
  368         }
  369 
  370         /* BPF writes need to be handled specially. */
  371         if (dst->sa_family == AF_UNSPEC)
  372                 bcopy(dst->sa_data, &af, sizeof(af));
  373         else
  374                 af = RO_GET_FAMILY(ro, dst);
  375 
  376         /* Berkeley packet filter */
  377         ng_iface_bpftap(ifp, m, af);
  378 
  379         if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
  380                 M_PREPEND(m, sizeof(sa_family_t), M_NOWAIT);
  381                 if (m == NULL) {
  382                         if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
  383                         return (ENOBUFS);
  384                 }
  385                 *(sa_family_t *)m->m_data = af;
  386                 error = (ifp->if_transmit)(ifp, m);
  387         } else
  388                 error = ng_iface_send(ifp, m, af);
  389 
  390         return (error);
  391 }
  392 
  393 /*
  394  * Start method is used only when ALTQ is enabled.
  395  */
  396 static void
  397 ng_iface_start(struct ifnet *ifp)
  398 {
  399         struct mbuf *m;
  400         sa_family_t sa;
  401 
  402         KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
  403 
  404         for(;;) {
  405                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
  406                 if (m == NULL)
  407                         break;
  408                 sa = *mtod(m, sa_family_t *);
  409                 m_adj(m, sizeof(sa_family_t));
  410                 ng_iface_send(ifp, m, sa);
  411         }
  412 }
  413 
  414 /*
  415  * Flash a packet by the BPF (requires prepending 4 byte AF header)
  416  * Note the phoney mbuf; this is OK because BPF treats it read-only.
  417  */
  418 static void
  419 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
  420 {
  421         KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
  422         if (bpf_peers_present(ifp->if_bpf)) {
  423                 int32_t family4 = (int32_t)family;
  424                 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
  425         }
  426 }
  427 
  428 /*
  429  * This routine does actual delivery of the packet into the
  430  * netgraph(4). It is called from ng_iface_start() and
  431  * ng_iface_output().
  432  */
  433 static int
  434 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
  435 {
  436         struct rm_priotracker priv_tracker;
  437         const priv_p priv = (priv_p) ifp->if_softc;
  438         const iffam_p iffam = get_iffam_from_af(sa);
  439         hook_p hook;
  440         int error;
  441         int len;
  442 
  443         /* Check address family to determine hook (if known) */
  444         if (iffam == NULL) {
  445                 m_freem(m);
  446                 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
  447                 return (EAFNOSUPPORT);
  448         }
  449 
  450         /* Copy length before the mbuf gets invalidated. */
  451         len = m->m_pkthdr.len;
  452 
  453         PRIV_RLOCK(priv, &priv_tracker);
  454         hook = *get_hook_from_iffam(priv, iffam);
  455         if (hook == NULL) {
  456                 NG_FREE_M(m);
  457                 PRIV_RUNLOCK(priv, &priv_tracker);
  458                 return ENETDOWN;
  459         }
  460         NG_HOOK_REF(hook);
  461         PRIV_RUNLOCK(priv, &priv_tracker);
  462 
  463         NG_OUTBOUND_THREAD_REF();
  464         NG_SEND_DATA_ONLY(error, hook, m);
  465         NG_OUTBOUND_THREAD_UNREF();
  466         NG_HOOK_UNREF(hook);
  467 
  468         /* Update stats. */
  469         if (error == 0) {
  470                 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
  471                 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
  472         }
  473 
  474         return (error);
  475 }
  476 
  477 #ifdef DEBUG
  478 /*
  479  * Display an ioctl to the virtual interface
  480  */
  481 
  482 static void
  483 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
  484 {
  485         char   *str;
  486 
  487         switch (command & IOC_DIRMASK) {
  488         case IOC_VOID:
  489                 str = "IO";
  490                 break;
  491         case IOC_OUT:
  492                 str = "IOR";
  493                 break;
  494         case IOC_IN:
  495                 str = "IOW";
  496                 break;
  497         case IOC_INOUT:
  498                 str = "IORW";
  499                 break;
  500         default:
  501                 str = "IO??";
  502         }
  503         log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
  504                ifp->if_xname,
  505                str,
  506                IOCGROUP(command),
  507                command & 0xff,
  508                IOCPARM_LEN(command));
  509 }
  510 #endif /* DEBUG */
  511 
  512 /************************************************************************
  513                         NETGRAPH NODE STUFF
  514  ************************************************************************/
  515 
  516 /*
  517  * Constructor for a node
  518  */
  519 static int
  520 ng_iface_constructor(node_p node)
  521 {
  522         struct ifnet *ifp;
  523         priv_p priv;
  524 
  525         /* Allocate node and interface private structures */
  526         priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | M_ZERO);
  527         ifp = if_alloc(IFT_PROPVIRTUAL);
  528         if (ifp == NULL) {
  529                 free(priv, M_NETGRAPH_IFACE);
  530                 return (ENOMEM);
  531         }
  532 
  533         rm_init(&priv->lock, "ng_iface private rmlock");
  534 
  535         /* Link them together */
  536         ifp->if_softc = priv;
  537         priv->ifp = ifp;
  538 
  539         /* Get an interface unit number */
  540         priv->unit = alloc_unr(V_ng_iface_unit);
  541 
  542         /* Link together node and private info */
  543         NG_NODE_SET_PRIVATE(node, priv);
  544         priv->node = node;
  545 
  546         /* Initialize interface structure */
  547         if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
  548         ifp->if_output = ng_iface_output;
  549         ifp->if_start = ng_iface_start;
  550         ifp->if_ioctl = ng_iface_ioctl;
  551         ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
  552         ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
  553         ifp->if_type = IFT_PROPVIRTUAL;         /* XXX */
  554         ifp->if_addrlen = 0;                    /* XXX */
  555         ifp->if_hdrlen = 0;                     /* XXX */
  556         ifp->if_baudrate = 64000;               /* XXX */
  557         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
  558         ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
  559         IFQ_SET_READY(&ifp->if_snd);
  560 
  561         /* Give this node the same name as the interface (if possible) */
  562         if (ng_name_node(node, ifp->if_xname) != 0)
  563                 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
  564                     ifp->if_xname);
  565 
  566         /* Attach the interface */
  567         if_attach(ifp);
  568         bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
  569 
  570         /* Done */
  571         return (0);
  572 }
  573 
  574 /*
  575  * Give our ok for a hook to be added
  576  */
  577 static int
  578 ng_iface_newhook(node_p node, hook_p hook, const char *name)
  579 {
  580         const iffam_p iffam = get_iffam_from_name(name);
  581         const priv_p priv = NG_NODE_PRIVATE(node);
  582         hook_p *hookptr;
  583 
  584         if (iffam == NULL)
  585                 return (EPFNOSUPPORT);
  586         PRIV_WLOCK(priv);
  587         hookptr = get_hook_from_iffam(priv, iffam);
  588         if (*hookptr != NULL) {
  589                 PRIV_WUNLOCK(priv);
  590                 return (EISCONN);
  591         }
  592         *hookptr = hook;
  593         NG_HOOK_HI_STACK(hook);
  594         NG_HOOK_SET_TO_INBOUND(hook);
  595         PRIV_WUNLOCK(priv);
  596         return (0);
  597 }
  598 
  599 /*
  600  * Receive a control message
  601  */
  602 static int
  603 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
  604 {
  605         const priv_p priv = NG_NODE_PRIVATE(node);
  606         struct ifnet *const ifp = priv->ifp;
  607         struct ng_mesg *resp = NULL;
  608         int error = 0;
  609         struct ng_mesg *msg;
  610 
  611         NGI_GET_MSG(item, msg);
  612         switch (msg->header.typecookie) {
  613         case NGM_IFACE_COOKIE:
  614                 switch (msg->header.cmd) {
  615                 case NGM_IFACE_GET_IFNAME:
  616                         NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
  617                         if (resp == NULL) {
  618                                 error = ENOMEM;
  619                                 break;
  620                         }
  621                         strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
  622                         break;
  623 
  624                 case NGM_IFACE_POINT2POINT:
  625                 case NGM_IFACE_BROADCAST:
  626                     {
  627                         /* Deny request if interface is UP */
  628                         if ((ifp->if_flags & IFF_UP) != 0)
  629                                 return (EBUSY);
  630 
  631                         /* Change flags */
  632                         switch (msg->header.cmd) {
  633                         case NGM_IFACE_POINT2POINT:
  634                                 ifp->if_flags |= IFF_POINTOPOINT;
  635                                 ifp->if_flags &= ~IFF_BROADCAST;
  636                                 break;
  637                         case NGM_IFACE_BROADCAST:
  638                                 ifp->if_flags &= ~IFF_POINTOPOINT;
  639                                 ifp->if_flags |= IFF_BROADCAST;
  640                                 break;
  641                         }
  642                         break;
  643                     }
  644 
  645                 case NGM_IFACE_GET_IFINDEX:
  646                         NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
  647                         if (resp == NULL) {
  648                                 error = ENOMEM;
  649                                 break;
  650                         }
  651                         *((uint32_t *)resp->data) = priv->ifp->if_index;
  652                         break;
  653 
  654                 default:
  655                         error = EINVAL;
  656                         break;
  657                 }
  658                 break;
  659         case NGM_FLOW_COOKIE:
  660                 switch (msg->header.cmd) {
  661                 case NGM_LINK_IS_UP:
  662                         if_link_state_change(ifp, LINK_STATE_UP);
  663                         break;
  664                 case NGM_LINK_IS_DOWN:
  665                         if_link_state_change(ifp, LINK_STATE_DOWN);
  666                         break;
  667                 default:
  668                         break;
  669                 }
  670                 break;
  671         default:
  672                 error = EINVAL;
  673                 break;
  674         }
  675         NG_RESPOND_MSG(error, node, item, resp);
  676         NG_FREE_MSG(msg);
  677         return (error);
  678 }
  679 
  680 /*
  681  * Recive data from a hook. Pass the packet to the correct input routine.
  682  */
  683 static int
  684 ng_iface_rcvdata(hook_p hook, item_p item)
  685 {
  686         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
  687         const iffam_p iffam = get_iffam_from_hook(priv, hook);
  688         struct ifnet *const ifp = priv->ifp;
  689         struct epoch_tracker et;
  690         struct mbuf *m;
  691         int isr;
  692 
  693         NGI_GET_M(item, m);
  694         NG_FREE_ITEM(item);
  695         /* Sanity checks */
  696         KASSERT(iffam != NULL, ("%s: iffam", __func__));
  697         M_ASSERTPKTHDR(m);
  698         if ((ifp->if_flags & IFF_UP) == 0) {
  699                 NG_FREE_M(m);
  700                 return (ENETDOWN);
  701         }
  702 
  703         /* Update interface stats */
  704         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
  705         if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
  706 
  707         /* Note receiving interface */
  708         m->m_pkthdr.rcvif = ifp;
  709 
  710         /* Berkeley packet filter */
  711         ng_iface_bpftap(ifp, m, iffam->family);
  712 
  713         /* Send packet */
  714         switch (iffam->family) {
  715 #ifdef INET
  716         case AF_INET:
  717                 isr = NETISR_IP;
  718                 break;
  719 #endif
  720 #ifdef INET6
  721         case AF_INET6:
  722                 isr = NETISR_IPV6;
  723                 break;
  724 #endif
  725         default:
  726                 m_freem(m);
  727                 return (EAFNOSUPPORT);
  728         }
  729         random_harvest_queue(m, sizeof(*m), RANDOM_NET_NG);
  730         M_SETFIB(m, ifp->if_fib);
  731         CURVNET_SET(ifp->if_vnet);
  732         NET_EPOCH_ENTER(et);
  733         netisr_dispatch(isr, m);
  734         NET_EPOCH_EXIT(et);
  735         CURVNET_RESTORE();
  736         return (0);
  737 }
  738 
  739 /*
  740  * Shutdown and remove the node and its associated interface.
  741  */
  742 static int
  743 ng_iface_shutdown(node_p node)
  744 {
  745         const priv_p priv = NG_NODE_PRIVATE(node);
  746 
  747         /*
  748          * The ifnet may be in a different vnet than the netgraph node, 
  749          * hence we have to change the current vnet context here.
  750          */
  751         CURVNET_SET_QUIET(priv->ifp->if_vnet);
  752         bpfdetach(priv->ifp);
  753         if_detach(priv->ifp);
  754         if_free(priv->ifp);
  755         CURVNET_RESTORE();
  756         priv->ifp = NULL;
  757         free_unr(V_ng_iface_unit, priv->unit);
  758         rm_destroy(&priv->lock);
  759         free(priv, M_NETGRAPH_IFACE);
  760         NG_NODE_SET_PRIVATE(node, NULL);
  761         NG_NODE_UNREF(node);
  762         return (0);
  763 }
  764 
  765 /*
  766  * Hook disconnection. Note that we do *not* shutdown when all
  767  * hooks have been disconnected.
  768  */
  769 static int
  770 ng_iface_disconnect(hook_p hook)
  771 {
  772         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
  773         const iffam_p iffam = get_iffam_from_hook(priv, hook);
  774 
  775         if (iffam == NULL)
  776                 panic("%s", __func__);
  777         PRIV_WLOCK(priv);
  778         *get_hook_from_iffam(priv, iffam) = NULL;
  779         PRIV_WUNLOCK(priv);
  780         return (0);
  781 }
  782 
  783 /*
  784  * Handle loading and unloading for this node type.
  785  */
  786 static int
  787 ng_iface_mod_event(module_t mod, int event, void *data)
  788 {
  789         int error = 0;
  790 
  791         switch (event) {
  792         case MOD_LOAD:
  793         case MOD_UNLOAD:
  794                 break;
  795         default:
  796                 error = EOPNOTSUPP;
  797                 break;
  798         }
  799         return (error);
  800 }
  801 
  802 static void
  803 vnet_ng_iface_init(const void *unused)
  804 {
  805 
  806         V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
  807 }
  808 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
  809     vnet_ng_iface_init, NULL);
  810 
  811 static void
  812 vnet_ng_iface_uninit(const void *unused)
  813 {
  814 
  815         delete_unrhdr(V_ng_iface_unit);
  816 }
  817 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
  818     vnet_ng_iface_uninit, NULL);

Cache object: f94561d3dc2c53c5585f92d2d1337b67


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