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