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