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.8 2004/03/08 19:44:32 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>
46 #include <sys/globaldata.h>
47 #include <sys/thread.h>
51 #include <sys/domain.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/protosw.h>
58 #include <net/route.h>
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
63 #include <netinet/ip6.h>
65 #include <netinet/in_pcb.h>
67 #include <netinet6/in6_pcb.h>
69 #include <netinet/in_var.h>
70 #include <netinet/ip_var.h>
72 #include <netinet6/ip6_var.h>
74 #include <netinet/tcp.h>
75 #include <netinet/tcp_fsm.h>
76 #include <netinet/tcp_seq.h>
77 #include <netinet/tcp_timer.h>
78 #include <netinet/tcp_var.h>
79 #include <netinet/tcpip.h>
81 #include <netinet/tcp_debug.h>
85 #include <netinet6/ipsec.h>
89 * TCP protocol interface to socket abstraction.
91 extern char *tcpstates[]; /* XXX ??? */
93 static int tcp_attach (struct socket *, struct pru_attach_info *);
94 static int tcp_connect (struct tcpcb *, struct sockaddr *,
97 static int tcp6_connect (struct tcpcb *, struct sockaddr *,
100 static struct tcpcb *
101 tcp_disconnect (struct tcpcb *);
102 static struct tcpcb *
103 tcp_usrclosed (struct tcpcb *);
106 #define TCPDEBUG0 int ostate = 0
107 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
108 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
109 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
113 #define TCPDEBUG2(req)
117 * TCP attaches to socket via pru_attach(), reserving space,
118 * and an internet control block.
121 tcp_usr_attach(struct socket *so, int proto, struct pru_attach_info *ai)
125 struct inpcb *inp = sotoinpcb(so);
126 struct tcpcb *tp = 0;
135 error = tcp_attach(so, ai);
139 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
140 so->so_linger = TCP_LINGERTIME;
143 TCPDEBUG2(PRU_ATTACH);
149 * pru_detach() detaches the TCP protocol from the socket.
150 * If the protocol state is non-embryonic, then can't
151 * do this directly: have to initiate a pru_disconnect(),
152 * which may finish later; embryonic TCB's can just
156 tcp_usr_detach(struct socket *so)
160 struct inpcb *inp = sotoinpcb(so);
166 return EINVAL; /* XXX */
170 tp = tcp_disconnect(tp);
172 TCPDEBUG2(PRU_DETACH);
177 #define COMMON_START() TCPDEBUG0; \
183 tp = intotcpcb(inp); \
187 #define COMMON_END(req) out: TCPDEBUG2(req); splx(s); return error; goto out
191 * Give the socket an address.
194 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
198 struct inpcb *inp = sotoinpcb(so);
200 struct sockaddr_in *sinp;
205 * Must check for multicast addresses and disallow binding
208 sinp = (struct sockaddr_in *)nam;
209 if (sinp->sin_family == AF_INET &&
210 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
211 error = EAFNOSUPPORT;
214 error = in_pcbbind(inp, nam, td);
217 COMMON_END(PRU_BIND);
223 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
227 struct inpcb *inp = sotoinpcb(so);
229 struct sockaddr_in6 *sin6p;
234 * Must check for multicast addresses and disallow binding
237 sin6p = (struct sockaddr_in6 *)nam;
238 if (sin6p->sin6_family == AF_INET6 &&
239 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
240 error = EAFNOSUPPORT;
243 inp->inp_vflag &= ~INP_IPV4;
244 inp->inp_vflag |= INP_IPV6;
245 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
246 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
247 inp->inp_vflag |= INP_IPV4;
248 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
249 struct sockaddr_in sin;
251 in6_sin6_2_sin(&sin, sin6p);
252 inp->inp_vflag |= INP_IPV4;
253 inp->inp_vflag &= ~INP_IPV6;
254 error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
258 error = in6_pcbbind(inp, nam, td);
261 COMMON_END(PRU_BIND);
266 * Prepare to accept connections.
269 tcp_usr_listen(struct socket *so, struct thread *td)
273 struct inpcb *inp = sotoinpcb(so);
277 if (inp->inp_lport == 0)
278 error = in_pcbbind(inp, (struct sockaddr *)0, td);
280 tp->t_state = TCPS_LISTEN;
281 COMMON_END(PRU_LISTEN);
286 tcp6_usr_listen(struct socket *so, struct thread *td)
290 struct inpcb *inp = sotoinpcb(so);
294 if (inp->inp_lport == 0) {
295 inp->inp_vflag &= ~INP_IPV4;
296 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
297 inp->inp_vflag |= INP_IPV4;
298 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
301 tp->t_state = TCPS_LISTEN;
302 COMMON_END(PRU_LISTEN);
307 * Initiate connection to peer.
308 * Create a template for use in transmissions on this connection.
309 * Enter SYN_SENT state, and mark socket as connecting.
310 * Start keep-alive timer, and seed output sequence space.
311 * Send initial segment on connection.
314 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
318 struct inpcb *inp = sotoinpcb(so);
320 struct sockaddr_in *sinp;
325 * Must disallow TCP ``connections'' to multicast addresses.
327 sinp = (struct sockaddr_in *)nam;
328 if (sinp->sin_family == AF_INET
329 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
330 error = EAFNOSUPPORT;
334 prison_remote_ip(td, 0, &sinp->sin_addr.s_addr);
336 if ((error = tcp_connect(tp, nam, td)) != 0)
338 error = tcp_output(tp);
339 COMMON_END(PRU_CONNECT);
344 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
348 struct inpcb *inp = sotoinpcb(so);
350 struct sockaddr_in6 *sin6p;
355 * Must disallow TCP ``connections'' to multicast addresses.
357 sin6p = (struct sockaddr_in6 *)nam;
358 if (sin6p->sin6_family == AF_INET6
359 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
360 error = EAFNOSUPPORT;
364 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
365 struct sockaddr_in sin;
367 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
372 in6_sin6_2_sin(&sin, sin6p);
373 inp->inp_vflag |= INP_IPV4;
374 inp->inp_vflag &= ~INP_IPV6;
375 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
377 error = tcp_output(tp);
380 inp->inp_vflag &= ~INP_IPV4;
381 inp->inp_vflag |= INP_IPV6;
382 inp->inp_inc.inc_isipv6 = 1;
383 if ((error = tcp6_connect(tp, nam, td)) != 0)
385 error = tcp_output(tp);
386 COMMON_END(PRU_CONNECT);
391 * Initiate disconnect from peer.
392 * If connection never passed embryonic stage, just drop;
393 * else if don't need to let data drain, then can just drop anyways,
394 * else have to begin TCP shutdown process: mark socket disconnecting,
395 * drain unread data, state switch to reflect user close, and
396 * send segment (e.g. FIN) to peer. Socket will be really disconnected
397 * when peer sends FIN and acks ours.
399 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
402 tcp_usr_disconnect(struct socket *so)
406 struct inpcb *inp = sotoinpcb(so);
410 tp = tcp_disconnect(tp);
411 COMMON_END(PRU_DISCONNECT);
415 * Accept a connection. Essentially all the work is
416 * done at higher levels; just return the address
417 * of the peer, storing through addr.
420 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
424 struct inpcb *inp = sotoinpcb(so);
425 struct tcpcb *tp = NULL;
428 if (so->so_state & SS_ISDISCONNECTED) {
429 error = ECONNABORTED;
438 in_setpeeraddr(so, nam);
439 COMMON_END(PRU_ACCEPT);
444 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
448 struct inpcb *inp = sotoinpcb(so);
449 struct tcpcb *tp = NULL;
452 if (so->so_state & SS_ISDISCONNECTED) {
453 error = ECONNABORTED;
462 in6_mapped_peeraddr(so, nam);
463 COMMON_END(PRU_ACCEPT);
467 * Mark the connection as being incapable of further output.
470 tcp_usr_shutdown(struct socket *so)
474 struct inpcb *inp = sotoinpcb(so);
479 tp = tcp_usrclosed(tp);
481 error = tcp_output(tp);
482 COMMON_END(PRU_SHUTDOWN);
486 * After a receive, possibly send window update to peer.
489 tcp_usr_rcvd(struct socket *so, int flags)
493 struct inpcb *inp = sotoinpcb(so);
498 COMMON_END(PRU_RCVD);
502 * Do a send by putting data in output queue and updating urgent
503 * marker if URG set. Possibly send more data. Unlike the other
504 * pru_*() routines, the mbuf chains are our responsibility. We
505 * must either enqueue them or free them. The other pru_* routines
506 * generally are caller-frees.
509 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
510 struct sockaddr *nam, struct mbuf *control, struct thread *td)
514 struct inpcb *inp = sotoinpcb(so);
523 * OOPS! we lost a race, the TCP session got reset after
524 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
525 * network interrupt in the non-splnet() section of sosend().
531 error = ECONNRESET; /* XXX EPIPE? */
537 isipv6 = nam && nam->sa_family == AF_INET6;
542 /* TCP doesn't do control messages (rights, creds, etc) */
543 if (control->m_len) {
550 m_freem(control); /* empty control, just free it */
552 if(!(flags & PRUS_OOB)) {
553 sbappend(&so->so_snd, m);
554 if (nam && tp->t_state < TCPS_SYN_SENT) {
556 * Do implied connect if not yet connected,
557 * initialize window to default value, and
558 * initialize maxseg/maxopd using peer's cached
563 error = tcp6_connect(tp, nam, td);
566 error = tcp_connect(tp, nam, td);
569 tp->snd_wnd = TTCP_CLIENT_SND_WND;
573 if (flags & PRUS_EOF) {
575 * Close the send side of the connection after
579 tp = tcp_usrclosed(tp);
582 if (flags & PRUS_MORETOCOME)
583 tp->t_flags |= TF_MORETOCOME;
584 error = tcp_output(tp);
585 if (flags & PRUS_MORETOCOME)
586 tp->t_flags &= ~TF_MORETOCOME;
589 if (sbspace(&so->so_snd) < -512) {
595 * According to RFC961 (Assigned Protocols),
596 * the urgent pointer points to the last octet
597 * of urgent data. We continue, however,
598 * to consider it to indicate the first octet
599 * of data past the urgent section.
600 * Otherwise, snd_up should be one lower.
602 sbappend(&so->so_snd, m);
603 if (nam && tp->t_state < TCPS_SYN_SENT) {
605 * Do implied connect if not yet connected,
606 * initialize window to default value, and
607 * initialize maxseg/maxopd using peer's cached
612 error = tcp6_connect(tp, nam, td);
615 error = tcp_connect(tp, nam, td);
618 tp->snd_wnd = TTCP_CLIENT_SND_WND;
621 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
623 error = tcp_output(tp);
626 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
627 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
634 tcp_usr_abort(struct socket *so)
638 struct inpcb *inp = sotoinpcb(so);
642 tp = tcp_drop(tp, ECONNABORTED);
643 COMMON_END(PRU_ABORT);
647 * Receive out-of-band data.
650 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
654 struct inpcb *inp = sotoinpcb(so);
658 if ((so->so_oobmark == 0 &&
659 (so->so_state & SS_RCVATMARK) == 0) ||
660 so->so_options & SO_OOBINLINE ||
661 tp->t_oobflags & TCPOOB_HADDATA) {
665 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
670 *mtod(m, caddr_t) = tp->t_iobc;
671 if ((flags & MSG_PEEK) == 0)
672 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
673 COMMON_END(PRU_RCVOOB);
676 /* xxx - should be const */
677 struct pr_usrreqs tcp_usrreqs = {
678 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
679 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
680 tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
681 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
682 in_setsockaddr, sosend, soreceive, sopoll
686 struct pr_usrreqs tcp6_usrreqs = {
687 tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
688 tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
689 tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
690 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
691 in6_mapped_sockaddr, sosend, soreceive, sopoll
696 * Common subroutine to open a TCP connection to remote host specified
697 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
698 * port number if needed. Call in_pcbladdr to do the routing and to choose
699 * a local host address (interface). If there is an existing incarnation
700 * of the same connection in TIME-WAIT state and if the remote host was
701 * sending CC options and if the connection duration was < MSL, then
702 * truncate the previous TIME-WAIT state and proceed.
703 * Initialize connection parameters and enter SYN-SENT state.
706 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
708 struct inpcb *inp = tp->t_inpcb, *oinp;
709 struct socket *so = inp->inp_socket;
711 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
712 struct sockaddr_in *if_sin;
713 struct rmxp_tao *taop;
714 struct rmxp_tao tao_noncached;
717 if (inp->inp_lport == 0) {
718 error = in_pcbbind(inp, (struct sockaddr *)0, td);
724 * Cannot simply call in_pcbconnect, because there might be an
725 * earlier incarnation of this same connection still in
726 * TIME_WAIT state, creating an ADDRINUSE error.
728 error = in_pcbladdr(inp, nam, &if_sin);
731 oinp = in_pcblookup_hash(inp->inp_pcbinfo,
732 sin->sin_addr, sin->sin_port,
733 inp->inp_laddr.s_addr != INADDR_ANY ?
734 inp->inp_laddr : if_sin->sin_addr,
735 inp->inp_lport, 0, NULL);
737 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
738 otp->t_state == TCPS_TIME_WAIT &&
739 (ticks - otp->t_starttime) < tcp_msl &&
740 (otp->t_flags & TF_RCVD_CC))
741 (void) tcp_close(otp);
745 if (inp->inp_laddr.s_addr == INADDR_ANY)
746 inp->inp_laddr = if_sin->sin_addr;
747 inp->inp_faddr = sin->sin_addr;
748 inp->inp_fport = sin->sin_port;
749 in_pcbrembindhash(inp);
750 in_pcbinsconnhash(inp);
752 /* Compute window scaling to request. */
753 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
754 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
755 tp->request_r_scale++;
758 tcpstat.tcps_connattempt++;
759 tp->t_state = TCPS_SYN_SENT;
760 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
761 tp->iss = tcp_new_isn(tp);
762 tp->t_bw_rtseq = tp->iss;
766 * Generate a CC value for this connection and
767 * check whether CC or CCnew should be used.
769 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
770 taop = &tao_noncached;
771 bzero(taop, sizeof(*taop));
774 tp->cc_send = CC_INC(tcp_ccgen);
775 if (taop->tao_ccsent != 0 &&
776 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
777 taop->tao_ccsent = tp->cc_send;
779 taop->tao_ccsent = 0;
780 tp->t_flags |= TF_SENDCCNEW;
788 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
790 struct inpcb *inp = tp->t_inpcb, *oinp;
791 struct socket *so = inp->inp_socket;
793 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
794 struct in6_addr *addr6;
795 struct rmxp_tao *taop;
796 struct rmxp_tao tao_noncached;
799 if (inp->inp_lport == 0) {
800 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
806 * Cannot simply call in_pcbconnect, because there might be an
807 * earlier incarnation of this same connection still in
808 * TIME_WAIT state, creating an ADDRINUSE error.
810 error = in6_pcbladdr(inp, nam, &addr6);
813 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
814 &sin6->sin6_addr, sin6->sin6_port,
815 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
816 addr6 : &inp->in6p_laddr,
817 inp->inp_lport, 0, NULL);
819 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
820 otp->t_state == TCPS_TIME_WAIT &&
821 (ticks - otp->t_starttime) < tcp_msl &&
822 (otp->t_flags & TF_RCVD_CC))
823 otp = tcp_close(otp);
827 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
828 inp->in6p_laddr = *addr6;
829 inp->in6p_faddr = sin6->sin6_addr;
830 inp->inp_fport = sin6->sin6_port;
831 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
832 inp->in6p_flowinfo = sin6->sin6_flowinfo;
833 in_pcbrembindhash(inp);
834 in_pcbinsconnhash(inp);
836 /* Compute window scaling to request. */
837 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
838 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
839 tp->request_r_scale++;
842 tcpstat.tcps_connattempt++;
843 tp->t_state = TCPS_SYN_SENT;
844 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
845 tp->iss = tcp_new_isn(tp);
846 tp->t_bw_rtseq = tp->iss;
850 * Generate a CC value for this connection and
851 * check whether CC or CCnew should be used.
853 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
854 taop = &tao_noncached;
855 bzero(taop, sizeof(*taop));
858 tp->cc_send = CC_INC(tcp_ccgen);
859 if (taop->tao_ccsent != 0 &&
860 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
861 taop->tao_ccsent = tp->cc_send;
863 taop->tao_ccsent = 0;
864 tp->t_flags |= TF_SENDCCNEW;
872 * The new sockopt interface makes it possible for us to block in the
873 * copyin/out step (if we take a page fault). Taking a page fault at
874 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
875 * use TSM, there probably isn't any need for this function to run at
876 * splnet() any more. This needs more examination.)
879 tcp_ctloutput(so, sopt)
881 struct sockopt *sopt;
883 int error, opt, optval, s;
888 s = splnet(); /* XXX */
894 if (sopt->sopt_level != IPPROTO_TCP) {
896 if (INP_CHECK_SOCKAF(so, AF_INET6))
897 error = ip6_ctloutput(so, sopt);
900 error = ip_ctloutput(so, sopt);
906 switch (sopt->sopt_dir) {
908 switch (sopt->sopt_name) {
911 error = sooptcopyin(sopt, &optval, sizeof optval,
916 switch (sopt->sopt_name) {
924 opt = 0; /* dead code to fool gcc */
935 error = sooptcopyin(sopt, &optval, sizeof optval,
941 tp->t_flags |= TF_NOPUSH;
943 tp->t_flags &= ~TF_NOPUSH;
944 error = tcp_output(tp);
949 error = sooptcopyin(sopt, &optval, sizeof optval,
954 if (optval > 0 && optval <= tp->t_maxseg)
955 tp->t_maxseg = optval;
967 switch (sopt->sopt_name) {
969 optval = tp->t_flags & TF_NODELAY;
972 optval = tp->t_maxseg;
975 optval = tp->t_flags & TF_NOOPT;
978 optval = tp->t_flags & TF_NOPUSH;
985 error = sooptcopyout(sopt, &optval, sizeof optval);
993 * tcp_sendspace and tcp_recvspace are the default send and receive window
994 * sizes, respectively. These are obsolescent (this information should
995 * be set by the route).
997 u_long tcp_sendspace = 1024*32;
998 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
999 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1000 u_long tcp_recvspace = 57344; /* largest multiple of PAGE_SIZE < 64k */
1001 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1002 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1005 * Attach TCP protocol to socket, allocating
1006 * internet protocol control block, tcp control block,
1007 * bufer space, and entering LISTEN state if to accept connections.
1010 tcp_attach(struct socket *so, struct pru_attach_info *ai)
1016 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1019 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1020 error = soreserve(so, tcp_sendspace, tcp_recvspace,
1025 error = in_pcballoc(so, &tcbinfo[mycpu->gd_cpuid]);
1028 inp = sotoinpcb(so);
1031 inp->inp_vflag |= INP_IPV6;
1032 inp->in6p_hops = -1; /* use kernel default */
1036 inp->inp_vflag |= INP_IPV4;
1037 tp = tcp_newtcpcb(inp);
1039 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1041 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1048 so->so_state |= nofd;
1051 tp->t_state = TCPS_CLOSED;
1056 * Initiate (or continue) disconnect.
1057 * If embryonic state, just send reset (once).
1058 * If in ``let data drain'' option and linger null, just drop.
1059 * Otherwise (hard), mark socket disconnecting and drop
1060 * current input data; switch states based on user close, and
1061 * send segment to peer (with FIN).
1063 static struct tcpcb *
1067 struct socket *so = tp->t_inpcb->inp_socket;
1069 if (tp->t_state < TCPS_ESTABLISHED)
1071 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1072 tp = tcp_drop(tp, 0);
1074 soisdisconnecting(so);
1075 sbflush(&so->so_rcv);
1076 tp = tcp_usrclosed(tp);
1078 (void) tcp_output(tp);
1084 * User issued close, and wish to trail through shutdown states:
1085 * if never received SYN, just forget it. If got a SYN from peer,
1086 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1087 * If already got a FIN from peer, then almost done; go to LAST_ACK
1088 * state. In all other cases, have already sent FIN to peer (e.g.
1089 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1090 * for peer to send FIN or not respond to keep-alives, etc.
1091 * We can let the user exit from the close as soon as the FIN is acked.
1093 static struct tcpcb *
1098 switch (tp->t_state) {
1102 tp->t_state = TCPS_CLOSED;
1107 case TCPS_SYN_RECEIVED:
1108 tp->t_flags |= TF_NEEDFIN;
1111 case TCPS_ESTABLISHED:
1112 tp->t_state = TCPS_FIN_WAIT_1;
1115 case TCPS_CLOSE_WAIT:
1116 tp->t_state = TCPS_LAST_ACK;
1119 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1120 soisdisconnected(tp->t_inpcb->inp_socket);
1121 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1122 if (tp->t_state == TCPS_FIN_WAIT_2)
1123 callout_reset(tp->tt_2msl, tcp_maxidle,
1124 tcp_timer_2msl, tp);