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>
56 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
61 #include <netinet/ip6.h>
63 #include <netinet/in_pcb.h>
65 #include <netinet6/in6_pcb.h>
67 #include <netinet/in_var.h>
68 #include <netinet/ip_var.h>
70 #include <netinet6/ip6_var.h>
72 #include <netinet/tcp.h>
73 #include <netinet/tcp_fsm.h>
74 #include <netinet/tcp_seq.h>
75 #include <netinet/tcp_timer.h>
76 #include <netinet/tcp_var.h>
77 #include <netinet/tcpip.h>
79 #include <netinet/tcp_debug.h>
83 #include <netinet6/ipsec.h>
87 * TCP protocol interface to socket abstraction.
89 extern char *tcpstates[]; /* XXX ??? */
91 static int tcp_attach(struct socket *, struct thread *td);
92 static int tcp_connect(struct tcpcb *, struct sockaddr *,
95 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
99 tcp_disconnect(struct tcpcb *);
100 static struct tcpcb *
101 tcp_usrclosed(struct tcpcb *);
104 #define TCPDEBUG0 int ostate = 0
105 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
106 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
107 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
111 #define TCPDEBUG2(req)
115 * TCP attaches to socket via pru_attach(), reserving space,
116 * and an internet control block.
119 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
123 struct inpcb *inp = sotoinpcb(so);
124 struct tcpcb *tp = 0;
133 error = tcp_attach(so, td);
137 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
138 so->so_linger = TCP_LINGERTIME;
141 TCPDEBUG2(PRU_ATTACH);
147 * pru_detach() detaches the TCP protocol from the socket.
148 * If the protocol state is non-embryonic, then can't
149 * do this directly: have to initiate a pru_disconnect(),
150 * which may finish later; embryonic TCB's can just
154 tcp_usr_detach(struct socket *so)
158 struct inpcb *inp = sotoinpcb(so);
164 return EINVAL; /* XXX */
168 tp = tcp_disconnect(tp);
170 TCPDEBUG2(PRU_DETACH);
175 #define COMMON_START() TCPDEBUG0; \
181 tp = intotcpcb(inp); \
185 #define COMMON_END(req) out: TCPDEBUG2(req); splx(s); return error; goto out
189 * Give the socket an address.
192 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
196 struct inpcb *inp = sotoinpcb(so);
198 struct sockaddr_in *sinp;
203 * Must check for multicast addresses and disallow binding
206 sinp = (struct sockaddr_in *)nam;
207 if (sinp->sin_family == AF_INET &&
208 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
209 error = EAFNOSUPPORT;
212 error = in_pcbbind(inp, nam, td);
215 COMMON_END(PRU_BIND);
221 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
225 struct inpcb *inp = sotoinpcb(so);
227 struct sockaddr_in6 *sin6p;
232 * Must check for multicast addresses and disallow binding
235 sin6p = (struct sockaddr_in6 *)nam;
236 if (sin6p->sin6_family == AF_INET6 &&
237 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
238 error = EAFNOSUPPORT;
241 inp->inp_vflag &= ~INP_IPV4;
242 inp->inp_vflag |= INP_IPV6;
243 if (ip6_mapped_addr_on && (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
244 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
245 inp->inp_vflag |= INP_IPV4;
246 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
247 struct sockaddr_in sin;
249 in6_sin6_2_sin(&sin, sin6p);
250 inp->inp_vflag |= INP_IPV4;
251 inp->inp_vflag &= ~INP_IPV6;
252 error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
256 error = in6_pcbbind(inp, nam, td);
259 COMMON_END(PRU_BIND);
264 * Prepare to accept connections.
267 tcp_usr_listen(struct socket *so, struct thread *td)
271 struct inpcb *inp = sotoinpcb(so);
275 if (inp->inp_lport == 0)
276 error = in_pcbbind(inp, (struct sockaddr *)0, td);
278 tp->t_state = TCPS_LISTEN;
279 COMMON_END(PRU_LISTEN);
284 tcp6_usr_listen(struct socket *so, struct thread *td)
288 struct inpcb *inp = sotoinpcb(so);
292 if (inp->inp_lport == 0) {
293 inp->inp_vflag &= ~INP_IPV4;
294 if (ip6_mapped_addr_on &&
295 (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
296 inp->inp_vflag |= INP_IPV4;
297 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
300 tp->t_state = TCPS_LISTEN;
301 COMMON_END(PRU_LISTEN);
306 * Initiate connection to peer.
307 * Create a template for use in transmissions on this connection.
308 * Enter SYN_SENT state, and mark socket as connecting.
309 * Start keep-alive timer, and seed output sequence space.
310 * Send initial segment on connection.
313 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
317 struct inpcb *inp = sotoinpcb(so);
319 struct sockaddr_in *sinp;
324 * Must disallow TCP ``connections'' to multicast addresses.
326 sinp = (struct sockaddr_in *)nam;
327 if (sinp->sin_family == AF_INET
328 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
329 error = EAFNOSUPPORT;
333 if (td && jailed(td->td_ucred))
334 prison_remote_ip(td->td_ucred, 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 (!ip6_mapped_addr_on ||
368 (inp->inp_flags & IN6P_IPV6_V6ONLY))
371 in6_sin6_2_sin(&sin, sin6p);
372 inp->inp_vflag |= INP_IPV4;
373 inp->inp_vflag &= ~INP_IPV6;
374 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
376 error = tcp_output(tp);
379 inp->inp_vflag &= ~INP_IPV4;
380 inp->inp_vflag |= INP_IPV6;
381 inp->inp_inc.inc_isipv6 = 1;
382 if ((error = tcp6_connect(tp, nam, td)) != 0)
384 error = tcp_output(tp);
385 COMMON_END(PRU_CONNECT);
390 * Initiate disconnect from peer.
391 * If connection never passed embryonic stage, just drop;
392 * else if don't need to let data drain, then can just drop anyways,
393 * else have to begin TCP shutdown process: mark socket disconnecting,
394 * drain unread data, state switch to reflect user close, and
395 * send segment (e.g. FIN) to peer. Socket will be really disconnected
396 * when peer sends FIN and acks ours.
398 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
401 tcp_usr_disconnect(struct socket *so)
405 struct inpcb *inp = sotoinpcb(so);
409 tp = tcp_disconnect(tp);
410 COMMON_END(PRU_DISCONNECT);
414 * Accept a connection. Essentially all the work is
415 * done at higher levels; just return the address
416 * of the peer, storing through addr.
419 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
423 struct inpcb *inp = sotoinpcb(so);
424 struct tcpcb *tp = NULL;
427 if (so->so_state & SS_ISDISCONNECTED) {
428 error = ECONNABORTED;
437 in_setpeeraddr(so, nam);
438 COMMON_END(PRU_ACCEPT);
443 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
447 struct inpcb *inp = sotoinpcb(so);
448 struct tcpcb *tp = NULL;
451 if (so->so_state & SS_ISDISCONNECTED) {
452 error = ECONNABORTED;
461 in6_mapped_peeraddr(so, nam);
462 COMMON_END(PRU_ACCEPT);
466 * Mark the connection as being incapable of further output.
469 tcp_usr_shutdown(struct socket *so)
473 struct inpcb *inp = sotoinpcb(so);
478 tp = tcp_usrclosed(tp);
480 error = tcp_output(tp);
481 COMMON_END(PRU_SHUTDOWN);
485 * After a receive, possibly send window update to peer.
488 tcp_usr_rcvd(struct socket *so, int flags)
492 struct inpcb *inp = sotoinpcb(so);
497 COMMON_END(PRU_RCVD);
501 * Do a send by putting data in output queue and updating urgent
502 * marker if URG set. Possibly send more data. Unlike the other
503 * pru_*() routines, the mbuf chains are our responsibility. We
504 * must either enqueue them or free them. The other pru_* routines
505 * generally are caller-frees.
508 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
509 struct sockaddr *nam, struct mbuf *control, struct thread *td)
513 struct inpcb *inp = sotoinpcb(so);
522 * OOPS! we lost a race, the TCP session got reset after
523 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
524 * network interrupt in the non-splnet() section of sosend().
530 error = ECONNRESET; /* XXX EPIPE? */
536 isipv6 = nam && nam->sa_family == AF_INET6;
541 /* TCP doesn't do control messages (rights, creds, etc) */
542 if (control->m_len) {
549 m_freem(control); /* empty control, just free it */
551 if(!(flags & PRUS_OOB)) {
552 sbappend(&so->so_snd, m);
553 if (nam && tp->t_state < TCPS_SYN_SENT) {
555 * Do implied connect if not yet connected,
556 * initialize window to default value, and
557 * initialize maxseg/maxopd using peer's cached
562 error = tcp6_connect(tp, nam, td);
565 error = tcp_connect(tp, nam, td);
568 tp->snd_wnd = TTCP_CLIENT_SND_WND;
572 if (flags & PRUS_EOF) {
574 * Close the send side of the connection after
578 tp = tcp_usrclosed(tp);
581 if (flags & PRUS_MORETOCOME)
582 tp->t_flags |= TF_MORETOCOME;
583 error = tcp_output(tp);
584 if (flags & PRUS_MORETOCOME)
585 tp->t_flags &= ~TF_MORETOCOME;
588 if (sbspace(&so->so_snd) < -512) {
594 * According to RFC961 (Assigned Protocols),
595 * the urgent pointer points to the last octet
596 * of urgent data. We continue, however,
597 * to consider it to indicate the first octet
598 * of data past the urgent section.
599 * Otherwise, snd_up should be one lower.
601 sbappend(&so->so_snd, m);
602 if (nam && tp->t_state < TCPS_SYN_SENT) {
604 * Do implied connect if not yet connected,
605 * initialize window to default value, and
606 * initialize maxseg/maxopd using peer's cached
611 error = tcp6_connect(tp, nam, td);
614 error = tcp_connect(tp, nam, td);
617 tp->snd_wnd = TTCP_CLIENT_SND_WND;
620 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
622 error = tcp_output(tp);
625 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
626 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
633 tcp_usr_abort(struct socket *so)
637 struct inpcb *inp = sotoinpcb(so);
641 tp = tcp_drop(tp, ECONNABORTED);
642 COMMON_END(PRU_ABORT);
646 * Receive out-of-band data.
649 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
653 struct inpcb *inp = sotoinpcb(so);
657 if ((so->so_oobmark == 0 &&
658 (so->so_state & SS_RCVATMARK) == 0) ||
659 so->so_options & SO_OOBINLINE ||
660 tp->t_oobflags & TCPOOB_HADDATA) {
664 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
669 *mtod(m, caddr_t) = tp->t_iobc;
670 if ((flags & MSG_PEEK) == 0)
671 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
672 COMMON_END(PRU_RCVOOB);
675 /* xxx - should be const */
676 struct pr_usrreqs tcp_usrreqs = {
677 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
678 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
679 tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
680 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
681 in_setsockaddr, sosend, soreceive, sopoll
685 struct pr_usrreqs tcp6_usrreqs = {
686 tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
687 tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
688 tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
689 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
690 in6_mapped_sockaddr, sosend, soreceive, sopoll
695 * Common subroutine to open a TCP connection to remote host specified
696 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
697 * port number if needed. Call in_pcbladdr to do the routing and to choose
698 * a local host address (interface). If there is an existing incarnation
699 * of the same connection in TIME-WAIT state and if the remote host was
700 * sending CC options and if the connection duration was < MSL, then
701 * truncate the previous TIME-WAIT state and proceed.
702 * Initialize connection parameters and enter SYN-SENT state.
705 tcp_connect(tp, nam, td)
706 register struct tcpcb *tp;
707 struct sockaddr *nam;
710 struct inpcb *inp = tp->t_inpcb, *oinp;
711 struct socket *so = inp->inp_socket;
713 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
714 struct sockaddr_in *ifaddr;
715 struct rmxp_tao *taop;
716 struct rmxp_tao tao_noncached;
719 if (inp->inp_lport == 0) {
720 error = in_pcbbind(inp, (struct sockaddr *)0, td);
726 * Cannot simply call in_pcbconnect, because there might be an
727 * earlier incarnation of this same connection still in
728 * TIME_WAIT state, creating an ADDRINUSE error.
730 error = in_pcbladdr(inp, nam, &ifaddr);
733 oinp = in_pcblookup_hash(inp->inp_pcbinfo,
734 sin->sin_addr, sin->sin_port,
735 inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
737 inp->inp_lport, 0, NULL);
739 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
740 otp->t_state == TCPS_TIME_WAIT &&
741 (ticks - otp->t_starttime) < tcp_msl &&
742 (otp->t_flags & TF_RCVD_CC))
743 otp = tcp_close(otp);
747 if (inp->inp_laddr.s_addr == INADDR_ANY)
748 inp->inp_laddr = ifaddr->sin_addr;
749 inp->inp_faddr = sin->sin_addr;
750 inp->inp_fport = sin->sin_port;
753 /* Compute window scaling to request. */
754 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
755 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
756 tp->request_r_scale++;
759 tcpstat.tcps_connattempt++;
760 tp->t_state = TCPS_SYN_SENT;
761 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
762 tp->iss = tcp_new_isn(tp);
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(tp, nam, td)
789 register struct tcpcb *tp;
790 struct sockaddr *nam;
793 struct inpcb *inp = tp->t_inpcb, *oinp;
794 struct socket *so = inp->inp_socket;
796 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
797 struct in6_addr *addr6;
798 struct rmxp_tao *taop;
799 struct rmxp_tao tao_noncached;
802 if (inp->inp_lport == 0) {
803 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
809 * Cannot simply call in_pcbconnect, because there might be an
810 * earlier incarnation of this same connection still in
811 * TIME_WAIT state, creating an ADDRINUSE error.
813 error = in6_pcbladdr(inp, nam, &addr6);
816 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
817 &sin6->sin6_addr, sin6->sin6_port,
818 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
821 inp->inp_lport, 0, NULL);
823 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
824 otp->t_state == TCPS_TIME_WAIT &&
825 (ticks - otp->t_starttime) < tcp_msl &&
826 (otp->t_flags & TF_RCVD_CC))
827 otp = tcp_close(otp);
831 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
832 inp->in6p_laddr = *addr6;
833 inp->in6p_faddr = sin6->sin6_addr;
834 inp->inp_fport = sin6->sin6_port;
835 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
836 inp->in6p_flowinfo = sin6->sin6_flowinfo;
839 /* Compute window scaling to request. */
840 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
841 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
842 tp->request_r_scale++;
845 tcpstat.tcps_connattempt++;
846 tp->t_state = TCPS_SYN_SENT;
847 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
848 tp->iss = tcp_new_isn(tp);
852 * Generate a CC value for this connection and
853 * check whether CC or CCnew should be used.
855 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
856 taop = &tao_noncached;
857 bzero(taop, sizeof(*taop));
860 tp->cc_send = CC_INC(tcp_ccgen);
861 if (taop->tao_ccsent != 0 &&
862 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
863 taop->tao_ccsent = tp->cc_send;
865 taop->tao_ccsent = 0;
866 tp->t_flags |= TF_SENDCCNEW;
874 * The new sockopt interface makes it possible for us to block in the
875 * copyin/out step (if we take a page fault). Taking a page fault at
876 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
877 * use TSM, there probably isn't any need for this function to run at
878 * splnet() any more. This needs more examination.)
881 tcp_ctloutput(so, sopt)
883 struct sockopt *sopt;
885 int error, opt, optval, s;
890 s = splnet(); /* XXX */
896 if (sopt->sopt_level != IPPROTO_TCP) {
898 if (INP_CHECK_SOCKAF(so, AF_INET6))
899 error = ip6_ctloutput(so, sopt);
902 error = ip_ctloutput(so, sopt);
908 switch (sopt->sopt_dir) {
910 switch (sopt->sopt_name) {
913 error = sooptcopyin(sopt, &optval, sizeof optval,
918 switch (sopt->sopt_name) {
926 opt = 0; /* dead code to fool gcc */
937 error = sooptcopyin(sopt, &optval, sizeof optval,
943 tp->t_flags |= TF_NOPUSH;
945 tp->t_flags &= ~TF_NOPUSH;
946 error = tcp_output(tp);
951 error = sooptcopyin(sopt, &optval, sizeof optval,
956 if (optval > 0 && optval <= tp->t_maxseg)
957 tp->t_maxseg = optval;
969 switch (sopt->sopt_name) {
971 optval = tp->t_flags & TF_NODELAY;
974 optval = tp->t_maxseg;
977 optval = tp->t_flags & TF_NOOPT;
980 optval = tp->t_flags & TF_NOPUSH;
987 error = sooptcopyout(sopt, &optval, sizeof optval);
995 * tcp_sendspace and tcp_recvspace are the default send and receive window
996 * sizes, respectively. These are obsolescent (this information should
997 * be set by the route).
999 u_long tcp_sendspace = 1024*32;
1000 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1001 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1002 u_long tcp_recvspace = 1024*64;
1003 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1004 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1007 * Attach TCP protocol to socket, allocating
1008 * internet protocol control block, tcp control block,
1009 * bufer space, and entering LISTEN state if to accept connections.
1016 register struct tcpcb *tp;
1020 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1023 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1024 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1028 error = in_pcballoc(so, &tcbinfo, td);
1031 inp = sotoinpcb(so);
1034 inp->inp_vflag |= INP_IPV6;
1035 inp->in6p_hops = -1; /* use kernel default */
1039 inp->inp_vflag |= INP_IPV4;
1040 tp = tcp_newtcpcb(inp);
1042 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1044 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1051 so->so_state |= nofd;
1054 tp->t_state = TCPS_CLOSED;
1059 * Initiate (or continue) disconnect.
1060 * If embryonic state, just send reset (once).
1061 * If in ``let data drain'' option and linger null, just drop.
1062 * Otherwise (hard), mark socket disconnecting and drop
1063 * current input data; switch states based on user close, and
1064 * send segment to peer (with FIN).
1066 static struct tcpcb *
1068 register struct tcpcb *tp;
1070 struct socket *so = tp->t_inpcb->inp_socket;
1072 if (tp->t_state < TCPS_ESTABLISHED)
1074 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1075 tp = tcp_drop(tp, 0);
1077 soisdisconnecting(so);
1078 sbflush(&so->so_rcv);
1079 tp = tcp_usrclosed(tp);
1081 (void) tcp_output(tp);
1087 * User issued close, and wish to trail through shutdown states:
1088 * if never received SYN, just forget it. If got a SYN from peer,
1089 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1090 * If already got a FIN from peer, then almost done; go to LAST_ACK
1091 * state. In all other cases, have already sent FIN to peer (e.g.
1092 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1093 * for peer to send FIN or not respond to keep-alives, etc.
1094 * We can let the user exit from the close as soon as the FIN is acked.
1096 static struct tcpcb *
1098 register struct tcpcb *tp;
1101 switch (tp->t_state) {
1105 tp->t_state = TCPS_CLOSED;
1110 case TCPS_SYN_RECEIVED:
1111 tp->t_flags |= TF_NEEDFIN;
1114 case TCPS_ESTABLISHED:
1115 tp->t_state = TCPS_FIN_WAIT_1;
1118 case TCPS_CLOSE_WAIT:
1119 tp->t_state = TCPS_LAST_ACK;
1122 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1123 soisdisconnected(tp->t_inpcb->inp_socket);
1124 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1125 if (tp->t_state == TCPS_FIN_WAIT_2)
1126 callout_reset(tp->tt_2msl, tcp_maxidle,
1127 tcp_timer_2msl, tp);