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