]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/tcp_usrreq.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1988, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
55 * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.51.2.9 2001/08/22 00:59:12 silby Exp $
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/sysctl.h>
65 #include <sys/domain.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/protosw.h>
72 #include <net/route.h>
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
77 #include <netinet/ip6.h>
79 #include <netinet/in_pcb.h>
81 #include <netinet6/in6_pcb.h>
83 #include <netinet/in_var.h>
84 #include <netinet/ip_var.h>
86 #include <netinet6/ip6_var.h>
88 #include <netinet/tcp.h>
89 #include <netinet/tcp_fsm.h>
90 #include <netinet/tcp_seq.h>
91 #include <netinet/tcp_timer.h>
92 #include <netinet/tcp_var.h>
93 #include <netinet/tcpip.h>
95 #include <netinet/tcp_debug.h>
99 #include <netinet6/ipsec.h>
103 * TCP protocol interface to socket abstraction.
105 extern char *tcpstates
[]; /* XXX ??? */
107 static int tcp_attach
__P((struct socket
*, struct proc
*));
108 static int tcp_connect
__P((struct tcpcb
*, struct sockaddr
*,
111 static int tcp6_connect
__P((struct tcpcb
*, struct sockaddr
*,
114 static struct tcpcb
*
115 tcp_disconnect
__P((struct tcpcb
*));
116 static struct tcpcb
*
117 tcp_usrclosed
__P((struct tcpcb
*));
120 #define TCPDEBUG0 int ostate = 0
121 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
122 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
123 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
127 #define TCPDEBUG2(req)
131 * TCP attaches to socket via pru_attach(), reserving space,
132 * and an internet control block.
135 tcp_usr_attach(struct socket
*so
, int proto
, struct proc
*p
)
139 struct inpcb
*inp
= sotoinpcb(so
);
140 struct tcpcb
*tp
= 0;
149 error
= tcp_attach(so
, p
);
153 if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
154 so
->so_linger
= TCP_LINGERTIME
* hz
;
157 TCPDEBUG2(PRU_ATTACH
);
163 * pru_detach() detaches the TCP protocol from the socket.
164 * If the protocol state is non-embryonic, then can't
165 * do this directly: have to initiate a pru_disconnect(),
166 * which may finish later; embryonic TCB's can just
170 tcp_usr_detach(struct socket
*so
)
174 struct inpcb
*inp
= sotoinpcb(so
);
180 return EINVAL
; /* XXX */
183 /* In case we got disconnected from the peer */
187 tp
= tcp_disconnect(tp
);
189 TCPDEBUG2(PRU_DETACH
);
194 #define COMMON_START() TCPDEBUG0; \
200 tp = intotcpcb(inp); \
204 #define COMMON_END(req) out: TCPDEBUG2(req); splx(s); return error; goto out
208 * Give the socket an address.
211 tcp_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
215 struct inpcb
*inp
= sotoinpcb(so
);
217 struct sockaddr_in
*sinp
;
222 * Must check for multicast addresses and disallow binding
225 sinp
= (struct sockaddr_in
*)nam
;
226 if (sinp
->sin_family
== AF_INET
&&
227 IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
228 error
= EAFNOSUPPORT
;
231 error
= in_pcbbind(inp
, nam
, p
);
234 COMMON_END(PRU_BIND
);
240 tcp6_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
244 struct inpcb
*inp
= sotoinpcb(so
);
246 struct sockaddr_in6
*sin6p
;
251 * Must check for multicast addresses and disallow binding
254 sin6p
= (struct sockaddr_in6
*)nam
;
255 if (sin6p
->sin6_family
== AF_INET6
&&
256 IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
257 error
= EAFNOSUPPORT
;
260 inp
->inp_vflag
&= ~INP_IPV4
;
261 inp
->inp_vflag
|= INP_IPV6
;
262 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0) {
263 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p
->sin6_addr
))
264 inp
->inp_vflag
|= INP_IPV4
;
265 else if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
266 struct sockaddr_in sin
;
268 in6_sin6_2_sin(&sin
, sin6p
);
269 inp
->inp_vflag
|= INP_IPV4
;
270 inp
->inp_vflag
&= ~INP_IPV6
;
271 error
= in_pcbbind(inp
, (struct sockaddr
*)&sin
, p
);
275 error
= in6_pcbbind(inp
, nam
, p
);
278 COMMON_END(PRU_BIND
);
283 * Prepare to accept connections.
286 tcp_usr_listen(struct socket
*so
, struct proc
*p
)
290 struct inpcb
*inp
= sotoinpcb(so
);
294 if (inp
->inp_lport
== 0)
295 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
297 tp
->t_state
= TCPS_LISTEN
;
298 COMMON_END(PRU_LISTEN
);
303 tcp6_usr_listen(struct socket
*so
, struct proc
*p
)
307 struct inpcb
*inp
= sotoinpcb(so
);
311 if (inp
->inp_lport
== 0) {
312 inp
->inp_vflag
&= ~INP_IPV4
;
313 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0)
314 inp
->inp_vflag
|= INP_IPV4
;
315 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
318 tp
->t_state
= TCPS_LISTEN
;
319 COMMON_END(PRU_LISTEN
);
324 * Initiate connection to peer.
325 * Create a template for use in transmissions on this connection.
326 * Enter SYN_SENT state, and mark socket as connecting.
327 * Start keep-alive timer, and seed output sequence space.
328 * Send initial segment on connection.
331 tcp_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
335 struct inpcb
*inp
= sotoinpcb(so
);
337 struct sockaddr_in
*sinp
;
342 * Must disallow TCP ``connections'' to multicast addresses.
344 sinp
= (struct sockaddr_in
*)nam
;
345 if (sinp
->sin_family
== AF_INET
346 && IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
347 error
= EAFNOSUPPORT
;
352 prison_remote_ip(p
, 0, &sinp
->sin_addr
.s_addr
);
355 if ((error
= tcp_connect(tp
, nam
, p
)) != 0)
357 error
= tcp_output(tp
);
358 COMMON_END(PRU_CONNECT
);
363 tcp6_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
367 struct inpcb
*inp
= sotoinpcb(so
);
369 struct sockaddr_in6
*sin6p
;
374 * Must disallow TCP ``connections'' to multicast addresses.
376 sin6p
= (struct sockaddr_in6
*)nam
;
377 if (sin6p
->sin6_family
== AF_INET6
378 && IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
379 error
= EAFNOSUPPORT
;
383 if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
384 struct sockaddr_in sin
;
386 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) != 0)
389 in6_sin6_2_sin(&sin
, sin6p
);
390 inp
->inp_vflag
|= INP_IPV4
;
391 inp
->inp_vflag
&= ~INP_IPV6
;
392 if ((error
= tcp_connect(tp
, (struct sockaddr
*)&sin
, p
)) != 0)
394 error
= tcp_output(tp
);
397 inp
->inp_vflag
&= ~INP_IPV4
;
398 inp
->inp_vflag
|= INP_IPV6
;
399 if ((error
= tcp6_connect(tp
, nam
, p
)) != 0)
401 error
= tcp_output(tp
);
404 COMMON_END(PRU_CONNECT
);
409 * Initiate disconnect from peer.
410 * If connection never passed embryonic stage, just drop;
411 * else if don't need to let data drain, then can just drop anyways,
412 * else have to begin TCP shutdown process: mark socket disconnecting,
413 * drain unread data, state switch to reflect user close, and
414 * send segment (e.g. FIN) to peer. Socket will be really disconnected
415 * when peer sends FIN and acks ours.
417 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
420 tcp_usr_disconnect(struct socket
*so
)
424 struct inpcb
*inp
= sotoinpcb(so
);
428 /* In case we got disconnected from the peer */
431 tp
= tcp_disconnect(tp
);
432 COMMON_END(PRU_DISCONNECT
);
436 * Accept a connection. Essentially all the work is
437 * done at higher levels; just return the address
438 * of the peer, storing through addr.
441 tcp_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
445 struct inpcb
*inp
= sotoinpcb(so
);
446 struct tcpcb
*tp
= NULL
;
449 if (so
->so_state
& SS_ISDISCONNECTED
) {
450 error
= ECONNABORTED
;
459 in_setpeeraddr(so
, nam
);
460 COMMON_END(PRU_ACCEPT
);
465 tcp6_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
469 struct inpcb
*inp
= sotoinpcb(so
);
470 struct tcpcb
*tp
= NULL
;
473 if (so
->so_state
& SS_ISDISCONNECTED
) {
474 error
= ECONNABORTED
;
483 in6_mapped_peeraddr(so
, nam
);
484 COMMON_END(PRU_ACCEPT
);
488 * Mark the connection as being incapable of further output.
491 tcp_usr_shutdown(struct socket
*so
)
495 struct inpcb
*inp
= sotoinpcb(so
);
500 /* In case we got disconnected from the peer */
503 tp
= tcp_usrclosed(tp
);
505 error
= tcp_output(tp
);
506 COMMON_END(PRU_SHUTDOWN
);
510 * After a receive, possibly send window update to peer.
513 tcp_usr_rcvd(struct socket
*so
, int flags
)
517 struct inpcb
*inp
= sotoinpcb(so
);
521 /* In case we got disconnected from the peer */
525 COMMON_END(PRU_RCVD
);
529 * Do a send by putting data in output queue and updating urgent
530 * marker if URG set. Possibly send more data. Unlike the other
531 * pru_*() routines, the mbuf chains are our responsibility. We
532 * must either enqueue them or free them. The other pru_* routines
533 * generally are caller-frees.
536 tcp_usr_send(struct socket
*so
, int flags
, struct mbuf
*m
,
537 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
541 struct inpcb
*inp
= sotoinpcb(so
);
550 * OOPS! we lost a race, the TCP session got reset after
551 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
552 * network interrupt in the non-splnet() section of sosend().
558 error
= ECONNRESET
; /* XXX EPIPE? */
564 isipv6
= nam
&& nam
->sa_family
== AF_INET6
;
569 /* TCP doesn't do control messages (rights, creds, etc) */
570 if (control
->m_len
) {
577 m_freem(control
); /* empty control, just free it */
579 if(!(flags
& PRUS_OOB
)) {
580 sbappend(&so
->so_snd
, m
);
581 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
583 * Do implied connect if not yet connected,
584 * initialize window to default value, and
585 * initialize maxseg/maxopd using peer's cached
590 error
= tcp6_connect(tp
, nam
, p
);
593 error
= tcp_connect(tp
, nam
, p
);
596 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
600 if (flags
& PRUS_EOF
) {
602 * Close the send side of the connection after
606 tp
= tcp_usrclosed(tp
);
609 if (flags
& PRUS_MORETOCOME
)
610 tp
->t_flags
|= TF_MORETOCOME
;
611 error
= tcp_output(tp
);
612 if (flags
& PRUS_MORETOCOME
)
613 tp
->t_flags
&= ~TF_MORETOCOME
;
616 if (sbspace(&so
->so_snd
) < -512) {
622 * According to RFC961 (Assigned Protocols),
623 * the urgent pointer points to the last octet
624 * of urgent data. We continue, however,
625 * to consider it to indicate the first octet
626 * of data past the urgent section.
627 * Otherwise, snd_up should be one lower.
629 sbappend(&so
->so_snd
, m
);
630 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
632 * Do implied connect if not yet connected,
633 * initialize window to default value, and
634 * initialize maxseg/maxopd using peer's cached
639 error
= tcp6_connect(tp
, nam
, p
);
642 error
= tcp_connect(tp
, nam
, p
);
645 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
648 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
650 error
= tcp_output(tp
);
653 COMMON_END((flags
& PRUS_OOB
) ? PRU_SENDOOB
:
654 ((flags
& PRUS_EOF
) ? PRU_SEND_EOF
: PRU_SEND
));
661 tcp_usr_abort(struct socket
*so
)
665 struct inpcb
*inp
= sotoinpcb(so
);
669 /* In case we got disconnected from the peer */
672 tp
= tcp_drop(tp
, ECONNABORTED
);
673 COMMON_END(PRU_ABORT
);
677 * Receive out-of-band data.
680 tcp_usr_rcvoob(struct socket
*so
, struct mbuf
*m
, int flags
)
684 struct inpcb
*inp
= sotoinpcb(so
);
688 if ((so
->so_oobmark
== 0 &&
689 (so
->so_state
& SS_RCVATMARK
) == 0) ||
690 so
->so_options
& SO_OOBINLINE
||
691 tp
->t_oobflags
& TCPOOB_HADDATA
) {
695 if ((tp
->t_oobflags
& TCPOOB_HAVEDATA
) == 0) {
700 *mtod(m
, caddr_t
) = tp
->t_iobc
;
701 if ((flags
& MSG_PEEK
) == 0)
702 tp
->t_oobflags
^= (TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
703 COMMON_END(PRU_RCVOOB
);
706 /* xxx - should be const */
707 struct pr_usrreqs tcp_usrreqs
= {
708 tcp_usr_abort
, tcp_usr_accept
, tcp_usr_attach
, tcp_usr_bind
,
709 tcp_usr_connect
, pru_connect2_notsupp
, in_control
, tcp_usr_detach
,
710 tcp_usr_disconnect
, tcp_usr_listen
, in_setpeeraddr
, tcp_usr_rcvd
,
711 tcp_usr_rcvoob
, tcp_usr_send
, pru_sense_null
, tcp_usr_shutdown
,
712 in_setsockaddr
, sosend
, soreceive
, sopoll
716 struct pr_usrreqs tcp6_usrreqs
= {
717 tcp_usr_abort
, tcp6_usr_accept
, tcp_usr_attach
, tcp6_usr_bind
,
718 tcp6_usr_connect
, pru_connect2_notsupp
, in6_control
, tcp_usr_detach
,
719 tcp_usr_disconnect
, tcp6_usr_listen
, in6_mapped_peeraddr
, tcp_usr_rcvd
,
720 tcp_usr_rcvoob
, tcp_usr_send
, pru_sense_null
, tcp_usr_shutdown
,
721 in6_mapped_sockaddr
, sosend
, soreceive
, sopoll
726 * Common subroutine to open a TCP connection to remote host specified
727 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
728 * port number if needed. Call in_pcbladdr to do the routing and to choose
729 * a local host address (interface). If there is an existing incarnation
730 * of the same connection in TIME-WAIT state and if the remote host was
731 * sending CC options and if the connection duration was < MSL, then
732 * truncate the previous TIME-WAIT state and proceed.
733 * Initialize connection parameters and enter SYN-SENT state.
736 tcp_connect(tp
, nam
, p
)
737 register struct tcpcb
*tp
;
738 struct sockaddr
*nam
;
741 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
742 struct socket
*so
= inp
->inp_socket
;
744 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
745 struct sockaddr_in
*ifaddr
;
746 struct rmxp_tao
*taop
;
747 struct rmxp_tao tao_noncached
;
750 if (inp
->inp_lport
== 0) {
751 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
757 * Cannot simply call in_pcbconnect, because there might be an
758 * earlier incarnation of this same connection still in
759 * TIME_WAIT state, creating an ADDRINUSE error.
761 error
= in_pcbladdr(inp
, nam
, &ifaddr
);
764 oinp
= in_pcblookup_hash(inp
->inp_pcbinfo
,
765 sin
->sin_addr
, sin
->sin_port
,
766 inp
->inp_laddr
.s_addr
!= INADDR_ANY
? inp
->inp_laddr
768 inp
->inp_lport
, 0, NULL
);
770 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
771 otp
->t_state
== TCPS_TIME_WAIT
&&
772 otp
->t_starttime
< tcp_msl
&&
773 (otp
->t_flags
& TF_RCVD_CC
))
774 otp
= tcp_close(otp
);
778 if ((inp
->inp_laddr
.s_addr
== INADDR_ANY
? ifaddr
->sin_addr
.s_addr
:
779 inp
->inp_laddr
.s_addr
) == sin
->sin_addr
.s_addr
&&
780 inp
->inp_lport
== sin
->sin_port
)
782 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
783 inp
->inp_laddr
= ifaddr
->sin_addr
;
784 inp
->inp_faddr
= sin
->sin_addr
;
785 inp
->inp_fport
= sin
->sin_port
;
788 /* Compute window scaling to request. */
789 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
790 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
791 tp
->request_r_scale
++;
794 tcpstat
.tcps_connattempt
++;
795 tp
->t_state
= TCPS_SYN_SENT
;
796 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
797 tp
->iss
= tcp_new_isn(tp
);
801 * Generate a CC value for this connection and
802 * check whether CC or CCnew should be used.
804 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
805 taop
= &tao_noncached
;
806 bzero(taop
, sizeof(*taop
));
809 tp
->cc_send
= CC_INC(tcp_ccgen
);
810 if (taop
->tao_ccsent
!= 0 &&
811 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
812 taop
->tao_ccsent
= tp
->cc_send
;
814 taop
->tao_ccsent
= 0;
815 tp
->t_flags
|= TF_SENDCCNEW
;
823 tcp6_connect(tp
, nam
, p
)
824 register struct tcpcb
*tp
;
825 struct sockaddr
*nam
;
828 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
829 struct socket
*so
= inp
->inp_socket
;
831 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
832 struct in6_addr
*addr6
;
833 struct rmxp_tao
*taop
;
834 struct rmxp_tao tao_noncached
;
837 if (inp
->inp_lport
== 0) {
838 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
844 * Cannot simply call in_pcbconnect, because there might be an
845 * earlier incarnation of this same connection still in
846 * TIME_WAIT state, creating an ADDRINUSE error.
848 error
= in6_pcbladdr(inp
, nam
, &addr6
);
851 oinp
= in6_pcblookup_hash(inp
->inp_pcbinfo
,
852 &sin6
->sin6_addr
, sin6
->sin6_port
,
853 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
856 inp
->inp_lport
, 0, NULL
);
858 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
859 otp
->t_state
== TCPS_TIME_WAIT
&&
860 otp
->t_starttime
< tcp_msl
&&
861 (otp
->t_flags
& TF_RCVD_CC
))
862 otp
= tcp_close(otp
);
866 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
867 inp
->in6p_laddr
= *addr6
;
868 inp
->in6p_faddr
= sin6
->sin6_addr
;
869 inp
->inp_fport
= sin6
->sin6_port
;
870 if ((sin6
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
) != NULL
)
871 inp
->in6p_flowinfo
= sin6
->sin6_flowinfo
;
874 /* Compute window scaling to request. */
875 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
876 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
877 tp
->request_r_scale
++;
880 tcpstat
.tcps_connattempt
++;
881 tp
->t_state
= TCPS_SYN_SENT
;
882 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
883 tp
->iss
= tcp_new_isn(tp
);
887 * Generate a CC value for this connection and
888 * check whether CC or CCnew should be used.
890 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
891 taop
= &tao_noncached
;
892 bzero(taop
, sizeof(*taop
));
895 tp
->cc_send
= CC_INC(tcp_ccgen
);
896 if (taop
->tao_ccsent
!= 0 &&
897 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
898 taop
->tao_ccsent
= tp
->cc_send
;
900 taop
->tao_ccsent
= 0;
901 tp
->t_flags
|= TF_SENDCCNEW
;
909 * The new sockopt interface makes it possible for us to block in the
910 * copyin/out step (if we take a page fault). Taking a page fault at
911 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
912 * use TSM, there probably isn't any need for this function to run at
913 * splnet() any more. This needs more examination.)
916 tcp_ctloutput(so
, sopt
)
918 struct sockopt
*sopt
;
920 int error
, opt
, optval
, s
;
925 s
= splnet(); /* XXX */
931 if (sopt
->sopt_level
!= IPPROTO_TCP
) {
933 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
934 error
= ip6_ctloutput(so
, sopt
);
937 error
= ip_ctloutput(so
, sopt
);
947 switch (sopt
->sopt_dir
) {
949 switch (sopt
->sopt_name
) {
953 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
958 switch (sopt
->sopt_name
) {
969 opt
= 0; /* dead code to fool gcc */
980 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
985 if (optval
> 0 && optval
<= tp
->t_maxseg
&&
986 optval
+ 40 >= tcp_minmss
)
987 tp
->t_maxseg
= optval
;
993 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1000 tp
->t_keepidle
= optval
* PR_SLOWHZ
;
1004 error
= ENOPROTOOPT
;
1010 switch (sopt
->sopt_name
) {
1012 optval
= tp
->t_flags
& TF_NODELAY
;
1015 optval
= tp
->t_maxseg
;
1018 optval
= tp
->t_keepidle
/ PR_SLOWHZ
;
1021 optval
= tp
->t_flags
& TF_NOOPT
;
1024 optval
= tp
->t_flags
& TF_NOPUSH
;
1027 error
= ENOPROTOOPT
;
1031 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1039 * tcp_sendspace and tcp_recvspace are the default send and receive window
1040 * sizes, respectively. These are obsolescent (this information should
1041 * be set by the route).
1043 u_long tcp_sendspace
= 1024*16;
1044 SYSCTL_INT(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
, CTLFLAG_RW
,
1045 &tcp_sendspace
, 0, "Maximum outgoing TCP datagram size");
1046 u_long tcp_recvspace
= 1024*16;
1047 SYSCTL_INT(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
, CTLFLAG_RW
,
1048 &tcp_recvspace
, 0, "Maximum incoming TCP datagram size");
1050 __private_extern__
int tcp_sockthreshold
= 256;
1051 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sockthreshold
, CTLFLAG_RW
,
1052 &tcp_sockthreshold
, 0, "TCP Socket size increased if less than threshold");
1054 #define TCP_INCREASED_SPACE 65535 /* Automatically increase tcp send/rcv space to this value */
1056 * Attach TCP protocol to socket, allocating
1057 * internet protocol control block, tcp control block,
1058 * bufer space, and entering LISTEN state if to accept connections.
1065 register struct tcpcb
*tp
;
1069 int isipv6
= INP_CHECK_SOCKAF(so
, AF_INET6
) != NULL
;
1072 error
= in_pcballoc(so
, &tcbinfo
, p
);
1076 inp
= sotoinpcb(so
);
1078 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
1080 * The goal is to let clients have large send/rcv default windows (TCP_INCREASED_SPACE)
1081 * while not hogging mbuf space for servers. This is done by watching a threshold
1082 * of tcpcbs in use and bumping the default send and rcvspace only if under that threshold.
1083 * The theory being that busy servers have a lot more active tcpcbs and don't want the potential
1084 * memory penalty of having much larger sockbuffs. The sysctl allows to fine tune that threshold value. */
1086 if (inp
->inp_pcbinfo
->ipi_count
< tcp_sockthreshold
)
1087 error
= soreserve(so
, MAX(TCP_INCREASED_SPACE
, tcp_sendspace
), MAX(TCP_INCREASED_SPACE
,tcp_recvspace
));
1089 error
= soreserve(so
, tcp_sendspace
, tcp_recvspace
);
1096 inp
->inp_vflag
|= INP_IPV6
;
1097 inp
->in6p_hops
= -1; /* use kernel default */
1101 inp
->inp_vflag
|= INP_IPV4
;
1102 tp
= tcp_newtcpcb(inp
);
1104 int nofd
= so
->so_state
& SS_NOFDREF
; /* XXX */
1106 so
->so_state
&= ~SS_NOFDREF
; /* don't free the socket yet */
1113 so
->so_state
|= nofd
;
1116 tp
->t_state
= TCPS_CLOSED
;
1121 * Initiate (or continue) disconnect.
1122 * If embryonic state, just send reset (once).
1123 * If in ``let data drain'' option and linger null, just drop.
1124 * Otherwise (hard), mark socket disconnecting and drop
1125 * current input data; switch states based on user close, and
1126 * send segment to peer (with FIN).
1128 static struct tcpcb
*
1130 register struct tcpcb
*tp
;
1132 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1134 if (tp
->t_state
< TCPS_ESTABLISHED
)
1136 else if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
1137 tp
= tcp_drop(tp
, 0);
1139 soisdisconnecting(so
);
1140 sbflush(&so
->so_rcv
);
1141 tp
= tcp_usrclosed(tp
);
1143 (void) tcp_output(tp
);
1149 * User issued close, and wish to trail through shutdown states:
1150 * if never received SYN, just forget it. If got a SYN from peer,
1151 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1152 * If already got a FIN from peer, then almost done; go to LAST_ACK
1153 * state. In all other cases, have already sent FIN to peer (e.g.
1154 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1155 * for peer to send FIN or not respond to keep-alives, etc.
1156 * We can let the user exit from the close as soon as the FIN is acked.
1158 static struct tcpcb
*
1160 register struct tcpcb
*tp
;
1163 switch (tp
->t_state
) {
1167 tp
->t_state
= TCPS_CLOSED
;
1172 case TCPS_SYN_RECEIVED
:
1173 tp
->t_flags
|= TF_NEEDFIN
;
1176 case TCPS_ESTABLISHED
:
1177 tp
->t_state
= TCPS_FIN_WAIT_1
;
1180 case TCPS_CLOSE_WAIT
:
1181 tp
->t_state
= TCPS_LAST_ACK
;
1184 if (tp
&& tp
->t_state
>= TCPS_FIN_WAIT_2
) {
1185 soisdisconnected(tp
->t_inpcb
->inp_socket
);
1186 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1187 if (tp
->t_state
== TCPS_FIN_WAIT_2
)
1188 tp
->t_timer
[TCPT_2MSL
] = tcp_maxidle
;