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