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_inet.h" 56 #include "opt_inet6.h" 57 58 #include <sys/param.h> 59 #include <sys/systm.h> 60 #include <sys/errno.h> 61 #include <sys/kernel.h> 62 #include <sys/lock.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/rmlock.h> 69 #include <sys/sockio.h> 70 #include <sys/socket.h> 71 #include <sys/sysctl.h> 72 #include <sys/syslog.h> 73 #include <sys/libkern.h> 74 75 #include <net/if.h> 76 #include <net/if_var.h> 77 #include <net/if_types.h> 78 #include <net/bpf.h> 79 #include <net/netisr.h> 80 #include <net/route.h> 81 #include <net/vnet.h> 82 83 #include <netinet/in.h> 84 85 #include <netgraph/ng_message.h> 86 #include <netgraph/netgraph.h> 87 #include <netgraph/ng_parse.h> 88 #include <netgraph/ng_iface.h> 89 90 #ifdef NG_SEPARATE_MALLOC 91 static MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node"); 92 #else 93 #define M_NETGRAPH_IFACE M_NETGRAPH 94 #endif 95 96 static SYSCTL_NODE(_net_graph, OID_AUTO, iface, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 97 "Point to point netgraph interface"); 98 VNET_DEFINE_STATIC(int, ng_iface_max_nest) = 2; 99 #define V_ng_iface_max_nest VNET(ng_iface_max_nest) 100 SYSCTL_INT(_net_graph_iface, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW, 101 &VNET_NAME(ng_iface_max_nest), 0, "Max nested tunnels"); 102 103 /* This struct describes one address family */ 104 struct iffam { 105 sa_family_t family; /* Address family */ 106 const char *hookname; /* Name for hook */ 107 }; 108 typedef const struct iffam *iffam_p; 109 110 /* List of address families supported by our interface */ 111 const static struct iffam gFamilies[] = { 112 { AF_INET, NG_IFACE_HOOK_INET }, 113 { AF_INET6, NG_IFACE_HOOK_INET6 }, 114 { AF_ATM, NG_IFACE_HOOK_ATM }, 115 { AF_NATM, NG_IFACE_HOOK_NATM }, 116 }; 117 #define NUM_FAMILIES nitems(gFamilies) 118 119 /* Node private data */ 120 struct ng_iface_private { 121 struct ifnet *ifp; /* Our interface */ 122 int unit; /* Interface unit number */ 123 node_p node; /* Our netgraph node */ 124 hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */ 125 struct rmlock lock; /* Protect private data changes */ 126 }; 127 typedef struct ng_iface_private *priv_p; 128 129 #define PRIV_RLOCK(priv, t) rm_rlock(&priv->lock, t) 130 #define PRIV_RUNLOCK(priv, t) rm_runlock(&priv->lock, t) 131 #define PRIV_WLOCK(priv) rm_wlock(&priv->lock) 132 #define PRIV_WUNLOCK(priv) rm_wunlock(&priv->lock) 133 134 /* Interface methods */ 135 static void ng_iface_start(struct ifnet *ifp); 136 static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data); 137 static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0, 138 const struct sockaddr *dst, struct route *ro); 139 static void ng_iface_bpftap(struct ifnet *ifp, 140 struct mbuf *m, sa_family_t family); 141 static int ng_iface_send(struct ifnet *ifp, struct mbuf *m, 142 sa_family_t sa); 143 #ifdef DEBUG 144 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data); 145 #endif 146 147 /* Netgraph methods */ 148 static int ng_iface_mod_event(module_t, int, void *); 149 static ng_constructor_t ng_iface_constructor; 150 static ng_rcvmsg_t ng_iface_rcvmsg; 151 static ng_shutdown_t ng_iface_shutdown; 152 static ng_newhook_t ng_iface_newhook; 153 static ng_rcvdata_t ng_iface_rcvdata; 154 static ng_disconnect_t ng_iface_disconnect; 155 156 /* Helper stuff */ 157 static iffam_p get_iffam_from_af(sa_family_t family); 158 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook); 159 static iffam_p get_iffam_from_name(const char *name); 160 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam); 161 162 /* List of commands and how to convert arguments to/from ASCII */ 163 static const struct ng_cmdlist ng_iface_cmds[] = { 164 { 165 NGM_IFACE_COOKIE, 166 NGM_IFACE_GET_IFNAME, 167 "getifname", 168 NULL, 169 &ng_parse_string_type 170 }, 171 { 172 NGM_IFACE_COOKIE, 173 NGM_IFACE_POINT2POINT, 174 "point2point", 175 NULL, 176 NULL 177 }, 178 { 179 NGM_IFACE_COOKIE, 180 NGM_IFACE_BROADCAST, 181 "broadcast", 182 NULL, 183 NULL 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 VNET_DEFINE_STATIC(struct unrhdr *, ng_iface_unit); 211 #define V_ng_iface_unit VNET(ng_iface_unit) 212 213 /************************************************************************ 214 HELPER STUFF 215 ************************************************************************/ 216 217 /* 218 * Get the family descriptor from the family ID 219 */ 220 static __inline iffam_p 221 get_iffam_from_af(sa_family_t family) 222 { 223 iffam_p iffam; 224 int k; 225 226 for (k = 0; k < NUM_FAMILIES; k++) { 227 iffam = &gFamilies[k]; 228 if (iffam->family == family) 229 return (iffam); 230 } 231 return (NULL); 232 } 233 234 /* 235 * Get the family descriptor from the hook 236 */ 237 static __inline iffam_p 238 get_iffam_from_hook(priv_p priv, hook_p hook) 239 { 240 int k; 241 242 for (k = 0; k < NUM_FAMILIES; k++) 243 if (priv->hooks[k] == hook) 244 return (&gFamilies[k]); 245 return (NULL); 246 } 247 248 /* 249 * Get the hook from the iffam descriptor 250 */ 251 252 static __inline hook_p * 253 get_hook_from_iffam(priv_p priv, iffam_p iffam) 254 { 255 return (&priv->hooks[iffam - gFamilies]); 256 } 257 258 /* 259 * Get the iffam descriptor from the name 260 */ 261 static __inline iffam_p 262 get_iffam_from_name(const char *name) 263 { 264 iffam_p iffam; 265 int k; 266 267 for (k = 0; k < NUM_FAMILIES; k++) { 268 iffam = &gFamilies[k]; 269 if (!strcmp(iffam->hookname, name)) 270 return (iffam); 271 } 272 return (NULL); 273 } 274 275 /************************************************************************ 276 INTERFACE STUFF 277 ************************************************************************/ 278 279 /* 280 * Process an ioctl for the virtual interface 281 */ 282 static int 283 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 284 { 285 struct ifreq *const ifr = (struct ifreq *) data; 286 int error = 0; 287 288 #ifdef DEBUG 289 ng_iface_print_ioctl(ifp, command, data); 290 #endif 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 return (error); 343 } 344 345 /* 346 * This routine is called to deliver a packet out the interface. 347 * We simply look at the address family and relay the packet to 348 * the corresponding hook, if it exists and is connected. 349 */ 350 351 static int 352 ng_iface_output(struct ifnet *ifp, struct mbuf *m, 353 const struct sockaddr *dst, struct route *ro) 354 { 355 uint32_t af; 356 int error; 357 358 /* Check interface flags */ 359 if (!((ifp->if_flags & IFF_UP) && 360 (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 361 m_freem(m); 362 return (ENETDOWN); 363 } 364 365 /* Protect from deadly infinite recursion. */ 366 error = if_tunnel_check_nesting(ifp, m, NGM_IFACE_COOKIE, 367 V_ng_iface_max_nest); 368 if (error) { 369 m_freem(m); 370 return (error); 371 } 372 373 /* BPF writes need to be handled specially. */ 374 if (dst->sa_family == AF_UNSPEC) 375 bcopy(dst->sa_data, &af, sizeof(af)); 376 else 377 af = dst->sa_family; 378 379 /* Berkeley packet filter */ 380 ng_iface_bpftap(ifp, m, af); 381 382 if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 383 M_PREPEND(m, sizeof(sa_family_t), M_NOWAIT); 384 if (m == NULL) { 385 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); 386 return (ENOBUFS); 387 } 388 *(sa_family_t *)m->m_data = af; 389 error = (ifp->if_transmit)(ifp, m); 390 } else 391 error = ng_iface_send(ifp, m, af); 392 393 return (error); 394 } 395 396 /* 397 * Start method is used only when ALTQ is enabled. 398 */ 399 static void 400 ng_iface_start(struct ifnet *ifp) 401 { 402 struct mbuf *m; 403 sa_family_t sa; 404 405 KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__)); 406 407 for(;;) { 408 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 409 if (m == NULL) 410 break; 411 sa = *mtod(m, sa_family_t *); 412 m_adj(m, sizeof(sa_family_t)); 413 ng_iface_send(ifp, m, sa); 414 } 415 } 416 417 /* 418 * Flash a packet by the BPF (requires prepending 4 byte AF header) 419 * Note the phoney mbuf; this is OK because BPF treats it read-only. 420 */ 421 static void 422 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family) 423 { 424 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__)); 425 if (bpf_peers_present(ifp->if_bpf)) { 426 int32_t family4 = (int32_t)family; 427 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m); 428 } 429 } 430 431 /* 432 * This routine does actual delivery of the packet into the 433 * netgraph(4). It is called from ng_iface_start() and 434 * ng_iface_output(). 435 */ 436 static int 437 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa) 438 { 439 struct rm_priotracker priv_tracker; 440 const priv_p priv = (priv_p) ifp->if_softc; 441 const iffam_p iffam = get_iffam_from_af(sa); 442 hook_p hook; 443 int error; 444 int len; 445 446 /* Check address family to determine hook (if known) */ 447 if (iffam == NULL) { 448 m_freem(m); 449 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa); 450 return (EAFNOSUPPORT); 451 } 452 453 /* Copy length before the mbuf gets invalidated. */ 454 len = m->m_pkthdr.len; 455 456 PRIV_RLOCK(priv, &priv_tracker); 457 hook = *get_hook_from_iffam(priv, iffam); 458 if (hook == NULL) { 459 NG_FREE_M(m); 460 PRIV_RUNLOCK(priv, &priv_tracker); 461 return ENETDOWN; 462 } 463 NG_HOOK_REF(hook); 464 PRIV_RUNLOCK(priv, &priv_tracker); 465 466 NG_OUTBOUND_THREAD_REF(); 467 NG_SEND_DATA_ONLY(error, hook, m); 468 NG_OUTBOUND_THREAD_UNREF(); 469 NG_HOOK_UNREF(hook); 470 471 /* Update stats. */ 472 if (error == 0) { 473 if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 474 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 475 } 476 477 return (error); 478 } 479 480 #ifdef DEBUG 481 /* 482 * Display an ioctl to the virtual interface 483 */ 484 485 static void 486 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data) 487 { 488 char *str; 489 490 switch (command & IOC_DIRMASK) { 491 case IOC_VOID: 492 str = "IO"; 493 break; 494 case IOC_OUT: 495 str = "IOR"; 496 break; 497 case IOC_IN: 498 str = "IOW"; 499 break; 500 case IOC_INOUT: 501 str = "IORW"; 502 break; 503 default: 504 str = "IO??"; 505 } 506 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n", 507 ifp->if_xname, 508 str, 509 IOCGROUP(command), 510 command & 0xff, 511 IOCPARM_LEN(command)); 512 } 513 #endif /* DEBUG */ 514 515 /************************************************************************ 516 NETGRAPH NODE STUFF 517 ************************************************************************/ 518 519 /* 520 * Constructor for a node 521 */ 522 static int 523 ng_iface_constructor(node_p node) 524 { 525 struct ifnet *ifp; 526 priv_p priv; 527 528 /* Allocate node and interface private structures */ 529 priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | M_ZERO); 530 ifp = if_alloc(IFT_PROPVIRTUAL); 531 if (ifp == NULL) { 532 free(priv, M_NETGRAPH_IFACE); 533 return (ENOMEM); 534 } 535 536 rm_init(&priv->lock, "ng_iface private rmlock"); 537 538 /* Link them together */ 539 ifp->if_softc = priv; 540 priv->ifp = ifp; 541 542 /* Get an interface unit number */ 543 priv->unit = alloc_unr(V_ng_iface_unit); 544 545 /* Link together node and private info */ 546 NG_NODE_SET_PRIVATE(node, priv); 547 priv->node = node; 548 549 /* Initialize interface structure */ 550 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit); 551 ifp->if_output = ng_iface_output; 552 ifp->if_start = ng_iface_start; 553 ifp->if_ioctl = ng_iface_ioctl; 554 ifp->if_mtu = NG_IFACE_MTU_DEFAULT; 555 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST); 556 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */ 557 ifp->if_addrlen = 0; /* XXX */ 558 ifp->if_hdrlen = 0; /* XXX */ 559 ifp->if_baudrate = 64000; /* XXX */ 560 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 561 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 562 IFQ_SET_READY(&ifp->if_snd); 563 564 /* Give this node the same name as the interface (if possible) */ 565 if (ng_name_node(node, ifp->if_xname) != 0) 566 log(LOG_WARNING, "%s: can't acquire netgraph name\n", 567 ifp->if_xname); 568 569 /* Attach the interface */ 570 if_attach(ifp); 571 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t)); 572 573 /* Done */ 574 return (0); 575 } 576 577 /* 578 * Give our ok for a hook to be added 579 */ 580 static int 581 ng_iface_newhook(node_p node, hook_p hook, const char *name) 582 { 583 const iffam_p iffam = get_iffam_from_name(name); 584 const priv_p priv = NG_NODE_PRIVATE(node); 585 hook_p *hookptr; 586 587 if (iffam == NULL) 588 return (EPFNOSUPPORT); 589 PRIV_WLOCK(priv); 590 hookptr = get_hook_from_iffam(priv, iffam); 591 if (*hookptr != NULL) { 592 PRIV_WUNLOCK(priv); 593 return (EISCONN); 594 } 595 *hookptr = hook; 596 NG_HOOK_HI_STACK(hook); 597 NG_HOOK_SET_TO_INBOUND(hook); 598 PRIV_WUNLOCK(priv); 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_FLOW_COOKIE: 664 switch (msg->header.cmd) { 665 case NGM_LINK_IS_UP: 666 if_link_state_change(ifp, LINK_STATE_UP); 667 break; 668 case NGM_LINK_IS_DOWN: 669 if_link_state_change(ifp, LINK_STATE_DOWN); 670 break; 671 default: 672 break; 673 } 674 break; 675 default: 676 error = EINVAL; 677 break; 678 } 679 NG_RESPOND_MSG(error, node, item, resp); 680 NG_FREE_MSG(msg); 681 return (error); 682 } 683 684 /* 685 * Recive data from a hook. Pass the packet to the correct input routine. 686 */ 687 static int 688 ng_iface_rcvdata(hook_p hook, item_p item) 689 { 690 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 691 const iffam_p iffam = get_iffam_from_hook(priv, hook); 692 struct ifnet *const ifp = priv->ifp; 693 struct epoch_tracker et; 694 struct mbuf *m; 695 int isr; 696 697 NGI_GET_M(item, m); 698 NG_FREE_ITEM(item); 699 /* Sanity checks */ 700 KASSERT(iffam != NULL, ("%s: iffam", __func__)); 701 M_ASSERTPKTHDR(m); 702 if ((ifp->if_flags & IFF_UP) == 0) { 703 NG_FREE_M(m); 704 return (ENETDOWN); 705 } 706 707 /* Update interface stats */ 708 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 709 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 710 711 /* Note receiving interface */ 712 m->m_pkthdr.rcvif = ifp; 713 714 /* Berkeley packet filter */ 715 ng_iface_bpftap(ifp, m, iffam->family); 716 717 /* Send packet */ 718 switch (iffam->family) { 719 #ifdef INET 720 case AF_INET: 721 isr = NETISR_IP; 722 break; 723 #endif 724 #ifdef INET6 725 case AF_INET6: 726 isr = NETISR_IPV6; 727 break; 728 #endif 729 default: 730 m_freem(m); 731 return (EAFNOSUPPORT); 732 } 733 random_harvest_queue(m, sizeof(*m), RANDOM_NET_NG); 734 M_SETFIB(m, ifp->if_fib); 735 NET_EPOCH_ENTER(et); 736 netisr_dispatch(isr, m); 737 NET_EPOCH_EXIT(et); 738 return (0); 739 } 740 741 /* 742 * Shutdown and remove the node and its associated interface. 743 */ 744 static int 745 ng_iface_shutdown(node_p node) 746 { 747 const priv_p priv = NG_NODE_PRIVATE(node); 748 749 /* 750 * The ifnet may be in a different vnet than the netgraph node, 751 * hence we have to change the current vnet context here. 752 */ 753 CURVNET_SET_QUIET(priv->ifp->if_vnet); 754 bpfdetach(priv->ifp); 755 if_detach(priv->ifp); 756 if_free(priv->ifp); 757 CURVNET_RESTORE(); 758 priv->ifp = NULL; 759 free_unr(V_ng_iface_unit, priv->unit); 760 rm_destroy(&priv->lock); 761 free(priv, M_NETGRAPH_IFACE); 762 NG_NODE_SET_PRIVATE(node, NULL); 763 NG_NODE_UNREF(node); 764 return (0); 765 } 766 767 /* 768 * Hook disconnection. Note that we do *not* shutdown when all 769 * hooks have been disconnected. 770 */ 771 static int 772 ng_iface_disconnect(hook_p hook) 773 { 774 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 775 const iffam_p iffam = get_iffam_from_hook(priv, hook); 776 777 if (iffam == NULL) 778 panic("%s", __func__); 779 PRIV_WLOCK(priv); 780 *get_hook_from_iffam(priv, iffam) = NULL; 781 PRIV_WUNLOCK(priv); 782 return (0); 783 } 784 785 /* 786 * Handle loading and unloading for this node type. 787 */ 788 static int 789 ng_iface_mod_event(module_t mod, int event, void *data) 790 { 791 int error = 0; 792 793 switch (event) { 794 case MOD_LOAD: 795 case MOD_UNLOAD: 796 break; 797 default: 798 error = EOPNOTSUPP; 799 break; 800 } 801 return (error); 802 } 803 804 static void 805 vnet_ng_iface_init(const void *unused) 806 { 807 808 V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL); 809 } 810 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY, 811 vnet_ng_iface_init, NULL); 812 813 static void 814 vnet_ng_iface_uninit(const void *unused) 815 { 816 817 delete_unrhdr(V_ng_iface_unit); 818 } 819 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY, 820 vnet_ng_iface_uninit, NULL); 821