tcp_usrreq.c revision 1.26 1 /* $NetBSD: tcp_usrreq.c,v 1.26 1997/07/28 22:31:10 thorpej 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/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/protosw.h>
46 #include <sys/errno.h>
47 #include <sys/stat.h>
48 #include <sys/proc.h>
49 #include <sys/ucred.h>
50
51 #include <vm/vm.h>
52 #include <sys/sysctl.h>
53
54 #include <net/if.h>
55 #include <net/route.h>
56
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip.h>
61 #include <netinet/in_pcb.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/tcp.h>
64 #include <netinet/tcp_fsm.h>
65 #include <netinet/tcp_seq.h>
66 #include <netinet/tcp_timer.h>
67 #include <netinet/tcp_var.h>
68 #include <netinet/tcpip.h>
69 #include <netinet/tcp_debug.h>
70
71 #include "opt_tcp_recvspace.h"
72 #include "opt_tcp_sendspace.h"
73
74 /*
75 * TCP protocol interface to socket abstraction.
76 */
77 extern char *tcpstates[];
78
79 /*
80 * Process a TCP user request for TCP tb. If this is a send request
81 * then m is the mbuf chain of send data. If this is a timer expiration
82 * (called from the software clock routine), then timertype tells which timer.
83 */
84 /*ARGSUSED*/
85 int
86 tcp_usrreq(so, req, m, nam, control, p)
87 struct socket *so;
88 int req;
89 struct mbuf *m, *nam, *control;
90 struct proc *p;
91 {
92 register struct inpcb *inp;
93 register struct tcpcb *tp = NULL;
94 int s;
95 int error = 0;
96 int ostate;
97
98 if (req == PRU_CONTROL)
99 return (in_control(so, (long)m, (caddr_t)nam,
100 (struct ifnet *)control, p));
101
102 s = splsoftnet();
103 inp = sotoinpcb(so);
104 #ifdef DIAGNOSTIC
105 if (req != PRU_SEND && req != PRU_SENDOOB && control)
106 panic("tcp_usrreq: unexpected control mbuf");
107 #endif
108 /*
109 * When a TCP is attached to a socket, then there will be
110 * a (struct inpcb) pointed at by the socket, and this
111 * structure will point at a subsidary (struct tcpcb).
112 */
113 if (inp == 0 && req != PRU_ATTACH) {
114 error = EINVAL;
115 goto release;
116 }
117 if (inp) {
118 tp = intotcpcb(inp);
119 /* WHAT IF TP IS 0? */
120 #ifdef KPROF
121 tcp_acounts[tp->t_state][req]++;
122 #endif
123 ostate = tp->t_state;
124 } else
125 ostate = 0;
126
127 switch (req) {
128
129 /*
130 * TCP attaches to socket via PRU_ATTACH, reserving space,
131 * and an internet control block.
132 */
133 case PRU_ATTACH:
134 if (inp != 0) {
135 error = EISCONN;
136 break;
137 }
138 error = tcp_attach(so);
139 if (error)
140 break;
141 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
142 so->so_linger = TCP_LINGERTIME * hz;
143 tp = sototcpcb(so);
144 break;
145
146 /*
147 * PRU_DETACH detaches the TCP protocol from the socket.
148 */
149 case PRU_DETACH:
150 tp = tcp_disconnect(tp);
151 break;
152
153 /*
154 * Give the socket an address.
155 */
156 case PRU_BIND:
157 error = in_pcbbind(inp, nam, p);
158 break;
159
160 /*
161 * Prepare to accept connections.
162 */
163 case PRU_LISTEN:
164 if (inp->inp_lport == 0) {
165 error = in_pcbbind(inp, (struct mbuf *)0,
166 (struct proc *)0);
167 if (error)
168 break;
169 }
170 tp->t_state = TCPS_LISTEN;
171 break;
172
173 /*
174 * Initiate connection to peer.
175 * Create a template for use in transmissions on this connection.
176 * Enter SYN_SENT state, and mark socket as connecting.
177 * Start keep-alive timer, and seed output sequence space.
178 * Send initial segment on connection.
179 */
180 case PRU_CONNECT:
181 if (inp->inp_lport == 0) {
182 error = in_pcbbind(inp, (struct mbuf *)0,
183 (struct proc *)0);
184 if (error)
185 break;
186 }
187 error = in_pcbconnect(inp, nam);
188 if (error)
189 break;
190 tp->t_template = tcp_template(tp);
191 if (tp->t_template == 0) {
192 in_pcbdisconnect(inp);
193 error = ENOBUFS;
194 break;
195 }
196 /* Compute window scaling to request. */
197 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
198 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
199 tp->request_r_scale++;
200 soisconnecting(so);
201 tcpstat.tcps_connattempt++;
202 tp->t_state = TCPS_SYN_SENT;
203 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
204 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
205 tcp_sendseqinit(tp);
206 error = tcp_output(tp);
207 break;
208
209 /*
210 * Create a TCP connection between two sockets.
211 */
212 case PRU_CONNECT2:
213 error = EOPNOTSUPP;
214 break;
215
216 /*
217 * Initiate disconnect from peer.
218 * If connection never passed embryonic stage, just drop;
219 * else if don't need to let data drain, then can just drop anyways,
220 * else have to begin TCP shutdown process: mark socket disconnecting,
221 * drain unread data, state switch to reflect user close, and
222 * send segment (e.g. FIN) to peer. Socket will be really disconnected
223 * when peer sends FIN and acks ours.
224 *
225 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
226 */
227 case PRU_DISCONNECT:
228 tp = tcp_disconnect(tp);
229 break;
230
231 /*
232 * Accept a connection. Essentially all the work is
233 * done at higher levels; just return the address
234 * of the peer, storing through addr.
235 */
236 case PRU_ACCEPT:
237 in_setpeeraddr(inp, nam);
238 break;
239
240 /*
241 * Mark the connection as being incapable of further output.
242 */
243 case PRU_SHUTDOWN:
244 socantsendmore(so);
245 tp = tcp_usrclosed(tp);
246 if (tp)
247 error = tcp_output(tp);
248 break;
249
250 /*
251 * After a receive, possibly send window update to peer.
252 */
253 case PRU_RCVD:
254 (void) tcp_output(tp);
255 break;
256
257 /*
258 * Do a send by putting data in output queue and updating urgent
259 * marker if URG set. Possibly send more data.
260 */
261 case PRU_SEND:
262 if (control && control->m_len) {
263 m_freem(control);
264 m_freem(m);
265 error = EINVAL;
266 break;
267 }
268 sbappend(&so->so_snd, m);
269 error = tcp_output(tp);
270 break;
271
272 /*
273 * Abort the TCP.
274 */
275 case PRU_ABORT:
276 tp = tcp_drop(tp, ECONNABORTED);
277 break;
278
279 case PRU_SENSE:
280 /*
281 * stat: don't bother with a blocksize.
282 */
283 splx(s);
284 return (0);
285
286 case PRU_RCVOOB:
287 if (control && control->m_len) {
288 m_freem(control);
289 m_freem(m);
290 error = EINVAL;
291 break;
292 }
293 if ((so->so_oobmark == 0 &&
294 (so->so_state & SS_RCVATMARK) == 0) ||
295 so->so_options & SO_OOBINLINE ||
296 tp->t_oobflags & TCPOOB_HADDATA) {
297 error = EINVAL;
298 break;
299 }
300 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
301 error = EWOULDBLOCK;
302 break;
303 }
304 m->m_len = 1;
305 *mtod(m, caddr_t) = tp->t_iobc;
306 if (((long)nam & MSG_PEEK) == 0)
307 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
308 break;
309
310 case PRU_SENDOOB:
311 if (sbspace(&so->so_snd) < -512) {
312 m_freem(m);
313 error = ENOBUFS;
314 break;
315 }
316 /*
317 * According to RFC961 (Assigned Protocols),
318 * the urgent pointer points to the last octet
319 * of urgent data. We continue, however,
320 * to consider it to indicate the first octet
321 * of data past the urgent section.
322 * Otherwise, snd_up should be one lower.
323 */
324 sbappend(&so->so_snd, m);
325 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
326 tp->t_force = 1;
327 error = tcp_output(tp);
328 tp->t_force = 0;
329 break;
330
331 case PRU_SOCKADDR:
332 in_setsockaddr(inp, nam);
333 break;
334
335 case PRU_PEERADDR:
336 in_setpeeraddr(inp, nam);
337 break;
338
339 /*
340 * TCP slow timer went off; going through this
341 * routine for tracing's sake.
342 */
343 case PRU_SLOWTIMO:
344 tp = tcp_timers(tp, (long)nam);
345 req |= (long)nam << 8; /* for debug's sake */
346 break;
347
348 default:
349 panic("tcp_usrreq");
350 }
351 if (tp && (so->so_options & SO_DEBUG))
352 tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
353
354 release:
355 splx(s);
356 return (error);
357 }
358
359 int
360 tcp_ctloutput(op, so, level, optname, mp)
361 int op;
362 struct socket *so;
363 int level, optname;
364 struct mbuf **mp;
365 {
366 int error = 0, s;
367 struct inpcb *inp;
368 register struct tcpcb *tp;
369 register struct mbuf *m;
370 register int i;
371
372 s = splsoftnet();
373 inp = sotoinpcb(so);
374 if (inp == NULL) {
375 splx(s);
376 if (op == PRCO_SETOPT && *mp)
377 (void) m_free(*mp);
378 return (ECONNRESET);
379 }
380 if (level != IPPROTO_TCP) {
381 error = ip_ctloutput(op, so, level, optname, mp);
382 splx(s);
383 return (error);
384 }
385 tp = intotcpcb(inp);
386
387 switch (op) {
388
389 case PRCO_SETOPT:
390 m = *mp;
391 switch (optname) {
392
393 case TCP_NODELAY:
394 if (m == NULL || m->m_len < sizeof (int))
395 error = EINVAL;
396 else if (*mtod(m, int *))
397 tp->t_flags |= TF_NODELAY;
398 else
399 tp->t_flags &= ~TF_NODELAY;
400 break;
401
402 case TCP_MAXSEG:
403 if (m && (i = *mtod(m, int *)) > 0 && i <= tp->t_maxseg)
404 tp->t_maxseg = i;
405 else
406 error = EINVAL;
407 break;
408
409 default:
410 error = ENOPROTOOPT;
411 break;
412 }
413 if (m)
414 (void) m_free(m);
415 break;
416
417 case PRCO_GETOPT:
418 *mp = m = m_get(M_WAIT, MT_SOOPTS);
419 m->m_len = sizeof(int);
420
421 switch (optname) {
422 case TCP_NODELAY:
423 *mtod(m, int *) = tp->t_flags & TF_NODELAY;
424 break;
425 case TCP_MAXSEG:
426 *mtod(m, int *) = tp->t_maxseg;
427 break;
428 default:
429 error = ENOPROTOOPT;
430 break;
431 }
432 break;
433 }
434 splx(s);
435 return (error);
436 }
437
438 #ifndef TCP_SENDSPACE
439 #define TCP_SENDSPACE 1024*16;
440 #endif
441 int tcp_sendspace = TCP_SENDSPACE;
442 #ifndef TCP_RECVSPACE
443 #define TCP_RECVSPACE 1024*16;
444 #endif
445 int tcp_recvspace = TCP_RECVSPACE;
446
447 /*
448 * Attach TCP protocol to socket, allocating
449 * internet protocol control block, tcp control block,
450 * bufer space, and entering LISTEN state if to accept connections.
451 */
452 int
453 tcp_attach(so)
454 struct socket *so;
455 {
456 register struct tcpcb *tp;
457 struct inpcb *inp;
458 int error;
459
460 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
461 error = soreserve(so, tcp_sendspace, tcp_recvspace);
462 if (error)
463 return (error);
464 }
465 error = in_pcballoc(so, &tcbtable);
466 if (error)
467 return (error);
468 inp = sotoinpcb(so);
469 tp = tcp_newtcpcb(inp);
470 if (tp == 0) {
471 int nofd = so->so_state & SS_NOFDREF; /* XXX */
472
473 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
474 in_pcbdetach(inp);
475 so->so_state |= nofd;
476 return (ENOBUFS);
477 }
478 tp->t_state = TCPS_CLOSED;
479 return (0);
480 }
481
482 /*
483 * Initiate (or continue) disconnect.
484 * If embryonic state, just send reset (once).
485 * If in ``let data drain'' option and linger null, just drop.
486 * Otherwise (hard), mark socket disconnecting and drop
487 * current input data; switch states based on user close, and
488 * send segment to peer (with FIN).
489 */
490 struct tcpcb *
491 tcp_disconnect(tp)
492 register struct tcpcb *tp;
493 {
494 struct socket *so = tp->t_inpcb->inp_socket;
495
496 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
497 tp = tcp_close(tp);
498 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
499 tp = tcp_drop(tp, 0);
500 else {
501 soisdisconnecting(so);
502 sbflush(&so->so_rcv);
503 tp = tcp_usrclosed(tp);
504 if (tp)
505 (void) tcp_output(tp);
506 }
507 return (tp);
508 }
509
510 /*
511 * User issued close, and wish to trail through shutdown states:
512 * if never received SYN, just forget it. If got a SYN from peer,
513 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
514 * If already got a FIN from peer, then almost done; go to LAST_ACK
515 * state. In all other cases, have already sent FIN to peer (e.g.
516 * after PRU_SHUTDOWN), and just have to play tedious game waiting
517 * for peer to send FIN or not respond to keep-alives, etc.
518 * We can let the user exit from the close as soon as the FIN is acked.
519 */
520 struct tcpcb *
521 tcp_usrclosed(tp)
522 register struct tcpcb *tp;
523 {
524
525 switch (tp->t_state) {
526
527 case TCPS_CLOSED:
528 case TCPS_LISTEN:
529 case TCPS_SYN_SENT:
530 tp->t_state = TCPS_CLOSED;
531 tp = tcp_close(tp);
532 break;
533
534 case TCPS_SYN_RECEIVED:
535 case TCPS_ESTABLISHED:
536 tp->t_state = TCPS_FIN_WAIT_1;
537 break;
538
539 case TCPS_CLOSE_WAIT:
540 tp->t_state = TCPS_LAST_ACK;
541 break;
542 }
543 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
544 soisdisconnected(tp->t_inpcb->inp_socket);
545 /*
546 * If we are in FIN_WAIT_2, we arrived here because the
547 * application did a shutdown of the send side. Like the
548 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
549 * a full close, we start a timer to make sure sockets are
550 * not left in FIN_WAIT_2 forever.
551 */
552 if (tp->t_state == TCPS_FIN_WAIT_2)
553 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
554 }
555 return (tp);
556 }
557
558 /*
559 * Sysctl for tcp variables.
560 */
561 int
562 tcp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
563 int *name;
564 u_int namelen;
565 void *oldp;
566 size_t *oldlenp;
567 void *newp;
568 size_t newlen;
569 {
570
571 /* All sysctl names at this level are terminal. */
572 if (namelen != 1)
573 return (ENOTDIR);
574
575 switch (name[0]) {
576 case TCPCTL_RFC1323:
577 return (sysctl_int(oldp, oldlenp, newp, newlen,
578 &tcp_do_rfc1323));
579 case TCPCTL_SENDSPACE:
580 return (sysctl_int(oldp, oldlenp, newp, newlen,
581 &tcp_sendspace));
582 case TCPCTL_RECVSPACE:
583 return (sysctl_int(oldp, oldlenp, newp, newlen,
584 &tcp_recvspace));
585 case TCPCTL_MSSDFLT:
586 return (sysctl_int(oldp, oldlenp, newp, newlen,
587 &tcp_mssdflt));
588 case TCPCTL_SYN_CACHE_LIMIT:
589 return (sysctl_int(oldp, oldlenp, newp, newlen,
590 &tcp_syn_cache_limit));
591 case TCPCTL_SYN_BUCKET_LIMIT:
592 return (sysctl_int(oldp, oldlenp, newp, newlen,
593 &tcp_syn_bucket_limit));
594 case TCPCTL_SYN_CACHE_INTER:
595 return (sysctl_int(oldp, oldlenp, newp, newlen,
596 &tcp_syn_cache_interval));
597 default:
598 return (ENOPROTOOPT);
599 }
600 /* NOTREACHED */
601 }
602