1e1e1452dSArchie Cobbs 2e1e1452dSArchie Cobbs /* 3e1e1452dSArchie Cobbs * ng_ether.c 4e1e1452dSArchie Cobbs * 5e1e1452dSArchie Cobbs * Copyright (c) 1996-2000 Whistle Communications, Inc. 6e1e1452dSArchie Cobbs * All rights reserved. 7e1e1452dSArchie Cobbs * 8e1e1452dSArchie Cobbs * Subject to the following obligations and disclaimer of warranty, use and 9e1e1452dSArchie Cobbs * redistribution of this software, in source or object code forms, with or 10e1e1452dSArchie Cobbs * without modifications are expressly permitted by Whistle Communications; 11e1e1452dSArchie Cobbs * provided, however, that: 12e1e1452dSArchie Cobbs * 1. Any and all reproductions of the source or object code must include the 13e1e1452dSArchie Cobbs * copyright notice above and the following disclaimer of warranties; and 14e1e1452dSArchie Cobbs * 2. No rights are granted, in any manner or form, to use Whistle 15e1e1452dSArchie Cobbs * Communications, Inc. trademarks, including the mark "WHISTLE 16e1e1452dSArchie Cobbs * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 17e1e1452dSArchie Cobbs * such appears in the above copyright notice or in the software. 18e1e1452dSArchie Cobbs * 19e1e1452dSArchie Cobbs * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 20e1e1452dSArchie Cobbs * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 21e1e1452dSArchie Cobbs * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 22e1e1452dSArchie Cobbs * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 23e1e1452dSArchie Cobbs * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 24e1e1452dSArchie Cobbs * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 25e1e1452dSArchie Cobbs * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 26e1e1452dSArchie Cobbs * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 27e1e1452dSArchie Cobbs * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 28e1e1452dSArchie Cobbs * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 29e1e1452dSArchie Cobbs * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 30e1e1452dSArchie Cobbs * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 31e1e1452dSArchie Cobbs * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 32e1e1452dSArchie Cobbs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33e1e1452dSArchie Cobbs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34e1e1452dSArchie Cobbs * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 35e1e1452dSArchie Cobbs * OF SUCH DAMAGE. 36e1e1452dSArchie Cobbs * 37e1e1452dSArchie Cobbs * Authors: Archie Cobbs <archie@freebsd.org> 38e1e1452dSArchie Cobbs * Julian Elischer <julian@freebsd.org> 39e1e1452dSArchie Cobbs * 40e1e1452dSArchie Cobbs * $FreeBSD$ 41e1e1452dSArchie Cobbs */ 42e1e1452dSArchie Cobbs 43e1e1452dSArchie Cobbs /* 44e1e1452dSArchie Cobbs * ng_ether(4) netgraph node type 45e1e1452dSArchie Cobbs */ 46e1e1452dSArchie Cobbs 47e1e1452dSArchie Cobbs #include <sys/param.h> 48e1e1452dSArchie Cobbs #include <sys/systm.h> 49e1e1452dSArchie Cobbs #include <sys/kernel.h> 50e1e1452dSArchie Cobbs #include <sys/malloc.h> 51e1e1452dSArchie Cobbs #include <sys/mbuf.h> 52e1e1452dSArchie Cobbs #include <sys/errno.h> 53e1e1452dSArchie Cobbs #include <sys/syslog.h> 54e1e1452dSArchie Cobbs #include <sys/socket.h> 55e1e1452dSArchie Cobbs 56a176c2aeSGleb Smirnoff #include <net/bridge.h> 57e1e1452dSArchie Cobbs #include <net/if.h> 58e1e1452dSArchie Cobbs #include <net/if_types.h> 59e1e1452dSArchie Cobbs #include <net/if_arp.h> 60e1e1452dSArchie Cobbs #include <net/if_var.h> 61e1e1452dSArchie Cobbs #include <net/ethernet.h> 62e1e1452dSArchie Cobbs 63e1e1452dSArchie Cobbs #include <netgraph/ng_message.h> 64e1e1452dSArchie Cobbs #include <netgraph/netgraph.h> 65e1e1452dSArchie Cobbs #include <netgraph/ng_parse.h> 66e1e1452dSArchie Cobbs #include <netgraph/ng_ether.h> 67e1e1452dSArchie Cobbs 68e1e1452dSArchie Cobbs #define IFP2NG(ifp) ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph) 69445e045bSAlexander Kabaev #define IFP2NG_SET(ifp, val) (((struct arpcom *)(ifp))->ac_netgraph = (val)) 70e1e1452dSArchie Cobbs 713c976c3fSPawel Jakub Dawidek /* Per-node private data */ 723c976c3fSPawel Jakub Dawidek struct private { 733c976c3fSPawel Jakub Dawidek struct ifnet *ifp; /* associated interface */ 743c976c3fSPawel Jakub Dawidek hook_p upper; /* upper hook connection */ 751a292b80SArchie Cobbs hook_p lower; /* lower hook connection */ 761a292b80SArchie Cobbs hook_p orphan; /* orphan hook connection */ 773c976c3fSPawel Jakub Dawidek u_char autoSrcAddr; /* always overwrite source address */ 783c976c3fSPawel Jakub Dawidek u_char promisc; /* promiscuous mode enabled */ 793c976c3fSPawel Jakub Dawidek u_long hwassist; /* hardware checksum capabilities */ 803c976c3fSPawel Jakub Dawidek u_int flags; /* flags e.g. really die */ 813c976c3fSPawel Jakub Dawidek }; 823c976c3fSPawel Jakub Dawidek typedef struct private *priv_p; 83e1e1452dSArchie Cobbs 84edbb5246SSam Leffler /* Hook pointers used by if_ethersubr.c to callback to netgraph */ 85edbb5246SSam Leffler extern void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 86edbb5246SSam Leffler extern void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 87edbb5246SSam Leffler extern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 88edbb5246SSam Leffler extern void (*ng_ether_attach_p)(struct ifnet *ifp); 89edbb5246SSam Leffler extern void (*ng_ether_detach_p)(struct ifnet *ifp); 90edbb5246SSam Leffler 91e1e1452dSArchie Cobbs /* Functional hooks called from if_ethersubr.c */ 92edbb5246SSam Leffler static void ng_ether_input(struct ifnet *ifp, struct mbuf **mp); 93edbb5246SSam Leffler static void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m); 94e1e1452dSArchie Cobbs static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp); 95e1e1452dSArchie Cobbs static void ng_ether_attach(struct ifnet *ifp); 96e1e1452dSArchie Cobbs static void ng_ether_detach(struct ifnet *ifp); 97e1e1452dSArchie Cobbs 98e1e1452dSArchie Cobbs /* Other functions */ 993ca24c28SJulian Elischer static int ng_ether_rcv_lower(node_p node, struct mbuf *m); 1003ca24c28SJulian Elischer static int ng_ether_rcv_upper(node_p node, struct mbuf *m); 101e1e1452dSArchie Cobbs 102e1e1452dSArchie Cobbs /* Netgraph node methods */ 103e1e1452dSArchie Cobbs static ng_constructor_t ng_ether_constructor; 104e1e1452dSArchie Cobbs static ng_rcvmsg_t ng_ether_rcvmsg; 105069154d5SJulian Elischer static ng_shutdown_t ng_ether_shutdown; 106e1e1452dSArchie Cobbs static ng_newhook_t ng_ether_newhook; 107859a4d16SJulian Elischer static ng_connect_t ng_ether_connect; 108e1e1452dSArchie Cobbs static ng_rcvdata_t ng_ether_rcvdata; 109e1e1452dSArchie Cobbs static ng_disconnect_t ng_ether_disconnect; 110e1e1452dSArchie Cobbs static int ng_ether_mod_event(module_t mod, int event, void *data); 111e1e1452dSArchie Cobbs 112e1e1452dSArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */ 113e1e1452dSArchie Cobbs static const struct ng_cmdlist ng_ether_cmdlist[] = { 114e1e1452dSArchie Cobbs { 115e1e1452dSArchie Cobbs NGM_ETHER_COOKIE, 116e1e1452dSArchie Cobbs NGM_ETHER_GET_IFNAME, 117e1e1452dSArchie Cobbs "getifname", 118e1e1452dSArchie Cobbs NULL, 119e1e1452dSArchie Cobbs &ng_parse_string_type 120e1e1452dSArchie Cobbs }, 121e1e1452dSArchie Cobbs { 122e1e1452dSArchie Cobbs NGM_ETHER_COOKIE, 123e1e1452dSArchie Cobbs NGM_ETHER_GET_IFINDEX, 124e1e1452dSArchie Cobbs "getifindex", 125e1e1452dSArchie Cobbs NULL, 126e1e1452dSArchie Cobbs &ng_parse_int32_type 127e1e1452dSArchie Cobbs }, 1284b39c3c7SArchie Cobbs { 1294b39c3c7SArchie Cobbs NGM_ETHER_COOKIE, 1304b39c3c7SArchie Cobbs NGM_ETHER_GET_ENADDR, 1314b39c3c7SArchie Cobbs "getenaddr", 1324b39c3c7SArchie Cobbs NULL, 1338c7e4101SRuslan Ermilov &ng_parse_enaddr_type 1344b39c3c7SArchie Cobbs }, 1354b39c3c7SArchie Cobbs { 1364b39c3c7SArchie Cobbs NGM_ETHER_COOKIE, 13756045c66SArchie Cobbs NGM_ETHER_SET_ENADDR, 13856045c66SArchie Cobbs "setenaddr", 1398c7e4101SRuslan Ermilov &ng_parse_enaddr_type, 14056045c66SArchie Cobbs NULL 14156045c66SArchie Cobbs }, 14256045c66SArchie Cobbs { 14356045c66SArchie Cobbs NGM_ETHER_COOKIE, 14456045c66SArchie Cobbs NGM_ETHER_GET_PROMISC, 14556045c66SArchie Cobbs "getpromisc", 14656045c66SArchie Cobbs NULL, 14756045c66SArchie Cobbs &ng_parse_int32_type 14856045c66SArchie Cobbs }, 14956045c66SArchie Cobbs { 15056045c66SArchie Cobbs NGM_ETHER_COOKIE, 1514b39c3c7SArchie Cobbs NGM_ETHER_SET_PROMISC, 1524b39c3c7SArchie Cobbs "setpromisc", 1534b39c3c7SArchie Cobbs &ng_parse_int32_type, 1544b39c3c7SArchie Cobbs NULL 1554b39c3c7SArchie Cobbs }, 1564b39c3c7SArchie Cobbs { 1574b39c3c7SArchie Cobbs NGM_ETHER_COOKIE, 15856045c66SArchie Cobbs NGM_ETHER_GET_AUTOSRC, 15956045c66SArchie Cobbs "getautosrc", 16056045c66SArchie Cobbs NULL, 16156045c66SArchie Cobbs &ng_parse_int32_type 16256045c66SArchie Cobbs }, 16356045c66SArchie Cobbs { 16456045c66SArchie Cobbs NGM_ETHER_COOKIE, 1654b39c3c7SArchie Cobbs NGM_ETHER_SET_AUTOSRC, 1664b39c3c7SArchie Cobbs "setautosrc", 1674b39c3c7SArchie Cobbs &ng_parse_int32_type, 1684b39c3c7SArchie Cobbs NULL 1694b39c3c7SArchie Cobbs }, 170e1e1452dSArchie Cobbs { 0 } 171e1e1452dSArchie Cobbs }; 172e1e1452dSArchie Cobbs 173e1e1452dSArchie Cobbs static struct ng_type ng_ether_typestruct = { 174f8aae777SJulian Elischer .version = NG_ABI_VERSION, 175f8aae777SJulian Elischer .name = NG_ETHER_NODE_TYPE, 176f8aae777SJulian Elischer .mod_event = ng_ether_mod_event, 177f8aae777SJulian Elischer .constructor = ng_ether_constructor, 178f8aae777SJulian Elischer .rcvmsg = ng_ether_rcvmsg, 179f8aae777SJulian Elischer .shutdown = ng_ether_shutdown, 180f8aae777SJulian Elischer .newhook = ng_ether_newhook, 181f8aae777SJulian Elischer .connect = ng_ether_connect, 182f8aae777SJulian Elischer .rcvdata = ng_ether_rcvdata, 183f8aae777SJulian Elischer .disconnect = ng_ether_disconnect, 184f8aae777SJulian Elischer .cmdlist = ng_ether_cmdlist, 185e1e1452dSArchie Cobbs }; 1866b795970SJulian Elischer MODULE_VERSION(ng_ether, 1); 187e1e1452dSArchie Cobbs NETGRAPH_INIT(ether, &ng_ether_typestruct); 188e1e1452dSArchie Cobbs 189e1e1452dSArchie Cobbs /****************************************************************** 190e1e1452dSArchie Cobbs ETHERNET FUNCTION HOOKS 191e1e1452dSArchie Cobbs ******************************************************************/ 192e1e1452dSArchie Cobbs 193e1e1452dSArchie Cobbs /* 194e1e1452dSArchie Cobbs * Handle a packet that has come in on an interface. We get to 195e1e1452dSArchie Cobbs * look at it here before any upper layer protocols do. 196e1e1452dSArchie Cobbs * 197e1e1452dSArchie Cobbs * NOTE: this function will get called at splimp() 198e1e1452dSArchie Cobbs */ 199e1e1452dSArchie Cobbs static void 200edbb5246SSam Leffler ng_ether_input(struct ifnet *ifp, struct mbuf **mp) 201e1e1452dSArchie Cobbs { 202e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp); 20330400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 2041a292b80SArchie Cobbs int error; 205e1e1452dSArchie Cobbs 206e1e1452dSArchie Cobbs /* If "lower" hook not connected, let packet continue */ 2071a292b80SArchie Cobbs if (priv->lower == NULL) 208e1e1452dSArchie Cobbs return; 2091a292b80SArchie Cobbs NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */ 210e1e1452dSArchie Cobbs } 211e1e1452dSArchie Cobbs 212e1e1452dSArchie Cobbs /* 213e1e1452dSArchie Cobbs * Handle a packet that has come in on an interface, and which 214e1e1452dSArchie Cobbs * does not match any of our known protocols (an ``orphan''). 215e1e1452dSArchie Cobbs * 216e1e1452dSArchie Cobbs * NOTE: this function will get called at splimp() 217e1e1452dSArchie Cobbs */ 218e1e1452dSArchie Cobbs static void 219edbb5246SSam Leffler ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m) 220e1e1452dSArchie Cobbs { 221e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp); 22230400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 2231a292b80SArchie Cobbs int error; 224e1e1452dSArchie Cobbs 2251a292b80SArchie Cobbs /* If "orphan" hook not connected, discard packet */ 2261a292b80SArchie Cobbs if (priv->orphan == NULL) { 227e1e1452dSArchie Cobbs m_freem(m); 228e1e1452dSArchie Cobbs return; 229e1e1452dSArchie Cobbs } 2301a292b80SArchie Cobbs NG_SEND_DATA_ONLY(error, priv->orphan, m); 231e1e1452dSArchie Cobbs } 232e1e1452dSArchie Cobbs 233e1e1452dSArchie Cobbs /* 234e1e1452dSArchie Cobbs * Handle a packet that is going out on an interface. 235e1e1452dSArchie Cobbs * The Ethernet header is already attached to the mbuf. 236e1e1452dSArchie Cobbs */ 237e1e1452dSArchie Cobbs static int 238e1e1452dSArchie Cobbs ng_ether_output(struct ifnet *ifp, struct mbuf **mp) 239e1e1452dSArchie Cobbs { 240e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp); 24130400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 242e1e1452dSArchie Cobbs int error = 0; 243e1e1452dSArchie Cobbs 244e1e1452dSArchie Cobbs /* If "upper" hook not connected, let packet continue */ 245e1e1452dSArchie Cobbs if (priv->upper == NULL) 246e1e1452dSArchie Cobbs return (0); 247e1e1452dSArchie Cobbs 248e1e1452dSArchie Cobbs /* Send it out "upper" hook */ 249069154d5SJulian Elischer NG_SEND_DATA_ONLY(error, priv->upper, *mp); 250e1e1452dSArchie Cobbs return (error); 251e1e1452dSArchie Cobbs } 252e1e1452dSArchie Cobbs 253e1e1452dSArchie Cobbs /* 254e1e1452dSArchie Cobbs * A new Ethernet interface has been attached. 255e1e1452dSArchie Cobbs * Create a new node for it, etc. 256e1e1452dSArchie Cobbs */ 257e1e1452dSArchie Cobbs static void 258e1e1452dSArchie Cobbs ng_ether_attach(struct ifnet *ifp) 259e1e1452dSArchie Cobbs { 260e1e1452dSArchie Cobbs priv_p priv; 261e1e1452dSArchie Cobbs node_p node; 262e1e1452dSArchie Cobbs 263e1e1452dSArchie Cobbs /* Create node */ 2646e551fb6SDavid E. O'Brien KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__)); 265e1e1452dSArchie Cobbs if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 266e1e1452dSArchie Cobbs log(LOG_ERR, "%s: can't %s for %s\n", 2679bf40edeSBrooks Davis __func__, "create node", ifp->if_xname); 268e1e1452dSArchie Cobbs return; 269e1e1452dSArchie Cobbs } 270e1e1452dSArchie Cobbs 271e1e1452dSArchie Cobbs /* Allocate private data */ 27299cdf4ccSDavid Malone MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 273e1e1452dSArchie Cobbs if (priv == NULL) { 274e1e1452dSArchie Cobbs log(LOG_ERR, "%s: can't %s for %s\n", 2759bf40edeSBrooks Davis __func__, "allocate memory", ifp->if_xname); 27630400f03SJulian Elischer NG_NODE_UNREF(node); 277e1e1452dSArchie Cobbs return; 278e1e1452dSArchie Cobbs } 27930400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, priv); 280e1e1452dSArchie Cobbs priv->ifp = ifp; 281445e045bSAlexander Kabaev IFP2NG_SET(ifp, node); 2824b39c3c7SArchie Cobbs priv->autoSrcAddr = 1; 283a3e232d6SArchie Cobbs priv->hwassist = ifp->if_hwassist; 284e1e1452dSArchie Cobbs 285e1e1452dSArchie Cobbs /* Try to give the node the same name as the interface */ 2869bf40edeSBrooks Davis if (ng_name_node(node, ifp->if_xname) != 0) { 287e1e1452dSArchie Cobbs log(LOG_WARNING, "%s: can't name node %s\n", 2889bf40edeSBrooks Davis __func__, ifp->if_xname); 289e1e1452dSArchie Cobbs } 290e1e1452dSArchie Cobbs } 291e1e1452dSArchie Cobbs 292e1e1452dSArchie Cobbs /* 293e1e1452dSArchie Cobbs * An Ethernet interface is being detached. 2941acb27c6SJulian Elischer * REALLY Destroy its node. 295e1e1452dSArchie Cobbs */ 296e1e1452dSArchie Cobbs static void 297e1e1452dSArchie Cobbs ng_ether_detach(struct ifnet *ifp) 298e1e1452dSArchie Cobbs { 299e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp); 3001acb27c6SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 301e1e1452dSArchie Cobbs 302e1e1452dSArchie Cobbs if (node == NULL) /* no node (why not?), ignore */ 303e1e1452dSArchie Cobbs return; 3041acb27c6SJulian Elischer NG_NODE_REALLY_DIE(node); /* Force real removal of node */ 3051acb27c6SJulian Elischer /* 3061acb27c6SJulian Elischer * We can't assume the ifnet is still around when we run shutdown 3071acb27c6SJulian Elischer * So zap it now. XXX We HOPE that anything running at this time 3081acb27c6SJulian Elischer * handles it (as it should in the non netgraph case). 3091acb27c6SJulian Elischer */ 310445e045bSAlexander Kabaev IFP2NG_SET(ifp, NULL); 3111acb27c6SJulian Elischer priv->ifp = NULL; /* XXX race if interrupted an output packet */ 3121acb27c6SJulian Elischer ng_rmnode_self(node); /* remove all netgraph parts */ 313e1e1452dSArchie Cobbs } 314e1e1452dSArchie Cobbs 315e1e1452dSArchie Cobbs /****************************************************************** 316e1e1452dSArchie Cobbs NETGRAPH NODE METHODS 317e1e1452dSArchie Cobbs ******************************************************************/ 318e1e1452dSArchie Cobbs 319e1e1452dSArchie Cobbs /* 320e1e1452dSArchie Cobbs * It is not possible or allowable to create a node of this type. 321e1e1452dSArchie Cobbs * Nodes get created when the interface is attached (or, when 322e1e1452dSArchie Cobbs * this node type's KLD is loaded). 323e1e1452dSArchie Cobbs */ 324e1e1452dSArchie Cobbs static int 325069154d5SJulian Elischer ng_ether_constructor(node_p node) 326e1e1452dSArchie Cobbs { 327e1e1452dSArchie Cobbs return (EINVAL); 328e1e1452dSArchie Cobbs } 329e1e1452dSArchie Cobbs 330e1e1452dSArchie Cobbs /* 331e1e1452dSArchie Cobbs * Check for attaching a new hook. 332e1e1452dSArchie Cobbs */ 333e1e1452dSArchie Cobbs static int 334e1e1452dSArchie Cobbs ng_ether_newhook(node_p node, hook_p hook, const char *name) 335e1e1452dSArchie Cobbs { 33630400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 337e1e1452dSArchie Cobbs hook_p *hookptr; 338e1e1452dSArchie Cobbs 339e1e1452dSArchie Cobbs /* Divert hook is an alias for lower */ 340e1e1452dSArchie Cobbs if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0) 341e1e1452dSArchie Cobbs name = NG_ETHER_HOOK_LOWER; 342e1e1452dSArchie Cobbs 343e1e1452dSArchie Cobbs /* Which hook? */ 344e1e1452dSArchie Cobbs if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) 345e1e1452dSArchie Cobbs hookptr = &priv->upper; 3461a292b80SArchie Cobbs else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) 347e1e1452dSArchie Cobbs hookptr = &priv->lower; 3481a292b80SArchie Cobbs else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) 3491a292b80SArchie Cobbs hookptr = &priv->orphan; 3501a292b80SArchie Cobbs else 351e1e1452dSArchie Cobbs return (EINVAL); 352e1e1452dSArchie Cobbs 353e1e1452dSArchie Cobbs /* Check if already connected (shouldn't be, but doesn't hurt) */ 354e1e1452dSArchie Cobbs if (*hookptr != NULL) 355e1e1452dSArchie Cobbs return (EISCONN); 356e1e1452dSArchie Cobbs 357a3e232d6SArchie Cobbs /* Disable hardware checksums while 'upper' hook is connected */ 358a3e232d6SArchie Cobbs if (hookptr == &priv->upper) 359a3e232d6SArchie Cobbs priv->ifp->if_hwassist = 0; 360a3e232d6SArchie Cobbs 361e1e1452dSArchie Cobbs /* OK */ 362e1e1452dSArchie Cobbs *hookptr = hook; 363e1e1452dSArchie Cobbs return (0); 364e1e1452dSArchie Cobbs } 365e1e1452dSArchie Cobbs 366e1e1452dSArchie Cobbs /* 367859a4d16SJulian Elischer * Hooks are attached, adjust to force queueing. 368859a4d16SJulian Elischer * We don't really care which hook it is. 369859a4d16SJulian Elischer * they should all be queuing for outgoing data. 370859a4d16SJulian Elischer */ 371859a4d16SJulian Elischer static int 372859a4d16SJulian Elischer ng_ether_connect(hook_p hook) 373859a4d16SJulian Elischer { 37430400f03SJulian Elischer NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook)); 375859a4d16SJulian Elischer return (0); 376859a4d16SJulian Elischer } 377859a4d16SJulian Elischer 378859a4d16SJulian Elischer /* 379e1e1452dSArchie Cobbs * Receive an incoming control message. 380e1e1452dSArchie Cobbs */ 381e1e1452dSArchie Cobbs static int 382069154d5SJulian Elischer ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook) 383e1e1452dSArchie Cobbs { 38430400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 385e1e1452dSArchie Cobbs struct ng_mesg *resp = NULL; 386e1e1452dSArchie Cobbs int error = 0; 387069154d5SJulian Elischer struct ng_mesg *msg; 388e1e1452dSArchie Cobbs 389069154d5SJulian Elischer NGI_GET_MSG(item, msg); 390e1e1452dSArchie Cobbs switch (msg->header.typecookie) { 391e1e1452dSArchie Cobbs case NGM_ETHER_COOKIE: 392e1e1452dSArchie Cobbs switch (msg->header.cmd) { 393e1e1452dSArchie Cobbs case NGM_ETHER_GET_IFNAME: 394e1e1452dSArchie Cobbs NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT); 395e1e1452dSArchie Cobbs if (resp == NULL) { 396e1e1452dSArchie Cobbs error = ENOMEM; 397e1e1452dSArchie Cobbs break; 398e1e1452dSArchie Cobbs } 3999bf40edeSBrooks Davis strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ + 1); 400e1e1452dSArchie Cobbs break; 401e1e1452dSArchie Cobbs case NGM_ETHER_GET_IFINDEX: 402e1e1452dSArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 403e1e1452dSArchie Cobbs if (resp == NULL) { 404e1e1452dSArchie Cobbs error = ENOMEM; 405e1e1452dSArchie Cobbs break; 406e1e1452dSArchie Cobbs } 407e1e1452dSArchie Cobbs *((u_int32_t *)resp->data) = priv->ifp->if_index; 408e1e1452dSArchie Cobbs break; 4094b39c3c7SArchie Cobbs case NGM_ETHER_GET_ENADDR: 4104b39c3c7SArchie Cobbs NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT); 4114b39c3c7SArchie Cobbs if (resp == NULL) { 4124b39c3c7SArchie Cobbs error = ENOMEM; 4134b39c3c7SArchie Cobbs break; 4144b39c3c7SArchie Cobbs } 4154b39c3c7SArchie Cobbs bcopy((IFP2AC(priv->ifp))->ac_enaddr, 4164b39c3c7SArchie Cobbs resp->data, ETHER_ADDR_LEN); 4174b39c3c7SArchie Cobbs break; 41856045c66SArchie Cobbs case NGM_ETHER_SET_ENADDR: 41956045c66SArchie Cobbs { 42056045c66SArchie Cobbs if (msg->header.arglen != ETHER_ADDR_LEN) { 42156045c66SArchie Cobbs error = EINVAL; 42256045c66SArchie Cobbs break; 42356045c66SArchie Cobbs } 42456045c66SArchie Cobbs error = if_setlladdr(priv->ifp, 42556045c66SArchie Cobbs (u_char *)msg->data, ETHER_ADDR_LEN); 42656045c66SArchie Cobbs break; 42756045c66SArchie Cobbs } 42856045c66SArchie Cobbs case NGM_ETHER_GET_PROMISC: 42956045c66SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 43056045c66SArchie Cobbs if (resp == NULL) { 43156045c66SArchie Cobbs error = ENOMEM; 43256045c66SArchie Cobbs break; 43356045c66SArchie Cobbs } 43456045c66SArchie Cobbs *((u_int32_t *)resp->data) = priv->promisc; 43556045c66SArchie Cobbs break; 4364b39c3c7SArchie Cobbs case NGM_ETHER_SET_PROMISC: 4374b39c3c7SArchie Cobbs { 4384b39c3c7SArchie Cobbs u_char want; 4394b39c3c7SArchie Cobbs 4404b39c3c7SArchie Cobbs if (msg->header.arglen != sizeof(u_int32_t)) { 4414b39c3c7SArchie Cobbs error = EINVAL; 4424b39c3c7SArchie Cobbs break; 4434b39c3c7SArchie Cobbs } 4444b39c3c7SArchie Cobbs want = !!*((u_int32_t *)msg->data); 4454b39c3c7SArchie Cobbs if (want ^ priv->promisc) { 4464b39c3c7SArchie Cobbs if ((error = ifpromisc(priv->ifp, want)) != 0) 4474b39c3c7SArchie Cobbs break; 4484b39c3c7SArchie Cobbs priv->promisc = want; 4494b39c3c7SArchie Cobbs } 4504b39c3c7SArchie Cobbs break; 4514b39c3c7SArchie Cobbs } 45256045c66SArchie Cobbs case NGM_ETHER_GET_AUTOSRC: 45356045c66SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 45456045c66SArchie Cobbs if (resp == NULL) { 45556045c66SArchie Cobbs error = ENOMEM; 45656045c66SArchie Cobbs break; 45756045c66SArchie Cobbs } 45856045c66SArchie Cobbs *((u_int32_t *)resp->data) = priv->autoSrcAddr; 45956045c66SArchie Cobbs break; 4604b39c3c7SArchie Cobbs case NGM_ETHER_SET_AUTOSRC: 4614b39c3c7SArchie Cobbs if (msg->header.arglen != sizeof(u_int32_t)) { 4624b39c3c7SArchie Cobbs error = EINVAL; 4634b39c3c7SArchie Cobbs break; 4644b39c3c7SArchie Cobbs } 4654b39c3c7SArchie Cobbs priv->autoSrcAddr = !!*((u_int32_t *)msg->data); 4664b39c3c7SArchie Cobbs break; 467e1e1452dSArchie Cobbs default: 468e1e1452dSArchie Cobbs error = EINVAL; 469e1e1452dSArchie Cobbs break; 470e1e1452dSArchie Cobbs } 471e1e1452dSArchie Cobbs break; 472e1e1452dSArchie Cobbs default: 473e1e1452dSArchie Cobbs error = EINVAL; 474e1e1452dSArchie Cobbs break; 475e1e1452dSArchie Cobbs } 476069154d5SJulian Elischer NG_RESPOND_MSG(error, node, item, resp); 477069154d5SJulian Elischer NG_FREE_MSG(msg); 478e1e1452dSArchie Cobbs return (error); 479e1e1452dSArchie Cobbs } 480e1e1452dSArchie Cobbs 481e1e1452dSArchie Cobbs /* 482e1e1452dSArchie Cobbs * Receive data on a hook. 483e1e1452dSArchie Cobbs */ 484e1e1452dSArchie Cobbs static int 485069154d5SJulian Elischer ng_ether_rcvdata(hook_p hook, item_p item) 486e1e1452dSArchie Cobbs { 48730400f03SJulian Elischer const node_p node = NG_HOOK_NODE(hook); 48830400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 489069154d5SJulian Elischer struct mbuf *m; 490e1e1452dSArchie Cobbs 491069154d5SJulian Elischer NGI_GET_M(item, m); 492069154d5SJulian Elischer NG_FREE_ITEM(item); 4933ca24c28SJulian Elischer 4941a292b80SArchie Cobbs if (hook == priv->lower || hook == priv->orphan) 4953ca24c28SJulian Elischer return ng_ether_rcv_lower(node, m); 496e1e1452dSArchie Cobbs if (hook == priv->upper) 4973ca24c28SJulian Elischer return ng_ether_rcv_upper(node, m); 4986e551fb6SDavid E. O'Brien panic("%s: weird hook", __func__); 4991a292b80SArchie Cobbs #ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */ 5007ea5573cSDag-Erling Smørgrav return (0); 501b40ce416SJulian Elischer #endif 502e1e1452dSArchie Cobbs } 503e1e1452dSArchie Cobbs 504e1e1452dSArchie Cobbs /* 5051a292b80SArchie Cobbs * Handle an mbuf received on the "lower" or "orphan" hook. 506e1e1452dSArchie Cobbs */ 507e1e1452dSArchie Cobbs static int 5083ca24c28SJulian Elischer ng_ether_rcv_lower(node_p node, struct mbuf *m) 509e1e1452dSArchie Cobbs { 51030400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 511a1479aa2SArchie Cobbs struct ifnet *const ifp = priv->ifp; 512a1479aa2SArchie Cobbs 513a1479aa2SArchie Cobbs /* Check whether interface is ready for packets */ 514a1479aa2SArchie Cobbs if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { 515a1479aa2SArchie Cobbs NG_FREE_M(m); 516a1479aa2SArchie Cobbs return (ENETDOWN); 517a1479aa2SArchie Cobbs } 518e1e1452dSArchie Cobbs 519e1e1452dSArchie Cobbs /* Make sure header is fully pulled up */ 520e1e1452dSArchie Cobbs if (m->m_pkthdr.len < sizeof(struct ether_header)) { 521069154d5SJulian Elischer NG_FREE_M(m); 522e1e1452dSArchie Cobbs return (EINVAL); 523e1e1452dSArchie Cobbs } 524e1e1452dSArchie Cobbs if (m->m_len < sizeof(struct ether_header) 5257b9f235fSArchie Cobbs && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 526e1e1452dSArchie Cobbs return (ENOBUFS); 527e1e1452dSArchie Cobbs 5284b39c3c7SArchie Cobbs /* Drop in the MAC address if desired */ 5294b39c3c7SArchie Cobbs if (priv->autoSrcAddr) { 5307b9f235fSArchie Cobbs 5317b9f235fSArchie Cobbs /* Make the mbuf writable if it's not already */ 5327b9f235fSArchie Cobbs if (!M_WRITABLE(m) 5337b9f235fSArchie Cobbs && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 5347b9f235fSArchie Cobbs return (ENOBUFS); 5357b9f235fSArchie Cobbs 5367b9f235fSArchie Cobbs /* Overwrite source MAC address */ 537a1479aa2SArchie Cobbs bcopy((IFP2AC(ifp))->ac_enaddr, 5384b39c3c7SArchie Cobbs mtod(m, struct ether_header *)->ether_shost, 5394b39c3c7SArchie Cobbs ETHER_ADDR_LEN); 5404b39c3c7SArchie Cobbs } 541561e4fb9SJulian Elischer 542e1e1452dSArchie Cobbs /* Send it on its way */ 543a1479aa2SArchie Cobbs return ether_output_frame(ifp, m); 544e1e1452dSArchie Cobbs } 545e1e1452dSArchie Cobbs 546e1e1452dSArchie Cobbs /* 547e1e1452dSArchie Cobbs * Handle an mbuf received on the "upper" hook. 548e1e1452dSArchie Cobbs */ 549e1e1452dSArchie Cobbs static int 5503ca24c28SJulian Elischer ng_ether_rcv_upper(node_p node, struct mbuf *m) 551e1e1452dSArchie Cobbs { 55230400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 553e1e1452dSArchie Cobbs 554a62b20c4SJulian Elischer m->m_pkthdr.rcvif = priv->ifp; 555e1e1452dSArchie Cobbs 556a176c2aeSGleb Smirnoff if (BDG_ACTIVE(priv->ifp) ) 557a176c2aeSGleb Smirnoff if ((m = bridge_in_ptr(priv->ifp, m)) == NULL) 558a176c2aeSGleb Smirnoff return (0); 559a176c2aeSGleb Smirnoff 560e1e1452dSArchie Cobbs /* Route packet back in */ 561edbb5246SSam Leffler ether_demux(priv->ifp, m); 562e1e1452dSArchie Cobbs return (0); 563e1e1452dSArchie Cobbs } 564e1e1452dSArchie Cobbs 565e1e1452dSArchie Cobbs /* 5661acb27c6SJulian Elischer * Shutdown node. This resets the node but does not remove it 5671acb27c6SJulian Elischer * unless the REALLY_DIE flag is set. 568e1e1452dSArchie Cobbs */ 569e1e1452dSArchie Cobbs static int 570069154d5SJulian Elischer ng_ether_shutdown(node_p node) 571e1e1452dSArchie Cobbs { 57230400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 5734b39c3c7SArchie Cobbs 574be4252b3SJulian Elischer if (node->nd_flags & NGF_REALLY_DIE) { 5751acb27c6SJulian Elischer /* 5761acb27c6SJulian Elischer * WE came here because the ethernet card is being unloaded, 5771acb27c6SJulian Elischer * so stop being persistant. 5781acb27c6SJulian Elischer * Actually undo all the things we did on creation. 5791acb27c6SJulian Elischer * Assume the ifp has already been freed. 5801acb27c6SJulian Elischer */ 5811acb27c6SJulian Elischer NG_NODE_SET_PRIVATE(node, NULL); 5821acb27c6SJulian Elischer FREE(priv, M_NETGRAPH); 5831acb27c6SJulian Elischer NG_NODE_UNREF(node); /* free node itself */ 5841acb27c6SJulian Elischer return (0); 585069154d5SJulian Elischer } 586018df1c3SBrian Feldman if (priv->promisc) { /* disable promiscuous mode */ 587018df1c3SBrian Feldman (void)ifpromisc(priv->ifp, 0); 588018df1c3SBrian Feldman priv->promisc = 0; 589018df1c3SBrian Feldman } 5904b39c3c7SArchie Cobbs priv->autoSrcAddr = 1; /* reset auto-src-addr flag */ 591be4252b3SJulian Elischer NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */ 592be4252b3SJulian Elischer 593e1e1452dSArchie Cobbs return (0); 594e1e1452dSArchie Cobbs } 595e1e1452dSArchie Cobbs 596e1e1452dSArchie Cobbs /* 597e1e1452dSArchie Cobbs * Hook disconnection. 598e1e1452dSArchie Cobbs */ 599e1e1452dSArchie Cobbs static int 600e1e1452dSArchie Cobbs ng_ether_disconnect(hook_p hook) 601e1e1452dSArchie Cobbs { 60230400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 603e1e1452dSArchie Cobbs 604a3e232d6SArchie Cobbs if (hook == priv->upper) { 605e1e1452dSArchie Cobbs priv->upper = NULL; 606b712e9ecSBrian Feldman if (priv->ifp != NULL) /* restore h/w csum */ 607b712e9ecSBrian Feldman priv->ifp->if_hwassist = priv->hwassist; 6081a292b80SArchie Cobbs } else if (hook == priv->lower) 609e1e1452dSArchie Cobbs priv->lower = NULL; 6101a292b80SArchie Cobbs else if (hook == priv->orphan) 6111a292b80SArchie Cobbs priv->orphan = NULL; 6121a292b80SArchie Cobbs else 6136e551fb6SDavid E. O'Brien panic("%s: weird hook", __func__); 61430400f03SJulian Elischer if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 61530400f03SJulian Elischer && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 61630400f03SJulian Elischer ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */ 617e1e1452dSArchie Cobbs return (0); 618e1e1452dSArchie Cobbs } 619e1e1452dSArchie Cobbs 620e1e1452dSArchie Cobbs /****************************************************************** 621e1e1452dSArchie Cobbs INITIALIZATION 622e1e1452dSArchie Cobbs ******************************************************************/ 623e1e1452dSArchie Cobbs 624e1e1452dSArchie Cobbs /* 625e1e1452dSArchie Cobbs * Handle loading and unloading for this node type. 626e1e1452dSArchie Cobbs */ 627e1e1452dSArchie Cobbs static int 628e1e1452dSArchie Cobbs ng_ether_mod_event(module_t mod, int event, void *data) 629e1e1452dSArchie Cobbs { 630e1e1452dSArchie Cobbs struct ifnet *ifp; 631e1e1452dSArchie Cobbs int error = 0; 632e1e1452dSArchie Cobbs int s; 633e1e1452dSArchie Cobbs 634e1e1452dSArchie Cobbs s = splnet(); 635e1e1452dSArchie Cobbs switch (event) { 636e1e1452dSArchie Cobbs case MOD_LOAD: 637e1e1452dSArchie Cobbs 638e1e1452dSArchie Cobbs /* Register function hooks */ 639e1e1452dSArchie Cobbs if (ng_ether_attach_p != NULL) { 640e1e1452dSArchie Cobbs error = EEXIST; 641e1e1452dSArchie Cobbs break; 642e1e1452dSArchie Cobbs } 643e1e1452dSArchie Cobbs ng_ether_attach_p = ng_ether_attach; 644e1e1452dSArchie Cobbs ng_ether_detach_p = ng_ether_detach; 645e1e1452dSArchie Cobbs ng_ether_output_p = ng_ether_output; 646e1e1452dSArchie Cobbs ng_ether_input_p = ng_ether_input; 647e1e1452dSArchie Cobbs ng_ether_input_orphan_p = ng_ether_input_orphan; 648e1e1452dSArchie Cobbs 649e1e1452dSArchie Cobbs /* Create nodes for any already-existing Ethernet interfaces */ 650b30a244cSJeffrey Hsu IFNET_RLOCK(); 651e1e1452dSArchie Cobbs TAILQ_FOREACH(ifp, &ifnet, if_link) { 652cf2010b8SArchie Cobbs if (ifp->if_type == IFT_ETHER 653cf2010b8SArchie Cobbs || ifp->if_type == IFT_L2VLAN) 654e1e1452dSArchie Cobbs ng_ether_attach(ifp); 655e1e1452dSArchie Cobbs } 656b30a244cSJeffrey Hsu IFNET_RUNLOCK(); 657e1e1452dSArchie Cobbs break; 658e1e1452dSArchie Cobbs 659e1e1452dSArchie Cobbs case MOD_UNLOAD: 660e1e1452dSArchie Cobbs 661e1e1452dSArchie Cobbs /* 662e1e1452dSArchie Cobbs * Note that the base code won't try to unload us until 663e1e1452dSArchie Cobbs * all nodes have been removed, and that can't happen 664e1e1452dSArchie Cobbs * until all Ethernet interfaces are removed. In any 665e1e1452dSArchie Cobbs * case, we know there are no nodes left if the action 666e1e1452dSArchie Cobbs * is MOD_UNLOAD, so there's no need to detach any nodes. 667e1e1452dSArchie Cobbs */ 668e1e1452dSArchie Cobbs 669e1e1452dSArchie Cobbs /* Unregister function hooks */ 670e1e1452dSArchie Cobbs ng_ether_attach_p = NULL; 671e1e1452dSArchie Cobbs ng_ether_detach_p = NULL; 672e1e1452dSArchie Cobbs ng_ether_output_p = NULL; 673e1e1452dSArchie Cobbs ng_ether_input_p = NULL; 674e1e1452dSArchie Cobbs ng_ether_input_orphan_p = NULL; 675e1e1452dSArchie Cobbs break; 676e1e1452dSArchie Cobbs 677e1e1452dSArchie Cobbs default: 678e1e1452dSArchie Cobbs error = EOPNOTSUPP; 679e1e1452dSArchie Cobbs break; 680e1e1452dSArchie Cobbs } 681e1e1452dSArchie Cobbs splx(s); 682e1e1452dSArchie Cobbs return (error); 683e1e1452dSArchie Cobbs } 684e1e1452dSArchie Cobbs 685