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