FreeBSD kernel netgraph code
ng_gif_demux.c
Go to the documentation of this file.
1/*
2 * ng_gif_demux.c
3 */
4
5/*-
6 * SPDX-License-Identifier: BSD-3-Clause AND BSD-2-Clause
7 *
8 * Copyright 2001 The Aerospace Corporation. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
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.
21 *
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
32 * SUCH DAMAGE.
33 *
34 *
35 * Copyright (c) 1996-1999 Whistle Communications, Inc.
36 * All rights reserved.
37 *
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.
48 *
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
65 * OF SUCH DAMAGE.
66 *
67 * $FreeBSD$
68 */
69
70/*
71 * ng_gif_demux(4) netgraph node type
72 *
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:
77 */
78
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>
84#include <sys/mbuf.h>
85#include <sys/errno.h>
86#include <sys/socket.h>
87
88#include <netgraph/ng_message.h>
89#include <netgraph/netgraph.h>
90#include <netgraph/ng_parse.h>
92
93#ifdef NG_SEPARATE_MALLOC
94static MALLOC_DEFINE(M_NETGRAPH_GIF_DEMUX, "netgraph_gif_demux",
95 "netgraph gif demux node");
96#else
97#define M_NETGRAPH_GIF_DEMUX M_NETGRAPH
98#endif
99
100/* This struct describes one address family */
101struct iffam {
102 sa_family_t family; /* Address family */
103 const char *hookname; /* Name for hook */
104};
105typedef const struct iffam *iffam_p;
106
107/* List of address families supported by our interface */
108const 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 },
115};
116#define NUM_FAMILIES nitems(gFamilies)
117
118/* Per-node private data */
120 node_p node; /* Our netgraph node */
121 hook_p gif; /* The gif hook */
122 hook_p hooks[NUM_FAMILIES]; /* The protocol hooks */
123};
125
126/* Netgraph node methods */
133
134/* Helper stuff */
135static iffam_p get_iffam_from_af(sa_family_t family);
137static iffam_p get_iffam_from_name(const char *name);
139
140/******************************************************************
141 NETGRAPH PARSE TYPES
142******************************************************************/
143
144/* List of commands and how to convert arguments to/from ASCII */
145static const struct ng_cmdlist ng_gif_demux_cmdlist[] = {
146 { 0 }
147};
148
149/* Node type descriptor */
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,
160};
162
163/************************************************************************
164 HELPER STUFF
165 ************************************************************************/
166
167/*
168 * Get the family descriptor from the family ID
169 */
170static __inline iffam_p
171get_iffam_from_af(sa_family_t family)
172{
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 */
187static __inline iffam_p
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
202static __inline hook_p *
204{
205 return (&priv->hooks[iffam - gFamilies]);
206}
207
208/*
209 * Get the iffam descriptor from the name
210 */
211static __inline iffam_p
213{
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 */
232static int
234{
235 priv_p priv;
236
237 /* Allocate and initialize private info */
238 priv = malloc(sizeof(*priv), M_NETGRAPH_GIF_DEMUX, M_WAITOK | M_ZERO);
239 priv->node = node;
240
242
243 /* Done */
244 return (0);
245}
246
247/*
248 * Method for attaching a new hook
249 */
250static int
251ng_gif_demux_newhook(node_p node, hook_p hook, const char *name)
252{
253 const priv_p priv = NG_NODE_PRIVATE(node);
255 hook_p *hookptr;
256
257 if (strcmp(NG_GIF_DEMUX_HOOK_GIF, name) == 0)
258 hookptr = &priv->gif;
259 else {
261 if (iffam == NULL)
262 return (EPFNOSUPPORT);
263 hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
264 }
265 if (*hookptr != NULL)
266 return (EISCONN);
267 *hookptr = hook;
268 return (0);
269}
270
271/*
272 * Receive a control message
273 */
274static int
276{
277 struct ng_mesg *resp = NULL;
278 int error = 0;
279 struct ng_mesg *msg;
280
281 NGI_GET_MSG(item, msg);
282 switch (msg->header.typecookie) {
284 switch (msg->header.cmd) {
285 /* XXX: Add commands here. */
286 default:
287 error = EINVAL;
288 break;
289 }
290 break;
291 default:
292 error = EINVAL;
293 break;
294 }
295
296 /* Done */
297 NG_RESPOND_MSG(error, node, item, resp);
298 NG_FREE_MSG(msg);
299 return (error);
300}
301
302/*
303 * Receive data on a hook
304 */
305static int
307{
308 const node_p node = NG_HOOK_NODE(hook);
309 const priv_p priv = NG_NODE_PRIVATE(node);
311 hook_p outhook;
312 int error = 0;
313 struct mbuf *m;
314
315 /* Pull the mbuf out of the item for processing. */
316 NGI_GET_M(item, m);
317
318 if (hook == priv->gif) {
319 /*
320 * Pull off the address family header and find the
321 * output hook.
322 */
323 if (m->m_pkthdr.len < sizeof(sa_family_t)) {
324 NG_FREE_M(m);
325 NG_FREE_ITEM(item);
326 return (EINVAL);
327 }
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);
332 }
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);
338 }
339 outhook = *get_hook_from_iffam(priv, iffam);
340 m_adj(m, sizeof(sa_family_t));
341 } else {
342 /*
343 * Add address family header and set the output hook.
344 */
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);
351 }
352 bcopy(&iffam->family, mtod(m, sa_family_t *),
353 sizeof(iffam->family));
354 outhook = priv->gif;
355 }
356
357 /* Stuff the mbuf back in. */
358 NGI_M(item) = m;
359
360 /* Deliver packet */
361 NG_FWD_ITEM_HOOK(error, item, outhook);
362 return (error);
363}
364
365/*
366 * Shutdown node
367 */
368static int
370{
371 const priv_p priv = NG_NODE_PRIVATE(node);
372
374 NG_NODE_SET_PRIVATE(node, NULL);
375 NG_NODE_UNREF(node);
376 return (0);
377}
378
379/*
380 * Hook disconnection.
381 */
382static int
384{
387
388 if (hook == priv->gif)
389 priv->gif = NULL;
390 else {
392 if (iffam == NULL)
393 panic("%s", __func__);
395 }
396
397 return (0);
398}
#define NGI_M(i)
Definition: netgraph.h:833
#define NG_HOOK_NODE(hook)
Definition: netgraph.h:339
#define NG_FREE_M(m)
Definition: netgraph.h:946
int ng_rcvmsg_t(node_p node, item_p item, hook_p lasthook)
Definition: netgraph.h:105
#define NG_FWD_ITEM_HOOK(error, item, hook)
Definition: netgraph.h:898
int ng_disconnect_t(hook_p hook)
Definition: netgraph.h:107
#define NG_NODE_SET_PRIVATE(node, val)
Definition: netgraph.h:608
#define NG_RESPOND_MSG(error, here, item, resp)
Definition: netgraph.h:1025
#define NG_NODE_UNREF(node)
Definition: netgraph.h:607
int ng_rcvdata_t(hook_p hook, item_p item)
Definition: netgraph.h:106
int ng_shutdown_t(node_p node)
Definition: netgraph.h:101
#define NG_FREE_ITEM(item)
Definition: netgraph.h:847
int ng_constructor_t(node_p node)
Definition: netgraph.h:99
#define NGI_GET_M(i, m)
Definition: netgraph.h:852
#define NG_FREE_MSG(msg)
Definition: netgraph.h:938
#define NG_ABI_VERSION
Definition: netgraph.h:77
#define NGI_GET_MSG(i, m)
Definition: netgraph.h:858
#define NG_NODE_PRIVATE(node)
Definition: netgraph.h:609
int ng_newhook_t(node_p node, hook_p hook, const char *name)
Definition: netgraph.h:102
MALLOC_DEFINE(M_NG_CCATM, "ng_ccatm", "netgraph uni api node")
static ng_shutdown_t ng_gif_demux_shutdown
Definition: ng_gif_demux.c:129
const struct iffam * iffam_p
Definition: ng_gif_demux.c:105
static iffam_p get_iffam_from_af(sa_family_t family)
Definition: ng_gif_demux.c:171
static const struct iffam gFamilies[]
Definition: ng_gif_demux.c:108
static ng_newhook_t ng_gif_demux_newhook
Definition: ng_gif_demux.c:130
static const struct ng_cmdlist ng_gif_demux_cmdlist[]
Definition: ng_gif_demux.c:145
static ng_rcvmsg_t ng_gif_demux_rcvmsg
Definition: ng_gif_demux.c:128
static hook_p * get_hook_from_iffam(priv_p priv, iffam_p iffam)
Definition: ng_gif_demux.c:203
static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook)
Definition: ng_gif_demux.c:188
NETGRAPH_INIT(gif_demux, &ng_gif_demux_typestruct)
static ng_disconnect_t ng_gif_demux_disconnect
Definition: ng_gif_demux.c:132
static struct ng_type ng_gif_demux_typestruct
Definition: ng_gif_demux.c:150
static ng_rcvdata_t ng_gif_demux_rcvdata
Definition: ng_gif_demux.c:131
static ng_constructor_t ng_gif_demux_constructor
Definition: ng_gif_demux.c:127
struct ng_gif_demux_private * priv_p
Definition: ng_gif_demux.c:124
#define NUM_FAMILIES
Definition: ng_gif_demux.c:116
#define M_NETGRAPH_GIF_DEMUX
Definition: ng_gif_demux.c:97
static iffam_p get_iffam_from_name(const char *name)
Definition: ng_gif_demux.c:212
#define NG_GIF_DEMUX_HOOK_GIF
Definition: ng_gif_demux.h:45
#define NGM_GIF_DEMUX_COOKIE
Definition: ng_gif_demux.h:42
#define NG_GIF_DEMUX_HOOK_INET6
Definition: ng_gif_demux.h:47
#define NG_GIF_DEMUX_HOOK_NATM
Definition: ng_gif_demux.h:51
#define NG_GIF_DEMUX_HOOK_ATALK
Definition: ng_gif_demux.h:48
#define NG_GIF_DEMUX_HOOK_ATM
Definition: ng_gif_demux.h:50
#define NG_GIF_DEMUX_NODE_TYPE
Definition: ng_gif_demux.h:41
#define NG_GIF_DEMUX_HOOK_INET
Definition: ng_gif_demux.h:46
#define NG_GIF_DEMUX_HOOK_IPX
Definition: ng_gif_demux.h:49
char *const name
Definition: ng_ppp.c:261
const char * hookname
Definition: ng_gif_demux.c:103
sa_family_t family
Definition: ng_gif_demux.c:102
hook_p hooks[NUM_FAMILIES]
Definition: ng_gif_demux.c:122
u_int32_t typecookie
Definition: ng_message.h:66
struct ng_mesg::ng_msghdr header
u_int32_t version
Definition: netgraph.h:1077
Definition: ng_sscfu.c:66