ng_gif.c revision 132464
1/*
2 * ng_gif.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-2000 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: head/sys/netgraph/ng_gif.c 132464 2004-07-20 17:15:38Z julian $
64 */
65
66/*
67 * ng_gif(4) netgraph node type
68 */
69
70#include <sys/param.h>
71#include <sys/systm.h>
72#include <sys/kernel.h>
73#include <sys/malloc.h>
74#include <sys/mbuf.h>
75#include <sys/errno.h>
76#include <sys/syslog.h>
77#include <sys/socket.h>
78
79#include <net/if.h>
80#include <net/route.h>
81#include <net/if_types.h>
82#include <net/if_var.h>
83#include <net/if_gif.h>
84
85#include <netgraph/ng_message.h>
86#include <netgraph/netgraph.h>
87#include <netgraph/ng_parse.h>
88#include <netgraph/ng_gif.h>
89
90#define IFP2NG(ifp)  ((struct ng_node *)((struct gif_softc *)(ifp))->gif_netgraph)
91
92/* Per-node private data */
93struct private {
94	struct ifnet	*ifp;		/* associated interface */
95	hook_p		lower;		/* lower OR orphan hook connection */
96	u_char		lowerOrphan;	/* whether lower is lower or orphan */
97};
98typedef struct private *priv_p;
99
100/* Functional hooks called from if_gif.c */
101static void	ng_gif_input(struct ifnet *ifp, struct mbuf **mp, int af);
102static void	ng_gif_input_orphan(struct ifnet *ifp, struct mbuf *m, int af);
103static void	ng_gif_attach(struct ifnet *ifp);
104static void	ng_gif_detach(struct ifnet *ifp);
105
106/* Other functions */
107static void	ng_gif_input2(node_p node, struct mbuf **mp, int af);
108static int	ng_gif_glue_af(struct mbuf **mp, int af);
109static int	ng_gif_rcv_lower(node_p node, struct mbuf *m);
110
111/* Netgraph node methods */
112static ng_constructor_t	ng_gif_constructor;
113static ng_rcvmsg_t	ng_gif_rcvmsg;
114static ng_shutdown_t	ng_gif_shutdown;
115static ng_newhook_t	ng_gif_newhook;
116static ng_connect_t	ng_gif_connect;
117static ng_rcvdata_t	ng_gif_rcvdata;
118static ng_disconnect_t	ng_gif_disconnect;
119static int		ng_gif_mod_event(module_t mod, int event, void *data);
120
121/* List of commands and how to convert arguments to/from ASCII */
122static const struct ng_cmdlist ng_gif_cmdlist[] = {
123	{
124	  NGM_GIF_COOKIE,
125	  NGM_GIF_GET_IFNAME,
126	  "getifname",
127	  NULL,
128	  &ng_parse_string_type
129	},
130	{
131	  NGM_GIF_COOKIE,
132	  NGM_GIF_GET_IFINDEX,
133	  "getifindex",
134	  NULL,
135	  &ng_parse_int32_type
136	},
137	{ 0 }
138};
139
140static struct ng_type ng_gif_typestruct = {
141	.version =	NG_ABI_VERSION,
142	.name =		NG_GIF_NODE_TYPE,
143	.mod_event =	ng_gif_mod_event,
144	.constructor =	ng_gif_constructor,
145	.rcvmsg =	ng_gif_rcvmsg,
146	.shutdown =	ng_gif_shutdown,
147	.newhook =	ng_gif_newhook,
148	.connect =	ng_gif_connect,
149	.rcvdata =	ng_gif_rcvdata,
150	.disconnect =	ng_gif_disconnect,
151	.cmdlist =	ng_gif_cmdlist,
152};
153MODULE_VERSION(ng_gif, 1);
154MODULE_DEPEND(ng_gif, if_gif, 1,1,1);
155NETGRAPH_INIT(gif, &ng_gif_typestruct);
156
157/******************************************************************
158		       GIF FUNCTION HOOKS
159******************************************************************/
160
161/*
162 * Handle a packet that has come in on an interface. We get to
163 * look at it here before any upper layer protocols do.
164 *
165 * NOTE: this function will get called at splimp()
166 */
167static void
168ng_gif_input(struct ifnet *ifp, struct mbuf **mp, int af)
169{
170	const node_p node = IFP2NG(ifp);
171	const priv_p priv = NG_NODE_PRIVATE(node);
172
173	/* If "lower" hook not connected, let packet continue */
174	if (priv->lower == NULL || priv->lowerOrphan)
175		return;
176	ng_gif_input2(node, mp, af);
177}
178
179/*
180 * Handle a packet that has come in on an interface, and which
181 * does not match any of our known protocols (an ``orphan'').
182 *
183 * NOTE: this function will get called at splimp()
184 */
185static void
186ng_gif_input_orphan(struct ifnet *ifp, struct mbuf *m, int af)
187{
188	const node_p node = IFP2NG(ifp);
189	const priv_p priv = NG_NODE_PRIVATE(node);
190
191	/* If "orphan" hook not connected, let packet continue */
192	if (priv->lower == NULL || !priv->lowerOrphan) {
193		m_freem(m);
194		return;
195	}
196	ng_gif_input2(node, &m, af);
197	if (m != NULL)
198		m_freem(m);
199}
200
201/*
202 * Handle a packet that has come in on a gif interface.
203 * Attach the address family to the mbuf for later use.
204 *
205 * NOTE: this function will get called at splimp()
206 */
207static void
208ng_gif_input2(node_p node, struct mbuf **mp, int af)
209{
210	const priv_p priv = NG_NODE_PRIVATE(node);
211	int error;
212
213	/* Glue address family on */
214	if ((error = ng_gif_glue_af(mp, af)) != 0)
215		return;
216
217	/* Send out lower/orphan hook */
218	NG_SEND_DATA_ONLY(error, priv->lower, *mp);
219	*mp = NULL;
220}
221
222/*
223 * A new gif interface has been attached.
224 * Create a new node for it, etc.
225 */
226static void
227ng_gif_attach(struct ifnet *ifp)
228{
229	priv_p priv;
230	node_p node;
231
232	/* Create node */
233	KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
234	if (ng_make_node_common(&ng_gif_typestruct, &node) != 0) {
235		log(LOG_ERR, "%s: can't %s for %s\n",
236		    __func__, "create node", ifp->if_xname);
237		return;
238	}
239
240	/* Allocate private data */
241	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
242	if (priv == NULL) {
243		log(LOG_ERR, "%s: can't %s for %s\n",
244		    __func__, "allocate memory", ifp->if_xname);
245		NG_NODE_UNREF(node);
246		return;
247	}
248	NG_NODE_SET_PRIVATE(node, priv);
249	priv->ifp = ifp;
250	IFP2NG(ifp) = node;
251
252	/* Try to give the node the same name as the interface */
253	if (ng_name_node(node, ifp->if_xname) != 0) {
254		log(LOG_WARNING, "%s: can't name node %s\n",
255		    __func__, ifp->if_xname);
256	}
257}
258
259/*
260 * An interface is being detached.
261 * REALLY Destroy its node.
262 */
263static void
264ng_gif_detach(struct ifnet *ifp)
265{
266	const node_p node = IFP2NG(ifp);
267	priv_p priv;
268
269	if (node == NULL)		/* no node (why not?), ignore */
270		return;
271	priv = NG_NODE_PRIVATE(node);
272	NG_NODE_REALLY_DIE(node);	/* Force real removal of node */
273	/*
274	 * We can't assume the ifnet is still around when we run shutdown
275	 * So zap it now. XXX We HOPE that anything running at this time
276	 * handles it (as it should in the non netgraph case).
277	 */
278	IFP2NG(ifp) = NULL;
279	priv->ifp = NULL;	/* XXX race if interrupted an output packet */
280	ng_rmnode_self(node);		/* remove all netgraph parts */
281}
282
283/*
284 * Optimization for gluing the address family onto
285 * the front of an incoming packet.
286 */
287static int
288ng_gif_glue_af(struct mbuf **mp, int af)
289{
290	struct mbuf *m = *mp;
291	int error = 0;
292	sa_family_t tmp_af;
293
294	tmp_af = (sa_family_t) af;
295
296	/*
297	 * XXX: should try to bring back some of the optimizations from
298	 * ng_ether.c
299	 */
300
301	/*
302	 * Doing anything more is likely to get more
303	 * expensive than it's worth..
304	 * it's probable that everything else is in one
305	 * big lump. The next node will do an m_pullup()
306	 * for exactly the amount of data it needs and
307	 * hopefully everything after that will not
308	 * need one. So let's just use M_PREPEND.
309	 */
310	M_PREPEND(m, sizeof (tmp_af), M_DONTWAIT);
311	if (m == NULL) {
312		error = ENOBUFS;
313		goto done;
314	}
315
316#if 0
317copy:
318#endif
319	/* Copy header and return (possibly new) mbuf */
320	*mtod(m, sa_family_t *) = tmp_af;
321#if 0
322	bcopy((caddr_t)&tmp_af, mtod(m, sa_family_t *), sizeof(tmp_af));
323#endif
324done:
325	*mp = m;
326	return error;
327}
328
329/******************************************************************
330		    NETGRAPH NODE METHODS
331******************************************************************/
332
333/*
334 * It is not possible or allowable to create a node of this type.
335 * Nodes get created when the interface is attached (or, when
336 * this node type's KLD is loaded).
337 */
338static int
339ng_gif_constructor(node_p node)
340{
341	return (EINVAL);
342}
343
344/*
345 * Check for attaching a new hook.
346 */
347static	int
348ng_gif_newhook(node_p node, hook_p hook, const char *name)
349{
350	const priv_p priv = NG_NODE_PRIVATE(node);
351	u_char orphan = priv->lowerOrphan;
352	hook_p *hookptr;
353
354	/* Divert hook is an alias for lower */
355	if (strcmp(name, NG_GIF_HOOK_DIVERT) == 0)
356		name = NG_GIF_HOOK_LOWER;
357
358	/* Which hook? */
359	if (strcmp(name, NG_GIF_HOOK_LOWER) == 0) {
360		hookptr = &priv->lower;
361		orphan = 0;
362	} else if (strcmp(name, NG_GIF_HOOK_ORPHAN) == 0) {
363		hookptr = &priv->lower;
364		orphan = 1;
365	} else
366		return (EINVAL);
367
368	/* Check if already connected (shouldn't be, but doesn't hurt) */
369	if (*hookptr != NULL)
370		return (EISCONN);
371
372	/* OK */
373	*hookptr = hook;
374	priv->lowerOrphan = orphan;
375	return (0);
376}
377
378/*
379 * Hooks are attached, adjust to force queueing.
380 * We don't really care which hook it is.
381 * they should all be queuing for outgoing data.
382 */
383static	int
384ng_gif_connect(hook_p hook)
385{
386	NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
387	return (0);
388}
389
390/*
391 * Receive an incoming control message.
392 */
393static int
394ng_gif_rcvmsg(node_p node, item_p item, hook_p lasthook)
395{
396	const priv_p priv = NG_NODE_PRIVATE(node);
397	struct ng_mesg *resp = NULL;
398	int error = 0;
399	struct ng_mesg *msg;
400
401	NGI_GET_MSG(item, msg);
402	switch (msg->header.typecookie) {
403	case NGM_GIF_COOKIE:
404		switch (msg->header.cmd) {
405		case NGM_GIF_GET_IFNAME:
406			NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT);
407			if (resp == NULL) {
408				error = ENOMEM;
409				break;
410			}
411			strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ + 1);
412			break;
413		case NGM_GIF_GET_IFINDEX:
414			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
415			if (resp == NULL) {
416				error = ENOMEM;
417				break;
418			}
419			*((u_int32_t *)resp->data) = priv->ifp->if_index;
420			break;
421		default:
422			error = EINVAL;
423			break;
424		}
425		break;
426	default:
427		error = EINVAL;
428		break;
429	}
430	NG_RESPOND_MSG(error, node, item, resp);
431	NG_FREE_MSG(msg);
432	return (error);
433}
434
435/*
436 * Receive data on a hook.
437 */
438static int
439ng_gif_rcvdata(hook_p hook, item_p item)
440{
441	const node_p node = NG_HOOK_NODE(hook);
442	const priv_p priv = NG_NODE_PRIVATE(node);
443	struct mbuf *m;
444
445	NGI_GET_M(item, m);
446	NG_FREE_ITEM(item);
447
448	if (hook == priv->lower)
449		return ng_gif_rcv_lower(node, m);
450	panic("%s: weird hook", __func__);
451}
452
453/*
454 * Handle an mbuf received on the "lower" hook.
455 */
456static int
457ng_gif_rcv_lower(node_p node, struct mbuf *m)
458{
459	struct sockaddr	dst;
460	const priv_p priv = NG_NODE_PRIVATE(node);
461
462	bzero(&dst, sizeof(dst));
463
464	/* Make sure header is fully pulled up */
465	if (m->m_pkthdr.len < sizeof(sa_family_t)) {
466		NG_FREE_M(m);
467		return (EINVAL);
468	}
469	if (m->m_len < sizeof(sa_family_t)
470	    && (m = m_pullup(m, sizeof(sa_family_t))) == NULL) {
471		return (ENOBUFS);
472	}
473
474	dst.sa_family = *mtod(m, sa_family_t *);
475	m_adj(m, sizeof(sa_family_t));
476
477	/* Send it on its way */
478	/*
479	 * XXX: gif_output only uses dst for the family and passes the
480	 * fourth argument (rt) to in{,6}_gif_output which ignore it.
481	 * If this changes ng_gif will probably break.
482	 */
483	return gif_output(priv->ifp, m, &dst, NULL);
484}
485
486/*
487 * Shutdown node. This resets the node but does not remove it
488 * unless the REALLY_DIE flag is set.
489 */
490static int
491ng_gif_shutdown(node_p node)
492{
493	const priv_p priv = NG_NODE_PRIVATE(node);
494
495	if (node->nd_flags & NGF_REALLY_DIE) {
496		/*
497		 * WE came here because the gif interface is being destroyed,
498		 * so stop being persistant.
499		 * Actually undo all the things we did on creation.
500		 * Assume the ifp has already been freed.
501		 */
502		NG_NODE_SET_PRIVATE(node, NULL);
503		FREE(priv, M_NETGRAPH);
504		NG_NODE_UNREF(node);	/* free node itself */
505		return (0);
506	}
507	NG_NODE_REVIVE(node);		/* Signal ng_rmnode we are persisant */
508	return (0);
509}
510
511/*
512 * Hook disconnection.
513 */
514static int
515ng_gif_disconnect(hook_p hook)
516{
517	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
518
519	if (hook == priv->lower) {
520		priv->lower = NULL;
521		priv->lowerOrphan = 0;
522	} else
523		panic("%s: weird hook", __func__);
524	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
525	    && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
526		ng_rmnode_self(NG_HOOK_NODE(hook));	/* reset node */
527
528	return (0);
529}
530
531/******************************************************************
532		    	INITIALIZATION
533******************************************************************/
534
535/*
536 * Handle loading and unloading for this node type.
537 */
538static int
539ng_gif_mod_event(module_t mod, int event, void *data)
540{
541	struct ifnet *ifp;
542	int error = 0;
543	int s;
544
545	s = splnet();
546	switch (event) {
547	case MOD_LOAD:
548
549		/* Register function hooks */
550		if (ng_gif_attach_p != NULL) {
551			error = EEXIST;
552			break;
553		}
554		ng_gif_attach_p = ng_gif_attach;
555		ng_gif_detach_p = ng_gif_detach;
556		ng_gif_input_p = ng_gif_input;
557		ng_gif_input_orphan_p = ng_gif_input_orphan;
558
559		/* Create nodes for any already-existing gif interfaces */
560		IFNET_RLOCK();
561		TAILQ_FOREACH(ifp, &ifnet, if_link) {
562			if (ifp->if_type == IFT_GIF)
563				ng_gif_attach(ifp);
564		}
565		IFNET_RUNLOCK();
566		break;
567
568	case MOD_UNLOAD:
569
570		/*
571		 * Note that the base code won't try to unload us until
572		 * all nodes have been removed, and that can't happen
573		 * until all gif interfaces are destroyed. In any
574		 * case, we know there are no nodes left if the action
575		 * is MOD_UNLOAD, so there's no need to detach any nodes.
576		 *
577		 * XXX: what about manual unloads?!?
578		 */
579
580		/* Unregister function hooks */
581		ng_gif_attach_p = NULL;
582		ng_gif_detach_p = NULL;
583		ng_gif_input_p = NULL;
584		ng_gif_input_orphan_p = NULL;
585		break;
586
587	default:
588		error = EOPNOTSUPP;
589		break;
590	}
591	splx(s);
592	return (error);
593}
594
595