- ifnet.if_output() should be called without ifnet.if_serializer being
[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.16 2008/05/28 12:11:13 sephe 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 #include <sys/thread2.h>
57
58 #include <net/if.h>
59 #include <net/if_types.h>
60 #include <net/if_arp.h>
61 #include <net/if_var.h>
62 #include <net/ethernet.h>
63
64 #include <netgraph/ng_message.h>
65 #include <netgraph/netgraph.h>
66 #include <netgraph/ng_parse.h>
67 #include "ng_ether.h"
68
69 #define IFP2AC(IFP)  ((struct arpcom *)IFP)
70 #define IFP2NG(ifp)  (IFP2AC((ifp))->ac_netgraph)
71
72 /* Per-node private data */
73 struct private {
74         struct ifnet    *ifp;           /* associated interface */
75         hook_p          upper;          /* upper hook connection */
76         hook_p          lower;          /* lower OR orphan hook connection */
77         u_char          lowerOrphan;    /* whether lower is lower or orphan */
78         u_char          autoSrcAddr;    /* always overwrite source address */
79         u_char          promisc;        /* promiscuous mode enabled */
80         u_long          hwassist;       /* hardware checksum capabilities */
81 };
82 typedef struct private *priv_p;
83
84 /* Functional hooks called from if_ethersubr.c */
85 static void     ng_ether_input(struct ifnet *ifp, struct mbuf **mp);
86 static void     ng_ether_input_orphan(struct ifnet *ifp,
87                     struct mbuf *m, const 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, const struct ether_header *eh);
95 static int      ng_ether_glueback_header(struct mbuf **mp,
96                         const 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 with ifp's serializer being held
209  */
210 static void
211 ng_ether_input(struct ifnet *ifp, struct mbuf **mp)
212 {
213         const node_p node = IFP2NG(ifp);
214         const priv_p priv = node->private;
215
216         /* If "lower" hook not connected, let packet continue */
217         if (priv->lower == NULL || priv->lowerOrphan)
218                 return;
219         ng_ether_input2(node, mp, NULL);
220 }
221
222 /*
223  * Handle a packet that has come in on an interface, and which
224  * does not match any of our known protocols (an ``orphan'').
225  *
226  * NOTE: this function will get called at splimp()
227  */
228 static void
229 ng_ether_input_orphan(struct ifnet *ifp,
230         struct mbuf *m, const struct ether_header *eh)
231 {
232         const node_p node = IFP2NG(ifp);
233         const priv_p priv = node->private;
234
235         /* If "orphan" hook not connected, let packet continue */
236         if (priv->lower == NULL || !priv->lowerOrphan) {
237                 m_freem(m);
238                 return;
239         }
240         ng_ether_input2(node, &m, eh);
241         if (m != NULL)
242                 m_freem(m);
243 }
244
245 /*
246  * Handle a packet that has come in on an interface.
247  * The Ethernet header has already been detached from the mbuf,
248  * so we have to put it back.
249  *
250  * NOTE: this function will get called at splimp()
251  */
252 static void
253 ng_ether_input2(node_p node, struct mbuf **mp, const struct ether_header *eh)
254 {
255         const priv_p priv = node->private;
256         meta_p meta = NULL;
257         int error;
258
259         /* Glue Ethernet header back on */
260         if (eh != NULL && (error = ng_ether_glueback_header(mp, eh)) != 0)
261                 return;
262
263         /* Send out lower/orphan hook */
264         ng_queue_data(priv->lower, *mp, meta);
265         *mp = NULL;
266 }
267
268 /*
269  * Handle a packet that is going out on an interface.
270  * The Ethernet header is already attached to the mbuf.
271  */
272 static int
273 ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
274 {
275         const node_p node = IFP2NG(ifp);
276         const priv_p priv = node->private;
277         meta_p meta = NULL;
278         int error = 0;
279
280         /* If "upper" hook not connected, let packet continue */
281         if (priv->upper == NULL)
282                 return (0);
283
284         /* Send it out "upper" hook */
285         NG_SEND_DATA(error, priv->upper, *mp, meta);
286         *mp = NULL;
287         return (error);
288 }
289
290 /*
291  * A new Ethernet interface has been attached.
292  * Create a new node for it, etc.
293  */
294 static void
295 ng_ether_attach(struct ifnet *ifp)
296 {
297         char name[IFNAMSIZ + 1];
298         priv_p priv;
299         node_p node;
300
301         /* Create node */
302         KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
303         strlcpy(name, ifp->if_xname, sizeof(name));
304         if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
305                 log(LOG_ERR, "%s: can't %s for %s\n",
306                     __func__, "create node", name);
307                 return;
308         }
309
310         /* Allocate private data */
311         MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
312         if (priv == NULL) {
313                 log(LOG_ERR, "%s: can't %s for %s\n",
314                     __func__, "allocate memory", name);
315                 ng_unref(node);
316                 return;
317         }
318         node->private = priv;
319         priv->ifp = ifp;
320         IFP2NG(ifp) = node;
321         priv->autoSrcAddr = 1;
322         priv->hwassist = ifp->if_hwassist;
323
324         /* Try to give the node the same name as the interface */
325         if (ng_name_node(node, name) != 0) {
326                 log(LOG_WARNING, "%s: can't name node %s\n",
327                     __func__, name);
328         }
329 }
330
331 /*
332  * An Ethernet interface is being detached.
333  * Destroy its node.
334  */
335 static void
336 ng_ether_detach(struct ifnet *ifp)
337 {
338         const node_p node = IFP2NG(ifp);
339         priv_p priv;
340
341         if (node == NULL)               /* no node (why not?), ignore */
342                 return;
343         ng_rmnode(node);                /* break all links to other nodes */
344         node->flags |= NG_INVALID;
345         ng_unname(node);                /* free name (and its reference) */
346         IFP2NG(ifp) = NULL;             /* detach node from interface */
347         priv = node->private;           /* free node private info */
348         bzero(priv, sizeof(*priv));
349         FREE(priv, M_NETGRAPH);
350         node->private = NULL;
351         ng_unref(node);                 /* free node itself */
352 }
353
354 /*
355  * Optimization for gluing the Ethernet header back onto
356  * the front of an incoming packet.
357  */
358 static int
359 ng_ether_glueback_header(struct mbuf **mp, const struct ether_header *eh)
360 {
361         struct mbuf *m = *mp;
362         int error = 0;
363
364         /*
365          * Optimize for the case where the header is already in place
366          * at the front of the mbuf. This is actually quite likely
367          * because many Ethernet drivers generate packets this way.
368          */
369         if (eh == mtod(m, struct ether_header *) - 1) {
370                 m->m_len += sizeof(*eh);
371                 m->m_data -= sizeof(*eh);
372                 m->m_pkthdr.len += sizeof(*eh);
373                 goto done;
374         }
375
376         /* Prepend the header back onto the front of the mbuf */
377         M_PREPEND(m, sizeof(*eh), MB_DONTWAIT);
378         if (m == NULL) {
379                 error = ENOBUFS;
380                 goto done;
381         }
382
383         /* Copy header into front of mbuf */
384         bcopy(eh, mtod(m, void *), sizeof(*eh));
385
386 done:
387         /* Done */
388         *mp = m;
389         return error;
390 }
391
392 /******************************************************************
393                     NETGRAPH NODE METHODS
394 ******************************************************************/
395
396 /*
397  * It is not possible or allowable to create a node of this type.
398  * Nodes get created when the interface is attached (or, when
399  * this node type's KLD is loaded).
400  */
401 static int
402 ng_ether_constructor(node_p *nodep)
403 {
404         return (EINVAL);
405 }
406
407 /*
408  * Check for attaching a new hook.
409  */
410 static  int
411 ng_ether_newhook(node_p node, hook_p hook, const char *name)
412 {
413         const priv_p priv = node->private;
414         u_char orphan = priv->lowerOrphan;
415         hook_p *hookptr;
416
417         /* Divert hook is an alias for lower */
418         if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
419                 name = NG_ETHER_HOOK_LOWER;
420
421         /* Which hook? */
422         if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
423                 hookptr = &priv->upper;
424         else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
425                 hookptr = &priv->lower;
426                 orphan = 0;
427         } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
428                 hookptr = &priv->lower;
429                 orphan = 1;
430         } else
431                 return (EINVAL);
432
433         /* Check if already connected (shouldn't be, but doesn't hurt) */
434         if (*hookptr != NULL)
435                 return (EISCONN);
436
437         /* Disable hardware checksums while 'upper' hook is connected */
438         if (hookptr == &priv->upper)
439                 priv->ifp->if_hwassist = 0;
440
441         /* OK */
442         *hookptr = hook;
443         priv->lowerOrphan = orphan;
444         return (0);
445 }
446
447 /*
448  * Receive an incoming control message.
449  */
450 static int
451 ng_ether_rcvmsg(node_p node, struct ng_mesg *msg,
452         const char *retaddr, struct ng_mesg **rptr)
453 {
454         const priv_p priv = node->private;
455         struct ng_mesg *resp = NULL;
456         int error = 0;
457
458         switch (msg->header.typecookie) {
459         case NGM_ETHER_COOKIE:
460                 switch (msg->header.cmd) {
461                 case NGM_ETHER_GET_IFNAME:
462                         NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT);
463                         if (resp == NULL) {
464                                 error = ENOMEM;
465                                 break;
466                         }
467                         strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
468                         break;
469                 case NGM_ETHER_GET_IFINDEX:
470                         NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
471                         if (resp == NULL) {
472                                 error = ENOMEM;
473                                 break;
474                         }
475                         *((u_int32_t *)resp->data) = priv->ifp->if_index;
476                         break;
477                 case NGM_ETHER_GET_ENADDR:
478                         NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
479                         if (resp == NULL) {
480                                 error = ENOMEM;
481                                 break;
482                         }
483                         bcopy((IFP2AC(priv->ifp))->ac_enaddr,
484                             resp->data, ETHER_ADDR_LEN);
485                         break;
486                 case NGM_ETHER_SET_ENADDR:
487                     {
488                         if (msg->header.arglen != ETHER_ADDR_LEN) {
489                                 error = EINVAL;
490                                 break;
491                         }
492                         error = if_setlladdr(priv->ifp,
493                             (u_char *)msg->data, ETHER_ADDR_LEN);
494                         break;
495                     }
496                 case NGM_ETHER_GET_PROMISC:
497                         NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
498                         if (resp == NULL) {
499                                 error = ENOMEM;
500                                 break;
501                         }
502                         *((u_int32_t *)resp->data) = priv->promisc;
503                         break;
504                 case NGM_ETHER_SET_PROMISC:
505                     {
506                         u_char want;
507
508                         if (msg->header.arglen != sizeof(u_int32_t)) {
509                                 error = EINVAL;
510                                 break;
511                         }
512                         want = !!*((u_int32_t *)msg->data);
513                         if (want ^ priv->promisc) {
514                                 if ((error = ifpromisc(priv->ifp, want)) != 0)
515                                         break;
516                                 priv->promisc = want;
517                         }
518                         break;
519                     }
520                 case NGM_ETHER_GET_AUTOSRC:
521                         NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
522                         if (resp == NULL) {
523                                 error = ENOMEM;
524                                 break;
525                         }
526                         *((u_int32_t *)resp->data) = priv->autoSrcAddr;
527                         break;
528                 case NGM_ETHER_SET_AUTOSRC:
529                         if (msg->header.arglen != sizeof(u_int32_t)) {
530                                 error = EINVAL;
531                                 break;
532                         }
533                         priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
534                         break;
535                 default:
536                         error = EINVAL;
537                         break;
538                 }
539                 break;
540         default:
541                 error = EINVAL;
542                 break;
543         }
544         if (rptr)
545                 *rptr = resp;
546         else if (resp != NULL)
547                 FREE(resp, M_NETGRAPH);
548         FREE(msg, M_NETGRAPH);
549         return (error);
550 }
551
552 /*
553  * Receive data on a hook.
554  */
555 static int
556 ng_ether_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
557 {
558         const node_p node = hook->node;
559         const priv_p priv = node->private;
560
561         if (hook == priv->lower)
562                 return ng_ether_rcv_lower(node, m, meta);
563         if (hook == priv->upper)
564                 return ng_ether_rcv_upper(node, m, meta);
565         panic("%s: weird hook", __func__);
566 }
567
568 /*
569  * Handle an mbuf received on the "lower" hook.
570  */
571 static int
572 ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta)
573 {
574         const priv_p priv = node->private;
575         struct ifnet *const ifp = priv->ifp;
576         int error;
577
578         /* Discard meta info */
579         NG_FREE_META(meta);
580
581         /* Check whether interface is ready for packets */
582         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
583                 m_freem(m);
584                 return (ENETDOWN);
585         }
586
587         /* Make sure header is fully pulled up */
588         if (m->m_pkthdr.len < sizeof(struct ether_header)) {
589                 m_freem(m);
590                 return (EINVAL);
591         }
592         if (m->m_len < sizeof(struct ether_header)
593             && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
594                 return (ENOBUFS);
595
596         /* Drop in the MAC address if desired */
597         if (priv->autoSrcAddr) {
598
599                 /* Make the mbuf writable if it's not already */
600                 if (!M_WRITABLE(m)
601                     && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
602                         return (ENOBUFS);
603
604                 /* Overwrite source MAC address */
605                 bcopy((IFP2AC(ifp))->ac_enaddr,
606                     mtod(m, struct ether_header *)->ether_shost,
607                     ETHER_ADDR_LEN);
608         }
609
610         /* Send it on its way */
611         error = ether_output_frame(ifp, m);
612         return (error);
613 }
614
615 /*
616  * Handle an mbuf received on the "upper" hook.
617  */
618 static int
619 ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta)
620 {
621         const priv_p priv = node->private;
622
623         /* Discard meta info */
624         NG_FREE_META(meta);
625
626         /* Check length and pull off header */
627         if (m->m_pkthdr.len < ETHER_HDR_LEN) {
628                 m_freem(m);
629                 return (EINVAL);
630         }
631         if (m->m_len < ETHER_HDR_LEN &&
632             (m = m_pullup(m, ETHER_HDR_LEN)) == NULL)
633                 return (ENOBUFS);
634
635         m->m_pkthdr.rcvif = priv->ifp;
636
637         /* Route packet back in */
638         lwkt_serialize_enter(priv->ifp->if_serializer);
639         ether_demux(priv->ifp, m);
640         lwkt_serialize_exit(priv->ifp->if_serializer);
641         return (0);
642 }
643
644 /*
645  * Shutdown node. This resets the node but does not remove it.
646  */
647 static int
648 ng_ether_rmnode(node_p node)
649 {
650         const priv_p priv = node->private;
651
652         ng_cutlinks(node);
653         node->flags &= ~NG_INVALID;     /* bounce back to life */
654         if (priv->promisc) {            /* disable promiscuous mode */
655                 ifpromisc(priv->ifp, 0);
656                 priv->promisc = 0;
657         }
658         priv->autoSrcAddr = 1;          /* reset auto-src-addr flag */
659         return (0);
660 }
661
662 /*
663  * Hook disconnection.
664  */
665 static int
666 ng_ether_disconnect(hook_p hook)
667 {
668         const priv_p priv = hook->node->private;
669
670         if (hook == priv->upper) {
671                 priv->upper = NULL;
672                 priv->ifp->if_hwassist = priv->hwassist;  /* restore h/w csum */
673         } else if (hook == priv->lower) {
674                 priv->lower = NULL;
675                 priv->lowerOrphan = 0;
676         } else
677                 panic("%s: weird hook", __func__);
678         if (hook->node->numhooks == 0)
679                 ng_rmnode(hook->node);  /* reset node */
680         return (0);
681 }
682
683 static int
684 ng_enaddr_parse(const struct ng_parse_type *type,
685         const char *s, int *const off, const u_char *const start,
686         u_char *const buf, int *const buflen)
687 {
688         char *eptr;
689         u_long val;
690         int i;
691
692         if (*buflen < ETHER_ADDR_LEN)
693                 return (ERANGE);
694         for (i = 0; i < ETHER_ADDR_LEN; i++) {
695                 val = strtoul(s + *off, &eptr, 16);
696                 if (val > 0xff || eptr == s + *off)
697                         return (EINVAL);
698                 buf[i] = (u_char)val;
699                 *off = (eptr - s);
700                 if (i < ETHER_ADDR_LEN - 1) {
701                         if (*eptr != ':')
702                                 return (EINVAL);
703                         (*off)++;
704                 }
705         }
706         *buflen = ETHER_ADDR_LEN;
707         return (0);
708 }
709
710 static int
711 ng_enaddr_unparse(const struct ng_parse_type *type,
712         const u_char *data, int *off, char *cbuf, int cbuflen)
713 {
714         int len;
715
716         len = ksnprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
717             data[*off], data[*off + 1], data[*off + 2],
718             data[*off + 3], data[*off + 4], data[*off + 5]);
719         if (len >= cbuflen)
720                 return (ERANGE);
721         *off += ETHER_ADDR_LEN;
722         return (0);
723 }
724
725 /******************************************************************
726                         INITIALIZATION
727 ******************************************************************/
728
729 /*
730  * Handle loading and unloading for this node type.
731  */
732 static int
733 ng_ether_mod_event(module_t mod, int event, void *data)
734 {
735         struct ifnet *ifp;
736         int error = 0;
737
738         crit_enter();
739         switch (event) {
740         case MOD_LOAD:
741
742                 /* Register function hooks */
743                 if (ng_ether_attach_p != NULL) {
744                         error = EEXIST;
745                         break;
746                 }
747                 ng_ether_attach_p = ng_ether_attach;
748                 ng_ether_detach_p = ng_ether_detach;
749                 ng_ether_output_p = ng_ether_output;
750                 ng_ether_input_p = ng_ether_input;
751                 ng_ether_input_orphan_p = ng_ether_input_orphan;
752
753                 /* Create nodes for any already-existing Ethernet interfaces */
754                 TAILQ_FOREACH(ifp, &ifnet, if_link) {
755                         if (ifp->if_type == IFT_ETHER ||
756                             ifp->if_type == IFT_L2VLAN)
757                                 ng_ether_attach(ifp);
758                 }
759                 break;
760
761         case MOD_UNLOAD:
762
763                 /*
764                  * Note that the base code won't try to unload us until
765                  * all nodes have been removed, and that can't happen
766                  * until all Ethernet interfaces are removed. In any
767                  * case, we know there are no nodes left if the action
768                  * is MOD_UNLOAD, so there's no need to detach any nodes.
769                  */
770
771                 /* Unregister function hooks */
772                 ng_ether_attach_p = NULL;
773                 ng_ether_detach_p = NULL;
774                 ng_ether_output_p = NULL;
775                 ng_ether_input_p = NULL;
776                 ng_ether_input_orphan_p = NULL;
777                 break;
778
779         default:
780                 error = EOPNOTSUPP;
781                 break;
782         }
783         crit_exit();
784         return (error);
785 }
786