Commit | Line | Data |
---|---|---|
8ae0e4b4 | 1 | /* |
0880b18e | 2 | * Copyright (c) 1982, 1986 Regents of the University of California. |
8ae0e4b4 KM |
3 | * All rights reserved. The Berkeley software License Agreement |
4 | * specifies the terms and conditions for redistribution. | |
5 | * | |
9f5105e3 | 6 | * @(#)tcp_subr.c 7.11 (Berkeley) %G% |
8ae0e4b4 | 7 | */ |
ecaa4e6f | 8 | |
20666ad3 JB |
9 | #include "param.h" |
10 | #include "systm.h" | |
11 | #include "mbuf.h" | |
12 | #include "socket.h" | |
13 | #include "socketvar.h" | |
14 | #include "protosw.h" | |
15 | #include "errno.h" | |
f4d55810 | 16 | |
c124e997 | 17 | #include "../net/route.h" |
f4d55810 SL |
18 | #include "../net/if.h" |
19 | ||
20666ad3 JB |
20 | #include "in.h" |
21 | #include "in_pcb.h" | |
22 | #include "in_systm.h" | |
23 | #include "ip.h" | |
24 | #include "ip_var.h" | |
25 | #include "ip_icmp.h" | |
26 | #include "tcp.h" | |
27 | #include "tcp_fsm.h" | |
28 | #include "tcp_seq.h" | |
29 | #include "tcp_timer.h" | |
30 | #include "tcp_var.h" | |
31 | #include "tcpip.h" | |
ecaa4e6f | 32 | |
10604dba | 33 | int tcp_ttl = TCP_TTL; |
10604dba | 34 | |
ecaa4e6f BJ |
35 | /* |
36 | * Tcp initialization | |
37 | */ | |
38 | tcp_init() | |
39 | { | |
40 | ||
41 | tcp_iss = 1; /* wrong */ | |
42 | tcb.inp_next = tcb.inp_prev = &tcb; | |
43 | } | |
44 | ||
45 | /* | |
46 | * Create template to be used to send tcp packets on a connection. | |
47 | * Call after host entry created, allocates an mbuf and fills | |
48 | * in a skeletal tcp/ip header, minimizing the amount of work | |
49 | * necessary when the connection is used. | |
50 | */ | |
51 | struct tcpiphdr * | |
52 | tcp_template(tp) | |
53 | struct tcpcb *tp; | |
54 | { | |
55 | register struct inpcb *inp = tp->t_inpcb; | |
56 | register struct mbuf *m; | |
57 | register struct tcpiphdr *n; | |
58 | ||
ece01391 | 59 | if ((n = tp->t_template) == 0) { |
9f5105e3 | 60 | m = m_get(M_DONTWAIT, MT_HEADER); |
ece01391 MK |
61 | if (m == NULL) |
62 | return (0); | |
63 | m->m_off = MMAXOFF - sizeof (struct tcpiphdr); | |
64 | m->m_len = sizeof (struct tcpiphdr); | |
65 | n = mtod(m, struct tcpiphdr *); | |
66 | } | |
ecaa4e6f BJ |
67 | n->ti_next = n->ti_prev = 0; |
68 | n->ti_x1 = 0; | |
69 | n->ti_pr = IPPROTO_TCP; | |
70 | n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); | |
71 | n->ti_src = inp->inp_laddr; | |
72 | n->ti_dst = inp->inp_faddr; | |
73 | n->ti_sport = inp->inp_lport; | |
74 | n->ti_dport = inp->inp_fport; | |
75 | n->ti_seq = 0; | |
0974b45c | 76 | n->ti_ack = 0; |
ecaa4e6f BJ |
77 | n->ti_x2 = 0; |
78 | n->ti_off = 5; | |
79 | n->ti_flags = 0; | |
80 | n->ti_win = 0; | |
81 | n->ti_sum = 0; | |
82 | n->ti_urp = 0; | |
83 | return (n); | |
84 | } | |
85 | ||
86 | /* | |
405c9168 BJ |
87 | * Send a single message to the TCP at address specified by |
88 | * the given TCP/IP header. If flags==0, then we make a copy | |
89 | * of the tcpiphdr at ti and send directly to the addressed host. | |
90 | * This is used to force keep alive messages out using the TCP | |
91 | * template for a connection tp->t_template. If flags are given | |
92 | * then we send a message back to the TCP which originated the | |
93 | * segment ti, and discard the mbuf containing it and any other | |
94 | * attached mbufs. | |
95 | * | |
96 | * In any case the ack and sequence number of the transmitted | |
97 | * segment are as specified by the parameters. | |
ecaa4e6f | 98 | */ |
8e65fd66 BJ |
99 | tcp_respond(tp, ti, ack, seq, flags) |
100 | struct tcpcb *tp; | |
ecaa4e6f | 101 | register struct tcpiphdr *ti; |
0974b45c | 102 | tcp_seq ack, seq; |
ecaa4e6f BJ |
103 | int flags; |
104 | { | |
8b6ad229 | 105 | register struct mbuf *m; |
1e977657 | 106 | int win = 0, tlen; |
c124e997 | 107 | struct route *ro = 0; |
ecaa4e6f | 108 | |
c124e997 | 109 | if (tp) { |
8e65fd66 | 110 | win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); |
c124e997 SL |
111 | ro = &tp->t_inpcb->inp_route; |
112 | } | |
405c9168 | 113 | if (flags == 0) { |
cce93e4b | 114 | m = m_get(M_DONTWAIT, MT_HEADER); |
5cdc4d65 | 115 | if (m == NULL) |
405c9168 | 116 | return; |
eeef4ac3 MK |
117 | #ifdef TCP_COMPAT_42 |
118 | tlen = 1; | |
119 | #else | |
120 | tlen = 0; | |
121 | #endif | |
8b6ad229 | 122 | m->m_len = sizeof (struct tcpiphdr) + tlen; |
405c9168 BJ |
123 | *mtod(m, struct tcpiphdr *) = *ti; |
124 | ti = mtod(m, struct tcpiphdr *); | |
125 | flags = TH_ACK; | |
126 | } else { | |
4aed14e3 | 127 | m = dtom(ti); |
405c9168 BJ |
128 | m_freem(m->m_next); |
129 | m->m_next = 0; | |
1acff8ec | 130 | m->m_off = (int)ti - (int)m; |
8b6ad229 | 131 | tlen = 0; |
405c9168 | 132 | m->m_len = sizeof (struct tcpiphdr); |
0974b45c | 133 | #define xchg(a,b,type) { type t; t=a; a=b; b=t; } |
405c9168 BJ |
134 | xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); |
135 | xchg(ti->ti_dport, ti->ti_sport, u_short); | |
ecaa4e6f | 136 | #undef xchg |
405c9168 | 137 | } |
0974b45c BJ |
138 | ti->ti_next = ti->ti_prev = 0; |
139 | ti->ti_x1 = 0; | |
af8f6a21 | 140 | ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen)); |
2c48b3f8 BJ |
141 | ti->ti_seq = htonl(seq); |
142 | ti->ti_ack = htonl(ack); | |
0974b45c BJ |
143 | ti->ti_x2 = 0; |
144 | ti->ti_off = sizeof (struct tcphdr) >> 2; | |
ecaa4e6f | 145 | ti->ti_flags = flags; |
af8f6a21 | 146 | ti->ti_win = htons((u_short)win); |
8e65fd66 | 147 | ti->ti_urp = 0; |
f3cdd721 | 148 | ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen); |
1e977657 | 149 | ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen; |
10604dba | 150 | ((struct ip *)ti)->ip_ttl = tcp_ttl; |
c124e997 | 151 | (void) ip_output(m, (struct mbuf *)0, ro, 0); |
ecaa4e6f | 152 | } |
a6503abf | 153 | |
0974b45c BJ |
154 | /* |
155 | * Create a new TCP control block, making an | |
156 | * empty reassembly queue and hooking it to the argument | |
157 | * protocol control block. | |
158 | */ | |
a6503abf BJ |
159 | struct tcpcb * |
160 | tcp_newtcpcb(inp) | |
161 | struct inpcb *inp; | |
162 | { | |
cce93e4b | 163 | struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB); |
a6503abf | 164 | register struct tcpcb *tp; |
a6503abf | 165 | |
5cdc4d65 SL |
166 | if (m == NULL) |
167 | return ((struct tcpcb *)0); | |
a6503abf | 168 | tp = mtod(m, struct tcpcb *); |
a6503abf | 169 | tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; |
247c6a81 | 170 | tp->t_maxseg = TCP_MSS; |
3e60e1e6 | 171 | tp->t_flags = 0; /* sends options! */ |
a6503abf | 172 | tp->t_inpcb = inp; |
7cc62c26 | 173 | /* |
5ca0b868 MK |
174 | * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no |
175 | * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives | |
176 | * reasonable initial retransmit time. | |
7cc62c26 | 177 | */ |
5ca0b868 MK |
178 | tp->t_srtt = TCPTV_SRTTBASE; |
179 | tp->t_rttvar = TCPTV_SRTTDFLT << 2; | |
05586739 | 180 | tp->snd_cwnd = sbspace(&inp->inp_socket->so_snd); |
9f5105e3 | 181 | tp->snd_ssthresh = 65535; /* XXX */ |
a6503abf BJ |
182 | inp->inp_ppcb = (caddr_t)tp; |
183 | return (tp); | |
184 | } | |
185 | ||
0974b45c BJ |
186 | /* |
187 | * Drop a TCP connection, reporting | |
188 | * the specified error. If connection is synchronized, | |
189 | * then send a RST to peer. | |
190 | */ | |
0e3936fa | 191 | struct tcpcb * |
a6503abf | 192 | tcp_drop(tp, errno) |
0e3936fa | 193 | register struct tcpcb *tp; |
a6503abf BJ |
194 | int errno; |
195 | { | |
196 | struct socket *so = tp->t_inpcb->inp_socket; | |
197 | ||
d3504cc0 | 198 | if (TCPS_HAVERCVDSYN(tp->t_state)) { |
a6503abf | 199 | tp->t_state = TCPS_CLOSED; |
39d536e6 | 200 | (void) tcp_output(tp); |
35f3fc10 MK |
201 | tcpstat.tcps_drops++; |
202 | } else | |
203 | tcpstat.tcps_conndrops++; | |
a6503abf | 204 | so->so_error = errno; |
0e3936fa | 205 | return (tcp_close(tp)); |
a6503abf BJ |
206 | } |
207 | ||
0974b45c BJ |
208 | /* |
209 | * Close a TCP control block: | |
210 | * discard all space held by the tcp | |
211 | * discard internet protocol block | |
212 | * wake up any sleepers | |
213 | */ | |
0e3936fa | 214 | struct tcpcb * |
a6503abf BJ |
215 | tcp_close(tp) |
216 | register struct tcpcb *tp; | |
217 | { | |
218 | register struct tcpiphdr *t; | |
364801f5 BJ |
219 | struct inpcb *inp = tp->t_inpcb; |
220 | struct socket *so = inp->inp_socket; | |
13e2480b | 221 | register struct mbuf *m; |
a6503abf | 222 | |
a6503abf | 223 | t = tp->seg_next; |
13e2480b SL |
224 | while (t != (struct tcpiphdr *)tp) { |
225 | t = (struct tcpiphdr *)t->ti_next; | |
226 | m = dtom(t->ti_prev); | |
227 | remque(t->ti_prev); | |
228 | m_freem(m); | |
229 | } | |
0974b45c | 230 | if (tp->t_template) |
a6503abf | 231 | (void) m_free(dtom(tp->t_template)); |
a6503abf | 232 | (void) m_free(dtom(tp)); |
364801f5 | 233 | inp->inp_ppcb = 0; |
4aed14e3 | 234 | soisdisconnected(so); |
86676257 | 235 | in_pcbdetach(inp); |
35f3fc10 | 236 | tcpstat.tcps_closed++; |
0e3936fa | 237 | return ((struct tcpcb *)0); |
a6503abf BJ |
238 | } |
239 | ||
a6503abf BJ |
240 | tcp_drain() |
241 | { | |
a6503abf | 242 | |
a6503abf BJ |
243 | } |
244 | ||
be841dc3 MK |
245 | /* |
246 | * Notify a tcp user of an asynchronous error; | |
247 | * just wake up so that he can collect error status. | |
248 | */ | |
249 | tcp_notify(inp) | |
250 | register struct inpcb *inp; | |
251 | { | |
252 | ||
253 | wakeup((caddr_t) &inp->inp_socket->so_timeo); | |
254 | sorwakeup(inp->inp_socket); | |
255 | sowwakeup(inp->inp_socket); | |
256 | } | |
7c626d4d | 257 | tcp_ctlinput(cmd, sa) |
72e4f44e | 258 | int cmd; |
7c626d4d | 259 | struct sockaddr *sa; |
a6503abf | 260 | { |
39674d5f | 261 | extern u_char inetctlerrmap[]; |
7c626d4d | 262 | struct sockaddr_in *sin; |
62a291b2 | 263 | int tcp_quench(), in_rtchange(); |
39674d5f | 264 | |
7c626d4d MK |
265 | if ((unsigned)cmd > PRC_NCMDS) |
266 | return; | |
267 | if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK) | |
268 | return; | |
269 | sin = (struct sockaddr_in *)sa; | |
270 | if (sin->sin_addr.s_addr == INADDR_ANY) | |
39674d5f | 271 | return; |
39674d5f | 272 | |
7c626d4d | 273 | switch (cmd) { |
39674d5f SL |
274 | |
275 | case PRC_QUENCH: | |
7c626d4d | 276 | in_pcbnotify(&tcb, &sin->sin_addr, 0, tcp_quench); |
62a291b2 MK |
277 | break; |
278 | ||
7c626d4d | 279 | case PRC_ROUTEDEAD: |
62a291b2 MK |
280 | case PRC_REDIRECT_NET: |
281 | case PRC_REDIRECT_HOST: | |
7c626d4d MK |
282 | case PRC_REDIRECT_TOSNET: |
283 | case PRC_REDIRECT_TOSHOST: | |
284 | in_pcbnotify(&tcb, &sin->sin_addr, 0, in_rtchange); | |
39674d5f SL |
285 | break; |
286 | ||
39674d5f | 287 | default: |
62a291b2 MK |
288 | if (inetctlerrmap[cmd] == 0) |
289 | return; /* XXX */ | |
7c626d4d | 290 | in_pcbnotify(&tcb, &sin->sin_addr, (int)inetctlerrmap[cmd], |
be841dc3 | 291 | tcp_notify); |
39674d5f | 292 | } |
a6503abf | 293 | } |
05586739 MK |
294 | |
295 | /* | |
296 | * When a source quench is received, close congestion window | |
2e5a76f2 | 297 | * to one segment. We will gradually open it again as we proceed. |
05586739 MK |
298 | */ |
299 | tcp_quench(inp) | |
300 | struct inpcb *inp; | |
301 | { | |
302 | struct tcpcb *tp = intotcpcb(inp); | |
303 | ||
7c626d4d | 304 | if (tp) |
2e5a76f2 | 305 | tp->snd_cwnd = tp->t_maxseg; |
05586739 | 306 | } |