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.11 2004/04/05 17:47:01 dillon 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 in_pcbinswildcardhash(inp);
283 COMMON_END(PRU_LISTEN);
288 tcp6_usr_listen(struct socket *so, struct thread *td)
292 struct inpcb *inp = sotoinpcb(so);
296 if (inp->inp_lport == 0) {
297 inp->inp_vflag &= ~INP_IPV4;
298 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
299 inp->inp_vflag |= INP_IPV4;
300 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
303 tp->t_state = TCPS_LISTEN;
304 COMMON_END(PRU_LISTEN);
309 * Initiate connection to peer.
310 * Create a template for use in transmissions on this connection.
311 * Enter SYN_SENT state, and mark socket as connecting.
312 * Start keep-alive timer, and seed output sequence space.
313 * Send initial segment on connection.
316 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
320 struct inpcb *inp = sotoinpcb(so);
322 struct sockaddr_in *sinp;
327 * Must disallow TCP ``connections'' to multicast addresses.
329 sinp = (struct sockaddr_in *)nam;
330 if (sinp->sin_family == AF_INET
331 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
332 error = EAFNOSUPPORT;
336 prison_remote_ip(td, 0, &sinp->sin_addr.s_addr);
338 if ((error = tcp_connect(tp, nam, td)) != 0)
340 error = tcp_output(tp);
341 COMMON_END(PRU_CONNECT);
346 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
350 struct inpcb *inp = sotoinpcb(so);
352 struct sockaddr_in6 *sin6p;
357 * Must disallow TCP ``connections'' to multicast addresses.
359 sin6p = (struct sockaddr_in6 *)nam;
360 if (sin6p->sin6_family == AF_INET6
361 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
362 error = EAFNOSUPPORT;
366 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
367 struct sockaddr_in sin;
369 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
374 in6_sin6_2_sin(&sin, sin6p);
375 inp->inp_vflag |= INP_IPV4;
376 inp->inp_vflag &= ~INP_IPV6;
377 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
379 error = tcp_output(tp);
382 inp->inp_vflag &= ~INP_IPV4;
383 inp->inp_vflag |= INP_IPV6;
384 inp->inp_inc.inc_isipv6 = 1;
385 if ((error = tcp6_connect(tp, nam, td)) != 0)
387 error = tcp_output(tp);
388 COMMON_END(PRU_CONNECT);
393 * Initiate disconnect from peer.
394 * If connection never passed embryonic stage, just drop;
395 * else if don't need to let data drain, then can just drop anyways,
396 * else have to begin TCP shutdown process: mark socket disconnecting,
397 * drain unread data, state switch to reflect user close, and
398 * send segment (e.g. FIN) to peer. Socket will be really disconnected
399 * when peer sends FIN and acks ours.
401 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
404 tcp_usr_disconnect(struct socket *so)
408 struct inpcb *inp = sotoinpcb(so);
412 tp = tcp_disconnect(tp);
413 COMMON_END(PRU_DISCONNECT);
417 * Accept a connection. Essentially all the work is
418 * done at higher levels; just return the address
419 * of the peer, storing through addr.
422 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
426 struct inpcb *inp = sotoinpcb(so);
427 struct tcpcb *tp = NULL;
430 if (so->so_state & SS_ISDISCONNECTED) {
431 error = ECONNABORTED;
440 in_setpeeraddr(so, nam);
441 COMMON_END(PRU_ACCEPT);
446 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
450 struct inpcb *inp = sotoinpcb(so);
451 struct tcpcb *tp = NULL;
454 if (so->so_state & SS_ISDISCONNECTED) {
455 error = ECONNABORTED;
464 in6_mapped_peeraddr(so, nam);
465 COMMON_END(PRU_ACCEPT);
469 * Mark the connection as being incapable of further output.
472 tcp_usr_shutdown(struct socket *so)
476 struct inpcb *inp = sotoinpcb(so);
481 tp = tcp_usrclosed(tp);
483 error = tcp_output(tp);
484 COMMON_END(PRU_SHUTDOWN);
488 * After a receive, possibly send window update to peer.
491 tcp_usr_rcvd(struct socket *so, int flags)
495 struct inpcb *inp = sotoinpcb(so);
500 COMMON_END(PRU_RCVD);
504 * Do a send by putting data in output queue and updating urgent
505 * marker if URG set. Possibly send more data. Unlike the other
506 * pru_*() routines, the mbuf chains are our responsibility. We
507 * must either enqueue them or free them. The other pru_* routines
508 * generally are caller-frees.
511 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
512 struct sockaddr *nam, struct mbuf *control, struct thread *td)
516 struct inpcb *inp = sotoinpcb(so);
525 * OOPS! we lost a race, the TCP session got reset after
526 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
527 * network interrupt in the non-splnet() section of sosend().
533 error = ECONNRESET; /* XXX EPIPE? */
539 isipv6 = nam && nam->sa_family == AF_INET6;
544 /* TCP doesn't do control messages (rights, creds, etc) */
545 if (control->m_len) {
552 m_freem(control); /* empty control, just free it */
554 if(!(flags & PRUS_OOB)) {
555 sbappend(&so->so_snd, m);
556 if (nam && tp->t_state < TCPS_SYN_SENT) {
558 * Do implied connect if not yet connected,
559 * initialize window to default value, and
560 * initialize maxseg/maxopd using peer's cached
565 error = tcp6_connect(tp, nam, td);
568 error = tcp_connect(tp, nam, td);
571 tp->snd_wnd = TTCP_CLIENT_SND_WND;
575 if (flags & PRUS_EOF) {
577 * Close the send side of the connection after
581 tp = tcp_usrclosed(tp);
584 if (flags & PRUS_MORETOCOME)
585 tp->t_flags |= TF_MORETOCOME;
586 error = tcp_output(tp);
587 if (flags & PRUS_MORETOCOME)
588 tp->t_flags &= ~TF_MORETOCOME;
591 if (sbspace(&so->so_snd) < -512) {
597 * According to RFC961 (Assigned Protocols),
598 * the urgent pointer points to the last octet
599 * of urgent data. We continue, however,
600 * to consider it to indicate the first octet
601 * of data past the urgent section.
602 * Otherwise, snd_up should be one lower.
604 sbappend(&so->so_snd, m);
605 if (nam && tp->t_state < TCPS_SYN_SENT) {
607 * Do implied connect if not yet connected,
608 * initialize window to default value, and
609 * initialize maxseg/maxopd using peer's cached
614 error = tcp6_connect(tp, nam, td);
617 error = tcp_connect(tp, nam, td);
620 tp->snd_wnd = TTCP_CLIENT_SND_WND;
623 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
625 error = tcp_output(tp);
628 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
629 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
636 tcp_usr_abort(struct socket *so)
640 struct inpcb *inp = sotoinpcb(so);
644 tp = tcp_drop(tp, ECONNABORTED);
645 COMMON_END(PRU_ABORT);
649 * Receive out-of-band data.
652 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
656 struct inpcb *inp = sotoinpcb(so);
660 if ((so->so_oobmark == 0 &&
661 (so->so_state & SS_RCVATMARK) == 0) ||
662 so->so_options & SO_OOBINLINE ||
663 tp->t_oobflags & TCPOOB_HADDATA) {
667 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
672 *mtod(m, caddr_t) = tp->t_iobc;
673 if ((flags & MSG_PEEK) == 0)
674 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
675 COMMON_END(PRU_RCVOOB);
678 /* xxx - should be const */
679 struct pr_usrreqs tcp_usrreqs = {
680 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
681 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
682 tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
683 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
684 in_setsockaddr, sosend, soreceive, sopoll
688 struct pr_usrreqs tcp6_usrreqs = {
689 tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
690 tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
691 tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
692 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
693 in6_mapped_sockaddr, sosend, soreceive, sopoll
698 * Common subroutine to open a TCP connection to remote host specified
699 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
700 * port number if needed. Call in_pcbladdr to do the routing and to choose
701 * a local host address (interface). If there is an existing incarnation
702 * of the same connection in TIME-WAIT state and if the remote host was
703 * sending CC options and if the connection duration was < MSL, then
704 * truncate the previous TIME-WAIT state and proceed.
705 * Initialize connection parameters and enter SYN-SENT state.
708 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
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 *if_sin;
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, &if_sin);
733 oinp = in_pcblookup_hash(inp->inp_pcbinfo,
734 sin->sin_addr, sin->sin_port,
735 inp->inp_laddr.s_addr != INADDR_ANY ?
736 inp->inp_laddr : if_sin->sin_addr,
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 (void) tcp_close(otp);
747 if (inp->inp_laddr.s_addr == INADDR_ANY)
748 inp->inp_laddr = if_sin->sin_addr;
749 inp->inp_faddr = sin->sin_addr;
750 inp->inp_fport = sin->sin_port;
751 in_pcbinsconnhash(inp);
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);
763 tp->t_bw_rtseq = tp->iss;
767 * Generate a CC value for this connection and
768 * check whether CC or CCnew should be used.
770 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
771 taop = &tao_noncached;
772 bzero(taop, sizeof(*taop));
775 tp->cc_send = CC_INC(tcp_ccgen);
776 if (taop->tao_ccsent != 0 &&
777 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
778 taop->tao_ccsent = tp->cc_send;
780 taop->tao_ccsent = 0;
781 tp->t_flags |= TF_SENDCCNEW;
789 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
791 struct inpcb *inp = tp->t_inpcb, *oinp;
792 struct socket *so = inp->inp_socket;
794 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
795 struct in6_addr *addr6;
796 struct rmxp_tao *taop;
797 struct rmxp_tao tao_noncached;
800 if (inp->inp_lport == 0) {
801 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
807 * Cannot simply call in_pcbconnect, because there might be an
808 * earlier incarnation of this same connection still in
809 * TIME_WAIT state, creating an ADDRINUSE error.
811 error = in6_pcbladdr(inp, nam, &addr6);
814 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
815 &sin6->sin6_addr, sin6->sin6_port,
816 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
817 addr6 : &inp->in6p_laddr,
818 inp->inp_lport, 0, NULL);
820 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
821 otp->t_state == TCPS_TIME_WAIT &&
822 (ticks - otp->t_starttime) < tcp_msl &&
823 (otp->t_flags & TF_RCVD_CC))
824 otp = tcp_close(otp);
828 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
829 inp->in6p_laddr = *addr6;
830 inp->in6p_faddr = sin6->sin6_addr;
831 inp->inp_fport = sin6->sin6_port;
832 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
833 inp->in6p_flowinfo = sin6->sin6_flowinfo;
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)
1017 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1020 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1021 error = soreserve(so, tcp_sendspace, tcp_recvspace,
1026 #ifdef TCP_DISTRIBUTED_TCBINFO
1027 cpu = mycpu->gd_cpuid;
1031 error = in_pcballoc(so, &tcbinfo[cpu]);
1034 inp = sotoinpcb(so);
1037 inp->inp_vflag |= INP_IPV6;
1038 inp->in6p_hops = -1; /* use kernel default */
1042 inp->inp_vflag |= INP_IPV4;
1043 tp = tcp_newtcpcb(inp);
1045 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1047 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1054 so->so_state |= nofd;
1057 tp->t_state = TCPS_CLOSED;
1062 * Initiate (or continue) disconnect.
1063 * If embryonic state, just send reset (once).
1064 * If in ``let data drain'' option and linger null, just drop.
1065 * Otherwise (hard), mark socket disconnecting and drop
1066 * current input data; switch states based on user close, and
1067 * send segment to peer (with FIN).
1069 static struct tcpcb *
1073 struct socket *so = tp->t_inpcb->inp_socket;
1075 if (tp->t_state < TCPS_ESTABLISHED)
1077 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1078 tp = tcp_drop(tp, 0);
1080 soisdisconnecting(so);
1081 sbflush(&so->so_rcv);
1082 tp = tcp_usrclosed(tp);
1084 (void) tcp_output(tp);
1090 * User issued close, and wish to trail through shutdown states:
1091 * if never received SYN, just forget it. If got a SYN from peer,
1092 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1093 * If already got a FIN from peer, then almost done; go to LAST_ACK
1094 * state. In all other cases, have already sent FIN to peer (e.g.
1095 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1096 * for peer to send FIN or not respond to keep-alives, etc.
1097 * We can let the user exit from the close as soon as the FIN is acked.
1099 static struct tcpcb *
1104 switch (tp->t_state) {
1108 tp->t_state = TCPS_CLOSED;
1113 case TCPS_SYN_RECEIVED:
1114 tp->t_flags |= TF_NEEDFIN;
1117 case TCPS_ESTABLISHED:
1118 tp->t_state = TCPS_FIN_WAIT_1;
1121 case TCPS_CLOSE_WAIT:
1122 tp->t_state = TCPS_LAST_ACK;
1125 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1126 soisdisconnected(tp->t_inpcb->inp_socket);
1127 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1128 if (tp->t_state == TCPS_FIN_WAIT_2)
1129 callout_reset(tp->tt_2msl, tcp_maxidle,
1130 tcp_timer_2msl, tp);