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_sppp.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_sppp.c Netgraph to Sppp module.
    3  *
    4  * Copyright (C) 2002-2004 Cronyx Engineering.
    5  * Copyright (C) 2002-2004 Roman Kurakin <rik@cronyx.ru>
    6  *
    7  * This software is distributed with NO WARRANTIES, not even the implied
    8  * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    9  *
   10  * Authors grant any other persons or organisations a permission to use,
   11  * modify and redistribute this software in source and binary forms,
   12  * as long as this message is kept with the software, all derivative
   13  * works or modified versions.
   14  *
   15  * Cronyx Id: ng_sppp.c,v 1.1.2.10 2004/03/01 15:17:21 rik Exp $
   16  */
   17 #include <sys/cdefs.h>
   18 __FBSDID("$FreeBSD: releng/5.3/sys/netgraph/ng_sppp.c 136588 2004-10-16 08:43:07Z cvs2svn $");
   19 
   20 #include <sys/param.h>
   21 #include <sys/systm.h>
   22 #include <sys/errno.h>
   23 #include <sys/kernel.h>
   24 #include <sys/malloc.h>
   25 #include <sys/mbuf.h>
   26 #include <sys/errno.h>
   27 #include <sys/sockio.h>
   28 #include <sys/socket.h>
   29 #include <sys/syslog.h>
   30 #include <sys/libkern.h>
   31 
   32 #include <net/if.h>
   33 #include <net/if_types.h>
   34 #include <net/bpf.h>
   35 #include <net/if_sppp.h>
   36 
   37 #include <netinet/in.h>
   38 
   39 #include <netgraph/ng_message.h>
   40 #include <netgraph/netgraph.h>
   41 #include <netgraph/ng_parse.h>
   42 #include <netgraph/ng_sppp.h>
   43 
   44 #ifdef NG_SEPARATE_MALLOC
   45 MALLOC_DEFINE(M_NETGRAPH_SPPP, "netgraph_sppp", "netgraph sppp node ");
   46 #else
   47 #define M_NETGRAPH_SPPP M_NETGRAPH
   48 #endif
   49 
   50 /* Node private data */
   51 struct ng_sppp_private {
   52         struct  sppp *pp;               /* Our interface */
   53         int     unit;                   /* Interface unit number */
   54         node_p  node;                   /* Our netgraph node */
   55         hook_p  hook;                   /* Hook */
   56 };
   57 typedef struct ng_sppp_private *priv_p;
   58 
   59 /* Interface methods */
   60 static void     ng_sppp_start (struct ifnet *ifp);
   61 static int      ng_sppp_ioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
   62 
   63 /* Netgraph methods */
   64 static ng_constructor_t ng_sppp_constructor;
   65 static ng_rcvmsg_t      ng_sppp_rcvmsg;
   66 static ng_shutdown_t    ng_sppp_shutdown;
   67 static ng_newhook_t     ng_sppp_newhook;
   68 static ng_rcvdata_t     ng_sppp_rcvdata;
   69 static ng_disconnect_t  ng_sppp_disconnect;
   70 
   71 /* Parse type for struct ng_sppp_ifname */
   72 static const struct ng_parse_fixedstring_info ng_sppp_ifname_info = {
   73         NG_SPPP_IFACE_NAME_MAX + 1
   74 };
   75 
   76 static const struct ng_parse_type ng_sppp_ifname_type = {
   77         &ng_parse_fixedstring_type,
   78         &ng_sppp_ifname_info
   79 };
   80 
   81 /* List of commands and how to convert arguments to/from ASCII */
   82 static const struct ng_cmdlist ng_sppp_cmds[] = {
   83         {
   84           NGM_SPPP_COOKIE,
   85           NGM_SPPP_GET_IFNAME,
   86           "getifname",
   87           NULL,
   88           &ng_sppp_ifname_type
   89         },
   90         { 0 }
   91 };
   92 
   93 /* Node type descriptor */
   94 static struct ng_type typestruct = {
   95         .version =      NG_ABI_VERSION,
   96         .name =         NG_SPPP_NODE_TYPE,
   97         .constructor =  ng_sppp_constructor,
   98         .rcvmsg =       ng_sppp_rcvmsg,
   99         .shutdown =     ng_sppp_shutdown,
  100         .newhook =      ng_sppp_newhook,
  101         .rcvdata =      ng_sppp_rcvdata,
  102         .disconnect =   ng_sppp_disconnect,
  103         .cmdlist =      ng_sppp_cmds,
  104 };
  105 NETGRAPH_INIT(sppp, &typestruct);
  106 
  107 MODULE_DEPEND (ng_sppp, sppp, 1, 1, 1);
  108 
  109 /* We keep a bitmap indicating which unit numbers are free.
  110    Zero means the unit number is free, one means it's taken. */
  111 static unsigned char    *ng_sppp_units = NULL;
  112 static unsigned char    ng_sppp_units_len = 0;
  113 static unsigned char    ng_units_in_use = 0;
  114 
  115 /*
  116  * Find the first free unit number for a new interface.
  117  * Increase the size of the unit bitmap as necessary.
  118  */
  119 static __inline int
  120 ng_sppp_get_unit (int *unit)
  121 {
  122         int index, bit;
  123         unsigned char mask;
  124 
  125         for (index = 0; index < ng_sppp_units_len
  126             && ng_sppp_units[index] == 0xFF; index++);
  127         if (index == ng_sppp_units_len) {               /* extend array */
  128                 unsigned char *newarray;
  129                 int newlen;
  130                 
  131                 newlen = (2 * ng_sppp_units_len) + sizeof (*ng_sppp_units);
  132                 MALLOC (newarray, unsigned char *,
  133                     newlen * sizeof (*ng_sppp_units), M_NETGRAPH_SPPP, M_NOWAIT);
  134                 if (newarray == NULL)
  135                         return (ENOMEM);
  136                 bcopy (ng_sppp_units, newarray,
  137                     ng_sppp_units_len * sizeof (*ng_sppp_units));
  138                 bzero (newarray + ng_sppp_units_len,
  139                     newlen - ng_sppp_units_len);
  140                 if (ng_sppp_units != NULL)
  141                         FREE (ng_sppp_units, M_NETGRAPH_SPPP);
  142                 ng_sppp_units = newarray;
  143                 ng_sppp_units_len = newlen;
  144         }
  145         mask = ng_sppp_units[index];
  146         for (bit = 0; (mask & 1) != 0; bit++)
  147                 mask >>= 1;
  148         KASSERT ((bit >= 0 && bit < NBBY),
  149             ("%s: word=%d bit=%d", __func__, ng_sppp_units[index], bit));
  150         ng_sppp_units[index] |= (1 << bit);
  151         *unit = (index * NBBY) + bit;
  152         ng_units_in_use++;
  153         return (0);
  154 }
  155 
  156 /*
  157  * Free a no longer needed unit number.
  158  */
  159 static __inline void
  160 ng_sppp_free_unit (int unit)
  161 {
  162         int index, bit;
  163 
  164         index = unit / NBBY;
  165         bit = unit % NBBY;
  166         KASSERT (index < ng_sppp_units_len,
  167             ("%s: unit=%d len=%d", __func__, unit, ng_sppp_units_len));
  168         KASSERT ((ng_sppp_units[index] & (1 << bit)) != 0,
  169             ("%s: unit=%d is free", __func__, unit));
  170         ng_sppp_units[index] &= ~(1 << bit);
  171 
  172         ng_units_in_use--;
  173         if (ng_units_in_use == 0) {
  174                 FREE (ng_sppp_units, M_NETGRAPH_SPPP);
  175                 ng_sppp_units_len = 0;
  176                 ng_sppp_units = NULL;
  177         }
  178 }
  179 
  180 /************************************************************************
  181                         INTERFACE STUFF
  182  ************************************************************************/
  183 
  184 /*
  185  * Process an ioctl for the interface
  186  */
  187 static int
  188 ng_sppp_ioctl (struct ifnet *ifp, u_long command, caddr_t data)
  189 {
  190         int error = 0;
  191 
  192         error = sppp_ioctl (ifp, command, data);
  193         if (error)
  194                 return error;
  195 
  196         return error;
  197 }
  198 
  199 /*
  200  * This routine should never be called
  201  */
  202 
  203 static void
  204 ng_sppp_start (struct ifnet *ifp)
  205 {
  206         struct mbuf *m;
  207         int len, error = 0;
  208         priv_p priv = ifp->if_softc;
  209         
  210         /* Check interface flags */
  211         /*
  212          * This has side effects. It is not good idea to stop sending if we
  213          * are not UP. If we are not running we still want to send LCP term
  214          * packets.
  215          */
  216 /*      if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {*/
  217 /*              return;*/
  218 /*      }*/
  219         
  220         if (ifp->if_flags & IFF_OACTIVE)
  221                 return;
  222                 
  223         if (!priv->hook)
  224                 return;
  225                 
  226         ifp->if_flags |= IFF_OACTIVE;
  227 
  228         while ((m = sppp_dequeue (ifp)) != NULL) {
  229                 if (ifp->if_bpf)
  230                         BPF_MTAP (ifp, m);
  231                 len = m->m_pkthdr.len;
  232                 
  233                 NG_SEND_DATA_ONLY (error, priv->hook, m);
  234                 
  235                 if (error) {
  236                         ifp->if_flags &= ~IFF_OACTIVE;
  237                         return;
  238                 }
  239         }
  240         ifp->if_flags &= ~IFF_OACTIVE;
  241 }
  242 
  243 /************************************************************************
  244                         NETGRAPH NODE STUFF
  245  ************************************************************************/
  246 
  247 /*
  248  * Constructor for a node
  249  */
  250 static int
  251 ng_sppp_constructor (node_p node)
  252 {
  253         char ifname[NG_SPPP_IFACE_NAME_MAX + 1];
  254         struct sppp *pp;
  255         priv_p priv;
  256         int error = 0;
  257 
  258         /* Allocate node and interface private structures */
  259         MALLOC (priv, priv_p, sizeof(*priv), M_NETGRAPH_SPPP, M_NOWAIT|M_ZERO);
  260         if (priv == NULL)
  261                 return (ENOMEM);
  262         MALLOC (pp, struct sppp *, sizeof(*pp), M_NETGRAPH_SPPP, M_NOWAIT|M_ZERO);
  263         if (pp == NULL) {
  264                 FREE (priv, M_NETGRAPH_SPPP);
  265                 return (ENOMEM);
  266         }
  267 
  268         /* Link them together */
  269         pp->pp_if.if_softc = priv;
  270         priv->pp = pp;
  271 
  272         /* Get an interface unit number */
  273         if ((error = ng_sppp_get_unit(&priv->unit)) != 0) {
  274                 FREE (pp, M_NETGRAPH_SPPP);
  275                 FREE (priv, M_NETGRAPH_SPPP);
  276                 return (error);
  277         }
  278 
  279 
  280         /* Link together node and private info */
  281         NG_NODE_SET_PRIVATE (node, priv);
  282         priv->node = node;
  283 
  284         /* Initialize interface structure */
  285         if_initname (&pp->pp_if, NG_SPPP_IFACE_NAME, priv->unit);
  286         pp->pp_if.if_start = ng_sppp_start;
  287         pp->pp_if.if_ioctl = ng_sppp_ioctl;
  288         pp->pp_if.if_watchdog = NULL;
  289         pp->pp_if.if_flags = (IFF_POINTOPOINT|IFF_MULTICAST);
  290 
  291         /* Give this node the same name as the interface (if possible) */
  292         bzero (ifname, sizeof(ifname));
  293         snprintf (ifname, sizeof(ifname), "%s%d", NG_SPPP_IFACE_NAME, priv->unit);
  294         if (ng_name_node(node, ifname) != 0)
  295                 log (LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
  296 
  297         /* Attach the interface */
  298         sppp_attach (&pp->pp_if);
  299         if_attach (&pp->pp_if);
  300         bpfattach (&pp->pp_if, DLT_NULL, sizeof(u_int));
  301 
  302         /* Done */
  303         return (0);
  304 }
  305 
  306 /*
  307  * Give our ok for a hook to be added
  308  */
  309 static int
  310 ng_sppp_newhook (node_p node, hook_p hook, const char *name)
  311 {
  312         priv_p priv = NG_NODE_PRIVATE (node);
  313 
  314         if (strcmp (name, NG_SPPP_HOOK_DOWNSTREAM) != 0)
  315                 return (EINVAL);
  316         
  317         if (priv->hook)
  318                 return (EISCONN);
  319                 
  320         priv->hook = hook;
  321         NG_HOOK_SET_PRIVATE (hook, priv);
  322         
  323         return (0);
  324 }
  325 
  326 /*
  327  * Receive a control message
  328  */
  329 static int
  330 ng_sppp_rcvmsg (node_p node, item_p item, hook_p lasthook)
  331 {
  332         const priv_p priv = NG_NODE_PRIVATE (node);
  333         struct ng_mesg *msg = NULL;
  334         struct ng_mesg *resp = NULL;
  335         struct sppp *const pp = priv->pp;
  336         int error = 0;
  337 
  338         NGI_GET_MSG (item, msg);
  339         switch (msg->header.typecookie) {
  340         case NGM_SPPP_COOKIE:
  341                 switch (msg->header.cmd) {
  342                 case NGM_SPPP_GET_IFNAME:
  343                     {
  344                         struct ng_sppp_ifname *arg;
  345 
  346                         NG_MKRESPONSE (resp, msg, sizeof (*arg), M_NOWAIT);
  347                         if (!resp) {
  348                                 error = ENOMEM;
  349                                 break;
  350                         }
  351                         arg = (struct ng_sppp_ifname *)resp->data;
  352                         snprintf (arg->ngif_name, sizeof (arg->ngif_name),
  353                             "%s", pp->pp_if.if_xname);
  354                         break;
  355                     }
  356 
  357                 default:
  358                         error = EINVAL;
  359                         break;
  360                 }
  361                 break;
  362         default:
  363                 error = EINVAL;
  364                 break;
  365         }
  366         NG_RESPOND_MSG (error, node, item, resp);
  367         NG_FREE_MSG (msg);
  368         return (error);
  369 }
  370 
  371 /*
  372  * Recive data from a hook. Pass the packet to the correct input routine.
  373  */
  374 static int
  375 ng_sppp_rcvdata (hook_p hook, item_p item)
  376 {
  377         struct mbuf *m;
  378         const priv_p priv = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
  379         struct sppp *const pp = priv->pp;
  380 
  381         NGI_GET_M (item, m);
  382         NG_FREE_ITEM (item);
  383         /* Sanity checks */
  384         KASSERT (m->m_flags & M_PKTHDR, ("%s: not pkthdr", __func__));
  385         if ((pp->pp_if.if_flags & IFF_UP) == 0) {
  386                 NG_FREE_M (m);
  387                 return (ENETDOWN);
  388         }
  389 
  390         /* Update interface stats */
  391         pp->pp_if.if_ipackets++;
  392 
  393         /* Note receiving interface */
  394         m->m_pkthdr.rcvif = &pp->pp_if;
  395 
  396         /* Berkeley packet filter */
  397         if (pp->pp_if.if_bpf)
  398                 BPF_MTAP (&pp->pp_if, m);
  399 
  400         /* Send packet */
  401         sppp_input (&pp->pp_if, m);
  402         return 0;
  403 }
  404 
  405 /*
  406  * Shutdown and remove the node and its associated interface.
  407  */
  408 static int
  409 ng_sppp_shutdown (node_p node)
  410 {
  411         const priv_p priv = NG_NODE_PRIVATE(node);
  412         /* Detach from the packet filter list of interfaces. */
  413         bpfdetach (&priv->pp->pp_if);
  414         sppp_detach (&priv->pp->pp_if);
  415         if_detach (&priv->pp->pp_if);
  416         FREE (priv->pp, M_NETGRAPH_SPPP);
  417         priv->pp = NULL;
  418         ng_sppp_free_unit (priv->unit);
  419         FREE (priv, M_NETGRAPH_SPPP);
  420         NG_NODE_SET_PRIVATE (node, NULL);
  421         NG_NODE_UNREF (node);
  422         return (0);
  423 }
  424 
  425 /*
  426  * Hook disconnection.
  427  */
  428 static int
  429 ng_sppp_disconnect (hook_p hook)
  430 {
  431         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
  432 
  433         if (priv)
  434                 priv->hook = NULL;
  435 
  436         return (0);
  437 }

Cache object: 457adeb442151614ea7cb6c31a98d272


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