]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/tcp_usrreq.c
xnu-4570.41.2.tar.gz
[apple/xnu.git] / bsd / netinet / tcp_usrreq.c
CommitLineData
1c79356b 1/*
5ba3f43e 2 * Copyright (c) 2000-2017 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39037602 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.
39037602 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.
39037602 17 *
2d21ac55
A
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.
39037602 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 */
5ba3f43e 73#if !CONFIG_EMBEDDED
39236c6e 74#include <sys/kasl.h>
5ba3f43e 75#endif
1c79356b
A
76#include <sys/socket.h>
77#include <sys/socketvar.h>
78#include <sys/protosw.h>
39236c6e 79#include <sys/syslog.h>
1c79356b
A
80
81#include <net/if.h>
82#include <net/route.h>
6d2010ae 83#include <net/ntstat.h>
fe8ab488 84#include <net/content_filter.h>
1c79356b
A
85
86#include <netinet/in.h>
87#include <netinet/in_systm.h>
9bccf70c
A
88#if INET6
89#include <netinet/ip6.h>
90#endif
1c79356b 91#include <netinet/in_pcb.h>
9bccf70c
A
92#if INET6
93#include <netinet6/in6_pcb.h>
94#endif
1c79356b
A
95#include <netinet/in_var.h>
96#include <netinet/ip_var.h>
97#if INET6
1c79356b
A
98#include <netinet6/ip6_var.h>
99#endif
100#include <netinet/tcp.h>
101#include <netinet/tcp_fsm.h>
102#include <netinet/tcp_seq.h>
103#include <netinet/tcp_timer.h>
104#include <netinet/tcp_var.h>
105#include <netinet/tcpip.h>
5ba3f43e 106#include <netinet/tcp_cc.h>
39236c6e 107#include <mach/sdt.h>
1c79356b
A
108#if TCPDEBUG
109#include <netinet/tcp_debug.h>
110#endif
39236c6e
A
111#if MPTCP
112#include <netinet/mptcp_var.h>
113#endif /* MPTCP */
1c79356b
A
114
115#if IPSEC
116#include <netinet6/ipsec.h>
117#endif /*IPSEC*/
118
39236c6e
A
119#if FLOW_DIVERT
120#include <netinet/flow_divert.h>
121#endif /* FLOW_DIVERT */
122
6d2010ae
A
123errno_t tcp_fill_info_for_info_tuple(struct info_tuple *, struct tcp_info *);
124
125int tcp_sysctl_info(struct sysctl_oid *, void *, int , struct sysctl_req *);
3e170ce0
A
126static void tcp_connection_fill_info(struct tcpcb *tp,
127 struct tcp_connection_info *tci);
6d2010ae 128
1c79356b
A
129/*
130 * TCP protocol interface to socket abstraction.
131 */
132extern char *tcpstates[]; /* XXX ??? */
133
91447636
A
134static int tcp_attach(struct socket *, struct proc *);
135static int tcp_connect(struct tcpcb *, struct sockaddr *, struct proc *);
1c79356b 136#if INET6
91447636 137static int tcp6_connect(struct tcpcb *, struct sockaddr *, struct proc *);
39236c6e
A
138static int tcp6_usr_connect(struct socket *, struct sockaddr *,
139 struct proc *);
1c79356b 140#endif /* INET6 */
39037602
A
141static struct tcpcb *tcp_disconnect(struct tcpcb *);
142static struct tcpcb *tcp_usrclosed(struct tcpcb *);
316670eb
A
143extern void tcp_sbrcv_trim(struct tcpcb *tp, struct sockbuf *sb);
144
1c79356b 145#if TCPDEBUG
9bccf70c 146#define TCPDEBUG0 int ostate = 0
1c79356b
A
147#define TCPDEBUG1() ostate = tp ? tp->t_state : 0
148#define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
9bccf70c 149 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1c79356b
A
150#else
151#define TCPDEBUG0
152#define TCPDEBUG1()
153#define TCPDEBUG2(req)
154#endif
155
39236c6e
A
156SYSCTL_PROC(_net_inet_tcp, OID_AUTO, info,
157 CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY | CTLFLAG_KERN,
6d2010ae
A
158 0 , 0, tcp_sysctl_info, "S", "TCP info per tuple");
159
1c79356b
A
160/*
161 * TCP attaches to socket via pru_attach(), reserving space,
162 * and an internet control block.
2d21ac55
A
163 *
164 * Returns: 0 Success
165 * EISCONN
166 * tcp_attach:ENOBUFS
167 * tcp_attach:ENOMEM
168 * tcp_attach:??? [IPSEC specific]
1c79356b
A
169 */
170static int
2d21ac55 171tcp_usr_attach(struct socket *so, __unused int proto, struct proc *p)
1c79356b 172{
1c79356b
A
173 int error;
174 struct inpcb *inp = sotoinpcb(so);
175 struct tcpcb *tp = 0;
176 TCPDEBUG0;
177
178 TCPDEBUG1();
179 if (inp) {
180 error = EISCONN;
181 goto out;
182 }
39037602 183
1c79356b
A
184 error = tcp_attach(so, p);
185 if (error)
186 goto out;
187
188 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
189 so->so_linger = TCP_LINGERTIME * hz;
190 tp = sototcpcb(so);
191out:
192 TCPDEBUG2(PRU_ATTACH);
1c79356b
A
193 return error;
194}
195
196/*
197 * pru_detach() detaches the TCP protocol from the socket.
198 * If the protocol state is non-embryonic, then can't
199 * do this directly: have to initiate a pru_disconnect(),
200 * which may finish later; embryonic TCB's can just
201 * be discarded here.
202 */
203static int
204tcp_usr_detach(struct socket *so)
205{
1c79356b
A
206 int error = 0;
207 struct inpcb *inp = sotoinpcb(so);
208 struct tcpcb *tp;
209 TCPDEBUG0;
210
91447636 211 if (inp == 0 || (inp->inp_state == INPCB_STATE_DEAD)) {
1c79356b
A
212 return EINVAL; /* XXX */
213 }
5ba3f43e 214 socket_lock_assert_owned(so);
1c79356b
A
215 tp = intotcpcb(inp);
216 /* In case we got disconnected from the peer */
39236c6e
A
217 if (tp == NULL)
218 goto out;
1c79356b 219 TCPDEBUG1();
6d2010ae
A
220
221 calculate_tcp_clock();
222
1c79356b
A
223 tp = tcp_disconnect(tp);
224out:
225 TCPDEBUG2(PRU_DETACH);
1c79356b
A
226 return error;
227}
228
fe8ab488 229#if NECP
39236c6e
A
230#define COMMON_START() TCPDEBUG0; \
231do { \
232 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) \
233 return (EINVAL); \
fe8ab488 234 if (necp_socket_should_use_flow_divert(inp)) \
39236c6e
A
235 return (EPROTOTYPE); \
236 tp = intotcpcb(inp); \
237 TCPDEBUG1(); \
238 calculate_tcp_clock(); \
239} while (0)
fe8ab488
A
240#else /* NECP */
241#define COMMON_START() TCPDEBUG0; \
242do { \
243 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) \
244 return (EINVAL); \
245 tp = intotcpcb(inp); \
246 TCPDEBUG1(); \
247 calculate_tcp_clock(); \
248} while (0)
249#endif /* !NECP */
39236c6e 250
91447636 251#define COMMON_END(req) out: TCPDEBUG2(req); return error; goto out
1c79356b
A
252
253
254/*
255 * Give the socket an address.
2d21ac55
A
256 *
257 * Returns: 0 Success
258 * EINVAL Invalid argument [COMMON_START]
259 * EAFNOSUPPORT Address family not supported
260 * in_pcbbind:EADDRNOTAVAIL Address not available.
261 * in_pcbbind:EINVAL Invalid argument
262 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
263 * in_pcbbind:EACCES Permission denied
264 * in_pcbbind:EADDRINUSE Address in use
265 * in_pcbbind:EAGAIN Resource unavailable, try again
266 * in_pcbbind:EPERM Operation not permitted
1c79356b
A
267 */
268static int
269tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
270{
1c79356b
A
271 int error = 0;
272 struct inpcb *inp = sotoinpcb(so);
273 struct tcpcb *tp;
274 struct sockaddr_in *sinp;
275
276 COMMON_START();
277
2d21ac55
A
278 if (nam->sa_family != 0 && nam->sa_family != AF_INET) {
279 error = EAFNOSUPPORT;
280 goto out;
281 }
282
1c79356b
A
283 /*
284 * Must check for multicast addresses and disallow binding
285 * to them.
286 */
316670eb 287 sinp = (struct sockaddr_in *)(void *)nam;
1c79356b
A
288 if (sinp->sin_family == AF_INET &&
289 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
290 error = EAFNOSUPPORT;
291 goto out;
292 }
293 error = in_pcbbind(inp, nam, p);
294 if (error)
295 goto out;
5ba3f43e
A
296
297#if NECP
298 /* Update NECP client with bind result if not in middle of connect */
299 if ((inp->inp_flags2 & INP2_CONNECT_IN_PROGRESS) &&
300 !uuid_is_null(inp->necp_client_uuid)) {
301 socket_unlock(so, 0);
302 necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp);
303 socket_lock(so, 0);
304 }
305#endif /* NECP */
306
1c79356b
A
307 COMMON_END(PRU_BIND);
308
309}
310
311#if INET6
312static int
313tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
314{
1c79356b
A
315 int error = 0;
316 struct inpcb *inp = sotoinpcb(so);
317 struct tcpcb *tp;
318 struct sockaddr_in6 *sin6p;
319
320 COMMON_START();
321
2d21ac55
A
322 if (nam->sa_family != 0 && nam->sa_family != AF_INET6) {
323 error = EAFNOSUPPORT;
324 goto out;
325 }
326
1c79356b
A
327 /*
328 * Must check for multicast addresses and disallow binding
329 * to them.
330 */
316670eb 331 sin6p = (struct sockaddr_in6 *)(void *)nam;
1c79356b
A
332 if (sin6p->sin6_family == AF_INET6 &&
333 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
334 error = EAFNOSUPPORT;
335 goto out;
336 }
337 inp->inp_vflag &= ~INP_IPV4;
338 inp->inp_vflag |= INP_IPV6;
55e303ae 339 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1c79356b
A
340 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
341 inp->inp_vflag |= INP_IPV4;
342 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
343 struct sockaddr_in sin;
344
345 in6_sin6_2_sin(&sin, sin6p);
346 inp->inp_vflag |= INP_IPV4;
347 inp->inp_vflag &= ~INP_IPV6;
348 error = in_pcbbind(inp, (struct sockaddr *)&sin, p);
349 goto out;
350 }
351 }
352 error = in6_pcbbind(inp, nam, p);
9bccf70c
A
353 if (error)
354 goto out;
1c79356b
A
355 COMMON_END(PRU_BIND);
356}
357#endif /* INET6 */
358
359/*
360 * Prepare to accept connections.
2d21ac55
A
361 *
362 * Returns: 0 Success
363 * EINVAL [COMMON_START]
364 * in_pcbbind:EADDRNOTAVAIL Address not available.
365 * in_pcbbind:EINVAL Invalid argument
366 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
367 * in_pcbbind:EACCES Permission denied
368 * in_pcbbind:EADDRINUSE Address in use
369 * in_pcbbind:EAGAIN Resource unavailable, try again
370 * in_pcbbind:EPERM Operation not permitted
1c79356b
A
371 */
372static int
373tcp_usr_listen(struct socket *so, struct proc *p)
374{
1c79356b
A
375 int error = 0;
376 struct inpcb *inp = sotoinpcb(so);
377 struct tcpcb *tp;
378
379 COMMON_START();
380 if (inp->inp_lport == 0)
39236c6e 381 error = in_pcbbind(inp, NULL, p);
1c79356b
A
382 if (error == 0)
383 tp->t_state = TCPS_LISTEN;
384 COMMON_END(PRU_LISTEN);
385}
386
387#if INET6
388static int
389tcp6_usr_listen(struct socket *so, struct proc *p)
390{
1c79356b
A
391 int error = 0;
392 struct inpcb *inp = sotoinpcb(so);
393 struct tcpcb *tp;
394
395 COMMON_START();
396 if (inp->inp_lport == 0) {
397 inp->inp_vflag &= ~INP_IPV4;
55e303ae 398 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
1c79356b 399 inp->inp_vflag |= INP_IPV4;
39236c6e 400 error = in6_pcbbind(inp, NULL, p);
1c79356b
A
401 }
402 if (error == 0)
403 tp->t_state = TCPS_LISTEN;
404 COMMON_END(PRU_LISTEN);
405}
406#endif /* INET6 */
407
3e170ce0
A
408static int
409tcp_connect_complete(struct socket *so)
410{
411 struct tcpcb *tp = sototcpcb(so);
5ba3f43e 412 struct inpcb *inp = sotoinpcb(so);
3e170ce0
A
413 int error = 0;
414
5ba3f43e
A
415#if NECP
416 /* Update NECP client with connected five-tuple */
417 if (!uuid_is_null(inp->necp_client_uuid)) {
418 socket_unlock(so, 0);
419 necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp);
420 socket_lock(so, 0);
421 }
422#endif /* NECP */
423
3e170ce0
A
424 /* TFO delays the tcp_output until later, when the app calls write() */
425 if (so->so_flags1 & SOF1_PRECONNECT_DATA) {
426 if (!necp_socket_is_allowed_to_send_recv(sotoinpcb(so), NULL, NULL))
427 return (EHOSTUNREACH);
428
429 /* Initialize enough state so that we can actually send data */
430 tcp_mss(tp, -1, IFSCOPE_NONE);
431 tp->snd_wnd = tp->t_maxseg;
432 } else {
433 error = tcp_output(tp);
434 }
435
436 return (error);
437}
438
1c79356b
A
439/*
440 * Initiate connection to peer.
441 * Create a template for use in transmissions on this connection.
442 * Enter SYN_SENT state, and mark socket as connecting.
443 * Start keep-alive timer, and seed output sequence space.
444 * Send initial segment on connection.
445 */
446static int
447tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
448{
1c79356b
A
449 int error = 0;
450 struct inpcb *inp = sotoinpcb(so);
451 struct tcpcb *tp;
452 struct sockaddr_in *sinp;
453
2d21ac55 454 TCPDEBUG0;
39236c6e 455 if (inp == NULL) {
2d21ac55 456 return EINVAL;
39236c6e 457 } else if (inp->inp_state == INPCB_STATE_DEAD) {
2d21ac55
A
458 if (so->so_error) {
459 error = so->so_error;
460 so->so_error = 0;
461 return error;
462 } else
463 return EINVAL;
464 }
fe8ab488 465#if NECP
39236c6e 466#if FLOW_DIVERT
fe8ab488
A
467 else if (necp_socket_should_use_flow_divert(inp)) {
468 uint32_t fd_ctl_unit = necp_socket_get_flow_divert_control_unit(inp);
469 if (fd_ctl_unit > 0) {
470 error = flow_divert_pcb_init(so, fd_ctl_unit);
471 if (error == 0) {
472 error = flow_divert_connect_out(so, nam, p);
39236c6e 473 }
fe8ab488
A
474 } else {
475 error = ENETDOWN;
39236c6e 476 }
3e170ce0 477
39236c6e
A
478 return error;
479 }
480#endif /* FLOW_DIVERT */
fe8ab488
A
481#if CONTENT_FILTER
482 error = cfil_sock_attach(so);
483 if (error != 0)
484 return error;
485#endif /* CONTENT_FILTER */
486#endif /* NECP */
2d21ac55
A
487 tp = intotcpcb(inp);
488 TCPDEBUG1();
1c79356b 489
6d2010ae
A
490 calculate_tcp_clock();
491
2d21ac55
A
492 if (nam->sa_family != 0 && nam->sa_family != AF_INET) {
493 error = EAFNOSUPPORT;
494 goto out;
495 }
1c79356b
A
496 /*
497 * Must disallow TCP ``connections'' to multicast addresses.
498 */
316670eb 499 sinp = (struct sockaddr_in *)(void *)nam;
1c79356b
A
500 if (sinp->sin_family == AF_INET
501 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
502 error = EAFNOSUPPORT;
503 goto out;
504 }
505
506 if ((error = tcp_connect(tp, nam, p)) != 0)
507 goto out;
3e170ce0
A
508
509 error = tcp_connect_complete(so);
510
1c79356b
A
511 COMMON_END(PRU_CONNECT);
512}
513
39236c6e
A
514static int
515tcp_usr_connectx_common(struct socket *so, int af,
813fb2f6 516 struct sockaddr *src, struct sockaddr *dst,
3e170ce0
A
517 struct proc *p, uint32_t ifscope, sae_associd_t aid, sae_connid_t *pcid,
518 uint32_t flags, void *arg, uint32_t arglen, struct uio *auio,
519 user_ssize_t *bytes_written)
39236c6e 520{
5ba3f43e 521#pragma unused(aid, flags, arg, arglen)
39236c6e 522 struct inpcb *inp = sotoinpcb(so);
5ba3f43e 523 int error = 0;
3e170ce0 524 user_ssize_t datalen = 0;
39236c6e
A
525
526 if (inp == NULL)
527 return (EINVAL);
528
813fb2f6 529 VERIFY(dst != NULL);
39236c6e 530
5ba3f43e
A
531 ASSERT(!(inp->inp_flags2 & INP2_CONNECT_IN_PROGRESS));
532 inp->inp_flags2 |= INP2_CONNECT_IN_PROGRESS;
533
fe8ab488 534#if NECP
813fb2f6 535 inp_update_necp_policy(inp, src, dst, ifscope);
fe8ab488 536#endif /* NECP */
3e170ce0
A
537
538 if ((so->so_flags1 & SOF1_DATA_IDEMPOTENT) &&
539 (tcp_fastopen & TCP_FASTOPEN_CLIENT))
540 sototcpcb(so)->t_flagsext |= TF_FASTOPEN;
541
5ba3f43e
A
542 /* bind socket to the specified interface, if requested */
543 if (ifscope != IFSCOPE_NONE &&
544 (error = inp_bindif(inp, ifscope, NULL)) != 0) {
545 goto done;
546 }
39236c6e 547
5ba3f43e
A
548 /* if source address and/or port is specified, bind to it */
549 if (src != NULL) {
550 error = sobindlock(so, src, 0); /* already locked */
551 if (error != 0) {
552 goto done;
39236c6e
A
553 }
554 }
555
556 switch (af) {
557 case AF_INET:
813fb2f6 558 error = tcp_usr_connect(so, dst, p);
39236c6e
A
559 break;
560#if INET6
561 case AF_INET6:
813fb2f6 562 error = tcp6_usr_connect(so, dst, p);
39236c6e
A
563 break;
564#endif /* INET6 */
565 default:
566 VERIFY(0);
567 /* NOTREACHED */
568 }
569
5ba3f43e
A
570 if (error != 0) {
571 goto done;
572 }
3e170ce0
A
573
574 /* if there is data, copy it */
575 if (auio != NULL) {
576 socket_unlock(so, 0);
577
578 VERIFY(bytes_written != NULL);
579
580 datalen = uio_resid(auio);
581 error = so->so_proto->pr_usrreqs->pru_sosend(so, NULL,
582 (uio_t)auio, NULL, NULL, 0);
583 socket_lock(so, 0);
584
585 if (error == 0 || error == EWOULDBLOCK)
586 *bytes_written = datalen - uio_resid(auio);
587
588 /*
589 * sosend returns EWOULDBLOCK if it's a non-blocking
590 * socket or a timeout occured (this allows to return
591 * the amount of queued data through sendit()).
592 *
593 * However, connectx() returns EINPROGRESS in case of a
594 * blocking socket. So we change the return value here.
595 */
596 if (error == EWOULDBLOCK)
597 error = EINPROGRESS;
598 }
599
39236c6e 600 if (error == 0 && pcid != NULL)
3e170ce0 601 *pcid = 1; /* there is only one connection in regular TCP */
39236c6e 602
5ba3f43e
A
603done:
604 inp->inp_flags2 &= ~INP2_CONNECT_IN_PROGRESS;
39236c6e
A
605 return (error);
606}
607
608static int
813fb2f6
A
609tcp_usr_connectx(struct socket *so, struct sockaddr *src,
610 struct sockaddr *dst, struct proc *p, uint32_t ifscope,
3e170ce0
A
611 sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg,
612 uint32_t arglen, struct uio *uio, user_ssize_t *bytes_written)
39236c6e 613{
813fb2f6
A
614 return (tcp_usr_connectx_common(so, AF_INET, src, dst, p, ifscope, aid,
615 pcid, flags, arg, arglen, uio, bytes_written));
39236c6e
A
616}
617
1c79356b
A
618#if INET6
619static int
620tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
621{
1c79356b
A
622 int error = 0;
623 struct inpcb *inp = sotoinpcb(so);
624 struct tcpcb *tp;
625 struct sockaddr_in6 *sin6p;
626
39236c6e
A
627 TCPDEBUG0;
628 if (inp == NULL) {
629 return EINVAL;
630 } else if (inp->inp_state == INPCB_STATE_DEAD) {
631 if (so->so_error) {
632 error = so->so_error;
633 so->so_error = 0;
634 return error;
635 } else
636 return EINVAL;
637 }
fe8ab488 638#if NECP
39236c6e 639#if FLOW_DIVERT
fe8ab488
A
640 else if (necp_socket_should_use_flow_divert(inp)) {
641 uint32_t fd_ctl_unit = necp_socket_get_flow_divert_control_unit(inp);
642 if (fd_ctl_unit > 0) {
643 error = flow_divert_pcb_init(so, fd_ctl_unit);
644 if (error == 0) {
645 error = flow_divert_connect_out(so, nam, p);
39236c6e 646 }
fe8ab488
A
647 } else {
648 error = ENETDOWN;
39236c6e 649 }
4bd07ac2 650
39236c6e
A
651 return error;
652 }
653#endif /* FLOW_DIVERT */
fe8ab488
A
654#if CONTENT_FILTER
655 error = cfil_sock_attach(so);
656 if (error != 0)
657 return error;
658#endif /* CONTENT_FILTER */
659#endif /* NECP */
660
39236c6e
A
661 tp = intotcpcb(inp);
662 TCPDEBUG1();
663
664 calculate_tcp_clock();
1c79356b 665
2d21ac55
A
666 if (nam->sa_family != 0 && nam->sa_family != AF_INET6) {
667 error = EAFNOSUPPORT;
668 goto out;
669 }
670
1c79356b
A
671 /*
672 * Must disallow TCP ``connections'' to multicast addresses.
673 */
316670eb 674 sin6p = (struct sockaddr_in6 *)(void *)nam;
1c79356b
A
675 if (sin6p->sin6_family == AF_INET6
676 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
677 error = EAFNOSUPPORT;
678 goto out;
679 }
9bccf70c
A
680
681 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
1c79356b
A
682 struct sockaddr_in sin;
683
55e303ae
A
684 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)
685 return (EINVAL);
9bccf70c 686
1c79356b
A
687 in6_sin6_2_sin(&sin, sin6p);
688 inp->inp_vflag |= INP_IPV4;
689 inp->inp_vflag &= ~INP_IPV6;
690 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, p)) != 0)
691 goto out;
3e170ce0
A
692
693 error = tcp_connect_complete(so);
1c79356b
A
694 goto out;
695 }
9bccf70c
A
696 inp->inp_vflag &= ~INP_IPV4;
697 inp->inp_vflag |= INP_IPV6;
1c79356b
A
698 if ((error = tcp6_connect(tp, nam, p)) != 0)
699 goto out;
3e170ce0
A
700
701 error = tcp_connect_complete(so);
1c79356b
A
702 COMMON_END(PRU_CONNECT);
703}
39236c6e
A
704
705static int
813fb2f6
A
706tcp6_usr_connectx(struct socket *so, struct sockaddr*src,
707 struct sockaddr *dst, struct proc *p, uint32_t ifscope,
3e170ce0
A
708 sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg,
709 uint32_t arglen, struct uio *uio, user_ssize_t *bytes_written)
39236c6e 710{
813fb2f6
A
711 return (tcp_usr_connectx_common(so, AF_INET6, src, dst, p, ifscope, aid,
712 pcid, flags, arg, arglen, uio, bytes_written));
39236c6e 713}
1c79356b
A
714#endif /* INET6 */
715
716/*
717 * Initiate disconnect from peer.
718 * If connection never passed embryonic stage, just drop;
719 * else if don't need to let data drain, then can just drop anyways,
720 * else have to begin TCP shutdown process: mark socket disconnecting,
721 * drain unread data, state switch to reflect user close, and
722 * send segment (e.g. FIN) to peer. Socket will be really disconnected
723 * when peer sends FIN and acks ours.
724 *
725 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
726 */
727static int
728tcp_usr_disconnect(struct socket *so)
729{
1c79356b
A
730 int error = 0;
731 struct inpcb *inp = sotoinpcb(so);
732 struct tcpcb *tp;
39236c6e 733
5ba3f43e 734 socket_lock_assert_owned(so);
1c79356b
A
735 COMMON_START();
736 /* In case we got disconnected from the peer */
39236c6e
A
737 if (tp == NULL)
738 goto out;
1c79356b
A
739 tp = tcp_disconnect(tp);
740 COMMON_END(PRU_DISCONNECT);
741}
742
39236c6e
A
743/*
744 * User-protocol pru_disconnectx callback.
745 */
746static int
3e170ce0 747tcp_usr_disconnectx(struct socket *so, sae_associd_t aid, sae_connid_t cid)
39236c6e
A
748{
749#pragma unused(cid)
3e170ce0 750 if (aid != SAE_ASSOCID_ANY && aid != SAE_ASSOCID_ALL)
39236c6e
A
751 return (EINVAL);
752
753 return (tcp_usr_disconnect(so));
754}
755
1c79356b
A
756/*
757 * Accept a connection. Essentially all the work is
758 * done at higher levels; just return the address
759 * of the peer, storing through addr.
760 */
761static int
762tcp_usr_accept(struct socket *so, struct sockaddr **nam)
763{
1c79356b
A
764 int error = 0;
765 struct inpcb *inp = sotoinpcb(so);
9bccf70c
A
766 struct tcpcb *tp = NULL;
767 TCPDEBUG0;
1c79356b 768
39236c6e
A
769 in_getpeeraddr(so, nam);
770
9bccf70c
A
771 if (so->so_state & SS_ISDISCONNECTED) {
772 error = ECONNABORTED;
773 goto out;
774 }
39236c6e 775 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD)
9bccf70c 776 return (EINVAL);
fe8ab488
A
777#if NECP
778 else if (necp_socket_should_use_flow_divert(inp))
39236c6e 779 return (EPROTOTYPE);
fe8ab488
A
780#if CONTENT_FILTER
781 error = cfil_sock_attach(so);
782 if (error != 0)
783 return (error);
784#endif /* CONTENT_FILTER */
785#endif /* NECP */
39236c6e 786
9bccf70c
A
787 tp = intotcpcb(inp);
788 TCPDEBUG1();
6d2010ae
A
789
790 calculate_tcp_clock();
791
1c79356b
A
792 COMMON_END(PRU_ACCEPT);
793}
794
795#if INET6
796static int
797tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
798{
1c79356b
A
799 int error = 0;
800 struct inpcb *inp = sotoinpcb(so);
9bccf70c
A
801 struct tcpcb *tp = NULL;
802 TCPDEBUG0;
1c79356b 803
9bccf70c
A
804 if (so->so_state & SS_ISDISCONNECTED) {
805 error = ECONNABORTED;
806 goto out;
807 }
39236c6e 808 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD)
9bccf70c 809 return (EINVAL);
fe8ab488
A
810#if NECP
811 else if (necp_socket_should_use_flow_divert(inp))
39236c6e 812 return (EPROTOTYPE);
fe8ab488
A
813#if CONTENT_FILTER
814 error = cfil_sock_attach(so);
815 if (error != 0)
816 return (error);
817#endif /* CONTENT_FILTER */
818#endif /* NECP */
39236c6e 819
9bccf70c
A
820 tp = intotcpcb(inp);
821 TCPDEBUG1();
6d2010ae
A
822
823 calculate_tcp_clock();
824
1c79356b
A
825 in6_mapped_peeraddr(so, nam);
826 COMMON_END(PRU_ACCEPT);
827}
828#endif /* INET6 */
2d21ac55 829
1c79356b
A
830/*
831 * Mark the connection as being incapable of further output.
2d21ac55
A
832 *
833 * Returns: 0 Success
834 * EINVAL [COMMON_START]
835 * tcp_output:EADDRNOTAVAIL
836 * tcp_output:ENOBUFS
837 * tcp_output:EMSGSIZE
838 * tcp_output:EHOSTUNREACH
839 * tcp_output:ENETUNREACH
840 * tcp_output:ENETDOWN
841 * tcp_output:ENOMEM
842 * tcp_output:EACCES
843 * tcp_output:EMSGSIZE
844 * tcp_output:ENOBUFS
845 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
1c79356b
A
846 */
847static int
848tcp_usr_shutdown(struct socket *so)
849{
1c79356b
A
850 int error = 0;
851 struct inpcb *inp = sotoinpcb(so);
852 struct tcpcb *tp;
853
39236c6e
A
854 TCPDEBUG0;
855 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD)
856 return (EINVAL);
857
1c79356b 858 socantsendmore(so);
39236c6e
A
859
860 /*
861 * In case we got disconnected from the peer, or if this is
862 * a socket that is to be flow-diverted (but not yet).
863 */
864 tp = intotcpcb(inp);
865 TCPDEBUG1();
fe8ab488
A
866
867 if (tp == NULL
868#if NECP
869 || (necp_socket_should_use_flow_divert(inp))
870#endif /* NECP */
871 ) {
39236c6e
A
872 if (tp != NULL)
873 error = EPROTOTYPE;
874 goto out;
875 }
876
877 calculate_tcp_clock();
878
1c79356b 879 tp = tcp_usrclosed(tp);
39236c6e
A
880#if MPTCP
881 /* A reset has been sent but socket exists, do not send FIN */
882 if ((so->so_flags & SOF_MP_SUBFLOW) &&
883 (tp) && (tp->t_mpflags & TMPF_RESET)) {
884 goto out;
885 }
886#endif
fe8ab488
A
887#if CONTENT_FILTER
888 /* Don't send a FIN yet */
889 if (tp && !(so->so_state & SS_ISDISCONNECTED) &&
890 cfil_sock_data_pending(&so->so_snd))
891 goto out;
892#endif /* CONTENT_FILTER */
1c79356b
A
893 if (tp)
894 error = tcp_output(tp);
895 COMMON_END(PRU_SHUTDOWN);
896}
897
898/*
899 * After a receive, possibly send window update to peer.
900 */
901static int
2d21ac55 902tcp_usr_rcvd(struct socket *so, __unused int flags)
1c79356b 903{
1c79356b
A
904 int error = 0;
905 struct inpcb *inp = sotoinpcb(so);
906 struct tcpcb *tp;
907
908 COMMON_START();
909 /* In case we got disconnected from the peer */
39236c6e
A
910 if (tp == NULL)
911 goto out;
316670eb
A
912 tcp_sbrcv_trim(tp, &so->so_rcv);
913
3e170ce0
A
914 /*
915 * This tcp_output is solely there to trigger window-updates.
916 * However, we really do not want these window-updates while we
917 * are still in SYN_SENT or SYN_RECEIVED.
918 */
919 if (TCPS_HAVEESTABLISHED(tp->t_state))
920 tcp_output(tp);
fe8ab488
A
921
922#if CONTENT_FILTER
923 cfil_sock_buf_update(&so->so_rcv);
924#endif /* CONTENT_FILTER */
925
1c79356b
A
926 COMMON_END(PRU_RCVD);
927}
928
929/*
930 * Do a send by putting data in output queue and updating urgent
9bccf70c
A
931 * marker if URG set. Possibly send more data. Unlike the other
932 * pru_*() routines, the mbuf chains are our responsibility. We
933 * must either enqueue them or free them. The other pru_* routines
934 * generally are caller-frees.
2d21ac55
A
935 *
936 * Returns: 0 Success
937 * ECONNRESET
938 * EINVAL
939 * ENOBUFS
940 * tcp_connect:EADDRINUSE Address in use
941 * tcp_connect:EADDRNOTAVAIL Address not available.
942 * tcp_connect:EINVAL Invalid argument
943 * tcp_connect:EAFNOSUPPORT Address family not supported [notdef]
944 * tcp_connect:EACCES Permission denied
945 * tcp_connect:EAGAIN Resource unavailable, try again
946 * tcp_connect:EPERM Operation not permitted
947 * tcp_output:EADDRNOTAVAIL
948 * tcp_output:ENOBUFS
949 * tcp_output:EMSGSIZE
950 * tcp_output:EHOSTUNREACH
951 * tcp_output:ENETUNREACH
952 * tcp_output:ENETDOWN
953 * tcp_output:ENOMEM
954 * tcp_output:EACCES
955 * tcp_output:EMSGSIZE
956 * tcp_output:ENOBUFS
957 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
958 * tcp6_connect:??? [IPV6 only]
1c79356b
A
959 */
960static int
39236c6e
A
961tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
962 struct sockaddr *nam, struct mbuf *control, struct proc *p)
1c79356b 963{
1c79356b
A
964 int error = 0;
965 struct inpcb *inp = sotoinpcb(so);
966 struct tcpcb *tp;
39236c6e 967 uint32_t msgpri = MSG_PRI_DEFAULT;
1c79356b
A
968#if INET6
969 int isipv6;
9bccf70c
A
970#endif
971 TCPDEBUG0;
1c79356b 972
fe8ab488
A
973 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD
974#if NECP
975 || (necp_socket_should_use_flow_divert(inp))
976#endif /* NECP */
977 ) {
9bccf70c
A
978 /*
979 * OOPS! we lost a race, the TCP session got reset after
980 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
981 * network interrupt in the non-splnet() section of sosend().
982 */
39236c6e 983 if (m != NULL)
1c79356b 984 m_freem(m);
39236c6e 985 if (control != NULL) {
9bccf70c 986 m_freem(control);
39236c6e
A
987 control = NULL;
988 }
fe8ab488
A
989
990 if (inp == NULL)
39236c6e 991 error = ECONNRESET; /* XXX EPIPE? */
fe8ab488
A
992 else
993 error = EPROTOTYPE;
9bccf70c
A
994 tp = NULL;
995 TCPDEBUG1();
1c79356b
A
996 goto out;
997 }
1c79356b
A
998#if INET6
999 isipv6 = nam && nam->sa_family == AF_INET6;
1000#endif /* INET6 */
9bccf70c
A
1001 tp = intotcpcb(inp);
1002 TCPDEBUG1();
6d2010ae
A
1003
1004 calculate_tcp_clock();
1005
39236c6e
A
1006 if (control != NULL) {
1007 if (so->so_flags & SOF_ENABLE_MSGS) {
1008 /* Get the msg priority from control mbufs */
1009 error = tcp_get_msg_priority(control, &msgpri);
1010 if (error) {
1011 m_freem(control);
1012 if (m != NULL)
1013 m_freem(m);
1014 control = NULL;
1015 m = NULL;
1016 goto out;
1017 }
9bccf70c 1018 m_freem(control);
39236c6e
A
1019 control = NULL;
1020 } else if (control->m_len) {
5ba3f43e
A
1021 /*
1022 * if not unordered, TCP should not have
39236c6e
A
1023 * control mbufs
1024 */
1025 m_freem(control);
1026 if (m != NULL)
9bccf70c 1027 m_freem(m);
39236c6e
A
1028 control = NULL;
1029 m = NULL;
9bccf70c
A
1030 error = EINVAL;
1031 goto out;
1032 }
9bccf70c 1033 }
39236c6e
A
1034
1035 if (so->so_flags & SOF_ENABLE_MSGS) {
1036 VERIFY(m->m_flags & M_PKTHDR);
1037 m->m_pkthdr.msg_pri = msgpri;
1038 }
1039
1040 /* MPTCP sublow socket buffers must not be compressed */
1041 VERIFY(!(so->so_flags & SOF_MP_SUBFLOW) ||
1042 (so->so_snd.sb_flags & SB_NOCOMPRESS));
1043
3e170ce0 1044 if(!(flags & PRUS_OOB) || (so->so_flags1 & SOF1_PRECONNECT_DATA)) {
39236c6e
A
1045 /* Call msg send if message delivery is enabled */
1046 if (so->so_flags & SOF_ENABLE_MSGS)
1047 sbappendmsg_snd(&so->so_snd, m);
1048 else
1049 sbappendstream(&so->so_snd, m);
1050
1c79356b 1051 if (nam && tp->t_state < TCPS_SYN_SENT) {
3e170ce0 1052
1c79356b
A
1053 /*
1054 * Do implied connect if not yet connected,
1055 * initialize window to default value, and
1056 * initialize maxseg/maxopd using peer's cached
1057 * MSS.
1058 */
1059#if INET6
1060 if (isipv6)
1061 error = tcp6_connect(tp, nam, p);
1062 else
1063#endif /* INET6 */
316670eb 1064 error = tcp_connect(tp, nam, p);
1c79356b
A
1065 if (error)
1066 goto out;
1067 tp->snd_wnd = TTCP_CLIENT_SND_WND;
c910b4d9 1068 tcp_mss(tp, -1, IFSCOPE_NONE);
1c79356b
A
1069 }
1070
1071 if (flags & PRUS_EOF) {
1072 /*
1073 * Close the send side of the connection after
1074 * the data is sent.
1075 */
1076 socantsendmore(so);
1077 tp = tcp_usrclosed(tp);
1078 }
1079 if (tp != NULL) {
1080 if (flags & PRUS_MORETOCOME)
1081 tp->t_flags |= TF_MORETOCOME;
1082 error = tcp_output(tp);
1083 if (flags & PRUS_MORETOCOME)
1084 tp->t_flags &= ~TF_MORETOCOME;
1085 }
1086 } else {
5ba3f43e
A
1087 if (sbspace(&so->so_snd) == 0) {
1088 /* if no space is left in sockbuf,
b0d623f7 1089 * do not try to squeeze in OOB traffic */
1c79356b
A
1090 m_freem(m);
1091 error = ENOBUFS;
1092 goto out;
1093 }
1094 /*
1095 * According to RFC961 (Assigned Protocols),
1096 * the urgent pointer points to the last octet
1097 * of urgent data. We continue, however,
1098 * to consider it to indicate the first octet
1099 * of data past the urgent section.
1100 * Otherwise, snd_up should be one lower.
1101 */
2d21ac55 1102 sbappendstream(&so->so_snd, m);
1c79356b
A
1103 if (nam && tp->t_state < TCPS_SYN_SENT) {
1104 /*
1105 * Do implied connect if not yet connected,
1106 * initialize window to default value, and
1107 * initialize maxseg/maxopd using peer's cached
1108 * MSS.
1109 */
1110#if INET6
1111 if (isipv6)
1112 error = tcp6_connect(tp, nam, p);
1113 else
1114#endif /* INET6 */
1115 error = tcp_connect(tp, nam, p);
1116 if (error)
1117 goto out;
1118 tp->snd_wnd = TTCP_CLIENT_SND_WND;
c910b4d9 1119 tcp_mss(tp, -1, IFSCOPE_NONE);
1c79356b
A
1120 }
1121 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
fe8ab488 1122 tp->t_flagsext |= TF_FORCE;
1c79356b 1123 error = tcp_output(tp);
fe8ab488 1124 tp->t_flagsext &= ~TF_FORCE;
1c79356b 1125 }
3e170ce0
A
1126
1127
1128 /*
1129 * We wait for the socket to successfully connect before returning.
1130 * This allows us to signal a timeout to the application.
1131 */
1132 if (so->so_state & SS_ISCONNECTING) {
1133 if (so->so_state & SS_NBIO)
1134 error = EWOULDBLOCK;
1135 else
1136 error = sbwait(&so->so_snd);
1137 }
1138
5ba3f43e 1139 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
1c79356b
A
1140 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1141}
1142
1143/*
1144 * Abort the TCP.
1145 */
1146static int
1147tcp_usr_abort(struct socket *so)
1148{
1c79356b
A
1149 int error = 0;
1150 struct inpcb *inp = sotoinpcb(so);
1151 struct tcpcb *tp;
1152
1153 COMMON_START();
1154 /* In case we got disconnected from the peer */
39236c6e
A
1155 if (tp == NULL)
1156 goto out;
1c79356b 1157 tp = tcp_drop(tp, ECONNABORTED);
d190cdc3 1158 VERIFY(so->so_usecount > 0);
91447636 1159 so->so_usecount--;
1c79356b
A
1160 COMMON_END(PRU_ABORT);
1161}
1162
1163/*
1164 * Receive out-of-band data.
2d21ac55
A
1165 *
1166 * Returns: 0 Success
1167 * EINVAL [COMMON_START]
1168 * EINVAL
1169 * EWOULDBLOCK
1c79356b
A
1170 */
1171static int
1172tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1173{
1c79356b
A
1174 int error = 0;
1175 struct inpcb *inp = sotoinpcb(so);
1176 struct tcpcb *tp;
1177
1178 COMMON_START();
1179 if ((so->so_oobmark == 0 &&
1180 (so->so_state & SS_RCVATMARK) == 0) ||
1181 so->so_options & SO_OOBINLINE ||
1182 tp->t_oobflags & TCPOOB_HADDATA) {
1183 error = EINVAL;
1184 goto out;
1185 }
1186 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1187 error = EWOULDBLOCK;
1188 goto out;
1189 }
1190 m->m_len = 1;
1191 *mtod(m, caddr_t) = tp->t_iobc;
39037602 1192 so->so_state &= ~SS_RCVATMARK;
1c79356b
A
1193 if ((flags & MSG_PEEK) == 0)
1194 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1195 COMMON_END(PRU_RCVOOB);
1196}
1197
3e170ce0
A
1198static int
1199tcp_usr_preconnect(struct socket *so)
1200{
490019cf
A
1201 struct inpcb *inp = sotoinpcb(so);
1202 int error = 0;
1203
1204#if NECP
1205 if (necp_socket_should_use_flow_divert(inp)) {
1206 /* May happen, if in tcp_usr_connect we did not had a chance
1207 * to set the usrreqs (due to some error). So, let's get out
1208 * of here.
1209 */
1210 goto out;
1211 }
1212#endif /* NECP */
1213
1214 error = tcp_output(sototcpcb(so));
3e170ce0 1215
5ba3f43e 1216 soclearfastopen(so);
3e170ce0
A
1217
1218 COMMON_END(PRU_PRECONNECT);
1219}
1220
1c79356b
A
1221/* xxx - should be const */
1222struct pr_usrreqs tcp_usrreqs = {
39236c6e
A
1223 .pru_abort = tcp_usr_abort,
1224 .pru_accept = tcp_usr_accept,
1225 .pru_attach = tcp_usr_attach,
1226 .pru_bind = tcp_usr_bind,
1227 .pru_connect = tcp_usr_connect,
1228 .pru_connectx = tcp_usr_connectx,
1229 .pru_control = in_control,
1230 .pru_detach = tcp_usr_detach,
1231 .pru_disconnect = tcp_usr_disconnect,
1232 .pru_disconnectx = tcp_usr_disconnectx,
1233 .pru_listen = tcp_usr_listen,
1234 .pru_peeraddr = in_getpeeraddr,
1235 .pru_rcvd = tcp_usr_rcvd,
1236 .pru_rcvoob = tcp_usr_rcvoob,
1237 .pru_send = tcp_usr_send,
1238 .pru_shutdown = tcp_usr_shutdown,
1239 .pru_sockaddr = in_getsockaddr,
1240 .pru_sosend = sosend,
1241 .pru_soreceive = soreceive,
3e170ce0 1242 .pru_preconnect = tcp_usr_preconnect,
1c79356b
A
1243};
1244
1245#if INET6
1246struct pr_usrreqs tcp6_usrreqs = {
39236c6e
A
1247 .pru_abort = tcp_usr_abort,
1248 .pru_accept = tcp6_usr_accept,
1249 .pru_attach = tcp_usr_attach,
1250 .pru_bind = tcp6_usr_bind,
1251 .pru_connect = tcp6_usr_connect,
1252 .pru_connectx = tcp6_usr_connectx,
1253 .pru_control = in6_control,
1254 .pru_detach = tcp_usr_detach,
1255 .pru_disconnect = tcp_usr_disconnect,
1256 .pru_disconnectx = tcp_usr_disconnectx,
1257 .pru_listen = tcp6_usr_listen,
1258 .pru_peeraddr = in6_mapped_peeraddr,
1259 .pru_rcvd = tcp_usr_rcvd,
1260 .pru_rcvoob = tcp_usr_rcvoob,
1261 .pru_send = tcp_usr_send,
1262 .pru_shutdown = tcp_usr_shutdown,
1263 .pru_sockaddr = in6_mapped_sockaddr,
1264 .pru_sosend = sosend,
1265 .pru_soreceive = soreceive,
3e170ce0 1266 .pru_preconnect = tcp_usr_preconnect,
1c79356b
A
1267};
1268#endif /* INET6 */
1269
1270/*
1271 * Common subroutine to open a TCP connection to remote host specified
1272 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1273 * port number if needed. Call in_pcbladdr to do the routing and to choose
1274 * a local host address (interface). If there is an existing incarnation
1275 * of the same connection in TIME-WAIT state and if the remote host was
1276 * sending CC options and if the connection duration was < MSL, then
1277 * truncate the previous TIME-WAIT state and proceed.
1278 * Initialize connection parameters and enter SYN-SENT state.
2d21ac55
A
1279 *
1280 * Returns: 0 Success
1281 * EADDRINUSE
1282 * EINVAL
1283 * in_pcbbind:EADDRNOTAVAIL Address not available.
1284 * in_pcbbind:EINVAL Invalid argument
1285 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
1286 * in_pcbbind:EACCES Permission denied
1287 * in_pcbbind:EADDRINUSE Address in use
1288 * in_pcbbind:EAGAIN Resource unavailable, try again
1289 * in_pcbbind:EPERM Operation not permitted
1290 * in_pcbladdr:EINVAL Invalid argument
1291 * in_pcbladdr:EAFNOSUPPORT Address family not supported
1292 * in_pcbladdr:EADDRNOTAVAIL Address not available
1c79356b
A
1293 */
1294static int
39037602 1295tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct proc *p)
1c79356b
A
1296{
1297 struct inpcb *inp = tp->t_inpcb, *oinp;
1298 struct socket *so = inp->inp_socket;
1299 struct tcpcb *otp;
316670eb 1300 struct sockaddr_in *sin = (struct sockaddr_in *)(void *)nam;
39236c6e 1301 struct in_addr laddr;
39236c6e 1302 int error = 0;
316670eb 1303 struct ifnet *outif = NULL;
1c79356b
A
1304
1305 if (inp->inp_lport == 0) {
39236c6e 1306 error = in_pcbbind(inp, NULL, p);
1c79356b 1307 if (error)
39236c6e 1308 goto done;
1c79356b
A
1309 }
1310
1311 /*
1312 * Cannot simply call in_pcbconnect, because there might be an
1313 * earlier incarnation of this same connection still in
1314 * TIME_WAIT state, creating an ADDRINUSE error.
1315 */
39037602 1316 error = in_pcbladdr(inp, nam, &laddr, IFSCOPE_NONE, &outif, 0);
1c79356b 1317 if (error)
39236c6e 1318 goto done;
91447636 1319
5ba3f43e 1320 socket_unlock(inp->inp_socket, 0);
1c79356b
A
1321 oinp = in_pcblookup_hash(inp->inp_pcbinfo,
1322 sin->sin_addr, sin->sin_port,
39236c6e 1323 inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr : laddr,
1c79356b 1324 inp->inp_lport, 0, NULL);
91447636 1325
5ba3f43e 1326 socket_lock(inp->inp_socket, 0);
1c79356b 1327 if (oinp) {
3a60a9f5 1328 if (oinp != inp) /* 4143933: avoid deadlock if inp == oinp */
5ba3f43e 1329 socket_lock(oinp->inp_socket, 1);
91447636 1330 if (in_pcb_checkstate(oinp, WNT_RELEASE, 1) == WNT_STOPUSING) {
3a60a9f5 1331 if (oinp != inp)
5ba3f43e 1332 socket_unlock(oinp->inp_socket, 1);
91447636
A
1333 goto skip_oinp;
1334 }
1335
1c79356b 1336 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
39236c6e 1337 otp->t_state == TCPS_TIME_WAIT &&
6d2010ae 1338 ((int)(tcp_now - otp->t_starttime)) < tcp_msl &&
39236c6e 1339 (otp->t_flags & TF_RCVD_CC)) {
1c79356b 1340 otp = tcp_close(otp);
39236c6e
A
1341 } else {
1342 printf("tcp_connect: inp=0x%llx err=EADDRINUSE\n",
1343 (uint64_t)VM_KERNEL_ADDRPERM(inp));
3a60a9f5 1344 if (oinp != inp)
5ba3f43e 1345 socket_unlock(oinp->inp_socket, 1);
39236c6e
A
1346 error = EADDRINUSE;
1347 goto done;
91447636 1348 }
3a60a9f5 1349 if (oinp != inp)
5ba3f43e 1350 socket_unlock(oinp->inp_socket, 1);
1c79356b 1351 }
91447636 1352skip_oinp:
39236c6e
A
1353 if ((inp->inp_laddr.s_addr == INADDR_ANY ? laddr.s_addr :
1354 inp->inp_laddr.s_addr) == sin->sin_addr.s_addr &&
1355 inp->inp_lport == sin->sin_port) {
1356 error = EINVAL;
1357 goto done;
1358 }
1359 if (!lck_rw_try_lock_exclusive(inp->inp_pcbinfo->ipi_lock)) {
91447636
A
1360 /*lock inversion issue, mostly with udp multicast packets */
1361 socket_unlock(inp->inp_socket, 0);
39236c6e 1362 lck_rw_lock_exclusive(inp->inp_pcbinfo->ipi_lock);
91447636
A
1363 socket_lock(inp->inp_socket, 0);
1364 }
6d2010ae 1365 if (inp->inp_laddr.s_addr == INADDR_ANY) {
39236c6e
A
1366 inp->inp_laddr = laddr;
1367 /* no reference needed */
316670eb 1368 inp->inp_last_outifp = outif;
5ba3f43e 1369
39236c6e 1370 inp->inp_flags |= INP_INADDR_ANY;
6d2010ae 1371 }
1c79356b
A
1372 inp->inp_faddr = sin->sin_addr;
1373 inp->inp_fport = sin->sin_port;
1374 in_pcbrehash(inp);
39236c6e 1375 lck_rw_done(inp->inp_pcbinfo->ipi_lock);
1c79356b 1376
316670eb
A
1377 if (inp->inp_flowhash == 0)
1378 inp->inp_flowhash = inp_calc_flowhash(inp);
1c79356b 1379
39037602 1380 tcp_set_max_rwinscale(tp, so, TCP_AUTORCVBUF_MAX(outif));
2d21ac55 1381
1c79356b
A
1382 soisconnecting(so);
1383 tcpstat.tcps_connattempt++;
1384 tp->t_state = TCPS_SYN_SENT;
39236c6e 1385 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp, TCP_CONN_KEEPINIT(tp));
9bccf70c 1386 tp->iss = tcp_new_isn(tp);
1c79356b 1387 tcp_sendseqinit(tp);
6d2010ae
A
1388 if (nstat_collect)
1389 nstat_route_connect_attempt(inp->inp_route.ro_rt);
1c79356b 1390
39236c6e
A
1391done:
1392 if (outif != NULL)
1393 ifnet_release(outif);
1394
1395 return (error);
1c79356b
A
1396}
1397
1398#if INET6
1399static int
39037602 1400tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct proc *p)
1c79356b
A
1401{
1402 struct inpcb *inp = tp->t_inpcb, *oinp;
1403 struct socket *so = inp->inp_socket;
1404 struct tcpcb *otp;
316670eb 1405 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(void *)nam;
91447636 1406 struct in6_addr addr6;
316670eb
A
1407 int error = 0;
1408 struct ifnet *outif = NULL;
1c79356b
A
1409
1410 if (inp->inp_lport == 0) {
39236c6e 1411 error = in6_pcbbind(inp, NULL, p);
1c79356b 1412 if (error)
316670eb 1413 goto done;
1c79356b
A
1414 }
1415
1416 /*
1417 * Cannot simply call in_pcbconnect, because there might be an
1418 * earlier incarnation of this same connection still in
1419 * TIME_WAIT state, creating an ADDRINUSE error.
316670eb
A
1420 *
1421 * in6_pcbladdr() might return an ifp with its reference held
1422 * even in the error case, so make sure that it's released
1423 * whenever it's non-NULL.
1c79356b 1424 */
6d2010ae 1425 error = in6_pcbladdr(inp, nam, &addr6, &outif);
1c79356b 1426 if (error)
316670eb 1427 goto done;
5ba3f43e 1428 socket_unlock(inp->inp_socket, 0);
1c79356b
A
1429 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1430 &sin6->sin6_addr, sin6->sin6_port,
1431 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
91447636 1432 ? &addr6
1c79356b
A
1433 : &inp->in6p_laddr,
1434 inp->inp_lport, 0, NULL);
5ba3f43e 1435 socket_lock(inp->inp_socket, 0);
1c79356b
A
1436 if (oinp) {
1437 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
1438 otp->t_state == TCPS_TIME_WAIT &&
6d2010ae 1439 ((int)(tcp_now - otp->t_starttime)) < tcp_msl &&
316670eb 1440 (otp->t_flags & TF_RCVD_CC)) {
1c79356b 1441 otp = tcp_close(otp);
316670eb
A
1442 } else {
1443 error = EADDRINUSE;
1444 goto done;
1445 }
1c79356b 1446 }
39236c6e 1447 if (!lck_rw_try_lock_exclusive(inp->inp_pcbinfo->ipi_lock)) {
91447636
A
1448 /*lock inversion issue, mostly with udp multicast packets */
1449 socket_unlock(inp->inp_socket, 0);
39236c6e 1450 lck_rw_lock_exclusive(inp->inp_pcbinfo->ipi_lock);
91447636
A
1451 socket_lock(inp->inp_socket, 0);
1452 }
6d2010ae 1453 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
91447636 1454 inp->in6p_laddr = addr6;
316670eb 1455 inp->in6p_last_outifp = outif; /* no reference needed */
39236c6e 1456 inp->in6p_flags |= INP_IN6ADDR_ANY;
6d2010ae 1457 }
1c79356b
A
1458 inp->in6p_faddr = sin6->sin6_addr;
1459 inp->inp_fport = sin6->sin6_port;
2d21ac55 1460 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != 0)
39236c6e 1461 inp->inp_flow = sin6->sin6_flowinfo;
1c79356b 1462 in_pcbrehash(inp);
39236c6e 1463 lck_rw_done(inp->inp_pcbinfo->ipi_lock);
1c79356b 1464
316670eb
A
1465 if (inp->inp_flowhash == 0)
1466 inp->inp_flowhash = inp_calc_flowhash(inp);
39236c6e
A
1467 /* update flowinfo - RFC 6437 */
1468 if (inp->inp_flow == 0 && inp->in6p_flags & IN6P_AUTOFLOWLABEL) {
1469 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
1470 inp->inp_flow |=
1471 (htonl(inp->inp_flowhash) & IPV6_FLOWLABEL_MASK);
1472 }
316670eb 1473
39037602 1474 tcp_set_max_rwinscale(tp, so, TCP_AUTORCVBUF_MAX(outif));
1c79356b
A
1475
1476 soisconnecting(so);
1477 tcpstat.tcps_connattempt++;
1478 tp->t_state = TCPS_SYN_SENT;
5ba3f43e 1479 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp,
39236c6e 1480 TCP_CONN_KEEPINIT(tp));
9bccf70c 1481 tp->iss = tcp_new_isn(tp);
1c79356b 1482 tcp_sendseqinit(tp);
6d2010ae
A
1483 if (nstat_collect)
1484 nstat_route_connect_attempt(inp->inp_route.ro_rt);
1c79356b 1485
316670eb
A
1486done:
1487 if (outif != NULL)
1488 ifnet_release(outif);
1489
1490 return (error);
1c79356b
A
1491}
1492#endif /* INET6 */
1493
6d2010ae
A
1494/*
1495 * Export TCP internal state information via a struct tcp_info
1496 */
5ba3f43e 1497void
6d2010ae
A
1498tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1499{
316670eb 1500 struct inpcb *inp = tp->t_inpcb;
5ba3f43e 1501
6d2010ae
A
1502 bzero(ti, sizeof(*ti));
1503
1504 ti->tcpi_state = tp->t_state;
4bd07ac2
A
1505 ti->tcpi_flowhash = inp->inp_flowhash;
1506
316670eb 1507 if (tp->t_state > TCPS_LISTEN) {
3e170ce0 1508 if (TSTMP_SUPPORTED(tp))
6d2010ae 1509 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
3e170ce0 1510 if (SACK_ENABLED(tp))
6d2010ae 1511 ti->tcpi_options |= TCPI_OPT_SACK;
3e170ce0 1512 if (TCP_WINDOW_SCALE_ENABLED(tp)) {
6d2010ae
A
1513 ti->tcpi_options |= TCPI_OPT_WSCALE;
1514 ti->tcpi_snd_wscale = tp->snd_scale;
1515 ti->tcpi_rcv_wscale = tp->rcv_scale;
1516 }
4bd07ac2
A
1517 if (TCP_ECN_ENABLED(tp))
1518 ti->tcpi_options |= TCPI_OPT_ECN;
316670eb
A
1519
1520 /* Are we in retranmission episode */
3e170ce0 1521 if (IN_FASTRECOVERY(tp) || tp->t_rxtshift > 0)
316670eb 1522 ti->tcpi_flags |= TCPI_FLAG_LOSSRECOVERY;
316670eb 1523
39037602
A
1524 if (tp->t_flags & TF_STREAMING_ON)
1525 ti->tcpi_flags |= TCPI_FLAG_STREAMING_ON;
1526
316670eb 1527 ti->tcpi_rto = tp->t_timer[TCPT_REXMT] ? tp->t_rxtcur : 0;
6d2010ae
A
1528 ti->tcpi_snd_mss = tp->t_maxseg;
1529 ti->tcpi_rcv_mss = tp->t_maxseg;
1530
316670eb
A
1531 ti->tcpi_rttcur = tp->t_rttcur;
1532 ti->tcpi_srtt = tp->t_srtt >> TCP_RTT_SHIFT;
1533 ti->tcpi_rttvar = tp->t_rttvar >> TCP_RTTVAR_SHIFT;
39236c6e 1534 ti->tcpi_rttbest = tp->t_rttbest >> TCP_RTT_SHIFT;
316670eb 1535
6d2010ae
A
1536 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1537 ti->tcpi_snd_cwnd = tp->snd_cwnd;
3e170ce0 1538 ti->tcpi_snd_sbbytes = inp->inp_socket->so_snd.sb_cc;
5ba3f43e 1539
6d2010ae
A
1540 ti->tcpi_rcv_space = tp->rcv_wnd;
1541
1542 ti->tcpi_snd_wnd = tp->snd_wnd;
6d2010ae
A
1543 ti->tcpi_snd_nxt = tp->snd_nxt;
1544 ti->tcpi_rcv_nxt = tp->rcv_nxt;
316670eb
A
1545
1546 /* convert bytes/msec to bits/sec */
1547 if ((tp->t_flagsext & TF_MEASURESNDBW) != 0 &&
1548 tp->t_bwmeas != NULL) {
1549 ti->tcpi_snd_bw = (tp->t_bwmeas->bw_sndbw * 8000);
1550 }
813fb2f6 1551
316670eb
A
1552 ti->tcpi_last_outif = (tp->t_inpcb->inp_last_outifp == NULL) ? 0 :
1553 tp->t_inpcb->inp_last_outifp->if_index;
1554
1555 //atomic_get_64(ti->tcpi_txbytes, &inp->inp_stat->txbytes);
39236c6e 1556 ti->tcpi_txpackets = inp->inp_stat->txpackets;
316670eb
A
1557 ti->tcpi_txbytes = inp->inp_stat->txbytes;
1558 ti->tcpi_txretransmitbytes = tp->t_stat.txretransmitbytes;
813fb2f6 1559 ti->tcpi_txretransmitpackets = tp->t_stat.rxmitpkts;
316670eb 1560 ti->tcpi_txunacked = tp->snd_max - tp->snd_una;
813fb2f6 1561
316670eb 1562 //atomic_get_64(ti->tcpi_rxbytes, &inp->inp_stat->rxbytes);
39236c6e 1563 ti->tcpi_rxpackets = inp->inp_stat->rxpackets;
316670eb
A
1564 ti->tcpi_rxbytes = inp->inp_stat->rxbytes;
1565 ti->tcpi_rxduplicatebytes = tp->t_stat.rxduplicatebytes;
39236c6e
A
1566 ti->tcpi_rxoutoforderbytes = tp->t_stat.rxoutoforderbytes;
1567
1568 if (tp->t_state > TCPS_LISTEN) {
1569 ti->tcpi_synrexmits = tp->t_stat.synrxtshift;
1570 }
1571 ti->tcpi_cell_rxpackets = inp->inp_cstat->rxpackets;
1572 ti->tcpi_cell_rxbytes = inp->inp_cstat->rxbytes;
1573 ti->tcpi_cell_txpackets = inp->inp_cstat->txpackets;
1574 ti->tcpi_cell_txbytes = inp->inp_cstat->txbytes;
1575
1576 ti->tcpi_wifi_rxpackets = inp->inp_wstat->rxpackets;
1577 ti->tcpi_wifi_rxbytes = inp->inp_wstat->rxbytes;
1578 ti->tcpi_wifi_txpackets = inp->inp_wstat->txpackets;
1579 ti->tcpi_wifi_txbytes = inp->inp_wstat->txbytes;
fe8ab488
A
1580
1581 ti->tcpi_wired_rxpackets = inp->inp_Wstat->rxpackets;
1582 ti->tcpi_wired_rxbytes = inp->inp_Wstat->rxbytes;
1583 ti->tcpi_wired_txpackets = inp->inp_Wstat->txpackets;
1584 ti->tcpi_wired_txbytes = inp->inp_Wstat->txbytes;
3e170ce0
A
1585 tcp_get_connectivity_status(tp, &ti->tcpi_connstatus);
1586
1587 ti->tcpi_tfo_syn_data_rcv = !!(tp->t_tfo_stats & TFO_S_SYNDATA_RCV);
1588 ti->tcpi_tfo_cookie_req_rcv = !!(tp->t_tfo_stats & TFO_S_COOKIEREQ_RECV);
1589 ti->tcpi_tfo_cookie_sent = !!(tp->t_tfo_stats & TFO_S_COOKIE_SENT);
1590 ti->tcpi_tfo_cookie_invalid = !!(tp->t_tfo_stats & TFO_S_COOKIE_INVALID);
1591
1592 ti->tcpi_tfo_cookie_req = !!(tp->t_tfo_stats & TFO_S_COOKIE_REQ);
1593 ti->tcpi_tfo_cookie_rcv = !!(tp->t_tfo_stats & TFO_S_COOKIE_RCV);
1594 ti->tcpi_tfo_syn_data_sent = !!(tp->t_tfo_stats & TFO_S_SYN_DATA_SENT);
1595 ti->tcpi_tfo_syn_data_acked = !!(tp->t_tfo_stats & TFO_S_SYN_DATA_ACKED);
1596 ti->tcpi_tfo_syn_loss = !!(tp->t_tfo_stats & TFO_S_SYN_LOSS);
39037602
A
1597 ti->tcpi_tfo_cookie_wrong = !!(tp->t_tfo_stats & TFO_S_COOKIE_WRONG);
1598 ti->tcpi_tfo_no_cookie_rcv = !!(tp->t_tfo_stats & TFO_S_NO_COOKIE_RCV);
1599 ti->tcpi_tfo_heuristics_disable = !!(tp->t_tfo_stats & TFO_S_HEURISTICS_DISABLE);
1600 ti->tcpi_tfo_send_blackhole = !!(tp->t_tfo_stats & TFO_S_SEND_BLACKHOLE);
1601 ti->tcpi_tfo_recv_blackhole = !!(tp->t_tfo_stats & TFO_S_RECV_BLACKHOLE);
5ba3f43e 1602 ti->tcpi_tfo_onebyte_proxy = !!(tp->t_tfo_stats & TFO_S_ONE_BYTE_PROXY);
4bd07ac2
A
1603
1604 ti->tcpi_ecn_client_setup = !!(tp->ecn_flags & TE_SETUPSENT);
1605 ti->tcpi_ecn_server_setup = !!(tp->ecn_flags & TE_SETUPRECEIVED);
1606 ti->tcpi_ecn_success = (tp->ecn_flags & TE_ECN_ON) == TE_ECN_ON ? 1 : 0;
1607 ti->tcpi_ecn_lost_syn = !!(tp->ecn_flags & TE_LOST_SYN);
1608 ti->tcpi_ecn_lost_synack = !!(tp->ecn_flags & TE_LOST_SYNACK);
1609
1610 ti->tcpi_local_peer = !!(tp->t_flags & TF_LOCAL);
1611
1612 if (tp->t_inpcb->inp_last_outifp != NULL) {
1613 if (IFNET_IS_CELLULAR(tp->t_inpcb->inp_last_outifp))
1614 ti->tcpi_if_cell = 1;
5ba3f43e 1615 if (IFNET_IS_WIFI(tp->t_inpcb->inp_last_outifp))
4bd07ac2 1616 ti->tcpi_if_wifi = 1;
5ba3f43e
A
1617 if (IFNET_IS_WIRED(tp->t_inpcb->inp_last_outifp))
1618 ti->tcpi_if_wired = 1;
1619 if (IFNET_IS_WIFI_INFRA(tp->t_inpcb->inp_last_outifp))
1620 ti->tcpi_if_wifi_infra = 1;
1621 if (tp->t_inpcb->inp_last_outifp->if_eflags & IFEF_AWDL)
1622 ti->tcpi_if_wifi_awdl = 1;
4bd07ac2 1623 }
5ba3f43e
A
1624 if (tp->tcp_cc_index == TCP_CC_ALGO_BACKGROUND_INDEX)
1625 ti->tcpi_snd_background = 1;
1626 if (tcp_recv_bg == 1 ||
1627 IS_TCP_RECV_BG(tp->t_inpcb->inp_socket))
1628 ti->tcpi_rcv_background = 1;
4bd07ac2
A
1629
1630 ti->tcpi_ecn_recv_ce = tp->t_ecn_recv_ce;
1631 ti->tcpi_ecn_recv_cwr = tp->t_ecn_recv_cwr;
1632
1633 ti->tcpi_rcvoopack = tp->t_rcvoopack;
1634 ti->tcpi_pawsdrop = tp->t_pawsdrop;
1635 ti->tcpi_sack_recovery_episode = tp->t_sack_recovery_episode;
1636 ti->tcpi_reordered_pkts = tp->t_reordered_pkts;
1637 ti->tcpi_dsack_sent = tp->t_dsack_sent;
1638 ti->tcpi_dsack_recvd = tp->t_dsack_recvd;
6d2010ae
A
1639 }
1640}
1641
1642__private_extern__ errno_t
1643tcp_fill_info_for_info_tuple(struct info_tuple *itpl, struct tcp_info *ti)
1644{
1645 struct inpcbinfo *pcbinfo = NULL;
1646 struct inpcb *inp = NULL;
1647 struct socket *so;
1648 struct tcpcb *tp;
5ba3f43e 1649
6d2010ae
A
1650 if (itpl->itpl_proto == IPPROTO_TCP)
1651 pcbinfo = &tcbinfo;
1652 else
1653 return EINVAL;
5ba3f43e 1654
6d2010ae
A
1655 if (itpl->itpl_local_sa.sa_family == AF_INET &&
1656 itpl->itpl_remote_sa.sa_family == AF_INET) {
5ba3f43e 1657 inp = in_pcblookup_hash(pcbinfo,
39236c6e
A
1658 itpl->itpl_remote_sin.sin_addr,
1659 itpl->itpl_remote_sin.sin_port,
1660 itpl->itpl_local_sin.sin_addr,
1661 itpl->itpl_local_sin.sin_port,
1662 0, NULL);
6d2010ae
A
1663 } else if (itpl->itpl_local_sa.sa_family == AF_INET6 &&
1664 itpl->itpl_remote_sa.sa_family == AF_INET6) {
1665 struct in6_addr ina6_local;
1666 struct in6_addr ina6_remote;
5ba3f43e 1667
6d2010ae 1668 ina6_local = itpl->itpl_local_sin6.sin6_addr;
5ba3f43e 1669 if (IN6_IS_SCOPE_LINKLOCAL(&ina6_local) &&
39236c6e 1670 itpl->itpl_local_sin6.sin6_scope_id)
6d2010ae
A
1671 ina6_local.s6_addr16[1] = htons(itpl->itpl_local_sin6.sin6_scope_id);
1672
1673 ina6_remote = itpl->itpl_remote_sin6.sin6_addr;
5ba3f43e 1674 if (IN6_IS_SCOPE_LINKLOCAL(&ina6_remote) &&
39236c6e 1675 itpl->itpl_remote_sin6.sin6_scope_id)
6d2010ae 1676 ina6_remote.s6_addr16[1] = htons(itpl->itpl_remote_sin6.sin6_scope_id);
5ba3f43e
A
1677
1678 inp = in6_pcblookup_hash(pcbinfo,
39236c6e
A
1679 &ina6_remote,
1680 itpl->itpl_remote_sin6.sin6_port,
1681 &ina6_local,
1682 itpl->itpl_local_sin6.sin6_port,
1683 0, NULL);
1684 } else {
6d2010ae 1685 return EINVAL;
39236c6e 1686 }
6d2010ae
A
1687 if (inp == NULL || (so = inp->inp_socket) == NULL)
1688 return ENOENT;
1689
1690 socket_lock(so, 0);
1691 if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) {
1692 socket_unlock(so, 0);
1693 return ENOENT;
1694 }
1695 tp = intotcpcb(inp);
1696
1697 tcp_fill_info(tp, ti);
1698 socket_unlock(so, 0);
1699
1700 return 0;
1701}
1702
3e170ce0
A
1703static void
1704tcp_connection_fill_info(struct tcpcb *tp, struct tcp_connection_info *tci)
1705{
1706 struct inpcb *inp = tp->t_inpcb;
1707
1708 bzero(tci, sizeof(*tci));
1709 tci->tcpi_state = tp->t_state;
1710 if (tp->t_state > TCPS_LISTEN) {
1711 if (TSTMP_SUPPORTED(tp))
1712 tci->tcpi_options |= TCPCI_OPT_TIMESTAMPS;
1713 if (SACK_ENABLED(tp))
1714 tci->tcpi_options |= TCPCI_OPT_SACK;
1715 if (TCP_WINDOW_SCALE_ENABLED(tp)) {
1716 tci->tcpi_options |= TCPCI_OPT_WSCALE;
1717 tci->tcpi_snd_wscale = tp->snd_scale;
1718 tci->tcpi_rcv_wscale = tp->rcv_scale;
1719 }
1720 if (TCP_ECN_ENABLED(tp))
1721 tci->tcpi_options |= TCPCI_OPT_ECN;
1722 if (IN_FASTRECOVERY(tp) || tp->t_rxtshift > 0)
1723 tci->tcpi_flags |= TCPCI_FLAG_LOSSRECOVERY;
1724 if (tp->t_flagsext & TF_PKTS_REORDERED)
1725 tci->tcpi_flags |= TCPCI_FLAG_REORDERING_DETECTED;
1726 tci->tcpi_rto = (tp->t_timer[TCPT_REXMT] > 0) ?
1727 tp->t_rxtcur : 0;
1728 tci->tcpi_maxseg = tp->t_maxseg;
1729 tci->tcpi_snd_ssthresh = tp->snd_ssthresh;
1730 tci->tcpi_snd_cwnd = tp->snd_cwnd;
1731 tci->tcpi_snd_wnd = tp->snd_wnd;
1732 tci->tcpi_snd_sbbytes = inp->inp_socket->so_snd.sb_cc;
1733 tci->tcpi_rcv_wnd = tp->rcv_wnd;
1734 tci->tcpi_rttcur = tp->t_rttcur;
1735 tci->tcpi_srtt = (tp->t_srtt >> TCP_RTT_SHIFT);
1736 tci->tcpi_rttvar = (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1737 tci->tcpi_txpackets = inp->inp_stat->txpackets;
1738 tci->tcpi_txbytes = inp->inp_stat->txbytes;
1739 tci->tcpi_txretransmitbytes = tp->t_stat.txretransmitbytes;
813fb2f6 1740 tci->tcpi_txretransmitpackets = tp->t_stat.rxmitpkts;
3e170ce0
A
1741 tci->tcpi_rxpackets = inp->inp_stat->rxpackets;
1742 tci->tcpi_rxbytes = inp->inp_stat->rxbytes;
1743 tci->tcpi_rxoutoforderbytes = tp->t_stat.rxoutoforderbytes;
1744
1745 tci->tcpi_tfo_syn_data_rcv = !!(tp->t_tfo_stats & TFO_S_SYNDATA_RCV);
1746 tci->tcpi_tfo_cookie_req_rcv = !!(tp->t_tfo_stats & TFO_S_COOKIEREQ_RECV);
1747 tci->tcpi_tfo_cookie_sent = !!(tp->t_tfo_stats & TFO_S_COOKIE_SENT);
1748 tci->tcpi_tfo_cookie_invalid = !!(tp->t_tfo_stats & TFO_S_COOKIE_INVALID);
1749 tci->tcpi_tfo_cookie_req = !!(tp->t_tfo_stats & TFO_S_COOKIE_REQ);
1750 tci->tcpi_tfo_cookie_rcv = !!(tp->t_tfo_stats & TFO_S_COOKIE_RCV);
1751 tci->tcpi_tfo_syn_data_sent = !!(tp->t_tfo_stats & TFO_S_SYN_DATA_SENT);
1752 tci->tcpi_tfo_syn_data_acked = !!(tp->t_tfo_stats & TFO_S_SYN_DATA_ACKED);
1753 tci->tcpi_tfo_syn_loss = !!(tp->t_tfo_stats & TFO_S_SYN_LOSS);
39037602
A
1754 tci->tcpi_tfo_cookie_wrong = !!(tp->t_tfo_stats & TFO_S_COOKIE_WRONG);
1755 tci->tcpi_tfo_no_cookie_rcv = !!(tp->t_tfo_stats & TFO_S_NO_COOKIE_RCV);
1756 tci->tcpi_tfo_heuristics_disable = !!(tp->t_tfo_stats & TFO_S_HEURISTICS_DISABLE);
1757 tci->tcpi_tfo_send_blackhole = !!(tp->t_tfo_stats & TFO_S_SEND_BLACKHOLE);
1758 tci->tcpi_tfo_recv_blackhole = !!(tp->t_tfo_stats & TFO_S_RECV_BLACKHOLE);
5ba3f43e 1759 tci->tcpi_tfo_onebyte_proxy = !!(tp->t_tfo_stats & TFO_S_ONE_BYTE_PROXY);
3e170ce0
A
1760 }
1761}
1762
6d2010ae 1763
5ba3f43e 1764__private_extern__ int
6d2010ae
A
1765tcp_sysctl_info(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1766{
1767 int error;
527f9951 1768 struct tcp_info ti = {};
6d2010ae 1769 struct info_tuple itpl;
5ba3f43e 1770#if !CONFIG_EMBEDDED
39236c6e
A
1771 proc_t caller = PROC_NULL;
1772 proc_t caller_parent = PROC_NULL;
1773 char command_name[MAXCOMLEN + 1] = "";
1774 char parent_name[MAXCOMLEN + 1] = "";
1775
1776 if ((caller = proc_self()) != PROC_NULL) {
1777 /* get process name */
1778 strlcpy(command_name, caller->p_comm, sizeof(command_name));
1779
1780 /* get parent process name if possible */
1781 if ((caller_parent = proc_find(caller->p_ppid)) != PROC_NULL) {
1782 strlcpy(parent_name, caller_parent->p_comm,
1783 sizeof(parent_name));
1784 proc_rele(caller_parent);
1785 }
1786
1787 if ((escape_str(command_name, strlen(command_name),
1788 sizeof(command_name)) == 0) &&
1789 (escape_str(parent_name, strlen(parent_name),
1790 sizeof(parent_name)) == 0)) {
1791 kern_asl_msg(LOG_DEBUG, "messagetracer",
1792 5,
1793 "com.apple.message.domain",
1794 "com.apple.kernel.tcpstat", /* 1 */
1795 "com.apple.message.signature",
1796 "tcpinfo", /* 2 */
1797 "com.apple.message.signature2", command_name, /* 3 */
1798 "com.apple.message.signature3", parent_name, /* 4 */
1799 "com.apple.message.summarize", "YES", /* 5 */
1800 NULL);
1801 }
1802 }
1803
1804 if (caller != PROC_NULL)
1805 proc_rele(caller);
5ba3f43e 1806#endif /* !CONFIG_EMBEDDED */
39236c6e 1807
6d2010ae
A
1808 if (req->newptr == USER_ADDR_NULL) {
1809 return EINVAL;
1810 }
1811 if (req->newlen < sizeof(struct info_tuple)) {
1812 return EINVAL;
1813 }
1814 error = SYSCTL_IN(req, &itpl, sizeof(struct info_tuple));
1815 if (error != 0) {
1816 return error;
1817 }
1818 error = tcp_fill_info_for_info_tuple(&itpl, &ti);
1819 if (error != 0) {
1820 return error;
1821 }
1822 error = SYSCTL_OUT(req, &ti, sizeof(struct tcp_info));
1823 if (error != 0) {
1824 return error;
1825 }
5ba3f43e 1826
6d2010ae
A
1827 return 0;
1828}
1829
316670eb
A
1830static int
1831tcp_lookup_peer_pid_locked(struct socket *so, pid_t *out_pid)
1832{
1833 int error = EHOSTUNREACH;
1834 *out_pid = -1;
1835 if ((so->so_state & SS_ISCONNECTED) == 0) return ENOTCONN;
5ba3f43e 1836
316670eb
A
1837 struct inpcb *inp = (struct inpcb*)so->so_pcb;
1838 uint16_t lport = inp->inp_lport;
1839 uint16_t fport = inp->inp_fport;
1840 struct inpcb *finp = NULL;
5ba3f43e 1841
316670eb
A
1842 if (inp->inp_vflag & INP_IPV6) {
1843 struct in6_addr laddr6 = inp->in6p_laddr;
1844 struct in6_addr faddr6 = inp->in6p_faddr;
1845 socket_unlock(so, 0);
1846 finp = in6_pcblookup_hash(&tcbinfo, &laddr6, lport, &faddr6, fport, 0, NULL);
1847 socket_lock(so, 0);
1848 } else if (inp->inp_vflag & INP_IPV4) {
1849 struct in_addr laddr4 = inp->inp_laddr;
1850 struct in_addr faddr4 = inp->inp_faddr;
1851 socket_unlock(so, 0);
1852 finp = in_pcblookup_hash(&tcbinfo, laddr4, lport, faddr4, fport, 0, NULL);
1853 socket_lock(so, 0);
1854 }
5ba3f43e 1855
316670eb
A
1856 if (finp) {
1857 *out_pid = finp->inp_socket->last_pid;
1858 error = 0;
5ba3f43e
A
1859 /* Avoid deadlock due to same inpcb for loopback socket */
1860 if (inp == finp)
1861 in_pcb_checkstate(finp, WNT_RELEASE, 1);
1862 else
1863 in_pcb_checkstate(finp, WNT_RELEASE, 0);
316670eb 1864 }
5ba3f43e 1865
316670eb
A
1866 return error;
1867}
1868
39236c6e
A
1869void
1870tcp_getconninfo(struct socket *so, struct conninfo_tcp *tcp_ci)
1871{
1872 (void) tcp_lookup_peer_pid_locked(so, &tcp_ci->tcpci_peer_pid);
1873 tcp_fill_info(sototcpcb(so), &tcp_ci->tcpci_tcp_info);
1874}
1875
1c79356b
A
1876/*
1877 * The new sockopt interface makes it possible for us to block in the
1878 * copyin/out step (if we take a page fault). Taking a page fault at
1879 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
1880 * use TSM, there probably isn't any need for this function to run at
1881 * splnet() any more. This needs more examination.)
1882 */
1883int
39037602 1884tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1c79356b 1885{
527f9951 1886 int error = 0, opt = 0, optval = 0;
1c79356b
A
1887 struct inpcb *inp;
1888 struct tcpcb *tp;
1889
1c79356b
A
1890 inp = sotoinpcb(so);
1891 if (inp == NULL) {
1c79356b
A
1892 return (ECONNRESET);
1893 }
39236c6e 1894 /* Allow <SOL_SOCKET,SO_FLUSH/SO_TRAFFIC_MGT_BACKGROUND> at this level */
316670eb 1895 if (sopt->sopt_level != IPPROTO_TCP &&
39236c6e
A
1896 !(sopt->sopt_level == SOL_SOCKET && (sopt->sopt_name == SO_FLUSH ||
1897 sopt->sopt_name == SO_TRAFFIC_MGT_BACKGROUND))) {
1c79356b 1898#if INET6
39236c6e 1899 if (SOCK_CHECK_DOM(so, PF_INET6))
1c79356b
A
1900 error = ip6_ctloutput(so, sopt);
1901 else
1902#endif /* INET6 */
1903 error = ip_ctloutput(so, sopt);
1c79356b
A
1904 return (error);
1905 }
1906 tp = intotcpcb(inp);
39236c6e
A
1907 if (tp == NULL) {
1908 return (ECONNRESET);
1909 }
1c79356b 1910
6d2010ae
A
1911 calculate_tcp_clock();
1912
1c79356b
A
1913 switch (sopt->sopt_dir) {
1914 case SOPT_SET:
1915 switch (sopt->sopt_name) {
1916 case TCP_NODELAY:
1917 case TCP_NOOPT:
1918 case TCP_NOPUSH:
1919 error = sooptcopyin(sopt, &optval, sizeof optval,
1920 sizeof optval);
1921 if (error)
1922 break;
1923
1924 switch (sopt->sopt_name) {
1925 case TCP_NODELAY:
1926 opt = TF_NODELAY;
1927 break;
1928 case TCP_NOOPT:
1929 opt = TF_NOOPT;
1930 break;
1931 case TCP_NOPUSH:
1932 opt = TF_NOPUSH;
1933 break;
1934 default:
1935 opt = 0; /* dead code to fool gcc */
1936 break;
1937 }
1938
1939 if (optval)
1940 tp->t_flags |= opt;
1941 else
1942 tp->t_flags &= ~opt;
1943 break;
6d2010ae 1944 case TCP_RXT_FINDROP:
fe8ab488 1945 case TCP_NOTIMEWAIT:
6d2010ae
A
1946 error = sooptcopyin(sopt, &optval, sizeof optval,
1947 sizeof optval);
1948 if (error)
1949 break;
fe8ab488
A
1950 switch (sopt->sopt_name) {
1951 case TCP_RXT_FINDROP:
1952 opt = TF_RXTFINDROP;
1953 break;
1954 case TCP_NOTIMEWAIT:
1955 opt = TF_NOTIMEWAIT;
1956 break;
1957 default:
1958 opt = 0;
1959 break;
1960 }
6d2010ae
A
1961 if (optval)
1962 tp->t_flagsext |= opt;
1963 else
1964 tp->t_flagsext &= ~opt;
1965 break;
316670eb
A
1966 case TCP_MEASURE_SND_BW:
1967 error = sooptcopyin(sopt, &optval, sizeof optval,
1968 sizeof optval);
1969 if (error)
1970 break;
1971 opt = TF_MEASURESNDBW;
1972 if (optval) {
1973 if (tp->t_bwmeas == NULL) {
1974 tp->t_bwmeas = tcp_bwmeas_alloc(tp);
1975 if (tp->t_bwmeas == NULL) {
1976 error = ENOMEM;
1977 break;
1978 }
1979 }
1980 tp->t_flagsext |= opt;
1981 } else {
1982 tp->t_flagsext &= ~opt;
1983 /* Reset snd bw measurement state */
1984 tp->t_flagsext &= ~(TF_BWMEAS_INPROGRESS);
1985 if (tp->t_bwmeas != NULL) {
1986 tcp_bwmeas_free(tp);
1987 }
1988 }
1989 break;
1990 case TCP_MEASURE_BW_BURST: {
1991 struct tcp_measure_bw_burst in;
1992 uint32_t minpkts, maxpkts;
1993 bzero(&in, sizeof(in));
1994
1995 error = sooptcopyin(sopt, &in, sizeof(in),
1996 sizeof(in));
1997 if (error)
1998 break;
1999 if ((tp->t_flagsext & TF_MEASURESNDBW) == 0 ||
2000 tp->t_bwmeas == NULL) {
2001 error = EINVAL;
2002 break;
2003 }
5ba3f43e 2004 minpkts = (in.min_burst_size != 0) ? in.min_burst_size :
316670eb
A
2005 tp->t_bwmeas->bw_minsizepkts;
2006 maxpkts = (in.max_burst_size != 0) ? in.max_burst_size :
2007 tp->t_bwmeas->bw_maxsizepkts;
2008 if (minpkts > maxpkts) {
2009 error = EINVAL;
2010 break;
2011 }
2012 tp->t_bwmeas->bw_minsizepkts = minpkts;
2013 tp->t_bwmeas->bw_maxsizepkts = maxpkts;
2014 tp->t_bwmeas->bw_minsize = (minpkts * tp->t_maxseg);
2015 tp->t_bwmeas->bw_maxsize = (maxpkts * tp->t_maxseg);
2016 break;
2017 }
1c79356b
A
2018 case TCP_MAXSEG:
2019 error = sooptcopyin(sopt, &optval, sizeof optval,
2020 sizeof optval);
2021 if (error)
2022 break;
2023
e5568f75
A
2024 if (optval > 0 && optval <= tp->t_maxseg &&
2025 optval + 40 >= tcp_minmss)
1c79356b
A
2026 tp->t_maxseg = optval;
2027 else
2028 error = EINVAL;
2029 break;
2030
2d21ac55
A
2031 case TCP_KEEPALIVE:
2032 error = sooptcopyin(sopt, &optval, sizeof optval,
2033 sizeof optval);
2034 if (error)
2035 break;
39236c6e 2036 if (optval < 0 || optval > UINT32_MAX/TCP_RETRANSHZ) {
2d21ac55 2037 error = EINVAL;
39236c6e 2038 } else {
2d21ac55 2039 tp->t_keepidle = optval * TCP_RETRANSHZ;
39236c6e 2040 /* reset the timer to new value */
5ba3f43e 2041 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp,
39236c6e 2042 TCP_CONN_KEEPIDLE(tp));
6d2010ae 2043 tcp_check_timer_state(tp);
2d21ac55 2044 }
55e303ae 2045 break;
b0d623f7
A
2046
2047 case TCP_CONNECTIONTIMEOUT:
2048 error = sooptcopyin(sopt, &optval, sizeof optval,
2049 sizeof optval);
2050 if (error)
2051 break;
39236c6e 2052 if (optval < 0 || optval > UINT32_MAX/TCP_RETRANSHZ) {
b0d623f7 2053 error = EINVAL;
39236c6e 2054 } else {
b0d623f7 2055 tp->t_keepinit = optval * TCP_RETRANSHZ;
39236c6e
A
2056 if (tp->t_state == TCPS_SYN_RECEIVED ||
2057 tp->t_state == TCPS_SYN_SENT) {
2058 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp,
2059 TCP_CONN_KEEPINIT(tp));
2060 tcp_check_timer_state(tp);
2061 }
2062 }
2063 break;
2064
2065 case TCP_KEEPINTVL:
2066 error = sooptcopyin(sopt, &optval, sizeof(optval),
2067 sizeof(optval));
2068 if (error)
2069 break;
2070 if (optval < 0 || optval > UINT32_MAX/TCP_RETRANSHZ) {
2071 error = EINVAL;
2072 } else {
2073 tp->t_keepintvl = optval * TCP_RETRANSHZ;
2074 if (tp->t_state == TCPS_FIN_WAIT_2 &&
2075 TCP_CONN_MAXIDLE(tp) > 0) {
2076 tp->t_timer[TCPT_2MSL] = OFFSET_FROM_START(tp,
2077 TCP_CONN_MAXIDLE(tp));
2078 tcp_check_timer_state(tp);
2079 }
2080 }
2081 break;
2082
2083 case TCP_KEEPCNT:
2084 error = sooptcopyin(sopt, &optval, sizeof(optval),
2085 sizeof(optval));
2086 if (error)
2087 break;
2088 if (optval < 0 || optval > INT32_MAX) {
2089 error = EINVAL;
2090 } else {
2091 tp->t_keepcnt = optval;
2092 if (tp->t_state == TCPS_FIN_WAIT_2 &&
2093 TCP_CONN_MAXIDLE(tp) > 0) {
2094 tp->t_timer[TCPT_2MSL] = OFFSET_FROM_START(tp,
2095 TCP_CONN_MAXIDLE(tp));
2096 tcp_check_timer_state(tp);
2097 }
2098 }
b0d623f7 2099 break;
316670eb 2100
39037602
A
2101 case TCP_KEEPALIVE_OFFLOAD:
2102 error = sooptcopyin(sopt, &optval, sizeof(optval),
2103 sizeof(optval));
2104 if (error)
2105 break;
2106 if (optval < 0 || optval > INT32_MAX) {
2107 error = EINVAL;
2108 break;
5ba3f43e 2109 }
39037602
A
2110 if (optval != 0)
2111 inp->inp_flags2 |= INP2_KEEPALIVE_OFFLOAD;
2112 else
2113 inp->inp_flags2 &= ~INP2_KEEPALIVE_OFFLOAD;
2114 break;
2115
6d2010ae
A
2116 case PERSIST_TIMEOUT:
2117 error = sooptcopyin(sopt, &optval, sizeof optval,
2118 sizeof optval);
2119 if (error)
2120 break;
2121 if (optval < 0)
2122 error = EINVAL;
5ba3f43e 2123 else
6d2010ae
A
2124 tp->t_persist_timeout = optval * TCP_RETRANSHZ;
2125 break;
2126 case TCP_RXT_CONNDROPTIME:
2127 error = sooptcopyin(sopt, &optval, sizeof(optval),
2128 sizeof(optval));
2129 if (error)
2130 break;
2131 if (optval < 0)
2132 error = EINVAL;
2133 else
39236c6e 2134 tp->t_rxt_conndroptime = optval * TCP_RETRANSHZ;
6d2010ae 2135 break;
316670eb
A
2136 case TCP_NOTSENT_LOWAT:
2137 error = sooptcopyin(sopt, &optval, sizeof(optval),
2138 sizeof(optval));
2139 if (error)
2140 break;
2141 if (optval < 0) {
2142 error = EINVAL;
2143 break;
2144 } else {
2145 if (optval == 0) {
2146 so->so_flags &= ~(SOF_NOTSENT_LOWAT);
2147 tp->t_notsent_lowat = 0;
5ba3f43e 2148 } else {
316670eb
A
2149 so->so_flags |= SOF_NOTSENT_LOWAT;
2150 tp->t_notsent_lowat = optval;
2151 }
2152 }
2153 break;
39236c6e
A
2154 case TCP_ADAPTIVE_READ_TIMEOUT:
2155 error = sooptcopyin(sopt, &optval, sizeof (optval),
3e170ce0 2156 sizeof(optval));
39236c6e
A
2157 if (error)
2158 break;
5ba3f43e 2159 if (optval < 0 ||
3e170ce0 2160 optval > TCP_ADAPTIVE_TIMEOUT_MAX) {
39236c6e
A
2161 error = EINVAL;
2162 break;
2163 } else if (optval == 0) {
2164 tp->t_adaptive_rtimo = 0;
2165 tcp_keepalive_reset(tp);
5ba3f43e
A
2166
2167 if (tp->t_mpsub)
2168 mptcp_reset_keepalive(tp);
39236c6e
A
2169 } else {
2170 tp->t_adaptive_rtimo = optval;
2171 }
2172 break;
2173 case TCP_ADAPTIVE_WRITE_TIMEOUT:
2174 error = sooptcopyin(sopt, &optval, sizeof (optval),
3e170ce0 2175 sizeof (optval));
39236c6e
A
2176 if (error)
2177 break;
5ba3f43e 2178 if (optval < 0 ||
3e170ce0 2179 optval > TCP_ADAPTIVE_TIMEOUT_MAX) {
39236c6e
A
2180 error = EINVAL;
2181 break;
2182 } else {
2183 tp->t_adaptive_wtimo = optval;
2184 }
2185 break;
2186 case TCP_ENABLE_MSGS:
2187 error = sooptcopyin(sopt, &optval, sizeof(optval),
2188 sizeof(optval));
2189 if (error)
2190 break;
2191 if (optval < 0 || optval > 1) {
2192 error = EINVAL;
2193 } else if (optval == 1) {
2194 /*
2195 * Check if messages option is already
2196 * enabled, if so return.
2197 */
2198 if (so->so_flags & SOF_ENABLE_MSGS) {
2199 VERIFY(so->so_msg_state != NULL);
2200 break;
2201 }
316670eb 2202
39236c6e 2203 /*
39037602 2204 * allocate memory for storing message
39236c6e
A
2205 * related state
2206 */
2207 VERIFY(so->so_msg_state == NULL);
39037602 2208 MALLOC(so->so_msg_state,
39236c6e 2209 struct msg_state *,
39037602 2210 sizeof(struct msg_state),
39236c6e
A
2211 M_TEMP, M_WAITOK | M_ZERO);
2212 if (so->so_msg_state == NULL) {
2213 error = ENOMEM;
2214 break;
2215 }
2216
2217 /* Enable message delivery */
2218 so->so_flags |= SOF_ENABLE_MSGS;
2219 } else {
39037602
A
2220 /*
2221 * Can't disable message delivery on socket
2222 * because of restrictions imposed by
39236c6e
A
2223 * encoding/decoding
2224 */
2225 error = EINVAL;
2226 }
2227 break;
2228 case TCP_SENDMOREACKS:
2229 error = sooptcopyin(sopt, &optval, sizeof(optval),
2230 sizeof(optval));
2231 if (error)
2232 break;
2233 if (optval < 0 || optval > 1) {
2234 error = EINVAL;
2235 } else if (optval == 0) {
2236 tp->t_flagsext &= ~(TF_NOSTRETCHACK);
2237 } else {
2238 tp->t_flagsext |= TF_NOSTRETCHACK;
2239 }
2240 break;
fe8ab488
A
2241 case TCP_DISABLE_BLACKHOLE_DETECTION:
2242 error = sooptcopyin(sopt, &optval, sizeof(optval),
2243 sizeof(optval));
2244 if (error)
2245 break;
2246 if (optval < 0 || optval > 1) {
2247 error = EINVAL;
2248 } else if (optval == 0) {
2249 tp->t_flagsext &= ~TF_NOBLACKHOLE_DETECTION;
2250 } else {
2251 tp->t_flagsext |= TF_NOBLACKHOLE_DETECTION;
2252 if ((tp->t_flags & TF_BLACKHOLE) &&
2253 tp->t_pmtud_saved_maxopd > 0)
2254 tcp_pmtud_revert_segment_size(tp);
2255 }
2256 break;
3e170ce0
A
2257 case TCP_FASTOPEN:
2258 if (!(tcp_fastopen & TCP_FASTOPEN_SERVER)) {
2259 error = ENOTSUP;
2260 break;
2261 }
2262
2263 error = sooptcopyin(sopt, &optval, sizeof(optval),
2264 sizeof(optval));
2265 if (error)
2266 break;
2267 if (optval < 0 || optval > 1) {
2268 error = EINVAL;
2269 break;
2270 }
2271 if (tp->t_state != TCPS_LISTEN) {
2272 error = EINVAL;
2273 break;
2274 }
2275 if (optval)
2276 tp->t_flagsext |= TF_FASTOPEN;
2277 else
2278 tcp_disable_tfo(tp);
2279 break;
5ba3f43e
A
2280 case TCP_FASTOPEN_FORCE_HEURISTICS:
2281 error = sooptcopyin(sopt, &optval, sizeof(optval),
2282 sizeof(optval));
2283
2284 if (error)
2285 break;
2286 if (optval < 0 || optval > 1) {
2287 error = EINVAL;
2288 break;
2289 }
2290
2291 if (tp->t_state != TCPS_CLOSED) {
2292 error = EINVAL;
2293 break;
2294 }
2295 if (optval)
2296 tp->t_flagsext |= TF_FASTOPEN_HEUR;
2297 else
2298 tp->t_flagsext &= ~TF_FASTOPEN_HEUR;
2299
2300 break;
4bd07ac2
A
2301 case TCP_ENABLE_ECN:
2302 error = sooptcopyin(sopt, &optval, sizeof optval,
2303 sizeof optval);
2304 if (error)
2305 break;
2306 if (optval) {
2307 tp->ecn_flags |= TE_ECN_MODE_ENABLE;
2308 tp->ecn_flags &= ~TE_ECN_MODE_DISABLE;
2309 } else {
2310 tp->ecn_flags &= ~TE_ECN_MODE_ENABLE;
5ba3f43e 2311 tp->ecn_flags |= TE_ECN_MODE_DISABLE;
4bd07ac2
A
2312 }
2313 break;
2314 case TCP_ECN_MODE:
2315 error = sooptcopyin(sopt, &optval, sizeof optval,
2316 sizeof optval);
2317 if (error)
2318 break;
2319 if (optval == ECN_MODE_DEFAULT) {
2320 tp->ecn_flags &= ~TE_ECN_MODE_ENABLE;
2321 tp->ecn_flags &= ~TE_ECN_MODE_DISABLE;
2322 } else if (optval == ECN_MODE_ENABLE) {
2323 tp->ecn_flags |= TE_ECN_MODE_ENABLE;
2324 tp->ecn_flags &= ~TE_ECN_MODE_DISABLE;
2325 } else if (optval == ECN_MODE_DISABLE) {
2326 tp->ecn_flags &= ~TE_ECN_MODE_ENABLE;
2327 tp->ecn_flags |= TE_ECN_MODE_DISABLE;
2328 } else {
2329 error = EINVAL;
2330 }
2331 break;
39037602
A
2332 case TCP_NOTIFY_ACKNOWLEDGEMENT:
2333 error = sooptcopyin(sopt, &optval,
2334 sizeof(optval), sizeof(optval));
2335 if (error)
2336 break;
2337 if (optval <= 0) {
2338 error = EINVAL;
2339 break;
2340 }
2341 if (tp->t_notify_ack_count >= TCP_MAX_NOTIFY_ACK) {
2342 error = ETOOMANYREFS;
2343 break;
2344 }
2345
2346 /*
2347 * validate that the given marker id is not
2348 * a duplicate to avoid ambiguity
2349 */
2350 if ((error = tcp_notify_ack_id_valid(tp, so,
2351 optval)) != 0) {
2352 break;
2353 }
2354 error = tcp_add_notify_ack_marker(tp, optval);
2355 break;
316670eb
A
2356 case SO_FLUSH:
2357 if ((error = sooptcopyin(sopt, &optval, sizeof (optval),
2358 sizeof (optval))) != 0)
2359 break;
2360
2361 error = inp_flush(inp, optval);
2362 break;
2363
39236c6e
A
2364 case SO_TRAFFIC_MGT_BACKGROUND:
2365 if ((error = sooptcopyin(sopt, &optval, sizeof (optval),
2366 sizeof (optval))) != 0)
2367 break;
2368
2369 if (optval) {
2370 socket_set_traffic_mgt_flags_locked(so,
2371 TRAFFIC_MGT_SO_BACKGROUND);
2372 } else {
2373 socket_clear_traffic_mgt_flags_locked(so,
2374 TRAFFIC_MGT_SO_BACKGROUND);
2375 }
2376 break;
5ba3f43e
A
2377 case TCP_RXT_MINIMUM_TIMEOUT:
2378 error = sooptcopyin(sopt, &optval, sizeof(optval),
2379 sizeof(optval));
2380 if (error)
2381 break;
2382 if (optval < 0) {
2383 error = EINVAL;
2384 break;
2385 }
2386 if (optval == 0) {
2387 tp->t_rxt_minimum_timeout = 0;
2388 } else {
2389 tp->t_rxt_minimum_timeout = min(optval,
2390 TCP_RXT_MINIMUM_TIMEOUT_LIMIT);
2391 /* convert to milliseconds */
2392 tp->t_rxt_minimum_timeout *= TCP_RETRANSHZ;
2393 }
2394 break;
1c79356b
A
2395 default:
2396 error = ENOPROTOOPT;
2397 break;
2398 }
2399 break;
2400
2401 case SOPT_GET:
2402 switch (sopt->sopt_name) {
2403 case TCP_NODELAY:
2404 optval = tp->t_flags & TF_NODELAY;
2405 break;
2406 case TCP_MAXSEG:
2407 optval = tp->t_maxseg;
2408 break;
55e303ae 2409 case TCP_KEEPALIVE:
39037602
A
2410 if (tp->t_keepidle > 0)
2411 optval = tp->t_keepidle / TCP_RETRANSHZ;
2412 else
2413 optval = tcp_keepidle / TCP_RETRANSHZ;
55e303ae 2414 break;
39236c6e 2415 case TCP_KEEPINTVL:
39037602
A
2416 if (tp->t_keepintvl > 0)
2417 optval = tp->t_keepintvl / TCP_RETRANSHZ;
2418 else
2419 optval = tcp_keepintvl / TCP_RETRANSHZ;
39236c6e
A
2420 break;
2421 case TCP_KEEPCNT:
39037602
A
2422 if (tp->t_keepcnt > 0)
2423 optval = tp->t_keepcnt;
2424 else
2425 optval = tcp_keepcnt;
2426 break;
2427 case TCP_KEEPALIVE_OFFLOAD:
2428 optval = !!(inp->inp_flags2 & INP2_KEEPALIVE_OFFLOAD);
39236c6e 2429 break;
1c79356b
A
2430 case TCP_NOOPT:
2431 optval = tp->t_flags & TF_NOOPT;
2432 break;
2433 case TCP_NOPUSH:
2434 optval = tp->t_flags & TF_NOPUSH;
2435 break;
fe8ab488 2436 case TCP_ENABLE_ECN:
4bd07ac2
A
2437 optval = (tp->ecn_flags & TE_ECN_MODE_ENABLE) ? 1 : 0;
2438 break;
2439 case TCP_ECN_MODE:
2440 if (tp->ecn_flags & TE_ECN_MODE_ENABLE)
2441 optval = ECN_MODE_ENABLE;
2442 else if (tp->ecn_flags & TE_ECN_MODE_DISABLE)
2443 optval = ECN_MODE_DISABLE;
2444 else
2445 optval = ECN_MODE_DEFAULT;
fe8ab488 2446 break;
b0d623f7
A
2447 case TCP_CONNECTIONTIMEOUT:
2448 optval = tp->t_keepinit / TCP_RETRANSHZ;
2449 break;
6d2010ae
A
2450 case PERSIST_TIMEOUT:
2451 optval = tp->t_persist_timeout / TCP_RETRANSHZ;
2452 break;
2453 case TCP_RXT_CONNDROPTIME:
39236c6e 2454 optval = tp->t_rxt_conndroptime / TCP_RETRANSHZ;
6d2010ae
A
2455 break;
2456 case TCP_RXT_FINDROP:
2457 optval = tp->t_flagsext & TF_RXTFINDROP;
5ba3f43e 2458 break;
fe8ab488
A
2459 case TCP_NOTIMEWAIT:
2460 optval = (tp->t_flagsext & TF_NOTIMEWAIT) ? 1 : 0;
2461 break;
3e170ce0
A
2462 case TCP_FASTOPEN:
2463 if (tp->t_state != TCPS_LISTEN ||
2464 !(tcp_fastopen & TCP_FASTOPEN_SERVER)) {
2465 error = ENOTSUP;
2466 break;
2467 }
2468 optval = tfo_enabled(tp);
2469 break;
5ba3f43e
A
2470 case TCP_FASTOPEN_FORCE_HEURISTICS:
2471 optval = (tp->t_flagsext & TF_FASTOPEN_HEUR) ? 1 : 0;
2472 break;
316670eb
A
2473 case TCP_MEASURE_SND_BW:
2474 optval = tp->t_flagsext & TF_MEASURESNDBW;
2475 break;
6d2010ae
A
2476 case TCP_INFO: {
2477 struct tcp_info ti;
2478
2479 tcp_fill_info(tp, &ti);
2480 error = sooptcopyout(sopt, &ti, sizeof(struct tcp_info));
2481 goto done;
316670eb
A
2482 /* NOT REACHED */
2483 }
3e170ce0
A
2484 case TCP_CONNECTION_INFO: {
2485 struct tcp_connection_info tci;
2486 tcp_connection_fill_info(tp, &tci);
2487 error = sooptcopyout(sopt, &tci,
2488 sizeof(struct tcp_connection_info));
2489 goto done;
2490 }
316670eb 2491 case TCP_MEASURE_BW_BURST: {
527f9951 2492 struct tcp_measure_bw_burst out = {};
316670eb
A
2493 if ((tp->t_flagsext & TF_MEASURESNDBW) == 0 ||
2494 tp->t_bwmeas == NULL) {
2495 error = EINVAL;
2496 break;
2497 }
2498 out.min_burst_size = tp->t_bwmeas->bw_minsizepkts;
2499 out.max_burst_size = tp->t_bwmeas->bw_maxsizepkts;
2500 error = sooptcopyout(sopt, &out, sizeof(out));
2501 goto done;
2502 }
2503 case TCP_NOTSENT_LOWAT:
2504 if ((so->so_flags & SOF_NOTSENT_LOWAT) != 0) {
2505 optval = tp->t_notsent_lowat;
2506 } else {
2507 optval = 0;
2508 }
2509 break;
39236c6e
A
2510
2511 case TCP_ENABLE_MSGS:
2512 if (so->so_flags & SOF_ENABLE_MSGS) {
2513 optval = 1;
2514 } else {
2515 optval = 0;
2516 }
2517 break;
2518 case TCP_SENDMOREACKS:
2519 if (tp->t_flagsext & TF_NOSTRETCHACK)
2520 optval = 1;
2521 else
2522 optval = 0;
2523 break;
fe8ab488
A
2524 case TCP_DISABLE_BLACKHOLE_DETECTION:
2525 if (tp->t_flagsext & TF_NOBLACKHOLE_DETECTION)
2526 optval = 1;
2527 else
2528 optval = 0;
2529 break;
316670eb
A
2530 case TCP_PEER_PID: {
2531 pid_t pid;
2532 error = tcp_lookup_peer_pid_locked(so, &pid);
2533 if (error == 0)
2534 error = sooptcopyout(sopt, &pid, sizeof(pid));
2535 goto done;
6d2010ae 2536 }
39236c6e
A
2537 case TCP_ADAPTIVE_READ_TIMEOUT:
2538 optval = tp->t_adaptive_rtimo;
2539 break;
2540 case TCP_ADAPTIVE_WRITE_TIMEOUT:
2541 optval = tp->t_adaptive_wtimo;
2542 break;
2543 case SO_TRAFFIC_MGT_BACKGROUND:
39037602
A
2544 optval = (so->so_flags1 &
2545 SOF1_TRAFFIC_MGT_SO_BACKGROUND) ? 1 : 0;
39236c6e 2546 break;
39037602
A
2547 case TCP_NOTIFY_ACKNOWLEDGEMENT: {
2548 struct tcp_notify_ack_complete retid;
2549
2550 if (sopt->sopt_valsize != sizeof (retid)) {
2551 error = EINVAL;
2552 break;
2553 }
2554 bzero(&retid, sizeof (retid));
2555 tcp_get_notify_ack_count(tp, &retid);
2556 if (retid.notify_complete_count > 0)
2557 tcp_get_notify_ack_ids(tp, &retid);
2558
2559 error = sooptcopyout(sopt, &retid, sizeof (retid));
2560 goto done;
2561 }
5ba3f43e
A
2562 case TCP_RXT_MINIMUM_TIMEOUT:
2563 optval = tp->t_rxt_minimum_timeout / TCP_RETRANSHZ;
2564 break;
1c79356b
A
2565 default:
2566 error = ENOPROTOOPT;
2567 break;
2568 }
2569 if (error == 0)
2570 error = sooptcopyout(sopt, &optval, sizeof optval);
2571 break;
2572 }
6d2010ae 2573done:
1c79356b
A
2574 return (error);
2575}
2576
2577/*
2578 * tcp_sendspace and tcp_recvspace are the default send and receive window
2579 * sizes, respectively. These are obsolescent (this information should
2580 * be set by the route).
2581 */
b0d623f7
A
2582u_int32_t tcp_sendspace = 1448*256;
2583u_int32_t tcp_recvspace = 1448*384;
2584
2585/* During attach, the size of socket buffer allocated is limited to
2586 * sb_max in sbreserve. Disallow setting the tcp send and recv space
2587 * to be more than sb_max because that will cause tcp_attach to fail
2588 * (see radar 5713060)
5ba3f43e 2589 */
b0d623f7
A
2590static int
2591sysctl_tcp_sospace(struct sysctl_oid *oidp, __unused void *arg1,
5ba3f43e
A
2592 int arg2, struct sysctl_req *req)
2593{
2594#pragma unused(arg2)
b0d623f7
A
2595 u_int32_t new_value = 0, *space_p = NULL;
2596 int changed = 0, error = 0;
2597 u_quad_t sb_effective_max = (sb_max / (MSIZE+MCLBYTES)) * MCLBYTES;
2598
2599 switch (oidp->oid_number) {
2600 case TCPCTL_SENDSPACE:
2601 space_p = &tcp_sendspace;
2602 break;
2603 case TCPCTL_RECVSPACE:
2604 space_p = &tcp_recvspace;
2605 break;
2606 default:
2607 return EINVAL;
2608 }
2609 error = sysctl_io_number(req, *space_p, sizeof(u_int32_t),
2610 &new_value, &changed);
2611 if (changed) {
2612 if (new_value > 0 && new_value <= sb_effective_max) {
2613 *space_p = new_value;
5ba3f43e 2614 SYSCTL_SKMEM_UPDATE_AT_OFFSET(arg2, new_value);
b0d623f7
A
2615 } else {
2616 error = ERANGE;
2617 }
2618 }
2619 return error;
2620}
2621
5ba3f43e
A
2622#if SYSCTL_SKMEM
2623SYSCTL_PROC(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace,
2624 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_sendspace,
2625 offsetof(skmem_sysctl, tcp.sendspace), sysctl_tcp_sospace,
2626 "IU", "Maximum outgoing TCP datagram size");
2627SYSCTL_PROC(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace,
2628 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_recvspace,
2629 offsetof(skmem_sysctl, tcp.recvspace), sysctl_tcp_sospace,
2630 "IU", "Maximum incoming TCP datagram size");
2631#else /* SYSCTL_SKMEM */
6d2010ae 2632SYSCTL_PROC(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
b0d623f7 2633 &tcp_sendspace , 0, &sysctl_tcp_sospace, "IU", "Maximum outgoing TCP datagram size");
6d2010ae 2634SYSCTL_PROC(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
b0d623f7 2635 &tcp_recvspace , 0, &sysctl_tcp_sospace, "IU", "Maximum incoming TCP datagram size");
5ba3f43e 2636#endif /* SYSCTL_SKMEM */
1c79356b
A
2637
2638/*
2639 * Attach TCP protocol to socket, allocating
2640 * internet protocol control block, tcp control block,
2641 * bufer space, and entering LISTEN state if to accept connections.
2d21ac55
A
2642 *
2643 * Returns: 0 Success
2644 * in_pcballoc:ENOBUFS
2645 * in_pcballoc:ENOMEM
2646 * in_pcballoc:??? [IPSEC specific]
2647 * soreserve:ENOBUFS
1c79356b
A
2648 */
2649static int
39037602 2650tcp_attach(struct socket *so, struct proc *p)
1c79356b 2651{
39037602 2652 struct tcpcb *tp;
1c79356b
A
2653 struct inpcb *inp;
2654 int error;
2655#if INET6
39236c6e 2656 int isipv6 = SOCK_CHECK_DOM(so, PF_INET6) != 0;
9bccf70c 2657#endif
1c79356b 2658
1c79356b
A
2659 error = in_pcballoc(so, &tcbinfo, p);
2660 if (error)
2661 return (error);
55e303ae 2662
1c79356b 2663 inp = sotoinpcb(so);
55e303ae
A
2664
2665 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
316670eb 2666 error = soreserve(so, tcp_sendspace, tcp_recvspace);
55e303ae
A
2667 if (error)
2668 return (error);
2669 }
3e170ce0
A
2670
2671 if (so->so_snd.sb_preconn_hiwat == 0) {
39037602 2672 soreserve_preconnect(so, 2048);
3e170ce0
A
2673 }
2674
316670eb
A
2675 if ((so->so_rcv.sb_flags & SB_USRSIZE) == 0)
2676 so->so_rcv.sb_flags |= SB_AUTOSIZE;
2677 if ((so->so_snd.sb_flags & SB_USRSIZE) == 0)
2678 so->so_snd.sb_flags |= SB_AUTOSIZE;
55e303ae 2679
1c79356b
A
2680#if INET6
2681 if (isipv6) {
2682 inp->inp_vflag |= INP_IPV6;
2683 inp->in6p_hops = -1; /* use kernel default */
2684 }
2685 else
2686#endif /* INET6 */
2687 inp->inp_vflag |= INP_IPV4;
2688 tp = tcp_newtcpcb(inp);
39236c6e 2689 if (tp == NULL) {
1c79356b
A
2690 int nofd = so->so_state & SS_NOFDREF; /* XXX */
2691
2692 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
2693#if INET6
2694 if (isipv6)
2695 in6_pcbdetach(inp);
2696 else
2697#endif /* INET6 */
2698 in_pcbdetach(inp);
2699 so->so_state |= nofd;
2700 return (ENOBUFS);
2701 }
fe8ab488 2702 if (nstat_collect)
6d2010ae 2703 nstat_tcp_new_pcb(inp);
1c79356b
A
2704 tp->t_state = TCPS_CLOSED;
2705 return (0);
2706}
2707
2708/*
2709 * Initiate (or continue) disconnect.
2710 * If embryonic state, just send reset (once).
2711 * If in ``let data drain'' option and linger null, just drop.
2712 * Otherwise (hard), mark socket disconnecting and drop
2713 * current input data; switch states based on user close, and
2714 * send segment to peer (with FIN).
2715 */
2716static struct tcpcb *
39037602 2717tcp_disconnect(struct tcpcb *tp)
1c79356b
A
2718{
2719 struct socket *so = tp->t_inpcb->inp_socket;
2720
5c9f4661
A
2721 if (so->so_rcv.sb_cc != 0 || tp->t_reassqlen != 0)
2722 return tcp_drop(tp, 0);
2723
1c79356b
A
2724 if (tp->t_state < TCPS_ESTABLISHED)
2725 tp = tcp_close(tp);
2726 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
2727 tp = tcp_drop(tp, 0);
2728 else {
2729 soisdisconnecting(so);
2730 sbflush(&so->so_rcv);
2731 tp = tcp_usrclosed(tp);
39236c6e
A
2732#if MPTCP
2733 /* A reset has been sent but socket exists, do not send FIN */
2734 if ((so->so_flags & SOF_MP_SUBFLOW) &&
2735 (tp) && (tp->t_mpflags & TMPF_RESET))
2736 return (tp);
2737#endif
1c79356b
A
2738 if (tp)
2739 (void) tcp_output(tp);
2740 }
2741 return (tp);
2742}
2743
2744/*
2745 * User issued close, and wish to trail through shutdown states:
2746 * if never received SYN, just forget it. If got a SYN from peer,
2747 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2748 * If already got a FIN from peer, then almost done; go to LAST_ACK
2749 * state. In all other cases, have already sent FIN to peer (e.g.
2750 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2751 * for peer to send FIN or not respond to keep-alives, etc.
2752 * We can let the user exit from the close as soon as the FIN is acked.
2753 */
2754static struct tcpcb *
39037602 2755tcp_usrclosed(struct tcpcb *tp)
1c79356b 2756{
1c79356b
A
2757 switch (tp->t_state) {
2758
2759 case TCPS_CLOSED:
2760 case TCPS_LISTEN:
1c79356b
A
2761 tp = tcp_close(tp);
2762 break;
2763
2764 case TCPS_SYN_SENT:
2765 case TCPS_SYN_RECEIVED:
2766 tp->t_flags |= TF_NEEDFIN;
2767 break;
2768
2769 case TCPS_ESTABLISHED:
5ba3f43e 2770 DTRACE_TCP4(state__change, void, NULL,
39236c6e 2771 struct inpcb *, tp->t_inpcb,
5ba3f43e 2772 struct tcpcb *, tp,
39236c6e 2773 int32_t, TCPS_FIN_WAIT_1);
1c79356b
A
2774 tp->t_state = TCPS_FIN_WAIT_1;
2775 break;
2776
2777 case TCPS_CLOSE_WAIT:
5ba3f43e 2778 DTRACE_TCP4(state__change, void, NULL,
39236c6e 2779 struct inpcb *, tp->t_inpcb,
5ba3f43e 2780 struct tcpcb *, tp,
39236c6e 2781 int32_t, TCPS_LAST_ACK);
1c79356b
A
2782 tp->t_state = TCPS_LAST_ACK;
2783 break;
2784 }
2785 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
2786 soisdisconnected(tp->t_inpcb->inp_socket);
2787 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
2788 if (tp->t_state == TCPS_FIN_WAIT_2)
5ba3f43e 2789 tp->t_timer[TCPT_2MSL] = OFFSET_FROM_START(tp,
39236c6e 2790 TCP_CONN_MAXIDLE(tp));
1c79356b
A
2791 }
2792 return (tp);
2793}
2794
2d21ac55
A
2795void
2796tcp_in_cksum_stats(u_int32_t len)
2797{
39236c6e
A
2798 tcpstat.tcps_rcv_swcsum++;
2799 tcpstat.tcps_rcv_swcsum_bytes += len;
2d21ac55
A
2800}
2801
2802void
2803tcp_out_cksum_stats(u_int32_t len)
2804{
39236c6e
A
2805 tcpstat.tcps_snd_swcsum++;
2806 tcpstat.tcps_snd_swcsum_bytes += len;
2807}
2808
2809#if INET6
2810void
2811tcp_in6_cksum_stats(u_int32_t len)
2812{
2813 tcpstat.tcps_rcv6_swcsum++;
2814 tcpstat.tcps_rcv6_swcsum_bytes += len;
2d21ac55 2815}
39236c6e
A
2816
2817void
2818tcp_out6_cksum_stats(u_int32_t len)
2819{
2820 tcpstat.tcps_snd6_swcsum++;
2821 tcpstat.tcps_snd6_swcsum_bytes += len;
2822}
2823
5ba3f43e 2824/*
39236c6e
A
2825 * When messages are enabled on a TCP socket, the message priority
2826 * is sent as a control message. This function will extract it.
2827 */
2828int
5ba3f43e 2829tcp_get_msg_priority(struct mbuf *control, uint32_t *msgpri)
39236c6e
A
2830{
2831 struct cmsghdr *cm;
2832 if (control == NULL)
2833 return(EINVAL);
2834
2835 for (cm = M_FIRST_CMSGHDR(control); cm;
2836 cm = M_NXT_CMSGHDR(control, cm)) {
2837 if (cm->cmsg_len < sizeof(struct cmsghdr) ||
2838 cm->cmsg_len > control->m_len) {
2839 return (EINVAL);
2840 }
2841 if (cm->cmsg_level == SOL_SOCKET &&
2842 cm->cmsg_type == SCM_MSG_PRIORITY) {
2843 *msgpri = *(unsigned int *)(void *)CMSG_DATA(cm);
2844 break;
2845 }
2846 }
2847
5ba3f43e 2848 VERIFY(*msgpri >= MSG_PRI_MIN && *msgpri <= MSG_PRI_MAX);
39236c6e
A
2849 return (0);
2850}
2851#endif /* INET6 */