5 * Copyright (c) 1996-2000 Whistle Communications, Inc.
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 * copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 * Communications, Inc. trademarks, including the mark "WHISTLE
16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 * such appears in the above copyright notice or in the software.
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * Authors: Archie Cobbs <archie@freebsd.org>
38 * Julian Elischer <julian@freebsd.org>
44 * ng_ether(4) netgraph node type
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
52 #include <sys/errno.h>
53 #include <sys/syslog.h>
54 #include <sys/socket.h>
57 #include <net/if_types.h>
58 #include <net/if_arp.h>
59 #include <net/if_var.h>
60 #include <net/ethernet.h>
62 #include <netgraph/ng_message.h>
63 #include <netgraph/netgraph.h>
64 #include <netgraph/ng_parse.h>
65 #include <netgraph/ng_ether.h>
67 #define IFP2AC(IFP) ((struct arpcom *)IFP)
68 #define IFP2NG(ifp) ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph)
70 /* Per-node private data */
72 struct ifnet *ifp; /* associated interface */
73 hook_p upper; /* upper hook connection */
74 hook_p lower; /* lower OR orphan hook connection */
75 u_char lowerOrphan; /* whether lower is lower or orphan */
76 u_char autoSrcAddr; /* always overwrite source address */
77 u_char promisc; /* promiscuous mode enabled */
78 u_long hwassist; /* hardware checksum capabilities */
79 u_int flags; /* flags e.g. really die */
81 typedef struct private *priv_p;
83 /* Functional hooks called from if_ethersubr.c */
84 static void ng_ether_input(struct ifnet *ifp,
85 struct mbuf **mp, struct ether_header *eh);
86 static void ng_ether_input_orphan(struct ifnet *ifp,
87 struct mbuf *m, struct ether_header *eh);
88 static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
89 static void ng_ether_attach(struct ifnet *ifp);
90 static void ng_ether_detach(struct ifnet *ifp);
93 static void ng_ether_input2(node_p node,
94 struct mbuf **mp, struct ether_header *eh);
95 static int ng_ether_glueback_header(struct mbuf **mp,
96 struct ether_header *eh);
97 static int ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta);
98 static int ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta);
100 /* Netgraph node methods */
101 static ng_constructor_t ng_ether_constructor;
102 static ng_rcvmsg_t ng_ether_rcvmsg;
103 static ng_shutdown_t ng_ether_shutdown;
104 static ng_newhook_t ng_ether_newhook;
105 static ng_connect_t ng_ether_connect;
106 static ng_rcvdata_t ng_ether_rcvdata;
107 static ng_disconnect_t ng_ether_disconnect;
108 static int ng_ether_mod_event(module_t mod, int event, void *data);
110 /* Parse type for an Ethernet address */
111 static ng_parse_t ng_enaddr_parse;
112 static ng_unparse_t ng_enaddr_unparse;
113 const struct ng_parse_type ng_ether_enaddr_type = {
119 NULL, /* no such thing as a "default" EN address */
123 /* List of commands and how to convert arguments to/from ASCII */
124 static const struct ng_cmdlist ng_ether_cmdlist[] = {
127 NGM_ETHER_GET_IFNAME,
130 &ng_parse_string_type
134 NGM_ETHER_GET_IFINDEX,
141 NGM_ETHER_GET_ENADDR,
144 &ng_ether_enaddr_type
148 NGM_ETHER_SET_ENADDR,
150 &ng_ether_enaddr_type,
155 NGM_ETHER_GET_PROMISC,
162 NGM_ETHER_SET_PROMISC,
164 &ng_parse_int32_type,
169 NGM_ETHER_GET_AUTOSRC,
176 NGM_ETHER_SET_AUTOSRC,
178 &ng_parse_int32_type,
184 static struct ng_type ng_ether_typestruct = {
188 ng_ether_constructor,
198 MODULE_VERSION(ng_ether, 1);
199 NETGRAPH_INIT(ether, &ng_ether_typestruct);
201 /******************************************************************
202 ETHERNET FUNCTION HOOKS
203 ******************************************************************/
206 * Handle a packet that has come in on an interface. We get to
207 * look at it here before any upper layer protocols do.
209 * NOTE: this function will get called at splimp()
212 ng_ether_input(struct ifnet *ifp,
213 struct mbuf **mp, struct ether_header *eh)
215 const node_p node = IFP2NG(ifp);
216 const priv_p priv = NG_NODE_PRIVATE(node);
218 /* If "lower" hook not connected, let packet continue */
219 if (priv->lower == NULL || priv->lowerOrphan)
221 ng_ether_input2(node, mp, eh);
225 * Handle a packet that has come in on an interface, and which
226 * does not match any of our known protocols (an ``orphan'').
228 * NOTE: this function will get called at splimp()
231 ng_ether_input_orphan(struct ifnet *ifp,
232 struct mbuf *m, struct ether_header *eh)
234 const node_p node = IFP2NG(ifp);
235 const priv_p priv = NG_NODE_PRIVATE(node);
237 /* If "orphan" hook not connected, let packet continue */
238 if (priv->lower == NULL || !priv->lowerOrphan) {
242 ng_ether_input2(node, &m, eh);
248 * Handle a packet that has come in on an ethernet interface.
249 * The Ethernet header has already been detached from the mbuf,
250 * so we have to put it back.
252 * NOTE: this function will get called at splimp()
255 ng_ether_input2(node_p node, struct mbuf **mp, struct ether_header *eh)
257 const priv_p priv = NG_NODE_PRIVATE(node);
260 /* Glue Ethernet header back on */
261 if ((error = ng_ether_glueback_header(mp, eh)) != 0)
264 /* Send out lower/orphan hook */
265 NG_SEND_DATA_ONLY(error, priv->lower, *mp);
270 * Handle a packet that is going out on an interface.
271 * The Ethernet header is already attached to the mbuf.
274 ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
276 const node_p node = IFP2NG(ifp);
277 const priv_p priv = NG_NODE_PRIVATE(node);
280 /* If "upper" hook not connected, let packet continue */
281 if (priv->upper == NULL)
284 /* Send it out "upper" hook */
285 NG_SEND_DATA_ONLY(error, priv->upper, *mp);
290 * A new Ethernet interface has been attached.
291 * Create a new node for it, etc.
294 ng_ether_attach(struct ifnet *ifp)
296 char name[IFNAMSIZ + 1];
301 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
302 snprintf(name, sizeof(name), "%s%d", ifp->if_name, ifp->if_unit);
303 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
304 log(LOG_ERR, "%s: can't %s for %s\n",
305 __func__, "create node", name);
309 /* Allocate private data */
310 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
312 log(LOG_ERR, "%s: can't %s for %s\n",
313 __func__, "allocate memory", name);
317 NG_NODE_SET_PRIVATE(node, priv);
320 priv->autoSrcAddr = 1;
321 priv->hwassist = ifp->if_hwassist;
323 /* Try to give the node the same name as the interface */
324 if (ng_name_node(node, name) != 0) {
325 log(LOG_WARNING, "%s: can't name node %s\n",
331 * An Ethernet interface is being detached.
332 * REALLY Destroy its node.
335 ng_ether_detach(struct ifnet *ifp)
337 const node_p node = IFP2NG(ifp);
338 const priv_p priv = NG_NODE_PRIVATE(node);
340 if (node == NULL) /* no node (why not?), ignore */
342 NG_NODE_REALLY_DIE(node); /* Force real removal of node */
344 * We can't assume the ifnet is still around when we run shutdown
345 * So zap it now. XXX We HOPE that anything running at this time
346 * handles it (as it should in the non netgraph case).
349 priv->ifp = NULL; /* XXX race if interrupted an output packet */
350 ng_rmnode_self(node); /* remove all netgraph parts */
354 * Optimization for gluing the Ethernet header back onto
355 * the front of an incoming packet.
358 ng_ether_glueback_header(struct mbuf **mp, struct ether_header *eh)
360 struct mbuf *m = *mp;
364 * Optimize for the case where the header is already in place
365 * at the front of the mbuf. This is actually quite likely
366 * because many Ethernet drivers generate packets this way.
368 if (eh == mtod(m, struct ether_header *) - 1) {
369 m->m_len += sizeof(*eh);
370 m->m_data -= sizeof(*eh);
371 m->m_pkthdr.len += sizeof(*eh);
375 /* Prepend the header back onto the front of the mbuf */
376 M_PREPEND(m, sizeof(*eh), M_DONTWAIT);
382 /* Copy header into front of mbuf */
383 bcopy(eh, mtod(m, void *), sizeof(*eh));
391 /******************************************************************
392 NETGRAPH NODE METHODS
393 ******************************************************************/
396 * It is not possible or allowable to create a node of this type.
397 * Nodes get created when the interface is attached (or, when
398 * this node type's KLD is loaded).
401 ng_ether_constructor(node_p node)
407 * Check for attaching a new hook.
410 ng_ether_newhook(node_p node, hook_p hook, const char *name)
412 const priv_p priv = NG_NODE_PRIVATE(node);
413 u_char orphan = priv->lowerOrphan;
416 /* Divert hook is an alias for lower */
417 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
418 name = NG_ETHER_HOOK_LOWER;
421 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
422 hookptr = &priv->upper;
423 else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
424 hookptr = &priv->lower;
426 } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
427 hookptr = &priv->lower;
432 /* Check if already connected (shouldn't be, but doesn't hurt) */
433 if (*hookptr != NULL)
436 /* Disable hardware checksums while 'upper' hook is connected */
437 if (hookptr == &priv->upper)
438 priv->ifp->if_hwassist = 0;
442 priv->lowerOrphan = orphan;
447 * Hooks are attached, adjust to force queueing.
448 * We don't really care which hook it is.
449 * they should all be queuing for outgoing data.
452 ng_ether_connect(hook_p hook)
454 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
459 * Receive an incoming control message.
462 ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
464 const priv_p priv = NG_NODE_PRIVATE(node);
465 struct ng_mesg *resp = NULL;
469 NGI_GET_MSG(item, msg);
470 switch (msg->header.typecookie) {
471 case NGM_ETHER_COOKIE:
472 switch (msg->header.cmd) {
473 case NGM_ETHER_GET_IFNAME:
474 NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT);
479 snprintf(resp->data, IFNAMSIZ + 1,
480 "%s%d", priv->ifp->if_name, priv->ifp->if_unit);
482 case NGM_ETHER_GET_IFINDEX:
483 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
488 *((u_int32_t *)resp->data) = priv->ifp->if_index;
490 case NGM_ETHER_GET_ENADDR:
491 NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
496 bcopy((IFP2AC(priv->ifp))->ac_enaddr,
497 resp->data, ETHER_ADDR_LEN);
499 case NGM_ETHER_SET_ENADDR:
501 if (msg->header.arglen != ETHER_ADDR_LEN) {
505 error = if_setlladdr(priv->ifp,
506 (u_char *)msg->data, ETHER_ADDR_LEN);
509 case NGM_ETHER_GET_PROMISC:
510 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
515 *((u_int32_t *)resp->data) = priv->promisc;
517 case NGM_ETHER_SET_PROMISC:
521 if (msg->header.arglen != sizeof(u_int32_t)) {
525 want = !!*((u_int32_t *)msg->data);
526 if (want ^ priv->promisc) {
527 if ((error = ifpromisc(priv->ifp, want)) != 0)
529 priv->promisc = want;
533 case NGM_ETHER_GET_AUTOSRC:
534 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
539 *((u_int32_t *)resp->data) = priv->autoSrcAddr;
541 case NGM_ETHER_SET_AUTOSRC:
542 if (msg->header.arglen != sizeof(u_int32_t)) {
546 priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
557 NG_RESPOND_MSG(error, node, item, resp);
563 * Receive data on a hook.
566 ng_ether_rcvdata(hook_p hook, item_p item)
568 const node_p node = NG_HOOK_NODE(hook);
569 const priv_p priv = NG_NODE_PRIVATE(node);
574 NGI_GET_META(item, meta);
576 if (hook == priv->lower)
577 return ng_ether_rcv_lower(node, m, meta);
578 if (hook == priv->upper)
579 return ng_ether_rcv_upper(node, m, meta);
580 panic("%s: weird hook", __func__);
581 #ifdef RESTARTABLE_PANICS /* so we don;t get an error msg in LINT */
587 * Handle an mbuf received on the "lower" hook.
590 ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta)
592 const priv_p priv = NG_NODE_PRIVATE(node);
593 struct ifnet *const ifp = priv->ifp;
595 /* Discard meta info */
598 /* Check whether interface is ready for packets */
599 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
604 /* Make sure header is fully pulled up */
605 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
609 if (m->m_len < sizeof(struct ether_header)
610 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
613 /* Drop in the MAC address if desired */
614 if (priv->autoSrcAddr) {
616 /* Make the mbuf writable if it's not already */
618 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
621 /* Overwrite source MAC address */
622 bcopy((IFP2AC(ifp))->ac_enaddr,
623 mtod(m, struct ether_header *)->ether_shost,
627 /* Send it on its way */
628 return ether_output_frame(ifp, m);
632 * Handle an mbuf received on the "upper" hook.
635 ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta)
637 const priv_p priv = NG_NODE_PRIVATE(node);
638 struct ether_header *eh;
640 /* Discard meta info */
643 /* Check length and pull off header */
644 if (m->m_pkthdr.len < sizeof(*eh)) {
648 if (m->m_len < sizeof(*eh) && (m = m_pullup(m, sizeof(*eh))) == NULL)
650 eh = mtod(m, struct ether_header *);
651 m->m_data += sizeof(*eh);
652 m->m_len -= sizeof(*eh);
653 m->m_pkthdr.len -= sizeof(*eh);
654 m->m_pkthdr.rcvif = priv->ifp;
656 /* Route packet back in */
657 ether_demux(priv->ifp, eh, m);
662 * Shutdown node. This resets the node but does not remove it
663 * unless the REALLY_DIE flag is set.
666 ng_ether_shutdown(node_p node)
668 const priv_p priv = NG_NODE_PRIVATE(node);
670 if (priv->promisc) { /* disable promiscuous mode */
671 (void)ifpromisc(priv->ifp, 0);
674 if (node->nd_flags & NG_REALLY_DIE) {
676 * WE came here because the ethernet card is being unloaded,
677 * so stop being persistant.
678 * Actually undo all the things we did on creation.
679 * Assume the ifp has already been freed.
681 NG_NODE_SET_PRIVATE(node, NULL);
682 FREE(priv, M_NETGRAPH);
683 NG_NODE_UNREF(node); /* free node itself */
686 priv->autoSrcAddr = 1; /* reset auto-src-addr flag */
687 node->nd_flags &= ~NG_INVALID; /* Signal ng_rmnode we are persisant */
692 * Hook disconnection.
695 ng_ether_disconnect(hook_p hook)
697 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
699 if (hook == priv->upper) {
701 priv->ifp->if_hwassist = priv->hwassist; /* restore h/w csum */
702 } else if (hook == priv->lower) {
704 priv->lowerOrphan = 0;
706 panic("%s: weird hook", __func__);
707 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
708 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
709 ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */
714 ng_enaddr_parse(const struct ng_parse_type *type,
715 const char *s, int *const off, const u_char *const start,
716 u_char *const buf, int *const buflen)
722 if (*buflen < ETHER_ADDR_LEN)
724 for (i = 0; i < ETHER_ADDR_LEN; i++) {
725 val = strtoul(s + *off, &eptr, 16);
726 if (val > 0xff || eptr == s + *off)
728 buf[i] = (u_char)val;
730 if (i < ETHER_ADDR_LEN - 1) {
736 *buflen = ETHER_ADDR_LEN;
741 ng_enaddr_unparse(const struct ng_parse_type *type,
742 const u_char *data, int *off, char *cbuf, int cbuflen)
746 len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
747 data[*off], data[*off + 1], data[*off + 2],
748 data[*off + 3], data[*off + 4], data[*off + 5]);
751 *off += ETHER_ADDR_LEN;
755 /******************************************************************
757 ******************************************************************/
760 * Handle loading and unloading for this node type.
763 ng_ether_mod_event(module_t mod, int event, void *data)
773 /* Register function hooks */
774 if (ng_ether_attach_p != NULL) {
778 ng_ether_attach_p = ng_ether_attach;
779 ng_ether_detach_p = ng_ether_detach;
780 ng_ether_output_p = ng_ether_output;
781 ng_ether_input_p = ng_ether_input;
782 ng_ether_input_orphan_p = ng_ether_input_orphan;
784 /* Create nodes for any already-existing Ethernet interfaces */
785 TAILQ_FOREACH(ifp, &ifnet, if_link) {
786 if (ifp->if_type == IFT_ETHER
787 || ifp->if_type == IFT_L2VLAN)
788 ng_ether_attach(ifp);
795 * Note that the base code won't try to unload us until
796 * all nodes have been removed, and that can't happen
797 * until all Ethernet interfaces are removed. In any
798 * case, we know there are no nodes left if the action
799 * is MOD_UNLOAD, so there's no need to detach any nodes.
802 /* Unregister function hooks */
803 ng_ether_attach_p = NULL;
804 ng_ether_detach_p = NULL;
805 ng_ether_output_p = NULL;
806 ng_ether_input_p = NULL;
807 ng_ether_input_orphan_p = NULL;