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