tcp_usrreq.c revision 1.115 1 /* $NetBSD: tcp_usrreq.c,v 1.115 2006/04/14 23:17:24 christos Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * 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. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 1997, 1998, 2005 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
38 * Facility, NASA Ames Research Center.
39 * This code is derived from software contributed to The NetBSD Foundation
40 * by Charles M. Hannum.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the NetBSD
53 * Foundation, Inc. and its contributors.
54 * 4. Neither the name of The NetBSD Foundation nor the names of its
55 * contributors may be used to endorse or promote products derived
56 * from this software without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
59 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
60 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
61 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
62 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
67 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68 * POSSIBILITY OF SUCH DAMAGE.
69 */
70
71 /*
72 * Copyright (c) 1982, 1986, 1988, 1993, 1995
73 * The Regents of the University of California. All rights reserved.
74 *
75 * Redistribution and use in source and binary forms, with or without
76 * modification, are permitted provided that the following conditions
77 * are met:
78 * 1. Redistributions of source code must retain the above copyright
79 * notice, this list of conditions and the following disclaimer.
80 * 2. Redistributions in binary form must reproduce the above copyright
81 * notice, this list of conditions and the following disclaimer in the
82 * documentation and/or other materials provided with the distribution.
83 * 3. Neither the name of the University nor the names of its contributors
84 * may be used to endorse or promote products derived from this software
85 * without specific prior written permission.
86 *
87 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
88 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
89 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
90 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
91 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
92 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
93 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
94 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
95 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
96 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
97 * SUCH DAMAGE.
98 *
99 * @(#)tcp_usrreq.c 8.5 (Berkeley) 6/21/95
100 */
101
102 #include <sys/cdefs.h>
103 __KERNEL_RCSID(0, "$NetBSD: tcp_usrreq.c,v 1.115 2006/04/14 23:17:24 christos Exp $");
104
105 #include "opt_inet.h"
106 #include "opt_ipsec.h"
107 #include "opt_tcp_debug.h"
108 #include "opt_mbuftrace.h"
109
110 #include <sys/param.h>
111 #include <sys/systm.h>
112 #include <sys/kernel.h>
113 #include <sys/malloc.h>
114 #include <sys/mbuf.h>
115 #include <sys/socket.h>
116 #include <sys/socketvar.h>
117 #include <sys/protosw.h>
118 #include <sys/errno.h>
119 #include <sys/stat.h>
120 #include <sys/proc.h>
121 #include <sys/domain.h>
122 #include <sys/sysctl.h>
123
124 #include <net/if.h>
125 #include <net/route.h>
126
127 #include <netinet/in.h>
128 #include <netinet/in_systm.h>
129 #include <netinet/in_var.h>
130 #include <netinet/ip.h>
131 #include <netinet/in_pcb.h>
132 #include <netinet/ip_var.h>
133 #include <netinet/in_offload.h>
134
135 #ifdef INET6
136 #ifndef INET
137 #include <netinet/in.h>
138 #endif
139 #include <netinet/ip6.h>
140 #include <netinet6/in6_pcb.h>
141 #include <netinet6/ip6_var.h>
142 #endif
143
144 #include <netinet/tcp.h>
145 #include <netinet/tcp_fsm.h>
146 #include <netinet/tcp_seq.h>
147 #include <netinet/tcp_timer.h>
148 #include <netinet/tcp_var.h>
149 #include <netinet/tcpip.h>
150 #include <netinet/tcp_debug.h>
151
152 #include "opt_tcp_space.h"
153
154 #ifdef IPSEC
155 #include <netinet6/ipsec.h>
156 #endif /*IPSEC*/
157
158 /*
159 * TCP protocol interface to socket abstraction.
160 */
161
162 /*
163 * Process a TCP user request for TCP tb. If this is a send request
164 * then m is the mbuf chain of send data. If this is a timer expiration
165 * (called from the software clock routine), then timertype tells which timer.
166 */
167 /*ARGSUSED*/
168 int
169 tcp_usrreq(struct socket *so, int req,
170 struct mbuf *m, struct mbuf *nam, struct mbuf *control, struct lwp *l)
171 {
172 struct inpcb *inp;
173 #ifdef INET6
174 struct in6pcb *in6p;
175 #endif
176 struct tcpcb *tp = NULL;
177 struct proc *p;
178 int s;
179 int error = 0;
180 #ifdef TCP_DEBUG
181 int ostate = 0;
182 #endif
183 int family; /* family of the socket */
184
185 p = l ? l->l_proc : NULL;
186 family = so->so_proto->pr_domain->dom_family;
187
188 if (req == PRU_CONTROL) {
189 switch (family) {
190 #ifdef INET
191 case PF_INET:
192 return (in_control(so, (long)m, (caddr_t)nam,
193 (struct ifnet *)control, p));
194 #endif
195 #ifdef INET6
196 case PF_INET6:
197 return (in6_control(so, (long)m, (caddr_t)nam,
198 (struct ifnet *)control, p));
199 #endif
200 default:
201 return EAFNOSUPPORT;
202 }
203 }
204
205 if (req == PRU_PURGEIF) {
206 switch (family) {
207 #ifdef INET
208 case PF_INET:
209 in_pcbpurgeif0(&tcbtable, (struct ifnet *)control);
210 in_purgeif((struct ifnet *)control);
211 in_pcbpurgeif(&tcbtable, (struct ifnet *)control);
212 break;
213 #endif
214 #ifdef INET6
215 case PF_INET6:
216 in6_pcbpurgeif0(&tcbtable, (struct ifnet *)control);
217 in6_purgeif((struct ifnet *)control);
218 in6_pcbpurgeif(&tcbtable, (struct ifnet *)control);
219 break;
220 #endif
221 default:
222 return (EAFNOSUPPORT);
223 }
224 return (0);
225 }
226
227 s = splsoftnet();
228 switch (family) {
229 #ifdef INET
230 case PF_INET:
231 inp = sotoinpcb(so);
232 #ifdef INET6
233 in6p = NULL;
234 #endif
235 break;
236 #endif
237 #ifdef INET6
238 case PF_INET6:
239 inp = NULL;
240 in6p = sotoin6pcb(so);
241 break;
242 #endif
243 default:
244 splx(s);
245 return EAFNOSUPPORT;
246 }
247
248 #ifdef DIAGNOSTIC
249 #ifdef INET6
250 if (inp && in6p)
251 panic("tcp_usrreq: both inp and in6p set to non-NULL");
252 #endif
253 if (req != PRU_SEND && req != PRU_SENDOOB && control)
254 panic("tcp_usrreq: unexpected control mbuf");
255 #endif
256 /*
257 * When a TCP is attached to a socket, then there will be
258 * a (struct inpcb) pointed at by the socket, and this
259 * structure will point at a subsidary (struct tcpcb).
260 */
261 #ifndef INET6
262 if (inp == 0 && req != PRU_ATTACH)
263 #else
264 if ((inp == 0 && in6p == 0) && req != PRU_ATTACH)
265 #endif
266 {
267 error = EINVAL;
268 goto release;
269 }
270 #ifdef INET
271 if (inp) {
272 tp = intotcpcb(inp);
273 /* WHAT IF TP IS 0? */
274 #ifdef KPROF
275 tcp_acounts[tp->t_state][req]++;
276 #endif
277 #ifdef TCP_DEBUG
278 ostate = tp->t_state;
279 #endif
280 }
281 #endif
282 #ifdef INET6
283 if (in6p) {
284 tp = in6totcpcb(in6p);
285 /* WHAT IF TP IS 0? */
286 #ifdef KPROF
287 tcp_acounts[tp->t_state][req]++;
288 #endif
289 #ifdef TCP_DEBUG
290 ostate = tp->t_state;
291 #endif
292 }
293 #endif
294
295 switch (req) {
296
297 /*
298 * TCP attaches to socket via PRU_ATTACH, reserving space,
299 * and an internet control block.
300 */
301 case PRU_ATTACH:
302 #ifndef INET6
303 if (inp != 0)
304 #else
305 if (inp != 0 || in6p != 0)
306 #endif
307 {
308 error = EISCONN;
309 break;
310 }
311 error = tcp_attach(so);
312 if (error)
313 break;
314 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
315 so->so_linger = TCP_LINGERTIME;
316 tp = sototcpcb(so);
317 break;
318
319 /*
320 * PRU_DETACH detaches the TCP protocol from the socket.
321 */
322 case PRU_DETACH:
323 tp = tcp_disconnect(tp);
324 break;
325
326 /*
327 * Give the socket an address.
328 */
329 case PRU_BIND:
330 switch (family) {
331 #ifdef INET
332 case PF_INET:
333 error = in_pcbbind(inp, nam, p);
334 break;
335 #endif
336 #ifdef INET6
337 case PF_INET6:
338 error = in6_pcbbind(in6p, nam, p);
339 if (!error) {
340 /* mapped addr case */
341 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
342 tp->t_family = AF_INET;
343 else
344 tp->t_family = AF_INET6;
345 }
346 break;
347 #endif
348 }
349 break;
350
351 /*
352 * Prepare to accept connections.
353 */
354 case PRU_LISTEN:
355 #ifdef INET
356 if (inp && inp->inp_lport == 0) {
357 error = in_pcbbind(inp, (struct mbuf *)0,
358 (struct proc *)0);
359 if (error)
360 break;
361 }
362 #endif
363 #ifdef INET6
364 if (in6p && in6p->in6p_lport == 0) {
365 error = in6_pcbbind(in6p, (struct mbuf *)0,
366 (struct proc *)0);
367 if (error)
368 break;
369 }
370 #endif
371 tp->t_state = TCPS_LISTEN;
372 break;
373
374 /*
375 * Initiate connection to peer.
376 * Create a template for use in transmissions on this connection.
377 * Enter SYN_SENT state, and mark socket as connecting.
378 * Start keep-alive timer, and seed output sequence space.
379 * Send initial segment on connection.
380 */
381 case PRU_CONNECT:
382 #ifdef INET
383 if (inp) {
384 if (inp->inp_lport == 0) {
385 error = in_pcbbind(inp, (struct mbuf *)0,
386 (struct proc *)0);
387 if (error)
388 break;
389 }
390 error = in_pcbconnect(inp, nam, p);
391 }
392 #endif
393 #ifdef INET6
394 if (in6p) {
395 if (in6p->in6p_lport == 0) {
396 error = in6_pcbbind(in6p, (struct mbuf *)0,
397 (struct proc *)0);
398 if (error)
399 break;
400 }
401 error = in6_pcbconnect(in6p, nam, p);
402 if (!error) {
403 /* mapped addr case */
404 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
405 tp->t_family = AF_INET;
406 else
407 tp->t_family = AF_INET6;
408 }
409 }
410 #endif
411 if (error)
412 break;
413 tp->t_template = tcp_template(tp);
414 if (tp->t_template == 0) {
415 #ifdef INET
416 if (inp)
417 in_pcbdisconnect(inp);
418 #endif
419 #ifdef INET6
420 if (in6p)
421 in6_pcbdisconnect(in6p);
422 #endif
423 error = ENOBUFS;
424 break;
425 }
426 /* Compute window scaling to request. */
427 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
428 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
429 tp->request_r_scale++;
430 soisconnecting(so);
431 tcpstat.tcps_connattempt++;
432 tp->t_state = TCPS_SYN_SENT;
433 TCP_TIMER_ARM(tp, TCPT_KEEP, TCPTV_KEEP_INIT);
434 tp->iss = tcp_new_iss(tp, 0);
435 tcp_sendseqinit(tp);
436 error = tcp_output(tp);
437 break;
438
439 /*
440 * Create a TCP connection between two sockets.
441 */
442 case PRU_CONNECT2:
443 error = EOPNOTSUPP;
444 break;
445
446 /*
447 * Initiate disconnect from peer.
448 * If connection never passed embryonic stage, just drop;
449 * else if don't need to let data drain, then can just drop anyways,
450 * else have to begin TCP shutdown process: mark socket disconnecting,
451 * drain unread data, state switch to reflect user close, and
452 * send segment (e.g. FIN) to peer. Socket will be really disconnected
453 * when peer sends FIN and acks ours.
454 *
455 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
456 */
457 case PRU_DISCONNECT:
458 tp = tcp_disconnect(tp);
459 break;
460
461 /*
462 * Accept a connection. Essentially all the work is
463 * done at higher levels; just return the address
464 * of the peer, storing through addr.
465 */
466 case PRU_ACCEPT:
467 #ifdef INET
468 if (inp)
469 in_setpeeraddr(inp, nam);
470 #endif
471 #ifdef INET6
472 if (in6p)
473 in6_setpeeraddr(in6p, nam);
474 #endif
475 break;
476
477 /*
478 * Mark the connection as being incapable of further output.
479 */
480 case PRU_SHUTDOWN:
481 socantsendmore(so);
482 tp = tcp_usrclosed(tp);
483 if (tp)
484 error = tcp_output(tp);
485 break;
486
487 /*
488 * After a receive, possibly send window update to peer.
489 */
490 case PRU_RCVD:
491 /*
492 * soreceive() calls this function when a user receives
493 * ancillary data on a listening socket. We don't call
494 * tcp_output in such a case, since there is no header
495 * template for a listening socket and hence the kernel
496 * will panic.
497 */
498 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) != 0)
499 (void) tcp_output(tp);
500 break;
501
502 /*
503 * Do a send by putting data in output queue and updating urgent
504 * marker if URG set. Possibly send more data.
505 */
506 case PRU_SEND:
507 if (control && control->m_len) {
508 m_freem(control);
509 m_freem(m);
510 error = EINVAL;
511 break;
512 }
513 sbappendstream(&so->so_snd, m);
514 error = tcp_output(tp);
515 break;
516
517 /*
518 * Abort the TCP.
519 */
520 case PRU_ABORT:
521 tp = tcp_drop(tp, ECONNABORTED);
522 break;
523
524 case PRU_SENSE:
525 /*
526 * stat: don't bother with a blocksize.
527 */
528 splx(s);
529 return (0);
530
531 case PRU_RCVOOB:
532 if (control && control->m_len) {
533 m_freem(control);
534 m_freem(m);
535 error = EINVAL;
536 break;
537 }
538 if ((so->so_oobmark == 0 &&
539 (so->so_state & SS_RCVATMARK) == 0) ||
540 so->so_options & SO_OOBINLINE ||
541 tp->t_oobflags & TCPOOB_HADDATA) {
542 error = EINVAL;
543 break;
544 }
545 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
546 error = EWOULDBLOCK;
547 break;
548 }
549 m->m_len = 1;
550 *mtod(m, caddr_t) = tp->t_iobc;
551 if (((long)nam & MSG_PEEK) == 0)
552 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
553 break;
554
555 case PRU_SENDOOB:
556 if (sbspace(&so->so_snd) < -512) {
557 m_freem(m);
558 error = ENOBUFS;
559 break;
560 }
561 /*
562 * According to RFC961 (Assigned Protocols),
563 * the urgent pointer points to the last octet
564 * of urgent data. We continue, however,
565 * to consider it to indicate the first octet
566 * of data past the urgent section.
567 * Otherwise, snd_up should be one lower.
568 */
569 sbappendstream(&so->so_snd, m);
570 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
571 tp->t_force = 1;
572 error = tcp_output(tp);
573 tp->t_force = 0;
574 break;
575
576 case PRU_SOCKADDR:
577 #ifdef INET
578 if (inp)
579 in_setsockaddr(inp, nam);
580 #endif
581 #ifdef INET6
582 if (in6p)
583 in6_setsockaddr(in6p, nam);
584 #endif
585 break;
586
587 case PRU_PEERADDR:
588 #ifdef INET
589 if (inp)
590 in_setpeeraddr(inp, nam);
591 #endif
592 #ifdef INET6
593 if (in6p)
594 in6_setpeeraddr(in6p, nam);
595 #endif
596 break;
597
598 default:
599 panic("tcp_usrreq");
600 }
601 #ifdef TCP_DEBUG
602 if (tp && (so->so_options & SO_DEBUG))
603 tcp_trace(TA_USER, ostate, tp, NULL, req);
604 #endif
605
606 release:
607 splx(s);
608 return (error);
609 }
610
611 int
612 tcp_ctloutput(int op, struct socket *so, int level, int optname,
613 struct mbuf **mp)
614 {
615 int error = 0, s;
616 struct inpcb *inp;
617 #ifdef INET6
618 struct in6pcb *in6p;
619 #endif
620 struct tcpcb *tp;
621 struct mbuf *m;
622 int i;
623 int family; /* family of the socket */
624
625 family = so->so_proto->pr_domain->dom_family;
626
627 s = splsoftnet();
628 switch (family) {
629 #ifdef INET
630 case PF_INET:
631 inp = sotoinpcb(so);
632 #ifdef INET6
633 in6p = NULL;
634 #endif
635 break;
636 #endif
637 #ifdef INET6
638 case PF_INET6:
639 inp = NULL;
640 in6p = sotoin6pcb(so);
641 break;
642 #endif
643 default:
644 splx(s);
645 return EAFNOSUPPORT;
646 }
647 #ifndef INET6
648 if (inp == NULL)
649 #else
650 if (inp == NULL && in6p == NULL)
651 #endif
652 {
653 splx(s);
654 if (op == PRCO_SETOPT && *mp)
655 (void) m_free(*mp);
656 return (ECONNRESET);
657 }
658 if (level != IPPROTO_TCP) {
659 switch (family) {
660 #ifdef INET
661 case PF_INET:
662 error = ip_ctloutput(op, so, level, optname, mp);
663 break;
664 #endif
665 #ifdef INET6
666 case PF_INET6:
667 error = ip6_ctloutput(op, so, level, optname, mp);
668 break;
669 #endif
670 }
671 splx(s);
672 return (error);
673 }
674 if (inp)
675 tp = intotcpcb(inp);
676 #ifdef INET6
677 else if (in6p)
678 tp = in6totcpcb(in6p);
679 #endif
680 else
681 tp = NULL;
682
683 switch (op) {
684
685 case PRCO_SETOPT:
686 m = *mp;
687 switch (optname) {
688
689 #ifdef TCP_SIGNATURE
690 case TCP_MD5SIG:
691 if (m == NULL || m->m_len < sizeof (int))
692 error = EINVAL;
693 if (error)
694 break;
695 if (*mtod(m, int *) > 0)
696 tp->t_flags |= TF_SIGNATURE;
697 else
698 tp->t_flags &= ~TF_SIGNATURE;
699 break;
700 #endif /* TCP_SIGNATURE */
701
702 case TCP_NODELAY:
703 if (m == NULL || m->m_len < sizeof (int))
704 error = EINVAL;
705 else if (*mtod(m, int *))
706 tp->t_flags |= TF_NODELAY;
707 else
708 tp->t_flags &= ~TF_NODELAY;
709 break;
710
711 case TCP_MAXSEG:
712 if (m && (i = *mtod(m, int *)) > 0 &&
713 i <= tp->t_peermss)
714 tp->t_peermss = i; /* limit on send size */
715 else
716 error = EINVAL;
717 break;
718
719 default:
720 error = ENOPROTOOPT;
721 break;
722 }
723 if (m)
724 (void) m_free(m);
725 break;
726
727 case PRCO_GETOPT:
728 *mp = m = m_get(M_WAIT, MT_SOOPTS);
729 m->m_len = sizeof(int);
730 MCLAIM(m, so->so_mowner);
731
732 switch (optname) {
733 #ifdef TCP_SIGNATURE
734 case TCP_MD5SIG:
735 *mtod(m, int *) = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
736 break;
737 #endif
738 case TCP_NODELAY:
739 *mtod(m, int *) = tp->t_flags & TF_NODELAY;
740 break;
741 case TCP_MAXSEG:
742 *mtod(m, int *) = tp->t_peermss;
743 break;
744 default:
745 error = ENOPROTOOPT;
746 break;
747 }
748 break;
749 }
750 splx(s);
751 return (error);
752 }
753
754 #ifndef TCP_SENDSPACE
755 #define TCP_SENDSPACE 1024*32
756 #endif
757 int tcp_sendspace = TCP_SENDSPACE;
758 #ifndef TCP_RECVSPACE
759 #define TCP_RECVSPACE 1024*32
760 #endif
761 int tcp_recvspace = TCP_RECVSPACE;
762
763 /*
764 * Attach TCP protocol to socket, allocating
765 * internet protocol control block, tcp control block,
766 * bufer space, and entering LISTEN state if to accept connections.
767 */
768 int
769 tcp_attach(struct socket *so)
770 {
771 struct tcpcb *tp;
772 struct inpcb *inp;
773 #ifdef INET6
774 struct in6pcb *in6p;
775 #endif
776 int error;
777 int family; /* family of the socket */
778
779 family = so->so_proto->pr_domain->dom_family;
780
781 #ifdef MBUFTRACE
782 so->so_mowner = &tcp_mowner;
783 so->so_rcv.sb_mowner = &tcp_rx_mowner;
784 so->so_snd.sb_mowner = &tcp_tx_mowner;
785 #endif
786 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
787 error = soreserve(so, tcp_sendspace, tcp_recvspace);
788 if (error)
789 return (error);
790 }
791 switch (family) {
792 #ifdef INET
793 case PF_INET:
794 error = in_pcballoc(so, &tcbtable);
795 if (error)
796 return (error);
797 inp = sotoinpcb(so);
798 #ifdef INET6
799 in6p = NULL;
800 #endif
801 break;
802 #endif
803 #ifdef INET6
804 case PF_INET6:
805 error = in6_pcballoc(so, &tcbtable);
806 if (error)
807 return (error);
808 inp = NULL;
809 in6p = sotoin6pcb(so);
810 break;
811 #endif
812 default:
813 return EAFNOSUPPORT;
814 }
815 if (inp)
816 tp = tcp_newtcpcb(family, (void *)inp);
817 #ifdef INET6
818 else if (in6p)
819 tp = tcp_newtcpcb(family, (void *)in6p);
820 #endif
821 else
822 tp = NULL;
823
824 if (tp == 0) {
825 int nofd = so->so_state & SS_NOFDREF; /* XXX */
826
827 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
828 #ifdef INET
829 if (inp)
830 in_pcbdetach(inp);
831 #endif
832 #ifdef INET6
833 if (in6p)
834 in6_pcbdetach(in6p);
835 #endif
836 so->so_state |= nofd;
837 return (ENOBUFS);
838 }
839 tp->t_state = TCPS_CLOSED;
840 return (0);
841 }
842
843 /*
844 * Initiate (or continue) disconnect.
845 * If embryonic state, just send reset (once).
846 * If in ``let data drain'' option and linger null, just drop.
847 * Otherwise (hard), mark socket disconnecting and drop
848 * current input data; switch states based on user close, and
849 * send segment to peer (with FIN).
850 */
851 struct tcpcb *
852 tcp_disconnect(struct tcpcb *tp)
853 {
854 struct socket *so;
855
856 if (tp->t_inpcb)
857 so = tp->t_inpcb->inp_socket;
858 #ifdef INET6
859 else if (tp->t_in6pcb)
860 so = tp->t_in6pcb->in6p_socket;
861 #endif
862 else
863 so = NULL;
864
865 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
866 tp = tcp_close(tp);
867 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
868 tp = tcp_drop(tp, 0);
869 else {
870 soisdisconnecting(so);
871 sbflush(&so->so_rcv);
872 tp = tcp_usrclosed(tp);
873 if (tp)
874 (void) tcp_output(tp);
875 }
876 return (tp);
877 }
878
879 /*
880 * User issued close, and wish to trail through shutdown states:
881 * if never received SYN, just forget it. If got a SYN from peer,
882 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
883 * If already got a FIN from peer, then almost done; go to LAST_ACK
884 * state. In all other cases, have already sent FIN to peer (e.g.
885 * after PRU_SHUTDOWN), and just have to play tedious game waiting
886 * for peer to send FIN or not respond to keep-alives, etc.
887 * We can let the user exit from the close as soon as the FIN is acked.
888 */
889 struct tcpcb *
890 tcp_usrclosed(struct tcpcb *tp)
891 {
892
893 switch (tp->t_state) {
894
895 case TCPS_CLOSED:
896 case TCPS_LISTEN:
897 case TCPS_SYN_SENT:
898 tp->t_state = TCPS_CLOSED;
899 tp = tcp_close(tp);
900 break;
901
902 case TCPS_SYN_RECEIVED:
903 case TCPS_ESTABLISHED:
904 tp->t_state = TCPS_FIN_WAIT_1;
905 break;
906
907 case TCPS_CLOSE_WAIT:
908 tp->t_state = TCPS_LAST_ACK;
909 break;
910 }
911 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
912 struct socket *so;
913 if (tp->t_inpcb)
914 so = tp->t_inpcb->inp_socket;
915 #ifdef INET6
916 else if (tp->t_in6pcb)
917 so = tp->t_in6pcb->in6p_socket;
918 #endif
919 else
920 so = NULL;
921 if (so)
922 soisdisconnected(so);
923 /*
924 * If we are in FIN_WAIT_2, we arrived here because the
925 * application did a shutdown of the send side. Like the
926 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
927 * a full close, we start a timer to make sure sockets are
928 * not left in FIN_WAIT_2 forever.
929 */
930 if ((tp->t_state == TCPS_FIN_WAIT_2) && (tcp_maxidle > 0))
931 TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_maxidle);
932 }
933 return (tp);
934 }
935
936 /*
937 * sysctl helper routine for net.inet.ip.mssdflt. it can't be less
938 * than 32.
939 */
940 static int
941 sysctl_net_inet_tcp_mssdflt(SYSCTLFN_ARGS)
942 {
943 int error, mssdflt;
944 struct sysctlnode node;
945
946 mssdflt = tcp_mssdflt;
947 node = *rnode;
948 node.sysctl_data = &mssdflt;
949 error = sysctl_lookup(SYSCTLFN_CALL(&node));
950 if (error || newp == NULL)
951 return (error);
952
953 if (mssdflt < 32)
954 return (EINVAL);
955 tcp_mssdflt = mssdflt;
956
957 return (0);
958 }
959
960 /*
961 * sysctl helper routine for setting port related values under
962 * net.inet.ip and net.inet6.ip6. does basic range checking and does
963 * additional checks for each type. this code has placed in
964 * tcp_input.c since INET and INET6 both use the same tcp code.
965 *
966 * this helper is not static so that both inet and inet6 can use it.
967 */
968 int
969 sysctl_net_inet_ip_ports(SYSCTLFN_ARGS)
970 {
971 int error, tmp;
972 int apmin, apmax;
973 #ifndef IPNOPRIVPORTS
974 int lpmin, lpmax;
975 #endif /* IPNOPRIVPORTS */
976 struct sysctlnode node;
977
978 if (namelen != 0)
979 return (EINVAL);
980
981 switch (name[-3]) {
982 #ifdef INET
983 case PF_INET:
984 apmin = anonportmin;
985 apmax = anonportmax;
986 #ifndef IPNOPRIVPORTS
987 lpmin = lowportmin;
988 lpmax = lowportmax;
989 #endif /* IPNOPRIVPORTS */
990 break;
991 #endif /* INET */
992 #ifdef INET6
993 case PF_INET6:
994 apmin = ip6_anonportmin;
995 apmax = ip6_anonportmax;
996 #ifndef IPNOPRIVPORTS
997 lpmin = ip6_lowportmin;
998 lpmax = ip6_lowportmax;
999 #endif /* IPNOPRIVPORTS */
1000 break;
1001 #endif /* INET6 */
1002 default:
1003 return (EINVAL);
1004 }
1005
1006 /*
1007 * insert temporary copy into node, perform lookup on
1008 * temporary, then restore pointer
1009 */
1010 node = *rnode;
1011 tmp = *(int*)rnode->sysctl_data;
1012 node.sysctl_data = &tmp;
1013 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1014 if (error || newp == NULL)
1015 return (error);
1016
1017 /*
1018 * simple port range check
1019 */
1020 if (tmp < 0 || tmp > 65535)
1021 return (EINVAL);
1022
1023 /*
1024 * per-node range checks
1025 */
1026 switch (rnode->sysctl_num) {
1027 case IPCTL_ANONPORTMIN:
1028 if (tmp >= apmax)
1029 return (EINVAL);
1030 #ifndef IPNOPRIVPORTS
1031 if (tmp < IPPORT_RESERVED)
1032 return (EINVAL);
1033 #endif /* IPNOPRIVPORTS */
1034 break;
1035
1036 case IPCTL_ANONPORTMAX:
1037 if (apmin >= tmp)
1038 return (EINVAL);
1039 #ifndef IPNOPRIVPORTS
1040 if (tmp < IPPORT_RESERVED)
1041 return (EINVAL);
1042 #endif /* IPNOPRIVPORTS */
1043 break;
1044
1045 #ifndef IPNOPRIVPORTS
1046 case IPCTL_LOWPORTMIN:
1047 if (tmp >= lpmax ||
1048 tmp > IPPORT_RESERVEDMAX ||
1049 tmp < IPPORT_RESERVEDMIN)
1050 return (EINVAL);
1051 break;
1052
1053 case IPCTL_LOWPORTMAX:
1054 if (lpmin >= tmp ||
1055 tmp > IPPORT_RESERVEDMAX ||
1056 tmp < IPPORT_RESERVEDMIN)
1057 return (EINVAL);
1058 break;
1059 #endif /* IPNOPRIVPORTS */
1060
1061 default:
1062 return (EINVAL);
1063 }
1064
1065 *(int*)rnode->sysctl_data = tmp;
1066
1067 return (0);
1068 }
1069
1070 /*
1071 * sysctl helper routine for the net.inet.tcp.ident and
1072 * net.inet6.tcp6.ident nodes. contains backwards compat code for the
1073 * old way of looking up the ident information for ipv4 which involves
1074 * stuffing the port/addr pairs into the mib lookup.
1075 */
1076 static int
1077 sysctl_net_inet_tcp_ident(SYSCTLFN_ARGS)
1078 {
1079 #ifdef INET
1080 struct inpcb *inb;
1081 struct sockaddr_in *si4[2];
1082 #endif /* INET */
1083 #ifdef INET6
1084 struct in6pcb *in6b;
1085 struct sockaddr_in6 *si6[2];
1086 #endif /* INET6 */
1087 struct sockaddr_storage sa[2];
1088 struct socket *sockp;
1089 size_t sz;
1090 uid_t uid;
1091 int error, pf;
1092
1093 if (namelen != 4 && namelen != 0)
1094 return (EINVAL);
1095 if (name[-2] != IPPROTO_TCP)
1096 return (EINVAL);
1097 pf = name[-3];
1098
1099 /* old style lookup, ipv4 only */
1100 if (namelen == 4) {
1101 #ifdef INET
1102 struct in_addr laddr, raddr;
1103 u_int lport, rport;
1104
1105 if (pf != PF_INET)
1106 return (EPROTONOSUPPORT);
1107 raddr.s_addr = (uint32_t)name[0];
1108 rport = (u_int)name[1];
1109 laddr.s_addr = (uint32_t)name[2];
1110 lport = (u_int)name[3];
1111 inb = in_pcblookup_connect(&tcbtable, raddr, rport,
1112 laddr, lport);
1113 if (inb == NULL || (sockp = inb->inp_socket) == NULL)
1114 return (ESRCH);
1115 uid = sockp->so_uidinfo->ui_uid;
1116 if (oldp) {
1117 sz = MIN(sizeof(uid), *oldlenp);
1118 error = copyout(&uid, oldp, sz);
1119 if (error)
1120 return (error);
1121 }
1122 *oldlenp = sizeof(uid);
1123 return (0);
1124 #else /* INET */
1125 return (EINVAL);
1126 #endif /* INET */
1127 }
1128
1129 if (newp == NULL || newlen != sizeof(sa))
1130 return (EINVAL);
1131 error = copyin(newp, &sa, newlen);
1132 if (error)
1133 return (error);
1134
1135 /*
1136 * requested families must match
1137 */
1138 if (pf != sa[0].ss_family || sa[0].ss_family != sa[1].ss_family)
1139 return (EINVAL);
1140
1141 switch (pf) {
1142 #ifdef INET
1143 case PF_INET:
1144 si4[0] = (struct sockaddr_in*)&sa[0];
1145 si4[1] = (struct sockaddr_in*)&sa[1];
1146 if (si4[0]->sin_len != sizeof(*si4[0]) ||
1147 si4[0]->sin_len != si4[1]->sin_len)
1148 return (EINVAL);
1149 inb = in_pcblookup_connect(&tcbtable,
1150 si4[0]->sin_addr, si4[0]->sin_port,
1151 si4[1]->sin_addr, si4[1]->sin_port);
1152 if (inb == NULL || (sockp = inb->inp_socket) == NULL)
1153 return (ESRCH);
1154 break;
1155 #endif /* INET */
1156 #ifdef INET6
1157 case PF_INET6:
1158 si6[0] = (struct sockaddr_in6*)&sa[0];
1159 si6[1] = (struct sockaddr_in6*)&sa[1];
1160 if (si6[0]->sin6_len != sizeof(*si6[0]) ||
1161 si6[0]->sin6_len != si6[1]->sin6_len)
1162 return (EINVAL);
1163 in6b = in6_pcblookup_connect(&tcbtable,
1164 &si6[0]->sin6_addr, si6[0]->sin6_port,
1165 &si6[1]->sin6_addr, si6[1]->sin6_port, 0);
1166 if (in6b == NULL || (sockp = in6b->in6p_socket) == NULL)
1167 return (ESRCH);
1168 break;
1169 #endif /* INET6 */
1170 default:
1171 return (EPROTONOSUPPORT);
1172 }
1173 *oldlenp = sizeof(uid);
1174
1175 uid = sockp->so_uidinfo->ui_uid;
1176 if (oldp) {
1177 sz = MIN(sizeof(uid), *oldlenp);
1178 error = copyout(&uid, oldp, sz);
1179 if (error)
1180 return (error);
1181 }
1182 *oldlenp = sizeof(uid);
1183
1184 return (0);
1185 }
1186
1187 /*
1188 * sysctl helper for the inet and inet6 pcblists. handles tcp/udp and
1189 * inet/inet6, as well as raw pcbs for each. specifically not
1190 * declared static so that raw sockets and udp/udp6 can use it as
1191 * well.
1192 */
1193 int
1194 sysctl_inpcblist(SYSCTLFN_ARGS)
1195 {
1196 #ifdef INET
1197 struct sockaddr_in *in;
1198 const struct inpcb *inp;
1199 #endif
1200 #ifdef INET6
1201 struct sockaddr_in6 *in6;
1202 const struct in6pcb *in6p;
1203 #endif
1204 /*
1205 * sysctl_data is const, but CIRCLEQ_FOREACH can't use a const
1206 * struct inpcbtable pointer, so we have to discard const. :-/
1207 */
1208 struct inpcbtable *pcbtbl = __UNCONST(rnode->sysctl_data);
1209 const struct inpcb_hdr *inph;
1210 struct tcpcb *tp;
1211 struct kinfo_pcb pcb;
1212 char *dp;
1213 u_int op, arg;
1214 size_t len, needed, elem_size, out_size;
1215 int error, elem_count, pf, proto, pf2;
1216
1217 if (namelen != 4)
1218 return (EINVAL);
1219
1220 if (oldp != NULL) {
1221 len = *oldlenp;
1222 elem_size = name[2];
1223 elem_count = name[3];
1224 if (elem_size != sizeof(pcb))
1225 return EINVAL;
1226 pf2 = pf;
1227 } else {
1228 len = 0;
1229 elem_count = INT_MAX;
1230 elem_size = sizeof(pcb);
1231 pf2 = 0;
1232 }
1233 error = 0;
1234 dp = oldp;
1235 op = name[0];
1236 arg = name[1];
1237 out_size = elem_size;
1238 needed = 0;
1239
1240 if (namelen == 1 && name[0] == CTL_QUERY)
1241 return (sysctl_query(SYSCTLFN_CALL(rnode)));
1242
1243 if (name - oname != 4)
1244 return (EINVAL);
1245
1246 pf = oname[1];
1247 proto = oname[2];
1248
1249 CIRCLEQ_FOREACH(inph, &pcbtbl->inpt_queue, inph_queue) {
1250 #ifdef INET
1251 inp = (const struct inpcb *)inph;
1252 #endif
1253 #ifdef INET6
1254 in6p = (const struct in6pcb *)inph;
1255 #endif
1256
1257 if (inph->inph_af != pf)
1258 continue;
1259
1260 if (CURTAIN(l->l_proc->p_ucred->cr_uid,
1261 inph->inph_socket->so_uidinfo->ui_uid))
1262 continue;
1263
1264 memset(&pcb, 0, sizeof(pcb));
1265
1266 pcb.ki_family = pf;
1267 pcb.ki_type = proto;
1268
1269 switch (pf2) {
1270 case 0:
1271 /* just probing for size */
1272 break;
1273 #ifdef INET
1274 case PF_INET:
1275 pcb.ki_family = inp->inp_socket->so_proto->
1276 pr_domain->dom_family;
1277 pcb.ki_type = inp->inp_socket->so_proto->
1278 pr_type;
1279 pcb.ki_protocol = inp->inp_socket->so_proto->
1280 pr_protocol;
1281 pcb.ki_pflags = inp->inp_flags;
1282
1283 pcb.ki_sostate = inp->inp_socket->so_state;
1284 pcb.ki_prstate = inp->inp_state;
1285 if (proto == IPPROTO_TCP) {
1286 tp = intotcpcb(inp);
1287 pcb.ki_tstate = tp->t_state;
1288 pcb.ki_tflags = tp->t_flags;
1289 }
1290
1291 pcb.ki_pcbaddr = PTRTOUINT64(inp);
1292 pcb.ki_ppcbaddr = PTRTOUINT64(inp->inp_ppcb);
1293 pcb.ki_sockaddr = PTRTOUINT64(inp->inp_socket);
1294
1295 pcb.ki_rcvq = inp->inp_socket->so_rcv.sb_cc;
1296 pcb.ki_sndq = inp->inp_socket->so_snd.sb_cc;
1297
1298 in = satosin(&pcb.ki_src);
1299 in->sin_len = sizeof(*in);
1300 in->sin_family = pf;
1301 in->sin_port = inp->inp_lport;
1302 in->sin_addr = inp->inp_laddr;
1303 if (pcb.ki_prstate >= INP_CONNECTED) {
1304 in = satosin(&pcb.ki_dst);
1305 in->sin_len = sizeof(*in);
1306 in->sin_family = pf;
1307 in->sin_port = inp->inp_fport;
1308 in->sin_addr = inp->inp_faddr;
1309 }
1310 break;
1311 #endif
1312 #ifdef INET6
1313 case PF_INET6:
1314 pcb.ki_family = in6p->in6p_socket->so_proto->
1315 pr_domain->dom_family;
1316 pcb.ki_type = in6p->in6p_socket->so_proto->pr_type;
1317 pcb.ki_protocol = in6p->in6p_socket->so_proto->
1318 pr_protocol;
1319 pcb.ki_pflags = in6p->in6p_flags;
1320
1321 pcb.ki_sostate = in6p->in6p_socket->so_state;
1322 pcb.ki_prstate = in6p->in6p_state;
1323 if (proto == IPPROTO_TCP) {
1324 tp = in6totcpcb(in6p);
1325 pcb.ki_tstate = tp->t_state;
1326 pcb.ki_tflags = tp->t_flags;
1327 }
1328
1329 pcb.ki_pcbaddr = PTRTOUINT64(in6p);
1330 pcb.ki_ppcbaddr = PTRTOUINT64(in6p->in6p_ppcb);
1331 pcb.ki_sockaddr = PTRTOUINT64(in6p->in6p_socket);
1332
1333 pcb.ki_rcvq = in6p->in6p_socket->so_rcv.sb_cc;
1334 pcb.ki_sndq = in6p->in6p_socket->so_snd.sb_cc;
1335
1336 in6 = satosin6(&pcb.ki_src);
1337 in6->sin6_len = sizeof(*in6);
1338 in6->sin6_family = pf;
1339 in6->sin6_port = in6p->in6p_lport;
1340 in6->sin6_flowinfo = in6p->in6p_flowinfo;
1341 in6->sin6_addr = in6p->in6p_laddr;
1342 in6->sin6_scope_id = 0; /* XXX? */
1343
1344 if (pcb.ki_prstate >= IN6P_CONNECTED) {
1345 in6 = satosin6(&pcb.ki_dst);
1346 in6->sin6_len = sizeof(*in6);
1347 in6->sin6_family = pf;
1348 in6->sin6_port = in6p->in6p_fport;
1349 in6->sin6_flowinfo = in6p->in6p_flowinfo;
1350 in6->sin6_addr = in6p->in6p_faddr;
1351 in6->sin6_scope_id = 0; /* XXX? */
1352 }
1353 break;
1354 #endif
1355 }
1356
1357 if (len >= elem_size && elem_count > 0) {
1358 error = copyout(&pcb, dp, out_size);
1359 if (error)
1360 return (error);
1361 dp += elem_size;
1362 len -= elem_size;
1363 }
1364 if (elem_count > 0) {
1365 needed += elem_size;
1366 if (elem_count != INT_MAX)
1367 elem_count--;
1368 }
1369 }
1370
1371 *oldlenp = needed;
1372 if (oldp == NULL)
1373 *oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
1374
1375 return (error);
1376 }
1377
1378 /*
1379 * this (second stage) setup routine is a replacement for tcp_sysctl()
1380 * (which is currently used for ipv4 and ipv6)
1381 */
1382 static void
1383 sysctl_net_inet_tcp_setup2(struct sysctllog **clog, int pf, const char *pfname,
1384 const char *tcpname)
1385 {
1386 const struct sysctlnode *sack_node;
1387 #ifdef TCP_DEBUG
1388 extern struct tcp_debug tcp_debug[TCP_NDEBUG];
1389 extern int tcp_debx;
1390 #endif
1391
1392 sysctl_createv(clog, 0, NULL, NULL,
1393 CTLFLAG_PERMANENT,
1394 CTLTYPE_NODE, "net", NULL,
1395 NULL, 0, NULL, 0,
1396 CTL_NET, CTL_EOL);
1397 sysctl_createv(clog, 0, NULL, NULL,
1398 CTLFLAG_PERMANENT,
1399 CTLTYPE_NODE, pfname, NULL,
1400 NULL, 0, NULL, 0,
1401 CTL_NET, pf, CTL_EOL);
1402 sysctl_createv(clog, 0, NULL, NULL,
1403 CTLFLAG_PERMANENT,
1404 CTLTYPE_NODE, tcpname,
1405 SYSCTL_DESCR("TCP related settings"),
1406 NULL, 0, NULL, 0,
1407 CTL_NET, pf, IPPROTO_TCP, CTL_EOL);
1408
1409 sysctl_createv(clog, 0, NULL, NULL,
1410 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1411 CTLTYPE_INT, "rfc1323",
1412 SYSCTL_DESCR("Enable RFC1323 TCP extensions"),
1413 NULL, 0, &tcp_do_rfc1323, 0,
1414 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RFC1323, CTL_EOL);
1415 sysctl_createv(clog, 0, NULL, NULL,
1416 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1417 CTLTYPE_INT, "sendspace",
1418 SYSCTL_DESCR("Default TCP send buffer size"),
1419 NULL, 0, &tcp_sendspace, 0,
1420 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SENDSPACE, CTL_EOL);
1421 sysctl_createv(clog, 0, NULL, NULL,
1422 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1423 CTLTYPE_INT, "recvspace",
1424 SYSCTL_DESCR("Default TCP receive buffer size"),
1425 NULL, 0, &tcp_recvspace, 0,
1426 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RECVSPACE, CTL_EOL);
1427 sysctl_createv(clog, 0, NULL, NULL,
1428 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1429 CTLTYPE_INT, "mssdflt",
1430 SYSCTL_DESCR("Default maximum segment size"),
1431 sysctl_net_inet_tcp_mssdflt, 0, &tcp_mssdflt, 0,
1432 CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSSDFLT, CTL_EOL);
1433 sysctl_createv(clog, 0, NULL, NULL,
1434 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1435 CTLTYPE_INT, "syn_cache_limit",
1436 SYSCTL_DESCR("Maximum number of entries in the TCP "
1437 "compressed state engine"),
1438 NULL, 0, &tcp_syn_cache_limit, 0,
1439 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_LIMIT,
1440 CTL_EOL);
1441 sysctl_createv(clog, 0, NULL, NULL,
1442 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1443 CTLTYPE_INT, "syn_bucket_limit",
1444 SYSCTL_DESCR("Maximum number of entries per hash "
1445 "bucket in the TCP compressed state "
1446 "engine"),
1447 NULL, 0, &tcp_syn_bucket_limit, 0,
1448 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_BUCKET_LIMIT,
1449 CTL_EOL);
1450 #if 0 /* obsoleted */
1451 sysctl_createv(clog, 0, NULL, NULL,
1452 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1453 CTLTYPE_INT, "syn_cache_interval",
1454 SYSCTL_DESCR("TCP compressed state engine's timer interval"),
1455 NULL, 0, &tcp_syn_cache_interval, 0,
1456 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_INTER,
1457 CTL_EOL);
1458 #endif
1459 sysctl_createv(clog, 0, NULL, NULL,
1460 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1461 CTLTYPE_INT, "init_win",
1462 SYSCTL_DESCR("Initial TCP congestion window"),
1463 NULL, 0, &tcp_init_win, 0,
1464 CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN, CTL_EOL);
1465 sysctl_createv(clog, 0, NULL, NULL,
1466 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1467 CTLTYPE_INT, "mss_ifmtu",
1468 SYSCTL_DESCR("Use interface MTU for calculating MSS"),
1469 NULL, 0, &tcp_mss_ifmtu, 0,
1470 CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSS_IFMTU, CTL_EOL);
1471 sysctl_createv(clog, 0, NULL, &sack_node,
1472 CTLFLAG_PERMANENT,
1473 CTLTYPE_NODE, "sack",
1474 SYSCTL_DESCR("RFC2018 Selective ACKnowledgement tunables"),
1475 NULL, 0, NULL, 0,
1476 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_EOL);
1477 sysctl_createv(clog, 0, NULL, NULL,
1478 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1479 CTLTYPE_INT, "win_scale",
1480 SYSCTL_DESCR("Use RFC1323 window scale options"),
1481 NULL, 0, &tcp_do_win_scale, 0,
1482 CTL_NET, pf, IPPROTO_TCP, TCPCTL_WSCALE, CTL_EOL);
1483 sysctl_createv(clog, 0, NULL, NULL,
1484 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1485 CTLTYPE_INT, "timestamps",
1486 SYSCTL_DESCR("Use RFC1323 time stamp options"),
1487 NULL, 0, &tcp_do_timestamps, 0,
1488 CTL_NET, pf, IPPROTO_TCP, TCPCTL_TSTAMP, CTL_EOL);
1489 sysctl_createv(clog, 0, NULL, NULL,
1490 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1491 CTLTYPE_INT, "compat_42",
1492 SYSCTL_DESCR("Enable workarounds for 4.2BSD TCP bugs"),
1493 NULL, 0, &tcp_compat_42, 0,
1494 CTL_NET, pf, IPPROTO_TCP, TCPCTL_COMPAT_42, CTL_EOL);
1495 sysctl_createv(clog, 0, NULL, NULL,
1496 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1497 CTLTYPE_INT, "cwm",
1498 SYSCTL_DESCR("Hughes/Touch/Heidemann Congestion Window "
1499 "Monitoring"),
1500 NULL, 0, &tcp_cwm, 0,
1501 CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM, CTL_EOL);
1502 sysctl_createv(clog, 0, NULL, NULL,
1503 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1504 CTLTYPE_INT, "cwm_burstsize",
1505 SYSCTL_DESCR("Congestion Window Monitoring allowed "
1506 "burst count in packets"),
1507 NULL, 0, &tcp_cwm_burstsize, 0,
1508 CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM_BURSTSIZE,
1509 CTL_EOL);
1510 sysctl_createv(clog, 0, NULL, NULL,
1511 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1512 CTLTYPE_INT, "ack_on_push",
1513 SYSCTL_DESCR("Immediately return ACK when PSH is "
1514 "received"),
1515 NULL, 0, &tcp_ack_on_push, 0,
1516 CTL_NET, pf, IPPROTO_TCP, TCPCTL_ACK_ON_PUSH, CTL_EOL);
1517 sysctl_createv(clog, 0, NULL, NULL,
1518 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1519 CTLTYPE_INT, "keepidle",
1520 SYSCTL_DESCR("Allowed connection idle ticks before a "
1521 "keepalive probe is sent"),
1522 NULL, 0, &tcp_keepidle, 0,
1523 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPIDLE, CTL_EOL);
1524 sysctl_createv(clog, 0, NULL, NULL,
1525 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1526 CTLTYPE_INT, "keepintvl",
1527 SYSCTL_DESCR("Ticks before next keepalive probe is sent"),
1528 NULL, 0, &tcp_keepintvl, 0,
1529 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPINTVL, CTL_EOL);
1530 sysctl_createv(clog, 0, NULL, NULL,
1531 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1532 CTLTYPE_INT, "keepcnt",
1533 SYSCTL_DESCR("Number of keepalive probes to send"),
1534 NULL, 0, &tcp_keepcnt, 0,
1535 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPCNT, CTL_EOL);
1536 sysctl_createv(clog, 0, NULL, NULL,
1537 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
1538 CTLTYPE_INT, "slowhz",
1539 SYSCTL_DESCR("Keepalive ticks per second"),
1540 NULL, PR_SLOWHZ, NULL, 0,
1541 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SLOWHZ, CTL_EOL);
1542 sysctl_createv(clog, 0, NULL, NULL,
1543 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1544 CTLTYPE_INT, "newreno",
1545 SYSCTL_DESCR("NewReno congestion control algorithm"),
1546 NULL, 0, &tcp_do_newreno, 0,
1547 CTL_NET, pf, IPPROTO_TCP, TCPCTL_NEWRENO, CTL_EOL);
1548 sysctl_createv(clog, 0, NULL, NULL,
1549 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1550 CTLTYPE_INT, "log_refused",
1551 SYSCTL_DESCR("Log refused TCP connections"),
1552 NULL, 0, &tcp_log_refused, 0,
1553 CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOG_REFUSED, CTL_EOL);
1554 #if 0 /* obsoleted */
1555 sysctl_createv(clog, 0, NULL, NULL,
1556 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1557 CTLTYPE_INT, "rstratelimit", NULL,
1558 NULL, 0, &tcp_rst_ratelim, 0,
1559 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTRATELIMIT, CTL_EOL);
1560 #endif
1561 sysctl_createv(clog, 0, NULL, NULL,
1562 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1563 CTLTYPE_INT, "rstppslimit",
1564 SYSCTL_DESCR("Maximum number of RST packets to send "
1565 "per second"),
1566 NULL, 0, &tcp_rst_ppslim, 0,
1567 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTPPSLIMIT, CTL_EOL);
1568 sysctl_createv(clog, 0, NULL, NULL,
1569 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1570 CTLTYPE_INT, "delack_ticks",
1571 SYSCTL_DESCR("Number of ticks to delay sending an ACK"),
1572 NULL, 0, &tcp_delack_ticks, 0,
1573 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DELACK_TICKS, CTL_EOL);
1574 sysctl_createv(clog, 0, NULL, NULL,
1575 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1576 CTLTYPE_INT, "init_win_local",
1577 SYSCTL_DESCR("Initial TCP window size (in segments)"),
1578 NULL, 0, &tcp_init_win_local, 0,
1579 CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN_LOCAL,
1580 CTL_EOL);
1581 sysctl_createv(clog, 0, NULL, NULL,
1582 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1583 CTLTYPE_STRUCT, "ident",
1584 SYSCTL_DESCR("RFC1413 Identification Protocol lookups"),
1585 sysctl_net_inet_tcp_ident, 0, NULL, sizeof(uid_t),
1586 CTL_NET, pf, IPPROTO_TCP, TCPCTL_IDENT, CTL_EOL);
1587 sysctl_createv(clog, 0, NULL, NULL,
1588 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1589 CTLTYPE_INT, "do_loopback_cksum",
1590 SYSCTL_DESCR("Perform TCP checksum on loopback"),
1591 NULL, 0, &tcp_do_loopback_cksum, 0,
1592 CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOOPBACKCKSUM,
1593 CTL_EOL);
1594 sysctl_createv(clog, 0, NULL, NULL,
1595 CTLFLAG_PERMANENT,
1596 CTLTYPE_STRUCT, "pcblist",
1597 SYSCTL_DESCR("TCP protocol control block list"),
1598 sysctl_inpcblist, 0, &tcbtable, 0,
1599 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
1600 CTL_EOL);
1601
1602 /* SACK gets it's own little subtree. */
1603 sysctl_createv(clog, 0, NULL, &sack_node,
1604 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1605 CTLTYPE_INT, "enable",
1606 SYSCTL_DESCR("Enable RFC2018 Selective ACKnowledgement"),
1607 NULL, 0, &tcp_do_sack, 0,
1608 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1609 sysctl_createv(clog, 0, NULL, &sack_node,
1610 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1611 CTLTYPE_INT, "maxholes",
1612 SYSCTL_DESCR("Maximum number of TCP SACK holes allowed per connection"),
1613 NULL, 0, &tcp_sack_tp_maxholes, 0,
1614 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1615 sysctl_createv(clog, 0, NULL, &sack_node,
1616 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1617 CTLTYPE_INT, "globalmaxholes",
1618 SYSCTL_DESCR("Global maximum number of TCP SACK holes"),
1619 NULL, 0, &tcp_sack_globalmaxholes, 0,
1620 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1621 sysctl_createv(clog, 0, NULL, &sack_node,
1622 CTLFLAG_PERMANENT,
1623 CTLTYPE_INT, "globalholes",
1624 SYSCTL_DESCR("Global number of TCP SACK holes"),
1625 NULL, 0, &tcp_sack_globalholes, 0,
1626 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1627
1628 sysctl_createv(clog, 0, NULL, NULL,
1629 CTLFLAG_PERMANENT,
1630 CTLTYPE_STRUCT, "stats",
1631 SYSCTL_DESCR("TCP statistics"),
1632 NULL, 0, &tcpstat, sizeof(tcpstat),
1633 CTL_NET, pf, IPPROTO_TCP, TCPCTL_STATS,
1634 CTL_EOL);
1635 #ifdef TCP_DEBUG
1636 sysctl_createv(clog, 0, NULL, NULL,
1637 CTLFLAG_PERMANENT,
1638 CTLTYPE_STRUCT, "debug",
1639 SYSCTL_DESCR("TCP sockets debug information"),
1640 NULL, 0, &tcp_debug, sizeof(tcp_debug),
1641 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBUG,
1642 CTL_EOL);
1643 sysctl_createv(clog, 0, NULL, NULL,
1644 CTLFLAG_PERMANENT,
1645 CTLTYPE_INT, "debx",
1646 SYSCTL_DESCR("Number of TCP debug sockets messages"),
1647 NULL, 0, &tcp_debx, sizeof(tcp_debx),
1648 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBX,
1649 CTL_EOL);
1650 #endif
1651
1652 }
1653
1654 /*
1655 * Sysctl for tcp variables.
1656 */
1657 #ifdef INET
1658 SYSCTL_SETUP(sysctl_net_inet_tcp_setup, "sysctl net.inet.tcp subtree setup")
1659 {
1660
1661 sysctl_net_inet_tcp_setup2(clog, PF_INET, "inet", "tcp");
1662 }
1663 #endif /* INET */
1664
1665 #ifdef INET6
1666 SYSCTL_SETUP(sysctl_net_inet6_tcp6_setup, "sysctl net.inet6.tcp6 subtree setup")
1667 {
1668
1669 sysctl_net_inet_tcp_setup2(clog, PF_INET6, "inet6", "tcp6");
1670 }
1671 #endif /* INET6 */
1672