tcp_usrreq.c revision 1.174 1 /* $NetBSD: tcp_usrreq.c,v 1.174 2014/05/20 19:04:00 rmind 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 *
53 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 * POSSIBILITY OF SUCH DAMAGE.
64 */
65
66 /*
67 * Copyright (c) 1982, 1986, 1988, 1993, 1995
68 * The Regents of the University of California. All rights reserved.
69 *
70 * Redistribution and use in source and binary forms, with or without
71 * modification, are permitted provided that the following conditions
72 * are met:
73 * 1. Redistributions of source code must retain the above copyright
74 * notice, this list of conditions and the following disclaimer.
75 * 2. Redistributions in binary form must reproduce the above copyright
76 * notice, this list of conditions and the following disclaimer in the
77 * documentation and/or other materials provided with the distribution.
78 * 3. Neither the name of the University nor the names of its contributors
79 * may be used to endorse or promote products derived from this software
80 * without specific prior written permission.
81 *
82 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
83 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
85 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
86 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
87 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
88 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
89 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
90 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
91 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
92 * SUCH DAMAGE.
93 *
94 * @(#)tcp_usrreq.c 8.5 (Berkeley) 6/21/95
95 */
96
97 /*
98 * TCP protocol interface to socket abstraction.
99 */
100
101 #include <sys/cdefs.h>
102 __KERNEL_RCSID(0, "$NetBSD: tcp_usrreq.c,v 1.174 2014/05/20 19:04:00 rmind Exp $");
103
104 #include "opt_inet.h"
105 #include "opt_ipsec.h"
106 #include "opt_tcp_debug.h"
107 #include "opt_mbuftrace.h"
108
109 #include <sys/param.h>
110 #include <sys/systm.h>
111 #include <sys/kernel.h>
112 #include <sys/malloc.h>
113 #include <sys/mbuf.h>
114 #include <sys/socket.h>
115 #include <sys/socketvar.h>
116 #include <sys/protosw.h>
117 #include <sys/errno.h>
118 #include <sys/stat.h>
119 #include <sys/proc.h>
120 #include <sys/domain.h>
121 #include <sys/sysctl.h>
122 #include <sys/kauth.h>
123 #include <sys/uidinfo.h>
124
125 #include <net/if.h>
126 #include <net/route.h>
127
128 #include <netinet/in.h>
129 #include <netinet/in_systm.h>
130 #include <netinet/in_var.h>
131 #include <netinet/ip.h>
132 #include <netinet/in_pcb.h>
133 #include <netinet/ip_var.h>
134 #include <netinet/in_offload.h>
135
136 #ifdef INET6
137 #ifndef INET
138 #include <netinet/in.h>
139 #endif
140 #include <netinet/ip6.h>
141 #include <netinet6/in6_pcb.h>
142 #include <netinet6/ip6_var.h>
143 #include <netinet6/scope6_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/tcp_private.h>
152 #include <netinet/tcp_congctl.h>
153 #include <netinet/tcpip.h>
154 #include <netinet/tcp_debug.h>
155 #include <netinet/tcp_vtw.h>
156
157 #include "opt_tcp_space.h"
158
159 /*
160 * Process a TCP user request for TCP tb. If this is a send request
161 * then m is the mbuf chain of send data. If this is a timer expiration
162 * (called from the software clock routine), then timertype tells which timer.
163 */
164 static int
165 tcp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
166 struct mbuf *control, struct lwp *l)
167 {
168 struct inpcb *inp;
169 #ifdef INET6
170 struct in6pcb *in6p;
171 #endif
172 struct tcpcb *tp = NULL;
173 int s;
174 int error = 0;
175 #ifdef TCP_DEBUG
176 int ostate = 0;
177 #endif
178 int family; /* family of the socket */
179
180 KASSERT(req != PRU_ATTACH);
181 KASSERT(req != PRU_DETACH);
182
183 family = so->so_proto->pr_domain->dom_family;
184
185 if (req == PRU_CONTROL) {
186 switch (family) {
187 #ifdef INET
188 case PF_INET:
189 return (in_control(so, (long)m, (void *)nam,
190 (struct ifnet *)control, l));
191 #endif
192 #ifdef INET6
193 case PF_INET6:
194 return (in6_control(so, (long)m, (void *)nam,
195 (struct ifnet *)control, l));
196 #endif
197 default:
198 return EAFNOSUPPORT;
199 }
200 }
201
202 s = splsoftnet();
203
204 if (req == PRU_PURGEIF) {
205 mutex_enter(softnet_lock);
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 mutex_exit(softnet_lock);
223 splx(s);
224 return (EAFNOSUPPORT);
225 }
226 mutex_exit(softnet_lock);
227 splx(s);
228 return (0);
229 }
230
231 KASSERT(solocked(so));
232
233 switch (family) {
234 #ifdef INET
235 case PF_INET:
236 inp = sotoinpcb(so);
237 #ifdef INET6
238 in6p = NULL;
239 #endif
240 break;
241 #endif
242 #ifdef INET6
243 case PF_INET6:
244 inp = NULL;
245 in6p = sotoin6pcb(so);
246 break;
247 #endif
248 default:
249 splx(s);
250 return EAFNOSUPPORT;
251 }
252
253 #ifdef INET6
254 KASSERT((inp != NULL) ^ (in6p != NULL));
255 #endif
256 KASSERT(!control || (req == PRU_SEND || req == PRU_SENDOOB));
257
258 /*
259 * When a TCP is attached to a socket, then there will be
260 * a (struct inpcb) pointed at by the socket, and this
261 * structure will point at a subsidary (struct tcpcb).
262 */
263 if ((inp == NULL
264 #ifdef INET6
265 && in6p == NULL
266 #endif
267 ) && req != PRU_SENSE)
268 {
269 error = EINVAL;
270 goto release;
271 }
272 #ifdef INET
273 if (inp) {
274 tp = intotcpcb(inp);
275 /* WHAT IF TP IS 0? */
276 #ifdef KPROF
277 tcp_acounts[tp->t_state][req]++;
278 #endif
279 #ifdef TCP_DEBUG
280 ostate = tp->t_state;
281 #endif
282 }
283 #endif
284 #ifdef INET6
285 if (in6p) {
286 tp = in6totcpcb(in6p);
287 /* WHAT IF TP IS 0? */
288 #ifdef KPROF
289 tcp_acounts[tp->t_state][req]++;
290 #endif
291 #ifdef TCP_DEBUG
292 ostate = tp->t_state;
293 #endif
294 }
295 #endif
296
297 switch (req) {
298
299 /*
300 * Give the socket an address.
301 */
302 case PRU_BIND:
303 switch (family) {
304 #ifdef INET
305 case PF_INET:
306 error = in_pcbbind(inp, nam, l);
307 break;
308 #endif
309 #ifdef INET6
310 case PF_INET6:
311 error = in6_pcbbind(in6p, nam, l);
312 if (!error) {
313 /* mapped addr case */
314 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
315 tp->t_family = AF_INET;
316 else
317 tp->t_family = AF_INET6;
318 }
319 break;
320 #endif
321 }
322 break;
323
324 /*
325 * Prepare to accept connections.
326 */
327 case PRU_LISTEN:
328 #ifdef INET
329 if (inp && inp->inp_lport == 0) {
330 error = in_pcbbind(inp, NULL, l);
331 if (error)
332 break;
333 }
334 #endif
335 #ifdef INET6
336 if (in6p && in6p->in6p_lport == 0) {
337 error = in6_pcbbind(in6p, NULL, l);
338 if (error)
339 break;
340 }
341 #endif
342 tp->t_state = TCPS_LISTEN;
343 break;
344
345 /*
346 * Initiate connection to peer.
347 * Create a template for use in transmissions on this connection.
348 * Enter SYN_SENT state, and mark socket as connecting.
349 * Start keep-alive timer, and seed output sequence space.
350 * Send initial segment on connection.
351 */
352 case PRU_CONNECT:
353 #ifdef INET
354 if (inp) {
355 if (inp->inp_lport == 0) {
356 error = in_pcbbind(inp, NULL, l);
357 if (error)
358 break;
359 }
360 error = in_pcbconnect(inp, nam, l);
361 }
362 #endif
363 #ifdef INET6
364 if (in6p) {
365 if (in6p->in6p_lport == 0) {
366 error = in6_pcbbind(in6p, NULL, l);
367 if (error)
368 break;
369 }
370 error = in6_pcbconnect(in6p, nam, l);
371 if (!error) {
372 /* mapped addr case */
373 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
374 tp->t_family = AF_INET;
375 else
376 tp->t_family = AF_INET6;
377 }
378 }
379 #endif
380 if (error)
381 break;
382 tp->t_template = tcp_template(tp);
383 if (tp->t_template == 0) {
384 #ifdef INET
385 if (inp)
386 in_pcbdisconnect(inp);
387 #endif
388 #ifdef INET6
389 if (in6p)
390 in6_pcbdisconnect(in6p);
391 #endif
392 error = ENOBUFS;
393 break;
394 }
395 /*
396 * Compute window scaling to request.
397 * XXX: This should be moved to tcp_output().
398 */
399 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
400 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
401 tp->request_r_scale++;
402 soisconnecting(so);
403 TCP_STATINC(TCP_STAT_CONNATTEMPT);
404 tp->t_state = TCPS_SYN_SENT;
405 TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepinit);
406 tp->iss = tcp_new_iss(tp, 0);
407 tcp_sendseqinit(tp);
408 error = tcp_output(tp);
409 break;
410
411 /*
412 * Create a TCP connection between two sockets.
413 */
414 case PRU_CONNECT2:
415 error = EOPNOTSUPP;
416 break;
417
418 /*
419 * Initiate disconnect from peer.
420 * If connection never passed embryonic stage, just drop;
421 * else if don't need to let data drain, then can just drop anyways,
422 * else have to begin TCP shutdown process: mark socket disconnecting,
423 * drain unread data, state switch to reflect user close, and
424 * send segment (e.g. FIN) to peer. Socket will be really disconnected
425 * when peer sends FIN and acks ours.
426 *
427 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
428 */
429 case PRU_DISCONNECT:
430 tp = tcp_disconnect(tp);
431 break;
432
433 /*
434 * Accept a connection. Essentially all the work is
435 * done at higher levels; just return the address
436 * of the peer, storing through addr.
437 */
438 case PRU_ACCEPT:
439 #ifdef INET
440 if (inp)
441 in_setpeeraddr(inp, nam);
442 #endif
443 #ifdef INET6
444 if (in6p)
445 in6_setpeeraddr(in6p, nam);
446 #endif
447 break;
448
449 /*
450 * Mark the connection as being incapable of further output.
451 */
452 case PRU_SHUTDOWN:
453 socantsendmore(so);
454 tp = tcp_usrclosed(tp);
455 if (tp)
456 error = tcp_output(tp);
457 break;
458
459 /*
460 * After a receive, possibly send window update to peer.
461 */
462 case PRU_RCVD:
463 /*
464 * soreceive() calls this function when a user receives
465 * ancillary data on a listening socket. We don't call
466 * tcp_output in such a case, since there is no header
467 * template for a listening socket and hence the kernel
468 * will panic.
469 */
470 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) != 0)
471 (void) tcp_output(tp);
472 break;
473
474 /*
475 * Do a send by putting data in output queue and updating urgent
476 * marker if URG set. Possibly send more data.
477 */
478 case PRU_SEND:
479 if (control && control->m_len) {
480 m_freem(control);
481 m_freem(m);
482 error = EINVAL;
483 break;
484 }
485 sbappendstream(&so->so_snd, m);
486 error = tcp_output(tp);
487 break;
488
489 /*
490 * Abort the TCP.
491 */
492 case PRU_ABORT:
493 tp = tcp_drop(tp, ECONNABORTED);
494 break;
495
496 case PRU_SENSE:
497 /*
498 * stat: don't bother with a blocksize.
499 */
500 splx(s);
501 return (0);
502
503 case PRU_RCVOOB:
504 if (control && control->m_len) {
505 m_freem(control);
506 m_freem(m);
507 error = EINVAL;
508 break;
509 }
510 if ((so->so_oobmark == 0 &&
511 (so->so_state & SS_RCVATMARK) == 0) ||
512 so->so_options & SO_OOBINLINE ||
513 tp->t_oobflags & TCPOOB_HADDATA) {
514 error = EINVAL;
515 break;
516 }
517 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
518 error = EWOULDBLOCK;
519 break;
520 }
521 m->m_len = 1;
522 *mtod(m, char *) = tp->t_iobc;
523 if (((long)nam & MSG_PEEK) == 0)
524 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
525 break;
526
527 case PRU_SENDOOB:
528 if (sbspace(&so->so_snd) < -512) {
529 m_freem(m);
530 error = ENOBUFS;
531 break;
532 }
533 /*
534 * According to RFC961 (Assigned Protocols),
535 * the urgent pointer points to the last octet
536 * of urgent data. We continue, however,
537 * to consider it to indicate the first octet
538 * of data past the urgent section.
539 * Otherwise, snd_up should be one lower.
540 */
541 sbappendstream(&so->so_snd, m);
542 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
543 tp->t_force = 1;
544 error = tcp_output(tp);
545 tp->t_force = 0;
546 break;
547
548 case PRU_SOCKADDR:
549 #ifdef INET
550 if (inp)
551 in_setsockaddr(inp, nam);
552 #endif
553 #ifdef INET6
554 if (in6p)
555 in6_setsockaddr(in6p, nam);
556 #endif
557 break;
558
559 case PRU_PEERADDR:
560 #ifdef INET
561 if (inp)
562 in_setpeeraddr(inp, nam);
563 #endif
564 #ifdef INET6
565 if (in6p)
566 in6_setpeeraddr(in6p, nam);
567 #endif
568 break;
569
570 default:
571 panic("tcp_usrreq");
572 }
573 #ifdef TCP_DEBUG
574 if (tp && (so->so_options & SO_DEBUG))
575 tcp_trace(TA_USER, ostate, tp, NULL, req);
576 #endif
577
578 release:
579 splx(s);
580 return (error);
581 }
582
583 static void
584 change_keepalive(struct socket *so, struct tcpcb *tp)
585 {
586 tp->t_maxidle = tp->t_keepcnt * tp->t_keepintvl;
587 TCP_TIMER_DISARM(tp, TCPT_KEEP);
588 TCP_TIMER_DISARM(tp, TCPT_2MSL);
589
590 if (tp->t_state == TCPS_SYN_RECEIVED ||
591 tp->t_state == TCPS_SYN_SENT) {
592 TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepinit);
593 } else if (so->so_options & SO_KEEPALIVE &&
594 tp->t_state <= TCPS_CLOSE_WAIT) {
595 TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepintvl);
596 } else {
597 TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepidle);
598 }
599
600 if ((tp->t_state == TCPS_FIN_WAIT_2) && (tp->t_maxidle > 0))
601 TCP_TIMER_ARM(tp, TCPT_2MSL, tp->t_maxidle);
602 }
603
604
605 int
606 tcp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
607 {
608 int error = 0, s;
609 struct inpcb *inp;
610 #ifdef INET6
611 struct in6pcb *in6p;
612 #endif
613 struct tcpcb *tp;
614 u_int ui;
615 int family; /* family of the socket */
616 int level, optname, optval;
617
618 level = sopt->sopt_level;
619 optname = sopt->sopt_name;
620
621 family = so->so_proto->pr_domain->dom_family;
622
623 s = splsoftnet();
624 switch (family) {
625 #ifdef INET
626 case PF_INET:
627 inp = sotoinpcb(so);
628 #ifdef INET6
629 in6p = NULL;
630 #endif
631 break;
632 #endif
633 #ifdef INET6
634 case PF_INET6:
635 inp = NULL;
636 in6p = sotoin6pcb(so);
637 break;
638 #endif
639 default:
640 splx(s);
641 panic("%s: af %d", __func__, family);
642 }
643 #ifndef INET6
644 if (inp == NULL)
645 #else
646 if (inp == NULL && in6p == NULL)
647 #endif
648 {
649 splx(s);
650 return (ECONNRESET);
651 }
652 if (level != IPPROTO_TCP) {
653 switch (family) {
654 #ifdef INET
655 case PF_INET:
656 error = ip_ctloutput(op, so, sopt);
657 break;
658 #endif
659 #ifdef INET6
660 case PF_INET6:
661 error = ip6_ctloutput(op, so, sopt);
662 break;
663 #endif
664 }
665 splx(s);
666 return (error);
667 }
668 if (inp)
669 tp = intotcpcb(inp);
670 #ifdef INET6
671 else if (in6p)
672 tp = in6totcpcb(in6p);
673 #endif
674 else
675 tp = NULL;
676
677 switch (op) {
678 case PRCO_SETOPT:
679 switch (optname) {
680 #ifdef TCP_SIGNATURE
681 case TCP_MD5SIG:
682 error = sockopt_getint(sopt, &optval);
683 if (error)
684 break;
685 if (optval > 0)
686 tp->t_flags |= TF_SIGNATURE;
687 else
688 tp->t_flags &= ~TF_SIGNATURE;
689 break;
690 #endif /* TCP_SIGNATURE */
691
692 case TCP_NODELAY:
693 error = sockopt_getint(sopt, &optval);
694 if (error)
695 break;
696 if (optval)
697 tp->t_flags |= TF_NODELAY;
698 else
699 tp->t_flags &= ~TF_NODELAY;
700 break;
701
702 case TCP_MAXSEG:
703 error = sockopt_getint(sopt, &optval);
704 if (error)
705 break;
706 if (optval > 0 && optval <= tp->t_peermss)
707 tp->t_peermss = optval; /* limit on send size */
708 else
709 error = EINVAL;
710 break;
711 #ifdef notyet
712 case TCP_CONGCTL:
713 /* XXX string overflow XXX */
714 error = tcp_congctl_select(tp, sopt->sopt_data);
715 break;
716 #endif
717
718 case TCP_KEEPIDLE:
719 error = sockopt_get(sopt, &ui, sizeof(ui));
720 if (error)
721 break;
722 if (ui > 0) {
723 tp->t_keepidle = ui;
724 change_keepalive(so, tp);
725 } else
726 error = EINVAL;
727 break;
728
729 case TCP_KEEPINTVL:
730 error = sockopt_get(sopt, &ui, sizeof(ui));
731 if (error)
732 break;
733 if (ui > 0) {
734 tp->t_keepintvl = ui;
735 change_keepalive(so, tp);
736 } else
737 error = EINVAL;
738 break;
739
740 case TCP_KEEPCNT:
741 error = sockopt_get(sopt, &ui, sizeof(ui));
742 if (error)
743 break;
744 if (ui > 0) {
745 tp->t_keepcnt = ui;
746 change_keepalive(so, tp);
747 } else
748 error = EINVAL;
749 break;
750
751 case TCP_KEEPINIT:
752 error = sockopt_get(sopt, &ui, sizeof(ui));
753 if (error)
754 break;
755 if (ui > 0) {
756 tp->t_keepinit = ui;
757 change_keepalive(so, tp);
758 } else
759 error = EINVAL;
760 break;
761
762 default:
763 error = ENOPROTOOPT;
764 break;
765 }
766 break;
767
768 case PRCO_GETOPT:
769 switch (optname) {
770 #ifdef TCP_SIGNATURE
771 case TCP_MD5SIG:
772 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
773 error = sockopt_set(sopt, &optval, sizeof(optval));
774 break;
775 #endif
776 case TCP_NODELAY:
777 optval = tp->t_flags & TF_NODELAY;
778 error = sockopt_set(sopt, &optval, sizeof(optval));
779 break;
780 case TCP_MAXSEG:
781 optval = tp->t_peermss;
782 error = sockopt_set(sopt, &optval, sizeof(optval));
783 break;
784 #ifdef notyet
785 case TCP_CONGCTL:
786 break;
787 #endif
788 default:
789 error = ENOPROTOOPT;
790 break;
791 }
792 break;
793 }
794 splx(s);
795 return (error);
796 }
797
798 #ifndef TCP_SENDSPACE
799 #define TCP_SENDSPACE 1024*32
800 #endif
801 int tcp_sendspace = TCP_SENDSPACE;
802 #ifndef TCP_RECVSPACE
803 #define TCP_RECVSPACE 1024*32
804 #endif
805 int tcp_recvspace = TCP_RECVSPACE;
806
807 /*
808 * tcp_attach: attach TCP protocol to socket, allocating internet protocol
809 * control block, TCP control block, buffer space and entering LISTEN state
810 * if to accept connections.
811 */
812 static int
813 tcp_attach(struct socket *so, int proto)
814 {
815 struct tcpcb *tp;
816 struct inpcb *inp;
817 #ifdef INET6
818 struct in6pcb *in6p;
819 #endif
820 int s, error, family;
821
822 /* Assign the lock (must happen even if we will error out). */
823 s = splsoftnet();
824 sosetlock(so);
825 KASSERT(solocked(so));
826
827 family = so->so_proto->pr_domain->dom_family;
828 switch (family) {
829 #ifdef INET
830 case PF_INET:
831 inp = sotoinpcb(so);
832 #ifdef INET6
833 in6p = NULL;
834 #endif
835 break;
836 #endif
837 #ifdef INET6
838 case PF_INET6:
839 inp = NULL;
840 in6p = sotoin6pcb(so);
841 break;
842 #endif
843 default:
844 error = EAFNOSUPPORT;
845 goto out;
846 }
847
848 KASSERT(inp == NULL);
849 #ifdef INET6
850 KASSERT(in6p == NULL);
851 #endif
852
853 #ifdef MBUFTRACE
854 so->so_mowner = &tcp_sock_mowner;
855 so->so_rcv.sb_mowner = &tcp_sock_rx_mowner;
856 so->so_snd.sb_mowner = &tcp_sock_tx_mowner;
857 #endif
858 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
859 error = soreserve(so, tcp_sendspace, tcp_recvspace);
860 if (error)
861 goto out;
862 }
863
864 so->so_rcv.sb_flags |= SB_AUTOSIZE;
865 so->so_snd.sb_flags |= SB_AUTOSIZE;
866
867 switch (family) {
868 #ifdef INET
869 case PF_INET:
870 error = in_pcballoc(so, &tcbtable);
871 if (error)
872 goto out;
873 inp = sotoinpcb(so);
874 #ifdef INET6
875 in6p = NULL;
876 #endif
877 break;
878 #endif
879 #ifdef INET6
880 case PF_INET6:
881 error = in6_pcballoc(so, &tcbtable);
882 if (error)
883 goto out;
884 inp = NULL;
885 in6p = sotoin6pcb(so);
886 break;
887 #endif
888 default:
889 error = EAFNOSUPPORT;
890 goto out;
891 }
892 if (inp)
893 tp = tcp_newtcpcb(family, (void *)inp);
894 #ifdef INET6
895 else if (in6p)
896 tp = tcp_newtcpcb(family, (void *)in6p);
897 #endif
898 else
899 tp = NULL;
900
901 if (tp == NULL) {
902 int nofd = so->so_state & SS_NOFDREF; /* XXX */
903
904 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
905 #ifdef INET
906 if (inp)
907 in_pcbdetach(inp);
908 #endif
909 #ifdef INET6
910 if (in6p)
911 in6_pcbdetach(in6p);
912 #endif
913 so->so_state |= nofd;
914 error = ENOBUFS;
915 goto out;
916 }
917 tp->t_state = TCPS_CLOSED;
918 if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
919 so->so_linger = TCP_LINGERTIME;
920 }
921 out:
922 KASSERT(solocked(so));
923 splx(s);
924 return error;
925 }
926
927 static void
928 tcp_detach(struct socket *so)
929 {
930 struct inpcb *inp;
931 #ifdef INET6
932 struct in6pcb *in6p;
933 #endif
934 struct tcpcb *tp = NULL;
935 int s, family;
936
937 KASSERT(solocked(so));
938
939 s = splsoftnet();
940 family = so->so_proto->pr_domain->dom_family;
941 switch (family) {
942 #ifdef INET
943 case PF_INET:
944 inp = sotoinpcb(so);
945 tp = intotcpcb(inp);
946 break;
947 #endif
948 #ifdef INET6
949 case PF_INET6:
950 in6p = sotoin6pcb(so);
951 tp = in6totcpcb(in6p);
952 break;
953 #endif
954 default:
955 splx(s);
956 return;
957 }
958 KASSERT(tp != NULL);
959 (void)tcp_disconnect(tp);
960 splx(s);
961 }
962
963 /*
964 * Initiate (or continue) disconnect.
965 * If embryonic state, just send reset (once).
966 * If in ``let data drain'' option and linger null, just drop.
967 * Otherwise (hard), mark socket disconnecting and drop
968 * current input data; switch states based on user close, and
969 * send segment to peer (with FIN).
970 */
971 struct tcpcb *
972 tcp_disconnect(struct tcpcb *tp)
973 {
974 struct socket *so;
975
976 if (tp->t_inpcb)
977 so = tp->t_inpcb->inp_socket;
978 #ifdef INET6
979 else if (tp->t_in6pcb)
980 so = tp->t_in6pcb->in6p_socket;
981 #endif
982 else
983 so = NULL;
984
985 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
986 tp = tcp_close(tp);
987 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
988 tp = tcp_drop(tp, 0);
989 else {
990 soisdisconnecting(so);
991 sbflush(&so->so_rcv);
992 tp = tcp_usrclosed(tp);
993 if (tp)
994 (void) tcp_output(tp);
995 }
996 return (tp);
997 }
998
999 /*
1000 * User issued close, and wish to trail through shutdown states:
1001 * if never received SYN, just forget it. If got a SYN from peer,
1002 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1003 * If already got a FIN from peer, then almost done; go to LAST_ACK
1004 * state. In all other cases, have already sent FIN to peer (e.g.
1005 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1006 * for peer to send FIN or not respond to keep-alives, etc.
1007 * We can let the user exit from the close as soon as the FIN is acked.
1008 */
1009 struct tcpcb *
1010 tcp_usrclosed(struct tcpcb *tp)
1011 {
1012
1013 switch (tp->t_state) {
1014
1015 case TCPS_CLOSED:
1016 case TCPS_LISTEN:
1017 case TCPS_SYN_SENT:
1018 tp->t_state = TCPS_CLOSED;
1019 tp = tcp_close(tp);
1020 break;
1021
1022 case TCPS_SYN_RECEIVED:
1023 case TCPS_ESTABLISHED:
1024 tp->t_state = TCPS_FIN_WAIT_1;
1025 break;
1026
1027 case TCPS_CLOSE_WAIT:
1028 tp->t_state = TCPS_LAST_ACK;
1029 break;
1030 }
1031 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1032 struct socket *so;
1033 if (tp->t_inpcb)
1034 so = tp->t_inpcb->inp_socket;
1035 #ifdef INET6
1036 else if (tp->t_in6pcb)
1037 so = tp->t_in6pcb->in6p_socket;
1038 #endif
1039 else
1040 so = NULL;
1041 if (so)
1042 soisdisconnected(so);
1043 /*
1044 * If we are in FIN_WAIT_2, we arrived here because the
1045 * application did a shutdown of the send side. Like the
1046 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
1047 * a full close, we start a timer to make sure sockets are
1048 * not left in FIN_WAIT_2 forever.
1049 */
1050 if ((tp->t_state == TCPS_FIN_WAIT_2) && (tp->t_maxidle > 0))
1051 TCP_TIMER_ARM(tp, TCPT_2MSL, tp->t_maxidle);
1052 else if (tp->t_state == TCPS_TIME_WAIT
1053 && ((tp->t_inpcb
1054 && (tcp4_vtw_enable & 1)
1055 && vtw_add(AF_INET, tp))
1056 ||
1057 (tp->t_in6pcb
1058 && (tcp6_vtw_enable & 1)
1059 && vtw_add(AF_INET6, tp)))) {
1060 tp = 0;
1061 }
1062 }
1063 return (tp);
1064 }
1065
1066 /*
1067 * sysctl helper routine for net.inet.ip.mssdflt. it can't be less
1068 * than 32.
1069 */
1070 static int
1071 sysctl_net_inet_tcp_mssdflt(SYSCTLFN_ARGS)
1072 {
1073 int error, mssdflt;
1074 struct sysctlnode node;
1075
1076 mssdflt = tcp_mssdflt;
1077 node = *rnode;
1078 node.sysctl_data = &mssdflt;
1079 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1080 if (error || newp == NULL)
1081 return (error);
1082
1083 if (mssdflt < 32)
1084 return (EINVAL);
1085 tcp_mssdflt = mssdflt;
1086
1087 mutex_enter(softnet_lock);
1088 tcp_tcpcb_template();
1089 mutex_exit(softnet_lock);
1090
1091 return (0);
1092 }
1093
1094 /*
1095 * sysctl helper for TCP CB template update
1096 */
1097 static int
1098 sysctl_update_tcpcb_template(SYSCTLFN_ARGS)
1099 {
1100 int t, error;
1101 struct sysctlnode node;
1102
1103 /* follow procedures in sysctl(9) manpage */
1104 t = *(int *)rnode->sysctl_data;
1105 node = *rnode;
1106 node.sysctl_data = &t;
1107 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1108 if (error || newp == NULL)
1109 return error;
1110
1111 if (t < 0)
1112 return EINVAL;
1113
1114 *(int *)rnode->sysctl_data = t;
1115
1116 mutex_enter(softnet_lock);
1117 tcp_tcpcb_template();
1118 mutex_exit(softnet_lock);
1119
1120 return 0;
1121 }
1122
1123 /*
1124 * sysctl helper routine for setting port related values under
1125 * net.inet.ip and net.inet6.ip6. does basic range checking and does
1126 * additional checks for each type. this code has placed in
1127 * tcp_input.c since INET and INET6 both use the same tcp code.
1128 *
1129 * this helper is not static so that both inet and inet6 can use it.
1130 */
1131 int
1132 sysctl_net_inet_ip_ports(SYSCTLFN_ARGS)
1133 {
1134 int error, tmp;
1135 int apmin, apmax;
1136 #ifndef IPNOPRIVPORTS
1137 int lpmin, lpmax;
1138 #endif /* IPNOPRIVPORTS */
1139 struct sysctlnode node;
1140
1141 if (namelen != 0)
1142 return (EINVAL);
1143
1144 switch (name[-3]) {
1145 #ifdef INET
1146 case PF_INET:
1147 apmin = anonportmin;
1148 apmax = anonportmax;
1149 #ifndef IPNOPRIVPORTS
1150 lpmin = lowportmin;
1151 lpmax = lowportmax;
1152 #endif /* IPNOPRIVPORTS */
1153 break;
1154 #endif /* INET */
1155 #ifdef INET6
1156 case PF_INET6:
1157 apmin = ip6_anonportmin;
1158 apmax = ip6_anonportmax;
1159 #ifndef IPNOPRIVPORTS
1160 lpmin = ip6_lowportmin;
1161 lpmax = ip6_lowportmax;
1162 #endif /* IPNOPRIVPORTS */
1163 break;
1164 #endif /* INET6 */
1165 default:
1166 return (EINVAL);
1167 }
1168
1169 /*
1170 * insert temporary copy into node, perform lookup on
1171 * temporary, then restore pointer
1172 */
1173 node = *rnode;
1174 tmp = *(int*)rnode->sysctl_data;
1175 node.sysctl_data = &tmp;
1176 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1177 if (error || newp == NULL)
1178 return (error);
1179
1180 /*
1181 * simple port range check
1182 */
1183 if (tmp < 0 || tmp > 65535)
1184 return (EINVAL);
1185
1186 /*
1187 * per-node range checks
1188 */
1189 switch (rnode->sysctl_num) {
1190 case IPCTL_ANONPORTMIN:
1191 case IPV6CTL_ANONPORTMIN:
1192 if (tmp >= apmax)
1193 return (EINVAL);
1194 #ifndef IPNOPRIVPORTS
1195 if (tmp < IPPORT_RESERVED)
1196 return (EINVAL);
1197 #endif /* IPNOPRIVPORTS */
1198 break;
1199
1200 case IPCTL_ANONPORTMAX:
1201 case IPV6CTL_ANONPORTMAX:
1202 if (apmin >= tmp)
1203 return (EINVAL);
1204 #ifndef IPNOPRIVPORTS
1205 if (tmp < IPPORT_RESERVED)
1206 return (EINVAL);
1207 #endif /* IPNOPRIVPORTS */
1208 break;
1209
1210 #ifndef IPNOPRIVPORTS
1211 case IPCTL_LOWPORTMIN:
1212 case IPV6CTL_LOWPORTMIN:
1213 if (tmp >= lpmax ||
1214 tmp > IPPORT_RESERVEDMAX ||
1215 tmp < IPPORT_RESERVEDMIN)
1216 return (EINVAL);
1217 break;
1218
1219 case IPCTL_LOWPORTMAX:
1220 case IPV6CTL_LOWPORTMAX:
1221 if (lpmin >= tmp ||
1222 tmp > IPPORT_RESERVEDMAX ||
1223 tmp < IPPORT_RESERVEDMIN)
1224 return (EINVAL);
1225 break;
1226 #endif /* IPNOPRIVPORTS */
1227
1228 default:
1229 return (EINVAL);
1230 }
1231
1232 *(int*)rnode->sysctl_data = tmp;
1233
1234 return (0);
1235 }
1236
1237 static inline int
1238 copyout_uid(struct socket *sockp, void *oldp, size_t *oldlenp)
1239 {
1240 if (oldp) {
1241 size_t sz;
1242 uid_t uid;
1243 int error;
1244
1245 if (sockp->so_cred == NULL)
1246 return EPERM;
1247
1248 uid = kauth_cred_geteuid(sockp->so_cred);
1249 sz = MIN(sizeof(uid), *oldlenp);
1250 if ((error = copyout(&uid, oldp, sz)) != 0)
1251 return error;
1252 }
1253 *oldlenp = sizeof(uid_t);
1254 return 0;
1255 }
1256
1257 static inline int
1258 inet4_ident_core(struct in_addr raddr, u_int rport,
1259 struct in_addr laddr, u_int lport,
1260 void *oldp, size_t *oldlenp,
1261 struct lwp *l, int dodrop)
1262 {
1263 struct inpcb *inp;
1264 struct socket *sockp;
1265
1266 inp = in_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport, 0);
1267
1268 if (inp == NULL || (sockp = inp->inp_socket) == NULL)
1269 return ESRCH;
1270
1271 if (dodrop) {
1272 struct tcpcb *tp;
1273 int error;
1274
1275 if (inp == NULL || (tp = intotcpcb(inp)) == NULL ||
1276 (inp->inp_socket->so_options & SO_ACCEPTCONN) != 0)
1277 return ESRCH;
1278
1279 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
1280 KAUTH_REQ_NETWORK_SOCKET_DROP, inp->inp_socket, tp, NULL);
1281 if (error)
1282 return (error);
1283
1284 (void)tcp_drop(tp, ECONNABORTED);
1285 return 0;
1286 }
1287 else
1288 return copyout_uid(sockp, oldp, oldlenp);
1289 }
1290
1291 #ifdef INET6
1292 static inline int
1293 inet6_ident_core(struct in6_addr *raddr, u_int rport,
1294 struct in6_addr *laddr, u_int lport,
1295 void *oldp, size_t *oldlenp,
1296 struct lwp *l, int dodrop)
1297 {
1298 struct in6pcb *in6p;
1299 struct socket *sockp;
1300
1301 in6p = in6_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport, 0, 0);
1302
1303 if (in6p == NULL || (sockp = in6p->in6p_socket) == NULL)
1304 return ESRCH;
1305
1306 if (dodrop) {
1307 struct tcpcb *tp;
1308 int error;
1309
1310 if (in6p == NULL || (tp = in6totcpcb(in6p)) == NULL ||
1311 (in6p->in6p_socket->so_options & SO_ACCEPTCONN) != 0)
1312 return ESRCH;
1313
1314 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
1315 KAUTH_REQ_NETWORK_SOCKET_DROP, in6p->in6p_socket, tp, NULL);
1316 if (error)
1317 return (error);
1318
1319 (void)tcp_drop(tp, ECONNABORTED);
1320 return 0;
1321 }
1322 else
1323 return copyout_uid(sockp, oldp, oldlenp);
1324 }
1325 #endif
1326
1327 /*
1328 * sysctl helper routine for the net.inet.tcp.drop and
1329 * net.inet6.tcp6.drop nodes.
1330 */
1331 #define sysctl_net_inet_tcp_drop sysctl_net_inet_tcp_ident
1332
1333 /*
1334 * sysctl helper routine for the net.inet.tcp.ident and
1335 * net.inet6.tcp6.ident nodes. contains backwards compat code for the
1336 * old way of looking up the ident information for ipv4 which involves
1337 * stuffing the port/addr pairs into the mib lookup.
1338 */
1339 static int
1340 sysctl_net_inet_tcp_ident(SYSCTLFN_ARGS)
1341 {
1342 #ifdef INET
1343 struct sockaddr_in *si4[2];
1344 #endif /* INET */
1345 #ifdef INET6
1346 struct sockaddr_in6 *si6[2];
1347 #endif /* INET6 */
1348 struct sockaddr_storage sa[2];
1349 int error, pf, dodrop;
1350
1351 dodrop = name[-1] == TCPCTL_DROP;
1352 if (dodrop) {
1353 if (oldp != NULL || *oldlenp != 0)
1354 return EINVAL;
1355 if (newp == NULL)
1356 return EPERM;
1357 if (newlen < sizeof(sa))
1358 return ENOMEM;
1359 }
1360 if (namelen != 4 && namelen != 0)
1361 return EINVAL;
1362 if (name[-2] != IPPROTO_TCP)
1363 return EINVAL;
1364 pf = name[-3];
1365
1366 /* old style lookup, ipv4 only */
1367 if (namelen == 4) {
1368 #ifdef INET
1369 struct in_addr laddr, raddr;
1370 u_int lport, rport;
1371
1372 if (pf != PF_INET)
1373 return EPROTONOSUPPORT;
1374 raddr.s_addr = (uint32_t)name[0];
1375 rport = (u_int)name[1];
1376 laddr.s_addr = (uint32_t)name[2];
1377 lport = (u_int)name[3];
1378
1379 mutex_enter(softnet_lock);
1380 error = inet4_ident_core(raddr, rport, laddr, lport,
1381 oldp, oldlenp, l, dodrop);
1382 mutex_exit(softnet_lock);
1383 return error;
1384 #else /* INET */
1385 return EINVAL;
1386 #endif /* INET */
1387 }
1388
1389 if (newp == NULL || newlen != sizeof(sa))
1390 return EINVAL;
1391 error = copyin(newp, &sa, newlen);
1392 if (error)
1393 return error;
1394
1395 /*
1396 * requested families must match
1397 */
1398 if (pf != sa[0].ss_family || sa[0].ss_family != sa[1].ss_family)
1399 return EINVAL;
1400
1401 switch (pf) {
1402 #ifdef INET6
1403 case PF_INET6:
1404 si6[0] = (struct sockaddr_in6*)&sa[0];
1405 si6[1] = (struct sockaddr_in6*)&sa[1];
1406 if (si6[0]->sin6_len != sizeof(*si6[0]) ||
1407 si6[1]->sin6_len != sizeof(*si6[1]))
1408 return EINVAL;
1409
1410 if (!IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) &&
1411 !IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr)) {
1412 error = sa6_embedscope(si6[0], ip6_use_defzone);
1413 if (error)
1414 return error;
1415 error = sa6_embedscope(si6[1], ip6_use_defzone);
1416 if (error)
1417 return error;
1418
1419 mutex_enter(softnet_lock);
1420 error = inet6_ident_core(&si6[0]->sin6_addr,
1421 si6[0]->sin6_port, &si6[1]->sin6_addr,
1422 si6[1]->sin6_port, oldp, oldlenp, l, dodrop);
1423 mutex_exit(softnet_lock);
1424 return error;
1425 }
1426
1427 if (IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) !=
1428 IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr))
1429 return EINVAL;
1430
1431 in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[0]);
1432 in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[1]);
1433 /*FALLTHROUGH*/
1434 #endif /* INET6 */
1435 #ifdef INET
1436 case PF_INET:
1437 si4[0] = (struct sockaddr_in*)&sa[0];
1438 si4[1] = (struct sockaddr_in*)&sa[1];
1439 if (si4[0]->sin_len != sizeof(*si4[0]) ||
1440 si4[0]->sin_len != sizeof(*si4[1]))
1441 return EINVAL;
1442
1443 mutex_enter(softnet_lock);
1444 error = inet4_ident_core(si4[0]->sin_addr, si4[0]->sin_port,
1445 si4[1]->sin_addr, si4[1]->sin_port,
1446 oldp, oldlenp, l, dodrop);
1447 mutex_exit(softnet_lock);
1448 return error;
1449 #endif /* INET */
1450 default:
1451 return EPROTONOSUPPORT;
1452 }
1453 }
1454
1455 /*
1456 * sysctl helper for the inet and inet6 pcblists. handles tcp/udp and
1457 * inet/inet6, as well as raw pcbs for each. specifically not
1458 * declared static so that raw sockets and udp/udp6 can use it as
1459 * well.
1460 */
1461 int
1462 sysctl_inpcblist(SYSCTLFN_ARGS)
1463 {
1464 #ifdef INET
1465 struct sockaddr_in *in;
1466 const struct inpcb *inp;
1467 #endif
1468 #ifdef INET6
1469 struct sockaddr_in6 *in6;
1470 const struct in6pcb *in6p;
1471 #endif
1472 struct inpcbtable *pcbtbl = __UNCONST(rnode->sysctl_data);
1473 const struct inpcb_hdr *inph;
1474 struct tcpcb *tp;
1475 struct kinfo_pcb pcb;
1476 char *dp;
1477 size_t len, needed, elem_size, out_size;
1478 int error, elem_count, pf, proto, pf2;
1479
1480 if (namelen != 4)
1481 return (EINVAL);
1482
1483 if (oldp != NULL) {
1484 len = *oldlenp;
1485 elem_size = name[2];
1486 elem_count = name[3];
1487 if (elem_size != sizeof(pcb))
1488 return EINVAL;
1489 } else {
1490 len = 0;
1491 elem_count = INT_MAX;
1492 elem_size = sizeof(pcb);
1493 }
1494 error = 0;
1495 dp = oldp;
1496 out_size = elem_size;
1497 needed = 0;
1498
1499 if (namelen == 1 && name[0] == CTL_QUERY)
1500 return (sysctl_query(SYSCTLFN_CALL(rnode)));
1501
1502 if (name - oname != 4)
1503 return (EINVAL);
1504
1505 pf = oname[1];
1506 proto = oname[2];
1507 pf2 = (oldp != NULL) ? pf : 0;
1508
1509 mutex_enter(softnet_lock);
1510
1511 TAILQ_FOREACH(inph, &pcbtbl->inpt_queue, inph_queue) {
1512 #ifdef INET
1513 inp = (const struct inpcb *)inph;
1514 #endif
1515 #ifdef INET6
1516 in6p = (const struct in6pcb *)inph;
1517 #endif
1518
1519 if (inph->inph_af != pf)
1520 continue;
1521
1522 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
1523 KAUTH_REQ_NETWORK_SOCKET_CANSEE, inph->inph_socket, NULL,
1524 NULL) != 0)
1525 continue;
1526
1527 memset(&pcb, 0, sizeof(pcb));
1528
1529 pcb.ki_family = pf;
1530 pcb.ki_type = proto;
1531
1532 switch (pf2) {
1533 case 0:
1534 /* just probing for size */
1535 break;
1536 #ifdef INET
1537 case PF_INET:
1538 pcb.ki_family = inp->inp_socket->so_proto->
1539 pr_domain->dom_family;
1540 pcb.ki_type = inp->inp_socket->so_proto->
1541 pr_type;
1542 pcb.ki_protocol = inp->inp_socket->so_proto->
1543 pr_protocol;
1544 pcb.ki_pflags = inp->inp_flags;
1545
1546 pcb.ki_sostate = inp->inp_socket->so_state;
1547 pcb.ki_prstate = inp->inp_state;
1548 if (proto == IPPROTO_TCP) {
1549 tp = intotcpcb(inp);
1550 pcb.ki_tstate = tp->t_state;
1551 pcb.ki_tflags = tp->t_flags;
1552 }
1553
1554 pcb.ki_pcbaddr = PTRTOUINT64(inp);
1555 pcb.ki_ppcbaddr = PTRTOUINT64(inp->inp_ppcb);
1556 pcb.ki_sockaddr = PTRTOUINT64(inp->inp_socket);
1557
1558 pcb.ki_rcvq = inp->inp_socket->so_rcv.sb_cc;
1559 pcb.ki_sndq = inp->inp_socket->so_snd.sb_cc;
1560
1561 in = satosin(&pcb.ki_src);
1562 in->sin_len = sizeof(*in);
1563 in->sin_family = pf;
1564 in->sin_port = inp->inp_lport;
1565 in->sin_addr = inp->inp_laddr;
1566 if (pcb.ki_prstate >= INP_CONNECTED) {
1567 in = satosin(&pcb.ki_dst);
1568 in->sin_len = sizeof(*in);
1569 in->sin_family = pf;
1570 in->sin_port = inp->inp_fport;
1571 in->sin_addr = inp->inp_faddr;
1572 }
1573 break;
1574 #endif
1575 #ifdef INET6
1576 case PF_INET6:
1577 pcb.ki_family = in6p->in6p_socket->so_proto->
1578 pr_domain->dom_family;
1579 pcb.ki_type = in6p->in6p_socket->so_proto->pr_type;
1580 pcb.ki_protocol = in6p->in6p_socket->so_proto->
1581 pr_protocol;
1582 pcb.ki_pflags = in6p->in6p_flags;
1583
1584 pcb.ki_sostate = in6p->in6p_socket->so_state;
1585 pcb.ki_prstate = in6p->in6p_state;
1586 if (proto == IPPROTO_TCP) {
1587 tp = in6totcpcb(in6p);
1588 pcb.ki_tstate = tp->t_state;
1589 pcb.ki_tflags = tp->t_flags;
1590 }
1591
1592 pcb.ki_pcbaddr = PTRTOUINT64(in6p);
1593 pcb.ki_ppcbaddr = PTRTOUINT64(in6p->in6p_ppcb);
1594 pcb.ki_sockaddr = PTRTOUINT64(in6p->in6p_socket);
1595
1596 pcb.ki_rcvq = in6p->in6p_socket->so_rcv.sb_cc;
1597 pcb.ki_sndq = in6p->in6p_socket->so_snd.sb_cc;
1598
1599 in6 = satosin6(&pcb.ki_src);
1600 in6->sin6_len = sizeof(*in6);
1601 in6->sin6_family = pf;
1602 in6->sin6_port = in6p->in6p_lport;
1603 in6->sin6_flowinfo = in6p->in6p_flowinfo;
1604 in6->sin6_addr = in6p->in6p_laddr;
1605 in6->sin6_scope_id = 0; /* XXX? */
1606
1607 if (pcb.ki_prstate >= IN6P_CONNECTED) {
1608 in6 = satosin6(&pcb.ki_dst);
1609 in6->sin6_len = sizeof(*in6);
1610 in6->sin6_family = pf;
1611 in6->sin6_port = in6p->in6p_fport;
1612 in6->sin6_flowinfo = in6p->in6p_flowinfo;
1613 in6->sin6_addr = in6p->in6p_faddr;
1614 in6->sin6_scope_id = 0; /* XXX? */
1615 }
1616 break;
1617 #endif
1618 }
1619
1620 if (len >= elem_size && elem_count > 0) {
1621 error = copyout(&pcb, dp, out_size);
1622 if (error) {
1623 mutex_exit(softnet_lock);
1624 return (error);
1625 }
1626 dp += elem_size;
1627 len -= elem_size;
1628 }
1629 needed += elem_size;
1630 if (elem_count > 0 && elem_count != INT_MAX)
1631 elem_count--;
1632 }
1633
1634 *oldlenp = needed;
1635 if (oldp == NULL)
1636 *oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
1637
1638 mutex_exit(softnet_lock);
1639
1640 return (error);
1641 }
1642
1643 static int
1644 sysctl_tcp_congctl(SYSCTLFN_ARGS)
1645 {
1646 struct sysctlnode node;
1647 int error;
1648 char newname[TCPCC_MAXLEN];
1649
1650 strlcpy(newname, tcp_congctl_global_name, sizeof(newname) - 1);
1651
1652 node = *rnode;
1653 node.sysctl_data = newname;
1654 node.sysctl_size = sizeof(newname);
1655
1656 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1657
1658 if (error ||
1659 newp == NULL ||
1660 strncmp(newname, tcp_congctl_global_name, sizeof(newname)) == 0)
1661 return error;
1662
1663 mutex_enter(softnet_lock);
1664 error = tcp_congctl_select(NULL, newname);
1665 mutex_exit(softnet_lock);
1666
1667 return error;
1668 }
1669
1670 static int
1671 sysctl_tcp_init_win(SYSCTLFN_ARGS)
1672 {
1673 int error;
1674 u_int iw;
1675 struct sysctlnode node;
1676
1677 iw = *(u_int *)rnode->sysctl_data;
1678 node = *rnode;
1679 node.sysctl_data = &iw;
1680 node.sysctl_size = sizeof(iw);
1681 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1682 if (error || newp == NULL)
1683 return error;
1684
1685 if (iw >= __arraycount(tcp_init_win_max))
1686 return EINVAL;
1687 *(u_int *)rnode->sysctl_data = iw;
1688 return 0;
1689 }
1690
1691 static int
1692 sysctl_tcp_keep(SYSCTLFN_ARGS)
1693 {
1694 int error;
1695 u_int tmp;
1696 struct sysctlnode node;
1697
1698 node = *rnode;
1699 tmp = *(u_int *)rnode->sysctl_data;
1700 node.sysctl_data = &tmp;
1701
1702 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1703 if (error || newp == NULL)
1704 return error;
1705
1706 mutex_enter(softnet_lock);
1707
1708 *(u_int *)rnode->sysctl_data = tmp;
1709 tcp_tcpcb_template(); /* update the template */
1710
1711 mutex_exit(softnet_lock);
1712 return 0;
1713 }
1714
1715 static int
1716 sysctl_net_inet_tcp_stats(SYSCTLFN_ARGS)
1717 {
1718
1719 return (NETSTAT_SYSCTL(tcpstat_percpu, TCP_NSTATS));
1720 }
1721
1722 /*
1723 * this (second stage) setup routine is a replacement for tcp_sysctl()
1724 * (which is currently used for ipv4 and ipv6)
1725 */
1726 static void
1727 sysctl_net_inet_tcp_setup2(struct sysctllog **clog, int pf, const char *pfname,
1728 const char *tcpname)
1729 {
1730 const struct sysctlnode *sack_node;
1731 const struct sysctlnode *abc_node;
1732 const struct sysctlnode *ecn_node;
1733 const struct sysctlnode *congctl_node;
1734 const struct sysctlnode *mslt_node;
1735 const struct sysctlnode *vtw_node;
1736 #ifdef TCP_DEBUG
1737 extern struct tcp_debug tcp_debug[TCP_NDEBUG];
1738 extern int tcp_debx;
1739 #endif
1740
1741 sysctl_createv(clog, 0, NULL, NULL,
1742 CTLFLAG_PERMANENT,
1743 CTLTYPE_NODE, pfname, NULL,
1744 NULL, 0, NULL, 0,
1745 CTL_NET, pf, CTL_EOL);
1746 sysctl_createv(clog, 0, NULL, NULL,
1747 CTLFLAG_PERMANENT,
1748 CTLTYPE_NODE, tcpname,
1749 SYSCTL_DESCR("TCP related settings"),
1750 NULL, 0, NULL, 0,
1751 CTL_NET, pf, IPPROTO_TCP, CTL_EOL);
1752
1753 sysctl_createv(clog, 0, NULL, NULL,
1754 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1755 CTLTYPE_INT, "rfc1323",
1756 SYSCTL_DESCR("Enable RFC1323 TCP extensions"),
1757 sysctl_update_tcpcb_template, 0, &tcp_do_rfc1323, 0,
1758 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RFC1323, CTL_EOL);
1759 sysctl_createv(clog, 0, NULL, NULL,
1760 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1761 CTLTYPE_INT, "sendspace",
1762 SYSCTL_DESCR("Default TCP send buffer size"),
1763 NULL, 0, &tcp_sendspace, 0,
1764 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SENDSPACE, CTL_EOL);
1765 sysctl_createv(clog, 0, NULL, NULL,
1766 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1767 CTLTYPE_INT, "recvspace",
1768 SYSCTL_DESCR("Default TCP receive buffer size"),
1769 NULL, 0, &tcp_recvspace, 0,
1770 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RECVSPACE, CTL_EOL);
1771 sysctl_createv(clog, 0, NULL, NULL,
1772 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1773 CTLTYPE_INT, "mssdflt",
1774 SYSCTL_DESCR("Default maximum segment size"),
1775 sysctl_net_inet_tcp_mssdflt, 0, &tcp_mssdflt, 0,
1776 CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSSDFLT, CTL_EOL);
1777 sysctl_createv(clog, 0, NULL, NULL,
1778 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1779 CTLTYPE_INT, "minmss",
1780 SYSCTL_DESCR("Lower limit for TCP maximum segment size"),
1781 NULL, 0, &tcp_minmss, 0,
1782 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1783 sysctl_createv(clog, 0, NULL, NULL,
1784 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1785 CTLTYPE_INT, "msl",
1786 SYSCTL_DESCR("Maximum Segment Life"),
1787 NULL, 0, &tcp_msl, 0,
1788 CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSL, CTL_EOL);
1789 sysctl_createv(clog, 0, NULL, NULL,
1790 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1791 CTLTYPE_INT, "syn_cache_limit",
1792 SYSCTL_DESCR("Maximum number of entries in the TCP "
1793 "compressed state engine"),
1794 NULL, 0, &tcp_syn_cache_limit, 0,
1795 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_LIMIT,
1796 CTL_EOL);
1797 sysctl_createv(clog, 0, NULL, NULL,
1798 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1799 CTLTYPE_INT, "syn_bucket_limit",
1800 SYSCTL_DESCR("Maximum number of entries per hash "
1801 "bucket in the TCP compressed state "
1802 "engine"),
1803 NULL, 0, &tcp_syn_bucket_limit, 0,
1804 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_BUCKET_LIMIT,
1805 CTL_EOL);
1806 #if 0 /* obsoleted */
1807 sysctl_createv(clog, 0, NULL, NULL,
1808 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1809 CTLTYPE_INT, "syn_cache_interval",
1810 SYSCTL_DESCR("TCP compressed state engine's timer interval"),
1811 NULL, 0, &tcp_syn_cache_interval, 0,
1812 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_INTER,
1813 CTL_EOL);
1814 #endif
1815 sysctl_createv(clog, 0, NULL, NULL,
1816 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1817 CTLTYPE_INT, "init_win",
1818 SYSCTL_DESCR("Initial TCP congestion window"),
1819 sysctl_tcp_init_win, 0, &tcp_init_win, 0,
1820 CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN, CTL_EOL);
1821 sysctl_createv(clog, 0, NULL, NULL,
1822 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1823 CTLTYPE_INT, "mss_ifmtu",
1824 SYSCTL_DESCR("Use interface MTU for calculating MSS"),
1825 NULL, 0, &tcp_mss_ifmtu, 0,
1826 CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSS_IFMTU, CTL_EOL);
1827 sysctl_createv(clog, 0, NULL, &sack_node,
1828 CTLFLAG_PERMANENT,
1829 CTLTYPE_NODE, "sack",
1830 SYSCTL_DESCR("RFC2018 Selective ACKnowledgement tunables"),
1831 NULL, 0, NULL, 0,
1832 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_EOL);
1833
1834 /* Congctl subtree */
1835 sysctl_createv(clog, 0, NULL, &congctl_node,
1836 CTLFLAG_PERMANENT,
1837 CTLTYPE_NODE, "congctl",
1838 SYSCTL_DESCR("TCP Congestion Control"),
1839 NULL, 0, NULL, 0,
1840 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1841 sysctl_createv(clog, 0, &congctl_node, NULL,
1842 CTLFLAG_PERMANENT,
1843 CTLTYPE_STRING, "available",
1844 SYSCTL_DESCR("Available Congestion Control Mechanisms"),
1845 NULL, 0, tcp_congctl_avail, 0, CTL_CREATE, CTL_EOL);
1846 sysctl_createv(clog, 0, &congctl_node, NULL,
1847 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1848 CTLTYPE_STRING, "selected",
1849 SYSCTL_DESCR("Selected Congestion Control Mechanism"),
1850 sysctl_tcp_congctl, 0, NULL, TCPCC_MAXLEN,
1851 CTL_CREATE, CTL_EOL);
1852
1853 sysctl_createv(clog, 0, NULL, NULL,
1854 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1855 CTLTYPE_INT, "win_scale",
1856 SYSCTL_DESCR("Use RFC1323 window scale options"),
1857 sysctl_update_tcpcb_template, 0, &tcp_do_win_scale, 0,
1858 CTL_NET, pf, IPPROTO_TCP, TCPCTL_WSCALE, CTL_EOL);
1859 sysctl_createv(clog, 0, NULL, NULL,
1860 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1861 CTLTYPE_INT, "timestamps",
1862 SYSCTL_DESCR("Use RFC1323 time stamp options"),
1863 sysctl_update_tcpcb_template, 0, &tcp_do_timestamps, 0,
1864 CTL_NET, pf, IPPROTO_TCP, TCPCTL_TSTAMP, CTL_EOL);
1865 sysctl_createv(clog, 0, NULL, NULL,
1866 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1867 CTLTYPE_INT, "compat_42",
1868 SYSCTL_DESCR("Enable workarounds for 4.2BSD TCP bugs"),
1869 NULL, 0, &tcp_compat_42, 0,
1870 CTL_NET, pf, IPPROTO_TCP, TCPCTL_COMPAT_42, CTL_EOL);
1871 sysctl_createv(clog, 0, NULL, NULL,
1872 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1873 CTLTYPE_INT, "cwm",
1874 SYSCTL_DESCR("Hughes/Touch/Heidemann Congestion Window "
1875 "Monitoring"),
1876 NULL, 0, &tcp_cwm, 0,
1877 CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM, CTL_EOL);
1878 sysctl_createv(clog, 0, NULL, NULL,
1879 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1880 CTLTYPE_INT, "cwm_burstsize",
1881 SYSCTL_DESCR("Congestion Window Monitoring allowed "
1882 "burst count in packets"),
1883 NULL, 0, &tcp_cwm_burstsize, 0,
1884 CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM_BURSTSIZE,
1885 CTL_EOL);
1886 sysctl_createv(clog, 0, NULL, NULL,
1887 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1888 CTLTYPE_INT, "ack_on_push",
1889 SYSCTL_DESCR("Immediately return ACK when PSH is "
1890 "received"),
1891 NULL, 0, &tcp_ack_on_push, 0,
1892 CTL_NET, pf, IPPROTO_TCP, TCPCTL_ACK_ON_PUSH, CTL_EOL);
1893 sysctl_createv(clog, 0, NULL, NULL,
1894 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1895 CTLTYPE_INT, "keepidle",
1896 SYSCTL_DESCR("Allowed connection idle ticks before a "
1897 "keepalive probe is sent"),
1898 sysctl_tcp_keep, 0, &tcp_keepidle, 0,
1899 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPIDLE, CTL_EOL);
1900 sysctl_createv(clog, 0, NULL, NULL,
1901 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1902 CTLTYPE_INT, "keepintvl",
1903 SYSCTL_DESCR("Ticks before next keepalive probe is sent"),
1904 sysctl_tcp_keep, 0, &tcp_keepintvl, 0,
1905 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPINTVL, CTL_EOL);
1906 sysctl_createv(clog, 0, NULL, NULL,
1907 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1908 CTLTYPE_INT, "keepcnt",
1909 SYSCTL_DESCR("Number of keepalive probes to send"),
1910 sysctl_tcp_keep, 0, &tcp_keepcnt, 0,
1911 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPCNT, CTL_EOL);
1912 sysctl_createv(clog, 0, NULL, NULL,
1913 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
1914 CTLTYPE_INT, "slowhz",
1915 SYSCTL_DESCR("Keepalive ticks per second"),
1916 NULL, PR_SLOWHZ, NULL, 0,
1917 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SLOWHZ, CTL_EOL);
1918 sysctl_createv(clog, 0, NULL, NULL,
1919 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1920 CTLTYPE_INT, "log_refused",
1921 SYSCTL_DESCR("Log refused TCP connections"),
1922 NULL, 0, &tcp_log_refused, 0,
1923 CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOG_REFUSED, CTL_EOL);
1924 #if 0 /* obsoleted */
1925 sysctl_createv(clog, 0, NULL, NULL,
1926 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1927 CTLTYPE_INT, "rstratelimit", NULL,
1928 NULL, 0, &tcp_rst_ratelim, 0,
1929 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTRATELIMIT, CTL_EOL);
1930 #endif
1931 sysctl_createv(clog, 0, NULL, NULL,
1932 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1933 CTLTYPE_INT, "rstppslimit",
1934 SYSCTL_DESCR("Maximum number of RST packets to send "
1935 "per second"),
1936 NULL, 0, &tcp_rst_ppslim, 0,
1937 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTPPSLIMIT, CTL_EOL);
1938 sysctl_createv(clog, 0, NULL, NULL,
1939 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1940 CTLTYPE_INT, "delack_ticks",
1941 SYSCTL_DESCR("Number of ticks to delay sending an ACK"),
1942 NULL, 0, &tcp_delack_ticks, 0,
1943 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DELACK_TICKS, CTL_EOL);
1944 sysctl_createv(clog, 0, NULL, NULL,
1945 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1946 CTLTYPE_INT, "init_win_local",
1947 SYSCTL_DESCR("Initial TCP window size (in segments)"),
1948 sysctl_tcp_init_win, 0, &tcp_init_win_local, 0,
1949 CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN_LOCAL,
1950 CTL_EOL);
1951 sysctl_createv(clog, 0, NULL, NULL,
1952 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1953 CTLTYPE_STRUCT, "ident",
1954 SYSCTL_DESCR("RFC1413 Identification Protocol lookups"),
1955 sysctl_net_inet_tcp_ident, 0, NULL, sizeof(uid_t),
1956 CTL_NET, pf, IPPROTO_TCP, TCPCTL_IDENT, CTL_EOL);
1957 sysctl_createv(clog, 0, NULL, NULL,
1958 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1959 CTLTYPE_INT, "do_loopback_cksum",
1960 SYSCTL_DESCR("Perform TCP checksum on loopback"),
1961 NULL, 0, &tcp_do_loopback_cksum, 0,
1962 CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOOPBACKCKSUM,
1963 CTL_EOL);
1964 sysctl_createv(clog, 0, NULL, NULL,
1965 CTLFLAG_PERMANENT,
1966 CTLTYPE_STRUCT, "pcblist",
1967 SYSCTL_DESCR("TCP protocol control block list"),
1968 sysctl_inpcblist, 0, &tcbtable, 0,
1969 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
1970 CTL_EOL);
1971 sysctl_createv(clog, 0, NULL, NULL,
1972 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1973 CTLTYPE_INT, "keepinit",
1974 SYSCTL_DESCR("Ticks before initial tcp connection times out"),
1975 sysctl_tcp_keep, 0, &tcp_keepinit, 0,
1976 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1977
1978 /* TCP socket buffers auto-sizing nodes */
1979 sysctl_createv(clog, 0, NULL, NULL,
1980 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1981 CTLTYPE_INT, "recvbuf_auto",
1982 SYSCTL_DESCR("Enable automatic receive "
1983 "buffer sizing (experimental)"),
1984 NULL, 0, &tcp_do_autorcvbuf, 0,
1985 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1986 sysctl_createv(clog, 0, NULL, NULL,
1987 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1988 CTLTYPE_INT, "recvbuf_inc",
1989 SYSCTL_DESCR("Incrementor step size of "
1990 "automatic receive buffer"),
1991 NULL, 0, &tcp_autorcvbuf_inc, 0,
1992 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1993 sysctl_createv(clog, 0, NULL, NULL,
1994 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1995 CTLTYPE_INT, "recvbuf_max",
1996 SYSCTL_DESCR("Max size of automatic receive buffer"),
1997 NULL, 0, &tcp_autorcvbuf_max, 0,
1998 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1999
2000 sysctl_createv(clog, 0, NULL, NULL,
2001 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2002 CTLTYPE_INT, "sendbuf_auto",
2003 SYSCTL_DESCR("Enable automatic send "
2004 "buffer sizing (experimental)"),
2005 NULL, 0, &tcp_do_autosndbuf, 0,
2006 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2007 sysctl_createv(clog, 0, NULL, NULL,
2008 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2009 CTLTYPE_INT, "sendbuf_inc",
2010 SYSCTL_DESCR("Incrementor step size of "
2011 "automatic send buffer"),
2012 NULL, 0, &tcp_autosndbuf_inc, 0,
2013 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2014 sysctl_createv(clog, 0, NULL, NULL,
2015 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2016 CTLTYPE_INT, "sendbuf_max",
2017 SYSCTL_DESCR("Max size of automatic send buffer"),
2018 NULL, 0, &tcp_autosndbuf_max, 0,
2019 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2020
2021 /* ECN subtree */
2022 sysctl_createv(clog, 0, NULL, &ecn_node,
2023 CTLFLAG_PERMANENT,
2024 CTLTYPE_NODE, "ecn",
2025 SYSCTL_DESCR("RFC3168 Explicit Congestion Notification"),
2026 NULL, 0, NULL, 0,
2027 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2028 sysctl_createv(clog, 0, &ecn_node, NULL,
2029 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2030 CTLTYPE_INT, "enable",
2031 SYSCTL_DESCR("Enable TCP Explicit Congestion "
2032 "Notification"),
2033 NULL, 0, &tcp_do_ecn, 0, CTL_CREATE, CTL_EOL);
2034 sysctl_createv(clog, 0, &ecn_node, NULL,
2035 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2036 CTLTYPE_INT, "maxretries",
2037 SYSCTL_DESCR("Number of times to retry ECN setup "
2038 "before disabling ECN on the connection"),
2039 NULL, 0, &tcp_ecn_maxretries, 0, CTL_CREATE, CTL_EOL);
2040
2041 /* SACK gets it's own little subtree. */
2042 sysctl_createv(clog, 0, NULL, &sack_node,
2043 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2044 CTLTYPE_INT, "enable",
2045 SYSCTL_DESCR("Enable RFC2018 Selective ACKnowledgement"),
2046 NULL, 0, &tcp_do_sack, 0,
2047 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
2048 sysctl_createv(clog, 0, NULL, &sack_node,
2049 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2050 CTLTYPE_INT, "maxholes",
2051 SYSCTL_DESCR("Maximum number of TCP SACK holes allowed per connection"),
2052 NULL, 0, &tcp_sack_tp_maxholes, 0,
2053 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
2054 sysctl_createv(clog, 0, NULL, &sack_node,
2055 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2056 CTLTYPE_INT, "globalmaxholes",
2057 SYSCTL_DESCR("Global maximum number of TCP SACK holes"),
2058 NULL, 0, &tcp_sack_globalmaxholes, 0,
2059 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
2060 sysctl_createv(clog, 0, NULL, &sack_node,
2061 CTLFLAG_PERMANENT,
2062 CTLTYPE_INT, "globalholes",
2063 SYSCTL_DESCR("Global number of TCP SACK holes"),
2064 NULL, 0, &tcp_sack_globalholes, 0,
2065 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
2066
2067 sysctl_createv(clog, 0, NULL, NULL,
2068 CTLFLAG_PERMANENT,
2069 CTLTYPE_STRUCT, "stats",
2070 SYSCTL_DESCR("TCP statistics"),
2071 sysctl_net_inet_tcp_stats, 0, NULL, 0,
2072 CTL_NET, pf, IPPROTO_TCP, TCPCTL_STATS,
2073 CTL_EOL);
2074 sysctl_createv(clog, 0, NULL, NULL,
2075 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2076 CTLTYPE_INT, "local_by_rtt",
2077 SYSCTL_DESCR("Use RTT estimator to decide which hosts "
2078 "are local"),
2079 NULL, 0, &tcp_rttlocal, 0,
2080 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2081 #ifdef TCP_DEBUG
2082 sysctl_createv(clog, 0, NULL, NULL,
2083 CTLFLAG_PERMANENT,
2084 CTLTYPE_STRUCT, "debug",
2085 SYSCTL_DESCR("TCP sockets debug information"),
2086 NULL, 0, &tcp_debug, sizeof(tcp_debug),
2087 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBUG,
2088 CTL_EOL);
2089 sysctl_createv(clog, 0, NULL, NULL,
2090 CTLFLAG_PERMANENT,
2091 CTLTYPE_INT, "debx",
2092 SYSCTL_DESCR("Number of TCP debug sockets messages"),
2093 NULL, 0, &tcp_debx, sizeof(tcp_debx),
2094 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBX,
2095 CTL_EOL);
2096 #endif
2097 sysctl_createv(clog, 0, NULL, NULL,
2098 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2099 CTLTYPE_STRUCT, "drop",
2100 SYSCTL_DESCR("TCP drop connection"),
2101 sysctl_net_inet_tcp_drop, 0, NULL, 0,
2102 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DROP, CTL_EOL);
2103 sysctl_createv(clog, 0, NULL, NULL,
2104 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2105 CTLTYPE_INT, "iss_hash",
2106 SYSCTL_DESCR("Enable RFC 1948 ISS by cryptographic "
2107 "hash computation"),
2108 NULL, 0, &tcp_do_rfc1948, sizeof(tcp_do_rfc1948),
2109 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
2110 CTL_EOL);
2111
2112 /* ABC subtree */
2113
2114 sysctl_createv(clog, 0, NULL, &abc_node,
2115 CTLFLAG_PERMANENT, CTLTYPE_NODE, "abc",
2116 SYSCTL_DESCR("RFC3465 Appropriate Byte Counting (ABC)"),
2117 NULL, 0, NULL, 0,
2118 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2119 sysctl_createv(clog, 0, &abc_node, NULL,
2120 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2121 CTLTYPE_INT, "enable",
2122 SYSCTL_DESCR("Enable RFC3465 Appropriate Byte Counting"),
2123 NULL, 0, &tcp_do_abc, 0, CTL_CREATE, CTL_EOL);
2124 sysctl_createv(clog, 0, &abc_node, NULL,
2125 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2126 CTLTYPE_INT, "aggressive",
2127 SYSCTL_DESCR("1: L=2*SMSS 0: L=1*SMSS"),
2128 NULL, 0, &tcp_abc_aggressive, 0, CTL_CREATE, CTL_EOL);
2129
2130 /* MSL tuning subtree */
2131
2132 sysctl_createv(clog, 0, NULL, &mslt_node,
2133 CTLFLAG_PERMANENT, CTLTYPE_NODE, "mslt",
2134 SYSCTL_DESCR("MSL Tuning for TIME_WAIT truncation"),
2135 NULL, 0, NULL, 0,
2136 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2137 sysctl_createv(clog, 0, &mslt_node, NULL,
2138 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2139 CTLTYPE_INT, "enable",
2140 SYSCTL_DESCR("Enable TIME_WAIT truncation"),
2141 NULL, 0, &tcp_msl_enable, 0, CTL_CREATE, CTL_EOL);
2142 sysctl_createv(clog, 0, &mslt_node, NULL,
2143 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2144 CTLTYPE_INT, "loopback",
2145 SYSCTL_DESCR("MSL value to use for loopback connections"),
2146 NULL, 0, &tcp_msl_loop, 0, CTL_CREATE, CTL_EOL);
2147 sysctl_createv(clog, 0, &mslt_node, NULL,
2148 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2149 CTLTYPE_INT, "local",
2150 SYSCTL_DESCR("MSL value to use for local connections"),
2151 NULL, 0, &tcp_msl_local, 0, CTL_CREATE, CTL_EOL);
2152 sysctl_createv(clog, 0, &mslt_node, NULL,
2153 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2154 CTLTYPE_INT, "remote",
2155 SYSCTL_DESCR("MSL value to use for remote connections"),
2156 NULL, 0, &tcp_msl_remote, 0, CTL_CREATE, CTL_EOL);
2157 sysctl_createv(clog, 0, &mslt_node, NULL,
2158 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2159 CTLTYPE_INT, "remote_threshold",
2160 SYSCTL_DESCR("RTT estimate value to promote local to remote"),
2161 NULL, 0, &tcp_msl_remote_threshold, 0, CTL_CREATE, CTL_EOL);
2162
2163 /* vestigial TIME_WAIT tuning subtree */
2164
2165 sysctl_createv(clog, 0, NULL, &vtw_node,
2166 CTLFLAG_PERMANENT, CTLTYPE_NODE, "vtw",
2167 SYSCTL_DESCR("Tuning for Vestigial TIME_WAIT"),
2168 NULL, 0, NULL, 0,
2169 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2170 sysctl_createv(clog, 0, &vtw_node, NULL,
2171 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2172 CTLTYPE_INT, "enable",
2173 SYSCTL_DESCR("Enable Vestigial TIME_WAIT"),
2174 sysctl_tcp_vtw_enable, 0,
2175 (pf == AF_INET) ? &tcp4_vtw_enable : &tcp6_vtw_enable,
2176 0, CTL_CREATE, CTL_EOL);
2177 sysctl_createv(clog, 0, &vtw_node, NULL,
2178 CTLFLAG_PERMANENT|CTLFLAG_READONLY,
2179 CTLTYPE_INT, "entries",
2180 SYSCTL_DESCR("Maximum number of vestigial TIME_WAIT entries"),
2181 NULL, 0, &tcp_vtw_entries, 0, CTL_CREATE, CTL_EOL);
2182 }
2183
2184 void
2185 tcp_usrreq_init(void)
2186 {
2187
2188 #ifdef INET
2189 sysctl_net_inet_tcp_setup2(NULL, PF_INET, "inet", "tcp");
2190 #endif
2191 #ifdef INET6
2192 sysctl_net_inet_tcp_setup2(NULL, PF_INET6, "inet6", "tcp6");
2193 #endif
2194 }
2195
2196 PR_WRAP_USRREQS(tcp)
2197 #define tcp_attach tcp_attach_wrapper
2198 #define tcp_detach tcp_detach_wrapper
2199 #define tcp_usrreq tcp_usrreq_wrapper
2200
2201 const struct pr_usrreqs tcp_usrreqs = {
2202 .pr_attach = tcp_attach,
2203 .pr_detach = tcp_detach,
2204 .pr_generic = tcp_usrreq,
2205 };
2206