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"));
123 INP_INFO_WLOCK(&tcbinfo);
126 error = tcp_attach(so);
130 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
131 so->so_linger = TCP_LINGERTIME;
136 TCPDEBUG2(PRU_ATTACH);
137 INP_INFO_WUNLOCK(&tcbinfo);
142 * pru_detach() detaches the TCP protocol from the socket.
143 * If the protocol state is non-embryonic, then can't
144 * do this directly: have to initiate a pru_disconnect(),
145 * which may finish later; embryonic TCB's can just
149 tcp_usr_detach(struct socket *so)
154 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
159 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
160 INP_INFO_WLOCK(&tcbinfo);
162 KASSERT(inp->inp_socket != NULL,
163 ("tcp_usr_detach: inp_socket == NULL"));
167 * First, if we still have full TCP state, and we're not dropped,
168 * initiate a disconnect.
170 if (!(inp->inp_vflag & INP_TIMEWAIT) &&
171 !(inp->inp_vflag & INP_DROPPED)) {
177 * Second, release any protocol state that we can reasonably release.
178 * Note that the call to tcp_disconnect() may actually have changed
179 * the TCP state, so we have to re-evaluate INP_TIMEWAIT and
182 if (inp->inp_vflag & INP_TIMEWAIT) {
183 if (inp->inp_vflag & INP_DROPPED) {
185 * Connection was in time wait and has been dropped;
186 * the calling path is either via tcp_twclose(), or
187 * as a result of an eventual soclose() after
188 * tcp_twclose() has been called. In either case,
189 * tcp_twclose() has detached the tcptw from the
190 * inpcb, so we just detach and free the inpcb.
192 * XXXRW: Would it be cleaner to free the tcptw
208 * Connection is in time wait and has not yet been
209 * dropped; allow the socket to be discarded, but
210 * need to keep inpcb until end of time wait.
222 * If not in timewait, there are two possible paths. First,
223 * the TCP connection is either embryonic or done, in which
224 * case we tear down all state. Second, it may still be
225 * active, in which case we acquire a reference to the socket
226 * and will free it later when TCP is done.
229 if (inp->inp_vflag & INP_DROPPED ||
230 tp->t_state < TCPS_SYN_SENT) {
245 so->so_state |= SS_PROTOREF;
247 inp->inp_vflag |= INP_SOCKREF;
252 TCPDEBUG2(PRU_DETACH);
253 INP_INFO_WUNLOCK(&tcbinfo);
257 * Give the socket an address.
260 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
264 struct tcpcb *tp = NULL;
265 struct sockaddr_in *sinp;
267 sinp = (struct sockaddr_in *)nam;
268 if (nam->sa_len != sizeof (*sinp))
271 * Must check for multicast addresses and disallow binding
274 if (sinp->sin_family == AF_INET &&
275 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
276 return (EAFNOSUPPORT);
279 INP_INFO_WLOCK(&tcbinfo);
281 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
283 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
289 error = in_pcbbind(inp, nam, td->td_ucred);
293 INP_INFO_WUNLOCK(&tcbinfo);
300 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
304 struct tcpcb *tp = NULL;
305 struct sockaddr_in6 *sin6p;
307 sin6p = (struct sockaddr_in6 *)nam;
308 if (nam->sa_len != sizeof (*sin6p))
311 * Must check for multicast addresses and disallow binding
314 if (sin6p->sin6_family == AF_INET6 &&
315 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
316 return (EAFNOSUPPORT);
319 INP_INFO_WLOCK(&tcbinfo);
321 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
323 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
329 inp->inp_vflag &= ~INP_IPV4;
330 inp->inp_vflag |= INP_IPV6;
331 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
332 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
333 inp->inp_vflag |= INP_IPV4;
334 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
335 struct sockaddr_in sin;
337 in6_sin6_2_sin(&sin, sin6p);
338 inp->inp_vflag |= INP_IPV4;
339 inp->inp_vflag &= ~INP_IPV6;
340 error = in_pcbbind(inp, (struct sockaddr *)&sin,
345 error = in6_pcbbind(inp, nam, td->td_ucred);
349 INP_INFO_WUNLOCK(&tcbinfo);
355 * Prepare to accept connections.
358 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
362 struct tcpcb *tp = NULL;
365 INP_INFO_WLOCK(&tcbinfo);
367 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
369 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
376 error = solisten_proto_check(so);
377 if (error == 0 && inp->inp_lport == 0)
378 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
380 tp->t_state = TCPS_LISTEN;
381 solisten_proto(so, backlog);
386 TCPDEBUG2(PRU_LISTEN);
388 INP_INFO_WUNLOCK(&tcbinfo);
394 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
398 struct tcpcb *tp = NULL;
401 INP_INFO_WLOCK(&tcbinfo);
403 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
405 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
412 error = solisten_proto_check(so);
413 if (error == 0 && inp->inp_lport == 0) {
414 inp->inp_vflag &= ~INP_IPV4;
415 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
416 inp->inp_vflag |= INP_IPV4;
417 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
420 tp->t_state = TCPS_LISTEN;
421 solisten_proto(so, backlog);
426 TCPDEBUG2(PRU_LISTEN);
428 INP_INFO_WUNLOCK(&tcbinfo);
434 * Initiate connection to peer.
435 * Create a template for use in transmissions on this connection.
436 * Enter SYN_SENT state, and mark socket as connecting.
437 * Start keep-alive timer, and seed output sequence space.
438 * Send initial segment on connection.
441 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
445 struct tcpcb *tp = NULL;
446 struct sockaddr_in *sinp;
448 sinp = (struct sockaddr_in *)nam;
449 if (nam->sa_len != sizeof (*sinp))
452 * Must disallow TCP ``connections'' to multicast addresses.
454 if (sinp->sin_family == AF_INET
455 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
456 return (EAFNOSUPPORT);
457 if (jailed(td->td_ucred))
458 prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
461 INP_INFO_WLOCK(&tcbinfo);
463 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
465 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
471 if ((error = tcp_connect(tp, nam, td)) != 0)
473 error = tcp_output(tp);
475 TCPDEBUG2(PRU_CONNECT);
477 INP_INFO_WUNLOCK(&tcbinfo);
483 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
487 struct tcpcb *tp = NULL;
488 struct sockaddr_in6 *sin6p;
492 sin6p = (struct sockaddr_in6 *)nam;
493 if (nam->sa_len != sizeof (*sin6p))
496 * Must disallow TCP ``connections'' to multicast addresses.
498 if (sin6p->sin6_family == AF_INET6
499 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
500 return (EAFNOSUPPORT);
502 INP_INFO_WLOCK(&tcbinfo);
504 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
506 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
512 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
513 struct sockaddr_in sin;
515 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
520 in6_sin6_2_sin(&sin, sin6p);
521 inp->inp_vflag |= INP_IPV4;
522 inp->inp_vflag &= ~INP_IPV6;
523 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
525 error = tcp_output(tp);
528 inp->inp_vflag &= ~INP_IPV4;
529 inp->inp_vflag |= INP_IPV6;
530 inp->inp_inc.inc_isipv6 = 1;
531 if ((error = tcp6_connect(tp, nam, td)) != 0)
533 error = tcp_output(tp);
536 TCPDEBUG2(PRU_CONNECT);
538 INP_INFO_WUNLOCK(&tcbinfo);
544 * Initiate disconnect from peer.
545 * If connection never passed embryonic stage, just drop;
546 * else if don't need to let data drain, then can just drop anyways,
547 * else have to begin TCP shutdown process: mark socket disconnecting,
548 * drain unread data, state switch to reflect user close, and
549 * send segment (e.g. FIN) to peer. Socket will be really disconnected
550 * when peer sends FIN and acks ours.
552 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
555 tcp_usr_disconnect(struct socket *so)
558 struct tcpcb *tp = NULL;
562 INP_INFO_WLOCK(&tcbinfo);
564 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
566 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
574 TCPDEBUG2(PRU_DISCONNECT);
576 INP_INFO_WUNLOCK(&tcbinfo);
581 * Accept a connection. Essentially all the work is
582 * done at higher levels; just return the address
583 * of the peer, storing through addr.
586 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
589 struct inpcb *inp = NULL;
590 struct tcpcb *tp = NULL;
595 if (so->so_state & SS_ISDISCONNECTED)
596 return (ECONNABORTED);
599 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
601 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
602 error = ECONNABORTED;
609 * We inline in_setpeeraddr and COMMON_END here, so that we can
610 * copy the data of interest and defer the malloc until after we
613 port = inp->inp_fport;
614 addr = inp->inp_faddr;
617 TCPDEBUG2(PRU_ACCEPT);
620 *nam = in_sockaddr(port, &addr);
626 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
628 struct inpcb *inp = NULL;
630 struct tcpcb *tp = NULL;
632 struct in6_addr addr6;
637 if (so->so_state & SS_ISDISCONNECTED) {
638 error = ECONNABORTED;
643 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
645 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
653 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
654 * copy the data of interest and defer the malloc until after we
657 if (inp->inp_vflag & INP_IPV4) {
659 port = inp->inp_fport;
660 addr = inp->inp_faddr;
662 port = inp->inp_fport;
663 addr6 = inp->in6p_faddr;
667 TCPDEBUG2(PRU_ACCEPT);
671 *nam = in6_v4mapsin6_sockaddr(port, &addr);
673 *nam = in6_sockaddr(port, &addr6);
680 * This is the wrapper function for in_setsockaddr. We just pass down
681 * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
682 * here because in_setsockaddr will call malloc and can block.
685 tcp_sockaddr(struct socket *so, struct sockaddr **nam)
687 return (in_setsockaddr(so, nam, &tcbinfo));
691 * This is the wrapper function for in_setpeeraddr. We just pass down
692 * the pcbinfo for in_setpeeraddr to lock.
695 tcp_peeraddr(struct socket *so, struct sockaddr **nam)
697 return (in_setpeeraddr(so, nam, &tcbinfo));
701 * Mark the connection as being incapable of further output.
704 tcp_usr_shutdown(struct socket *so)
708 struct tcpcb *tp = NULL;
711 INP_INFO_WLOCK(&tcbinfo);
713 KASSERT(inp != NULL, ("inp == NULL"));
715 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
723 error = tcp_output(tp);
726 TCPDEBUG2(PRU_SHUTDOWN);
728 INP_INFO_WUNLOCK(&tcbinfo);
734 * After a receive, possibly send window update to peer.
737 tcp_usr_rcvd(struct socket *so, int flags)
740 struct tcpcb *tp = NULL;
745 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
747 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
762 * Do a send by putting data in output queue and updating urgent
763 * marker if URG set. Possibly send more data. Unlike the other
764 * pru_*() routines, the mbuf chains are our responsibility. We
765 * must either enqueue them or free them. The other pru_* routines
766 * generally are caller-frees.
769 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
770 struct sockaddr *nam, struct mbuf *control, struct thread *td)
774 struct tcpcb *tp = NULL;
782 * We require the pcbinfo lock in two cases:
784 * (1) An implied connect is taking place, which can result in
785 * binding IPs and ports and hence modification of the pcb hash
788 * (2) PRUS_EOF is set, resulting in explicit close on the send.
790 if ((nam != NULL) || (flags & PRUS_EOF)) {
791 INP_INFO_WLOCK(&tcbinfo);
795 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
797 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
802 isipv6 = nam && nam->sa_family == AF_INET6;
807 /* TCP doesn't do control messages (rights, creds, etc) */
808 if (control->m_len) {
815 m_freem(control); /* empty control, just free it */
817 if (!(flags & PRUS_OOB)) {
818 sbappendstream(&so->so_snd, m);
819 if (nam && tp->t_state < TCPS_SYN_SENT) {
821 * Do implied connect if not yet connected,
822 * initialize window to default value, and
823 * initialize maxseg/maxopd using peer's cached
826 INP_INFO_WLOCK_ASSERT(&tcbinfo);
829 error = tcp6_connect(tp, nam, td);
832 error = tcp_connect(tp, nam, td);
835 tp->snd_wnd = TTCP_CLIENT_SND_WND;
838 if (flags & PRUS_EOF) {
840 * Close the send side of the connection after
843 INP_INFO_WLOCK_ASSERT(&tcbinfo);
848 INP_INFO_WUNLOCK(&tcbinfo);
852 if (flags & PRUS_MORETOCOME)
853 tp->t_flags |= TF_MORETOCOME;
854 error = tcp_output(tp);
855 if (flags & PRUS_MORETOCOME)
856 tp->t_flags &= ~TF_MORETOCOME;
860 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
862 SOCKBUF_LOCK(&so->so_snd);
863 if (sbspace(&so->so_snd) < -512) {
864 SOCKBUF_UNLOCK(&so->so_snd);
870 * According to RFC961 (Assigned Protocols),
871 * the urgent pointer points to the last octet
872 * of urgent data. We continue, however,
873 * to consider it to indicate the first octet
874 * of data past the urgent section.
875 * Otherwise, snd_up should be one lower.
877 sbappendstream_locked(&so->so_snd, m);
878 SOCKBUF_UNLOCK(&so->so_snd);
879 if (nam && tp->t_state < TCPS_SYN_SENT) {
881 * Do implied connect if not yet connected,
882 * initialize window to default value, and
883 * initialize maxseg/maxopd using peer's cached
886 INP_INFO_WLOCK_ASSERT(&tcbinfo);
889 error = tcp6_connect(tp, nam, td);
892 error = tcp_connect(tp, nam, td);
895 tp->snd_wnd = TTCP_CLIENT_SND_WND;
897 INP_INFO_WUNLOCK(&tcbinfo);
900 INP_INFO_WUNLOCK(&tcbinfo);
903 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
904 tp->t_flags |= TF_FORCEDATA;
905 error = tcp_output(tp);
906 tp->t_flags &= ~TF_FORCEDATA;
909 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
910 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
913 INP_INFO_WUNLOCK(&tcbinfo);
921 tcp_usr_abort(struct socket *so)
929 * XXXRW: This is not really quite the same, as we want to tcp_drop()
930 * rather than tcp_disconnect(), I think, but for now I'll avoid
931 * replicating all the tear-down logic here.
937 INP_INFO_WLOCK(&tcbinfo);
941 * Do we need to handle timewait here? Aborted connections should
942 * never generate a FIN?
944 KASSERT((inp->inp_vflag & INP_TIMEWAIT) == 0,
945 ("tcp_usr_abort: timewait"));
948 tp = tcp_drop(tp, ECONNABORTED);
949 TCPDEBUG2(PRU_ABORT);
952 INP_INFO_WUNLOCK(&tcbinfo);
957 * Receive out-of-band data.
960 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
964 struct tcpcb *tp = NULL;
968 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
970 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
976 if ((so->so_oobmark == 0 &&
977 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
978 so->so_options & SO_OOBINLINE ||
979 tp->t_oobflags & TCPOOB_HADDATA) {
983 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
988 *mtod(m, caddr_t) = tp->t_iobc;
989 if ((flags & MSG_PEEK) == 0)
990 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
993 TCPDEBUG2(PRU_RCVOOB);
998 struct pr_usrreqs tcp_usrreqs = {
999 .pru_abort = tcp_usr_abort,
1000 .pru_accept = tcp_usr_accept,
1001 .pru_attach = tcp_usr_attach,
1002 .pru_bind = tcp_usr_bind,
1003 .pru_connect = tcp_usr_connect,
1004 .pru_control = in_control,
1005 .pru_detach = tcp_usr_detach,
1006 .pru_disconnect = tcp_usr_disconnect,
1007 .pru_listen = tcp_usr_listen,
1008 .pru_peeraddr = tcp_peeraddr,
1009 .pru_rcvd = tcp_usr_rcvd,
1010 .pru_rcvoob = tcp_usr_rcvoob,
1011 .pru_send = tcp_usr_send,
1012 .pru_shutdown = tcp_usr_shutdown,
1013 .pru_sockaddr = tcp_sockaddr,
1014 .pru_sosetlabel = in_pcbsosetlabel
1018 struct pr_usrreqs tcp6_usrreqs = {
1019 .pru_abort = tcp_usr_abort,
1020 .pru_accept = tcp6_usr_accept,
1021 .pru_attach = tcp_usr_attach,
1022 .pru_bind = tcp6_usr_bind,
1023 .pru_connect = tcp6_usr_connect,
1024 .pru_control = in6_control,
1025 .pru_detach = tcp_usr_detach,
1026 .pru_disconnect = tcp_usr_disconnect,
1027 .pru_listen = tcp6_usr_listen,
1028 .pru_peeraddr = in6_mapped_peeraddr,
1029 .pru_rcvd = tcp_usr_rcvd,
1030 .pru_rcvoob = tcp_usr_rcvoob,
1031 .pru_send = tcp_usr_send,
1032 .pru_shutdown = tcp_usr_shutdown,
1033 .pru_sockaddr = in6_mapped_sockaddr,
1034 .pru_sosetlabel = in_pcbsosetlabel
1039 * Common subroutine to open a TCP connection to remote host specified
1040 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1041 * port number if needed. Call in_pcbconnect_setup to do the routing and
1042 * to choose a local host address (interface). If there is an existing
1043 * incarnation of the same connection in TIME-WAIT state and if the remote
1044 * host was sending CC options and if the connection duration was < MSL, then
1045 * truncate the previous TIME-WAIT state and proceed.
1046 * Initialize connection parameters and enter SYN-SENT state.
1049 tcp_connect(tp, nam, td)
1050 register struct tcpcb *tp;
1051 struct sockaddr *nam;
1054 struct inpcb *inp = tp->t_inpcb, *oinp;
1055 struct socket *so = inp->inp_socket;
1056 struct in_addr laddr;
1060 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1061 INP_LOCK_ASSERT(inp);
1063 if (inp->inp_lport == 0) {
1064 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1070 * Cannot simply call in_pcbconnect, because there might be an
1071 * earlier incarnation of this same connection still in
1072 * TIME_WAIT state, creating an ADDRINUSE error.
1074 laddr = inp->inp_laddr;
1075 lport = inp->inp_lport;
1076 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1077 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1078 if (error && oinp == NULL)
1082 inp->inp_laddr = laddr;
1085 /* Compute window scaling to request. */
1086 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1087 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1088 tp->request_r_scale++;
1091 tcpstat.tcps_connattempt++;
1092 tp->t_state = TCPS_SYN_SENT;
1093 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1094 tp->iss = tcp_new_isn(tp);
1095 tp->t_bw_rtseq = tp->iss;
1096 tcp_sendseqinit(tp);
1103 tcp6_connect(tp, nam, td)
1104 register struct tcpcb *tp;
1105 struct sockaddr *nam;
1108 struct inpcb *inp = tp->t_inpcb, *oinp;
1109 struct socket *so = inp->inp_socket;
1110 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1111 struct in6_addr *addr6;
1114 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1115 INP_LOCK_ASSERT(inp);
1117 if (inp->inp_lport == 0) {
1118 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1124 * Cannot simply call in_pcbconnect, because there might be an
1125 * earlier incarnation of this same connection still in
1126 * TIME_WAIT state, creating an ADDRINUSE error.
1127 * in6_pcbladdr() also handles scope zone IDs.
1129 error = in6_pcbladdr(inp, nam, &addr6);
1132 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1133 &sin6->sin6_addr, sin6->sin6_port,
1134 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1137 inp->inp_lport, 0, NULL);
1140 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1141 inp->in6p_laddr = *addr6;
1142 inp->in6p_faddr = sin6->sin6_addr;
1143 inp->inp_fport = sin6->sin6_port;
1144 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1145 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
1146 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
1147 inp->in6p_flowinfo |=
1148 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1151 /* Compute window scaling to request. */
1152 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1153 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1154 tp->request_r_scale++;
1157 tcpstat.tcps_connattempt++;
1158 tp->t_state = TCPS_SYN_SENT;
1159 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1160 tp->iss = tcp_new_isn(tp);
1161 tp->t_bw_rtseq = tp->iss;
1162 tcp_sendseqinit(tp);
1169 * Export TCP internal state information via a struct tcp_info, based on the
1170 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1171 * (TCP state machine, etc). We export all information using FreeBSD-native
1172 * constants -- for example, the numeric values for tcpi_state will differ
1176 tcp_fill_info(tp, ti)
1178 struct tcp_info *ti;
1181 INP_LOCK_ASSERT(tp->t_inpcb);
1182 bzero(ti, sizeof(*ti));
1184 ti->tcpi_state = tp->t_state;
1185 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1186 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1187 if (tp->sack_enable)
1188 ti->tcpi_options |= TCPI_OPT_SACK;
1189 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1190 ti->tcpi_options |= TCPI_OPT_WSCALE;
1191 ti->tcpi_snd_wscale = tp->snd_scale;
1192 ti->tcpi_rcv_wscale = tp->rcv_scale;
1194 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1195 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1198 * FreeBSD-specific extension fields for tcp_info.
1200 ti->tcpi_rcv_space = tp->rcv_wnd;
1201 ti->tcpi_snd_wnd = tp->snd_wnd;
1202 ti->tcpi_snd_bwnd = tp->snd_bwnd;
1206 * The new sockopt interface makes it possible for us to block in the
1207 * copyin/out step (if we take a page fault). Taking a page fault at
1208 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
1209 * use TSM, there probably isn't any need for this function to run at
1210 * splnet() any more. This needs more examination.)
1212 * XXXRW: The locking here is wrong; we may take a page fault while holding
1216 tcp_ctloutput(so, sopt)
1218 struct sockopt *sopt;
1220 int error, opt, optval;
1226 inp = sotoinpcb(so);
1227 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1229 if (sopt->sopt_level != IPPROTO_TCP) {
1232 if (INP_CHECK_SOCKAF(so, AF_INET6))
1233 error = ip6_ctloutput(so, sopt);
1236 error = ip_ctloutput(so, sopt);
1239 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
1243 tp = intotcpcb(inp);
1245 switch (sopt->sopt_dir) {
1247 switch (sopt->sopt_name) {
1248 #ifdef TCP_SIGNATURE
1250 error = sooptcopyin(sopt, &optval, sizeof optval,
1256 tp->t_flags |= TF_SIGNATURE;
1258 tp->t_flags &= ~TF_SIGNATURE;
1260 #endif /* TCP_SIGNATURE */
1263 error = sooptcopyin(sopt, &optval, sizeof optval,
1268 switch (sopt->sopt_name) {
1276 opt = 0; /* dead code to fool gcc */
1283 tp->t_flags &= ~opt;
1287 error = sooptcopyin(sopt, &optval, sizeof optval,
1293 tp->t_flags |= TF_NOPUSH;
1295 tp->t_flags &= ~TF_NOPUSH;
1296 error = tcp_output(tp);
1301 error = sooptcopyin(sopt, &optval, sizeof optval,
1306 if (optval > 0 && optval <= tp->t_maxseg &&
1307 optval + 40 >= tcp_minmss)
1308 tp->t_maxseg = optval;
1318 error = ENOPROTOOPT;
1324 switch (sopt->sopt_name) {
1325 #ifdef TCP_SIGNATURE
1327 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1328 error = sooptcopyout(sopt, &optval, sizeof optval);
1332 optval = tp->t_flags & TF_NODELAY;
1333 error = sooptcopyout(sopt, &optval, sizeof optval);
1336 optval = tp->t_maxseg;
1337 error = sooptcopyout(sopt, &optval, sizeof optval);
1340 optval = tp->t_flags & TF_NOOPT;
1341 error = sooptcopyout(sopt, &optval, sizeof optval);
1344 optval = tp->t_flags & TF_NOPUSH;
1345 error = sooptcopyout(sopt, &optval, sizeof optval);
1348 tcp_fill_info(tp, &ti);
1349 error = sooptcopyout(sopt, &ti, sizeof ti);
1352 error = ENOPROTOOPT;
1363 * tcp_sendspace and tcp_recvspace are the default send and receive window
1364 * sizes, respectively. These are obsolescent (this information should
1365 * be set by the route).
1367 u_long tcp_sendspace = 1024*32;
1368 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1369 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1370 u_long tcp_recvspace = 1024*64;
1371 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1372 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1375 * Attach TCP protocol to socket, allocating
1376 * internet protocol control block, tcp control block,
1377 * bufer space, and entering LISTEN state if to accept connections.
1383 register struct tcpcb *tp;
1387 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1390 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1392 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1393 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1397 error = in_pcballoc(so, &tcbinfo, "tcpinp");
1400 inp = sotoinpcb(so);
1403 inp->inp_vflag |= INP_IPV6;
1404 inp->in6p_hops = -1; /* use kernel default */
1408 inp->inp_vflag |= INP_IPV4;
1409 tp = tcp_newtcpcb(inp);
1425 tp->t_state = TCPS_CLOSED;
1430 * Initiate (or continue) disconnect.
1431 * If embryonic state, just send reset (once).
1432 * If in ``let data drain'' option and linger null, just drop.
1433 * Otherwise (hard), mark socket disconnecting and drop
1434 * current input data; switch states based on user close, and
1435 * send segment to peer (with FIN).
1439 register struct tcpcb *tp;
1441 struct inpcb *inp = tp->t_inpcb;
1442 struct socket *so = inp->inp_socket;
1444 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1445 INP_LOCK_ASSERT(inp);
1448 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1449 * socket is still open.
1451 if (tp->t_state < TCPS_ESTABLISHED) {
1454 ("tcp_disconnect: tcp_close() returned NULL"));
1455 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1456 tp = tcp_drop(tp, 0);
1458 ("tcp_disconnect: tcp_drop() returned NULL"));
1460 soisdisconnecting(so);
1461 sbflush(&so->so_rcv);
1463 if (!(inp->inp_vflag & INP_DROPPED))
1469 * User issued close, and wish to trail through shutdown states:
1470 * if never received SYN, just forget it. If got a SYN from peer,
1471 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1472 * If already got a FIN from peer, then almost done; go to LAST_ACK
1473 * state. In all other cases, have already sent FIN to peer (e.g.
1474 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1475 * for peer to send FIN or not respond to keep-alives, etc.
1476 * We can let the user exit from the close as soon as the FIN is acked.
1480 register struct tcpcb *tp;
1483 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1484 INP_LOCK_ASSERT(tp->t_inpcb);
1486 switch (tp->t_state) {
1490 tp->t_state = TCPS_CLOSED;
1493 * tcp_close() should never return NULL here as the socket is
1497 ("tcp_usrclosed: tcp_close() returned NULL"));
1501 case TCPS_SYN_RECEIVED:
1502 tp->t_flags |= TF_NEEDFIN;
1505 case TCPS_ESTABLISHED:
1506 tp->t_state = TCPS_FIN_WAIT_1;
1509 case TCPS_CLOSE_WAIT:
1510 tp->t_state = TCPS_LAST_ACK;
1513 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1514 soisdisconnected(tp->t_inpcb->inp_socket);
1515 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1516 if (tp->t_state == TCPS_FIN_WAIT_2)
1517 callout_reset(tp->tt_2msl, tcp_maxidle,
1518 tcp_timer_2msl, tp);