raw_ip.c revision 119489
1/* 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95 34 * $FreeBSD: head/sys/netinet/raw_ip.c 119489 2003-08-26 14:11:48Z rwatson $ 35 */ 36 37#include "opt_inet6.h" 38#include "opt_ipsec.h" 39#include "opt_mac.h" 40#include "opt_random_ip_id.h" 41 42#include <sys/param.h> 43#include <sys/kernel.h> 44#include <sys/lock.h> 45#include <sys/mac.h> 46#include <sys/malloc.h> 47#include <sys/mbuf.h> 48#include <sys/proc.h> 49#include <sys/protosw.h> 50#include <sys/signalvar.h> 51#include <sys/socket.h> 52#include <sys/socketvar.h> 53#include <sys/sx.h> 54#include <sys/sysctl.h> 55#include <sys/systm.h> 56 57#include <vm/uma.h> 58 59#include <net/if.h> 60#include <net/route.h> 61 62#include <netinet/in.h> 63#include <netinet/in_systm.h> 64#include <netinet/in_pcb.h> 65#include <netinet/in_var.h> 66#include <netinet/ip.h> 67#include <netinet/ip_var.h> 68#include <netinet/ip_mroute.h> 69 70#include <netinet/ip_fw.h> 71#include <netinet/ip_dummynet.h> 72 73#ifdef FAST_IPSEC 74#include <netipsec/ipsec.h> 75#endif /*FAST_IPSEC*/ 76 77#ifdef IPSEC 78#include <netinet6/ipsec.h> 79#endif /*IPSEC*/ 80 81struct inpcbhead ripcb; 82struct inpcbinfo ripcbinfo; 83 84/* control hooks for ipfw and dummynet */ 85ip_fw_ctl_t *ip_fw_ctl_ptr; 86ip_dn_ctl_t *ip_dn_ctl_ptr; 87 88/* 89 * hooks for multicast routing. They all default to NULL, 90 * so leave them not initialized and rely on BSS being set to 0. 91 */ 92 93/* The socket used to communicate with the multicast routing daemon. */ 94struct socket *ip_mrouter; 95 96/* The various mrouter and rsvp functions */ 97int (*ip_mrouter_set)(struct socket *, struct sockopt *); 98int (*ip_mrouter_get)(struct socket *, struct sockopt *); 99int (*ip_mrouter_done)(void); 100int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *, 101 struct ip_moptions *); 102int (*mrt_ioctl)(int, caddr_t); 103int (*legal_vif_num)(int); 104u_long (*ip_mcast_src)(int); 105 106void (*rsvp_input_p)(struct mbuf *m, int off); 107int (*ip_rsvp_vif)(struct socket *, struct sockopt *); 108void (*ip_rsvp_force_done)(struct socket *); 109 110/* 111 * Nominal space allocated to a raw ip socket. 112 */ 113#define RIPSNDQ 8192 114#define RIPRCVQ 8192 115 116/* 117 * Raw interface to IP protocol. 118 */ 119 120/* 121 * Initialize raw connection block q. 122 */ 123void 124rip_init() 125{ 126 INP_INFO_LOCK_INIT(&ripcbinfo, "rip"); 127 LIST_INIT(&ripcb); 128 ripcbinfo.listhead = &ripcb; 129 /* 130 * XXX We don't use the hash list for raw IP, but it's easier 131 * to allocate a one entry hash list than it is to check all 132 * over the place for hashbase == NULL. 133 */ 134 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask); 135 ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask); 136 ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb), 137 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 138 uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets); 139} 140 141static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET }; 142/* 143 * Setup generic address and protocol structures 144 * for raw_input routine, then pass them along with 145 * mbuf chain. 146 */ 147void 148rip_input(m, off) 149 struct mbuf *m; 150 int off; 151{ 152 register struct ip *ip = mtod(m, struct ip *); 153 register struct inpcb *inp; 154 struct inpcb *last = 0; 155 struct mbuf *opts = 0; 156 int proto = ip->ip_p; 157 158 ripsrc.sin_addr = ip->ip_src; 159 LIST_FOREACH(inp, &ripcb, inp_list) { 160#ifdef INET6 161 if ((inp->inp_vflag & INP_IPV4) == 0) 162 continue; 163#endif 164 if (inp->inp_ip_p && inp->inp_ip_p != proto) 165 continue; 166 if (inp->inp_laddr.s_addr && 167 inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 168 continue; 169 if (inp->inp_faddr.s_addr && 170 inp->inp_faddr.s_addr != ip->ip_src.s_addr) 171 continue; 172 if (last) { 173 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 174 int policyfail = 0; 175 176 if (n != NULL) { 177#ifdef IPSEC 178 /* check AH/ESP integrity. */ 179 if (ipsec4_in_reject_so(n, last->inp_socket)) { 180 policyfail = 1; 181 ipsecstat.in_polvio++; 182 /* do not inject data to pcb */ 183 } 184#endif /*IPSEC*/ 185#ifdef FAST_IPSEC 186 /* check AH/ESP integrity. */ 187 if (ipsec4_in_reject(n, last)) { 188 policyfail = 1; 189 /* do not inject data to pcb */ 190 } 191#endif /*FAST_IPSEC*/ 192#ifdef MAC 193 if (policyfail == 0 && 194 mac_check_socket_deliver(last->inp_socket, 195 n) != 0) 196 policyfail = 1; 197#endif 198 } 199 if (policyfail) 200 m_freem(n); 201 else if (n) { 202 if (last->inp_flags & INP_CONTROLOPTS || 203 last->inp_socket->so_options & SO_TIMESTAMP) 204 ip_savecontrol(last, &opts, ip, n); 205 if (sbappendaddr(&last->inp_socket->so_rcv, 206 (struct sockaddr *)&ripsrc, n, 207 opts) == 0) { 208 /* should notify about lost packet */ 209 m_freem(n); 210 if (opts) 211 m_freem(opts); 212 } else 213 sorwakeup(last->inp_socket); 214 opts = 0; 215 } 216 } 217 last = inp; 218 } 219 if (last) { 220#ifdef IPSEC 221 /* check AH/ESP integrity. */ 222 if (ipsec4_in_reject_so(m, last->inp_socket)) { 223 m_freem(m); 224 ipsecstat.in_polvio++; 225 ipstat.ips_delivered--; 226 /* do not inject data to pcb */ 227 return; 228 } 229#endif /*IPSEC*/ 230#ifdef FAST_IPSEC 231 /* check AH/ESP integrity. */ 232 if (ipsec4_in_reject(m, last)) { 233 m_freem(m); 234 ipstat.ips_delivered--; 235 /* do not inject data to pcb */ 236 return; 237 } 238#endif /*FAST_IPSEC*/ 239#ifdef MAC 240 if (mac_check_socket_deliver(last->inp_socket, m) != 0) { 241 m_freem(m); 242 ipstat.ips_delivered--; 243 return; 244 } 245#endif 246 if (last->inp_flags & INP_CONTROLOPTS || 247 last->inp_socket->so_options & SO_TIMESTAMP) 248 ip_savecontrol(last, &opts, ip, m); 249 if (sbappendaddr(&last->inp_socket->so_rcv, 250 (struct sockaddr *)&ripsrc, m, opts) == 0) { 251 m_freem(m); 252 if (opts) 253 m_freem(opts); 254 } else 255 sorwakeup(last->inp_socket); 256 } else { 257 m_freem(m); 258 ipstat.ips_noproto++; 259 ipstat.ips_delivered--; 260 } 261} 262 263/* 264 * Generate IP header and pass packet to ip_output. 265 * Tack on options user may have setup with control call. 266 */ 267int 268rip_output(m, so, dst) 269 struct mbuf *m; 270 struct socket *so; 271 u_long dst; 272{ 273 register struct ip *ip; 274 register struct inpcb *inp = sotoinpcb(so); 275 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST; 276 277#ifdef MAC 278 mac_create_mbuf_from_socket(so, m); 279#endif 280 281 /* 282 * If the user handed us a complete IP packet, use it. 283 * Otherwise, allocate an mbuf for a header and fill it in. 284 */ 285 if ((inp->inp_flags & INP_HDRINCL) == 0) { 286 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) { 287 m_freem(m); 288 return(EMSGSIZE); 289 } 290 M_PREPEND(m, sizeof(struct ip), M_TRYWAIT); 291 if (m == NULL) 292 return(ENOBUFS); 293 ip = mtod(m, struct ip *); 294 ip->ip_tos = inp->inp_ip_tos; 295 ip->ip_off = 0; 296 ip->ip_p = inp->inp_ip_p; 297 ip->ip_len = m->m_pkthdr.len; 298 ip->ip_src = inp->inp_laddr; 299 ip->ip_dst.s_addr = dst; 300 ip->ip_ttl = inp->inp_ip_ttl; 301 } else { 302 if (m->m_pkthdr.len > IP_MAXPACKET) { 303 m_freem(m); 304 return(EMSGSIZE); 305 } 306 ip = mtod(m, struct ip *); 307 /* don't allow both user specified and setsockopt options, 308 and don't allow packet length sizes that will crash */ 309 if (((ip->ip_hl != (sizeof (*ip) >> 2)) 310 && inp->inp_options) 311 || (ip->ip_len > m->m_pkthdr.len) 312 || (ip->ip_len < (ip->ip_hl << 2))) { 313 m_freem(m); 314 return EINVAL; 315 } 316 if (ip->ip_id == 0) 317#ifdef RANDOM_IP_ID 318 ip->ip_id = ip_randomid(); 319#else 320 ip->ip_id = htons(ip_id++); 321#endif 322 /* XXX prevent ip_output from overwriting header fields */ 323 flags |= IP_RAWOUTPUT; 324 ipstat.ips_rawout++; 325 } 326 327 if (inp->inp_flags & INP_ONESBCAST) 328 flags |= IP_SENDONES; 329 330 return (ip_output(m, inp->inp_options, &inp->inp_route, flags, 331 inp->inp_moptions, inp)); 332} 333 334/* 335 * Raw IP socket option processing. 336 * 337 * Note that access to all of the IP administrative functions here is 338 * implicitly protected by suser() as gaining access to a raw socket 339 * requires either that the thread pass a suser() check, or that it be 340 * passed a raw socket by another thread that has passed a suser() check. 341 * If FreeBSD moves to a more fine-grained access control mechanism, 342 * additional checks will need to be placed here if the raw IP attachment 343 * check is not equivilent the the check required for these 344 * administrative operations; in some cases, these checks are already 345 * present. 346 */ 347int 348rip_ctloutput(so, sopt) 349 struct socket *so; 350 struct sockopt *sopt; 351{ 352 struct inpcb *inp = sotoinpcb(so); 353 int error, optval; 354 355 if (sopt->sopt_level != IPPROTO_IP) 356 return (EINVAL); 357 358 error = 0; 359 360 switch (sopt->sopt_dir) { 361 case SOPT_GET: 362 switch (sopt->sopt_name) { 363 case IP_HDRINCL: 364 optval = inp->inp_flags & INP_HDRINCL; 365 error = sooptcopyout(sopt, &optval, sizeof optval); 366 break; 367 368 case IP_FW_ADD: /* ADD actually returns the body... */ 369 case IP_FW_GET: 370 if (IPFW_LOADED) 371 error = ip_fw_ctl_ptr(sopt); 372 else 373 error = ENOPROTOOPT; 374 break; 375 376 case IP_DUMMYNET_GET: 377 if (DUMMYNET_LOADED) 378 error = ip_dn_ctl_ptr(sopt); 379 else 380 error = ENOPROTOOPT; 381 break ; 382 383 case MRT_INIT: 384 case MRT_DONE: 385 case MRT_ADD_VIF: 386 case MRT_DEL_VIF: 387 case MRT_ADD_MFC: 388 case MRT_DEL_MFC: 389 case MRT_VERSION: 390 case MRT_ASSERT: 391 case MRT_API_SUPPORT: 392 case MRT_API_CONFIG: 393 case MRT_ADD_BW_UPCALL: 394 case MRT_DEL_BW_UPCALL: 395 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) : 396 EOPNOTSUPP; 397 break; 398 399 default: 400 error = ip_ctloutput(so, sopt); 401 break; 402 } 403 break; 404 405 case SOPT_SET: 406 switch (sopt->sopt_name) { 407 case IP_HDRINCL: 408 error = sooptcopyin(sopt, &optval, sizeof optval, 409 sizeof optval); 410 if (error) 411 break; 412 if (optval) 413 inp->inp_flags |= INP_HDRINCL; 414 else 415 inp->inp_flags &= ~INP_HDRINCL; 416 break; 417 418 case IP_FW_ADD: 419 case IP_FW_DEL: 420 case IP_FW_FLUSH: 421 case IP_FW_ZERO: 422 case IP_FW_RESETLOG: 423 if (IPFW_LOADED) 424 error = ip_fw_ctl_ptr(sopt); 425 else 426 error = ENOPROTOOPT; 427 break; 428 429 case IP_DUMMYNET_CONFIGURE: 430 case IP_DUMMYNET_DEL: 431 case IP_DUMMYNET_FLUSH: 432 if (DUMMYNET_LOADED) 433 error = ip_dn_ctl_ptr(sopt); 434 else 435 error = ENOPROTOOPT ; 436 break ; 437 438 case IP_RSVP_ON: 439 error = ip_rsvp_init(so); 440 break; 441 442 case IP_RSVP_OFF: 443 error = ip_rsvp_done(); 444 break; 445 446 case IP_RSVP_VIF_ON: 447 case IP_RSVP_VIF_OFF: 448 error = ip_rsvp_vif ? 449 ip_rsvp_vif(so, sopt) : EINVAL; 450 break; 451 452 case MRT_INIT: 453 case MRT_DONE: 454 case MRT_ADD_VIF: 455 case MRT_DEL_VIF: 456 case MRT_ADD_MFC: 457 case MRT_DEL_MFC: 458 case MRT_VERSION: 459 case MRT_ASSERT: 460 case MRT_API_SUPPORT: 461 case MRT_API_CONFIG: 462 case MRT_ADD_BW_UPCALL: 463 case MRT_DEL_BW_UPCALL: 464 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) : 465 EOPNOTSUPP; 466 break; 467 468 default: 469 error = ip_ctloutput(so, sopt); 470 break; 471 } 472 break; 473 } 474 475 return (error); 476} 477 478/* 479 * This function exists solely to receive the PRC_IFDOWN messages which 480 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, 481 * and calls in_ifadown() to remove all routes corresponding to that address. 482 * It also receives the PRC_IFUP messages from if_up() and reinstalls the 483 * interface routes. 484 */ 485void 486rip_ctlinput(cmd, sa, vip) 487 int cmd; 488 struct sockaddr *sa; 489 void *vip; 490{ 491 struct in_ifaddr *ia; 492 struct ifnet *ifp; 493 int err; 494 int flags; 495 496 switch (cmd) { 497 case PRC_IFDOWN: 498 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 499 if (ia->ia_ifa.ifa_addr == sa 500 && (ia->ia_flags & IFA_ROUTE)) { 501 /* 502 * in_ifscrub kills the interface route. 503 */ 504 in_ifscrub(ia->ia_ifp, ia); 505 /* 506 * in_ifadown gets rid of all the rest of 507 * the routes. This is not quite the right 508 * thing to do, but at least if we are running 509 * a routing process they will come back. 510 */ 511 in_ifadown(&ia->ia_ifa, 0); 512 break; 513 } 514 } 515 break; 516 517 case PRC_IFUP: 518 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 519 if (ia->ia_ifa.ifa_addr == sa) 520 break; 521 } 522 if (ia == 0 || (ia->ia_flags & IFA_ROUTE)) 523 return; 524 flags = RTF_UP; 525 ifp = ia->ia_ifa.ifa_ifp; 526 527 if ((ifp->if_flags & IFF_LOOPBACK) 528 || (ifp->if_flags & IFF_POINTOPOINT)) 529 flags |= RTF_HOST; 530 531 err = rtinit(&ia->ia_ifa, RTM_ADD, flags); 532 if (err == 0) 533 ia->ia_flags |= IFA_ROUTE; 534 break; 535 } 536} 537 538u_long rip_sendspace = RIPSNDQ; 539u_long rip_recvspace = RIPRCVQ; 540int rip_olddiverterror = 1; 541 542SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, 543 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size"); 544SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, 545 &rip_recvspace, 0, "Maximum incoming raw IP datagram size"); 546SYSCTL_INT(_net_inet_raw, OID_AUTO, olddiverterror, CTLFLAG_RW, 547 &rip_olddiverterror, 0, "Return an error when creating an 'old' DIVERT socket"); 548 549static int 550rip_attach(struct socket *so, int proto, struct thread *td) 551{ 552 struct inpcb *inp; 553 int error, s; 554 555 inp = sotoinpcb(so); 556 if (inp) 557 panic("rip_attach"); 558 if (td && (error = suser(td)) != 0) 559 return error; 560 561 if (proto >= IPPROTO_MAX || proto < 0) 562 return EPROTONOSUPPORT; 563 564 /* To be removed before 5.2 */ 565 if (rip_olddiverterror && proto == IPPROTO_OLD_DIVERT) { 566 printf("Old IPDIVERT program needs to be recompiled, or new IP proto 254 user needs sysctl net.inet.raw.olddiverterror=0\n"); 567 return EPROTONOSUPPORT; 568 } 569 570 error = soreserve(so, rip_sendspace, rip_recvspace); 571 if (error) 572 return error; 573 s = splnet(); 574 error = in_pcballoc(so, &ripcbinfo, td); 575 splx(s); 576 if (error) 577 return error; 578 inp = (struct inpcb *)so->so_pcb; 579 inp->inp_vflag |= INP_IPV4; 580 inp->inp_ip_p = proto; 581 inp->inp_ip_ttl = ip_defttl; 582 return 0; 583} 584 585static int 586rip_detach(struct socket *so) 587{ 588 struct inpcb *inp; 589 590 inp = sotoinpcb(so); 591 if (inp == 0) 592 panic("rip_detach"); 593 if (so == ip_mrouter && ip_mrouter_done) 594 ip_mrouter_done(); 595 if (ip_rsvp_force_done) 596 ip_rsvp_force_done(so); 597 if (so == ip_rsvpd) 598 ip_rsvp_done(); 599 in_pcbdetach(inp); 600 return 0; 601} 602 603static int 604rip_abort(struct socket *so) 605{ 606 soisdisconnected(so); 607 if (so->so_state & SS_NOFDREF) 608 return rip_detach(so); 609 return 0; 610} 611 612static int 613rip_disconnect(struct socket *so) 614{ 615 if ((so->so_state & SS_ISCONNECTED) == 0) 616 return ENOTCONN; 617 return rip_abort(so); 618} 619 620static int 621rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 622{ 623 struct inpcb *inp = sotoinpcb(so); 624 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 625 626 if (nam->sa_len != sizeof(*addr)) 627 return EINVAL; 628 629 if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) && 630 (addr->sin_family != AF_IMPLINK)) || 631 (addr->sin_addr.s_addr && 632 ifa_ifwithaddr((struct sockaddr *)addr) == 0)) 633 return EADDRNOTAVAIL; 634 inp->inp_laddr = addr->sin_addr; 635 return 0; 636} 637 638static int 639rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 640{ 641 struct inpcb *inp = sotoinpcb(so); 642 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 643 644 if (nam->sa_len != sizeof(*addr)) 645 return EINVAL; 646 if (TAILQ_EMPTY(&ifnet)) 647 return EADDRNOTAVAIL; 648 if ((addr->sin_family != AF_INET) && 649 (addr->sin_family != AF_IMPLINK)) 650 return EAFNOSUPPORT; 651 inp->inp_faddr = addr->sin_addr; 652 soisconnected(so); 653 return 0; 654} 655 656static int 657rip_shutdown(struct socket *so) 658{ 659 socantsendmore(so); 660 return 0; 661} 662 663static int 664rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 665 struct mbuf *control, struct thread *td) 666{ 667 struct inpcb *inp = sotoinpcb(so); 668 register u_long dst; 669 670 if (so->so_state & SS_ISCONNECTED) { 671 if (nam) { 672 m_freem(m); 673 return EISCONN; 674 } 675 dst = inp->inp_faddr.s_addr; 676 } else { 677 if (nam == NULL) { 678 m_freem(m); 679 return ENOTCONN; 680 } 681 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr; 682 } 683 return rip_output(m, so, dst); 684} 685 686static int 687rip_pcblist(SYSCTL_HANDLER_ARGS) 688{ 689 int error, i, n, s; 690 struct inpcb *inp, **inp_list; 691 inp_gen_t gencnt; 692 struct xinpgen xig; 693 694 /* 695 * The process of preparing the TCB list is too time-consuming and 696 * resource-intensive to repeat twice on every request. 697 */ 698 if (req->oldptr == 0) { 699 n = ripcbinfo.ipi_count; 700 req->oldidx = 2 * (sizeof xig) 701 + (n + n/8) * sizeof(struct xinpcb); 702 return 0; 703 } 704 705 if (req->newptr != 0) 706 return EPERM; 707 708 /* 709 * OK, now we're committed to doing something. 710 */ 711 s = splnet(); 712 gencnt = ripcbinfo.ipi_gencnt; 713 n = ripcbinfo.ipi_count; 714 splx(s); 715 716 xig.xig_len = sizeof xig; 717 xig.xig_count = n; 718 xig.xig_gen = gencnt; 719 xig.xig_sogen = so_gencnt; 720 error = SYSCTL_OUT(req, &xig, sizeof xig); 721 if (error) 722 return error; 723 724 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 725 if (inp_list == 0) 726 return ENOMEM; 727 728 s = splnet(); 729 for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n; 730 inp = LIST_NEXT(inp, inp_list)) { 731 if (inp->inp_gencnt <= gencnt) { 732 if (cr_canseesocket(req->td->td_ucred, 733 inp->inp_socket)) 734 continue; 735 inp_list[i++] = inp; 736 } 737 } 738 splx(s); 739 n = i; 740 741 error = 0; 742 for (i = 0; i < n; i++) { 743 inp = inp_list[i]; 744 if (inp->inp_gencnt <= gencnt) { 745 struct xinpcb xi; 746 xi.xi_len = sizeof xi; 747 /* XXX should avoid extra copy */ 748 bcopy(inp, &xi.xi_inp, sizeof *inp); 749 if (inp->inp_socket) 750 sotoxsocket(inp->inp_socket, &xi.xi_socket); 751 error = SYSCTL_OUT(req, &xi, sizeof xi); 752 } 753 } 754 if (!error) { 755 /* 756 * Give the user an updated idea of our state. 757 * If the generation differs from what we told 758 * her before, she knows that something happened 759 * while we were processing this request, and it 760 * might be necessary to retry. 761 */ 762 s = splnet(); 763 xig.xig_gen = ripcbinfo.ipi_gencnt; 764 xig.xig_sogen = so_gencnt; 765 xig.xig_count = ripcbinfo.ipi_count; 766 splx(s); 767 error = SYSCTL_OUT(req, &xig, sizeof xig); 768 } 769 free(inp_list, M_TEMP); 770 return error; 771} 772 773/* 774 * This is the wrapper function for in_setsockaddr. We just pass down 775 * the pcbinfo for in_setpeeraddr to lock. 776 */ 777static int 778rip_sockaddr(struct socket *so, struct sockaddr **nam) 779{ 780 return (in_setsockaddr(so, nam, &ripcbinfo)); 781} 782 783/* 784 * This is the wrapper function for in_setpeeraddr. We just pass down 785 * the pcbinfo for in_setpeeraddr to lock. 786 */ 787static int 788rip_peeraddr(struct socket *so, struct sockaddr **nam) 789{ 790 return (in_setpeeraddr(so, nam, &ripcbinfo)); 791} 792 793 794SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0, 795 rip_pcblist, "S,xinpcb", "List of active raw IP sockets"); 796 797struct pr_usrreqs rip_usrreqs = { 798 rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect, 799 pru_connect2_notsupp, in_control, rip_detach, rip_disconnect, 800 pru_listen_notsupp, rip_peeraddr, pru_rcvd_notsupp, 801 pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown, 802 rip_sockaddr, sosend, soreceive, sopoll 803}; 804