ng_ether.c revision 152315
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 152315 2005-11-11 16:04:59Z ru $ 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> 5762143Sarchie 5862143Sarchie#include <net/if.h> 59141721Sglebius#include <net/if_dl.h> 6062143Sarchie#include <net/if_types.h> 6162143Sarchie#include <net/if_arp.h> 6262143Sarchie#include <net/if_var.h> 6362143Sarchie#include <net/ethernet.h> 64151305Sthompsa#include <net/if_bridgevar.h> 6562143Sarchie 6662143Sarchie#include <netgraph/ng_message.h> 6762143Sarchie#include <netgraph/netgraph.h> 6862143Sarchie#include <netgraph/ng_parse.h> 6962143Sarchie#include <netgraph/ng_ether.h> 7062143Sarchie 71152243Sru#define IFP2NG(ifp) (IFP2AC((ifp))->ac_netgraph) 7262143Sarchie 73126035Spjd/* Per-node private data */ 74126035Spjdstruct private { 75126035Spjd struct ifnet *ifp; /* associated interface */ 76126035Spjd hook_p upper; /* upper hook connection */ 77129281Sarchie hook_p lower; /* lower hook connection */ 78129281Sarchie hook_p orphan; /* orphan hook connection */ 79126035Spjd u_char autoSrcAddr; /* always overwrite source address */ 80126035Spjd u_char promisc; /* promiscuous mode enabled */ 81126035Spjd u_long hwassist; /* hardware checksum capabilities */ 82126035Spjd u_int flags; /* flags e.g. really die */ 83126035Spjd}; 84126035Spjdtypedef struct private *priv_p; 8562143Sarchie 86106933Ssam/* Hook pointers used by if_ethersubr.c to callback to netgraph */ 87106933Ssamextern void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 88106933Ssamextern void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 89106933Ssamextern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 90106933Ssamextern void (*ng_ether_attach_p)(struct ifnet *ifp); 91106933Ssamextern void (*ng_ether_detach_p)(struct ifnet *ifp); 92139903Sglebiusextern void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 93106933Ssam 9462143Sarchie/* Functional hooks called from if_ethersubr.c */ 95106933Ssamstatic void ng_ether_input(struct ifnet *ifp, struct mbuf **mp); 96106933Ssamstatic void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m); 9762143Sarchiestatic int ng_ether_output(struct ifnet *ifp, struct mbuf **mp); 9862143Sarchiestatic void ng_ether_attach(struct ifnet *ifp); 9962143Sarchiestatic void ng_ether_detach(struct ifnet *ifp); 100139903Sglebiusstatic void ng_ether_link_state(struct ifnet *ifp, int state); 10162143Sarchie 10262143Sarchie/* Other functions */ 103131155Sjulianstatic int ng_ether_rcv_lower(node_p node, struct mbuf *m); 104131155Sjulianstatic int ng_ether_rcv_upper(node_p node, struct mbuf *m); 10562143Sarchie 10662143Sarchie/* Netgraph node methods */ 10762143Sarchiestatic ng_constructor_t ng_ether_constructor; 10862143Sarchiestatic ng_rcvmsg_t ng_ether_rcvmsg; 10970700Sjulianstatic ng_shutdown_t ng_ether_shutdown; 11062143Sarchiestatic ng_newhook_t ng_ether_newhook; 11169922Sjulianstatic ng_connect_t ng_ether_connect; 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 .connect = ng_ether_connect, 207129823Sjulian .rcvdata = ng_ether_rcvdata, 208129823Sjulian .disconnect = ng_ether_disconnect, 209129823Sjulian .cmdlist = ng_ether_cmdlist, 21062143Sarchie}; 21162143SarchieNETGRAPH_INIT(ether, &ng_ether_typestruct); 21262143Sarchie 21362143Sarchie/****************************************************************** 21462143Sarchie ETHERNET FUNCTION HOOKS 21562143Sarchie******************************************************************/ 21662143Sarchie 21762143Sarchie/* 21862143Sarchie * Handle a packet that has come in on an interface. We get to 21962143Sarchie * look at it here before any upper layer protocols do. 22062143Sarchie * 22162143Sarchie * NOTE: this function will get called at splimp() 22262143Sarchie */ 22362143Sarchiestatic void 224106933Ssamng_ether_input(struct ifnet *ifp, struct mbuf **mp) 22562143Sarchie{ 22662143Sarchie const node_p node = IFP2NG(ifp); 22770784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 228129281Sarchie int error; 22962143Sarchie 23062143Sarchie /* If "lower" hook not connected, let packet continue */ 231129281Sarchie if (priv->lower == NULL) 23262143Sarchie return; 233129281Sarchie NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */ 23462143Sarchie} 23562143Sarchie 23662143Sarchie/* 23762143Sarchie * Handle a packet that has come in on an interface, and which 23862143Sarchie * does not match any of our known protocols (an ``orphan''). 23962143Sarchie * 24062143Sarchie * NOTE: this function will get called at splimp() 24162143Sarchie */ 24262143Sarchiestatic void 243106933Ssamng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m) 24462143Sarchie{ 24562143Sarchie const node_p node = IFP2NG(ifp); 24670784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 247129281Sarchie int error; 24862143Sarchie 249129281Sarchie /* If "orphan" hook not connected, discard packet */ 250129281Sarchie if (priv->orphan == NULL) { 25162143Sarchie m_freem(m); 25262143Sarchie return; 25362143Sarchie } 254129281Sarchie NG_SEND_DATA_ONLY(error, priv->orphan, m); 25562143Sarchie} 25662143Sarchie 25762143Sarchie/* 25862143Sarchie * Handle a packet that is going out on an interface. 25962143Sarchie * The Ethernet header is already attached to the mbuf. 26062143Sarchie */ 26162143Sarchiestatic int 26262143Sarchieng_ether_output(struct ifnet *ifp, struct mbuf **mp) 26362143Sarchie{ 26462143Sarchie const node_p node = IFP2NG(ifp); 26570784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 26662143Sarchie int error = 0; 26762143Sarchie 26862143Sarchie /* If "upper" hook not connected, let packet continue */ 26962143Sarchie if (priv->upper == NULL) 27062143Sarchie return (0); 27162143Sarchie 27262143Sarchie /* Send it out "upper" hook */ 27370700Sjulian NG_SEND_DATA_ONLY(error, priv->upper, *mp); 27462143Sarchie return (error); 27562143Sarchie} 27662143Sarchie 27762143Sarchie/* 27862143Sarchie * A new Ethernet interface has been attached. 27962143Sarchie * Create a new node for it, etc. 28062143Sarchie */ 28162143Sarchiestatic void 28262143Sarchieng_ether_attach(struct ifnet *ifp) 28362143Sarchie{ 28462143Sarchie priv_p priv; 28562143Sarchie node_p node; 28662143Sarchie 28762143Sarchie /* Create node */ 28887599Sobrien KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__)); 28962143Sarchie if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 29062143Sarchie log(LOG_ERR, "%s: can't %s for %s\n", 291121816Sbrooks __func__, "create node", ifp->if_xname); 29262143Sarchie return; 29362143Sarchie } 29462143Sarchie 29562143Sarchie /* Allocate private data */ 29668876Sdwmalone MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 29762143Sarchie if (priv == NULL) { 29862143Sarchie log(LOG_ERR, "%s: can't %s for %s\n", 299121816Sbrooks __func__, "allocate memory", ifp->if_xname); 30070784Sjulian NG_NODE_UNREF(node); 30162143Sarchie return; 30262143Sarchie } 30370784Sjulian NG_NODE_SET_PRIVATE(node, priv); 30462143Sarchie priv->ifp = ifp; 305152243Sru IFP2NG(ifp) = node; 30664358Sarchie priv->autoSrcAddr = 1; 30790249Sarchie priv->hwassist = ifp->if_hwassist; 30862143Sarchie 30962143Sarchie /* Try to give the node the same name as the interface */ 310121816Sbrooks if (ng_name_node(node, ifp->if_xname) != 0) { 31162143Sarchie log(LOG_WARNING, "%s: can't name node %s\n", 312121816Sbrooks __func__, ifp->if_xname); 31362143Sarchie } 31462143Sarchie} 31562143Sarchie 31662143Sarchie/* 31762143Sarchie * An Ethernet interface is being detached. 31871849Sjulian * REALLY Destroy its node. 31962143Sarchie */ 32062143Sarchiestatic void 32162143Sarchieng_ether_detach(struct ifnet *ifp) 32262143Sarchie{ 32362143Sarchie const node_p node = IFP2NG(ifp); 32471849Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 32562143Sarchie 32671849Sjulian NG_NODE_REALLY_DIE(node); /* Force real removal of node */ 32771849Sjulian /* 32871849Sjulian * We can't assume the ifnet is still around when we run shutdown 32971849Sjulian * So zap it now. XXX We HOPE that anything running at this time 33071849Sjulian * handles it (as it should in the non netgraph case). 33171849Sjulian */ 332152243Sru IFP2NG(ifp) = NULL; 33371849Sjulian priv->ifp = NULL; /* XXX race if interrupted an output packet */ 33471849Sjulian ng_rmnode_self(node); /* remove all netgraph parts */ 33562143Sarchie} 33662143Sarchie 337139903Sglebius/* 338139903Sglebius * Notify graph about link event. 339139903Sglebius * if_link_state_change() has already checked that the state has changed. 340139903Sglebius */ 341139903Sglebiusstatic void 342139903Sglebiusng_ether_link_state(struct ifnet *ifp, int state) 343139903Sglebius{ 344139903Sglebius const node_p node = IFP2NG(ifp); 345139903Sglebius const priv_p priv = NG_NODE_PRIVATE(node); 346139903Sglebius struct ng_mesg *msg; 347139903Sglebius int cmd, dummy_error = 0; 348139903Sglebius 349139903Sglebius if (priv->lower == NULL) 350139903Sglebius return; 351139903Sglebius 352139903Sglebius if (state == LINK_STATE_UP) 353139903Sglebius cmd = NGM_LINK_IS_UP; 354139903Sglebius else if (state == LINK_STATE_DOWN) 355139903Sglebius cmd = NGM_LINK_IS_DOWN; 356139903Sglebius else 357139903Sglebius return; 358139903Sglebius 359139903Sglebius NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT); 360139903Sglebius if (msg != NULL) 361139903Sglebius NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0); 362139903Sglebius} 363139903Sglebius 36462143Sarchie/****************************************************************** 36562143Sarchie NETGRAPH NODE METHODS 36662143Sarchie******************************************************************/ 36762143Sarchie 36862143Sarchie/* 36962143Sarchie * It is not possible or allowable to create a node of this type. 37062143Sarchie * Nodes get created when the interface is attached (or, when 37162143Sarchie * this node type's KLD is loaded). 37262143Sarchie */ 37362143Sarchiestatic int 37470700Sjulianng_ether_constructor(node_p node) 37562143Sarchie{ 37662143Sarchie return (EINVAL); 37762143Sarchie} 37862143Sarchie 37962143Sarchie/* 38062143Sarchie * Check for attaching a new hook. 38162143Sarchie */ 38262143Sarchiestatic int 38362143Sarchieng_ether_newhook(node_p node, hook_p hook, const char *name) 38462143Sarchie{ 38570784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 38662143Sarchie hook_p *hookptr; 38762143Sarchie 38862143Sarchie /* Divert hook is an alias for lower */ 38962143Sarchie if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0) 39062143Sarchie name = NG_ETHER_HOOK_LOWER; 39162143Sarchie 39262143Sarchie /* Which hook? */ 39362143Sarchie if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) 39462143Sarchie hookptr = &priv->upper; 395129281Sarchie else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) 39662143Sarchie hookptr = &priv->lower; 397129281Sarchie else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) 398129281Sarchie hookptr = &priv->orphan; 399129281Sarchie else 40062143Sarchie return (EINVAL); 40162143Sarchie 40262143Sarchie /* Check if already connected (shouldn't be, but doesn't hurt) */ 40362143Sarchie if (*hookptr != NULL) 40462143Sarchie return (EISCONN); 40562143Sarchie 40690249Sarchie /* Disable hardware checksums while 'upper' hook is connected */ 40790249Sarchie if (hookptr == &priv->upper) 40890249Sarchie priv->ifp->if_hwassist = 0; 40990249Sarchie 41062143Sarchie /* OK */ 41162143Sarchie *hookptr = hook; 41262143Sarchie return (0); 41362143Sarchie} 41462143Sarchie 41562143Sarchie/* 41669922Sjulian * Hooks are attached, adjust to force queueing. 41769922Sjulian * We don't really care which hook it is. 41869922Sjulian * they should all be queuing for outgoing data. 41969922Sjulian */ 42069922Sjulianstatic int 42169922Sjulianng_ether_connect(hook_p hook) 42269922Sjulian{ 42370784Sjulian NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook)); 42469922Sjulian return (0); 42569922Sjulian} 42669922Sjulian 42769922Sjulian/* 42862143Sarchie * Receive an incoming control message. 42962143Sarchie */ 43062143Sarchiestatic int 43170700Sjulianng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook) 43262143Sarchie{ 43370784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 43462143Sarchie struct ng_mesg *resp = NULL; 43562143Sarchie int error = 0; 43670700Sjulian struct ng_mesg *msg; 43762143Sarchie 43870700Sjulian NGI_GET_MSG(item, msg); 43962143Sarchie switch (msg->header.typecookie) { 44062143Sarchie case NGM_ETHER_COOKIE: 44162143Sarchie switch (msg->header.cmd) { 44262143Sarchie case NGM_ETHER_GET_IFNAME: 443141195Sru NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 44462143Sarchie if (resp == NULL) { 44562143Sarchie error = ENOMEM; 44662143Sarchie break; 44762143Sarchie } 448141195Sru strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ); 44962143Sarchie break; 45062143Sarchie case NGM_ETHER_GET_IFINDEX: 45162143Sarchie NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 45262143Sarchie if (resp == NULL) { 45362143Sarchie error = ENOMEM; 45462143Sarchie break; 45562143Sarchie } 45662143Sarchie *((u_int32_t *)resp->data) = priv->ifp->if_index; 45762143Sarchie break; 45864358Sarchie case NGM_ETHER_GET_ENADDR: 45964358Sarchie NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT); 46064358Sarchie if (resp == NULL) { 46164358Sarchie error = ENOMEM; 46264358Sarchie break; 46364358Sarchie } 464152315Sru bcopy(IF_LLADDR(priv->ifp), 46564358Sarchie resp->data, ETHER_ADDR_LEN); 46664358Sarchie break; 46764653Sarchie case NGM_ETHER_SET_ENADDR: 46864653Sarchie { 46964653Sarchie if (msg->header.arglen != ETHER_ADDR_LEN) { 47064653Sarchie error = EINVAL; 47164653Sarchie break; 47264653Sarchie } 47364653Sarchie error = if_setlladdr(priv->ifp, 47464653Sarchie (u_char *)msg->data, ETHER_ADDR_LEN); 47564653Sarchie break; 47664653Sarchie } 47764653Sarchie case NGM_ETHER_GET_PROMISC: 47864653Sarchie NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 47964653Sarchie if (resp == NULL) { 48064653Sarchie error = ENOMEM; 48164653Sarchie break; 48264653Sarchie } 48364653Sarchie *((u_int32_t *)resp->data) = priv->promisc; 48464653Sarchie break; 48564358Sarchie case NGM_ETHER_SET_PROMISC: 48664358Sarchie { 48764358Sarchie u_char want; 48864358Sarchie 48964358Sarchie if (msg->header.arglen != sizeof(u_int32_t)) { 49064358Sarchie error = EINVAL; 49164358Sarchie break; 49264358Sarchie } 49364358Sarchie want = !!*((u_int32_t *)msg->data); 49464358Sarchie if (want ^ priv->promisc) { 49564358Sarchie if ((error = ifpromisc(priv->ifp, want)) != 0) 49664358Sarchie break; 49764358Sarchie priv->promisc = want; 49864358Sarchie } 49964358Sarchie break; 50064358Sarchie } 50164653Sarchie case NGM_ETHER_GET_AUTOSRC: 50264653Sarchie NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 50364653Sarchie if (resp == NULL) { 50464653Sarchie error = ENOMEM; 50564653Sarchie break; 50664653Sarchie } 50764653Sarchie *((u_int32_t *)resp->data) = priv->autoSrcAddr; 50864653Sarchie break; 50964358Sarchie case NGM_ETHER_SET_AUTOSRC: 51064358Sarchie if (msg->header.arglen != sizeof(u_int32_t)) { 51164358Sarchie error = EINVAL; 51264358Sarchie break; 51364358Sarchie } 51464358Sarchie priv->autoSrcAddr = !!*((u_int32_t *)msg->data); 51564358Sarchie break; 516141721Sglebius case NGM_ETHER_ADD_MULTI: 517141721Sglebius { 518141721Sglebius struct sockaddr_dl sa_dl; 519141721Sglebius struct ifmultiaddr *ifm; 520141721Sglebius 521141721Sglebius if (msg->header.arglen != ETHER_ADDR_LEN) { 522141721Sglebius error = EINVAL; 523141721Sglebius break; 524141721Sglebius } 525141755Sglebius bzero(&sa_dl, sizeof(struct sockaddr_dl)); 526141721Sglebius sa_dl.sdl_len = sizeof(struct sockaddr_dl); 527141721Sglebius sa_dl.sdl_family = AF_LINK; 528141755Sglebius sa_dl.sdl_alen = ETHER_ADDR_LEN; 529141721Sglebius bcopy((void *)msg->data, LLADDR(&sa_dl), 530141721Sglebius ETHER_ADDR_LEN); 531141721Sglebius error = if_addmulti(priv->ifp, 532141721Sglebius (struct sockaddr *)&sa_dl, &ifm); 533141721Sglebius break; 534141721Sglebius } 535141721Sglebius case NGM_ETHER_DEL_MULTI: 536141721Sglebius { 537141721Sglebius struct sockaddr_dl sa_dl; 538141721Sglebius 539141721Sglebius if (msg->header.arglen != ETHER_ADDR_LEN) { 540141721Sglebius error = EINVAL; 541141721Sglebius break; 542141721Sglebius } 543141755Sglebius bzero(&sa_dl, sizeof(struct sockaddr_dl)); 544141721Sglebius sa_dl.sdl_len = sizeof(struct sockaddr_dl); 545141721Sglebius sa_dl.sdl_family = AF_LINK; 546141755Sglebius sa_dl.sdl_alen = ETHER_ADDR_LEN; 547141721Sglebius bcopy((void *)msg->data, LLADDR(&sa_dl), 548141721Sglebius ETHER_ADDR_LEN); 549141721Sglebius error = if_delmulti(priv->ifp, 550141721Sglebius (struct sockaddr *)&sa_dl); 551141721Sglebius break; 552141721Sglebius } 553141910Sglebius case NGM_ETHER_DETACH: 554141910Sglebius ng_ether_detach(priv->ifp); 555141910Sglebius break; 55662143Sarchie default: 55762143Sarchie error = EINVAL; 55862143Sarchie break; 55962143Sarchie } 56062143Sarchie break; 56162143Sarchie default: 56262143Sarchie error = EINVAL; 56362143Sarchie break; 56462143Sarchie } 56570700Sjulian NG_RESPOND_MSG(error, node, item, resp); 56670700Sjulian NG_FREE_MSG(msg); 56762143Sarchie return (error); 56862143Sarchie} 56962143Sarchie 57062143Sarchie/* 57162143Sarchie * Receive data on a hook. 57262143Sarchie */ 57362143Sarchiestatic int 57470700Sjulianng_ether_rcvdata(hook_p hook, item_p item) 57562143Sarchie{ 57670784Sjulian const node_p node = NG_HOOK_NODE(hook); 57770784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 57870700Sjulian struct mbuf *m; 57962143Sarchie 58070700Sjulian NGI_GET_M(item, m); 58170700Sjulian NG_FREE_ITEM(item); 582131155Sjulian 583129281Sarchie if (hook == priv->lower || hook == priv->orphan) 584131155Sjulian return ng_ether_rcv_lower(node, m); 58562143Sarchie if (hook == priv->upper) 586131155Sjulian return ng_ether_rcv_upper(node, m); 58787599Sobrien panic("%s: weird hook", __func__); 588129281Sarchie#ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */ 589136312Sdes return (0); 59083366Sjulian#endif 59162143Sarchie} 59262143Sarchie 59362143Sarchie/* 594129281Sarchie * Handle an mbuf received on the "lower" or "orphan" hook. 59562143Sarchie */ 59662143Sarchiestatic int 597131155Sjulianng_ether_rcv_lower(node_p node, struct mbuf *m) 59862143Sarchie{ 59970784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 60096265Sarchie struct ifnet *const ifp = priv->ifp; 60162143Sarchie 60296265Sarchie /* Check whether interface is ready for packets */ 603148887Srwatson if (!((ifp->if_flags & IFF_UP) && 604148887Srwatson (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 60596265Sarchie NG_FREE_M(m); 60696265Sarchie return (ENETDOWN); 60796265Sarchie } 60896265Sarchie 60962143Sarchie /* Make sure header is fully pulled up */ 61062143Sarchie if (m->m_pkthdr.len < sizeof(struct ether_header)) { 61170700Sjulian NG_FREE_M(m); 61262143Sarchie return (EINVAL); 61362143Sarchie } 61462143Sarchie if (m->m_len < sizeof(struct ether_header) 61597896Sarchie && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 61662143Sarchie return (ENOBUFS); 61762143Sarchie 61864358Sarchie /* Drop in the MAC address if desired */ 61964358Sarchie if (priv->autoSrcAddr) { 62097896Sarchie 62197896Sarchie /* Make the mbuf writable if it's not already */ 62297896Sarchie if (!M_WRITABLE(m) 62397896Sarchie && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 62497896Sarchie return (ENOBUFS); 62597896Sarchie 62697896Sarchie /* Overwrite source MAC address */ 627152315Sru bcopy(IF_LLADDR(ifp), 62864358Sarchie mtod(m, struct ether_header *)->ether_shost, 62964358Sarchie ETHER_ADDR_LEN); 63064358Sarchie } 63162678Sjulian 63262143Sarchie /* Send it on its way */ 63396265Sarchie return ether_output_frame(ifp, m); 63462143Sarchie} 63562143Sarchie 63662143Sarchie/* 63762143Sarchie * Handle an mbuf received on the "upper" hook. 63862143Sarchie */ 63962143Sarchiestatic int 640131155Sjulianng_ether_rcv_upper(node_p node, struct mbuf *m) 64162143Sarchie{ 64270784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 643151063Sglebius struct ifnet *ifp = priv->ifp; 64462143Sarchie 645152001Sru /* Check length and pull off header */ 646152001Sru if (m->m_pkthdr.len < sizeof(struct ether_header)) { 647152001Sru NG_FREE_M(m); 648152001Sru return (EINVAL); 649152001Sru } 650152001Sru if (m->m_len < sizeof(struct ether_header) && 651152001Sru (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 652152001Sru return (ENOBUFS); 653152001Sru 654151063Sglebius m->m_pkthdr.rcvif = ifp; 65562143Sarchie 656151305Sthompsa /* Pass the packet to the bridge, it may come back to us */ 657151063Sglebius if (ifp->if_bridge) { 658151305Sthompsa BRIDGE_INPUT(ifp, m); 659151063Sglebius if (m == NULL) 660151063Sglebius return (0); 661151063Sglebius } 662151063Sglebius 66362143Sarchie /* Route packet back in */ 664151305Sthompsa ether_demux(ifp, m); 66562143Sarchie return (0); 66662143Sarchie} 66762143Sarchie 66862143Sarchie/* 66971849Sjulian * Shutdown node. This resets the node but does not remove it 67071849Sjulian * unless the REALLY_DIE flag is set. 67162143Sarchie */ 67262143Sarchiestatic int 67370700Sjulianng_ether_shutdown(node_p node) 67462143Sarchie{ 67570784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 67664358Sarchie 677132464Sjulian if (node->nd_flags & NGF_REALLY_DIE) { 67871849Sjulian /* 67971849Sjulian * WE came here because the ethernet card is being unloaded, 68071849Sjulian * so stop being persistant. 68171849Sjulian * Actually undo all the things we did on creation. 68271849Sjulian * Assume the ifp has already been freed. 68371849Sjulian */ 68471849Sjulian NG_NODE_SET_PRIVATE(node, NULL); 68571849Sjulian FREE(priv, M_NETGRAPH); 68671849Sjulian NG_NODE_UNREF(node); /* free node itself */ 68771849Sjulian return (0); 68870700Sjulian } 689124269Sgreen if (priv->promisc) { /* disable promiscuous mode */ 690124269Sgreen (void)ifpromisc(priv->ifp, 0); 691124269Sgreen priv->promisc = 0; 692124269Sgreen } 69364358Sarchie priv->autoSrcAddr = 1; /* reset auto-src-addr flag */ 694132464Sjulian NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */ 695132464Sjulian 69662143Sarchie return (0); 69762143Sarchie} 69862143Sarchie 69962143Sarchie/* 70062143Sarchie * Hook disconnection. 70162143Sarchie */ 70262143Sarchiestatic int 70362143Sarchieng_ether_disconnect(hook_p hook) 70462143Sarchie{ 70570784Sjulian const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 70662143Sarchie 70790249Sarchie if (hook == priv->upper) { 70862143Sarchie priv->upper = NULL; 709124270Sgreen if (priv->ifp != NULL) /* restore h/w csum */ 710124270Sgreen priv->ifp->if_hwassist = priv->hwassist; 711129281Sarchie } else if (hook == priv->lower) 71262143Sarchie priv->lower = NULL; 713129281Sarchie else if (hook == priv->orphan) 714129281Sarchie priv->orphan = NULL; 715129281Sarchie else 71687599Sobrien panic("%s: weird hook", __func__); 71770784Sjulian if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 71870784Sjulian && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 71970784Sjulian ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */ 72062143Sarchie return (0); 72162143Sarchie} 72262143Sarchie 72362143Sarchie/****************************************************************** 72462143Sarchie INITIALIZATION 72562143Sarchie******************************************************************/ 72662143Sarchie 72762143Sarchie/* 72862143Sarchie * Handle loading and unloading for this node type. 72962143Sarchie */ 73062143Sarchiestatic int 73162143Sarchieng_ether_mod_event(module_t mod, int event, void *data) 73262143Sarchie{ 73362143Sarchie struct ifnet *ifp; 73462143Sarchie int error = 0; 73562143Sarchie int s; 73662143Sarchie 73762143Sarchie s = splnet(); 73862143Sarchie switch (event) { 73962143Sarchie case MOD_LOAD: 74062143Sarchie 74162143Sarchie /* Register function hooks */ 74262143Sarchie if (ng_ether_attach_p != NULL) { 74362143Sarchie error = EEXIST; 74462143Sarchie break; 74562143Sarchie } 74662143Sarchie ng_ether_attach_p = ng_ether_attach; 74762143Sarchie ng_ether_detach_p = ng_ether_detach; 74862143Sarchie ng_ether_output_p = ng_ether_output; 74962143Sarchie ng_ether_input_p = ng_ether_input; 75062143Sarchie ng_ether_input_orphan_p = ng_ether_input_orphan; 751139903Sglebius ng_ether_link_state_p = ng_ether_link_state; 75262143Sarchie 75362143Sarchie /* Create nodes for any already-existing Ethernet interfaces */ 754108172Shsu IFNET_RLOCK(); 75562143Sarchie TAILQ_FOREACH(ifp, &ifnet, if_link) { 75682586Sarchie if (ifp->if_type == IFT_ETHER 75782586Sarchie || ifp->if_type == IFT_L2VLAN) 75862143Sarchie ng_ether_attach(ifp); 75962143Sarchie } 760108172Shsu IFNET_RUNLOCK(); 76162143Sarchie break; 76262143Sarchie 76362143Sarchie case MOD_UNLOAD: 76462143Sarchie 76562143Sarchie /* 76662143Sarchie * Note that the base code won't try to unload us until 76762143Sarchie * all nodes have been removed, and that can't happen 76862143Sarchie * until all Ethernet interfaces are removed. In any 76962143Sarchie * case, we know there are no nodes left if the action 77062143Sarchie * is MOD_UNLOAD, so there's no need to detach any nodes. 77162143Sarchie */ 77262143Sarchie 77362143Sarchie /* Unregister function hooks */ 77462143Sarchie ng_ether_attach_p = NULL; 77562143Sarchie ng_ether_detach_p = NULL; 77662143Sarchie ng_ether_output_p = NULL; 77762143Sarchie ng_ether_input_p = NULL; 77862143Sarchie ng_ether_input_orphan_p = NULL; 779139903Sglebius ng_ether_link_state_p = NULL; 78062143Sarchie break; 78162143Sarchie 78262143Sarchie default: 78362143Sarchie error = EOPNOTSUPP; 78462143Sarchie break; 78562143Sarchie } 78662143Sarchie splx(s); 78762143Sarchie return (error); 78862143Sarchie} 78962143Sarchie 790