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