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