tcp_subr.c revision 1.84 1 /* $NetBSD: tcp_subr.c,v 1.84 1999/12/13 15:17:20 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_ipsec.h"
106 #include "opt_tcp_compat_42.h"
107 #include "rnd.h"
108
109 #include <sys/param.h>
110 #include <sys/proc.h>
111 #include <sys/systm.h>
112 #include <sys/malloc.h>
113 #include <sys/mbuf.h>
114 #include <sys/socket.h>
115 #include <sys/socketvar.h>
116 #include <sys/protosw.h>
117 #include <sys/errno.h>
118 #include <sys/kernel.h>
119 #include <sys/pool.h>
120 #if NRND > 0
121 #include <sys/rnd.h>
122 #endif
123
124 #include <net/route.h>
125 #include <net/if.h>
126
127 #include <netinet/in.h>
128 #include <netinet/in_systm.h>
129 #include <netinet/ip.h>
130 #include <netinet/in_pcb.h>
131 #include <netinet/ip_var.h>
132 #include <netinet/ip_icmp.h>
133
134 #ifdef INET6
135 #ifndef INET
136 #include <netinet/in.h>
137 #endif
138 #include <netinet/ip6.h>
139 #include <netinet6/in6_pcb.h>
140 #include <netinet6/ip6_var.h>
141 #include <netinet6/in6_var.h>
142 #include <netinet6/ip6protosw.h>
143 #endif
144
145 #include <netinet/tcp.h>
146 #include <netinet/tcp_fsm.h>
147 #include <netinet/tcp_seq.h>
148 #include <netinet/tcp_timer.h>
149 #include <netinet/tcp_var.h>
150 #include <netinet/tcpip.h>
151
152 #ifdef IPSEC
153 #include <netinet6/ipsec.h>
154 #endif /*IPSEC*/
155
156 #ifdef INET6
157 struct in6pcb tcb6;
158 #endif
159
160 /* patchable/settable parameters for tcp */
161 int tcp_mssdflt = TCP_MSS;
162 int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
163 int tcp_do_rfc1323 = 1; /* window scaling / timestamps (obsolete) */
164 int tcp_do_sack = 1; /* selective acknowledgement */
165 int tcp_do_win_scale = 1; /* RFC1323 window scaling */
166 int tcp_do_timestamps = 1; /* RFC1323 timestamps */
167 int tcp_do_newreno = 0; /* Use the New Reno algorithms */
168 int tcp_ack_on_push = 0; /* set to enable immediate ACK-on-PUSH */
169 int tcp_init_win = 1;
170 int tcp_mss_ifmtu = 0;
171 #ifdef TCP_COMPAT_42
172 int tcp_compat_42 = 1;
173 #else
174 int tcp_compat_42 = 0;
175 #endif
176
177 #ifndef TCBHASHSIZE
178 #define TCBHASHSIZE 128
179 #endif
180 int tcbhashsize = TCBHASHSIZE;
181
182 int tcp_freeq __P((struct tcpcb *));
183
184 struct pool tcpcb_pool;
185
186 /*
187 * Tcp initialization
188 */
189 void
190 tcp_init()
191 {
192 int hlen;
193
194 pool_init(&tcpcb_pool, sizeof(struct tcpcb), 0, 0, 0, "tcpcbpl",
195 0, NULL, NULL, M_PCB);
196 in_pcbinit(&tcbtable, tcbhashsize, tcbhashsize);
197 #ifdef INET6
198 tcb6.in6p_next = tcb6.in6p_prev = &tcb6;
199 #endif
200 LIST_INIT(&tcp_delacks);
201
202 hlen = sizeof(struct ip) + sizeof(struct tcphdr);
203 #ifdef INET6
204 if (sizeof(struct ip) < sizeof(struct ip6_hdr))
205 hlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
206 #endif
207 if (max_protohdr < hlen)
208 max_protohdr = hlen;
209 if (max_linkhdr + hlen > MHLEN)
210 panic("tcp_init");
211
212 /* Initialize the compressed state engine. */
213 syn_cache_init();
214 }
215
216 /*
217 * Create template to be used to send tcp packets on a connection.
218 * Call after host entry created, allocates an mbuf and fills
219 * in a skeletal tcp/ip header, minimizing the amount of work
220 * necessary when the connection is used.
221 */
222 struct mbuf *
223 tcp_template(tp)
224 struct tcpcb *tp;
225 {
226 register struct inpcb *inp = tp->t_inpcb;
227 #ifdef INET6
228 register struct in6pcb *in6p = tp->t_in6pcb;
229 #endif
230 register struct tcphdr *n;
231 register struct mbuf *m;
232 int hlen;
233
234 switch (tp->t_family) {
235 case AF_INET:
236 hlen = sizeof(struct ip);
237 if (inp)
238 break;
239 #ifdef INET6
240 if (in6p) {
241 /* mapped addr case */
242 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
243 && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
244 break;
245 }
246 #endif
247 return NULL; /*EINVAL*/
248 #ifdef INET6
249 case AF_INET6:
250 hlen = sizeof(struct ip6_hdr);
251 if (in6p) {
252 /* more sainty check? */
253 break;
254 }
255 return NULL; /*EINVAL*/
256 #endif
257 default:
258 hlen = 0; /*pacify gcc*/
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_pkthdr.len = 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, th0, ack, seq, flags)
356 struct tcpcb *tp;
357 struct mbuf *template;
358 register struct mbuf *m;
359 struct tcphdr *th0;
360 tcp_seq ack, seq;
361 int flags;
362 {
363 #ifndef INET6
364 struct route iproute;
365 #else
366 struct route_in6 iproute; /* sizeof(route_in6) > sizeof(route) */
367 #endif
368 struct route *ro;
369 struct rtentry *rt;
370 int error, tlen, win = 0;
371 int hlen;
372 struct ip *ip;
373 #ifdef INET6
374 struct ip6_hdr *ip6;
375 #endif
376 int family; /* family on packet, not inpcb/in6pcb! */
377 struct tcphdr *th;
378
379 if (tp != NULL && (flags & TH_RST) == 0) {
380 if (tp->t_inpcb)
381 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
382 #ifdef INET6
383 else if (tp->t_in6pcb)
384 win = sbspace(&tp->t_in6pcb->in6p_socket->so_rcv);
385 #endif
386 }
387
388 ip = NULL;
389 #ifdef INET6
390 ip6 = NULL;
391 #endif
392 if (m == 0) {
393 if (!template)
394 return EINVAL;
395
396 /* get family information from template */
397 switch (mtod(template, struct ip *)->ip_v) {
398 case 4:
399 family = AF_INET;
400 hlen = sizeof(struct ip);
401 break;
402 #ifdef INET6
403 case 6:
404 family = AF_INET6;
405 hlen = sizeof(struct ip6_hdr);
406 break;
407 #endif
408 default:
409 return EAFNOSUPPORT;
410 }
411
412 MGETHDR(m, M_DONTWAIT, MT_HEADER);
413 if (m) {
414 MCLGET(m, M_DONTWAIT);
415 if ((m->m_flags & M_EXT) == 0) {
416 m_free(m);
417 m = NULL;
418 }
419 }
420 if (m == NULL)
421 return (ENOBUFS);
422
423 if (tcp_compat_42)
424 tlen = 1;
425 else
426 tlen = 0;
427
428 m->m_data += max_linkhdr;
429 bcopy(mtod(template, caddr_t), mtod(m, caddr_t),
430 template->m_len);
431 switch (family) {
432 case AF_INET:
433 ip = mtod(m, struct ip *);
434 th = (struct tcphdr *)(ip + 1);
435 break;
436 #ifdef INET6
437 case AF_INET6:
438 ip6 = mtod(m, struct ip6_hdr *);
439 th = (struct tcphdr *)(ip6 + 1);
440 break;
441 #endif
442 #if 0
443 default:
444 /* noone will visit here */
445 m_freem(m);
446 return EAFNOSUPPORT;
447 #endif
448 }
449 flags = TH_ACK;
450 } else {
451 /* get family information from m */
452 switch (mtod(m, struct ip *)->ip_v) {
453 case 4:
454 family = AF_INET;
455 hlen = sizeof(struct ip);
456 break;
457 #ifdef INET6
458 case 6:
459 family = AF_INET6;
460 hlen = sizeof(struct ip6_hdr);
461 break;
462 #endif
463 default:
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 ip->ip_p = IPPROTO_TCP;
486 break;
487 #ifdef INET6
488 case AF_INET6:
489 ip6 = mtod(m, struct ip6_hdr *);
490 th = (struct tcphdr *)(ip6 + 1);
491 xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
492 ip6->ip6_nxt = IPPROTO_TCP;
493 break;
494 #endif
495 }
496 *th = *th0;
497 xchg(th->th_dport, th->th_sport, u_int16_t);
498 #undef xchg
499 }
500 th->th_seq = htonl(seq);
501 th->th_ack = htonl(ack);
502 th->th_x2 = 0;
503 if ((flags & TH_SYN) == 0) {
504 if (tp)
505 th->th_win = htons((u_int16_t) (win >> tp->rcv_scale));
506 else
507 th->th_win = htons((u_int16_t)win);
508 th->th_off = sizeof (struct tcphdr) >> 2;
509 tlen += sizeof (struct tcphdr);
510 } else
511 tlen += th->th_off << 2;
512 m->m_len = hlen + tlen;
513 m->m_pkthdr.len = hlen + tlen;
514 m->m_pkthdr.rcvif = (struct ifnet *) 0;
515 th->th_flags = flags;
516 th->th_urp = 0;
517
518 switch (family) {
519 case AF_INET:
520 {
521 struct ipovly *ipov = (struct ipovly *)ip;
522 bzero(ipov->ih_x1, sizeof ipov->ih_x1);
523 ipov->ih_len = htons((u_int16_t)tlen);
524
525 th->th_sum = 0;
526 th->th_sum = in_cksum(m, hlen + tlen);
527 ip->ip_len = hlen + tlen; /*will be flipped on output*/
528 ip->ip_ttl = ip_defttl;
529 break;
530 }
531 #ifdef INET6
532 case AF_INET6:
533 {
534 th->th_sum = 0;
535 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
536 tlen);
537 ip6->ip6_plen = ntohs(tlen);
538 if (tp && tp->t_in6pcb) {
539 struct ifnet *oifp;
540 ro = (struct route *)&tp->t_in6pcb->in6p_route;
541 oifp = ro->ro_rt ? ro->ro_rt->rt_ifp : NULL;
542 ip6->ip6_hlim = in6_selecthlim(tp->t_in6pcb, oifp);
543 } else
544 ip6->ip6_hlim = ip6_defhlim;
545 ip6->ip6_flow &= ~IPV6_FLOWINFO_MASK;
546 if (ip6_auto_flowlabel) {
547 ip6->ip6_flow |=
548 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
549 }
550 break;
551 }
552 #endif
553 }
554
555 #ifdef IPSEC
556 m->m_pkthdr.rcvif = NULL;
557 #endif /*IPSEC*/
558
559 /*
560 * If we're doing Path MTU discovery, we need to set DF unless
561 * the route's MTU is locked. If we lack a route, we need to
562 * look it up now.
563 *
564 * ip_output() could do this for us, but it's convenient to just
565 * do it here unconditionally.
566 */
567 if (tp != NULL && tp->t_inpcb != NULL) {
568 ro = &tp->t_inpcb->inp_route;
569 #ifdef IPSEC
570 m->m_pkthdr.rcvif = (struct ifnet *)tp->t_inpcb->inp_socket;
571 #endif
572 #ifdef DIAGNOSTIC
573 if (family != AF_INET)
574 panic("tcp_respond: address family mismatch");
575 if (!in_hosteq(ip->ip_dst, tp->t_inpcb->inp_faddr)) {
576 panic("tcp_respond: ip_dst %x != inp_faddr %x",
577 ntohl(ip->ip_dst.s_addr),
578 ntohl(tp->t_inpcb->inp_faddr.s_addr));
579 }
580 #endif
581 }
582 #ifdef INET6
583 else if (tp != NULL && tp->t_in6pcb != NULL) {
584 ro = (struct route *)&tp->t_in6pcb->in6p_route;
585 #ifdef IPSEC
586 m->m_pkthdr.rcvif = (struct ifnet *)tp->t_in6pcb->in6p_socket;
587 #endif
588 #ifdef DIAGNOSTIC
589 if (family == AF_INET) {
590 if (!IN6_IS_ADDR_V4MAPPED(&tp->t_in6pcb->in6p_faddr))
591 panic("tcp_respond: not mapped addr");
592 if (bcmp(&ip->ip_dst,
593 &tp->t_in6pcb->in6p_faddr.s6_addr32[3],
594 sizeof(ip->ip_dst)) != 0) {
595 panic("tcp_respond: ip_dst != in6p_faddr");
596 }
597 } else if (family == AF_INET6) {
598 if (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &tp->t_in6pcb->in6p_faddr))
599 panic("tcp_respond: ip6_dst != in6p_faddr");
600 } else
601 panic("tcp_respond: address family mismatch");
602 #endif
603 }
604 #endif
605 else {
606 ro = (struct route *)&iproute;
607 bzero(ro, sizeof(iproute));
608 }
609 if ((rt = ro->ro_rt) == NULL || (rt->rt_flags & RTF_UP) == 0) {
610 if (ro->ro_rt != NULL) {
611 RTFREE(ro->ro_rt);
612 ro->ro_rt = NULL;
613 }
614 switch (family) {
615 case AF_INET:
616 {
617 struct sockaddr_in *dst;
618 dst = satosin(&ro->ro_dst);
619 dst->sin_family = AF_INET;
620 dst->sin_len = sizeof(*dst);
621 dst->sin_addr = ip->ip_dst;
622 break;
623 }
624 #ifdef INET6
625 case AF_INET6:
626 {
627 struct sockaddr_in6 *dst;
628 dst = satosin6(&ro->ro_dst);
629 bzero(dst, sizeof(*dst));
630 dst->sin6_family = AF_INET6;
631 dst->sin6_len = sizeof(*dst);
632 dst->sin6_addr = ip6->ip6_dst;
633 break;
634 }
635 #endif
636 }
637 rtalloc(ro);
638 if ((rt = ro->ro_rt) == NULL) {
639 m_freem(m);
640 switch (family) {
641 case AF_INET:
642 ipstat.ips_noroute++;
643 break;
644 #ifdef INET6
645 case AF_INET6:
646 ip6stat.ip6s_noroute++;
647 break;
648 #endif
649 }
650 return (EHOSTUNREACH);
651 }
652 }
653 switch (family) {
654 case AF_INET:
655 if (ip_mtudisc != 0 && (rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
656 ip->ip_off |= IP_DF;
657
658 error = ip_output(m, NULL, ro, 0, NULL);
659 break;
660 #ifdef INET6
661 case AF_INET6:
662 error = ip6_output(m, NULL, (struct route_in6 *)ro, 0, NULL,
663 NULL);
664 break;
665 #endif
666 default:
667 error = EAFNOSUPPORT;
668 break;
669 }
670
671 if (ro == (struct route *)&iproute) {
672 RTFREE(ro->ro_rt);
673 ro->ro_rt = NULL;
674 }
675
676 return (error);
677 }
678
679 /*
680 * Create a new TCP control block, making an
681 * empty reassembly queue and hooking it to the argument
682 * protocol control block.
683 */
684 struct tcpcb *
685 tcp_newtcpcb(family, aux)
686 int family; /* selects inpcb, or in6pcb */
687 void *aux;
688 {
689 register struct tcpcb *tp;
690
691 switch (family) {
692 case PF_INET:
693 break;
694 #ifdef INET6
695 case PF_INET6:
696 break;
697 #endif
698 default:
699 return NULL;
700 }
701
702 tp = pool_get(&tcpcb_pool, PR_NOWAIT);
703 if (tp == NULL)
704 return (NULL);
705 bzero((caddr_t)tp, sizeof(struct tcpcb));
706 LIST_INIT(&tp->segq);
707 LIST_INIT(&tp->timeq);
708 tp->t_family = family; /* may be overridden later on */
709 tp->t_peermss = tcp_mssdflt;
710 tp->t_ourmss = tcp_mssdflt;
711 tp->t_segsz = tcp_mssdflt;
712 LIST_INIT(&tp->t_sc);
713
714 tp->t_flags = 0;
715 if (tcp_do_rfc1323 && tcp_do_win_scale)
716 tp->t_flags |= TF_REQ_SCALE;
717 if (tcp_do_rfc1323 && tcp_do_timestamps)
718 tp->t_flags |= TF_REQ_TSTMP;
719 if (tcp_do_sack == 2)
720 tp->t_flags |= TF_WILL_SACK;
721 else if (tcp_do_sack == 1)
722 tp->t_flags |= TF_WILL_SACK|TF_IGNR_RXSACK;
723 tp->t_flags |= TF_CANT_TXSACK;
724 switch (family) {
725 case PF_INET:
726 tp->t_inpcb = (struct inpcb *)aux;
727 break;
728 #ifdef INET6
729 case PF_INET6:
730 tp->t_in6pcb = (struct in6pcb *)aux;
731 break;
732 #endif
733 }
734 /*
735 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
736 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives
737 * reasonable initial retransmit time.
738 */
739 tp->t_srtt = TCPTV_SRTTBASE;
740 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1);
741 tp->t_rttmin = TCPTV_MIN;
742 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
743 TCPTV_MIN, TCPTV_REXMTMAX);
744 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
745 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
746 if (family == AF_INET) {
747 struct inpcb *inp = (struct inpcb *)aux;
748 inp->inp_ip.ip_ttl = ip_defttl;
749 inp->inp_ppcb = (caddr_t)tp;
750 }
751 #ifdef INET6
752 else if (family == AF_INET6) {
753 struct in6pcb *in6p = (struct in6pcb *)aux;
754 in6p->in6p_ip6.ip6_hlim = in6_selecthlim(in6p,
755 in6p->in6p_route.ro_rt ? in6p->in6p_route.ro_rt->rt_ifp
756 : NULL);
757 in6p->in6p_ppcb = (caddr_t)tp;
758 }
759 #endif
760 return (tp);
761 }
762
763 /*
764 * Drop a TCP connection, reporting
765 * the specified error. If connection is synchronized,
766 * then send a RST to peer.
767 */
768 struct tcpcb *
769 tcp_drop(tp, errno)
770 register struct tcpcb *tp;
771 int errno;
772 {
773 struct socket *so;
774
775 if (tp->t_inpcb)
776 so = tp->t_inpcb->inp_socket;
777 #ifdef INET6
778 else if (tp->t_in6pcb)
779 so = tp->t_in6pcb->in6p_socket;
780 #endif
781 else
782 return NULL;
783
784 if (TCPS_HAVERCVDSYN(tp->t_state)) {
785 tp->t_state = TCPS_CLOSED;
786 (void) tcp_output(tp);
787 tcpstat.tcps_drops++;
788 } else
789 tcpstat.tcps_conndrops++;
790 if (errno == ETIMEDOUT && tp->t_softerror)
791 errno = tp->t_softerror;
792 so->so_error = errno;
793 return (tcp_close(tp));
794 }
795
796 /*
797 * Close a TCP control block:
798 * discard all space held by the tcp
799 * discard internet protocol block
800 * wake up any sleepers
801 */
802 struct tcpcb *
803 tcp_close(tp)
804 register struct tcpcb *tp;
805 {
806 struct inpcb *inp;
807 #ifdef INET6
808 struct in6pcb *in6p;
809 #endif
810 struct socket *so;
811 #ifdef RTV_RTT
812 register struct rtentry *rt;
813 #endif
814 struct route *ro;
815
816 inp = tp->t_inpcb;
817 #ifdef INET6
818 in6p = tp->t_in6pcb;
819 #endif
820 so = NULL;
821 ro = NULL;
822 if (inp) {
823 so = inp->inp_socket;
824 ro = &inp->inp_route;
825 }
826 #ifdef INET6
827 else if (in6p) {
828 so = in6p->in6p_socket;
829 ro = (struct route *)&in6p->in6p_route;
830 }
831 #endif
832
833 #ifdef RTV_RTT
834 /*
835 * If we sent enough data to get some meaningful characteristics,
836 * save them in the routing entry. 'Enough' is arbitrarily
837 * defined as the sendpipesize (default 4K) * 16. This would
838 * give us 16 rtt samples assuming we only get one sample per
839 * window (the usual case on a long haul net). 16 samples is
840 * enough for the srtt filter to converge to within 5% of the correct
841 * value; fewer samples and we could save a very bogus rtt.
842 *
843 * Don't update the default route's characteristics and don't
844 * update anything that the user "locked".
845 */
846 if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
847 ro && (rt = ro->ro_rt) &&
848 !in_nullhost(satosin(rt_key(rt))->sin_addr)) {
849 register u_long i = 0;
850
851 if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
852 i = tp->t_srtt *
853 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
854 if (rt->rt_rmx.rmx_rtt && i)
855 /*
856 * filter this update to half the old & half
857 * the new values, converting scale.
858 * See route.h and tcp_var.h for a
859 * description of the scaling constants.
860 */
861 rt->rt_rmx.rmx_rtt =
862 (rt->rt_rmx.rmx_rtt + i) / 2;
863 else
864 rt->rt_rmx.rmx_rtt = i;
865 }
866 if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
867 i = tp->t_rttvar *
868 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTTVAR_SHIFT + 2));
869 if (rt->rt_rmx.rmx_rttvar && i)
870 rt->rt_rmx.rmx_rttvar =
871 (rt->rt_rmx.rmx_rttvar + i) / 2;
872 else
873 rt->rt_rmx.rmx_rttvar = i;
874 }
875 /*
876 * update the pipelimit (ssthresh) if it has been updated
877 * already or if a pipesize was specified & the threshhold
878 * got below half the pipesize. I.e., wait for bad news
879 * before we start updating, then update on both good
880 * and bad news.
881 */
882 if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
883 (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) ||
884 i < (rt->rt_rmx.rmx_sendpipe / 2)) {
885 /*
886 * convert the limit from user data bytes to
887 * packets then to packet data bytes.
888 */
889 i = (i + tp->t_segsz / 2) / tp->t_segsz;
890 if (i < 2)
891 i = 2;
892 i *= (u_long)(tp->t_segsz + sizeof (struct tcpiphdr));
893 if (rt->rt_rmx.rmx_ssthresh)
894 rt->rt_rmx.rmx_ssthresh =
895 (rt->rt_rmx.rmx_ssthresh + i) / 2;
896 else
897 rt->rt_rmx.rmx_ssthresh = i;
898 }
899 }
900 #endif /* RTV_RTT */
901 /* free the reassembly queue, if any */
902 TCP_REASS_LOCK(tp);
903 (void) tcp_freeq(tp);
904 TCP_REASS_UNLOCK(tp);
905
906 TCP_CLEAR_DELACK(tp);
907 syn_cache_cleanup(tp);
908
909 if (tp->t_template) {
910 m_free(tp->t_template);
911 tp->t_template = NULL;
912 }
913 pool_put(&tcpcb_pool, tp);
914 if (inp) {
915 inp->inp_ppcb = 0;
916 soisdisconnected(so);
917 in_pcbdetach(inp);
918 }
919 #ifdef INET6
920 else if (in6p) {
921 in6p->in6p_ppcb = 0;
922 soisdisconnected(so);
923 in6_pcbdetach(in6p);
924 }
925 #endif
926 tcpstat.tcps_closed++;
927 return ((struct tcpcb *)0);
928 }
929
930 int
931 tcp_freeq(tp)
932 struct tcpcb *tp;
933 {
934 register struct ipqent *qe;
935 int rv = 0;
936 #ifdef TCPREASS_DEBUG
937 int i = 0;
938 #endif
939
940 TCP_REASS_LOCK_CHECK(tp);
941
942 while ((qe = tp->segq.lh_first) != NULL) {
943 #ifdef TCPREASS_DEBUG
944 printf("tcp_freeq[%p,%d]: %u:%u(%u) 0x%02x\n",
945 tp, i++, qe->ipqe_seq, qe->ipqe_seq + qe->ipqe_len,
946 qe->ipqe_len, qe->ipqe_flags & (TH_SYN|TH_FIN|TH_RST));
947 #endif
948 LIST_REMOVE(qe, ipqe_q);
949 LIST_REMOVE(qe, ipqe_timeq);
950 m_freem(qe->ipqe_m);
951 pool_put(&ipqent_pool, qe);
952 rv = 1;
953 }
954 return (rv);
955 }
956
957 /*
958 * Protocol drain routine. Called when memory is in short supply.
959 */
960 void
961 tcp_drain()
962 {
963 register struct inpcb *inp;
964 register struct tcpcb *tp;
965
966 /*
967 * Free the sequence queue of all TCP connections.
968 */
969 inp = tcbtable.inpt_queue.cqh_first;
970 if (inp) /* XXX */
971 for (; inp != (struct inpcb *)&tcbtable.inpt_queue;
972 inp = inp->inp_queue.cqe_next) {
973 if ((tp = intotcpcb(inp)) != NULL) {
974 /*
975 * We may be called from a device's interrupt
976 * context. If the tcpcb is already busy,
977 * just bail out now.
978 */
979 if (tcp_reass_lock_try(tp) == 0)
980 continue;
981 if (tcp_freeq(tp))
982 tcpstat.tcps_connsdrained++;
983 TCP_REASS_UNLOCK(tp);
984 }
985 }
986 }
987
988 /*
989 * Notify a tcp user of an asynchronous error;
990 * store error as soft error, but wake up user
991 * (for now, won't do anything until can select for soft error).
992 */
993 void
994 tcp_notify(inp, error)
995 struct inpcb *inp;
996 int error;
997 {
998 register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
999 register struct socket *so = inp->inp_socket;
1000
1001 /*
1002 * Ignore some errors if we are hooked up.
1003 * If connection hasn't completed, has retransmitted several times,
1004 * and receives a second error, give up now. This is better
1005 * than waiting a long time to establish a connection that
1006 * can never complete.
1007 */
1008 if (tp->t_state == TCPS_ESTABLISHED &&
1009 (error == EHOSTUNREACH || error == ENETUNREACH ||
1010 error == EHOSTDOWN)) {
1011 return;
1012 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
1013 tp->t_rxtshift > 3 && tp->t_softerror)
1014 so->so_error = error;
1015 else
1016 tp->t_softerror = error;
1017 wakeup((caddr_t) &so->so_timeo);
1018 sorwakeup(so);
1019 sowwakeup(so);
1020 }
1021
1022 #if defined(INET6) && !defined(TCP6)
1023 void
1024 tcp6_notify(in6p, error)
1025 struct in6pcb *in6p;
1026 int error;
1027 {
1028 register struct tcpcb *tp = (struct tcpcb *)in6p->in6p_ppcb;
1029 register struct socket *so = in6p->in6p_socket;
1030
1031 /*
1032 * Ignore some errors if we are hooked up.
1033 * If connection hasn't completed, has retransmitted several times,
1034 * and receives a second error, give up now. This is better
1035 * than waiting a long time to establish a connection that
1036 * can never complete.
1037 */
1038 if (tp->t_state == TCPS_ESTABLISHED &&
1039 (error == EHOSTUNREACH || error == ENETUNREACH ||
1040 error == EHOSTDOWN)) {
1041 return;
1042 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
1043 tp->t_rxtshift > 3 && tp->t_softerror)
1044 so->so_error = error;
1045 else
1046 tp->t_softerror = error;
1047 wakeup((caddr_t) &so->so_timeo);
1048 sorwakeup(so);
1049 sowwakeup(so);
1050 }
1051 #endif
1052
1053 #if defined(INET6) && !defined(TCP6)
1054 void
1055 tcp6_ctlinput(cmd, sa, d)
1056 int cmd;
1057 struct sockaddr *sa;
1058 void *d;
1059 {
1060 register struct tcphdr *thp;
1061 struct tcphdr th;
1062 void (*notify) __P((struct in6pcb *, int)) = tcp6_notify;
1063 int nmatch;
1064 extern struct in6_addr zeroin6_addr; /* netinet6/in6_pcb.c */
1065 struct sockaddr_in6 sa6;
1066 register struct ip6_hdr *ip6;
1067 struct mbuf *m;
1068 int off;
1069
1070 if (sa->sa_family != AF_INET6 ||
1071 sa->sa_len != sizeof(struct sockaddr_in6))
1072 return;
1073 if ((unsigned)cmd >= PRC_NCMDS)
1074 return;
1075 else if (cmd == PRC_QUENCH) {
1076 /* XXX there's no PRC_QUENCH in IPv6 */
1077 notify = tcp6_quench;
1078 } else if (PRC_IS_REDIRECT(cmd))
1079 notify = in6_rtchange, d = NULL;
1080 else if (cmd == PRC_MSGSIZE)
1081 notify = tcp6_mtudisc, d = NULL;
1082 else if (cmd == PRC_HOSTDEAD)
1083 d = NULL;
1084 else if (inet6ctlerrmap[cmd] == 0)
1085 return;
1086
1087 /* translate addresses into internal form */
1088 sa6 = *(struct sockaddr_in6 *)sa;
1089 if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr))
1090 sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
1091
1092 /* if the parameter is from icmp6, decode it. */
1093 if (d != NULL) {
1094 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
1095 m = ip6cp->ip6c_m;
1096 ip6 = ip6cp->ip6c_ip6;
1097 off = ip6cp->ip6c_off;
1098 } else {
1099 m = NULL;
1100 ip6 = NULL;
1101 }
1102
1103 if (ip6) {
1104 /*
1105 * XXX: We assume that when ip6 is non NULL,
1106 * M and OFF are valid.
1107 */
1108 struct in6_addr s;
1109
1110 /* translate addresses into internal form */
1111 memcpy(&s, &ip6->ip6_src, sizeof(s));
1112 if (IN6_IS_ADDR_LINKLOCAL(&s))
1113 s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
1114
1115 if (m->m_len < off + sizeof(th)) {
1116 /*
1117 * this should be rare case,
1118 * so we compromise on this copy...
1119 */
1120 m_copydata(m, off, sizeof(th), (caddr_t)&th);
1121 thp = &th;
1122 } else
1123 thp = (struct tcphdr *)(mtod(m, caddr_t) + off);
1124 nmatch = in6_pcbnotify(&tcb6, (struct sockaddr *)&sa6,
1125 thp->th_dport, &s, thp->th_sport, cmd, notify);
1126 if (nmatch == 0 && syn_cache_count &&
1127 (inet6ctlerrmap[cmd] == EHOSTUNREACH ||
1128 inet6ctlerrmap[cmd] == ENETUNREACH ||
1129 inet6ctlerrmap[cmd] == EHOSTDOWN)) {
1130 struct sockaddr_in6 sin6;
1131 bzero(&sin6, sizeof(sin6));
1132 sin6.sin6_len = sizeof(sin6);
1133 sin6.sin6_family = AF_INET6;
1134 sin6.sin6_port = thp->th_sport;
1135 sin6.sin6_addr = s;
1136 syn_cache_unreach((struct sockaddr *)&sin6, sa, thp);
1137 }
1138 } else {
1139 (void) in6_pcbnotify(&tcb6, (struct sockaddr *)&sa6, 0,
1140 &zeroin6_addr, 0, cmd, notify);
1141 }
1142 }
1143 #endif
1144
1145 /* assumes that ip header and tcp header are contiguous on mbuf */
1146 void *
1147 tcp_ctlinput(cmd, sa, v)
1148 int cmd;
1149 struct sockaddr *sa;
1150 register void *v;
1151 {
1152 register struct ip *ip = v;
1153 register struct tcphdr *th;
1154 extern int inetctlerrmap[];
1155 void (*notify) __P((struct inpcb *, int)) = tcp_notify;
1156 int errno;
1157 int nmatch;
1158
1159 if (sa->sa_family != AF_INET ||
1160 sa->sa_len != sizeof(struct sockaddr_in))
1161 return NULL;
1162 if ((unsigned)cmd >= PRC_NCMDS)
1163 return NULL;
1164 errno = inetctlerrmap[cmd];
1165 if (cmd == PRC_QUENCH)
1166 notify = tcp_quench;
1167 else if (PRC_IS_REDIRECT(cmd))
1168 notify = in_rtchange, ip = 0;
1169 else if (cmd == PRC_MSGSIZE && ip_mtudisc)
1170 notify = tcp_mtudisc, ip = 0;
1171 else if (cmd == PRC_HOSTDEAD)
1172 ip = 0;
1173 else if (errno == 0)
1174 return NULL;
1175 if (ip && ip->ip_v == 4 && sa->sa_family == AF_INET) {
1176 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1177 nmatch = in_pcbnotify(&tcbtable, satosin(sa)->sin_addr,
1178 th->th_dport, ip->ip_src, th->th_sport, errno, notify);
1179 if (nmatch == 0 && syn_cache_count &&
1180 (inetctlerrmap[cmd] == EHOSTUNREACH ||
1181 inetctlerrmap[cmd] == ENETUNREACH ||
1182 inetctlerrmap[cmd] == EHOSTDOWN)) {
1183 struct sockaddr_in sin;
1184 bzero(&sin, sizeof(sin));
1185 sin.sin_len = sizeof(sin);
1186 sin.sin_family = AF_INET;
1187 sin.sin_port = th->th_sport;
1188 sin.sin_addr = ip->ip_src;
1189 syn_cache_unreach((struct sockaddr *)&sin, sa, th);
1190 }
1191
1192 /* XXX mapped address case */
1193 }
1194 else {
1195 (void)in_pcbnotifyall(&tcbtable, satosin(sa)->sin_addr, errno,
1196 notify);
1197 }
1198 return NULL;
1199 }
1200
1201 /*
1202 * When a source quence is received, we are being notifed of congestion.
1203 * Close the congestion window down to the Loss Window (one segment).
1204 * We will gradually open it again as we proceed.
1205 */
1206 void
1207 tcp_quench(inp, errno)
1208 struct inpcb *inp;
1209 int errno;
1210 {
1211 struct tcpcb *tp = intotcpcb(inp);
1212
1213 if (tp)
1214 tp->snd_cwnd = tp->t_segsz;
1215 }
1216
1217 #if defined(INET6) && !defined(TCP6)
1218 void
1219 tcp6_quench(in6p, errno)
1220 struct in6pcb *in6p;
1221 int errno;
1222 {
1223 struct tcpcb *tp = in6totcpcb(in6p);
1224
1225 if (tp)
1226 tp->snd_cwnd = tp->t_segsz;
1227 }
1228 #endif
1229
1230 /*
1231 * On receipt of path MTU corrections, flush old route and replace it
1232 * with the new one. Retransmit all unacknowledged packets, to ensure
1233 * that all packets will be received.
1234 */
1235 void
1236 tcp_mtudisc(inp, errno)
1237 struct inpcb *inp;
1238 int errno;
1239 {
1240 struct tcpcb *tp = intotcpcb(inp);
1241 struct rtentry *rt = in_pcbrtentry(inp);
1242
1243 if (tp != 0) {
1244 if (rt != 0) {
1245 /*
1246 * If this was not a host route, remove and realloc.
1247 */
1248 if ((rt->rt_flags & RTF_HOST) == 0) {
1249 in_rtchange(inp, errno);
1250 if ((rt = in_pcbrtentry(inp)) == 0)
1251 return;
1252 }
1253
1254 /*
1255 * Slow start out of the error condition. We
1256 * use the MTU because we know it's smaller
1257 * than the previously transmitted segment.
1258 *
1259 * Note: This is more conservative than the
1260 * suggestion in draft-floyd-incr-init-win-03.
1261 */
1262 if (rt->rt_rmx.rmx_mtu != 0)
1263 tp->snd_cwnd =
1264 TCP_INITIAL_WINDOW(tcp_init_win,
1265 rt->rt_rmx.rmx_mtu);
1266 }
1267
1268 /*
1269 * Resend unacknowledged packets.
1270 */
1271 tp->snd_nxt = tp->snd_una;
1272 tcp_output(tp);
1273 }
1274 }
1275
1276 #if defined(INET6) && !defined(TCP6)
1277 void
1278 tcp6_mtudisc(in6p, errno)
1279 struct in6pcb *in6p;
1280 int errno;
1281 {
1282 struct tcpcb *tp = in6totcpcb(in6p);
1283 struct rtentry *rt = in6_pcbrtentry(in6p);
1284
1285 if (tp != 0) {
1286 if (rt != 0) {
1287 /*
1288 * If this was not a host route, remove and realloc.
1289 */
1290 if ((rt->rt_flags & RTF_HOST) == 0) {
1291 in6_rtchange(in6p, errno);
1292 if ((rt = in6_pcbrtentry(in6p)) == 0)
1293 return;
1294 }
1295
1296 /*
1297 * Slow start out of the error condition. We
1298 * use the MTU because we know it's smaller
1299 * than the previously transmitted segment.
1300 *
1301 * Note: This is more conservative than the
1302 * suggestion in draft-floyd-incr-init-win-03.
1303 */
1304 if (rt->rt_rmx.rmx_mtu != 0)
1305 tp->snd_cwnd =
1306 TCP_INITIAL_WINDOW(tcp_init_win,
1307 rt->rt_rmx.rmx_mtu);
1308 }
1309
1310 /*
1311 * Resend unacknowledged packets.
1312 */
1313 tp->snd_nxt = tp->snd_una;
1314 tcp_output(tp);
1315 }
1316 }
1317 #endif
1318
1319 /*
1320 * Compute the MSS to advertise to the peer. Called only during
1321 * the 3-way handshake. If we are the server (peer initiated
1322 * connection), we are called with a pointer to the interface
1323 * on which the SYN packet arrived. If we are the client (we
1324 * initiated connection), we are called with a pointer to the
1325 * interface out which this connection should go.
1326 *
1327 * NOTE: Do not subtract IP option/extension header size nor IPsec
1328 * header size from MSS advertisement. MSS option must hold the maximum
1329 * segment size we can accept, so it must always be:
1330 * max(if mtu) - ip header - tcp header
1331 */
1332 u_long
1333 tcp_mss_to_advertise(ifp, af)
1334 const struct ifnet *ifp;
1335 int af;
1336 {
1337 extern u_long in_maxmtu;
1338 u_long mss = 0;
1339 u_long hdrsiz;
1340
1341 /*
1342 * In order to avoid defeating path MTU discovery on the peer,
1343 * we advertise the max MTU of all attached networks as our MSS,
1344 * per RFC 1191, section 3.1.
1345 *
1346 * We provide the option to advertise just the MTU of
1347 * the interface on which we hope this connection will
1348 * be receiving. If we are responding to a SYN, we
1349 * will have a pretty good idea about this, but when
1350 * initiating a connection there is a bit more doubt.
1351 *
1352 * We also need to ensure that loopback has a large enough
1353 * MSS, as the loopback MTU is never included in in_maxmtu.
1354 */
1355
1356 if (ifp != NULL)
1357 mss = ifp->if_mtu;
1358
1359 if (tcp_mss_ifmtu == 0)
1360 mss = max(in_maxmtu, mss);
1361
1362 switch (af) {
1363 case AF_INET:
1364 hdrsiz = sizeof(struct ip);
1365 break;
1366 #ifdef INET6
1367 case AF_INET6:
1368 hdrsiz = sizeof(struct ip6_hdr);
1369 break;
1370 #endif
1371 default:
1372 hdrsiz = 0;
1373 break;
1374 }
1375 hdrsiz += sizeof(struct tcphdr);
1376 if (mss > hdrsiz)
1377 mss -= hdrsiz;
1378
1379 mss = max(tcp_mssdflt, mss);
1380 return (mss);
1381 }
1382
1383 /*
1384 * Set connection variables based on the peer's advertised MSS.
1385 * We are passed the TCPCB for the actual connection. If we
1386 * are the server, we are called by the compressed state engine
1387 * when the 3-way handshake is complete. If we are the client,
1388 * we are called when we recieve the SYN,ACK from the server.
1389 *
1390 * NOTE: Our advertised MSS value must be initialized in the TCPCB
1391 * before this routine is called!
1392 */
1393 void
1394 tcp_mss_from_peer(tp, offer)
1395 struct tcpcb *tp;
1396 int offer;
1397 {
1398 struct socket *so;
1399 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1400 struct rtentry *rt;
1401 #endif
1402 u_long bufsize;
1403 int mss;
1404
1405 so = NULL;
1406 rt = NULL;
1407 if (tp->t_inpcb) {
1408 so = tp->t_inpcb->inp_socket;
1409 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1410 rt = in_pcbrtentry(tp->t_inpcb);
1411 #endif
1412 }
1413 #ifdef INET6
1414 else if (tp->t_in6pcb) {
1415 so = tp->t_in6pcb->in6p_socket;
1416 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1417 #ifdef TCP6
1418 rt = NULL;
1419 #else
1420 rt = in6_pcbrtentry(tp->t_in6pcb);
1421 #endif
1422 #endif
1423 }
1424 #endif
1425
1426 /*
1427 * As per RFC1122, use the default MSS value, unless they
1428 * sent us an offer. Do not accept offers less than 32 bytes.
1429 */
1430 mss = tcp_mssdflt;
1431 if (offer)
1432 mss = offer;
1433 mss = max(mss, 32); /* sanity */
1434 tp->t_peermss = mss;
1435 mss -= tcp_optlen(tp);
1436 if (tp->t_inpcb)
1437 mss -= ip_optlen(tp->t_inpcb);
1438 #ifdef INET6
1439 else if (tp->t_in6pcb)
1440 mss -= ip6_optlen(tp->t_in6pcb);
1441 #endif
1442
1443 /*
1444 * If there's a pipesize, change the socket buffer to that size.
1445 * Make the socket buffer an integral number of MSS units. If
1446 * the MSS is larger than the socket buffer, artificially decrease
1447 * the MSS.
1448 */
1449 #ifdef RTV_SPIPE
1450 if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0)
1451 bufsize = rt->rt_rmx.rmx_sendpipe;
1452 else
1453 #endif
1454 bufsize = so->so_snd.sb_hiwat;
1455 if (bufsize < mss)
1456 mss = bufsize;
1457 else {
1458 bufsize = roundup(bufsize, mss);
1459 if (bufsize > sb_max)
1460 bufsize = sb_max;
1461 (void) sbreserve(&so->so_snd, bufsize);
1462 }
1463 tp->t_segsz = mss;
1464
1465 #ifdef RTV_SSTHRESH
1466 if (rt != NULL && rt->rt_rmx.rmx_ssthresh) {
1467 /*
1468 * There's some sort of gateway or interface buffer
1469 * limit on the path. Use this to set the slow
1470 * start threshold, but set the threshold to no less
1471 * than 2 * MSS.
1472 */
1473 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
1474 }
1475 #endif
1476 }
1477
1478 /*
1479 * Processing necessary when a TCP connection is established.
1480 */
1481 void
1482 tcp_established(tp)
1483 struct tcpcb *tp;
1484 {
1485 struct socket *so;
1486 #ifdef RTV_RPIPE
1487 struct rtentry *rt;
1488 #endif
1489 u_long bufsize;
1490
1491 so = NULL;
1492 rt = NULL;
1493 if (tp->t_inpcb) {
1494 so = tp->t_inpcb->inp_socket;
1495 #if defined(RTV_RPIPE)
1496 rt = in_pcbrtentry(tp->t_inpcb);
1497 #endif
1498 }
1499 #ifdef INET6
1500 else if (tp->t_in6pcb) {
1501 so = tp->t_in6pcb->in6p_socket;
1502 #if defined(RTV_RPIPE)
1503 #ifdef TCP6
1504 rt = NULL;
1505 #else
1506 rt = in6_pcbrtentry(tp->t_in6pcb);
1507 #endif
1508 #endif
1509 }
1510 #endif
1511
1512 tp->t_state = TCPS_ESTABLISHED;
1513 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
1514
1515 #ifdef RTV_RPIPE
1516 if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0)
1517 bufsize = rt->rt_rmx.rmx_recvpipe;
1518 else
1519 #endif
1520 bufsize = so->so_rcv.sb_hiwat;
1521 if (bufsize > tp->t_ourmss) {
1522 bufsize = roundup(bufsize, tp->t_ourmss);
1523 if (bufsize > sb_max)
1524 bufsize = sb_max;
1525 (void) sbreserve(&so->so_rcv, bufsize);
1526 }
1527 }
1528
1529 /*
1530 * Check if there's an initial rtt or rttvar. Convert from the
1531 * route-table units to scaled multiples of the slow timeout timer.
1532 * Called only during the 3-way handshake.
1533 */
1534 void
1535 tcp_rmx_rtt(tp)
1536 struct tcpcb *tp;
1537 {
1538 #ifdef RTV_RTT
1539 struct rtentry *rt = NULL;
1540 int rtt;
1541
1542 if (tp->t_inpcb)
1543 rt = in_pcbrtentry(tp->t_inpcb);
1544 #ifdef INET6
1545 else if (tp->t_in6pcb) {
1546 #ifdef TCP6
1547 rt = NULL;
1548 #else
1549 rt = in6_pcbrtentry(tp->t_in6pcb);
1550 #endif
1551 }
1552 #endif
1553 if (rt == NULL)
1554 return;
1555
1556 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
1557 /*
1558 * XXX The lock bit for MTU indicates that the value
1559 * is also a minimum value; this is subject to time.
1560 */
1561 if (rt->rt_rmx.rmx_locks & RTV_RTT)
1562 TCPT_RANGESET(tp->t_rttmin,
1563 rtt / (RTM_RTTUNIT / PR_SLOWHZ),
1564 TCPTV_MIN, TCPTV_REXMTMAX);
1565 tp->t_srtt = rtt /
1566 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
1567 if (rt->rt_rmx.rmx_rttvar) {
1568 tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
1569 ((RTM_RTTUNIT / PR_SLOWHZ) >>
1570 (TCP_RTTVAR_SHIFT + 2));
1571 } else {
1572 /* Default variation is +- 1 rtt */
1573 tp->t_rttvar =
1574 tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT);
1575 }
1576 TCPT_RANGESET(tp->t_rxtcur,
1577 ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2),
1578 tp->t_rttmin, TCPTV_REXMTMAX);
1579 }
1580 #endif
1581 }
1582
1583 tcp_seq tcp_iss_seq = 0; /* tcp initial seq # */
1584
1585 /*
1586 * Get a new sequence value given a tcp control block
1587 */
1588 tcp_seq
1589 tcp_new_iss(tp, len, addin)
1590 void *tp;
1591 u_long len;
1592 tcp_seq addin;
1593 {
1594 tcp_seq tcp_iss;
1595
1596 /*
1597 * Randomize.
1598 */
1599 #if NRND > 0
1600 rnd_extract_data(&tcp_iss, sizeof(tcp_iss), RND_EXTRACT_ANY);
1601 #else
1602 tcp_iss = random();
1603 #endif
1604
1605 /*
1606 * If we were asked to add some amount to a known value,
1607 * we will take a random value obtained above, mask off the upper
1608 * bits, and add in the known value. We also add in a constant to
1609 * ensure that we are at least a certain distance from the original
1610 * value.
1611 *
1612 * This is used when an old connection is in timed wait
1613 * and we have a new one coming in, for instance.
1614 */
1615 if (addin != 0) {
1616 #ifdef TCPISS_DEBUG
1617 printf("Random %08x, ", tcp_iss);
1618 #endif
1619 tcp_iss &= TCP_ISS_RANDOM_MASK;
1620 tcp_iss += addin + TCP_ISSINCR;
1621 #ifdef TCPISS_DEBUG
1622 printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss);
1623 #endif
1624 } else {
1625 tcp_iss &= TCP_ISS_RANDOM_MASK;
1626 tcp_iss += tcp_iss_seq;
1627 tcp_iss_seq += TCP_ISSINCR;
1628 #ifdef TCPISS_DEBUG
1629 printf("ISS %08x\n", tcp_iss);
1630 #endif
1631 }
1632
1633 if (tcp_compat_42) {
1634 /*
1635 * Limit it to the positive range for really old TCP
1636 * implementations.
1637 */
1638 if (tcp_iss >= 0x80000000)
1639 tcp_iss &= 0x7fffffff; /* XXX */
1640 }
1641
1642 return tcp_iss;
1643 }
1644
1645 #ifdef IPSEC
1646 /* compute ESP/AH header size for TCP, including outer IP header. */
1647 size_t
1648 ipsec4_hdrsiz_tcp(tp)
1649 struct tcpcb *tp;
1650 {
1651 struct inpcb *inp;
1652 size_t hdrsiz;
1653
1654 /* XXX mapped addr case (tp->t_in6pcb) */
1655 if (!tp || !tp->t_template || !(inp = tp->t_inpcb))
1656 return 0;
1657 switch (tp->t_family) {
1658 case AF_INET:
1659 hdrsiz = ipsec4_hdrsiz(tp->t_template, inp);
1660 break;
1661 default:
1662 hdrsiz = 0;
1663 break;
1664 }
1665
1666 return hdrsiz;
1667 }
1668
1669 #if defined(INET6) && !defined(TCP6)
1670 size_t
1671 ipsec6_hdrsiz_tcp(tp)
1672 struct tcpcb *tp;
1673 {
1674 struct in6pcb *in6p;
1675 size_t hdrsiz;
1676
1677 if (!tp || !tp->t_template || !(in6p = tp->t_in6pcb))
1678 return 0;
1679 switch (tp->t_family) {
1680 case AF_INET6:
1681 hdrsiz = ipsec6_hdrsiz(tp->t_template, in6p);
1682 break;
1683 case AF_INET:
1684 /* mapped address case - tricky */
1685 default:
1686 hdrsiz = 0;
1687 break;
1688 }
1689
1690 return hdrsiz;
1691 }
1692 #endif
1693 #endif /*IPSEC*/
1694
1695 /*
1696 * Determine the length of the TCP options for this connection.
1697 *
1698 * XXX: What do we do for SACK, when we add that? Just reserve
1699 * all of the space? Otherwise we can't exactly be incrementing
1700 * cwnd by an amount that varies depending on the amount we last
1701 * had to SACK!
1702 */
1703
1704 u_int
1705 tcp_optlen(tp)
1706 struct tcpcb *tp;
1707 {
1708 if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) ==
1709 (TF_REQ_TSTMP | TF_RCVD_TSTMP))
1710 return TCPOLEN_TSTAMP_APPA;
1711 else
1712 return 0;
1713 }
1714