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"
73 #include "opt_inet6.h"
74 #include "opt_tcpdebug.h"
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/malloc.h>
80 #include <sys/sysctl.h>
81 #include <sys/globaldata.h>
82 #include <sys/thread.h>
86 #include <sys/domain.h>
88 #include <sys/socket.h>
89 #include <sys/socketvar.h>
90 #include <sys/protosw.h>
92 #include <sys/thread2.h>
93 #include <sys/msgport2.h>
94 #include <sys/socketvar2.h>
97 #include <net/netisr.h>
98 #include <net/route.h>
100 #include <net/netmsg2.h>
102 #include <netinet/in.h>
103 #include <netinet/in_systm.h>
105 #include <netinet/ip6.h>
107 #include <netinet/in_pcb.h>
109 #include <netinet6/in6_pcb.h>
111 #include <netinet/in_var.h>
112 #include <netinet/ip_var.h>
114 #include <netinet6/ip6_var.h>
115 #include <netinet6/tcp6_var.h>
117 #include <netinet/tcp.h>
118 #include <netinet/tcp_fsm.h>
119 #include <netinet/tcp_seq.h>
120 #include <netinet/tcp_timer.h>
121 #include <netinet/tcp_timer2.h>
122 #include <netinet/tcp_var.h>
123 #include <netinet/tcpip.h>
125 #include <netinet/tcp_debug.h>
129 #include <netinet6/ipsec.h>
133 * TCP protocol interface to socket abstraction.
135 extern char *tcpstates[]; /* XXX ??? */
137 static int tcp_attach (struct socket *, struct pru_attach_info *);
138 static int tcp_connect (struct tcpcb *, int flags, struct mbuf *m,
139 struct sockaddr *, struct thread *);
141 static int tcp6_connect (struct tcpcb *, int flags, struct mbuf *m,
142 struct sockaddr *, struct thread *);
143 static int tcp6_connect_oncpu(struct tcpcb *tp, int flags, struct mbuf *m,
144 struct sockaddr_in6 *sin6,
145 struct in6_addr *addr6);
147 static struct tcpcb *
148 tcp_disconnect (struct tcpcb *);
149 static struct tcpcb *
150 tcp_usrclosed (struct tcpcb *);
153 #define TCPDEBUG0 int ostate = 0
154 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
155 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
156 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
160 #define TCPDEBUG2(req)
164 * TCP attaches to socket via pru_attach(), reserving space,
165 * and an internet control block. This is likely occuring on
166 * cpu0 and may have to move later when we bind/connect.
169 tcp_usr_attach(struct socket *so, int proto, struct pru_attach_info *ai)
173 struct tcpcb *tp = 0;
184 error = tcp_attach(so, ai);
188 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
189 so->so_linger = TCP_LINGERTIME;
192 sofree(so); /* from ref above */
193 TCPDEBUG2(PRU_ATTACH);
198 * pru_detach() detaches the TCP protocol from the socket.
199 * If the protocol state is non-embryonic, then can't
200 * do this directly: have to initiate a pru_disconnect(),
201 * which may finish later; embryonic TCB's can just
205 tcp_usr_detach(struct socket *so)
215 * If the inp is already detached it may have been due to an async
216 * close. Just return as if no error occured.
222 * It's possible for the tcpcb (tp) to disconnect from the inp due
223 * to tcp_drop()->tcp_close() being called. This may occur *after*
224 * the detach message has been queued so we may find a NULL tp here.
226 if ((tp = intotcpcb(inp)) != NULL) {
228 tp = tcp_disconnect(tp);
229 TCPDEBUG2(PRU_DETACH);
235 * Note: ignore_error is non-zero for certain disconnection races
236 * which we want to silently allow, otherwise close() may return
237 * an unexpected error.
239 #define COMMON_START(so, inp, ignore_error) \
245 return (ignore_error ? 0 : EINVAL); \
247 tp = intotcpcb(inp); \
251 #define COMMON_END(req) out: TCPDEBUG2(req); 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, NULL, &netisr_afree_rport,
382 0, in_pcbinswildcardhash_handler);
384 msg->nm_pcbinfo = &tcbinfo[cpu];
385 lwkt_sendmsg(cpu_portfn(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, NULL, 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, NULL, &netisr_afree_rport,
431 0, in_pcbinswildcardhash_handler);
433 msg->nm_pcbinfo = &tcbinfo[cpu];
434 lwkt_sendmsg(cpu_portfn(cpu), &msg->nm_netmsg.nm_lmsg);
437 in_pcbinswildcardhash(inp);
439 COMMON_END(PRU_LISTEN);
444 * Initiate connection to peer.
445 * Create a template for use in transmissions on this connection.
446 * Enter SYN_SENT state, and mark socket as connecting.
447 * Start keep-alive timer, and seed output sequence space.
448 * Send initial segment on connection.
451 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
456 struct sockaddr_in *sinp;
458 COMMON_START(so, inp, 0);
461 * Must disallow TCP ``connections'' to multicast addresses.
463 sinp = (struct sockaddr_in *)nam;
464 if (sinp->sin_family == AF_INET
465 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
466 error = EAFNOSUPPORT;
470 if (!prison_remote_ip(td, (struct sockaddr*)sinp)) {
471 error = EAFNOSUPPORT; /* IPv6 only jail */
475 if ((error = tcp_connect(tp, 0, NULL, nam, td)) != 0)
477 COMMON_END(PRU_CONNECT);
482 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
487 struct sockaddr_in6 *sin6p;
489 COMMON_START(so, inp, 0);
492 * Must disallow TCP ``connections'' to multicast addresses.
494 sin6p = (struct sockaddr_in6 *)nam;
495 if (sin6p->sin6_family == AF_INET6
496 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
497 error = EAFNOSUPPORT;
501 if (!prison_remote_ip(td, nam)) {
502 error = EAFNOSUPPORT; /* IPv4 only jail */
506 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
507 struct sockaddr_in sin;
509 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
514 in6_sin6_2_sin(&sin, sin6p);
515 inp->inp_vflag |= INP_IPV4;
516 inp->inp_vflag &= ~INP_IPV6;
517 error = tcp_connect(tp, 0, NULL, (struct sockaddr *)&sin, td);
522 inp->inp_vflag &= ~INP_IPV4;
523 inp->inp_vflag |= INP_IPV6;
524 inp->inp_inc.inc_isipv6 = 1;
525 if ((error = tcp6_connect(tp, 0, NULL, nam, td)) != 0)
527 error = tcp_output(tp);
528 COMMON_END(PRU_CONNECT);
533 * Initiate disconnect from peer.
534 * If connection never passed embryonic stage, just drop;
535 * else if don't need to let data drain, then can just drop anyways,
536 * else have to begin TCP shutdown process: mark socket disconnecting,
537 * drain unread data, state switch to reflect user close, and
538 * send segment (e.g. FIN) to peer. Socket will be really disconnected
539 * when peer sends FIN and acks ours.
541 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
544 tcp_usr_disconnect(struct socket *so)
550 COMMON_START(so, inp, 1);
551 tp = tcp_disconnect(tp);
552 COMMON_END(PRU_DISCONNECT);
556 * Accept a connection. Essentially all the work is
557 * done at higher levels; just return the address
558 * of the peer, storing through addr.
561 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
565 struct tcpcb *tp = NULL;
569 if (so->so_state & SS_ISDISCONNECTED) {
570 error = ECONNABORTED;
578 in_setpeeraddr(so, nam);
579 COMMON_END(PRU_ACCEPT);
584 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
588 struct tcpcb *tp = NULL;
593 if (so->so_state & SS_ISDISCONNECTED) {
594 error = ECONNABORTED;
601 in6_mapped_peeraddr(so, nam);
602 COMMON_END(PRU_ACCEPT);
606 * Mark the connection as being incapable of further output.
609 tcp_usr_shutdown(struct socket *so)
615 COMMON_START(so, inp, 0);
617 tp = tcp_usrclosed(tp);
619 error = tcp_output(tp);
620 COMMON_END(PRU_SHUTDOWN);
624 * After a receive, possibly send window update to peer.
627 tcp_usr_rcvd(struct socket *so, int flags)
633 COMMON_START(so, inp, 0);
635 COMMON_END(PRU_RCVD);
639 * Do a send by putting data in output queue and updating urgent
640 * marker if URG set. Possibly send more data. Unlike the other
641 * pru_*() routines, the mbuf chains are our responsibility. We
642 * must either enqueue them or free them. The other pru_* routines
643 * generally are caller-frees.
646 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
647 struct sockaddr *nam, struct mbuf *control, struct thread *td)
661 * OOPS! we lost a race, the TCP session got reset after
662 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
663 * network interrupt in the non-critical section of sosend().
668 error = ECONNRESET; /* XXX EPIPE? */
674 isipv6 = nam && nam->sa_family == AF_INET6;
679 /* TCP doesn't do control messages (rights, creds, etc) */
680 if (control->m_len) {
686 m_freem(control); /* empty control, just free it */
690 * Don't let too much OOB data build up
692 if (flags & PRUS_OOB) {
693 if (ssb_space(&so->so_snd) < -512) {
701 * Do implied connect if not yet connected. Any data sent
702 * with the connect is handled by tcp_connect() and friends.
704 * NOTE! PROTOCOL THREAD MAY BE CHANGED BY THE CONNECT!
706 if (nam && tp->t_state < TCPS_SYN_SENT) {
709 error = tcp6_connect(tp, flags, m, nam, td);
712 error = tcp_connect(tp, flags, m, nam, td);
714 /* WTF is this doing here? */
715 tp->snd_wnd = TTCP_CLIENT_SND_WND;
722 * Pump the data into the socket.
725 ssb_appendstream(&so->so_snd, m);
726 if (flags & PRUS_OOB) {
728 * According to RFC961 (Assigned Protocols),
729 * the urgent pointer points to the last octet
730 * of urgent data. We continue, however,
731 * to consider it to indicate the first octet
732 * of data past the urgent section.
733 * Otherwise, snd_up should be one lower.
735 tp->snd_up = tp->snd_una + so->so_snd.ssb_cc;
736 tp->t_flags |= TF_FORCE;
737 error = tcp_output(tp);
738 tp->t_flags &= ~TF_FORCE;
740 if (flags & PRUS_EOF) {
742 * Close the send side of the connection after
746 tp = tcp_usrclosed(tp);
749 if (flags & PRUS_MORETOCOME)
750 tp->t_flags |= TF_MORETOCOME;
751 error = tcp_output(tp);
752 if (flags & PRUS_MORETOCOME)
753 tp->t_flags &= ~TF_MORETOCOME;
756 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
757 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
761 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
762 * will sofree() it when we return.
765 tcp_usr_abort(struct socket *so)
771 COMMON_START(so, inp, 1);
772 tp = tcp_drop(tp, ECONNABORTED);
773 COMMON_END(PRU_ABORT);
777 * Receive out-of-band data.
780 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
786 COMMON_START(so, inp, 0);
787 if ((so->so_oobmark == 0 &&
788 (so->so_state & SS_RCVATMARK) == 0) ||
789 so->so_options & SO_OOBINLINE ||
790 tp->t_oobflags & TCPOOB_HADDATA) {
794 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
799 *mtod(m, caddr_t) = tp->t_iobc;
800 if ((flags & MSG_PEEK) == 0)
801 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
802 COMMON_END(PRU_RCVOOB);
805 /* xxx - should be const */
806 struct pr_usrreqs tcp_usrreqs = {
807 .pru_abort = tcp_usr_abort,
808 .pru_accept = tcp_usr_accept,
809 .pru_attach = tcp_usr_attach,
810 .pru_bind = tcp_usr_bind,
811 .pru_connect = tcp_usr_connect,
812 .pru_connect2 = pru_connect2_notsupp,
813 .pru_control = in_control,
814 .pru_detach = tcp_usr_detach,
815 .pru_disconnect = tcp_usr_disconnect,
816 .pru_listen = tcp_usr_listen,
817 .pru_peeraddr = in_setpeeraddr,
818 .pru_rcvd = tcp_usr_rcvd,
819 .pru_rcvoob = tcp_usr_rcvoob,
820 .pru_send = tcp_usr_send,
821 .pru_sense = pru_sense_null,
822 .pru_shutdown = tcp_usr_shutdown,
823 .pru_sockaddr = in_setsockaddr,
824 .pru_sosend = sosend,
825 .pru_soreceive = soreceive
829 struct pr_usrreqs tcp6_usrreqs = {
830 .pru_abort = tcp_usr_abort,
831 .pru_accept = tcp6_usr_accept,
832 .pru_attach = tcp_usr_attach,
833 .pru_bind = tcp6_usr_bind,
834 .pru_connect = tcp6_usr_connect,
835 .pru_connect2 = pru_connect2_notsupp,
836 .pru_control = in6_control,
837 .pru_detach = tcp_usr_detach,
838 .pru_disconnect = tcp_usr_disconnect,
839 .pru_listen = tcp6_usr_listen,
840 .pru_peeraddr = in6_mapped_peeraddr,
841 .pru_rcvd = tcp_usr_rcvd,
842 .pru_rcvoob = tcp_usr_rcvoob,
843 .pru_send = tcp_usr_send,
844 .pru_sense = pru_sense_null,
845 .pru_shutdown = tcp_usr_shutdown,
846 .pru_sockaddr = in6_mapped_sockaddr,
847 .pru_sosend = sosend,
848 .pru_soreceive = soreceive
853 tcp_connect_oncpu(struct tcpcb *tp, int flags, struct mbuf *m,
854 struct sockaddr_in *sin, struct sockaddr_in *if_sin)
856 struct inpcb *inp = tp->t_inpcb, *oinp;
857 struct socket *so = inp->inp_socket;
858 struct route *ro = &inp->inp_route;
860 oinp = in_pcblookup_hash(&tcbinfo[mycpu->gd_cpuid],
861 sin->sin_addr, sin->sin_port,
862 inp->inp_laddr.s_addr != INADDR_ANY ?
863 inp->inp_laddr : if_sin->sin_addr,
864 inp->inp_lport, 0, NULL);
869 if (inp->inp_laddr.s_addr == INADDR_ANY)
870 inp->inp_laddr = if_sin->sin_addr;
871 inp->inp_faddr = sin->sin_addr;
872 inp->inp_fport = sin->sin_port;
873 inp->inp_cpcbinfo = &tcbinfo[mycpu->gd_cpuid];
874 in_pcbinsconnhash(inp);
877 * We are now on the inpcb's owner CPU, if the cached route was
878 * freed because the rtentry's owner CPU is not the current CPU
879 * (e.g. in tcp_connect()), then we try to reallocate it here with
880 * the hope that a rtentry may be cloned from a RTF_PRCLONING
883 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
885 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
886 ro->ro_dst.sa_family = AF_INET;
887 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
888 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr =
894 * Now that no more errors can occur, change the protocol processing
895 * port to the current thread (which is the correct thread).
897 * Create TCP timer message now; we are on the tcpcb's owner
900 tcp_create_timermsg(tp, &curthread->td_msgport);
903 * Compute window scaling to request. Use a larger scaling then
904 * needed for the initial receive buffer in case the receive buffer
907 if (tp->request_r_scale < TCP_MIN_WINSHIFT)
908 tp->request_r_scale = TCP_MIN_WINSHIFT;
909 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
910 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.ssb_hiwat
912 tp->request_r_scale++;
916 tcpstat.tcps_connattempt++;
917 tp->t_state = TCPS_SYN_SENT;
918 tcp_callout_reset(tp, tp->tt_keep, tcp_keepinit, tcp_timer_keep);
919 tp->iss = tcp_new_isn(tp);
922 ssb_appendstream(&so->so_snd, m);
924 if (flags & PRUS_OOB)
925 tp->snd_up = tp->snd_una + so->so_snd.ssb_cc;
929 * Close the send side of the connection after
930 * the data is sent if flagged.
932 if ((flags & (PRUS_OOB|PRUS_EOF)) == PRUS_EOF) {
934 tp = tcp_usrclosed(tp);
936 return (tcp_output(tp));
941 struct netmsg_tcp_connect {
942 struct netmsg nm_netmsg;
944 struct sockaddr_in *nm_sin;
945 struct sockaddr_in *nm_ifsin;
951 * This is called in the target protocol processing thread. We must
952 * re-link our pcb to the new tcpcb
955 tcp_connect_handler(netmsg_t netmsg)
957 struct netmsg_tcp_connect *msg = (void *)netmsg;
958 struct socket *so = netmsg->nm_so;
961 in_pcblink(so->so_pcb, &tcbinfo[mycpu->gd_cpuid]);
962 error = tcp_connect_oncpu(msg->nm_tp, msg->nm_flags, msg->nm_m,
963 msg->nm_sin, msg->nm_ifsin);
964 lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, error);
967 struct netmsg_tcp6_connect {
968 struct netmsg nm_netmsg;
970 struct sockaddr_in6 *nm_sin6;
971 struct in6_addr *nm_addr6;
978 tcp6_connect_handler(netmsg_t netmsg)
980 struct netmsg_tcp6_connect *msg = (void *)netmsg;
983 error = tcp6_connect_oncpu(msg->nm_tp, msg->nm_flags, msg->nm_m,
984 msg->nm_sin6, msg->nm_addr6);
985 lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, error);
992 * Common subroutine to open a TCP connection to remote host specified
993 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
994 * port number if needed. Call in_pcbladdr to do the routing and to choose
995 * a local host address (interface).
996 * Initialize connection parameters and enter SYN-SENT state.
999 tcp_connect(struct tcpcb *tp, int flags, struct mbuf *m,
1000 struct sockaddr *nam, struct thread *td)
1002 struct inpcb *inp = tp->t_inpcb;
1003 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1004 struct sockaddr_in *if_sin;
1012 * Bind if we have to
1014 if (inp->inp_lport == 0) {
1015 error = in_pcbbind(inp, NULL, td);
1021 so = inp->inp_socket;
1025 * Calculate the correct protocol processing thread. The connect
1026 * operation must run there. Set the forwarding port before we
1027 * forward the message or it will get bounced right back to us.
1029 error = in_pcbladdr(inp, nam, &if_sin, td);
1036 port = tcp_addrport(sin->sin_addr.s_addr, sin->sin_port,
1037 inp->inp_laddr.s_addr ?
1038 inp->inp_laddr.s_addr : if_sin->sin_addr.s_addr,
1041 if (port != &curthread->td_msgport) {
1042 struct netmsg_tcp_connect msg;
1043 struct route *ro = &inp->inp_route;
1046 * in_pcbladdr() may have allocated a route entry for us
1047 * on the current CPU, but we need a route entry on the
1048 * inpcb's owner CPU, so free it here.
1050 if (ro->ro_rt != NULL)
1052 bzero(ro, sizeof(*ro));
1055 * We are moving the protocol processing port the socket
1056 * is on, we have to unlink here and re-link on the
1059 in_pcbunlink(so->so_pcb, &tcbinfo[mycpu->gd_cpuid]);
1060 sosetport(so, port);
1062 netmsg_init(&msg.nm_netmsg, so, &curthread->td_msgport,
1063 0, tcp_connect_handler);
1066 msg.nm_ifsin = if_sin;
1067 msg.nm_flags = flags;
1069 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0);
1071 error = tcp_connect_oncpu(tp, flags, m, sin, if_sin);
1074 KKASSERT(so->so_port == &curthread->td_msgport);
1075 error = tcp_connect_oncpu(tp, flags, m, sin, if_sin);
1083 tcp6_connect(struct tcpcb *tp, int flags, struct mbuf *m,
1084 struct sockaddr *nam, struct thread *td)
1086 struct inpcb *inp = tp->t_inpcb;
1087 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1088 struct in6_addr *addr6;
1094 if (inp->inp_lport == 0) {
1095 error = in6_pcbbind(inp, NULL, td);
1103 * Cannot simply call in_pcbconnect, because there might be an
1104 * earlier incarnation of this same connection still in
1105 * TIME_WAIT state, creating an ADDRINUSE error.
1107 error = in6_pcbladdr(inp, nam, &addr6, td);
1114 port = tcp6_addrport(); /* XXX hack for now, always cpu0 */
1116 if (port != &curthread->td_msgport) {
1117 struct netmsg_tcp6_connect msg;
1118 struct route *ro = &inp->inp_route;
1121 * in_pcbladdr() may have allocated a route entry for us
1122 * on the current CPU, but we need a route entry on the
1123 * inpcb's owner CPU, so free it here.
1125 if (ro->ro_rt != NULL)
1127 bzero(ro, sizeof(*ro));
1129 netmsg_init(&msg.nm_netmsg, NULL, &curthread->td_msgport,
1130 0, tcp6_connect_handler);
1133 msg.nm_addr6 = addr6;
1134 msg.nm_flags = flags;
1136 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0);
1138 error = tcp6_connect_oncpu(tp, flags, m, sin6, addr6);
1141 error = tcp6_connect_oncpu(tp, flags, m, sin6, addr6);
1147 tcp6_connect_oncpu(struct tcpcb *tp, int flags, struct mbuf *m,
1148 struct sockaddr_in6 *sin6, struct in6_addr *addr6)
1150 struct inpcb *inp = tp->t_inpcb;
1151 struct socket *so = inp->inp_socket;
1155 * Cannot simply call in_pcbconnect, because there might be an
1156 * earlier incarnation of this same connection still in
1157 * TIME_WAIT state, creating an ADDRINUSE error.
1159 oinp = in6_pcblookup_hash(inp->inp_cpcbinfo,
1160 &sin6->sin6_addr, sin6->sin6_port,
1161 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
1162 addr6 : &inp->in6p_laddr,
1163 inp->inp_lport, 0, NULL);
1166 return (EADDRINUSE);
1168 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1169 inp->in6p_laddr = *addr6;
1170 inp->in6p_faddr = sin6->sin6_addr;
1171 inp->inp_fport = sin6->sin6_port;
1172 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != 0)
1173 inp->in6p_flowinfo = sin6->sin6_flowinfo;
1174 in_pcbinsconnhash(inp);
1177 * Now that no more errors can occur, change the protocol processing
1178 * port to the current thread (which is the correct thread).
1180 * Create TCP timer message now; we are on the tcpcb's owner
1183 tcp_create_timermsg(tp, &curthread->td_msgport);
1185 /* Compute window scaling to request. */
1186 if (tp->request_r_scale < TCP_MIN_WINSHIFT)
1187 tp->request_r_scale = TCP_MIN_WINSHIFT;
1188 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1189 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.ssb_hiwat) {
1190 tp->request_r_scale++;
1194 tcpstat.tcps_connattempt++;
1195 tp->t_state = TCPS_SYN_SENT;
1196 tcp_callout_reset(tp, tp->tt_keep, tcp_keepinit, tcp_timer_keep);
1197 tp->iss = tcp_new_isn(tp);
1198 tcp_sendseqinit(tp);
1200 ssb_appendstream(&so->so_snd, m);
1202 if (flags & PRUS_OOB)
1203 tp->snd_up = tp->snd_una + so->so_snd.ssb_cc;
1207 * Close the send side of the connection after
1208 * the data is sent if flagged.
1210 if ((flags & (PRUS_OOB|PRUS_EOF)) == PRUS_EOF) {
1212 tp = tcp_usrclosed(tp);
1214 return (tcp_output(tp));
1220 * The new sockopt interface makes it possible for us to block in the
1221 * copyin/out step (if we take a page fault). Taking a page fault while
1222 * in a critical section is probably a Bad Thing. (Since sockets and pcbs
1223 * both now use TSM, there probably isn't any need for this function to
1224 * run in a critical section any more. This needs more examination.)
1227 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1229 int error, opt, optval;
1236 return (ECONNRESET);
1238 if (sopt->sopt_level != IPPROTO_TCP) {
1240 if (INP_CHECK_SOCKAF(so, AF_INET6))
1241 error = ip6_ctloutput(so, sopt);
1244 error = ip_ctloutput(so, sopt);
1247 tp = intotcpcb(inp);
1249 switch (sopt->sopt_dir) {
1251 error = soopt_to_kbuf(sopt, &optval, sizeof optval,
1255 switch (sopt->sopt_name) {
1256 #ifdef TCP_SIGNATURE
1257 case TCP_SIGNATURE_ENABLE:
1259 tp->t_flags |= TF_SIGNATURE;
1261 tp->t_flags &= ~TF_SIGNATURE;
1263 #endif /* TCP_SIGNATURE */
1266 switch (sopt->sopt_name) {
1274 opt = 0; /* dead code to fool gcc */
1281 tp->t_flags &= ~opt;
1286 tp->t_flags |= TF_NOPUSH;
1288 tp->t_flags &= ~TF_NOPUSH;
1289 error = tcp_output(tp);
1295 * Must be between 0 and maxseg. If the requested
1296 * maxseg is too small to satisfy the desired minmss,
1297 * pump it up (silently so sysctl modifications of
1298 * minmss do not create unexpected program failures).
1299 * Handle degenerate cases.
1301 if (optval > 0 && optval <= tp->t_maxseg) {
1302 if (optval + 40 < tcp_minmss) {
1303 optval = tcp_minmss - 40;
1307 tp->t_maxseg = optval;
1314 error = ENOPROTOOPT;
1320 switch (sopt->sopt_name) {
1321 #ifdef TCP_SIGNATURE
1322 case TCP_SIGNATURE_ENABLE:
1323 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1325 #endif /* TCP_SIGNATURE */
1327 optval = tp->t_flags & TF_NODELAY;
1330 optval = tp->t_maxseg;
1333 optval = tp->t_flags & TF_NOOPT;
1336 optval = tp->t_flags & TF_NOPUSH;
1339 error = ENOPROTOOPT;
1343 soopt_from_kbuf(sopt, &optval, sizeof optval);
1350 * tcp_sendspace and tcp_recvspace are the default send and receive window
1351 * sizes, respectively. These are obsolescent (this information should
1352 * be set by the route).
1354 * Use a default that does not require tcp window scaling to be turned
1355 * on. Individual programs or the administrator can increase the default.
1357 u_long tcp_sendspace = 57344; /* largest multiple of PAGE_SIZE < 64k */
1358 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1359 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1360 u_long tcp_recvspace = 57344; /* largest multiple of PAGE_SIZE < 64k */
1361 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1362 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1365 * Attach TCP protocol to socket, allocating internet protocol control
1366 * block, tcp control block, bufer space, and entering LISTEN state
1367 * if to accept connections.
1370 tcp_attach(struct socket *so, struct pru_attach_info *ai)
1377 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1380 if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) {
1381 lwkt_gettoken(&so->so_rcv.ssb_token);
1382 error = soreserve(so, tcp_sendspace, tcp_recvspace,
1384 lwkt_reltoken(&so->so_rcv.ssb_token);
1388 atomic_set_int(&so->so_rcv.ssb_flags, SSB_AUTOSIZE);
1389 atomic_set_int(&so->so_snd.ssb_flags, SSB_AUTOSIZE);
1390 cpu = mycpu->gd_cpuid;
1393 * Set the default port for protocol processing. This will likely
1394 * change when we connect.
1396 error = in_pcballoc(so, &tcbinfo[cpu]);
1402 inp->inp_vflag |= INP_IPV6;
1403 inp->in6p_hops = -1; /* use kernel default */
1407 inp->inp_vflag |= INP_IPV4;
1408 tp = tcp_newtcpcb(inp);
1411 * Make sure the socket is destroyed by the pcbdetach.
1420 sofree(so); /* from ref above */
1423 tp->t_state = TCPS_CLOSED;
1428 * Initiate (or continue) disconnect.
1429 * If embryonic state, just send reset (once).
1430 * If in ``let data drain'' option and linger null, just drop.
1431 * Otherwise (hard), mark socket disconnecting and drop
1432 * current input data; switch states based on user close, and
1433 * send segment to peer (with FIN).
1435 static struct tcpcb *
1436 tcp_disconnect(struct tcpcb *tp)
1438 struct socket *so = tp->t_inpcb->inp_socket;
1440 if (tp->t_state < TCPS_ESTABLISHED) {
1442 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1443 tp = tcp_drop(tp, 0);
1445 lwkt_gettoken(&so->so_rcv.ssb_token);
1446 soisdisconnecting(so);
1447 sbflush(&so->so_rcv.sb);
1448 tp = tcp_usrclosed(tp);
1451 lwkt_reltoken(&so->so_rcv.ssb_token);
1457 * User issued close, and wish to trail through shutdown states:
1458 * if never received SYN, just forget it. If got a SYN from peer,
1459 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1460 * If already got a FIN from peer, then almost done; go to LAST_ACK
1461 * state. In all other cases, have already sent FIN to peer (e.g.
1462 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1463 * for peer to send FIN or not respond to keep-alives, etc.
1464 * We can let the user exit from the close as soon as the FIN is acked.
1466 static struct tcpcb *
1467 tcp_usrclosed(struct tcpcb *tp)
1470 switch (tp->t_state) {
1474 tp->t_state = TCPS_CLOSED;
1479 case TCPS_SYN_RECEIVED:
1480 tp->t_flags |= TF_NEEDFIN;
1483 case TCPS_ESTABLISHED:
1484 tp->t_state = TCPS_FIN_WAIT_1;
1487 case TCPS_CLOSE_WAIT:
1488 tp->t_state = TCPS_LAST_ACK;
1491 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1492 soisdisconnected(tp->t_inpcb->inp_socket);
1493 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1494 if (tp->t_state == TCPS_FIN_WAIT_2) {
1495 tcp_callout_reset(tp, tp->tt_2msl, tcp_maxidle,