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