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: stable/12/sys/netgraph/ng_iface.c 364015 2020-08-07 13:42:47Z markj $
   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, 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 
  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         return (error);
  341 }
  342 
  343 /*
  344  * This routine is called to deliver a packet out the interface.
  345  * We simply look at the address family and relay the packet to
  346  * the corresponding hook, if it exists and is connected.
  347  */
  348 
  349 static int
  350 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
  351         const struct sockaddr *dst, struct route *ro)
  352 {
  353         uint32_t af;
  354         int error;
  355 
  356         /* Check interface flags */
  357         if (!((ifp->if_flags & IFF_UP) &&
  358             (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
  359                 m_freem(m);
  360                 return (ENETDOWN);
  361         }
  362 
  363         /* Protect from deadly infinite recursion. */
  364         error = if_tunnel_check_nesting(ifp, m, NGM_IFACE_COOKIE,
  365             V_ng_iface_max_nest);
  366         if (error) {
  367                 m_freem(m);
  368                 return (error);
  369         }
  370 
  371         /* BPF writes need to be handled specially. */
  372         if (dst->sa_family == AF_UNSPEC)
  373                 bcopy(dst->sa_data, &af, sizeof(af));
  374         else
  375                 af = dst->sa_family;
  376 
  377         /* Berkeley packet filter */
  378         ng_iface_bpftap(ifp, m, af);
  379 
  380         if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
  381                 M_PREPEND(m, sizeof(sa_family_t), M_NOWAIT);
  382                 if (m == NULL) {
  383                         if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
  384                         return (ENOBUFS);
  385                 }
  386                 *(sa_family_t *)m->m_data = af;
  387                 error = (ifp->if_transmit)(ifp, m);
  388         } else
  389                 error = ng_iface_send(ifp, m, af);
  390 
  391         return (error);
  392 }
  393 
  394 /*
  395  * Start method is used only when ALTQ is enabled.
  396  */
  397 static void
  398 ng_iface_start(struct ifnet *ifp)
  399 {
  400         struct mbuf *m;
  401         sa_family_t sa;
  402 
  403         KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
  404 
  405         for(;;) {
  406                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
  407                 if (m == NULL)
  408                         break;
  409                 sa = *mtod(m, sa_family_t *);
  410                 m_adj(m, sizeof(sa_family_t));
  411                 ng_iface_send(ifp, m, sa);
  412         }
  413 }
  414 
  415 /*
  416  * Flash a packet by the BPF (requires prepending 4 byte AF header)
  417  * Note the phoney mbuf; this is OK because BPF treats it read-only.
  418  */
  419 static void
  420 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
  421 {
  422         KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
  423         if (bpf_peers_present(ifp->if_bpf)) {
  424                 int32_t family4 = (int32_t)family;
  425                 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
  426         }
  427 }
  428 
  429 /*
  430  * This routine does actual delivery of the packet into the
  431  * netgraph(4). It is called from ng_iface_start() and
  432  * ng_iface_output().
  433  */
  434 static int
  435 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
  436 {
  437         struct rm_priotracker priv_tracker;
  438         const priv_p priv = (priv_p) ifp->if_softc;
  439         const iffam_p iffam = get_iffam_from_af(sa);
  440         hook_p hook;
  441         int error;
  442         int len;
  443 
  444         /* Check address family to determine hook (if known) */
  445         if (iffam == NULL) {
  446                 m_freem(m);
  447                 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
  448                 return (EAFNOSUPPORT);
  449         }
  450 
  451         /* Copy length before the mbuf gets invalidated. */
  452         len = m->m_pkthdr.len;
  453 
  454         PRIV_RLOCK(priv, &priv_tracker);
  455         hook = *get_hook_from_iffam(priv, iffam);
  456         if (hook == NULL) {
  457                 NG_FREE_M(m);
  458                 PRIV_RUNLOCK(priv, &priv_tracker);
  459                 return ENETDOWN;
  460         }
  461         NG_HOOK_REF(hook);
  462         PRIV_RUNLOCK(priv, &priv_tracker);
  463 
  464         NG_OUTBOUND_THREAD_REF();
  465         NG_SEND_DATA_ONLY(error, hook, m);
  466         NG_OUTBOUND_THREAD_UNREF();
  467         NG_HOOK_UNREF(hook);
  468 
  469         /* Update stats. */
  470         if (error == 0) {
  471                 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
  472                 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
  473         }
  474 
  475         return (error);
  476 }
  477 
  478 #ifdef DEBUG
  479 /*
  480  * Display an ioctl to the virtual interface
  481  */
  482 
  483 static void
  484 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
  485 {
  486         char   *str;
  487 
  488         switch (command & IOC_DIRMASK) {
  489         case IOC_VOID:
  490                 str = "IO";
  491                 break;
  492         case IOC_OUT:
  493                 str = "IOR";
  494                 break;
  495         case IOC_IN:
  496                 str = "IOW";
  497                 break;
  498         case IOC_INOUT:
  499                 str = "IORW";
  500                 break;
  501         default:
  502                 str = "IO??";
  503         }
  504         log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
  505                ifp->if_xname,
  506                str,
  507                IOCGROUP(command),
  508                command & 0xff,
  509                IOCPARM_LEN(command));
  510 }
  511 #endif /* DEBUG */
  512 
  513 /************************************************************************
  514                         NETGRAPH NODE STUFF
  515  ************************************************************************/
  516 
  517 /*
  518  * Constructor for a node
  519  */
  520 static int
  521 ng_iface_constructor(node_p node)
  522 {
  523         struct ifnet *ifp;
  524         priv_p priv;
  525 
  526         /* Allocate node and interface private structures */
  527         priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | M_ZERO);
  528         ifp = if_alloc(IFT_PROPVIRTUAL);
  529         if (ifp == NULL) {
  530                 free(priv, M_NETGRAPH_IFACE);
  531                 return (ENOMEM);
  532         }
  533 
  534         rm_init(&priv->lock, "ng_iface private rmlock");
  535 
  536         /* Link them together */
  537         ifp->if_softc = priv;
  538         priv->ifp = ifp;
  539 
  540         /* Get an interface unit number */
  541         priv->unit = alloc_unr(V_ng_iface_unit);
  542 
  543         /* Link together node and private info */
  544         NG_NODE_SET_PRIVATE(node, priv);
  545         priv->node = node;
  546 
  547         /* Initialize interface structure */
  548         if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
  549         ifp->if_output = ng_iface_output;
  550         ifp->if_start = ng_iface_start;
  551         ifp->if_ioctl = ng_iface_ioctl;
  552         ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
  553         ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
  554         ifp->if_type = IFT_PROPVIRTUAL;         /* XXX */
  555         ifp->if_addrlen = 0;                    /* XXX */
  556         ifp->if_hdrlen = 0;                     /* XXX */
  557         ifp->if_baudrate = 64000;               /* XXX */
  558         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
  559         ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
  560         IFQ_SET_READY(&ifp->if_snd);
  561 
  562         /* Give this node the same name as the interface (if possible) */
  563         if (ng_name_node(node, ifp->if_xname) != 0)
  564                 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
  565                     ifp->if_xname);
  566 
  567         /* Attach the interface */
  568         if_attach(ifp);
  569         bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
  570 
  571         /* Done */
  572         return (0);
  573 }
  574 
  575 /*
  576  * Give our ok for a hook to be added
  577  */
  578 static int
  579 ng_iface_newhook(node_p node, hook_p hook, const char *name)
  580 {
  581         const iffam_p iffam = get_iffam_from_name(name);
  582         const priv_p priv = NG_NODE_PRIVATE(node);
  583         hook_p *hookptr;
  584 
  585         if (iffam == NULL)
  586                 return (EPFNOSUPPORT);
  587         PRIV_WLOCK(priv);
  588         hookptr = get_hook_from_iffam(priv, iffam);
  589         if (*hookptr != NULL) {
  590                 PRIV_WUNLOCK(priv);
  591                 return (EISCONN);
  592         }
  593         *hookptr = hook;
  594         NG_HOOK_HI_STACK(hook);
  595         NG_HOOK_SET_TO_INBOUND(hook);
  596         PRIV_WUNLOCK(priv);
  597         return (0);
  598 }
  599 
  600 /*
  601  * Receive a control message
  602  */
  603 static int
  604 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
  605 {
  606         const priv_p priv = NG_NODE_PRIVATE(node);
  607         struct ifnet *const ifp = priv->ifp;
  608         struct ng_mesg *resp = NULL;
  609         int error = 0;
  610         struct ng_mesg *msg;
  611 
  612         NGI_GET_MSG(item, msg);
  613         switch (msg->header.typecookie) {
  614         case NGM_IFACE_COOKIE:
  615                 switch (msg->header.cmd) {
  616                 case NGM_IFACE_GET_IFNAME:
  617                         NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
  618                         if (resp == NULL) {
  619                                 error = ENOMEM;
  620                                 break;
  621                         }
  622                         strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
  623                         break;
  624 
  625                 case NGM_IFACE_POINT2POINT:
  626                 case NGM_IFACE_BROADCAST:
  627                     {
  628 
  629                         /* Deny request if interface is UP */
  630                         if ((ifp->if_flags & IFF_UP) != 0)
  631                                 return (EBUSY);
  632 
  633                         /* Change flags */
  634                         switch (msg->header.cmd) {
  635                         case NGM_IFACE_POINT2POINT:
  636                                 ifp->if_flags |= IFF_POINTOPOINT;
  637                                 ifp->if_flags &= ~IFF_BROADCAST;
  638                                 break;
  639                         case NGM_IFACE_BROADCAST:
  640                                 ifp->if_flags &= ~IFF_POINTOPOINT;
  641                                 ifp->if_flags |= IFF_BROADCAST;
  642                                 break;
  643                         }
  644                         break;
  645                     }
  646 
  647                 case NGM_IFACE_GET_IFINDEX:
  648                         NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
  649                         if (resp == NULL) {
  650                                 error = ENOMEM;
  651                                 break;
  652                         }
  653                         *((uint32_t *)resp->data) = priv->ifp->if_index;
  654                         break;
  655 
  656                 default:
  657                         error = EINVAL;
  658                         break;
  659                 }
  660                 break;
  661         case NGM_FLOW_COOKIE:
  662                 switch (msg->header.cmd) {
  663                 case NGM_LINK_IS_UP:
  664                         if_link_state_change(ifp, LINK_STATE_UP);
  665                         break;
  666                 case NGM_LINK_IS_DOWN:
  667                         if_link_state_change(ifp, LINK_STATE_DOWN);
  668                         break;
  669                 default:
  670                         break;
  671                 }
  672                 break;
  673         default:
  674                 error = EINVAL;
  675                 break;
  676         }
  677         NG_RESPOND_MSG(error, node, item, resp);
  678         NG_FREE_MSG(msg);
  679         return (error);
  680 }
  681 
  682 /*
  683  * Recive data from a hook. Pass the packet to the correct input routine.
  684  */
  685 static int
  686 ng_iface_rcvdata(hook_p hook, item_p item)
  687 {
  688         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
  689         const iffam_p iffam = get_iffam_from_hook(priv, hook);
  690         struct ifnet *const ifp = priv->ifp;
  691         struct mbuf *m;
  692         int isr;
  693 
  694         NGI_GET_M(item, m);
  695         NG_FREE_ITEM(item);
  696         /* Sanity checks */
  697         KASSERT(iffam != NULL, ("%s: iffam", __func__));
  698         M_ASSERTPKTHDR(m);
  699         if ((ifp->if_flags & IFF_UP) == 0) {
  700                 NG_FREE_M(m);
  701                 return (ENETDOWN);
  702         }
  703 
  704         /* Update interface stats */
  705         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
  706         if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
  707 
  708         /* Note receiving interface */
  709         m->m_pkthdr.rcvif = ifp;
  710 
  711         /* Berkeley packet filter */
  712         ng_iface_bpftap(ifp, m, iffam->family);
  713 
  714         /* Send packet */
  715         switch (iffam->family) {
  716 #ifdef INET
  717         case AF_INET:
  718                 isr = NETISR_IP;
  719                 break;
  720 #endif
  721 #ifdef INET6
  722         case AF_INET6:
  723                 isr = NETISR_IPV6;
  724                 break;
  725 #endif
  726         default:
  727                 m_freem(m);
  728                 return (EAFNOSUPPORT);
  729         }
  730         random_harvest_queue(m, sizeof(*m), RANDOM_NET_NG);
  731         M_SETFIB(m, ifp->if_fib);
  732         CURVNET_SET(ifp->if_vnet);
  733         netisr_dispatch(isr, m);
  734         CURVNET_RESTORE();
  735         return (0);
  736 }
  737 
  738 /*
  739  * Shutdown and remove the node and its associated interface.
  740  */
  741 static int
  742 ng_iface_shutdown(node_p node)
  743 {
  744         const priv_p priv = NG_NODE_PRIVATE(node);
  745 
  746         /*
  747          * The ifnet may be in a different vnet than the netgraph node, 
  748          * hence we have to change the current vnet context here.
  749          */
  750         CURVNET_SET_QUIET(priv->ifp->if_vnet);
  751         bpfdetach(priv->ifp);
  752         if_detach(priv->ifp);
  753         if_free(priv->ifp);
  754         CURVNET_RESTORE();
  755         priv->ifp = NULL;
  756         free_unr(V_ng_iface_unit, priv->unit);
  757         rm_destroy(&priv->lock);
  758         free(priv, M_NETGRAPH_IFACE);
  759         NG_NODE_SET_PRIVATE(node, NULL);
  760         NG_NODE_UNREF(node);
  761         return (0);
  762 }
  763 
  764 /*
  765  * Hook disconnection. Note that we do *not* shutdown when all
  766  * hooks have been disconnected.
  767  */
  768 static int
  769 ng_iface_disconnect(hook_p hook)
  770 {
  771         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
  772         const iffam_p iffam = get_iffam_from_hook(priv, hook);
  773 
  774         if (iffam == NULL)
  775                 panic("%s", __func__);
  776         PRIV_WLOCK(priv);
  777         *get_hook_from_iffam(priv, iffam) = NULL;
  778         PRIV_WUNLOCK(priv);
  779         return (0);
  780 }
  781 
  782 /*
  783  * Handle loading and unloading for this node type.
  784  */
  785 static int
  786 ng_iface_mod_event(module_t mod, int event, void *data)
  787 {
  788         int error = 0;
  789 
  790         switch (event) {
  791         case MOD_LOAD:
  792         case MOD_UNLOAD:
  793                 break;
  794         default:
  795                 error = EOPNOTSUPP;
  796                 break;
  797         }
  798         return (error);
  799 }
  800 
  801 static void
  802 vnet_ng_iface_init(const void *unused)
  803 {
  804 
  805         V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
  806 }
  807 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
  808     vnet_ng_iface_init, NULL);
  809 
  810 static void
  811 vnet_ng_iface_uninit(const void *unused)
  812 {
  813 
  814         delete_unrhdr(V_ng_iface_unit);
  815 }
  816 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
  817     vnet_ng_iface_uninit, NULL);

Cache object: e1f52736bbbd94464eb70750db0f70f0


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