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