ng_ether.c revision 151305
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 151305 2005-10-14 02:38:47Z thompsa $ 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 71147256Sbrooks#define IFP2NG(ifp) ((struct ng_node *)IFP2AC((ifp))->ac_netgraph) 72147256Sbrooks#define IFP2NG_SET(ifp, val) (IFP2AC((ifp))->ac_netgraph = (val)) 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; 11269922Sjulianstatic ng_connect_t ng_ether_connect; 11362143Sarchiestatic ng_rcvdata_t ng_ether_rcvdata; 11462143Sarchiestatic ng_disconnect_t ng_ether_disconnect; 11562143Sarchiestatic int ng_ether_mod_event(module_t mod, int event, void *data); 11662143Sarchie 11762143Sarchie/* List of commands and how to convert arguments to/from ASCII */ 11862143Sarchiestatic const struct ng_cmdlist ng_ether_cmdlist[] = { 11962143Sarchie { 12062143Sarchie NGM_ETHER_COOKIE, 12162143Sarchie NGM_ETHER_GET_IFNAME, 12262143Sarchie "getifname", 12362143Sarchie NULL, 12462143Sarchie &ng_parse_string_type 12562143Sarchie }, 12662143Sarchie { 12762143Sarchie NGM_ETHER_COOKIE, 12862143Sarchie NGM_ETHER_GET_IFINDEX, 12962143Sarchie "getifindex", 13062143Sarchie NULL, 13162143Sarchie &ng_parse_int32_type 13262143Sarchie }, 13364358Sarchie { 13464358Sarchie NGM_ETHER_COOKIE, 13564358Sarchie NGM_ETHER_GET_ENADDR, 13664358Sarchie "getenaddr", 13764358Sarchie NULL, 138123600Sru &ng_parse_enaddr_type 13964358Sarchie }, 14064358Sarchie { 14164358Sarchie NGM_ETHER_COOKIE, 14264653Sarchie NGM_ETHER_SET_ENADDR, 14364653Sarchie "setenaddr", 144123600Sru &ng_parse_enaddr_type, 14564653Sarchie NULL 14664653Sarchie }, 14764653Sarchie { 14864653Sarchie NGM_ETHER_COOKIE, 14964653Sarchie NGM_ETHER_GET_PROMISC, 15064653Sarchie "getpromisc", 15164653Sarchie NULL, 15264653Sarchie &ng_parse_int32_type 15364653Sarchie }, 15464653Sarchie { 15564653Sarchie NGM_ETHER_COOKIE, 15664358Sarchie NGM_ETHER_SET_PROMISC, 15764358Sarchie "setpromisc", 15864358Sarchie &ng_parse_int32_type, 15964358Sarchie NULL 16064358Sarchie }, 16164358Sarchie { 16264358Sarchie NGM_ETHER_COOKIE, 16364653Sarchie NGM_ETHER_GET_AUTOSRC, 16464653Sarchie "getautosrc", 16564653Sarchie NULL, 16664653Sarchie &ng_parse_int32_type 16764653Sarchie }, 16864653Sarchie { 16964653Sarchie NGM_ETHER_COOKIE, 17064358Sarchie NGM_ETHER_SET_AUTOSRC, 17164358Sarchie "setautosrc", 17264358Sarchie &ng_parse_int32_type, 17364358Sarchie NULL 17464358Sarchie }, 175141721Sglebius { 176141721Sglebius NGM_ETHER_COOKIE, 177141721Sglebius NGM_ETHER_ADD_MULTI, 178141721Sglebius "addmulti", 179141721Sglebius &ng_parse_enaddr_type, 180141721Sglebius NULL 181141721Sglebius }, 182141721Sglebius { 183141721Sglebius NGM_ETHER_COOKIE, 184141721Sglebius NGM_ETHER_DEL_MULTI, 185141721Sglebius "delmulti", 186141721Sglebius &ng_parse_enaddr_type, 187141721Sglebius NULL 188141721Sglebius }, 189141910Sglebius { 190141910Sglebius NGM_ETHER_COOKIE, 191141910Sglebius NGM_ETHER_DETACH, 192141910Sglebius "detach", 193141910Sglebius NULL, 194141910Sglebius NULL 195141910Sglebius }, 19662143Sarchie { 0 } 19762143Sarchie}; 19862143Sarchie 19962143Sarchiestatic struct ng_type ng_ether_typestruct = { 200129823Sjulian .version = NG_ABI_VERSION, 201129823Sjulian .name = NG_ETHER_NODE_TYPE, 202129823Sjulian .mod_event = ng_ether_mod_event, 203129823Sjulian .constructor = ng_ether_constructor, 204129823Sjulian .rcvmsg = ng_ether_rcvmsg, 205129823Sjulian .shutdown = ng_ether_shutdown, 206129823Sjulian .newhook = ng_ether_newhook, 207129823Sjulian .connect = ng_ether_connect, 208129823Sjulian .rcvdata = ng_ether_rcvdata, 209129823Sjulian .disconnect = ng_ether_disconnect, 210129823Sjulian .cmdlist = ng_ether_cmdlist, 21162143Sarchie}; 21262143SarchieNETGRAPH_INIT(ether, &ng_ether_typestruct); 21362143Sarchie 21462143Sarchie/****************************************************************** 21562143Sarchie ETHERNET FUNCTION HOOKS 21662143Sarchie******************************************************************/ 21762143Sarchie 21862143Sarchie/* 21962143Sarchie * Handle a packet that has come in on an interface. We get to 22062143Sarchie * look at it here before any upper layer protocols do. 22162143Sarchie * 22262143Sarchie * NOTE: this function will get called at splimp() 22362143Sarchie */ 22462143Sarchiestatic void 225106933Ssamng_ether_input(struct ifnet *ifp, struct mbuf **mp) 22662143Sarchie{ 22762143Sarchie const node_p node = IFP2NG(ifp); 22870784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 229129281Sarchie int error; 23062143Sarchie 23162143Sarchie /* If "lower" hook not connected, let packet continue */ 232129281Sarchie if (priv->lower == NULL) 23362143Sarchie return; 234129281Sarchie NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */ 23562143Sarchie} 23662143Sarchie 23762143Sarchie/* 23862143Sarchie * Handle a packet that has come in on an interface, and which 23962143Sarchie * does not match any of our known protocols (an ``orphan''). 24062143Sarchie * 24162143Sarchie * NOTE: this function will get called at splimp() 24262143Sarchie */ 24362143Sarchiestatic void 244106933Ssamng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m) 24562143Sarchie{ 24662143Sarchie const node_p node = IFP2NG(ifp); 24770784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 248129281Sarchie int error; 24962143Sarchie 250129281Sarchie /* If "orphan" hook not connected, discard packet */ 251129281Sarchie if (priv->orphan == NULL) { 25262143Sarchie m_freem(m); 25362143Sarchie return; 25462143Sarchie } 255129281Sarchie NG_SEND_DATA_ONLY(error, priv->orphan, m); 25662143Sarchie} 25762143Sarchie 25862143Sarchie/* 25962143Sarchie * Handle a packet that is going out on an interface. 26062143Sarchie * The Ethernet header is already attached to the mbuf. 26162143Sarchie */ 26262143Sarchiestatic int 26362143Sarchieng_ether_output(struct ifnet *ifp, struct mbuf **mp) 26462143Sarchie{ 26562143Sarchie const node_p node = IFP2NG(ifp); 26670784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 26762143Sarchie int error = 0; 26862143Sarchie 26962143Sarchie /* If "upper" hook not connected, let packet continue */ 27062143Sarchie if (priv->upper == NULL) 27162143Sarchie return (0); 27262143Sarchie 27362143Sarchie /* Send it out "upper" hook */ 27470700Sjulian NG_SEND_DATA_ONLY(error, priv->upper, *mp); 27562143Sarchie return (error); 27662143Sarchie} 27762143Sarchie 27862143Sarchie/* 27962143Sarchie * A new Ethernet interface has been attached. 28062143Sarchie * Create a new node for it, etc. 28162143Sarchie */ 28262143Sarchiestatic void 28362143Sarchieng_ether_attach(struct ifnet *ifp) 28462143Sarchie{ 28562143Sarchie priv_p priv; 28662143Sarchie node_p node; 28762143Sarchie 28862143Sarchie /* Create node */ 28987599Sobrien KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__)); 29062143Sarchie if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 29162143Sarchie log(LOG_ERR, "%s: can't %s for %s\n", 292121816Sbrooks __func__, "create node", ifp->if_xname); 29362143Sarchie return; 29462143Sarchie } 29562143Sarchie 29662143Sarchie /* Allocate private data */ 29768876Sdwmalone MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 29862143Sarchie if (priv == NULL) { 29962143Sarchie log(LOG_ERR, "%s: can't %s for %s\n", 300121816Sbrooks __func__, "allocate memory", ifp->if_xname); 30170784Sjulian NG_NODE_UNREF(node); 30262143Sarchie return; 30362143Sarchie } 30470784Sjulian NG_NODE_SET_PRIVATE(node, priv); 30562143Sarchie priv->ifp = ifp; 306132780Skan IFP2NG_SET(ifp, node); 30764358Sarchie priv->autoSrcAddr = 1; 30890249Sarchie priv->hwassist = ifp->if_hwassist; 30962143Sarchie 31062143Sarchie /* Try to give the node the same name as the interface */ 311121816Sbrooks if (ng_name_node(node, ifp->if_xname) != 0) { 31262143Sarchie log(LOG_WARNING, "%s: can't name node %s\n", 313121816Sbrooks __func__, ifp->if_xname); 31462143Sarchie } 31562143Sarchie} 31662143Sarchie 31762143Sarchie/* 31862143Sarchie * An Ethernet interface is being detached. 31971849Sjulian * REALLY Destroy its node. 32062143Sarchie */ 32162143Sarchiestatic void 32262143Sarchieng_ether_detach(struct ifnet *ifp) 32362143Sarchie{ 32462143Sarchie const node_p node = IFP2NG(ifp); 32571849Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 32662143Sarchie 32771849Sjulian NG_NODE_REALLY_DIE(node); /* Force real removal of node */ 32871849Sjulian /* 32971849Sjulian * We can't assume the ifnet is still around when we run shutdown 33071849Sjulian * So zap it now. XXX We HOPE that anything running at this time 33171849Sjulian * handles it (as it should in the non netgraph case). 33271849Sjulian */ 333132780Skan IFP2NG_SET(ifp, NULL); 33471849Sjulian priv->ifp = NULL; /* XXX race if interrupted an output packet */ 33571849Sjulian ng_rmnode_self(node); /* remove all netgraph parts */ 33662143Sarchie} 33762143Sarchie 338139903Sglebius/* 339139903Sglebius * Notify graph about link event. 340139903Sglebius * if_link_state_change() has already checked that the state has changed. 341139903Sglebius */ 342139903Sglebiusstatic void 343139903Sglebiusng_ether_link_state(struct ifnet *ifp, int state) 344139903Sglebius{ 345139903Sglebius const node_p node = IFP2NG(ifp); 346139903Sglebius const priv_p priv = NG_NODE_PRIVATE(node); 347139903Sglebius struct ng_mesg *msg; 348139903Sglebius int cmd, dummy_error = 0; 349139903Sglebius 350139903Sglebius if (priv->lower == NULL) 351139903Sglebius return; 352139903Sglebius 353139903Sglebius if (state == LINK_STATE_UP) 354139903Sglebius cmd = NGM_LINK_IS_UP; 355139903Sglebius else if (state == LINK_STATE_DOWN) 356139903Sglebius cmd = NGM_LINK_IS_DOWN; 357139903Sglebius else 358139903Sglebius return; 359139903Sglebius 360139903Sglebius NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT); 361139903Sglebius if (msg != NULL) 362139903Sglebius NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0); 363139903Sglebius} 364139903Sglebius 36562143Sarchie/****************************************************************** 36662143Sarchie NETGRAPH NODE METHODS 36762143Sarchie******************************************************************/ 36862143Sarchie 36962143Sarchie/* 37062143Sarchie * It is not possible or allowable to create a node of this type. 37162143Sarchie * Nodes get created when the interface is attached (or, when 37262143Sarchie * this node type's KLD is loaded). 37362143Sarchie */ 37462143Sarchiestatic int 37570700Sjulianng_ether_constructor(node_p node) 37662143Sarchie{ 37762143Sarchie return (EINVAL); 37862143Sarchie} 37962143Sarchie 38062143Sarchie/* 38162143Sarchie * Check for attaching a new hook. 38262143Sarchie */ 38362143Sarchiestatic int 38462143Sarchieng_ether_newhook(node_p node, hook_p hook, const char *name) 38562143Sarchie{ 38670784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 38762143Sarchie hook_p *hookptr; 38862143Sarchie 38962143Sarchie /* Divert hook is an alias for lower */ 39062143Sarchie if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0) 39162143Sarchie name = NG_ETHER_HOOK_LOWER; 39262143Sarchie 39362143Sarchie /* Which hook? */ 39462143Sarchie if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) 39562143Sarchie hookptr = &priv->upper; 396129281Sarchie else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) 39762143Sarchie hookptr = &priv->lower; 398129281Sarchie else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) 399129281Sarchie hookptr = &priv->orphan; 400129281Sarchie else 40162143Sarchie return (EINVAL); 40262143Sarchie 40362143Sarchie /* Check if already connected (shouldn't be, but doesn't hurt) */ 40462143Sarchie if (*hookptr != NULL) 40562143Sarchie return (EISCONN); 40662143Sarchie 40790249Sarchie /* Disable hardware checksums while 'upper' hook is connected */ 40890249Sarchie if (hookptr == &priv->upper) 40990249Sarchie priv->ifp->if_hwassist = 0; 41090249Sarchie 41162143Sarchie /* OK */ 41262143Sarchie *hookptr = hook; 41362143Sarchie return (0); 41462143Sarchie} 41562143Sarchie 41662143Sarchie/* 41769922Sjulian * Hooks are attached, adjust to force queueing. 41869922Sjulian * We don't really care which hook it is. 41969922Sjulian * they should all be queuing for outgoing data. 42069922Sjulian */ 42169922Sjulianstatic int 42269922Sjulianng_ether_connect(hook_p hook) 42369922Sjulian{ 42470784Sjulian NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook)); 42569922Sjulian return (0); 42669922Sjulian} 42769922Sjulian 42869922Sjulian/* 42962143Sarchie * Receive an incoming control message. 43062143Sarchie */ 43162143Sarchiestatic int 43270700Sjulianng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook) 43362143Sarchie{ 43470784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 43562143Sarchie struct ng_mesg *resp = NULL; 43662143Sarchie int error = 0; 43770700Sjulian struct ng_mesg *msg; 43862143Sarchie 43970700Sjulian NGI_GET_MSG(item, msg); 44062143Sarchie switch (msg->header.typecookie) { 44162143Sarchie case NGM_ETHER_COOKIE: 44262143Sarchie switch (msg->header.cmd) { 44362143Sarchie case NGM_ETHER_GET_IFNAME: 444141195Sru NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 44562143Sarchie if (resp == NULL) { 44662143Sarchie error = ENOMEM; 44762143Sarchie break; 44862143Sarchie } 449141195Sru strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ); 45062143Sarchie break; 45162143Sarchie case NGM_ETHER_GET_IFINDEX: 45262143Sarchie NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 45362143Sarchie if (resp == NULL) { 45462143Sarchie error = ENOMEM; 45562143Sarchie break; 45662143Sarchie } 45762143Sarchie *((u_int32_t *)resp->data) = priv->ifp->if_index; 45862143Sarchie break; 45964358Sarchie case NGM_ETHER_GET_ENADDR: 46064358Sarchie NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT); 46164358Sarchie if (resp == NULL) { 46264358Sarchie error = ENOMEM; 46364358Sarchie break; 46464358Sarchie } 465147256Sbrooks bcopy(IFP2ENADDR(priv->ifp), 46664358Sarchie resp->data, ETHER_ADDR_LEN); 46764358Sarchie break; 46864653Sarchie case NGM_ETHER_SET_ENADDR: 46964653Sarchie { 47064653Sarchie if (msg->header.arglen != ETHER_ADDR_LEN) { 47164653Sarchie error = EINVAL; 47264653Sarchie break; 47364653Sarchie } 47464653Sarchie error = if_setlladdr(priv->ifp, 47564653Sarchie (u_char *)msg->data, ETHER_ADDR_LEN); 47664653Sarchie break; 47764653Sarchie } 47864653Sarchie case NGM_ETHER_GET_PROMISC: 47964653Sarchie NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 48064653Sarchie if (resp == NULL) { 48164653Sarchie error = ENOMEM; 48264653Sarchie break; 48364653Sarchie } 48464653Sarchie *((u_int32_t *)resp->data) = priv->promisc; 48564653Sarchie break; 48664358Sarchie case NGM_ETHER_SET_PROMISC: 48764358Sarchie { 48864358Sarchie u_char want; 48964358Sarchie 49064358Sarchie if (msg->header.arglen != sizeof(u_int32_t)) { 49164358Sarchie error = EINVAL; 49264358Sarchie break; 49364358Sarchie } 49464358Sarchie want = !!*((u_int32_t *)msg->data); 49564358Sarchie if (want ^ priv->promisc) { 49664358Sarchie if ((error = ifpromisc(priv->ifp, want)) != 0) 49764358Sarchie break; 49864358Sarchie priv->promisc = want; 49964358Sarchie } 50064358Sarchie break; 50164358Sarchie } 50264653Sarchie case NGM_ETHER_GET_AUTOSRC: 50364653Sarchie NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 50464653Sarchie if (resp == NULL) { 50564653Sarchie error = ENOMEM; 50664653Sarchie break; 50764653Sarchie } 50864653Sarchie *((u_int32_t *)resp->data) = priv->autoSrcAddr; 50964653Sarchie break; 51064358Sarchie case NGM_ETHER_SET_AUTOSRC: 51164358Sarchie if (msg->header.arglen != sizeof(u_int32_t)) { 51264358Sarchie error = EINVAL; 51364358Sarchie break; 51464358Sarchie } 51564358Sarchie priv->autoSrcAddr = !!*((u_int32_t *)msg->data); 51664358Sarchie break; 517141721Sglebius case NGM_ETHER_ADD_MULTI: 518141721Sglebius { 519141721Sglebius struct sockaddr_dl sa_dl; 520141721Sglebius struct ifmultiaddr *ifm; 521141721Sglebius 522141721Sglebius if (msg->header.arglen != ETHER_ADDR_LEN) { 523141721Sglebius error = EINVAL; 524141721Sglebius break; 525141721Sglebius } 526141755Sglebius bzero(&sa_dl, sizeof(struct sockaddr_dl)); 527141721Sglebius sa_dl.sdl_len = sizeof(struct sockaddr_dl); 528141721Sglebius sa_dl.sdl_family = AF_LINK; 529141755Sglebius sa_dl.sdl_alen = ETHER_ADDR_LEN; 530141721Sglebius bcopy((void *)msg->data, LLADDR(&sa_dl), 531141721Sglebius ETHER_ADDR_LEN); 532141721Sglebius error = if_addmulti(priv->ifp, 533141721Sglebius (struct sockaddr *)&sa_dl, &ifm); 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 */ 628147256Sbrooks bcopy(IFP2ENADDR(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 646151063Sglebius m->m_pkthdr.rcvif = ifp; 64762143Sarchie 648151305Sthompsa /* Pass the packet to the bridge, it may come back to us */ 649151063Sglebius if (ifp->if_bridge) { 650151305Sthompsa BRIDGE_INPUT(ifp, m); 651151063Sglebius if (m == NULL) 652151063Sglebius return (0); 653151063Sglebius } 654151063Sglebius 65562143Sarchie /* Route packet back in */ 656151305Sthompsa ether_demux(ifp, m); 65762143Sarchie return (0); 65862143Sarchie} 65962143Sarchie 66062143Sarchie/* 66171849Sjulian * Shutdown node. This resets the node but does not remove it 66271849Sjulian * unless the REALLY_DIE flag is set. 66362143Sarchie */ 66462143Sarchiestatic int 66570700Sjulianng_ether_shutdown(node_p node) 66662143Sarchie{ 66770784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 66864358Sarchie 669132464Sjulian if (node->nd_flags & NGF_REALLY_DIE) { 67071849Sjulian /* 67171849Sjulian * WE came here because the ethernet card is being unloaded, 67271849Sjulian * so stop being persistant. 67371849Sjulian * Actually undo all the things we did on creation. 67471849Sjulian * Assume the ifp has already been freed. 67571849Sjulian */ 67671849Sjulian NG_NODE_SET_PRIVATE(node, NULL); 67771849Sjulian FREE(priv, M_NETGRAPH); 67871849Sjulian NG_NODE_UNREF(node); /* free node itself */ 67971849Sjulian return (0); 68070700Sjulian } 681124269Sgreen if (priv->promisc) { /* disable promiscuous mode */ 682124269Sgreen (void)ifpromisc(priv->ifp, 0); 683124269Sgreen priv->promisc = 0; 684124269Sgreen } 68564358Sarchie priv->autoSrcAddr = 1; /* reset auto-src-addr flag */ 686132464Sjulian NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */ 687132464Sjulian 68862143Sarchie return (0); 68962143Sarchie} 69062143Sarchie 69162143Sarchie/* 69262143Sarchie * Hook disconnection. 69362143Sarchie */ 69462143Sarchiestatic int 69562143Sarchieng_ether_disconnect(hook_p hook) 69662143Sarchie{ 69770784Sjulian const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 69862143Sarchie 69990249Sarchie if (hook == priv->upper) { 70062143Sarchie priv->upper = NULL; 701124270Sgreen if (priv->ifp != NULL) /* restore h/w csum */ 702124270Sgreen priv->ifp->if_hwassist = priv->hwassist; 703129281Sarchie } else if (hook == priv->lower) 70462143Sarchie priv->lower = NULL; 705129281Sarchie else if (hook == priv->orphan) 706129281Sarchie priv->orphan = NULL; 707129281Sarchie else 70887599Sobrien panic("%s: weird hook", __func__); 70970784Sjulian if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 71070784Sjulian && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 71170784Sjulian ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */ 71262143Sarchie return (0); 71362143Sarchie} 71462143Sarchie 71562143Sarchie/****************************************************************** 71662143Sarchie INITIALIZATION 71762143Sarchie******************************************************************/ 71862143Sarchie 71962143Sarchie/* 72062143Sarchie * Handle loading and unloading for this node type. 72162143Sarchie */ 72262143Sarchiestatic int 72362143Sarchieng_ether_mod_event(module_t mod, int event, void *data) 72462143Sarchie{ 72562143Sarchie struct ifnet *ifp; 72662143Sarchie int error = 0; 72762143Sarchie int s; 72862143Sarchie 72962143Sarchie s = splnet(); 73062143Sarchie switch (event) { 73162143Sarchie case MOD_LOAD: 73262143Sarchie 73362143Sarchie /* Register function hooks */ 73462143Sarchie if (ng_ether_attach_p != NULL) { 73562143Sarchie error = EEXIST; 73662143Sarchie break; 73762143Sarchie } 73862143Sarchie ng_ether_attach_p = ng_ether_attach; 73962143Sarchie ng_ether_detach_p = ng_ether_detach; 74062143Sarchie ng_ether_output_p = ng_ether_output; 74162143Sarchie ng_ether_input_p = ng_ether_input; 74262143Sarchie ng_ether_input_orphan_p = ng_ether_input_orphan; 743139903Sglebius ng_ether_link_state_p = ng_ether_link_state; 74462143Sarchie 74562143Sarchie /* Create nodes for any already-existing Ethernet interfaces */ 746108172Shsu IFNET_RLOCK(); 74762143Sarchie TAILQ_FOREACH(ifp, &ifnet, if_link) { 74882586Sarchie if (ifp->if_type == IFT_ETHER 74982586Sarchie || ifp->if_type == IFT_L2VLAN) 75062143Sarchie ng_ether_attach(ifp); 75162143Sarchie } 752108172Shsu IFNET_RUNLOCK(); 75362143Sarchie break; 75462143Sarchie 75562143Sarchie case MOD_UNLOAD: 75662143Sarchie 75762143Sarchie /* 75862143Sarchie * Note that the base code won't try to unload us until 75962143Sarchie * all nodes have been removed, and that can't happen 76062143Sarchie * until all Ethernet interfaces are removed. In any 76162143Sarchie * case, we know there are no nodes left if the action 76262143Sarchie * is MOD_UNLOAD, so there's no need to detach any nodes. 76362143Sarchie */ 76462143Sarchie 76562143Sarchie /* Unregister function hooks */ 76662143Sarchie ng_ether_attach_p = NULL; 76762143Sarchie ng_ether_detach_p = NULL; 76862143Sarchie ng_ether_output_p = NULL; 76962143Sarchie ng_ether_input_p = NULL; 77062143Sarchie ng_ether_input_orphan_p = NULL; 771139903Sglebius ng_ether_link_state_p = NULL; 77262143Sarchie break; 77362143Sarchie 77462143Sarchie default: 77562143Sarchie error = EOPNOTSUPP; 77662143Sarchie break; 77762143Sarchie } 77862143Sarchie splx(s); 77962143Sarchie return (error); 78062143Sarchie} 78162143Sarchie 782