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