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