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