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