1 2/* 3 * ng_ether.c 4 */ 5 6/*- 7 * Copyright (c) 1996-2000 Whistle Communications, Inc. 8 * All rights reserved. 9 * 10 * Subject to the following obligations and disclaimer of warranty, use and 11 * redistribution of this software, in source or object code forms, with or 12 * without modifications are expressly permitted by Whistle Communications; 13 * provided, however, that: 14 * 1. Any and all reproductions of the source or object code must include the 15 * copyright notice above and the following disclaimer of warranties; and 16 * 2. No rights are granted, in any manner or form, to use Whistle 17 * Communications, Inc. trademarks, including the mark "WHISTLE 18 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 19 * such appears in the above copyright notice or in the software. 20 * 21 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 22 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 23 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 24 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 26 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 27 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 28 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 29 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 30 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 31 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 32 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 33 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 36 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 37 * OF SUCH DAMAGE. 38 * 39 * Authors: Archie Cobbs <archie@freebsd.org> 40 * Julian Elischer <julian@freebsd.org> 41 *
| 1 2/* 3 * ng_ether.c 4 */ 5 6/*- 7 * Copyright (c) 1996-2000 Whistle Communications, Inc. 8 * All rights reserved. 9 * 10 * Subject to the following obligations and disclaimer of warranty, use and 11 * redistribution of this software, in source or object code forms, with or 12 * without modifications are expressly permitted by Whistle Communications; 13 * provided, however, that: 14 * 1. Any and all reproductions of the source or object code must include the 15 * copyright notice above and the following disclaimer of warranties; and 16 * 2. No rights are granted, in any manner or form, to use Whistle 17 * Communications, Inc. trademarks, including the mark "WHISTLE 18 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 19 * such appears in the above copyright notice or in the software. 20 * 21 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 22 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 23 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 24 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 26 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 27 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 28 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 29 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 30 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 31 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 32 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 33 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 36 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 37 * OF SUCH DAMAGE. 38 * 39 * Authors: Archie Cobbs <archie@freebsd.org> 40 * Julian Elischer <julian@freebsd.org> 41 *
|
42 * $FreeBSD: head/sys/netgraph/ng_ether.c 238844 2012-07-27 13:57:28Z emaste $
| 42 * $FreeBSD: head/sys/netgraph/ng_ether.c 241686 2012-10-18 13:57:24Z andre $
|
43 */ 44 45/* 46 * ng_ether(4) netgraph node type 47 */ 48 49#include <sys/param.h> 50#include <sys/systm.h> 51#include <sys/kernel.h> 52#include <sys/malloc.h> 53#include <sys/mbuf.h> 54#include <sys/errno.h> 55#include <sys/proc.h> 56#include <sys/syslog.h> 57#include <sys/socket.h> 58#include <sys/taskqueue.h> 59 60#include <net/if.h> 61#include <net/if_dl.h> 62#include <net/if_types.h> 63#include <net/if_arp.h> 64#include <net/if_var.h> 65#include <net/ethernet.h> 66#include <net/if_bridgevar.h> 67#include <net/vnet.h> 68 69#include <netgraph/ng_message.h> 70#include <netgraph/netgraph.h> 71#include <netgraph/ng_parse.h> 72#include <netgraph/ng_ether.h> 73 74MODULE_VERSION(ng_ether, 1); 75 76#define IFP2NG(ifp) (IFP2AC((ifp))->ac_netgraph) 77 78/* Per-node private data */ 79struct private { 80 struct ifnet *ifp; /* associated interface */ 81 hook_p upper; /* upper hook connection */ 82 hook_p lower; /* lower hook connection */ 83 hook_p orphan; /* orphan hook connection */ 84 u_char autoSrcAddr; /* always overwrite source address */ 85 u_char promisc; /* promiscuous mode enabled */ 86 u_long hwassist; /* hardware checksum capabilities */ 87 u_int flags; /* flags e.g. really die */ 88}; 89typedef struct private *priv_p; 90 91/* Hook pointers used by if_ethersubr.c to callback to netgraph */ 92extern void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 93extern void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 94extern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 95extern void (*ng_ether_attach_p)(struct ifnet *ifp); 96extern void (*ng_ether_detach_p)(struct ifnet *ifp); 97extern void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 98 99/* Functional hooks called from if_ethersubr.c */ 100static void ng_ether_input(struct ifnet *ifp, struct mbuf **mp); 101static void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m); 102static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp); 103static void ng_ether_attach(struct ifnet *ifp); 104static void ng_ether_detach(struct ifnet *ifp); 105static void ng_ether_link_state(struct ifnet *ifp, int state); 106 107/* Other functions */ 108static int ng_ether_rcv_lower(hook_p node, item_p item); 109static int ng_ether_rcv_upper(hook_p node, item_p item); 110 111/* Netgraph node methods */ 112static ng_constructor_t ng_ether_constructor; 113static ng_rcvmsg_t ng_ether_rcvmsg; 114static ng_shutdown_t ng_ether_shutdown; 115static ng_newhook_t ng_ether_newhook; 116static ng_rcvdata_t ng_ether_rcvdata; 117static ng_disconnect_t ng_ether_disconnect; 118static int ng_ether_mod_event(module_t mod, int event, void *data); 119 120/* List of commands and how to convert arguments to/from ASCII */ 121static const struct ng_cmdlist ng_ether_cmdlist[] = { 122 { 123 NGM_ETHER_COOKIE, 124 NGM_ETHER_GET_IFNAME, 125 "getifname", 126 NULL, 127 &ng_parse_string_type 128 }, 129 { 130 NGM_ETHER_COOKIE, 131 NGM_ETHER_GET_IFINDEX, 132 "getifindex", 133 NULL, 134 &ng_parse_int32_type 135 }, 136 { 137 NGM_ETHER_COOKIE, 138 NGM_ETHER_GET_ENADDR, 139 "getenaddr", 140 NULL, 141 &ng_parse_enaddr_type 142 }, 143 { 144 NGM_ETHER_COOKIE, 145 NGM_ETHER_SET_ENADDR, 146 "setenaddr", 147 &ng_parse_enaddr_type, 148 NULL 149 }, 150 { 151 NGM_ETHER_COOKIE, 152 NGM_ETHER_GET_PROMISC, 153 "getpromisc", 154 NULL, 155 &ng_parse_int32_type 156 }, 157 { 158 NGM_ETHER_COOKIE, 159 NGM_ETHER_SET_PROMISC, 160 "setpromisc", 161 &ng_parse_int32_type, 162 NULL 163 }, 164 { 165 NGM_ETHER_COOKIE, 166 NGM_ETHER_GET_AUTOSRC, 167 "getautosrc", 168 NULL, 169 &ng_parse_int32_type 170 }, 171 { 172 NGM_ETHER_COOKIE, 173 NGM_ETHER_SET_AUTOSRC, 174 "setautosrc", 175 &ng_parse_int32_type, 176 NULL 177 }, 178 { 179 NGM_ETHER_COOKIE, 180 NGM_ETHER_ADD_MULTI, 181 "addmulti", 182 &ng_parse_enaddr_type, 183 NULL 184 }, 185 { 186 NGM_ETHER_COOKIE, 187 NGM_ETHER_DEL_MULTI, 188 "delmulti", 189 &ng_parse_enaddr_type, 190 NULL 191 }, 192 { 193 NGM_ETHER_COOKIE, 194 NGM_ETHER_DETACH, 195 "detach", 196 NULL, 197 NULL 198 }, 199 { 0 } 200}; 201 202static struct ng_type ng_ether_typestruct = { 203 .version = NG_ABI_VERSION, 204 .name = NG_ETHER_NODE_TYPE, 205 .mod_event = ng_ether_mod_event, 206 .constructor = ng_ether_constructor, 207 .rcvmsg = ng_ether_rcvmsg, 208 .shutdown = ng_ether_shutdown, 209 .newhook = ng_ether_newhook, 210 .rcvdata = ng_ether_rcvdata, 211 .disconnect = ng_ether_disconnect, 212 .cmdlist = ng_ether_cmdlist, 213}; 214NETGRAPH_INIT(ether, &ng_ether_typestruct); 215 216/****************************************************************** 217 ETHERNET FUNCTION HOOKS 218******************************************************************/ 219 220/* 221 * Handle a packet that has come in on an interface. We get to 222 * look at it here before any upper layer protocols do.
| 43 */ 44 45/* 46 * ng_ether(4) netgraph node type 47 */ 48 49#include <sys/param.h> 50#include <sys/systm.h> 51#include <sys/kernel.h> 52#include <sys/malloc.h> 53#include <sys/mbuf.h> 54#include <sys/errno.h> 55#include <sys/proc.h> 56#include <sys/syslog.h> 57#include <sys/socket.h> 58#include <sys/taskqueue.h> 59 60#include <net/if.h> 61#include <net/if_dl.h> 62#include <net/if_types.h> 63#include <net/if_arp.h> 64#include <net/if_var.h> 65#include <net/ethernet.h> 66#include <net/if_bridgevar.h> 67#include <net/vnet.h> 68 69#include <netgraph/ng_message.h> 70#include <netgraph/netgraph.h> 71#include <netgraph/ng_parse.h> 72#include <netgraph/ng_ether.h> 73 74MODULE_VERSION(ng_ether, 1); 75 76#define IFP2NG(ifp) (IFP2AC((ifp))->ac_netgraph) 77 78/* Per-node private data */ 79struct private { 80 struct ifnet *ifp; /* associated interface */ 81 hook_p upper; /* upper hook connection */ 82 hook_p lower; /* lower hook connection */ 83 hook_p orphan; /* orphan hook connection */ 84 u_char autoSrcAddr; /* always overwrite source address */ 85 u_char promisc; /* promiscuous mode enabled */ 86 u_long hwassist; /* hardware checksum capabilities */ 87 u_int flags; /* flags e.g. really die */ 88}; 89typedef struct private *priv_p; 90 91/* Hook pointers used by if_ethersubr.c to callback to netgraph */ 92extern void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 93extern void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 94extern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 95extern void (*ng_ether_attach_p)(struct ifnet *ifp); 96extern void (*ng_ether_detach_p)(struct ifnet *ifp); 97extern void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 98 99/* Functional hooks called from if_ethersubr.c */ 100static void ng_ether_input(struct ifnet *ifp, struct mbuf **mp); 101static void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m); 102static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp); 103static void ng_ether_attach(struct ifnet *ifp); 104static void ng_ether_detach(struct ifnet *ifp); 105static void ng_ether_link_state(struct ifnet *ifp, int state); 106 107/* Other functions */ 108static int ng_ether_rcv_lower(hook_p node, item_p item); 109static int ng_ether_rcv_upper(hook_p node, item_p item); 110 111/* Netgraph node methods */ 112static ng_constructor_t ng_ether_constructor; 113static ng_rcvmsg_t ng_ether_rcvmsg; 114static ng_shutdown_t ng_ether_shutdown; 115static ng_newhook_t ng_ether_newhook; 116static ng_rcvdata_t ng_ether_rcvdata; 117static ng_disconnect_t ng_ether_disconnect; 118static int ng_ether_mod_event(module_t mod, int event, void *data); 119 120/* List of commands and how to convert arguments to/from ASCII */ 121static const struct ng_cmdlist ng_ether_cmdlist[] = { 122 { 123 NGM_ETHER_COOKIE, 124 NGM_ETHER_GET_IFNAME, 125 "getifname", 126 NULL, 127 &ng_parse_string_type 128 }, 129 { 130 NGM_ETHER_COOKIE, 131 NGM_ETHER_GET_IFINDEX, 132 "getifindex", 133 NULL, 134 &ng_parse_int32_type 135 }, 136 { 137 NGM_ETHER_COOKIE, 138 NGM_ETHER_GET_ENADDR, 139 "getenaddr", 140 NULL, 141 &ng_parse_enaddr_type 142 }, 143 { 144 NGM_ETHER_COOKIE, 145 NGM_ETHER_SET_ENADDR, 146 "setenaddr", 147 &ng_parse_enaddr_type, 148 NULL 149 }, 150 { 151 NGM_ETHER_COOKIE, 152 NGM_ETHER_GET_PROMISC, 153 "getpromisc", 154 NULL, 155 &ng_parse_int32_type 156 }, 157 { 158 NGM_ETHER_COOKIE, 159 NGM_ETHER_SET_PROMISC, 160 "setpromisc", 161 &ng_parse_int32_type, 162 NULL 163 }, 164 { 165 NGM_ETHER_COOKIE, 166 NGM_ETHER_GET_AUTOSRC, 167 "getautosrc", 168 NULL, 169 &ng_parse_int32_type 170 }, 171 { 172 NGM_ETHER_COOKIE, 173 NGM_ETHER_SET_AUTOSRC, 174 "setautosrc", 175 &ng_parse_int32_type, 176 NULL 177 }, 178 { 179 NGM_ETHER_COOKIE, 180 NGM_ETHER_ADD_MULTI, 181 "addmulti", 182 &ng_parse_enaddr_type, 183 NULL 184 }, 185 { 186 NGM_ETHER_COOKIE, 187 NGM_ETHER_DEL_MULTI, 188 "delmulti", 189 &ng_parse_enaddr_type, 190 NULL 191 }, 192 { 193 NGM_ETHER_COOKIE, 194 NGM_ETHER_DETACH, 195 "detach", 196 NULL, 197 NULL 198 }, 199 { 0 } 200}; 201 202static struct ng_type ng_ether_typestruct = { 203 .version = NG_ABI_VERSION, 204 .name = NG_ETHER_NODE_TYPE, 205 .mod_event = ng_ether_mod_event, 206 .constructor = ng_ether_constructor, 207 .rcvmsg = ng_ether_rcvmsg, 208 .shutdown = ng_ether_shutdown, 209 .newhook = ng_ether_newhook, 210 .rcvdata = ng_ether_rcvdata, 211 .disconnect = ng_ether_disconnect, 212 .cmdlist = ng_ether_cmdlist, 213}; 214NETGRAPH_INIT(ether, &ng_ether_typestruct); 215 216/****************************************************************** 217 ETHERNET FUNCTION HOOKS 218******************************************************************/ 219 220/* 221 * Handle a packet that has come in on an interface. We get to 222 * look at it here before any upper layer protocols do.
|
223 * 224 * NOTE: this function will get called at splimp()
| |
225 */ 226static void 227ng_ether_input(struct ifnet *ifp, struct mbuf **mp) 228{ 229 const node_p node = IFP2NG(ifp); 230 const priv_p priv = NG_NODE_PRIVATE(node); 231 int error; 232 233 /* If "lower" hook not connected, let packet continue */ 234 if (priv->lower == NULL) 235 return; 236 NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */ 237} 238 239/* 240 * Handle a packet that has come in on an interface, and which 241 * does not match any of our known protocols (an ``orphan'').
| 223 */ 224static void 225ng_ether_input(struct ifnet *ifp, struct mbuf **mp) 226{ 227 const node_p node = IFP2NG(ifp); 228 const priv_p priv = NG_NODE_PRIVATE(node); 229 int error; 230 231 /* If "lower" hook not connected, let packet continue */ 232 if (priv->lower == NULL) 233 return; 234 NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */ 235} 236 237/* 238 * Handle a packet that has come in on an interface, and which 239 * does not match any of our known protocols (an ``orphan'').
|
242 * 243 * NOTE: this function will get called at splimp()
| |
244 */ 245static void 246ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m) 247{ 248 const node_p node = IFP2NG(ifp); 249 const priv_p priv = NG_NODE_PRIVATE(node); 250 int error; 251 252 /* If "orphan" hook not connected, discard packet */ 253 if (priv->orphan == NULL) { 254 m_freem(m); 255 return; 256 } 257 NG_SEND_DATA_ONLY(error, priv->orphan, m); 258} 259 260/* 261 * Handle a packet that is going out on an interface. 262 * The Ethernet header is already attached to the mbuf. 263 */ 264static int 265ng_ether_output(struct ifnet *ifp, struct mbuf **mp) 266{ 267 const node_p node = IFP2NG(ifp); 268 const priv_p priv = NG_NODE_PRIVATE(node); 269 int error = 0; 270 271 /* If "upper" hook not connected, let packet continue */ 272 if (priv->upper == NULL) 273 return (0); 274 275 /* Send it out "upper" hook */ 276 NG_OUTBOUND_THREAD_REF(); 277 NG_SEND_DATA_ONLY(error, priv->upper, *mp); 278 NG_OUTBOUND_THREAD_UNREF(); 279 return (error); 280} 281 282/* 283 * A new Ethernet interface has been attached. 284 * Create a new node for it, etc. 285 */ 286static void 287ng_ether_attach(struct ifnet *ifp) 288{ 289 priv_p priv; 290 node_p node; 291 292 /* 293 * Do not create / attach an ether node to this ifnet if 294 * a netgraph node with the same name already exists. 295 * This should prevent ether nodes to become attached to 296 * eiface nodes, which may be problematic due to naming 297 * clashes. 298 */ 299 if ((node = ng_name2noderef(NULL, ifp->if_xname)) != NULL) { 300 NG_NODE_UNREF(node); 301 return; 302 } 303 304 /* Create node */ 305 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__)); 306 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 307 log(LOG_ERR, "%s: can't %s for %s\n", 308 __func__, "create node", ifp->if_xname); 309 return; 310 } 311 312 /* Allocate private data */ 313 priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 314 if (priv == NULL) { 315 log(LOG_ERR, "%s: can't %s for %s\n", 316 __func__, "allocate memory", ifp->if_xname); 317 NG_NODE_UNREF(node); 318 return; 319 } 320 NG_NODE_SET_PRIVATE(node, priv); 321 priv->ifp = ifp; 322 IFP2NG(ifp) = node; 323 priv->hwassist = ifp->if_hwassist; 324 325 /* Try to give the node the same name as the interface */ 326 if (ng_name_node(node, ifp->if_xname) != 0) { 327 log(LOG_WARNING, "%s: can't name node %s\n", 328 __func__, ifp->if_xname); 329 } 330} 331 332/* 333 * An Ethernet interface is being detached. 334 * REALLY Destroy its node. 335 */ 336static void 337ng_ether_detach(struct ifnet *ifp) 338{ 339 const node_p node = IFP2NG(ifp); 340 const priv_p priv = NG_NODE_PRIVATE(node); 341 342 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 343 NG_NODE_REALLY_DIE(node); /* Force real removal of node */ 344 /* 345 * We can't assume the ifnet is still around when we run shutdown 346 * So zap it now. XXX We HOPE that anything running at this time 347 * handles it (as it should in the non netgraph case). 348 */ 349 IFP2NG(ifp) = NULL; 350 priv->ifp = NULL; /* XXX race if interrupted an output packet */ 351 ng_rmnode_self(node); /* remove all netgraph parts */ 352} 353 354/* 355 * Notify graph about link event. 356 * if_link_state_change() has already checked that the state has changed. 357 */ 358static void 359ng_ether_link_state(struct ifnet *ifp, int state) 360{ 361 const node_p node = IFP2NG(ifp); 362 const priv_p priv = NG_NODE_PRIVATE(node); 363 struct ng_mesg *msg; 364 int cmd, dummy_error = 0; 365 366 if (state == LINK_STATE_UP) 367 cmd = NGM_LINK_IS_UP; 368 else if (state == LINK_STATE_DOWN) 369 cmd = NGM_LINK_IS_DOWN; 370 else 371 return; 372 373 if (priv->lower != NULL) { 374 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT); 375 if (msg != NULL) 376 NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0); 377 } 378 if (priv->orphan != NULL) { 379 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT); 380 if (msg != NULL) 381 NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->orphan, 0); 382 } 383} 384 385/****************************************************************** 386 NETGRAPH NODE METHODS 387******************************************************************/ 388 389/* 390 * It is not possible or allowable to create a node of this type. 391 * Nodes get created when the interface is attached (or, when 392 * this node type's KLD is loaded). 393 */ 394static int 395ng_ether_constructor(node_p node) 396{ 397 return (EINVAL); 398} 399 400/* 401 * Check for attaching a new hook. 402 */ 403static int 404ng_ether_newhook(node_p node, hook_p hook, const char *name) 405{ 406 const priv_p priv = NG_NODE_PRIVATE(node); 407 hook_p *hookptr; 408 409 /* Divert hook is an alias for lower */ 410 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0) 411 name = NG_ETHER_HOOK_LOWER; 412 413 /* Which hook? */ 414 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) { 415 hookptr = &priv->upper; 416 NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_upper); 417 NG_HOOK_SET_TO_INBOUND(hook); 418 } else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) { 419 hookptr = &priv->lower; 420 NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower); 421 } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) { 422 hookptr = &priv->orphan; 423 NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower); 424 } else 425 return (EINVAL); 426 427 /* Check if already connected (shouldn't be, but doesn't hurt) */ 428 if (*hookptr != NULL) 429 return (EISCONN); 430 431 /* Disable hardware checksums while 'upper' hook is connected */ 432 if (hookptr == &priv->upper) 433 priv->ifp->if_hwassist = 0; 434 NG_HOOK_HI_STACK(hook); 435 /* OK */ 436 *hookptr = hook; 437 return (0); 438} 439 440/* 441 * Receive an incoming control message. 442 */ 443static int 444ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook) 445{ 446 const priv_p priv = NG_NODE_PRIVATE(node); 447 struct ng_mesg *resp = NULL; 448 int error = 0; 449 struct ng_mesg *msg; 450 451 NGI_GET_MSG(item, msg); 452 switch (msg->header.typecookie) { 453 case NGM_ETHER_COOKIE: 454 switch (msg->header.cmd) { 455 case NGM_ETHER_GET_IFNAME: 456 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 457 if (resp == NULL) { 458 error = ENOMEM; 459 break; 460 } 461 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ); 462 break; 463 case NGM_ETHER_GET_IFINDEX: 464 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 465 if (resp == NULL) { 466 error = ENOMEM; 467 break; 468 } 469 *((u_int32_t *)resp->data) = priv->ifp->if_index; 470 break; 471 case NGM_ETHER_GET_ENADDR: 472 NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT); 473 if (resp == NULL) { 474 error = ENOMEM; 475 break; 476 } 477 bcopy(IF_LLADDR(priv->ifp), 478 resp->data, ETHER_ADDR_LEN); 479 break; 480 case NGM_ETHER_SET_ENADDR: 481 { 482 if (msg->header.arglen != ETHER_ADDR_LEN) { 483 error = EINVAL; 484 break; 485 } 486 error = if_setlladdr(priv->ifp, 487 (u_char *)msg->data, ETHER_ADDR_LEN); 488 EVENTHANDLER_INVOKE(iflladdr_event, priv->ifp); 489 break; 490 } 491 case NGM_ETHER_GET_PROMISC: 492 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 493 if (resp == NULL) { 494 error = ENOMEM; 495 break; 496 } 497 *((u_int32_t *)resp->data) = priv->promisc; 498 break; 499 case NGM_ETHER_SET_PROMISC: 500 { 501 u_char want; 502 503 if (msg->header.arglen != sizeof(u_int32_t)) { 504 error = EINVAL; 505 break; 506 } 507 want = !!*((u_int32_t *)msg->data); 508 if (want ^ priv->promisc) { 509 if ((error = ifpromisc(priv->ifp, want)) != 0) 510 break; 511 priv->promisc = want; 512 } 513 break; 514 } 515 case NGM_ETHER_GET_AUTOSRC: 516 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 517 if (resp == NULL) { 518 error = ENOMEM; 519 break; 520 } 521 *((u_int32_t *)resp->data) = priv->autoSrcAddr; 522 break; 523 case NGM_ETHER_SET_AUTOSRC: 524 if (msg->header.arglen != sizeof(u_int32_t)) { 525 error = EINVAL; 526 break; 527 } 528 priv->autoSrcAddr = !!*((u_int32_t *)msg->data); 529 break; 530 case NGM_ETHER_ADD_MULTI: 531 { 532 struct sockaddr_dl sa_dl; 533 struct ifmultiaddr *ifma; 534 535 if (msg->header.arglen != ETHER_ADDR_LEN) { 536 error = EINVAL; 537 break; 538 } 539 bzero(&sa_dl, sizeof(struct sockaddr_dl)); 540 sa_dl.sdl_len = sizeof(struct sockaddr_dl); 541 sa_dl.sdl_family = AF_LINK; 542 sa_dl.sdl_alen = ETHER_ADDR_LEN; 543 bcopy((void *)msg->data, LLADDR(&sa_dl), 544 ETHER_ADDR_LEN); 545 /* 546 * Netgraph is only permitted to join groups once 547 * via the if_addmulti() KPI, because it cannot hold 548 * struct ifmultiaddr * between calls. It may also 549 * lose a race while we check if the membership 550 * already exists. 551 */ 552 if_maddr_rlock(priv->ifp); 553 ifma = if_findmulti(priv->ifp, 554 (struct sockaddr *)&sa_dl); 555 if_maddr_runlock(priv->ifp); 556 if (ifma != NULL) { 557 error = EADDRINUSE; 558 } else { 559 error = if_addmulti(priv->ifp, 560 (struct sockaddr *)&sa_dl, &ifma); 561 } 562 break; 563 } 564 case NGM_ETHER_DEL_MULTI: 565 { 566 struct sockaddr_dl sa_dl; 567 568 if (msg->header.arglen != ETHER_ADDR_LEN) { 569 error = EINVAL; 570 break; 571 } 572 bzero(&sa_dl, sizeof(struct sockaddr_dl)); 573 sa_dl.sdl_len = sizeof(struct sockaddr_dl); 574 sa_dl.sdl_family = AF_LINK; 575 sa_dl.sdl_alen = ETHER_ADDR_LEN; 576 bcopy((void *)msg->data, LLADDR(&sa_dl), 577 ETHER_ADDR_LEN); 578 error = if_delmulti(priv->ifp, 579 (struct sockaddr *)&sa_dl); 580 break; 581 } 582 case NGM_ETHER_DETACH: 583 ng_ether_detach(priv->ifp); 584 break; 585 default: 586 error = EINVAL; 587 break; 588 } 589 break; 590 default: 591 error = EINVAL; 592 break; 593 } 594 NG_RESPOND_MSG(error, node, item, resp); 595 NG_FREE_MSG(msg); 596 return (error); 597} 598 599/* 600 * Receive data on a hook. 601 * Since we use per-hook recveive methods this should never be called. 602 */ 603static int 604ng_ether_rcvdata(hook_p hook, item_p item) 605{ 606 NG_FREE_ITEM(item); 607 608 panic("%s: weird hook", __func__); 609} 610 611/* 612 * Handle an mbuf received on the "lower" or "orphan" hook. 613 */ 614static int 615ng_ether_rcv_lower(hook_p hook, item_p item) 616{ 617 struct mbuf *m; 618 const node_p node = NG_HOOK_NODE(hook); 619 const priv_p priv = NG_NODE_PRIVATE(node); 620 struct ifnet *const ifp = priv->ifp; 621 622 NGI_GET_M(item, m); 623 NG_FREE_ITEM(item); 624 625 /* Check whether interface is ready for packets */ 626 627 if (!((ifp->if_flags & IFF_UP) && 628 (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 629 NG_FREE_M(m); 630 return (ENETDOWN); 631 } 632 633 /* Make sure header is fully pulled up */ 634 if (m->m_pkthdr.len < sizeof(struct ether_header)) { 635 NG_FREE_M(m); 636 return (EINVAL); 637 } 638 if (m->m_len < sizeof(struct ether_header) 639 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 640 return (ENOBUFS); 641 642 /* Drop in the MAC address if desired */ 643 if (priv->autoSrcAddr) { 644 645 /* Make the mbuf writable if it's not already */ 646 if (!M_WRITABLE(m) 647 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 648 return (ENOBUFS); 649 650 /* Overwrite source MAC address */ 651 bcopy(IF_LLADDR(ifp), 652 mtod(m, struct ether_header *)->ether_shost, 653 ETHER_ADDR_LEN); 654 } 655 656 /* Send it on its way */ 657 return ether_output_frame(ifp, m); 658} 659 660/* 661 * Handle an mbuf received on the "upper" hook. 662 */ 663static int 664ng_ether_rcv_upper(hook_p hook, item_p item) 665{ 666 struct mbuf *m; 667 const node_p node = NG_HOOK_NODE(hook); 668 const priv_p priv = NG_NODE_PRIVATE(node); 669 struct ifnet *ifp = priv->ifp; 670 671 NGI_GET_M(item, m); 672 NG_FREE_ITEM(item); 673 674 /* Check length and pull off header */ 675 if (m->m_pkthdr.len < sizeof(struct ether_header)) { 676 NG_FREE_M(m); 677 return (EINVAL); 678 } 679 if (m->m_len < sizeof(struct ether_header) && 680 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 681 return (ENOBUFS); 682 683 m->m_pkthdr.rcvif = ifp; 684 685 /* Pass the packet to the bridge, it may come back to us */ 686 if (ifp->if_bridge) { 687 BRIDGE_INPUT(ifp, m); 688 if (m == NULL) 689 return (0); 690 } 691 692 /* Route packet back in */ 693 ether_demux(ifp, m); 694 return (0); 695} 696 697/* 698 * Shutdown node. This resets the node but does not remove it 699 * unless the REALLY_DIE flag is set. 700 */ 701static int 702ng_ether_shutdown(node_p node) 703{ 704 const priv_p priv = NG_NODE_PRIVATE(node); 705 706 if (node->nd_flags & NGF_REALLY_DIE) { 707 /* 708 * WE came here because the ethernet card is being unloaded, 709 * so stop being persistant. 710 * Actually undo all the things we did on creation. 711 * Assume the ifp has already been freed. 712 */ 713 NG_NODE_SET_PRIVATE(node, NULL); 714 free(priv, M_NETGRAPH); 715 NG_NODE_UNREF(node); /* free node itself */ 716 return (0); 717 } 718 if (priv->promisc) { /* disable promiscuous mode */ 719 (void)ifpromisc(priv->ifp, 0); 720 priv->promisc = 0; 721 } 722 NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */ 723 724 return (0); 725} 726 727/* 728 * Hook disconnection. 729 */ 730static int 731ng_ether_disconnect(hook_p hook) 732{ 733 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 734 735 if (hook == priv->upper) { 736 priv->upper = NULL; 737 if (priv->ifp != NULL) /* restore h/w csum */ 738 priv->ifp->if_hwassist = priv->hwassist; 739 } else if (hook == priv->lower) 740 priv->lower = NULL; 741 else if (hook == priv->orphan) 742 priv->orphan = NULL; 743 else 744 panic("%s: weird hook", __func__); 745 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 746 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 747 ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */ 748 return (0); 749} 750 751/****************************************************************** 752 INITIALIZATION 753******************************************************************/ 754 755/* 756 * Handle loading and unloading for this node type. 757 */ 758static int 759ng_ether_mod_event(module_t mod, int event, void *data) 760{ 761 int error = 0;
| 240 */ 241static void 242ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m) 243{ 244 const node_p node = IFP2NG(ifp); 245 const priv_p priv = NG_NODE_PRIVATE(node); 246 int error; 247 248 /* If "orphan" hook not connected, discard packet */ 249 if (priv->orphan == NULL) { 250 m_freem(m); 251 return; 252 } 253 NG_SEND_DATA_ONLY(error, priv->orphan, m); 254} 255 256/* 257 * Handle a packet that is going out on an interface. 258 * The Ethernet header is already attached to the mbuf. 259 */ 260static int 261ng_ether_output(struct ifnet *ifp, struct mbuf **mp) 262{ 263 const node_p node = IFP2NG(ifp); 264 const priv_p priv = NG_NODE_PRIVATE(node); 265 int error = 0; 266 267 /* If "upper" hook not connected, let packet continue */ 268 if (priv->upper == NULL) 269 return (0); 270 271 /* Send it out "upper" hook */ 272 NG_OUTBOUND_THREAD_REF(); 273 NG_SEND_DATA_ONLY(error, priv->upper, *mp); 274 NG_OUTBOUND_THREAD_UNREF(); 275 return (error); 276} 277 278/* 279 * A new Ethernet interface has been attached. 280 * Create a new node for it, etc. 281 */ 282static void 283ng_ether_attach(struct ifnet *ifp) 284{ 285 priv_p priv; 286 node_p node; 287 288 /* 289 * Do not create / attach an ether node to this ifnet if 290 * a netgraph node with the same name already exists. 291 * This should prevent ether nodes to become attached to 292 * eiface nodes, which may be problematic due to naming 293 * clashes. 294 */ 295 if ((node = ng_name2noderef(NULL, ifp->if_xname)) != NULL) { 296 NG_NODE_UNREF(node); 297 return; 298 } 299 300 /* Create node */ 301 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__)); 302 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 303 log(LOG_ERR, "%s: can't %s for %s\n", 304 __func__, "create node", ifp->if_xname); 305 return; 306 } 307 308 /* Allocate private data */ 309 priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 310 if (priv == NULL) { 311 log(LOG_ERR, "%s: can't %s for %s\n", 312 __func__, "allocate memory", ifp->if_xname); 313 NG_NODE_UNREF(node); 314 return; 315 } 316 NG_NODE_SET_PRIVATE(node, priv); 317 priv->ifp = ifp; 318 IFP2NG(ifp) = node; 319 priv->hwassist = ifp->if_hwassist; 320 321 /* Try to give the node the same name as the interface */ 322 if (ng_name_node(node, ifp->if_xname) != 0) { 323 log(LOG_WARNING, "%s: can't name node %s\n", 324 __func__, ifp->if_xname); 325 } 326} 327 328/* 329 * An Ethernet interface is being detached. 330 * REALLY Destroy its node. 331 */ 332static void 333ng_ether_detach(struct ifnet *ifp) 334{ 335 const node_p node = IFP2NG(ifp); 336 const priv_p priv = NG_NODE_PRIVATE(node); 337 338 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 339 NG_NODE_REALLY_DIE(node); /* Force real removal of node */ 340 /* 341 * We can't assume the ifnet is still around when we run shutdown 342 * So zap it now. XXX We HOPE that anything running at this time 343 * handles it (as it should in the non netgraph case). 344 */ 345 IFP2NG(ifp) = NULL; 346 priv->ifp = NULL; /* XXX race if interrupted an output packet */ 347 ng_rmnode_self(node); /* remove all netgraph parts */ 348} 349 350/* 351 * Notify graph about link event. 352 * if_link_state_change() has already checked that the state has changed. 353 */ 354static void 355ng_ether_link_state(struct ifnet *ifp, int state) 356{ 357 const node_p node = IFP2NG(ifp); 358 const priv_p priv = NG_NODE_PRIVATE(node); 359 struct ng_mesg *msg; 360 int cmd, dummy_error = 0; 361 362 if (state == LINK_STATE_UP) 363 cmd = NGM_LINK_IS_UP; 364 else if (state == LINK_STATE_DOWN) 365 cmd = NGM_LINK_IS_DOWN; 366 else 367 return; 368 369 if (priv->lower != NULL) { 370 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT); 371 if (msg != NULL) 372 NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0); 373 } 374 if (priv->orphan != NULL) { 375 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT); 376 if (msg != NULL) 377 NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->orphan, 0); 378 } 379} 380 381/****************************************************************** 382 NETGRAPH NODE METHODS 383******************************************************************/ 384 385/* 386 * It is not possible or allowable to create a node of this type. 387 * Nodes get created when the interface is attached (or, when 388 * this node type's KLD is loaded). 389 */ 390static int 391ng_ether_constructor(node_p node) 392{ 393 return (EINVAL); 394} 395 396/* 397 * Check for attaching a new hook. 398 */ 399static int 400ng_ether_newhook(node_p node, hook_p hook, const char *name) 401{ 402 const priv_p priv = NG_NODE_PRIVATE(node); 403 hook_p *hookptr; 404 405 /* Divert hook is an alias for lower */ 406 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0) 407 name = NG_ETHER_HOOK_LOWER; 408 409 /* Which hook? */ 410 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) { 411 hookptr = &priv->upper; 412 NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_upper); 413 NG_HOOK_SET_TO_INBOUND(hook); 414 } else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) { 415 hookptr = &priv->lower; 416 NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower); 417 } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) { 418 hookptr = &priv->orphan; 419 NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower); 420 } else 421 return (EINVAL); 422 423 /* Check if already connected (shouldn't be, but doesn't hurt) */ 424 if (*hookptr != NULL) 425 return (EISCONN); 426 427 /* Disable hardware checksums while 'upper' hook is connected */ 428 if (hookptr == &priv->upper) 429 priv->ifp->if_hwassist = 0; 430 NG_HOOK_HI_STACK(hook); 431 /* OK */ 432 *hookptr = hook; 433 return (0); 434} 435 436/* 437 * Receive an incoming control message. 438 */ 439static int 440ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook) 441{ 442 const priv_p priv = NG_NODE_PRIVATE(node); 443 struct ng_mesg *resp = NULL; 444 int error = 0; 445 struct ng_mesg *msg; 446 447 NGI_GET_MSG(item, msg); 448 switch (msg->header.typecookie) { 449 case NGM_ETHER_COOKIE: 450 switch (msg->header.cmd) { 451 case NGM_ETHER_GET_IFNAME: 452 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 453 if (resp == NULL) { 454 error = ENOMEM; 455 break; 456 } 457 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ); 458 break; 459 case NGM_ETHER_GET_IFINDEX: 460 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 461 if (resp == NULL) { 462 error = ENOMEM; 463 break; 464 } 465 *((u_int32_t *)resp->data) = priv->ifp->if_index; 466 break; 467 case NGM_ETHER_GET_ENADDR: 468 NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT); 469 if (resp == NULL) { 470 error = ENOMEM; 471 break; 472 } 473 bcopy(IF_LLADDR(priv->ifp), 474 resp->data, ETHER_ADDR_LEN); 475 break; 476 case NGM_ETHER_SET_ENADDR: 477 { 478 if (msg->header.arglen != ETHER_ADDR_LEN) { 479 error = EINVAL; 480 break; 481 } 482 error = if_setlladdr(priv->ifp, 483 (u_char *)msg->data, ETHER_ADDR_LEN); 484 EVENTHANDLER_INVOKE(iflladdr_event, priv->ifp); 485 break; 486 } 487 case NGM_ETHER_GET_PROMISC: 488 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 489 if (resp == NULL) { 490 error = ENOMEM; 491 break; 492 } 493 *((u_int32_t *)resp->data) = priv->promisc; 494 break; 495 case NGM_ETHER_SET_PROMISC: 496 { 497 u_char want; 498 499 if (msg->header.arglen != sizeof(u_int32_t)) { 500 error = EINVAL; 501 break; 502 } 503 want = !!*((u_int32_t *)msg->data); 504 if (want ^ priv->promisc) { 505 if ((error = ifpromisc(priv->ifp, want)) != 0) 506 break; 507 priv->promisc = want; 508 } 509 break; 510 } 511 case NGM_ETHER_GET_AUTOSRC: 512 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 513 if (resp == NULL) { 514 error = ENOMEM; 515 break; 516 } 517 *((u_int32_t *)resp->data) = priv->autoSrcAddr; 518 break; 519 case NGM_ETHER_SET_AUTOSRC: 520 if (msg->header.arglen != sizeof(u_int32_t)) { 521 error = EINVAL; 522 break; 523 } 524 priv->autoSrcAddr = !!*((u_int32_t *)msg->data); 525 break; 526 case NGM_ETHER_ADD_MULTI: 527 { 528 struct sockaddr_dl sa_dl; 529 struct ifmultiaddr *ifma; 530 531 if (msg->header.arglen != ETHER_ADDR_LEN) { 532 error = EINVAL; 533 break; 534 } 535 bzero(&sa_dl, sizeof(struct sockaddr_dl)); 536 sa_dl.sdl_len = sizeof(struct sockaddr_dl); 537 sa_dl.sdl_family = AF_LINK; 538 sa_dl.sdl_alen = ETHER_ADDR_LEN; 539 bcopy((void *)msg->data, LLADDR(&sa_dl), 540 ETHER_ADDR_LEN); 541 /* 542 * Netgraph is only permitted to join groups once 543 * via the if_addmulti() KPI, because it cannot hold 544 * struct ifmultiaddr * between calls. It may also 545 * lose a race while we check if the membership 546 * already exists. 547 */ 548 if_maddr_rlock(priv->ifp); 549 ifma = if_findmulti(priv->ifp, 550 (struct sockaddr *)&sa_dl); 551 if_maddr_runlock(priv->ifp); 552 if (ifma != NULL) { 553 error = EADDRINUSE; 554 } else { 555 error = if_addmulti(priv->ifp, 556 (struct sockaddr *)&sa_dl, &ifma); 557 } 558 break; 559 } 560 case NGM_ETHER_DEL_MULTI: 561 { 562 struct sockaddr_dl sa_dl; 563 564 if (msg->header.arglen != ETHER_ADDR_LEN) { 565 error = EINVAL; 566 break; 567 } 568 bzero(&sa_dl, sizeof(struct sockaddr_dl)); 569 sa_dl.sdl_len = sizeof(struct sockaddr_dl); 570 sa_dl.sdl_family = AF_LINK; 571 sa_dl.sdl_alen = ETHER_ADDR_LEN; 572 bcopy((void *)msg->data, LLADDR(&sa_dl), 573 ETHER_ADDR_LEN); 574 error = if_delmulti(priv->ifp, 575 (struct sockaddr *)&sa_dl); 576 break; 577 } 578 case NGM_ETHER_DETACH: 579 ng_ether_detach(priv->ifp); 580 break; 581 default: 582 error = EINVAL; 583 break; 584 } 585 break; 586 default: 587 error = EINVAL; 588 break; 589 } 590 NG_RESPOND_MSG(error, node, item, resp); 591 NG_FREE_MSG(msg); 592 return (error); 593} 594 595/* 596 * Receive data on a hook. 597 * Since we use per-hook recveive methods this should never be called. 598 */ 599static int 600ng_ether_rcvdata(hook_p hook, item_p item) 601{ 602 NG_FREE_ITEM(item); 603 604 panic("%s: weird hook", __func__); 605} 606 607/* 608 * Handle an mbuf received on the "lower" or "orphan" hook. 609 */ 610static int 611ng_ether_rcv_lower(hook_p hook, item_p item) 612{ 613 struct mbuf *m; 614 const node_p node = NG_HOOK_NODE(hook); 615 const priv_p priv = NG_NODE_PRIVATE(node); 616 struct ifnet *const ifp = priv->ifp; 617 618 NGI_GET_M(item, m); 619 NG_FREE_ITEM(item); 620 621 /* Check whether interface is ready for packets */ 622 623 if (!((ifp->if_flags & IFF_UP) && 624 (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 625 NG_FREE_M(m); 626 return (ENETDOWN); 627 } 628 629 /* Make sure header is fully pulled up */ 630 if (m->m_pkthdr.len < sizeof(struct ether_header)) { 631 NG_FREE_M(m); 632 return (EINVAL); 633 } 634 if (m->m_len < sizeof(struct ether_header) 635 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 636 return (ENOBUFS); 637 638 /* Drop in the MAC address if desired */ 639 if (priv->autoSrcAddr) { 640 641 /* Make the mbuf writable if it's not already */ 642 if (!M_WRITABLE(m) 643 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 644 return (ENOBUFS); 645 646 /* Overwrite source MAC address */ 647 bcopy(IF_LLADDR(ifp), 648 mtod(m, struct ether_header *)->ether_shost, 649 ETHER_ADDR_LEN); 650 } 651 652 /* Send it on its way */ 653 return ether_output_frame(ifp, m); 654} 655 656/* 657 * Handle an mbuf received on the "upper" hook. 658 */ 659static int 660ng_ether_rcv_upper(hook_p hook, item_p item) 661{ 662 struct mbuf *m; 663 const node_p node = NG_HOOK_NODE(hook); 664 const priv_p priv = NG_NODE_PRIVATE(node); 665 struct ifnet *ifp = priv->ifp; 666 667 NGI_GET_M(item, m); 668 NG_FREE_ITEM(item); 669 670 /* Check length and pull off header */ 671 if (m->m_pkthdr.len < sizeof(struct ether_header)) { 672 NG_FREE_M(m); 673 return (EINVAL); 674 } 675 if (m->m_len < sizeof(struct ether_header) && 676 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 677 return (ENOBUFS); 678 679 m->m_pkthdr.rcvif = ifp; 680 681 /* Pass the packet to the bridge, it may come back to us */ 682 if (ifp->if_bridge) { 683 BRIDGE_INPUT(ifp, m); 684 if (m == NULL) 685 return (0); 686 } 687 688 /* Route packet back in */ 689 ether_demux(ifp, m); 690 return (0); 691} 692 693/* 694 * Shutdown node. This resets the node but does not remove it 695 * unless the REALLY_DIE flag is set. 696 */ 697static int 698ng_ether_shutdown(node_p node) 699{ 700 const priv_p priv = NG_NODE_PRIVATE(node); 701 702 if (node->nd_flags & NGF_REALLY_DIE) { 703 /* 704 * WE came here because the ethernet card is being unloaded, 705 * so stop being persistant. 706 * Actually undo all the things we did on creation. 707 * Assume the ifp has already been freed. 708 */ 709 NG_NODE_SET_PRIVATE(node, NULL); 710 free(priv, M_NETGRAPH); 711 NG_NODE_UNREF(node); /* free node itself */ 712 return (0); 713 } 714 if (priv->promisc) { /* disable promiscuous mode */ 715 (void)ifpromisc(priv->ifp, 0); 716 priv->promisc = 0; 717 } 718 NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */ 719 720 return (0); 721} 722 723/* 724 * Hook disconnection. 725 */ 726static int 727ng_ether_disconnect(hook_p hook) 728{ 729 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 730 731 if (hook == priv->upper) { 732 priv->upper = NULL; 733 if (priv->ifp != NULL) /* restore h/w csum */ 734 priv->ifp->if_hwassist = priv->hwassist; 735 } else if (hook == priv->lower) 736 priv->lower = NULL; 737 else if (hook == priv->orphan) 738 priv->orphan = NULL; 739 else 740 panic("%s: weird hook", __func__); 741 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 742 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 743 ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */ 744 return (0); 745} 746 747/****************************************************************** 748 INITIALIZATION 749******************************************************************/ 750 751/* 752 * Handle loading and unloading for this node type. 753 */ 754static int 755ng_ether_mod_event(module_t mod, int event, void *data) 756{ 757 int error = 0;
|
762 int s;
| |
763
| 758
|
764 s = splnet();
| |
765 switch (event) { 766 case MOD_LOAD: 767 768 /* Register function hooks */ 769 if (ng_ether_attach_p != NULL) { 770 error = EEXIST; 771 break; 772 } 773 ng_ether_attach_p = ng_ether_attach; 774 ng_ether_detach_p = ng_ether_detach; 775 ng_ether_output_p = ng_ether_output; 776 ng_ether_input_p = ng_ether_input; 777 ng_ether_input_orphan_p = ng_ether_input_orphan; 778 ng_ether_link_state_p = ng_ether_link_state; 779 780 break; 781 782 case MOD_UNLOAD: 783 784 /* 785 * Note that the base code won't try to unload us until 786 * all nodes have been removed, and that can't happen 787 * until all Ethernet interfaces are removed. In any 788 * case, we know there are no nodes left if the action 789 * is MOD_UNLOAD, so there's no need to detach any nodes. 790 */ 791 792 /* Unregister function hooks */ 793 ng_ether_attach_p = NULL; 794 ng_ether_detach_p = NULL; 795 ng_ether_output_p = NULL; 796 ng_ether_input_p = NULL; 797 ng_ether_input_orphan_p = NULL; 798 ng_ether_link_state_p = NULL; 799 break; 800 801 default: 802 error = EOPNOTSUPP; 803 break; 804 }
| 759 switch (event) { 760 case MOD_LOAD: 761 762 /* Register function hooks */ 763 if (ng_ether_attach_p != NULL) { 764 error = EEXIST; 765 break; 766 } 767 ng_ether_attach_p = ng_ether_attach; 768 ng_ether_detach_p = ng_ether_detach; 769 ng_ether_output_p = ng_ether_output; 770 ng_ether_input_p = ng_ether_input; 771 ng_ether_input_orphan_p = ng_ether_input_orphan; 772 ng_ether_link_state_p = ng_ether_link_state; 773 774 break; 775 776 case MOD_UNLOAD: 777 778 /* 779 * Note that the base code won't try to unload us until 780 * all nodes have been removed, and that can't happen 781 * until all Ethernet interfaces are removed. In any 782 * case, we know there are no nodes left if the action 783 * is MOD_UNLOAD, so there's no need to detach any nodes. 784 */ 785 786 /* Unregister function hooks */ 787 ng_ether_attach_p = NULL; 788 ng_ether_detach_p = NULL; 789 ng_ether_output_p = NULL; 790 ng_ether_input_p = NULL; 791 ng_ether_input_orphan_p = NULL; 792 ng_ether_link_state_p = NULL; 793 break; 794 795 default: 796 error = EOPNOTSUPP; 797 break; 798 }
|
805 splx(s);
| |
806 return (error); 807} 808 809static void 810vnet_ng_ether_init(const void *unused) 811{ 812 struct ifnet *ifp; 813 814 /* If module load was rejected, don't attach to vnets. */ 815 if (ng_ether_attach_p != ng_ether_attach) 816 return; 817 818 /* Create nodes for any already-existing Ethernet interfaces. */ 819 IFNET_RLOCK(); 820 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 821 if (ifp->if_type == IFT_ETHER 822 || ifp->if_type == IFT_L2VLAN) 823 ng_ether_attach(ifp); 824 } 825 IFNET_RUNLOCK(); 826} 827VNET_SYSINIT(vnet_ng_ether_init, SI_SUB_PSEUDO, SI_ORDER_ANY, 828 vnet_ng_ether_init, NULL);
| 799 return (error); 800} 801 802static void 803vnet_ng_ether_init(const void *unused) 804{ 805 struct ifnet *ifp; 806 807 /* If module load was rejected, don't attach to vnets. */ 808 if (ng_ether_attach_p != ng_ether_attach) 809 return; 810 811 /* Create nodes for any already-existing Ethernet interfaces. */ 812 IFNET_RLOCK(); 813 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 814 if (ifp->if_type == IFT_ETHER 815 || ifp->if_type == IFT_L2VLAN) 816 ng_ether_attach(ifp); 817 } 818 IFNET_RUNLOCK(); 819} 820VNET_SYSINIT(vnet_ng_ether_init, SI_SUB_PSEUDO, SI_ORDER_ANY, 821 vnet_ng_ether_init, NULL);
|