xref: /freebsd/sys/netgraph/ng_gif_demux.c (revision 1ede983cc905643549d8cae56a9d0e28fc68375f)
1 /*
2  * ng_gif_demux.c
3  */
4 
5 /*-
6  * Copyright 2001 The Aerospace Corporation.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
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.
19  *
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.
31  *
32  *
33  * Copyright (c) 1996-1999 Whistle Communications, Inc.
34  * All rights reserved.
35  *
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.
46  *
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.
64  *
65  * $FreeBSD$
66  */
67 
68 /*
69  * ng_gif_demux(4) netgraph node type
70  *
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:
75  */
76 
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>
85 
86 #include <netgraph/ng_message.h>
87 #include <netgraph/netgraph.h>
88 #include <netgraph/ng_parse.h>
89 #include <netgraph/ng_gif_demux.h>
90 
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
97 
98 /* This struct describes one address family */
99 struct iffam {
100 	sa_family_t	family;		/* Address family */
101 	const char	*hookname;	/* Name for hook */
102 };
103 typedef const struct iffam *iffam_p;
104 
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_APPLETALK,	NG_GIF_DEMUX_HOOK_ATALK	},
110 	{ AF_IPX,	NG_GIF_DEMUX_HOOK_IPX	},
111 	{ AF_ATM,	NG_GIF_DEMUX_HOOK_ATM	},
112 	{ AF_NATM,	NG_GIF_DEMUX_HOOK_NATM	},
113 };
114 #define NUM_FAMILIES		(sizeof(gFamilies) / sizeof(*gFamilies))
115 
116 /* Per-node private data */
117 struct ng_gif_demux_private {
118 	node_p	node;			/* Our netgraph node */
119 	hook_p	gif;			/* The gif hook */
120 	hook_p	hooks[NUM_FAMILIES];	/* The protocol hooks */
121 };
122 typedef struct ng_gif_demux_private *priv_p;
123 
124 /* Netgraph node methods */
125 static ng_constructor_t	ng_gif_demux_constructor;
126 static ng_rcvmsg_t	ng_gif_demux_rcvmsg;
127 static ng_shutdown_t	ng_gif_demux_shutdown;
128 static ng_newhook_t	ng_gif_demux_newhook;
129 static ng_rcvdata_t	ng_gif_demux_rcvdata;
130 static ng_disconnect_t	ng_gif_demux_disconnect;
131 
132 /* Helper stuff */
133 static iffam_p	get_iffam_from_af(sa_family_t family);
134 static iffam_p	get_iffam_from_hook(priv_p priv, hook_p hook);
135 static iffam_p	get_iffam_from_name(const char *name);
136 static hook_p	*get_hook_from_iffam(priv_p priv, iffam_p iffam);
137 
138 /******************************************************************
139 		    NETGRAPH PARSE TYPES
140 ******************************************************************/
141 
142 /* List of commands and how to convert arguments to/from ASCII */
143 static const struct ng_cmdlist ng_gif_demux_cmdlist[] = {
144 	{ 0 }
145 };
146 
147 /* Node type descriptor */
148 static struct ng_type ng_gif_demux_typestruct = {
149 	.version =	NG_ABI_VERSION,
150 	.name =		NG_GIF_DEMUX_NODE_TYPE,
151 	.constructor =	ng_gif_demux_constructor,
152 	.rcvmsg =	ng_gif_demux_rcvmsg,
153 	.shutdown =	ng_gif_demux_shutdown,
154 	.newhook =	ng_gif_demux_newhook,
155 	.rcvdata =	ng_gif_demux_rcvdata,
156 	.disconnect =	ng_gif_demux_disconnect,
157 	.cmdlist =	ng_gif_demux_cmdlist,
158 };
159 NETGRAPH_INIT(gif_demux, &ng_gif_demux_typestruct);
160 
161 /************************************************************************
162 		    HELPER STUFF
163  ************************************************************************/
164 
165 /*
166  * Get the family descriptor from the family ID
167  */
168 static __inline iffam_p
169 get_iffam_from_af(sa_family_t family)
170 {
171 	iffam_p iffam;
172 	int k;
173 
174 	for (k = 0; k < NUM_FAMILIES; k++) {
175 		iffam = &gFamilies[k];
176 		if (iffam->family == family)
177 			return (iffam);
178 	}
179 	return (NULL);
180 }
181 
182 /*
183  * Get the family descriptor from the hook
184  */
185 static __inline iffam_p
186 get_iffam_from_hook(priv_p priv, hook_p hook)
187 {
188 	int k;
189 
190 	for (k = 0; k < NUM_FAMILIES; k++)
191 		if (priv->hooks[k] == hook)
192 			return (&gFamilies[k]);
193 	return (NULL);
194 }
195 
196 /*
197  * Get the hook from the iffam descriptor
198  */
199 
200 static __inline hook_p *
201 get_hook_from_iffam(priv_p priv, iffam_p iffam)
202 {
203 	return (&priv->hooks[iffam - gFamilies]);
204 }
205 
206 /*
207  * Get the iffam descriptor from the name
208  */
209 static __inline iffam_p
210 get_iffam_from_name(const char *name)
211 {
212 	iffam_p iffam;
213 	int k;
214 
215 	for (k = 0; k < NUM_FAMILIES; k++) {
216 		iffam = &gFamilies[k];
217 		if (!strcmp(iffam->hookname, name))
218 			return (iffam);
219 	}
220 	return (NULL);
221 }
222 
223 /******************************************************************
224 		    NETGRAPH NODE METHODS
225 ******************************************************************/
226 
227 /*
228  * Node constructor
229  */
230 static int
231 ng_gif_demux_constructor(node_p node)
232 {
233 	priv_p priv;
234 
235 	/* Allocate and initialize private info */
236 	priv = malloc(sizeof(*priv), M_NETGRAPH_GIF_DEMUX,
237 	    M_NOWAIT | M_ZERO);
238 	if (priv == NULL)
239 		return (ENOMEM);
240 	priv->node = node;
241 
242 	NG_NODE_SET_PRIVATE(node, priv);
243 
244 	/* Done */
245 	return (0);
246 }
247 
248 /*
249  * Method for attaching a new hook
250  */
251 static	int
252 ng_gif_demux_newhook(node_p node, hook_p hook, const char *name)
253 {
254 	const priv_p priv = NG_NODE_PRIVATE(node);
255 	iffam_p iffam;
256 	hook_p *hookptr;
257 
258 	if (strcmp(NG_GIF_DEMUX_HOOK_GIF, name) == 0)
259 		hookptr = &priv->gif;
260 	else {
261 		iffam = get_iffam_from_name(name);
262 		if (iffam == NULL)
263 			return (EPFNOSUPPORT);
264 		hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
265 	}
266 	if (*hookptr != NULL)
267 		return (EISCONN);
268 	*hookptr = hook;
269 	return (0);
270 }
271 
272 /*
273  * Receive a control message
274  */
275 static int
276 ng_gif_demux_rcvmsg(node_p node, item_p item, hook_p lasthook)
277 {
278 	struct ng_mesg *resp = NULL;
279 	int error = 0;
280 	struct ng_mesg *msg;
281 
282 	NGI_GET_MSG(item, msg);
283 	switch (msg->header.typecookie) {
284 	case NGM_GIF_DEMUX_COOKIE:
285 		switch (msg->header.cmd) {
286 		/* XXX: Add commands here. */
287 		default:
288 			error = EINVAL;
289 			break;
290 		}
291 		break;
292 	default:
293 		error = EINVAL;
294 		break;
295 	}
296 
297 	/* Done */
298 	NG_RESPOND_MSG(error, node, item, resp);
299 	NG_FREE_MSG(msg);
300 	return (error);
301 }
302 
303 /*
304  * Receive data on a hook
305  */
306 static int
307 ng_gif_demux_rcvdata(hook_p hook, item_p item)
308 {
309 	const node_p node = NG_HOOK_NODE(hook);
310 	const priv_p priv = NG_NODE_PRIVATE(node);
311 	iffam_p iffam;
312 	hook_p outhook;
313 	int error = 0;
314 	struct mbuf *m;
315 
316 	/* Pull the mbuf out of the item for processing. */
317 	NGI_GET_M(item, m);
318 
319 	if (hook == priv->gif) {
320 		/*
321 		 * Pull off the address family header and find the
322 		 * output hook.
323 		 */
324 		if (m->m_pkthdr.len < sizeof(sa_family_t)) {
325 			NG_FREE_M(m);
326 			NG_FREE_ITEM(item);
327 			return (EINVAL);
328 		}
329 		if (m->m_len < sizeof(sa_family_t)
330 		    && (m = m_pullup(m, sizeof(sa_family_t))) == NULL) {
331 			NG_FREE_ITEM(item);
332 			return (ENOBUFS);
333 		}
334 		iffam = get_iffam_from_af(*mtod(m, sa_family_t *));
335 		if (iffam == NULL) {
336 			NG_FREE_M(m);
337 			NG_FREE_ITEM(item);
338 			return (EINVAL);
339 		}
340 		outhook = *get_hook_from_iffam(priv, iffam);
341 		m_adj(m, sizeof(sa_family_t));
342 	} else {
343 		/*
344 		 * Add address family header and set the output hook.
345 		 */
346 		iffam = get_iffam_from_hook(priv, hook);
347 		M_PREPEND(m, sizeof (iffam->family), M_DONTWAIT);
348 		if (m == NULL) {
349 			NG_FREE_M(m);
350 			NG_FREE_ITEM(item);
351 			return (ENOBUFS);
352 		}
353 		bcopy(&iffam->family, mtod(m, sa_family_t *),
354 		    sizeof(iffam->family));
355 		outhook = priv->gif;
356 	}
357 
358 	/* Stuff the mbuf back in. */
359 	NGI_M(item) = m;
360 
361 	/* Deliver packet */
362 	NG_FWD_ITEM_HOOK(error, item, outhook);
363 	return (error);
364 }
365 
366 /*
367  * Shutdown node
368  */
369 static int
370 ng_gif_demux_shutdown(node_p node)
371 {
372 	const priv_p priv = NG_NODE_PRIVATE(node);
373 
374 	free(priv, M_NETGRAPH_GIF_DEMUX);
375 	NG_NODE_SET_PRIVATE(node, NULL);
376 	NG_NODE_UNREF(node);
377 	return (0);
378 }
379 
380 /*
381  * Hook disconnection.
382  */
383 static int
384 ng_gif_demux_disconnect(hook_p hook)
385 {
386 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
387 	iffam_p iffam;
388 
389 	if (hook == priv->gif)
390 		priv->gif = NULL;
391 	else {
392 		iffam = get_iffam_from_hook(priv, hook);
393 		if (iffam == NULL)
394 			panic(__func__);
395 		*get_hook_from_iffam(priv, iffam) = NULL;
396 	}
397 
398 	return (0);
399 }
400