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