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

Cache object: a2ebcb5160df84ebf12481883acaffc1


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