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 * $Whistle: ng_iface.c,v 1.33 1999/11/01 09:24:51 julian Exp $ 40 */ 41 42 /* 43 * This node is also a system networking interface. It has 44 * a hook for each protocol (IP, AppleTalk, etc). Packets 45 * are simply relayed between the interface and the hooks. 46 * 47 * Interfaces are named ng0, ng1, etc. New nodes take the 48 * first available interface name. 49 * 50 * This node also includes Berkeley packet filter support. 51 */ 52 53 #include "opt_inet.h" 54 #include "opt_inet6.h" 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/errno.h> 59 #include <sys/kernel.h> 60 #include <sys/lock.h> 61 #include <sys/malloc.h> 62 #include <sys/mbuf.h> 63 #include <sys/errno.h> 64 #include <sys/proc.h> 65 #include <sys/random.h> 66 #include <sys/rmlock.h> 67 #include <sys/sockio.h> 68 #include <sys/socket.h> 69 #include <sys/sysctl.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_private.h> 76 #include <net/if_types.h> 77 #include <net/bpf.h> 78 #include <net/netisr.h> 79 #include <net/route.h> 80 #include <net/vnet.h> 81 82 #include <netinet/in.h> 83 84 #include <netgraph/ng_message.h> 85 #include <netgraph/netgraph.h> 86 #include <netgraph/ng_parse.h> 87 #include <netgraph/ng_iface.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 static SYSCTL_NODE(_net_graph, OID_AUTO, iface, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 96 "Point to point netgraph interface"); 97 VNET_DEFINE_STATIC(int, ng_iface_max_nest) = 2; 98 #define V_ng_iface_max_nest VNET(ng_iface_max_nest) 99 SYSCTL_INT(_net_graph_iface, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW, 100 &VNET_NAME(ng_iface_max_nest), 0, "Max nested tunnels"); 101 102 /* This struct describes one address family */ 103 struct iffam { 104 sa_family_t family; /* Address family */ 105 const char *hookname; /* Name for hook */ 106 }; 107 typedef const struct iffam *iffam_p; 108 109 /* List of address families supported by our interface */ 110 const static struct iffam gFamilies[] = { 111 { AF_INET, NG_IFACE_HOOK_INET }, 112 { AF_INET6, NG_IFACE_HOOK_INET6 }, 113 }; 114 #define NUM_FAMILIES nitems(gFamilies) 115 116 /* Node private data */ 117 struct ng_iface_private { 118 struct ifnet *ifp; /* Our interface */ 119 int unit; /* Interface unit number */ 120 node_p node; /* Our netgraph node */ 121 hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */ 122 struct rmlock lock; /* Protect private data changes */ 123 }; 124 typedef struct ng_iface_private *priv_p; 125 126 #define PRIV_RLOCK(priv, t) rm_rlock(&priv->lock, t) 127 #define PRIV_RUNLOCK(priv, t) rm_runlock(&priv->lock, t) 128 #define PRIV_WLOCK(priv) rm_wlock(&priv->lock) 129 #define PRIV_WUNLOCK(priv) rm_wunlock(&priv->lock) 130 131 /* Interface methods */ 132 static void ng_iface_start(struct ifnet *ifp); 133 static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data); 134 static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0, 135 const struct sockaddr *dst, struct route *ro); 136 static void ng_iface_bpftap(struct ifnet *ifp, 137 struct mbuf *m, sa_family_t family); 138 static int ng_iface_send(struct ifnet *ifp, struct mbuf *m, 139 sa_family_t sa); 140 #ifdef DEBUG 141 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data); 142 #endif 143 144 /* Netgraph methods */ 145 static int ng_iface_mod_event(module_t, int, void *); 146 static ng_constructor_t ng_iface_constructor; 147 static ng_rcvmsg_t ng_iface_rcvmsg; 148 static ng_shutdown_t ng_iface_shutdown; 149 static ng_newhook_t ng_iface_newhook; 150 static ng_rcvdata_t ng_iface_rcvdata; 151 static ng_disconnect_t ng_iface_disconnect; 152 153 /* Helper stuff */ 154 static iffam_p get_iffam_from_af(sa_family_t family); 155 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook); 156 static iffam_p get_iffam_from_name(const char *name); 157 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam); 158 159 /* List of commands and how to convert arguments to/from ASCII */ 160 static const struct ng_cmdlist ng_iface_cmds[] = { 161 { 162 NGM_IFACE_COOKIE, 163 NGM_IFACE_GET_IFNAME, 164 "getifname", 165 NULL, 166 &ng_parse_string_type 167 }, 168 { 169 NGM_IFACE_COOKIE, 170 NGM_IFACE_POINT2POINT, 171 "point2point", 172 NULL, 173 NULL 174 }, 175 { 176 NGM_IFACE_COOKIE, 177 NGM_IFACE_BROADCAST, 178 "broadcast", 179 NULL, 180 NULL 181 }, 182 { 183 NGM_IFACE_COOKIE, 184 NGM_IFACE_GET_IFINDEX, 185 "getifindex", 186 NULL, 187 &ng_parse_uint32_type 188 }, 189 { 0 } 190 }; 191 192 /* Node type descriptor */ 193 static struct ng_type typestruct = { 194 .version = NG_ABI_VERSION, 195 .name = NG_IFACE_NODE_TYPE, 196 .mod_event = ng_iface_mod_event, 197 .constructor = ng_iface_constructor, 198 .rcvmsg = ng_iface_rcvmsg, 199 .shutdown = ng_iface_shutdown, 200 .newhook = ng_iface_newhook, 201 .rcvdata = ng_iface_rcvdata, 202 .disconnect = ng_iface_disconnect, 203 .cmdlist = ng_iface_cmds, 204 }; 205 NETGRAPH_INIT(iface, &typestruct); 206 207 VNET_DEFINE_STATIC(struct unrhdr *, ng_iface_unit); 208 #define V_ng_iface_unit VNET(ng_iface_unit) 209 210 /************************************************************************ 211 HELPER STUFF 212 ************************************************************************/ 213 214 /* 215 * Get the family descriptor from the family ID 216 */ 217 static __inline iffam_p 218 get_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 */ 234 static __inline iffam_p 235 get_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 249 static __inline hook_p * 250 get_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 */ 258 static __inline iffam_p 259 get_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 */ 279 static int 280 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 281 { 282 struct ifreq *const ifr = (struct ifreq *) data; 283 int error = 0; 284 285 #ifdef DEBUG 286 ng_iface_print_ioctl(ifp, command, data); 287 #endif 288 switch (command) { 289 /* These two are mostly handled at a higher layer */ 290 case SIOCSIFADDR: 291 ifp->if_flags |= IFF_UP; 292 ifp->if_drv_flags |= IFF_DRV_RUNNING; 293 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE); 294 break; 295 case SIOCGIFADDR: 296 break; 297 298 /* Set flags */ 299 case SIOCSIFFLAGS: 300 /* 301 * If the interface is marked up and stopped, then start it. 302 * If it is marked down and running, then stop it. 303 */ 304 if (ifr->ifr_flags & IFF_UP) { 305 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 306 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE); 307 ifp->if_drv_flags |= IFF_DRV_RUNNING; 308 } 309 } else { 310 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 311 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | 312 IFF_DRV_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 return (error); 339 } 340 341 /* 342 * This routine is called to deliver a packet out the interface. 343 * We simply look at the address family and relay the packet to 344 * the corresponding hook, if it exists and is connected. 345 */ 346 347 static int 348 ng_iface_output(struct ifnet *ifp, struct mbuf *m, 349 const struct sockaddr *dst, struct route *ro) 350 { 351 uint32_t af; 352 int error; 353 354 /* Check interface flags */ 355 if (!((ifp->if_flags & IFF_UP) && 356 (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 357 m_freem(m); 358 return (ENETDOWN); 359 } 360 361 /* Protect from deadly infinite recursion. */ 362 error = if_tunnel_check_nesting(ifp, m, NGM_IFACE_COOKIE, 363 V_ng_iface_max_nest); 364 if (error) { 365 m_freem(m); 366 return (error); 367 } 368 369 /* BPF writes need to be handled specially. */ 370 if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT) 371 bcopy(dst->sa_data, &af, sizeof(af)); 372 else 373 af = RO_GET_FAMILY(ro, dst); 374 375 /* Berkeley packet filter */ 376 ng_iface_bpftap(ifp, m, af); 377 378 if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 379 M_PREPEND(m, sizeof(sa_family_t), M_NOWAIT); 380 if (m == NULL) { 381 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); 382 return (ENOBUFS); 383 } 384 *(sa_family_t *)m->m_data = af; 385 error = (ifp->if_transmit)(ifp, m); 386 } else 387 error = ng_iface_send(ifp, m, af); 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 struct rm_priotracker priv_tracker; 436 const priv_p priv = (priv_p) ifp->if_softc; 437 const iffam_p iffam = get_iffam_from_af(sa); 438 hook_p hook; 439 int error; 440 int len; 441 442 /* Check address family to determine hook (if known) */ 443 if (iffam == NULL) { 444 m_freem(m); 445 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa); 446 return (EAFNOSUPPORT); 447 } 448 449 /* Copy length before the mbuf gets invalidated. */ 450 len = m->m_pkthdr.len; 451 452 PRIV_RLOCK(priv, &priv_tracker); 453 hook = *get_hook_from_iffam(priv, iffam); 454 if (hook == NULL) { 455 NG_FREE_M(m); 456 PRIV_RUNLOCK(priv, &priv_tracker); 457 return ENETDOWN; 458 } 459 NG_HOOK_REF(hook); 460 PRIV_RUNLOCK(priv, &priv_tracker); 461 462 NG_OUTBOUND_THREAD_REF(); 463 NG_SEND_DATA_ONLY(error, hook, m); 464 NG_OUTBOUND_THREAD_UNREF(); 465 NG_HOOK_UNREF(hook); 466 467 /* Update stats. */ 468 if (error == 0) { 469 if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 470 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 471 } 472 473 return (error); 474 } 475 476 #ifdef DEBUG 477 /* 478 * Display an ioctl to the virtual interface 479 */ 480 481 static void 482 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data) 483 { 484 char *str; 485 486 switch (command & IOC_DIRMASK) { 487 case IOC_VOID: 488 str = "IO"; 489 break; 490 case IOC_OUT: 491 str = "IOR"; 492 break; 493 case IOC_IN: 494 str = "IOW"; 495 break; 496 case IOC_INOUT: 497 str = "IORW"; 498 break; 499 default: 500 str = "IO??"; 501 } 502 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n", 503 ifp->if_xname, 504 str, 505 IOCGROUP(command), 506 command & 0xff, 507 IOCPARM_LEN(command)); 508 } 509 #endif /* DEBUG */ 510 511 /************************************************************************ 512 NETGRAPH NODE STUFF 513 ************************************************************************/ 514 515 /* 516 * Constructor for a node 517 */ 518 static int 519 ng_iface_constructor(node_p node) 520 { 521 struct ifnet *ifp; 522 priv_p priv; 523 524 /* Allocate node and interface private structures */ 525 priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | M_ZERO); 526 ifp = if_alloc(IFT_PROPVIRTUAL); 527 528 rm_init(&priv->lock, "ng_iface private rmlock"); 529 530 /* Link them together */ 531 ifp->if_softc = priv; 532 priv->ifp = ifp; 533 534 /* Get an interface unit number */ 535 priv->unit = alloc_unr(V_ng_iface_unit); 536 537 /* Link together node and private info */ 538 NG_NODE_SET_PRIVATE(node, priv); 539 priv->node = node; 540 541 /* Initialize interface structure */ 542 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit); 543 ifp->if_output = ng_iface_output; 544 ifp->if_start = ng_iface_start; 545 ifp->if_ioctl = ng_iface_ioctl; 546 ifp->if_mtu = NG_IFACE_MTU_DEFAULT; 547 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST); 548 ifp->if_addrlen = 0; /* XXX */ 549 ifp->if_hdrlen = 0; /* XXX */ 550 ifp->if_baudrate = 64000; /* XXX */ 551 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 552 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 553 IFQ_SET_READY(&ifp->if_snd); 554 555 /* Give this node the same name as the interface (if possible) */ 556 if (ng_name_node(node, ifp->if_xname) != 0) 557 log(LOG_WARNING, "%s: can't acquire netgraph name\n", 558 ifp->if_xname); 559 560 /* Attach the interface */ 561 if_attach(ifp); 562 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t)); 563 564 /* Done */ 565 return (0); 566 } 567 568 /* 569 * Give our ok for a hook to be added 570 */ 571 static int 572 ng_iface_newhook(node_p node, hook_p hook, const char *name) 573 { 574 const iffam_p iffam = get_iffam_from_name(name); 575 const priv_p priv = NG_NODE_PRIVATE(node); 576 hook_p *hookptr; 577 578 if (iffam == NULL) 579 return (EPFNOSUPPORT); 580 PRIV_WLOCK(priv); 581 hookptr = get_hook_from_iffam(priv, iffam); 582 if (*hookptr != NULL) { 583 PRIV_WUNLOCK(priv); 584 return (EISCONN); 585 } 586 *hookptr = hook; 587 NG_HOOK_HI_STACK(hook); 588 NG_HOOK_SET_TO_INBOUND(hook); 589 PRIV_WUNLOCK(priv); 590 return (0); 591 } 592 593 /* 594 * Receive a control message 595 */ 596 static int 597 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook) 598 { 599 const priv_p priv = NG_NODE_PRIVATE(node); 600 struct ifnet *const ifp = priv->ifp; 601 struct ng_mesg *resp = NULL; 602 int error = 0; 603 struct ng_mesg *msg; 604 605 NGI_GET_MSG(item, msg); 606 switch (msg->header.typecookie) { 607 case NGM_IFACE_COOKIE: 608 switch (msg->header.cmd) { 609 case NGM_IFACE_GET_IFNAME: 610 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT); 611 if (resp == NULL) { 612 error = ENOMEM; 613 break; 614 } 615 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ); 616 break; 617 618 case NGM_IFACE_POINT2POINT: 619 case NGM_IFACE_BROADCAST: 620 { 621 /* Deny request if interface is UP */ 622 if ((ifp->if_flags & IFF_UP) != 0) 623 return (EBUSY); 624 625 /* Change flags */ 626 switch (msg->header.cmd) { 627 case NGM_IFACE_POINT2POINT: 628 ifp->if_flags |= IFF_POINTOPOINT; 629 ifp->if_flags &= ~IFF_BROADCAST; 630 break; 631 case NGM_IFACE_BROADCAST: 632 ifp->if_flags &= ~IFF_POINTOPOINT; 633 ifp->if_flags |= IFF_BROADCAST; 634 break; 635 } 636 break; 637 } 638 639 case NGM_IFACE_GET_IFINDEX: 640 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT); 641 if (resp == NULL) { 642 error = ENOMEM; 643 break; 644 } 645 *((uint32_t *)resp->data) = priv->ifp->if_index; 646 break; 647 648 default: 649 error = EINVAL; 650 break; 651 } 652 break; 653 case NGM_FLOW_COOKIE: 654 switch (msg->header.cmd) { 655 case NGM_LINK_IS_UP: 656 if_link_state_change(ifp, LINK_STATE_UP); 657 break; 658 case NGM_LINK_IS_DOWN: 659 if_link_state_change(ifp, LINK_STATE_DOWN); 660 break; 661 default: 662 break; 663 } 664 break; 665 default: 666 error = EINVAL; 667 break; 668 } 669 NG_RESPOND_MSG(error, node, item, resp); 670 NG_FREE_MSG(msg); 671 return (error); 672 } 673 674 /* 675 * Recive data from a hook. Pass the packet to the correct input routine. 676 */ 677 static int 678 ng_iface_rcvdata(hook_p hook, item_p item) 679 { 680 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 681 const iffam_p iffam = get_iffam_from_hook(priv, hook); 682 struct ifnet *const ifp = priv->ifp; 683 struct epoch_tracker et; 684 struct mbuf *m; 685 int isr; 686 687 NGI_GET_M(item, m); 688 NG_FREE_ITEM(item); 689 /* Sanity checks */ 690 KASSERT(iffam != NULL, ("%s: iffam", __func__)); 691 M_ASSERTPKTHDR(m); 692 if ((ifp->if_flags & IFF_UP) == 0) { 693 NG_FREE_M(m); 694 return (ENETDOWN); 695 } 696 697 /* Update interface stats */ 698 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 699 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 700 701 /* Note receiving interface */ 702 m->m_pkthdr.rcvif = ifp; 703 704 /* Berkeley packet filter */ 705 ng_iface_bpftap(ifp, m, iffam->family); 706 707 /* Send packet */ 708 switch (iffam->family) { 709 #ifdef INET 710 case AF_INET: 711 isr = NETISR_IP; 712 break; 713 #endif 714 #ifdef INET6 715 case AF_INET6: 716 isr = NETISR_IPV6; 717 break; 718 #endif 719 default: 720 m_freem(m); 721 return (EAFNOSUPPORT); 722 } 723 random_harvest_queue(m, sizeof(*m), RANDOM_NET_NG); 724 M_SETFIB(m, ifp->if_fib); 725 CURVNET_SET(ifp->if_vnet); 726 NET_EPOCH_ENTER(et); 727 netisr_dispatch(isr, m); 728 NET_EPOCH_EXIT(et); 729 CURVNET_RESTORE(); 730 return (0); 731 } 732 733 /* 734 * Shutdown and remove the node and its associated interface. 735 */ 736 static int 737 ng_iface_shutdown(node_p node) 738 { 739 const priv_p priv = NG_NODE_PRIVATE(node); 740 741 /* 742 * The ifnet may be in a different vnet than the netgraph node, 743 * hence we have to change the current vnet context here. 744 */ 745 CURVNET_SET_QUIET(priv->ifp->if_vnet); 746 bpfdetach(priv->ifp); 747 if_detach(priv->ifp); 748 if_free(priv->ifp); 749 CURVNET_RESTORE(); 750 priv->ifp = NULL; 751 free_unr(V_ng_iface_unit, priv->unit); 752 rm_destroy(&priv->lock); 753 free(priv, M_NETGRAPH_IFACE); 754 NG_NODE_SET_PRIVATE(node, NULL); 755 NG_NODE_UNREF(node); 756 return (0); 757 } 758 759 /* 760 * Hook disconnection. Note that we do *not* shutdown when all 761 * hooks have been disconnected. 762 */ 763 static int 764 ng_iface_disconnect(hook_p hook) 765 { 766 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 767 const iffam_p iffam = get_iffam_from_hook(priv, hook); 768 769 if (iffam == NULL) 770 panic("%s", __func__); 771 PRIV_WLOCK(priv); 772 *get_hook_from_iffam(priv, iffam) = NULL; 773 PRIV_WUNLOCK(priv); 774 return (0); 775 } 776 777 /* 778 * Handle loading and unloading for this node type. 779 */ 780 static int 781 ng_iface_mod_event(module_t mod, int event, void *data) 782 { 783 int error = 0; 784 785 switch (event) { 786 case MOD_LOAD: 787 case MOD_UNLOAD: 788 break; 789 default: 790 error = EOPNOTSUPP; 791 break; 792 } 793 return (error); 794 } 795 796 static void 797 vnet_ng_iface_init(const void *unused) 798 { 799 800 V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL); 801 } 802 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY, 803 vnet_ng_iface_init, NULL); 804 805 static void 806 vnet_ng_iface_uninit(const void *unused) 807 { 808 809 delete_unrhdr(V_ng_iface_unit); 810 } 811 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY, 812 vnet_ng_iface_uninit, NULL); 813