ng_ether.c revision 201924
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 201924 2010-01-09 19:03:48Z fjoe $ 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 59#include <net/if.h> 60#include <net/if_dl.h> 61#include <net/if_types.h> 62#include <net/if_arp.h> 63#include <net/if_var.h> 64#include <net/ethernet.h> 65#include <net/if_bridgevar.h> 66#include <net/vnet.h> 67 68#include <netgraph/ng_message.h> 69#include <netgraph/netgraph.h> 70#include <netgraph/ng_parse.h> 71#include <netgraph/ng_ether.h> 72 73#define IFP2NG(ifp) (IFP2AC((ifp))->ac_netgraph) 74 75/* Per-node private data */ 76struct private { 77 struct ifnet *ifp; /* associated interface */ 78 hook_p upper; /* upper hook connection */ 79 hook_p lower; /* lower hook connection */ 80 hook_p orphan; /* orphan hook connection */ 81 u_char autoSrcAddr; /* always overwrite source address */ 82 u_char promisc; /* promiscuous mode enabled */ 83 u_long hwassist; /* hardware checksum capabilities */ 84 u_int flags; /* flags e.g. really die */ 85}; 86typedef struct private *priv_p; 87 88/* Hook pointers used by if_ethersubr.c to callback to netgraph */ 89extern void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 90extern void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 91extern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 92extern void (*ng_ether_attach_p)(struct ifnet *ifp); 93extern void (*ng_ether_detach_p)(struct ifnet *ifp); 94extern void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 95 96/* Functional hooks called from if_ethersubr.c */ 97static void ng_ether_input(struct ifnet *ifp, struct mbuf **mp); 98static void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m); 99static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp); 100static void ng_ether_attach(struct ifnet *ifp); 101static void ng_ether_detach(struct ifnet *ifp); 102static void ng_ether_link_state(struct ifnet *ifp, int state); 103 104/* Other functions */ 105static int ng_ether_rcv_lower(hook_p node, item_p item); 106static int ng_ether_rcv_upper(hook_p node, item_p item); 107 108/* Netgraph node methods */ 109static ng_constructor_t ng_ether_constructor; 110static ng_rcvmsg_t ng_ether_rcvmsg; 111static ng_shutdown_t ng_ether_shutdown; 112static ng_newhook_t ng_ether_newhook; 113static ng_rcvdata_t ng_ether_rcvdata; 114static ng_disconnect_t ng_ether_disconnect; 115static int ng_ether_mod_event(module_t mod, int event, void *data); 116 117/* List of commands and how to convert arguments to/from ASCII */ 118static const struct ng_cmdlist ng_ether_cmdlist[] = { 119 { 120 NGM_ETHER_COOKIE, 121 NGM_ETHER_GET_IFNAME, 122 "getifname", 123 NULL, 124 &ng_parse_string_type 125 }, 126 { 127 NGM_ETHER_COOKIE, 128 NGM_ETHER_GET_IFINDEX, 129 "getifindex", 130 NULL, 131 &ng_parse_int32_type 132 }, 133 { 134 NGM_ETHER_COOKIE, 135 NGM_ETHER_GET_ENADDR, 136 "getenaddr", 137 NULL, 138 &ng_parse_enaddr_type 139 }, 140 { 141 NGM_ETHER_COOKIE, 142 NGM_ETHER_SET_ENADDR, 143 "setenaddr", 144 &ng_parse_enaddr_type, 145 NULL 146 }, 147 { 148 NGM_ETHER_COOKIE, 149 NGM_ETHER_GET_PROMISC, 150 "getpromisc", 151 NULL, 152 &ng_parse_int32_type 153 }, 154 { 155 NGM_ETHER_COOKIE, 156 NGM_ETHER_SET_PROMISC, 157 "setpromisc", 158 &ng_parse_int32_type, 159 NULL 160 }, 161 { 162 NGM_ETHER_COOKIE, 163 NGM_ETHER_GET_AUTOSRC, 164 "getautosrc", 165 NULL, 166 &ng_parse_int32_type 167 }, 168 { 169 NGM_ETHER_COOKIE, 170 NGM_ETHER_SET_AUTOSRC, 171 "setautosrc", 172 &ng_parse_int32_type, 173 NULL 174 }, 175 { 176 NGM_ETHER_COOKIE, 177 NGM_ETHER_ADD_MULTI, 178 "addmulti", 179 &ng_parse_enaddr_type, 180 NULL 181 }, 182 { 183 NGM_ETHER_COOKIE, 184 NGM_ETHER_DEL_MULTI, 185 "delmulti", 186 &ng_parse_enaddr_type, 187 NULL 188 }, 189 { 190 NGM_ETHER_COOKIE, 191 NGM_ETHER_DETACH, 192 "detach", 193 NULL, 194 NULL 195 }, 196 { 0 } 197}; 198 199static struct ng_type ng_ether_typestruct = { 200 .version = NG_ABI_VERSION, 201 .name = NG_ETHER_NODE_TYPE, 202 .mod_event = ng_ether_mod_event, 203 .constructor = ng_ether_constructor, 204 .rcvmsg = ng_ether_rcvmsg, 205 .shutdown = ng_ether_shutdown, 206 .newhook = ng_ether_newhook, 207 .rcvdata = ng_ether_rcvdata, 208 .disconnect = ng_ether_disconnect, 209 .cmdlist = ng_ether_cmdlist, 210}; 211NETGRAPH_INIT(ether, &ng_ether_typestruct); 212 213/****************************************************************** 214 ETHERNET FUNCTION HOOKS 215******************************************************************/ 216 217/* 218 * Handle a packet that has come in on an interface. We get to 219 * look at it here before any upper layer protocols do. 220 * 221 * NOTE: this function will get called at splimp() 222 */ 223static void 224ng_ether_input(struct ifnet *ifp, struct mbuf **mp) 225{ 226 const node_p node = IFP2NG(ifp); 227 const priv_p priv = NG_NODE_PRIVATE(node); 228 int error; 229 230 /* If "lower" hook not connected, let packet continue */ 231 if (priv->lower == NULL) 232 return; 233 NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */ 234} 235 236/* 237 * Handle a packet that has come in on an interface, and which 238 * does not match any of our known protocols (an ``orphan''). 239 * 240 * NOTE: this function will get called at splimp() 241 */ 242static void 243ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m) 244{ 245 const node_p node = IFP2NG(ifp); 246 const priv_p priv = NG_NODE_PRIVATE(node); 247 int error; 248 249 /* If "orphan" hook not connected, discard packet */ 250 if (priv->orphan == NULL) { 251 m_freem(m); 252 return; 253 } 254 NG_SEND_DATA_ONLY(error, priv->orphan, m); 255} 256 257/* 258 * Handle a packet that is going out on an interface. 259 * The Ethernet header is already attached to the mbuf. 260 */ 261static int 262ng_ether_output(struct ifnet *ifp, struct mbuf **mp) 263{ 264 const node_p node = IFP2NG(ifp); 265 const priv_p priv = NG_NODE_PRIVATE(node); 266 int error = 0; 267 268 /* If "upper" hook not connected, let packet continue */ 269 if (priv->upper == NULL) 270 return (0); 271 272 /* Send it out "upper" hook */ 273 NG_OUTBOUND_THREAD_REF(); 274 NG_SEND_DATA_ONLY(error, priv->upper, *mp); 275 NG_OUTBOUND_THREAD_UNREF(); 276 return (error); 277} 278 279/* 280 * A new Ethernet interface has been attached. 281 * Create a new node for it, etc. 282 */ 283static void 284ng_ether_attach(struct ifnet *ifp) 285{ 286 priv_p priv; 287 node_p node; 288 289 /* 290 * Do not create / attach an ether node to this ifnet if 291 * a netgraph node with the same name already exists. 292 * This should prevent ether nodes to become attached to 293 * eiface nodes, which may be problematic due to naming 294 * clashes. 295 */ 296 if ((node = ng_name2noderef(NULL, ifp->if_xname)) != NULL) { 297 NG_NODE_UNREF(node); 298 return; 299 } 300 301 /* Create node */ 302 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__)); 303 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 304 log(LOG_ERR, "%s: can't %s for %s\n", 305 __func__, "create node", ifp->if_xname); 306 return; 307 } 308 309 /* Allocate private data */ 310 priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 311 if (priv == NULL) { 312 log(LOG_ERR, "%s: can't %s for %s\n", 313 __func__, "allocate memory", ifp->if_xname); 314 NG_NODE_UNREF(node); 315 return; 316 } 317 NG_NODE_SET_PRIVATE(node, priv); 318 priv->ifp = ifp; 319 IFP2NG(ifp) = node; 320 priv->hwassist = ifp->if_hwassist; 321 322 /* Try to give the node the same name as the interface */ 323 if (ng_name_node(node, ifp->if_xname) != 0) { 324 log(LOG_WARNING, "%s: can't name node %s\n", 325 __func__, ifp->if_xname); 326 } 327} 328 329/* 330 * An Ethernet interface is being detached. 331 * REALLY Destroy its node. 332 */ 333static void 334ng_ether_detach(struct ifnet *ifp) 335{ 336 const node_p node = IFP2NG(ifp); 337 const priv_p priv = NG_NODE_PRIVATE(node); 338 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 break; 485 } 486 case NGM_ETHER_GET_PROMISC: 487 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 488 if (resp == NULL) { 489 error = ENOMEM; 490 break; 491 } 492 *((u_int32_t *)resp->data) = priv->promisc; 493 break; 494 case NGM_ETHER_SET_PROMISC: 495 { 496 u_char want; 497 498 if (msg->header.arglen != sizeof(u_int32_t)) { 499 error = EINVAL; 500 break; 501 } 502 want = !!*((u_int32_t *)msg->data); 503 if (want ^ priv->promisc) { 504 if ((error = ifpromisc(priv->ifp, want)) != 0) 505 break; 506 priv->promisc = want; 507 } 508 break; 509 } 510 case NGM_ETHER_GET_AUTOSRC: 511 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 512 if (resp == NULL) { 513 error = ENOMEM; 514 break; 515 } 516 *((u_int32_t *)resp->data) = priv->autoSrcAddr; 517 break; 518 case NGM_ETHER_SET_AUTOSRC: 519 if (msg->header.arglen != sizeof(u_int32_t)) { 520 error = EINVAL; 521 break; 522 } 523 priv->autoSrcAddr = !!*((u_int32_t *)msg->data); 524 break; 525 case NGM_ETHER_ADD_MULTI: 526 { 527 struct sockaddr_dl sa_dl; 528 struct ifmultiaddr *ifma; 529 530 if (msg->header.arglen != ETHER_ADDR_LEN) { 531 error = EINVAL; 532 break; 533 } 534 bzero(&sa_dl, sizeof(struct sockaddr_dl)); 535 sa_dl.sdl_len = sizeof(struct sockaddr_dl); 536 sa_dl.sdl_family = AF_LINK; 537 sa_dl.sdl_alen = ETHER_ADDR_LEN; 538 bcopy((void *)msg->data, LLADDR(&sa_dl), 539 ETHER_ADDR_LEN); 540 /* 541 * Netgraph is only permitted to join groups once 542 * via the if_addmulti() KPI, because it cannot hold 543 * struct ifmultiaddr * between calls. It may also 544 * lose a race while we check if the membership 545 * already exists. 546 */ 547 if_maddr_rlock(priv->ifp); 548 ifma = if_findmulti(priv->ifp, 549 (struct sockaddr *)&sa_dl); 550 if_maddr_runlock(priv->ifp); 551 if (ifma != NULL) { 552 error = EADDRINUSE; 553 } else { 554 error = if_addmulti(priv->ifp, 555 (struct sockaddr *)&sa_dl, &ifma); 556 } 557 break; 558 } 559 case NGM_ETHER_DEL_MULTI: 560 { 561 struct sockaddr_dl sa_dl; 562 563 if (msg->header.arglen != ETHER_ADDR_LEN) { 564 error = EINVAL; 565 break; 566 } 567 bzero(&sa_dl, sizeof(struct sockaddr_dl)); 568 sa_dl.sdl_len = sizeof(struct sockaddr_dl); 569 sa_dl.sdl_family = AF_LINK; 570 sa_dl.sdl_alen = ETHER_ADDR_LEN; 571 bcopy((void *)msg->data, LLADDR(&sa_dl), 572 ETHER_ADDR_LEN); 573 error = if_delmulti(priv->ifp, 574 (struct sockaddr *)&sa_dl); 575 break; 576 } 577 case NGM_ETHER_DETACH: 578 ng_ether_detach(priv->ifp); 579 break; 580 default: 581 error = EINVAL; 582 break; 583 } 584 break; 585 default: 586 error = EINVAL; 587 break; 588 } 589 NG_RESPOND_MSG(error, node, item, resp); 590 NG_FREE_MSG(msg); 591 return (error); 592} 593 594/* 595 * Receive data on a hook. 596 * Since we use per-hook recveive methods this should never be called. 597 */ 598static int 599ng_ether_rcvdata(hook_p hook, item_p item) 600{ 601 NG_FREE_ITEM(item); 602 603 panic("%s: weird hook", __func__); 604#ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */ 605 return (0); 606#endif 607} 608 609/* 610 * Handle an mbuf received on the "lower" or "orphan" hook. 611 */ 612static int 613ng_ether_rcv_lower(hook_p hook, item_p item) 614{ 615 struct mbuf *m; 616 const node_p node = NG_HOOK_NODE(hook); 617 const priv_p priv = NG_NODE_PRIVATE(node); 618 struct ifnet *const ifp = priv->ifp; 619 620 NGI_GET_M(item, m); 621 NG_FREE_ITEM(item); 622 623 /* Check whether interface is ready for packets */ 624 625 if (!((ifp->if_flags & IFF_UP) && 626 (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 627 NG_FREE_M(m); 628 return (ENETDOWN); 629 } 630 631 /* Make sure header is fully pulled up */ 632 if (m->m_pkthdr.len < sizeof(struct ether_header)) { 633 NG_FREE_M(m); 634 return (EINVAL); 635 } 636 if (m->m_len < sizeof(struct ether_header) 637 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 638 return (ENOBUFS); 639 640 /* Drop in the MAC address if desired */ 641 if (priv->autoSrcAddr) { 642 643 /* Make the mbuf writable if it's not already */ 644 if (!M_WRITABLE(m) 645 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 646 return (ENOBUFS); 647 648 /* Overwrite source MAC address */ 649 bcopy(IF_LLADDR(ifp), 650 mtod(m, struct ether_header *)->ether_shost, 651 ETHER_ADDR_LEN); 652 } 653 654 /* Send it on its way */ 655 return ether_output_frame(ifp, m); 656} 657 658/* 659 * Handle an mbuf received on the "upper" hook. 660 */ 661static int 662ng_ether_rcv_upper(hook_p hook, item_p item) 663{ 664 struct mbuf *m; 665 const node_p node = NG_HOOK_NODE(hook); 666 const priv_p priv = NG_NODE_PRIVATE(node); 667 struct ifnet *ifp = priv->ifp; 668 669 NGI_GET_M(item, m); 670 NG_FREE_ITEM(item); 671 672 /* Check length and pull off header */ 673 if (m->m_pkthdr.len < sizeof(struct ether_header)) { 674 NG_FREE_M(m); 675 return (EINVAL); 676 } 677 if (m->m_len < sizeof(struct ether_header) && 678 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 679 return (ENOBUFS); 680 681 m->m_pkthdr.rcvif = ifp; 682 683 /* Pass the packet to the bridge, it may come back to us */ 684 if (ifp->if_bridge) { 685 BRIDGE_INPUT(ifp, m); 686 if (m == NULL) 687 return (0); 688 } 689 690 /* Route packet back in */ 691 ether_demux(ifp, m); 692 return (0); 693} 694 695/* 696 * Shutdown node. This resets the node but does not remove it 697 * unless the REALLY_DIE flag is set. 698 */ 699static int 700ng_ether_shutdown(node_p node) 701{ 702 const priv_p priv = NG_NODE_PRIVATE(node); 703 704 if (node->nd_flags & NGF_REALLY_DIE) { 705 /* 706 * WE came here because the ethernet card is being unloaded, 707 * so stop being persistant. 708 * Actually undo all the things we did on creation. 709 * Assume the ifp has already been freed. 710 */ 711 NG_NODE_SET_PRIVATE(node, NULL); 712 free(priv, M_NETGRAPH); 713 NG_NODE_UNREF(node); /* free node itself */ 714 return (0); 715 } 716 if (priv->promisc) { /* disable promiscuous mode */ 717 (void)ifpromisc(priv->ifp, 0); 718 priv->promisc = 0; 719 } 720 priv->autoSrcAddr = 1; /* reset auto-src-addr flag */ 721 NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */ 722 723 return (0); 724} 725 726/* 727 * Hook disconnection. 728 */ 729static int 730ng_ether_disconnect(hook_p hook) 731{ 732 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 733 734 if (hook == priv->upper) { 735 priv->upper = NULL; 736 if (priv->ifp != NULL) /* restore h/w csum */ 737 priv->ifp->if_hwassist = priv->hwassist; 738 } else if (hook == priv->lower) 739 priv->lower = NULL; 740 else if (hook == priv->orphan) 741 priv->orphan = NULL; 742 else 743 panic("%s: weird hook", __func__); 744 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 745 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 746 ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */ 747 return (0); 748} 749 750/****************************************************************** 751 INITIALIZATION 752******************************************************************/ 753 754/* 755 * Handle loading and unloading for this node type. 756 */ 757static int 758ng_ether_mod_event(module_t mod, int event, void *data) 759{ 760 int error = 0; 761 int s; 762 763 s = splnet(); 764 switch (event) { 765 case MOD_LOAD: 766 767 /* Register function hooks */ 768 if (ng_ether_attach_p != NULL) { 769 error = EEXIST; 770 break; 771 } 772 ng_ether_attach_p = ng_ether_attach; 773 ng_ether_detach_p = ng_ether_detach; 774 ng_ether_output_p = ng_ether_output; 775 ng_ether_input_p = ng_ether_input; 776 ng_ether_input_orphan_p = ng_ether_input_orphan; 777 ng_ether_link_state_p = ng_ether_link_state; 778 779 break; 780 781 case MOD_UNLOAD: 782 783 /* 784 * Note that the base code won't try to unload us until 785 * all nodes have been removed, and that can't happen 786 * until all Ethernet interfaces are removed. In any 787 * case, we know there are no nodes left if the action 788 * is MOD_UNLOAD, so there's no need to detach any nodes. 789 */ 790 791 /* Unregister function hooks */ 792 ng_ether_attach_p = NULL; 793 ng_ether_detach_p = NULL; 794 ng_ether_output_p = NULL; 795 ng_ether_input_p = NULL; 796 ng_ether_input_orphan_p = NULL; 797 ng_ether_link_state_p = NULL; 798 break; 799 800 default: 801 error = EOPNOTSUPP; 802 break; 803 } 804 splx(s); 805 return (error); 806} 807 808static void 809vnet_ng_ether_init(const void *unused) 810{ 811 struct ifnet *ifp; 812 813 /* If module load was rejected, don't attach to vnets. */ 814 if (ng_ether_attach_p != ng_ether_attach) 815 return; 816 817 /* Create nodes for any already-existing Ethernet interfaces. */ 818 IFNET_RLOCK(); 819 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 820 if (ifp->if_type == IFT_ETHER 821 || ifp->if_type == IFT_L2VLAN) 822 ng_ether_attach(ifp); 823 } 824 IFNET_RUNLOCK(); 825} 826VNET_SYSINIT(vnet_ng_ether_init, SI_SUB_PSEUDO, SI_ORDER_ANY, 827 vnet_ng_ether_init, NULL); 828