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.40 2007/03/04 18:51:59 swildner 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 <netinet/in.h>
99 #include <netinet/in_systm.h>
101 #include <netinet/ip6.h>
103 #include <netinet/in_pcb.h>
105 #include <netinet6/in6_pcb.h>
107 #include <netinet/in_var.h>
108 #include <netinet/ip_var.h>
110 #include <netinet6/ip6_var.h>
112 #include <netinet/tcp.h>
113 #include <netinet/tcp_fsm.h>
114 #include <netinet/tcp_seq.h>
115 #include <netinet/tcp_timer.h>
116 #include <netinet/tcp_var.h>
117 #include <netinet/tcpip.h>
119 #include <netinet/tcp_debug.h>
123 #include <netinet6/ipsec.h>
127 * TCP protocol interface to socket abstraction.
129 extern char *tcpstates[]; /* XXX ??? */
131 static int tcp_attach (struct socket *, struct pru_attach_info *);
132 static int tcp_connect (struct tcpcb *, struct sockaddr *,
135 static int tcp6_connect (struct tcpcb *, struct sockaddr *,
138 static struct tcpcb *
139 tcp_disconnect (struct tcpcb *);
140 static struct tcpcb *
141 tcp_usrclosed (struct tcpcb *);
144 #define TCPDEBUG0 int ostate = 0
145 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
146 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
147 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
151 #define TCPDEBUG2(req)
155 * TCP attaches to socket via pru_attach(), reserving space,
156 * and an internet control block.
159 tcp_usr_attach(struct socket *so, int proto, struct pru_attach_info *ai)
163 struct tcpcb *tp = 0;
174 error = tcp_attach(so, ai);
178 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
179 so->so_linger = TCP_LINGERTIME;
182 TCPDEBUG2(PRU_ATTACH);
188 * pru_detach() detaches the TCP protocol from the socket.
189 * If the protocol state is non-embryonic, then can't
190 * do this directly: have to initiate a pru_disconnect(),
191 * which may finish later; embryonic TCB's can just
195 tcp_usr_detach(struct socket *so)
206 return EINVAL; /* XXX */
210 * It's possible for the tcpcb (tp) to disconnect from the inp due
211 * to tcp_drop()->tcp_close() being called. This may occur *after*
212 * the detach message has been queued so we may find a NULL tp here.
214 if ((tp = intotcpcb(inp)) != NULL) {
216 tp = tcp_disconnect(tp);
217 TCPDEBUG2(PRU_DETACH);
223 #define COMMON_START(so, inp) \
233 tp = intotcpcb(inp); \
237 #define COMMON_END(req) out: TCPDEBUG2(req); crit_exit(); return error; goto out
241 * Give the socket an address.
244 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
249 struct sockaddr_in *sinp;
251 COMMON_START(so, inp);
254 * Must check for multicast addresses and disallow binding
257 sinp = (struct sockaddr_in *)nam;
258 if (sinp->sin_family == AF_INET &&
259 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
260 error = EAFNOSUPPORT;
263 error = in_pcbbind(inp, nam, td);
266 COMMON_END(PRU_BIND);
272 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
277 struct sockaddr_in6 *sin6p;
279 COMMON_START(so, inp);
282 * Must check for multicast addresses and disallow binding
285 sin6p = (struct sockaddr_in6 *)nam;
286 if (sin6p->sin6_family == AF_INET6 &&
287 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
288 error = EAFNOSUPPORT;
291 inp->inp_vflag &= ~INP_IPV4;
292 inp->inp_vflag |= INP_IPV6;
293 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
294 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
295 inp->inp_vflag |= INP_IPV4;
296 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
297 struct sockaddr_in sin;
299 in6_sin6_2_sin(&sin, sin6p);
300 inp->inp_vflag |= INP_IPV4;
301 inp->inp_vflag &= ~INP_IPV6;
302 error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
306 error = in6_pcbbind(inp, nam, td);
309 COMMON_END(PRU_BIND);
314 struct netmsg_inswildcard {
315 struct lwkt_msg nm_lmsg;
316 struct inpcb *nm_inp;
317 struct inpcbinfo *nm_pcbinfo;
321 in_pcbinswildcardhash_handler(struct lwkt_msg *msg0)
323 struct netmsg_inswildcard *msg = (struct netmsg_inswildcard *)msg0;
325 in_pcbinswildcardhash_oncpu(msg->nm_inp, msg->nm_pcbinfo);
326 lwkt_replymsg(&msg->nm_lmsg, 0);
332 * Prepare to accept connections.
335 tcp_usr_listen(struct socket *so, struct thread *td)
344 COMMON_START(so, inp);
345 if (inp->inp_lport == 0) {
346 error = in_pcbbind(inp, NULL, td);
351 tp->t_state = TCPS_LISTEN;
354 * We have to set the flag because we can't have other cpus
355 * messing with our inp's flags.
357 inp->inp_flags |= INP_WILDCARD_MP;
358 for (cpu = 0; cpu < ncpus2; cpu++) {
359 struct netmsg_inswildcard *msg;
361 if (cpu == mycpu->gd_cpuid) {
362 in_pcbinswildcardhash(inp);
366 msg = kmalloc(sizeof(struct netmsg_inswildcard), M_LWKTMSG,
368 lwkt_initmsg(&msg->nm_lmsg, &netisr_afree_rport, 0,
369 lwkt_cmd_func(in_pcbinswildcardhash_handler),
372 msg->nm_pcbinfo = &tcbinfo[cpu];
373 lwkt_sendmsg(tcp_cport(cpu), &msg->nm_lmsg);
376 in_pcbinswildcardhash(inp);
378 COMMON_END(PRU_LISTEN);
383 tcp6_usr_listen(struct socket *so, struct thread *td)
392 COMMON_START(so, inp);
393 if (inp->inp_lport == 0) {
394 if (!(inp->inp_flags & IN6P_IPV6_V6ONLY))
395 inp->inp_vflag |= INP_IPV4;
397 inp->inp_vflag &= ~INP_IPV4;
398 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
401 tp->t_state = TCPS_LISTEN;
404 * We have to set the flag because we can't have other cpus
405 * messing with our inp's flags.
407 inp->inp_flags |= INP_WILDCARD_MP;
408 for (cpu = 0; cpu < ncpus2; cpu++) {
409 struct netmsg_inswildcard *msg;
411 if (cpu == mycpu->gd_cpuid) {
412 in_pcbinswildcardhash(inp);
416 msg = kmalloc(sizeof(struct netmsg_inswildcard), M_LWKTMSG,
418 lwkt_initmsg(&msg->nm_lmsg, &netisr_afree_rport, 0,
419 lwkt_cmd_func(in_pcbinswildcardhash_handler),
422 msg->nm_pcbinfo = &tcbinfo[cpu];
423 lwkt_sendmsg(tcp_cport(cpu), &msg->nm_lmsg);
426 in_pcbinswildcardhash(inp);
428 COMMON_END(PRU_LISTEN);
433 * Initiate connection to peer.
434 * Create a template for use in transmissions on this connection.
435 * Enter SYN_SENT state, and mark socket as connecting.
436 * Start keep-alive timer, and seed output sequence space.
437 * Send initial segment on connection.
440 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
445 struct sockaddr_in *sinp;
447 COMMON_START(so, inp);
450 * Must disallow TCP ``connections'' to multicast addresses.
452 sinp = (struct sockaddr_in *)nam;
453 if (sinp->sin_family == AF_INET
454 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
455 error = EAFNOSUPPORT;
459 if (!prison_remote_ip(td, (struct sockaddr*)sinp)) {
460 error = EAFNOSUPPORT; /* IPv6 only jail */
464 if ((error = tcp_connect(tp, nam, td)) != 0)
466 error = tcp_output(tp);
467 COMMON_END(PRU_CONNECT);
472 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
477 struct sockaddr_in6 *sin6p;
479 COMMON_START(so, inp);
482 * Must disallow TCP ``connections'' to multicast addresses.
484 sin6p = (struct sockaddr_in6 *)nam;
485 if (sin6p->sin6_family == AF_INET6
486 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
487 error = EAFNOSUPPORT;
491 if (!prison_remote_ip(td, nam)) {
492 error = EAFNOSUPPORT; /* IPv4 only jail */
496 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
497 struct sockaddr_in sin;
499 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
504 in6_sin6_2_sin(&sin, sin6p);
505 inp->inp_vflag |= INP_IPV4;
506 inp->inp_vflag &= ~INP_IPV6;
507 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
509 error = tcp_output(tp);
512 inp->inp_vflag &= ~INP_IPV4;
513 inp->inp_vflag |= INP_IPV6;
514 inp->inp_inc.inc_isipv6 = 1;
515 if ((error = tcp6_connect(tp, nam, td)) != 0)
517 error = tcp_output(tp);
518 COMMON_END(PRU_CONNECT);
523 * Initiate disconnect from peer.
524 * If connection never passed embryonic stage, just drop;
525 * else if don't need to let data drain, then can just drop anyways,
526 * else have to begin TCP shutdown process: mark socket disconnecting,
527 * drain unread data, state switch to reflect user close, and
528 * send segment (e.g. FIN) to peer. Socket will be really disconnected
529 * when peer sends FIN and acks ours.
531 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
534 tcp_usr_disconnect(struct socket *so)
540 COMMON_START(so, inp);
541 tp = tcp_disconnect(tp);
542 COMMON_END(PRU_DISCONNECT);
546 * Accept a connection. Essentially all the work is
547 * done at higher levels; just return the address
548 * of the peer, storing through addr.
551 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
555 struct tcpcb *tp = NULL;
560 if (so->so_state & SS_ISDISCONNECTED) {
561 error = ECONNABORTED;
570 in_setpeeraddr(so, nam);
571 COMMON_END(PRU_ACCEPT);
576 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
580 struct tcpcb *tp = NULL;
586 if (so->so_state & SS_ISDISCONNECTED) {
587 error = ECONNABORTED;
596 in6_mapped_peeraddr(so, nam);
597 COMMON_END(PRU_ACCEPT);
601 * Mark the connection as being incapable of further output.
604 tcp_usr_shutdown(struct socket *so)
610 COMMON_START(so, inp);
612 tp = tcp_usrclosed(tp);
614 error = tcp_output(tp);
615 COMMON_END(PRU_SHUTDOWN);
619 * After a receive, possibly send window update to peer.
622 tcp_usr_rcvd(struct socket *so, int flags)
628 COMMON_START(so, inp);
630 COMMON_END(PRU_RCVD);
634 * Do a send by putting data in output queue and updating urgent
635 * marker if URG set. Possibly send more data. Unlike the other
636 * pru_*() routines, the mbuf chains are our responsibility. We
637 * must either enqueue them or free them. The other pru_* routines
638 * generally are caller-frees.
641 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
642 struct sockaddr *nam, struct mbuf *control, struct thread *td)
657 * OOPS! we lost a race, the TCP session got reset after
658 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
659 * network interrupt in the non-critical section of sosend().
665 error = ECONNRESET; /* XXX EPIPE? */
671 isipv6 = nam && nam->sa_family == AF_INET6;
676 /* TCP doesn't do control messages (rights, creds, etc) */
677 if (control->m_len) {
684 m_freem(control); /* empty control, just free it */
686 if(!(flags & PRUS_OOB)) {
687 sbappendstream(&so->so_snd, m);
688 if (nam && tp->t_state < TCPS_SYN_SENT) {
690 * Do implied connect if not yet connected,
691 * initialize window to default value, and
692 * initialize maxseg/maxopd using peer's cached
697 error = tcp6_connect(tp, nam, td);
700 error = tcp_connect(tp, nam, td);
703 tp->snd_wnd = TTCP_CLIENT_SND_WND;
707 if (flags & PRUS_EOF) {
709 * Close the send side of the connection after
713 tp = tcp_usrclosed(tp);
716 if (flags & PRUS_MORETOCOME)
717 tp->t_flags |= TF_MORETOCOME;
718 error = tcp_output(tp);
719 if (flags & PRUS_MORETOCOME)
720 tp->t_flags &= ~TF_MORETOCOME;
723 if (sbspace(&so->so_snd) < -512) {
729 * According to RFC961 (Assigned Protocols),
730 * the urgent pointer points to the last octet
731 * of urgent data. We continue, however,
732 * to consider it to indicate the first octet
733 * of data past the urgent section.
734 * Otherwise, snd_up should be one lower.
736 sbappendstream(&so->so_snd, m);
737 if (nam && tp->t_state < TCPS_SYN_SENT) {
739 * Do implied connect if not yet connected,
740 * initialize window to default value, and
741 * initialize maxseg/maxopd using peer's cached
746 error = tcp6_connect(tp, nam, td);
749 error = tcp_connect(tp, nam, td);
752 tp->snd_wnd = TTCP_CLIENT_SND_WND;
755 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
756 tp->t_flags |= TF_FORCE;
757 error = tcp_output(tp);
758 tp->t_flags &= ~TF_FORCE;
760 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
761 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
768 tcp_usr_abort(struct socket *so)
774 COMMON_START(so, inp);
775 tp = tcp_drop(tp, ECONNABORTED);
776 COMMON_END(PRU_ABORT);
780 * Receive out-of-band data.
783 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
789 COMMON_START(so, inp);
790 if ((so->so_oobmark == 0 &&
791 (so->so_state & SS_RCVATMARK) == 0) ||
792 so->so_options & SO_OOBINLINE ||
793 tp->t_oobflags & TCPOOB_HADDATA) {
797 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
802 *mtod(m, caddr_t) = tp->t_iobc;
803 if ((flags & MSG_PEEK) == 0)
804 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
805 COMMON_END(PRU_RCVOOB);
808 /* xxx - should be const */
809 struct pr_usrreqs tcp_usrreqs = {
810 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
811 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
812 tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
813 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
814 in_setsockaddr, sosend, soreceive, sopoll
818 struct pr_usrreqs tcp6_usrreqs = {
819 tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
820 tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
821 tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
822 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
823 in6_mapped_sockaddr, sosend, soreceive, sopoll
828 tcp_connect_oncpu(struct tcpcb *tp, struct sockaddr_in *sin,
829 struct sockaddr_in *if_sin)
831 struct inpcb *inp = tp->t_inpcb, *oinp;
832 struct socket *so = inp->inp_socket;
834 struct rmxp_tao *taop;
835 struct rmxp_tao tao_noncached;
837 oinp = in_pcblookup_hash(&tcbinfo[mycpu->gd_cpuid],
838 sin->sin_addr, sin->sin_port,
839 inp->inp_laddr.s_addr != INADDR_ANY ?
840 inp->inp_laddr : if_sin->sin_addr,
841 inp->inp_lport, 0, NULL);
843 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
844 otp->t_state == TCPS_TIME_WAIT &&
845 (ticks - otp->t_starttime) < tcp_msl &&
846 (otp->t_flags & TF_RCVD_CC))
851 if (inp->inp_laddr.s_addr == INADDR_ANY)
852 inp->inp_laddr = if_sin->sin_addr;
853 inp->inp_faddr = sin->sin_addr;
854 inp->inp_fport = sin->sin_port;
855 inp->inp_cpcbinfo = &tcbinfo[mycpu->gd_cpuid];
856 in_pcbinsconnhash(inp);
858 /* Compute window scaling to request. */
859 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
860 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
861 tp->request_r_scale++;
864 tcpstat.tcps_connattempt++;
865 tp->t_state = TCPS_SYN_SENT;
866 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
867 tp->iss = tcp_new_isn(tp);
871 * Generate a CC value for this connection and
872 * check whether CC or CCnew should be used.
874 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
875 taop = &tao_noncached;
876 bzero(taop, sizeof *taop);
879 tp->cc_send = CC_INC(tcp_ccgen);
880 if (taop->tao_ccsent != 0 &&
881 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
882 taop->tao_ccsent = tp->cc_send;
884 taop->tao_ccsent = 0;
885 tp->t_flags |= TF_SENDCCNEW;
893 struct netmsg_tcp_connect {
894 struct lwkt_msg nm_lmsg;
896 struct sockaddr_in *nm_sin;
897 struct sockaddr_in *nm_ifsin;
901 tcp_connect_handler(lwkt_msg_t lmsg)
903 struct netmsg_tcp_connect *msg = (void *)lmsg;
906 error = tcp_connect_oncpu(msg->nm_tp, msg->nm_sin, msg->nm_ifsin);
907 lwkt_replymsg(lmsg, error);
914 * Common subroutine to open a TCP connection to remote host specified
915 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
916 * port number if needed. Call in_pcbladdr to do the routing and to choose
917 * a local host address (interface). If there is an existing incarnation
918 * of the same connection in TIME-WAIT state and if the remote host was
919 * sending CC options and if the connection duration was < MSL, then
920 * truncate the previous TIME-WAIT state and proceed.
921 * Initialize connection parameters and enter SYN-SENT state.
924 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
926 struct inpcb *inp = tp->t_inpcb;
927 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
928 struct sockaddr_in *if_sin;
934 if (inp->inp_lport == 0) {
935 error = in_pcbbind(inp, (struct sockaddr *)NULL, td);
941 * Cannot simply call in_pcbconnect, because there might be an
942 * earlier incarnation of this same connection still in
943 * TIME_WAIT state, creating an ADDRINUSE error.
945 error = in_pcbladdr(inp, nam, &if_sin, td);
950 port = tcp_addrport(sin->sin_addr.s_addr, sin->sin_port,
951 inp->inp_laddr.s_addr ?
952 inp->inp_laddr.s_addr : if_sin->sin_addr.s_addr,
955 if (port->mp_td != curthread) {
956 struct netmsg_tcp_connect msg;
958 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
959 lwkt_cmd_func(tcp_connect_handler), lwkt_cmd_op_none);
962 msg.nm_ifsin = if_sin;
963 error = lwkt_domsg(port, &msg.nm_lmsg);
966 error = tcp_connect_oncpu(tp, sin, if_sin);
973 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
975 struct inpcb *inp = tp->t_inpcb, *oinp;
976 struct socket *so = inp->inp_socket;
978 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
979 struct in6_addr *addr6;
980 struct rmxp_tao *taop;
981 struct rmxp_tao tao_noncached;
984 if (inp->inp_lport == 0) {
985 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
991 * Cannot simply call in_pcbconnect, because there might be an
992 * earlier incarnation of this same connection still in
993 * TIME_WAIT state, creating an ADDRINUSE error.
995 error = in6_pcbladdr(inp, nam, &addr6, td);
998 oinp = in6_pcblookup_hash(inp->inp_cpcbinfo,
999 &sin6->sin6_addr, sin6->sin6_port,
1000 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
1001 addr6 : &inp->in6p_laddr,
1002 inp->inp_lport, 0, NULL);
1004 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
1005 otp->t_state == TCPS_TIME_WAIT &&
1006 (ticks - otp->t_starttime) < tcp_msl &&
1007 (otp->t_flags & TF_RCVD_CC))
1008 otp = tcp_close(otp);
1010 return (EADDRINUSE);
1012 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1013 inp->in6p_laddr = *addr6;
1014 inp->in6p_faddr = sin6->sin6_addr;
1015 inp->inp_fport = sin6->sin6_port;
1016 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
1017 inp->in6p_flowinfo = sin6->sin6_flowinfo;
1018 in_pcbinsconnhash(inp);
1020 /* Compute window scaling to request. */
1021 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1022 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1023 tp->request_r_scale++;
1026 tcpstat.tcps_connattempt++;
1027 tp->t_state = TCPS_SYN_SENT;
1028 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1029 tp->iss = tcp_new_isn(tp);
1030 tcp_sendseqinit(tp);
1033 * Generate a CC value for this connection and
1034 * check whether CC or CCnew should be used.
1036 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
1037 taop = &tao_noncached;
1038 bzero(taop, sizeof *taop);
1041 tp->cc_send = CC_INC(tcp_ccgen);
1042 if (taop->tao_ccsent != 0 &&
1043 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
1044 taop->tao_ccsent = tp->cc_send;
1046 taop->tao_ccsent = 0;
1047 tp->t_flags |= TF_SENDCCNEW;
1055 * The new sockopt interface makes it possible for us to block in the
1056 * copyin/out step (if we take a page fault). Taking a page fault while
1057 * in a critical section is probably a Bad Thing. (Since sockets and pcbs
1058 * both now use TSM, there probably isn't any need for this function to
1059 * run in a critical section any more. This needs more examination.)
1062 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1064 int error, opt, optval;
1069 crit_enter(); /* XXX */
1073 return (ECONNRESET);
1075 if (sopt->sopt_level != IPPROTO_TCP) {
1077 if (INP_CHECK_SOCKAF(so, AF_INET6))
1078 error = ip6_ctloutput(so, sopt);
1081 error = ip_ctloutput(so, sopt);
1085 tp = intotcpcb(inp);
1087 switch (sopt->sopt_dir) {
1089 switch (sopt->sopt_name) {
1092 error = sooptcopyin(sopt, &optval, sizeof optval,
1097 switch (sopt->sopt_name) {
1105 opt = 0; /* dead code to fool gcc */
1112 tp->t_flags &= ~opt;
1116 error = sooptcopyin(sopt, &optval, sizeof optval,
1122 tp->t_flags |= TF_NOPUSH;
1124 tp->t_flags &= ~TF_NOPUSH;
1125 error = tcp_output(tp);
1130 error = sooptcopyin(sopt, &optval, sizeof optval,
1135 if (optval > 0 && optval <= tp->t_maxseg)
1136 tp->t_maxseg = optval;
1142 error = ENOPROTOOPT;
1148 switch (sopt->sopt_name) {
1150 optval = tp->t_flags & TF_NODELAY;
1153 optval = tp->t_maxseg;
1156 optval = tp->t_flags & TF_NOOPT;
1159 optval = tp->t_flags & TF_NOPUSH;
1162 error = ENOPROTOOPT;
1166 error = sooptcopyout(sopt, &optval, sizeof optval);
1174 * tcp_sendspace and tcp_recvspace are the default send and receive window
1175 * sizes, respectively. These are obsolescent (this information should
1176 * be set by the route).
1178 u_long tcp_sendspace = 1024*32;
1179 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1180 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1181 u_long tcp_recvspace = 57344; /* largest multiple of PAGE_SIZE < 64k */
1182 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1183 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1186 * Attach TCP protocol to socket, allocating
1187 * internet protocol control block, tcp control block,
1188 * bufer space, and entering LISTEN state if to accept connections.
1191 tcp_attach(struct socket *so, struct pru_attach_info *ai)
1198 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1201 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1202 error = soreserve(so, tcp_sendspace, tcp_recvspace,
1207 cpu = mycpu->gd_cpuid;
1208 error = in_pcballoc(so, &tcbinfo[cpu]);
1214 inp->inp_vflag |= INP_IPV6;
1215 inp->in6p_hops = -1; /* use kernel default */
1219 inp->inp_vflag |= INP_IPV4;
1220 tp = tcp_newtcpcb(inp);
1222 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1224 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1231 so->so_state |= nofd;
1234 tp->t_state = TCPS_CLOSED;
1239 * Initiate (or continue) disconnect.
1240 * If embryonic state, just send reset (once).
1241 * If in ``let data drain'' option and linger null, just drop.
1242 * Otherwise (hard), mark socket disconnecting and drop
1243 * current input data; switch states based on user close, and
1244 * send segment to peer (with FIN).
1246 static struct tcpcb *
1247 tcp_disconnect(struct tcpcb *tp)
1249 struct socket *so = tp->t_inpcb->inp_socket;
1251 if (tp->t_state < TCPS_ESTABLISHED)
1253 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1254 tp = tcp_drop(tp, 0);
1256 soisdisconnecting(so);
1257 sbflush(&so->so_rcv);
1258 tp = tcp_usrclosed(tp);
1266 * User issued close, and wish to trail through shutdown states:
1267 * if never received SYN, just forget it. If got a SYN from peer,
1268 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1269 * If already got a FIN from peer, then almost done; go to LAST_ACK
1270 * state. In all other cases, have already sent FIN to peer (e.g.
1271 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1272 * for peer to send FIN or not respond to keep-alives, etc.
1273 * We can let the user exit from the close as soon as the FIN is acked.
1275 static struct tcpcb *
1276 tcp_usrclosed(struct tcpcb *tp)
1279 switch (tp->t_state) {
1283 tp->t_state = TCPS_CLOSED;
1288 case TCPS_SYN_RECEIVED:
1289 tp->t_flags |= TF_NEEDFIN;
1292 case TCPS_ESTABLISHED:
1293 tp->t_state = TCPS_FIN_WAIT_1;
1296 case TCPS_CLOSE_WAIT:
1297 tp->t_state = TCPS_LAST_ACK;
1300 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1301 soisdisconnected(tp->t_inpcb->inp_socket);
1302 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1303 if (tp->t_state == TCPS_FIN_WAIT_2)
1304 callout_reset(tp->tt_2msl, tcp_maxidle,
1305 tcp_timer_2msl, tp);