tcp_usrreq.c revision 1.29 1 /* $NetBSD: tcp_usrreq.c,v 1.29 1997/12/11 06:53:06 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_new_iss(tp, sizeof(struct tcpcb), 0);
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 /*
255 * Don't send a window update if we're about to send
256 * a delayed ACK; we'll be able to piggyback.
257 */
258 if (tp->t_flags & TF_DELACK) {
259 tcpstat.tcps_delwin++;
260 break;
261 }
262 (void) tcp_output(tp);
263 break;
264
265 /*
266 * Do a send by putting data in output queue and updating urgent
267 * marker if URG set. Possibly send more data.
268 */
269 case PRU_SEND:
270 if (control && control->m_len) {
271 m_freem(control);
272 m_freem(m);
273 error = EINVAL;
274 break;
275 }
276 sbappend(&so->so_snd, m);
277 error = tcp_output(tp);
278 break;
279
280 /*
281 * Abort the TCP.
282 */
283 case PRU_ABORT:
284 tp = tcp_drop(tp, ECONNABORTED);
285 break;
286
287 case PRU_SENSE:
288 /*
289 * stat: don't bother with a blocksize.
290 */
291 splx(s);
292 return (0);
293
294 case PRU_RCVOOB:
295 if (control && control->m_len) {
296 m_freem(control);
297 m_freem(m);
298 error = EINVAL;
299 break;
300 }
301 if ((so->so_oobmark == 0 &&
302 (so->so_state & SS_RCVATMARK) == 0) ||
303 so->so_options & SO_OOBINLINE ||
304 tp->t_oobflags & TCPOOB_HADDATA) {
305 error = EINVAL;
306 break;
307 }
308 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
309 error = EWOULDBLOCK;
310 break;
311 }
312 m->m_len = 1;
313 *mtod(m, caddr_t) = tp->t_iobc;
314 if (((long)nam & MSG_PEEK) == 0)
315 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
316 break;
317
318 case PRU_SENDOOB:
319 if (sbspace(&so->so_snd) < -512) {
320 m_freem(m);
321 error = ENOBUFS;
322 break;
323 }
324 /*
325 * According to RFC961 (Assigned Protocols),
326 * the urgent pointer points to the last octet
327 * of urgent data. We continue, however,
328 * to consider it to indicate the first octet
329 * of data past the urgent section.
330 * Otherwise, snd_up should be one lower.
331 */
332 sbappend(&so->so_snd, m);
333 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
334 tp->t_force = 1;
335 error = tcp_output(tp);
336 tp->t_force = 0;
337 break;
338
339 case PRU_SOCKADDR:
340 in_setsockaddr(inp, nam);
341 break;
342
343 case PRU_PEERADDR:
344 in_setpeeraddr(inp, nam);
345 break;
346
347 /*
348 * TCP slow timer went off; going through this
349 * routine for tracing's sake.
350 */
351 case PRU_SLOWTIMO:
352 tp = tcp_timers(tp, (long)nam);
353 req |= (long)nam << 8; /* for debug's sake */
354 break;
355
356 default:
357 panic("tcp_usrreq");
358 }
359 if (tp && (so->so_options & SO_DEBUG))
360 tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
361
362 release:
363 splx(s);
364 return (error);
365 }
366
367 int
368 tcp_ctloutput(op, so, level, optname, mp)
369 int op;
370 struct socket *so;
371 int level, optname;
372 struct mbuf **mp;
373 {
374 int error = 0, s;
375 struct inpcb *inp;
376 register struct tcpcb *tp;
377 register struct mbuf *m;
378 register int i;
379
380 s = splsoftnet();
381 inp = sotoinpcb(so);
382 if (inp == NULL) {
383 splx(s);
384 if (op == PRCO_SETOPT && *mp)
385 (void) m_free(*mp);
386 return (ECONNRESET);
387 }
388 if (level != IPPROTO_TCP) {
389 error = ip_ctloutput(op, so, level, optname, mp);
390 splx(s);
391 return (error);
392 }
393 tp = intotcpcb(inp);
394
395 switch (op) {
396
397 case PRCO_SETOPT:
398 m = *mp;
399 switch (optname) {
400
401 case TCP_NODELAY:
402 if (m == NULL || m->m_len < sizeof (int))
403 error = EINVAL;
404 else if (*mtod(m, int *))
405 tp->t_flags |= TF_NODELAY;
406 else
407 tp->t_flags &= ~TF_NODELAY;
408 break;
409
410 case TCP_MAXSEG:
411 if (m && (i = *mtod(m, int *)) > 0 &&
412 i <= tp->t_peermss)
413 tp->t_peermss = i; /* limit on send size */
414 else
415 error = EINVAL;
416 break;
417
418 default:
419 error = ENOPROTOOPT;
420 break;
421 }
422 if (m)
423 (void) m_free(m);
424 break;
425
426 case PRCO_GETOPT:
427 *mp = m = m_get(M_WAIT, MT_SOOPTS);
428 m->m_len = sizeof(int);
429
430 switch (optname) {
431 case TCP_NODELAY:
432 *mtod(m, int *) = tp->t_flags & TF_NODELAY;
433 break;
434 case TCP_MAXSEG:
435 *mtod(m, int *) = tp->t_peermss;
436 break;
437 default:
438 error = ENOPROTOOPT;
439 break;
440 }
441 break;
442 }
443 splx(s);
444 return (error);
445 }
446
447 #ifndef TCP_SENDSPACE
448 #define TCP_SENDSPACE 1024*16;
449 #endif
450 int tcp_sendspace = TCP_SENDSPACE;
451 #ifndef TCP_RECVSPACE
452 #define TCP_RECVSPACE 1024*16;
453 #endif
454 int tcp_recvspace = TCP_RECVSPACE;
455
456 /*
457 * Attach TCP protocol to socket, allocating
458 * internet protocol control block, tcp control block,
459 * bufer space, and entering LISTEN state if to accept connections.
460 */
461 int
462 tcp_attach(so)
463 struct socket *so;
464 {
465 register struct tcpcb *tp;
466 struct inpcb *inp;
467 int error;
468
469 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
470 error = soreserve(so, tcp_sendspace, tcp_recvspace);
471 if (error)
472 return (error);
473 }
474 error = in_pcballoc(so, &tcbtable);
475 if (error)
476 return (error);
477 inp = sotoinpcb(so);
478 tp = tcp_newtcpcb(inp);
479 if (tp == 0) {
480 int nofd = so->so_state & SS_NOFDREF; /* XXX */
481
482 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
483 in_pcbdetach(inp);
484 so->so_state |= nofd;
485 return (ENOBUFS);
486 }
487 tp->t_state = TCPS_CLOSED;
488 return (0);
489 }
490
491 /*
492 * Initiate (or continue) disconnect.
493 * If embryonic state, just send reset (once).
494 * If in ``let data drain'' option and linger null, just drop.
495 * Otherwise (hard), mark socket disconnecting and drop
496 * current input data; switch states based on user close, and
497 * send segment to peer (with FIN).
498 */
499 struct tcpcb *
500 tcp_disconnect(tp)
501 register struct tcpcb *tp;
502 {
503 struct socket *so = tp->t_inpcb->inp_socket;
504
505 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
506 tp = tcp_close(tp);
507 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
508 tp = tcp_drop(tp, 0);
509 else {
510 soisdisconnecting(so);
511 sbflush(&so->so_rcv);
512 tp = tcp_usrclosed(tp);
513 if (tp)
514 (void) tcp_output(tp);
515 }
516 return (tp);
517 }
518
519 /*
520 * User issued close, and wish to trail through shutdown states:
521 * if never received SYN, just forget it. If got a SYN from peer,
522 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
523 * If already got a FIN from peer, then almost done; go to LAST_ACK
524 * state. In all other cases, have already sent FIN to peer (e.g.
525 * after PRU_SHUTDOWN), and just have to play tedious game waiting
526 * for peer to send FIN or not respond to keep-alives, etc.
527 * We can let the user exit from the close as soon as the FIN is acked.
528 */
529 struct tcpcb *
530 tcp_usrclosed(tp)
531 register struct tcpcb *tp;
532 {
533
534 switch (tp->t_state) {
535
536 case TCPS_CLOSED:
537 case TCPS_LISTEN:
538 case TCPS_SYN_SENT:
539 tp->t_state = TCPS_CLOSED;
540 tp = tcp_close(tp);
541 break;
542
543 case TCPS_SYN_RECEIVED:
544 case TCPS_ESTABLISHED:
545 tp->t_state = TCPS_FIN_WAIT_1;
546 break;
547
548 case TCPS_CLOSE_WAIT:
549 tp->t_state = TCPS_LAST_ACK;
550 break;
551 }
552 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
553 soisdisconnected(tp->t_inpcb->inp_socket);
554 /*
555 * If we are in FIN_WAIT_2, we arrived here because the
556 * application did a shutdown of the send side. Like the
557 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
558 * a full close, we start a timer to make sure sockets are
559 * not left in FIN_WAIT_2 forever.
560 */
561 if (tp->t_state == TCPS_FIN_WAIT_2)
562 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
563 }
564 return (tp);
565 }
566
567 /*
568 * Sysctl for tcp variables.
569 */
570 int
571 tcp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
572 int *name;
573 u_int namelen;
574 void *oldp;
575 size_t *oldlenp;
576 void *newp;
577 size_t newlen;
578 {
579
580 /* All sysctl names at this level are terminal. */
581 if (namelen != 1)
582 return (ENOTDIR);
583
584 switch (name[0]) {
585 case TCPCTL_RFC1323:
586 return (sysctl_int(oldp, oldlenp, newp, newlen,
587 &tcp_do_rfc1323));
588 case TCPCTL_SENDSPACE:
589 return (sysctl_int(oldp, oldlenp, newp, newlen,
590 &tcp_sendspace));
591 case TCPCTL_RECVSPACE:
592 return (sysctl_int(oldp, oldlenp, newp, newlen,
593 &tcp_recvspace));
594 case TCPCTL_MSSDFLT:
595 return (sysctl_int(oldp, oldlenp, newp, newlen,
596 &tcp_mssdflt));
597 case TCPCTL_SYN_CACHE_LIMIT:
598 return (sysctl_int(oldp, oldlenp, newp, newlen,
599 &tcp_syn_cache_limit));
600 case TCPCTL_SYN_BUCKET_LIMIT:
601 return (sysctl_int(oldp, oldlenp, newp, newlen,
602 &tcp_syn_bucket_limit));
603 case TCPCTL_SYN_CACHE_INTER:
604 return (sysctl_int(oldp, oldlenp, newp, newlen,
605 &tcp_syn_cache_interval));
606 default:
607 return (ENOPROTOOPT);
608 }
609 /* NOTREACHED */
610 }
611