tcp_usrreq.c revision 1.113.8.2 1 /* $NetBSD: tcp_usrreq.c,v 1.113.8.2 2006/08/11 15:46:33 yamt 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 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 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the NetBSD
53 * Foundation, Inc. and its contributors.
54 * 4. Neither the name of The NetBSD Foundation nor the names of its
55 * contributors may be used to endorse or promote products derived
56 * from this software without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
59 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
60 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
61 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
62 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
67 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68 * POSSIBILITY OF SUCH DAMAGE.
69 */
70
71 /*
72 * Copyright (c) 1982, 1986, 1988, 1993, 1995
73 * The Regents of the University of California. All rights reserved.
74 *
75 * Redistribution and use in source and binary forms, with or without
76 * modification, are permitted provided that the following conditions
77 * are met:
78 * 1. Redistributions of source code must retain the above copyright
79 * notice, this list of conditions and the following disclaimer.
80 * 2. Redistributions in binary form must reproduce the above copyright
81 * notice, this list of conditions and the following disclaimer in the
82 * documentation and/or other materials provided with the distribution.
83 * 3. Neither the name of the University nor the names of its contributors
84 * may be used to endorse or promote products derived from this software
85 * without specific prior written permission.
86 *
87 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
88 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
89 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
90 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
91 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
92 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
93 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
94 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
95 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
96 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
97 * SUCH DAMAGE.
98 *
99 * @(#)tcp_usrreq.c 8.5 (Berkeley) 6/21/95
100 */
101
102 #include <sys/cdefs.h>
103 __KERNEL_RCSID(0, "$NetBSD: tcp_usrreq.c,v 1.113.8.2 2006/08/11 15:46:33 yamt Exp $");
104
105 #include "opt_inet.h"
106 #include "opt_ipsec.h"
107 #include "opt_tcp_debug.h"
108 #include "opt_mbuftrace.h"
109
110 #include <sys/param.h>
111 #include <sys/systm.h>
112 #include <sys/kernel.h>
113 #include <sys/malloc.h>
114 #include <sys/mbuf.h>
115 #include <sys/socket.h>
116 #include <sys/socketvar.h>
117 #include <sys/protosw.h>
118 #include <sys/errno.h>
119 #include <sys/stat.h>
120 #include <sys/proc.h>
121 #include <sys/domain.h>
122 #include <sys/sysctl.h>
123 #include <sys/kauth.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 #endif
144
145 #include <netinet/tcp.h>
146 #include <netinet/tcp_fsm.h>
147 #include <netinet/tcp_seq.h>
148 #include <netinet/tcp_timer.h>
149 #include <netinet/tcp_var.h>
150 #include <netinet/tcpip.h>
151 #include <netinet/tcp_debug.h>
152
153 #include "opt_tcp_space.h"
154
155 #ifdef IPSEC
156 #include <netinet6/ipsec.h>
157 #endif /*IPSEC*/
158
159 /*
160 * TCP protocol interface to socket abstraction.
161 */
162
163 /*
164 * Process a TCP user request for TCP tb. If this is a send request
165 * then m is the mbuf chain of send data. If this is a timer expiration
166 * (called from the software clock routine), then timertype tells which timer.
167 */
168 /*ARGSUSED*/
169 int
170 tcp_usrreq(struct socket *so, int req,
171 struct mbuf *m, struct mbuf *nam, struct mbuf *control, struct lwp *l)
172 {
173 struct inpcb *inp;
174 #ifdef INET6
175 struct in6pcb *in6p;
176 #endif
177 struct tcpcb *tp = NULL;
178 int s;
179 int error = 0;
180 #ifdef TCP_DEBUG
181 int ostate = 0;
182 #endif
183 int family; /* family of the socket */
184
185 family = so->so_proto->pr_domain->dom_family;
186
187 if (req == PRU_CONTROL) {
188 switch (family) {
189 #ifdef INET
190 case PF_INET:
191 return (in_control(so, (long)m, (caddr_t)nam,
192 (struct ifnet *)control, l));
193 #endif
194 #ifdef INET6
195 case PF_INET6:
196 return (in6_control(so, (long)m, (caddr_t)nam,
197 (struct ifnet *)control, l));
198 #endif
199 default:
200 return EAFNOSUPPORT;
201 }
202 }
203
204 if (req == PRU_PURGEIF) {
205 switch (family) {
206 #ifdef INET
207 case PF_INET:
208 in_pcbpurgeif0(&tcbtable, (struct ifnet *)control);
209 in_purgeif((struct ifnet *)control);
210 in_pcbpurgeif(&tcbtable, (struct ifnet *)control);
211 break;
212 #endif
213 #ifdef INET6
214 case PF_INET6:
215 in6_pcbpurgeif0(&tcbtable, (struct ifnet *)control);
216 in6_purgeif((struct ifnet *)control);
217 in6_pcbpurgeif(&tcbtable, (struct ifnet *)control);
218 break;
219 #endif
220 default:
221 return (EAFNOSUPPORT);
222 }
223 return (0);
224 }
225
226 s = splsoftnet();
227 switch (family) {
228 #ifdef INET
229 case PF_INET:
230 inp = sotoinpcb(so);
231 #ifdef INET6
232 in6p = NULL;
233 #endif
234 break;
235 #endif
236 #ifdef INET6
237 case PF_INET6:
238 inp = NULL;
239 in6p = sotoin6pcb(so);
240 break;
241 #endif
242 default:
243 splx(s);
244 return EAFNOSUPPORT;
245 }
246
247 #ifdef DIAGNOSTIC
248 #ifdef INET6
249 if (inp && in6p)
250 panic("tcp_usrreq: both inp and in6p set to non-NULL");
251 #endif
252 if (req != PRU_SEND && req != PRU_SENDOOB && control)
253 panic("tcp_usrreq: unexpected control mbuf");
254 #endif
255 /*
256 * When a TCP is attached to a socket, then there will be
257 * a (struct inpcb) pointed at by the socket, and this
258 * structure will point at a subsidary (struct tcpcb).
259 */
260 #ifndef INET6
261 if (inp == 0 && req != PRU_ATTACH)
262 #else
263 if ((inp == 0 && in6p == 0) && req != PRU_ATTACH)
264 #endif
265 {
266 error = EINVAL;
267 goto release;
268 }
269 #ifdef INET
270 if (inp) {
271 tp = intotcpcb(inp);
272 /* WHAT IF TP IS 0? */
273 #ifdef KPROF
274 tcp_acounts[tp->t_state][req]++;
275 #endif
276 #ifdef TCP_DEBUG
277 ostate = tp->t_state;
278 #endif
279 }
280 #endif
281 #ifdef INET6
282 if (in6p) {
283 tp = in6totcpcb(in6p);
284 /* WHAT IF TP IS 0? */
285 #ifdef KPROF
286 tcp_acounts[tp->t_state][req]++;
287 #endif
288 #ifdef TCP_DEBUG
289 ostate = tp->t_state;
290 #endif
291 }
292 #endif
293
294 switch (req) {
295
296 /*
297 * TCP attaches to socket via PRU_ATTACH, reserving space,
298 * and an internet control block.
299 */
300 case PRU_ATTACH:
301 #ifndef INET6
302 if (inp != 0)
303 #else
304 if (inp != 0 || in6p != 0)
305 #endif
306 {
307 error = EISCONN;
308 break;
309 }
310 error = tcp_attach(so);
311 if (error)
312 break;
313 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
314 so->so_linger = TCP_LINGERTIME;
315 tp = sototcpcb(so);
316 break;
317
318 /*
319 * PRU_DETACH detaches the TCP protocol from the socket.
320 */
321 case PRU_DETACH:
322 tp = tcp_disconnect(tp);
323 break;
324
325 /*
326 * Give the socket an address.
327 */
328 case PRU_BIND:
329 switch (family) {
330 #ifdef INET
331 case PF_INET:
332 error = in_pcbbind(inp, nam, l);
333 break;
334 #endif
335 #ifdef INET6
336 case PF_INET6:
337 error = in6_pcbbind(in6p, nam, l);
338 if (!error) {
339 /* mapped addr case */
340 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
341 tp->t_family = AF_INET;
342 else
343 tp->t_family = AF_INET6;
344 }
345 break;
346 #endif
347 }
348 break;
349
350 /*
351 * Prepare to accept connections.
352 */
353 case PRU_LISTEN:
354 #ifdef INET
355 if (inp && inp->inp_lport == 0) {
356 error = in_pcbbind(inp, (struct mbuf *)0,
357 (struct lwp *)0);
358 if (error)
359 break;
360 }
361 #endif
362 #ifdef INET6
363 if (in6p && in6p->in6p_lport == 0) {
364 error = in6_pcbbind(in6p, (struct mbuf *)0,
365 (struct lwp *)0);
366 if (error)
367 break;
368 }
369 #endif
370 tp->t_state = TCPS_LISTEN;
371 break;
372
373 /*
374 * Initiate connection to peer.
375 * Create a template for use in transmissions on this connection.
376 * Enter SYN_SENT state, and mark socket as connecting.
377 * Start keep-alive timer, and seed output sequence space.
378 * Send initial segment on connection.
379 */
380 case PRU_CONNECT:
381 #ifdef INET
382 if (inp) {
383 if (inp->inp_lport == 0) {
384 error = in_pcbbind(inp, (struct mbuf *)0,
385 (struct lwp *)0);
386 if (error)
387 break;
388 }
389 error = in_pcbconnect(inp, nam, l);
390 }
391 #endif
392 #ifdef INET6
393 if (in6p) {
394 if (in6p->in6p_lport == 0) {
395 error = in6_pcbbind(in6p, (struct mbuf *)0,
396 (struct lwp *)0);
397 if (error)
398 break;
399 }
400 error = in6_pcbconnect(in6p, nam, l);
401 if (!error) {
402 /* mapped addr case */
403 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
404 tp->t_family = AF_INET;
405 else
406 tp->t_family = AF_INET6;
407 }
408 }
409 #endif
410 if (error)
411 break;
412 tp->t_template = tcp_template(tp);
413 if (tp->t_template == 0) {
414 #ifdef INET
415 if (inp)
416 in_pcbdisconnect(inp);
417 #endif
418 #ifdef INET6
419 if (in6p)
420 in6_pcbdisconnect(in6p);
421 #endif
422 error = ENOBUFS;
423 break;
424 }
425 /* Compute window scaling to request. */
426 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
427 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
428 tp->request_r_scale++;
429 soisconnecting(so);
430 tcpstat.tcps_connattempt++;
431 tp->t_state = TCPS_SYN_SENT;
432 TCP_TIMER_ARM(tp, TCPT_KEEP, TCPTV_KEEP_INIT);
433 tp->iss = tcp_new_iss(tp, 0);
434 tcp_sendseqinit(tp);
435 error = tcp_output(tp);
436 break;
437
438 /*
439 * Create a TCP connection between two sockets.
440 */
441 case PRU_CONNECT2:
442 error = EOPNOTSUPP;
443 break;
444
445 /*
446 * Initiate disconnect from peer.
447 * If connection never passed embryonic stage, just drop;
448 * else if don't need to let data drain, then can just drop anyways,
449 * else have to begin TCP shutdown process: mark socket disconnecting,
450 * drain unread data, state switch to reflect user close, and
451 * send segment (e.g. FIN) to peer. Socket will be really disconnected
452 * when peer sends FIN and acks ours.
453 *
454 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
455 */
456 case PRU_DISCONNECT:
457 tp = tcp_disconnect(tp);
458 break;
459
460 /*
461 * Accept a connection. Essentially all the work is
462 * done at higher levels; just return the address
463 * of the peer, storing through addr.
464 */
465 case PRU_ACCEPT:
466 #ifdef INET
467 if (inp)
468 in_setpeeraddr(inp, nam);
469 #endif
470 #ifdef INET6
471 if (in6p)
472 in6_setpeeraddr(in6p, nam);
473 #endif
474 break;
475
476 /*
477 * Mark the connection as being incapable of further output.
478 */
479 case PRU_SHUTDOWN:
480 socantsendmore(so);
481 tp = tcp_usrclosed(tp);
482 if (tp)
483 error = tcp_output(tp);
484 break;
485
486 /*
487 * After a receive, possibly send window update to peer.
488 */
489 case PRU_RCVD:
490 /*
491 * soreceive() calls this function when a user receives
492 * ancillary data on a listening socket. We don't call
493 * tcp_output in such a case, since there is no header
494 * template for a listening socket and hence the kernel
495 * will panic.
496 */
497 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) != 0)
498 (void) tcp_output(tp);
499 break;
500
501 /*
502 * Do a send by putting data in output queue and updating urgent
503 * marker if URG set. Possibly send more data.
504 */
505 case PRU_SEND:
506 if (control && control->m_len) {
507 m_freem(control);
508 m_freem(m);
509 error = EINVAL;
510 break;
511 }
512 sbappendstream(&so->so_snd, m);
513 error = tcp_output(tp);
514 break;
515
516 /*
517 * Abort the TCP.
518 */
519 case PRU_ABORT:
520 tp = tcp_drop(tp, ECONNABORTED);
521 break;
522
523 case PRU_SENSE:
524 /*
525 * stat: don't bother with a blocksize.
526 */
527 splx(s);
528 return (0);
529
530 case PRU_RCVOOB:
531 if (control && control->m_len) {
532 m_freem(control);
533 m_freem(m);
534 error = EINVAL;
535 break;
536 }
537 if ((so->so_oobmark == 0 &&
538 (so->so_state & SS_RCVATMARK) == 0) ||
539 so->so_options & SO_OOBINLINE ||
540 tp->t_oobflags & TCPOOB_HADDATA) {
541 error = EINVAL;
542 break;
543 }
544 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
545 error = EWOULDBLOCK;
546 break;
547 }
548 m->m_len = 1;
549 *mtod(m, caddr_t) = tp->t_iobc;
550 if (((long)nam & MSG_PEEK) == 0)
551 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
552 break;
553
554 case PRU_SENDOOB:
555 if (sbspace(&so->so_snd) < -512) {
556 m_freem(m);
557 error = ENOBUFS;
558 break;
559 }
560 /*
561 * According to RFC961 (Assigned Protocols),
562 * the urgent pointer points to the last octet
563 * of urgent data. We continue, however,
564 * to consider it to indicate the first octet
565 * of data past the urgent section.
566 * Otherwise, snd_up should be one lower.
567 */
568 sbappendstream(&so->so_snd, m);
569 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
570 tp->t_force = 1;
571 error = tcp_output(tp);
572 tp->t_force = 0;
573 break;
574
575 case PRU_SOCKADDR:
576 #ifdef INET
577 if (inp)
578 in_setsockaddr(inp, nam);
579 #endif
580 #ifdef INET6
581 if (in6p)
582 in6_setsockaddr(in6p, nam);
583 #endif
584 break;
585
586 case PRU_PEERADDR:
587 #ifdef INET
588 if (inp)
589 in_setpeeraddr(inp, nam);
590 #endif
591 #ifdef INET6
592 if (in6p)
593 in6_setpeeraddr(in6p, nam);
594 #endif
595 break;
596
597 default:
598 panic("tcp_usrreq");
599 }
600 #ifdef TCP_DEBUG
601 if (tp && (so->so_options & SO_DEBUG))
602 tcp_trace(TA_USER, ostate, tp, NULL, req);
603 #endif
604
605 release:
606 splx(s);
607 return (error);
608 }
609
610 int
611 tcp_ctloutput(int op, struct socket *so, int level, int optname,
612 struct mbuf **mp)
613 {
614 int error = 0, s;
615 struct inpcb *inp;
616 #ifdef INET6
617 struct in6pcb *in6p;
618 #endif
619 struct tcpcb *tp;
620 struct mbuf *m;
621 int i;
622 int family; /* family of the socket */
623
624 family = so->so_proto->pr_domain->dom_family;
625
626 s = splsoftnet();
627 switch (family) {
628 #ifdef INET
629 case PF_INET:
630 inp = sotoinpcb(so);
631 #ifdef INET6
632 in6p = NULL;
633 #endif
634 break;
635 #endif
636 #ifdef INET6
637 case PF_INET6:
638 inp = NULL;
639 in6p = sotoin6pcb(so);
640 break;
641 #endif
642 default:
643 splx(s);
644 return EAFNOSUPPORT;
645 }
646 #ifndef INET6
647 if (inp == NULL)
648 #else
649 if (inp == NULL && in6p == NULL)
650 #endif
651 {
652 splx(s);
653 if (op == PRCO_SETOPT && *mp)
654 (void) m_free(*mp);
655 return (ECONNRESET);
656 }
657 if (level != IPPROTO_TCP) {
658 switch (family) {
659 #ifdef INET
660 case PF_INET:
661 error = ip_ctloutput(op, so, level, optname, mp);
662 break;
663 #endif
664 #ifdef INET6
665 case PF_INET6:
666 error = ip6_ctloutput(op, so, level, optname, mp);
667 break;
668 #endif
669 }
670 splx(s);
671 return (error);
672 }
673 if (inp)
674 tp = intotcpcb(inp);
675 #ifdef INET6
676 else if (in6p)
677 tp = in6totcpcb(in6p);
678 #endif
679 else
680 tp = NULL;
681
682 switch (op) {
683
684 case PRCO_SETOPT:
685 m = *mp;
686 switch (optname) {
687
688 #ifdef TCP_SIGNATURE
689 case TCP_MD5SIG:
690 if (m == NULL || m->m_len < sizeof (int))
691 error = EINVAL;
692 if (error)
693 break;
694 if (*mtod(m, int *) > 0)
695 tp->t_flags |= TF_SIGNATURE;
696 else
697 tp->t_flags &= ~TF_SIGNATURE;
698 break;
699 #endif /* TCP_SIGNATURE */
700
701 case TCP_NODELAY:
702 if (m == NULL || m->m_len < sizeof (int))
703 error = EINVAL;
704 else if (*mtod(m, int *))
705 tp->t_flags |= TF_NODELAY;
706 else
707 tp->t_flags &= ~TF_NODELAY;
708 break;
709
710 case TCP_MAXSEG:
711 if (m && (i = *mtod(m, int *)) > 0 &&
712 i <= tp->t_peermss)
713 tp->t_peermss = i; /* limit on send size */
714 else
715 error = EINVAL;
716 break;
717
718 default:
719 error = ENOPROTOOPT;
720 break;
721 }
722 if (m)
723 (void) m_free(m);
724 break;
725
726 case PRCO_GETOPT:
727 *mp = m = m_get(M_WAIT, MT_SOOPTS);
728 m->m_len = sizeof(int);
729 MCLAIM(m, so->so_mowner);
730
731 switch (optname) {
732 #ifdef TCP_SIGNATURE
733 case TCP_MD5SIG:
734 *mtod(m, int *) = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
735 break;
736 #endif
737 case TCP_NODELAY:
738 *mtod(m, int *) = tp->t_flags & TF_NODELAY;
739 break;
740 case TCP_MAXSEG:
741 *mtod(m, int *) = tp->t_peermss;
742 break;
743 default:
744 error = ENOPROTOOPT;
745 break;
746 }
747 break;
748 }
749 splx(s);
750 return (error);
751 }
752
753 #ifndef TCP_SENDSPACE
754 #define TCP_SENDSPACE 1024*32
755 #endif
756 int tcp_sendspace = TCP_SENDSPACE;
757 #ifndef TCP_RECVSPACE
758 #define TCP_RECVSPACE 1024*32
759 #endif
760 int tcp_recvspace = TCP_RECVSPACE;
761
762 /*
763 * Attach TCP protocol to socket, allocating
764 * internet protocol control block, tcp control block,
765 * bufer space, and entering LISTEN state if to accept connections.
766 */
767 int
768 tcp_attach(struct socket *so)
769 {
770 struct tcpcb *tp;
771 struct inpcb *inp;
772 #ifdef INET6
773 struct in6pcb *in6p;
774 #endif
775 int error;
776 int family; /* family of the socket */
777
778 family = so->so_proto->pr_domain->dom_family;
779
780 #ifdef MBUFTRACE
781 so->so_mowner = &tcp_mowner;
782 so->so_rcv.sb_mowner = &tcp_rx_mowner;
783 so->so_snd.sb_mowner = &tcp_tx_mowner;
784 #endif
785 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
786 error = soreserve(so, tcp_sendspace, tcp_recvspace);
787 if (error)
788 return (error);
789 }
790 switch (family) {
791 #ifdef INET
792 case PF_INET:
793 error = in_pcballoc(so, &tcbtable);
794 if (error)
795 return (error);
796 inp = sotoinpcb(so);
797 #ifdef INET6
798 in6p = NULL;
799 #endif
800 break;
801 #endif
802 #ifdef INET6
803 case PF_INET6:
804 error = in6_pcballoc(so, &tcbtable);
805 if (error)
806 return (error);
807 inp = NULL;
808 in6p = sotoin6pcb(so);
809 break;
810 #endif
811 default:
812 return EAFNOSUPPORT;
813 }
814 if (inp)
815 tp = tcp_newtcpcb(family, (void *)inp);
816 #ifdef INET6
817 else if (in6p)
818 tp = tcp_newtcpcb(family, (void *)in6p);
819 #endif
820 else
821 tp = NULL;
822
823 if (tp == 0) {
824 int nofd = so->so_state & SS_NOFDREF; /* XXX */
825
826 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
827 #ifdef INET
828 if (inp)
829 in_pcbdetach(inp);
830 #endif
831 #ifdef INET6
832 if (in6p)
833 in6_pcbdetach(in6p);
834 #endif
835 so->so_state |= nofd;
836 return (ENOBUFS);
837 }
838 tp->t_state = TCPS_CLOSED;
839 return (0);
840 }
841
842 /*
843 * Initiate (or continue) disconnect.
844 * If embryonic state, just send reset (once).
845 * If in ``let data drain'' option and linger null, just drop.
846 * Otherwise (hard), mark socket disconnecting and drop
847 * current input data; switch states based on user close, and
848 * send segment to peer (with FIN).
849 */
850 struct tcpcb *
851 tcp_disconnect(struct tcpcb *tp)
852 {
853 struct socket *so;
854
855 if (tp->t_inpcb)
856 so = tp->t_inpcb->inp_socket;
857 #ifdef INET6
858 else if (tp->t_in6pcb)
859 so = tp->t_in6pcb->in6p_socket;
860 #endif
861 else
862 so = NULL;
863
864 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
865 tp = tcp_close(tp);
866 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
867 tp = tcp_drop(tp, 0);
868 else {
869 soisdisconnecting(so);
870 sbflush(&so->so_rcv);
871 tp = tcp_usrclosed(tp);
872 if (tp)
873 (void) tcp_output(tp);
874 }
875 return (tp);
876 }
877
878 /*
879 * User issued close, and wish to trail through shutdown states:
880 * if never received SYN, just forget it. If got a SYN from peer,
881 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
882 * If already got a FIN from peer, then almost done; go to LAST_ACK
883 * state. In all other cases, have already sent FIN to peer (e.g.
884 * after PRU_SHUTDOWN), and just have to play tedious game waiting
885 * for peer to send FIN or not respond to keep-alives, etc.
886 * We can let the user exit from the close as soon as the FIN is acked.
887 */
888 struct tcpcb *
889 tcp_usrclosed(struct tcpcb *tp)
890 {
891
892 switch (tp->t_state) {
893
894 case TCPS_CLOSED:
895 case TCPS_LISTEN:
896 case TCPS_SYN_SENT:
897 tp->t_state = TCPS_CLOSED;
898 tp = tcp_close(tp);
899 break;
900
901 case TCPS_SYN_RECEIVED:
902 case TCPS_ESTABLISHED:
903 tp->t_state = TCPS_FIN_WAIT_1;
904 break;
905
906 case TCPS_CLOSE_WAIT:
907 tp->t_state = TCPS_LAST_ACK;
908 break;
909 }
910 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
911 struct socket *so;
912 if (tp->t_inpcb)
913 so = tp->t_inpcb->inp_socket;
914 #ifdef INET6
915 else if (tp->t_in6pcb)
916 so = tp->t_in6pcb->in6p_socket;
917 #endif
918 else
919 so = NULL;
920 if (so)
921 soisdisconnected(so);
922 /*
923 * If we are in FIN_WAIT_2, we arrived here because the
924 * application did a shutdown of the send side. Like the
925 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
926 * a full close, we start a timer to make sure sockets are
927 * not left in FIN_WAIT_2 forever.
928 */
929 if ((tp->t_state == TCPS_FIN_WAIT_2) && (tcp_maxidle > 0))
930 TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_maxidle);
931 }
932 return (tp);
933 }
934
935 /*
936 * sysctl helper routine for net.inet.ip.mssdflt. it can't be less
937 * than 32.
938 */
939 static int
940 sysctl_net_inet_tcp_mssdflt(SYSCTLFN_ARGS)
941 {
942 int error, mssdflt;
943 struct sysctlnode node;
944
945 mssdflt = tcp_mssdflt;
946 node = *rnode;
947 node.sysctl_data = &mssdflt;
948 error = sysctl_lookup(SYSCTLFN_CALL(&node));
949 if (error || newp == NULL)
950 return (error);
951
952 if (mssdflt < 32)
953 return (EINVAL);
954 tcp_mssdflt = mssdflt;
955
956 return (0);
957 }
958
959 /*
960 * sysctl helper routine for setting port related values under
961 * net.inet.ip and net.inet6.ip6. does basic range checking and does
962 * additional checks for each type. this code has placed in
963 * tcp_input.c since INET and INET6 both use the same tcp code.
964 *
965 * this helper is not static so that both inet and inet6 can use it.
966 */
967 int
968 sysctl_net_inet_ip_ports(SYSCTLFN_ARGS)
969 {
970 int error, tmp;
971 int apmin, apmax;
972 #ifndef IPNOPRIVPORTS
973 int lpmin, lpmax;
974 #endif /* IPNOPRIVPORTS */
975 struct sysctlnode node;
976
977 if (namelen != 0)
978 return (EINVAL);
979
980 switch (name[-3]) {
981 #ifdef INET
982 case PF_INET:
983 apmin = anonportmin;
984 apmax = anonportmax;
985 #ifndef IPNOPRIVPORTS
986 lpmin = lowportmin;
987 lpmax = lowportmax;
988 #endif /* IPNOPRIVPORTS */
989 break;
990 #endif /* INET */
991 #ifdef INET6
992 case PF_INET6:
993 apmin = ip6_anonportmin;
994 apmax = ip6_anonportmax;
995 #ifndef IPNOPRIVPORTS
996 lpmin = ip6_lowportmin;
997 lpmax = ip6_lowportmax;
998 #endif /* IPNOPRIVPORTS */
999 break;
1000 #endif /* INET6 */
1001 default:
1002 return (EINVAL);
1003 }
1004
1005 /*
1006 * insert temporary copy into node, perform lookup on
1007 * temporary, then restore pointer
1008 */
1009 node = *rnode;
1010 tmp = *(int*)rnode->sysctl_data;
1011 node.sysctl_data = &tmp;
1012 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1013 if (error || newp == NULL)
1014 return (error);
1015
1016 /*
1017 * simple port range check
1018 */
1019 if (tmp < 0 || tmp > 65535)
1020 return (EINVAL);
1021
1022 /*
1023 * per-node range checks
1024 */
1025 switch (rnode->sysctl_num) {
1026 case IPCTL_ANONPORTMIN:
1027 if (tmp >= apmax)
1028 return (EINVAL);
1029 #ifndef IPNOPRIVPORTS
1030 if (tmp < IPPORT_RESERVED)
1031 return (EINVAL);
1032 #endif /* IPNOPRIVPORTS */
1033 break;
1034
1035 case IPCTL_ANONPORTMAX:
1036 if (apmin >= tmp)
1037 return (EINVAL);
1038 #ifndef IPNOPRIVPORTS
1039 if (tmp < IPPORT_RESERVED)
1040 return (EINVAL);
1041 #endif /* IPNOPRIVPORTS */
1042 break;
1043
1044 #ifndef IPNOPRIVPORTS
1045 case IPCTL_LOWPORTMIN:
1046 if (tmp >= lpmax ||
1047 tmp > IPPORT_RESERVEDMAX ||
1048 tmp < IPPORT_RESERVEDMIN)
1049 return (EINVAL);
1050 break;
1051
1052 case IPCTL_LOWPORTMAX:
1053 if (lpmin >= tmp ||
1054 tmp > IPPORT_RESERVEDMAX ||
1055 tmp < IPPORT_RESERVEDMIN)
1056 return (EINVAL);
1057 break;
1058 #endif /* IPNOPRIVPORTS */
1059
1060 default:
1061 return (EINVAL);
1062 }
1063
1064 *(int*)rnode->sysctl_data = tmp;
1065
1066 return (0);
1067 }
1068
1069 /*
1070 * sysctl helper routine for the net.inet.tcp.ident and
1071 * net.inet6.tcp6.ident nodes. contains backwards compat code for the
1072 * old way of looking up the ident information for ipv4 which involves
1073 * stuffing the port/addr pairs into the mib lookup.
1074 */
1075 static int
1076 sysctl_net_inet_tcp_ident(SYSCTLFN_ARGS)
1077 {
1078 #ifdef INET
1079 struct inpcb *inb;
1080 struct sockaddr_in *si4[2];
1081 #endif /* INET */
1082 #ifdef INET6
1083 struct in6pcb *in6b;
1084 struct sockaddr_in6 *si6[2];
1085 #endif /* INET6 */
1086 struct sockaddr_storage sa[2];
1087 struct socket *sockp;
1088 size_t sz;
1089 uid_t uid;
1090 int error, pf;
1091
1092 if (namelen != 4 && namelen != 0)
1093 return (EINVAL);
1094 if (name[-2] != IPPROTO_TCP)
1095 return (EINVAL);
1096 pf = name[-3];
1097
1098 /* old style lookup, ipv4 only */
1099 if (namelen == 4) {
1100 #ifdef INET
1101 struct in_addr laddr, raddr;
1102 u_int lport, rport;
1103
1104 if (pf != PF_INET)
1105 return (EPROTONOSUPPORT);
1106 raddr.s_addr = (uint32_t)name[0];
1107 rport = (u_int)name[1];
1108 laddr.s_addr = (uint32_t)name[2];
1109 lport = (u_int)name[3];
1110 inb = in_pcblookup_connect(&tcbtable, raddr, rport,
1111 laddr, lport);
1112 if (inb == NULL || (sockp = inb->inp_socket) == NULL)
1113 return (ESRCH);
1114 uid = sockp->so_uidinfo->ui_uid;
1115 if (oldp) {
1116 sz = MIN(sizeof(uid), *oldlenp);
1117 error = copyout(&uid, oldp, sz);
1118 if (error)
1119 return (error);
1120 }
1121 *oldlenp = sizeof(uid);
1122 return (0);
1123 #else /* INET */
1124 return (EINVAL);
1125 #endif /* INET */
1126 }
1127
1128 if (newp == NULL || newlen != sizeof(sa))
1129 return (EINVAL);
1130 error = copyin(newp, &sa, newlen);
1131 if (error)
1132 return (error);
1133
1134 /*
1135 * requested families must match
1136 */
1137 if (pf != sa[0].ss_family || sa[0].ss_family != sa[1].ss_family)
1138 return (EINVAL);
1139
1140 switch (pf) {
1141 #ifdef INET
1142 case PF_INET:
1143 si4[0] = (struct sockaddr_in*)&sa[0];
1144 si4[1] = (struct sockaddr_in*)&sa[1];
1145 if (si4[0]->sin_len != sizeof(*si4[0]) ||
1146 si4[0]->sin_len != si4[1]->sin_len)
1147 return (EINVAL);
1148 inb = in_pcblookup_connect(&tcbtable,
1149 si4[0]->sin_addr, si4[0]->sin_port,
1150 si4[1]->sin_addr, si4[1]->sin_port);
1151 if (inb == NULL || (sockp = inb->inp_socket) == NULL)
1152 return (ESRCH);
1153 break;
1154 #endif /* INET */
1155 #ifdef INET6
1156 case PF_INET6:
1157 si6[0] = (struct sockaddr_in6*)&sa[0];
1158 si6[1] = (struct sockaddr_in6*)&sa[1];
1159 if (si6[0]->sin6_len != sizeof(*si6[0]) ||
1160 si6[0]->sin6_len != si6[1]->sin6_len)
1161 return (EINVAL);
1162 in6b = in6_pcblookup_connect(&tcbtable,
1163 &si6[0]->sin6_addr, si6[0]->sin6_port,
1164 &si6[1]->sin6_addr, si6[1]->sin6_port, 0);
1165 if (in6b == NULL || (sockp = in6b->in6p_socket) == NULL)
1166 return (ESRCH);
1167 break;
1168 #endif /* INET6 */
1169 default:
1170 return (EPROTONOSUPPORT);
1171 }
1172 *oldlenp = sizeof(uid);
1173
1174 uid = sockp->so_uidinfo->ui_uid;
1175 if (oldp) {
1176 sz = MIN(sizeof(uid), *oldlenp);
1177 error = copyout(&uid, oldp, sz);
1178 if (error)
1179 return (error);
1180 }
1181 *oldlenp = sizeof(uid);
1182
1183 return (0);
1184 }
1185
1186 /*
1187 * sysctl helper for the inet and inet6 pcblists. handles tcp/udp and
1188 * inet/inet6, as well as raw pcbs for each. specifically not
1189 * declared static so that raw sockets and udp/udp6 can use it as
1190 * well.
1191 */
1192 int
1193 sysctl_inpcblist(SYSCTLFN_ARGS)
1194 {
1195 #ifdef INET
1196 struct sockaddr_in *in;
1197 const struct inpcb *inp;
1198 #endif
1199 #ifdef INET6
1200 struct sockaddr_in6 *in6;
1201 const struct in6pcb *in6p;
1202 #endif
1203 /*
1204 * sysctl_data is const, but CIRCLEQ_FOREACH can't use a const
1205 * struct inpcbtable pointer, so we have to discard const. :-/
1206 */
1207 struct inpcbtable *pcbtbl = __UNCONST(rnode->sysctl_data);
1208 const struct inpcb_hdr *inph;
1209 struct tcpcb *tp;
1210 struct kinfo_pcb pcb;
1211 char *dp;
1212 u_int op, arg;
1213 size_t len, needed, elem_size, out_size;
1214 int error, elem_count, pf, proto, pf2;
1215
1216 if (namelen != 4)
1217 return (EINVAL);
1218
1219 if (oldp != NULL) {
1220 len = *oldlenp;
1221 elem_size = name[2];
1222 elem_count = name[3];
1223 if (elem_size != sizeof(pcb))
1224 return EINVAL;
1225 } else {
1226 len = 0;
1227 elem_count = INT_MAX;
1228 elem_size = sizeof(pcb);
1229 }
1230 error = 0;
1231 dp = oldp;
1232 op = name[0];
1233 arg = name[1];
1234 out_size = elem_size;
1235 needed = 0;
1236
1237 if (namelen == 1 && name[0] == CTL_QUERY)
1238 return (sysctl_query(SYSCTLFN_CALL(rnode)));
1239
1240 if (name - oname != 4)
1241 return (EINVAL);
1242
1243 pf = oname[1];
1244 proto = oname[2];
1245 pf2 = (oldp != NULL) ? pf : 0;
1246
1247 CIRCLEQ_FOREACH(inph, &pcbtbl->inpt_queue, inph_queue) {
1248 #ifdef INET
1249 inp = (const struct inpcb *)inph;
1250 #endif
1251 #ifdef INET6
1252 in6p = (const struct in6pcb *)inph;
1253 #endif
1254
1255 if (inph->inph_af != pf)
1256 continue;
1257
1258 /* XXX elad - should be done better */
1259 if (security_curtain &&
1260 (kauth_cred_geteuid(l->l_cred) != 0) &&
1261 (kauth_cred_geteuid(l->l_cred) !=
1262 inph->inph_socket->so_uidinfo->ui_uid))
1263 continue;
1264
1265 memset(&pcb, 0, sizeof(pcb));
1266
1267 pcb.ki_family = pf;
1268 pcb.ki_type = proto;
1269
1270 switch (pf2) {
1271 case 0:
1272 /* just probing for size */
1273 break;
1274 #ifdef INET
1275 case PF_INET:
1276 pcb.ki_family = inp->inp_socket->so_proto->
1277 pr_domain->dom_family;
1278 pcb.ki_type = inp->inp_socket->so_proto->
1279 pr_type;
1280 pcb.ki_protocol = inp->inp_socket->so_proto->
1281 pr_protocol;
1282 pcb.ki_pflags = inp->inp_flags;
1283
1284 pcb.ki_sostate = inp->inp_socket->so_state;
1285 pcb.ki_prstate = inp->inp_state;
1286 if (proto == IPPROTO_TCP) {
1287 tp = intotcpcb(inp);
1288 pcb.ki_tstate = tp->t_state;
1289 pcb.ki_tflags = tp->t_flags;
1290 }
1291
1292 pcb.ki_pcbaddr = PTRTOUINT64(inp);
1293 pcb.ki_ppcbaddr = PTRTOUINT64(inp->inp_ppcb);
1294 pcb.ki_sockaddr = PTRTOUINT64(inp->inp_socket);
1295
1296 pcb.ki_rcvq = inp->inp_socket->so_rcv.sb_cc;
1297 pcb.ki_sndq = inp->inp_socket->so_snd.sb_cc;
1298
1299 in = satosin(&pcb.ki_src);
1300 in->sin_len = sizeof(*in);
1301 in->sin_family = pf;
1302 in->sin_port = inp->inp_lport;
1303 in->sin_addr = inp->inp_laddr;
1304 if (pcb.ki_prstate >= INP_CONNECTED) {
1305 in = satosin(&pcb.ki_dst);
1306 in->sin_len = sizeof(*in);
1307 in->sin_family = pf;
1308 in->sin_port = inp->inp_fport;
1309 in->sin_addr = inp->inp_faddr;
1310 }
1311 break;
1312 #endif
1313 #ifdef INET6
1314 case PF_INET6:
1315 pcb.ki_family = in6p->in6p_socket->so_proto->
1316 pr_domain->dom_family;
1317 pcb.ki_type = in6p->in6p_socket->so_proto->pr_type;
1318 pcb.ki_protocol = in6p->in6p_socket->so_proto->
1319 pr_protocol;
1320 pcb.ki_pflags = in6p->in6p_flags;
1321
1322 pcb.ki_sostate = in6p->in6p_socket->so_state;
1323 pcb.ki_prstate = in6p->in6p_state;
1324 if (proto == IPPROTO_TCP) {
1325 tp = in6totcpcb(in6p);
1326 pcb.ki_tstate = tp->t_state;
1327 pcb.ki_tflags = tp->t_flags;
1328 }
1329
1330 pcb.ki_pcbaddr = PTRTOUINT64(in6p);
1331 pcb.ki_ppcbaddr = PTRTOUINT64(in6p->in6p_ppcb);
1332 pcb.ki_sockaddr = PTRTOUINT64(in6p->in6p_socket);
1333
1334 pcb.ki_rcvq = in6p->in6p_socket->so_rcv.sb_cc;
1335 pcb.ki_sndq = in6p->in6p_socket->so_snd.sb_cc;
1336
1337 in6 = satosin6(&pcb.ki_src);
1338 in6->sin6_len = sizeof(*in6);
1339 in6->sin6_family = pf;
1340 in6->sin6_port = in6p->in6p_lport;
1341 in6->sin6_flowinfo = in6p->in6p_flowinfo;
1342 in6->sin6_addr = in6p->in6p_laddr;
1343 in6->sin6_scope_id = 0; /* XXX? */
1344
1345 if (pcb.ki_prstate >= IN6P_CONNECTED) {
1346 in6 = satosin6(&pcb.ki_dst);
1347 in6->sin6_len = sizeof(*in6);
1348 in6->sin6_family = pf;
1349 in6->sin6_port = in6p->in6p_fport;
1350 in6->sin6_flowinfo = in6p->in6p_flowinfo;
1351 in6->sin6_addr = in6p->in6p_faddr;
1352 in6->sin6_scope_id = 0; /* XXX? */
1353 }
1354 break;
1355 #endif
1356 }
1357
1358 if (len >= elem_size && elem_count > 0) {
1359 error = copyout(&pcb, dp, out_size);
1360 if (error)
1361 return (error);
1362 dp += elem_size;
1363 len -= elem_size;
1364 }
1365 if (elem_count > 0) {
1366 needed += elem_size;
1367 if (elem_count != INT_MAX)
1368 elem_count--;
1369 }
1370 }
1371
1372 *oldlenp = needed;
1373 if (oldp == NULL)
1374 *oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
1375
1376 return (error);
1377 }
1378
1379 /*
1380 * this (second stage) setup routine is a replacement for tcp_sysctl()
1381 * (which is currently used for ipv4 and ipv6)
1382 */
1383 static void
1384 sysctl_net_inet_tcp_setup2(struct sysctllog **clog, int pf, const char *pfname,
1385 const char *tcpname)
1386 {
1387 const struct sysctlnode *sack_node;
1388 #ifdef TCP_DEBUG
1389 extern struct tcp_debug tcp_debug[TCP_NDEBUG];
1390 extern int tcp_debx;
1391 #endif
1392
1393 sysctl_createv(clog, 0, NULL, NULL,
1394 CTLFLAG_PERMANENT,
1395 CTLTYPE_NODE, "net", NULL,
1396 NULL, 0, NULL, 0,
1397 CTL_NET, CTL_EOL);
1398 sysctl_createv(clog, 0, NULL, NULL,
1399 CTLFLAG_PERMANENT,
1400 CTLTYPE_NODE, pfname, NULL,
1401 NULL, 0, NULL, 0,
1402 CTL_NET, pf, CTL_EOL);
1403 sysctl_createv(clog, 0, NULL, NULL,
1404 CTLFLAG_PERMANENT,
1405 CTLTYPE_NODE, tcpname,
1406 SYSCTL_DESCR("TCP related settings"),
1407 NULL, 0, NULL, 0,
1408 CTL_NET, pf, IPPROTO_TCP, CTL_EOL);
1409
1410 sysctl_createv(clog, 0, NULL, NULL,
1411 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1412 CTLTYPE_INT, "rfc1323",
1413 SYSCTL_DESCR("Enable RFC1323 TCP extensions"),
1414 NULL, 0, &tcp_do_rfc1323, 0,
1415 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RFC1323, CTL_EOL);
1416 sysctl_createv(clog, 0, NULL, NULL,
1417 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1418 CTLTYPE_INT, "sendspace",
1419 SYSCTL_DESCR("Default TCP send buffer size"),
1420 NULL, 0, &tcp_sendspace, 0,
1421 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SENDSPACE, CTL_EOL);
1422 sysctl_createv(clog, 0, NULL, NULL,
1423 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1424 CTLTYPE_INT, "recvspace",
1425 SYSCTL_DESCR("Default TCP receive buffer size"),
1426 NULL, 0, &tcp_recvspace, 0,
1427 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RECVSPACE, CTL_EOL);
1428 sysctl_createv(clog, 0, NULL, NULL,
1429 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1430 CTLTYPE_INT, "mssdflt",
1431 SYSCTL_DESCR("Default maximum segment size"),
1432 sysctl_net_inet_tcp_mssdflt, 0, &tcp_mssdflt, 0,
1433 CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSSDFLT, CTL_EOL);
1434 sysctl_createv(clog, 0, NULL, NULL,
1435 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1436 CTLTYPE_INT, "syn_cache_limit",
1437 SYSCTL_DESCR("Maximum number of entries in the TCP "
1438 "compressed state engine"),
1439 NULL, 0, &tcp_syn_cache_limit, 0,
1440 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_LIMIT,
1441 CTL_EOL);
1442 sysctl_createv(clog, 0, NULL, NULL,
1443 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1444 CTLTYPE_INT, "syn_bucket_limit",
1445 SYSCTL_DESCR("Maximum number of entries per hash "
1446 "bucket in the TCP compressed state "
1447 "engine"),
1448 NULL, 0, &tcp_syn_bucket_limit, 0,
1449 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_BUCKET_LIMIT,
1450 CTL_EOL);
1451 #if 0 /* obsoleted */
1452 sysctl_createv(clog, 0, NULL, NULL,
1453 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1454 CTLTYPE_INT, "syn_cache_interval",
1455 SYSCTL_DESCR("TCP compressed state engine's timer interval"),
1456 NULL, 0, &tcp_syn_cache_interval, 0,
1457 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_INTER,
1458 CTL_EOL);
1459 #endif
1460 sysctl_createv(clog, 0, NULL, NULL,
1461 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1462 CTLTYPE_INT, "init_win",
1463 SYSCTL_DESCR("Initial TCP congestion window"),
1464 NULL, 0, &tcp_init_win, 0,
1465 CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN, CTL_EOL);
1466 sysctl_createv(clog, 0, NULL, NULL,
1467 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1468 CTLTYPE_INT, "mss_ifmtu",
1469 SYSCTL_DESCR("Use interface MTU for calculating MSS"),
1470 NULL, 0, &tcp_mss_ifmtu, 0,
1471 CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSS_IFMTU, CTL_EOL);
1472 sysctl_createv(clog, 0, NULL, &sack_node,
1473 CTLFLAG_PERMANENT,
1474 CTLTYPE_NODE, "sack",
1475 SYSCTL_DESCR("RFC2018 Selective ACKnowledgement tunables"),
1476 NULL, 0, NULL, 0,
1477 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_EOL);
1478 sysctl_createv(clog, 0, NULL, NULL,
1479 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1480 CTLTYPE_INT, "win_scale",
1481 SYSCTL_DESCR("Use RFC1323 window scale options"),
1482 NULL, 0, &tcp_do_win_scale, 0,
1483 CTL_NET, pf, IPPROTO_TCP, TCPCTL_WSCALE, CTL_EOL);
1484 sysctl_createv(clog, 0, NULL, NULL,
1485 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1486 CTLTYPE_INT, "timestamps",
1487 SYSCTL_DESCR("Use RFC1323 time stamp options"),
1488 NULL, 0, &tcp_do_timestamps, 0,
1489 CTL_NET, pf, IPPROTO_TCP, TCPCTL_TSTAMP, CTL_EOL);
1490 sysctl_createv(clog, 0, NULL, NULL,
1491 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1492 CTLTYPE_INT, "compat_42",
1493 SYSCTL_DESCR("Enable workarounds for 4.2BSD TCP bugs"),
1494 NULL, 0, &tcp_compat_42, 0,
1495 CTL_NET, pf, IPPROTO_TCP, TCPCTL_COMPAT_42, CTL_EOL);
1496 sysctl_createv(clog, 0, NULL, NULL,
1497 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1498 CTLTYPE_INT, "cwm",
1499 SYSCTL_DESCR("Hughes/Touch/Heidemann Congestion Window "
1500 "Monitoring"),
1501 NULL, 0, &tcp_cwm, 0,
1502 CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM, CTL_EOL);
1503 sysctl_createv(clog, 0, NULL, NULL,
1504 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1505 CTLTYPE_INT, "cwm_burstsize",
1506 SYSCTL_DESCR("Congestion Window Monitoring allowed "
1507 "burst count in packets"),
1508 NULL, 0, &tcp_cwm_burstsize, 0,
1509 CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM_BURSTSIZE,
1510 CTL_EOL);
1511 sysctl_createv(clog, 0, NULL, NULL,
1512 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1513 CTLTYPE_INT, "ack_on_push",
1514 SYSCTL_DESCR("Immediately return ACK when PSH is "
1515 "received"),
1516 NULL, 0, &tcp_ack_on_push, 0,
1517 CTL_NET, pf, IPPROTO_TCP, TCPCTL_ACK_ON_PUSH, CTL_EOL);
1518 sysctl_createv(clog, 0, NULL, NULL,
1519 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1520 CTLTYPE_INT, "keepidle",
1521 SYSCTL_DESCR("Allowed connection idle ticks before a "
1522 "keepalive probe is sent"),
1523 NULL, 0, &tcp_keepidle, 0,
1524 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPIDLE, CTL_EOL);
1525 sysctl_createv(clog, 0, NULL, NULL,
1526 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1527 CTLTYPE_INT, "keepintvl",
1528 SYSCTL_DESCR("Ticks before next keepalive probe is sent"),
1529 NULL, 0, &tcp_keepintvl, 0,
1530 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPINTVL, CTL_EOL);
1531 sysctl_createv(clog, 0, NULL, NULL,
1532 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1533 CTLTYPE_INT, "keepcnt",
1534 SYSCTL_DESCR("Number of keepalive probes to send"),
1535 NULL, 0, &tcp_keepcnt, 0,
1536 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPCNT, CTL_EOL);
1537 sysctl_createv(clog, 0, NULL, NULL,
1538 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
1539 CTLTYPE_INT, "slowhz",
1540 SYSCTL_DESCR("Keepalive ticks per second"),
1541 NULL, PR_SLOWHZ, NULL, 0,
1542 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SLOWHZ, CTL_EOL);
1543 sysctl_createv(clog, 0, NULL, NULL,
1544 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1545 CTLTYPE_INT, "newreno",
1546 SYSCTL_DESCR("NewReno congestion control algorithm"),
1547 NULL, 0, &tcp_do_newreno, 0,
1548 CTL_NET, pf, IPPROTO_TCP, TCPCTL_NEWRENO, CTL_EOL);
1549 sysctl_createv(clog, 0, NULL, NULL,
1550 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1551 CTLTYPE_INT, "log_refused",
1552 SYSCTL_DESCR("Log refused TCP connections"),
1553 NULL, 0, &tcp_log_refused, 0,
1554 CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOG_REFUSED, CTL_EOL);
1555 #if 0 /* obsoleted */
1556 sysctl_createv(clog, 0, NULL, NULL,
1557 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1558 CTLTYPE_INT, "rstratelimit", NULL,
1559 NULL, 0, &tcp_rst_ratelim, 0,
1560 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTRATELIMIT, CTL_EOL);
1561 #endif
1562 sysctl_createv(clog, 0, NULL, NULL,
1563 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1564 CTLTYPE_INT, "rstppslimit",
1565 SYSCTL_DESCR("Maximum number of RST packets to send "
1566 "per second"),
1567 NULL, 0, &tcp_rst_ppslim, 0,
1568 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTPPSLIMIT, CTL_EOL);
1569 sysctl_createv(clog, 0, NULL, NULL,
1570 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1571 CTLTYPE_INT, "delack_ticks",
1572 SYSCTL_DESCR("Number of ticks to delay sending an ACK"),
1573 NULL, 0, &tcp_delack_ticks, 0,
1574 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DELACK_TICKS, CTL_EOL);
1575 sysctl_createv(clog, 0, NULL, NULL,
1576 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1577 CTLTYPE_INT, "init_win_local",
1578 SYSCTL_DESCR("Initial TCP window size (in segments)"),
1579 NULL, 0, &tcp_init_win_local, 0,
1580 CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN_LOCAL,
1581 CTL_EOL);
1582 sysctl_createv(clog, 0, NULL, NULL,
1583 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1584 CTLTYPE_STRUCT, "ident",
1585 SYSCTL_DESCR("RFC1413 Identification Protocol lookups"),
1586 sysctl_net_inet_tcp_ident, 0, NULL, sizeof(uid_t),
1587 CTL_NET, pf, IPPROTO_TCP, TCPCTL_IDENT, CTL_EOL);
1588 sysctl_createv(clog, 0, NULL, NULL,
1589 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1590 CTLTYPE_INT, "do_loopback_cksum",
1591 SYSCTL_DESCR("Perform TCP checksum on loopback"),
1592 NULL, 0, &tcp_do_loopback_cksum, 0,
1593 CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOOPBACKCKSUM,
1594 CTL_EOL);
1595 sysctl_createv(clog, 0, NULL, NULL,
1596 CTLFLAG_PERMANENT,
1597 CTLTYPE_STRUCT, "pcblist",
1598 SYSCTL_DESCR("TCP protocol control block list"),
1599 sysctl_inpcblist, 0, &tcbtable, 0,
1600 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
1601 CTL_EOL);
1602
1603 /* SACK gets it's own little subtree. */
1604 sysctl_createv(clog, 0, NULL, &sack_node,
1605 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1606 CTLTYPE_INT, "enable",
1607 SYSCTL_DESCR("Enable RFC2018 Selective ACKnowledgement"),
1608 NULL, 0, &tcp_do_sack, 0,
1609 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1610 sysctl_createv(clog, 0, NULL, &sack_node,
1611 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1612 CTLTYPE_INT, "maxholes",
1613 SYSCTL_DESCR("Maximum number of TCP SACK holes allowed per connection"),
1614 NULL, 0, &tcp_sack_tp_maxholes, 0,
1615 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1616 sysctl_createv(clog, 0, NULL, &sack_node,
1617 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1618 CTLTYPE_INT, "globalmaxholes",
1619 SYSCTL_DESCR("Global maximum number of TCP SACK holes"),
1620 NULL, 0, &tcp_sack_globalmaxholes, 0,
1621 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1622 sysctl_createv(clog, 0, NULL, &sack_node,
1623 CTLFLAG_PERMANENT,
1624 CTLTYPE_INT, "globalholes",
1625 SYSCTL_DESCR("Global number of TCP SACK holes"),
1626 NULL, 0, &tcp_sack_globalholes, 0,
1627 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1628
1629 sysctl_createv(clog, 0, NULL, NULL,
1630 CTLFLAG_PERMANENT,
1631 CTLTYPE_STRUCT, "stats",
1632 SYSCTL_DESCR("TCP statistics"),
1633 NULL, 0, &tcpstat, sizeof(tcpstat),
1634 CTL_NET, pf, IPPROTO_TCP, TCPCTL_STATS,
1635 CTL_EOL);
1636 #ifdef TCP_DEBUG
1637 sysctl_createv(clog, 0, NULL, NULL,
1638 CTLFLAG_PERMANENT,
1639 CTLTYPE_STRUCT, "debug",
1640 SYSCTL_DESCR("TCP sockets debug information"),
1641 NULL, 0, &tcp_debug, sizeof(tcp_debug),
1642 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBUG,
1643 CTL_EOL);
1644 sysctl_createv(clog, 0, NULL, NULL,
1645 CTLFLAG_PERMANENT,
1646 CTLTYPE_INT, "debx",
1647 SYSCTL_DESCR("Number of TCP debug sockets messages"),
1648 NULL, 0, &tcp_debx, sizeof(tcp_debx),
1649 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBX,
1650 CTL_EOL);
1651 #endif
1652
1653 }
1654
1655 /*
1656 * Sysctl for tcp variables.
1657 */
1658 #ifdef INET
1659 SYSCTL_SETUP(sysctl_net_inet_tcp_setup, "sysctl net.inet.tcp subtree setup")
1660 {
1661
1662 sysctl_net_inet_tcp_setup2(clog, PF_INET, "inet", "tcp");
1663 }
1664 #endif /* INET */
1665
1666 #ifdef INET6
1667 SYSCTL_SETUP(sysctl_net_inet6_tcp6_setup, "sysctl net.inet6.tcp6 subtree setup")
1668 {
1669
1670 sysctl_net_inet_tcp_setup2(clog, PF_INET6, "inet6", "tcp6");
1671 }
1672 #endif /* INET6 */
1673