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>
55 #include <sys/syslog.h>
56 #include <sys/socket.h>
59 #include <net/if_dl.h>
60 #include <net/if_types.h>
61 #include <net/if_arp.h>
62 #include <net/if_var.h>
63 #include <net/ethernet.h>
64 #include <net/if_bridgevar.h>
66 #include <netgraph/ng_message.h>
67 #include <netgraph/netgraph.h>
68 #include <netgraph/ng_parse.h>
69 #include <netgraph/ng_ether.h>
71 #define IFP2NG(ifp) (IFP2AC((ifp))->ac_netgraph)
73 /* Per-node private data */
75 struct ifnet *ifp; /* associated interface */
76 hook_p upper; /* upper hook connection */
77 hook_p lower; /* lower hook connection */
78 hook_p orphan; /* orphan hook connection */
79 u_char autoSrcAddr; /* always overwrite source address */
80 u_char promisc; /* promiscuous mode enabled */
81 u_long hwassist; /* hardware checksum capabilities */
82 u_int flags; /* flags e.g. really die */
84 typedef struct private *priv_p;
86 /* Hook pointers used by if_ethersubr.c to callback to netgraph */
87 extern void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
88 extern void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
89 extern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
90 extern void (*ng_ether_attach_p)(struct ifnet *ifp);
91 extern void (*ng_ether_detach_p)(struct ifnet *ifp);
92 extern void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
94 /* Functional hooks called from if_ethersubr.c */
95 static void ng_ether_input(struct ifnet *ifp, struct mbuf **mp);
96 static void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m);
97 static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
98 static void ng_ether_attach(struct ifnet *ifp);
99 static void ng_ether_detach(struct ifnet *ifp);
100 static void ng_ether_link_state(struct ifnet *ifp, int state);
102 /* Other functions */
103 static int ng_ether_rcv_lower(node_p node, struct mbuf *m);
104 static int ng_ether_rcv_upper(node_p node, struct mbuf *m);
106 /* Netgraph node methods */
107 static ng_constructor_t ng_ether_constructor;
108 static ng_rcvmsg_t ng_ether_rcvmsg;
109 static ng_shutdown_t ng_ether_shutdown;
110 static ng_newhook_t ng_ether_newhook;
111 static ng_rcvdata_t ng_ether_rcvdata;
112 static ng_disconnect_t ng_ether_disconnect;
113 static int ng_ether_mod_event(module_t mod, int event, void *data);
115 /* List of commands and how to convert arguments to/from ASCII */
116 static const struct ng_cmdlist ng_ether_cmdlist[] = {
119 NGM_ETHER_GET_IFNAME,
122 &ng_parse_string_type
126 NGM_ETHER_GET_IFINDEX,
133 NGM_ETHER_GET_ENADDR,
136 &ng_parse_enaddr_type
140 NGM_ETHER_SET_ENADDR,
142 &ng_parse_enaddr_type,
147 NGM_ETHER_GET_PROMISC,
154 NGM_ETHER_SET_PROMISC,
156 &ng_parse_int32_type,
161 NGM_ETHER_GET_AUTOSRC,
168 NGM_ETHER_SET_AUTOSRC,
170 &ng_parse_int32_type,
177 &ng_parse_enaddr_type,
184 &ng_parse_enaddr_type,
197 static struct ng_type ng_ether_typestruct = {
198 .version = NG_ABI_VERSION,
199 .name = NG_ETHER_NODE_TYPE,
200 .mod_event = ng_ether_mod_event,
201 .constructor = ng_ether_constructor,
202 .rcvmsg = ng_ether_rcvmsg,
203 .shutdown = ng_ether_shutdown,
204 .newhook = ng_ether_newhook,
205 .rcvdata = ng_ether_rcvdata,
206 .disconnect = ng_ether_disconnect,
207 .cmdlist = ng_ether_cmdlist,
209 NETGRAPH_INIT(ether, &ng_ether_typestruct);
211 /******************************************************************
212 ETHERNET FUNCTION HOOKS
213 ******************************************************************/
216 * Handle a packet that has come in on an interface. We get to
217 * look at it here before any upper layer protocols do.
219 * NOTE: this function will get called at splimp()
222 ng_ether_input(struct ifnet *ifp, struct mbuf **mp)
224 const node_p node = IFP2NG(ifp);
225 const priv_p priv = NG_NODE_PRIVATE(node);
228 /* If "lower" hook not connected, let packet continue */
229 if (priv->lower == NULL)
231 NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */
235 * Handle a packet that has come in on an interface, and which
236 * does not match any of our known protocols (an ``orphan'').
238 * NOTE: this function will get called at splimp()
241 ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m)
243 const node_p node = IFP2NG(ifp);
244 const priv_p priv = NG_NODE_PRIVATE(node);
247 /* If "orphan" hook not connected, discard packet */
248 if (priv->orphan == NULL) {
252 NG_SEND_DATA_ONLY(error, priv->orphan, m);
256 * Handle a packet that is going out on an interface.
257 * The Ethernet header is already attached to the mbuf.
260 ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
262 const node_p node = IFP2NG(ifp);
263 const priv_p priv = NG_NODE_PRIVATE(node);
266 /* If "upper" hook not connected, let packet continue */
267 if (priv->upper == NULL)
270 /* Send it out "upper" hook */
271 NG_SEND_DATA_ONLY(error, priv->upper, *mp);
276 * A new Ethernet interface has been attached.
277 * Create a new node for it, etc.
280 ng_ether_attach(struct ifnet *ifp)
286 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
287 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
288 log(LOG_ERR, "%s: can't %s for %s\n",
289 __func__, "create node", ifp->if_xname);
293 /* Allocate private data */
294 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
296 log(LOG_ERR, "%s: can't %s for %s\n",
297 __func__, "allocate memory", ifp->if_xname);
301 NG_NODE_SET_PRIVATE(node, priv);
304 priv->hwassist = ifp->if_hwassist;
306 /* Try to give the node the same name as the interface */
307 if (ng_name_node(node, ifp->if_xname) != 0) {
308 log(LOG_WARNING, "%s: can't name node %s\n",
309 __func__, ifp->if_xname);
314 * An Ethernet interface is being detached.
315 * REALLY Destroy its node.
318 ng_ether_detach(struct ifnet *ifp)
320 const node_p node = IFP2NG(ifp);
321 const priv_p priv = NG_NODE_PRIVATE(node);
323 NG_NODE_REALLY_DIE(node); /* Force real removal of node */
325 * We can't assume the ifnet is still around when we run shutdown
326 * So zap it now. XXX We HOPE that anything running at this time
327 * handles it (as it should in the non netgraph case).
330 priv->ifp = NULL; /* XXX race if interrupted an output packet */
331 ng_rmnode_self(node); /* remove all netgraph parts */
335 * Notify graph about link event.
336 * if_link_state_change() has already checked that the state has changed.
339 ng_ether_link_state(struct ifnet *ifp, int state)
341 const node_p node = IFP2NG(ifp);
342 const priv_p priv = NG_NODE_PRIVATE(node);
344 int cmd, dummy_error = 0;
346 if (priv->lower == NULL)
349 if (state == LINK_STATE_UP)
350 cmd = NGM_LINK_IS_UP;
351 else if (state == LINK_STATE_DOWN)
352 cmd = NGM_LINK_IS_DOWN;
356 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
358 NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0);
361 /******************************************************************
362 NETGRAPH NODE METHODS
363 ******************************************************************/
366 * It is not possible or allowable to create a node of this type.
367 * Nodes get created when the interface is attached (or, when
368 * this node type's KLD is loaded).
371 ng_ether_constructor(node_p node)
377 * Check for attaching a new hook.
380 ng_ether_newhook(node_p node, hook_p hook, const char *name)
382 const priv_p priv = NG_NODE_PRIVATE(node);
385 /* Divert hook is an alias for lower */
386 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
387 name = NG_ETHER_HOOK_LOWER;
390 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
391 hookptr = &priv->upper;
392 else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0)
393 hookptr = &priv->lower;
394 else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0)
395 hookptr = &priv->orphan;
399 /* Check if already connected (shouldn't be, but doesn't hurt) */
400 if (*hookptr != NULL)
403 /* Disable hardware checksums while 'upper' hook is connected */
404 if (hookptr == &priv->upper)
405 priv->ifp->if_hwassist = 0;
413 * Receive an incoming control message.
416 ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
418 const priv_p priv = NG_NODE_PRIVATE(node);
419 struct ng_mesg *resp = NULL;
423 NGI_GET_MSG(item, msg);
424 switch (msg->header.typecookie) {
425 case NGM_ETHER_COOKIE:
426 switch (msg->header.cmd) {
427 case NGM_ETHER_GET_IFNAME:
428 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
433 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
435 case NGM_ETHER_GET_IFINDEX:
436 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
441 *((u_int32_t *)resp->data) = priv->ifp->if_index;
443 case NGM_ETHER_GET_ENADDR:
444 NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
449 bcopy(IF_LLADDR(priv->ifp),
450 resp->data, ETHER_ADDR_LEN);
452 case NGM_ETHER_SET_ENADDR:
454 if (msg->header.arglen != ETHER_ADDR_LEN) {
458 error = if_setlladdr(priv->ifp,
459 (u_char *)msg->data, ETHER_ADDR_LEN);
462 case NGM_ETHER_GET_PROMISC:
463 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
468 *((u_int32_t *)resp->data) = priv->promisc;
470 case NGM_ETHER_SET_PROMISC:
474 if (msg->header.arglen != sizeof(u_int32_t)) {
478 want = !!*((u_int32_t *)msg->data);
479 if (want ^ priv->promisc) {
480 if ((error = ifpromisc(priv->ifp, want)) != 0)
482 priv->promisc = want;
486 case NGM_ETHER_GET_AUTOSRC:
487 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
492 *((u_int32_t *)resp->data) = priv->autoSrcAddr;
494 case NGM_ETHER_SET_AUTOSRC:
495 if (msg->header.arglen != sizeof(u_int32_t)) {
499 priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
501 case NGM_ETHER_ADD_MULTI:
503 struct sockaddr_dl sa_dl;
504 struct ifmultiaddr *ifm;
506 if (msg->header.arglen != ETHER_ADDR_LEN) {
510 bzero(&sa_dl, sizeof(struct sockaddr_dl));
511 sa_dl.sdl_len = sizeof(struct sockaddr_dl);
512 sa_dl.sdl_family = AF_LINK;
513 sa_dl.sdl_alen = ETHER_ADDR_LEN;
514 bcopy((void *)msg->data, LLADDR(&sa_dl),
516 error = if_addmulti(priv->ifp,
517 (struct sockaddr *)&sa_dl, &ifm);
520 case NGM_ETHER_DEL_MULTI:
522 struct sockaddr_dl sa_dl;
524 if (msg->header.arglen != ETHER_ADDR_LEN) {
528 bzero(&sa_dl, sizeof(struct sockaddr_dl));
529 sa_dl.sdl_len = sizeof(struct sockaddr_dl);
530 sa_dl.sdl_family = AF_LINK;
531 sa_dl.sdl_alen = ETHER_ADDR_LEN;
532 bcopy((void *)msg->data, LLADDR(&sa_dl),
534 error = if_delmulti(priv->ifp,
535 (struct sockaddr *)&sa_dl);
538 case NGM_ETHER_DETACH:
539 ng_ether_detach(priv->ifp);
550 NG_RESPOND_MSG(error, node, item, resp);
556 * Receive data on a hook.
559 ng_ether_rcvdata(hook_p hook, item_p item)
561 const node_p node = NG_HOOK_NODE(hook);
562 const priv_p priv = NG_NODE_PRIVATE(node);
568 if (hook == priv->lower || hook == priv->orphan)
569 return ng_ether_rcv_lower(node, m);
570 if (hook == priv->upper)
571 return ng_ether_rcv_upper(node, m);
572 panic("%s: weird hook", __func__);
573 #ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */
579 * Handle an mbuf received on the "lower" or "orphan" hook.
582 ng_ether_rcv_lower(node_p node, struct mbuf *m)
584 const priv_p priv = NG_NODE_PRIVATE(node);
585 struct ifnet *const ifp = priv->ifp;
587 /* Check whether interface is ready for packets */
588 if (!((ifp->if_flags & IFF_UP) &&
589 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
594 /* Make sure header is fully pulled up */
595 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
599 if (m->m_len < sizeof(struct ether_header)
600 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
603 /* Drop in the MAC address if desired */
604 if (priv->autoSrcAddr) {
606 /* Make the mbuf writable if it's not already */
608 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
611 /* Overwrite source MAC address */
612 bcopy(IF_LLADDR(ifp),
613 mtod(m, struct ether_header *)->ether_shost,
617 /* Send it on its way */
618 return ether_output_frame(ifp, m);
622 * Handle an mbuf received on the "upper" hook.
625 ng_ether_rcv_upper(node_p node, struct mbuf *m)
627 const priv_p priv = NG_NODE_PRIVATE(node);
628 struct ifnet *ifp = priv->ifp;
630 /* Check length and pull off header */
631 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
635 if (m->m_len < sizeof(struct ether_header) &&
636 (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
639 m->m_pkthdr.rcvif = ifp;
641 /* Pass the packet to the bridge, it may come back to us */
642 if (ifp->if_bridge) {
643 BRIDGE_INPUT(ifp, m);
648 /* Route packet back in */
654 * Shutdown node. This resets the node but does not remove it
655 * unless the REALLY_DIE flag is set.
658 ng_ether_shutdown(node_p node)
660 const priv_p priv = NG_NODE_PRIVATE(node);
662 if (node->nd_flags & NGF_REALLY_DIE) {
664 * WE came here because the ethernet card is being unloaded,
665 * so stop being persistant.
666 * Actually undo all the things we did on creation.
667 * Assume the ifp has already been freed.
669 NG_NODE_SET_PRIVATE(node, NULL);
670 FREE(priv, M_NETGRAPH);
671 NG_NODE_UNREF(node); /* free node itself */
674 if (priv->promisc) { /* disable promiscuous mode */
675 (void)ifpromisc(priv->ifp, 0);
678 priv->autoSrcAddr = 1; /* reset auto-src-addr flag */
679 NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */
685 * Hook disconnection.
688 ng_ether_disconnect(hook_p hook)
690 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
692 if (hook == priv->upper) {
694 if (priv->ifp != NULL) /* restore h/w csum */
695 priv->ifp->if_hwassist = priv->hwassist;
696 } else if (hook == priv->lower)
698 else if (hook == priv->orphan)
701 panic("%s: weird hook", __func__);
702 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
703 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
704 ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */
708 /******************************************************************
710 ******************************************************************/
713 * Handle loading and unloading for this node type.
716 ng_ether_mod_event(module_t mod, int event, void *data)
726 /* Register function hooks */
727 if (ng_ether_attach_p != NULL) {
731 ng_ether_attach_p = ng_ether_attach;
732 ng_ether_detach_p = ng_ether_detach;
733 ng_ether_output_p = ng_ether_output;
734 ng_ether_input_p = ng_ether_input;
735 ng_ether_input_orphan_p = ng_ether_input_orphan;
736 ng_ether_link_state_p = ng_ether_link_state;
738 /* Create nodes for any already-existing Ethernet interfaces */
740 TAILQ_FOREACH(ifp, &ifnet, if_link) {
741 if (ifp->if_type == IFT_ETHER
742 || ifp->if_type == IFT_L2VLAN)
743 ng_ether_attach(ifp);
751 * Note that the base code won't try to unload us until
752 * all nodes have been removed, and that can't happen
753 * until all Ethernet interfaces are removed. In any
754 * case, we know there are no nodes left if the action
755 * is MOD_UNLOAD, so there's no need to detach any nodes.
758 /* Unregister function hooks */
759 ng_ether_attach_p = NULL;
760 ng_ether_detach_p = NULL;
761 ng_ether_output_p = NULL;
762 ng_ether_input_p = NULL;
763 ng_ether_input_orphan_p = NULL;
764 ng_ether_link_state_p = NULL;