]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netgraph/ng_ether.c
if you have taken the mbuf out of the message object, then if you pass
[FreeBSD/FreeBSD.git] / sys / netgraph / 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$
41  */
42
43 /*
44  * ng_ether(4) netgraph node type
45  */
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/mbuf.h>
52 #include <sys/errno.h>
53 #include <sys/syslog.h>
54 #include <sys/socket.h>
55
56 #include <net/if.h>
57 #include <net/if_types.h>
58 #include <net/if_arp.h>
59 #include <net/if_var.h>
60 #include <net/ethernet.h>
61
62 #include <netgraph/ng_message.h>
63 #include <netgraph/netgraph.h>
64 #include <netgraph/ng_parse.h>
65 #include <netgraph/ng_ether.h>
66
67 #define IFP2AC(IFP)  ((struct arpcom *)IFP)
68 #define IFP2NG(ifp)  ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph)
69
70 /* Per-node private data */
71 struct private {
72         struct ifnet    *ifp;           /* associated interface */
73         hook_p          upper;          /* upper hook connection */
74         hook_p          lower;          /* lower OR orphan hook connection */
75         u_char          lowerOrphan;    /* whether lower is lower or orphan */
76         u_char          autoSrcAddr;    /* always overwrite source address */
77         u_char          promisc;        /* promiscuous mode enabled */
78         u_long          hwassist;       /* hardware checksum capabilities */
79         u_int           flags;          /* flags e.g. really die */
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_shutdown;
104 static ng_newhook_t     ng_ether_newhook;
105 static ng_connect_t     ng_ether_connect;
106 static ng_rcvdata_t     ng_ether_rcvdata;
107 static ng_disconnect_t  ng_ether_disconnect;
108 static int              ng_ether_mod_event(module_t mod, int event, void *data);
109
110 /* Parse type for an Ethernet address */
111 static ng_parse_t       ng_enaddr_parse;
112 static ng_unparse_t     ng_enaddr_unparse;
113 const struct ng_parse_type ng_ether_enaddr_type = {
114         NULL,
115         NULL,
116         NULL,
117         ng_enaddr_parse,
118         ng_enaddr_unparse,
119         NULL,                   /* no such thing as a "default" EN address */
120         0
121 };
122
123 /* List of commands and how to convert arguments to/from ASCII */
124 static const struct ng_cmdlist ng_ether_cmdlist[] = {
125         {
126           NGM_ETHER_COOKIE,
127           NGM_ETHER_GET_IFNAME,
128           "getifname",
129           NULL,
130           &ng_parse_string_type
131         },
132         {
133           NGM_ETHER_COOKIE,
134           NGM_ETHER_GET_IFINDEX,
135           "getifindex",
136           NULL,
137           &ng_parse_int32_type
138         },
139         {
140           NGM_ETHER_COOKIE,
141           NGM_ETHER_GET_ENADDR,
142           "getenaddr",
143           NULL,
144           &ng_ether_enaddr_type
145         },
146         {
147           NGM_ETHER_COOKIE,
148           NGM_ETHER_SET_ENADDR,
149           "setenaddr",
150           &ng_ether_enaddr_type,
151           NULL
152         },
153         {
154           NGM_ETHER_COOKIE,
155           NGM_ETHER_GET_PROMISC,
156           "getpromisc",
157           NULL,
158           &ng_parse_int32_type
159         },
160         {
161           NGM_ETHER_COOKIE,
162           NGM_ETHER_SET_PROMISC,
163           "setpromisc",
164           &ng_parse_int32_type,
165           NULL
166         },
167         {
168           NGM_ETHER_COOKIE,
169           NGM_ETHER_GET_AUTOSRC,
170           "getautosrc",
171           NULL,
172           &ng_parse_int32_type
173         },
174         {
175           NGM_ETHER_COOKIE,
176           NGM_ETHER_SET_AUTOSRC,
177           "setautosrc",
178           &ng_parse_int32_type,
179           NULL
180         },
181         { 0 }
182 };
183
184 static struct ng_type ng_ether_typestruct = {
185         NG_ABI_VERSION,
186         NG_ETHER_NODE_TYPE,
187         ng_ether_mod_event,
188         ng_ether_constructor,
189         ng_ether_rcvmsg,
190         ng_ether_shutdown,
191         ng_ether_newhook,
192         NULL,
193         ng_ether_connect,
194         ng_ether_rcvdata,
195         ng_ether_disconnect,
196         ng_ether_cmdlist,
197 };
198 MODULE_VERSION(ng_ether, 1);
199 NETGRAPH_INIT(ether, &ng_ether_typestruct);
200
201 /******************************************************************
202                     ETHERNET FUNCTION HOOKS
203 ******************************************************************/
204
205 /*
206  * Handle a packet that has come in on an interface. We get to
207  * look at it here before any upper layer protocols do.
208  *
209  * NOTE: this function will get called at splimp()
210  */
211 static void
212 ng_ether_input(struct ifnet *ifp,
213         struct mbuf **mp, struct ether_header *eh)
214 {
215         const node_p node = IFP2NG(ifp);
216         const priv_p priv = NG_NODE_PRIVATE(node);
217
218         /* If "lower" hook not connected, let packet continue */
219         if (priv->lower == NULL || priv->lowerOrphan)
220                 return;
221         ng_ether_input2(node, mp, eh);
222 }
223
224 /*
225  * Handle a packet that has come in on an interface, and which
226  * does not match any of our known protocols (an ``orphan'').
227  *
228  * NOTE: this function will get called at splimp()
229  */
230 static void
231 ng_ether_input_orphan(struct ifnet *ifp,
232         struct mbuf *m, struct ether_header *eh)
233 {
234         const node_p node = IFP2NG(ifp);
235         const priv_p priv = NG_NODE_PRIVATE(node);
236
237         /* If "orphan" hook not connected, let packet continue */
238         if (priv->lower == NULL || !priv->lowerOrphan) {
239                 m_freem(m);
240                 return;
241         }
242         ng_ether_input2(node, &m, eh);
243         if (m != NULL)
244                 m_freem(m);
245 }
246
247 /*
248  * Handle a packet that has come in on an ethernet interface.
249  * The Ethernet header has already been detached from the mbuf,
250  * so we have to put it back.
251  *
252  * NOTE: this function will get called at splimp()
253  */
254 static void
255 ng_ether_input2(node_p node, struct mbuf **mp, struct ether_header *eh)
256 {
257         const priv_p priv = NG_NODE_PRIVATE(node);
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         NG_SEND_DATA_ONLY(error, priv->lower, *mp);
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 = NG_NODE_PRIVATE(node);
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_ONLY(error, priv->upper, *mp);
286         return (error);
287 }
288
289 /*
290  * A new Ethernet interface has been attached.
291  * Create a new node for it, etc.
292  */
293 static void
294 ng_ether_attach(struct ifnet *ifp)
295 {
296         char name[IFNAMSIZ + 1];
297         priv_p priv;
298         node_p node;
299
300         /* Create node */
301         KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
302         snprintf(name, sizeof(name), "%s%d", ifp->if_name, ifp->if_unit);
303         if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
304                 log(LOG_ERR, "%s: can't %s for %s\n",
305                     __func__, "create node", name);
306                 return;
307         }
308
309         /* Allocate private data */
310         MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
311         if (priv == NULL) {
312                 log(LOG_ERR, "%s: can't %s for %s\n",
313                     __func__, "allocate memory", name);
314                 NG_NODE_UNREF(node);
315                 return;
316         }
317         NG_NODE_SET_PRIVATE(node, priv);
318         priv->ifp = ifp;
319         IFP2NG(ifp) = node;
320         priv->autoSrcAddr = 1;
321         priv->hwassist = ifp->if_hwassist;
322
323         /* Try to give the node the same name as the interface */
324         if (ng_name_node(node, name) != 0) {
325                 log(LOG_WARNING, "%s: can't name node %s\n",
326                     __func__, name);
327         }
328 }
329
330 /*
331  * An Ethernet interface is being detached.
332  * REALLY Destroy its node.
333  */
334 static void
335 ng_ether_detach(struct ifnet *ifp)
336 {
337         const node_p node = IFP2NG(ifp);
338         const priv_p priv = NG_NODE_PRIVATE(node);
339
340         if (node == NULL)               /* no node (why not?), ignore */
341                 return;
342         NG_NODE_REALLY_DIE(node);       /* Force real removal of node */
343         /*
344          * We can't assume the ifnet is still around when we run shutdown
345          * So zap it now. XXX We HOPE that anything running at this time
346          * handles it (as it should in the non netgraph case).
347          */
348         IFP2NG(ifp) = NULL;
349         priv->ifp = NULL;       /* XXX race if interrupted an output packet */
350         ng_rmnode_self(node);           /* remove all netgraph parts */
351 }
352
353 /*
354  * Optimization for gluing the Ethernet header back onto
355  * the front of an incoming packet.
356  */
357 static int
358 ng_ether_glueback_header(struct mbuf **mp, struct ether_header *eh)
359 {
360         struct mbuf *m = *mp;
361         int error = 0;
362
363         /*
364          * Optimize for the case where the header is already in place
365          * at the front of the mbuf. This is actually quite likely
366          * because many Ethernet drivers generate packets this way.
367          */
368         if (eh == mtod(m, struct ether_header *) - 1) {
369                 m->m_len += sizeof(*eh);
370                 m->m_data -= sizeof(*eh);
371                 m->m_pkthdr.len += sizeof(*eh);
372                 goto done;
373         }
374
375         /* Prepend the header back onto the front of the mbuf */
376         M_PREPEND(m, sizeof(*eh), M_DONTWAIT);
377         if (m == NULL) {
378                 error = ENOBUFS;
379                 goto done;
380         }
381
382         /* Copy header into front of mbuf */
383         bcopy(eh, mtod(m, void *), sizeof(*eh));
384
385 done:
386         /* Done */
387         *mp = m;
388         return error;
389 }
390
391 /******************************************************************
392                     NETGRAPH NODE METHODS
393 ******************************************************************/
394
395 /*
396  * It is not possible or allowable to create a node of this type.
397  * Nodes get created when the interface is attached (or, when
398  * this node type's KLD is loaded).
399  */
400 static int
401 ng_ether_constructor(node_p node)
402 {
403         return (EINVAL);
404 }
405
406 /*
407  * Check for attaching a new hook.
408  */
409 static  int
410 ng_ether_newhook(node_p node, hook_p hook, const char *name)
411 {
412         const priv_p priv = NG_NODE_PRIVATE(node);
413         u_char orphan = priv->lowerOrphan;
414         hook_p *hookptr;
415
416         /* Divert hook is an alias for lower */
417         if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
418                 name = NG_ETHER_HOOK_LOWER;
419
420         /* Which hook? */
421         if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
422                 hookptr = &priv->upper;
423         else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
424                 hookptr = &priv->lower;
425                 orphan = 0;
426         } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
427                 hookptr = &priv->lower;
428                 orphan = 1;
429         } else
430                 return (EINVAL);
431
432         /* Check if already connected (shouldn't be, but doesn't hurt) */
433         if (*hookptr != NULL)
434                 return (EISCONN);
435
436         /* Disable hardware checksums while 'upper' hook is connected */
437         if (hookptr == &priv->upper)
438                 priv->ifp->if_hwassist = 0;
439
440         /* OK */
441         *hookptr = hook;
442         priv->lowerOrphan = orphan;
443         return (0);
444 }
445
446 /*
447  * Hooks are attached, adjust to force queueing.
448  * We don't really care which hook it is.
449  * they should all be queuing for outgoing data.
450  */
451 static  int
452 ng_ether_connect(hook_p hook)
453 {
454         NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
455         return (0);
456 }
457
458 /*
459  * Receive an incoming control message.
460  */
461 static int
462 ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
463 {
464         const priv_p priv = NG_NODE_PRIVATE(node);
465         struct ng_mesg *resp = NULL;
466         int error = 0;
467         struct ng_mesg *msg;
468
469         NGI_GET_MSG(item, msg);
470         switch (msg->header.typecookie) {
471         case NGM_ETHER_COOKIE:
472                 switch (msg->header.cmd) {
473                 case NGM_ETHER_GET_IFNAME:
474                         NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT);
475                         if (resp == NULL) {
476                                 error = ENOMEM;
477                                 break;
478                         }
479                         snprintf(resp->data, IFNAMSIZ + 1,
480                             "%s%d", priv->ifp->if_name, priv->ifp->if_unit);
481                         break;
482                 case NGM_ETHER_GET_IFINDEX:
483                         NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
484                         if (resp == NULL) {
485                                 error = ENOMEM;
486                                 break;
487                         }
488                         *((u_int32_t *)resp->data) = priv->ifp->if_index;
489                         break;
490                 case NGM_ETHER_GET_ENADDR:
491                         NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
492                         if (resp == NULL) {
493                                 error = ENOMEM;
494                                 break;
495                         }
496                         bcopy((IFP2AC(priv->ifp))->ac_enaddr,
497                             resp->data, ETHER_ADDR_LEN);
498                         break;
499                 case NGM_ETHER_SET_ENADDR:
500                     {
501                         if (msg->header.arglen != ETHER_ADDR_LEN) {
502                                 error = EINVAL;
503                                 break;
504                         }
505                         error = if_setlladdr(priv->ifp,
506                             (u_char *)msg->data, ETHER_ADDR_LEN);
507                         break;
508                     }
509                 case NGM_ETHER_GET_PROMISC:
510                         NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
511                         if (resp == NULL) {
512                                 error = ENOMEM;
513                                 break;
514                         }
515                         *((u_int32_t *)resp->data) = priv->promisc;
516                         break;
517                 case NGM_ETHER_SET_PROMISC:
518                     {
519                         u_char want;
520
521                         if (msg->header.arglen != sizeof(u_int32_t)) {
522                                 error = EINVAL;
523                                 break;
524                         }
525                         want = !!*((u_int32_t *)msg->data);
526                         if (want ^ priv->promisc) {
527                                 if ((error = ifpromisc(priv->ifp, want)) != 0)
528                                         break;
529                                 priv->promisc = want;
530                         }
531                         break;
532                     }
533                 case NGM_ETHER_GET_AUTOSRC:
534                         NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
535                         if (resp == NULL) {
536                                 error = ENOMEM;
537                                 break;
538                         }
539                         *((u_int32_t *)resp->data) = priv->autoSrcAddr;
540                         break;
541                 case NGM_ETHER_SET_AUTOSRC:
542                         if (msg->header.arglen != sizeof(u_int32_t)) {
543                                 error = EINVAL;
544                                 break;
545                         }
546                         priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
547                         break;
548                 default:
549                         error = EINVAL;
550                         break;
551                 }
552                 break;
553         default:
554                 error = EINVAL;
555                 break;
556         }
557         NG_RESPOND_MSG(error, node, item, resp);
558         NG_FREE_MSG(msg);
559         return (error);
560 }
561
562 /*
563  * Receive data on a hook.
564  */
565 static int
566 ng_ether_rcvdata(hook_p hook, item_p item)
567 {
568         const node_p node = NG_HOOK_NODE(hook);
569         const priv_p priv = NG_NODE_PRIVATE(node);
570         struct mbuf *m;
571         meta_p meta;
572
573         NGI_GET_M(item, m);
574         NGI_GET_META(item, meta);
575         NG_FREE_ITEM(item);
576         if (hook == priv->lower)
577                 return ng_ether_rcv_lower(node, m, meta);
578         if (hook == priv->upper)
579                 return ng_ether_rcv_upper(node, m, meta);
580         panic("%s: weird hook", __func__);
581 #ifdef RESTARTABLE_PANICS /* so we don;t get an error msg in LINT */
582         return NULL;
583 #endif
584 }
585
586 /*
587  * Handle an mbuf received on the "lower" hook.
588  */
589 static int
590 ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta)
591 {
592         const priv_p priv = NG_NODE_PRIVATE(node);
593         struct ifnet *const ifp = priv->ifp;
594
595         /* Discard meta info */
596         NG_FREE_META(meta);
597
598         /* Check whether interface is ready for packets */
599         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
600                 NG_FREE_M(m);
601                 return (ENETDOWN);
602         }
603
604         /* Make sure header is fully pulled up */
605         if (m->m_pkthdr.len < sizeof(struct ether_header)) {
606                 NG_FREE_M(m);
607                 return (EINVAL);
608         }
609         if (m->m_len < sizeof(struct ether_header)
610             && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
611                 return (ENOBUFS);
612
613         /* Drop in the MAC address if desired */
614         if (priv->autoSrcAddr) {
615
616                 /* Make the mbuf writable if it's not already */
617                 if (!M_WRITABLE(m)
618                     && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
619                         return (ENOBUFS);
620
621                 /* Overwrite source MAC address */
622                 bcopy((IFP2AC(ifp))->ac_enaddr,
623                     mtod(m, struct ether_header *)->ether_shost,
624                     ETHER_ADDR_LEN);
625         }
626
627         /* Send it on its way */
628         return ether_output_frame(ifp, m);
629 }
630
631 /*
632  * Handle an mbuf received on the "upper" hook.
633  */
634 static int
635 ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta)
636 {
637         const priv_p priv = NG_NODE_PRIVATE(node);
638         struct ether_header *eh;
639
640         /* Discard meta info */
641         NG_FREE_META(meta);
642
643         /* Check length and pull off header */
644         if (m->m_pkthdr.len < sizeof(*eh)) {
645                 NG_FREE_M(m);
646                 return (EINVAL);
647         }
648         if (m->m_len < sizeof(*eh) && (m = m_pullup(m, sizeof(*eh))) == NULL)
649                 return (ENOBUFS);
650         eh = mtod(m, struct ether_header *);
651         m->m_data += sizeof(*eh);
652         m->m_len -= sizeof(*eh);
653         m->m_pkthdr.len -= sizeof(*eh);
654         m->m_pkthdr.rcvif = priv->ifp;
655
656         /* Route packet back in */
657         ether_demux(priv->ifp, eh, m);
658         return (0);
659 }
660
661 /*
662  * Shutdown node. This resets the node but does not remove it
663  * unless the REALLY_DIE flag is set.
664  */
665 static int
666 ng_ether_shutdown(node_p node)
667 {
668         const priv_p priv = NG_NODE_PRIVATE(node);
669
670         if (priv->promisc) {            /* disable promiscuous mode */
671                 (void)ifpromisc(priv->ifp, 0);
672                 priv->promisc = 0;
673         }
674         if (node->nd_flags & NG_REALLY_DIE) {
675                 /*
676                  * WE came here because the ethernet card is being unloaded,
677                  * so stop being persistant.
678                  * Actually undo all the things we did on creation.
679                  * Assume the ifp has already been freed.
680                  */
681                 NG_NODE_SET_PRIVATE(node, NULL);
682                 FREE(priv, M_NETGRAPH);         
683                 NG_NODE_UNREF(node);    /* free node itself */
684                 return (0);
685         }
686         priv->autoSrcAddr = 1;          /* reset auto-src-addr flag */
687         node->nd_flags &= ~NG_INVALID;  /* Signal ng_rmnode we are persisant */
688         return (0);
689 }
690
691 /*
692  * Hook disconnection.
693  */
694 static int
695 ng_ether_disconnect(hook_p hook)
696 {
697         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
698
699         if (hook == priv->upper) {
700                 priv->upper = NULL;
701                 priv->ifp->if_hwassist = priv->hwassist;  /* restore h/w csum */
702         } else if (hook == priv->lower) {
703                 priv->lower = NULL;
704                 priv->lowerOrphan = 0;
705         } else
706                 panic("%s: weird hook", __func__);
707         if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
708         && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
709                 ng_rmnode_self(NG_HOOK_NODE(hook));     /* reset node */
710         return (0);
711 }
712
713 static int
714 ng_enaddr_parse(const struct ng_parse_type *type,
715         const char *s, int *const off, const u_char *const start,
716         u_char *const buf, int *const buflen)
717 {
718         char *eptr;
719         u_long val;
720         int i;
721
722         if (*buflen < ETHER_ADDR_LEN)
723                 return (ERANGE);
724         for (i = 0; i < ETHER_ADDR_LEN; i++) {
725                 val = strtoul(s + *off, &eptr, 16);
726                 if (val > 0xff || eptr == s + *off)
727                         return (EINVAL);
728                 buf[i] = (u_char)val;
729                 *off = (eptr - s);
730                 if (i < ETHER_ADDR_LEN - 1) {
731                         if (*eptr != ':')
732                                 return (EINVAL);
733                         (*off)++;
734                 }
735         }
736         *buflen = ETHER_ADDR_LEN;
737         return (0);
738 }
739
740 static int
741 ng_enaddr_unparse(const struct ng_parse_type *type,
742         const u_char *data, int *off, char *cbuf, int cbuflen)
743 {
744         int len;
745
746         len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
747             data[*off], data[*off + 1], data[*off + 2],
748             data[*off + 3], data[*off + 4], data[*off + 5]);
749         if (len >= cbuflen)
750                 return (ERANGE);
751         *off += ETHER_ADDR_LEN;
752         return (0);
753 }
754
755 /******************************************************************
756                         INITIALIZATION
757 ******************************************************************/
758
759 /*
760  * Handle loading and unloading for this node type.
761  */
762 static int
763 ng_ether_mod_event(module_t mod, int event, void *data)
764 {
765         struct ifnet *ifp;
766         int error = 0;
767         int s;
768
769         s = splnet();
770         switch (event) {
771         case MOD_LOAD:
772
773                 /* Register function hooks */
774                 if (ng_ether_attach_p != NULL) {
775                         error = EEXIST;
776                         break;
777                 }
778                 ng_ether_attach_p = ng_ether_attach;
779                 ng_ether_detach_p = ng_ether_detach;
780                 ng_ether_output_p = ng_ether_output;
781                 ng_ether_input_p = ng_ether_input;
782                 ng_ether_input_orphan_p = ng_ether_input_orphan;
783
784                 /* Create nodes for any already-existing Ethernet interfaces */
785                 TAILQ_FOREACH(ifp, &ifnet, if_link) {
786                         if (ifp->if_type == IFT_ETHER
787                             || ifp->if_type == IFT_L2VLAN)
788                                 ng_ether_attach(ifp);
789                 }
790                 break;
791
792         case MOD_UNLOAD:
793
794                 /*
795                  * Note that the base code won't try to unload us until
796                  * all nodes have been removed, and that can't happen
797                  * until all Ethernet interfaces are removed. In any
798                  * case, we know there are no nodes left if the action
799                  * is MOD_UNLOAD, so there's no need to detach any nodes.
800                  */
801
802                 /* Unregister function hooks */
803                 ng_ether_attach_p = NULL;
804                 ng_ether_detach_p = NULL;
805                 ng_ether_output_p = NULL;
806                 ng_ether_input_p = NULL;
807                 ng_ether_input_orphan_p = NULL;
808                 break;
809
810         default:
811                 error = EOPNOTSUPP;
812                 break;
813         }
814         splx(s);
815         return (error);
816 }
817