]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/tcp_subr.c
xnu-517.3.15.tar.gz
[apple/xnu.git] / bsd / netinet / tcp_subr.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
27 * The Regents of the University of California. All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95
58 * $FreeBSD: src/sys/netinet/tcp_subr.c,v 1.73.2.22 2001/08/22 00:59:12 silby Exp $
59 */
60
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/callout.h>
65 #include <sys/kernel.h>
66 #include <sys/sysctl.h>
67 #include <sys/malloc.h>
68 #include <sys/mbuf.h>
69 #if INET6
70 #include <sys/domain.h>
71 #endif
72 #include <sys/proc.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/protosw.h>
76 #include <sys/random.h>
77 #include <sys/syslog.h>
78
79
80
81 #include <net/route.h>
82 #include <net/if.h>
83
84 #define _IP_VHL
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #if INET6
89 #include <netinet/ip6.h>
90 #endif
91 #include <netinet/in_pcb.h>
92 #if INET6
93 #include <netinet6/in6_pcb.h>
94 #endif
95 #include <netinet/in_var.h>
96 #include <netinet/ip_var.h>
97 #if INET6
98 #include <netinet6/ip6_var.h>
99 #endif
100 #include <netinet/tcp.h>
101 #include <netinet/tcp_fsm.h>
102 #include <netinet/tcp_seq.h>
103 #include <netinet/tcp_timer.h>
104 #include <netinet/tcp_var.h>
105 #if INET6
106 #include <netinet6/tcp6_var.h>
107 #endif
108 #include <netinet/tcpip.h>
109 #if TCPDEBUG
110 #include <netinet/tcp_debug.h>
111 #endif
112 #include <netinet6/ip6protosw.h>
113
114 #if IPSEC
115 #include <netinet6/ipsec.h>
116 #if INET6
117 #include <netinet6/ipsec6.h>
118 #endif
119 #endif /*IPSEC*/
120
121 #include <sys/md5.h>
122 #include <sys/kdebug.h>
123
124 #define DBG_FNC_TCP_CLOSE NETDBG_CODE(DBG_NETTCP, ((5 << 8) | 2))
125
126
127 /* temporary: for testing */
128 #if IPSEC
129 extern int ipsec_bypass;
130 #endif
131
132 int tcp_mssdflt = TCP_MSS;
133 SYSCTL_INT(_net_inet_tcp, TCPCTL_MSSDFLT, mssdflt, CTLFLAG_RW,
134 &tcp_mssdflt , 0, "Default TCP Maximum Segment Size");
135
136 #if INET6
137 int tcp_v6mssdflt = TCP6_MSS;
138 SYSCTL_INT(_net_inet_tcp, TCPCTL_V6MSSDFLT, v6mssdflt,
139 CTLFLAG_RW, &tcp_v6mssdflt , 0,
140 "Default TCP Maximum Segment Size for IPv6");
141 #endif
142
143 static int tcp_do_rfc1323 = 1;
144 SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1323, rfc1323, CTLFLAG_RW,
145 &tcp_do_rfc1323 , 0, "Enable rfc1323 (high performance TCP) extensions");
146
147 static int tcp_do_rfc1644 = 0;
148 SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1644, rfc1644, CTLFLAG_RW,
149 &tcp_do_rfc1644 , 0, "Enable rfc1644 (TTCP) extensions");
150
151 static int tcp_tcbhashsize = 0;
152 SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcbhashsize, CTLFLAG_RD,
153 &tcp_tcbhashsize, 0, "Size of TCP control-block hashtable");
154
155 static int do_tcpdrain = 1;
156 SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_tcpdrain, CTLFLAG_RW, &do_tcpdrain, 0,
157 "Enable tcp_drain routine for extra help when low on mbufs");
158
159 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pcbcount, CTLFLAG_RD,
160 &tcbinfo.ipi_count, 0, "Number of active PCBs");
161
162 static int icmp_may_rst = 1;
163 SYSCTL_INT(_net_inet_tcp, OID_AUTO, icmp_may_rst, CTLFLAG_RW, &icmp_may_rst, 0,
164 "Certain ICMP unreachable messages may abort connections in SYN_SENT");
165
166 static int tcp_strict_rfc1948 = 0;
167 SYSCTL_INT(_net_inet_tcp, OID_AUTO, strict_rfc1948, CTLFLAG_RW,
168 &tcp_strict_rfc1948, 0, "Determines if RFC1948 is followed exactly");
169
170 static int tcp_isn_reseed_interval = 0;
171 SYSCTL_INT(_net_inet_tcp, OID_AUTO, isn_reseed_interval, CTLFLAG_RW,
172 &tcp_isn_reseed_interval, 0, "Seconds between reseeding of ISN secret");
173
174 static void tcp_cleartaocache __P((void));
175 static void tcp_notify __P((struct inpcb *, int));
176
177 /*
178 * Target size of TCP PCB hash tables. Must be a power of two.
179 *
180 * Note that this can be overridden by the kernel environment
181 * variable net.inet.tcp.tcbhashsize
182 */
183 #ifndef TCBHASHSIZE
184 #define TCBHASHSIZE 4096
185 #endif
186
187 /*
188 * This is the actual shape of what we allocate using the zone
189 * allocator. Doing it this way allows us to protect both structures
190 * using the same generation count, and also eliminates the overhead
191 * of allocating tcpcbs separately. By hiding the structure here,
192 * we avoid changing most of the rest of the code (although it needs
193 * to be changed, eventually, for greater efficiency).
194 */
195 #define ALIGNMENT 32
196 #define ALIGNM1 (ALIGNMENT - 1)
197 struct inp_tp {
198 union {
199 struct inpcb inp;
200 char align[(sizeof(struct inpcb) + ALIGNM1) & ~ALIGNM1];
201 } inp_tp_u;
202 struct tcpcb tcb;
203 #ifndef __APPLE__
204 struct callout inp_tp_rexmt, inp_tp_persist, inp_tp_keep, inp_tp_2msl;
205 struct callout inp_tp_delack;
206 #endif
207 };
208 #undef ALIGNMENT
209 #undef ALIGNM1
210
211 static struct tcpcb dummy_tcb;
212
213
214 extern struct inpcbhead time_wait_slots[];
215 extern int cur_tw_slot;
216 extern u_long *delack_bitmask;
217 extern u_long route_generation;
218
219
220 int get_inpcb_str_size()
221 {
222 return sizeof(struct inpcb);
223 }
224
225
226 int get_tcp_str_size()
227 {
228 return sizeof(struct tcpcb);
229 }
230
231 int tcp_freeq __P((struct tcpcb *tp));
232
233
234 /*
235 * Tcp initialization
236 */
237 void
238 tcp_init()
239 {
240 int hashsize = TCBHASHSIZE;
241 vm_size_t str_size;
242 int i;
243
244 tcp_ccgen = 1;
245 tcp_cleartaocache();
246
247 tcp_delacktime = TCPTV_DELACK;
248 tcp_keepinit = TCPTV_KEEP_INIT;
249 tcp_keepidle = TCPTV_KEEP_IDLE;
250 tcp_keepintvl = TCPTV_KEEPINTVL;
251 tcp_maxpersistidle = TCPTV_KEEP_IDLE;
252 tcp_msl = TCPTV_MSL;
253 read_random(&tcp_now, sizeof(tcp_now));
254 tcp_now = tcp_now & 0x7fffffffffffffff; /* Starts tcp internal 500ms clock at a random value */
255
256
257 LIST_INIT(&tcb);
258 tcbinfo.listhead = &tcb;
259 #ifndef __APPLE__
260 TUNABLE_INT_FETCH("net.inet.tcp.tcbhashsize", &hashsize);
261 #endif
262 if (!powerof2(hashsize)) {
263 printf("WARNING: TCB hash size not a power of 2\n");
264 hashsize = 512; /* safe default */
265 }
266 tcp_tcbhashsize = hashsize;
267 tcbinfo.hashsize = hashsize;
268 tcbinfo.hashbase = hashinit(hashsize, M_PCB, &tcbinfo.hashmask);
269 tcbinfo.porthashbase = hashinit(hashsize, M_PCB,
270 &tcbinfo.porthashmask);
271 #ifdef __APPLE__
272 str_size = (vm_size_t) sizeof(struct inp_tp);
273 tcbinfo.ipi_zone = (void *) zinit(str_size, 120000*str_size, 8192, "tcpcb");
274 #else
275 tcbinfo.ipi_zone = zinit("tcpcb", sizeof(struct inp_tp), maxsockets,
276 ZONE_INTERRUPT, 0);
277 #endif
278 #if INET6
279 #define TCP_MINPROTOHDR (sizeof(struct ip6_hdr) + sizeof(struct tcphdr))
280 #else /* INET6 */
281 #define TCP_MINPROTOHDR (sizeof(struct tcpiphdr))
282 #endif /* INET6 */
283 if (max_protohdr < TCP_MINPROTOHDR)
284 max_protohdr = TCP_MINPROTOHDR;
285 if (max_linkhdr + TCP_MINPROTOHDR > MHLEN)
286 panic("tcp_init");
287 #undef TCP_MINPROTOHDR
288 tcbinfo.last_pcb = 0;
289 dummy_tcb.t_state = TCP_NSTATES;
290 dummy_tcb.t_flags = 0;
291 tcbinfo.dummy_cb = (caddr_t) &dummy_tcb;
292 in_pcb_nat_init(&tcbinfo, AF_INET, IPPROTO_TCP, SOCK_STREAM);
293
294 delack_bitmask = _MALLOC((4 * hashsize)/32, M_PCB, M_WAITOK);
295 if (delack_bitmask == 0)
296 panic("Delack Memory");
297
298 for (i=0; i < (tcbinfo.hashsize / 32); i++)
299 delack_bitmask[i] = 0;
300
301 for (i=0; i < N_TIME_WAIT_SLOTS; i++) {
302 LIST_INIT(&time_wait_slots[i]);
303 }
304 }
305
306 /*
307 * Fill in the IP and TCP headers for an outgoing packet, given the tcpcb.
308 * tcp_template used to store this data in mbufs, but we now recopy it out
309 * of the tcpcb each time to conserve mbufs.
310 */
311 void
312 tcp_fillheaders(tp, ip_ptr, tcp_ptr)
313 struct tcpcb *tp;
314 void *ip_ptr;
315 void *tcp_ptr;
316 {
317 struct inpcb *inp = tp->t_inpcb;
318 struct tcphdr *tcp_hdr = (struct tcphdr *)tcp_ptr;
319
320 #if INET6
321 if ((inp->inp_vflag & INP_IPV6) != 0) {
322 struct ip6_hdr *ip6;
323
324 ip6 = (struct ip6_hdr *)ip_ptr;
325 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
326 (inp->in6p_flowinfo & IPV6_FLOWINFO_MASK);
327 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
328 (IPV6_VERSION & IPV6_VERSION_MASK);
329 ip6->ip6_nxt = IPPROTO_TCP;
330 ip6->ip6_plen = sizeof(struct tcphdr);
331 ip6->ip6_src = inp->in6p_laddr;
332 ip6->ip6_dst = inp->in6p_faddr;
333 tcp_hdr->th_sum = 0;
334 } else
335 #endif
336 {
337 struct ip *ip = (struct ip *) ip_ptr;
338
339 ip->ip_vhl = IP_VHL_BORING;
340 ip->ip_tos = 0;
341 ip->ip_len = 0;
342 ip->ip_id = 0;
343 ip->ip_off = 0;
344 ip->ip_ttl = 0;
345 ip->ip_sum = 0;
346 ip->ip_p = IPPROTO_TCP;
347 ip->ip_src = inp->inp_laddr;
348 ip->ip_dst = inp->inp_faddr;
349 tcp_hdr->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
350 htons(sizeof(struct tcphdr) + IPPROTO_TCP));
351 }
352
353 tcp_hdr->th_sport = inp->inp_lport;
354 tcp_hdr->th_dport = inp->inp_fport;
355 tcp_hdr->th_seq = 0;
356 tcp_hdr->th_ack = 0;
357 tcp_hdr->th_x2 = 0;
358 tcp_hdr->th_off = 5;
359 tcp_hdr->th_flags = 0;
360 tcp_hdr->th_win = 0;
361 tcp_hdr->th_urp = 0;
362 }
363
364 /*
365 * Create template to be used to send tcp packets on a connection.
366 * Allocates an mbuf and fills in a skeletal tcp/ip header. The only
367 * use for this function is in keepalives, which use tcp_respond.
368 */
369 struct tcptemp *
370 tcp_maketemplate(tp)
371 struct tcpcb *tp;
372 {
373 struct mbuf *m;
374 struct tcptemp *n;
375
376 m = m_get(M_DONTWAIT, MT_HEADER);
377 if (m == NULL)
378 return (0);
379 m->m_len = sizeof(struct tcptemp);
380 n = mtod(m, struct tcptemp *);
381
382 tcp_fillheaders(tp, (void *)&n->tt_ipgen, (void *)&n->tt_t);
383 return (n);
384 }
385
386 /*
387 * Send a single message to the TCP at address specified by
388 * the given TCP/IP header. If m == 0, then we make a copy
389 * of the tcpiphdr at ti and send directly to the addressed host.
390 * This is used to force keep alive messages out using the TCP
391 * template for a connection. If flags are given then we send
392 * a message back to the TCP which originated the * segment ti,
393 * and discard the mbuf containing it and any other attached mbufs.
394 *
395 * In any case the ack and sequence number of the transmitted
396 * segment are as specified by the parameters.
397 *
398 * NOTE: If m != NULL, then ti must point to *inside* the mbuf.
399 */
400 void
401 tcp_respond(tp, ipgen, th, m, ack, seq, flags)
402 struct tcpcb *tp;
403 void *ipgen;
404 register struct tcphdr *th;
405 register struct mbuf *m;
406 tcp_seq ack, seq;
407 int flags;
408 {
409 register int tlen;
410 int win = 0;
411 struct route *ro = 0;
412 struct route sro;
413 struct ip *ip;
414 struct tcphdr *nth;
415 #if INET6
416 struct route_in6 *ro6 = 0;
417 struct route_in6 sro6;
418 struct ip6_hdr *ip6;
419 int isipv6;
420 #endif /* INET6 */
421 int ipflags = 0;
422
423 #if INET6
424 isipv6 = IP_VHL_V(((struct ip *)ipgen)->ip_vhl) == 6;
425 ip6 = ipgen;
426 #endif /* INET6 */
427 ip = ipgen;
428
429 if (tp) {
430 if (!(flags & TH_RST)) {
431 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
432 if (win > (long)TCP_MAXWIN << tp->rcv_scale)
433 win = (long)TCP_MAXWIN << tp->rcv_scale;
434 }
435 #if INET6
436 if (isipv6)
437 ro6 = &tp->t_inpcb->in6p_route;
438 else
439 #endif /* INET6 */
440 ro = &tp->t_inpcb->inp_route;
441 } else {
442 #if INET6
443 if (isipv6) {
444 ro6 = &sro6;
445 bzero(ro6, sizeof *ro6);
446 } else
447 #endif /* INET6 */
448 {
449 ro = &sro;
450 bzero(ro, sizeof *ro);
451 }
452 }
453 if (m == 0) {
454 m = m_gethdr(M_DONTWAIT, MT_HEADER);
455 if (m == NULL)
456 return;
457 tlen = 0;
458 m->m_data += max_linkhdr;
459 #if INET6
460 if (isipv6) {
461 bcopy((caddr_t)ip6, mtod(m, caddr_t),
462 sizeof(struct ip6_hdr));
463 ip6 = mtod(m, struct ip6_hdr *);
464 nth = (struct tcphdr *)(ip6 + 1);
465 } else
466 #endif /* INET6 */
467 {
468 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
469 ip = mtod(m, struct ip *);
470 nth = (struct tcphdr *)(ip + 1);
471 }
472 bcopy((caddr_t)th, (caddr_t)nth, sizeof(struct tcphdr));
473 flags = TH_ACK;
474 } else {
475 m_freem(m->m_next);
476 m->m_next = 0;
477 m->m_data = (caddr_t)ipgen;
478 /* m_len is set later */
479 tlen = 0;
480 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
481 #if INET6
482 if (isipv6) {
483 xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
484 nth = (struct tcphdr *)(ip6 + 1);
485 } else
486 #endif /* INET6 */
487 {
488 xchg(ip->ip_dst.s_addr, ip->ip_src.s_addr, n_long);
489 nth = (struct tcphdr *)(ip + 1);
490 }
491 if (th != nth) {
492 /*
493 * this is usually a case when an extension header
494 * exists between the IPv6 header and the
495 * TCP header.
496 */
497 nth->th_sport = th->th_sport;
498 nth->th_dport = th->th_dport;
499 }
500 xchg(nth->th_dport, nth->th_sport, n_short);
501 #undef xchg
502 }
503 #if INET6
504 if (isipv6) {
505 ip6->ip6_plen = htons((u_short)(sizeof (struct tcphdr) +
506 tlen));
507 tlen += sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
508 } else
509 #endif
510 {
511 tlen += sizeof (struct tcpiphdr);
512 ip->ip_len = tlen;
513 ip->ip_ttl = ip_defttl;
514 }
515 m->m_len = tlen;
516 m->m_pkthdr.len = tlen;
517 m->m_pkthdr.rcvif = (struct ifnet *) 0;
518 nth->th_seq = htonl(seq);
519 nth->th_ack = htonl(ack);
520 nth->th_x2 = 0;
521 nth->th_off = sizeof (struct tcphdr) >> 2;
522 nth->th_flags = flags;
523 if (tp)
524 nth->th_win = htons((u_short) (win >> tp->rcv_scale));
525 else
526 nth->th_win = htons((u_short)win);
527 nth->th_urp = 0;
528 #if INET6
529 if (isipv6) {
530 nth->th_sum = 0;
531 nth->th_sum = in6_cksum(m, IPPROTO_TCP,
532 sizeof(struct ip6_hdr),
533 tlen - sizeof(struct ip6_hdr));
534 ip6->ip6_hlim = in6_selecthlim(tp ? tp->t_inpcb : NULL,
535 ro6 && ro6->ro_rt ?
536 ro6->ro_rt->rt_ifp :
537 NULL);
538 } else
539 #endif /* INET6 */
540 {
541 nth->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
542 htons((u_short)(tlen - sizeof(struct ip) + ip->ip_p)));
543 m->m_pkthdr.csum_flags = CSUM_TCP;
544 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
545 }
546 #if TCPDEBUG
547 if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
548 tcp_trace(TA_OUTPUT, 0, tp, mtod(m, void *), th, 0);
549 #endif
550 #if IPSEC
551 if (ipsec_bypass == 0 && ipsec_setsocket(m, tp ? tp->t_inpcb->inp_socket : NULL) != 0) {
552 m_freem(m);
553 return;
554 }
555 #endif
556 #if INET6
557 if (isipv6) {
558 (void)ip6_output(m, NULL, ro6, ipflags, NULL, NULL);
559 if (ro6 == &sro6 && ro6->ro_rt) {
560 rtfree(ro6->ro_rt);
561 ro6->ro_rt = NULL;
562 }
563 } else
564 #endif /* INET6 */
565 {
566 (void) ip_output(m, NULL, ro, ipflags, NULL);
567 if (ro == &sro && ro->ro_rt) {
568 rtfree(ro->ro_rt);
569 ro->ro_rt = NULL;
570 }
571 }
572 }
573
574 /*
575 * Create a new TCP control block, making an
576 * empty reassembly queue and hooking it to the argument
577 * protocol control block. The `inp' parameter must have
578 * come from the zone allocator set up in tcp_init().
579 */
580 struct tcpcb *
581 tcp_newtcpcb(inp)
582 struct inpcb *inp;
583 {
584 struct inp_tp *it;
585 register struct tcpcb *tp;
586 register struct socket *so = inp->inp_socket;
587 #if INET6
588 int isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
589 #endif /* INET6 */
590
591 if (so->cached_in_sock_layer == 0) {
592 it = (struct inp_tp *)inp;
593 tp = &it->tcb;
594 }
595 else
596 tp = (struct tcpcb *) inp->inp_saved_ppcb;
597
598 bzero((char *) tp, sizeof(struct tcpcb));
599 LIST_INIT(&tp->t_segq);
600 tp->t_maxseg = tp->t_maxopd =
601 #if INET6
602 isipv6 ? tcp_v6mssdflt :
603 #endif /* INET6 */
604 tcp_mssdflt;
605
606 #ifndef __APPLE__
607 /* Set up our timeouts. */
608 callout_init(tp->tt_rexmt = &it->inp_tp_rexmt);
609 callout_init(tp->tt_persist = &it->inp_tp_persist);
610 callout_init(tp->tt_keep = &it->inp_tp_keep);
611 callout_init(tp->tt_2msl = &it->inp_tp_2msl);
612 callout_init(tp->tt_delack = &it->inp_tp_delack);
613 #endif
614
615 if (tcp_do_rfc1323)
616 tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP);
617 if (tcp_do_rfc1644)
618 tp->t_flags |= TF_REQ_CC;
619 tp->t_inpcb = inp; /* XXX */
620 /*
621 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
622 * rtt estimate. Set rttvar so that srtt + 4 * rttvar gives
623 * reasonable initial retransmit time.
624 */
625 tp->t_srtt = TCPTV_SRTTBASE;
626 tp->t_rttvar = ((TCPTV_RTOBASE - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4;
627 tp->t_rttmin = TCPTV_MIN;
628 tp->t_rxtcur = TCPTV_RTOBASE;
629 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
630 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
631 /*
632 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
633 * because the socket may be bound to an IPv6 wildcard address,
634 * which may match an IPv4-mapped IPv6 address.
635 */
636 inp->inp_ip_ttl = ip_defttl;
637 inp->inp_ppcb = (caddr_t)tp;
638 return (tp); /* XXX */
639 }
640
641 /*
642 * Drop a TCP connection, reporting
643 * the specified error. If connection is synchronized,
644 * then send a RST to peer.
645 */
646 struct tcpcb *
647 tcp_drop(tp, errno)
648 register struct tcpcb *tp;
649 int errno;
650 {
651 struct socket *so = tp->t_inpcb->inp_socket;
652
653 #ifdef __APPLE__
654 switch (tp->t_state)
655 {
656 case TCPS_ESTABLISHED:
657 case TCPS_FIN_WAIT_1:
658 case TCPS_CLOSING:
659 case TCPS_CLOSE_WAIT:
660 case TCPS_LAST_ACK:
661 break;
662 }
663 #endif
664
665 if (TCPS_HAVERCVDSYN(tp->t_state)) {
666 tp->t_state = TCPS_CLOSED;
667 (void) tcp_output(tp);
668 tcpstat.tcps_drops++;
669 } else
670 tcpstat.tcps_conndrops++;
671 if (errno == ETIMEDOUT && tp->t_softerror)
672 errno = tp->t_softerror;
673 so->so_error = errno;
674 return (tcp_close(tp));
675 }
676
677 /*
678 * Close a TCP control block:
679 * discard all space held by the tcp
680 * discard internet protocol block
681 * wake up any sleepers
682 */
683 struct tcpcb *
684 tcp_close(tp)
685 register struct tcpcb *tp;
686 {
687 register struct tseg_qent *q;
688 struct inpcb *inp = tp->t_inpcb;
689 struct socket *so = inp->inp_socket;
690 #if INET6
691 int isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
692 #endif /* INET6 */
693 register struct rtentry *rt;
694 int dosavessthresh;
695
696 if ( inp->inp_ppcb == NULL) /* tcp_close was called previously, bail */
697 return;
698
699 #ifndef __APPLE__
700 /*
701 * Make sure that all of our timers are stopped before we
702 * delete the PCB.
703 */
704 callout_stop(tp->tt_rexmt);
705 callout_stop(tp->tt_persist);
706 callout_stop(tp->tt_keep);
707 callout_stop(tp->tt_2msl);
708 callout_stop(tp->tt_delack);
709 #else
710 /* Clear the timers before we delete the PCB. */
711 {
712 int i;
713 for (i = 0; i < TCPT_NTIMERS; i++) {
714 tp->t_timer[i] = 0;
715 }
716 }
717 #endif
718
719
720 KERNEL_DEBUG(DBG_FNC_TCP_CLOSE | DBG_FUNC_START, tp,0,0,0,0);
721 switch (tp->t_state)
722 {
723 case TCPS_ESTABLISHED:
724 case TCPS_FIN_WAIT_1:
725 case TCPS_CLOSING:
726 case TCPS_CLOSE_WAIT:
727 case TCPS_LAST_ACK:
728 break;
729 }
730
731
732 /*
733 * If we got enough samples through the srtt filter,
734 * save the rtt and rttvar in the routing entry.
735 * 'Enough' is arbitrarily defined as the 16 samples.
736 * 16 samples is enough for the srtt filter to converge
737 * to within 5% of the correct value; fewer samples and
738 * we could save a very bogus rtt.
739 *
740 * Don't update the default route's characteristics and don't
741 * update anything that the user "locked".
742 */
743 if (tp->t_rttupdated >= 16) {
744 register u_long i = 0;
745 #if INET6
746 if (isipv6) {
747 struct sockaddr_in6 *sin6;
748
749 if ((rt = inp->in6p_route.ro_rt) == NULL)
750 goto no_valid_rt;
751 sin6 = (struct sockaddr_in6 *)rt_key(rt);
752 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
753 goto no_valid_rt;
754 }
755 else
756 #endif /* INET6 */
757 rt = inp->inp_route.ro_rt;
758 if (rt == NULL ||
759 ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr
760 == INADDR_ANY || rt->generation_id != route_generation) {
761 if (tp->t_state >= TCPS_CLOSE_WAIT)
762 tp->t_state = TCPS_CLOSING;
763
764 goto no_valid_rt;
765 }
766
767 if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
768 i = tp->t_srtt *
769 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
770 if (rt->rt_rmx.rmx_rtt && i)
771 /*
772 * filter this update to half the old & half
773 * the new values, converting scale.
774 * See route.h and tcp_var.h for a
775 * description of the scaling constants.
776 */
777 rt->rt_rmx.rmx_rtt =
778 (rt->rt_rmx.rmx_rtt + i) / 2;
779 else
780 rt->rt_rmx.rmx_rtt = i;
781 tcpstat.tcps_cachedrtt++;
782 }
783 if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
784 i = tp->t_rttvar *
785 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
786 if (rt->rt_rmx.rmx_rttvar && i)
787 rt->rt_rmx.rmx_rttvar =
788 (rt->rt_rmx.rmx_rttvar + i) / 2;
789 else
790 rt->rt_rmx.rmx_rttvar = i;
791 tcpstat.tcps_cachedrttvar++;
792 }
793 /*
794 * The old comment here said:
795 * update the pipelimit (ssthresh) if it has been updated
796 * already or if a pipesize was specified & the threshhold
797 * got below half the pipesize. I.e., wait for bad news
798 * before we start updating, then update on both good
799 * and bad news.
800 *
801 * But we want to save the ssthresh even if no pipesize is
802 * specified explicitly in the route, because such
803 * connections still have an implicit pipesize specified
804 * by the global tcp_sendspace. In the absence of a reliable
805 * way to calculate the pipesize, it will have to do.
806 */
807 i = tp->snd_ssthresh;
808 if (rt->rt_rmx.rmx_sendpipe != 0)
809 dosavessthresh = (i < rt->rt_rmx.rmx_sendpipe / 2);
810 else
811 dosavessthresh = (i < so->so_snd.sb_hiwat / 2);
812 if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
813 i != 0 && rt->rt_rmx.rmx_ssthresh != 0)
814 || dosavessthresh) {
815 /*
816 * convert the limit from user data bytes to
817 * packets then to packet data bytes.
818 */
819 i = (i + tp->t_maxseg / 2) / tp->t_maxseg;
820 if (i < 2)
821 i = 2;
822 i *= (u_long)(tp->t_maxseg +
823 #if INET6
824 (isipv6 ? sizeof (struct ip6_hdr) +
825 sizeof (struct tcphdr) :
826 #endif
827 sizeof (struct tcpiphdr)
828 #if INET6
829 )
830 #endif
831 );
832 if (rt->rt_rmx.rmx_ssthresh)
833 rt->rt_rmx.rmx_ssthresh =
834 (rt->rt_rmx.rmx_ssthresh + i) / 2;
835 else
836 rt->rt_rmx.rmx_ssthresh = i;
837 tcpstat.tcps_cachedssthresh++;
838 }
839 }
840 rt = inp->inp_route.ro_rt;
841 if (rt) {
842 /*
843 * mark route for deletion if no information is
844 * cached.
845 */
846 if ((tp->t_flags & TF_LQ_OVERFLOW) &&
847 ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0)){
848 if (rt->rt_rmx.rmx_rtt == 0)
849 rt->rt_flags |= RTF_DELCLONE;
850 }
851 }
852 no_valid_rt:
853 /* free the reassembly queue, if any */
854 (void) tcp_freeq(tp);
855
856 #ifdef __APPLE__
857 if (so->cached_in_sock_layer)
858 inp->inp_saved_ppcb = (caddr_t) tp;
859 #endif
860
861 inp->inp_ppcb = NULL;
862 soisdisconnected(so);
863 #if INET6
864 if (INP_CHECK_SOCKAF(so, AF_INET6))
865 in6_pcbdetach(inp);
866 else
867 #endif /* INET6 */
868 in_pcbdetach(inp);
869 tcpstat.tcps_closed++;
870 KERNEL_DEBUG(DBG_FNC_TCP_CLOSE | DBG_FUNC_END, tcpstat.tcps_closed,0,0,0,0);
871 return ((struct tcpcb *)0);
872 }
873
874 int
875 tcp_freeq(tp)
876 struct tcpcb *tp;
877 {
878
879 register struct tseg_qent *q;
880 int rv = 0;
881
882 while((q = LIST_FIRST(&tp->t_segq)) != NULL) {
883 LIST_REMOVE(q, tqe_q);
884 m_freem(q->tqe_m);
885 FREE(q, M_TSEGQ);
886 rv = 1;
887 }
888 return (rv);
889 }
890
891 void
892 tcp_drain()
893 {
894 if (do_tcpdrain)
895 {
896 struct inpcb *inpb;
897 struct tcpcb *tcpb;
898 struct tseg_qent *te;
899
900 /*
901 * Walk the tcpbs, if existing, and flush the reassembly queue,
902 * if there is one...
903 * XXX: The "Net/3" implementation doesn't imply that the TCP
904 * reassembly queue should be flushed, but in a situation
905 * where we're really low on mbufs, this is potentially
906 * usefull.
907 */
908 for (inpb = LIST_FIRST(tcbinfo.listhead); inpb;
909 inpb = LIST_NEXT(inpb, inp_list)) {
910 if ((tcpb = intotcpcb(inpb))) {
911 while ((te = LIST_FIRST(&tcpb->t_segq))
912 != NULL) {
913 LIST_REMOVE(te, tqe_q);
914 m_freem(te->tqe_m);
915 FREE(te, M_TSEGQ);
916 }
917 }
918 }
919
920 }
921 }
922
923 /*
924 * Notify a tcp user of an asynchronous error;
925 * store error as soft error, but wake up user
926 * (for now, won't do anything until can select for soft error).
927 *
928 * Do not wake up user since there currently is no mechanism for
929 * reporting soft errors (yet - a kqueue filter may be added).
930 */
931 static void
932 tcp_notify(inp, error)
933 struct inpcb *inp;
934 int error;
935 {
936 struct tcpcb *tp;
937
938 if (inp == NULL)
939 return; /* pcb is gone already */
940
941 tp = (struct tcpcb *)inp->inp_ppcb;
942
943 /*
944 * Ignore some errors if we are hooked up.
945 * If connection hasn't completed, has retransmitted several times,
946 * and receives a second error, give up now. This is better
947 * than waiting a long time to establish a connection that
948 * can never complete.
949 */
950 if (tp->t_state == TCPS_ESTABLISHED &&
951 (error == EHOSTUNREACH || error == ENETUNREACH ||
952 error == EHOSTDOWN)) {
953 return;
954 } else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 &&
955 tp->t_softerror)
956 tcp_drop(tp, error);
957 else
958 tp->t_softerror = error;
959 #if 0
960 wakeup((caddr_t) &so->so_timeo);
961 sorwakeup(so);
962 sowwakeup(so);
963 #endif
964 }
965
966 static int
967 tcp_pcblist SYSCTL_HANDLER_ARGS
968 {
969 int error, i, n, s;
970 struct inpcb *inp, **inp_list;
971 inp_gen_t gencnt;
972 struct xinpgen xig;
973
974 /*
975 * The process of preparing the TCB list is too time-consuming and
976 * resource-intensive to repeat twice on every request.
977 */
978 if (req->oldptr == 0) {
979 n = tcbinfo.ipi_count;
980 req->oldidx = 2 * (sizeof xig)
981 + (n + n/8) * sizeof(struct xtcpcb);
982 return 0;
983 }
984
985 if (req->newptr != 0)
986 return EPERM;
987
988 /*
989 * OK, now we're committed to doing something.
990 */
991 s = splnet();
992 gencnt = tcbinfo.ipi_gencnt;
993 n = tcbinfo.ipi_count;
994 splx(s);
995
996 xig.xig_len = sizeof xig;
997 xig.xig_count = n;
998 xig.xig_gen = gencnt;
999 xig.xig_sogen = so_gencnt;
1000 error = SYSCTL_OUT(req, &xig, sizeof xig);
1001 if (error)
1002 return error;
1003 /*
1004 * We are done if there is no pcb
1005 */
1006 if (n == 0)
1007 return 0;
1008
1009 inp_list = _MALLOC(n * sizeof *inp_list, M_TEMP, M_WAITOK);
1010 if (inp_list == 0)
1011 return ENOMEM;
1012
1013 s = splnet();
1014 for (inp = LIST_FIRST(tcbinfo.listhead), i = 0; inp && i < n;
1015 inp = LIST_NEXT(inp, inp_list)) {
1016 #ifdef __APPLE__
1017 if (inp->inp_gencnt <= gencnt)
1018 #else
1019 if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp))
1020 #endif
1021 inp_list[i++] = inp;
1022 }
1023 splx(s);
1024 n = i;
1025
1026 error = 0;
1027 for (i = 0; i < n; i++) {
1028 inp = inp_list[i];
1029 if (inp->inp_gencnt <= gencnt) {
1030 struct xtcpcb xt;
1031 caddr_t inp_ppcb;
1032 xt.xt_len = sizeof xt;
1033 /* XXX should avoid extra copy */
1034 bcopy(inp, &xt.xt_inp, sizeof *inp);
1035 inp_ppcb = inp->inp_ppcb;
1036 if (inp_ppcb != NULL)
1037 bcopy(inp_ppcb, &xt.xt_tp, sizeof xt.xt_tp);
1038 else
1039 bzero((char *) &xt.xt_tp, sizeof xt.xt_tp);
1040 if (inp->inp_socket)
1041 sotoxsocket(inp->inp_socket, &xt.xt_socket);
1042 error = SYSCTL_OUT(req, &xt, sizeof xt);
1043 }
1044 }
1045 if (!error) {
1046 /*
1047 * Give the user an updated idea of our state.
1048 * If the generation differs from what we told
1049 * her before, she knows that something happened
1050 * while we were processing this request, and it
1051 * might be necessary to retry.
1052 */
1053 s = splnet();
1054 xig.xig_gen = tcbinfo.ipi_gencnt;
1055 xig.xig_sogen = so_gencnt;
1056 xig.xig_count = tcbinfo.ipi_count;
1057 splx(s);
1058 error = SYSCTL_OUT(req, &xig, sizeof xig);
1059 }
1060 FREE(inp_list, M_TEMP);
1061 return error;
1062 }
1063
1064 SYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
1065 tcp_pcblist, "S,xtcpcb", "List of active TCP connections");
1066
1067 #ifndef __APPLE__
1068 static int
1069 tcp_getcred(SYSCTL_HANDLER_ARGS)
1070 {
1071 struct sockaddr_in addrs[2];
1072 struct inpcb *inp;
1073 int error, s;
1074
1075 error = suser(req->p);
1076 if (error)
1077 return (error);
1078 error = SYSCTL_IN(req, addrs, sizeof(addrs));
1079 if (error)
1080 return (error);
1081 s = splnet();
1082 inp = in_pcblookup_hash(&tcbinfo, addrs[1].sin_addr, addrs[1].sin_port,
1083 addrs[0].sin_addr, addrs[0].sin_port, 0, NULL);
1084 if (inp == NULL || inp->inp_socket == NULL) {
1085 error = ENOENT;
1086 goto out;
1087 }
1088 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
1089 out:
1090 splx(s);
1091 return (error);
1092 }
1093
1094 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
1095 0, 0, tcp_getcred, "S,ucred", "Get the ucred of a TCP connection");
1096
1097 #if INET6
1098 static int
1099 tcp6_getcred(SYSCTL_HANDLER_ARGS)
1100 {
1101 struct sockaddr_in6 addrs[2];
1102 struct inpcb *inp;
1103 int error, s, mapped = 0;
1104
1105 error = suser(req->p);
1106 if (error)
1107 return (error);
1108 error = SYSCTL_IN(req, addrs, sizeof(addrs));
1109 if (error)
1110 return (error);
1111 if (IN6_IS_ADDR_V4MAPPED(&addrs[0].sin6_addr)) {
1112 if (IN6_IS_ADDR_V4MAPPED(&addrs[1].sin6_addr))
1113 mapped = 1;
1114 else
1115 return (EINVAL);
1116 }
1117 s = splnet();
1118 if (mapped == 1)
1119 inp = in_pcblookup_hash(&tcbinfo,
1120 *(struct in_addr *)&addrs[1].sin6_addr.s6_addr[12],
1121 addrs[1].sin6_port,
1122 *(struct in_addr *)&addrs[0].sin6_addr.s6_addr[12],
1123 addrs[0].sin6_port,
1124 0, NULL);
1125 else
1126 inp = in6_pcblookup_hash(&tcbinfo, &addrs[1].sin6_addr,
1127 addrs[1].sin6_port,
1128 &addrs[0].sin6_addr, addrs[0].sin6_port,
1129 0, NULL);
1130 if (inp == NULL || inp->inp_socket == NULL) {
1131 error = ENOENT;
1132 goto out;
1133 }
1134 error = SYSCTL_OUT(req, inp->inp_socket->so_cred,
1135 sizeof(struct ucred));
1136 out:
1137 splx(s);
1138 return (error);
1139 }
1140
1141 SYSCTL_PROC(_net_inet6_tcp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
1142 0, 0,
1143 tcp6_getcred, "S,ucred", "Get the ucred of a TCP6 connection");
1144 #endif
1145 #endif /* __APPLE__*/
1146
1147 void
1148 tcp_ctlinput(cmd, sa, vip)
1149 int cmd;
1150 struct sockaddr *sa;
1151 void *vip;
1152 {
1153 struct ip *ip = vip;
1154 struct tcphdr *th;
1155 struct in_addr faddr;
1156 struct inpcb *inp;
1157 struct tcpcb *tp;
1158 void (*notify) __P((struct inpcb *, int)) = tcp_notify;
1159 tcp_seq icmp_seq;
1160 int s;
1161
1162 faddr = ((struct sockaddr_in *)sa)->sin_addr;
1163 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
1164 return;
1165
1166 if (cmd == PRC_QUENCH)
1167 notify = tcp_quench;
1168 else if (icmp_may_rst && (cmd == PRC_UNREACH_ADMIN_PROHIB ||
1169 cmd == PRC_UNREACH_PORT) && ip)
1170 notify = tcp_drop_syn_sent;
1171 else if (cmd == PRC_MSGSIZE)
1172 notify = tcp_mtudisc;
1173 else if (PRC_IS_REDIRECT(cmd)) {
1174 ip = 0;
1175 notify = in_rtchange;
1176 } else if (cmd == PRC_HOSTDEAD)
1177 ip = 0;
1178 else if ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0)
1179 return;
1180 if (ip) {
1181 s = splnet();
1182 th = (struct tcphdr *)((caddr_t)ip
1183 + (IP_VHL_HL(ip->ip_vhl) << 2));
1184 inp = in_pcblookup_hash(&tcbinfo, faddr, th->th_dport,
1185 ip->ip_src, th->th_sport, 0, NULL);
1186 if (inp != NULL && inp->inp_socket != NULL) {
1187 icmp_seq = htonl(th->th_seq);
1188 tp = intotcpcb(inp);
1189 if (SEQ_GEQ(icmp_seq, tp->snd_una) &&
1190 SEQ_LT(icmp_seq, tp->snd_max))
1191 (*notify)(inp, inetctlerrmap[cmd]);
1192 }
1193 splx(s);
1194 } else
1195 in_pcbnotifyall(&tcb, faddr, inetctlerrmap[cmd], notify);
1196 }
1197
1198 #if INET6
1199 void
1200 tcp6_ctlinput(cmd, sa, d)
1201 int cmd;
1202 struct sockaddr *sa;
1203 void *d;
1204 {
1205 struct tcphdr th;
1206 void (*notify) __P((struct inpcb *, int)) = tcp_notify;
1207 struct ip6_hdr *ip6;
1208 struct mbuf *m;
1209 struct ip6ctlparam *ip6cp = NULL;
1210 const struct sockaddr_in6 *sa6_src = NULL;
1211 int off;
1212 struct tcp_portonly {
1213 u_int16_t th_sport;
1214 u_int16_t th_dport;
1215 } *thp;
1216
1217 if (sa->sa_family != AF_INET6 ||
1218 sa->sa_len != sizeof(struct sockaddr_in6))
1219 return;
1220
1221 if (cmd == PRC_QUENCH)
1222 notify = tcp_quench;
1223 else if (cmd == PRC_MSGSIZE)
1224 notify = tcp_mtudisc;
1225 else if (!PRC_IS_REDIRECT(cmd) &&
1226 ((unsigned)cmd > PRC_NCMDS || inet6ctlerrmap[cmd] == 0))
1227 return;
1228
1229 /* if the parameter is from icmp6, decode it. */
1230 if (d != NULL) {
1231 ip6cp = (struct ip6ctlparam *)d;
1232 m = ip6cp->ip6c_m;
1233 ip6 = ip6cp->ip6c_ip6;
1234 off = ip6cp->ip6c_off;
1235 sa6_src = ip6cp->ip6c_src;
1236 } else {
1237 m = NULL;
1238 ip6 = NULL;
1239 off = 0; /* fool gcc */
1240 sa6_src = &sa6_any;
1241 }
1242
1243 if (ip6) {
1244 /*
1245 * XXX: We assume that when IPV6 is non NULL,
1246 * M and OFF are valid.
1247 */
1248
1249 /* check if we can safely examine src and dst ports */
1250 if (m->m_pkthdr.len < off + sizeof(*thp))
1251 return;
1252
1253 bzero(&th, sizeof(th));
1254 m_copydata(m, off, sizeof(*thp), (caddr_t)&th);
1255
1256 in6_pcbnotify(&tcb, sa, th.th_dport,
1257 (struct sockaddr *)ip6cp->ip6c_src,
1258 th.th_sport, cmd, notify);
1259 } else
1260 in6_pcbnotify(&tcb, sa, 0, (struct sockaddr *)sa6_src,
1261 0, cmd, notify);
1262 }
1263 #endif /* INET6 */
1264
1265
1266 /*
1267 * Following is where TCP initial sequence number generation occurs.
1268 *
1269 * There are two places where we must use initial sequence numbers:
1270 * 1. In SYN-ACK packets.
1271 * 2. In SYN packets.
1272 *
1273 * The ISNs in SYN-ACK packets have no monotonicity requirement,
1274 * and should be as unpredictable as possible to avoid the possibility
1275 * of spoofing and/or connection hijacking. To satisfy this
1276 * requirement, SYN-ACK ISNs are generated via the arc4random()
1277 * function. If exact RFC 1948 compliance is requested via sysctl,
1278 * these ISNs will be generated just like those in SYN packets.
1279 *
1280 * The ISNs in SYN packets must be monotonic; TIME_WAIT recycling
1281 * depends on this property. In addition, these ISNs should be
1282 * unguessable so as to prevent connection hijacking. To satisfy
1283 * the requirements of this situation, the algorithm outlined in
1284 * RFC 1948 is used to generate sequence numbers.
1285 *
1286 * For more information on the theory of operation, please see
1287 * RFC 1948.
1288 *
1289 * Implementation details:
1290 *
1291 * Time is based off the system timer, and is corrected so that it
1292 * increases by one megabyte per second. This allows for proper
1293 * recycling on high speed LANs while still leaving over an hour
1294 * before rollover.
1295 *
1296 * Two sysctls control the generation of ISNs:
1297 *
1298 * net.inet.tcp.isn_reseed_interval controls the number of seconds
1299 * between seeding of isn_secret. This is normally set to zero,
1300 * as reseeding should not be necessary.
1301 *
1302 * net.inet.tcp.strict_rfc1948 controls whether RFC 1948 is followed
1303 * strictly. When strict compliance is requested, reseeding is
1304 * disabled and SYN-ACKs will be generated in the same manner as
1305 * SYNs. Strict mode is disabled by default.
1306 *
1307 */
1308
1309 #define ISN_BYTES_PER_SECOND 1048576
1310
1311 u_char isn_secret[32];
1312 int isn_last_reseed;
1313 MD5_CTX isn_ctx;
1314
1315 tcp_seq
1316 tcp_new_isn(tp)
1317 struct tcpcb *tp;
1318 {
1319 u_int32_t md5_buffer[4];
1320 tcp_seq new_isn;
1321 struct timeval time;
1322
1323 /* Use arc4random for SYN-ACKs when not in exact RFC1948 mode. */
1324 if (((tp->t_state == TCPS_LISTEN) || (tp->t_state == TCPS_TIME_WAIT))
1325 && tcp_strict_rfc1948 == 0)
1326 #ifdef __APPLE__
1327 return random();
1328 #else
1329 return arc4random();
1330 #endif
1331
1332 /* Seed if this is the first use, reseed if requested. */
1333 if ((isn_last_reseed == 0) ||
1334 ((tcp_strict_rfc1948 == 0) && (tcp_isn_reseed_interval > 0) &&
1335 (((u_int)isn_last_reseed + (u_int)tcp_isn_reseed_interval*hz)
1336 < (u_int)time.tv_sec))) {
1337 #ifdef __APPLE__
1338 read_random(&isn_secret, sizeof(isn_secret));
1339 #else
1340 read_random_unlimited(&isn_secret, sizeof(isn_secret));
1341 #endif
1342 isn_last_reseed = time.tv_sec;
1343 }
1344
1345 /* Compute the md5 hash and return the ISN. */
1346 MD5Init(&isn_ctx);
1347 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_fport, sizeof(u_short));
1348 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_lport, sizeof(u_short));
1349 #if INET6
1350 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) {
1351 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->in6p_faddr,
1352 sizeof(struct in6_addr));
1353 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->in6p_laddr,
1354 sizeof(struct in6_addr));
1355 } else
1356 #endif
1357 {
1358 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_faddr,
1359 sizeof(struct in_addr));
1360 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_laddr,
1361 sizeof(struct in_addr));
1362 }
1363 MD5Update(&isn_ctx, (u_char *) &isn_secret, sizeof(isn_secret));
1364 MD5Final((u_char *) &md5_buffer, &isn_ctx);
1365 new_isn = (tcp_seq) md5_buffer[0];
1366 new_isn += time.tv_sec * (ISN_BYTES_PER_SECOND / hz);
1367 return new_isn;
1368 }
1369
1370 /*
1371 * When a source quench is received, close congestion window
1372 * to one segment. We will gradually open it again as we proceed.
1373 */
1374 void
1375 tcp_quench(inp, errno)
1376 struct inpcb *inp;
1377 int errno;
1378 {
1379 struct tcpcb *tp = intotcpcb(inp);
1380
1381 if (tp)
1382 tp->snd_cwnd = tp->t_maxseg;
1383 }
1384
1385 /*
1386 * When a specific ICMP unreachable message is received and the
1387 * connection state is SYN-SENT, drop the connection. This behavior
1388 * is controlled by the icmp_may_rst sysctl.
1389 */
1390 void
1391 tcp_drop_syn_sent(inp, errno)
1392 struct inpcb *inp;
1393 int errno;
1394 {
1395 struct tcpcb *tp = intotcpcb(inp);
1396
1397 if (tp && tp->t_state == TCPS_SYN_SENT)
1398 tcp_drop(tp, errno);
1399 }
1400
1401 /*
1402 * When `need fragmentation' ICMP is received, update our idea of the MSS
1403 * based on the new value in the route. Also nudge TCP to send something,
1404 * since we know the packet we just sent was dropped.
1405 * This duplicates some code in the tcp_mss() function in tcp_input.c.
1406 */
1407 void
1408 tcp_mtudisc(inp, errno)
1409 struct inpcb *inp;
1410 int errno;
1411 {
1412 struct tcpcb *tp = intotcpcb(inp);
1413 struct rtentry *rt;
1414 struct rmxp_tao *taop;
1415 struct socket *so = inp->inp_socket;
1416 int offered;
1417 int mss;
1418 #if INET6
1419 int isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
1420 #endif /* INET6 */
1421
1422 if (tp) {
1423 #if INET6
1424 if (isipv6)
1425 rt = tcp_rtlookup6(inp);
1426 else
1427 #endif /* INET6 */
1428 rt = tcp_rtlookup(inp);
1429 if (!rt || !rt->rt_rmx.rmx_mtu) {
1430 tp->t_maxopd = tp->t_maxseg =
1431 #if INET6
1432 isipv6 ? tcp_v6mssdflt :
1433 #endif /* INET6 */
1434 tcp_mssdflt;
1435 return;
1436 }
1437 taop = rmx_taop(rt->rt_rmx);
1438 offered = taop->tao_mssopt;
1439 mss = rt->rt_rmx.rmx_mtu -
1440 #if INET6
1441 (isipv6 ?
1442 sizeof(struct ip6_hdr) + sizeof(struct tcphdr) :
1443 #endif /* INET6 */
1444 sizeof(struct tcpiphdr)
1445 #if INET6
1446 )
1447 #endif /* INET6 */
1448 ;
1449
1450 if (offered)
1451 mss = min(mss, offered);
1452 /*
1453 * XXX - The above conditional probably violates the TCP
1454 * spec. The problem is that, since we don't know the
1455 * other end's MSS, we are supposed to use a conservative
1456 * default. But, if we do that, then MTU discovery will
1457 * never actually take place, because the conservative
1458 * default is much less than the MTUs typically seen
1459 * on the Internet today. For the moment, we'll sweep
1460 * this under the carpet.
1461 *
1462 * The conservative default might not actually be a problem
1463 * if the only case this occurs is when sending an initial
1464 * SYN with options and data to a host we've never talked
1465 * to before. Then, they will reply with an MSS value which
1466 * will get recorded and the new parameters should get
1467 * recomputed. For Further Study.
1468 */
1469 if (tp->t_maxopd <= mss)
1470 return;
1471 tp->t_maxopd = mss;
1472
1473 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
1474 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)
1475 mss -= TCPOLEN_TSTAMP_APPA;
1476 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
1477 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)
1478 mss -= TCPOLEN_CC_APPA;
1479
1480 if (so->so_snd.sb_hiwat < mss)
1481 mss = so->so_snd.sb_hiwat;
1482
1483 tp->t_maxseg = mss;
1484
1485 tcpstat.tcps_mturesent++;
1486 tp->t_rtttime = 0;
1487 tp->snd_nxt = tp->snd_una;
1488 tcp_output(tp);
1489 }
1490 }
1491
1492 /*
1493 * Look-up the routing entry to the peer of this inpcb. If no route
1494 * is found and it cannot be allocated the return NULL. This routine
1495 * is called by TCP routines that access the rmx structure and by tcp_mss
1496 * to get the interface MTU.
1497 */
1498 struct rtentry *
1499 tcp_rtlookup(inp)
1500 struct inpcb *inp;
1501 {
1502 struct route *ro;
1503 struct rtentry *rt;
1504
1505 ro = &inp->inp_route;
1506 if (ro == NULL)
1507 return (NULL);
1508 rt = ro->ro_rt;
1509 if (rt == NULL || !(rt->rt_flags & RTF_UP) || rt->generation_id != route_generation) {
1510 /* No route yet, so try to acquire one */
1511 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1512 ro->ro_dst.sa_family = AF_INET;
1513 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
1514 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
1515 inp->inp_faddr;
1516 rtalloc(ro);
1517 rt = ro->ro_rt;
1518 }
1519 }
1520 return rt;
1521 }
1522
1523 #if INET6
1524 struct rtentry *
1525 tcp_rtlookup6(inp)
1526 struct inpcb *inp;
1527 {
1528 struct route_in6 *ro6;
1529 struct rtentry *rt;
1530
1531 ro6 = &inp->in6p_route;
1532 rt = ro6->ro_rt;
1533 if (rt == NULL || !(rt->rt_flags & RTF_UP)) {
1534 /* No route yet, so try to acquire one */
1535 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1536 struct sockaddr_in6 *dst6;
1537
1538 dst6 = (struct sockaddr_in6 *)&ro6->ro_dst;
1539 dst6->sin6_family = AF_INET6;
1540 dst6->sin6_len = sizeof(*dst6);
1541 dst6->sin6_addr = inp->in6p_faddr;
1542 rtalloc((struct route *)ro6);
1543 rt = ro6->ro_rt;
1544 }
1545 }
1546 return rt;
1547 }
1548 #endif /* INET6 */
1549
1550 #if IPSEC
1551 /* compute ESP/AH header size for TCP, including outer IP header. */
1552 size_t
1553 ipsec_hdrsiz_tcp(tp)
1554 struct tcpcb *tp;
1555 {
1556 struct inpcb *inp;
1557 struct mbuf *m;
1558 size_t hdrsiz;
1559 struct ip *ip;
1560 #if INET6
1561 struct ip6_hdr *ip6 = NULL;
1562 #endif /* INET6 */
1563 struct tcphdr *th;
1564
1565 if ((tp == NULL) || ((inp = tp->t_inpcb) == NULL))
1566 return 0;
1567 MGETHDR(m, M_DONTWAIT, MT_DATA);
1568 if (!m)
1569 return 0;
1570
1571 #if INET6
1572 if ((inp->inp_vflag & INP_IPV6) != 0) {
1573 ip6 = mtod(m, struct ip6_hdr *);
1574 th = (struct tcphdr *)(ip6 + 1);
1575 m->m_pkthdr.len = m->m_len =
1576 sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
1577 tcp_fillheaders(tp, ip6, th);
1578 hdrsiz = ipsec6_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp);
1579 } else
1580 #endif /* INET6 */
1581 {
1582 ip = mtod(m, struct ip *);
1583 th = (struct tcphdr *)(ip + 1);
1584 m->m_pkthdr.len = m->m_len = sizeof(struct tcpiphdr);
1585 tcp_fillheaders(tp, ip, th);
1586 hdrsiz = ipsec4_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp);
1587 }
1588
1589 m_free(m);
1590 return hdrsiz;
1591 }
1592 #endif /*IPSEC*/
1593
1594 /*
1595 * Return a pointer to the cached information about the remote host.
1596 * The cached information is stored in the protocol specific part of
1597 * the route metrics.
1598 */
1599 struct rmxp_tao *
1600 tcp_gettaocache(inp)
1601 struct inpcb *inp;
1602 {
1603 struct rtentry *rt;
1604
1605 #if INET6
1606 if ((inp->inp_vflag & INP_IPV6) != 0)
1607 rt = tcp_rtlookup6(inp);
1608 else
1609 #endif /* INET6 */
1610 rt = tcp_rtlookup(inp);
1611
1612 /* Make sure this is a host route and is up. */
1613 if (rt == NULL ||
1614 (rt->rt_flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST))
1615 return NULL;
1616
1617 return rmx_taop(rt->rt_rmx);
1618 }
1619
1620 /*
1621 * Clear all the TAO cache entries, called from tcp_init.
1622 *
1623 * XXX
1624 * This routine is just an empty one, because we assume that the routing
1625 * routing tables are initialized at the same time when TCP, so there is
1626 * nothing in the cache left over.
1627 */
1628 static void
1629 tcp_cleartaocache()
1630 {
1631 }