1e1e1452dSArchie Cobbs 2e1e1452dSArchie Cobbs /* 3e1e1452dSArchie Cobbs * ng_ether.c 4c398230bSWarner Losh */ 5c398230bSWarner Losh 6c398230bSWarner Losh /*- 7e1e1452dSArchie Cobbs * Copyright (c) 1996-2000 Whistle Communications, Inc. 8e1e1452dSArchie Cobbs * All rights reserved. 9e1e1452dSArchie Cobbs * 10e1e1452dSArchie Cobbs * Subject to the following obligations and disclaimer of warranty, use and 11e1e1452dSArchie Cobbs * redistribution of this software, in source or object code forms, with or 12e1e1452dSArchie Cobbs * without modifications are expressly permitted by Whistle Communications; 13e1e1452dSArchie Cobbs * provided, however, that: 14e1e1452dSArchie Cobbs * 1. Any and all reproductions of the source or object code must include the 15e1e1452dSArchie Cobbs * copyright notice above and the following disclaimer of warranties; and 16e1e1452dSArchie Cobbs * 2. No rights are granted, in any manner or form, to use Whistle 17e1e1452dSArchie Cobbs * Communications, Inc. trademarks, including the mark "WHISTLE 18e1e1452dSArchie Cobbs * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 19e1e1452dSArchie Cobbs * such appears in the above copyright notice or in the software. 20e1e1452dSArchie Cobbs * 21e1e1452dSArchie Cobbs * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 22e1e1452dSArchie Cobbs * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 23e1e1452dSArchie Cobbs * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 24e1e1452dSArchie Cobbs * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 25e1e1452dSArchie Cobbs * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 26e1e1452dSArchie Cobbs * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 27e1e1452dSArchie Cobbs * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 28e1e1452dSArchie Cobbs * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 29e1e1452dSArchie Cobbs * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 30e1e1452dSArchie Cobbs * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 31e1e1452dSArchie Cobbs * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 32e1e1452dSArchie Cobbs * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 33e1e1452dSArchie Cobbs * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 34e1e1452dSArchie Cobbs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35e1e1452dSArchie Cobbs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 36e1e1452dSArchie Cobbs * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 37e1e1452dSArchie Cobbs * OF SUCH DAMAGE. 38e1e1452dSArchie Cobbs * 39e1e1452dSArchie Cobbs * Authors: Archie Cobbs <archie@freebsd.org> 40e1e1452dSArchie Cobbs * Julian Elischer <julian@freebsd.org> 41e1e1452dSArchie Cobbs * 42e1e1452dSArchie Cobbs * $FreeBSD$ 43e1e1452dSArchie Cobbs */ 44e1e1452dSArchie Cobbs 45e1e1452dSArchie Cobbs /* 46e1e1452dSArchie Cobbs * ng_ether(4) netgraph node type 47e1e1452dSArchie Cobbs */ 48e1e1452dSArchie Cobbs 49e1e1452dSArchie Cobbs #include <sys/param.h> 50c3322cb9SGleb Smirnoff #include <sys/eventhandler.h> 51e1e1452dSArchie Cobbs #include <sys/systm.h> 52e1e1452dSArchie Cobbs #include <sys/kernel.h> 53e1e1452dSArchie Cobbs #include <sys/malloc.h> 54e1e1452dSArchie Cobbs #include <sys/mbuf.h> 55e1e1452dSArchie Cobbs #include <sys/errno.h> 56530c0060SRobert Watson #include <sys/proc.h> 57e1e1452dSArchie Cobbs #include <sys/syslog.h> 58e1e1452dSArchie Cobbs #include <sys/socket.h> 592cdf8c49SMarko Zec #include <sys/taskqueue.h> 60e1e1452dSArchie Cobbs 61e1e1452dSArchie Cobbs #include <net/if.h> 62810d5e89SGleb Smirnoff #include <net/if_dl.h> 63e1e1452dSArchie Cobbs #include <net/if_types.h> 64e1e1452dSArchie Cobbs #include <net/if_arp.h> 65e1e1452dSArchie Cobbs #include <net/if_var.h> 66e1e1452dSArchie Cobbs #include <net/ethernet.h> 67fd6238a6SAndrew Thompson #include <net/if_bridgevar.h> 684b79449eSBjoern A. Zeeb #include <net/vnet.h> 69e1e1452dSArchie Cobbs 70e1e1452dSArchie Cobbs #include <netgraph/ng_message.h> 71e1e1452dSArchie Cobbs #include <netgraph/netgraph.h> 72e1e1452dSArchie Cobbs #include <netgraph/ng_parse.h> 73e1e1452dSArchie Cobbs #include <netgraph/ng_ether.h> 74e1e1452dSArchie Cobbs 7539b553ceSEd Maste MODULE_VERSION(ng_ether, 1); 7639b553ceSEd Maste 77*833e8dc5SGleb Smirnoff #define IFP2NG(ifp) ((ifp)->if_l2com) 78e1e1452dSArchie Cobbs 793c976c3fSPawel Jakub Dawidek /* Per-node private data */ 803c976c3fSPawel Jakub Dawidek struct private { 813c976c3fSPawel Jakub Dawidek struct ifnet *ifp; /* associated interface */ 823c976c3fSPawel Jakub Dawidek hook_p upper; /* upper hook connection */ 831a292b80SArchie Cobbs hook_p lower; /* lower hook connection */ 841a292b80SArchie Cobbs hook_p orphan; /* orphan hook connection */ 853c976c3fSPawel Jakub Dawidek u_char autoSrcAddr; /* always overwrite source address */ 863c976c3fSPawel Jakub Dawidek u_char promisc; /* promiscuous mode enabled */ 873c976c3fSPawel Jakub Dawidek u_long hwassist; /* hardware checksum capabilities */ 883c976c3fSPawel Jakub Dawidek u_int flags; /* flags e.g. really die */ 893c976c3fSPawel Jakub Dawidek }; 903c976c3fSPawel Jakub Dawidek typedef struct private *priv_p; 91e1e1452dSArchie Cobbs 92edbb5246SSam Leffler /* Hook pointers used by if_ethersubr.c to callback to netgraph */ 93edbb5246SSam Leffler extern void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 94edbb5246SSam Leffler extern void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 95edbb5246SSam Leffler extern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 96edbb5246SSam Leffler extern void (*ng_ether_attach_p)(struct ifnet *ifp); 97edbb5246SSam Leffler extern void (*ng_ether_detach_p)(struct ifnet *ifp); 981c7899c7SGleb Smirnoff extern void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 99edbb5246SSam Leffler 100e1e1452dSArchie Cobbs /* Functional hooks called from if_ethersubr.c */ 101edbb5246SSam Leffler static void ng_ether_input(struct ifnet *ifp, struct mbuf **mp); 102edbb5246SSam Leffler static void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m); 103e1e1452dSArchie Cobbs static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp); 104e1e1452dSArchie Cobbs static void ng_ether_attach(struct ifnet *ifp); 105e1e1452dSArchie Cobbs static void ng_ether_detach(struct ifnet *ifp); 1061c7899c7SGleb Smirnoff static void ng_ether_link_state(struct ifnet *ifp, int state); 107e1e1452dSArchie Cobbs 108e1e1452dSArchie Cobbs /* Other functions */ 109f664dcdeSJulian Elischer static int ng_ether_rcv_lower(hook_p node, item_p item); 110f664dcdeSJulian Elischer static int ng_ether_rcv_upper(hook_p node, item_p item); 111e1e1452dSArchie Cobbs 112e1e1452dSArchie Cobbs /* Netgraph node methods */ 113e1e1452dSArchie Cobbs static ng_constructor_t ng_ether_constructor; 114e1e1452dSArchie Cobbs static ng_rcvmsg_t ng_ether_rcvmsg; 115069154d5SJulian Elischer static ng_shutdown_t ng_ether_shutdown; 116e1e1452dSArchie Cobbs static ng_newhook_t ng_ether_newhook; 117e1e1452dSArchie Cobbs static ng_rcvdata_t ng_ether_rcvdata; 118e1e1452dSArchie Cobbs static ng_disconnect_t ng_ether_disconnect; 119e1e1452dSArchie Cobbs static int ng_ether_mod_event(module_t mod, int event, void *data); 120e1e1452dSArchie Cobbs 121499f60b1SAndriy Gapon static eventhandler_tag ng_ether_ifnet_arrival_cookie; 122499f60b1SAndriy Gapon 123e1e1452dSArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */ 124e1e1452dSArchie Cobbs static const struct ng_cmdlist ng_ether_cmdlist[] = { 125e1e1452dSArchie Cobbs { 126e1e1452dSArchie Cobbs NGM_ETHER_COOKIE, 127e1e1452dSArchie Cobbs NGM_ETHER_GET_IFNAME, 128e1e1452dSArchie Cobbs "getifname", 129e1e1452dSArchie Cobbs NULL, 130e1e1452dSArchie Cobbs &ng_parse_string_type 131e1e1452dSArchie Cobbs }, 132e1e1452dSArchie Cobbs { 133e1e1452dSArchie Cobbs NGM_ETHER_COOKIE, 134e1e1452dSArchie Cobbs NGM_ETHER_GET_IFINDEX, 135e1e1452dSArchie Cobbs "getifindex", 136e1e1452dSArchie Cobbs NULL, 137e1e1452dSArchie Cobbs &ng_parse_int32_type 138e1e1452dSArchie Cobbs }, 1394b39c3c7SArchie Cobbs { 1404b39c3c7SArchie Cobbs NGM_ETHER_COOKIE, 1414b39c3c7SArchie Cobbs NGM_ETHER_GET_ENADDR, 1424b39c3c7SArchie Cobbs "getenaddr", 1434b39c3c7SArchie Cobbs NULL, 1448c7e4101SRuslan Ermilov &ng_parse_enaddr_type 1454b39c3c7SArchie Cobbs }, 1464b39c3c7SArchie Cobbs { 1474b39c3c7SArchie Cobbs NGM_ETHER_COOKIE, 14856045c66SArchie Cobbs NGM_ETHER_SET_ENADDR, 14956045c66SArchie Cobbs "setenaddr", 1508c7e4101SRuslan Ermilov &ng_parse_enaddr_type, 15156045c66SArchie Cobbs NULL 15256045c66SArchie Cobbs }, 15356045c66SArchie Cobbs { 15456045c66SArchie Cobbs NGM_ETHER_COOKIE, 15556045c66SArchie Cobbs NGM_ETHER_GET_PROMISC, 15656045c66SArchie Cobbs "getpromisc", 15756045c66SArchie Cobbs NULL, 15856045c66SArchie Cobbs &ng_parse_int32_type 15956045c66SArchie Cobbs }, 16056045c66SArchie Cobbs { 16156045c66SArchie Cobbs NGM_ETHER_COOKIE, 1624b39c3c7SArchie Cobbs NGM_ETHER_SET_PROMISC, 1634b39c3c7SArchie Cobbs "setpromisc", 1644b39c3c7SArchie Cobbs &ng_parse_int32_type, 1654b39c3c7SArchie Cobbs NULL 1664b39c3c7SArchie Cobbs }, 1674b39c3c7SArchie Cobbs { 1684b39c3c7SArchie Cobbs NGM_ETHER_COOKIE, 16956045c66SArchie Cobbs NGM_ETHER_GET_AUTOSRC, 17056045c66SArchie Cobbs "getautosrc", 17156045c66SArchie Cobbs NULL, 17256045c66SArchie Cobbs &ng_parse_int32_type 17356045c66SArchie Cobbs }, 17456045c66SArchie Cobbs { 17556045c66SArchie Cobbs NGM_ETHER_COOKIE, 1764b39c3c7SArchie Cobbs NGM_ETHER_SET_AUTOSRC, 1774b39c3c7SArchie Cobbs "setautosrc", 1784b39c3c7SArchie Cobbs &ng_parse_int32_type, 1794b39c3c7SArchie Cobbs NULL 1804b39c3c7SArchie Cobbs }, 181810d5e89SGleb Smirnoff { 182810d5e89SGleb Smirnoff NGM_ETHER_COOKIE, 183810d5e89SGleb Smirnoff NGM_ETHER_ADD_MULTI, 184810d5e89SGleb Smirnoff "addmulti", 185810d5e89SGleb Smirnoff &ng_parse_enaddr_type, 186810d5e89SGleb Smirnoff NULL 187810d5e89SGleb Smirnoff }, 188810d5e89SGleb Smirnoff { 189810d5e89SGleb Smirnoff NGM_ETHER_COOKIE, 190810d5e89SGleb Smirnoff NGM_ETHER_DEL_MULTI, 191810d5e89SGleb Smirnoff "delmulti", 192810d5e89SGleb Smirnoff &ng_parse_enaddr_type, 193810d5e89SGleb Smirnoff NULL 194810d5e89SGleb Smirnoff }, 195cefddd66SGleb Smirnoff { 196cefddd66SGleb Smirnoff NGM_ETHER_COOKIE, 197cefddd66SGleb Smirnoff NGM_ETHER_DETACH, 198cefddd66SGleb Smirnoff "detach", 199cefddd66SGleb Smirnoff NULL, 200cefddd66SGleb Smirnoff NULL 201cefddd66SGleb Smirnoff }, 202e1e1452dSArchie Cobbs { 0 } 203e1e1452dSArchie Cobbs }; 204e1e1452dSArchie Cobbs 205e1e1452dSArchie Cobbs static struct ng_type ng_ether_typestruct = { 206f8aae777SJulian Elischer .version = NG_ABI_VERSION, 207f8aae777SJulian Elischer .name = NG_ETHER_NODE_TYPE, 208f8aae777SJulian Elischer .mod_event = ng_ether_mod_event, 209f8aae777SJulian Elischer .constructor = ng_ether_constructor, 210f8aae777SJulian Elischer .rcvmsg = ng_ether_rcvmsg, 211f8aae777SJulian Elischer .shutdown = ng_ether_shutdown, 212f8aae777SJulian Elischer .newhook = ng_ether_newhook, 213f8aae777SJulian Elischer .rcvdata = ng_ether_rcvdata, 214f8aae777SJulian Elischer .disconnect = ng_ether_disconnect, 215f8aae777SJulian Elischer .cmdlist = ng_ether_cmdlist, 216e1e1452dSArchie Cobbs }; 217e1e1452dSArchie Cobbs NETGRAPH_INIT(ether, &ng_ether_typestruct); 218e1e1452dSArchie Cobbs 219e1e1452dSArchie Cobbs /****************************************************************** 220499f60b1SAndriy Gapon UTILITY FUNCTIONS 221499f60b1SAndriy Gapon ******************************************************************/ 222499f60b1SAndriy Gapon static void 223499f60b1SAndriy Gapon ng_ether_sanitize_ifname(const char *ifname, char *name) 224499f60b1SAndriy Gapon { 225499f60b1SAndriy Gapon int i; 226499f60b1SAndriy Gapon 227499f60b1SAndriy Gapon for (i = 0; i < IFNAMSIZ; i++) { 228499f60b1SAndriy Gapon if (ifname[i] == '.' || ifname[i] == ':') 229499f60b1SAndriy Gapon name[i] = '_'; 230499f60b1SAndriy Gapon else 231499f60b1SAndriy Gapon name[i] = ifname[i]; 232499f60b1SAndriy Gapon if (name[i] == '\0') 233499f60b1SAndriy Gapon break; 234499f60b1SAndriy Gapon } 235499f60b1SAndriy Gapon } 236499f60b1SAndriy Gapon 237499f60b1SAndriy Gapon /****************************************************************** 238e1e1452dSArchie Cobbs ETHERNET FUNCTION HOOKS 239e1e1452dSArchie Cobbs ******************************************************************/ 240e1e1452dSArchie Cobbs 241e1e1452dSArchie Cobbs /* 242e1e1452dSArchie Cobbs * Handle a packet that has come in on an interface. We get to 243e1e1452dSArchie Cobbs * look at it here before any upper layer protocols do. 244e1e1452dSArchie Cobbs */ 245e1e1452dSArchie Cobbs static void 246edbb5246SSam Leffler ng_ether_input(struct ifnet *ifp, struct mbuf **mp) 247e1e1452dSArchie Cobbs { 248e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp); 24930400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 2501a292b80SArchie Cobbs int error; 251e1e1452dSArchie Cobbs 252e1e1452dSArchie Cobbs /* If "lower" hook not connected, let packet continue */ 2531a292b80SArchie Cobbs if (priv->lower == NULL) 254e1e1452dSArchie Cobbs return; 2551a292b80SArchie Cobbs NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */ 256e1e1452dSArchie Cobbs } 257e1e1452dSArchie Cobbs 258e1e1452dSArchie Cobbs /* 259e1e1452dSArchie Cobbs * Handle a packet that has come in on an interface, and which 260e1e1452dSArchie Cobbs * does not match any of our known protocols (an ``orphan''). 261e1e1452dSArchie Cobbs */ 262e1e1452dSArchie Cobbs static void 263edbb5246SSam Leffler ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m) 264e1e1452dSArchie Cobbs { 265e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp); 26630400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 2671a292b80SArchie Cobbs int error; 268e1e1452dSArchie Cobbs 2691a292b80SArchie Cobbs /* If "orphan" hook not connected, discard packet */ 2701a292b80SArchie Cobbs if (priv->orphan == NULL) { 271e1e1452dSArchie Cobbs m_freem(m); 272e1e1452dSArchie Cobbs return; 273e1e1452dSArchie Cobbs } 2741a292b80SArchie Cobbs NG_SEND_DATA_ONLY(error, priv->orphan, m); 275e1e1452dSArchie Cobbs } 276e1e1452dSArchie Cobbs 277e1e1452dSArchie Cobbs /* 278e1e1452dSArchie Cobbs * Handle a packet that is going out on an interface. 279e1e1452dSArchie Cobbs * The Ethernet header is already attached to the mbuf. 280e1e1452dSArchie Cobbs */ 281e1e1452dSArchie Cobbs static int 282e1e1452dSArchie Cobbs ng_ether_output(struct ifnet *ifp, struct mbuf **mp) 283e1e1452dSArchie Cobbs { 284e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp); 28530400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 286e1e1452dSArchie Cobbs int error = 0; 287e1e1452dSArchie Cobbs 288e1e1452dSArchie Cobbs /* If "upper" hook not connected, let packet continue */ 289e1e1452dSArchie Cobbs if (priv->upper == NULL) 290e1e1452dSArchie Cobbs return (0); 291e1e1452dSArchie Cobbs 292e1e1452dSArchie Cobbs /* Send it out "upper" hook */ 293f089869fSMarko Zec NG_OUTBOUND_THREAD_REF(); 294069154d5SJulian Elischer NG_SEND_DATA_ONLY(error, priv->upper, *mp); 295f089869fSMarko Zec NG_OUTBOUND_THREAD_UNREF(); 296e1e1452dSArchie Cobbs return (error); 297e1e1452dSArchie Cobbs } 298e1e1452dSArchie Cobbs 299e1e1452dSArchie Cobbs /* 300e1e1452dSArchie Cobbs * A new Ethernet interface has been attached. 301e1e1452dSArchie Cobbs * Create a new node for it, etc. 302e1e1452dSArchie Cobbs */ 303e1e1452dSArchie Cobbs static void 304e1e1452dSArchie Cobbs ng_ether_attach(struct ifnet *ifp) 305e1e1452dSArchie Cobbs { 306499f60b1SAndriy Gapon char name[IFNAMSIZ]; 307e1e1452dSArchie Cobbs priv_p priv; 308e1e1452dSArchie Cobbs node_p node; 309e1e1452dSArchie Cobbs 310aef8f344SMarko Zec /* 311aef8f344SMarko Zec * Do not create / attach an ether node to this ifnet if 312aef8f344SMarko Zec * a netgraph node with the same name already exists. 313aef8f344SMarko Zec * This should prevent ether nodes to become attached to 314aef8f344SMarko Zec * eiface nodes, which may be problematic due to naming 315aef8f344SMarko Zec * clashes. 316aef8f344SMarko Zec */ 317aef8f344SMarko Zec if ((node = ng_name2noderef(NULL, ifp->if_xname)) != NULL) { 318aef8f344SMarko Zec NG_NODE_UNREF(node); 319aef8f344SMarko Zec return; 320aef8f344SMarko Zec } 321aef8f344SMarko Zec 322e1e1452dSArchie Cobbs /* Create node */ 3236e551fb6SDavid E. O'Brien KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__)); 324e1e1452dSArchie Cobbs if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 325e1e1452dSArchie Cobbs log(LOG_ERR, "%s: can't %s for %s\n", 3269bf40edeSBrooks Davis __func__, "create node", ifp->if_xname); 327e1e1452dSArchie Cobbs return; 328e1e1452dSArchie Cobbs } 329e1e1452dSArchie Cobbs 330e1e1452dSArchie Cobbs /* Allocate private data */ 3311ede983cSDag-Erling Smørgrav priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 332e1e1452dSArchie Cobbs if (priv == NULL) { 333e1e1452dSArchie Cobbs log(LOG_ERR, "%s: can't %s for %s\n", 3349bf40edeSBrooks Davis __func__, "allocate memory", ifp->if_xname); 33530400f03SJulian Elischer NG_NODE_UNREF(node); 336e1e1452dSArchie Cobbs return; 337e1e1452dSArchie Cobbs } 33830400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, priv); 339e1e1452dSArchie Cobbs priv->ifp = ifp; 3405240dcdbSRuslan Ermilov IFP2NG(ifp) = node; 341a3e232d6SArchie Cobbs priv->hwassist = ifp->if_hwassist; 342e1e1452dSArchie Cobbs 343e1e1452dSArchie Cobbs /* Try to give the node the same name as the interface */ 344499f60b1SAndriy Gapon ng_ether_sanitize_ifname(ifp->if_xname, name); 345499f60b1SAndriy Gapon if (ng_name_node(node, name) != 0) 346499f60b1SAndriy Gapon log(LOG_WARNING, "%s: can't name node %s\n", __func__, name); 347e1e1452dSArchie Cobbs } 348e1e1452dSArchie Cobbs 349e1e1452dSArchie Cobbs /* 350e1e1452dSArchie Cobbs * An Ethernet interface is being detached. 3511acb27c6SJulian Elischer * REALLY Destroy its node. 352e1e1452dSArchie Cobbs */ 353e1e1452dSArchie Cobbs static void 354e1e1452dSArchie Cobbs ng_ether_detach(struct ifnet *ifp) 355e1e1452dSArchie Cobbs { 356e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp); 3571acb27c6SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 358e1e1452dSArchie Cobbs 3592cdf8c49SMarko Zec taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 3601acb27c6SJulian Elischer NG_NODE_REALLY_DIE(node); /* Force real removal of node */ 3611acb27c6SJulian Elischer /* 3621acb27c6SJulian Elischer * We can't assume the ifnet is still around when we run shutdown 3631acb27c6SJulian Elischer * So zap it now. XXX We HOPE that anything running at this time 3641acb27c6SJulian Elischer * handles it (as it should in the non netgraph case). 3651acb27c6SJulian Elischer */ 3665240dcdbSRuslan Ermilov IFP2NG(ifp) = NULL; 3671acb27c6SJulian Elischer priv->ifp = NULL; /* XXX race if interrupted an output packet */ 3681acb27c6SJulian Elischer ng_rmnode_self(node); /* remove all netgraph parts */ 369e1e1452dSArchie Cobbs } 370e1e1452dSArchie Cobbs 3711c7899c7SGleb Smirnoff /* 3721c7899c7SGleb Smirnoff * Notify graph about link event. 3731c7899c7SGleb Smirnoff * if_link_state_change() has already checked that the state has changed. 3741c7899c7SGleb Smirnoff */ 3751c7899c7SGleb Smirnoff static void 3761c7899c7SGleb Smirnoff ng_ether_link_state(struct ifnet *ifp, int state) 3771c7899c7SGleb Smirnoff { 3781c7899c7SGleb Smirnoff const node_p node = IFP2NG(ifp); 3791c7899c7SGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 3801c7899c7SGleb Smirnoff struct ng_mesg *msg; 3811c7899c7SGleb Smirnoff int cmd, dummy_error = 0; 3821c7899c7SGleb Smirnoff 3831c7899c7SGleb Smirnoff if (state == LINK_STATE_UP) 3841c7899c7SGleb Smirnoff cmd = NGM_LINK_IS_UP; 3851c7899c7SGleb Smirnoff else if (state == LINK_STATE_DOWN) 3861c7899c7SGleb Smirnoff cmd = NGM_LINK_IS_DOWN; 3871c7899c7SGleb Smirnoff else 3881c7899c7SGleb Smirnoff return; 3891c7899c7SGleb Smirnoff 390bb027e06SMax Khon if (priv->lower != NULL) { 3911c7899c7SGleb Smirnoff NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT); 3921c7899c7SGleb Smirnoff if (msg != NULL) 3931c7899c7SGleb Smirnoff NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0); 3941c7899c7SGleb Smirnoff } 395bb027e06SMax Khon if (priv->orphan != NULL) { 396bb027e06SMax Khon NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT); 397bb027e06SMax Khon if (msg != NULL) 398bb027e06SMax Khon NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->orphan, 0); 399bb027e06SMax Khon } 400bb027e06SMax Khon } 4011c7899c7SGleb Smirnoff 402499f60b1SAndriy Gapon /* 403499f60b1SAndriy Gapon * Interface arrival notification handler. 404499f60b1SAndriy Gapon * The notification is produced in two cases: 405499f60b1SAndriy Gapon * o a new interface arrives 406499f60b1SAndriy Gapon * o an existing interface got renamed 407499f60b1SAndriy Gapon * Currently the first case is handled by ng_ether_attach via special 408499f60b1SAndriy Gapon * hook ng_ether_attach_p. 409499f60b1SAndriy Gapon */ 410499f60b1SAndriy Gapon static void 411499f60b1SAndriy Gapon ng_ether_ifnet_arrival_event(void *arg __unused, struct ifnet *ifp) 412499f60b1SAndriy Gapon { 413499f60b1SAndriy Gapon char name[IFNAMSIZ]; 41430bc1032SAndriy Gapon node_p node; 41530bc1032SAndriy Gapon 41630bc1032SAndriy Gapon /* Only ethernet interfaces are of interest. */ 41730bc1032SAndriy Gapon if (ifp->if_type != IFT_ETHER 41830bc1032SAndriy Gapon && ifp->if_type != IFT_L2VLAN) 41930bc1032SAndriy Gapon return; 420499f60b1SAndriy Gapon 421499f60b1SAndriy Gapon /* 422499f60b1SAndriy Gapon * Just return if it's a new interface without an ng_ether companion. 423499f60b1SAndriy Gapon */ 42430bc1032SAndriy Gapon node = IFP2NG(ifp); 425499f60b1SAndriy Gapon if (node == NULL) 426499f60b1SAndriy Gapon return; 427499f60b1SAndriy Gapon 428499f60b1SAndriy Gapon /* Try to give the node the same name as the new interface name */ 429499f60b1SAndriy Gapon ng_ether_sanitize_ifname(ifp->if_xname, name); 430499f60b1SAndriy Gapon if (ng_name_node(node, name) != 0) 431499f60b1SAndriy Gapon log(LOG_WARNING, "%s: can't re-name node %s\n", __func__, name); 432499f60b1SAndriy Gapon } 433499f60b1SAndriy Gapon 434e1e1452dSArchie Cobbs /****************************************************************** 435e1e1452dSArchie Cobbs NETGRAPH NODE METHODS 436e1e1452dSArchie Cobbs ******************************************************************/ 437e1e1452dSArchie Cobbs 438e1e1452dSArchie Cobbs /* 439e1e1452dSArchie Cobbs * It is not possible or allowable to create a node of this type. 440e1e1452dSArchie Cobbs * Nodes get created when the interface is attached (or, when 441e1e1452dSArchie Cobbs * this node type's KLD is loaded). 442e1e1452dSArchie Cobbs */ 443e1e1452dSArchie Cobbs static int 444069154d5SJulian Elischer ng_ether_constructor(node_p node) 445e1e1452dSArchie Cobbs { 446e1e1452dSArchie Cobbs return (EINVAL); 447e1e1452dSArchie Cobbs } 448e1e1452dSArchie Cobbs 449e1e1452dSArchie Cobbs /* 450e1e1452dSArchie Cobbs * Check for attaching a new hook. 451e1e1452dSArchie Cobbs */ 452e1e1452dSArchie Cobbs static int 453e1e1452dSArchie Cobbs ng_ether_newhook(node_p node, hook_p hook, const char *name) 454e1e1452dSArchie Cobbs { 45530400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 456e1e1452dSArchie Cobbs hook_p *hookptr; 457e1e1452dSArchie Cobbs 458e1e1452dSArchie Cobbs /* Divert hook is an alias for lower */ 459e1e1452dSArchie Cobbs if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0) 460e1e1452dSArchie Cobbs name = NG_ETHER_HOOK_LOWER; 461e1e1452dSArchie Cobbs 462e1e1452dSArchie Cobbs /* Which hook? */ 463f664dcdeSJulian Elischer if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) { 464e1e1452dSArchie Cobbs hookptr = &priv->upper; 465f664dcdeSJulian Elischer NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_upper); 466f089869fSMarko Zec NG_HOOK_SET_TO_INBOUND(hook); 467f664dcdeSJulian Elischer } else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) { 468e1e1452dSArchie Cobbs hookptr = &priv->lower; 469f664dcdeSJulian Elischer NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower); 470f664dcdeSJulian Elischer } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) { 4711a292b80SArchie Cobbs hookptr = &priv->orphan; 472f664dcdeSJulian Elischer NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower); 473f664dcdeSJulian Elischer } else 474e1e1452dSArchie Cobbs return (EINVAL); 475e1e1452dSArchie Cobbs 476e1e1452dSArchie Cobbs /* Check if already connected (shouldn't be, but doesn't hurt) */ 477e1e1452dSArchie Cobbs if (*hookptr != NULL) 478e1e1452dSArchie Cobbs return (EISCONN); 479e1e1452dSArchie Cobbs 480a3e232d6SArchie Cobbs /* Disable hardware checksums while 'upper' hook is connected */ 481a3e232d6SArchie Cobbs if (hookptr == &priv->upper) 482a3e232d6SArchie Cobbs priv->ifp->if_hwassist = 0; 483c1b8a9edSAlexander Motin NG_HOOK_HI_STACK(hook); 484e1e1452dSArchie Cobbs /* OK */ 485e1e1452dSArchie Cobbs *hookptr = hook; 486e1e1452dSArchie Cobbs return (0); 487e1e1452dSArchie Cobbs } 488e1e1452dSArchie Cobbs 489e1e1452dSArchie Cobbs /* 490e1e1452dSArchie Cobbs * Receive an incoming control message. 491e1e1452dSArchie Cobbs */ 492e1e1452dSArchie Cobbs static int 493069154d5SJulian Elischer ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook) 494e1e1452dSArchie Cobbs { 49530400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 496e1e1452dSArchie Cobbs struct ng_mesg *resp = NULL; 497e1e1452dSArchie Cobbs int error = 0; 498069154d5SJulian Elischer struct ng_mesg *msg; 499e1e1452dSArchie Cobbs 500069154d5SJulian Elischer NGI_GET_MSG(item, msg); 501e1e1452dSArchie Cobbs switch (msg->header.typecookie) { 502e1e1452dSArchie Cobbs case NGM_ETHER_COOKIE: 503e1e1452dSArchie Cobbs switch (msg->header.cmd) { 504e1e1452dSArchie Cobbs case NGM_ETHER_GET_IFNAME: 505bbb75d78SRuslan Ermilov NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 506e1e1452dSArchie Cobbs if (resp == NULL) { 507e1e1452dSArchie Cobbs error = ENOMEM; 508e1e1452dSArchie Cobbs break; 509e1e1452dSArchie Cobbs } 510bbb75d78SRuslan Ermilov strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ); 511e1e1452dSArchie Cobbs break; 512e1e1452dSArchie Cobbs case NGM_ETHER_GET_IFINDEX: 513e1e1452dSArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 514e1e1452dSArchie Cobbs if (resp == NULL) { 515e1e1452dSArchie Cobbs error = ENOMEM; 516e1e1452dSArchie Cobbs break; 517e1e1452dSArchie Cobbs } 518e1e1452dSArchie Cobbs *((u_int32_t *)resp->data) = priv->ifp->if_index; 519e1e1452dSArchie Cobbs break; 5204b39c3c7SArchie Cobbs case NGM_ETHER_GET_ENADDR: 5214b39c3c7SArchie Cobbs NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT); 5224b39c3c7SArchie Cobbs if (resp == NULL) { 5234b39c3c7SArchie Cobbs error = ENOMEM; 5244b39c3c7SArchie Cobbs break; 5254b39c3c7SArchie Cobbs } 5264a0d6638SRuslan Ermilov bcopy(IF_LLADDR(priv->ifp), 5274b39c3c7SArchie Cobbs resp->data, ETHER_ADDR_LEN); 5284b39c3c7SArchie Cobbs break; 52956045c66SArchie Cobbs case NGM_ETHER_SET_ENADDR: 53056045c66SArchie Cobbs { 53156045c66SArchie Cobbs if (msg->header.arglen != ETHER_ADDR_LEN) { 53256045c66SArchie Cobbs error = EINVAL; 53356045c66SArchie Cobbs break; 53456045c66SArchie Cobbs } 53556045c66SArchie Cobbs error = if_setlladdr(priv->ifp, 53656045c66SArchie Cobbs (u_char *)msg->data, ETHER_ADDR_LEN); 537ea4ca115SAndrew Thompson EVENTHANDLER_INVOKE(iflladdr_event, priv->ifp); 53856045c66SArchie Cobbs break; 53956045c66SArchie Cobbs } 54056045c66SArchie Cobbs case NGM_ETHER_GET_PROMISC: 54156045c66SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 54256045c66SArchie Cobbs if (resp == NULL) { 54356045c66SArchie Cobbs error = ENOMEM; 54456045c66SArchie Cobbs break; 54556045c66SArchie Cobbs } 54656045c66SArchie Cobbs *((u_int32_t *)resp->data) = priv->promisc; 54756045c66SArchie Cobbs break; 5484b39c3c7SArchie Cobbs case NGM_ETHER_SET_PROMISC: 5494b39c3c7SArchie Cobbs { 5504b39c3c7SArchie Cobbs u_char want; 5514b39c3c7SArchie Cobbs 5524b39c3c7SArchie Cobbs if (msg->header.arglen != sizeof(u_int32_t)) { 5534b39c3c7SArchie Cobbs error = EINVAL; 5544b39c3c7SArchie Cobbs break; 5554b39c3c7SArchie Cobbs } 5564b39c3c7SArchie Cobbs want = !!*((u_int32_t *)msg->data); 5574b39c3c7SArchie Cobbs if (want ^ priv->promisc) { 5584b39c3c7SArchie Cobbs if ((error = ifpromisc(priv->ifp, want)) != 0) 5594b39c3c7SArchie Cobbs break; 5604b39c3c7SArchie Cobbs priv->promisc = want; 5614b39c3c7SArchie Cobbs } 5624b39c3c7SArchie Cobbs break; 5634b39c3c7SArchie Cobbs } 56456045c66SArchie Cobbs case NGM_ETHER_GET_AUTOSRC: 56556045c66SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 56656045c66SArchie Cobbs if (resp == NULL) { 56756045c66SArchie Cobbs error = ENOMEM; 56856045c66SArchie Cobbs break; 56956045c66SArchie Cobbs } 57056045c66SArchie Cobbs *((u_int32_t *)resp->data) = priv->autoSrcAddr; 57156045c66SArchie Cobbs break; 5724b39c3c7SArchie Cobbs case NGM_ETHER_SET_AUTOSRC: 5734b39c3c7SArchie Cobbs if (msg->header.arglen != sizeof(u_int32_t)) { 5744b39c3c7SArchie Cobbs error = EINVAL; 5754b39c3c7SArchie Cobbs break; 5764b39c3c7SArchie Cobbs } 5774b39c3c7SArchie Cobbs priv->autoSrcAddr = !!*((u_int32_t *)msg->data); 5784b39c3c7SArchie Cobbs break; 579810d5e89SGleb Smirnoff case NGM_ETHER_ADD_MULTI: 580810d5e89SGleb Smirnoff { 581810d5e89SGleb Smirnoff struct sockaddr_dl sa_dl; 582ec002feeSBruce M Simpson struct ifmultiaddr *ifma; 583810d5e89SGleb Smirnoff 584810d5e89SGleb Smirnoff if (msg->header.arglen != ETHER_ADDR_LEN) { 585810d5e89SGleb Smirnoff error = EINVAL; 586810d5e89SGleb Smirnoff break; 587810d5e89SGleb Smirnoff } 588ba20540eSGleb Smirnoff bzero(&sa_dl, sizeof(struct sockaddr_dl)); 589810d5e89SGleb Smirnoff sa_dl.sdl_len = sizeof(struct sockaddr_dl); 590810d5e89SGleb Smirnoff sa_dl.sdl_family = AF_LINK; 591ba20540eSGleb Smirnoff sa_dl.sdl_alen = ETHER_ADDR_LEN; 592810d5e89SGleb Smirnoff bcopy((void *)msg->data, LLADDR(&sa_dl), 593810d5e89SGleb Smirnoff ETHER_ADDR_LEN); 594ec002feeSBruce M Simpson /* 595ec002feeSBruce M Simpson * Netgraph is only permitted to join groups once 596ec002feeSBruce M Simpson * via the if_addmulti() KPI, because it cannot hold 597ec002feeSBruce M Simpson * struct ifmultiaddr * between calls. It may also 598ec002feeSBruce M Simpson * lose a race while we check if the membership 599ec002feeSBruce M Simpson * already exists. 600ec002feeSBruce M Simpson */ 601eb956cd0SRobert Watson if_maddr_rlock(priv->ifp); 602ec002feeSBruce M Simpson ifma = if_findmulti(priv->ifp, 603ec002feeSBruce M Simpson (struct sockaddr *)&sa_dl); 604eb956cd0SRobert Watson if_maddr_runlock(priv->ifp); 605ec002feeSBruce M Simpson if (ifma != NULL) { 606ec002feeSBruce M Simpson error = EADDRINUSE; 607ec002feeSBruce M Simpson } else { 608810d5e89SGleb Smirnoff error = if_addmulti(priv->ifp, 609ec002feeSBruce M Simpson (struct sockaddr *)&sa_dl, &ifma); 610ec002feeSBruce M Simpson } 611810d5e89SGleb Smirnoff break; 612810d5e89SGleb Smirnoff } 613810d5e89SGleb Smirnoff case NGM_ETHER_DEL_MULTI: 614810d5e89SGleb Smirnoff { 615810d5e89SGleb Smirnoff struct sockaddr_dl sa_dl; 616810d5e89SGleb Smirnoff 617810d5e89SGleb Smirnoff if (msg->header.arglen != ETHER_ADDR_LEN) { 618810d5e89SGleb Smirnoff error = EINVAL; 619810d5e89SGleb Smirnoff break; 620810d5e89SGleb Smirnoff } 621ba20540eSGleb Smirnoff bzero(&sa_dl, sizeof(struct sockaddr_dl)); 622810d5e89SGleb Smirnoff sa_dl.sdl_len = sizeof(struct sockaddr_dl); 623810d5e89SGleb Smirnoff sa_dl.sdl_family = AF_LINK; 624ba20540eSGleb Smirnoff sa_dl.sdl_alen = ETHER_ADDR_LEN; 625810d5e89SGleb Smirnoff bcopy((void *)msg->data, LLADDR(&sa_dl), 626810d5e89SGleb Smirnoff ETHER_ADDR_LEN); 627810d5e89SGleb Smirnoff error = if_delmulti(priv->ifp, 628810d5e89SGleb Smirnoff (struct sockaddr *)&sa_dl); 629810d5e89SGleb Smirnoff break; 630810d5e89SGleb Smirnoff } 631cefddd66SGleb Smirnoff case NGM_ETHER_DETACH: 632cefddd66SGleb Smirnoff ng_ether_detach(priv->ifp); 633cefddd66SGleb Smirnoff break; 634e1e1452dSArchie Cobbs default: 635e1e1452dSArchie Cobbs error = EINVAL; 636e1e1452dSArchie Cobbs break; 637e1e1452dSArchie Cobbs } 638e1e1452dSArchie Cobbs break; 639e1e1452dSArchie Cobbs default: 640e1e1452dSArchie Cobbs error = EINVAL; 641e1e1452dSArchie Cobbs break; 642e1e1452dSArchie Cobbs } 643069154d5SJulian Elischer NG_RESPOND_MSG(error, node, item, resp); 644069154d5SJulian Elischer NG_FREE_MSG(msg); 645e1e1452dSArchie Cobbs return (error); 646e1e1452dSArchie Cobbs } 647e1e1452dSArchie Cobbs 648e1e1452dSArchie Cobbs /* 649e1e1452dSArchie Cobbs * Receive data on a hook. 650f664dcdeSJulian Elischer * Since we use per-hook recveive methods this should never be called. 651e1e1452dSArchie Cobbs */ 652e1e1452dSArchie Cobbs static int 653069154d5SJulian Elischer ng_ether_rcvdata(hook_p hook, item_p item) 654e1e1452dSArchie Cobbs { 655069154d5SJulian Elischer NG_FREE_ITEM(item); 6563ca24c28SJulian Elischer 6576e551fb6SDavid E. O'Brien panic("%s: weird hook", __func__); 658e1e1452dSArchie Cobbs } 659e1e1452dSArchie Cobbs 660e1e1452dSArchie Cobbs /* 6611a292b80SArchie Cobbs * Handle an mbuf received on the "lower" or "orphan" hook. 662e1e1452dSArchie Cobbs */ 663e1e1452dSArchie Cobbs static int 664f664dcdeSJulian Elischer ng_ether_rcv_lower(hook_p hook, item_p item) 665e1e1452dSArchie Cobbs { 666f664dcdeSJulian Elischer struct mbuf *m; 667f664dcdeSJulian Elischer const node_p node = NG_HOOK_NODE(hook); 66830400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 669a1479aa2SArchie Cobbs struct ifnet *const ifp = priv->ifp; 670a1479aa2SArchie Cobbs 671f664dcdeSJulian Elischer NGI_GET_M(item, m); 672f664dcdeSJulian Elischer NG_FREE_ITEM(item); 673f664dcdeSJulian Elischer 674a1479aa2SArchie Cobbs /* Check whether interface is ready for packets */ 675f664dcdeSJulian Elischer 67613f4c340SRobert Watson if (!((ifp->if_flags & IFF_UP) && 67713f4c340SRobert Watson (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 678a1479aa2SArchie Cobbs NG_FREE_M(m); 679a1479aa2SArchie Cobbs return (ENETDOWN); 680a1479aa2SArchie Cobbs } 681e1e1452dSArchie Cobbs 682e1e1452dSArchie Cobbs /* Make sure header is fully pulled up */ 683e1e1452dSArchie Cobbs if (m->m_pkthdr.len < sizeof(struct ether_header)) { 684069154d5SJulian Elischer NG_FREE_M(m); 685e1e1452dSArchie Cobbs return (EINVAL); 686e1e1452dSArchie Cobbs } 687e1e1452dSArchie Cobbs if (m->m_len < sizeof(struct ether_header) 6887b9f235fSArchie Cobbs && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 689e1e1452dSArchie Cobbs return (ENOBUFS); 690e1e1452dSArchie Cobbs 6914b39c3c7SArchie Cobbs /* Drop in the MAC address if desired */ 6924b39c3c7SArchie Cobbs if (priv->autoSrcAddr) { 6937b9f235fSArchie Cobbs 6947b9f235fSArchie Cobbs /* Make the mbuf writable if it's not already */ 6957b9f235fSArchie Cobbs if (!M_WRITABLE(m) 6967b9f235fSArchie Cobbs && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 6977b9f235fSArchie Cobbs return (ENOBUFS); 6987b9f235fSArchie Cobbs 6997b9f235fSArchie Cobbs /* Overwrite source MAC address */ 7004a0d6638SRuslan Ermilov bcopy(IF_LLADDR(ifp), 7014b39c3c7SArchie Cobbs mtod(m, struct ether_header *)->ether_shost, 7024b39c3c7SArchie Cobbs ETHER_ADDR_LEN); 7034b39c3c7SArchie Cobbs } 704561e4fb9SJulian Elischer 705e1e1452dSArchie Cobbs /* Send it on its way */ 706a1479aa2SArchie Cobbs return ether_output_frame(ifp, m); 707e1e1452dSArchie Cobbs } 708e1e1452dSArchie Cobbs 709e1e1452dSArchie Cobbs /* 710e1e1452dSArchie Cobbs * Handle an mbuf received on the "upper" hook. 711e1e1452dSArchie Cobbs */ 712e1e1452dSArchie Cobbs static int 713f664dcdeSJulian Elischer ng_ether_rcv_upper(hook_p hook, item_p item) 714e1e1452dSArchie Cobbs { 715f664dcdeSJulian Elischer struct mbuf *m; 716f664dcdeSJulian Elischer const node_p node = NG_HOOK_NODE(hook); 71730400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 7186512768bSGleb Smirnoff struct ifnet *ifp = priv->ifp; 719e1e1452dSArchie Cobbs 720f664dcdeSJulian Elischer NGI_GET_M(item, m); 721f664dcdeSJulian Elischer NG_FREE_ITEM(item); 722f664dcdeSJulian Elischer 723c60c00bcSRuslan Ermilov /* Check length and pull off header */ 724c60c00bcSRuslan Ermilov if (m->m_pkthdr.len < sizeof(struct ether_header)) { 725c60c00bcSRuslan Ermilov NG_FREE_M(m); 726c60c00bcSRuslan Ermilov return (EINVAL); 727c60c00bcSRuslan Ermilov } 728c60c00bcSRuslan Ermilov if (m->m_len < sizeof(struct ether_header) && 729c60c00bcSRuslan Ermilov (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 730c60c00bcSRuslan Ermilov return (ENOBUFS); 731c60c00bcSRuslan Ermilov 7326512768bSGleb Smirnoff m->m_pkthdr.rcvif = ifp; 733e1e1452dSArchie Cobbs 734fd6238a6SAndrew Thompson /* Pass the packet to the bridge, it may come back to us */ 7356512768bSGleb Smirnoff if (ifp->if_bridge) { 736fd6238a6SAndrew Thompson BRIDGE_INPUT(ifp, m); 7376512768bSGleb Smirnoff if (m == NULL) 7386512768bSGleb Smirnoff return (0); 7396512768bSGleb Smirnoff } 740a176c2aeSGleb Smirnoff 741e1e1452dSArchie Cobbs /* Route packet back in */ 742fd6238a6SAndrew Thompson ether_demux(ifp, m); 743e1e1452dSArchie Cobbs return (0); 744e1e1452dSArchie Cobbs } 745e1e1452dSArchie Cobbs 746e1e1452dSArchie Cobbs /* 7471acb27c6SJulian Elischer * Shutdown node. This resets the node but does not remove it 7481acb27c6SJulian Elischer * unless the REALLY_DIE flag is set. 749e1e1452dSArchie Cobbs */ 750e1e1452dSArchie Cobbs static int 751069154d5SJulian Elischer ng_ether_shutdown(node_p node) 752e1e1452dSArchie Cobbs { 75330400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 7544b39c3c7SArchie Cobbs 755be4252b3SJulian Elischer if (node->nd_flags & NGF_REALLY_DIE) { 7561acb27c6SJulian Elischer /* 7571acb27c6SJulian Elischer * WE came here because the ethernet card is being unloaded, 7581acb27c6SJulian Elischer * so stop being persistant. 7591acb27c6SJulian Elischer * Actually undo all the things we did on creation. 7601acb27c6SJulian Elischer * Assume the ifp has already been freed. 7611acb27c6SJulian Elischer */ 7621acb27c6SJulian Elischer NG_NODE_SET_PRIVATE(node, NULL); 7631ede983cSDag-Erling Smørgrav free(priv, M_NETGRAPH); 7641acb27c6SJulian Elischer NG_NODE_UNREF(node); /* free node itself */ 7651acb27c6SJulian Elischer return (0); 766069154d5SJulian Elischer } 767018df1c3SBrian Feldman if (priv->promisc) { /* disable promiscuous mode */ 768018df1c3SBrian Feldman (void)ifpromisc(priv->ifp, 0); 769018df1c3SBrian Feldman priv->promisc = 0; 770018df1c3SBrian Feldman } 771be4252b3SJulian Elischer NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */ 772be4252b3SJulian Elischer 773e1e1452dSArchie Cobbs return (0); 774e1e1452dSArchie Cobbs } 775e1e1452dSArchie Cobbs 776e1e1452dSArchie Cobbs /* 777e1e1452dSArchie Cobbs * Hook disconnection. 778e1e1452dSArchie Cobbs */ 779e1e1452dSArchie Cobbs static int 780e1e1452dSArchie Cobbs ng_ether_disconnect(hook_p hook) 781e1e1452dSArchie Cobbs { 78230400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 783e1e1452dSArchie Cobbs 784a3e232d6SArchie Cobbs if (hook == priv->upper) { 785e1e1452dSArchie Cobbs priv->upper = NULL; 786b712e9ecSBrian Feldman if (priv->ifp != NULL) /* restore h/w csum */ 787b712e9ecSBrian Feldman priv->ifp->if_hwassist = priv->hwassist; 7881a292b80SArchie Cobbs } else if (hook == priv->lower) 789e1e1452dSArchie Cobbs priv->lower = NULL; 7901a292b80SArchie Cobbs else if (hook == priv->orphan) 7911a292b80SArchie Cobbs priv->orphan = NULL; 7921a292b80SArchie Cobbs else 7936e551fb6SDavid E. O'Brien panic("%s: weird hook", __func__); 79430400f03SJulian Elischer if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 79530400f03SJulian Elischer && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 79630400f03SJulian Elischer ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */ 797e1e1452dSArchie Cobbs return (0); 798e1e1452dSArchie Cobbs } 799e1e1452dSArchie Cobbs 800e1e1452dSArchie Cobbs /****************************************************************** 801e1e1452dSArchie Cobbs INITIALIZATION 802e1e1452dSArchie Cobbs ******************************************************************/ 803e1e1452dSArchie Cobbs 804e1e1452dSArchie Cobbs /* 805e1e1452dSArchie Cobbs * Handle loading and unloading for this node type. 806e1e1452dSArchie Cobbs */ 807e1e1452dSArchie Cobbs static int 808e1e1452dSArchie Cobbs ng_ether_mod_event(module_t mod, int event, void *data) 809e1e1452dSArchie Cobbs { 810e1e1452dSArchie Cobbs int error = 0; 811e1e1452dSArchie Cobbs 812e1e1452dSArchie Cobbs switch (event) { 813e1e1452dSArchie Cobbs case MOD_LOAD: 814e1e1452dSArchie Cobbs 815e1e1452dSArchie Cobbs /* Register function hooks */ 816e1e1452dSArchie Cobbs if (ng_ether_attach_p != NULL) { 817e1e1452dSArchie Cobbs error = EEXIST; 818e1e1452dSArchie Cobbs break; 819e1e1452dSArchie Cobbs } 820e1e1452dSArchie Cobbs ng_ether_attach_p = ng_ether_attach; 821e1e1452dSArchie Cobbs ng_ether_detach_p = ng_ether_detach; 822e1e1452dSArchie Cobbs ng_ether_output_p = ng_ether_output; 823e1e1452dSArchie Cobbs ng_ether_input_p = ng_ether_input; 824e1e1452dSArchie Cobbs ng_ether_input_orphan_p = ng_ether_input_orphan; 8251c7899c7SGleb Smirnoff ng_ether_link_state_p = ng_ether_link_state; 826e1e1452dSArchie Cobbs 827499f60b1SAndriy Gapon ng_ether_ifnet_arrival_cookie = 828499f60b1SAndriy Gapon EVENTHANDLER_REGISTER(ifnet_arrival_event, 829499f60b1SAndriy Gapon ng_ether_ifnet_arrival_event, NULL, EVENTHANDLER_PRI_ANY); 830e1e1452dSArchie Cobbs break; 831e1e1452dSArchie Cobbs 832e1e1452dSArchie Cobbs case MOD_UNLOAD: 833e1e1452dSArchie Cobbs 834e1e1452dSArchie Cobbs /* 835e1e1452dSArchie Cobbs * Note that the base code won't try to unload us until 836e1e1452dSArchie Cobbs * all nodes have been removed, and that can't happen 837e1e1452dSArchie Cobbs * until all Ethernet interfaces are removed. In any 838e1e1452dSArchie Cobbs * case, we know there are no nodes left if the action 839e1e1452dSArchie Cobbs * is MOD_UNLOAD, so there's no need to detach any nodes. 840e1e1452dSArchie Cobbs */ 841e1e1452dSArchie Cobbs 842499f60b1SAndriy Gapon EVENTHANDLER_DEREGISTER(ifnet_arrival_event, 843499f60b1SAndriy Gapon ng_ether_ifnet_arrival_cookie); 844499f60b1SAndriy Gapon 845e1e1452dSArchie Cobbs /* Unregister function hooks */ 846e1e1452dSArchie Cobbs ng_ether_attach_p = NULL; 847e1e1452dSArchie Cobbs ng_ether_detach_p = NULL; 848e1e1452dSArchie Cobbs ng_ether_output_p = NULL; 849e1e1452dSArchie Cobbs ng_ether_input_p = NULL; 850e1e1452dSArchie Cobbs ng_ether_input_orphan_p = NULL; 8511c7899c7SGleb Smirnoff ng_ether_link_state_p = NULL; 852e1e1452dSArchie Cobbs break; 853e1e1452dSArchie Cobbs 854e1e1452dSArchie Cobbs default: 855e1e1452dSArchie Cobbs error = EOPNOTSUPP; 856e1e1452dSArchie Cobbs break; 857e1e1452dSArchie Cobbs } 858e1e1452dSArchie Cobbs return (error); 859e1e1452dSArchie Cobbs } 860e1e1452dSArchie Cobbs 861d0728d71SRobert Watson static void 862d0728d71SRobert Watson vnet_ng_ether_init(const void *unused) 863aef8f344SMarko Zec { 864aef8f344SMarko Zec struct ifnet *ifp; 865aef8f344SMarko Zec 866d0728d71SRobert Watson /* If module load was rejected, don't attach to vnets. */ 867d0728d71SRobert Watson if (ng_ether_attach_p != ng_ether_attach) 868d0728d71SRobert Watson return; 869d0728d71SRobert Watson 870aef8f344SMarko Zec /* Create nodes for any already-existing Ethernet interfaces. */ 871aef8f344SMarko Zec IFNET_RLOCK(); 872aef8f344SMarko Zec TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 873aef8f344SMarko Zec if (ifp->if_type == IFT_ETHER 874aef8f344SMarko Zec || ifp->if_type == IFT_L2VLAN) 875aef8f344SMarko Zec ng_ether_attach(ifp); 876aef8f344SMarko Zec } 877aef8f344SMarko Zec IFNET_RUNLOCK(); 878aef8f344SMarko Zec } 879d0728d71SRobert Watson VNET_SYSINIT(vnet_ng_ether_init, SI_SUB_PSEUDO, SI_ORDER_ANY, 880d0728d71SRobert Watson vnet_ng_ether_init, NULL); 881