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/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
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_BINDV6ONLY) == 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, p);
255 error = in6_pcbbind(inp, nam, p);
258 COMMON_END(PRU_BIND);
263 * Prepare to accept connections.
266 tcp_usr_listen(struct socket *so, struct proc *p)
270 struct inpcb *inp = sotoinpcb(so);
274 if (inp->inp_lport == 0)
275 error = in_pcbbind(inp, (struct sockaddr *)0, p);
277 tp->t_state = TCPS_LISTEN;
278 COMMON_END(PRU_LISTEN);
283 tcp6_usr_listen(struct socket *so, struct proc *p)
287 struct inpcb *inp = sotoinpcb(so);
291 if (inp->inp_lport == 0) {
292 inp->inp_vflag &= ~INP_IPV4;
293 if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0)
294 inp->inp_vflag |= INP_IPV4;
295 error = in6_pcbbind(inp, (struct sockaddr *)0, p);
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 proc *p)
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(p, 0, &sinp->sin_addr.s_addr);
333 if ((error = tcp_connect(tp, nam, p)) != 0)
335 error = tcp_output(tp);
336 COMMON_END(PRU_CONNECT);
341 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
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 ((inp->inp_flags & IN6P_BINDV6ONLY) == 0 &&
362 IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
363 struct sockaddr_in sin;
365 in6_sin6_2_sin(&sin, sin6p);
366 inp->inp_vflag |= INP_IPV4;
367 inp->inp_vflag &= ~INP_IPV6;
368 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, p)) != 0)
370 error = tcp_output(tp);
373 inp->inp_vflag &= ~INP_IPV4;
374 inp->inp_vflag |= INP_IPV6;
375 if ((error = tcp6_connect(tp, nam, p)) != 0)
377 error = tcp_output(tp);
378 COMMON_END(PRU_CONNECT);
383 * Initiate disconnect from peer.
384 * If connection never passed embryonic stage, just drop;
385 * else if don't need to let data drain, then can just drop anyways,
386 * else have to begin TCP shutdown process: mark socket disconnecting,
387 * drain unread data, state switch to reflect user close, and
388 * send segment (e.g. FIN) to peer. Socket will be really disconnected
389 * when peer sends FIN and acks ours.
391 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
394 tcp_usr_disconnect(struct socket *so)
398 struct inpcb *inp = sotoinpcb(so);
402 tp = tcp_disconnect(tp);
403 COMMON_END(PRU_DISCONNECT);
407 * Accept a connection. Essentially all the work is
408 * done at higher levels; just return the address
409 * of the peer, storing through addr.
412 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
416 struct inpcb *inp = sotoinpcb(so);
420 in_setpeeraddr(so, nam);
421 COMMON_END(PRU_ACCEPT);
426 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
430 struct inpcb *inp = sotoinpcb(so);
434 in6_mapped_peeraddr(so, nam);
435 COMMON_END(PRU_ACCEPT);
439 * Mark the connection as being incapable of further output.
442 tcp_usr_shutdown(struct socket *so)
446 struct inpcb *inp = sotoinpcb(so);
451 tp = tcp_usrclosed(tp);
453 error = tcp_output(tp);
454 COMMON_END(PRU_SHUTDOWN);
458 * After a receive, possibly send window update to peer.
461 tcp_usr_rcvd(struct socket *so, int flags)
465 struct inpcb *inp = sotoinpcb(so);
470 COMMON_END(PRU_RCVD);
474 * Do a send by putting data in output queue and updating urgent
475 * marker if URG set. Possibly send more data. Unlike the other
476 * pru_*() routines, the mbuf chains are our responsibility. We
477 * must either enqueue them or free them. The other pru_* routines
478 * generally are caller-frees.
481 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
482 struct sockaddr *nam, struct mbuf *control, struct proc *p)
486 struct inpcb *inp = sotoinpcb(so);
495 * OOPS! we lost a race, the TCP session got reset after
496 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
497 * network interrupt in the non-splnet() section of sosend().
503 error = ECONNRESET; /* XXX EPIPE? */
509 isipv6 = nam && nam->sa_family == AF_INET6;
514 /* TCP doesn't do control messages (rights, creds, etc) */
515 if (control->m_len) {
522 m_freem(control); /* empty control, just free it */
524 if(!(flags & PRUS_OOB)) {
525 sbappend(&so->so_snd, m);
526 if (nam && tp->t_state < TCPS_SYN_SENT) {
528 * Do implied connect if not yet connected,
529 * initialize window to default value, and
530 * initialize maxseg/maxopd using peer's cached
535 error = tcp6_connect(tp, nam, p);
538 error = tcp_connect(tp, nam, p);
541 tp->snd_wnd = TTCP_CLIENT_SND_WND;
545 if (flags & PRUS_EOF) {
547 * Close the send side of the connection after
551 tp = tcp_usrclosed(tp);
554 if (flags & PRUS_MORETOCOME)
555 tp->t_flags |= TF_MORETOCOME;
556 error = tcp_output(tp);
557 if (flags & PRUS_MORETOCOME)
558 tp->t_flags &= ~TF_MORETOCOME;
561 if (sbspace(&so->so_snd) < -512) {
567 * According to RFC961 (Assigned Protocols),
568 * the urgent pointer points to the last octet
569 * of urgent data. We continue, however,
570 * to consider it to indicate the first octet
571 * of data past the urgent section.
572 * Otherwise, snd_up should be one lower.
574 sbappend(&so->so_snd, m);
575 if (nam && tp->t_state < TCPS_SYN_SENT) {
577 * Do implied connect if not yet connected,
578 * initialize window to default value, and
579 * initialize maxseg/maxopd using peer's cached
584 error = tcp6_connect(tp, nam, p);
587 error = tcp_connect(tp, nam, p);
590 tp->snd_wnd = TTCP_CLIENT_SND_WND;
593 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
595 error = tcp_output(tp);
598 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
599 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
606 tcp_usr_abort(struct socket *so)
610 struct inpcb *inp = sotoinpcb(so);
614 tp = tcp_drop(tp, ECONNABORTED);
615 COMMON_END(PRU_ABORT);
619 * Receive out-of-band data.
622 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
626 struct inpcb *inp = sotoinpcb(so);
630 if ((so->so_oobmark == 0 &&
631 (so->so_state & SS_RCVATMARK) == 0) ||
632 so->so_options & SO_OOBINLINE ||
633 tp->t_oobflags & TCPOOB_HADDATA) {
637 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
642 *mtod(m, caddr_t) = tp->t_iobc;
643 if ((flags & MSG_PEEK) == 0)
644 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
645 COMMON_END(PRU_RCVOOB);
648 /* xxx - should be const */
649 struct pr_usrreqs tcp_usrreqs = {
650 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
651 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
652 tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
653 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
654 in_setsockaddr, sosend, soreceive, sopoll
658 struct pr_usrreqs tcp6_usrreqs = {
659 tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
660 tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
661 tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
662 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
663 in6_mapped_sockaddr, sosend, soreceive, sopoll
668 * Common subroutine to open a TCP connection to remote host specified
669 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
670 * port number if needed. Call in_pcbladdr to do the routing and to choose
671 * a local host address (interface). If there is an existing incarnation
672 * of the same connection in TIME-WAIT state and if the remote host was
673 * sending CC options and if the connection duration was < MSL, then
674 * truncate the previous TIME-WAIT state and proceed.
675 * Initialize connection parameters and enter SYN-SENT state.
678 tcp_connect(tp, nam, p)
679 register struct tcpcb *tp;
680 struct sockaddr *nam;
683 struct inpcb *inp = tp->t_inpcb, *oinp;
684 struct socket *so = inp->inp_socket;
686 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
687 struct sockaddr_in *ifaddr;
688 struct rmxp_tao *taop;
689 struct rmxp_tao tao_noncached;
692 if (inp->inp_lport == 0) {
693 error = in_pcbbind(inp, (struct sockaddr *)0, p);
699 * Cannot simply call in_pcbconnect, because there might be an
700 * earlier incarnation of this same connection still in
701 * TIME_WAIT state, creating an ADDRINUSE error.
703 error = in_pcbladdr(inp, nam, &ifaddr);
706 oinp = in_pcblookup_hash(inp->inp_pcbinfo,
707 sin->sin_addr, sin->sin_port,
708 inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
710 inp->inp_lport, 0, NULL);
712 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
713 otp->t_state == TCPS_TIME_WAIT &&
714 (ticks - otp->t_starttime) < tcp_msl &&
715 (otp->t_flags & TF_RCVD_CC))
716 otp = tcp_close(otp);
720 if (inp->inp_laddr.s_addr == INADDR_ANY)
721 inp->inp_laddr = ifaddr->sin_addr;
722 inp->inp_faddr = sin->sin_addr;
723 inp->inp_fport = sin->sin_port;
726 tp->t_template = tcp_template(tp);
727 if (tp->t_template == 0) {
728 in_pcbdisconnect(inp);
732 /* Compute window scaling to request. */
733 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
734 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
735 tp->request_r_scale++;
738 tcpstat.tcps_connattempt++;
739 tp->t_state = TCPS_SYN_SENT;
740 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
741 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
745 * Generate a CC value for this connection and
746 * check whether CC or CCnew should be used.
748 if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
749 taop = &tao_noncached;
750 bzero(taop, sizeof(*taop));
753 tp->cc_send = CC_INC(tcp_ccgen);
754 if (taop->tao_ccsent != 0 &&
755 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
756 taop->tao_ccsent = tp->cc_send;
758 taop->tao_ccsent = 0;
759 tp->t_flags |= TF_SENDCCNEW;
767 tcp6_connect(tp, nam, p)
768 register struct tcpcb *tp;
769 struct sockaddr *nam;
772 struct inpcb *inp = tp->t_inpcb, *oinp;
773 struct socket *so = inp->inp_socket;
775 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
776 struct in6_addr *addr6;
777 struct rmxp_tao *taop;
778 struct rmxp_tao tao_noncached;
781 if (inp->inp_lport == 0) {
782 error = in6_pcbbind(inp, (struct sockaddr *)0, p);
788 * Cannot simply call in_pcbconnect, because there might be an
789 * earlier incarnation of this same connection still in
790 * TIME_WAIT state, creating an ADDRINUSE error.
792 error = in6_pcbladdr(inp, nam, &addr6);
795 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
796 &sin6->sin6_addr, sin6->sin6_port,
797 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
800 inp->inp_lport, 0, NULL);
802 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
803 otp->t_state == TCPS_TIME_WAIT &&
804 (ticks - otp->t_starttime) < tcp_msl &&
805 (otp->t_flags & TF_RCVD_CC))
806 otp = tcp_close(otp);
810 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
811 inp->in6p_laddr = *addr6;
812 inp->in6p_faddr = sin6->sin6_addr;
813 inp->inp_fport = sin6->sin6_port;
814 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
815 inp->in6p_flowinfo = sin6->sin6_flowinfo;
818 tp->t_template = tcp_template(tp);
819 if (tp->t_template == 0) {
820 in6_pcbdisconnect(inp);
824 /* Compute window scaling to request. */
825 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
826 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
827 tp->request_r_scale++;
830 tcpstat.tcps_connattempt++;
831 tp->t_state = TCPS_SYN_SENT;
832 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
833 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
837 * Generate a CC value for this connection and
838 * check whether CC or CCnew should be used.
840 if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
841 taop = &tao_noncached;
842 bzero(taop, sizeof(*taop));
845 tp->cc_send = CC_INC(tcp_ccgen);
846 if (taop->tao_ccsent != 0 &&
847 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
848 taop->tao_ccsent = tp->cc_send;
850 taop->tao_ccsent = 0;
851 tp->t_flags |= TF_SENDCCNEW;
859 * The new sockopt interface makes it possible for us to block in the
860 * copyin/out step (if we take a page fault). Taking a page fault at
861 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
862 * use TSM, there probably isn't any need for this function to run at
863 * splnet() any more. This needs more examination.)
866 tcp_ctloutput(so, sopt)
868 struct sockopt *sopt;
870 int error, opt, optval, s;
875 s = splnet(); /* XXX */
881 if (sopt->sopt_level != IPPROTO_TCP) {
883 if (INP_CHECK_SOCKAF(so, AF_INET6))
884 error = ip6_ctloutput(so, sopt);
887 error = ip_ctloutput(so, sopt);
893 switch (sopt->sopt_dir) {
895 switch (sopt->sopt_name) {
899 error = sooptcopyin(sopt, &optval, sizeof optval,
904 switch (sopt->sopt_name) {
915 opt = 0; /* dead code to fool gcc */
926 error = sooptcopyin(sopt, &optval, sizeof optval,
931 if (optval > 0 && optval <= tp->t_maxseg)
932 tp->t_maxseg = optval;
944 switch (sopt->sopt_name) {
946 optval = tp->t_flags & TF_NODELAY;
949 optval = tp->t_maxseg;
952 optval = tp->t_flags & TF_NOOPT;
955 optval = tp->t_flags & TF_NOPUSH;
962 error = sooptcopyout(sopt, &optval, sizeof optval);
970 * tcp_sendspace and tcp_recvspace are the default send and receive window
971 * sizes, respectively. These are obsolescent (this information should
972 * be set by the route).
974 u_long tcp_sendspace = 1024*16;
975 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
976 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
977 u_long tcp_recvspace = 1024*16;
978 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
979 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
982 * Attach TCP protocol to socket, allocating
983 * internet protocol control block, tcp control block,
984 * bufer space, and entering LISTEN state if to accept connections.
991 register struct tcpcb *tp;
995 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
998 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
999 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1003 error = in_pcballoc(so, &tcbinfo, p);
1006 inp = sotoinpcb(so);
1008 error = ipsec_init_policy(so, &inp->inp_sp);
1021 inp->inp_vflag |= INP_IPV6;
1022 inp->in6p_hops = -1; /* use kernel default */
1026 inp->inp_vflag |= INP_IPV4;
1027 tp = tcp_newtcpcb(inp);
1029 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1031 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1038 so->so_state |= nofd;
1041 tp->t_state = TCPS_CLOSED;
1046 * Initiate (or continue) disconnect.
1047 * If embryonic state, just send reset (once).
1048 * If in ``let data drain'' option and linger null, just drop.
1049 * Otherwise (hard), mark socket disconnecting and drop
1050 * current input data; switch states based on user close, and
1051 * send segment to peer (with FIN).
1053 static struct tcpcb *
1055 register struct tcpcb *tp;
1057 struct socket *so = tp->t_inpcb->inp_socket;
1059 if (tp->t_state < TCPS_ESTABLISHED)
1061 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1062 tp = tcp_drop(tp, 0);
1064 soisdisconnecting(so);
1065 sbflush(&so->so_rcv);
1066 tp = tcp_usrclosed(tp);
1068 (void) tcp_output(tp);
1074 * User issued close, and wish to trail through shutdown states:
1075 * if never received SYN, just forget it. If got a SYN from peer,
1076 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1077 * If already got a FIN from peer, then almost done; go to LAST_ACK
1078 * state. In all other cases, have already sent FIN to peer (e.g.
1079 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1080 * for peer to send FIN or not respond to keep-alives, etc.
1081 * We can let the user exit from the close as soon as the FIN is acked.
1083 static struct tcpcb *
1085 register struct tcpcb *tp;
1088 switch (tp->t_state) {
1092 tp->t_state = TCPS_CLOSED;
1097 case TCPS_SYN_RECEIVED:
1098 tp->t_flags |= TF_NEEDFIN;
1101 case TCPS_ESTABLISHED:
1102 tp->t_state = TCPS_FIN_WAIT_1;
1105 case TCPS_CLOSE_WAIT:
1106 tp->t_state = TCPS_LAST_ACK;
1109 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1110 soisdisconnected(tp->t_inpcb->inp_socket);
1111 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1112 if (tp->t_state == TCPS_FIN_WAIT_2)
1113 callout_reset(tp->tt_2msl, tcp_maxidle,
1114 tcp_timer_2msl, tp);