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