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$ 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 120static eventhandler_tag ng_ether_ifnet_arrival_cookie; 121 122/* List of commands and how to convert arguments to/from ASCII */ 123static const struct ng_cmdlist ng_ether_cmdlist[] = { 124 { 125 NGM_ETHER_COOKIE, 126 NGM_ETHER_GET_IFNAME, 127 "getifname", 128 NULL, 129 &ng_parse_string_type 130 }, 131 { 132 NGM_ETHER_COOKIE, 133 NGM_ETHER_GET_IFINDEX, 134 "getifindex", 135 NULL, 136 &ng_parse_int32_type 137 }, 138 { 139 NGM_ETHER_COOKIE, 140 NGM_ETHER_GET_ENADDR, 141 "getenaddr", 142 NULL, 143 &ng_parse_enaddr_type 144 }, 145 { 146 NGM_ETHER_COOKIE, 147 NGM_ETHER_SET_ENADDR, 148 "setenaddr", 149 &ng_parse_enaddr_type, 150 NULL 151 }, 152 { 153 NGM_ETHER_COOKIE, 154 NGM_ETHER_GET_PROMISC, 155 "getpromisc", 156 NULL, 157 &ng_parse_int32_type 158 }, 159 { 160 NGM_ETHER_COOKIE, 161 NGM_ETHER_SET_PROMISC, 162 "setpromisc", 163 &ng_parse_int32_type, 164 NULL 165 }, 166 { 167 NGM_ETHER_COOKIE, 168 NGM_ETHER_GET_AUTOSRC, 169 "getautosrc", 170 NULL, 171 &ng_parse_int32_type 172 }, 173 { 174 NGM_ETHER_COOKIE, 175 NGM_ETHER_SET_AUTOSRC, 176 "setautosrc", 177 &ng_parse_int32_type, 178 NULL 179 }, 180 { 181 NGM_ETHER_COOKIE, 182 NGM_ETHER_ADD_MULTI, 183 "addmulti", 184 &ng_parse_enaddr_type, 185 NULL 186 }, 187 { 188 NGM_ETHER_COOKIE, 189 NGM_ETHER_DEL_MULTI, 190 "delmulti", 191 &ng_parse_enaddr_type, 192 NULL 193 }, 194 { 195 NGM_ETHER_COOKIE, 196 NGM_ETHER_DETACH, 197 "detach", 198 NULL, 199 NULL 200 }, 201 { 0 } 202}; 203 204static struct ng_type ng_ether_typestruct = { 205 .version = NG_ABI_VERSION, 206 .name = NG_ETHER_NODE_TYPE, 207 .mod_event = ng_ether_mod_event, 208 .constructor = ng_ether_constructor, 209 .rcvmsg = ng_ether_rcvmsg, 210 .shutdown = ng_ether_shutdown, 211 .newhook = ng_ether_newhook, 212 .rcvdata = ng_ether_rcvdata, 213 .disconnect = ng_ether_disconnect, 214 .cmdlist = ng_ether_cmdlist, 215}; 216NETGRAPH_INIT(ether, &ng_ether_typestruct); 217 218/****************************************************************** 219 UTILITY FUNCTIONS 220******************************************************************/ 221static void 222ng_ether_sanitize_ifname(const char *ifname, char *name) 223{ 224 int i; 225 226 for (i = 0; i < IFNAMSIZ; i++) { 227 if (ifname[i] == '.' || ifname[i] == ':') 228 name[i] = '_'; 229 else 230 name[i] = ifname[i]; 231 if (name[i] == '\0') 232 break; 233 } 234} 235 236/****************************************************************** 237 ETHERNET FUNCTION HOOKS 238******************************************************************/ 239 240/* 241 * Handle a packet that has come in on an interface. We get to 242 * look at it here before any upper layer protocols do. 243 * 244 * NOTE: this function will get called at splimp() 245 */ 246static void 247ng_ether_input(struct ifnet *ifp, struct mbuf **mp) 248{ 249 const node_p node = IFP2NG(ifp); 250 const priv_p priv = NG_NODE_PRIVATE(node); 251 int error; 252 253 /* If "lower" hook not connected, let packet continue */ 254 if (priv->lower == NULL) 255 return; 256 NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */ 257} 258 259/* 260 * Handle a packet that has come in on an interface, and which 261 * does not match any of our known protocols (an ``orphan''). 262 * 263 * NOTE: this function will get called at splimp() 264 */ 265static void 266ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m) 267{ 268 const node_p node = IFP2NG(ifp); 269 const priv_p priv = NG_NODE_PRIVATE(node); 270 int error; 271 272 /* If "orphan" hook not connected, discard packet */ 273 if (priv->orphan == NULL) { 274 m_freem(m); 275 return; 276 } 277 NG_SEND_DATA_ONLY(error, priv->orphan, m); 278} 279 280/* 281 * Handle a packet that is going out on an interface. 282 * The Ethernet header is already attached to the mbuf. 283 */ 284static int 285ng_ether_output(struct ifnet *ifp, struct mbuf **mp) 286{ 287 const node_p node = IFP2NG(ifp); 288 const priv_p priv = NG_NODE_PRIVATE(node); 289 int error = 0; 290 291 /* If "upper" hook not connected, let packet continue */ 292 if (priv->upper == NULL) 293 return (0); 294 295 /* Send it out "upper" hook */ 296 NG_OUTBOUND_THREAD_REF(); 297 NG_SEND_DATA_ONLY(error, priv->upper, *mp); 298 NG_OUTBOUND_THREAD_UNREF(); 299 return (error); 300} 301 302/* 303 * A new Ethernet interface has been attached. 304 * Create a new node for it, etc. 305 */ 306static void 307ng_ether_attach(struct ifnet *ifp) 308{ 309 char name[IFNAMSIZ]; 310 priv_p priv; 311 node_p node; 312 313 /* 314 * Do not create / attach an ether node to this ifnet if 315 * a netgraph node with the same name already exists. 316 * This should prevent ether nodes to become attached to 317 * eiface nodes, which may be problematic due to naming 318 * clashes. 319 */ 320 if ((node = ng_name2noderef(NULL, ifp->if_xname)) != NULL) { 321 NG_NODE_UNREF(node); 322 return; 323 } 324 325 /* Create node */ 326 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__)); 327 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 328 log(LOG_ERR, "%s: can't %s for %s\n", 329 __func__, "create node", ifp->if_xname); 330 return; 331 } 332 333 /* Allocate private data */ 334 priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 335 if (priv == NULL) { 336 log(LOG_ERR, "%s: can't %s for %s\n", 337 __func__, "allocate memory", ifp->if_xname); 338 NG_NODE_UNREF(node); 339 return; 340 } 341 NG_NODE_SET_PRIVATE(node, priv); 342 priv->ifp = ifp; 343 IFP2NG(ifp) = node; 344 priv->hwassist = ifp->if_hwassist; 345 346 /* Try to give the node the same name as the interface */ 347 ng_ether_sanitize_ifname(ifp->if_xname, name); 348 if (ng_name_node(node, name) != 0) 349 log(LOG_WARNING, "%s: can't name node %s\n", __func__, name); 350} 351 352/* 353 * An Ethernet interface is being detached. 354 * REALLY Destroy its node. 355 */ 356static void 357ng_ether_detach(struct ifnet *ifp) 358{ 359 const node_p node = IFP2NG(ifp); 360 const priv_p priv = NG_NODE_PRIVATE(node); 361 362 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 363 NG_NODE_REALLY_DIE(node); /* Force real removal of node */ 364 /* 365 * We can't assume the ifnet is still around when we run shutdown 366 * So zap it now. XXX We HOPE that anything running at this time 367 * handles it (as it should in the non netgraph case). 368 */ 369 IFP2NG(ifp) = NULL; 370 priv->ifp = NULL; /* XXX race if interrupted an output packet */ 371 ng_rmnode_self(node); /* remove all netgraph parts */ 372} 373 374/* 375 * Notify graph about link event. 376 * if_link_state_change() has already checked that the state has changed. 377 */ 378static void 379ng_ether_link_state(struct ifnet *ifp, int state) 380{ 381 const node_p node = IFP2NG(ifp); 382 const priv_p priv = NG_NODE_PRIVATE(node); 383 struct ng_mesg *msg; 384 int cmd, dummy_error = 0; 385 386 if (state == LINK_STATE_UP) 387 cmd = NGM_LINK_IS_UP; 388 else if (state == LINK_STATE_DOWN) 389 cmd = NGM_LINK_IS_DOWN; 390 else 391 return; 392 393 if (priv->lower != NULL) { 394 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT); 395 if (msg != NULL) 396 NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0); 397 } 398 if (priv->orphan != NULL) { 399 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT); 400 if (msg != NULL) 401 NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->orphan, 0); 402 } 403} 404 405/* 406 * Interface arrival notification handler. 407 * The notification is produced in two cases: 408 * o a new interface arrives 409 * o an existing interface got renamed 410 * Currently the first case is handled by ng_ether_attach via special 411 * hook ng_ether_attach_p. 412 */ 413static void 414ng_ether_ifnet_arrival_event(void *arg __unused, struct ifnet *ifp) 415{ 416 char name[IFNAMSIZ]; 417 node_p node; 418 419 /* Only ethernet interfaces are of interest. */ 420 if (ifp->if_type != IFT_ETHER 421 && ifp->if_type != IFT_L2VLAN) 422 return; 423 424 /* 425 * Just return if it's a new interface without an ng_ether companion. 426 */ 427 node = IFP2NG(ifp); 428 if (node == NULL) 429 return; 430 431 /* Try to give the node the same name as the new interface name */ 432 ng_ether_sanitize_ifname(ifp->if_xname, name); 433 if (ng_name_node(node, name) != 0) 434 log(LOG_WARNING, "%s: can't re-name node %s\n", __func__, name); 435} 436 437/****************************************************************** 438 NETGRAPH NODE METHODS 439******************************************************************/ 440 441/* 442 * It is not possible or allowable to create a node of this type. 443 * Nodes get created when the interface is attached (or, when 444 * this node type's KLD is loaded). 445 */ 446static int 447ng_ether_constructor(node_p node) 448{ 449 return (EINVAL); 450} 451 452/* 453 * Check for attaching a new hook. 454 */ 455static int 456ng_ether_newhook(node_p node, hook_p hook, const char *name) 457{ 458 const priv_p priv = NG_NODE_PRIVATE(node); 459 hook_p *hookptr; 460 461 /* Divert hook is an alias for lower */ 462 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0) 463 name = NG_ETHER_HOOK_LOWER; 464 465 /* Which hook? */ 466 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) { 467 hookptr = &priv->upper; 468 NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_upper); 469 NG_HOOK_SET_TO_INBOUND(hook); 470 } else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) { 471 hookptr = &priv->lower; 472 NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower); 473 } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) { 474 hookptr = &priv->orphan; 475 NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower); 476 } else 477 return (EINVAL); 478 479 /* Check if already connected (shouldn't be, but doesn't hurt) */ 480 if (*hookptr != NULL) 481 return (EISCONN); 482 483 /* Disable hardware checksums while 'upper' hook is connected */ 484 if (hookptr == &priv->upper) 485 priv->ifp->if_hwassist = 0; 486 NG_HOOK_HI_STACK(hook); 487 /* OK */ 488 *hookptr = hook; 489 return (0); 490} 491 492/* 493 * Receive an incoming control message. 494 */ 495static int 496ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook) 497{ 498 const priv_p priv = NG_NODE_PRIVATE(node); 499 struct ng_mesg *resp = NULL; 500 int error = 0; 501 struct ng_mesg *msg; 502 503 NGI_GET_MSG(item, msg); 504 switch (msg->header.typecookie) { 505 case NGM_ETHER_COOKIE: 506 switch (msg->header.cmd) { 507 case NGM_ETHER_GET_IFNAME: 508 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 509 if (resp == NULL) { 510 error = ENOMEM; 511 break; 512 } 513 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ); 514 break; 515 case NGM_ETHER_GET_IFINDEX: 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->ifp->if_index; 522 break; 523 case NGM_ETHER_GET_ENADDR: 524 NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT); 525 if (resp == NULL) { 526 error = ENOMEM; 527 break; 528 } 529 bcopy(IF_LLADDR(priv->ifp), 530 resp->data, ETHER_ADDR_LEN); 531 break; 532 case NGM_ETHER_SET_ENADDR: 533 { 534 if (msg->header.arglen != ETHER_ADDR_LEN) { 535 error = EINVAL; 536 break; 537 } 538 error = if_setlladdr(priv->ifp, 539 (u_char *)msg->data, ETHER_ADDR_LEN); 540 EVENTHANDLER_INVOKE(iflladdr_event, priv->ifp); 541 break; 542 } 543 case NGM_ETHER_GET_PROMISC: 544 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 545 if (resp == NULL) { 546 error = ENOMEM; 547 break; 548 } 549 *((u_int32_t *)resp->data) = priv->promisc; 550 break; 551 case NGM_ETHER_SET_PROMISC: 552 { 553 u_char want; 554 555 if (msg->header.arglen != sizeof(u_int32_t)) { 556 error = EINVAL; 557 break; 558 } 559 want = !!*((u_int32_t *)msg->data); 560 if (want ^ priv->promisc) { 561 if ((error = ifpromisc(priv->ifp, want)) != 0) 562 break; 563 priv->promisc = want; 564 } 565 break; 566 } 567 case NGM_ETHER_GET_AUTOSRC: 568 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 569 if (resp == NULL) { 570 error = ENOMEM; 571 break; 572 } 573 *((u_int32_t *)resp->data) = priv->autoSrcAddr; 574 break; 575 case NGM_ETHER_SET_AUTOSRC: 576 if (msg->header.arglen != sizeof(u_int32_t)) { 577 error = EINVAL; 578 break; 579 } 580 priv->autoSrcAddr = !!*((u_int32_t *)msg->data); 581 break; 582 case NGM_ETHER_ADD_MULTI: 583 { 584 struct sockaddr_dl sa_dl; 585 struct ifmultiaddr *ifma; 586 587 if (msg->header.arglen != ETHER_ADDR_LEN) { 588 error = EINVAL; 589 break; 590 } 591 bzero(&sa_dl, sizeof(struct sockaddr_dl)); 592 sa_dl.sdl_len = sizeof(struct sockaddr_dl); 593 sa_dl.sdl_family = AF_LINK; 594 sa_dl.sdl_alen = ETHER_ADDR_LEN; 595 bcopy((void *)msg->data, LLADDR(&sa_dl), 596 ETHER_ADDR_LEN); 597 /* 598 * Netgraph is only permitted to join groups once 599 * via the if_addmulti() KPI, because it cannot hold 600 * struct ifmultiaddr * between calls. It may also 601 * lose a race while we check if the membership 602 * already exists. 603 */ 604 if_maddr_rlock(priv->ifp); 605 ifma = if_findmulti(priv->ifp, 606 (struct sockaddr *)&sa_dl); 607 if_maddr_runlock(priv->ifp); 608 if (ifma != NULL) { 609 error = EADDRINUSE; 610 } else { 611 error = if_addmulti(priv->ifp, 612 (struct sockaddr *)&sa_dl, &ifma); 613 } 614 break; 615 } 616 case NGM_ETHER_DEL_MULTI: 617 { 618 struct sockaddr_dl sa_dl; 619 620 if (msg->header.arglen != ETHER_ADDR_LEN) { 621 error = EINVAL; 622 break; 623 } 624 bzero(&sa_dl, sizeof(struct sockaddr_dl)); 625 sa_dl.sdl_len = sizeof(struct sockaddr_dl); 626 sa_dl.sdl_family = AF_LINK; 627 sa_dl.sdl_alen = ETHER_ADDR_LEN; 628 bcopy((void *)msg->data, LLADDR(&sa_dl), 629 ETHER_ADDR_LEN); 630 error = if_delmulti(priv->ifp, 631 (struct sockaddr *)&sa_dl); 632 break; 633 } 634 case NGM_ETHER_DETACH: 635 ng_ether_detach(priv->ifp); 636 break; 637 default: 638 error = EINVAL; 639 break; 640 } 641 break; 642 default: 643 error = EINVAL; 644 break; 645 } 646 NG_RESPOND_MSG(error, node, item, resp); 647 NG_FREE_MSG(msg); 648 return (error); 649} 650 651/* 652 * Receive data on a hook. 653 * Since we use per-hook recveive methods this should never be called. 654 */ 655static int 656ng_ether_rcvdata(hook_p hook, item_p item) 657{ 658 NG_FREE_ITEM(item); 659 660 panic("%s: weird hook", __func__); 661} 662 663/* 664 * Handle an mbuf received on the "lower" or "orphan" hook. 665 */ 666static int 667ng_ether_rcv_lower(hook_p hook, item_p item) 668{ 669 struct mbuf *m; 670 const node_p node = NG_HOOK_NODE(hook); 671 const priv_p priv = NG_NODE_PRIVATE(node); 672 struct ifnet *const ifp = priv->ifp; 673 674 NGI_GET_M(item, m); 675 NG_FREE_ITEM(item); 676 677 /* Check whether interface is ready for packets */ 678 679 if (!((ifp->if_flags & IFF_UP) && 680 (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 681 NG_FREE_M(m); 682 return (ENETDOWN); 683 } 684 685 /* Make sure header is fully pulled up */ 686 if (m->m_pkthdr.len < sizeof(struct ether_header)) { 687 NG_FREE_M(m); 688 return (EINVAL); 689 } 690 if (m->m_len < sizeof(struct ether_header) 691 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 692 return (ENOBUFS); 693 694 /* Drop in the MAC address if desired */ 695 if (priv->autoSrcAddr) { 696 697 /* Make the mbuf writable if it's not already */ 698 if (!M_WRITABLE(m) 699 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 700 return (ENOBUFS); 701 702 /* Overwrite source MAC address */ 703 bcopy(IF_LLADDR(ifp), 704 mtod(m, struct ether_header *)->ether_shost, 705 ETHER_ADDR_LEN); 706 } 707 708 /* Send it on its way */ 709 return ether_output_frame(ifp, m); 710} 711 712/* 713 * Handle an mbuf received on the "upper" hook. 714 */ 715static int 716ng_ether_rcv_upper(hook_p hook, item_p item) 717{ 718 struct mbuf *m; 719 const node_p node = NG_HOOK_NODE(hook); 720 const priv_p priv = NG_NODE_PRIVATE(node); 721 struct ifnet *ifp = priv->ifp; 722 723 NGI_GET_M(item, m); 724 NG_FREE_ITEM(item); 725 726 /* Check length and pull off header */ 727 if (m->m_pkthdr.len < sizeof(struct ether_header)) { 728 NG_FREE_M(m); 729 return (EINVAL); 730 } 731 if (m->m_len < sizeof(struct ether_header) && 732 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 733 return (ENOBUFS); 734 735 m->m_pkthdr.rcvif = ifp; 736 737 /* Pass the packet to the bridge, it may come back to us */ 738 if (ifp->if_bridge) { 739 BRIDGE_INPUT(ifp, m); 740 if (m == NULL) 741 return (0); 742 } 743 744 /* Route packet back in */ 745 ether_demux(ifp, m); 746 return (0); 747} 748 749/* 750 * Shutdown node. This resets the node but does not remove it 751 * unless the REALLY_DIE flag is set. 752 */ 753static int 754ng_ether_shutdown(node_p node) 755{ 756 const priv_p priv = NG_NODE_PRIVATE(node); 757 758 if (node->nd_flags & NGF_REALLY_DIE) { 759 /* 760 * WE came here because the ethernet card is being unloaded, 761 * so stop being persistant. 762 * Actually undo all the things we did on creation. 763 * Assume the ifp has already been freed. 764 */ 765 NG_NODE_SET_PRIVATE(node, NULL); 766 free(priv, M_NETGRAPH); 767 NG_NODE_UNREF(node); /* free node itself */ 768 return (0); 769 } 770 if (priv->promisc) { /* disable promiscuous mode */ 771 (void)ifpromisc(priv->ifp, 0); 772 priv->promisc = 0; 773 } 774 NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */ 775 776 return (0); 777} 778 779/* 780 * Hook disconnection. 781 */ 782static int 783ng_ether_disconnect(hook_p hook) 784{ 785 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 786 787 if (hook == priv->upper) { 788 priv->upper = NULL; 789 if (priv->ifp != NULL) /* restore h/w csum */ 790 priv->ifp->if_hwassist = priv->hwassist; 791 } else if (hook == priv->lower) 792 priv->lower = NULL; 793 else if (hook == priv->orphan) 794 priv->orphan = NULL; 795 else 796 panic("%s: weird hook", __func__); 797 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 798 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 799 ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */ 800 return (0); 801} 802 803/****************************************************************** 804 INITIALIZATION 805******************************************************************/ 806 807/* 808 * Handle loading and unloading for this node type. 809 */ 810static int 811ng_ether_mod_event(module_t mod, int event, void *data) 812{ 813 int error = 0; 814 int s; 815 816 s = splnet(); 817 switch (event) { 818 case MOD_LOAD: 819 820 /* Register function hooks */ 821 if (ng_ether_attach_p != NULL) { 822 error = EEXIST; 823 break; 824 } 825 ng_ether_attach_p = ng_ether_attach; 826 ng_ether_detach_p = ng_ether_detach; 827 ng_ether_output_p = ng_ether_output; 828 ng_ether_input_p = ng_ether_input; 829 ng_ether_input_orphan_p = ng_ether_input_orphan; 830 ng_ether_link_state_p = ng_ether_link_state; 831 832 ng_ether_ifnet_arrival_cookie = 833 EVENTHANDLER_REGISTER(ifnet_arrival_event, 834 ng_ether_ifnet_arrival_event, NULL, EVENTHANDLER_PRI_ANY); 835 break; 836 837 case MOD_UNLOAD: 838 839 /* 840 * Note that the base code won't try to unload us until 841 * all nodes have been removed, and that can't happen 842 * until all Ethernet interfaces are removed. In any 843 * case, we know there are no nodes left if the action 844 * is MOD_UNLOAD, so there's no need to detach any nodes. 845 */ 846 847 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, 848 ng_ether_ifnet_arrival_cookie); 849 850 /* Unregister function hooks */ 851 ng_ether_attach_p = NULL; 852 ng_ether_detach_p = NULL; 853 ng_ether_output_p = NULL; 854 ng_ether_input_p = NULL; 855 ng_ether_input_orphan_p = NULL; 856 ng_ether_link_state_p = NULL; 857 break; 858 859 default: 860 error = EOPNOTSUPP; 861 break; 862 } 863 splx(s); 864 return (error); 865} 866 867static void 868vnet_ng_ether_init(const void *unused) 869{ 870 struct ifnet *ifp; 871 872 /* If module load was rejected, don't attach to vnets. */ 873 if (ng_ether_attach_p != ng_ether_attach) 874 return; 875 876 /* Create nodes for any already-existing Ethernet interfaces. */ 877 IFNET_RLOCK(); 878 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 879 if (ifp->if_type == IFT_ETHER 880 || ifp->if_type == IFT_L2VLAN) 881 ng_ether_attach(ifp); 882 } 883 IFNET_RUNLOCK(); 884} 885VNET_SYSINIT(vnet_ng_ether_init, SI_SUB_PSEUDO, SI_ORDER_ANY, 886 vnet_ng_ether_init, NULL); 887