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