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