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