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