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 56e1e1452dSArchie Cobbs #include <net/if.h> 57e1e1452dSArchie Cobbs #include <net/if_types.h> 58e1e1452dSArchie Cobbs #include <net/if_arp.h> 59e1e1452dSArchie Cobbs #include <net/if_var.h> 60e1e1452dSArchie Cobbs #include <net/ethernet.h> 61e1e1452dSArchie Cobbs 62e1e1452dSArchie Cobbs #include <netgraph/ng_message.h> 63e1e1452dSArchie Cobbs #include <netgraph/netgraph.h> 64e1e1452dSArchie Cobbs #include <netgraph/ng_parse.h> 65e1e1452dSArchie Cobbs #include <netgraph/ng_ether.h> 66e1e1452dSArchie Cobbs 67561e4fb9SJulian Elischer #define IFP2AC(IFP) ((struct arpcom *)IFP) 68e1e1452dSArchie Cobbs #define IFP2NG(ifp) ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph) 69e1e1452dSArchie Cobbs 70e1e1452dSArchie Cobbs /* Per-node private data */ 71e1e1452dSArchie Cobbs struct private { 72e1e1452dSArchie Cobbs struct ifnet *ifp; /* associated interface */ 73e1e1452dSArchie Cobbs hook_p upper; /* upper hook connection */ 74e1e1452dSArchie Cobbs hook_p lower; /* lower OR orphan hook connection */ 75e1e1452dSArchie Cobbs u_char lowerOrphan; /* whether lower is lower or orphan */ 764b39c3c7SArchie Cobbs u_char autoSrcAddr; /* always overwrite source address */ 774b39c3c7SArchie Cobbs u_char promisc; /* promiscuous mode enabled */ 781acb27c6SJulian Elischer u_int flags; /* flags e.g. really die */ 79e1e1452dSArchie Cobbs }; 80e1e1452dSArchie Cobbs typedef struct private *priv_p; 81e1e1452dSArchie Cobbs 82e1e1452dSArchie Cobbs /* Functional hooks called from if_ethersubr.c */ 83e1e1452dSArchie Cobbs static void ng_ether_input(struct ifnet *ifp, 84e1e1452dSArchie Cobbs struct mbuf **mp, struct ether_header *eh); 85e1e1452dSArchie Cobbs static void ng_ether_input_orphan(struct ifnet *ifp, 86e1e1452dSArchie Cobbs struct mbuf *m, struct ether_header *eh); 87e1e1452dSArchie Cobbs static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp); 88e1e1452dSArchie Cobbs static void ng_ether_attach(struct ifnet *ifp); 89e1e1452dSArchie Cobbs static void ng_ether_detach(struct ifnet *ifp); 90e1e1452dSArchie Cobbs 91e1e1452dSArchie Cobbs /* Other functions */ 92e1e1452dSArchie Cobbs static void ng_ether_input2(node_p node, 93e1e1452dSArchie Cobbs struct mbuf **mp, struct ether_header *eh); 94e1e1452dSArchie Cobbs static int ng_ether_glueback_header(struct mbuf **mp, 95e1e1452dSArchie Cobbs struct ether_header *eh); 96e1e1452dSArchie Cobbs static int ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta); 97e1e1452dSArchie Cobbs static int ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta); 98e1e1452dSArchie Cobbs 99e1e1452dSArchie Cobbs /* Netgraph node methods */ 100e1e1452dSArchie Cobbs static ng_constructor_t ng_ether_constructor; 101e1e1452dSArchie Cobbs static ng_rcvmsg_t ng_ether_rcvmsg; 102069154d5SJulian Elischer static ng_shutdown_t ng_ether_shutdown; 103e1e1452dSArchie Cobbs static ng_newhook_t ng_ether_newhook; 104859a4d16SJulian Elischer static ng_connect_t ng_ether_connect; 105e1e1452dSArchie Cobbs static ng_rcvdata_t ng_ether_rcvdata; 106e1e1452dSArchie Cobbs static ng_disconnect_t ng_ether_disconnect; 107e1e1452dSArchie Cobbs static int ng_ether_mod_event(module_t mod, int event, void *data); 108e1e1452dSArchie Cobbs 10956045c66SArchie Cobbs /* Parse type for an Ethernet address */ 11056045c66SArchie Cobbs static ng_parse_t ng_enaddr_parse; 11156045c66SArchie Cobbs static ng_unparse_t ng_enaddr_unparse; 11256045c66SArchie Cobbs const struct ng_parse_type ng_ether_enaddr_type = { 11356045c66SArchie Cobbs NULL, 11456045c66SArchie Cobbs NULL, 11556045c66SArchie Cobbs NULL, 11656045c66SArchie Cobbs ng_enaddr_parse, 11756045c66SArchie Cobbs ng_enaddr_unparse, 11856045c66SArchie Cobbs NULL, /* no such thing as a "default" EN address */ 11956045c66SArchie Cobbs 0 1204b39c3c7SArchie Cobbs }; 1214b39c3c7SArchie Cobbs 122e1e1452dSArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */ 123e1e1452dSArchie Cobbs static const struct ng_cmdlist ng_ether_cmdlist[] = { 124e1e1452dSArchie Cobbs { 125e1e1452dSArchie Cobbs NGM_ETHER_COOKIE, 126e1e1452dSArchie Cobbs NGM_ETHER_GET_IFNAME, 127e1e1452dSArchie Cobbs "getifname", 128e1e1452dSArchie Cobbs NULL, 129e1e1452dSArchie Cobbs &ng_parse_string_type 130e1e1452dSArchie Cobbs }, 131e1e1452dSArchie Cobbs { 132e1e1452dSArchie Cobbs NGM_ETHER_COOKIE, 133e1e1452dSArchie Cobbs NGM_ETHER_GET_IFINDEX, 134e1e1452dSArchie Cobbs "getifindex", 135e1e1452dSArchie Cobbs NULL, 136e1e1452dSArchie Cobbs &ng_parse_int32_type 137e1e1452dSArchie Cobbs }, 1384b39c3c7SArchie Cobbs { 1394b39c3c7SArchie Cobbs NGM_ETHER_COOKIE, 1404b39c3c7SArchie Cobbs NGM_ETHER_GET_ENADDR, 1414b39c3c7SArchie Cobbs "getenaddr", 1424b39c3c7SArchie Cobbs NULL, 1434b39c3c7SArchie Cobbs &ng_ether_enaddr_type 1444b39c3c7SArchie Cobbs }, 1454b39c3c7SArchie Cobbs { 1464b39c3c7SArchie Cobbs NGM_ETHER_COOKIE, 14756045c66SArchie Cobbs NGM_ETHER_SET_ENADDR, 14856045c66SArchie Cobbs "setenaddr", 14956045c66SArchie Cobbs &ng_ether_enaddr_type, 15056045c66SArchie Cobbs NULL 15156045c66SArchie Cobbs }, 15256045c66SArchie Cobbs { 15356045c66SArchie Cobbs NGM_ETHER_COOKIE, 15456045c66SArchie Cobbs NGM_ETHER_GET_PROMISC, 15556045c66SArchie Cobbs "getpromisc", 15656045c66SArchie Cobbs NULL, 15756045c66SArchie Cobbs &ng_parse_int32_type 15856045c66SArchie Cobbs }, 15956045c66SArchie Cobbs { 16056045c66SArchie Cobbs NGM_ETHER_COOKIE, 1614b39c3c7SArchie Cobbs NGM_ETHER_SET_PROMISC, 1624b39c3c7SArchie Cobbs "setpromisc", 1634b39c3c7SArchie Cobbs &ng_parse_int32_type, 1644b39c3c7SArchie Cobbs NULL 1654b39c3c7SArchie Cobbs }, 1664b39c3c7SArchie Cobbs { 1674b39c3c7SArchie Cobbs NGM_ETHER_COOKIE, 16856045c66SArchie Cobbs NGM_ETHER_GET_AUTOSRC, 16956045c66SArchie Cobbs "getautosrc", 17056045c66SArchie Cobbs NULL, 17156045c66SArchie Cobbs &ng_parse_int32_type 17256045c66SArchie Cobbs }, 17356045c66SArchie Cobbs { 17456045c66SArchie Cobbs NGM_ETHER_COOKIE, 1754b39c3c7SArchie Cobbs NGM_ETHER_SET_AUTOSRC, 1764b39c3c7SArchie Cobbs "setautosrc", 1774b39c3c7SArchie Cobbs &ng_parse_int32_type, 1784b39c3c7SArchie Cobbs NULL 1794b39c3c7SArchie Cobbs }, 180e1e1452dSArchie Cobbs { 0 } 181e1e1452dSArchie Cobbs }; 182e1e1452dSArchie Cobbs 183e1e1452dSArchie Cobbs static struct ng_type ng_ether_typestruct = { 184589f6ed8SJulian Elischer NG_ABI_VERSION, 185e1e1452dSArchie Cobbs NG_ETHER_NODE_TYPE, 186e1e1452dSArchie Cobbs ng_ether_mod_event, 187e1e1452dSArchie Cobbs ng_ether_constructor, 188e1e1452dSArchie Cobbs ng_ether_rcvmsg, 189069154d5SJulian Elischer ng_ether_shutdown, 190e1e1452dSArchie Cobbs ng_ether_newhook, 191e1e1452dSArchie Cobbs NULL, 192859a4d16SJulian Elischer ng_ether_connect, 193e1e1452dSArchie Cobbs ng_ether_rcvdata, 194e1e1452dSArchie Cobbs ng_ether_disconnect, 195e1e1452dSArchie Cobbs ng_ether_cmdlist, 196e1e1452dSArchie Cobbs }; 1976b795970SJulian Elischer MODULE_VERSION(ng_ether, 1); 198e1e1452dSArchie Cobbs NETGRAPH_INIT(ether, &ng_ether_typestruct); 199e1e1452dSArchie Cobbs 200e1e1452dSArchie Cobbs /****************************************************************** 201e1e1452dSArchie Cobbs ETHERNET FUNCTION HOOKS 202e1e1452dSArchie Cobbs ******************************************************************/ 203e1e1452dSArchie Cobbs 204e1e1452dSArchie Cobbs /* 205e1e1452dSArchie Cobbs * Handle a packet that has come in on an interface. We get to 206e1e1452dSArchie Cobbs * look at it here before any upper layer protocols do. 207e1e1452dSArchie Cobbs * 208e1e1452dSArchie Cobbs * NOTE: this function will get called at splimp() 209e1e1452dSArchie Cobbs */ 210e1e1452dSArchie Cobbs static void 211e1e1452dSArchie Cobbs ng_ether_input(struct ifnet *ifp, 212e1e1452dSArchie Cobbs struct mbuf **mp, struct ether_header *eh) 213e1e1452dSArchie Cobbs { 214e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp); 21530400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 216e1e1452dSArchie Cobbs 217e1e1452dSArchie Cobbs /* If "lower" hook not connected, let packet continue */ 218e1e1452dSArchie Cobbs if (priv->lower == NULL || priv->lowerOrphan) 219e1e1452dSArchie Cobbs return; 220e1e1452dSArchie Cobbs ng_ether_input2(node, mp, eh); 221e1e1452dSArchie Cobbs } 222e1e1452dSArchie Cobbs 223e1e1452dSArchie Cobbs /* 224e1e1452dSArchie Cobbs * Handle a packet that has come in on an interface, and which 225e1e1452dSArchie Cobbs * does not match any of our known protocols (an ``orphan''). 226e1e1452dSArchie Cobbs * 227e1e1452dSArchie Cobbs * NOTE: this function will get called at splimp() 228e1e1452dSArchie Cobbs */ 229e1e1452dSArchie Cobbs static void 230e1e1452dSArchie Cobbs ng_ether_input_orphan(struct ifnet *ifp, 231e1e1452dSArchie Cobbs struct mbuf *m, struct ether_header *eh) 232e1e1452dSArchie Cobbs { 233e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp); 23430400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 235e1e1452dSArchie Cobbs 236e1e1452dSArchie Cobbs /* If "orphan" hook not connected, let packet continue */ 237e1e1452dSArchie Cobbs if (priv->lower == NULL || !priv->lowerOrphan) { 238e1e1452dSArchie Cobbs m_freem(m); 239e1e1452dSArchie Cobbs return; 240e1e1452dSArchie Cobbs } 241e1e1452dSArchie Cobbs ng_ether_input2(node, &m, eh); 242e1e1452dSArchie Cobbs if (m != NULL) 243e1e1452dSArchie Cobbs m_freem(m); 244e1e1452dSArchie Cobbs } 245e1e1452dSArchie Cobbs 246e1e1452dSArchie Cobbs /* 247859a4d16SJulian Elischer * Handle a packet that has come in on an ethernet interface. 248e1e1452dSArchie Cobbs * The Ethernet header has already been detached from the mbuf, 249e1e1452dSArchie Cobbs * so we have to put it back. 250e1e1452dSArchie Cobbs * 251e1e1452dSArchie Cobbs * NOTE: this function will get called at splimp() 252e1e1452dSArchie Cobbs */ 253e1e1452dSArchie Cobbs static void 254e1e1452dSArchie Cobbs ng_ether_input2(node_p node, struct mbuf **mp, struct ether_header *eh) 255e1e1452dSArchie Cobbs { 25630400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 257e1e1452dSArchie Cobbs int error; 258e1e1452dSArchie Cobbs 259e1e1452dSArchie Cobbs /* Glue Ethernet header back on */ 260e1e1452dSArchie Cobbs if ((error = ng_ether_glueback_header(mp, eh)) != 0) 261e1e1452dSArchie Cobbs return; 262e1e1452dSArchie Cobbs 263e1e1452dSArchie Cobbs /* Send out lower/orphan hook */ 264859a4d16SJulian Elischer NG_SEND_DATA_ONLY(error, priv->lower, *mp); 265e1e1452dSArchie Cobbs *mp = NULL; 266e1e1452dSArchie Cobbs } 267e1e1452dSArchie Cobbs 268e1e1452dSArchie Cobbs /* 269e1e1452dSArchie Cobbs * Handle a packet that is going out on an interface. 270e1e1452dSArchie Cobbs * The Ethernet header is already attached to the mbuf. 271e1e1452dSArchie Cobbs */ 272e1e1452dSArchie Cobbs static int 273e1e1452dSArchie Cobbs ng_ether_output(struct ifnet *ifp, struct mbuf **mp) 274e1e1452dSArchie Cobbs { 275e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp); 27630400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 277e1e1452dSArchie Cobbs int error = 0; 278e1e1452dSArchie Cobbs 279e1e1452dSArchie Cobbs /* If "upper" hook not connected, let packet continue */ 280e1e1452dSArchie Cobbs if (priv->upper == NULL) 281e1e1452dSArchie Cobbs return (0); 282e1e1452dSArchie Cobbs 283e1e1452dSArchie Cobbs /* Send it out "upper" hook */ 284069154d5SJulian Elischer NG_SEND_DATA_ONLY(error, priv->upper, *mp); 285e1e1452dSArchie Cobbs return (error); 286e1e1452dSArchie Cobbs } 287e1e1452dSArchie Cobbs 288e1e1452dSArchie Cobbs /* 289e1e1452dSArchie Cobbs * A new Ethernet interface has been attached. 290e1e1452dSArchie Cobbs * Create a new node for it, etc. 291e1e1452dSArchie Cobbs */ 292e1e1452dSArchie Cobbs static void 293e1e1452dSArchie Cobbs ng_ether_attach(struct ifnet *ifp) 294e1e1452dSArchie Cobbs { 295e1e1452dSArchie Cobbs char name[IFNAMSIZ + 1]; 296e1e1452dSArchie Cobbs priv_p priv; 297e1e1452dSArchie Cobbs node_p node; 298e1e1452dSArchie Cobbs 299e1e1452dSArchie Cobbs /* Create node */ 3006e551fb6SDavid E. O'Brien KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__)); 301e1e1452dSArchie Cobbs snprintf(name, sizeof(name), "%s%d", ifp->if_name, ifp->if_unit); 302e1e1452dSArchie Cobbs if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 303e1e1452dSArchie Cobbs log(LOG_ERR, "%s: can't %s for %s\n", 3046e551fb6SDavid E. O'Brien __func__, "create node", name); 305e1e1452dSArchie Cobbs return; 306e1e1452dSArchie Cobbs } 307e1e1452dSArchie Cobbs 308e1e1452dSArchie Cobbs /* Allocate private data */ 30999cdf4ccSDavid Malone MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 310e1e1452dSArchie Cobbs if (priv == NULL) { 311e1e1452dSArchie Cobbs log(LOG_ERR, "%s: can't %s for %s\n", 3126e551fb6SDavid E. O'Brien __func__, "allocate memory", name); 31330400f03SJulian Elischer NG_NODE_UNREF(node); 314e1e1452dSArchie Cobbs return; 315e1e1452dSArchie Cobbs } 31630400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, priv); 317e1e1452dSArchie Cobbs priv->ifp = ifp; 318e1e1452dSArchie Cobbs IFP2NG(ifp) = node; 3194b39c3c7SArchie Cobbs priv->autoSrcAddr = 1; 320e1e1452dSArchie Cobbs 321e1e1452dSArchie Cobbs /* Try to give the node the same name as the interface */ 322e1e1452dSArchie Cobbs if (ng_name_node(node, name) != 0) { 323e1e1452dSArchie Cobbs log(LOG_WARNING, "%s: can't name node %s\n", 3246e551fb6SDavid E. O'Brien __func__, name); 325e1e1452dSArchie Cobbs } 326e1e1452dSArchie Cobbs } 327e1e1452dSArchie Cobbs 328e1e1452dSArchie Cobbs /* 329e1e1452dSArchie Cobbs * An Ethernet interface is being detached. 3301acb27c6SJulian Elischer * REALLY Destroy its node. 331e1e1452dSArchie Cobbs */ 332e1e1452dSArchie Cobbs static void 333e1e1452dSArchie Cobbs ng_ether_detach(struct ifnet *ifp) 334e1e1452dSArchie Cobbs { 335e1e1452dSArchie Cobbs const node_p node = IFP2NG(ifp); 3361acb27c6SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 337e1e1452dSArchie Cobbs 338e1e1452dSArchie Cobbs if (node == NULL) /* no node (why not?), ignore */ 339e1e1452dSArchie Cobbs return; 3401acb27c6SJulian Elischer NG_NODE_REALLY_DIE(node); /* Force real removal of node */ 3411acb27c6SJulian Elischer /* 3421acb27c6SJulian Elischer * We can't assume the ifnet is still around when we run shutdown 3431acb27c6SJulian Elischer * So zap it now. XXX We HOPE that anything running at this time 3441acb27c6SJulian Elischer * handles it (as it should in the non netgraph case). 3451acb27c6SJulian Elischer */ 3461acb27c6SJulian Elischer IFP2NG(ifp) = NULL; 3471acb27c6SJulian Elischer priv->ifp = NULL; /* XXX race if interrupted an output packet */ 3481acb27c6SJulian Elischer ng_rmnode_self(node); /* remove all netgraph parts */ 349e1e1452dSArchie Cobbs } 350e1e1452dSArchie Cobbs 351e1e1452dSArchie Cobbs /* 352e1e1452dSArchie Cobbs * Optimization for gluing the Ethernet header back onto 353e1e1452dSArchie Cobbs * the front of an incoming packet. 354e1e1452dSArchie Cobbs */ 355e1e1452dSArchie Cobbs static int 356e1e1452dSArchie Cobbs ng_ether_glueback_header(struct mbuf **mp, struct ether_header *eh) 357e1e1452dSArchie Cobbs { 358e1e1452dSArchie Cobbs struct mbuf *m = *mp; 359e1e1452dSArchie Cobbs uintfptr_t room; 360e1e1452dSArchie Cobbs int error = 0; 361e1e1452dSArchie Cobbs 362e1e1452dSArchie Cobbs /* 363e1e1452dSArchie Cobbs * Possibly the header is already on the front. 364e1e1452dSArchie Cobbs * If this is the case so just move the markers back 365e1e1452dSArchie Cobbs * to re-include it. We lucked out. 366e1e1452dSArchie Cobbs * This allows us to avoid a yucky m_pullup 367e1e1452dSArchie Cobbs * in later nodes if it works. 368e1e1452dSArchie Cobbs */ 369e1e1452dSArchie Cobbs if (eh == mtod(m, struct ether_header *) - 1) { 370e1e1452dSArchie Cobbs m->m_len += sizeof(*eh); 371e1e1452dSArchie Cobbs m->m_data -= sizeof(*eh); 372e1e1452dSArchie Cobbs m->m_pkthdr.len += sizeof(*eh); 373e1e1452dSArchie Cobbs goto done; 374e1e1452dSArchie Cobbs } 375e1e1452dSArchie Cobbs 376e1e1452dSArchie Cobbs /* 377e1e1452dSArchie Cobbs * Alternatively there may be room even though 378e1e1452dSArchie Cobbs * it is stored somewhere else. If so, copy it in. 379e1e1452dSArchie Cobbs * This only safe because we KNOW that this packet has 380e1e1452dSArchie Cobbs * just been generated by an ethernet card, so there are 381e1e1452dSArchie Cobbs * no aliases to the buffer (not so for outgoing packets). 382e1e1452dSArchie Cobbs * Nearly all ethernet cards will end up producing mbufs 383e1e1452dSArchie Cobbs * that fall into these cases. So we are not optimizing 384e1e1452dSArchie Cobbs * contorted cases. 385e1e1452dSArchie Cobbs */ 386e1e1452dSArchie Cobbs if ((m->m_flags & M_EXT) != 0) { 387e1e1452dSArchie Cobbs room = mtod(m, caddr_t) - m->m_ext.ext_buf; 388e1e1452dSArchie Cobbs if (room > m->m_ext.ext_size) /* garbage, fail immediately */ 389e1e1452dSArchie Cobbs room = 0; 390e1e1452dSArchie Cobbs } else 391e1e1452dSArchie Cobbs room = mtod(m, caddr_t) - m->m_pktdat; 392e1e1452dSArchie Cobbs 393e1e1452dSArchie Cobbs /* 394e1e1452dSArchie Cobbs * If we have room, just copy it and adjust 395e1e1452dSArchie Cobbs */ 396e1e1452dSArchie Cobbs if (room >= sizeof(*eh)) { 397e1e1452dSArchie Cobbs m->m_len += sizeof(*eh); 398e1e1452dSArchie Cobbs m->m_data -= sizeof(*eh); 399e1e1452dSArchie Cobbs m->m_pkthdr.len += sizeof(*eh); 400e1e1452dSArchie Cobbs goto copy; 401e1e1452dSArchie Cobbs } 402e1e1452dSArchie Cobbs 403e1e1452dSArchie Cobbs /* 404e1e1452dSArchie Cobbs * Doing anything more is likely to get more 405e1e1452dSArchie Cobbs * expensive than it's worth.. 406e1e1452dSArchie Cobbs * it's probable that everything else is in one 407e1e1452dSArchie Cobbs * big lump. The next node will do an m_pullup() 408e1e1452dSArchie Cobbs * for exactly the amount of data it needs and 409e1e1452dSArchie Cobbs * hopefully everything after that will not 410e1e1452dSArchie Cobbs * need one. So let's just use M_PREPEND. 411e1e1452dSArchie Cobbs */ 412e1e1452dSArchie Cobbs M_PREPEND(m, sizeof (*eh), M_DONTWAIT); 413e1e1452dSArchie Cobbs if (m == NULL) { 414e1e1452dSArchie Cobbs error = ENOBUFS; 415e1e1452dSArchie Cobbs goto done; 416e1e1452dSArchie Cobbs } 417e1e1452dSArchie Cobbs 418e1e1452dSArchie Cobbs copy: 419e1e1452dSArchie Cobbs /* Copy header and return (possibly new) mbuf */ 420e1e1452dSArchie Cobbs bcopy((caddr_t)eh, mtod(m, struct ether_header *), sizeof(*eh)); 421e1e1452dSArchie Cobbs done: 422e1e1452dSArchie Cobbs *mp = m; 423e1e1452dSArchie Cobbs return error; 424e1e1452dSArchie Cobbs } 425e1e1452dSArchie Cobbs 426e1e1452dSArchie Cobbs /****************************************************************** 427e1e1452dSArchie Cobbs NETGRAPH NODE METHODS 428e1e1452dSArchie Cobbs ******************************************************************/ 429e1e1452dSArchie Cobbs 430e1e1452dSArchie Cobbs /* 431e1e1452dSArchie Cobbs * It is not possible or allowable to create a node of this type. 432e1e1452dSArchie Cobbs * Nodes get created when the interface is attached (or, when 433e1e1452dSArchie Cobbs * this node type's KLD is loaded). 434e1e1452dSArchie Cobbs */ 435e1e1452dSArchie Cobbs static int 436069154d5SJulian Elischer ng_ether_constructor(node_p node) 437e1e1452dSArchie Cobbs { 438e1e1452dSArchie Cobbs return (EINVAL); 439e1e1452dSArchie Cobbs } 440e1e1452dSArchie Cobbs 441e1e1452dSArchie Cobbs /* 442e1e1452dSArchie Cobbs * Check for attaching a new hook. 443e1e1452dSArchie Cobbs */ 444e1e1452dSArchie Cobbs static int 445e1e1452dSArchie Cobbs ng_ether_newhook(node_p node, hook_p hook, const char *name) 446e1e1452dSArchie Cobbs { 44730400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 448e1e1452dSArchie Cobbs u_char orphan = priv->lowerOrphan; 449e1e1452dSArchie Cobbs hook_p *hookptr; 450e1e1452dSArchie Cobbs 451e1e1452dSArchie Cobbs /* Divert hook is an alias for lower */ 452e1e1452dSArchie Cobbs if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0) 453e1e1452dSArchie Cobbs name = NG_ETHER_HOOK_LOWER; 454e1e1452dSArchie Cobbs 455e1e1452dSArchie Cobbs /* Which hook? */ 456e1e1452dSArchie Cobbs if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) 457e1e1452dSArchie Cobbs hookptr = &priv->upper; 458e1e1452dSArchie Cobbs else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) { 459e1e1452dSArchie Cobbs hookptr = &priv->lower; 460e1e1452dSArchie Cobbs orphan = 0; 461e1e1452dSArchie Cobbs } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) { 462e1e1452dSArchie Cobbs hookptr = &priv->lower; 463e1e1452dSArchie Cobbs orphan = 1; 464e1e1452dSArchie Cobbs } else 465e1e1452dSArchie Cobbs return (EINVAL); 466e1e1452dSArchie Cobbs 467e1e1452dSArchie Cobbs /* Check if already connected (shouldn't be, but doesn't hurt) */ 468e1e1452dSArchie Cobbs if (*hookptr != NULL) 469e1e1452dSArchie Cobbs return (EISCONN); 470e1e1452dSArchie Cobbs 471e1e1452dSArchie Cobbs /* OK */ 472e1e1452dSArchie Cobbs *hookptr = hook; 473e1e1452dSArchie Cobbs priv->lowerOrphan = orphan; 474e1e1452dSArchie Cobbs return (0); 475e1e1452dSArchie Cobbs } 476e1e1452dSArchie Cobbs 477e1e1452dSArchie Cobbs /* 478859a4d16SJulian Elischer * Hooks are attached, adjust to force queueing. 479859a4d16SJulian Elischer * We don't really care which hook it is. 480859a4d16SJulian Elischer * they should all be queuing for outgoing data. 481859a4d16SJulian Elischer */ 482859a4d16SJulian Elischer static int 483859a4d16SJulian Elischer ng_ether_connect(hook_p hook) 484859a4d16SJulian Elischer { 48530400f03SJulian Elischer NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook)); 486859a4d16SJulian Elischer return (0); 487859a4d16SJulian Elischer } 488859a4d16SJulian Elischer 489859a4d16SJulian Elischer /* 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: 505e1e1452dSArchie Cobbs NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT); 506e1e1452dSArchie Cobbs if (resp == NULL) { 507e1e1452dSArchie Cobbs error = ENOMEM; 508e1e1452dSArchie Cobbs break; 509e1e1452dSArchie Cobbs } 510e1e1452dSArchie Cobbs snprintf(resp->data, IFNAMSIZ + 1, 511e1e1452dSArchie Cobbs "%s%d", priv->ifp->if_name, priv->ifp->if_unit); 512e1e1452dSArchie Cobbs break; 513e1e1452dSArchie Cobbs case NGM_ETHER_GET_IFINDEX: 514e1e1452dSArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 515e1e1452dSArchie Cobbs if (resp == NULL) { 516e1e1452dSArchie Cobbs error = ENOMEM; 517e1e1452dSArchie Cobbs break; 518e1e1452dSArchie Cobbs } 519e1e1452dSArchie Cobbs *((u_int32_t *)resp->data) = priv->ifp->if_index; 520e1e1452dSArchie Cobbs break; 5214b39c3c7SArchie Cobbs case NGM_ETHER_GET_ENADDR: 5224b39c3c7SArchie Cobbs NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT); 5234b39c3c7SArchie Cobbs if (resp == NULL) { 5244b39c3c7SArchie Cobbs error = ENOMEM; 5254b39c3c7SArchie Cobbs break; 5264b39c3c7SArchie Cobbs } 5274b39c3c7SArchie Cobbs bcopy((IFP2AC(priv->ifp))->ac_enaddr, 5284b39c3c7SArchie Cobbs resp->data, ETHER_ADDR_LEN); 5294b39c3c7SArchie Cobbs break; 53056045c66SArchie Cobbs case NGM_ETHER_SET_ENADDR: 53156045c66SArchie Cobbs { 53256045c66SArchie Cobbs if (msg->header.arglen != ETHER_ADDR_LEN) { 53356045c66SArchie Cobbs error = EINVAL; 53456045c66SArchie Cobbs break; 53556045c66SArchie Cobbs } 53656045c66SArchie Cobbs error = if_setlladdr(priv->ifp, 53756045c66SArchie Cobbs (u_char *)msg->data, ETHER_ADDR_LEN); 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; 579e1e1452dSArchie Cobbs default: 580e1e1452dSArchie Cobbs error = EINVAL; 581e1e1452dSArchie Cobbs break; 582e1e1452dSArchie Cobbs } 583e1e1452dSArchie Cobbs break; 584e1e1452dSArchie Cobbs default: 585e1e1452dSArchie Cobbs error = EINVAL; 586e1e1452dSArchie Cobbs break; 587e1e1452dSArchie Cobbs } 588069154d5SJulian Elischer NG_RESPOND_MSG(error, node, item, resp); 589069154d5SJulian Elischer NG_FREE_MSG(msg); 590e1e1452dSArchie Cobbs return (error); 591e1e1452dSArchie Cobbs } 592e1e1452dSArchie Cobbs 593e1e1452dSArchie Cobbs /* 594e1e1452dSArchie Cobbs * Receive data on a hook. 595e1e1452dSArchie Cobbs */ 596e1e1452dSArchie Cobbs static int 597069154d5SJulian Elischer ng_ether_rcvdata(hook_p hook, item_p item) 598e1e1452dSArchie Cobbs { 59930400f03SJulian Elischer const node_p node = NG_HOOK_NODE(hook); 60030400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 601069154d5SJulian Elischer struct mbuf *m; 602069154d5SJulian Elischer meta_p meta; 603e1e1452dSArchie Cobbs 604069154d5SJulian Elischer NGI_GET_M(item, m); 605069154d5SJulian Elischer NGI_GET_META(item, meta); 606069154d5SJulian Elischer NG_FREE_ITEM(item); 607e1e1452dSArchie Cobbs if (hook == priv->lower) 608e1e1452dSArchie Cobbs return ng_ether_rcv_lower(node, m, meta); 609e1e1452dSArchie Cobbs if (hook == priv->upper) 610e1e1452dSArchie Cobbs return ng_ether_rcv_upper(node, m, meta); 6116e551fb6SDavid E. O'Brien panic("%s: weird hook", __func__); 612b40ce416SJulian Elischer #ifdef RESTARTABLE_PANICS /* so we don;t get an error msg in LINT */ 613b40ce416SJulian Elischer return NULL; 614b40ce416SJulian Elischer #endif 615e1e1452dSArchie Cobbs } 616e1e1452dSArchie Cobbs 617e1e1452dSArchie Cobbs /* 618e1e1452dSArchie Cobbs * Handle an mbuf received on the "lower" hook. 619e1e1452dSArchie Cobbs */ 620e1e1452dSArchie Cobbs static int 621e1e1452dSArchie Cobbs ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta) 622e1e1452dSArchie Cobbs { 62330400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 624e1e1452dSArchie Cobbs 625e1e1452dSArchie Cobbs /* Make sure header is fully pulled up */ 626e1e1452dSArchie Cobbs if (m->m_pkthdr.len < sizeof(struct ether_header)) { 627069154d5SJulian Elischer NG_FREE_M(m); 628069154d5SJulian Elischer NG_FREE_META(meta); 629e1e1452dSArchie Cobbs return (EINVAL); 630e1e1452dSArchie Cobbs } 631e1e1452dSArchie Cobbs if (m->m_len < sizeof(struct ether_header) 632e1e1452dSArchie Cobbs && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) { 633e1e1452dSArchie Cobbs NG_FREE_META(meta); 634e1e1452dSArchie Cobbs return (ENOBUFS); 635e1e1452dSArchie Cobbs } 636e1e1452dSArchie Cobbs 6374b39c3c7SArchie Cobbs /* Drop in the MAC address if desired */ 6384b39c3c7SArchie Cobbs if (priv->autoSrcAddr) { 6394b39c3c7SArchie Cobbs bcopy((IFP2AC(priv->ifp))->ac_enaddr, 6404b39c3c7SArchie Cobbs mtod(m, struct ether_header *)->ether_shost, 6414b39c3c7SArchie Cobbs ETHER_ADDR_LEN); 6424b39c3c7SArchie Cobbs } 643561e4fb9SJulian Elischer 644e1e1452dSArchie Cobbs /* Send it on its way */ 645e1e1452dSArchie Cobbs NG_FREE_META(meta); 646e1e1452dSArchie Cobbs return ether_output_frame(priv->ifp, m); 647e1e1452dSArchie Cobbs } 648e1e1452dSArchie Cobbs 649e1e1452dSArchie Cobbs /* 650e1e1452dSArchie Cobbs * Handle an mbuf received on the "upper" hook. 651e1e1452dSArchie Cobbs */ 652e1e1452dSArchie Cobbs static int 653e1e1452dSArchie Cobbs ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta) 654e1e1452dSArchie Cobbs { 65530400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 656e1e1452dSArchie Cobbs struct ether_header *eh; 657e1e1452dSArchie Cobbs 658e1e1452dSArchie Cobbs /* Check length and pull off header */ 659e1e1452dSArchie Cobbs if (m->m_pkthdr.len < sizeof(*eh)) { 660069154d5SJulian Elischer NG_FREE_M(m); 661069154d5SJulian Elischer NG_FREE_META(meta); 662e1e1452dSArchie Cobbs return (EINVAL); 663e1e1452dSArchie Cobbs } 664e1e1452dSArchie Cobbs if (m->m_len < sizeof(*eh) && (m = m_pullup(m, sizeof(*eh))) == NULL) { 665e1e1452dSArchie Cobbs NG_FREE_META(meta); 666e1e1452dSArchie Cobbs return (ENOBUFS); 667e1e1452dSArchie Cobbs } 668e1e1452dSArchie Cobbs eh = mtod(m, struct ether_header *); 669e1e1452dSArchie Cobbs m->m_data += sizeof(*eh); 670e1e1452dSArchie Cobbs m->m_len -= sizeof(*eh); 671e1e1452dSArchie Cobbs m->m_pkthdr.len -= sizeof(*eh); 672a62b20c4SJulian Elischer m->m_pkthdr.rcvif = priv->ifp; 673e1e1452dSArchie Cobbs 674e1e1452dSArchie Cobbs /* Route packet back in */ 675e1e1452dSArchie Cobbs NG_FREE_META(meta); 676e1e1452dSArchie Cobbs ether_demux(priv->ifp, eh, m); 677e1e1452dSArchie Cobbs return (0); 678e1e1452dSArchie Cobbs } 679e1e1452dSArchie Cobbs 680e1e1452dSArchie Cobbs /* 6811acb27c6SJulian Elischer * Shutdown node. This resets the node but does not remove it 6821acb27c6SJulian Elischer * unless the REALLY_DIE flag is set. 683e1e1452dSArchie Cobbs */ 684e1e1452dSArchie Cobbs static int 685069154d5SJulian Elischer ng_ether_shutdown(node_p node) 686e1e1452dSArchie Cobbs { 68730400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 6884b39c3c7SArchie Cobbs 6894b39c3c7SArchie Cobbs if (priv->promisc) { /* disable promiscuous mode */ 6904b39c3c7SArchie Cobbs (void)ifpromisc(priv->ifp, 0); 6914b39c3c7SArchie Cobbs priv->promisc = 0; 6924b39c3c7SArchie Cobbs } 6931acb27c6SJulian Elischer if (node->nd_flags & NG_REALLY_DIE) { 6941acb27c6SJulian Elischer /* 6951acb27c6SJulian Elischer * WE came here because the ethernet card is being unloaded, 6961acb27c6SJulian Elischer * so stop being persistant. 6971acb27c6SJulian Elischer * Actually undo all the things we did on creation. 6981acb27c6SJulian Elischer * Assume the ifp has already been freed. 6991acb27c6SJulian Elischer */ 7001acb27c6SJulian Elischer NG_NODE_SET_PRIVATE(node, NULL); 7011acb27c6SJulian Elischer FREE(priv, M_NETGRAPH); 7021acb27c6SJulian Elischer NG_NODE_UNREF(node); /* free node itself */ 7031acb27c6SJulian Elischer return (0); 704069154d5SJulian Elischer } 7054b39c3c7SArchie Cobbs priv->autoSrcAddr = 1; /* reset auto-src-addr flag */ 7061acb27c6SJulian Elischer node->nd_flags &= ~NG_INVALID; /* Signal ng_rmnode we are persisant */ 707e1e1452dSArchie Cobbs return (0); 708e1e1452dSArchie Cobbs } 709e1e1452dSArchie Cobbs 710e1e1452dSArchie Cobbs /* 711e1e1452dSArchie Cobbs * Hook disconnection. 712e1e1452dSArchie Cobbs */ 713e1e1452dSArchie Cobbs static int 714e1e1452dSArchie Cobbs ng_ether_disconnect(hook_p hook) 715e1e1452dSArchie Cobbs { 71630400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 717e1e1452dSArchie Cobbs 718e1e1452dSArchie Cobbs if (hook == priv->upper) 719e1e1452dSArchie Cobbs priv->upper = NULL; 720e1e1452dSArchie Cobbs else if (hook == priv->lower) { 721e1e1452dSArchie Cobbs priv->lower = NULL; 722e1e1452dSArchie Cobbs priv->lowerOrphan = 0; 723e1e1452dSArchie Cobbs } else 7246e551fb6SDavid E. O'Brien panic("%s: weird hook", __func__); 72530400f03SJulian Elischer if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 72630400f03SJulian Elischer && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 72730400f03SJulian Elischer ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */ 728e1e1452dSArchie Cobbs return (0); 729e1e1452dSArchie Cobbs } 730e1e1452dSArchie Cobbs 73156045c66SArchie Cobbs static int 73256045c66SArchie Cobbs ng_enaddr_parse(const struct ng_parse_type *type, 73356045c66SArchie Cobbs const char *s, int *const off, const u_char *const start, 73456045c66SArchie Cobbs u_char *const buf, int *const buflen) 73556045c66SArchie Cobbs { 73656045c66SArchie Cobbs char *eptr; 73756045c66SArchie Cobbs u_long val; 73856045c66SArchie Cobbs int i; 73956045c66SArchie Cobbs 74056045c66SArchie Cobbs if (*buflen < ETHER_ADDR_LEN) 74156045c66SArchie Cobbs return (ERANGE); 74256045c66SArchie Cobbs for (i = 0; i < ETHER_ADDR_LEN; i++) { 74356045c66SArchie Cobbs val = strtoul(s + *off, &eptr, 16); 74456045c66SArchie Cobbs if (val > 0xff || eptr == s + *off) 74556045c66SArchie Cobbs return (EINVAL); 74656045c66SArchie Cobbs buf[i] = (u_char)val; 74756045c66SArchie Cobbs *off = (eptr - s); 74856045c66SArchie Cobbs if (i < ETHER_ADDR_LEN - 1) { 74956045c66SArchie Cobbs if (*eptr != ':') 75056045c66SArchie Cobbs return (EINVAL); 75156045c66SArchie Cobbs (*off)++; 75256045c66SArchie Cobbs } 75356045c66SArchie Cobbs } 75456045c66SArchie Cobbs *buflen = ETHER_ADDR_LEN; 75556045c66SArchie Cobbs return (0); 75656045c66SArchie Cobbs } 75756045c66SArchie Cobbs 75856045c66SArchie Cobbs static int 75956045c66SArchie Cobbs ng_enaddr_unparse(const struct ng_parse_type *type, 76056045c66SArchie Cobbs const u_char *data, int *off, char *cbuf, int cbuflen) 76156045c66SArchie Cobbs { 76256045c66SArchie Cobbs int len; 76356045c66SArchie Cobbs 76456045c66SArchie Cobbs len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x", 7657f98dc04SArchie Cobbs data[*off], data[*off + 1], data[*off + 2], 7667f98dc04SArchie Cobbs data[*off + 3], data[*off + 4], data[*off + 5]); 76756045c66SArchie Cobbs if (len >= cbuflen) 76856045c66SArchie Cobbs return (ERANGE); 76956045c66SArchie Cobbs *off += ETHER_ADDR_LEN; 77056045c66SArchie Cobbs return (0); 77156045c66SArchie Cobbs } 77256045c66SArchie Cobbs 773e1e1452dSArchie Cobbs /****************************************************************** 774e1e1452dSArchie Cobbs INITIALIZATION 775e1e1452dSArchie Cobbs ******************************************************************/ 776e1e1452dSArchie Cobbs 777e1e1452dSArchie Cobbs /* 778e1e1452dSArchie Cobbs * Handle loading and unloading for this node type. 779e1e1452dSArchie Cobbs */ 780e1e1452dSArchie Cobbs static int 781e1e1452dSArchie Cobbs ng_ether_mod_event(module_t mod, int event, void *data) 782e1e1452dSArchie Cobbs { 783e1e1452dSArchie Cobbs struct ifnet *ifp; 784e1e1452dSArchie Cobbs int error = 0; 785e1e1452dSArchie Cobbs int s; 786e1e1452dSArchie Cobbs 787e1e1452dSArchie Cobbs s = splnet(); 788e1e1452dSArchie Cobbs switch (event) { 789e1e1452dSArchie Cobbs case MOD_LOAD: 790e1e1452dSArchie Cobbs 791e1e1452dSArchie Cobbs /* Register function hooks */ 792e1e1452dSArchie Cobbs if (ng_ether_attach_p != NULL) { 793e1e1452dSArchie Cobbs error = EEXIST; 794e1e1452dSArchie Cobbs break; 795e1e1452dSArchie Cobbs } 796e1e1452dSArchie Cobbs ng_ether_attach_p = ng_ether_attach; 797e1e1452dSArchie Cobbs ng_ether_detach_p = ng_ether_detach; 798e1e1452dSArchie Cobbs ng_ether_output_p = ng_ether_output; 799e1e1452dSArchie Cobbs ng_ether_input_p = ng_ether_input; 800e1e1452dSArchie Cobbs ng_ether_input_orphan_p = ng_ether_input_orphan; 801e1e1452dSArchie Cobbs 802e1e1452dSArchie Cobbs /* Create nodes for any already-existing Ethernet interfaces */ 803e1e1452dSArchie Cobbs TAILQ_FOREACH(ifp, &ifnet, if_link) { 804cf2010b8SArchie Cobbs if (ifp->if_type == IFT_ETHER 805cf2010b8SArchie Cobbs || ifp->if_type == IFT_L2VLAN) 806e1e1452dSArchie Cobbs ng_ether_attach(ifp); 807e1e1452dSArchie Cobbs } 808e1e1452dSArchie Cobbs break; 809e1e1452dSArchie Cobbs 810e1e1452dSArchie Cobbs case MOD_UNLOAD: 811e1e1452dSArchie Cobbs 812e1e1452dSArchie Cobbs /* 813e1e1452dSArchie Cobbs * Note that the base code won't try to unload us until 814e1e1452dSArchie Cobbs * all nodes have been removed, and that can't happen 815e1e1452dSArchie Cobbs * until all Ethernet interfaces are removed. In any 816e1e1452dSArchie Cobbs * case, we know there are no nodes left if the action 817e1e1452dSArchie Cobbs * is MOD_UNLOAD, so there's no need to detach any nodes. 818e1e1452dSArchie Cobbs */ 819e1e1452dSArchie Cobbs 820e1e1452dSArchie Cobbs /* Unregister function hooks */ 821e1e1452dSArchie Cobbs ng_ether_attach_p = NULL; 822e1e1452dSArchie Cobbs ng_ether_detach_p = NULL; 823e1e1452dSArchie Cobbs ng_ether_output_p = NULL; 824e1e1452dSArchie Cobbs ng_ether_input_p = NULL; 825e1e1452dSArchie Cobbs ng_ether_input_orphan_p = NULL; 826e1e1452dSArchie Cobbs break; 827e1e1452dSArchie Cobbs 828e1e1452dSArchie Cobbs default: 829e1e1452dSArchie Cobbs error = EOPNOTSUPP; 830e1e1452dSArchie Cobbs break; 831e1e1452dSArchie Cobbs } 832e1e1452dSArchie Cobbs splx(s); 833e1e1452dSArchie Cobbs return (error); 834e1e1452dSArchie Cobbs } 835e1e1452dSArchie Cobbs 836