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