tcp_usrreq.c revision 1.181 1 /* $NetBSD: tcp_usrreq.c,v 1.181 2014/07/06 03:33:33 rtr 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.181 2014/07/06 03:33:33 rtr 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 KASSERT(req != PRU_CONTROL);
183 KASSERT(req != PRU_SENSE);
184
185 family = so->so_proto->pr_domain->dom_family;
186
187 s = splsoftnet();
188
189 if (req == PRU_PURGEIF) {
190 mutex_enter(softnet_lock);
191 switch (family) {
192 #ifdef INET
193 case PF_INET:
194 in_pcbpurgeif0(&tcbtable, (struct ifnet *)control);
195 in_purgeif((struct ifnet *)control);
196 in_pcbpurgeif(&tcbtable, (struct ifnet *)control);
197 break;
198 #endif
199 #ifdef INET6
200 case PF_INET6:
201 in6_pcbpurgeif0(&tcbtable, (struct ifnet *)control);
202 in6_purgeif((struct ifnet *)control);
203 in6_pcbpurgeif(&tcbtable, (struct ifnet *)control);
204 break;
205 #endif
206 default:
207 mutex_exit(softnet_lock);
208 splx(s);
209 return (EAFNOSUPPORT);
210 }
211 mutex_exit(softnet_lock);
212 splx(s);
213 return (0);
214 }
215
216 KASSERT(solocked(so));
217
218 switch (family) {
219 #ifdef INET
220 case PF_INET:
221 inp = sotoinpcb(so);
222 #ifdef INET6
223 in6p = NULL;
224 #endif
225 break;
226 #endif
227 #ifdef INET6
228 case PF_INET6:
229 inp = NULL;
230 in6p = sotoin6pcb(so);
231 break;
232 #endif
233 default:
234 splx(s);
235 return EAFNOSUPPORT;
236 }
237 KASSERT(!control || (req == PRU_SEND || req == PRU_SENDOOB));
238 #ifdef INET6
239 /* XXX: KASSERT((inp != NULL) ^ (in6p != NULL)); */
240 #endif
241 /*
242 * When a TCP is attached to a socket, then there will be
243 * a (struct inpcb) pointed at by the socket, and this
244 * structure will point at a subsidary (struct tcpcb).
245 */
246 if (inp == NULL
247 #ifdef INET6
248 && in6p == NULL
249 #endif
250 )
251 {
252 error = EINVAL;
253 goto release;
254 }
255 #ifdef INET
256 if (inp) {
257 tp = intotcpcb(inp);
258 /* WHAT IF TP IS 0? */
259 #ifdef KPROF
260 tcp_acounts[tp->t_state][req]++;
261 #endif
262 #ifdef TCP_DEBUG
263 ostate = tp->t_state;
264 #endif
265 }
266 #endif
267 #ifdef INET6
268 if (in6p) {
269 tp = in6totcpcb(in6p);
270 /* WHAT IF TP IS 0? */
271 #ifdef KPROF
272 tcp_acounts[tp->t_state][req]++;
273 #endif
274 #ifdef TCP_DEBUG
275 ostate = tp->t_state;
276 #endif
277 }
278 #endif
279
280 switch (req) {
281
282 /*
283 * Give the socket an address.
284 */
285 case PRU_BIND:
286 switch (family) {
287 #ifdef INET
288 case PF_INET:
289 error = in_pcbbind(inp, nam, l);
290 break;
291 #endif
292 #ifdef INET6
293 case PF_INET6:
294 error = in6_pcbbind(in6p, nam, l);
295 if (!error) {
296 /* mapped addr case */
297 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
298 tp->t_family = AF_INET;
299 else
300 tp->t_family = AF_INET6;
301 }
302 break;
303 #endif
304 }
305 break;
306
307 /*
308 * Prepare to accept connections.
309 */
310 case PRU_LISTEN:
311 #ifdef INET
312 if (inp && inp->inp_lport == 0) {
313 error = in_pcbbind(inp, NULL, l);
314 if (error)
315 break;
316 }
317 #endif
318 #ifdef INET6
319 if (in6p && in6p->in6p_lport == 0) {
320 error = in6_pcbbind(in6p, NULL, l);
321 if (error)
322 break;
323 }
324 #endif
325 tp->t_state = TCPS_LISTEN;
326 break;
327
328 /*
329 * Initiate connection to peer.
330 * Create a template for use in transmissions on this connection.
331 * Enter SYN_SENT state, and mark socket as connecting.
332 * Start keep-alive timer, and seed output sequence space.
333 * Send initial segment on connection.
334 */
335 case PRU_CONNECT:
336 #ifdef INET
337 if (inp) {
338 if (inp->inp_lport == 0) {
339 error = in_pcbbind(inp, NULL, l);
340 if (error)
341 break;
342 }
343 error = in_pcbconnect(inp, nam, l);
344 }
345 #endif
346 #ifdef INET6
347 if (in6p) {
348 if (in6p->in6p_lport == 0) {
349 error = in6_pcbbind(in6p, NULL, l);
350 if (error)
351 break;
352 }
353 error = in6_pcbconnect(in6p, nam, l);
354 if (!error) {
355 /* mapped addr case */
356 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
357 tp->t_family = AF_INET;
358 else
359 tp->t_family = AF_INET6;
360 }
361 }
362 #endif
363 if (error)
364 break;
365 tp->t_template = tcp_template(tp);
366 if (tp->t_template == 0) {
367 #ifdef INET
368 if (inp)
369 in_pcbdisconnect(inp);
370 #endif
371 #ifdef INET6
372 if (in6p)
373 in6_pcbdisconnect(in6p);
374 #endif
375 error = ENOBUFS;
376 break;
377 }
378 /*
379 * Compute window scaling to request.
380 * XXX: This should be moved to tcp_output().
381 */
382 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
383 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
384 tp->request_r_scale++;
385 soisconnecting(so);
386 TCP_STATINC(TCP_STAT_CONNATTEMPT);
387 tp->t_state = TCPS_SYN_SENT;
388 TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepinit);
389 tp->iss = tcp_new_iss(tp, 0);
390 tcp_sendseqinit(tp);
391 error = tcp_output(tp);
392 break;
393
394 /*
395 * Create a TCP connection between two sockets.
396 */
397 case PRU_CONNECT2:
398 error = EOPNOTSUPP;
399 break;
400
401 /*
402 * Initiate disconnect from peer.
403 * If connection never passed embryonic stage, just drop;
404 * else if don't need to let data drain, then can just drop anyways,
405 * else have to begin TCP shutdown process: mark socket disconnecting,
406 * drain unread data, state switch to reflect user close, and
407 * send segment (e.g. FIN) to peer. Socket will be really disconnected
408 * when peer sends FIN and acks ours.
409 *
410 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
411 */
412 case PRU_DISCONNECT:
413 tp = tcp_disconnect(tp);
414 break;
415
416 /*
417 * Accept a connection. Essentially all the work is
418 * done at higher levels; just return the address
419 * of the peer, storing through addr.
420 */
421 case PRU_ACCEPT:
422 #ifdef INET
423 if (inp)
424 in_setpeeraddr(inp, nam);
425 #endif
426 #ifdef INET6
427 if (in6p)
428 in6_setpeeraddr(in6p, nam);
429 #endif
430 break;
431
432 /*
433 * Mark the connection as being incapable of further output.
434 */
435 case PRU_SHUTDOWN:
436 socantsendmore(so);
437 tp = tcp_usrclosed(tp);
438 if (tp)
439 error = tcp_output(tp);
440 break;
441
442 /*
443 * After a receive, possibly send window update to peer.
444 */
445 case PRU_RCVD:
446 /*
447 * soreceive() calls this function when a user receives
448 * ancillary data on a listening socket. We don't call
449 * tcp_output in such a case, since there is no header
450 * template for a listening socket and hence the kernel
451 * will panic.
452 */
453 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) != 0)
454 (void) tcp_output(tp);
455 break;
456
457 /*
458 * Do a send by putting data in output queue and updating urgent
459 * marker if URG set. Possibly send more data.
460 */
461 case PRU_SEND:
462 if (control && control->m_len) {
463 m_freem(control);
464 m_freem(m);
465 error = EINVAL;
466 break;
467 }
468 sbappendstream(&so->so_snd, m);
469 error = tcp_output(tp);
470 break;
471
472 /*
473 * Abort the TCP.
474 */
475 case PRU_ABORT:
476 tp = tcp_drop(tp, ECONNABORTED);
477 break;
478
479 case PRU_RCVOOB:
480 if (control && control->m_len) {
481 m_freem(control);
482 m_freem(m);
483 error = EINVAL;
484 break;
485 }
486 if ((so->so_oobmark == 0 &&
487 (so->so_state & SS_RCVATMARK) == 0) ||
488 so->so_options & SO_OOBINLINE ||
489 tp->t_oobflags & TCPOOB_HADDATA) {
490 error = EINVAL;
491 break;
492 }
493 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
494 error = EWOULDBLOCK;
495 break;
496 }
497 m->m_len = 1;
498 *mtod(m, char *) = tp->t_iobc;
499 if (((long)nam & MSG_PEEK) == 0)
500 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
501 break;
502
503 case PRU_SENDOOB:
504 if (sbspace(&so->so_snd) < -512) {
505 m_freem(m);
506 error = ENOBUFS;
507 break;
508 }
509 /*
510 * According to RFC961 (Assigned Protocols),
511 * the urgent pointer points to the last octet
512 * of urgent data. We continue, however,
513 * to consider it to indicate the first octet
514 * of data past the urgent section.
515 * Otherwise, snd_up should be one lower.
516 */
517 sbappendstream(&so->so_snd, m);
518 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
519 tp->t_force = 1;
520 error = tcp_output(tp);
521 tp->t_force = 0;
522 break;
523
524 case PRU_SOCKADDR:
525 #ifdef INET
526 if (inp)
527 in_setsockaddr(inp, nam);
528 #endif
529 #ifdef INET6
530 if (in6p)
531 in6_setsockaddr(in6p, nam);
532 #endif
533 break;
534
535 case PRU_PEERADDR:
536 #ifdef INET
537 if (inp)
538 in_setpeeraddr(inp, nam);
539 #endif
540 #ifdef INET6
541 if (in6p)
542 in6_setpeeraddr(in6p, nam);
543 #endif
544 break;
545
546 default:
547 panic("tcp_usrreq");
548 }
549 #ifdef TCP_DEBUG
550 if (tp && (so->so_options & SO_DEBUG))
551 tcp_trace(TA_USER, ostate, tp, NULL, req);
552 #endif
553
554 release:
555 splx(s);
556 return (error);
557 }
558
559 static void
560 change_keepalive(struct socket *so, struct tcpcb *tp)
561 {
562 tp->t_maxidle = tp->t_keepcnt * tp->t_keepintvl;
563 TCP_TIMER_DISARM(tp, TCPT_KEEP);
564 TCP_TIMER_DISARM(tp, TCPT_2MSL);
565
566 if (tp->t_state == TCPS_SYN_RECEIVED ||
567 tp->t_state == TCPS_SYN_SENT) {
568 TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepinit);
569 } else if (so->so_options & SO_KEEPALIVE &&
570 tp->t_state <= TCPS_CLOSE_WAIT) {
571 TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepintvl);
572 } else {
573 TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepidle);
574 }
575
576 if ((tp->t_state == TCPS_FIN_WAIT_2) && (tp->t_maxidle > 0))
577 TCP_TIMER_ARM(tp, TCPT_2MSL, tp->t_maxidle);
578 }
579
580
581 int
582 tcp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
583 {
584 int error = 0, s;
585 struct inpcb *inp;
586 #ifdef INET6
587 struct in6pcb *in6p;
588 #endif
589 struct tcpcb *tp;
590 u_int ui;
591 int family; /* family of the socket */
592 int level, optname, optval;
593
594 level = sopt->sopt_level;
595 optname = sopt->sopt_name;
596
597 family = so->so_proto->pr_domain->dom_family;
598
599 s = splsoftnet();
600 switch (family) {
601 #ifdef INET
602 case PF_INET:
603 inp = sotoinpcb(so);
604 #ifdef INET6
605 in6p = NULL;
606 #endif
607 break;
608 #endif
609 #ifdef INET6
610 case PF_INET6:
611 inp = NULL;
612 in6p = sotoin6pcb(so);
613 break;
614 #endif
615 default:
616 splx(s);
617 panic("%s: af %d", __func__, family);
618 }
619 #ifndef INET6
620 if (inp == NULL)
621 #else
622 if (inp == NULL && in6p == NULL)
623 #endif
624 {
625 splx(s);
626 return (ECONNRESET);
627 }
628 if (level != IPPROTO_TCP) {
629 switch (family) {
630 #ifdef INET
631 case PF_INET:
632 error = ip_ctloutput(op, so, sopt);
633 break;
634 #endif
635 #ifdef INET6
636 case PF_INET6:
637 error = ip6_ctloutput(op, so, sopt);
638 break;
639 #endif
640 }
641 splx(s);
642 return (error);
643 }
644 if (inp)
645 tp = intotcpcb(inp);
646 #ifdef INET6
647 else if (in6p)
648 tp = in6totcpcb(in6p);
649 #endif
650 else
651 tp = NULL;
652
653 switch (op) {
654 case PRCO_SETOPT:
655 switch (optname) {
656 #ifdef TCP_SIGNATURE
657 case TCP_MD5SIG:
658 error = sockopt_getint(sopt, &optval);
659 if (error)
660 break;
661 if (optval > 0)
662 tp->t_flags |= TF_SIGNATURE;
663 else
664 tp->t_flags &= ~TF_SIGNATURE;
665 break;
666 #endif /* TCP_SIGNATURE */
667
668 case TCP_NODELAY:
669 error = sockopt_getint(sopt, &optval);
670 if (error)
671 break;
672 if (optval)
673 tp->t_flags |= TF_NODELAY;
674 else
675 tp->t_flags &= ~TF_NODELAY;
676 break;
677
678 case TCP_MAXSEG:
679 error = sockopt_getint(sopt, &optval);
680 if (error)
681 break;
682 if (optval > 0 && optval <= tp->t_peermss)
683 tp->t_peermss = optval; /* limit on send size */
684 else
685 error = EINVAL;
686 break;
687 #ifdef notyet
688 case TCP_CONGCTL:
689 /* XXX string overflow XXX */
690 error = tcp_congctl_select(tp, sopt->sopt_data);
691 break;
692 #endif
693
694 case TCP_KEEPIDLE:
695 error = sockopt_get(sopt, &ui, sizeof(ui));
696 if (error)
697 break;
698 if (ui > 0) {
699 tp->t_keepidle = ui;
700 change_keepalive(so, tp);
701 } else
702 error = EINVAL;
703 break;
704
705 case TCP_KEEPINTVL:
706 error = sockopt_get(sopt, &ui, sizeof(ui));
707 if (error)
708 break;
709 if (ui > 0) {
710 tp->t_keepintvl = ui;
711 change_keepalive(so, tp);
712 } else
713 error = EINVAL;
714 break;
715
716 case TCP_KEEPCNT:
717 error = sockopt_get(sopt, &ui, sizeof(ui));
718 if (error)
719 break;
720 if (ui > 0) {
721 tp->t_keepcnt = ui;
722 change_keepalive(so, tp);
723 } else
724 error = EINVAL;
725 break;
726
727 case TCP_KEEPINIT:
728 error = sockopt_get(sopt, &ui, sizeof(ui));
729 if (error)
730 break;
731 if (ui > 0) {
732 tp->t_keepinit = ui;
733 change_keepalive(so, tp);
734 } else
735 error = EINVAL;
736 break;
737
738 default:
739 error = ENOPROTOOPT;
740 break;
741 }
742 break;
743
744 case PRCO_GETOPT:
745 switch (optname) {
746 #ifdef TCP_SIGNATURE
747 case TCP_MD5SIG:
748 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
749 error = sockopt_set(sopt, &optval, sizeof(optval));
750 break;
751 #endif
752 case TCP_NODELAY:
753 optval = tp->t_flags & TF_NODELAY;
754 error = sockopt_set(sopt, &optval, sizeof(optval));
755 break;
756 case TCP_MAXSEG:
757 optval = tp->t_peermss;
758 error = sockopt_set(sopt, &optval, sizeof(optval));
759 break;
760 #ifdef notyet
761 case TCP_CONGCTL:
762 break;
763 #endif
764 default:
765 error = ENOPROTOOPT;
766 break;
767 }
768 break;
769 }
770 splx(s);
771 return (error);
772 }
773
774 #ifndef TCP_SENDSPACE
775 #define TCP_SENDSPACE 1024*32
776 #endif
777 int tcp_sendspace = TCP_SENDSPACE;
778 #ifndef TCP_RECVSPACE
779 #define TCP_RECVSPACE 1024*32
780 #endif
781 int tcp_recvspace = TCP_RECVSPACE;
782
783 /*
784 * tcp_attach: attach TCP protocol to socket, allocating internet protocol
785 * control block, TCP control block, buffer space and entering LISTEN state
786 * if to accept connections.
787 */
788 static int
789 tcp_attach(struct socket *so, int proto)
790 {
791 struct tcpcb *tp;
792 struct inpcb *inp;
793 #ifdef INET6
794 struct in6pcb *in6p;
795 #endif
796 int s, error, family;
797
798 /* Assign the lock (must happen even if we will error out). */
799 s = splsoftnet();
800 sosetlock(so);
801 KASSERT(solocked(so));
802
803 family = so->so_proto->pr_domain->dom_family;
804 switch (family) {
805 #ifdef INET
806 case PF_INET:
807 inp = sotoinpcb(so);
808 #ifdef INET6
809 in6p = NULL;
810 #endif
811 break;
812 #endif
813 #ifdef INET6
814 case PF_INET6:
815 inp = NULL;
816 in6p = sotoin6pcb(so);
817 break;
818 #endif
819 default:
820 error = EAFNOSUPPORT;
821 goto out;
822 }
823
824 KASSERT(inp == NULL);
825 #ifdef INET6
826 KASSERT(in6p == NULL);
827 #endif
828
829 #ifdef MBUFTRACE
830 so->so_mowner = &tcp_sock_mowner;
831 so->so_rcv.sb_mowner = &tcp_sock_rx_mowner;
832 so->so_snd.sb_mowner = &tcp_sock_tx_mowner;
833 #endif
834 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
835 error = soreserve(so, tcp_sendspace, tcp_recvspace);
836 if (error)
837 goto out;
838 }
839
840 so->so_rcv.sb_flags |= SB_AUTOSIZE;
841 so->so_snd.sb_flags |= SB_AUTOSIZE;
842
843 switch (family) {
844 #ifdef INET
845 case PF_INET:
846 error = in_pcballoc(so, &tcbtable);
847 if (error)
848 goto out;
849 inp = sotoinpcb(so);
850 #ifdef INET6
851 in6p = NULL;
852 #endif
853 break;
854 #endif
855 #ifdef INET6
856 case PF_INET6:
857 error = in6_pcballoc(so, &tcbtable);
858 if (error)
859 goto out;
860 inp = NULL;
861 in6p = sotoin6pcb(so);
862 break;
863 #endif
864 default:
865 error = EAFNOSUPPORT;
866 goto out;
867 }
868 if (inp)
869 tp = tcp_newtcpcb(family, (void *)inp);
870 #ifdef INET6
871 else if (in6p)
872 tp = tcp_newtcpcb(family, (void *)in6p);
873 #endif
874 else
875 tp = NULL;
876
877 if (tp == NULL) {
878 int nofd = so->so_state & SS_NOFDREF; /* XXX */
879
880 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
881 #ifdef INET
882 if (inp)
883 in_pcbdetach(inp);
884 #endif
885 #ifdef INET6
886 if (in6p)
887 in6_pcbdetach(in6p);
888 #endif
889 so->so_state |= nofd;
890 error = ENOBUFS;
891 goto out;
892 }
893 tp->t_state = TCPS_CLOSED;
894 if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
895 so->so_linger = TCP_LINGERTIME;
896 }
897 out:
898 KASSERT(solocked(so));
899 splx(s);
900 return error;
901 }
902
903 static void
904 tcp_detach(struct socket *so)
905 {
906 struct inpcb *inp;
907 #ifdef INET6
908 struct in6pcb *in6p;
909 #endif
910 struct tcpcb *tp = NULL;
911 int s, family;
912
913 KASSERT(solocked(so));
914
915 s = splsoftnet();
916 family = so->so_proto->pr_domain->dom_family;
917 switch (family) {
918 #ifdef INET
919 case PF_INET:
920 inp = sotoinpcb(so);
921 tp = intotcpcb(inp);
922 break;
923 #endif
924 #ifdef INET6
925 case PF_INET6:
926 in6p = sotoin6pcb(so);
927 tp = in6totcpcb(in6p);
928 break;
929 #endif
930 default:
931 splx(s);
932 return;
933 }
934 KASSERT(tp != NULL);
935 (void)tcp_disconnect(tp);
936 splx(s);
937 }
938
939 static int
940 tcp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
941 {
942 switch (so->so_proto->pr_domain->dom_family) {
943 #ifdef INET
944 case PF_INET:
945 return in_control(so, cmd, nam, ifp);
946 #endif
947 #ifdef INET6
948 case PF_INET6:
949 return in6_control(so, cmd, nam, ifp);
950 #endif
951 default:
952 return EAFNOSUPPORT;
953 }
954 }
955
956 static int
957 tcp_stat(struct socket *so, struct stat *ub)
958 {
959 /* stat: don't bother with a blocksize. */
960 return 0;
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_ioctl tcp_ioctl_wrapper
2200 #define tcp_stat tcp_stat_wrapper
2201 #define tcp_usrreq tcp_usrreq_wrapper
2202
2203 const struct pr_usrreqs tcp_usrreqs = {
2204 .pr_attach = tcp_attach,
2205 .pr_detach = tcp_detach,
2206 .pr_ioctl = tcp_ioctl,
2207 .pr_stat = tcp_stat,
2208 .pr_generic = tcp_usrreq,
2209 };
2210