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 58 #include <net/bridge.h> 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 66 #include <netgraph/ng_message.h> 67 #include <netgraph/netgraph.h> 68 #include <netgraph/ng_parse.h> 69 #include <netgraph/ng_ether.h> 70 71 #define IFP2NG(ifp) ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph) 72 #define IFP2NG_SET(ifp, val) (((struct arpcom *)(ifp))->ac_netgraph = (val)) 73 74 /* Per-node private data */ 75 struct private { 76 struct ifnet *ifp; /* associated interface */ 77 hook_p upper; /* upper hook connection */ 78 hook_p lower; /* lower hook connection */ 79 hook_p orphan; /* orphan hook connection */ 80 u_char autoSrcAddr; /* always overwrite source address */ 81 u_char promisc; /* promiscuous mode enabled */ 82 u_long hwassist; /* hardware checksum capabilities */ 83 u_int flags; /* flags e.g. really die */ 84 }; 85 typedef struct private *priv_p; 86 87 /* Hook pointers used by if_ethersubr.c to callback to netgraph */ 88 extern void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); 89 extern void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m); 90 extern int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); 91 extern void (*ng_ether_attach_p)(struct ifnet *ifp); 92 extern void (*ng_ether_detach_p)(struct ifnet *ifp); 93 extern void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 94 95 /* Functional hooks called from if_ethersubr.c */ 96 static void ng_ether_input(struct ifnet *ifp, struct mbuf **mp); 97 static void ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m); 98 static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp); 99 static void ng_ether_attach(struct ifnet *ifp); 100 static void ng_ether_detach(struct ifnet *ifp); 101 static void ng_ether_link_state(struct ifnet *ifp, int state); 102 103 /* Other functions */ 104 static int ng_ether_rcv_lower(node_p node, struct mbuf *m); 105 static int ng_ether_rcv_upper(node_p node, struct mbuf *m); 106 107 /* Netgraph node methods */ 108 static ng_constructor_t ng_ether_constructor; 109 static ng_rcvmsg_t ng_ether_rcvmsg; 110 static ng_shutdown_t ng_ether_shutdown; 111 static ng_newhook_t ng_ether_newhook; 112 static ng_connect_t ng_ether_connect; 113 static ng_rcvdata_t ng_ether_rcvdata; 114 static ng_disconnect_t ng_ether_disconnect; 115 static 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 */ 118 static 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 { 0 } 190 }; 191 192 static struct ng_type ng_ether_typestruct = { 193 .version = NG_ABI_VERSION, 194 .name = NG_ETHER_NODE_TYPE, 195 .mod_event = ng_ether_mod_event, 196 .constructor = ng_ether_constructor, 197 .rcvmsg = ng_ether_rcvmsg, 198 .shutdown = ng_ether_shutdown, 199 .newhook = ng_ether_newhook, 200 .connect = ng_ether_connect, 201 .rcvdata = ng_ether_rcvdata, 202 .disconnect = ng_ether_disconnect, 203 .cmdlist = ng_ether_cmdlist, 204 }; 205 NETGRAPH_INIT(ether, &ng_ether_typestruct); 206 207 /****************************************************************** 208 ETHERNET FUNCTION HOOKS 209 ******************************************************************/ 210 211 /* 212 * Handle a packet that has come in on an interface. We get to 213 * look at it here before any upper layer protocols do. 214 * 215 * NOTE: this function will get called at splimp() 216 */ 217 static void 218 ng_ether_input(struct ifnet *ifp, struct mbuf **mp) 219 { 220 const node_p node = IFP2NG(ifp); 221 const priv_p priv = NG_NODE_PRIVATE(node); 222 int error; 223 224 /* If "lower" hook not connected, let packet continue */ 225 if (priv->lower == NULL) 226 return; 227 NG_SEND_DATA_ONLY(error, priv->lower, *mp); /* sets *mp = NULL */ 228 } 229 230 /* 231 * Handle a packet that has come in on an interface, and which 232 * does not match any of our known protocols (an ``orphan''). 233 * 234 * NOTE: this function will get called at splimp() 235 */ 236 static void 237 ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m) 238 { 239 const node_p node = IFP2NG(ifp); 240 const priv_p priv = NG_NODE_PRIVATE(node); 241 int error; 242 243 /* If "orphan" hook not connected, discard packet */ 244 if (priv->orphan == NULL) { 245 m_freem(m); 246 return; 247 } 248 NG_SEND_DATA_ONLY(error, priv->orphan, m); 249 } 250 251 /* 252 * Handle a packet that is going out on an interface. 253 * The Ethernet header is already attached to the mbuf. 254 */ 255 static int 256 ng_ether_output(struct ifnet *ifp, struct mbuf **mp) 257 { 258 const node_p node = IFP2NG(ifp); 259 const priv_p priv = NG_NODE_PRIVATE(node); 260 int error = 0; 261 262 /* If "upper" hook not connected, let packet continue */ 263 if (priv->upper == NULL) 264 return (0); 265 266 /* Send it out "upper" hook */ 267 NG_SEND_DATA_ONLY(error, priv->upper, *mp); 268 return (error); 269 } 270 271 /* 272 * A new Ethernet interface has been attached. 273 * Create a new node for it, etc. 274 */ 275 static void 276 ng_ether_attach(struct ifnet *ifp) 277 { 278 priv_p priv; 279 node_p node; 280 281 /* Create node */ 282 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__)); 283 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 284 log(LOG_ERR, "%s: can't %s for %s\n", 285 __func__, "create node", ifp->if_xname); 286 return; 287 } 288 289 /* Allocate private data */ 290 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 291 if (priv == NULL) { 292 log(LOG_ERR, "%s: can't %s for %s\n", 293 __func__, "allocate memory", ifp->if_xname); 294 NG_NODE_UNREF(node); 295 return; 296 } 297 NG_NODE_SET_PRIVATE(node, priv); 298 priv->ifp = ifp; 299 IFP2NG_SET(ifp, node); 300 priv->autoSrcAddr = 1; 301 priv->hwassist = ifp->if_hwassist; 302 303 /* Try to give the node the same name as the interface */ 304 if (ng_name_node(node, ifp->if_xname) != 0) { 305 log(LOG_WARNING, "%s: can't name node %s\n", 306 __func__, ifp->if_xname); 307 } 308 } 309 310 /* 311 * An Ethernet interface is being detached. 312 * REALLY Destroy its node. 313 */ 314 static void 315 ng_ether_detach(struct ifnet *ifp) 316 { 317 const node_p node = IFP2NG(ifp); 318 const priv_p priv = NG_NODE_PRIVATE(node); 319 320 NG_NODE_REALLY_DIE(node); /* Force real removal of node */ 321 /* 322 * We can't assume the ifnet is still around when we run shutdown 323 * So zap it now. XXX We HOPE that anything running at this time 324 * handles it (as it should in the non netgraph case). 325 */ 326 IFP2NG_SET(ifp, NULL); 327 priv->ifp = NULL; /* XXX race if interrupted an output packet */ 328 ng_rmnode_self(node); /* remove all netgraph parts */ 329 } 330 331 /* 332 * Notify graph about link event. 333 * if_link_state_change() has already checked that the state has changed. 334 */ 335 static void 336 ng_ether_link_state(struct ifnet *ifp, int state) 337 { 338 const node_p node = IFP2NG(ifp); 339 const priv_p priv = NG_NODE_PRIVATE(node); 340 struct ng_mesg *msg; 341 int cmd, dummy_error = 0; 342 343 if (priv->lower == NULL) 344 return; 345 346 if (state == LINK_STATE_UP) 347 cmd = NGM_LINK_IS_UP; 348 else if (state == LINK_STATE_DOWN) 349 cmd = NGM_LINK_IS_DOWN; 350 else 351 return; 352 353 NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT); 354 if (msg != NULL) 355 NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0); 356 } 357 358 /****************************************************************** 359 NETGRAPH NODE METHODS 360 ******************************************************************/ 361 362 /* 363 * It is not possible or allowable to create a node of this type. 364 * Nodes get created when the interface is attached (or, when 365 * this node type's KLD is loaded). 366 */ 367 static int 368 ng_ether_constructor(node_p node) 369 { 370 return (EINVAL); 371 } 372 373 /* 374 * Check for attaching a new hook. 375 */ 376 static int 377 ng_ether_newhook(node_p node, hook_p hook, const char *name) 378 { 379 const priv_p priv = NG_NODE_PRIVATE(node); 380 hook_p *hookptr; 381 382 /* Divert hook is an alias for lower */ 383 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0) 384 name = NG_ETHER_HOOK_LOWER; 385 386 /* Which hook? */ 387 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) 388 hookptr = &priv->upper; 389 else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) 390 hookptr = &priv->lower; 391 else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) 392 hookptr = &priv->orphan; 393 else 394 return (EINVAL); 395 396 /* Check if already connected (shouldn't be, but doesn't hurt) */ 397 if (*hookptr != NULL) 398 return (EISCONN); 399 400 /* Disable hardware checksums while 'upper' hook is connected */ 401 if (hookptr == &priv->upper) 402 priv->ifp->if_hwassist = 0; 403 404 /* OK */ 405 *hookptr = hook; 406 return (0); 407 } 408 409 /* 410 * Hooks are attached, adjust to force queueing. 411 * We don't really care which hook it is. 412 * they should all be queuing for outgoing data. 413 */ 414 static int 415 ng_ether_connect(hook_p hook) 416 { 417 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook)); 418 return (0); 419 } 420 421 /* 422 * Receive an incoming control message. 423 */ 424 static int 425 ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook) 426 { 427 const priv_p priv = NG_NODE_PRIVATE(node); 428 struct ng_mesg *resp = NULL; 429 int error = 0; 430 struct ng_mesg *msg; 431 432 NGI_GET_MSG(item, msg); 433 switch (msg->header.typecookie) { 434 case NGM_ETHER_COOKIE: 435 switch (msg->header.cmd) { 436 case NGM_ETHER_GET_IFNAME: 437 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 438 if (resp == NULL) { 439 error = ENOMEM; 440 break; 441 } 442 strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ); 443 break; 444 case NGM_ETHER_GET_IFINDEX: 445 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 446 if (resp == NULL) { 447 error = ENOMEM; 448 break; 449 } 450 *((u_int32_t *)resp->data) = priv->ifp->if_index; 451 break; 452 case NGM_ETHER_GET_ENADDR: 453 NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT); 454 if (resp == NULL) { 455 error = ENOMEM; 456 break; 457 } 458 bcopy((IFP2AC(priv->ifp))->ac_enaddr, 459 resp->data, ETHER_ADDR_LEN); 460 break; 461 case NGM_ETHER_SET_ENADDR: 462 { 463 if (msg->header.arglen != ETHER_ADDR_LEN) { 464 error = EINVAL; 465 break; 466 } 467 error = if_setlladdr(priv->ifp, 468 (u_char *)msg->data, ETHER_ADDR_LEN); 469 break; 470 } 471 case NGM_ETHER_GET_PROMISC: 472 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 473 if (resp == NULL) { 474 error = ENOMEM; 475 break; 476 } 477 *((u_int32_t *)resp->data) = priv->promisc; 478 break; 479 case NGM_ETHER_SET_PROMISC: 480 { 481 u_char want; 482 483 if (msg->header.arglen != sizeof(u_int32_t)) { 484 error = EINVAL; 485 break; 486 } 487 want = !!*((u_int32_t *)msg->data); 488 if (want ^ priv->promisc) { 489 if ((error = ifpromisc(priv->ifp, want)) != 0) 490 break; 491 priv->promisc = want; 492 } 493 break; 494 } 495 case NGM_ETHER_GET_AUTOSRC: 496 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 497 if (resp == NULL) { 498 error = ENOMEM; 499 break; 500 } 501 *((u_int32_t *)resp->data) = priv->autoSrcAddr; 502 break; 503 case NGM_ETHER_SET_AUTOSRC: 504 if (msg->header.arglen != sizeof(u_int32_t)) { 505 error = EINVAL; 506 break; 507 } 508 priv->autoSrcAddr = !!*((u_int32_t *)msg->data); 509 break; 510 case NGM_ETHER_ADD_MULTI: 511 { 512 struct sockaddr_dl sa_dl; 513 struct ifmultiaddr *ifm; 514 515 if (msg->header.arglen != ETHER_ADDR_LEN) { 516 error = EINVAL; 517 break; 518 } 519 bzero(&sa_dl, sizeof(struct sockaddr_dl)); 520 sa_dl.sdl_len = sizeof(struct sockaddr_dl); 521 sa_dl.sdl_family = AF_LINK; 522 sa_dl.sdl_alen = ETHER_ADDR_LEN; 523 bcopy((void *)msg->data, LLADDR(&sa_dl), 524 ETHER_ADDR_LEN); 525 error = if_addmulti(priv->ifp, 526 (struct sockaddr *)&sa_dl, &ifm); 527 break; 528 } 529 case NGM_ETHER_DEL_MULTI: 530 { 531 struct sockaddr_dl sa_dl; 532 533 if (msg->header.arglen != ETHER_ADDR_LEN) { 534 error = EINVAL; 535 break; 536 } 537 bzero(&sa_dl, sizeof(struct sockaddr_dl)); 538 sa_dl.sdl_len = sizeof(struct sockaddr_dl); 539 sa_dl.sdl_family = AF_LINK; 540 sa_dl.sdl_alen = ETHER_ADDR_LEN; 541 bcopy((void *)msg->data, LLADDR(&sa_dl), 542 ETHER_ADDR_LEN); 543 error = if_delmulti(priv->ifp, 544 (struct sockaddr *)&sa_dl); 545 break; 546 } 547 default: 548 error = EINVAL; 549 break; 550 } 551 break; 552 default: 553 error = EINVAL; 554 break; 555 } 556 NG_RESPOND_MSG(error, node, item, resp); 557 NG_FREE_MSG(msg); 558 return (error); 559 } 560 561 /* 562 * Receive data on a hook. 563 */ 564 static int 565 ng_ether_rcvdata(hook_p hook, item_p item) 566 { 567 const node_p node = NG_HOOK_NODE(hook); 568 const priv_p priv = NG_NODE_PRIVATE(node); 569 struct mbuf *m; 570 571 NGI_GET_M(item, m); 572 NG_FREE_ITEM(item); 573 574 if (hook == priv->lower || hook == priv->orphan) 575 return ng_ether_rcv_lower(node, m); 576 if (hook == priv->upper) 577 return ng_ether_rcv_upper(node, m); 578 panic("%s: weird hook", __func__); 579 #ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */ 580 return (0); 581 #endif 582 } 583 584 /* 585 * Handle an mbuf received on the "lower" or "orphan" hook. 586 */ 587 static int 588 ng_ether_rcv_lower(node_p node, struct mbuf *m) 589 { 590 const priv_p priv = NG_NODE_PRIVATE(node); 591 struct ifnet *const ifp = priv->ifp; 592 593 /* Check whether interface is ready for packets */ 594 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { 595 NG_FREE_M(m); 596 return (ENETDOWN); 597 } 598 599 /* Make sure header is fully pulled up */ 600 if (m->m_pkthdr.len < sizeof(struct ether_header)) { 601 NG_FREE_M(m); 602 return (EINVAL); 603 } 604 if (m->m_len < sizeof(struct ether_header) 605 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 606 return (ENOBUFS); 607 608 /* Drop in the MAC address if desired */ 609 if (priv->autoSrcAddr) { 610 611 /* Make the mbuf writable if it's not already */ 612 if (!M_WRITABLE(m) 613 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) 614 return (ENOBUFS); 615 616 /* Overwrite source MAC address */ 617 bcopy((IFP2AC(ifp))->ac_enaddr, 618 mtod(m, struct ether_header *)->ether_shost, 619 ETHER_ADDR_LEN); 620 } 621 622 /* Send it on its way */ 623 return ether_output_frame(ifp, m); 624 } 625 626 /* 627 * Handle an mbuf received on the "upper" hook. 628 */ 629 static int 630 ng_ether_rcv_upper(node_p node, struct mbuf *m) 631 { 632 const priv_p priv = NG_NODE_PRIVATE(node); 633 634 m->m_pkthdr.rcvif = priv->ifp; 635 636 if (BDG_ACTIVE(priv->ifp) ) 637 if ((m = bridge_in_ptr(priv->ifp, m)) == NULL) 638 return (0); 639 640 /* Route packet back in */ 641 ether_demux(priv->ifp, m); 642 return (0); 643 } 644 645 /* 646 * Shutdown node. This resets the node but does not remove it 647 * unless the REALLY_DIE flag is set. 648 */ 649 static int 650 ng_ether_shutdown(node_p node) 651 { 652 const priv_p priv = NG_NODE_PRIVATE(node); 653 654 if (node->nd_flags & NGF_REALLY_DIE) { 655 /* 656 * WE came here because the ethernet card is being unloaded, 657 * so stop being persistant. 658 * Actually undo all the things we did on creation. 659 * Assume the ifp has already been freed. 660 */ 661 NG_NODE_SET_PRIVATE(node, NULL); 662 FREE(priv, M_NETGRAPH); 663 NG_NODE_UNREF(node); /* free node itself */ 664 return (0); 665 } 666 if (priv->promisc) { /* disable promiscuous mode */ 667 (void)ifpromisc(priv->ifp, 0); 668 priv->promisc = 0; 669 } 670 priv->autoSrcAddr = 1; /* reset auto-src-addr flag */ 671 NG_NODE_REVIVE(node); /* Signal ng_rmnode we are persisant */ 672 673 return (0); 674 } 675 676 /* 677 * Hook disconnection. 678 */ 679 static int 680 ng_ether_disconnect(hook_p hook) 681 { 682 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 683 684 if (hook == priv->upper) { 685 priv->upper = NULL; 686 if (priv->ifp != NULL) /* restore h/w csum */ 687 priv->ifp->if_hwassist = priv->hwassist; 688 } else if (hook == priv->lower) 689 priv->lower = NULL; 690 else if (hook == priv->orphan) 691 priv->orphan = NULL; 692 else 693 panic("%s: weird hook", __func__); 694 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 695 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 696 ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */ 697 return (0); 698 } 699 700 /****************************************************************** 701 INITIALIZATION 702 ******************************************************************/ 703 704 /* 705 * Handle loading and unloading for this node type. 706 */ 707 static int 708 ng_ether_mod_event(module_t mod, int event, void *data) 709 { 710 struct ifnet *ifp; 711 int error = 0; 712 int s; 713 714 s = splnet(); 715 switch (event) { 716 case MOD_LOAD: 717 718 /* Register function hooks */ 719 if (ng_ether_attach_p != NULL) { 720 error = EEXIST; 721 break; 722 } 723 ng_ether_attach_p = ng_ether_attach; 724 ng_ether_detach_p = ng_ether_detach; 725 ng_ether_output_p = ng_ether_output; 726 ng_ether_input_p = ng_ether_input; 727 ng_ether_input_orphan_p = ng_ether_input_orphan; 728 ng_ether_link_state_p = ng_ether_link_state; 729 730 /* Create nodes for any already-existing Ethernet interfaces */ 731 IFNET_RLOCK(); 732 TAILQ_FOREACH(ifp, &ifnet, if_link) { 733 if (ifp->if_type == IFT_ETHER 734 || ifp->if_type == IFT_L2VLAN) 735 ng_ether_attach(ifp); 736 } 737 IFNET_RUNLOCK(); 738 break; 739 740 case MOD_UNLOAD: 741 742 /* 743 * Note that the base code won't try to unload us until 744 * all nodes have been removed, and that can't happen 745 * until all Ethernet interfaces are removed. In any 746 * case, we know there are no nodes left if the action 747 * is MOD_UNLOAD, so there's no need to detach any nodes. 748 */ 749 750 /* Unregister function hooks */ 751 ng_ether_attach_p = NULL; 752 ng_ether_detach_p = NULL; 753 ng_ether_output_p = NULL; 754 ng_ether_input_p = NULL; 755 ng_ether_input_orphan_p = NULL; 756 ng_ether_link_state_p = NULL; 757 break; 758 759 default: 760 error = EOPNOTSUPP; 761 break; 762 } 763 splx(s); 764 return (error); 765 } 766 767