6 * SPDX-License-Identifier: BSD-3-Clause AND BSD-2-Clause
8 * Copyright 2001 The Aerospace Corporation. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions, and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of The Aerospace Corporation may not be used to endorse or
20 * promote products derived from this software.
22 * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1996-1999 Whistle Communications, Inc.
36 * All rights reserved.
38 * Subject to the following obligations and disclaimer of warranty, use and
39 * redistribution of this software, in source or object code forms, with or
40 * without modifications are expressly permitted by Whistle Communications;
41 * provided, however, that:
42 * 1. Any and all reproductions of the source or object code must include the
43 * copyright notice above and the following disclaimer of warranties; and
44 * 2. No rights are granted, in any manner or form, to use Whistle
45 * Communications, Inc. trademarks, including the mark "WHISTLE
46 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
47 * such appears in the above copyright notice or in the software.
49 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
50 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
51 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
52 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
53 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
54 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
55 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
56 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
57 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
58 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
59 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
60 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
61 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
64 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
71 * ng_gif_demux(4) netgraph node type
73 * Packets received on the "gif" hook have their type header removed
74 * and are passed to the appropriate hook protocol hook. Packets
75 * received on a protocol hook have a type header added back and are
76 * passed out the gif hook. The currently supported protocol hooks are:
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/malloc.h>
83 #include <sys/ctype.h>
85 #include <sys/errno.h>
86 #include <sys/socket.h>
88 #include <netgraph/ng_message.h>
89 #include <netgraph/netgraph.h>
90 #include <netgraph/ng_parse.h>
91 #include <netgraph/ng_gif_demux.h>
93 #ifdef NG_SEPARATE_MALLOC
94 static MALLOC_DEFINE(M_NETGRAPH_GIF_DEMUX, "netgraph_gif_demux",
95 "netgraph gif demux node");
97 #define M_NETGRAPH_GIF_DEMUX M_NETGRAPH
100 /* This struct describes one address family */
102 sa_family_t family; /* Address family */
103 const char *hookname; /* Name for hook */
105 typedef const struct iffam *iffam_p;
107 /* List of address families supported by our interface */
108 const static struct iffam gFamilies[] = {
109 { AF_INET, NG_GIF_DEMUX_HOOK_INET },
110 { AF_INET6, NG_GIF_DEMUX_HOOK_INET6 },
111 { AF_APPLETALK, NG_GIF_DEMUX_HOOK_ATALK },
112 { AF_IPX, NG_GIF_DEMUX_HOOK_IPX },
113 { AF_ATM, NG_GIF_DEMUX_HOOK_ATM },
114 { AF_NATM, NG_GIF_DEMUX_HOOK_NATM },
116 #define NUM_FAMILIES nitems(gFamilies)
118 /* Per-node private data */
119 struct ng_gif_demux_private {
120 node_p node; /* Our netgraph node */
121 hook_p gif; /* The gif hook */
122 hook_p hooks[NUM_FAMILIES]; /* The protocol hooks */
124 typedef struct ng_gif_demux_private *priv_p;
126 /* Netgraph node methods */
127 static ng_constructor_t ng_gif_demux_constructor;
128 static ng_rcvmsg_t ng_gif_demux_rcvmsg;
129 static ng_shutdown_t ng_gif_demux_shutdown;
130 static ng_newhook_t ng_gif_demux_newhook;
131 static ng_rcvdata_t ng_gif_demux_rcvdata;
132 static ng_disconnect_t ng_gif_demux_disconnect;
135 static iffam_p get_iffam_from_af(sa_family_t family);
136 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
137 static iffam_p get_iffam_from_name(const char *name);
138 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
140 /******************************************************************
142 ******************************************************************/
144 /* List of commands and how to convert arguments to/from ASCII */
145 static const struct ng_cmdlist ng_gif_demux_cmdlist[] = {
149 /* Node type descriptor */
150 static struct ng_type ng_gif_demux_typestruct = {
151 .version = NG_ABI_VERSION,
152 .name = NG_GIF_DEMUX_NODE_TYPE,
153 .constructor = ng_gif_demux_constructor,
154 .rcvmsg = ng_gif_demux_rcvmsg,
155 .shutdown = ng_gif_demux_shutdown,
156 .newhook = ng_gif_demux_newhook,
157 .rcvdata = ng_gif_demux_rcvdata,
158 .disconnect = ng_gif_demux_disconnect,
159 .cmdlist = ng_gif_demux_cmdlist,
161 NETGRAPH_INIT(gif_demux, &ng_gif_demux_typestruct);
163 /************************************************************************
165 ************************************************************************/
168 * Get the family descriptor from the family ID
170 static __inline iffam_p
171 get_iffam_from_af(sa_family_t family)
176 for (k = 0; k < NUM_FAMILIES; k++) {
177 iffam = &gFamilies[k];
178 if (iffam->family == family)
185 * Get the family descriptor from the hook
187 static __inline iffam_p
188 get_iffam_from_hook(priv_p priv, hook_p hook)
192 for (k = 0; k < NUM_FAMILIES; k++)
193 if (priv->hooks[k] == hook)
194 return (&gFamilies[k]);
199 * Get the hook from the iffam descriptor
202 static __inline hook_p *
203 get_hook_from_iffam(priv_p priv, iffam_p iffam)
205 return (&priv->hooks[iffam - gFamilies]);
209 * Get the iffam descriptor from the name
211 static __inline iffam_p
212 get_iffam_from_name(const char *name)
217 for (k = 0; k < NUM_FAMILIES; k++) {
218 iffam = &gFamilies[k];
219 if (!strcmp(iffam->hookname, name))
225 /******************************************************************
226 NETGRAPH NODE METHODS
227 ******************************************************************/
233 ng_gif_demux_constructor(node_p node)
237 /* Allocate and initialize private info */
238 priv = malloc(sizeof(*priv), M_NETGRAPH_GIF_DEMUX, M_WAITOK | M_ZERO);
241 NG_NODE_SET_PRIVATE(node, priv);
248 * Method for attaching a new hook
251 ng_gif_demux_newhook(node_p node, hook_p hook, const char *name)
253 const priv_p priv = NG_NODE_PRIVATE(node);
257 if (strcmp(NG_GIF_DEMUX_HOOK_GIF, name) == 0)
258 hookptr = &priv->gif;
260 iffam = get_iffam_from_name(name);
262 return (EPFNOSUPPORT);
263 hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
265 if (*hookptr != NULL)
272 * Receive a control message
275 ng_gif_demux_rcvmsg(node_p node, item_p item, hook_p lasthook)
277 struct ng_mesg *resp = NULL;
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. */
297 NG_RESPOND_MSG(error, node, item, resp);
303 * Receive data on a hook
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);
315 /* Pull the mbuf out of the item for processing. */
318 if (hook == priv->gif) {
320 * Pull off the address family header and find the
323 if (m->m_pkthdr.len < sizeof(sa_family_t)) {
328 if (m->m_len < sizeof(sa_family_t)
329 && (m = m_pullup(m, sizeof(sa_family_t))) == NULL) {
333 iffam = get_iffam_from_af(*mtod(m, sa_family_t *));
339 outhook = *get_hook_from_iffam(priv, iffam);
340 m_adj(m, sizeof(sa_family_t));
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);
352 bcopy(&iffam->family, mtod(m, sa_family_t *),
353 sizeof(iffam->family));
357 /* Stuff the mbuf back in. */
361 NG_FWD_ITEM_HOOK(error, item, outhook);
369 ng_gif_demux_shutdown(node_p node)
371 const priv_p priv = NG_NODE_PRIVATE(node);
373 free(priv, M_NETGRAPH_GIF_DEMUX);
374 NG_NODE_SET_PRIVATE(node, NULL);
380 * Hook disconnection.
383 ng_gif_demux_disconnect(hook_p hook)
385 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
388 if (hook == priv->gif)
391 iffam = get_iffam_from_hook(priv, hook);
393 panic("%s", __func__);
394 *get_hook_from_iffam(priv, iffam) = NULL;