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