7 * Copyright (c) 1996-2000 Whistle Communications, Inc.
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.
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
39 * Authors: Archie Cobbs <archie@freebsd.org>
40 * Julian Elischer <julian@freebsd.org>
46 * ng_ether(4) netgraph node type
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
54 #include <sys/errno.h>
56 #include <sys/syslog.h>
57 #include <sys/socket.h>
58 #include <sys/taskqueue.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>
69 #include <netgraph/ng_message.h>
70 #include <netgraph/netgraph.h>
71 #include <netgraph/ng_parse.h>
72 #include <netgraph/ng_ether.h>
74 MODULE_VERSION(ng_ether, 1);
76 #define IFP2NG(ifp) (IFP2AC((ifp))->ac_netgraph)
78 /* Per-node private data */
80 struct ifnet *ifp; /* associated interface */
81 hook_p upper; /* upper hook connection */
82 hook_p lower; /* lower hook connection */
83 hook_p orphan; /* orphan hook connection */
84 u_char autoSrcAddr; /* always overwrite source address */
85 u_char promisc; /* promiscuous mode enabled */
86 u_long hwassist; /* hardware checksum capabilities */
87 u_int flags; /* flags e.g. really die */
89 typedef struct private *priv_p;
91 /* Hook pointers used by if_ethersubr.c to callback to netgraph */
92 extern void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
93 extern void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
94 extern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
95 extern void (*ng_ether_attach_p)(struct ifnet *ifp);
96 extern void (*ng_ether_detach_p)(struct ifnet *ifp);
97 extern void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
99 /* Functional hooks called from if_ethersubr.c */
100 static void ng_ether_input(struct ifnet *ifp, struct mbuf **mp);
101 static void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m);
102 static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
103 static void ng_ether_attach(struct ifnet *ifp);
104 static void ng_ether_detach(struct ifnet *ifp);
105 static void ng_ether_link_state(struct ifnet *ifp, int state);
107 /* Other functions */
108 static int ng_ether_rcv_lower(hook_p node, item_p item);
109 static int ng_ether_rcv_upper(hook_p node, item_p item);
111 /* Netgraph node methods */
112 static ng_constructor_t ng_ether_constructor;
113 static ng_rcvmsg_t ng_ether_rcvmsg;
114 static ng_shutdown_t ng_ether_shutdown;
115 static ng_newhook_t ng_ether_newhook;
116 static ng_rcvdata_t ng_ether_rcvdata;
117 static ng_disconnect_t ng_ether_disconnect;
118 static int ng_ether_mod_event(module_t mod, int event, void *data);
120 /* List of commands and how to convert arguments to/from ASCII */
121 static const struct ng_cmdlist ng_ether_cmdlist[] = {
124 NGM_ETHER_GET_IFNAME,
127 &ng_parse_string_type
131 NGM_ETHER_GET_IFINDEX,
138 NGM_ETHER_GET_ENADDR,
141 &ng_parse_enaddr_type
145 NGM_ETHER_SET_ENADDR,
147 &ng_parse_enaddr_type,
152 NGM_ETHER_GET_PROMISC,
159 NGM_ETHER_SET_PROMISC,
161 &ng_parse_int32_type,
166 NGM_ETHER_GET_AUTOSRC,
173 NGM_ETHER_SET_AUTOSRC,
175 &ng_parse_int32_type,
182 &ng_parse_enaddr_type,
189 &ng_parse_enaddr_type,
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,
214 NETGRAPH_INIT(ether, &ng_ether_typestruct);
216 /******************************************************************
217 ETHERNET FUNCTION HOOKS
218 ******************************************************************/
221 * Handle a packet that has come in on an interface. We get to
222 * look at it here before any upper layer protocols do.
225 ng_ether_input(struct ifnet *ifp, struct mbuf **mp)
227 const node_p node = IFP2NG(ifp);
228 const priv_p priv = NG_NODE_PRIVATE(node);
231 /* If "lower" hook not connected, let packet continue */
232 if (priv->lower == NULL)
234 NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */
238 * Handle a packet that has come in on an interface, and which
239 * does not match any of our known protocols (an ``orphan'').
242 ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m)
244 const node_p node = IFP2NG(ifp);
245 const priv_p priv = NG_NODE_PRIVATE(node);
248 /* If "orphan" hook not connected, discard packet */
249 if (priv->orphan == NULL) {
253 NG_SEND_DATA_ONLY(error, priv->orphan, m);
257 * Handle a packet that is going out on an interface.
258 * The Ethernet header is already attached to the mbuf.
261 ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
263 const node_p node = IFP2NG(ifp);
264 const priv_p priv = NG_NODE_PRIVATE(node);
267 /* If "upper" hook not connected, let packet continue */
268 if (priv->upper == NULL)
271 /* Send it out "upper" hook */
272 NG_OUTBOUND_THREAD_REF();
273 NG_SEND_DATA_ONLY(error, priv->upper, *mp);
274 NG_OUTBOUND_THREAD_UNREF();
279 * A new Ethernet interface has been attached.
280 * Create a new node for it, etc.
283 ng_ether_attach(struct ifnet *ifp)
289 * Do not create / attach an ether node to this ifnet if
290 * a netgraph node with the same name already exists.
291 * This should prevent ether nodes to become attached to
292 * eiface nodes, which may be problematic due to naming
295 if ((node = ng_name2noderef(NULL, ifp->if_xname)) != NULL) {
301 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
302 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
303 log(LOG_ERR, "%s: can't %s for %s\n",
304 __func__, "create node", ifp->if_xname);
308 /* Allocate private data */
309 priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
311 log(LOG_ERR, "%s: can't %s for %s\n",
312 __func__, "allocate memory", ifp->if_xname);
316 NG_NODE_SET_PRIVATE(node, priv);
319 priv->hwassist = ifp->if_hwassist;
321 /* Try to give the node the same name as the interface */
322 if (ng_name_node(node, ifp->if_xname) != 0) {
323 log(LOG_WARNING, "%s: can't name node %s\n",
324 __func__, ifp->if_xname);
329 * An Ethernet interface is being detached.
330 * REALLY Destroy its node.
333 ng_ether_detach(struct ifnet *ifp)
335 const node_p node = IFP2NG(ifp);
336 const priv_p priv = NG_NODE_PRIVATE(node);
338 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
339 NG_NODE_REALLY_DIE(node); /* Force real removal of node */
341 * We can't assume the ifnet is still around when we run shutdown
342 * So zap it now. XXX We HOPE that anything running at this time
343 * handles it (as it should in the non netgraph case).
346 priv->ifp = NULL; /* XXX race if interrupted an output packet */
347 ng_rmnode_self(node); /* remove all netgraph parts */
351 * Notify graph about link event.
352 * if_link_state_change() has already checked that the state has changed.
355 ng_ether_link_state(struct ifnet *ifp, int state)
357 const node_p node = IFP2NG(ifp);
358 const priv_p priv = NG_NODE_PRIVATE(node);
360 int cmd, dummy_error = 0;
362 if (state == LINK_STATE_UP)
363 cmd = NGM_LINK_IS_UP;
364 else if (state == LINK_STATE_DOWN)
365 cmd = NGM_LINK_IS_DOWN;
369 if (priv->lower != NULL) {
370 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
372 NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0);
374 if (priv->orphan != NULL) {
375 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
377 NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->orphan, 0);
381 /******************************************************************
382 NETGRAPH NODE METHODS
383 ******************************************************************/
386 * It is not possible or allowable to create a node of this type.
387 * Nodes get created when the interface is attached (or, when
388 * this node type's KLD is loaded).
391 ng_ether_constructor(node_p node)
397 * Check for attaching a new hook.
400 ng_ether_newhook(node_p node, hook_p hook, const char *name)
402 const priv_p priv = NG_NODE_PRIVATE(node);
405 /* Divert hook is an alias for lower */
406 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
407 name = NG_ETHER_HOOK_LOWER;
410 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) {
411 hookptr = &priv->upper;
412 NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_upper);
413 NG_HOOK_SET_TO_INBOUND(hook);
414 } else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
415 hookptr = &priv->lower;
416 NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower);
417 } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
418 hookptr = &priv->orphan;
419 NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower);
423 /* Check if already connected (shouldn't be, but doesn't hurt) */
424 if (*hookptr != NULL)
427 /* Disable hardware checksums while 'upper' hook is connected */
428 if (hookptr == &priv->upper)
429 priv->ifp->if_hwassist = 0;
430 NG_HOOK_HI_STACK(hook);
437 * Receive an incoming control message.
440 ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
442 const priv_p priv = NG_NODE_PRIVATE(node);
443 struct ng_mesg *resp = NULL;
447 NGI_GET_MSG(item, msg);
448 switch (msg->header.typecookie) {
449 case NGM_ETHER_COOKIE:
450 switch (msg->header.cmd) {
451 case NGM_ETHER_GET_IFNAME:
452 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
457 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
459 case NGM_ETHER_GET_IFINDEX:
460 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
465 *((u_int32_t *)resp->data) = priv->ifp->if_index;
467 case NGM_ETHER_GET_ENADDR:
468 NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
473 bcopy(IF_LLADDR(priv->ifp),
474 resp->data, ETHER_ADDR_LEN);
476 case NGM_ETHER_SET_ENADDR:
478 if (msg->header.arglen != ETHER_ADDR_LEN) {
482 error = if_setlladdr(priv->ifp,
483 (u_char *)msg->data, ETHER_ADDR_LEN);
484 EVENTHANDLER_INVOKE(iflladdr_event, priv->ifp);
487 case NGM_ETHER_GET_PROMISC:
488 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
493 *((u_int32_t *)resp->data) = priv->promisc;
495 case NGM_ETHER_SET_PROMISC:
499 if (msg->header.arglen != sizeof(u_int32_t)) {
503 want = !!*((u_int32_t *)msg->data);
504 if (want ^ priv->promisc) {
505 if ((error = ifpromisc(priv->ifp, want)) != 0)
507 priv->promisc = want;
511 case NGM_ETHER_GET_AUTOSRC:
512 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
517 *((u_int32_t *)resp->data) = priv->autoSrcAddr;
519 case NGM_ETHER_SET_AUTOSRC:
520 if (msg->header.arglen != sizeof(u_int32_t)) {
524 priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
526 case NGM_ETHER_ADD_MULTI:
528 struct sockaddr_dl sa_dl;
529 struct ifmultiaddr *ifma;
531 if (msg->header.arglen != ETHER_ADDR_LEN) {
535 bzero(&sa_dl, sizeof(struct sockaddr_dl));
536 sa_dl.sdl_len = sizeof(struct sockaddr_dl);
537 sa_dl.sdl_family = AF_LINK;
538 sa_dl.sdl_alen = ETHER_ADDR_LEN;
539 bcopy((void *)msg->data, LLADDR(&sa_dl),
542 * Netgraph is only permitted to join groups once
543 * via the if_addmulti() KPI, because it cannot hold
544 * struct ifmultiaddr * between calls. It may also
545 * lose a race while we check if the membership
548 if_maddr_rlock(priv->ifp);
549 ifma = if_findmulti(priv->ifp,
550 (struct sockaddr *)&sa_dl);
551 if_maddr_runlock(priv->ifp);
555 error = if_addmulti(priv->ifp,
556 (struct sockaddr *)&sa_dl, &ifma);
560 case NGM_ETHER_DEL_MULTI:
562 struct sockaddr_dl sa_dl;
564 if (msg->header.arglen != ETHER_ADDR_LEN) {
568 bzero(&sa_dl, sizeof(struct sockaddr_dl));
569 sa_dl.sdl_len = sizeof(struct sockaddr_dl);
570 sa_dl.sdl_family = AF_LINK;
571 sa_dl.sdl_alen = ETHER_ADDR_LEN;
572 bcopy((void *)msg->data, LLADDR(&sa_dl),
574 error = if_delmulti(priv->ifp,
575 (struct sockaddr *)&sa_dl);
578 case NGM_ETHER_DETACH:
579 ng_ether_detach(priv->ifp);
590 NG_RESPOND_MSG(error, node, item, resp);
596 * Receive data on a hook.
597 * Since we use per-hook recveive methods this should never be called.
600 ng_ether_rcvdata(hook_p hook, item_p item)
604 panic("%s: weird hook", __func__);
608 * Handle an mbuf received on the "lower" or "orphan" hook.
611 ng_ether_rcv_lower(hook_p hook, item_p item)
614 const node_p node = NG_HOOK_NODE(hook);
615 const priv_p priv = NG_NODE_PRIVATE(node);
616 struct ifnet *const ifp = priv->ifp;
621 /* Check whether interface is ready for packets */
623 if (!((ifp->if_flags & IFF_UP) &&
624 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
629 /* Make sure header is fully pulled up */
630 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
634 if (m->m_len < sizeof(struct ether_header)
635 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
638 /* Drop in the MAC address if desired */
639 if (priv->autoSrcAddr) {
641 /* Make the mbuf writable if it's not already */
643 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
646 /* Overwrite source MAC address */
647 bcopy(IF_LLADDR(ifp),
648 mtod(m, struct ether_header *)->ether_shost,
652 /* Send it on its way */
653 return ether_output_frame(ifp, m);
657 * Handle an mbuf received on the "upper" hook.
660 ng_ether_rcv_upper(hook_p hook, item_p item)
663 const node_p node = NG_HOOK_NODE(hook);
664 const priv_p priv = NG_NODE_PRIVATE(node);
665 struct ifnet *ifp = priv->ifp;
670 /* Check length and pull off header */
671 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
675 if (m->m_len < sizeof(struct ether_header) &&
676 (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
679 m->m_pkthdr.rcvif = ifp;
681 /* Pass the packet to the bridge, it may come back to us */
682 if (ifp->if_bridge) {
683 BRIDGE_INPUT(ifp, m);
688 /* Route packet back in */
694 * Shutdown node. This resets the node but does not remove it
695 * unless the REALLY_DIE flag is set.
698 ng_ether_shutdown(node_p node)
700 const priv_p priv = NG_NODE_PRIVATE(node);
702 if (node->nd_flags & NGF_REALLY_DIE) {
704 * WE came here because the ethernet card is being unloaded,
705 * so stop being persistant.
706 * Actually undo all the things we did on creation.
707 * Assume the ifp has already been freed.
709 NG_NODE_SET_PRIVATE(node, NULL);
710 free(priv, M_NETGRAPH);
711 NG_NODE_UNREF(node); /* free node itself */
714 if (priv->promisc) { /* disable promiscuous mode */
715 (void)ifpromisc(priv->ifp, 0);
718 NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */
724 * Hook disconnection.
727 ng_ether_disconnect(hook_p hook)
729 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
731 if (hook == priv->upper) {
733 if (priv->ifp != NULL) /* restore h/w csum */
734 priv->ifp->if_hwassist = priv->hwassist;
735 } else if (hook == priv->lower)
737 else if (hook == priv->orphan)
740 panic("%s: weird hook", __func__);
741 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
742 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
743 ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */
747 /******************************************************************
749 ******************************************************************/
752 * Handle loading and unloading for this node type.
755 ng_ether_mod_event(module_t mod, int event, void *data)
762 /* Register function hooks */
763 if (ng_ether_attach_p != NULL) {
767 ng_ether_attach_p = ng_ether_attach;
768 ng_ether_detach_p = ng_ether_detach;
769 ng_ether_output_p = ng_ether_output;
770 ng_ether_input_p = ng_ether_input;
771 ng_ether_input_orphan_p = ng_ether_input_orphan;
772 ng_ether_link_state_p = ng_ether_link_state;
779 * Note that the base code won't try to unload us until
780 * all nodes have been removed, and that can't happen
781 * until all Ethernet interfaces are removed. In any
782 * case, we know there are no nodes left if the action
783 * is MOD_UNLOAD, so there's no need to detach any nodes.
786 /* Unregister function hooks */
787 ng_ether_attach_p = NULL;
788 ng_ether_detach_p = NULL;
789 ng_ether_output_p = NULL;
790 ng_ether_input_p = NULL;
791 ng_ether_input_orphan_p = NULL;
792 ng_ether_link_state_p = NULL;
803 vnet_ng_ether_init(const void *unused)
807 /* If module load was rejected, don't attach to vnets. */
808 if (ng_ether_attach_p != ng_ether_attach)
811 /* Create nodes for any already-existing Ethernet interfaces. */
813 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
814 if (ifp->if_type == IFT_ETHER
815 || ifp->if_type == IFT_L2VLAN)
816 ng_ether_attach(ifp);
820 VNET_SYSINIT(vnet_ng_ether_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
821 vnet_ng_ether_init, NULL);