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 }; 79 typedef struct private *priv_p; 80 81 /* Functional hooks called from if_ethersubr.c */ 82 static void ng_ether_input(struct ifnet *ifp, 83 struct mbuf **mp, struct ether_header *eh); 84 static void ng_ether_input_orphan(struct ifnet *ifp, 85 struct mbuf *m, struct ether_header *eh); 86 static int ng_ether_output(struct ifnet *ifp, struct mbuf **mp); 87 static void ng_ether_attach(struct ifnet *ifp); 88 static void ng_ether_detach(struct ifnet *ifp); 89 90 /* Other functions */ 91 static void ng_ether_input2(node_p node, 92 struct mbuf **mp, struct ether_header *eh); 93 static int ng_ether_glueback_header(struct mbuf **mp, 94 struct ether_header *eh); 95 static int ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta); 96 static int ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta); 97 98 /* Netgraph node methods */ 99 static ng_constructor_t ng_ether_constructor; 100 static ng_rcvmsg_t ng_ether_rcvmsg; 101 static ng_shutdown_t ng_ether_rmnode; 102 static ng_newhook_t ng_ether_newhook; 103 static ng_rcvdata_t ng_ether_rcvdata; 104 static ng_disconnect_t ng_ether_disconnect; 105 static int ng_ether_mod_event(module_t mod, int event, void *data); 106 107 /* Parse type for an Ethernet address */ 108 static ng_parse_t ng_enaddr_parse; 109 static ng_unparse_t ng_enaddr_unparse; 110 const struct ng_parse_type ng_ether_enaddr_type = { 111 NULL, 112 NULL, 113 NULL, 114 ng_enaddr_parse, 115 ng_enaddr_unparse, 116 NULL, /* no such thing as a "default" EN address */ 117 0 118 }; 119 120 /* List of commands and how to convert arguments to/from ASCII */ 121 static const struct ng_cmdlist ng_ether_cmdlist[] = { 122 { 123 NGM_ETHER_COOKIE, 124 NGM_ETHER_GET_IFNAME, 125 "getifname", 126 NULL, 127 &ng_parse_string_type 128 }, 129 { 130 NGM_ETHER_COOKIE, 131 NGM_ETHER_GET_IFINDEX, 132 "getifindex", 133 NULL, 134 &ng_parse_int32_type 135 }, 136 { 137 NGM_ETHER_COOKIE, 138 NGM_ETHER_GET_ENADDR, 139 "getenaddr", 140 NULL, 141 &ng_ether_enaddr_type 142 }, 143 { 144 NGM_ETHER_COOKIE, 145 NGM_ETHER_SET_ENADDR, 146 "setenaddr", 147 &ng_ether_enaddr_type, 148 NULL 149 }, 150 { 151 NGM_ETHER_COOKIE, 152 NGM_ETHER_GET_PROMISC, 153 "getpromisc", 154 NULL, 155 &ng_parse_int32_type 156 }, 157 { 158 NGM_ETHER_COOKIE, 159 NGM_ETHER_SET_PROMISC, 160 "setpromisc", 161 &ng_parse_int32_type, 162 NULL 163 }, 164 { 165 NGM_ETHER_COOKIE, 166 NGM_ETHER_GET_AUTOSRC, 167 "getautosrc", 168 NULL, 169 &ng_parse_int32_type 170 }, 171 { 172 NGM_ETHER_COOKIE, 173 NGM_ETHER_SET_AUTOSRC, 174 "setautosrc", 175 &ng_parse_int32_type, 176 NULL 177 }, 178 { 0 } 179 }; 180 181 static struct ng_type ng_ether_typestruct = { 182 NG_VERSION, 183 NG_ETHER_NODE_TYPE, 184 ng_ether_mod_event, 185 ng_ether_constructor, 186 ng_ether_rcvmsg, 187 ng_ether_rmnode, 188 ng_ether_newhook, 189 NULL, 190 NULL, 191 ng_ether_rcvdata, 192 ng_ether_rcvdata, 193 ng_ether_disconnect, 194 ng_ether_cmdlist, 195 }; 196 NETGRAPH_INIT(ether, &ng_ether_typestruct); 197 198 /****************************************************************** 199 ETHERNET FUNCTION HOOKS 200 ******************************************************************/ 201 202 /* 203 * Handle a packet that has come in on an interface. We get to 204 * look at it here before any upper layer protocols do. 205 * 206 * NOTE: this function will get called at splimp() 207 */ 208 static void 209 ng_ether_input(struct ifnet *ifp, 210 struct mbuf **mp, struct ether_header *eh) 211 { 212 const node_p node = IFP2NG(ifp); 213 const priv_p priv = node->private; 214 215 /* If "lower" hook not connected, let packet continue */ 216 if (priv->lower == NULL || priv->lowerOrphan) 217 return; 218 ng_ether_input2(node, mp, eh); 219 } 220 221 /* 222 * Handle a packet that has come in on an interface, and which 223 * does not match any of our known protocols (an ``orphan''). 224 * 225 * NOTE: this function will get called at splimp() 226 */ 227 static void 228 ng_ether_input_orphan(struct ifnet *ifp, 229 struct mbuf *m, struct ether_header *eh) 230 { 231 const node_p node = IFP2NG(ifp); 232 const priv_p priv = node->private; 233 234 /* If "orphan" hook not connected, let packet continue */ 235 if (priv->lower == NULL || !priv->lowerOrphan) { 236 m_freem(m); 237 return; 238 } 239 ng_ether_input2(node, &m, eh); 240 if (m != NULL) 241 m_freem(m); 242 } 243 244 /* 245 * Handle a packet that has come in on an interface. 246 * The Ethernet header has already been detached from the mbuf, 247 * so we have to put it back. 248 * 249 * NOTE: this function will get called at splimp() 250 */ 251 static void 252 ng_ether_input2(node_p node, struct mbuf **mp, struct ether_header *eh) 253 { 254 const priv_p priv = node->private; 255 meta_p meta = NULL; 256 int error; 257 258 /* Glue Ethernet header back on */ 259 if ((error = ng_ether_glueback_header(mp, eh)) != 0) 260 return; 261 262 /* Send out lower/orphan hook */ 263 (void)ng_queue_data(priv->lower, *mp, meta); 264 *mp = NULL; 265 } 266 267 /* 268 * Handle a packet that is going out on an interface. 269 * The Ethernet header is already attached to the mbuf. 270 */ 271 static int 272 ng_ether_output(struct ifnet *ifp, struct mbuf **mp) 273 { 274 const node_p node = IFP2NG(ifp); 275 const priv_p priv = node->private; 276 meta_p meta = NULL; 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_RET(error, priv->upper, *mp, meta); 285 286 /* If we got a reflected packet back, handle it */ 287 if (error == 0 && *mp != NULL) { 288 error = ng_ether_rcv_upper(node, *mp, meta); 289 *mp = NULL; 290 } 291 return (error); 292 } 293 294 /* 295 * A new Ethernet interface has been attached. 296 * Create a new node for it, etc. 297 */ 298 static void 299 ng_ether_attach(struct ifnet *ifp) 300 { 301 char name[IFNAMSIZ + 1]; 302 priv_p priv; 303 node_p node; 304 305 /* Create node */ 306 KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __FUNCTION__)); 307 snprintf(name, sizeof(name), "%s%d", ifp->if_name, ifp->if_unit); 308 if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) { 309 log(LOG_ERR, "%s: can't %s for %s\n", 310 __FUNCTION__, "create node", name); 311 return; 312 } 313 314 /* Allocate private data */ 315 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 316 if (priv == NULL) { 317 log(LOG_ERR, "%s: can't %s for %s\n", 318 __FUNCTION__, "allocate memory", name); 319 ng_unref(node); 320 return; 321 } 322 node->private = priv; 323 priv->ifp = ifp; 324 IFP2NG(ifp) = node; 325 priv->autoSrcAddr = 1; 326 327 /* Try to give the node the same name as the interface */ 328 if (ng_name_node(node, name) != 0) { 329 log(LOG_WARNING, "%s: can't name node %s\n", 330 __FUNCTION__, name); 331 } 332 } 333 334 /* 335 * An Ethernet interface is being detached. 336 * Destroy its node. 337 */ 338 static void 339 ng_ether_detach(struct ifnet *ifp) 340 { 341 const node_p node = IFP2NG(ifp); 342 priv_p priv; 343 344 if (node == NULL) /* no node (why not?), ignore */ 345 return; 346 ng_rmnode(node); /* break all links to other nodes */ 347 node->flags |= NG_INVALID; 348 ng_unname(node); /* free name (and its reference) */ 349 IFP2NG(ifp) = NULL; /* detach node from interface */ 350 priv = node->private; /* free node private info */ 351 bzero(priv, sizeof(*priv)); 352 FREE(priv, M_NETGRAPH); 353 node->private = NULL; 354 ng_unref(node); /* free node itself */ 355 } 356 357 /* 358 * Optimization for gluing the Ethernet header back onto 359 * the front of an incoming packet. 360 */ 361 static int 362 ng_ether_glueback_header(struct mbuf **mp, struct ether_header *eh) 363 { 364 struct mbuf *m = *mp; 365 uintfptr_t room; 366 int error = 0; 367 368 /* 369 * Possibly the header is already on the front. 370 * If this is the case so just move the markers back 371 * to re-include it. We lucked out. 372 * This allows us to avoid a yucky m_pullup 373 * in later nodes if it works. 374 */ 375 if (eh == mtod(m, struct ether_header *) - 1) { 376 m->m_len += sizeof(*eh); 377 m->m_data -= sizeof(*eh); 378 m->m_pkthdr.len += sizeof(*eh); 379 goto done; 380 } 381 382 /* 383 * Alternatively there may be room even though 384 * it is stored somewhere else. If so, copy it in. 385 * This only safe because we KNOW that this packet has 386 * just been generated by an ethernet card, so there are 387 * no aliases to the buffer (not so for outgoing packets). 388 * Nearly all ethernet cards will end up producing mbufs 389 * that fall into these cases. So we are not optimizing 390 * contorted cases. 391 */ 392 if ((m->m_flags & M_EXT) != 0) { 393 room = mtod(m, caddr_t) - m->m_ext.ext_buf; 394 if (room > m->m_ext.ext_size) /* garbage, fail immediately */ 395 room = 0; 396 } else 397 room = mtod(m, caddr_t) - m->m_pktdat; 398 399 /* 400 * If we have room, just copy it and adjust 401 */ 402 if (room >= sizeof(*eh)) { 403 m->m_len += sizeof(*eh); 404 m->m_data -= sizeof(*eh); 405 m->m_pkthdr.len += sizeof(*eh); 406 goto copy; 407 } 408 409 /* 410 * Doing anything more is likely to get more 411 * expensive than it's worth.. 412 * it's probable that everything else is in one 413 * big lump. The next node will do an m_pullup() 414 * for exactly the amount of data it needs and 415 * hopefully everything after that will not 416 * need one. So let's just use M_PREPEND. 417 */ 418 M_PREPEND(m, sizeof (*eh), M_DONTWAIT); 419 if (m == NULL) { 420 error = ENOBUFS; 421 goto done; 422 } 423 424 copy: 425 /* Copy header and return (possibly new) mbuf */ 426 bcopy((caddr_t)eh, mtod(m, struct ether_header *), sizeof(*eh)); 427 done: 428 *mp = m; 429 return error; 430 } 431 432 /****************************************************************** 433 NETGRAPH NODE METHODS 434 ******************************************************************/ 435 436 /* 437 * It is not possible or allowable to create a node of this type. 438 * Nodes get created when the interface is attached (or, when 439 * this node type's KLD is loaded). 440 */ 441 static int 442 ng_ether_constructor(node_p *nodep) 443 { 444 return (EINVAL); 445 } 446 447 /* 448 * Check for attaching a new hook. 449 */ 450 static int 451 ng_ether_newhook(node_p node, hook_p hook, const char *name) 452 { 453 const priv_p priv = node->private; 454 u_char orphan = priv->lowerOrphan; 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 else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) { 465 hookptr = &priv->lower; 466 orphan = 0; 467 } else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) { 468 hookptr = &priv->lower; 469 orphan = 1; 470 } else 471 return (EINVAL); 472 473 /* Check if already connected (shouldn't be, but doesn't hurt) */ 474 if (*hookptr != NULL) 475 return (EISCONN); 476 477 /* OK */ 478 *hookptr = hook; 479 priv->lowerOrphan = orphan; 480 return (0); 481 } 482 483 /* 484 * Receive an incoming control message. 485 */ 486 static int 487 ng_ether_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr, 488 struct ng_mesg **rptr, hook_p lasthook) 489 { 490 const priv_p priv = node->private; 491 struct ng_mesg *resp = NULL; 492 int error = 0; 493 494 switch (msg->header.typecookie) { 495 case NGM_ETHER_COOKIE: 496 switch (msg->header.cmd) { 497 case NGM_ETHER_GET_IFNAME: 498 NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT); 499 if (resp == NULL) { 500 error = ENOMEM; 501 break; 502 } 503 snprintf(resp->data, IFNAMSIZ + 1, 504 "%s%d", priv->ifp->if_name, priv->ifp->if_unit); 505 break; 506 case NGM_ETHER_GET_IFINDEX: 507 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 508 if (resp == NULL) { 509 error = ENOMEM; 510 break; 511 } 512 *((u_int32_t *)resp->data) = priv->ifp->if_index; 513 break; 514 case NGM_ETHER_GET_ENADDR: 515 NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT); 516 if (resp == NULL) { 517 error = ENOMEM; 518 break; 519 } 520 bcopy((IFP2AC(priv->ifp))->ac_enaddr, 521 resp->data, ETHER_ADDR_LEN); 522 break; 523 case NGM_ETHER_SET_ENADDR: 524 { 525 if (msg->header.arglen != ETHER_ADDR_LEN) { 526 error = EINVAL; 527 break; 528 } 529 error = if_setlladdr(priv->ifp, 530 (u_char *)msg->data, ETHER_ADDR_LEN); 531 break; 532 } 533 case NGM_ETHER_GET_PROMISC: 534 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 535 if (resp == NULL) { 536 error = ENOMEM; 537 break; 538 } 539 *((u_int32_t *)resp->data) = priv->promisc; 540 break; 541 case NGM_ETHER_SET_PROMISC: 542 { 543 u_char want; 544 545 if (msg->header.arglen != sizeof(u_int32_t)) { 546 error = EINVAL; 547 break; 548 } 549 want = !!*((u_int32_t *)msg->data); 550 if (want ^ priv->promisc) { 551 if ((error = ifpromisc(priv->ifp, want)) != 0) 552 break; 553 priv->promisc = want; 554 } 555 break; 556 } 557 case NGM_ETHER_GET_AUTOSRC: 558 NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT); 559 if (resp == NULL) { 560 error = ENOMEM; 561 break; 562 } 563 *((u_int32_t *)resp->data) = priv->autoSrcAddr; 564 break; 565 case NGM_ETHER_SET_AUTOSRC: 566 if (msg->header.arglen != sizeof(u_int32_t)) { 567 error = EINVAL; 568 break; 569 } 570 priv->autoSrcAddr = !!*((u_int32_t *)msg->data); 571 break; 572 default: 573 error = EINVAL; 574 break; 575 } 576 break; 577 default: 578 error = EINVAL; 579 break; 580 } 581 if (rptr) 582 *rptr = resp; 583 else if (resp != NULL) 584 FREE(resp, M_NETGRAPH); 585 FREE(msg, M_NETGRAPH); 586 return (error); 587 } 588 589 /* 590 * Receive data on a hook. 591 */ 592 static int 593 ng_ether_rcvdata(hook_p hook, struct mbuf *m, meta_p meta, 594 struct mbuf **ret_m, meta_p *ret_meta) 595 { 596 const node_p node = hook->node; 597 const priv_p priv = node->private; 598 599 if (hook == priv->lower) 600 return ng_ether_rcv_lower(node, m, meta); 601 if (hook == priv->upper) 602 return ng_ether_rcv_upper(node, m, meta); 603 panic("%s: weird hook", __FUNCTION__); 604 } 605 606 /* 607 * Handle an mbuf received on the "lower" hook. 608 */ 609 static int 610 ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta) 611 { 612 const priv_p priv = node->private; 613 614 /* Make sure header is fully pulled up */ 615 if (m->m_pkthdr.len < sizeof(struct ether_header)) { 616 NG_FREE_DATA(m, meta); 617 return (EINVAL); 618 } 619 if (m->m_len < sizeof(struct ether_header) 620 && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) { 621 NG_FREE_META(meta); 622 return (ENOBUFS); 623 } 624 625 /* Drop in the MAC address if desired */ 626 if (priv->autoSrcAddr) { 627 bcopy((IFP2AC(priv->ifp))->ac_enaddr, 628 mtod(m, struct ether_header *)->ether_shost, 629 ETHER_ADDR_LEN); 630 } 631 632 /* Send it on its way */ 633 NG_FREE_META(meta); 634 return ether_output_frame(priv->ifp, m); 635 } 636 637 /* 638 * Handle an mbuf received on the "upper" hook. 639 */ 640 static int 641 ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta) 642 { 643 const priv_p priv = node->private; 644 struct ether_header *eh; 645 646 /* Check length and pull off header */ 647 if (m->m_pkthdr.len < sizeof(*eh)) { 648 NG_FREE_DATA(m, meta); 649 return (EINVAL); 650 } 651 if (m->m_len < sizeof(*eh) && (m = m_pullup(m, sizeof(*eh))) == NULL) { 652 NG_FREE_META(meta); 653 return (ENOBUFS); 654 } 655 eh = mtod(m, struct ether_header *); 656 m->m_data += sizeof(*eh); 657 m->m_len -= sizeof(*eh); 658 m->m_pkthdr.len -= sizeof(*eh); 659 m->m_pkthdr.rcvif = priv->ifp; 660 661 /* Route packet back in */ 662 NG_FREE_META(meta); 663 ether_demux(priv->ifp, eh, m); 664 return (0); 665 } 666 667 /* 668 * Shutdown node. This resets the node but does not remove it. 669 */ 670 static int 671 ng_ether_rmnode(node_p node) 672 { 673 const priv_p priv = node->private; 674 675 ng_cutlinks(node); 676 node->flags &= ~NG_INVALID; /* bounce back to life */ 677 if (priv->promisc) { /* disable promiscuous mode */ 678 (void)ifpromisc(priv->ifp, 0); 679 priv->promisc = 0; 680 } 681 priv->autoSrcAddr = 1; /* reset auto-src-addr flag */ 682 return (0); 683 } 684 685 /* 686 * Hook disconnection. 687 */ 688 static int 689 ng_ether_disconnect(hook_p hook) 690 { 691 const priv_p priv = hook->node->private; 692 693 if (hook == priv->upper) 694 priv->upper = NULL; 695 else if (hook == priv->lower) { 696 priv->lower = NULL; 697 priv->lowerOrphan = 0; 698 } else 699 panic("%s: weird hook", __FUNCTION__); 700 return (0); 701 } 702 703 static int 704 ng_enaddr_parse(const struct ng_parse_type *type, 705 const char *s, int *const off, const u_char *const start, 706 u_char *const buf, int *const buflen) 707 { 708 char *eptr; 709 u_long val; 710 int i; 711 712 if (*buflen < ETHER_ADDR_LEN) 713 return (ERANGE); 714 for (i = 0; i < ETHER_ADDR_LEN; i++) { 715 val = strtoul(s + *off, &eptr, 16); 716 if (val > 0xff || eptr == s + *off) 717 return (EINVAL); 718 buf[i] = (u_char)val; 719 *off = (eptr - s); 720 if (i < ETHER_ADDR_LEN - 1) { 721 if (*eptr != ':') 722 return (EINVAL); 723 (*off)++; 724 } 725 } 726 *buflen = ETHER_ADDR_LEN; 727 return (0); 728 } 729 730 static int 731 ng_enaddr_unparse(const struct ng_parse_type *type, 732 const u_char *data, int *off, char *cbuf, int cbuflen) 733 { 734 int len; 735 736 len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x", 737 data[*off], data[*off + 1], data[*off + 2], 738 data[*off + 3], data[*off + 4], data[*off + 5]); 739 if (len >= cbuflen) 740 return (ERANGE); 741 *off += ETHER_ADDR_LEN; 742 return (0); 743 } 744 745 /****************************************************************** 746 INITIALIZATION 747 ******************************************************************/ 748 749 /* 750 * Handle loading and unloading for this node type. 751 */ 752 static int 753 ng_ether_mod_event(module_t mod, int event, void *data) 754 { 755 struct ifnet *ifp; 756 int error = 0; 757 int s; 758 759 s = splnet(); 760 switch (event) { 761 case MOD_LOAD: 762 763 /* Register function hooks */ 764 if (ng_ether_attach_p != NULL) { 765 error = EEXIST; 766 break; 767 } 768 ng_ether_attach_p = ng_ether_attach; 769 ng_ether_detach_p = ng_ether_detach; 770 ng_ether_output_p = ng_ether_output; 771 ng_ether_input_p = ng_ether_input; 772 ng_ether_input_orphan_p = ng_ether_input_orphan; 773 774 /* Create nodes for any already-existing Ethernet interfaces */ 775 TAILQ_FOREACH(ifp, &ifnet, if_link) { 776 if (ifp->if_type == IFT_ETHER) 777 ng_ether_attach(ifp); 778 } 779 break; 780 781 case MOD_UNLOAD: 782 783 /* 784 * Note that the base code won't try to unload us until 785 * all nodes have been removed, and that can't happen 786 * until all Ethernet interfaces are removed. In any 787 * case, we know there are no nodes left if the action 788 * is MOD_UNLOAD, so there's no need to detach any nodes. 789 */ 790 791 /* Unregister function hooks */ 792 ng_ether_attach_p = NULL; 793 ng_ether_detach_p = NULL; 794 ng_ether_output_p = NULL; 795 ng_ether_input_p = NULL; 796 ng_ether_input_orphan_p = NULL; 797 break; 798 799 default: 800 error = EOPNOTSUPP; 801 break; 802 } 803 splx(s); 804 return (error); 805 } 806 807