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.15 2004/04/13 07:10:34 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/malloc.h>
46 #include <sys/sysctl.h>
47 #include <sys/globaldata.h>
48 #include <sys/thread.h>
52 #include <sys/domain.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/protosw.h>
58 #include <sys/msgport2.h>
61 #include <net/netisr.h>
62 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
67 #include <netinet/ip6.h>
69 #include <netinet/in_pcb.h>
71 #include <netinet6/in6_pcb.h>
73 #include <netinet/in_var.h>
74 #include <netinet/ip_var.h>
76 #include <netinet6/ip6_var.h>
78 #include <netinet/tcp.h>
79 #include <netinet/tcp_fsm.h>
80 #include <netinet/tcp_seq.h>
81 #include <netinet/tcp_timer.h>
82 #include <netinet/tcp_var.h>
83 #include <netinet/tcpip.h>
85 #include <netinet/tcp_debug.h>
89 #include <netinet6/ipsec.h>
93 * TCP protocol interface to socket abstraction.
95 extern char *tcpstates[]; /* XXX ??? */
97 static int tcp_attach (struct socket *, struct pru_attach_info *);
98 static int tcp_connect (struct tcpcb *, struct sockaddr *,
101 static int tcp6_connect (struct tcpcb *, struct sockaddr *,
104 static struct tcpcb *
105 tcp_disconnect (struct tcpcb *);
106 static struct tcpcb *
107 tcp_usrclosed (struct tcpcb *);
110 #define TCPDEBUG0 int ostate = 0
111 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
112 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
113 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
117 #define TCPDEBUG2(req)
121 * TCP attaches to socket via pru_attach(), reserving space,
122 * and an internet control block.
125 tcp_usr_attach(struct socket *so, int proto, struct pru_attach_info *ai)
129 struct inpcb *inp = sotoinpcb(so);
130 struct tcpcb *tp = 0;
139 error = tcp_attach(so, ai);
143 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
144 so->so_linger = TCP_LINGERTIME;
147 TCPDEBUG2(PRU_ATTACH);
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)
164 struct inpcb *inp = sotoinpcb(so);
170 return EINVAL; /* XXX */
174 tp = tcp_disconnect(tp);
176 TCPDEBUG2(PRU_DETACH);
181 #define COMMON_START() TCPDEBUG0; \
187 tp = intotcpcb(inp); \
191 #define COMMON_END(req) out: TCPDEBUG2(req); splx(s); return error; goto out
195 * Give the socket an address.
198 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
202 struct inpcb *inp = sotoinpcb(so);
204 struct sockaddr_in *sinp;
209 * Must check for multicast addresses and disallow binding
212 sinp = (struct sockaddr_in *)nam;
213 if (sinp->sin_family == AF_INET &&
214 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
215 error = EAFNOSUPPORT;
218 error = in_pcbbind(inp, nam, td);
221 COMMON_END(PRU_BIND);
227 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
231 struct inpcb *inp = sotoinpcb(so);
233 struct sockaddr_in6 *sin6p;
238 * Must check for multicast addresses and disallow binding
241 sin6p = (struct sockaddr_in6 *)nam;
242 if (sin6p->sin6_family == AF_INET6 &&
243 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
244 error = EAFNOSUPPORT;
247 inp->inp_vflag &= ~INP_IPV4;
248 inp->inp_vflag |= INP_IPV6;
249 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
250 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
251 inp->inp_vflag |= INP_IPV4;
252 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
253 struct sockaddr_in sin;
255 in6_sin6_2_sin(&sin, sin6p);
256 inp->inp_vflag |= INP_IPV4;
257 inp->inp_vflag &= ~INP_IPV6;
258 error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
262 error = in6_pcbbind(inp, nam, td);
265 COMMON_END(PRU_BIND);
270 * Prepare to accept connections.
273 tcp_usr_listen(struct socket *so, struct thread *td)
277 struct inpcb *inp = sotoinpcb(so);
281 if (inp->inp_lport == 0)
282 error = in_pcbbind(inp, (struct sockaddr *)0, td);
284 tp->t_state = TCPS_LISTEN;
285 in_pcbinswildcardhash(inp);
287 COMMON_END(PRU_LISTEN);
292 tcp6_usr_listen(struct socket *so, struct thread *td)
296 struct inpcb *inp = sotoinpcb(so);
300 if (inp->inp_lport == 0) {
301 inp->inp_vflag &= ~INP_IPV4;
302 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
303 inp->inp_vflag |= INP_IPV4;
304 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
307 tp->t_state = TCPS_LISTEN;
308 COMMON_END(PRU_LISTEN);
313 * Initiate connection to peer.
314 * Create a template for use in transmissions on this connection.
315 * Enter SYN_SENT state, and mark socket as connecting.
316 * Start keep-alive timer, and seed output sequence space.
317 * Send initial segment on connection.
320 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
324 struct inpcb *inp = sotoinpcb(so);
326 struct sockaddr_in *sinp;
331 * Must disallow TCP ``connections'' to multicast addresses.
333 sinp = (struct sockaddr_in *)nam;
334 if (sinp->sin_family == AF_INET
335 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
336 error = EAFNOSUPPORT;
340 prison_remote_ip(td, 0, &sinp->sin_addr.s_addr);
342 if ((error = tcp_connect(tp, nam, td)) != 0)
344 error = tcp_output(tp);
345 COMMON_END(PRU_CONNECT);
350 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
354 struct inpcb *inp = sotoinpcb(so);
356 struct sockaddr_in6 *sin6p;
361 * Must disallow TCP ``connections'' to multicast addresses.
363 sin6p = (struct sockaddr_in6 *)nam;
364 if (sin6p->sin6_family == AF_INET6
365 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
366 error = EAFNOSUPPORT;
370 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
371 struct sockaddr_in sin;
373 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
378 in6_sin6_2_sin(&sin, sin6p);
379 inp->inp_vflag |= INP_IPV4;
380 inp->inp_vflag &= ~INP_IPV6;
381 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
383 error = tcp_output(tp);
386 inp->inp_vflag &= ~INP_IPV4;
387 inp->inp_vflag |= INP_IPV6;
388 inp->inp_inc.inc_isipv6 = 1;
389 if ((error = tcp6_connect(tp, nam, td)) != 0)
391 error = tcp_output(tp);
392 COMMON_END(PRU_CONNECT);
397 * Initiate disconnect from peer.
398 * If connection never passed embryonic stage, just drop;
399 * else if don't need to let data drain, then can just drop anyways,
400 * else have to begin TCP shutdown process: mark socket disconnecting,
401 * drain unread data, state switch to reflect user close, and
402 * send segment (e.g. FIN) to peer. Socket will be really disconnected
403 * when peer sends FIN and acks ours.
405 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
408 tcp_usr_disconnect(struct socket *so)
412 struct inpcb *inp = sotoinpcb(so);
416 tp = tcp_disconnect(tp);
417 COMMON_END(PRU_DISCONNECT);
421 * Accept a connection. Essentially all the work is
422 * done at higher levels; just return the address
423 * of the peer, storing through addr.
426 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
430 struct inpcb *inp = sotoinpcb(so);
431 struct tcpcb *tp = NULL;
434 if (so->so_state & SS_ISDISCONNECTED) {
435 error = ECONNABORTED;
444 in_setpeeraddr(so, nam);
445 COMMON_END(PRU_ACCEPT);
450 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
454 struct inpcb *inp = sotoinpcb(so);
455 struct tcpcb *tp = NULL;
458 if (so->so_state & SS_ISDISCONNECTED) {
459 error = ECONNABORTED;
468 in6_mapped_peeraddr(so, nam);
469 COMMON_END(PRU_ACCEPT);
473 * Mark the connection as being incapable of further output.
476 tcp_usr_shutdown(struct socket *so)
480 struct inpcb *inp = sotoinpcb(so);
485 tp = tcp_usrclosed(tp);
487 error = tcp_output(tp);
488 COMMON_END(PRU_SHUTDOWN);
492 * After a receive, possibly send window update to peer.
495 tcp_usr_rcvd(struct socket *so, int flags)
499 struct inpcb *inp = sotoinpcb(so);
504 COMMON_END(PRU_RCVD);
508 * Do a send by putting data in output queue and updating urgent
509 * marker if URG set. Possibly send more data. Unlike the other
510 * pru_*() routines, the mbuf chains are our responsibility. We
511 * must either enqueue them or free them. The other pru_* routines
512 * generally are caller-frees.
515 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
516 struct sockaddr *nam, struct mbuf *control, struct thread *td)
520 struct inpcb *inp = sotoinpcb(so);
529 * OOPS! we lost a race, the TCP session got reset after
530 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
531 * network interrupt in the non-splnet() section of sosend().
537 error = ECONNRESET; /* XXX EPIPE? */
543 isipv6 = nam && nam->sa_family == AF_INET6;
548 /* TCP doesn't do control messages (rights, creds, etc) */
549 if (control->m_len) {
556 m_freem(control); /* empty control, just free it */
558 if(!(flags & PRUS_OOB)) {
559 sbappend(&so->so_snd, m);
560 if (nam && tp->t_state < TCPS_SYN_SENT) {
562 * Do implied connect if not yet connected,
563 * initialize window to default value, and
564 * initialize maxseg/maxopd using peer's cached
569 error = tcp6_connect(tp, nam, td);
572 error = tcp_connect(tp, nam, td);
575 tp->snd_wnd = TTCP_CLIENT_SND_WND;
579 if (flags & PRUS_EOF) {
581 * Close the send side of the connection after
585 tp = tcp_usrclosed(tp);
588 if (flags & PRUS_MORETOCOME)
589 tp->t_flags |= TF_MORETOCOME;
590 error = tcp_output(tp);
591 if (flags & PRUS_MORETOCOME)
592 tp->t_flags &= ~TF_MORETOCOME;
595 if (sbspace(&so->so_snd) < -512) {
601 * According to RFC961 (Assigned Protocols),
602 * the urgent pointer points to the last octet
603 * of urgent data. We continue, however,
604 * to consider it to indicate the first octet
605 * of data past the urgent section.
606 * Otherwise, snd_up should be one lower.
608 sbappend(&so->so_snd, m);
609 if (nam && tp->t_state < TCPS_SYN_SENT) {
611 * Do implied connect if not yet connected,
612 * initialize window to default value, and
613 * initialize maxseg/maxopd using peer's cached
618 error = tcp6_connect(tp, nam, td);
621 error = tcp_connect(tp, nam, td);
624 tp->snd_wnd = TTCP_CLIENT_SND_WND;
627 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
629 error = tcp_output(tp);
632 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
633 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
640 tcp_usr_abort(struct socket *so)
644 struct inpcb *inp = sotoinpcb(so);
648 tp = tcp_drop(tp, ECONNABORTED);
649 COMMON_END(PRU_ABORT);
653 * Receive out-of-band data.
656 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
660 struct inpcb *inp = sotoinpcb(so);
664 if ((so->so_oobmark == 0 &&
665 (so->so_state & SS_RCVATMARK) == 0) ||
666 so->so_options & SO_OOBINLINE ||
667 tp->t_oobflags & TCPOOB_HADDATA) {
671 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
676 *mtod(m, caddr_t) = tp->t_iobc;
677 if ((flags & MSG_PEEK) == 0)
678 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
679 COMMON_END(PRU_RCVOOB);
682 /* xxx - should be const */
683 struct pr_usrreqs tcp_usrreqs = {
684 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
685 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
686 tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
687 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
688 in_setsockaddr, sosend, soreceive, sopoll
692 struct pr_usrreqs tcp6_usrreqs = {
693 tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
694 tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
695 tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
696 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
697 in6_mapped_sockaddr, sosend, soreceive, sopoll
701 struct netmsg_tcp_connect {
702 struct lwkt_msg nm_lmsg;
703 netisr_fn_t nm_handler;
705 struct sockaddr_in *nm_sin;
706 struct sockaddr_in *nm_ifsin;
710 tcp_connect_oncpu(struct tcpcb *tp, struct sockaddr_in *sin,
711 struct sockaddr_in *if_sin)
713 struct inpcb *inp = tp->t_inpcb, *oinp;
714 struct socket *so = inp->inp_socket;
716 struct rmxp_tao *taop;
717 struct rmxp_tao tao_noncached;
719 oinp = in_pcblookup_hash(&tcbinfo[mycpu->gd_cpuid],
720 sin->sin_addr, sin->sin_port,
721 inp->inp_laddr.s_addr != INADDR_ANY ?
722 inp->inp_laddr : if_sin->sin_addr,
723 inp->inp_lport, 0, NULL);
725 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
726 otp->t_state == TCPS_TIME_WAIT &&
727 (ticks - otp->t_starttime) < tcp_msl &&
728 (otp->t_flags & TF_RCVD_CC))
729 (void) tcp_close(otp);
733 if (inp->inp_laddr.s_addr == INADDR_ANY)
734 inp->inp_laddr = if_sin->sin_addr;
735 inp->inp_faddr = sin->sin_addr;
736 inp->inp_fport = sin->sin_port;
737 inp->inp_cpcbinfo = &tcbinfo[mycpu->gd_cpuid];
738 in_pcbinsconnhash(inp);
740 /* Compute window scaling to request. */
741 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
742 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
743 tp->request_r_scale++;
746 tcpstat.tcps_connattempt++;
747 tp->t_state = TCPS_SYN_SENT;
748 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
749 tp->iss = tcp_new_isn(tp);
750 tp->t_bw_rtseq = tp->iss;
754 * Generate a CC value for this connection and
755 * check whether CC or CCnew should be used.
757 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
758 taop = &tao_noncached;
759 bzero(taop, sizeof(*taop));
762 tp->cc_send = CC_INC(tcp_ccgen);
763 if (taop->tao_ccsent != 0 &&
764 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
765 taop->tao_ccsent = tp->cc_send;
767 taop->tao_ccsent = 0;
768 tp->t_flags |= TF_SENDCCNEW;
775 tcp_connect_handler(struct netmsg *msg0)
777 struct netmsg_tcp_connect *msg = (struct netmsg_tcp_connect *)msg0;
780 error = tcp_connect_oncpu(msg->nm_tp, msg->nm_sin, msg->nm_ifsin);
781 lwkt_replymsg(&msg0->nm_lmsg, error);
785 * Common subroutine to open a TCP connection to remote host specified
786 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
787 * port number if needed. Call in_pcbladdr to do the routing and to choose
788 * a local host address (interface). If there is an existing incarnation
789 * of the same connection in TIME-WAIT state and if the remote host was
790 * sending CC options and if the connection duration was < MSL, then
791 * truncate the previous TIME-WAIT state and proceed.
792 * Initialize connection parameters and enter SYN-SENT state.
795 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
797 struct inpcb *inp = tp->t_inpcb;
798 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
799 struct sockaddr_in *if_sin;
801 boolean_t didbind = FALSE;
806 if (inp->inp_lport == 0) {
807 error = in_pcbbind(inp, (struct sockaddr *)NULL, td);
814 * Cannot simply call in_pcbconnect, because there might be an
815 * earlier incarnation of this same connection still in
816 * TIME_WAIT state, creating an ADDRINUSE error.
818 error = in_pcbladdr(inp, nam, &if_sin);
823 port = tcp_addrport(sin->sin_addr.s_addr, sin->sin_port,
824 inp->inp_laddr.s_addr ?
825 inp->inp_laddr.s_addr : if_sin->sin_addr.s_addr,
828 if (port->mp_td != curthread) {
829 struct netmsg_tcp_connect *msg;
831 msg = malloc(sizeof(struct netmsg_tcp_connect), M_LWKTMSG,
834 if (didbind) { /* need to unwind bind */
836 inp->inp_laddr.s_addr = INADDR_ANY;
837 in_pcbremwildcardhash(inp);
841 lwkt_initmsg(&msg->nm_lmsg, CMD_NETMSG_ONCPU);
842 msg->nm_handler = tcp_connect_handler;
845 msg->nm_ifsin = if_sin;
846 error = lwkt_domsg(port, &msg->nm_lmsg);
849 error = tcp_connect_oncpu(tp, sin, if_sin);
856 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
858 struct inpcb *inp = tp->t_inpcb, *oinp;
859 struct socket *so = inp->inp_socket;
861 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
862 struct in6_addr *addr6;
863 struct rmxp_tao *taop;
864 struct rmxp_tao tao_noncached;
867 if (inp->inp_lport == 0) {
868 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
874 * Cannot simply call in_pcbconnect, because there might be an
875 * earlier incarnation of this same connection still in
876 * TIME_WAIT state, creating an ADDRINUSE error.
878 error = in6_pcbladdr(inp, nam, &addr6);
881 oinp = in6_pcblookup_hash(inp->inp_cpcbinfo,
882 &sin6->sin6_addr, sin6->sin6_port,
883 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
884 addr6 : &inp->in6p_laddr,
885 inp->inp_lport, 0, NULL);
887 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
888 otp->t_state == TCPS_TIME_WAIT &&
889 (ticks - otp->t_starttime) < tcp_msl &&
890 (otp->t_flags & TF_RCVD_CC))
891 otp = tcp_close(otp);
895 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
896 inp->in6p_laddr = *addr6;
897 inp->in6p_faddr = sin6->sin6_addr;
898 inp->inp_fport = sin6->sin6_port;
899 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
900 inp->in6p_flowinfo = sin6->sin6_flowinfo;
901 in_pcbinsconnhash(inp);
903 /* Compute window scaling to request. */
904 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
905 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
906 tp->request_r_scale++;
909 tcpstat.tcps_connattempt++;
910 tp->t_state = TCPS_SYN_SENT;
911 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
912 tp->iss = tcp_new_isn(tp);
913 tp->t_bw_rtseq = tp->iss;
917 * Generate a CC value for this connection and
918 * check whether CC or CCnew should be used.
920 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
921 taop = &tao_noncached;
922 bzero(taop, sizeof(*taop));
925 tp->cc_send = CC_INC(tcp_ccgen);
926 if (taop->tao_ccsent != 0 &&
927 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
928 taop->tao_ccsent = tp->cc_send;
930 taop->tao_ccsent = 0;
931 tp->t_flags |= TF_SENDCCNEW;
939 * The new sockopt interface makes it possible for us to block in the
940 * copyin/out step (if we take a page fault). Taking a page fault at
941 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
942 * use TSM, there probably isn't any need for this function to run at
943 * splnet() any more. This needs more examination.)
946 tcp_ctloutput(so, sopt)
948 struct sockopt *sopt;
950 int error, opt, optval, s;
955 s = splnet(); /* XXX */
961 if (sopt->sopt_level != IPPROTO_TCP) {
963 if (INP_CHECK_SOCKAF(so, AF_INET6))
964 error = ip6_ctloutput(so, sopt);
967 error = ip_ctloutput(so, sopt);
973 switch (sopt->sopt_dir) {
975 switch (sopt->sopt_name) {
978 error = sooptcopyin(sopt, &optval, sizeof optval,
983 switch (sopt->sopt_name) {
991 opt = 0; /* dead code to fool gcc */
1002 error = sooptcopyin(sopt, &optval, sizeof optval,
1008 tp->t_flags |= TF_NOPUSH;
1010 tp->t_flags &= ~TF_NOPUSH;
1011 error = tcp_output(tp);
1016 error = sooptcopyin(sopt, &optval, sizeof optval,
1021 if (optval > 0 && optval <= tp->t_maxseg)
1022 tp->t_maxseg = optval;
1028 error = ENOPROTOOPT;
1034 switch (sopt->sopt_name) {
1036 optval = tp->t_flags & TF_NODELAY;
1039 optval = tp->t_maxseg;
1042 optval = tp->t_flags & TF_NOOPT;
1045 optval = tp->t_flags & TF_NOPUSH;
1048 error = ENOPROTOOPT;
1052 error = sooptcopyout(sopt, &optval, sizeof optval);
1060 * tcp_sendspace and tcp_recvspace are the default send and receive window
1061 * sizes, respectively. These are obsolescent (this information should
1062 * be set by the route).
1064 u_long tcp_sendspace = 1024*32;
1065 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1066 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1067 u_long tcp_recvspace = 57344; /* largest multiple of PAGE_SIZE < 64k */
1068 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1069 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1072 * Attach TCP protocol to socket, allocating
1073 * internet protocol control block, tcp control block,
1074 * bufer space, and entering LISTEN state if to accept connections.
1077 tcp_attach(struct socket *so, struct pru_attach_info *ai)
1084 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1087 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1088 error = soreserve(so, tcp_sendspace, tcp_recvspace,
1093 cpu = mycpu->gd_cpuid;
1094 error = in_pcballoc(so, &tcbinfo[cpu]);
1097 inp = sotoinpcb(so);
1100 inp->inp_vflag |= INP_IPV6;
1101 inp->in6p_hops = -1; /* use kernel default */
1105 inp->inp_vflag |= INP_IPV4;
1106 tp = tcp_newtcpcb(inp);
1108 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1110 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1117 so->so_state |= nofd;
1120 tp->t_state = TCPS_CLOSED;
1125 * Initiate (or continue) disconnect.
1126 * If embryonic state, just send reset (once).
1127 * If in ``let data drain'' option and linger null, just drop.
1128 * Otherwise (hard), mark socket disconnecting and drop
1129 * current input data; switch states based on user close, and
1130 * send segment to peer (with FIN).
1132 static struct tcpcb *
1136 struct socket *so = tp->t_inpcb->inp_socket;
1138 if (tp->t_state < TCPS_ESTABLISHED)
1140 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1141 tp = tcp_drop(tp, 0);
1143 soisdisconnecting(so);
1144 sbflush(&so->so_rcv);
1145 tp = tcp_usrclosed(tp);
1147 (void) tcp_output(tp);
1153 * User issued close, and wish to trail through shutdown states:
1154 * if never received SYN, just forget it. If got a SYN from peer,
1155 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1156 * If already got a FIN from peer, then almost done; go to LAST_ACK
1157 * state. In all other cases, have already sent FIN to peer (e.g.
1158 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1159 * for peer to send FIN or not respond to keep-alives, etc.
1160 * We can let the user exit from the close as soon as the FIN is acked.
1162 static struct tcpcb *
1167 switch (tp->t_state) {
1171 tp->t_state = TCPS_CLOSED;
1176 case TCPS_SYN_RECEIVED:
1177 tp->t_flags |= TF_NEEDFIN;
1180 case TCPS_ESTABLISHED:
1181 tp->t_state = TCPS_FIN_WAIT_1;
1184 case TCPS_CLOSE_WAIT:
1185 tp->t_state = TCPS_LAST_ACK;
1188 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1189 soisdisconnected(tp->t_inpcb->inp_socket);
1190 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1191 if (tp->t_state == TCPS_FIN_WAIT_2)
1192 callout_reset(tp->tt_2msl, tcp_maxidle,
1193 tcp_timer_2msl, tp);