]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/netgraph/ng_ether.c
MFC r290326:
[FreeBSD/stable/8.git] / sys / netgraph / ng_ether.c
1
2 /*
3  * ng_ether.c
4  */
5
6 /*-
7  * Copyright (c) 1996-2000 Whistle Communications, Inc.
8  * All rights reserved.
9  * 
10  * Subject to the following obligations and disclaimer of warranty, use and
11  * redistribution of this software, in source or object code forms, with or
12  * without modifications are expressly permitted by Whistle Communications;
13  * provided, however, that:
14  * 1. Any and all reproductions of the source or object code must include the
15  *    copyright notice above and the following disclaimer of warranties; and
16  * 2. No rights are granted, in any manner or form, to use Whistle
17  *    Communications, Inc. trademarks, including the mark "WHISTLE
18  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
19  *    such appears in the above copyright notice or in the software.
20  * 
21  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
22  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
23  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
24  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
26  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
27  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
28  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
29  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
30  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
31  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
32  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
33  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37  * OF SUCH DAMAGE.
38  *
39  * Authors: Archie Cobbs <archie@freebsd.org>
40  *          Julian Elischer <julian@freebsd.org>
41  *
42  * $FreeBSD$
43  */
44
45 /*
46  * ng_ether(4) netgraph node type
47  */
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/errno.h>
55 #include <sys/proc.h>
56 #include <sys/syslog.h>
57 #include <sys/socket.h>
58 #include <sys/taskqueue.h>
59
60 #include <net/if.h>
61 #include <net/if_dl.h>
62 #include <net/if_types.h>
63 #include <net/if_arp.h>
64 #include <net/if_var.h>
65 #include <net/ethernet.h>
66 #include <net/if_bridgevar.h>
67 #include <net/vnet.h>
68
69 #include <netgraph/ng_message.h>
70 #include <netgraph/netgraph.h>
71 #include <netgraph/ng_parse.h>
72 #include <netgraph/ng_ether.h>
73
74 #define IFP2NG(ifp)  (IFP2AC((ifp))->ac_netgraph)
75
76 /* Per-node private data */
77 struct private {
78         struct ifnet    *ifp;           /* associated interface */
79         hook_p          upper;          /* upper hook connection */
80         hook_p          lower;          /* lower hook connection */
81         hook_p          orphan;         /* orphan hook connection */
82         u_char          autoSrcAddr;    /* always overwrite source address */
83         u_char          promisc;        /* promiscuous mode enabled */
84         u_long          hwassist;       /* hardware checksum capabilities */
85         u_int           flags;          /* flags e.g. really die */
86 };
87 typedef struct private *priv_p;
88
89 /* Hook pointers used by if_ethersubr.c to callback to netgraph */
90 extern  void    (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
91 extern  void    (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
92 extern  int     (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
93 extern  void    (*ng_ether_attach_p)(struct ifnet *ifp);
94 extern  void    (*ng_ether_detach_p)(struct ifnet *ifp);
95 extern  void    (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
96
97 /* Functional hooks called from if_ethersubr.c */
98 static void     ng_ether_input(struct ifnet *ifp, struct mbuf **mp);
99 static void     ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m);
100 static int      ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
101 static void     ng_ether_attach(struct ifnet *ifp);
102 static void     ng_ether_detach(struct ifnet *ifp); 
103 static void     ng_ether_link_state(struct ifnet *ifp, int state); 
104
105 /* Other functions */
106 static int      ng_ether_rcv_lower(hook_p node, item_p item);
107 static int      ng_ether_rcv_upper(hook_p node, item_p item);
108
109 /* Netgraph node methods */
110 static ng_constructor_t ng_ether_constructor;
111 static ng_rcvmsg_t      ng_ether_rcvmsg;
112 static ng_shutdown_t    ng_ether_shutdown;
113 static ng_newhook_t     ng_ether_newhook;
114 static ng_rcvdata_t     ng_ether_rcvdata;
115 static ng_disconnect_t  ng_ether_disconnect;
116 static int              ng_ether_mod_event(module_t mod, int event, void *data);
117
118 static eventhandler_tag ng_ether_ifnet_arrival_cookie;
119
120 /* List of commands and how to convert arguments to/from ASCII */
121 static const struct ng_cmdlist ng_ether_cmdlist[] = {
122         {
123           NGM_ETHER_COOKIE,
124           NGM_ETHER_GET_IFNAME,
125           "getifname",
126           NULL,
127           &ng_parse_string_type
128         },
129         {
130           NGM_ETHER_COOKIE,
131           NGM_ETHER_GET_IFINDEX,
132           "getifindex",
133           NULL,
134           &ng_parse_int32_type
135         },
136         {
137           NGM_ETHER_COOKIE,
138           NGM_ETHER_GET_ENADDR,
139           "getenaddr",
140           NULL,
141           &ng_parse_enaddr_type
142         },
143         {
144           NGM_ETHER_COOKIE,
145           NGM_ETHER_SET_ENADDR,
146           "setenaddr",
147           &ng_parse_enaddr_type,
148           NULL
149         },
150         {
151           NGM_ETHER_COOKIE,
152           NGM_ETHER_GET_PROMISC,
153           "getpromisc",
154           NULL,
155           &ng_parse_int32_type
156         },
157         {
158           NGM_ETHER_COOKIE,
159           NGM_ETHER_SET_PROMISC,
160           "setpromisc",
161           &ng_parse_int32_type,
162           NULL
163         },
164         {
165           NGM_ETHER_COOKIE,
166           NGM_ETHER_GET_AUTOSRC,
167           "getautosrc",
168           NULL,
169           &ng_parse_int32_type
170         },
171         {
172           NGM_ETHER_COOKIE,
173           NGM_ETHER_SET_AUTOSRC,
174           "setautosrc",
175           &ng_parse_int32_type,
176           NULL
177         },
178         {
179           NGM_ETHER_COOKIE,
180           NGM_ETHER_ADD_MULTI,
181           "addmulti",
182           &ng_parse_enaddr_type,
183           NULL
184         },
185         {
186           NGM_ETHER_COOKIE,
187           NGM_ETHER_DEL_MULTI,
188           "delmulti",
189           &ng_parse_enaddr_type,
190           NULL
191         },
192         {
193           NGM_ETHER_COOKIE,
194           NGM_ETHER_DETACH,
195           "detach",
196           NULL,
197           NULL
198         },
199         { 0 }
200 };
201
202 static struct ng_type ng_ether_typestruct = {
203         .version =      NG_ABI_VERSION,
204         .name =         NG_ETHER_NODE_TYPE,
205         .mod_event =    ng_ether_mod_event,
206         .constructor =  ng_ether_constructor,
207         .rcvmsg =       ng_ether_rcvmsg,
208         .shutdown =     ng_ether_shutdown,
209         .newhook =      ng_ether_newhook,
210         .rcvdata =      ng_ether_rcvdata,
211         .disconnect =   ng_ether_disconnect,
212         .cmdlist =      ng_ether_cmdlist,
213 };
214 NETGRAPH_INIT(ether, &ng_ether_typestruct);
215
216 /******************************************************************
217                     UTILITY FUNCTIONS
218 ******************************************************************/
219 static void
220 ng_ether_sanitize_ifname(const char *ifname, char *name)
221 {
222         int i;
223
224         for (i = 0; i < IFNAMSIZ; i++) {
225                 if (ifname[i] == '.' || ifname[i] == ':')
226                         name[i] = '_';
227                 else
228                         name[i] = ifname[i];
229                 if (name[i] == '\0')
230                         break;
231         }
232 }
233
234 /******************************************************************
235                     ETHERNET FUNCTION HOOKS
236 ******************************************************************/
237
238 /*
239  * Handle a packet that has come in on an interface. We get to
240  * look at it here before any upper layer protocols do.
241  *
242  * NOTE: this function will get called at splimp()
243  */
244 static void
245 ng_ether_input(struct ifnet *ifp, struct mbuf **mp)
246 {
247         const node_p node = IFP2NG(ifp);
248         const priv_p priv = NG_NODE_PRIVATE(node);
249         int error;
250
251         /* If "lower" hook not connected, let packet continue */
252         if (priv->lower == NULL)
253                 return;
254         NG_SEND_DATA_ONLY(error, priv->lower, *mp);     /* sets *mp = NULL */
255 }
256
257 /*
258  * Handle a packet that has come in on an interface, and which
259  * does not match any of our known protocols (an ``orphan'').
260  *
261  * NOTE: this function will get called at splimp()
262  */
263 static void
264 ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m)
265 {
266         const node_p node = IFP2NG(ifp);
267         const priv_p priv = NG_NODE_PRIVATE(node);
268         int error;
269
270         /* If "orphan" hook not connected, discard packet */
271         if (priv->orphan == NULL) {
272                 m_freem(m);
273                 return;
274         }
275         NG_SEND_DATA_ONLY(error, priv->orphan, m);
276 }
277
278 /*
279  * Handle a packet that is going out on an interface.
280  * The Ethernet header is already attached to the mbuf.
281  */
282 static int
283 ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
284 {
285         const node_p node = IFP2NG(ifp);
286         const priv_p priv = NG_NODE_PRIVATE(node);
287         int error = 0;
288
289         /* If "upper" hook not connected, let packet continue */
290         if (priv->upper == NULL)
291                 return (0);
292
293         /* Send it out "upper" hook */
294         NG_OUTBOUND_THREAD_REF();
295         NG_SEND_DATA_ONLY(error, priv->upper, *mp);
296         NG_OUTBOUND_THREAD_UNREF();
297         return (error);
298 }
299
300 /*
301  * A new Ethernet interface has been attached.
302  * Create a new node for it, etc.
303  */
304 static void
305 ng_ether_attach(struct ifnet *ifp)
306 {
307         char name[IFNAMSIZ];
308         priv_p priv;
309         node_p node;
310
311         /*
312          * Do not create / attach an ether node to this ifnet if
313          * a netgraph node with the same name already exists.
314          * This should prevent ether nodes to become attached to
315          * eiface nodes, which may be problematic due to naming
316          * clashes.
317          */
318         if ((node = ng_name2noderef(NULL, ifp->if_xname)) != NULL) {
319                 NG_NODE_UNREF(node);
320                 return;
321         }
322
323         /* Create node */
324         KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
325         if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
326                 log(LOG_ERR, "%s: can't %s for %s\n",
327                     __func__, "create node", ifp->if_xname);
328                 return;
329         }
330
331         /* Allocate private data */
332         priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
333         if (priv == NULL) {
334                 log(LOG_ERR, "%s: can't %s for %s\n",
335                     __func__, "allocate memory", ifp->if_xname);
336                 NG_NODE_UNREF(node);
337                 return;
338         }
339         NG_NODE_SET_PRIVATE(node, priv);
340         priv->ifp = ifp;
341         IFP2NG(ifp) = node;
342         priv->hwassist = ifp->if_hwassist;
343
344         /* Try to give the node the same name as the interface */
345         ng_ether_sanitize_ifname(ifp->if_xname, name);
346         if (ng_name_node(node, name) != 0)
347                 log(LOG_WARNING, "%s: can't name node %s\n", __func__, name);
348 }
349
350 /*
351  * An Ethernet interface is being detached.
352  * REALLY Destroy its node.
353  */
354 static void
355 ng_ether_detach(struct ifnet *ifp)
356 {
357         const node_p node = IFP2NG(ifp);
358         const priv_p priv = NG_NODE_PRIVATE(node);
359
360         taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
361         NG_NODE_REALLY_DIE(node);       /* Force real removal of node */
362         /*
363          * We can't assume the ifnet is still around when we run shutdown
364          * So zap it now. XXX We HOPE that anything running at this time
365          * handles it (as it should in the non netgraph case).
366          */
367         IFP2NG(ifp) = NULL;
368         priv->ifp = NULL;       /* XXX race if interrupted an output packet */
369         ng_rmnode_self(node);           /* remove all netgraph parts */
370 }
371
372 /*
373  * Notify graph about link event.
374  * if_link_state_change() has already checked that the state has changed.
375  */
376 static void
377 ng_ether_link_state(struct ifnet *ifp, int state)
378 {
379         const node_p node = IFP2NG(ifp);
380         const priv_p priv = NG_NODE_PRIVATE(node);
381         struct ng_mesg *msg;
382         int cmd, dummy_error = 0;
383
384         if (state == LINK_STATE_UP)
385                 cmd = NGM_LINK_IS_UP;
386         else if (state == LINK_STATE_DOWN)
387                 cmd = NGM_LINK_IS_DOWN;
388         else
389                 return;
390
391         if (priv->lower != NULL) {
392                 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
393                 if (msg != NULL)
394                         NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0);
395         }
396         if (priv->orphan != NULL) {
397                 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
398                 if (msg != NULL)
399                         NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->orphan, 0);
400         }
401 }
402
403 /*
404  * Interface arrival notification handler.
405  * The notification is produced in two cases:
406  *  o a new interface arrives
407  *  o an existing interface got renamed
408  * Currently the first case is handled by ng_ether_attach via special
409  * hook ng_ether_attach_p.
410  */
411 static void
412 ng_ether_ifnet_arrival_event(void *arg __unused, struct ifnet *ifp)
413 {
414         char name[IFNAMSIZ];
415         node_p node;
416
417         /* Only ethernet interfaces are of interest. */
418         if (ifp->if_type != IFT_ETHER
419             && ifp->if_type != IFT_L2VLAN)
420                 return;
421
422         /*
423          * Just return if it's a new interface without an ng_ether companion.
424          */
425         node = IFP2NG(ifp);
426         if (node == NULL)
427                 return;
428
429         /* Try to give the node the same name as the new interface name */
430         ng_ether_sanitize_ifname(ifp->if_xname, name);
431         if (ng_name_node(node, name) != 0)
432                 log(LOG_WARNING, "%s: can't re-name node %s\n", __func__, name);
433 }
434
435 /******************************************************************
436                     NETGRAPH NODE METHODS
437 ******************************************************************/
438
439 /*
440  * It is not possible or allowable to create a node of this type.
441  * Nodes get created when the interface is attached (or, when
442  * this node type's KLD is loaded).
443  */
444 static int
445 ng_ether_constructor(node_p node)
446 {
447         return (EINVAL);
448 }
449
450 /*
451  * Check for attaching a new hook.
452  */
453 static  int
454 ng_ether_newhook(node_p node, hook_p hook, const char *name)
455 {
456         const priv_p priv = NG_NODE_PRIVATE(node);
457         hook_p *hookptr;
458
459         /* Divert hook is an alias for lower */
460         if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
461                 name = NG_ETHER_HOOK_LOWER;
462
463         /* Which hook? */
464         if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) {
465                 hookptr = &priv->upper;
466                 NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_upper);
467                 NG_HOOK_SET_TO_INBOUND(hook);
468         } else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
469                 hookptr = &priv->lower;
470                 NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower);
471         } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
472                 hookptr = &priv->orphan;
473                 NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower);
474         } else
475                 return (EINVAL);
476
477         /* Check if already connected (shouldn't be, but doesn't hurt) */
478         if (*hookptr != NULL)
479                 return (EISCONN);
480
481         /* Disable hardware checksums while 'upper' hook is connected */
482         if (hookptr == &priv->upper)
483                 priv->ifp->if_hwassist = 0;
484         NG_HOOK_HI_STACK(hook);
485         /* OK */
486         *hookptr = hook;
487         return (0);
488 }
489
490 /*
491  * Receive an incoming control message.
492  */
493 static int
494 ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
495 {
496         const priv_p priv = NG_NODE_PRIVATE(node);
497         struct ng_mesg *resp = NULL;
498         int error = 0;
499         struct ng_mesg *msg;
500
501         NGI_GET_MSG(item, msg);
502         switch (msg->header.typecookie) {
503         case NGM_ETHER_COOKIE:
504                 switch (msg->header.cmd) {
505                 case NGM_ETHER_GET_IFNAME:
506                         NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
507                         if (resp == NULL) {
508                                 error = ENOMEM;
509                                 break;
510                         }
511                         strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
512                         break;
513                 case NGM_ETHER_GET_IFINDEX:
514                         NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
515                         if (resp == NULL) {
516                                 error = ENOMEM;
517                                 break;
518                         }
519                         *((u_int32_t *)resp->data) = priv->ifp->if_index;
520                         break;
521                 case NGM_ETHER_GET_ENADDR:
522                         NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
523                         if (resp == NULL) {
524                                 error = ENOMEM;
525                                 break;
526                         }
527                         bcopy(IF_LLADDR(priv->ifp),
528                             resp->data, ETHER_ADDR_LEN);
529                         break;
530                 case NGM_ETHER_SET_ENADDR:
531                     {
532                         if (msg->header.arglen != ETHER_ADDR_LEN) {
533                                 error = EINVAL;
534                                 break;
535                         }
536                         error = if_setlladdr(priv->ifp,
537                             (u_char *)msg->data, ETHER_ADDR_LEN);
538                         EVENTHANDLER_INVOKE(iflladdr_event, priv->ifp);
539                         break;
540                     }
541                 case NGM_ETHER_GET_PROMISC:
542                         NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
543                         if (resp == NULL) {
544                                 error = ENOMEM;
545                                 break;
546                         }
547                         *((u_int32_t *)resp->data) = priv->promisc;
548                         break;
549                 case NGM_ETHER_SET_PROMISC:
550                     {
551                         u_char want;
552
553                         if (msg->header.arglen != sizeof(u_int32_t)) {
554                                 error = EINVAL;
555                                 break;
556                         }
557                         want = !!*((u_int32_t *)msg->data);
558                         if (want ^ priv->promisc) {
559                                 if ((error = ifpromisc(priv->ifp, want)) != 0)
560                                         break;
561                                 priv->promisc = want;
562                         }
563                         break;
564                     }
565                 case NGM_ETHER_GET_AUTOSRC:
566                         NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
567                         if (resp == NULL) {
568                                 error = ENOMEM;
569                                 break;
570                         }
571                         *((u_int32_t *)resp->data) = priv->autoSrcAddr;
572                         break;
573                 case NGM_ETHER_SET_AUTOSRC:
574                         if (msg->header.arglen != sizeof(u_int32_t)) {
575                                 error = EINVAL;
576                                 break;
577                         }
578                         priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
579                         break;
580                 case NGM_ETHER_ADD_MULTI:
581                     {
582                         struct sockaddr_dl sa_dl;
583                         struct ifmultiaddr *ifma;
584
585                         if (msg->header.arglen != ETHER_ADDR_LEN) {
586                                 error = EINVAL;
587                                 break;
588                         }
589                         bzero(&sa_dl, sizeof(struct sockaddr_dl));
590                         sa_dl.sdl_len = sizeof(struct sockaddr_dl);
591                         sa_dl.sdl_family = AF_LINK;
592                         sa_dl.sdl_alen = ETHER_ADDR_LEN;
593                         bcopy((void *)msg->data, LLADDR(&sa_dl),
594                             ETHER_ADDR_LEN);
595                         /*
596                          * Netgraph is only permitted to join groups once
597                          * via the if_addmulti() KPI, because it cannot hold
598                          * struct ifmultiaddr * between calls. It may also
599                          * lose a race while we check if the membership
600                          * already exists.
601                          */
602                         if_maddr_rlock(priv->ifp);
603                         ifma = if_findmulti(priv->ifp,
604                             (struct sockaddr *)&sa_dl);
605                         if_maddr_runlock(priv->ifp);
606                         if (ifma != NULL) {
607                                 error = EADDRINUSE;
608                         } else {
609                                 error = if_addmulti(priv->ifp,
610                                     (struct sockaddr *)&sa_dl, &ifma);
611                         }
612                         break;
613                     }
614                 case NGM_ETHER_DEL_MULTI:
615                     {
616                         struct sockaddr_dl sa_dl;
617
618                         if (msg->header.arglen != ETHER_ADDR_LEN) {
619                                 error = EINVAL;
620                                 break;
621                         }
622                         bzero(&sa_dl, sizeof(struct sockaddr_dl));
623                         sa_dl.sdl_len = sizeof(struct sockaddr_dl);
624                         sa_dl.sdl_family = AF_LINK;
625                         sa_dl.sdl_alen = ETHER_ADDR_LEN;
626                         bcopy((void *)msg->data, LLADDR(&sa_dl),
627                             ETHER_ADDR_LEN);
628                         error = if_delmulti(priv->ifp,
629                             (struct sockaddr *)&sa_dl);
630                         break;
631                     }
632                 case NGM_ETHER_DETACH:
633                         ng_ether_detach(priv->ifp);
634                         break;
635                 default:
636                         error = EINVAL;
637                         break;
638                 }
639                 break;
640         default:
641                 error = EINVAL;
642                 break;
643         }
644         NG_RESPOND_MSG(error, node, item, resp);
645         NG_FREE_MSG(msg);
646         return (error);
647 }
648
649 /*
650  * Receive data on a hook.
651  * Since we use per-hook recveive methods this should never be called.
652  */
653 static int
654 ng_ether_rcvdata(hook_p hook, item_p item)
655 {
656         NG_FREE_ITEM(item);
657
658         panic("%s: weird hook", __func__);
659 #ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */
660         return (0);
661 #endif
662 }
663
664 /*
665  * Handle an mbuf received on the "lower" or "orphan" hook.
666  */
667 static int
668 ng_ether_rcv_lower(hook_p hook, item_p item)
669 {
670         struct mbuf *m;
671         const node_p node = NG_HOOK_NODE(hook);
672         const priv_p priv = NG_NODE_PRIVATE(node);
673         struct ifnet *const ifp = priv->ifp;
674
675         NGI_GET_M(item, m);
676         NG_FREE_ITEM(item);
677
678         /* Check whether interface is ready for packets */
679
680         if (!((ifp->if_flags & IFF_UP) &&
681             (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
682                 NG_FREE_M(m);
683                 return (ENETDOWN);
684         }
685
686         /* Make sure header is fully pulled up */
687         if (m->m_pkthdr.len < sizeof(struct ether_header)) {
688                 NG_FREE_M(m);
689                 return (EINVAL);
690         }
691         if (m->m_len < sizeof(struct ether_header)
692             && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
693                 return (ENOBUFS);
694
695         /* Drop in the MAC address if desired */
696         if (priv->autoSrcAddr) {
697
698                 /* Make the mbuf writable if it's not already */
699                 if (!M_WRITABLE(m)
700                     && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
701                         return (ENOBUFS);
702
703                 /* Overwrite source MAC address */
704                 bcopy(IF_LLADDR(ifp),
705                     mtod(m, struct ether_header *)->ether_shost,
706                     ETHER_ADDR_LEN);
707         }
708
709         /* Send it on its way */
710         return ether_output_frame(ifp, m);
711 }
712
713 /*
714  * Handle an mbuf received on the "upper" hook.
715  */
716 static int
717 ng_ether_rcv_upper(hook_p hook, item_p item)
718 {
719         struct mbuf *m;
720         const node_p node = NG_HOOK_NODE(hook);
721         const priv_p priv = NG_NODE_PRIVATE(node);
722         struct ifnet *ifp = priv->ifp;
723
724         NGI_GET_M(item, m);
725         NG_FREE_ITEM(item);
726
727         /* Check length and pull off header */
728         if (m->m_pkthdr.len < sizeof(struct ether_header)) {
729                 NG_FREE_M(m);
730                 return (EINVAL);
731         }
732         if (m->m_len < sizeof(struct ether_header) &&
733             (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
734                 return (ENOBUFS);
735
736         m->m_pkthdr.rcvif = ifp;
737
738         /* Pass the packet to the bridge, it may come back to us */
739         if (ifp->if_bridge) {
740                 BRIDGE_INPUT(ifp, m);
741                 if (m == NULL)
742                         return (0);
743         }
744
745         /* Route packet back in */
746         ether_demux(ifp, m);
747         return (0);
748 }
749
750 /*
751  * Shutdown node. This resets the node but does not remove it
752  * unless the REALLY_DIE flag is set.
753  */
754 static int
755 ng_ether_shutdown(node_p node)
756 {
757         const priv_p priv = NG_NODE_PRIVATE(node);
758
759         if (node->nd_flags & NGF_REALLY_DIE) {
760                 /*
761                  * WE came here because the ethernet card is being unloaded,
762                  * so stop being persistant.
763                  * Actually undo all the things we did on creation.
764                  * Assume the ifp has already been freed.
765                  */
766                 NG_NODE_SET_PRIVATE(node, NULL);
767                 free(priv, M_NETGRAPH);         
768                 NG_NODE_UNREF(node);    /* free node itself */
769                 return (0);
770         }
771         if (priv->promisc) {            /* disable promiscuous mode */
772                 (void)ifpromisc(priv->ifp, 0);
773                 priv->promisc = 0;
774         }
775         priv->autoSrcAddr = 1;          /* reset auto-src-addr flag */
776         NG_NODE_REVIVE(node);           /* Signal ng_rmnode we are persisant */
777
778         return (0);
779 }
780
781 /*
782  * Hook disconnection.
783  */
784 static int
785 ng_ether_disconnect(hook_p hook)
786 {
787         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
788
789         if (hook == priv->upper) {
790                 priv->upper = NULL;
791                 if (priv->ifp != NULL)          /* restore h/w csum */
792                         priv->ifp->if_hwassist = priv->hwassist;
793         } else if (hook == priv->lower)
794                 priv->lower = NULL;
795         else if (hook == priv->orphan)
796                 priv->orphan = NULL;
797         else
798                 panic("%s: weird hook", __func__);
799         if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
800         && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
801                 ng_rmnode_self(NG_HOOK_NODE(hook));     /* reset node */
802         return (0);
803 }
804
805 /******************************************************************
806                         INITIALIZATION
807 ******************************************************************/
808
809 /*
810  * Handle loading and unloading for this node type.
811  */
812 static int
813 ng_ether_mod_event(module_t mod, int event, void *data)
814 {
815         int error = 0;
816         int s;
817
818         s = splnet();
819         switch (event) {
820         case MOD_LOAD:
821
822                 /* Register function hooks */
823                 if (ng_ether_attach_p != NULL) {
824                         error = EEXIST;
825                         break;
826                 }
827                 ng_ether_attach_p = ng_ether_attach;
828                 ng_ether_detach_p = ng_ether_detach;
829                 ng_ether_output_p = ng_ether_output;
830                 ng_ether_input_p = ng_ether_input;
831                 ng_ether_input_orphan_p = ng_ether_input_orphan;
832                 ng_ether_link_state_p = ng_ether_link_state;
833
834                 ng_ether_ifnet_arrival_cookie =
835                     EVENTHANDLER_REGISTER(ifnet_arrival_event,
836                     ng_ether_ifnet_arrival_event, NULL, EVENTHANDLER_PRI_ANY);
837                 break;
838
839         case MOD_UNLOAD:
840
841                 /*
842                  * Note that the base code won't try to unload us until
843                  * all nodes have been removed, and that can't happen
844                  * until all Ethernet interfaces are removed. In any
845                  * case, we know there are no nodes left if the action
846                  * is MOD_UNLOAD, so there's no need to detach any nodes.
847                  */
848
849                 EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
850                     ng_ether_ifnet_arrival_cookie);
851
852                 /* Unregister function hooks */
853                 ng_ether_attach_p = NULL;
854                 ng_ether_detach_p = NULL;
855                 ng_ether_output_p = NULL;
856                 ng_ether_input_p = NULL;
857                 ng_ether_input_orphan_p = NULL;
858                 ng_ether_link_state_p = NULL;
859                 break;
860
861         default:
862                 error = EOPNOTSUPP;
863                 break;
864         }
865         splx(s);
866         return (error);
867 }
868
869 static void
870 vnet_ng_ether_init(const void *unused)
871 {
872         struct ifnet *ifp;
873
874         /* If module load was rejected, don't attach to vnets. */
875         if (ng_ether_attach_p != ng_ether_attach)
876                 return;
877
878         /* Create nodes for any already-existing Ethernet interfaces. */
879         IFNET_RLOCK();
880         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
881                 if (ifp->if_type == IFT_ETHER
882                     || ifp->if_type == IFT_L2VLAN)
883                         ng_ether_attach(ifp);
884         }
885         IFNET_RUNLOCK();
886 }
887 VNET_SYSINIT(vnet_ng_ether_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
888     vnet_ng_ether_init, NULL);