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