6 * Copyright 2001 The Aerospace Corporation. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions, and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of The Aerospace Corporation may not be used to endorse or
18 * promote products derived from this software.
20 * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 1996-2000 Whistle Communications, Inc.
34 * All rights reserved.
36 * Subject to the following obligations and disclaimer of warranty, use and
37 * redistribution of this software, in source or object code forms, with or
38 * without modifications are expressly permitted by Whistle Communications;
39 * provided, however, that:
40 * 1. Any and all reproductions of the source or object code must include the
41 * copyright notice above and the following disclaimer of warranties; and
42 * 2. No rights are granted, in any manner or form, to use Whistle
43 * Communications, Inc. trademarks, including the mark "WHISTLE
44 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
45 * such appears in the above copyright notice or in the software.
47 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
48 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
49 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
50 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
51 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
52 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
53 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
54 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
55 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
56 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
57 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
58 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
59 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
62 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
65 * $FreeBSD: src/sys/netgraph/ng_gif.c,v 1.19 2005/06/10 16:49:21 brooks Exp $
69 * ng_gif(4) netgraph node type
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
75 #include <sys/malloc.h>
77 #include <sys/errno.h>
78 #include <sys/syslog.h>
79 #include <sys/socket.h>
82 #include <net/route.h>
83 #include <net/if_types.h>
84 #include <net/if_var.h>
85 #include <net/if_gif.h>
87 #include "ng_message.h"
92 #define IFP2NG(ifp) ((struct ng_node *)((struct gif_softc *)(ifp->if_softc))->gif_netgraph)
93 #define IFP2NG_SET(ifp, val) (((struct gif_softc *)(ifp->if_softc))->gif_netgraph = (val))
95 /* Per-node private data */
97 struct ifnet
*ifp
; /* associated interface */
98 hook_p lower
; /* lower OR orphan hook connection */
99 u_char lowerOrphan
; /* whether lower is lower or orphan */
101 typedef struct private *priv_p
;
103 /* Functional hooks called from if_gif.c */
104 static void ng_gif_input(struct ifnet
*ifp
, struct mbuf
**mp
, int af
);
105 static void ng_gif_input_orphan(struct ifnet
*ifp
, struct mbuf
*m
, int af
);
106 static void ng_gif_attach(struct ifnet
*ifp
);
107 static void ng_gif_detach(struct ifnet
*ifp
);
109 /* Other functions */
110 static void ng_gif_input2(node_p node
, struct mbuf
**mp
, int af
);
111 static int ng_gif_glue_af(struct mbuf
**mp
, int af
);
112 static int ng_gif_rcv_lower(node_p node
, struct mbuf
*m
);
114 /* Netgraph node methods */
115 static ng_constructor_t ng_gif_constructor
;
116 static ng_rcvmsg_t ng_gif_rcvmsg
;
117 static ng_shutdown_t ng_gif_shutdown
;
118 static ng_newhook_t ng_gif_newhook
;
119 static ng_connect_t ng_gif_connect
;
120 static ng_rcvdata_t ng_gif_rcvdata
;
121 static ng_disconnect_t ng_gif_disconnect
;
122 static int ng_gif_mod_event(module_t mod
, int event
, void *data
);
124 /* List of commands and how to convert arguments to/from ASCII */
125 static const struct ng_cmdlist ng_gif_cmdlist
[] = {
131 &ng_parse_string_type
143 static struct ng_type ng_gif_typestruct
= {
144 .version
= NG_ABI_VERSION
,
145 .name
= NG_GIF_NODE_TYPE
,
146 .mod_event
= ng_gif_mod_event
,
147 .constructor
= ng_gif_constructor
,
148 .rcvmsg
= ng_gif_rcvmsg
,
149 .shutdown
= ng_gif_shutdown
,
150 .newhook
= ng_gif_newhook
,
151 .connect
= ng_gif_connect
,
152 .rcvdata
= ng_gif_rcvdata
,
153 .disconnect
= ng_gif_disconnect
,
154 .cmdlist
= ng_gif_cmdlist
,
156 MODULE_DEPEND(ng_gif
, if_gif
, 1,1,1);
157 NETGRAPH_INIT(gif
, &ng_gif_typestruct
);
159 /******************************************************************
161 ******************************************************************/
164 * Handle a packet that has come in on an interface. We get to
165 * look at it here before any upper layer protocols do.
167 * NOTE: this function will get called at splimp()
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'').
185 * NOTE: this function will get called at splimp()
188 ng_gif_input_orphan(struct ifnet
*ifp
, struct mbuf
*m
, int af
)
190 const node_p node
= IFP2NG(ifp
);
191 const priv_p priv
= NG_NODE_PRIVATE(node
);
193 /* If "orphan" hook not connected, let packet continue */
194 if (priv
->lower
== NULL
|| !priv
->lowerOrphan
) {
198 ng_gif_input2(node
, &m
, af
);
204 * Handle a packet that has come in on a gif interface.
205 * Attach the address family to the mbuf for later use.
207 * NOTE: this function will get called at splimp()
210 ng_gif_input2(node_p node
, struct mbuf
**mp
, int af
)
212 const priv_p priv
= NG_NODE_PRIVATE(node
);
215 /* Glue address family on */
216 if ((error
= ng_gif_glue_af(mp
, af
)) != 0)
219 /* Send out lower/orphan hook */
220 NG_SEND_DATA_ONLY(error
, priv
->lower
, *mp
);
225 * A new gif interface has been attached.
226 * Create a new node for it, etc.
229 ng_gif_attach(struct ifnet
*ifp
)
235 KASSERT(!IFP2NG(ifp
), ("%s: node already exists?", __func__
));
236 if (ng_make_node_common(&ng_gif_typestruct
, &node
) != 0) {
237 log(LOG_ERR
, "%s: can't %s for %s\n",
238 __func__
, "create node", ifp
->if_xname
);
242 /* Allocate private data */
243 priv
= kmalloc(sizeof(*priv
), M_NETGRAPH
,
244 M_WAITOK
| M_NULLOK
| M_ZERO
);
246 log(LOG_ERR
, "%s: can't %s for %s\n",
247 __func__
, "allocate memory", ifp
->if_xname
);
251 NG_NODE_SET_PRIVATE(node
, priv
);
253 IFP2NG_SET(ifp
, node
);
255 /* Try to give the node the same name as the interface */
256 if (ng_name_node(node
, ifp
->if_xname
) != 0) {
257 log(LOG_WARNING
, "%s: can't name node %s\n",
258 __func__
, ifp
->if_xname
);
263 * An interface is being detached.
264 * REALLY Destroy its node.
267 ng_gif_detach(struct ifnet
*ifp
)
269 const node_p node
= IFP2NG(ifp
);
272 if (node
== NULL
) /* no node (why not?), ignore */
274 priv
= NG_NODE_PRIVATE(node
);
275 NG_NODE_REALLY_DIE(node
); /* Force real removal of node */
277 * We can't assume the ifnet is still around when we run shutdown
278 * So zap it now. XXX We HOPE that anything running at this time
279 * handles it (as it should in the non netgraph case).
281 IFP2NG_SET(ifp
, NULL
);
282 priv
->ifp
= NULL
; /* XXX race if interrupted an output packet */
283 ng_rmnode_self(node
); /* remove all netgraph parts */
287 * Optimization for gluing the address family onto
288 * the front of an incoming packet.
291 ng_gif_glue_af(struct mbuf
**mp
, int af
)
293 struct mbuf
*m
= *mp
;
297 tmp_af
= (sa_family_t
) af
;
300 * XXX: should try to bring back some of the optimizations from
305 * Doing anything more is likely to get more
306 * expensive than it's worth..
307 * it's probable that everything else is in one
308 * big lump. The next node will do an m_pullup()
309 * for exactly the amount of data it needs and
310 * hopefully everything after that will not
311 * need one. So let's just use M_PREPEND.
313 M_PREPEND(m
, sizeof (tmp_af
), M_NOWAIT
);
322 /* Copy header and return (possibly new) mbuf */
323 *mtod(m
, sa_family_t
*) = tmp_af
;
325 bcopy((caddr_t
)&tmp_af
, mtod(m
, sa_family_t
*), sizeof(tmp_af
));
332 /******************************************************************
333 NETGRAPH NODE METHODS
334 ******************************************************************/
337 * It is not possible or allowable to create a node of this type.
338 * Nodes get created when the interface is attached (or, when
339 * this node type's KLD is loaded).
342 ng_gif_constructor(node_p node
)
348 * Check for attaching a new hook.
351 ng_gif_newhook(node_p node
, hook_p hook
, const char *name
)
353 const priv_p priv
= NG_NODE_PRIVATE(node
);
354 u_char orphan
= priv
->lowerOrphan
;
357 /* Divert hook is an alias for lower */
358 if (strcmp(name
, NG_GIF_HOOK_DIVERT
) == 0)
359 name
= NG_GIF_HOOK_LOWER
;
362 if (strcmp(name
, NG_GIF_HOOK_LOWER
) == 0) {
363 hookptr
= &priv
->lower
;
365 } else if (strcmp(name
, NG_GIF_HOOK_ORPHAN
) == 0) {
366 hookptr
= &priv
->lower
;
371 /* Check if already connected (shouldn't be, but doesn't hurt) */
372 if (*hookptr
!= NULL
)
377 priv
->lowerOrphan
= orphan
;
382 * Hooks are attached, adjust to force queueing.
383 * We don't really care which hook it is.
384 * they should all be queuing for outgoing data.
387 ng_gif_connect(hook_p hook
)
389 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook
));
394 * Receive an incoming control message.
397 ng_gif_rcvmsg(node_p node
, item_p item
, hook_p lasthook
)
399 const priv_p priv
= NG_NODE_PRIVATE(node
);
400 struct ng_mesg
*resp
= NULL
;
404 NGI_GET_MSG(item
, msg
);
405 switch (msg
->header
.typecookie
) {
407 switch (msg
->header
.cmd
) {
408 case NGM_GIF_GET_IFNAME
:
409 NG_MKRESPONSE(resp
, msg
, IFNAMSIZ
, M_WAITOK
| M_NULLOK
);
414 strlcpy(resp
->data
, priv
->ifp
->if_xname
, IFNAMSIZ
);
416 case NGM_GIF_GET_IFINDEX
:
417 NG_MKRESPONSE(resp
, msg
, sizeof(u_int32_t
), M_WAITOK
| M_NULLOK
);
422 *((u_int32_t
*)resp
->data
) = priv
->ifp
->if_index
;
433 NG_RESPOND_MSG(error
, node
, item
, resp
);
439 * Receive data on a hook.
442 ng_gif_rcvdata(hook_p hook
, item_p item
)
444 const node_p node
= NG_HOOK_NODE(hook
);
445 const priv_p priv
= NG_NODE_PRIVATE(node
);
451 if (hook
== priv
->lower
)
452 return ng_gif_rcv_lower(node
, m
);
453 panic("%s: weird hook", __func__
);
457 * Handle an mbuf received on the "lower" hook.
460 ng_gif_rcv_lower(node_p node
, struct mbuf
*m
)
463 const priv_p priv
= NG_NODE_PRIVATE(node
);
465 bzero(&dst
, sizeof(dst
));
467 /* Make sure header is fully pulled up */
468 if (m
->m_pkthdr
.len
< sizeof(sa_family_t
)) {
472 if (m
->m_len
< sizeof(sa_family_t
)
473 && (m
= m_pullup(m
, sizeof(sa_family_t
))) == NULL
) {
477 dst
.sa_family
= *mtod(m
, sa_family_t
*);
478 m_adj(m
, sizeof(sa_family_t
));
480 /* Send it on its way */
482 * XXX: gif_output only uses dst for the family and passes the
483 * fourth argument (rt) to in{,6}_gif_output which ignore it.
484 * If this changes ng_gif will probably break.
486 return gif_output(priv
->ifp
, m
, &dst
, NULL
);
490 * Shutdown node. This resets the node but does not remove it
491 * unless the REALLY_DIE flag is set.
494 ng_gif_shutdown(node_p node
)
496 const priv_p priv
= NG_NODE_PRIVATE(node
);
498 if (node
->nd_flags
& NGF_REALLY_DIE
) {
500 * WE came here because the gif interface is being destroyed,
501 * so stop being persistant.
502 * Actually undo all the things we did on creation.
503 * Assume the ifp has already been freed.
505 NG_NODE_SET_PRIVATE(node
, NULL
);
506 kfree(priv
, M_NETGRAPH
);
507 NG_NODE_UNREF(node
); /* free node itself */
510 NG_NODE_REVIVE(node
); /* Signal ng_rmnode we are persisant */
515 * Hook disconnection.
518 ng_gif_disconnect(hook_p hook
)
520 const priv_p priv
= NG_NODE_PRIVATE(NG_HOOK_NODE(hook
));
522 if (hook
== priv
->lower
) {
524 priv
->lowerOrphan
= 0;
526 panic("%s: weird hook", __func__
);
527 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook
)) == 0)
528 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook
))))
529 ng_rmnode_self(NG_HOOK_NODE(hook
)); /* reset node */
534 /******************************************************************
536 ******************************************************************/
539 * Handle loading and unloading for this node type.
542 ng_gif_mod_event(module_t mod
, int event
, void *data
)
552 /* Register function hooks */
553 if (ng_gif_attach_p
!= NULL
) {
557 ng_gif_attach_p
= ng_gif_attach
;
558 ng_gif_detach_p
= ng_gif_detach
;
559 ng_gif_input_p
= ng_gif_input
;
560 ng_gif_input_orphan_p
= ng_gif_input_orphan
;
562 /* Create nodes for any already-existing gif interfaces */
564 TAILQ_FOREACH(ifp
, &ifnetlist
, if_link
) {
565 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
;