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