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