2 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1988, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
67 * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.51.2.17 2002/10/11 11:46:44 ume Exp $
68 * $DragonFly: src/sys/netinet/tcp_usrreq.c,v 1.49 2008/06/17 20:50:11 aggelos Exp $
71 #include "opt_ipsec.h"
72 #include "opt_inet6.h"
73 #include "opt_tcpdebug.h"
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/malloc.h>
79 #include <sys/sysctl.h>
80 #include <sys/globaldata.h>
81 #include <sys/thread.h>
85 #include <sys/domain.h>
87 #include <sys/socket.h>
88 #include <sys/socketvar.h>
89 #include <sys/protosw.h>
91 #include <sys/thread2.h>
92 #include <sys/msgport2.h>
95 #include <net/netisr.h>
96 #include <net/route.h>
98 #include <net/netmsg2.h>
100 #include <netinet/in.h>
101 #include <netinet/in_systm.h>
103 #include <netinet/ip6.h>
105 #include <netinet/in_pcb.h>
107 #include <netinet6/in6_pcb.h>
109 #include <netinet/in_var.h>
110 #include <netinet/ip_var.h>
112 #include <netinet6/ip6_var.h>
114 #include <netinet/tcp.h>
115 #include <netinet/tcp_fsm.h>
116 #include <netinet/tcp_seq.h>
117 #include <netinet/tcp_timer.h>
118 #include <netinet/tcp_var.h>
119 #include <netinet/tcpip.h>
121 #include <netinet/tcp_debug.h>
125 #include <netinet6/ipsec.h>
129 * TCP protocol interface to socket abstraction.
131 extern char *tcpstates
[]; /* XXX ??? */
133 static int tcp_attach (struct socket
*, struct pru_attach_info
*);
134 static int tcp_connect (struct tcpcb
*, struct sockaddr
*,
137 static int tcp6_connect (struct tcpcb
*, struct sockaddr
*,
140 static struct tcpcb
*
141 tcp_disconnect (struct tcpcb
*);
142 static struct tcpcb
*
143 tcp_usrclosed (struct tcpcb
*);
146 #define TCPDEBUG0 int ostate = 0
147 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
148 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
149 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
153 #define TCPDEBUG2(req)
157 * TCP attaches to socket via pru_attach(), reserving space,
158 * and an internet control block.
161 tcp_usr_attach(struct socket
*so
, int proto
, struct pru_attach_info
*ai
)
165 struct tcpcb
*tp
= 0;
176 error
= tcp_attach(so
, ai
);
180 if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
181 so
->so_linger
= TCP_LINGERTIME
;
184 TCPDEBUG2(PRU_ATTACH
);
190 * pru_detach() detaches the TCP protocol from the socket.
191 * If the protocol state is non-embryonic, then can't
192 * do this directly: have to initiate a pru_disconnect(),
193 * which may finish later; embryonic TCB's can just
197 tcp_usr_detach(struct socket
*so
)
208 return EINVAL
; /* XXX */
212 * It's possible for the tcpcb (tp) to disconnect from the inp due
213 * to tcp_drop()->tcp_close() being called. This may occur *after*
214 * the detach message has been queued so we may find a NULL tp here.
216 if ((tp
= intotcpcb(inp
)) != NULL
) {
218 tp
= tcp_disconnect(tp
);
219 TCPDEBUG2(PRU_DETACH
);
225 #define COMMON_START(so, inp) \
235 tp = intotcpcb(inp); \
239 #define COMMON_END(req) out: TCPDEBUG2(req); crit_exit(); return error; goto out
243 * Give the socket an address.
246 tcp_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
)
251 struct sockaddr_in
*sinp
;
253 COMMON_START(so
, inp
);
256 * Must check for multicast addresses and disallow binding
259 sinp
= (struct sockaddr_in
*)nam
;
260 if (sinp
->sin_family
== AF_INET
&&
261 IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
262 error
= EAFNOSUPPORT
;
265 error
= in_pcbbind(inp
, nam
, td
);
268 COMMON_END(PRU_BIND
);
274 tcp6_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
)
279 struct sockaddr_in6
*sin6p
;
281 COMMON_START(so
, inp
);
284 * Must check for multicast addresses and disallow binding
287 sin6p
= (struct sockaddr_in6
*)nam
;
288 if (sin6p
->sin6_family
== AF_INET6
&&
289 IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
290 error
= EAFNOSUPPORT
;
293 inp
->inp_vflag
&= ~INP_IPV4
;
294 inp
->inp_vflag
|= INP_IPV6
;
295 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0) {
296 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p
->sin6_addr
))
297 inp
->inp_vflag
|= INP_IPV4
;
298 else if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
299 struct sockaddr_in sin
;
301 in6_sin6_2_sin(&sin
, sin6p
);
302 inp
->inp_vflag
|= INP_IPV4
;
303 inp
->inp_vflag
&= ~INP_IPV6
;
304 error
= in_pcbbind(inp
, (struct sockaddr
*)&sin
, td
);
308 error
= in6_pcbbind(inp
, nam
, td
);
311 COMMON_END(PRU_BIND
);
316 struct netmsg_inswildcard
{
317 struct netmsg nm_netmsg
;
318 struct inpcb
*nm_inp
;
319 struct inpcbinfo
*nm_pcbinfo
;
323 in_pcbinswildcardhash_handler(struct netmsg
*msg0
)
325 struct netmsg_inswildcard
*msg
= (struct netmsg_inswildcard
*)msg0
;
327 in_pcbinswildcardhash_oncpu(msg
->nm_inp
, msg
->nm_pcbinfo
);
328 lwkt_replymsg(&msg
->nm_netmsg
.nm_lmsg
, 0);
333 * Prepare to accept connections.
336 tcp_usr_listen(struct socket
*so
, struct thread
*td
)
345 COMMON_START(so
, inp
);
346 if (inp
->inp_lport
== 0) {
347 error
= in_pcbbind(inp
, NULL
, td
);
352 tp
->t_state
= TCPS_LISTEN
;
355 * We have to set the flag because we can't have other cpus
356 * messing with our inp's flags.
358 inp
->inp_flags
|= INP_WILDCARD_MP
;
359 for (cpu
= 0; cpu
< ncpus2
; cpu
++) {
360 struct netmsg_inswildcard
*msg
;
362 if (cpu
== mycpu
->gd_cpuid
) {
363 in_pcbinswildcardhash(inp
);
367 msg
= kmalloc(sizeof(struct netmsg_inswildcard
), M_LWKTMSG
,
369 netmsg_init(&msg
->nm_netmsg
, &netisr_afree_rport
, 0,
370 in_pcbinswildcardhash_handler
);
372 msg
->nm_pcbinfo
= &tcbinfo
[cpu
];
373 lwkt_sendmsg(tcp_cport(cpu
), &msg
->nm_netmsg
.nm_lmsg
);
376 in_pcbinswildcardhash(inp
);
378 COMMON_END(PRU_LISTEN
);
383 tcp6_usr_listen(struct socket
*so
, struct thread
*td
)
392 COMMON_START(so
, inp
);
393 if (inp
->inp_lport
== 0) {
394 if (!(inp
->inp_flags
& IN6P_IPV6_V6ONLY
))
395 inp
->inp_vflag
|= INP_IPV4
;
397 inp
->inp_vflag
&= ~INP_IPV4
;
398 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, td
);
401 tp
->t_state
= TCPS_LISTEN
;
404 * We have to set the flag because we can't have other cpus
405 * messing with our inp's flags.
407 inp
->inp_flags
|= INP_WILDCARD_MP
;
408 for (cpu
= 0; cpu
< ncpus2
; cpu
++) {
409 struct netmsg_inswildcard
*msg
;
411 if (cpu
== mycpu
->gd_cpuid
) {
412 in_pcbinswildcardhash(inp
);
416 msg
= kmalloc(sizeof(struct netmsg_inswildcard
), M_LWKTMSG
,
418 netmsg_init(&msg
->nm_netmsg
, &netisr_afree_rport
, 0,
419 in_pcbinswildcardhash_handler
);
421 msg
->nm_pcbinfo
= &tcbinfo
[cpu
];
422 lwkt_sendmsg(tcp_cport(cpu
), &msg
->nm_netmsg
.nm_lmsg
);
425 in_pcbinswildcardhash(inp
);
427 COMMON_END(PRU_LISTEN
);
433 tcp_output_dispatch(struct netmsg
*nmsg
)
435 struct lwkt_msg
*msg
= &nmsg
->nm_lmsg
;
436 struct tcpcb
*tp
= msg
->u
.ms_resultp
;
439 error
= tcp_output(tp
);
440 lwkt_replymsg(msg
, error
);
445 * Initiate connection to peer.
446 * Create a template for use in transmissions on this connection.
447 * Enter SYN_SENT state, and mark socket as connecting.
448 * Start keep-alive timer, and seed output sequence space.
449 * Send initial segment on connection.
452 tcp_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
)
457 struct sockaddr_in
*sinp
;
462 COMMON_START(so
, inp
);
465 * Must disallow TCP ``connections'' to multicast addresses.
467 sinp
= (struct sockaddr_in
*)nam
;
468 if (sinp
->sin_family
== AF_INET
469 && IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
470 error
= EAFNOSUPPORT
;
474 if (!prison_remote_ip(td
, (struct sockaddr
*)sinp
)) {
475 error
= EAFNOSUPPORT
; /* IPv6 only jail */
479 if ((error
= tcp_connect(tp
, nam
, td
)) != 0)
483 port
= tcp_addrport(inp
->inp_faddr
.s_addr
, inp
->inp_fport
,
484 inp
->inp_laddr
.s_addr
, inp
->inp_lport
);
485 if (port
!= &curthread
->td_msgport
) {
487 struct lwkt_msg
*msg
;
489 netmsg_init(&nmsg
, &curthread
->td_msgport
, 0,
490 tcp_output_dispatch
);
492 msg
->u
.ms_resultp
= tp
;
494 error
= lwkt_domsg(port
, msg
, 0);
497 error
= tcp_output(tp
);
498 COMMON_END(PRU_CONNECT
);
503 tcp6_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
)
508 struct sockaddr_in6
*sin6p
;
510 COMMON_START(so
, inp
);
513 * Must disallow TCP ``connections'' to multicast addresses.
515 sin6p
= (struct sockaddr_in6
*)nam
;
516 if (sin6p
->sin6_family
== AF_INET6
517 && IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
518 error
= EAFNOSUPPORT
;
522 if (!prison_remote_ip(td
, nam
)) {
523 error
= EAFNOSUPPORT
; /* IPv4 only jail */
527 if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
528 struct sockaddr_in sin
;
530 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) != 0) {
535 in6_sin6_2_sin(&sin
, sin6p
);
536 inp
->inp_vflag
|= INP_IPV4
;
537 inp
->inp_vflag
&= ~INP_IPV6
;
538 if ((error
= tcp_connect(tp
, (struct sockaddr
*)&sin
, td
)) != 0)
540 error
= tcp_output(tp
);
543 inp
->inp_vflag
&= ~INP_IPV4
;
544 inp
->inp_vflag
|= INP_IPV6
;
545 inp
->inp_inc
.inc_isipv6
= 1;
546 if ((error
= tcp6_connect(tp
, nam
, td
)) != 0)
548 error
= tcp_output(tp
);
549 COMMON_END(PRU_CONNECT
);
554 * Initiate disconnect from peer.
555 * If connection never passed embryonic stage, just drop;
556 * else if don't need to let data drain, then can just drop anyways,
557 * else have to begin TCP shutdown process: mark socket disconnecting,
558 * drain unread data, state switch to reflect user close, and
559 * send segment (e.g. FIN) to peer. Socket will be really disconnected
560 * when peer sends FIN and acks ours.
562 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
565 tcp_usr_disconnect(struct socket
*so
)
571 COMMON_START(so
, inp
);
572 tp
= tcp_disconnect(tp
);
573 COMMON_END(PRU_DISCONNECT
);
577 * Accept a connection. Essentially all the work is
578 * done at higher levels; just return the address
579 * of the peer, storing through addr.
582 tcp_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
586 struct tcpcb
*tp
= NULL
;
591 if (so
->so_state
& SS_ISDISCONNECTED
) {
592 error
= ECONNABORTED
;
601 in_setpeeraddr(so
, nam
);
602 COMMON_END(PRU_ACCEPT
);
607 tcp6_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
611 struct tcpcb
*tp
= NULL
;
617 if (so
->so_state
& SS_ISDISCONNECTED
) {
618 error
= ECONNABORTED
;
627 in6_mapped_peeraddr(so
, nam
);
628 COMMON_END(PRU_ACCEPT
);
632 * Mark the connection as being incapable of further output.
635 tcp_usr_shutdown(struct socket
*so
)
641 COMMON_START(so
, inp
);
643 tp
= tcp_usrclosed(tp
);
645 error
= tcp_output(tp
);
646 COMMON_END(PRU_SHUTDOWN
);
650 * After a receive, possibly send window update to peer.
653 tcp_usr_rcvd(struct socket
*so
, int flags
)
659 COMMON_START(so
, inp
);
661 COMMON_END(PRU_RCVD
);
665 * Do a send by putting data in output queue and updating urgent
666 * marker if URG set. Possibly send more data. Unlike the other
667 * pru_*() routines, the mbuf chains are our responsibility. We
668 * must either enqueue them or free them. The other pru_* routines
669 * generally are caller-frees.
672 tcp_usr_send(struct socket
*so
, int flags
, struct mbuf
*m
,
673 struct sockaddr
*nam
, struct mbuf
*control
, struct thread
*td
)
688 * OOPS! we lost a race, the TCP session got reset after
689 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
690 * network interrupt in the non-critical section of sosend().
696 error
= ECONNRESET
; /* XXX EPIPE? */
702 isipv6
= nam
&& nam
->sa_family
== AF_INET6
;
707 /* TCP doesn't do control messages (rights, creds, etc) */
708 if (control
->m_len
) {
715 m_freem(control
); /* empty control, just free it */
717 if(!(flags
& PRUS_OOB
)) {
718 ssb_appendstream(&so
->so_snd
, m
);
719 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
721 * Do implied connect if not yet connected,
722 * initialize window to default value, and
723 * initialize maxseg/maxopd using peer's cached
728 error
= tcp6_connect(tp
, nam
, td
);
731 error
= tcp_connect(tp
, nam
, td
);
734 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
738 if (flags
& PRUS_EOF
) {
740 * Close the send side of the connection after
744 tp
= tcp_usrclosed(tp
);
747 if (flags
& PRUS_MORETOCOME
)
748 tp
->t_flags
|= TF_MORETOCOME
;
749 error
= tcp_output(tp
);
750 if (flags
& PRUS_MORETOCOME
)
751 tp
->t_flags
&= ~TF_MORETOCOME
;
754 if (ssb_space(&so
->so_snd
) < -512) {
760 * According to RFC961 (Assigned Protocols),
761 * the urgent pointer points to the last octet
762 * of urgent data. We continue, however,
763 * to consider it to indicate the first octet
764 * of data past the urgent section.
765 * Otherwise, snd_up should be one lower.
767 ssb_appendstream(&so
->so_snd
, m
);
768 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
770 * Do implied connect if not yet connected,
771 * initialize window to default value, and
772 * initialize maxseg/maxopd using peer's cached
777 error
= tcp6_connect(tp
, nam
, td
);
780 error
= tcp_connect(tp
, nam
, td
);
783 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
786 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.ssb_cc
;
787 tp
->t_flags
|= TF_FORCE
;
788 error
= tcp_output(tp
);
789 tp
->t_flags
&= ~TF_FORCE
;
791 COMMON_END((flags
& PRUS_OOB
) ? PRU_SENDOOB
:
792 ((flags
& PRUS_EOF
) ? PRU_SEND_EOF
: PRU_SEND
));
799 tcp_usr_abort(struct socket
*so
)
805 COMMON_START(so
, inp
);
806 tp
= tcp_drop(tp
, ECONNABORTED
);
807 COMMON_END(PRU_ABORT
);
811 * Receive out-of-band data.
814 tcp_usr_rcvoob(struct socket
*so
, struct mbuf
*m
, int flags
)
820 COMMON_START(so
, inp
);
821 if ((so
->so_oobmark
== 0 &&
822 (so
->so_state
& SS_RCVATMARK
) == 0) ||
823 so
->so_options
& SO_OOBINLINE
||
824 tp
->t_oobflags
& TCPOOB_HADDATA
) {
828 if ((tp
->t_oobflags
& TCPOOB_HAVEDATA
) == 0) {
833 *mtod(m
, caddr_t
) = tp
->t_iobc
;
834 if ((flags
& MSG_PEEK
) == 0)
835 tp
->t_oobflags
^= (TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
836 COMMON_END(PRU_RCVOOB
);
839 /* xxx - should be const */
840 struct pr_usrreqs tcp_usrreqs
= {
841 .pru_abort
= tcp_usr_abort
,
842 .pru_accept
= tcp_usr_accept
,
843 .pru_attach
= tcp_usr_attach
,
844 .pru_bind
= tcp_usr_bind
,
845 .pru_connect
= tcp_usr_connect
,
846 .pru_connect2
= pru_connect2_notsupp
,
847 .pru_control
= in_control
,
848 .pru_detach
= tcp_usr_detach
,
849 .pru_disconnect
= tcp_usr_disconnect
,
850 .pru_listen
= tcp_usr_listen
,
851 .pru_peeraddr
= in_setpeeraddr
,
852 .pru_rcvd
= tcp_usr_rcvd
,
853 .pru_rcvoob
= tcp_usr_rcvoob
,
854 .pru_send
= tcp_usr_send
,
855 .pru_sense
= pru_sense_null
,
856 .pru_shutdown
= tcp_usr_shutdown
,
857 .pru_sockaddr
= in_setsockaddr
,
858 .pru_sosend
= sosend
,
859 .pru_soreceive
= soreceive
,
864 struct pr_usrreqs tcp6_usrreqs
= {
865 .pru_abort
= tcp_usr_abort
,
866 .pru_accept
= tcp6_usr_accept
,
867 .pru_attach
= tcp_usr_attach
,
868 .pru_bind
= tcp6_usr_bind
,
869 .pru_connect
= tcp6_usr_connect
,
870 .pru_connect2
= pru_connect2_notsupp
,
871 .pru_control
= in6_control
,
872 .pru_detach
= tcp_usr_detach
,
873 .pru_disconnect
= tcp_usr_disconnect
,
874 .pru_listen
= tcp6_usr_listen
,
875 .pru_peeraddr
= in6_mapped_peeraddr
,
876 .pru_rcvd
= tcp_usr_rcvd
,
877 .pru_rcvoob
= tcp_usr_rcvoob
,
878 .pru_send
= tcp_usr_send
,
879 .pru_sense
= pru_sense_null
,
880 .pru_shutdown
= tcp_usr_shutdown
,
881 .pru_sockaddr
= in6_mapped_sockaddr
,
882 .pru_sosend
= sosend
,
883 .pru_soreceive
= soreceive
,
889 tcp_connect_oncpu(struct tcpcb
*tp
, struct sockaddr_in
*sin
,
890 struct sockaddr_in
*if_sin
)
892 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
893 struct socket
*so
= inp
->inp_socket
;
895 struct rmxp_tao
*taop
;
896 struct rmxp_tao tao_noncached
;
898 oinp
= in_pcblookup_hash(&tcbinfo
[mycpu
->gd_cpuid
],
899 sin
->sin_addr
, sin
->sin_port
,
900 inp
->inp_laddr
.s_addr
!= INADDR_ANY
?
901 inp
->inp_laddr
: if_sin
->sin_addr
,
902 inp
->inp_lport
, 0, NULL
);
904 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
905 otp
->t_state
== TCPS_TIME_WAIT
&&
906 (ticks
- otp
->t_starttime
) < tcp_msl
&&
907 (otp
->t_flags
& TF_RCVD_CC
))
912 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
913 inp
->inp_laddr
= if_sin
->sin_addr
;
914 inp
->inp_faddr
= sin
->sin_addr
;
915 inp
->inp_fport
= sin
->sin_port
;
916 inp
->inp_cpcbinfo
= &tcbinfo
[mycpu
->gd_cpuid
];
917 in_pcbinsconnhash(inp
);
919 /* Compute window scaling to request. */
920 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
921 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.ssb_hiwat
)
922 tp
->request_r_scale
++;
925 tcpstat
.tcps_connattempt
++;
926 tp
->t_state
= TCPS_SYN_SENT
;
927 callout_reset(tp
->tt_keep
, tcp_keepinit
, tcp_timer_keep
, tp
);
928 tp
->iss
= tcp_new_isn(tp
);
932 * Generate a CC value for this connection and
933 * check whether CC or CCnew should be used.
935 if ((taop
= tcp_gettaocache(&tp
->t_inpcb
->inp_inc
)) == NULL
) {
936 taop
= &tao_noncached
;
937 bzero(taop
, sizeof *taop
);
940 tp
->cc_send
= CC_INC(tcp_ccgen
);
941 if (taop
->tao_ccsent
!= 0 &&
942 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
943 taop
->tao_ccsent
= tp
->cc_send
;
945 taop
->tao_ccsent
= 0;
946 tp
->t_flags
|= TF_SENDCCNEW
;
954 struct netmsg_tcp_connect
{
955 struct netmsg nm_netmsg
;
957 struct sockaddr_in
*nm_sin
;
958 struct sockaddr_in
*nm_ifsin
;
962 tcp_connect_handler(netmsg_t netmsg
)
964 struct netmsg_tcp_connect
*msg
= (void *)netmsg
;
967 error
= tcp_connect_oncpu(msg
->nm_tp
, msg
->nm_sin
, msg
->nm_ifsin
);
968 lwkt_replymsg(&msg
->nm_netmsg
.nm_lmsg
, error
);
974 * Common subroutine to open a TCP connection to remote host specified
975 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
976 * port number if needed. Call in_pcbladdr to do the routing and to choose
977 * a local host address (interface). If there is an existing incarnation
978 * of the same connection in TIME-WAIT state and if the remote host was
979 * sending CC options and if the connection duration was < MSL, then
980 * truncate the previous TIME-WAIT state and proceed.
981 * Initialize connection parameters and enter SYN-SENT state.
984 tcp_connect(struct tcpcb
*tp
, struct sockaddr
*nam
, struct thread
*td
)
986 struct inpcb
*inp
= tp
->t_inpcb
;
987 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
988 struct sockaddr_in
*if_sin
;
994 if (inp
->inp_lport
== 0) {
995 error
= in_pcbbind(inp
, (struct sockaddr
*)NULL
, td
);
1001 * Cannot simply call in_pcbconnect, because there might be an
1002 * earlier incarnation of this same connection still in
1003 * TIME_WAIT state, creating an ADDRINUSE error.
1005 error
= in_pcbladdr(inp
, nam
, &if_sin
, td
);
1010 port
= tcp_addrport(sin
->sin_addr
.s_addr
, sin
->sin_port
,
1011 inp
->inp_laddr
.s_addr
?
1012 inp
->inp_laddr
.s_addr
: if_sin
->sin_addr
.s_addr
,
1015 if (port
!= &curthread
->td_msgport
) {
1016 struct netmsg_tcp_connect msg
;
1017 struct route
*ro
= &inp
->inp_route
;
1020 * in_pcbladdr() may have allocated a route entry for us
1021 * on the current CPU, but we need a route entry on the
1022 * target CPU, so free it here.
1024 if (ro
->ro_rt
!= NULL
)
1026 bzero(ro
, sizeof(*ro
));
1028 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
1029 tcp_connect_handler
);
1032 msg
.nm_ifsin
= if_sin
;
1033 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
1036 error
= tcp_connect_oncpu(tp
, sin
, if_sin
);
1043 tcp6_connect(struct tcpcb
*tp
, struct sockaddr
*nam
, struct thread
*td
)
1045 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
1046 struct socket
*so
= inp
->inp_socket
;
1048 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
1049 struct in6_addr
*addr6
;
1050 struct rmxp_tao
*taop
;
1051 struct rmxp_tao tao_noncached
;
1054 if (inp
->inp_lport
== 0) {
1055 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, td
);
1061 * Cannot simply call in_pcbconnect, because there might be an
1062 * earlier incarnation of this same connection still in
1063 * TIME_WAIT state, creating an ADDRINUSE error.
1065 error
= in6_pcbladdr(inp
, nam
, &addr6
, td
);
1068 oinp
= in6_pcblookup_hash(inp
->inp_cpcbinfo
,
1069 &sin6
->sin6_addr
, sin6
->sin6_port
,
1070 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
) ?
1071 addr6
: &inp
->in6p_laddr
,
1072 inp
->inp_lport
, 0, NULL
);
1074 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
1075 otp
->t_state
== TCPS_TIME_WAIT
&&
1076 (ticks
- otp
->t_starttime
) < tcp_msl
&&
1077 (otp
->t_flags
& TF_RCVD_CC
))
1078 otp
= tcp_close(otp
);
1080 return (EADDRINUSE
);
1082 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
1083 inp
->in6p_laddr
= *addr6
;
1084 inp
->in6p_faddr
= sin6
->sin6_addr
;
1085 inp
->inp_fport
= sin6
->sin6_port
;
1086 if ((sin6
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
) != 0)
1087 inp
->in6p_flowinfo
= sin6
->sin6_flowinfo
;
1088 in_pcbinsconnhash(inp
);
1090 /* Compute window scaling to request. */
1091 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
1092 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.ssb_hiwat
)
1093 tp
->request_r_scale
++;
1096 tcpstat
.tcps_connattempt
++;
1097 tp
->t_state
= TCPS_SYN_SENT
;
1098 callout_reset(tp
->tt_keep
, tcp_keepinit
, tcp_timer_keep
, tp
);
1099 tp
->iss
= tcp_new_isn(tp
);
1100 tcp_sendseqinit(tp
);
1103 * Generate a CC value for this connection and
1104 * check whether CC or CCnew should be used.
1106 if ((taop
= tcp_gettaocache(&tp
->t_inpcb
->inp_inc
)) == NULL
) {
1107 taop
= &tao_noncached
;
1108 bzero(taop
, sizeof *taop
);
1111 tp
->cc_send
= CC_INC(tcp_ccgen
);
1112 if (taop
->tao_ccsent
!= 0 &&
1113 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
1114 taop
->tao_ccsent
= tp
->cc_send
;
1116 taop
->tao_ccsent
= 0;
1117 tp
->t_flags
|= TF_SENDCCNEW
;
1125 * The new sockopt interface makes it possible for us to block in the
1126 * copyin/out step (if we take a page fault). Taking a page fault while
1127 * in a critical section is probably a Bad Thing. (Since sockets and pcbs
1128 * both now use TSM, there probably isn't any need for this function to
1129 * run in a critical section any more. This needs more examination.)
1132 tcp_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
1134 int error
, opt
, optval
;
1139 crit_enter(); /* XXX */
1143 return (ECONNRESET
);
1145 if (sopt
->sopt_level
!= IPPROTO_TCP
) {
1147 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
1148 error
= ip6_ctloutput(so
, sopt
);
1151 error
= ip_ctloutput(so
, sopt
);
1155 tp
= intotcpcb(inp
);
1157 switch (sopt
->sopt_dir
) {
1159 error
= soopt_to_kbuf(sopt
, &optval
, sizeof optval
,
1163 switch (sopt
->sopt_name
) {
1166 switch (sopt
->sopt_name
) {
1174 opt
= 0; /* dead code to fool gcc */
1181 tp
->t_flags
&= ~opt
;
1186 tp
->t_flags
|= TF_NOPUSH
;
1188 tp
->t_flags
&= ~TF_NOPUSH
;
1189 error
= tcp_output(tp
);
1194 if (optval
> 0 && optval
<= tp
->t_maxseg
)
1195 tp
->t_maxseg
= optval
;
1201 error
= ENOPROTOOPT
;
1207 switch (sopt
->sopt_name
) {
1209 optval
= tp
->t_flags
& TF_NODELAY
;
1212 optval
= tp
->t_maxseg
;
1215 optval
= tp
->t_flags
& TF_NOOPT
;
1218 optval
= tp
->t_flags
& TF_NOPUSH
;
1221 error
= ENOPROTOOPT
;
1225 soopt_from_kbuf(sopt
, &optval
, sizeof optval
);
1233 * tcp_sendspace and tcp_recvspace are the default send and receive window
1234 * sizes, respectively. These are obsolescent (this information should
1235 * be set by the route).
1237 u_long tcp_sendspace
= 1024*32;
1238 SYSCTL_INT(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
, CTLFLAG_RW
,
1239 &tcp_sendspace
, 0, "Maximum outgoing TCP datagram size");
1240 u_long tcp_recvspace
= 57344; /* largest multiple of PAGE_SIZE < 64k */
1241 SYSCTL_INT(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
, CTLFLAG_RW
,
1242 &tcp_recvspace
, 0, "Maximum incoming TCP datagram size");
1245 * Attach TCP protocol to socket, allocating
1246 * internet protocol control block, tcp control block,
1247 * bufer space, and entering LISTEN state if to accept connections.
1250 tcp_attach(struct socket
*so
, struct pru_attach_info
*ai
)
1257 int isipv6
= INP_CHECK_SOCKAF(so
, AF_INET6
) != 0;
1260 if (so
->so_snd
.ssb_hiwat
== 0 || so
->so_rcv
.ssb_hiwat
== 0) {
1261 error
= soreserve(so
, tcp_sendspace
, tcp_recvspace
,
1266 cpu
= mycpu
->gd_cpuid
;
1267 error
= in_pcballoc(so
, &tcbinfo
[cpu
]);
1273 inp
->inp_vflag
|= INP_IPV6
;
1274 inp
->in6p_hops
= -1; /* use kernel default */
1278 inp
->inp_vflag
|= INP_IPV4
;
1279 tp
= tcp_newtcpcb(inp
);
1281 int nofd
= so
->so_state
& SS_NOFDREF
; /* XXX */
1283 so
->so_state
&= ~SS_NOFDREF
; /* don't free the socket yet */
1290 so
->so_state
|= nofd
;
1293 tp
->t_state
= TCPS_CLOSED
;
1298 * Initiate (or continue) disconnect.
1299 * If embryonic state, just send reset (once).
1300 * If in ``let data drain'' option and linger null, just drop.
1301 * Otherwise (hard), mark socket disconnecting and drop
1302 * current input data; switch states based on user close, and
1303 * send segment to peer (with FIN).
1305 static struct tcpcb
*
1306 tcp_disconnect(struct tcpcb
*tp
)
1308 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1310 if (tp
->t_state
< TCPS_ESTABLISHED
)
1312 else if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
1313 tp
= tcp_drop(tp
, 0);
1315 soisdisconnecting(so
);
1316 sbflush(&so
->so_rcv
.sb
);
1317 tp
= tcp_usrclosed(tp
);
1325 * User issued close, and wish to trail through shutdown states:
1326 * if never received SYN, just forget it. If got a SYN from peer,
1327 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1328 * If already got a FIN from peer, then almost done; go to LAST_ACK
1329 * state. In all other cases, have already sent FIN to peer (e.g.
1330 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1331 * for peer to send FIN or not respond to keep-alives, etc.
1332 * We can let the user exit from the close as soon as the FIN is acked.
1334 static struct tcpcb
*
1335 tcp_usrclosed(struct tcpcb
*tp
)
1338 switch (tp
->t_state
) {
1342 tp
->t_state
= TCPS_CLOSED
;
1347 case TCPS_SYN_RECEIVED
:
1348 tp
->t_flags
|= TF_NEEDFIN
;
1351 case TCPS_ESTABLISHED
:
1352 tp
->t_state
= TCPS_FIN_WAIT_1
;
1355 case TCPS_CLOSE_WAIT
:
1356 tp
->t_state
= TCPS_LAST_ACK
;
1359 if (tp
&& tp
->t_state
>= TCPS_FIN_WAIT_2
) {
1360 soisdisconnected(tp
->t_inpcb
->inp_socket
);
1361 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1362 if (tp
->t_state
== TCPS_FIN_WAIT_2
)
1363 callout_reset(tp
->tt_2msl
, tcp_maxidle
,
1364 tcp_timer_2msl
, tp
);