]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netgraph/ng_gif.c
This commit was generated by cvs2svn to compensate for changes in r118611,
[FreeBSD/FreeBSD.git] / sys / netgraph / ng_gif.c
1 /*
2  * ng_gif.c
3  *
4  * Copyright 2001 The Aerospace Corporation.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions, and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of The Aerospace Corporation may not be used to endorse or
16  *    promote products derived from this software.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *
31  * Copyright (c) 1996-2000 Whistle Communications, Inc.
32  * All rights reserved.
33  * 
34  * Subject to the following obligations and disclaimer of warranty, use and
35  * redistribution of this software, in source or object code forms, with or
36  * without modifications are expressly permitted by Whistle Communications;
37  * provided, however, that:
38  * 1. Any and all reproductions of the source or object code must include the
39  *    copyright notice above and the following disclaimer of warranties; and
40  * 2. No rights are granted, in any manner or form, to use Whistle
41  *    Communications, Inc. trademarks, including the mark "WHISTLE
42  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
43  *    such appears in the above copyright notice or in the software.
44  * 
45  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
46  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
47  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
48  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
49  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
50  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
51  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
52  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
53  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
54  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
55  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
56  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
57  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
61  * OF SUCH DAMAGE.
62  *
63  * $FreeBSD$
64  */
65
66 /*
67  * ng_gif(4) netgraph node type
68  */
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/malloc.h>
74 #include <sys/mbuf.h>
75 #include <sys/errno.h>
76 #include <sys/syslog.h>
77 #include <sys/socket.h>
78
79 #include <net/if.h>
80 #include <net/route.h>
81 #include <net/if_types.h>
82 #include <net/if_var.h>
83 #include <net/if_gif.h>
84
85 #include <netgraph/ng_message.h>
86 #include <netgraph/netgraph.h>
87 #include <netgraph/ng_parse.h>
88 #include <netgraph/ng_gif.h>
89
90 #define IFP2NG(ifp)  ((struct ng_node *)((struct gif_softc *)(ifp))->gif_netgraph)
91
92 /* Per-node private data */
93 struct private {
94         struct ifnet    *ifp;           /* associated interface */
95         hook_p          lower;          /* lower OR orphan hook connection */
96         u_char          lowerOrphan;    /* whether lower is lower or orphan */
97 };
98 typedef struct private *priv_p;
99
100 /* Functional hooks called from if_gif.c */
101 static void     ng_gif_input(struct ifnet *ifp, struct mbuf **mp, int af);
102 static void     ng_gif_input_orphan(struct ifnet *ifp, struct mbuf *m, int af);
103 static void     ng_gif_attach(struct ifnet *ifp);
104 static void     ng_gif_detach(struct ifnet *ifp); 
105
106 /* Other functions */
107 static void     ng_gif_input2(node_p node, struct mbuf **mp, int af);
108 static int      ng_gif_glue_af(struct mbuf **mp, int af);
109 static int      ng_gif_rcv_lower(node_p node, struct mbuf *m, meta_p meta);
110
111 /* Netgraph node methods */
112 static ng_constructor_t ng_gif_constructor;
113 static ng_rcvmsg_t      ng_gif_rcvmsg;
114 static ng_shutdown_t    ng_gif_shutdown;
115 static ng_newhook_t     ng_gif_newhook;
116 static ng_connect_t     ng_gif_connect;
117 static ng_rcvdata_t     ng_gif_rcvdata;
118 static ng_disconnect_t  ng_gif_disconnect;
119 static int              ng_gif_mod_event(module_t mod, int event, void *data);
120
121 /* List of commands and how to convert arguments to/from ASCII */
122 static const struct ng_cmdlist ng_gif_cmdlist[] = {
123         {
124           NGM_GIF_COOKIE,
125           NGM_GIF_GET_IFNAME,
126           "getifname",
127           NULL,
128           &ng_parse_string_type
129         },
130         {
131           NGM_GIF_COOKIE,
132           NGM_GIF_GET_IFINDEX,
133           "getifindex",
134           NULL,
135           &ng_parse_int32_type
136         },
137         { 0 }
138 };
139
140 static struct ng_type ng_gif_typestruct = {
141         NG_ABI_VERSION,
142         NG_GIF_NODE_TYPE,
143         ng_gif_mod_event,
144         ng_gif_constructor,
145         ng_gif_rcvmsg,
146         ng_gif_shutdown,
147         ng_gif_newhook,
148         NULL,
149         ng_gif_connect,
150         ng_gif_rcvdata,
151         ng_gif_disconnect,
152         ng_gif_cmdlist,
153 };
154 MODULE_VERSION(ng_gif, 1);
155 MODULE_DEPEND(ng_gif, if_gif, 1,1,1);
156 NETGRAPH_INIT(gif, &ng_gif_typestruct);
157
158 /******************************************************************
159                        GIF FUNCTION HOOKS
160 ******************************************************************/
161
162 /*
163  * Handle a packet that has come in on an interface. We get to
164  * look at it here before any upper layer protocols do.
165  *
166  * NOTE: this function will get called at splimp()
167  */
168 static void
169 ng_gif_input(struct ifnet *ifp, struct mbuf **mp, int af)
170 {
171         const node_p node = IFP2NG(ifp);
172         const priv_p priv = NG_NODE_PRIVATE(node);
173
174         /* If "lower" hook not connected, let packet continue */
175         if (priv->lower == NULL || priv->lowerOrphan)
176                 return;
177         ng_gif_input2(node, mp, af);
178 }
179
180 /*
181  * Handle a packet that has come in on an interface, and which
182  * does not match any of our known protocols (an ``orphan'').
183  *
184  * NOTE: this function will get called at splimp()
185  */
186 static void
187 ng_gif_input_orphan(struct ifnet *ifp, struct mbuf *m, int af)
188 {
189         const node_p node = IFP2NG(ifp);
190         const priv_p priv = NG_NODE_PRIVATE(node);
191
192         /* If "orphan" hook not connected, let packet continue */
193         if (priv->lower == NULL || !priv->lowerOrphan) {
194                 m_freem(m);
195                 return;
196         }
197         ng_gif_input2(node, &m, af);
198         if (m != NULL)
199                 m_freem(m);
200 }
201
202 /*
203  * Handle a packet that has come in on a gif interface.
204  * Attach the address family to the mbuf for later use.
205  *
206  * NOTE: this function will get called at splimp()
207  */
208 static void
209 ng_gif_input2(node_p node, struct mbuf **mp, int af)
210 {
211         const priv_p priv = NG_NODE_PRIVATE(node);
212         int error;
213
214         /* Glue address family on */
215         if ((error = ng_gif_glue_af(mp, af)) != 0)
216                 return;
217
218         /* Send out lower/orphan hook */
219         NG_SEND_DATA_ONLY(error, priv->lower, *mp);
220         *mp = NULL;
221 }
222
223 /*
224  * A new gif interface has been attached.
225  * Create a new node for it, etc.
226  */
227 static void
228 ng_gif_attach(struct ifnet *ifp)
229 {
230         char name[IFNAMSIZ + 1];
231         priv_p priv;
232         node_p node;
233
234         /* Create node */
235         KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
236         snprintf(name, sizeof(name), "%s%d", ifp->if_name, ifp->if_unit);
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", name);
240                 return;
241         }
242
243         /* Allocate private data */
244         MALLOC(priv, priv_p, 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", name);
248                 NG_NODE_UNREF(node);
249                 return;
250         }
251         NG_NODE_SET_PRIVATE(node, priv);
252         priv->ifp = ifp;
253         IFP2NG(ifp) = node;
254
255         /* Try to give the node the same name as the interface */
256         if (ng_name_node(node, name) != 0) {
257                 log(LOG_WARNING, "%s: can't name node %s\n",
258                     __func__, name);
259         }
260 }
261
262 /*
263  * An interface is being detached.
264  * REALLY Destroy its node.
265  */
266 static void
267 ng_gif_detach(struct ifnet *ifp)
268 {
269         const node_p node = IFP2NG(ifp);
270         const priv_p priv = NG_NODE_PRIVATE(node);
271
272         if (node == NULL)               /* no node (why not?), ignore */
273                 return;
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(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  */
289 static int
290 ng_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
319 copy:
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
326 done:
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  */
340 static int
341 ng_gif_constructor(node_p node)
342 {
343         return (EINVAL);
344 }
345
346 /*
347  * Check for attaching a new hook.
348  */
349 static  int
350 ng_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  */
385 static  int
386 ng_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  */
395 static int
396 ng_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 + 1, M_NOWAIT);
409                         if (resp == NULL) {
410                                 error = ENOMEM;
411                                 break;
412                         }
413                         snprintf(resp->data, IFNAMSIZ + 1,
414                             "%s%d", priv->ifp->if_name, priv->ifp->if_unit);
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  */
441 static int
442 ng_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         meta_p meta;
448
449         NGI_GET_M(item, m);
450         NGI_GET_META(item, meta);
451         NG_FREE_ITEM(item);
452         if (hook == priv->lower)
453                 return ng_gif_rcv_lower(node, m, meta);
454         panic("%s: weird hook", __func__);
455 }
456
457 /*
458  * Handle an mbuf received on the "lower" hook.
459  */
460 static int
461 ng_gif_rcv_lower(node_p node, struct mbuf *m, meta_p meta)
462 {
463         struct sockaddr dst;
464         const priv_p priv = NG_NODE_PRIVATE(node);
465
466         bzero(&dst, sizeof(dst));
467
468         /* We don't process metadata. */
469         NG_FREE_META(meta);
470
471         /* Make sure header is fully pulled up */
472         if (m->m_pkthdr.len < sizeof(sa_family_t)) {
473                 NG_FREE_M(m);
474                 return (EINVAL);
475         }
476         if (m->m_len < sizeof(sa_family_t)
477             && (m = m_pullup(m, sizeof(sa_family_t))) == NULL) {
478                 return (ENOBUFS);
479         }
480
481         dst.sa_family = *mtod(m, sa_family_t *);
482         m_adj(m, sizeof(sa_family_t));
483
484         /* Send it on its way */
485         /*
486          * XXX: gif_output only uses dst for the family and passes the
487          * fourth argument (rt) to in{,6}_gif_output which ignore it.
488          * If this changes ng_gif will probably break.
489          */
490         return gif_output(priv->ifp, m, &dst, NULL);
491 }
492
493 /*
494  * Shutdown node. This resets the node but does not remove it
495  * unless the REALLY_DIE flag is set.
496  */
497 static int
498 ng_gif_shutdown(node_p node)
499 {
500         const priv_p priv = NG_NODE_PRIVATE(node);
501
502         if (node->nd_flags & NG_REALLY_DIE) {
503                 /*
504                  * WE came here because the gif interface is being destroyed,
505                  * so stop being persistant.
506                  * Actually undo all the things we did on creation.
507                  * Assume the ifp has already been freed.
508                  */
509                 NG_NODE_SET_PRIVATE(node, NULL);
510                 FREE(priv, M_NETGRAPH);         
511                 NG_NODE_UNREF(node);    /* free node itself */
512                 return (0);
513         }
514         node->nd_flags &= ~NG_INVALID;  /* Signal ng_rmnode we are persisant */
515         return (0);
516 }
517
518 /*
519  * Hook disconnection.
520  */
521 static int
522 ng_gif_disconnect(hook_p hook)
523 {
524         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
525
526         if (hook == priv->lower) {
527                 priv->lower = NULL;
528                 priv->lowerOrphan = 0;
529         } else 
530                 panic("%s: weird hook", __func__);
531         if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
532             && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
533                 ng_rmnode_self(NG_HOOK_NODE(hook));     /* reset node */
534
535         return (0);
536 }
537
538 /******************************************************************
539                         INITIALIZATION
540 ******************************************************************/
541
542 /*
543  * Handle loading and unloading for this node type.
544  */
545 static int
546 ng_gif_mod_event(module_t mod, int event, void *data)
547 {
548         struct ifnet *ifp;
549         int error = 0;
550         int s;
551
552         s = splnet();
553         switch (event) {
554         case MOD_LOAD:
555
556                 /* Register function hooks */
557                 if (ng_gif_attach_p != NULL) {
558                         error = EEXIST;
559                         break;
560                 }
561                 ng_gif_attach_p = ng_gif_attach;
562                 ng_gif_detach_p = ng_gif_detach;
563                 ng_gif_input_p = ng_gif_input;
564                 ng_gif_input_orphan_p = ng_gif_input_orphan;
565
566                 /* Create nodes for any already-existing gif interfaces */
567                 IFNET_RLOCK();
568                 TAILQ_FOREACH(ifp, &ifnet, if_link) {
569                         if (ifp->if_type == IFT_GIF)
570                                 ng_gif_attach(ifp);
571                 }
572                 IFNET_RUNLOCK();
573                 break;
574
575         case MOD_UNLOAD:
576
577                 /*
578                  * Note that the base code won't try to unload us until
579                  * all nodes have been removed, and that can't happen
580                  * until all gif interfaces are destroyed. In any
581                  * case, we know there are no nodes left if the action
582                  * is MOD_UNLOAD, so there's no need to detach any nodes.
583                  *
584                  * XXX: what about manual unloads?!?
585                  */
586
587                 /* Unregister function hooks */
588                 ng_gif_attach_p = NULL;
589                 ng_gif_detach_p = NULL;
590                 ng_gif_input_p = NULL;
591                 ng_gif_input_orphan_p = NULL;
592                 break;
593
594         default:
595                 error = EOPNOTSUPP;
596                 break;
597         }
598         splx(s);
599         return (error);
600 }
601