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