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