Commit | Line | Data |
---|---|---|
8ae0e4b4 KM |
1 | /* |
2 | * Copyright (c) 1982 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | * | |
6 | * @(#)tcp_usrreq.c 6.6 (Berkeley) %G% | |
7 | */ | |
72f24d7d | 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" | |
16 | #include "stat.h" | |
6e7edb25 BJ |
17 | |
18 | #include "../net/if.h" | |
c124e997 | 19 | #include "../net/route.h" |
f4d55810 | 20 | |
20666ad3 JB |
21 | #include "in.h" |
22 | #include "in_pcb.h" | |
23 | #include "in_systm.h" | |
24 | #include "ip.h" | |
25 | #include "ip_var.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" | |
32 | #include "tcp_debug.h" | |
eee3ab16 | 33 | |
290e0b0a BJ |
34 | /* |
35 | * TCP protocol interface to socket abstraction. | |
36 | */ | |
37 | extern char *tcpstates[]; | |
4ad99bae | 38 | struct tcpcb *tcp_newtcpcb(); |
ab85b059 | 39 | int tcpsenderrors; |
290e0b0a | 40 | |
9c5022e3 | 41 | /* |
290e0b0a | 42 | * Process a TCP user request for TCP tb. If this is a send request |
9c5022e3 BJ |
43 | * then m is the mbuf chain of send data. If this is a timer expiration |
44 | * (called from the software clock routine), then timertype tells which timer. | |
45 | */ | |
a8d3bf7f | 46 | /*ARGSUSED*/ |
ab85b059 | 47 | tcp_usrreq(so, req, m, nam, rights) |
eee3ab16 BJ |
48 | struct socket *so; |
49 | int req; | |
ab85b059 | 50 | struct mbuf *m, *nam, *rights; |
4eb5d593 | 51 | { |
53a5409e | 52 | register struct inpcb *inp = sotoinpcb(so); |
cdad2eb1 | 53 | register struct tcpcb *tp; |
72f24d7d | 54 | int s = splnet(); |
eee3ab16 | 55 | int error = 0; |
17b82ed4 | 56 | int ostate; |
72f24d7d | 57 | |
1d14d351 MK |
58 | if (req == PRU_CONTROL) |
59 | return (in_control(so, (int)m, (caddr_t)nam, | |
60 | (struct ifnet *)rights)); | |
ab85b059 SL |
61 | if (rights && rights->m_len) { |
62 | splx(s); | |
63 | return (EINVAL); | |
64 | } | |
53a5409e | 65 | /* |
290e0b0a BJ |
66 | * When a TCP is attached to a socket, then there will be |
67 | * a (struct inpcb) pointed at by the socket, and this | |
68 | * structure will point at a subsidary (struct tcpcb). | |
53a5409e | 69 | */ |
0974b45c | 70 | if (inp == 0 && req != PRU_ATTACH) { |
a6503abf | 71 | splx(s); |
290e0b0a | 72 | return (EINVAL); /* XXX */ |
a6503abf BJ |
73 | } |
74 | if (inp) { | |
cdad2eb1 | 75 | tp = intotcpcb(inp); |
8075bb0e | 76 | /* WHAT IF TP IS 0? */ |
9c5022e3 | 77 | #ifdef KPROF |
a6503abf | 78 | tcp_acounts[tp->t_state][req]++; |
9c5022e3 | 79 | #endif |
17b82ed4 | 80 | ostate = tp->t_state; |
ebf42a75 BJ |
81 | } else |
82 | ostate = 0; | |
eee3ab16 | 83 | switch (req) { |
4eb5d593 | 84 | |
290e0b0a BJ |
85 | /* |
86 | * TCP attaches to socket via PRU_ATTACH, reserving space, | |
8075bb0e | 87 | * and an internet control block. |
290e0b0a | 88 | */ |
eee3ab16 | 89 | case PRU_ATTACH: |
4ad99bae | 90 | if (inp) { |
eee3ab16 | 91 | error = EISCONN; |
cdad2eb1 | 92 | break; |
53a5409e | 93 | } |
a1edc12b | 94 | error = tcp_attach(so); |
a6503abf | 95 | if (error) |
4ad99bae | 96 | break; |
0e3936fa | 97 | if ((so->so_options & SO_LINGER) && so->so_linger == 0) |
8e65fd66 | 98 | so->so_linger = TCP_LINGERTIME; |
290e0b0a | 99 | tp = sototcpcb(so); |
72f24d7d | 100 | break; |
4eb5d593 | 101 | |
290e0b0a BJ |
102 | /* |
103 | * PRU_DETACH detaches the TCP protocol from the socket. | |
104 | * If the protocol state is non-embryonic, then can't | |
105 | * do this directly: have to initiate a PRU_DISCONNECT, | |
106 | * which may finish later; embryonic TCB's can just | |
107 | * be discarded here. | |
108 | */ | |
eee3ab16 | 109 | case PRU_DETACH: |
290e0b0a | 110 | if (tp->t_state > TCPS_LISTEN) |
0e3936fa SL |
111 | tp = tcp_disconnect(tp); |
112 | else | |
113 | tp = tcp_close(tp); | |
eee3ab16 BJ |
114 | break; |
115 | ||
8075bb0e BJ |
116 | /* |
117 | * Give the socket an address. | |
118 | */ | |
119 | case PRU_BIND: | |
120 | error = in_pcbbind(inp, nam); | |
121 | if (error) | |
122 | break; | |
123 | break; | |
124 | ||
125 | /* | |
126 | * Prepare to accept connections. | |
127 | */ | |
128 | case PRU_LISTEN: | |
129 | if (inp->inp_lport == 0) | |
130 | error = in_pcbbind(inp, (struct mbuf *)0); | |
131 | if (error == 0) | |
132 | tp->t_state = TCPS_LISTEN; | |
133 | break; | |
134 | ||
290e0b0a BJ |
135 | /* |
136 | * Initiate connection to peer. | |
137 | * Create a template for use in transmissions on this connection. | |
138 | * Enter SYN_SENT state, and mark socket as connecting. | |
139 | * Start keep-alive timer, and seed output sequence space. | |
140 | * Send initial segment on connection. | |
141 | */ | |
eee3ab16 | 142 | case PRU_CONNECT: |
8075bb0e BJ |
143 | if (inp->inp_lport == 0) { |
144 | error = in_pcbbind(inp, (struct mbuf *)0); | |
145 | if (error) | |
146 | break; | |
147 | } | |
148 | error = in_pcbconnect(inp, nam); | |
4ad99bae | 149 | if (error) |
53a5409e | 150 | break; |
b454c3ea | 151 | tp->t_template = tcp_template(tp); |
290e0b0a BJ |
152 | if (tp->t_template == 0) { |
153 | in_pcbdisconnect(inp); | |
154 | error = ENOBUFS; | |
155 | break; | |
156 | } | |
53a5409e | 157 | soisconnecting(so); |
a6503abf | 158 | tp->t_state = TCPS_SYN_SENT; |
4aed14e3 BJ |
159 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; |
160 | tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; | |
161 | tcp_sendseqinit(tp); | |
8a2f82db | 162 | error = tcp_output(tp); |
72f24d7d | 163 | break; |
4eb5d593 | 164 | |
4945768c SL |
165 | /* |
166 | * Create a TCP connection between two sockets. | |
167 | */ | |
168 | case PRU_CONNECT2: | |
169 | error = EOPNOTSUPP; | |
170 | break; | |
171 | ||
290e0b0a BJ |
172 | /* |
173 | * Initiate disconnect from peer. | |
174 | * If connection never passed embryonic stage, just drop; | |
175 | * else if don't need to let data drain, then can just drop anyways, | |
176 | * else have to begin TCP shutdown process: mark socket disconnecting, | |
177 | * drain unread data, state switch to reflect user close, and | |
178 | * send segment (e.g. FIN) to peer. Socket will be really disconnected | |
179 | * when peer sends FIN and acks ours. | |
180 | * | |
181 | * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. | |
182 | */ | |
183 | case PRU_DISCONNECT: | |
0e3936fa | 184 | tp = tcp_disconnect(tp); |
4aed14e3 BJ |
185 | break; |
186 | ||
290e0b0a BJ |
187 | /* |
188 | * Accept a connection. Essentially all the work is | |
189 | * done at higher levels; just return the address | |
190 | * of the peer, storing through addr. | |
191 | */ | |
1acff8ec | 192 | case PRU_ACCEPT: { |
8075bb0e | 193 | struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); |
1acff8ec | 194 | |
8075bb0e BJ |
195 | nam->m_len = sizeof (struct sockaddr_in); |
196 | sin->sin_family = AF_INET; | |
197 | sin->sin_port = inp->inp_fport; | |
198 | sin->sin_addr = inp->inp_faddr; | |
eee3ab16 | 199 | break; |
8075bb0e | 200 | } |
eee3ab16 | 201 | |
290e0b0a BJ |
202 | /* |
203 | * Mark the connection as being incapable of further output. | |
204 | */ | |
eee3ab16 | 205 | case PRU_SHUTDOWN: |
0974b45c | 206 | socantsendmore(so); |
0e3936fa SL |
207 | tp = tcp_usrclosed(tp); |
208 | if (tp) | |
209 | error = tcp_output(tp); | |
72f24d7d BJ |
210 | break; |
211 | ||
290e0b0a BJ |
212 | /* |
213 | * After a receive, possibly send window update to peer. | |
214 | */ | |
eee3ab16 | 215 | case PRU_RCVD: |
f1b2fa5b | 216 | (void) tcp_output(tp); |
72f24d7d BJ |
217 | break; |
218 | ||
290e0b0a BJ |
219 | /* |
220 | * Do a send by putting data in output queue and updating urgent | |
221 | * marker if URG set. Possibly send more data. | |
222 | */ | |
eee3ab16 | 223 | case PRU_SEND: |
a6503abf | 224 | sbappend(&so->so_snd, m); |
8a2f82db | 225 | #ifdef notdef |
0974b45c | 226 | if (tp->t_flags & TF_PUSH) |
a6503abf | 227 | tp->snd_end = tp->snd_una + so->so_snd.sb_cc; |
8a2f82db SL |
228 | #endif |
229 | error = tcp_output(tp); | |
ab85b059 SL |
230 | if (error) { /* XXX fix to use other path */ |
231 | if (error == ENOBUFS) /* XXX */ | |
232 | error = 0; /* XXX */ | |
233 | tcpsenderrors++; | |
234 | } | |
72f24d7d BJ |
235 | break; |
236 | ||
290e0b0a BJ |
237 | /* |
238 | * Abort the TCP. | |
239 | */ | |
eee3ab16 | 240 | case PRU_ABORT: |
0e3936fa | 241 | tp = tcp_drop(tp, ECONNABORTED); |
72f24d7d BJ |
242 | break; |
243 | ||
f1b2fa5b | 244 | case PRU_SENSE: |
74040e68 MK |
245 | ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; |
246 | return (0); | |
f1b2fa5b BJ |
247 | |
248 | case PRU_RCVOOB: | |
8b5a83bb BJ |
249 | if (so->so_oobmark == 0 && |
250 | (so->so_state & SS_RCVATMARK) == 0) { | |
0244dbc7 BJ |
251 | error = EINVAL; |
252 | break; | |
253 | } | |
b2db9217 | 254 | if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { |
8b5a83bb | 255 | error = EWOULDBLOCK; |
b2db9217 | 256 | break; |
8b5a83bb | 257 | } |
283ea225 | 258 | m->m_len = 1; |
b2db9217 | 259 | *mtod(m, caddr_t) = tp->t_iobc; |
f1b2fa5b BJ |
260 | break; |
261 | ||
262 | case PRU_SENDOOB: | |
8b5a83bb | 263 | if (sbspace(&so->so_snd) < -512) { |
37279c1b | 264 | m_freem(m); |
8b5a83bb BJ |
265 | error = ENOBUFS; |
266 | break; | |
267 | } | |
0244dbc7 BJ |
268 | tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1; |
269 | sbappend(&so->so_snd, m); | |
b2db9217 | 270 | tp->t_force = 1; |
8a2f82db | 271 | error = tcp_output(tp); |
b2db9217 | 272 | tp->t_force = 0; |
f1b2fa5b BJ |
273 | break; |
274 | ||
126472ab | 275 | case PRU_SOCKADDR: |
8075bb0e | 276 | in_setsockaddr(inp, nam); |
126472ab SL |
277 | break; |
278 | ||
a7343092 SL |
279 | case PRU_PEERADDR: |
280 | in_setpeeraddr(inp, nam); | |
281 | break; | |
282 | ||
290e0b0a BJ |
283 | /* |
284 | * TCP slow timer went off; going through this | |
285 | * routine for tracing's sake. | |
286 | */ | |
eee3ab16 | 287 | case PRU_SLOWTIMO: |
0e3936fa | 288 | tp = tcp_timers(tp, (int)nam); |
8075bb0e | 289 | req |= (int)nam << 8; /* for debug's sake */ |
eee3ab16 BJ |
290 | break; |
291 | ||
9c5022e3 BJ |
292 | default: |
293 | panic("tcp_usrreq"); | |
72f24d7d | 294 | } |
17b82ed4 BJ |
295 | if (tp && (so->so_options & SO_DEBUG)) |
296 | tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); | |
72f24d7d | 297 | splx(s); |
53a5409e | 298 | return (error); |
4eb5d593 | 299 | } |
4aed14e3 | 300 | |
1d14d351 MK |
301 | int tcp_sendspace = 1024*4; |
302 | int tcp_recvspace = 1024*4; | |
290e0b0a BJ |
303 | /* |
304 | * Attach TCP protocol to socket, allocating | |
305 | * internet protocol control block, tcp control block, | |
306 | * bufer space, and entering LISTEN state if to accept connections. | |
307 | */ | |
8075bb0e | 308 | tcp_attach(so) |
290e0b0a | 309 | struct socket *so; |
290e0b0a BJ |
310 | { |
311 | register struct tcpcb *tp; | |
312 | struct inpcb *inp; | |
313 | int error; | |
314 | ||
59965020 | 315 | error = soreserve(so, tcp_sendspace, tcp_recvspace); |
ebf42a75 | 316 | if (error) |
054054fd | 317 | return (error); |
ebf42a75 | 318 | error = in_pcballoc(so, &tcb); |
290e0b0a | 319 | if (error) |
054054fd | 320 | return (error); |
8075bb0e | 321 | inp = sotoinpcb(so); |
290e0b0a | 322 | tp = tcp_newtcpcb(inp); |
ebf42a75 | 323 | if (tp == 0) { |
054054fd MK |
324 | int nofd = so->so_state & SS_NOFDREF; /* XXX */ |
325 | ||
326 | so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ | |
327 | in_pcbdetach(inp); | |
328 | so->so_state |= nofd; | |
329 | return (ENOBUFS); | |
ebf42a75 | 330 | } |
8075bb0e | 331 | tp->t_state = TCPS_CLOSED; |
290e0b0a BJ |
332 | return (0); |
333 | } | |
334 | ||
335 | /* | |
336 | * Initiate (or continue) disconnect. | |
337 | * If embryonic state, just send reset (once). | |
f9e4ec68 | 338 | * If in ``let data drain'' option and linger null, just drop. |
290e0b0a BJ |
339 | * Otherwise (hard), mark socket disconnecting and drop |
340 | * current input data; switch states based on user close, and | |
341 | * send segment to peer (with FIN). | |
342 | */ | |
0e3936fa | 343 | struct tcpcb * |
290e0b0a | 344 | tcp_disconnect(tp) |
0e3936fa | 345 | register struct tcpcb *tp; |
290e0b0a BJ |
346 | { |
347 | struct socket *so = tp->t_inpcb->inp_socket; | |
348 | ||
349 | if (tp->t_state < TCPS_ESTABLISHED) | |
0e3936fa | 350 | tp = tcp_close(tp); |
f9e4ec68 | 351 | else if ((so->so_options & SO_LINGER) && so->so_linger == 0) |
0e3936fa | 352 | tp = tcp_drop(tp, 0); |
290e0b0a BJ |
353 | else { |
354 | soisdisconnecting(so); | |
355 | sbflush(&so->so_rcv); | |
0e3936fa SL |
356 | tp = tcp_usrclosed(tp); |
357 | if (tp) | |
358 | (void) tcp_output(tp); | |
290e0b0a | 359 | } |
0e3936fa | 360 | return (tp); |
290e0b0a BJ |
361 | } |
362 | ||
363 | /* | |
364 | * User issued close, and wish to trail through shutdown states: | |
365 | * if never received SYN, just forget it. If got a SYN from peer, | |
366 | * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. | |
367 | * If already got a FIN from peer, then almost done; go to LAST_ACK | |
368 | * state. In all other cases, have already sent FIN to peer (e.g. | |
369 | * after PRU_SHUTDOWN), and just have to play tedious game waiting | |
370 | * for peer to send FIN or not respond to keep-alives, etc. | |
085a0b90 | 371 | * We can let the user exit from the close as soon as the FIN is acked. |
290e0b0a | 372 | */ |
0e3936fa | 373 | struct tcpcb * |
4aed14e3 | 374 | tcp_usrclosed(tp) |
0e3936fa | 375 | register struct tcpcb *tp; |
4aed14e3 BJ |
376 | { |
377 | ||
4aed14e3 BJ |
378 | switch (tp->t_state) { |
379 | ||
815b24e1 | 380 | case TCPS_CLOSED: |
4aed14e3 BJ |
381 | case TCPS_LISTEN: |
382 | case TCPS_SYN_SENT: | |
383 | tp->t_state = TCPS_CLOSED; | |
0e3936fa | 384 | tp = tcp_close(tp); |
4aed14e3 BJ |
385 | break; |
386 | ||
387 | case TCPS_SYN_RECEIVED: | |
388 | case TCPS_ESTABLISHED: | |
389 | tp->t_state = TCPS_FIN_WAIT_1; | |
390 | break; | |
391 | ||
392 | case TCPS_CLOSE_WAIT: | |
393 | tp->t_state = TCPS_LAST_ACK; | |
394 | break; | |
395 | } | |
0e3936fa | 396 | if (tp && tp->t_state >= TCPS_FIN_WAIT_2) |
085a0b90 | 397 | soisdisconnected(tp->t_inpcb->inp_socket); |
0e3936fa | 398 | return (tp); |
4aed14e3 | 399 | } |