FreeBSD kernel netgraph code
ng_ether.c
Go to the documentation of this file.
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/eventhandler.h>
51#include <sys/systm.h>
52#include <sys/kernel.h>
53#include <sys/malloc.h>
54#include <sys/mbuf.h>
55#include <sys/errno.h>
56#include <sys/proc.h>
57#include <sys/syslog.h>
58#include <sys/socket.h>
59#include <sys/taskqueue.h>
60
61#include <net/if.h>
62#include <net/if_dl.h>
63#include <net/if_types.h>
64#include <net/if_arp.h>
65#include <net/if_var.h>
66#include <net/ethernet.h>
67#include <net/if_bridgevar.h>
68#include <net/vnet.h>
69
70#include <netgraph/ng_message.h>
71#include <netgraph/netgraph.h>
72#include <netgraph/ng_parse.h>
73#include <netgraph/ng_ether.h>
74
75MODULE_VERSION(ng_ether, 1);
76
77#define IFP2NG(ifp) ((ifp)->if_l2com)
78
79/* Per-node private data */
80struct private {
81 struct ifnet *ifp; /* associated interface */
82 hook_p upper; /* upper hook connection */
83 hook_p lower; /* lower hook connection */
84 hook_p orphan; /* orphan hook connection */
85 u_char autoSrcAddr; /* always overwrite source address */
86 u_char promisc; /* promiscuous mode enabled */
87 u_long hwassist; /* hardware checksum capabilities */
88 u_int flags; /* flags e.g. really die */
89};
90typedef struct private *priv_p;
91
92/* Hook pointers used by if_ethersubr.c to callback to netgraph */
93extern void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
94extern void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
95extern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
96extern void (*ng_ether_attach_p)(struct ifnet *ifp);
97extern void (*ng_ether_detach_p)(struct ifnet *ifp);
98extern void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
99
100/* Functional hooks called from if_ethersubr.c */
101static void ng_ether_input(struct ifnet *ifp, struct mbuf **mp);
102static void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m);
103static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
104static void ng_ether_attach(struct ifnet *ifp);
105static void ng_ether_detach(struct ifnet *ifp);
106static void ng_ether_link_state(struct ifnet *ifp, int state);
107
108/* Other functions */
109static int ng_ether_rcv_lower(hook_p node, item_p item);
110static int ng_ether_rcv_upper(hook_p node, item_p item);
111
112/* Netgraph node methods */
119static int ng_ether_mod_event(module_t mod, int event, void *data);
120
121static eventhandler_tag ng_ether_ifnet_arrival_cookie;
122
123/* List of commands and how to convert arguments to/from ASCII */
124static const struct ng_cmdlist ng_ether_cmdlist[] = {
125 {
128 "getifname",
129 NULL,
131 },
132 {
135 "getifindex",
136 NULL,
138 },
139 {
142 "getenaddr",
143 NULL,
145 },
146 {
149 "setenaddr",
151 NULL
152 },
153 {
156 "getpromisc",
157 NULL,
159 },
160 {
163 "setpromisc",
165 NULL
166 },
167 {
170 "getautosrc",
171 NULL,
173 },
174 {
177 "setautosrc",
179 NULL
180 },
181 {
184 "addmulti",
186 NULL
187 },
188 {
191 "delmulti",
193 NULL
194 },
195 {
198 "detach",
199 NULL,
200 NULL
201 },
202 { 0 }
203};
204
207 .name = NG_ETHER_NODE_TYPE,
208 .mod_event = ng_ether_mod_event,
209 .constructor = ng_ether_constructor,
210 .rcvmsg = ng_ether_rcvmsg,
211 .shutdown = ng_ether_shutdown,
212 .newhook = ng_ether_newhook,
213 .rcvdata = ng_ether_rcvdata,
214 .disconnect = ng_ether_disconnect,
215 .cmdlist = ng_ether_cmdlist,
216};
218
219/******************************************************************
220 UTILITY FUNCTIONS
221******************************************************************/
222static void
223ng_ether_sanitize_ifname(const char *ifname, char *name)
224{
225 int i;
226
227 for (i = 0; i < IFNAMSIZ; i++) {
228 if (ifname[i] == '.' || ifname[i] == ':')
229 name[i] = '_';
230 else
231 name[i] = ifname[i];
232 if (name[i] == '\0')
233 break;
234 }
235}
236
237/******************************************************************
238 ETHERNET FUNCTION HOOKS
239******************************************************************/
240
241/*
242 * Handle a packet that has come in on an interface. We get to
243 * look at it here before any upper layer protocols do.
244 */
245static void
246ng_ether_input(struct ifnet *ifp, struct mbuf **mp)
247{
248 const node_p node = IFP2NG(ifp);
249 const priv_p priv = NG_NODE_PRIVATE(node);
250 int error;
251
252 /* If "lower" hook not connected, let packet continue */
253 if (priv->lower == NULL)
254 return;
255 NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */
256}
257
258/*
259 * Handle a packet that has come in on an interface, and which
260 * does not match any of our known protocols (an ``orphan'').
261 */
262static void
263ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m)
264{
265 const node_p node = IFP2NG(ifp);
266 const priv_p priv = NG_NODE_PRIVATE(node);
267 int error;
268
269 /* If "orphan" hook not connected, discard packet */
270 if (priv->orphan == NULL) {
271 m_freem(m);
272 return;
273 }
274 NG_SEND_DATA_ONLY(error, priv->orphan, m);
275}
276
277/*
278 * Handle a packet that is going out on an interface.
279 * The Ethernet header is already attached to the mbuf.
280 */
281static int
282ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
283{
284 const node_p node = IFP2NG(ifp);
285 const priv_p priv = NG_NODE_PRIVATE(node);
286 int error = 0;
287
288 /* If "upper" hook not connected, let packet continue */
289 if (priv->upper == NULL)
290 return (0);
291
292 /* Send it out "upper" hook */
294 NG_SEND_DATA_ONLY(error, priv->upper, *mp);
296 return (error);
297}
298
299/*
300 * A new Ethernet interface has been attached.
301 * Create a new node for it, etc.
302 */
303static void
304ng_ether_attach(struct ifnet *ifp)
305{
306 char name[IFNAMSIZ];
307 priv_p priv;
308 node_p node;
309
310 /*
311 * Do not create / attach an ether node to this ifnet if
312 * a netgraph node with the same name already exists.
313 * This should prevent ether nodes to become attached to
314 * eiface nodes, which may be problematic due to naming
315 * clashes.
316 */
317 ng_ether_sanitize_ifname(ifp->if_xname, name);
318 if ((node = ng_name2noderef(NULL, name)) != 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 }
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 if (ng_name_node(node, name) != 0)
346 log(LOG_WARNING, "%s: can't name node %s\n", __func__, name);
347}
348
349/*
350 * An Ethernet interface is being detached.
351 * REALLY Destroy its node.
352 */
353static void
354ng_ether_detach(struct ifnet *ifp)
355{
356 const node_p node = IFP2NG(ifp);
357 const priv_p priv = NG_NODE_PRIVATE(node);
358
359 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
360 NG_NODE_REALLY_DIE(node); /* Force real removal of node */
361 /*
362 * We can't assume the ifnet is still around when we run shutdown
363 * So zap it now. XXX We HOPE that anything running at this time
364 * handles it (as it should in the non netgraph case).
365 */
366 IFP2NG(ifp) = NULL;
367 priv->ifp = NULL; /* XXX race if interrupted an output packet */
368 ng_rmnode_self(node); /* remove all netgraph parts */
369}
370
371/*
372 * Notify graph about link event.
373 * if_link_state_change() has already checked that the state has changed.
374 */
375static void
376ng_ether_link_state(struct ifnet *ifp, int state)
377{
378 const node_p node = IFP2NG(ifp);
379 const priv_p priv = NG_NODE_PRIVATE(node);
380 struct ng_mesg *msg;
381 int cmd, dummy_error = 0;
382
383 if (state == LINK_STATE_UP)
385 else if (state == LINK_STATE_DOWN)
387 else
388 return;
389
390 if (priv->lower != NULL) {
391 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
392 if (msg != NULL)
393 NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0);
394 }
395 if (priv->orphan != NULL) {
396 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
397 if (msg != NULL)
398 NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->orphan, 0);
399 }
400}
401
402/*
403 * Interface arrival notification handler.
404 * The notification is produced in two cases:
405 * o a new interface arrives
406 * o an existing interface got renamed
407 * Currently the first case is handled by ng_ether_attach via special
408 * hook ng_ether_attach_p.
409 */
410static void
411ng_ether_ifnet_arrival_event(void *arg __unused, struct ifnet *ifp)
412{
413 char name[IFNAMSIZ];
414 node_p node;
415
416 /* Only ethernet interfaces are of interest. */
417 if (ifp->if_type != IFT_ETHER &&
418 ifp->if_type != IFT_L2VLAN &&
419 ifp->if_type != IFT_BRIDGE)
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 */
444static int
446{
447 return (EINVAL);
448}
449
450/*
451 * Check for attaching a new hook.
452 */
453static int
454ng_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)
462
463 /* Which hook? */
464 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) {
465 hookptr = &priv->upper;
468 } else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
469 hookptr = &priv->lower;
471 } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
472 hookptr = &priv->orphan;
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 */
493static int
494ng_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) {
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;
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;
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;
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 break;
539 }
541 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
542 if (resp == NULL) {
543 error = ENOMEM;
544 break;
545 }
546 *((u_int32_t *)resp->data) = priv->promisc;
547 break;
549 {
550 u_char want;
551
552 if (msg->header.arglen != sizeof(u_int32_t)) {
553 error = EINVAL;
554 break;
555 }
556 want = !!*((u_int32_t *)msg->data);
557 if (want ^ priv->promisc) {
558 if ((error = ifpromisc(priv->ifp, want)) != 0)
559 break;
560 priv->promisc = want;
561 }
562 break;
563 }
565 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
566 if (resp == NULL) {
567 error = ENOMEM;
568 break;
569 }
570 *((u_int32_t *)resp->data) = priv->autoSrcAddr;
571 break;
573 if (msg->header.arglen != sizeof(u_int32_t)) {
574 error = EINVAL;
575 break;
576 }
577 priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
578 break;
580 {
581 struct sockaddr_dl sa_dl;
582 struct epoch_tracker et;
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 NET_EPOCH_ENTER(et);
603 ifma = if_findmulti(priv->ifp,
604 (struct sockaddr *)&sa_dl);
605 NET_EPOCH_EXIT(et);
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 }
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 */
653static int
655{
656 NG_FREE_ITEM(item);
657
658 panic("%s: weird hook", __func__);
659}
660
661/*
662 * Handle an mbuf received on the "lower" or "orphan" hook.
663 */
664static int
666{
667 struct mbuf *m;
668 const node_p node = NG_HOOK_NODE(hook);
669 const priv_p priv = NG_NODE_PRIVATE(node);
670 struct ifnet *const ifp = priv->ifp;
671
672 NGI_GET_M(item, m);
673 NG_FREE_ITEM(item);
674
675 /* Check whether interface is ready for packets */
676
677 if (!((ifp->if_flags & IFF_UP) &&
678 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
679 NG_FREE_M(m);
680 return (ENETDOWN);
681 }
682
683 /* Make sure header is fully pulled up */
684 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
685 NG_FREE_M(m);
686 return (EINVAL);
687 }
688 if (m->m_len < sizeof(struct ether_header)
689 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
690 return (ENOBUFS);
691
692 /* Drop in the MAC address if desired */
693 if (priv->autoSrcAddr) {
694 /* Make the mbuf writable if it's not already */
695 if (!M_WRITABLE(m)
696 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
697 return (ENOBUFS);
698
699 /* Overwrite source MAC address */
700 bcopy(IF_LLADDR(ifp),
701 mtod(m, struct ether_header *)->ether_shost,
702 ETHER_ADDR_LEN);
703 }
704
705 /* Send it on its way */
706 return ether_output_frame(ifp, m);
707}
708
709/*
710 * Handle an mbuf received on the "upper" hook.
711 */
712static int
714{
715 struct mbuf *m;
716 const node_p node = NG_HOOK_NODE(hook);
717 const priv_p priv = NG_NODE_PRIVATE(node);
718 struct ifnet *ifp = priv->ifp;
719
720 NGI_GET_M(item, m);
721 NG_FREE_ITEM(item);
722
723 /* Check length and pull off header */
724 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
725 NG_FREE_M(m);
726 return (EINVAL);
727 }
728 if (m->m_len < sizeof(struct ether_header) &&
729 (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
730 return (ENOBUFS);
731
732 m->m_pkthdr.rcvif = ifp;
733
734 /* Pass the packet to the bridge, it may come back to us */
735 if (ifp->if_bridge) {
736 BRIDGE_INPUT(ifp, m);
737 if (m == NULL)
738 return (0);
739 }
740
741 /* Route packet back in */
742 ether_demux(ifp, m);
743 return (0);
744}
745
746/*
747 * Shutdown node. This resets the node but does not remove it
748 * unless the REALLY_DIE flag is set.
749 */
750static int
752{
753 const priv_p priv = NG_NODE_PRIVATE(node);
754
755 if (node->nd_flags & NGF_REALLY_DIE) {
756 /*
757 * The ifnet is going away, perhaps because the driver was
758 * unloaded or its vnet is being torn down.
759 */
760 NG_NODE_SET_PRIVATE(node, NULL);
761 if (priv->ifp != NULL)
762 IFP2NG(priv->ifp) = NULL;
763 free(priv, M_NETGRAPH);
764 NG_NODE_UNREF(node); /* free node itself */
765 return (0);
766 }
767 if (priv->promisc) { /* disable promiscuous mode */
768 (void)ifpromisc(priv->ifp, 0);
769 priv->promisc = 0;
770 }
771 NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */
772
773 return (0);
774}
775
776/*
777 * Hook disconnection.
778 */
779static int
781{
783
784 if (hook == priv->upper) {
785 priv->upper = NULL;
786 if (priv->ifp != NULL) /* restore h/w csum */
787 priv->ifp->if_hwassist = priv->hwassist;
788 } else if (hook == priv->lower)
789 priv->lower = NULL;
790 else if (hook == priv->orphan)
791 priv->orphan = NULL;
792 else
793 panic("%s: weird hook", __func__);
794 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
795 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
796 ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */
797 return (0);
798}
799
800/******************************************************************
801 INITIALIZATION
802******************************************************************/
803
804/*
805 * Handle loading and unloading for this node type.
806 */
807static int
808ng_ether_mod_event(module_t mod, int event, void *data)
809{
810 int error = 0;
811
812 switch (event) {
813 case MOD_LOAD:
814
815 /* Register function hooks */
816 if (ng_ether_attach_p != NULL) {
817 error = EEXIST;
818 break;
819 }
826
828 EVENTHANDLER_REGISTER(ifnet_arrival_event,
829 ng_ether_ifnet_arrival_event, NULL, EVENTHANDLER_PRI_ANY);
830 break;
831
832 case MOD_UNLOAD:
833
834 /*
835 * Note that the base code won't try to unload us until
836 * all nodes have been removed, and that can't happen
837 * until all Ethernet interfaces are removed. In any
838 * case, we know there are no nodes left if the action
839 * is MOD_UNLOAD, so there's no need to detach any nodes.
840 */
841
842 EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
844
845 /* Unregister function hooks */
846 ng_ether_attach_p = NULL;
847 ng_ether_detach_p = NULL;
848 ng_ether_output_p = NULL;
849 ng_ether_input_p = NULL;
852 break;
853
854 default:
855 error = EOPNOTSUPP;
856 break;
857 }
858 return (error);
859}
860
861static void
862vnet_ng_ether_init(const void *unused)
863{
864 struct ifnet *ifp;
865
866 /* If module load was rejected, don't attach to vnets. */
868 return;
869
870 /* Create nodes for any already-existing Ethernet interfaces. */
871 IFNET_RLOCK();
872 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
873 if (ifp->if_type == IFT_ETHER ||
874 ifp->if_type == IFT_L2VLAN ||
875 ifp->if_type == IFT_BRIDGE)
876 ng_ether_attach(ifp);
877 }
878 IFNET_RUNLOCK();
879}
880VNET_SYSINIT(vnet_ng_ether_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
881 vnet_ng_ether_init, NULL);
#define NG_OUTBOUND_THREAD_UNREF()
Definition: netgraph.h:1216
#define NG_HOOK_NODE(hook)
Definition: netgraph.h:339
#define NG_FREE_M(m)
Definition: netgraph.h:946
#define NG_HOOK_SET_TO_INBOUND(hook)
Definition: netgraph.h:343
#define NG_HOOK_HI_STACK(hook)
Definition: netgraph.h:344
#define NGF_REALLY_DIE
Definition: netgraph.h:394
int ng_rcvmsg_t(node_p node, item_p item, hook_p lasthook)
Definition: netgraph.h:105
int ng_disconnect_t(hook_p hook)
Definition: netgraph.h:107
#define NG_NODE_SET_PRIVATE(node, val)
Definition: netgraph.h:608
#define NG_RESPOND_MSG(error, here, item, resp)
Definition: netgraph.h:1025
#define NG_NODE_IS_VALID(node)
Definition: netgraph.h:610
#define NG_NODE_UNREF(node)
Definition: netgraph.h:607
int ng_rmnode_self(node_p here)
Definition: ng_base.c:1609
#define NG_OUTBOUND_THREAD_REF()
Definition: netgraph.h:1214
#define NG_SEND_DATA_ONLY(error, hook, m)
Definition: netgraph.h:932
#define NG_NODE_REVIVE(node)
Definition: netgraph.h:616
int ng_rcvdata_t(hook_p hook, item_p item)
Definition: netgraph.h:106
#define NG_NODE_REALLY_DIE(node)
Definition: netgraph.h:614
int ng_shutdown_t(node_p node)
Definition: netgraph.h:101
#define NG_FREE_ITEM(item)
Definition: netgraph.h:847
#define NG_HOOK_SET_RCVDATA(hook, val)
Definition: netgraph.h:335
int ng_make_node_common(struct ng_type *typep, node_p *nodep)
Definition: ng_base.c:642
int ng_name_node(node_p node, const char *name)
Definition: ng_base.c:852
int ng_constructor_t(node_p node)
Definition: netgraph.h:99
#define NGI_GET_M(i, m)
Definition: netgraph.h:852
#define NG_FREE_MSG(msg)
Definition: netgraph.h:938
node_p ng_name2noderef(node_p node, const char *name)
Definition: ng_base.c:913
#define NG_ABI_VERSION
Definition: netgraph.h:77
#define NG_NODE_NUMHOOKS(node)
Definition: netgraph.h:615
#define NGI_GET_MSG(i, m)
Definition: netgraph.h:858
#define NG_NODE_PRIVATE(node)
Definition: netgraph.h:609
#define NG_SEND_MSG_HOOK(error, here, msg, hook, retaddr)
Definition: netgraph.h:958
int ng_newhook_t(node_p node, hook_p hook, const char *name)
Definition: netgraph.h:102
static void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m)
Definition: ng_ether.c:263
static void ng_ether_attach(struct ifnet *ifp)
Definition: ng_ether.c:304
static ng_rcvmsg_t ng_ether_rcvmsg
Definition: ng_ether.c:114
static int ng_ether_rcv_upper(hook_p node, item_p item)
Definition: ng_ether.c:713
static void vnet_ng_ether_init(const void *unused)
Definition: ng_ether.c:862
static struct ng_type ng_ether_typestruct
Definition: ng_ether.c:205
static int ng_ether_mod_event(module_t mod, int event, void *data)
Definition: ng_ether.c:808
VNET_SYSINIT(vnet_ng_ether_init, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_ng_ether_init, NULL)
#define IFP2NG(ifp)
Definition: ng_ether.c:77
static void ng_ether_input(struct ifnet *ifp, struct mbuf **mp)
Definition: ng_ether.c:246
static void ng_ether_detach(struct ifnet *ifp)
Definition: ng_ether.c:354
void(* ng_ether_attach_p)(struct ifnet *ifp)
NETGRAPH_INIT(ether, &ng_ether_typestruct)
static ng_newhook_t ng_ether_newhook
Definition: ng_ether.c:116
static void ng_ether_link_state(struct ifnet *ifp, int state)
Definition: ng_ether.c:376
static const struct ng_cmdlist ng_ether_cmdlist[]
Definition: ng_ether.c:124
void(* ng_ether_link_state_p)(struct ifnet *ifp, int state)
static ng_constructor_t ng_ether_constructor
Definition: ng_ether.c:113
static ng_shutdown_t ng_ether_shutdown
Definition: ng_ether.c:115
static void ng_ether_ifnet_arrival_event(void *arg __unused, struct ifnet *ifp)
Definition: ng_ether.c:411
static void ng_ether_sanitize_ifname(const char *ifname, char *name)
Definition: ng_ether.c:223
void(* ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp)
void(* ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m)
MODULE_VERSION(ng_ether, 1)
static eventhandler_tag ng_ether_ifnet_arrival_cookie
Definition: ng_ether.c:121
static ng_rcvdata_t ng_ether_rcvdata
Definition: ng_ether.c:117
void(* ng_ether_detach_p)(struct ifnet *ifp)
static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
Definition: ng_ether.c:282
static ng_disconnect_t ng_ether_disconnect
Definition: ng_ether.c:118
int(* ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp)
struct private * priv_p
Definition: ng_ether.c:90
static int ng_ether_rcv_lower(hook_p node, item_p item)
Definition: ng_ether.c:665
@ NGM_ETHER_DETACH
Definition: ng_ether.h:70
@ NGM_ETHER_SET_PROMISC
Definition: ng_ether.h:65
@ NGM_ETHER_ADD_MULTI
Definition: ng_ether.h:68
@ NGM_ETHER_SET_AUTOSRC
Definition: ng_ether.h:67
@ NGM_ETHER_DEL_MULTI
Definition: ng_ether.h:69
@ NGM_ETHER_SET_ENADDR
Definition: ng_ether.h:63
@ NGM_ETHER_GET_IFINDEX
Definition: ng_ether.h:61
@ NGM_ETHER_GET_AUTOSRC
Definition: ng_ether.h:66
@ NGM_ETHER_GET_ENADDR
Definition: ng_ether.h:62
@ NGM_ETHER_GET_PROMISC
Definition: ng_ether.h:64
@ NGM_ETHER_GET_IFNAME
Definition: ng_ether.h:60
#define NGM_ETHER_COOKIE
Definition: ng_ether.h:50
#define NG_ETHER_HOOK_DIVERT
Definition: ng_ether.h:55
#define NG_ETHER_HOOK_ORPHAN
Definition: ng_ether.h:56
#define NG_ETHER_NODE_TYPE
Definition: ng_ether.h:49
#define NG_ETHER_HOOK_UPPER
Definition: ng_ether.h:54
#define NG_ETHER_HOOK_LOWER
Definition: ng_ether.h:53
#define NG_MKRESPONSE(rsp, msg, len, how)
Definition: ng_message.h:396
#define NGM_LINK_IS_DOWN
Definition: ng_message.h:156
#define NGM_FLOW_COOKIE
Definition: ng_message.h:152
#define NGM_LINK_IS_UP
Definition: ng_message.h:155
#define NG_MKMESSAGE(msg, cookie, cmdid, len, how)
Definition: ng_message.h:378
const struct ng_parse_type ng_parse_int32_type
Definition: ng_parse.c:598
const struct ng_parse_type ng_parse_enaddr_type
Definition: ng_parse.c:1058
const struct ng_parse_type ng_parse_string_type
Definition: ng_parse.c:766
char *const name
Definition: ng_ppp.c:261
state
Definition: ng_pppoe.c:215
cmd
Definition: ng_pppoe.h:74
uint8_t data[]
Definition: ng_ubt_var.h:2
uint8_t event
Definition: ng_ubt_var.h:0
u_int32_t arglen
Definition: ng_message.h:62
u_int32_t typecookie
Definition: ng_message.h:66
struct ng_mesg::ng_msghdr header
char data[]
Definition: ng_message.h:69
int nd_flags
Definition: netgraph.h:368
u_int32_t version
Definition: netgraph.h:1077
Definition: ng_sscfu.c:66
hook_p lower
Definition: ng_sscfu.c:68
hook_p upper
Definition: ng_sscfu.c:67
u_char autoSrcAddr
Definition: ng_ether.c:85
hook_p lower
Definition: ng_ether.c:83
u_long hwassist
Definition: ng_ether.c:87
u_int flags
Definition: ng_ether.c:88
hook_p upper
Definition: ng_ether.c:82
struct ifnet * ifp
Definition: ng_ether.c:81
u_char promisc
Definition: ng_ether.c:86
hook_p orphan
Definition: ng_ether.c:84