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> 50e1e1452dSArchie Cobbs #include <sys/systm.h> 51e1e1452dSArchie Cobbs #include <sys/kernel.h> 52e1e1452dSArchie Cobbs #include <sys/malloc.h> 53e1e1452dSArchie Cobbs #include <sys/mbuf.h> 54e1e1452dSArchie Cobbs #include <sys/errno.h> 55530c0060SRobert Watson #include <sys/proc.h> 56e1e1452dSArchie Cobbs #include <sys/syslog.h> 57e1e1452dSArchie Cobbs #include <sys/socket.h> 582cdf8c49SMarko Zec #include <sys/taskqueue.h> 59e1e1452dSArchie Cobbs 60e1e1452dSArchie Cobbs #include <net/if.h> 61810d5e89SGleb Smirnoff #include <net/if_dl.h> 62e1e1452dSArchie Cobbs #include <net/if_types.h> 63e1e1452dSArchie Cobbs #include <net/if_arp.h> 64e1e1452dSArchie Cobbs #include <net/if_var.h> 65e1e1452dSArchie Cobbs #include <net/ethernet.h> 66fd6238a6SAndrew Thompson #include <net/if_bridgevar.h> 674b79449eSBjoern A. Zeeb #include <net/vnet.h> 68e1e1452dSArchie Cobbs 69e1e1452dSArchie Cobbs #include <netgraph/ng_message.h> 70e1e1452dSArchie Cobbs #include <netgraph/netgraph.h> 71e1e1452dSArchie Cobbs #include <netgraph/ng_parse.h> 72e1e1452dSArchie Cobbs #include <netgraph/ng_ether.h> 73e1e1452dSArchie Cobbs 74*39b553ceSEd Maste MODULE_VERSION(ng_ether, 1); 75*39b553ceSEd Maste 765240dcdbSRuslan Ermilov #define IFP2NG(ifp) (IFP2AC((ifp))->ac_netgraph) 77e1e1452dSArchie Cobbs 783c976c3fSPawel Jakub Dawidek /* Per-node private data */ 793c976c3fSPawel Jakub Dawidek struct private { 803c976c3fSPawel Jakub Dawidek struct ifnet *ifp; /* associated interface */ 813c976c3fSPawel Jakub Dawidek hook_p upper; /* upper hook connection */ 821a292b80SArchie Cobbs hook_p lower; /* lower hook connection */ 831a292b80SArchie Cobbs hook_p orphan; /* orphan hook connection */ 843c976c3fSPawel Jakub Dawidek u_char autoSrcAddr; /* always overwrite source address */ 853c976c3fSPawel Jakub Dawidek u_char promisc; /* promiscuous mode enabled */ 863c976c3fSPawel Jakub Dawidek u_long hwassist; /* hardware checksum capabilities */ 873c976c3fSPawel Jakub Dawidek u_int flags; /* flags e.g. really die */ 883c976c3fSPawel Jakub Dawidek }; 893c976c3fSPawel Jakub Dawidek typedef struct private *priv_p; 90e1e1452dSArchie Cobbs 91edbb5246SSam Leffler /* Hook pointers used by if_ethersubr.c to callback to netgraph */ 92edbb5246SSam Leffler extern void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 93edbb5246SSam Leffler extern void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 94edbb5246SSam Leffler extern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 95edbb5246SSam Leffler extern void (*ng_ether_attach_p)(struct ifnet *ifp); 96edbb5246SSam Leffler extern void (*ng_ether_detach_p)(struct ifnet *ifp); 971c7899c7SGleb Smirnoff extern void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 98edbb5246SSam Leffler 99e1e1452dSArchie Cobbs /* Functional hooks called from if_ethersubr.c */ 100edbb5246SSam Leffler static void ng_ether_input(struct ifnet *ifp, struct mbuf **mp); 101edbb5246SSam Leffler static void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m); 102e1e1452dSArchie Cobbs static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp); 103e1e1452dSArchie Cobbs static void ng_ether_attach(struct ifnet *ifp); 104e1e1452dSArchie Cobbs static void ng_ether_detach(struct ifnet *ifp); 1051c7899c7SGleb Smirnoff static void ng_ether_link_state(struct ifnet *ifp, int state); 106e1e1452dSArchie Cobbs 107e1e1452dSArchie Cobbs /* Other functions */ 108f664dcdeSJulian Elischer static int ng_ether_rcv_lower(hook_p node, item_p item); 109f664dcdeSJulian Elischer static int ng_ether_rcv_upper(hook_p node, item_p item); 110e1e1452dSArchie Cobbs 111e1e1452dSArchie Cobbs /* Netgraph node methods */ 112e1e1452dSArchie Cobbs static ng_constructor_t ng_ether_constructor; 113e1e1452dSArchie Cobbs static ng_rcvmsg_t ng_ether_rcvmsg; 114069154d5SJulian Elischer static ng_shutdown_t ng_ether_shutdown; 115e1e1452dSArchie Cobbs static ng_newhook_t ng_ether_newhook; 116e1e1452dSArchie Cobbs static ng_rcvdata_t ng_ether_rcvdata; 117e1e1452dSArchie Cobbs static ng_disconnect_t ng_ether_disconnect; 118e1e1452dSArchie Cobbs static int ng_ether_mod_event(module_t mod, int event, void *data); 119e1e1452dSArchie Cobbs 120e1e1452dSArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */ 121e1e1452dSArchie Cobbs static const struct ng_cmdlist ng_ether_cmdlist[] = { 122e1e1452dSArchie Cobbs { 123e1e1452dSArchie Cobbs NGM_ETHER_COOKIE, 124e1e1452dSArchie Cobbs NGM_ETHER_GET_IFNAME, 125e1e1452dSArchie Cobbs "getifname", 126e1e1452dSArchie Cobbs NULL, 127e1e1452dSArchie Cobbs &ng_parse_string_type 128e1e1452dSArchie Cobbs }, 129e1e1452dSArchie Cobbs { 130e1e1452dSArchie Cobbs NGM_ETHER_COOKIE, 131e1e1452dSArchie Cobbs NGM_ETHER_GET_IFINDEX, 132e1e1452dSArchie Cobbs "getifindex", 133e1e1452dSArchie Cobbs NULL, 134e1e1452dSArchie Cobbs &ng_parse_int32_type 135e1e1452dSArchie Cobbs }, 1364b39c3c7SArchie Cobbs { 1374b39c3c7SArchie Cobbs NGM_ETHER_COOKIE, 1384b39c3c7SArchie Cobbs NGM_ETHER_GET_ENADDR, 1394b39c3c7SArchie Cobbs "getenaddr", 1404b39c3c7SArchie Cobbs NULL, 1418c7e4101SRuslan Ermilov &ng_parse_enaddr_type 1424b39c3c7SArchie Cobbs }, 1434b39c3c7SArchie Cobbs { 1444b39c3c7SArchie Cobbs NGM_ETHER_COOKIE, 14556045c66SArchie Cobbs NGM_ETHER_SET_ENADDR, 14656045c66SArchie Cobbs "setenaddr", 1478c7e4101SRuslan Ermilov &ng_parse_enaddr_type, 14856045c66SArchie Cobbs NULL 14956045c66SArchie Cobbs }, 15056045c66SArchie Cobbs { 15156045c66SArchie Cobbs NGM_ETHER_COOKIE, 15256045c66SArchie Cobbs NGM_ETHER_GET_PROMISC, 15356045c66SArchie Cobbs "getpromisc", 15456045c66SArchie Cobbs NULL, 15556045c66SArchie Cobbs &ng_parse_int32_type 15656045c66SArchie Cobbs }, 15756045c66SArchie Cobbs { 15856045c66SArchie Cobbs NGM_ETHER_COOKIE, 1594b39c3c7SArchie Cobbs NGM_ETHER_SET_PROMISC, 1604b39c3c7SArchie Cobbs "setpromisc", 1614b39c3c7SArchie Cobbs &ng_parse_int32_type, 1624b39c3c7SArchie Cobbs NULL 1634b39c3c7SArchie Cobbs }, 1644b39c3c7SArchie Cobbs { 1654b39c3c7SArchie Cobbs NGM_ETHER_COOKIE, 16656045c66SArchie Cobbs NGM_ETHER_GET_AUTOSRC, 16756045c66SArchie Cobbs "getautosrc", 16856045c66SArchie Cobbs NULL, 16956045c66SArchie Cobbs &ng_parse_int32_type 17056045c66SArchie Cobbs }, 17156045c66SArchie Cobbs { 17256045c66SArchie Cobbs NGM_ETHER_COOKIE, 1734b39c3c7SArchie Cobbs NGM_ETHER_SET_AUTOSRC, 1744b39c3c7SArchie Cobbs "setautosrc", 1754b39c3c7SArchie Cobbs &ng_parse_int32_type, 1764b39c3c7SArchie Cobbs NULL 1774b39c3c7SArchie Cobbs }, 178810d5e89SGleb Smirnoff { 179810d5e89SGleb Smirnoff NGM_ETHER_COOKIE, 180810d5e89SGleb Smirnoff NGM_ETHER_ADD_MULTI, 181810d5e89SGleb Smirnoff "addmulti", 182810d5e89SGleb Smirnoff &ng_parse_enaddr_type, 183810d5e89SGleb Smirnoff NULL 184810d5e89SGleb Smirnoff }, 185810d5e89SGleb Smirnoff { 186810d5e89SGleb Smirnoff NGM_ETHER_COOKIE, 187810d5e89SGleb Smirnoff NGM_ETHER_DEL_MULTI, 188810d5e89SGleb Smirnoff "delmulti", 189810d5e89SGleb Smirnoff &ng_parse_enaddr_type, 190810d5e89SGleb Smirnoff NULL 191810d5e89SGleb Smirnoff }, 192cefddd66SGleb Smirnoff { 193cefddd66SGleb Smirnoff NGM_ETHER_COOKIE, 194cefddd66SGleb Smirnoff NGM_ETHER_DETACH, 195cefddd66SGleb Smirnoff "detach", 196cefddd66SGleb Smirnoff NULL, 197cefddd66SGleb Smirnoff NULL 198cefddd66SGleb Smirnoff }, 199e1e1452dSArchie Cobbs { 0 } 200e1e1452dSArchie Cobbs }; 201e1e1452dSArchie Cobbs 202e1e1452dSArchie Cobbs static struct ng_type ng_ether_typestruct = { 203f8aae777SJulian Elischer .version = NG_ABI_VERSION, 204f8aae777SJulian Elischer .name = NG_ETHER_NODE_TYPE, 205f8aae777SJulian Elischer .mod_event = ng_ether_mod_event, 206f8aae777SJulian Elischer .constructor = ng_ether_constructor, 207f8aae777SJulian Elischer .rcvmsg = ng_ether_rcvmsg, 208f8aae777SJulian Elischer .shutdown = ng_ether_shutdown, 209f8aae777SJulian Elischer .newhook = ng_ether_newhook, 210f8aae777SJulian Elischer .rcvdata = ng_ether_rcvdata, 211f8aae777SJulian Elischer .disconnect = ng_ether_disconnect, 212f8aae777SJulian Elischer .cmdlist = ng_ether_cmdlist, 213e1e1452dSArchie Cobbs }; 214e1e1452dSArchie Cobbs NETGRAPH_INIT(ether, &ng_ether_typestruct); 215e1e1452dSArchie Cobbs 216e1e1452dSArchie Cobbs /****************************************************************** 217e1e1452dSArchie Cobbs ETHERNET FUNCTION HOOKS 218e1e1452dSArchie Cobbs ******************************************************************/ 219e1e1452dSArchie Cobbs 220e1e1452dSArchie Cobbs /* 221e1e1452dSArchie Cobbs * Handle a packet that has come in on an interface. We get to 222e1e1452dSArchie Cobbs * look at it here before any upper layer protocols do. 223e1e1452dSArchie Cobbs * 224e1e1452dSArchie Cobbs * NOTE: this function will get called at splimp() 225e1e1452dSArchie Cobbs */ 226e1e1452dSArchie Cobbs static void 227edbb5246SSam Leffler ng_ether_input(struct ifnet *ifp, struct mbuf **mp) 228e1e1452dSArchie Cobbs { 229e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp); 23030400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 2311a292b80SArchie Cobbs int error; 232e1e1452dSArchie Cobbs 233e1e1452dSArchie Cobbs /* If "lower" hook not connected, let packet continue */ 2341a292b80SArchie Cobbs if (priv->lower == NULL) 235e1e1452dSArchie Cobbs return; 2361a292b80SArchie Cobbs NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */ 237e1e1452dSArchie Cobbs } 238e1e1452dSArchie Cobbs 239e1e1452dSArchie Cobbs /* 240e1e1452dSArchie Cobbs * Handle a packet that has come in on an interface, and which 241e1e1452dSArchie Cobbs * does not match any of our known protocols (an ``orphan''). 242e1e1452dSArchie Cobbs * 243e1e1452dSArchie Cobbs * NOTE: this function will get called at splimp() 244e1e1452dSArchie Cobbs */ 245e1e1452dSArchie Cobbs static void 246edbb5246SSam Leffler ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m) 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 2521a292b80SArchie Cobbs /* If "orphan" hook not connected, discard packet */ 2531a292b80SArchie Cobbs if (priv->orphan == NULL) { 254e1e1452dSArchie Cobbs m_freem(m); 255e1e1452dSArchie Cobbs return; 256e1e1452dSArchie Cobbs } 2571a292b80SArchie Cobbs NG_SEND_DATA_ONLY(error, priv->orphan, m); 258e1e1452dSArchie Cobbs } 259e1e1452dSArchie Cobbs 260e1e1452dSArchie Cobbs /* 261e1e1452dSArchie Cobbs * Handle a packet that is going out on an interface. 262e1e1452dSArchie Cobbs * The Ethernet header is already attached to the mbuf. 263e1e1452dSArchie Cobbs */ 264e1e1452dSArchie Cobbs static int 265e1e1452dSArchie Cobbs ng_ether_output(struct ifnet *ifp, struct mbuf **mp) 266e1e1452dSArchie Cobbs { 267e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp); 26830400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 269e1e1452dSArchie Cobbs int error = 0; 270e1e1452dSArchie Cobbs 271e1e1452dSArchie Cobbs /* If "upper" hook not connected, let packet continue */ 272e1e1452dSArchie Cobbs if (priv->upper == NULL) 273e1e1452dSArchie Cobbs return (0); 274e1e1452dSArchie Cobbs 275e1e1452dSArchie Cobbs /* Send it out "upper" hook */ 276f089869fSMarko Zec NG_OUTBOUND_THREAD_REF(); 277069154d5SJulian Elischer NG_SEND_DATA_ONLY(error, priv->upper, *mp); 278f089869fSMarko Zec NG_OUTBOUND_THREAD_UNREF(); 279e1e1452dSArchie Cobbs return (error); 280e1e1452dSArchie Cobbs } 281e1e1452dSArchie Cobbs 282e1e1452dSArchie Cobbs /* 283e1e1452dSArchie Cobbs * A new Ethernet interface has been attached. 284e1e1452dSArchie Cobbs * Create a new node for it, etc. 285e1e1452dSArchie Cobbs */ 286e1e1452dSArchie Cobbs static void 287e1e1452dSArchie Cobbs ng_ether_attach(struct ifnet *ifp) 288e1e1452dSArchie Cobbs { 289e1e1452dSArchie Cobbs priv_p priv; 290e1e1452dSArchie Cobbs node_p node; 291e1e1452dSArchie Cobbs 292aef8f344SMarko Zec /* 293aef8f344SMarko Zec * Do not create / attach an ether node to this ifnet if 294aef8f344SMarko Zec * a netgraph node with the same name already exists. 295aef8f344SMarko Zec * This should prevent ether nodes to become attached to 296aef8f344SMarko Zec * eiface nodes, which may be problematic due to naming 297aef8f344SMarko Zec * clashes. 298aef8f344SMarko Zec */ 299aef8f344SMarko Zec if ((node = ng_name2noderef(NULL, ifp->if_xname)) != NULL) { 300aef8f344SMarko Zec NG_NODE_UNREF(node); 301aef8f344SMarko Zec return; 302aef8f344SMarko Zec } 303aef8f344SMarko Zec 304e1e1452dSArchie Cobbs /* Create node */ 3056e551fb6SDavid E. O'Brien KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__)); 306e1e1452dSArchie Cobbs if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 307e1e1452dSArchie Cobbs log(LOG_ERR, "%s: can't %s for %s\n", 3089bf40edeSBrooks Davis __func__, "create node", ifp->if_xname); 309e1e1452dSArchie Cobbs return; 310e1e1452dSArchie Cobbs } 311e1e1452dSArchie Cobbs 312e1e1452dSArchie Cobbs /* Allocate private data */ 3131ede983cSDag-Erling Smørgrav priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 314e1e1452dSArchie Cobbs if (priv == NULL) { 315e1e1452dSArchie Cobbs log(LOG_ERR, "%s: can't %s for %s\n", 3169bf40edeSBrooks Davis __func__, "allocate memory", ifp->if_xname); 31730400f03SJulian Elischer NG_NODE_UNREF(node); 318e1e1452dSArchie Cobbs return; 319e1e1452dSArchie Cobbs } 32030400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, priv); 321e1e1452dSArchie Cobbs priv->ifp = ifp; 3225240dcdbSRuslan Ermilov IFP2NG(ifp) = node; 323a3e232d6SArchie Cobbs priv->hwassist = ifp->if_hwassist; 324e1e1452dSArchie Cobbs 325e1e1452dSArchie Cobbs /* Try to give the node the same name as the interface */ 3269bf40edeSBrooks Davis if (ng_name_node(node, ifp->if_xname) != 0) { 327e1e1452dSArchie Cobbs log(LOG_WARNING, "%s: can't name node %s\n", 3289bf40edeSBrooks Davis __func__, ifp->if_xname); 329e1e1452dSArchie Cobbs } 330e1e1452dSArchie Cobbs } 331e1e1452dSArchie Cobbs 332e1e1452dSArchie Cobbs /* 333e1e1452dSArchie Cobbs * An Ethernet interface is being detached. 3341acb27c6SJulian Elischer * REALLY Destroy its node. 335e1e1452dSArchie Cobbs */ 336e1e1452dSArchie Cobbs static void 337e1e1452dSArchie Cobbs ng_ether_detach(struct ifnet *ifp) 338e1e1452dSArchie Cobbs { 339e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp); 3401acb27c6SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 341e1e1452dSArchie Cobbs 3422cdf8c49SMarko Zec taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 3431acb27c6SJulian Elischer NG_NODE_REALLY_DIE(node); /* Force real removal of node */ 3441acb27c6SJulian Elischer /* 3451acb27c6SJulian Elischer * We can't assume the ifnet is still around when we run shutdown 3461acb27c6SJulian Elischer * So zap it now. XXX We HOPE that anything running at this time 3471acb27c6SJulian Elischer * handles it (as it should in the non netgraph case). 3481acb27c6SJulian Elischer */ 3495240dcdbSRuslan Ermilov IFP2NG(ifp) = NULL; 3501acb27c6SJulian Elischer priv->ifp = NULL; /* XXX race if interrupted an output packet */ 3511acb27c6SJulian Elischer ng_rmnode_self(node); /* remove all netgraph parts */ 352e1e1452dSArchie Cobbs } 353e1e1452dSArchie Cobbs 3541c7899c7SGleb Smirnoff /* 3551c7899c7SGleb Smirnoff * Notify graph about link event. 3561c7899c7SGleb Smirnoff * if_link_state_change() has already checked that the state has changed. 3571c7899c7SGleb Smirnoff */ 3581c7899c7SGleb Smirnoff static void 3591c7899c7SGleb Smirnoff ng_ether_link_state(struct ifnet *ifp, int state) 3601c7899c7SGleb Smirnoff { 3611c7899c7SGleb Smirnoff const node_p node = IFP2NG(ifp); 3621c7899c7SGleb Smirnoff const priv_p priv = NG_NODE_PRIVATE(node); 3631c7899c7SGleb Smirnoff struct ng_mesg *msg; 3641c7899c7SGleb Smirnoff int cmd, dummy_error = 0; 3651c7899c7SGleb Smirnoff 3661c7899c7SGleb Smirnoff if (state == LINK_STATE_UP) 3671c7899c7SGleb Smirnoff cmd = NGM_LINK_IS_UP; 3681c7899c7SGleb Smirnoff else if (state == LINK_STATE_DOWN) 3691c7899c7SGleb Smirnoff cmd = NGM_LINK_IS_DOWN; 3701c7899c7SGleb Smirnoff else 3711c7899c7SGleb Smirnoff return; 3721c7899c7SGleb Smirnoff 373bb027e06SMax Khon if (priv->lower != NULL) { 3741c7899c7SGleb Smirnoff NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT); 3751c7899c7SGleb Smirnoff if (msg != NULL) 3761c7899c7SGleb Smirnoff NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0); 3771c7899c7SGleb Smirnoff } 378bb027e06SMax Khon if (priv->orphan != NULL) { 379bb027e06SMax Khon NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT); 380bb027e06SMax Khon if (msg != NULL) 381bb027e06SMax Khon NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->orphan, 0); 382bb027e06SMax Khon } 383bb027e06SMax Khon } 3841c7899c7SGleb Smirnoff 385e1e1452dSArchie Cobbs /****************************************************************** 386e1e1452dSArchie Cobbs NETGRAPH NODE METHODS 387e1e1452dSArchie Cobbs ******************************************************************/ 388e1e1452dSArchie Cobbs 389e1e1452dSArchie Cobbs /* 390e1e1452dSArchie Cobbs * It is not possible or allowable to create a node of this type. 391e1e1452dSArchie Cobbs * Nodes get created when the interface is attached (or, when 392e1e1452dSArchie Cobbs * this node type's KLD is loaded). 393e1e1452dSArchie Cobbs */ 394e1e1452dSArchie Cobbs static int 395069154d5SJulian Elischer ng_ether_constructor(node_p node) 396e1e1452dSArchie Cobbs { 397e1e1452dSArchie Cobbs return (EINVAL); 398e1e1452dSArchie Cobbs } 399e1e1452dSArchie Cobbs 400e1e1452dSArchie Cobbs /* 401e1e1452dSArchie Cobbs * Check for attaching a new hook. 402e1e1452dSArchie Cobbs */ 403e1e1452dSArchie Cobbs static int 404e1e1452dSArchie Cobbs ng_ether_newhook(node_p node, hook_p hook, const char *name) 405e1e1452dSArchie Cobbs { 40630400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 407e1e1452dSArchie Cobbs hook_p *hookptr; 408e1e1452dSArchie Cobbs 409e1e1452dSArchie Cobbs /* Divert hook is an alias for lower */ 410e1e1452dSArchie Cobbs if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0) 411e1e1452dSArchie Cobbs name = NG_ETHER_HOOK_LOWER; 412e1e1452dSArchie Cobbs 413e1e1452dSArchie Cobbs /* Which hook? */ 414f664dcdeSJulian Elischer if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) { 415e1e1452dSArchie Cobbs hookptr = &priv->upper; 416f664dcdeSJulian Elischer NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_upper); 417f089869fSMarko Zec NG_HOOK_SET_TO_INBOUND(hook); 418f664dcdeSJulian Elischer } else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) { 419e1e1452dSArchie Cobbs hookptr = &priv->lower; 420f664dcdeSJulian Elischer NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower); 421f664dcdeSJulian Elischer } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) { 4221a292b80SArchie Cobbs hookptr = &priv->orphan; 423f664dcdeSJulian Elischer NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower); 424f664dcdeSJulian Elischer } else 425e1e1452dSArchie Cobbs return (EINVAL); 426e1e1452dSArchie Cobbs 427e1e1452dSArchie Cobbs /* Check if already connected (shouldn't be, but doesn't hurt) */ 428e1e1452dSArchie Cobbs if (*hookptr != NULL) 429e1e1452dSArchie Cobbs return (EISCONN); 430e1e1452dSArchie Cobbs 431a3e232d6SArchie Cobbs /* Disable hardware checksums while 'upper' hook is connected */ 432a3e232d6SArchie Cobbs if (hookptr == &priv->upper) 433a3e232d6SArchie Cobbs priv->ifp->if_hwassist = 0; 434c1b8a9edSAlexander Motin NG_HOOK_HI_STACK(hook); 435e1e1452dSArchie Cobbs /* OK */ 436e1e1452dSArchie Cobbs *hookptr = hook; 437e1e1452dSArchie Cobbs return (0); 438e1e1452dSArchie Cobbs } 439e1e1452dSArchie Cobbs 440e1e1452dSArchie Cobbs /* 441e1e1452dSArchie Cobbs * Receive an incoming control message. 442e1e1452dSArchie Cobbs */ 443e1e1452dSArchie Cobbs static int 444069154d5SJulian Elischer ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook) 445e1e1452dSArchie Cobbs { 44630400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 447e1e1452dSArchie Cobbs struct ng_mesg *resp = NULL; 448e1e1452dSArchie Cobbs int error = 0; 449069154d5SJulian Elischer struct ng_mesg *msg; 450e1e1452dSArchie Cobbs 451069154d5SJulian Elischer NGI_GET_MSG(item, msg); 452e1e1452dSArchie Cobbs switch (msg->header.typecookie) { 453e1e1452dSArchie Cobbs case NGM_ETHER_COOKIE: 454e1e1452dSArchie Cobbs switch (msg->header.cmd) { 455e1e1452dSArchie Cobbs case NGM_ETHER_GET_IFNAME: 456bbb75d78SRuslan Ermilov NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 457e1e1452dSArchie Cobbs if (resp == NULL) { 458e1e1452dSArchie Cobbs error = ENOMEM; 459e1e1452dSArchie Cobbs break; 460e1e1452dSArchie Cobbs } 461bbb75d78SRuslan Ermilov strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ); 462e1e1452dSArchie Cobbs break; 463e1e1452dSArchie Cobbs case NGM_ETHER_GET_IFINDEX: 464e1e1452dSArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 465e1e1452dSArchie Cobbs if (resp == NULL) { 466e1e1452dSArchie Cobbs error = ENOMEM; 467e1e1452dSArchie Cobbs break; 468e1e1452dSArchie Cobbs } 469e1e1452dSArchie Cobbs *((u_int32_t *)resp->data) = priv->ifp->if_index; 470e1e1452dSArchie Cobbs break; 4714b39c3c7SArchie Cobbs case NGM_ETHER_GET_ENADDR: 4724b39c3c7SArchie Cobbs NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT); 4734b39c3c7SArchie Cobbs if (resp == NULL) { 4744b39c3c7SArchie Cobbs error = ENOMEM; 4754b39c3c7SArchie Cobbs break; 4764b39c3c7SArchie Cobbs } 4774a0d6638SRuslan Ermilov bcopy(IF_LLADDR(priv->ifp), 4784b39c3c7SArchie Cobbs resp->data, ETHER_ADDR_LEN); 4794b39c3c7SArchie Cobbs break; 48056045c66SArchie Cobbs case NGM_ETHER_SET_ENADDR: 48156045c66SArchie Cobbs { 48256045c66SArchie Cobbs if (msg->header.arglen != ETHER_ADDR_LEN) { 48356045c66SArchie Cobbs error = EINVAL; 48456045c66SArchie Cobbs break; 48556045c66SArchie Cobbs } 48656045c66SArchie Cobbs error = if_setlladdr(priv->ifp, 48756045c66SArchie Cobbs (u_char *)msg->data, ETHER_ADDR_LEN); 488ea4ca115SAndrew Thompson EVENTHANDLER_INVOKE(iflladdr_event, priv->ifp); 48956045c66SArchie Cobbs break; 49056045c66SArchie Cobbs } 49156045c66SArchie Cobbs case NGM_ETHER_GET_PROMISC: 49256045c66SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 49356045c66SArchie Cobbs if (resp == NULL) { 49456045c66SArchie Cobbs error = ENOMEM; 49556045c66SArchie Cobbs break; 49656045c66SArchie Cobbs } 49756045c66SArchie Cobbs *((u_int32_t *)resp->data) = priv->promisc; 49856045c66SArchie Cobbs break; 4994b39c3c7SArchie Cobbs case NGM_ETHER_SET_PROMISC: 5004b39c3c7SArchie Cobbs { 5014b39c3c7SArchie Cobbs u_char want; 5024b39c3c7SArchie Cobbs 5034b39c3c7SArchie Cobbs if (msg->header.arglen != sizeof(u_int32_t)) { 5044b39c3c7SArchie Cobbs error = EINVAL; 5054b39c3c7SArchie Cobbs break; 5064b39c3c7SArchie Cobbs } 5074b39c3c7SArchie Cobbs want = !!*((u_int32_t *)msg->data); 5084b39c3c7SArchie Cobbs if (want ^ priv->promisc) { 5094b39c3c7SArchie Cobbs if ((error = ifpromisc(priv->ifp, want)) != 0) 5104b39c3c7SArchie Cobbs break; 5114b39c3c7SArchie Cobbs priv->promisc = want; 5124b39c3c7SArchie Cobbs } 5134b39c3c7SArchie Cobbs break; 5144b39c3c7SArchie Cobbs } 51556045c66SArchie Cobbs case NGM_ETHER_GET_AUTOSRC: 51656045c66SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 51756045c66SArchie Cobbs if (resp == NULL) { 51856045c66SArchie Cobbs error = ENOMEM; 51956045c66SArchie Cobbs break; 52056045c66SArchie Cobbs } 52156045c66SArchie Cobbs *((u_int32_t *)resp->data) = priv->autoSrcAddr; 52256045c66SArchie Cobbs break; 5234b39c3c7SArchie Cobbs case NGM_ETHER_SET_AUTOSRC: 5244b39c3c7SArchie Cobbs if (msg->header.arglen != sizeof(u_int32_t)) { 5254b39c3c7SArchie Cobbs error = EINVAL; 5264b39c3c7SArchie Cobbs break; 5274b39c3c7SArchie Cobbs } 5284b39c3c7SArchie Cobbs priv->autoSrcAddr = !!*((u_int32_t *)msg->data); 5294b39c3c7SArchie Cobbs break; 530810d5e89SGleb Smirnoff case NGM_ETHER_ADD_MULTI: 531810d5e89SGleb Smirnoff { 532810d5e89SGleb Smirnoff struct sockaddr_dl sa_dl; 533ec002feeSBruce M Simpson struct ifmultiaddr *ifma; 534810d5e89SGleb Smirnoff 535810d5e89SGleb Smirnoff if (msg->header.arglen != ETHER_ADDR_LEN) { 536810d5e89SGleb Smirnoff error = EINVAL; 537810d5e89SGleb Smirnoff break; 538810d5e89SGleb Smirnoff } 539ba20540eSGleb Smirnoff bzero(&sa_dl, sizeof(struct sockaddr_dl)); 540810d5e89SGleb Smirnoff sa_dl.sdl_len = sizeof(struct sockaddr_dl); 541810d5e89SGleb Smirnoff sa_dl.sdl_family = AF_LINK; 542ba20540eSGleb Smirnoff sa_dl.sdl_alen = ETHER_ADDR_LEN; 543810d5e89SGleb Smirnoff bcopy((void *)msg->data, LLADDR(&sa_dl), 544810d5e89SGleb Smirnoff ETHER_ADDR_LEN); 545ec002feeSBruce M Simpson /* 546ec002feeSBruce M Simpson * Netgraph is only permitted to join groups once 547ec002feeSBruce M Simpson * via the if_addmulti() KPI, because it cannot hold 548ec002feeSBruce M Simpson * struct ifmultiaddr * between calls. It may also 549ec002feeSBruce M Simpson * lose a race while we check if the membership 550ec002feeSBruce M Simpson * already exists. 551ec002feeSBruce M Simpson */ 552eb956cd0SRobert Watson if_maddr_rlock(priv->ifp); 553ec002feeSBruce M Simpson ifma = if_findmulti(priv->ifp, 554ec002feeSBruce M Simpson (struct sockaddr *)&sa_dl); 555eb956cd0SRobert Watson if_maddr_runlock(priv->ifp); 556ec002feeSBruce M Simpson if (ifma != NULL) { 557ec002feeSBruce M Simpson error = EADDRINUSE; 558ec002feeSBruce M Simpson } else { 559810d5e89SGleb Smirnoff error = if_addmulti(priv->ifp, 560ec002feeSBruce M Simpson (struct sockaddr *)&sa_dl, &ifma); 561ec002feeSBruce M Simpson } 562810d5e89SGleb Smirnoff break; 563810d5e89SGleb Smirnoff } 564810d5e89SGleb Smirnoff case NGM_ETHER_DEL_MULTI: 565810d5e89SGleb Smirnoff { 566810d5e89SGleb Smirnoff struct sockaddr_dl sa_dl; 567810d5e89SGleb Smirnoff 568810d5e89SGleb Smirnoff if (msg->header.arglen != ETHER_ADDR_LEN) { 569810d5e89SGleb Smirnoff error = EINVAL; 570810d5e89SGleb Smirnoff break; 571810d5e89SGleb Smirnoff } 572ba20540eSGleb Smirnoff bzero(&sa_dl, sizeof(struct sockaddr_dl)); 573810d5e89SGleb Smirnoff sa_dl.sdl_len = sizeof(struct sockaddr_dl); 574810d5e89SGleb Smirnoff sa_dl.sdl_family = AF_LINK; 575ba20540eSGleb Smirnoff sa_dl.sdl_alen = ETHER_ADDR_LEN; 576810d5e89SGleb Smirnoff bcopy((void *)msg->data, LLADDR(&sa_dl), 577810d5e89SGleb Smirnoff ETHER_ADDR_LEN); 578810d5e89SGleb Smirnoff error = if_delmulti(priv->ifp, 579810d5e89SGleb Smirnoff (struct sockaddr *)&sa_dl); 580810d5e89SGleb Smirnoff break; 581810d5e89SGleb Smirnoff } 582cefddd66SGleb Smirnoff case NGM_ETHER_DETACH: 583cefddd66SGleb Smirnoff ng_ether_detach(priv->ifp); 584cefddd66SGleb Smirnoff break; 585e1e1452dSArchie Cobbs default: 586e1e1452dSArchie Cobbs error = EINVAL; 587e1e1452dSArchie Cobbs break; 588e1e1452dSArchie Cobbs } 589e1e1452dSArchie Cobbs break; 590e1e1452dSArchie Cobbs default: 591e1e1452dSArchie Cobbs error = EINVAL; 592e1e1452dSArchie Cobbs break; 593e1e1452dSArchie Cobbs } 594069154d5SJulian Elischer NG_RESPOND_MSG(error, node, item, resp); 595069154d5SJulian Elischer NG_FREE_MSG(msg); 596e1e1452dSArchie Cobbs return (error); 597e1e1452dSArchie Cobbs } 598e1e1452dSArchie Cobbs 599e1e1452dSArchie Cobbs /* 600e1e1452dSArchie Cobbs * Receive data on a hook. 601f664dcdeSJulian Elischer * Since we use per-hook recveive methods this should never be called. 602e1e1452dSArchie Cobbs */ 603e1e1452dSArchie Cobbs static int 604069154d5SJulian Elischer ng_ether_rcvdata(hook_p hook, item_p item) 605e1e1452dSArchie Cobbs { 606069154d5SJulian Elischer NG_FREE_ITEM(item); 6073ca24c28SJulian Elischer 6086e551fb6SDavid E. O'Brien panic("%s: weird hook", __func__); 609e1e1452dSArchie Cobbs } 610e1e1452dSArchie Cobbs 611e1e1452dSArchie Cobbs /* 6121a292b80SArchie Cobbs * Handle an mbuf received on the "lower" or "orphan" hook. 613e1e1452dSArchie Cobbs */ 614e1e1452dSArchie Cobbs static int 615f664dcdeSJulian Elischer ng_ether_rcv_lower(hook_p hook, item_p item) 616e1e1452dSArchie Cobbs { 617f664dcdeSJulian Elischer struct mbuf *m; 618f664dcdeSJulian Elischer const node_p node = NG_HOOK_NODE(hook); 61930400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 620a1479aa2SArchie Cobbs struct ifnet *const ifp = priv->ifp; 621a1479aa2SArchie Cobbs 622f664dcdeSJulian Elischer NGI_GET_M(item, m); 623f664dcdeSJulian Elischer NG_FREE_ITEM(item); 624f664dcdeSJulian Elischer 625a1479aa2SArchie Cobbs /* Check whether interface is ready for packets */ 626f664dcdeSJulian Elischer 62713f4c340SRobert Watson if (!((ifp->if_flags & IFF_UP) && 62813f4c340SRobert Watson (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 629a1479aa2SArchie Cobbs NG_FREE_M(m); 630a1479aa2SArchie Cobbs return (ENETDOWN); 631a1479aa2SArchie Cobbs } 632e1e1452dSArchie Cobbs 633e1e1452dSArchie Cobbs /* Make sure header is fully pulled up */ 634e1e1452dSArchie Cobbs if (m->m_pkthdr.len < sizeof(struct ether_header)) { 635069154d5SJulian Elischer NG_FREE_M(m); 636e1e1452dSArchie Cobbs return (EINVAL); 637e1e1452dSArchie Cobbs } 638e1e1452dSArchie Cobbs if (m->m_len < sizeof(struct ether_header) 6397b9f235fSArchie Cobbs && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 640e1e1452dSArchie Cobbs return (ENOBUFS); 641e1e1452dSArchie Cobbs 6424b39c3c7SArchie Cobbs /* Drop in the MAC address if desired */ 6434b39c3c7SArchie Cobbs if (priv->autoSrcAddr) { 6447b9f235fSArchie Cobbs 6457b9f235fSArchie Cobbs /* Make the mbuf writable if it's not already */ 6467b9f235fSArchie Cobbs if (!M_WRITABLE(m) 6477b9f235fSArchie Cobbs && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 6487b9f235fSArchie Cobbs return (ENOBUFS); 6497b9f235fSArchie Cobbs 6507b9f235fSArchie Cobbs /* Overwrite source MAC address */ 6514a0d6638SRuslan Ermilov bcopy(IF_LLADDR(ifp), 6524b39c3c7SArchie Cobbs mtod(m, struct ether_header *)->ether_shost, 6534b39c3c7SArchie Cobbs ETHER_ADDR_LEN); 6544b39c3c7SArchie Cobbs } 655561e4fb9SJulian Elischer 656e1e1452dSArchie Cobbs /* Send it on its way */ 657a1479aa2SArchie Cobbs return ether_output_frame(ifp, m); 658e1e1452dSArchie Cobbs } 659e1e1452dSArchie Cobbs 660e1e1452dSArchie Cobbs /* 661e1e1452dSArchie Cobbs * Handle an mbuf received on the "upper" hook. 662e1e1452dSArchie Cobbs */ 663e1e1452dSArchie Cobbs static int 664f664dcdeSJulian Elischer ng_ether_rcv_upper(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); 6696512768bSGleb Smirnoff struct ifnet *ifp = priv->ifp; 670e1e1452dSArchie Cobbs 671f664dcdeSJulian Elischer NGI_GET_M(item, m); 672f664dcdeSJulian Elischer NG_FREE_ITEM(item); 673f664dcdeSJulian Elischer 674c60c00bcSRuslan Ermilov /* Check length and pull off header */ 675c60c00bcSRuslan Ermilov if (m->m_pkthdr.len < sizeof(struct ether_header)) { 676c60c00bcSRuslan Ermilov NG_FREE_M(m); 677c60c00bcSRuslan Ermilov return (EINVAL); 678c60c00bcSRuslan Ermilov } 679c60c00bcSRuslan Ermilov if (m->m_len < sizeof(struct ether_header) && 680c60c00bcSRuslan Ermilov (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 681c60c00bcSRuslan Ermilov return (ENOBUFS); 682c60c00bcSRuslan Ermilov 6836512768bSGleb Smirnoff m->m_pkthdr.rcvif = ifp; 684e1e1452dSArchie Cobbs 685fd6238a6SAndrew Thompson /* Pass the packet to the bridge, it may come back to us */ 6866512768bSGleb Smirnoff if (ifp->if_bridge) { 687fd6238a6SAndrew Thompson BRIDGE_INPUT(ifp, m); 6886512768bSGleb Smirnoff if (m == NULL) 6896512768bSGleb Smirnoff return (0); 6906512768bSGleb Smirnoff } 691a176c2aeSGleb Smirnoff 692e1e1452dSArchie Cobbs /* Route packet back in */ 693fd6238a6SAndrew Thompson ether_demux(ifp, m); 694e1e1452dSArchie Cobbs return (0); 695e1e1452dSArchie Cobbs } 696e1e1452dSArchie Cobbs 697e1e1452dSArchie Cobbs /* 6981acb27c6SJulian Elischer * Shutdown node. This resets the node but does not remove it 6991acb27c6SJulian Elischer * unless the REALLY_DIE flag is set. 700e1e1452dSArchie Cobbs */ 701e1e1452dSArchie Cobbs static int 702069154d5SJulian Elischer ng_ether_shutdown(node_p node) 703e1e1452dSArchie Cobbs { 70430400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 7054b39c3c7SArchie Cobbs 706be4252b3SJulian Elischer if (node->nd_flags & NGF_REALLY_DIE) { 7071acb27c6SJulian Elischer /* 7081acb27c6SJulian Elischer * WE came here because the ethernet card is being unloaded, 7091acb27c6SJulian Elischer * so stop being persistant. 7101acb27c6SJulian Elischer * Actually undo all the things we did on creation. 7111acb27c6SJulian Elischer * Assume the ifp has already been freed. 7121acb27c6SJulian Elischer */ 7131acb27c6SJulian Elischer NG_NODE_SET_PRIVATE(node, NULL); 7141ede983cSDag-Erling Smørgrav free(priv, M_NETGRAPH); 7151acb27c6SJulian Elischer NG_NODE_UNREF(node); /* free node itself */ 7161acb27c6SJulian Elischer return (0); 717069154d5SJulian Elischer } 718018df1c3SBrian Feldman if (priv->promisc) { /* disable promiscuous mode */ 719018df1c3SBrian Feldman (void)ifpromisc(priv->ifp, 0); 720018df1c3SBrian Feldman priv->promisc = 0; 721018df1c3SBrian Feldman } 722be4252b3SJulian Elischer NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */ 723be4252b3SJulian Elischer 724e1e1452dSArchie Cobbs return (0); 725e1e1452dSArchie Cobbs } 726e1e1452dSArchie Cobbs 727e1e1452dSArchie Cobbs /* 728e1e1452dSArchie Cobbs * Hook disconnection. 729e1e1452dSArchie Cobbs */ 730e1e1452dSArchie Cobbs static int 731e1e1452dSArchie Cobbs ng_ether_disconnect(hook_p hook) 732e1e1452dSArchie Cobbs { 73330400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 734e1e1452dSArchie Cobbs 735a3e232d6SArchie Cobbs if (hook == priv->upper) { 736e1e1452dSArchie Cobbs priv->upper = NULL; 737b712e9ecSBrian Feldman if (priv->ifp != NULL) /* restore h/w csum */ 738b712e9ecSBrian Feldman priv->ifp->if_hwassist = priv->hwassist; 7391a292b80SArchie Cobbs } else if (hook == priv->lower) 740e1e1452dSArchie Cobbs priv->lower = NULL; 7411a292b80SArchie Cobbs else if (hook == priv->orphan) 7421a292b80SArchie Cobbs priv->orphan = NULL; 7431a292b80SArchie Cobbs else 7446e551fb6SDavid E. O'Brien panic("%s: weird hook", __func__); 74530400f03SJulian Elischer if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 74630400f03SJulian Elischer && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 74730400f03SJulian Elischer ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */ 748e1e1452dSArchie Cobbs return (0); 749e1e1452dSArchie Cobbs } 750e1e1452dSArchie Cobbs 751e1e1452dSArchie Cobbs /****************************************************************** 752e1e1452dSArchie Cobbs INITIALIZATION 753e1e1452dSArchie Cobbs ******************************************************************/ 754e1e1452dSArchie Cobbs 755e1e1452dSArchie Cobbs /* 756e1e1452dSArchie Cobbs * Handle loading and unloading for this node type. 757e1e1452dSArchie Cobbs */ 758e1e1452dSArchie Cobbs static int 759e1e1452dSArchie Cobbs ng_ether_mod_event(module_t mod, int event, void *data) 760e1e1452dSArchie Cobbs { 761e1e1452dSArchie Cobbs int error = 0; 762e1e1452dSArchie Cobbs int s; 763e1e1452dSArchie Cobbs 764e1e1452dSArchie Cobbs s = splnet(); 765e1e1452dSArchie Cobbs switch (event) { 766e1e1452dSArchie Cobbs case MOD_LOAD: 767e1e1452dSArchie Cobbs 768e1e1452dSArchie Cobbs /* Register function hooks */ 769e1e1452dSArchie Cobbs if (ng_ether_attach_p != NULL) { 770e1e1452dSArchie Cobbs error = EEXIST; 771e1e1452dSArchie Cobbs break; 772e1e1452dSArchie Cobbs } 773e1e1452dSArchie Cobbs ng_ether_attach_p = ng_ether_attach; 774e1e1452dSArchie Cobbs ng_ether_detach_p = ng_ether_detach; 775e1e1452dSArchie Cobbs ng_ether_output_p = ng_ether_output; 776e1e1452dSArchie Cobbs ng_ether_input_p = ng_ether_input; 777e1e1452dSArchie Cobbs ng_ether_input_orphan_p = ng_ether_input_orphan; 7781c7899c7SGleb Smirnoff ng_ether_link_state_p = ng_ether_link_state; 779e1e1452dSArchie Cobbs 780e1e1452dSArchie Cobbs break; 781e1e1452dSArchie Cobbs 782e1e1452dSArchie Cobbs case MOD_UNLOAD: 783e1e1452dSArchie Cobbs 784e1e1452dSArchie Cobbs /* 785e1e1452dSArchie Cobbs * Note that the base code won't try to unload us until 786e1e1452dSArchie Cobbs * all nodes have been removed, and that can't happen 787e1e1452dSArchie Cobbs * until all Ethernet interfaces are removed. In any 788e1e1452dSArchie Cobbs * case, we know there are no nodes left if the action 789e1e1452dSArchie Cobbs * is MOD_UNLOAD, so there's no need to detach any nodes. 790e1e1452dSArchie Cobbs */ 791e1e1452dSArchie Cobbs 792e1e1452dSArchie Cobbs /* Unregister function hooks */ 793e1e1452dSArchie Cobbs ng_ether_attach_p = NULL; 794e1e1452dSArchie Cobbs ng_ether_detach_p = NULL; 795e1e1452dSArchie Cobbs ng_ether_output_p = NULL; 796e1e1452dSArchie Cobbs ng_ether_input_p = NULL; 797e1e1452dSArchie Cobbs ng_ether_input_orphan_p = NULL; 7981c7899c7SGleb Smirnoff ng_ether_link_state_p = NULL; 799e1e1452dSArchie Cobbs break; 800e1e1452dSArchie Cobbs 801e1e1452dSArchie Cobbs default: 802e1e1452dSArchie Cobbs error = EOPNOTSUPP; 803e1e1452dSArchie Cobbs break; 804e1e1452dSArchie Cobbs } 805e1e1452dSArchie Cobbs splx(s); 806e1e1452dSArchie Cobbs return (error); 807e1e1452dSArchie Cobbs } 808e1e1452dSArchie Cobbs 809d0728d71SRobert Watson static void 810d0728d71SRobert Watson vnet_ng_ether_init(const void *unused) 811aef8f344SMarko Zec { 812aef8f344SMarko Zec struct ifnet *ifp; 813aef8f344SMarko Zec 814d0728d71SRobert Watson /* If module load was rejected, don't attach to vnets. */ 815d0728d71SRobert Watson if (ng_ether_attach_p != ng_ether_attach) 816d0728d71SRobert Watson return; 817d0728d71SRobert Watson 818aef8f344SMarko Zec /* Create nodes for any already-existing Ethernet interfaces. */ 819aef8f344SMarko Zec IFNET_RLOCK(); 820aef8f344SMarko Zec TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 821aef8f344SMarko Zec if (ifp->if_type == IFT_ETHER 822aef8f344SMarko Zec || ifp->if_type == IFT_L2VLAN) 823aef8f344SMarko Zec ng_ether_attach(ifp); 824aef8f344SMarko Zec } 825aef8f344SMarko Zec IFNET_RUNLOCK(); 826aef8f344SMarko Zec } 827d0728d71SRobert Watson VNET_SYSINIT(vnet_ng_ether_init, SI_SUB_PSEUDO, SI_ORDER_ANY, 828d0728d71SRobert Watson vnet_ng_ether_init, NULL); 829