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