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/bridge/if_bridgevar.h>
67 #include <netgraph7/ng_message.h>
68 #include <netgraph7/netgraph.h>
69 #include <netgraph7/ng_parse.h>
72 #define IFP2AC(ifp) ((struct arpcom *)ifp)
73 #define IFP2NG(ifp) (IFP2AC((ifp))->ac_netgraph)
75 /* Per-node private data */
77 struct ifnet *ifp; /* associated interface */
78 hook_p upper; /* upper hook connection */
79 hook_p lower; /* lower hook connection */
80 hook_p orphan; /* orphan hook connection */
81 u_char autoSrcAddr; /* always overwrite source address */
82 u_char promisc; /* promiscuous mode enabled */
83 u_long hwassist; /* hardware checksum capabilities */
84 u_int flags; /* flags e.g. really die */
86 typedef struct private *priv_p;
88 /* Functional hooks called from if_ethersubr.c */
89 static void ng_ether_input(struct ifnet *ifp, struct mbuf **mp);
90 static void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m);
91 static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
92 static void ng_ether_attach(struct ifnet *ifp);
93 static void ng_ether_detach(struct ifnet *ifp);
95 static void ng_ether_link_state(struct ifnet *ifp, int state);
99 static int ng_ether_rcv_lower(node_p node, struct mbuf *m);
100 static int ng_ether_rcv_upper(node_p node, struct mbuf *m);
102 /* Netgraph node methods */
103 static ng_constructor_t ng_ether_constructor;
104 static ng_rcvmsg_t ng_ether_rcvmsg;
105 static ng_shutdown_t ng_ether_shutdown;
106 static ng_newhook_t ng_ether_newhook;
107 static ng_rcvdata_t ng_ether_rcvdata;
108 static ng_disconnect_t ng_ether_disconnect;
109 static int ng_ether_mod_event(module_t mod, int event, void *data);
111 /* List of commands and how to convert arguments to/from ASCII */
112 static const struct ng_cmdlist ng_ether_cmdlist[] = {
115 NGM_ETHER_GET_IFNAME,
118 &ng_parse_string_type
122 NGM_ETHER_GET_IFINDEX,
129 NGM_ETHER_GET_ENADDR,
132 &ng_parse_enaddr_type
136 NGM_ETHER_SET_ENADDR,
138 &ng_parse_enaddr_type,
143 NGM_ETHER_GET_PROMISC,
150 NGM_ETHER_SET_PROMISC,
152 &ng_parse_int32_type,
157 NGM_ETHER_GET_AUTOSRC,
164 NGM_ETHER_SET_AUTOSRC,
166 &ng_parse_int32_type,
173 &ng_parse_enaddr_type,
180 &ng_parse_enaddr_type,
193 static struct ng_type ng_ether_typestruct = {
194 .version = NG_ABI_VERSION,
195 .name = NG_ETHER_NODE_TYPE,
196 .mod_event = ng_ether_mod_event,
197 .constructor = ng_ether_constructor,
198 .rcvmsg = ng_ether_rcvmsg,
199 .shutdown = ng_ether_shutdown,
200 .newhook = ng_ether_newhook,
201 .rcvdata = ng_ether_rcvdata,
202 .disconnect = ng_ether_disconnect,
203 .cmdlist = ng_ether_cmdlist,
205 NETGRAPH_INIT(ether, &ng_ether_typestruct);
207 /******************************************************************
208 ETHERNET FUNCTION HOOKS
209 ******************************************************************/
212 * Handle a packet that has come in on an interface. We get to
213 * look at it here before any upper layer protocols do.
215 * NOTE: this function will get called at splimp()
218 ng_ether_input(struct ifnet *ifp, struct mbuf **mp)
220 const node_p node = IFP2NG(ifp);
221 const priv_p priv = NG_NODE_PRIVATE(node);
224 /* If "lower" hook not connected, let packet continue */
225 if (priv->lower == NULL)
227 NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */
231 * Handle a packet that has come in on an interface, and which
232 * does not match any of our known protocols (an ``orphan'').
234 * NOTE: this function will get called at splimp()
237 ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m)
239 const node_p node = IFP2NG(ifp);
240 const priv_p priv = NG_NODE_PRIVATE(node);
243 /* If "orphan" hook not connected, discard packet */
244 if (priv->orphan == NULL) {
248 NG_SEND_DATA_ONLY(error, priv->orphan, m);
252 * Handle a packet that is going out on an interface.
253 * The Ethernet header is already attached to the mbuf.
256 ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
258 const node_p node = IFP2NG(ifp);
259 const priv_p priv = NG_NODE_PRIVATE(node);
262 /* If "upper" hook not connected, let packet continue */
263 if (priv->upper == NULL)
266 /* Send it out "upper" hook */
267 NG_SEND_DATA_ONLY(error, priv->upper, *mp);
272 * A new Ethernet interface has been attached.
273 * Create a new node for it, etc.
276 ng_ether_attach(struct ifnet *ifp)
282 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
283 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
284 log(LOG_ERR, "%s: can't %s for %s\n",
285 __func__, "create node", ifp->if_xname);
289 /* Allocate private data */
290 priv = kmalloc(sizeof(*priv), M_NETGRAPH,
291 M_WAITOK | M_NULLOK | M_ZERO);
293 log(LOG_ERR, "%s: can't %s for %s\n",
294 __func__, "allocate memory", ifp->if_xname);
298 NG_NODE_SET_PRIVATE(node, priv);
301 priv->hwassist = ifp->if_hwassist;
303 /* Try to give the node the same name as the interface */
304 if (ng_name_node(node, ifp->if_xname) != 0) {
305 log(LOG_WARNING, "%s: can't name node %s\n",
306 __func__, ifp->if_xname);
311 * An Ethernet interface is being detached.
312 * REALLY Destroy its node.
315 ng_ether_detach(struct ifnet *ifp)
317 const node_p node = IFP2NG(ifp);
318 const priv_p priv = NG_NODE_PRIVATE(node);
320 NG_NODE_REALLY_DIE(node); /* Force real removal of node */
322 * We can't assume the ifnet is still around when we run shutdown
323 * So zap it now. XXX We HOPE that anything running at this time
324 * handles it (as it should in the non netgraph case).
327 priv->ifp = NULL; /* XXX race if interrupted an output packet */
328 ng_rmnode_self(node); /* remove all netgraph parts */
333 * Notify graph about link event.
334 * if_link_state_change() has already checked that the state has changed.
337 ng_ether_link_state(struct ifnet *ifp, int state)
339 const node_p node = IFP2NG(ifp);
340 const priv_p priv = NG_NODE_PRIVATE(node);
342 int cmd, dummy_error = 0;
344 if (priv->lower == NULL)
347 if (state == LINK_STATE_UP)
348 cmd = NGM_LINK_IS_UP;
349 else if (state == LINK_STATE_DOWN)
350 cmd = NGM_LINK_IS_DOWN;
354 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_WAITOK | M_NULLOK);
356 NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0);
359 /******************************************************************
360 NETGRAPH NODE METHODS
361 ******************************************************************/
364 * It is not possible or allowable to create a node of this type.
365 * Nodes get created when the interface is attached (or, when
366 * this node type's KLD is loaded).
369 ng_ether_constructor(node_p node)
375 * Check for attaching a new hook.
378 ng_ether_newhook(node_p node, hook_p hook, const char *name)
380 const priv_p priv = NG_NODE_PRIVATE(node);
383 /* Divert hook is an alias for lower */
384 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
385 name = NG_ETHER_HOOK_LOWER;
388 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
389 hookptr = &priv->upper;
390 else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0)
391 hookptr = &priv->lower;
392 else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0)
393 hookptr = &priv->orphan;
397 /* Check if already connected (shouldn't be, but doesn't hurt) */
398 if (*hookptr != NULL)
401 /* Disable hardware checksums while 'upper' hook is connected */
402 if (hookptr == &priv->upper)
403 priv->ifp->if_hwassist = 0;
411 * Receive an incoming control message.
414 ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
416 const priv_p priv = NG_NODE_PRIVATE(node);
417 struct ng_mesg *resp = NULL;
421 NGI_GET_MSG(item, msg);
422 switch (msg->header.typecookie) {
423 case NGM_ETHER_COOKIE:
424 switch (msg->header.cmd) {
425 case NGM_ETHER_GET_IFNAME:
426 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_WAITOK | M_NULLOK);
431 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
433 case NGM_ETHER_GET_IFINDEX:
434 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_WAITOK | M_NULLOK);
439 *((u_int32_t *)resp->data) = priv->ifp->if_index;
441 case NGM_ETHER_GET_ENADDR:
442 NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_WAITOK | M_NULLOK);
447 bcopy(IF_LLADDR(priv->ifp),
448 resp->data, ETHER_ADDR_LEN);
450 case NGM_ETHER_SET_ENADDR:
452 if (msg->header.arglen != ETHER_ADDR_LEN) {
456 error = if_setlladdr(priv->ifp,
457 (u_char *)msg->data, ETHER_ADDR_LEN);
460 case NGM_ETHER_GET_PROMISC:
461 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_WAITOK | M_NULLOK);
466 *((u_int32_t *)resp->data) = priv->promisc;
468 case NGM_ETHER_SET_PROMISC:
472 if (msg->header.arglen != sizeof(u_int32_t)) {
476 want = !!*((u_int32_t *)msg->data);
477 if (want ^ priv->promisc) {
478 if ((error = ifpromisc(priv->ifp, want)) != 0)
480 priv->promisc = want;
484 case NGM_ETHER_GET_AUTOSRC:
485 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_WAITOK | M_NULLOK);
490 *((u_int32_t *)resp->data) = priv->autoSrcAddr;
492 case NGM_ETHER_SET_AUTOSRC:
493 if (msg->header.arglen != sizeof(u_int32_t)) {
497 priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
500 case NGM_ETHER_ADD_MULTI:
502 struct sockaddr_dl sa_dl;
503 struct ifmultiaddr *ifma;
505 if (msg->header.arglen != ETHER_ADDR_LEN) {
509 bzero(&sa_dl, sizeof(struct sockaddr_dl));
510 sa_dl.sdl_len = sizeof(struct sockaddr_dl);
511 sa_dl.sdl_family = AF_LINK;
512 sa_dl.sdl_alen = ETHER_ADDR_LEN;
513 bcopy((void *)msg->data, LLADDR(&sa_dl),
516 * Netgraph is only permitted to join groups once
517 * via the if_addmulti() KPI, because it cannot hold
518 * struct ifmultiaddr * between calls. It may also
519 * lose a race while we check if the membership
522 IF_ADDR_LOCK(priv->ifp);
523 ifma = if_findmulti(priv->ifp,
524 (struct sockaddr *)&sa_dl);
525 IF_ADDR_UNLOCK(priv->ifp);
529 error = if_addmulti(priv->ifp,
530 (struct sockaddr *)&sa_dl, &ifma);
535 case NGM_ETHER_DEL_MULTI:
537 struct sockaddr_dl sa_dl;
539 if (msg->header.arglen != ETHER_ADDR_LEN) {
543 bzero(&sa_dl, sizeof(struct sockaddr_dl));
544 sa_dl.sdl_len = sizeof(struct sockaddr_dl);
545 sa_dl.sdl_family = AF_LINK;
546 sa_dl.sdl_alen = ETHER_ADDR_LEN;
547 bcopy((void *)msg->data, LLADDR(&sa_dl),
549 error = if_delmulti(priv->ifp,
550 (struct sockaddr *)&sa_dl);
553 case NGM_ETHER_DETACH:
554 ng_ether_detach(priv->ifp);
565 NG_RESPOND_MSG(error, node, item, resp);
571 * Receive data on a hook.
574 ng_ether_rcvdata(hook_p hook, item_p item)
576 const node_p node = NG_HOOK_NODE(hook);
577 const priv_p priv = NG_NODE_PRIVATE(node);
583 if (hook == priv->lower || hook == priv->orphan)
584 return ng_ether_rcv_lower(node, m);
585 if (hook == priv->upper)
586 return ng_ether_rcv_upper(node, m);
587 panic("%s: weird hook", __func__);
588 #ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */
594 * Handle an mbuf received on the "lower" or "orphan" hook.
597 ng_ether_rcv_lower(node_p node, struct mbuf *m)
599 const priv_p priv = NG_NODE_PRIVATE(node);
600 struct ifnet *const ifp = priv->ifp;
602 /* Check whether interface is ready for packets */
603 if (!((ifp->if_flags & IFF_UP) &&
604 (ifp->if_flags & IFF_RUNNING))) {
609 /* Make sure header is fully pulled up */
610 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
614 if (m->m_len < sizeof(struct ether_header)
615 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
618 /* Drop in the MAC address if desired */
619 if (priv->autoSrcAddr) {
621 /* Make the mbuf writable if it's not already */
623 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
626 /* Overwrite source MAC address */
627 bcopy(IF_LLADDR(ifp),
628 mtod(m, struct ether_header *)->ether_shost,
632 /* Send it on its way */
633 return ether_output_frame(ifp, m);
637 * Handle an mbuf received on the "upper" hook.
639 extern struct mbuf *bridge_input_p(struct ifnet *, struct mbuf *);
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;
645 struct ether_header *eh;
647 /* Check length and pull off header */
648 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
652 if (m->m_len < sizeof(struct ether_header) &&
653 (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
656 m->m_pkthdr.rcvif = ifp;
658 /* Pass the packet to the bridge, it may come back to us */
659 if (ifp->if_bridge) {
660 bridge_input_p(ifp, m);
665 /* Route packet back in */
666 eh = mtod(m, struct ether_header *);
667 ether_demux_oncpu(ifp, m);
672 * Shutdown node. This resets the node but does not remove it
673 * unless the REALLY_DIE flag is set.
676 ng_ether_shutdown(node_p node)
678 const priv_p priv = NG_NODE_PRIVATE(node);
680 if (node->nd_flags & NGF_REALLY_DIE) {
682 * WE came here because the ethernet card is being unloaded,
683 * so stop being persistant.
684 * Actually undo all the things we did on creation.
685 * Assume the ifp has already been freed.
687 NG_NODE_SET_PRIVATE(node, NULL);
688 kfree(priv, M_NETGRAPH);
689 NG_NODE_UNREF(node); /* free node itself */
692 if (priv->promisc) { /* disable promiscuous mode */
693 (void)ifpromisc(priv->ifp, 0);
696 priv->autoSrcAddr = 1; /* reset auto-src-addr flag */
697 NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */
703 * Hook disconnection.
706 ng_ether_disconnect(hook_p hook)
708 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
710 if (hook == priv->upper) {
712 if (priv->ifp != NULL) /* restore h/w csum */
713 priv->ifp->if_hwassist = priv->hwassist;
714 } else if (hook == priv->lower)
716 else if (hook == priv->orphan)
719 panic("%s: weird hook", __func__);
720 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
721 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
722 ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */
726 /******************************************************************
728 ******************************************************************/
731 * Handle loading and unloading for this node type.
734 ng_ether_mod_event(module_t mod, int event, void *data)
743 /* Register function hooks */
744 if (ng_ether_attach_p != NULL) {
748 ng_ether_attach_p = ng_ether_attach;
749 ng_ether_detach_p = ng_ether_detach;
750 ng_ether_output_p = ng_ether_output;
751 ng_ether_input_p = ng_ether_input;
752 ng_ether_input_orphan_p = ng_ether_input_orphan;
754 ng_ether_link_state_p = ng_ether_link_state;
757 /* Create nodes for any already-existing Ethernet interfaces */
759 TAILQ_FOREACH(ifp, &ifnet, if_link) {
760 if (ifp->if_type == IFT_ETHER
761 || ifp->if_type == IFT_L2VLAN)
762 ng_ether_attach(ifp);
770 * Note that the base code won't try to unload us until
771 * all nodes have been removed, and that can't happen
772 * until all Ethernet interfaces are removed. In any
773 * case, we know there are no nodes left if the action
774 * is MOD_UNLOAD, so there's no need to detach any nodes.
777 /* Unregister function hooks */
778 ng_ether_attach_p = NULL;
779 ng_ether_detach_p = NULL;
780 ng_ether_output_p = NULL;
781 ng_ether_input_p = NULL;
782 ng_ether_input_orphan_p = NULL;
784 ng_ether_link_state_p = NULL;