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