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 $
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/bridge/if_bridgevar.h>
66 #include <netgraph7/ng_message.h>
67 #include <netgraph7/netgraph.h>
68 #include <netgraph7/ng_parse.h>
71 #define IFP2AC(ifp) ((struct arpcom *)ifp)
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 /* Functional hooks called from if_ethersubr.c */
88 static void ng_ether_input(struct ifnet *ifp, struct mbuf **mp);
89 static void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m);
90 static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
91 static void ng_ether_attach(struct ifnet *ifp);
92 static void ng_ether_detach(struct ifnet *ifp);
94 static void ng_ether_link_state(struct ifnet *ifp, int state);
98 static int ng_ether_rcv_lower(node_p node, struct mbuf *m);
99 static int ng_ether_rcv_upper(node_p node, struct mbuf *m);
101 /* Netgraph node methods */
102 static ng_constructor_t ng_ether_constructor;
103 static ng_rcvmsg_t ng_ether_rcvmsg;
104 static ng_shutdown_t ng_ether_shutdown;
105 static ng_newhook_t ng_ether_newhook;
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 /* List of commands and how to convert arguments to/from ASCII */
111 static const struct ng_cmdlist ng_ether_cmdlist[] = {
114 NGM_ETHER_GET_IFNAME,
117 &ng_parse_string_type
121 NGM_ETHER_GET_IFINDEX,
128 NGM_ETHER_GET_ENADDR,
131 &ng_parse_enaddr_type
135 NGM_ETHER_SET_ENADDR,
137 &ng_parse_enaddr_type,
142 NGM_ETHER_GET_PROMISC,
149 NGM_ETHER_SET_PROMISC,
151 &ng_parse_int32_type,
156 NGM_ETHER_GET_AUTOSRC,
163 NGM_ETHER_SET_AUTOSRC,
165 &ng_parse_int32_type,
172 &ng_parse_enaddr_type,
179 &ng_parse_enaddr_type,
192 static struct ng_type ng_ether_typestruct = {
193 .version = NG_ABI_VERSION,
194 .name = NG_ETHER_NODE_TYPE,
195 .mod_event = ng_ether_mod_event,
196 .constructor = ng_ether_constructor,
197 .rcvmsg = ng_ether_rcvmsg,
198 .shutdown = ng_ether_shutdown,
199 .newhook = ng_ether_newhook,
200 .rcvdata = ng_ether_rcvdata,
201 .disconnect = ng_ether_disconnect,
202 .cmdlist = ng_ether_cmdlist,
204 NETGRAPH_INIT(ether, &ng_ether_typestruct);
206 /******************************************************************
207 ETHERNET FUNCTION HOOKS
208 ******************************************************************/
211 * Handle a packet that has come in on an interface. We get to
212 * look at it here before any upper layer protocols do.
214 * NOTE: this function will get called at splimp()
217 ng_ether_input(struct ifnet *ifp, struct mbuf **mp)
219 const node_p node = IFP2NG(ifp);
220 const priv_p priv = NG_NODE_PRIVATE(node);
223 /* If "lower" hook not connected, let packet continue */
224 if (priv->lower == NULL)
226 NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */
230 * Handle a packet that has come in on an interface, and which
231 * does not match any of our known protocols (an ``orphan'').
233 * NOTE: this function will get called at splimp()
236 ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m)
238 const node_p node = IFP2NG(ifp);
239 const priv_p priv = NG_NODE_PRIVATE(node);
242 /* If "orphan" hook not connected, discard packet */
243 if (priv->orphan == NULL) {
247 NG_SEND_DATA_ONLY(error, priv->orphan, m);
251 * Handle a packet that is going out on an interface.
252 * The Ethernet header is already attached to the mbuf.
255 ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
257 const node_p node = IFP2NG(ifp);
258 const priv_p priv = NG_NODE_PRIVATE(node);
261 /* If "upper" hook not connected, let packet continue */
262 if (priv->upper == NULL)
265 /* Send it out "upper" hook */
266 NG_SEND_DATA_ONLY(error, priv->upper, *mp);
271 * A new Ethernet interface has been attached.
272 * Create a new node for it, etc.
275 ng_ether_attach(struct ifnet *ifp)
281 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
282 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
283 log(LOG_ERR, "%s: can't %s for %s\n",
284 __func__, "create node", ifp->if_xname);
288 /* Allocate private data */
289 priv = kmalloc(sizeof(*priv), M_NETGRAPH,
290 M_WAITOK | M_NULLOK | M_ZERO);
292 log(LOG_ERR, "%s: can't %s for %s\n",
293 __func__, "allocate memory", ifp->if_xname);
297 NG_NODE_SET_PRIVATE(node, priv);
300 priv->hwassist = ifp->if_hwassist;
302 /* Try to give the node the same name as the interface */
303 if (ng_name_node(node, ifp->if_xname) != 0) {
304 log(LOG_WARNING, "%s: can't name node %s\n",
305 __func__, ifp->if_xname);
310 * An Ethernet interface is being detached.
311 * REALLY Destroy its node.
314 ng_ether_detach(struct ifnet *ifp)
316 const node_p node = IFP2NG(ifp);
317 const priv_p priv = NG_NODE_PRIVATE(node);
319 NG_NODE_REALLY_DIE(node); /* Force real removal of node */
321 * We can't assume the ifnet is still around when we run shutdown
322 * So zap it now. XXX We HOPE that anything running at this time
323 * handles it (as it should in the non netgraph case).
326 priv->ifp = NULL; /* XXX race if interrupted an output packet */
327 ng_rmnode_self(node); /* remove all netgraph parts */
332 * Notify graph about link event.
333 * if_link_state_change() has already checked that the state has changed.
336 ng_ether_link_state(struct ifnet *ifp, int state)
338 const node_p node = IFP2NG(ifp);
339 const priv_p priv = NG_NODE_PRIVATE(node);
341 int cmd, dummy_error = 0;
343 if (priv->lower == NULL)
346 if (state == LINK_STATE_UP)
347 cmd = NGM_LINK_IS_UP;
348 else if (state == LINK_STATE_DOWN)
349 cmd = NGM_LINK_IS_DOWN;
353 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_WAITOK | M_NULLOK);
355 NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0);
358 /******************************************************************
359 NETGRAPH NODE METHODS
360 ******************************************************************/
363 * It is not possible or allowable to create a node of this type.
364 * Nodes get created when the interface is attached (or, when
365 * this node type's KLD is loaded).
368 ng_ether_constructor(node_p node)
374 * Check for attaching a new hook.
377 ng_ether_newhook(node_p node, hook_p hook, const char *name)
379 const priv_p priv = NG_NODE_PRIVATE(node);
382 /* Divert hook is an alias for lower */
383 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
384 name = NG_ETHER_HOOK_LOWER;
387 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
388 hookptr = &priv->upper;
389 else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0)
390 hookptr = &priv->lower;
391 else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0)
392 hookptr = &priv->orphan;
396 /* Check if already connected (shouldn't be, but doesn't hurt) */
397 if (*hookptr != NULL)
400 /* Disable hardware checksums while 'upper' hook is connected */
401 if (hookptr == &priv->upper)
402 priv->ifp->if_hwassist = 0;
410 * Receive an incoming control message.
413 ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
415 const priv_p priv = NG_NODE_PRIVATE(node);
416 struct ng_mesg *resp = NULL;
420 NGI_GET_MSG(item, msg);
421 switch (msg->header.typecookie) {
422 case NGM_ETHER_COOKIE:
423 switch (msg->header.cmd) {
424 case NGM_ETHER_GET_IFNAME:
425 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_WAITOK | M_NULLOK);
430 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
432 case NGM_ETHER_GET_IFINDEX:
433 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_WAITOK | M_NULLOK);
438 *((u_int32_t *)resp->data) = priv->ifp->if_index;
440 case NGM_ETHER_GET_ENADDR:
441 NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_WAITOK | M_NULLOK);
446 bcopy(IF_LLADDR(priv->ifp),
447 resp->data, ETHER_ADDR_LEN);
449 case NGM_ETHER_SET_ENADDR:
451 if (msg->header.arglen != ETHER_ADDR_LEN) {
455 error = if_setlladdr(priv->ifp,
456 (u_char *)msg->data, ETHER_ADDR_LEN);
459 case NGM_ETHER_GET_PROMISC:
460 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_WAITOK | M_NULLOK);
465 *((u_int32_t *)resp->data) = priv->promisc;
467 case NGM_ETHER_SET_PROMISC:
471 if (msg->header.arglen != sizeof(u_int32_t)) {
475 want = !!*((u_int32_t *)msg->data);
476 if (want ^ priv->promisc) {
477 if ((error = ifpromisc(priv->ifp, want)) != 0)
479 priv->promisc = want;
483 case NGM_ETHER_GET_AUTOSRC:
484 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_WAITOK | M_NULLOK);
489 *((u_int32_t *)resp->data) = priv->autoSrcAddr;
491 case NGM_ETHER_SET_AUTOSRC:
492 if (msg->header.arglen != sizeof(u_int32_t)) {
496 priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
499 case NGM_ETHER_ADD_MULTI:
501 struct sockaddr_dl sa_dl;
502 struct ifmultiaddr *ifma;
504 if (msg->header.arglen != ETHER_ADDR_LEN) {
508 bzero(&sa_dl, sizeof(struct sockaddr_dl));
509 sa_dl.sdl_len = sizeof(struct sockaddr_dl);
510 sa_dl.sdl_family = AF_LINK;
511 sa_dl.sdl_alen = ETHER_ADDR_LEN;
512 bcopy((void *)msg->data, LLADDR(&sa_dl),
515 * Netgraph is only permitted to join groups once
516 * via the if_addmulti() KPI, because it cannot hold
517 * struct ifmultiaddr * between calls. It may also
518 * lose a race while we check if the membership
521 IF_ADDR_LOCK(priv->ifp);
522 ifma = if_findmulti(priv->ifp,
523 (struct sockaddr *)&sa_dl);
524 IF_ADDR_UNLOCK(priv->ifp);
528 error = if_addmulti(priv->ifp,
529 (struct sockaddr *)&sa_dl, &ifma);
534 case NGM_ETHER_DEL_MULTI:
536 struct sockaddr_dl sa_dl;
538 if (msg->header.arglen != ETHER_ADDR_LEN) {
542 bzero(&sa_dl, sizeof(struct sockaddr_dl));
543 sa_dl.sdl_len = sizeof(struct sockaddr_dl);
544 sa_dl.sdl_family = AF_LINK;
545 sa_dl.sdl_alen = ETHER_ADDR_LEN;
546 bcopy((void *)msg->data, LLADDR(&sa_dl),
548 error = if_delmulti(priv->ifp,
549 (struct sockaddr *)&sa_dl);
552 case NGM_ETHER_DETACH:
553 ng_ether_detach(priv->ifp);
564 NG_RESPOND_MSG(error, node, item, resp);
570 * Receive data on a hook.
573 ng_ether_rcvdata(hook_p hook, item_p item)
575 const node_p node = NG_HOOK_NODE(hook);
576 const priv_p priv = NG_NODE_PRIVATE(node);
582 if (hook == priv->lower || hook == priv->orphan)
583 return ng_ether_rcv_lower(node, m);
584 if (hook == priv->upper)
585 return ng_ether_rcv_upper(node, m);
586 panic("%s: weird hook", __func__);
587 #ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */
593 * Handle an mbuf received on the "lower" or "orphan" hook.
596 ng_ether_rcv_lower(node_p node, struct mbuf *m)
598 const priv_p priv = NG_NODE_PRIVATE(node);
599 struct ifnet *const ifp = priv->ifp;
601 /* Check whether interface is ready for packets */
602 if (!((ifp->if_flags & IFF_UP) &&
603 (ifp->if_flags & IFF_RUNNING))) {
608 /* Make sure header is fully pulled up */
609 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
613 if (m->m_len < sizeof(struct ether_header)
614 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
617 /* Drop in the MAC address if desired */
618 if (priv->autoSrcAddr) {
620 /* Make the mbuf writable if it's not already */
622 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
625 /* Overwrite source MAC address */
626 bcopy(IF_LLADDR(ifp),
627 mtod(m, struct ether_header *)->ether_shost,
631 /* Send it on its way */
632 return ether_output_frame(ifp, m);
636 * Handle an mbuf received on the "upper" hook.
638 extern struct mbuf *bridge_input_p(struct ifnet *, struct mbuf *);
640 ng_ether_rcv_upper(node_p node, struct mbuf *m)
642 const priv_p priv = NG_NODE_PRIVATE(node);
643 struct ifnet *ifp = priv->ifp;
645 /* Check length and pull off header */
646 if (m->m_pkthdr.len < sizeof(struct ether_header)) {
650 if (m->m_len < sizeof(struct ether_header) &&
651 (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
654 m->m_pkthdr.rcvif = ifp;
656 /* Pass the packet to the bridge, it may come back to us */
657 if (ifp->if_bridge) {
658 bridge_input_p(ifp, m);
663 /* Route packet back in */
664 ether_demux_oncpu(ifp, m);
669 * Shutdown node. This resets the node but does not remove it
670 * unless the REALLY_DIE flag is set.
673 ng_ether_shutdown(node_p node)
675 const priv_p priv = NG_NODE_PRIVATE(node);
677 if (node->nd_flags & NGF_REALLY_DIE) {
679 * WE came here because the ethernet card is being unloaded,
680 * so stop being persistant.
681 * Actually undo all the things we did on creation.
682 * Assume the ifp has already been freed.
684 NG_NODE_SET_PRIVATE(node, NULL);
685 kfree(priv, M_NETGRAPH);
686 NG_NODE_UNREF(node); /* free node itself */
689 if (priv->promisc) { /* disable promiscuous mode */
690 (void)ifpromisc(priv->ifp, 0);
693 priv->autoSrcAddr = 1; /* reset auto-src-addr flag */
694 NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */
700 * Hook disconnection.
703 ng_ether_disconnect(hook_p hook)
705 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
707 if (hook == priv->upper) {
709 if (priv->ifp != NULL) /* restore h/w csum */
710 priv->ifp->if_hwassist = priv->hwassist;
711 } else if (hook == priv->lower)
713 else if (hook == priv->orphan)
716 panic("%s: weird hook", __func__);
717 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
718 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
719 ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */
723 /******************************************************************
725 ******************************************************************/
728 * Handle loading and unloading for this node type.
731 ng_ether_mod_event(module_t mod, int event, void *data)
740 /* Register function hooks */
741 if (ng_ether_attach_p != NULL) {
745 ng_ether_attach_p = ng_ether_attach;
746 ng_ether_detach_p = ng_ether_detach;
747 ng_ether_output_p = ng_ether_output;
748 ng_ether_input_p = ng_ether_input;
749 ng_ether_input_orphan_p = ng_ether_input_orphan;
751 ng_ether_link_state_p = ng_ether_link_state;
754 /* Create nodes for any already-existing Ethernet interfaces */
756 TAILQ_FOREACH(ifp, &ifnetlist, 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;
781 ng_ether_link_state_p = NULL;