tcp_usrreq.c revision 142190
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 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94 30 * $FreeBSD: head/sys/netinet/tcp_usrreq.c 142190 2005-02-21 21:58:17Z rwatson $ 31 */ 32 33#include "opt_ipsec.h" 34#include "opt_inet.h" 35#include "opt_inet6.h" 36#include "opt_tcpdebug.h" 37 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/malloc.h> 41#include <sys/kernel.h> 42#include <sys/sysctl.h> 43#include <sys/mbuf.h> 44#ifdef INET6 45#include <sys/domain.h> 46#endif /* INET6 */ 47#include <sys/socket.h> 48#include <sys/socketvar.h> 49#include <sys/protosw.h> 50#include <sys/proc.h> 51#include <sys/jail.h> 52 53#include <net/if.h> 54#include <net/route.h> 55 56#include <netinet/in.h> 57#include <netinet/in_systm.h> 58#ifdef INET6 59#include <netinet/ip6.h> 60#endif 61#include <netinet/in_pcb.h> 62#ifdef INET6 63#include <netinet6/in6_pcb.h> 64#endif 65#include <netinet/in_var.h> 66#include <netinet/ip_var.h> 67#ifdef INET6 68#include <netinet6/ip6_var.h> 69#endif 70#include <netinet/tcp.h> 71#include <netinet/tcp_fsm.h> 72#include <netinet/tcp_seq.h> 73#include <netinet/tcp_timer.h> 74#include <netinet/tcp_var.h> 75#include <netinet/tcpip.h> 76#ifdef TCPDEBUG 77#include <netinet/tcp_debug.h> 78#endif 79 80#ifdef IPSEC 81#include <netinet6/ipsec.h> 82#endif /*IPSEC*/ 83 84/* 85 * TCP protocol interface to socket abstraction. 86 */ 87extern char *tcpstates[]; /* XXX ??? */ 88 89static int tcp_attach(struct socket *); 90static int tcp_connect(struct tcpcb *, struct sockaddr *, 91 struct thread *td); 92#ifdef INET6 93static int tcp6_connect(struct tcpcb *, struct sockaddr *, 94 struct thread *td); 95#endif /* INET6 */ 96static struct tcpcb * 97 tcp_disconnect(struct tcpcb *); 98static struct tcpcb * 99 tcp_usrclosed(struct tcpcb *); 100static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 101 102#ifdef TCPDEBUG 103#define TCPDEBUG0 int ostate = 0 104#define TCPDEBUG1() ostate = tp ? tp->t_state : 0 105#define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 106 tcp_trace(TA_USER, ostate, tp, 0, 0, req) 107#else 108#define TCPDEBUG0 109#define TCPDEBUG1() 110#define TCPDEBUG2(req) 111#endif 112 113/* 114 * TCP attaches to socket via pru_attach(), reserving space, 115 * and an internet control block. 116 */ 117static int 118tcp_usr_attach(struct socket *so, int proto, struct thread *td) 119{ 120 int error; 121 struct inpcb *inp; 122 struct tcpcb *tp = 0; 123 TCPDEBUG0; 124 125 INP_INFO_WLOCK(&tcbinfo); 126 TCPDEBUG1(); 127 inp = sotoinpcb(so); 128 if (inp) { 129 error = EISCONN; 130 goto out; 131 } 132 133 error = tcp_attach(so); 134 if (error) 135 goto out; 136 137 if ((so->so_options & SO_LINGER) && so->so_linger == 0) 138 so->so_linger = TCP_LINGERTIME; 139 140 inp = sotoinpcb(so); 141 tp = intotcpcb(inp); 142out: 143 TCPDEBUG2(PRU_ATTACH); 144 INP_INFO_WUNLOCK(&tcbinfo); 145 return error; 146} 147 148/* 149 * pru_detach() detaches the TCP protocol from the socket. 150 * If the protocol state is non-embryonic, then can't 151 * do this directly: have to initiate a pru_disconnect(), 152 * which may finish later; embryonic TCB's can just 153 * be discarded here. 154 */ 155static int 156tcp_usr_detach(struct socket *so) 157{ 158 int error = 0; 159 struct inpcb *inp; 160 struct tcpcb *tp; 161 TCPDEBUG0; 162 163 INP_INFO_WLOCK(&tcbinfo); 164 inp = sotoinpcb(so); 165 if (inp == NULL) { 166 INP_INFO_WUNLOCK(&tcbinfo); 167 return error; 168 } 169 INP_LOCK(inp); 170 tp = intotcpcb(inp); 171 TCPDEBUG1(); 172 tp = tcp_disconnect(tp); 173 174 TCPDEBUG2(PRU_DETACH); 175 if (tp) 176 INP_UNLOCK(inp); 177 INP_INFO_WUNLOCK(&tcbinfo); 178 return error; 179} 180 181#define INI_NOLOCK 0 182#define INI_READ 1 183#define INI_WRITE 2 184 185#define COMMON_START() \ 186 TCPDEBUG0; \ 187 do { \ 188 if (inirw == INI_READ) \ 189 INP_INFO_RLOCK(&tcbinfo); \ 190 else if (inirw == INI_WRITE) \ 191 INP_INFO_WLOCK(&tcbinfo); \ 192 inp = sotoinpcb(so); \ 193 if (inp == 0) { \ 194 if (inirw == INI_READ) \ 195 INP_INFO_RUNLOCK(&tcbinfo); \ 196 else if (inirw == INI_WRITE) \ 197 INP_INFO_WUNLOCK(&tcbinfo); \ 198 return EINVAL; \ 199 } \ 200 INP_LOCK(inp); \ 201 if (inirw == INI_READ) \ 202 INP_INFO_RUNLOCK(&tcbinfo); \ 203 tp = intotcpcb(inp); \ 204 TCPDEBUG1(); \ 205} while(0) 206 207#define COMMON_END(req) \ 208out: TCPDEBUG2(req); \ 209 do { \ 210 if (tp) \ 211 INP_UNLOCK(inp); \ 212 if (inirw == INI_WRITE) \ 213 INP_INFO_WUNLOCK(&tcbinfo); \ 214 return error; \ 215 goto out; \ 216} while(0) 217 218/* 219 * Give the socket an address. 220 */ 221static int 222tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 223{ 224 int error = 0; 225 struct inpcb *inp; 226 struct tcpcb *tp; 227 struct sockaddr_in *sinp; 228 const int inirw = INI_WRITE; 229 230 sinp = (struct sockaddr_in *)nam; 231 if (nam->sa_len != sizeof (*sinp)) 232 return (EINVAL); 233 /* 234 * Must check for multicast addresses and disallow binding 235 * to them. 236 */ 237 if (sinp->sin_family == AF_INET && 238 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 239 return (EAFNOSUPPORT); 240 241 COMMON_START(); 242 error = in_pcbbind(inp, nam, td->td_ucred); 243 if (error) 244 goto out; 245 COMMON_END(PRU_BIND); 246} 247 248#ifdef INET6 249static int 250tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 251{ 252 int error = 0; 253 struct inpcb *inp; 254 struct tcpcb *tp; 255 struct sockaddr_in6 *sin6p; 256 const int inirw = INI_WRITE; 257 258 sin6p = (struct sockaddr_in6 *)nam; 259 if (nam->sa_len != sizeof (*sin6p)) 260 return (EINVAL); 261 /* 262 * Must check for multicast addresses and disallow binding 263 * to them. 264 */ 265 if (sin6p->sin6_family == AF_INET6 && 266 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 267 return (EAFNOSUPPORT); 268 269 COMMON_START(); 270 inp->inp_vflag &= ~INP_IPV4; 271 inp->inp_vflag |= INP_IPV6; 272 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 273 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 274 inp->inp_vflag |= INP_IPV4; 275 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 276 struct sockaddr_in sin; 277 278 in6_sin6_2_sin(&sin, sin6p); 279 inp->inp_vflag |= INP_IPV4; 280 inp->inp_vflag &= ~INP_IPV6; 281 error = in_pcbbind(inp, (struct sockaddr *)&sin, 282 td->td_ucred); 283 goto out; 284 } 285 } 286 error = in6_pcbbind(inp, nam, td->td_ucred); 287 if (error) 288 goto out; 289 COMMON_END(PRU_BIND); 290} 291#endif /* INET6 */ 292 293/* 294 * Prepare to accept connections. 295 */ 296static int 297tcp_usr_listen(struct socket *so, struct thread *td) 298{ 299 int error = 0; 300 struct inpcb *inp; 301 struct tcpcb *tp; 302 const int inirw = INI_WRITE; 303 304 COMMON_START(); 305 SOCK_LOCK(so); 306 error = solisten_proto_check(so); 307 if (error == 0 && inp->inp_lport == 0) 308 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 309 if (error == 0) { 310 tp->t_state = TCPS_LISTEN; 311 solisten_proto(so); 312 } 313 SOCK_UNLOCK(so); 314 COMMON_END(PRU_LISTEN); 315} 316 317#ifdef INET6 318static int 319tcp6_usr_listen(struct socket *so, struct thread *td) 320{ 321 int error = 0; 322 struct inpcb *inp; 323 struct tcpcb *tp; 324 const int inirw = INI_WRITE; 325 326 COMMON_START(); 327 SOCK_LOCK(so); 328 error = solisten_proto_check(so); 329 if (error == 0 && inp->inp_lport == 0) { 330 inp->inp_vflag &= ~INP_IPV4; 331 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 332 inp->inp_vflag |= INP_IPV4; 333 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 334 } 335 if (error == 0) { 336 tp->t_state = TCPS_LISTEN; 337 solisten_proto(so); 338 } 339 SOCK_UNLOCK(so); 340 COMMON_END(PRU_LISTEN); 341} 342#endif /* INET6 */ 343 344/* 345 * Initiate connection to peer. 346 * Create a template for use in transmissions on this connection. 347 * Enter SYN_SENT state, and mark socket as connecting. 348 * Start keep-alive timer, and seed output sequence space. 349 * Send initial segment on connection. 350 */ 351static int 352tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 353{ 354 int error = 0; 355 struct inpcb *inp; 356 struct tcpcb *tp; 357 struct sockaddr_in *sinp; 358 const int inirw = INI_WRITE; 359 360 sinp = (struct sockaddr_in *)nam; 361 if (nam->sa_len != sizeof (*sinp)) 362 return (EINVAL); 363 /* 364 * Must disallow TCP ``connections'' to multicast addresses. 365 */ 366 if (sinp->sin_family == AF_INET 367 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 368 return (EAFNOSUPPORT); 369 if (td && jailed(td->td_ucred)) 370 prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr); 371 372 COMMON_START(); 373 if ((error = tcp_connect(tp, nam, td)) != 0) 374 goto out; 375 error = tcp_output(tp); 376 COMMON_END(PRU_CONNECT); 377} 378 379#ifdef INET6 380static int 381tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 382{ 383 int error = 0; 384 struct inpcb *inp; 385 struct tcpcb *tp; 386 struct sockaddr_in6 *sin6p; 387 const int inirw = INI_WRITE; 388 389 sin6p = (struct sockaddr_in6 *)nam; 390 if (nam->sa_len != sizeof (*sin6p)) 391 return (EINVAL); 392 /* 393 * Must disallow TCP ``connections'' to multicast addresses. 394 */ 395 if (sin6p->sin6_family == AF_INET6 396 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 397 return (EAFNOSUPPORT); 398 399 COMMON_START(); 400 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 401 struct sockaddr_in sin; 402 403 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 404 error = EINVAL; 405 goto out; 406 } 407 408 in6_sin6_2_sin(&sin, sin6p); 409 inp->inp_vflag |= INP_IPV4; 410 inp->inp_vflag &= ~INP_IPV6; 411 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 412 goto out; 413 error = tcp_output(tp); 414 goto out; 415 } 416 inp->inp_vflag &= ~INP_IPV4; 417 inp->inp_vflag |= INP_IPV6; 418 inp->inp_inc.inc_isipv6 = 1; 419 if ((error = tcp6_connect(tp, nam, td)) != 0) 420 goto out; 421 error = tcp_output(tp); 422 COMMON_END(PRU_CONNECT); 423} 424#endif /* INET6 */ 425 426/* 427 * Initiate disconnect from peer. 428 * If connection never passed embryonic stage, just drop; 429 * else if don't need to let data drain, then can just drop anyways, 430 * else have to begin TCP shutdown process: mark socket disconnecting, 431 * drain unread data, state switch to reflect user close, and 432 * send segment (e.g. FIN) to peer. Socket will be really disconnected 433 * when peer sends FIN and acks ours. 434 * 435 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 436 */ 437static int 438tcp_usr_disconnect(struct socket *so) 439{ 440 int error = 0; 441 struct inpcb *inp; 442 struct tcpcb *tp; 443 const int inirw = INI_WRITE; 444 445 COMMON_START(); 446 tp = tcp_disconnect(tp); 447 COMMON_END(PRU_DISCONNECT); 448} 449 450/* 451 * Accept a connection. Essentially all the work is 452 * done at higher levels; just return the address 453 * of the peer, storing through addr. 454 */ 455static int 456tcp_usr_accept(struct socket *so, struct sockaddr **nam) 457{ 458 int error = 0; 459 struct inpcb *inp = NULL; 460 struct tcpcb *tp = NULL; 461 struct in_addr addr; 462 in_port_t port = 0; 463 TCPDEBUG0; 464 465 if (so->so_state & SS_ISDISCONNECTED) { 466 error = ECONNABORTED; 467 goto out; 468 } 469 470 INP_INFO_RLOCK(&tcbinfo); 471 inp = sotoinpcb(so); 472 if (!inp) { 473 INP_INFO_RUNLOCK(&tcbinfo); 474 return (EINVAL); 475 } 476 INP_LOCK(inp); 477 INP_INFO_RUNLOCK(&tcbinfo); 478 tp = intotcpcb(inp); 479 TCPDEBUG1(); 480 481 /* 482 * We inline in_setpeeraddr and COMMON_END here, so that we can 483 * copy the data of interest and defer the malloc until after we 484 * release the lock. 485 */ 486 port = inp->inp_fport; 487 addr = inp->inp_faddr; 488 489out: TCPDEBUG2(PRU_ACCEPT); 490 if (tp) 491 INP_UNLOCK(inp); 492 if (error == 0) 493 *nam = in_sockaddr(port, &addr); 494 return error; 495} 496 497#ifdef INET6 498static int 499tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 500{ 501 struct inpcb *inp = NULL; 502 int error = 0; 503 struct tcpcb *tp = NULL; 504 struct in_addr addr; 505 struct in6_addr addr6; 506 in_port_t port = 0; 507 int v4 = 0; 508 TCPDEBUG0; 509 510 if (so->so_state & SS_ISDISCONNECTED) { 511 error = ECONNABORTED; 512 goto out; 513 } 514 515 INP_INFO_RLOCK(&tcbinfo); 516 inp = sotoinpcb(so); 517 if (inp == 0) { 518 INP_INFO_RUNLOCK(&tcbinfo); 519 return (EINVAL); 520 } 521 INP_LOCK(inp); 522 INP_INFO_RUNLOCK(&tcbinfo); 523 tp = intotcpcb(inp); 524 TCPDEBUG1(); 525 /* 526 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 527 * copy the data of interest and defer the malloc until after we 528 * release the lock. 529 */ 530 if (inp->inp_vflag & INP_IPV4) { 531 v4 = 1; 532 port = inp->inp_fport; 533 addr = inp->inp_faddr; 534 } else { 535 port = inp->inp_fport; 536 addr6 = inp->in6p_faddr; 537 } 538 539out: TCPDEBUG2(PRU_ACCEPT); 540 if (tp) 541 INP_UNLOCK(inp); 542 if (error == 0) { 543 if (v4) 544 *nam = in6_v4mapsin6_sockaddr(port, &addr); 545 else 546 *nam = in6_sockaddr(port, &addr6); 547 } 548 return error; 549} 550#endif /* INET6 */ 551 552/* 553 * This is the wrapper function for in_setsockaddr. We just pass down 554 * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking 555 * here because in_setsockaddr will call malloc and can block. 556 */ 557static int 558tcp_sockaddr(struct socket *so, struct sockaddr **nam) 559{ 560 return (in_setsockaddr(so, nam, &tcbinfo)); 561} 562 563/* 564 * This is the wrapper function for in_setpeeraddr. We just pass down 565 * the pcbinfo for in_setpeeraddr to lock. 566 */ 567static int 568tcp_peeraddr(struct socket *so, struct sockaddr **nam) 569{ 570 return (in_setpeeraddr(so, nam, &tcbinfo)); 571} 572 573/* 574 * Mark the connection as being incapable of further output. 575 */ 576static int 577tcp_usr_shutdown(struct socket *so) 578{ 579 int error = 0; 580 struct inpcb *inp; 581 struct tcpcb *tp; 582 const int inirw = INI_WRITE; 583 584 COMMON_START(); 585 socantsendmore(so); 586 tp = tcp_usrclosed(tp); 587 if (tp) 588 error = tcp_output(tp); 589 COMMON_END(PRU_SHUTDOWN); 590} 591 592/* 593 * After a receive, possibly send window update to peer. 594 */ 595static int 596tcp_usr_rcvd(struct socket *so, int flags) 597{ 598 int error = 0; 599 struct inpcb *inp; 600 struct tcpcb *tp; 601 const int inirw = INI_READ; 602 603 COMMON_START(); 604 tcp_output(tp); 605 COMMON_END(PRU_RCVD); 606} 607 608/* 609 * Do a send by putting data in output queue and updating urgent 610 * marker if URG set. Possibly send more data. Unlike the other 611 * pru_*() routines, the mbuf chains are our responsibility. We 612 * must either enqueue them or free them. The other pru_* routines 613 * generally are caller-frees. 614 */ 615static int 616tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 617 struct sockaddr *nam, struct mbuf *control, struct thread *td) 618{ 619 int error = 0; 620 struct inpcb *inp; 621 struct tcpcb *tp; 622 const int inirw = INI_WRITE; 623#ifdef INET6 624 int isipv6; 625#endif 626 TCPDEBUG0; 627 628 /* 629 * Need write lock here because this function might call 630 * tcp_connect or tcp_usrclosed. 631 * We really want to have to this function upgrade from read lock 632 * to write lock. XXX 633 */ 634 INP_INFO_WLOCK(&tcbinfo); 635 inp = sotoinpcb(so); 636 if (inp == NULL) { 637 /* 638 * OOPS! we lost a race, the TCP session got reset after 639 * we checked SBS_CANTSENDMORE, eg: while doing uiomove or a 640 * network interrupt in the non-splnet() section of sosend(). 641 */ 642 if (m) 643 m_freem(m); 644 if (control) 645 m_freem(control); 646 error = ECONNRESET; /* XXX EPIPE? */ 647 tp = NULL; 648 TCPDEBUG1(); 649 goto out; 650 } 651 INP_LOCK(inp); 652#ifdef INET6 653 isipv6 = nam && nam->sa_family == AF_INET6; 654#endif /* INET6 */ 655 tp = intotcpcb(inp); 656 TCPDEBUG1(); 657 if (control) { 658 /* TCP doesn't do control messages (rights, creds, etc) */ 659 if (control->m_len) { 660 m_freem(control); 661 if (m) 662 m_freem(m); 663 error = EINVAL; 664 goto out; 665 } 666 m_freem(control); /* empty control, just free it */ 667 } 668 if (!(flags & PRUS_OOB)) { 669 sbappendstream(&so->so_snd, m); 670 if (nam && tp->t_state < TCPS_SYN_SENT) { 671 /* 672 * Do implied connect if not yet connected, 673 * initialize window to default value, and 674 * initialize maxseg/maxopd using peer's cached 675 * MSS. 676 */ 677#ifdef INET6 678 if (isipv6) 679 error = tcp6_connect(tp, nam, td); 680 else 681#endif /* INET6 */ 682 error = tcp_connect(tp, nam, td); 683 if (error) 684 goto out; 685 tp->snd_wnd = TTCP_CLIENT_SND_WND; 686 tcp_mss(tp, -1); 687 } 688 689 if (flags & PRUS_EOF) { 690 /* 691 * Close the send side of the connection after 692 * the data is sent. 693 */ 694 socantsendmore(so); 695 tp = tcp_usrclosed(tp); 696 } 697 if (tp != NULL) { 698 if (flags & PRUS_MORETOCOME) 699 tp->t_flags |= TF_MORETOCOME; 700 error = tcp_output(tp); 701 if (flags & PRUS_MORETOCOME) 702 tp->t_flags &= ~TF_MORETOCOME; 703 } 704 } else { 705 if (sbspace(&so->so_snd) < -512) { 706 m_freem(m); 707 error = ENOBUFS; 708 goto out; 709 } 710 /* 711 * According to RFC961 (Assigned Protocols), 712 * the urgent pointer points to the last octet 713 * of urgent data. We continue, however, 714 * to consider it to indicate the first octet 715 * of data past the urgent section. 716 * Otherwise, snd_up should be one lower. 717 */ 718 sbappendstream(&so->so_snd, m); 719 if (nam && tp->t_state < TCPS_SYN_SENT) { 720 /* 721 * Do implied connect if not yet connected, 722 * initialize window to default value, and 723 * initialize maxseg/maxopd using peer's cached 724 * MSS. 725 */ 726#ifdef INET6 727 if (isipv6) 728 error = tcp6_connect(tp, nam, td); 729 else 730#endif /* INET6 */ 731 error = tcp_connect(tp, nam, td); 732 if (error) 733 goto out; 734 tp->snd_wnd = TTCP_CLIENT_SND_WND; 735 tcp_mss(tp, -1); 736 } 737 tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 738 tp->t_force = 1; 739 error = tcp_output(tp); 740 tp->t_force = 0; 741 } 742 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB : 743 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 744} 745 746/* 747 * Abort the TCP. 748 */ 749static int 750tcp_usr_abort(struct socket *so) 751{ 752 int error = 0; 753 struct inpcb *inp; 754 struct tcpcb *tp; 755 const int inirw = INI_WRITE; 756 757 COMMON_START(); 758 tp = tcp_drop(tp, ECONNABORTED); 759 COMMON_END(PRU_ABORT); 760} 761 762/* 763 * Receive out-of-band data. 764 */ 765static int 766tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 767{ 768 int error = 0; 769 struct inpcb *inp; 770 struct tcpcb *tp; 771 const int inirw = INI_READ; 772 773 COMMON_START(); 774 if ((so->so_oobmark == 0 && 775 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 776 so->so_options & SO_OOBINLINE || 777 tp->t_oobflags & TCPOOB_HADDATA) { 778 error = EINVAL; 779 goto out; 780 } 781 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 782 error = EWOULDBLOCK; 783 goto out; 784 } 785 m->m_len = 1; 786 *mtod(m, caddr_t) = tp->t_iobc; 787 if ((flags & MSG_PEEK) == 0) 788 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 789 COMMON_END(PRU_RCVOOB); 790} 791 792struct pr_usrreqs tcp_usrreqs = { 793 .pru_abort = tcp_usr_abort, 794 .pru_accept = tcp_usr_accept, 795 .pru_attach = tcp_usr_attach, 796 .pru_bind = tcp_usr_bind, 797 .pru_connect = tcp_usr_connect, 798 .pru_control = in_control, 799 .pru_detach = tcp_usr_detach, 800 .pru_disconnect = tcp_usr_disconnect, 801 .pru_listen = tcp_usr_listen, 802 .pru_peeraddr = tcp_peeraddr, 803 .pru_rcvd = tcp_usr_rcvd, 804 .pru_rcvoob = tcp_usr_rcvoob, 805 .pru_send = tcp_usr_send, 806 .pru_shutdown = tcp_usr_shutdown, 807 .pru_sockaddr = tcp_sockaddr, 808 .pru_sosetlabel = in_pcbsosetlabel 809}; 810 811#ifdef INET6 812struct pr_usrreqs tcp6_usrreqs = { 813 .pru_abort = tcp_usr_abort, 814 .pru_accept = tcp6_usr_accept, 815 .pru_attach = tcp_usr_attach, 816 .pru_bind = tcp6_usr_bind, 817 .pru_connect = tcp6_usr_connect, 818 .pru_control = in6_control, 819 .pru_detach = tcp_usr_detach, 820 .pru_disconnect = tcp_usr_disconnect, 821 .pru_listen = tcp6_usr_listen, 822 .pru_peeraddr = in6_mapped_peeraddr, 823 .pru_rcvd = tcp_usr_rcvd, 824 .pru_rcvoob = tcp_usr_rcvoob, 825 .pru_send = tcp_usr_send, 826 .pru_shutdown = tcp_usr_shutdown, 827 .pru_sockaddr = in6_mapped_sockaddr, 828 .pru_sosetlabel = in_pcbsosetlabel 829}; 830#endif /* INET6 */ 831 832/* 833 * Common subroutine to open a TCP connection to remote host specified 834 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 835 * port number if needed. Call in_pcbconnect_setup to do the routing and 836 * to choose a local host address (interface). If there is an existing 837 * incarnation of the same connection in TIME-WAIT state and if the remote 838 * host was sending CC options and if the connection duration was < MSL, then 839 * truncate the previous TIME-WAIT state and proceed. 840 * Initialize connection parameters and enter SYN-SENT state. 841 */ 842static int 843tcp_connect(tp, nam, td) 844 register struct tcpcb *tp; 845 struct sockaddr *nam; 846 struct thread *td; 847{ 848 struct inpcb *inp = tp->t_inpcb, *oinp; 849 struct socket *so = inp->inp_socket; 850 struct in_addr laddr; 851 u_short lport; 852 int error; 853 854 if (inp->inp_lport == 0) { 855 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 856 if (error) 857 return error; 858 } 859 860 /* 861 * Cannot simply call in_pcbconnect, because there might be an 862 * earlier incarnation of this same connection still in 863 * TIME_WAIT state, creating an ADDRINUSE error. 864 */ 865 laddr = inp->inp_laddr; 866 lport = inp->inp_lport; 867 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 868 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 869 if (error && oinp == NULL) 870 return error; 871 if (oinp) 872 return EADDRINUSE; 873 inp->inp_laddr = laddr; 874 in_pcbrehash(inp); 875 876 /* Compute window scaling to request. */ 877 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 878 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 879 tp->request_r_scale++; 880 881 soisconnecting(so); 882 tcpstat.tcps_connattempt++; 883 tp->t_state = TCPS_SYN_SENT; 884 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 885 tp->iss = tcp_new_isn(tp); 886 tp->t_bw_rtseq = tp->iss; 887 tcp_sendseqinit(tp); 888 889 return 0; 890} 891 892#ifdef INET6 893static int 894tcp6_connect(tp, nam, td) 895 register struct tcpcb *tp; 896 struct sockaddr *nam; 897 struct thread *td; 898{ 899 struct inpcb *inp = tp->t_inpcb, *oinp; 900 struct socket *so = inp->inp_socket; 901 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 902 struct in6_addr *addr6; 903 int error; 904 905 if (inp->inp_lport == 0) { 906 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 907 if (error) 908 return error; 909 } 910 911 /* 912 * Cannot simply call in_pcbconnect, because there might be an 913 * earlier incarnation of this same connection still in 914 * TIME_WAIT state, creating an ADDRINUSE error. 915 */ 916 error = in6_pcbladdr(inp, nam, &addr6); 917 if (error) 918 return error; 919 oinp = in6_pcblookup_hash(inp->inp_pcbinfo, 920 &sin6->sin6_addr, sin6->sin6_port, 921 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 922 ? addr6 923 : &inp->in6p_laddr, 924 inp->inp_lport, 0, NULL); 925 if (oinp) 926 return EADDRINUSE; 927 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 928 inp->in6p_laddr = *addr6; 929 inp->in6p_faddr = sin6->sin6_addr; 930 inp->inp_fport = sin6->sin6_port; 931 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 932 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 933 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL) 934 inp->in6p_flowinfo |= 935 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 936 in_pcbrehash(inp); 937 938 /* Compute window scaling to request. */ 939 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 940 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 941 tp->request_r_scale++; 942 943 soisconnecting(so); 944 tcpstat.tcps_connattempt++; 945 tp->t_state = TCPS_SYN_SENT; 946 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 947 tp->iss = tcp_new_isn(tp); 948 tp->t_bw_rtseq = tp->iss; 949 tcp_sendseqinit(tp); 950 951 return 0; 952} 953#endif /* INET6 */ 954 955/* 956 * Export TCP internal state information via a struct tcp_info, based on the 957 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 958 * (TCP state machine, etc). We export all information using FreeBSD-native 959 * constants -- for example, the numeric values for tcpi_state will differ 960 * from Linux. 961 */ 962static void 963tcp_fill_info(tp, ti) 964 struct tcpcb *tp; 965 struct tcp_info *ti; 966{ 967 968 INP_LOCK_ASSERT(tp->t_inpcb); 969 bzero(ti, sizeof(*ti)); 970 971 ti->tcpi_state = tp->t_state; 972 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 973 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 974 if (tp->sack_enable) 975 ti->tcpi_options |= TCPI_OPT_SACK; 976 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 977 ti->tcpi_options |= TCPI_OPT_WSCALE; 978 ti->tcpi_snd_wscale = tp->snd_scale; 979 ti->tcpi_rcv_wscale = tp->rcv_scale; 980 } 981 ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 982 ti->tcpi_snd_cwnd = tp->snd_cwnd; 983 984 /* 985 * FreeBSD-specific extension fields for tcp_info. 986 */ 987 ti->tcpi_rcv_space = tp->rcv_wnd; 988 ti->tcpi_snd_wnd = tp->snd_wnd; 989 ti->tcpi_snd_bwnd = tp->snd_bwnd; 990} 991 992/* 993 * The new sockopt interface makes it possible for us to block in the 994 * copyin/out step (if we take a page fault). Taking a page fault at 995 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now 996 * use TSM, there probably isn't any need for this function to run at 997 * splnet() any more. This needs more examination.) 998 * 999 * XXXRW: The locking here is wrong; we may take a page fault while holding 1000 * the inpcb lock. 1001 */ 1002int 1003tcp_ctloutput(so, sopt) 1004 struct socket *so; 1005 struct sockopt *sopt; 1006{ 1007 int error, opt, optval; 1008 struct inpcb *inp; 1009 struct tcpcb *tp; 1010 struct tcp_info ti; 1011 1012 error = 0; 1013 INP_INFO_RLOCK(&tcbinfo); 1014 inp = sotoinpcb(so); 1015 if (inp == NULL) { 1016 INP_INFO_RUNLOCK(&tcbinfo); 1017 return (ECONNRESET); 1018 } 1019 INP_LOCK(inp); 1020 INP_INFO_RUNLOCK(&tcbinfo); 1021 if (sopt->sopt_level != IPPROTO_TCP) { 1022 INP_UNLOCK(inp); 1023#ifdef INET6 1024 if (INP_CHECK_SOCKAF(so, AF_INET6)) 1025 error = ip6_ctloutput(so, sopt); 1026 else 1027#endif /* INET6 */ 1028 error = ip_ctloutput(so, sopt); 1029 return (error); 1030 } 1031 tp = intotcpcb(inp); 1032 1033 switch (sopt->sopt_dir) { 1034 case SOPT_SET: 1035 switch (sopt->sopt_name) { 1036#ifdef TCP_SIGNATURE 1037 case TCP_MD5SIG: 1038 error = sooptcopyin(sopt, &optval, sizeof optval, 1039 sizeof optval); 1040 if (error) 1041 break; 1042 1043 if (optval > 0) 1044 tp->t_flags |= TF_SIGNATURE; 1045 else 1046 tp->t_flags &= ~TF_SIGNATURE; 1047 break; 1048#endif /* TCP_SIGNATURE */ 1049 case TCP_NODELAY: 1050 case TCP_NOOPT: 1051 error = sooptcopyin(sopt, &optval, sizeof optval, 1052 sizeof optval); 1053 if (error) 1054 break; 1055 1056 switch (sopt->sopt_name) { 1057 case TCP_NODELAY: 1058 opt = TF_NODELAY; 1059 break; 1060 case TCP_NOOPT: 1061 opt = TF_NOOPT; 1062 break; 1063 default: 1064 opt = 0; /* dead code to fool gcc */ 1065 break; 1066 } 1067 1068 if (optval) 1069 tp->t_flags |= opt; 1070 else 1071 tp->t_flags &= ~opt; 1072 break; 1073 1074 case TCP_NOPUSH: 1075 error = sooptcopyin(sopt, &optval, sizeof optval, 1076 sizeof optval); 1077 if (error) 1078 break; 1079 1080 if (optval) 1081 tp->t_flags |= TF_NOPUSH; 1082 else { 1083 tp->t_flags &= ~TF_NOPUSH; 1084 error = tcp_output(tp); 1085 } 1086 break; 1087 1088 case TCP_MAXSEG: 1089 error = sooptcopyin(sopt, &optval, sizeof optval, 1090 sizeof optval); 1091 if (error) 1092 break; 1093 1094 if (optval > 0 && optval <= tp->t_maxseg && 1095 optval + 40 >= tcp_minmss) 1096 tp->t_maxseg = optval; 1097 else 1098 error = EINVAL; 1099 break; 1100 1101 case TCP_INFO: 1102 error = EINVAL; 1103 break; 1104 1105 default: 1106 error = ENOPROTOOPT; 1107 break; 1108 } 1109 break; 1110 1111 case SOPT_GET: 1112 switch (sopt->sopt_name) { 1113#ifdef TCP_SIGNATURE 1114 case TCP_MD5SIG: 1115 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 1116 error = sooptcopyout(sopt, &optval, sizeof optval); 1117 break; 1118#endif 1119 case TCP_NODELAY: 1120 optval = tp->t_flags & TF_NODELAY; 1121 error = sooptcopyout(sopt, &optval, sizeof optval); 1122 break; 1123 case TCP_MAXSEG: 1124 optval = tp->t_maxseg; 1125 error = sooptcopyout(sopt, &optval, sizeof optval); 1126 break; 1127 case TCP_NOOPT: 1128 optval = tp->t_flags & TF_NOOPT; 1129 error = sooptcopyout(sopt, &optval, sizeof optval); 1130 break; 1131 case TCP_NOPUSH: 1132 optval = tp->t_flags & TF_NOPUSH; 1133 error = sooptcopyout(sopt, &optval, sizeof optval); 1134 break; 1135 case TCP_INFO: 1136 tcp_fill_info(tp, &ti); 1137 error = sooptcopyout(sopt, &ti, sizeof ti); 1138 break; 1139 default: 1140 error = ENOPROTOOPT; 1141 break; 1142 } 1143 break; 1144 } 1145 INP_UNLOCK(inp); 1146 return (error); 1147} 1148 1149/* 1150 * tcp_sendspace and tcp_recvspace are the default send and receive window 1151 * sizes, respectively. These are obsolescent (this information should 1152 * be set by the route). 1153 */ 1154u_long tcp_sendspace = 1024*32; 1155SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 1156 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); 1157u_long tcp_recvspace = 1024*64; 1158SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 1159 &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); 1160 1161/* 1162 * Attach TCP protocol to socket, allocating 1163 * internet protocol control block, tcp control block, 1164 * bufer space, and entering LISTEN state if to accept connections. 1165 */ 1166static int 1167tcp_attach(so) 1168 struct socket *so; 1169{ 1170 register struct tcpcb *tp; 1171 struct inpcb *inp; 1172 int error; 1173#ifdef INET6 1174 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0; 1175#endif 1176 1177 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1178 error = soreserve(so, tcp_sendspace, tcp_recvspace); 1179 if (error) 1180 return (error); 1181 } 1182 error = in_pcballoc(so, &tcbinfo, "tcpinp"); 1183 if (error) 1184 return (error); 1185 inp = sotoinpcb(so); 1186#ifdef INET6 1187 if (isipv6) { 1188 inp->inp_vflag |= INP_IPV6; 1189 inp->in6p_hops = -1; /* use kernel default */ 1190 } 1191 else 1192#endif 1193 inp->inp_vflag |= INP_IPV4; 1194 tp = tcp_newtcpcb(inp); 1195 if (tp == 0) { 1196 int nofd = so->so_state & SS_NOFDREF; /* XXX */ 1197 1198 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 1199#ifdef INET6 1200 if (isipv6) 1201 in6_pcbdetach(inp); 1202 else 1203#endif 1204 in_pcbdetach(inp); 1205 so->so_state |= nofd; 1206 return (ENOBUFS); 1207 } 1208 tp->t_state = TCPS_CLOSED; 1209 return (0); 1210} 1211 1212/* 1213 * Initiate (or continue) disconnect. 1214 * If embryonic state, just send reset (once). 1215 * If in ``let data drain'' option and linger null, just drop. 1216 * Otherwise (hard), mark socket disconnecting and drop 1217 * current input data; switch states based on user close, and 1218 * send segment to peer (with FIN). 1219 */ 1220static struct tcpcb * 1221tcp_disconnect(tp) 1222 register struct tcpcb *tp; 1223{ 1224 struct socket *so = tp->t_inpcb->inp_socket; 1225 1226 if (tp->t_state < TCPS_ESTABLISHED) 1227 tp = tcp_close(tp); 1228 else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1229 tp = tcp_drop(tp, 0); 1230 else { 1231 soisdisconnecting(so); 1232 sbflush(&so->so_rcv); 1233 tp = tcp_usrclosed(tp); 1234 if (tp) 1235 (void) tcp_output(tp); 1236 } 1237 return (tp); 1238} 1239 1240/* 1241 * User issued close, and wish to trail through shutdown states: 1242 * if never received SYN, just forget it. If got a SYN from peer, 1243 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1244 * If already got a FIN from peer, then almost done; go to LAST_ACK 1245 * state. In all other cases, have already sent FIN to peer (e.g. 1246 * after PRU_SHUTDOWN), and just have to play tedious game waiting 1247 * for peer to send FIN or not respond to keep-alives, etc. 1248 * We can let the user exit from the close as soon as the FIN is acked. 1249 */ 1250static struct tcpcb * 1251tcp_usrclosed(tp) 1252 register struct tcpcb *tp; 1253{ 1254 1255 switch (tp->t_state) { 1256 1257 case TCPS_CLOSED: 1258 case TCPS_LISTEN: 1259 tp->t_state = TCPS_CLOSED; 1260 tp = tcp_close(tp); 1261 break; 1262 1263 case TCPS_SYN_SENT: 1264 case TCPS_SYN_RECEIVED: 1265 tp->t_flags |= TF_NEEDFIN; 1266 break; 1267 1268 case TCPS_ESTABLISHED: 1269 tp->t_state = TCPS_FIN_WAIT_1; 1270 break; 1271 1272 case TCPS_CLOSE_WAIT: 1273 tp->t_state = TCPS_LAST_ACK; 1274 break; 1275 } 1276 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 1277 soisdisconnected(tp->t_inpcb->inp_socket); 1278 /* To prevent the connection hanging in FIN_WAIT_2 forever. */ 1279 if (tp->t_state == TCPS_FIN_WAIT_2) 1280 callout_reset(tp->tt_2msl, tcp_maxidle, 1281 tcp_timer_2msl, tp); 1282 } 1283 return (tp); 1284} 1285