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. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
63 * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.51.2.17 2002/10/11 11:46:44 ume Exp $
66 #include "opt_ipsec.h"
68 #include "opt_inet6.h"
69 #include "opt_tcpdebug.h"
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
74 #include <sys/malloc.h>
75 #include <sys/sysctl.h>
76 #include <sys/globaldata.h>
77 #include <sys/thread.h>
81 #include <sys/domain.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/socketops.h>
86 #include <sys/protosw.h>
88 #include <sys/thread2.h>
89 #include <sys/msgport2.h>
90 #include <sys/socketvar2.h>
93 #include <net/netisr.h>
94 #include <net/route.h>
96 #include <net/netmsg2.h>
97 #include <net/netisr2.h>
99 #include <netinet/in.h>
100 #include <netinet/in_systm.h>
102 #include <netinet/ip6.h>
104 #include <netinet/in_pcb.h>
106 #include <netinet6/in6_pcb.h>
108 #include <netinet/in_var.h>
109 #include <netinet/ip_var.h>
111 #include <netinet6/ip6_var.h>
112 #include <netinet6/tcp6_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_timer2.h>
119 #include <netinet/tcp_var.h>
120 #include <netinet/tcpip.h>
122 #include <netinet/tcp_debug.h>
126 #include <netinet6/ipsec.h>
130 * TCP protocol interface to socket abstraction.
132 extern char *tcpstates
[]; /* XXX ??? */
134 static int tcp_attach (struct socket
*, struct pru_attach_info
*);
135 static void tcp_connect (netmsg_t msg
);
137 static void tcp6_connect (netmsg_t msg
);
138 static int tcp6_connect_oncpu(struct tcpcb
*tp
, int flags
,
140 struct sockaddr_in6
*sin6
,
141 struct in6_addr
*addr6
);
143 static struct tcpcb
*
144 tcp_disconnect (struct tcpcb
*);
145 static struct tcpcb
*
146 tcp_usrclosed (struct tcpcb
*);
149 #define TCPDEBUG0 int ostate = 0
150 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
151 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
152 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
156 #define TCPDEBUG2(req)
160 * For some ill optimized programs, which try to use TCP_NOPUSH
161 * to improve performance, will have small amount of data sits
162 * in the sending buffer. These small amount of data will _not_
163 * be pushed into the network until more data are written into
164 * the socket or the socket write side is shutdown.
166 static int tcp_disable_nopush
= 1;
167 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, disable_nopush
, CTLFLAG_RW
,
168 &tcp_disable_nopush
, 0, "TCP_NOPUSH socket option will have no effect");
171 * Allocate socket buffer space.
174 tcp_usr_preattach(struct socket
*so
, int proto __unused
,
175 struct pru_attach_info
*ai
)
179 if (so
->so_snd
.ssb_hiwat
== 0 || so
->so_rcv
.ssb_hiwat
== 0) {
180 error
= soreserve(so
, tcp_sendspace
, tcp_recvspace
,
185 atomic_set_int(&so
->so_rcv
.ssb_flags
, SSB_AUTOSIZE
);
186 atomic_set_int(&so
->so_snd
.ssb_flags
, SSB_AUTOSIZE
| SSB_PREALLOC
);
192 * TCP attaches to socket via pru_attach(), reserving space,
193 * and an internet control block. This socket may move to
194 * other CPU later when we bind/connect.
197 tcp_usr_attach(netmsg_t msg
)
199 struct socket
*so
= msg
->base
.nm_so
;
200 struct pru_attach_info
*ai
= msg
->attach
.nm_ai
;
203 struct tcpcb
*tp
= NULL
;
207 KASSERT(inp
== NULL
, ("tcp socket attached"));
210 error
= tcp_attach(so
, ai
);
214 if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
215 so
->so_linger
= TCP_LINGERTIME
;
218 TCPDEBUG2(PRU_ATTACH
);
219 lwkt_replymsg(&msg
->lmsg
, error
);
223 * pru_detach() detaches the TCP protocol from the socket.
224 * If the protocol state is non-embryonic, then can't
225 * do this directly: have to initiate a pru_disconnect(),
226 * which may finish later; embryonic TCB's can just
230 tcp_usr_detach(netmsg_t msg
)
232 struct socket
*so
= msg
->base
.nm_so
;
241 * If the inp is already detached or never attached, it may have
242 * been due to an async close or async attach failure. Just return
243 * as if no error occured.
247 KASSERT(tp
!= NULL
, ("tcp_usr_detach: tp is NULL"));
249 tp
= tcp_disconnect(tp
);
250 TCPDEBUG2(PRU_DETACH
);
252 lwkt_replymsg(&msg
->lmsg
, error
);
256 * NOTE: ignore_error is non-zero for certain disconnection races
257 * which we want to silently allow, otherwise close() may return
258 * an unexpected error.
260 * NOTE: The variables (msg) and (tp) are assumed.
262 #define COMMON_START(so, inp, ignore_error) \
268 error = ignore_error ? 0 : EINVAL; \
272 tp = intotcpcb(inp); \
276 #define COMMON_END1(req, noreply) \
280 lwkt_replymsg(&msg->lmsg, error); \
284 #define COMMON_END(req) COMMON_END1((req), 0)
287 tcp_sosetport(struct lwkt_msg
*msg
, lwkt_port_t port
)
289 sosetport(((struct netmsg_base
*)msg
)->nm_so
, port
);
293 * Give the socket an address.
296 tcp_usr_bind(netmsg_t msg
)
298 struct socket
*so
= msg
->bind
.base
.nm_so
;
299 struct sockaddr
*nam
= msg
->bind
.nm_nam
;
300 struct thread
*td
= msg
->bind
.nm_td
;
304 struct sockaddr_in
*sinp
;
305 lwkt_port_t port0
= netisr_cpuport(0);
307 COMMON_START(so
, inp
, 0);
310 * Must check for multicast addresses and disallow binding
313 sinp
= (struct sockaddr_in
*)nam
;
314 if (sinp
->sin_family
== AF_INET
&&
315 IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
316 error
= EAFNOSUPPORT
;
321 * Check "already bound" here (in_pcbbind() does the same check
322 * though), so we don't forward a connected socket to netisr0,
323 * which would panic in the following in_pcbunlink().
325 if (inp
->inp_lport
!= 0 || inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
326 error
= EINVAL
; /* already bound */
331 * Use netisr0 to serialize in_pcbbind(), so that pru_detach and
332 * pru_bind for different sockets on the same local port could be
333 * properly ordered. The original race is illustrated here for
338 * close(s1); <----- asynchronous
342 * All will expect bind(s2, *.PORT) to succeed. However, it will
343 * fail, if following sequence happens due to random socket initial
344 * msgport and asynchronous close(2):
348 * : pru_bind(s2) [*.PORT is used by s1]
351 if (&curthread
->td_msgport
!= port0
) {
352 lwkt_msg_t lmsg
= &msg
->bind
.base
.lmsg
;
354 KASSERT((msg
->bind
.nm_flags
& PRUB_RELINK
) == 0,
355 ("already asked to relink"));
357 in_pcbunlink(so
->so_pcb
, &tcbinfo
[mycpuid
]);
358 msg
->bind
.nm_flags
|= PRUB_RELINK
;
360 TCP_STATE_MIGRATE_START(tp
);
362 /* See the related comment in tcp_connect() */
363 lwkt_setmsg_receipt(lmsg
, tcp_sosetport
);
364 lwkt_forwardmsg(port0
, lmsg
);
365 /* msg invalid now */
368 KASSERT(so
->so_port
== port0
, ("so_port is not netisr0"));
370 if (msg
->bind
.nm_flags
& PRUB_RELINK
) {
371 msg
->bind
.nm_flags
&= ~PRUB_RELINK
;
372 TCP_STATE_MIGRATE_END(tp
);
373 in_pcblink(so
->so_pcb
, &tcbinfo
[mycpuid
]);
375 KASSERT(inp
->inp_pcbinfo
== &tcbinfo
[0], ("pcbinfo is not tcbinfo0"));
377 error
= in_pcbbind(inp
, nam
, td
);
381 COMMON_END(PRU_BIND
);
387 tcp6_usr_bind(netmsg_t msg
)
389 struct socket
*so
= msg
->bind
.base
.nm_so
;
390 struct sockaddr
*nam
= msg
->bind
.nm_nam
;
391 struct thread
*td
= msg
->bind
.nm_td
;
395 struct sockaddr_in6
*sin6p
;
397 COMMON_START(so
, inp
, 0);
400 * Must check for multicast addresses and disallow binding
403 sin6p
= (struct sockaddr_in6
*)nam
;
404 if (sin6p
->sin6_family
== AF_INET6
&&
405 IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
406 error
= EAFNOSUPPORT
;
409 error
= in6_pcbbind(inp
, nam
, td
);
412 COMMON_END(PRU_BIND
);
416 struct netmsg_inswildcard
{
417 struct netmsg_base base
;
418 struct inpcb
*nm_inp
;
422 in_pcbinswildcardhash_handler(netmsg_t msg
)
424 struct netmsg_inswildcard
*nm
= (struct netmsg_inswildcard
*)msg
;
425 int cpu
= mycpuid
, nextcpu
;
427 in_pcbinswildcardhash_oncpu(nm
->nm_inp
, &tcbinfo
[cpu
]);
430 if (nextcpu
< netisr_ncpus
)
431 lwkt_forwardmsg(netisr_cpuport(nextcpu
), &nm
->base
.lmsg
);
433 lwkt_replymsg(&nm
->base
.lmsg
, 0);
437 * Prepare to accept connections.
440 tcp_usr_listen(netmsg_t msg
)
442 struct socket
*so
= msg
->listen
.base
.nm_so
;
443 struct thread
*td
= msg
->listen
.nm_td
;
447 struct netmsg_inswildcard nm
;
448 lwkt_port_t port0
= netisr_cpuport(0);
450 COMMON_START(so
, inp
, 0);
452 if (&curthread
->td_msgport
!= port0
) {
453 lwkt_msg_t lmsg
= &msg
->listen
.base
.lmsg
;
455 KASSERT((msg
->listen
.nm_flags
& PRUL_RELINK
) == 0,
456 ("already asked to relink"));
458 in_pcbunlink(so
->so_pcb
, &tcbinfo
[mycpuid
]);
459 msg
->listen
.nm_flags
|= PRUL_RELINK
;
461 TCP_STATE_MIGRATE_START(tp
);
463 /* See the related comment in tcp_connect() */
464 lwkt_setmsg_receipt(lmsg
, tcp_sosetport
);
465 lwkt_forwardmsg(port0
, lmsg
);
466 /* msg invalid now */
469 KASSERT(so
->so_port
== port0
, ("so_port is not netisr0"));
471 if (msg
->listen
.nm_flags
& PRUL_RELINK
) {
472 msg
->listen
.nm_flags
&= ~PRUL_RELINK
;
473 TCP_STATE_MIGRATE_END(tp
);
474 in_pcblink(so
->so_pcb
, &tcbinfo
[mycpuid
]);
476 KASSERT(inp
->inp_pcbinfo
== &tcbinfo
[0], ("pcbinfo is not tcbinfo0"));
478 if (tp
->t_flags
& TF_LISTEN
)
481 if (inp
->inp_lport
== 0) {
482 error
= in_pcbbind(inp
, NULL
, td
);
487 TCP_STATE_CHANGE(tp
, TCPS_LISTEN
);
488 tp
->t_flags
|= TF_LISTEN
;
489 tp
->tt_msg
= NULL
; /* Catch any invalid timer usage */
492 * Create tcpcb per-cpu port cache
495 * This _must_ be done before installing this inpcb into
498 tcp_pcbport_create(tp
);
500 if (netisr_ncpus
> 1) {
502 * Put this inpcb into wildcard hash on other cpus.
504 ASSERT_INP_NOTINHASH(inp
);
505 netmsg_init(&nm
.base
, NULL
, &curthread
->td_msgport
,
506 MSGF_PRIORITY
, in_pcbinswildcardhash_handler
);
508 lwkt_domsg(netisr_cpuport(1), &nm
.base
.lmsg
, 0);
510 in_pcbinswildcardhash(inp
);
511 COMMON_END(PRU_LISTEN
);
517 tcp6_usr_listen(netmsg_t msg
)
519 struct socket
*so
= msg
->listen
.base
.nm_so
;
520 struct thread
*td
= msg
->listen
.nm_td
;
524 struct netmsg_inswildcard nm
;
526 COMMON_START(so
, inp
, 0);
528 if (tp
->t_flags
& TF_LISTEN
)
531 if (inp
->inp_lport
== 0) {
532 error
= in6_pcbbind(inp
, NULL
, td
);
537 TCP_STATE_CHANGE(tp
, TCPS_LISTEN
);
538 tp
->t_flags
|= TF_LISTEN
;
539 tp
->tt_msg
= NULL
; /* Catch any invalid timer usage */
542 * Create tcpcb per-cpu port cache
545 * This _must_ be done before installing this inpcb into
548 tcp_pcbport_create(tp
);
550 if (netisr_ncpus
> 1) {
552 * Put this inpcb into wildcard hash on other cpus.
554 KKASSERT(so
->so_port
== netisr_cpuport(0));
556 KKASSERT(inp
->inp_pcbinfo
== &tcbinfo
[0]);
557 ASSERT_INP_NOTINHASH(inp
);
559 netmsg_init(&nm
.base
, NULL
, &curthread
->td_msgport
,
560 MSGF_PRIORITY
, in_pcbinswildcardhash_handler
);
562 lwkt_domsg(netisr_cpuport(1), &nm
.base
.lmsg
, 0);
564 in_pcbinswildcardhash(inp
);
565 COMMON_END(PRU_LISTEN
);
570 * Initiate connection to peer.
571 * Create a template for use in transmissions on this connection.
572 * Enter SYN_SENT state, and mark socket as connecting.
573 * Start keep-alive timer, and seed output sequence space.
574 * Send initial segment on connection.
577 tcp_usr_connect(netmsg_t msg
)
579 struct socket
*so
= msg
->connect
.base
.nm_so
;
580 struct sockaddr
*nam
= msg
->connect
.nm_nam
;
581 struct thread
*td
= msg
->connect
.nm_td
;
585 struct sockaddr_in
*sinp
;
587 ASSERT_NETISR_NCPUS(mycpuid
);
589 COMMON_START(so
, inp
, 0);
592 * Must disallow TCP ``connections'' to multicast addresses.
594 sinp
= (struct sockaddr_in
*)nam
;
595 if (sinp
->sin_family
== AF_INET
596 && IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
597 error
= EAFNOSUPPORT
;
601 if (!prison_remote_ip(td
, (struct sockaddr
*)sinp
)) {
602 error
= EAFNOSUPPORT
; /* IPv6 only jail */
607 /* msg is invalid now */
610 if (msg
->connect
.nm_m
) {
611 m_freem(msg
->connect
.nm_m
);
612 msg
->connect
.nm_m
= NULL
;
614 if (msg
->connect
.nm_flags
& PRUC_HELDTD
)
616 if (error
&& (msg
->connect
.nm_flags
& PRUC_ASYNC
)) {
617 so
->so_error
= error
;
618 soisdisconnected(so
);
620 lwkt_replymsg(&msg
->lmsg
, error
);
626 tcp6_usr_connect(netmsg_t msg
)
628 struct socket
*so
= msg
->connect
.base
.nm_so
;
629 struct sockaddr
*nam
= msg
->connect
.nm_nam
;
630 struct thread
*td
= msg
->connect
.nm_td
;
634 struct sockaddr_in6
*sin6p
;
636 ASSERT_NETISR_NCPUS(mycpuid
);
638 COMMON_START(so
, inp
, 0);
641 * Must disallow TCP ``connections'' to multicast addresses.
643 sin6p
= (struct sockaddr_in6
*)nam
;
644 if (sin6p
->sin6_family
== AF_INET6
645 && IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
646 error
= EAFNOSUPPORT
;
650 if (!prison_remote_ip(td
, nam
)) {
651 error
= EAFNOSUPPORT
; /* IPv4 only jail */
655 /* Reject v4-mapped address */
656 if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
657 error
= EADDRNOTAVAIL
;
661 inp
->inp_inc
.inc_isipv6
= 1;
663 /* msg is invalid now */
666 if (msg
->connect
.nm_m
) {
667 m_freem(msg
->connect
.nm_m
);
668 msg
->connect
.nm_m
= NULL
;
670 lwkt_replymsg(&msg
->lmsg
, error
);
676 * Initiate disconnect from peer.
677 * If connection never passed embryonic stage, just drop;
678 * else if don't need to let data drain, then can just drop anyways,
679 * else have to begin TCP shutdown process: mark socket disconnecting,
680 * drain unread data, state switch to reflect user close, and
681 * send segment (e.g. FIN) to peer. Socket will be really disconnected
682 * when peer sends FIN and acks ours.
684 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
687 tcp_usr_disconnect(netmsg_t msg
)
689 struct socket
*so
= msg
->disconnect
.base
.nm_so
;
694 COMMON_START(so
, inp
, 1);
695 tp
= tcp_disconnect(tp
);
696 COMMON_END(PRU_DISCONNECT
);
700 * Accept a connection. Essentially all the work is
701 * done at higher levels; just return the address
702 * of the peer, storing through addr.
705 tcp_usr_accept(netmsg_t msg
)
707 struct socket
*so
= msg
->accept
.base
.nm_so
;
708 struct sockaddr
**nam
= msg
->accept
.nm_nam
;
711 struct tcpcb
*tp
= NULL
;
715 if (so
->so_state
& SS_ISDISCONNECTED
) {
716 error
= ECONNABORTED
;
726 in_setpeeraddr(so
, nam
);
727 COMMON_END(PRU_ACCEPT
);
732 tcp6_usr_accept(netmsg_t msg
)
734 struct socket
*so
= msg
->accept
.base
.nm_so
;
735 struct sockaddr
**nam
= msg
->accept
.nm_nam
;
738 struct tcpcb
*tp
= NULL
;
743 if (so
->so_state
& SS_ISDISCONNECTED
) {
744 error
= ECONNABORTED
;
753 in6_setpeeraddr(so
, nam
);
754 COMMON_END(PRU_ACCEPT
);
759 * Mark the connection as being incapable of further output.
762 tcp_usr_shutdown(netmsg_t msg
)
764 struct socket
*so
= msg
->shutdown
.base
.nm_so
;
769 COMMON_START(so
, inp
, 0);
771 tp
= tcp_usrclosed(tp
);
773 error
= tcp_output(tp
);
774 COMMON_END(PRU_SHUTDOWN
);
778 * After a receive, possibly send window update to peer.
781 tcp_usr_rcvd(netmsg_t msg
)
783 struct socket
*so
= msg
->rcvd
.base
.nm_so
;
784 int error
= 0, noreply
= 0;
788 COMMON_START(so
, inp
, 0);
790 if (msg
->rcvd
.nm_pru_flags
& PRUR_ASYNC
) {
792 so_async_rcvd_reply(so
);
796 COMMON_END1(PRU_RCVD
, noreply
);
800 * Do a send by putting data in output queue and updating urgent
801 * marker if URG set. Possibly send more data. Unlike the other
802 * pru_*() routines, the mbuf chains are our responsibility. We
803 * must either enqueue them or free them. The other pru_* routines
804 * generally are caller-frees.
807 tcp_usr_send(netmsg_t msg
)
809 struct socket
*so
= msg
->send
.base
.nm_so
;
810 int flags
= msg
->send
.nm_flags
;
811 struct mbuf
*m
= msg
->send
.nm_m
;
817 KKASSERT(msg
->send
.nm_control
== NULL
);
818 KKASSERT(msg
->send
.nm_addr
== NULL
);
819 KKASSERT((flags
& PRUS_FREEADDR
) == 0);
825 * OOPS! we lost a race, the TCP session got reset after
826 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
827 * network interrupt in the non-critical section of sosend().
830 error
= ECONNRESET
; /* XXX EPIPE? */
840 * This is no longer necessary, since:
841 * - sosendtcp() has already checked it for us
842 * - It does not work with asynchronized send
846 * Don't let too much OOB data build up
848 if (flags
& PRUS_OOB
) {
849 if (ssb_space(&so
->so_snd
) < -512) {
858 * Pump the data into the socket.
861 ssb_appendstream(&so
->so_snd
, m
);
864 if (flags
& PRUS_OOB
) {
866 * According to RFC961 (Assigned Protocols),
867 * the urgent pointer points to the last octet
868 * of urgent data. We continue, however,
869 * to consider it to indicate the first octet
870 * of data past the urgent section.
871 * Otherwise, snd_up should be one lower.
873 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.ssb_cc
;
874 tp
->t_flags
|= TF_FORCE
;
875 error
= tcp_output(tp
);
876 tp
->t_flags
&= ~TF_FORCE
;
878 if (flags
& PRUS_EOF
) {
880 * Close the send side of the connection after
884 tp
= tcp_usrclosed(tp
);
886 if (tp
!= NULL
&& !tcp_output_pending(tp
)) {
887 if (flags
& PRUS_MORETOCOME
)
888 tp
->t_flags
|= TF_MORETOCOME
;
889 error
= tcp_output_fair(tp
);
890 if (flags
& PRUS_MORETOCOME
)
891 tp
->t_flags
&= ~TF_MORETOCOME
;
894 COMMON_END1((flags
& PRUS_OOB
) ? PRU_SENDOOB
:
895 ((flags
& PRUS_EOF
) ? PRU_SEND_EOF
: PRU_SEND
),
896 (flags
& PRUS_NOREPLY
));
900 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
901 * will sofree() it when we return.
904 tcp_usr_abort(netmsg_t msg
)
906 struct socket
*so
= msg
->abort
.base
.nm_so
;
911 COMMON_START(so
, inp
, 1);
912 tp
= tcp_drop(tp
, ECONNABORTED
);
913 COMMON_END(PRU_ABORT
);
917 * Receive out-of-band data.
920 tcp_usr_rcvoob(netmsg_t msg
)
922 struct socket
*so
= msg
->rcvoob
.base
.nm_so
;
923 struct mbuf
*m
= msg
->rcvoob
.nm_m
;
924 int flags
= msg
->rcvoob
.nm_flags
;
929 COMMON_START(so
, inp
, 0);
930 if ((so
->so_oobmark
== 0 &&
931 (so
->so_state
& SS_RCVATMARK
) == 0) ||
932 so
->so_options
& SO_OOBINLINE
||
933 tp
->t_oobflags
& TCPOOB_HADDATA
) {
937 if ((tp
->t_oobflags
& TCPOOB_HAVEDATA
) == 0) {
942 *mtod(m
, caddr_t
) = tp
->t_iobc
;
943 if ((flags
& MSG_PEEK
) == 0)
944 tp
->t_oobflags
^= (TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
945 COMMON_END(PRU_RCVOOB
);
949 tcp_usr_savefaddr(struct socket
*so
, const struct sockaddr
*faddr
)
951 in_savefaddr(so
, faddr
);
956 tcp6_usr_savefaddr(struct socket
*so
, const struct sockaddr
*faddr
)
958 in6_savefaddr(so
, faddr
);
963 tcp_usr_preconnect(struct socket
*so
, const struct sockaddr
*nam
,
964 struct thread
*td __unused
)
966 const struct sockaddr_in
*sinp
;
968 sinp
= (const struct sockaddr_in
*)nam
;
969 if (sinp
->sin_family
== AF_INET
&&
970 IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
)))
977 /* xxx - should be const */
978 struct pr_usrreqs tcp_usrreqs
= {
979 .pru_abort
= tcp_usr_abort
,
980 .pru_accept
= tcp_usr_accept
,
981 .pru_attach
= tcp_usr_attach
,
982 .pru_bind
= tcp_usr_bind
,
983 .pru_connect
= tcp_usr_connect
,
984 .pru_connect2
= pr_generic_notsupp
,
985 .pru_control
= in_control_dispatch
,
986 .pru_detach
= tcp_usr_detach
,
987 .pru_disconnect
= tcp_usr_disconnect
,
988 .pru_listen
= tcp_usr_listen
,
989 .pru_peeraddr
= in_setpeeraddr_dispatch
,
990 .pru_rcvd
= tcp_usr_rcvd
,
991 .pru_rcvoob
= tcp_usr_rcvoob
,
992 .pru_send
= tcp_usr_send
,
993 .pru_sense
= pru_sense_null
,
994 .pru_shutdown
= tcp_usr_shutdown
,
995 .pru_sockaddr
= in_setsockaddr_dispatch
,
996 .pru_sosend
= sosendtcp
,
997 .pru_soreceive
= sorecvtcp
,
998 .pru_savefaddr
= tcp_usr_savefaddr
,
999 .pru_preconnect
= tcp_usr_preconnect
,
1000 .pru_preattach
= tcp_usr_preattach
1004 struct pr_usrreqs tcp6_usrreqs
= {
1005 .pru_abort
= tcp_usr_abort
,
1006 .pru_accept
= tcp6_usr_accept
,
1007 .pru_attach
= tcp_usr_attach
,
1008 .pru_bind
= tcp6_usr_bind
,
1009 .pru_connect
= tcp6_usr_connect
,
1010 .pru_connect2
= pr_generic_notsupp
,
1011 .pru_control
= in6_control_dispatch
,
1012 .pru_detach
= tcp_usr_detach
,
1013 .pru_disconnect
= tcp_usr_disconnect
,
1014 .pru_listen
= tcp6_usr_listen
,
1015 .pru_peeraddr
= in6_setpeeraddr_dispatch
,
1016 .pru_rcvd
= tcp_usr_rcvd
,
1017 .pru_rcvoob
= tcp_usr_rcvoob
,
1018 .pru_send
= tcp_usr_send
,
1019 .pru_sense
= pru_sense_null
,
1020 .pru_shutdown
= tcp_usr_shutdown
,
1021 .pru_sockaddr
= in6_setsockaddr_dispatch
,
1022 .pru_sosend
= sosendtcp
,
1023 .pru_soreceive
= sorecvtcp
,
1024 .pru_savefaddr
= tcp6_usr_savefaddr
1029 tcp_connect_oncpu(struct tcpcb
*tp
, int flags
, struct mbuf
*m
,
1030 const struct sockaddr_in
*sin
, struct sockaddr_in
*if_sin
,
1033 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
1034 struct socket
*so
= inp
->inp_socket
;
1035 struct route
*ro
= &inp
->inp_route
;
1037 KASSERT(inp
->inp_pcbinfo
== &tcbinfo
[mycpu
->gd_cpuid
],
1038 ("pcbinfo mismatch"));
1040 oinp
= in_pcblookup_hash(inp
->inp_pcbinfo
,
1041 sin
->sin_addr
, sin
->sin_port
,
1042 (inp
->inp_laddr
.s_addr
!= INADDR_ANY
?
1043 inp
->inp_laddr
: if_sin
->sin_addr
),
1044 inp
->inp_lport
, 0, NULL
);
1047 return (EADDRINUSE
);
1049 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
1050 inp
->inp_laddr
= if_sin
->sin_addr
;
1051 KASSERT(inp
->inp_faddr
.s_addr
== sin
->sin_addr
.s_addr
,
1052 ("faddr mismatch for reconnect"));
1053 KASSERT(inp
->inp_fport
== sin
->sin_port
,
1054 ("fport mismatch for reconnect"));
1055 in_pcbinsconnhash(inp
);
1057 inp
->inp_flags
|= INP_HASH
;
1058 inp
->inp_hashval
= hash
;
1061 * We are now on the inpcb's owner CPU, if the cached route was
1062 * freed because the rtentry's owner CPU is not the current CPU
1063 * (e.g. in tcp_connect()), then we try to reallocate it here with
1064 * the hope that a rtentry may be cloned from a RTF_PRCLONING
1067 if (!(inp
->inp_socket
->so_options
& SO_DONTROUTE
) && /*XXX*/
1068 ro
->ro_rt
== NULL
) {
1069 bzero(&ro
->ro_dst
, sizeof(struct sockaddr_in
));
1070 ro
->ro_dst
.sa_family
= AF_INET
;
1071 ro
->ro_dst
.sa_len
= sizeof(struct sockaddr_in
);
1072 ((struct sockaddr_in
*)&ro
->ro_dst
)->sin_addr
=
1078 * Now that no more errors can occur, change the protocol processing
1079 * port to the current thread (which is the correct thread).
1081 * Create TCP timer message now; we are on the tcpcb's owner
1084 tcp_create_timermsg(tp
, &curthread
->td_msgport
);
1087 * Compute window scaling to request. Use a larger scaling then
1088 * needed for the initial receive buffer in case the receive buffer
1091 if (tp
->request_r_scale
< TCP_MIN_WINSHIFT
)
1092 tp
->request_r_scale
= TCP_MIN_WINSHIFT
;
1093 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
1094 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.ssb_hiwat
1096 tp
->request_r_scale
++;
1100 tcpstat
.tcps_connattempt
++;
1101 TCP_STATE_CHANGE(tp
, TCPS_SYN_SENT
);
1102 tcp_callout_reset(tp
, tp
->tt_keep
, tp
->t_keepinit
, tcp_timer_keep
);
1103 tp
->iss
= tcp_new_isn(tp
);
1104 tcp_sendseqinit(tp
);
1106 ssb_appendstream(&so
->so_snd
, m
);
1108 if (flags
& PRUS_OOB
)
1109 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.ssb_cc
;
1113 * Close the send side of the connection after
1114 * the data is sent if flagged.
1116 if ((flags
& (PRUS_OOB
|PRUS_EOF
)) == PRUS_EOF
) {
1118 tp
= tcp_usrclosed(tp
);
1120 return (tcp_output(tp
));
1124 * Common subroutine to open a TCP connection to remote host specified
1125 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1126 * port number if needed. Call in_pcbladdr to do the routing and to choose
1127 * a local host address (interface).
1128 * Initialize connection parameters and enter SYN-SENT state.
1131 tcp_connect(netmsg_t msg
)
1133 struct socket
*so
= msg
->connect
.base
.nm_so
;
1134 struct sockaddr
*nam
= msg
->connect
.nm_nam
;
1135 struct thread
*td
= msg
->connect
.nm_td
;
1136 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
1137 struct sockaddr_in
*if_sin
= NULL
;
1144 COMMON_START(so
, inp
, 0);
1147 * Reconnect our pcb if we have to
1149 if (msg
->connect
.nm_flags
& PRUC_RECONNECT
) {
1150 msg
->connect
.nm_flags
&= ~PRUC_RECONNECT
;
1151 TCP_STATE_MIGRATE_END(tp
);
1152 in_pcblink(so
->so_pcb
, &tcbinfo
[mycpu
->gd_cpuid
]);
1154 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
1155 kprintf("inpcb %p, double-connect race\n", inp
);
1157 if (so
->so_state
& SS_ISCONNECTING
)
1161 KASSERT(inp
->inp_fport
== 0, ("invalid fport"));
1165 * Select local port, if it is not yet selected.
1167 if (inp
->inp_lport
== 0) {
1168 KKASSERT(inp
->inp_laddr
.s_addr
== INADDR_ANY
);
1170 error
= in_pcbladdr(inp
, nam
, &if_sin
, td
);
1173 inp
->inp_laddr
.s_addr
= if_sin
->sin_addr
.s_addr
;
1174 msg
->connect
.nm_flags
|= PRUC_HASLADDR
;
1177 * Install faddr/fport earlier, so that when this
1178 * inpcb is installed on to the lport hash, the
1179 * 4-tuple contains correct value.
1181 * NOTE: The faddr/fport will have to be installed
1182 * after the in_pcbladdr(), which may change them.
1184 inp
->inp_faddr
= sin
->sin_addr
;
1185 inp
->inp_fport
= sin
->sin_port
;
1187 error
= in_pcbbind_remote(inp
, nam
, td
);
1192 if ((msg
->connect
.nm_flags
& PRUC_HASLADDR
) == 0) {
1195 * This inpcb was bound before this connect.
1197 error
= in_pcbladdr(inp
, nam
, &if_sin
, td
);
1202 * Save or refresh the faddr/fport, since they may
1203 * be changed by in_pcbladdr().
1205 inp
->inp_faddr
= sin
->sin_addr
;
1206 inp
->inp_fport
= sin
->sin_port
;
1210 KASSERT(inp
->inp_faddr
.s_addr
== sin
->sin_addr
.s_addr
,
1211 ("faddr mismatch for reconnect"));
1212 KASSERT(inp
->inp_fport
== sin
->sin_port
,
1213 ("fport mismatch for reconnect"));
1216 KKASSERT(inp
->inp_socket
== so
);
1218 hash
= tcp_addrhash(sin
->sin_addr
.s_addr
, sin
->sin_port
,
1219 (inp
->inp_laddr
.s_addr
!= INADDR_ANY
?
1220 inp
->inp_laddr
.s_addr
: if_sin
->sin_addr
.s_addr
),
1222 port
= netisr_hashport(hash
);
1224 if (port
!= &curthread
->td_msgport
) {
1225 lwkt_msg_t lmsg
= &msg
->connect
.base
.lmsg
;
1228 * in_pcbladdr() may have allocated a route entry for us
1229 * on the current CPU, but we need a route entry on the
1230 * inpcb's owner CPU, so free it here.
1232 in_pcbresetroute(inp
);
1235 * We are moving the protocol processing port the socket
1236 * is on, we have to unlink here and re-link on the
1239 in_pcbunlink(so
->so_pcb
, &tcbinfo
[mycpu
->gd_cpuid
]);
1240 msg
->connect
.nm_flags
|= PRUC_RECONNECT
;
1241 msg
->connect
.base
.nm_dispatch
= tcp_connect
;
1243 TCP_STATE_MIGRATE_START(tp
);
1246 * Use message put done receipt to change this socket's
1247 * so_port, i.e. _after_ this message was put onto the
1248 * target netisr's msgport but _before_ the message could
1249 * be pulled from the target netisr's msgport, so that:
1250 * - The upper half (socket code) will not see the new
1251 * msgport before this message reaches the new msgport
1252 * and messages for this socket will be ordered.
1253 * - This message will see the new msgport, when its
1254 * handler is called in the target netisr.
1257 * We MUST use messege put done receipt to change this
1259 * If we changed the so_port in this netisr after the
1260 * lwkt_forwardmsg (so messages for this socket will be
1261 * ordered) and changed the so_port in the target netisr
1262 * at the very beginning of this message's handler, we
1263 * would suffer so_port overwritten race, given this
1264 * message might be forwarded again.
1267 * This mechanism depends on that the netisr's msgport
1268 * is spin msgport (currently it is :).
1270 * If the upper half saw the new msgport before this
1271 * message reached the target netisr's msgport, the
1272 * messages sent from the upper half could reach the new
1273 * msgport before this message, thus there would be
1274 * message reordering. The worst case could be soclose()
1275 * saw the new msgport and the detach message could reach
1276 * the new msgport before this message, i.e. the inpcb
1277 * could have been destroyed when this message was still
1278 * pending on or on its way to the new msgport. Other
1279 * weird cases could also happen, e.g. inpcb->inp_pcbinfo,
1280 * since we have unlinked this inpcb from the current
1283 lwkt_setmsg_receipt(lmsg
, tcp_sosetport
);
1284 lwkt_forwardmsg(port
, lmsg
);
1285 /* msg invalid now */
1287 } else if (msg
->connect
.nm_flags
& PRUC_HELDTD
) {
1289 * The original thread is no longer needed; release it.
1292 msg
->connect
.nm_flags
&= ~PRUC_HELDTD
;
1294 error
= tcp_connect_oncpu(tp
, msg
->connect
.nm_sndflags
,
1295 msg
->connect
.nm_m
, sin
, if_sin
, hash
);
1296 msg
->connect
.nm_m
= NULL
;
1298 if (msg
->connect
.nm_m
) {
1299 m_freem(msg
->connect
.nm_m
);
1300 msg
->connect
.nm_m
= NULL
;
1302 if (msg
->connect
.nm_flags
& PRUC_HELDTD
)
1304 if (error
&& (msg
->connect
.nm_flags
& PRUC_ASYNC
)) {
1305 so
->so_error
= error
;
1306 soisdisconnected(so
);
1308 lwkt_replymsg(&msg
->connect
.base
.lmsg
, error
);
1309 /* msg invalid now */
1315 tcp6_connect(netmsg_t msg
)
1318 struct socket
*so
= msg
->connect
.base
.nm_so
;
1319 struct sockaddr
*nam
= msg
->connect
.nm_nam
;
1320 struct thread
*td
= msg
->connect
.nm_td
;
1322 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
1323 struct in6_addr
*addr6
;
1327 COMMON_START(so
, inp
, 0);
1330 * Reconnect our pcb if we have to
1332 if (msg
->connect
.nm_flags
& PRUC_RECONNECT
) {
1333 msg
->connect
.nm_flags
&= ~PRUC_RECONNECT
;
1334 TCP_STATE_MIGRATE_END(tp
);
1335 in_pcblink(so
->so_pcb
, &tcbinfo
[mycpu
->gd_cpuid
]);
1339 * Bind if we have to
1341 if (inp
->inp_lport
== 0) {
1342 error
= in6_pcbbind(inp
, NULL
, td
);
1348 * Cannot simply call in_pcbconnect, because there might be an
1349 * earlier incarnation of this same connection still in
1350 * TIME_WAIT state, creating an ADDRINUSE error.
1352 error
= in6_pcbladdr(inp
, nam
, &addr6
, td
);
1356 port
= tcp6_addrport(); /* XXX hack for now, always cpu0 */
1358 if (port
!= &curthread
->td_msgport
) {
1359 lwkt_msg_t lmsg
= &msg
->connect
.base
.lmsg
;
1362 * in_pcbladdr() may have allocated a route entry for us
1363 * on the current CPU, but we need a route entry on the
1364 * inpcb's owner CPU, so free it here.
1366 in_pcbresetroute(inp
);
1368 in_pcbunlink(so
->so_pcb
, &tcbinfo
[mycpu
->gd_cpuid
]);
1369 msg
->connect
.nm_flags
|= PRUC_RECONNECT
;
1370 msg
->connect
.base
.nm_dispatch
= tcp6_connect
;
1372 TCP_STATE_MIGRATE_START(tp
);
1374 /* See the related comment in tcp_connect() */
1375 lwkt_setmsg_receipt(lmsg
, tcp_sosetport
);
1376 lwkt_forwardmsg(port
, lmsg
);
1377 /* msg invalid now */
1380 error
= tcp6_connect_oncpu(tp
, msg
->connect
.nm_sndflags
,
1381 &msg
->connect
.nm_m
, sin6
, addr6
);
1382 /* nm_m may still be intact */
1384 if (msg
->connect
.nm_m
) {
1385 m_freem(msg
->connect
.nm_m
);
1386 msg
->connect
.nm_m
= NULL
;
1388 lwkt_replymsg(&msg
->connect
.base
.lmsg
, error
);
1389 /* msg invalid now */
1393 tcp6_connect_oncpu(struct tcpcb
*tp
, int flags
, struct mbuf
**mp
,
1394 struct sockaddr_in6
*sin6
, struct in6_addr
*addr6
)
1396 struct mbuf
*m
= *mp
;
1397 struct inpcb
*inp
= tp
->t_inpcb
;
1398 struct socket
*so
= inp
->inp_socket
;
1402 * Cannot simply call in_pcbconnect, because there might be an
1403 * earlier incarnation of this same connection still in
1404 * TIME_WAIT state, creating an ADDRINUSE error.
1406 oinp
= in6_pcblookup_hash(inp
->inp_pcbinfo
,
1407 &sin6
->sin6_addr
, sin6
->sin6_port
,
1408 (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
) ?
1409 addr6
: &inp
->in6p_laddr
),
1410 inp
->inp_lport
, 0, NULL
);
1412 return (EADDRINUSE
);
1414 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
1415 inp
->in6p_laddr
= *addr6
;
1416 inp
->in6p_faddr
= sin6
->sin6_addr
;
1417 inp
->inp_fport
= sin6
->sin6_port
;
1418 if ((sin6
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
) != 0)
1419 inp
->in6p_flowinfo
= sin6
->sin6_flowinfo
;
1420 in_pcbinsconnhash(inp
);
1423 * Now that no more errors can occur, change the protocol processing
1424 * port to the current thread (which is the correct thread).
1426 * Create TCP timer message now; we are on the tcpcb's owner
1429 tcp_create_timermsg(tp
, &curthread
->td_msgport
);
1431 /* Compute window scaling to request. */
1432 if (tp
->request_r_scale
< TCP_MIN_WINSHIFT
)
1433 tp
->request_r_scale
= TCP_MIN_WINSHIFT
;
1434 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
1435 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.ssb_hiwat
) {
1436 tp
->request_r_scale
++;
1440 tcpstat
.tcps_connattempt
++;
1441 TCP_STATE_CHANGE(tp
, TCPS_SYN_SENT
);
1442 tcp_callout_reset(tp
, tp
->tt_keep
, tp
->t_keepinit
, tcp_timer_keep
);
1443 tp
->iss
= tcp_new_isn(tp
);
1444 tcp_sendseqinit(tp
);
1446 ssb_appendstream(&so
->so_snd
, m
);
1448 if (flags
& PRUS_OOB
)
1449 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.ssb_cc
;
1453 * Close the send side of the connection after
1454 * the data is sent if flagged.
1456 if ((flags
& (PRUS_OOB
|PRUS_EOF
)) == PRUS_EOF
) {
1458 tp
= tcp_usrclosed(tp
);
1460 return (tcp_output(tp
));
1466 * The new sockopt interface makes it possible for us to block in the
1467 * copyin/out step (if we take a page fault). Taking a page fault while
1468 * in a critical section is probably a Bad Thing. (Since sockets and pcbs
1469 * both now use TSM, there probably isn't any need for this function to
1470 * run in a critical section any more. This needs more examination.)
1473 tcp_ctloutput(netmsg_t msg
)
1475 struct socket
*so
= msg
->base
.nm_so
;
1476 struct sockopt
*sopt
= msg
->ctloutput
.nm_sopt
;
1477 struct thread
*td
= NULL
;
1478 int error
, opt
, optval
, opthz
;
1482 if (msg
->ctloutput
.nm_flags
& PRCO_HELDTD
)
1491 tp
= intotcpcb(inp
);
1493 /* Get socket's owner cpuid hint */
1494 if (sopt
->sopt_level
== SOL_SOCKET
&&
1495 sopt
->sopt_dir
== SOPT_GET
&&
1496 sopt
->sopt_name
== SO_CPUHINT
) {
1497 if (tp
->t_flags
& TF_LISTEN
) {
1499 * Listen sockets owner cpuid is always 0,
1500 * which does not make sense if SO_REUSEPORT
1503 * NOTE: inp_lgrpindex is _not_ assigned in jail.
1505 if ((so
->so_options
& SO_REUSEPORT
) &&
1506 inp
->inp_lgrpindex
>= 0)
1507 optval
= inp
->inp_lgrpindex
% netisr_ncpus
;
1509 optval
= -1; /* no hint */
1513 soopt_from_kbuf(sopt
, &optval
, sizeof(optval
));
1517 if (sopt
->sopt_level
!= IPPROTO_TCP
) {
1518 if (sopt
->sopt_level
== IPPROTO_IP
) {
1519 switch (sopt
->sopt_name
) {
1520 case IP_MULTICAST_IF
:
1521 case IP_MULTICAST_VIF
:
1522 case IP_MULTICAST_TTL
:
1523 case IP_MULTICAST_LOOP
:
1524 case IP_ADD_MEMBERSHIP
:
1525 case IP_DROP_MEMBERSHIP
:
1527 * Multicast does not make sense on
1535 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
1536 ip6_ctloutput_dispatch(msg
);
1540 /* msg invalid now */
1546 switch (sopt
->sopt_dir
) {
1548 error
= soopt_to_kbuf(sopt
, &optval
, sizeof optval
,
1552 switch (sopt
->sopt_name
) {
1555 tp
->t_keepidle
= tp
->t_keepintvl
;
1557 tp
->t_keepidle
= tcp_keepidle
;
1558 tcp_timer_keep_activity(tp
, 0);
1560 #ifdef TCP_SIGNATURE
1561 case TCP_SIGNATURE_ENABLE
:
1562 if (tp
->t_state
== TCPS_CLOSED
) {
1564 * This is the only safe state that this
1565 * option could be changed. Some segments
1566 * could already have been sent in other
1570 tp
->t_flags
|= TF_SIGNATURE
;
1572 tp
->t_flags
&= ~TF_SIGNATURE
;
1577 #endif /* TCP_SIGNATURE */
1580 switch (sopt
->sopt_name
) {
1588 opt
= 0; /* dead code to fool gcc */
1595 tp
->t_flags
&= ~opt
;
1599 if (tcp_disable_nopush
)
1602 tp
->t_flags
|= TF_NOPUSH
;
1604 tp
->t_flags
&= ~TF_NOPUSH
;
1605 error
= tcp_output(tp
);
1611 * Must be between 0 and maxseg. If the requested
1612 * maxseg is too small to satisfy the desired minmss,
1613 * pump it up (silently so sysctl modifications of
1614 * minmss do not create unexpected program failures).
1615 * Handle degenerate cases.
1617 if (optval
> 0 && optval
<= tp
->t_maxseg
) {
1618 if (optval
+ 40 < tcp_minmss
) {
1619 optval
= tcp_minmss
- 40;
1623 tp
->t_maxseg
= optval
;
1630 opthz
= ((int64_t)optval
* hz
) / 1000;
1632 tp
->t_keepinit
= opthz
;
1638 opthz
= ((int64_t)optval
* hz
) / 1000;
1640 tp
->t_keepidle
= opthz
;
1641 tcp_timer_keep_activity(tp
, 0);
1648 opthz
= ((int64_t)optval
* hz
) / 1000;
1650 tp
->t_keepintvl
= opthz
;
1651 tp
->t_maxidle
= tp
->t_keepintvl
* tp
->t_keepcnt
;
1659 tp
->t_keepcnt
= optval
;
1660 tp
->t_maxidle
= tp
->t_keepintvl
* tp
->t_keepcnt
;
1667 error
= ENOPROTOOPT
;
1673 switch (sopt
->sopt_name
) {
1674 #ifdef TCP_SIGNATURE
1675 case TCP_SIGNATURE_ENABLE
:
1676 optval
= (tp
->t_flags
& TF_SIGNATURE
) ? 1 : 0;
1678 #endif /* TCP_SIGNATURE */
1680 optval
= tp
->t_flags
& TF_NODELAY
;
1683 optval
= tp
->t_maxseg
;
1686 optval
= tp
->t_flags
& TF_NOOPT
;
1689 optval
= tp
->t_flags
& TF_NOPUSH
;
1692 optval
= ((int64_t)tp
->t_keepinit
* 1000) / hz
;
1695 optval
= ((int64_t)tp
->t_keepidle
* 1000) / hz
;
1698 optval
= ((int64_t)tp
->t_keepintvl
* 1000) / hz
;
1701 optval
= tp
->t_keepcnt
;
1704 error
= ENOPROTOOPT
;
1708 soopt_from_kbuf(sopt
, &optval
, sizeof optval
);
1714 lwkt_replymsg(&msg
->lmsg
, error
);
1717 struct netmsg_tcp_ctloutput
{
1718 struct netmsg_pr_ctloutput ctloutput
;
1719 struct sockopt sopt
;
1724 * Allocate netmsg_pr_ctloutput for asynchronous tcp_ctloutput.
1726 struct netmsg_pr_ctloutput
*
1727 tcp_ctloutmsg(struct sockopt
*sopt
)
1729 struct netmsg_tcp_ctloutput
*msg
;
1730 int flags
= 0, error
;
1732 KASSERT(sopt
->sopt_dir
== SOPT_SET
, ("not from ctloutput"));
1734 /* Only small set of options allows asynchronous setting. */
1735 if (sopt
->sopt_level
!= IPPROTO_TCP
)
1737 switch (sopt
->sopt_name
) {
1747 msg
= kmalloc(sizeof(*msg
), M_LWKTMSG
, M_WAITOK
| M_NULLOK
);
1749 /* Fallback to synchronous tcp_ctloutput */
1753 /* Save the sockopt */
1756 /* Fixup the sopt.sopt_val ptr */
1757 error
= sooptcopyin(sopt
, &msg
->sopt_val
,
1758 sizeof(msg
->sopt_val
), sizeof(msg
->sopt_val
));
1760 kfree(msg
, M_LWKTMSG
);
1763 msg
->sopt
.sopt_val
= &msg
->sopt_val
;
1765 /* Hold the current thread */
1766 if (msg
->sopt
.sopt_td
!= NULL
) {
1767 flags
|= PRCO_HELDTD
;
1768 lwkt_hold(msg
->sopt
.sopt_td
);
1771 msg
->ctloutput
.nm_flags
= flags
;
1772 msg
->ctloutput
.nm_sopt
= &msg
->sopt
;
1774 return &msg
->ctloutput
;
1778 * tcp_sendspace and tcp_recvspace are the default send and receive window
1779 * sizes, respectively. These are obsolescent (this information should
1780 * be set by the route).
1782 * Use a default that does not require tcp window scaling to be turned
1783 * on. Individual programs or the administrator can increase the default.
1785 u_long tcp_sendspace
= 57344; /* largest multiple of PAGE_SIZE < 64k */
1786 SYSCTL_INT(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
, CTLFLAG_RW
,
1787 &tcp_sendspace
, 0, "Maximum outgoing TCP datagram size");
1788 u_long tcp_recvspace
= 57344; /* largest multiple of PAGE_SIZE < 64k */
1789 SYSCTL_INT(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
, CTLFLAG_RW
,
1790 &tcp_recvspace
, 0, "Maximum incoming TCP datagram size");
1793 * Attach TCP protocol to socket, allocating internet protocol control
1794 * block, tcp control block, buffer space, and entering CLOSED state.
1797 tcp_attach(struct socket
*so
, struct pru_attach_info
*ai
)
1803 boolean_t isipv6
= INP_CHECK_SOCKAF(so
, AF_INET6
);
1807 error
= tcp_usr_preattach(so
, 0 /* don't care */, ai
);
1811 /* Post attach; do nothing */
1814 cpu
= mycpu
->gd_cpuid
;
1817 * Set the default pcbinfo. This will likely change when we
1820 error
= in_pcballoc(so
, &tcbinfo
[cpu
]);
1826 inp
->in6p_hops
= -1; /* use kernel default */
1829 /* Keep a reference for asynchronized pru_rcvd */
1835 * Initiate (or continue) disconnect.
1836 * If embryonic state, just send reset (once).
1837 * If in ``let data drain'' option and linger null, just drop.
1838 * Otherwise (hard), mark socket disconnecting and drop
1839 * current input data; switch states based on user close, and
1840 * send segment to peer (with FIN).
1842 static struct tcpcb
*
1843 tcp_disconnect(struct tcpcb
*tp
)
1845 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1847 if (tp
->t_state
< TCPS_ESTABLISHED
) {
1849 } else if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0) {
1850 tp
= tcp_drop(tp
, 0);
1852 lwkt_gettoken(&so
->so_rcv
.ssb_token
);
1853 soisdisconnecting(so
);
1854 sbflush(&so
->so_rcv
.sb
);
1855 tp
= tcp_usrclosed(tp
);
1858 lwkt_reltoken(&so
->so_rcv
.ssb_token
);
1864 * User issued close, and wish to trail through shutdown states:
1865 * if never received SYN, just forget it. If got a SYN from peer,
1866 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1867 * If already got a FIN from peer, then almost done; go to LAST_ACK
1868 * state. In all other cases, have already sent FIN to peer (e.g.
1869 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1870 * for peer to send FIN or not respond to keep-alives, etc.
1871 * We can let the user exit from the close as soon as the FIN is acked.
1873 static struct tcpcb
*
1874 tcp_usrclosed(struct tcpcb
*tp
)
1877 switch (tp
->t_state
) {
1881 TCP_STATE_CHANGE(tp
, TCPS_CLOSED
);
1886 case TCPS_SYN_RECEIVED
:
1887 tp
->t_flags
|= TF_NEEDFIN
;
1890 case TCPS_ESTABLISHED
:
1891 TCP_STATE_CHANGE(tp
, TCPS_FIN_WAIT_1
);
1894 case TCPS_CLOSE_WAIT
:
1895 TCP_STATE_CHANGE(tp
, TCPS_LAST_ACK
);
1898 if (tp
&& tp
->t_state
>= TCPS_FIN_WAIT_2
) {
1899 soisdisconnected(tp
->t_inpcb
->inp_socket
);
1900 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1901 if (tp
->t_state
== TCPS_FIN_WAIT_2
) {
1902 tcp_callout_reset(tp
, tp
->tt_2msl
, tp
->t_maxidle
,