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