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