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.51 2008/09/29 20:52:23 dillon 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_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 int tcp_connect (struct tcpcb *, struct sockaddr *,
138 static int tcp6_connect (struct tcpcb *, struct sockaddr *,
141 static struct tcpcb *
142 tcp_disconnect (struct tcpcb *);
143 static struct tcpcb *
144 tcp_usrclosed (struct tcpcb *);
147 #define TCPDEBUG0 int ostate = 0
148 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
149 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
150 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
154 #define TCPDEBUG2(req)
158 * TCP attaches to socket via pru_attach(), reserving space,
159 * and an internet control block.
162 tcp_usr_attach(struct socket *so, int proto, struct pru_attach_info *ai)
166 struct tcpcb *tp = 0;
177 error = tcp_attach(so, ai);
181 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
182 so->so_linger = TCP_LINGERTIME;
185 TCPDEBUG2(PRU_ATTACH);
191 * pru_detach() detaches the TCP protocol from the socket.
192 * If the protocol state is non-embryonic, then can't
193 * do this directly: have to initiate a pru_disconnect(),
194 * which may finish later; embryonic TCB's can just
198 tcp_usr_detach(struct socket *so)
209 * If the inp is already detached it may have been due to an async
210 * close. Just return as if no error occured.
218 * It's possible for the tcpcb (tp) to disconnect from the inp due
219 * to tcp_drop()->tcp_close() being called. This may occur *after*
220 * the detach message has been queued so we may find a NULL tp here.
222 if ((tp = intotcpcb(inp)) != NULL) {
224 tp = tcp_disconnect(tp);
225 TCPDEBUG2(PRU_DETACH);
232 * Note: ignore_error is non-zero for certain disconnection races
233 * which we want to silently allow, otherwise close() may return
234 * an unexpected error.
236 #define COMMON_START(so, inp, ignore_error) \
244 return (ignore_error ? 0 : EINVAL); \
246 tp = intotcpcb(inp); \
250 #define COMMON_END(req) out: TCPDEBUG2(req); crit_exit(); return error; goto out
254 * Give the socket an address.
257 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
262 struct sockaddr_in *sinp;
264 COMMON_START(so, inp, 0);
267 * Must check for multicast addresses and disallow binding
270 sinp = (struct sockaddr_in *)nam;
271 if (sinp->sin_family == AF_INET &&
272 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
273 error = EAFNOSUPPORT;
276 error = in_pcbbind(inp, nam, td);
279 COMMON_END(PRU_BIND);
285 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
290 struct sockaddr_in6 *sin6p;
292 COMMON_START(so, inp, 0);
295 * Must check for multicast addresses and disallow binding
298 sin6p = (struct sockaddr_in6 *)nam;
299 if (sin6p->sin6_family == AF_INET6 &&
300 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
301 error = EAFNOSUPPORT;
304 inp->inp_vflag &= ~INP_IPV4;
305 inp->inp_vflag |= INP_IPV6;
306 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
307 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
308 inp->inp_vflag |= INP_IPV4;
309 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
310 struct sockaddr_in sin;
312 in6_sin6_2_sin(&sin, sin6p);
313 inp->inp_vflag |= INP_IPV4;
314 inp->inp_vflag &= ~INP_IPV6;
315 error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
319 error = in6_pcbbind(inp, nam, td);
322 COMMON_END(PRU_BIND);
327 struct netmsg_inswildcard {
328 struct netmsg nm_netmsg;
329 struct inpcb *nm_inp;
330 struct inpcbinfo *nm_pcbinfo;
334 in_pcbinswildcardhash_handler(struct netmsg *msg0)
336 struct netmsg_inswildcard *msg = (struct netmsg_inswildcard *)msg0;
338 in_pcbinswildcardhash_oncpu(msg->nm_inp, msg->nm_pcbinfo);
339 lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, 0);
344 * Prepare to accept connections.
347 tcp_usr_listen(struct socket *so, struct thread *td)
356 COMMON_START(so, inp, 0);
357 if (inp->inp_lport == 0) {
358 error = in_pcbbind(inp, NULL, td);
363 tp->t_state = TCPS_LISTEN;
364 tp->tt_msg = NULL; /* Catch any invalid timer usage */
367 * We have to set the flag because we can't have other cpus
368 * messing with our inp's flags.
370 inp->inp_flags |= INP_WILDCARD_MP;
371 for (cpu = 0; cpu < ncpus2; cpu++) {
372 struct netmsg_inswildcard *msg;
374 if (cpu == mycpu->gd_cpuid) {
375 in_pcbinswildcardhash(inp);
379 msg = kmalloc(sizeof(struct netmsg_inswildcard), M_LWKTMSG,
381 netmsg_init(&msg->nm_netmsg, &netisr_afree_rport, 0,
382 in_pcbinswildcardhash_handler);
384 msg->nm_pcbinfo = &tcbinfo[cpu];
385 lwkt_sendmsg(tcp_cport(cpu), &msg->nm_netmsg.nm_lmsg);
388 in_pcbinswildcardhash(inp);
390 COMMON_END(PRU_LISTEN);
395 tcp6_usr_listen(struct socket *so, struct thread *td)
404 COMMON_START(so, inp, 0);
405 if (inp->inp_lport == 0) {
406 if (!(inp->inp_flags & IN6P_IPV6_V6ONLY))
407 inp->inp_vflag |= INP_IPV4;
409 inp->inp_vflag &= ~INP_IPV4;
410 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
413 tp->t_state = TCPS_LISTEN;
416 * We have to set the flag because we can't have other cpus
417 * messing with our inp's flags.
419 inp->inp_flags |= INP_WILDCARD_MP;
420 for (cpu = 0; cpu < ncpus2; cpu++) {
421 struct netmsg_inswildcard *msg;
423 if (cpu == mycpu->gd_cpuid) {
424 in_pcbinswildcardhash(inp);
428 msg = kmalloc(sizeof(struct netmsg_inswildcard), M_LWKTMSG,
430 netmsg_init(&msg->nm_netmsg, &netisr_afree_rport, 0,
431 in_pcbinswildcardhash_handler);
433 msg->nm_pcbinfo = &tcbinfo[cpu];
434 lwkt_sendmsg(tcp_cport(cpu), &msg->nm_netmsg.nm_lmsg);
437 in_pcbinswildcardhash(inp);
439 COMMON_END(PRU_LISTEN);
445 tcp_output_dispatch(struct netmsg *nmsg)
447 struct lwkt_msg *msg = &nmsg->nm_lmsg;
448 struct tcpcb *tp = msg->u.ms_resultp;
451 error = tcp_output(tp);
452 lwkt_replymsg(msg, error);
457 * Initiate connection to peer.
458 * Create a template for use in transmissions on this connection.
459 * Enter SYN_SENT state, and mark socket as connecting.
460 * Start keep-alive timer, and seed output sequence space.
461 * Send initial segment on connection.
464 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
469 struct sockaddr_in *sinp;
474 COMMON_START(so, inp, 0);
477 * Must disallow TCP ``connections'' to multicast addresses.
479 sinp = (struct sockaddr_in *)nam;
480 if (sinp->sin_family == AF_INET
481 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
482 error = EAFNOSUPPORT;
486 if (!prison_remote_ip(td, (struct sockaddr*)sinp)) {
487 error = EAFNOSUPPORT; /* IPv6 only jail */
491 if ((error = tcp_connect(tp, nam, td)) != 0)
495 port = tcp_addrport(inp->inp_faddr.s_addr, inp->inp_fport,
496 inp->inp_laddr.s_addr, inp->inp_lport);
497 if (port != &curthread->td_msgport) {
499 struct lwkt_msg *msg;
501 netmsg_init(&nmsg, &curthread->td_msgport, 0,
502 tcp_output_dispatch);
504 msg->u.ms_resultp = tp;
506 error = lwkt_domsg(port, msg, 0);
509 error = tcp_output(tp);
510 COMMON_END(PRU_CONNECT);
515 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
520 struct sockaddr_in6 *sin6p;
522 COMMON_START(so, inp, 0);
525 * Must disallow TCP ``connections'' to multicast addresses.
527 sin6p = (struct sockaddr_in6 *)nam;
528 if (sin6p->sin6_family == AF_INET6
529 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
530 error = EAFNOSUPPORT;
534 if (!prison_remote_ip(td, nam)) {
535 error = EAFNOSUPPORT; /* IPv4 only jail */
539 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
540 struct sockaddr_in sin;
542 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
547 in6_sin6_2_sin(&sin, sin6p);
548 inp->inp_vflag |= INP_IPV4;
549 inp->inp_vflag &= ~INP_IPV6;
550 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
552 error = tcp_output(tp);
555 inp->inp_vflag &= ~INP_IPV4;
556 inp->inp_vflag |= INP_IPV6;
557 inp->inp_inc.inc_isipv6 = 1;
558 if ((error = tcp6_connect(tp, nam, td)) != 0)
560 error = tcp_output(tp);
561 COMMON_END(PRU_CONNECT);
566 * Initiate disconnect from peer.
567 * If connection never passed embryonic stage, just drop;
568 * else if don't need to let data drain, then can just drop anyways,
569 * else have to begin TCP shutdown process: mark socket disconnecting,
570 * drain unread data, state switch to reflect user close, and
571 * send segment (e.g. FIN) to peer. Socket will be really disconnected
572 * when peer sends FIN and acks ours.
574 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
577 tcp_usr_disconnect(struct socket *so)
583 COMMON_START(so, inp, 1);
584 tp = tcp_disconnect(tp);
585 COMMON_END(PRU_DISCONNECT);
589 * Accept a connection. Essentially all the work is
590 * done at higher levels; just return the address
591 * of the peer, storing through addr.
594 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
598 struct tcpcb *tp = NULL;
603 if (so->so_state & SS_ISDISCONNECTED) {
604 error = ECONNABORTED;
613 in_setpeeraddr(so, nam);
614 COMMON_END(PRU_ACCEPT);
619 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
623 struct tcpcb *tp = NULL;
629 if (so->so_state & SS_ISDISCONNECTED) {
630 error = ECONNABORTED;
639 in6_mapped_peeraddr(so, nam);
640 COMMON_END(PRU_ACCEPT);
644 * Mark the connection as being incapable of further output.
647 tcp_usr_shutdown(struct socket *so)
653 COMMON_START(so, inp, 0);
655 tp = tcp_usrclosed(tp);
657 error = tcp_output(tp);
658 COMMON_END(PRU_SHUTDOWN);
662 * After a receive, possibly send window update to peer.
665 tcp_usr_rcvd(struct socket *so, int flags)
671 COMMON_START(so, inp, 0);
673 COMMON_END(PRU_RCVD);
677 * Do a send by putting data in output queue and updating urgent
678 * marker if URG set. Possibly send more data. Unlike the other
679 * pru_*() routines, the mbuf chains are our responsibility. We
680 * must either enqueue them or free them. The other pru_* routines
681 * generally are caller-frees.
684 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
685 struct sockaddr *nam, struct mbuf *control, struct thread *td)
700 * OOPS! we lost a race, the TCP session got reset after
701 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
702 * network interrupt in the non-critical section of sosend().
708 error = ECONNRESET; /* XXX EPIPE? */
714 isipv6 = nam && nam->sa_family == AF_INET6;
719 /* TCP doesn't do control messages (rights, creds, etc) */
720 if (control->m_len) {
727 m_freem(control); /* empty control, just free it */
729 if(!(flags & PRUS_OOB)) {
730 ssb_appendstream(&so->so_snd, m);
731 if (nam && tp->t_state < TCPS_SYN_SENT) {
733 * Do implied connect if not yet connected,
734 * initialize window to default value, and
735 * initialize maxseg/maxopd using peer's cached
740 error = tcp6_connect(tp, nam, td);
743 error = tcp_connect(tp, nam, td);
746 tp->snd_wnd = TTCP_CLIENT_SND_WND;
750 if (flags & PRUS_EOF) {
752 * Close the send side of the connection after
756 tp = tcp_usrclosed(tp);
759 if (flags & PRUS_MORETOCOME)
760 tp->t_flags |= TF_MORETOCOME;
761 error = tcp_output(tp);
762 if (flags & PRUS_MORETOCOME)
763 tp->t_flags &= ~TF_MORETOCOME;
766 if (ssb_space(&so->so_snd) < -512) {
772 * According to RFC961 (Assigned Protocols),
773 * the urgent pointer points to the last octet
774 * of urgent data. We continue, however,
775 * to consider it to indicate the first octet
776 * of data past the urgent section.
777 * Otherwise, snd_up should be one lower.
779 ssb_appendstream(&so->so_snd, m);
780 if (nam && tp->t_state < TCPS_SYN_SENT) {
782 * Do implied connect if not yet connected,
783 * initialize window to default value, and
784 * initialize maxseg/maxopd using peer's cached
789 error = tcp6_connect(tp, nam, td);
792 error = tcp_connect(tp, nam, td);
795 tp->snd_wnd = TTCP_CLIENT_SND_WND;
798 tp->snd_up = tp->snd_una + so->so_snd.ssb_cc;
799 tp->t_flags |= TF_FORCE;
800 error = tcp_output(tp);
801 tp->t_flags &= ~TF_FORCE;
803 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
804 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
811 tcp_usr_abort(struct socket *so)
817 COMMON_START(so, inp, 1);
818 tp = tcp_drop(tp, ECONNABORTED);
819 COMMON_END(PRU_ABORT);
823 * Receive out-of-band data.
826 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
832 COMMON_START(so, inp, 0);
833 if ((so->so_oobmark == 0 &&
834 (so->so_state & SS_RCVATMARK) == 0) ||
835 so->so_options & SO_OOBINLINE ||
836 tp->t_oobflags & TCPOOB_HADDATA) {
840 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
845 *mtod(m, caddr_t) = tp->t_iobc;
846 if ((flags & MSG_PEEK) == 0)
847 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
848 COMMON_END(PRU_RCVOOB);
851 /* xxx - should be const */
852 struct pr_usrreqs tcp_usrreqs = {
853 .pru_abort = tcp_usr_abort,
854 .pru_accept = tcp_usr_accept,
855 .pru_attach = tcp_usr_attach,
856 .pru_bind = tcp_usr_bind,
857 .pru_connect = tcp_usr_connect,
858 .pru_connect2 = pru_connect2_notsupp,
859 .pru_control = in_control,
860 .pru_detach = tcp_usr_detach,
861 .pru_disconnect = tcp_usr_disconnect,
862 .pru_listen = tcp_usr_listen,
863 .pru_peeraddr = in_setpeeraddr,
864 .pru_rcvd = tcp_usr_rcvd,
865 .pru_rcvoob = tcp_usr_rcvoob,
866 .pru_send = tcp_usr_send,
867 .pru_sense = pru_sense_null,
868 .pru_shutdown = tcp_usr_shutdown,
869 .pru_sockaddr = in_setsockaddr,
870 .pru_sosend = sosend,
871 .pru_soreceive = soreceive,
876 struct pr_usrreqs tcp6_usrreqs = {
877 .pru_abort = tcp_usr_abort,
878 .pru_accept = tcp6_usr_accept,
879 .pru_attach = tcp_usr_attach,
880 .pru_bind = tcp6_usr_bind,
881 .pru_connect = tcp6_usr_connect,
882 .pru_connect2 = pru_connect2_notsupp,
883 .pru_control = in6_control,
884 .pru_detach = tcp_usr_detach,
885 .pru_disconnect = tcp_usr_disconnect,
886 .pru_listen = tcp6_usr_listen,
887 .pru_peeraddr = in6_mapped_peeraddr,
888 .pru_rcvd = tcp_usr_rcvd,
889 .pru_rcvoob = tcp_usr_rcvoob,
890 .pru_send = tcp_usr_send,
891 .pru_sense = pru_sense_null,
892 .pru_shutdown = tcp_usr_shutdown,
893 .pru_sockaddr = in6_mapped_sockaddr,
894 .pru_sosend = sosend,
895 .pru_soreceive = soreceive,
901 tcp_connect_oncpu(struct tcpcb *tp, struct sockaddr_in *sin,
902 struct sockaddr_in *if_sin)
904 struct inpcb *inp = tp->t_inpcb, *oinp;
905 struct socket *so = inp->inp_socket;
906 struct route *ro = &inp->inp_route;
908 struct rmxp_tao *taop;
909 struct rmxp_tao tao_noncached;
911 oinp = in_pcblookup_hash(&tcbinfo[mycpu->gd_cpuid],
912 sin->sin_addr, sin->sin_port,
913 inp->inp_laddr.s_addr != INADDR_ANY ?
914 inp->inp_laddr : if_sin->sin_addr,
915 inp->inp_lport, 0, NULL);
917 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
918 otp->t_state == TCPS_TIME_WAIT &&
919 (ticks - otp->t_starttime) < tcp_msl &&
920 (otp->t_flags & TF_RCVD_CC))
925 if (inp->inp_laddr.s_addr == INADDR_ANY)
926 inp->inp_laddr = if_sin->sin_addr;
927 inp->inp_faddr = sin->sin_addr;
928 inp->inp_fport = sin->sin_port;
929 inp->inp_cpcbinfo = &tcbinfo[mycpu->gd_cpuid];
930 in_pcbinsconnhash(inp);
933 * We are now on the inpcb's owner CPU, if the cached route was
934 * freed because the rtentry's owner CPU is not the current CPU
935 * (e.g. in tcp_connect()), then we try to reallocate it here with
936 * the hope that a rtentry may be cloned from a RTF_PRCLONING
939 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
941 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
942 ro->ro_dst.sa_family = AF_INET;
943 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
944 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr =
949 tcp_create_timermsg(tp);
951 /* Compute window scaling to request. */
952 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
953 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.ssb_hiwat)
954 tp->request_r_scale++;
957 tcpstat.tcps_connattempt++;
958 tp->t_state = TCPS_SYN_SENT;
959 tcp_callout_reset(tp, tp->tt_keep, tcp_keepinit, tcp_timer_keep);
960 tp->iss = tcp_new_isn(tp);
964 * Generate a CC value for this connection and
965 * check whether CC or CCnew should be used.
967 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
968 taop = &tao_noncached;
969 bzero(taop, sizeof *taop);
972 tp->cc_send = CC_INC(tcp_ccgen);
973 if (taop->tao_ccsent != 0 &&
974 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
975 taop->tao_ccsent = tp->cc_send;
977 taop->tao_ccsent = 0;
978 tp->t_flags |= TF_SENDCCNEW;
986 struct netmsg_tcp_connect {
987 struct netmsg nm_netmsg;
989 struct sockaddr_in *nm_sin;
990 struct sockaddr_in *nm_ifsin;
994 tcp_connect_handler(netmsg_t netmsg)
996 struct netmsg_tcp_connect *msg = (void *)netmsg;
999 error = tcp_connect_oncpu(msg->nm_tp, msg->nm_sin, msg->nm_ifsin);
1000 lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, error);
1006 * Common subroutine to open a TCP connection to remote host specified
1007 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1008 * port number if needed. Call in_pcbladdr to do the routing and to choose
1009 * a local host address (interface). If there is an existing incarnation
1010 * of the same connection in TIME-WAIT state and if the remote host was
1011 * sending CC options and if the connection duration was < MSL, then
1012 * truncate the previous TIME-WAIT state and proceed.
1013 * Initialize connection parameters and enter SYN-SENT state.
1016 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1018 struct inpcb *inp = tp->t_inpcb;
1019 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1020 struct sockaddr_in *if_sin;
1026 if (inp->inp_lport == 0) {
1027 error = in_pcbbind(inp, (struct sockaddr *)NULL, td);
1033 * Cannot simply call in_pcbconnect, because there might be an
1034 * earlier incarnation of this same connection still in
1035 * TIME_WAIT state, creating an ADDRINUSE error.
1037 error = in_pcbladdr(inp, nam, &if_sin, td);
1042 port = tcp_addrport(sin->sin_addr.s_addr, sin->sin_port,
1043 inp->inp_laddr.s_addr ?
1044 inp->inp_laddr.s_addr : if_sin->sin_addr.s_addr,
1047 if (port != &curthread->td_msgport) {
1048 struct netmsg_tcp_connect msg;
1049 struct route *ro = &inp->inp_route;
1052 * in_pcbladdr() may have allocated a route entry for us
1053 * on the current CPU, but we need a route entry on the
1054 * inpcb's owner CPU, so free it here.
1056 if (ro->ro_rt != NULL)
1058 bzero(ro, sizeof(*ro));
1060 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0,
1061 tcp_connect_handler);
1064 msg.nm_ifsin = if_sin;
1065 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0);
1068 error = tcp_connect_oncpu(tp, sin, if_sin);
1075 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1077 struct inpcb *inp = tp->t_inpcb, *oinp;
1078 struct socket *so = inp->inp_socket;
1080 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1081 struct in6_addr *addr6;
1082 struct rmxp_tao *taop;
1083 struct rmxp_tao tao_noncached;
1086 if (inp->inp_lport == 0) {
1087 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
1093 * Cannot simply call in_pcbconnect, because there might be an
1094 * earlier incarnation of this same connection still in
1095 * TIME_WAIT state, creating an ADDRINUSE error.
1097 error = in6_pcbladdr(inp, nam, &addr6, td);
1100 oinp = in6_pcblookup_hash(inp->inp_cpcbinfo,
1101 &sin6->sin6_addr, sin6->sin6_port,
1102 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
1103 addr6 : &inp->in6p_laddr,
1104 inp->inp_lport, 0, NULL);
1106 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
1107 otp->t_state == TCPS_TIME_WAIT &&
1108 (ticks - otp->t_starttime) < tcp_msl &&
1109 (otp->t_flags & TF_RCVD_CC))
1110 otp = tcp_close(otp);
1112 return (EADDRINUSE);
1114 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1115 inp->in6p_laddr = *addr6;
1116 inp->in6p_faddr = sin6->sin6_addr;
1117 inp->inp_fport = sin6->sin6_port;
1118 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != 0)
1119 inp->in6p_flowinfo = sin6->sin6_flowinfo;
1120 in_pcbinsconnhash(inp);
1122 /* Compute window scaling to request. */
1123 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1124 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.ssb_hiwat)
1125 tp->request_r_scale++;
1128 tcpstat.tcps_connattempt++;
1129 tp->t_state = TCPS_SYN_SENT;
1130 tcp_callout_reset(tp, tp->tt_keep, tcp_keepinit, tcp_timer_keep);
1131 tp->iss = tcp_new_isn(tp);
1132 tcp_sendseqinit(tp);
1135 * Generate a CC value for this connection and
1136 * check whether CC or CCnew should be used.
1138 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
1139 taop = &tao_noncached;
1140 bzero(taop, sizeof *taop);
1143 tp->cc_send = CC_INC(tcp_ccgen);
1144 if (taop->tao_ccsent != 0 &&
1145 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
1146 taop->tao_ccsent = tp->cc_send;
1148 taop->tao_ccsent = 0;
1149 tp->t_flags |= TF_SENDCCNEW;
1157 * The new sockopt interface makes it possible for us to block in the
1158 * copyin/out step (if we take a page fault). Taking a page fault while
1159 * in a critical section is probably a Bad Thing. (Since sockets and pcbs
1160 * both now use TSM, there probably isn't any need for this function to
1161 * run in a critical section any more. This needs more examination.)
1164 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1166 int error, opt, optval;
1171 crit_enter(); /* XXX */
1175 return (ECONNRESET);
1177 if (sopt->sopt_level != IPPROTO_TCP) {
1179 if (INP_CHECK_SOCKAF(so, AF_INET6))
1180 error = ip6_ctloutput(so, sopt);
1183 error = ip_ctloutput(so, sopt);
1187 tp = intotcpcb(inp);
1189 switch (sopt->sopt_dir) {
1191 error = soopt_to_kbuf(sopt, &optval, sizeof optval,
1195 switch (sopt->sopt_name) {
1198 switch (sopt->sopt_name) {
1206 opt = 0; /* dead code to fool gcc */
1213 tp->t_flags &= ~opt;
1218 tp->t_flags |= TF_NOPUSH;
1220 tp->t_flags &= ~TF_NOPUSH;
1221 error = tcp_output(tp);
1226 if (optval > 0 && optval <= tp->t_maxseg)
1227 tp->t_maxseg = optval;
1233 error = ENOPROTOOPT;
1239 switch (sopt->sopt_name) {
1241 optval = tp->t_flags & TF_NODELAY;
1244 optval = tp->t_maxseg;
1247 optval = tp->t_flags & TF_NOOPT;
1250 optval = tp->t_flags & TF_NOPUSH;
1253 error = ENOPROTOOPT;
1257 soopt_from_kbuf(sopt, &optval, sizeof optval);
1265 * tcp_sendspace and tcp_recvspace are the default send and receive window
1266 * sizes, respectively. These are obsolescent (this information should
1267 * be set by the route).
1269 * Use a default that does not require tcp window scaling to be turned
1270 * on. Individual programs or the administrator can increase the default.
1272 u_long tcp_sendspace = 57344; /* largest multiple of PAGE_SIZE < 64k */
1273 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1274 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1275 u_long tcp_recvspace = 57344; /* largest multiple of PAGE_SIZE < 64k */
1276 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1277 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1280 * Attach TCP protocol to socket, allocating
1281 * internet protocol control block, tcp control block,
1282 * bufer space, and entering LISTEN state if to accept connections.
1285 tcp_attach(struct socket *so, struct pru_attach_info *ai)
1292 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1295 if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) {
1296 error = soreserve(so, tcp_sendspace, tcp_recvspace,
1301 cpu = mycpu->gd_cpuid;
1302 error = in_pcballoc(so, &tcbinfo[cpu]);
1308 inp->inp_vflag |= INP_IPV6;
1309 inp->in6p_hops = -1; /* use kernel default */
1313 inp->inp_vflag |= INP_IPV4;
1314 tp = tcp_newtcpcb(inp);
1316 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1318 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1325 so->so_state |= nofd;
1328 tp->t_state = TCPS_CLOSED;
1333 * Initiate (or continue) disconnect.
1334 * If embryonic state, just send reset (once).
1335 * If in ``let data drain'' option and linger null, just drop.
1336 * Otherwise (hard), mark socket disconnecting and drop
1337 * current input data; switch states based on user close, and
1338 * send segment to peer (with FIN).
1340 static struct tcpcb *
1341 tcp_disconnect(struct tcpcb *tp)
1343 struct socket *so = tp->t_inpcb->inp_socket;
1345 if (tp->t_state < TCPS_ESTABLISHED)
1347 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1348 tp = tcp_drop(tp, 0);
1350 soisdisconnecting(so);
1351 sbflush(&so->so_rcv.sb);
1352 tp = tcp_usrclosed(tp);
1360 * User issued close, and wish to trail through shutdown states:
1361 * if never received SYN, just forget it. If got a SYN from peer,
1362 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1363 * If already got a FIN from peer, then almost done; go to LAST_ACK
1364 * state. In all other cases, have already sent FIN to peer (e.g.
1365 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1366 * for peer to send FIN or not respond to keep-alives, etc.
1367 * We can let the user exit from the close as soon as the FIN is acked.
1369 static struct tcpcb *
1370 tcp_usrclosed(struct tcpcb *tp)
1373 switch (tp->t_state) {
1377 tp->t_state = TCPS_CLOSED;
1382 case TCPS_SYN_RECEIVED:
1383 tp->t_flags |= TF_NEEDFIN;
1386 case TCPS_ESTABLISHED:
1387 tp->t_state = TCPS_FIN_WAIT_1;
1390 case TCPS_CLOSE_WAIT:
1391 tp->t_state = TCPS_LAST_ACK;
1394 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1395 soisdisconnected(tp->t_inpcb->inp_socket);
1396 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1397 if (tp->t_state == TCPS_FIN_WAIT_2) {
1398 tcp_callout_reset(tp, tp->tt_2msl, tcp_maxidle,