xref: /xnu-11215.61.5/bsd/netinet/mptcp_usrreq.c (revision 4f1223e81cd707a65cc109d0b8ad6653699da3c4)
1 /*
2  * Copyright (c) 2012-2020 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 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/socket.h>
33 #include <sys/socketvar.h>
34 #include <sys/protosw.h>
35 #include <sys/mcache.h>
36 #include <sys/syslog.h>
37 #include <sys/proc.h>
38 #include <sys/proc_internal.h>
39 #include <sys/resourcevar.h>
40 #include <sys/kauth.h>
41 #include <sys/priv.h>
42 
43 #include <net/if.h>
44 #include <netinet/in.h>
45 #include <netinet/in_var.h>
46 #include <netinet/tcp.h>
47 #include <netinet/tcp_fsm.h>
48 #include <netinet/tcp_seq.h>
49 #include <netinet/tcp_var.h>
50 #include <netinet/tcp_timer.h>
51 #include <netinet/mptcp.h>
52 #include <netinet/mptcp_var.h>
53 #include <netinet/mptcp_timer.h>
54 
55 #include <mach/sdt.h>
56 #include <net/sockaddr_utils.h>
57 
58 static int mptcp_usr_attach(struct socket *, int, struct proc *);
59 static int mptcp_usr_detach(struct socket *);
60 static int mptcp_attach(struct socket *, struct proc *);
61 static int mptcp_usr_connectx(struct socket *, struct sockaddr *,
62     struct sockaddr *, struct proc *, uint32_t, sae_associd_t,
63     sae_connid_t *, uint32_t, void *, uint32_t, struct uio *, user_ssize_t *);
64 static int mptcp_getassocids(struct mptses *, uint32_t *, user_addr_t);
65 static int mptcp_getconnids(struct mptses *, sae_associd_t, uint32_t *,
66     user_addr_t);
67 static int mptcp_getconninfo(struct mptses *, sae_connid_t *, uint32_t *,
68     uint32_t *, int32_t *, user_addr_t, socklen_t *, user_addr_t, socklen_t *,
69     uint32_t *, user_addr_t, uint32_t *);
70 static int mptcp_usr_control(struct socket *, u_long cmd,
71     caddr_t __sized_by(IOCPARM_LEN(cmd)), struct ifnet *,
72     struct proc *);
73 static int mptcp_disconnect(struct mptses *);
74 static int mptcp_usr_disconnect(struct socket *);
75 static int mptcp_usr_disconnectx(struct socket *, sae_associd_t, sae_connid_t);
76 static struct mptses *mptcp_usrclosed(struct mptses *);
77 static int mptcp_usr_rcvd(struct socket *, int);
78 static int mptcp_usr_send(struct socket *, int, struct mbuf *,
79     struct sockaddr *, struct mbuf *, struct proc *);
80 static int mptcp_usr_shutdown(struct socket *);
81 static int mptcp_usr_sosend(struct socket *, struct sockaddr *, struct uio *,
82     struct mbuf *, struct mbuf *, int);
83 static int mptcp_usr_socheckopt(struct socket *, struct sockopt *);
84 static int mptcp_usr_preconnect(struct socket *so);
85 
86 struct pr_usrreqs mptcp_usrreqs = {
87 	.pru_attach =           mptcp_usr_attach,
88 	.pru_connectx =         mptcp_usr_connectx,
89 	.pru_control =          mptcp_usr_control,
90 	.pru_detach =           mptcp_usr_detach,
91 	.pru_disconnect =       mptcp_usr_disconnect,
92 	.pru_disconnectx =      mptcp_usr_disconnectx,
93 	.pru_peeraddr =         mp_getpeeraddr,
94 	.pru_rcvd =             mptcp_usr_rcvd,
95 	.pru_send =             mptcp_usr_send,
96 	.pru_shutdown =         mptcp_usr_shutdown,
97 	.pru_sockaddr =         mp_getsockaddr,
98 	.pru_sosend =           mptcp_usr_sosend,
99 	.pru_soreceive =        soreceive,
100 	.pru_socheckopt =       mptcp_usr_socheckopt,
101 	.pru_preconnect =       mptcp_usr_preconnect,
102 };
103 
104 
105 int mptcp_developer_mode = 0;
106 SYSCTL_INT(_net_inet_mptcp, OID_AUTO, allow_aggregate, CTLFLAG_RW | CTLFLAG_LOCKED,
107     &mptcp_developer_mode, 0, "Allow the Multipath aggregation mode");
108 
109 int mptcp_no_first_party = 0;
110 SYSCTL_INT(_net_inet_mptcp, OID_AUTO, no_first_party, CTLFLAG_RW | CTLFLAG_LOCKED,
111     &mptcp_no_first_party, 0, "Do not do first-party app exemptions");
112 
113 static unsigned long mptcp_expected_progress_headstart = 5000;
114 SYSCTL_ULONG(_net_inet_mptcp, OID_AUTO, expected_progress_headstart, CTLFLAG_RW | CTLFLAG_LOCKED,
115     &mptcp_expected_progress_headstart, "Headstart to give MPTCP before meeting the progress deadline");
116 
117 
118 /*
119  * Attaches an MPTCP control block to a socket.
120  */
121 static int
mptcp_usr_attach(struct socket * mp_so,int proto,struct proc * p)122 mptcp_usr_attach(struct socket *mp_so, int proto, struct proc *p)
123 {
124 #pragma unused(proto)
125 	int error;
126 
127 	VERIFY(mpsotomppcb(mp_so) == NULL);
128 
129 	error = mptcp_attach(mp_so, p);
130 	if (error) {
131 		goto out;
132 	}
133 
134 	if ((mp_so->so_options & SO_LINGER) && mp_so->so_linger == 0) {
135 		mp_so->so_linger = (short)(TCP_LINGERTIME * hz);
136 	}
137 out:
138 	return error;
139 }
140 
141 /*
142  * Detaches an MPTCP control block from a socket.
143  */
144 static int
mptcp_usr_detach(struct socket * mp_so)145 mptcp_usr_detach(struct socket *mp_so)
146 {
147 	struct mptses *__single mpte = mpsotompte(mp_so);
148 	struct mppcb *__single mpp = mpsotomppcb(mp_so);
149 
150 	if (mpp == NULL || mpp->mpp_state == MPPCB_STATE_DEAD) {
151 		os_log_error(mptcp_log_handle, "%s - %lx: state: %d\n",
152 		    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte),
153 		    mpp ? mpp->mpp_state : -1);
154 		return EINVAL;
155 	}
156 
157 	/*
158 	 * We are done with this MPTCP socket (it has been closed);
159 	 * trigger all subflows to be disconnected, if not already,
160 	 * by initiating the PCB detach sequence (SOF_PCBCLEARING
161 	 * will be set.)
162 	 */
163 	mp_pcbdetach(mp_so);
164 
165 	mptcp_disconnect(mpte);
166 
167 	return 0;
168 }
169 
170 /*
171  * Attach MPTCP protocol to socket, allocating MP control block,
172  * MPTCP session, control block, buffer space, etc.
173  */
174 static int
mptcp_attach(struct socket * mp_so,struct proc * p)175 mptcp_attach(struct socket *mp_so, struct proc *p)
176 {
177 #pragma unused(p)
178 	struct mptses *__single mpte = NULL;
179 	struct mptcb *__single mp_tp = NULL;
180 	struct mppcb *__single mpp = NULL;
181 	int error = 0;
182 
183 	if (mp_so->so_snd.sb_hiwat == 0 || mp_so->so_rcv.sb_hiwat == 0) {
184 		error = soreserve(mp_so, tcp_sendspace, tcp_recvspace);
185 		if (error != 0) {
186 			goto out;
187 		}
188 	}
189 
190 	if (mp_so->so_snd.sb_preconn_hiwat == 0) {
191 		soreserve_preconnect(mp_so, 2048);
192 	}
193 
194 	if ((mp_so->so_rcv.sb_flags & SB_USRSIZE) == 0) {
195 		mp_so->so_rcv.sb_flags |= SB_AUTOSIZE;
196 	}
197 	if ((mp_so->so_snd.sb_flags & SB_USRSIZE) == 0) {
198 		mp_so->so_snd.sb_flags |= SB_AUTOSIZE;
199 	}
200 
201 	/*
202 	 * MPTCP send-socket buffers cannot be compressed, due to the
203 	 * fact that each mbuf chained via m_next is a M_PKTHDR
204 	 * which carries some MPTCP metadata.
205 	 */
206 	mp_so->so_snd.sb_flags |= SB_NOCOMPRESS;
207 
208 	if ((error = mp_pcballoc(mp_so, &mtcbinfo)) != 0) {
209 		goto out;
210 	}
211 
212 	mpp = mpsotomppcb(mp_so);
213 	mpte = (struct mptses *)mpp->mpp_pcbe;
214 	mp_tp = mpte->mpte_mptcb;
215 
216 	VERIFY(mp_tp != NULL);
217 out:
218 	return error;
219 }
220 
221 static int
mptcp_entitlement_check(struct socket * mp_so,uint8_t svctype)222 mptcp_entitlement_check(struct socket *mp_so, uint8_t svctype)
223 {
224 	struct mptses *mpte = mpsotompte(mp_so);
225 
226 	if (mptcp_no_first_party) {
227 		return 0;
228 	}
229 
230 	/* First, check for mptcp_extended without delegation */
231 	if (soopt_cred_check(mp_so, PRIV_NET_RESTRICTED_MULTIPATH_EXTENDED, TRUE, FALSE) == 0) {
232 		/*
233 		 * This means the app has the extended entitlement. Thus,
234 		 * it's a first party app and can run without restrictions.
235 		 */
236 		mpte->mpte_flags |= MPTE_FIRSTPARTY;
237 		return 0;
238 	}
239 
240 	/* Now with delegation */
241 	if (mp_so->so_flags & SOF_DELEGATED &&
242 	    soopt_cred_check(mp_so, PRIV_NET_RESTRICTED_MULTIPATH_EXTENDED, TRUE, TRUE) == 0) {
243 		/*
244 		 * This means the app has the extended entitlement. Thus,
245 		 * it's a first party app and can run without restrictions.
246 		 */
247 		mpte->mpte_flags |= MPTE_FIRSTPARTY;
248 		return 0;
249 	}
250 
251 	if (svctype == MPTCP_SVCTYPE_AGGREGATE) {
252 		if (mptcp_developer_mode) {
253 			return 0;
254 		}
255 
256 		os_log_error(mptcp_log_handle, "%s - %lx: MPTCP prohibited on svc %u\n",
257 		    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), mpte->mpte_svctype);
258 		return -1;
259 	}
260 
261 	return 0;
262 }
263 
264 /*
265  * Common subroutine to open a MPTCP connection to one of the remote hosts
266  * specified by dst_sl.  This includes allocating and establishing a
267  * subflow TCP connection, either initially to establish MPTCP connection,
268  * or to join an existing one.  Returns a connection handle upon success.
269  */
270 static int
mptcp_connectx(struct mptses * mpte,struct sockaddr * src,struct sockaddr * dst,uint32_t ifscope,sae_connid_t * pcid)271 mptcp_connectx(struct mptses *mpte, struct sockaddr *src,
272     struct sockaddr *dst, uint32_t ifscope, sae_connid_t *pcid)
273 {
274 	int error = 0;
275 
276 	VERIFY(dst != NULL);
277 	VERIFY(pcid != NULL);
278 
279 	error = mptcp_subflow_add(mpte, src, dst, ifscope, pcid);
280 
281 	return error;
282 }
283 
284 /*
285  * User-protocol pru_connectx callback.
286  */
287 static int
mptcp_usr_connectx(struct socket * mp_so,struct sockaddr * src,struct sockaddr * dst,struct proc * p,uint32_t ifscope,sae_associd_t aid,sae_connid_t * pcid,uint32_t flags,void * arg,uint32_t arglen,struct uio * auio,user_ssize_t * bytes_written)288 mptcp_usr_connectx(struct socket *mp_so, struct sockaddr *src,
289     struct sockaddr *dst, struct proc *p, uint32_t ifscope,
290     sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg,
291     uint32_t arglen, struct uio *auio, user_ssize_t *bytes_written)
292 {
293 #pragma unused(p, aid, flags, arg, arglen)
294 	struct mppcb *mpp = mpsotomppcb(mp_so);
295 	struct mptses *mpte = NULL;
296 	struct mptcb *mp_tp = NULL;
297 	user_ssize_t    datalen;
298 	int error = 0;
299 
300 	if (mpp == NULL || mpp->mpp_state == MPPCB_STATE_DEAD) {
301 		os_log_error(mptcp_log_handle, "%s - %lx: state %d\n",
302 		    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte),
303 		    mpp ? mpp->mpp_state : -1);
304 		error = EINVAL;
305 		goto out;
306 	}
307 	mpte = mptompte(mpp);
308 	mp_tp = mpte->mpte_mptcb;
309 
310 	if (mp_tp->mpt_flags &  MPTCPF_FALLBACK_TO_TCP) {
311 		os_log_error(mptcp_log_handle, "%s - %lx: fell back to TCP\n",
312 		    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte));
313 		error = EINVAL;
314 		goto out;
315 	}
316 
317 	if (dst->sa_family != AF_INET && dst->sa_family != AF_INET6) {
318 		error = EAFNOSUPPORT;
319 		goto out;
320 	}
321 
322 	if (dst->sa_family == AF_INET &&
323 	    dst->sa_len != sizeof(mpte->__mpte_dst_v4)) {
324 		os_log_error(mptcp_log_handle, "%s - %lx: IPv4 dst len %u\n",
325 		    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), dst->sa_len);
326 		error = EINVAL;
327 		goto out;
328 	}
329 
330 	if (dst->sa_family == AF_INET6 &&
331 	    dst->sa_len != sizeof(mpte->__mpte_dst_v6)) {
332 		os_log_error(mptcp_log_handle, "%s - %lx: IPv6 dst len %u\n",
333 		    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), dst->sa_len);
334 		error = EINVAL;
335 		goto out;
336 	}
337 
338 	if (!(mpte->mpte_flags & MPTE_SVCTYPE_CHECKED)) {
339 		if (mptcp_entitlement_check(mp_so, mpte->mpte_svctype) < 0) {
340 			error = EPERM;
341 			goto out;
342 		}
343 
344 		mpte->mpte_flags |= MPTE_SVCTYPE_CHECKED;
345 	}
346 
347 	if ((mp_so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING)) == 0) {
348 		SOCKADDR_COPY(dst, &mpte->mpte_dst, dst->sa_len);
349 
350 		if (dst->sa_family == AF_INET) {
351 			SOCKADDR_COPY(dst, &mpte->mpte_sub_dst_v4, dst->sa_len);
352 		} else {
353 			SOCKADDR_COPY(dst, &mpte->mpte_sub_dst_v6, dst->sa_len);
354 		}
355 	}
356 
357 	if (src) {
358 		if (src->sa_family != AF_INET && src->sa_family != AF_INET6) {
359 			error = EAFNOSUPPORT;
360 			goto out;
361 		}
362 
363 		if (src->sa_family == AF_INET &&
364 		    src->sa_len != sizeof(mpte->__mpte_src_v4)) {
365 			os_log_error(mptcp_log_handle, "%s - %lx: IPv4 src len %u\n",
366 			    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), src->sa_len);
367 			error = EINVAL;
368 			goto out;
369 		}
370 
371 		if (src->sa_family == AF_INET6 &&
372 		    src->sa_len != sizeof(mpte->__mpte_src_v6)) {
373 			os_log_error(mptcp_log_handle, "%s - %lx: IPv6 src len %u\n",
374 			    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), src->sa_len);
375 			error = EINVAL;
376 			goto out;
377 		}
378 
379 		if ((mp_so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING)) == 0) {
380 			SOCKADDR_COPY(src, &mpte->mpte_src, src->sa_len);
381 		}
382 	}
383 
384 	error = mptcp_connectx(mpte, src, dst, ifscope, pcid);
385 
386 	/* If there is data, copy it */
387 	if (auio != NULL) {
388 		datalen = uio_resid(auio);
389 		socket_unlock(mp_so, 0);
390 		error = mp_so->so_proto->pr_usrreqs->pru_sosend(mp_so, NULL,
391 		    (uio_t) auio, NULL, NULL, 0);
392 
393 		if (error == 0 || error == EWOULDBLOCK) {
394 			*bytes_written = datalen - uio_resid(auio);
395 		}
396 
397 		if (error == EWOULDBLOCK) {
398 			error = EINPROGRESS;
399 		}
400 
401 		socket_lock(mp_so, 0);
402 	}
403 
404 out:
405 	return error;
406 }
407 
408 /*
409  * Handle SIOCGASSOCIDS ioctl for PF_MULTIPATH domain.
410  */
411 static int
mptcp_getassocids(struct mptses * mpte,uint32_t * cnt,user_addr_t aidp)412 mptcp_getassocids(struct mptses *mpte, uint32_t *cnt, user_addr_t aidp)
413 {
414 	/* MPTCP has at most 1 association */
415 	*cnt = (mpte->mpte_associd != SAE_ASSOCID_ANY) ? 1 : 0;
416 
417 	/* just asking how many there are? */
418 	if (aidp == USER_ADDR_NULL) {
419 		return 0;
420 	}
421 
422 	return copyout(&mpte->mpte_associd, aidp,
423 	           sizeof(mpte->mpte_associd));
424 }
425 
426 /*
427  * Handle SIOCGCONNIDS ioctl for PF_MULTIPATH domain.
428  */
429 static int
mptcp_getconnids(struct mptses * mpte,sae_associd_t aid,uint32_t * cnt,user_addr_t cidp)430 mptcp_getconnids(struct mptses *mpte, sae_associd_t aid, uint32_t *cnt,
431     user_addr_t cidp)
432 {
433 	struct mptsub *mpts;
434 	int error = 0;
435 
436 	if (aid != SAE_ASSOCID_ANY && aid != SAE_ASSOCID_ALL &&
437 	    aid != mpte->mpte_associd) {
438 		return EINVAL;
439 	}
440 
441 	*cnt = mpte->mpte_numflows;
442 
443 	/* just asking how many there are? */
444 	if (cidp == USER_ADDR_NULL) {
445 		return 0;
446 	}
447 
448 	TAILQ_FOREACH(mpts, &mpte->mpte_subflows, mpts_entry) {
449 		if ((error = copyout(&mpts->mpts_connid, cidp,
450 		    sizeof(mpts->mpts_connid))) != 0) {
451 			break;
452 		}
453 
454 		cidp += sizeof(mpts->mpts_connid);
455 	}
456 
457 	return error;
458 }
459 
460 /*
461  * Handle SIOCGCONNINFO ioctl for PF_MULTIPATH domain.
462  */
463 static int
mptcp_getconninfo(struct mptses * mpte,sae_connid_t * cid,uint32_t * flags,uint32_t * ifindex,int32_t * soerror,user_addr_t src,socklen_t * src_len,user_addr_t dst,socklen_t * dst_len,uint32_t * aux_type,user_addr_t aux_data,uint32_t * aux_len)464 mptcp_getconninfo(struct mptses *mpte, sae_connid_t *cid, uint32_t *flags,
465     uint32_t *ifindex, int32_t *soerror, user_addr_t src, socklen_t *src_len,
466     user_addr_t dst, socklen_t *dst_len, uint32_t *aux_type,
467     user_addr_t aux_data, uint32_t *aux_len)
468 {
469 	*flags = 0;
470 	*aux_type = 0;
471 	*ifindex = 0;
472 	*soerror = 0;
473 	struct mptcb *mp_tp = mpte->mpte_mptcb;
474 
475 	/* MPTCP-level global stats */
476 	if (*cid == SAE_CONNID_ALL) {
477 		struct socket *mp_so = mptetoso(mpte);
478 		struct conninfo_multipathtcp mptcp_ci;
479 		int error = 0;
480 
481 		if (*aux_len != 0 && *aux_len != sizeof(mptcp_ci)) {
482 			return EINVAL;
483 		}
484 
485 		if (mp_so->so_state & SS_ISCONNECTING) {
486 			*flags |= CIF_CONNECTING;
487 		}
488 		if (mp_so->so_state & SS_ISCONNECTED) {
489 			*flags |= CIF_CONNECTED;
490 		}
491 		if (mp_so->so_state & SS_ISDISCONNECTING) {
492 			*flags |= CIF_DISCONNECTING;
493 		}
494 		if (mp_so->so_state & SS_ISDISCONNECTED) {
495 			*flags |= CIF_DISCONNECTED;
496 		}
497 		if (!(mp_tp->mpt_flags & MPTCPF_FALLBACK_TO_TCP)) {
498 			*flags |= CIF_MP_CAPABLE;
499 		}
500 		if (mp_tp->mpt_flags & MPTCPF_FALLBACK_TO_TCP) {
501 			*flags |= CIF_MP_DEGRADED;
502 		}
503 		if (mp_tp->mpt_version == MPTCP_VERSION_1) {
504 			*flags |= CIF_MP_V1;
505 		}
506 
507 		*src_len = 0;
508 		*dst_len = 0;
509 
510 		*aux_type = CIAUX_MPTCP;
511 		*aux_len = sizeof(mptcp_ci);
512 
513 		if (aux_data != USER_ADDR_NULL) {
514 			const struct mptsub *mpts;
515 			int initial_info_set = 0;
516 			unsigned long i = 0;
517 
518 			bzero(&mptcp_ci, sizeof(mptcp_ci));
519 			mptcp_ci.mptcpci_subflow_count = mpte->mpte_numflows;
520 			mptcp_ci.mptcpci_switch_count = mpte->mpte_subflow_switches;
521 
522 			VERIFY(sizeof(mptcp_ci.mptcpci_itfstats) == sizeof(mpte->mpte_itfstats));
523 			memcpy(mptcp_ci.mptcpci_itfstats, mpte->mpte_itfstats, sizeof(mptcp_ci.mptcpci_itfstats));
524 
525 			TAILQ_FOREACH(mpts, &mpte->mpte_subflows, mpts_entry) {
526 				if (i >= sizeof(mptcp_ci.mptcpci_subflow_connids) / sizeof(sae_connid_t)) {
527 					break;
528 				}
529 				mptcp_ci.mptcpci_subflow_connids[i] = mpts->mpts_connid;
530 
531 				if (mpts->mpts_flags & MPTSF_INITIAL_SUB) {
532 					const struct inpcb *inp;
533 
534 					inp = sotoinpcb(mpts->mpts_socket);
535 
536 					mptcp_ci.mptcpci_init_rxbytes = inp->inp_stat->rxbytes;
537 					mptcp_ci.mptcpci_init_txbytes = inp->inp_stat->txbytes;
538 					initial_info_set = 1;
539 				}
540 
541 				mptcpstats_update(mptcp_ci.mptcpci_itfstats, mpts);
542 
543 				i++;
544 			}
545 
546 			if (initial_info_set == 0) {
547 				mptcp_ci.mptcpci_init_rxbytes = mpte->mpte_init_rxbytes;
548 				mptcp_ci.mptcpci_init_txbytes = mpte->mpte_init_txbytes;
549 			}
550 
551 			if (mpte->mpte_flags & MPTE_FIRSTPARTY) {
552 				mptcp_ci.mptcpci_flags |= MPTCPCI_FIRSTPARTY;
553 			}
554 
555 			error = copyout(&mptcp_ci, aux_data, sizeof(mptcp_ci));
556 			if (error != 0) {
557 				os_log_error(mptcp_log_handle, "%s - %lx: copyout failed: %d\n",
558 				    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), error);
559 				return error;
560 			}
561 		}
562 
563 		return 0;
564 	}
565 
566 	/* Any stats of any subflow */
567 	if (*cid == SAE_CONNID_ANY) {
568 		const struct mptsub *mpts;
569 		struct socket *so;
570 		const struct inpcb *inp;
571 		int error = 0;
572 
573 		mpts = TAILQ_FIRST(&mpte->mpte_subflows);
574 		if (mpts == NULL) {
575 			return ENXIO;
576 		}
577 
578 		so = mpts->mpts_socket;
579 		inp = sotoinpcb(so);
580 
581 		if (inp->inp_vflag & INP_IPV4) {
582 			error = in_getconninfo(so, SAE_CONNID_ANY, flags, ifindex,
583 			    soerror, src, src_len, dst, dst_len,
584 			    aux_type, aux_data, aux_len);
585 		} else {
586 			error = in6_getconninfo(so, SAE_CONNID_ANY, flags, ifindex,
587 			    soerror, src, src_len, dst, dst_len,
588 			    aux_type, aux_data, aux_len);
589 		}
590 
591 		if (error != 0) {
592 			os_log_error(mptcp_log_handle, "%s - %lx:error from in_getconninfo %d\n",
593 			    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), error);
594 			return error;
595 		}
596 
597 		if (mpts->mpts_flags & MPTSF_MP_CAPABLE) {
598 			*flags |= CIF_MP_CAPABLE;
599 		}
600 		if (mpts->mpts_flags & MPTSF_MP_DEGRADED) {
601 			*flags |= CIF_MP_DEGRADED;
602 		}
603 		if (mpts->mpts_flags & MPTSF_MP_READY) {
604 			*flags |= CIF_MP_READY;
605 		}
606 		if (mpts->mpts_flags & MPTSF_ACTIVE) {
607 			*flags |= CIF_MP_ACTIVE;
608 		}
609 		if (mp_tp->mpt_version == MPTCP_VERSION_1) {
610 			*flags |= CIF_MP_V1;
611 		}
612 
613 		return 0;
614 	} else {
615 		/* Per-interface stats */
616 		const struct mptsub *mpts, *orig_mpts = NULL;
617 		struct conninfo_tcp tcp_ci;
618 		const struct inpcb *inp;
619 		struct socket *so;
620 		int error = 0;
621 		int index;
622 
623 		/* cid is thus an ifindex - range-check first! */
624 		if (*cid > USHRT_MAX) {
625 			return EINVAL;
626 		}
627 
628 		bzero(&tcp_ci, sizeof(tcp_ci));
629 
630 		/* First, get a subflow to fill in the "regular" info. */
631 		TAILQ_FOREACH(mpts, &mpte->mpte_subflows, mpts_entry) {
632 			const struct ifnet *ifp = sotoinpcb(mpts->mpts_socket)->inp_last_outifp;
633 
634 			if (ifp && ifp->if_index == *cid) {
635 				break;
636 			}
637 		}
638 
639 		if (mpts == NULL) {
640 			/* No subflow there - well, let's just get the basic itf-info */
641 			goto interface_info;
642 		}
643 
644 		so = mpts->mpts_socket;
645 		inp = sotoinpcb(so);
646 
647 		/* Give it USER_ADDR_NULL, because we are doing this on our own */
648 		if (inp->inp_vflag & INP_IPV4) {
649 			error = in_getconninfo(so, SAE_CONNID_ANY, flags, ifindex,
650 			    soerror, src, src_len, dst, dst_len,
651 			    aux_type, USER_ADDR_NULL, aux_len);
652 		} else {
653 			error = in6_getconninfo(so, SAE_CONNID_ANY, flags, ifindex,
654 			    soerror, src, src_len, dst, dst_len,
655 			    aux_type, USER_ADDR_NULL, aux_len);
656 		}
657 
658 		if (error != 0) {
659 			os_log_error(mptcp_log_handle, "%s - %lx:error from in_getconninfo %d\n",
660 			    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), error);
661 			return error;
662 		}
663 
664 		/* ToDo: Nobody is reading these flags on subflows. Why bother ? */
665 		if (mpts->mpts_flags & MPTSF_MP_CAPABLE) {
666 			*flags |= CIF_MP_CAPABLE;
667 		}
668 		if (mpts->mpts_flags & MPTSF_MP_DEGRADED) {
669 			*flags |= CIF_MP_DEGRADED;
670 		}
671 		if (mpts->mpts_flags & MPTSF_MP_READY) {
672 			*flags |= CIF_MP_READY;
673 		}
674 		if (mpts->mpts_flags & MPTSF_ACTIVE) {
675 			*flags |= CIF_MP_ACTIVE;
676 		}
677 		if (mp_tp->mpt_version == MPTCP_VERSION_1) {
678 			*flags |= CIF_MP_V1;
679 		}
680 
681 		/*
682 		 * Now, we gather the metrics (aka., tcp_info) and roll them in
683 		 * across all subflows of this interface to build an aggregated
684 		 * view.
685 		 *
686 		 * We take the TCP_INFO from the first subflow as the "master",
687 		 * feeding into those fields that we do not roll.
688 		 */
689 		if (aux_data != USER_ADDR_NULL) {
690 			tcp_getconninfo(so, &tcp_ci);
691 
692 			orig_mpts = mpts;
693 			TAILQ_FOREACH(mpts, &mpte->mpte_subflows, mpts_entry) {
694 				const struct inpcb *mptsinp = sotoinpcb(mpts->mpts_socket);
695 				const struct ifnet *ifp;
696 
697 				ifp = mptsinp->inp_last_outifp;
698 
699 				if (ifp == NULL || ifp->if_index != *cid || mpts == orig_mpts) {
700 					continue;
701 				}
702 
703 				/* Roll the itf-stats into the tcp_info */
704 				tcp_ci.tcpci_tcp_info.tcpi_txbytes +=
705 				    mptsinp->inp_stat->txbytes;
706 				tcp_ci.tcpci_tcp_info.tcpi_rxbytes +=
707 				    mptsinp->inp_stat->rxbytes;
708 
709 				tcp_ci.tcpci_tcp_info.tcpi_wifi_txbytes +=
710 				    mptsinp->inp_wstat->txbytes;
711 				tcp_ci.tcpci_tcp_info.tcpi_wifi_rxbytes +=
712 				    mptsinp->inp_wstat->rxbytes;
713 
714 				tcp_ci.tcpci_tcp_info.tcpi_wired_txbytes +=
715 				    mptsinp->inp_Wstat->txbytes;
716 				tcp_ci.tcpci_tcp_info.tcpi_wired_rxbytes +=
717 				    mptsinp->inp_Wstat->rxbytes;
718 
719 				tcp_ci.tcpci_tcp_info.tcpi_cell_txbytes +=
720 				    mptsinp->inp_cstat->txbytes;
721 				tcp_ci.tcpci_tcp_info.tcpi_cell_rxbytes +=
722 				    mptsinp->inp_cstat->rxbytes;
723 			}
724 		}
725 
726 interface_info:
727 		*aux_type = CIAUX_TCP;
728 		if (*aux_len == 0) {
729 			*aux_len = sizeof(tcp_ci);
730 		} else if (aux_data != USER_ADDR_NULL) {
731 			boolean_t create;
732 
733 			/*
734 			 * Finally, old subflows might have been closed - we
735 			 * want this data as well, so grab it from the interface
736 			 * stats.
737 			 */
738 			create = orig_mpts != NULL;
739 
740 			/*
741 			 * When we found a subflow, we are willing to create a stats-index
742 			 * because we have some data to return. If there isn't a subflow,
743 			 * nor anything in the stats, return EINVAL. Because the
744 			 * ifindex belongs to something that doesn't exist.
745 			 */
746 			index = mptcpstats_get_index_by_ifindex(mpte->mpte_itfstats, (u_short)(*cid), false);
747 			if (index == -1) {
748 				os_log_error(mptcp_log_handle,
749 				    "%s - %lx: Asking for too many ifindex: %u subcount %u, mpts? %s\n",
750 				    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte),
751 				    *cid, mpte->mpte_numflows,
752 				    orig_mpts ? "yes" : "no");
753 
754 				if (orig_mpts == NULL) {
755 					return EINVAL;
756 				}
757 			} else {
758 				struct mptcp_itf_stats *stats;
759 
760 				stats = &mpte->mpte_itfstats[index];
761 
762 				/* Roll the itf-stats into the tcp_info */
763 				tcp_ci.tcpci_tcp_info.tcpi_last_outif = *cid;
764 				tcp_ci.tcpci_tcp_info.tcpi_txbytes +=
765 				    stats->mpis_txbytes;
766 				tcp_ci.tcpci_tcp_info.tcpi_rxbytes +=
767 				    stats->mpis_rxbytes;
768 
769 				tcp_ci.tcpci_tcp_info.tcpi_wifi_txbytes +=
770 				    stats->mpis_wifi_txbytes;
771 				tcp_ci.tcpci_tcp_info.tcpi_wifi_rxbytes +=
772 				    stats->mpis_wifi_rxbytes;
773 
774 				tcp_ci.tcpci_tcp_info.tcpi_wired_txbytes +=
775 				    stats->mpis_wired_txbytes;
776 				tcp_ci.tcpci_tcp_info.tcpi_wired_rxbytes +=
777 				    stats->mpis_wired_rxbytes;
778 
779 				tcp_ci.tcpci_tcp_info.tcpi_cell_txbytes +=
780 				    stats->mpis_cell_txbytes;
781 				tcp_ci.tcpci_tcp_info.tcpi_cell_rxbytes +=
782 				    stats->mpis_cell_rxbytes;
783 			}
784 
785 			*aux_len = min(*aux_len, sizeof(tcp_ci));
786 			error = copyout(&tcp_ci, aux_data, *aux_len);
787 			if (error != 0) {
788 				return error;
789 			}
790 		}
791 	}
792 
793 	return 0;
794 }
795 
796 /*
797  * User-protocol pru_control callback.
798  */
799 static int
mptcp_usr_control(struct socket * mp_so,u_long cmd,caddr_t __sized_by (IOCPARM_LEN (cmd))data,struct ifnet * ifp,struct proc * p)800 mptcp_usr_control(struct socket *mp_so, u_long cmd,
801     caddr_t __sized_by(IOCPARM_LEN(cmd)) data,
802     struct ifnet *ifp, struct proc *p)
803 {
804 #pragma unused(ifp, p)
805 	struct mppcb *mpp = mpsotomppcb(mp_so);
806 	struct mptses *mpte;
807 	int error = 0;
808 
809 	if (mpp == NULL || mpp->mpp_state == MPPCB_STATE_DEAD) {
810 		error = EINVAL;
811 		goto out;
812 	}
813 	mpte = mptompte(mpp);
814 
815 	switch (cmd) {
816 	case SIOCGASSOCIDS32: {         /* struct so_aidreq32 */
817 		struct so_aidreq32 aidr;
818 		bcopy(data, &aidr, sizeof(aidr));
819 		error = mptcp_getassocids(mpte, &aidr.sar_cnt,
820 		    aidr.sar_aidp);
821 		if (error == 0) {
822 			bcopy(&aidr, data, sizeof(aidr));
823 		}
824 		break;
825 	}
826 
827 	case SIOCGASSOCIDS64: {         /* struct so_aidreq64 */
828 		struct so_aidreq64 aidr;
829 		bcopy(data, &aidr, sizeof(aidr));
830 		error = mptcp_getassocids(mpte, &aidr.sar_cnt,
831 		    (user_addr_t)aidr.sar_aidp);
832 		if (error == 0) {
833 			bcopy(&aidr, data, sizeof(aidr));
834 		}
835 		break;
836 	}
837 
838 	case SIOCGCONNIDS32: {          /* struct so_cidreq32 */
839 		struct so_cidreq32 cidr;
840 		bcopy(data, &cidr, sizeof(cidr));
841 		error = mptcp_getconnids(mpte, cidr.scr_aid, &cidr.scr_cnt,
842 		    cidr.scr_cidp);
843 		if (error == 0) {
844 			bcopy(&cidr, data, sizeof(cidr));
845 		}
846 		break;
847 	}
848 
849 	case SIOCGCONNIDS64: {          /* struct so_cidreq64 */
850 		struct so_cidreq64 cidr;
851 		bcopy(data, &cidr, sizeof(cidr));
852 		error = mptcp_getconnids(mpte, cidr.scr_aid, &cidr.scr_cnt,
853 		    (user_addr_t)cidr.scr_cidp);
854 		if (error == 0) {
855 			bcopy(&cidr, data, sizeof(cidr));
856 		}
857 		break;
858 	}
859 
860 	case SIOCGCONNINFO32: {         /* struct so_cinforeq32 */
861 		struct so_cinforeq32 cifr;
862 		bcopy(data, &cifr, sizeof(cifr));
863 		error = mptcp_getconninfo(mpte, &cifr.scir_cid,
864 		    &cifr.scir_flags, &cifr.scir_ifindex, &cifr.scir_error,
865 		    cifr.scir_src, &cifr.scir_src_len, cifr.scir_dst,
866 		    &cifr.scir_dst_len, &cifr.scir_aux_type, cifr.scir_aux_data,
867 		    &cifr.scir_aux_len);
868 		if (error == 0) {
869 			bcopy(&cifr, data, sizeof(cifr));
870 		}
871 		break;
872 	}
873 
874 	case SIOCGCONNINFO64: {         /* struct so_cinforeq64 */
875 		struct so_cinforeq64 cifr;
876 		bcopy(data, &cifr, sizeof(cifr));
877 		error = mptcp_getconninfo(mpte, &cifr.scir_cid,
878 		    &cifr.scir_flags, &cifr.scir_ifindex, &cifr.scir_error,
879 		    (user_addr_t)cifr.scir_src, &cifr.scir_src_len,
880 		    (user_addr_t)cifr.scir_dst, &cifr.scir_dst_len,
881 		    &cifr.scir_aux_type, (user_addr_t)cifr.scir_aux_data,
882 		    &cifr.scir_aux_len);
883 		if (error == 0) {
884 			bcopy(&cifr, data, sizeof(cifr));
885 		}
886 		break;
887 	}
888 
889 	default:
890 		error = EOPNOTSUPP;
891 		break;
892 	}
893 out:
894 	return error;
895 }
896 
897 static int
mptcp_disconnect(struct mptses * mpte)898 mptcp_disconnect(struct mptses *mpte)
899 {
900 	struct socket *mp_so;
901 	struct mptcb *mp_tp;
902 	int error = 0;
903 
904 	mp_so = mptetoso(mpte);
905 	mp_tp = mpte->mpte_mptcb;
906 
907 	/* if we're not detached, go thru socket state checks */
908 	if (!(mp_so->so_flags & SOF_PCBCLEARING) && !(mp_so->so_flags & SOF_DEFUNCT)) {
909 		if (!(mp_so->so_state & (SS_ISCONNECTED |
910 		    SS_ISCONNECTING))) {
911 			error = ENOTCONN;
912 			goto out;
913 		}
914 		if (mp_so->so_state & SS_ISDISCONNECTING) {
915 			error = EALREADY;
916 			goto out;
917 		}
918 	}
919 
920 	mptcp_cancel_all_timers(mp_tp);
921 	if (mp_tp->mpt_state < MPTCPS_ESTABLISHED) {
922 		mptcp_close(mpte, mp_tp);
923 	} else if (((mp_so->so_options & SO_LINGER) &&
924 	    mp_so->so_linger == 0) ||
925 	    (mp_so->so_flags1 & SOF1_DEFUNCTINPROG)) {
926 		mptcp_drop(mpte, mp_tp, 0);
927 	} else {
928 		soisdisconnecting(mp_so);
929 		sbflush(&mp_so->so_rcv);
930 		if (mptcp_usrclosed(mpte) != NULL) {
931 			mptcp_output(mpte);
932 		}
933 	}
934 
935 	if (error == 0) {
936 		mptcp_subflow_workloop(mpte);
937 	}
938 
939 out:
940 	return error;
941 }
942 
943 /*
944  * Wrapper function to support disconnect on socket
945  */
946 static int
mptcp_usr_disconnect(struct socket * mp_so)947 mptcp_usr_disconnect(struct socket *mp_so)
948 {
949 	return mptcp_disconnect(mpsotompte(mp_so));
950 }
951 
952 /*
953  * User-protocol pru_disconnectx callback.
954  */
955 static int
mptcp_usr_disconnectx(struct socket * mp_so,sae_associd_t aid,sae_connid_t cid)956 mptcp_usr_disconnectx(struct socket *mp_so, sae_associd_t aid, sae_connid_t cid)
957 {
958 	if (aid != SAE_ASSOCID_ANY && aid != SAE_ASSOCID_ALL) {
959 		return EINVAL;
960 	}
961 
962 	if (cid != SAE_CONNID_ANY && cid != SAE_CONNID_ALL) {
963 		return EINVAL;
964 	}
965 
966 	return mptcp_usr_disconnect(mp_so);
967 }
968 
969 void
mptcp_finish_usrclosed(struct mptses * mpte)970 mptcp_finish_usrclosed(struct mptses *mpte)
971 {
972 	struct mptcb *mp_tp = mpte->mpte_mptcb;
973 	struct socket *mp_so = mptetoso(mpte);
974 
975 	if (mp_tp->mpt_state == MPTCPS_CLOSED || mp_tp->mpt_state == MPTCPS_TERMINATE) {
976 		mpte = mptcp_close(mpte, mp_tp);
977 	} else if (mp_tp->mpt_state >= MPTCPS_FIN_WAIT_2) {
978 		soisdisconnected(mp_so);
979 	} else {
980 		struct mptsub *mpts;
981 
982 		TAILQ_FOREACH(mpts, &mpte->mpte_subflows, mpts_entry) {
983 			if ((mp_so->so_state & (SS_CANTRCVMORE | SS_CANTSENDMORE)) ==
984 			    (SS_CANTRCVMORE | SS_CANTSENDMORE)) {
985 				mptcp_subflow_disconnect(mpte, mpts);
986 			} else {
987 				mptcp_subflow_shutdown(mpte, mpts);
988 			}
989 		}
990 	}
991 }
992 
993 /*
994  * User issued close, and wish to trail thru shutdown states.
995  */
996 static struct mptses *
mptcp_usrclosed(struct mptses * mpte)997 mptcp_usrclosed(struct mptses *mpte)
998 {
999 	struct mptcb *mp_tp = mpte->mpte_mptcb;
1000 
1001 	mptcp_close_fsm(mp_tp, MPCE_CLOSE);
1002 
1003 	/* Not everything has been acknowledged - don't close the subflows! */
1004 	if (mp_tp->mpt_state != MPTCPS_TERMINATE &&
1005 	    mp_tp->mpt_sndnxt + 1 != mp_tp->mpt_sndmax) {
1006 		return mpte;
1007 	}
1008 
1009 	mptcp_finish_usrclosed(mpte);
1010 
1011 	return mpte;
1012 }
1013 
1014 /*
1015  * After a receive, possible send some update to peer.
1016  */
1017 static int
mptcp_usr_rcvd(struct socket * mp_so,int flags)1018 mptcp_usr_rcvd(struct socket *mp_so, int flags)
1019 {
1020 #pragma unused(flags)
1021 	struct mppcb *mpp = mpsotomppcb(mp_so);
1022 	struct mptses *mpte;
1023 	struct mptsub *mpts;
1024 	int error = 0;
1025 
1026 	if (mpp == NULL || mpp->mpp_state == MPPCB_STATE_DEAD) {
1027 		error = EINVAL;
1028 		goto out;
1029 	}
1030 
1031 	mpte = mptompte(mpp);
1032 
1033 	TAILQ_FOREACH(mpts, &mpte->mpte_subflows, mpts_entry) {
1034 		struct socket *so = mpts->mpts_socket;
1035 
1036 		if (so->so_proto->pr_flags & PR_WANTRCVD && so->so_pcb != NULL) {
1037 			(*so->so_proto->pr_usrreqs->pru_rcvd)(so, 0);
1038 		}
1039 	}
1040 
1041 	error = mptcp_output(mpte);
1042 out:
1043 	return error;
1044 }
1045 
1046 /*
1047  * Do a send by putting data in the output queue.
1048  */
1049 static int
mptcp_usr_send(struct socket * mp_so,int prus_flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct proc * p)1050 mptcp_usr_send(struct socket *mp_so, int prus_flags, struct mbuf *m,
1051     struct sockaddr *nam, struct mbuf *control, struct proc *p)
1052 {
1053 #pragma unused(nam, p)
1054 	struct mppcb *mpp = mpsotomppcb(mp_so);
1055 	struct mptses *mpte;
1056 	int error = 0;
1057 
1058 	if (prus_flags & (PRUS_OOB | PRUS_EOF)) {
1059 		error = EOPNOTSUPP;
1060 		goto out;
1061 	}
1062 
1063 	if (nam != NULL) {
1064 		error = EOPNOTSUPP;
1065 		goto out;
1066 	}
1067 
1068 	if (control != NULL && control->m_len != 0) {
1069 		error = EOPNOTSUPP;
1070 		goto out;
1071 	}
1072 
1073 	if (mpp == NULL || mpp->mpp_state == MPPCB_STATE_DEAD) {
1074 		error = ECONNRESET;
1075 		goto out;
1076 	}
1077 	mpte = mptompte(mpp);
1078 	VERIFY(mpte != NULL);
1079 
1080 	if (!(mp_so->so_state & SS_ISCONNECTED) &&
1081 	    !(mp_so->so_flags1 & SOF1_PRECONNECT_DATA)) {
1082 		error = ENOTCONN;
1083 		goto out;
1084 	}
1085 
1086 	mptcp_insert_dsn(mpp, m);
1087 	VERIFY(mp_so->so_snd.sb_flags & SB_NOCOMPRESS);
1088 	sbappendstream(&mp_so->so_snd, m);
1089 	m = NULL;
1090 
1091 	error = mptcp_output(mpte);
1092 	if (error != 0) {
1093 		goto out;
1094 	}
1095 
1096 	if (mp_so->so_state & SS_ISCONNECTING) {
1097 		if (mp_so->so_state & SS_NBIO) {
1098 			error = EWOULDBLOCK;
1099 		} else {
1100 			error = sbwait(&mp_so->so_snd);
1101 		}
1102 	}
1103 
1104 out:
1105 	if (error) {
1106 		if (m != NULL) {
1107 			m_freem(m);
1108 		}
1109 		if (control != NULL) {
1110 			m_freem(control);
1111 		}
1112 	}
1113 	return error;
1114 }
1115 
1116 /*
1117  * Mark the MPTCP connection as being incapable of further output.
1118  */
1119 static int
mptcp_usr_shutdown(struct socket * mp_so)1120 mptcp_usr_shutdown(struct socket *mp_so)
1121 {
1122 	struct mppcb *mpp = mpsotomppcb(mp_so);
1123 	struct mptses *mpte;
1124 	int error = 0;
1125 
1126 	if (mpp == NULL || mpp->mpp_state == MPPCB_STATE_DEAD) {
1127 		error = EINVAL;
1128 		goto out;
1129 	}
1130 	mpte = mptompte(mpp);
1131 	VERIFY(mpte != NULL);
1132 
1133 	socantsendmore(mp_so);
1134 
1135 	mpte = mptcp_usrclosed(mpte);
1136 	if (mpte != NULL) {
1137 		error = mptcp_output(mpte);
1138 	}
1139 out:
1140 	return error;
1141 }
1142 
1143 /*
1144  * Copy the contents of uio into a properly sized mbuf chain.
1145  */
1146 static int
mptcp_uiotombuf(struct uio * uio,int how,user_ssize_t space,struct mbuf ** top)1147 mptcp_uiotombuf(struct uio *uio, int how, user_ssize_t space, struct mbuf **top)
1148 {
1149 	struct mbuf *m, *mb, *nm = NULL, *mtail = NULL;
1150 	int progress, len, error;
1151 	user_ssize_t resid, tot;
1152 
1153 	VERIFY(top != NULL && *top == NULL);
1154 
1155 	/*
1156 	 * space can be zero or an arbitrary large value bound by
1157 	 * the total data supplied by the uio.
1158 	 */
1159 	resid = uio_resid(uio);
1160 	if (space > 0) {
1161 		tot = MIN(resid, space);
1162 	} else {
1163 		tot = resid;
1164 	}
1165 
1166 	if (tot < 0 || tot > INT_MAX) {
1167 		return EINVAL;
1168 	}
1169 
1170 	len = (int)tot;
1171 	if (len == 0) {
1172 		len = 1;
1173 	}
1174 
1175 	/* Loop and append maximum sized mbufs to the chain tail. */
1176 	while (len > 0) {
1177 		uint32_t m_needed = 1;
1178 
1179 		if (njcl > 0 && len > MBIGCLBYTES) {
1180 			mb = m_getpackets_internal(&m_needed, 1,
1181 			    how, 1, M16KCLBYTES);
1182 		} else if (len > MCLBYTES) {
1183 			mb = m_getpackets_internal(&m_needed, 1,
1184 			    how, 1, MBIGCLBYTES);
1185 		} else if (len >= (signed)MINCLSIZE) {
1186 			mb = m_getpackets_internal(&m_needed, 1,
1187 			    how, 1, MCLBYTES);
1188 		} else {
1189 			mb = m_gethdr(how, MT_DATA);
1190 		}
1191 
1192 		/* Fail the whole operation if one mbuf can't be allocated. */
1193 		if (mb == NULL) {
1194 			if (nm != NULL) {
1195 				m_freem(nm);
1196 			}
1197 			return ENOBUFS;
1198 		}
1199 
1200 		/* Book keeping. */
1201 		VERIFY(mb->m_flags & M_PKTHDR);
1202 		len -= ((mb->m_flags & M_EXT) ? mb->m_ext.ext_size : MHLEN);
1203 		if (mtail != NULL) {
1204 			mtail->m_next = mb;
1205 		} else {
1206 			nm = mb;
1207 		}
1208 		mtail = mb;
1209 	}
1210 
1211 	m = nm;
1212 
1213 	progress = 0;
1214 	/* Fill all mbufs with uio data and update header information. */
1215 	for (mb = m; mb != NULL; mb = mb->m_next) {
1216 		/* tot >= 0 && tot <= INT_MAX (see above) */
1217 		len = MIN((int)M_TRAILINGSPACE(mb), (int)(tot - progress));
1218 
1219 		error = uiomove(mtod(mb, char *), len, uio);
1220 		if (error != 0) {
1221 			m_freem(m);
1222 			return error;
1223 		}
1224 
1225 		/* each mbuf is M_PKTHDR chained via m_next */
1226 		mb->m_len = len;
1227 		mb->m_pkthdr.len = len;
1228 
1229 		progress += len;
1230 	}
1231 	VERIFY(progress == tot);
1232 	*top = m;
1233 	return 0;
1234 }
1235 
1236 /*
1237  * MPTCP socket protocol-user socket send routine, derived from sosend().
1238  */
1239 static int
mptcp_usr_sosend(struct socket * mp_so,struct sockaddr * addr,struct uio * uio,struct mbuf * top,struct mbuf * control,int flags)1240 mptcp_usr_sosend(struct socket *mp_so, struct sockaddr *addr, struct uio *uio,
1241     struct mbuf *top, struct mbuf *control, int flags)
1242 {
1243 #pragma unused(addr)
1244 	user_ssize_t resid, space;
1245 	int error, sendflags;
1246 	struct proc *p = current_proc();
1247 	int sblocked = 0;
1248 
1249 	/* UIO is required for now, due to per-mbuf M_PKTHDR constrains */
1250 	if (uio == NULL || top != NULL) {
1251 		error = EINVAL;
1252 		goto out;
1253 	}
1254 	resid = uio_resid(uio);
1255 
1256 	socket_lock(mp_so, 1);
1257 	so_update_last_owner_locked(mp_so, p);
1258 	so_update_policy(mp_so);
1259 
1260 	VERIFY(mp_so->so_type == SOCK_STREAM);
1261 	VERIFY(!(mp_so->so_flags & SOF_MP_SUBFLOW));
1262 
1263 	if (flags & (MSG_OOB | MSG_DONTROUTE)) {
1264 		error = EOPNOTSUPP;
1265 		socket_unlock(mp_so, 1);
1266 		goto out;
1267 	}
1268 
1269 	/*
1270 	 * In theory resid should be unsigned.  However, space must be
1271 	 * signed, as it might be less than 0 if we over-committed, and we
1272 	 * must use a signed comparison of space and resid.  On the other
1273 	 * hand, a negative resid causes us to loop sending 0-length
1274 	 * segments to the protocol.
1275 	 */
1276 	if (resid < 0 || resid > INT_MAX ||
1277 	    (flags & MSG_EOR) || control != NULL) {
1278 		error = EINVAL;
1279 		socket_unlock(mp_so, 1);
1280 		goto out;
1281 	}
1282 
1283 	OSIncrementAtomicLong(&p->p_stats->p_ru.ru_msgsnd);
1284 
1285 	do {
1286 		error = sosendcheck(mp_so, NULL, resid, 0, 0, flags,
1287 		    &sblocked);
1288 		if (error != 0) {
1289 			goto release;
1290 		}
1291 
1292 		space = sbspace(&mp_so->so_snd);
1293 		do {
1294 			socket_unlock(mp_so, 0);
1295 			/*
1296 			 * Copy the data from userland into an mbuf chain.
1297 			 */
1298 			error = mptcp_uiotombuf(uio, M_WAITOK, space, &top);
1299 			if (error != 0) {
1300 				socket_lock(mp_so, 0);
1301 				goto release;
1302 			}
1303 			VERIFY(top != NULL);
1304 			space -= resid - uio_resid(uio);
1305 			resid = uio_resid(uio);
1306 			socket_lock(mp_so, 0);
1307 
1308 			/*
1309 			 * Compute flags here, for pru_send and NKEs.
1310 			 */
1311 			sendflags = (resid > 0 && space > 0) ?
1312 			    PRUS_MORETOCOME : 0;
1313 
1314 			/*
1315 			 * Socket filter processing
1316 			 */
1317 			VERIFY(control == NULL);
1318 			error = sflt_data_out(mp_so, NULL, &top, &control, 0);
1319 			if (error != 0) {
1320 				if (error == EJUSTRETURN) {
1321 					error = 0;
1322 					top = NULL;
1323 					/* always free control if any */
1324 				}
1325 				goto release;
1326 			}
1327 			if (control != NULL) {
1328 				m_freem(control);
1329 				control = NULL;
1330 			}
1331 
1332 			/*
1333 			 * Pass data to protocol.
1334 			 */
1335 			error = (*mp_so->so_proto->pr_usrreqs->pru_send)
1336 			    (mp_so, sendflags, top, NULL, NULL, p);
1337 
1338 			top = NULL;
1339 			if (error != 0) {
1340 				goto release;
1341 			}
1342 		} while (resid != 0 && space > 0);
1343 	} while (resid != 0);
1344 
1345 release:
1346 	if (sblocked) {
1347 		sbunlock(&mp_so->so_snd, FALSE); /* will unlock socket */
1348 	} else {
1349 		socket_unlock(mp_so, 1);
1350 	}
1351 out:
1352 	if (top != NULL) {
1353 		m_freem(top);
1354 	}
1355 	if (control != NULL) {
1356 		m_freem(control);
1357 	}
1358 
1359 	soclearfastopen(mp_so);
1360 
1361 	return error;
1362 }
1363 
1364 /*
1365  * Called to filter SOPT_{SET,GET} for SOL_SOCKET level socket options.
1366  * This routine simply indicates to the caller whether or not to proceed
1367  * further with the given socket option.  This is invoked by sosetoptlock()
1368  * and sogetoptlock().
1369  */
1370 static int
mptcp_usr_socheckopt(struct socket * mp_so,struct sockopt * sopt)1371 mptcp_usr_socheckopt(struct socket *mp_so, struct sockopt *sopt)
1372 {
1373 #pragma unused(mp_so)
1374 	int error = 0;
1375 
1376 	VERIFY(sopt->sopt_level == SOL_SOCKET);
1377 
1378 	/*
1379 	 * We could check for sopt_dir (set/get) here, but we'll just
1380 	 * let the caller deal with it as appropriate; therefore the
1381 	 * following is a superset of the socket options which we
1382 	 * allow for set/get.
1383 	 *
1384 	 * XXX: [email protected]
1385 	 *
1386 	 * Need to consider the following cases:
1387 	 *
1388 	 *   a.	Certain socket options don't have a clear definition
1389 	 *	on the expected behavior post connect(2).  At the time
1390 	 *	those options are issued on the MP socket, there may
1391 	 *	be existing subflow sockets that are already connected.
1392 	 */
1393 	switch (sopt->sopt_name) {
1394 	case SO_LINGER:                         /* MP */
1395 	case SO_LINGER_SEC:                     /* MP */
1396 	case SO_TYPE:                           /* MP */
1397 	case SO_NREAD:                          /* MP */
1398 	case SO_NWRITE:                         /* MP */
1399 	case SO_ERROR:                          /* MP */
1400 	case SO_SNDBUF:                         /* MP */
1401 	case SO_RCVBUF:                         /* MP */
1402 	case SO_SNDLOWAT:                       /* MP */
1403 	case SO_RCVLOWAT:                       /* MP */
1404 	case SO_SNDTIMEO:                       /* MP */
1405 	case SO_RCVTIMEO:                       /* MP */
1406 	case SO_NKE:                            /* MP */
1407 	case SO_NOSIGPIPE:                      /* MP */
1408 	case SO_NOADDRERR:                      /* MP */
1409 	case SO_LABEL:                          /* MP */
1410 	case SO_PEERLABEL:                      /* MP */
1411 	case SO_DEFUNCTIT:                      /* MP */
1412 	case SO_DEFUNCTOK:                      /* MP */
1413 	case SO_ISDEFUNCT:                      /* MP */
1414 	case SO_TRAFFIC_CLASS_DBG:              /* MP */
1415 	case SO_DELEGATED:                      /* MP */
1416 	case SO_DELEGATED_UUID:                 /* MP */
1417 #if NECP
1418 	case SO_NECP_ATTRIBUTES:
1419 	case SO_NECP_CLIENTUUID:
1420 #endif /* NECP */
1421 	case SO_MPKL_SEND_INFO:
1422 		/*
1423 		 * Tell the caller that these options are to be processed.
1424 		 */
1425 		break;
1426 
1427 	case SO_DEBUG:                          /* MP + subflow */
1428 	case SO_KEEPALIVE:                      /* MP + subflow */
1429 	case SO_USELOOPBACK:                    /* MP + subflow */
1430 	case SO_RANDOMPORT:                     /* MP + subflow */
1431 	case SO_TRAFFIC_CLASS:                  /* MP + subflow */
1432 	case SO_RECV_TRAFFIC_CLASS:             /* MP + subflow */
1433 	case SO_PRIVILEGED_TRAFFIC_CLASS:       /* MP + subflow */
1434 	case SO_RECV_ANYIF:                     /* MP + subflow */
1435 	case SO_RESTRICTIONS:                   /* MP + subflow */
1436 	case SO_FLUSH:                          /* MP + subflow */
1437 	case SO_NOWAKEFROMSLEEP:
1438 	case SO_NOAPNFALLBK:
1439 	case SO_MARK_CELLFALLBACK:
1440 	case SO_MARK_CELLFALLBACK_UUID:
1441 	case SO_MARK_KNOWN_TRACKER:
1442 	case SO_MARK_KNOWN_TRACKER_NON_APP_INITIATED:
1443 	case SO_MARK_APPROVED_APP_DOMAIN:
1444 	case SO_FALLBACK_MODE:
1445 		/*
1446 		 * Tell the caller that these options are to be processed;
1447 		 * these will also be recorded later by mptcp_setopt().
1448 		 *
1449 		 * NOTE: Only support integer option value for now.
1450 		 */
1451 		if (sopt->sopt_valsize != sizeof(int)) {
1452 			error = EINVAL;
1453 		}
1454 		break;
1455 
1456 	default:
1457 		/*
1458 		 * Tell the caller to stop immediately and return an error.
1459 		 */
1460 		error = ENOPROTOOPT;
1461 		break;
1462 	}
1463 
1464 	return error;
1465 }
1466 
1467 /*
1468  * Issue SOPT_SET for all MPTCP subflows (for integer option values.)
1469  */
1470 static int
mptcp_setopt_apply(struct mptses * mpte,struct mptopt * mpo)1471 mptcp_setopt_apply(struct mptses *mpte, struct mptopt *mpo)
1472 {
1473 	struct socket *mp_so;
1474 	struct mptsub *mpts;
1475 	struct mptopt smpo;
1476 	int error = 0;
1477 
1478 	/* just bail now if this isn't applicable to subflow sockets */
1479 	if (!(mpo->mpo_flags & MPOF_SUBFLOW_OK)) {
1480 		error = ENOPROTOOPT;
1481 		goto out;
1482 	}
1483 
1484 	/*
1485 	 * Skip those that are handled internally; these options
1486 	 * should not have been recorded and marked with the
1487 	 * MPOF_SUBFLOW_OK by mptcp_setopt(), but just in case.
1488 	 */
1489 	if (mpo->mpo_level == SOL_SOCKET &&
1490 	    (mpo->mpo_name == SO_NOSIGPIPE || mpo->mpo_name == SO_NOADDRERR)) {
1491 		error = ENOPROTOOPT;
1492 		goto out;
1493 	}
1494 
1495 	mp_so = mptetoso(mpte);
1496 
1497 	/*
1498 	 * Don't bother going further if there's no subflow; mark the option
1499 	 * with MPOF_INTERIM so that we know whether or not to remove this
1500 	 * option upon encountering an error while issuing it during subflow
1501 	 * socket creation.
1502 	 */
1503 	if (mpte->mpte_numflows == 0) {
1504 		VERIFY(TAILQ_EMPTY(&mpte->mpte_subflows));
1505 		mpo->mpo_flags |= MPOF_INTERIM;
1506 		/* return success */
1507 		goto out;
1508 	}
1509 
1510 	bzero(&smpo, sizeof(smpo));
1511 	smpo.mpo_flags |= MPOF_SUBFLOW_OK;
1512 	smpo.mpo_level = mpo->mpo_level;
1513 	smpo.mpo_name = mpo->mpo_name;
1514 
1515 	/* grab exisiting values in case we need to rollback */
1516 	TAILQ_FOREACH(mpts, &mpte->mpte_subflows, mpts_entry) {
1517 		struct socket *so;
1518 
1519 		mpts->mpts_flags &= ~(MPTSF_SOPT_OLDVAL | MPTSF_SOPT_INPROG);
1520 		mpts->mpts_oldintval = 0;
1521 		smpo.mpo_intval = 0;
1522 		VERIFY(mpts->mpts_socket != NULL);
1523 		so = mpts->mpts_socket;
1524 		if (mptcp_subflow_sogetopt(mpte, so, &smpo) == 0) {
1525 			mpts->mpts_flags |= MPTSF_SOPT_OLDVAL;
1526 			mpts->mpts_oldintval = smpo.mpo_intval;
1527 		}
1528 	}
1529 
1530 	/* apply socket option */
1531 	TAILQ_FOREACH(mpts, &mpte->mpte_subflows, mpts_entry) {
1532 		struct socket *so;
1533 
1534 		mpts->mpts_flags |= MPTSF_SOPT_INPROG;
1535 		VERIFY(mpts->mpts_socket != NULL);
1536 		so = mpts->mpts_socket;
1537 		error = mptcp_subflow_sosetopt(mpte, mpts, mpo);
1538 		if (error != 0) {
1539 			break;
1540 		}
1541 	}
1542 
1543 	/* cleanup, and rollback if needed */
1544 	TAILQ_FOREACH(mpts, &mpte->mpte_subflows, mpts_entry) {
1545 		struct socket *so;
1546 
1547 		if (!(mpts->mpts_flags & MPTSF_SOPT_INPROG)) {
1548 			/* clear in case it's set */
1549 			mpts->mpts_flags &= ~MPTSF_SOPT_OLDVAL;
1550 			mpts->mpts_oldintval = 0;
1551 			continue;
1552 		}
1553 		if (!(mpts->mpts_flags & MPTSF_SOPT_OLDVAL)) {
1554 			mpts->mpts_flags &= ~MPTSF_SOPT_INPROG;
1555 			VERIFY(mpts->mpts_oldintval == 0);
1556 			continue;
1557 		}
1558 		/* error during sosetopt, so roll it back */
1559 		if (error != 0) {
1560 			VERIFY(mpts->mpts_socket != NULL);
1561 			so = mpts->mpts_socket;
1562 			smpo.mpo_intval = mpts->mpts_oldintval;
1563 			mptcp_subflow_sosetopt(mpte, mpts, &smpo);
1564 		}
1565 		mpts->mpts_oldintval = 0;
1566 		mpts->mpts_flags &= ~(MPTSF_SOPT_OLDVAL | MPTSF_SOPT_INPROG);
1567 	}
1568 
1569 out:
1570 	return error;
1571 }
1572 
1573 /*
1574  * Handle SOPT_SET for socket options issued on MP socket.
1575  */
1576 static int
mptcp_setopt(struct mptses * mpte,struct sockopt * sopt)1577 mptcp_setopt(struct mptses *mpte, struct sockopt *sopt)
1578 {
1579 	int error = 0, optval = 0, level, optname, rec = 1;
1580 	struct mptopt smpo, *mpo = NULL;
1581 	struct socket *mp_so;
1582 
1583 	level = sopt->sopt_level;
1584 	optname = sopt->sopt_name;
1585 
1586 	mp_so = mptetoso(mpte);
1587 
1588 	VERIFY(!(mpsotomppcb(mp_so)->mpp_flags & MPP_INSIDE_SETGETOPT));
1589 	mpsotomppcb(mp_so)->mpp_flags |= MPP_INSIDE_SETGETOPT;
1590 
1591 	/*
1592 	 * Record socket options which are applicable to subflow sockets so
1593 	 * that we can replay them for new ones; see mptcp_usr_socheckopt()
1594 	 * for the list of eligible socket-level options.
1595 	 */
1596 	if (level == SOL_SOCKET) {
1597 		switch (optname) {
1598 		case SO_DEBUG:
1599 		case SO_KEEPALIVE:
1600 		case SO_USELOOPBACK:
1601 		case SO_RANDOMPORT:
1602 		case SO_TRAFFIC_CLASS:
1603 		case SO_RECV_TRAFFIC_CLASS:
1604 		case SO_PRIVILEGED_TRAFFIC_CLASS:
1605 		case SO_RECV_ANYIF:
1606 		case SO_RESTRICTIONS:
1607 		case SO_NOWAKEFROMSLEEP:
1608 		case SO_NOAPNFALLBK:
1609 		case SO_MARK_CELLFALLBACK:
1610 		case SO_MARK_KNOWN_TRACKER:
1611 		case SO_MARK_KNOWN_TRACKER_NON_APP_INITIATED:
1612 		case SO_MARK_APPROVED_APP_DOMAIN:
1613 		case SO_FALLBACK_MODE:
1614 			/* record it */
1615 			break;
1616 		case SO_FLUSH:
1617 			/* don't record it */
1618 			rec = 0;
1619 			break;
1620 
1621 		/* Next ones, record at MPTCP-level */
1622 		case SO_DELEGATED:
1623 			error = sooptcopyin(sopt, &mpte->mpte_epid,
1624 			    sizeof(int), sizeof(int));
1625 			if (error != 0) {
1626 				goto err_out;
1627 			}
1628 
1629 			goto out;
1630 		case SO_DELEGATED_UUID:
1631 			error = sooptcopyin(sopt, &mpte->mpte_euuid,
1632 			    sizeof(uuid_t), sizeof(uuid_t));
1633 			if (error != 0) {
1634 				goto err_out;
1635 			}
1636 
1637 			goto out;
1638 #if NECP
1639 		case SO_NECP_CLIENTUUID:
1640 			if (!uuid_is_null(mpsotomppcb(mp_so)->necp_client_uuid)) {
1641 				error = EINVAL;
1642 				goto err_out;
1643 			}
1644 
1645 			error = sooptcopyin(sopt, &mpsotomppcb(mp_so)->necp_client_uuid,
1646 			    sizeof(uuid_t), sizeof(uuid_t));
1647 			if (error != 0) {
1648 				goto err_out;
1649 			}
1650 
1651 			mpsotomppcb(mp_so)->necp_cb = mptcp_session_necp_cb;
1652 			error = necp_client_register_multipath_cb(mp_so->last_pid,
1653 			    mpsotomppcb(mp_so)->necp_client_uuid,
1654 			    mpsotomppcb(mp_so));
1655 			if (error) {
1656 				goto err_out;
1657 			}
1658 
1659 			if (uuid_is_null(mpsotomppcb(mp_so)->necp_client_uuid)) {
1660 				error = EINVAL;
1661 				goto err_out;
1662 			}
1663 
1664 			goto out;
1665 		case SO_NECP_ATTRIBUTES:
1666 			error = necp_set_socket_attributes(&mpsotomppcb(mp_so)->inp_necp_attributes, sopt);
1667 			if (error) {
1668 				goto err_out;
1669 			}
1670 
1671 			goto out;
1672 #endif /* NECP */
1673 		default:
1674 			/* nothing to do; just return */
1675 			goto out;
1676 		}
1677 	} else if (sopt->sopt_level == IPPROTO_IP) {
1678 		switch (optname) {
1679 		case IP_TOS:
1680 			/* eligible; record it */
1681 			break;
1682 		default:
1683 			/* not eligible */
1684 			error = ENOPROTOOPT;
1685 			goto err_out;
1686 		}
1687 	} else if (sopt->sopt_level == IPPROTO_IPV6) {
1688 		switch (optname) {
1689 		case IPV6_TCLASS:
1690 			/* eligible; record it */
1691 			break;
1692 		default:
1693 			/* not eligible */
1694 			error = ENOPROTOOPT;
1695 			goto err_out;
1696 		}
1697 	} else {
1698 		switch (optname) {
1699 		case TCP_NODELAY:
1700 		case TCP_RXT_FINDROP:
1701 		case TCP_KEEPALIVE:
1702 		case TCP_KEEPINTVL:
1703 		case TCP_KEEPCNT:
1704 		case TCP_CONNECTIONTIMEOUT:
1705 		case TCP_RXT_CONNDROPTIME:
1706 		case PERSIST_TIMEOUT:
1707 		case TCP_ADAPTIVE_READ_TIMEOUT:
1708 		case TCP_ADAPTIVE_WRITE_TIMEOUT:
1709 		case TCP_FASTOPEN_FORCE_ENABLE:
1710 			/* eligible; record it */
1711 			break;
1712 		case TCP_NOTSENT_LOWAT:
1713 			/* record at MPTCP level */
1714 			error = sooptcopyin(sopt, &optval, sizeof(optval),
1715 			    sizeof(optval));
1716 			if (error) {
1717 				goto err_out;
1718 			}
1719 			if (optval < 0) {
1720 				error = EINVAL;
1721 				goto err_out;
1722 			} else {
1723 				if (optval == 0) {
1724 					mp_so->so_flags &= ~SOF_NOTSENT_LOWAT;
1725 					error = mptcp_set_notsent_lowat(mpte, 0);
1726 				} else {
1727 					mp_so->so_flags |= SOF_NOTSENT_LOWAT;
1728 					error = mptcp_set_notsent_lowat(mpte,
1729 					    optval);
1730 				}
1731 
1732 				if (error) {
1733 					goto err_out;
1734 				}
1735 			}
1736 			goto out;
1737 		case MPTCP_SERVICE_TYPE:
1738 			/* record at MPTCP level */
1739 			error = sooptcopyin(sopt, &optval, sizeof(optval),
1740 			    sizeof(optval));
1741 			if (error) {
1742 				goto err_out;
1743 			}
1744 			if (optval < 0 || optval >= MPTCP_SVCTYPE_MAX) {
1745 				error = EINVAL;
1746 				goto err_out;
1747 			}
1748 
1749 			if (mptcp_entitlement_check(mp_so, (uint8_t)optval) < 0) {
1750 				error = EACCES;
1751 				goto err_out;
1752 			}
1753 
1754 			mpte->mpte_svctype = (uint8_t)optval;
1755 			mpte->mpte_flags |= MPTE_SVCTYPE_CHECKED;
1756 
1757 			goto out;
1758 		case MPTCP_ALTERNATE_PORT:
1759 			/* record at MPTCP level */
1760 			error = sooptcopyin(sopt, &optval, sizeof(optval),
1761 			    sizeof(optval));
1762 			if (error) {
1763 				goto err_out;
1764 			}
1765 
1766 			if (optval < 0 || optval > UINT16_MAX) {
1767 				error = EINVAL;
1768 				goto err_out;
1769 			}
1770 
1771 			mpte->mpte_alternate_port = (uint16_t)optval;
1772 
1773 			goto out;
1774 		case MPTCP_FORCE_ENABLE:
1775 			/* record at MPTCP level */
1776 			error = sooptcopyin(sopt, &optval, sizeof(optval),
1777 			    sizeof(optval));
1778 			if (error) {
1779 				goto err_out;
1780 			}
1781 
1782 			if (optval < 0 || optval > 1) {
1783 				error = EINVAL;
1784 				goto err_out;
1785 			}
1786 
1787 			if (optval) {
1788 				mpte->mpte_flags |= MPTE_FORCE_ENABLE;
1789 			} else {
1790 				mpte->mpte_flags &= ~MPTE_FORCE_ENABLE;
1791 			}
1792 
1793 			goto out;
1794 		case MPTCP_FORCE_VERSION:
1795 			error = sooptcopyin(sopt, &optval, sizeof(optval),
1796 			    sizeof(optval));
1797 			if (error) {
1798 				goto err_out;
1799 			}
1800 
1801 			if (optval != 0 && optval != 1) {
1802 				error = EINVAL;
1803 				goto err_out;
1804 			}
1805 
1806 			if (optval == 0) {
1807 				mpte->mpte_flags |= MPTE_FORCE_V0;
1808 				mpte->mpte_flags &= ~MPTE_FORCE_V1;
1809 			} else {
1810 				mpte->mpte_flags |= MPTE_FORCE_V1;
1811 				mpte->mpte_flags &= ~MPTE_FORCE_V0;
1812 			}
1813 
1814 			goto out;
1815 		case MPTCP_EXPECTED_PROGRESS_TARGET:
1816 		{
1817 			struct mptcb *mp_tp = mpte->mpte_mptcb;
1818 			uint64_t mach_time_target;
1819 			uint64_t nanoseconds;
1820 
1821 			if (mpte->mpte_svctype != MPTCP_SVCTYPE_TARGET_BASED) {
1822 				os_log(mptcp_log_handle, "%s - %lx: Can't set urgent activity when svctype is %u\n",
1823 				    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), mpte->mpte_svctype);
1824 				error = EINVAL;
1825 				goto err_out;
1826 			}
1827 
1828 			error = sooptcopyin(sopt, &mach_time_target, sizeof(mach_time_target), sizeof(mach_time_target));
1829 			if (error) {
1830 				goto err_out;
1831 			}
1832 
1833 			if (!mptcp_ok_to_create_subflows(mp_tp)) {
1834 				os_log(mptcp_log_handle, "%s - %lx: Not ok to create subflows, state %u flags %#x\n",
1835 				    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), mp_tp->mpt_state, mp_tp->mpt_flags);
1836 				error = EINVAL;
1837 				goto err_out;
1838 			}
1839 
1840 			if (mach_time_target) {
1841 				uint64_t time_now = 0;
1842 				uint64_t time_now_nanoseconds;
1843 
1844 				absolutetime_to_nanoseconds(mach_time_target, &nanoseconds);
1845 				nanoseconds = nanoseconds - (mptcp_expected_progress_headstart * NSEC_PER_MSEC);
1846 
1847 				time_now = mach_continuous_time();
1848 				absolutetime_to_nanoseconds(time_now, &time_now_nanoseconds);
1849 
1850 				nanoseconds_to_absolutetime(nanoseconds, &mach_time_target);
1851 				/* If the timer is already running and it would
1852 				 * fire in less than mptcp_expected_progress_headstart
1853 				 * seconds, then it's not worth canceling it.
1854 				 */
1855 				if (mpte->mpte_time_target &&
1856 				    mpte->mpte_time_target < time_now &&
1857 				    time_now_nanoseconds > nanoseconds - (mptcp_expected_progress_headstart * NSEC_PER_MSEC)) {
1858 					os_log(mptcp_log_handle, "%s - %lx: Not rescheduling timer %llu now %llu target %llu\n",
1859 					    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte),
1860 					    mpte->mpte_time_target,
1861 					    time_now,
1862 					    mach_time_target);
1863 					goto out;
1864 				}
1865 			}
1866 
1867 			mpte->mpte_time_target = mach_time_target;
1868 			mptcp_set_urgency_timer(mpte);
1869 
1870 			goto out;
1871 		}
1872 		default:
1873 			/* not eligible */
1874 			error = ENOPROTOOPT;
1875 			goto err_out;
1876 		}
1877 	}
1878 
1879 	if ((error = sooptcopyin(sopt, &optval, sizeof(optval),
1880 	    sizeof(optval))) != 0) {
1881 		goto err_out;
1882 	}
1883 
1884 	if (rec) {
1885 		/* search for an existing one; if not found, allocate */
1886 		if ((mpo = mptcp_sopt_find(mpte, sopt)) == NULL) {
1887 			mpo = mptcp_sopt_alloc();
1888 		}
1889 
1890 		/* initialize or update, as needed */
1891 		mpo->mpo_intval = optval;
1892 		if (!(mpo->mpo_flags & MPOF_ATTACHED)) {
1893 			mpo->mpo_level = level;
1894 			mpo->mpo_name = optname;
1895 			mptcp_sopt_insert(mpte, mpo);
1896 		}
1897 		/* this can be issued on the subflow socket */
1898 		mpo->mpo_flags |= MPOF_SUBFLOW_OK;
1899 	} else {
1900 		bzero(&smpo, sizeof(smpo));
1901 		mpo = &smpo;
1902 		mpo->mpo_flags |= MPOF_SUBFLOW_OK;
1903 		mpo->mpo_level = level;
1904 		mpo->mpo_name = optname;
1905 		mpo->mpo_intval = optval;
1906 	}
1907 
1908 	/* issue this socket option on existing subflows */
1909 	error = mptcp_setopt_apply(mpte, mpo);
1910 	if (error != 0 && (mpo->mpo_flags & MPOF_ATTACHED)) {
1911 		VERIFY(mpo != &smpo);
1912 		mptcp_sopt_remove(mpte, mpo);
1913 		mptcp_sopt_free(mpo);
1914 	}
1915 	if (mpo == &smpo) {
1916 		mpo->mpo_flags &= ~MPOF_INTERIM;
1917 	}
1918 
1919 	if (error) {
1920 		goto err_out;
1921 	}
1922 
1923 out:
1924 
1925 	mpsotomppcb(mp_so)->mpp_flags &= ~MPP_INSIDE_SETGETOPT;
1926 	return 0;
1927 
1928 err_out:
1929 	os_log_error(mptcp_log_handle, "%s - %lx: sopt %s (%d, %d) val %d can't be issued error %d\n",
1930 	    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte),
1931 	    mptcp_sopt2str(level, optname), level, optname, optval, error);
1932 	mpsotomppcb(mp_so)->mpp_flags &= ~MPP_INSIDE_SETGETOPT;
1933 	return error;
1934 }
1935 
1936 static void
mptcp_fill_info_bytestats(struct tcp_info * ti,struct mptses * mpte)1937 mptcp_fill_info_bytestats(struct tcp_info *ti, struct mptses *mpte)
1938 {
1939 	struct mptsub *mpts;
1940 	int i;
1941 
1942 	TAILQ_FOREACH(mpts, &mpte->mpte_subflows, mpts_entry) {
1943 		const struct inpcb *inp = sotoinpcb(mpts->mpts_socket);
1944 
1945 		if (inp == NULL) {
1946 			continue;
1947 		}
1948 
1949 		ti->tcpi_txbytes += inp->inp_stat->txbytes;
1950 		ti->tcpi_rxbytes += inp->inp_stat->rxbytes;
1951 		ti->tcpi_cell_txbytes += inp->inp_cstat->txbytes;
1952 		ti->tcpi_cell_rxbytes += inp->inp_cstat->rxbytes;
1953 		ti->tcpi_wifi_txbytes += inp->inp_wstat->txbytes;
1954 		ti->tcpi_wifi_rxbytes += inp->inp_wstat->rxbytes;
1955 		ti->tcpi_wired_txbytes += inp->inp_Wstat->txbytes;
1956 		ti->tcpi_wired_rxbytes += inp->inp_Wstat->rxbytes;
1957 	}
1958 
1959 	for (i = 0; i < MPTCP_ITFSTATS_SIZE; i++) {
1960 		struct mptcp_itf_stats *stats = &mpte->mpte_itfstats[i];
1961 
1962 		ti->tcpi_txbytes += stats->mpis_txbytes;
1963 		ti->tcpi_rxbytes += stats->mpis_rxbytes;
1964 
1965 		ti->tcpi_wifi_txbytes += stats->mpis_wifi_txbytes;
1966 		ti->tcpi_wifi_rxbytes += stats->mpis_wifi_rxbytes;
1967 
1968 		ti->tcpi_wired_txbytes += stats->mpis_wired_txbytes;
1969 		ti->tcpi_wired_rxbytes += stats->mpis_wired_rxbytes;
1970 
1971 		ti->tcpi_cell_txbytes += stats->mpis_cell_txbytes;
1972 		ti->tcpi_cell_rxbytes += stats->mpis_cell_rxbytes;
1973 	}
1974 }
1975 
1976 static void
mptcp_fill_info(struct mptses * mpte,struct tcp_info * ti)1977 mptcp_fill_info(struct mptses *mpte, struct tcp_info *ti)
1978 {
1979 	struct mptsub *actsub = mpte->mpte_active_sub;
1980 	struct mptcb *mp_tp = mpte->mpte_mptcb;
1981 	struct tcpcb *acttp = NULL;
1982 
1983 	if (actsub) {
1984 		acttp = sototcpcb(actsub->mpts_socket);
1985 	}
1986 
1987 	bzero(ti, sizeof(*ti));
1988 
1989 	ti->tcpi_state = (uint8_t)mp_tp->mpt_state;
1990 	/* tcpi_options */
1991 	/* tcpi_snd_wscale */
1992 	/* tcpi_rcv_wscale */
1993 	/* tcpi_flags */
1994 	if (acttp) {
1995 		ti->tcpi_rto = acttp->t_timer[TCPT_REXMT] ? acttp->t_rxtcur : 0;
1996 	}
1997 
1998 	/* tcpi_snd_mss */
1999 	/* tcpi_rcv_mss */
2000 	if (acttp) {
2001 		ti->tcpi_rttcur = acttp->t_rttcur;
2002 		ti->tcpi_srtt = acttp->t_srtt >> TCP_RTT_SHIFT;
2003 		ti->tcpi_rttvar = acttp->t_rttvar >> TCP_RTTVAR_SHIFT;
2004 		ti->tcpi_rttbest = acttp->t_rttbest >> TCP_RTT_SHIFT;
2005 		ti->tcpi_rcv_srtt = acttp->rcv_srtt >> TCP_RTT_SHIFT;
2006 	}
2007 	/* tcpi_snd_ssthresh */
2008 	/* tcpi_snd_cwnd */
2009 	/* tcpi_rcv_space */
2010 	ti->tcpi_snd_wnd = mp_tp->mpt_sndwnd;
2011 	ti->tcpi_snd_nxt = (uint32_t)mp_tp->mpt_sndnxt;
2012 	ti->tcpi_rcv_nxt = (uint32_t)mp_tp->mpt_rcvnxt;
2013 	if (acttp) {
2014 		ti->tcpi_last_outif = (acttp->t_inpcb->inp_last_outifp == NULL) ? 0 :
2015 		    acttp->t_inpcb->inp_last_outifp->if_index;
2016 	}
2017 
2018 	mptcp_fill_info_bytestats(ti, mpte);
2019 	/* tcpi_txpackets */
2020 
2021 	/* tcpi_txretransmitbytes */
2022 	/* tcpi_txunacked */
2023 	/* tcpi_rxpackets */
2024 
2025 	/* tcpi_rxduplicatebytes */
2026 	/* tcpi_rxoutoforderbytes */
2027 	/* tcpi_snd_bw */
2028 	/* tcpi_synrexmits */
2029 	/* tcpi_unused1 */
2030 	/* tcpi_unused2 */
2031 	/* tcpi_cell_rxpackets */
2032 
2033 	/* tcpi_cell_txpackets */
2034 
2035 	/* tcpi_wifi_rxpackets */
2036 
2037 	/* tcpi_wifi_txpackets */
2038 
2039 	/* tcpi_wired_rxpackets */
2040 	/* tcpi_wired_txpackets */
2041 	/* tcpi_connstatus */
2042 	/* TFO-stuff */
2043 	/* ECN stuff */
2044 	/* tcpi_ecn_recv_ce */
2045 	/* tcpi_ecn_recv_cwr */
2046 	if (acttp) {
2047 		ti->tcpi_rcvoopack = acttp->t_rcvoopack;
2048 	}
2049 	/* tcpi_pawsdrop */
2050 	/* tcpi_sack_recovery_episode */
2051 	/* tcpi_reordered_pkts */
2052 	/* tcpi_dsack_sent */
2053 	/* tcpi_dsack_recvd */
2054 	/* tcpi_flowhash */
2055 	if (acttp) {
2056 		ti->tcpi_txretransmitpackets = acttp->t_stat.rxmitpkts;
2057 	}
2058 }
2059 
2060 /*
2061  * Handle SOPT_GET for socket options issued on MP socket.
2062  */
2063 static int
mptcp_getopt(struct mptses * mpte,struct sockopt * sopt)2064 mptcp_getopt(struct mptses *mpte, struct sockopt *sopt)
2065 {
2066 	int error = 0, optval = 0;
2067 	struct socket *__single mp_so;
2068 
2069 	mp_so = mptetoso(mpte);
2070 
2071 	VERIFY(!(mpsotomppcb(mp_so)->mpp_flags & MPP_INSIDE_SETGETOPT));
2072 	mpsotomppcb(mp_so)->mpp_flags |= MPP_INSIDE_SETGETOPT;
2073 
2074 	/*
2075 	 * We only handle SOPT_GET for TCP level socket options; we should
2076 	 * not get here for socket level options since they are already
2077 	 * handled at the socket layer.
2078 	 */
2079 	if (sopt->sopt_level != IPPROTO_TCP) {
2080 		error = ENOPROTOOPT;
2081 		goto out;
2082 	}
2083 
2084 	switch (sopt->sopt_name) {
2085 	case PERSIST_TIMEOUT:
2086 		/* Only case for which we have a non-zero default */
2087 		optval = tcp_max_persist_timeout;
2088 		OS_FALLTHROUGH;
2089 	case TCP_NODELAY:
2090 	case TCP_RXT_FINDROP:
2091 	case TCP_KEEPALIVE:
2092 	case TCP_KEEPINTVL:
2093 	case TCP_KEEPCNT:
2094 	case TCP_CONNECTIONTIMEOUT:
2095 	case TCP_RXT_CONNDROPTIME:
2096 	case TCP_ADAPTIVE_READ_TIMEOUT:
2097 	case TCP_ADAPTIVE_WRITE_TIMEOUT:
2098 	case TCP_FASTOPEN_FORCE_ENABLE:
2099 	{
2100 		struct mptopt *__single mpo = mptcp_sopt_find(mpte, sopt);
2101 
2102 		if (mpo != NULL) {
2103 			optval = mpo->mpo_intval;
2104 		}
2105 		break;
2106 	}
2107 
2108 	/* The next ones are stored at the MPTCP-level */
2109 	case TCP_NOTSENT_LOWAT:
2110 		if (mptetoso(mpte)->so_flags & SOF_NOTSENT_LOWAT) {
2111 			optval = mptcp_get_notsent_lowat(mpte);
2112 		} else {
2113 			optval = 0;
2114 		}
2115 		break;
2116 	case TCP_INFO:
2117 	{
2118 		struct tcp_info ti;
2119 
2120 		mptcp_fill_info(mpte, &ti);
2121 		error = sooptcopyout(sopt, &ti, sizeof(struct tcp_info));
2122 
2123 		goto out;
2124 	}
2125 	case MPTCP_SERVICE_TYPE:
2126 		optval = mpte->mpte_svctype;
2127 		break;
2128 	case MPTCP_ALTERNATE_PORT:
2129 		optval = mpte->mpte_alternate_port;
2130 		break;
2131 	case MPTCP_FORCE_ENABLE:
2132 		optval = !!(mpte->mpte_flags & MPTE_FORCE_ENABLE);
2133 		break;
2134 	case MPTCP_FORCE_VERSION:
2135 		if (mpte->mpte_flags & MPTE_FORCE_V0) {
2136 			optval = 0;
2137 		} else if (mpte->mpte_flags & MPTE_FORCE_V1) {
2138 			optval = 1;
2139 		} else {
2140 			optval = -1;
2141 		}
2142 		break;
2143 	case MPTCP_EXPECTED_PROGRESS_TARGET:
2144 		error = sooptcopyout(sopt, &mpte->mpte_time_target, sizeof(mpte->mpte_time_target));
2145 
2146 		goto out;
2147 	default:
2148 		/* not eligible */
2149 		error = ENOPROTOOPT;
2150 		break;
2151 	}
2152 
2153 	if (error == 0) {
2154 		error = sooptcopyout(sopt, &optval, sizeof(int));
2155 	}
2156 
2157 out:
2158 	mpsotomppcb(mp_so)->mpp_flags &= ~MPP_INSIDE_SETGETOPT;
2159 	return error;
2160 }
2161 
2162 /*
2163  * MPTCP SOPT_{SET,GET} socket option handler, for options issued on the MP
2164  * socket, at SOL_SOCKET and IPPROTO_TCP levels.  The former is restricted
2165  * to those that are allowed by mptcp_usr_socheckopt().
2166  */
2167 int
mptcp_ctloutput(struct socket * mp_so,struct sockopt * sopt)2168 mptcp_ctloutput(struct socket *mp_so, struct sockopt *sopt)
2169 {
2170 	struct mppcb *__single mpp = mpsotomppcb(mp_so);
2171 	struct mptses *__single mpte;
2172 	int error = 0;
2173 
2174 	if (mpp == NULL || mpp->mpp_state == MPPCB_STATE_DEAD) {
2175 		error = EINVAL;
2176 		goto out;
2177 	}
2178 	mpte = mptompte(mpp);
2179 	socket_lock_assert_owned(mp_so);
2180 
2181 	/* we only handle socket and TCP-level socket options for MPTCP */
2182 	if (sopt->sopt_level != SOL_SOCKET && sopt->sopt_level != IPPROTO_TCP &&
2183 	    sopt->sopt_level != IPPROTO_IP && sopt->sopt_level != IPPROTO_IPV6) {
2184 		error = EINVAL;
2185 		goto out;
2186 	}
2187 
2188 	switch (sopt->sopt_dir) {
2189 	case SOPT_SET:
2190 		error = mptcp_setopt(mpte, sopt);
2191 		break;
2192 
2193 	case SOPT_GET:
2194 		error = mptcp_getopt(mpte, sopt);
2195 		break;
2196 	}
2197 out:
2198 	return error;
2199 }
2200 
2201 const char *
mptcp_sopt2str(int level,int optname)2202 mptcp_sopt2str(int level, int optname)
2203 {
2204 	switch (level) {
2205 	case SOL_SOCKET:
2206 		switch (optname) {
2207 		case SO_LINGER:
2208 			return "SO_LINGER";
2209 		case SO_LINGER_SEC:
2210 			return "SO_LINGER_SEC";
2211 		case SO_DEBUG:
2212 			return "SO_DEBUG";
2213 		case SO_KEEPALIVE:
2214 			return "SO_KEEPALIVE";
2215 		case SO_USELOOPBACK:
2216 			return "SO_USELOOPBACK";
2217 		case SO_TYPE:
2218 			return "SO_TYPE";
2219 		case SO_NREAD:
2220 			return "SO_NREAD";
2221 		case SO_NWRITE:
2222 			return "SO_NWRITE";
2223 		case SO_ERROR:
2224 			return "SO_ERROR";
2225 		case SO_SNDBUF:
2226 			return "SO_SNDBUF";
2227 		case SO_RCVBUF:
2228 			return "SO_RCVBUF";
2229 		case SO_SNDLOWAT:
2230 			return "SO_SNDLOWAT";
2231 		case SO_RCVLOWAT:
2232 			return "SO_RCVLOWAT";
2233 		case SO_SNDTIMEO:
2234 			return "SO_SNDTIMEO";
2235 		case SO_RCVTIMEO:
2236 			return "SO_RCVTIMEO";
2237 		case SO_NKE:
2238 			return "SO_NKE";
2239 		case SO_NOSIGPIPE:
2240 			return "SO_NOSIGPIPE";
2241 		case SO_NOADDRERR:
2242 			return "SO_NOADDRERR";
2243 		case SO_RESTRICTIONS:
2244 			return "SO_RESTRICTIONS";
2245 		case SO_LABEL:
2246 			return "SO_LABEL";
2247 		case SO_PEERLABEL:
2248 			return "SO_PEERLABEL";
2249 		case SO_RANDOMPORT:
2250 			return "SO_RANDOMPORT";
2251 		case SO_TRAFFIC_CLASS:
2252 			return "SO_TRAFFIC_CLASS";
2253 		case SO_RECV_TRAFFIC_CLASS:
2254 			return "SO_RECV_TRAFFIC_CLASS";
2255 		case SO_TRAFFIC_CLASS_DBG:
2256 			return "SO_TRAFFIC_CLASS_DBG";
2257 		case SO_PRIVILEGED_TRAFFIC_CLASS:
2258 			return "SO_PRIVILEGED_TRAFFIC_CLASS";
2259 		case SO_DEFUNCTIT:
2260 			return "SO_DEFUNCTIT";
2261 		case SO_DEFUNCTOK:
2262 			return "SO_DEFUNCTOK";
2263 		case SO_ISDEFUNCT:
2264 			return "SO_ISDEFUNCT";
2265 		case SO_OPPORTUNISTIC:
2266 			return "SO_OPPORTUNISTIC";
2267 		case SO_FLUSH:
2268 			return "SO_FLUSH";
2269 		case SO_RECV_ANYIF:
2270 			return "SO_RECV_ANYIF";
2271 		case SO_NOWAKEFROMSLEEP:
2272 			return "SO_NOWAKEFROMSLEEP";
2273 		case SO_NOAPNFALLBK:
2274 			return "SO_NOAPNFALLBK";
2275 		case SO_MARK_CELLFALLBACK:
2276 			return "SO_CELLFALLBACK";
2277 		case SO_FALLBACK_MODE:
2278 			return "SO_FALLBACK_MODE";
2279 		case SO_MARK_KNOWN_TRACKER:
2280 			return "SO_MARK_KNOWN_TRACKER";
2281 		case SO_MARK_KNOWN_TRACKER_NON_APP_INITIATED:
2282 			return "SO_MARK_KNOWN_TRACKER_NON_APP_INITIATED";
2283 		case SO_MARK_APPROVED_APP_DOMAIN:
2284 			return "SO_MARK_APPROVED_APP_DOMAIN";
2285 		case SO_DELEGATED:
2286 			return "SO_DELEGATED";
2287 		case SO_DELEGATED_UUID:
2288 			return "SO_DELEGATED_UUID";
2289 #if NECP
2290 		case SO_NECP_ATTRIBUTES:
2291 			return "SO_NECP_ATTRIBUTES";
2292 		case SO_NECP_CLIENTUUID:
2293 			return "SO_NECP_CLIENTUUID";
2294 #endif /* NECP */
2295 		}
2296 
2297 		break;
2298 	case IPPROTO_IP:
2299 		switch (optname) {
2300 		case IP_TOS:
2301 			return "IP_TOS";
2302 		}
2303 
2304 		break;
2305 	case IPPROTO_IPV6:
2306 		switch (optname) {
2307 		case IPV6_TCLASS:
2308 			return "IPV6_TCLASS";
2309 		}
2310 
2311 		break;
2312 	case IPPROTO_TCP:
2313 		switch (optname) {
2314 		case TCP_NODELAY:
2315 			return "TCP_NODELAY";
2316 		case TCP_KEEPALIVE:
2317 			return "TCP_KEEPALIVE";
2318 		case TCP_KEEPINTVL:
2319 			return "TCP_KEEPINTVL";
2320 		case TCP_KEEPCNT:
2321 			return "TCP_KEEPCNT";
2322 		case TCP_CONNECTIONTIMEOUT:
2323 			return "TCP_CONNECTIONTIMEOUT";
2324 		case TCP_RXT_CONNDROPTIME:
2325 			return "TCP_RXT_CONNDROPTIME";
2326 		case PERSIST_TIMEOUT:
2327 			return "PERSIST_TIMEOUT";
2328 		case TCP_NOTSENT_LOWAT:
2329 			return "NOTSENT_LOWAT";
2330 		case TCP_ADAPTIVE_READ_TIMEOUT:
2331 			return "ADAPTIVE_READ_TIMEOUT";
2332 		case TCP_ADAPTIVE_WRITE_TIMEOUT:
2333 			return "ADAPTIVE_WRITE_TIMEOUT";
2334 		case TCP_FASTOPEN_FORCE_ENABLE:
2335 			return "TCP_FASTOPEN_FORCE_ENABLE";
2336 		case MPTCP_SERVICE_TYPE:
2337 			return "MPTCP_SERVICE_TYPE";
2338 		case MPTCP_ALTERNATE_PORT:
2339 			return "MPTCP_ALTERNATE_PORT";
2340 		case MPTCP_FORCE_ENABLE:
2341 			return "MPTCP_FORCE_ENABLE";
2342 		case MPTCP_FORCE_VERSION:
2343 			return "MPTCP_FORCE_VERSION";
2344 		case MPTCP_EXPECTED_PROGRESS_TARGET:
2345 			return "MPTCP_EXPECTED_PROGRESS_TARGET";
2346 		}
2347 
2348 		break;
2349 	}
2350 
2351 	return "unknown";
2352 }
2353 
2354 static int
mptcp_usr_preconnect(struct socket * mp_so)2355 mptcp_usr_preconnect(struct socket *mp_so)
2356 {
2357 	struct mptsub *__single mpts = NULL;
2358 	struct mppcb *__single mpp = mpsotomppcb(mp_so);
2359 	struct mptses *__single mpte;
2360 	struct socket *__single so;
2361 	struct tcpcb *__single tp = NULL;
2362 	int error;
2363 
2364 	mpte = mptompte(mpp);
2365 
2366 	mpts = mptcp_get_subflow(mpte, NULL);
2367 	if (mpts == NULL) {
2368 		os_log_error(mptcp_log_handle, "%s - %lx: invalid preconnect ",
2369 		    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte));
2370 		return EINVAL;
2371 	}
2372 	mpts->mpts_flags &= ~MPTSF_TFO_REQD;
2373 	so = mpts->mpts_socket;
2374 	tp = intotcpcb(sotoinpcb(so));
2375 	tp->t_mpflags &= ~TMPF_TFO_REQUEST;
2376 	error = tcp_output(sototcpcb(so));
2377 
2378 	soclearfastopen(mp_so);
2379 
2380 	return error;
2381 }
2382