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