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