Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / netgraph / ether / ng_ether.c
1
2 /*
3  * ng_ether.c
4  *
5  * Copyright (c) 1996-2000 Whistle Communications, Inc.
6  * All rights reserved.
7  * 
8  * Subject to the following obligations and disclaimer of warranty, use and
9  * redistribution of this software, in source or object code forms, with or
10  * without modifications are expressly permitted by Whistle Communications;
11  * provided, however, that:
12  * 1. Any and all reproductions of the source or object code must include the
13  *    copyright notice above and the following disclaimer of warranties; and
14  * 2. No rights are granted, in any manner or form, to use Whistle
15  *    Communications, Inc. trademarks, including the mark "WHISTLE
16  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17  *    such appears in the above copyright notice or in the software.
18  * 
19  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35  * OF SUCH DAMAGE.
36  *
37  * Authors: Archie Cobbs <archie@freebsd.org>
38  *          Julian Elischer <julian@freebsd.org>
39  *
40  * $FreeBSD: src/sys/netgraph/ng_ether.c,v 1.2.2.13 2002/07/02 20:10:25 archie Exp $
41  * $DragonFly: src/sys/netgraph/ether/ng_ether.c,v 1.2 2003/06/17 04:28:49 dillon Exp $
42  */
43
44 /*
45  * ng_ether(4) netgraph node type
46  */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/errno.h>
54 #include <sys/syslog.h>
55 #include <sys/socket.h>
56
57 #include <net/if.h>
58 #include <net/if_types.h>
59 #include <net/if_arp.h>
60 #include <net/if_var.h>
61 #include <net/ethernet.h>
62
63 #include <netgraph/ng_message.h>
64 #include <netgraph/netgraph.h>
65 #include <netgraph/ng_parse.h>
66 #include <netgraph/ng_ether.h>
67
68 #define IFP2AC(IFP)  ((struct arpcom *)IFP)
69 #define IFP2NG(ifp)  ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph)
70
71 /* Per-node private data */
72 struct private {
73         struct ifnet    *ifp;           /* associated interface */
74         hook_p          upper;          /* upper hook connection */
75         hook_p          lower;          /* lower OR orphan hook connection */
76         u_char          lowerOrphan;    /* whether lower is lower or orphan */
77         u_char          autoSrcAddr;    /* always overwrite source address */
78         u_char          promisc;        /* promiscuous mode enabled */
79         u_long          hwassist;       /* hardware checksum capabilities */
80 };
81 typedef struct private *priv_p;
82
83 /* Functional hooks called from if_ethersubr.c */
84 static void     ng_ether_input(struct ifnet *ifp,
85                     struct mbuf **mp, struct ether_header *eh);
86 static void     ng_ether_input_orphan(struct ifnet *ifp,
87                     struct mbuf *m, struct ether_header *eh);
88 static int      ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
89 static void     ng_ether_attach(struct ifnet *ifp);
90 static void     ng_ether_detach(struct ifnet *ifp); 
91
92 /* Other functions */
93 static void     ng_ether_input2(node_p node,
94                     struct mbuf **mp, struct ether_header *eh);
95 static int      ng_ether_glueback_header(struct mbuf **mp,
96                         struct ether_header *eh);
97 static int      ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta);
98 static int      ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta);
99
100 /* Netgraph node methods */
101 static ng_constructor_t ng_ether_constructor;
102 static ng_rcvmsg_t      ng_ether_rcvmsg;
103 static ng_shutdown_t    ng_ether_rmnode;
104 static ng_newhook_t     ng_ether_newhook;
105 static ng_rcvdata_t     ng_ether_rcvdata;
106 static ng_disconnect_t  ng_ether_disconnect;
107 static int              ng_ether_mod_event(module_t mod, int event, void *data);
108
109 /* Parse type for an Ethernet address */
110 static ng_parse_t       ng_enaddr_parse;
111 static ng_unparse_t     ng_enaddr_unparse;
112 const struct ng_parse_type ng_ether_enaddr_type = {
113         NULL,
114         NULL,
115         NULL,
116         ng_enaddr_parse,
117         ng_enaddr_unparse,
118         NULL,                   /* no such thing as a "default" EN address */
119         0
120 };
121
122 /* List of commands and how to convert arguments to/from ASCII */
123 static const struct ng_cmdlist ng_ether_cmdlist[] = {
124         {
125           NGM_ETHER_COOKIE,
126           NGM_ETHER_GET_IFNAME,
127           "getifname",
128           NULL,
129           &ng_parse_string_type
130         },
131         {
132           NGM_ETHER_COOKIE,
133           NGM_ETHER_GET_IFINDEX,
134           "getifindex",
135           NULL,
136           &ng_parse_int32_type
137         },
138         {
139           NGM_ETHER_COOKIE,
140           NGM_ETHER_GET_ENADDR,
141           "getenaddr",
142           NULL,
143           &ng_ether_enaddr_type
144         },
145         {
146           NGM_ETHER_COOKIE,
147           NGM_ETHER_SET_ENADDR,
148           "setenaddr",
149           &ng_ether_enaddr_type,
150           NULL
151         },
152         {
153           NGM_ETHER_COOKIE,
154           NGM_ETHER_GET_PROMISC,
155           "getpromisc",
156           NULL,
157           &ng_parse_int32_type
158         },
159         {
160           NGM_ETHER_COOKIE,
161           NGM_ETHER_SET_PROMISC,
162           "setpromisc",
163           &ng_parse_int32_type,
164           NULL
165         },
166         {
167           NGM_ETHER_COOKIE,
168           NGM_ETHER_GET_AUTOSRC,
169           "getautosrc",
170           NULL,
171           &ng_parse_int32_type
172         },
173         {
174           NGM_ETHER_COOKIE,
175           NGM_ETHER_SET_AUTOSRC,
176           "setautosrc",
177           &ng_parse_int32_type,
178           NULL
179         },
180         { 0 }
181 };
182
183 static struct ng_type ng_ether_typestruct = {
184         NG_VERSION,
185         NG_ETHER_NODE_TYPE,
186         ng_ether_mod_event,
187         ng_ether_constructor,
188         ng_ether_rcvmsg,
189         ng_ether_rmnode,
190         ng_ether_newhook,
191         NULL,
192         NULL,
193         ng_ether_rcvdata,
194         ng_ether_rcvdata,
195         ng_ether_disconnect,
196         ng_ether_cmdlist,
197 };
198 NETGRAPH_INIT(ether, &ng_ether_typestruct);
199
200 /******************************************************************
201                     ETHERNET FUNCTION HOOKS
202 ******************************************************************/
203
204 /*
205  * Handle a packet that has come in on an interface. We get to
206  * look at it here before any upper layer protocols do.
207  *
208  * NOTE: this function will get called at splimp()
209  */
210 static void
211 ng_ether_input(struct ifnet *ifp,
212         struct mbuf **mp, struct ether_header *eh)
213 {
214         const node_p node = IFP2NG(ifp);
215         const priv_p priv = node->private;
216
217         /* If "lower" hook not connected, let packet continue */
218         if (priv->lower == NULL || priv->lowerOrphan)
219                 return;
220         ng_ether_input2(node, mp, eh);
221 }
222
223 /*
224  * Handle a packet that has come in on an interface, and which
225  * does not match any of our known protocols (an ``orphan'').
226  *
227  * NOTE: this function will get called at splimp()
228  */
229 static void
230 ng_ether_input_orphan(struct ifnet *ifp,
231         struct mbuf *m, struct ether_header *eh)
232 {
233         const node_p node = IFP2NG(ifp);
234         const priv_p priv = node->private;
235
236         /* If "orphan" hook not connected, let packet continue */
237         if (priv->lower == NULL || !priv->lowerOrphan) {
238                 m_freem(m);
239                 return;
240         }
241         ng_ether_input2(node, &m, eh);
242         if (m != NULL)
243                 m_freem(m);
244 }
245
246 /*
247  * Handle a packet that has come in on an interface.
248  * The Ethernet header has already been detached from the mbuf,
249  * so we have to put it back.
250  *
251  * NOTE: this function will get called at splimp()
252  */
253 static void
254 ng_ether_input2(node_p node, struct mbuf **mp, struct ether_header *eh)
255 {
256         const priv_p priv = node->private;
257         meta_p meta = NULL;
258         int error;
259
260         /* Glue Ethernet header back on */
261         if ((error = ng_ether_glueback_header(mp, eh)) != 0)
262                 return;
263
264         /* Send out lower/orphan hook */
265         (void)ng_queue_data(priv->lower, *mp, meta);
266         *mp = NULL;
267 }
268
269 /*
270  * Handle a packet that is going out on an interface.
271  * The Ethernet header is already attached to the mbuf.
272  */
273 static int
274 ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
275 {
276         const node_p node = IFP2NG(ifp);
277         const priv_p priv = node->private;
278         meta_p meta = NULL;
279         int error = 0;
280
281         /* If "upper" hook not connected, let packet continue */
282         if (priv->upper == NULL)
283                 return (0);
284
285         /* Send it out "upper" hook */
286         NG_SEND_DATA(error, priv->upper, *mp, meta);
287         *mp = NULL;
288         return (error);
289 }
290
291 /*
292  * A new Ethernet interface has been attached.
293  * Create a new node for it, etc.
294  */
295 static void
296 ng_ether_attach(struct ifnet *ifp)
297 {
298         char name[IFNAMSIZ + 1];
299         priv_p priv;
300         node_p node;
301
302         /* Create node */
303         KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __FUNCTION__));
304         snprintf(name, sizeof(name), "%s%d", ifp->if_name, ifp->if_unit);
305         if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
306                 log(LOG_ERR, "%s: can't %s for %s\n",
307                     __FUNCTION__, "create node", name);
308                 return;
309         }
310
311         /* Allocate private data */
312         MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT);
313         if (priv == NULL) {
314                 log(LOG_ERR, "%s: can't %s for %s\n",
315                     __FUNCTION__, "allocate memory", name);
316                 ng_unref(node);
317                 return;
318         }
319         bzero(priv, sizeof(*priv));
320         node->private = priv;
321         priv->ifp = ifp;
322         IFP2NG(ifp) = node;
323         priv->autoSrcAddr = 1;
324         priv->hwassist = ifp->if_hwassist;
325
326         /* Try to give the node the same name as the interface */
327         if (ng_name_node(node, name) != 0) {
328                 log(LOG_WARNING, "%s: can't name node %s\n",
329                     __FUNCTION__, name);
330         }
331 }
332
333 /*
334  * An Ethernet interface is being detached.
335  * Destroy its node.
336  */
337 static void
338 ng_ether_detach(struct ifnet *ifp)
339 {
340         const node_p node = IFP2NG(ifp);
341         priv_p priv;
342
343         if (node == NULL)               /* no node (why not?), ignore */
344                 return;
345         ng_rmnode(node);                /* break all links to other nodes */
346         node->flags |= NG_INVALID;
347         ng_unname(node);                /* free name (and its reference) */
348         IFP2NG(ifp) = NULL;             /* detach node from interface */
349         priv = node->private;           /* free node private info */
350         bzero(priv, sizeof(*priv));
351         FREE(priv, M_NETGRAPH);
352         node->private = NULL;
353         ng_unref(node);                 /* free node itself */
354 }
355
356 /*
357  * Optimization for gluing the Ethernet header back onto
358  * the front of an incoming packet.
359  */
360 static int
361 ng_ether_glueback_header(struct mbuf **mp, struct ether_header *eh)
362 {
363         struct mbuf *m = *mp;
364         int error = 0;
365
366         /*
367          * Optimize for the case where the header is already in place
368          * at the front of the mbuf. This is actually quite likely
369          * because many Ethernet drivers generate packets this way.
370          */
371         if (eh == mtod(m, struct ether_header *) - 1) {
372                 m->m_len += sizeof(*eh);
373                 m->m_data -= sizeof(*eh);
374                 m->m_pkthdr.len += sizeof(*eh);
375                 goto done;
376         }
377
378         /* Prepend the header back onto the front of the mbuf */
379         M_PREPEND(m, sizeof(*eh), M_DONTWAIT);
380         if (m == NULL) {
381                 error = ENOBUFS;
382                 goto done;
383         }
384
385         /* Copy header into front of mbuf */
386         bcopy(eh, mtod(m, void *), sizeof(*eh));
387
388 done:
389         /* Done */
390         *mp = m;
391         return error;
392 }
393
394 /******************************************************************
395                     NETGRAPH NODE METHODS
396 ******************************************************************/
397
398 /*
399  * It is not possible or allowable to create a node of this type.
400  * Nodes get created when the interface is attached (or, when
401  * this node type's KLD is loaded).
402  */
403 static int
404 ng_ether_constructor(node_p *nodep)
405 {
406         return (EINVAL);
407 }
408
409 /*
410  * Check for attaching a new hook.
411  */
412 static  int
413 ng_ether_newhook(node_p node, hook_p hook, const char *name)
414 {
415         const priv_p priv = node->private;
416         u_char orphan = priv->lowerOrphan;
417         hook_p *hookptr;
418
419         /* Divert hook is an alias for lower */
420         if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
421                 name = NG_ETHER_HOOK_LOWER;
422
423         /* Which hook? */
424         if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
425                 hookptr = &priv->upper;
426         else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
427                 hookptr = &priv->lower;
428                 orphan = 0;
429         } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
430                 hookptr = &priv->lower;
431                 orphan = 1;
432         } else
433                 return (EINVAL);
434
435         /* Check if already connected (shouldn't be, but doesn't hurt) */
436         if (*hookptr != NULL)
437                 return (EISCONN);
438
439         /* Disable hardware checksums while 'upper' hook is connected */
440         if (hookptr == &priv->upper)
441                 priv->ifp->if_hwassist = 0;
442
443         /* OK */
444         *hookptr = hook;
445         priv->lowerOrphan = orphan;
446         return (0);
447 }
448
449 /*
450  * Receive an incoming control message.
451  */
452 static int
453 ng_ether_rcvmsg(node_p node, struct ng_mesg *msg,
454         const char *retaddr, struct ng_mesg **rptr)
455 {
456         const priv_p priv = node->private;
457         struct ng_mesg *resp = NULL;
458         int error = 0;
459
460         switch (msg->header.typecookie) {
461         case NGM_ETHER_COOKIE:
462                 switch (msg->header.cmd) {
463                 case NGM_ETHER_GET_IFNAME:
464                         NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT);
465                         if (resp == NULL) {
466                                 error = ENOMEM;
467                                 break;
468                         }
469                         snprintf(resp->data, IFNAMSIZ + 1,
470                             "%s%d", priv->ifp->if_name, priv->ifp->if_unit);
471                         break;
472                 case NGM_ETHER_GET_IFINDEX:
473                         NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
474                         if (resp == NULL) {
475                                 error = ENOMEM;
476                                 break;
477                         }
478                         *((u_int32_t *)resp->data) = priv->ifp->if_index;
479                         break;
480                 case NGM_ETHER_GET_ENADDR:
481                         NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
482                         if (resp == NULL) {
483                                 error = ENOMEM;
484                                 break;
485                         }
486                         bcopy((IFP2AC(priv->ifp))->ac_enaddr,
487                             resp->data, ETHER_ADDR_LEN);
488                         break;
489                 case NGM_ETHER_SET_ENADDR:
490                     {
491                         if (msg->header.arglen != ETHER_ADDR_LEN) {
492                                 error = EINVAL;
493                                 break;
494                         }
495                         error = if_setlladdr(priv->ifp,
496                             (u_char *)msg->data, ETHER_ADDR_LEN);
497                         break;
498                     }
499                 case NGM_ETHER_GET_PROMISC:
500                         NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
501                         if (resp == NULL) {
502                                 error = ENOMEM;
503                                 break;
504                         }
505                         *((u_int32_t *)resp->data) = priv->promisc;
506                         break;
507                 case NGM_ETHER_SET_PROMISC:
508                     {
509                         u_char want;
510
511                         if (msg->header.arglen != sizeof(u_int32_t)) {
512                                 error = EINVAL;
513                                 break;
514                         }
515                         want = !!*((u_int32_t *)msg->data);
516                         if (want ^ priv->promisc) {
517                                 if ((error = ifpromisc(priv->ifp, want)) != 0)
518                                         break;
519                                 priv->promisc = want;
520                         }
521                         break;
522                     }
523                 case NGM_ETHER_GET_AUTOSRC:
524                         NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
525                         if (resp == NULL) {
526                                 error = ENOMEM;
527                                 break;
528                         }
529                         *((u_int32_t *)resp->data) = priv->autoSrcAddr;
530                         break;
531                 case NGM_ETHER_SET_AUTOSRC:
532                         if (msg->header.arglen != sizeof(u_int32_t)) {
533                                 error = EINVAL;
534                                 break;
535                         }
536                         priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
537                         break;
538                 default:
539                         error = EINVAL;
540                         break;
541                 }
542                 break;
543         default:
544                 error = EINVAL;
545                 break;
546         }
547         if (rptr)
548                 *rptr = resp;
549         else if (resp != NULL)
550                 FREE(resp, M_NETGRAPH);
551         FREE(msg, M_NETGRAPH);
552         return (error);
553 }
554
555 /*
556  * Receive data on a hook.
557  */
558 static int
559 ng_ether_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
560 {
561         const node_p node = hook->node;
562         const priv_p priv = node->private;
563
564         if (hook == priv->lower)
565                 return ng_ether_rcv_lower(node, m, meta);
566         if (hook == priv->upper)
567                 return ng_ether_rcv_upper(node, m, meta);
568         panic("%s: weird hook", __FUNCTION__);
569 }
570
571 /*
572  * Handle an mbuf received on the "lower" hook.
573  */
574 static int
575 ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta)
576 {
577         const priv_p priv = node->private;
578         struct ifnet *const ifp = priv->ifp;
579
580         /* Discard meta info */
581         NG_FREE_META(meta);
582
583         /* Check whether interface is ready for packets */
584         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
585                 m_freem(m);
586                 return (ENETDOWN);
587         }
588
589         /* Make sure header is fully pulled up */
590         if (m->m_pkthdr.len < sizeof(struct ether_header)) {
591                 m_freem(m);
592                 return (EINVAL);
593         }
594         if (m->m_len < sizeof(struct ether_header)
595             && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
596                 return (ENOBUFS);
597
598         /* Drop in the MAC address if desired */
599         if (priv->autoSrcAddr) {
600
601                 /* Make the mbuf writable if it's not already */
602                 if (!M_WRITABLE(m)
603                     && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
604                         return (ENOBUFS);
605
606                 /* Overwrite source MAC address */
607                 bcopy((IFP2AC(ifp))->ac_enaddr,
608                     mtod(m, struct ether_header *)->ether_shost,
609                     ETHER_ADDR_LEN);
610         }
611
612         /* Send it on its way */
613         return ether_output_frame(ifp, m);
614 }
615
616 /*
617  * Handle an mbuf received on the "upper" hook.
618  */
619 static int
620 ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta)
621 {
622         const priv_p priv = node->private;
623         struct ether_header *eh;
624
625         /* Discard meta info */
626         NG_FREE_META(meta);
627
628         /* Check length and pull off header */
629         if (m->m_pkthdr.len < sizeof(*eh)) {
630                 m_freem(m);
631                 return (EINVAL);
632         }
633         if (m->m_len < sizeof(*eh) && (m = m_pullup(m, sizeof(*eh))) == NULL)
634                 return (ENOBUFS);
635         eh = mtod(m, struct ether_header *);
636         m->m_data += sizeof(*eh);
637         m->m_len -= sizeof(*eh);
638         m->m_pkthdr.len -= sizeof(*eh);
639         m->m_pkthdr.rcvif = priv->ifp;
640
641         /* Route packet back in */
642         ether_demux(priv->ifp, eh, m);
643         return (0);
644 }
645
646 /*
647  * Shutdown node. This resets the node but does not remove it.
648  */
649 static int
650 ng_ether_rmnode(node_p node)
651 {
652         const priv_p priv = node->private;
653
654         ng_cutlinks(node);
655         node->flags &= ~NG_INVALID;     /* bounce back to life */
656         if (priv->promisc) {            /* disable promiscuous mode */
657                 (void)ifpromisc(priv->ifp, 0);
658                 priv->promisc = 0;
659         }
660         priv->autoSrcAddr = 1;          /* reset auto-src-addr flag */
661         return (0);
662 }
663
664 /*
665  * Hook disconnection.
666  */
667 static int
668 ng_ether_disconnect(hook_p hook)
669 {
670         const priv_p priv = hook->node->private;
671
672         if (hook == priv->upper) {
673                 priv->upper = NULL;
674                 priv->ifp->if_hwassist = priv->hwassist;  /* restore h/w csum */
675         } else if (hook == priv->lower) {
676                 priv->lower = NULL;
677                 priv->lowerOrphan = 0;
678         } else
679                 panic("%s: weird hook", __FUNCTION__);
680         if (hook->node->numhooks == 0)
681                 ng_rmnode(hook->node);  /* reset node */
682         return (0);
683 }
684
685 static int
686 ng_enaddr_parse(const struct ng_parse_type *type,
687         const char *s, int *const off, const u_char *const start,
688         u_char *const buf, int *const buflen)
689 {
690         char *eptr;
691         u_long val;
692         int i;
693
694         if (*buflen < ETHER_ADDR_LEN)
695                 return (ERANGE);
696         for (i = 0; i < ETHER_ADDR_LEN; i++) {
697                 val = strtoul(s + *off, &eptr, 16);
698                 if (val > 0xff || eptr == s + *off)
699                         return (EINVAL);
700                 buf[i] = (u_char)val;
701                 *off = (eptr - s);
702                 if (i < ETHER_ADDR_LEN - 1) {
703                         if (*eptr != ':')
704                                 return (EINVAL);
705                         (*off)++;
706                 }
707         }
708         *buflen = ETHER_ADDR_LEN;
709         return (0);
710 }
711
712 static int
713 ng_enaddr_unparse(const struct ng_parse_type *type,
714         const u_char *data, int *off, char *cbuf, int cbuflen)
715 {
716         int len;
717
718         len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
719             data[*off], data[*off + 1], data[*off + 2],
720             data[*off + 3], data[*off + 4], data[*off + 5]);
721         if (len >= cbuflen)
722                 return (ERANGE);
723         *off += ETHER_ADDR_LEN;
724         return (0);
725 }
726
727 /******************************************************************
728                         INITIALIZATION
729 ******************************************************************/
730
731 /*
732  * Handle loading and unloading for this node type.
733  */
734 static int
735 ng_ether_mod_event(module_t mod, int event, void *data)
736 {
737         struct ifnet *ifp;
738         int error = 0;
739         int s;
740
741         s = splnet();
742         switch (event) {
743         case MOD_LOAD:
744
745                 /* Register function hooks */
746                 if (ng_ether_attach_p != NULL) {
747                         error = EEXIST;
748                         break;
749                 }
750                 ng_ether_attach_p = ng_ether_attach;
751                 ng_ether_detach_p = ng_ether_detach;
752                 ng_ether_output_p = ng_ether_output;
753                 ng_ether_input_p = ng_ether_input;
754                 ng_ether_input_orphan_p = ng_ether_input_orphan;
755
756                 /* Create nodes for any already-existing Ethernet interfaces */
757                 TAILQ_FOREACH(ifp, &ifnet, if_link) {
758                         if (ifp->if_type == IFT_ETHER ||
759                             ifp->if_type == IFT_L2VLAN)
760                                 ng_ether_attach(ifp);
761                 }
762                 break;
763
764         case MOD_UNLOAD:
765
766                 /*
767                  * Note that the base code won't try to unload us until
768                  * all nodes have been removed, and that can't happen
769                  * until all Ethernet interfaces are removed. In any
770                  * case, we know there are no nodes left if the action
771                  * is MOD_UNLOAD, so there's no need to detach any nodes.
772                  */
773
774                 /* Unregister function hooks */
775                 ng_ether_attach_p = NULL;
776                 ng_ether_detach_p = NULL;
777                 ng_ether_output_p = NULL;
778                 ng_ether_input_p = NULL;
779                 ng_ether_input_orphan_p = NULL;
780                 break;
781
782         default:
783                 error = EOPNOTSUPP;
784                 break;
785         }
786         splx(s);
787         return (error);
788 }
789