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