ng_iface.c revision 147611
1/* 2 * ng_iface.c 3 */ 4 5/*- 6 * Copyright (c) 1996-1999 Whistle Communications, Inc. 7 * All rights reserved. 8 * 9 * Subject to the following obligations and disclaimer of warranty, use and 10 * redistribution of this software, in source or object code forms, with or 11 * without modifications are expressly permitted by Whistle Communications; 12 * provided, however, that: 13 * 1. Any and all reproductions of the source or object code must include the 14 * copyright notice above and the following disclaimer of warranties; and 15 * 2. No rights are granted, in any manner or form, to use Whistle 16 * Communications, Inc. trademarks, including the mark "WHISTLE 17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 18 * such appears in the above copyright notice or in the software. 19 * 20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 36 * OF SUCH DAMAGE. 37 * 38 * Author: Archie Cobbs <archie@freebsd.org> 39 * 40 * $FreeBSD: head/sys/netgraph/ng_iface.c 147611 2005-06-26 18:11:11Z dwmalone $ 41 * $Whistle: ng_iface.c,v 1.33 1999/11/01 09:24:51 julian Exp $ 42 */ 43 44/* 45 * This node is also a system networking interface. It has 46 * a hook for each protocol (IP, AppleTalk, IPX, etc). Packets 47 * are simply relayed between the interface and the hooks. 48 * 49 * Interfaces are named ng0, ng1, etc. New nodes take the 50 * first available interface name. 51 * 52 * This node also includes Berkeley packet filter support. 53 */ 54 55#include "opt_atalk.h" 56#include "opt_inet.h" 57#include "opt_inet6.h" 58#include "opt_ipx.h" 59 60#include <sys/param.h> 61#include <sys/systm.h> 62#include <sys/errno.h> 63#include <sys/kernel.h> 64#include <sys/malloc.h> 65#include <sys/mbuf.h> 66#include <sys/errno.h> 67#include <sys/random.h> 68#include <sys/sockio.h> 69#include <sys/socket.h> 70#include <sys/syslog.h> 71#include <sys/libkern.h> 72 73#include <net/if.h> 74#include <net/if_types.h> 75#include <net/bpf.h> 76#include <net/netisr.h> 77 78#include <netinet/in.h> 79 80#include <netgraph/ng_message.h> 81#include <netgraph/netgraph.h> 82#include <netgraph/ng_parse.h> 83#include <netgraph/ng_iface.h> 84#include <netgraph/ng_cisco.h> 85 86#ifdef NG_SEPARATE_MALLOC 87MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node "); 88#else 89#define M_NETGRAPH_IFACE M_NETGRAPH 90#endif 91 92/* This struct describes one address family */ 93struct iffam { 94 sa_family_t family; /* Address family */ 95 const char *hookname; /* Name for hook */ 96}; 97typedef const struct iffam *iffam_p; 98 99/* List of address families supported by our interface */ 100const static struct iffam gFamilies[] = { 101 { AF_INET, NG_IFACE_HOOK_INET }, 102 { AF_INET6, NG_IFACE_HOOK_INET6 }, 103 { AF_APPLETALK, NG_IFACE_HOOK_ATALK }, 104 { AF_IPX, NG_IFACE_HOOK_IPX }, 105 { AF_ATM, NG_IFACE_HOOK_ATM }, 106 { AF_NATM, NG_IFACE_HOOK_NATM }, 107}; 108#define NUM_FAMILIES (sizeof(gFamilies) / sizeof(*gFamilies)) 109 110/* Node private data */ 111struct ng_iface_private { 112 struct ifnet *ifp; /* Our interface */ 113 int unit; /* Interface unit number */ 114 node_p node; /* Our netgraph node */ 115 hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */ 116}; 117typedef struct ng_iface_private *priv_p; 118 119/* Interface methods */ 120static void ng_iface_start(struct ifnet *ifp); 121static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data); 122static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0, 123 struct sockaddr *dst, struct rtentry *rt0); 124static void ng_iface_bpftap(struct ifnet *ifp, 125 struct mbuf *m, sa_family_t family); 126#ifdef DEBUG 127static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data); 128#endif 129 130/* Netgraph methods */ 131static int ng_iface_mod_event(module_t, int, void *); 132static ng_constructor_t ng_iface_constructor; 133static ng_rcvmsg_t ng_iface_rcvmsg; 134static ng_shutdown_t ng_iface_shutdown; 135static ng_newhook_t ng_iface_newhook; 136static ng_rcvdata_t ng_iface_rcvdata; 137static ng_disconnect_t ng_iface_disconnect; 138 139/* Helper stuff */ 140static iffam_p get_iffam_from_af(sa_family_t family); 141static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook); 142static iffam_p get_iffam_from_name(const char *name); 143static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam); 144 145/* Parse type for struct ng_cisco_ipaddr */ 146static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[] 147 = NG_CISCO_IPADDR_TYPE_INFO; 148static const struct ng_parse_type ng_cisco_ipaddr_type = { 149 &ng_parse_struct_type, 150 &ng_cisco_ipaddr_type_fields 151}; 152 153/* List of commands and how to convert arguments to/from ASCII */ 154static const struct ng_cmdlist ng_iface_cmds[] = { 155 { 156 NGM_IFACE_COOKIE, 157 NGM_IFACE_GET_IFNAME, 158 "getifname", 159 NULL, 160 &ng_parse_string_type 161 }, 162 { 163 NGM_IFACE_COOKIE, 164 NGM_IFACE_POINT2POINT, 165 "point2point", 166 NULL, 167 NULL 168 }, 169 { 170 NGM_IFACE_COOKIE, 171 NGM_IFACE_BROADCAST, 172 "broadcast", 173 NULL, 174 NULL 175 }, 176 { 177 NGM_CISCO_COOKIE, 178 NGM_CISCO_GET_IPADDR, 179 "getipaddr", 180 NULL, 181 &ng_cisco_ipaddr_type 182 }, 183 { 184 NGM_IFACE_COOKIE, 185 NGM_IFACE_GET_IFINDEX, 186 "getifindex", 187 NULL, 188 &ng_parse_uint32_type 189 }, 190 { 0 } 191}; 192 193/* Node type descriptor */ 194static struct ng_type typestruct = { 195 .version = NG_ABI_VERSION, 196 .name = NG_IFACE_NODE_TYPE, 197 .mod_event = ng_iface_mod_event, 198 .constructor = ng_iface_constructor, 199 .rcvmsg = ng_iface_rcvmsg, 200 .shutdown = ng_iface_shutdown, 201 .newhook = ng_iface_newhook, 202 .rcvdata = ng_iface_rcvdata, 203 .disconnect = ng_iface_disconnect, 204 .cmdlist = ng_iface_cmds, 205}; 206NETGRAPH_INIT(iface, &typestruct); 207 208static struct unrhdr *ng_iface_unit; 209 210/************************************************************************ 211 HELPER STUFF 212 ************************************************************************/ 213 214/* 215 * Get the family descriptor from the family ID 216 */ 217static __inline iffam_p 218get_iffam_from_af(sa_family_t family) 219{ 220 iffam_p iffam; 221 int k; 222 223 for (k = 0; k < NUM_FAMILIES; k++) { 224 iffam = &gFamilies[k]; 225 if (iffam->family == family) 226 return (iffam); 227 } 228 return (NULL); 229} 230 231/* 232 * Get the family descriptor from the hook 233 */ 234static __inline iffam_p 235get_iffam_from_hook(priv_p priv, hook_p hook) 236{ 237 int k; 238 239 for (k = 0; k < NUM_FAMILIES; k++) 240 if (priv->hooks[k] == hook) 241 return (&gFamilies[k]); 242 return (NULL); 243} 244 245/* 246 * Get the hook from the iffam descriptor 247 */ 248 249static __inline hook_p * 250get_hook_from_iffam(priv_p priv, iffam_p iffam) 251{ 252 return (&priv->hooks[iffam - gFamilies]); 253} 254 255/* 256 * Get the iffam descriptor from the name 257 */ 258static __inline iffam_p 259get_iffam_from_name(const char *name) 260{ 261 iffam_p iffam; 262 int k; 263 264 for (k = 0; k < NUM_FAMILIES; k++) { 265 iffam = &gFamilies[k]; 266 if (!strcmp(iffam->hookname, name)) 267 return (iffam); 268 } 269 return (NULL); 270} 271 272/************************************************************************ 273 INTERFACE STUFF 274 ************************************************************************/ 275 276/* 277 * Process an ioctl for the virtual interface 278 */ 279static int 280ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 281{ 282 struct ifreq *const ifr = (struct ifreq *) data; 283 int s, error = 0; 284 285#ifdef DEBUG 286 ng_iface_print_ioctl(ifp, command, data); 287#endif 288 s = splimp(); 289 switch (command) { 290 291 /* These two are mostly handled at a higher layer */ 292 case SIOCSIFADDR: 293 ifp->if_flags |= (IFF_UP | IFF_RUNNING); 294 ifp->if_flags &= ~(IFF_OACTIVE); 295 break; 296 case SIOCGIFADDR: 297 break; 298 299 /* Set flags */ 300 case SIOCSIFFLAGS: 301 /* 302 * If the interface is marked up and stopped, then start it. 303 * If it is marked down and running, then stop it. 304 */ 305 if (ifr->ifr_flags & IFF_UP) { 306 if (!(ifp->if_flags & IFF_RUNNING)) { 307 ifp->if_flags &= ~(IFF_OACTIVE); 308 ifp->if_flags |= IFF_RUNNING; 309 } 310 } else { 311 if (ifp->if_flags & IFF_RUNNING) 312 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 313 } 314 break; 315 316 /* Set the interface MTU */ 317 case SIOCSIFMTU: 318 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX 319 || ifr->ifr_mtu < NG_IFACE_MTU_MIN) 320 error = EINVAL; 321 else 322 ifp->if_mtu = ifr->ifr_mtu; 323 break; 324 325 /* Stuff that's not supported */ 326 case SIOCADDMULTI: 327 case SIOCDELMULTI: 328 error = 0; 329 break; 330 case SIOCSIFPHYS: 331 error = EOPNOTSUPP; 332 break; 333 334 default: 335 error = EINVAL; 336 break; 337 } 338 (void) splx(s); 339 return (error); 340} 341 342/* 343 * This routine is called to deliver a packet out the interface. 344 * We simply look at the address family and relay the packet to 345 * the corresponding hook, if it exists and is connected. 346 */ 347 348static int 349ng_iface_output(struct ifnet *ifp, struct mbuf *m, 350 struct sockaddr *dst, struct rtentry *rt0) 351{ 352 const priv_p priv = (priv_p) ifp->if_softc; 353 const iffam_p iffam = get_iffam_from_af(dst->sa_family); 354 int len, error = 0; 355 u_int32_t af; 356 357 /* Check interface flags */ 358 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { 359 m_freem(m); 360 return (ENETDOWN); 361 } 362 363 /* BPF writes need to be handled specially. */ 364 if (dst->sa_family == AF_UNSPEC) { 365 bcopy(dst->sa_data, &af, sizeof(af)); 366 dst->sa_family = af; 367 } 368 369 /* Berkeley packet filter */ 370 ng_iface_bpftap(ifp, m, dst->sa_family); 371 372 /* Check address family to determine hook (if known) */ 373 if (iffam == NULL) { 374 m_freem(m); 375 log(LOG_WARNING, "%s: can't handle af%d\n", 376 ifp->if_xname, (int)dst->sa_family); 377 return (EAFNOSUPPORT); 378 } 379 380 /* Copy length before the mbuf gets invalidated */ 381 len = m->m_pkthdr.len; 382 383 /* Send packet; if hook is not connected, mbuf will get freed. */ 384 NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m); 385 386 /* Update stats */ 387 if (error == 0) { 388 ifp->if_obytes += len; 389 ifp->if_opackets++; 390 } 391 return (error); 392} 393 394/* 395 * This routine should never be called 396 */ 397 398static void 399ng_iface_start(struct ifnet *ifp) 400{ 401 if_printf(ifp, "%s called?", __func__); 402} 403 404/* 405 * Flash a packet by the BPF (requires prepending 4 byte AF header) 406 * Note the phoney mbuf; this is OK because BPF treats it read-only. 407 */ 408static void 409ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family) 410{ 411 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__)); 412 if (ifp->if_bpf != NULL) { 413 int32_t family4 = (int32_t)family; 414 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m); 415 } 416} 417 418#ifdef DEBUG 419/* 420 * Display an ioctl to the virtual interface 421 */ 422 423static void 424ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data) 425{ 426 char *str; 427 428 switch (command & IOC_DIRMASK) { 429 case IOC_VOID: 430 str = "IO"; 431 break; 432 case IOC_OUT: 433 str = "IOR"; 434 break; 435 case IOC_IN: 436 str = "IOW"; 437 break; 438 case IOC_INOUT: 439 str = "IORW"; 440 break; 441 default: 442 str = "IO??"; 443 } 444 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n", 445 ifp->if_xname, 446 str, 447 IOCGROUP(command), 448 command & 0xff, 449 IOCPARM_LEN(command)); 450} 451#endif /* DEBUG */ 452 453/************************************************************************ 454 NETGRAPH NODE STUFF 455 ************************************************************************/ 456 457/* 458 * Constructor for a node 459 */ 460static int 461ng_iface_constructor(node_p node) 462{ 463 struct ifnet *ifp; 464 priv_p priv; 465 466 /* Allocate node and interface private structures */ 467 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_IFACE, M_NOWAIT|M_ZERO); 468 if (priv == NULL) 469 return (ENOMEM); 470 ifp = if_alloc(IFT_PROPVIRTUAL); 471 if (ifp == NULL) { 472 FREE(priv, M_NETGRAPH_IFACE); 473 return (ENOMEM); 474 } 475 476 /* Link them together */ 477 ifp->if_softc = priv; 478 priv->ifp = ifp; 479 480 /* Get an interface unit number */ 481 priv->unit = alloc_unr(ng_iface_unit); 482 483 /* Link together node and private info */ 484 NG_NODE_SET_PRIVATE(node, priv); 485 priv->node = node; 486 487 /* Initialize interface structure */ 488 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit); 489 ifp->if_output = ng_iface_output; 490 ifp->if_start = ng_iface_start; 491 ifp->if_ioctl = ng_iface_ioctl; 492 ifp->if_watchdog = NULL; 493 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 494 ifp->if_mtu = NG_IFACE_MTU_DEFAULT; 495 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST); 496 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */ 497 ifp->if_addrlen = 0; /* XXX */ 498 ifp->if_hdrlen = 0; /* XXX */ 499 ifp->if_baudrate = 64000; /* XXX */ 500 501 /* Give this node the same name as the interface (if possible) */ 502 if (ng_name_node(node, ifp->if_xname) != 0) 503 log(LOG_WARNING, "%s: can't acquire netgraph name\n", 504 ifp->if_xname); 505 506 /* Attach the interface */ 507 if_attach(ifp); 508 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t)); 509 510 /* Done */ 511 return (0); 512} 513 514/* 515 * Give our ok for a hook to be added 516 */ 517static int 518ng_iface_newhook(node_p node, hook_p hook, const char *name) 519{ 520 const iffam_p iffam = get_iffam_from_name(name); 521 hook_p *hookptr; 522 523 if (iffam == NULL) 524 return (EPFNOSUPPORT); 525 hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam); 526 if (*hookptr != NULL) 527 return (EISCONN); 528 *hookptr = hook; 529 return (0); 530} 531 532/* 533 * Receive a control message 534 */ 535static int 536ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook) 537{ 538 const priv_p priv = NG_NODE_PRIVATE(node); 539 struct ifnet *const ifp = priv->ifp; 540 struct ng_mesg *resp = NULL; 541 int error = 0; 542 struct ng_mesg *msg; 543 544 NGI_GET_MSG(item, msg); 545 switch (msg->header.typecookie) { 546 case NGM_IFACE_COOKIE: 547 switch (msg->header.cmd) { 548 case NGM_IFACE_GET_IFNAME: 549 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 550 if (resp == NULL) { 551 error = ENOMEM; 552 break; 553 } 554 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ); 555 break; 556 557 case NGM_IFACE_POINT2POINT: 558 case NGM_IFACE_BROADCAST: 559 { 560 561 /* Deny request if interface is UP */ 562 if ((ifp->if_flags & IFF_UP) != 0) 563 return (EBUSY); 564 565 /* Change flags */ 566 switch (msg->header.cmd) { 567 case NGM_IFACE_POINT2POINT: 568 ifp->if_flags |= IFF_POINTOPOINT; 569 ifp->if_flags &= ~IFF_BROADCAST; 570 break; 571 case NGM_IFACE_BROADCAST: 572 ifp->if_flags &= ~IFF_POINTOPOINT; 573 ifp->if_flags |= IFF_BROADCAST; 574 break; 575 } 576 break; 577 } 578 579 case NGM_IFACE_GET_IFINDEX: 580 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT); 581 if (resp == NULL) { 582 error = ENOMEM; 583 break; 584 } 585 *((uint32_t *)resp->data) = priv->ifp->if_index; 586 break; 587 588 default: 589 error = EINVAL; 590 break; 591 } 592 break; 593 case NGM_CISCO_COOKIE: 594 switch (msg->header.cmd) { 595 case NGM_CISCO_GET_IPADDR: /* we understand this too */ 596 { 597 struct ifaddr *ifa; 598 599 /* Return the first configured IP address */ 600 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 601 struct ng_cisco_ipaddr *ips; 602 603 if (ifa->ifa_addr->sa_family != AF_INET) 604 continue; 605 NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT); 606 if (resp == NULL) { 607 error = ENOMEM; 608 break; 609 } 610 ips = (struct ng_cisco_ipaddr *)resp->data; 611 ips->ipaddr = ((struct sockaddr_in *) 612 ifa->ifa_addr)->sin_addr; 613 ips->netmask = ((struct sockaddr_in *) 614 ifa->ifa_netmask)->sin_addr; 615 break; 616 } 617 618 /* No IP addresses on this interface? */ 619 if (ifa == NULL) 620 error = EADDRNOTAVAIL; 621 break; 622 } 623 default: 624 error = EINVAL; 625 break; 626 } 627 break; 628 case NGM_FLOW_COOKIE: 629 switch (msg->header.cmd) { 630 case NGM_LINK_IS_UP: 631 ifp->if_flags |= IFF_RUNNING; 632 break; 633 case NGM_LINK_IS_DOWN: 634 ifp->if_flags &= ~IFF_RUNNING; 635 break; 636 default: 637 break; 638 } 639 break; 640 default: 641 error = EINVAL; 642 break; 643 } 644 NG_RESPOND_MSG(error, node, item, resp); 645 NG_FREE_MSG(msg); 646 return (error); 647} 648 649/* 650 * Recive data from a hook. Pass the packet to the correct input routine. 651 */ 652static int 653ng_iface_rcvdata(hook_p hook, item_p item) 654{ 655 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 656 const iffam_p iffam = get_iffam_from_hook(priv, hook); 657 struct ifnet *const ifp = priv->ifp; 658 struct mbuf *m; 659 int isr; 660 661 NGI_GET_M(item, m); 662 NG_FREE_ITEM(item); 663 /* Sanity checks */ 664 KASSERT(iffam != NULL, ("%s: iffam", __func__)); 665 M_ASSERTPKTHDR(m); 666 if ((ifp->if_flags & IFF_UP) == 0) { 667 NG_FREE_M(m); 668 return (ENETDOWN); 669 } 670 671 /* Update interface stats */ 672 ifp->if_ipackets++; 673 ifp->if_ibytes += m->m_pkthdr.len; 674 675 /* Note receiving interface */ 676 m->m_pkthdr.rcvif = ifp; 677 678 /* Berkeley packet filter */ 679 ng_iface_bpftap(ifp, m, iffam->family); 680 681 /* Send packet */ 682 switch (iffam->family) { 683#ifdef INET 684 case AF_INET: 685 isr = NETISR_IP; 686 break; 687#endif 688#ifdef INET6 689 case AF_INET6: 690 isr = NETISR_IPV6; 691 break; 692#endif 693#ifdef IPX 694 case AF_IPX: 695 isr = NETISR_IPX; 696 break; 697#endif 698#ifdef NETATALK 699 case AF_APPLETALK: 700 isr = NETISR_ATALK2; 701 break; 702#endif 703 default: 704 m_freem(m); 705 return (EAFNOSUPPORT); 706 } 707 /* First chunk of an mbuf contains good junk */ 708 if (harvest.point_to_point) 709 random_harvest(m, 16, 3, 0, RANDOM_NET); 710 netisr_dispatch(isr, m); 711 return (0); 712} 713 714/* 715 * Shutdown and remove the node and its associated interface. 716 */ 717static int 718ng_iface_shutdown(node_p node) 719{ 720 const priv_p priv = NG_NODE_PRIVATE(node); 721 722 bpfdetach(priv->ifp); 723 if_detach(priv->ifp); 724 if_free(priv->ifp); 725 priv->ifp = NULL; 726 free_unr(ng_iface_unit, priv->unit); 727 FREE(priv, M_NETGRAPH_IFACE); 728 NG_NODE_SET_PRIVATE(node, NULL); 729 NG_NODE_UNREF(node); 730 return (0); 731} 732 733/* 734 * Hook disconnection. Note that we do *not* shutdown when all 735 * hooks have been disconnected. 736 */ 737static int 738ng_iface_disconnect(hook_p hook) 739{ 740 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 741 const iffam_p iffam = get_iffam_from_hook(priv, hook); 742 743 if (iffam == NULL) 744 panic(__func__); 745 *get_hook_from_iffam(priv, iffam) = NULL; 746 return (0); 747} 748 749/* 750 * Handle loading and unloading for this node type. 751 */ 752static int 753ng_iface_mod_event(module_t mod, int event, void *data) 754{ 755 int error = 0; 756 757 switch (event) { 758 case MOD_LOAD: 759 ng_iface_unit = new_unrhdr(0, 0xffff, NULL); 760 break; 761 case MOD_UNLOAD: 762 delete_unrhdr(ng_iface_unit); 763 break; 764 default: 765 error = EOPNOTSUPP; 766 break; 767 } 768 return (error); 769} 770