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 $
35 * $DragonFly: src/sys/netinet/tcp_usrreq.c,v 1.6 2004/03/04 01:02:05 hsu Exp $
38 #include "opt_ipsec.h"
39 #include "opt_inet6.h"
40 #include "opt_tcpdebug.h"
42 #include <sys/param.h>
43 #include <sys/systm.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>
55 #include <net/route.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
60 #include <netinet/ip6.h>
62 #include <netinet/in_pcb.h>
64 #include <netinet6/in6_pcb.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip_var.h>
69 #include <netinet6/ip6_var.h>
71 #include <netinet/tcp.h>
72 #include <netinet/tcp_fsm.h>
73 #include <netinet/tcp_seq.h>
74 #include <netinet/tcp_timer.h>
75 #include <netinet/tcp_var.h>
76 #include <netinet/tcpip.h>
78 #include <netinet/tcp_debug.h>
82 #include <netinet6/ipsec.h>
86 * TCP protocol interface to socket abstraction.
88 extern char *tcpstates[]; /* XXX ??? */
90 static int tcp_attach (struct socket *, struct thread *);
91 static int tcp_connect (struct tcpcb *, struct sockaddr *,
94 static int tcp6_connect (struct tcpcb *, struct sockaddr *,
98 tcp_disconnect (struct tcpcb *);
100 tcp_usrclosed (struct tcpcb *);
103 #define TCPDEBUG0 int ostate = 0
104 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
105 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
106 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
110 #define TCPDEBUG2(req)
114 * TCP attaches to socket via pru_attach(), reserving space,
115 * and an internet control block.
118 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
122 struct inpcb *inp = sotoinpcb(so);
123 struct tcpcb *tp = 0;
132 error = tcp_attach(so, td);
136 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
137 so->so_linger = TCP_LINGERTIME;
140 TCPDEBUG2(PRU_ATTACH);
146 * pru_detach() detaches the TCP protocol from the socket.
147 * If the protocol state is non-embryonic, then can't
148 * do this directly: have to initiate a pru_disconnect(),
149 * which may finish later; embryonic TCB's can just
153 tcp_usr_detach(struct socket *so)
157 struct inpcb *inp = sotoinpcb(so);
163 return EINVAL; /* XXX */
167 tp = tcp_disconnect(tp);
169 TCPDEBUG2(PRU_DETACH);
174 #define COMMON_START() TCPDEBUG0; \
180 tp = intotcpcb(inp); \
184 #define COMMON_END(req) out: TCPDEBUG2(req); splx(s); return error; goto out
188 * Give the socket an address.
191 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
195 struct inpcb *inp = sotoinpcb(so);
197 struct sockaddr_in *sinp;
202 * Must check for multicast addresses and disallow binding
205 sinp = (struct sockaddr_in *)nam;
206 if (sinp->sin_family == AF_INET &&
207 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
208 error = EAFNOSUPPORT;
211 error = in_pcbbind(inp, nam, td);
214 COMMON_END(PRU_BIND);
220 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
224 struct inpcb *inp = sotoinpcb(so);
226 struct sockaddr_in6 *sin6p;
231 * Must check for multicast addresses and disallow binding
234 sin6p = (struct sockaddr_in6 *)nam;
235 if (sin6p->sin6_family == AF_INET6 &&
236 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
237 error = EAFNOSUPPORT;
240 inp->inp_vflag &= ~INP_IPV4;
241 inp->inp_vflag |= INP_IPV6;
242 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
243 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
244 inp->inp_vflag |= INP_IPV4;
245 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
246 struct sockaddr_in sin;
248 in6_sin6_2_sin(&sin, sin6p);
249 inp->inp_vflag |= INP_IPV4;
250 inp->inp_vflag &= ~INP_IPV6;
251 error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
255 error = in6_pcbbind(inp, nam, td);
258 COMMON_END(PRU_BIND);
263 * Prepare to accept connections.
266 tcp_usr_listen(struct socket *so, struct thread *td)
270 struct inpcb *inp = sotoinpcb(so);
274 if (inp->inp_lport == 0)
275 error = in_pcbbind(inp, (struct sockaddr *)0, td);
277 tp->t_state = TCPS_LISTEN;
278 COMMON_END(PRU_LISTEN);
283 tcp6_usr_listen(struct socket *so, struct thread *td)
287 struct inpcb *inp = sotoinpcb(so);
291 if (inp->inp_lport == 0) {
292 inp->inp_vflag &= ~INP_IPV4;
293 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
294 inp->inp_vflag |= INP_IPV4;
295 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
298 tp->t_state = TCPS_LISTEN;
299 COMMON_END(PRU_LISTEN);
304 * Initiate connection to peer.
305 * Create a template for use in transmissions on this connection.
306 * Enter SYN_SENT state, and mark socket as connecting.
307 * Start keep-alive timer, and seed output sequence space.
308 * Send initial segment on connection.
311 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
315 struct inpcb *inp = sotoinpcb(so);
317 struct sockaddr_in *sinp;
322 * Must disallow TCP ``connections'' to multicast addresses.
324 sinp = (struct sockaddr_in *)nam;
325 if (sinp->sin_family == AF_INET
326 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
327 error = EAFNOSUPPORT;
331 prison_remote_ip(td, 0, &sinp->sin_addr.s_addr);
333 if ((error = tcp_connect(tp, nam, td)) != 0)
335 error = tcp_output(tp);
336 COMMON_END(PRU_CONNECT);
341 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
345 struct inpcb *inp = sotoinpcb(so);
347 struct sockaddr_in6 *sin6p;
352 * Must disallow TCP ``connections'' to multicast addresses.
354 sin6p = (struct sockaddr_in6 *)nam;
355 if (sin6p->sin6_family == AF_INET6
356 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
357 error = EAFNOSUPPORT;
361 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
362 struct sockaddr_in sin;
364 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
369 in6_sin6_2_sin(&sin, sin6p);
370 inp->inp_vflag |= INP_IPV4;
371 inp->inp_vflag &= ~INP_IPV6;
372 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
374 error = tcp_output(tp);
377 inp->inp_vflag &= ~INP_IPV4;
378 inp->inp_vflag |= INP_IPV6;
379 inp->inp_inc.inc_isipv6 = 1;
380 if ((error = tcp6_connect(tp, nam, td)) != 0)
382 error = tcp_output(tp);
383 COMMON_END(PRU_CONNECT);
388 * Initiate disconnect from peer.
389 * If connection never passed embryonic stage, just drop;
390 * else if don't need to let data drain, then can just drop anyways,
391 * else have to begin TCP shutdown process: mark socket disconnecting,
392 * drain unread data, state switch to reflect user close, and
393 * send segment (e.g. FIN) to peer. Socket will be really disconnected
394 * when peer sends FIN and acks ours.
396 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
399 tcp_usr_disconnect(struct socket *so)
403 struct inpcb *inp = sotoinpcb(so);
407 tp = tcp_disconnect(tp);
408 COMMON_END(PRU_DISCONNECT);
412 * Accept a connection. Essentially all the work is
413 * done at higher levels; just return the address
414 * of the peer, storing through addr.
417 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
421 struct inpcb *inp = sotoinpcb(so);
422 struct tcpcb *tp = NULL;
425 if (so->so_state & SS_ISDISCONNECTED) {
426 error = ECONNABORTED;
435 in_setpeeraddr(so, nam);
436 COMMON_END(PRU_ACCEPT);
441 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
445 struct inpcb *inp = sotoinpcb(so);
446 struct tcpcb *tp = NULL;
449 if (so->so_state & SS_ISDISCONNECTED) {
450 error = ECONNABORTED;
459 in6_mapped_peeraddr(so, nam);
460 COMMON_END(PRU_ACCEPT);
464 * Mark the connection as being incapable of further output.
467 tcp_usr_shutdown(struct socket *so)
471 struct inpcb *inp = sotoinpcb(so);
476 tp = tcp_usrclosed(tp);
478 error = tcp_output(tp);
479 COMMON_END(PRU_SHUTDOWN);
483 * After a receive, possibly send window update to peer.
486 tcp_usr_rcvd(struct socket *so, int flags)
490 struct inpcb *inp = sotoinpcb(so);
495 COMMON_END(PRU_RCVD);
499 * Do a send by putting data in output queue and updating urgent
500 * marker if URG set. Possibly send more data. Unlike the other
501 * pru_*() routines, the mbuf chains are our responsibility. We
502 * must either enqueue them or free them. The other pru_* routines
503 * generally are caller-frees.
506 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
507 struct sockaddr *nam, struct mbuf *control, struct thread *td)
511 struct inpcb *inp = sotoinpcb(so);
520 * OOPS! we lost a race, the TCP session got reset after
521 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
522 * network interrupt in the non-splnet() section of sosend().
528 error = ECONNRESET; /* XXX EPIPE? */
534 isipv6 = nam && nam->sa_family == AF_INET6;
539 /* TCP doesn't do control messages (rights, creds, etc) */
540 if (control->m_len) {
547 m_freem(control); /* empty control, just free it */
549 if(!(flags & PRUS_OOB)) {
550 sbappend(&so->so_snd, m);
551 if (nam && tp->t_state < TCPS_SYN_SENT) {
553 * Do implied connect if not yet connected,
554 * initialize window to default value, and
555 * initialize maxseg/maxopd using peer's cached
560 error = tcp6_connect(tp, nam, td);
563 error = tcp_connect(tp, nam, td);
566 tp->snd_wnd = TTCP_CLIENT_SND_WND;
570 if (flags & PRUS_EOF) {
572 * Close the send side of the connection after
576 tp = tcp_usrclosed(tp);
579 if (flags & PRUS_MORETOCOME)
580 tp->t_flags |= TF_MORETOCOME;
581 error = tcp_output(tp);
582 if (flags & PRUS_MORETOCOME)
583 tp->t_flags &= ~TF_MORETOCOME;
586 if (sbspace(&so->so_snd) < -512) {
592 * According to RFC961 (Assigned Protocols),
593 * the urgent pointer points to the last octet
594 * of urgent data. We continue, however,
595 * to consider it to indicate the first octet
596 * of data past the urgent section.
597 * Otherwise, snd_up should be one lower.
599 sbappend(&so->so_snd, m);
600 if (nam && tp->t_state < TCPS_SYN_SENT) {
602 * Do implied connect if not yet connected,
603 * initialize window to default value, and
604 * initialize maxseg/maxopd using peer's cached
609 error = tcp6_connect(tp, nam, td);
612 error = tcp_connect(tp, nam, td);
615 tp->snd_wnd = TTCP_CLIENT_SND_WND;
618 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
620 error = tcp_output(tp);
623 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
624 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
631 tcp_usr_abort(struct socket *so)
635 struct inpcb *inp = sotoinpcb(so);
639 tp = tcp_drop(tp, ECONNABORTED);
640 COMMON_END(PRU_ABORT);
644 * Receive out-of-band data.
647 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
651 struct inpcb *inp = sotoinpcb(so);
655 if ((so->so_oobmark == 0 &&
656 (so->so_state & SS_RCVATMARK) == 0) ||
657 so->so_options & SO_OOBINLINE ||
658 tp->t_oobflags & TCPOOB_HADDATA) {
662 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
667 *mtod(m, caddr_t) = tp->t_iobc;
668 if ((flags & MSG_PEEK) == 0)
669 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
670 COMMON_END(PRU_RCVOOB);
673 /* xxx - should be const */
674 struct pr_usrreqs tcp_usrreqs = {
675 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
676 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
677 tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
678 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
679 in_setsockaddr, sosend, soreceive, sopoll
683 struct pr_usrreqs tcp6_usrreqs = {
684 tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
685 tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
686 tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
687 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
688 in6_mapped_sockaddr, sosend, soreceive, sopoll
693 * Common subroutine to open a TCP connection to remote host specified
694 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
695 * port number if needed. Call in_pcbladdr to do the routing and to choose
696 * a local host address (interface). If there is an existing incarnation
697 * of the same connection in TIME-WAIT state and if the remote host was
698 * sending CC options and if the connection duration was < MSL, then
699 * truncate the previous TIME-WAIT state and proceed.
700 * Initialize connection parameters and enter SYN-SENT state.
703 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
705 struct inpcb *inp = tp->t_inpcb, *oinp;
706 struct socket *so = inp->inp_socket;
708 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
709 struct sockaddr_in *if_sin;
710 struct rmxp_tao *taop;
711 struct rmxp_tao tao_noncached;
714 if (inp->inp_lport == 0) {
715 error = in_pcbbind(inp, (struct sockaddr *)0, td);
721 * Cannot simply call in_pcbconnect, because there might be an
722 * earlier incarnation of this same connection still in
723 * TIME_WAIT state, creating an ADDRINUSE error.
725 error = in_pcbladdr(inp, nam, &if_sin);
728 oinp = in_pcblookup_hash(inp->inp_pcbinfo,
729 sin->sin_addr, sin->sin_port,
730 inp->inp_laddr.s_addr != INADDR_ANY ?
731 inp->inp_laddr : if_sin->sin_addr,
732 inp->inp_lport, 0, NULL);
734 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
735 otp->t_state == TCPS_TIME_WAIT &&
736 (ticks - otp->t_starttime) < tcp_msl &&
737 (otp->t_flags & TF_RCVD_CC))
738 (void) tcp_close(otp);
742 if (inp->inp_laddr.s_addr == INADDR_ANY)
743 inp->inp_laddr = if_sin->sin_addr;
744 inp->inp_faddr = sin->sin_addr;
745 inp->inp_fport = sin->sin_port;
746 in_pcbrembindhash(inp);
747 in_pcbinsconnhash(inp);
749 /* Compute window scaling to request. */
750 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
751 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
752 tp->request_r_scale++;
755 tcpstat.tcps_connattempt++;
756 tp->t_state = TCPS_SYN_SENT;
757 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
758 tp->iss = tcp_new_isn(tp);
759 tp->t_bw_rtseq = tp->iss;
763 * Generate a CC value for this connection and
764 * check whether CC or CCnew should be used.
766 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
767 taop = &tao_noncached;
768 bzero(taop, sizeof(*taop));
771 tp->cc_send = CC_INC(tcp_ccgen);
772 if (taop->tao_ccsent != 0 &&
773 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
774 taop->tao_ccsent = tp->cc_send;
776 taop->tao_ccsent = 0;
777 tp->t_flags |= TF_SENDCCNEW;
785 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
787 struct inpcb *inp = tp->t_inpcb, *oinp;
788 struct socket *so = inp->inp_socket;
790 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
791 struct in6_addr *addr6;
792 struct rmxp_tao *taop;
793 struct rmxp_tao tao_noncached;
796 if (inp->inp_lport == 0) {
797 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
803 * Cannot simply call in_pcbconnect, because there might be an
804 * earlier incarnation of this same connection still in
805 * TIME_WAIT state, creating an ADDRINUSE error.
807 error = in6_pcbladdr(inp, nam, &addr6);
810 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
811 &sin6->sin6_addr, sin6->sin6_port,
812 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
813 addr6 : &inp->in6p_laddr,
814 inp->inp_lport, 0, NULL);
816 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
817 otp->t_state == TCPS_TIME_WAIT &&
818 (ticks - otp->t_starttime) < tcp_msl &&
819 (otp->t_flags & TF_RCVD_CC))
820 otp = tcp_close(otp);
824 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
825 inp->in6p_laddr = *addr6;
826 inp->in6p_faddr = sin6->sin6_addr;
827 inp->inp_fport = sin6->sin6_port;
828 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
829 inp->in6p_flowinfo = sin6->sin6_flowinfo;
830 in_pcbrembindhash(inp);
831 in_pcbinsconnhash(inp);
833 /* Compute window scaling to request. */
834 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
835 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
836 tp->request_r_scale++;
839 tcpstat.tcps_connattempt++;
840 tp->t_state = TCPS_SYN_SENT;
841 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
842 tp->iss = tcp_new_isn(tp);
843 tp->t_bw_rtseq = tp->iss;
847 * Generate a CC value for this connection and
848 * check whether CC or CCnew should be used.
850 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
851 taop = &tao_noncached;
852 bzero(taop, sizeof(*taop));
855 tp->cc_send = CC_INC(tcp_ccgen);
856 if (taop->tao_ccsent != 0 &&
857 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
858 taop->tao_ccsent = tp->cc_send;
860 taop->tao_ccsent = 0;
861 tp->t_flags |= TF_SENDCCNEW;
869 * The new sockopt interface makes it possible for us to block in the
870 * copyin/out step (if we take a page fault). Taking a page fault at
871 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
872 * use TSM, there probably isn't any need for this function to run at
873 * splnet() any more. This needs more examination.)
876 tcp_ctloutput(so, sopt)
878 struct sockopt *sopt;
880 int error, opt, optval, s;
885 s = splnet(); /* XXX */
891 if (sopt->sopt_level != IPPROTO_TCP) {
893 if (INP_CHECK_SOCKAF(so, AF_INET6))
894 error = ip6_ctloutput(so, sopt);
897 error = ip_ctloutput(so, sopt);
903 switch (sopt->sopt_dir) {
905 switch (sopt->sopt_name) {
908 error = sooptcopyin(sopt, &optval, sizeof optval,
913 switch (sopt->sopt_name) {
921 opt = 0; /* dead code to fool gcc */
932 error = sooptcopyin(sopt, &optval, sizeof optval,
938 tp->t_flags |= TF_NOPUSH;
940 tp->t_flags &= ~TF_NOPUSH;
941 error = tcp_output(tp);
946 error = sooptcopyin(sopt, &optval, sizeof optval,
951 if (optval > 0 && optval <= tp->t_maxseg)
952 tp->t_maxseg = optval;
964 switch (sopt->sopt_name) {
966 optval = tp->t_flags & TF_NODELAY;
969 optval = tp->t_maxseg;
972 optval = tp->t_flags & TF_NOOPT;
975 optval = tp->t_flags & TF_NOPUSH;
982 error = sooptcopyout(sopt, &optval, sizeof optval);
990 * tcp_sendspace and tcp_recvspace are the default send and receive window
991 * sizes, respectively. These are obsolescent (this information should
992 * be set by the route).
994 u_long tcp_sendspace = 1024*32;
995 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
996 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
997 u_long tcp_recvspace = 57344; /* largest multiple of PAGE_SIZE < 64k */
998 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
999 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1002 * Attach TCP protocol to socket, allocating
1003 * internet protocol control block, tcp control block,
1004 * bufer space, and entering LISTEN state if to accept connections.
1007 tcp_attach(struct socket *so, struct thread *td)
1013 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1016 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1017 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1021 error = in_pcballoc(so, &tcbinfo, td);
1024 inp = sotoinpcb(so);
1027 inp->inp_vflag |= INP_IPV6;
1028 inp->in6p_hops = -1; /* use kernel default */
1032 inp->inp_vflag |= INP_IPV4;
1033 tp = tcp_newtcpcb(inp);
1035 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1037 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1044 so->so_state |= nofd;
1047 tp->t_state = TCPS_CLOSED;
1052 * Initiate (or continue) disconnect.
1053 * If embryonic state, just send reset (once).
1054 * If in ``let data drain'' option and linger null, just drop.
1055 * Otherwise (hard), mark socket disconnecting and drop
1056 * current input data; switch states based on user close, and
1057 * send segment to peer (with FIN).
1059 static struct tcpcb *
1063 struct socket *so = tp->t_inpcb->inp_socket;
1065 if (tp->t_state < TCPS_ESTABLISHED)
1067 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1068 tp = tcp_drop(tp, 0);
1070 soisdisconnecting(so);
1071 sbflush(&so->so_rcv);
1072 tp = tcp_usrclosed(tp);
1074 (void) tcp_output(tp);
1080 * User issued close, and wish to trail through shutdown states:
1081 * if never received SYN, just forget it. If got a SYN from peer,
1082 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1083 * If already got a FIN from peer, then almost done; go to LAST_ACK
1084 * state. In all other cases, have already sent FIN to peer (e.g.
1085 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1086 * for peer to send FIN or not respond to keep-alives, etc.
1087 * We can let the user exit from the close as soon as the FIN is acked.
1089 static struct tcpcb *
1094 switch (tp->t_state) {
1098 tp->t_state = TCPS_CLOSED;
1103 case TCPS_SYN_RECEIVED:
1104 tp->t_flags |= TF_NEEDFIN;
1107 case TCPS_ESTABLISHED:
1108 tp->t_state = TCPS_FIN_WAIT_1;
1111 case TCPS_CLOSE_WAIT:
1112 tp->t_state = TCPS_LAST_ACK;
1115 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1116 soisdisconnected(tp->t_inpcb->inp_socket);
1117 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1118 if (tp->t_state == TCPS_FIN_WAIT_2)
1119 callout_reset(tp->tt_2msl, tcp_maxidle,
1120 tcp_timer_2msl, tp);