1acefbc1847da58336b76e1cde441540c2ab395c
[dragonfly.git] / sys / netinet / tcp_usrreq.c
1 /*
2  * Copyright (c) 2003, 2004 Jeffrey M. Hsu.  All rights reserved.
3  * Copyright (c) 2003, 2004 The DragonFly Project.  All rights reserved.
4  *
5  * This code is derived from software contributed to The DragonFly Project
6  * by Jeffrey M. Hsu.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of The DragonFly Project nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific, prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 /*
35  * Copyright (c) 1982, 1986, 1988, 1993
36  *      The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *      This product includes software developed by the University of
49  *      California, Berkeley and its contributors.
50  * 4. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *      From: @(#)tcp_usrreq.c  8.2 (Berkeley) 1/3/94
67  * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.51.2.17 2002/10/11 11:46:44 ume Exp $
68  * $DragonFly: src/sys/netinet/tcp_usrreq.c,v 1.51 2008/09/29 20:52:23 dillon Exp $
69  */
70
71 #include "opt_ipsec.h"
72 #include "opt_inet6.h"
73 #include "opt_tcpdebug.h"
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/malloc.h>
79 #include <sys/sysctl.h>
80 #include <sys/globaldata.h>
81 #include <sys/thread.h>
82
83 #include <sys/mbuf.h>
84 #ifdef INET6
85 #include <sys/domain.h>
86 #endif /* INET6 */
87 #include <sys/socket.h>
88 #include <sys/socketvar.h>
89 #include <sys/protosw.h>
90
91 #include <sys/thread2.h>
92 #include <sys/msgport2.h>
93
94 #include <net/if.h>
95 #include <net/netisr.h>
96 #include <net/route.h>
97
98 #include <net/netmsg2.h>
99
100 #include <netinet/in.h>
101 #include <netinet/in_systm.h>
102 #ifdef INET6
103 #include <netinet/ip6.h>
104 #endif
105 #include <netinet/in_pcb.h>
106 #ifdef INET6
107 #include <netinet6/in6_pcb.h>
108 #endif
109 #include <netinet/in_var.h>
110 #include <netinet/ip_var.h>
111 #ifdef INET6
112 #include <netinet6/ip6_var.h>
113 #endif
114 #include <netinet/tcp.h>
115 #include <netinet/tcp_fsm.h>
116 #include <netinet/tcp_seq.h>
117 #include <netinet/tcp_timer.h>
118 #include <netinet/tcp_timer2.h>
119 #include <netinet/tcp_var.h>
120 #include <netinet/tcpip.h>
121 #ifdef TCPDEBUG
122 #include <netinet/tcp_debug.h>
123 #endif
124
125 #ifdef IPSEC
126 #include <netinet6/ipsec.h>
127 #endif /*IPSEC*/
128
129 /*
130  * TCP protocol interface to socket abstraction.
131  */
132 extern  char *tcpstates[];      /* XXX ??? */
133
134 static int      tcp_attach (struct socket *, struct pru_attach_info *);
135 static int      tcp_connect (struct tcpcb *, struct sockaddr *,
136                                  struct thread *);
137 #ifdef INET6
138 static int      tcp6_connect (struct tcpcb *, struct sockaddr *,
139                                  struct thread *);
140 #endif /* INET6 */
141 static struct tcpcb *
142                 tcp_disconnect (struct tcpcb *);
143 static struct tcpcb *
144                 tcp_usrclosed (struct tcpcb *);
145
146 #ifdef TCPDEBUG
147 #define TCPDEBUG0       int ostate = 0
148 #define TCPDEBUG1()     ostate = tp ? tp->t_state : 0
149 #define TCPDEBUG2(req)  if (tp && (so->so_options & SO_DEBUG)) \
150                                 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
151 #else
152 #define TCPDEBUG0
153 #define TCPDEBUG1()
154 #define TCPDEBUG2(req)
155 #endif
156
157 /*
158  * TCP attaches to socket via pru_attach(), reserving space,
159  * and an internet control block.
160  */
161 static int
162 tcp_usr_attach(struct socket *so, int proto, struct pru_attach_info *ai)
163 {
164         int error;
165         struct inpcb *inp;
166         struct tcpcb *tp = 0;
167         TCPDEBUG0;
168
169         crit_enter();
170         inp = so->so_pcb;
171         TCPDEBUG1();
172         if (inp) {
173                 error = EISCONN;
174                 goto out;
175         }
176
177         error = tcp_attach(so, ai);
178         if (error)
179                 goto out;
180
181         if ((so->so_options & SO_LINGER) && so->so_linger == 0)
182                 so->so_linger = TCP_LINGERTIME;
183         tp = sototcpcb(so);
184 out:
185         TCPDEBUG2(PRU_ATTACH);
186         crit_exit();
187         return error;
188 }
189
190 /*
191  * pru_detach() detaches the TCP protocol from the socket.
192  * If the protocol state is non-embryonic, then can't
193  * do this directly: have to initiate a pru_disconnect(),
194  * which may finish later; embryonic TCB's can just
195  * be discarded here.
196  */
197 static int
198 tcp_usr_detach(struct socket *so)
199 {
200         int error = 0;
201         struct inpcb *inp;
202         struct tcpcb *tp;
203         TCPDEBUG0;
204
205         crit_enter();
206         inp = so->so_pcb;
207
208         /*
209          * If the inp is already detached it may have been due to an async
210          * close.  Just return as if no error occured.
211          */
212         if (inp == NULL) {
213                 crit_exit();
214                 return 0;
215         }
216
217         /*
218          * It's possible for the tcpcb (tp) to disconnect from the inp due
219          * to tcp_drop()->tcp_close() being called.  This may occur *after*
220          * the detach message has been queued so we may find a NULL tp here.
221          */
222         if ((tp = intotcpcb(inp)) != NULL) {
223                 TCPDEBUG1();
224                 tp = tcp_disconnect(tp);
225                 TCPDEBUG2(PRU_DETACH);
226         }
227         crit_exit();
228         return error;
229 }
230
231 /*
232  * Note: ignore_error is non-zero for certain disconnection races
233  * which we want to silently allow, otherwise close() may return
234  * an unexpected error.
235  */
236 #define COMMON_START(so, inp, ignore_error)                     \
237         TCPDEBUG0;              \
238                                 \
239         crit_enter();           \
240         inp = so->so_pcb;       \
241         do {                    \
242                  if (inp == NULL) {                             \
243                          crit_exit();                           \
244                          return (ignore_error ? 0 : EINVAL);    \
245                  }                                              \
246                  tp = intotcpcb(inp);                           \
247                  TCPDEBUG1();                                   \
248         } while(0)
249
250 #define COMMON_END(req) out: TCPDEBUG2(req); crit_exit(); return error; goto out
251
252
253 /*
254  * Give the socket an address.
255  */
256 static int
257 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
258 {
259         int error = 0;
260         struct inpcb *inp;
261         struct tcpcb *tp;
262         struct sockaddr_in *sinp;
263
264         COMMON_START(so, inp, 0);
265
266         /*
267          * Must check for multicast addresses and disallow binding
268          * to them.
269          */
270         sinp = (struct sockaddr_in *)nam;
271         if (sinp->sin_family == AF_INET &&
272             IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
273                 error = EAFNOSUPPORT;
274                 goto out;
275         }
276         error = in_pcbbind(inp, nam, td);
277         if (error)
278                 goto out;
279         COMMON_END(PRU_BIND);
280
281 }
282
283 #ifdef INET6
284 static int
285 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
286 {
287         int error = 0;
288         struct inpcb *inp;
289         struct tcpcb *tp;
290         struct sockaddr_in6 *sin6p;
291
292         COMMON_START(so, inp, 0);
293
294         /*
295          * Must check for multicast addresses and disallow binding
296          * to them.
297          */
298         sin6p = (struct sockaddr_in6 *)nam;
299         if (sin6p->sin6_family == AF_INET6 &&
300             IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
301                 error = EAFNOSUPPORT;
302                 goto out;
303         }
304         inp->inp_vflag &= ~INP_IPV4;
305         inp->inp_vflag |= INP_IPV6;
306         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
307                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
308                         inp->inp_vflag |= INP_IPV4;
309                 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
310                         struct sockaddr_in sin;
311
312                         in6_sin6_2_sin(&sin, sin6p);
313                         inp->inp_vflag |= INP_IPV4;
314                         inp->inp_vflag &= ~INP_IPV6;
315                         error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
316                         goto out;
317                 }
318         }
319         error = in6_pcbbind(inp, nam, td);
320         if (error)
321                 goto out;
322         COMMON_END(PRU_BIND);
323 }
324 #endif /* INET6 */
325
326 #ifdef SMP
327 struct netmsg_inswildcard {
328         struct netmsg           nm_netmsg;
329         struct inpcb            *nm_inp;
330         struct inpcbinfo        *nm_pcbinfo;
331 };
332
333 static void
334 in_pcbinswildcardhash_handler(struct netmsg *msg0)
335 {
336         struct netmsg_inswildcard *msg = (struct netmsg_inswildcard *)msg0;
337
338         in_pcbinswildcardhash_oncpu(msg->nm_inp, msg->nm_pcbinfo);
339         lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, 0);
340 }
341 #endif
342
343 /*
344  * Prepare to accept connections.
345  */
346 static int
347 tcp_usr_listen(struct socket *so, struct thread *td)
348 {
349         int error = 0;
350         struct inpcb *inp;
351         struct tcpcb *tp;
352 #ifdef SMP
353         int cpu;
354 #endif
355
356         COMMON_START(so, inp, 0);
357         if (inp->inp_lport == 0) {
358                 error = in_pcbbind(inp, NULL, td);
359                 if (error != 0)
360                         goto out;
361         }
362
363         tp->t_state = TCPS_LISTEN;
364         tp->tt_msg = NULL; /* Catch any invalid timer usage */
365 #ifdef SMP
366         /*
367          * We have to set the flag because we can't have other cpus
368          * messing with our inp's flags.
369          */
370         inp->inp_flags |= INP_WILDCARD_MP;
371         for (cpu = 0; cpu < ncpus2; cpu++) {
372                 struct netmsg_inswildcard *msg;
373
374                 if (cpu == mycpu->gd_cpuid) {
375                         in_pcbinswildcardhash(inp);
376                         continue;
377                 }
378
379                 msg = kmalloc(sizeof(struct netmsg_inswildcard), M_LWKTMSG,
380                               M_INTWAIT);
381                 netmsg_init(&msg->nm_netmsg, &netisr_afree_rport, 0,
382                             in_pcbinswildcardhash_handler);
383                 msg->nm_inp = inp;
384                 msg->nm_pcbinfo = &tcbinfo[cpu];
385                 lwkt_sendmsg(tcp_cport(cpu), &msg->nm_netmsg.nm_lmsg);
386         }
387 #else
388         in_pcbinswildcardhash(inp);
389 #endif
390         COMMON_END(PRU_LISTEN);
391 }
392
393 #ifdef INET6
394 static int
395 tcp6_usr_listen(struct socket *so, struct thread *td)
396 {
397         int error = 0;
398         struct inpcb *inp;
399         struct tcpcb *tp;
400 #ifdef SMP
401         int cpu;
402 #endif
403
404         COMMON_START(so, inp, 0);
405         if (inp->inp_lport == 0) {
406                 if (!(inp->inp_flags & IN6P_IPV6_V6ONLY))
407                         inp->inp_vflag |= INP_IPV4;
408                 else
409                         inp->inp_vflag &= ~INP_IPV4;
410                 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
411         }
412         if (error == 0)
413                 tp->t_state = TCPS_LISTEN;
414 #ifdef SMP
415         /*
416          * We have to set the flag because we can't have other cpus
417          * messing with our inp's flags.
418          */
419         inp->inp_flags |= INP_WILDCARD_MP;
420         for (cpu = 0; cpu < ncpus2; cpu++) {
421                 struct netmsg_inswildcard *msg;
422
423                 if (cpu == mycpu->gd_cpuid) {
424                         in_pcbinswildcardhash(inp);
425                         continue;
426                 }
427
428                 msg = kmalloc(sizeof(struct netmsg_inswildcard), M_LWKTMSG,
429                               M_INTWAIT);
430                 netmsg_init(&msg->nm_netmsg, &netisr_afree_rport, 0,
431                             in_pcbinswildcardhash_handler);
432                 msg->nm_inp = inp;
433                 msg->nm_pcbinfo = &tcbinfo[cpu];
434                 lwkt_sendmsg(tcp_cport(cpu), &msg->nm_netmsg.nm_lmsg);
435         }
436 #else
437         in_pcbinswildcardhash(inp);
438 #endif
439         COMMON_END(PRU_LISTEN);
440 }
441 #endif /* INET6 */
442
443 #ifdef SMP
444 static void
445 tcp_output_dispatch(struct netmsg *nmsg)
446 {
447         struct lwkt_msg *msg = &nmsg->nm_lmsg;
448         struct tcpcb *tp = msg->u.ms_resultp;
449         int error;
450
451         error = tcp_output(tp);
452         lwkt_replymsg(msg, error);
453 }
454 #endif
455
456 /*
457  * Initiate connection to peer.
458  * Create a template for use in transmissions on this connection.
459  * Enter SYN_SENT state, and mark socket as connecting.
460  * Start keep-alive timer, and seed output sequence space.
461  * Send initial segment on connection.
462  */
463 static int
464 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
465 {
466         int error = 0;
467         struct inpcb *inp;
468         struct tcpcb *tp;
469         struct sockaddr_in *sinp;
470 #ifdef SMP
471         lwkt_port_t port;
472 #endif
473
474         COMMON_START(so, inp, 0);
475
476         /*
477          * Must disallow TCP ``connections'' to multicast addresses.
478          */
479         sinp = (struct sockaddr_in *)nam;
480         if (sinp->sin_family == AF_INET
481             && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
482                 error = EAFNOSUPPORT;
483                 goto out;
484         }
485
486         if (!prison_remote_ip(td, (struct sockaddr*)sinp)) {
487                 error = EAFNOSUPPORT; /* IPv6 only jail */
488                 goto out;
489         }
490
491         if ((error = tcp_connect(tp, nam, td)) != 0)
492                 goto out;
493
494 #ifdef SMP
495         port = tcp_addrport(inp->inp_faddr.s_addr, inp->inp_fport,
496                             inp->inp_laddr.s_addr, inp->inp_lport);
497         if (port != &curthread->td_msgport) {
498                 struct netmsg nmsg;
499                 struct lwkt_msg *msg;
500
501                 netmsg_init(&nmsg, &curthread->td_msgport, 0,
502                             tcp_output_dispatch);
503                 msg = &nmsg.nm_lmsg;
504                 msg->u.ms_resultp = tp;
505
506                 error = lwkt_domsg(port, msg, 0);
507         } else
508 #endif
509                 error = tcp_output(tp);
510         COMMON_END(PRU_CONNECT);
511 }
512
513 #ifdef INET6
514 static int
515 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
516 {
517         int error = 0;
518         struct inpcb *inp;
519         struct tcpcb *tp;
520         struct sockaddr_in6 *sin6p;
521
522         COMMON_START(so, inp, 0);
523
524         /*
525          * Must disallow TCP ``connections'' to multicast addresses.
526          */
527         sin6p = (struct sockaddr_in6 *)nam;
528         if (sin6p->sin6_family == AF_INET6
529             && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
530                 error = EAFNOSUPPORT;
531                 goto out;
532         }
533
534         if (!prison_remote_ip(td, nam)) {
535                 error = EAFNOSUPPORT; /* IPv4 only jail */
536                 goto out;
537         }
538
539         if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
540                 struct sockaddr_in sin;
541
542                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
543                         error = EINVAL;
544                         goto out;
545                 }
546
547                 in6_sin6_2_sin(&sin, sin6p);
548                 inp->inp_vflag |= INP_IPV4;
549                 inp->inp_vflag &= ~INP_IPV6;
550                 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
551                         goto out;
552                 error = tcp_output(tp);
553                 goto out;
554         }
555         inp->inp_vflag &= ~INP_IPV4;
556         inp->inp_vflag |= INP_IPV6;
557         inp->inp_inc.inc_isipv6 = 1;
558         if ((error = tcp6_connect(tp, nam, td)) != 0)
559                 goto out;
560         error = tcp_output(tp);
561         COMMON_END(PRU_CONNECT);
562 }
563 #endif /* INET6 */
564
565 /*
566  * Initiate disconnect from peer.
567  * If connection never passed embryonic stage, just drop;
568  * else if don't need to let data drain, then can just drop anyways,
569  * else have to begin TCP shutdown process: mark socket disconnecting,
570  * drain unread data, state switch to reflect user close, and
571  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
572  * when peer sends FIN and acks ours.
573  *
574  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
575  */
576 static int
577 tcp_usr_disconnect(struct socket *so)
578 {
579         int error = 0;
580         struct inpcb *inp;
581         struct tcpcb *tp;
582
583         COMMON_START(so, inp, 1);
584         tp = tcp_disconnect(tp);
585         COMMON_END(PRU_DISCONNECT);
586 }
587
588 /*
589  * Accept a connection.  Essentially all the work is
590  * done at higher levels; just return the address
591  * of the peer, storing through addr.
592  */
593 static int
594 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
595 {
596         int error = 0;
597         struct inpcb *inp;
598         struct tcpcb *tp = NULL;
599         TCPDEBUG0;
600
601         crit_enter();
602         inp = so->so_pcb;
603         if (so->so_state & SS_ISDISCONNECTED) {
604                 error = ECONNABORTED;
605                 goto out;
606         }
607         if (inp == 0) {
608                 crit_exit();
609                 return (EINVAL);
610         }
611         tp = intotcpcb(inp);
612         TCPDEBUG1();
613         in_setpeeraddr(so, nam);
614         COMMON_END(PRU_ACCEPT);
615 }
616
617 #ifdef INET6
618 static int
619 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
620 {
621         int error = 0;
622         struct inpcb *inp;
623         struct tcpcb *tp = NULL;
624         TCPDEBUG0;
625
626         crit_enter();
627         inp = so->so_pcb;
628
629         if (so->so_state & SS_ISDISCONNECTED) {
630                 error = ECONNABORTED;
631                 goto out;
632         }
633         if (inp == 0) {
634                 crit_exit();
635                 return (EINVAL);
636         }
637         tp = intotcpcb(inp);
638         TCPDEBUG1();
639         in6_mapped_peeraddr(so, nam);
640         COMMON_END(PRU_ACCEPT);
641 }
642 #endif /* INET6 */
643 /*
644  * Mark the connection as being incapable of further output.
645  */
646 static int
647 tcp_usr_shutdown(struct socket *so)
648 {
649         int error = 0;
650         struct inpcb *inp;
651         struct tcpcb *tp;
652
653         COMMON_START(so, inp, 0);
654         socantsendmore(so);
655         tp = tcp_usrclosed(tp);
656         if (tp)
657                 error = tcp_output(tp);
658         COMMON_END(PRU_SHUTDOWN);
659 }
660
661 /*
662  * After a receive, possibly send window update to peer.
663  */
664 static int
665 tcp_usr_rcvd(struct socket *so, int flags)
666 {
667         int error = 0;
668         struct inpcb *inp;
669         struct tcpcb *tp;
670
671         COMMON_START(so, inp, 0);
672         tcp_output(tp);
673         COMMON_END(PRU_RCVD);
674 }
675
676 /*
677  * Do a send by putting data in output queue and updating urgent
678  * marker if URG set.  Possibly send more data.  Unlike the other
679  * pru_*() routines, the mbuf chains are our responsibility.  We
680  * must either enqueue them or free them.  The other pru_* routines
681  * generally are caller-frees.
682  */
683 static int
684 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
685              struct sockaddr *nam, struct mbuf *control, struct thread *td)
686 {
687         int error = 0;
688         struct inpcb *inp;
689         struct tcpcb *tp;
690 #ifdef INET6
691         int isipv6;
692 #endif
693         TCPDEBUG0;
694
695         crit_enter();
696         inp = so->so_pcb;
697
698         if (inp == NULL) {
699                 /*
700                  * OOPS! we lost a race, the TCP session got reset after
701                  * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
702                  * network interrupt in the non-critical section of sosend().
703                  */
704                 if (m)
705                         m_freem(m);
706                 if (control)
707                         m_freem(control);
708                 error = ECONNRESET;     /* XXX EPIPE? */
709                 tp = NULL;
710                 TCPDEBUG1();
711                 goto out;
712         }
713 #ifdef INET6
714         isipv6 = nam && nam->sa_family == AF_INET6;
715 #endif /* INET6 */
716         tp = intotcpcb(inp);
717         TCPDEBUG1();
718         if (control) {
719                 /* TCP doesn't do control messages (rights, creds, etc) */
720                 if (control->m_len) {
721                         m_freem(control);
722                         if (m)
723                                 m_freem(m);
724                         error = EINVAL;
725                         goto out;
726                 }
727                 m_freem(control);       /* empty control, just free it */
728         }
729         if(!(flags & PRUS_OOB)) {
730                 ssb_appendstream(&so->so_snd, m);
731                 if (nam && tp->t_state < TCPS_SYN_SENT) {
732                         /*
733                          * Do implied connect if not yet connected,
734                          * initialize window to default value, and
735                          * initialize maxseg/maxopd using peer's cached
736                          * MSS.
737                          */
738 #ifdef INET6
739                         if (isipv6)
740                                 error = tcp6_connect(tp, nam, td);
741                         else
742 #endif /* INET6 */
743                         error = tcp_connect(tp, nam, td);
744                         if (error)
745                                 goto out;
746                         tp->snd_wnd = TTCP_CLIENT_SND_WND;
747                         tcp_mss(tp, -1);
748                 }
749
750                 if (flags & PRUS_EOF) {
751                         /*
752                          * Close the send side of the connection after
753                          * the data is sent.
754                          */
755                         socantsendmore(so);
756                         tp = tcp_usrclosed(tp);
757                 }
758                 if (tp != NULL) {
759                         if (flags & PRUS_MORETOCOME)
760                                 tp->t_flags |= TF_MORETOCOME;
761                         error = tcp_output(tp);
762                         if (flags & PRUS_MORETOCOME)
763                                 tp->t_flags &= ~TF_MORETOCOME;
764                 }
765         } else {
766                 if (ssb_space(&so->so_snd) < -512) {
767                         m_freem(m);
768                         error = ENOBUFS;
769                         goto out;
770                 }
771                 /*
772                  * According to RFC961 (Assigned Protocols),
773                  * the urgent pointer points to the last octet
774                  * of urgent data.  We continue, however,
775                  * to consider it to indicate the first octet
776                  * of data past the urgent section.
777                  * Otherwise, snd_up should be one lower.
778                  */
779                 ssb_appendstream(&so->so_snd, m);
780                 if (nam && tp->t_state < TCPS_SYN_SENT) {
781                         /*
782                          * Do implied connect if not yet connected,
783                          * initialize window to default value, and
784                          * initialize maxseg/maxopd using peer's cached
785                          * MSS.
786                          */
787 #ifdef INET6
788                         if (isipv6)
789                                 error = tcp6_connect(tp, nam, td);
790                         else
791 #endif /* INET6 */
792                         error = tcp_connect(tp, nam, td);
793                         if (error)
794                                 goto out;
795                         tp->snd_wnd = TTCP_CLIENT_SND_WND;
796                         tcp_mss(tp, -1);
797                 }
798                 tp->snd_up = tp->snd_una + so->so_snd.ssb_cc;
799                 tp->t_flags |= TF_FORCE;
800                 error = tcp_output(tp);
801                 tp->t_flags &= ~TF_FORCE;
802         }
803         COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
804                    ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
805 }
806
807 /*
808  * Abort the TCP.
809  */
810 static int
811 tcp_usr_abort(struct socket *so)
812 {
813         int error = 0;
814         struct inpcb *inp;
815         struct tcpcb *tp;
816
817         COMMON_START(so, inp, 1);
818         tp = tcp_drop(tp, ECONNABORTED);
819         COMMON_END(PRU_ABORT);
820 }
821
822 /*
823  * Receive out-of-band data.
824  */
825 static int
826 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
827 {
828         int error = 0;
829         struct inpcb *inp;
830         struct tcpcb *tp;
831
832         COMMON_START(so, inp, 0);
833         if ((so->so_oobmark == 0 &&
834              (so->so_state & SS_RCVATMARK) == 0) ||
835             so->so_options & SO_OOBINLINE ||
836             tp->t_oobflags & TCPOOB_HADDATA) {
837                 error = EINVAL;
838                 goto out;
839         }
840         if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
841                 error = EWOULDBLOCK;
842                 goto out;
843         }
844         m->m_len = 1;
845         *mtod(m, caddr_t) = tp->t_iobc;
846         if ((flags & MSG_PEEK) == 0)
847                 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
848         COMMON_END(PRU_RCVOOB);
849 }
850
851 /* xxx - should be const */
852 struct pr_usrreqs tcp_usrreqs = {
853         .pru_abort = tcp_usr_abort,
854         .pru_accept = tcp_usr_accept,
855         .pru_attach = tcp_usr_attach,
856         .pru_bind = tcp_usr_bind,
857         .pru_connect = tcp_usr_connect,
858         .pru_connect2 = pru_connect2_notsupp,
859         .pru_control = in_control,
860         .pru_detach = tcp_usr_detach,
861         .pru_disconnect = tcp_usr_disconnect,
862         .pru_listen = tcp_usr_listen,
863         .pru_peeraddr = in_setpeeraddr,
864         .pru_rcvd = tcp_usr_rcvd,
865         .pru_rcvoob = tcp_usr_rcvoob,
866         .pru_send = tcp_usr_send,
867         .pru_sense = pru_sense_null,
868         .pru_shutdown = tcp_usr_shutdown,
869         .pru_sockaddr = in_setsockaddr,
870         .pru_sosend = sosend,
871         .pru_soreceive = soreceive,
872         .pru_sopoll = sopoll
873 };
874
875 #ifdef INET6
876 struct pr_usrreqs tcp6_usrreqs = {
877         .pru_abort = tcp_usr_abort,
878         .pru_accept = tcp6_usr_accept,
879         .pru_attach = tcp_usr_attach,
880         .pru_bind = tcp6_usr_bind,
881         .pru_connect = tcp6_usr_connect,
882         .pru_connect2 = pru_connect2_notsupp,
883         .pru_control = in6_control,
884         .pru_detach = tcp_usr_detach,
885         .pru_disconnect = tcp_usr_disconnect,
886         .pru_listen = tcp6_usr_listen,
887         .pru_peeraddr = in6_mapped_peeraddr,
888         .pru_rcvd = tcp_usr_rcvd,
889         .pru_rcvoob = tcp_usr_rcvoob,
890         .pru_send = tcp_usr_send,
891         .pru_sense = pru_sense_null,
892         .pru_shutdown = tcp_usr_shutdown,
893         .pru_sockaddr = in6_mapped_sockaddr,
894         .pru_sosend = sosend,
895         .pru_soreceive = soreceive,
896         .pru_sopoll = sopoll
897 };
898 #endif /* INET6 */
899
900 static int
901 tcp_connect_oncpu(struct tcpcb *tp, struct sockaddr_in *sin,
902                   struct sockaddr_in *if_sin)
903 {
904         struct inpcb *inp = tp->t_inpcb, *oinp;
905         struct socket *so = inp->inp_socket;
906         struct tcpcb *otp;
907         struct rmxp_tao *taop;
908         struct rmxp_tao tao_noncached;
909
910         oinp = in_pcblookup_hash(&tcbinfo[mycpu->gd_cpuid],
911             sin->sin_addr, sin->sin_port,
912             inp->inp_laddr.s_addr != INADDR_ANY ?
913                 inp->inp_laddr : if_sin->sin_addr,
914             inp->inp_lport, 0, NULL);
915         if (oinp != NULL) {
916                 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
917                     otp->t_state == TCPS_TIME_WAIT &&
918                     (ticks - otp->t_starttime) < tcp_msl &&
919                     (otp->t_flags & TF_RCVD_CC))
920                         tcp_close(otp);
921                 else
922                         return (EADDRINUSE);
923         }
924         if (inp->inp_laddr.s_addr == INADDR_ANY)
925                 inp->inp_laddr = if_sin->sin_addr;
926         inp->inp_faddr = sin->sin_addr;
927         inp->inp_fport = sin->sin_port;
928         inp->inp_cpcbinfo = &tcbinfo[mycpu->gd_cpuid];
929         in_pcbinsconnhash(inp);
930
931         tcp_create_timermsg(tp);
932
933         /* Compute window scaling to request.  */
934         while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
935             (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.ssb_hiwat)
936                 tp->request_r_scale++;
937
938         soisconnecting(so);
939         tcpstat.tcps_connattempt++;
940         tp->t_state = TCPS_SYN_SENT;
941         tcp_callout_reset(tp, tp->tt_keep, tcp_keepinit, tcp_timer_keep);
942         tp->iss = tcp_new_isn(tp);
943         tcp_sendseqinit(tp);
944
945         /*
946          * Generate a CC value for this connection and
947          * check whether CC or CCnew should be used.
948          */
949         if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
950                 taop = &tao_noncached;
951                 bzero(taop, sizeof *taop);
952         }
953
954         tp->cc_send = CC_INC(tcp_ccgen);
955         if (taop->tao_ccsent != 0 &&
956             CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
957                 taop->tao_ccsent = tp->cc_send;
958         } else {
959                 taop->tao_ccsent = 0;
960                 tp->t_flags |= TF_SENDCCNEW;
961         }
962
963         return (0);
964 }
965
966 #ifdef SMP
967
968 struct netmsg_tcp_connect {
969         struct netmsg           nm_netmsg;
970         struct tcpcb            *nm_tp;
971         struct sockaddr_in      *nm_sin;
972         struct sockaddr_in      *nm_ifsin;
973 };
974
975 static void
976 tcp_connect_handler(netmsg_t netmsg)
977 {
978         struct netmsg_tcp_connect *msg = (void *)netmsg;
979         int error;
980
981         error = tcp_connect_oncpu(msg->nm_tp, msg->nm_sin, msg->nm_ifsin);
982         lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, error);
983 }
984
985 #endif
986
987 /*
988  * Common subroutine to open a TCP connection to remote host specified
989  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
990  * port number if needed.  Call in_pcbladdr to do the routing and to choose
991  * a local host address (interface).  If there is an existing incarnation
992  * of the same connection in TIME-WAIT state and if the remote host was
993  * sending CC options and if the connection duration was < MSL, then
994  * truncate the previous TIME-WAIT state and proceed.
995  * Initialize connection parameters and enter SYN-SENT state.
996  */
997 static int
998 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
999 {
1000         struct inpcb *inp = tp->t_inpcb;
1001         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1002         struct sockaddr_in *if_sin;
1003         int error;
1004 #ifdef SMP
1005         lwkt_port_t port;
1006 #endif
1007
1008         if (inp->inp_lport == 0) {
1009                 error = in_pcbbind(inp, (struct sockaddr *)NULL, td);
1010                 if (error)
1011                         return (error);
1012         }
1013
1014         /*
1015          * Cannot simply call in_pcbconnect, because there might be an
1016          * earlier incarnation of this same connection still in
1017          * TIME_WAIT state, creating an ADDRINUSE error.
1018          */
1019         error = in_pcbladdr(inp, nam, &if_sin, td);
1020         if (error)
1021                 return (error);
1022
1023 #ifdef SMP
1024         port = tcp_addrport(sin->sin_addr.s_addr, sin->sin_port,
1025             inp->inp_laddr.s_addr ?
1026                 inp->inp_laddr.s_addr : if_sin->sin_addr.s_addr,
1027             inp->inp_lport);
1028
1029         if (port != &curthread->td_msgport) {
1030                 struct netmsg_tcp_connect msg;
1031                 struct route *ro = &inp->inp_route;
1032
1033                 /*
1034                  * in_pcbladdr() may have allocated a route entry for us
1035                  * on the current CPU, but we need a route entry on the
1036                  * target CPU, so free it here.
1037                  */
1038                 if (ro->ro_rt != NULL)
1039                         RTFREE(ro->ro_rt);
1040                 bzero(ro, sizeof(*ro));
1041
1042                 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0,
1043                             tcp_connect_handler);
1044                 msg.nm_tp = tp;
1045                 msg.nm_sin = sin;
1046                 msg.nm_ifsin = if_sin;
1047                 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0);
1048         } else
1049 #endif
1050                 error = tcp_connect_oncpu(tp, sin, if_sin);
1051
1052         return (error);
1053 }
1054
1055 #ifdef INET6
1056 static int
1057 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1058 {
1059         struct inpcb *inp = tp->t_inpcb, *oinp;
1060         struct socket *so = inp->inp_socket;
1061         struct tcpcb *otp;
1062         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1063         struct in6_addr *addr6;
1064         struct rmxp_tao *taop;
1065         struct rmxp_tao tao_noncached;
1066         int error;
1067
1068         if (inp->inp_lport == 0) {
1069                 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
1070                 if (error)
1071                         return error;
1072         }
1073
1074         /*
1075          * Cannot simply call in_pcbconnect, because there might be an
1076          * earlier incarnation of this same connection still in
1077          * TIME_WAIT state, creating an ADDRINUSE error.
1078          */
1079         error = in6_pcbladdr(inp, nam, &addr6, td);
1080         if (error)
1081                 return error;
1082         oinp = in6_pcblookup_hash(inp->inp_cpcbinfo,
1083                                   &sin6->sin6_addr, sin6->sin6_port,
1084                                   IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
1085                                       addr6 : &inp->in6p_laddr,
1086                                   inp->inp_lport,  0, NULL);
1087         if (oinp) {
1088                 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
1089                     otp->t_state == TCPS_TIME_WAIT &&
1090                     (ticks - otp->t_starttime) < tcp_msl &&
1091                     (otp->t_flags & TF_RCVD_CC))
1092                         otp = tcp_close(otp);
1093                 else
1094                         return (EADDRINUSE);
1095         }
1096         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1097                 inp->in6p_laddr = *addr6;
1098         inp->in6p_faddr = sin6->sin6_addr;
1099         inp->inp_fport = sin6->sin6_port;
1100         if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != 0)
1101                 inp->in6p_flowinfo = sin6->sin6_flowinfo;
1102         in_pcbinsconnhash(inp);
1103
1104         /* Compute window scaling to request.  */
1105         while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1106             (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.ssb_hiwat)
1107                 tp->request_r_scale++;
1108
1109         soisconnecting(so);
1110         tcpstat.tcps_connattempt++;
1111         tp->t_state = TCPS_SYN_SENT;
1112         tcp_callout_reset(tp, tp->tt_keep, tcp_keepinit, tcp_timer_keep);
1113         tp->iss = tcp_new_isn(tp);
1114         tcp_sendseqinit(tp);
1115
1116         /*
1117          * Generate a CC value for this connection and
1118          * check whether CC or CCnew should be used.
1119          */
1120         if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
1121                 taop = &tao_noncached;
1122                 bzero(taop, sizeof *taop);
1123         }
1124
1125         tp->cc_send = CC_INC(tcp_ccgen);
1126         if (taop->tao_ccsent != 0 &&
1127             CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
1128                 taop->tao_ccsent = tp->cc_send;
1129         } else {
1130                 taop->tao_ccsent = 0;
1131                 tp->t_flags |= TF_SENDCCNEW;
1132         }
1133
1134         return (0);
1135 }
1136 #endif /* INET6 */
1137
1138 /*
1139  * The new sockopt interface makes it possible for us to block in the
1140  * copyin/out step (if we take a page fault).  Taking a page fault while
1141  * in a critical section is probably a Bad Thing.  (Since sockets and pcbs
1142  * both now use TSM, there probably isn't any need for this function to 
1143  * run in a critical section any more.  This needs more examination.)
1144  */
1145 int
1146 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1147 {
1148         int     error, opt, optval;
1149         struct  inpcb *inp;
1150         struct  tcpcb *tp;
1151
1152         error = 0;
1153         crit_enter();           /* XXX */
1154         inp = so->so_pcb;
1155         if (inp == NULL) {
1156                 crit_exit();
1157                 return (ECONNRESET);
1158         }
1159         if (sopt->sopt_level != IPPROTO_TCP) {
1160 #ifdef INET6
1161                 if (INP_CHECK_SOCKAF(so, AF_INET6))
1162                         error = ip6_ctloutput(so, sopt);
1163                 else
1164 #endif /* INET6 */
1165                 error = ip_ctloutput(so, sopt);
1166                 crit_exit();
1167                 return (error);
1168         }
1169         tp = intotcpcb(inp);
1170
1171         switch (sopt->sopt_dir) {
1172         case SOPT_SET:
1173                 error = soopt_to_kbuf(sopt, &optval, sizeof optval,
1174                                       sizeof optval);
1175                 if (error)
1176                         break;
1177                 switch (sopt->sopt_name) {
1178                 case TCP_NODELAY:
1179                 case TCP_NOOPT:
1180                         switch (sopt->sopt_name) {
1181                         case TCP_NODELAY:
1182                                 opt = TF_NODELAY;
1183                                 break;
1184                         case TCP_NOOPT:
1185                                 opt = TF_NOOPT;
1186                                 break;
1187                         default:
1188                                 opt = 0; /* dead code to fool gcc */
1189                                 break;
1190                         }
1191
1192                         if (optval)
1193                                 tp->t_flags |= opt;
1194                         else
1195                                 tp->t_flags &= ~opt;
1196                         break;
1197
1198                 case TCP_NOPUSH:
1199                         if (optval)
1200                                 tp->t_flags |= TF_NOPUSH;
1201                         else {
1202                                 tp->t_flags &= ~TF_NOPUSH;
1203                                 error = tcp_output(tp);
1204                         }
1205                         break;
1206
1207                 case TCP_MAXSEG:
1208                         if (optval > 0 && optval <= tp->t_maxseg)
1209                                 tp->t_maxseg = optval;
1210                         else
1211                                 error = EINVAL;
1212                         break;
1213
1214                 default:
1215                         error = ENOPROTOOPT;
1216                         break;
1217                 }
1218                 break;
1219
1220         case SOPT_GET:
1221                 switch (sopt->sopt_name) {
1222                 case TCP_NODELAY:
1223                         optval = tp->t_flags & TF_NODELAY;
1224                         break;
1225                 case TCP_MAXSEG:
1226                         optval = tp->t_maxseg;
1227                         break;
1228                 case TCP_NOOPT:
1229                         optval = tp->t_flags & TF_NOOPT;
1230                         break;
1231                 case TCP_NOPUSH:
1232                         optval = tp->t_flags & TF_NOPUSH;
1233                         break;
1234                 default:
1235                         error = ENOPROTOOPT;
1236                         break;
1237                 }
1238                 if (error == 0)
1239                         soopt_from_kbuf(sopt, &optval, sizeof optval);
1240                 break;
1241         }
1242         crit_exit();
1243         return (error);
1244 }
1245
1246 /*
1247  * tcp_sendspace and tcp_recvspace are the default send and receive window
1248  * sizes, respectively.  These are obsolescent (this information should
1249  * be set by the route).
1250  *
1251  * Use a default that does not require tcp window scaling to be turned
1252  * on.  Individual programs or the administrator can increase the default.
1253  */
1254 u_long  tcp_sendspace = 57344;  /* largest multiple of PAGE_SIZE < 64k */
1255 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1256     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1257 u_long  tcp_recvspace = 57344;  /* largest multiple of PAGE_SIZE < 64k */
1258 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1259     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1260
1261 /*
1262  * Attach TCP protocol to socket, allocating
1263  * internet protocol control block, tcp control block,
1264  * bufer space, and entering LISTEN state if to accept connections.
1265  */
1266 static int
1267 tcp_attach(struct socket *so, struct pru_attach_info *ai)
1268 {
1269         struct tcpcb *tp;
1270         struct inpcb *inp;
1271         int error;
1272         int cpu;
1273 #ifdef INET6
1274         int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1275 #endif
1276
1277         if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) {
1278                 error = soreserve(so, tcp_sendspace, tcp_recvspace,
1279                                   ai->sb_rlimit);
1280                 if (error)
1281                         return (error);
1282         }
1283         cpu = mycpu->gd_cpuid;
1284         error = in_pcballoc(so, &tcbinfo[cpu]);
1285         if (error)
1286                 return (error);
1287         inp = so->so_pcb;
1288 #ifdef INET6
1289         if (isipv6) {
1290                 inp->inp_vflag |= INP_IPV6;
1291                 inp->in6p_hops = -1;    /* use kernel default */
1292         }
1293         else
1294 #endif
1295         inp->inp_vflag |= INP_IPV4;
1296         tp = tcp_newtcpcb(inp);
1297         if (tp == 0) {
1298                 int nofd = so->so_state & SS_NOFDREF;   /* XXX */
1299
1300                 so->so_state &= ~SS_NOFDREF;    /* don't free the socket yet */
1301 #ifdef INET6
1302                 if (isipv6)
1303                         in6_pcbdetach(inp);
1304                 else
1305 #endif
1306                 in_pcbdetach(inp);
1307                 so->so_state |= nofd;
1308                 return (ENOBUFS);
1309         }
1310         tp->t_state = TCPS_CLOSED;
1311         return (0);
1312 }
1313
1314 /*
1315  * Initiate (or continue) disconnect.
1316  * If embryonic state, just send reset (once).
1317  * If in ``let data drain'' option and linger null, just drop.
1318  * Otherwise (hard), mark socket disconnecting and drop
1319  * current input data; switch states based on user close, and
1320  * send segment to peer (with FIN).
1321  */
1322 static struct tcpcb *
1323 tcp_disconnect(struct tcpcb *tp)
1324 {
1325         struct socket *so = tp->t_inpcb->inp_socket;
1326
1327         if (tp->t_state < TCPS_ESTABLISHED)
1328                 tp = tcp_close(tp);
1329         else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1330                 tp = tcp_drop(tp, 0);
1331         else {
1332                 soisdisconnecting(so);
1333                 sbflush(&so->so_rcv.sb);
1334                 tp = tcp_usrclosed(tp);
1335                 if (tp)
1336                         tcp_output(tp);
1337         }
1338         return (tp);
1339 }
1340
1341 /*
1342  * User issued close, and wish to trail through shutdown states:
1343  * if never received SYN, just forget it.  If got a SYN from peer,
1344  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1345  * If already got a FIN from peer, then almost done; go to LAST_ACK
1346  * state.  In all other cases, have already sent FIN to peer (e.g.
1347  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1348  * for peer to send FIN or not respond to keep-alives, etc.
1349  * We can let the user exit from the close as soon as the FIN is acked.
1350  */
1351 static struct tcpcb *
1352 tcp_usrclosed(struct tcpcb *tp)
1353 {
1354
1355         switch (tp->t_state) {
1356
1357         case TCPS_CLOSED:
1358         case TCPS_LISTEN:
1359                 tp->t_state = TCPS_CLOSED;
1360                 tp = tcp_close(tp);
1361                 break;
1362
1363         case TCPS_SYN_SENT:
1364         case TCPS_SYN_RECEIVED:
1365                 tp->t_flags |= TF_NEEDFIN;
1366                 break;
1367
1368         case TCPS_ESTABLISHED:
1369                 tp->t_state = TCPS_FIN_WAIT_1;
1370                 break;
1371
1372         case TCPS_CLOSE_WAIT:
1373                 tp->t_state = TCPS_LAST_ACK;
1374                 break;
1375         }
1376         if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1377                 soisdisconnected(tp->t_inpcb->inp_socket);
1378                 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1379                 if (tp->t_state == TCPS_FIN_WAIT_2) {
1380                         tcp_callout_reset(tp, tp->tt_2msl, tcp_maxidle,
1381                             tcp_timer_2msl);
1382                 }
1383         }
1384         return (tp);
1385 }