2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2006 Robert N. M. Watson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
36 #include "opt_inet6.h"
37 #include "opt_tcpdebug.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
46 #include <sys/domain.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
55 #include <net/route.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
60 #include <netinet/ip6.h>
62 #include <netinet/in_pcb.h>
64 #include <netinet6/in6_pcb.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip_var.h>
69 #include <netinet6/ip6_var.h>
70 #include <netinet6/scope6_var.h>
72 #include <netinet/tcp.h>
73 #include <netinet/tcp_fsm.h>
74 #include <netinet/tcp_seq.h>
75 #include <netinet/tcp_timer.h>
76 #include <netinet/tcp_var.h>
77 #include <netinet/tcpip.h>
79 #include <netinet/tcp_debug.h>
83 * TCP protocol interface to socket abstraction.
85 extern char *tcpstates[]; /* XXX ??? */
87 static int tcp_attach(struct socket *);
88 static int tcp_connect(struct tcpcb *, struct sockaddr *,
91 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
94 static void tcp_disconnect(struct tcpcb *);
95 static void tcp_usrclosed(struct tcpcb *);
96 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
99 #define TCPDEBUG0 int ostate = 0
100 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
101 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
102 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
106 #define TCPDEBUG2(req)
110 * TCP attaches to socket via pru_attach(), reserving space,
111 * and an internet control block.
114 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
117 struct tcpcb *tp = NULL;
122 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
125 error = tcp_attach(so);
129 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
130 so->so_linger = TCP_LINGERTIME;
135 TCPDEBUG2(PRU_ATTACH);
140 * tcp_detach is called when the socket layer loses its final reference
141 * to the socket, be it a file descriptor reference, a reference from TCP,
142 * etc. At this point, there is only one case in which we will keep around
143 * inpcb state: time wait.
145 * This function can probably be re-absorbed back into tcp_usr_detach() now
146 * that there is a single detach path.
149 tcp_detach(struct socket *so, struct inpcb *inp)
153 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
156 INP_INFO_WLOCK_ASSERT(&tcbinfo);
157 INP_LOCK_ASSERT(inp);
159 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
160 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
164 if (inp->inp_vflag & INP_TIMEWAIT) {
166 * There are two cases to handle: one in which the time wait
167 * state is being discarded (INP_DROPPED), and one in which
168 * this connection will remain in timewait. In the former,
169 * it is time to discard all state (except tcptw, which has
170 * already been discarded by the timewait close code, which
171 * should be further up the call stack somewhere). In the
172 * latter case, we detach from the socket, but leave the pcb
173 * present until timewait ends.
175 * XXXRW: Would it be cleaner to free the tcptw here?
177 if (inp->inp_vflag & INP_DROPPED) {
178 KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
179 "INP_DROPPED && tp != NULL"));
202 * If the connection is not in timewait, we consider two
203 * two conditions: one in which no further processing is
204 * necessary (dropped || embryonic), and one in which TCP is
205 * not yet done, but no longer requires the socket, so the
206 * pcb will persist for the time being.
208 * XXXRW: Does the second case still occur?
210 if (inp->inp_vflag & INP_DROPPED ||
211 tp->t_state < TCPS_SYN_SENT) {
236 * pru_detach() detaches the TCP protocol from the socket.
237 * If the protocol state is non-embryonic, then can't
238 * do this directly: have to initiate a pru_disconnect(),
239 * which may finish later; embryonic TCB's can just
243 tcp_usr_detach(struct socket *so)
250 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
251 INP_INFO_WLOCK(&tcbinfo);
253 KASSERT(inp->inp_socket != NULL,
254 ("tcp_usr_detach: inp_socket == NULL"));
259 TCPDEBUG2(PRU_DETACH);
260 INP_INFO_WUNLOCK(&tcbinfo);
264 * Give the socket an address.
267 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
271 struct tcpcb *tp = NULL;
272 struct sockaddr_in *sinp;
274 sinp = (struct sockaddr_in *)nam;
275 if (nam->sa_len != sizeof (*sinp))
278 * Must check for multicast addresses and disallow binding
281 if (sinp->sin_family == AF_INET &&
282 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
283 return (EAFNOSUPPORT);
286 INP_INFO_WLOCK(&tcbinfo);
288 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
290 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
296 error = in_pcbbind(inp, nam, td->td_ucred);
300 INP_INFO_WUNLOCK(&tcbinfo);
307 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
311 struct tcpcb *tp = NULL;
312 struct sockaddr_in6 *sin6p;
314 sin6p = (struct sockaddr_in6 *)nam;
315 if (nam->sa_len != sizeof (*sin6p))
318 * Must check for multicast addresses and disallow binding
321 if (sin6p->sin6_family == AF_INET6 &&
322 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
323 return (EAFNOSUPPORT);
326 INP_INFO_WLOCK(&tcbinfo);
328 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
330 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
336 inp->inp_vflag &= ~INP_IPV4;
337 inp->inp_vflag |= INP_IPV6;
338 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
339 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
340 inp->inp_vflag |= INP_IPV4;
341 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
342 struct sockaddr_in sin;
344 in6_sin6_2_sin(&sin, sin6p);
345 inp->inp_vflag |= INP_IPV4;
346 inp->inp_vflag &= ~INP_IPV6;
347 error = in_pcbbind(inp, (struct sockaddr *)&sin,
352 error = in6_pcbbind(inp, nam, td->td_ucred);
356 INP_INFO_WUNLOCK(&tcbinfo);
362 * Prepare to accept connections.
365 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
369 struct tcpcb *tp = NULL;
372 INP_INFO_WLOCK(&tcbinfo);
374 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
376 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
383 error = solisten_proto_check(so);
384 if (error == 0 && inp->inp_lport == 0)
385 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
387 tp->t_state = TCPS_LISTEN;
388 solisten_proto(so, backlog);
393 TCPDEBUG2(PRU_LISTEN);
395 INP_INFO_WUNLOCK(&tcbinfo);
401 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
405 struct tcpcb *tp = NULL;
408 INP_INFO_WLOCK(&tcbinfo);
410 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
412 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
419 error = solisten_proto_check(so);
420 if (error == 0 && inp->inp_lport == 0) {
421 inp->inp_vflag &= ~INP_IPV4;
422 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
423 inp->inp_vflag |= INP_IPV4;
424 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
427 tp->t_state = TCPS_LISTEN;
428 solisten_proto(so, backlog);
433 TCPDEBUG2(PRU_LISTEN);
435 INP_INFO_WUNLOCK(&tcbinfo);
441 * Initiate connection to peer.
442 * Create a template for use in transmissions on this connection.
443 * Enter SYN_SENT state, and mark socket as connecting.
444 * Start keep-alive timer, and seed output sequence space.
445 * Send initial segment on connection.
448 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
452 struct tcpcb *tp = NULL;
453 struct sockaddr_in *sinp;
455 sinp = (struct sockaddr_in *)nam;
456 if (nam->sa_len != sizeof (*sinp))
459 * Must disallow TCP ``connections'' to multicast addresses.
461 if (sinp->sin_family == AF_INET
462 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
463 return (EAFNOSUPPORT);
464 if (jailed(td->td_ucred))
465 prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
468 INP_INFO_WLOCK(&tcbinfo);
470 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
472 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
478 if ((error = tcp_connect(tp, nam, td)) != 0)
480 error = tcp_output(tp);
482 TCPDEBUG2(PRU_CONNECT);
484 INP_INFO_WUNLOCK(&tcbinfo);
490 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
494 struct tcpcb *tp = NULL;
495 struct sockaddr_in6 *sin6p;
499 sin6p = (struct sockaddr_in6 *)nam;
500 if (nam->sa_len != sizeof (*sin6p))
503 * Must disallow TCP ``connections'' to multicast addresses.
505 if (sin6p->sin6_family == AF_INET6
506 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
507 return (EAFNOSUPPORT);
509 INP_INFO_WLOCK(&tcbinfo);
511 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
513 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
519 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
520 struct sockaddr_in sin;
522 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
527 in6_sin6_2_sin(&sin, sin6p);
528 inp->inp_vflag |= INP_IPV4;
529 inp->inp_vflag &= ~INP_IPV6;
530 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
532 error = tcp_output(tp);
535 inp->inp_vflag &= ~INP_IPV4;
536 inp->inp_vflag |= INP_IPV6;
537 inp->inp_inc.inc_isipv6 = 1;
538 if ((error = tcp6_connect(tp, nam, td)) != 0)
540 error = tcp_output(tp);
543 TCPDEBUG2(PRU_CONNECT);
545 INP_INFO_WUNLOCK(&tcbinfo);
551 * Initiate disconnect from peer.
552 * If connection never passed embryonic stage, just drop;
553 * else if don't need to let data drain, then can just drop anyways,
554 * else have to begin TCP shutdown process: mark socket disconnecting,
555 * drain unread data, state switch to reflect user close, and
556 * send segment (e.g. FIN) to peer. Socket will be really disconnected
557 * when peer sends FIN and acks ours.
559 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
562 tcp_usr_disconnect(struct socket *so)
565 struct tcpcb *tp = NULL;
569 INP_INFO_WLOCK(&tcbinfo);
571 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
573 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
581 TCPDEBUG2(PRU_DISCONNECT);
583 INP_INFO_WUNLOCK(&tcbinfo);
588 * Accept a connection. Essentially all the work is
589 * done at higher levels; just return the address
590 * of the peer, storing through addr.
593 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
596 struct inpcb *inp = NULL;
597 struct tcpcb *tp = NULL;
602 if (so->so_state & SS_ISDISCONNECTED)
603 return (ECONNABORTED);
606 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
608 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
609 error = ECONNABORTED;
616 * We inline in_setpeeraddr and COMMON_END here, so that we can
617 * copy the data of interest and defer the malloc until after we
620 port = inp->inp_fport;
621 addr = inp->inp_faddr;
624 TCPDEBUG2(PRU_ACCEPT);
627 *nam = in_sockaddr(port, &addr);
633 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
635 struct inpcb *inp = NULL;
637 struct tcpcb *tp = NULL;
639 struct in6_addr addr6;
644 if (so->so_state & SS_ISDISCONNECTED)
645 return (ECONNABORTED);
648 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
650 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
658 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
659 * copy the data of interest and defer the malloc until after we
662 if (inp->inp_vflag & INP_IPV4) {
664 port = inp->inp_fport;
665 addr = inp->inp_faddr;
667 port = inp->inp_fport;
668 addr6 = inp->in6p_faddr;
672 TCPDEBUG2(PRU_ACCEPT);
676 *nam = in6_v4mapsin6_sockaddr(port, &addr);
678 *nam = in6_sockaddr(port, &addr6);
685 * This is the wrapper function for in_setsockaddr. We just pass down
686 * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
687 * here because in_setsockaddr will call malloc and can block.
690 tcp_sockaddr(struct socket *so, struct sockaddr **nam)
692 return (in_setsockaddr(so, nam, &tcbinfo));
696 * This is the wrapper function for in_setpeeraddr. We just pass down
697 * the pcbinfo for in_setpeeraddr to lock.
700 tcp_peeraddr(struct socket *so, struct sockaddr **nam)
702 return (in_setpeeraddr(so, nam, &tcbinfo));
706 * Mark the connection as being incapable of further output.
709 tcp_usr_shutdown(struct socket *so)
713 struct tcpcb *tp = NULL;
716 INP_INFO_WLOCK(&tcbinfo);
718 KASSERT(inp != NULL, ("inp == NULL"));
720 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
728 error = tcp_output(tp);
731 TCPDEBUG2(PRU_SHUTDOWN);
733 INP_INFO_WUNLOCK(&tcbinfo);
739 * After a receive, possibly send window update to peer.
742 tcp_usr_rcvd(struct socket *so, int flags)
745 struct tcpcb *tp = NULL;
750 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
752 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
767 * Do a send by putting data in output queue and updating urgent
768 * marker if URG set. Possibly send more data. Unlike the other
769 * pru_*() routines, the mbuf chains are our responsibility. We
770 * must either enqueue them or free them. The other pru_* routines
771 * generally are caller-frees.
774 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
775 struct sockaddr *nam, struct mbuf *control, struct thread *td)
779 struct tcpcb *tp = NULL;
787 * We require the pcbinfo lock in two cases:
789 * (1) An implied connect is taking place, which can result in
790 * binding IPs and ports and hence modification of the pcb hash
793 * (2) PRUS_EOF is set, resulting in explicit close on the send.
795 if ((nam != NULL) || (flags & PRUS_EOF)) {
796 INP_INFO_WLOCK(&tcbinfo);
800 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
802 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
807 isipv6 = nam && nam->sa_family == AF_INET6;
812 /* TCP doesn't do control messages (rights, creds, etc) */
813 if (control->m_len) {
820 m_freem(control); /* empty control, just free it */
822 if (!(flags & PRUS_OOB)) {
823 sbappendstream(&so->so_snd, m);
824 if (nam && tp->t_state < TCPS_SYN_SENT) {
826 * Do implied connect if not yet connected,
827 * initialize window to default value, and
828 * initialize maxseg/maxopd using peer's cached
831 INP_INFO_WLOCK_ASSERT(&tcbinfo);
834 error = tcp6_connect(tp, nam, td);
837 error = tcp_connect(tp, nam, td);
840 tp->snd_wnd = TTCP_CLIENT_SND_WND;
843 if (flags & PRUS_EOF) {
845 * Close the send side of the connection after
848 INP_INFO_WLOCK_ASSERT(&tcbinfo);
853 INP_INFO_WUNLOCK(&tcbinfo);
857 if (flags & PRUS_MORETOCOME)
858 tp->t_flags |= TF_MORETOCOME;
859 error = tcp_output(tp);
860 if (flags & PRUS_MORETOCOME)
861 tp->t_flags &= ~TF_MORETOCOME;
865 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
867 SOCKBUF_LOCK(&so->so_snd);
868 if (sbspace(&so->so_snd) < -512) {
869 SOCKBUF_UNLOCK(&so->so_snd);
875 * According to RFC961 (Assigned Protocols),
876 * the urgent pointer points to the last octet
877 * of urgent data. We continue, however,
878 * to consider it to indicate the first octet
879 * of data past the urgent section.
880 * Otherwise, snd_up should be one lower.
882 sbappendstream_locked(&so->so_snd, m);
883 SOCKBUF_UNLOCK(&so->so_snd);
884 if (nam && tp->t_state < TCPS_SYN_SENT) {
886 * Do implied connect if not yet connected,
887 * initialize window to default value, and
888 * initialize maxseg/maxopd using peer's cached
891 INP_INFO_WLOCK_ASSERT(&tcbinfo);
894 error = tcp6_connect(tp, nam, td);
897 error = tcp_connect(tp, nam, td);
900 tp->snd_wnd = TTCP_CLIENT_SND_WND;
902 INP_INFO_WUNLOCK(&tcbinfo);
905 INP_INFO_WUNLOCK(&tcbinfo);
908 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
909 tp->t_flags |= TF_FORCEDATA;
910 error = tcp_output(tp);
911 tp->t_flags &= ~TF_FORCEDATA;
914 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
915 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
918 INP_INFO_WUNLOCK(&tcbinfo);
923 * Abort the TCP. Drop the connection abruptly.
926 tcp_usr_abort(struct socket *so)
929 struct tcpcb *tp = NULL;
933 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
935 INP_INFO_WLOCK(&tcbinfo);
937 KASSERT(inp->inp_socket != NULL,
938 ("tcp_usr_abort: inp_socket == NULL"));
941 * If we still have full TCP state, and we're not dropped, drop.
943 if (!(inp->inp_vflag & INP_TIMEWAIT) &&
944 !(inp->inp_vflag & INP_DROPPED)) {
947 tcp_drop(tp, ECONNABORTED);
948 TCPDEBUG2(PRU_ABORT);
950 if (!(inp->inp_vflag & INP_DROPPED)) {
952 so->so_state |= SS_PROTOREF;
954 inp->inp_vflag |= INP_SOCKREF;
957 INP_INFO_WUNLOCK(&tcbinfo);
961 * TCP socket is closed. Start friendly disconnect.
964 tcp_usr_close(struct socket *so)
967 struct tcpcb *tp = NULL;
971 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
973 INP_INFO_WLOCK(&tcbinfo);
975 KASSERT(inp->inp_socket != NULL,
976 ("tcp_usr_close: inp_socket == NULL"));
979 * If we still have full TCP state, and we're not dropped, initiate
982 if (!(inp->inp_vflag & INP_TIMEWAIT) &&
983 !(inp->inp_vflag & INP_DROPPED)) {
987 TCPDEBUG2(PRU_CLOSE);
989 if (!(inp->inp_vflag & INP_DROPPED)) {
991 so->so_state |= SS_PROTOREF;
993 inp->inp_vflag |= INP_SOCKREF;
996 INP_INFO_WUNLOCK(&tcbinfo);
1000 * Receive out-of-band data.
1003 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1007 struct tcpcb *tp = NULL;
1010 inp = sotoinpcb(so);
1011 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1013 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
1017 tp = intotcpcb(inp);
1019 if ((so->so_oobmark == 0 &&
1020 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1021 so->so_options & SO_OOBINLINE ||
1022 tp->t_oobflags & TCPOOB_HADDATA) {
1026 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1027 error = EWOULDBLOCK;
1031 *mtod(m, caddr_t) = tp->t_iobc;
1032 if ((flags & MSG_PEEK) == 0)
1033 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1036 TCPDEBUG2(PRU_RCVOOB);
1041 struct pr_usrreqs tcp_usrreqs = {
1042 .pru_abort = tcp_usr_abort,
1043 .pru_accept = tcp_usr_accept,
1044 .pru_attach = tcp_usr_attach,
1045 .pru_bind = tcp_usr_bind,
1046 .pru_connect = tcp_usr_connect,
1047 .pru_control = in_control,
1048 .pru_detach = tcp_usr_detach,
1049 .pru_disconnect = tcp_usr_disconnect,
1050 .pru_listen = tcp_usr_listen,
1051 .pru_peeraddr = tcp_peeraddr,
1052 .pru_rcvd = tcp_usr_rcvd,
1053 .pru_rcvoob = tcp_usr_rcvoob,
1054 .pru_send = tcp_usr_send,
1055 .pru_shutdown = tcp_usr_shutdown,
1056 .pru_sockaddr = tcp_sockaddr,
1057 .pru_sosetlabel = in_pcbsosetlabel,
1058 .pru_close = tcp_usr_close,
1062 struct pr_usrreqs tcp6_usrreqs = {
1063 .pru_abort = tcp_usr_abort,
1064 .pru_accept = tcp6_usr_accept,
1065 .pru_attach = tcp_usr_attach,
1066 .pru_bind = tcp6_usr_bind,
1067 .pru_connect = tcp6_usr_connect,
1068 .pru_control = in6_control,
1069 .pru_detach = tcp_usr_detach,
1070 .pru_disconnect = tcp_usr_disconnect,
1071 .pru_listen = tcp6_usr_listen,
1072 .pru_peeraddr = in6_mapped_peeraddr,
1073 .pru_rcvd = tcp_usr_rcvd,
1074 .pru_rcvoob = tcp_usr_rcvoob,
1075 .pru_send = tcp_usr_send,
1076 .pru_shutdown = tcp_usr_shutdown,
1077 .pru_sockaddr = in6_mapped_sockaddr,
1078 .pru_sosetlabel = in_pcbsosetlabel,
1079 .pru_close = tcp_usr_close,
1084 * Common subroutine to open a TCP connection to remote host specified
1085 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1086 * port number if needed. Call in_pcbconnect_setup to do the routing and
1087 * to choose a local host address (interface). If there is an existing
1088 * incarnation of the same connection in TIME-WAIT state and if the remote
1089 * host was sending CC options and if the connection duration was < MSL, then
1090 * truncate the previous TIME-WAIT state and proceed.
1091 * Initialize connection parameters and enter SYN-SENT state.
1094 tcp_connect(tp, nam, td)
1095 register struct tcpcb *tp;
1096 struct sockaddr *nam;
1099 struct inpcb *inp = tp->t_inpcb, *oinp;
1100 struct socket *so = inp->inp_socket;
1101 struct in_addr laddr;
1105 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1106 INP_LOCK_ASSERT(inp);
1108 if (inp->inp_lport == 0) {
1109 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1115 * Cannot simply call in_pcbconnect, because there might be an
1116 * earlier incarnation of this same connection still in
1117 * TIME_WAIT state, creating an ADDRINUSE error.
1119 laddr = inp->inp_laddr;
1120 lport = inp->inp_lport;
1121 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1122 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1123 if (error && oinp == NULL)
1127 inp->inp_laddr = laddr;
1130 /* Compute window scaling to request. */
1131 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1132 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1133 tp->request_r_scale++;
1136 tcpstat.tcps_connattempt++;
1137 tp->t_state = TCPS_SYN_SENT;
1138 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1139 tp->iss = tcp_new_isn(tp);
1140 tp->t_bw_rtseq = tp->iss;
1141 tcp_sendseqinit(tp);
1148 tcp6_connect(tp, nam, td)
1149 register struct tcpcb *tp;
1150 struct sockaddr *nam;
1153 struct inpcb *inp = tp->t_inpcb, *oinp;
1154 struct socket *so = inp->inp_socket;
1155 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1156 struct in6_addr *addr6;
1159 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1160 INP_LOCK_ASSERT(inp);
1162 if (inp->inp_lport == 0) {
1163 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1169 * Cannot simply call in_pcbconnect, because there might be an
1170 * earlier incarnation of this same connection still in
1171 * TIME_WAIT state, creating an ADDRINUSE error.
1172 * in6_pcbladdr() also handles scope zone IDs.
1174 error = in6_pcbladdr(inp, nam, &addr6);
1177 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1178 &sin6->sin6_addr, sin6->sin6_port,
1179 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1182 inp->inp_lport, 0, NULL);
1185 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1186 inp->in6p_laddr = *addr6;
1187 inp->in6p_faddr = sin6->sin6_addr;
1188 inp->inp_fport = sin6->sin6_port;
1189 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1190 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
1191 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
1192 inp->in6p_flowinfo |=
1193 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1196 /* Compute window scaling to request. */
1197 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1198 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1199 tp->request_r_scale++;
1202 tcpstat.tcps_connattempt++;
1203 tp->t_state = TCPS_SYN_SENT;
1204 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1205 tp->iss = tcp_new_isn(tp);
1206 tp->t_bw_rtseq = tp->iss;
1207 tcp_sendseqinit(tp);
1214 * Export TCP internal state information via a struct tcp_info, based on the
1215 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1216 * (TCP state machine, etc). We export all information using FreeBSD-native
1217 * constants -- for example, the numeric values for tcpi_state will differ
1221 tcp_fill_info(tp, ti)
1223 struct tcp_info *ti;
1226 INP_LOCK_ASSERT(tp->t_inpcb);
1227 bzero(ti, sizeof(*ti));
1229 ti->tcpi_state = tp->t_state;
1230 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1231 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1232 if (tp->sack_enable)
1233 ti->tcpi_options |= TCPI_OPT_SACK;
1234 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1235 ti->tcpi_options |= TCPI_OPT_WSCALE;
1236 ti->tcpi_snd_wscale = tp->snd_scale;
1237 ti->tcpi_rcv_wscale = tp->rcv_scale;
1239 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1240 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1243 * FreeBSD-specific extension fields for tcp_info.
1245 ti->tcpi_rcv_space = tp->rcv_wnd;
1246 ti->tcpi_snd_wnd = tp->snd_wnd;
1247 ti->tcpi_snd_bwnd = tp->snd_bwnd;
1251 * The new sockopt interface makes it possible for us to block in the
1252 * copyin/out step (if we take a page fault). Taking a page fault at
1253 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
1254 * use TSM, there probably isn't any need for this function to run at
1255 * splnet() any more. This needs more examination.)
1257 * XXXRW: The locking here is wrong; we may take a page fault while holding
1261 tcp_ctloutput(so, sopt)
1263 struct sockopt *sopt;
1265 int error, opt, optval;
1271 inp = sotoinpcb(so);
1272 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1274 if (sopt->sopt_level != IPPROTO_TCP) {
1277 if (INP_CHECK_SOCKAF(so, AF_INET6))
1278 error = ip6_ctloutput(so, sopt);
1281 error = ip_ctloutput(so, sopt);
1284 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
1288 tp = intotcpcb(inp);
1290 switch (sopt->sopt_dir) {
1292 switch (sopt->sopt_name) {
1293 #ifdef TCP_SIGNATURE
1295 error = sooptcopyin(sopt, &optval, sizeof optval,
1301 tp->t_flags |= TF_SIGNATURE;
1303 tp->t_flags &= ~TF_SIGNATURE;
1305 #endif /* TCP_SIGNATURE */
1308 error = sooptcopyin(sopt, &optval, sizeof optval,
1313 switch (sopt->sopt_name) {
1321 opt = 0; /* dead code to fool gcc */
1328 tp->t_flags &= ~opt;
1332 error = sooptcopyin(sopt, &optval, sizeof optval,
1338 tp->t_flags |= TF_NOPUSH;
1340 tp->t_flags &= ~TF_NOPUSH;
1341 error = tcp_output(tp);
1346 error = sooptcopyin(sopt, &optval, sizeof optval,
1351 if (optval > 0 && optval <= tp->t_maxseg &&
1352 optval + 40 >= tcp_minmss)
1353 tp->t_maxseg = optval;
1363 error = ENOPROTOOPT;
1369 switch (sopt->sopt_name) {
1370 #ifdef TCP_SIGNATURE
1372 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1373 error = sooptcopyout(sopt, &optval, sizeof optval);
1377 optval = tp->t_flags & TF_NODELAY;
1378 error = sooptcopyout(sopt, &optval, sizeof optval);
1381 optval = tp->t_maxseg;
1382 error = sooptcopyout(sopt, &optval, sizeof optval);
1385 optval = tp->t_flags & TF_NOOPT;
1386 error = sooptcopyout(sopt, &optval, sizeof optval);
1389 optval = tp->t_flags & TF_NOPUSH;
1390 error = sooptcopyout(sopt, &optval, sizeof optval);
1393 tcp_fill_info(tp, &ti);
1394 error = sooptcopyout(sopt, &ti, sizeof ti);
1397 error = ENOPROTOOPT;
1408 * tcp_sendspace and tcp_recvspace are the default send and receive window
1409 * sizes, respectively. These are obsolescent (this information should
1410 * be set by the route).
1412 u_long tcp_sendspace = 1024*32;
1413 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1414 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1415 u_long tcp_recvspace = 1024*64;
1416 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1417 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1420 * Attach TCP protocol to socket, allocating
1421 * internet protocol control block, tcp control block,
1422 * bufer space, and entering LISTEN state if to accept connections.
1428 register struct tcpcb *tp;
1432 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1435 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1436 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1440 INP_INFO_WLOCK(&tcbinfo);
1441 error = in_pcballoc(so, &tcbinfo);
1443 INP_INFO_WUNLOCK(&tcbinfo);
1446 inp = sotoinpcb(so);
1449 inp->inp_vflag |= INP_IPV6;
1450 inp->in6p_hops = -1; /* use kernel default */
1454 inp->inp_vflag |= INP_IPV4;
1455 tp = tcp_newtcpcb(inp);
1468 INP_INFO_WUNLOCK(&tcbinfo);
1471 tp->t_state = TCPS_CLOSED;
1473 INP_INFO_WUNLOCK(&tcbinfo);
1478 * Initiate (or continue) disconnect.
1479 * If embryonic state, just send reset (once).
1480 * If in ``let data drain'' option and linger null, just drop.
1481 * Otherwise (hard), mark socket disconnecting and drop
1482 * current input data; switch states based on user close, and
1483 * send segment to peer (with FIN).
1487 register struct tcpcb *tp;
1489 struct inpcb *inp = tp->t_inpcb;
1490 struct socket *so = inp->inp_socket;
1492 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1493 INP_LOCK_ASSERT(inp);
1496 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1497 * socket is still open.
1499 if (tp->t_state < TCPS_ESTABLISHED) {
1502 ("tcp_disconnect: tcp_close() returned NULL"));
1503 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1504 tp = tcp_drop(tp, 0);
1506 ("tcp_disconnect: tcp_drop() returned NULL"));
1508 soisdisconnecting(so);
1509 sbflush(&so->so_rcv);
1511 if (!(inp->inp_vflag & INP_DROPPED))
1517 * User issued close, and wish to trail through shutdown states:
1518 * if never received SYN, just forget it. If got a SYN from peer,
1519 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1520 * If already got a FIN from peer, then almost done; go to LAST_ACK
1521 * state. In all other cases, have already sent FIN to peer (e.g.
1522 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1523 * for peer to send FIN or not respond to keep-alives, etc.
1524 * We can let the user exit from the close as soon as the FIN is acked.
1528 register struct tcpcb *tp;
1531 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1532 INP_LOCK_ASSERT(tp->t_inpcb);
1534 switch (tp->t_state) {
1538 tp->t_state = TCPS_CLOSED;
1541 * tcp_close() should never return NULL here as the socket is
1545 ("tcp_usrclosed: tcp_close() returned NULL"));
1549 case TCPS_SYN_RECEIVED:
1550 tp->t_flags |= TF_NEEDFIN;
1553 case TCPS_ESTABLISHED:
1554 tp->t_state = TCPS_FIN_WAIT_1;
1557 case TCPS_CLOSE_WAIT:
1558 tp->t_state = TCPS_LAST_ACK;
1561 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1562 soisdisconnected(tp->t_inpcb->inp_socket);
1563 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1564 if (tp->t_state == TCPS_FIN_WAIT_2)
1565 callout_reset(tp->tt_2msl, tcp_maxidle,
1566 tcp_timer_2msl, tp);