xref: /freebsd/sys/netinet/tcp_subr.c (revision f9ce010afdd3136fc73e2b500f2ed916bf9cfa59)
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)tcp_subr.c	8.2 (Berkeley) 5/24/95
34  * $FreeBSD$
35  */
36 
37 #include "opt_compat.h"
38 #include "opt_tcpdebug.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/callout.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/proc.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
51 
52 #include <vm/vm_zone.h>
53 
54 #include <net/route.h>
55 #include <net/if.h>
56 
57 #define _IP_VHL
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/ip.h>
61 #include <netinet/in_pcb.h>
62 #include <netinet/in_var.h>
63 #include <netinet/ip_var.h>
64 #include <netinet/tcp.h>
65 #include <netinet/tcp_fsm.h>
66 #include <netinet/tcp_seq.h>
67 #include <netinet/tcp_timer.h>
68 #include <netinet/tcp_var.h>
69 #include <netinet/tcpip.h>
70 #ifdef TCPDEBUG
71 #include <netinet/tcp_debug.h>
72 #endif
73 
74 int 	tcp_mssdflt = TCP_MSS;
75 SYSCTL_INT(_net_inet_tcp, TCPCTL_MSSDFLT, mssdflt, CTLFLAG_RW,
76     &tcp_mssdflt , 0, "Default TCP Maximum Segment Size");
77 
78 #if 0
79 static int 	tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
80 SYSCTL_INT(_net_inet_tcp, TCPCTL_RTTDFLT, rttdflt, CTLFLAG_RW,
81     &tcp_rttdflt , 0, "Default maximum TCP Round Trip Time");
82 #endif
83 
84 static int	tcp_do_rfc1323 = 1;
85 SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1323, rfc1323, CTLFLAG_RW,
86     &tcp_do_rfc1323 , 0, "Enable rfc1323 (high performance TCP) extensions");
87 
88 static int	tcp_do_rfc1644 = 0;
89 SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1644, rfc1644, CTLFLAG_RW,
90     &tcp_do_rfc1644 , 0, "Enable rfc1644 (TTCP) extensions");
91 
92 static int	tcp_tcbhashsize = 0;
93 SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcbhashsize, CTLFLAG_RD,
94      &tcp_tcbhashsize, 0, "Size of TCP control-block hashtable");
95 
96 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pcbcount, CTLFLAG_RD,
97     &tcbinfo.ipi_count, 0, "Number of active PCBs");
98 
99 static void	tcp_cleartaocache __P((void));
100 static void	tcp_notify __P((struct inpcb *, int));
101 
102 /*
103  * Target size of TCP PCB hash tables. Must be a power of two.
104  *
105  * Note that this can be overridden by the kernel environment
106  * variable net.inet.tcp.tcbhashsize
107  */
108 #ifndef TCBHASHSIZE
109 #define TCBHASHSIZE	512
110 #endif
111 
112 /*
113  * This is the actual shape of what we allocate using the zone
114  * allocator.  Doing it this way allows us to protect both structures
115  * using the same generation count, and also eliminates the overhead
116  * of allocating tcpcbs separately.  By hiding the structure here,
117  * we avoid changing most of the rest of the code (although it needs
118  * to be changed, eventually, for greater efficiency).
119  */
120 #define	ALIGNMENT	32
121 #define	ALIGNM1		(ALIGNMENT - 1)
122 struct	inp_tp {
123 	union {
124 		struct	inpcb inp;
125 		char	align[(sizeof(struct inpcb) + ALIGNM1) & ~ALIGNM1];
126 	} inp_tp_u;
127 	struct	tcpcb tcb;
128 	struct	callout inp_tp_rexmt, inp_tp_persist, inp_tp_keep, inp_tp_2msl;
129 	struct	callout inp_tp_delack;
130 };
131 #undef ALIGNMENT
132 #undef ALIGNM1
133 
134 /*
135  * Tcp initialization
136  */
137 void
138 tcp_init()
139 {
140 	int hashsize;
141 
142 	tcp_iss = random();	/* wrong, but better than a constant */
143 	tcp_ccgen = 1;
144 	tcp_cleartaocache();
145 
146 	tcp_delacktime = TCPTV_DELACK;
147 	tcp_keepinit = TCPTV_KEEP_INIT;
148 	tcp_keepidle = TCPTV_KEEP_IDLE;
149 	tcp_keepintvl = TCPTV_KEEPINTVL;
150 	tcp_maxpersistidle = TCPTV_KEEP_IDLE;
151 	tcp_msl = TCPTV_MSL;
152 
153 	LIST_INIT(&tcb);
154 	tcbinfo.listhead = &tcb;
155 	TUNABLE_INT_FETCH("net.inet.tcp.tcbhashsize", TCBHASHSIZE, hashsize);
156 	if (!powerof2(hashsize)) {
157 		printf("WARNING: TCB hash size not a power of 2\n");
158 		hashsize = 512; /* safe default */
159 	}
160 	tcp_tcbhashsize = hashsize;
161 	tcbinfo.hashbase = hashinit(hashsize, M_PCB, &tcbinfo.hashmask);
162 	tcbinfo.porthashbase = hashinit(hashsize, M_PCB,
163 					&tcbinfo.porthashmask);
164 	tcbinfo.ipi_zone = zinit("tcpcb", sizeof(struct inp_tp), maxsockets,
165 				 ZONE_INTERRUPT, 0);
166 
167 	if (max_protohdr < sizeof(struct tcpiphdr))
168 		max_protohdr = sizeof(struct tcpiphdr);
169 	if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN)
170 		panic("tcp_init");
171 }
172 
173 /*
174  * Create template to be used to send tcp packets on a connection.
175  * Call after host entry created, allocates an mbuf and fills
176  * in a skeletal tcp/ip header, minimizing the amount of work
177  * necessary when the connection is used.
178  */
179 struct tcpiphdr *
180 tcp_template(tp)
181 	struct tcpcb *tp;
182 {
183 	register struct inpcb *inp = tp->t_inpcb;
184 	register struct mbuf *m;
185 	register struct tcpiphdr *n;
186 
187 	if ((n = tp->t_template) == 0) {
188 		m = m_get(M_DONTWAIT, MT_HEADER);
189 		if (m == NULL)
190 			return (0);
191 		m->m_len = sizeof (struct tcpiphdr);
192 		n = mtod(m, struct tcpiphdr *);
193 	}
194 	bzero(n->ti_x1, sizeof(n->ti_x1));
195 	n->ti_pr = IPPROTO_TCP;
196 	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
197 	n->ti_src = inp->inp_laddr;
198 	n->ti_dst = inp->inp_faddr;
199 	n->ti_sport = inp->inp_lport;
200 	n->ti_dport = inp->inp_fport;
201 	n->ti_seq = 0;
202 	n->ti_ack = 0;
203 	n->ti_x2 = 0;
204 	n->ti_off = 5;
205 	n->ti_flags = 0;
206 	n->ti_win = 0;
207 	n->ti_sum = 0;
208 	n->ti_urp = 0;
209 	return (n);
210 }
211 
212 /*
213  * Send a single message to the TCP at address specified by
214  * the given TCP/IP header.  If m == 0, then we make a copy
215  * of the tcpiphdr at ti and send directly to the addressed host.
216  * This is used to force keep alive messages out using the TCP
217  * template for a connection tp->t_template.  If flags are given
218  * then we send a message back to the TCP which originated the
219  * segment ti, and discard the mbuf containing it and any other
220  * attached mbufs.
221  *
222  * In any case the ack and sequence number of the transmitted
223  * segment are as specified by the parameters.
224  *
225  * NOTE: If m != NULL, then ti must point to *inside* the mbuf.
226  */
227 void
228 tcp_respond(tp, ti, m, ack, seq, flags)
229 	struct tcpcb *tp;
230 	register struct tcpiphdr *ti;
231 	register struct mbuf *m;
232 	tcp_seq ack, seq;
233 	int flags;
234 {
235 	register int tlen;
236 	int win = 0;
237 	struct route *ro = 0;
238 	struct route sro;
239 
240 	if (tp) {
241 		if (!(flags & TH_RST))
242 			win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
243 		ro = &tp->t_inpcb->inp_route;
244 	} else {
245 		ro = &sro;
246 		bzero(ro, sizeof *ro);
247 	}
248 	if (m == 0) {
249 		m = m_gethdr(M_DONTWAIT, MT_HEADER);
250 		if (m == NULL)
251 			return;
252 #ifdef TCP_COMPAT_42
253 		tlen = 1;
254 #else
255 		tlen = 0;
256 #endif
257 		m->m_data += max_linkhdr;
258 		*mtod(m, struct tcpiphdr *) = *ti;
259 		ti = mtod(m, struct tcpiphdr *);
260 		flags = TH_ACK;
261 	} else {
262 		m_freem(m->m_next);
263 		m->m_next = 0;
264 		m->m_data = (caddr_t)ti;
265 		m->m_len = sizeof (struct tcpiphdr);
266 		tlen = 0;
267 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
268 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, n_long);
269 		xchg(ti->ti_dport, ti->ti_sport, n_short);
270 #undef xchg
271 	}
272 	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
273 	tlen += sizeof (struct tcpiphdr);
274 	m->m_len = tlen;
275 	m->m_pkthdr.len = tlen;
276 	m->m_pkthdr.rcvif = (struct ifnet *) 0;
277 	bzero(ti->ti_x1, sizeof(ti->ti_x1));
278 	ti->ti_seq = htonl(seq);
279 	ti->ti_ack = htonl(ack);
280 	ti->ti_x2 = 0;
281 	ti->ti_off = sizeof (struct tcphdr) >> 2;
282 	ti->ti_flags = flags;
283 	if (tp)
284 		ti->ti_win = htons((u_short) (win >> tp->rcv_scale));
285 	else
286 		ti->ti_win = htons((u_short)win);
287 	ti->ti_urp = 0;
288 	ti->ti_sum = 0;
289 	ti->ti_sum = in_cksum(m, tlen);
290 	((struct ip *)ti)->ip_len = tlen;
291 	((struct ip *)ti)->ip_ttl = ip_defttl;
292 #ifdef TCPDEBUG
293 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
294 		tcp_trace(TA_OUTPUT, 0, tp, ti, 0);
295 #endif
296 	(void) ip_output(m, NULL, ro, 0, NULL);
297 	if (ro == &sro && ro->ro_rt) {
298 		RTFREE(ro->ro_rt);
299 	}
300 }
301 
302 /*
303  * Create a new TCP control block, making an
304  * empty reassembly queue and hooking it to the argument
305  * protocol control block.  The `inp' parameter must have
306  * come from the zone allocator set up in tcp_init().
307  */
308 struct tcpcb *
309 tcp_newtcpcb(inp)
310 	struct inpcb *inp;
311 {
312 	struct inp_tp *it;
313 	register struct tcpcb *tp;
314 
315 	it = (struct inp_tp *)inp;
316 	tp = &it->tcb;
317 	bzero((char *) tp, sizeof(struct tcpcb));
318 	tp->t_segq = NULL;
319 	tp->t_maxseg = tp->t_maxopd = tcp_mssdflt;
320 
321 	/* Set up our timeouts. */
322 	callout_init(tp->tt_rexmt = &it->inp_tp_rexmt);
323 	callout_init(tp->tt_persist = &it->inp_tp_persist);
324 	callout_init(tp->tt_keep = &it->inp_tp_keep);
325 	callout_init(tp->tt_2msl = &it->inp_tp_2msl);
326 	callout_init(tp->tt_delack = &it->inp_tp_delack);
327 
328 	if (tcp_do_rfc1323)
329 		tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP);
330 	if (tcp_do_rfc1644)
331 		tp->t_flags |= TF_REQ_CC;
332 	tp->t_inpcb = inp;	/* XXX */
333 	/*
334 	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
335 	 * rtt estimate.  Set rttvar so that srtt + 4 * rttvar gives
336 	 * reasonable initial retransmit time.
337 	 */
338 	tp->t_srtt = TCPTV_SRTTBASE;
339 	tp->t_rttvar = ((TCPTV_RTOBASE - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4;
340 	tp->t_rttmin = TCPTV_MIN;
341 	tp->t_rxtcur = TCPTV_RTOBASE;
342 	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
343 	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
344 	tp->t_rcvtime = ticks;
345 	inp->inp_ip_ttl = ip_defttl;
346 	inp->inp_ppcb = (caddr_t)tp;
347 	return (tp);		/* XXX */
348 }
349 
350 /*
351  * Drop a TCP connection, reporting
352  * the specified error.  If connection is synchronized,
353  * then send a RST to peer.
354  */
355 struct tcpcb *
356 tcp_drop(tp, errno)
357 	register struct tcpcb *tp;
358 	int errno;
359 {
360 	struct socket *so = tp->t_inpcb->inp_socket;
361 
362 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
363 		tp->t_state = TCPS_CLOSED;
364 		(void) tcp_output(tp);
365 		tcpstat.tcps_drops++;
366 	} else
367 		tcpstat.tcps_conndrops++;
368 	if (errno == ETIMEDOUT && tp->t_softerror)
369 		errno = tp->t_softerror;
370 	so->so_error = errno;
371 	return (tcp_close(tp));
372 }
373 
374 /*
375  * Close a TCP control block:
376  *	discard all space held by the tcp
377  *	discard internet protocol block
378  *	wake up any sleepers
379  */
380 struct tcpcb *
381 tcp_close(tp)
382 	register struct tcpcb *tp;
383 {
384 	register struct mbuf *q;
385 	register struct mbuf *nq;
386 	struct inpcb *inp = tp->t_inpcb;
387 	struct socket *so = inp->inp_socket;
388 	register struct rtentry *rt;
389 	int dosavessthresh;
390 
391 	/*
392 	 * Make sure that all of our timers are stopped before we
393 	 * delete the PCB.
394 	 */
395 	callout_stop(tp->tt_rexmt);
396 	callout_stop(tp->tt_persist);
397 	callout_stop(tp->tt_keep);
398 	callout_stop(tp->tt_2msl);
399 	callout_stop(tp->tt_delack);
400 
401 	/*
402 	 * If we got enough samples through the srtt filter,
403 	 * save the rtt and rttvar in the routing entry.
404 	 * 'Enough' is arbitrarily defined as the 16 samples.
405 	 * 16 samples is enough for the srtt filter to converge
406 	 * to within 5% of the correct value; fewer samples and
407 	 * we could save a very bogus rtt.
408 	 *
409 	 * Don't update the default route's characteristics and don't
410 	 * update anything that the user "locked".
411 	 */
412 	if (tp->t_rttupdated >= 16 &&
413 	    (rt = inp->inp_route.ro_rt) &&
414 	    ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr != INADDR_ANY) {
415 		register u_long i = 0;
416 
417 		if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
418 			i = tp->t_srtt *
419 			    (RTM_RTTUNIT / (hz * TCP_RTT_SCALE));
420 			if (rt->rt_rmx.rmx_rtt && i)
421 				/*
422 				 * filter this update to half the old & half
423 				 * the new values, converting scale.
424 				 * See route.h and tcp_var.h for a
425 				 * description of the scaling constants.
426 				 */
427 				rt->rt_rmx.rmx_rtt =
428 				    (rt->rt_rmx.rmx_rtt + i) / 2;
429 			else
430 				rt->rt_rmx.rmx_rtt = i;
431 			tcpstat.tcps_cachedrtt++;
432 		}
433 		if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
434 			i = tp->t_rttvar *
435 			    (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE));
436 			if (rt->rt_rmx.rmx_rttvar && i)
437 				rt->rt_rmx.rmx_rttvar =
438 				    (rt->rt_rmx.rmx_rttvar + i) / 2;
439 			else
440 				rt->rt_rmx.rmx_rttvar = i;
441 			tcpstat.tcps_cachedrttvar++;
442 		}
443 		/*
444 		 * The old comment here said:
445 		 * update the pipelimit (ssthresh) if it has been updated
446 		 * already or if a pipesize was specified & the threshhold
447 		 * got below half the pipesize.  I.e., wait for bad news
448 		 * before we start updating, then update on both good
449 		 * and bad news.
450 		 *
451 		 * But we want to save the ssthresh even if no pipesize is
452 		 * specified explicitly in the route, because such
453 		 * connections still have an implicit pipesize specified
454 		 * by the global tcp_sendspace.  In the absence of a reliable
455 		 * way to calculate the pipesize, it will have to do.
456 		 */
457 		i = tp->snd_ssthresh;
458 		if (rt->rt_rmx.rmx_sendpipe != 0)
459 			dosavessthresh = (i < rt->rt_rmx.rmx_sendpipe / 2);
460 		else
461 			dosavessthresh = (i < so->so_snd.sb_hiwat / 2);
462 		if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
463 		     i != 0 && rt->rt_rmx.rmx_ssthresh != 0)
464 		    || dosavessthresh) {
465 			/*
466 			 * convert the limit from user data bytes to
467 			 * packets then to packet data bytes.
468 			 */
469 			i = (i + tp->t_maxseg / 2) / tp->t_maxseg;
470 			if (i < 2)
471 				i = 2;
472 			i *= (u_long)(tp->t_maxseg + sizeof (struct tcpiphdr));
473 			if (rt->rt_rmx.rmx_ssthresh)
474 				rt->rt_rmx.rmx_ssthresh =
475 				    (rt->rt_rmx.rmx_ssthresh + i) / 2;
476 			else
477 				rt->rt_rmx.rmx_ssthresh = i;
478 			tcpstat.tcps_cachedssthresh++;
479 		}
480 	}
481 	/* free the reassembly queue, if any */
482 	for (q = tp->t_segq; q; q = nq) {
483 		nq = q->m_nextpkt;
484 		tp->t_segq = nq;
485 		m_freem(q);
486 	}
487 	if (tp->t_template)
488 		(void) m_free(dtom(tp->t_template));
489 	inp->inp_ppcb = NULL;
490 	soisdisconnected(so);
491 	in_pcbdetach(inp);
492 	tcpstat.tcps_closed++;
493 	return ((struct tcpcb *)0);
494 }
495 
496 void
497 tcp_drain()
498 {
499 
500 }
501 
502 /*
503  * Notify a tcp user of an asynchronous error;
504  * store error as soft error, but wake up user
505  * (for now, won't do anything until can select for soft error).
506  */
507 static void
508 tcp_notify(inp, error)
509 	struct inpcb *inp;
510 	int error;
511 {
512 	register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
513 	register struct socket *so = inp->inp_socket;
514 
515 	/*
516 	 * Ignore some errors if we are hooked up.
517 	 * If connection hasn't completed, has retransmitted several times,
518 	 * and receives a second error, give up now.  This is better
519 	 * than waiting a long time to establish a connection that
520 	 * can never complete.
521 	 */
522 	if (tp->t_state == TCPS_ESTABLISHED &&
523 	     (error == EHOSTUNREACH || error == ENETUNREACH ||
524 	      error == EHOSTDOWN)) {
525 		return;
526 	} else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 &&
527 	    tp->t_softerror)
528 		so->so_error = error;
529 	else
530 		tp->t_softerror = error;
531 	wakeup((caddr_t) &so->so_timeo);
532 	sorwakeup(so);
533 	sowwakeup(so);
534 }
535 
536 static int
537 tcp_pcblist SYSCTL_HANDLER_ARGS
538 {
539 	int error, i, n, s;
540 	struct inpcb *inp, **inp_list;
541 	inp_gen_t gencnt;
542 	struct xinpgen xig;
543 
544 	/*
545 	 * The process of preparing the TCB list is too time-consuming and
546 	 * resource-intensive to repeat twice on every request.
547 	 */
548 	if (req->oldptr == 0) {
549 		n = tcbinfo.ipi_count;
550 		req->oldidx = 2 * (sizeof xig)
551 			+ (n + n/8) * sizeof(struct xtcpcb);
552 		return 0;
553 	}
554 
555 	if (req->newptr != 0)
556 		return EPERM;
557 
558 	/*
559 	 * OK, now we're committed to doing something.
560 	 */
561 	s = splnet();
562 	gencnt = tcbinfo.ipi_gencnt;
563 	n = tcbinfo.ipi_count;
564 	splx(s);
565 
566 	xig.xig_len = sizeof xig;
567 	xig.xig_count = n;
568 	xig.xig_gen = gencnt;
569 	xig.xig_sogen = so_gencnt;
570 	error = SYSCTL_OUT(req, &xig, sizeof xig);
571 	if (error)
572 		return error;
573 
574 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
575 	if (inp_list == 0)
576 		return ENOMEM;
577 
578 	s = splnet();
579 	for (inp = tcbinfo.listhead->lh_first, i = 0; inp && i < n;
580 	     inp = inp->inp_list.le_next) {
581 		if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp))
582 			inp_list[i++] = inp;
583 	}
584 	splx(s);
585 	n = i;
586 
587 	error = 0;
588 	for (i = 0; i < n; i++) {
589 		inp = inp_list[i];
590 		if (inp->inp_gencnt <= gencnt) {
591 			struct xtcpcb xt;
592 			caddr_t inp_ppcb;
593 			xt.xt_len = sizeof xt;
594 			/* XXX should avoid extra copy */
595 			bcopy(inp, &xt.xt_inp, sizeof *inp);
596 			inp_ppcb = inp->inp_ppcb;
597 			if (inp_ppcb != NULL)
598 				bcopy(inp_ppcb, &xt.xt_tp, sizeof xt.xt_tp);
599 			else
600 				bzero((char *) &xt.xt_tp, sizeof xt.xt_tp);
601 			if (inp->inp_socket)
602 				sotoxsocket(inp->inp_socket, &xt.xt_socket);
603 			error = SYSCTL_OUT(req, &xt, sizeof xt);
604 		}
605 	}
606 	if (!error) {
607 		/*
608 		 * Give the user an updated idea of our state.
609 		 * If the generation differs from what we told
610 		 * her before, she knows that something happened
611 		 * while we were processing this request, and it
612 		 * might be necessary to retry.
613 		 */
614 		s = splnet();
615 		xig.xig_gen = tcbinfo.ipi_gencnt;
616 		xig.xig_sogen = so_gencnt;
617 		xig.xig_count = tcbinfo.ipi_count;
618 		splx(s);
619 		error = SYSCTL_OUT(req, &xig, sizeof xig);
620 	}
621 	free(inp_list, M_TEMP);
622 	return error;
623 }
624 
625 SYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
626 	    tcp_pcblist, "S,xtcpcb", "List of active TCP connections");
627 
628 static int
629 tcp_getcred SYSCTL_HANDLER_ARGS
630 {
631 	struct sockaddr_in addrs[2];
632 	struct inpcb *inp;
633 	int error, s;
634 
635 	error = suser(req->p);
636 	if (error)
637 		return (error);
638 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
639 	if (error)
640 		return (error);
641 	s = splnet();
642 	inp = in_pcblookup_hash(&tcbinfo, addrs[1].sin_addr, addrs[1].sin_port,
643 	    addrs[0].sin_addr, addrs[0].sin_port, 0);
644 	if (inp == NULL || inp->inp_socket == NULL ||
645 	    inp->inp_socket->so_cred == NULL) {
646 		error = ENOENT;
647 		goto out;
648 	}
649 	error = SYSCTL_OUT(req, inp->inp_socket->so_cred->pc_ucred,
650 	    sizeof(struct ucred));
651 out:
652 	splx(s);
653 	return (error);
654 }
655 
656 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
657     0, 0, tcp_getcred, "S,ucred", "Get the ucred of a TCP connection");
658 
659 void
660 tcp_ctlinput(cmd, sa, vip)
661 	int cmd;
662 	struct sockaddr *sa;
663 	void *vip;
664 {
665 	register struct ip *ip = vip;
666 	register struct tcphdr *th;
667 	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
668 
669 	if (cmd == PRC_QUENCH)
670 		notify = tcp_quench;
671 	else if (cmd == PRC_MSGSIZE)
672 		notify = tcp_mtudisc;
673 	else if (!PRC_IS_REDIRECT(cmd) &&
674 		 ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0))
675 		return;
676 	if (ip) {
677 		th = (struct tcphdr *)((caddr_t)ip
678 				       + (IP_VHL_HL(ip->ip_vhl) << 2));
679 		in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport,
680 			cmd, notify);
681 	} else
682 		in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify);
683 }
684 
685 /*
686  * When a source quench is received, close congestion window
687  * to one segment.  We will gradually open it again as we proceed.
688  */
689 void
690 tcp_quench(inp, errno)
691 	struct inpcb *inp;
692 	int errno;
693 {
694 	struct tcpcb *tp = intotcpcb(inp);
695 
696 	if (tp)
697 		tp->snd_cwnd = tp->t_maxseg;
698 }
699 
700 /*
701  * When `need fragmentation' ICMP is received, update our idea of the MSS
702  * based on the new value in the route.  Also nudge TCP to send something,
703  * since we know the packet we just sent was dropped.
704  * This duplicates some code in the tcp_mss() function in tcp_input.c.
705  */
706 void
707 tcp_mtudisc(inp, errno)
708 	struct inpcb *inp;
709 	int errno;
710 {
711 	struct tcpcb *tp = intotcpcb(inp);
712 	struct rtentry *rt;
713 	struct rmxp_tao *taop;
714 	struct socket *so = inp->inp_socket;
715 	int offered;
716 	int mss;
717 
718 	if (tp) {
719 		rt = tcp_rtlookup(inp);
720 		if (!rt || !rt->rt_rmx.rmx_mtu) {
721 			tp->t_maxopd = tp->t_maxseg = tcp_mssdflt;
722 			return;
723 		}
724 		taop = rmx_taop(rt->rt_rmx);
725 		offered = taop->tao_mssopt;
726 		mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
727 		if (offered)
728 			mss = min(mss, offered);
729 		/*
730 		 * XXX - The above conditional probably violates the TCP
731 		 * spec.  The problem is that, since we don't know the
732 		 * other end's MSS, we are supposed to use a conservative
733 		 * default.  But, if we do that, then MTU discovery will
734 		 * never actually take place, because the conservative
735 		 * default is much less than the MTUs typically seen
736 		 * on the Internet today.  For the moment, we'll sweep
737 		 * this under the carpet.
738 		 *
739 		 * The conservative default might not actually be a problem
740 		 * if the only case this occurs is when sending an initial
741 		 * SYN with options and data to a host we've never talked
742 		 * to before.  Then, they will reply with an MSS value which
743 		 * will get recorded and the new parameters should get
744 		 * recomputed.  For Further Study.
745 		 */
746 		if (tp->t_maxopd <= mss)
747 			return;
748 		tp->t_maxopd = mss;
749 
750 		if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
751 		    (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)
752 			mss -= TCPOLEN_TSTAMP_APPA;
753 		if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
754 		    (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)
755 			mss -= TCPOLEN_CC_APPA;
756 #if	(MCLBYTES & (MCLBYTES - 1)) == 0
757 		if (mss > MCLBYTES)
758 			mss &= ~(MCLBYTES-1);
759 #else
760 		if (mss > MCLBYTES)
761 			mss = mss / MCLBYTES * MCLBYTES;
762 #endif
763 		if (so->so_snd.sb_hiwat < mss)
764 			mss = so->so_snd.sb_hiwat;
765 
766 		tp->t_maxseg = mss;
767 
768 		tcpstat.tcps_mturesent++;
769 		tp->t_rtttime = 0;
770 		tp->snd_nxt = tp->snd_una;
771 		tcp_output(tp);
772 	}
773 }
774 
775 /*
776  * Look-up the routing entry to the peer of this inpcb.  If no route
777  * is found and it cannot be allocated the return NULL.  This routine
778  * is called by TCP routines that access the rmx structure and by tcp_mss
779  * to get the interface MTU.
780  */
781 struct rtentry *
782 tcp_rtlookup(inp)
783 	struct inpcb *inp;
784 {
785 	struct route *ro;
786 	struct rtentry *rt;
787 
788 	ro = &inp->inp_route;
789 	rt = ro->ro_rt;
790 	if (rt == NULL || !(rt->rt_flags & RTF_UP)) {
791 		/* No route yet, so try to acquire one */
792 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
793 			ro->ro_dst.sa_family = AF_INET;
794 			ro->ro_dst.sa_len = sizeof(ro->ro_dst);
795 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
796 				inp->inp_faddr;
797 			rtalloc(ro);
798 			rt = ro->ro_rt;
799 		}
800 	}
801 	return rt;
802 }
803 
804 /*
805  * Return a pointer to the cached information about the remote host.
806  * The cached information is stored in the protocol specific part of
807  * the route metrics.
808  */
809 struct rmxp_tao *
810 tcp_gettaocache(inp)
811 	struct inpcb *inp;
812 {
813 	struct rtentry *rt = tcp_rtlookup(inp);
814 
815 	/* Make sure this is a host route and is up. */
816 	if (rt == NULL ||
817 	    (rt->rt_flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST))
818 		return NULL;
819 
820 	return rmx_taop(rt->rt_rmx);
821 }
822 
823 /*
824  * Clear all the TAO cache entries, called from tcp_init.
825  *
826  * XXX
827  * This routine is just an empty one, because we assume that the routing
828  * routing tables are initialized at the same time when TCP, so there is
829  * nothing in the cache left over.
830  */
831 static void
832 tcp_cleartaocache()
833 {
834 }
835