2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
37 #include "opt_ipsec.h"
38 #include "opt_inet6.h"
39 #include "opt_tcpdebug.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/kernel.h>
45 #include <sys/sysctl.h>
48 #include <sys/domain.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/protosw.h>
57 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
62 #include <netinet/ip6.h>
64 #include <netinet/in_pcb.h>
66 #include <netinet6/in6_pcb.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip_var.h>
71 #include <netinet6/ip6_var.h>
73 #include <netinet/tcp.h>
74 #include <netinet/tcp_fsm.h>
75 #include <netinet/tcp_seq.h>
76 #include <netinet/tcp_timer.h>
77 #include <netinet/tcp_var.h>
78 #include <netinet/tcpip.h>
80 #include <netinet/tcp_debug.h>
84 #include <netinet6/ipsec.h>
88 * TCP protocol interface to socket abstraction.
90 extern char *tcpstates[]; /* XXX ??? */
92 static int tcp_attach(struct socket *, struct thread *td);
93 static int tcp_connect(struct tcpcb *, struct sockaddr *,
96 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
100 tcp_disconnect(struct tcpcb *);
101 static struct tcpcb *
102 tcp_usrclosed(struct tcpcb *);
105 #define TCPDEBUG0 int ostate = 0
106 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
107 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
108 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
112 #define TCPDEBUG2(req)
116 * TCP attaches to socket via pru_attach(), reserving space,
117 * and an internet control block.
120 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
125 struct tcpcb *tp = 0;
128 INP_INFO_WLOCK(&tcbinfo);
136 error = tcp_attach(so, td);
140 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
141 so->so_linger = TCP_LINGERTIME;
146 TCPDEBUG2(PRU_ATTACH);
147 INP_INFO_WUNLOCK(&tcbinfo);
153 * pru_detach() detaches the TCP protocol from the socket.
154 * If the protocol state is non-embryonic, then can't
155 * do this directly: have to initiate a pru_disconnect(),
156 * which may finish later; embryonic TCB's can just
160 tcp_usr_detach(struct socket *so)
168 INP_INFO_WLOCK(&tcbinfo);
171 INP_INFO_WUNLOCK(&tcbinfo);
173 return EINVAL; /* XXX */
178 tp = tcp_disconnect(tp);
180 TCPDEBUG2(PRU_DETACH);
183 INP_INFO_WUNLOCK(&tcbinfo);
192 #define COMMON_START() \
195 if (inirw == INI_READ) \
196 INP_INFO_RLOCK(&tcbinfo); \
197 else if (inirw == INI_WRITE) \
198 INP_INFO_WLOCK(&tcbinfo); \
199 inp = sotoinpcb(so); \
201 if (inirw == INI_READ) \
202 INP_INFO_RUNLOCK(&tcbinfo); \
203 else if (inirw == INI_WRITE) \
204 INP_INFO_WUNLOCK(&tcbinfo); \
209 if (inirw == INI_READ) \
210 INP_INFO_RUNLOCK(&tcbinfo); \
211 tp = intotcpcb(inp); \
215 #define COMMON_END(req) \
216 out: TCPDEBUG2(req); \
220 if (inirw == INI_WRITE) \
221 INP_INFO_WUNLOCK(&tcbinfo); \
228 * Give the socket an address.
231 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
237 struct sockaddr_in *sinp;
238 const int inirw = INI_READ;
243 * Must check for multicast addresses and disallow binding
246 sinp = (struct sockaddr_in *)nam;
247 if (sinp->sin_family == AF_INET &&
248 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
249 error = EAFNOSUPPORT;
252 error = in_pcbbind(inp, nam, td);
255 COMMON_END(PRU_BIND);
260 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
266 struct sockaddr_in6 *sin6p;
267 const int inirw = INI_READ;
272 * Must check for multicast addresses and disallow binding
275 sin6p = (struct sockaddr_in6 *)nam;
276 if (sin6p->sin6_family == AF_INET6 &&
277 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
278 error = EAFNOSUPPORT;
281 inp->inp_vflag &= ~INP_IPV4;
282 inp->inp_vflag |= INP_IPV6;
283 if (ip6_mapped_addr_on && (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
284 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
285 inp->inp_vflag |= INP_IPV4;
286 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
287 struct sockaddr_in sin;
289 in6_sin6_2_sin(&sin, sin6p);
290 inp->inp_vflag |= INP_IPV4;
291 inp->inp_vflag &= ~INP_IPV6;
292 error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
296 error = in6_pcbbind(inp, nam, td);
299 COMMON_END(PRU_BIND);
304 * Prepare to accept connections.
307 tcp_usr_listen(struct socket *so, struct thread *td)
313 const int inirw = INI_READ;
316 if (inp->inp_lport == 0)
317 error = in_pcbbind(inp, (struct sockaddr *)0, td);
319 tp->t_state = TCPS_LISTEN;
320 COMMON_END(PRU_LISTEN);
325 tcp6_usr_listen(struct socket *so, struct thread *td)
331 const int inirw = INI_READ;
334 if (inp->inp_lport == 0) {
335 inp->inp_vflag &= ~INP_IPV4;
336 if (ip6_mapped_addr_on &&
337 (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
338 inp->inp_vflag |= INP_IPV4;
339 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
342 tp->t_state = TCPS_LISTEN;
343 COMMON_END(PRU_LISTEN);
348 * Initiate connection to peer.
349 * Create a template for use in transmissions on this connection.
350 * Enter SYN_SENT state, and mark socket as connecting.
351 * Start keep-alive timer, and seed output sequence space.
352 * Send initial segment on connection.
355 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
361 struct sockaddr_in *sinp;
362 const int inirw = INI_WRITE;
367 * Must disallow TCP ``connections'' to multicast addresses.
369 sinp = (struct sockaddr_in *)nam;
370 if (sinp->sin_family == AF_INET
371 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
372 error = EAFNOSUPPORT;
376 if (td && jailed(td->td_ucred))
377 prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
379 if ((error = tcp_connect(tp, nam, td)) != 0)
381 error = tcp_output(tp);
382 COMMON_END(PRU_CONNECT);
387 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
393 struct sockaddr_in6 *sin6p;
394 const int inirw = INI_WRITE;
399 * Must disallow TCP ``connections'' to multicast addresses.
401 sin6p = (struct sockaddr_in6 *)nam;
402 if (sin6p->sin6_family == AF_INET6
403 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
404 error = EAFNOSUPPORT;
408 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
409 struct sockaddr_in sin;
411 if (!ip6_mapped_addr_on ||
412 (inp->inp_flags & IN6P_IPV6_V6ONLY))
415 in6_sin6_2_sin(&sin, sin6p);
416 inp->inp_vflag |= INP_IPV4;
417 inp->inp_vflag &= ~INP_IPV6;
418 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
420 error = tcp_output(tp);
423 inp->inp_vflag &= ~INP_IPV4;
424 inp->inp_vflag |= INP_IPV6;
425 inp->inp_inc.inc_isipv6 = 1;
426 if ((error = tcp6_connect(tp, nam, td)) != 0)
428 error = tcp_output(tp);
429 COMMON_END(PRU_CONNECT);
434 * Initiate disconnect from peer.
435 * If connection never passed embryonic stage, just drop;
436 * else if don't need to let data drain, then can just drop anyways,
437 * else have to begin TCP shutdown process: mark socket disconnecting,
438 * drain unread data, state switch to reflect user close, and
439 * send segment (e.g. FIN) to peer. Socket will be really disconnected
440 * when peer sends FIN and acks ours.
442 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
445 tcp_usr_disconnect(struct socket *so)
451 const int inirw = INI_WRITE;
454 tp = tcp_disconnect(tp);
455 COMMON_END(PRU_DISCONNECT);
459 * Accept a connection. Essentially all the work is
460 * done at higher levels; just return the address
461 * of the peer, storing through addr.
464 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
468 struct inpcb *inp = NULL;
469 struct tcpcb *tp = NULL;
470 struct sockaddr_in *sin;
471 const int inirw = INI_READ;
474 if (so->so_state & SS_ISDISCONNECTED) {
475 error = ECONNABORTED;
480 * Do the malloc first in case it blocks.
482 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
484 sin->sin_family = AF_INET;
485 sin->sin_len = sizeof(*sin);
488 INP_INFO_RLOCK(&tcbinfo);
491 INP_INFO_RUNLOCK(&tcbinfo);
497 INP_INFO_RUNLOCK(&tcbinfo);
502 * We inline in_setpeeraddr here, because we have already done
503 * the locking and the malloc.
505 sin->sin_port = inp->inp_fport;
506 sin->sin_addr = inp->inp_faddr;
507 *nam = (struct sockaddr *)sin;
509 COMMON_END(PRU_ACCEPT);
514 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
517 struct inpcb *inp = NULL;
519 struct tcpcb *tp = NULL;
520 const int inirw = INI_READ;
523 if (so->so_state & SS_ISDISCONNECTED) {
524 error = ECONNABORTED;
529 INP_INFO_RLOCK(&tcbinfo);
532 INP_INFO_RUNLOCK(&tcbinfo);
537 INP_INFO_RUNLOCK(&tcbinfo);
540 in6_mapped_peeraddr(so, nam);
541 COMMON_END(PRU_ACCEPT);
546 * This is the wrapper function for in_setsockaddr. We just pass down
547 * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
548 * here because in_setsockaddr will call malloc and can block.
551 tcp_sockaddr(struct socket *so, struct sockaddr **nam)
553 return (in_setsockaddr(so, nam, &tcbinfo));
557 * This is the wrapper function for in_setpeeraddr. We just pass down
558 * the pcbinfo for in_setpeeraddr to lock.
561 tcp_peeraddr(struct socket *so, struct sockaddr **nam)
563 return (in_setpeeraddr(so, nam, &tcbinfo));
567 * Mark the connection as being incapable of further output.
570 tcp_usr_shutdown(struct socket *so)
576 const int inirw = INI_WRITE;
580 tp = tcp_usrclosed(tp);
582 error = tcp_output(tp);
583 COMMON_END(PRU_SHUTDOWN);
587 * After a receive, possibly send window update to peer.
590 tcp_usr_rcvd(struct socket *so, int flags)
596 const int inirw = INI_READ;
600 COMMON_END(PRU_RCVD);
604 * Do a send by putting data in output queue and updating urgent
605 * marker if URG set. Possibly send more data. Unlike the other
606 * pru_*() routines, the mbuf chains are our responsibility. We
607 * must either enqueue them or free them. The other pru_* routines
608 * generally are caller-frees.
611 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
612 struct sockaddr *nam, struct mbuf *control, struct thread *td)
618 const int inirw = INI_WRITE;
625 * Need write lock here because this function might call
626 * tcp_connect or tcp_usrclosed.
627 * We really want to have to this function upgrade from read lock
630 INP_INFO_WLOCK(&tcbinfo);
634 * OOPS! we lost a race, the TCP session got reset after
635 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
636 * network interrupt in the non-splnet() section of sosend().
642 error = ECONNRESET; /* XXX EPIPE? */
649 isipv6 = nam && nam->sa_family == AF_INET6;
654 /* TCP doesn't do control messages (rights, creds, etc) */
655 if (control->m_len) {
662 m_freem(control); /* empty control, just free it */
664 if (!(flags & PRUS_OOB)) {
665 sbappend(&so->so_snd, m);
666 if (nam && tp->t_state < TCPS_SYN_SENT) {
668 * Do implied connect if not yet connected,
669 * initialize window to default value, and
670 * initialize maxseg/maxopd using peer's cached
675 error = tcp6_connect(tp, nam, td);
678 error = tcp_connect(tp, nam, td);
681 tp->snd_wnd = TTCP_CLIENT_SND_WND;
685 if (flags & PRUS_EOF) {
687 * Close the send side of the connection after
691 tp = tcp_usrclosed(tp);
694 if (flags & PRUS_MORETOCOME)
695 tp->t_flags |= TF_MORETOCOME;
696 error = tcp_output(tp);
697 if (flags & PRUS_MORETOCOME)
698 tp->t_flags &= ~TF_MORETOCOME;
701 if (sbspace(&so->so_snd) < -512) {
707 * According to RFC961 (Assigned Protocols),
708 * the urgent pointer points to the last octet
709 * of urgent data. We continue, however,
710 * to consider it to indicate the first octet
711 * of data past the urgent section.
712 * Otherwise, snd_up should be one lower.
714 sbappend(&so->so_snd, m);
715 if (nam && tp->t_state < TCPS_SYN_SENT) {
717 * Do implied connect if not yet connected,
718 * initialize window to default value, and
719 * initialize maxseg/maxopd using peer's cached
724 error = tcp6_connect(tp, nam, td);
727 error = tcp_connect(tp, nam, td);
730 tp->snd_wnd = TTCP_CLIENT_SND_WND;
733 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
735 error = tcp_output(tp);
738 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
739 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
746 tcp_usr_abort(struct socket *so)
752 const int inirw = INI_WRITE;
755 tp = tcp_drop(tp, ECONNABORTED);
756 COMMON_END(PRU_ABORT);
760 * Receive out-of-band data.
763 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
769 const int inirw = INI_READ;
772 if ((so->so_oobmark == 0 &&
773 (so->so_state & SS_RCVATMARK) == 0) ||
774 so->so_options & SO_OOBINLINE ||
775 tp->t_oobflags & TCPOOB_HADDATA) {
779 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
784 *mtod(m, caddr_t) = tp->t_iobc;
785 if ((flags & MSG_PEEK) == 0)
786 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
787 COMMON_END(PRU_RCVOOB);
790 /* xxx - should be const */
791 struct pr_usrreqs tcp_usrreqs = {
792 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
793 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
794 tcp_usr_disconnect, tcp_usr_listen, tcp_peeraddr, tcp_usr_rcvd,
795 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
796 tcp_sockaddr, sosend, soreceive, sopoll
800 struct pr_usrreqs tcp6_usrreqs = {
801 tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
802 tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
803 tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
804 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
805 in6_mapped_sockaddr, sosend, soreceive, sopoll
810 * Common subroutine to open a TCP connection to remote host specified
811 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
812 * port number if needed. Call in_pcbladdr to do the routing and to choose
813 * a local host address (interface). If there is an existing incarnation
814 * of the same connection in TIME-WAIT state and if the remote host was
815 * sending CC options and if the connection duration was < MSL, then
816 * truncate the previous TIME-WAIT state and proceed.
817 * Initialize connection parameters and enter SYN-SENT state.
820 tcp_connect(tp, nam, td)
821 register struct tcpcb *tp;
822 struct sockaddr *nam;
825 struct inpcb *inp = tp->t_inpcb, *oinp;
826 struct socket *so = inp->inp_socket;
828 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
829 struct sockaddr_in *ifaddr;
830 struct rmxp_tao *taop;
831 struct rmxp_tao tao_noncached;
834 if (inp->inp_lport == 0) {
835 error = in_pcbbind(inp, (struct sockaddr *)0, td);
841 * Cannot simply call in_pcbconnect, because there might be an
842 * earlier incarnation of this same connection still in
843 * TIME_WAIT state, creating an ADDRINUSE error.
845 error = in_pcbladdr(inp, nam, &ifaddr);
848 oinp = in_pcblookup_hash(inp->inp_pcbinfo,
849 sin->sin_addr, sin->sin_port,
850 inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
852 inp->inp_lport, 0, NULL);
854 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
855 otp->t_state == TCPS_TIME_WAIT &&
856 (ticks - otp->t_starttime) < tcp_msl &&
857 (otp->t_flags & TF_RCVD_CC))
858 otp = tcp_close(otp);
862 if (inp->inp_laddr.s_addr == INADDR_ANY)
863 inp->inp_laddr = ifaddr->sin_addr;
864 inp->inp_faddr = sin->sin_addr;
865 inp->inp_fport = sin->sin_port;
868 /* Compute window scaling to request. */
869 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
870 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
871 tp->request_r_scale++;
874 tcpstat.tcps_connattempt++;
875 tp->t_state = TCPS_SYN_SENT;
876 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
877 tp->iss = tcp_new_isn(tp);
881 * Generate a CC value for this connection and
882 * check whether CC or CCnew should be used.
884 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
885 taop = &tao_noncached;
886 bzero(taop, sizeof(*taop));
889 tp->cc_send = CC_INC(tcp_ccgen);
890 if (taop->tao_ccsent != 0 &&
891 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
892 taop->tao_ccsent = tp->cc_send;
894 taop->tao_ccsent = 0;
895 tp->t_flags |= TF_SENDCCNEW;
903 tcp6_connect(tp, nam, td)
904 register struct tcpcb *tp;
905 struct sockaddr *nam;
908 struct inpcb *inp = tp->t_inpcb, *oinp;
909 struct socket *so = inp->inp_socket;
911 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
912 struct in6_addr *addr6;
913 struct rmxp_tao *taop;
914 struct rmxp_tao tao_noncached;
917 if (inp->inp_lport == 0) {
918 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
924 * Cannot simply call in_pcbconnect, because there might be an
925 * earlier incarnation of this same connection still in
926 * TIME_WAIT state, creating an ADDRINUSE error.
928 error = in6_pcbladdr(inp, nam, &addr6);
931 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
932 &sin6->sin6_addr, sin6->sin6_port,
933 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
936 inp->inp_lport, 0, NULL);
938 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
939 otp->t_state == TCPS_TIME_WAIT &&
940 (ticks - otp->t_starttime) < tcp_msl &&
941 (otp->t_flags & TF_RCVD_CC))
942 otp = tcp_close(otp);
946 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
947 inp->in6p_laddr = *addr6;
948 inp->in6p_faddr = sin6->sin6_addr;
949 inp->inp_fport = sin6->sin6_port;
950 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
951 inp->in6p_flowinfo = sin6->sin6_flowinfo;
954 /* Compute window scaling to request. */
955 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
956 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
957 tp->request_r_scale++;
960 tcpstat.tcps_connattempt++;
961 tp->t_state = TCPS_SYN_SENT;
962 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
963 tp->iss = tcp_new_isn(tp);
967 * Generate a CC value for this connection and
968 * check whether CC or CCnew should be used.
970 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
971 taop = &tao_noncached;
972 bzero(taop, sizeof(*taop));
975 tp->cc_send = CC_INC(tcp_ccgen);
976 if (taop->tao_ccsent != 0 &&
977 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
978 taop->tao_ccsent = tp->cc_send;
980 taop->tao_ccsent = 0;
981 tp->t_flags |= TF_SENDCCNEW;
989 * The new sockopt interface makes it possible for us to block in the
990 * copyin/out step (if we take a page fault). Taking a page fault at
991 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
992 * use TSM, there probably isn't any need for this function to run at
993 * splnet() any more. This needs more examination.)
996 tcp_ctloutput(so, sopt)
998 struct sockopt *sopt;
1000 int error, opt, optval, s;
1005 s = splnet(); /* XXX */
1006 INP_INFO_RLOCK(&tcbinfo);
1007 inp = sotoinpcb(so);
1009 INP_INFO_RUNLOCK(&tcbinfo);
1011 return (ECONNRESET);
1014 INP_INFO_RUNLOCK(&tcbinfo);
1015 if (sopt->sopt_level != IPPROTO_TCP) {
1017 if (INP_CHECK_SOCKAF(so, AF_INET6))
1018 error = ip6_ctloutput(so, sopt);
1021 error = ip_ctloutput(so, sopt);
1026 tp = intotcpcb(inp);
1028 switch (sopt->sopt_dir) {
1030 switch (sopt->sopt_name) {
1033 error = sooptcopyin(sopt, &optval, sizeof optval,
1038 switch (sopt->sopt_name) {
1046 opt = 0; /* dead code to fool gcc */
1053 tp->t_flags &= ~opt;
1057 error = sooptcopyin(sopt, &optval, sizeof optval,
1063 tp->t_flags |= TF_NOPUSH;
1065 tp->t_flags &= ~TF_NOPUSH;
1066 error = tcp_output(tp);
1071 error = sooptcopyin(sopt, &optval, sizeof optval,
1076 if (optval > 0 && optval <= tp->t_maxseg)
1077 tp->t_maxseg = optval;
1083 error = ENOPROTOOPT;
1089 switch (sopt->sopt_name) {
1091 optval = tp->t_flags & TF_NODELAY;
1094 optval = tp->t_maxseg;
1097 optval = tp->t_flags & TF_NOOPT;
1100 optval = tp->t_flags & TF_NOPUSH;
1103 error = ENOPROTOOPT;
1107 error = sooptcopyout(sopt, &optval, sizeof optval);
1116 * tcp_sendspace and tcp_recvspace are the default send and receive window
1117 * sizes, respectively. These are obsolescent (this information should
1118 * be set by the route).
1120 u_long tcp_sendspace = 1024*32;
1121 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1122 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1123 u_long tcp_recvspace = 1024*64;
1124 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1125 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1128 * Attach TCP protocol to socket, allocating
1129 * internet protocol control block, tcp control block,
1130 * bufer space, and entering LISTEN state if to accept connections.
1137 register struct tcpcb *tp;
1141 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1144 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1145 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1149 error = in_pcballoc(so, &tcbinfo, td);
1152 inp = sotoinpcb(so);
1155 inp->inp_vflag |= INP_IPV6;
1156 inp->in6p_hops = -1; /* use kernel default */
1160 inp->inp_vflag |= INP_IPV4;
1161 tp = tcp_newtcpcb(inp);
1163 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1165 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1172 so->so_state |= nofd;
1175 tp->t_state = TCPS_CLOSED;
1180 * Initiate (or continue) disconnect.
1181 * If embryonic state, just send reset (once).
1182 * If in ``let data drain'' option and linger null, just drop.
1183 * Otherwise (hard), mark socket disconnecting and drop
1184 * current input data; switch states based on user close, and
1185 * send segment to peer (with FIN).
1187 static struct tcpcb *
1189 register struct tcpcb *tp;
1191 struct socket *so = tp->t_inpcb->inp_socket;
1193 if (tp->t_state < TCPS_ESTABLISHED)
1195 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1196 tp = tcp_drop(tp, 0);
1198 soisdisconnecting(so);
1199 sbflush(&so->so_rcv);
1200 tp = tcp_usrclosed(tp);
1202 (void) tcp_output(tp);
1208 * User issued close, and wish to trail through shutdown states:
1209 * if never received SYN, just forget it. If got a SYN from peer,
1210 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1211 * If already got a FIN from peer, then almost done; go to LAST_ACK
1212 * state. In all other cases, have already sent FIN to peer (e.g.
1213 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1214 * for peer to send FIN or not respond to keep-alives, etc.
1215 * We can let the user exit from the close as soon as the FIN is acked.
1217 static struct tcpcb *
1219 register struct tcpcb *tp;
1222 switch (tp->t_state) {
1226 tp->t_state = TCPS_CLOSED;
1231 case TCPS_SYN_RECEIVED:
1232 tp->t_flags |= TF_NEEDFIN;
1235 case TCPS_ESTABLISHED:
1236 tp->t_state = TCPS_FIN_WAIT_1;
1239 case TCPS_CLOSE_WAIT:
1240 tp->t_state = TCPS_LAST_ACK;
1243 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1244 soisdisconnected(tp->t_inpcb->inp_socket);
1245 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1246 if (tp->t_state == TCPS_FIN_WAIT_2)
1247 callout_reset(tp->tt_2msl, tcp_maxidle,
1248 tcp_timer_2msl, tp);