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