tcp_usrreq.c revision 1.28 1 /* $NetBSD: tcp_usrreq.c,v 1.28 1997/11/08 02:35:26 kml 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 (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 &&
404 i <= tp->t_peermss)
405 tp->t_peermss = i; /* limit on send size */
406 else
407 error = EINVAL;
408 break;
409
410 default:
411 error = ENOPROTOOPT;
412 break;
413 }
414 if (m)
415 (void) m_free(m);
416 break;
417
418 case PRCO_GETOPT:
419 *mp = m = m_get(M_WAIT, MT_SOOPTS);
420 m->m_len = sizeof(int);
421
422 switch (optname) {
423 case TCP_NODELAY:
424 *mtod(m, int *) = tp->t_flags & TF_NODELAY;
425 break;
426 case TCP_MAXSEG:
427 *mtod(m, int *) = tp->t_peermss;
428 break;
429 default:
430 error = ENOPROTOOPT;
431 break;
432 }
433 break;
434 }
435 splx(s);
436 return (error);
437 }
438
439 #ifndef TCP_SENDSPACE
440 #define TCP_SENDSPACE 1024*16;
441 #endif
442 int tcp_sendspace = TCP_SENDSPACE;
443 #ifndef TCP_RECVSPACE
444 #define TCP_RECVSPACE 1024*16;
445 #endif
446 int tcp_recvspace = TCP_RECVSPACE;
447
448 /*
449 * Attach TCP protocol to socket, allocating
450 * internet protocol control block, tcp control block,
451 * bufer space, and entering LISTEN state if to accept connections.
452 */
453 int
454 tcp_attach(so)
455 struct socket *so;
456 {
457 register struct tcpcb *tp;
458 struct inpcb *inp;
459 int error;
460
461 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
462 error = soreserve(so, tcp_sendspace, tcp_recvspace);
463 if (error)
464 return (error);
465 }
466 error = in_pcballoc(so, &tcbtable);
467 if (error)
468 return (error);
469 inp = sotoinpcb(so);
470 tp = tcp_newtcpcb(inp);
471 if (tp == 0) {
472 int nofd = so->so_state & SS_NOFDREF; /* XXX */
473
474 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
475 in_pcbdetach(inp);
476 so->so_state |= nofd;
477 return (ENOBUFS);
478 }
479 tp->t_state = TCPS_CLOSED;
480 return (0);
481 }
482
483 /*
484 * Initiate (or continue) disconnect.
485 * If embryonic state, just send reset (once).
486 * If in ``let data drain'' option and linger null, just drop.
487 * Otherwise (hard), mark socket disconnecting and drop
488 * current input data; switch states based on user close, and
489 * send segment to peer (with FIN).
490 */
491 struct tcpcb *
492 tcp_disconnect(tp)
493 register struct tcpcb *tp;
494 {
495 struct socket *so = tp->t_inpcb->inp_socket;
496
497 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
498 tp = tcp_close(tp);
499 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
500 tp = tcp_drop(tp, 0);
501 else {
502 soisdisconnecting(so);
503 sbflush(&so->so_rcv);
504 tp = tcp_usrclosed(tp);
505 if (tp)
506 (void) tcp_output(tp);
507 }
508 return (tp);
509 }
510
511 /*
512 * User issued close, and wish to trail through shutdown states:
513 * if never received SYN, just forget it. If got a SYN from peer,
514 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
515 * If already got a FIN from peer, then almost done; go to LAST_ACK
516 * state. In all other cases, have already sent FIN to peer (e.g.
517 * after PRU_SHUTDOWN), and just have to play tedious game waiting
518 * for peer to send FIN or not respond to keep-alives, etc.
519 * We can let the user exit from the close as soon as the FIN is acked.
520 */
521 struct tcpcb *
522 tcp_usrclosed(tp)
523 register struct tcpcb *tp;
524 {
525
526 switch (tp->t_state) {
527
528 case TCPS_CLOSED:
529 case TCPS_LISTEN:
530 case TCPS_SYN_SENT:
531 tp->t_state = TCPS_CLOSED;
532 tp = tcp_close(tp);
533 break;
534
535 case TCPS_SYN_RECEIVED:
536 case TCPS_ESTABLISHED:
537 tp->t_state = TCPS_FIN_WAIT_1;
538 break;
539
540 case TCPS_CLOSE_WAIT:
541 tp->t_state = TCPS_LAST_ACK;
542 break;
543 }
544 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
545 soisdisconnected(tp->t_inpcb->inp_socket);
546 /*
547 * If we are in FIN_WAIT_2, we arrived here because the
548 * application did a shutdown of the send side. Like the
549 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
550 * a full close, we start a timer to make sure sockets are
551 * not left in FIN_WAIT_2 forever.
552 */
553 if (tp->t_state == TCPS_FIN_WAIT_2)
554 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
555 }
556 return (tp);
557 }
558
559 /*
560 * Sysctl for tcp variables.
561 */
562 int
563 tcp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
564 int *name;
565 u_int namelen;
566 void *oldp;
567 size_t *oldlenp;
568 void *newp;
569 size_t newlen;
570 {
571
572 /* All sysctl names at this level are terminal. */
573 if (namelen != 1)
574 return (ENOTDIR);
575
576 switch (name[0]) {
577 case TCPCTL_RFC1323:
578 return (sysctl_int(oldp, oldlenp, newp, newlen,
579 &tcp_do_rfc1323));
580 case TCPCTL_SENDSPACE:
581 return (sysctl_int(oldp, oldlenp, newp, newlen,
582 &tcp_sendspace));
583 case TCPCTL_RECVSPACE:
584 return (sysctl_int(oldp, oldlenp, newp, newlen,
585 &tcp_recvspace));
586 case TCPCTL_MSSDFLT:
587 return (sysctl_int(oldp, oldlenp, newp, newlen,
588 &tcp_mssdflt));
589 case TCPCTL_SYN_CACHE_LIMIT:
590 return (sysctl_int(oldp, oldlenp, newp, newlen,
591 &tcp_syn_cache_limit));
592 case TCPCTL_SYN_BUCKET_LIMIT:
593 return (sysctl_int(oldp, oldlenp, newp, newlen,
594 &tcp_syn_bucket_limit));
595 case TCPCTL_SYN_CACHE_INTER:
596 return (sysctl_int(oldp, oldlenp, newp, newlen,
597 &tcp_syn_cache_interval));
598 default:
599 return (ENOPROTOOPT);
600 }
601 /* NOTREACHED */
602 }
603