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

Cache object: bbfcb696297674bc4f9583dcb42b1c60


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