1 2 /* 3 * ng_ether.c 4 * 5 * Copyright (c) 1996-2000 Whistle Communications, Inc. 6 * All rights reserved. 7 * 8 * Subject to the following obligations and disclaimer of warranty, use and 9 * redistribution of this software, in source or object code forms, with or 10 * without modifications are expressly permitted by Whistle Communications; 11 * provided, however, that: 12 * 1. Any and all reproductions of the source or object code must include the 13 * copyright notice above and the following disclaimer of warranties; and 14 * 2. No rights are granted, in any manner or form, to use Whistle 15 * Communications, Inc. trademarks, including the mark "WHISTLE 16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 17 * such appears in the above copyright notice or in the software. 18 * 19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 35 * OF SUCH DAMAGE. 36 * 37 * Authors: Archie Cobbs <archie@freebsd.org> 38 * Julian Elischer <julian@freebsd.org> 39 * 40 * $FreeBSD$ 41 */ 42 43 /* 44 * ng_ether(4) netgraph node type 45 */ 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/kernel.h> 50 #include <sys/malloc.h> 51 #include <sys/mbuf.h> 52 #include <sys/errno.h> 53 #include <sys/syslog.h> 54 #include <sys/socket.h> 55 56 #include <net/if.h> 57 #include <net/if_types.h> 58 #include <net/if_arp.h> 59 #include <net/if_var.h> 60 #include <net/ethernet.h> 61 62 #include <netgraph/ng_message.h> 63 #include <netgraph/netgraph.h> 64 #include <netgraph/ng_parse.h> 65 #include <netgraph/ng_ether.h> 66 67 #define IFP2AC(IFP) ((struct arpcom *)IFP) 68 #define IFP2NG(ifp) ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph) 69 70 /* Per-node private data */ 71 struct private { 72 struct ifnet *ifp; /* associated interface */ 73 hook_p upper; /* upper hook connection */ 74 hook_p lower; /* lower OR orphan hook connection */ 75 u_char lowerOrphan; /* whether lower is lower or orphan */ 76 u_char autoSrcAddr; /* always overwrite source address */ 77 u_char promisc; /* promiscuous mode enabled */ 78 u_int flags; /* flags e.g. really die */ 79 }; 80 typedef struct private *priv_p; 81 82 /* Functional hooks called from if_ethersubr.c */ 83 static void ng_ether_input(struct ifnet *ifp, 84 struct mbuf **mp, struct ether_header *eh); 85 static void ng_ether_input_orphan(struct ifnet *ifp, 86 struct mbuf *m, struct ether_header *eh); 87 static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp); 88 static void ng_ether_attach(struct ifnet *ifp); 89 static void ng_ether_detach(struct ifnet *ifp); 90 91 /* Other functions */ 92 static void ng_ether_input2(node_p node, 93 struct mbuf **mp, struct ether_header *eh); 94 static int ng_ether_glueback_header(struct mbuf **mp, 95 struct ether_header *eh); 96 static int ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta); 97 static int ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta); 98 99 /* Netgraph node methods */ 100 static ng_constructor_t ng_ether_constructor; 101 static ng_rcvmsg_t ng_ether_rcvmsg; 102 static ng_shutdown_t ng_ether_shutdown; 103 static ng_newhook_t ng_ether_newhook; 104 static ng_connect_t ng_ether_connect; 105 static ng_rcvdata_t ng_ether_rcvdata; 106 static ng_disconnect_t ng_ether_disconnect; 107 static int ng_ether_mod_event(module_t mod, int event, void *data); 108 109 /* Parse type for an Ethernet address */ 110 static ng_parse_t ng_enaddr_parse; 111 static ng_unparse_t ng_enaddr_unparse; 112 const struct ng_parse_type ng_ether_enaddr_type = { 113 NULL, 114 NULL, 115 NULL, 116 ng_enaddr_parse, 117 ng_enaddr_unparse, 118 NULL, /* no such thing as a "default" EN address */ 119 0 120 }; 121 122 /* List of commands and how to convert arguments to/from ASCII */ 123 static 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_ether_enaddr_type 144 }, 145 { 146 NGM_ETHER_COOKIE, 147 NGM_ETHER_SET_ENADDR, 148 "setenaddr", 149 &ng_ether_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 { 0 } 181 }; 182 183 static struct ng_type ng_ether_typestruct = { 184 NG_ABI_VERSION, 185 NG_ETHER_NODE_TYPE, 186 ng_ether_mod_event, 187 ng_ether_constructor, 188 ng_ether_rcvmsg, 189 ng_ether_shutdown, 190 ng_ether_newhook, 191 NULL, 192 ng_ether_connect, 193 ng_ether_rcvdata, 194 ng_ether_disconnect, 195 ng_ether_cmdlist, 196 }; 197 MODULE_VERSION(ng_ether, 1); 198 NETGRAPH_INIT(ether, &ng_ether_typestruct); 199 200 /****************************************************************** 201 ETHERNET FUNCTION HOOKS 202 ******************************************************************/ 203 204 /* 205 * Handle a packet that has come in on an interface. We get to 206 * look at it here before any upper layer protocols do. 207 * 208 * NOTE: this function will get called at splimp() 209 */ 210 static void 211 ng_ether_input(struct ifnet *ifp, 212 struct mbuf **mp, struct ether_header *eh) 213 { 214 const node_p node = IFP2NG(ifp); 215 const priv_p priv = NG_NODE_PRIVATE(node); 216 217 /* If "lower" hook not connected, let packet continue */ 218 if (priv->lower == NULL || priv->lowerOrphan) 219 return; 220 ng_ether_input2(node, mp, eh); 221 } 222 223 /* 224 * Handle a packet that has come in on an interface, and which 225 * does not match any of our known protocols (an ``orphan''). 226 * 227 * NOTE: this function will get called at splimp() 228 */ 229 static void 230 ng_ether_input_orphan(struct ifnet *ifp, 231 struct mbuf *m, struct ether_header *eh) 232 { 233 const node_p node = IFP2NG(ifp); 234 const priv_p priv = NG_NODE_PRIVATE(node); 235 236 /* If "orphan" hook not connected, let packet continue */ 237 if (priv->lower == NULL || !priv->lowerOrphan) { 238 m_freem(m); 239 return; 240 } 241 ng_ether_input2(node, &m, eh); 242 if (m != NULL) 243 m_freem(m); 244 } 245 246 /* 247 * Handle a packet that has come in on an ethernet interface. 248 * The Ethernet header has already been detached from the mbuf, 249 * so we have to put it back. 250 * 251 * NOTE: this function will get called at splimp() 252 */ 253 static void 254 ng_ether_input2(node_p node, struct mbuf **mp, struct ether_header *eh) 255 { 256 const priv_p priv = NG_NODE_PRIVATE(node); 257 int error; 258 259 /* Glue Ethernet header back on */ 260 if ((error = ng_ether_glueback_header(mp, eh)) != 0) 261 return; 262 263 /* Send out lower/orphan hook */ 264 NG_SEND_DATA_ONLY(error, priv->lower, *mp); 265 *mp = NULL; 266 } 267 268 /* 269 * Handle a packet that is going out on an interface. 270 * The Ethernet header is already attached to the mbuf. 271 */ 272 static int 273 ng_ether_output(struct ifnet *ifp, struct mbuf **mp) 274 { 275 const node_p node = IFP2NG(ifp); 276 const priv_p priv = NG_NODE_PRIVATE(node); 277 int error = 0; 278 279 /* If "upper" hook not connected, let packet continue */ 280 if (priv->upper == NULL) 281 return (0); 282 283 /* Send it out "upper" hook */ 284 NG_SEND_DATA_ONLY(error, priv->upper, *mp); 285 return (error); 286 } 287 288 /* 289 * A new Ethernet interface has been attached. 290 * Create a new node for it, etc. 291 */ 292 static void 293 ng_ether_attach(struct ifnet *ifp) 294 { 295 char name[IFNAMSIZ + 1]; 296 priv_p priv; 297 node_p node; 298 299 /* Create node */ 300 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __FUNCTION__)); 301 snprintf(name, sizeof(name), "%s%d", ifp->if_name, ifp->if_unit); 302 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 303 log(LOG_ERR, "%s: can't %s for %s\n", 304 __FUNCTION__, "create node", name); 305 return; 306 } 307 308 /* Allocate private data */ 309 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 310 if (priv == NULL) { 311 log(LOG_ERR, "%s: can't %s for %s\n", 312 __FUNCTION__, "allocate memory", name); 313 NG_NODE_UNREF(node); 314 return; 315 } 316 NG_NODE_SET_PRIVATE(node, priv); 317 priv->ifp = ifp; 318 IFP2NG(ifp) = node; 319 priv->autoSrcAddr = 1; 320 321 /* Try to give the node the same name as the interface */ 322 if (ng_name_node(node, name) != 0) { 323 log(LOG_WARNING, "%s: can't name node %s\n", 324 __FUNCTION__, name); 325 } 326 } 327 328 /* 329 * An Ethernet interface is being detached. 330 * REALLY Destroy its node. 331 */ 332 static void 333 ng_ether_detach(struct ifnet *ifp) 334 { 335 const node_p node = IFP2NG(ifp); 336 const priv_p priv = NG_NODE_PRIVATE(node); 337 338 if (node == NULL) /* no node (why not?), ignore */ 339 return; 340 NG_NODE_REALLY_DIE(node); /* Force real removal of node */ 341 /* 342 * We can't assume the ifnet is still around when we run shutdown 343 * So zap it now. XXX We HOPE that anything running at this time 344 * handles it (as it should in the non netgraph case). 345 */ 346 IFP2NG(ifp) = NULL; 347 priv->ifp = NULL; /* XXX race if interrupted an output packet */ 348 ng_rmnode_self(node); /* remove all netgraph parts */ 349 } 350 351 /* 352 * Optimization for gluing the Ethernet header back onto 353 * the front of an incoming packet. 354 */ 355 static int 356 ng_ether_glueback_header(struct mbuf **mp, struct ether_header *eh) 357 { 358 struct mbuf *m = *mp; 359 uintfptr_t room; 360 int error = 0; 361 362 /* 363 * Possibly the header is already on the front. 364 * If this is the case so just move the markers back 365 * to re-include it. We lucked out. 366 * This allows us to avoid a yucky m_pullup 367 * in later nodes if it works. 368 */ 369 if (eh == mtod(m, struct ether_header *) - 1) { 370 m->m_len += sizeof(*eh); 371 m->m_data -= sizeof(*eh); 372 m->m_pkthdr.len += sizeof(*eh); 373 goto done; 374 } 375 376 /* 377 * Alternatively there may be room even though 378 * it is stored somewhere else. If so, copy it in. 379 * This only safe because we KNOW that this packet has 380 * just been generated by an ethernet card, so there are 381 * no aliases to the buffer (not so for outgoing packets). 382 * Nearly all ethernet cards will end up producing mbufs 383 * that fall into these cases. So we are not optimizing 384 * contorted cases. 385 */ 386 if ((m->m_flags & M_EXT) != 0) { 387 room = mtod(m, caddr_t) - m->m_ext.ext_buf; 388 if (room > m->m_ext.ext_size) /* garbage, fail immediately */ 389 room = 0; 390 } else 391 room = mtod(m, caddr_t) - m->m_pktdat; 392 393 /* 394 * If we have room, just copy it and adjust 395 */ 396 if (room >= sizeof(*eh)) { 397 m->m_len += sizeof(*eh); 398 m->m_data -= sizeof(*eh); 399 m->m_pkthdr.len += sizeof(*eh); 400 goto copy; 401 } 402 403 /* 404 * Doing anything more is likely to get more 405 * expensive than it's worth.. 406 * it's probable that everything else is in one 407 * big lump. The next node will do an m_pullup() 408 * for exactly the amount of data it needs and 409 * hopefully everything after that will not 410 * need one. So let's just use M_PREPEND. 411 */ 412 M_PREPEND(m, sizeof (*eh), M_DONTWAIT); 413 if (m == NULL) { 414 error = ENOBUFS; 415 goto done; 416 } 417 418 copy: 419 /* Copy header and return (possibly new) mbuf */ 420 bcopy((caddr_t)eh, mtod(m, struct ether_header *), sizeof(*eh)); 421 done: 422 *mp = m; 423 return error; 424 } 425 426 /****************************************************************** 427 NETGRAPH NODE METHODS 428 ******************************************************************/ 429 430 /* 431 * It is not possible or allowable to create a node of this type. 432 * Nodes get created when the interface is attached (or, when 433 * this node type's KLD is loaded). 434 */ 435 static int 436 ng_ether_constructor(node_p node) 437 { 438 return (EINVAL); 439 } 440 441 /* 442 * Check for attaching a new hook. 443 */ 444 static int 445 ng_ether_newhook(node_p node, hook_p hook, const char *name) 446 { 447 const priv_p priv = NG_NODE_PRIVATE(node); 448 u_char orphan = priv->lowerOrphan; 449 hook_p *hookptr; 450 451 /* Divert hook is an alias for lower */ 452 if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0) 453 name = NG_ETHER_HOOK_LOWER; 454 455 /* Which hook? */ 456 if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) 457 hookptr = &priv->upper; 458 else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) { 459 hookptr = &priv->lower; 460 orphan = 0; 461 } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) { 462 hookptr = &priv->lower; 463 orphan = 1; 464 } else 465 return (EINVAL); 466 467 /* Check if already connected (shouldn't be, but doesn't hurt) */ 468 if (*hookptr != NULL) 469 return (EISCONN); 470 471 /* OK */ 472 *hookptr = hook; 473 priv->lowerOrphan = orphan; 474 return (0); 475 } 476 477 /* 478 * Hooks are attached, adjust to force queueing. 479 * We don't really care which hook it is. 480 * they should all be queuing for outgoing data. 481 */ 482 static int 483 ng_ether_connect(hook_p hook) 484 { 485 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook)); 486 return (0); 487 } 488 489 /* 490 * Receive an incoming control message. 491 */ 492 static int 493 ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook) 494 { 495 const priv_p priv = NG_NODE_PRIVATE(node); 496 struct ng_mesg *resp = NULL; 497 int error = 0; 498 struct ng_mesg *msg; 499 500 NGI_GET_MSG(item, msg); 501 switch (msg->header.typecookie) { 502 case NGM_ETHER_COOKIE: 503 switch (msg->header.cmd) { 504 case NGM_ETHER_GET_IFNAME: 505 NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT); 506 if (resp == NULL) { 507 error = ENOMEM; 508 break; 509 } 510 snprintf(resp->data, IFNAMSIZ + 1, 511 "%s%d", priv->ifp->if_name, priv->ifp->if_unit); 512 break; 513 case NGM_ETHER_GET_IFINDEX: 514 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 515 if (resp == NULL) { 516 error = ENOMEM; 517 break; 518 } 519 *((u_int32_t *)resp->data) = priv->ifp->if_index; 520 break; 521 case NGM_ETHER_GET_ENADDR: 522 NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT); 523 if (resp == NULL) { 524 error = ENOMEM; 525 break; 526 } 527 bcopy((IFP2AC(priv->ifp))->ac_enaddr, 528 resp->data, ETHER_ADDR_LEN); 529 break; 530 case NGM_ETHER_SET_ENADDR: 531 { 532 if (msg->header.arglen != ETHER_ADDR_LEN) { 533 error = EINVAL; 534 break; 535 } 536 error = if_setlladdr(priv->ifp, 537 (u_char *)msg->data, ETHER_ADDR_LEN); 538 break; 539 } 540 case NGM_ETHER_GET_PROMISC: 541 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 542 if (resp == NULL) { 543 error = ENOMEM; 544 break; 545 } 546 *((u_int32_t *)resp->data) = priv->promisc; 547 break; 548 case NGM_ETHER_SET_PROMISC: 549 { 550 u_char want; 551 552 if (msg->header.arglen != sizeof(u_int32_t)) { 553 error = EINVAL; 554 break; 555 } 556 want = !!*((u_int32_t *)msg->data); 557 if (want ^ priv->promisc) { 558 if ((error = ifpromisc(priv->ifp, want)) != 0) 559 break; 560 priv->promisc = want; 561 } 562 break; 563 } 564 case NGM_ETHER_GET_AUTOSRC: 565 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 566 if (resp == NULL) { 567 error = ENOMEM; 568 break; 569 } 570 *((u_int32_t *)resp->data) = priv->autoSrcAddr; 571 break; 572 case NGM_ETHER_SET_AUTOSRC: 573 if (msg->header.arglen != sizeof(u_int32_t)) { 574 error = EINVAL; 575 break; 576 } 577 priv->autoSrcAddr = !!*((u_int32_t *)msg->data); 578 break; 579 default: 580 error = EINVAL; 581 break; 582 } 583 break; 584 default: 585 error = EINVAL; 586 break; 587 } 588 NG_RESPOND_MSG(error, node, item, resp); 589 NG_FREE_MSG(msg); 590 return (error); 591 } 592 593 /* 594 * Receive data on a hook. 595 */ 596 static int 597 ng_ether_rcvdata(hook_p hook, item_p item) 598 { 599 const node_p node = NG_HOOK_NODE(hook); 600 const priv_p priv = NG_NODE_PRIVATE(node); 601 struct mbuf *m; 602 meta_p meta; 603 604 NGI_GET_M(item, m); 605 NGI_GET_META(item, meta); 606 NG_FREE_ITEM(item); 607 if (hook == priv->lower) 608 return ng_ether_rcv_lower(node, m, meta); 609 if (hook == priv->upper) 610 return ng_ether_rcv_upper(node, m, meta); 611 panic("%s: weird hook", __FUNCTION__); 612 } 613 614 /* 615 * Handle an mbuf received on the "lower" hook. 616 */ 617 static int 618 ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta) 619 { 620 const priv_p priv = NG_NODE_PRIVATE(node); 621 622 /* Make sure header is fully pulled up */ 623 if (m->m_pkthdr.len < sizeof(struct ether_header)) { 624 NG_FREE_M(m); 625 NG_FREE_META(meta); 626 return (EINVAL); 627 } 628 if (m->m_len < sizeof(struct ether_header) 629 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) { 630 NG_FREE_META(meta); 631 return (ENOBUFS); 632 } 633 634 /* Drop in the MAC address if desired */ 635 if (priv->autoSrcAddr) { 636 bcopy((IFP2AC(priv->ifp))->ac_enaddr, 637 mtod(m, struct ether_header *)->ether_shost, 638 ETHER_ADDR_LEN); 639 } 640 641 /* Send it on its way */ 642 NG_FREE_META(meta); 643 return ether_output_frame(priv->ifp, m); 644 } 645 646 /* 647 * Handle an mbuf received on the "upper" hook. 648 */ 649 static int 650 ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta) 651 { 652 const priv_p priv = NG_NODE_PRIVATE(node); 653 struct ether_header *eh; 654 655 /* Check length and pull off header */ 656 if (m->m_pkthdr.len < sizeof(*eh)) { 657 NG_FREE_M(m); 658 NG_FREE_META(meta); 659 return (EINVAL); 660 } 661 if (m->m_len < sizeof(*eh) && (m = m_pullup(m, sizeof(*eh))) == NULL) { 662 NG_FREE_META(meta); 663 return (ENOBUFS); 664 } 665 eh = mtod(m, struct ether_header *); 666 m->m_data += sizeof(*eh); 667 m->m_len -= sizeof(*eh); 668 m->m_pkthdr.len -= sizeof(*eh); 669 m->m_pkthdr.rcvif = priv->ifp; 670 671 /* Route packet back in */ 672 NG_FREE_META(meta); 673 ether_demux(priv->ifp, eh, m); 674 return (0); 675 } 676 677 /* 678 * Shutdown node. This resets the node but does not remove it 679 * unless the REALLY_DIE flag is set. 680 */ 681 static int 682 ng_ether_shutdown(node_p node) 683 { 684 const priv_p priv = NG_NODE_PRIVATE(node); 685 686 if (priv->promisc) { /* disable promiscuous mode */ 687 (void)ifpromisc(priv->ifp, 0); 688 priv->promisc = 0; 689 } 690 if (node->nd_flags & NG_REALLY_DIE) { 691 /* 692 * WE came here because the ethernet card is being unloaded, 693 * so stop being persistant. 694 * Actually undo all the things we did on creation. 695 * Assume the ifp has already been freed. 696 */ 697 NG_NODE_SET_PRIVATE(node, NULL); 698 FREE(priv, M_NETGRAPH); 699 NG_NODE_UNREF(node); /* free node itself */ 700 return (0); 701 } 702 priv->autoSrcAddr = 1; /* reset auto-src-addr flag */ 703 node->nd_flags &= ~NG_INVALID; /* Signal ng_rmnode we are persisant */ 704 return (0); 705 } 706 707 /* 708 * Hook disconnection. 709 */ 710 static int 711 ng_ether_disconnect(hook_p hook) 712 { 713 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 714 715 if (hook == priv->upper) 716 priv->upper = NULL; 717 else if (hook == priv->lower) { 718 priv->lower = NULL; 719 priv->lowerOrphan = 0; 720 } else 721 panic("%s: weird hook", __FUNCTION__); 722 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 723 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 724 ng_rmnode_self(NG_HOOK_NODE(hook)); /* reset node */ 725 return (0); 726 } 727 728 static int 729 ng_enaddr_parse(const struct ng_parse_type *type, 730 const char *s, int *const off, const u_char *const start, 731 u_char *const buf, int *const buflen) 732 { 733 char *eptr; 734 u_long val; 735 int i; 736 737 if (*buflen < ETHER_ADDR_LEN) 738 return (ERANGE); 739 for (i = 0; i < ETHER_ADDR_LEN; i++) { 740 val = strtoul(s + *off, &eptr, 16); 741 if (val > 0xff || eptr == s + *off) 742 return (EINVAL); 743 buf[i] = (u_char)val; 744 *off = (eptr - s); 745 if (i < ETHER_ADDR_LEN - 1) { 746 if (*eptr != ':') 747 return (EINVAL); 748 (*off)++; 749 } 750 } 751 *buflen = ETHER_ADDR_LEN; 752 return (0); 753 } 754 755 static int 756 ng_enaddr_unparse(const struct ng_parse_type *type, 757 const u_char *data, int *off, char *cbuf, int cbuflen) 758 { 759 int len; 760 761 len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x", 762 data[*off], data[*off + 1], data[*off + 2], 763 data[*off + 3], data[*off + 4], data[*off + 5]); 764 if (len >= cbuflen) 765 return (ERANGE); 766 *off += ETHER_ADDR_LEN; 767 return (0); 768 } 769 770 /****************************************************************** 771 INITIALIZATION 772 ******************************************************************/ 773 774 /* 775 * Handle loading and unloading for this node type. 776 */ 777 static int 778 ng_ether_mod_event(module_t mod, int event, void *data) 779 { 780 struct ifnet *ifp; 781 int error = 0; 782 int s; 783 784 s = splnet(); 785 switch (event) { 786 case MOD_LOAD: 787 788 /* Register function hooks */ 789 if (ng_ether_attach_p != NULL) { 790 error = EEXIST; 791 break; 792 } 793 ng_ether_attach_p = ng_ether_attach; 794 ng_ether_detach_p = ng_ether_detach; 795 ng_ether_output_p = ng_ether_output; 796 ng_ether_input_p = ng_ether_input; 797 ng_ether_input_orphan_p = ng_ether_input_orphan; 798 799 /* Create nodes for any already-existing Ethernet interfaces */ 800 TAILQ_FOREACH(ifp, &ifnet, if_link) { 801 if (ifp->if_type == IFT_ETHER) 802 ng_ether_attach(ifp); 803 } 804 break; 805 806 case MOD_UNLOAD: 807 808 /* 809 * Note that the base code won't try to unload us until 810 * all nodes have been removed, and that can't happen 811 * until all Ethernet interfaces are removed. In any 812 * case, we know there are no nodes left if the action 813 * is MOD_UNLOAD, so there's no need to detach any nodes. 814 */ 815 816 /* Unregister function hooks */ 817 ng_ether_attach_p = NULL; 818 ng_ether_detach_p = NULL; 819 ng_ether_output_p = NULL; 820 ng_ether_input_p = NULL; 821 ng_ether_input_orphan_p = NULL; 822 break; 823 824 default: 825 error = EOPNOTSUPP; 826 break; 827 } 828 splx(s); 829 return (error); 830 } 831 832