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