ng_ether.c revision 184205
162143Sarchie 262143Sarchie/* 362143Sarchie * ng_ether.c 4139823Simp */ 5139823Simp 6139823Simp/*- 762143Sarchie * Copyright (c) 1996-2000 Whistle Communications, Inc. 862143Sarchie * All rights reserved. 962143Sarchie * 1062143Sarchie * Subject to the following obligations and disclaimer of warranty, use and 1162143Sarchie * redistribution of this software, in source or object code forms, with or 1262143Sarchie * without modifications are expressly permitted by Whistle Communications; 1362143Sarchie * provided, however, that: 1462143Sarchie * 1. Any and all reproductions of the source or object code must include the 1562143Sarchie * copyright notice above and the following disclaimer of warranties; and 1662143Sarchie * 2. No rights are granted, in any manner or form, to use Whistle 1762143Sarchie * Communications, Inc. trademarks, including the mark "WHISTLE 1862143Sarchie * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 1962143Sarchie * such appears in the above copyright notice or in the software. 2062143Sarchie * 2162143Sarchie * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 2262143Sarchie * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 2362143Sarchie * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 2462143Sarchie * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 2562143Sarchie * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 2662143Sarchie * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 2762143Sarchie * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 2862143Sarchie * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 2962143Sarchie * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 3062143Sarchie * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 3162143Sarchie * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 3262143Sarchie * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 3362143Sarchie * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 3462143Sarchie * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3562143Sarchie * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3662143Sarchie * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 3762143Sarchie * OF SUCH DAMAGE. 3862143Sarchie * 3962143Sarchie * Authors: Archie Cobbs <archie@freebsd.org> 4062143Sarchie * Julian Elischer <julian@freebsd.org> 4162143Sarchie * 4262143Sarchie * $FreeBSD: head/sys/netgraph/ng_ether.c 184205 2008-10-23 15:53:51Z des $ 4362143Sarchie */ 4462143Sarchie 4562143Sarchie/* 4662143Sarchie * ng_ether(4) netgraph node type 4762143Sarchie */ 4862143Sarchie 4962143Sarchie#include <sys/param.h> 5062143Sarchie#include <sys/systm.h> 5162143Sarchie#include <sys/kernel.h> 5262143Sarchie#include <sys/malloc.h> 5362143Sarchie#include <sys/mbuf.h> 5462143Sarchie#include <sys/errno.h> 5562143Sarchie#include <sys/syslog.h> 5662143Sarchie#include <sys/socket.h> 57181803Sbz#include <sys/vimage.h> 5862143Sarchie 5962143Sarchie#include <net/if.h> 60141721Sglebius#include <net/if_dl.h> 6162143Sarchie#include <net/if_types.h> 6262143Sarchie#include <net/if_arp.h> 6362143Sarchie#include <net/if_var.h> 6462143Sarchie#include <net/ethernet.h> 65151305Sthompsa#include <net/if_bridgevar.h> 6662143Sarchie 6762143Sarchie#include <netgraph/ng_message.h> 6862143Sarchie#include <netgraph/netgraph.h> 6962143Sarchie#include <netgraph/ng_parse.h> 7062143Sarchie#include <netgraph/ng_ether.h> 7162143Sarchie 72152243Sru#define IFP2NG(ifp) (IFP2AC((ifp))->ac_netgraph) 7362143Sarchie 74126035Spjd/* Per-node private data */ 75126035Spjdstruct private { 76126035Spjd struct ifnet *ifp; /* associated interface */ 77126035Spjd hook_p upper; /* upper hook connection */ 78129281Sarchie hook_p lower; /* lower hook connection */ 79129281Sarchie hook_p orphan; /* orphan hook connection */ 80126035Spjd u_char autoSrcAddr; /* always overwrite source address */ 81126035Spjd u_char promisc; /* promiscuous mode enabled */ 82126035Spjd u_long hwassist; /* hardware checksum capabilities */ 83126035Spjd u_int flags; /* flags e.g. really die */ 84126035Spjd}; 85126035Spjdtypedef struct private *priv_p; 8662143Sarchie 87106933Ssam/* Hook pointers used by if_ethersubr.c to callback to netgraph */ 88106933Ssamextern void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 89106933Ssamextern void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 90106933Ssamextern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 91106933Ssamextern void (*ng_ether_attach_p)(struct ifnet *ifp); 92106933Ssamextern void (*ng_ether_detach_p)(struct ifnet *ifp); 93139903Sglebiusextern void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 94106933Ssam 9562143Sarchie/* Functional hooks called from if_ethersubr.c */ 96106933Ssamstatic void ng_ether_input(struct ifnet *ifp, struct mbuf **mp); 97106933Ssamstatic void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m); 9862143Sarchiestatic int ng_ether_output(struct ifnet *ifp, struct mbuf **mp); 9962143Sarchiestatic void ng_ether_attach(struct ifnet *ifp); 10062143Sarchiestatic void ng_ether_detach(struct ifnet *ifp); 101139903Sglebiusstatic void ng_ether_link_state(struct ifnet *ifp, int state); 10262143Sarchie 10362143Sarchie/* Other functions */ 104131155Sjulianstatic int ng_ether_rcv_lower(node_p node, struct mbuf *m); 105131155Sjulianstatic int ng_ether_rcv_upper(node_p node, struct mbuf *m); 10662143Sarchie 10762143Sarchie/* Netgraph node methods */ 10862143Sarchiestatic ng_constructor_t ng_ether_constructor; 10962143Sarchiestatic ng_rcvmsg_t ng_ether_rcvmsg; 11070700Sjulianstatic ng_shutdown_t ng_ether_shutdown; 11162143Sarchiestatic ng_newhook_t ng_ether_newhook; 11262143Sarchiestatic ng_rcvdata_t ng_ether_rcvdata; 11362143Sarchiestatic ng_disconnect_t ng_ether_disconnect; 11462143Sarchiestatic int ng_ether_mod_event(module_t mod, int event, void *data); 11562143Sarchie 11662143Sarchie/* List of commands and how to convert arguments to/from ASCII */ 11762143Sarchiestatic const struct ng_cmdlist ng_ether_cmdlist[] = { 11862143Sarchie { 11962143Sarchie NGM_ETHER_COOKIE, 12062143Sarchie NGM_ETHER_GET_IFNAME, 12162143Sarchie "getifname", 12262143Sarchie NULL, 12362143Sarchie &ng_parse_string_type 12462143Sarchie }, 12562143Sarchie { 12662143Sarchie NGM_ETHER_COOKIE, 12762143Sarchie NGM_ETHER_GET_IFINDEX, 12862143Sarchie "getifindex", 12962143Sarchie NULL, 13062143Sarchie &ng_parse_int32_type 13162143Sarchie }, 13264358Sarchie { 13364358Sarchie NGM_ETHER_COOKIE, 13464358Sarchie NGM_ETHER_GET_ENADDR, 13564358Sarchie "getenaddr", 13664358Sarchie NULL, 137123600Sru &ng_parse_enaddr_type 13864358Sarchie }, 13964358Sarchie { 14064358Sarchie NGM_ETHER_COOKIE, 14164653Sarchie NGM_ETHER_SET_ENADDR, 14264653Sarchie "setenaddr", 143123600Sru &ng_parse_enaddr_type, 14464653Sarchie NULL 14564653Sarchie }, 14664653Sarchie { 14764653Sarchie NGM_ETHER_COOKIE, 14864653Sarchie NGM_ETHER_GET_PROMISC, 14964653Sarchie "getpromisc", 15064653Sarchie NULL, 15164653Sarchie &ng_parse_int32_type 15264653Sarchie }, 15364653Sarchie { 15464653Sarchie NGM_ETHER_COOKIE, 15564358Sarchie NGM_ETHER_SET_PROMISC, 15664358Sarchie "setpromisc", 15764358Sarchie &ng_parse_int32_type, 15864358Sarchie NULL 15964358Sarchie }, 16064358Sarchie { 16164358Sarchie NGM_ETHER_COOKIE, 16264653Sarchie NGM_ETHER_GET_AUTOSRC, 16364653Sarchie "getautosrc", 16464653Sarchie NULL, 16564653Sarchie &ng_parse_int32_type 16664653Sarchie }, 16764653Sarchie { 16864653Sarchie NGM_ETHER_COOKIE, 16964358Sarchie NGM_ETHER_SET_AUTOSRC, 17064358Sarchie "setautosrc", 17164358Sarchie &ng_parse_int32_type, 17264358Sarchie NULL 17364358Sarchie }, 174141721Sglebius { 175141721Sglebius NGM_ETHER_COOKIE, 176141721Sglebius NGM_ETHER_ADD_MULTI, 177141721Sglebius "addmulti", 178141721Sglebius &ng_parse_enaddr_type, 179141721Sglebius NULL 180141721Sglebius }, 181141721Sglebius { 182141721Sglebius NGM_ETHER_COOKIE, 183141721Sglebius NGM_ETHER_DEL_MULTI, 184141721Sglebius "delmulti", 185141721Sglebius &ng_parse_enaddr_type, 186141721Sglebius NULL 187141721Sglebius }, 188141910Sglebius { 189141910Sglebius NGM_ETHER_COOKIE, 190141910Sglebius NGM_ETHER_DETACH, 191141910Sglebius "detach", 192141910Sglebius NULL, 193141910Sglebius NULL 194141910Sglebius }, 19562143Sarchie { 0 } 19662143Sarchie}; 19762143Sarchie 19862143Sarchiestatic struct ng_type ng_ether_typestruct = { 199129823Sjulian .version = NG_ABI_VERSION, 200129823Sjulian .name = NG_ETHER_NODE_TYPE, 201129823Sjulian .mod_event = ng_ether_mod_event, 202129823Sjulian .constructor = ng_ether_constructor, 203129823Sjulian .rcvmsg = ng_ether_rcvmsg, 204129823Sjulian .shutdown = ng_ether_shutdown, 205129823Sjulian .newhook = ng_ether_newhook, 206129823Sjulian .rcvdata = ng_ether_rcvdata, 207129823Sjulian .disconnect = ng_ether_disconnect, 208129823Sjulian .cmdlist = ng_ether_cmdlist, 20962143Sarchie}; 21062143SarchieNETGRAPH_INIT(ether, &ng_ether_typestruct); 21162143Sarchie 21262143Sarchie/****************************************************************** 21362143Sarchie ETHERNET FUNCTION HOOKS 21462143Sarchie******************************************************************/ 21562143Sarchie 21662143Sarchie/* 21762143Sarchie * Handle a packet that has come in on an interface. We get to 21862143Sarchie * look at it here before any upper layer protocols do. 21962143Sarchie * 22062143Sarchie * NOTE: this function will get called at splimp() 22162143Sarchie */ 22262143Sarchiestatic void 223106933Ssamng_ether_input(struct ifnet *ifp, struct mbuf **mp) 22462143Sarchie{ 22562143Sarchie const node_p node = IFP2NG(ifp); 22670784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 227129281Sarchie int error; 22862143Sarchie 22962143Sarchie /* If "lower" hook not connected, let packet continue */ 230129281Sarchie if (priv->lower == NULL) 23162143Sarchie return; 232129281Sarchie NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */ 23362143Sarchie} 23462143Sarchie 23562143Sarchie/* 23662143Sarchie * Handle a packet that has come in on an interface, and which 23762143Sarchie * does not match any of our known protocols (an ``orphan''). 23862143Sarchie * 23962143Sarchie * NOTE: this function will get called at splimp() 24062143Sarchie */ 24162143Sarchiestatic void 242106933Ssamng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m) 24362143Sarchie{ 24462143Sarchie const node_p node = IFP2NG(ifp); 24570784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 246129281Sarchie int error; 24762143Sarchie 248129281Sarchie /* If "orphan" hook not connected, discard packet */ 249129281Sarchie if (priv->orphan == NULL) { 25062143Sarchie m_freem(m); 25162143Sarchie return; 25262143Sarchie } 253129281Sarchie NG_SEND_DATA_ONLY(error, priv->orphan, m); 25462143Sarchie} 25562143Sarchie 25662143Sarchie/* 25762143Sarchie * Handle a packet that is going out on an interface. 25862143Sarchie * The Ethernet header is already attached to the mbuf. 25962143Sarchie */ 26062143Sarchiestatic int 26162143Sarchieng_ether_output(struct ifnet *ifp, struct mbuf **mp) 26262143Sarchie{ 26362143Sarchie const node_p node = IFP2NG(ifp); 26470784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 26562143Sarchie int error = 0; 26662143Sarchie 26762143Sarchie /* If "upper" hook not connected, let packet continue */ 26862143Sarchie if (priv->upper == NULL) 26962143Sarchie return (0); 27062143Sarchie 27162143Sarchie /* Send it out "upper" hook */ 27270700Sjulian NG_SEND_DATA_ONLY(error, priv->upper, *mp); 27362143Sarchie return (error); 27462143Sarchie} 27562143Sarchie 27662143Sarchie/* 27762143Sarchie * A new Ethernet interface has been attached. 27862143Sarchie * Create a new node for it, etc. 27962143Sarchie */ 28062143Sarchiestatic void 28162143Sarchieng_ether_attach(struct ifnet *ifp) 28262143Sarchie{ 28362143Sarchie priv_p priv; 28462143Sarchie node_p node; 28562143Sarchie 28662143Sarchie /* Create node */ 28787599Sobrien KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__)); 28862143Sarchie if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 28962143Sarchie log(LOG_ERR, "%s: can't %s for %s\n", 290121816Sbrooks __func__, "create node", ifp->if_xname); 29162143Sarchie return; 29262143Sarchie } 29362143Sarchie 29462143Sarchie /* Allocate private data */ 295184205Sdes priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 29662143Sarchie if (priv == NULL) { 29762143Sarchie log(LOG_ERR, "%s: can't %s for %s\n", 298121816Sbrooks __func__, "allocate memory", ifp->if_xname); 29970784Sjulian NG_NODE_UNREF(node); 30062143Sarchie return; 30162143Sarchie } 30270784Sjulian NG_NODE_SET_PRIVATE(node, priv); 30362143Sarchie priv->ifp = ifp; 304152243Sru IFP2NG(ifp) = node; 30590249Sarchie priv->hwassist = ifp->if_hwassist; 30662143Sarchie 30762143Sarchie /* Try to give the node the same name as the interface */ 308121816Sbrooks if (ng_name_node(node, ifp->if_xname) != 0) { 30962143Sarchie log(LOG_WARNING, "%s: can't name node %s\n", 310121816Sbrooks __func__, ifp->if_xname); 31162143Sarchie } 31262143Sarchie} 31362143Sarchie 31462143Sarchie/* 31562143Sarchie * An Ethernet interface is being detached. 31671849Sjulian * REALLY Destroy its node. 31762143Sarchie */ 31862143Sarchiestatic void 31962143Sarchieng_ether_detach(struct ifnet *ifp) 32062143Sarchie{ 32162143Sarchie const node_p node = IFP2NG(ifp); 32271849Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 32362143Sarchie 32471849Sjulian NG_NODE_REALLY_DIE(node); /* Force real removal of node */ 32571849Sjulian /* 32671849Sjulian * We can't assume the ifnet is still around when we run shutdown 32771849Sjulian * So zap it now. XXX We HOPE that anything running at this time 32871849Sjulian * handles it (as it should in the non netgraph case). 32971849Sjulian */ 330152243Sru IFP2NG(ifp) = NULL; 33171849Sjulian priv->ifp = NULL; /* XXX race if interrupted an output packet */ 33271849Sjulian ng_rmnode_self(node); /* remove all netgraph parts */ 33362143Sarchie} 33462143Sarchie 335139903Sglebius/* 336139903Sglebius * Notify graph about link event. 337139903Sglebius * if_link_state_change() has already checked that the state has changed. 338139903Sglebius */ 339139903Sglebiusstatic void 340139903Sglebiusng_ether_link_state(struct ifnet *ifp, int state) 341139903Sglebius{ 342139903Sglebius const node_p node = IFP2NG(ifp); 343139903Sglebius const priv_p priv = NG_NODE_PRIVATE(node); 344139903Sglebius struct ng_mesg *msg; 345139903Sglebius int cmd, dummy_error = 0; 346139903Sglebius 347139903Sglebius if (priv->lower == NULL) 348139903Sglebius return; 349139903Sglebius 350139903Sglebius if (state == LINK_STATE_UP) 351139903Sglebius cmd = NGM_LINK_IS_UP; 352139903Sglebius else if (state == LINK_STATE_DOWN) 353139903Sglebius cmd = NGM_LINK_IS_DOWN; 354139903Sglebius else 355139903Sglebius return; 356139903Sglebius 357139903Sglebius NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT); 358139903Sglebius if (msg != NULL) 359139903Sglebius NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0); 360139903Sglebius} 361139903Sglebius 36262143Sarchie/****************************************************************** 36362143Sarchie NETGRAPH NODE METHODS 36462143Sarchie******************************************************************/ 36562143Sarchie 36662143Sarchie/* 36762143Sarchie * It is not possible or allowable to create a node of this type. 36862143Sarchie * Nodes get created when the interface is attached (or, when 36962143Sarchie * this node type's KLD is loaded). 37062143Sarchie */ 37162143Sarchiestatic int 37270700Sjulianng_ether_constructor(node_p node) 37362143Sarchie{ 37462143Sarchie return (EINVAL); 37562143Sarchie} 37662143Sarchie 37762143Sarchie/* 37862143Sarchie * Check for attaching a new hook. 37962143Sarchie */ 38062143Sarchiestatic int 38162143Sarchieng_ether_newhook(node_p node, hook_p hook, const char *name) 38262143Sarchie{ 38370784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 38462143Sarchie hook_p *hookptr; 38562143Sarchie 38662143Sarchie /* Divert hook is an alias for lower */ 38762143Sarchie if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0) 38862143Sarchie name = NG_ETHER_HOOK_LOWER; 38962143Sarchie 39062143Sarchie /* Which hook? */ 39162143Sarchie if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) 39262143Sarchie hookptr = &priv->upper; 393129281Sarchie else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) 39462143Sarchie hookptr = &priv->lower; 395129281Sarchie else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) 396129281Sarchie hookptr = &priv->orphan; 397129281Sarchie else 39862143Sarchie return (EINVAL); 39962143Sarchie 40062143Sarchie /* Check if already connected (shouldn't be, but doesn't hurt) */ 40162143Sarchie if (*hookptr != NULL) 40262143Sarchie return (EISCONN); 40362143Sarchie 40490249Sarchie /* Disable hardware checksums while 'upper' hook is connected */ 40590249Sarchie if (hookptr == &priv->upper) 40690249Sarchie priv->ifp->if_hwassist = 0; 40790249Sarchie 40862143Sarchie /* OK */ 40962143Sarchie *hookptr = hook; 41062143Sarchie return (0); 41162143Sarchie} 41262143Sarchie 41362143Sarchie/* 41462143Sarchie * Receive an incoming control message. 41562143Sarchie */ 41662143Sarchiestatic int 41770700Sjulianng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook) 41862143Sarchie{ 41970784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 42062143Sarchie struct ng_mesg *resp = NULL; 42162143Sarchie int error = 0; 42270700Sjulian struct ng_mesg *msg; 42362143Sarchie 42470700Sjulian NGI_GET_MSG(item, msg); 42562143Sarchie switch (msg->header.typecookie) { 42662143Sarchie case NGM_ETHER_COOKIE: 42762143Sarchie switch (msg->header.cmd) { 42862143Sarchie case NGM_ETHER_GET_IFNAME: 429141195Sru NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 43062143Sarchie if (resp == NULL) { 43162143Sarchie error = ENOMEM; 43262143Sarchie break; 43362143Sarchie } 434141195Sru strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ); 43562143Sarchie break; 43662143Sarchie case NGM_ETHER_GET_IFINDEX: 43762143Sarchie NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 43862143Sarchie if (resp == NULL) { 43962143Sarchie error = ENOMEM; 44062143Sarchie break; 44162143Sarchie } 44262143Sarchie *((u_int32_t *)resp->data) = priv->ifp->if_index; 44362143Sarchie break; 44464358Sarchie case NGM_ETHER_GET_ENADDR: 44564358Sarchie NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT); 44664358Sarchie if (resp == NULL) { 44764358Sarchie error = ENOMEM; 44864358Sarchie break; 44964358Sarchie } 450152315Sru bcopy(IF_LLADDR(priv->ifp), 45164358Sarchie resp->data, ETHER_ADDR_LEN); 45264358Sarchie break; 45364653Sarchie case NGM_ETHER_SET_ENADDR: 45464653Sarchie { 45564653Sarchie if (msg->header.arglen != ETHER_ADDR_LEN) { 45664653Sarchie error = EINVAL; 45764653Sarchie break; 45864653Sarchie } 45964653Sarchie error = if_setlladdr(priv->ifp, 46064653Sarchie (u_char *)msg->data, ETHER_ADDR_LEN); 46164653Sarchie break; 46264653Sarchie } 46364653Sarchie case NGM_ETHER_GET_PROMISC: 46464653Sarchie NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 46564653Sarchie if (resp == NULL) { 46664653Sarchie error = ENOMEM; 46764653Sarchie break; 46864653Sarchie } 46964653Sarchie *((u_int32_t *)resp->data) = priv->promisc; 47064653Sarchie break; 47164358Sarchie case NGM_ETHER_SET_PROMISC: 47264358Sarchie { 47364358Sarchie u_char want; 47464358Sarchie 47564358Sarchie if (msg->header.arglen != sizeof(u_int32_t)) { 47664358Sarchie error = EINVAL; 47764358Sarchie break; 47864358Sarchie } 47964358Sarchie want = !!*((u_int32_t *)msg->data); 48064358Sarchie if (want ^ priv->promisc) { 48164358Sarchie if ((error = ifpromisc(priv->ifp, want)) != 0) 48264358Sarchie break; 48364358Sarchie priv->promisc = want; 48464358Sarchie } 48564358Sarchie break; 48664358Sarchie } 48764653Sarchie case NGM_ETHER_GET_AUTOSRC: 48864653Sarchie NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 48964653Sarchie if (resp == NULL) { 49064653Sarchie error = ENOMEM; 49164653Sarchie break; 49264653Sarchie } 49364653Sarchie *((u_int32_t *)resp->data) = priv->autoSrcAddr; 49464653Sarchie break; 49564358Sarchie case NGM_ETHER_SET_AUTOSRC: 49664358Sarchie if (msg->header.arglen != sizeof(u_int32_t)) { 49764358Sarchie error = EINVAL; 49864358Sarchie break; 49964358Sarchie } 50064358Sarchie priv->autoSrcAddr = !!*((u_int32_t *)msg->data); 50164358Sarchie break; 502141721Sglebius case NGM_ETHER_ADD_MULTI: 503141721Sglebius { 504141721Sglebius struct sockaddr_dl sa_dl; 505167729Sbms struct ifmultiaddr *ifma; 506141721Sglebius 507141721Sglebius if (msg->header.arglen != ETHER_ADDR_LEN) { 508141721Sglebius error = EINVAL; 509141721Sglebius break; 510141721Sglebius } 511141755Sglebius bzero(&sa_dl, sizeof(struct sockaddr_dl)); 512141721Sglebius sa_dl.sdl_len = sizeof(struct sockaddr_dl); 513141721Sglebius sa_dl.sdl_family = AF_LINK; 514141755Sglebius sa_dl.sdl_alen = ETHER_ADDR_LEN; 515141721Sglebius bcopy((void *)msg->data, LLADDR(&sa_dl), 516141721Sglebius ETHER_ADDR_LEN); 517167729Sbms /* 518167729Sbms * Netgraph is only permitted to join groups once 519167729Sbms * via the if_addmulti() KPI, because it cannot hold 520167729Sbms * struct ifmultiaddr * between calls. It may also 521167729Sbms * lose a race while we check if the membership 522167729Sbms * already exists. 523167729Sbms */ 524167729Sbms IF_ADDR_LOCK(priv->ifp); 525167729Sbms ifma = if_findmulti(priv->ifp, 526167729Sbms (struct sockaddr *)&sa_dl); 527167729Sbms IF_ADDR_UNLOCK(priv->ifp); 528167729Sbms if (ifma != NULL) { 529167729Sbms error = EADDRINUSE; 530167729Sbms } else { 531167729Sbms error = if_addmulti(priv->ifp, 532167729Sbms (struct sockaddr *)&sa_dl, &ifma); 533167729Sbms } 534141721Sglebius break; 535141721Sglebius } 536141721Sglebius case NGM_ETHER_DEL_MULTI: 537141721Sglebius { 538141721Sglebius struct sockaddr_dl sa_dl; 539141721Sglebius 540141721Sglebius if (msg->header.arglen != ETHER_ADDR_LEN) { 541141721Sglebius error = EINVAL; 542141721Sglebius break; 543141721Sglebius } 544141755Sglebius bzero(&sa_dl, sizeof(struct sockaddr_dl)); 545141721Sglebius sa_dl.sdl_len = sizeof(struct sockaddr_dl); 546141721Sglebius sa_dl.sdl_family = AF_LINK; 547141755Sglebius sa_dl.sdl_alen = ETHER_ADDR_LEN; 548141721Sglebius bcopy((void *)msg->data, LLADDR(&sa_dl), 549141721Sglebius ETHER_ADDR_LEN); 550141721Sglebius error = if_delmulti(priv->ifp, 551141721Sglebius (struct sockaddr *)&sa_dl); 552141721Sglebius break; 553141721Sglebius } 554141910Sglebius case NGM_ETHER_DETACH: 555141910Sglebius ng_ether_detach(priv->ifp); 556141910Sglebius break; 55762143Sarchie default: 55862143Sarchie error = EINVAL; 55962143Sarchie break; 56062143Sarchie } 56162143Sarchie break; 56262143Sarchie default: 56362143Sarchie error = EINVAL; 56462143Sarchie break; 56562143Sarchie } 56670700Sjulian NG_RESPOND_MSG(error, node, item, resp); 56770700Sjulian NG_FREE_MSG(msg); 56862143Sarchie return (error); 56962143Sarchie} 57062143Sarchie 57162143Sarchie/* 57262143Sarchie * Receive data on a hook. 57362143Sarchie */ 57462143Sarchiestatic int 57570700Sjulianng_ether_rcvdata(hook_p hook, item_p item) 57662143Sarchie{ 57770784Sjulian const node_p node = NG_HOOK_NODE(hook); 57870784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 57970700Sjulian struct mbuf *m; 58062143Sarchie 58170700Sjulian NGI_GET_M(item, m); 58270700Sjulian NG_FREE_ITEM(item); 583131155Sjulian 584129281Sarchie if (hook == priv->lower || hook == priv->orphan) 585131155Sjulian return ng_ether_rcv_lower(node, m); 58662143Sarchie if (hook == priv->upper) 587131155Sjulian return ng_ether_rcv_upper(node, m); 58887599Sobrien panic("%s: weird hook", __func__); 589129281Sarchie#ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */ 590136312Sdes return (0); 59183366Sjulian#endif 59262143Sarchie} 59362143Sarchie 59462143Sarchie/* 595129281Sarchie * Handle an mbuf received on the "lower" or "orphan" hook. 59662143Sarchie */ 59762143Sarchiestatic int 598131155Sjulianng_ether_rcv_lower(node_p node, struct mbuf *m) 59962143Sarchie{ 60070784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 60196265Sarchie struct ifnet *const ifp = priv->ifp; 60262143Sarchie 60396265Sarchie /* Check whether interface is ready for packets */ 604148887Srwatson if (!((ifp->if_flags & IFF_UP) && 605148887Srwatson (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 60696265Sarchie NG_FREE_M(m); 60796265Sarchie return (ENETDOWN); 60896265Sarchie } 60996265Sarchie 61062143Sarchie /* Make sure header is fully pulled up */ 61162143Sarchie if (m->m_pkthdr.len < sizeof(struct ether_header)) { 61270700Sjulian NG_FREE_M(m); 61362143Sarchie return (EINVAL); 61462143Sarchie } 61562143Sarchie if (m->m_len < sizeof(struct ether_header) 61697896Sarchie && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 61762143Sarchie return (ENOBUFS); 61862143Sarchie 61964358Sarchie /* Drop in the MAC address if desired */ 62064358Sarchie if (priv->autoSrcAddr) { 62197896Sarchie 62297896Sarchie /* Make the mbuf writable if it's not already */ 62397896Sarchie if (!M_WRITABLE(m) 62497896Sarchie && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 62597896Sarchie return (ENOBUFS); 62697896Sarchie 62797896Sarchie /* Overwrite source MAC address */ 628152315Sru bcopy(IF_LLADDR(ifp), 62964358Sarchie mtod(m, struct ether_header *)->ether_shost, 63064358Sarchie ETHER_ADDR_LEN); 63164358Sarchie } 63262678Sjulian 63362143Sarchie /* Send it on its way */ 63496265Sarchie return ether_output_frame(ifp, m); 63562143Sarchie} 63662143Sarchie 63762143Sarchie/* 63862143Sarchie * Handle an mbuf received on the "upper" hook. 63962143Sarchie */ 64062143Sarchiestatic int 641131155Sjulianng_ether_rcv_upper(node_p node, struct mbuf *m) 64262143Sarchie{ 64370784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 644151063Sglebius struct ifnet *ifp = priv->ifp; 64562143Sarchie 646152001Sru /* Check length and pull off header */ 647152001Sru if (m->m_pkthdr.len < sizeof(struct ether_header)) { 648152001Sru NG_FREE_M(m); 649152001Sru return (EINVAL); 650152001Sru } 651152001Sru if (m->m_len < sizeof(struct ether_header) && 652152001Sru (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 653152001Sru return (ENOBUFS); 654152001Sru 655151063Sglebius m->m_pkthdr.rcvif = ifp; 65662143Sarchie 657151305Sthompsa /* Pass the packet to the bridge, it may come back to us */ 658151063Sglebius if (ifp->if_bridge) { 659151305Sthompsa BRIDGE_INPUT(ifp, m); 660151063Sglebius if (m == NULL) 661151063Sglebius return (0); 662151063Sglebius } 663151063Sglebius 66462143Sarchie /* Route packet back in */ 665151305Sthompsa ether_demux(ifp, m); 66662143Sarchie return (0); 66762143Sarchie} 66862143Sarchie 66962143Sarchie/* 67071849Sjulian * Shutdown node. This resets the node but does not remove it 67171849Sjulian * unless the REALLY_DIE flag is set. 67262143Sarchie */ 67362143Sarchiestatic int 67470700Sjulianng_ether_shutdown(node_p node) 67562143Sarchie{ 67670784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 67764358Sarchie 678132464Sjulian if (node->nd_flags & NGF_REALLY_DIE) { 67971849Sjulian /* 68071849Sjulian * WE came here because the ethernet card is being unloaded, 68171849Sjulian * so stop being persistant. 68271849Sjulian * Actually undo all the things we did on creation. 68371849Sjulian * Assume the ifp has already been freed. 68471849Sjulian */ 68571849Sjulian NG_NODE_SET_PRIVATE(node, NULL); 686184205Sdes free(priv, M_NETGRAPH); 68771849Sjulian NG_NODE_UNREF(node); /* free node itself */ 68871849Sjulian return (0); 68970700Sjulian } 690124269Sgreen if (priv->promisc) { /* disable promiscuous mode */ 691124269Sgreen (void)ifpromisc(priv->ifp, 0); 692124269Sgreen priv->promisc = 0; 693124269Sgreen } 69464358Sarchie priv->autoSrcAddr = 1; /* reset auto-src-addr flag */ 695132464Sjulian NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */ 696132464Sjulian 69762143Sarchie return (0); 69862143Sarchie} 69962143Sarchie 70062143Sarchie/* 70162143Sarchie * Hook disconnection. 70262143Sarchie */ 70362143Sarchiestatic int 70462143Sarchieng_ether_disconnect(hook_p hook) 70562143Sarchie{ 70670784Sjulian const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 70762143Sarchie 70890249Sarchie if (hook == priv->upper) { 70962143Sarchie priv->upper = NULL; 710124270Sgreen if (priv->ifp != NULL) /* restore h/w csum */ 711124270Sgreen priv->ifp->if_hwassist = priv->hwassist; 712129281Sarchie } else if (hook == priv->lower) 71362143Sarchie priv->lower = NULL; 714129281Sarchie else if (hook == priv->orphan) 715129281Sarchie priv->orphan = NULL; 716129281Sarchie else 71787599Sobrien panic("%s: weird hook", __func__); 71870784Sjulian if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 71970784Sjulian && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 72070784Sjulian ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */ 72162143Sarchie return (0); 72262143Sarchie} 72362143Sarchie 72462143Sarchie/****************************************************************** 72562143Sarchie INITIALIZATION 72662143Sarchie******************************************************************/ 72762143Sarchie 72862143Sarchie/* 72962143Sarchie * Handle loading and unloading for this node type. 73062143Sarchie */ 73162143Sarchiestatic int 73262143Sarchieng_ether_mod_event(module_t mod, int event, void *data) 73362143Sarchie{ 73462143Sarchie struct ifnet *ifp; 73562143Sarchie int error = 0; 73662143Sarchie int s; 73762143Sarchie 73862143Sarchie s = splnet(); 73962143Sarchie switch (event) { 74062143Sarchie case MOD_LOAD: 74162143Sarchie 74262143Sarchie /* Register function hooks */ 74362143Sarchie if (ng_ether_attach_p != NULL) { 74462143Sarchie error = EEXIST; 74562143Sarchie break; 74662143Sarchie } 74762143Sarchie ng_ether_attach_p = ng_ether_attach; 74862143Sarchie ng_ether_detach_p = ng_ether_detach; 74962143Sarchie ng_ether_output_p = ng_ether_output; 75062143Sarchie ng_ether_input_p = ng_ether_input; 75162143Sarchie ng_ether_input_orphan_p = ng_ether_input_orphan; 752139903Sglebius ng_ether_link_state_p = ng_ether_link_state; 75362143Sarchie 75462143Sarchie /* Create nodes for any already-existing Ethernet interfaces */ 755108172Shsu IFNET_RLOCK(); 756181803Sbz TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 75782586Sarchie if (ifp->if_type == IFT_ETHER 75882586Sarchie || ifp->if_type == IFT_L2VLAN) 75962143Sarchie ng_ether_attach(ifp); 76062143Sarchie } 761108172Shsu IFNET_RUNLOCK(); 76262143Sarchie break; 76362143Sarchie 76462143Sarchie case MOD_UNLOAD: 76562143Sarchie 76662143Sarchie /* 76762143Sarchie * Note that the base code won't try to unload us until 76862143Sarchie * all nodes have been removed, and that can't happen 76962143Sarchie * until all Ethernet interfaces are removed. In any 77062143Sarchie * case, we know there are no nodes left if the action 77162143Sarchie * is MOD_UNLOAD, so there's no need to detach any nodes. 77262143Sarchie */ 77362143Sarchie 77462143Sarchie /* Unregister function hooks */ 77562143Sarchie ng_ether_attach_p = NULL; 77662143Sarchie ng_ether_detach_p = NULL; 77762143Sarchie ng_ether_output_p = NULL; 77862143Sarchie ng_ether_input_p = NULL; 77962143Sarchie ng_ether_input_orphan_p = NULL; 780139903Sglebius ng_ether_link_state_p = NULL; 78162143Sarchie break; 78262143Sarchie 78362143Sarchie default: 78462143Sarchie error = EOPNOTSUPP; 78562143Sarchie break; 78662143Sarchie } 78762143Sarchie splx(s); 78862143Sarchie return (error); 78962143Sarchie} 79062143Sarchie 791