1 /*-
2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2006-2007 Robert N. M. Watson
5 * Copyright (c) 2010-2011 Juniper Networks, Inc.
6 * All rights reserved.
7 *
8 * Portions of this software were developed by Robert N. M. Watson under
9 * contract to Juniper Networks, Inc.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
36 */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include "opt_ddb.h"
42 #include "opt_inet.h"
43 #include "opt_inet6.h"
44 #include "opt_ipsec.h"
45 #include "opt_tcpdebug.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/limits.h>
50 #include <sys/malloc.h>
51 #include <sys/refcount.h>
52 #include <sys/kernel.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/syslog.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_systm.h>
78 #include <netinet/in_var.h>
79 #include <netinet/ip_var.h>
80 #ifdef INET6
81 #include <netinet/ip6.h>
82 #include <netinet6/in6_pcb.h>
83 #include <netinet6/ip6_var.h>
84 #include <netinet6/scope6_var.h>
85 #endif
86 #ifdef TCP_RFC7413
87 #include <netinet/tcp_fastopen.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/tcpip.h>
95 #include <netinet/cc/cc.h>
96 #ifdef TCPPCAP
97 #include <netinet/tcp_pcap.h>
98 #endif
99 #ifdef TCPDEBUG
100 #include <netinet/tcp_debug.h>
101 #endif
102 #ifdef TCP_OFFLOAD
103 #include <netinet/tcp_offload.h>
104 #endif
105 #include <netipsec/ipsec_support.h>
106
107 /*
108 * TCP protocol interface to socket abstraction.
109 */
110 static int tcp_attach(struct socket *);
111 #ifdef INET
112 static int tcp_connect(struct tcpcb *, struct sockaddr *,
113 struct thread *td);
114 #endif /* INET */
115 #ifdef INET6
116 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
117 struct thread *td);
118 #endif /* INET6 */
119 static void tcp_disconnect(struct tcpcb *);
120 static void tcp_usrclosed(struct tcpcb *);
121 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
122
123 #ifdef TCPDEBUG
124 #define TCPDEBUG0 int ostate = 0
125 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
126 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
127 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
128 #else
129 #define TCPDEBUG0
130 #define TCPDEBUG1()
131 #define TCPDEBUG2(req)
132 #endif
133
134 /*
135 * TCP attaches to socket via pru_attach(), reserving space,
136 * and an internet control block.
137 */
138 static int
139 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
140 {
141 struct inpcb *inp;
142 struct tcpcb *tp = NULL;
143 int error;
144 TCPDEBUG0;
145
146 inp = sotoinpcb(so);
147 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
148 TCPDEBUG1();
149
150 error = tcp_attach(so);
151 if (error)
152 goto out;
153
154 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
155 so->so_linger = TCP_LINGERTIME;
156
157 inp = sotoinpcb(so);
158 tp = intotcpcb(inp);
159 out:
160 TCPDEBUG2(PRU_ATTACH);
161 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
162 return error;
163 }
164
165 /*
166 * tcp_detach is called when the socket layer loses its final reference
167 * to the socket, be it a file descriptor reference, a reference from TCP,
168 * etc. At this point, there is only one case in which we will keep around
169 * inpcb state: time wait.
170 *
171 * This function can probably be re-absorbed back into tcp_usr_detach() now
172 * that there is a single detach path.
173 */
174 static void
175 tcp_detach(struct socket *so, struct inpcb *inp)
176 {
177 struct tcpcb *tp;
178
179 INP_INFO_LOCK_ASSERT(&V_tcbinfo);
180 INP_WLOCK_ASSERT(inp);
181
182 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
183 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
184
185 tp = intotcpcb(inp);
186
187 if (inp->inp_flags & INP_TIMEWAIT) {
188 /*
189 * There are two cases to handle: one in which the time wait
190 * state is being discarded (INP_DROPPED), and one in which
191 * this connection will remain in timewait. In the former,
192 * it is time to discard all state (except tcptw, which has
193 * already been discarded by the timewait close code, which
194 * should be further up the call stack somewhere). In the
195 * latter case, we detach from the socket, but leave the pcb
196 * present until timewait ends.
197 *
198 * XXXRW: Would it be cleaner to free the tcptw here?
199 *
200 * Astute question indeed, from twtcp perspective there are
201 * three cases to consider:
202 *
203 * #1 tcp_detach is called at tcptw creation time by
204 * tcp_twstart, then do not discard the newly created tcptw
205 * and leave inpcb present until timewait ends
206 * #2 tcp_detach is called at timewait end (or reuse) by
207 * tcp_twclose, then the tcptw has already been discarded
208 * (or reused) and inpcb is freed here
209 * #3 tcp_detach is called() after timewait ends (or reuse)
210 * (e.g. by soclose), then tcptw has already been discarded
211 * (or reused) and inpcb is freed here
212 *
213 * In all three cases the tcptw should not be freed here.
214 */
215 if (inp->inp_flags & INP_DROPPED) {
216 in_pcbdetach(inp);
217 if (__predict_true(tp == NULL)) {
218 in_pcbfree(inp);
219 } else {
220 /*
221 * This case should not happen as in TIMEWAIT
222 * state the inp should not be destroyed before
223 * its tcptw. If INVARIANTS is defined, panic.
224 */
225 #ifdef INVARIANTS
226 panic("%s: Panic before an inp double-free: "
227 "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
228 , __func__);
229 #else
230 log(LOG_ERR, "%s: Avoid an inp double-free: "
231 "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
232 , __func__);
233 #endif
234 INP_WUNLOCK(inp);
235 }
236 } else {
237 in_pcbdetach(inp);
238 INP_WUNLOCK(inp);
239 }
240 } else {
241 /*
242 * If the connection is not in timewait, we consider two
243 * two conditions: one in which no further processing is
244 * necessary (dropped || embryonic), and one in which TCP is
245 * not yet done, but no longer requires the socket, so the
246 * pcb will persist for the time being.
247 *
248 * XXXRW: Does the second case still occur?
249 */
250 if (inp->inp_flags & INP_DROPPED ||
251 tp->t_state < TCPS_SYN_SENT) {
252 tcp_discardcb(tp);
253 in_pcbdetach(inp);
254 in_pcbfree(inp);
255 } else {
256 in_pcbdetach(inp);
257 INP_WUNLOCK(inp);
258 }
259 }
260 }
261
262 /*
263 * pru_detach() detaches the TCP protocol from the socket.
264 * If the protocol state is non-embryonic, then can't
265 * do this directly: have to initiate a pru_disconnect(),
266 * which may finish later; embryonic TCB's can just
267 * be discarded here.
268 */
269 static void
270 tcp_usr_detach(struct socket *so)
271 {
272 struct inpcb *inp;
273 int rlock = 0;
274
275 inp = sotoinpcb(so);
276 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
277 if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
278 INP_INFO_RLOCK(&V_tcbinfo);
279 rlock = 1;
280 }
281 INP_WLOCK(inp);
282 KASSERT(inp->inp_socket != NULL,
283 ("tcp_usr_detach: inp_socket == NULL"));
284 tcp_detach(so, inp);
285 if (rlock)
286 INP_INFO_RUNLOCK(&V_tcbinfo);
287 }
288
289 #ifdef INET
290 /*
291 * Give the socket an address.
292 */
293 static int
294 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
295 {
296 int error = 0;
297 struct inpcb *inp;
298 struct tcpcb *tp = NULL;
299 struct sockaddr_in *sinp;
300
301 sinp = (struct sockaddr_in *)nam;
302 if (nam->sa_len != sizeof (*sinp))
303 return (EINVAL);
304 /*
305 * Must check for multicast addresses and disallow binding
306 * to them.
307 */
308 if (sinp->sin_family == AF_INET &&
309 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
310 return (EAFNOSUPPORT);
311
312 TCPDEBUG0;
313 inp = sotoinpcb(so);
314 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
315 INP_WLOCK(inp);
316 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
317 error = EINVAL;
318 goto out;
319 }
320 tp = intotcpcb(inp);
321 TCPDEBUG1();
322 INP_HASH_WLOCK(&V_tcbinfo);
323 error = in_pcbbind(inp, nam, td->td_ucred);
324 INP_HASH_WUNLOCK(&V_tcbinfo);
325 out:
326 TCPDEBUG2(PRU_BIND);
327 TCP_PROBE2(debug__user, tp, PRU_BIND);
328 INP_WUNLOCK(inp);
329
330 return (error);
331 }
332 #endif /* INET */
333
334 #ifdef INET6
335 static int
336 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
337 {
338 int error = 0;
339 struct inpcb *inp;
340 struct tcpcb *tp = NULL;
341 struct sockaddr_in6 *sin6p;
342 u_char vflagsav;
343
344 sin6p = (struct sockaddr_in6 *)nam;
345 if (nam->sa_len != sizeof (*sin6p))
346 return (EINVAL);
347 /*
348 * Must check for multicast addresses and disallow binding
349 * to them.
350 */
351 if (sin6p->sin6_family == AF_INET6 &&
352 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
353 return (EAFNOSUPPORT);
354
355 TCPDEBUG0;
356 inp = sotoinpcb(so);
357 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
358 INP_WLOCK(inp);
359 vflagsav = inp->inp_vflag;
360 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
361 error = EINVAL;
362 goto out;
363 }
364 tp = intotcpcb(inp);
365 TCPDEBUG1();
366 INP_HASH_WLOCK(&V_tcbinfo);
367 inp->inp_vflag &= ~INP_IPV4;
368 inp->inp_vflag |= INP_IPV6;
369 #ifdef INET
370 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
371 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
372 inp->inp_vflag |= INP_IPV4;
373 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
374 struct sockaddr_in sin;
375
376 in6_sin6_2_sin(&sin, sin6p);
377 inp->inp_vflag |= INP_IPV4;
378 inp->inp_vflag &= ~INP_IPV6;
379 error = in_pcbbind(inp, (struct sockaddr *)&sin,
380 td->td_ucred);
381 INP_HASH_WUNLOCK(&V_tcbinfo);
382 goto out;
383 }
384 }
385 #endif
386 error = in6_pcbbind(inp, nam, td->td_ucred);
387 INP_HASH_WUNLOCK(&V_tcbinfo);
388 out:
389 if (error != 0)
390 inp->inp_vflag = vflagsav;
391 TCPDEBUG2(PRU_BIND);
392 TCP_PROBE2(debug__user, tp, PRU_BIND);
393 INP_WUNLOCK(inp);
394 return (error);
395 }
396 #endif /* INET6 */
397
398 #ifdef INET
399 /*
400 * Prepare to accept connections.
401 */
402 static int
403 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
404 {
405 int error = 0;
406 struct inpcb *inp;
407 struct tcpcb *tp = NULL;
408
409 TCPDEBUG0;
410 inp = sotoinpcb(so);
411 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
412 INP_WLOCK(inp);
413 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
414 error = EINVAL;
415 goto out;
416 }
417 tp = intotcpcb(inp);
418 TCPDEBUG1();
419 SOCK_LOCK(so);
420 error = solisten_proto_check(so);
421 INP_HASH_WLOCK(&V_tcbinfo);
422 if (error == 0 && inp->inp_lport == 0)
423 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
424 INP_HASH_WUNLOCK(&V_tcbinfo);
425 if (error == 0) {
426 tcp_state_change(tp, TCPS_LISTEN);
427 solisten_proto(so, backlog);
428 #ifdef TCP_OFFLOAD
429 if ((so->so_options & SO_NO_OFFLOAD) == 0)
430 tcp_offload_listen_start(tp);
431 #endif
432 }
433 SOCK_UNLOCK(so);
434
435 #ifdef TCP_RFC7413
436 if (tp->t_flags & TF_FASTOPEN)
437 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
438 #endif
439 out:
440 TCPDEBUG2(PRU_LISTEN);
441 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
442 INP_WUNLOCK(inp);
443 return (error);
444 }
445 #endif /* INET */
446
447 #ifdef INET6
448 static int
449 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
450 {
451 int error = 0;
452 struct inpcb *inp;
453 struct tcpcb *tp = NULL;
454 u_char vflagsav;
455
456 TCPDEBUG0;
457 inp = sotoinpcb(so);
458 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
459 INP_WLOCK(inp);
460 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
461 error = EINVAL;
462 goto out;
463 }
464 vflagsav = inp->inp_vflag;
465 tp = intotcpcb(inp);
466 TCPDEBUG1();
467 SOCK_LOCK(so);
468 error = solisten_proto_check(so);
469 INP_HASH_WLOCK(&V_tcbinfo);
470 if (error == 0 && inp->inp_lport == 0) {
471 inp->inp_vflag &= ~INP_IPV4;
472 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
473 inp->inp_vflag |= INP_IPV4;
474 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
475 }
476 INP_HASH_WUNLOCK(&V_tcbinfo);
477 if (error == 0) {
478 tcp_state_change(tp, TCPS_LISTEN);
479 solisten_proto(so, backlog);
480 #ifdef TCP_OFFLOAD
481 if ((so->so_options & SO_NO_OFFLOAD) == 0)
482 tcp_offload_listen_start(tp);
483 #endif
484 }
485 SOCK_UNLOCK(so);
486
487 #ifdef TCP_RFC7413
488 if (tp->t_flags & TF_FASTOPEN)
489 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
490 #endif
491 if (error != 0)
492 inp->inp_vflag = vflagsav;
493
494 out:
495 TCPDEBUG2(PRU_LISTEN);
496 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
497 INP_WUNLOCK(inp);
498 return (error);
499 }
500 #endif /* INET6 */
501
502 #ifdef INET
503 /*
504 * Initiate connection to peer.
505 * Create a template for use in transmissions on this connection.
506 * Enter SYN_SENT state, and mark socket as connecting.
507 * Start keep-alive timer, and seed output sequence space.
508 * Send initial segment on connection.
509 */
510 static int
511 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
512 {
513 int error = 0;
514 struct inpcb *inp;
515 struct tcpcb *tp = NULL;
516 struct sockaddr_in *sinp;
517
518 sinp = (struct sockaddr_in *)nam;
519 if (nam->sa_len != sizeof (*sinp))
520 return (EINVAL);
521 /*
522 * Must disallow TCP ``connections'' to multicast addresses.
523 */
524 if (sinp->sin_family == AF_INET
525 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
526 return (EAFNOSUPPORT);
527 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
528 return (error);
529
530 TCPDEBUG0;
531 inp = sotoinpcb(so);
532 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
533 INP_WLOCK(inp);
534 if (inp->inp_flags & INP_TIMEWAIT) {
535 error = EADDRINUSE;
536 goto out;
537 }
538 if (inp->inp_flags & INP_DROPPED) {
539 error = ECONNREFUSED;
540 goto out;
541 }
542 tp = intotcpcb(inp);
543 TCPDEBUG1();
544 if ((error = tcp_connect(tp, nam, td)) != 0)
545 goto out;
546 #ifdef TCP_OFFLOAD
547 if (registered_toedevs > 0 &&
548 (so->so_options & SO_NO_OFFLOAD) == 0 &&
549 (error = tcp_offload_connect(so, nam)) == 0)
550 goto out;
551 #endif
552 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
553 error = tp->t_fb->tfb_tcp_output(tp);
554 out:
555 TCPDEBUG2(PRU_CONNECT);
556 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
557 INP_WUNLOCK(inp);
558 return (error);
559 }
560 #endif /* INET */
561
562 #ifdef INET6
563 static int
564 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
565 {
566 int error = 0;
567 struct inpcb *inp;
568 struct tcpcb *tp = NULL;
569 struct sockaddr_in6 *sin6p;
570 u_int8_t incflagsav;
571 u_char vflagsav;
572
573 TCPDEBUG0;
574
575 sin6p = (struct sockaddr_in6 *)nam;
576 if (nam->sa_len != sizeof (*sin6p))
577 return (EINVAL);
578 /*
579 * Must disallow TCP ``connections'' to multicast addresses.
580 */
581 if (sin6p->sin6_family == AF_INET6
582 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
583 return (EAFNOSUPPORT);
584
585 inp = sotoinpcb(so);
586 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
587 INP_WLOCK(inp);
588 vflagsav = inp->inp_vflag;
589 incflagsav = inp->inp_inc.inc_flags;
590 if (inp->inp_flags & INP_TIMEWAIT) {
591 error = EADDRINUSE;
592 goto out;
593 }
594 if (inp->inp_flags & INP_DROPPED) {
595 error = ECONNREFUSED;
596 goto out;
597 }
598 tp = intotcpcb(inp);
599 TCPDEBUG1();
600 #ifdef INET
601 /*
602 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
603 * therefore probably require the hash lock, which isn't held here.
604 * Is this a significant problem?
605 */
606 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
607 struct sockaddr_in sin;
608
609 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
610 error = EINVAL;
611 goto out;
612 }
613 if ((inp->inp_vflag & INP_IPV4) == 0) {
614 error = EAFNOSUPPORT;
615 goto out;
616 }
617
618 in6_sin6_2_sin(&sin, sin6p);
619 if ((error = prison_remote_ip4(td->td_ucred,
620 &sin.sin_addr)) != 0)
621 goto out;
622 inp->inp_vflag |= INP_IPV4;
623 inp->inp_vflag &= ~INP_IPV6;
624 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
625 goto out;
626 #ifdef TCP_OFFLOAD
627 if (registered_toedevs > 0 &&
628 (so->so_options & SO_NO_OFFLOAD) == 0 &&
629 (error = tcp_offload_connect(so, nam)) == 0)
630 goto out;
631 #endif
632 error = tp->t_fb->tfb_tcp_output(tp);
633 goto out;
634 } else {
635 if ((inp->inp_vflag & INP_IPV6) == 0) {
636 error = EAFNOSUPPORT;
637 goto out;
638 }
639 }
640 #endif
641 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
642 goto out;
643 inp->inp_vflag &= ~INP_IPV4;
644 inp->inp_vflag |= INP_IPV6;
645 inp->inp_inc.inc_flags |= INC_ISIPV6;
646 if ((error = tcp6_connect(tp, nam, td)) != 0)
647 goto out;
648 #ifdef TCP_OFFLOAD
649 if (registered_toedevs > 0 &&
650 (so->so_options & SO_NO_OFFLOAD) == 0 &&
651 (error = tcp_offload_connect(so, nam)) == 0)
652 goto out;
653 #endif
654 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
655 error = tp->t_fb->tfb_tcp_output(tp);
656
657 out:
658 /*
659 * If the implicit bind in the connect call fails, restore
660 * the flags we modified.
661 */
662 if (error != 0 && inp->inp_lport == 0) {
663 inp->inp_vflag = vflagsav;
664 inp->inp_inc.inc_flags = incflagsav;
665 }
666
667 TCPDEBUG2(PRU_CONNECT);
668 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
669 INP_WUNLOCK(inp);
670 return (error);
671 }
672 #endif /* INET6 */
673
674 /*
675 * Initiate disconnect from peer.
676 * If connection never passed embryonic stage, just drop;
677 * else if don't need to let data drain, then can just drop anyways,
678 * else have to begin TCP shutdown process: mark socket disconnecting,
679 * drain unread data, state switch to reflect user close, and
680 * send segment (e.g. FIN) to peer. Socket will be really disconnected
681 * when peer sends FIN and acks ours.
682 *
683 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
684 */
685 static int
686 tcp_usr_disconnect(struct socket *so)
687 {
688 struct inpcb *inp;
689 struct tcpcb *tp = NULL;
690 int error = 0;
691
692 TCPDEBUG0;
693 INP_INFO_RLOCK(&V_tcbinfo);
694 inp = sotoinpcb(so);
695 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
696 INP_WLOCK(inp);
697 if (inp->inp_flags & INP_TIMEWAIT)
698 goto out;
699 if (inp->inp_flags & INP_DROPPED) {
700 error = ECONNRESET;
701 goto out;
702 }
703 tp = intotcpcb(inp);
704 TCPDEBUG1();
705 tcp_disconnect(tp);
706 out:
707 TCPDEBUG2(PRU_DISCONNECT);
708 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
709 INP_WUNLOCK(inp);
710 INP_INFO_RUNLOCK(&V_tcbinfo);
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
720 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
721 {
722 int error = 0;
723 struct inpcb *inp = NULL;
724 struct tcpcb *tp = NULL;
725 struct in_addr addr;
726 in_port_t port = 0;
727 TCPDEBUG0;
728
729 if (so->so_state & SS_ISDISCONNECTED)
730 return (ECONNABORTED);
731
732 inp = sotoinpcb(so);
733 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
734 INP_WLOCK(inp);
735 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
736 error = ECONNABORTED;
737 goto out;
738 }
739 tp = intotcpcb(inp);
740 TCPDEBUG1();
741
742 /*
743 * We inline in_getpeeraddr and COMMON_END here, so that we can
744 * copy the data of interest and defer the malloc until after we
745 * release the lock.
746 */
747 port = inp->inp_fport;
748 addr = inp->inp_faddr;
749
750 out:
751 TCPDEBUG2(PRU_ACCEPT);
752 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
753 INP_WUNLOCK(inp);
754 if (error == 0)
755 *nam = in_sockaddr(port, &addr);
756 return error;
757 }
758 #endif /* INET */
759
760 #ifdef INET6
761 static int
762 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
763 {
764 struct inpcb *inp = NULL;
765 int error = 0;
766 struct tcpcb *tp = NULL;
767 struct in_addr addr;
768 struct in6_addr addr6;
769 in_port_t port = 0;
770 int v4 = 0;
771 TCPDEBUG0;
772
773 if (so->so_state & SS_ISDISCONNECTED)
774 return (ECONNABORTED);
775
776 inp = sotoinpcb(so);
777 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
778 INP_INFO_RLOCK(&V_tcbinfo);
779 INP_WLOCK(inp);
780 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
781 error = ECONNABORTED;
782 goto out;
783 }
784 tp = intotcpcb(inp);
785 TCPDEBUG1();
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 TCPDEBUG2(PRU_ACCEPT);
803 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
804 INP_WUNLOCK(inp);
805 INP_INFO_RUNLOCK(&V_tcbinfo);
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
820 tcp_usr_shutdown(struct socket *so)
821 {
822 int error = 0;
823 struct inpcb *inp;
824 struct tcpcb *tp = NULL;
825
826 TCPDEBUG0;
827 INP_INFO_RLOCK(&V_tcbinfo);
828 inp = sotoinpcb(so);
829 KASSERT(inp != NULL, ("inp == NULL"));
830 INP_WLOCK(inp);
831 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
832 error = ECONNRESET;
833 goto out;
834 }
835 tp = intotcpcb(inp);
836 TCPDEBUG1();
837 socantsendmore(so);
838 tcp_usrclosed(tp);
839 if (!(inp->inp_flags & INP_DROPPED))
840 error = tp->t_fb->tfb_tcp_output(tp);
841
842 out:
843 TCPDEBUG2(PRU_SHUTDOWN);
844 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
845 INP_WUNLOCK(inp);
846 INP_INFO_RUNLOCK(&V_tcbinfo);
847
848 return (error);
849 }
850
851 /*
852 * After a receive, possibly send window update to peer.
853 */
854 static int
855 tcp_usr_rcvd(struct socket *so, int flags)
856 {
857 struct inpcb *inp;
858 struct tcpcb *tp = NULL;
859 int error = 0;
860
861 TCPDEBUG0;
862 inp = sotoinpcb(so);
863 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
864 INP_WLOCK(inp);
865 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
866 error = ECONNRESET;
867 goto out;
868 }
869 tp = intotcpcb(inp);
870 TCPDEBUG1();
871 #ifdef TCP_RFC7413
872 /*
873 * For passively-created TFO connections, don't attempt a window
874 * update while still in SYN_RECEIVED as this may trigger an early
875 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
876 * application response data, or failing that, when the DELACK timer
877 * expires.
878 */
879 if ((tp->t_flags & TF_FASTOPEN) &&
880 (tp->t_state == TCPS_SYN_RECEIVED))
881 goto out;
882 #endif
883 #ifdef TCP_OFFLOAD
884 if (tp->t_flags & TF_TOE)
885 tcp_offload_rcvd(tp);
886 else
887 #endif
888 tp->t_fb->tfb_tcp_output(tp);
889
890 out:
891 TCPDEBUG2(PRU_RCVD);
892 TCP_PROBE2(debug__user, tp, PRU_RCVD);
893 INP_WUNLOCK(inp);
894 return (error);
895 }
896
897 /*
898 * Do a send by putting data in output queue and updating urgent
899 * marker if URG set. Possibly send more data. Unlike the other
900 * pru_*() routines, the mbuf chains are our responsibility. We
901 * must either enqueue them or free them. The other pru_* routines
902 * generally are caller-frees.
903 */
904 static int
905 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
906 struct sockaddr *nam, struct mbuf *control, struct thread *td)
907 {
908 int error = 0;
909 struct inpcb *inp;
910 struct tcpcb *tp = NULL;
911 #ifdef INET6
912 int isipv6;
913 #endif
914 TCPDEBUG0;
915
916 /*
917 * We require the pcbinfo lock if we will close the socket as part of
918 * this call.
919 */
920 if (flags & PRUS_EOF)
921 INP_INFO_RLOCK(&V_tcbinfo);
922 inp = sotoinpcb(so);
923 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
924 INP_WLOCK(inp);
925 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
926 if (control)
927 m_freem(control);
928 /*
929 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
930 * for freeing memory.
931 */
932 if (m && (flags & PRUS_NOTREADY) == 0)
933 m_freem(m);
934 error = ECONNRESET;
935 goto out;
936 }
937 #ifdef INET6
938 isipv6 = nam && nam->sa_family == AF_INET6;
939 #endif /* INET6 */
940 tp = intotcpcb(inp);
941 TCPDEBUG1();
942 if (control) {
943 /* TCP doesn't do control messages (rights, creds, etc) */
944 if (control->m_len) {
945 m_freem(control);
946 if (m)
947 m_freem(m);
948 error = EINVAL;
949 goto out;
950 }
951 m_freem(control); /* empty control, just free it */
952 }
953 if (!(flags & PRUS_OOB)) {
954 sbappendstream(&so->so_snd, m, flags);
955 if (nam && tp->t_state < TCPS_SYN_SENT) {
956 /*
957 * Do implied connect if not yet connected,
958 * initialize window to default value, and
959 * initialize maxseg using peer's cached MSS.
960 */
961 #ifdef INET6
962 if (isipv6)
963 error = tcp6_connect(tp, nam, td);
964 #endif /* INET6 */
965 #if defined(INET6) && defined(INET)
966 else
967 #endif
968 #ifdef INET
969 error = tcp_connect(tp, nam, td);
970 #endif
971 if (error)
972 goto out;
973 tp->snd_wnd = TTCP_CLIENT_SND_WND;
974 tcp_mss(tp, -1);
975 }
976 if (flags & PRUS_EOF) {
977 /*
978 * Close the send side of the connection after
979 * the data is sent.
980 */
981 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
982 socantsendmore(so);
983 tcp_usrclosed(tp);
984 }
985 if (!(inp->inp_flags & INP_DROPPED) &&
986 !(flags & PRUS_NOTREADY)) {
987 if (flags & PRUS_MORETOCOME)
988 tp->t_flags |= TF_MORETOCOME;
989 error = tp->t_fb->tfb_tcp_output(tp);
990 if (flags & PRUS_MORETOCOME)
991 tp->t_flags &= ~TF_MORETOCOME;
992 }
993 } else {
994 /*
995 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
996 */
997 SOCKBUF_LOCK(&so->so_snd);
998 if (sbspace(&so->so_snd) < -512) {
999 SOCKBUF_UNLOCK(&so->so_snd);
1000 m_freem(m);
1001 error = ENOBUFS;
1002 goto out;
1003 }
1004 /*
1005 * According to RFC961 (Assigned Protocols),
1006 * the urgent pointer points to the last octet
1007 * of urgent data. We continue, however,
1008 * to consider it to indicate the first octet
1009 * of data past the urgent section.
1010 * Otherwise, snd_up should be one lower.
1011 */
1012 sbappendstream_locked(&so->so_snd, m, flags);
1013 SOCKBUF_UNLOCK(&so->so_snd);
1014 if (nam && tp->t_state < TCPS_SYN_SENT) {
1015 /*
1016 * Do implied connect if not yet connected,
1017 * initialize window to default value, and
1018 * initialize maxseg using peer's cached MSS.
1019 */
1020 #ifdef INET6
1021 if (isipv6)
1022 error = tcp6_connect(tp, nam, td);
1023 #endif /* INET6 */
1024 #if defined(INET6) && defined(INET)
1025 else
1026 #endif
1027 #ifdef INET
1028 error = tcp_connect(tp, nam, td);
1029 #endif
1030 if (error)
1031 goto out;
1032 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1033 tcp_mss(tp, -1);
1034 }
1035 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1036 if (!(flags & PRUS_NOTREADY)) {
1037 tp->t_flags |= TF_FORCEDATA;
1038 error = tp->t_fb->tfb_tcp_output(tp);
1039 tp->t_flags &= ~TF_FORCEDATA;
1040 }
1041 }
1042 out:
1043 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1044 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1045 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1046 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1047 INP_WUNLOCK(inp);
1048 if (flags & PRUS_EOF)
1049 INP_INFO_RUNLOCK(&V_tcbinfo);
1050 return (error);
1051 }
1052
1053 static int
1054 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1055 {
1056 struct inpcb *inp;
1057 struct tcpcb *tp;
1058 int error;
1059
1060 inp = sotoinpcb(so);
1061 INP_WLOCK(inp);
1062 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1063 INP_WUNLOCK(inp);
1064 for (int i = 0; i < count; i++)
1065 m = m_free(m);
1066 return (ECONNRESET);
1067 }
1068 tp = intotcpcb(inp);
1069
1070 SOCKBUF_LOCK(&so->so_snd);
1071 error = sbready(&so->so_snd, m, count);
1072 SOCKBUF_UNLOCK(&so->so_snd);
1073 if (error == 0)
1074 error = tp->t_fb->tfb_tcp_output(tp);
1075 INP_WUNLOCK(inp);
1076
1077 return (error);
1078 }
1079
1080 /*
1081 * Abort the TCP. Drop the connection abruptly.
1082 */
1083 static void
1084 tcp_usr_abort(struct socket *so)
1085 {
1086 struct inpcb *inp;
1087 struct tcpcb *tp = NULL;
1088 TCPDEBUG0;
1089
1090 inp = sotoinpcb(so);
1091 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1092
1093 INP_INFO_RLOCK(&V_tcbinfo);
1094 INP_WLOCK(inp);
1095 KASSERT(inp->inp_socket != NULL,
1096 ("tcp_usr_abort: inp_socket == NULL"));
1097
1098 /*
1099 * If we still have full TCP state, and we're not dropped, drop.
1100 */
1101 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1102 !(inp->inp_flags & INP_DROPPED)) {
1103 tp = intotcpcb(inp);
1104 TCPDEBUG1();
1105 tp = tcp_drop(tp, ECONNABORTED);
1106 if (tp == NULL)
1107 goto dropped;
1108 TCPDEBUG2(PRU_ABORT);
1109 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1110 }
1111 if (!(inp->inp_flags & INP_DROPPED)) {
1112 SOCK_LOCK(so);
1113 so->so_state |= SS_PROTOREF;
1114 SOCK_UNLOCK(so);
1115 inp->inp_flags |= INP_SOCKREF;
1116 }
1117 INP_WUNLOCK(inp);
1118 dropped:
1119 INP_INFO_RUNLOCK(&V_tcbinfo);
1120 }
1121
1122 /*
1123 * TCP socket is closed. Start friendly disconnect.
1124 */
1125 static void
1126 tcp_usr_close(struct socket *so)
1127 {
1128 struct inpcb *inp;
1129 struct tcpcb *tp = NULL;
1130 TCPDEBUG0;
1131
1132 inp = sotoinpcb(so);
1133 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1134
1135 INP_INFO_RLOCK(&V_tcbinfo);
1136 INP_WLOCK(inp);
1137 KASSERT(inp->inp_socket != NULL,
1138 ("tcp_usr_close: inp_socket == NULL"));
1139
1140 /*
1141 * If we still have full TCP state, and we're not dropped, initiate
1142 * a disconnect.
1143 */
1144 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1145 !(inp->inp_flags & INP_DROPPED)) {
1146 tp = intotcpcb(inp);
1147 TCPDEBUG1();
1148 tcp_disconnect(tp);
1149 TCPDEBUG2(PRU_CLOSE);
1150 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1151 }
1152 if (!(inp->inp_flags & INP_DROPPED)) {
1153 SOCK_LOCK(so);
1154 so->so_state |= SS_PROTOREF;
1155 SOCK_UNLOCK(so);
1156 inp->inp_flags |= INP_SOCKREF;
1157 }
1158 INP_WUNLOCK(inp);
1159 INP_INFO_RUNLOCK(&V_tcbinfo);
1160 }
1161
1162 /*
1163 * Receive out-of-band data.
1164 */
1165 static int
1166 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1167 {
1168 int error = 0;
1169 struct inpcb *inp;
1170 struct tcpcb *tp = NULL;
1171
1172 TCPDEBUG0;
1173 inp = sotoinpcb(so);
1174 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1175 INP_WLOCK(inp);
1176 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1177 error = ECONNRESET;
1178 goto out;
1179 }
1180 tp = intotcpcb(inp);
1181 TCPDEBUG1();
1182 if ((so->so_oobmark == 0 &&
1183 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1184 so->so_options & SO_OOBINLINE ||
1185 tp->t_oobflags & TCPOOB_HADDATA) {
1186 error = EINVAL;
1187 goto out;
1188 }
1189 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1190 error = EWOULDBLOCK;
1191 goto out;
1192 }
1193 m->m_len = 1;
1194 *mtod(m, caddr_t) = tp->t_iobc;
1195 if ((flags & MSG_PEEK) == 0)
1196 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1197
1198 out:
1199 TCPDEBUG2(PRU_RCVOOB);
1200 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1201 INP_WUNLOCK(inp);
1202 return (error);
1203 }
1204
1205 #ifdef INET
1206 struct pr_usrreqs tcp_usrreqs = {
1207 .pru_abort = tcp_usr_abort,
1208 .pru_accept = tcp_usr_accept,
1209 .pru_attach = tcp_usr_attach,
1210 .pru_bind = tcp_usr_bind,
1211 .pru_connect = tcp_usr_connect,
1212 .pru_control = in_control,
1213 .pru_detach = tcp_usr_detach,
1214 .pru_disconnect = tcp_usr_disconnect,
1215 .pru_listen = tcp_usr_listen,
1216 .pru_peeraddr = in_getpeeraddr,
1217 .pru_rcvd = tcp_usr_rcvd,
1218 .pru_rcvoob = tcp_usr_rcvoob,
1219 .pru_send = tcp_usr_send,
1220 .pru_ready = tcp_usr_ready,
1221 .pru_shutdown = tcp_usr_shutdown,
1222 .pru_sockaddr = in_getsockaddr,
1223 .pru_sosetlabel = in_pcbsosetlabel,
1224 .pru_close = tcp_usr_close,
1225 };
1226 #endif /* INET */
1227
1228 #ifdef INET6
1229 struct pr_usrreqs tcp6_usrreqs = {
1230 .pru_abort = tcp_usr_abort,
1231 .pru_accept = tcp6_usr_accept,
1232 .pru_attach = tcp_usr_attach,
1233 .pru_bind = tcp6_usr_bind,
1234 .pru_connect = tcp6_usr_connect,
1235 .pru_control = in6_control,
1236 .pru_detach = tcp_usr_detach,
1237 .pru_disconnect = tcp_usr_disconnect,
1238 .pru_listen = tcp6_usr_listen,
1239 .pru_peeraddr = in6_mapped_peeraddr,
1240 .pru_rcvd = tcp_usr_rcvd,
1241 .pru_rcvoob = tcp_usr_rcvoob,
1242 .pru_send = tcp_usr_send,
1243 .pru_ready = tcp_usr_ready,
1244 .pru_shutdown = tcp_usr_shutdown,
1245 .pru_sockaddr = in6_mapped_sockaddr,
1246 .pru_sosetlabel = in_pcbsosetlabel,
1247 .pru_close = tcp_usr_close,
1248 };
1249 #endif /* INET6 */
1250
1251 #ifdef INET
1252 /*
1253 * Common subroutine to open a TCP connection to remote host specified
1254 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1255 * port number if needed. Call in_pcbconnect_setup to do the routing and
1256 * to choose a local host address (interface). If there is an existing
1257 * incarnation of the same connection in TIME-WAIT state and if the remote
1258 * host was sending CC options and if the connection duration was < MSL, then
1259 * truncate the previous TIME-WAIT state and proceed.
1260 * Initialize connection parameters and enter SYN-SENT state.
1261 */
1262 static int
1263 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1264 {
1265 struct inpcb *inp = tp->t_inpcb, *oinp;
1266 struct socket *so = inp->inp_socket;
1267 struct in_addr laddr;
1268 u_short lport;
1269 int error;
1270
1271 INP_WLOCK_ASSERT(inp);
1272 INP_HASH_WLOCK(&V_tcbinfo);
1273
1274 if (inp->inp_lport == 0) {
1275 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1276 if (error)
1277 goto out;
1278 }
1279
1280 /*
1281 * Cannot simply call in_pcbconnect, because there might be an
1282 * earlier incarnation of this same connection still in
1283 * TIME_WAIT state, creating an ADDRINUSE error.
1284 */
1285 laddr = inp->inp_laddr;
1286 lport = inp->inp_lport;
1287 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1288 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1289 if (error && oinp == NULL)
1290 goto out;
1291 if (oinp) {
1292 error = EADDRINUSE;
1293 goto out;
1294 }
1295 inp->inp_laddr = laddr;
1296 in_pcbrehash(inp);
1297 INP_HASH_WUNLOCK(&V_tcbinfo);
1298
1299 /*
1300 * Compute window scaling to request:
1301 * Scale to fit into sweet spot. See tcp_syncache.c.
1302 * XXX: This should move to tcp_output().
1303 */
1304 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1305 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1306 tp->request_r_scale++;
1307
1308 soisconnecting(so);
1309 TCPSTAT_INC(tcps_connattempt);
1310 tcp_state_change(tp, TCPS_SYN_SENT);
1311 tp->iss = tcp_new_isn(&inp->inp_inc);
1312 if (tp->t_flags & TF_REQ_TSTMP)
1313 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1314 tcp_sendseqinit(tp);
1315
1316 return 0;
1317
1318 out:
1319 INP_HASH_WUNLOCK(&V_tcbinfo);
1320 return (error);
1321 }
1322 #endif /* INET */
1323
1324 #ifdef INET6
1325 static int
1326 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1327 {
1328 struct inpcb *inp = tp->t_inpcb;
1329 int error;
1330
1331 INP_WLOCK_ASSERT(inp);
1332 INP_HASH_WLOCK(&V_tcbinfo);
1333
1334 if (inp->inp_lport == 0) {
1335 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1336 if (error)
1337 goto out;
1338 }
1339 error = in6_pcbconnect(inp, nam, td->td_ucred);
1340 if (error != 0)
1341 goto out;
1342 INP_HASH_WUNLOCK(&V_tcbinfo);
1343
1344 /* Compute window scaling to request. */
1345 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1346 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1347 tp->request_r_scale++;
1348
1349 soisconnecting(inp->inp_socket);
1350 TCPSTAT_INC(tcps_connattempt);
1351 tcp_state_change(tp, TCPS_SYN_SENT);
1352 tp->iss = tcp_new_isn(&inp->inp_inc);
1353 if (tp->t_flags & TF_REQ_TSTMP)
1354 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1355 tcp_sendseqinit(tp);
1356
1357 return 0;
1358
1359 out:
1360 INP_HASH_WUNLOCK(&V_tcbinfo);
1361 return error;
1362 }
1363 #endif /* INET6 */
1364
1365 /*
1366 * Export TCP internal state information via a struct tcp_info, based on the
1367 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1368 * (TCP state machine, etc). We export all information using FreeBSD-native
1369 * constants -- for example, the numeric values for tcpi_state will differ
1370 * from Linux.
1371 */
1372 static void
1373 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1374 {
1375
1376 INP_WLOCK_ASSERT(tp->t_inpcb);
1377 bzero(ti, sizeof(*ti));
1378
1379 ti->tcpi_state = tp->t_state;
1380 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1381 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1382 if (tp->t_flags & TF_SACK_PERMIT)
1383 ti->tcpi_options |= TCPI_OPT_SACK;
1384 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1385 ti->tcpi_options |= TCPI_OPT_WSCALE;
1386 ti->tcpi_snd_wscale = tp->snd_scale;
1387 ti->tcpi_rcv_wscale = tp->rcv_scale;
1388 }
1389 if (tp->t_flags & TF_ECN_PERMIT)
1390 ti->tcpi_options |= TCPI_OPT_ECN;
1391
1392 ti->tcpi_rto = tp->t_rxtcur * tick;
1393 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1394 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1395 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1396
1397 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1398 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1399
1400 /*
1401 * FreeBSD-specific extension fields for tcp_info.
1402 */
1403 ti->tcpi_rcv_space = tp->rcv_wnd;
1404 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1405 ti->tcpi_snd_wnd = tp->snd_wnd;
1406 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1407 ti->tcpi_snd_nxt = tp->snd_nxt;
1408 ti->tcpi_snd_mss = tp->t_maxseg;
1409 ti->tcpi_rcv_mss = tp->t_maxseg;
1410 if (tp->t_flags & TF_TOE)
1411 ti->tcpi_options |= TCPI_OPT_TOE;
1412 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1413 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1414 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1415 }
1416
1417 /*
1418 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1419 * socket option arguments. When it re-acquires the lock after the copy, it
1420 * has to revalidate that the connection is still valid for the socket
1421 * option.
1422 */
1423 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1424 INP_WLOCK(inp); \
1425 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1426 INP_WUNLOCK(inp); \
1427 cleanup; \
1428 return (ECONNRESET); \
1429 } \
1430 tp = intotcpcb(inp); \
1431 } while(0)
1432 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1433
1434 int
1435 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1436 {
1437 int error;
1438 struct inpcb *inp;
1439 struct tcpcb *tp;
1440 struct tcp_function_block *blk;
1441 struct tcp_function_set fsn;
1442
1443 error = 0;
1444 inp = sotoinpcb(so);
1445 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1446 if (sopt->sopt_level != IPPROTO_TCP) {
1447 #ifdef INET6
1448 if (inp->inp_vflag & INP_IPV6PROTO) {
1449 error = ip6_ctloutput(so, sopt);
1450 /*
1451 * In case of the IPV6_USE_MIN_MTU socket option,
1452 * the INC_IPV6MINMTU flag to announce a corresponding
1453 * MSS during the initial handshake.
1454 * If the TCP connection is not in the front states,
1455 * just reduce the MSS being used.
1456 * This avoids the sending of TCP segments which will
1457 * be fragmented at the IPv6 layer.
1458 */
1459 if ((error == 0) &&
1460 (sopt->sopt_dir == SOPT_SET) &&
1461 (sopt->sopt_level == IPPROTO_IPV6) &&
1462 (sopt->sopt_name == IPV6_USE_MIN_MTU)) {
1463 INP_WLOCK(inp);
1464 if ((inp->inp_flags &
1465 (INP_TIMEWAIT | INP_DROPPED))) {
1466 INP_WUNLOCK(inp);
1467 return (ECONNRESET);
1468 }
1469 inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
1470 tp = intotcpcb(inp);
1471 if ((tp->t_state >= TCPS_SYN_SENT) &&
1472 (inp->inp_inc.inc_flags & INC_ISIPV6)) {
1473 struct ip6_pktopts *opt;
1474
1475 opt = inp->in6p_outputopts;
1476 if ((opt != NULL) &&
1477 (opt->ip6po_minmtu ==
1478 IP6PO_MINMTU_ALL)) {
1479 if (tp->t_maxseg > TCP6_MSS) {
1480 tp->t_maxseg = TCP6_MSS;
1481 }
1482 }
1483 }
1484 INP_WUNLOCK(inp);
1485 }
1486 }
1487 #endif /* INET6 */
1488 #if defined(INET6) && defined(INET)
1489 else
1490 #endif
1491 #ifdef INET
1492 {
1493 error = ip_ctloutput(so, sopt);
1494 }
1495 #endif
1496 return (error);
1497 }
1498 INP_WLOCK(inp);
1499 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1500 INP_WUNLOCK(inp);
1501 return (ECONNRESET);
1502 }
1503 tp = intotcpcb(inp);
1504 /*
1505 * Protect the TCP option TCP_FUNCTION_BLK so
1506 * that a sub-function can *never* overwrite this.
1507 */
1508 if ((sopt->sopt_dir == SOPT_SET) &&
1509 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1510 INP_WUNLOCK(inp);
1511 error = sooptcopyin(sopt, &fsn, sizeof fsn,
1512 sizeof fsn);
1513 if (error)
1514 return (error);
1515 INP_WLOCK_RECHECK(inp);
1516 if (tp->t_state != TCPS_CLOSED) {
1517 /*
1518 * The user has advanced the state
1519 * past the initial point, we can't
1520 * switch since we are down the road
1521 * and a new set of functions may
1522 * not be compatibile.
1523 */
1524 INP_WUNLOCK(inp);
1525 return(EINVAL);
1526 }
1527 blk = find_and_ref_tcp_functions(&fsn);
1528 if (blk == NULL) {
1529 INP_WUNLOCK(inp);
1530 return (ENOENT);
1531 }
1532 if (tp->t_fb != blk) {
1533 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1534 refcount_release(&blk->tfb_refcnt);
1535 INP_WUNLOCK(inp);
1536 return (ENOENT);
1537 }
1538 /*
1539 * Release the old refcnt, the
1540 * lookup acquires a ref on the
1541 * new one.
1542 */
1543 if (tp->t_fb->tfb_tcp_fb_fini)
1544 (*tp->t_fb->tfb_tcp_fb_fini)(tp);
1545 refcount_release(&tp->t_fb->tfb_refcnt);
1546 tp->t_fb = blk;
1547 if (tp->t_fb->tfb_tcp_fb_init) {
1548 (*tp->t_fb->tfb_tcp_fb_init)(tp);
1549 }
1550 }
1551 #ifdef TCP_OFFLOAD
1552 if (tp->t_flags & TF_TOE) {
1553 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1554 sopt->sopt_name);
1555 }
1556 #endif
1557 INP_WUNLOCK(inp);
1558 return (error);
1559 } else if ((sopt->sopt_dir == SOPT_GET) &&
1560 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1561 strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1562 TCP_FUNCTION_NAME_LEN_MAX);
1563 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1564 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1565 INP_WUNLOCK(inp);
1566 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1567 return (error);
1568 }
1569 /* Pass in the INP locked, called must unlock it */
1570 return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1571 }
1572
1573 int
1574 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1575 {
1576 int error, opt, optval;
1577 u_int ui;
1578 struct tcp_info ti;
1579 struct cc_algo *algo;
1580 char *pbuf, buf[TCP_CA_NAME_MAX];
1581 size_t len;
1582
1583 /*
1584 * For TCP_CCALGOOPT forward the control to CC module, for both
1585 * SOPT_SET and SOPT_GET.
1586 */
1587 switch (sopt->sopt_name) {
1588 case TCP_CCALGOOPT:
1589 INP_WUNLOCK(inp);
1590 if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
1591 return (EINVAL);
1592 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1593 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1594 sopt->sopt_valsize);
1595 if (error) {
1596 free(pbuf, M_TEMP);
1597 return (error);
1598 }
1599 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1600 if (CC_ALGO(tp)->ctl_output != NULL)
1601 error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1602 else
1603 error = ENOENT;
1604 INP_WUNLOCK(inp);
1605 if (error == 0 && sopt->sopt_dir == SOPT_GET)
1606 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1607 free(pbuf, M_TEMP);
1608 return (error);
1609 }
1610
1611 switch (sopt->sopt_dir) {
1612 case SOPT_SET:
1613 switch (sopt->sopt_name) {
1614 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1615 case TCP_MD5SIG:
1616 if (!TCPMD5_ENABLED()) {
1617 INP_WUNLOCK(inp);
1618 return (ENOPROTOOPT);
1619 }
1620 error = TCPMD5_PCBCTL(inp, sopt);
1621 if (error)
1622 return (error);
1623 goto unlock_and_done;
1624 #endif /* IPSEC */
1625
1626 case TCP_NODELAY:
1627 case TCP_NOOPT:
1628 INP_WUNLOCK(inp);
1629 error = sooptcopyin(sopt, &optval, sizeof optval,
1630 sizeof optval);
1631 if (error)
1632 return (error);
1633
1634 INP_WLOCK_RECHECK(inp);
1635 switch (sopt->sopt_name) {
1636 case TCP_NODELAY:
1637 opt = TF_NODELAY;
1638 break;
1639 case TCP_NOOPT:
1640 opt = TF_NOOPT;
1641 break;
1642 default:
1643 opt = 0; /* dead code to fool gcc */
1644 break;
1645 }
1646
1647 if (optval)
1648 tp->t_flags |= opt;
1649 else
1650 tp->t_flags &= ~opt;
1651 unlock_and_done:
1652 #ifdef TCP_OFFLOAD
1653 if (tp->t_flags & TF_TOE) {
1654 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1655 sopt->sopt_name);
1656 }
1657 #endif
1658 INP_WUNLOCK(inp);
1659 break;
1660
1661 case TCP_NOPUSH:
1662 INP_WUNLOCK(inp);
1663 error = sooptcopyin(sopt, &optval, sizeof optval,
1664 sizeof optval);
1665 if (error)
1666 return (error);
1667
1668 INP_WLOCK_RECHECK(inp);
1669 if (optval)
1670 tp->t_flags |= TF_NOPUSH;
1671 else if (tp->t_flags & TF_NOPUSH) {
1672 tp->t_flags &= ~TF_NOPUSH;
1673 if (TCPS_HAVEESTABLISHED(tp->t_state))
1674 error = tp->t_fb->tfb_tcp_output(tp);
1675 }
1676 goto unlock_and_done;
1677
1678 case TCP_MAXSEG:
1679 INP_WUNLOCK(inp);
1680 error = sooptcopyin(sopt, &optval, sizeof optval,
1681 sizeof optval);
1682 if (error)
1683 return (error);
1684
1685 INP_WLOCK_RECHECK(inp);
1686 if (optval > 0 && optval <= tp->t_maxseg &&
1687 optval + 40 >= V_tcp_minmss)
1688 tp->t_maxseg = optval;
1689 else
1690 error = EINVAL;
1691 goto unlock_and_done;
1692
1693 case TCP_INFO:
1694 INP_WUNLOCK(inp);
1695 error = EINVAL;
1696 break;
1697
1698 case TCP_CONGESTION:
1699 INP_WUNLOCK(inp);
1700 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1701 if (error)
1702 break;
1703 buf[sopt->sopt_valsize] = '\0';
1704 INP_WLOCK_RECHECK(inp);
1705 CC_LIST_RLOCK();
1706 STAILQ_FOREACH(algo, &cc_list, entries)
1707 if (strncmp(buf, algo->name,
1708 TCP_CA_NAME_MAX) == 0)
1709 break;
1710 CC_LIST_RUNLOCK();
1711 if (algo == NULL) {
1712 INP_WUNLOCK(inp);
1713 error = EINVAL;
1714 break;
1715 }
1716 /*
1717 * We hold a write lock over the tcb so it's safe to
1718 * do these things without ordering concerns.
1719 */
1720 if (CC_ALGO(tp)->cb_destroy != NULL)
1721 CC_ALGO(tp)->cb_destroy(tp->ccv);
1722 CC_ALGO(tp) = algo;
1723 /*
1724 * If something goes pear shaped initialising the new
1725 * algo, fall back to newreno (which does not
1726 * require initialisation).
1727 */
1728 if (algo->cb_init != NULL &&
1729 algo->cb_init(tp->ccv) != 0) {
1730 CC_ALGO(tp) = &newreno_cc_algo;
1731 /*
1732 * The only reason init should fail is
1733 * because of malloc.
1734 */
1735 error = ENOMEM;
1736 }
1737 INP_WUNLOCK(inp);
1738 break;
1739
1740 case TCP_KEEPIDLE:
1741 case TCP_KEEPINTVL:
1742 case TCP_KEEPINIT:
1743 INP_WUNLOCK(inp);
1744 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1745 if (error)
1746 return (error);
1747
1748 if (ui > (UINT_MAX / hz)) {
1749 error = EINVAL;
1750 break;
1751 }
1752 ui *= hz;
1753
1754 INP_WLOCK_RECHECK(inp);
1755 switch (sopt->sopt_name) {
1756 case TCP_KEEPIDLE:
1757 tp->t_keepidle = ui;
1758 /*
1759 * XXX: better check current remaining
1760 * timeout and "merge" it with new value.
1761 */
1762 if ((tp->t_state > TCPS_LISTEN) &&
1763 (tp->t_state <= TCPS_CLOSING))
1764 tcp_timer_activate(tp, TT_KEEP,
1765 TP_KEEPIDLE(tp));
1766 break;
1767 case TCP_KEEPINTVL:
1768 tp->t_keepintvl = ui;
1769 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1770 (TP_MAXIDLE(tp) > 0))
1771 tcp_timer_activate(tp, TT_2MSL,
1772 TP_MAXIDLE(tp));
1773 break;
1774 case TCP_KEEPINIT:
1775 tp->t_keepinit = ui;
1776 if (tp->t_state == TCPS_SYN_RECEIVED ||
1777 tp->t_state == TCPS_SYN_SENT)
1778 tcp_timer_activate(tp, TT_KEEP,
1779 TP_KEEPINIT(tp));
1780 break;
1781 }
1782 goto unlock_and_done;
1783
1784 case TCP_KEEPCNT:
1785 INP_WUNLOCK(inp);
1786 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1787 if (error)
1788 return (error);
1789
1790 INP_WLOCK_RECHECK(inp);
1791 tp->t_keepcnt = ui;
1792 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1793 (TP_MAXIDLE(tp) > 0))
1794 tcp_timer_activate(tp, TT_2MSL,
1795 TP_MAXIDLE(tp));
1796 goto unlock_and_done;
1797
1798 #ifdef TCPPCAP
1799 case TCP_PCAP_OUT:
1800 case TCP_PCAP_IN:
1801 INP_WUNLOCK(inp);
1802 error = sooptcopyin(sopt, &optval, sizeof optval,
1803 sizeof optval);
1804 if (error)
1805 return (error);
1806
1807 INP_WLOCK_RECHECK(inp);
1808 if (optval >= 0)
1809 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
1810 &(tp->t_outpkts) : &(tp->t_inpkts),
1811 optval);
1812 else
1813 error = EINVAL;
1814 goto unlock_and_done;
1815 #endif
1816
1817 #ifdef TCP_RFC7413
1818 case TCP_FASTOPEN:
1819 INP_WUNLOCK(inp);
1820 if (!V_tcp_fastopen_enabled)
1821 return (EPERM);
1822
1823 error = sooptcopyin(sopt, &optval, sizeof optval,
1824 sizeof optval);
1825 if (error)
1826 return (error);
1827
1828 INP_WLOCK_RECHECK(inp);
1829 if (optval) {
1830 tp->t_flags |= TF_FASTOPEN;
1831 if ((tp->t_state == TCPS_LISTEN) &&
1832 (tp->t_tfo_pending == NULL))
1833 tp->t_tfo_pending =
1834 tcp_fastopen_alloc_counter();
1835 } else
1836 tp->t_flags &= ~TF_FASTOPEN;
1837 goto unlock_and_done;
1838 #endif
1839
1840 default:
1841 INP_WUNLOCK(inp);
1842 error = ENOPROTOOPT;
1843 break;
1844 }
1845 break;
1846
1847 case SOPT_GET:
1848 tp = intotcpcb(inp);
1849 switch (sopt->sopt_name) {
1850 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1851 case TCP_MD5SIG:
1852 if (!TCPMD5_ENABLED()) {
1853 INP_WUNLOCK(inp);
1854 return (ENOPROTOOPT);
1855 }
1856 error = TCPMD5_PCBCTL(inp, sopt);
1857 break;
1858 #endif
1859
1860 case TCP_NODELAY:
1861 optval = tp->t_flags & TF_NODELAY;
1862 INP_WUNLOCK(inp);
1863 error = sooptcopyout(sopt, &optval, sizeof optval);
1864 break;
1865 case TCP_MAXSEG:
1866 optval = tp->t_maxseg;
1867 INP_WUNLOCK(inp);
1868 error = sooptcopyout(sopt, &optval, sizeof optval);
1869 break;
1870 case TCP_NOOPT:
1871 optval = tp->t_flags & TF_NOOPT;
1872 INP_WUNLOCK(inp);
1873 error = sooptcopyout(sopt, &optval, sizeof optval);
1874 break;
1875 case TCP_NOPUSH:
1876 optval = tp->t_flags & TF_NOPUSH;
1877 INP_WUNLOCK(inp);
1878 error = sooptcopyout(sopt, &optval, sizeof optval);
1879 break;
1880 case TCP_INFO:
1881 tcp_fill_info(tp, &ti);
1882 INP_WUNLOCK(inp);
1883 error = sooptcopyout(sopt, &ti, sizeof ti);
1884 break;
1885 case TCP_CONGESTION:
1886 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1887 INP_WUNLOCK(inp);
1888 error = sooptcopyout(sopt, buf, len + 1);
1889 break;
1890 case TCP_KEEPIDLE:
1891 case TCP_KEEPINTVL:
1892 case TCP_KEEPINIT:
1893 case TCP_KEEPCNT:
1894 switch (sopt->sopt_name) {
1895 case TCP_KEEPIDLE:
1896 ui = TP_KEEPIDLE(tp) / hz;
1897 break;
1898 case TCP_KEEPINTVL:
1899 ui = TP_KEEPINTVL(tp) / hz;
1900 break;
1901 case TCP_KEEPINIT:
1902 ui = TP_KEEPINIT(tp) / hz;
1903 break;
1904 case TCP_KEEPCNT:
1905 ui = TP_KEEPCNT(tp);
1906 break;
1907 }
1908 INP_WUNLOCK(inp);
1909 error = sooptcopyout(sopt, &ui, sizeof(ui));
1910 break;
1911 #ifdef TCPPCAP
1912 case TCP_PCAP_OUT:
1913 case TCP_PCAP_IN:
1914 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
1915 &(tp->t_outpkts) : &(tp->t_inpkts));
1916 INP_WUNLOCK(inp);
1917 error = sooptcopyout(sopt, &optval, sizeof optval);
1918 break;
1919 #endif
1920
1921 #ifdef TCP_RFC7413
1922 case TCP_FASTOPEN:
1923 optval = tp->t_flags & TF_FASTOPEN;
1924 INP_WUNLOCK(inp);
1925 error = sooptcopyout(sopt, &optval, sizeof optval);
1926 break;
1927 #endif
1928 default:
1929 INP_WUNLOCK(inp);
1930 error = ENOPROTOOPT;
1931 break;
1932 }
1933 break;
1934 }
1935 return (error);
1936 }
1937 #undef INP_WLOCK_RECHECK
1938 #undef INP_WLOCK_RECHECK_CLEANUP
1939
1940 /*
1941 * Attach TCP protocol to socket, allocating
1942 * internet protocol control block, tcp control block,
1943 * bufer space, and entering LISTEN state if to accept connections.
1944 */
1945 static int
1946 tcp_attach(struct socket *so)
1947 {
1948 struct tcpcb *tp;
1949 struct inpcb *inp;
1950 int error;
1951
1952 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1953 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1954 if (error)
1955 return (error);
1956 }
1957 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1958 so->so_snd.sb_flags |= SB_AUTOSIZE;
1959 INP_INFO_RLOCK(&V_tcbinfo);
1960 error = in_pcballoc(so, &V_tcbinfo);
1961 if (error) {
1962 INP_INFO_RUNLOCK(&V_tcbinfo);
1963 return (error);
1964 }
1965 inp = sotoinpcb(so);
1966 #ifdef INET6
1967 if (inp->inp_vflag & INP_IPV6PROTO) {
1968 inp->inp_vflag |= INP_IPV6;
1969 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
1970 inp->inp_vflag |= INP_IPV4;
1971 inp->in6p_hops = -1; /* use kernel default */
1972 }
1973 else
1974 #endif
1975 inp->inp_vflag |= INP_IPV4;
1976 tp = tcp_newtcpcb(inp);
1977 if (tp == NULL) {
1978 in_pcbdetach(inp);
1979 in_pcbfree(inp);
1980 INP_INFO_RUNLOCK(&V_tcbinfo);
1981 return (ENOBUFS);
1982 }
1983 tp->t_state = TCPS_CLOSED;
1984 INP_WUNLOCK(inp);
1985 INP_INFO_RUNLOCK(&V_tcbinfo);
1986 TCPSTATES_INC(TCPS_CLOSED);
1987 return (0);
1988 }
1989
1990 /*
1991 * Initiate (or continue) disconnect.
1992 * If embryonic state, just send reset (once).
1993 * If in ``let data drain'' option and linger null, just drop.
1994 * Otherwise (hard), mark socket disconnecting and drop
1995 * current input data; switch states based on user close, and
1996 * send segment to peer (with FIN).
1997 */
1998 static void
1999 tcp_disconnect(struct tcpcb *tp)
2000 {
2001 struct inpcb *inp = tp->t_inpcb;
2002 struct socket *so = inp->inp_socket;
2003
2004 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2005 INP_WLOCK_ASSERT(inp);
2006
2007 /*
2008 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2009 * socket is still open.
2010 */
2011 if (tp->t_state < TCPS_ESTABLISHED) {
2012 tp = tcp_close(tp);
2013 KASSERT(tp != NULL,
2014 ("tcp_disconnect: tcp_close() returned NULL"));
2015 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2016 tp = tcp_drop(tp, 0);
2017 KASSERT(tp != NULL,
2018 ("tcp_disconnect: tcp_drop() returned NULL"));
2019 } else {
2020 soisdisconnecting(so);
2021 sbflush(&so->so_rcv);
2022 tcp_usrclosed(tp);
2023 if (!(inp->inp_flags & INP_DROPPED))
2024 tp->t_fb->tfb_tcp_output(tp);
2025 }
2026 }
2027
2028 /*
2029 * User issued close, and wish to trail through shutdown states:
2030 * if never received SYN, just forget it. If got a SYN from peer,
2031 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2032 * If already got a FIN from peer, then almost done; go to LAST_ACK
2033 * state. In all other cases, have already sent FIN to peer (e.g.
2034 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2035 * for peer to send FIN or not respond to keep-alives, etc.
2036 * We can let the user exit from the close as soon as the FIN is acked.
2037 */
2038 static void
2039 tcp_usrclosed(struct tcpcb *tp)
2040 {
2041
2042 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2043 INP_WLOCK_ASSERT(tp->t_inpcb);
2044
2045 switch (tp->t_state) {
2046 case TCPS_LISTEN:
2047 #ifdef TCP_OFFLOAD
2048 tcp_offload_listen_stop(tp);
2049 #endif
2050 tcp_state_change(tp, TCPS_CLOSED);
2051 /* FALLTHROUGH */
2052 case TCPS_CLOSED:
2053 tp = tcp_close(tp);
2054 /*
2055 * tcp_close() should never return NULL here as the socket is
2056 * still open.
2057 */
2058 KASSERT(tp != NULL,
2059 ("tcp_usrclosed: tcp_close() returned NULL"));
2060 break;
2061
2062 case TCPS_SYN_SENT:
2063 case TCPS_SYN_RECEIVED:
2064 tp->t_flags |= TF_NEEDFIN;
2065 break;
2066
2067 case TCPS_ESTABLISHED:
2068 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2069 break;
2070
2071 case TCPS_CLOSE_WAIT:
2072 tcp_state_change(tp, TCPS_LAST_ACK);
2073 break;
2074 }
2075 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2076 soisdisconnected(tp->t_inpcb->inp_socket);
2077 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2078 if (tp->t_state == TCPS_FIN_WAIT_2) {
2079 int timeout;
2080
2081 timeout = (tcp_fast_finwait2_recycle) ?
2082 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2083 tcp_timer_activate(tp, TT_2MSL, timeout);
2084 }
2085 }
2086 }
2087
2088 #ifdef DDB
2089 static void
2090 db_print_indent(int indent)
2091 {
2092 int i;
2093
2094 for (i = 0; i < indent; i++)
2095 db_printf(" ");
2096 }
2097
2098 static void
2099 db_print_tstate(int t_state)
2100 {
2101
2102 switch (t_state) {
2103 case TCPS_CLOSED:
2104 db_printf("TCPS_CLOSED");
2105 return;
2106
2107 case TCPS_LISTEN:
2108 db_printf("TCPS_LISTEN");
2109 return;
2110
2111 case TCPS_SYN_SENT:
2112 db_printf("TCPS_SYN_SENT");
2113 return;
2114
2115 case TCPS_SYN_RECEIVED:
2116 db_printf("TCPS_SYN_RECEIVED");
2117 return;
2118
2119 case TCPS_ESTABLISHED:
2120 db_printf("TCPS_ESTABLISHED");
2121 return;
2122
2123 case TCPS_CLOSE_WAIT:
2124 db_printf("TCPS_CLOSE_WAIT");
2125 return;
2126
2127 case TCPS_FIN_WAIT_1:
2128 db_printf("TCPS_FIN_WAIT_1");
2129 return;
2130
2131 case TCPS_CLOSING:
2132 db_printf("TCPS_CLOSING");
2133 return;
2134
2135 case TCPS_LAST_ACK:
2136 db_printf("TCPS_LAST_ACK");
2137 return;
2138
2139 case TCPS_FIN_WAIT_2:
2140 db_printf("TCPS_FIN_WAIT_2");
2141 return;
2142
2143 case TCPS_TIME_WAIT:
2144 db_printf("TCPS_TIME_WAIT");
2145 return;
2146
2147 default:
2148 db_printf("unknown");
2149 return;
2150 }
2151 }
2152
2153 static void
2154 db_print_tflags(u_int t_flags)
2155 {
2156 int comma;
2157
2158 comma = 0;
2159 if (t_flags & TF_ACKNOW) {
2160 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2161 comma = 1;
2162 }
2163 if (t_flags & TF_DELACK) {
2164 db_printf("%sTF_DELACK", comma ? ", " : "");
2165 comma = 1;
2166 }
2167 if (t_flags & TF_NODELAY) {
2168 db_printf("%sTF_NODELAY", comma ? ", " : "");
2169 comma = 1;
2170 }
2171 if (t_flags & TF_NOOPT) {
2172 db_printf("%sTF_NOOPT", comma ? ", " : "");
2173 comma = 1;
2174 }
2175 if (t_flags & TF_SENTFIN) {
2176 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2177 comma = 1;
2178 }
2179 if (t_flags & TF_REQ_SCALE) {
2180 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2181 comma = 1;
2182 }
2183 if (t_flags & TF_RCVD_SCALE) {
2184 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2185 comma = 1;
2186 }
2187 if (t_flags & TF_REQ_TSTMP) {
2188 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2189 comma = 1;
2190 }
2191 if (t_flags & TF_RCVD_TSTMP) {
2192 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2193 comma = 1;
2194 }
2195 if (t_flags & TF_SACK_PERMIT) {
2196 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2197 comma = 1;
2198 }
2199 if (t_flags & TF_NEEDSYN) {
2200 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2201 comma = 1;
2202 }
2203 if (t_flags & TF_NEEDFIN) {
2204 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2205 comma = 1;
2206 }
2207 if (t_flags & TF_NOPUSH) {
2208 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2209 comma = 1;
2210 }
2211 if (t_flags & TF_MORETOCOME) {
2212 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2213 comma = 1;
2214 }
2215 if (t_flags & TF_LQ_OVERFLOW) {
2216 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2217 comma = 1;
2218 }
2219 if (t_flags & TF_LASTIDLE) {
2220 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2221 comma = 1;
2222 }
2223 if (t_flags & TF_RXWIN0SENT) {
2224 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2225 comma = 1;
2226 }
2227 if (t_flags & TF_FASTRECOVERY) {
2228 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2229 comma = 1;
2230 }
2231 if (t_flags & TF_CONGRECOVERY) {
2232 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2233 comma = 1;
2234 }
2235 if (t_flags & TF_WASFRECOVERY) {
2236 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2237 comma = 1;
2238 }
2239 if (t_flags & TF_SIGNATURE) {
2240 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2241 comma = 1;
2242 }
2243 if (t_flags & TF_FORCEDATA) {
2244 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2245 comma = 1;
2246 }
2247 if (t_flags & TF_TSO) {
2248 db_printf("%sTF_TSO", comma ? ", " : "");
2249 comma = 1;
2250 }
2251 if (t_flags & TF_ECN_PERMIT) {
2252 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2253 comma = 1;
2254 }
2255 if (t_flags & TF_FASTOPEN) {
2256 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2257 comma = 1;
2258 }
2259 }
2260
2261 static void
2262 db_print_toobflags(char t_oobflags)
2263 {
2264 int comma;
2265
2266 comma = 0;
2267 if (t_oobflags & TCPOOB_HAVEDATA) {
2268 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2269 comma = 1;
2270 }
2271 if (t_oobflags & TCPOOB_HADDATA) {
2272 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2273 comma = 1;
2274 }
2275 }
2276
2277 static void
2278 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2279 {
2280
2281 db_print_indent(indent);
2282 db_printf("%s at %p\n", name, tp);
2283
2284 indent += 2;
2285
2286 db_print_indent(indent);
2287 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2288 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2289
2290 db_print_indent(indent);
2291 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2292 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2293
2294 db_print_indent(indent);
2295 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2296 &tp->t_timers->tt_delack, tp->t_inpcb);
2297
2298 db_print_indent(indent);
2299 db_printf("t_state: %d (", tp->t_state);
2300 db_print_tstate(tp->t_state);
2301 db_printf(")\n");
2302
2303 db_print_indent(indent);
2304 db_printf("t_flags: 0x%x (", tp->t_flags);
2305 db_print_tflags(tp->t_flags);
2306 db_printf(")\n");
2307
2308 db_print_indent(indent);
2309 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2310 tp->snd_una, tp->snd_max, tp->snd_nxt);
2311
2312 db_print_indent(indent);
2313 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2314 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2315
2316 db_print_indent(indent);
2317 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2318 tp->iss, tp->irs, tp->rcv_nxt);
2319
2320 db_print_indent(indent);
2321 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n",
2322 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2323
2324 db_print_indent(indent);
2325 db_printf("snd_wnd: %lu snd_cwnd: %lu\n",
2326 tp->snd_wnd, tp->snd_cwnd);
2327
2328 db_print_indent(indent);
2329 db_printf("snd_ssthresh: %lu snd_recover: "
2330 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2331
2332 db_print_indent(indent);
2333 db_printf("t_rcvtime: %u t_startime: %u\n",
2334 tp->t_rcvtime, tp->t_starttime);
2335
2336 db_print_indent(indent);
2337 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2338 tp->t_rtttime, tp->t_rtseq);
2339
2340 db_print_indent(indent);
2341 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2342 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2343
2344 db_print_indent(indent);
2345 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2346 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2347 tp->t_rttbest);
2348
2349 db_print_indent(indent);
2350 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n",
2351 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2352
2353 db_print_indent(indent);
2354 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2355 db_print_toobflags(tp->t_oobflags);
2356 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2357
2358 db_print_indent(indent);
2359 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2360 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2361
2362 db_print_indent(indent);
2363 db_printf("ts_recent: %u ts_recent_age: %u\n",
2364 tp->ts_recent, tp->ts_recent_age);
2365
2366 db_print_indent(indent);
2367 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2368 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2369
2370 db_print_indent(indent);
2371 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x "
2372 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2373 tp->snd_recover_prev, tp->t_badrxtwin);
2374
2375 db_print_indent(indent);
2376 db_printf("snd_numholes: %d snd_holes first: %p\n",
2377 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2378
2379 db_print_indent(indent);
2380 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2381 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2382
2383 /* Skip sackblks, sackhint. */
2384
2385 db_print_indent(indent);
2386 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2387 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2388 }
2389
2390 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2391 {
2392 struct tcpcb *tp;
2393
2394 if (!have_addr) {
2395 db_printf("usage: show tcpcb <addr>\n");
2396 return;
2397 }
2398 tp = (struct tcpcb *)addr;
2399
2400 db_print_tcpcb(tp, "tcpcb", 0);
2401 }
2402 #endif
Cache object: 2ca4b037e3ebbf22c5e71920336043ab
|