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