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