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