tcp_subr.c revision 1.67 1 /* $NetBSD: tcp_subr.c,v 1.67 1999/07/01 08:12:51 itojun Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
38 * Facility, NASA Ames Research Center.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the NetBSD
51 * Foundation, Inc. and its contributors.
52 * 4. Neither the name of The NetBSD Foundation nor the names of its
53 * contributors may be used to endorse or promote products derived
54 * from this software without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 * POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 /*
70 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
71 * The Regents of the University of California. All rights reserved.
72 *
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
75 * are met:
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 * 3. All advertising materials mentioning features or use of this software
82 * must display the following acknowledgement:
83 * This product includes software developed by the University of
84 * California, Berkeley and its contributors.
85 * 4. Neither the name of the University nor the names of its contributors
86 * may be used to endorse or promote products derived from this software
87 * without specific prior written permission.
88 *
89 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
92 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
93 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
94 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
95 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
96 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
97 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
98 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99 * SUCH DAMAGE.
100 *
101 * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95
102 */
103
104 #include "opt_inet.h"
105 #include "opt_tcp_compat_42.h"
106 #include "rnd.h"
107
108 #include <sys/param.h>
109 #include <sys/proc.h>
110 #include <sys/systm.h>
111 #include <sys/malloc.h>
112 #include <sys/mbuf.h>
113 #include <sys/socket.h>
114 #include <sys/socketvar.h>
115 #include <sys/protosw.h>
116 #include <sys/errno.h>
117 #include <sys/kernel.h>
118 #include <sys/pool.h>
119 #if NRND > 0
120 #include <sys/rnd.h>
121 #endif
122
123 #include <net/route.h>
124 #include <net/if.h>
125
126 #include <netinet/in.h>
127 #include <netinet/in_systm.h>
128 #include <netinet/ip.h>
129 #include <netinet/in_pcb.h>
130 #include <netinet/ip_var.h>
131 #include <netinet/ip_icmp.h>
132
133 #ifdef INET6
134 #ifndef INET
135 #include <netinet/in.h>
136 #endif
137 #include <netinet/ip6.h>
138 #include <netinet6/in6_pcb.h>
139 #include <netinet6/ip6_var.h>
140 #endif
141
142 #include <netinet/tcp.h>
143 #include <netinet/tcp_fsm.h>
144 #include <netinet/tcp_seq.h>
145 #include <netinet/tcp_timer.h>
146 #include <netinet/tcp_var.h>
147 #include <netinet/tcpip.h>
148
149 #ifdef IPSEC
150 #include <netinet6/ipsec.h>
151 #include <netinet6/ah.h>
152 #ifdef IPSEC_ESP
153 #include <netinet6/esp.h>
154 #endif
155 #endif /*IPSEC*/
156
157 #ifdef INET6
158 struct in6pcb tcb6;
159 #endif
160
161 /* patchable/settable parameters for tcp */
162 int tcp_mssdflt = TCP_MSS;
163 int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
164 int tcp_do_rfc1323 = 1; /* window scaling / timestamps (obsolete) */
165 int tcp_do_sack = 1; /* selective acknowledgement */
166 int tcp_do_win_scale = 1; /* RFC1323 window scaling */
167 int tcp_do_timestamps = 1; /* RFC1323 timestamps */
168 int tcp_do_newreno = 0; /* Use the New Reno algorithms */
169 int tcp_ack_on_push = 0; /* set to enable immediate ACK-on-PUSH */
170 int tcp_init_win = 1;
171 int tcp_mss_ifmtu = 0;
172 #ifdef TCP_COMPAT_42
173 int tcp_compat_42 = 1;
174 #else
175 int tcp_compat_42 = 0;
176 #endif
177
178 #ifndef TCBHASHSIZE
179 #define TCBHASHSIZE 128
180 #endif
181 int tcbhashsize = TCBHASHSIZE;
182
183 int tcp_freeq __P((struct tcpcb *));
184
185 struct pool tcpcb_pool;
186
187 /*
188 * Tcp initialization
189 */
190 void
191 tcp_init()
192 {
193 int hlen;
194
195 pool_init(&tcpcb_pool, sizeof(struct tcpcb), 0, 0, 0, "tcpcbpl",
196 0, NULL, NULL, M_PCB);
197 in_pcbinit(&tcbtable, tcbhashsize, tcbhashsize);
198 #ifdef INET6
199 tcb6.in6p_next = tcb6.in6p_prev = &tcb6;
200 #endif
201 LIST_INIT(&tcp_delacks);
202
203 hlen = sizeof(struct ip) + sizeof(struct tcphdr);
204 #ifdef INET6
205 if (sizeof(struct ip) < sizeof(struct ip6_hdr))
206 hlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
207 #endif
208 if (max_protohdr < hlen)
209 max_protohdr = hlen;
210 if (max_linkhdr + hlen > MHLEN)
211 panic("tcp_init");
212
213 /* Initialize the compressed state engine. */
214 syn_cache_init();
215 }
216
217 /*
218 * Create template to be used to send tcp packets on a connection.
219 * Call after host entry created, allocates an mbuf and fills
220 * in a skeletal tcp/ip header, minimizing the amount of work
221 * necessary when the connection is used.
222 */
223 struct mbuf *
224 tcp_template(tp)
225 struct tcpcb *tp;
226 {
227 register struct inpcb *inp = tp->t_inpcb;
228 #ifdef INET6
229 register struct in6pcb *in6p = tp->t_in6pcb;
230 #endif
231 register struct tcphdr *n;
232 register struct mbuf *m;
233 int hlen;
234
235 switch (tp->t_family) {
236 case AF_INET:
237 hlen = sizeof(struct ip);
238 if (inp)
239 break;
240 #ifdef INET6
241 if (in6p) {
242 /* mapped addr case */
243 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
244 && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
245 break;
246 }
247 #endif
248 return NULL; /*EINVAL*/
249 #ifdef INET6
250 case AF_INET6:
251 hlen = sizeof(struct ip6_hdr);
252 if (in6p) {
253 /* more sainty check? */
254 break;
255 }
256 return NULL; /*EINVAL*/
257 #endif
258 default:
259 return NULL; /*EAFNOSUPPORT*/
260 }
261 if ((m = tp->t_template) == 0) {
262 MGETHDR(m, M_DONTWAIT, MT_HEADER);
263 if (m) {
264 MCLGET(m, M_DONTWAIT);
265 if ((m->m_flags & M_EXT) == 0) {
266 m_free(m);
267 m = NULL;
268 }
269 }
270 if (m == NULL)
271 return NULL;
272 m->m_len = hlen + sizeof(struct tcphdr);
273 }
274 bzero(mtod(m, caddr_t), m->m_len);
275 switch (tp->t_family) {
276 case AF_INET:
277 {
278 struct ipovly *ipov;
279 mtod(m, struct ip *)->ip_v = 4;
280 ipov = mtod(m, struct ipovly *);
281 ipov->ih_pr = IPPROTO_TCP;
282 ipov->ih_len = htons(sizeof(struct tcphdr));
283 if (inp) {
284 ipov->ih_src = inp->inp_laddr;
285 ipov->ih_dst = inp->inp_faddr;
286 }
287 #ifdef INET6
288 else if (in6p) {
289 /* mapped addr case */
290 bcopy(&in6p->in6p_laddr.s6_addr32[3], &ipov->ih_src,
291 sizeof(ipov->ih_src));
292 bcopy(&in6p->in6p_faddr.s6_addr32[3], &ipov->ih_dst,
293 sizeof(ipov->ih_dst));
294 }
295 #endif
296 break;
297 }
298 #ifdef INET6
299 case AF_INET6:
300 {
301 struct ip6_hdr *ip6;
302 mtod(m, struct ip *)->ip_v = 6;
303 ip6 = mtod(m, struct ip6_hdr *);
304 ip6->ip6_nxt = IPPROTO_TCP;
305 ip6->ip6_plen = htons(sizeof(struct tcphdr));
306 ip6->ip6_src = in6p->in6p_laddr;
307 ip6->ip6_dst = in6p->in6p_faddr;
308 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
309 if (ip6_auto_flowlabel) {
310 ip6->ip6_flow &= ~IPV6_FLOWLABEL_MASK;
311 ip6->ip6_flow |=
312 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
313 }
314 ip6->ip6_vfc = IPV6_VERSION;
315 break;
316 }
317 #endif
318 }
319 n = (struct tcphdr *)(mtod(m, caddr_t) + hlen);
320 if (inp) {
321 n->th_sport = inp->inp_lport;
322 n->th_dport = inp->inp_fport;
323 }
324 #ifdef INET6
325 else if (in6p) {
326 n->th_sport = in6p->in6p_lport;
327 n->th_dport = in6p->in6p_fport;
328 }
329 #endif
330 n->th_seq = 0;
331 n->th_ack = 0;
332 n->th_x2 = 0;
333 n->th_off = 5;
334 n->th_flags = 0;
335 n->th_win = 0;
336 n->th_sum = 0;
337 n->th_urp = 0;
338 return (m);
339 }
340
341 /*
342 * Send a single message to the TCP at address specified by
343 * the given TCP/IP header. If m == 0, then we make a copy
344 * of the tcpiphdr at ti and send directly to the addressed host.
345 * This is used to force keep alive messages out using the TCP
346 * template for a connection tp->t_template. If flags are given
347 * then we send a message back to the TCP which originated the
348 * segment ti, and discard the mbuf containing it and any other
349 * attached mbufs.
350 *
351 * In any case the ack and sequence number of the transmitted
352 * segment are as specified by the parameters.
353 */
354 int
355 tcp_respond(tp, template, m, ack, seq, flags)
356 struct tcpcb *tp;
357 struct mbuf *template; /* XXX should be struct mbuf? */
358 register struct mbuf *m;
359 tcp_seq ack, seq;
360 int flags;
361 {
362 #ifndef INET6
363 struct route iproute;
364 #else
365 struct route_in6 iproute; /* sizeof(route_in6) > sizeof(route) */
366 #endif
367 struct route *ro;
368 struct rtentry *rt;
369 int error, tlen, win = 0;
370 int hlen;
371 struct ip *ip;
372 #ifdef INET6
373 struct ip6_hdr *ip6;
374 #endif
375 int family; /* family on packet, not inpcb/in6pcb! */
376 struct tcphdr *th;
377
378 if (tp != NULL && (flags & TH_RST) == 0) {
379 if (tp->t_inpcb)
380 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
381 #ifdef INET6
382 else if (tp->t_in6pcb)
383 win = sbspace(&tp->t_in6pcb->in6p_socket->so_rcv);
384 #endif
385 }
386
387 ip = NULL;
388 #ifdef INET6
389 ip6 = NULL;
390 #endif
391 if (m == 0) {
392 if (template
393 && template->m_len < hlen + sizeof(struct tcphdr)) {
394 if (m)
395 m_freem(m);
396 return EINVAL;
397 }
398
399 /* get family information from template */
400 switch (mtod(template, struct ip *)->ip_v) {
401 case 4:
402 family = AF_INET;
403 hlen = sizeof(struct ip);
404 break;
405 #ifdef INET6
406 case 6:
407 family = AF_INET6;
408 hlen = sizeof(struct ip6_hdr);
409 break;
410 #endif
411 default:
412 if (m)
413 m_freem(m);
414 return EAFNOSUPPORT;
415 }
416
417 MGETHDR(m, M_DONTWAIT, MT_HEADER);
418 if (m) {
419 MCLGET(m, M_DONTWAIT);
420 if ((m->m_flags & M_EXT)) {
421 m_free(m);
422 m = NULL;
423 }
424 }
425 if (m == NULL)
426 return (ENOBUFS);
427
428 if (tcp_compat_42)
429 tlen = 1;
430 else
431 tlen = 0;
432
433 m->m_data += max_linkhdr;
434 bcopy(mtod(template, caddr_t), mtod(m, caddr_t),
435 template->m_len);
436 switch (family) {
437 case AF_INET:
438 ip = mtod(m, struct ip *);
439 th = (struct tcphdr *)(ip + 1);
440 break;
441 #ifdef INET6
442 case AF_INET6:
443 ip6 = mtod(m, struct ip6_hdr *);
444 th = (struct tcphdr *)(ip6 + 1);
445 break;
446 #endif
447 }
448 flags = TH_ACK;
449 } else {
450 /* get family information from m */
451 switch (mtod(m, struct ip *)->ip_v) {
452 case 4:
453 family = AF_INET;
454 hlen = sizeof(struct ip);
455 break;
456 #ifdef INET6
457 case 6:
458 family = AF_INET6;
459 hlen = sizeof(struct ip6_hdr);
460 break;
461 #endif
462 default:
463 if (m)
464 m_freem(m);
465 return EAFNOSUPPORT;
466 }
467
468 /* template pointer almost has no meaning */
469 m_freem(m->m_next);
470 m->m_next = 0;
471 m->m_len = hlen + sizeof(struct tcphdr);
472 if ((m->m_flags & M_PKTHDR) == 0) {
473 printf("non PKTHDR to tcp_respond\n");
474 m_freem(m);
475 return EINVAL;
476 }
477
478 tlen = 0;
479 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
480 switch (family) {
481 case AF_INET:
482 ip = mtod(m, struct ip *);
483 th = (struct tcphdr *)(ip + 1);
484 xchg(ip->ip_dst, ip->ip_src, struct in_addr);
485 break;
486 #ifdef INET6
487 case AF_INET6:
488 ip6 = mtod(m, struct ip6_hdr *);
489 th = (struct tcphdr *)(ip6 + 1);
490 xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
491 break;
492 #endif
493 }
494 xchg(th->th_dport, th->th_sport, u_int16_t);
495 #undef xchg
496 }
497 th->th_seq = htonl(seq);
498 th->th_ack = htonl(ack);
499 th->th_x2 = 0;
500 if ((flags & TH_SYN) == 0) {
501 if (tp)
502 th->th_win = htons((u_int16_t) (win >> tp->rcv_scale));
503 else
504 th->th_win = htons((u_int16_t)win);
505 th->th_off = sizeof (struct tcphdr) >> 2;
506 tlen += sizeof (struct tcphdr);
507 } else
508 tlen += th->th_off << 2;
509 m->m_len = hlen + tlen;
510 m->m_pkthdr.len = hlen + tlen;
511 m->m_pkthdr.rcvif = (struct ifnet *) 0;
512 th->th_flags = flags;
513 th->th_urp = 0;
514
515 switch (family) {
516 case AF_INET:
517 {
518 struct ipovly *ipov = (struct ipovly *)ip;
519 bzero(ipov->ih_x1, sizeof ipov->ih_x1);
520 ipov->ih_len = htons((u_int16_t)tlen);
521
522 th->th_sum = 0;
523 th->th_sum = in_cksum(m, hlen + tlen);
524 ip->ip_len = hlen + tlen; /*will be flipped on output*/
525 ip->ip_ttl = ip_defttl;
526 break;
527 }
528 #ifdef INET6
529 case AF_INET6:
530 {
531 th->th_sum = 0;
532 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
533 tlen);
534 ip6->ip6_plen = ntohs(tlen);
535 ip6->ip6_hlim = ip6_defhlim;
536 ip6->ip6_flow &= ~IPV6_FLOWINFO_MASK;
537 if (ip6_auto_flowlabel) {
538 ip6->ip6_flow |=
539 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
540 }
541 break;
542 }
543 #endif
544 }
545
546 #ifdef IPSEC
547 m->m_pkthdr.rcvif = NULL;
548 #endif /*IPSEC*/
549
550 /*
551 * If we're doing Path MTU discovery, we need to set DF unless
552 * the route's MTU is locked. If we lack a route, we need to
553 * look it up now.
554 *
555 * ip_output() could do this for us, but it's convenient to just
556 * do it here unconditionally.
557 */
558 if (tp != NULL && tp->t_inpcb != NULL) {
559 ro = &tp->t_inpcb->inp_route;
560 #ifdef IPSEC
561 m->m_pkthdr.rcvif = (struct ifnet *)tp->t_inpcb->inp_socket;
562 #endif
563 #ifdef DIAGNOSTIC
564 if (family != AF_INET)
565 panic("tcp_respond: address family mismatch");
566 if (!in_hosteq(ip->ip_dst, tp->t_inpcb->inp_faddr)) {
567 panic("tcp_respond: ip_dst %x != inp_faddr %x",
568 ntohl(ip->ip_dst.s_addr),
569 ntohl(tp->t_inpcb->inp_faddr.s_addr));
570 }
571 #endif
572 }
573 #ifdef INET6
574 else if (tp != NULL && tp->t_in6pcb != NULL) {
575 ro = (struct route *)&tp->t_in6pcb->in6p_route;
576 #ifdef IPSEC
577 m->m_pkthdr.rcvif = (struct ifnet *)tp->t_in6pcb->in6p_socket;
578 #endif
579 #ifdef DIAGNOSTIC
580 if (family == AF_INET) {
581 if (!IN6_IS_ADDR_V4MAPPED(&tp->t_in6pcb->in6p_faddr))
582 panic("tcp_respond: not mapped addr");
583 if (bcmp(&ip->ip_dst,
584 &tp->t_in6pcb->in6p_faddr.s6_addr32[3],
585 sizeof(ip->ip_dst)) != 0) {
586 panic("tcp_respond: ip_dst != in6p_faddr");
587 }
588 } else if (family == AF_INET6) {
589 if (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &tp->t_in6pcb->in6p_faddr))
590 panic("tcp_respond: ip6_dst != in6p_faddr");
591 } else
592 panic("tcp_respond: address family mismatch");
593 #endif
594 }
595 #endif
596 else {
597 ro = (struct route *)&iproute;
598 bzero(ro, sizeof(iproute));
599 }
600 if ((rt = ro->ro_rt) == NULL || (rt->rt_flags & RTF_UP) == 0) {
601 if (ro->ro_rt != NULL) {
602 RTFREE(ro->ro_rt);
603 ro->ro_rt = NULL;
604 }
605 switch (family) {
606 case AF_INET:
607 {
608 struct sockaddr_in *dst;
609 dst = satosin(&ro->ro_dst);
610 dst->sin_family = AF_INET;
611 dst->sin_len = sizeof(*dst);
612 dst->sin_addr = ip->ip_dst;
613 break;
614 }
615 #ifdef INET6
616 case AF_INET6:
617 {
618 struct sockaddr_in6 *dst;
619 dst = satosin6(&ro->ro_dst);
620 bzero(dst, sizeof(*dst));
621 dst->sin6_family = AF_INET6;
622 dst->sin6_len = sizeof(*dst);
623 dst->sin6_addr = ip6->ip6_dst;
624 break;
625 }
626 #endif
627 }
628 rtalloc(ro);
629 if ((rt = ro->ro_rt) == NULL) {
630 m_freem(m);
631 switch (family) {
632 case AF_INET:
633 ipstat.ips_noroute++;
634 break;
635 #ifdef INET6
636 case AF_INET6:
637 ip6stat.ip6s_noroute++;
638 break;
639 #endif
640 }
641 return (EHOSTUNREACH);
642 }
643 }
644 switch (family) {
645 case AF_INET:
646 if (ip_mtudisc != 0 && (rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
647 ip->ip_off |= IP_DF;
648
649 error = ip_output(m, NULL, ro, 0, NULL);
650 break;
651 #ifdef INET6
652 case AF_INET6:
653 error = ip6_output(m, NULL, (struct route_in6 *)ro, 0, NULL);
654 break;
655 #endif
656 }
657
658 if (ro == (struct route *)&iproute) {
659 RTFREE(ro->ro_rt);
660 ro->ro_rt = NULL;
661 }
662
663 return (error);
664 }
665
666 /*
667 * Create a new TCP control block, making an
668 * empty reassembly queue and hooking it to the argument
669 * protocol control block.
670 */
671 struct tcpcb *
672 tcp_newtcpcb(family, aux)
673 int family; /* selects inpcb, or in6pcb */
674 void *aux;
675 {
676 register struct tcpcb *tp;
677
678 switch (family) {
679 case PF_INET:
680 break;
681 #ifdef INET6
682 case PF_INET6:
683 break;
684 #endif
685 default:
686 return NULL;
687 }
688
689 tp = pool_get(&tcpcb_pool, PR_NOWAIT);
690 if (tp == NULL)
691 return (NULL);
692 bzero((caddr_t)tp, sizeof(struct tcpcb));
693 LIST_INIT(&tp->segq);
694 LIST_INIT(&tp->timeq);
695 tp->t_family = family; /* may be overridden later on */
696 tp->t_peermss = tcp_mssdflt;
697 tp->t_ourmss = tcp_mssdflt;
698 tp->t_segsz = tcp_mssdflt;
699
700 tp->t_flags = 0;
701 if (tcp_do_rfc1323 && tcp_do_win_scale)
702 tp->t_flags |= TF_REQ_SCALE;
703 if (tcp_do_rfc1323 && tcp_do_timestamps)
704 tp->t_flags |= TF_REQ_TSTMP;
705 if (tcp_do_sack == 2)
706 tp->t_flags |= TF_WILL_SACK;
707 else if (tcp_do_sack == 1)
708 tp->t_flags |= TF_WILL_SACK|TF_IGNR_RXSACK;
709 tp->t_flags |= TF_CANT_TXSACK;
710 switch (family) {
711 case PF_INET:
712 tp->t_inpcb = (struct inpcb *)aux;
713 break;
714 #ifdef INET6
715 case PF_INET6:
716 tp->t_in6pcb = (struct in6pcb *)aux;
717 break;
718 #endif
719 }
720 /*
721 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
722 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives
723 * reasonable initial retransmit time.
724 */
725 tp->t_srtt = TCPTV_SRTTBASE;
726 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1);
727 tp->t_rttmin = TCPTV_MIN;
728 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
729 TCPTV_MIN, TCPTV_REXMTMAX);
730 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
731 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
732 if (family == AF_INET) {
733 struct inpcb *inp = (struct inpcb *)aux;
734 inp->inp_ip.ip_ttl = ip_defttl;
735 inp->inp_ppcb = (caddr_t)tp;
736 }
737 #ifdef INET6
738 else if (family == AF_INET6) {
739 struct in6pcb *in6p = (struct in6pcb *)aux;
740 in6p->in6p_ip6.ip6_hlim = ip6_defhlim;
741 in6p->in6p_ppcb = (caddr_t)tp;
742 }
743 #endif
744 return (tp);
745 }
746
747 /*
748 * Drop a TCP connection, reporting
749 * the specified error. If connection is synchronized,
750 * then send a RST to peer.
751 */
752 struct tcpcb *
753 tcp_drop(tp, errno)
754 register struct tcpcb *tp;
755 int errno;
756 {
757 struct socket *so;
758
759 if (tp->t_inpcb)
760 so = tp->t_inpcb->inp_socket;
761 #ifdef INET6
762 else if (tp->t_in6pcb)
763 so = tp->t_in6pcb->in6p_socket;
764 #endif
765 else
766 return NULL;
767
768 if (TCPS_HAVERCVDSYN(tp->t_state)) {
769 tp->t_state = TCPS_CLOSED;
770 (void) tcp_output(tp);
771 tcpstat.tcps_drops++;
772 } else
773 tcpstat.tcps_conndrops++;
774 if (errno == ETIMEDOUT && tp->t_softerror)
775 errno = tp->t_softerror;
776 so->so_error = errno;
777 return (tcp_close(tp));
778 }
779
780 /*
781 * Close a TCP control block:
782 * discard all space held by the tcp
783 * discard internet protocol block
784 * wake up any sleepers
785 */
786 struct tcpcb *
787 tcp_close(tp)
788 register struct tcpcb *tp;
789 {
790 struct inpcb *inp;
791 #ifdef INET6
792 struct in6pcb *in6p;
793 #endif
794 struct socket *so;
795 #ifdef RTV_RTT
796 register struct rtentry *rt;
797 #endif
798 struct route *ro;
799
800 inp = tp->t_inpcb;
801 #ifdef INET6
802 in6p = tp->t_in6pcb;
803 #endif
804 so = NULL;
805 ro = NULL;
806 if (inp) {
807 so = inp->inp_socket;
808 ro = &inp->inp_route;
809 }
810 #ifdef INET6
811 else if (in6p) {
812 so = in6p->in6p_socket;
813 ro = (struct route *)&in6p->in6p_route;
814 }
815 #endif
816
817 #ifdef RTV_RTT
818 /*
819 * If we sent enough data to get some meaningful characteristics,
820 * save them in the routing entry. 'Enough' is arbitrarily
821 * defined as the sendpipesize (default 4K) * 16. This would
822 * give us 16 rtt samples assuming we only get one sample per
823 * window (the usual case on a long haul net). 16 samples is
824 * enough for the srtt filter to converge to within 5% of the correct
825 * value; fewer samples and we could save a very bogus rtt.
826 *
827 * Don't update the default route's characteristics and don't
828 * update anything that the user "locked".
829 */
830 if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
831 ro && (rt = ro->ro_rt) &&
832 !in_nullhost(satosin(rt_key(rt))->sin_addr)) {
833 register u_long i = 0;
834
835 if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
836 i = tp->t_srtt *
837 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
838 if (rt->rt_rmx.rmx_rtt && i)
839 /*
840 * filter this update to half the old & half
841 * the new values, converting scale.
842 * See route.h and tcp_var.h for a
843 * description of the scaling constants.
844 */
845 rt->rt_rmx.rmx_rtt =
846 (rt->rt_rmx.rmx_rtt + i) / 2;
847 else
848 rt->rt_rmx.rmx_rtt = i;
849 }
850 if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
851 i = tp->t_rttvar *
852 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTTVAR_SHIFT + 2));
853 if (rt->rt_rmx.rmx_rttvar && i)
854 rt->rt_rmx.rmx_rttvar =
855 (rt->rt_rmx.rmx_rttvar + i) / 2;
856 else
857 rt->rt_rmx.rmx_rttvar = i;
858 }
859 /*
860 * update the pipelimit (ssthresh) if it has been updated
861 * already or if a pipesize was specified & the threshhold
862 * got below half the pipesize. I.e., wait for bad news
863 * before we start updating, then update on both good
864 * and bad news.
865 */
866 if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
867 (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) ||
868 i < (rt->rt_rmx.rmx_sendpipe / 2)) {
869 /*
870 * convert the limit from user data bytes to
871 * packets then to packet data bytes.
872 */
873 i = (i + tp->t_segsz / 2) / tp->t_segsz;
874 if (i < 2)
875 i = 2;
876 i *= (u_long)(tp->t_segsz + sizeof (struct tcpiphdr));
877 if (rt->rt_rmx.rmx_ssthresh)
878 rt->rt_rmx.rmx_ssthresh =
879 (rt->rt_rmx.rmx_ssthresh + i) / 2;
880 else
881 rt->rt_rmx.rmx_ssthresh = i;
882 }
883 }
884 #endif /* RTV_RTT */
885 /* free the reassembly queue, if any */
886 TCP_REASS_LOCK(tp);
887 (void) tcp_freeq(tp);
888 TCP_REASS_UNLOCK(tp);
889
890 TCP_CLEAR_DELACK(tp);
891
892 if (tp->t_template) {
893 m_free(tp->t_template);
894 tp->t_template = NULL;
895 }
896 pool_put(&tcpcb_pool, tp);
897 if (inp) {
898 inp->inp_ppcb = 0;
899 soisdisconnected(so);
900 in_pcbdetach(inp);
901 }
902 #ifdef INET6
903 else if (in6p) {
904 in6p->in6p_ppcb = 0;
905 soisdisconnected(so);
906 in6_pcbdetach(in6p);
907 }
908 #endif
909 tcpstat.tcps_closed++;
910 return ((struct tcpcb *)0);
911 }
912
913 int
914 tcp_freeq(tp)
915 struct tcpcb *tp;
916 {
917 register struct ipqent *qe;
918 int rv = 0;
919 #ifdef TCPREASS_DEBUG
920 int i = 0;
921 #endif
922
923 TCP_REASS_LOCK_CHECK(tp);
924
925 while ((qe = tp->segq.lh_first) != NULL) {
926 #ifdef TCPREASS_DEBUG
927 printf("tcp_freeq[%p,%d]: %u:%u(%u) 0x%02x\n",
928 tp, i++, qe->ipqe_seq, qe->ipqe_seq + qe->ipqe_len,
929 qe->ipqe_len, qe->ipqe_flags & (TH_SYN|TH_FIN|TH_RST));
930 #endif
931 LIST_REMOVE(qe, ipqe_q);
932 LIST_REMOVE(qe, ipqe_timeq);
933 m_freem(qe->ipqe_m);
934 pool_put(&ipqent_pool, qe);
935 rv = 1;
936 }
937 return (rv);
938 }
939
940 /*
941 * Protocol drain routine. Called when memory is in short supply.
942 */
943 void
944 tcp_drain()
945 {
946 register struct inpcb *inp;
947 register struct tcpcb *tp;
948
949 /*
950 * Free the sequence queue of all TCP connections.
951 */
952 inp = tcbtable.inpt_queue.cqh_first;
953 if (inp) /* XXX */
954 for (; inp != (struct inpcb *)&tcbtable.inpt_queue;
955 inp = inp->inp_queue.cqe_next) {
956 if ((tp = intotcpcb(inp)) != NULL) {
957 /*
958 * We may be called from a device's interrupt
959 * context. If the tcpcb is already busy,
960 * just bail out now.
961 */
962 if (tcp_reass_lock_try(tp) == 0)
963 continue;
964 if (tcp_freeq(tp))
965 tcpstat.tcps_connsdrained++;
966 TCP_REASS_UNLOCK(tp);
967 }
968 }
969 }
970
971 /*
972 * Notify a tcp user of an asynchronous error;
973 * store error as soft error, but wake up user
974 * (for now, won't do anything until can select for soft error).
975 */
976 void
977 tcp_notify(inp, error)
978 struct inpcb *inp;
979 int error;
980 {
981 register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
982 register struct socket *so = inp->inp_socket;
983
984 /*
985 * Ignore some errors if we are hooked up.
986 * If connection hasn't completed, has retransmitted several times,
987 * and receives a second error, give up now. This is better
988 * than waiting a long time to establish a connection that
989 * can never complete.
990 */
991 if (tp->t_state == TCPS_ESTABLISHED &&
992 (error == EHOSTUNREACH || error == ENETUNREACH ||
993 error == EHOSTDOWN)) {
994 return;
995 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
996 tp->t_rxtshift > 3 && tp->t_softerror)
997 so->so_error = error;
998 else
999 tp->t_softerror = error;
1000 wakeup((caddr_t) &so->so_timeo);
1001 sorwakeup(so);
1002 sowwakeup(so);
1003 }
1004
1005 #if defined(INET6) && !defined(TCP6)
1006 void
1007 tcp6_ctlinput(cmd, sa, ip6, m, off)
1008 int cmd;
1009 struct sockaddr *sa;
1010 register struct ip6_hdr *ip6;
1011 struct mbuf *m;
1012 int off;
1013 {
1014 (void)tcp_ctlinput(cmd, sa, (void *)ip6);
1015 }
1016 #endif
1017
1018 /* assumes that ip header and tcp header are contiguous on mbuf */
1019 void *
1020 tcp_ctlinput(cmd, sa, v)
1021 int cmd;
1022 struct sockaddr *sa;
1023 register void *v;
1024 {
1025 register struct ip *ip = v;
1026 register struct tcphdr *th;
1027 extern int inetctlerrmap[];
1028 void (*notify) __P((struct inpcb *, int)) = tcp_notify;
1029 int errno;
1030 int nmatch;
1031
1032 if ((unsigned)cmd >= PRC_NCMDS)
1033 return NULL;
1034 errno = inetctlerrmap[cmd];
1035 if (cmd == PRC_QUENCH)
1036 notify = tcp_quench;
1037 else if (PRC_IS_REDIRECT(cmd))
1038 notify = in_rtchange, ip = 0;
1039 else if (cmd == PRC_MSGSIZE && ip_mtudisc)
1040 notify = tcp_mtudisc, ip = 0;
1041 else if (cmd == PRC_HOSTDEAD)
1042 ip = 0;
1043 else if (errno == 0)
1044 return NULL;
1045 if (ip && ip->ip_v == 4 && sa->sa_family == AF_INET) {
1046 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1047 nmatch = in_pcbnotify(&tcbtable, satosin(sa)->sin_addr,
1048 th->th_dport, ip->ip_src, th->th_sport, errno, notify);
1049 if (nmatch == 0 && syn_cache_count &&
1050 (inetctlerrmap[cmd] == EHOSTUNREACH ||
1051 inetctlerrmap[cmd] == ENETUNREACH ||
1052 inetctlerrmap[cmd] == EHOSTDOWN)) {
1053 struct sockaddr_in sin;
1054 bzero(&sin, sizeof(sin));
1055 sin.sin_len = sizeof(sin);
1056 sin.sin_family = AF_INET;
1057 sin.sin_port = th->th_sport;
1058 sin.sin_addr = ip->ip_src;
1059 syn_cache_unreach((struct sockaddr *)&sin, sa, th);
1060 }
1061
1062 /* XXX mapped address case */
1063 }
1064 #ifdef INET6
1065 else if (ip && ip->ip_v == 6 && sa->sa_family == AF_INET6) {
1066 /* XXX do something for ip6 */
1067 }
1068 #endif
1069 else {
1070 (void)in_pcbnotifyall(&tcbtable, satosin(sa)->sin_addr, errno,
1071 notify);
1072 #ifdef INET6
1073 /* XXX do something for ip6 */
1074 #endif
1075 }
1076 return NULL;
1077 }
1078
1079 /*
1080 * When a source quence is received, we are being notifed of congestion.
1081 * Close the congestion window down to the Loss Window (one segment).
1082 * We will gradually open it again as we proceed.
1083 */
1084 void
1085 tcp_quench(inp, errno)
1086 struct inpcb *inp;
1087 int errno;
1088 {
1089 struct tcpcb *tp = intotcpcb(inp);
1090
1091 if (tp)
1092 tp->snd_cwnd = tp->t_segsz;
1093 }
1094
1095 /*
1096 * On receipt of path MTU corrections, flush old route and replace it
1097 * with the new one. Retransmit all unacknowledged packets, to ensure
1098 * that all packets will be received.
1099 */
1100 void
1101 tcp_mtudisc(inp, errno)
1102 struct inpcb *inp;
1103 int errno;
1104 {
1105 struct tcpcb *tp = intotcpcb(inp);
1106 struct rtentry *rt = in_pcbrtentry(inp);
1107
1108 if (tp != 0) {
1109 if (rt != 0) {
1110 /*
1111 * If this was not a host route, remove and realloc.
1112 */
1113 if ((rt->rt_flags & RTF_HOST) == 0) {
1114 in_rtchange(inp, errno);
1115 if ((rt = in_pcbrtentry(inp)) == 0)
1116 return;
1117 }
1118
1119 /*
1120 * Slow start out of the error condition. We
1121 * use the MTU because we know it's smaller
1122 * than the previously transmitted segment.
1123 *
1124 * Note: This is more conservative than the
1125 * suggestion in draft-floyd-incr-init-win-03.
1126 */
1127 if (rt->rt_rmx.rmx_mtu != 0)
1128 tp->snd_cwnd =
1129 TCP_INITIAL_WINDOW(tcp_init_win,
1130 rt->rt_rmx.rmx_mtu);
1131 }
1132
1133 /*
1134 * Resend unacknowledged packets.
1135 */
1136 tp->snd_nxt = tp->snd_una;
1137 tcp_output(tp);
1138 }
1139 }
1140
1141
1142 /*
1143 * Compute the MSS to advertise to the peer. Called only during
1144 * the 3-way handshake. If we are the server (peer initiated
1145 * connection), we are called with a pointer to the interface
1146 * on which the SYN packet arrived. If we are the client (we
1147 * initiated connection), we are called with a pointer to the
1148 * interface out which this connection should go.
1149 */
1150 u_long
1151 tcp_mss_to_advertise(ifp)
1152 const struct ifnet *ifp;
1153 {
1154 extern u_long in_maxmtu;
1155 u_long mss = 0;
1156
1157 /*
1158 * In order to avoid defeating path MTU discovery on the peer,
1159 * we advertise the max MTU of all attached networks as our MSS,
1160 * per RFC 1191, section 3.1.
1161 *
1162 * We provide the option to advertise just the MTU of
1163 * the interface on which we hope this connection will
1164 * be receiving. If we are responding to a SYN, we
1165 * will have a pretty good idea about this, but when
1166 * initiating a connection there is a bit more doubt.
1167 *
1168 * We also need to ensure that loopback has a large enough
1169 * MSS, as the loopback MTU is never included in in_maxmtu.
1170 */
1171
1172 if (ifp != NULL)
1173 mss = ifp->if_mtu;
1174
1175 if (tcp_mss_ifmtu == 0)
1176 mss = max(in_maxmtu, mss);
1177
1178 if (mss > sizeof(struct tcpiphdr))
1179 mss -= sizeof(struct tcpiphdr);
1180
1181 mss = max(tcp_mssdflt, mss);
1182 return (mss);
1183 }
1184
1185 /*
1186 * Set connection variables based on the peer's advertised MSS.
1187 * We are passed the TCPCB for the actual connection. If we
1188 * are the server, we are called by the compressed state engine
1189 * when the 3-way handshake is complete. If we are the client,
1190 * we are called when we recieve the SYN,ACK from the server.
1191 *
1192 * NOTE: Our advertised MSS value must be initialized in the TCPCB
1193 * before this routine is called!
1194 */
1195 void
1196 tcp_mss_from_peer(tp, offer)
1197 struct tcpcb *tp;
1198 int offer;
1199 {
1200 struct socket *so;
1201 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1202 struct rtentry *rt;
1203 #endif
1204 u_long bufsize;
1205 int mss;
1206
1207 so = NULL;
1208 rt = NULL;
1209 if (tp->t_inpcb) {
1210 so = tp->t_inpcb->inp_socket;
1211 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1212 rt = in_pcbrtentry(tp->t_inpcb);
1213 #endif
1214 }
1215 #ifdef INET6
1216 else if (tp->t_in6pcb) {
1217 so = tp->t_in6pcb->in6p_socket;
1218 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1219 #ifdef TCP6
1220 rt = NULL;
1221 #else
1222 rt = in6_pcbrtentry(tp->t_in6pcb);
1223 #endif
1224 #endif
1225 }
1226 #endif
1227
1228 /*
1229 * As per RFC1122, use the default MSS value, unless they
1230 * sent us an offer. Do not accept offers less than 32 bytes.
1231 */
1232 mss = tcp_mssdflt;
1233 if (offer)
1234 mss = offer;
1235 mss = max(mss, 32); /* sanity */
1236 tp->t_peermss = mss;
1237 mss -= tcp_optlen(tp);
1238 if (tp->t_inpcb)
1239 mss -= ip_optlen(tp->t_inpcb);
1240 #ifdef INET6
1241 else if (tp->t_in6pcb)
1242 mss -= ip6_optlen(tp->t_in6pcb);
1243 #endif
1244
1245 /*
1246 * If there's a pipesize, change the socket buffer to that size.
1247 * Make the socket buffer an integral number of MSS units. If
1248 * the MSS is larger than the socket buffer, artificially decrease
1249 * the MSS.
1250 */
1251 #ifdef RTV_SPIPE
1252 if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0)
1253 bufsize = rt->rt_rmx.rmx_sendpipe;
1254 else
1255 #endif
1256 bufsize = so->so_snd.sb_hiwat;
1257 if (bufsize < mss)
1258 mss = bufsize;
1259 else {
1260 bufsize = roundup(bufsize, mss);
1261 if (bufsize > sb_max)
1262 bufsize = sb_max;
1263 (void) sbreserve(&so->so_snd, bufsize);
1264 }
1265 tp->t_segsz = mss;
1266
1267 #ifdef RTV_SSTHRESH
1268 if (rt != NULL && rt->rt_rmx.rmx_ssthresh) {
1269 /*
1270 * There's some sort of gateway or interface buffer
1271 * limit on the path. Use this to set the slow
1272 * start threshold, but set the threshold to no less
1273 * than 2 * MSS.
1274 */
1275 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
1276 }
1277 #endif
1278 }
1279
1280 /*
1281 * Processing necessary when a TCP connection is established.
1282 */
1283 void
1284 tcp_established(tp)
1285 struct tcpcb *tp;
1286 {
1287 struct socket *so;
1288 #ifdef RTV_RPIPE
1289 struct rtentry *rt;
1290 #endif
1291 u_long bufsize;
1292
1293 so = NULL;
1294 rt = NULL;
1295 if (tp->t_inpcb) {
1296 so = tp->t_inpcb->inp_socket;
1297 #if defined(RTV_RPIPE)
1298 rt = in_pcbrtentry(tp->t_inpcb);
1299 #endif
1300 }
1301 #ifdef INET6
1302 else if (tp->t_in6pcb) {
1303 so = tp->t_in6pcb->in6p_socket;
1304 #if defined(RTV_RPIPE)
1305 #ifdef TCP6
1306 rt = NULL;
1307 #else
1308 rt = in6_pcbrtentry(tp->t_in6pcb);
1309 #endif
1310 #endif
1311 }
1312 #endif
1313
1314 tp->t_state = TCPS_ESTABLISHED;
1315 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
1316
1317 #ifdef RTV_RPIPE
1318 if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0)
1319 bufsize = rt->rt_rmx.rmx_recvpipe;
1320 else
1321 #endif
1322 bufsize = so->so_rcv.sb_hiwat;
1323 if (bufsize > tp->t_ourmss) {
1324 bufsize = roundup(bufsize, tp->t_ourmss);
1325 if (bufsize > sb_max)
1326 bufsize = sb_max;
1327 (void) sbreserve(&so->so_rcv, bufsize);
1328 }
1329 }
1330
1331 /*
1332 * Check if there's an initial rtt or rttvar. Convert from the
1333 * route-table units to scaled multiples of the slow timeout timer.
1334 * Called only during the 3-way handshake.
1335 */
1336 void
1337 tcp_rmx_rtt(tp)
1338 struct tcpcb *tp;
1339 {
1340 #ifdef RTV_RTT
1341 struct rtentry *rt = NULL;
1342 int rtt;
1343
1344 if (tp->t_inpcb)
1345 rt = in_pcbrtentry(tp->t_inpcb);
1346 #ifdef INET6
1347 else if (tp->t_in6pcb) {
1348 #ifdef TCP6
1349 rt = NULL;
1350 #else
1351 rt = in6_pcbrtentry(tp->t_in6pcb);
1352 #endif
1353 }
1354 #endif
1355 if (rt == NULL)
1356 return;
1357
1358 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
1359 /*
1360 * XXX The lock bit for MTU indicates that the value
1361 * is also a minimum value; this is subject to time.
1362 */
1363 if (rt->rt_rmx.rmx_locks & RTV_RTT)
1364 TCPT_RANGESET(tp->t_rttmin,
1365 rtt / (RTM_RTTUNIT / PR_SLOWHZ),
1366 TCPTV_MIN, TCPTV_REXMTMAX);
1367 tp->t_srtt = rtt /
1368 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
1369 if (rt->rt_rmx.rmx_rttvar) {
1370 tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
1371 ((RTM_RTTUNIT / PR_SLOWHZ) >>
1372 (TCP_RTTVAR_SHIFT + 2));
1373 } else {
1374 /* Default variation is +- 1 rtt */
1375 tp->t_rttvar =
1376 tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT);
1377 }
1378 TCPT_RANGESET(tp->t_rxtcur,
1379 ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2),
1380 tp->t_rttmin, TCPTV_REXMTMAX);
1381 }
1382 #endif
1383 }
1384
1385 tcp_seq tcp_iss_seq = 0; /* tcp initial seq # */
1386
1387 /*
1388 * Get a new sequence value given a tcp control block
1389 */
1390 tcp_seq
1391 tcp_new_iss(tp, len, addin)
1392 void *tp;
1393 u_long len;
1394 tcp_seq addin;
1395 {
1396 tcp_seq tcp_iss;
1397
1398 /*
1399 * Randomize.
1400 */
1401 #if NRND > 0
1402 rnd_extract_data(&tcp_iss, sizeof(tcp_iss), RND_EXTRACT_ANY);
1403 #else
1404 tcp_iss = random();
1405 #endif
1406
1407 /*
1408 * If we were asked to add some amount to a known value,
1409 * we will take a random value obtained above, mask off the upper
1410 * bits, and add in the known value. We also add in a constant to
1411 * ensure that we are at least a certain distance from the original
1412 * value.
1413 *
1414 * This is used when an old connection is in timed wait
1415 * and we have a new one coming in, for instance.
1416 */
1417 if (addin != 0) {
1418 #ifdef TCPISS_DEBUG
1419 printf("Random %08x, ", tcp_iss);
1420 #endif
1421 tcp_iss &= TCP_ISS_RANDOM_MASK;
1422 tcp_iss += addin + TCP_ISSINCR;
1423 #ifdef TCPISS_DEBUG
1424 printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss);
1425 #endif
1426 } else {
1427 tcp_iss &= TCP_ISS_RANDOM_MASK;
1428 tcp_iss += tcp_iss_seq;
1429 tcp_iss_seq += TCP_ISSINCR;
1430 #ifdef TCPISS_DEBUG
1431 printf("ISS %08x\n", tcp_iss);
1432 #endif
1433 }
1434
1435 if (tcp_compat_42) {
1436 /*
1437 * Limit it to the positive range for really old TCP
1438 * implementations.
1439 */
1440 if (tcp_iss >= 0x80000000)
1441 tcp_iss &= 0x7fffffff; /* XXX */
1442 }
1443
1444 return tcp_iss;
1445 }
1446
1447 #ifdef IPSEC
1448 /* compute ESP/AH header size for TCP, including outer IP header. */
1449 size_t
1450 ipsec4_hdrsiz_tcp(tp)
1451 struct tcpcb *tp;
1452 {
1453 struct inpcb *inp;
1454 size_t hdrsiz;
1455
1456 /* XXX mapped addr case (tp->t_in6pcb) */
1457 if (!tp || !tp->t_template || !(inp = tp->t_inpcb))
1458 return 0;
1459 switch (tp->t_family) {
1460 case AF_INET:
1461 hdrsiz = ipsec4_hdrsiz(tp->t_template, inp);
1462 break;
1463 default:
1464 hdrsiz = 0;
1465 break;
1466 }
1467
1468 return hdrsiz;
1469 }
1470
1471 #if defined(INET6) && !defined(TCP6)
1472 size_t
1473 ipsec6_hdrsiz_tcp(tp)
1474 struct tcpcb *tp;
1475 {
1476 struct in6pcb *in6p;
1477 size_t hdrsiz;
1478
1479 if (!tp || !tp->t_template || !(in6p = tp->t_in6pcb))
1480 return 0;
1481 switch (tp->t_family) {
1482 case AF_INET6:
1483 hdrsiz = ipsec6_hdrsiz(tp->t_template, in6p);
1484 break;
1485 case AF_INET:
1486 /* mapped address case - tricky */
1487 default:
1488 hdrsiz = 0;
1489 break;
1490 }
1491
1492 return hdrsiz;
1493 }
1494 #endif
1495 #endif /*IPSEC*/
1496
1497 /*
1498 * Determine the length of the TCP options for this connection.
1499 *
1500 * XXX: What do we do for SACK, when we add that? Just reserve
1501 * all of the space? Otherwise we can't exactly be incrementing
1502 * cwnd by an amount that varies depending on the amount we last
1503 * had to SACK!
1504 */
1505
1506 u_int
1507 tcp_optlen(tp)
1508 struct tcpcb *tp;
1509 {
1510 if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) ==
1511 (TF_REQ_TSTMP | TF_RCVD_TSTMP))
1512 return TCPOLEN_TSTAMP_APPA;
1513 else
1514 return 0;
1515 }
1516