1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 1988, 1993
5 * The Regents of the University of California.
6 * Copyright (c) 2006-2007 Robert N. M. Watson
7 * Copyright (c) 2010-2011 Juniper Networks, Inc.
8 * All rights reserved.
9 *
10 * Portions of this software were developed by Robert N. M. Watson under
11 * contract to Juniper Networks, Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #include <sys/cdefs.h>
39 #include "opt_ddb.h"
40 #include "opt_inet.h"
41 #include "opt_inet6.h"
42 #include "opt_ipsec.h"
43 #include "opt_kern_tls.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/arb.h>
48 #include <sys/limits.h>
49 #include <sys/malloc.h>
50 #include <sys/refcount.h>
51 #include <sys/kernel.h>
52 #include <sys/ktls.h>
53 #include <sys/qmath.h>
54 #include <sys/sysctl.h>
55 #include <sys/mbuf.h>
56 #ifdef INET6
57 #include <sys/domain.h>
58 #endif /* INET6 */
59 #include <sys/socket.h>
60 #include <sys/socketvar.h>
61 #include <sys/protosw.h>
62 #include <sys/proc.h>
63 #include <sys/jail.h>
64 #include <sys/stats.h>
65
66 #ifdef DDB
67 #include <ddb/ddb.h>
68 #endif
69
70 #include <net/if.h>
71 #include <net/if_var.h>
72 #include <net/route.h>
73 #include <net/vnet.h>
74
75 #include <netinet/in.h>
76 #include <netinet/in_kdtrace.h>
77 #include <netinet/in_pcb.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_var.h>
82 #ifdef INET6
83 #include <netinet/ip6.h>
84 #include <netinet6/in6_pcb.h>
85 #include <netinet6/ip6_var.h>
86 #include <netinet6/scope6_var.h>
87 #endif
88 #include <netinet/tcp.h>
89 #include <netinet/tcp_fsm.h>
90 #include <netinet/tcp_seq.h>
91 #include <netinet/tcp_timer.h>
92 #include <netinet/tcp_var.h>
93 #include <netinet/tcp_log_buf.h>
94 #include <netinet/tcpip.h>
95 #include <netinet/cc/cc.h>
96 #include <netinet/tcp_fastopen.h>
97 #include <netinet/tcp_hpts.h>
98 #ifdef TCP_OFFLOAD
99 #include <netinet/tcp_offload.h>
100 #endif
101 #include <netipsec/ipsec_support.h>
102
103 #include <vm/vm.h>
104 #include <vm/vm_param.h>
105 #include <vm/pmap.h>
106 #include <vm/vm_extern.h>
107 #include <vm/vm_map.h>
108 #include <vm/vm_page.h>
109
110 /*
111 * TCP protocol interface to socket abstraction.
112 */
113 #ifdef INET
114 static int tcp_connect(struct tcpcb *, struct sockaddr_in *,
115 struct thread *td);
116 #endif /* INET */
117 #ifdef INET6
118 static int tcp6_connect(struct tcpcb *, struct sockaddr_in6 *,
119 struct thread *td);
120 #endif /* INET6 */
121 static void tcp_disconnect(struct tcpcb *);
122 static void tcp_usrclosed(struct tcpcb *);
123 static void tcp_fill_info(const struct tcpcb *, struct tcp_info *);
124
125 static int tcp_pru_options_support(struct tcpcb *tp, int flags);
126
127 static void
tcp_bblog_pru(struct tcpcb * tp,uint32_t pru,int error)128 tcp_bblog_pru(struct tcpcb *tp, uint32_t pru, int error)
129 {
130 struct tcp_log_buffer *lgb;
131
132 KASSERT(tp != NULL, ("tcp_bblog_pru: tp == NULL"));
133 INP_WLOCK_ASSERT(tptoinpcb(tp));
134 if (tcp_bblogging_on(tp)) {
135 lgb = tcp_log_event(tp, NULL, NULL, NULL, TCP_LOG_PRU, error,
136 0, NULL, false, NULL, NULL, 0, NULL);
137 } else {
138 lgb = NULL;
139 }
140 if (lgb != NULL) {
141 if (error >= 0) {
142 lgb->tlb_errno = (uint32_t)error;
143 }
144 lgb->tlb_flex1 = pru;
145 }
146 }
147
148 /*
149 * TCP attaches to socket via pr_attach(), reserving space,
150 * and an internet control block.
151 */
152 static int
tcp_usr_attach(struct socket * so,int proto,struct thread * td)153 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
154 {
155 struct inpcb *inp;
156 struct tcpcb *tp = NULL;
157 int error;
158
159 inp = sotoinpcb(so);
160 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
161
162 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
163 if (error)
164 goto out;
165
166 so->so_rcv.sb_flags |= SB_AUTOSIZE;
167 so->so_snd.sb_flags |= SB_AUTOSIZE;
168 error = in_pcballoc(so, &V_tcbinfo);
169 if (error)
170 goto out;
171 inp = sotoinpcb(so);
172 tp = tcp_newtcpcb(inp, NULL);
173 if (tp == NULL) {
174 error = ENOBUFS;
175 in_pcbfree(inp);
176 goto out;
177 }
178 tp->t_state = TCPS_CLOSED;
179 tcp_bblog_pru(tp, PRU_ATTACH, error);
180 INP_WUNLOCK(inp);
181 TCPSTATES_INC(TCPS_CLOSED);
182 out:
183 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
184 return (error);
185 }
186
187 /*
188 * tcp_usr_detach is called when the socket layer loses its final reference
189 * to the socket, be it a file descriptor reference, a reference from TCP,
190 * etc. At this point, there is only one case in which we will keep around
191 * inpcb state: time wait.
192 */
193 static void
tcp_usr_detach(struct socket * so)194 tcp_usr_detach(struct socket *so)
195 {
196 struct inpcb *inp;
197 struct tcpcb *tp;
198
199 inp = sotoinpcb(so);
200 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
201 INP_WLOCK(inp);
202 KASSERT(so->so_pcb == inp && inp->inp_socket == so,
203 ("%s: socket %p inp %p mismatch", __func__, so, inp));
204
205 tp = intotcpcb(inp);
206
207 KASSERT(inp->inp_flags & INP_DROPPED ||
208 tp->t_state < TCPS_SYN_SENT,
209 ("%s: inp %p not dropped or embryonic", __func__, inp));
210
211 tcp_discardcb(tp);
212 in_pcbfree(inp);
213 }
214
215 #ifdef INET
216 /*
217 * Give the socket an address.
218 */
219 static int
tcp_usr_bind(struct socket * so,struct sockaddr * nam,struct thread * td)220 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
221 {
222 int error = 0;
223 struct inpcb *inp;
224 struct tcpcb *tp;
225 struct sockaddr_in *sinp;
226
227 inp = sotoinpcb(so);
228 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
229 INP_WLOCK(inp);
230 if (inp->inp_flags & INP_DROPPED) {
231 INP_WUNLOCK(inp);
232 return (EINVAL);
233 }
234 tp = intotcpcb(inp);
235
236 sinp = (struct sockaddr_in *)nam;
237 if (nam->sa_family != AF_INET) {
238 /*
239 * Preserve compatibility with old programs.
240 */
241 if (nam->sa_family != AF_UNSPEC ||
242 nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
243 sinp->sin_addr.s_addr != INADDR_ANY) {
244 error = EAFNOSUPPORT;
245 goto out;
246 }
247 nam->sa_family = AF_INET;
248 }
249 if (nam->sa_len != sizeof(*sinp)) {
250 error = EINVAL;
251 goto out;
252 }
253 /*
254 * Must check for multicast addresses and disallow binding
255 * to them.
256 */
257 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
258 error = EAFNOSUPPORT;
259 goto out;
260 }
261 INP_HASH_WLOCK(&V_tcbinfo);
262 error = in_pcbbind(inp, sinp, V_tcp_bind_all_fibs ? 0 : INPBIND_FIB,
263 td->td_ucred);
264 INP_HASH_WUNLOCK(&V_tcbinfo);
265 out:
266 tcp_bblog_pru(tp, PRU_BIND, error);
267 TCP_PROBE2(debug__user, tp, PRU_BIND);
268 INP_WUNLOCK(inp);
269
270 return (error);
271 }
272 #endif /* INET */
273
274 #ifdef INET6
275 static int
tcp6_usr_bind(struct socket * so,struct sockaddr * nam,struct thread * td)276 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
277 {
278 int error = 0;
279 struct inpcb *inp;
280 struct tcpcb *tp;
281 struct sockaddr_in6 *sin6;
282 u_char vflagsav;
283
284 inp = sotoinpcb(so);
285 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
286 INP_WLOCK(inp);
287 if (inp->inp_flags & INP_DROPPED) {
288 INP_WUNLOCK(inp);
289 return (EINVAL);
290 }
291 tp = intotcpcb(inp);
292
293 vflagsav = inp->inp_vflag;
294
295 sin6 = (struct sockaddr_in6 *)nam;
296 if (nam->sa_family != AF_INET6) {
297 error = EAFNOSUPPORT;
298 goto out;
299 }
300 if (nam->sa_len != sizeof(*sin6)) {
301 error = EINVAL;
302 goto out;
303 }
304 /*
305 * Must check for multicast addresses and disallow binding
306 * to them.
307 */
308 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
309 error = EAFNOSUPPORT;
310 goto out;
311 }
312
313 INP_HASH_WLOCK(&V_tcbinfo);
314 inp->inp_vflag &= ~INP_IPV4;
315 inp->inp_vflag |= INP_IPV6;
316 #ifdef INET
317 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
318 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
319 inp->inp_vflag |= INP_IPV4;
320 else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
321 struct sockaddr_in sin;
322
323 in6_sin6_2_sin(&sin, sin6);
324 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
325 error = EAFNOSUPPORT;
326 INP_HASH_WUNLOCK(&V_tcbinfo);
327 goto out;
328 }
329 inp->inp_vflag |= INP_IPV4;
330 inp->inp_vflag &= ~INP_IPV6;
331 error = in_pcbbind(inp, &sin, 0, td->td_ucred);
332 INP_HASH_WUNLOCK(&V_tcbinfo);
333 goto out;
334 }
335 }
336 #endif
337 error = in6_pcbbind(inp, sin6, V_tcp_bind_all_fibs ? 0 : INPBIND_FIB,
338 td->td_ucred);
339 INP_HASH_WUNLOCK(&V_tcbinfo);
340 out:
341 if (error != 0)
342 inp->inp_vflag = vflagsav;
343 tcp_bblog_pru(tp, PRU_BIND, error);
344 TCP_PROBE2(debug__user, tp, PRU_BIND);
345 INP_WUNLOCK(inp);
346 return (error);
347 }
348 #endif /* INET6 */
349
350 #ifdef INET
351 /*
352 * Prepare to accept connections.
353 */
354 static int
tcp_usr_listen(struct socket * so,int backlog,struct thread * td)355 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
356 {
357 struct inpcb *inp;
358 struct tcpcb *tp;
359 int error = 0;
360 bool already_listening;
361
362 inp = sotoinpcb(so);
363 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
364 INP_WLOCK(inp);
365 if (inp->inp_flags & INP_DROPPED) {
366 INP_WUNLOCK(inp);
367 return (EINVAL);
368 }
369 tp = intotcpcb(inp);
370
371 SOCK_LOCK(so);
372 already_listening = SOLISTENING(so);
373 error = solisten_proto_check(so);
374 if (error != 0) {
375 SOCK_UNLOCK(so);
376 goto out;
377 }
378 if (inp->inp_lport == 0) {
379 INP_HASH_WLOCK(&V_tcbinfo);
380 error = in_pcbbind(inp, NULL,
381 V_tcp_bind_all_fibs ? 0 : INPBIND_FIB, td->td_ucred);
382 INP_HASH_WUNLOCK(&V_tcbinfo);
383 }
384 if (error == 0) {
385 tcp_state_change(tp, TCPS_LISTEN);
386 solisten_proto(so, backlog);
387 #ifdef TCP_OFFLOAD
388 if ((so->so_options & SO_NO_OFFLOAD) == 0)
389 tcp_offload_listen_start(tp);
390 #endif
391 } else {
392 solisten_proto_abort(so);
393 }
394 SOCK_UNLOCK(so);
395 if (already_listening)
396 goto out;
397
398 if (error == 0)
399 in_pcblisten(inp);
400 if (tp->t_flags & TF_FASTOPEN)
401 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
402
403 out:
404 tcp_bblog_pru(tp, PRU_LISTEN, error);
405 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
406 INP_WUNLOCK(inp);
407 return (error);
408 }
409 #endif /* INET */
410
411 #ifdef INET6
412 static int
tcp6_usr_listen(struct socket * so,int backlog,struct thread * td)413 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
414 {
415 struct inpcb *inp;
416 struct tcpcb *tp;
417 u_char vflagsav;
418 int error = 0;
419 bool already_listening;
420
421 inp = sotoinpcb(so);
422 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
423 INP_WLOCK(inp);
424 if (inp->inp_flags & INP_DROPPED) {
425 INP_WUNLOCK(inp);
426 return (EINVAL);
427 }
428 tp = intotcpcb(inp);
429
430 vflagsav = inp->inp_vflag;
431
432 SOCK_LOCK(so);
433 already_listening = SOLISTENING(so);
434 error = solisten_proto_check(so);
435 if (error != 0) {
436 SOCK_UNLOCK(so);
437 goto out;
438 }
439 INP_HASH_WLOCK(&V_tcbinfo);
440 if (inp->inp_lport == 0) {
441 inp->inp_vflag &= ~INP_IPV4;
442 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
443 inp->inp_vflag |= INP_IPV4;
444 error = in6_pcbbind(inp, NULL,
445 V_tcp_bind_all_fibs ? 0 : INPBIND_FIB, td->td_ucred);
446 }
447 INP_HASH_WUNLOCK(&V_tcbinfo);
448 if (error == 0) {
449 tcp_state_change(tp, TCPS_LISTEN);
450 solisten_proto(so, backlog);
451 #ifdef TCP_OFFLOAD
452 if ((so->so_options & SO_NO_OFFLOAD) == 0)
453 tcp_offload_listen_start(tp);
454 #endif
455 } else {
456 solisten_proto_abort(so);
457 }
458 SOCK_UNLOCK(so);
459 if (already_listening)
460 goto out;
461
462 if (error == 0)
463 in_pcblisten(inp);
464 if (tp->t_flags & TF_FASTOPEN)
465 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
466
467 if (error != 0)
468 inp->inp_vflag = vflagsav;
469
470 out:
471 tcp_bblog_pru(tp, PRU_LISTEN, error);
472 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
473 INP_WUNLOCK(inp);
474 return (error);
475 }
476 #endif /* INET6 */
477
478 #ifdef INET
479 /*
480 * Initiate connection to peer.
481 * Create a template for use in transmissions on this connection.
482 * Enter SYN_SENT state, and mark socket as connecting.
483 * Start keep-alive timer, and seed output sequence space.
484 * Send initial segment on connection.
485 */
486 static int
tcp_usr_connect(struct socket * so,struct sockaddr * nam,struct thread * td)487 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
488 {
489 struct epoch_tracker et;
490 int error = 0;
491 struct inpcb *inp;
492 struct tcpcb *tp;
493 struct sockaddr_in *sinp;
494
495 inp = sotoinpcb(so);
496 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
497 INP_WLOCK(inp);
498 if (inp->inp_flags & INP_DROPPED) {
499 INP_WUNLOCK(inp);
500 return (ECONNREFUSED);
501 }
502 tp = intotcpcb(inp);
503
504 sinp = (struct sockaddr_in *)nam;
505 if (nam->sa_family != AF_INET) {
506 error = EAFNOSUPPORT;
507 goto out;
508 }
509 if (nam->sa_len != sizeof (*sinp)) {
510 error = EINVAL;
511 goto out;
512 }
513 /*
514 * Must disallow TCP ``connections'' to multicast addresses.
515 */
516 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
517 error = EAFNOSUPPORT;
518 goto out;
519 }
520 if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
521 error = EACCES;
522 goto out;
523 }
524 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
525 goto out;
526 if (SOLISTENING(so)) {
527 error = EOPNOTSUPP;
528 goto out;
529 }
530 NET_EPOCH_ENTER(et);
531 if ((error = tcp_connect(tp, sinp, td)) != 0)
532 goto out_in_epoch;
533 #ifdef TCP_OFFLOAD
534 if (registered_toedevs > 0 &&
535 (so->so_options & SO_NO_OFFLOAD) == 0 &&
536 (error = tcp_offload_connect(so, nam)) == 0)
537 goto out_in_epoch;
538 #endif
539 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
540 error = tcp_output(tp);
541 KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
542 ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
543 out_in_epoch:
544 NET_EPOCH_EXIT(et);
545 out:
546 tcp_bblog_pru(tp, PRU_CONNECT, error);
547 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
548 INP_WUNLOCK(inp);
549 return (error);
550 }
551 #endif /* INET */
552
553 #ifdef INET6
554 static int
tcp6_usr_connect(struct socket * so,struct sockaddr * nam,struct thread * td)555 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
556 {
557 struct epoch_tracker et;
558 int error = 0;
559 struct inpcb *inp;
560 struct tcpcb *tp;
561 struct sockaddr_in6 *sin6;
562 u_int8_t incflagsav;
563 u_char vflagsav;
564
565 inp = sotoinpcb(so);
566 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
567 INP_WLOCK(inp);
568 if (inp->inp_flags & INP_DROPPED) {
569 INP_WUNLOCK(inp);
570 return (ECONNREFUSED);
571 }
572 tp = intotcpcb(inp);
573
574 vflagsav = inp->inp_vflag;
575 incflagsav = inp->inp_inc.inc_flags;
576
577 sin6 = (struct sockaddr_in6 *)nam;
578 if (nam->sa_family != AF_INET6) {
579 error = EAFNOSUPPORT;
580 goto out;
581 }
582 if (nam->sa_len != sizeof (*sin6)) {
583 error = EINVAL;
584 goto out;
585 }
586 /*
587 * Must disallow TCP ``connections'' to multicast addresses.
588 */
589 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
590 error = EAFNOSUPPORT;
591 goto out;
592 }
593 if (SOLISTENING(so)) {
594 error = EOPNOTSUPP;
595 goto out;
596 }
597 #ifdef INET
598 /*
599 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
600 * therefore probably require the hash lock, which isn't held here.
601 * Is this a significant problem?
602 */
603 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
604 struct sockaddr_in sin;
605
606 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
607 error = EINVAL;
608 goto out;
609 }
610 if ((inp->inp_vflag & INP_IPV4) == 0) {
611 error = EAFNOSUPPORT;
612 goto out;
613 }
614
615 in6_sin6_2_sin(&sin, sin6);
616 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
617 error = EAFNOSUPPORT;
618 goto out;
619 }
620 if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
621 error = EACCES;
622 goto out;
623 }
624 if ((error = prison_remote_ip4(td->td_ucred,
625 &sin.sin_addr)) != 0)
626 goto out;
627 inp->inp_vflag |= INP_IPV4;
628 inp->inp_vflag &= ~INP_IPV6;
629 NET_EPOCH_ENTER(et);
630 if ((error = tcp_connect(tp, &sin, td)) != 0)
631 goto out_in_epoch;
632 #ifdef TCP_OFFLOAD
633 if (registered_toedevs > 0 &&
634 (so->so_options & SO_NO_OFFLOAD) == 0 &&
635 (error = tcp_offload_connect(so, nam)) == 0)
636 goto out_in_epoch;
637 #endif
638 error = tcp_output(tp);
639 goto out_in_epoch;
640 } else {
641 if ((inp->inp_vflag & INP_IPV6) == 0) {
642 error = EAFNOSUPPORT;
643 goto out;
644 }
645 }
646 #endif
647 if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
648 goto out;
649 inp->inp_vflag &= ~INP_IPV4;
650 inp->inp_vflag |= INP_IPV6;
651 inp->inp_inc.inc_flags |= INC_ISIPV6;
652 NET_EPOCH_ENTER(et);
653 if ((error = tcp6_connect(tp, sin6, td)) != 0)
654 goto out_in_epoch;
655 #ifdef TCP_OFFLOAD
656 if (registered_toedevs > 0 &&
657 (so->so_options & SO_NO_OFFLOAD) == 0 &&
658 (error = tcp_offload_connect(so, nam)) == 0)
659 goto out_in_epoch;
660 #endif
661 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
662 error = tcp_output(tp);
663 out_in_epoch:
664 NET_EPOCH_EXIT(et);
665 out:
666 KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
667 ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
668 /*
669 * If the implicit bind in the connect call fails, restore
670 * the flags we modified.
671 */
672 if (error != 0 && inp->inp_lport == 0) {
673 inp->inp_vflag = vflagsav;
674 inp->inp_inc.inc_flags = incflagsav;
675 }
676
677 tcp_bblog_pru(tp, PRU_CONNECT, error);
678 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
679 INP_WUNLOCK(inp);
680 return (error);
681 }
682 #endif /* INET6 */
683
684 /*
685 * Initiate disconnect from peer.
686 * If connection never passed embryonic stage, just drop;
687 * else if don't need to let data drain, then can just drop anyways,
688 * else have to begin TCP shutdown process: mark socket disconnecting,
689 * drain unread data, state switch to reflect user close, and
690 * send segment (e.g. FIN) to peer. Socket will be really disconnected
691 * when peer sends FIN and acks ours.
692 *
693 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
694 */
695 static int
tcp_usr_disconnect(struct socket * so)696 tcp_usr_disconnect(struct socket *so)
697 {
698 struct inpcb *inp;
699 struct tcpcb *tp = NULL;
700 struct epoch_tracker et;
701
702 NET_EPOCH_ENTER(et);
703 inp = sotoinpcb(so);
704 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
705 INP_WLOCK(inp);
706 tp = intotcpcb(inp);
707
708 if (tp->t_state == TCPS_TIME_WAIT)
709 goto out;
710 tcp_disconnect(tp);
711 out:
712 tcp_bblog_pru(tp, PRU_DISCONNECT, 0);
713 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
714 INP_WUNLOCK(inp);
715 NET_EPOCH_EXIT(et);
716 return (0);
717 }
718
719 #ifdef INET
720 /*
721 * Accept a connection. Essentially all the work is done at higher levels;
722 * just return the address of the peer, storing through addr.
723 */
724 static int
tcp_usr_accept(struct socket * so,struct sockaddr * sa)725 tcp_usr_accept(struct socket *so, struct sockaddr *sa)
726 {
727 struct inpcb *inp;
728 struct tcpcb *tp;
729 int error = 0;
730
731 inp = sotoinpcb(so);
732 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
733 INP_WLOCK(inp);
734 if (inp->inp_flags & INP_DROPPED) {
735 INP_WUNLOCK(inp);
736 return (ECONNABORTED);
737 }
738 tp = intotcpcb(inp);
739
740 if (so->so_state & SS_ISDISCONNECTED)
741 error = ECONNABORTED;
742 else
743 *(struct sockaddr_in *)sa = (struct sockaddr_in ){
744 .sin_family = AF_INET,
745 .sin_len = sizeof(struct sockaddr_in),
746 .sin_port = inp->inp_fport,
747 .sin_addr = inp->inp_faddr,
748 };
749 tcp_bblog_pru(tp, PRU_ACCEPT, error);
750 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
751 INP_WUNLOCK(inp);
752
753 return (error);
754 }
755 #endif /* INET */
756
757 #ifdef INET6
758 static int
tcp6_usr_accept(struct socket * so,struct sockaddr * sa)759 tcp6_usr_accept(struct socket *so, struct sockaddr *sa)
760 {
761 struct inpcb *inp;
762 struct tcpcb *tp;
763 int error = 0;
764
765 inp = sotoinpcb(so);
766 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
767 INP_WLOCK(inp);
768 if (inp->inp_flags & INP_DROPPED) {
769 INP_WUNLOCK(inp);
770 return (ECONNABORTED);
771 }
772 tp = intotcpcb(inp);
773
774 if (so->so_state & SS_ISDISCONNECTED) {
775 error = ECONNABORTED;
776 } else {
777 if (inp->inp_vflag & INP_IPV4) {
778 struct sockaddr_in sin = {
779 .sin_family = AF_INET,
780 .sin_len = sizeof(struct sockaddr_in),
781 .sin_port = inp->inp_fport,
782 .sin_addr = inp->inp_faddr,
783 };
784 in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa);
785 } else {
786 *(struct sockaddr_in6 *)sa = (struct sockaddr_in6 ){
787 .sin6_family = AF_INET6,
788 .sin6_len = sizeof(struct sockaddr_in6),
789 .sin6_port = inp->inp_fport,
790 .sin6_addr = inp->in6p_faddr,
791 };
792 /* XXX: should catch errors */
793 (void)sa6_recoverscope((struct sockaddr_in6 *)sa);
794 }
795 }
796
797 tcp_bblog_pru(tp, PRU_ACCEPT, error);
798 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
799 INP_WUNLOCK(inp);
800
801 return (error);
802 }
803 #endif /* INET6 */
804
805 /*
806 * Mark the connection as being incapable of further output.
807 */
808 static int
tcp_usr_shutdown(struct socket * so,enum shutdown_how how)809 tcp_usr_shutdown(struct socket *so, enum shutdown_how how)
810 {
811 struct epoch_tracker et;
812 struct inpcb *inp = sotoinpcb(so);
813 struct tcpcb *tp = intotcpcb(inp);
814 int error = 0;
815
816 SOCK_LOCK(so);
817 if (SOLISTENING(so)) {
818 if (how != SHUT_WR) {
819 so->so_error = ECONNABORTED;
820 solisten_wakeup(so); /* unlocks so */
821 } else
822 SOCK_UNLOCK(so);
823 return (ENOTCONN);
824 } else if ((so->so_state &
825 (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
826 SOCK_UNLOCK(so);
827 return (ENOTCONN);
828 }
829 SOCK_UNLOCK(so);
830
831 switch (how) {
832 case SHUT_RD:
833 sorflush(so);
834 break;
835 case SHUT_RDWR:
836 sorflush(so);
837 /* FALLTHROUGH */
838 case SHUT_WR:
839 /*
840 * XXXGL: mimicing old soshutdown() here. But shouldn't we
841 * return ECONNRESEST for SHUT_RD as well?
842 */
843 INP_WLOCK(inp);
844 if (inp->inp_flags & INP_DROPPED) {
845 INP_WUNLOCK(inp);
846 return (ECONNRESET);
847 }
848
849 socantsendmore(so);
850 NET_EPOCH_ENTER(et);
851 tcp_usrclosed(tp);
852 error = tcp_output_nodrop(tp);
853 tcp_bblog_pru(tp, PRU_SHUTDOWN, error);
854 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
855 error = tcp_unlock_or_drop(tp, error);
856 NET_EPOCH_EXIT(et);
857 }
858 wakeup(&so->so_timeo);
859
860 return (error);
861 }
862
863 /*
864 * After a receive, possibly send window update to peer.
865 */
866 static int
tcp_usr_rcvd(struct socket * so,int flags)867 tcp_usr_rcvd(struct socket *so, int flags)
868 {
869 struct epoch_tracker et;
870 struct inpcb *inp;
871 struct tcpcb *tp;
872 int outrv = 0, error = 0;
873
874 inp = sotoinpcb(so);
875 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
876 INP_WLOCK(inp);
877 if (inp->inp_flags & INP_DROPPED) {
878 INP_WUNLOCK(inp);
879 return (ECONNRESET);
880 }
881 tp = intotcpcb(inp);
882
883 NET_EPOCH_ENTER(et);
884 /*
885 * For passively-created TFO connections, don't attempt a window
886 * update while still in SYN_RECEIVED as this may trigger an early
887 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
888 * application response data, or failing that, when the DELACK timer
889 * expires.
890 */
891 if ((tp->t_flags & TF_FASTOPEN) && (tp->t_state == TCPS_SYN_RECEIVED))
892 goto out;
893 #ifdef TCP_OFFLOAD
894 if (tp->t_flags & TF_TOE)
895 tcp_offload_rcvd(tp);
896 else
897 #endif
898 outrv = tcp_output_nodrop(tp);
899 out:
900 tcp_bblog_pru(tp, PRU_RCVD, error);
901 TCP_PROBE2(debug__user, tp, PRU_RCVD);
902 (void) tcp_unlock_or_drop(tp, outrv);
903 NET_EPOCH_EXIT(et);
904 return (error);
905 }
906
907 /*
908 * Do a send by putting data in output queue and updating urgent
909 * marker if URG set. Possibly send more data. Unlike the other
910 * pr_*() routines, the mbuf chains are our responsibility. We
911 * must either enqueue them or free them. The other pr_*() routines
912 * generally are caller-frees.
913 */
914 static int
tcp_usr_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct thread * td)915 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
916 struct sockaddr *nam, struct mbuf *control, struct thread *td)
917 {
918 struct epoch_tracker et;
919 int error = 0;
920 struct inpcb *inp;
921 struct tcpcb *tp;
922 #ifdef INET
923 #ifdef INET6
924 struct sockaddr_in sin;
925 #endif
926 struct sockaddr_in *sinp;
927 #endif
928 #ifdef INET6
929 struct sockaddr_in6 *sin6;
930 int isipv6;
931 #endif
932 u_int8_t incflagsav;
933 u_char vflagsav;
934 bool restoreflags;
935
936 inp = sotoinpcb(so);
937 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
938 INP_WLOCK(inp);
939 if (inp->inp_flags & INP_DROPPED) {
940 if (m != NULL && (flags & PRUS_NOTREADY) == 0)
941 m_freem(m);
942 INP_WUNLOCK(inp);
943 return (ECONNRESET);
944 }
945 tp = intotcpcb(inp);
946
947 vflagsav = inp->inp_vflag;
948 incflagsav = inp->inp_inc.inc_flags;
949 restoreflags = false;
950
951 NET_EPOCH_ENTER(et);
952 if (control != NULL) {
953 /* TCP doesn't do control messages (rights, creds, etc) */
954 if (control->m_len > 0) {
955 m_freem(control);
956 error = EINVAL;
957 goto out;
958 }
959 m_freem(control); /* empty control, just free it */
960 }
961
962 if ((flags & PRUS_OOB) != 0 &&
963 (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
964 goto out;
965
966 if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
967 if (tp->t_state == TCPS_LISTEN) {
968 error = EINVAL;
969 goto out;
970 }
971 switch (nam->sa_family) {
972 #ifdef INET
973 case AF_INET:
974 sinp = (struct sockaddr_in *)nam;
975 if (sinp->sin_len != sizeof(struct sockaddr_in)) {
976 error = EINVAL;
977 goto out;
978 }
979 if ((inp->inp_vflag & INP_IPV6) != 0) {
980 error = EAFNOSUPPORT;
981 goto out;
982 }
983 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
984 error = EAFNOSUPPORT;
985 goto out;
986 }
987 if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
988 error = EACCES;
989 goto out;
990 }
991 if ((error = prison_remote_ip4(td->td_ucred,
992 &sinp->sin_addr)))
993 goto out;
994 #ifdef INET6
995 isipv6 = 0;
996 #endif
997 break;
998 #endif /* INET */
999 #ifdef INET6
1000 case AF_INET6:
1001 sin6 = (struct sockaddr_in6 *)nam;
1002 if (sin6->sin6_len != sizeof(*sin6)) {
1003 error = EINVAL;
1004 goto out;
1005 }
1006 if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
1007 error = EAFNOSUPPORT;
1008 goto out;
1009 }
1010 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1011 error = EAFNOSUPPORT;
1012 goto out;
1013 }
1014 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1015 #ifdef INET
1016 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1017 error = EINVAL;
1018 goto out;
1019 }
1020 if ((inp->inp_vflag & INP_IPV4) == 0) {
1021 error = EAFNOSUPPORT;
1022 goto out;
1023 }
1024 restoreflags = true;
1025 inp->inp_vflag &= ~INP_IPV6;
1026 sinp = &sin;
1027 in6_sin6_2_sin(sinp, sin6);
1028 if (IN_MULTICAST(
1029 ntohl(sinp->sin_addr.s_addr))) {
1030 error = EAFNOSUPPORT;
1031 goto out;
1032 }
1033 if ((error = prison_remote_ip4(td->td_ucred,
1034 &sinp->sin_addr)))
1035 goto out;
1036 isipv6 = 0;
1037 #else /* !INET */
1038 error = EAFNOSUPPORT;
1039 goto out;
1040 #endif /* INET */
1041 } else {
1042 if ((inp->inp_vflag & INP_IPV6) == 0) {
1043 error = EAFNOSUPPORT;
1044 goto out;
1045 }
1046 restoreflags = true;
1047 inp->inp_vflag &= ~INP_IPV4;
1048 inp->inp_inc.inc_flags |= INC_ISIPV6;
1049 if ((error = prison_remote_ip6(td->td_ucred,
1050 &sin6->sin6_addr)))
1051 goto out;
1052 isipv6 = 1;
1053 }
1054 break;
1055 #endif /* INET6 */
1056 default:
1057 error = EAFNOSUPPORT;
1058 goto out;
1059 }
1060 }
1061 if (!(flags & PRUS_OOB)) {
1062 if (tp->t_acktime == 0)
1063 tp->t_acktime = ticks;
1064 sbappendstream(&so->so_snd, m, flags);
1065 m = NULL;
1066 if (nam && tp->t_state < TCPS_SYN_SENT) {
1067 KASSERT(tp->t_state == TCPS_CLOSED,
1068 ("%s: tp %p is listening", __func__, tp));
1069
1070 /*
1071 * Do implied connect if not yet connected,
1072 * initialize window to default value, and
1073 * initialize maxseg using peer's cached MSS.
1074 */
1075 #ifdef INET6
1076 if (isipv6)
1077 error = tcp6_connect(tp, sin6, td);
1078 #endif /* INET6 */
1079 #if defined(INET6) && defined(INET)
1080 else
1081 #endif
1082 #ifdef INET
1083 error = tcp_connect(tp, sinp, td);
1084 #endif
1085 /*
1086 * The bind operation in tcp_connect succeeded. We
1087 * no longer want to restore the flags if later
1088 * operations fail.
1089 */
1090 if (error == 0 || inp->inp_lport != 0)
1091 restoreflags = false;
1092
1093 if (error) {
1094 /* m is freed if PRUS_NOTREADY is unset. */
1095 sbflush(&so->so_snd);
1096 goto out;
1097 }
1098 if (tp->t_flags & TF_FASTOPEN)
1099 tcp_fastopen_connect(tp);
1100 else {
1101 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1102 tcp_mss(tp, -1);
1103 }
1104 }
1105 if (flags & PRUS_EOF) {
1106 /*
1107 * Close the send side of the connection after
1108 * the data is sent.
1109 */
1110 socantsendmore(so);
1111 tcp_usrclosed(tp);
1112 }
1113 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1114 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1115 (tp->t_fbyte_out == 0) &&
1116 (so->so_snd.sb_ccc > 0)) {
1117 tp->t_fbyte_out = ticks;
1118 if (tp->t_fbyte_out == 0)
1119 tp->t_fbyte_out = 1;
1120 if (tp->t_fbyte_out && tp->t_fbyte_in)
1121 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1122 }
1123 if (!(inp->inp_flags & INP_DROPPED) &&
1124 !(flags & PRUS_NOTREADY)) {
1125 if (flags & PRUS_MORETOCOME)
1126 tp->t_flags |= TF_MORETOCOME;
1127 error = tcp_output_nodrop(tp);
1128 if (flags & PRUS_MORETOCOME)
1129 tp->t_flags &= ~TF_MORETOCOME;
1130 }
1131 } else {
1132 /*
1133 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1134 */
1135 SOCK_SENDBUF_LOCK(so);
1136 if (sbspace(&so->so_snd) < -512) {
1137 SOCK_SENDBUF_UNLOCK(so);
1138 error = ENOBUFS;
1139 goto out;
1140 }
1141 /*
1142 * According to RFC961 (Assigned Protocols),
1143 * the urgent pointer points to the last octet
1144 * of urgent data. We continue, however,
1145 * to consider it to indicate the first octet
1146 * of data past the urgent section.
1147 * Otherwise, snd_up should be one lower.
1148 */
1149 if (tp->t_acktime == 0)
1150 tp->t_acktime = ticks;
1151 sbappendstream_locked(&so->so_snd, m, flags);
1152 SOCK_SENDBUF_UNLOCK(so);
1153 m = NULL;
1154 if (nam && tp->t_state < TCPS_SYN_SENT) {
1155 /*
1156 * Do implied connect if not yet connected,
1157 * initialize window to default value, and
1158 * initialize maxseg using peer's cached MSS.
1159 */
1160
1161 /*
1162 * Not going to contemplate SYN|URG
1163 */
1164 if (tp->t_flags & TF_FASTOPEN)
1165 tp->t_flags &= ~TF_FASTOPEN;
1166 #ifdef INET6
1167 if (isipv6)
1168 error = tcp6_connect(tp, sin6, td);
1169 #endif /* INET6 */
1170 #if defined(INET6) && defined(INET)
1171 else
1172 #endif
1173 #ifdef INET
1174 error = tcp_connect(tp, sinp, td);
1175 #endif
1176 /*
1177 * The bind operation in tcp_connect succeeded. We
1178 * no longer want to restore the flags if later
1179 * operations fail.
1180 */
1181 if (error == 0 || inp->inp_lport != 0)
1182 restoreflags = false;
1183
1184 if (error != 0) {
1185 /* m is freed if PRUS_NOTREADY is unset. */
1186 sbflush(&so->so_snd);
1187 goto out;
1188 }
1189 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1190 tcp_mss(tp, -1);
1191 }
1192 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1193 if ((flags & PRUS_NOTREADY) == 0) {
1194 tp->t_flags |= TF_FORCEDATA;
1195 error = tcp_output_nodrop(tp);
1196 tp->t_flags &= ~TF_FORCEDATA;
1197 }
1198 }
1199 TCP_LOG_EVENT(tp, NULL,
1200 &inp->inp_socket->so_rcv,
1201 &inp->inp_socket->so_snd,
1202 TCP_LOG_USERSEND, error,
1203 0, NULL, false);
1204
1205 out:
1206 /*
1207 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
1208 * responsible for freeing memory.
1209 */
1210 if (m != NULL && (flags & PRUS_NOTREADY) == 0)
1211 m_freem(m);
1212
1213 /*
1214 * If the request was unsuccessful and we changed flags,
1215 * restore the original flags.
1216 */
1217 if (error != 0 && restoreflags) {
1218 inp->inp_vflag = vflagsav;
1219 inp->inp_inc.inc_flags = incflagsav;
1220 }
1221 tcp_bblog_pru(tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1222 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND), error);
1223 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1224 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1225 error = tcp_unlock_or_drop(tp, error);
1226 NET_EPOCH_EXIT(et);
1227 return (error);
1228 }
1229
1230 static int
tcp_usr_ready(struct socket * so,struct mbuf * m,int count)1231 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1232 {
1233 struct epoch_tracker et;
1234 struct inpcb *inp;
1235 struct tcpcb *tp;
1236 int error;
1237
1238 inp = sotoinpcb(so);
1239 INP_WLOCK(inp);
1240 if (inp->inp_flags & INP_DROPPED) {
1241 INP_WUNLOCK(inp);
1242 mb_free_notready(m, count);
1243 return (ECONNRESET);
1244 }
1245 tp = intotcpcb(inp);
1246
1247 SOCK_SENDBUF_LOCK(so);
1248 error = sbready(&so->so_snd, m, count);
1249 SOCK_SENDBUF_UNLOCK(so);
1250 if (error) {
1251 INP_WUNLOCK(inp);
1252 return (error);
1253 }
1254 NET_EPOCH_ENTER(et);
1255 error = tcp_output_unlock(tp);
1256 NET_EPOCH_EXIT(et);
1257
1258 return (error);
1259 }
1260
1261 /*
1262 * Abort the TCP. Drop the connection abruptly.
1263 */
1264 static void
tcp_usr_abort(struct socket * so)1265 tcp_usr_abort(struct socket *so)
1266 {
1267 struct inpcb *inp;
1268 struct tcpcb *tp;
1269 struct epoch_tracker et;
1270
1271 inp = sotoinpcb(so);
1272 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1273
1274 NET_EPOCH_ENTER(et);
1275 INP_WLOCK(inp);
1276 KASSERT(inp->inp_socket != NULL,
1277 ("tcp_usr_abort: inp_socket == NULL"));
1278
1279 /*
1280 * If we still have full TCP state, and we're not dropped, drop.
1281 */
1282 if (!(inp->inp_flags & INP_DROPPED)) {
1283 tp = intotcpcb(inp);
1284 tp = tcp_drop(tp, ECONNABORTED);
1285 if (tp == NULL)
1286 goto dropped;
1287 tcp_bblog_pru(tp, PRU_ABORT, 0);
1288 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1289 }
1290 if (!(inp->inp_flags & INP_DROPPED)) {
1291 soref(so);
1292 inp->inp_flags |= INP_SOCKREF;
1293 }
1294 INP_WUNLOCK(inp);
1295 dropped:
1296 NET_EPOCH_EXIT(et);
1297 }
1298
1299 /*
1300 * TCP socket is closed. Start friendly disconnect.
1301 */
1302 static void
tcp_usr_close(struct socket * so)1303 tcp_usr_close(struct socket *so)
1304 {
1305 struct inpcb *inp;
1306 struct tcpcb *tp;
1307 struct epoch_tracker et;
1308
1309 inp = sotoinpcb(so);
1310 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1311
1312 NET_EPOCH_ENTER(et);
1313 INP_WLOCK(inp);
1314 KASSERT(inp->inp_socket != NULL,
1315 ("tcp_usr_close: inp_socket == NULL"));
1316
1317 /*
1318 * If we are still connected and we're not dropped, initiate
1319 * a disconnect.
1320 */
1321 if (!(inp->inp_flags & INP_DROPPED)) {
1322 tp = intotcpcb(inp);
1323 if (tp->t_state != TCPS_TIME_WAIT) {
1324 tp->t_flags |= TF_CLOSED;
1325 tcp_disconnect(tp);
1326 tcp_bblog_pru(tp, PRU_CLOSE, 0);
1327 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1328 }
1329 }
1330 if (!(inp->inp_flags & INP_DROPPED)) {
1331 soref(so);
1332 inp->inp_flags |= INP_SOCKREF;
1333 }
1334 INP_WUNLOCK(inp);
1335 NET_EPOCH_EXIT(et);
1336 }
1337
1338 static int
tcp_pru_options_support(struct tcpcb * tp,int flags)1339 tcp_pru_options_support(struct tcpcb *tp, int flags)
1340 {
1341 /*
1342 * If the specific TCP stack has a pru_options
1343 * specified then it does not always support
1344 * all the PRU_XX options and we must ask it.
1345 * If the function is not specified then all
1346 * of the PRU_XX options are supported.
1347 */
1348 int ret = 0;
1349
1350 if (tp->t_fb->tfb_pru_options) {
1351 ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1352 }
1353 return (ret);
1354 }
1355
1356 /*
1357 * Receive out-of-band data.
1358 */
1359 static int
tcp_usr_rcvoob(struct socket * so,struct mbuf * m,int flags)1360 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1361 {
1362 int error = 0;
1363 struct inpcb *inp;
1364 struct tcpcb *tp;
1365
1366 inp = sotoinpcb(so);
1367 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1368 INP_WLOCK(inp);
1369 if (inp->inp_flags & INP_DROPPED) {
1370 INP_WUNLOCK(inp);
1371 return (ECONNRESET);
1372 }
1373 tp = intotcpcb(inp);
1374
1375 error = tcp_pru_options_support(tp, PRUS_OOB);
1376 if (error) {
1377 goto out;
1378 }
1379 if ((so->so_oobmark == 0 &&
1380 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1381 so->so_options & SO_OOBINLINE ||
1382 tp->t_oobflags & TCPOOB_HADDATA) {
1383 error = EINVAL;
1384 goto out;
1385 }
1386 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1387 error = EWOULDBLOCK;
1388 goto out;
1389 }
1390 m->m_len = 1;
1391 *mtod(m, caddr_t) = tp->t_iobc;
1392 if ((flags & MSG_PEEK) == 0)
1393 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1394
1395 out:
1396 tcp_bblog_pru(tp, PRU_RCVOOB, error);
1397 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1398 INP_WUNLOCK(inp);
1399 return (error);
1400 }
1401
1402 #ifdef INET
1403 struct protosw tcp_protosw = {
1404 .pr_type = SOCK_STREAM,
1405 .pr_protocol = IPPROTO_TCP,
1406 .pr_flags = PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD |
1407 PR_CAPATTACH,
1408 .pr_ctloutput = tcp_ctloutput,
1409 .pr_abort = tcp_usr_abort,
1410 .pr_accept = tcp_usr_accept,
1411 .pr_attach = tcp_usr_attach,
1412 .pr_bind = tcp_usr_bind,
1413 .pr_connect = tcp_usr_connect,
1414 .pr_control = in_control,
1415 .pr_detach = tcp_usr_detach,
1416 .pr_disconnect = tcp_usr_disconnect,
1417 .pr_listen = tcp_usr_listen,
1418 .pr_peeraddr = in_getpeeraddr,
1419 .pr_rcvd = tcp_usr_rcvd,
1420 .pr_rcvoob = tcp_usr_rcvoob,
1421 .pr_send = tcp_usr_send,
1422 .pr_sendfile_wait = sendfile_wait_generic,
1423 .pr_ready = tcp_usr_ready,
1424 .pr_shutdown = tcp_usr_shutdown,
1425 .pr_sockaddr = in_getsockaddr,
1426 .pr_sosetlabel = in_pcbsosetlabel,
1427 .pr_close = tcp_usr_close,
1428 };
1429 #endif /* INET */
1430
1431 #ifdef INET6
1432 struct protosw tcp6_protosw = {
1433 .pr_type = SOCK_STREAM,
1434 .pr_protocol = IPPROTO_TCP,
1435 .pr_flags = PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD |
1436 PR_CAPATTACH,
1437 .pr_ctloutput = tcp_ctloutput,
1438 .pr_abort = tcp_usr_abort,
1439 .pr_accept = tcp6_usr_accept,
1440 .pr_attach = tcp_usr_attach,
1441 .pr_bind = tcp6_usr_bind,
1442 .pr_connect = tcp6_usr_connect,
1443 .pr_control = in6_control,
1444 .pr_detach = tcp_usr_detach,
1445 .pr_disconnect = tcp_usr_disconnect,
1446 .pr_listen = tcp6_usr_listen,
1447 .pr_peeraddr = in6_mapped_peeraddr,
1448 .pr_rcvd = tcp_usr_rcvd,
1449 .pr_rcvoob = tcp_usr_rcvoob,
1450 .pr_send = tcp_usr_send,
1451 .pr_sendfile_wait = sendfile_wait_generic,
1452 .pr_ready = tcp_usr_ready,
1453 .pr_shutdown = tcp_usr_shutdown,
1454 .pr_sockaddr = in6_mapped_sockaddr,
1455 .pr_sosetlabel = in_pcbsosetlabel,
1456 .pr_close = tcp_usr_close,
1457 };
1458 #endif /* INET6 */
1459
1460 #ifdef INET
1461 /*
1462 * Common subroutine to open a TCP connection to remote host specified
1463 * by struct sockaddr_in. Call in_pcbconnect() to choose local host address
1464 * and assign a local port number and install the inpcb into the hash.
1465 * Initialize connection parameters and enter SYN-SENT state.
1466 */
1467 static int
tcp_connect(struct tcpcb * tp,struct sockaddr_in * sin,struct thread * td)1468 tcp_connect(struct tcpcb *tp, struct sockaddr_in *sin, struct thread *td)
1469 {
1470 struct inpcb *inp = tptoinpcb(tp);
1471 struct socket *so = tptosocket(tp);
1472 int error;
1473
1474 NET_EPOCH_ASSERT();
1475 INP_WLOCK_ASSERT(inp);
1476
1477 if (__predict_false((so->so_state &
1478 (SS_ISCONNECTING | SS_ISCONNECTED | SS_ISDISCONNECTING |
1479 SS_ISDISCONNECTED)) != 0))
1480 return (EISCONN);
1481 if (__predict_false((so->so_options & SO_REUSEPORT_LB) != 0))
1482 return (EOPNOTSUPP);
1483
1484 INP_HASH_WLOCK(&V_tcbinfo);
1485 error = in_pcbconnect(inp, sin, td->td_ucred);
1486 INP_HASH_WUNLOCK(&V_tcbinfo);
1487 if (error != 0)
1488 return (error);
1489
1490 /*
1491 * Compute window scaling to request:
1492 * Scale to fit into sweet spot. See tcp_syncache.c.
1493 * XXX: This should move to tcp_output().
1494 */
1495 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1496 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1497 tp->request_r_scale++;
1498
1499 soisconnecting(so);
1500 TCPSTAT_INC(tcps_connattempt);
1501 tcp_state_change(tp, TCPS_SYN_SENT);
1502 tp->iss = tcp_new_isn(&inp->inp_inc);
1503 if (tp->t_flags & TF_REQ_TSTMP)
1504 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1505 tcp_sendseqinit(tp);
1506
1507 return (0);
1508 }
1509 #endif /* INET */
1510
1511 #ifdef INET6
1512 static int
tcp6_connect(struct tcpcb * tp,struct sockaddr_in6 * sin6,struct thread * td)1513 tcp6_connect(struct tcpcb *tp, struct sockaddr_in6 *sin6, struct thread *td)
1514 {
1515 struct inpcb *inp = tptoinpcb(tp);
1516 struct socket *so = tptosocket(tp);
1517 int error;
1518
1519 NET_EPOCH_ASSERT();
1520 INP_WLOCK_ASSERT(inp);
1521
1522 if (__predict_false((so->so_state &
1523 (SS_ISCONNECTING | SS_ISCONNECTED | SS_ISDISCONNECTING |
1524 SS_ISDISCONNECTED)) != 0))
1525 return (EISCONN);
1526 if (__predict_false((so->so_options & SO_REUSEPORT_LB) != 0))
1527 return (EOPNOTSUPP);
1528
1529 INP_HASH_WLOCK(&V_tcbinfo);
1530 error = in6_pcbconnect(inp, sin6, td->td_ucred, true);
1531 INP_HASH_WUNLOCK(&V_tcbinfo);
1532 if (error != 0)
1533 return (error);
1534
1535 /* Compute window scaling to request. */
1536 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1537 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1538 tp->request_r_scale++;
1539
1540 soisconnecting(so);
1541 TCPSTAT_INC(tcps_connattempt);
1542 tcp_state_change(tp, TCPS_SYN_SENT);
1543 tp->iss = tcp_new_isn(&inp->inp_inc);
1544 if (tp->t_flags & TF_REQ_TSTMP)
1545 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1546 tcp_sendseqinit(tp);
1547
1548 return (0);
1549 }
1550 #endif /* INET6 */
1551
1552 /*
1553 * Export TCP internal state information via a struct tcp_info, based on the
1554 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1555 * (TCP state machine, etc). We export all information using FreeBSD-native
1556 * constants -- for example, the numeric values for tcpi_state will differ
1557 * from Linux.
1558 */
1559 void
tcp_fill_info(const struct tcpcb * tp,struct tcp_info * ti)1560 tcp_fill_info(const struct tcpcb *tp, struct tcp_info *ti)
1561 {
1562
1563 INP_LOCK_ASSERT(tptoinpcb(tp));
1564 bzero(ti, sizeof(*ti));
1565
1566 ti->tcpi_state = tp->t_state;
1567 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1568 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1569 if (tp->t_flags & TF_SACK_PERMIT)
1570 ti->tcpi_options |= TCPI_OPT_SACK;
1571 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1572 ti->tcpi_options |= TCPI_OPT_WSCALE;
1573 ti->tcpi_snd_wscale = tp->snd_scale;
1574 ti->tcpi_rcv_wscale = tp->rcv_scale;
1575 }
1576 switch (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) {
1577 case TF2_ECN_PERMIT:
1578 ti->tcpi_options |= TCPI_OPT_ECN;
1579 break;
1580 case TF2_ACE_PERMIT:
1581 /* FALLTHROUGH */
1582 case TF2_ECN_PERMIT | TF2_ACE_PERMIT:
1583 ti->tcpi_options |= TCPI_OPT_ACE;
1584 break;
1585 default:
1586 break;
1587 }
1588 if (tp->t_flags & TF_FASTOPEN)
1589 ti->tcpi_options |= TCPI_OPT_TFO;
1590
1591 ti->tcpi_rto = tp->t_rxtcur * tick;
1592 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1593 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1594 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1595
1596 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1597 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1598
1599 /*
1600 * FreeBSD-specific extension fields for tcp_info.
1601 */
1602 ti->tcpi_rcv_space = tp->rcv_wnd;
1603 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1604 ti->tcpi_snd_wnd = tp->snd_wnd;
1605 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1606 ti->tcpi_snd_nxt = tp->snd_nxt;
1607 ti->tcpi_snd_mss = tp->t_maxseg;
1608 ti->tcpi_rcv_mss = tp->t_maxseg;
1609 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1610 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1611 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1612 ti->tcpi_snd_una = tp->snd_una;
1613 ti->tcpi_snd_max = tp->snd_max;
1614 ti->tcpi_rcv_numsacks = tp->rcv_numsacks;
1615 ti->tcpi_rcv_adv = tp->rcv_adv;
1616 ti->tcpi_dupacks = tp->t_dupacks;
1617 ti->tcpi_rttmin = tp->t_rttlow;
1618 #ifdef TCP_OFFLOAD
1619 if (tp->t_flags & TF_TOE) {
1620 ti->tcpi_options |= TCPI_OPT_TOE;
1621 tcp_offload_tcp_info(tp, ti);
1622 }
1623 #endif
1624 /*
1625 * AccECN related counters.
1626 */
1627 if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) ==
1628 (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
1629 /*
1630 * Internal counter starts at 5 for AccECN
1631 * but 0 for RFC3168 ECN.
1632 */
1633 ti->tcpi_delivered_ce = tp->t_scep - 5;
1634 else
1635 ti->tcpi_delivered_ce = tp->t_scep;
1636 ti->tcpi_received_ce = tp->t_rcep;
1637 }
1638
1639 /*
1640 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1641 * socket option arguments. When it re-acquires the lock after the copy, it
1642 * has to revalidate that the connection is still valid for the socket
1643 * option.
1644 */
1645 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1646 INP_WLOCK(inp); \
1647 if (inp->inp_flags & INP_DROPPED) { \
1648 INP_WUNLOCK(inp); \
1649 cleanup; \
1650 return (ECONNRESET); \
1651 } \
1652 tp = intotcpcb(inp); \
1653 } while(0)
1654 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1655
1656 int
tcp_ctloutput_set(struct inpcb * inp,struct sockopt * sopt)1657 tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1658 {
1659 struct socket *so = inp->inp_socket;
1660 struct tcpcb *tp = intotcpcb(inp);
1661 int error = 0;
1662
1663 MPASS(sopt->sopt_dir == SOPT_SET);
1664 INP_WLOCK_ASSERT(inp);
1665 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1666 ("inp_flags == %x", inp->inp_flags));
1667 KASSERT(so != NULL, ("inp_socket == NULL"));
1668
1669 if (sopt->sopt_level != IPPROTO_TCP) {
1670 INP_WUNLOCK(inp);
1671 #ifdef INET6
1672 if (inp->inp_vflag & INP_IPV6PROTO)
1673 error = ip6_ctloutput(so, sopt);
1674 #endif
1675 #if defined(INET6) && defined(INET)
1676 else
1677 #endif
1678 #ifdef INET
1679 error = ip_ctloutput(so, sopt);
1680 #endif
1681 /*
1682 * When an IP-level socket option affects TCP, pass control
1683 * down to stack tfb_tcp_ctloutput, otherwise return what
1684 * IP level returned.
1685 */
1686 switch (sopt->sopt_level) {
1687 #ifdef INET6
1688 case IPPROTO_IPV6:
1689 if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1690 return (error);
1691 switch (sopt->sopt_name) {
1692 case IPV6_TCLASS:
1693 /* Notify tcp stacks that care (e.g. RACK). */
1694 break;
1695 case IPV6_USE_MIN_MTU:
1696 /* Update t_maxseg accordingly. */
1697 break;
1698 default:
1699 return (error);
1700 }
1701 break;
1702 #endif
1703 #ifdef INET
1704 case IPPROTO_IP:
1705 switch (sopt->sopt_name) {
1706 case IP_TOS:
1707 inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
1708 break;
1709 case IP_TTL:
1710 /* Notify tcp stacks that care (e.g. RACK). */
1711 break;
1712 default:
1713 return (error);
1714 }
1715 break;
1716 #endif
1717 default:
1718 return (error);
1719 }
1720 INP_WLOCK_RECHECK(inp);
1721 } else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1722 /*
1723 * Protect the TCP option TCP_FUNCTION_BLK so
1724 * that a sub-function can *never* overwrite this.
1725 */
1726 struct tcp_function_set fsn;
1727 struct tcp_function_block *blk;
1728 void *ptr = NULL;
1729
1730 INP_WUNLOCK(inp);
1731 error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1732 if (error)
1733 return (error);
1734
1735 INP_WLOCK_RECHECK(inp);
1736
1737 blk = find_and_ref_tcp_functions(&fsn);
1738 if (blk == NULL) {
1739 INP_WUNLOCK(inp);
1740 return (ENOENT);
1741 }
1742 if (tp->t_fb == blk) {
1743 /* You already have this */
1744 refcount_release(&blk->tfb_refcnt);
1745 INP_WUNLOCK(inp);
1746 return (0);
1747 }
1748 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1749 refcount_release(&blk->tfb_refcnt);
1750 INP_WUNLOCK(inp);
1751 return (ENOENT);
1752 }
1753 error = (*blk->tfb_tcp_handoff_ok)(tp);
1754 if (error) {
1755 refcount_release(&blk->tfb_refcnt);
1756 INP_WUNLOCK(inp);
1757 return (error);
1758 }
1759 /*
1760 * Ensure the new stack takes ownership with a
1761 * clean slate on peak rate threshold.
1762 */
1763 if (tp->t_fb->tfb_tcp_timer_stop_all != NULL)
1764 tp->t_fb->tfb_tcp_timer_stop_all(tp);
1765 if (blk->tfb_tcp_fb_init) {
1766 error = (*blk->tfb_tcp_fb_init)(tp, &ptr);
1767 if (error) {
1768 /*
1769 * Release the ref count the lookup
1770 * acquired.
1771 */
1772 refcount_release(&blk->tfb_refcnt);
1773 /*
1774 * Now there is a chance that the
1775 * init() function mucked with some
1776 * things before it failed, such as
1777 * hpts or inp_flags2 or timer granularity.
1778 * It should not of, but lets give the old
1779 * stack a chance to reset to a known good state.
1780 */
1781 if (tp->t_fb->tfb_switch_failed) {
1782 (*tp->t_fb->tfb_switch_failed)(tp);
1783 }
1784 goto err_out;
1785 }
1786 }
1787 if (tp->t_fb->tfb_tcp_fb_fini) {
1788 struct epoch_tracker et;
1789 /*
1790 * Tell the stack to cleanup with 0 i.e.
1791 * the tcb is not going away.
1792 */
1793 NET_EPOCH_ENTER(et);
1794 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1795 NET_EPOCH_EXIT(et);
1796 }
1797 /*
1798 * Release the old refcnt, the
1799 * lookup acquired a ref on the
1800 * new one already.
1801 */
1802 refcount_release(&tp->t_fb->tfb_refcnt);
1803 /*
1804 * Set in the new stack.
1805 */
1806 tp->t_fb = blk;
1807 tp->t_fb_ptr = ptr;
1808 #ifdef TCP_OFFLOAD
1809 if (tp->t_flags & TF_TOE) {
1810 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1811 sopt->sopt_name);
1812 }
1813 #endif
1814 err_out:
1815 INP_WUNLOCK(inp);
1816 return (error);
1817
1818 }
1819
1820 /* Pass in the INP locked, callee must unlock it. */
1821 return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1822 }
1823
1824 static int
tcp_ctloutput_get(struct inpcb * inp,struct sockopt * sopt)1825 tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1826 {
1827 struct socket *so = inp->inp_socket;
1828 struct tcpcb *tp = intotcpcb(inp);
1829 int error = 0;
1830
1831 MPASS(sopt->sopt_dir == SOPT_GET);
1832 INP_WLOCK_ASSERT(inp);
1833 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1834 ("inp_flags == %x", inp->inp_flags));
1835 KASSERT(so != NULL, ("inp_socket == NULL"));
1836
1837 if (sopt->sopt_level != IPPROTO_TCP) {
1838 INP_WUNLOCK(inp);
1839 #ifdef INET6
1840 if (inp->inp_vflag & INP_IPV6PROTO)
1841 error = ip6_ctloutput(so, sopt);
1842 #endif /* INET6 */
1843 #if defined(INET6) && defined(INET)
1844 else
1845 #endif
1846 #ifdef INET
1847 error = ip_ctloutput(so, sopt);
1848 #endif
1849 return (error);
1850 }
1851 if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1852 (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1853 struct tcp_function_set fsn;
1854
1855 if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1856 memset(&fsn, 0, sizeof(fsn));
1857 find_tcp_function_alias(tp->t_fb, &fsn);
1858 } else {
1859 strncpy(fsn.function_set_name,
1860 tp->t_fb->tfb_tcp_block_name,
1861 TCP_FUNCTION_NAME_LEN_MAX);
1862 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1863 }
1864 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1865 INP_WUNLOCK(inp);
1866 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1867 return (error);
1868 }
1869
1870 /* Pass in the INP locked, callee must unlock it. */
1871 return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1872 }
1873
1874 int
tcp_ctloutput(struct socket * so,struct sockopt * sopt)1875 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1876 {
1877 struct inpcb *inp;
1878
1879 inp = sotoinpcb(so);
1880 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1881
1882 INP_WLOCK(inp);
1883 if (inp->inp_flags & INP_DROPPED) {
1884 INP_WUNLOCK(inp);
1885 return (ECONNRESET);
1886 }
1887 if (sopt->sopt_dir == SOPT_SET)
1888 return (tcp_ctloutput_set(inp, sopt));
1889 else if (sopt->sopt_dir == SOPT_GET)
1890 return (tcp_ctloutput_get(inp, sopt));
1891 else
1892 panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
1893 }
1894
1895 /*
1896 * If this assert becomes untrue, we need to change the size of the buf
1897 * variable in tcp_default_ctloutput().
1898 */
1899 #ifdef CTASSERT
1900 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1901 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1902 #endif
1903
1904 extern struct cc_algo newreno_cc_algo;
1905
1906 static int
tcp_set_cc_mod(struct inpcb * inp,struct sockopt * sopt)1907 tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1908 {
1909 struct cc_algo *algo;
1910 void *ptr = NULL;
1911 struct tcpcb *tp;
1912 struct cc_var cc_mem;
1913 char buf[TCP_CA_NAME_MAX];
1914 size_t mem_sz;
1915 int error;
1916
1917 INP_WUNLOCK(inp);
1918 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1919 if (error)
1920 return(error);
1921 buf[sopt->sopt_valsize] = '\0';
1922 CC_LIST_RLOCK();
1923 STAILQ_FOREACH(algo, &cc_list, entries) {
1924 if (strncmp(buf, algo->name,
1925 TCP_CA_NAME_MAX) == 0) {
1926 if (algo->flags & CC_MODULE_BEING_REMOVED) {
1927 /* We can't "see" modules being unloaded */
1928 continue;
1929 }
1930 break;
1931 }
1932 }
1933 if (algo == NULL) {
1934 CC_LIST_RUNLOCK();
1935 return(ESRCH);
1936 }
1937 /*
1938 * With a reference the algorithm cannot be removed
1939 * so we hold a reference through the change process.
1940 */
1941 cc_refer(algo);
1942 CC_LIST_RUNLOCK();
1943 if (algo->cb_init != NULL) {
1944 /* We can now pre-get the memory for the CC */
1945 mem_sz = (*algo->cc_data_sz)();
1946 if (mem_sz == 0) {
1947 goto no_mem_needed;
1948 }
1949 ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
1950 } else {
1951 no_mem_needed:
1952 mem_sz = 0;
1953 ptr = NULL;
1954 }
1955 /*
1956 * Make sure its all clean and zero and also get
1957 * back the inplock.
1958 */
1959 memset(&cc_mem, 0, sizeof(cc_mem));
1960 INP_WLOCK(inp);
1961 if (inp->inp_flags & INP_DROPPED) {
1962 INP_WUNLOCK(inp);
1963 if (ptr)
1964 free(ptr, M_CC_MEM);
1965 /* Release our temp reference */
1966 CC_LIST_RLOCK();
1967 cc_release(algo);
1968 CC_LIST_RUNLOCK();
1969 return (ECONNRESET);
1970 }
1971 tp = intotcpcb(inp);
1972 if (ptr != NULL)
1973 memset(ptr, 0, mem_sz);
1974 cc_mem.tp = tp;
1975 /*
1976 * We once again hold a write lock over the tcb so it's
1977 * safe to do these things without ordering concerns.
1978 * Note here we init into stack memory.
1979 */
1980 if (algo->cb_init != NULL)
1981 error = algo->cb_init(&cc_mem, ptr);
1982 else
1983 error = 0;
1984 /*
1985 * The CC algorithms, when given their memory
1986 * should not fail we could in theory have a
1987 * KASSERT here.
1988 */
1989 if (error == 0) {
1990 /*
1991 * Touchdown, lets go ahead and move the
1992 * connection to the new CC module by
1993 * copying in the cc_mem after we call
1994 * the old ones cleanup (if any).
1995 */
1996 if (CC_ALGO(tp)->cb_destroy != NULL)
1997 CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
1998 /* Detach the old CC from the tcpcb */
1999 cc_detach(tp);
2000 /* Copy in our temp memory that was inited */
2001 memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var));
2002 /* Now attach the new, which takes a reference */
2003 cc_attach(tp, algo);
2004 /* Ok now are we where we have gotten past any conn_init? */
2005 if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
2006 /* Yep run the connection init for the new CC */
2007 CC_ALGO(tp)->conn_init(&tp->t_ccv);
2008 }
2009 } else if (ptr)
2010 free(ptr, M_CC_MEM);
2011 INP_WUNLOCK(inp);
2012 /* Now lets release our temp reference */
2013 CC_LIST_RLOCK();
2014 cc_release(algo);
2015 CC_LIST_RUNLOCK();
2016 return (error);
2017 }
2018
2019 int
tcp_default_ctloutput(struct tcpcb * tp,struct sockopt * sopt)2020 tcp_default_ctloutput(struct tcpcb *tp, struct sockopt *sopt)
2021 {
2022 struct inpcb *inp = tptoinpcb(tp);
2023 int error, opt, optval;
2024 u_int ui;
2025 struct tcp_info ti;
2026 #ifdef KERN_TLS
2027 struct tls_enable tls;
2028 struct socket *so = inp->inp_socket;
2029 #endif
2030 char *pbuf, buf[TCP_LOG_ID_LEN];
2031 #ifdef STATS
2032 struct statsblob *sbp;
2033 #endif
2034 size_t len;
2035
2036 INP_WLOCK_ASSERT(inp);
2037 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
2038 ("inp_flags == %x", inp->inp_flags));
2039 KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2040
2041 switch (sopt->sopt_level) {
2042 #ifdef INET6
2043 case IPPROTO_IPV6:
2044 MPASS(inp->inp_vflag & INP_IPV6PROTO);
2045 switch (sopt->sopt_name) {
2046 case IPV6_USE_MIN_MTU:
2047 tcp6_use_min_mtu(tp);
2048 /* FALLTHROUGH */
2049 }
2050 INP_WUNLOCK(inp);
2051 return (0);
2052 #endif
2053 #ifdef INET
2054 case IPPROTO_IP:
2055 INP_WUNLOCK(inp);
2056 return (0);
2057 #endif
2058 }
2059
2060 /*
2061 * For TCP_CCALGOOPT forward the control to CC module, for both
2062 * SOPT_SET and SOPT_GET.
2063 */
2064 switch (sopt->sopt_name) {
2065 case TCP_CCALGOOPT:
2066 INP_WUNLOCK(inp);
2067 if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2068 return (EINVAL);
2069 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2070 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2071 sopt->sopt_valsize);
2072 if (error) {
2073 free(pbuf, M_TEMP);
2074 return (error);
2075 }
2076 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2077 if (CC_ALGO(tp)->ctl_output != NULL)
2078 error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf);
2079 else
2080 error = ENOENT;
2081 INP_WUNLOCK(inp);
2082 if (error == 0 && sopt->sopt_dir == SOPT_GET)
2083 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2084 free(pbuf, M_TEMP);
2085 return (error);
2086 }
2087
2088 switch (sopt->sopt_dir) {
2089 case SOPT_SET:
2090 switch (sopt->sopt_name) {
2091 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2092 case TCP_MD5SIG:
2093 INP_WUNLOCK(inp);
2094 if (!TCPMD5_ENABLED())
2095 return (ENOPROTOOPT);
2096 error = TCPMD5_PCBCTL(inp, sopt);
2097 if (error)
2098 return (error);
2099 INP_WLOCK_RECHECK(inp);
2100 goto unlock_and_done;
2101 #endif /* IPSEC */
2102
2103 case TCP_NODELAY:
2104 case TCP_NOOPT:
2105 INP_WUNLOCK(inp);
2106 error = sooptcopyin(sopt, &optval, sizeof optval,
2107 sizeof optval);
2108 if (error)
2109 return (error);
2110
2111 INP_WLOCK_RECHECK(inp);
2112 switch (sopt->sopt_name) {
2113 case TCP_NODELAY:
2114 opt = TF_NODELAY;
2115 break;
2116 case TCP_NOOPT:
2117 opt = TF_NOOPT;
2118 break;
2119 default:
2120 opt = 0; /* dead code to fool gcc */
2121 break;
2122 }
2123
2124 if (optval)
2125 tp->t_flags |= opt;
2126 else
2127 tp->t_flags &= ~opt;
2128 unlock_and_done:
2129 #ifdef TCP_OFFLOAD
2130 if (tp->t_flags & TF_TOE) {
2131 tcp_offload_ctloutput(tp, sopt->sopt_dir,
2132 sopt->sopt_name);
2133 }
2134 #endif
2135 INP_WUNLOCK(inp);
2136 break;
2137
2138 case TCP_NOPUSH:
2139 INP_WUNLOCK(inp);
2140 error = sooptcopyin(sopt, &optval, sizeof optval,
2141 sizeof optval);
2142 if (error)
2143 return (error);
2144
2145 INP_WLOCK_RECHECK(inp);
2146 if (optval)
2147 tp->t_flags |= TF_NOPUSH;
2148 else if (tp->t_flags & TF_NOPUSH) {
2149 tp->t_flags &= ~TF_NOPUSH;
2150 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2151 struct epoch_tracker et;
2152
2153 NET_EPOCH_ENTER(et);
2154 error = tcp_output_nodrop(tp);
2155 NET_EPOCH_EXIT(et);
2156 }
2157 }
2158 goto unlock_and_done;
2159
2160 case TCP_REMOTE_UDP_ENCAPS_PORT:
2161 INP_WUNLOCK(inp);
2162 error = sooptcopyin(sopt, &optval, sizeof optval,
2163 sizeof optval);
2164 if (error)
2165 return (error);
2166 if ((optval < TCP_TUNNELING_PORT_MIN) ||
2167 (optval > TCP_TUNNELING_PORT_MAX)) {
2168 /* Its got to be in range */
2169 return (EINVAL);
2170 }
2171 if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
2172 /* You have to have enabled a UDP tunneling port first */
2173 return (EINVAL);
2174 }
2175 INP_WLOCK_RECHECK(inp);
2176 if (tp->t_state != TCPS_CLOSED) {
2177 /* You can't change after you are connected */
2178 error = EINVAL;
2179 } else {
2180 /* Ok we are all good set the port */
2181 tp->t_port = htons(optval);
2182 }
2183 goto unlock_and_done;
2184
2185 case TCP_MAXSEG:
2186 INP_WUNLOCK(inp);
2187 error = sooptcopyin(sopt, &optval, sizeof optval,
2188 sizeof optval);
2189 if (error)
2190 return (error);
2191
2192 INP_WLOCK_RECHECK(inp);
2193 if (optval > 0 && optval <= tp->t_maxseg &&
2194 optval + 40 >= V_tcp_minmss) {
2195 tp->t_maxseg = optval;
2196 if (tp->t_maxseg < V_tcp_mssdflt) {
2197 /*
2198 * The MSS is so small we should not process incoming
2199 * SACK's since we are subject to attack in such a
2200 * case.
2201 */
2202 tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT;
2203 } else {
2204 tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT;
2205 }
2206 } else
2207 error = EINVAL;
2208 goto unlock_and_done;
2209
2210 case TCP_INFO:
2211 INP_WUNLOCK(inp);
2212 error = EINVAL;
2213 break;
2214
2215 case TCP_STATS:
2216 INP_WUNLOCK(inp);
2217 #ifdef STATS
2218 error = sooptcopyin(sopt, &optval, sizeof optval,
2219 sizeof optval);
2220 if (error)
2221 return (error);
2222
2223 if (optval > 0)
2224 sbp = stats_blob_alloc(
2225 V_tcp_perconn_stats_dflt_tpl, 0);
2226 else
2227 sbp = NULL;
2228
2229 INP_WLOCK_RECHECK(inp);
2230 if ((tp->t_stats != NULL && sbp == NULL) ||
2231 (tp->t_stats == NULL && sbp != NULL)) {
2232 struct statsblob *t = tp->t_stats;
2233 tp->t_stats = sbp;
2234 sbp = t;
2235 }
2236 INP_WUNLOCK(inp);
2237
2238 stats_blob_destroy(sbp);
2239 #else
2240 return (EOPNOTSUPP);
2241 #endif /* !STATS */
2242 break;
2243
2244 case TCP_CONGESTION:
2245 error = tcp_set_cc_mod(inp, sopt);
2246 break;
2247
2248 case TCP_REUSPORT_LB_NUMA:
2249 INP_WUNLOCK(inp);
2250 error = sooptcopyin(sopt, &optval, sizeof(optval),
2251 sizeof(optval));
2252 INP_WLOCK_RECHECK(inp);
2253 if (!error)
2254 error = in_pcblbgroup_numa(inp, optval);
2255 INP_WUNLOCK(inp);
2256 break;
2257
2258 #ifdef KERN_TLS
2259 case TCP_TXTLS_ENABLE:
2260 INP_WUNLOCK(inp);
2261 error = ktls_copyin_tls_enable(sopt, &tls);
2262 if (error != 0)
2263 break;
2264 error = ktls_enable_tx(so, &tls);
2265 ktls_cleanup_tls_enable(&tls);
2266 break;
2267 case TCP_TXTLS_MODE:
2268 INP_WUNLOCK(inp);
2269 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2270 if (error != 0)
2271 return (error);
2272
2273 INP_WLOCK_RECHECK(inp);
2274 error = ktls_set_tx_mode(so, ui);
2275 INP_WUNLOCK(inp);
2276 break;
2277 case TCP_RXTLS_ENABLE:
2278 INP_WUNLOCK(inp);
2279 error = ktls_copyin_tls_enable(sopt, &tls);
2280 if (error != 0)
2281 break;
2282 error = ktls_enable_rx(so, &tls);
2283 ktls_cleanup_tls_enable(&tls);
2284 break;
2285 #endif
2286 case TCP_MAXUNACKTIME:
2287 case TCP_KEEPIDLE:
2288 case TCP_KEEPINTVL:
2289 case TCP_KEEPINIT:
2290 INP_WUNLOCK(inp);
2291 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2292 if (error)
2293 return (error);
2294
2295 if (ui > (UINT_MAX / hz)) {
2296 error = EINVAL;
2297 break;
2298 }
2299 ui *= hz;
2300
2301 INP_WLOCK_RECHECK(inp);
2302 switch (sopt->sopt_name) {
2303 case TCP_MAXUNACKTIME:
2304 tp->t_maxunacktime = ui;
2305 break;
2306
2307 case TCP_KEEPIDLE:
2308 tp->t_keepidle = ui;
2309 /*
2310 * XXX: better check current remaining
2311 * timeout and "merge" it with new value.
2312 */
2313 if ((tp->t_state > TCPS_LISTEN) &&
2314 (tp->t_state <= TCPS_CLOSING))
2315 tcp_timer_activate(tp, TT_KEEP,
2316 TP_KEEPIDLE(tp));
2317 break;
2318 case TCP_KEEPINTVL:
2319 tp->t_keepintvl = ui;
2320 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2321 (TP_MAXIDLE(tp) > 0))
2322 tcp_timer_activate(tp, TT_2MSL,
2323 TP_MAXIDLE(tp));
2324 break;
2325 case TCP_KEEPINIT:
2326 tp->t_keepinit = ui;
2327 if (tp->t_state == TCPS_SYN_RECEIVED ||
2328 tp->t_state == TCPS_SYN_SENT)
2329 tcp_timer_activate(tp, TT_KEEP,
2330 TP_KEEPINIT(tp));
2331 break;
2332 }
2333 goto unlock_and_done;
2334
2335 case TCP_KEEPCNT:
2336 INP_WUNLOCK(inp);
2337 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2338 if (error)
2339 return (error);
2340
2341 INP_WLOCK_RECHECK(inp);
2342 tp->t_keepcnt = ui;
2343 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2344 (TP_MAXIDLE(tp) > 0))
2345 tcp_timer_activate(tp, TT_2MSL,
2346 TP_MAXIDLE(tp));
2347 goto unlock_and_done;
2348
2349 case TCP_FASTOPEN: {
2350 struct tcp_fastopen tfo_optval;
2351
2352 INP_WUNLOCK(inp);
2353 if (!V_tcp_fastopen_client_enable &&
2354 !V_tcp_fastopen_server_enable)
2355 return (EPERM);
2356
2357 error = sooptcopyin(sopt, &tfo_optval,
2358 sizeof(tfo_optval), sizeof(int));
2359 if (error)
2360 return (error);
2361
2362 INP_WLOCK_RECHECK(inp);
2363 if ((tp->t_state != TCPS_CLOSED) &&
2364 (tp->t_state != TCPS_LISTEN)) {
2365 error = EINVAL;
2366 goto unlock_and_done;
2367 }
2368 if (tfo_optval.enable) {
2369 if (tp->t_state == TCPS_LISTEN) {
2370 if (!V_tcp_fastopen_server_enable) {
2371 error = EPERM;
2372 goto unlock_and_done;
2373 }
2374
2375 if (tp->t_tfo_pending == NULL)
2376 tp->t_tfo_pending =
2377 tcp_fastopen_alloc_counter();
2378 } else {
2379 /*
2380 * If a pre-shared key was provided,
2381 * stash it in the client cookie
2382 * field of the tcpcb for use during
2383 * connect.
2384 */
2385 if (sopt->sopt_valsize ==
2386 sizeof(tfo_optval)) {
2387 memcpy(tp->t_tfo_cookie.client,
2388 tfo_optval.psk,
2389 TCP_FASTOPEN_PSK_LEN);
2390 tp->t_tfo_client_cookie_len =
2391 TCP_FASTOPEN_PSK_LEN;
2392 }
2393 }
2394 tp->t_flags |= TF_FASTOPEN;
2395 } else
2396 tp->t_flags &= ~TF_FASTOPEN;
2397 goto unlock_and_done;
2398 }
2399
2400 #ifdef TCP_BLACKBOX
2401 case TCP_LOG:
2402 INP_WUNLOCK(inp);
2403 error = sooptcopyin(sopt, &optval, sizeof optval,
2404 sizeof optval);
2405 if (error)
2406 return (error);
2407
2408 INP_WLOCK_RECHECK(inp);
2409 error = tcp_log_state_change(tp, optval);
2410 goto unlock_and_done;
2411
2412 case TCP_LOGBUF:
2413 INP_WUNLOCK(inp);
2414 error = EINVAL;
2415 break;
2416
2417 case TCP_LOGID:
2418 INP_WUNLOCK(inp);
2419 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2420 if (error)
2421 break;
2422 buf[sopt->sopt_valsize] = '\0';
2423 INP_WLOCK_RECHECK(inp);
2424 error = tcp_log_set_id(tp, buf);
2425 /* tcp_log_set_id() unlocks the INP. */
2426 break;
2427
2428 case TCP_LOGDUMP:
2429 case TCP_LOGDUMPID:
2430 INP_WUNLOCK(inp);
2431 error =
2432 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2433 if (error)
2434 break;
2435 buf[sopt->sopt_valsize] = '\0';
2436 INP_WLOCK_RECHECK(inp);
2437 if (sopt->sopt_name == TCP_LOGDUMP) {
2438 error = tcp_log_dump_tp_logbuf(tp, buf,
2439 M_WAITOK, true);
2440 INP_WUNLOCK(inp);
2441 } else {
2442 tcp_log_dump_tp_bucket_logbufs(tp, buf);
2443 /*
2444 * tcp_log_dump_tp_bucket_logbufs() drops the
2445 * INP lock.
2446 */
2447 }
2448 break;
2449 #endif
2450
2451 default:
2452 INP_WUNLOCK(inp);
2453 error = ENOPROTOOPT;
2454 break;
2455 }
2456 break;
2457
2458 case SOPT_GET:
2459 tp = intotcpcb(inp);
2460 switch (sopt->sopt_name) {
2461 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2462 case TCP_MD5SIG:
2463 INP_WUNLOCK(inp);
2464 if (!TCPMD5_ENABLED())
2465 return (ENOPROTOOPT);
2466 error = TCPMD5_PCBCTL(inp, sopt);
2467 break;
2468 #endif
2469
2470 case TCP_NODELAY:
2471 optval = tp->t_flags & TF_NODELAY;
2472 INP_WUNLOCK(inp);
2473 error = sooptcopyout(sopt, &optval, sizeof optval);
2474 break;
2475 case TCP_MAXSEG:
2476 optval = tp->t_maxseg;
2477 INP_WUNLOCK(inp);
2478 error = sooptcopyout(sopt, &optval, sizeof optval);
2479 break;
2480 case TCP_REMOTE_UDP_ENCAPS_PORT:
2481 optval = ntohs(tp->t_port);
2482 INP_WUNLOCK(inp);
2483 error = sooptcopyout(sopt, &optval, sizeof optval);
2484 break;
2485 case TCP_NOOPT:
2486 optval = tp->t_flags & TF_NOOPT;
2487 INP_WUNLOCK(inp);
2488 error = sooptcopyout(sopt, &optval, sizeof optval);
2489 break;
2490 case TCP_NOPUSH:
2491 optval = tp->t_flags & TF_NOPUSH;
2492 INP_WUNLOCK(inp);
2493 error = sooptcopyout(sopt, &optval, sizeof optval);
2494 break;
2495 case TCP_INFO:
2496 tcp_fill_info(tp, &ti);
2497 INP_WUNLOCK(inp);
2498 error = sooptcopyout(sopt, &ti, sizeof ti);
2499 break;
2500 case TCP_STATS:
2501 {
2502 #ifdef STATS
2503 int nheld;
2504 TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2505
2506 error = 0;
2507 socklen_t outsbsz = sopt->sopt_valsize;
2508 if (tp->t_stats == NULL)
2509 error = ENOENT;
2510 else if (outsbsz >= tp->t_stats->cursz)
2511 outsbsz = tp->t_stats->cursz;
2512 else if (outsbsz >= sizeof(struct statsblob))
2513 outsbsz = sizeof(struct statsblob);
2514 else
2515 error = EINVAL;
2516 INP_WUNLOCK(inp);
2517 if (error)
2518 break;
2519
2520 sbp = sopt->sopt_val;
2521 nheld = atop(round_page(((vm_offset_t)sbp) +
2522 (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2523 vm_page_t ma[nheld];
2524 if (vm_fault_quick_hold_pages(
2525 &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2526 outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2527 nheld) < 0) {
2528 error = EFAULT;
2529 break;
2530 }
2531
2532 if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2533 SIZEOF_MEMBER(struct statsblob, flags))))
2534 goto unhold;
2535
2536 INP_WLOCK_RECHECK(inp);
2537 error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2538 sbflags | SB_CLONE_USRDSTNOFAULT);
2539 INP_WUNLOCK(inp);
2540 sopt->sopt_valsize = outsbsz;
2541 unhold:
2542 vm_page_unhold_pages(ma, nheld);
2543 #else
2544 INP_WUNLOCK(inp);
2545 error = EOPNOTSUPP;
2546 #endif /* !STATS */
2547 break;
2548 }
2549 case TCP_CONGESTION:
2550 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2551 INP_WUNLOCK(inp);
2552 error = sooptcopyout(sopt, buf, len + 1);
2553 break;
2554 case TCP_MAXUNACKTIME:
2555 case TCP_KEEPIDLE:
2556 case TCP_KEEPINTVL:
2557 case TCP_KEEPINIT:
2558 case TCP_KEEPCNT:
2559 switch (sopt->sopt_name) {
2560 case TCP_MAXUNACKTIME:
2561 ui = TP_MAXUNACKTIME(tp) / hz;
2562 break;
2563 case TCP_KEEPIDLE:
2564 ui = TP_KEEPIDLE(tp) / hz;
2565 break;
2566 case TCP_KEEPINTVL:
2567 ui = TP_KEEPINTVL(tp) / hz;
2568 break;
2569 case TCP_KEEPINIT:
2570 ui = TP_KEEPINIT(tp) / hz;
2571 break;
2572 case TCP_KEEPCNT:
2573 ui = TP_KEEPCNT(tp);
2574 break;
2575 }
2576 INP_WUNLOCK(inp);
2577 error = sooptcopyout(sopt, &ui, sizeof(ui));
2578 break;
2579 case TCP_FASTOPEN:
2580 optval = tp->t_flags & TF_FASTOPEN;
2581 INP_WUNLOCK(inp);
2582 error = sooptcopyout(sopt, &optval, sizeof optval);
2583 break;
2584 #ifdef TCP_BLACKBOX
2585 case TCP_LOG:
2586 optval = tcp_get_bblog_state(tp);
2587 INP_WUNLOCK(inp);
2588 error = sooptcopyout(sopt, &optval, sizeof(optval));
2589 break;
2590 case TCP_LOGBUF:
2591 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2592 error = tcp_log_getlogbuf(sopt, tp);
2593 break;
2594 case TCP_LOGID:
2595 len = tcp_log_get_id(tp, buf);
2596 INP_WUNLOCK(inp);
2597 error = sooptcopyout(sopt, buf, len + 1);
2598 break;
2599 case TCP_LOGDUMP:
2600 case TCP_LOGDUMPID:
2601 INP_WUNLOCK(inp);
2602 error = EINVAL;
2603 break;
2604 #endif
2605 #ifdef KERN_TLS
2606 case TCP_TXTLS_MODE:
2607 error = ktls_get_tx_mode(so, &optval);
2608 INP_WUNLOCK(inp);
2609 if (error == 0)
2610 error = sooptcopyout(sopt, &optval,
2611 sizeof(optval));
2612 break;
2613 case TCP_RXTLS_MODE:
2614 error = ktls_get_rx_mode(so, &optval);
2615 INP_WUNLOCK(inp);
2616 if (error == 0)
2617 error = sooptcopyout(sopt, &optval,
2618 sizeof(optval));
2619 break;
2620 #endif
2621 default:
2622 INP_WUNLOCK(inp);
2623 error = ENOPROTOOPT;
2624 break;
2625 }
2626 break;
2627 }
2628 return (error);
2629 }
2630 #undef INP_WLOCK_RECHECK
2631 #undef INP_WLOCK_RECHECK_CLEANUP
2632
2633 /*
2634 * Initiate (or continue) disconnect.
2635 * If embryonic state, just send reset (once).
2636 * If in ``let data drain'' option and linger null, just drop.
2637 * Otherwise (hard), mark socket disconnecting and drop
2638 * current input data; switch states based on user close, and
2639 * send segment to peer (with FIN).
2640 */
2641 static void
tcp_disconnect(struct tcpcb * tp)2642 tcp_disconnect(struct tcpcb *tp)
2643 {
2644 struct inpcb *inp = tptoinpcb(tp);
2645 struct socket *so = tptosocket(tp);
2646
2647 NET_EPOCH_ASSERT();
2648 INP_WLOCK_ASSERT(inp);
2649
2650 /*
2651 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2652 * socket is still open.
2653 */
2654 if (tp->t_state < TCPS_ESTABLISHED &&
2655 !(tp->t_state > TCPS_LISTEN && (tp->t_flags & TF_FASTOPEN))) {
2656 tp = tcp_close(tp);
2657 KASSERT(tp != NULL,
2658 ("tcp_disconnect: tcp_close() returned NULL"));
2659 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2660 tp = tcp_drop(tp, 0);
2661 KASSERT(tp != NULL,
2662 ("tcp_disconnect: tcp_drop() returned NULL"));
2663 } else {
2664 soisdisconnecting(so);
2665 sbflush(&so->so_rcv);
2666 tcp_usrclosed(tp);
2667 if (!(inp->inp_flags & INP_DROPPED))
2668 /* Ignore stack's drop request, we already at it. */
2669 (void)tcp_output_nodrop(tp);
2670 }
2671 }
2672
2673 /*
2674 * User issued close, and wish to trail through shutdown states:
2675 * if never received SYN, just forget it. If got a SYN from peer,
2676 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2677 * If already got a FIN from peer, then almost done; go to LAST_ACK
2678 * state. In all other cases, have already sent FIN to peer (e.g.
2679 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2680 * for peer to send FIN or not respond to keep-alives, etc.
2681 * We can let the user exit from the close as soon as the FIN is acked.
2682 */
2683 static void
tcp_usrclosed(struct tcpcb * tp)2684 tcp_usrclosed(struct tcpcb *tp)
2685 {
2686
2687 NET_EPOCH_ASSERT();
2688 INP_WLOCK_ASSERT(tptoinpcb(tp));
2689
2690 switch (tp->t_state) {
2691 case TCPS_LISTEN:
2692 #ifdef TCP_OFFLOAD
2693 tcp_offload_listen_stop(tp);
2694 #endif
2695 tcp_state_change(tp, TCPS_CLOSED);
2696 /* FALLTHROUGH */
2697 case TCPS_CLOSED:
2698 tp = tcp_close(tp);
2699 /*
2700 * tcp_close() should never return NULL here as the socket is
2701 * still open.
2702 */
2703 KASSERT(tp != NULL,
2704 ("tcp_usrclosed: tcp_close() returned NULL"));
2705 break;
2706
2707 case TCPS_SYN_SENT:
2708 case TCPS_SYN_RECEIVED:
2709 tp->t_flags |= TF_NEEDFIN;
2710 break;
2711
2712 case TCPS_ESTABLISHED:
2713 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2714 break;
2715
2716 case TCPS_CLOSE_WAIT:
2717 tcp_state_change(tp, TCPS_LAST_ACK);
2718 break;
2719 }
2720 if (tp->t_acktime == 0)
2721 tp->t_acktime = ticks;
2722 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2723 tcp_free_sackholes(tp);
2724 soisdisconnected(tptosocket(tp));
2725 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2726 if (tp->t_state == TCPS_FIN_WAIT_2) {
2727 int timeout;
2728
2729 timeout = (tcp_fast_finwait2_recycle) ?
2730 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2731 tcp_timer_activate(tp, TT_2MSL, timeout);
2732 }
2733 }
2734 }
2735
2736 #ifdef DDB
2737 static void
db_print_indent(int indent)2738 db_print_indent(int indent)
2739 {
2740 int i;
2741
2742 for (i = 0; i < indent; i++)
2743 db_printf(" ");
2744 }
2745
2746 static void
db_print_tstate(int t_state)2747 db_print_tstate(int t_state)
2748 {
2749
2750 switch (t_state) {
2751 case TCPS_CLOSED:
2752 db_printf("TCPS_CLOSED");
2753 return;
2754
2755 case TCPS_LISTEN:
2756 db_printf("TCPS_LISTEN");
2757 return;
2758
2759 case TCPS_SYN_SENT:
2760 db_printf("TCPS_SYN_SENT");
2761 return;
2762
2763 case TCPS_SYN_RECEIVED:
2764 db_printf("TCPS_SYN_RECEIVED");
2765 return;
2766
2767 case TCPS_ESTABLISHED:
2768 db_printf("TCPS_ESTABLISHED");
2769 return;
2770
2771 case TCPS_CLOSE_WAIT:
2772 db_printf("TCPS_CLOSE_WAIT");
2773 return;
2774
2775 case TCPS_FIN_WAIT_1:
2776 db_printf("TCPS_FIN_WAIT_1");
2777 return;
2778
2779 case TCPS_CLOSING:
2780 db_printf("TCPS_CLOSING");
2781 return;
2782
2783 case TCPS_LAST_ACK:
2784 db_printf("TCPS_LAST_ACK");
2785 return;
2786
2787 case TCPS_FIN_WAIT_2:
2788 db_printf("TCPS_FIN_WAIT_2");
2789 return;
2790
2791 case TCPS_TIME_WAIT:
2792 db_printf("TCPS_TIME_WAIT");
2793 return;
2794
2795 default:
2796 db_printf("unknown");
2797 return;
2798 }
2799 }
2800
2801 static void
db_print_tflags(u_int t_flags)2802 db_print_tflags(u_int t_flags)
2803 {
2804 int comma;
2805
2806 comma = 0;
2807 if (t_flags & TF_ACKNOW) {
2808 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2809 comma = 1;
2810 }
2811 if (t_flags & TF_DELACK) {
2812 db_printf("%sTF_DELACK", comma ? ", " : "");
2813 comma = 1;
2814 }
2815 if (t_flags & TF_NODELAY) {
2816 db_printf("%sTF_NODELAY", comma ? ", " : "");
2817 comma = 1;
2818 }
2819 if (t_flags & TF_NOOPT) {
2820 db_printf("%sTF_NOOPT", comma ? ", " : "");
2821 comma = 1;
2822 }
2823 if (t_flags & TF_SENTFIN) {
2824 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2825 comma = 1;
2826 }
2827 if (t_flags & TF_REQ_SCALE) {
2828 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2829 comma = 1;
2830 }
2831 if (t_flags & TF_RCVD_SCALE) {
2832 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2833 comma = 1;
2834 }
2835 if (t_flags & TF_REQ_TSTMP) {
2836 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2837 comma = 1;
2838 }
2839 if (t_flags & TF_RCVD_TSTMP) {
2840 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2841 comma = 1;
2842 }
2843 if (t_flags & TF_SACK_PERMIT) {
2844 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2845 comma = 1;
2846 }
2847 if (t_flags & TF_NEEDSYN) {
2848 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2849 comma = 1;
2850 }
2851 if (t_flags & TF_NEEDFIN) {
2852 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2853 comma = 1;
2854 }
2855 if (t_flags & TF_NOPUSH) {
2856 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2857 comma = 1;
2858 }
2859 if (t_flags & TF_PREVVALID) {
2860 db_printf("%sTF_PREVVALID", comma ? ", " : "");
2861 comma = 1;
2862 }
2863 if (t_flags & TF_WAKESOR) {
2864 db_printf("%sTF_WAKESOR", comma ? ", " : "");
2865 comma = 1;
2866 }
2867 if (t_flags & TF_GPUTINPROG) {
2868 db_printf("%sTF_GPUTINPROG", comma ? ", " : "");
2869 comma = 1;
2870 }
2871 if (t_flags & TF_MORETOCOME) {
2872 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2873 comma = 1;
2874 }
2875 if (t_flags & TF_SONOTCONN) {
2876 db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2877 comma = 1;
2878 }
2879 if (t_flags & TF_LASTIDLE) {
2880 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2881 comma = 1;
2882 }
2883 if (t_flags & TF_RXWIN0SENT) {
2884 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2885 comma = 1;
2886 }
2887 if (t_flags & TF_FASTRECOVERY) {
2888 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2889 comma = 1;
2890 }
2891 if (t_flags & TF_WASFRECOVERY) {
2892 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2893 comma = 1;
2894 }
2895 if (t_flags & TF_SIGNATURE) {
2896 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2897 comma = 1;
2898 }
2899 if (t_flags & TF_FORCEDATA) {
2900 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2901 comma = 1;
2902 }
2903 if (t_flags & TF_TSO) {
2904 db_printf("%sTF_TSO", comma ? ", " : "");
2905 comma = 1;
2906 }
2907 if (t_flags & TF_TOE) {
2908 db_printf("%sTF_TOE", comma ? ", " : "");
2909 comma = 1;
2910 }
2911 if (t_flags & TF_CLOSED) {
2912 db_printf("%sTF_CLOSED", comma ? ", " : "");
2913 comma = 1;
2914 }
2915 if (t_flags & TF_SENTSYN) {
2916 db_printf("%sTF_SENTSYN", comma ? ", " : "");
2917 comma = 1;
2918 }
2919 if (t_flags & TF_LRD) {
2920 db_printf("%sTF_LRD", comma ? ", " : "");
2921 comma = 1;
2922 }
2923 if (t_flags & TF_CONGRECOVERY) {
2924 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2925 comma = 1;
2926 }
2927 if (t_flags & TF_WASCRECOVERY) {
2928 db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
2929 comma = 1;
2930 }
2931 if (t_flags & TF_FASTOPEN) {
2932 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2933 comma = 1;
2934 }
2935 }
2936
2937 static void
db_print_tflags2(u_int t_flags2)2938 db_print_tflags2(u_int t_flags2)
2939 {
2940 int comma;
2941
2942 comma = 0;
2943 if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
2944 db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
2945 comma = 1;
2946 }
2947 if (t_flags2 & TF2_PLPMTU_PMTUD) {
2948 db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
2949 comma = 1;
2950 }
2951 if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
2952 db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
2953 comma = 1;
2954 }
2955 if (t_flags2 & TF2_LOG_AUTO) {
2956 db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
2957 comma = 1;
2958 }
2959 if (t_flags2 & TF2_DROP_AF_DATA) {
2960 db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
2961 comma = 1;
2962 }
2963 if (t_flags2 & TF2_ECN_PERMIT) {
2964 db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
2965 comma = 1;
2966 }
2967 if (t_flags2 & TF2_ECN_SND_CWR) {
2968 db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
2969 comma = 1;
2970 }
2971 if (t_flags2 & TF2_ECN_SND_ECE) {
2972 db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
2973 comma = 1;
2974 }
2975 if (t_flags2 & TF2_ACE_PERMIT) {
2976 db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
2977 comma = 1;
2978 }
2979 if (t_flags2 & TF2_HPTS_CPU_SET) {
2980 db_printf("%sTF2_HPTS_CPU_SET", comma ? ", " : "");
2981 comma = 1;
2982 }
2983 if (t_flags2 & TF2_FBYTES_COMPLETE) {
2984 db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
2985 comma = 1;
2986 }
2987 if (t_flags2 & TF2_ECN_USE_ECT1) {
2988 db_printf("%sTF2_ECN_USE_ECT1", comma ? ", " : "");
2989 comma = 1;
2990 }
2991 if (t_flags2 & TF2_TCP_ACCOUNTING) {
2992 db_printf("%sTF2_TCP_ACCOUNTING", comma ? ", " : "");
2993 comma = 1;
2994 }
2995 if (t_flags2 & TF2_HPTS_CALLS) {
2996 db_printf("%sTF2_HPTS_CALLS", comma ? ", " : "");
2997 comma = 1;
2998 }
2999 if (t_flags2 & TF2_MBUF_L_ACKS) {
3000 db_printf("%sTF2_MBUF_L_ACKS", comma ? ", " : "");
3001 comma = 1;
3002 }
3003 if (t_flags2 & TF2_MBUF_ACKCMP) {
3004 db_printf("%sTF2_MBUF_ACKCMP", comma ? ", " : "");
3005 comma = 1;
3006 }
3007 if (t_flags2 & TF2_SUPPORTS_MBUFQ) {
3008 db_printf("%sTF2_SUPPORTS_MBUFQ", comma ? ", " : "");
3009 comma = 1;
3010 }
3011 if (t_flags2 & TF2_MBUF_QUEUE_READY) {
3012 db_printf("%sTF2_MBUF_QUEUE_READY", comma ? ", " : "");
3013 comma = 1;
3014 }
3015 if (t_flags2 & TF2_DONT_SACK_QUEUE) {
3016 db_printf("%sTF2_DONT_SACK_QUEUE", comma ? ", " : "");
3017 comma = 1;
3018 }
3019 if (t_flags2 & TF2_CANNOT_DO_ECN) {
3020 db_printf("%sTF2_CANNOT_DO_ECN", comma ? ", " : "");
3021 comma = 1;
3022 }
3023 if (t_flags2 & TF2_PROC_SACK_PROHIBIT) {
3024 db_printf("%sTF2_PROC_SACK_PROHIBIT", comma ? ", " : "");
3025 comma = 1;
3026 }
3027 if (t_flags2 & TF2_IPSEC_TSO) {
3028 db_printf("%sTF2_IPSEC_TSO", comma ? ", " : "");
3029 comma = 1;
3030 }
3031 if (t_flags2 & TF2_NO_ISS_CHECK) {
3032 db_printf("%sTF2_NO_ISS_CHECK", comma ? ", " : "");
3033 comma = 1;
3034 }
3035 }
3036
3037 static void
db_print_toobflags(char t_oobflags)3038 db_print_toobflags(char t_oobflags)
3039 {
3040 int comma;
3041
3042 comma = 0;
3043 if (t_oobflags & TCPOOB_HAVEDATA) {
3044 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
3045 comma = 1;
3046 }
3047 if (t_oobflags & TCPOOB_HADDATA) {
3048 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
3049 comma = 1;
3050 }
3051 }
3052
3053 static void
db_print_bblog_state(int state)3054 db_print_bblog_state(int state)
3055 {
3056 switch (state) {
3057 case TCP_LOG_STATE_RATIO_OFF:
3058 db_printf("TCP_LOG_STATE_RATIO_OFF");
3059 break;
3060 case TCP_LOG_STATE_CLEAR:
3061 db_printf("TCP_LOG_STATE_CLEAR");
3062 break;
3063 case TCP_LOG_STATE_OFF:
3064 db_printf("TCP_LOG_STATE_OFF");
3065 break;
3066 case TCP_LOG_STATE_TAIL:
3067 db_printf("TCP_LOG_STATE_TAIL");
3068 break;
3069 case TCP_LOG_STATE_HEAD:
3070 db_printf("TCP_LOG_STATE_HEAD");
3071 break;
3072 case TCP_LOG_STATE_HEAD_AUTO:
3073 db_printf("TCP_LOG_STATE_HEAD_AUTO");
3074 break;
3075 case TCP_LOG_STATE_CONTINUAL:
3076 db_printf("TCP_LOG_STATE_CONTINUAL");
3077 break;
3078 case TCP_LOG_STATE_TAIL_AUTO:
3079 db_printf("TCP_LOG_STATE_TAIL_AUTO");
3080 break;
3081 case TCP_LOG_VIA_BBPOINTS:
3082 db_printf("TCP_LOG_STATE_BBPOINTS");
3083 break;
3084 default:
3085 db_printf("UNKNOWN(%d)", state);
3086 break;
3087 }
3088 }
3089
3090 static void
db_print_tcpcb(struct tcpcb * tp,const char * name,int indent,bool show_bblog)3091 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent, bool show_bblog)
3092 {
3093
3094 db_print_indent(indent);
3095 db_printf("%s at %p\n", name, tp);
3096
3097 indent += 2;
3098
3099 db_print_indent(indent);
3100 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
3101 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3102
3103 db_print_indent(indent);
3104 db_printf("t_callout: %p t_timers: %p\n",
3105 &tp->t_callout, &tp->t_timers);
3106
3107 db_print_indent(indent);
3108 db_printf("t_state: %d (", tp->t_state);
3109 db_print_tstate(tp->t_state);
3110 db_printf(")\n");
3111
3112 db_print_indent(indent);
3113 db_printf("t_flags: 0x%x (", tp->t_flags);
3114 db_print_tflags(tp->t_flags);
3115 db_printf(")\n");
3116
3117 db_print_indent(indent);
3118 db_printf("t_flags2: 0x%x (", tp->t_flags2);
3119 db_print_tflags2(tp->t_flags2);
3120 db_printf(")\n");
3121
3122 db_print_indent(indent);
3123 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: 0x%08x\n",
3124 tp->snd_una, tp->snd_max, tp->snd_nxt);
3125
3126 db_print_indent(indent);
3127 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
3128 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3129
3130 db_print_indent(indent);
3131 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
3132 tp->iss, tp->irs, tp->rcv_nxt);
3133
3134 db_print_indent(indent);
3135 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n",
3136 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3137
3138 db_print_indent(indent);
3139 db_printf("snd_wnd: %u snd_cwnd: %u\n",
3140 tp->snd_wnd, tp->snd_cwnd);
3141
3142 db_print_indent(indent);
3143 db_printf("snd_ssthresh: %u snd_recover: "
3144 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3145
3146 db_print_indent(indent);
3147 db_printf("t_rcvtime: %u t_startime: %u\n",
3148 tp->t_rcvtime, tp->t_starttime);
3149
3150 db_print_indent(indent);
3151 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
3152 tp->t_rtttime, tp->t_rtseq);
3153
3154 db_print_indent(indent);
3155 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
3156 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3157
3158 db_print_indent(indent);
3159 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u\n",
3160 tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3161
3162 db_print_indent(indent);
3163 db_printf("t_rttupdated: %u max_sndwnd: %u t_softerror: %d\n",
3164 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3165
3166 db_print_indent(indent);
3167 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3168 db_print_toobflags(tp->t_oobflags);
3169 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
3170
3171 db_print_indent(indent);
3172 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
3173 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3174
3175 db_print_indent(indent);
3176 db_printf("ts_recent: %u ts_recent_age: %u\n",
3177 tp->ts_recent, tp->ts_recent_age);
3178
3179 db_print_indent(indent);
3180 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
3181 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3182
3183 db_print_indent(indent);
3184 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x "
3185 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3186 tp->snd_recover_prev, tp->t_badrxtwin);
3187
3188 db_print_indent(indent);
3189 db_printf("snd_numholes: %d snd_holes first: %p\n",
3190 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3191
3192 db_print_indent(indent);
3193 db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n",
3194 tp->snd_fack, tp->rcv_numsacks);
3195
3196 /* Skip sackblks, sackhint. */
3197
3198 db_print_indent(indent);
3199 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
3200 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3201
3202 db_print_indent(indent);
3203 db_printf("t_fb.tfb_tcp_block_name: %s\n", tp->t_fb->tfb_tcp_block_name);
3204
3205 db_print_indent(indent);
3206 db_printf("t_cc.name: %s\n", tp->t_cc->name);
3207
3208 db_print_indent(indent);
3209 db_printf("_t_logstate: %d (", tp->_t_logstate);
3210 db_print_bblog_state(tp->_t_logstate);
3211 db_printf(")\n");
3212
3213 db_print_indent(indent);
3214 db_printf("t_lognum: %d t_loglimit: %d t_logsn: %u\n",
3215 tp->t_lognum, tp->t_loglimit, tp->t_logsn);
3216
3217 if (show_bblog) {
3218 #ifdef TCP_BLACKBOX
3219 db_print_bblog_entries(&tp->t_logs, indent);
3220 #else
3221 db_print_indent(indent);
3222 db_printf("BBLog not supported\n");
3223 #endif
3224 }
3225 }
3226
DB_SHOW_COMMAND(tcpcb,db_show_tcpcb)3227 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3228 {
3229 struct tcpcb *tp;
3230 bool show_bblog;
3231
3232 if (!have_addr) {
3233 db_printf("usage: show tcpcb <addr>\n");
3234 return;
3235 }
3236 show_bblog = strchr(modif, 'b') != NULL;
3237 tp = (struct tcpcb *)addr;
3238
3239 db_print_tcpcb(tp, "tcpcb", 0, show_bblog);
3240 }
3241
DB_SHOW_ALL_COMMAND(tcpcbs,db_show_all_tcpcbs)3242 DB_SHOW_ALL_COMMAND(tcpcbs, db_show_all_tcpcbs)
3243 {
3244 VNET_ITERATOR_DECL(vnet_iter);
3245 struct inpcb *inp;
3246 bool only_locked, show_bblog;
3247
3248 only_locked = strchr(modif, 'l') != NULL;
3249 show_bblog = strchr(modif, 'b') != NULL;
3250 VNET_FOREACH(vnet_iter) {
3251 CURVNET_SET(vnet_iter);
3252 CK_LIST_FOREACH(inp, &V_tcbinfo.ipi_listhead, inp_list) {
3253 if (only_locked &&
3254 inp->inp_lock.rw_lock == RW_UNLOCKED)
3255 continue;
3256 db_print_tcpcb(intotcpcb(inp), "tcpcb", 0, show_bblog);
3257 if (db_pager_quit)
3258 break;
3259 }
3260 CURVNET_RESTORE();
3261 if (db_pager_quit)
3262 break;
3263 }
3264 }
3265 #endif
3266