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) \
110 if (so->so_options & SO_DEBUG) { \
112 tcp_trace(TA_USER, ostate, tp, 0, 0, req); \
120 #define TCPDEBUG2(req)
124 * TCP attaches to socket via pru_attach(), reserving space,
125 * and an internet control block.
128 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
132 struct inpcb *inp = sotoinpcb(so);
133 struct tcpcb *tp = 0;
142 error = tcp_attach(so, td);
147 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
148 so->so_linger = TCP_LINGERTIME;
152 TCPDEBUG2(PRU_ATTACH);
158 * pru_detach() detaches the TCP protocol from the socket.
159 * If the protocol state is non-embryonic, then can't
160 * do this directly: have to initiate a pru_disconnect(),
161 * which may finish later; embryonic TCB's can just
165 tcp_usr_detach(struct socket *so)
169 struct inpcb *inp = sotoinpcb(so);
175 return EINVAL; /* XXX */
179 tp = tcp_disconnect(tp);
181 TCPDEBUG2(PRU_DETACH);
186 #define COMMON_START() TCPDEBUG0; \
192 tp = intotcpcb(inp); \
196 #define COMMON_END(req) out: TCPDEBUG2(req); splx(s); return error; goto out
200 * Give the socket an address.
203 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
207 struct inpcb *inp = sotoinpcb(so);
209 struct sockaddr_in *sinp;
214 * Must check for multicast addresses and disallow binding
217 sinp = (struct sockaddr_in *)nam;
218 if (sinp->sin_family == AF_INET &&
219 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
220 error = EAFNOSUPPORT;
223 error = in_pcbbind(inp, nam, td);
226 COMMON_END(PRU_BIND);
232 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
236 struct inpcb *inp = sotoinpcb(so);
238 struct sockaddr_in6 *sin6p;
243 * Must check for multicast addresses and disallow binding
246 sin6p = (struct sockaddr_in6 *)nam;
247 if (sin6p->sin6_family == AF_INET6 &&
248 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
249 error = EAFNOSUPPORT;
252 inp->inp_vflag &= ~INP_IPV4;
253 inp->inp_vflag |= INP_IPV6;
254 if (ip6_mapped_addr_on && (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
255 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
256 inp->inp_vflag |= INP_IPV4;
257 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
258 struct sockaddr_in sin;
260 in6_sin6_2_sin(&sin, sin6p);
261 inp->inp_vflag |= INP_IPV4;
262 inp->inp_vflag &= ~INP_IPV6;
263 error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
267 error = in6_pcbbind(inp, nam, td);
270 COMMON_END(PRU_BIND);
275 * Prepare to accept connections.
278 tcp_usr_listen(struct socket *so, struct thread *td)
282 struct inpcb *inp = sotoinpcb(so);
286 if (inp->inp_lport == 0)
287 error = in_pcbbind(inp, (struct sockaddr *)0, td);
289 tp->t_state = TCPS_LISTEN;
290 COMMON_END(PRU_LISTEN);
295 tcp6_usr_listen(struct socket *so, struct thread *td)
299 struct inpcb *inp = sotoinpcb(so);
303 if (inp->inp_lport == 0) {
304 inp->inp_vflag &= ~INP_IPV4;
305 if (ip6_mapped_addr_on &&
306 (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
307 inp->inp_vflag |= INP_IPV4;
308 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
311 tp->t_state = TCPS_LISTEN;
312 COMMON_END(PRU_LISTEN);
317 * Initiate connection to peer.
318 * Create a template for use in transmissions on this connection.
319 * Enter SYN_SENT state, and mark socket as connecting.
320 * Start keep-alive timer, and seed output sequence space.
321 * Send initial segment on connection.
324 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
328 struct inpcb *inp = sotoinpcb(so);
330 struct sockaddr_in *sinp;
335 * Must disallow TCP ``connections'' to multicast addresses.
337 sinp = (struct sockaddr_in *)nam;
338 if (sinp->sin_family == AF_INET
339 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
340 error = EAFNOSUPPORT;
344 if (td && jailed(td->td_ucred))
345 prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
347 if ((error = tcp_connect(tp, nam, td)) != 0)
349 error = tcp_output(tp);
350 COMMON_END(PRU_CONNECT);
355 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
359 struct inpcb *inp = sotoinpcb(so);
361 struct sockaddr_in6 *sin6p;
366 * Must disallow TCP ``connections'' to multicast addresses.
368 sin6p = (struct sockaddr_in6 *)nam;
369 if (sin6p->sin6_family == AF_INET6
370 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
371 error = EAFNOSUPPORT;
375 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
376 struct sockaddr_in sin;
378 if (!ip6_mapped_addr_on ||
379 (inp->inp_flags & IN6P_IPV6_V6ONLY))
382 in6_sin6_2_sin(&sin, sin6p);
383 inp->inp_vflag |= INP_IPV4;
384 inp->inp_vflag &= ~INP_IPV6;
385 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
387 error = tcp_output(tp);
390 inp->inp_vflag &= ~INP_IPV4;
391 inp->inp_vflag |= INP_IPV6;
392 inp->inp_inc.inc_isipv6 = 1;
393 if ((error = tcp6_connect(tp, nam, td)) != 0)
395 error = tcp_output(tp);
396 COMMON_END(PRU_CONNECT);
401 * Initiate disconnect from peer.
402 * If connection never passed embryonic stage, just drop;
403 * else if don't need to let data drain, then can just drop anyways,
404 * else have to begin TCP shutdown process: mark socket disconnecting,
405 * drain unread data, state switch to reflect user close, and
406 * send segment (e.g. FIN) to peer. Socket will be really disconnected
407 * when peer sends FIN and acks ours.
409 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
412 tcp_usr_disconnect(struct socket *so)
416 struct inpcb *inp = sotoinpcb(so);
420 tp = tcp_disconnect(tp);
421 COMMON_END(PRU_DISCONNECT);
425 * Accept a connection. Essentially all the work is
426 * done at higher levels; just return the address
427 * of the peer, storing through addr.
430 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
434 struct inpcb *inp = sotoinpcb(so);
435 struct tcpcb *tp = NULL;
439 if (so->so_state & SS_ISDISCONNECTED) {
441 error = ECONNABORTED;
451 in_setpeeraddr(so, nam);
452 COMMON_END(PRU_ACCEPT);
457 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
461 struct inpcb *inp = sotoinpcb(so);
462 struct tcpcb *tp = NULL;
466 if (so->so_state & SS_ISDISCONNECTED) {
468 error = ECONNABORTED;
478 in6_mapped_peeraddr(so, nam);
479 COMMON_END(PRU_ACCEPT);
483 * Mark the connection as being incapable of further output.
486 tcp_usr_shutdown(struct socket *so)
490 struct inpcb *inp = sotoinpcb(so);
495 tp = tcp_usrclosed(tp);
497 error = tcp_output(tp);
498 COMMON_END(PRU_SHUTDOWN);
502 * After a receive, possibly send window update to peer.
505 tcp_usr_rcvd(struct socket *so, int flags)
509 struct inpcb *inp = sotoinpcb(so);
514 COMMON_END(PRU_RCVD);
518 * Do a send by putting data in output queue and updating urgent
519 * marker if URG set. Possibly send more data. Unlike the other
520 * pru_*() routines, the mbuf chains are our responsibility. We
521 * must either enqueue them or free them. The other pru_* routines
522 * generally are caller-frees.
525 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
526 struct sockaddr *nam, struct mbuf *control, struct thread *td)
530 struct inpcb *inp = sotoinpcb(so);
539 * OOPS! we lost a race, the TCP session got reset after
540 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
541 * network interrupt in the non-splnet() section of sosend().
547 error = ECONNRESET; /* XXX EPIPE? */
553 isipv6 = nam && nam->sa_family == AF_INET6;
558 /* TCP doesn't do control messages (rights, creds, etc) */
559 if (control->m_len) {
566 m_freem(control); /* empty control, just free it */
568 if(!(flags & PRUS_OOB)) {
569 sbappend(&so->so_snd, m);
570 if (nam && tp->t_state < TCPS_SYN_SENT) {
572 * Do implied connect if not yet connected,
573 * initialize window to default value, and
574 * initialize maxseg/maxopd using peer's cached
579 error = tcp6_connect(tp, nam, td);
582 error = tcp_connect(tp, nam, td);
585 tp->snd_wnd = TTCP_CLIENT_SND_WND;
589 if (flags & PRUS_EOF) {
591 * Close the send side of the connection after
595 tp = tcp_usrclosed(tp);
598 if (flags & PRUS_MORETOCOME)
599 tp->t_flags |= TF_MORETOCOME;
600 error = tcp_output(tp);
601 if (flags & PRUS_MORETOCOME)
602 tp->t_flags &= ~TF_MORETOCOME;
605 if (sbspace(&so->so_snd) < -512) {
611 * According to RFC961 (Assigned Protocols),
612 * the urgent pointer points to the last octet
613 * of urgent data. We continue, however,
614 * to consider it to indicate the first octet
615 * of data past the urgent section.
616 * Otherwise, snd_up should be one lower.
618 sbappend(&so->so_snd, m);
619 if (nam && tp->t_state < TCPS_SYN_SENT) {
621 * Do implied connect if not yet connected,
622 * initialize window to default value, and
623 * initialize maxseg/maxopd using peer's cached
628 error = tcp6_connect(tp, nam, td);
631 error = tcp_connect(tp, nam, td);
634 tp->snd_wnd = TTCP_CLIENT_SND_WND;
637 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
639 error = tcp_output(tp);
642 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
643 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
650 tcp_usr_abort(struct socket *so)
654 struct inpcb *inp = sotoinpcb(so);
658 tp = tcp_drop(tp, ECONNABORTED);
659 COMMON_END(PRU_ABORT);
663 * Receive out-of-band data.
666 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
670 struct inpcb *inp = sotoinpcb(so);
675 if ((so->so_oobmark == 0 &&
676 (so->so_state & SS_RCVATMARK) == 0) ||
677 so->so_options & SO_OOBINLINE) {
683 if (tp->t_oobflags & TCPOOB_HADDATA) {
687 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
692 *mtod(m, caddr_t) = tp->t_iobc;
693 if ((flags & MSG_PEEK) == 0)
694 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
695 COMMON_END(PRU_RCVOOB);
698 /* xxx - should be const */
699 struct pr_usrreqs tcp_usrreqs = {
700 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
701 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
702 tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
703 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
704 in_setsockaddr, sosend, soreceive, sopoll
708 struct pr_usrreqs tcp6_usrreqs = {
709 tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
710 tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
711 tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
712 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
713 in6_mapped_sockaddr, sosend, soreceive, sopoll
718 * Common subroutine to open a TCP connection to remote host specified
719 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
720 * port number if needed. Call in_pcbladdr to do the routing and to choose
721 * a local host address (interface). If there is an existing incarnation
722 * of the same connection in TIME-WAIT state and if the remote host was
723 * sending CC options and if the connection duration was < MSL, then
724 * truncate the previous TIME-WAIT state and proceed.
725 * Initialize connection parameters and enter SYN-SENT state.
728 tcp_connect(tp, nam, td)
729 register struct tcpcb *tp;
730 struct sockaddr *nam;
733 struct inpcb *inp = tp->t_inpcb, *oinp;
734 struct socket *so = inp->inp_socket;
736 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
737 struct sockaddr_in *ifaddr;
738 struct rmxp_tao *taop;
739 struct rmxp_tao tao_noncached;
742 if (inp->inp_lport == 0) {
743 error = in_pcbbind(inp, (struct sockaddr *)0, td);
749 * Cannot simply call in_pcbconnect, because there might be an
750 * earlier incarnation of this same connection still in
751 * TIME_WAIT state, creating an ADDRINUSE error.
753 error = in_pcbladdr(inp, nam, &ifaddr);
756 oinp = in_pcblookup_hash(inp->inp_pcbinfo,
757 sin->sin_addr, sin->sin_port,
758 inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
760 inp->inp_lport, 0, NULL);
762 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
763 otp->t_state == TCPS_TIME_WAIT &&
764 (ticks - otp->t_starttime) < tcp_msl &&
765 (otp->t_flags & TF_RCVD_CC))
766 otp = tcp_close(otp);
770 if (inp->inp_laddr.s_addr == INADDR_ANY)
771 inp->inp_laddr = ifaddr->sin_addr;
772 inp->inp_faddr = sin->sin_addr;
773 inp->inp_fport = sin->sin_port;
776 /* Compute window scaling to request. */
777 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
778 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
779 tp->request_r_scale++;
784 tcpstat.tcps_connattempt++;
785 tp->t_state = TCPS_SYN_SENT;
786 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
787 tp->iss = tcp_new_isn(tp);
791 * Generate a CC value for this connection and
792 * check whether CC or CCnew should be used.
794 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
795 taop = &tao_noncached;
796 bzero(taop, sizeof(*taop));
799 tp->cc_send = CC_INC(tcp_ccgen);
800 if (taop->tao_ccsent != 0 &&
801 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
802 taop->tao_ccsent = tp->cc_send;
804 taop->tao_ccsent = 0;
805 tp->t_flags |= TF_SENDCCNEW;
813 tcp6_connect(tp, nam, td)
814 register struct tcpcb *tp;
815 struct sockaddr *nam;
818 struct inpcb *inp = tp->t_inpcb, *oinp;
819 struct socket *so = inp->inp_socket;
821 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
822 struct in6_addr *addr6;
823 struct rmxp_tao *taop;
824 struct rmxp_tao tao_noncached;
827 if (inp->inp_lport == 0) {
828 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
834 * Cannot simply call in_pcbconnect, because there might be an
835 * earlier incarnation of this same connection still in
836 * TIME_WAIT state, creating an ADDRINUSE error.
838 error = in6_pcbladdr(inp, nam, &addr6);
841 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
842 &sin6->sin6_addr, sin6->sin6_port,
843 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
846 inp->inp_lport, 0, NULL);
848 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
849 otp->t_state == TCPS_TIME_WAIT &&
850 (ticks - otp->t_starttime) < tcp_msl &&
851 (otp->t_flags & TF_RCVD_CC))
852 otp = tcp_close(otp);
856 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
857 inp->in6p_laddr = *addr6;
858 inp->in6p_faddr = sin6->sin6_addr;
859 inp->inp_fport = sin6->sin6_port;
860 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
861 inp->in6p_flowinfo = sin6->sin6_flowinfo;
864 /* Compute window scaling to request. */
865 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
866 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
867 tp->request_r_scale++;
872 tcpstat.tcps_connattempt++;
873 tp->t_state = TCPS_SYN_SENT;
874 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
875 tp->iss = tcp_new_isn(tp);
879 * Generate a CC value for this connection and
880 * check whether CC or CCnew should be used.
882 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
883 taop = &tao_noncached;
884 bzero(taop, sizeof(*taop));
887 tp->cc_send = CC_INC(tcp_ccgen);
888 if (taop->tao_ccsent != 0 &&
889 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
890 taop->tao_ccsent = tp->cc_send;
892 taop->tao_ccsent = 0;
893 tp->t_flags |= TF_SENDCCNEW;
901 * The new sockopt interface makes it possible for us to block in the
902 * copyin/out step (if we take a page fault). Taking a page fault at
903 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
904 * use TSM, there probably isn't any need for this function to run at
905 * splnet() any more. This needs more examination.)
908 tcp_ctloutput(so, sopt)
910 struct sockopt *sopt;
912 int error, opt, optval, s;
917 s = splnet(); /* XXX */
923 if (sopt->sopt_level != IPPROTO_TCP) {
925 if (INP_CHECK_SOCKAF(so, AF_INET6))
926 error = ip6_ctloutput(so, sopt);
929 error = ip_ctloutput(so, sopt);
935 switch (sopt->sopt_dir) {
937 switch (sopt->sopt_name) {
940 error = sooptcopyin(sopt, &optval, sizeof optval,
945 switch (sopt->sopt_name) {
953 opt = 0; /* dead code to fool gcc */
964 error = sooptcopyin(sopt, &optval, sizeof optval,
970 tp->t_flags |= TF_NOPUSH;
972 tp->t_flags &= ~TF_NOPUSH;
973 error = tcp_output(tp);
978 error = sooptcopyin(sopt, &optval, sizeof optval,
983 if (optval > 0 && optval <= tp->t_maxseg)
984 tp->t_maxseg = optval;
996 switch (sopt->sopt_name) {
998 optval = tp->t_flags & TF_NODELAY;
1001 optval = tp->t_maxseg;
1004 optval = tp->t_flags & TF_NOOPT;
1007 optval = tp->t_flags & TF_NOPUSH;
1010 error = ENOPROTOOPT;
1014 error = sooptcopyout(sopt, &optval, sizeof optval);
1022 * tcp_sendspace and tcp_recvspace are the default send and receive window
1023 * sizes, respectively. These are obsolescent (this information should
1024 * be set by the route).
1026 u_long tcp_sendspace = 1024*32;
1027 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1028 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1029 u_long tcp_recvspace = 1024*64;
1030 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1031 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1034 * Attach TCP protocol to socket, allocating
1035 * internet protocol control block, tcp control block,
1036 * bufer space, and entering LISTEN state if to accept connections.
1043 register struct tcpcb *tp;
1047 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1050 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1051 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1055 error = in_pcballoc(so, &tcbinfo, td);
1058 inp = sotoinpcb(so);
1061 inp->inp_vflag |= INP_IPV6;
1062 inp->in6p_hops = -1; /* use kernel default */
1066 inp->inp_vflag |= INP_IPV4;
1067 tp = tcp_newtcpcb(inp);
1072 nofd = so->so_state & SS_NOFDREF; /* XXX */
1073 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1082 so->so_state |= nofd;
1086 tp->t_state = TCPS_CLOSED;
1091 * Initiate (or continue) disconnect.
1092 * If embryonic state, just send reset (once).
1093 * If in ``let data drain'' option and linger null, just drop.
1094 * Otherwise (hard), mark socket disconnecting and drop
1095 * current input data; switch states based on user close, and
1096 * send segment to peer (with FIN).
1098 static struct tcpcb *
1100 register struct tcpcb *tp;
1102 struct socket *so = tp->t_inpcb->inp_socket;
1104 if (tp->t_state < TCPS_ESTABLISHED)
1108 if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1110 tp = tcp_drop(tp, 0);
1112 soisdisconnecting(so);
1114 sbflush(&so->so_rcv);
1115 tp = tcp_usrclosed(tp);
1117 (void) tcp_output(tp);
1124 * User issued close, and wish to trail through shutdown states:
1125 * if never received SYN, just forget it. If got a SYN from peer,
1126 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1127 * If already got a FIN from peer, then almost done; go to LAST_ACK
1128 * state. In all other cases, have already sent FIN to peer (e.g.
1129 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1130 * for peer to send FIN or not respond to keep-alives, etc.
1131 * We can let the user exit from the close as soon as the FIN is acked.
1133 static struct tcpcb *
1135 register struct tcpcb *tp;
1138 switch (tp->t_state) {
1142 tp->t_state = TCPS_CLOSED;
1147 case TCPS_SYN_RECEIVED:
1148 tp->t_flags |= TF_NEEDFIN;
1151 case TCPS_ESTABLISHED:
1152 tp->t_state = TCPS_FIN_WAIT_1;
1155 case TCPS_CLOSE_WAIT:
1156 tp->t_state = TCPS_LAST_ACK;
1159 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1160 SOCK_LOCK(tp->t_inpcb->inp_socket);
1161 soisdisconnected(tp->t_inpcb->inp_socket);
1162 SOCK_UNLOCK(tp->t_inpcb->inp_socket);
1163 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1164 if (tp->t_state == TCPS_FIN_WAIT_2)
1165 callout_reset(tp->tt_2msl, tcp_maxidle,
1166 tcp_timer_2msl, tp);