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, 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_iface_ifname */
  146 static const struct ng_parse_fixedstring_info ng_iface_ifname_info = {
  147         NG_IFACE_IFACE_NAME_MAX + 1
  148 };
  149 static const struct ng_parse_type ng_iface_ifname_type = {
  150         &ng_parse_fixedstring_type,
  151         &ng_iface_ifname_info
  152 };
  153 
  154 /* Parse type for struct ng_cisco_ipaddr */
  155 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
  156         = NG_CISCO_IPADDR_TYPE_INFO;
  157 static const struct ng_parse_type ng_cisco_ipaddr_type = {
  158         &ng_parse_struct_type,
  159         &ng_cisco_ipaddr_type_fields
  160 };
  161 
  162 /* List of commands and how to convert arguments to/from ASCII */
  163 static const struct ng_cmdlist ng_iface_cmds[] = {
  164         {
  165           NGM_IFACE_COOKIE,
  166           NGM_IFACE_GET_IFNAME,
  167           "getifname",
  168           NULL,
  169           &ng_iface_ifname_type
  170         },
  171         {
  172           NGM_IFACE_COOKIE,
  173           NGM_IFACE_POINT2POINT,
  174           "point2point",
  175           NULL,
  176           NULL
  177         },
  178         {
  179           NGM_IFACE_COOKIE,
  180           NGM_IFACE_BROADCAST,
  181           "broadcast",
  182           NULL,
  183           NULL
  184         },
  185         {
  186           NGM_CISCO_COOKIE,
  187           NGM_CISCO_GET_IPADDR,
  188           "getipaddr",
  189           NULL,
  190           &ng_cisco_ipaddr_type
  191         },
  192         {
  193           NGM_IFACE_COOKIE,
  194           NGM_IFACE_GET_IFINDEX,
  195           "getifindex",
  196           NULL,
  197           &ng_parse_uint32_type
  198         },
  199         { 0 }
  200 };
  201 
  202 /* Node type descriptor */
  203 static struct ng_type typestruct = {
  204         .version =      NG_ABI_VERSION,
  205         .name =         NG_IFACE_NODE_TYPE,
  206         .mod_event =    ng_iface_mod_event,
  207         .constructor =  ng_iface_constructor,
  208         .rcvmsg =       ng_iface_rcvmsg,
  209         .shutdown =     ng_iface_shutdown,
  210         .newhook =      ng_iface_newhook,
  211         .rcvdata =      ng_iface_rcvdata,
  212         .disconnect =   ng_iface_disconnect,
  213         .cmdlist =      ng_iface_cmds,
  214 };
  215 NETGRAPH_INIT(iface, &typestruct);
  216 
  217 static struct unrhdr    *ng_iface_unit;
  218 
  219 /************************************************************************
  220                         HELPER STUFF
  221  ************************************************************************/
  222 
  223 /*
  224  * Get the family descriptor from the family ID
  225  */
  226 static __inline iffam_p
  227 get_iffam_from_af(sa_family_t family)
  228 {
  229         iffam_p iffam;
  230         int k;
  231 
  232         for (k = 0; k < NUM_FAMILIES; k++) {
  233                 iffam = &gFamilies[k];
  234                 if (iffam->family == family)
  235                         return (iffam);
  236         }
  237         return (NULL);
  238 }
  239 
  240 /*
  241  * Get the family descriptor from the hook
  242  */
  243 static __inline iffam_p
  244 get_iffam_from_hook(priv_p priv, hook_p hook)
  245 {
  246         int k;
  247 
  248         for (k = 0; k < NUM_FAMILIES; k++)
  249                 if (priv->hooks[k] == hook)
  250                         return (&gFamilies[k]);
  251         return (NULL);
  252 }
  253 
  254 /*
  255  * Get the hook from the iffam descriptor
  256  */
  257 
  258 static __inline hook_p *
  259 get_hook_from_iffam(priv_p priv, iffam_p iffam)
  260 {
  261         return (&priv->hooks[iffam - gFamilies]);
  262 }
  263 
  264 /*
  265  * Get the iffam descriptor from the name
  266  */
  267 static __inline iffam_p
  268 get_iffam_from_name(const char *name)
  269 {
  270         iffam_p iffam;
  271         int k;
  272 
  273         for (k = 0; k < NUM_FAMILIES; k++) {
  274                 iffam = &gFamilies[k];
  275                 if (!strcmp(iffam->hookname, name))
  276                         return (iffam);
  277         }
  278         return (NULL);
  279 }
  280 
  281 /************************************************************************
  282                         INTERFACE STUFF
  283  ************************************************************************/
  284 
  285 /*
  286  * Process an ioctl for the virtual interface
  287  */
  288 static int
  289 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
  290 {
  291         struct ifreq *const ifr = (struct ifreq *) data;
  292         int s, error = 0;
  293 
  294 #ifdef DEBUG
  295         ng_iface_print_ioctl(ifp, command, data);
  296 #endif
  297         s = splimp();
  298         switch (command) {
  299 
  300         /* These two are mostly handled at a higher layer */
  301         case SIOCSIFADDR:
  302                 ifp->if_flags |= (IFF_UP | IFF_RUNNING);
  303                 ifp->if_flags &= ~(IFF_OACTIVE);
  304                 break;
  305         case SIOCGIFADDR:
  306                 break;
  307 
  308         /* Set flags */
  309         case SIOCSIFFLAGS:
  310                 /*
  311                  * If the interface is marked up and stopped, then start it.
  312                  * If it is marked down and running, then stop it.
  313                  */
  314                 if (ifr->ifr_flags & IFF_UP) {
  315                         if (!(ifp->if_flags & IFF_RUNNING)) {
  316                                 ifp->if_flags &= ~(IFF_OACTIVE);
  317                                 ifp->if_flags |= IFF_RUNNING;
  318                         }
  319                 } else {
  320                         if (ifp->if_flags & IFF_RUNNING)
  321                                 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
  322                 }
  323                 break;
  324 
  325         /* Set the interface MTU */
  326         case SIOCSIFMTU:
  327                 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
  328                     || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
  329                         error = EINVAL;
  330                 else
  331                         ifp->if_mtu = ifr->ifr_mtu;
  332                 break;
  333 
  334         /* Stuff that's not supported */
  335         case SIOCADDMULTI:
  336         case SIOCDELMULTI:
  337                 error = 0;
  338                 break;
  339         case SIOCSIFPHYS:
  340                 error = EOPNOTSUPP;
  341                 break;
  342 
  343         default:
  344                 error = EINVAL;
  345                 break;
  346         }
  347         (void) splx(s);
  348         return (error);
  349 }
  350 
  351 /*
  352  * This routine is called to deliver a packet out the interface.
  353  * We simply look at the address family and relay the packet to
  354  * the corresponding hook, if it exists and is connected.
  355  */
  356 
  357 static int
  358 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
  359                 struct sockaddr *dst, struct rtentry *rt0)
  360 {
  361         const priv_p priv = (priv_p) ifp->if_softc;
  362         const iffam_p iffam = get_iffam_from_af(dst->sa_family);
  363         int len, error = 0;
  364 
  365         /* Check interface flags */
  366         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
  367                 m_freem(m);
  368                 return (ENETDOWN);
  369         }
  370 
  371         /* BPF writes need to be handled specially */
  372         if (dst->sa_family == AF_UNSPEC) {
  373                 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL)
  374                         return (ENOBUFS);
  375                 dst->sa_family = (sa_family_t)*mtod(m, int32_t *);
  376                 m->m_data += 4;
  377                 m->m_len -= 4;
  378                 m->m_pkthdr.len -= 4;
  379         }
  380 
  381         /* Berkeley packet filter */
  382         ng_iface_bpftap(ifp, m, dst->sa_family);
  383 
  384         /* Check address family to determine hook (if known) */
  385         if (iffam == NULL) {
  386                 m_freem(m);
  387                 log(LOG_WARNING, "%s: can't handle af%d\n",
  388                        ifp->if_xname, (int)dst->sa_family);
  389                 return (EAFNOSUPPORT);
  390         }
  391 
  392         /* Copy length before the mbuf gets invalidated */
  393         len = m->m_pkthdr.len;
  394 
  395         /* Send packet; if hook is not connected, mbuf will get freed. */
  396         NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m);
  397 
  398         /* Update stats */
  399         if (error == 0) {
  400                 ifp->if_obytes += len;
  401                 ifp->if_opackets++;
  402         }
  403         return (error);
  404 }
  405 
  406 /*
  407  * This routine should never be called
  408  */
  409 
  410 static void
  411 ng_iface_start(struct ifnet *ifp)
  412 {
  413         if_printf(ifp, "%s called?", __func__);
  414 }
  415 
  416 /*
  417  * Flash a packet by the BPF (requires prepending 4 byte AF header)
  418  * Note the phoney mbuf; this is OK because BPF treats it read-only.
  419  */
  420 static void
  421 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
  422 {
  423         KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
  424         if (ifp->if_bpf != NULL) {
  425                 int32_t family4 = (int32_t)family;
  426                 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
  427         }
  428 }
  429 
  430 #ifdef DEBUG
  431 /*
  432  * Display an ioctl to the virtual interface
  433  */
  434 
  435 static void
  436 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
  437 {
  438         char   *str;
  439 
  440         switch (command & IOC_DIRMASK) {
  441         case IOC_VOID:
  442                 str = "IO";
  443                 break;
  444         case IOC_OUT:
  445                 str = "IOR";
  446                 break;
  447         case IOC_IN:
  448                 str = "IOW";
  449                 break;
  450         case IOC_INOUT:
  451                 str = "IORW";
  452                 break;
  453         default:
  454                 str = "IO??";
  455         }
  456         log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
  457                ifp->if_xname,
  458                str,
  459                IOCGROUP(command),
  460                command & 0xff,
  461                IOCPARM_LEN(command));
  462 }
  463 #endif /* DEBUG */
  464 
  465 /************************************************************************
  466                         NETGRAPH NODE STUFF
  467  ************************************************************************/
  468 
  469 /*
  470  * Constructor for a node
  471  */
  472 static int
  473 ng_iface_constructor(node_p node)
  474 {
  475         char ifname[NG_IFACE_IFACE_NAME_MAX + 1];
  476         struct ifnet *ifp;
  477         priv_p priv;
  478 
  479         /* Allocate node and interface private structures */
  480         MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_IFACE, M_NOWAIT|M_ZERO);
  481         if (priv == NULL)
  482                 return (ENOMEM);
  483         MALLOC(ifp, struct ifnet *, sizeof(*ifp), M_NETGRAPH_IFACE, M_NOWAIT|M_ZERO);
  484         if (ifp == NULL) {
  485                 FREE(priv, M_NETGRAPH_IFACE);
  486                 return (ENOMEM);
  487         }
  488 
  489         /* Link them together */
  490         ifp->if_softc = priv;
  491         priv->ifp = ifp;
  492 
  493         /* Get an interface unit number */
  494         priv->unit = alloc_unr(ng_iface_unit);
  495 
  496         /* Link together node and private info */
  497         NG_NODE_SET_PRIVATE(node, priv);
  498         priv->node = node;
  499 
  500         /* Initialize interface structure */
  501         if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
  502         ifp->if_output = ng_iface_output;
  503         ifp->if_start = ng_iface_start;
  504         ifp->if_ioctl = ng_iface_ioctl;
  505         ifp->if_watchdog = NULL;
  506         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
  507         ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
  508         ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
  509         ifp->if_type = IFT_PROPVIRTUAL;         /* XXX */
  510         ifp->if_addrlen = 0;                    /* XXX */
  511         ifp->if_hdrlen = 0;                     /* XXX */
  512         ifp->if_baudrate = 64000;               /* XXX */
  513         TAILQ_INIT(&ifp->if_addrhead);
  514 
  515         /* Give this node the same name as the interface (if possible) */
  516         bzero(ifname, sizeof(ifname));
  517         strlcpy(ifname, ifp->if_xname, sizeof(ifname));
  518         if (ng_name_node(node, ifname) != 0)
  519                 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
  520 
  521         /* Attach the interface */
  522         if_attach(ifp);
  523         bpfattach(ifp, DLT_NULL, sizeof(u_int));
  524 
  525         /* Done */
  526         return (0);
  527 }
  528 
  529 /*
  530  * Give our ok for a hook to be added
  531  */
  532 static int
  533 ng_iface_newhook(node_p node, hook_p hook, const char *name)
  534 {
  535         const iffam_p iffam = get_iffam_from_name(name);
  536         hook_p *hookptr;
  537 
  538         if (iffam == NULL)
  539                 return (EPFNOSUPPORT);
  540         hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
  541         if (*hookptr != NULL)
  542                 return (EISCONN);
  543         *hookptr = hook;
  544         return (0);
  545 }
  546 
  547 /*
  548  * Receive a control message
  549  */
  550 static int
  551 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
  552 {
  553         const priv_p priv = NG_NODE_PRIVATE(node);
  554         struct ifnet *const ifp = priv->ifp;
  555         struct ng_mesg *resp = NULL;
  556         int error = 0;
  557         struct ng_mesg *msg;
  558 
  559         NGI_GET_MSG(item, msg);
  560         switch (msg->header.typecookie) {
  561         case NGM_IFACE_COOKIE:
  562                 switch (msg->header.cmd) {
  563                 case NGM_IFACE_GET_IFNAME:
  564                     {
  565                         struct ng_iface_ifname *arg;
  566 
  567                         NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT);
  568                         if (resp == NULL) {
  569                                 error = ENOMEM;
  570                                 break;
  571                         }
  572                         arg = (struct ng_iface_ifname *)resp->data;
  573                         strlcpy(arg->ngif_name, ifp->if_xname,
  574                             sizeof(arg->ngif_name));
  575                         break;
  576                     }
  577 
  578                 case NGM_IFACE_POINT2POINT:
  579                 case NGM_IFACE_BROADCAST:
  580                     {
  581 
  582                         /* Deny request if interface is UP */
  583                         if ((ifp->if_flags & IFF_UP) != 0)
  584                                 return (EBUSY);
  585 
  586                         /* Change flags */
  587                         switch (msg->header.cmd) {
  588                         case NGM_IFACE_POINT2POINT:
  589                                 ifp->if_flags |= IFF_POINTOPOINT;
  590                                 ifp->if_flags &= ~IFF_BROADCAST;
  591                                 break;
  592                         case NGM_IFACE_BROADCAST:
  593                                 ifp->if_flags &= ~IFF_POINTOPOINT;
  594                                 ifp->if_flags |= IFF_BROADCAST;
  595                                 break;
  596                         }
  597                         break;
  598                     }
  599 
  600                 case NGM_IFACE_GET_IFINDEX:
  601                         NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
  602                         if (resp == NULL) {
  603                                 error = ENOMEM;
  604                                 break;
  605                         }
  606                         *((uint32_t *)resp->data) = priv->ifp->if_index;
  607                         break;
  608 
  609                 default:
  610                         error = EINVAL;
  611                         break;
  612                 }
  613                 break;
  614         case NGM_CISCO_COOKIE:
  615                 switch (msg->header.cmd) {
  616                 case NGM_CISCO_GET_IPADDR:      /* we understand this too */
  617                     {
  618                         struct ifaddr *ifa;
  619 
  620                         /* Return the first configured IP address */
  621                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  622                                 struct ng_cisco_ipaddr *ips;
  623 
  624                                 if (ifa->ifa_addr->sa_family != AF_INET)
  625                                         continue;
  626                                 NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
  627                                 if (resp == NULL) {
  628                                         error = ENOMEM;
  629                                         break;
  630                                 }
  631                                 ips = (struct ng_cisco_ipaddr *)resp->data;
  632                                 ips->ipaddr = ((struct sockaddr_in *)
  633                                                 ifa->ifa_addr)->sin_addr;
  634                                 ips->netmask = ((struct sockaddr_in *)
  635                                                 ifa->ifa_netmask)->sin_addr;
  636                                 break;
  637                         }
  638 
  639                         /* No IP addresses on this interface? */
  640                         if (ifa == NULL)
  641                                 error = EADDRNOTAVAIL;
  642                         break;
  643                     }
  644                 default:
  645                         error = EINVAL;
  646                         break;
  647                 }
  648                 break;
  649         case NGM_FLOW_COOKIE:
  650                 switch (msg->header.cmd) {
  651                 case NGM_LINK_IS_UP:
  652                         ifp->if_flags |= IFF_UP;
  653                         break;
  654                 case NGM_LINK_IS_DOWN:
  655                         ifp->if_flags &= ~IFF_UP;
  656                         break;
  657                 default:
  658                         break;
  659                 }
  660                 break;
  661         default:
  662                 error = EINVAL;
  663                 break;
  664         }
  665         NG_RESPOND_MSG(error, node, item, resp);
  666         NG_FREE_MSG(msg);
  667         return (error);
  668 }
  669 
  670 /*
  671  * Recive data from a hook. Pass the packet to the correct input routine.
  672  */
  673 static int
  674 ng_iface_rcvdata(hook_p hook, item_p item)
  675 {
  676         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
  677         const iffam_p iffam = get_iffam_from_hook(priv, hook);
  678         struct ifnet *const ifp = priv->ifp;
  679         struct mbuf *m;
  680         int isr;
  681 
  682         NGI_GET_M(item, m);
  683         NG_FREE_ITEM(item);
  684         /* Sanity checks */
  685         KASSERT(iffam != NULL, ("%s: iffam", __func__));
  686         M_ASSERTPKTHDR(m);
  687         if ((ifp->if_flags & IFF_UP) == 0) {
  688                 NG_FREE_M(m);
  689                 return (ENETDOWN);
  690         }
  691 
  692         /* Update interface stats */
  693         ifp->if_ipackets++;
  694         ifp->if_ibytes += m->m_pkthdr.len;
  695 
  696         /* Note receiving interface */
  697         m->m_pkthdr.rcvif = ifp;
  698 
  699         /* Berkeley packet filter */
  700         ng_iface_bpftap(ifp, m, iffam->family);
  701 
  702         /* Send packet */
  703         switch (iffam->family) {
  704 #ifdef INET
  705         case AF_INET:
  706                 isr = NETISR_IP;
  707                 break;
  708 #endif
  709 #ifdef INET6
  710         case AF_INET6:
  711                 isr = NETISR_IPV6;
  712                 break;
  713 #endif
  714 #ifdef IPX
  715         case AF_IPX:
  716                 isr = NETISR_IPX;
  717                 break;
  718 #endif
  719 #ifdef NETATALK
  720         case AF_APPLETALK:
  721                 isr = NETISR_ATALK2;
  722                 break;
  723 #endif
  724         default:
  725                 m_freem(m);
  726                 return (EAFNOSUPPORT);
  727         }
  728         /* First chunk of an mbuf contains good junk */
  729         if (harvest.point_to_point)
  730                 random_harvest(m, 16, 3, 0, RANDOM_NET);
  731         netisr_dispatch(isr, m);
  732         return (0);
  733 }
  734 
  735 /*
  736  * Shutdown and remove the node and its associated interface.
  737  */
  738 static int
  739 ng_iface_shutdown(node_p node)
  740 {
  741         const priv_p priv = NG_NODE_PRIVATE(node);
  742 
  743         bpfdetach(priv->ifp);
  744         if_detach(priv->ifp);
  745         FREE(priv->ifp, M_NETGRAPH_IFACE);
  746         priv->ifp = NULL;
  747         free_unr(ng_iface_unit, priv->unit);
  748         FREE(priv, M_NETGRAPH_IFACE);
  749         NG_NODE_SET_PRIVATE(node, NULL);
  750         NG_NODE_UNREF(node);
  751         return (0);
  752 }
  753 
  754 /*
  755  * Hook disconnection. Note that we do *not* shutdown when all
  756  * hooks have been disconnected.
  757  */
  758 static int
  759 ng_iface_disconnect(hook_p hook)
  760 {
  761         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
  762         const iffam_p iffam = get_iffam_from_hook(priv, hook);
  763 
  764         if (iffam == NULL)
  765                 panic(__func__);
  766         *get_hook_from_iffam(priv, iffam) = NULL;
  767         return (0);
  768 }
  769 
  770 /*
  771  * Handle loading and unloading for this node type.
  772  */
  773 static int
  774 ng_iface_mod_event(module_t mod, int event, void *data)
  775 {
  776         int error = 0;
  777 
  778         switch (event) {
  779         case MOD_LOAD:
  780                 ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
  781                 break;
  782         case MOD_UNLOAD:
  783                 delete_unrhdr(ng_iface_unit);
  784                 break;
  785         default:
  786                 error = EOPNOTSUPP;
  787                 break;
  788         }
  789         return (error);
  790 }

Cache object: 37bd5a2fed40ab6a7018ad6ee40af723


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