6 * SPDX-License-Identifier: BSD-3-Clause AND BSD-2-Clause
8 * Copyright 2001 The Aerospace Corporation. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
35 * Copyright (c) 1996-2000 Whistle Communications, Inc.
36 * All rights reserved.
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.
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
71 * ng_gif(4) netgraph node type
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/malloc.h>
78 #include <sys/errno.h>
79 #include <sys/syslog.h>
80 #include <sys/socket.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>
90 #include <netgraph/ng_message.h>
91 #include <netgraph/netgraph.h>
92 #include <netgraph/ng_parse.h>
93 #include <netgraph/ng_gif.h>
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))
98 /* Per-node private data */
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 */
104 typedef struct private *priv_p;
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);
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);
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);
127 /* List of commands and how to convert arguments to/from ASCII */
128 static const struct ng_cmdlist ng_gif_cmdlist[] = {
134 &ng_parse_string_type
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,
159 MODULE_DEPEND(ng_gif, if_gif, 1,1,1);
160 NETGRAPH_INIT(gif, &ng_gif_typestruct);
162 /******************************************************************
164 ******************************************************************/
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.
171 ng_gif_input(struct ifnet *ifp, struct mbuf **mp, int af)
173 const node_p node = IFP2NG(ifp);
174 const priv_p priv = NG_NODE_PRIVATE(node);
176 /* If "lower" hook not connected, let packet continue */
177 if (priv->lower == NULL || priv->lowerOrphan)
179 ng_gif_input2(node, mp, af);
183 * Handle a packet that has come in on an interface, and which
184 * does not match any of our known protocols (an ``orphan'').
187 ng_gif_input_orphan(struct ifnet *ifp, struct mbuf *m, int af)
189 const node_p node = IFP2NG(ifp);
190 const priv_p priv = NG_NODE_PRIVATE(node);
192 /* If "orphan" hook not connected, let packet continue */
193 if (priv->lower == NULL || !priv->lowerOrphan) {
197 ng_gif_input2(node, &m, af);
203 * Handle a packet that has come in on a gif interface.
204 * Attach the address family to the mbuf for later use.
207 ng_gif_input2(node_p node, struct mbuf **mp, int af)
209 const priv_p priv = NG_NODE_PRIVATE(node);
212 /* Glue address family on */
213 if ((error = ng_gif_glue_af(mp, af)) != 0)
216 /* Send out lower/orphan hook */
217 NG_SEND_DATA_ONLY(error, priv->lower, *mp);
222 * A new gif interface has been attached.
223 * Create a new node for it, etc.
226 ng_gif_attach(struct ifnet *ifp)
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);
239 /* Allocate private data */
240 priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
242 log(LOG_ERR, "%s: can't %s for %s\n",
243 __func__, "allocate memory", ifp->if_xname);
247 NG_NODE_SET_PRIVATE(node, priv);
249 IFP2NG_SET(ifp, node);
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);
259 * An interface is being detached.
260 * REALLY Destroy its node.
263 ng_gif_detach(struct ifnet *ifp)
265 const node_p node = IFP2NG(ifp);
268 if (node == NULL) /* no node (why not?), ignore */
270 priv = NG_NODE_PRIVATE(node);
271 NG_NODE_REALLY_DIE(node); /* Force real removal of node */
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).
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 */
283 * Optimization for gluing the address family onto
284 * the front of an incoming packet.
287 ng_gif_glue_af(struct mbuf **mp, int af)
289 struct mbuf *m = *mp;
293 tmp_af = (sa_family_t) af;
296 * XXX: should try to bring back some of the optimizations from
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.
309 M_PREPEND(m, sizeof (tmp_af), M_NOWAIT);
318 /* Copy header and return (possibly new) mbuf */
319 *mtod(m, sa_family_t *) = tmp_af;
321 bcopy((caddr_t)&tmp_af, mtod(m, sa_family_t *), sizeof(tmp_af));
328 /******************************************************************
329 NETGRAPH NODE METHODS
330 ******************************************************************/
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).
338 ng_gif_constructor(node_p node)
344 * Check for attaching a new hook.
347 ng_gif_newhook(node_p node, hook_p hook, const char *name)
349 const priv_p priv = NG_NODE_PRIVATE(node);
350 u_char orphan = priv->lowerOrphan;
353 /* Divert hook is an alias for lower */
354 if (strcmp(name, NG_GIF_HOOK_DIVERT) == 0)
355 name = NG_GIF_HOOK_LOWER;
358 if (strcmp(name, NG_GIF_HOOK_LOWER) == 0) {
359 hookptr = &priv->lower;
361 } else if (strcmp(name, NG_GIF_HOOK_ORPHAN) == 0) {
362 hookptr = &priv->lower;
367 /* Check if already connected (shouldn't be, but doesn't hurt) */
368 if (*hookptr != NULL)
373 priv->lowerOrphan = orphan;
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.
383 ng_gif_connect(hook_p hook)
385 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
390 * Receive an incoming control message.
393 ng_gif_rcvmsg(node_p node, item_p item, hook_p lasthook)
395 const priv_p priv = NG_NODE_PRIVATE(node);
396 struct ng_mesg *resp = NULL;
400 NGI_GET_MSG(item, msg);
401 switch (msg->header.typecookie) {
403 switch (msg->header.cmd) {
404 case NGM_GIF_GET_IFNAME:
405 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
410 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
412 case NGM_GIF_GET_IFINDEX:
413 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
418 *((u_int32_t *)resp->data) = priv->ifp->if_index;
429 NG_RESPOND_MSG(error, node, item, resp);
435 * Receive data on a hook.
438 ng_gif_rcvdata(hook_p hook, item_p item)
440 const node_p node = NG_HOOK_NODE(hook);
441 const priv_p priv = NG_NODE_PRIVATE(node);
447 if (hook == priv->lower)
448 return ng_gif_rcv_lower(node, m);
449 panic("%s: weird hook", __func__);
453 * Handle an mbuf received on the "lower" hook.
456 ng_gif_rcv_lower(node_p node, struct mbuf *m)
459 const priv_p priv = NG_NODE_PRIVATE(node);
461 bzero(&dst, sizeof(dst));
463 /* Make sure header is fully pulled up */
464 if (m->m_pkthdr.len < sizeof(sa_family_t)) {
468 if (m->m_len < sizeof(sa_family_t)
469 && (m = m_pullup(m, sizeof(sa_family_t))) == NULL) {
473 dst.sa_family = *mtod(m, sa_family_t *);
474 m_adj(m, sizeof(sa_family_t));
476 /* Send it on its way */
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.
482 return gif_output(priv->ifp, m, &dst, NULL);
486 * Shutdown node. This resets the node but does not remove it
487 * unless the REALLY_DIE flag is set.
490 ng_gif_shutdown(node_p node)
492 const priv_p priv = NG_NODE_PRIVATE(node);
494 if (node->nd_flags & NGF_REALLY_DIE) {
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.
501 NG_NODE_SET_PRIVATE(node, NULL);
502 free(priv, M_NETGRAPH);
503 NG_NODE_UNREF(node); /* free node itself */
506 NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */
511 * Hook disconnection.
514 ng_gif_disconnect(hook_p hook)
516 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
518 if (hook == priv->lower) {
520 priv->lowerOrphan = 0;
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 */
530 /******************************************************************
532 ******************************************************************/
535 * Handle loading and unloading for this node type.
538 ng_gif_mod_event(module_t mod, int event, void *data)
540 VNET_ITERATOR_DECL(vnet_iter);
547 /* Register function hooks */
548 if (ng_gif_attach_p != NULL) {
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;
557 /* Create nodes for any already-existing gif interfaces */
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)
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.
581 * XXX: what about manual unloads?!?
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;