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