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
34 * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.51.2.17 2002/10/11 11:46:44 ume Exp $
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/kernel.h>
44 #include <sys/sysctl.h>
47 #include <sys/domain.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/protosw.h>
54 #include <net/route.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
59 #include <netinet/ip6.h>
61 #include <netinet/in_pcb.h>
63 #include <netinet6/in6_pcb.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip_var.h>
68 #include <netinet6/ip6_var.h>
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>
77 #include <netinet/tcp_debug.h>
81 #include <netinet6/ipsec.h>
85 * TCP protocol interface to socket abstraction.
87 extern char *tcpstates[]; /* XXX ??? */
89 static int tcp_attach __P((struct socket *, struct proc *));
90 static int tcp_connect __P((struct tcpcb *, struct sockaddr *,
93 static int tcp6_connect __P((struct tcpcb *, struct sockaddr *,
97 tcp_disconnect __P((struct tcpcb *));
99 tcp_usrclosed __P((struct tcpcb *));
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)
109 #define TCPDEBUG2(req)
113 * TCP attaches to socket via pru_attach(), reserving space,
114 * and an internet control block.
117 tcp_usr_attach(struct socket *so, int proto, struct proc *p)
121 struct inpcb *inp = sotoinpcb(so);
122 struct tcpcb *tp = 0;
131 error = tcp_attach(so, p);
135 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
136 so->so_linger = TCP_LINGERTIME;
139 TCPDEBUG2(PRU_ATTACH);
145 * pru_detach() detaches the TCP protocol from the socket.
146 * If the protocol state is non-embryonic, then can't
147 * do this directly: have to initiate a pru_disconnect(),
148 * which may finish later; embryonic TCB's can just
152 tcp_usr_detach(struct socket *so)
156 struct inpcb *inp = sotoinpcb(so);
162 return EINVAL; /* XXX */
166 tp = tcp_disconnect(tp);
168 TCPDEBUG2(PRU_DETACH);
173 #define COMMON_START() TCPDEBUG0; \
179 tp = intotcpcb(inp); \
183 #define COMMON_END(req) out: TCPDEBUG2(req); splx(s); return error; goto out
187 * Give the socket an address.
190 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
194 struct inpcb *inp = sotoinpcb(so);
196 struct sockaddr_in *sinp;
201 * Must check for multicast addresses and disallow binding
204 sinp = (struct sockaddr_in *)nam;
205 if (sinp->sin_family == AF_INET &&
206 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
207 error = EAFNOSUPPORT;
210 error = in_pcbbind(inp, nam, p);
213 COMMON_END(PRU_BIND);
219 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
223 struct inpcb *inp = sotoinpcb(so);
225 struct sockaddr_in6 *sin6p;
230 * Must check for multicast addresses and disallow binding
233 sin6p = (struct sockaddr_in6 *)nam;
234 if (sin6p->sin6_family == AF_INET6 &&
235 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
236 error = EAFNOSUPPORT;
239 inp->inp_vflag &= ~INP_IPV4;
240 inp->inp_vflag |= INP_IPV6;
241 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
242 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
243 inp->inp_vflag |= INP_IPV4;
244 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
245 struct sockaddr_in sin;
247 in6_sin6_2_sin(&sin, sin6p);
248 inp->inp_vflag |= INP_IPV4;
249 inp->inp_vflag &= ~INP_IPV6;
250 error = in_pcbbind(inp, (struct sockaddr *)&sin, p);
254 error = in6_pcbbind(inp, nam, p);
257 COMMON_END(PRU_BIND);
262 * Prepare to accept connections.
265 tcp_usr_listen(struct socket *so, struct proc *p)
269 struct inpcb *inp = sotoinpcb(so);
273 if (inp->inp_lport == 0)
274 error = in_pcbbind(inp, (struct sockaddr *)0, p);
276 tp->t_state = TCPS_LISTEN;
277 COMMON_END(PRU_LISTEN);
282 tcp6_usr_listen(struct socket *so, struct proc *p)
286 struct inpcb *inp = sotoinpcb(so);
290 if (inp->inp_lport == 0) {
291 inp->inp_vflag &= ~INP_IPV4;
292 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
293 inp->inp_vflag |= INP_IPV4;
294 error = in6_pcbbind(inp, (struct sockaddr *)0, p);
297 tp->t_state = TCPS_LISTEN;
298 COMMON_END(PRU_LISTEN);
303 * Initiate connection to peer.
304 * Create a template for use in transmissions on this connection.
305 * Enter SYN_SENT state, and mark socket as connecting.
306 * Start keep-alive timer, and seed output sequence space.
307 * Send initial segment on connection.
310 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
314 struct inpcb *inp = sotoinpcb(so);
316 struct sockaddr_in *sinp;
321 * Must disallow TCP ``connections'' to multicast addresses.
323 sinp = (struct sockaddr_in *)nam;
324 if (sinp->sin_family == AF_INET
325 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
326 error = EAFNOSUPPORT;
330 prison_remote_ip(p, 0, &sinp->sin_addr.s_addr);
332 if ((error = tcp_connect(tp, nam, p)) != 0)
334 error = tcp_output(tp);
335 COMMON_END(PRU_CONNECT);
340 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
344 struct inpcb *inp = sotoinpcb(so);
346 struct sockaddr_in6 *sin6p;
351 * Must disallow TCP ``connections'' to multicast addresses.
353 sin6p = (struct sockaddr_in6 *)nam;
354 if (sin6p->sin6_family == AF_INET6
355 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
356 error = EAFNOSUPPORT;
360 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
361 struct sockaddr_in sin;
363 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
368 in6_sin6_2_sin(&sin, sin6p);
369 inp->inp_vflag |= INP_IPV4;
370 inp->inp_vflag &= ~INP_IPV6;
371 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, p)) != 0)
373 error = tcp_output(tp);
376 inp->inp_vflag &= ~INP_IPV4;
377 inp->inp_vflag |= INP_IPV6;
378 inp->inp_inc.inc_isipv6 = 1;
379 if ((error = tcp6_connect(tp, nam, p)) != 0)
381 error = tcp_output(tp);
382 COMMON_END(PRU_CONNECT);
387 * Initiate disconnect from peer.
388 * If connection never passed embryonic stage, just drop;
389 * else if don't need to let data drain, then can just drop anyways,
390 * else have to begin TCP shutdown process: mark socket disconnecting,
391 * drain unread data, state switch to reflect user close, and
392 * send segment (e.g. FIN) to peer. Socket will be really disconnected
393 * when peer sends FIN and acks ours.
395 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
398 tcp_usr_disconnect(struct socket *so)
402 struct inpcb *inp = sotoinpcb(so);
406 tp = tcp_disconnect(tp);
407 COMMON_END(PRU_DISCONNECT);
411 * Accept a connection. Essentially all the work is
412 * done at higher levels; just return the address
413 * of the peer, storing through addr.
416 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
420 struct inpcb *inp = sotoinpcb(so);
421 struct tcpcb *tp = NULL;
424 if (so->so_state & SS_ISDISCONNECTED) {
425 error = ECONNABORTED;
434 in_setpeeraddr(so, nam);
435 COMMON_END(PRU_ACCEPT);
440 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
444 struct inpcb *inp = sotoinpcb(so);
445 struct tcpcb *tp = NULL;
448 if (so->so_state & SS_ISDISCONNECTED) {
449 error = ECONNABORTED;
458 in6_mapped_peeraddr(so, nam);
459 COMMON_END(PRU_ACCEPT);
463 * Mark the connection as being incapable of further output.
466 tcp_usr_shutdown(struct socket *so)
470 struct inpcb *inp = sotoinpcb(so);
475 tp = tcp_usrclosed(tp);
477 error = tcp_output(tp);
478 COMMON_END(PRU_SHUTDOWN);
482 * After a receive, possibly send window update to peer.
485 tcp_usr_rcvd(struct socket *so, int flags)
489 struct inpcb *inp = sotoinpcb(so);
494 COMMON_END(PRU_RCVD);
498 * Do a send by putting data in output queue and updating urgent
499 * marker if URG set. Possibly send more data. Unlike the other
500 * pru_*() routines, the mbuf chains are our responsibility. We
501 * must either enqueue them or free them. The other pru_* routines
502 * generally are caller-frees.
505 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
506 struct sockaddr *nam, struct mbuf *control, struct proc *p)
510 struct inpcb *inp = sotoinpcb(so);
519 * OOPS! we lost a race, the TCP session got reset after
520 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
521 * network interrupt in the non-splnet() section of sosend().
527 error = ECONNRESET; /* XXX EPIPE? */
533 isipv6 = nam && nam->sa_family == AF_INET6;
538 /* TCP doesn't do control messages (rights, creds, etc) */
539 if (control->m_len) {
546 m_freem(control); /* empty control, just free it */
548 if(!(flags & PRUS_OOB)) {
549 sbappend(&so->so_snd, m);
550 if (nam && tp->t_state < TCPS_SYN_SENT) {
552 * Do implied connect if not yet connected,
553 * initialize window to default value, and
554 * initialize maxseg/maxopd using peer's cached
559 error = tcp6_connect(tp, nam, p);
562 error = tcp_connect(tp, nam, p);
565 tp->snd_wnd = TTCP_CLIENT_SND_WND;
569 if (flags & PRUS_EOF) {
571 * Close the send side of the connection after
575 tp = tcp_usrclosed(tp);
578 if (flags & PRUS_MORETOCOME)
579 tp->t_flags |= TF_MORETOCOME;
580 error = tcp_output(tp);
581 if (flags & PRUS_MORETOCOME)
582 tp->t_flags &= ~TF_MORETOCOME;
585 if (sbspace(&so->so_snd) < -512) {
591 * According to RFC961 (Assigned Protocols),
592 * the urgent pointer points to the last octet
593 * of urgent data. We continue, however,
594 * to consider it to indicate the first octet
595 * of data past the urgent section.
596 * Otherwise, snd_up should be one lower.
598 sbappend(&so->so_snd, m);
599 if (nam && tp->t_state < TCPS_SYN_SENT) {
601 * Do implied connect if not yet connected,
602 * initialize window to default value, and
603 * initialize maxseg/maxopd using peer's cached
608 error = tcp6_connect(tp, nam, p);
611 error = tcp_connect(tp, nam, p);
614 tp->snd_wnd = TTCP_CLIENT_SND_WND;
617 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
619 error = tcp_output(tp);
622 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
623 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
630 tcp_usr_abort(struct socket *so)
634 struct inpcb *inp = sotoinpcb(so);
638 tp = tcp_drop(tp, ECONNABORTED);
639 COMMON_END(PRU_ABORT);
643 * Receive out-of-band data.
646 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
650 struct inpcb *inp = sotoinpcb(so);
654 if ((so->so_oobmark == 0 &&
655 (so->so_state & SS_RCVATMARK) == 0) ||
656 so->so_options & SO_OOBINLINE ||
657 tp->t_oobflags & TCPOOB_HADDATA) {
661 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
666 *mtod(m, caddr_t) = tp->t_iobc;
667 if ((flags & MSG_PEEK) == 0)
668 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
669 COMMON_END(PRU_RCVOOB);
672 /* xxx - should be const */
673 struct pr_usrreqs tcp_usrreqs = {
674 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
675 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
676 tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
677 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
678 in_setsockaddr, sosend, soreceive, sopoll
682 struct pr_usrreqs tcp6_usrreqs = {
683 tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
684 tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
685 tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
686 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
687 in6_mapped_sockaddr, sosend, soreceive, sopoll
692 * Common subroutine to open a TCP connection to remote host specified
693 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
694 * port number if needed. Call in_pcbladdr to do the routing and to choose
695 * a local host address (interface). If there is an existing incarnation
696 * of the same connection in TIME-WAIT state and if the remote host was
697 * sending CC options and if the connection duration was < MSL, then
698 * truncate the previous TIME-WAIT state and proceed.
699 * Initialize connection parameters and enter SYN-SENT state.
702 tcp_connect(tp, nam, p)
703 register struct tcpcb *tp;
704 struct sockaddr *nam;
707 struct inpcb *inp = tp->t_inpcb, *oinp;
708 struct socket *so = inp->inp_socket;
710 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
711 struct sockaddr_in *ifaddr;
712 struct rmxp_tao *taop;
713 struct rmxp_tao tao_noncached;
716 if (inp->inp_lport == 0) {
717 error = in_pcbbind(inp, (struct sockaddr *)0, p);
723 * Cannot simply call in_pcbconnect, because there might be an
724 * earlier incarnation of this same connection still in
725 * TIME_WAIT state, creating an ADDRINUSE error.
727 error = in_pcbladdr(inp, nam, &ifaddr);
730 oinp = in_pcblookup_hash(inp->inp_pcbinfo,
731 sin->sin_addr, sin->sin_port,
732 inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
734 inp->inp_lport, 0, NULL);
736 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
737 otp->t_state == TCPS_TIME_WAIT &&
738 (ticks - otp->t_starttime) < tcp_msl &&
739 (otp->t_flags & TF_RCVD_CC))
740 otp = tcp_close(otp);
744 if (inp->inp_laddr.s_addr == INADDR_ANY)
745 inp->inp_laddr = ifaddr->sin_addr;
746 inp->inp_faddr = sin->sin_addr;
747 inp->inp_fport = sin->sin_port;
750 /* Compute window scaling to request. */
751 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
752 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
753 tp->request_r_scale++;
756 tcpstat.tcps_connattempt++;
757 tp->t_state = TCPS_SYN_SENT;
758 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
759 tp->iss = tcp_new_isn(tp);
760 tp->t_bw_rtseq = tp->iss;
764 * Generate a CC value for this connection and
765 * check whether CC or CCnew should be used.
767 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
768 taop = &tao_noncached;
769 bzero(taop, sizeof(*taop));
772 tp->cc_send = CC_INC(tcp_ccgen);
773 if (taop->tao_ccsent != 0 &&
774 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
775 taop->tao_ccsent = tp->cc_send;
777 taop->tao_ccsent = 0;
778 tp->t_flags |= TF_SENDCCNEW;
786 tcp6_connect(tp, nam, p)
787 register struct tcpcb *tp;
788 struct sockaddr *nam;
791 struct inpcb *inp = tp->t_inpcb, *oinp;
792 struct socket *so = inp->inp_socket;
794 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
795 struct in6_addr *addr6;
796 struct rmxp_tao *taop;
797 struct rmxp_tao tao_noncached;
800 if (inp->inp_lport == 0) {
801 error = in6_pcbbind(inp, (struct sockaddr *)0, p);
807 * Cannot simply call in_pcbconnect, because there might be an
808 * earlier incarnation of this same connection still in
809 * TIME_WAIT state, creating an ADDRINUSE error.
811 error = in6_pcbladdr(inp, nam, &addr6);
814 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
815 &sin6->sin6_addr, sin6->sin6_port,
816 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
819 inp->inp_lport, 0, NULL);
821 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
822 otp->t_state == TCPS_TIME_WAIT &&
823 (ticks - otp->t_starttime) < tcp_msl &&
824 (otp->t_flags & TF_RCVD_CC))
825 otp = tcp_close(otp);
829 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
830 inp->in6p_laddr = *addr6;
831 inp->in6p_faddr = sin6->sin6_addr;
832 inp->inp_fport = sin6->sin6_port;
833 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
834 inp->in6p_flowinfo = sin6->sin6_flowinfo;
837 /* Compute window scaling to request. */
838 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
839 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
840 tp->request_r_scale++;
843 tcpstat.tcps_connattempt++;
844 tp->t_state = TCPS_SYN_SENT;
845 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
846 tp->iss = tcp_new_isn(tp);
847 tp->t_bw_rtseq = tp->iss;
851 * Generate a CC value for this connection and
852 * check whether CC or CCnew should be used.
854 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
855 taop = &tao_noncached;
856 bzero(taop, sizeof(*taop));
859 tp->cc_send = CC_INC(tcp_ccgen);
860 if (taop->tao_ccsent != 0 &&
861 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
862 taop->tao_ccsent = tp->cc_send;
864 taop->tao_ccsent = 0;
865 tp->t_flags |= TF_SENDCCNEW;
873 * The new sockopt interface makes it possible for us to block in the
874 * copyin/out step (if we take a page fault). Taking a page fault at
875 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
876 * use TSM, there probably isn't any need for this function to run at
877 * splnet() any more. This needs more examination.)
880 tcp_ctloutput(so, sopt)
882 struct sockopt *sopt;
884 int error, opt, optval, s;
889 s = splnet(); /* XXX */
895 if (sopt->sopt_level != IPPROTO_TCP) {
897 if (INP_CHECK_SOCKAF(so, AF_INET6))
898 error = ip6_ctloutput(so, sopt);
901 error = ip_ctloutput(so, sopt);
907 switch (sopt->sopt_dir) {
909 switch (sopt->sopt_name) {
912 error = sooptcopyin(sopt, &optval, sizeof optval,
917 switch (sopt->sopt_name) {
925 opt = 0; /* dead code to fool gcc */
936 error = sooptcopyin(sopt, &optval, sizeof optval,
942 tp->t_flags |= TF_NOPUSH;
944 tp->t_flags &= ~TF_NOPUSH;
945 error = tcp_output(tp);
950 error = sooptcopyin(sopt, &optval, sizeof optval,
955 if (optval > 0 && optval <= tp->t_maxseg)
956 tp->t_maxseg = optval;
968 switch (sopt->sopt_name) {
970 optval = tp->t_flags & TF_NODELAY;
973 optval = tp->t_maxseg;
976 optval = tp->t_flags & TF_NOOPT;
979 optval = tp->t_flags & TF_NOPUSH;
986 error = sooptcopyout(sopt, &optval, sizeof optval);
994 * tcp_sendspace and tcp_recvspace are the default send and receive window
995 * sizes, respectively. These are obsolescent (this information should
996 * be set by the route).
998 u_long tcp_sendspace = 1024*32;
999 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1000 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1001 u_long tcp_recvspace = 57344; /* largest multiple of PAGE_SIZE < 64k */
1002 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1003 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1006 * Attach TCP protocol to socket, allocating
1007 * internet protocol control block, tcp control block,
1008 * bufer space, and entering LISTEN state if to accept connections.
1015 register struct tcpcb *tp;
1019 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1022 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1023 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1027 error = in_pcballoc(so, &tcbinfo, p);
1030 inp = sotoinpcb(so);
1033 inp->inp_vflag |= INP_IPV6;
1034 inp->in6p_hops = -1; /* use kernel default */
1038 inp->inp_vflag |= INP_IPV4;
1039 tp = tcp_newtcpcb(inp);
1041 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1043 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1050 so->so_state |= nofd;
1053 tp->t_state = TCPS_CLOSED;
1058 * Initiate (or continue) disconnect.
1059 * If embryonic state, just send reset (once).
1060 * If in ``let data drain'' option and linger null, just drop.
1061 * Otherwise (hard), mark socket disconnecting and drop
1062 * current input data; switch states based on user close, and
1063 * send segment to peer (with FIN).
1065 static struct tcpcb *
1067 register struct tcpcb *tp;
1069 struct socket *so = tp->t_inpcb->inp_socket;
1071 if (tp->t_state < TCPS_ESTABLISHED)
1073 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1074 tp = tcp_drop(tp, 0);
1076 soisdisconnecting(so);
1077 sbflush(&so->so_rcv);
1078 tp = tcp_usrclosed(tp);
1080 (void) tcp_output(tp);
1086 * User issued close, and wish to trail through shutdown states:
1087 * if never received SYN, just forget it. If got a SYN from peer,
1088 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1089 * If already got a FIN from peer, then almost done; go to LAST_ACK
1090 * state. In all other cases, have already sent FIN to peer (e.g.
1091 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1092 * for peer to send FIN or not respond to keep-alives, etc.
1093 * We can let the user exit from the close as soon as the FIN is acked.
1095 static struct tcpcb *
1097 register struct tcpcb *tp;
1100 switch (tp->t_state) {
1104 tp->t_state = TCPS_CLOSED;
1109 case TCPS_SYN_RECEIVED:
1110 tp->t_flags |= TF_NEEDFIN;
1113 case TCPS_ESTABLISHED:
1114 tp->t_state = TCPS_FIN_WAIT_1;
1117 case TCPS_CLOSE_WAIT:
1118 tp->t_state = TCPS_LAST_ACK;
1121 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1122 soisdisconnected(tp->t_inpcb->inp_socket);
1123 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1124 if (tp->t_state == TCPS_FIN_WAIT_2)
1125 callout_reset(tp->tt_2msl, tcp_maxidle,
1126 tcp_timer_2msl, tp);