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