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