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