if_vtnet - Use ifsq_watchdog_* functions as the watchdog.
[dragonfly.git] / sys / netgraph7 / ng_gif_demux.c
blobe37b92a09853df3c6006d0635c08052899bedd09
1 /*
2 * ng_gif_demux.c
3 */
5 /*-
6 * Copyright 2001 The Aerospace Corporation. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions, and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of The Aerospace Corporation may not be used to endorse or
18 * promote products derived from this software.
20 * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 * Copyright (c) 1996-1999 Whistle Communications, Inc.
34 * All rights reserved.
36 * Subject to the following obligations and disclaimer of warranty, use and
37 * redistribution of this software, in source or object code forms, with or
38 * without modifications are expressly permitted by Whistle Communications;
39 * provided, however, that:
40 * 1. Any and all reproductions of the source or object code must include the
41 * copyright notice above and the following disclaimer of warranties; and
42 * 2. No rights are granted, in any manner or form, to use Whistle
43 * Communications, Inc. trademarks, including the mark "WHISTLE
44 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
45 * such appears in the above copyright notice or in the software.
47 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
48 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
49 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
50 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
51 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
52 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
53 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
54 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
55 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
56 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
57 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
58 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
59 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
62 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
63 * OF SUCH DAMAGE.
65 * $FreeBSD: src/sys/netgraph/ng_gif_demux.c,v 1.10 2005/01/07 01:45:39 imp Exp $
69 * ng_gif_demux(4) netgraph node type
71 * Packets received on the "gif" hook have their type header removed
72 * and are passed to the appropriate hook protocol hook. Packets
73 * recieved on a protocol hook have a type header added back and are
74 * passed out the gif hook. The currently supported protocol hooks are:
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/malloc.h>
81 #include <sys/ctype.h>
82 #include <sys/mbuf.h>
83 #include <sys/errno.h>
84 #include <sys/socket.h>
86 #include "ng_message.h"
87 #include "netgraph.h"
88 #include "ng_parse.h"
89 #include "ng_gif_demux.h"
91 #ifdef NG_SEPARATE_MALLOC
92 MALLOC_DEFINE(M_NETGRAPH_GIF_DEMUX, "netgraph_gif_demux",
93 "netgraph gif demux node");
94 #else
95 #define M_NETGRAPH_GIF_DEMUX M_NETGRAPH
96 #endif
98 /* This struct describes one address family */
99 struct iffam {
100 sa_family_t family; /* Address family */
101 const char *hookname; /* Name for hook */
103 typedef const struct iffam *iffam_p;
105 /* List of address families supported by our interface */
106 const static struct iffam gFamilies[] = {
107 { AF_INET, NG_GIF_DEMUX_HOOK_INET },
108 { AF_INET6, NG_GIF_DEMUX_HOOK_INET6 },
109 { AF_IPX, NG_GIF_DEMUX_HOOK_IPX },
110 { AF_ATM, NG_GIF_DEMUX_HOOK_ATM },
111 { AF_NATM, NG_GIF_DEMUX_HOOK_NATM },
113 #define NUM_FAMILIES NELEM(gFamilies)
115 /* Per-node private data */
116 struct ng_gif_demux_private {
117 node_p node; /* Our netgraph node */
118 hook_p gif; /* The gif hook */
119 hook_p hooks[NUM_FAMILIES]; /* The protocol hooks */
121 typedef struct ng_gif_demux_private *priv_p;
123 /* Netgraph node methods */
124 static ng_constructor_t ng_gif_demux_constructor;
125 static ng_rcvmsg_t ng_gif_demux_rcvmsg;
126 static ng_shutdown_t ng_gif_demux_shutdown;
127 static ng_newhook_t ng_gif_demux_newhook;
128 static ng_rcvdata_t ng_gif_demux_rcvdata;
129 static ng_disconnect_t ng_gif_demux_disconnect;
131 /* Helper stuff */
132 static iffam_p get_iffam_from_af(sa_family_t family);
133 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
134 static iffam_p get_iffam_from_name(const char *name);
135 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
137 /******************************************************************
138 NETGRAPH PARSE TYPES
139 ******************************************************************/
141 /* List of commands and how to convert arguments to/from ASCII */
142 static const struct ng_cmdlist ng_gif_demux_cmdlist[] = {
143 { 0 }
146 /* Node type descriptor */
147 static struct ng_type ng_gif_demux_typestruct = {
148 .version = NG_ABI_VERSION,
149 .name = NG_GIF_DEMUX_NODE_TYPE,
150 .constructor = ng_gif_demux_constructor,
151 .rcvmsg = ng_gif_demux_rcvmsg,
152 .shutdown = ng_gif_demux_shutdown,
153 .newhook = ng_gif_demux_newhook,
154 .rcvdata = ng_gif_demux_rcvdata,
155 .disconnect = ng_gif_demux_disconnect,
156 .cmdlist = ng_gif_demux_cmdlist,
158 NETGRAPH_INIT(gif_demux, &ng_gif_demux_typestruct);
160 /************************************************************************
161 HELPER STUFF
162 ************************************************************************/
165 * Get the family descriptor from the family ID
167 static __inline iffam_p
168 get_iffam_from_af(sa_family_t family)
170 iffam_p iffam;
171 int k;
173 for (k = 0; k < NUM_FAMILIES; k++) {
174 iffam = &gFamilies[k];
175 if (iffam->family == family)
176 return (iffam);
178 return (NULL);
182 * Get the family descriptor from the hook
184 static __inline iffam_p
185 get_iffam_from_hook(priv_p priv, hook_p hook)
187 int k;
189 for (k = 0; k < NUM_FAMILIES; k++)
190 if (priv->hooks[k] == hook)
191 return (&gFamilies[k]);
192 return (NULL);
196 * Get the hook from the iffam descriptor
199 static __inline hook_p *
200 get_hook_from_iffam(priv_p priv, iffam_p iffam)
202 return (&priv->hooks[iffam - gFamilies]);
206 * Get the iffam descriptor from the name
208 static __inline iffam_p
209 get_iffam_from_name(const char *name)
211 iffam_p iffam;
212 int k;
214 for (k = 0; k < NUM_FAMILIES; k++) {
215 iffam = &gFamilies[k];
216 if (!strcmp(iffam->hookname, name))
217 return (iffam);
219 return (NULL);
222 /******************************************************************
223 NETGRAPH NODE METHODS
224 ******************************************************************/
227 * Node constructor
229 static int
230 ng_gif_demux_constructor(node_p node)
232 priv_p priv;
234 /* Allocate and initialize private info */
235 priv = kmalloc(sizeof(*priv), M_NETGRAPH_GIF_DEMUX,
236 M_WAITOK | M_NULLOK | M_ZERO);
237 if (priv == NULL)
238 return (ENOMEM);
239 priv->node = node;
241 NG_NODE_SET_PRIVATE(node, priv);
243 /* Done */
244 return (0);
248 * Method for attaching a new hook
250 static int
251 ng_gif_demux_newhook(node_p node, hook_p hook, const char *name)
253 const priv_p priv = NG_NODE_PRIVATE(node);
254 iffam_p iffam;
255 hook_p *hookptr;
257 if (strcmp(NG_GIF_DEMUX_HOOK_GIF, name) == 0)
258 hookptr = &priv->gif;
259 else {
260 iffam = get_iffam_from_name(name);
261 if (iffam == NULL)
262 return (EPFNOSUPPORT);
263 hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
265 if (*hookptr != NULL)
266 return (EISCONN);
267 *hookptr = hook;
268 return (0);
272 * Receive a control message
274 static int
275 ng_gif_demux_rcvmsg(node_p node, item_p item, hook_p lasthook)
277 struct ng_mesg *resp = NULL;
278 int error = 0;
279 struct ng_mesg *msg;
281 NGI_GET_MSG(item, msg);
282 switch (msg->header.typecookie) {
283 case NGM_GIF_DEMUX_COOKIE:
284 switch (msg->header.cmd) {
285 /* XXX: Add commands here. */
286 default:
287 error = EINVAL;
288 break;
290 break;
291 default:
292 error = EINVAL;
293 break;
296 /* Done */
297 NG_RESPOND_MSG(error, node, item, resp);
298 NG_FREE_MSG(msg);
299 return (error);
303 * Receive data on a hook
305 static int
306 ng_gif_demux_rcvdata(hook_p hook, item_p item)
308 const node_p node = NG_HOOK_NODE(hook);
309 const priv_p priv = NG_NODE_PRIVATE(node);
310 iffam_p iffam;
311 hook_p outhook;
312 int error = 0;
313 struct mbuf *m;
315 /* Pull the mbuf out of the item for processing. */
316 NGI_GET_M(item, m);
318 if (hook == priv->gif) {
320 * Pull off the address family header and find the
321 * output hook.
323 if (m->m_pkthdr.len < sizeof(sa_family_t)) {
324 NG_FREE_M(m);
325 NG_FREE_ITEM(item);
326 return (EINVAL);
328 if (m->m_len < sizeof(sa_family_t)
329 && (m = m_pullup(m, sizeof(sa_family_t))) == NULL) {
330 NG_FREE_ITEM(item);
331 return (ENOBUFS);
333 iffam = get_iffam_from_af(*mtod(m, sa_family_t *));
334 if (iffam == NULL) {
335 NG_FREE_M(m);
336 NG_FREE_ITEM(item);
337 return (EINVAL);
339 outhook = *get_hook_from_iffam(priv, iffam);
340 m_adj(m, sizeof(sa_family_t));
341 } else {
343 * Add address family header and set the output hook.
345 iffam = get_iffam_from_hook(priv, hook);
346 M_PREPEND(m, sizeof (iffam->family), M_NOWAIT);
347 if (m == NULL) {
348 NG_FREE_M(m);
349 NG_FREE_ITEM(item);
350 return (ENOBUFS);
352 bcopy(&iffam->family, mtod(m, sa_family_t *),
353 sizeof(iffam->family));
354 outhook = priv->gif;
357 /* Stuff the mbuf back in. */
358 NGI_M(item) = m;
360 /* Deliver packet */
361 NG_FWD_ITEM_HOOK(error, item, outhook);
362 return (error);
366 * Shutdown node
368 static int
369 ng_gif_demux_shutdown(node_p node)
371 const priv_p priv = NG_NODE_PRIVATE(node);
373 kfree(priv, M_NETGRAPH_GIF_DEMUX);
374 NG_NODE_SET_PRIVATE(node, NULL);
375 NG_NODE_UNREF(node);
376 return (0);
380 * Hook disconnection.
382 static int
383 ng_gif_demux_disconnect(hook_p hook)
385 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
386 iffam_p iffam;
388 if (hook == priv->gif)
389 priv->gif = NULL;
390 else {
391 iffam = get_iffam_from_hook(priv, hook);
392 if (iffam == NULL)
393 panic(__func__);
394 *get_hook_from_iffam(priv, iffam) = NULL;
397 return (0);