Commit | Line | Data |
---|---|---|
77a4e3ca | 1 | /* tcp_usrreq.c 1.64 82/10/05 */ |
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" | |
0974b45c | 9 | #include "../net/in.h" |
c124e997 | 10 | #include "../net/route.h" |
0974b45c BJ |
11 | #include "../net/in_pcb.h" |
12 | #include "../net/in_systm.h" | |
4ad99bae | 13 | #include "../net/if.h" |
eee3ab16 | 14 | #include "../net/ip.h" |
eb44bfb2 | 15 | #include "../net/ip_var.h" |
eee3ab16 | 16 | #include "../net/tcp.h" |
eee3ab16 | 17 | #include "../net/tcp_fsm.h" |
0974b45c BJ |
18 | #include "../net/tcp_seq.h" |
19 | #include "../net/tcp_timer.h" | |
eee3ab16 | 20 | #include "../net/tcp_var.h" |
0974b45c | 21 | #include "../net/tcpip.h" |
17b82ed4 | 22 | #include "../net/tcp_debug.h" |
8a2f82db | 23 | #include <errno.h> |
eee3ab16 | 24 | |
290e0b0a BJ |
25 | /* |
26 | * TCP protocol interface to socket abstraction. | |
27 | */ | |
28 | extern char *tcpstates[]; | |
4ad99bae | 29 | struct tcpcb *tcp_newtcpcb(); |
290e0b0a | 30 | |
9c5022e3 | 31 | /* |
290e0b0a | 32 | * Process a TCP user request for TCP tb. If this is a send request |
9c5022e3 BJ |
33 | * then m is the mbuf chain of send data. If this is a timer expiration |
34 | * (called from the software clock routine), then timertype tells which timer. | |
35 | */ | |
8075bb0e | 36 | tcp_usrreq(so, req, m, nam, opt) |
eee3ab16 BJ |
37 | struct socket *so; |
38 | int req; | |
8075bb0e BJ |
39 | struct mbuf *m, *nam; |
40 | struct socketopt *opt; | |
4eb5d593 | 41 | { |
53a5409e | 42 | register struct inpcb *inp = sotoinpcb(so); |
cdad2eb1 | 43 | register struct tcpcb *tp; |
72f24d7d | 44 | int s = splnet(); |
eee3ab16 | 45 | int error = 0; |
17b82ed4 | 46 | int ostate; |
72f24d7d | 47 | |
53a5409e | 48 | /* |
290e0b0a BJ |
49 | * When a TCP is attached to a socket, then there will be |
50 | * a (struct inpcb) pointed at by the socket, and this | |
51 | * structure will point at a subsidary (struct tcpcb). | |
53a5409e | 52 | */ |
0974b45c | 53 | if (inp == 0 && req != PRU_ATTACH) { |
a6503abf | 54 | splx(s); |
290e0b0a | 55 | return (EINVAL); /* XXX */ |
a6503abf BJ |
56 | } |
57 | if (inp) { | |
cdad2eb1 | 58 | tp = intotcpcb(inp); |
8075bb0e | 59 | /* WHAT IF TP IS 0? */ |
9c5022e3 | 60 | #ifdef KPROF |
a6503abf | 61 | tcp_acounts[tp->t_state][req]++; |
9c5022e3 | 62 | #endif |
17b82ed4 | 63 | ostate = tp->t_state; |
ebf42a75 BJ |
64 | } else |
65 | ostate = 0; | |
eee3ab16 | 66 | switch (req) { |
4eb5d593 | 67 | |
290e0b0a BJ |
68 | /* |
69 | * TCP attaches to socket via PRU_ATTACH, reserving space, | |
8075bb0e | 70 | * and an internet control block. |
290e0b0a | 71 | */ |
eee3ab16 | 72 | case PRU_ATTACH: |
4ad99bae | 73 | if (inp) { |
eee3ab16 | 74 | error = EISCONN; |
cdad2eb1 | 75 | break; |
53a5409e | 76 | } |
8075bb0e | 77 | error = tcp_attach(so, nam); |
a6503abf | 78 | if (error) |
4ad99bae | 79 | break; |
8e65fd66 BJ |
80 | if ((so->so_options & SO_DONTLINGER) == 0) |
81 | so->so_linger = TCP_LINGERTIME; | |
290e0b0a | 82 | tp = sototcpcb(so); |
72f24d7d | 83 | break; |
4eb5d593 | 84 | |
290e0b0a BJ |
85 | /* |
86 | * PRU_DETACH detaches the TCP protocol from the socket. | |
87 | * If the protocol state is non-embryonic, then can't | |
88 | * do this directly: have to initiate a PRU_DISCONNECT, | |
89 | * which may finish later; embryonic TCB's can just | |
90 | * be discarded here. | |
91 | */ | |
eee3ab16 | 92 | case PRU_DETACH: |
290e0b0a BJ |
93 | if (tp->t_state > TCPS_LISTEN) |
94 | tcp_disconnect(tp); | |
95 | else { | |
96 | tcp_close(tp); | |
97 | tp = 0; | |
98 | } | |
eee3ab16 BJ |
99 | break; |
100 | ||
8075bb0e BJ |
101 | /* |
102 | * Give the socket an address. | |
103 | */ | |
104 | case PRU_BIND: | |
105 | error = in_pcbbind(inp, nam); | |
106 | if (error) | |
107 | break; | |
108 | break; | |
109 | ||
110 | /* | |
111 | * Prepare to accept connections. | |
112 | */ | |
113 | case PRU_LISTEN: | |
114 | if (inp->inp_lport == 0) | |
115 | error = in_pcbbind(inp, (struct mbuf *)0); | |
116 | if (error == 0) | |
117 | tp->t_state = TCPS_LISTEN; | |
118 | break; | |
119 | ||
290e0b0a BJ |
120 | /* |
121 | * Initiate connection to peer. | |
122 | * Create a template for use in transmissions on this connection. | |
123 | * Enter SYN_SENT state, and mark socket as connecting. | |
124 | * Start keep-alive timer, and seed output sequence space. | |
125 | * Send initial segment on connection. | |
126 | */ | |
eee3ab16 | 127 | case PRU_CONNECT: |
8075bb0e BJ |
128 | if (inp->inp_lport == 0) { |
129 | error = in_pcbbind(inp, (struct mbuf *)0); | |
130 | if (error) | |
131 | break; | |
132 | } | |
133 | error = in_pcbconnect(inp, nam); | |
4ad99bae | 134 | if (error) |
53a5409e | 135 | break; |
b454c3ea | 136 | tp->t_template = tcp_template(tp); |
290e0b0a BJ |
137 | if (tp->t_template == 0) { |
138 | in_pcbdisconnect(inp); | |
139 | error = ENOBUFS; | |
140 | break; | |
141 | } | |
53a5409e | 142 | soisconnecting(so); |
a6503abf | 143 | tp->t_state = TCPS_SYN_SENT; |
4aed14e3 BJ |
144 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; |
145 | tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; | |
146 | tcp_sendseqinit(tp); | |
8a2f82db | 147 | error = tcp_output(tp); |
72f24d7d | 148 | break; |
4eb5d593 | 149 | |
290e0b0a BJ |
150 | /* |
151 | * Initiate disconnect from peer. | |
152 | * If connection never passed embryonic stage, just drop; | |
153 | * else if don't need to let data drain, then can just drop anyways, | |
154 | * else have to begin TCP shutdown process: mark socket disconnecting, | |
155 | * drain unread data, state switch to reflect user close, and | |
156 | * send segment (e.g. FIN) to peer. Socket will be really disconnected | |
157 | * when peer sends FIN and acks ours. | |
158 | * | |
159 | * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. | |
160 | */ | |
161 | case PRU_DISCONNECT: | |
162 | tcp_disconnect(tp); | |
4aed14e3 BJ |
163 | break; |
164 | ||
290e0b0a BJ |
165 | /* |
166 | * Accept a connection. Essentially all the work is | |
167 | * done at higher levels; just return the address | |
168 | * of the peer, storing through addr. | |
169 | */ | |
1acff8ec | 170 | case PRU_ACCEPT: { |
8075bb0e | 171 | struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); |
1acff8ec | 172 | |
8075bb0e BJ |
173 | nam->m_len = sizeof (struct sockaddr_in); |
174 | sin->sin_family = AF_INET; | |
175 | sin->sin_port = inp->inp_fport; | |
176 | sin->sin_addr = inp->inp_faddr; | |
eee3ab16 | 177 | break; |
8075bb0e | 178 | } |
eee3ab16 | 179 | |
290e0b0a BJ |
180 | /* |
181 | * Mark the connection as being incapable of further output. | |
182 | */ | |
eee3ab16 | 183 | case PRU_SHUTDOWN: |
0974b45c | 184 | socantsendmore(so); |
4aed14e3 | 185 | tcp_usrclosed(tp); |
8a2f82db | 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: |
a6503abf | 213 | 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: |
8075bb0e BJ |
261 | tcp_timers(tp, (int)nam); |
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 | ||
ebf42a75 BJ |
288 | error = in_pcbreserve(so, tcp_sendspace, tcp_recvspace); |
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 | */ | |
316 | tcp_disconnect(tp) | |
317 | struct tcpcb *tp; | |
318 | { | |
319 | struct socket *so = tp->t_inpcb->inp_socket; | |
320 | ||
321 | if (tp->t_state < TCPS_ESTABLISHED) | |
322 | tcp_close(tp); | |
8e65fd66 | 323 | else if (so->so_linger == 0) |
290e0b0a BJ |
324 | tcp_drop(tp, 0); |
325 | else { | |
326 | soisdisconnecting(so); | |
327 | sbflush(&so->so_rcv); | |
328 | tcp_usrclosed(tp); | |
329 | (void) tcp_output(tp); | |
330 | } | |
331 | } | |
332 | ||
333 | /* | |
334 | * User issued close, and wish to trail through shutdown states: | |
335 | * if never received SYN, just forget it. If got a SYN from peer, | |
336 | * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. | |
337 | * If already got a FIN from peer, then almost done; go to LAST_ACK | |
338 | * state. In all other cases, have already sent FIN to peer (e.g. | |
339 | * after PRU_SHUTDOWN), and just have to play tedious game waiting | |
340 | * for peer to send FIN or not respond to keep-alives, etc. | |
085a0b90 | 341 | * We can let the user exit from the close as soon as the FIN is acked. |
290e0b0a | 342 | */ |
4aed14e3 BJ |
343 | tcp_usrclosed(tp) |
344 | struct tcpcb *tp; | |
345 | { | |
346 | ||
4aed14e3 BJ |
347 | switch (tp->t_state) { |
348 | ||
349 | case TCPS_LISTEN: | |
350 | case TCPS_SYN_SENT: | |
351 | tp->t_state = TCPS_CLOSED; | |
352 | tcp_close(tp); | |
353 | break; | |
354 | ||
355 | case TCPS_SYN_RECEIVED: | |
356 | case TCPS_ESTABLISHED: | |
357 | tp->t_state = TCPS_FIN_WAIT_1; | |
358 | break; | |
359 | ||
360 | case TCPS_CLOSE_WAIT: | |
361 | tp->t_state = TCPS_LAST_ACK; | |
362 | break; | |
363 | } | |
085a0b90 BJ |
364 | if (tp->t_state >= TCPS_FIN_WAIT_2) |
365 | soisdisconnected(tp->t_inpcb->inp_socket); | |
4aed14e3 | 366 | } |