4 * Copyright 2001 The Aerospace Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions, and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of The Aerospace Corporation may not be used to endorse or
16 * promote products derived from this software.
18 * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Copyright (c) 1996-1999 Whistle Communications, Inc.
32 * All rights reserved.
34 * Subject to the following obligations and disclaimer of warranty, use and
35 * redistribution of this software, in source or object code forms, with or
36 * without modifications are expressly permitted by Whistle Communications;
37 * provided, however, that:
38 * 1. Any and all reproductions of the source or object code must include the
39 * copyright notice above and the following disclaimer of warranties; and
40 * 2. No rights are granted, in any manner or form, to use Whistle
41 * Communications, Inc. trademarks, including the mark "WHISTLE
42 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
43 * such appears in the above copyright notice or in the software.
45 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
46 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
47 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
48 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
49 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
50 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
51 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
52 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
53 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
54 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
55 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
56 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
57 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
67 * ng_gif_demux(4) netgraph node type
69 * Packets received on the "gif" hook have their type header removed
70 * and are passed to the appropriate hook protocol hook. Packets
71 * recieved on a protocol hook have a type header added back and are
72 * passed out the gif hook. The currently supported protocol hooks are:
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/malloc.h>
79 #include <sys/ctype.h>
81 #include <sys/errno.h>
82 #include <sys/socket.h>
84 #include <netgraph/ng_message.h>
85 #include <netgraph/netgraph.h>
86 #include <netgraph/ng_parse.h>
87 #include <netgraph/ng_gif_demux.h>
89 #ifdef NG_SEPARATE_MALLOC
90 MALLOC_DEFINE(M_NETGRAPH_GIF_DEMUX, "netgraph_gif_demux",
91 "netgraph gif demux node");
93 #define M_NETGRAPH_GIF_DEMUX M_NETGRAPH
96 /* This struct describes one address family */
98 sa_family_t family; /* Address family */
99 const char *hookname; /* Name for hook */
101 typedef const struct iffam *iffam_p;
103 /* List of address families supported by our interface */
104 const static struct iffam gFamilies[] = {
105 { AF_INET, NG_GIF_DEMUX_HOOK_INET },
106 { AF_INET6, NG_GIF_DEMUX_HOOK_INET6 },
107 { AF_APPLETALK, NG_GIF_DEMUX_HOOK_ATALK },
108 { AF_IPX, NG_GIF_DEMUX_HOOK_IPX },
109 { AF_ATM, NG_GIF_DEMUX_HOOK_ATM },
110 { AF_NATM, NG_GIF_DEMUX_HOOK_NATM },
112 #define NUM_FAMILIES (sizeof(gFamilies) / sizeof(*gFamilies))
114 /* Per-node private data */
115 struct ng_gif_demux_private {
116 node_p node; /* Our netgraph node */
117 hook_p gif; /* The gif hook */
118 hook_p hooks[NUM_FAMILIES]; /* The protocol hooks */
120 typedef struct ng_gif_demux_private *priv_p;
122 /* Netgraph node methods */
123 static ng_constructor_t ng_gif_demux_constructor;
124 static ng_rcvmsg_t ng_gif_demux_rcvmsg;
125 static ng_shutdown_t ng_gif_demux_shutdown;
126 static ng_newhook_t ng_gif_demux_newhook;
127 static ng_rcvdata_t ng_gif_demux_rcvdata;
128 static ng_disconnect_t ng_gif_demux_disconnect;
131 static iffam_p get_iffam_from_af(sa_family_t family);
132 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
133 static iffam_p get_iffam_from_name(const char *name);
134 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
136 /******************************************************************
138 ******************************************************************/
140 /* List of commands and how to convert arguments to/from ASCII */
141 static const struct ng_cmdlist ng_gif_demux_cmdlist[] = {
145 /* Node type descriptor */
146 static struct ng_type ng_gif_demux_typestruct = {
147 .version = NG_ABI_VERSION,
148 .name = NG_GIF_DEMUX_NODE_TYPE,
149 .constructor = ng_gif_demux_constructor,
150 .rcvmsg = ng_gif_demux_rcvmsg,
151 .shutdown = ng_gif_demux_shutdown,
152 .newhook = ng_gif_demux_newhook,
153 .rcvdata = ng_gif_demux_rcvdata,
154 .disconnect = ng_gif_demux_disconnect,
155 .cmdlist = ng_gif_demux_cmdlist,
157 NETGRAPH_INIT(gif_demux, &ng_gif_demux_typestruct);
159 /************************************************************************
161 ************************************************************************/
164 * Get the family descriptor from the family ID
166 static __inline iffam_p
167 get_iffam_from_af(sa_family_t family)
172 for (k = 0; k < NUM_FAMILIES; k++) {
173 iffam = &gFamilies[k];
174 if (iffam->family == family)
181 * Get the family descriptor from the hook
183 static __inline iffam_p
184 get_iffam_from_hook(priv_p priv, hook_p hook)
188 for (k = 0; k < NUM_FAMILIES; k++)
189 if (priv->hooks[k] == hook)
190 return (&gFamilies[k]);
195 * Get the hook from the iffam descriptor
198 static __inline hook_p *
199 get_hook_from_iffam(priv_p priv, iffam_p iffam)
201 return (&priv->hooks[iffam - gFamilies]);
205 * Get the iffam descriptor from the name
207 static __inline iffam_p
208 get_iffam_from_name(const char *name)
213 for (k = 0; k < NUM_FAMILIES; k++) {
214 iffam = &gFamilies[k];
215 if (!strcmp(iffam->hookname, name))
221 /******************************************************************
222 NETGRAPH NODE METHODS
223 ******************************************************************/
229 ng_gif_demux_constructor(node_p node)
233 /* Allocate and initialize private info */
234 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_GIF_DEMUX,
240 NG_NODE_SET_PRIVATE(node, priv);
247 * Method for attaching a new hook
250 ng_gif_demux_newhook(node_p node, hook_p hook, const char *name)
252 const priv_p priv = NG_NODE_PRIVATE(node);
256 if (strcmp(NG_GIF_DEMUX_HOOK_GIF, name) == 0)
257 hookptr = &priv->gif;
259 iffam = get_iffam_from_name(name);
261 return (EPFNOSUPPORT);
262 hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
264 if (*hookptr != NULL)
271 * Receive a control message
274 ng_gif_demux_rcvmsg(node_p node, item_p item, hook_p lasthook)
276 struct ng_mesg *resp = NULL;
280 NGI_GET_MSG(item, msg);
281 switch (msg->header.typecookie) {
282 case NGM_GIF_DEMUX_COOKIE:
283 switch (msg->header.cmd) {
284 /* XXX: Add commands here. */
296 NG_RESPOND_MSG(error, node, item, resp);
302 * Receive data on a hook
305 ng_gif_demux_rcvdata(hook_p hook, item_p item)
307 const node_p node = NG_HOOK_NODE(hook);
308 const priv_p priv = NG_NODE_PRIVATE(node);
314 /* Pull the mbuf out of the item for processing. */
317 if (hook == priv->gif) {
319 * Pull off the address family header and find the
322 if (m->m_pkthdr.len < sizeof(sa_family_t)) {
327 if (m->m_len < sizeof(sa_family_t)
328 && (m = m_pullup(m, sizeof(sa_family_t))) == NULL) {
332 iffam = get_iffam_from_af(*mtod(m, sa_family_t *));
338 outhook = *get_hook_from_iffam(priv, iffam);
339 m_adj(m, sizeof(sa_family_t));
342 * Add address family header and set the output hook.
344 iffam = get_iffam_from_hook(priv, hook);
345 M_PREPEND(m, sizeof (iffam->family), M_DONTWAIT);
351 bcopy(&iffam->family, mtod(m, sa_family_t *),
352 sizeof(iffam->family));
356 /* Stuff the mbuf back in. */
360 NG_FWD_ITEM_HOOK(error, item, outhook);
368 ng_gif_demux_shutdown(node_p node)
370 const priv_p priv = NG_NODE_PRIVATE(node);
372 FREE(priv, M_NETGRAPH_GIF_DEMUX);
373 NG_NODE_SET_PRIVATE(node, NULL);
379 * Hook disconnection.
382 ng_gif_demux_disconnect(hook_p hook)
384 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
387 if (hook == priv->gif)
390 iffam = get_iffam_from_hook(priv, hook);
393 *get_hook_from_iffam(priv, iffam) = NULL;