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$ 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 87 MALLOC_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 */ 93 struct iffam { 94 sa_family_t family; /* Address family */ 95 const char *hookname; /* Name for hook */ 96 }; 97 typedef const struct iffam *iffam_p; 98 99 /* List of address families supported by our interface */ 100 const 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 */ 111 struct 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 }; 117 typedef struct ng_iface_private *priv_p; 118 119 /* Interface methods */ 120 static void ng_iface_start(struct ifnet *ifp); 121 static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data); 122 static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0, 123 struct sockaddr *dst, struct rtentry *rt0); 124 static void ng_iface_bpftap(struct ifnet *ifp, 125 struct mbuf *m, sa_family_t family); 126 static int ng_iface_send(struct ifnet *ifp, struct mbuf *m, 127 sa_family_t sa); 128 #ifdef DEBUG 129 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data); 130 #endif 131 132 /* Netgraph methods */ 133 static int ng_iface_mod_event(module_t, int, void *); 134 static ng_constructor_t ng_iface_constructor; 135 static ng_rcvmsg_t ng_iface_rcvmsg; 136 static ng_shutdown_t ng_iface_shutdown; 137 static ng_newhook_t ng_iface_newhook; 138 static ng_rcvdata_t ng_iface_rcvdata; 139 static ng_disconnect_t ng_iface_disconnect; 140 141 /* Helper stuff */ 142 static iffam_p get_iffam_from_af(sa_family_t family); 143 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook); 144 static iffam_p get_iffam_from_name(const char *name); 145 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam); 146 147 /* Parse type for struct ng_cisco_ipaddr */ 148 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[] 149 = NG_CISCO_IPADDR_TYPE_INFO; 150 static const struct ng_parse_type ng_cisco_ipaddr_type = { 151 &ng_parse_struct_type, 152 &ng_cisco_ipaddr_type_fields 153 }; 154 155 /* List of commands and how to convert arguments to/from ASCII */ 156 static const struct ng_cmdlist ng_iface_cmds[] = { 157 { 158 NGM_IFACE_COOKIE, 159 NGM_IFACE_GET_IFNAME, 160 "getifname", 161 NULL, 162 &ng_parse_string_type 163 }, 164 { 165 NGM_IFACE_COOKIE, 166 NGM_IFACE_POINT2POINT, 167 "point2point", 168 NULL, 169 NULL 170 }, 171 { 172 NGM_IFACE_COOKIE, 173 NGM_IFACE_BROADCAST, 174 "broadcast", 175 NULL, 176 NULL 177 }, 178 { 179 NGM_CISCO_COOKIE, 180 NGM_CISCO_GET_IPADDR, 181 "getipaddr", 182 NULL, 183 &ng_cisco_ipaddr_type 184 }, 185 { 186 NGM_IFACE_COOKIE, 187 NGM_IFACE_GET_IFINDEX, 188 "getifindex", 189 NULL, 190 &ng_parse_uint32_type 191 }, 192 { 0 } 193 }; 194 195 /* Node type descriptor */ 196 static struct ng_type typestruct = { 197 .version = NG_ABI_VERSION, 198 .name = NG_IFACE_NODE_TYPE, 199 .mod_event = ng_iface_mod_event, 200 .constructor = ng_iface_constructor, 201 .rcvmsg = ng_iface_rcvmsg, 202 .shutdown = ng_iface_shutdown, 203 .newhook = ng_iface_newhook, 204 .rcvdata = ng_iface_rcvdata, 205 .disconnect = ng_iface_disconnect, 206 .cmdlist = ng_iface_cmds, 207 }; 208 NETGRAPH_INIT(iface, &typestruct); 209 210 static struct unrhdr *ng_iface_unit; 211 212 /************************************************************************ 213 HELPER STUFF 214 ************************************************************************/ 215 216 /* 217 * Get the family descriptor from the family ID 218 */ 219 static __inline iffam_p 220 get_iffam_from_af(sa_family_t family) 221 { 222 iffam_p iffam; 223 int k; 224 225 for (k = 0; k < NUM_FAMILIES; k++) { 226 iffam = &gFamilies[k]; 227 if (iffam->family == family) 228 return (iffam); 229 } 230 return (NULL); 231 } 232 233 /* 234 * Get the family descriptor from the hook 235 */ 236 static __inline iffam_p 237 get_iffam_from_hook(priv_p priv, hook_p hook) 238 { 239 int k; 240 241 for (k = 0; k < NUM_FAMILIES; k++) 242 if (priv->hooks[k] == hook) 243 return (&gFamilies[k]); 244 return (NULL); 245 } 246 247 /* 248 * Get the hook from the iffam descriptor 249 */ 250 251 static __inline hook_p * 252 get_hook_from_iffam(priv_p priv, iffam_p iffam) 253 { 254 return (&priv->hooks[iffam - gFamilies]); 255 } 256 257 /* 258 * Get the iffam descriptor from the name 259 */ 260 static __inline iffam_p 261 get_iffam_from_name(const char *name) 262 { 263 iffam_p iffam; 264 int k; 265 266 for (k = 0; k < NUM_FAMILIES; k++) { 267 iffam = &gFamilies[k]; 268 if (!strcmp(iffam->hookname, name)) 269 return (iffam); 270 } 271 return (NULL); 272 } 273 274 /************************************************************************ 275 INTERFACE STUFF 276 ************************************************************************/ 277 278 /* 279 * Process an ioctl for the virtual interface 280 */ 281 static int 282 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 283 { 284 struct ifreq *const ifr = (struct ifreq *) data; 285 int s, error = 0; 286 287 #ifdef DEBUG 288 ng_iface_print_ioctl(ifp, command, data); 289 #endif 290 s = splimp(); 291 switch (command) { 292 293 /* These two are mostly handled at a higher layer */ 294 case SIOCSIFADDR: 295 ifp->if_flags |= IFF_UP; 296 ifp->if_drv_flags |= IFF_DRV_RUNNING; 297 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE); 298 break; 299 case SIOCGIFADDR: 300 break; 301 302 /* Set flags */ 303 case SIOCSIFFLAGS: 304 /* 305 * If the interface is marked up and stopped, then start it. 306 * If it is marked down and running, then stop it. 307 */ 308 if (ifr->ifr_flags & IFF_UP) { 309 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 310 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE); 311 ifp->if_drv_flags |= IFF_DRV_RUNNING; 312 } 313 } else { 314 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 315 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | 316 IFF_DRV_OACTIVE); 317 } 318 break; 319 320 /* Set the interface MTU */ 321 case SIOCSIFMTU: 322 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX 323 || ifr->ifr_mtu < NG_IFACE_MTU_MIN) 324 error = EINVAL; 325 else 326 ifp->if_mtu = ifr->ifr_mtu; 327 break; 328 329 /* Stuff that's not supported */ 330 case SIOCADDMULTI: 331 case SIOCDELMULTI: 332 error = 0; 333 break; 334 case SIOCSIFPHYS: 335 error = EOPNOTSUPP; 336 break; 337 338 default: 339 error = EINVAL; 340 break; 341 } 342 (void) splx(s); 343 return (error); 344 } 345 346 /* 347 * This routine is called to deliver a packet out the interface. 348 * We simply look at the address family and relay the packet to 349 * the corresponding hook, if it exists and is connected. 350 */ 351 352 static int 353 ng_iface_output(struct ifnet *ifp, struct mbuf *m, 354 struct sockaddr *dst, struct rtentry *rt0) 355 { 356 uint32_t af; 357 int error; 358 359 /* Check interface flags */ 360 if (!((ifp->if_flags & IFF_UP) && 361 (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 362 m_freem(m); 363 return (ENETDOWN); 364 } 365 366 /* BPF writes need to be handled specially. */ 367 if (dst->sa_family == AF_UNSPEC) { 368 bcopy(dst->sa_data, &af, sizeof(af)); 369 dst->sa_family = af; 370 } 371 372 /* Berkeley packet filter */ 373 ng_iface_bpftap(ifp, m, dst->sa_family); 374 375 if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 376 M_PREPEND(m, sizeof(sa_family_t), M_DONTWAIT); 377 if (m == NULL) { 378 IFQ_LOCK(&ifp->if_snd); 379 IFQ_INC_DROPS(&ifp->if_snd); 380 IFQ_UNLOCK(&ifp->if_snd); 381 ifp->if_oerrors++; 382 return (ENOBUFS); 383 } 384 *(sa_family_t *)m->m_data = dst->sa_family; 385 IFQ_HANDOFF(ifp, m, error); 386 } else 387 error = ng_iface_send(ifp, m, dst->sa_family); 388 389 return (error); 390 } 391 392 /* 393 * Start method is used only when ALTQ is enabled. 394 */ 395 static void 396 ng_iface_start(struct ifnet *ifp) 397 { 398 struct mbuf *m; 399 sa_family_t sa; 400 401 KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__)); 402 403 for(;;) { 404 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 405 if (m == NULL) 406 break; 407 sa = *mtod(m, sa_family_t *); 408 m_adj(m, sizeof(sa_family_t)); 409 ng_iface_send(ifp, m, sa); 410 } 411 } 412 413 /* 414 * Flash a packet by the BPF (requires prepending 4 byte AF header) 415 * Note the phoney mbuf; this is OK because BPF treats it read-only. 416 */ 417 static void 418 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family) 419 { 420 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__)); 421 if (bpf_peers_present(ifp->if_bpf)) { 422 int32_t family4 = (int32_t)family; 423 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m); 424 } 425 } 426 427 /* 428 * This routine does actual delivery of the packet into the 429 * netgraph(4). It is called from ng_iface_start() and 430 * ng_iface_output(). 431 */ 432 static int 433 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa) 434 { 435 const priv_p priv = (priv_p) ifp->if_softc; 436 const iffam_p iffam = get_iffam_from_af(sa); 437 int error; 438 int len; 439 440 /* Check address family to determine hook (if known) */ 441 if (iffam == NULL) { 442 m_freem(m); 443 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa); 444 return (EAFNOSUPPORT); 445 } 446 447 /* Copy length before the mbuf gets invalidated. */ 448 len = m->m_pkthdr.len; 449 450 /* Send packet. If hook is not connected, 451 mbuf will get freed. */ 452 NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m); 453 454 /* Update stats. */ 455 if (error == 0) { 456 ifp->if_obytes += len; 457 ifp->if_opackets++; 458 } 459 460 return (error); 461 } 462 463 #ifdef DEBUG 464 /* 465 * Display an ioctl to the virtual interface 466 */ 467 468 static void 469 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data) 470 { 471 char *str; 472 473 switch (command & IOC_DIRMASK) { 474 case IOC_VOID: 475 str = "IO"; 476 break; 477 case IOC_OUT: 478 str = "IOR"; 479 break; 480 case IOC_IN: 481 str = "IOW"; 482 break; 483 case IOC_INOUT: 484 str = "IORW"; 485 break; 486 default: 487 str = "IO??"; 488 } 489 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n", 490 ifp->if_xname, 491 str, 492 IOCGROUP(command), 493 command & 0xff, 494 IOCPARM_LEN(command)); 495 } 496 #endif /* DEBUG */ 497 498 /************************************************************************ 499 NETGRAPH NODE STUFF 500 ************************************************************************/ 501 502 /* 503 * Constructor for a node 504 */ 505 static int 506 ng_iface_constructor(node_p node) 507 { 508 struct ifnet *ifp; 509 priv_p priv; 510 511 /* Allocate node and interface private structures */ 512 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_IFACE, M_NOWAIT|M_ZERO); 513 if (priv == NULL) 514 return (ENOMEM); 515 ifp = if_alloc(IFT_PROPVIRTUAL); 516 if (ifp == NULL) { 517 FREE(priv, M_NETGRAPH_IFACE); 518 return (ENOMEM); 519 } 520 521 /* Link them together */ 522 ifp->if_softc = priv; 523 priv->ifp = ifp; 524 525 /* Get an interface unit number */ 526 priv->unit = alloc_unr(ng_iface_unit); 527 528 /* Link together node and private info */ 529 NG_NODE_SET_PRIVATE(node, priv); 530 priv->node = node; 531 532 /* Initialize interface structure */ 533 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit); 534 ifp->if_output = ng_iface_output; 535 ifp->if_start = ng_iface_start; 536 ifp->if_ioctl = ng_iface_ioctl; 537 ifp->if_watchdog = NULL; 538 ifp->if_mtu = NG_IFACE_MTU_DEFAULT; 539 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST); 540 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */ 541 ifp->if_addrlen = 0; /* XXX */ 542 ifp->if_hdrlen = 0; /* XXX */ 543 ifp->if_baudrate = 64000; /* XXX */ 544 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 545 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 546 IFQ_SET_READY(&ifp->if_snd); 547 548 /* Give this node the same name as the interface (if possible) */ 549 if (ng_name_node(node, ifp->if_xname) != 0) 550 log(LOG_WARNING, "%s: can't acquire netgraph name\n", 551 ifp->if_xname); 552 553 /* Attach the interface */ 554 if_attach(ifp); 555 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t)); 556 557 /* Done */ 558 return (0); 559 } 560 561 /* 562 * Give our ok for a hook to be added 563 */ 564 static int 565 ng_iface_newhook(node_p node, hook_p hook, const char *name) 566 { 567 const iffam_p iffam = get_iffam_from_name(name); 568 hook_p *hookptr; 569 570 if (iffam == NULL) 571 return (EPFNOSUPPORT); 572 hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam); 573 if (*hookptr != NULL) 574 return (EISCONN); 575 *hookptr = hook; 576 NG_HOOK_HI_STACK(hook); 577 return (0); 578 } 579 580 /* 581 * Receive a control message 582 */ 583 static int 584 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook) 585 { 586 const priv_p priv = NG_NODE_PRIVATE(node); 587 struct ifnet *const ifp = priv->ifp; 588 struct ng_mesg *resp = NULL; 589 int error = 0; 590 struct ng_mesg *msg; 591 592 NGI_GET_MSG(item, msg); 593 switch (msg->header.typecookie) { 594 case NGM_IFACE_COOKIE: 595 switch (msg->header.cmd) { 596 case NGM_IFACE_GET_IFNAME: 597 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 598 if (resp == NULL) { 599 error = ENOMEM; 600 break; 601 } 602 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ); 603 break; 604 605 case NGM_IFACE_POINT2POINT: 606 case NGM_IFACE_BROADCAST: 607 { 608 609 /* Deny request if interface is UP */ 610 if ((ifp->if_flags & IFF_UP) != 0) 611 return (EBUSY); 612 613 /* Change flags */ 614 switch (msg->header.cmd) { 615 case NGM_IFACE_POINT2POINT: 616 ifp->if_flags |= IFF_POINTOPOINT; 617 ifp->if_flags &= ~IFF_BROADCAST; 618 break; 619 case NGM_IFACE_BROADCAST: 620 ifp->if_flags &= ~IFF_POINTOPOINT; 621 ifp->if_flags |= IFF_BROADCAST; 622 break; 623 } 624 break; 625 } 626 627 case NGM_IFACE_GET_IFINDEX: 628 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT); 629 if (resp == NULL) { 630 error = ENOMEM; 631 break; 632 } 633 *((uint32_t *)resp->data) = priv->ifp->if_index; 634 break; 635 636 default: 637 error = EINVAL; 638 break; 639 } 640 break; 641 case NGM_CISCO_COOKIE: 642 switch (msg->header.cmd) { 643 case NGM_CISCO_GET_IPADDR: /* we understand this too */ 644 { 645 struct ifaddr *ifa; 646 647 /* Return the first configured IP address */ 648 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 649 struct ng_cisco_ipaddr *ips; 650 651 if (ifa->ifa_addr->sa_family != AF_INET) 652 continue; 653 NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT); 654 if (resp == NULL) { 655 error = ENOMEM; 656 break; 657 } 658 ips = (struct ng_cisco_ipaddr *)resp->data; 659 ips->ipaddr = ((struct sockaddr_in *) 660 ifa->ifa_addr)->sin_addr; 661 ips->netmask = ((struct sockaddr_in *) 662 ifa->ifa_netmask)->sin_addr; 663 break; 664 } 665 666 /* No IP addresses on this interface? */ 667 if (ifa == NULL) 668 error = EADDRNOTAVAIL; 669 break; 670 } 671 default: 672 error = EINVAL; 673 break; 674 } 675 break; 676 case NGM_FLOW_COOKIE: 677 switch (msg->header.cmd) { 678 case NGM_LINK_IS_UP: 679 ifp->if_drv_flags |= IFF_DRV_RUNNING; 680 break; 681 case NGM_LINK_IS_DOWN: 682 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 683 break; 684 default: 685 break; 686 } 687 break; 688 default: 689 error = EINVAL; 690 break; 691 } 692 NG_RESPOND_MSG(error, node, item, resp); 693 NG_FREE_MSG(msg); 694 return (error); 695 } 696 697 /* 698 * Recive data from a hook. Pass the packet to the correct input routine. 699 */ 700 static int 701 ng_iface_rcvdata(hook_p hook, item_p item) 702 { 703 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 704 const iffam_p iffam = get_iffam_from_hook(priv, hook); 705 struct ifnet *const ifp = priv->ifp; 706 struct mbuf *m; 707 int isr; 708 709 NGI_GET_M(item, m); 710 NG_FREE_ITEM(item); 711 /* Sanity checks */ 712 KASSERT(iffam != NULL, ("%s: iffam", __func__)); 713 M_ASSERTPKTHDR(m); 714 if ((ifp->if_flags & IFF_UP) == 0) { 715 NG_FREE_M(m); 716 return (ENETDOWN); 717 } 718 719 /* Update interface stats */ 720 ifp->if_ipackets++; 721 ifp->if_ibytes += m->m_pkthdr.len; 722 723 /* Note receiving interface */ 724 m->m_pkthdr.rcvif = ifp; 725 726 /* Berkeley packet filter */ 727 ng_iface_bpftap(ifp, m, iffam->family); 728 729 /* Send packet */ 730 switch (iffam->family) { 731 #ifdef INET 732 case AF_INET: 733 isr = NETISR_IP; 734 break; 735 #endif 736 #ifdef INET6 737 case AF_INET6: 738 isr = NETISR_IPV6; 739 break; 740 #endif 741 #ifdef IPX 742 case AF_IPX: 743 isr = NETISR_IPX; 744 break; 745 #endif 746 #ifdef NETATALK 747 case AF_APPLETALK: 748 isr = NETISR_ATALK2; 749 break; 750 #endif 751 default: 752 m_freem(m); 753 return (EAFNOSUPPORT); 754 } 755 /* First chunk of an mbuf contains good junk */ 756 if (harvest.point_to_point) 757 random_harvest(m, 16, 3, 0, RANDOM_NET); 758 netisr_dispatch(isr, m); 759 return (0); 760 } 761 762 /* 763 * Shutdown and remove the node and its associated interface. 764 */ 765 static int 766 ng_iface_shutdown(node_p node) 767 { 768 const priv_p priv = NG_NODE_PRIVATE(node); 769 770 bpfdetach(priv->ifp); 771 if_detach(priv->ifp); 772 if_free(priv->ifp); 773 priv->ifp = NULL; 774 free_unr(ng_iface_unit, priv->unit); 775 FREE(priv, M_NETGRAPH_IFACE); 776 NG_NODE_SET_PRIVATE(node, NULL); 777 NG_NODE_UNREF(node); 778 return (0); 779 } 780 781 /* 782 * Hook disconnection. Note that we do *not* shutdown when all 783 * hooks have been disconnected. 784 */ 785 static int 786 ng_iface_disconnect(hook_p hook) 787 { 788 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 789 const iffam_p iffam = get_iffam_from_hook(priv, hook); 790 791 if (iffam == NULL) 792 panic(__func__); 793 *get_hook_from_iffam(priv, iffam) = NULL; 794 return (0); 795 } 796 797 /* 798 * Handle loading and unloading for this node type. 799 */ 800 static int 801 ng_iface_mod_event(module_t mod, int event, void *data) 802 { 803 int error = 0; 804 805 switch (event) { 806 case MOD_LOAD: 807 ng_iface_unit = new_unrhdr(0, 0xffff, NULL); 808 break; 809 case MOD_UNLOAD: 810 delete_unrhdr(ng_iface_unit); 811 break; 812 default: 813 error = EOPNOTSUPP; 814 break; 815 } 816 return (error); 817 } 818