2 * ng_sppp.c Netgraph to Sppp module.
6 * Copyright (C) 2002-2004 Cronyx Engineering.
7 * Copyright (C) 2002-2004 Roman Kurakin <rik@cronyx.ru>
9 * This software is distributed with NO WARRANTIES, not even the implied
10 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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.
17 * Cronyx Id: ng_sppp.c,v 1.1.2.10 2004/03/01 15:17:21 rik Exp $
19 #include <sys/cdefs.h>
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>
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>
33 #include <net/if_var.h>
34 #include <net/if_types.h>
36 #include <net/if_sppp.h>
38 #include <netinet/in.h>
40 #include <netgraph/ng_message.h>
41 #include <netgraph/netgraph.h>
42 #include <netgraph/ng_parse.h>
43 #include <netgraph/ng_sppp.h>
45 #ifdef NG_SEPARATE_MALLOC
46 static MALLOC_DEFINE(M_NETGRAPH_SPPP, "netgraph_sppp", "netgraph sppp node");
48 #define M_NETGRAPH_SPPP M_NETGRAPH
51 /* Node private data */
52 struct ng_sppp_private {
53 struct ifnet *ifp; /* Our interface */
54 int unit; /* Interface unit number */
55 node_p node; /* Our netgraph node */
56 hook_p hook; /* Hook */
58 typedef struct ng_sppp_private *priv_p;
60 /* Interface methods */
61 static void ng_sppp_start (struct ifnet *ifp);
62 static int ng_sppp_ioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
64 /* Netgraph methods */
65 static ng_constructor_t ng_sppp_constructor;
66 static ng_rcvmsg_t ng_sppp_rcvmsg;
67 static ng_shutdown_t ng_sppp_shutdown;
68 static ng_newhook_t ng_sppp_newhook;
69 static ng_rcvdata_t ng_sppp_rcvdata;
70 static ng_disconnect_t ng_sppp_disconnect;
72 /* List of commands and how to convert arguments to/from ASCII */
73 static const struct ng_cmdlist ng_sppp_cmds[] = {
84 /* Node type descriptor */
85 static struct ng_type typestruct = {
86 .version = NG_ABI_VERSION,
87 .name = NG_SPPP_NODE_TYPE,
88 .constructor = ng_sppp_constructor,
89 .rcvmsg = ng_sppp_rcvmsg,
90 .shutdown = ng_sppp_shutdown,
91 .newhook = ng_sppp_newhook,
92 .rcvdata = ng_sppp_rcvdata,
93 .disconnect = ng_sppp_disconnect,
94 .cmdlist = ng_sppp_cmds,
96 NETGRAPH_INIT(sppp, &typestruct);
98 MODULE_DEPEND (ng_sppp, sppp, 1, 1, 1);
100 /* We keep a bitmap indicating which unit numbers are free.
101 Zero means the unit number is free, one means it's taken. */
102 static unsigned char *ng_sppp_units = NULL;
103 static unsigned char ng_sppp_units_len = 0;
104 static unsigned char ng_units_in_use = 0;
107 * Find the first free unit number for a new interface.
108 * Increase the size of the unit bitmap as necessary.
111 ng_sppp_get_unit (int *unit)
116 for (index = 0; index < ng_sppp_units_len
117 && ng_sppp_units[index] == 0xFF; index++);
118 if (index == ng_sppp_units_len) { /* extend array */
119 unsigned char *newarray;
122 newlen = (2 * ng_sppp_units_len) + sizeof (*ng_sppp_units);
123 newarray = malloc (newlen * sizeof (*ng_sppp_units),
124 M_NETGRAPH_SPPP, M_WAITOK);
125 bcopy (ng_sppp_units, newarray,
126 ng_sppp_units_len * sizeof (*ng_sppp_units));
127 bzero (newarray + ng_sppp_units_len,
128 newlen - ng_sppp_units_len);
129 if (ng_sppp_units != NULL)
130 free (ng_sppp_units, M_NETGRAPH_SPPP);
131 ng_sppp_units = newarray;
132 ng_sppp_units_len = newlen;
134 mask = ng_sppp_units[index];
135 for (bit = 0; (mask & 1) != 0; bit++)
137 KASSERT ((bit >= 0 && bit < NBBY),
138 ("%s: word=%d bit=%d", __func__, ng_sppp_units[index], bit));
139 ng_sppp_units[index] |= (1 << bit);
140 *unit = (index * NBBY) + bit;
145 * Free a no longer needed unit number.
148 ng_sppp_free_unit (int unit)
154 KASSERT (index < ng_sppp_units_len,
155 ("%s: unit=%d len=%d", __func__, unit, ng_sppp_units_len));
156 KASSERT ((ng_sppp_units[index] & (1 << bit)) != 0,
157 ("%s: unit=%d is free", __func__, unit));
158 ng_sppp_units[index] &= ~(1 << bit);
161 if (ng_units_in_use == 0) {
162 free (ng_sppp_units, M_NETGRAPH_SPPP);
163 ng_sppp_units_len = 0;
164 ng_sppp_units = NULL;
168 /************************************************************************
170 ************************************************************************/
173 * Process an ioctl for the interface
176 ng_sppp_ioctl (struct ifnet *ifp, u_long command, caddr_t data)
180 error = sppp_ioctl (ifp, command, data);
188 * This routine should never be called
192 ng_sppp_start (struct ifnet *ifp)
196 priv_p priv = ifp->if_softc;
198 /* Check interface flags */
200 * This has side effects. It is not good idea to stop sending if we
201 * are not UP. If we are not running we still want to send LCP term
204 /* if (!((ifp->if_flags & IFF_UP) && */
205 /* (ifp->if_drv_flags & IFF_DRV_RUNNING))) { */
209 if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
215 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
217 while ((m = sppp_dequeue (ifp)) != NULL) {
219 len = m->m_pkthdr.len;
221 NG_SEND_DATA_ONLY (error, priv->hook, m);
224 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
228 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
231 /************************************************************************
233 ************************************************************************/
236 * Constructor for a node
239 ng_sppp_constructor (node_p node)
245 /* Allocate node and interface private structures */
246 priv = malloc(sizeof(*priv), M_NETGRAPH_SPPP, M_WAITOK | M_ZERO);
248 ifp = if_alloc(IFT_PPP);
250 free (priv, M_NETGRAPH_SPPP);
255 /* Link them together */
256 ifp->if_softc = priv;
259 /* Get an interface unit number */
260 ng_sppp_get_unit(&priv->unit);
262 /* Link together node and private info */
263 NG_NODE_SET_PRIVATE (node, priv);
266 /* Initialize interface structure */
267 if_initname (SP2IFP(pp), NG_SPPP_IFACE_NAME, priv->unit);
268 ifp->if_start = ng_sppp_start;
269 ifp->if_ioctl = ng_sppp_ioctl;
270 ifp->if_flags = (IFF_POINTOPOINT|IFF_MULTICAST);
272 /* Give this node the same name as the interface (if possible) */
273 if (ng_name_node(node, SP2IFP(pp)->if_xname) != 0)
274 log (LOG_WARNING, "%s: can't acquire netgraph name\n",
275 SP2IFP(pp)->if_xname);
277 /* Attach the interface */
280 bpfattach (ifp, DLT_NULL, sizeof(u_int32_t));
287 * Give our ok for a hook to be added
290 ng_sppp_newhook (node_p node, hook_p hook, const char *name)
292 priv_p priv = NG_NODE_PRIVATE (node);
294 if (strcmp (name, NG_SPPP_HOOK_DOWNSTREAM) != 0)
301 NG_HOOK_SET_PRIVATE (hook, priv);
307 * Receive a control message
310 ng_sppp_rcvmsg (node_p node, item_p item, hook_p lasthook)
312 const priv_p priv = NG_NODE_PRIVATE (node);
313 struct ng_mesg *msg = NULL;
314 struct ng_mesg *resp = NULL;
315 struct sppp *const pp = IFP2SP(priv->ifp);
318 NGI_GET_MSG (item, msg);
319 switch (msg->header.typecookie) {
320 case NGM_SPPP_COOKIE:
321 switch (msg->header.cmd) {
322 case NGM_SPPP_GET_IFNAME:
323 NG_MKRESPONSE (resp, msg, IFNAMSIZ, M_NOWAIT);
328 strlcpy(resp->data, SP2IFP(pp)->if_xname, IFNAMSIZ);
340 NG_RESPOND_MSG (error, node, item, resp);
346 * Recive data from a hook. Pass the packet to the correct input routine.
349 ng_sppp_rcvdata (hook_p hook, item_p item)
352 const priv_p priv = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
353 struct sppp *const pp = IFP2SP(priv->ifp);
358 KASSERT (m->m_flags & M_PKTHDR, ("%s: not pkthdr", __func__));
359 if ((SP2IFP(pp)->if_flags & IFF_UP) == 0) {
364 /* Update interface stats */
365 if_inc_counter(SP2IFP(pp), IFCOUNTER_IPACKETS, 1);
367 /* Note receiving interface */
368 m->m_pkthdr.rcvif = SP2IFP(pp);
370 /* Berkeley packet filter */
371 BPF_MTAP (SP2IFP(pp), m);
374 sppp_input (SP2IFP(pp), m);
379 * Shutdown and remove the node and its associated interface.
382 ng_sppp_shutdown (node_p node)
384 const priv_p priv = NG_NODE_PRIVATE(node);
385 /* Detach from the packet filter list of interfaces. */
386 bpfdetach (priv->ifp);
387 sppp_detach (priv->ifp);
388 if_detach (priv->ifp);
390 ng_sppp_free_unit (priv->unit);
391 free (priv, M_NETGRAPH_SPPP);
392 NG_NODE_SET_PRIVATE (node, NULL);
393 NG_NODE_UNREF (node);
398 * Hook disconnection.
401 ng_sppp_disconnect (hook_p hook)
403 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));