ip_divert.c revision 134793
1317021Sdim/* 2317021Sdim * Copyright (c) 1982, 1986, 1988, 1993 3317021Sdim * The Regents of the University of California. All rights reserved. 4317021Sdim * 5317021Sdim * Redistribution and use in source and binary forms, with or without 6317021Sdim * modification, are permitted provided that the following conditions 7317021Sdim * are met: 8317021Sdim * 1. Redistributions of source code must retain the above copyright 9317021Sdim * notice, this list of conditions and the following disclaimer. 10317021Sdim * 2. Redistributions in binary form must reproduce the above copyright 11317021Sdim * notice, this list of conditions and the following disclaimer in the 12317021Sdim * documentation and/or other materials provided with the distribution. 13317021Sdim * 4. Neither the name of the University nor the names of its contributors 14317021Sdim * may be used to endorse or promote products derived from this software 15317021Sdim * without specific prior written permission. 16317021Sdim * 17317021Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18317021Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19317021Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20317021Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21317021Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22317021Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23317021Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24317021Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25317021Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26317021Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27317021Sdim * SUCH DAMAGE. 28317021Sdim * 29317021Sdim * $FreeBSD: head/sys/netinet/ip_divert.c 134793 2004-09-05 02:34:12Z jmg $ 30317021Sdim */ 31317021Sdim 32317021Sdim#include "opt_inet.h" 33317021Sdim#include "opt_ipfw.h" 34317021Sdim#include "opt_ipdivert.h" 35317021Sdim#include "opt_ipsec.h" 36317021Sdim#include "opt_mac.h" 37317021Sdim 38317021Sdim#ifndef INET 39317021Sdim#error "IPDIVERT requires INET." 40317021Sdim#endif 41317021Sdim 42317021Sdim#include <sys/param.h> 43317021Sdim#include <sys/kernel.h> 44317021Sdim#include <sys/lock.h> 45317021Sdim#include <sys/malloc.h> 46317021Sdim#include <sys/mac.h> 47317021Sdim#include <sys/mbuf.h> 48317021Sdim#include <sys/proc.h> 49317021Sdim#include <sys/protosw.h> 50317021Sdim#include <sys/signalvar.h> 51317021Sdim#include <sys/socket.h> 52317021Sdim#include <sys/socketvar.h> 53317021Sdim#include <sys/sx.h> 54317021Sdim#include <sys/sysctl.h> 55317021Sdim#include <sys/systm.h> 56317021Sdim 57317021Sdim#include <vm/uma.h> 58317021Sdim 59317021Sdim#include <net/if.h> 60317021Sdim#include <net/route.h> 61317021Sdim 62317021Sdim#include <netinet/in.h> 63317021Sdim#include <netinet/in_pcb.h> 64317021Sdim#include <netinet/in_systm.h> 65317021Sdim#include <netinet/in_var.h> 66317021Sdim#include <netinet/ip.h> 67317021Sdim#include <netinet/ip_divert.h> 68317021Sdim#include <netinet/ip_var.h> 69317021Sdim 70317021Sdim/* 71317021Sdim * Divert sockets 72317021Sdim */ 73317021Sdim 74317021Sdim/* 75317021Sdim * Allocate enough space to hold a full IP packet 76317021Sdim */ 77317021Sdim#define DIVSNDQ (65536 + 100) 78317021Sdim#define DIVRCVQ (65536 + 100) 79317021Sdim 80317021Sdim/* 81317021Sdim * Divert sockets work in conjunction with ipfw, see the divert(4) 82317021Sdim * manpage for features. 83317021Sdim * Internally, packets selected by ipfw in ip_input() or ip_output(), 84317021Sdim * and never diverted before, are passed to the input queue of the 85317021Sdim * divert socket with a given 'divert_port' number (as specified in 86317021Sdim * the matching ipfw rule), and they are tagged with a 16 bit cookie 87317021Sdim * (representing the rule number of the matching ipfw rule), which 88317021Sdim * is passed to process reading from the socket. 89317021Sdim * 90317021Sdim * Packets written to the divert socket are again tagged with a cookie 91317021Sdim * (usually the same as above) and a destination address. 92317021Sdim * If the destination address is INADDR_ANY then the packet is 93317021Sdim * treated as outgoing and sent to ip_output(), otherwise it is 94317021Sdim * treated as incoming and sent to ip_input(). 95317021Sdim * In both cases, the packet is tagged with the cookie. 96317021Sdim * 97317021Sdim * On reinjection, processing in ip_input() and ip_output() 98317021Sdim * will be exactly the same as for the original packet, except that 99317021Sdim * ipfw processing will start at the rule number after the one 100317021Sdim * written in the cookie (so, tagging a packet with a cookie of 0 101317021Sdim * will cause it to be effectively considered as a standard packet). 102317021Sdim */ 103317021Sdim 104317021Sdim/* Internal variables */ 105317021Sdimstatic struct inpcbhead divcb; 106317021Sdimstatic struct inpcbinfo divcbinfo; 107317021Sdim 108317021Sdimstatic u_long div_sendspace = DIVSNDQ; /* XXX sysctl ? */ 109317021Sdimstatic u_long div_recvspace = DIVRCVQ; /* XXX sysctl ? */ 110317021Sdim 111317021Sdim/* 112317021Sdim * Initialize divert connection block queue. 113317021Sdim */ 114317021Sdimvoid 115317021Sdimdiv_init(void) 116317021Sdim{ 117317021Sdim INP_INFO_LOCK_INIT(&divcbinfo, "div"); 118317021Sdim LIST_INIT(&divcb); 119317021Sdim divcbinfo.listhead = &divcb; 120317021Sdim /* 121317021Sdim * XXX We don't use the hash list for divert IP, but it's easier 122317021Sdim * to allocate a one entry hash list than it is to check all 123317021Sdim * over the place for hashbase == NULL. 124317021Sdim */ 125317021Sdim divcbinfo.hashbase = hashinit(1, M_PCB, &divcbinfo.hashmask); 126317021Sdim divcbinfo.porthashbase = hashinit(1, M_PCB, &divcbinfo.porthashmask); 127317021Sdim divcbinfo.ipi_zone = uma_zcreate("divcb", sizeof(struct inpcb), 128317021Sdim NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 129317021Sdim uma_zone_set_max(divcbinfo.ipi_zone, maxsockets); 130317021Sdim} 131317021Sdim 132317021Sdim/* 133317021Sdim * IPPROTO_DIVERT is not in the real IP protocol number space; this 134317021Sdim * function should never be called. Just in case, drop any packets. 135317021Sdim */ 136317021Sdimvoid 137317021Sdimdiv_input(struct mbuf *m, int off) 138317021Sdim{ 139317021Sdim ipstat.ips_noproto++; 140317021Sdim m_freem(m); 141317021Sdim} 142317021Sdim 143317021Sdim/* 144317021Sdim * Divert a packet by passing it up to the divert socket at port 'port'. 145317021Sdim * 146317021Sdim * Setup generic address and protocol structures for div_input routine, 147317021Sdim * then pass them along with mbuf chain. 148318384Sdim */ 149318384Sdimvoid 150318384Sdimdivert_packet(struct mbuf *m, int incoming) 151318384Sdim{ 152317021Sdim struct ip *ip; 153318384Sdim struct inpcb *inp; 154318384Sdim struct socket *sa; 155318384Sdim u_int16_t nport; 156317021Sdim struct sockaddr_in divsrc; 157318384Sdim struct m_tag *mtag; 158318384Sdim 159318384Sdim mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL); 160318384Sdim if (mtag == NULL) { 161318384Sdim printf("%s: no divert tag\n", __func__); 162318384Sdim m_freem(m); 163318384Sdim return; 164318384Sdim } 165318384Sdim /* Assure header */ 166318384Sdim if (m->m_len < sizeof(struct ip) && 167318384Sdim (m = m_pullup(m, sizeof(struct ip))) == 0) 168318384Sdim return; 169318384Sdim ip = mtod(m, struct ip *); 170318384Sdim 171318384Sdim /* Delayed checksums are currently not compatible with divert. */ 172317021Sdim if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 173318384Sdim ip->ip_len = ntohs(ip->ip_len); 174318384Sdim in_delayed_cksum(m); 175318384Sdim m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 176318384Sdim ip->ip_len = htons(ip->ip_len); 177318384Sdim } 178318384Sdim 179318384Sdim /* 180318384Sdim * Record receive interface address, if any. 181318384Sdim * But only for incoming packets. 182318384Sdim */ 183318384Sdim bzero(&divsrc, sizeof(divsrc)); 184318384Sdim divsrc.sin_len = sizeof(divsrc); 185318384Sdim divsrc.sin_family = AF_INET; 186318384Sdim divsrc.sin_port = divert_cookie(mtag); /* record matching rule */ 187318384Sdim if (incoming) { 188317021Sdim struct ifaddr *ifa; 189318384Sdim 190318384Sdim /* Sanity check */ 191317021Sdim M_ASSERTPKTHDR(m); 192317021Sdim 193317021Sdim /* Find IP address for receive interface */ 194318384Sdim TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 195318384Sdim if (ifa->ifa_addr == NULL) 196318384Sdim continue; 197318384Sdim if (ifa->ifa_addr->sa_family != AF_INET) 198318384Sdim continue; 199318384Sdim divsrc.sin_addr = 200318384Sdim ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr; 201318384Sdim break; 202318384Sdim } 203318384Sdim } 204318384Sdim /* 205318384Sdim * Record the incoming interface name whenever we have one. 206318384Sdim */ 207318384Sdim if (m->m_pkthdr.rcvif) { 208318384Sdim /* 209318384Sdim * Hide the actual interface name in there in the 210318384Sdim * sin_zero array. XXX This needs to be moved to a 211318384Sdim * different sockaddr type for divert, e.g. 212318384Sdim * sockaddr_div with multiple fields like 213318384Sdim * sockaddr_dl. Presently we have only 7 bytes 214318384Sdim * but that will do for now as most interfaces 215318384Sdim * are 4 or less + 2 or less bytes for unit. 216318384Sdim * There is probably a faster way of doing this, 217318384Sdim * possibly taking it from the sockaddr_dl on the iface. 218318384Sdim * This solves the problem of a P2P link and a LAN interface 219318384Sdim * having the same address, which can result in the wrong 220318384Sdim * interface being assigned to the packet when fed back 221318384Sdim * into the divert socket. Theoretically if the daemon saves 222318384Sdim * and re-uses the sockaddr_in as suggested in the man pages, 223318384Sdim * this iface name will come along for the ride. 224318384Sdim * (see div_output for the other half of this.) 225318384Sdim */ 226318384Sdim strlcpy(divsrc.sin_zero, m->m_pkthdr.rcvif->if_xname, 227318384Sdim sizeof(divsrc.sin_zero)); 228317021Sdim } 229318384Sdim 230317021Sdim /* Put packet on socket queue, if any */ 231318384Sdim sa = NULL; 232318384Sdim nport = htons((u_int16_t)divert_info(mtag)); 233318384Sdim INP_INFO_RLOCK(&divcbinfo); 234317021Sdim LIST_FOREACH(inp, &divcb, inp_list) { 235317021Sdim INP_LOCK(inp); 236 /* XXX why does only one socket match? */ 237 if (inp->inp_lport == nport) { 238 sa = inp->inp_socket; 239 SOCKBUF_LOCK(&sa->so_rcv); 240 if (sbappendaddr_locked(&sa->so_rcv, 241 (struct sockaddr *)&divsrc, m, 242 (struct mbuf *)0) == 0) { 243 SOCKBUF_UNLOCK(&sa->so_rcv); 244 sa = NULL; /* force mbuf reclaim below */ 245 } else 246 sorwakeup_locked(sa); 247 INP_UNLOCK(inp); 248 break; 249 } 250 INP_UNLOCK(inp); 251 } 252 INP_INFO_RUNLOCK(&divcbinfo); 253 if (sa == NULL) { 254 m_freem(m); 255 ipstat.ips_noproto++; 256 ipstat.ips_delivered--; 257 } 258} 259 260/* 261 * Deliver packet back into the IP processing machinery. 262 * 263 * If no address specified, or address is 0.0.0.0, send to ip_output(); 264 * otherwise, send to ip_input() and mark as having been received on 265 * the interface with that address. 266 */ 267static int 268div_output(struct socket *so, struct mbuf *m, 269 struct sockaddr_in *sin, struct mbuf *control) 270{ 271 int error = 0; 272 273 KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null")); 274 275 if (control) 276 m_freem(control); /* XXX */ 277 278 /* Loopback avoidance and state recovery */ 279 if (sin) { 280 struct m_tag *mtag; 281 struct divert_tag *dt; 282 int i; 283 284 mtag = m_tag_get(PACKET_TAG_DIVERT, 285 sizeof(struct divert_tag), M_NOWAIT); 286 if (mtag == NULL) { 287 error = ENOBUFS; 288 goto cantsend; 289 } 290 dt = (struct divert_tag *)(mtag+1); 291 dt->info = 0; 292 dt->cookie = sin->sin_port; 293 m_tag_prepend(m, mtag); 294 295 /* 296 * Find receive interface with the given name, stuffed 297 * (if it exists) in the sin_zero[] field. 298 * The name is user supplied data so don't trust its size 299 * or that it is zero terminated. 300 */ 301 for (i = 0; i < sizeof(sin->sin_zero) && sin->sin_zero[i]; i++) 302 ; 303 if ( i > 0 && i < sizeof(sin->sin_zero)) 304 m->m_pkthdr.rcvif = ifunit(sin->sin_zero); 305 } 306 307 /* Reinject packet into the system as incoming or outgoing */ 308 if (!sin || sin->sin_addr.s_addr == 0) { 309 struct ip *const ip = mtod(m, struct ip *); 310 struct inpcb *inp; 311 312 INP_INFO_WLOCK(&divcbinfo); 313 inp = sotoinpcb(so); 314 INP_LOCK(inp); 315 /* 316 * Don't allow both user specified and setsockopt options, 317 * and don't allow packet length sizes that will crash 318 */ 319 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) || 320 ((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) { 321 error = EINVAL; 322 m_freem(m); 323 } else { 324 /* Convert fields to host order for ip_output() */ 325 ip->ip_len = ntohs(ip->ip_len); 326 ip->ip_off = ntohs(ip->ip_off); 327 328 /* Send packet to output processing */ 329 ipstat.ips_rawout++; /* XXX */ 330 331#ifdef MAC 332 mac_create_mbuf_from_inpcb(inp, m); 333#endif 334 error = ip_output(m, 335 inp->inp_options, NULL, 336 ((so->so_options & SO_DONTROUTE) ? 337 IP_ROUTETOIF : 0) | 338 IP_ALLOWBROADCAST | IP_RAWOUTPUT, 339 inp->inp_moptions, NULL); 340 } 341 INP_UNLOCK(inp); 342 INP_INFO_WUNLOCK(&divcbinfo); 343 } else { 344 if (m->m_pkthdr.rcvif == NULL) { 345 /* 346 * No luck with the name, check by IP address. 347 * Clear the port and the ifname to make sure 348 * there are no distractions for ifa_ifwithaddr. 349 */ 350 struct ifaddr *ifa; 351 352 bzero(sin->sin_zero, sizeof(sin->sin_zero)); 353 sin->sin_port = 0; 354 ifa = ifa_ifwithaddr((struct sockaddr *) sin); 355 if (ifa == NULL) { 356 error = EADDRNOTAVAIL; 357 goto cantsend; 358 } 359 m->m_pkthdr.rcvif = ifa->ifa_ifp; 360 } 361#ifdef MAC 362 SOCK_LOCK(so); 363 mac_create_mbuf_from_socket(so, m); 364 SOCK_UNLOCK(so); 365#endif 366 /* Send packet to input processing */ 367 ip_input(m); 368 } 369 370 return error; 371 372cantsend: 373 m_freem(m); 374 return error; 375} 376 377static int 378div_attach(struct socket *so, int proto, struct thread *td) 379{ 380 struct inpcb *inp; 381 int error; 382 383 INP_INFO_WLOCK(&divcbinfo); 384 inp = sotoinpcb(so); 385 if (inp != 0) { 386 INP_INFO_WUNLOCK(&divcbinfo); 387 return EINVAL; 388 } 389 if (td && (error = suser(td)) != 0) { 390 INP_INFO_WUNLOCK(&divcbinfo); 391 return error; 392 } 393 error = soreserve(so, div_sendspace, div_recvspace); 394 if (error) { 395 INP_INFO_WUNLOCK(&divcbinfo); 396 return error; 397 } 398 error = in_pcballoc(so, &divcbinfo, "divinp"); 399 if (error) { 400 INP_INFO_WUNLOCK(&divcbinfo); 401 return error; 402 } 403 inp = (struct inpcb *)so->so_pcb; 404 INP_LOCK(inp); 405 INP_INFO_WUNLOCK(&divcbinfo); 406 inp->inp_ip_p = proto; 407 inp->inp_vflag |= INP_IPV4; 408 inp->inp_flags |= INP_HDRINCL; 409 /* The socket is always "connected" because 410 we always know "where" to send the packet */ 411 INP_UNLOCK(inp); 412 SOCK_LOCK(so); 413 so->so_state |= SS_ISCONNECTED; 414 SOCK_UNLOCK(so); 415 return 0; 416} 417 418static int 419div_detach(struct socket *so) 420{ 421 struct inpcb *inp; 422 423 INP_INFO_WLOCK(&divcbinfo); 424 inp = sotoinpcb(so); 425 if (inp == 0) { 426 INP_INFO_WUNLOCK(&divcbinfo); 427 return EINVAL; 428 } 429 INP_LOCK(inp); 430 in_pcbdetach(inp); 431 INP_INFO_WUNLOCK(&divcbinfo); 432 return 0; 433} 434 435static int 436div_abort(struct socket *so) 437{ 438 struct inpcb *inp; 439 440 INP_INFO_WLOCK(&divcbinfo); 441 inp = sotoinpcb(so); 442 if (inp == 0) { 443 INP_INFO_WUNLOCK(&divcbinfo); 444 return EINVAL; /* ??? possible? panic instead? */ 445 } 446 INP_LOCK(inp); 447 soisdisconnected(so); 448 in_pcbdetach(inp); 449 INP_INFO_WUNLOCK(&divcbinfo); 450 return 0; 451} 452 453static int 454div_disconnect(struct socket *so) 455{ 456 if ((so->so_state & SS_ISCONNECTED) == 0) 457 return ENOTCONN; 458 return div_abort(so); 459} 460 461static int 462div_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 463{ 464 struct inpcb *inp; 465 int error; 466 467 INP_INFO_WLOCK(&divcbinfo); 468 inp = sotoinpcb(so); 469 if (inp == 0) { 470 INP_INFO_WUNLOCK(&divcbinfo); 471 return EINVAL; 472 } 473 /* in_pcbbind assumes that nam is a sockaddr_in 474 * and in_pcbbind requires a valid address. Since divert 475 * sockets don't we need to make sure the address is 476 * filled in properly. 477 * XXX -- divert should not be abusing in_pcbind 478 * and should probably have its own family. 479 */ 480 if (nam->sa_family != AF_INET) 481 error = EAFNOSUPPORT; 482 else { 483 ((struct sockaddr_in *)nam)->sin_addr.s_addr = INADDR_ANY; 484 INP_LOCK(inp); 485 error = in_pcbbind(inp, nam, td->td_ucred); 486 INP_UNLOCK(inp); 487 } 488 INP_INFO_WUNLOCK(&divcbinfo); 489 return error; 490} 491 492static int 493div_shutdown(struct socket *so) 494{ 495 struct inpcb *inp; 496 497 INP_INFO_RLOCK(&divcbinfo); 498 inp = sotoinpcb(so); 499 if (inp == 0) { 500 INP_INFO_RUNLOCK(&divcbinfo); 501 return EINVAL; 502 } 503 INP_LOCK(inp); 504 INP_INFO_RUNLOCK(&divcbinfo); 505 socantsendmore(so); 506 INP_UNLOCK(inp); 507 return 0; 508} 509 510static int 511div_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 512 struct mbuf *control, struct thread *td) 513{ 514 /* Packet must have a header (but that's about it) */ 515 if (m->m_len < sizeof (struct ip) && 516 (m = m_pullup(m, sizeof (struct ip))) == 0) { 517 ipstat.ips_toosmall++; 518 m_freem(m); 519 return EINVAL; 520 } 521 522 /* Send packet */ 523 return div_output(so, m, (struct sockaddr_in *)nam, control); 524} 525 526void 527div_ctlinput(int cmd, struct sockaddr *sa, void *vip) 528{ 529 struct in_addr faddr; 530 531 faddr = ((struct sockaddr_in *)sa)->sin_addr; 532 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 533 return; 534 if (PRC_IS_REDIRECT(cmd)) 535 return; 536} 537 538static int 539div_pcblist(SYSCTL_HANDLER_ARGS) 540{ 541 int error, i, n; 542 struct inpcb *inp, **inp_list; 543 inp_gen_t gencnt; 544 struct xinpgen xig; 545 546 /* 547 * The process of preparing the TCB list is too time-consuming and 548 * resource-intensive to repeat twice on every request. 549 */ 550 if (req->oldptr == 0) { 551 n = divcbinfo.ipi_count; 552 req->oldidx = 2 * (sizeof xig) 553 + (n + n/8) * sizeof(struct xinpcb); 554 return 0; 555 } 556 557 if (req->newptr != 0) 558 return EPERM; 559 560 /* 561 * OK, now we're committed to doing something. 562 */ 563 INP_INFO_RLOCK(&divcbinfo); 564 gencnt = divcbinfo.ipi_gencnt; 565 n = divcbinfo.ipi_count; 566 INP_INFO_RUNLOCK(&divcbinfo); 567 568 error = sysctl_wire_old_buffer(req, 569 2 * sizeof(xig) + n*sizeof(struct xinpcb)); 570 if (error != 0) 571 return (error); 572 573 xig.xig_len = sizeof xig; 574 xig.xig_count = n; 575 xig.xig_gen = gencnt; 576 xig.xig_sogen = so_gencnt; 577 error = SYSCTL_OUT(req, &xig, sizeof xig); 578 if (error) 579 return error; 580 581 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 582 if (inp_list == 0) 583 return ENOMEM; 584 585 INP_INFO_RLOCK(&divcbinfo); 586 for (inp = LIST_FIRST(divcbinfo.listhead), i = 0; inp && i < n; 587 inp = LIST_NEXT(inp, inp_list)) { 588 INP_LOCK(inp); 589 if (inp->inp_gencnt <= gencnt && 590 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) 591 inp_list[i++] = inp; 592 INP_UNLOCK(inp); 593 } 594 INP_INFO_RUNLOCK(&divcbinfo); 595 n = i; 596 597 error = 0; 598 for (i = 0; i < n; i++) { 599 inp = inp_list[i]; 600 if (inp->inp_gencnt <= gencnt) { 601 struct xinpcb xi; 602 xi.xi_len = sizeof xi; 603 /* XXX should avoid extra copy */ 604 bcopy(inp, &xi.xi_inp, sizeof *inp); 605 if (inp->inp_socket) 606 sotoxsocket(inp->inp_socket, &xi.xi_socket); 607 error = SYSCTL_OUT(req, &xi, sizeof xi); 608 } 609 } 610 if (!error) { 611 /* 612 * Give the user an updated idea of our state. 613 * If the generation differs from what we told 614 * her before, she knows that something happened 615 * while we were processing this request, and it 616 * might be necessary to retry. 617 */ 618 INP_INFO_RLOCK(&divcbinfo); 619 xig.xig_gen = divcbinfo.ipi_gencnt; 620 xig.xig_sogen = so_gencnt; 621 xig.xig_count = divcbinfo.ipi_count; 622 INP_INFO_RUNLOCK(&divcbinfo); 623 error = SYSCTL_OUT(req, &xig, sizeof xig); 624 } 625 free(inp_list, M_TEMP); 626 return error; 627} 628 629/* 630 * This is the wrapper function for in_setsockaddr. We just pass down 631 * the pcbinfo for in_setpeeraddr to lock. 632 */ 633static int 634div_sockaddr(struct socket *so, struct sockaddr **nam) 635{ 636 return (in_setsockaddr(so, nam, &divcbinfo)); 637} 638 639/* 640 * This is the wrapper function for in_setpeeraddr. We just pass down 641 * the pcbinfo for in_setpeeraddr to lock. 642 */ 643static int 644div_peeraddr(struct socket *so, struct sockaddr **nam) 645{ 646 return (in_setpeeraddr(so, nam, &divcbinfo)); 647} 648 649 650SYSCTL_DECL(_net_inet_divert); 651SYSCTL_PROC(_net_inet_divert, OID_AUTO, pcblist, CTLFLAG_RD, 0, 0, 652 div_pcblist, "S,xinpcb", "List of active divert sockets"); 653 654struct pr_usrreqs div_usrreqs = { 655 div_abort, pru_accept_notsupp, div_attach, div_bind, 656 pru_connect_notsupp, pru_connect2_notsupp, in_control, div_detach, 657 div_disconnect, pru_listen_notsupp, div_peeraddr, pru_rcvd_notsupp, 658 pru_rcvoob_notsupp, div_send, pru_sense_null, div_shutdown, 659 div_sockaddr, sosend, soreceive, sopoll, in_pcbsosetlabel 660}; 661