tcp_usrreq.c revision 1.113.4.3 1 /* $NetBSD: tcp_usrreq.c,v 1.113.4.3 2006/09/09 02:58:47 rpaulo 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, 2006 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 * This code is derived from software contributed to The NetBSD Foundation
42 * by Rui Paulo.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the NetBSD
55 * Foundation, Inc. and its contributors.
56 * 4. Neither the name of The NetBSD Foundation nor the names of its
57 * contributors may be used to endorse or promote products derived
58 * from this software without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
61 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
62 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
63 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
64 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
65 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
66 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
67 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
68 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
69 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
70 * POSSIBILITY OF SUCH DAMAGE.
71 */
72
73 /*
74 * Copyright (c) 1982, 1986, 1988, 1993, 1995
75 * The Regents of the University of California. All rights reserved.
76 *
77 * Redistribution and use in source and binary forms, with or without
78 * modification, are permitted provided that the following conditions
79 * are met:
80 * 1. Redistributions of source code must retain the above copyright
81 * notice, this list of conditions and the following disclaimer.
82 * 2. Redistributions in binary form must reproduce the above copyright
83 * notice, this list of conditions and the following disclaimer in the
84 * documentation and/or other materials provided with the distribution.
85 * 3. Neither the name of the University nor the names of its contributors
86 * may be used to endorse or promote products derived from this software
87 * without specific prior written permission.
88 *
89 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
92 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
93 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
94 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
95 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
96 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
97 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
98 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99 * SUCH DAMAGE.
100 *
101 * @(#)tcp_usrreq.c 8.5 (Berkeley) 6/21/95
102 */
103
104 #include <sys/cdefs.h>
105 __KERNEL_RCSID(0, "$NetBSD: tcp_usrreq.c,v 1.113.4.3 2006/09/09 02:58:47 rpaulo Exp $");
106
107 #include "opt_inet.h"
108 #include "opt_ipsec.h"
109 #include "opt_tcp_debug.h"
110 #include "opt_mbuftrace.h"
111
112 #include <sys/param.h>
113 #include <sys/systm.h>
114 #include <sys/kernel.h>
115 #include <sys/malloc.h>
116 #include <sys/mbuf.h>
117 #include <sys/socket.h>
118 #include <sys/socketvar.h>
119 #include <sys/protosw.h>
120 #include <sys/errno.h>
121 #include <sys/stat.h>
122 #include <sys/proc.h>
123 #include <sys/domain.h>
124 #include <sys/sysctl.h>
125 #include <sys/kauth.h>
126
127 #include <net/if.h>
128 #include <net/route.h>
129
130 #include <netinet/in.h>
131 #include <netinet/in_systm.h>
132 #include <netinet/in_var.h>
133 #include <netinet/ip.h>
134 #include <netinet/in_pcb.h>
135 #include <netinet/ip_var.h>
136 #include <netinet/in_offload.h>
137
138 #ifdef INET6
139 #ifndef INET
140 #include <netinet/in.h>
141 #endif
142 #include <netinet/ip6.h>
143 #include <netinet6/ip6_var.h>
144 #endif
145
146 #include <netinet/tcp.h>
147 #include <netinet/tcp_fsm.h>
148 #include <netinet/tcp_seq.h>
149 #include <netinet/tcp_timer.h>
150 #include <netinet/tcp_var.h>
151 #include <netinet/tcpip.h>
152 #include <netinet/tcp_debug.h>
153
154 #include "opt_tcp_space.h"
155
156 #ifdef IPSEC
157 #include <netinet6/ipsec.h>
158 #endif /*IPSEC*/
159
160 /*
161 * TCP protocol interface to socket abstraction.
162 */
163
164 /*
165 * Process a TCP user request for TCP tb. If this is a send request
166 * then m is the mbuf chain of send data. If this is a timer expiration
167 * (called from the software clock routine), then timertype tells which timer.
168 */
169 /*ARGSUSED*/
170 int
171 tcp_usrreq(struct socket *so, int req,
172 struct mbuf *m, struct mbuf *nam, struct mbuf *control, struct lwp *l)
173 {
174 struct inpcb *inp;
175 #ifdef INET6
176 struct in6pcb *in6p;
177 #endif
178 struct tcpcb *tp = NULL;
179 int s;
180 int error = 0;
181 #ifdef TCP_DEBUG
182 int ostate = 0;
183 #endif
184 int family; /* family of the socket */
185
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, l));
194 #endif
195 #ifdef INET6
196 case PF_INET6:
197 return (in6_control(so, (long)m, (caddr_t)nam,
198 (struct ifnet *)control, l));
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, l);
334 break;
335 #endif
336 #ifdef INET6
337 case PF_INET6:
338 error = in6_pcbbind(in6p, nam, l);
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 lwp *)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 lwp *)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 lwp *)0);
387 if (error)
388 break;
389 }
390 error = in_pcbconnect(inp, nam, l);
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 lwp *)0);
398 if (error)
399 break;
400 }
401 error = in6_pcbconnect(in6p, nam, l);
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 const struct inpcb *inp;
1197 #ifdef INET
1198 struct sockaddr_in *in;
1199 #endif
1200 #ifdef INET6
1201 struct sockaddr_in6 *in6;
1202 #endif
1203 /*
1204 * sysctl_data is const, but CIRCLEQ_FOREACH can't use a const
1205 * struct inpcbtable pointer, so we have to discard const. :-/
1206 */
1207 struct inpcbtable *pcbtbl = __UNCONST(rnode->sysctl_data);
1208 struct tcpcb *tp;
1209 struct kinfo_pcb pcb;
1210 char *dp;
1211 u_int op, arg;
1212 size_t len, needed, elem_size, out_size;
1213 int error, elem_count, pf, proto, pf2;
1214
1215 if (namelen != 4)
1216 return (EINVAL);
1217
1218 if (oldp != NULL) {
1219 len = *oldlenp;
1220 elem_size = name[2];
1221 elem_count = name[3];
1222 if (elem_size != sizeof(pcb))
1223 return EINVAL;
1224 } else {
1225 len = 0;
1226 elem_count = INT_MAX;
1227 elem_size = sizeof(pcb);
1228 }
1229 error = 0;
1230 dp = oldp;
1231 op = name[0];
1232 arg = name[1];
1233 out_size = elem_size;
1234 needed = 0;
1235
1236 if (namelen == 1 && name[0] == CTL_QUERY)
1237 return (sysctl_query(SYSCTLFN_CALL(rnode)));
1238
1239 if (name - oname != 4)
1240 return (EINVAL);
1241
1242 pf = oname[1];
1243 proto = oname[2];
1244 pf2 = (oldp != NULL) ? pf : 0;
1245
1246 CIRCLEQ_FOREACH(inp, &pcbtbl->inpt_queue, inp_queue) {
1247 if (inp->inp_af != pf)
1248 continue;
1249
1250 /* XXX elad - should be done better */
1251 if (security_curtain &&
1252 (kauth_cred_geteuid(l->l_cred) != 0) &&
1253 (kauth_cred_geteuid(l->l_cred) !=
1254 inph->inph_socket->so_uidinfo->ui_uid))
1255 continue;
1256
1257 memset(&pcb, 0, sizeof(pcb));
1258
1259 pcb.ki_family = pf;
1260 pcb.ki_type = proto;
1261
1262 switch (pf2) {
1263 case 0:
1264 /* just probing for size */
1265 break;
1266 #ifdef INET
1267 case PF_INET:
1268 pcb.ki_family = inp->inp_socket->so_proto->
1269 pr_domain->dom_family;
1270 pcb.ki_type = inp->inp_socket->so_proto->
1271 pr_type;
1272 pcb.ki_protocol = inp->inp_socket->so_proto->
1273 pr_protocol;
1274 pcb.ki_pflags = inp->inp_flags;
1275
1276 pcb.ki_sostate = inp->inp_socket->so_state;
1277 pcb.ki_prstate = inp->inp_state;
1278 if (proto == IPPROTO_TCP) {
1279 tp = intotcpcb(inp);
1280 pcb.ki_tstate = tp->t_state;
1281 pcb.ki_tflags = tp->t_flags;
1282 }
1283
1284 pcb.ki_pcbaddr = PTRTOUINT64(inp);
1285 pcb.ki_ppcbaddr = PTRTOUINT64(inp->inp_ppcb);
1286 pcb.ki_sockaddr = PTRTOUINT64(inp->inp_socket);
1287
1288 pcb.ki_rcvq = inp->inp_socket->so_rcv.sb_cc;
1289 pcb.ki_sndq = inp->inp_socket->so_snd.sb_cc;
1290
1291 in = satosin(&pcb.ki_src);
1292 in->sin_len = sizeof(*in);
1293 in->sin_family = pf;
1294 in->sin_port = inp->inp_lport;
1295 in->sin_addr = inp->inp_laddr;
1296 if (pcb.ki_prstate >= INP_CONNECTED) {
1297 in = satosin(&pcb.ki_dst);
1298 in->sin_len = sizeof(*in);
1299 in->sin_family = pf;
1300 in->sin_port = inp->inp_fport;
1301 in->sin_addr = inp->inp_faddr;
1302 }
1303 break;
1304 #endif
1305 #ifdef INET6
1306 case PF_INET6:
1307 pcb.ki_family = inp->inp_socket->so_proto->
1308 pr_domain->dom_family;
1309 pcb.ki_type = inp->inp_socket->so_proto->pr_type;
1310 pcb.ki_protocol = inp->inp_socket->so_proto->
1311 pr_protocol;
1312 pcb.ki_pflags = inp->in6p_flags;
1313
1314 pcb.ki_sostate = inp->inp_socket->so_state;
1315 pcb.ki_prstate = inp->inp_state;
1316 if (proto == IPPROTO_TCP) {
1317 tp = intotcpcb(inp);
1318 pcb.ki_tstate = tp->t_state;
1319 pcb.ki_tflags = tp->t_flags;
1320 }
1321
1322 pcb.ki_pcbaddr = PTRTOUINT64(inp);
1323 pcb.ki_ppcbaddr = PTRTOUINT64(inp->inp_ppcb);
1324 pcb.ki_sockaddr = PTRTOUINT64(inp->inp_socket);
1325
1326 pcb.ki_rcvq = inp->inp_socket->so_rcv.sb_cc;
1327 pcb.ki_sndq = inp->inp_socket->so_snd.sb_cc;
1328
1329 in6 = satosin6(&pcb.ki_src);
1330 in6->sin6_len = sizeof(*in6);
1331 in6->sin6_family = pf;
1332 in6->sin6_port = inp->inp_lport;
1333 in6->sin6_flowinfo = inp->in6p_flowinfo;
1334 in6->sin6_addr = inp->in6p_laddr;
1335 in6->sin6_scope_id = 0; /* XXX? */
1336
1337 if (pcb.ki_prstate >= INP_CONNECTED) {
1338 in6 = satosin6(&pcb.ki_dst);
1339 in6->sin6_len = sizeof(*in6);
1340 in6->sin6_family = pf;
1341 in6->sin6_port = inp->inp_fport;
1342 in6->sin6_flowinfo = inp->in6p_flowinfo;
1343 in6->sin6_addr = inp->in6p_faddr;
1344 in6->sin6_scope_id = 0; /* XXX? */
1345 }
1346 break;
1347 #endif
1348 }
1349
1350 if (len >= elem_size && elem_count > 0) {
1351 error = copyout(&pcb, dp, out_size);
1352 if (error)
1353 return (error);
1354 dp += elem_size;
1355 len -= elem_size;
1356 }
1357 if (elem_count > 0) {
1358 needed += elem_size;
1359 if (elem_count != INT_MAX)
1360 elem_count--;
1361 }
1362 }
1363
1364 *oldlenp = needed;
1365 if (oldp == NULL)
1366 *oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
1367
1368 return (error);
1369 }
1370
1371 /*
1372 * this (second stage) setup routine is a replacement for tcp_sysctl()
1373 * (which is currently used for ipv4 and ipv6)
1374 */
1375 static void
1376 sysctl_net_inet_tcp_setup2(struct sysctllog **clog, int pf, const char *pfname,
1377 const char *tcpname)
1378 {
1379 int ecn_node;
1380 const struct sysctlnode *sack_node, *node;
1381 #ifdef TCP_DEBUG
1382 extern struct tcp_debug tcp_debug[TCP_NDEBUG];
1383 extern int tcp_debx;
1384 #endif
1385
1386 sysctl_createv(clog, 0, NULL, NULL,
1387 CTLFLAG_PERMANENT,
1388 CTLTYPE_NODE, "net", NULL,
1389 NULL, 0, NULL, 0,
1390 CTL_NET, CTL_EOL);
1391 sysctl_createv(clog, 0, NULL, NULL,
1392 CTLFLAG_PERMANENT,
1393 CTLTYPE_NODE, pfname, NULL,
1394 NULL, 0, NULL, 0,
1395 CTL_NET, pf, CTL_EOL);
1396 sysctl_createv(clog, 0, NULL, NULL,
1397 CTLFLAG_PERMANENT,
1398 CTLTYPE_NODE, tcpname,
1399 SYSCTL_DESCR("TCP related settings"),
1400 NULL, 0, NULL, 0,
1401 CTL_NET, pf, IPPROTO_TCP, CTL_EOL);
1402
1403 sysctl_createv(clog, 0, NULL, NULL,
1404 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1405 CTLTYPE_INT, "rfc1323",
1406 SYSCTL_DESCR("Enable RFC1323 TCP extensions"),
1407 NULL, 0, &tcp_do_rfc1323, 0,
1408 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RFC1323, CTL_EOL);
1409 sysctl_createv(clog, 0, NULL, NULL,
1410 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1411 CTLTYPE_INT, "sendspace",
1412 SYSCTL_DESCR("Default TCP send buffer size"),
1413 NULL, 0, &tcp_sendspace, 0,
1414 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SENDSPACE, CTL_EOL);
1415 sysctl_createv(clog, 0, NULL, NULL,
1416 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1417 CTLTYPE_INT, "recvspace",
1418 SYSCTL_DESCR("Default TCP receive buffer size"),
1419 NULL, 0, &tcp_recvspace, 0,
1420 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RECVSPACE, CTL_EOL);
1421 sysctl_createv(clog, 0, NULL, NULL,
1422 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1423 CTLTYPE_INT, "mssdflt",
1424 SYSCTL_DESCR("Default maximum segment size"),
1425 sysctl_net_inet_tcp_mssdflt, 0, &tcp_mssdflt, 0,
1426 CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSSDFLT, CTL_EOL);
1427 sysctl_createv(clog, 0, NULL, NULL,
1428 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1429 CTLTYPE_INT, "syn_cache_limit",
1430 SYSCTL_DESCR("Maximum number of entries in the TCP "
1431 "compressed state engine"),
1432 NULL, 0, &tcp_syn_cache_limit, 0,
1433 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_LIMIT,
1434 CTL_EOL);
1435 sysctl_createv(clog, 0, NULL, NULL,
1436 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1437 CTLTYPE_INT, "syn_bucket_limit",
1438 SYSCTL_DESCR("Maximum number of entries per hash "
1439 "bucket in the TCP compressed state "
1440 "engine"),
1441 NULL, 0, &tcp_syn_bucket_limit, 0,
1442 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_BUCKET_LIMIT,
1443 CTL_EOL);
1444 #if 0 /* obsoleted */
1445 sysctl_createv(clog, 0, NULL, NULL,
1446 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1447 CTLTYPE_INT, "syn_cache_interval",
1448 SYSCTL_DESCR("TCP compressed state engine's timer interval"),
1449 NULL, 0, &tcp_syn_cache_interval, 0,
1450 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_INTER,
1451 CTL_EOL);
1452 #endif
1453 sysctl_createv(clog, 0, NULL, NULL,
1454 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1455 CTLTYPE_INT, "init_win",
1456 SYSCTL_DESCR("Initial TCP congestion window"),
1457 NULL, 0, &tcp_init_win, 0,
1458 CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN, CTL_EOL);
1459 sysctl_createv(clog, 0, NULL, NULL,
1460 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1461 CTLTYPE_INT, "mss_ifmtu",
1462 SYSCTL_DESCR("Use interface MTU for calculating MSS"),
1463 NULL, 0, &tcp_mss_ifmtu, 0,
1464 CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSS_IFMTU, CTL_EOL);
1465 sysctl_createv(clog, 0, NULL, &sack_node,
1466 CTLFLAG_PERMANENT,
1467 CTLTYPE_NODE, "sack",
1468 SYSCTL_DESCR("RFC2018 Selective ACKnowledgement tunables"),
1469 NULL, 0, NULL, 0,
1470 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_EOL);
1471 sysctl_createv(clog, 0, NULL, &node,
1472 CTLFLAG_PERMANENT,
1473 CTLTYPE_NODE, "ecn",
1474 SYSCTL_DESCR("RFC3168 Explicit Congestion Notification"),
1475 NULL, 0, NULL, 0,
1476 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1477 ecn_node = node->sysctl_num;
1478 sysctl_createv(clog, 0, NULL, NULL,
1479 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1480 CTLTYPE_INT, "win_scale",
1481 SYSCTL_DESCR("Use RFC1323 window scale options"),
1482 NULL, 0, &tcp_do_win_scale, 0,
1483 CTL_NET, pf, IPPROTO_TCP, TCPCTL_WSCALE, CTL_EOL);
1484 sysctl_createv(clog, 0, NULL, NULL,
1485 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1486 CTLTYPE_INT, "timestamps",
1487 SYSCTL_DESCR("Use RFC1323 time stamp options"),
1488 NULL, 0, &tcp_do_timestamps, 0,
1489 CTL_NET, pf, IPPROTO_TCP, TCPCTL_TSTAMP, CTL_EOL);
1490 sysctl_createv(clog, 0, NULL, NULL,
1491 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1492 CTLTYPE_INT, "compat_42",
1493 SYSCTL_DESCR("Enable workarounds for 4.2BSD TCP bugs"),
1494 NULL, 0, &tcp_compat_42, 0,
1495 CTL_NET, pf, IPPROTO_TCP, TCPCTL_COMPAT_42, CTL_EOL);
1496 sysctl_createv(clog, 0, NULL, NULL,
1497 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1498 CTLTYPE_INT, "cwm",
1499 SYSCTL_DESCR("Hughes/Touch/Heidemann Congestion Window "
1500 "Monitoring"),
1501 NULL, 0, &tcp_cwm, 0,
1502 CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM, CTL_EOL);
1503 sysctl_createv(clog, 0, NULL, NULL,
1504 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1505 CTLTYPE_INT, "cwm_burstsize",
1506 SYSCTL_DESCR("Congestion Window Monitoring allowed "
1507 "burst count in packets"),
1508 NULL, 0, &tcp_cwm_burstsize, 0,
1509 CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM_BURSTSIZE,
1510 CTL_EOL);
1511 sysctl_createv(clog, 0, NULL, NULL,
1512 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1513 CTLTYPE_INT, "ack_on_push",
1514 SYSCTL_DESCR("Immediately return ACK when PSH is "
1515 "received"),
1516 NULL, 0, &tcp_ack_on_push, 0,
1517 CTL_NET, pf, IPPROTO_TCP, TCPCTL_ACK_ON_PUSH, CTL_EOL);
1518 sysctl_createv(clog, 0, NULL, NULL,
1519 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1520 CTLTYPE_INT, "keepidle",
1521 SYSCTL_DESCR("Allowed connection idle ticks before a "
1522 "keepalive probe is sent"),
1523 NULL, 0, &tcp_keepidle, 0,
1524 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPIDLE, CTL_EOL);
1525 sysctl_createv(clog, 0, NULL, NULL,
1526 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1527 CTLTYPE_INT, "keepintvl",
1528 SYSCTL_DESCR("Ticks before next keepalive probe is sent"),
1529 NULL, 0, &tcp_keepintvl, 0,
1530 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPINTVL, CTL_EOL);
1531 sysctl_createv(clog, 0, NULL, NULL,
1532 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1533 CTLTYPE_INT, "keepcnt",
1534 SYSCTL_DESCR("Number of keepalive probes to send"),
1535 NULL, 0, &tcp_keepcnt, 0,
1536 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPCNT, CTL_EOL);
1537 sysctl_createv(clog, 0, NULL, NULL,
1538 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
1539 CTLTYPE_INT, "slowhz",
1540 SYSCTL_DESCR("Keepalive ticks per second"),
1541 NULL, PR_SLOWHZ, NULL, 0,
1542 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SLOWHZ, CTL_EOL);
1543 sysctl_createv(clog, 0, NULL, NULL,
1544 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1545 CTLTYPE_INT, "newreno",
1546 SYSCTL_DESCR("NewReno congestion control algorithm"),
1547 NULL, 0, &tcp_do_newreno, 0,
1548 CTL_NET, pf, IPPROTO_TCP, TCPCTL_NEWRENO, CTL_EOL);
1549 sysctl_createv(clog, 0, NULL, NULL,
1550 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1551 CTLTYPE_INT, "log_refused",
1552 SYSCTL_DESCR("Log refused TCP connections"),
1553 NULL, 0, &tcp_log_refused, 0,
1554 CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOG_REFUSED, CTL_EOL);
1555 #if 0 /* obsoleted */
1556 sysctl_createv(clog, 0, NULL, NULL,
1557 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1558 CTLTYPE_INT, "rstratelimit", NULL,
1559 NULL, 0, &tcp_rst_ratelim, 0,
1560 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTRATELIMIT, CTL_EOL);
1561 #endif
1562 sysctl_createv(clog, 0, NULL, NULL,
1563 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1564 CTLTYPE_INT, "rstppslimit",
1565 SYSCTL_DESCR("Maximum number of RST packets to send "
1566 "per second"),
1567 NULL, 0, &tcp_rst_ppslim, 0,
1568 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTPPSLIMIT, CTL_EOL);
1569 sysctl_createv(clog, 0, NULL, NULL,
1570 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1571 CTLTYPE_INT, "delack_ticks",
1572 SYSCTL_DESCR("Number of ticks to delay sending an ACK"),
1573 NULL, 0, &tcp_delack_ticks, 0,
1574 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DELACK_TICKS, CTL_EOL);
1575 sysctl_createv(clog, 0, NULL, NULL,
1576 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1577 CTLTYPE_INT, "init_win_local",
1578 SYSCTL_DESCR("Initial TCP window size (in segments)"),
1579 NULL, 0, &tcp_init_win_local, 0,
1580 CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN_LOCAL,
1581 CTL_EOL);
1582 sysctl_createv(clog, 0, NULL, NULL,
1583 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1584 CTLTYPE_STRUCT, "ident",
1585 SYSCTL_DESCR("RFC1413 Identification Protocol lookups"),
1586 sysctl_net_inet_tcp_ident, 0, NULL, sizeof(uid_t),
1587 CTL_NET, pf, IPPROTO_TCP, TCPCTL_IDENT, CTL_EOL);
1588 sysctl_createv(clog, 0, NULL, NULL,
1589 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1590 CTLTYPE_INT, "do_loopback_cksum",
1591 SYSCTL_DESCR("Perform TCP checksum on loopback"),
1592 NULL, 0, &tcp_do_loopback_cksum, 0,
1593 CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOOPBACKCKSUM,
1594 CTL_EOL);
1595 sysctl_createv(clog, 0, NULL, NULL,
1596 CTLFLAG_PERMANENT,
1597 CTLTYPE_STRUCT, "pcblist",
1598 SYSCTL_DESCR("TCP protocol control block list"),
1599 sysctl_inpcblist, 0, &tcbtable, 0,
1600 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
1601 CTL_EOL);
1602
1603 sysctl_createv(clog, 0, NULL, NULL,
1604 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1605 CTLTYPE_INT, "enable",
1606 SYSCTL_DESCR("Enable TCP Explicit Congestion "
1607 "Notification"),
1608 NULL, 0, &tcp_do_ecn, 0,
1609 CTL_NET, pf, IPPROTO_TCP, ecn_node,
1610 CTL_CREATE, CTL_EOL);
1611 sysctl_createv(clog, 0, NULL, NULL,
1612 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1613 CTLTYPE_INT, "maxretries",
1614 SYSCTL_DESCR("Number of times to retry ECN setup "
1615 "before disabling ECN on the connection"),
1616 NULL, 0, &tcp_ecn_maxretries, 0,
1617 CTL_NET, pf, IPPROTO_TCP, ecn_node,
1618 CTL_CREATE, CTL_EOL);
1619
1620 /* SACK gets it's own little subtree. */
1621 sysctl_createv(clog, 0, NULL, &sack_node,
1622 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1623 CTLTYPE_INT, "enable",
1624 SYSCTL_DESCR("Enable RFC2018 Selective ACKnowledgement"),
1625 NULL, 0, &tcp_do_sack, 0,
1626 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1627 sysctl_createv(clog, 0, NULL, &sack_node,
1628 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1629 CTLTYPE_INT, "maxholes",
1630 SYSCTL_DESCR("Maximum number of TCP SACK holes allowed per connection"),
1631 NULL, 0, &tcp_sack_tp_maxholes, 0,
1632 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1633 sysctl_createv(clog, 0, NULL, &sack_node,
1634 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1635 CTLTYPE_INT, "globalmaxholes",
1636 SYSCTL_DESCR("Global maximum number of TCP SACK holes"),
1637 NULL, 0, &tcp_sack_globalmaxholes, 0,
1638 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1639 sysctl_createv(clog, 0, NULL, &sack_node,
1640 CTLFLAG_PERMANENT,
1641 CTLTYPE_INT, "globalholes",
1642 SYSCTL_DESCR("Global number of TCP SACK holes"),
1643 NULL, 0, &tcp_sack_globalholes, 0,
1644 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1645
1646 sysctl_createv(clog, 0, NULL, NULL,
1647 CTLFLAG_PERMANENT,
1648 CTLTYPE_STRUCT, "stats",
1649 SYSCTL_DESCR("TCP statistics"),
1650 NULL, 0, &tcpstat, sizeof(tcpstat),
1651 CTL_NET, pf, IPPROTO_TCP, TCPCTL_STATS,
1652 CTL_EOL);
1653 #ifdef TCP_DEBUG
1654 sysctl_createv(clog, 0, NULL, NULL,
1655 CTLFLAG_PERMANENT,
1656 CTLTYPE_STRUCT, "debug",
1657 SYSCTL_DESCR("TCP sockets debug information"),
1658 NULL, 0, &tcp_debug, sizeof(tcp_debug),
1659 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBUG,
1660 CTL_EOL);
1661 sysctl_createv(clog, 0, NULL, NULL,
1662 CTLFLAG_PERMANENT,
1663 CTLTYPE_INT, "debx",
1664 SYSCTL_DESCR("Number of TCP debug sockets messages"),
1665 NULL, 0, &tcp_debx, sizeof(tcp_debx),
1666 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBX,
1667 CTL_EOL);
1668 #endif
1669
1670 }
1671
1672 /*
1673 * Sysctl for tcp variables.
1674 */
1675 #ifdef INET
1676 SYSCTL_SETUP(sysctl_net_inet_tcp_setup, "sysctl net.inet.tcp subtree setup")
1677 {
1678
1679 sysctl_net_inet_tcp_setup2(clog, PF_INET, "inet", "tcp");
1680 }
1681 #endif /* INET */
1682
1683 #ifdef INET6
1684 SYSCTL_SETUP(sysctl_net_inet6_tcp6_setup, "sysctl net.inet6.tcp6 subtree setup")
1685 {
1686
1687 sysctl_net_inet_tcp_setup2(clog, PF_INET6, "inet6", "tcp6");
1688 }
1689 #endif /* INET6 */
1690