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