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 ((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 ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
337 inp->inp_vflag |= INP_IPV4;
338 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
341 tp->t_state = TCPS_LISTEN;
342 COMMON_END(PRU_LISTEN);
347 * Initiate connection to peer.
348 * Create a template for use in transmissions on this connection.
349 * Enter SYN_SENT state, and mark socket as connecting.
350 * Start keep-alive timer, and seed output sequence space.
351 * Send initial segment on connection.
354 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
360 struct sockaddr_in *sinp;
361 const int inirw = INI_WRITE;
366 * Must disallow TCP ``connections'' to multicast addresses.
368 sinp = (struct sockaddr_in *)nam;
369 if (sinp->sin_family == AF_INET
370 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
371 error = EAFNOSUPPORT;
375 if (td && jailed(td->td_ucred))
376 prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
378 if ((error = tcp_connect(tp, nam, td)) != 0)
380 error = tcp_output(tp);
381 COMMON_END(PRU_CONNECT);
386 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
392 struct sockaddr_in6 *sin6p;
393 const int inirw = INI_WRITE;
398 * Must disallow TCP ``connections'' to multicast addresses.
400 sin6p = (struct sockaddr_in6 *)nam;
401 if (sin6p->sin6_family == AF_INET6
402 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
403 error = EAFNOSUPPORT;
407 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
408 struct sockaddr_in sin;
410 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
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;
474 if (so->so_state & SS_ISDISCONNECTED) {
475 error = ECONNABORTED;
480 INP_INFO_RLOCK(&tcbinfo);
483 INP_INFO_RUNLOCK(&tcbinfo);
488 INP_INFO_RUNLOCK(&tcbinfo);
493 * We inline in_setpeeraddr and COMMON_END here, so that we can
494 * copy the data of interest and defer the malloc until after we
497 port = inp->inp_fport;
498 addr = inp->inp_faddr;
500 out: TCPDEBUG2(PRU_ACCEPT);
505 *nam = in_sockaddr(port, &addr);
511 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
514 struct inpcb *inp = NULL;
516 struct tcpcb *tp = NULL;
518 struct in6_addr addr6;
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);
541 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
542 * copy the data of interest and defer the malloc until after we
545 if (inp->inp_vflag & INP_IPV4) {
547 port = inp->inp_fport;
548 addr = inp->inp_faddr;
550 port = inp->inp_fport;
551 addr6 = inp->in6p_faddr;
554 out: TCPDEBUG2(PRU_ACCEPT);
560 *nam = in6_v4mapsin6_sockaddr(port, &addr);
562 *nam = in6_sockaddr(port, &addr6);
569 * This is the wrapper function for in_setsockaddr. We just pass down
570 * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
571 * here because in_setsockaddr will call malloc and can block.
574 tcp_sockaddr(struct socket *so, struct sockaddr **nam)
576 return (in_setsockaddr(so, nam, &tcbinfo));
580 * This is the wrapper function for in_setpeeraddr. We just pass down
581 * the pcbinfo for in_setpeeraddr to lock.
584 tcp_peeraddr(struct socket *so, struct sockaddr **nam)
586 return (in_setpeeraddr(so, nam, &tcbinfo));
590 * Mark the connection as being incapable of further output.
593 tcp_usr_shutdown(struct socket *so)
599 const int inirw = INI_WRITE;
603 tp = tcp_usrclosed(tp);
605 error = tcp_output(tp);
606 COMMON_END(PRU_SHUTDOWN);
610 * After a receive, possibly send window update to peer.
613 tcp_usr_rcvd(struct socket *so, int flags)
619 const int inirw = INI_READ;
623 COMMON_END(PRU_RCVD);
627 * Do a send by putting data in output queue and updating urgent
628 * marker if URG set. Possibly send more data. Unlike the other
629 * pru_*() routines, the mbuf chains are our responsibility. We
630 * must either enqueue them or free them. The other pru_* routines
631 * generally are caller-frees.
634 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
635 struct sockaddr *nam, struct mbuf *control, struct thread *td)
641 const int inirw = INI_WRITE;
648 * Need write lock here because this function might call
649 * tcp_connect or tcp_usrclosed.
650 * We really want to have to this function upgrade from read lock
653 INP_INFO_WLOCK(&tcbinfo);
657 * OOPS! we lost a race, the TCP session got reset after
658 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
659 * network interrupt in the non-splnet() section of sosend().
665 error = ECONNRESET; /* XXX EPIPE? */
672 isipv6 = nam && nam->sa_family == AF_INET6;
677 /* TCP doesn't do control messages (rights, creds, etc) */
678 if (control->m_len) {
685 m_freem(control); /* empty control, just free it */
687 if (!(flags & PRUS_OOB)) {
688 sbappend(&so->so_snd, m);
689 if (nam && tp->t_state < TCPS_SYN_SENT) {
691 * Do implied connect if not yet connected,
692 * initialize window to default value, and
693 * initialize maxseg/maxopd using peer's cached
698 error = tcp6_connect(tp, nam, td);
701 error = tcp_connect(tp, nam, td);
704 tp->snd_wnd = TTCP_CLIENT_SND_WND;
708 if (flags & PRUS_EOF) {
710 * Close the send side of the connection after
714 tp = tcp_usrclosed(tp);
717 if (flags & PRUS_MORETOCOME)
718 tp->t_flags |= TF_MORETOCOME;
719 error = tcp_output(tp);
720 if (flags & PRUS_MORETOCOME)
721 tp->t_flags &= ~TF_MORETOCOME;
724 if (sbspace(&so->so_snd) < -512) {
730 * According to RFC961 (Assigned Protocols),
731 * the urgent pointer points to the last octet
732 * of urgent data. We continue, however,
733 * to consider it to indicate the first octet
734 * of data past the urgent section.
735 * Otherwise, snd_up should be one lower.
737 sbappend(&so->so_snd, m);
738 if (nam && tp->t_state < TCPS_SYN_SENT) {
740 * Do implied connect if not yet connected,
741 * initialize window to default value, and
742 * initialize maxseg/maxopd using peer's cached
747 error = tcp6_connect(tp, nam, td);
750 error = tcp_connect(tp, nam, td);
753 tp->snd_wnd = TTCP_CLIENT_SND_WND;
756 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
758 error = tcp_output(tp);
761 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
762 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
769 tcp_usr_abort(struct socket *so)
775 const int inirw = INI_WRITE;
778 tp = tcp_drop(tp, ECONNABORTED);
779 COMMON_END(PRU_ABORT);
783 * Receive out-of-band data.
786 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
792 const int inirw = INI_READ;
795 if ((so->so_oobmark == 0 &&
796 (so->so_state & SS_RCVATMARK) == 0) ||
797 so->so_options & SO_OOBINLINE ||
798 tp->t_oobflags & TCPOOB_HADDATA) {
802 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
807 *mtod(m, caddr_t) = tp->t_iobc;
808 if ((flags & MSG_PEEK) == 0)
809 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
810 COMMON_END(PRU_RCVOOB);
813 /* xxx - should be const */
814 struct pr_usrreqs tcp_usrreqs = {
815 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
816 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
817 tcp_usr_disconnect, tcp_usr_listen, tcp_peeraddr, tcp_usr_rcvd,
818 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
819 tcp_sockaddr, sosend, soreceive, sopoll
823 struct pr_usrreqs tcp6_usrreqs = {
824 tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
825 tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
826 tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
827 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
828 in6_mapped_sockaddr, sosend, soreceive, sopoll
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_pcbladdr to do the routing and to choose
836 * a local host address (interface). If there is an existing incarnation
837 * of the same connection in TIME-WAIT state and if the remote host was
838 * 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.
843 tcp_connect(tp, nam, td)
844 register struct tcpcb *tp;
845 struct sockaddr *nam;
848 struct inpcb *inp = tp->t_inpcb, *oinp;
849 struct socket *so = inp->inp_socket;
851 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
852 struct sockaddr_in *ifaddr;
853 struct rmxp_tao *taop;
854 struct rmxp_tao tao_noncached;
857 if (inp->inp_lport == 0) {
858 error = in_pcbbind(inp, (struct sockaddr *)0, td);
864 * Cannot simply call in_pcbconnect, because there might be an
865 * earlier incarnation of this same connection still in
866 * TIME_WAIT state, creating an ADDRINUSE error.
868 error = in_pcbladdr(inp, nam, &ifaddr);
871 oinp = in_pcblookup_hash(inp->inp_pcbinfo,
872 sin->sin_addr, sin->sin_port,
873 inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
875 inp->inp_lport, 0, NULL);
877 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
878 otp->t_state == TCPS_TIME_WAIT &&
879 (ticks - otp->t_starttime) < tcp_msl &&
880 (otp->t_flags & TF_RCVD_CC))
881 otp = tcp_close(otp);
885 if (inp->inp_laddr.s_addr == INADDR_ANY)
886 inp->inp_laddr = ifaddr->sin_addr;
887 inp->inp_faddr = sin->sin_addr;
888 inp->inp_fport = sin->sin_port;
891 /* Compute window scaling to request. */
892 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
893 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
894 tp->request_r_scale++;
897 tcpstat.tcps_connattempt++;
898 tp->t_state = TCPS_SYN_SENT;
899 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
900 tp->iss = tcp_new_isn(tp);
901 tp->t_bw_rtseq = tp->iss;
905 * Generate a CC value for this connection and
906 * check whether CC or CCnew should be used.
908 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
909 taop = &tao_noncached;
910 bzero(taop, sizeof(*taop));
913 tp->cc_send = CC_INC(tcp_ccgen);
914 if (taop->tao_ccsent != 0 &&
915 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
916 taop->tao_ccsent = tp->cc_send;
918 taop->tao_ccsent = 0;
919 tp->t_flags |= TF_SENDCCNEW;
927 tcp6_connect(tp, nam, td)
928 register struct tcpcb *tp;
929 struct sockaddr *nam;
932 struct inpcb *inp = tp->t_inpcb, *oinp;
933 struct socket *so = inp->inp_socket;
935 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
936 struct in6_addr *addr6;
937 struct rmxp_tao *taop;
938 struct rmxp_tao tao_noncached;
941 if (inp->inp_lport == 0) {
942 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
948 * Cannot simply call in_pcbconnect, because there might be an
949 * earlier incarnation of this same connection still in
950 * TIME_WAIT state, creating an ADDRINUSE error.
952 error = in6_pcbladdr(inp, nam, &addr6);
955 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
956 &sin6->sin6_addr, sin6->sin6_port,
957 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
960 inp->inp_lport, 0, NULL);
962 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
963 otp->t_state == TCPS_TIME_WAIT &&
964 (ticks - otp->t_starttime) < tcp_msl &&
965 (otp->t_flags & TF_RCVD_CC))
966 otp = tcp_close(otp);
970 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
971 inp->in6p_laddr = *addr6;
972 inp->in6p_faddr = sin6->sin6_addr;
973 inp->inp_fport = sin6->sin6_port;
974 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != 0)
975 inp->in6p_flowinfo = sin6->sin6_flowinfo;
978 /* Compute window scaling to request. */
979 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
980 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
981 tp->request_r_scale++;
984 tcpstat.tcps_connattempt++;
985 tp->t_state = TCPS_SYN_SENT;
986 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
987 tp->iss = tcp_new_isn(tp);
988 tp->t_bw_rtseq = tp->iss;
992 * Generate a CC value for this connection and
993 * check whether CC or CCnew should be used.
995 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
996 taop = &tao_noncached;
997 bzero(taop, sizeof(*taop));
1000 tp->cc_send = CC_INC(tcp_ccgen);
1001 if (taop->tao_ccsent != 0 &&
1002 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
1003 taop->tao_ccsent = tp->cc_send;
1005 taop->tao_ccsent = 0;
1006 tp->t_flags |= TF_SENDCCNEW;
1014 * The new sockopt interface makes it possible for us to block in the
1015 * copyin/out step (if we take a page fault). Taking a page fault at
1016 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
1017 * use TSM, there probably isn't any need for this function to run at
1018 * splnet() any more. This needs more examination.)
1021 tcp_ctloutput(so, sopt)
1023 struct sockopt *sopt;
1025 int error, opt, optval, s;
1030 s = splnet(); /* XXX */
1031 INP_INFO_RLOCK(&tcbinfo);
1032 inp = sotoinpcb(so);
1034 INP_INFO_RUNLOCK(&tcbinfo);
1036 return (ECONNRESET);
1039 INP_INFO_RUNLOCK(&tcbinfo);
1040 if (sopt->sopt_level != IPPROTO_TCP) {
1042 if (INP_CHECK_SOCKAF(so, AF_INET6))
1043 error = ip6_ctloutput(so, sopt);
1046 error = ip_ctloutput(so, sopt);
1051 tp = intotcpcb(inp);
1053 switch (sopt->sopt_dir) {
1055 switch (sopt->sopt_name) {
1058 error = sooptcopyin(sopt, &optval, sizeof optval,
1063 switch (sopt->sopt_name) {
1071 opt = 0; /* dead code to fool gcc */
1078 tp->t_flags &= ~opt;
1082 error = sooptcopyin(sopt, &optval, sizeof optval,
1088 tp->t_flags |= TF_NOPUSH;
1090 tp->t_flags &= ~TF_NOPUSH;
1091 error = tcp_output(tp);
1096 error = sooptcopyin(sopt, &optval, sizeof optval,
1101 if (optval > 0 && optval <= tp->t_maxseg)
1102 tp->t_maxseg = optval;
1108 error = ENOPROTOOPT;
1114 switch (sopt->sopt_name) {
1116 optval = tp->t_flags & TF_NODELAY;
1119 optval = tp->t_maxseg;
1122 optval = tp->t_flags & TF_NOOPT;
1125 optval = tp->t_flags & TF_NOPUSH;
1128 error = ENOPROTOOPT;
1132 error = sooptcopyout(sopt, &optval, sizeof optval);
1141 * tcp_sendspace and tcp_recvspace are the default send and receive window
1142 * sizes, respectively. These are obsolescent (this information should
1143 * be set by the route).
1145 u_long tcp_sendspace = 1024*32;
1146 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1147 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1148 u_long tcp_recvspace = 1024*64;
1149 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1150 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1153 * Attach TCP protocol to socket, allocating
1154 * internet protocol control block, tcp control block,
1155 * bufer space, and entering LISTEN state if to accept connections.
1162 register struct tcpcb *tp;
1166 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1169 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1170 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1174 error = in_pcballoc(so, &tcbinfo, td);
1177 inp = sotoinpcb(so);
1180 inp->inp_vflag |= INP_IPV6;
1181 inp->in6p_hops = -1; /* use kernel default */
1185 inp->inp_vflag |= INP_IPV4;
1186 tp = tcp_newtcpcb(inp);
1188 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1190 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1197 so->so_state |= nofd;
1200 tp->t_state = TCPS_CLOSED;
1205 * Initiate (or continue) disconnect.
1206 * If embryonic state, just send reset (once).
1207 * If in ``let data drain'' option and linger null, just drop.
1208 * Otherwise (hard), mark socket disconnecting and drop
1209 * current input data; switch states based on user close, and
1210 * send segment to peer (with FIN).
1212 static struct tcpcb *
1214 register struct tcpcb *tp;
1216 struct socket *so = tp->t_inpcb->inp_socket;
1218 if (tp->t_state < TCPS_ESTABLISHED)
1220 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1221 tp = tcp_drop(tp, 0);
1223 soisdisconnecting(so);
1224 sbflush(&so->so_rcv);
1225 tp = tcp_usrclosed(tp);
1227 (void) tcp_output(tp);
1233 * User issued close, and wish to trail through shutdown states:
1234 * if never received SYN, just forget it. If got a SYN from peer,
1235 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1236 * If already got a FIN from peer, then almost done; go to LAST_ACK
1237 * state. In all other cases, have already sent FIN to peer (e.g.
1238 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1239 * for peer to send FIN or not respond to keep-alives, etc.
1240 * We can let the user exit from the close as soon as the FIN is acked.
1242 static struct tcpcb *
1244 register struct tcpcb *tp;
1247 switch (tp->t_state) {
1251 tp->t_state = TCPS_CLOSED;
1256 case TCPS_SYN_RECEIVED:
1257 tp->t_flags |= TF_NEEDFIN;
1260 case TCPS_ESTABLISHED:
1261 tp->t_state = TCPS_FIN_WAIT_1;
1264 case TCPS_CLOSE_WAIT:
1265 tp->t_state = TCPS_LAST_ACK;
1268 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1269 soisdisconnected(tp->t_inpcb->inp_socket);
1270 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1271 if (tp->t_state == TCPS_FIN_WAIT_2)
1272 callout_reset(tp->tt_2msl, tcp_maxidle,
1273 tcp_timer_2msl, tp);