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