]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/netinet/tcp_usrreq.c
MFC r281549:
[FreeBSD/stable/10.git] / sys / netinet / tcp_usrreq.c
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *      The Regents of the University of California.
4  * Copyright (c) 2006-2007 Robert N. M. Watson
5  * Copyright (c) 2010-2011 Juniper Networks, Inc.
6  * All rights reserved.
7  *
8  * Portions of this software were developed by Robert N. M. Watson under
9  * contract to Juniper Networks, Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *      From: @(#)tcp_usrreq.c  8.2 (Berkeley) 1/3/94
36  */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include "opt_ddb.h"
42 #include "opt_inet.h"
43 #include "opt_inet6.h"
44 #include "opt_tcpdebug.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/limits.h>
49 #include <sys/malloc.h>
50 #include <sys/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 | INP_DROPPED)) {
479                 error = EINVAL;
480                 goto out;
481         }
482         tp = intotcpcb(inp);
483         TCPDEBUG1();
484         if ((error = tcp_connect(tp, nam, td)) != 0)
485                 goto out;
486 #ifdef TCP_OFFLOAD
487         if (registered_toedevs > 0 &&
488             (so->so_options & SO_NO_OFFLOAD) == 0 &&
489             (error = tcp_offload_connect(so, nam)) == 0)
490                 goto out;
491 #endif
492         tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
493         error = tcp_output(tp);
494 out:
495         TCPDEBUG2(PRU_CONNECT);
496         INP_WUNLOCK(inp);
497         return (error);
498 }
499 #endif /* INET */
500
501 #ifdef INET6
502 static int
503 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
504 {
505         int error = 0;
506         struct inpcb *inp;
507         struct tcpcb *tp = NULL;
508         struct sockaddr_in6 *sin6p;
509
510         TCPDEBUG0;
511
512         sin6p = (struct sockaddr_in6 *)nam;
513         if (nam->sa_len != sizeof (*sin6p))
514                 return (EINVAL);
515         /*
516          * Must disallow TCP ``connections'' to multicast addresses.
517          */
518         if (sin6p->sin6_family == AF_INET6
519             && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
520                 return (EAFNOSUPPORT);
521
522         inp = sotoinpcb(so);
523         KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
524         INP_WLOCK(inp);
525         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
526                 error = EINVAL;
527                 goto out;
528         }
529         tp = intotcpcb(inp);
530         TCPDEBUG1();
531 #ifdef INET
532         /*
533          * XXXRW: Some confusion: V4/V6 flags relate to binding, and
534          * therefore probably require the hash lock, which isn't held here.
535          * Is this a significant problem?
536          */
537         if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
538                 struct sockaddr_in sin;
539
540                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
541                         error = EINVAL;
542                         goto out;
543                 }
544
545                 in6_sin6_2_sin(&sin, sin6p);
546                 inp->inp_vflag |= INP_IPV4;
547                 inp->inp_vflag &= ~INP_IPV6;
548                 if ((error = prison_remote_ip4(td->td_ucred,
549                     &sin.sin_addr)) != 0)
550                         goto out;
551                 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
552                         goto out;
553 #ifdef TCP_OFFLOAD
554                 if (registered_toedevs > 0 &&
555                     (so->so_options & SO_NO_OFFLOAD) == 0 &&
556                     (error = tcp_offload_connect(so, nam)) == 0)
557                         goto out;
558 #endif
559                 error = tcp_output(tp);
560                 goto out;
561         }
562 #endif
563         inp->inp_vflag &= ~INP_IPV4;
564         inp->inp_vflag |= INP_IPV6;
565         inp->inp_inc.inc_flags |= INC_ISIPV6;
566         if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
567                 goto out;
568         if ((error = tcp6_connect(tp, nam, td)) != 0)
569                 goto out;
570 #ifdef TCP_OFFLOAD
571         if (registered_toedevs > 0 &&
572             (so->so_options & SO_NO_OFFLOAD) == 0 &&
573             (error = tcp_offload_connect(so, nam)) == 0)
574                 goto out;
575 #endif
576         tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
577         error = tcp_output(tp);
578
579 out:
580         TCPDEBUG2(PRU_CONNECT);
581         INP_WUNLOCK(inp);
582         return (error);
583 }
584 #endif /* INET6 */
585
586 /*
587  * Initiate disconnect from peer.
588  * If connection never passed embryonic stage, just drop;
589  * else if don't need to let data drain, then can just drop anyways,
590  * else have to begin TCP shutdown process: mark socket disconnecting,
591  * drain unread data, state switch to reflect user close, and
592  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
593  * when peer sends FIN and acks ours.
594  *
595  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
596  */
597 static int
598 tcp_usr_disconnect(struct socket *so)
599 {
600         struct inpcb *inp;
601         struct tcpcb *tp = NULL;
602         int error = 0;
603
604         TCPDEBUG0;
605         INP_INFO_WLOCK(&V_tcbinfo);
606         inp = sotoinpcb(so);
607         KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
608         INP_WLOCK(inp);
609         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
610                 error = ECONNRESET;
611                 goto out;
612         }
613         tp = intotcpcb(inp);
614         TCPDEBUG1();
615         tcp_disconnect(tp);
616 out:
617         TCPDEBUG2(PRU_DISCONNECT);
618         INP_WUNLOCK(inp);
619         INP_INFO_WUNLOCK(&V_tcbinfo);
620         return (error);
621 }
622
623 #ifdef INET
624 /*
625  * Accept a connection.  Essentially all the work is done at higher levels;
626  * just return the address of the peer, storing through addr.
627  *
628  * The rationale for acquiring the tcbinfo lock here is somewhat complicated,
629  * and is described in detail in the commit log entry for r175612.  Acquiring
630  * it delays an accept(2) racing with sonewconn(), which inserts the socket
631  * before the inpcb address/port fields are initialized.  A better fix would
632  * prevent the socket from being placed in the listen queue until all fields
633  * are fully initialized.
634  */
635 static int
636 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
637 {
638         int error = 0;
639         struct inpcb *inp = NULL;
640         struct tcpcb *tp = NULL;
641         struct in_addr addr;
642         in_port_t port = 0;
643         TCPDEBUG0;
644
645         if (so->so_state & SS_ISDISCONNECTED)
646                 return (ECONNABORTED);
647
648         inp = sotoinpcb(so);
649         KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
650         INP_INFO_RLOCK(&V_tcbinfo);
651         INP_WLOCK(inp);
652         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
653                 error = ECONNABORTED;
654                 goto out;
655         }
656         tp = intotcpcb(inp);
657         TCPDEBUG1();
658
659         /*
660          * We inline in_getpeeraddr and COMMON_END here, so that we can
661          * copy the data of interest and defer the malloc until after we
662          * release the lock.
663          */
664         port = inp->inp_fport;
665         addr = inp->inp_faddr;
666
667 out:
668         TCPDEBUG2(PRU_ACCEPT);
669         INP_WUNLOCK(inp);
670         INP_INFO_RUNLOCK(&V_tcbinfo);
671         if (error == 0)
672                 *nam = in_sockaddr(port, &addr);
673         return error;
674 }
675 #endif /* INET */
676
677 #ifdef INET6
678 static int
679 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
680 {
681         struct inpcb *inp = NULL;
682         int error = 0;
683         struct tcpcb *tp = NULL;
684         struct in_addr addr;
685         struct in6_addr addr6;
686         in_port_t port = 0;
687         int v4 = 0;
688         TCPDEBUG0;
689
690         if (so->so_state & SS_ISDISCONNECTED)
691                 return (ECONNABORTED);
692
693         inp = sotoinpcb(so);
694         KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
695         INP_INFO_RLOCK(&V_tcbinfo);
696         INP_WLOCK(inp);
697         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
698                 error = ECONNABORTED;
699                 goto out;
700         }
701         tp = intotcpcb(inp);
702         TCPDEBUG1();
703
704         /*
705          * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
706          * copy the data of interest and defer the malloc until after we
707          * release the lock.
708          */
709         if (inp->inp_vflag & INP_IPV4) {
710                 v4 = 1;
711                 port = inp->inp_fport;
712                 addr = inp->inp_faddr;
713         } else {
714                 port = inp->inp_fport;
715                 addr6 = inp->in6p_faddr;
716         }
717
718 out:
719         TCPDEBUG2(PRU_ACCEPT);
720         INP_WUNLOCK(inp);
721         INP_INFO_RUNLOCK(&V_tcbinfo);
722         if (error == 0) {
723                 if (v4)
724                         *nam = in6_v4mapsin6_sockaddr(port, &addr);
725                 else
726                         *nam = in6_sockaddr(port, &addr6);
727         }
728         return error;
729 }
730 #endif /* INET6 */
731
732 /*
733  * Mark the connection as being incapable of further output.
734  */
735 static int
736 tcp_usr_shutdown(struct socket *so)
737 {
738         int error = 0;
739         struct inpcb *inp;
740         struct tcpcb *tp = NULL;
741
742         TCPDEBUG0;
743         INP_INFO_WLOCK(&V_tcbinfo);
744         inp = sotoinpcb(so);
745         KASSERT(inp != NULL, ("inp == NULL"));
746         INP_WLOCK(inp);
747         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
748                 error = ECONNRESET;
749                 goto out;
750         }
751         tp = intotcpcb(inp);
752         TCPDEBUG1();
753         socantsendmore(so);
754         tcp_usrclosed(tp);
755         if (!(inp->inp_flags & INP_DROPPED))
756                 error = tcp_output(tp);
757
758 out:
759         TCPDEBUG2(PRU_SHUTDOWN);
760         INP_WUNLOCK(inp);
761         INP_INFO_WUNLOCK(&V_tcbinfo);
762
763         return (error);
764 }
765
766 /*
767  * After a receive, possibly send window update to peer.
768  */
769 static int
770 tcp_usr_rcvd(struct socket *so, int flags)
771 {
772         struct inpcb *inp;
773         struct tcpcb *tp = NULL;
774         int error = 0;
775
776         TCPDEBUG0;
777         inp = sotoinpcb(so);
778         KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
779         INP_WLOCK(inp);
780         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
781                 error = ECONNRESET;
782                 goto out;
783         }
784         tp = intotcpcb(inp);
785         TCPDEBUG1();
786 #ifdef TCP_OFFLOAD
787         if (tp->t_flags & TF_TOE)
788                 tcp_offload_rcvd(tp);
789         else
790 #endif
791         tcp_output(tp);
792
793 out:
794         TCPDEBUG2(PRU_RCVD);
795         INP_WUNLOCK(inp);
796         return (error);
797 }
798
799 /*
800  * Do a send by putting data in output queue and updating urgent
801  * marker if URG set.  Possibly send more data.  Unlike the other
802  * pru_*() routines, the mbuf chains are our responsibility.  We
803  * must either enqueue them or free them.  The other pru_* routines
804  * generally are caller-frees.
805  */
806 static int
807 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
808     struct sockaddr *nam, struct mbuf *control, struct thread *td)
809 {
810         int error = 0;
811         struct inpcb *inp;
812         struct tcpcb *tp = NULL;
813 #ifdef INET6
814         int isipv6;
815 #endif
816         TCPDEBUG0;
817
818         /*
819          * We require the pcbinfo lock if we will close the socket as part of
820          * this call.
821          */
822         if (flags & PRUS_EOF)
823                 INP_INFO_WLOCK(&V_tcbinfo);
824         inp = sotoinpcb(so);
825         KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
826         INP_WLOCK(inp);
827         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
828                 if (control)
829                         m_freem(control);
830                 if (m)
831                         m_freem(m);
832                 error = ECONNRESET;
833                 goto out;
834         }
835 #ifdef INET6
836         isipv6 = nam && nam->sa_family == AF_INET6;
837 #endif /* INET6 */
838         tp = intotcpcb(inp);
839         TCPDEBUG1();
840         if (control) {
841                 /* TCP doesn't do control messages (rights, creds, etc) */
842                 if (control->m_len) {
843                         m_freem(control);
844                         if (m)
845                                 m_freem(m);
846                         error = EINVAL;
847                         goto out;
848                 }
849                 m_freem(control);       /* empty control, just free it */
850         }
851         if (!(flags & PRUS_OOB)) {
852                 sbappendstream(&so->so_snd, m);
853                 if (nam && tp->t_state < TCPS_SYN_SENT) {
854                         /*
855                          * Do implied connect if not yet connected,
856                          * initialize window to default value, and
857                          * initialize maxseg/maxopd using peer's cached
858                          * MSS.
859                          */
860 #ifdef INET6
861                         if (isipv6)
862                                 error = tcp6_connect(tp, nam, td);
863 #endif /* INET6 */
864 #if defined(INET6) && defined(INET)
865                         else
866 #endif
867 #ifdef INET
868                                 error = tcp_connect(tp, nam, td);
869 #endif
870                         if (error)
871                                 goto out;
872                         tp->snd_wnd = TTCP_CLIENT_SND_WND;
873                         tcp_mss(tp, -1);
874                 }
875                 if (flags & PRUS_EOF) {
876                         /*
877                          * Close the send side of the connection after
878                          * the data is sent.
879                          */
880                         INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
881                         socantsendmore(so);
882                         tcp_usrclosed(tp);
883                 }
884                 if (!(inp->inp_flags & INP_DROPPED)) {
885                         if (flags & PRUS_MORETOCOME)
886                                 tp->t_flags |= TF_MORETOCOME;
887                         error = tcp_output(tp);
888                         if (flags & PRUS_MORETOCOME)
889                                 tp->t_flags &= ~TF_MORETOCOME;
890                 }
891         } else {
892                 /*
893                  * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
894                  */
895                 SOCKBUF_LOCK(&so->so_snd);
896                 if (sbspace(&so->so_snd) < -512) {
897                         SOCKBUF_UNLOCK(&so->so_snd);
898                         m_freem(m);
899                         error = ENOBUFS;
900                         goto out;
901                 }
902                 /*
903                  * According to RFC961 (Assigned Protocols),
904                  * the urgent pointer points to the last octet
905                  * of urgent data.  We continue, however,
906                  * to consider it to indicate the first octet
907                  * of data past the urgent section.
908                  * Otherwise, snd_up should be one lower.
909                  */
910                 sbappendstream_locked(&so->so_snd, m);
911                 SOCKBUF_UNLOCK(&so->so_snd);
912                 if (nam && tp->t_state < TCPS_SYN_SENT) {
913                         /*
914                          * Do implied connect if not yet connected,
915                          * initialize window to default value, and
916                          * initialize maxseg/maxopd using peer's cached
917                          * MSS.
918                          */
919 #ifdef INET6
920                         if (isipv6)
921                                 error = tcp6_connect(tp, nam, td);
922 #endif /* INET6 */
923 #if defined(INET6) && defined(INET)
924                         else
925 #endif
926 #ifdef INET
927                                 error = tcp_connect(tp, nam, td);
928 #endif
929                         if (error)
930                                 goto out;
931                         tp->snd_wnd = TTCP_CLIENT_SND_WND;
932                         tcp_mss(tp, -1);
933                 }
934                 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
935                 tp->t_flags |= TF_FORCEDATA;
936                 error = tcp_output(tp);
937                 tp->t_flags &= ~TF_FORCEDATA;
938         }
939 out:
940         TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
941                   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
942         INP_WUNLOCK(inp);
943         if (flags & PRUS_EOF)
944                 INP_INFO_WUNLOCK(&V_tcbinfo);
945         return (error);
946 }
947
948 /*
949  * Abort the TCP.  Drop the connection abruptly.
950  */
951 static void
952 tcp_usr_abort(struct socket *so)
953 {
954         struct inpcb *inp;
955         struct tcpcb *tp = NULL;
956         TCPDEBUG0;
957
958         inp = sotoinpcb(so);
959         KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
960
961         INP_INFO_WLOCK(&V_tcbinfo);
962         INP_WLOCK(inp);
963         KASSERT(inp->inp_socket != NULL,
964             ("tcp_usr_abort: inp_socket == NULL"));
965
966         /*
967          * If we still have full TCP state, and we're not dropped, drop.
968          */
969         if (!(inp->inp_flags & INP_TIMEWAIT) &&
970             !(inp->inp_flags & INP_DROPPED)) {
971                 tp = intotcpcb(inp);
972                 TCPDEBUG1();
973                 tcp_drop(tp, ECONNABORTED);
974                 TCPDEBUG2(PRU_ABORT);
975         }
976         if (!(inp->inp_flags & INP_DROPPED)) {
977                 SOCK_LOCK(so);
978                 so->so_state |= SS_PROTOREF;
979                 SOCK_UNLOCK(so);
980                 inp->inp_flags |= INP_SOCKREF;
981         }
982         INP_WUNLOCK(inp);
983         INP_INFO_WUNLOCK(&V_tcbinfo);
984 }
985
986 /*
987  * TCP socket is closed.  Start friendly disconnect.
988  */
989 static void
990 tcp_usr_close(struct socket *so)
991 {
992         struct inpcb *inp;
993         struct tcpcb *tp = NULL;
994         TCPDEBUG0;
995
996         inp = sotoinpcb(so);
997         KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
998
999         INP_INFO_WLOCK(&V_tcbinfo);
1000         INP_WLOCK(inp);
1001         KASSERT(inp->inp_socket != NULL,
1002             ("tcp_usr_close: inp_socket == NULL"));
1003
1004         /*
1005          * If we still have full TCP state, and we're not dropped, initiate
1006          * a disconnect.
1007          */
1008         if (!(inp->inp_flags & INP_TIMEWAIT) &&
1009             !(inp->inp_flags & INP_DROPPED)) {
1010                 tp = intotcpcb(inp);
1011                 TCPDEBUG1();
1012                 tcp_disconnect(tp);
1013                 TCPDEBUG2(PRU_CLOSE);
1014         }
1015         if (!(inp->inp_flags & INP_DROPPED)) {
1016                 SOCK_LOCK(so);
1017                 so->so_state |= SS_PROTOREF;
1018                 SOCK_UNLOCK(so);
1019                 inp->inp_flags |= INP_SOCKREF;
1020         }
1021         INP_WUNLOCK(inp);
1022         INP_INFO_WUNLOCK(&V_tcbinfo);
1023 }
1024
1025 /*
1026  * Receive out-of-band data.
1027  */
1028 static int
1029 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1030 {
1031         int error = 0;
1032         struct inpcb *inp;
1033         struct tcpcb *tp = NULL;
1034
1035         TCPDEBUG0;
1036         inp = sotoinpcb(so);
1037         KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1038         INP_WLOCK(inp);
1039         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1040                 error = ECONNRESET;
1041                 goto out;
1042         }
1043         tp = intotcpcb(inp);
1044         TCPDEBUG1();
1045         if ((so->so_oobmark == 0 &&
1046              (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1047             so->so_options & SO_OOBINLINE ||
1048             tp->t_oobflags & TCPOOB_HADDATA) {
1049                 error = EINVAL;
1050                 goto out;
1051         }
1052         if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1053                 error = EWOULDBLOCK;
1054                 goto out;
1055         }
1056         m->m_len = 1;
1057         *mtod(m, caddr_t) = tp->t_iobc;
1058         if ((flags & MSG_PEEK) == 0)
1059                 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1060
1061 out:
1062         TCPDEBUG2(PRU_RCVOOB);
1063         INP_WUNLOCK(inp);
1064         return (error);
1065 }
1066
1067 #ifdef INET
1068 struct pr_usrreqs tcp_usrreqs = {
1069         .pru_abort =            tcp_usr_abort,
1070         .pru_accept =           tcp_usr_accept,
1071         .pru_attach =           tcp_usr_attach,
1072         .pru_bind =             tcp_usr_bind,
1073         .pru_connect =          tcp_usr_connect,
1074         .pru_control =          in_control,
1075         .pru_detach =           tcp_usr_detach,
1076         .pru_disconnect =       tcp_usr_disconnect,
1077         .pru_listen =           tcp_usr_listen,
1078         .pru_peeraddr =         in_getpeeraddr,
1079         .pru_rcvd =             tcp_usr_rcvd,
1080         .pru_rcvoob =           tcp_usr_rcvoob,
1081         .pru_send =             tcp_usr_send,
1082         .pru_shutdown =         tcp_usr_shutdown,
1083         .pru_sockaddr =         in_getsockaddr,
1084         .pru_sosetlabel =       in_pcbsosetlabel,
1085         .pru_close =            tcp_usr_close,
1086 };
1087 #endif /* INET */
1088
1089 #ifdef INET6
1090 struct pr_usrreqs tcp6_usrreqs = {
1091         .pru_abort =            tcp_usr_abort,
1092         .pru_accept =           tcp6_usr_accept,
1093         .pru_attach =           tcp_usr_attach,
1094         .pru_bind =             tcp6_usr_bind,
1095         .pru_connect =          tcp6_usr_connect,
1096         .pru_control =          in6_control,
1097         .pru_detach =           tcp_usr_detach,
1098         .pru_disconnect =       tcp_usr_disconnect,
1099         .pru_listen =           tcp6_usr_listen,
1100         .pru_peeraddr =         in6_mapped_peeraddr,
1101         .pru_rcvd =             tcp_usr_rcvd,
1102         .pru_rcvoob =           tcp_usr_rcvoob,
1103         .pru_send =             tcp_usr_send,
1104         .pru_shutdown =         tcp_usr_shutdown,
1105         .pru_sockaddr =         in6_mapped_sockaddr,
1106         .pru_sosetlabel =       in_pcbsosetlabel,
1107         .pru_close =            tcp_usr_close,
1108 };
1109 #endif /* INET6 */
1110
1111 #ifdef INET
1112 /*
1113  * Common subroutine to open a TCP connection to remote host specified
1114  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
1115  * port number if needed.  Call in_pcbconnect_setup to do the routing and
1116  * to choose a local host address (interface).  If there is an existing
1117  * incarnation of the same connection in TIME-WAIT state and if the remote
1118  * host was sending CC options and if the connection duration was < MSL, then
1119  * truncate the previous TIME-WAIT state and proceed.
1120  * Initialize connection parameters and enter SYN-SENT state.
1121  */
1122 static int
1123 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1124 {
1125         struct inpcb *inp = tp->t_inpcb, *oinp;
1126         struct socket *so = inp->inp_socket;
1127         struct in_addr laddr;
1128         u_short lport;
1129         int error;
1130
1131         INP_WLOCK_ASSERT(inp);
1132         INP_HASH_WLOCK(&V_tcbinfo);
1133
1134         if (inp->inp_lport == 0) {
1135                 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1136                 if (error)
1137                         goto out;
1138         }
1139
1140         /*
1141          * Cannot simply call in_pcbconnect, because there might be an
1142          * earlier incarnation of this same connection still in
1143          * TIME_WAIT state, creating an ADDRINUSE error.
1144          */
1145         laddr = inp->inp_laddr;
1146         lport = inp->inp_lport;
1147         error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1148             &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1149         if (error && oinp == NULL)
1150                 goto out;
1151         if (oinp) {
1152                 error = EADDRINUSE;
1153                 goto out;
1154         }
1155         inp->inp_laddr = laddr;
1156         in_pcbrehash(inp);
1157         INP_HASH_WUNLOCK(&V_tcbinfo);
1158
1159         /*
1160          * Compute window scaling to request:
1161          * Scale to fit into sweet spot.  See tcp_syncache.c.
1162          * XXX: This should move to tcp_output().
1163          */
1164         while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1165             (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1166                 tp->request_r_scale++;
1167
1168         soisconnecting(so);
1169         TCPSTAT_INC(tcps_connattempt);
1170         tcp_state_change(tp, TCPS_SYN_SENT);
1171         tp->iss = tcp_new_isn(tp);
1172         tcp_sendseqinit(tp);
1173
1174         return 0;
1175
1176 out:
1177         INP_HASH_WUNLOCK(&V_tcbinfo);
1178         return (error);
1179 }
1180 #endif /* INET */
1181
1182 #ifdef INET6
1183 static int
1184 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1185 {
1186         struct inpcb *inp = tp->t_inpcb, *oinp;
1187         struct socket *so = inp->inp_socket;
1188         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1189         struct in6_addr addr6;
1190         int error;
1191
1192         INP_WLOCK_ASSERT(inp);
1193         INP_HASH_WLOCK(&V_tcbinfo);
1194
1195         if (inp->inp_lport == 0) {
1196                 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1197                 if (error)
1198                         goto out;
1199         }
1200
1201         /*
1202          * Cannot simply call in_pcbconnect, because there might be an
1203          * earlier incarnation of this same connection still in
1204          * TIME_WAIT state, creating an ADDRINUSE error.
1205          * in6_pcbladdr() also handles scope zone IDs.
1206          *
1207          * XXXRW: We wouldn't need to expose in6_pcblookup_hash_locked()
1208          * outside of in6_pcb.c if there were an in6_pcbconnect_setup().
1209          */
1210         error = in6_pcbladdr(inp, nam, &addr6);
1211         if (error)
1212                 goto out;
1213         oinp = in6_pcblookup_hash_locked(inp->inp_pcbinfo,
1214                                   &sin6->sin6_addr, sin6->sin6_port,
1215                                   IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1216                                   ? &addr6
1217                                   : &inp->in6p_laddr,
1218                                   inp->inp_lport,  0, NULL);
1219         if (oinp) {
1220                 error = EADDRINUSE;
1221                 goto out;
1222         }
1223         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1224                 inp->in6p_laddr = addr6;
1225         inp->in6p_faddr = sin6->sin6_addr;
1226         inp->inp_fport = sin6->sin6_port;
1227         /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1228         inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
1229         if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
1230                 inp->inp_flow |=
1231                     (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1232         in_pcbrehash(inp);
1233         INP_HASH_WUNLOCK(&V_tcbinfo);
1234
1235         /* Compute window scaling to request.  */
1236         while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1237             (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1238                 tp->request_r_scale++;
1239
1240         soisconnecting(so);
1241         TCPSTAT_INC(tcps_connattempt);
1242         tcp_state_change(tp, TCPS_SYN_SENT);
1243         tp->iss = tcp_new_isn(tp);
1244         tcp_sendseqinit(tp);
1245
1246         return 0;
1247
1248 out:
1249         INP_HASH_WUNLOCK(&V_tcbinfo);
1250         return error;
1251 }
1252 #endif /* INET6 */
1253
1254 /*
1255  * Export TCP internal state information via a struct tcp_info, based on the
1256  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1257  * (TCP state machine, etc).  We export all information using FreeBSD-native
1258  * constants -- for example, the numeric values for tcpi_state will differ
1259  * from Linux.
1260  */
1261 static void
1262 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1263 {
1264
1265         INP_WLOCK_ASSERT(tp->t_inpcb);
1266         bzero(ti, sizeof(*ti));
1267
1268         ti->tcpi_state = tp->t_state;
1269         if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1270                 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1271         if (tp->t_flags & TF_SACK_PERMIT)
1272                 ti->tcpi_options |= TCPI_OPT_SACK;
1273         if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1274                 ti->tcpi_options |= TCPI_OPT_WSCALE;
1275                 ti->tcpi_snd_wscale = tp->snd_scale;
1276                 ti->tcpi_rcv_wscale = tp->rcv_scale;
1277         }
1278
1279         ti->tcpi_rto = tp->t_rxtcur * tick;
1280         ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1281         ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1282         ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1283
1284         ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1285         ti->tcpi_snd_cwnd = tp->snd_cwnd;
1286
1287         /*
1288          * FreeBSD-specific extension fields for tcp_info.
1289          */
1290         ti->tcpi_rcv_space = tp->rcv_wnd;
1291         ti->tcpi_rcv_nxt = tp->rcv_nxt;
1292         ti->tcpi_snd_wnd = tp->snd_wnd;
1293         ti->tcpi_snd_bwnd = 0;          /* Unused, kept for compat. */
1294         ti->tcpi_snd_nxt = tp->snd_nxt;
1295         ti->tcpi_snd_mss = tp->t_maxseg;
1296         ti->tcpi_rcv_mss = tp->t_maxseg;
1297         if (tp->t_flags & TF_TOE)
1298                 ti->tcpi_options |= TCPI_OPT_TOE;
1299         ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1300         ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1301         ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1302 }
1303
1304 /*
1305  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1306  * socket option arguments.  When it re-acquires the lock after the copy, it
1307  * has to revalidate that the connection is still valid for the socket
1308  * option.
1309  */
1310 #define INP_WLOCK_RECHECK(inp) do {                                     \
1311         INP_WLOCK(inp);                                                 \
1312         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {            \
1313                 INP_WUNLOCK(inp);                                       \
1314                 return (ECONNRESET);                                    \
1315         }                                                               \
1316         tp = intotcpcb(inp);                                            \
1317 } while(0)
1318
1319 int
1320 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1321 {
1322         int     error, opt, optval;
1323         u_int   ui;
1324         struct  inpcb *inp;
1325         struct  tcpcb *tp;
1326         struct  tcp_info ti;
1327         char buf[TCP_CA_NAME_MAX];
1328         struct cc_algo *algo;
1329
1330         error = 0;
1331         inp = sotoinpcb(so);
1332         KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1333         INP_WLOCK(inp);
1334         if (sopt->sopt_level != IPPROTO_TCP) {
1335 #ifdef INET6
1336                 if (inp->inp_vflag & INP_IPV6PROTO) {
1337                         INP_WUNLOCK(inp);
1338                         error = ip6_ctloutput(so, sopt);
1339                 }
1340 #endif /* INET6 */
1341 #if defined(INET6) && defined(INET)
1342                 else
1343 #endif
1344 #ifdef INET
1345                 {
1346                         INP_WUNLOCK(inp);
1347                         error = ip_ctloutput(so, sopt);
1348                 }
1349 #endif
1350                 return (error);
1351         }
1352         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1353                 INP_WUNLOCK(inp);
1354                 return (ECONNRESET);
1355         }
1356
1357         switch (sopt->sopt_dir) {
1358         case SOPT_SET:
1359                 switch (sopt->sopt_name) {
1360 #ifdef TCP_SIGNATURE
1361                 case TCP_MD5SIG:
1362                         INP_WUNLOCK(inp);
1363                         error = sooptcopyin(sopt, &optval, sizeof optval,
1364                             sizeof optval);
1365                         if (error)
1366                                 return (error);
1367
1368                         INP_WLOCK_RECHECK(inp);
1369                         if (optval > 0)
1370                                 tp->t_flags |= TF_SIGNATURE;
1371                         else
1372                                 tp->t_flags &= ~TF_SIGNATURE;
1373                         goto unlock_and_done;
1374 #endif /* TCP_SIGNATURE */
1375
1376                 case TCP_NODELAY:
1377                 case TCP_NOOPT:
1378                         INP_WUNLOCK(inp);
1379                         error = sooptcopyin(sopt, &optval, sizeof optval,
1380                             sizeof optval);
1381                         if (error)
1382                                 return (error);
1383
1384                         INP_WLOCK_RECHECK(inp);
1385                         switch (sopt->sopt_name) {
1386                         case TCP_NODELAY:
1387                                 opt = TF_NODELAY;
1388                                 break;
1389                         case TCP_NOOPT:
1390                                 opt = TF_NOOPT;
1391                                 break;
1392                         default:
1393                                 opt = 0; /* dead code to fool gcc */
1394                                 break;
1395                         }
1396
1397                         if (optval)
1398                                 tp->t_flags |= opt;
1399                         else
1400                                 tp->t_flags &= ~opt;
1401 unlock_and_done:
1402 #ifdef TCP_OFFLOAD
1403                         if (tp->t_flags & TF_TOE) {
1404                                 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1405                                     sopt->sopt_name);
1406                         }
1407 #endif
1408                         INP_WUNLOCK(inp);
1409                         break;
1410
1411                 case TCP_NOPUSH:
1412                         INP_WUNLOCK(inp);
1413                         error = sooptcopyin(sopt, &optval, sizeof optval,
1414                             sizeof optval);
1415                         if (error)
1416                                 return (error);
1417
1418                         INP_WLOCK_RECHECK(inp);
1419                         if (optval)
1420                                 tp->t_flags |= TF_NOPUSH;
1421                         else if (tp->t_flags & TF_NOPUSH) {
1422                                 tp->t_flags &= ~TF_NOPUSH;
1423                                 if (TCPS_HAVEESTABLISHED(tp->t_state))
1424                                         error = tcp_output(tp);
1425                         }
1426                         goto unlock_and_done;
1427
1428                 case TCP_MAXSEG:
1429                         INP_WUNLOCK(inp);
1430                         error = sooptcopyin(sopt, &optval, sizeof optval,
1431                             sizeof optval);
1432                         if (error)
1433                                 return (error);
1434
1435                         INP_WLOCK_RECHECK(inp);
1436                         if (optval > 0 && optval <= tp->t_maxseg &&
1437                             optval + 40 >= V_tcp_minmss)
1438                                 tp->t_maxseg = optval;
1439                         else
1440                                 error = EINVAL;
1441                         goto unlock_and_done;
1442
1443                 case TCP_INFO:
1444                         INP_WUNLOCK(inp);
1445                         error = EINVAL;
1446                         break;
1447
1448                 case TCP_CONGESTION:
1449                         INP_WUNLOCK(inp);
1450                         bzero(buf, sizeof(buf));
1451                         error = sooptcopyin(sopt, &buf, sizeof(buf), 1);
1452                         if (error)
1453                                 break;
1454                         INP_WLOCK_RECHECK(inp);
1455                         /*
1456                          * Return EINVAL if we can't find the requested cc algo.
1457                          */
1458                         error = EINVAL;
1459                         CC_LIST_RLOCK();
1460                         STAILQ_FOREACH(algo, &cc_list, entries) {
1461                                 if (strncmp(buf, algo->name, TCP_CA_NAME_MAX)
1462                                     == 0) {
1463                                         /* We've found the requested algo. */
1464                                         error = 0;
1465                                         /*
1466                                          * We hold a write lock over the tcb
1467                                          * so it's safe to do these things
1468                                          * without ordering concerns.
1469                                          */
1470                                         if (CC_ALGO(tp)->cb_destroy != NULL)
1471                                                 CC_ALGO(tp)->cb_destroy(tp->ccv);
1472                                         CC_ALGO(tp) = algo;
1473                                         /*
1474                                          * If something goes pear shaped
1475                                          * initialising the new algo,
1476                                          * fall back to newreno (which
1477                                          * does not require initialisation).
1478                                          */
1479                                         if (algo->cb_init != NULL)
1480                                                 if (algo->cb_init(tp->ccv) > 0) {
1481                                                         CC_ALGO(tp) = &newreno_cc_algo;
1482                                                         /*
1483                                                          * The only reason init
1484                                                          * should fail is
1485                                                          * because of malloc.
1486                                                          */
1487                                                         error = ENOMEM;
1488                                                 }
1489                                         break; /* Break the STAILQ_FOREACH. */
1490                                 }
1491                         }
1492                         CC_LIST_RUNLOCK();
1493                         goto unlock_and_done;
1494
1495                 case TCP_KEEPIDLE:
1496                 case TCP_KEEPINTVL:
1497                 case TCP_KEEPINIT:
1498                         INP_WUNLOCK(inp);
1499                         error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1500                         if (error)
1501                                 return (error);
1502
1503                         if (ui > (UINT_MAX / hz)) {
1504                                 error = EINVAL;
1505                                 break;
1506                         }
1507                         ui *= hz;
1508
1509                         INP_WLOCK_RECHECK(inp);
1510                         switch (sopt->sopt_name) {
1511                         case TCP_KEEPIDLE:
1512                                 tp->t_keepidle = ui;
1513                                 /*
1514                                  * XXX: better check current remaining
1515                                  * timeout and "merge" it with new value.
1516                                  */
1517                                 if ((tp->t_state > TCPS_LISTEN) &&
1518                                     (tp->t_state <= TCPS_CLOSING))
1519                                         tcp_timer_activate(tp, TT_KEEP,
1520                                             TP_KEEPIDLE(tp));
1521                                 break;
1522                         case TCP_KEEPINTVL:
1523                                 tp->t_keepintvl = ui;
1524                                 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1525                                     (TP_MAXIDLE(tp) > 0))
1526                                         tcp_timer_activate(tp, TT_2MSL,
1527                                             TP_MAXIDLE(tp));
1528                                 break;
1529                         case TCP_KEEPINIT:
1530                                 tp->t_keepinit = ui;
1531                                 if (tp->t_state == TCPS_SYN_RECEIVED ||
1532                                     tp->t_state == TCPS_SYN_SENT)
1533                                         tcp_timer_activate(tp, TT_KEEP,
1534                                             TP_KEEPINIT(tp));
1535                                 break;
1536                         }
1537                         goto unlock_and_done;
1538
1539                 case TCP_KEEPCNT:
1540                         INP_WUNLOCK(inp);
1541                         error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1542                         if (error)
1543                                 return (error);
1544
1545                         INP_WLOCK_RECHECK(inp);
1546                         tp->t_keepcnt = ui;
1547                         if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1548                             (TP_MAXIDLE(tp) > 0))
1549                                 tcp_timer_activate(tp, TT_2MSL,
1550                                     TP_MAXIDLE(tp));
1551                         goto unlock_and_done;
1552
1553                 default:
1554                         INP_WUNLOCK(inp);
1555                         error = ENOPROTOOPT;
1556                         break;
1557                 }
1558                 break;
1559
1560         case SOPT_GET:
1561                 tp = intotcpcb(inp);
1562                 switch (sopt->sopt_name) {
1563 #ifdef TCP_SIGNATURE
1564                 case TCP_MD5SIG:
1565                         optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1566                         INP_WUNLOCK(inp);
1567                         error = sooptcopyout(sopt, &optval, sizeof optval);
1568                         break;
1569 #endif
1570
1571                 case TCP_NODELAY:
1572                         optval = tp->t_flags & TF_NODELAY;
1573                         INP_WUNLOCK(inp);
1574                         error = sooptcopyout(sopt, &optval, sizeof optval);
1575                         break;
1576                 case TCP_MAXSEG:
1577                         optval = tp->t_maxseg;
1578                         INP_WUNLOCK(inp);
1579                         error = sooptcopyout(sopt, &optval, sizeof optval);
1580                         break;
1581                 case TCP_NOOPT:
1582                         optval = tp->t_flags & TF_NOOPT;
1583                         INP_WUNLOCK(inp);
1584                         error = sooptcopyout(sopt, &optval, sizeof optval);
1585                         break;
1586                 case TCP_NOPUSH:
1587                         optval = tp->t_flags & TF_NOPUSH;
1588                         INP_WUNLOCK(inp);
1589                         error = sooptcopyout(sopt, &optval, sizeof optval);
1590                         break;
1591                 case TCP_INFO:
1592                         tcp_fill_info(tp, &ti);
1593                         INP_WUNLOCK(inp);
1594                         error = sooptcopyout(sopt, &ti, sizeof ti);
1595                         break;
1596                 case TCP_CONGESTION:
1597                         bzero(buf, sizeof(buf));
1598                         strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1599                         INP_WUNLOCK(inp);
1600                         error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX);
1601                         break;
1602                 case TCP_KEEPIDLE:
1603                 case TCP_KEEPINTVL:
1604                 case TCP_KEEPINIT:
1605                 case TCP_KEEPCNT:
1606                         switch (sopt->sopt_name) {
1607                         case TCP_KEEPIDLE:
1608                                 ui = tp->t_keepidle / hz;
1609                                 break;
1610                         case TCP_KEEPINTVL:
1611                                 ui = tp->t_keepintvl / hz;
1612                                 break;
1613                         case TCP_KEEPINIT:
1614                                 ui = tp->t_keepinit / hz;
1615                                 break;
1616                         case TCP_KEEPCNT:
1617                                 ui = tp->t_keepcnt;
1618                                 break;
1619                         }
1620                         INP_WUNLOCK(inp);
1621                         error = sooptcopyout(sopt, &ui, sizeof(ui));
1622                         break;
1623                 default:
1624                         INP_WUNLOCK(inp);
1625                         error = ENOPROTOOPT;
1626                         break;
1627                 }
1628                 break;
1629         }
1630         return (error);
1631 }
1632 #undef INP_WLOCK_RECHECK
1633
1634 /*
1635  * Attach TCP protocol to socket, allocating
1636  * internet protocol control block, tcp control block,
1637  * bufer space, and entering LISTEN state if to accept connections.
1638  */
1639 static int
1640 tcp_attach(struct socket *so)
1641 {
1642         struct tcpcb *tp;
1643         struct inpcb *inp;
1644         int error;
1645
1646         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1647                 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1648                 if (error)
1649                         return (error);
1650         }
1651         so->so_rcv.sb_flags |= SB_AUTOSIZE;
1652         so->so_snd.sb_flags |= SB_AUTOSIZE;
1653         INP_INFO_WLOCK(&V_tcbinfo);
1654         error = in_pcballoc(so, &V_tcbinfo);
1655         if (error) {
1656                 INP_INFO_WUNLOCK(&V_tcbinfo);
1657                 return (error);
1658         }
1659         inp = sotoinpcb(so);
1660 #ifdef INET6
1661         if (inp->inp_vflag & INP_IPV6PROTO) {
1662                 inp->inp_vflag |= INP_IPV6;
1663                 inp->in6p_hops = -1;    /* use kernel default */
1664         }
1665         else
1666 #endif
1667         inp->inp_vflag |= INP_IPV4;
1668         tp = tcp_newtcpcb(inp);
1669         if (tp == NULL) {
1670                 in_pcbdetach(inp);
1671                 in_pcbfree(inp);
1672                 INP_INFO_WUNLOCK(&V_tcbinfo);
1673                 return (ENOBUFS);
1674         }
1675         tp->t_state = TCPS_CLOSED;
1676         INP_WUNLOCK(inp);
1677         INP_INFO_WUNLOCK(&V_tcbinfo);
1678         return (0);
1679 }
1680
1681 /*
1682  * Initiate (or continue) disconnect.
1683  * If embryonic state, just send reset (once).
1684  * If in ``let data drain'' option and linger null, just drop.
1685  * Otherwise (hard), mark socket disconnecting and drop
1686  * current input data; switch states based on user close, and
1687  * send segment to peer (with FIN).
1688  */
1689 static void
1690 tcp_disconnect(struct tcpcb *tp)
1691 {
1692         struct inpcb *inp = tp->t_inpcb;
1693         struct socket *so = inp->inp_socket;
1694
1695         INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1696         INP_WLOCK_ASSERT(inp);
1697
1698         /*
1699          * Neither tcp_close() nor tcp_drop() should return NULL, as the
1700          * socket is still open.
1701          */
1702         if (tp->t_state < TCPS_ESTABLISHED) {
1703                 tp = tcp_close(tp);
1704                 KASSERT(tp != NULL,
1705                     ("tcp_disconnect: tcp_close() returned NULL"));
1706         } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1707                 tp = tcp_drop(tp, 0);
1708                 KASSERT(tp != NULL,
1709                     ("tcp_disconnect: tcp_drop() returned NULL"));
1710         } else {
1711                 soisdisconnecting(so);
1712                 sbflush(&so->so_rcv);
1713                 tcp_usrclosed(tp);
1714                 if (!(inp->inp_flags & INP_DROPPED))
1715                         tcp_output(tp);
1716         }
1717 }
1718
1719 /*
1720  * User issued close, and wish to trail through shutdown states:
1721  * if never received SYN, just forget it.  If got a SYN from peer,
1722  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1723  * If already got a FIN from peer, then almost done; go to LAST_ACK
1724  * state.  In all other cases, have already sent FIN to peer (e.g.
1725  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1726  * for peer to send FIN or not respond to keep-alives, etc.
1727  * We can let the user exit from the close as soon as the FIN is acked.
1728  */
1729 static void
1730 tcp_usrclosed(struct tcpcb *tp)
1731 {
1732
1733         INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1734         INP_WLOCK_ASSERT(tp->t_inpcb);
1735
1736         switch (tp->t_state) {
1737         case TCPS_LISTEN:
1738 #ifdef TCP_OFFLOAD
1739                 tcp_offload_listen_stop(tp);
1740 #endif
1741                 /* FALLTHROUGH */
1742         case TCPS_CLOSED:
1743                 tcp_state_change(tp, TCPS_CLOSED);
1744                 tp = tcp_close(tp);
1745                 /*
1746                  * tcp_close() should never return NULL here as the socket is
1747                  * still open.
1748                  */
1749                 KASSERT(tp != NULL,
1750                     ("tcp_usrclosed: tcp_close() returned NULL"));
1751                 break;
1752
1753         case TCPS_SYN_SENT:
1754         case TCPS_SYN_RECEIVED:
1755                 tp->t_flags |= TF_NEEDFIN;
1756                 break;
1757
1758         case TCPS_ESTABLISHED:
1759                 tcp_state_change(tp, TCPS_FIN_WAIT_1);
1760                 break;
1761
1762         case TCPS_CLOSE_WAIT:
1763                 tcp_state_change(tp, TCPS_LAST_ACK);
1764                 break;
1765         }
1766         if (tp->t_state >= TCPS_FIN_WAIT_2) {
1767                 soisdisconnected(tp->t_inpcb->inp_socket);
1768                 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
1769                 if (tp->t_state == TCPS_FIN_WAIT_2) {
1770                         int timeout;
1771
1772                         timeout = (tcp_fast_finwait2_recycle) ? 
1773                             tcp_finwait2_timeout : TP_MAXIDLE(tp);
1774                         tcp_timer_activate(tp, TT_2MSL, timeout);
1775                 }
1776         }
1777 }
1778
1779 #ifdef DDB
1780 static void
1781 db_print_indent(int indent)
1782 {
1783         int i;
1784
1785         for (i = 0; i < indent; i++)
1786                 db_printf(" ");
1787 }
1788
1789 static void
1790 db_print_tstate(int t_state)
1791 {
1792
1793         switch (t_state) {
1794         case TCPS_CLOSED:
1795                 db_printf("TCPS_CLOSED");
1796                 return;
1797
1798         case TCPS_LISTEN:
1799                 db_printf("TCPS_LISTEN");
1800                 return;
1801
1802         case TCPS_SYN_SENT:
1803                 db_printf("TCPS_SYN_SENT");
1804                 return;
1805
1806         case TCPS_SYN_RECEIVED:
1807                 db_printf("TCPS_SYN_RECEIVED");
1808                 return;
1809
1810         case TCPS_ESTABLISHED:
1811                 db_printf("TCPS_ESTABLISHED");
1812                 return;
1813
1814         case TCPS_CLOSE_WAIT:
1815                 db_printf("TCPS_CLOSE_WAIT");
1816                 return;
1817
1818         case TCPS_FIN_WAIT_1:
1819                 db_printf("TCPS_FIN_WAIT_1");
1820                 return;
1821
1822         case TCPS_CLOSING:
1823                 db_printf("TCPS_CLOSING");
1824                 return;
1825
1826         case TCPS_LAST_ACK:
1827                 db_printf("TCPS_LAST_ACK");
1828                 return;
1829
1830         case TCPS_FIN_WAIT_2:
1831                 db_printf("TCPS_FIN_WAIT_2");
1832                 return;
1833
1834         case TCPS_TIME_WAIT:
1835                 db_printf("TCPS_TIME_WAIT");
1836                 return;
1837
1838         default:
1839                 db_printf("unknown");
1840                 return;
1841         }
1842 }
1843
1844 static void
1845 db_print_tflags(u_int t_flags)
1846 {
1847         int comma;
1848
1849         comma = 0;
1850         if (t_flags & TF_ACKNOW) {
1851                 db_printf("%sTF_ACKNOW", comma ? ", " : "");
1852                 comma = 1;
1853         }
1854         if (t_flags & TF_DELACK) {
1855                 db_printf("%sTF_DELACK", comma ? ", " : "");
1856                 comma = 1;
1857         }
1858         if (t_flags & TF_NODELAY) {
1859                 db_printf("%sTF_NODELAY", comma ? ", " : "");
1860                 comma = 1;
1861         }
1862         if (t_flags & TF_NOOPT) {
1863                 db_printf("%sTF_NOOPT", comma ? ", " : "");
1864                 comma = 1;
1865         }
1866         if (t_flags & TF_SENTFIN) {
1867                 db_printf("%sTF_SENTFIN", comma ? ", " : "");
1868                 comma = 1;
1869         }
1870         if (t_flags & TF_REQ_SCALE) {
1871                 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1872                 comma = 1;
1873         }
1874         if (t_flags & TF_RCVD_SCALE) {
1875                 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1876                 comma = 1;
1877         }
1878         if (t_flags & TF_REQ_TSTMP) {
1879                 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1880                 comma = 1;
1881         }
1882         if (t_flags & TF_RCVD_TSTMP) {
1883                 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1884                 comma = 1;
1885         }
1886         if (t_flags & TF_SACK_PERMIT) {
1887                 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1888                 comma = 1;
1889         }
1890         if (t_flags & TF_NEEDSYN) {
1891                 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1892                 comma = 1;
1893         }
1894         if (t_flags & TF_NEEDFIN) {
1895                 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1896                 comma = 1;
1897         }
1898         if (t_flags & TF_NOPUSH) {
1899                 db_printf("%sTF_NOPUSH", comma ? ", " : "");
1900                 comma = 1;
1901         }
1902         if (t_flags & TF_MORETOCOME) {
1903                 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1904                 comma = 1;
1905         }
1906         if (t_flags & TF_LQ_OVERFLOW) {
1907                 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1908                 comma = 1;
1909         }
1910         if (t_flags & TF_LASTIDLE) {
1911                 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1912                 comma = 1;
1913         }
1914         if (t_flags & TF_RXWIN0SENT) {
1915                 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1916                 comma = 1;
1917         }
1918         if (t_flags & TF_FASTRECOVERY) {
1919                 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1920                 comma = 1;
1921         }
1922         if (t_flags & TF_CONGRECOVERY) {
1923                 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
1924                 comma = 1;
1925         }
1926         if (t_flags & TF_WASFRECOVERY) {
1927                 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1928                 comma = 1;
1929         }
1930         if (t_flags & TF_SIGNATURE) {
1931                 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1932                 comma = 1;
1933         }
1934         if (t_flags & TF_FORCEDATA) {
1935                 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1936                 comma = 1;
1937         }
1938         if (t_flags & TF_TSO) {
1939                 db_printf("%sTF_TSO", comma ? ", " : "");
1940                 comma = 1;
1941         }
1942         if (t_flags & TF_ECN_PERMIT) {
1943                 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
1944                 comma = 1;
1945         }
1946 }
1947
1948 static void
1949 db_print_toobflags(char t_oobflags)
1950 {
1951         int comma;
1952
1953         comma = 0;
1954         if (t_oobflags & TCPOOB_HAVEDATA) {
1955                 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1956                 comma = 1;
1957         }
1958         if (t_oobflags & TCPOOB_HADDATA) {
1959                 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1960                 comma = 1;
1961         }
1962 }
1963
1964 static void
1965 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1966 {
1967
1968         db_print_indent(indent);
1969         db_printf("%s at %p\n", name, tp);
1970
1971         indent += 2;
1972
1973         db_print_indent(indent);
1974         db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
1975            LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
1976
1977         db_print_indent(indent);
1978         db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
1979             &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1980
1981         db_print_indent(indent);
1982         db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1983             &tp->t_timers->tt_delack, tp->t_inpcb);
1984
1985         db_print_indent(indent);
1986         db_printf("t_state: %d (", tp->t_state);
1987         db_print_tstate(tp->t_state);
1988         db_printf(")\n");
1989
1990         db_print_indent(indent);
1991         db_printf("t_flags: 0x%x (", tp->t_flags);
1992         db_print_tflags(tp->t_flags);
1993         db_printf(")\n");
1994
1995         db_print_indent(indent);
1996         db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
1997             tp->snd_una, tp->snd_max, tp->snd_nxt);
1998
1999         db_print_indent(indent);
2000         db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2001            tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2002
2003         db_print_indent(indent);
2004         db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2005             tp->iss, tp->irs, tp->rcv_nxt);
2006
2007         db_print_indent(indent);
2008         db_printf("rcv_adv: 0x%08x   rcv_wnd: %lu   rcv_up: 0x%08x\n",
2009             tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2010
2011         db_print_indent(indent);
2012         db_printf("snd_wnd: %lu   snd_cwnd: %lu\n",
2013            tp->snd_wnd, tp->snd_cwnd);
2014
2015         db_print_indent(indent);
2016         db_printf("snd_ssthresh: %lu   snd_recover: "
2017             "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2018
2019         db_print_indent(indent);
2020         db_printf("t_maxopd: %u   t_rcvtime: %u   t_startime: %u\n",
2021             tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
2022
2023         db_print_indent(indent);
2024         db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
2025             tp->t_rtttime, tp->t_rtseq);
2026
2027         db_print_indent(indent);
2028         db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
2029             tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2030
2031         db_print_indent(indent);
2032         db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2033             "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2034             tp->t_rttbest);
2035
2036         db_print_indent(indent);
2037         db_printf("t_rttupdated: %lu   max_sndwnd: %lu   t_softerror: %d\n",
2038             tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2039
2040         db_print_indent(indent);
2041         db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2042         db_print_toobflags(tp->t_oobflags);
2043         db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2044
2045         db_print_indent(indent);
2046         db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2047             tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2048
2049         db_print_indent(indent);
2050         db_printf("ts_recent: %u   ts_recent_age: %u\n",
2051             tp->ts_recent, tp->ts_recent_age);
2052
2053         db_print_indent(indent);
2054         db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
2055             "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2056
2057         db_print_indent(indent);
2058         db_printf("snd_ssthresh_prev: %lu   snd_recover_prev: 0x%08x   "
2059             "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2060             tp->snd_recover_prev, tp->t_badrxtwin);
2061
2062         db_print_indent(indent);
2063         db_printf("snd_numholes: %d  snd_holes first: %p\n",
2064             tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2065
2066         db_print_indent(indent);
2067         db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
2068             "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2069
2070         /* Skip sackblks, sackhint. */
2071
2072         db_print_indent(indent);
2073         db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2074             tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2075 }
2076
2077 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2078 {
2079         struct tcpcb *tp;
2080
2081         if (!have_addr) {
2082                 db_printf("usage: show tcpcb <addr>\n");
2083                 return;
2084         }
2085         tp = (struct tcpcb *)addr;
2086
2087         db_print_tcpcb(tp, "tcpcb", 0);
2088 }
2089 #endif