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$ 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 77 struct inpcbhead ripcb; 78 struct inpcbinfo ripcbinfo; 79 80 /* control hooks for ipfw and dummynet */ 81 ip_fw_ctl_t *ip_fw_ctl_ptr = NULL; 82 ip_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. */ 90 struct socket *ip_mrouter; 91 92 /* The various mrouter and rsvp functions */ 93 int (*ip_mrouter_set)(struct socket *, struct sockopt *); 94 int (*ip_mrouter_get)(struct socket *, struct sockopt *); 95 int (*ip_mrouter_done)(void); 96 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *, 97 struct ip_moptions *); 98 int (*mrt_ioctl)(int, caddr_t); 99 int (*legal_vif_num)(int); 100 u_long (*ip_mcast_src)(int); 101 102 void (*rsvp_input_p)(struct mbuf *m, int off); 103 int (*ip_rsvp_vif)(struct socket *, struct sockopt *); 104 void (*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 */ 119 void 120 rip_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 137 static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET }; 138 139 static int 140 raw_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 */ 188 void 189 rip_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 */ 246 int 247 rip_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 */ 344 int 345 rip_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 */ 511 void 512 rip_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 561 u_long rip_sendspace = RIPSNDQ; 562 u_long rip_recvspace = RIPRCVQ; 563 564 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, 565 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size"); 566 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, 567 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams"); 568 569 static int 570 rip_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 616 static void 617 rip_pcbdetach(struct socket *so, struct inpcb *inp) 618 { 619 620 INP_INFO_WLOCK_ASSERT(&ripcbinfo); 621 INP_LOCK_ASSERT(inp); 622 623 if (so == ip_mrouter && ip_mrouter_done) 624 ip_mrouter_done(); 625 if (ip_rsvp_force_done) 626 ip_rsvp_force_done(so); 627 if (so == ip_rsvpd) 628 ip_rsvp_done(); 629 in_pcbdetach(inp); 630 } 631 632 static int 633 rip_detach(struct socket *so) 634 { 635 struct inpcb *inp; 636 637 INP_INFO_WLOCK(&ripcbinfo); 638 inp = sotoinpcb(so); 639 if (inp == 0) { 640 /* XXX counter, printf */ 641 INP_INFO_WUNLOCK(&ripcbinfo); 642 return EINVAL; 643 } 644 INP_LOCK(inp); 645 rip_pcbdetach(so, inp); 646 INP_INFO_WUNLOCK(&ripcbinfo); 647 return 0; 648 } 649 650 static int 651 rip_abort(struct socket *so) 652 { 653 struct inpcb *inp; 654 655 INP_INFO_WLOCK(&ripcbinfo); 656 inp = sotoinpcb(so); 657 if (inp == 0) { 658 INP_INFO_WUNLOCK(&ripcbinfo); 659 return EINVAL; /* ??? possible? panic instead? */ 660 } 661 INP_LOCK(inp); 662 soisdisconnected(so); 663 if (so->so_state & SS_NOFDREF) 664 rip_pcbdetach(so, inp); 665 else 666 INP_UNLOCK(inp); 667 INP_INFO_WUNLOCK(&ripcbinfo); 668 return 0; 669 } 670 671 static int 672 rip_disconnect(struct socket *so) 673 { 674 if ((so->so_state & SS_ISCONNECTED) == 0) 675 return ENOTCONN; 676 return rip_abort(so); 677 } 678 679 static int 680 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 681 { 682 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 683 struct inpcb *inp; 684 685 if (nam->sa_len != sizeof(*addr)) 686 return EINVAL; 687 688 if (jailed(td->td_ucred)) { 689 if (addr->sin_addr.s_addr == INADDR_ANY) 690 addr->sin_addr.s_addr = 691 htonl(prison_getip(td->td_ucred)); 692 if (htonl(prison_getip(td->td_ucred)) != addr->sin_addr.s_addr) 693 return (EADDRNOTAVAIL); 694 } 695 696 if (TAILQ_EMPTY(&ifnet) || 697 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) || 698 (addr->sin_addr.s_addr && 699 ifa_ifwithaddr((struct sockaddr *)addr) == 0)) 700 return EADDRNOTAVAIL; 701 702 INP_INFO_WLOCK(&ripcbinfo); 703 inp = sotoinpcb(so); 704 if (inp == 0) { 705 INP_INFO_WUNLOCK(&ripcbinfo); 706 return EINVAL; 707 } 708 INP_LOCK(inp); 709 inp->inp_laddr = addr->sin_addr; 710 INP_UNLOCK(inp); 711 INP_INFO_WUNLOCK(&ripcbinfo); 712 return 0; 713 } 714 715 static int 716 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 717 { 718 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 719 struct inpcb *inp; 720 721 if (nam->sa_len != sizeof(*addr)) 722 return EINVAL; 723 if (TAILQ_EMPTY(&ifnet)) 724 return EADDRNOTAVAIL; 725 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) 726 return EAFNOSUPPORT; 727 728 INP_INFO_WLOCK(&ripcbinfo); 729 inp = sotoinpcb(so); 730 if (inp == 0) { 731 INP_INFO_WUNLOCK(&ripcbinfo); 732 return EINVAL; 733 } 734 INP_LOCK(inp); 735 inp->inp_faddr = addr->sin_addr; 736 soisconnected(so); 737 INP_UNLOCK(inp); 738 INP_INFO_WUNLOCK(&ripcbinfo); 739 return 0; 740 } 741 742 static int 743 rip_shutdown(struct socket *so) 744 { 745 struct inpcb *inp; 746 747 INP_INFO_RLOCK(&ripcbinfo); 748 inp = sotoinpcb(so); 749 if (inp == 0) { 750 INP_INFO_RUNLOCK(&ripcbinfo); 751 return EINVAL; 752 } 753 INP_LOCK(inp); 754 INP_INFO_RUNLOCK(&ripcbinfo); 755 socantsendmore(so); 756 INP_UNLOCK(inp); 757 return 0; 758 } 759 760 static int 761 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 762 struct mbuf *control, struct thread *td) 763 { 764 struct inpcb *inp; 765 u_long dst; 766 int ret; 767 768 INP_INFO_WLOCK(&ripcbinfo); 769 inp = sotoinpcb(so); 770 if (so->so_state & SS_ISCONNECTED) { 771 if (nam) { 772 INP_INFO_WUNLOCK(&ripcbinfo); 773 m_freem(m); 774 return EISCONN; 775 } 776 dst = inp->inp_faddr.s_addr; 777 } else { 778 if (nam == NULL) { 779 INP_INFO_WUNLOCK(&ripcbinfo); 780 m_freem(m); 781 return ENOTCONN; 782 } 783 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr; 784 } 785 ret = rip_output(m, so, dst); 786 INP_INFO_WUNLOCK(&ripcbinfo); 787 return ret; 788 } 789 790 static int 791 rip_pcblist(SYSCTL_HANDLER_ARGS) 792 { 793 int error, i, n; 794 struct inpcb *inp, **inp_list; 795 inp_gen_t gencnt; 796 struct xinpgen xig; 797 798 /* 799 * The process of preparing the TCB list is too time-consuming and 800 * resource-intensive to repeat twice on every request. 801 */ 802 if (req->oldptr == 0) { 803 n = ripcbinfo.ipi_count; 804 req->oldidx = 2 * (sizeof xig) 805 + (n + n/8) * sizeof(struct xinpcb); 806 return 0; 807 } 808 809 if (req->newptr != 0) 810 return EPERM; 811 812 /* 813 * OK, now we're committed to doing something. 814 */ 815 INP_INFO_RLOCK(&ripcbinfo); 816 gencnt = ripcbinfo.ipi_gencnt; 817 n = ripcbinfo.ipi_count; 818 INP_INFO_RUNLOCK(&ripcbinfo); 819 820 xig.xig_len = sizeof xig; 821 xig.xig_count = n; 822 xig.xig_gen = gencnt; 823 xig.xig_sogen = so_gencnt; 824 error = SYSCTL_OUT(req, &xig, sizeof xig); 825 if (error) 826 return error; 827 828 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 829 if (inp_list == 0) 830 return ENOMEM; 831 832 INP_INFO_RLOCK(&ripcbinfo); 833 for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n; 834 inp = LIST_NEXT(inp, inp_list)) { 835 INP_LOCK(inp); 836 if (inp->inp_gencnt <= gencnt && 837 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) { 838 /* XXX held references? */ 839 inp_list[i++] = inp; 840 } 841 INP_UNLOCK(inp); 842 } 843 INP_INFO_RUNLOCK(&ripcbinfo); 844 n = i; 845 846 error = 0; 847 for (i = 0; i < n; i++) { 848 inp = inp_list[i]; 849 if (inp->inp_gencnt <= gencnt) { 850 struct xinpcb xi; 851 bzero(&xi, sizeof(xi)); 852 xi.xi_len = sizeof xi; 853 /* XXX should avoid extra copy */ 854 bcopy(inp, &xi.xi_inp, sizeof *inp); 855 if (inp->inp_socket) 856 sotoxsocket(inp->inp_socket, &xi.xi_socket); 857 error = SYSCTL_OUT(req, &xi, sizeof xi); 858 } 859 } 860 if (!error) { 861 /* 862 * Give the user an updated idea of our state. 863 * If the generation differs from what we told 864 * her before, she knows that something happened 865 * while we were processing this request, and it 866 * might be necessary to retry. 867 */ 868 INP_INFO_RLOCK(&ripcbinfo); 869 xig.xig_gen = ripcbinfo.ipi_gencnt; 870 xig.xig_sogen = so_gencnt; 871 xig.xig_count = ripcbinfo.ipi_count; 872 INP_INFO_RUNLOCK(&ripcbinfo); 873 error = SYSCTL_OUT(req, &xig, sizeof xig); 874 } 875 free(inp_list, M_TEMP); 876 return error; 877 } 878 879 /* 880 * This is the wrapper function for in_setsockaddr. We just pass down 881 * the pcbinfo for in_setpeeraddr to lock. 882 */ 883 static int 884 rip_sockaddr(struct socket *so, struct sockaddr **nam) 885 { 886 return (in_setsockaddr(so, nam, &ripcbinfo)); 887 } 888 889 /* 890 * This is the wrapper function for in_setpeeraddr. We just pass down 891 * the pcbinfo for in_setpeeraddr to lock. 892 */ 893 static int 894 rip_peeraddr(struct socket *so, struct sockaddr **nam) 895 { 896 return (in_setpeeraddr(so, nam, &ripcbinfo)); 897 } 898 899 900 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0, 901 rip_pcblist, "S,xinpcb", "List of active raw IP sockets"); 902 903 struct pr_usrreqs rip_usrreqs = { 904 .pru_abort = rip_abort, 905 .pru_attach = rip_attach, 906 .pru_bind = rip_bind, 907 .pru_connect = rip_connect, 908 .pru_control = in_control, 909 .pru_detach = rip_detach, 910 .pru_disconnect = rip_disconnect, 911 .pru_peeraddr = rip_peeraddr, 912 .pru_send = rip_send, 913 .pru_shutdown = rip_shutdown, 914 .pru_sockaddr = rip_sockaddr, 915 .pru_sosetlabel = in_pcbsosetlabel 916 }; 917