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>
42 * $FreeBSD: src/sys/netgraph/ng_ether.c,v 1.62 2007/03/20 00:36:10 bms Exp $
43 * $DragonFly: src/sys/netgraph7/ng_ether.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
47 * ng_ether(4) netgraph node type
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
55 #include <sys/errno.h>
56 #include <sys/syslog.h>
57 #include <sys/socket.h>
60 #include <net/if_dl.h>
61 #include <net/if_types.h>
62 #include <net/if_arp.h>
63 #include <net/if_var.h>
64 #include <net/ethernet.h>
65 #include <net/if_bridgevar.h>
67 #include "ng_message.h"
72 #define IFP2NG(ifp) (IFP2AC((ifp))->ac_netgraph)
74 /* Per-node private data */
76 struct ifnet *ifp; /* associated interface */
77 hook_p upper; /* upper hook connection */
78 hook_p lower; /* lower hook connection */
79 hook_p orphan; /* orphan hook connection */
80 u_char autoSrcAddr; /* always overwrite source address */
81 u_char promisc; /* promiscuous mode enabled */
82 u_long hwassist; /* hardware checksum capabilities */
83 u_int flags; /* flags e.g. really die */
85 typedef struct private *priv_p;
87 /* Hook pointers used by if_ethersubr.c to callback to netgraph */
88 extern void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
89 extern void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
90 extern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
91 extern void (*ng_ether_attach_p)(struct ifnet *ifp);
92 extern void (*ng_ether_detach_p)(struct ifnet *ifp);
93 extern void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
95 /* Functional hooks called from if_ethersubr.c */
96 static void ng_ether_input(struct ifnet *ifp, struct mbuf **mp);
97 static void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m);
98 static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
99 static void ng_ether_attach(struct ifnet *ifp);
100 static void ng_ether_detach(struct ifnet *ifp);
101 static void ng_ether_link_state(struct ifnet *ifp, int state);
103 /* Other functions */
104 static int ng_ether_rcv_lower(node_p node, struct mbuf *m);
105 static int ng_ether_rcv_upper(node_p node, struct mbuf *m);
107 /* Netgraph node methods */
108 static ng_constructor_t ng_ether_constructor;
109 static ng_rcvmsg_t ng_ether_rcvmsg;
110 static ng_shutdown_t ng_ether_shutdown;
111 static ng_newhook_t ng_ether_newhook;
112 static ng_rcvdata_t ng_ether_rcvdata;
113 static ng_disconnect_t ng_ether_disconnect;
114 static int ng_ether_mod_event(module_t mod, int event, void *data);
116 /* List of commands and how to convert arguments to/from ASCII */
117 static const struct ng_cmdlist ng_ether_cmdlist[] = {
120 NGM_ETHER_GET_IFNAME,
123 &ng_parse_string_type
127 NGM_ETHER_GET_IFINDEX,
134 NGM_ETHER_GET_ENADDR,
137 &ng_parse_enaddr_type
141 NGM_ETHER_SET_ENADDR,
143 &ng_parse_enaddr_type,
148 NGM_ETHER_GET_PROMISC,
155 NGM_ETHER_SET_PROMISC,
157 &ng_parse_int32_type,
162 NGM_ETHER_GET_AUTOSRC,
169 NGM_ETHER_SET_AUTOSRC,
171 &ng_parse_int32_type,
178 &ng_parse_enaddr_type,
185 &ng_parse_enaddr_type,
198 static struct ng_type ng_ether_typestruct = {
199 .version = NG_ABI_VERSION,
200 .name = NG_ETHER_NODE_TYPE,
201 .mod_event = ng_ether_mod_event,
202 .constructor = ng_ether_constructor,
203 .rcvmsg = ng_ether_rcvmsg,
204 .shutdown = ng_ether_shutdown,
205 .newhook = ng_ether_newhook,
206 .rcvdata = ng_ether_rcvdata,
207 .disconnect = ng_ether_disconnect,
208 .cmdlist = ng_ether_cmdlist,
210 NETGRAPH_INIT(ether, &ng_ether_typestruct);
212 /******************************************************************
213 ETHERNET FUNCTION HOOKS
214 ******************************************************************/
217 * Handle a packet that has come in on an interface. We get to
218 * look at it here before any upper layer protocols do.
220 * NOTE: this function will get called at splimp()
223 ng_ether_input(struct ifnet *ifp, struct mbuf **mp)
225 const node_p node = IFP2NG(ifp);
226 const priv_p priv = NG_NODE_PRIVATE(node);
229 /* If "lower" hook not connected, let packet continue */
230 if (priv->lower == NULL)
232 NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */
236 * Handle a packet that has come in on an interface, and which
237 * does not match any of our known protocols (an ``orphan'').
239 * NOTE: this function will get called at splimp()
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_SEND_DATA_ONLY(error, priv->upper, *mp);
277 * A new Ethernet interface has been attached.
278 * Create a new node for it, etc.
281 ng_ether_attach(struct ifnet *ifp)
287 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
288 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
289 log(LOG_ERR, "%s: can't %s for %s\n",
290 __func__, "create node", ifp->if_xname);
294 /* Allocate private data */
295 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_WAITOK | M_NULLOK | M_ZERO);
297 log(LOG_ERR, "%s: can't %s for %s\n",
298 __func__, "allocate memory", ifp->if_xname);
302 NG_NODE_SET_PRIVATE(node, priv);
305 priv->hwassist = ifp->if_hwassist;
307 /* Try to give the node the same name as the interface */
308 if (ng_name_node(node, ifp->if_xname) != 0) {
309 log(LOG_WARNING, "%s: can't name node %s\n",
310 __func__, ifp->if_xname);
315 * An Ethernet interface is being detached.
316 * REALLY Destroy its node.
319 ng_ether_detach(struct ifnet *ifp)
321 const node_p node = IFP2NG(ifp);
322 const priv_p priv = NG_NODE_PRIVATE(node);
324 NG_NODE_REALLY_DIE(node); /* Force real removal of node */
326 * We can't assume the ifnet is still around when we run shutdown
327 * So zap it now. XXX We HOPE that anything running at this time
328 * handles it (as it should in the non netgraph case).
331 priv->ifp = NULL; /* XXX race if interrupted an output packet */
332 ng_rmnode_self(node); /* remove all netgraph parts */
336 * Notify graph about link event.
337 * if_link_state_change() has already checked that the state has changed.
340 ng_ether_link_state(struct ifnet *ifp, int state)
342 const node_p node = IFP2NG(ifp);
343 const priv_p priv = NG_NODE_PRIVATE(node);
345 int cmd, dummy_error = 0;
347 if (priv->lower == NULL)
350 if (state == LINK_STATE_UP)
351 cmd = NGM_LINK_IS_UP;
352 else if (state == LINK_STATE_DOWN)
353 cmd = NGM_LINK_IS_DOWN;
357 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_WAITOK | M_NULLOK);
359 NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0);
362 /******************************************************************
363 NETGRAPH NODE METHODS
364 ******************************************************************/
367 * It is not possible or allowable to create a node of this type.
368 * Nodes get created when the interface is attached (or, when
369 * this node type's KLD is loaded).
372 ng_ether_constructor(node_p node)
378 * Check for attaching a new hook.
381 ng_ether_newhook(node_p node, hook_p hook, const char *name)
383 const priv_p priv = NG_NODE_PRIVATE(node);
386 /* Divert hook is an alias for lower */
387 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
388 name = NG_ETHER_HOOK_LOWER;
391 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
392 hookptr = &priv->upper;
393 else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0)
394 hookptr = &priv->lower;
395 else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0)
396 hookptr = &priv->orphan;
400 /* Check if already connected (shouldn't be, but doesn't hurt) */
401 if (*hookptr != NULL)
404 /* Disable hardware checksums while 'upper' hook is connected */
405 if (hookptr == &priv->upper)
406 priv->ifp->if_hwassist = 0;
414 * Receive an incoming control message.
417 ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
419 const priv_p priv = NG_NODE_PRIVATE(node);
420 struct ng_mesg *resp = NULL;
424 NGI_GET_MSG(item, msg);
425 switch (msg->header.typecookie) {
426 case NGM_ETHER_COOKIE:
427 switch (msg->header.cmd) {
428 case NGM_ETHER_GET_IFNAME:
429 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_WAITOK | M_NULLOK);
434 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
436 case NGM_ETHER_GET_IFINDEX:
437 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_WAITOK | M_NULLOK);
442 *((u_int32_t *)resp->data) = priv->ifp->if_index;
444 case NGM_ETHER_GET_ENADDR:
445 NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_WAITOK | M_NULLOK);
450 bcopy(IF_LLADDR(priv->ifp),
451 resp->data, ETHER_ADDR_LEN);
453 case NGM_ETHER_SET_ENADDR:
455 if (msg->header.arglen != ETHER_ADDR_LEN) {
459 error = if_setlladdr(priv->ifp,
460 (u_char *)msg->data, ETHER_ADDR_LEN);
463 case NGM_ETHER_GET_PROMISC:
464 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_WAITOK | M_NULLOK);
469 *((u_int32_t *)resp->data) = priv->promisc;
471 case NGM_ETHER_SET_PROMISC:
475 if (msg->header.arglen != sizeof(u_int32_t)) {
479 want = !!*((u_int32_t *)msg->data);
480 if (want ^ priv->promisc) {
481 if ((error = ifpromisc(priv->ifp, want)) != 0)
483 priv->promisc = want;
487 case NGM_ETHER_GET_AUTOSRC:
488 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_WAITOK | M_NULLOK);
493 *((u_int32_t *)resp->data) = priv->autoSrcAddr;
495 case NGM_ETHER_SET_AUTOSRC:
496 if (msg->header.arglen != sizeof(u_int32_t)) {
500 priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
502 case NGM_ETHER_ADD_MULTI:
504 struct sockaddr_dl sa_dl;
505 struct ifmultiaddr *ifma;
507 if (msg->header.arglen != ETHER_ADDR_LEN) {
511 bzero(&sa_dl, sizeof(struct sockaddr_dl));
512 sa_dl.sdl_len = sizeof(struct sockaddr_dl);
513 sa_dl.sdl_family = AF_LINK;
514 sa_dl.sdl_alen = ETHER_ADDR_LEN;
515 bcopy((void *)msg->data, LLADDR(&sa_dl),
518 * Netgraph is only permitted to join groups once
519 * via the if_addmulti() KPI, because it cannot hold
520 * struct ifmultiaddr * between calls. It may also
521 * lose a race while we check if the membership
524 IF_ADDR_LOCK(priv->ifp);
525 ifma = if_findmulti(priv->ifp,
526 (struct sockaddr *)&sa_dl);
527 IF_ADDR_UNLOCK(priv->ifp);
531 error = if_addmulti(priv->ifp,
532 (struct sockaddr *)&sa_dl, &ifma);
536 case NGM_ETHER_DEL_MULTI:
538 struct sockaddr_dl sa_dl;
540 if (msg->header.arglen != ETHER_ADDR_LEN) {
544 bzero(&sa_dl, sizeof(struct sockaddr_dl));
545 sa_dl.sdl_len = sizeof(struct sockaddr_dl);
546 sa_dl.sdl_family = AF_LINK;
547 sa_dl.sdl_alen = ETHER_ADDR_LEN;
548 bcopy((void *)msg->data, LLADDR(&sa_dl),
550 error = if_delmulti(priv->ifp,
551 (struct sockaddr *)&sa_dl);
554 case NGM_ETHER_DETACH:
555 ng_ether_detach(priv->ifp);
566 NG_RESPOND_MSG(error, node, item, resp);
572 * Receive data on a hook.
575 ng_ether_rcvdata(hook_p hook, item_p item)
577 const node_p node = NG_HOOK_NODE(hook);
578 const priv_p priv = NG_NODE_PRIVATE(node);
584 if (hook == priv->lower || hook == priv->orphan)
585 return ng_ether_rcv_lower(node, m);
586 if (hook == priv->upper)
587 return ng_ether_rcv_upper(node, m);
588 panic("%s: weird hook", __func__);
589 #ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */
595 * Handle an mbuf received on the "lower" or "orphan" hook.
598 ng_ether_rcv_lower(node_p node, struct mbuf *m)
600 const priv_p priv = NG_NODE_PRIVATE(node);
601 struct ifnet *const ifp = priv->ifp;
603 /* Check whether interface is ready for packets */
604 if (!((ifp->if_flags & IFF_UP) &&
605 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
610 /* Make sure header is fully pulled up */
611 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
615 if (m->m_len < sizeof(struct ether_header)
616 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
619 /* Drop in the MAC address if desired */
620 if (priv->autoSrcAddr) {
622 /* Make the mbuf writable if it's not already */
624 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
627 /* Overwrite source MAC address */
628 bcopy(IF_LLADDR(ifp),
629 mtod(m, struct ether_header *)->ether_shost,
633 /* Send it on its way */
634 return ether_output_frame(ifp, m);
638 * Handle an mbuf received on the "upper" hook.
641 ng_ether_rcv_upper(node_p node, struct mbuf *m)
643 const priv_p priv = NG_NODE_PRIVATE(node);
644 struct ifnet *ifp = priv->ifp;
646 /* Check length and pull off header */
647 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
651 if (m->m_len < sizeof(struct ether_header) &&
652 (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
655 m->m_pkthdr.rcvif = ifp;
657 /* Pass the packet to the bridge, it may come back to us */
658 if (ifp->if_bridge) {
659 BRIDGE_INPUT(ifp, m);
664 /* Route packet back in */
670 * Shutdown node. This resets the node but does not remove it
671 * unless the REALLY_DIE flag is set.
674 ng_ether_shutdown(node_p node)
676 const priv_p priv = NG_NODE_PRIVATE(node);
678 if (node->nd_flags & NGF_REALLY_DIE) {
680 * WE came here because the ethernet card is being unloaded,
681 * so stop being persistant.
682 * Actually undo all the things we did on creation.
683 * Assume the ifp has already been freed.
685 NG_NODE_SET_PRIVATE(node, NULL);
686 FREE(priv, M_NETGRAPH);
687 NG_NODE_UNREF(node); /* free node itself */
690 if (priv->promisc) { /* disable promiscuous mode */
691 (void)ifpromisc(priv->ifp, 0);
694 priv->autoSrcAddr = 1; /* reset auto-src-addr flag */
695 NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */
701 * Hook disconnection.
704 ng_ether_disconnect(hook_p hook)
706 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
708 if (hook == priv->upper) {
710 if (priv->ifp != NULL) /* restore h/w csum */
711 priv->ifp->if_hwassist = priv->hwassist;
712 } else if (hook == priv->lower)
714 else if (hook == priv->orphan)
717 panic("%s: weird hook", __func__);
718 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
719 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
720 ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */
724 /******************************************************************
726 ******************************************************************/
729 * Handle loading and unloading for this node type.
732 ng_ether_mod_event(module_t mod, int event, void *data)
742 /* Register function hooks */
743 if (ng_ether_attach_p != NULL) {
747 ng_ether_attach_p = ng_ether_attach;
748 ng_ether_detach_p = ng_ether_detach;
749 ng_ether_output_p = ng_ether_output;
750 ng_ether_input_p = ng_ether_input;
751 ng_ether_input_orphan_p = ng_ether_input_orphan;
752 ng_ether_link_state_p = ng_ether_link_state;
754 /* Create nodes for any already-existing Ethernet interfaces */
756 TAILQ_FOREACH(ifp, &ifnet, if_link) {
757 if (ifp->if_type == IFT_ETHER
758 || ifp->if_type == IFT_L2VLAN)
759 ng_ether_attach(ifp);
767 * Note that the base code won't try to unload us until
768 * all nodes have been removed, and that can't happen
769 * until all Ethernet interfaces are removed. In any
770 * case, we know there are no nodes left if the action
771 * is MOD_UNLOAD, so there's no need to detach any nodes.
774 /* Unregister function hooks */
775 ng_ether_attach_p = NULL;
776 ng_ether_detach_p = NULL;
777 ng_ether_output_p = NULL;
778 ng_ether_input_p = NULL;
779 ng_ether_input_orphan_p = NULL;
780 ng_ether_link_state_p = NULL;