fix per Jeffrey Jongeward
[unix-history] / usr / src / sys / netinet / tcp_subr.c
... / ...
CommitLineData
1/* tcp_subr.c 4.26 82/04/30 */
2
3#include "../h/param.h"
4#include "../h/systm.h"
5#include "../h/mbuf.h"
6#include "../h/socket.h"
7#include "../h/socketvar.h"
8#include "../h/protosw.h"
9#include "../net/in.h"
10#include "../net/route.h"
11#include "../net/in_pcb.h"
12#include "../net/in_systm.h"
13#include "../net/if.h"
14#include "../net/ip.h"
15#include "../net/ip_var.h"
16#include "../net/ip_icmp.h"
17#include "../net/tcp.h"
18#include "../net/tcp_fsm.h"
19#include "../net/tcp_seq.h"
20#include "../net/tcp_timer.h"
21#include "../net/tcp_var.h"
22#include "../net/tcpip.h"
23#include "../errno.h"
24
25/*
26 * Tcp initialization
27 */
28tcp_init()
29{
30
31COUNT(TCP_INIT);
32 tcp_iss = 1; /* wrong */
33 tcb.inp_next = tcb.inp_prev = &tcb;
34 tcp_alpha = TCP_ALPHA;
35 tcp_beta = TCP_BETA;
36}
37
38/*
39 * Create template to be used to send tcp packets on a connection.
40 * Call after host entry created, allocates an mbuf and fills
41 * in a skeletal tcp/ip header, minimizing the amount of work
42 * necessary when the connection is used.
43 */
44struct tcpiphdr *
45tcp_template(tp)
46 struct tcpcb *tp;
47{
48 register struct inpcb *inp = tp->t_inpcb;
49 register struct mbuf *m;
50 register struct tcpiphdr *n;
51
52COUNT(TCP_TEMPLATE);
53 m = m_get(M_WAIT);
54 if (m == 0)
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 */
91tcp_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
101COUNT(TCP_RESPOND);
102 if (tp) {
103 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
104 ro = &tp->t_inpcb->inp_route;
105 }
106 if (flags == 0) {
107 m = m_get(M_DONTWAIT);
108 if (m == 0)
109 return;
110 m->m_off = MMINOFF;
111 m->m_len = sizeof (struct tcpiphdr) + 1;
112 *mtod(m, struct tcpiphdr *) = *ti;
113 ti = mtod(m, struct tcpiphdr *);
114 flags = TH_ACK;
115 tlen = 1;
116 } else {
117 m = dtom(ti);
118 m_freem(m->m_next);
119 m->m_next = 0;
120 m->m_off = (int)ti - (int)m;
121 m->m_len = sizeof (struct tcpiphdr);
122#define xchg(a,b,type) { type t; t=a; a=b; b=t; }
123 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
124 xchg(ti->ti_dport, ti->ti_sport, u_short);
125#undef xchg
126 tlen = 0;
127 }
128 ti->ti_next = ti->ti_prev = 0;
129 ti->ti_x1 = 0;
130 ti->ti_len = sizeof (struct tcphdr) + tlen;
131 ti->ti_seq = seq;
132 ti->ti_ack = ack;
133#if vax
134 ti->ti_len = htons((u_short)ti->ti_len);
135 ti->ti_seq = htonl(ti->ti_seq);
136 ti->ti_ack = htonl(ti->ti_ack);
137#endif
138 ti->ti_x2 = 0;
139 ti->ti_off = sizeof (struct tcphdr) >> 2;
140 ti->ti_flags = flags;
141 ti->ti_win = win;
142#if vax
143 ti->ti_win = htons(ti->ti_win);
144#endif
145 ti->ti_urp = 0;
146 ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen);
147 ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen;
148 ((struct ip *)ti)->ip_ttl = TCP_TTL;
149 (void) ip_output(m, (struct mbuf *)0, ro, 0);
150}
151
152/*
153 * Create a new TCP control block, making an
154 * empty reassembly queue and hooking it to the argument
155 * protocol control block.
156 */
157struct tcpcb *
158tcp_newtcpcb(inp)
159 struct inpcb *inp;
160{
161 struct mbuf *m = m_getclr(M_DONTWAIT);
162 register struct tcpcb *tp;
163COUNT(TCP_NEWTCPCB);
164
165 if (m == 0)
166 return (0);
167 tp = mtod(m, struct tcpcb *);
168 tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
169 tp->t_maxseg = 576; /* satisfy the rest of the world */
170 tp->t_flags = 0; /* sends options! */
171 tp->t_inpcb = inp;
172 inp->inp_ppcb = (caddr_t)tp;
173 return (tp);
174}
175
176/*
177 * Drop a TCP connection, reporting
178 * the specified error. If connection is synchronized,
179 * then send a RST to peer.
180 */
181tcp_drop(tp, errno)
182 struct tcpcb *tp;
183 int errno;
184{
185 struct socket *so = tp->t_inpcb->inp_socket;
186
187COUNT(TCP_DROP);
188 if (TCPS_HAVERCVDSYN(tp->t_state)) {
189 tp->t_state = TCPS_CLOSED;
190 tcp_output(tp);
191 }
192 so->so_error = errno;
193 tcp_close(tp);
194}
195
196tcp_abort(inp)
197 struct inpcb *inp;
198{
199 tcp_close(inp->inp_ppcb);
200}
201
202/*
203 * Close a TCP control block:
204 * discard all space held by the tcp
205 * discard internet protocol block
206 * wake up any sleepers
207 */
208tcp_close(tp)
209 register struct tcpcb *tp;
210{
211 register struct tcpiphdr *t;
212 struct inpcb *inp = tp->t_inpcb;
213 struct socket *so = inp->inp_socket;
214
215COUNT(TCP_CLOSE);
216 t = tp->seg_next;
217 for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next)
218 m_freem(dtom(t));
219 if (tp->t_template)
220 (void) m_free(dtom(tp->t_template));
221 if (tp->t_tcpopt)
222 (void) m_free(dtom(tp->t_tcpopt));
223 if (tp->t_ipopt)
224 (void) m_free(dtom(tp->t_ipopt));
225 (void) m_free(dtom(tp));
226 inp->inp_ppcb = 0;
227 soisdisconnected(so);
228 in_pcbdetach(inp);
229}
230
231tcp_drain()
232{
233
234COUNT(TCP_DRAIN);
235}
236
237tcp_ctlinput(cmd, arg)
238 int cmd;
239 caddr_t arg;
240{
241 struct in_addr *sin;
242 extern u_char inetctlerrmap[];
243COUNT(TCP_CTLINPUT);
244
245 if (cmd < 0 || cmd > PRC_NCMDS)
246 return;
247 switch (cmd) {
248
249 case PRC_ROUTEDEAD:
250 break;
251
252 case PRC_QUENCH:
253 break;
254
255 /* these are handled by ip */
256 case PRC_IFDOWN:
257 case PRC_HOSTDEAD:
258 case PRC_HOSTUNREACH:
259 break;
260
261 default:
262 sin = &((struct icmp *)arg)->icmp_ip.ip_dst;
263 in_pcbnotify(&tcb, sin, inetctlerrmap[cmd], tcp_abort);
264 }
265}