Home | History | Annotate | Line # | Download | only in netinet
tcp_subr.c revision 1.13
      1 /*	$NetBSD: tcp_subr.c,v 1.13 1995/04/13 06:36:44 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1988, 1990, 1993
      5  *	The Regents of the University of California.  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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)tcp_subr.c	8.1 (Berkeley) 6/10/93
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/proc.h>
     40 #include <sys/systm.h>
     41 #include <sys/malloc.h>
     42 #include <sys/mbuf.h>
     43 #include <sys/socket.h>
     44 #include <sys/socketvar.h>
     45 #include <sys/protosw.h>
     46 #include <sys/errno.h>
     47 
     48 #include <net/route.h>
     49 #include <net/if.h>
     50 
     51 #include <netinet/in.h>
     52 #include <netinet/in_systm.h>
     53 #include <netinet/ip.h>
     54 #include <netinet/in_pcb.h>
     55 #include <netinet/ip_var.h>
     56 #include <netinet/ip_icmp.h>
     57 #include <netinet/tcp.h>
     58 #include <netinet/tcp_fsm.h>
     59 #include <netinet/tcp_seq.h>
     60 #include <netinet/tcp_timer.h>
     61 #include <netinet/tcp_var.h>
     62 #include <netinet/tcpip.h>
     63 
     64 /* patchable/settable parameters for tcp */
     65 int 	tcp_mssdflt = TCP_MSS;
     66 int 	tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
     67 int	tcp_do_rfc1323 = 1;
     68 
     69 extern	struct inpcb *tcp_last_inpcb;
     70 
     71 /*
     72  * Tcp initialization
     73  */
     74 void
     75 tcp_init()
     76 {
     77 
     78 	tcp_iss = 1;		/* wrong */
     79 	tcb.inp_next = tcb.inp_prev = &tcb;
     80 	if (max_protohdr < sizeof(struct tcpiphdr))
     81 		max_protohdr = sizeof(struct tcpiphdr);
     82 	if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN)
     83 		panic("tcp_init");
     84 }
     85 
     86 /*
     87  * Create template to be used to send tcp packets on a connection.
     88  * Call after host entry created, allocates an mbuf and fills
     89  * in a skeletal tcp/ip header, minimizing the amount of work
     90  * necessary when the connection is used.
     91  */
     92 struct tcpiphdr *
     93 tcp_template(tp)
     94 	struct tcpcb *tp;
     95 {
     96 	register struct inpcb *inp = tp->t_inpcb;
     97 	register struct mbuf *m;
     98 	register struct tcpiphdr *n;
     99 
    100 	if ((n = tp->t_template) == 0) {
    101 		m = m_get(M_DONTWAIT, MT_HEADER);
    102 		if (m == NULL)
    103 			return (0);
    104 		m->m_len = sizeof (struct tcpiphdr);
    105 		n = mtod(m, struct tcpiphdr *);
    106 	}
    107 	n->ti_next = n->ti_prev = 0;
    108 	n->ti_x1 = 0;
    109 	n->ti_pr = IPPROTO_TCP;
    110 	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
    111 	n->ti_src = inp->inp_laddr;
    112 	n->ti_dst = inp->inp_faddr;
    113 	n->ti_sport = inp->inp_lport;
    114 	n->ti_dport = inp->inp_fport;
    115 	n->ti_seq = 0;
    116 	n->ti_ack = 0;
    117 	n->ti_x2 = 0;
    118 	n->ti_off = 5;
    119 	n->ti_flags = 0;
    120 	n->ti_win = 0;
    121 	n->ti_sum = 0;
    122 	n->ti_urp = 0;
    123 	return (n);
    124 }
    125 
    126 /*
    127  * Send a single message to the TCP at address specified by
    128  * the given TCP/IP header.  If m == 0, then we make a copy
    129  * of the tcpiphdr at ti and send directly to the addressed host.
    130  * This is used to force keep alive messages out using the TCP
    131  * template for a connection tp->t_template.  If flags are given
    132  * then we send a message back to the TCP which originated the
    133  * segment ti, and discard the mbuf containing it and any other
    134  * attached mbufs.
    135  *
    136  * In any case the ack and sequence number of the transmitted
    137  * segment are as specified by the parameters.
    138  */
    139 void
    140 tcp_respond(tp, ti, m, ack, seq, flags)
    141 	struct tcpcb *tp;
    142 	register struct tcpiphdr *ti;
    143 	register struct mbuf *m;
    144 	tcp_seq ack, seq;
    145 	int flags;
    146 {
    147 	register int tlen;
    148 	int win = 0;
    149 	struct route *ro = 0;
    150 
    151 	if (tp) {
    152 		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
    153 		ro = &tp->t_inpcb->inp_route;
    154 	}
    155 	if (m == 0) {
    156 		m = m_gethdr(M_DONTWAIT, MT_HEADER);
    157 		if (m == NULL)
    158 			return;
    159 #ifdef TCP_COMPAT_42
    160 		tlen = 1;
    161 #else
    162 		tlen = 0;
    163 #endif
    164 		m->m_data += max_linkhdr;
    165 		*mtod(m, struct tcpiphdr *) = *ti;
    166 		ti = mtod(m, struct tcpiphdr *);
    167 		flags = TH_ACK;
    168 	} else {
    169 		m_freem(m->m_next);
    170 		m->m_next = 0;
    171 		m->m_data = (caddr_t)ti;
    172 		m->m_len = sizeof (struct tcpiphdr);
    173 		tlen = 0;
    174 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
    175 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_int32_t);
    176 		xchg(ti->ti_dport, ti->ti_sport, u_int16_t);
    177 #undef xchg
    178 	}
    179 	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
    180 	tlen += sizeof (struct tcpiphdr);
    181 	m->m_len = tlen;
    182 	m->m_pkthdr.len = tlen;
    183 	m->m_pkthdr.rcvif = (struct ifnet *) 0;
    184 	ti->ti_next = ti->ti_prev = 0;
    185 	ti->ti_x1 = 0;
    186 	ti->ti_seq = htonl(seq);
    187 	ti->ti_ack = htonl(ack);
    188 	ti->ti_x2 = 0;
    189 	ti->ti_off = sizeof (struct tcphdr) >> 2;
    190 	ti->ti_flags = flags;
    191 	if (tp)
    192 		ti->ti_win = htons((u_int16_t) (win >> tp->rcv_scale));
    193 	else
    194 		ti->ti_win = htons((u_int16_t)win);
    195 	ti->ti_urp = 0;
    196 	ti->ti_sum = 0;
    197 	ti->ti_sum = in_cksum(m, tlen);
    198 	((struct ip *)ti)->ip_len = tlen;
    199 	((struct ip *)ti)->ip_ttl = ip_defttl;
    200 	(void) ip_output(m, NULL, ro, 0, NULL);
    201 }
    202 
    203 /*
    204  * Create a new TCP control block, making an
    205  * empty reassembly queue and hooking it to the argument
    206  * protocol control block.
    207  */
    208 struct tcpcb *
    209 tcp_newtcpcb(inp)
    210 	struct inpcb *inp;
    211 {
    212 	register struct tcpcb *tp;
    213 
    214 	tp = malloc(sizeof(*tp), M_PCB, M_NOWAIT);
    215 	if (tp == NULL)
    216 		return ((struct tcpcb *)0);
    217 	bzero((char *) tp, sizeof(struct tcpcb));
    218 	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
    219 	tp->t_maxseg = tcp_mssdflt;
    220 
    221 	tp->t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
    222 	tp->t_inpcb = inp;
    223 	/*
    224 	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
    225 	 * rtt estimate.  Set rttvar so that srtt + 2 * rttvar gives
    226 	 * reasonable initial retransmit time.
    227 	 */
    228 	tp->t_srtt = TCPTV_SRTTBASE;
    229 	tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2;
    230 	tp->t_rttmin = TCPTV_MIN;
    231 	TCPT_RANGESET(tp->t_rxtcur,
    232 	    ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1,
    233 	    TCPTV_MIN, TCPTV_REXMTMAX);
    234 	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
    235 	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
    236 	inp->inp_ip.ip_ttl = ip_defttl;
    237 	inp->inp_ppcb = (caddr_t)tp;
    238 	return (tp);
    239 }
    240 
    241 /*
    242  * Drop a TCP connection, reporting
    243  * the specified error.  If connection is synchronized,
    244  * then send a RST to peer.
    245  */
    246 struct tcpcb *
    247 tcp_drop(tp, errno)
    248 	register struct tcpcb *tp;
    249 	int errno;
    250 {
    251 	struct socket *so = tp->t_inpcb->inp_socket;
    252 
    253 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
    254 		tp->t_state = TCPS_CLOSED;
    255 		(void) tcp_output(tp);
    256 		tcpstat.tcps_drops++;
    257 	} else
    258 		tcpstat.tcps_conndrops++;
    259 	if (errno == ETIMEDOUT && tp->t_softerror)
    260 		errno = tp->t_softerror;
    261 	so->so_error = errno;
    262 	return (tcp_close(tp));
    263 }
    264 
    265 /*
    266  * Close a TCP control block:
    267  *	discard all space held by the tcp
    268  *	discard internet protocol block
    269  *	wake up any sleepers
    270  */
    271 struct tcpcb *
    272 tcp_close(tp)
    273 	register struct tcpcb *tp;
    274 {
    275 	register struct tcpiphdr *t;
    276 	struct inpcb *inp = tp->t_inpcb;
    277 	struct socket *so = inp->inp_socket;
    278 	register struct mbuf *m;
    279 #ifdef RTV_RTT
    280 	register struct rtentry *rt;
    281 
    282 	/*
    283 	 * If we sent enough data to get some meaningful characteristics,
    284 	 * save them in the routing entry.  'Enough' is arbitrarily
    285 	 * defined as the sendpipesize (default 4K) * 16.  This would
    286 	 * give us 16 rtt samples assuming we only get one sample per
    287 	 * window (the usual case on a long haul net).  16 samples is
    288 	 * enough for the srtt filter to converge to within 5% of the correct
    289 	 * value; fewer samples and we could save a very bogus rtt.
    290 	 *
    291 	 * Don't update the default route's characteristics and don't
    292 	 * update anything that the user "locked".
    293 	 */
    294 	if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
    295 	    (rt = inp->inp_route.ro_rt) &&
    296 	    ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr != INADDR_ANY) {
    297 		register u_long i;
    298 
    299 		if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
    300 			i = tp->t_srtt *
    301 			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
    302 			if (rt->rt_rmx.rmx_rtt && i)
    303 				/*
    304 				 * filter this update to half the old & half
    305 				 * the new values, converting scale.
    306 				 * See route.h and tcp_var.h for a
    307 				 * description of the scaling constants.
    308 				 */
    309 				rt->rt_rmx.rmx_rtt =
    310 				    (rt->rt_rmx.rmx_rtt + i) / 2;
    311 			else
    312 				rt->rt_rmx.rmx_rtt = i;
    313 		}
    314 		if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
    315 			i = tp->t_rttvar *
    316 			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
    317 			if (rt->rt_rmx.rmx_rttvar && i)
    318 				rt->rt_rmx.rmx_rttvar =
    319 				    (rt->rt_rmx.rmx_rttvar + i) / 2;
    320 			else
    321 				rt->rt_rmx.rmx_rttvar = i;
    322 		}
    323 		/*
    324 		 * update the pipelimit (ssthresh) if it has been updated
    325 		 * already or if a pipesize was specified & the threshhold
    326 		 * got below half the pipesize.  I.e., wait for bad news
    327 		 * before we start updating, then update on both good
    328 		 * and bad news.
    329 		 */
    330 		if ((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
    331 		    (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh ||
    332 		    i < (rt->rt_rmx.rmx_sendpipe / 2)) {
    333 			/*
    334 			 * convert the limit from user data bytes to
    335 			 * packets then to packet data bytes.
    336 			 */
    337 			i = (i + tp->t_maxseg / 2) / tp->t_maxseg;
    338 			if (i < 2)
    339 				i = 2;
    340 			i *= (u_long)(tp->t_maxseg + sizeof (struct tcpiphdr));
    341 			if (rt->rt_rmx.rmx_ssthresh)
    342 				rt->rt_rmx.rmx_ssthresh =
    343 				    (rt->rt_rmx.rmx_ssthresh + i) / 2;
    344 			else
    345 				rt->rt_rmx.rmx_ssthresh = i;
    346 		}
    347 	}
    348 #endif /* RTV_RTT */
    349 	/* free the reassembly queue, if any */
    350 	t = tp->seg_next;
    351 	while (t != (struct tcpiphdr *)tp) {
    352 		t = (struct tcpiphdr *)t->ti_next;
    353 		m = REASS_MBUF((struct tcpiphdr *)t->ti_prev);
    354 		remque(t->ti_prev);
    355 		m_freem(m);
    356 	}
    357 	if (tp->t_template)
    358 		(void) m_free(dtom(tp->t_template));
    359 	free(tp, M_PCB);
    360 	inp->inp_ppcb = 0;
    361 	soisdisconnected(so);
    362 	/* clobber input pcb cache if we're closing the cached connection */
    363 	if (inp == tcp_last_inpcb)
    364 		tcp_last_inpcb = &tcb;
    365 	in_pcbdetach(inp);
    366 	tcpstat.tcps_closed++;
    367 	return ((struct tcpcb *)0);
    368 }
    369 
    370 void
    371 tcp_drain()
    372 {
    373 
    374 }
    375 
    376 /*
    377  * Notify a tcp user of an asynchronous error;
    378  * store error as soft error, but wake up user
    379  * (for now, won't do anything until can select for soft error).
    380  */
    381 void
    382 tcp_notify(inp, error)
    383 	struct inpcb *inp;
    384 	int error;
    385 {
    386 	register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
    387 	register struct socket *so = inp->inp_socket;
    388 
    389 	/*
    390 	 * Ignore some errors if we are hooked up.
    391 	 * If connection hasn't completed, has retransmitted several times,
    392 	 * and receives a second error, give up now.  This is better
    393 	 * than waiting a long time to establish a connection that
    394 	 * can never complete.
    395 	 */
    396 	if (tp->t_state == TCPS_ESTABLISHED &&
    397 	     (error == EHOSTUNREACH || error == ENETUNREACH ||
    398 	      error == EHOSTDOWN)) {
    399 		return;
    400 	} else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
    401 	    tp->t_rxtshift > 3 && tp->t_softerror)
    402 		so->so_error = error;
    403 	else
    404 		tp->t_softerror = error;
    405 	wakeup((caddr_t) &so->so_timeo);
    406 	sorwakeup(so);
    407 	sowwakeup(so);
    408 }
    409 
    410 void
    411 tcp_ctlinput(cmd, sa, ip)
    412 	int cmd;
    413 	struct sockaddr *sa;
    414 	register struct ip *ip;
    415 {
    416 	register struct tcphdr *th;
    417 	extern struct in_addr zeroin_addr;
    418 	extern u_char inetctlerrmap[];
    419 	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
    420 
    421 	if (cmd == PRC_QUENCH)
    422 		notify = tcp_quench;
    423 	else if (!PRC_IS_REDIRECT(cmd) &&
    424 		 ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0))
    425 		return;
    426 	if (ip) {
    427 		th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
    428 		in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport,
    429 			cmd, notify);
    430 	} else
    431 		in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify);
    432 }
    433 
    434 /*
    435  * When a source quench is received, close congestion window
    436  * to one segment.  We will gradually open it again as we proceed.
    437  */
    438 void
    439 tcp_quench(inp, errno)
    440 	struct inpcb *inp;
    441 	int errno;
    442 {
    443 	struct tcpcb *tp = intotcpcb(inp);
    444 
    445 	if (tp)
    446 		tp->snd_cwnd = tp->t_maxseg;
    447 }
    448