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