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

Cache object: 3d903cd7bdbe1ee95f0f123a4e6be8a5


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