raw_ip.c revision 119178
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 119178 2003-08-20 14:46:40Z bms $ 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 ip = mtod(m, struct ip *); 292 ip->ip_tos = inp->inp_ip_tos; 293 ip->ip_off = 0; 294 ip->ip_p = inp->inp_ip_p; 295 ip->ip_len = m->m_pkthdr.len; 296 ip->ip_src = inp->inp_laddr; 297 ip->ip_dst.s_addr = dst; 298 ip->ip_ttl = inp->inp_ip_ttl; 299 } else { 300 if (m->m_pkthdr.len > IP_MAXPACKET) { 301 m_freem(m); 302 return(EMSGSIZE); 303 } 304 ip = mtod(m, struct ip *); 305 /* don't allow both user specified and setsockopt options, 306 and don't allow packet length sizes that will crash */ 307 if (((ip->ip_hl != (sizeof (*ip) >> 2)) 308 && inp->inp_options) 309 || (ip->ip_len > m->m_pkthdr.len) 310 || (ip->ip_len < (ip->ip_hl << 2))) { 311 m_freem(m); 312 return EINVAL; 313 } 314 if (ip->ip_id == 0) 315#ifdef RANDOM_IP_ID 316 ip->ip_id = ip_randomid(); 317#else 318 ip->ip_id = htons(ip_id++); 319#endif 320 /* XXX prevent ip_output from overwriting header fields */ 321 flags |= IP_RAWOUTPUT; 322 ipstat.ips_rawout++; 323 } 324 325 if (inp->inp_flags & INP_ONESBCAST) 326 flags |= IP_SENDONES; 327 328 return (ip_output(m, inp->inp_options, &inp->inp_route, flags, 329 inp->inp_moptions, inp)); 330} 331 332/* 333 * Raw IP socket option processing. 334 * 335 * Note that access to all of the IP administrative functions here is 336 * implicitly protected by suser() as gaining access to a raw socket 337 * requires either that the thread pass a suser() check, or that it be 338 * passed a raw socket by another thread that has passed a suser() check. 339 * If FreeBSD moves to a more fine-grained access control mechanism, 340 * additional checks will need to be placed here if the raw IP attachment 341 * check is not equivilent the the check required for these 342 * administrative operations; in some cases, these checks are already 343 * present. 344 */ 345int 346rip_ctloutput(so, sopt) 347 struct socket *so; 348 struct sockopt *sopt; 349{ 350 struct inpcb *inp = sotoinpcb(so); 351 int error, optval; 352 353 if (sopt->sopt_level != IPPROTO_IP) 354 return (EINVAL); 355 356 error = 0; 357 358 switch (sopt->sopt_dir) { 359 case SOPT_GET: 360 switch (sopt->sopt_name) { 361 case IP_HDRINCL: 362 optval = inp->inp_flags & INP_HDRINCL; 363 error = sooptcopyout(sopt, &optval, sizeof optval); 364 break; 365 366 case IP_FW_ADD: /* ADD actually returns the body... */ 367 case IP_FW_GET: 368 if (IPFW_LOADED) 369 error = ip_fw_ctl_ptr(sopt); 370 else 371 error = ENOPROTOOPT; 372 break; 373 374 case IP_DUMMYNET_GET: 375 if (DUMMYNET_LOADED) 376 error = ip_dn_ctl_ptr(sopt); 377 else 378 error = ENOPROTOOPT; 379 break ; 380 381 case MRT_INIT: 382 case MRT_DONE: 383 case MRT_ADD_VIF: 384 case MRT_DEL_VIF: 385 case MRT_ADD_MFC: 386 case MRT_DEL_MFC: 387 case MRT_VERSION: 388 case MRT_ASSERT: 389 case MRT_API_SUPPORT: 390 case MRT_API_CONFIG: 391 case MRT_ADD_BW_UPCALL: 392 case MRT_DEL_BW_UPCALL: 393 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) : 394 EOPNOTSUPP; 395 break; 396 397 default: 398 error = ip_ctloutput(so, sopt); 399 break; 400 } 401 break; 402 403 case SOPT_SET: 404 switch (sopt->sopt_name) { 405 case IP_HDRINCL: 406 error = sooptcopyin(sopt, &optval, sizeof optval, 407 sizeof optval); 408 if (error) 409 break; 410 if (optval) 411 inp->inp_flags |= INP_HDRINCL; 412 else 413 inp->inp_flags &= ~INP_HDRINCL; 414 break; 415 416 case IP_FW_ADD: 417 case IP_FW_DEL: 418 case IP_FW_FLUSH: 419 case IP_FW_ZERO: 420 case IP_FW_RESETLOG: 421 if (IPFW_LOADED) 422 error = ip_fw_ctl_ptr(sopt); 423 else 424 error = ENOPROTOOPT; 425 break; 426 427 case IP_DUMMYNET_CONFIGURE: 428 case IP_DUMMYNET_DEL: 429 case IP_DUMMYNET_FLUSH: 430 if (DUMMYNET_LOADED) 431 error = ip_dn_ctl_ptr(sopt); 432 else 433 error = ENOPROTOOPT ; 434 break ; 435 436 case IP_RSVP_ON: 437 error = ip_rsvp_init(so); 438 break; 439 440 case IP_RSVP_OFF: 441 error = ip_rsvp_done(); 442 break; 443 444 case IP_RSVP_VIF_ON: 445 case IP_RSVP_VIF_OFF: 446 error = ip_rsvp_vif ? 447 ip_rsvp_vif(so, sopt) : EINVAL; 448 break; 449 450 case MRT_INIT: 451 case MRT_DONE: 452 case MRT_ADD_VIF: 453 case MRT_DEL_VIF: 454 case MRT_ADD_MFC: 455 case MRT_DEL_MFC: 456 case MRT_VERSION: 457 case MRT_ASSERT: 458 case MRT_API_SUPPORT: 459 case MRT_API_CONFIG: 460 case MRT_ADD_BW_UPCALL: 461 case MRT_DEL_BW_UPCALL: 462 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) : 463 EOPNOTSUPP; 464 break; 465 466 default: 467 error = ip_ctloutput(so, sopt); 468 break; 469 } 470 break; 471 } 472 473 return (error); 474} 475 476/* 477 * This function exists solely to receive the PRC_IFDOWN messages which 478 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, 479 * and calls in_ifadown() to remove all routes corresponding to that address. 480 * It also receives the PRC_IFUP messages from if_up() and reinstalls the 481 * interface routes. 482 */ 483void 484rip_ctlinput(cmd, sa, vip) 485 int cmd; 486 struct sockaddr *sa; 487 void *vip; 488{ 489 struct in_ifaddr *ia; 490 struct ifnet *ifp; 491 int err; 492 int flags; 493 494 switch (cmd) { 495 case PRC_IFDOWN: 496 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 497 if (ia->ia_ifa.ifa_addr == sa 498 && (ia->ia_flags & IFA_ROUTE)) { 499 /* 500 * in_ifscrub kills the interface route. 501 */ 502 in_ifscrub(ia->ia_ifp, ia); 503 /* 504 * in_ifadown gets rid of all the rest of 505 * the routes. This is not quite the right 506 * thing to do, but at least if we are running 507 * a routing process they will come back. 508 */ 509 in_ifadown(&ia->ia_ifa, 0); 510 break; 511 } 512 } 513 break; 514 515 case PRC_IFUP: 516 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 517 if (ia->ia_ifa.ifa_addr == sa) 518 break; 519 } 520 if (ia == 0 || (ia->ia_flags & IFA_ROUTE)) 521 return; 522 flags = RTF_UP; 523 ifp = ia->ia_ifa.ifa_ifp; 524 525 if ((ifp->if_flags & IFF_LOOPBACK) 526 || (ifp->if_flags & IFF_POINTOPOINT)) 527 flags |= RTF_HOST; 528 529 err = rtinit(&ia->ia_ifa, RTM_ADD, flags); 530 if (err == 0) 531 ia->ia_flags |= IFA_ROUTE; 532 break; 533 } 534} 535 536u_long rip_sendspace = RIPSNDQ; 537u_long rip_recvspace = RIPRCVQ; 538int rip_olddiverterror = 1; 539 540SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, 541 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size"); 542SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, 543 &rip_recvspace, 0, "Maximum incoming raw IP datagram size"); 544SYSCTL_INT(_net_inet_raw, OID_AUTO, olddiverterror, CTLFLAG_RW, 545 &rip_olddiverterror, 0, "Return an error when creating an 'old' DIVERT socket"); 546 547static int 548rip_attach(struct socket *so, int proto, struct thread *td) 549{ 550 struct inpcb *inp; 551 int error, s; 552 553 inp = sotoinpcb(so); 554 if (inp) 555 panic("rip_attach"); 556 if (td && (error = suser(td)) != 0) 557 return error; 558 559 if (proto >= IPPROTO_MAX || proto < 0) 560 return EPROTONOSUPPORT; 561 562 /* To be removed before 5.2 */ 563 if (rip_olddiverterror && proto == IPPROTO_OLD_DIVERT) { 564 printf("Old IPDIVERT program needs to be recompiled, or new IP proto 254 user needs sysctl net.inet.raw.olddiverterror=0\n"); 565 return EPROTONOSUPPORT; 566 } 567 568 error = soreserve(so, rip_sendspace, rip_recvspace); 569 if (error) 570 return error; 571 s = splnet(); 572 error = in_pcballoc(so, &ripcbinfo, td); 573 splx(s); 574 if (error) 575 return error; 576 inp = (struct inpcb *)so->so_pcb; 577 inp->inp_vflag |= INP_IPV4; 578 inp->inp_ip_p = proto; 579 inp->inp_ip_ttl = ip_defttl; 580 return 0; 581} 582 583static int 584rip_detach(struct socket *so) 585{ 586 struct inpcb *inp; 587 588 inp = sotoinpcb(so); 589 if (inp == 0) 590 panic("rip_detach"); 591 if (so == ip_mrouter && ip_mrouter_done) 592 ip_mrouter_done(); 593 if (ip_rsvp_force_done) 594 ip_rsvp_force_done(so); 595 if (so == ip_rsvpd) 596 ip_rsvp_done(); 597 in_pcbdetach(inp); 598 return 0; 599} 600 601static int 602rip_abort(struct socket *so) 603{ 604 soisdisconnected(so); 605 if (so->so_state & SS_NOFDREF) 606 return rip_detach(so); 607 return 0; 608} 609 610static int 611rip_disconnect(struct socket *so) 612{ 613 if ((so->so_state & SS_ISCONNECTED) == 0) 614 return ENOTCONN; 615 return rip_abort(so); 616} 617 618static int 619rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 620{ 621 struct inpcb *inp = sotoinpcb(so); 622 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 623 624 if (nam->sa_len != sizeof(*addr)) 625 return EINVAL; 626 627 if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) && 628 (addr->sin_family != AF_IMPLINK)) || 629 (addr->sin_addr.s_addr && 630 ifa_ifwithaddr((struct sockaddr *)addr) == 0)) 631 return EADDRNOTAVAIL; 632 inp->inp_laddr = addr->sin_addr; 633 return 0; 634} 635 636static int 637rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 638{ 639 struct inpcb *inp = sotoinpcb(so); 640 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 641 642 if (nam->sa_len != sizeof(*addr)) 643 return EINVAL; 644 if (TAILQ_EMPTY(&ifnet)) 645 return EADDRNOTAVAIL; 646 if ((addr->sin_family != AF_INET) && 647 (addr->sin_family != AF_IMPLINK)) 648 return EAFNOSUPPORT; 649 inp->inp_faddr = addr->sin_addr; 650 soisconnected(so); 651 return 0; 652} 653 654static int 655rip_shutdown(struct socket *so) 656{ 657 socantsendmore(so); 658 return 0; 659} 660 661static int 662rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 663 struct mbuf *control, struct thread *td) 664{ 665 struct inpcb *inp = sotoinpcb(so); 666 register u_long dst; 667 668 if (so->so_state & SS_ISCONNECTED) { 669 if (nam) { 670 m_freem(m); 671 return EISCONN; 672 } 673 dst = inp->inp_faddr.s_addr; 674 } else { 675 if (nam == NULL) { 676 m_freem(m); 677 return ENOTCONN; 678 } 679 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr; 680 } 681 return rip_output(m, so, dst); 682} 683 684static int 685rip_pcblist(SYSCTL_HANDLER_ARGS) 686{ 687 int error, i, n, s; 688 struct inpcb *inp, **inp_list; 689 inp_gen_t gencnt; 690 struct xinpgen xig; 691 692 /* 693 * The process of preparing the TCB list is too time-consuming and 694 * resource-intensive to repeat twice on every request. 695 */ 696 if (req->oldptr == 0) { 697 n = ripcbinfo.ipi_count; 698 req->oldidx = 2 * (sizeof xig) 699 + (n + n/8) * sizeof(struct xinpcb); 700 return 0; 701 } 702 703 if (req->newptr != 0) 704 return EPERM; 705 706 /* 707 * OK, now we're committed to doing something. 708 */ 709 s = splnet(); 710 gencnt = ripcbinfo.ipi_gencnt; 711 n = ripcbinfo.ipi_count; 712 splx(s); 713 714 xig.xig_len = sizeof xig; 715 xig.xig_count = n; 716 xig.xig_gen = gencnt; 717 xig.xig_sogen = so_gencnt; 718 error = SYSCTL_OUT(req, &xig, sizeof xig); 719 if (error) 720 return error; 721 722 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 723 if (inp_list == 0) 724 return ENOMEM; 725 726 s = splnet(); 727 for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n; 728 inp = LIST_NEXT(inp, inp_list)) { 729 if (inp->inp_gencnt <= gencnt) { 730 if (cr_canseesocket(req->td->td_ucred, 731 inp->inp_socket)) 732 continue; 733 inp_list[i++] = inp; 734 } 735 } 736 splx(s); 737 n = i; 738 739 error = 0; 740 for (i = 0; i < n; i++) { 741 inp = inp_list[i]; 742 if (inp->inp_gencnt <= gencnt) { 743 struct xinpcb xi; 744 xi.xi_len = sizeof xi; 745 /* XXX should avoid extra copy */ 746 bcopy(inp, &xi.xi_inp, sizeof *inp); 747 if (inp->inp_socket) 748 sotoxsocket(inp->inp_socket, &xi.xi_socket); 749 error = SYSCTL_OUT(req, &xi, sizeof xi); 750 } 751 } 752 if (!error) { 753 /* 754 * Give the user an updated idea of our state. 755 * If the generation differs from what we told 756 * her before, she knows that something happened 757 * while we were processing this request, and it 758 * might be necessary to retry. 759 */ 760 s = splnet(); 761 xig.xig_gen = ripcbinfo.ipi_gencnt; 762 xig.xig_sogen = so_gencnt; 763 xig.xig_count = ripcbinfo.ipi_count; 764 splx(s); 765 error = SYSCTL_OUT(req, &xig, sizeof xig); 766 } 767 free(inp_list, M_TEMP); 768 return error; 769} 770 771/* 772 * This is the wrapper function for in_setsockaddr. We just pass down 773 * the pcbinfo for in_setpeeraddr to lock. 774 */ 775static int 776rip_sockaddr(struct socket *so, struct sockaddr **nam) 777{ 778 return (in_setsockaddr(so, nam, &ripcbinfo)); 779} 780 781/* 782 * This is the wrapper function for in_setpeeraddr. We just pass down 783 * the pcbinfo for in_setpeeraddr to lock. 784 */ 785static int 786rip_peeraddr(struct socket *so, struct sockaddr **nam) 787{ 788 return (in_setpeeraddr(so, nam, &ripcbinfo)); 789} 790 791 792SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0, 793 rip_pcblist, "S,xinpcb", "List of active raw IP sockets"); 794 795struct pr_usrreqs rip_usrreqs = { 796 rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect, 797 pru_connect2_notsupp, in_control, rip_detach, rip_disconnect, 798 pru_listen_notsupp, rip_peeraddr, pru_rcvd_notsupp, 799 pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown, 800 rip_sockaddr, sosend, soreceive, sopoll 801}; 802