Resync patch with contrib.
[dragonfly.git] / sys / netinet / tcp_usrreq.c
blobc711c7d6536853f687dba4532c058d4e4ce160e8
1 /*
2 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Jeffrey M. Hsu.
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.
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.
35 * Copyright (c) 1982, 1986, 1988, 1993
36 * The Regents of the University of California. All rights reserved.
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.
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.
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.45 2007/05/24 20:51:22 dillon Exp $
71 #include "opt_ipsec.h"
72 #include "opt_inet6.h"
73 #include "opt_tcpdebug.h"
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>
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>
91 #include <sys/thread2.h>
92 #include <sys/msgport2.h>
94 #include <net/if.h>
95 #include <net/netisr.h>
96 #include <net/route.h>
98 #include <net/netmsg2.h>
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_var.h>
119 #include <netinet/tcpip.h>
120 #ifdef TCPDEBUG
121 #include <netinet/tcp_debug.h>
122 #endif
124 #ifdef IPSEC
125 #include <netinet6/ipsec.h>
126 #endif /*IPSEC*/
129 * TCP protocol interface to socket abstraction.
131 extern char *tcpstates[]; /* XXX ??? */
133 static int tcp_attach (struct socket *, struct pru_attach_info *);
134 static int tcp_connect (struct tcpcb *, struct sockaddr *,
135 struct thread *);
136 #ifdef INET6
137 static int tcp6_connect (struct tcpcb *, struct sockaddr *,
138 struct thread *);
139 #endif /* INET6 */
140 static struct tcpcb *
141 tcp_disconnect (struct tcpcb *);
142 static struct tcpcb *
143 tcp_usrclosed (struct tcpcb *);
145 #ifdef TCPDEBUG
146 #define TCPDEBUG0 int ostate = 0
147 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
148 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
149 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
150 #else
151 #define TCPDEBUG0
152 #define TCPDEBUG1()
153 #define TCPDEBUG2(req)
154 #endif
157 * TCP attaches to socket via pru_attach(), reserving space,
158 * and an internet control block.
160 static int
161 tcp_usr_attach(struct socket *so, int proto, struct pru_attach_info *ai)
163 int error;
164 struct inpcb *inp;
165 struct tcpcb *tp = 0;
166 TCPDEBUG0;
168 crit_enter();
169 inp = so->so_pcb;
170 TCPDEBUG1();
171 if (inp) {
172 error = EISCONN;
173 goto out;
176 error = tcp_attach(so, ai);
177 if (error)
178 goto out;
180 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
181 so->so_linger = TCP_LINGERTIME;
182 tp = sototcpcb(so);
183 out:
184 TCPDEBUG2(PRU_ATTACH);
185 crit_exit();
186 return error;
190 * pru_detach() detaches the TCP protocol from the socket.
191 * If the protocol state is non-embryonic, then can't
192 * do this directly: have to initiate a pru_disconnect(),
193 * which may finish later; embryonic TCB's can just
194 * be discarded here.
196 static int
197 tcp_usr_detach(struct socket *so)
199 int error = 0;
200 struct inpcb *inp;
201 struct tcpcb *tp;
202 TCPDEBUG0;
204 crit_enter();
205 inp = so->so_pcb;
206 if (inp == NULL) {
207 crit_exit();
208 return EINVAL; /* XXX */
212 * It's possible for the tcpcb (tp) to disconnect from the inp due
213 * to tcp_drop()->tcp_close() being called. This may occur *after*
214 * the detach message has been queued so we may find a NULL tp here.
216 if ((tp = intotcpcb(inp)) != NULL) {
217 TCPDEBUG1();
218 tp = tcp_disconnect(tp);
219 TCPDEBUG2(PRU_DETACH);
221 crit_exit();
222 return error;
225 #define COMMON_START(so, inp) \
226 TCPDEBUG0; \
228 crit_enter(); \
229 inp = so->so_pcb; \
230 do { \
231 if (inp == 0) { \
232 crit_exit(); \
233 return EINVAL; \
235 tp = intotcpcb(inp); \
236 TCPDEBUG1(); \
237 } while(0)
239 #define COMMON_END(req) out: TCPDEBUG2(req); crit_exit(); return error; goto out
243 * Give the socket an address.
245 static int
246 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
248 int error = 0;
249 struct inpcb *inp;
250 struct tcpcb *tp;
251 struct sockaddr_in *sinp;
253 COMMON_START(so, inp);
256 * Must check for multicast addresses and disallow binding
257 * to them.
259 sinp = (struct sockaddr_in *)nam;
260 if (sinp->sin_family == AF_INET &&
261 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
262 error = EAFNOSUPPORT;
263 goto out;
265 error = in_pcbbind(inp, nam, td);
266 if (error)
267 goto out;
268 COMMON_END(PRU_BIND);
272 #ifdef INET6
273 static int
274 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
276 int error = 0;
277 struct inpcb *inp;
278 struct tcpcb *tp;
279 struct sockaddr_in6 *sin6p;
281 COMMON_START(so, inp);
284 * Must check for multicast addresses and disallow binding
285 * to them.
287 sin6p = (struct sockaddr_in6 *)nam;
288 if (sin6p->sin6_family == AF_INET6 &&
289 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
290 error = EAFNOSUPPORT;
291 goto out;
293 inp->inp_vflag &= ~INP_IPV4;
294 inp->inp_vflag |= INP_IPV6;
295 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
296 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
297 inp->inp_vflag |= INP_IPV4;
298 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
299 struct sockaddr_in sin;
301 in6_sin6_2_sin(&sin, sin6p);
302 inp->inp_vflag |= INP_IPV4;
303 inp->inp_vflag &= ~INP_IPV6;
304 error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
305 goto out;
308 error = in6_pcbbind(inp, nam, td);
309 if (error)
310 goto out;
311 COMMON_END(PRU_BIND);
313 #endif /* INET6 */
315 #ifdef SMP
316 struct netmsg_inswildcard {
317 struct netmsg nm_netmsg;
318 struct inpcb *nm_inp;
319 struct inpcbinfo *nm_pcbinfo;
322 static void
323 in_pcbinswildcardhash_handler(struct netmsg *msg0)
325 struct netmsg_inswildcard *msg = (struct netmsg_inswildcard *)msg0;
327 in_pcbinswildcardhash_oncpu(msg->nm_inp, msg->nm_pcbinfo);
328 lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, 0);
330 #endif
333 * Prepare to accept connections.
335 static int
336 tcp_usr_listen(struct socket *so, struct thread *td)
338 int error = 0;
339 struct inpcb *inp;
340 struct tcpcb *tp;
341 #ifdef SMP
342 int cpu;
343 #endif
345 COMMON_START(so, inp);
346 if (inp->inp_lport == 0) {
347 error = in_pcbbind(inp, NULL, td);
348 if (error != 0)
349 goto out;
352 tp->t_state = TCPS_LISTEN;
353 #ifdef SMP
355 * We have to set the flag because we can't have other cpus
356 * messing with our inp's flags.
358 inp->inp_flags |= INP_WILDCARD_MP;
359 for (cpu = 0; cpu < ncpus2; cpu++) {
360 struct netmsg_inswildcard *msg;
362 if (cpu == mycpu->gd_cpuid) {
363 in_pcbinswildcardhash(inp);
364 continue;
367 msg = kmalloc(sizeof(struct netmsg_inswildcard), M_LWKTMSG,
368 M_INTWAIT);
369 netmsg_init(&msg->nm_netmsg, &netisr_afree_rport, 0,
370 in_pcbinswildcardhash_handler);
371 msg->nm_inp = inp;
372 msg->nm_pcbinfo = &tcbinfo[cpu];
373 lwkt_sendmsg(tcp_cport(cpu), &msg->nm_netmsg.nm_lmsg);
375 #else
376 in_pcbinswildcardhash(inp);
377 #endif
378 COMMON_END(PRU_LISTEN);
381 #ifdef INET6
382 static int
383 tcp6_usr_listen(struct socket *so, struct thread *td)
385 int error = 0;
386 struct inpcb *inp;
387 struct tcpcb *tp;
388 #ifdef SMP
389 int cpu;
390 #endif
392 COMMON_START(so, inp);
393 if (inp->inp_lport == 0) {
394 if (!(inp->inp_flags & IN6P_IPV6_V6ONLY))
395 inp->inp_vflag |= INP_IPV4;
396 else
397 inp->inp_vflag &= ~INP_IPV4;
398 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
400 if (error == 0)
401 tp->t_state = TCPS_LISTEN;
402 #ifdef SMP
404 * We have to set the flag because we can't have other cpus
405 * messing with our inp's flags.
407 inp->inp_flags |= INP_WILDCARD_MP;
408 for (cpu = 0; cpu < ncpus2; cpu++) {
409 struct netmsg_inswildcard *msg;
411 if (cpu == mycpu->gd_cpuid) {
412 in_pcbinswildcardhash(inp);
413 continue;
416 msg = kmalloc(sizeof(struct netmsg_inswildcard), M_LWKTMSG,
417 M_INTWAIT);
418 netmsg_init(&msg->nm_netmsg, &netisr_afree_rport, 0,
419 in_pcbinswildcardhash_handler);
420 msg->nm_inp = inp;
421 msg->nm_pcbinfo = &tcbinfo[cpu];
422 lwkt_sendmsg(tcp_cport(cpu), &msg->nm_netmsg.nm_lmsg);
424 #else
425 in_pcbinswildcardhash(inp);
426 #endif
427 COMMON_END(PRU_LISTEN);
429 #endif /* INET6 */
432 * Initiate connection to peer.
433 * Create a template for use in transmissions on this connection.
434 * Enter SYN_SENT state, and mark socket as connecting.
435 * Start keep-alive timer, and seed output sequence space.
436 * Send initial segment on connection.
438 static int
439 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
441 int error = 0;
442 struct inpcb *inp;
443 struct tcpcb *tp;
444 struct sockaddr_in *sinp;
446 COMMON_START(so, inp);
449 * Must disallow TCP ``connections'' to multicast addresses.
451 sinp = (struct sockaddr_in *)nam;
452 if (sinp->sin_family == AF_INET
453 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
454 error = EAFNOSUPPORT;
455 goto out;
458 if (!prison_remote_ip(td, (struct sockaddr*)sinp)) {
459 error = EAFNOSUPPORT; /* IPv6 only jail */
460 goto out;
463 if ((error = tcp_connect(tp, nam, td)) != 0)
464 goto out;
465 error = tcp_output(tp);
466 COMMON_END(PRU_CONNECT);
469 #ifdef INET6
470 static int
471 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
473 int error = 0;
474 struct inpcb *inp;
475 struct tcpcb *tp;
476 struct sockaddr_in6 *sin6p;
478 COMMON_START(so, inp);
481 * Must disallow TCP ``connections'' to multicast addresses.
483 sin6p = (struct sockaddr_in6 *)nam;
484 if (sin6p->sin6_family == AF_INET6
485 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
486 error = EAFNOSUPPORT;
487 goto out;
490 if (!prison_remote_ip(td, nam)) {
491 error = EAFNOSUPPORT; /* IPv4 only jail */
492 goto out;
495 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
496 struct sockaddr_in sin;
498 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
499 error = EINVAL;
500 goto out;
503 in6_sin6_2_sin(&sin, sin6p);
504 inp->inp_vflag |= INP_IPV4;
505 inp->inp_vflag &= ~INP_IPV6;
506 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
507 goto out;
508 error = tcp_output(tp);
509 goto out;
511 inp->inp_vflag &= ~INP_IPV4;
512 inp->inp_vflag |= INP_IPV6;
513 inp->inp_inc.inc_isipv6 = 1;
514 if ((error = tcp6_connect(tp, nam, td)) != 0)
515 goto out;
516 error = tcp_output(tp);
517 COMMON_END(PRU_CONNECT);
519 #endif /* INET6 */
522 * Initiate disconnect from peer.
523 * If connection never passed embryonic stage, just drop;
524 * else if don't need to let data drain, then can just drop anyways,
525 * else have to begin TCP shutdown process: mark socket disconnecting,
526 * drain unread data, state switch to reflect user close, and
527 * send segment (e.g. FIN) to peer. Socket will be really disconnected
528 * when peer sends FIN and acks ours.
530 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
532 static int
533 tcp_usr_disconnect(struct socket *so)
535 int error = 0;
536 struct inpcb *inp;
537 struct tcpcb *tp;
539 COMMON_START(so, inp);
540 tp = tcp_disconnect(tp);
541 COMMON_END(PRU_DISCONNECT);
545 * Accept a connection. Essentially all the work is
546 * done at higher levels; just return the address
547 * of the peer, storing through addr.
549 static int
550 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
552 int error = 0;
553 struct inpcb *inp;
554 struct tcpcb *tp = NULL;
555 TCPDEBUG0;
557 crit_enter();
558 inp = so->so_pcb;
559 if (so->so_state & SS_ISDISCONNECTED) {
560 error = ECONNABORTED;
561 goto out;
563 if (inp == 0) {
564 crit_exit();
565 return (EINVAL);
567 tp = intotcpcb(inp);
568 TCPDEBUG1();
569 in_setpeeraddr(so, nam);
570 COMMON_END(PRU_ACCEPT);
573 #ifdef INET6
574 static int
575 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
577 int error = 0;
578 struct inpcb *inp;
579 struct tcpcb *tp = NULL;
580 TCPDEBUG0;
582 crit_enter();
583 inp = so->so_pcb;
585 if (so->so_state & SS_ISDISCONNECTED) {
586 error = ECONNABORTED;
587 goto out;
589 if (inp == 0) {
590 crit_exit();
591 return (EINVAL);
593 tp = intotcpcb(inp);
594 TCPDEBUG1();
595 in6_mapped_peeraddr(so, nam);
596 COMMON_END(PRU_ACCEPT);
598 #endif /* INET6 */
600 * Mark the connection as being incapable of further output.
602 static int
603 tcp_usr_shutdown(struct socket *so)
605 int error = 0;
606 struct inpcb *inp;
607 struct tcpcb *tp;
609 COMMON_START(so, inp);
610 socantsendmore(so);
611 tp = tcp_usrclosed(tp);
612 if (tp)
613 error = tcp_output(tp);
614 COMMON_END(PRU_SHUTDOWN);
618 * After a receive, possibly send window update to peer.
620 static int
621 tcp_usr_rcvd(struct socket *so, int flags)
623 int error = 0;
624 struct inpcb *inp;
625 struct tcpcb *tp;
627 COMMON_START(so, inp);
628 tcp_output(tp);
629 COMMON_END(PRU_RCVD);
633 * Do a send by putting data in output queue and updating urgent
634 * marker if URG set. Possibly send more data. Unlike the other
635 * pru_*() routines, the mbuf chains are our responsibility. We
636 * must either enqueue them or free them. The other pru_* routines
637 * generally are caller-frees.
639 static int
640 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
641 struct sockaddr *nam, struct mbuf *control, struct thread *td)
643 int error = 0;
644 struct inpcb *inp;
645 struct tcpcb *tp;
646 #ifdef INET6
647 int isipv6;
648 #endif
649 TCPDEBUG0;
651 crit_enter();
652 inp = so->so_pcb;
654 if (inp == NULL) {
656 * OOPS! we lost a race, the TCP session got reset after
657 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
658 * network interrupt in the non-critical section of sosend().
660 if (m)
661 m_freem(m);
662 if (control)
663 m_freem(control);
664 error = ECONNRESET; /* XXX EPIPE? */
665 tp = NULL;
666 TCPDEBUG1();
667 goto out;
669 #ifdef INET6
670 isipv6 = nam && nam->sa_family == AF_INET6;
671 #endif /* INET6 */
672 tp = intotcpcb(inp);
673 TCPDEBUG1();
674 if (control) {
675 /* TCP doesn't do control messages (rights, creds, etc) */
676 if (control->m_len) {
677 m_freem(control);
678 if (m)
679 m_freem(m);
680 error = EINVAL;
681 goto out;
683 m_freem(control); /* empty control, just free it */
685 if(!(flags & PRUS_OOB)) {
686 ssb_appendstream(&so->so_snd, m);
687 if (nam && tp->t_state < TCPS_SYN_SENT) {
689 * Do implied connect if not yet connected,
690 * initialize window to default value, and
691 * initialize maxseg/maxopd using peer's cached
692 * MSS.
694 #ifdef INET6
695 if (isipv6)
696 error = tcp6_connect(tp, nam, td);
697 else
698 #endif /* INET6 */
699 error = tcp_connect(tp, nam, td);
700 if (error)
701 goto out;
702 tp->snd_wnd = TTCP_CLIENT_SND_WND;
703 tcp_mss(tp, -1);
706 if (flags & PRUS_EOF) {
708 * Close the send side of the connection after
709 * the data is sent.
711 socantsendmore(so);
712 tp = tcp_usrclosed(tp);
714 if (tp != NULL) {
715 if (flags & PRUS_MORETOCOME)
716 tp->t_flags |= TF_MORETOCOME;
717 error = tcp_output(tp);
718 if (flags & PRUS_MORETOCOME)
719 tp->t_flags &= ~TF_MORETOCOME;
721 } else {
722 if (ssb_space(&so->so_snd) < -512) {
723 m_freem(m);
724 error = ENOBUFS;
725 goto out;
728 * According to RFC961 (Assigned Protocols),
729 * the urgent pointer points to the last octet
730 * of urgent data. We continue, however,
731 * to consider it to indicate the first octet
732 * of data past the urgent section.
733 * Otherwise, snd_up should be one lower.
735 ssb_appendstream(&so->so_snd, m);
736 if (nam && tp->t_state < TCPS_SYN_SENT) {
738 * Do implied connect if not yet connected,
739 * initialize window to default value, and
740 * initialize maxseg/maxopd using peer's cached
741 * MSS.
743 #ifdef INET6
744 if (isipv6)
745 error = tcp6_connect(tp, nam, td);
746 else
747 #endif /* INET6 */
748 error = tcp_connect(tp, nam, td);
749 if (error)
750 goto out;
751 tp->snd_wnd = TTCP_CLIENT_SND_WND;
752 tcp_mss(tp, -1);
754 tp->snd_up = tp->snd_una + so->so_snd.ssb_cc;
755 tp->t_flags |= TF_FORCE;
756 error = tcp_output(tp);
757 tp->t_flags &= ~TF_FORCE;
759 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
760 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
764 * Abort the TCP.
766 static int
767 tcp_usr_abort(struct socket *so)
769 int error = 0;
770 struct inpcb *inp;
771 struct tcpcb *tp;
773 COMMON_START(so, inp);
774 tp = tcp_drop(tp, ECONNABORTED);
775 COMMON_END(PRU_ABORT);
779 * Receive out-of-band data.
781 static int
782 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
784 int error = 0;
785 struct inpcb *inp;
786 struct tcpcb *tp;
788 COMMON_START(so, inp);
789 if ((so->so_oobmark == 0 &&
790 (so->so_state & SS_RCVATMARK) == 0) ||
791 so->so_options & SO_OOBINLINE ||
792 tp->t_oobflags & TCPOOB_HADDATA) {
793 error = EINVAL;
794 goto out;
796 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
797 error = EWOULDBLOCK;
798 goto out;
800 m->m_len = 1;
801 *mtod(m, caddr_t) = tp->t_iobc;
802 if ((flags & MSG_PEEK) == 0)
803 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
804 COMMON_END(PRU_RCVOOB);
807 /* xxx - should be const */
808 struct pr_usrreqs tcp_usrreqs = {
809 .pru_abort = tcp_usr_abort,
810 .pru_accept = tcp_usr_accept,
811 .pru_attach = tcp_usr_attach,
812 .pru_bind = tcp_usr_bind,
813 .pru_connect = tcp_usr_connect,
814 .pru_connect2 = pru_connect2_notsupp,
815 .pru_control = in_control,
816 .pru_detach = tcp_usr_detach,
817 .pru_disconnect = tcp_usr_disconnect,
818 .pru_listen = tcp_usr_listen,
819 .pru_peeraddr = in_setpeeraddr,
820 .pru_rcvd = tcp_usr_rcvd,
821 .pru_rcvoob = tcp_usr_rcvoob,
822 .pru_send = tcp_usr_send,
823 .pru_sense = pru_sense_null,
824 .pru_shutdown = tcp_usr_shutdown,
825 .pru_sockaddr = in_setsockaddr,
826 .pru_sosend = sosend,
827 .pru_soreceive = soreceive,
828 .pru_sopoll = sopoll
831 #ifdef INET6
832 struct pr_usrreqs tcp6_usrreqs = {
833 .pru_abort = tcp_usr_abort,
834 .pru_accept = tcp6_usr_accept,
835 .pru_attach = tcp_usr_attach,
836 .pru_bind = tcp6_usr_bind,
837 .pru_connect = tcp6_usr_connect,
838 .pru_connect2 = pru_connect2_notsupp,
839 .pru_control = in6_control,
840 .pru_detach = tcp_usr_detach,
841 .pru_disconnect = tcp_usr_disconnect,
842 .pru_listen = tcp6_usr_listen,
843 .pru_peeraddr = in6_mapped_peeraddr,
844 .pru_rcvd = tcp_usr_rcvd,
845 .pru_rcvoob = tcp_usr_rcvoob,
846 .pru_send = tcp_usr_send,
847 .pru_sense = pru_sense_null,
848 .pru_shutdown = tcp_usr_shutdown,
849 .pru_sockaddr = in6_mapped_sockaddr,
850 .pru_sosend = sosend,
851 .pru_soreceive = soreceive,
852 .pru_sopoll = sopoll
854 #endif /* INET6 */
856 static int
857 tcp_connect_oncpu(struct tcpcb *tp, struct sockaddr_in *sin,
858 struct sockaddr_in *if_sin)
860 struct inpcb *inp = tp->t_inpcb, *oinp;
861 struct socket *so = inp->inp_socket;
862 struct tcpcb *otp;
863 struct rmxp_tao *taop;
864 struct rmxp_tao tao_noncached;
866 oinp = in_pcblookup_hash(&tcbinfo[mycpu->gd_cpuid],
867 sin->sin_addr, sin->sin_port,
868 inp->inp_laddr.s_addr != INADDR_ANY ?
869 inp->inp_laddr : if_sin->sin_addr,
870 inp->inp_lport, 0, NULL);
871 if (oinp != NULL) {
872 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
873 otp->t_state == TCPS_TIME_WAIT &&
874 (ticks - otp->t_starttime) < tcp_msl &&
875 (otp->t_flags & TF_RCVD_CC))
876 tcp_close(otp);
877 else
878 return (EADDRINUSE);
880 if (inp->inp_laddr.s_addr == INADDR_ANY)
881 inp->inp_laddr = if_sin->sin_addr;
882 inp->inp_faddr = sin->sin_addr;
883 inp->inp_fport = sin->sin_port;
884 inp->inp_cpcbinfo = &tcbinfo[mycpu->gd_cpuid];
885 in_pcbinsconnhash(inp);
887 /* Compute window scaling to request. */
888 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
889 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.ssb_hiwat)
890 tp->request_r_scale++;
892 soisconnecting(so);
893 tcpstat.tcps_connattempt++;
894 tp->t_state = TCPS_SYN_SENT;
895 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
896 tp->iss = tcp_new_isn(tp);
897 tcp_sendseqinit(tp);
900 * Generate a CC value for this connection and
901 * check whether CC or CCnew should be used.
903 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
904 taop = &tao_noncached;
905 bzero(taop, sizeof *taop);
908 tp->cc_send = CC_INC(tcp_ccgen);
909 if (taop->tao_ccsent != 0 &&
910 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
911 taop->tao_ccsent = tp->cc_send;
912 } else {
913 taop->tao_ccsent = 0;
914 tp->t_flags |= TF_SENDCCNEW;
917 return (0);
920 #ifdef SMP
922 struct netmsg_tcp_connect {
923 struct netmsg nm_netmsg;
924 struct tcpcb *nm_tp;
925 struct sockaddr_in *nm_sin;
926 struct sockaddr_in *nm_ifsin;
929 static void
930 tcp_connect_handler(netmsg_t netmsg)
932 struct netmsg_tcp_connect *msg = (void *)netmsg;
933 int error;
935 error = tcp_connect_oncpu(msg->nm_tp, msg->nm_sin, msg->nm_ifsin);
936 lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, error);
939 #endif
942 * Common subroutine to open a TCP connection to remote host specified
943 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
944 * port number if needed. Call in_pcbladdr to do the routing and to choose
945 * a local host address (interface). If there is an existing incarnation
946 * of the same connection in TIME-WAIT state and if the remote host was
947 * sending CC options and if the connection duration was < MSL, then
948 * truncate the previous TIME-WAIT state and proceed.
949 * Initialize connection parameters and enter SYN-SENT state.
951 static int
952 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
954 struct inpcb *inp = tp->t_inpcb;
955 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
956 struct sockaddr_in *if_sin;
957 int error;
958 #ifdef SMP
959 lwkt_port_t port;
960 #endif
962 if (inp->inp_lport == 0) {
963 error = in_pcbbind(inp, (struct sockaddr *)NULL, td);
964 if (error)
965 return (error);
969 * Cannot simply call in_pcbconnect, because there might be an
970 * earlier incarnation of this same connection still in
971 * TIME_WAIT state, creating an ADDRINUSE error.
973 error = in_pcbladdr(inp, nam, &if_sin, td);
974 if (error)
975 return (error);
977 #ifdef SMP
978 port = tcp_addrport(sin->sin_addr.s_addr, sin->sin_port,
979 inp->inp_laddr.s_addr ?
980 inp->inp_laddr.s_addr : if_sin->sin_addr.s_addr,
981 inp->inp_lport);
983 if (port != &curthread->td_msgport) {
984 struct netmsg_tcp_connect msg;
986 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0,
987 tcp_connect_handler);
988 msg.nm_tp = tp;
989 msg.nm_sin = sin;
990 msg.nm_ifsin = if_sin;
991 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0);
992 } else
993 #endif
994 error = tcp_connect_oncpu(tp, sin, if_sin);
996 return (error);
999 #ifdef INET6
1000 static int
1001 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1003 struct inpcb *inp = tp->t_inpcb, *oinp;
1004 struct socket *so = inp->inp_socket;
1005 struct tcpcb *otp;
1006 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1007 struct in6_addr *addr6;
1008 struct rmxp_tao *taop;
1009 struct rmxp_tao tao_noncached;
1010 int error;
1012 if (inp->inp_lport == 0) {
1013 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
1014 if (error)
1015 return error;
1019 * Cannot simply call in_pcbconnect, because there might be an
1020 * earlier incarnation of this same connection still in
1021 * TIME_WAIT state, creating an ADDRINUSE error.
1023 error = in6_pcbladdr(inp, nam, &addr6, td);
1024 if (error)
1025 return error;
1026 oinp = in6_pcblookup_hash(inp->inp_cpcbinfo,
1027 &sin6->sin6_addr, sin6->sin6_port,
1028 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
1029 addr6 : &inp->in6p_laddr,
1030 inp->inp_lport, 0, NULL);
1031 if (oinp) {
1032 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
1033 otp->t_state == TCPS_TIME_WAIT &&
1034 (ticks - otp->t_starttime) < tcp_msl &&
1035 (otp->t_flags & TF_RCVD_CC))
1036 otp = tcp_close(otp);
1037 else
1038 return (EADDRINUSE);
1040 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1041 inp->in6p_laddr = *addr6;
1042 inp->in6p_faddr = sin6->sin6_addr;
1043 inp->inp_fport = sin6->sin6_port;
1044 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
1045 inp->in6p_flowinfo = sin6->sin6_flowinfo;
1046 in_pcbinsconnhash(inp);
1048 /* Compute window scaling to request. */
1049 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1050 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.ssb_hiwat)
1051 tp->request_r_scale++;
1053 soisconnecting(so);
1054 tcpstat.tcps_connattempt++;
1055 tp->t_state = TCPS_SYN_SENT;
1056 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1057 tp->iss = tcp_new_isn(tp);
1058 tcp_sendseqinit(tp);
1061 * Generate a CC value for this connection and
1062 * check whether CC or CCnew should be used.
1064 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
1065 taop = &tao_noncached;
1066 bzero(taop, sizeof *taop);
1069 tp->cc_send = CC_INC(tcp_ccgen);
1070 if (taop->tao_ccsent != 0 &&
1071 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
1072 taop->tao_ccsent = tp->cc_send;
1073 } else {
1074 taop->tao_ccsent = 0;
1075 tp->t_flags |= TF_SENDCCNEW;
1078 return (0);
1080 #endif /* INET6 */
1083 * The new sockopt interface makes it possible for us to block in the
1084 * copyin/out step (if we take a page fault). Taking a page fault while
1085 * in a critical section is probably a Bad Thing. (Since sockets and pcbs
1086 * both now use TSM, there probably isn't any need for this function to
1087 * run in a critical section any more. This needs more examination.)
1090 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1092 int error, opt, optval;
1093 struct inpcb *inp;
1094 struct tcpcb *tp;
1096 error = 0;
1097 crit_enter(); /* XXX */
1098 inp = so->so_pcb;
1099 if (inp == NULL) {
1100 crit_exit();
1101 return (ECONNRESET);
1103 if (sopt->sopt_level != IPPROTO_TCP) {
1104 #ifdef INET6
1105 if (INP_CHECK_SOCKAF(so, AF_INET6))
1106 error = ip6_ctloutput(so, sopt);
1107 else
1108 #endif /* INET6 */
1109 error = ip_ctloutput(so, sopt);
1110 crit_exit();
1111 return (error);
1113 tp = intotcpcb(inp);
1115 switch (sopt->sopt_dir) {
1116 case SOPT_SET:
1117 switch (sopt->sopt_name) {
1118 case TCP_NODELAY:
1119 case TCP_NOOPT:
1120 error = sooptcopyin(sopt, &optval, sizeof optval,
1121 sizeof optval);
1122 if (error)
1123 break;
1125 switch (sopt->sopt_name) {
1126 case TCP_NODELAY:
1127 opt = TF_NODELAY;
1128 break;
1129 case TCP_NOOPT:
1130 opt = TF_NOOPT;
1131 break;
1132 default:
1133 opt = 0; /* dead code to fool gcc */
1134 break;
1137 if (optval)
1138 tp->t_flags |= opt;
1139 else
1140 tp->t_flags &= ~opt;
1141 break;
1143 case TCP_NOPUSH:
1144 error = sooptcopyin(sopt, &optval, sizeof optval,
1145 sizeof optval);
1146 if (error)
1147 break;
1149 if (optval)
1150 tp->t_flags |= TF_NOPUSH;
1151 else {
1152 tp->t_flags &= ~TF_NOPUSH;
1153 error = tcp_output(tp);
1155 break;
1157 case TCP_MAXSEG:
1158 error = sooptcopyin(sopt, &optval, sizeof optval,
1159 sizeof optval);
1160 if (error)
1161 break;
1163 if (optval > 0 && optval <= tp->t_maxseg)
1164 tp->t_maxseg = optval;
1165 else
1166 error = EINVAL;
1167 break;
1169 default:
1170 error = ENOPROTOOPT;
1171 break;
1173 break;
1175 case SOPT_GET:
1176 switch (sopt->sopt_name) {
1177 case TCP_NODELAY:
1178 optval = tp->t_flags & TF_NODELAY;
1179 break;
1180 case TCP_MAXSEG:
1181 optval = tp->t_maxseg;
1182 break;
1183 case TCP_NOOPT:
1184 optval = tp->t_flags & TF_NOOPT;
1185 break;
1186 case TCP_NOPUSH:
1187 optval = tp->t_flags & TF_NOPUSH;
1188 break;
1189 default:
1190 error = ENOPROTOOPT;
1191 break;
1193 if (error == 0)
1194 error = sooptcopyout(sopt, &optval, sizeof optval);
1195 break;
1197 crit_exit();
1198 return (error);
1202 * tcp_sendspace and tcp_recvspace are the default send and receive window
1203 * sizes, respectively. These are obsolescent (this information should
1204 * be set by the route).
1206 u_long tcp_sendspace = 1024*32;
1207 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1208 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1209 u_long tcp_recvspace = 57344; /* largest multiple of PAGE_SIZE < 64k */
1210 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1211 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1214 * Attach TCP protocol to socket, allocating
1215 * internet protocol control block, tcp control block,
1216 * bufer space, and entering LISTEN state if to accept connections.
1218 static int
1219 tcp_attach(struct socket *so, struct pru_attach_info *ai)
1221 struct tcpcb *tp;
1222 struct inpcb *inp;
1223 int error;
1224 int cpu;
1225 #ifdef INET6
1226 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1227 #endif
1229 if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) {
1230 error = soreserve(so, tcp_sendspace, tcp_recvspace,
1231 ai->sb_rlimit);
1232 if (error)
1233 return (error);
1235 cpu = mycpu->gd_cpuid;
1236 error = in_pcballoc(so, &tcbinfo[cpu]);
1237 if (error)
1238 return (error);
1239 inp = so->so_pcb;
1240 #ifdef INET6
1241 if (isipv6) {
1242 inp->inp_vflag |= INP_IPV6;
1243 inp->in6p_hops = -1; /* use kernel default */
1245 else
1246 #endif
1247 inp->inp_vflag |= INP_IPV4;
1248 tp = tcp_newtcpcb(inp);
1249 if (tp == 0) {
1250 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1252 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1253 #ifdef INET6
1254 if (isipv6)
1255 in6_pcbdetach(inp);
1256 else
1257 #endif
1258 in_pcbdetach(inp);
1259 so->so_state |= nofd;
1260 return (ENOBUFS);
1262 tp->t_state = TCPS_CLOSED;
1263 return (0);
1267 * Initiate (or continue) disconnect.
1268 * If embryonic state, just send reset (once).
1269 * If in ``let data drain'' option and linger null, just drop.
1270 * Otherwise (hard), mark socket disconnecting and drop
1271 * current input data; switch states based on user close, and
1272 * send segment to peer (with FIN).
1274 static struct tcpcb *
1275 tcp_disconnect(struct tcpcb *tp)
1277 struct socket *so = tp->t_inpcb->inp_socket;
1279 if (tp->t_state < TCPS_ESTABLISHED)
1280 tp = tcp_close(tp);
1281 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1282 tp = tcp_drop(tp, 0);
1283 else {
1284 soisdisconnecting(so);
1285 sbflush(&so->so_rcv.sb);
1286 tp = tcp_usrclosed(tp);
1287 if (tp)
1288 tcp_output(tp);
1290 return (tp);
1294 * User issued close, and wish to trail through shutdown states:
1295 * if never received SYN, just forget it. If got a SYN from peer,
1296 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1297 * If already got a FIN from peer, then almost done; go to LAST_ACK
1298 * state. In all other cases, have already sent FIN to peer (e.g.
1299 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1300 * for peer to send FIN or not respond to keep-alives, etc.
1301 * We can let the user exit from the close as soon as the FIN is acked.
1303 static struct tcpcb *
1304 tcp_usrclosed(struct tcpcb *tp)
1307 switch (tp->t_state) {
1309 case TCPS_CLOSED:
1310 case TCPS_LISTEN:
1311 tp->t_state = TCPS_CLOSED;
1312 tp = tcp_close(tp);
1313 break;
1315 case TCPS_SYN_SENT:
1316 case TCPS_SYN_RECEIVED:
1317 tp->t_flags |= TF_NEEDFIN;
1318 break;
1320 case TCPS_ESTABLISHED:
1321 tp->t_state = TCPS_FIN_WAIT_1;
1322 break;
1324 case TCPS_CLOSE_WAIT:
1325 tp->t_state = TCPS_LAST_ACK;
1326 break;
1328 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1329 soisdisconnected(tp->t_inpcb->inp_socket);
1330 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1331 if (tp->t_state == TCPS_FIN_WAIT_2)
1332 callout_reset(tp->tt_2msl, tcp_maxidle,
1333 tcp_timer_2msl, tp);
1335 return (tp);