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