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) 2003, 2004 Jeffrey M. Hsu. All rights reserved.
37 * License terms: all terms for the DragonFly license above plus the following:
39 * 4. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
42 * This product includes software developed by Jeffrey M. Hsu
43 * for the DragonFly Project.
45 * This requirement may be waived with permission from Jeffrey Hsu.
46 * This requirement will sunset and may be removed on July 8 2005,
47 * after which the standard DragonFly license (as shown above) will
52 * Copyright (c) 1982, 1986, 1988, 1993
53 * The Regents of the University of California. All rights reserved.
55 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions
58 * 1. Redistributions of source code must retain the above copyright
59 * notice, this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in the
62 * documentation and/or other materials provided with the distribution.
63 * 3. All advertising materials mentioning features or use of this software
64 * must display the following acknowledgement:
65 * This product includes software developed by the University of
66 * California, Berkeley and its contributors.
67 * 4. Neither the name of the University nor the names of its contributors
68 * may be used to endorse or promote products derived from this software
69 * without specific prior written permission.
71 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
84 * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.51.2.17 2002/10/11 11:46:44 ume Exp $
85 * $DragonFly: src/sys/netinet/tcp_usrreq.c,v 1.37 2006/01/14 11:33:50 swildner Exp $
88 #include "opt_ipsec.h"
89 #include "opt_inet6.h"
90 #include "opt_tcpdebug.h"
92 #include <sys/param.h>
93 #include <sys/systm.h>
94 #include <sys/kernel.h>
95 #include <sys/malloc.h>
96 #include <sys/sysctl.h>
97 #include <sys/globaldata.h>
98 #include <sys/thread.h>
100 #include <sys/mbuf.h>
102 #include <sys/domain.h>
104 #include <sys/socket.h>
105 #include <sys/socketvar.h>
106 #include <sys/protosw.h>
108 #include <sys/thread2.h>
109 #include <sys/msgport2.h>
112 #include <net/netisr.h>
113 #include <net/route.h>
115 #include <netinet/in.h>
116 #include <netinet/in_systm.h>
118 #include <netinet/ip6.h>
120 #include <netinet/in_pcb.h>
122 #include <netinet6/in6_pcb.h>
124 #include <netinet/in_var.h>
125 #include <netinet/ip_var.h>
127 #include <netinet6/ip6_var.h>
129 #include <netinet/tcp.h>
130 #include <netinet/tcp_fsm.h>
131 #include <netinet/tcp_seq.h>
132 #include <netinet/tcp_timer.h>
133 #include <netinet/tcp_var.h>
134 #include <netinet/tcpip.h>
136 #include <netinet/tcp_debug.h>
140 #include <netinet6/ipsec.h>
144 * TCP protocol interface to socket abstraction.
146 extern char *tcpstates[]; /* XXX ??? */
148 static int tcp_attach (struct socket *, struct pru_attach_info *);
149 static int tcp_connect (struct tcpcb *, struct sockaddr *,
152 static int tcp6_connect (struct tcpcb *, struct sockaddr *,
155 static struct tcpcb *
156 tcp_disconnect (struct tcpcb *);
157 static struct tcpcb *
158 tcp_usrclosed (struct tcpcb *);
161 #define TCPDEBUG0 int ostate = 0
162 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
163 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
164 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
168 #define TCPDEBUG2(req)
172 * TCP attaches to socket via pru_attach(), reserving space,
173 * and an internet control block.
176 tcp_usr_attach(struct socket *so, int proto, struct pru_attach_info *ai)
180 struct tcpcb *tp = 0;
191 error = tcp_attach(so, ai);
195 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
196 so->so_linger = TCP_LINGERTIME;
199 TCPDEBUG2(PRU_ATTACH);
205 * pru_detach() detaches the TCP protocol from the socket.
206 * If the protocol state is non-embryonic, then can't
207 * do this directly: have to initiate a pru_disconnect(),
208 * which may finish later; embryonic TCB's can just
212 tcp_usr_detach(struct socket *so)
223 return EINVAL; /* XXX */
227 * It's possible for the tcpcb (tp) to disconnect from the inp due
228 * to tcp_drop()->tcp_close() being called. This may occur *after*
229 * the detach message has been queued so we may find a NULL tp here.
231 if ((tp = intotcpcb(inp)) != NULL) {
233 tp = tcp_disconnect(tp);
234 TCPDEBUG2(PRU_DETACH);
240 #define COMMON_START(so, inp) \
250 tp = intotcpcb(inp); \
254 #define COMMON_END(req) out: TCPDEBUG2(req); crit_exit(); return error; goto out
258 * Give the socket an address.
261 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
266 struct sockaddr_in *sinp;
268 COMMON_START(so, inp);
271 * Must check for multicast addresses and disallow binding
274 sinp = (struct sockaddr_in *)nam;
275 if (sinp->sin_family == AF_INET &&
276 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
277 error = EAFNOSUPPORT;
280 error = in_pcbbind(inp, nam, td);
283 COMMON_END(PRU_BIND);
289 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
294 struct sockaddr_in6 *sin6p;
296 COMMON_START(so, inp);
299 * Must check for multicast addresses and disallow binding
302 sin6p = (struct sockaddr_in6 *)nam;
303 if (sin6p->sin6_family == AF_INET6 &&
304 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
305 error = EAFNOSUPPORT;
308 inp->inp_vflag &= ~INP_IPV4;
309 inp->inp_vflag |= INP_IPV6;
310 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
311 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
312 inp->inp_vflag |= INP_IPV4;
313 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
314 struct sockaddr_in sin;
316 in6_sin6_2_sin(&sin, sin6p);
317 inp->inp_vflag |= INP_IPV4;
318 inp->inp_vflag &= ~INP_IPV6;
319 error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
323 error = in6_pcbbind(inp, nam, td);
326 COMMON_END(PRU_BIND);
331 struct netmsg_inswildcard {
332 struct lwkt_msg nm_lmsg;
333 struct inpcb *nm_inp;
334 struct inpcbinfo *nm_pcbinfo;
338 in_pcbinswildcardhash_handler(struct lwkt_msg *msg0)
340 struct netmsg_inswildcard *msg = (struct netmsg_inswildcard *)msg0;
342 in_pcbinswildcardhash_oncpu(msg->nm_inp, msg->nm_pcbinfo);
343 lwkt_replymsg(&msg->nm_lmsg, 0);
349 * Prepare to accept connections.
352 tcp_usr_listen(struct socket *so, struct thread *td)
361 COMMON_START(so, inp);
362 if (inp->inp_lport == 0) {
363 error = in_pcbbind(inp, NULL, td);
368 tp->t_state = TCPS_LISTEN;
371 * We have to set the flag because we can't have other cpus
372 * messing with our inp's flags.
374 inp->inp_flags |= INP_WILDCARD_MP;
375 for (cpu = 0; cpu < ncpus2; cpu++) {
376 struct netmsg_inswildcard *msg;
378 if (cpu == mycpu->gd_cpuid) {
379 in_pcbinswildcardhash(inp);
383 msg = malloc(sizeof(struct netmsg_inswildcard), M_LWKTMSG,
385 lwkt_initmsg(&msg->nm_lmsg, &netisr_afree_rport, 0,
386 lwkt_cmd_func(in_pcbinswildcardhash_handler),
389 msg->nm_pcbinfo = &tcbinfo[cpu];
390 lwkt_sendmsg(tcp_cport(cpu), &msg->nm_lmsg);
393 in_pcbinswildcardhash(inp);
395 COMMON_END(PRU_LISTEN);
400 tcp6_usr_listen(struct socket *so, struct thread *td)
409 COMMON_START(so, inp);
410 if (inp->inp_lport == 0) {
411 if (!(inp->inp_flags & IN6P_IPV6_V6ONLY))
412 inp->inp_vflag |= INP_IPV4;
414 inp->inp_vflag &= ~INP_IPV4;
415 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
418 tp->t_state = TCPS_LISTEN;
421 * We have to set the flag because we can't have other cpus
422 * messing with our inp's flags.
424 inp->inp_flags |= INP_WILDCARD_MP;
425 for (cpu = 0; cpu < ncpus2; cpu++) {
426 struct netmsg_inswildcard *msg;
428 if (cpu == mycpu->gd_cpuid) {
429 in_pcbinswildcardhash(inp);
433 msg = malloc(sizeof(struct netmsg_inswildcard), M_LWKTMSG,
435 lwkt_initmsg(&msg->nm_lmsg, &netisr_afree_rport, 0,
436 lwkt_cmd_func(in_pcbinswildcardhash_handler),
439 msg->nm_pcbinfo = &tcbinfo[cpu];
440 lwkt_sendmsg(tcp_cport(cpu), &msg->nm_lmsg);
443 in_pcbinswildcardhash(inp);
445 COMMON_END(PRU_LISTEN);
450 * Initiate connection to peer.
451 * Create a template for use in transmissions on this connection.
452 * Enter SYN_SENT state, and mark socket as connecting.
453 * Start keep-alive timer, and seed output sequence space.
454 * Send initial segment on connection.
457 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
462 struct sockaddr_in *sinp;
464 COMMON_START(so, inp);
467 * Must disallow TCP ``connections'' to multicast addresses.
469 sinp = (struct sockaddr_in *)nam;
470 if (sinp->sin_family == AF_INET
471 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
472 error = EAFNOSUPPORT;
476 prison_remote_ip(td, 0, &sinp->sin_addr.s_addr);
478 if ((error = tcp_connect(tp, nam, td)) != 0)
480 error = tcp_output(tp);
481 COMMON_END(PRU_CONNECT);
486 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
491 struct sockaddr_in6 *sin6p;
493 COMMON_START(so, inp);
496 * Must disallow TCP ``connections'' to multicast addresses.
498 sin6p = (struct sockaddr_in6 *)nam;
499 if (sin6p->sin6_family == AF_INET6
500 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
501 error = EAFNOSUPPORT;
505 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
506 struct sockaddr_in sin;
508 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
513 in6_sin6_2_sin(&sin, sin6p);
514 inp->inp_vflag |= INP_IPV4;
515 inp->inp_vflag &= ~INP_IPV6;
516 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
518 error = tcp_output(tp);
521 inp->inp_vflag &= ~INP_IPV4;
522 inp->inp_vflag |= INP_IPV6;
523 inp->inp_inc.inc_isipv6 = 1;
524 if ((error = tcp6_connect(tp, nam, td)) != 0)
526 error = tcp_output(tp);
527 COMMON_END(PRU_CONNECT);
532 * Initiate disconnect from peer.
533 * If connection never passed embryonic stage, just drop;
534 * else if don't need to let data drain, then can just drop anyways,
535 * else have to begin TCP shutdown process: mark socket disconnecting,
536 * drain unread data, state switch to reflect user close, and
537 * send segment (e.g. FIN) to peer. Socket will be really disconnected
538 * when peer sends FIN and acks ours.
540 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
543 tcp_usr_disconnect(struct socket *so)
549 COMMON_START(so, inp);
550 tp = tcp_disconnect(tp);
551 COMMON_END(PRU_DISCONNECT);
555 * Accept a connection. Essentially all the work is
556 * done at higher levels; just return the address
557 * of the peer, storing through addr.
560 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
564 struct tcpcb *tp = NULL;
569 if (so->so_state & SS_ISDISCONNECTED) {
570 error = ECONNABORTED;
579 in_setpeeraddr(so, nam);
580 COMMON_END(PRU_ACCEPT);
585 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
589 struct tcpcb *tp = NULL;
595 if (so->so_state & SS_ISDISCONNECTED) {
596 error = ECONNABORTED;
605 in6_mapped_peeraddr(so, nam);
606 COMMON_END(PRU_ACCEPT);
610 * Mark the connection as being incapable of further output.
613 tcp_usr_shutdown(struct socket *so)
619 COMMON_START(so, inp);
621 tp = tcp_usrclosed(tp);
623 error = tcp_output(tp);
624 COMMON_END(PRU_SHUTDOWN);
628 * After a receive, possibly send window update to peer.
631 tcp_usr_rcvd(struct socket *so, int flags)
637 COMMON_START(so, inp);
639 COMMON_END(PRU_RCVD);
643 * Do a send by putting data in output queue and updating urgent
644 * marker if URG set. Possibly send more data. Unlike the other
645 * pru_*() routines, the mbuf chains are our responsibility. We
646 * must either enqueue them or free them. The other pru_* routines
647 * generally are caller-frees.
650 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
651 struct sockaddr *nam, struct mbuf *control, struct thread *td)
666 * OOPS! we lost a race, the TCP session got reset after
667 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
668 * network interrupt in the non-critical section of sosend().
674 error = ECONNRESET; /* XXX EPIPE? */
680 isipv6 = nam && nam->sa_family == AF_INET6;
685 /* TCP doesn't do control messages (rights, creds, etc) */
686 if (control->m_len) {
693 m_freem(control); /* empty control, just free it */
695 if(!(flags & PRUS_OOB)) {
696 sbappendstream(&so->so_snd, m);
697 if (nam && tp->t_state < TCPS_SYN_SENT) {
699 * Do implied connect if not yet connected,
700 * initialize window to default value, and
701 * initialize maxseg/maxopd using peer's cached
706 error = tcp6_connect(tp, nam, td);
709 error = tcp_connect(tp, nam, td);
712 tp->snd_wnd = TTCP_CLIENT_SND_WND;
716 if (flags & PRUS_EOF) {
718 * Close the send side of the connection after
722 tp = tcp_usrclosed(tp);
725 if (flags & PRUS_MORETOCOME)
726 tp->t_flags |= TF_MORETOCOME;
727 error = tcp_output(tp);
728 if (flags & PRUS_MORETOCOME)
729 tp->t_flags &= ~TF_MORETOCOME;
732 if (sbspace(&so->so_snd) < -512) {
738 * According to RFC961 (Assigned Protocols),
739 * the urgent pointer points to the last octet
740 * of urgent data. We continue, however,
741 * to consider it to indicate the first octet
742 * of data past the urgent section.
743 * Otherwise, snd_up should be one lower.
745 sbappendstream(&so->so_snd, m);
746 if (nam && tp->t_state < TCPS_SYN_SENT) {
748 * Do implied connect if not yet connected,
749 * initialize window to default value, and
750 * initialize maxseg/maxopd using peer's cached
755 error = tcp6_connect(tp, nam, td);
758 error = tcp_connect(tp, nam, td);
761 tp->snd_wnd = TTCP_CLIENT_SND_WND;
764 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
765 tp->t_flags |= TF_FORCE;
766 error = tcp_output(tp);
767 tp->t_flags &= ~TF_FORCE;
769 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
770 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
777 tcp_usr_abort(struct socket *so)
783 COMMON_START(so, inp);
784 tp = tcp_drop(tp, ECONNABORTED);
785 COMMON_END(PRU_ABORT);
789 * Receive out-of-band data.
792 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
798 COMMON_START(so, inp);
799 if ((so->so_oobmark == 0 &&
800 (so->so_state & SS_RCVATMARK) == 0) ||
801 so->so_options & SO_OOBINLINE ||
802 tp->t_oobflags & TCPOOB_HADDATA) {
806 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
811 *mtod(m, caddr_t) = tp->t_iobc;
812 if ((flags & MSG_PEEK) == 0)
813 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
814 COMMON_END(PRU_RCVOOB);
817 /* xxx - should be const */
818 struct pr_usrreqs tcp_usrreqs = {
819 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
820 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
821 tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
822 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
823 in_setsockaddr, sosend, soreceive, sopoll
827 struct pr_usrreqs tcp6_usrreqs = {
828 tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
829 tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
830 tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
831 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
832 in6_mapped_sockaddr, sosend, soreceive, sopoll
837 tcp_connect_oncpu(struct tcpcb *tp, struct sockaddr_in *sin,
838 struct sockaddr_in *if_sin)
840 struct inpcb *inp = tp->t_inpcb, *oinp;
841 struct socket *so = inp->inp_socket;
843 struct rmxp_tao *taop;
844 struct rmxp_tao tao_noncached;
846 oinp = in_pcblookup_hash(&tcbinfo[mycpu->gd_cpuid],
847 sin->sin_addr, sin->sin_port,
848 inp->inp_laddr.s_addr != INADDR_ANY ?
849 inp->inp_laddr : if_sin->sin_addr,
850 inp->inp_lport, 0, NULL);
852 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
853 otp->t_state == TCPS_TIME_WAIT &&
854 (ticks - otp->t_starttime) < tcp_msl &&
855 (otp->t_flags & TF_RCVD_CC))
860 if (inp->inp_laddr.s_addr == INADDR_ANY)
861 inp->inp_laddr = if_sin->sin_addr;
862 inp->inp_faddr = sin->sin_addr;
863 inp->inp_fport = sin->sin_port;
864 inp->inp_cpcbinfo = &tcbinfo[mycpu->gd_cpuid];
865 in_pcbinsconnhash(inp);
867 /* Compute window scaling to request. */
868 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
869 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
870 tp->request_r_scale++;
873 tcpstat.tcps_connattempt++;
874 tp->t_state = TCPS_SYN_SENT;
875 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
876 tp->iss = tcp_new_isn(tp);
880 * Generate a CC value for this connection and
881 * check whether CC or CCnew should be used.
883 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
884 taop = &tao_noncached;
885 bzero(taop, sizeof *taop);
888 tp->cc_send = CC_INC(tcp_ccgen);
889 if (taop->tao_ccsent != 0 &&
890 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
891 taop->tao_ccsent = tp->cc_send;
893 taop->tao_ccsent = 0;
894 tp->t_flags |= TF_SENDCCNEW;
902 struct netmsg_tcp_connect {
903 struct lwkt_msg nm_lmsg;
905 struct sockaddr_in *nm_sin;
906 struct sockaddr_in *nm_ifsin;
910 tcp_connect_handler(lwkt_msg_t lmsg)
912 struct netmsg_tcp_connect *msg = (void *)lmsg;
915 error = tcp_connect_oncpu(msg->nm_tp, msg->nm_sin, msg->nm_ifsin);
916 lwkt_replymsg(lmsg, error);
923 * Common subroutine to open a TCP connection to remote host specified
924 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
925 * port number if needed. Call in_pcbladdr to do the routing and to choose
926 * a local host address (interface). If there is an existing incarnation
927 * of the same connection in TIME-WAIT state and if the remote host was
928 * sending CC options and if the connection duration was < MSL, then
929 * truncate the previous TIME-WAIT state and proceed.
930 * Initialize connection parameters and enter SYN-SENT state.
933 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
935 struct inpcb *inp = tp->t_inpcb;
936 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
937 struct sockaddr_in *if_sin;
943 if (inp->inp_lport == 0) {
944 error = in_pcbbind(inp, (struct sockaddr *)NULL, td);
950 * Cannot simply call in_pcbconnect, because there might be an
951 * earlier incarnation of this same connection still in
952 * TIME_WAIT state, creating an ADDRINUSE error.
954 error = in_pcbladdr(inp, nam, &if_sin);
959 port = tcp_addrport(sin->sin_addr.s_addr, sin->sin_port,
960 inp->inp_laddr.s_addr ?
961 inp->inp_laddr.s_addr : if_sin->sin_addr.s_addr,
964 if (port->mp_td != curthread) {
965 struct netmsg_tcp_connect msg;
967 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
968 lwkt_cmd_func(tcp_connect_handler), lwkt_cmd_op_none);
971 msg.nm_ifsin = if_sin;
972 error = lwkt_domsg(port, &msg.nm_lmsg);
975 error = tcp_connect_oncpu(tp, sin, if_sin);
982 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
984 struct inpcb *inp = tp->t_inpcb, *oinp;
985 struct socket *so = inp->inp_socket;
987 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
988 struct in6_addr *addr6;
989 struct rmxp_tao *taop;
990 struct rmxp_tao tao_noncached;
993 if (inp->inp_lport == 0) {
994 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
1000 * Cannot simply call in_pcbconnect, because there might be an
1001 * earlier incarnation of this same connection still in
1002 * TIME_WAIT state, creating an ADDRINUSE error.
1004 error = in6_pcbladdr(inp, nam, &addr6);
1007 oinp = in6_pcblookup_hash(inp->inp_cpcbinfo,
1008 &sin6->sin6_addr, sin6->sin6_port,
1009 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
1010 addr6 : &inp->in6p_laddr,
1011 inp->inp_lport, 0, NULL);
1013 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
1014 otp->t_state == TCPS_TIME_WAIT &&
1015 (ticks - otp->t_starttime) < tcp_msl &&
1016 (otp->t_flags & TF_RCVD_CC))
1017 otp = tcp_close(otp);
1019 return (EADDRINUSE);
1021 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1022 inp->in6p_laddr = *addr6;
1023 inp->in6p_faddr = sin6->sin6_addr;
1024 inp->inp_fport = sin6->sin6_port;
1025 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
1026 inp->in6p_flowinfo = sin6->sin6_flowinfo;
1027 in_pcbinsconnhash(inp);
1029 /* Compute window scaling to request. */
1030 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1031 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1032 tp->request_r_scale++;
1035 tcpstat.tcps_connattempt++;
1036 tp->t_state = TCPS_SYN_SENT;
1037 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1038 tp->iss = tcp_new_isn(tp);
1039 tcp_sendseqinit(tp);
1042 * Generate a CC value for this connection and
1043 * check whether CC or CCnew should be used.
1045 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
1046 taop = &tao_noncached;
1047 bzero(taop, sizeof *taop);
1050 tp->cc_send = CC_INC(tcp_ccgen);
1051 if (taop->tao_ccsent != 0 &&
1052 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
1053 taop->tao_ccsent = tp->cc_send;
1055 taop->tao_ccsent = 0;
1056 tp->t_flags |= TF_SENDCCNEW;
1064 * The new sockopt interface makes it possible for us to block in the
1065 * copyin/out step (if we take a page fault). Taking a page fault while
1066 * in a critical section is probably a Bad Thing. (Since sockets and pcbs
1067 * both now use TSM, there probably isn't any need for this function to
1068 * run in a critical section any more. This needs more examination.)
1071 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1073 int error, opt, optval;
1078 crit_enter(); /* XXX */
1082 return (ECONNRESET);
1084 if (sopt->sopt_level != IPPROTO_TCP) {
1086 if (INP_CHECK_SOCKAF(so, AF_INET6))
1087 error = ip6_ctloutput(so, sopt);
1090 error = ip_ctloutput(so, sopt);
1094 tp = intotcpcb(inp);
1096 switch (sopt->sopt_dir) {
1098 switch (sopt->sopt_name) {
1101 error = sooptcopyin(sopt, &optval, sizeof optval,
1106 switch (sopt->sopt_name) {
1114 opt = 0; /* dead code to fool gcc */
1121 tp->t_flags &= ~opt;
1125 error = sooptcopyin(sopt, &optval, sizeof optval,
1131 tp->t_flags |= TF_NOPUSH;
1133 tp->t_flags &= ~TF_NOPUSH;
1134 error = tcp_output(tp);
1139 error = sooptcopyin(sopt, &optval, sizeof optval,
1144 if (optval > 0 && optval <= tp->t_maxseg)
1145 tp->t_maxseg = optval;
1151 error = ENOPROTOOPT;
1157 switch (sopt->sopt_name) {
1159 optval = tp->t_flags & TF_NODELAY;
1162 optval = tp->t_maxseg;
1165 optval = tp->t_flags & TF_NOOPT;
1168 optval = tp->t_flags & TF_NOPUSH;
1171 error = ENOPROTOOPT;
1175 error = sooptcopyout(sopt, &optval, sizeof optval);
1183 * tcp_sendspace and tcp_recvspace are the default send and receive window
1184 * sizes, respectively. These are obsolescent (this information should
1185 * be set by the route).
1187 u_long tcp_sendspace = 1024*32;
1188 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1189 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1190 u_long tcp_recvspace = 57344; /* largest multiple of PAGE_SIZE < 64k */
1191 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1192 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1195 * Attach TCP protocol to socket, allocating
1196 * internet protocol control block, tcp control block,
1197 * bufer space, and entering LISTEN state if to accept connections.
1200 tcp_attach(struct socket *so, struct pru_attach_info *ai)
1207 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1210 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1211 error = soreserve(so, tcp_sendspace, tcp_recvspace,
1216 cpu = mycpu->gd_cpuid;
1217 error = in_pcballoc(so, &tcbinfo[cpu]);
1223 inp->inp_vflag |= INP_IPV6;
1224 inp->in6p_hops = -1; /* use kernel default */
1228 inp->inp_vflag |= INP_IPV4;
1229 tp = tcp_newtcpcb(inp);
1231 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1233 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1240 so->so_state |= nofd;
1243 tp->t_state = TCPS_CLOSED;
1248 * Initiate (or continue) disconnect.
1249 * If embryonic state, just send reset (once).
1250 * If in ``let data drain'' option and linger null, just drop.
1251 * Otherwise (hard), mark socket disconnecting and drop
1252 * current input data; switch states based on user close, and
1253 * send segment to peer (with FIN).
1255 static struct tcpcb *
1256 tcp_disconnect(struct tcpcb *tp)
1258 struct socket *so = tp->t_inpcb->inp_socket;
1260 if (tp->t_state < TCPS_ESTABLISHED)
1262 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1263 tp = tcp_drop(tp, 0);
1265 soisdisconnecting(so);
1266 sbflush(&so->so_rcv);
1267 tp = tcp_usrclosed(tp);
1275 * User issued close, and wish to trail through shutdown states:
1276 * if never received SYN, just forget it. If got a SYN from peer,
1277 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1278 * If already got a FIN from peer, then almost done; go to LAST_ACK
1279 * state. In all other cases, have already sent FIN to peer (e.g.
1280 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1281 * for peer to send FIN or not respond to keep-alives, etc.
1282 * We can let the user exit from the close as soon as the FIN is acked.
1284 static struct tcpcb *
1285 tcp_usrclosed(struct tcpcb *tp)
1288 switch (tp->t_state) {
1292 tp->t_state = TCPS_CLOSED;
1297 case TCPS_SYN_RECEIVED:
1298 tp->t_flags |= TF_NEEDFIN;
1301 case TCPS_ESTABLISHED:
1302 tp->t_state = TCPS_FIN_WAIT_1;
1305 case TCPS_CLOSE_WAIT:
1306 tp->t_state = TCPS_LAST_ACK;
1309 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1310 soisdisconnected(tp->t_inpcb->inp_socket);
1311 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1312 if (tp->t_state == TCPS_FIN_WAIT_2)
1313 callout_reset(tp->tt_2msl, tcp_maxidle,
1314 tcp_timer_2msl, tp);