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