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