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