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>
89 #include <netgraph/ng_message.h>
90 #include <netgraph/netgraph.h>
91 #include <netgraph/ng_parse.h>
92 #include <netgraph/ng_gif.h>
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))
97 /* Per-node private data */
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 */
103 typedef struct private *priv_p;
105 /* Functional hooks called from if_gif.c */
106 static void ng_gif_input(struct ifnet *ifp, struct mbuf **mp, int af);
107 static void ng_gif_input_orphan(struct ifnet *ifp, struct mbuf *m, int af);
108 static void ng_gif_attach(struct ifnet *ifp);
109 static void ng_gif_detach(struct ifnet *ifp);
111 /* Other functions */
112 static void ng_gif_input2(node_p node, struct mbuf **mp, int af);
113 static int ng_gif_glue_af(struct mbuf **mp, int af);
114 static int ng_gif_rcv_lower(node_p node, struct mbuf *m);
116 /* Netgraph node methods */
117 static ng_constructor_t ng_gif_constructor;
118 static ng_rcvmsg_t ng_gif_rcvmsg;
119 static ng_shutdown_t ng_gif_shutdown;
120 static ng_newhook_t ng_gif_newhook;
121 static ng_connect_t ng_gif_connect;
122 static ng_rcvdata_t ng_gif_rcvdata;
123 static ng_disconnect_t ng_gif_disconnect;
124 static int ng_gif_mod_event(module_t mod, int event, void *data);
126 /* List of commands and how to convert arguments to/from ASCII */
127 static const struct ng_cmdlist ng_gif_cmdlist[] = {
133 &ng_parse_string_type
145 static 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,
158 MODULE_DEPEND(ng_gif, if_gif, 1,1,1);
159 NETGRAPH_INIT(gif, &ng_gif_typestruct);
161 /******************************************************************
163 ******************************************************************/
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.
170 ng_gif_input(struct ifnet *ifp, struct mbuf **mp, int af)
172 const node_p node = IFP2NG(ifp);
173 const priv_p priv = NG_NODE_PRIVATE(node);
175 /* If "lower" hook not connected, let packet continue */
176 if (priv->lower == NULL || priv->lowerOrphan)
178 ng_gif_input2(node, mp, af);
182 * Handle a packet that has come in on an interface, and which
183 * does not match any of our known protocols (an ``orphan'').
186 ng_gif_input_orphan(struct ifnet *ifp, struct mbuf *m, int af)
188 const node_p node = IFP2NG(ifp);
189 const priv_p priv = NG_NODE_PRIVATE(node);
191 /* If "orphan" hook not connected, let packet continue */
192 if (priv->lower == NULL || !priv->lowerOrphan) {
196 ng_gif_input2(node, &m, af);
202 * Handle a packet that has come in on a gif interface.
203 * Attach the address family to the mbuf for later use.
206 ng_gif_input2(node_p node, struct mbuf **mp, int af)
208 const priv_p priv = NG_NODE_PRIVATE(node);
211 /* Glue address family on */
212 if ((error = ng_gif_glue_af(mp, af)) != 0)
215 /* Send out lower/orphan hook */
216 NG_SEND_DATA_ONLY(error, priv->lower, *mp);
221 * A new gif interface has been attached.
222 * Create a new node for it, etc.
225 ng_gif_attach(struct ifnet *ifp)
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);
238 /* Allocate private data */
239 priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
241 log(LOG_ERR, "%s: can't %s for %s\n",
242 __func__, "allocate memory", ifp->if_xname);
246 NG_NODE_SET_PRIVATE(node, priv);
248 IFP2NG_SET(ifp, node);
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);
258 * An interface is being detached.
259 * REALLY Destroy its node.
262 ng_gif_detach(struct ifnet *ifp)
264 const node_p node = IFP2NG(ifp);
267 if (node == NULL) /* no node (why not?), ignore */
269 priv = NG_NODE_PRIVATE(node);
270 NG_NODE_REALLY_DIE(node); /* Force real removal of node */
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).
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 */
282 * Optimization for gluing the address family onto
283 * the front of an incoming packet.
286 ng_gif_glue_af(struct mbuf **mp, int af)
288 struct mbuf *m = *mp;
292 tmp_af = (sa_family_t) af;
295 * XXX: should try to bring back some of the optimizations from
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.
308 M_PREPEND(m, sizeof (tmp_af), M_NOWAIT);
317 /* Copy header and return (possibly new) mbuf */
318 *mtod(m, sa_family_t *) = tmp_af;
320 bcopy((caddr_t)&tmp_af, mtod(m, sa_family_t *), sizeof(tmp_af));
327 /******************************************************************
328 NETGRAPH NODE METHODS
329 ******************************************************************/
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).
337 ng_gif_constructor(node_p node)
343 * Check for attaching a new hook.
346 ng_gif_newhook(node_p node, hook_p hook, const char *name)
348 const priv_p priv = NG_NODE_PRIVATE(node);
349 u_char orphan = priv->lowerOrphan;
352 /* Divert hook is an alias for lower */
353 if (strcmp(name, NG_GIF_HOOK_DIVERT) == 0)
354 name = NG_GIF_HOOK_LOWER;
357 if (strcmp(name, NG_GIF_HOOK_LOWER) == 0) {
358 hookptr = &priv->lower;
360 } else if (strcmp(name, NG_GIF_HOOK_ORPHAN) == 0) {
361 hookptr = &priv->lower;
366 /* Check if already connected (shouldn't be, but doesn't hurt) */
367 if (*hookptr != NULL)
372 priv->lowerOrphan = orphan;
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.
382 ng_gif_connect(hook_p hook)
384 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
389 * Receive an incoming control message.
392 ng_gif_rcvmsg(node_p node, item_p item, hook_p lasthook)
394 const priv_p priv = NG_NODE_PRIVATE(node);
395 struct ng_mesg *resp = NULL;
399 NGI_GET_MSG(item, msg);
400 switch (msg->header.typecookie) {
402 switch (msg->header.cmd) {
403 case NGM_GIF_GET_IFNAME:
404 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
409 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
411 case NGM_GIF_GET_IFINDEX:
412 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
417 *((u_int32_t *)resp->data) = priv->ifp->if_index;
428 NG_RESPOND_MSG(error, node, item, resp);
434 * Receive data on a hook.
437 ng_gif_rcvdata(hook_p hook, item_p item)
439 const node_p node = NG_HOOK_NODE(hook);
440 const priv_p priv = NG_NODE_PRIVATE(node);
446 if (hook == priv->lower)
447 return ng_gif_rcv_lower(node, m);
448 panic("%s: weird hook", __func__);
452 * Handle an mbuf received on the "lower" hook.
455 ng_gif_rcv_lower(node_p node, struct mbuf *m)
458 const priv_p priv = NG_NODE_PRIVATE(node);
460 bzero(&dst, sizeof(dst));
462 /* Make sure header is fully pulled up */
463 if (m->m_pkthdr.len < sizeof(sa_family_t)) {
467 if (m->m_len < sizeof(sa_family_t)
468 && (m = m_pullup(m, sizeof(sa_family_t))) == NULL) {
472 dst.sa_family = *mtod(m, sa_family_t *);
473 m_adj(m, sizeof(sa_family_t));
475 /* Send it on its way */
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.
481 return gif_output(priv->ifp, m, &dst, NULL);
485 * Shutdown node. This resets the node but does not remove it
486 * unless the REALLY_DIE flag is set.
489 ng_gif_shutdown(node_p node)
491 const priv_p priv = NG_NODE_PRIVATE(node);
493 if (node->nd_flags & NGF_REALLY_DIE) {
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.
500 NG_NODE_SET_PRIVATE(node, NULL);
501 free(priv, M_NETGRAPH);
502 NG_NODE_UNREF(node); /* free node itself */
505 NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */
510 * Hook disconnection.
513 ng_gif_disconnect(hook_p hook)
515 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
517 if (hook == priv->lower) {
519 priv->lowerOrphan = 0;
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 */
529 /******************************************************************
531 ******************************************************************/
534 * Handle loading and unloading for this node type.
537 ng_gif_mod_event(module_t mod, int event, void *data)
539 VNET_ITERATOR_DECL(vnet_iter);
546 /* Register function hooks */
547 if (ng_gif_attach_p != NULL) {
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;
556 /* Create nodes for any already-existing gif interfaces */
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)
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.
580 * XXX: what about manual unloads?!?
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;