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