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