xref: /csrg-svn/sys/netinet/tcp_subr.c (revision 17317)
1 /*	tcp_subr.c	6.3	84/11/01	*/
2 
3 #include "param.h"
4 #include "systm.h"
5 #include "mbuf.h"
6 #include "socket.h"
7 #include "socketvar.h"
8 #include "protosw.h"
9 #include "errno.h"
10 
11 #include "../net/route.h"
12 #include "../net/if.h"
13 
14 #include "in.h"
15 #include "in_pcb.h"
16 #include "in_systm.h"
17 #include "ip.h"
18 #include "ip_var.h"
19 #include "ip_icmp.h"
20 #include "tcp.h"
21 #include "tcp_fsm.h"
22 #include "tcp_seq.h"
23 #include "tcp_timer.h"
24 #include "tcp_var.h"
25 #include "tcpip.h"
26 
27 /*
28  * Tcp initialization
29  */
30 tcp_init()
31 {
32 
33 	tcp_iss = 1;		/* wrong */
34 	tcb.inp_next = tcb.inp_prev = &tcb;
35 	tcp_alpha = TCP_ALPHA;
36 	tcp_beta = TCP_BETA;
37 }
38 
39 /*
40  * Create template to be used to send tcp packets on a connection.
41  * Call after host entry created, allocates an mbuf and fills
42  * in a skeletal tcp/ip header, minimizing the amount of work
43  * necessary when the connection is used.
44  */
45 struct tcpiphdr *
46 tcp_template(tp)
47 	struct tcpcb *tp;
48 {
49 	register struct inpcb *inp = tp->t_inpcb;
50 	register struct mbuf *m;
51 	register struct tcpiphdr *n;
52 
53 	m = m_get(M_WAIT, MT_HEADER);
54 	if (m == NULL)
55 		return (0);
56 	m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
57 	m->m_len = sizeof (struct tcpiphdr);
58 	n = mtod(m, struct tcpiphdr *);
59 	n->ti_next = n->ti_prev = 0;
60 	n->ti_x1 = 0;
61 	n->ti_pr = IPPROTO_TCP;
62 	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
63 	n->ti_src = inp->inp_laddr;
64 	n->ti_dst = inp->inp_faddr;
65 	n->ti_sport = inp->inp_lport;
66 	n->ti_dport = inp->inp_fport;
67 	n->ti_seq = 0;
68 	n->ti_ack = 0;
69 	n->ti_x2 = 0;
70 	n->ti_off = 5;
71 	n->ti_flags = 0;
72 	n->ti_win = 0;
73 	n->ti_sum = 0;
74 	n->ti_urp = 0;
75 	return (n);
76 }
77 
78 /*
79  * Send a single message to the TCP at address specified by
80  * the given TCP/IP header.  If flags==0, then we make a copy
81  * of the tcpiphdr at ti and send directly to the addressed host.
82  * This is used to force keep alive messages out using the TCP
83  * template for a connection tp->t_template.  If flags are given
84  * then we send a message back to the TCP which originated the
85  * segment ti, and discard the mbuf containing it and any other
86  * attached mbufs.
87  *
88  * In any case the ack and sequence number of the transmitted
89  * segment are as specified by the parameters.
90  */
91 tcp_respond(tp, ti, ack, seq, flags)
92 	struct tcpcb *tp;
93 	register struct tcpiphdr *ti;
94 	tcp_seq ack, seq;
95 	int flags;
96 {
97 	struct mbuf *m;
98 	int win = 0, tlen;
99 	struct route *ro = 0;
100 
101 	if (tp) {
102 		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
103 		ro = &tp->t_inpcb->inp_route;
104 	}
105 	if (flags == 0) {
106 		m = m_get(M_DONTWAIT, MT_HEADER);
107 		if (m == NULL)
108 			return;
109 		m->m_len = sizeof (struct tcpiphdr) + 1;
110 		*mtod(m, struct tcpiphdr *) = *ti;
111 		ti = mtod(m, struct tcpiphdr *);
112 		flags = TH_ACK;
113 		tlen = 1;
114 	} else {
115 		m = dtom(ti);
116 		m_freem(m->m_next);
117 		m->m_next = 0;
118 		m->m_off = (int)ti - (int)m;
119 		m->m_len = sizeof (struct tcpiphdr);
120 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
121 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
122 		xchg(ti->ti_dport, ti->ti_sport, u_short);
123 #undef xchg
124 		tlen = 0;
125 	}
126 	ti->ti_next = ti->ti_prev = 0;
127 	ti->ti_x1 = 0;
128 	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
129 	ti->ti_seq = htonl(seq);
130 	ti->ti_ack = htonl(ack);
131 	ti->ti_x2 = 0;
132 	ti->ti_off = sizeof (struct tcphdr) >> 2;
133 	ti->ti_flags = flags;
134 	ti->ti_win = htons((u_short)win);
135 	ti->ti_urp = 0;
136 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen);
137 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen;
138 	((struct ip *)ti)->ip_ttl = TCP_TTL;
139 	(void) ip_output(m, (struct mbuf *)0, ro, 0);
140 }
141 
142 /*
143  * Create a new TCP control block, making an
144  * empty reassembly queue and hooking it to the argument
145  * protocol control block.
146  */
147 struct tcpcb *
148 tcp_newtcpcb(inp)
149 	struct inpcb *inp;
150 {
151 	struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB);
152 	register struct tcpcb *tp;
153 
154 	if (m == NULL)
155 		return ((struct tcpcb *)0);
156 	tp = mtod(m, struct tcpcb *);
157 	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
158 	tp->t_maxseg = TCP_MSS;
159 	tp->t_flags = 0;		/* sends options! */
160 	tp->t_inpcb = inp;
161 	tp->t_srtt = TCPTV_SRTTBASE;
162 	inp->inp_ppcb = (caddr_t)tp;
163 	return (tp);
164 }
165 
166 /*
167  * Drop a TCP connection, reporting
168  * the specified error.  If connection is synchronized,
169  * then send a RST to peer.
170  */
171 struct tcpcb *
172 tcp_drop(tp, errno)
173 	register struct tcpcb *tp;
174 	int errno;
175 {
176 	struct socket *so = tp->t_inpcb->inp_socket;
177 
178 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
179 		tp->t_state = TCPS_CLOSED;
180 		(void) tcp_output(tp);
181 	}
182 	so->so_error = errno;
183 	return (tcp_close(tp));
184 }
185 
186 tcp_abort(inp)
187 	struct inpcb *inp;
188 {
189 
190 	(void) tcp_close((struct tcpcb *)inp->inp_ppcb);
191 }
192 
193 /*
194  * Close a TCP control block:
195  *	discard all space held by the tcp
196  *	discard internet protocol block
197  *	wake up any sleepers
198  */
199 struct tcpcb *
200 tcp_close(tp)
201 	register struct tcpcb *tp;
202 {
203 	register struct tcpiphdr *t;
204 	struct inpcb *inp = tp->t_inpcb;
205 	struct socket *so = inp->inp_socket;
206 	register struct mbuf *m;
207 
208 	t = tp->seg_next;
209 	while (t != (struct tcpiphdr *)tp) {
210 		t = (struct tcpiphdr *)t->ti_next;
211 		m = dtom(t->ti_prev);
212 		remque(t->ti_prev);
213 		m_freem(m);
214 	}
215 	if (tp->t_template)
216 		(void) m_free(dtom(tp->t_template));
217 	if (tp->t_tcpopt)
218 		(void) m_free(dtom(tp->t_tcpopt));
219 	if (tp->t_ipopt)
220 		(void) m_free(dtom(tp->t_ipopt));
221 	(void) m_free(dtom(tp));
222 	inp->inp_ppcb = 0;
223 	soisdisconnected(so);
224 	in_pcbdetach(inp);
225 	return ((struct tcpcb *)0);
226 }
227 
228 tcp_drain()
229 {
230 
231 }
232 
233 tcp_ctlinput(cmd, arg)
234 	int cmd;
235 	caddr_t arg;
236 {
237 	struct in_addr *sin;
238 	extern u_char inetctlerrmap[];
239 
240 	if (cmd < 0 || cmd > PRC_NCMDS)
241 		return;
242 	switch (cmd) {
243 
244 	case PRC_ROUTEDEAD:
245 		break;
246 
247 	case PRC_QUENCH:
248 		break;
249 
250 	/* these are handled by ip */
251 	case PRC_IFDOWN:
252 	case PRC_HOSTDEAD:
253 	case PRC_HOSTUNREACH:
254 		break;
255 
256 	default:
257 		sin = &((struct icmp *)arg)->icmp_ip.ip_dst;
258 		in_pcbnotify(&tcb, sin, (int)inetctlerrmap[cmd], tcp_abort);
259 	}
260 }
261