1/* 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
| 1/* 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
|
34 * $FreeBSD: head/sys/netinet/tcp_input.c 121285 2003-10-20 16:19:01Z ume $
| 34 * $FreeBSD: head/sys/netinet/tcp_input.c 121628 2003-10-28 05:47:40Z sam $
|
35 */ 36 37#include "opt_ipfw.h" /* for ipfw_fwd */ 38#include "opt_inet6.h" 39#include "opt_ipsec.h" 40#include "opt_mac.h" 41#include "opt_tcpdebug.h" 42#include "opt_tcp_input.h" 43 44#include <sys/param.h> 45#include <sys/kernel.h> 46#include <sys/mac.h> 47#include <sys/malloc.h> 48#include <sys/mbuf.h> 49#include <sys/proc.h> /* for proc0 declaration */ 50#include <sys/protosw.h> 51#include <sys/signalvar.h> 52#include <sys/socket.h> 53#include <sys/socketvar.h> 54#include <sys/sysctl.h> 55#include <sys/syslog.h> 56#include <sys/systm.h> 57 58#include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 59 60#include <net/if.h> 61#include <net/route.h> 62 63#include <netinet/in.h> 64#include <netinet/in_pcb.h> 65#include <netinet/in_systm.h> 66#include <netinet/in_var.h> 67#include <netinet/ip.h> 68#include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */ 69#include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 70#include <netinet/ip_var.h> 71#include <netinet/ip6.h> 72#include <netinet/icmp6.h> 73#include <netinet6/in6_pcb.h> 74#include <netinet6/ip6_var.h> 75#include <netinet6/nd6.h> 76#include <netinet/tcp.h> 77#include <netinet/tcp_fsm.h> 78#include <netinet/tcp_seq.h> 79#include <netinet/tcp_timer.h> 80#include <netinet/tcp_var.h> 81#include <netinet6/tcp6_var.h> 82#include <netinet/tcpip.h> 83#ifdef TCPDEBUG 84#include <netinet/tcp_debug.h> 85#endif /* TCPDEBUG */ 86 87#ifdef FAST_IPSEC 88#include <netipsec/ipsec.h> 89#include <netipsec/ipsec6.h> 90#endif /*FAST_IPSEC*/ 91 92#ifdef IPSEC 93#include <netinet6/ipsec.h> 94#include <netinet6/ipsec6.h> 95#include <netkey/key.h> 96#endif /*IPSEC*/ 97 98#include <machine/in_cksum.h> 99 100MALLOC_DEFINE(M_TSEGQ, "tseg_qent", "TCP segment queue entry"); 101 102static const int tcprexmtthresh = 3; 103tcp_cc tcp_ccgen; 104 105struct tcpstat tcpstat; 106SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW, 107 &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 108 109static int log_in_vain = 0; 110SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 111 &log_in_vain, 0, "Log all incoming TCP connections"); 112 113static int blackhole = 0; 114SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 115 &blackhole, 0, "Do not send RST when dropping refused connections"); 116 117int tcp_delack_enabled = 1; 118SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 119 &tcp_delack_enabled, 0, 120 "Delay ACK to try and piggyback it onto a data packet"); 121 122#ifdef TCP_DROP_SYNFIN 123static int drop_synfin = 0; 124SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 125 &drop_synfin, 0, "Drop TCP packets with SYN+FIN set"); 126#endif 127 128static int tcp_do_rfc3042 = 0; 129SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 130 &tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)"); 131 132static int tcp_do_rfc3390 = 0; 133SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 134 &tcp_do_rfc3390, 0, 135 "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 136 137struct inpcbhead tcb; 138#define tcb6 tcb /* for KAME src sync over BSD*'s */ 139struct inpcbinfo tcbinfo; 140struct mtx *tcbinfo_mtx; 141 142static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 143static void tcp_pulloutofband(struct socket *, 144 struct tcphdr *, struct mbuf *, int); 145static int tcp_reass(struct tcpcb *, struct tcphdr *, int *, 146 struct mbuf *); 147static void tcp_xmit_timer(struct tcpcb *, int); 148static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 149static int tcp_timewait(struct tcptw *, struct tcpopt *, 150 struct tcphdr *, struct mbuf *, int); 151 152/* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 153#ifdef INET6 154#define ND6_HINT(tp) \ 155do { \ 156 if ((tp) && (tp)->t_inpcb && \ 157 ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0 && \ 158 (tp)->t_inpcb->in6p_route.ro_rt) \ 159 nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \ 160} while (0) 161#else 162#define ND6_HINT(tp) 163#endif 164 165/* 166 * Indicate whether this ack should be delayed. We can delay the ack if 167 * - there is no delayed ack timer in progress and 168 * - our last ack wasn't a 0-sized window. We never want to delay 169 * the ack that opens up a 0-sized window and 170 * - delayed acks are enabled or 171 * - this is a half-synchronized T/TCP connection. 172 */ 173#define DELAY_ACK(tp) \ 174 ((!callout_active(tp->tt_delack) && \ 175 (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 176 (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 177 178static int 179tcp_reass(tp, th, tlenp, m) 180 register struct tcpcb *tp; 181 register struct tcphdr *th; 182 int *tlenp; 183 struct mbuf *m; 184{ 185 struct tseg_qent *q; 186 struct tseg_qent *p = NULL; 187 struct tseg_qent *nq; 188 struct tseg_qent *te; 189 struct socket *so = tp->t_inpcb->inp_socket; 190 int flags; 191 192 /* 193 * Call with th==0 after become established to 194 * force pre-ESTABLISHED data up to user socket. 195 */ 196 if (th == 0) 197 goto present; 198 199 /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */ 200 MALLOC(te, struct tseg_qent *, sizeof (struct tseg_qent), M_TSEGQ, 201 M_NOWAIT); 202 if (te == NULL) { 203 tcpstat.tcps_rcvmemdrop++; 204 m_freem(m); 205 return (0); 206 } 207 208 /* 209 * Find a segment which begins after this one does. 210 */ 211 LIST_FOREACH(q, &tp->t_segq, tqe_q) { 212 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) 213 break; 214 p = q; 215 } 216 217 /* 218 * If there is a preceding segment, it may provide some of 219 * our data already. If so, drop the data from the incoming 220 * segment. If it provides all of our data, drop us. 221 */ 222 if (p != NULL) { 223 register int i; 224 /* conversion to int (in i) handles seq wraparound */ 225 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; 226 if (i > 0) { 227 if (i >= *tlenp) { 228 tcpstat.tcps_rcvduppack++; 229 tcpstat.tcps_rcvdupbyte += *tlenp; 230 m_freem(m); 231 FREE(te, M_TSEGQ); 232 /* 233 * Try to present any queued data 234 * at the left window edge to the user. 235 * This is needed after the 3-WHS 236 * completes. 237 */ 238 goto present; /* ??? */ 239 } 240 m_adj(m, i); 241 *tlenp -= i; 242 th->th_seq += i; 243 } 244 } 245 tcpstat.tcps_rcvoopack++; 246 tcpstat.tcps_rcvoobyte += *tlenp; 247 248 /* 249 * While we overlap succeeding segments trim them or, 250 * if they are completely covered, dequeue them. 251 */ 252 while (q) { 253 register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; 254 if (i <= 0) 255 break; 256 if (i < q->tqe_len) { 257 q->tqe_th->th_seq += i; 258 q->tqe_len -= i; 259 m_adj(q->tqe_m, i); 260 break; 261 } 262 263 nq = LIST_NEXT(q, tqe_q); 264 LIST_REMOVE(q, tqe_q); 265 m_freem(q->tqe_m); 266 FREE(q, M_TSEGQ); 267 q = nq; 268 } 269 270 /* Insert the new segment queue entry into place. */ 271 te->tqe_m = m; 272 te->tqe_th = th; 273 te->tqe_len = *tlenp; 274 275 if (p == NULL) { 276 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); 277 } else { 278 LIST_INSERT_AFTER(p, te, tqe_q); 279 } 280 281present: 282 /* 283 * Present data to user, advancing rcv_nxt through 284 * completed sequence space. 285 */ 286 if (!TCPS_HAVEESTABLISHED(tp->t_state)) 287 return (0); 288 q = LIST_FIRST(&tp->t_segq); 289 if (!q || q->tqe_th->th_seq != tp->rcv_nxt) 290 return (0); 291 do { 292 tp->rcv_nxt += q->tqe_len; 293 flags = q->tqe_th->th_flags & TH_FIN; 294 nq = LIST_NEXT(q, tqe_q); 295 LIST_REMOVE(q, tqe_q); 296 if (so->so_state & SS_CANTRCVMORE) 297 m_freem(q->tqe_m); 298 else
| 35 */ 36 37#include "opt_ipfw.h" /* for ipfw_fwd */ 38#include "opt_inet6.h" 39#include "opt_ipsec.h" 40#include "opt_mac.h" 41#include "opt_tcpdebug.h" 42#include "opt_tcp_input.h" 43 44#include <sys/param.h> 45#include <sys/kernel.h> 46#include <sys/mac.h> 47#include <sys/malloc.h> 48#include <sys/mbuf.h> 49#include <sys/proc.h> /* for proc0 declaration */ 50#include <sys/protosw.h> 51#include <sys/signalvar.h> 52#include <sys/socket.h> 53#include <sys/socketvar.h> 54#include <sys/sysctl.h> 55#include <sys/syslog.h> 56#include <sys/systm.h> 57 58#include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 59 60#include <net/if.h> 61#include <net/route.h> 62 63#include <netinet/in.h> 64#include <netinet/in_pcb.h> 65#include <netinet/in_systm.h> 66#include <netinet/in_var.h> 67#include <netinet/ip.h> 68#include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */ 69#include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 70#include <netinet/ip_var.h> 71#include <netinet/ip6.h> 72#include <netinet/icmp6.h> 73#include <netinet6/in6_pcb.h> 74#include <netinet6/ip6_var.h> 75#include <netinet6/nd6.h> 76#include <netinet/tcp.h> 77#include <netinet/tcp_fsm.h> 78#include <netinet/tcp_seq.h> 79#include <netinet/tcp_timer.h> 80#include <netinet/tcp_var.h> 81#include <netinet6/tcp6_var.h> 82#include <netinet/tcpip.h> 83#ifdef TCPDEBUG 84#include <netinet/tcp_debug.h> 85#endif /* TCPDEBUG */ 86 87#ifdef FAST_IPSEC 88#include <netipsec/ipsec.h> 89#include <netipsec/ipsec6.h> 90#endif /*FAST_IPSEC*/ 91 92#ifdef IPSEC 93#include <netinet6/ipsec.h> 94#include <netinet6/ipsec6.h> 95#include <netkey/key.h> 96#endif /*IPSEC*/ 97 98#include <machine/in_cksum.h> 99 100MALLOC_DEFINE(M_TSEGQ, "tseg_qent", "TCP segment queue entry"); 101 102static const int tcprexmtthresh = 3; 103tcp_cc tcp_ccgen; 104 105struct tcpstat tcpstat; 106SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW, 107 &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 108 109static int log_in_vain = 0; 110SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 111 &log_in_vain, 0, "Log all incoming TCP connections"); 112 113static int blackhole = 0; 114SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 115 &blackhole, 0, "Do not send RST when dropping refused connections"); 116 117int tcp_delack_enabled = 1; 118SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 119 &tcp_delack_enabled, 0, 120 "Delay ACK to try and piggyback it onto a data packet"); 121 122#ifdef TCP_DROP_SYNFIN 123static int drop_synfin = 0; 124SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 125 &drop_synfin, 0, "Drop TCP packets with SYN+FIN set"); 126#endif 127 128static int tcp_do_rfc3042 = 0; 129SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 130 &tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)"); 131 132static int tcp_do_rfc3390 = 0; 133SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 134 &tcp_do_rfc3390, 0, 135 "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 136 137struct inpcbhead tcb; 138#define tcb6 tcb /* for KAME src sync over BSD*'s */ 139struct inpcbinfo tcbinfo; 140struct mtx *tcbinfo_mtx; 141 142static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 143static void tcp_pulloutofband(struct socket *, 144 struct tcphdr *, struct mbuf *, int); 145static int tcp_reass(struct tcpcb *, struct tcphdr *, int *, 146 struct mbuf *); 147static void tcp_xmit_timer(struct tcpcb *, int); 148static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 149static int tcp_timewait(struct tcptw *, struct tcpopt *, 150 struct tcphdr *, struct mbuf *, int); 151 152/* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 153#ifdef INET6 154#define ND6_HINT(tp) \ 155do { \ 156 if ((tp) && (tp)->t_inpcb && \ 157 ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0 && \ 158 (tp)->t_inpcb->in6p_route.ro_rt) \ 159 nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \ 160} while (0) 161#else 162#define ND6_HINT(tp) 163#endif 164 165/* 166 * Indicate whether this ack should be delayed. We can delay the ack if 167 * - there is no delayed ack timer in progress and 168 * - our last ack wasn't a 0-sized window. We never want to delay 169 * the ack that opens up a 0-sized window and 170 * - delayed acks are enabled or 171 * - this is a half-synchronized T/TCP connection. 172 */ 173#define DELAY_ACK(tp) \ 174 ((!callout_active(tp->tt_delack) && \ 175 (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 176 (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 177 178static int 179tcp_reass(tp, th, tlenp, m) 180 register struct tcpcb *tp; 181 register struct tcphdr *th; 182 int *tlenp; 183 struct mbuf *m; 184{ 185 struct tseg_qent *q; 186 struct tseg_qent *p = NULL; 187 struct tseg_qent *nq; 188 struct tseg_qent *te; 189 struct socket *so = tp->t_inpcb->inp_socket; 190 int flags; 191 192 /* 193 * Call with th==0 after become established to 194 * force pre-ESTABLISHED data up to user socket. 195 */ 196 if (th == 0) 197 goto present; 198 199 /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */ 200 MALLOC(te, struct tseg_qent *, sizeof (struct tseg_qent), M_TSEGQ, 201 M_NOWAIT); 202 if (te == NULL) { 203 tcpstat.tcps_rcvmemdrop++; 204 m_freem(m); 205 return (0); 206 } 207 208 /* 209 * Find a segment which begins after this one does. 210 */ 211 LIST_FOREACH(q, &tp->t_segq, tqe_q) { 212 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) 213 break; 214 p = q; 215 } 216 217 /* 218 * If there is a preceding segment, it may provide some of 219 * our data already. If so, drop the data from the incoming 220 * segment. If it provides all of our data, drop us. 221 */ 222 if (p != NULL) { 223 register int i; 224 /* conversion to int (in i) handles seq wraparound */ 225 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; 226 if (i > 0) { 227 if (i >= *tlenp) { 228 tcpstat.tcps_rcvduppack++; 229 tcpstat.tcps_rcvdupbyte += *tlenp; 230 m_freem(m); 231 FREE(te, M_TSEGQ); 232 /* 233 * Try to present any queued data 234 * at the left window edge to the user. 235 * This is needed after the 3-WHS 236 * completes. 237 */ 238 goto present; /* ??? */ 239 } 240 m_adj(m, i); 241 *tlenp -= i; 242 th->th_seq += i; 243 } 244 } 245 tcpstat.tcps_rcvoopack++; 246 tcpstat.tcps_rcvoobyte += *tlenp; 247 248 /* 249 * While we overlap succeeding segments trim them or, 250 * if they are completely covered, dequeue them. 251 */ 252 while (q) { 253 register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; 254 if (i <= 0) 255 break; 256 if (i < q->tqe_len) { 257 q->tqe_th->th_seq += i; 258 q->tqe_len -= i; 259 m_adj(q->tqe_m, i); 260 break; 261 } 262 263 nq = LIST_NEXT(q, tqe_q); 264 LIST_REMOVE(q, tqe_q); 265 m_freem(q->tqe_m); 266 FREE(q, M_TSEGQ); 267 q = nq; 268 } 269 270 /* Insert the new segment queue entry into place. */ 271 te->tqe_m = m; 272 te->tqe_th = th; 273 te->tqe_len = *tlenp; 274 275 if (p == NULL) { 276 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); 277 } else { 278 LIST_INSERT_AFTER(p, te, tqe_q); 279 } 280 281present: 282 /* 283 * Present data to user, advancing rcv_nxt through 284 * completed sequence space. 285 */ 286 if (!TCPS_HAVEESTABLISHED(tp->t_state)) 287 return (0); 288 q = LIST_FIRST(&tp->t_segq); 289 if (!q || q->tqe_th->th_seq != tp->rcv_nxt) 290 return (0); 291 do { 292 tp->rcv_nxt += q->tqe_len; 293 flags = q->tqe_th->th_flags & TH_FIN; 294 nq = LIST_NEXT(q, tqe_q); 295 LIST_REMOVE(q, tqe_q); 296 if (so->so_state & SS_CANTRCVMORE) 297 m_freem(q->tqe_m); 298 else
|
299 sbappend(&so->so_rcv, q->tqe_m);
| 299 sbappendstream(&so->so_rcv, q->tqe_m);
|
300 FREE(q, M_TSEGQ); 301 q = nq; 302 } while (q && q->tqe_th->th_seq == tp->rcv_nxt); 303 ND6_HINT(tp); 304 sorwakeup(so); 305 return (flags); 306} 307 308/* 309 * TCP input routine, follows pages 65-76 of the 310 * protocol specification dated September, 1981 very closely. 311 */ 312#ifdef INET6 313int 314tcp6_input(mp, offp, proto) 315 struct mbuf **mp; 316 int *offp, proto; 317{ 318 register struct mbuf *m = *mp; 319 struct in6_ifaddr *ia6; 320 321 IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 322 323 /* 324 * draft-itojun-ipv6-tcp-to-anycast 325 * better place to put this in? 326 */ 327 ia6 = ip6_getdstifaddr(m); 328 if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 329 struct ip6_hdr *ip6; 330 331 ip6 = mtod(m, struct ip6_hdr *); 332 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 333 (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 334 return IPPROTO_DONE; 335 } 336 337 tcp_input(m, *offp); 338 return IPPROTO_DONE; 339} 340#endif 341 342void 343tcp_input(m, off0) 344 register struct mbuf *m; 345 int off0; 346{ 347 register struct tcphdr *th; 348 register struct ip *ip = NULL; 349 register struct ipovly *ipov; 350 register struct inpcb *inp = NULL; 351 u_char *optp = NULL; 352 int optlen = 0; 353 int len, tlen, off; 354 int drop_hdrlen; 355 register struct tcpcb *tp = 0; 356 register int thflags; 357 struct socket *so = 0; 358 int todrop, acked, ourfinisacked, needoutput = 0; 359 u_long tiwin; 360 struct tcpopt to; /* options in this segment */ 361 struct rmxp_tao *taop; /* pointer to our TAO cache entry */ 362 struct rmxp_tao tao_noncached; /* in case there's no cached entry */ 363 int headlocked = 0; 364 struct sockaddr_in *next_hop = NULL; 365 int rstreason; /* For badport_bandlim accounting purposes */ 366 367 struct ip6_hdr *ip6 = NULL; 368#ifdef INET6 369 int isipv6; 370#else 371 const int isipv6 = 0; 372#endif 373 374#ifdef TCPDEBUG 375 /* 376 * The size of tcp_saveipgen must be the size of the max ip header, 377 * now IPv6. 378 */ 379 u_char tcp_saveipgen[40]; 380 struct tcphdr tcp_savetcp; 381 short ostate = 0; 382#endif 383 384 /* Grab info from MT_TAG mbufs prepended to the chain. */ 385 for (;m && m->m_type == MT_TAG; m = m->m_next) { 386 if (m->_m_tag_id == PACKET_TAG_IPFORWARD) 387 next_hop = (struct sockaddr_in *)m->m_hdr.mh_data; 388 } 389#ifdef INET6 390 isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 391#endif 392 bzero((char *)&to, sizeof(to)); 393 394 tcpstat.tcps_rcvtotal++; 395 396 if (isipv6) { 397 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ 398 ip6 = mtod(m, struct ip6_hdr *); 399 tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 400 if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 401 tcpstat.tcps_rcvbadsum++; 402 goto drop; 403 } 404 th = (struct tcphdr *)((caddr_t)ip6 + off0); 405 406 /* 407 * Be proactive about unspecified IPv6 address in source. 408 * As we use all-zero to indicate unbounded/unconnected pcb, 409 * unspecified IPv6 address can be used to confuse us. 410 * 411 * Note that packets with unspecified IPv6 destination is 412 * already dropped in ip6_input. 413 */ 414 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 415 /* XXX stat */ 416 goto drop; 417 } 418 } else { 419 /* 420 * Get IP and TCP header together in first mbuf. 421 * Note: IP leaves IP header in first mbuf. 422 */ 423 if (off0 > sizeof (struct ip)) { 424 ip_stripoptions(m, (struct mbuf *)0); 425 off0 = sizeof(struct ip); 426 } 427 if (m->m_len < sizeof (struct tcpiphdr)) { 428 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 429 tcpstat.tcps_rcvshort++; 430 return; 431 } 432 } 433 ip = mtod(m, struct ip *); 434 ipov = (struct ipovly *)ip; 435 th = (struct tcphdr *)((caddr_t)ip + off0); 436 tlen = ip->ip_len; 437 438 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 439 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 440 th->th_sum = m->m_pkthdr.csum_data; 441 else 442 th->th_sum = in_pseudo(ip->ip_src.s_addr, 443 ip->ip_dst.s_addr, 444 htonl(m->m_pkthdr.csum_data + 445 ip->ip_len + 446 IPPROTO_TCP)); 447 th->th_sum ^= 0xffff; 448#ifdef TCPDEBUG 449 ipov->ih_len = (u_short)tlen; 450 ipov->ih_len = htons(ipov->ih_len); 451#endif 452 } else { 453 /* 454 * Checksum extended TCP header and data. 455 */ 456 len = sizeof (struct ip) + tlen; 457 bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 458 ipov->ih_len = (u_short)tlen; 459 ipov->ih_len = htons(ipov->ih_len); 460 th->th_sum = in_cksum(m, len); 461 } 462 if (th->th_sum) { 463 tcpstat.tcps_rcvbadsum++; 464 goto drop; 465 } 466#ifdef INET6 467 /* Re-initialization for later version check */ 468 ip->ip_v = IPVERSION; 469#endif 470 } 471 472 /* 473 * Check that TCP offset makes sense, 474 * pull out TCP options and adjust length. XXX 475 */ 476 off = th->th_off << 2; 477 if (off < sizeof (struct tcphdr) || off > tlen) { 478 tcpstat.tcps_rcvbadoff++; 479 goto drop; 480 } 481 tlen -= off; /* tlen is used instead of ti->ti_len */ 482 if (off > sizeof (struct tcphdr)) { 483 if (isipv6) { 484 IP6_EXTHDR_CHECK(m, off0, off, ); 485 ip6 = mtod(m, struct ip6_hdr *); 486 th = (struct tcphdr *)((caddr_t)ip6 + off0); 487 } else { 488 if (m->m_len < sizeof(struct ip) + off) { 489 if ((m = m_pullup(m, sizeof (struct ip) + off)) 490 == 0) { 491 tcpstat.tcps_rcvshort++; 492 return; 493 } 494 ip = mtod(m, struct ip *); 495 ipov = (struct ipovly *)ip; 496 th = (struct tcphdr *)((caddr_t)ip + off0); 497 } 498 } 499 optlen = off - sizeof (struct tcphdr); 500 optp = (u_char *)(th + 1); 501 } 502 thflags = th->th_flags; 503 504#ifdef TCP_DROP_SYNFIN 505 /* 506 * If the drop_synfin option is enabled, drop all packets with 507 * both the SYN and FIN bits set. This prevents e.g. nmap from 508 * identifying the TCP/IP stack. 509 * 510 * This is a violation of the TCP specification. 511 */ 512 if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN)) 513 goto drop; 514#endif 515 516 /* 517 * Convert TCP protocol specific fields to host format. 518 */ 519 th->th_seq = ntohl(th->th_seq); 520 th->th_ack = ntohl(th->th_ack); 521 th->th_win = ntohs(th->th_win); 522 th->th_urp = ntohs(th->th_urp); 523 524 /* 525 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options, 526 * until after ip6_savecontrol() is called and before other functions 527 * which don't want those proto headers. 528 * Because ip6_savecontrol() is going to parse the mbuf to 529 * search for data to be passed up to user-land, it wants mbuf 530 * parameters to be unchanged. 531 * XXX: the call of ip6_savecontrol() has been obsoleted based on 532 * latest version of the advanced API (20020110). 533 */ 534 drop_hdrlen = off0 + off; 535 536 /* 537 * Locate pcb for segment. 538 */ 539 INP_INFO_WLOCK(&tcbinfo); 540 headlocked = 1; 541findpcb: 542 /* IPFIREWALL_FORWARD section */ 543 if (next_hop != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 544 /* 545 * Transparently forwarded. Pretend to be the destination. 546 * already got one like this? 547 */ 548 inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, 549 ip->ip_dst, th->th_dport, 550 0, m->m_pkthdr.rcvif); 551 if (!inp) { 552 /* It's new. Try find the ambushing socket. */ 553 inp = in_pcblookup_hash(&tcbinfo, 554 ip->ip_src, th->th_sport, 555 next_hop->sin_addr, 556 next_hop->sin_port ? 557 ntohs(next_hop->sin_port) : 558 th->th_dport, 559 1, m->m_pkthdr.rcvif); 560 } 561 } else { 562 if (isipv6) 563 inp = in6_pcblookup_hash(&tcbinfo, 564 &ip6->ip6_src, th->th_sport, 565 &ip6->ip6_dst, th->th_dport, 566 1, m->m_pkthdr.rcvif); 567 else 568 inp = in_pcblookup_hash(&tcbinfo, 569 ip->ip_src, th->th_sport, 570 ip->ip_dst, th->th_dport, 571 1, m->m_pkthdr.rcvif); 572 } 573 574#ifdef IPSEC 575 if (isipv6) { 576 if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) { 577 ipsec6stat.in_polvio++; 578 goto drop; 579 } 580 } else { 581 if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) { 582 ipsecstat.in_polvio++; 583 goto drop; 584 } 585 } 586#endif 587#ifdef FAST_IPSEC 588 if (isipv6) { 589 if (inp != NULL && ipsec6_in_reject(m, inp)) { 590 goto drop; 591 } 592 } else 593 if (inp != NULL && ipsec4_in_reject(m, inp)) { 594 goto drop; 595 } 596#endif /*FAST_IPSEC*/ 597 598 /* 599 * If the state is CLOSED (i.e., TCB does not exist) then 600 * all data in the incoming segment is discarded. 601 * If the TCB exists but is in CLOSED state, it is embryonic, 602 * but should either do a listen or a connect soon. 603 */ 604 if (inp == NULL) { 605 if (log_in_vain) { 606#ifdef INET6 607 char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2]; 608#else 609 char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"]; 610#endif 611 612 if (isipv6) { 613 strcpy(dbuf, "["); 614 strcpy(sbuf, "["); 615 strcat(dbuf, ip6_sprintf(&ip6->ip6_dst)); 616 strcat(sbuf, ip6_sprintf(&ip6->ip6_src)); 617 strcat(dbuf, "]"); 618 strcat(sbuf, "]"); 619 } else { 620 strcpy(dbuf, inet_ntoa(ip->ip_dst)); 621 strcpy(sbuf, inet_ntoa(ip->ip_src)); 622 } 623 switch (log_in_vain) { 624 case 1: 625 if ((thflags & TH_SYN) == 0) 626 break; 627 /* FALLTHROUGH */ 628 case 2: 629 log(LOG_INFO, 630 "Connection attempt to TCP %s:%d " 631 "from %s:%d flags:0x%02x\n", 632 dbuf, ntohs(th->th_dport), sbuf, 633 ntohs(th->th_sport), thflags); 634 break; 635 default: 636 break; 637 } 638 } 639 if (blackhole) { 640 switch (blackhole) { 641 case 1: 642 if (thflags & TH_SYN) 643 goto drop; 644 break; 645 case 2: 646 goto drop; 647 default: 648 goto drop; 649 } 650 } 651 rstreason = BANDLIM_RST_CLOSEDPORT; 652 goto dropwithreset; 653 } 654 INP_LOCK(inp); 655 if (inp->inp_vflag & INP_TIMEWAIT) { 656 /* 657 * The only option of relevance is TOF_CC, and only if 658 * present in a SYN segment. See tcp_timewait(). 659 */ 660 if (thflags & TH_SYN) 661 tcp_dooptions(&to, optp, optlen, 1); 662 if (tcp_timewait((struct tcptw *)inp->inp_ppcb, 663 &to, th, m, tlen)) 664 goto findpcb; 665 /* 666 * tcp_timewait unlocks inp. 667 */ 668 INP_INFO_WUNLOCK(&tcbinfo); 669 return; 670 } 671 tp = intotcpcb(inp); 672 if (tp == 0) { 673 INP_UNLOCK(inp); 674 rstreason = BANDLIM_RST_CLOSEDPORT; 675 goto dropwithreset; 676 } 677 if (tp->t_state == TCPS_CLOSED) 678 goto drop; 679 680 /* Unscale the window into a 32-bit value. */ 681 if ((thflags & TH_SYN) == 0) 682 tiwin = th->th_win << tp->snd_scale; 683 else 684 tiwin = th->th_win; 685 686 so = inp->inp_socket; 687#ifdef MAC 688 if (mac_check_socket_deliver(so, m)) 689 goto drop; 690#endif 691#ifdef TCPDEBUG 692 if (so->so_options & SO_DEBUG) { 693 ostate = tp->t_state; 694 if (isipv6) 695 bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 696 else 697 bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 698 tcp_savetcp = *th; 699 } 700#endif 701 if (so->so_options & SO_ACCEPTCONN) { 702 struct in_conninfo inc; 703 704#ifdef INET6 705 inc.inc_isipv6 = isipv6; 706#endif 707 if (isipv6) { 708 inc.inc6_faddr = ip6->ip6_src; 709 inc.inc6_laddr = ip6->ip6_dst; 710 inc.inc6_route.ro_rt = NULL; /* XXX */ 711 } else { 712 inc.inc_faddr = ip->ip_src; 713 inc.inc_laddr = ip->ip_dst; 714 inc.inc_route.ro_rt = NULL; /* XXX */ 715 } 716 inc.inc_fport = th->th_sport; 717 inc.inc_lport = th->th_dport; 718 719 /* 720 * If the state is LISTEN then ignore segment if it contains 721 * a RST. If the segment contains an ACK then it is bad and 722 * send a RST. If it does not contain a SYN then it is not 723 * interesting; drop it. 724 * 725 * If the state is SYN_RECEIVED (syncache) and seg contains 726 * an ACK, but not for our SYN/ACK, send a RST. If the seg 727 * contains a RST, check the sequence number to see if it 728 * is a valid reset segment. 729 */ 730 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 731 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 732 if (!syncache_expand(&inc, th, &so, m)) { 733 /* 734 * No syncache entry, or ACK was not 735 * for our SYN/ACK. Send a RST. 736 */ 737 tcpstat.tcps_badsyn++; 738 rstreason = BANDLIM_RST_OPENPORT; 739 goto dropwithreset; 740 } 741 if (so == NULL) { 742 /* 743 * Could not complete 3-way handshake, 744 * connection is being closed down, and 745 * syncache will free mbuf. 746 */ 747 INP_UNLOCK(inp); 748 INP_INFO_WUNLOCK(&tcbinfo); 749 return; 750 } 751 /* 752 * Socket is created in state SYN_RECEIVED. 753 * Continue processing segment. 754 */ 755 INP_UNLOCK(inp); 756 inp = sotoinpcb(so); 757 INP_LOCK(inp); 758 tp = intotcpcb(inp); 759 /* 760 * This is what would have happened in 761 * tcp_output() when the SYN,ACK was sent. 762 */ 763 tp->snd_up = tp->snd_una; 764 tp->snd_max = tp->snd_nxt = tp->iss + 1; 765 tp->last_ack_sent = tp->rcv_nxt; 766 /* 767 * RFC1323: The window in SYN & SYN/ACK 768 * segments is never scaled. 769 */ 770 tp->snd_wnd = tiwin; /* unscaled */ 771 goto after_listen; 772 } 773 if (thflags & TH_RST) { 774 syncache_chkrst(&inc, th); 775 goto drop; 776 } 777 if (thflags & TH_ACK) { 778 syncache_badack(&inc); 779 tcpstat.tcps_badsyn++; 780 rstreason = BANDLIM_RST_OPENPORT; 781 goto dropwithreset; 782 } 783 goto drop; 784 } 785 786 /* 787 * Segment's flags are (SYN) or (SYN|FIN). 788 */ 789#ifdef INET6 790 /* 791 * If deprecated address is forbidden, 792 * we do not accept SYN to deprecated interface 793 * address to prevent any new inbound connection from 794 * getting established. 795 * When we do not accept SYN, we send a TCP RST, 796 * with deprecated source address (instead of dropping 797 * it). We compromise it as it is much better for peer 798 * to send a RST, and RST will be the final packet 799 * for the exchange. 800 * 801 * If we do not forbid deprecated addresses, we accept 802 * the SYN packet. RFC2462 does not suggest dropping 803 * SYN in this case. 804 * If we decipher RFC2462 5.5.4, it says like this: 805 * 1. use of deprecated addr with existing 806 * communication is okay - "SHOULD continue to be 807 * used" 808 * 2. use of it with new communication: 809 * (2a) "SHOULD NOT be used if alternate address 810 * with sufficient scope is available" 811 * (2b) nothing mentioned otherwise. 812 * Here we fall into (2b) case as we have no choice in 813 * our source address selection - we must obey the peer. 814 * 815 * The wording in RFC2462 is confusing, and there are 816 * multiple description text for deprecated address 817 * handling - worse, they are not exactly the same. 818 * I believe 5.5.4 is the best one, so we follow 5.5.4. 819 */ 820 if (isipv6 && !ip6_use_deprecated) { 821 struct in6_ifaddr *ia6; 822 823 if ((ia6 = ip6_getdstifaddr(m)) && 824 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 825 INP_UNLOCK(inp); 826 tp = NULL; 827 rstreason = BANDLIM_RST_OPENPORT; 828 goto dropwithreset; 829 } 830 } 831#endif 832 /* 833 * If it is from this socket, drop it, it must be forged. 834 * Don't bother responding if the destination was a broadcast. 835 */ 836 if (th->th_dport == th->th_sport) { 837 if (isipv6) { 838 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 839 &ip6->ip6_src)) 840 goto drop; 841 } else { 842 if (ip->ip_dst.s_addr == ip->ip_src.s_addr) 843 goto drop; 844 } 845 } 846 /* 847 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 848 * 849 * Note that it is quite possible to receive unicast 850 * link-layer packets with a broadcast IP address. Use 851 * in_broadcast() to find them. 852 */ 853 if (m->m_flags & (M_BCAST|M_MCAST)) 854 goto drop; 855 if (isipv6) { 856 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 857 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 858 goto drop; 859 } else { 860 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 861 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 862 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 863 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 864 goto drop; 865 } 866 /* 867 * SYN appears to be valid; create compressed TCP state 868 * for syncache, or perform t/tcp connection. 869 */ 870 if (so->so_qlen <= so->so_qlimit) { 871#ifdef TCPDEBUG 872 if (so->so_options & SO_DEBUG) 873 tcp_trace(TA_INPUT, ostate, tp, 874 (void *)tcp_saveipgen, &tcp_savetcp, 0); 875#endif 876 tcp_dooptions(&to, optp, optlen, 1); 877 if (!syncache_add(&inc, &to, th, &so, m)) 878 goto drop; 879 if (so == NULL) { 880 /* 881 * Entry added to syncache, mbuf used to 882 * send SYN,ACK packet. 883 */ 884 KASSERT(headlocked, ("headlocked")); 885 INP_UNLOCK(inp); 886 INP_INFO_WUNLOCK(&tcbinfo); 887 return; 888 } 889 /* 890 * Segment passed TAO tests. 891 */ 892 INP_UNLOCK(inp); 893 inp = sotoinpcb(so); 894 INP_LOCK(inp); 895 tp = intotcpcb(inp); 896 tp->snd_wnd = tiwin; 897 tp->t_starttime = ticks; 898 tp->t_state = TCPS_ESTABLISHED; 899 900 /* 901 * T/TCP logic: 902 * If there is a FIN or if there is data, then 903 * delay SYN,ACK(SYN) in the hope of piggy-backing 904 * it on a response segment. Otherwise must send 905 * ACK now in case the other side is slow starting. 906 */ 907 if (thflags & TH_FIN || tlen != 0) 908 tp->t_flags |= (TF_DELACK | TF_NEEDSYN); 909 else 910 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 911 tcpstat.tcps_connects++; 912 soisconnected(so); 913 goto trimthenstep6; 914 } 915 goto drop; 916 } 917after_listen: 918 919/* XXX temp debugging */ 920 /* should not happen - syncache should pick up these connections */ 921 if (tp->t_state == TCPS_LISTEN) 922 panic("tcp_input: TCPS_LISTEN"); 923 924 /* 925 * Segment received on connection. 926 * Reset idle time and keep-alive timer. 927 */ 928 tp->t_rcvtime = ticks; 929 if (TCPS_HAVEESTABLISHED(tp->t_state)) 930 callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 931 932 /* 933 * Process options. 934 * XXX this is tradtitional behavior, may need to be cleaned up. 935 */ 936 tcp_dooptions(&to, optp, optlen, thflags & TH_SYN); 937 if (thflags & TH_SYN) { 938 if (to.to_flags & TOF_SCALE) { 939 tp->t_flags |= TF_RCVD_SCALE; 940 tp->requested_s_scale = to.to_requested_s_scale; 941 } 942 if (to.to_flags & TOF_TS) { 943 tp->t_flags |= TF_RCVD_TSTMP; 944 tp->ts_recent = to.to_tsval; 945 tp->ts_recent_age = ticks; 946 } 947 if (to.to_flags & (TOF_CC|TOF_CCNEW)) 948 tp->t_flags |= TF_RCVD_CC; 949 if (to.to_flags & TOF_MSS) 950 tcp_mss(tp, to.to_mss); 951 } 952 953 /* 954 * Header prediction: check for the two common cases 955 * of a uni-directional data xfer. If the packet has 956 * no control flags, is in-sequence, the window didn't 957 * change and we're not retransmitting, it's a 958 * candidate. If the length is zero and the ack moved 959 * forward, we're the sender side of the xfer. Just 960 * free the data acked & wake any higher level process 961 * that was blocked waiting for space. If the length 962 * is non-zero and the ack didn't move, we're the 963 * receiver side. If we're getting packets in-order 964 * (the reassembly queue is empty), add the data to 965 * the socket buffer and note that we need a delayed ack. 966 * Make sure that the hidden state-flags are also off. 967 * Since we check for TCPS_ESTABLISHED above, it can only 968 * be TH_NEEDSYN. 969 */ 970 if (tp->t_state == TCPS_ESTABLISHED && 971 (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 972 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 973 ((to.to_flags & TOF_TS) == 0 || 974 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 975 /* 976 * Using the CC option is compulsory if once started: 977 * the segment is OK if no T/TCP was negotiated or 978 * if the segment has a CC option equal to CCrecv 979 */ 980 ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 981 ((to.to_flags & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) && 982 th->th_seq == tp->rcv_nxt && 983 tiwin && tiwin == tp->snd_wnd && 984 tp->snd_nxt == tp->snd_max) { 985 986 /* 987 * If last ACK falls within this segment's sequence numbers, 988 * record the timestamp. 989 * NOTE that the test is modified according to the latest 990 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 991 */ 992 if ((to.to_flags & TOF_TS) != 0 && 993 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 994 tp->ts_recent_age = ticks; 995 tp->ts_recent = to.to_tsval; 996 } 997 998 if (tlen == 0) { 999 if (SEQ_GT(th->th_ack, tp->snd_una) && 1000 SEQ_LEQ(th->th_ack, tp->snd_max) && 1001 tp->snd_cwnd >= tp->snd_wnd && 1002 ((!tcp_do_newreno && 1003 tp->t_dupacks < tcprexmtthresh) || 1004 (tcp_do_newreno && !IN_FASTRECOVERY(tp)))) { 1005 KASSERT(headlocked, ("headlocked")); 1006 INP_INFO_WUNLOCK(&tcbinfo); 1007 /* 1008 * this is a pure ack for outstanding data. 1009 */ 1010 ++tcpstat.tcps_predack; 1011 /* 1012 * "bad retransmit" recovery 1013 */ 1014 if (tp->t_rxtshift == 1 && 1015 ticks < tp->t_badrxtwin) { 1016 ++tcpstat.tcps_sndrexmitbad; 1017 tp->snd_cwnd = tp->snd_cwnd_prev; 1018 tp->snd_ssthresh = 1019 tp->snd_ssthresh_prev; 1020 tp->snd_recover = tp->snd_recover_prev; 1021 if (tp->t_flags & TF_WASFRECOVERY) 1022 ENTER_FASTRECOVERY(tp); 1023 tp->snd_nxt = tp->snd_max; 1024 tp->t_badrxtwin = 0; 1025 } 1026 1027 /* 1028 * Recalculate the transmit timer / rtt. 1029 * 1030 * Some boxes send broken timestamp replies 1031 * during the SYN+ACK phase, ignore 1032 * timestamps of 0 or we could calculate a 1033 * huge RTT and blow up the retransmit timer. 1034 */ 1035 if ((to.to_flags & TOF_TS) != 0 && 1036 to.to_tsecr) { 1037 tcp_xmit_timer(tp, 1038 ticks - to.to_tsecr + 1); 1039 } else if (tp->t_rtttime && 1040 SEQ_GT(th->th_ack, tp->t_rtseq)) { 1041 tcp_xmit_timer(tp, 1042 ticks - tp->t_rtttime); 1043 } 1044 tcp_xmit_bandwidth_limit(tp, th->th_ack); 1045 acked = th->th_ack - tp->snd_una; 1046 tcpstat.tcps_rcvackpack++; 1047 tcpstat.tcps_rcvackbyte += acked; 1048 sbdrop(&so->so_snd, acked); 1049 if (SEQ_GT(tp->snd_una, tp->snd_recover) && 1050 SEQ_LEQ(th->th_ack, tp->snd_recover)) 1051 tp->snd_recover = th->th_ack - 1; 1052 tp->snd_una = th->th_ack; 1053 /* 1054 * pull snd_wl2 up to prevent seq wrap relative 1055 * to th_ack. 1056 */ 1057 tp->snd_wl2 = th->th_ack; 1058 tp->t_dupacks = 0; 1059 m_freem(m); 1060 ND6_HINT(tp); /* some progress has been done */ 1061 1062 /* 1063 * If all outstanding data are acked, stop 1064 * retransmit timer, otherwise restart timer 1065 * using current (possibly backed-off) value. 1066 * If process is waiting for space, 1067 * wakeup/selwakeup/signal. If data 1068 * are ready to send, let tcp_output 1069 * decide between more output or persist. 1070 1071#ifdef TCPDEBUG 1072 if (so->so_options & SO_DEBUG) 1073 tcp_trace(TA_INPUT, ostate, tp, 1074 (void *)tcp_saveipgen, 1075 &tcp_savetcp, 0); 1076#endif 1077 */ 1078 if (tp->snd_una == tp->snd_max) 1079 callout_stop(tp->tt_rexmt); 1080 else if (!callout_active(tp->tt_persist)) 1081 callout_reset(tp->tt_rexmt, 1082 tp->t_rxtcur, 1083 tcp_timer_rexmt, tp); 1084 1085 sowwakeup(so); 1086 if (so->so_snd.sb_cc) 1087 (void) tcp_output(tp); 1088 goto check_delack; 1089 } 1090 } else if (th->th_ack == tp->snd_una && 1091 LIST_EMPTY(&tp->t_segq) && 1092 tlen <= sbspace(&so->so_rcv)) { 1093 KASSERT(headlocked, ("headlocked")); 1094 INP_INFO_WUNLOCK(&tcbinfo); 1095 /* 1096 * this is a pure, in-sequence data packet 1097 * with nothing on the reassembly queue and 1098 * we have enough buffer space to take it. 1099 */ 1100 ++tcpstat.tcps_preddat; 1101 tp->rcv_nxt += tlen; 1102 /* 1103 * Pull snd_wl1 up to prevent seq wrap relative to 1104 * th_seq. 1105 */ 1106 tp->snd_wl1 = th->th_seq; 1107 /* 1108 * Pull rcv_up up to prevent seq wrap relative to 1109 * rcv_nxt. 1110 */ 1111 tp->rcv_up = tp->rcv_nxt; 1112 tcpstat.tcps_rcvpack++; 1113 tcpstat.tcps_rcvbyte += tlen; 1114 ND6_HINT(tp); /* some progress has been done */ 1115 /* 1116#ifdef TCPDEBUG 1117 if (so->so_options & SO_DEBUG) 1118 tcp_trace(TA_INPUT, ostate, tp, 1119 (void *)tcp_saveipgen, &tcp_savetcp, 0); 1120#endif 1121 * Add data to socket buffer. 1122 */ 1123 if (so->so_state & SS_CANTRCVMORE) { 1124 m_freem(m); 1125 } else { 1126 m_adj(m, drop_hdrlen); /* delayed header drop */
| 300 FREE(q, M_TSEGQ); 301 q = nq; 302 } while (q && q->tqe_th->th_seq == tp->rcv_nxt); 303 ND6_HINT(tp); 304 sorwakeup(so); 305 return (flags); 306} 307 308/* 309 * TCP input routine, follows pages 65-76 of the 310 * protocol specification dated September, 1981 very closely. 311 */ 312#ifdef INET6 313int 314tcp6_input(mp, offp, proto) 315 struct mbuf **mp; 316 int *offp, proto; 317{ 318 register struct mbuf *m = *mp; 319 struct in6_ifaddr *ia6; 320 321 IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 322 323 /* 324 * draft-itojun-ipv6-tcp-to-anycast 325 * better place to put this in? 326 */ 327 ia6 = ip6_getdstifaddr(m); 328 if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 329 struct ip6_hdr *ip6; 330 331 ip6 = mtod(m, struct ip6_hdr *); 332 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 333 (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 334 return IPPROTO_DONE; 335 } 336 337 tcp_input(m, *offp); 338 return IPPROTO_DONE; 339} 340#endif 341 342void 343tcp_input(m, off0) 344 register struct mbuf *m; 345 int off0; 346{ 347 register struct tcphdr *th; 348 register struct ip *ip = NULL; 349 register struct ipovly *ipov; 350 register struct inpcb *inp = NULL; 351 u_char *optp = NULL; 352 int optlen = 0; 353 int len, tlen, off; 354 int drop_hdrlen; 355 register struct tcpcb *tp = 0; 356 register int thflags; 357 struct socket *so = 0; 358 int todrop, acked, ourfinisacked, needoutput = 0; 359 u_long tiwin; 360 struct tcpopt to; /* options in this segment */ 361 struct rmxp_tao *taop; /* pointer to our TAO cache entry */ 362 struct rmxp_tao tao_noncached; /* in case there's no cached entry */ 363 int headlocked = 0; 364 struct sockaddr_in *next_hop = NULL; 365 int rstreason; /* For badport_bandlim accounting purposes */ 366 367 struct ip6_hdr *ip6 = NULL; 368#ifdef INET6 369 int isipv6; 370#else 371 const int isipv6 = 0; 372#endif 373 374#ifdef TCPDEBUG 375 /* 376 * The size of tcp_saveipgen must be the size of the max ip header, 377 * now IPv6. 378 */ 379 u_char tcp_saveipgen[40]; 380 struct tcphdr tcp_savetcp; 381 short ostate = 0; 382#endif 383 384 /* Grab info from MT_TAG mbufs prepended to the chain. */ 385 for (;m && m->m_type == MT_TAG; m = m->m_next) { 386 if (m->_m_tag_id == PACKET_TAG_IPFORWARD) 387 next_hop = (struct sockaddr_in *)m->m_hdr.mh_data; 388 } 389#ifdef INET6 390 isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 391#endif 392 bzero((char *)&to, sizeof(to)); 393 394 tcpstat.tcps_rcvtotal++; 395 396 if (isipv6) { 397 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ 398 ip6 = mtod(m, struct ip6_hdr *); 399 tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 400 if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 401 tcpstat.tcps_rcvbadsum++; 402 goto drop; 403 } 404 th = (struct tcphdr *)((caddr_t)ip6 + off0); 405 406 /* 407 * Be proactive about unspecified IPv6 address in source. 408 * As we use all-zero to indicate unbounded/unconnected pcb, 409 * unspecified IPv6 address can be used to confuse us. 410 * 411 * Note that packets with unspecified IPv6 destination is 412 * already dropped in ip6_input. 413 */ 414 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 415 /* XXX stat */ 416 goto drop; 417 } 418 } else { 419 /* 420 * Get IP and TCP header together in first mbuf. 421 * Note: IP leaves IP header in first mbuf. 422 */ 423 if (off0 > sizeof (struct ip)) { 424 ip_stripoptions(m, (struct mbuf *)0); 425 off0 = sizeof(struct ip); 426 } 427 if (m->m_len < sizeof (struct tcpiphdr)) { 428 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 429 tcpstat.tcps_rcvshort++; 430 return; 431 } 432 } 433 ip = mtod(m, struct ip *); 434 ipov = (struct ipovly *)ip; 435 th = (struct tcphdr *)((caddr_t)ip + off0); 436 tlen = ip->ip_len; 437 438 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 439 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 440 th->th_sum = m->m_pkthdr.csum_data; 441 else 442 th->th_sum = in_pseudo(ip->ip_src.s_addr, 443 ip->ip_dst.s_addr, 444 htonl(m->m_pkthdr.csum_data + 445 ip->ip_len + 446 IPPROTO_TCP)); 447 th->th_sum ^= 0xffff; 448#ifdef TCPDEBUG 449 ipov->ih_len = (u_short)tlen; 450 ipov->ih_len = htons(ipov->ih_len); 451#endif 452 } else { 453 /* 454 * Checksum extended TCP header and data. 455 */ 456 len = sizeof (struct ip) + tlen; 457 bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 458 ipov->ih_len = (u_short)tlen; 459 ipov->ih_len = htons(ipov->ih_len); 460 th->th_sum = in_cksum(m, len); 461 } 462 if (th->th_sum) { 463 tcpstat.tcps_rcvbadsum++; 464 goto drop; 465 } 466#ifdef INET6 467 /* Re-initialization for later version check */ 468 ip->ip_v = IPVERSION; 469#endif 470 } 471 472 /* 473 * Check that TCP offset makes sense, 474 * pull out TCP options and adjust length. XXX 475 */ 476 off = th->th_off << 2; 477 if (off < sizeof (struct tcphdr) || off > tlen) { 478 tcpstat.tcps_rcvbadoff++; 479 goto drop; 480 } 481 tlen -= off; /* tlen is used instead of ti->ti_len */ 482 if (off > sizeof (struct tcphdr)) { 483 if (isipv6) { 484 IP6_EXTHDR_CHECK(m, off0, off, ); 485 ip6 = mtod(m, struct ip6_hdr *); 486 th = (struct tcphdr *)((caddr_t)ip6 + off0); 487 } else { 488 if (m->m_len < sizeof(struct ip) + off) { 489 if ((m = m_pullup(m, sizeof (struct ip) + off)) 490 == 0) { 491 tcpstat.tcps_rcvshort++; 492 return; 493 } 494 ip = mtod(m, struct ip *); 495 ipov = (struct ipovly *)ip; 496 th = (struct tcphdr *)((caddr_t)ip + off0); 497 } 498 } 499 optlen = off - sizeof (struct tcphdr); 500 optp = (u_char *)(th + 1); 501 } 502 thflags = th->th_flags; 503 504#ifdef TCP_DROP_SYNFIN 505 /* 506 * If the drop_synfin option is enabled, drop all packets with 507 * both the SYN and FIN bits set. This prevents e.g. nmap from 508 * identifying the TCP/IP stack. 509 * 510 * This is a violation of the TCP specification. 511 */ 512 if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN)) 513 goto drop; 514#endif 515 516 /* 517 * Convert TCP protocol specific fields to host format. 518 */ 519 th->th_seq = ntohl(th->th_seq); 520 th->th_ack = ntohl(th->th_ack); 521 th->th_win = ntohs(th->th_win); 522 th->th_urp = ntohs(th->th_urp); 523 524 /* 525 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options, 526 * until after ip6_savecontrol() is called and before other functions 527 * which don't want those proto headers. 528 * Because ip6_savecontrol() is going to parse the mbuf to 529 * search for data to be passed up to user-land, it wants mbuf 530 * parameters to be unchanged. 531 * XXX: the call of ip6_savecontrol() has been obsoleted based on 532 * latest version of the advanced API (20020110). 533 */ 534 drop_hdrlen = off0 + off; 535 536 /* 537 * Locate pcb for segment. 538 */ 539 INP_INFO_WLOCK(&tcbinfo); 540 headlocked = 1; 541findpcb: 542 /* IPFIREWALL_FORWARD section */ 543 if (next_hop != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 544 /* 545 * Transparently forwarded. Pretend to be the destination. 546 * already got one like this? 547 */ 548 inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, 549 ip->ip_dst, th->th_dport, 550 0, m->m_pkthdr.rcvif); 551 if (!inp) { 552 /* It's new. Try find the ambushing socket. */ 553 inp = in_pcblookup_hash(&tcbinfo, 554 ip->ip_src, th->th_sport, 555 next_hop->sin_addr, 556 next_hop->sin_port ? 557 ntohs(next_hop->sin_port) : 558 th->th_dport, 559 1, m->m_pkthdr.rcvif); 560 } 561 } else { 562 if (isipv6) 563 inp = in6_pcblookup_hash(&tcbinfo, 564 &ip6->ip6_src, th->th_sport, 565 &ip6->ip6_dst, th->th_dport, 566 1, m->m_pkthdr.rcvif); 567 else 568 inp = in_pcblookup_hash(&tcbinfo, 569 ip->ip_src, th->th_sport, 570 ip->ip_dst, th->th_dport, 571 1, m->m_pkthdr.rcvif); 572 } 573 574#ifdef IPSEC 575 if (isipv6) { 576 if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) { 577 ipsec6stat.in_polvio++; 578 goto drop; 579 } 580 } else { 581 if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) { 582 ipsecstat.in_polvio++; 583 goto drop; 584 } 585 } 586#endif 587#ifdef FAST_IPSEC 588 if (isipv6) { 589 if (inp != NULL && ipsec6_in_reject(m, inp)) { 590 goto drop; 591 } 592 } else 593 if (inp != NULL && ipsec4_in_reject(m, inp)) { 594 goto drop; 595 } 596#endif /*FAST_IPSEC*/ 597 598 /* 599 * If the state is CLOSED (i.e., TCB does not exist) then 600 * all data in the incoming segment is discarded. 601 * If the TCB exists but is in CLOSED state, it is embryonic, 602 * but should either do a listen or a connect soon. 603 */ 604 if (inp == NULL) { 605 if (log_in_vain) { 606#ifdef INET6 607 char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2]; 608#else 609 char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"]; 610#endif 611 612 if (isipv6) { 613 strcpy(dbuf, "["); 614 strcpy(sbuf, "["); 615 strcat(dbuf, ip6_sprintf(&ip6->ip6_dst)); 616 strcat(sbuf, ip6_sprintf(&ip6->ip6_src)); 617 strcat(dbuf, "]"); 618 strcat(sbuf, "]"); 619 } else { 620 strcpy(dbuf, inet_ntoa(ip->ip_dst)); 621 strcpy(sbuf, inet_ntoa(ip->ip_src)); 622 } 623 switch (log_in_vain) { 624 case 1: 625 if ((thflags & TH_SYN) == 0) 626 break; 627 /* FALLTHROUGH */ 628 case 2: 629 log(LOG_INFO, 630 "Connection attempt to TCP %s:%d " 631 "from %s:%d flags:0x%02x\n", 632 dbuf, ntohs(th->th_dport), sbuf, 633 ntohs(th->th_sport), thflags); 634 break; 635 default: 636 break; 637 } 638 } 639 if (blackhole) { 640 switch (blackhole) { 641 case 1: 642 if (thflags & TH_SYN) 643 goto drop; 644 break; 645 case 2: 646 goto drop; 647 default: 648 goto drop; 649 } 650 } 651 rstreason = BANDLIM_RST_CLOSEDPORT; 652 goto dropwithreset; 653 } 654 INP_LOCK(inp); 655 if (inp->inp_vflag & INP_TIMEWAIT) { 656 /* 657 * The only option of relevance is TOF_CC, and only if 658 * present in a SYN segment. See tcp_timewait(). 659 */ 660 if (thflags & TH_SYN) 661 tcp_dooptions(&to, optp, optlen, 1); 662 if (tcp_timewait((struct tcptw *)inp->inp_ppcb, 663 &to, th, m, tlen)) 664 goto findpcb; 665 /* 666 * tcp_timewait unlocks inp. 667 */ 668 INP_INFO_WUNLOCK(&tcbinfo); 669 return; 670 } 671 tp = intotcpcb(inp); 672 if (tp == 0) { 673 INP_UNLOCK(inp); 674 rstreason = BANDLIM_RST_CLOSEDPORT; 675 goto dropwithreset; 676 } 677 if (tp->t_state == TCPS_CLOSED) 678 goto drop; 679 680 /* Unscale the window into a 32-bit value. */ 681 if ((thflags & TH_SYN) == 0) 682 tiwin = th->th_win << tp->snd_scale; 683 else 684 tiwin = th->th_win; 685 686 so = inp->inp_socket; 687#ifdef MAC 688 if (mac_check_socket_deliver(so, m)) 689 goto drop; 690#endif 691#ifdef TCPDEBUG 692 if (so->so_options & SO_DEBUG) { 693 ostate = tp->t_state; 694 if (isipv6) 695 bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 696 else 697 bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 698 tcp_savetcp = *th; 699 } 700#endif 701 if (so->so_options & SO_ACCEPTCONN) { 702 struct in_conninfo inc; 703 704#ifdef INET6 705 inc.inc_isipv6 = isipv6; 706#endif 707 if (isipv6) { 708 inc.inc6_faddr = ip6->ip6_src; 709 inc.inc6_laddr = ip6->ip6_dst; 710 inc.inc6_route.ro_rt = NULL; /* XXX */ 711 } else { 712 inc.inc_faddr = ip->ip_src; 713 inc.inc_laddr = ip->ip_dst; 714 inc.inc_route.ro_rt = NULL; /* XXX */ 715 } 716 inc.inc_fport = th->th_sport; 717 inc.inc_lport = th->th_dport; 718 719 /* 720 * If the state is LISTEN then ignore segment if it contains 721 * a RST. If the segment contains an ACK then it is bad and 722 * send a RST. If it does not contain a SYN then it is not 723 * interesting; drop it. 724 * 725 * If the state is SYN_RECEIVED (syncache) and seg contains 726 * an ACK, but not for our SYN/ACK, send a RST. If the seg 727 * contains a RST, check the sequence number to see if it 728 * is a valid reset segment. 729 */ 730 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 731 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 732 if (!syncache_expand(&inc, th, &so, m)) { 733 /* 734 * No syncache entry, or ACK was not 735 * for our SYN/ACK. Send a RST. 736 */ 737 tcpstat.tcps_badsyn++; 738 rstreason = BANDLIM_RST_OPENPORT; 739 goto dropwithreset; 740 } 741 if (so == NULL) { 742 /* 743 * Could not complete 3-way handshake, 744 * connection is being closed down, and 745 * syncache will free mbuf. 746 */ 747 INP_UNLOCK(inp); 748 INP_INFO_WUNLOCK(&tcbinfo); 749 return; 750 } 751 /* 752 * Socket is created in state SYN_RECEIVED. 753 * Continue processing segment. 754 */ 755 INP_UNLOCK(inp); 756 inp = sotoinpcb(so); 757 INP_LOCK(inp); 758 tp = intotcpcb(inp); 759 /* 760 * This is what would have happened in 761 * tcp_output() when the SYN,ACK was sent. 762 */ 763 tp->snd_up = tp->snd_una; 764 tp->snd_max = tp->snd_nxt = tp->iss + 1; 765 tp->last_ack_sent = tp->rcv_nxt; 766 /* 767 * RFC1323: The window in SYN & SYN/ACK 768 * segments is never scaled. 769 */ 770 tp->snd_wnd = tiwin; /* unscaled */ 771 goto after_listen; 772 } 773 if (thflags & TH_RST) { 774 syncache_chkrst(&inc, th); 775 goto drop; 776 } 777 if (thflags & TH_ACK) { 778 syncache_badack(&inc); 779 tcpstat.tcps_badsyn++; 780 rstreason = BANDLIM_RST_OPENPORT; 781 goto dropwithreset; 782 } 783 goto drop; 784 } 785 786 /* 787 * Segment's flags are (SYN) or (SYN|FIN). 788 */ 789#ifdef INET6 790 /* 791 * If deprecated address is forbidden, 792 * we do not accept SYN to deprecated interface 793 * address to prevent any new inbound connection from 794 * getting established. 795 * When we do not accept SYN, we send a TCP RST, 796 * with deprecated source address (instead of dropping 797 * it). We compromise it as it is much better for peer 798 * to send a RST, and RST will be the final packet 799 * for the exchange. 800 * 801 * If we do not forbid deprecated addresses, we accept 802 * the SYN packet. RFC2462 does not suggest dropping 803 * SYN in this case. 804 * If we decipher RFC2462 5.5.4, it says like this: 805 * 1. use of deprecated addr with existing 806 * communication is okay - "SHOULD continue to be 807 * used" 808 * 2. use of it with new communication: 809 * (2a) "SHOULD NOT be used if alternate address 810 * with sufficient scope is available" 811 * (2b) nothing mentioned otherwise. 812 * Here we fall into (2b) case as we have no choice in 813 * our source address selection - we must obey the peer. 814 * 815 * The wording in RFC2462 is confusing, and there are 816 * multiple description text for deprecated address 817 * handling - worse, they are not exactly the same. 818 * I believe 5.5.4 is the best one, so we follow 5.5.4. 819 */ 820 if (isipv6 && !ip6_use_deprecated) { 821 struct in6_ifaddr *ia6; 822 823 if ((ia6 = ip6_getdstifaddr(m)) && 824 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 825 INP_UNLOCK(inp); 826 tp = NULL; 827 rstreason = BANDLIM_RST_OPENPORT; 828 goto dropwithreset; 829 } 830 } 831#endif 832 /* 833 * If it is from this socket, drop it, it must be forged. 834 * Don't bother responding if the destination was a broadcast. 835 */ 836 if (th->th_dport == th->th_sport) { 837 if (isipv6) { 838 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 839 &ip6->ip6_src)) 840 goto drop; 841 } else { 842 if (ip->ip_dst.s_addr == ip->ip_src.s_addr) 843 goto drop; 844 } 845 } 846 /* 847 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 848 * 849 * Note that it is quite possible to receive unicast 850 * link-layer packets with a broadcast IP address. Use 851 * in_broadcast() to find them. 852 */ 853 if (m->m_flags & (M_BCAST|M_MCAST)) 854 goto drop; 855 if (isipv6) { 856 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 857 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 858 goto drop; 859 } else { 860 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 861 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 862 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 863 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 864 goto drop; 865 } 866 /* 867 * SYN appears to be valid; create compressed TCP state 868 * for syncache, or perform t/tcp connection. 869 */ 870 if (so->so_qlen <= so->so_qlimit) { 871#ifdef TCPDEBUG 872 if (so->so_options & SO_DEBUG) 873 tcp_trace(TA_INPUT, ostate, tp, 874 (void *)tcp_saveipgen, &tcp_savetcp, 0); 875#endif 876 tcp_dooptions(&to, optp, optlen, 1); 877 if (!syncache_add(&inc, &to, th, &so, m)) 878 goto drop; 879 if (so == NULL) { 880 /* 881 * Entry added to syncache, mbuf used to 882 * send SYN,ACK packet. 883 */ 884 KASSERT(headlocked, ("headlocked")); 885 INP_UNLOCK(inp); 886 INP_INFO_WUNLOCK(&tcbinfo); 887 return; 888 } 889 /* 890 * Segment passed TAO tests. 891 */ 892 INP_UNLOCK(inp); 893 inp = sotoinpcb(so); 894 INP_LOCK(inp); 895 tp = intotcpcb(inp); 896 tp->snd_wnd = tiwin; 897 tp->t_starttime = ticks; 898 tp->t_state = TCPS_ESTABLISHED; 899 900 /* 901 * T/TCP logic: 902 * If there is a FIN or if there is data, then 903 * delay SYN,ACK(SYN) in the hope of piggy-backing 904 * it on a response segment. Otherwise must send 905 * ACK now in case the other side is slow starting. 906 */ 907 if (thflags & TH_FIN || tlen != 0) 908 tp->t_flags |= (TF_DELACK | TF_NEEDSYN); 909 else 910 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 911 tcpstat.tcps_connects++; 912 soisconnected(so); 913 goto trimthenstep6; 914 } 915 goto drop; 916 } 917after_listen: 918 919/* XXX temp debugging */ 920 /* should not happen - syncache should pick up these connections */ 921 if (tp->t_state == TCPS_LISTEN) 922 panic("tcp_input: TCPS_LISTEN"); 923 924 /* 925 * Segment received on connection. 926 * Reset idle time and keep-alive timer. 927 */ 928 tp->t_rcvtime = ticks; 929 if (TCPS_HAVEESTABLISHED(tp->t_state)) 930 callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 931 932 /* 933 * Process options. 934 * XXX this is tradtitional behavior, may need to be cleaned up. 935 */ 936 tcp_dooptions(&to, optp, optlen, thflags & TH_SYN); 937 if (thflags & TH_SYN) { 938 if (to.to_flags & TOF_SCALE) { 939 tp->t_flags |= TF_RCVD_SCALE; 940 tp->requested_s_scale = to.to_requested_s_scale; 941 } 942 if (to.to_flags & TOF_TS) { 943 tp->t_flags |= TF_RCVD_TSTMP; 944 tp->ts_recent = to.to_tsval; 945 tp->ts_recent_age = ticks; 946 } 947 if (to.to_flags & (TOF_CC|TOF_CCNEW)) 948 tp->t_flags |= TF_RCVD_CC; 949 if (to.to_flags & TOF_MSS) 950 tcp_mss(tp, to.to_mss); 951 } 952 953 /* 954 * Header prediction: check for the two common cases 955 * of a uni-directional data xfer. If the packet has 956 * no control flags, is in-sequence, the window didn't 957 * change and we're not retransmitting, it's a 958 * candidate. If the length is zero and the ack moved 959 * forward, we're the sender side of the xfer. Just 960 * free the data acked & wake any higher level process 961 * that was blocked waiting for space. If the length 962 * is non-zero and the ack didn't move, we're the 963 * receiver side. If we're getting packets in-order 964 * (the reassembly queue is empty), add the data to 965 * the socket buffer and note that we need a delayed ack. 966 * Make sure that the hidden state-flags are also off. 967 * Since we check for TCPS_ESTABLISHED above, it can only 968 * be TH_NEEDSYN. 969 */ 970 if (tp->t_state == TCPS_ESTABLISHED && 971 (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 972 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 973 ((to.to_flags & TOF_TS) == 0 || 974 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 975 /* 976 * Using the CC option is compulsory if once started: 977 * the segment is OK if no T/TCP was negotiated or 978 * if the segment has a CC option equal to CCrecv 979 */ 980 ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 981 ((to.to_flags & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) && 982 th->th_seq == tp->rcv_nxt && 983 tiwin && tiwin == tp->snd_wnd && 984 tp->snd_nxt == tp->snd_max) { 985 986 /* 987 * If last ACK falls within this segment's sequence numbers, 988 * record the timestamp. 989 * NOTE that the test is modified according to the latest 990 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 991 */ 992 if ((to.to_flags & TOF_TS) != 0 && 993 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 994 tp->ts_recent_age = ticks; 995 tp->ts_recent = to.to_tsval; 996 } 997 998 if (tlen == 0) { 999 if (SEQ_GT(th->th_ack, tp->snd_una) && 1000 SEQ_LEQ(th->th_ack, tp->snd_max) && 1001 tp->snd_cwnd >= tp->snd_wnd && 1002 ((!tcp_do_newreno && 1003 tp->t_dupacks < tcprexmtthresh) || 1004 (tcp_do_newreno && !IN_FASTRECOVERY(tp)))) { 1005 KASSERT(headlocked, ("headlocked")); 1006 INP_INFO_WUNLOCK(&tcbinfo); 1007 /* 1008 * this is a pure ack for outstanding data. 1009 */ 1010 ++tcpstat.tcps_predack; 1011 /* 1012 * "bad retransmit" recovery 1013 */ 1014 if (tp->t_rxtshift == 1 && 1015 ticks < tp->t_badrxtwin) { 1016 ++tcpstat.tcps_sndrexmitbad; 1017 tp->snd_cwnd = tp->snd_cwnd_prev; 1018 tp->snd_ssthresh = 1019 tp->snd_ssthresh_prev; 1020 tp->snd_recover = tp->snd_recover_prev; 1021 if (tp->t_flags & TF_WASFRECOVERY) 1022 ENTER_FASTRECOVERY(tp); 1023 tp->snd_nxt = tp->snd_max; 1024 tp->t_badrxtwin = 0; 1025 } 1026 1027 /* 1028 * Recalculate the transmit timer / rtt. 1029 * 1030 * Some boxes send broken timestamp replies 1031 * during the SYN+ACK phase, ignore 1032 * timestamps of 0 or we could calculate a 1033 * huge RTT and blow up the retransmit timer. 1034 */ 1035 if ((to.to_flags & TOF_TS) != 0 && 1036 to.to_tsecr) { 1037 tcp_xmit_timer(tp, 1038 ticks - to.to_tsecr + 1); 1039 } else if (tp->t_rtttime && 1040 SEQ_GT(th->th_ack, tp->t_rtseq)) { 1041 tcp_xmit_timer(tp, 1042 ticks - tp->t_rtttime); 1043 } 1044 tcp_xmit_bandwidth_limit(tp, th->th_ack); 1045 acked = th->th_ack - tp->snd_una; 1046 tcpstat.tcps_rcvackpack++; 1047 tcpstat.tcps_rcvackbyte += acked; 1048 sbdrop(&so->so_snd, acked); 1049 if (SEQ_GT(tp->snd_una, tp->snd_recover) && 1050 SEQ_LEQ(th->th_ack, tp->snd_recover)) 1051 tp->snd_recover = th->th_ack - 1; 1052 tp->snd_una = th->th_ack; 1053 /* 1054 * pull snd_wl2 up to prevent seq wrap relative 1055 * to th_ack. 1056 */ 1057 tp->snd_wl2 = th->th_ack; 1058 tp->t_dupacks = 0; 1059 m_freem(m); 1060 ND6_HINT(tp); /* some progress has been done */ 1061 1062 /* 1063 * If all outstanding data are acked, stop 1064 * retransmit timer, otherwise restart timer 1065 * using current (possibly backed-off) value. 1066 * If process is waiting for space, 1067 * wakeup/selwakeup/signal. If data 1068 * are ready to send, let tcp_output 1069 * decide between more output or persist. 1070 1071#ifdef TCPDEBUG 1072 if (so->so_options & SO_DEBUG) 1073 tcp_trace(TA_INPUT, ostate, tp, 1074 (void *)tcp_saveipgen, 1075 &tcp_savetcp, 0); 1076#endif 1077 */ 1078 if (tp->snd_una == tp->snd_max) 1079 callout_stop(tp->tt_rexmt); 1080 else if (!callout_active(tp->tt_persist)) 1081 callout_reset(tp->tt_rexmt, 1082 tp->t_rxtcur, 1083 tcp_timer_rexmt, tp); 1084 1085 sowwakeup(so); 1086 if (so->so_snd.sb_cc) 1087 (void) tcp_output(tp); 1088 goto check_delack; 1089 } 1090 } else if (th->th_ack == tp->snd_una && 1091 LIST_EMPTY(&tp->t_segq) && 1092 tlen <= sbspace(&so->so_rcv)) { 1093 KASSERT(headlocked, ("headlocked")); 1094 INP_INFO_WUNLOCK(&tcbinfo); 1095 /* 1096 * this is a pure, in-sequence data packet 1097 * with nothing on the reassembly queue and 1098 * we have enough buffer space to take it. 1099 */ 1100 ++tcpstat.tcps_preddat; 1101 tp->rcv_nxt += tlen; 1102 /* 1103 * Pull snd_wl1 up to prevent seq wrap relative to 1104 * th_seq. 1105 */ 1106 tp->snd_wl1 = th->th_seq; 1107 /* 1108 * Pull rcv_up up to prevent seq wrap relative to 1109 * rcv_nxt. 1110 */ 1111 tp->rcv_up = tp->rcv_nxt; 1112 tcpstat.tcps_rcvpack++; 1113 tcpstat.tcps_rcvbyte += tlen; 1114 ND6_HINT(tp); /* some progress has been done */ 1115 /* 1116#ifdef TCPDEBUG 1117 if (so->so_options & SO_DEBUG) 1118 tcp_trace(TA_INPUT, ostate, tp, 1119 (void *)tcp_saveipgen, &tcp_savetcp, 0); 1120#endif 1121 * Add data to socket buffer. 1122 */ 1123 if (so->so_state & SS_CANTRCVMORE) { 1124 m_freem(m); 1125 } else { 1126 m_adj(m, drop_hdrlen); /* delayed header drop */
|
1127 sbappend(&so->so_rcv, m);
| 1127 sbappendstream(&so->so_rcv, m);
|
1128 } 1129 sorwakeup(so); 1130 if (DELAY_ACK(tp)) { 1131 tp->t_flags |= TF_DELACK; 1132 } else { 1133 tp->t_flags |= TF_ACKNOW; 1134 tcp_output(tp); 1135 } 1136 goto check_delack; 1137 } 1138 } 1139 1140 /* 1141 * Calculate amount of space in receive window, 1142 * and then do TCP input processing. 1143 * Receive window is amount of space in rcv queue, 1144 * but not less than advertised window. 1145 */ 1146 { int win; 1147 1148 win = sbspace(&so->so_rcv); 1149 if (win < 0) 1150 win = 0; 1151 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1152 } 1153 1154 switch (tp->t_state) { 1155 1156 /* 1157 * If the state is SYN_RECEIVED: 1158 * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1159 */ 1160 case TCPS_SYN_RECEIVED: 1161 if ((thflags & TH_ACK) && 1162 (SEQ_LEQ(th->th_ack, tp->snd_una) || 1163 SEQ_GT(th->th_ack, tp->snd_max))) { 1164 rstreason = BANDLIM_RST_OPENPORT; 1165 goto dropwithreset; 1166 } 1167 break; 1168 1169 /* 1170 * If the state is SYN_SENT: 1171 * if seg contains an ACK, but not for our SYN, drop the input. 1172 * if seg contains a RST, then drop the connection. 1173 * if seg does not contain SYN, then drop it. 1174 * Otherwise this is an acceptable SYN segment 1175 * initialize tp->rcv_nxt and tp->irs 1176 * if seg contains ack then advance tp->snd_una 1177 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1178 * arrange for segment to be acked (eventually) 1179 * continue processing rest of data/controls, beginning with URG 1180 */ 1181 case TCPS_SYN_SENT: 1182 if ((taop = tcp_gettaocache(&inp->inp_inc)) == NULL) { 1183 taop = &tao_noncached; 1184 bzero(taop, sizeof(*taop)); 1185 } 1186 1187 if ((thflags & TH_ACK) && 1188 (SEQ_LEQ(th->th_ack, tp->iss) || 1189 SEQ_GT(th->th_ack, tp->snd_max))) { 1190 /* 1191 * If we have a cached CCsent for the remote host, 1192 * hence we haven't just crashed and restarted, 1193 * do not send a RST. This may be a retransmission 1194 * from the other side after our earlier ACK was lost. 1195 * Our new SYN, when it arrives, will serve as the 1196 * needed ACK. 1197 */ 1198 if (taop->tao_ccsent != 0) 1199 goto drop; 1200 else { 1201 rstreason = BANDLIM_UNLIMITED; 1202 goto dropwithreset; 1203 } 1204 } 1205 if (thflags & TH_RST) { 1206 if (thflags & TH_ACK) 1207 tp = tcp_drop(tp, ECONNREFUSED); 1208 goto drop; 1209 } 1210 if ((thflags & TH_SYN) == 0) 1211 goto drop; 1212 tp->snd_wnd = th->th_win; /* initial send window */ 1213 tp->cc_recv = to.to_cc; /* foreign CC */ 1214 1215 tp->irs = th->th_seq; 1216 tcp_rcvseqinit(tp); 1217 if (thflags & TH_ACK) { 1218 /* 1219 * Our SYN was acked. If segment contains CC.ECHO 1220 * option, check it to make sure this segment really 1221 * matches our SYN. If not, just drop it as old 1222 * duplicate, but send an RST if we're still playing 1223 * by the old rules. If no CC.ECHO option, make sure 1224 * we don't get fooled into using T/TCP. 1225 */ 1226 if (to.to_flags & TOF_CCECHO) { 1227 if (tp->cc_send != to.to_ccecho) { 1228 if (taop->tao_ccsent != 0) 1229 goto drop; 1230 else { 1231 rstreason = BANDLIM_UNLIMITED; 1232 goto dropwithreset; 1233 } 1234 } 1235 } else 1236 tp->t_flags &= ~TF_RCVD_CC; 1237 tcpstat.tcps_connects++; 1238 soisconnected(so); 1239#ifdef MAC 1240 mac_set_socket_peer_from_mbuf(m, so); 1241#endif 1242 /* Do window scaling on this connection? */ 1243 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1244 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1245 tp->snd_scale = tp->requested_s_scale; 1246 tp->rcv_scale = tp->request_r_scale; 1247 } 1248 /* Segment is acceptable, update cache if undefined. */ 1249 if (taop->tao_ccsent == 0) 1250 taop->tao_ccsent = to.to_ccecho; 1251 1252 tp->rcv_adv += tp->rcv_wnd; 1253 tp->snd_una++; /* SYN is acked */ 1254 /* 1255 * If there's data, delay ACK; if there's also a FIN 1256 * ACKNOW will be turned on later. 1257 */ 1258 if (DELAY_ACK(tp) && tlen != 0) 1259 callout_reset(tp->tt_delack, tcp_delacktime, 1260 tcp_timer_delack, tp); 1261 else 1262 tp->t_flags |= TF_ACKNOW; 1263 /* 1264 * Received <SYN,ACK> in SYN_SENT[*] state. 1265 * Transitions: 1266 * SYN_SENT --> ESTABLISHED 1267 * SYN_SENT* --> FIN_WAIT_1 1268 */ 1269 tp->t_starttime = ticks; 1270 if (tp->t_flags & TF_NEEDFIN) { 1271 tp->t_state = TCPS_FIN_WAIT_1; 1272 tp->t_flags &= ~TF_NEEDFIN; 1273 thflags &= ~TH_SYN; 1274 } else { 1275 tp->t_state = TCPS_ESTABLISHED; 1276 callout_reset(tp->tt_keep, tcp_keepidle, 1277 tcp_timer_keep, tp); 1278 } 1279 } else { 1280 /* 1281 * Received initial SYN in SYN-SENT[*] state => 1282 * simultaneous open. If segment contains CC option 1283 * and there is a cached CC, apply TAO test. 1284 * If it succeeds, connection is * half-synchronized. 1285 * Otherwise, do 3-way handshake: 1286 * SYN-SENT -> SYN-RECEIVED 1287 * SYN-SENT* -> SYN-RECEIVED* 1288 * If there was no CC option, clear cached CC value. 1289 */ 1290 tp->t_flags |= TF_ACKNOW; 1291 callout_stop(tp->tt_rexmt); 1292 if (to.to_flags & TOF_CC) { 1293 if (taop->tao_cc != 0 && 1294 CC_GT(to.to_cc, taop->tao_cc)) { 1295 /* 1296 * update cache and make transition: 1297 * SYN-SENT -> ESTABLISHED* 1298 * SYN-SENT* -> FIN-WAIT-1* 1299 */ 1300 taop->tao_cc = to.to_cc; 1301 tp->t_starttime = ticks; 1302 if (tp->t_flags & TF_NEEDFIN) { 1303 tp->t_state = TCPS_FIN_WAIT_1; 1304 tp->t_flags &= ~TF_NEEDFIN; 1305 } else { 1306 tp->t_state = TCPS_ESTABLISHED; 1307 callout_reset(tp->tt_keep, 1308 tcp_keepidle, 1309 tcp_timer_keep, 1310 tp); 1311 } 1312 tp->t_flags |= TF_NEEDSYN; 1313 } else 1314 tp->t_state = TCPS_SYN_RECEIVED; 1315 } else { 1316 /* CC.NEW or no option => invalidate cache */ 1317 taop->tao_cc = 0; 1318 tp->t_state = TCPS_SYN_RECEIVED; 1319 } 1320 } 1321 1322trimthenstep6: 1323 /* 1324 * Advance th->th_seq to correspond to first data byte. 1325 * If data, trim to stay within window, 1326 * dropping FIN if necessary. 1327 */ 1328 th->th_seq++; 1329 if (tlen > tp->rcv_wnd) { 1330 todrop = tlen - tp->rcv_wnd; 1331 m_adj(m, -todrop); 1332 tlen = tp->rcv_wnd; 1333 thflags &= ~TH_FIN; 1334 tcpstat.tcps_rcvpackafterwin++; 1335 tcpstat.tcps_rcvbyteafterwin += todrop; 1336 } 1337 tp->snd_wl1 = th->th_seq - 1; 1338 tp->rcv_up = th->th_seq; 1339 /* 1340 * Client side of transaction: already sent SYN and data. 1341 * If the remote host used T/TCP to validate the SYN, 1342 * our data will be ACK'd; if so, enter normal data segment 1343 * processing in the middle of step 5, ack processing. 1344 * Otherwise, goto step 6. 1345 */ 1346 if (thflags & TH_ACK) 1347 goto process_ACK; 1348 1349 goto step6; 1350 1351 /* 1352 * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1353 * if segment contains a SYN and CC [not CC.NEW] option: 1354 * if state == TIME_WAIT and connection duration > MSL, 1355 * drop packet and send RST; 1356 * 1357 * if SEG.CC > CCrecv then is new SYN, and can implicitly 1358 * ack the FIN (and data) in retransmission queue. 1359 * Complete close and delete TCPCB. Then reprocess 1360 * segment, hoping to find new TCPCB in LISTEN state; 1361 * 1362 * else must be old SYN; drop it. 1363 * else do normal processing. 1364 */ 1365 case TCPS_LAST_ACK: 1366 case TCPS_CLOSING: 1367 case TCPS_TIME_WAIT: 1368 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1369 if ((thflags & TH_SYN) && 1370 (to.to_flags & TOF_CC) && tp->cc_recv != 0) { 1371 if (tp->t_state == TCPS_TIME_WAIT && 1372 (ticks - tp->t_starttime) > tcp_msl) { 1373 rstreason = BANDLIM_UNLIMITED; 1374 goto dropwithreset; 1375 } 1376 if (CC_GT(to.to_cc, tp->cc_recv)) { 1377 tp = tcp_close(tp); 1378 goto findpcb; 1379 } 1380 else 1381 goto drop; 1382 } 1383 break; /* continue normal processing */ 1384 } 1385 1386 /* 1387 * States other than LISTEN or SYN_SENT. 1388 * First check the RST flag and sequence number since reset segments 1389 * are exempt from the timestamp and connection count tests. This 1390 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 1391 * below which allowed reset segments in half the sequence space 1392 * to fall though and be processed (which gives forged reset 1393 * segments with a random sequence number a 50 percent chance of 1394 * killing a connection). 1395 * Then check timestamp, if present. 1396 * Then check the connection count, if present. 1397 * Then check that at least some bytes of segment are within 1398 * receive window. If segment begins before rcv_nxt, 1399 * drop leading data (and SYN); if nothing left, just ack. 1400 * 1401 * 1402 * If the RST bit is set, check the sequence number to see 1403 * if this is a valid reset segment. 1404 * RFC 793 page 37: 1405 * In all states except SYN-SENT, all reset (RST) segments 1406 * are validated by checking their SEQ-fields. A reset is 1407 * valid if its sequence number is in the window. 1408 * Note: this does not take into account delayed ACKs, so 1409 * we should test against last_ack_sent instead of rcv_nxt. 1410 * The sequence number in the reset segment is normally an 1411 * echo of our outgoing acknowlegement numbers, but some hosts 1412 * send a reset with the sequence number at the rightmost edge 1413 * of our receive window, and we have to handle this case. 1414 * If we have multiple segments in flight, the intial reset 1415 * segment sequence numbers will be to the left of last_ack_sent, 1416 * but they will eventually catch up. 1417 * In any case, it never made sense to trim reset segments to 1418 * fit the receive window since RFC 1122 says: 1419 * 4.2.2.12 RST Segment: RFC-793 Section 3.4 1420 * 1421 * A TCP SHOULD allow a received RST segment to include data. 1422 * 1423 * DISCUSSION 1424 * It has been suggested that a RST segment could contain 1425 * ASCII text that encoded and explained the cause of the 1426 * RST. No standard has yet been established for such 1427 * data. 1428 * 1429 * If the reset segment passes the sequence number test examine 1430 * the state: 1431 * SYN_RECEIVED STATE: 1432 * If passive open, return to LISTEN state. 1433 * If active open, inform user that connection was refused. 1434 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 1435 * Inform user that connection was reset, and close tcb. 1436 * CLOSING, LAST_ACK STATES: 1437 * Close the tcb. 1438 * TIME_WAIT STATE: 1439 * Drop the segment - see Stevens, vol. 2, p. 964 and 1440 * RFC 1337. 1441 */ 1442 if (thflags & TH_RST) { 1443 if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 1444 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 1445 switch (tp->t_state) { 1446 1447 case TCPS_SYN_RECEIVED: 1448 so->so_error = ECONNREFUSED; 1449 goto close; 1450 1451 case TCPS_ESTABLISHED: 1452 case TCPS_FIN_WAIT_1: 1453 case TCPS_FIN_WAIT_2: 1454 case TCPS_CLOSE_WAIT: 1455 so->so_error = ECONNRESET; 1456 close: 1457 tp->t_state = TCPS_CLOSED; 1458 tcpstat.tcps_drops++; 1459 tp = tcp_close(tp); 1460 break; 1461 1462 case TCPS_CLOSING: 1463 case TCPS_LAST_ACK: 1464 tp = tcp_close(tp); 1465 break; 1466 1467 case TCPS_TIME_WAIT: 1468 KASSERT(tp->t_state != TCPS_TIME_WAIT, 1469 ("timewait")); 1470 break; 1471 } 1472 } 1473 goto drop; 1474 } 1475 1476 /* 1477 * RFC 1323 PAWS: If we have a timestamp reply on this segment 1478 * and it's less than ts_recent, drop it. 1479 */ 1480 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 1481 TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1482 1483 /* Check to see if ts_recent is over 24 days old. */ 1484 if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1485 /* 1486 * Invalidate ts_recent. If this segment updates 1487 * ts_recent, the age will be reset later and ts_recent 1488 * will get a valid value. If it does not, setting 1489 * ts_recent to zero will at least satisfy the 1490 * requirement that zero be placed in the timestamp 1491 * echo reply when ts_recent isn't valid. The 1492 * age isn't reset until we get a valid ts_recent 1493 * because we don't want out-of-order segments to be 1494 * dropped when ts_recent is old. 1495 */ 1496 tp->ts_recent = 0; 1497 } else { 1498 tcpstat.tcps_rcvduppack++; 1499 tcpstat.tcps_rcvdupbyte += tlen; 1500 tcpstat.tcps_pawsdrop++; 1501 if (tlen) 1502 goto dropafterack; 1503 goto drop; 1504 } 1505 } 1506 1507 /* 1508 * T/TCP mechanism 1509 * If T/TCP was negotiated and the segment doesn't have CC, 1510 * or if its CC is wrong then drop the segment. 1511 * RST segments do not have to comply with this. 1512 */ 1513 if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 1514 ((to.to_flags & TOF_CC) == 0 || tp->cc_recv != to.to_cc)) 1515 goto dropafterack; 1516 1517 /* 1518 * In the SYN-RECEIVED state, validate that the packet belongs to 1519 * this connection before trimming the data to fit the receive 1520 * window. Check the sequence number versus IRS since we know 1521 * the sequence numbers haven't wrapped. This is a partial fix 1522 * for the "LAND" DoS attack. 1523 */ 1524 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1525 rstreason = BANDLIM_RST_OPENPORT; 1526 goto dropwithreset; 1527 } 1528 1529 todrop = tp->rcv_nxt - th->th_seq; 1530 if (todrop > 0) { 1531 if (thflags & TH_SYN) { 1532 thflags &= ~TH_SYN; 1533 th->th_seq++; 1534 if (th->th_urp > 1) 1535 th->th_urp--; 1536 else 1537 thflags &= ~TH_URG; 1538 todrop--; 1539 } 1540 /* 1541 * Following if statement from Stevens, vol. 2, p. 960. 1542 */ 1543 if (todrop > tlen 1544 || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1545 /* 1546 * Any valid FIN must be to the left of the window. 1547 * At this point the FIN must be a duplicate or out 1548 * of sequence; drop it. 1549 */ 1550 thflags &= ~TH_FIN; 1551 1552 /* 1553 * Send an ACK to resynchronize and drop any data. 1554 * But keep on processing for RST or ACK. 1555 */ 1556 tp->t_flags |= TF_ACKNOW; 1557 todrop = tlen; 1558 tcpstat.tcps_rcvduppack++; 1559 tcpstat.tcps_rcvdupbyte += todrop; 1560 } else { 1561 tcpstat.tcps_rcvpartduppack++; 1562 tcpstat.tcps_rcvpartdupbyte += todrop; 1563 } 1564 drop_hdrlen += todrop; /* drop from the top afterwards */ 1565 th->th_seq += todrop; 1566 tlen -= todrop; 1567 if (th->th_urp > todrop) 1568 th->th_urp -= todrop; 1569 else { 1570 thflags &= ~TH_URG; 1571 th->th_urp = 0; 1572 } 1573 } 1574 1575 /* 1576 * If new data are received on a connection after the 1577 * user processes are gone, then RST the other end. 1578 */ 1579 if ((so->so_state & SS_NOFDREF) && 1580 tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1581 tp = tcp_close(tp); 1582 tcpstat.tcps_rcvafterclose++; 1583 rstreason = BANDLIM_UNLIMITED; 1584 goto dropwithreset; 1585 } 1586 1587 /* 1588 * If segment ends after window, drop trailing data 1589 * (and PUSH and FIN); if nothing left, just ACK. 1590 */ 1591 todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1592 if (todrop > 0) { 1593 tcpstat.tcps_rcvpackafterwin++; 1594 if (todrop >= tlen) { 1595 tcpstat.tcps_rcvbyteafterwin += tlen; 1596 /* 1597 * If a new connection request is received 1598 * while in TIME_WAIT, drop the old connection 1599 * and start over if the sequence numbers 1600 * are above the previous ones. 1601 */ 1602 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1603 if (thflags & TH_SYN && 1604 tp->t_state == TCPS_TIME_WAIT && 1605 SEQ_GT(th->th_seq, tp->rcv_nxt)) { 1606 tp = tcp_close(tp); 1607 goto findpcb; 1608 } 1609 /* 1610 * If window is closed can only take segments at 1611 * window edge, and have to drop data and PUSH from 1612 * incoming segments. Continue processing, but 1613 * remember to ack. Otherwise, drop segment 1614 * and ack. 1615 */ 1616 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1617 tp->t_flags |= TF_ACKNOW; 1618 tcpstat.tcps_rcvwinprobe++; 1619 } else 1620 goto dropafterack; 1621 } else 1622 tcpstat.tcps_rcvbyteafterwin += todrop; 1623 m_adj(m, -todrop); 1624 tlen -= todrop; 1625 thflags &= ~(TH_PUSH|TH_FIN); 1626 } 1627 1628 /* 1629 * If last ACK falls within this segment's sequence numbers, 1630 * record its timestamp. 1631 * NOTE that the test is modified according to the latest 1632 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1633 */ 1634 if ((to.to_flags & TOF_TS) != 0 && 1635 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1636 tp->ts_recent_age = ticks; 1637 tp->ts_recent = to.to_tsval; 1638 } 1639 1640 /* 1641 * If a SYN is in the window, then this is an 1642 * error and we send an RST and drop the connection. 1643 */ 1644 if (thflags & TH_SYN) { 1645 tp = tcp_drop(tp, ECONNRESET); 1646 rstreason = BANDLIM_UNLIMITED; 1647 goto dropwithreset; 1648 } 1649 1650 /* 1651 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1652 * flag is on (half-synchronized state), then queue data for 1653 * later processing; else drop segment and return. 1654 */ 1655 if ((thflags & TH_ACK) == 0) { 1656 if (tp->t_state == TCPS_SYN_RECEIVED || 1657 (tp->t_flags & TF_NEEDSYN)) 1658 goto step6; 1659 else 1660 goto drop; 1661 } 1662 1663 /* 1664 * Ack processing. 1665 */ 1666 switch (tp->t_state) { 1667 1668 /* 1669 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1670 * ESTABLISHED state and continue processing. 1671 * The ACK was checked above. 1672 */ 1673 case TCPS_SYN_RECEIVED: 1674 1675 tcpstat.tcps_connects++; 1676 soisconnected(so); 1677 /* Do window scaling? */ 1678 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1679 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1680 tp->snd_scale = tp->requested_s_scale; 1681 tp->rcv_scale = tp->request_r_scale; 1682 } 1683 /* 1684 * Upon successful completion of 3-way handshake, 1685 * update cache.CC if it was undefined, pass any queued 1686 * data to the user, and advance state appropriately. 1687 */ 1688 if ((taop = tcp_gettaocache(&inp->inp_inc)) != NULL && 1689 taop->tao_cc == 0) 1690 taop->tao_cc = tp->cc_recv; 1691 1692 /* 1693 * Make transitions: 1694 * SYN-RECEIVED -> ESTABLISHED 1695 * SYN-RECEIVED* -> FIN-WAIT-1 1696 */ 1697 tp->t_starttime = ticks; 1698 if (tp->t_flags & TF_NEEDFIN) { 1699 tp->t_state = TCPS_FIN_WAIT_1; 1700 tp->t_flags &= ~TF_NEEDFIN; 1701 } else { 1702 tp->t_state = TCPS_ESTABLISHED; 1703 callout_reset(tp->tt_keep, tcp_keepidle, 1704 tcp_timer_keep, tp); 1705 } 1706 /* 1707 * If segment contains data or ACK, will call tcp_reass() 1708 * later; if not, do so now to pass queued data to user. 1709 */ 1710 if (tlen == 0 && (thflags & TH_FIN) == 0) 1711 (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1712 (struct mbuf *)0); 1713 tp->snd_wl1 = th->th_seq - 1; 1714 /* FALLTHROUGH */ 1715 1716 /* 1717 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1718 * ACKs. If the ack is in the range 1719 * tp->snd_una < th->th_ack <= tp->snd_max 1720 * then advance tp->snd_una to th->th_ack and drop 1721 * data from the retransmission queue. If this ACK reflects 1722 * more up to date window information we update our window information. 1723 */ 1724 case TCPS_ESTABLISHED: 1725 case TCPS_FIN_WAIT_1: 1726 case TCPS_FIN_WAIT_2: 1727 case TCPS_CLOSE_WAIT: 1728 case TCPS_CLOSING: 1729 case TCPS_LAST_ACK: 1730 case TCPS_TIME_WAIT: 1731 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1732 if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1733 if (tlen == 0 && tiwin == tp->snd_wnd) { 1734 tcpstat.tcps_rcvdupack++; 1735 /* 1736 * If we have outstanding data (other than 1737 * a window probe), this is a completely 1738 * duplicate ack (ie, window info didn't 1739 * change), the ack is the biggest we've 1740 * seen and we've seen exactly our rexmt 1741 * threshhold of them, assume a packet 1742 * has been dropped and retransmit it. 1743 * Kludge snd_nxt & the congestion 1744 * window so we send only this one 1745 * packet. 1746 * 1747 * We know we're losing at the current 1748 * window size so do congestion avoidance 1749 * (set ssthresh to half the current window 1750 * and pull our congestion window back to 1751 * the new ssthresh). 1752 * 1753 * Dup acks mean that packets have left the 1754 * network (they're now cached at the receiver) 1755 * so bump cwnd by the amount in the receiver 1756 * to keep a constant cwnd packets in the 1757 * network. 1758 */ 1759 if (!callout_active(tp->tt_rexmt) || 1760 th->th_ack != tp->snd_una) 1761 tp->t_dupacks = 0; 1762 else if (++tp->t_dupacks > tcprexmtthresh || 1763 (tcp_do_newreno && 1764 IN_FASTRECOVERY(tp))) { 1765 tp->snd_cwnd += tp->t_maxseg; 1766 (void) tcp_output(tp); 1767 goto drop; 1768 } else if (tp->t_dupacks == tcprexmtthresh) { 1769 tcp_seq onxt = tp->snd_nxt; 1770 u_int win; 1771 if (tcp_do_newreno && 1772 SEQ_LEQ(th->th_ack, 1773 tp->snd_recover)) { 1774 tp->t_dupacks = 0; 1775 break; 1776 } 1777 win = min(tp->snd_wnd, tp->snd_cwnd) / 1778 2 / tp->t_maxseg; 1779 if (win < 2) 1780 win = 2; 1781 tp->snd_ssthresh = win * tp->t_maxseg; 1782 ENTER_FASTRECOVERY(tp); 1783 tp->snd_recover = tp->snd_max; 1784 callout_stop(tp->tt_rexmt); 1785 tp->t_rtttime = 0; 1786 tp->snd_nxt = th->th_ack; 1787 tp->snd_cwnd = tp->t_maxseg; 1788 (void) tcp_output(tp); 1789 KASSERT(tp->snd_limited <= 2, 1790 ("tp->snd_limited too big")); 1791 tp->snd_cwnd = tp->snd_ssthresh + 1792 tp->t_maxseg * 1793 (tp->t_dupacks - tp->snd_limited); 1794 if (SEQ_GT(onxt, tp->snd_nxt)) 1795 tp->snd_nxt = onxt; 1796 goto drop; 1797 } else if (tcp_do_rfc3042) { 1798 u_long oldcwnd = tp->snd_cwnd; 1799 tcp_seq oldsndmax = tp->snd_max; 1800 u_int sent; 1801 KASSERT(tp->t_dupacks == 1 || 1802 tp->t_dupacks == 2, 1803 ("dupacks not 1 or 2")); 1804 if (tp->t_dupacks == 1) { 1805 tp->snd_limited = 0; 1806 tp->snd_cwnd += tp->t_maxseg; 1807 } else { 1808 tp->snd_cwnd += 1809 tp->t_maxseg * 2; 1810 } 1811 (void) tcp_output(tp); 1812 sent = tp->snd_max - oldsndmax; 1813 if (sent > tp->t_maxseg) { 1814 KASSERT(tp->snd_limited == 0 && 1815 tp->t_dupacks == 2, 1816 ("sent too much")); 1817 tp->snd_limited = 2; 1818 } else if (sent > 0) 1819 ++tp->snd_limited; 1820 tp->snd_cwnd = oldcwnd; 1821 goto drop; 1822 } 1823 } else 1824 tp->t_dupacks = 0; 1825 break; 1826 } 1827 1828 KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una")); 1829 1830 /* 1831 * If the congestion window was inflated to account 1832 * for the other side's cached packets, retract it. 1833 */ 1834 if (tcp_do_newreno) { 1835 if (IN_FASTRECOVERY(tp)) { 1836 if (SEQ_LT(th->th_ack, tp->snd_recover)) { 1837 tcp_newreno_partial_ack(tp, th); 1838 } else { 1839 /* 1840 * Window inflation should have left us 1841 * with approximately snd_ssthresh 1842 * outstanding data. 1843 * But in case we would be inclined to 1844 * send a burst, better to do it via 1845 * the slow start mechanism. 1846 */ 1847 if (SEQ_GT(th->th_ack + 1848 tp->snd_ssthresh, 1849 tp->snd_max)) 1850 tp->snd_cwnd = tp->snd_max - 1851 th->th_ack + 1852 tp->t_maxseg; 1853 else 1854 tp->snd_cwnd = tp->snd_ssthresh; 1855 } 1856 } 1857 } else { 1858 if (tp->t_dupacks >= tcprexmtthresh && 1859 tp->snd_cwnd > tp->snd_ssthresh) 1860 tp->snd_cwnd = tp->snd_ssthresh; 1861 } 1862 tp->t_dupacks = 0; 1863 if (SEQ_GT(th->th_ack, tp->snd_max)) { 1864 tcpstat.tcps_rcvacktoomuch++; 1865 goto dropafterack; 1866 } 1867 /* 1868 * If we reach this point, ACK is not a duplicate, 1869 * i.e., it ACKs something we sent. 1870 */ 1871 if (tp->t_flags & TF_NEEDSYN) { 1872 /* 1873 * T/TCP: Connection was half-synchronized, and our 1874 * SYN has been ACK'd (so connection is now fully 1875 * synchronized). Go to non-starred state, 1876 * increment snd_una for ACK of SYN, and check if 1877 * we can do window scaling. 1878 */ 1879 tp->t_flags &= ~TF_NEEDSYN; 1880 tp->snd_una++; 1881 /* Do window scaling? */ 1882 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1883 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1884 tp->snd_scale = tp->requested_s_scale; 1885 tp->rcv_scale = tp->request_r_scale; 1886 } 1887 } 1888 1889process_ACK: 1890 acked = th->th_ack - tp->snd_una; 1891 tcpstat.tcps_rcvackpack++; 1892 tcpstat.tcps_rcvackbyte += acked; 1893 1894 /* 1895 * If we just performed our first retransmit, and the ACK 1896 * arrives within our recovery window, then it was a mistake 1897 * to do the retransmit in the first place. Recover our 1898 * original cwnd and ssthresh, and proceed to transmit where 1899 * we left off. 1900 */ 1901 if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 1902 ++tcpstat.tcps_sndrexmitbad; 1903 tp->snd_cwnd = tp->snd_cwnd_prev; 1904 tp->snd_ssthresh = tp->snd_ssthresh_prev; 1905 tp->snd_recover = tp->snd_recover_prev; 1906 if (tp->t_flags & TF_WASFRECOVERY) 1907 ENTER_FASTRECOVERY(tp); 1908 tp->snd_nxt = tp->snd_max; 1909 tp->t_badrxtwin = 0; /* XXX probably not required */ 1910 } 1911 1912 /* 1913 * If we have a timestamp reply, update smoothed 1914 * round trip time. If no timestamp is present but 1915 * transmit timer is running and timed sequence 1916 * number was acked, update smoothed round trip time. 1917 * Since we now have an rtt measurement, cancel the 1918 * timer backoff (cf., Phil Karn's retransmit alg.). 1919 * Recompute the initial retransmit timer. 1920 * 1921 * Some boxes send broken timestamp replies 1922 * during the SYN+ACK phase, ignore 1923 * timestamps of 0 or we could calculate a 1924 * huge RTT and blow up the retransmit timer. 1925 */ 1926 if ((to.to_flags & TOF_TS) != 0 && 1927 to.to_tsecr) { 1928 tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 1929 } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 1930 tcp_xmit_timer(tp, ticks - tp->t_rtttime); 1931 } 1932 tcp_xmit_bandwidth_limit(tp, th->th_ack); 1933 1934 /* 1935 * If all outstanding data is acked, stop retransmit 1936 * timer and remember to restart (more output or persist). 1937 * If there is more data to be acked, restart retransmit 1938 * timer, using current (possibly backed-off) value. 1939 */ 1940 if (th->th_ack == tp->snd_max) { 1941 callout_stop(tp->tt_rexmt); 1942 needoutput = 1; 1943 } else if (!callout_active(tp->tt_persist)) 1944 callout_reset(tp->tt_rexmt, tp->t_rxtcur, 1945 tcp_timer_rexmt, tp); 1946 1947 /* 1948 * If no data (only SYN) was ACK'd, 1949 * skip rest of ACK processing. 1950 */ 1951 if (acked == 0) 1952 goto step6; 1953 1954 /* 1955 * When new data is acked, open the congestion window. 1956 * If the window gives us less than ssthresh packets 1957 * in flight, open exponentially (maxseg per packet). 1958 * Otherwise open linearly: maxseg per window 1959 * (maxseg^2 / cwnd per packet). 1960 */ 1961 if (!tcp_do_newreno || !IN_FASTRECOVERY(tp)) { 1962 register u_int cw = tp->snd_cwnd; 1963 register u_int incr = tp->t_maxseg; 1964 if (cw > tp->snd_ssthresh) 1965 incr = incr * incr / cw; 1966 tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 1967 } 1968 if (acked > so->so_snd.sb_cc) { 1969 tp->snd_wnd -= so->so_snd.sb_cc; 1970 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1971 ourfinisacked = 1; 1972 } else { 1973 sbdrop(&so->so_snd, acked); 1974 tp->snd_wnd -= acked; 1975 ourfinisacked = 0; 1976 } 1977 sowwakeup(so); 1978 /* detect una wraparound */ 1979 if (tcp_do_newreno && !IN_FASTRECOVERY(tp) && 1980 SEQ_GT(tp->snd_una, tp->snd_recover) && 1981 SEQ_LEQ(th->th_ack, tp->snd_recover)) 1982 tp->snd_recover = th->th_ack - 1; 1983 if (tcp_do_newreno && IN_FASTRECOVERY(tp) && 1984 SEQ_GEQ(th->th_ack, tp->snd_recover)) 1985 EXIT_FASTRECOVERY(tp); 1986 tp->snd_una = th->th_ack; 1987 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1988 tp->snd_nxt = tp->snd_una; 1989 1990 switch (tp->t_state) { 1991 1992 /* 1993 * In FIN_WAIT_1 STATE in addition to the processing 1994 * for the ESTABLISHED state if our FIN is now acknowledged 1995 * then enter FIN_WAIT_2. 1996 */ 1997 case TCPS_FIN_WAIT_1: 1998 if (ourfinisacked) { 1999 /* 2000 * If we can't receive any more 2001 * data, then closing user can proceed. 2002 * Starting the timer is contrary to the 2003 * specification, but if we don't get a FIN 2004 * we'll hang forever. 2005 */ 2006 /* XXXjl 2007 * we should release the tp also, and use a 2008 * compressed state. 2009 */ 2010 if (so->so_state & SS_CANTRCVMORE) { 2011 soisdisconnected(so); 2012 callout_reset(tp->tt_2msl, tcp_maxidle, 2013 tcp_timer_2msl, tp); 2014 } 2015 tp->t_state = TCPS_FIN_WAIT_2; 2016 } 2017 break; 2018 2019 /* 2020 * In CLOSING STATE in addition to the processing for 2021 * the ESTABLISHED state if the ACK acknowledges our FIN 2022 * then enter the TIME-WAIT state, otherwise ignore 2023 * the segment. 2024 */ 2025 case TCPS_CLOSING: 2026 if (ourfinisacked) { 2027 KASSERT(headlocked, ("headlocked")); 2028 tcp_twstart(tp); 2029 INP_INFO_WUNLOCK(&tcbinfo); 2030 m_freem(m); 2031 return; 2032 } 2033 break; 2034 2035 /* 2036 * In LAST_ACK, we may still be waiting for data to drain 2037 * and/or to be acked, as well as for the ack of our FIN. 2038 * If our FIN is now acknowledged, delete the TCB, 2039 * enter the closed state and return. 2040 */ 2041 case TCPS_LAST_ACK: 2042 if (ourfinisacked) { 2043 tp = tcp_close(tp); 2044 goto drop; 2045 } 2046 break; 2047 2048 /* 2049 * In TIME_WAIT state the only thing that should arrive 2050 * is a retransmission of the remote FIN. Acknowledge 2051 * it and restart the finack timer. 2052 */ 2053 case TCPS_TIME_WAIT: 2054 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 2055 callout_reset(tp->tt_2msl, 2 * tcp_msl, 2056 tcp_timer_2msl, tp); 2057 goto dropafterack; 2058 } 2059 } 2060 2061step6: 2062 /* 2063 * Update window information. 2064 * Don't look at window if no ACK: TAC's send garbage on first SYN. 2065 */ 2066 if ((thflags & TH_ACK) && 2067 (SEQ_LT(tp->snd_wl1, th->th_seq) || 2068 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2069 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2070 /* keep track of pure window updates */ 2071 if (tlen == 0 && 2072 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2073 tcpstat.tcps_rcvwinupd++; 2074 tp->snd_wnd = tiwin; 2075 tp->snd_wl1 = th->th_seq; 2076 tp->snd_wl2 = th->th_ack; 2077 if (tp->snd_wnd > tp->max_sndwnd) 2078 tp->max_sndwnd = tp->snd_wnd; 2079 needoutput = 1; 2080 } 2081 2082 /* 2083 * Process segments with URG. 2084 */ 2085 if ((thflags & TH_URG) && th->th_urp && 2086 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2087 /* 2088 * This is a kludge, but if we receive and accept 2089 * random urgent pointers, we'll crash in 2090 * soreceive. It's hard to imagine someone 2091 * actually wanting to send this much urgent data. 2092 */ 2093 if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2094 th->th_urp = 0; /* XXX */ 2095 thflags &= ~TH_URG; /* XXX */ 2096 goto dodata; /* XXX */ 2097 } 2098 /* 2099 * If this segment advances the known urgent pointer, 2100 * then mark the data stream. This should not happen 2101 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2102 * a FIN has been received from the remote side. 2103 * In these states we ignore the URG. 2104 * 2105 * According to RFC961 (Assigned Protocols), 2106 * the urgent pointer points to the last octet 2107 * of urgent data. We continue, however, 2108 * to consider it to indicate the first octet 2109 * of data past the urgent section as the original 2110 * spec states (in one of two places). 2111 */ 2112 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2113 tp->rcv_up = th->th_seq + th->th_urp; 2114 so->so_oobmark = so->so_rcv.sb_cc + 2115 (tp->rcv_up - tp->rcv_nxt) - 1; 2116 if (so->so_oobmark == 0) 2117 so->so_state |= SS_RCVATMARK; 2118 sohasoutofband(so); 2119 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2120 } 2121 /* 2122 * Remove out of band data so doesn't get presented to user. 2123 * This can happen independent of advancing the URG pointer, 2124 * but if two URG's are pending at once, some out-of-band 2125 * data may creep in... ick. 2126 */ 2127 if (th->th_urp <= (u_long)tlen && 2128 !(so->so_options & SO_OOBINLINE)) { 2129 /* hdr drop is delayed */ 2130 tcp_pulloutofband(so, th, m, drop_hdrlen); 2131 } 2132 } else { 2133 /* 2134 * If no out of band data is expected, 2135 * pull receive urgent pointer along 2136 * with the receive window. 2137 */ 2138 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2139 tp->rcv_up = tp->rcv_nxt; 2140 } 2141dodata: /* XXX */ 2142 KASSERT(headlocked, ("headlocked")); 2143 /* 2144 * Process the segment text, merging it into the TCP sequencing queue, 2145 * and arranging for acknowledgment of receipt if necessary. 2146 * This process logically involves adjusting tp->rcv_wnd as data 2147 * is presented to the user (this happens in tcp_usrreq.c, 2148 * case PRU_RCVD). If a FIN has already been received on this 2149 * connection then we just ignore the text. 2150 */ 2151 if ((tlen || (thflags & TH_FIN)) && 2152 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2153 m_adj(m, drop_hdrlen); /* delayed header drop */ 2154 /* 2155 * Insert segment which includes th into TCP reassembly queue 2156 * with control block tp. Set thflags to whether reassembly now 2157 * includes a segment with FIN. This handles the common case 2158 * inline (segment is the next to be received on an established 2159 * connection, and the queue is empty), avoiding linkage into 2160 * and removal from the queue and repetition of various 2161 * conversions. 2162 * Set DELACK for segments received in order, but ack 2163 * immediately when segments are out of order (so 2164 * fast retransmit can work). 2165 */ 2166 if (th->th_seq == tp->rcv_nxt && 2167 LIST_EMPTY(&tp->t_segq) && 2168 TCPS_HAVEESTABLISHED(tp->t_state)) { 2169 if (DELAY_ACK(tp)) 2170 tp->t_flags |= TF_DELACK; 2171 else 2172 tp->t_flags |= TF_ACKNOW; 2173 tp->rcv_nxt += tlen; 2174 thflags = th->th_flags & TH_FIN; 2175 tcpstat.tcps_rcvpack++; 2176 tcpstat.tcps_rcvbyte += tlen; 2177 ND6_HINT(tp); 2178 if (so->so_state & SS_CANTRCVMORE) 2179 m_freem(m); 2180 else
| 1128 } 1129 sorwakeup(so); 1130 if (DELAY_ACK(tp)) { 1131 tp->t_flags |= TF_DELACK; 1132 } else { 1133 tp->t_flags |= TF_ACKNOW; 1134 tcp_output(tp); 1135 } 1136 goto check_delack; 1137 } 1138 } 1139 1140 /* 1141 * Calculate amount of space in receive window, 1142 * and then do TCP input processing. 1143 * Receive window is amount of space in rcv queue, 1144 * but not less than advertised window. 1145 */ 1146 { int win; 1147 1148 win = sbspace(&so->so_rcv); 1149 if (win < 0) 1150 win = 0; 1151 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1152 } 1153 1154 switch (tp->t_state) { 1155 1156 /* 1157 * If the state is SYN_RECEIVED: 1158 * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1159 */ 1160 case TCPS_SYN_RECEIVED: 1161 if ((thflags & TH_ACK) && 1162 (SEQ_LEQ(th->th_ack, tp->snd_una) || 1163 SEQ_GT(th->th_ack, tp->snd_max))) { 1164 rstreason = BANDLIM_RST_OPENPORT; 1165 goto dropwithreset; 1166 } 1167 break; 1168 1169 /* 1170 * If the state is SYN_SENT: 1171 * if seg contains an ACK, but not for our SYN, drop the input. 1172 * if seg contains a RST, then drop the connection. 1173 * if seg does not contain SYN, then drop it. 1174 * Otherwise this is an acceptable SYN segment 1175 * initialize tp->rcv_nxt and tp->irs 1176 * if seg contains ack then advance tp->snd_una 1177 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1178 * arrange for segment to be acked (eventually) 1179 * continue processing rest of data/controls, beginning with URG 1180 */ 1181 case TCPS_SYN_SENT: 1182 if ((taop = tcp_gettaocache(&inp->inp_inc)) == NULL) { 1183 taop = &tao_noncached; 1184 bzero(taop, sizeof(*taop)); 1185 } 1186 1187 if ((thflags & TH_ACK) && 1188 (SEQ_LEQ(th->th_ack, tp->iss) || 1189 SEQ_GT(th->th_ack, tp->snd_max))) { 1190 /* 1191 * If we have a cached CCsent for the remote host, 1192 * hence we haven't just crashed and restarted, 1193 * do not send a RST. This may be a retransmission 1194 * from the other side after our earlier ACK was lost. 1195 * Our new SYN, when it arrives, will serve as the 1196 * needed ACK. 1197 */ 1198 if (taop->tao_ccsent != 0) 1199 goto drop; 1200 else { 1201 rstreason = BANDLIM_UNLIMITED; 1202 goto dropwithreset; 1203 } 1204 } 1205 if (thflags & TH_RST) { 1206 if (thflags & TH_ACK) 1207 tp = tcp_drop(tp, ECONNREFUSED); 1208 goto drop; 1209 } 1210 if ((thflags & TH_SYN) == 0) 1211 goto drop; 1212 tp->snd_wnd = th->th_win; /* initial send window */ 1213 tp->cc_recv = to.to_cc; /* foreign CC */ 1214 1215 tp->irs = th->th_seq; 1216 tcp_rcvseqinit(tp); 1217 if (thflags & TH_ACK) { 1218 /* 1219 * Our SYN was acked. If segment contains CC.ECHO 1220 * option, check it to make sure this segment really 1221 * matches our SYN. If not, just drop it as old 1222 * duplicate, but send an RST if we're still playing 1223 * by the old rules. If no CC.ECHO option, make sure 1224 * we don't get fooled into using T/TCP. 1225 */ 1226 if (to.to_flags & TOF_CCECHO) { 1227 if (tp->cc_send != to.to_ccecho) { 1228 if (taop->tao_ccsent != 0) 1229 goto drop; 1230 else { 1231 rstreason = BANDLIM_UNLIMITED; 1232 goto dropwithreset; 1233 } 1234 } 1235 } else 1236 tp->t_flags &= ~TF_RCVD_CC; 1237 tcpstat.tcps_connects++; 1238 soisconnected(so); 1239#ifdef MAC 1240 mac_set_socket_peer_from_mbuf(m, so); 1241#endif 1242 /* Do window scaling on this connection? */ 1243 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1244 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1245 tp->snd_scale = tp->requested_s_scale; 1246 tp->rcv_scale = tp->request_r_scale; 1247 } 1248 /* Segment is acceptable, update cache if undefined. */ 1249 if (taop->tao_ccsent == 0) 1250 taop->tao_ccsent = to.to_ccecho; 1251 1252 tp->rcv_adv += tp->rcv_wnd; 1253 tp->snd_una++; /* SYN is acked */ 1254 /* 1255 * If there's data, delay ACK; if there's also a FIN 1256 * ACKNOW will be turned on later. 1257 */ 1258 if (DELAY_ACK(tp) && tlen != 0) 1259 callout_reset(tp->tt_delack, tcp_delacktime, 1260 tcp_timer_delack, tp); 1261 else 1262 tp->t_flags |= TF_ACKNOW; 1263 /* 1264 * Received <SYN,ACK> in SYN_SENT[*] state. 1265 * Transitions: 1266 * SYN_SENT --> ESTABLISHED 1267 * SYN_SENT* --> FIN_WAIT_1 1268 */ 1269 tp->t_starttime = ticks; 1270 if (tp->t_flags & TF_NEEDFIN) { 1271 tp->t_state = TCPS_FIN_WAIT_1; 1272 tp->t_flags &= ~TF_NEEDFIN; 1273 thflags &= ~TH_SYN; 1274 } else { 1275 tp->t_state = TCPS_ESTABLISHED; 1276 callout_reset(tp->tt_keep, tcp_keepidle, 1277 tcp_timer_keep, tp); 1278 } 1279 } else { 1280 /* 1281 * Received initial SYN in SYN-SENT[*] state => 1282 * simultaneous open. If segment contains CC option 1283 * and there is a cached CC, apply TAO test. 1284 * If it succeeds, connection is * half-synchronized. 1285 * Otherwise, do 3-way handshake: 1286 * SYN-SENT -> SYN-RECEIVED 1287 * SYN-SENT* -> SYN-RECEIVED* 1288 * If there was no CC option, clear cached CC value. 1289 */ 1290 tp->t_flags |= TF_ACKNOW; 1291 callout_stop(tp->tt_rexmt); 1292 if (to.to_flags & TOF_CC) { 1293 if (taop->tao_cc != 0 && 1294 CC_GT(to.to_cc, taop->tao_cc)) { 1295 /* 1296 * update cache and make transition: 1297 * SYN-SENT -> ESTABLISHED* 1298 * SYN-SENT* -> FIN-WAIT-1* 1299 */ 1300 taop->tao_cc = to.to_cc; 1301 tp->t_starttime = ticks; 1302 if (tp->t_flags & TF_NEEDFIN) { 1303 tp->t_state = TCPS_FIN_WAIT_1; 1304 tp->t_flags &= ~TF_NEEDFIN; 1305 } else { 1306 tp->t_state = TCPS_ESTABLISHED; 1307 callout_reset(tp->tt_keep, 1308 tcp_keepidle, 1309 tcp_timer_keep, 1310 tp); 1311 } 1312 tp->t_flags |= TF_NEEDSYN; 1313 } else 1314 tp->t_state = TCPS_SYN_RECEIVED; 1315 } else { 1316 /* CC.NEW or no option => invalidate cache */ 1317 taop->tao_cc = 0; 1318 tp->t_state = TCPS_SYN_RECEIVED; 1319 } 1320 } 1321 1322trimthenstep6: 1323 /* 1324 * Advance th->th_seq to correspond to first data byte. 1325 * If data, trim to stay within window, 1326 * dropping FIN if necessary. 1327 */ 1328 th->th_seq++; 1329 if (tlen > tp->rcv_wnd) { 1330 todrop = tlen - tp->rcv_wnd; 1331 m_adj(m, -todrop); 1332 tlen = tp->rcv_wnd; 1333 thflags &= ~TH_FIN; 1334 tcpstat.tcps_rcvpackafterwin++; 1335 tcpstat.tcps_rcvbyteafterwin += todrop; 1336 } 1337 tp->snd_wl1 = th->th_seq - 1; 1338 tp->rcv_up = th->th_seq; 1339 /* 1340 * Client side of transaction: already sent SYN and data. 1341 * If the remote host used T/TCP to validate the SYN, 1342 * our data will be ACK'd; if so, enter normal data segment 1343 * processing in the middle of step 5, ack processing. 1344 * Otherwise, goto step 6. 1345 */ 1346 if (thflags & TH_ACK) 1347 goto process_ACK; 1348 1349 goto step6; 1350 1351 /* 1352 * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1353 * if segment contains a SYN and CC [not CC.NEW] option: 1354 * if state == TIME_WAIT and connection duration > MSL, 1355 * drop packet and send RST; 1356 * 1357 * if SEG.CC > CCrecv then is new SYN, and can implicitly 1358 * ack the FIN (and data) in retransmission queue. 1359 * Complete close and delete TCPCB. Then reprocess 1360 * segment, hoping to find new TCPCB in LISTEN state; 1361 * 1362 * else must be old SYN; drop it. 1363 * else do normal processing. 1364 */ 1365 case TCPS_LAST_ACK: 1366 case TCPS_CLOSING: 1367 case TCPS_TIME_WAIT: 1368 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1369 if ((thflags & TH_SYN) && 1370 (to.to_flags & TOF_CC) && tp->cc_recv != 0) { 1371 if (tp->t_state == TCPS_TIME_WAIT && 1372 (ticks - tp->t_starttime) > tcp_msl) { 1373 rstreason = BANDLIM_UNLIMITED; 1374 goto dropwithreset; 1375 } 1376 if (CC_GT(to.to_cc, tp->cc_recv)) { 1377 tp = tcp_close(tp); 1378 goto findpcb; 1379 } 1380 else 1381 goto drop; 1382 } 1383 break; /* continue normal processing */ 1384 } 1385 1386 /* 1387 * States other than LISTEN or SYN_SENT. 1388 * First check the RST flag and sequence number since reset segments 1389 * are exempt from the timestamp and connection count tests. This 1390 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 1391 * below which allowed reset segments in half the sequence space 1392 * to fall though and be processed (which gives forged reset 1393 * segments with a random sequence number a 50 percent chance of 1394 * killing a connection). 1395 * Then check timestamp, if present. 1396 * Then check the connection count, if present. 1397 * Then check that at least some bytes of segment are within 1398 * receive window. If segment begins before rcv_nxt, 1399 * drop leading data (and SYN); if nothing left, just ack. 1400 * 1401 * 1402 * If the RST bit is set, check the sequence number to see 1403 * if this is a valid reset segment. 1404 * RFC 793 page 37: 1405 * In all states except SYN-SENT, all reset (RST) segments 1406 * are validated by checking their SEQ-fields. A reset is 1407 * valid if its sequence number is in the window. 1408 * Note: this does not take into account delayed ACKs, so 1409 * we should test against last_ack_sent instead of rcv_nxt. 1410 * The sequence number in the reset segment is normally an 1411 * echo of our outgoing acknowlegement numbers, but some hosts 1412 * send a reset with the sequence number at the rightmost edge 1413 * of our receive window, and we have to handle this case. 1414 * If we have multiple segments in flight, the intial reset 1415 * segment sequence numbers will be to the left of last_ack_sent, 1416 * but they will eventually catch up. 1417 * In any case, it never made sense to trim reset segments to 1418 * fit the receive window since RFC 1122 says: 1419 * 4.2.2.12 RST Segment: RFC-793 Section 3.4 1420 * 1421 * A TCP SHOULD allow a received RST segment to include data. 1422 * 1423 * DISCUSSION 1424 * It has been suggested that a RST segment could contain 1425 * ASCII text that encoded and explained the cause of the 1426 * RST. No standard has yet been established for such 1427 * data. 1428 * 1429 * If the reset segment passes the sequence number test examine 1430 * the state: 1431 * SYN_RECEIVED STATE: 1432 * If passive open, return to LISTEN state. 1433 * If active open, inform user that connection was refused. 1434 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 1435 * Inform user that connection was reset, and close tcb. 1436 * CLOSING, LAST_ACK STATES: 1437 * Close the tcb. 1438 * TIME_WAIT STATE: 1439 * Drop the segment - see Stevens, vol. 2, p. 964 and 1440 * RFC 1337. 1441 */ 1442 if (thflags & TH_RST) { 1443 if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 1444 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 1445 switch (tp->t_state) { 1446 1447 case TCPS_SYN_RECEIVED: 1448 so->so_error = ECONNREFUSED; 1449 goto close; 1450 1451 case TCPS_ESTABLISHED: 1452 case TCPS_FIN_WAIT_1: 1453 case TCPS_FIN_WAIT_2: 1454 case TCPS_CLOSE_WAIT: 1455 so->so_error = ECONNRESET; 1456 close: 1457 tp->t_state = TCPS_CLOSED; 1458 tcpstat.tcps_drops++; 1459 tp = tcp_close(tp); 1460 break; 1461 1462 case TCPS_CLOSING: 1463 case TCPS_LAST_ACK: 1464 tp = tcp_close(tp); 1465 break; 1466 1467 case TCPS_TIME_WAIT: 1468 KASSERT(tp->t_state != TCPS_TIME_WAIT, 1469 ("timewait")); 1470 break; 1471 } 1472 } 1473 goto drop; 1474 } 1475 1476 /* 1477 * RFC 1323 PAWS: If we have a timestamp reply on this segment 1478 * and it's less than ts_recent, drop it. 1479 */ 1480 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 1481 TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1482 1483 /* Check to see if ts_recent is over 24 days old. */ 1484 if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1485 /* 1486 * Invalidate ts_recent. If this segment updates 1487 * ts_recent, the age will be reset later and ts_recent 1488 * will get a valid value. If it does not, setting 1489 * ts_recent to zero will at least satisfy the 1490 * requirement that zero be placed in the timestamp 1491 * echo reply when ts_recent isn't valid. The 1492 * age isn't reset until we get a valid ts_recent 1493 * because we don't want out-of-order segments to be 1494 * dropped when ts_recent is old. 1495 */ 1496 tp->ts_recent = 0; 1497 } else { 1498 tcpstat.tcps_rcvduppack++; 1499 tcpstat.tcps_rcvdupbyte += tlen; 1500 tcpstat.tcps_pawsdrop++; 1501 if (tlen) 1502 goto dropafterack; 1503 goto drop; 1504 } 1505 } 1506 1507 /* 1508 * T/TCP mechanism 1509 * If T/TCP was negotiated and the segment doesn't have CC, 1510 * or if its CC is wrong then drop the segment. 1511 * RST segments do not have to comply with this. 1512 */ 1513 if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 1514 ((to.to_flags & TOF_CC) == 0 || tp->cc_recv != to.to_cc)) 1515 goto dropafterack; 1516 1517 /* 1518 * In the SYN-RECEIVED state, validate that the packet belongs to 1519 * this connection before trimming the data to fit the receive 1520 * window. Check the sequence number versus IRS since we know 1521 * the sequence numbers haven't wrapped. This is a partial fix 1522 * for the "LAND" DoS attack. 1523 */ 1524 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1525 rstreason = BANDLIM_RST_OPENPORT; 1526 goto dropwithreset; 1527 } 1528 1529 todrop = tp->rcv_nxt - th->th_seq; 1530 if (todrop > 0) { 1531 if (thflags & TH_SYN) { 1532 thflags &= ~TH_SYN; 1533 th->th_seq++; 1534 if (th->th_urp > 1) 1535 th->th_urp--; 1536 else 1537 thflags &= ~TH_URG; 1538 todrop--; 1539 } 1540 /* 1541 * Following if statement from Stevens, vol. 2, p. 960. 1542 */ 1543 if (todrop > tlen 1544 || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1545 /* 1546 * Any valid FIN must be to the left of the window. 1547 * At this point the FIN must be a duplicate or out 1548 * of sequence; drop it. 1549 */ 1550 thflags &= ~TH_FIN; 1551 1552 /* 1553 * Send an ACK to resynchronize and drop any data. 1554 * But keep on processing for RST or ACK. 1555 */ 1556 tp->t_flags |= TF_ACKNOW; 1557 todrop = tlen; 1558 tcpstat.tcps_rcvduppack++; 1559 tcpstat.tcps_rcvdupbyte += todrop; 1560 } else { 1561 tcpstat.tcps_rcvpartduppack++; 1562 tcpstat.tcps_rcvpartdupbyte += todrop; 1563 } 1564 drop_hdrlen += todrop; /* drop from the top afterwards */ 1565 th->th_seq += todrop; 1566 tlen -= todrop; 1567 if (th->th_urp > todrop) 1568 th->th_urp -= todrop; 1569 else { 1570 thflags &= ~TH_URG; 1571 th->th_urp = 0; 1572 } 1573 } 1574 1575 /* 1576 * If new data are received on a connection after the 1577 * user processes are gone, then RST the other end. 1578 */ 1579 if ((so->so_state & SS_NOFDREF) && 1580 tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1581 tp = tcp_close(tp); 1582 tcpstat.tcps_rcvafterclose++; 1583 rstreason = BANDLIM_UNLIMITED; 1584 goto dropwithreset; 1585 } 1586 1587 /* 1588 * If segment ends after window, drop trailing data 1589 * (and PUSH and FIN); if nothing left, just ACK. 1590 */ 1591 todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1592 if (todrop > 0) { 1593 tcpstat.tcps_rcvpackafterwin++; 1594 if (todrop >= tlen) { 1595 tcpstat.tcps_rcvbyteafterwin += tlen; 1596 /* 1597 * If a new connection request is received 1598 * while in TIME_WAIT, drop the old connection 1599 * and start over if the sequence numbers 1600 * are above the previous ones. 1601 */ 1602 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1603 if (thflags & TH_SYN && 1604 tp->t_state == TCPS_TIME_WAIT && 1605 SEQ_GT(th->th_seq, tp->rcv_nxt)) { 1606 tp = tcp_close(tp); 1607 goto findpcb; 1608 } 1609 /* 1610 * If window is closed can only take segments at 1611 * window edge, and have to drop data and PUSH from 1612 * incoming segments. Continue processing, but 1613 * remember to ack. Otherwise, drop segment 1614 * and ack. 1615 */ 1616 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1617 tp->t_flags |= TF_ACKNOW; 1618 tcpstat.tcps_rcvwinprobe++; 1619 } else 1620 goto dropafterack; 1621 } else 1622 tcpstat.tcps_rcvbyteafterwin += todrop; 1623 m_adj(m, -todrop); 1624 tlen -= todrop; 1625 thflags &= ~(TH_PUSH|TH_FIN); 1626 } 1627 1628 /* 1629 * If last ACK falls within this segment's sequence numbers, 1630 * record its timestamp. 1631 * NOTE that the test is modified according to the latest 1632 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1633 */ 1634 if ((to.to_flags & TOF_TS) != 0 && 1635 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1636 tp->ts_recent_age = ticks; 1637 tp->ts_recent = to.to_tsval; 1638 } 1639 1640 /* 1641 * If a SYN is in the window, then this is an 1642 * error and we send an RST and drop the connection. 1643 */ 1644 if (thflags & TH_SYN) { 1645 tp = tcp_drop(tp, ECONNRESET); 1646 rstreason = BANDLIM_UNLIMITED; 1647 goto dropwithreset; 1648 } 1649 1650 /* 1651 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1652 * flag is on (half-synchronized state), then queue data for 1653 * later processing; else drop segment and return. 1654 */ 1655 if ((thflags & TH_ACK) == 0) { 1656 if (tp->t_state == TCPS_SYN_RECEIVED || 1657 (tp->t_flags & TF_NEEDSYN)) 1658 goto step6; 1659 else 1660 goto drop; 1661 } 1662 1663 /* 1664 * Ack processing. 1665 */ 1666 switch (tp->t_state) { 1667 1668 /* 1669 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1670 * ESTABLISHED state and continue processing. 1671 * The ACK was checked above. 1672 */ 1673 case TCPS_SYN_RECEIVED: 1674 1675 tcpstat.tcps_connects++; 1676 soisconnected(so); 1677 /* Do window scaling? */ 1678 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1679 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1680 tp->snd_scale = tp->requested_s_scale; 1681 tp->rcv_scale = tp->request_r_scale; 1682 } 1683 /* 1684 * Upon successful completion of 3-way handshake, 1685 * update cache.CC if it was undefined, pass any queued 1686 * data to the user, and advance state appropriately. 1687 */ 1688 if ((taop = tcp_gettaocache(&inp->inp_inc)) != NULL && 1689 taop->tao_cc == 0) 1690 taop->tao_cc = tp->cc_recv; 1691 1692 /* 1693 * Make transitions: 1694 * SYN-RECEIVED -> ESTABLISHED 1695 * SYN-RECEIVED* -> FIN-WAIT-1 1696 */ 1697 tp->t_starttime = ticks; 1698 if (tp->t_flags & TF_NEEDFIN) { 1699 tp->t_state = TCPS_FIN_WAIT_1; 1700 tp->t_flags &= ~TF_NEEDFIN; 1701 } else { 1702 tp->t_state = TCPS_ESTABLISHED; 1703 callout_reset(tp->tt_keep, tcp_keepidle, 1704 tcp_timer_keep, tp); 1705 } 1706 /* 1707 * If segment contains data or ACK, will call tcp_reass() 1708 * later; if not, do so now to pass queued data to user. 1709 */ 1710 if (tlen == 0 && (thflags & TH_FIN) == 0) 1711 (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1712 (struct mbuf *)0); 1713 tp->snd_wl1 = th->th_seq - 1; 1714 /* FALLTHROUGH */ 1715 1716 /* 1717 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1718 * ACKs. If the ack is in the range 1719 * tp->snd_una < th->th_ack <= tp->snd_max 1720 * then advance tp->snd_una to th->th_ack and drop 1721 * data from the retransmission queue. If this ACK reflects 1722 * more up to date window information we update our window information. 1723 */ 1724 case TCPS_ESTABLISHED: 1725 case TCPS_FIN_WAIT_1: 1726 case TCPS_FIN_WAIT_2: 1727 case TCPS_CLOSE_WAIT: 1728 case TCPS_CLOSING: 1729 case TCPS_LAST_ACK: 1730 case TCPS_TIME_WAIT: 1731 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1732 if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1733 if (tlen == 0 && tiwin == tp->snd_wnd) { 1734 tcpstat.tcps_rcvdupack++; 1735 /* 1736 * If we have outstanding data (other than 1737 * a window probe), this is a completely 1738 * duplicate ack (ie, window info didn't 1739 * change), the ack is the biggest we've 1740 * seen and we've seen exactly our rexmt 1741 * threshhold of them, assume a packet 1742 * has been dropped and retransmit it. 1743 * Kludge snd_nxt & the congestion 1744 * window so we send only this one 1745 * packet. 1746 * 1747 * We know we're losing at the current 1748 * window size so do congestion avoidance 1749 * (set ssthresh to half the current window 1750 * and pull our congestion window back to 1751 * the new ssthresh). 1752 * 1753 * Dup acks mean that packets have left the 1754 * network (they're now cached at the receiver) 1755 * so bump cwnd by the amount in the receiver 1756 * to keep a constant cwnd packets in the 1757 * network. 1758 */ 1759 if (!callout_active(tp->tt_rexmt) || 1760 th->th_ack != tp->snd_una) 1761 tp->t_dupacks = 0; 1762 else if (++tp->t_dupacks > tcprexmtthresh || 1763 (tcp_do_newreno && 1764 IN_FASTRECOVERY(tp))) { 1765 tp->snd_cwnd += tp->t_maxseg; 1766 (void) tcp_output(tp); 1767 goto drop; 1768 } else if (tp->t_dupacks == tcprexmtthresh) { 1769 tcp_seq onxt = tp->snd_nxt; 1770 u_int win; 1771 if (tcp_do_newreno && 1772 SEQ_LEQ(th->th_ack, 1773 tp->snd_recover)) { 1774 tp->t_dupacks = 0; 1775 break; 1776 } 1777 win = min(tp->snd_wnd, tp->snd_cwnd) / 1778 2 / tp->t_maxseg; 1779 if (win < 2) 1780 win = 2; 1781 tp->snd_ssthresh = win * tp->t_maxseg; 1782 ENTER_FASTRECOVERY(tp); 1783 tp->snd_recover = tp->snd_max; 1784 callout_stop(tp->tt_rexmt); 1785 tp->t_rtttime = 0; 1786 tp->snd_nxt = th->th_ack; 1787 tp->snd_cwnd = tp->t_maxseg; 1788 (void) tcp_output(tp); 1789 KASSERT(tp->snd_limited <= 2, 1790 ("tp->snd_limited too big")); 1791 tp->snd_cwnd = tp->snd_ssthresh + 1792 tp->t_maxseg * 1793 (tp->t_dupacks - tp->snd_limited); 1794 if (SEQ_GT(onxt, tp->snd_nxt)) 1795 tp->snd_nxt = onxt; 1796 goto drop; 1797 } else if (tcp_do_rfc3042) { 1798 u_long oldcwnd = tp->snd_cwnd; 1799 tcp_seq oldsndmax = tp->snd_max; 1800 u_int sent; 1801 KASSERT(tp->t_dupacks == 1 || 1802 tp->t_dupacks == 2, 1803 ("dupacks not 1 or 2")); 1804 if (tp->t_dupacks == 1) { 1805 tp->snd_limited = 0; 1806 tp->snd_cwnd += tp->t_maxseg; 1807 } else { 1808 tp->snd_cwnd += 1809 tp->t_maxseg * 2; 1810 } 1811 (void) tcp_output(tp); 1812 sent = tp->snd_max - oldsndmax; 1813 if (sent > tp->t_maxseg) { 1814 KASSERT(tp->snd_limited == 0 && 1815 tp->t_dupacks == 2, 1816 ("sent too much")); 1817 tp->snd_limited = 2; 1818 } else if (sent > 0) 1819 ++tp->snd_limited; 1820 tp->snd_cwnd = oldcwnd; 1821 goto drop; 1822 } 1823 } else 1824 tp->t_dupacks = 0; 1825 break; 1826 } 1827 1828 KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una")); 1829 1830 /* 1831 * If the congestion window was inflated to account 1832 * for the other side's cached packets, retract it. 1833 */ 1834 if (tcp_do_newreno) { 1835 if (IN_FASTRECOVERY(tp)) { 1836 if (SEQ_LT(th->th_ack, tp->snd_recover)) { 1837 tcp_newreno_partial_ack(tp, th); 1838 } else { 1839 /* 1840 * Window inflation should have left us 1841 * with approximately snd_ssthresh 1842 * outstanding data. 1843 * But in case we would be inclined to 1844 * send a burst, better to do it via 1845 * the slow start mechanism. 1846 */ 1847 if (SEQ_GT(th->th_ack + 1848 tp->snd_ssthresh, 1849 tp->snd_max)) 1850 tp->snd_cwnd = tp->snd_max - 1851 th->th_ack + 1852 tp->t_maxseg; 1853 else 1854 tp->snd_cwnd = tp->snd_ssthresh; 1855 } 1856 } 1857 } else { 1858 if (tp->t_dupacks >= tcprexmtthresh && 1859 tp->snd_cwnd > tp->snd_ssthresh) 1860 tp->snd_cwnd = tp->snd_ssthresh; 1861 } 1862 tp->t_dupacks = 0; 1863 if (SEQ_GT(th->th_ack, tp->snd_max)) { 1864 tcpstat.tcps_rcvacktoomuch++; 1865 goto dropafterack; 1866 } 1867 /* 1868 * If we reach this point, ACK is not a duplicate, 1869 * i.e., it ACKs something we sent. 1870 */ 1871 if (tp->t_flags & TF_NEEDSYN) { 1872 /* 1873 * T/TCP: Connection was half-synchronized, and our 1874 * SYN has been ACK'd (so connection is now fully 1875 * synchronized). Go to non-starred state, 1876 * increment snd_una for ACK of SYN, and check if 1877 * we can do window scaling. 1878 */ 1879 tp->t_flags &= ~TF_NEEDSYN; 1880 tp->snd_una++; 1881 /* Do window scaling? */ 1882 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1883 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1884 tp->snd_scale = tp->requested_s_scale; 1885 tp->rcv_scale = tp->request_r_scale; 1886 } 1887 } 1888 1889process_ACK: 1890 acked = th->th_ack - tp->snd_una; 1891 tcpstat.tcps_rcvackpack++; 1892 tcpstat.tcps_rcvackbyte += acked; 1893 1894 /* 1895 * If we just performed our first retransmit, and the ACK 1896 * arrives within our recovery window, then it was a mistake 1897 * to do the retransmit in the first place. Recover our 1898 * original cwnd and ssthresh, and proceed to transmit where 1899 * we left off. 1900 */ 1901 if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 1902 ++tcpstat.tcps_sndrexmitbad; 1903 tp->snd_cwnd = tp->snd_cwnd_prev; 1904 tp->snd_ssthresh = tp->snd_ssthresh_prev; 1905 tp->snd_recover = tp->snd_recover_prev; 1906 if (tp->t_flags & TF_WASFRECOVERY) 1907 ENTER_FASTRECOVERY(tp); 1908 tp->snd_nxt = tp->snd_max; 1909 tp->t_badrxtwin = 0; /* XXX probably not required */ 1910 } 1911 1912 /* 1913 * If we have a timestamp reply, update smoothed 1914 * round trip time. If no timestamp is present but 1915 * transmit timer is running and timed sequence 1916 * number was acked, update smoothed round trip time. 1917 * Since we now have an rtt measurement, cancel the 1918 * timer backoff (cf., Phil Karn's retransmit alg.). 1919 * Recompute the initial retransmit timer. 1920 * 1921 * Some boxes send broken timestamp replies 1922 * during the SYN+ACK phase, ignore 1923 * timestamps of 0 or we could calculate a 1924 * huge RTT and blow up the retransmit timer. 1925 */ 1926 if ((to.to_flags & TOF_TS) != 0 && 1927 to.to_tsecr) { 1928 tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 1929 } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 1930 tcp_xmit_timer(tp, ticks - tp->t_rtttime); 1931 } 1932 tcp_xmit_bandwidth_limit(tp, th->th_ack); 1933 1934 /* 1935 * If all outstanding data is acked, stop retransmit 1936 * timer and remember to restart (more output or persist). 1937 * If there is more data to be acked, restart retransmit 1938 * timer, using current (possibly backed-off) value. 1939 */ 1940 if (th->th_ack == tp->snd_max) { 1941 callout_stop(tp->tt_rexmt); 1942 needoutput = 1; 1943 } else if (!callout_active(tp->tt_persist)) 1944 callout_reset(tp->tt_rexmt, tp->t_rxtcur, 1945 tcp_timer_rexmt, tp); 1946 1947 /* 1948 * If no data (only SYN) was ACK'd, 1949 * skip rest of ACK processing. 1950 */ 1951 if (acked == 0) 1952 goto step6; 1953 1954 /* 1955 * When new data is acked, open the congestion window. 1956 * If the window gives us less than ssthresh packets 1957 * in flight, open exponentially (maxseg per packet). 1958 * Otherwise open linearly: maxseg per window 1959 * (maxseg^2 / cwnd per packet). 1960 */ 1961 if (!tcp_do_newreno || !IN_FASTRECOVERY(tp)) { 1962 register u_int cw = tp->snd_cwnd; 1963 register u_int incr = tp->t_maxseg; 1964 if (cw > tp->snd_ssthresh) 1965 incr = incr * incr / cw; 1966 tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 1967 } 1968 if (acked > so->so_snd.sb_cc) { 1969 tp->snd_wnd -= so->so_snd.sb_cc; 1970 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1971 ourfinisacked = 1; 1972 } else { 1973 sbdrop(&so->so_snd, acked); 1974 tp->snd_wnd -= acked; 1975 ourfinisacked = 0; 1976 } 1977 sowwakeup(so); 1978 /* detect una wraparound */ 1979 if (tcp_do_newreno && !IN_FASTRECOVERY(tp) && 1980 SEQ_GT(tp->snd_una, tp->snd_recover) && 1981 SEQ_LEQ(th->th_ack, tp->snd_recover)) 1982 tp->snd_recover = th->th_ack - 1; 1983 if (tcp_do_newreno && IN_FASTRECOVERY(tp) && 1984 SEQ_GEQ(th->th_ack, tp->snd_recover)) 1985 EXIT_FASTRECOVERY(tp); 1986 tp->snd_una = th->th_ack; 1987 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1988 tp->snd_nxt = tp->snd_una; 1989 1990 switch (tp->t_state) { 1991 1992 /* 1993 * In FIN_WAIT_1 STATE in addition to the processing 1994 * for the ESTABLISHED state if our FIN is now acknowledged 1995 * then enter FIN_WAIT_2. 1996 */ 1997 case TCPS_FIN_WAIT_1: 1998 if (ourfinisacked) { 1999 /* 2000 * If we can't receive any more 2001 * data, then closing user can proceed. 2002 * Starting the timer is contrary to the 2003 * specification, but if we don't get a FIN 2004 * we'll hang forever. 2005 */ 2006 /* XXXjl 2007 * we should release the tp also, and use a 2008 * compressed state. 2009 */ 2010 if (so->so_state & SS_CANTRCVMORE) { 2011 soisdisconnected(so); 2012 callout_reset(tp->tt_2msl, tcp_maxidle, 2013 tcp_timer_2msl, tp); 2014 } 2015 tp->t_state = TCPS_FIN_WAIT_2; 2016 } 2017 break; 2018 2019 /* 2020 * In CLOSING STATE in addition to the processing for 2021 * the ESTABLISHED state if the ACK acknowledges our FIN 2022 * then enter the TIME-WAIT state, otherwise ignore 2023 * the segment. 2024 */ 2025 case TCPS_CLOSING: 2026 if (ourfinisacked) { 2027 KASSERT(headlocked, ("headlocked")); 2028 tcp_twstart(tp); 2029 INP_INFO_WUNLOCK(&tcbinfo); 2030 m_freem(m); 2031 return; 2032 } 2033 break; 2034 2035 /* 2036 * In LAST_ACK, we may still be waiting for data to drain 2037 * and/or to be acked, as well as for the ack of our FIN. 2038 * If our FIN is now acknowledged, delete the TCB, 2039 * enter the closed state and return. 2040 */ 2041 case TCPS_LAST_ACK: 2042 if (ourfinisacked) { 2043 tp = tcp_close(tp); 2044 goto drop; 2045 } 2046 break; 2047 2048 /* 2049 * In TIME_WAIT state the only thing that should arrive 2050 * is a retransmission of the remote FIN. Acknowledge 2051 * it and restart the finack timer. 2052 */ 2053 case TCPS_TIME_WAIT: 2054 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 2055 callout_reset(tp->tt_2msl, 2 * tcp_msl, 2056 tcp_timer_2msl, tp); 2057 goto dropafterack; 2058 } 2059 } 2060 2061step6: 2062 /* 2063 * Update window information. 2064 * Don't look at window if no ACK: TAC's send garbage on first SYN. 2065 */ 2066 if ((thflags & TH_ACK) && 2067 (SEQ_LT(tp->snd_wl1, th->th_seq) || 2068 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2069 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2070 /* keep track of pure window updates */ 2071 if (tlen == 0 && 2072 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2073 tcpstat.tcps_rcvwinupd++; 2074 tp->snd_wnd = tiwin; 2075 tp->snd_wl1 = th->th_seq; 2076 tp->snd_wl2 = th->th_ack; 2077 if (tp->snd_wnd > tp->max_sndwnd) 2078 tp->max_sndwnd = tp->snd_wnd; 2079 needoutput = 1; 2080 } 2081 2082 /* 2083 * Process segments with URG. 2084 */ 2085 if ((thflags & TH_URG) && th->th_urp && 2086 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2087 /* 2088 * This is a kludge, but if we receive and accept 2089 * random urgent pointers, we'll crash in 2090 * soreceive. It's hard to imagine someone 2091 * actually wanting to send this much urgent data. 2092 */ 2093 if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2094 th->th_urp = 0; /* XXX */ 2095 thflags &= ~TH_URG; /* XXX */ 2096 goto dodata; /* XXX */ 2097 } 2098 /* 2099 * If this segment advances the known urgent pointer, 2100 * then mark the data stream. This should not happen 2101 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2102 * a FIN has been received from the remote side. 2103 * In these states we ignore the URG. 2104 * 2105 * According to RFC961 (Assigned Protocols), 2106 * the urgent pointer points to the last octet 2107 * of urgent data. We continue, however, 2108 * to consider it to indicate the first octet 2109 * of data past the urgent section as the original 2110 * spec states (in one of two places). 2111 */ 2112 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2113 tp->rcv_up = th->th_seq + th->th_urp; 2114 so->so_oobmark = so->so_rcv.sb_cc + 2115 (tp->rcv_up - tp->rcv_nxt) - 1; 2116 if (so->so_oobmark == 0) 2117 so->so_state |= SS_RCVATMARK; 2118 sohasoutofband(so); 2119 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2120 } 2121 /* 2122 * Remove out of band data so doesn't get presented to user. 2123 * This can happen independent of advancing the URG pointer, 2124 * but if two URG's are pending at once, some out-of-band 2125 * data may creep in... ick. 2126 */ 2127 if (th->th_urp <= (u_long)tlen && 2128 !(so->so_options & SO_OOBINLINE)) { 2129 /* hdr drop is delayed */ 2130 tcp_pulloutofband(so, th, m, drop_hdrlen); 2131 } 2132 } else { 2133 /* 2134 * If no out of band data is expected, 2135 * pull receive urgent pointer along 2136 * with the receive window. 2137 */ 2138 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2139 tp->rcv_up = tp->rcv_nxt; 2140 } 2141dodata: /* XXX */ 2142 KASSERT(headlocked, ("headlocked")); 2143 /* 2144 * Process the segment text, merging it into the TCP sequencing queue, 2145 * and arranging for acknowledgment of receipt if necessary. 2146 * This process logically involves adjusting tp->rcv_wnd as data 2147 * is presented to the user (this happens in tcp_usrreq.c, 2148 * case PRU_RCVD). If a FIN has already been received on this 2149 * connection then we just ignore the text. 2150 */ 2151 if ((tlen || (thflags & TH_FIN)) && 2152 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2153 m_adj(m, drop_hdrlen); /* delayed header drop */ 2154 /* 2155 * Insert segment which includes th into TCP reassembly queue 2156 * with control block tp. Set thflags to whether reassembly now 2157 * includes a segment with FIN. This handles the common case 2158 * inline (segment is the next to be received on an established 2159 * connection, and the queue is empty), avoiding linkage into 2160 * and removal from the queue and repetition of various 2161 * conversions. 2162 * Set DELACK for segments received in order, but ack 2163 * immediately when segments are out of order (so 2164 * fast retransmit can work). 2165 */ 2166 if (th->th_seq == tp->rcv_nxt && 2167 LIST_EMPTY(&tp->t_segq) && 2168 TCPS_HAVEESTABLISHED(tp->t_state)) { 2169 if (DELAY_ACK(tp)) 2170 tp->t_flags |= TF_DELACK; 2171 else 2172 tp->t_flags |= TF_ACKNOW; 2173 tp->rcv_nxt += tlen; 2174 thflags = th->th_flags & TH_FIN; 2175 tcpstat.tcps_rcvpack++; 2176 tcpstat.tcps_rcvbyte += tlen; 2177 ND6_HINT(tp); 2178 if (so->so_state & SS_CANTRCVMORE) 2179 m_freem(m); 2180 else
|
2181 sbappend(&so->so_rcv, m);
| 2181 sbappendstream(&so->so_rcv, m);
|
2182 sorwakeup(so); 2183 } else { 2184 thflags = tcp_reass(tp, th, &tlen, m); 2185 tp->t_flags |= TF_ACKNOW; 2186 } 2187 2188 /* 2189 * Note the amount of data that peer has sent into 2190 * our window, in order to estimate the sender's 2191 * buffer size. 2192 */ 2193 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2194 } else { 2195 m_freem(m); 2196 thflags &= ~TH_FIN; 2197 } 2198 2199 /* 2200 * If FIN is received ACK the FIN and let the user know 2201 * that the connection is closing. 2202 */ 2203 if (thflags & TH_FIN) { 2204 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2205 socantrcvmore(so); 2206 /* 2207 * If connection is half-synchronized 2208 * (ie NEEDSYN flag on) then delay ACK, 2209 * so it may be piggybacked when SYN is sent. 2210 * Otherwise, since we received a FIN then no 2211 * more input can be expected, send ACK now. 2212 */ 2213 if (tp->t_flags & TF_NEEDSYN) 2214 tp->t_flags |= TF_DELACK; 2215 else 2216 tp->t_flags |= TF_ACKNOW; 2217 tp->rcv_nxt++; 2218 } 2219 switch (tp->t_state) { 2220 2221 /* 2222 * In SYN_RECEIVED and ESTABLISHED STATES 2223 * enter the CLOSE_WAIT state. 2224 */ 2225 case TCPS_SYN_RECEIVED: 2226 tp->t_starttime = ticks; 2227 /*FALLTHROUGH*/ 2228 case TCPS_ESTABLISHED: 2229 tp->t_state = TCPS_CLOSE_WAIT; 2230 break; 2231 2232 /* 2233 * If still in FIN_WAIT_1 STATE FIN has not been acked so 2234 * enter the CLOSING state. 2235 */ 2236 case TCPS_FIN_WAIT_1: 2237 tp->t_state = TCPS_CLOSING; 2238 break; 2239 2240 /* 2241 * In FIN_WAIT_2 state enter the TIME_WAIT state, 2242 * starting the time-wait timer, turning off the other 2243 * standard timers. 2244 */ 2245 case TCPS_FIN_WAIT_2: 2246 KASSERT(headlocked == 1, ("headlocked should be 1")); 2247 tcp_twstart(tp); 2248 INP_INFO_WUNLOCK(&tcbinfo); 2249 return; 2250 2251 /* 2252 * In TIME_WAIT state restart the 2 MSL time_wait timer. 2253 */ 2254 case TCPS_TIME_WAIT: 2255 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 2256 callout_reset(tp->tt_2msl, 2 * tcp_msl, 2257 tcp_timer_2msl, tp); 2258 break; 2259 } 2260 } 2261 INP_INFO_WUNLOCK(&tcbinfo); 2262#ifdef TCPDEBUG 2263 if (so->so_options & SO_DEBUG) 2264 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2265 &tcp_savetcp, 0); 2266#endif 2267 2268 /* 2269 * Return any desired output. 2270 */ 2271 if (needoutput || (tp->t_flags & TF_ACKNOW)) 2272 (void) tcp_output(tp); 2273 2274check_delack: 2275 if (tp->t_flags & TF_DELACK) { 2276 tp->t_flags &= ~TF_DELACK; 2277 callout_reset(tp->tt_delack, tcp_delacktime, 2278 tcp_timer_delack, tp); 2279 } 2280 INP_UNLOCK(inp); 2281 return; 2282 2283dropafterack: 2284 /* 2285 * Generate an ACK dropping incoming segment if it occupies 2286 * sequence space, where the ACK reflects our state. 2287 * 2288 * We can now skip the test for the RST flag since all 2289 * paths to this code happen after packets containing 2290 * RST have been dropped. 2291 * 2292 * In the SYN-RECEIVED state, don't send an ACK unless the 2293 * segment we received passes the SYN-RECEIVED ACK test. 2294 * If it fails send a RST. This breaks the loop in the 2295 * "LAND" DoS attack, and also prevents an ACK storm 2296 * between two listening ports that have been sent forged 2297 * SYN segments, each with the source address of the other. 2298 */ 2299 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2300 (SEQ_GT(tp->snd_una, th->th_ack) || 2301 SEQ_GT(th->th_ack, tp->snd_max)) ) { 2302 rstreason = BANDLIM_RST_OPENPORT; 2303 goto dropwithreset; 2304 } 2305#ifdef TCPDEBUG 2306 if (so->so_options & SO_DEBUG) 2307 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2308 &tcp_savetcp, 0); 2309#endif 2310 KASSERT(headlocked, ("headlocked should be 1")); 2311 INP_INFO_WUNLOCK(&tcbinfo); 2312 m_freem(m); 2313 tp->t_flags |= TF_ACKNOW; 2314 (void) tcp_output(tp); 2315 INP_UNLOCK(inp); 2316 return; 2317 2318dropwithreset: 2319 /* 2320 * Generate a RST, dropping incoming segment. 2321 * Make ACK acceptable to originator of segment. 2322 * Don't bother to respond if destination was broadcast/multicast. 2323 */ 2324 if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2325 goto drop; 2326 if (isipv6) { 2327 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2328 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2329 goto drop; 2330 } else { 2331 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2332 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2333 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 2334 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2335 goto drop; 2336 } 2337 /* IPv6 anycast check is done at tcp6_input() */ 2338 2339 /* 2340 * Perform bandwidth limiting. 2341 */ 2342 if (badport_bandlim(rstreason) < 0) 2343 goto drop; 2344 2345#ifdef TCPDEBUG 2346 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2347 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2348 &tcp_savetcp, 0); 2349#endif 2350 2351 if (tp) 2352 INP_UNLOCK(inp); 2353 2354 if (thflags & TH_ACK) 2355 /* mtod() below is safe as long as hdr dropping is delayed */ 2356 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack, 2357 TH_RST); 2358 else { 2359 if (thflags & TH_SYN) 2360 tlen++; 2361 /* mtod() below is safe as long as hdr dropping is delayed */ 2362 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2363 (tcp_seq)0, TH_RST|TH_ACK); 2364 } 2365 if (headlocked) 2366 INP_INFO_WUNLOCK(&tcbinfo); 2367 return; 2368 2369drop: 2370 /* 2371 * Drop space held by incoming segment and return. 2372 */ 2373#ifdef TCPDEBUG 2374 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2375 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2376 &tcp_savetcp, 0); 2377#endif 2378 if (tp) 2379 INP_UNLOCK(inp); 2380 m_freem(m); 2381 if (headlocked) 2382 INP_INFO_WUNLOCK(&tcbinfo); 2383 return; 2384} 2385 2386/* 2387 * Parse TCP options and place in tcpopt. 2388 */ 2389static void 2390tcp_dooptions(to, cp, cnt, is_syn) 2391 struct tcpopt *to; 2392 u_char *cp; 2393 int cnt; 2394 int is_syn; 2395{ 2396 int opt, optlen; 2397 2398 to->to_flags = 0; 2399 for (; cnt > 0; cnt -= optlen, cp += optlen) { 2400 opt = cp[0]; 2401 if (opt == TCPOPT_EOL) 2402 break; 2403 if (opt == TCPOPT_NOP) 2404 optlen = 1; 2405 else { 2406 if (cnt < 2) 2407 break; 2408 optlen = cp[1]; 2409 if (optlen < 2 || optlen > cnt) 2410 break; 2411 } 2412 switch (opt) { 2413 case TCPOPT_MAXSEG: 2414 if (optlen != TCPOLEN_MAXSEG) 2415 continue; 2416 if (!is_syn) 2417 continue; 2418 to->to_flags |= TOF_MSS; 2419 bcopy((char *)cp + 2, 2420 (char *)&to->to_mss, sizeof(to->to_mss)); 2421 to->to_mss = ntohs(to->to_mss); 2422 break; 2423 case TCPOPT_WINDOW: 2424 if (optlen != TCPOLEN_WINDOW) 2425 continue; 2426 if (! is_syn) 2427 continue; 2428 to->to_flags |= TOF_SCALE; 2429 to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 2430 break; 2431 case TCPOPT_TIMESTAMP: 2432 if (optlen != TCPOLEN_TIMESTAMP) 2433 continue; 2434 to->to_flags |= TOF_TS; 2435 bcopy((char *)cp + 2, 2436 (char *)&to->to_tsval, sizeof(to->to_tsval)); 2437 to->to_tsval = ntohl(to->to_tsval); 2438 bcopy((char *)cp + 6, 2439 (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2440 to->to_tsecr = ntohl(to->to_tsecr); 2441 break; 2442 case TCPOPT_CC: 2443 if (optlen != TCPOLEN_CC) 2444 continue; 2445 to->to_flags |= TOF_CC; 2446 bcopy((char *)cp + 2, 2447 (char *)&to->to_cc, sizeof(to->to_cc)); 2448 to->to_cc = ntohl(to->to_cc); 2449 break; 2450 case TCPOPT_CCNEW: 2451 if (optlen != TCPOLEN_CC) 2452 continue; 2453 if (!is_syn) 2454 continue; 2455 to->to_flags |= TOF_CCNEW; 2456 bcopy((char *)cp + 2, 2457 (char *)&to->to_cc, sizeof(to->to_cc)); 2458 to->to_cc = ntohl(to->to_cc); 2459 break; 2460 case TCPOPT_CCECHO: 2461 if (optlen != TCPOLEN_CC) 2462 continue; 2463 if (!is_syn) 2464 continue; 2465 to->to_flags |= TOF_CCECHO; 2466 bcopy((char *)cp + 2, 2467 (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 2468 to->to_ccecho = ntohl(to->to_ccecho); 2469 break; 2470 default: 2471 continue; 2472 } 2473 } 2474} 2475 2476/* 2477 * Pull out of band byte out of a segment so 2478 * it doesn't appear in the user's data queue. 2479 * It is still reflected in the segment length for 2480 * sequencing purposes. 2481 */ 2482static void 2483tcp_pulloutofband(so, th, m, off) 2484 struct socket *so; 2485 struct tcphdr *th; 2486 register struct mbuf *m; 2487 int off; /* delayed to be droped hdrlen */ 2488{ 2489 int cnt = off + th->th_urp - 1; 2490 2491 while (cnt >= 0) { 2492 if (m->m_len > cnt) { 2493 char *cp = mtod(m, caddr_t) + cnt; 2494 struct tcpcb *tp = sototcpcb(so); 2495 2496 tp->t_iobc = *cp; 2497 tp->t_oobflags |= TCPOOB_HAVEDATA; 2498 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2499 m->m_len--; 2500 if (m->m_flags & M_PKTHDR) 2501 m->m_pkthdr.len--; 2502 return; 2503 } 2504 cnt -= m->m_len; 2505 m = m->m_next; 2506 if (m == 0) 2507 break; 2508 } 2509 panic("tcp_pulloutofband"); 2510} 2511 2512/* 2513 * Collect new round-trip time estimate 2514 * and update averages and current timeout. 2515 */ 2516static void 2517tcp_xmit_timer(tp, rtt) 2518 register struct tcpcb *tp; 2519 int rtt; 2520{ 2521 register int delta; 2522 2523 tcpstat.tcps_rttupdated++; 2524 tp->t_rttupdated++; 2525 if (tp->t_srtt != 0) { 2526 /* 2527 * srtt is stored as fixed point with 5 bits after the 2528 * binary point (i.e., scaled by 8). The following magic 2529 * is equivalent to the smoothing algorithm in rfc793 with 2530 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2531 * point). Adjust rtt to origin 0. 2532 */ 2533 delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2534 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2535 2536 if ((tp->t_srtt += delta) <= 0) 2537 tp->t_srtt = 1; 2538 2539 /* 2540 * We accumulate a smoothed rtt variance (actually, a 2541 * smoothed mean difference), then set the retransmit 2542 * timer to smoothed rtt + 4 times the smoothed variance. 2543 * rttvar is stored as fixed point with 4 bits after the 2544 * binary point (scaled by 16). The following is 2545 * equivalent to rfc793 smoothing with an alpha of .75 2546 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2547 * rfc793's wired-in beta. 2548 */ 2549 if (delta < 0) 2550 delta = -delta; 2551 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2552 if ((tp->t_rttvar += delta) <= 0) 2553 tp->t_rttvar = 1; 2554 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 2555 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2556 } else { 2557 /* 2558 * No rtt measurement yet - use the unsmoothed rtt. 2559 * Set the variance to half the rtt (so our first 2560 * retransmit happens at 3*rtt). 2561 */ 2562 tp->t_srtt = rtt << TCP_RTT_SHIFT; 2563 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 2564 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2565 } 2566 tp->t_rtttime = 0; 2567 tp->t_rxtshift = 0; 2568 2569 /* 2570 * the retransmit should happen at rtt + 4 * rttvar. 2571 * Because of the way we do the smoothing, srtt and rttvar 2572 * will each average +1/2 tick of bias. When we compute 2573 * the retransmit timer, we want 1/2 tick of rounding and 2574 * 1 extra tick because of +-1/2 tick uncertainty in the 2575 * firing of the timer. The bias will give us exactly the 2576 * 1.5 tick we need. But, because the bias is 2577 * statistical, we have to test that we don't drop below 2578 * the minimum feasible timer (which is 2 ticks). 2579 */ 2580 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 2581 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2582 2583 /* 2584 * We received an ack for a packet that wasn't retransmitted; 2585 * it is probably safe to discard any error indications we've 2586 * received recently. This isn't quite right, but close enough 2587 * for now (a route might have failed after we sent a segment, 2588 * and the return path might not be symmetrical). 2589 */ 2590 tp->t_softerror = 0; 2591} 2592 2593/* 2594 * Determine a reasonable value for maxseg size. 2595 * If the route is known, check route for mtu. 2596 * If none, use an mss that can be handled on the outgoing 2597 * interface without forcing IP to fragment; if bigger than 2598 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2599 * to utilize large mbufs. If no route is found, route has no mtu, 2600 * or the destination isn't local, use a default, hopefully conservative 2601 * size (usually 512 or the default IP max size, but no more than the mtu 2602 * of the interface), as we can't discover anything about intervening 2603 * gateways or networks. We also initialize the congestion/slow start 2604 * window to be a single segment if the destination isn't local. 2605 * While looking at the routing entry, we also initialize other path-dependent 2606 * parameters from pre-set or cached values in the routing entry. 2607 * 2608 * Also take into account the space needed for options that we 2609 * send regularly. Make maxseg shorter by that amount to assure 2610 * that we can send maxseg amount of data even when the options 2611 * are present. Store the upper limit of the length of options plus 2612 * data in maxopd. 2613 * 2614 * NOTE that this routine is only called when we process an incoming 2615 * segment, for outgoing segments only tcp_mssopt is called. 2616 * 2617 * In case of T/TCP, we call this routine during implicit connection 2618 * setup as well (offer = -1), to initialize maxseg from the cached 2619 * MSS of our peer. 2620 */ 2621void 2622tcp_mss(tp, offer) 2623 struct tcpcb *tp; 2624 int offer; 2625{ 2626 register struct rtentry *rt; 2627 struct ifnet *ifp; 2628 register int rtt, mss; 2629 u_long bufsize; 2630 struct inpcb *inp = tp->t_inpcb; 2631 struct socket *so; 2632 struct rmxp_tao *taop; 2633 int origoffer = offer; 2634#ifdef INET6 2635 int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2636 size_t min_protoh = isipv6 ? 2637 sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2638 sizeof (struct tcpiphdr); 2639#else 2640 const int isipv6 = 0; 2641 const size_t min_protoh = sizeof (struct tcpiphdr); 2642#endif 2643 2644 if (isipv6) 2645 rt = tcp_rtlookup6(&inp->inp_inc); 2646 else 2647 rt = tcp_rtlookup(&inp->inp_inc); 2648 if (rt == NULL) { 2649 tp->t_maxopd = tp->t_maxseg = 2650 isipv6 ? tcp_v6mssdflt : tcp_mssdflt; 2651 return; 2652 } 2653 ifp = rt->rt_ifp; 2654 so = inp->inp_socket; 2655 2656 taop = rmx_taop(rt->rt_rmx); 2657 /* 2658 * Offer == -1 means that we didn't receive SYN yet, 2659 * use cached value in that case; 2660 */ 2661 if (offer == -1) 2662 offer = taop->tao_mssopt; 2663 /* 2664 * Offer == 0 means that there was no MSS on the SYN segment, 2665 * in this case we use tcp_mssdflt. 2666 */ 2667 if (offer == 0) 2668 offer = isipv6 ? tcp_v6mssdflt : tcp_mssdflt; 2669 else 2670 /* 2671 * Sanity check: make sure that maxopd will be large 2672 * enough to allow some data on segments even is the 2673 * all the option space is used (40bytes). Otherwise 2674 * funny things may happen in tcp_output. 2675 */ 2676 offer = max(offer, 64); 2677 taop->tao_mssopt = offer; 2678 2679 /* 2680 * While we're here, check if there's an initial rtt 2681 * or rttvar. Convert from the route-table units 2682 * to scaled multiples of the slow timeout timer. 2683 */ 2684 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 2685 /* 2686 * XXX the lock bit for RTT indicates that the value 2687 * is also a minimum value; this is subject to time. 2688 */ 2689 if (rt->rt_rmx.rmx_locks & RTV_RTT) 2690 tp->t_rttmin = rtt / (RTM_RTTUNIT / hz); 2691 tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE)); 2692 tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 2693 tcpstat.tcps_usedrtt++; 2694 if (rt->rt_rmx.rmx_rttvar) { 2695 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 2696 (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE)); 2697 tcpstat.tcps_usedrttvar++; 2698 } else { 2699 /* default variation is +- 1 rtt */ 2700 tp->t_rttvar = 2701 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 2702 } 2703 TCPT_RANGESET(tp->t_rxtcur, 2704 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 2705 tp->t_rttmin, TCPTV_REXMTMAX); 2706 } 2707 /* 2708 * if there's an mtu associated with the route, use it 2709 * else, use the link mtu. 2710 */ 2711 if (rt->rt_rmx.rmx_mtu) 2712 mss = rt->rt_rmx.rmx_mtu - min_protoh; 2713 else { 2714#ifdef INET6 2715 mss = (isipv6 ? IN6_LINKMTU(rt->rt_ifp) : ifp->if_mtu) 2716 - min_protoh; 2717#else 2718 mss = ifp->if_mtu - min_protoh; 2719#endif 2720#ifdef INET6 2721 if (isipv6) { 2722 if (!in6_localaddr(&inp->in6p_faddr)) 2723 mss = min(mss, tcp_v6mssdflt); 2724 } else 2725#endif 2726 if (!in_localaddr(inp->inp_faddr)) 2727 mss = min(mss, tcp_mssdflt); 2728 } 2729 mss = min(mss, offer); 2730 /* 2731 * maxopd stores the maximum length of data AND options 2732 * in a segment; maxseg is the amount of data in a normal 2733 * segment. We need to store this value (maxopd) apart 2734 * from maxseg, because now every segment carries options 2735 * and thus we normally have somewhat less data in segments. 2736 */ 2737 tp->t_maxopd = mss; 2738 2739 /* 2740 * In case of T/TCP, origoffer==-1 indicates, that no segments 2741 * were received yet. In this case we just guess, otherwise 2742 * we do the same as before T/TCP. 2743 */ 2744 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2745 (origoffer == -1 || 2746 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2747 mss -= TCPOLEN_TSTAMP_APPA; 2748 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2749 (origoffer == -1 || 2750 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2751 mss -= TCPOLEN_CC_APPA; 2752 2753#if (MCLBYTES & (MCLBYTES - 1)) == 0 2754 if (mss > MCLBYTES) 2755 mss &= ~(MCLBYTES-1); 2756#else 2757 if (mss > MCLBYTES) 2758 mss = mss / MCLBYTES * MCLBYTES; 2759#endif 2760 /* 2761 * If there's a pipesize, change the socket buffer 2762 * to that size. Make the socket buffers an integral 2763 * number of mss units; if the mss is larger than 2764 * the socket buffer, decrease the mss. 2765 */ 2766#ifdef RTV_SPIPE 2767 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 2768#endif 2769 bufsize = so->so_snd.sb_hiwat; 2770 if (bufsize < mss) 2771 mss = bufsize; 2772 else { 2773 bufsize = roundup(bufsize, mss); 2774 if (bufsize > sb_max) 2775 bufsize = sb_max; 2776 if (bufsize > so->so_snd.sb_hiwat) 2777 (void)sbreserve(&so->so_snd, bufsize, so, NULL); 2778 } 2779 tp->t_maxseg = mss; 2780 2781#ifdef RTV_RPIPE 2782 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 2783#endif 2784 bufsize = so->so_rcv.sb_hiwat; 2785 if (bufsize > mss) { 2786 bufsize = roundup(bufsize, mss); 2787 if (bufsize > sb_max) 2788 bufsize = sb_max; 2789 if (bufsize > so->so_rcv.sb_hiwat) 2790 (void)sbreserve(&so->so_rcv, bufsize, so, NULL); 2791 } 2792 2793 /* 2794 * Set the slow-start flight size depending on whether this 2795 * is a local network or not. 2796 */ 2797 if (tcp_do_rfc3390) 2798 tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380)); 2799 else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 2800 (!isipv6 && in_localaddr(inp->inp_faddr))) 2801 tp->snd_cwnd = mss * ss_fltsz_local; 2802 else 2803 tp->snd_cwnd = mss * ss_fltsz; 2804 2805 if (rt->rt_rmx.rmx_ssthresh) { 2806 /* 2807 * There's some sort of gateway or interface 2808 * buffer limit on the path. Use this to set 2809 * the slow start threshhold, but set the 2810 * threshold to no less than 2*mss. 2811 */ 2812 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 2813 tcpstat.tcps_usedssthresh++; 2814 } 2815} 2816 2817/* 2818 * Determine the MSS option to send on an outgoing SYN. 2819 */ 2820int 2821tcp_mssopt(tp) 2822 struct tcpcb *tp; 2823{ 2824 struct rtentry *rt; 2825#ifdef INET6 2826 int isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2827 size_t min_protoh = isipv6 ? 2828 sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2829 sizeof (struct tcpiphdr); 2830#else 2831 const int isipv6 = 0; 2832 const size_t min_protoh = sizeof (struct tcpiphdr); 2833#endif 2834 2835 if (isipv6) 2836 rt = tcp_rtlookup6(&tp->t_inpcb->inp_inc); 2837 else 2838 rt = tcp_rtlookup(&tp->t_inpcb->inp_inc); 2839 if (rt == NULL) 2840 return (isipv6 ? tcp_v6mssdflt : tcp_mssdflt); 2841 2842#ifdef INET6 2843 return (isipv6 ? IN6_LINKMTU(rt->rt_ifp) : 2844 rt->rt_ifp->if_mtu - min_protoh); 2845#else 2846 return (rt->rt_ifp->if_mtu - min_protoh); 2847#endif 2848} 2849 2850 2851/* 2852 * On a partial ack arrives, force the retransmission of the 2853 * next unacknowledged segment. Do not clear tp->t_dupacks. 2854 * By setting snd_nxt to ti_ack, this forces retransmission timer to 2855 * be started again. 2856 */ 2857static void 2858tcp_newreno_partial_ack(tp, th) 2859 struct tcpcb *tp; 2860 struct tcphdr *th; 2861{ 2862 tcp_seq onxt = tp->snd_nxt; 2863 u_long ocwnd = tp->snd_cwnd; 2864 2865 callout_stop(tp->tt_rexmt); 2866 tp->t_rtttime = 0; 2867 tp->snd_nxt = th->th_ack; 2868 /* 2869 * Set snd_cwnd to one segment beyond acknowledged offset. 2870 * (tp->snd_una has not yet been updated when this function is called.) 2871 */ 2872 tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 2873 tp->t_flags |= TF_ACKNOW; 2874 (void) tcp_output(tp); 2875 tp->snd_cwnd = ocwnd; 2876 if (SEQ_GT(onxt, tp->snd_nxt)) 2877 tp->snd_nxt = onxt; 2878 /* 2879 * Partial window deflation. Relies on fact that tp->snd_una 2880 * not updated yet. 2881 */ 2882 tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg); 2883} 2884 2885/* 2886 * Returns 1 if the TIME_WAIT state was killed and we should start over, 2887 * looking for a pcb in the listen state. Returns 0 otherwise. 2888 */ 2889static int 2890tcp_timewait(tw, to, th, m, tlen) 2891 struct tcptw *tw; 2892 struct tcpopt *to; 2893 struct tcphdr *th; 2894 struct mbuf *m; 2895 int tlen; 2896{ 2897 int thflags; 2898 tcp_seq seq; 2899#ifdef INET6 2900 int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 2901#else 2902 const int isipv6 = 0; 2903#endif 2904 2905 thflags = th->th_flags; 2906 2907 /* 2908 * NOTE: for FIN_WAIT_2 (to be added later), 2909 * must validate sequence number before accepting RST 2910 */ 2911 2912 /* 2913 * If the segment contains RST: 2914 * Drop the segment - see Stevens, vol. 2, p. 964 and 2915 * RFC 1337. 2916 */ 2917 if (thflags & TH_RST) 2918 goto drop; 2919 2920 /* 2921 * If segment contains a SYN and CC [not CC.NEW] option: 2922 * if connection duration > MSL, drop packet and send RST; 2923 * 2924 * if SEG.CC > CCrecv then is new SYN. 2925 * Complete close and delete TCPCB. Then reprocess 2926 * segment, hoping to find new TCPCB in LISTEN state; 2927 * 2928 * else must be old SYN; drop it. 2929 * else do normal processing. 2930 */ 2931 if ((thflags & TH_SYN) && (to->to_flags & TOF_CC) && tw->cc_recv != 0) { 2932 if ((ticks - tw->t_starttime) > tcp_msl) 2933 goto reset; 2934 if (CC_GT(to->to_cc, tw->cc_recv)) { 2935 (void) tcp_twclose(tw, 0); 2936 return (1); 2937 } 2938 goto drop; 2939 } 2940 2941#if 0 2942/* PAWS not needed at the moment */ 2943 /* 2944 * RFC 1323 PAWS: If we have a timestamp reply on this segment 2945 * and it's less than ts_recent, drop it. 2946 */ 2947 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 2948 TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2949 if ((thflags & TH_ACK) == 0) 2950 goto drop; 2951 goto ack; 2952 } 2953 /* 2954 * ts_recent is never updated because we never accept new segments. 2955 */ 2956#endif 2957 2958 /* 2959 * If a new connection request is received 2960 * while in TIME_WAIT, drop the old connection 2961 * and start over if the sequence numbers 2962 * are above the previous ones. 2963 */ 2964 if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) { 2965 (void) tcp_twclose(tw, 0); 2966 return (1); 2967 } 2968 2969 /* 2970 * Drop the the segment if it does not contain an ACK. 2971 */ 2972 if ((thflags & TH_ACK) == 0) 2973 goto drop; 2974 2975 /* 2976 * Reset the 2MSL timer if this is a duplicate FIN. 2977 */ 2978 if (thflags & TH_FIN) { 2979 seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0); 2980 if (seq + 1 == tw->rcv_nxt) 2981 tcp_timer_2msl_reset(tw, 2 * tcp_msl); 2982 } 2983 2984 /* 2985 * Acknowledge the segment if it has data or is not a duplicate ACK. 2986 */ 2987 if (thflags != TH_ACK || tlen != 0 || 2988 th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt) 2989 tcp_twrespond(tw, NULL, m, TH_ACK); 2990 goto drop; 2991 2992reset: 2993 /* 2994 * Generate a RST, dropping incoming segment. 2995 * Make ACK acceptable to originator of segment. 2996 * Don't bother to respond if destination was broadcast/multicast. 2997 */ 2998 if (m->m_flags & (M_BCAST|M_MCAST)) 2999 goto drop; 3000 if (isipv6) { 3001 struct ip6_hdr *ip6; 3002 3003 /* IPv6 anycast check is done at tcp6_input() */ 3004 ip6 = mtod(m, struct ip6_hdr *); 3005 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3006 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3007 goto drop; 3008 } else { 3009 struct ip *ip; 3010 3011 ip = mtod(m, struct ip *); 3012 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3013 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3014 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3015 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3016 goto drop; 3017 } 3018 if (thflags & TH_ACK) { 3019 tcp_respond(NULL, 3020 mtod(m, void *), th, m, 0, th->th_ack, TH_RST); 3021 } else { 3022 seq = th->th_seq + (thflags & TH_SYN ? 1 : 0); 3023 tcp_respond(NULL, 3024 mtod(m, void *), th, m, seq, 0, TH_RST|TH_ACK); 3025 } 3026 INP_UNLOCK(tw->tw_inpcb); 3027 return (0); 3028 3029drop: 3030 INP_UNLOCK(tw->tw_inpcb); 3031 m_freem(m); 3032 return (0); 3033}
| 2182 sorwakeup(so); 2183 } else { 2184 thflags = tcp_reass(tp, th, &tlen, m); 2185 tp->t_flags |= TF_ACKNOW; 2186 } 2187 2188 /* 2189 * Note the amount of data that peer has sent into 2190 * our window, in order to estimate the sender's 2191 * buffer size. 2192 */ 2193 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2194 } else { 2195 m_freem(m); 2196 thflags &= ~TH_FIN; 2197 } 2198 2199 /* 2200 * If FIN is received ACK the FIN and let the user know 2201 * that the connection is closing. 2202 */ 2203 if (thflags & TH_FIN) { 2204 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2205 socantrcvmore(so); 2206 /* 2207 * If connection is half-synchronized 2208 * (ie NEEDSYN flag on) then delay ACK, 2209 * so it may be piggybacked when SYN is sent. 2210 * Otherwise, since we received a FIN then no 2211 * more input can be expected, send ACK now. 2212 */ 2213 if (tp->t_flags & TF_NEEDSYN) 2214 tp->t_flags |= TF_DELACK; 2215 else 2216 tp->t_flags |= TF_ACKNOW; 2217 tp->rcv_nxt++; 2218 } 2219 switch (tp->t_state) { 2220 2221 /* 2222 * In SYN_RECEIVED and ESTABLISHED STATES 2223 * enter the CLOSE_WAIT state. 2224 */ 2225 case TCPS_SYN_RECEIVED: 2226 tp->t_starttime = ticks; 2227 /*FALLTHROUGH*/ 2228 case TCPS_ESTABLISHED: 2229 tp->t_state = TCPS_CLOSE_WAIT; 2230 break; 2231 2232 /* 2233 * If still in FIN_WAIT_1 STATE FIN has not been acked so 2234 * enter the CLOSING state. 2235 */ 2236 case TCPS_FIN_WAIT_1: 2237 tp->t_state = TCPS_CLOSING; 2238 break; 2239 2240 /* 2241 * In FIN_WAIT_2 state enter the TIME_WAIT state, 2242 * starting the time-wait timer, turning off the other 2243 * standard timers. 2244 */ 2245 case TCPS_FIN_WAIT_2: 2246 KASSERT(headlocked == 1, ("headlocked should be 1")); 2247 tcp_twstart(tp); 2248 INP_INFO_WUNLOCK(&tcbinfo); 2249 return; 2250 2251 /* 2252 * In TIME_WAIT state restart the 2 MSL time_wait timer. 2253 */ 2254 case TCPS_TIME_WAIT: 2255 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 2256 callout_reset(tp->tt_2msl, 2 * tcp_msl, 2257 tcp_timer_2msl, tp); 2258 break; 2259 } 2260 } 2261 INP_INFO_WUNLOCK(&tcbinfo); 2262#ifdef TCPDEBUG 2263 if (so->so_options & SO_DEBUG) 2264 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2265 &tcp_savetcp, 0); 2266#endif 2267 2268 /* 2269 * Return any desired output. 2270 */ 2271 if (needoutput || (tp->t_flags & TF_ACKNOW)) 2272 (void) tcp_output(tp); 2273 2274check_delack: 2275 if (tp->t_flags & TF_DELACK) { 2276 tp->t_flags &= ~TF_DELACK; 2277 callout_reset(tp->tt_delack, tcp_delacktime, 2278 tcp_timer_delack, tp); 2279 } 2280 INP_UNLOCK(inp); 2281 return; 2282 2283dropafterack: 2284 /* 2285 * Generate an ACK dropping incoming segment if it occupies 2286 * sequence space, where the ACK reflects our state. 2287 * 2288 * We can now skip the test for the RST flag since all 2289 * paths to this code happen after packets containing 2290 * RST have been dropped. 2291 * 2292 * In the SYN-RECEIVED state, don't send an ACK unless the 2293 * segment we received passes the SYN-RECEIVED ACK test. 2294 * If it fails send a RST. This breaks the loop in the 2295 * "LAND" DoS attack, and also prevents an ACK storm 2296 * between two listening ports that have been sent forged 2297 * SYN segments, each with the source address of the other. 2298 */ 2299 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2300 (SEQ_GT(tp->snd_una, th->th_ack) || 2301 SEQ_GT(th->th_ack, tp->snd_max)) ) { 2302 rstreason = BANDLIM_RST_OPENPORT; 2303 goto dropwithreset; 2304 } 2305#ifdef TCPDEBUG 2306 if (so->so_options & SO_DEBUG) 2307 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2308 &tcp_savetcp, 0); 2309#endif 2310 KASSERT(headlocked, ("headlocked should be 1")); 2311 INP_INFO_WUNLOCK(&tcbinfo); 2312 m_freem(m); 2313 tp->t_flags |= TF_ACKNOW; 2314 (void) tcp_output(tp); 2315 INP_UNLOCK(inp); 2316 return; 2317 2318dropwithreset: 2319 /* 2320 * Generate a RST, dropping incoming segment. 2321 * Make ACK acceptable to originator of segment. 2322 * Don't bother to respond if destination was broadcast/multicast. 2323 */ 2324 if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2325 goto drop; 2326 if (isipv6) { 2327 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2328 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2329 goto drop; 2330 } else { 2331 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2332 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2333 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 2334 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2335 goto drop; 2336 } 2337 /* IPv6 anycast check is done at tcp6_input() */ 2338 2339 /* 2340 * Perform bandwidth limiting. 2341 */ 2342 if (badport_bandlim(rstreason) < 0) 2343 goto drop; 2344 2345#ifdef TCPDEBUG 2346 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2347 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2348 &tcp_savetcp, 0); 2349#endif 2350 2351 if (tp) 2352 INP_UNLOCK(inp); 2353 2354 if (thflags & TH_ACK) 2355 /* mtod() below is safe as long as hdr dropping is delayed */ 2356 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack, 2357 TH_RST); 2358 else { 2359 if (thflags & TH_SYN) 2360 tlen++; 2361 /* mtod() below is safe as long as hdr dropping is delayed */ 2362 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2363 (tcp_seq)0, TH_RST|TH_ACK); 2364 } 2365 if (headlocked) 2366 INP_INFO_WUNLOCK(&tcbinfo); 2367 return; 2368 2369drop: 2370 /* 2371 * Drop space held by incoming segment and return. 2372 */ 2373#ifdef TCPDEBUG 2374 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2375 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2376 &tcp_savetcp, 0); 2377#endif 2378 if (tp) 2379 INP_UNLOCK(inp); 2380 m_freem(m); 2381 if (headlocked) 2382 INP_INFO_WUNLOCK(&tcbinfo); 2383 return; 2384} 2385 2386/* 2387 * Parse TCP options and place in tcpopt. 2388 */ 2389static void 2390tcp_dooptions(to, cp, cnt, is_syn) 2391 struct tcpopt *to; 2392 u_char *cp; 2393 int cnt; 2394 int is_syn; 2395{ 2396 int opt, optlen; 2397 2398 to->to_flags = 0; 2399 for (; cnt > 0; cnt -= optlen, cp += optlen) { 2400 opt = cp[0]; 2401 if (opt == TCPOPT_EOL) 2402 break; 2403 if (opt == TCPOPT_NOP) 2404 optlen = 1; 2405 else { 2406 if (cnt < 2) 2407 break; 2408 optlen = cp[1]; 2409 if (optlen < 2 || optlen > cnt) 2410 break; 2411 } 2412 switch (opt) { 2413 case TCPOPT_MAXSEG: 2414 if (optlen != TCPOLEN_MAXSEG) 2415 continue; 2416 if (!is_syn) 2417 continue; 2418 to->to_flags |= TOF_MSS; 2419 bcopy((char *)cp + 2, 2420 (char *)&to->to_mss, sizeof(to->to_mss)); 2421 to->to_mss = ntohs(to->to_mss); 2422 break; 2423 case TCPOPT_WINDOW: 2424 if (optlen != TCPOLEN_WINDOW) 2425 continue; 2426 if (! is_syn) 2427 continue; 2428 to->to_flags |= TOF_SCALE; 2429 to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 2430 break; 2431 case TCPOPT_TIMESTAMP: 2432 if (optlen != TCPOLEN_TIMESTAMP) 2433 continue; 2434 to->to_flags |= TOF_TS; 2435 bcopy((char *)cp + 2, 2436 (char *)&to->to_tsval, sizeof(to->to_tsval)); 2437 to->to_tsval = ntohl(to->to_tsval); 2438 bcopy((char *)cp + 6, 2439 (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2440 to->to_tsecr = ntohl(to->to_tsecr); 2441 break; 2442 case TCPOPT_CC: 2443 if (optlen != TCPOLEN_CC) 2444 continue; 2445 to->to_flags |= TOF_CC; 2446 bcopy((char *)cp + 2, 2447 (char *)&to->to_cc, sizeof(to->to_cc)); 2448 to->to_cc = ntohl(to->to_cc); 2449 break; 2450 case TCPOPT_CCNEW: 2451 if (optlen != TCPOLEN_CC) 2452 continue; 2453 if (!is_syn) 2454 continue; 2455 to->to_flags |= TOF_CCNEW; 2456 bcopy((char *)cp + 2, 2457 (char *)&to->to_cc, sizeof(to->to_cc)); 2458 to->to_cc = ntohl(to->to_cc); 2459 break; 2460 case TCPOPT_CCECHO: 2461 if (optlen != TCPOLEN_CC) 2462 continue; 2463 if (!is_syn) 2464 continue; 2465 to->to_flags |= TOF_CCECHO; 2466 bcopy((char *)cp + 2, 2467 (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 2468 to->to_ccecho = ntohl(to->to_ccecho); 2469 break; 2470 default: 2471 continue; 2472 } 2473 } 2474} 2475 2476/* 2477 * Pull out of band byte out of a segment so 2478 * it doesn't appear in the user's data queue. 2479 * It is still reflected in the segment length for 2480 * sequencing purposes. 2481 */ 2482static void 2483tcp_pulloutofband(so, th, m, off) 2484 struct socket *so; 2485 struct tcphdr *th; 2486 register struct mbuf *m; 2487 int off; /* delayed to be droped hdrlen */ 2488{ 2489 int cnt = off + th->th_urp - 1; 2490 2491 while (cnt >= 0) { 2492 if (m->m_len > cnt) { 2493 char *cp = mtod(m, caddr_t) + cnt; 2494 struct tcpcb *tp = sototcpcb(so); 2495 2496 tp->t_iobc = *cp; 2497 tp->t_oobflags |= TCPOOB_HAVEDATA; 2498 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2499 m->m_len--; 2500 if (m->m_flags & M_PKTHDR) 2501 m->m_pkthdr.len--; 2502 return; 2503 } 2504 cnt -= m->m_len; 2505 m = m->m_next; 2506 if (m == 0) 2507 break; 2508 } 2509 panic("tcp_pulloutofband"); 2510} 2511 2512/* 2513 * Collect new round-trip time estimate 2514 * and update averages and current timeout. 2515 */ 2516static void 2517tcp_xmit_timer(tp, rtt) 2518 register struct tcpcb *tp; 2519 int rtt; 2520{ 2521 register int delta; 2522 2523 tcpstat.tcps_rttupdated++; 2524 tp->t_rttupdated++; 2525 if (tp->t_srtt != 0) { 2526 /* 2527 * srtt is stored as fixed point with 5 bits after the 2528 * binary point (i.e., scaled by 8). The following magic 2529 * is equivalent to the smoothing algorithm in rfc793 with 2530 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2531 * point). Adjust rtt to origin 0. 2532 */ 2533 delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2534 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2535 2536 if ((tp->t_srtt += delta) <= 0) 2537 tp->t_srtt = 1; 2538 2539 /* 2540 * We accumulate a smoothed rtt variance (actually, a 2541 * smoothed mean difference), then set the retransmit 2542 * timer to smoothed rtt + 4 times the smoothed variance. 2543 * rttvar is stored as fixed point with 4 bits after the 2544 * binary point (scaled by 16). The following is 2545 * equivalent to rfc793 smoothing with an alpha of .75 2546 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2547 * rfc793's wired-in beta. 2548 */ 2549 if (delta < 0) 2550 delta = -delta; 2551 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2552 if ((tp->t_rttvar += delta) <= 0) 2553 tp->t_rttvar = 1; 2554 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 2555 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2556 } else { 2557 /* 2558 * No rtt measurement yet - use the unsmoothed rtt. 2559 * Set the variance to half the rtt (so our first 2560 * retransmit happens at 3*rtt). 2561 */ 2562 tp->t_srtt = rtt << TCP_RTT_SHIFT; 2563 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 2564 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2565 } 2566 tp->t_rtttime = 0; 2567 tp->t_rxtshift = 0; 2568 2569 /* 2570 * the retransmit should happen at rtt + 4 * rttvar. 2571 * Because of the way we do the smoothing, srtt and rttvar 2572 * will each average +1/2 tick of bias. When we compute 2573 * the retransmit timer, we want 1/2 tick of rounding and 2574 * 1 extra tick because of +-1/2 tick uncertainty in the 2575 * firing of the timer. The bias will give us exactly the 2576 * 1.5 tick we need. But, because the bias is 2577 * statistical, we have to test that we don't drop below 2578 * the minimum feasible timer (which is 2 ticks). 2579 */ 2580 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 2581 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2582 2583 /* 2584 * We received an ack for a packet that wasn't retransmitted; 2585 * it is probably safe to discard any error indications we've 2586 * received recently. This isn't quite right, but close enough 2587 * for now (a route might have failed after we sent a segment, 2588 * and the return path might not be symmetrical). 2589 */ 2590 tp->t_softerror = 0; 2591} 2592 2593/* 2594 * Determine a reasonable value for maxseg size. 2595 * If the route is known, check route for mtu. 2596 * If none, use an mss that can be handled on the outgoing 2597 * interface without forcing IP to fragment; if bigger than 2598 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2599 * to utilize large mbufs. If no route is found, route has no mtu, 2600 * or the destination isn't local, use a default, hopefully conservative 2601 * size (usually 512 or the default IP max size, but no more than the mtu 2602 * of the interface), as we can't discover anything about intervening 2603 * gateways or networks. We also initialize the congestion/slow start 2604 * window to be a single segment if the destination isn't local. 2605 * While looking at the routing entry, we also initialize other path-dependent 2606 * parameters from pre-set or cached values in the routing entry. 2607 * 2608 * Also take into account the space needed for options that we 2609 * send regularly. Make maxseg shorter by that amount to assure 2610 * that we can send maxseg amount of data even when the options 2611 * are present. Store the upper limit of the length of options plus 2612 * data in maxopd. 2613 * 2614 * NOTE that this routine is only called when we process an incoming 2615 * segment, for outgoing segments only tcp_mssopt is called. 2616 * 2617 * In case of T/TCP, we call this routine during implicit connection 2618 * setup as well (offer = -1), to initialize maxseg from the cached 2619 * MSS of our peer. 2620 */ 2621void 2622tcp_mss(tp, offer) 2623 struct tcpcb *tp; 2624 int offer; 2625{ 2626 register struct rtentry *rt; 2627 struct ifnet *ifp; 2628 register int rtt, mss; 2629 u_long bufsize; 2630 struct inpcb *inp = tp->t_inpcb; 2631 struct socket *so; 2632 struct rmxp_tao *taop; 2633 int origoffer = offer; 2634#ifdef INET6 2635 int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2636 size_t min_protoh = isipv6 ? 2637 sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2638 sizeof (struct tcpiphdr); 2639#else 2640 const int isipv6 = 0; 2641 const size_t min_protoh = sizeof (struct tcpiphdr); 2642#endif 2643 2644 if (isipv6) 2645 rt = tcp_rtlookup6(&inp->inp_inc); 2646 else 2647 rt = tcp_rtlookup(&inp->inp_inc); 2648 if (rt == NULL) { 2649 tp->t_maxopd = tp->t_maxseg = 2650 isipv6 ? tcp_v6mssdflt : tcp_mssdflt; 2651 return; 2652 } 2653 ifp = rt->rt_ifp; 2654 so = inp->inp_socket; 2655 2656 taop = rmx_taop(rt->rt_rmx); 2657 /* 2658 * Offer == -1 means that we didn't receive SYN yet, 2659 * use cached value in that case; 2660 */ 2661 if (offer == -1) 2662 offer = taop->tao_mssopt; 2663 /* 2664 * Offer == 0 means that there was no MSS on the SYN segment, 2665 * in this case we use tcp_mssdflt. 2666 */ 2667 if (offer == 0) 2668 offer = isipv6 ? tcp_v6mssdflt : tcp_mssdflt; 2669 else 2670 /* 2671 * Sanity check: make sure that maxopd will be large 2672 * enough to allow some data on segments even is the 2673 * all the option space is used (40bytes). Otherwise 2674 * funny things may happen in tcp_output. 2675 */ 2676 offer = max(offer, 64); 2677 taop->tao_mssopt = offer; 2678 2679 /* 2680 * While we're here, check if there's an initial rtt 2681 * or rttvar. Convert from the route-table units 2682 * to scaled multiples of the slow timeout timer. 2683 */ 2684 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 2685 /* 2686 * XXX the lock bit for RTT indicates that the value 2687 * is also a minimum value; this is subject to time. 2688 */ 2689 if (rt->rt_rmx.rmx_locks & RTV_RTT) 2690 tp->t_rttmin = rtt / (RTM_RTTUNIT / hz); 2691 tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE)); 2692 tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 2693 tcpstat.tcps_usedrtt++; 2694 if (rt->rt_rmx.rmx_rttvar) { 2695 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 2696 (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE)); 2697 tcpstat.tcps_usedrttvar++; 2698 } else { 2699 /* default variation is +- 1 rtt */ 2700 tp->t_rttvar = 2701 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 2702 } 2703 TCPT_RANGESET(tp->t_rxtcur, 2704 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 2705 tp->t_rttmin, TCPTV_REXMTMAX); 2706 } 2707 /* 2708 * if there's an mtu associated with the route, use it 2709 * else, use the link mtu. 2710 */ 2711 if (rt->rt_rmx.rmx_mtu) 2712 mss = rt->rt_rmx.rmx_mtu - min_protoh; 2713 else { 2714#ifdef INET6 2715 mss = (isipv6 ? IN6_LINKMTU(rt->rt_ifp) : ifp->if_mtu) 2716 - min_protoh; 2717#else 2718 mss = ifp->if_mtu - min_protoh; 2719#endif 2720#ifdef INET6 2721 if (isipv6) { 2722 if (!in6_localaddr(&inp->in6p_faddr)) 2723 mss = min(mss, tcp_v6mssdflt); 2724 } else 2725#endif 2726 if (!in_localaddr(inp->inp_faddr)) 2727 mss = min(mss, tcp_mssdflt); 2728 } 2729 mss = min(mss, offer); 2730 /* 2731 * maxopd stores the maximum length of data AND options 2732 * in a segment; maxseg is the amount of data in a normal 2733 * segment. We need to store this value (maxopd) apart 2734 * from maxseg, because now every segment carries options 2735 * and thus we normally have somewhat less data in segments. 2736 */ 2737 tp->t_maxopd = mss; 2738 2739 /* 2740 * In case of T/TCP, origoffer==-1 indicates, that no segments 2741 * were received yet. In this case we just guess, otherwise 2742 * we do the same as before T/TCP. 2743 */ 2744 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2745 (origoffer == -1 || 2746 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2747 mss -= TCPOLEN_TSTAMP_APPA; 2748 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2749 (origoffer == -1 || 2750 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2751 mss -= TCPOLEN_CC_APPA; 2752 2753#if (MCLBYTES & (MCLBYTES - 1)) == 0 2754 if (mss > MCLBYTES) 2755 mss &= ~(MCLBYTES-1); 2756#else 2757 if (mss > MCLBYTES) 2758 mss = mss / MCLBYTES * MCLBYTES; 2759#endif 2760 /* 2761 * If there's a pipesize, change the socket buffer 2762 * to that size. Make the socket buffers an integral 2763 * number of mss units; if the mss is larger than 2764 * the socket buffer, decrease the mss. 2765 */ 2766#ifdef RTV_SPIPE 2767 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 2768#endif 2769 bufsize = so->so_snd.sb_hiwat; 2770 if (bufsize < mss) 2771 mss = bufsize; 2772 else { 2773 bufsize = roundup(bufsize, mss); 2774 if (bufsize > sb_max) 2775 bufsize = sb_max; 2776 if (bufsize > so->so_snd.sb_hiwat) 2777 (void)sbreserve(&so->so_snd, bufsize, so, NULL); 2778 } 2779 tp->t_maxseg = mss; 2780 2781#ifdef RTV_RPIPE 2782 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 2783#endif 2784 bufsize = so->so_rcv.sb_hiwat; 2785 if (bufsize > mss) { 2786 bufsize = roundup(bufsize, mss); 2787 if (bufsize > sb_max) 2788 bufsize = sb_max; 2789 if (bufsize > so->so_rcv.sb_hiwat) 2790 (void)sbreserve(&so->so_rcv, bufsize, so, NULL); 2791 } 2792 2793 /* 2794 * Set the slow-start flight size depending on whether this 2795 * is a local network or not. 2796 */ 2797 if (tcp_do_rfc3390) 2798 tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380)); 2799 else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 2800 (!isipv6 && in_localaddr(inp->inp_faddr))) 2801 tp->snd_cwnd = mss * ss_fltsz_local; 2802 else 2803 tp->snd_cwnd = mss * ss_fltsz; 2804 2805 if (rt->rt_rmx.rmx_ssthresh) { 2806 /* 2807 * There's some sort of gateway or interface 2808 * buffer limit on the path. Use this to set 2809 * the slow start threshhold, but set the 2810 * threshold to no less than 2*mss. 2811 */ 2812 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 2813 tcpstat.tcps_usedssthresh++; 2814 } 2815} 2816 2817/* 2818 * Determine the MSS option to send on an outgoing SYN. 2819 */ 2820int 2821tcp_mssopt(tp) 2822 struct tcpcb *tp; 2823{ 2824 struct rtentry *rt; 2825#ifdef INET6 2826 int isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2827 size_t min_protoh = isipv6 ? 2828 sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2829 sizeof (struct tcpiphdr); 2830#else 2831 const int isipv6 = 0; 2832 const size_t min_protoh = sizeof (struct tcpiphdr); 2833#endif 2834 2835 if (isipv6) 2836 rt = tcp_rtlookup6(&tp->t_inpcb->inp_inc); 2837 else 2838 rt = tcp_rtlookup(&tp->t_inpcb->inp_inc); 2839 if (rt == NULL) 2840 return (isipv6 ? tcp_v6mssdflt : tcp_mssdflt); 2841 2842#ifdef INET6 2843 return (isipv6 ? IN6_LINKMTU(rt->rt_ifp) : 2844 rt->rt_ifp->if_mtu - min_protoh); 2845#else 2846 return (rt->rt_ifp->if_mtu - min_protoh); 2847#endif 2848} 2849 2850 2851/* 2852 * On a partial ack arrives, force the retransmission of the 2853 * next unacknowledged segment. Do not clear tp->t_dupacks. 2854 * By setting snd_nxt to ti_ack, this forces retransmission timer to 2855 * be started again. 2856 */ 2857static void 2858tcp_newreno_partial_ack(tp, th) 2859 struct tcpcb *tp; 2860 struct tcphdr *th; 2861{ 2862 tcp_seq onxt = tp->snd_nxt; 2863 u_long ocwnd = tp->snd_cwnd; 2864 2865 callout_stop(tp->tt_rexmt); 2866 tp->t_rtttime = 0; 2867 tp->snd_nxt = th->th_ack; 2868 /* 2869 * Set snd_cwnd to one segment beyond acknowledged offset. 2870 * (tp->snd_una has not yet been updated when this function is called.) 2871 */ 2872 tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 2873 tp->t_flags |= TF_ACKNOW; 2874 (void) tcp_output(tp); 2875 tp->snd_cwnd = ocwnd; 2876 if (SEQ_GT(onxt, tp->snd_nxt)) 2877 tp->snd_nxt = onxt; 2878 /* 2879 * Partial window deflation. Relies on fact that tp->snd_una 2880 * not updated yet. 2881 */ 2882 tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg); 2883} 2884 2885/* 2886 * Returns 1 if the TIME_WAIT state was killed and we should start over, 2887 * looking for a pcb in the listen state. Returns 0 otherwise. 2888 */ 2889static int 2890tcp_timewait(tw, to, th, m, tlen) 2891 struct tcptw *tw; 2892 struct tcpopt *to; 2893 struct tcphdr *th; 2894 struct mbuf *m; 2895 int tlen; 2896{ 2897 int thflags; 2898 tcp_seq seq; 2899#ifdef INET6 2900 int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 2901#else 2902 const int isipv6 = 0; 2903#endif 2904 2905 thflags = th->th_flags; 2906 2907 /* 2908 * NOTE: for FIN_WAIT_2 (to be added later), 2909 * must validate sequence number before accepting RST 2910 */ 2911 2912 /* 2913 * If the segment contains RST: 2914 * Drop the segment - see Stevens, vol. 2, p. 964 and 2915 * RFC 1337. 2916 */ 2917 if (thflags & TH_RST) 2918 goto drop; 2919 2920 /* 2921 * If segment contains a SYN and CC [not CC.NEW] option: 2922 * if connection duration > MSL, drop packet and send RST; 2923 * 2924 * if SEG.CC > CCrecv then is new SYN. 2925 * Complete close and delete TCPCB. Then reprocess 2926 * segment, hoping to find new TCPCB in LISTEN state; 2927 * 2928 * else must be old SYN; drop it. 2929 * else do normal processing. 2930 */ 2931 if ((thflags & TH_SYN) && (to->to_flags & TOF_CC) && tw->cc_recv != 0) { 2932 if ((ticks - tw->t_starttime) > tcp_msl) 2933 goto reset; 2934 if (CC_GT(to->to_cc, tw->cc_recv)) { 2935 (void) tcp_twclose(tw, 0); 2936 return (1); 2937 } 2938 goto drop; 2939 } 2940 2941#if 0 2942/* PAWS not needed at the moment */ 2943 /* 2944 * RFC 1323 PAWS: If we have a timestamp reply on this segment 2945 * and it's less than ts_recent, drop it. 2946 */ 2947 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 2948 TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2949 if ((thflags & TH_ACK) == 0) 2950 goto drop; 2951 goto ack; 2952 } 2953 /* 2954 * ts_recent is never updated because we never accept new segments. 2955 */ 2956#endif 2957 2958 /* 2959 * If a new connection request is received 2960 * while in TIME_WAIT, drop the old connection 2961 * and start over if the sequence numbers 2962 * are above the previous ones. 2963 */ 2964 if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) { 2965 (void) tcp_twclose(tw, 0); 2966 return (1); 2967 } 2968 2969 /* 2970 * Drop the the segment if it does not contain an ACK. 2971 */ 2972 if ((thflags & TH_ACK) == 0) 2973 goto drop; 2974 2975 /* 2976 * Reset the 2MSL timer if this is a duplicate FIN. 2977 */ 2978 if (thflags & TH_FIN) { 2979 seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0); 2980 if (seq + 1 == tw->rcv_nxt) 2981 tcp_timer_2msl_reset(tw, 2 * tcp_msl); 2982 } 2983 2984 /* 2985 * Acknowledge the segment if it has data or is not a duplicate ACK. 2986 */ 2987 if (thflags != TH_ACK || tlen != 0 || 2988 th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt) 2989 tcp_twrespond(tw, NULL, m, TH_ACK); 2990 goto drop; 2991 2992reset: 2993 /* 2994 * Generate a RST, dropping incoming segment. 2995 * Make ACK acceptable to originator of segment. 2996 * Don't bother to respond if destination was broadcast/multicast. 2997 */ 2998 if (m->m_flags & (M_BCAST|M_MCAST)) 2999 goto drop; 3000 if (isipv6) { 3001 struct ip6_hdr *ip6; 3002 3003 /* IPv6 anycast check is done at tcp6_input() */ 3004 ip6 = mtod(m, struct ip6_hdr *); 3005 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3006 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3007 goto drop; 3008 } else { 3009 struct ip *ip; 3010 3011 ip = mtod(m, struct ip *); 3012 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3013 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3014 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3015 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3016 goto drop; 3017 } 3018 if (thflags & TH_ACK) { 3019 tcp_respond(NULL, 3020 mtod(m, void *), th, m, 0, th->th_ack, TH_RST); 3021 } else { 3022 seq = th->th_seq + (thflags & TH_SYN ? 1 : 0); 3023 tcp_respond(NULL, 3024 mtod(m, void *), th, m, seq, 0, TH_RST|TH_ACK); 3025 } 3026 INP_UNLOCK(tw->tw_inpcb); 3027 return (0); 3028 3029drop: 3030 INP_UNLOCK(tw->tw_inpcb); 3031 m_freem(m); 3032 return (0); 3033}
|