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
69 * ng_gif(4) netgraph node type
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
74 #include <sys/malloc.h>
76 #include <sys/errno.h>
77 #include <sys/syslog.h>
78 #include <sys/socket.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>
88 #include <netgraph/ng_message.h>
89 #include <netgraph/netgraph.h>
90 #include <netgraph/ng_parse.h>
91 #include <netgraph/ng_gif.h>
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))
96 /* Per-node private data */
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 */
102 typedef struct private *priv_p;
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);
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);
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);
125 /* List of commands and how to convert arguments to/from ASCII */
126 static const struct ng_cmdlist ng_gif_cmdlist[] = {
132 &ng_parse_string_type
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,
157 MODULE_DEPEND(ng_gif, if_gif, 1,1,1);
158 NETGRAPH_INIT(gif, &ng_gif_typestruct);
160 /******************************************************************
162 ******************************************************************/
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.
169 ng_gif_input(struct ifnet *ifp, struct mbuf **mp, int af)
171 const node_p node = IFP2NG(ifp);
172 const priv_p priv = NG_NODE_PRIVATE(node);
174 /* If "lower" hook not connected, let packet continue */
175 if (priv->lower == NULL || priv->lowerOrphan)
177 ng_gif_input2(node, mp, af);
181 * Handle a packet that has come in on an interface, and which
182 * does not match any of our known protocols (an ``orphan'').
185 ng_gif_input_orphan(struct ifnet *ifp, struct mbuf *m, int af)
187 const node_p node = IFP2NG(ifp);
188 const priv_p priv = NG_NODE_PRIVATE(node);
190 /* If "orphan" hook not connected, let packet continue */
191 if (priv->lower == NULL || !priv->lowerOrphan) {
195 ng_gif_input2(node, &m, af);
201 * Handle a packet that has come in on a gif interface.
202 * Attach the address family to the mbuf for later use.
205 ng_gif_input2(node_p node, struct mbuf **mp, int af)
207 const priv_p priv = NG_NODE_PRIVATE(node);
210 /* Glue address family on */
211 if ((error = ng_gif_glue_af(mp, af)) != 0)
214 /* Send out lower/orphan hook */
215 NG_SEND_DATA_ONLY(error, priv->lower, *mp);
220 * A new gif interface has been attached.
221 * Create a new node for it, etc.
224 ng_gif_attach(struct ifnet *ifp)
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);
237 /* Allocate private data */
238 priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
240 log(LOG_ERR, "%s: can't %s for %s\n",
241 __func__, "allocate memory", ifp->if_xname);
245 NG_NODE_SET_PRIVATE(node, priv);
247 IFP2NG_SET(ifp, node);
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);
257 * An interface is being detached.
258 * REALLY Destroy its node.
261 ng_gif_detach(struct ifnet *ifp)
263 const node_p node = IFP2NG(ifp);
266 if (node == NULL) /* no node (why not?), ignore */
268 priv = NG_NODE_PRIVATE(node);
269 NG_NODE_REALLY_DIE(node); /* Force real removal of node */
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).
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 */
281 * Optimization for gluing the address family onto
282 * the front of an incoming packet.
285 ng_gif_glue_af(struct mbuf **mp, int af)
287 struct mbuf *m = *mp;
291 tmp_af = (sa_family_t) af;
294 * XXX: should try to bring back some of the optimizations from
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.
307 M_PREPEND(m, sizeof (tmp_af), M_NOWAIT);
316 /* Copy header and return (possibly new) mbuf */
317 *mtod(m, sa_family_t *) = tmp_af;
319 bcopy((caddr_t)&tmp_af, mtod(m, sa_family_t *), sizeof(tmp_af));
326 /******************************************************************
327 NETGRAPH NODE METHODS
328 ******************************************************************/
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).
336 ng_gif_constructor(node_p node)
342 * Check for attaching a new hook.
345 ng_gif_newhook(node_p node, hook_p hook, const char *name)
347 const priv_p priv = NG_NODE_PRIVATE(node);
348 u_char orphan = priv->lowerOrphan;
351 /* Divert hook is an alias for lower */
352 if (strcmp(name, NG_GIF_HOOK_DIVERT) == 0)
353 name = NG_GIF_HOOK_LOWER;
356 if (strcmp(name, NG_GIF_HOOK_LOWER) == 0) {
357 hookptr = &priv->lower;
359 } else if (strcmp(name, NG_GIF_HOOK_ORPHAN) == 0) {
360 hookptr = &priv->lower;
365 /* Check if already connected (shouldn't be, but doesn't hurt) */
366 if (*hookptr != NULL)
371 priv->lowerOrphan = orphan;
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.
381 ng_gif_connect(hook_p hook)
383 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
388 * Receive an incoming control message.
391 ng_gif_rcvmsg(node_p node, item_p item, hook_p lasthook)
393 const priv_p priv = NG_NODE_PRIVATE(node);
394 struct ng_mesg *resp = NULL;
398 NGI_GET_MSG(item, msg);
399 switch (msg->header.typecookie) {
401 switch (msg->header.cmd) {
402 case NGM_GIF_GET_IFNAME:
403 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
408 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
410 case NGM_GIF_GET_IFINDEX:
411 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
416 *((u_int32_t *)resp->data) = priv->ifp->if_index;
427 NG_RESPOND_MSG(error, node, item, resp);
433 * Receive data on a hook.
436 ng_gif_rcvdata(hook_p hook, item_p item)
438 const node_p node = NG_HOOK_NODE(hook);
439 const priv_p priv = NG_NODE_PRIVATE(node);
445 if (hook == priv->lower)
446 return ng_gif_rcv_lower(node, m);
447 panic("%s: weird hook", __func__);
451 * Handle an mbuf received on the "lower" hook.
454 ng_gif_rcv_lower(node_p node, struct mbuf *m)
457 const priv_p priv = NG_NODE_PRIVATE(node);
459 bzero(&dst, sizeof(dst));
461 /* Make sure header is fully pulled up */
462 if (m->m_pkthdr.len < sizeof(sa_family_t)) {
466 if (m->m_len < sizeof(sa_family_t)
467 && (m = m_pullup(m, sizeof(sa_family_t))) == NULL) {
471 dst.sa_family = *mtod(m, sa_family_t *);
472 m_adj(m, sizeof(sa_family_t));
474 /* Send it on its way */
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.
480 return gif_output(priv->ifp, m, &dst, NULL);
484 * Shutdown node. This resets the node but does not remove it
485 * unless the REALLY_DIE flag is set.
488 ng_gif_shutdown(node_p node)
490 const priv_p priv = NG_NODE_PRIVATE(node);
492 if (node->nd_flags & NGF_REALLY_DIE) {
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.
499 NG_NODE_SET_PRIVATE(node, NULL);
500 free(priv, M_NETGRAPH);
501 NG_NODE_UNREF(node); /* free node itself */
504 NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */
509 * Hook disconnection.
512 ng_gif_disconnect(hook_p hook)
514 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
516 if (hook == priv->lower) {
518 priv->lowerOrphan = 0;
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 */
528 /******************************************************************
530 ******************************************************************/
533 * Handle loading and unloading for this node type.
536 ng_gif_mod_event(module_t mod, int event, void *data)
538 VNET_ITERATOR_DECL(vnet_iter);
545 /* Register function hooks */
546 if (ng_gif_attach_p != NULL) {
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;
555 /* Create nodes for any already-existing gif interfaces */
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)
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.
579 * XXX: what about manual unloads?!?
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;