2 * Copyright (c) 2003-2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 1982, 1986, 1988, 1993
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
35 * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.51.2.17 2002/10/11 11:46:44 ume Exp $
36 * $DragonFly: src/sys/netinet/tcp_usrreq.c,v 1.24 2004/07/02 04:41:01 hsu Exp $
39 #include "opt_ipsec.h"
40 #include "opt_inet6.h"
41 #include "opt_tcpdebug.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/sysctl.h>
48 #include <sys/globaldata.h>
49 #include <sys/thread.h>
53 #include <sys/domain.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/protosw.h>
59 #include <sys/msgport2.h>
62 #include <net/netisr.h>
63 #include <net/route.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
68 #include <netinet/ip6.h>
70 #include <netinet/in_pcb.h>
72 #include <netinet6/in6_pcb.h>
74 #include <netinet/in_var.h>
75 #include <netinet/ip_var.h>
77 #include <netinet6/ip6_var.h>
79 #include <netinet/tcp.h>
80 #include <netinet/tcp_fsm.h>
81 #include <netinet/tcp_seq.h>
82 #include <netinet/tcp_timer.h>
83 #include <netinet/tcp_var.h>
84 #include <netinet/tcpip.h>
86 #include <netinet/tcp_debug.h>
90 #include <netinet6/ipsec.h>
94 * TCP protocol interface to socket abstraction.
96 extern char *tcpstates[]; /* XXX ??? */
98 static int tcp_attach (struct socket *, struct pru_attach_info *);
99 static int tcp_connect (struct tcpcb *, struct sockaddr *,
102 static int tcp6_connect (struct tcpcb *, struct sockaddr *,
105 static struct tcpcb *
106 tcp_disconnect (struct tcpcb *);
107 static struct tcpcb *
108 tcp_usrclosed (struct tcpcb *);
111 #define TCPDEBUG0 int ostate = 0
112 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
113 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
114 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
118 #define TCPDEBUG2(req)
122 * TCP attaches to socket via pru_attach(), reserving space,
123 * and an internet control block.
126 tcp_usr_attach(struct socket *so, int proto, struct pru_attach_info *ai)
130 struct inpcb *inp = sotoinpcb(so);
131 struct tcpcb *tp = 0;
140 error = tcp_attach(so, ai);
144 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
145 so->so_linger = TCP_LINGERTIME;
148 TCPDEBUG2(PRU_ATTACH);
154 * pru_detach() detaches the TCP protocol from the socket.
155 * If the protocol state is non-embryonic, then can't
156 * do this directly: have to initiate a pru_disconnect(),
157 * which may finish later; embryonic TCB's can just
161 tcp_usr_detach(struct socket *so)
165 struct inpcb *inp = sotoinpcb(so);
171 return EINVAL; /* XXX */
175 tp = tcp_disconnect(tp);
177 TCPDEBUG2(PRU_DETACH);
182 #define COMMON_START() TCPDEBUG0; \
188 tp = intotcpcb(inp); \
192 #define COMMON_END(req) out: TCPDEBUG2(req); splx(s); return error; goto out
196 * Give the socket an address.
199 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
203 struct inpcb *inp = sotoinpcb(so);
205 struct sockaddr_in *sinp;
210 * Must check for multicast addresses and disallow binding
213 sinp = (struct sockaddr_in *)nam;
214 if (sinp->sin_family == AF_INET &&
215 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
216 error = EAFNOSUPPORT;
219 error = in_pcbbind(inp, nam, td);
222 COMMON_END(PRU_BIND);
228 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
232 struct inpcb *inp = sotoinpcb(so);
234 struct sockaddr_in6 *sin6p;
239 * Must check for multicast addresses and disallow binding
242 sin6p = (struct sockaddr_in6 *)nam;
243 if (sin6p->sin6_family == AF_INET6 &&
244 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
245 error = EAFNOSUPPORT;
248 inp->inp_vflag &= ~INP_IPV4;
249 inp->inp_vflag |= INP_IPV6;
250 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
251 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
252 inp->inp_vflag |= INP_IPV4;
253 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
254 struct sockaddr_in sin;
256 in6_sin6_2_sin(&sin, sin6p);
257 inp->inp_vflag |= INP_IPV4;
258 inp->inp_vflag &= ~INP_IPV6;
259 error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
263 error = in6_pcbbind(inp, nam, td);
266 COMMON_END(PRU_BIND);
271 struct netmsg_inswildcard {
272 struct lwkt_msg nm_lmsg;
273 struct inpcb *nm_inp;
274 struct inpcbinfo *nm_pcbinfo;
278 in_pcbinswildcardhash_handler(struct lwkt_msg *msg0)
280 struct netmsg_inswildcard *msg = (struct netmsg_inswildcard *)msg0;
282 in_pcbinswildcardhash_oncpu(msg->nm_inp, msg->nm_pcbinfo);
283 lwkt_replymsg(&msg->nm_lmsg, 0);
289 * Prepare to accept connections.
292 tcp_usr_listen(struct socket *so, struct thread *td)
296 struct inpcb *inp = sotoinpcb(so);
303 if (inp->inp_lport == 0) {
304 error = in_pcbbind(inp, NULL, td);
309 tp->t_state = TCPS_LISTEN;
311 for (cpu = 0; cpu < ncpus2; cpu++) {
312 struct netmsg_inswildcard *msg;
314 if (cpu == mycpu->gd_cpuid) {
315 in_pcbinswildcardhash_oncpu(inp, &tcbinfo[cpu]);
319 msg = malloc(sizeof(struct netmsg_inswildcard), M_LWKTMSG,
321 lwkt_initmsg(&msg->nm_lmsg, &netisr_afree_rport, 0,
322 lwkt_cmd_func(in_pcbinswildcardhash_handler),
325 msg->nm_pcbinfo = &tcbinfo[cpu];
326 lwkt_sendmsg(tcp_cport(cpu), &msg->nm_lmsg);
328 inp->inp_flags |= INP_WILDCARD_MP;
330 in_pcbinswildcardhash(inp);
332 COMMON_END(PRU_LISTEN);
337 tcp6_usr_listen(struct socket *so, struct thread *td)
341 struct inpcb *inp = sotoinpcb(so);
345 if (inp->inp_lport == 0) {
346 inp->inp_vflag &= ~INP_IPV4;
347 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
348 inp->inp_vflag |= INP_IPV4;
349 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
352 tp->t_state = TCPS_LISTEN;
353 in_pcbinswildcardhash(inp);
354 COMMON_END(PRU_LISTEN);
359 * Initiate connection to peer.
360 * Create a template for use in transmissions on this connection.
361 * Enter SYN_SENT state, and mark socket as connecting.
362 * Start keep-alive timer, and seed output sequence space.
363 * Send initial segment on connection.
366 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
370 struct inpcb *inp = sotoinpcb(so);
372 struct sockaddr_in *sinp;
377 * Must disallow TCP ``connections'' to multicast addresses.
379 sinp = (struct sockaddr_in *)nam;
380 if (sinp->sin_family == AF_INET
381 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
382 error = EAFNOSUPPORT;
386 prison_remote_ip(td, 0, &sinp->sin_addr.s_addr);
388 if ((error = tcp_connect(tp, nam, td)) != 0)
390 error = tcp_output(tp);
391 COMMON_END(PRU_CONNECT);
396 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
400 struct inpcb *inp = sotoinpcb(so);
402 struct sockaddr_in6 *sin6p;
407 * Must disallow TCP ``connections'' to multicast addresses.
409 sin6p = (struct sockaddr_in6 *)nam;
410 if (sin6p->sin6_family == AF_INET6
411 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
412 error = EAFNOSUPPORT;
416 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
417 struct sockaddr_in sin;
419 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
424 in6_sin6_2_sin(&sin, sin6p);
425 inp->inp_vflag |= INP_IPV4;
426 inp->inp_vflag &= ~INP_IPV6;
427 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
429 error = tcp_output(tp);
432 inp->inp_vflag &= ~INP_IPV4;
433 inp->inp_vflag |= INP_IPV6;
434 inp->inp_inc.inc_isipv6 = 1;
435 if ((error = tcp6_connect(tp, nam, td)) != 0)
437 error = tcp_output(tp);
438 COMMON_END(PRU_CONNECT);
443 * Initiate disconnect from peer.
444 * If connection never passed embryonic stage, just drop;
445 * else if don't need to let data drain, then can just drop anyways,
446 * else have to begin TCP shutdown process: mark socket disconnecting,
447 * drain unread data, state switch to reflect user close, and
448 * send segment (e.g. FIN) to peer. Socket will be really disconnected
449 * when peer sends FIN and acks ours.
451 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
454 tcp_usr_disconnect(struct socket *so)
458 struct inpcb *inp = sotoinpcb(so);
462 tp = tcp_disconnect(tp);
463 COMMON_END(PRU_DISCONNECT);
467 * Accept a connection. Essentially all the work is
468 * done at higher levels; just return the address
469 * of the peer, storing through addr.
472 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
476 struct inpcb *inp = sotoinpcb(so);
477 struct tcpcb *tp = NULL;
480 if (so->so_state & SS_ISDISCONNECTED) {
481 error = ECONNABORTED;
490 in_setpeeraddr(so, nam);
491 COMMON_END(PRU_ACCEPT);
496 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
500 struct inpcb *inp = sotoinpcb(so);
501 struct tcpcb *tp = NULL;
504 if (so->so_state & SS_ISDISCONNECTED) {
505 error = ECONNABORTED;
514 in6_mapped_peeraddr(so, nam);
515 COMMON_END(PRU_ACCEPT);
519 * Mark the connection as being incapable of further output.
522 tcp_usr_shutdown(struct socket *so)
526 struct inpcb *inp = sotoinpcb(so);
531 tp = tcp_usrclosed(tp);
533 error = tcp_output(tp);
534 COMMON_END(PRU_SHUTDOWN);
538 * After a receive, possibly send window update to peer.
541 tcp_usr_rcvd(struct socket *so, int flags)
545 struct inpcb *inp = sotoinpcb(so);
550 COMMON_END(PRU_RCVD);
554 * Do a send by putting data in output queue and updating urgent
555 * marker if URG set. Possibly send more data. Unlike the other
556 * pru_*() routines, the mbuf chains are our responsibility. We
557 * must either enqueue them or free them. The other pru_* routines
558 * generally are caller-frees.
561 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
562 struct sockaddr *nam, struct mbuf *control, struct thread *td)
566 struct inpcb *inp = sotoinpcb(so);
575 * OOPS! we lost a race, the TCP session got reset after
576 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
577 * network interrupt in the non-splnet() section of sosend().
583 error = ECONNRESET; /* XXX EPIPE? */
589 isipv6 = nam && nam->sa_family == AF_INET6;
594 /* TCP doesn't do control messages (rights, creds, etc) */
595 if (control->m_len) {
602 m_freem(control); /* empty control, just free it */
604 if(!(flags & PRUS_OOB)) {
605 sbappend(&so->so_snd, m);
606 if (nam && tp->t_state < TCPS_SYN_SENT) {
608 * Do implied connect if not yet connected,
609 * initialize window to default value, and
610 * initialize maxseg/maxopd using peer's cached
615 error = tcp6_connect(tp, nam, td);
618 error = tcp_connect(tp, nam, td);
621 tp->snd_wnd = TTCP_CLIENT_SND_WND;
625 if (flags & PRUS_EOF) {
627 * Close the send side of the connection after
631 tp = tcp_usrclosed(tp);
634 if (flags & PRUS_MORETOCOME)
635 tp->t_flags |= TF_MORETOCOME;
636 error = tcp_output(tp);
637 if (flags & PRUS_MORETOCOME)
638 tp->t_flags &= ~TF_MORETOCOME;
641 if (sbspace(&so->so_snd) < -512) {
647 * According to RFC961 (Assigned Protocols),
648 * the urgent pointer points to the last octet
649 * of urgent data. We continue, however,
650 * to consider it to indicate the first octet
651 * of data past the urgent section.
652 * Otherwise, snd_up should be one lower.
654 sbappend(&so->so_snd, m);
655 if (nam && tp->t_state < TCPS_SYN_SENT) {
657 * Do implied connect if not yet connected,
658 * initialize window to default value, and
659 * initialize maxseg/maxopd using peer's cached
664 error = tcp6_connect(tp, nam, td);
667 error = tcp_connect(tp, nam, td);
670 tp->snd_wnd = TTCP_CLIENT_SND_WND;
673 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
674 tp->t_flags |= TF_FORCE;
675 error = tcp_output(tp);
676 tp->t_flags &= ~TF_FORCE;
678 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
679 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
686 tcp_usr_abort(struct socket *so)
690 struct inpcb *inp = sotoinpcb(so);
694 tp = tcp_drop(tp, ECONNABORTED);
695 COMMON_END(PRU_ABORT);
699 * Receive out-of-band data.
702 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
706 struct inpcb *inp = sotoinpcb(so);
710 if ((so->so_oobmark == 0 &&
711 (so->so_state & SS_RCVATMARK) == 0) ||
712 so->so_options & SO_OOBINLINE ||
713 tp->t_oobflags & TCPOOB_HADDATA) {
717 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
722 *mtod(m, caddr_t) = tp->t_iobc;
723 if ((flags & MSG_PEEK) == 0)
724 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
725 COMMON_END(PRU_RCVOOB);
728 /* xxx - should be const */
729 struct pr_usrreqs tcp_usrreqs = {
730 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
731 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
732 tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
733 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
734 in_setsockaddr, sosend, soreceive, sopoll
738 struct pr_usrreqs tcp6_usrreqs = {
739 tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
740 tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
741 tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
742 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
743 in6_mapped_sockaddr, sosend, soreceive, sopoll
748 tcp_connect_oncpu(struct tcpcb *tp, struct sockaddr_in *sin,
749 struct sockaddr_in *if_sin)
751 struct inpcb *inp = tp->t_inpcb, *oinp;
752 struct socket *so = inp->inp_socket;
754 struct rmxp_tao *taop;
755 struct rmxp_tao tao_noncached;
757 oinp = in_pcblookup_hash(&tcbinfo[mycpu->gd_cpuid],
758 sin->sin_addr, sin->sin_port,
759 inp->inp_laddr.s_addr != INADDR_ANY ?
760 inp->inp_laddr : if_sin->sin_addr,
761 inp->inp_lport, 0, NULL);
763 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
764 otp->t_state == TCPS_TIME_WAIT &&
765 (ticks - otp->t_starttime) < tcp_msl &&
766 (otp->t_flags & TF_RCVD_CC))
767 (void) tcp_close(otp);
771 if (inp->inp_laddr.s_addr == INADDR_ANY)
772 inp->inp_laddr = if_sin->sin_addr;
773 inp->inp_faddr = sin->sin_addr;
774 inp->inp_fport = sin->sin_port;
775 inp->inp_cpcbinfo = &tcbinfo[mycpu->gd_cpuid];
776 in_pcbinsconnhash(inp);
778 /* Compute window scaling to request. */
779 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
780 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
781 tp->request_r_scale++;
784 tcpstat.tcps_connattempt++;
785 tp->t_state = TCPS_SYN_SENT;
786 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
787 tp->iss = tcp_new_isn(tp);
788 tp->t_bw_rtseq = tp->iss;
792 * Generate a CC value for this connection and
793 * check whether CC or CCnew should be used.
795 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
796 taop = &tao_noncached;
797 bzero(taop, sizeof(*taop));
800 tp->cc_send = CC_INC(tcp_ccgen);
801 if (taop->tao_ccsent != 0 &&
802 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
803 taop->tao_ccsent = tp->cc_send;
805 taop->tao_ccsent = 0;
806 tp->t_flags |= TF_SENDCCNEW;
814 struct netmsg_tcp_connect {
815 struct lwkt_msg nm_lmsg;
817 struct sockaddr_in *nm_sin;
818 struct sockaddr_in *nm_ifsin;
822 tcp_connect_handler(lwkt_msg_t lmsg)
824 struct netmsg_tcp_connect *msg = (void *)lmsg;
827 error = tcp_connect_oncpu(msg->nm_tp, msg->nm_sin, msg->nm_ifsin);
828 lwkt_replymsg(lmsg, error);
835 * Common subroutine to open a TCP connection to remote host specified
836 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
837 * port number if needed. Call in_pcbladdr to do the routing and to choose
838 * a local host address (interface). If there is an existing incarnation
839 * of the same connection in TIME-WAIT state and if the remote host was
840 * sending CC options and if the connection duration was < MSL, then
841 * truncate the previous TIME-WAIT state and proceed.
842 * Initialize connection parameters and enter SYN-SENT state.
845 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
847 struct inpcb *inp = tp->t_inpcb;
848 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
849 struct sockaddr_in *if_sin;
855 if (inp->inp_lport == 0) {
856 error = in_pcbbind(inp, (struct sockaddr *)NULL, td);
862 * Cannot simply call in_pcbconnect, because there might be an
863 * earlier incarnation of this same connection still in
864 * TIME_WAIT state, creating an ADDRINUSE error.
866 error = in_pcbladdr(inp, nam, &if_sin);
871 port = tcp_addrport(sin->sin_addr.s_addr, sin->sin_port,
872 inp->inp_laddr.s_addr ?
873 inp->inp_laddr.s_addr : if_sin->sin_addr.s_addr,
876 if (port->mp_td != curthread) {
877 struct netmsg_tcp_connect msg;
879 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
880 lwkt_cmd_func(tcp_connect_handler), lwkt_cmd_op_none);
883 msg.nm_ifsin = if_sin;
884 error = lwkt_domsg(port, &msg.nm_lmsg);
887 error = tcp_connect_oncpu(tp, sin, if_sin);
894 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
896 struct inpcb *inp = tp->t_inpcb, *oinp;
897 struct socket *so = inp->inp_socket;
899 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
900 struct in6_addr *addr6;
901 struct rmxp_tao *taop;
902 struct rmxp_tao tao_noncached;
905 if (inp->inp_lport == 0) {
906 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
912 * Cannot simply call in_pcbconnect, because there might be an
913 * earlier incarnation of this same connection still in
914 * TIME_WAIT state, creating an ADDRINUSE error.
916 error = in6_pcbladdr(inp, nam, &addr6);
919 oinp = in6_pcblookup_hash(inp->inp_cpcbinfo,
920 &sin6->sin6_addr, sin6->sin6_port,
921 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
922 addr6 : &inp->in6p_laddr,
923 inp->inp_lport, 0, NULL);
925 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
926 otp->t_state == TCPS_TIME_WAIT &&
927 (ticks - otp->t_starttime) < tcp_msl &&
928 (otp->t_flags & TF_RCVD_CC))
929 otp = tcp_close(otp);
933 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
934 inp->in6p_laddr = *addr6;
935 inp->in6p_faddr = sin6->sin6_addr;
936 inp->inp_fport = sin6->sin6_port;
937 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
938 inp->in6p_flowinfo = sin6->sin6_flowinfo;
939 in_pcbinsconnhash(inp);
941 /* Compute window scaling to request. */
942 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
943 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
944 tp->request_r_scale++;
947 tcpstat.tcps_connattempt++;
948 tp->t_state = TCPS_SYN_SENT;
949 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
950 tp->iss = tcp_new_isn(tp);
951 tp->t_bw_rtseq = tp->iss;
955 * Generate a CC value for this connection and
956 * check whether CC or CCnew should be used.
958 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
959 taop = &tao_noncached;
960 bzero(taop, sizeof(*taop));
963 tp->cc_send = CC_INC(tcp_ccgen);
964 if (taop->tao_ccsent != 0 &&
965 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
966 taop->tao_ccsent = tp->cc_send;
968 taop->tao_ccsent = 0;
969 tp->t_flags |= TF_SENDCCNEW;
977 * The new sockopt interface makes it possible for us to block in the
978 * copyin/out step (if we take a page fault). Taking a page fault at
979 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
980 * use TSM, there probably isn't any need for this function to run at
981 * splnet() any more. This needs more examination.)
984 tcp_ctloutput(so, sopt)
986 struct sockopt *sopt;
988 int error, opt, optval, s;
993 s = splnet(); /* XXX */
999 if (sopt->sopt_level != IPPROTO_TCP) {
1001 if (INP_CHECK_SOCKAF(so, AF_INET6))
1002 error = ip6_ctloutput(so, sopt);
1005 error = ip_ctloutput(so, sopt);
1009 tp = intotcpcb(inp);
1011 switch (sopt->sopt_dir) {
1013 switch (sopt->sopt_name) {
1016 error = sooptcopyin(sopt, &optval, sizeof optval,
1021 switch (sopt->sopt_name) {
1029 opt = 0; /* dead code to fool gcc */
1036 tp->t_flags &= ~opt;
1040 error = sooptcopyin(sopt, &optval, sizeof optval,
1046 tp->t_flags |= TF_NOPUSH;
1048 tp->t_flags &= ~TF_NOPUSH;
1049 error = tcp_output(tp);
1054 error = sooptcopyin(sopt, &optval, sizeof optval,
1059 if (optval > 0 && optval <= tp->t_maxseg)
1060 tp->t_maxseg = optval;
1066 error = ENOPROTOOPT;
1072 switch (sopt->sopt_name) {
1074 optval = tp->t_flags & TF_NODELAY;
1077 optval = tp->t_maxseg;
1080 optval = tp->t_flags & TF_NOOPT;
1083 optval = tp->t_flags & TF_NOPUSH;
1086 error = ENOPROTOOPT;
1090 error = sooptcopyout(sopt, &optval, sizeof optval);
1098 * tcp_sendspace and tcp_recvspace are the default send and receive window
1099 * sizes, respectively. These are obsolescent (this information should
1100 * be set by the route).
1102 u_long tcp_sendspace = 1024*32;
1103 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1104 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1105 u_long tcp_recvspace = 57344; /* largest multiple of PAGE_SIZE < 64k */
1106 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1107 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1110 * Attach TCP protocol to socket, allocating
1111 * internet protocol control block, tcp control block,
1112 * bufer space, and entering LISTEN state if to accept connections.
1115 tcp_attach(struct socket *so, struct pru_attach_info *ai)
1122 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1125 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1126 error = soreserve(so, tcp_sendspace, tcp_recvspace,
1131 cpu = mycpu->gd_cpuid;
1132 error = in_pcballoc(so, &tcbinfo[cpu]);
1135 inp = sotoinpcb(so);
1138 inp->inp_vflag |= INP_IPV6;
1139 inp->in6p_hops = -1; /* use kernel default */
1143 inp->inp_vflag |= INP_IPV4;
1144 tp = tcp_newtcpcb(inp);
1146 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1148 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1155 so->so_state |= nofd;
1158 tp->t_state = TCPS_CLOSED;
1163 * Initiate (or continue) disconnect.
1164 * If embryonic state, just send reset (once).
1165 * If in ``let data drain'' option and linger null, just drop.
1166 * Otherwise (hard), mark socket disconnecting and drop
1167 * current input data; switch states based on user close, and
1168 * send segment to peer (with FIN).
1170 static struct tcpcb *
1174 struct socket *so = tp->t_inpcb->inp_socket;
1176 if (tp->t_state < TCPS_ESTABLISHED)
1178 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1179 tp = tcp_drop(tp, 0);
1181 soisdisconnecting(so);
1182 sbflush(&so->so_rcv);
1183 tp = tcp_usrclosed(tp);
1185 (void) tcp_output(tp);
1191 * User issued close, and wish to trail through shutdown states:
1192 * if never received SYN, just forget it. If got a SYN from peer,
1193 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1194 * If already got a FIN from peer, then almost done; go to LAST_ACK
1195 * state. In all other cases, have already sent FIN to peer (e.g.
1196 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1197 * for peer to send FIN or not respond to keep-alives, etc.
1198 * We can let the user exit from the close as soon as the FIN is acked.
1200 static struct tcpcb *
1205 switch (tp->t_state) {
1209 tp->t_state = TCPS_CLOSED;
1214 case TCPS_SYN_RECEIVED:
1215 tp->t_flags |= TF_NEEDFIN;
1218 case TCPS_ESTABLISHED:
1219 tp->t_state = TCPS_FIN_WAIT_1;
1222 case TCPS_CLOSE_WAIT:
1223 tp->t_state = TCPS_LAST_ACK;
1226 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1227 soisdisconnected(tp->t_inpcb->inp_socket);
1228 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1229 if (tp->t_state == TCPS_FIN_WAIT_2)
1230 callout_reset(tp->tt_2msl, tcp_maxidle,
1231 tcp_timer_2msl, tp);