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