xref: /xnu-12377.1.9/bsd/netinet/mptcp_usrreq.c (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
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_mstat.ms_total.ts_rxbytes;
537 					mptcp_ci.mptcpci_init_txbytes = inp->inp_mstat.ms_total.ts_txbytes;
538 					initial_info_set = 1;
539 				}
540 
541 				mptcpstats_update(mptcp_ci.mptcpci_itfstats, MPTCP_ITFSTATS_SIZE, 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_mstat.ms_total.ts_txbytes;
706 				tcp_ci.tcpci_tcp_info.tcpi_rxbytes +=
707 				    mptsinp->inp_mstat.ms_total.ts_rxbytes;
708 
709 				tcp_ci.tcpci_tcp_info.tcpi_wifi_txbytes +=
710 				    mptsinp->inp_mstat.ms_wifi_infra.ts_txbytes +
711 				    mptsinp->inp_mstat.ms_wifi_non_infra.ts_txbytes;
712 				tcp_ci.tcpci_tcp_info.tcpi_wifi_rxbytes +=
713 				    mptsinp->inp_mstat.ms_wifi_infra.ts_rxbytes +
714 				    mptsinp->inp_mstat.ms_wifi_non_infra.ts_rxbytes;
715 
716 				tcp_ci.tcpci_tcp_info.tcpi_wired_txbytes +=
717 				    mptsinp->inp_mstat.ms_wired.ts_txbytes;
718 				tcp_ci.tcpci_tcp_info.tcpi_wired_rxbytes +=
719 				    mptsinp->inp_mstat.ms_wired.ts_rxbytes;
720 
721 				tcp_ci.tcpci_tcp_info.tcpi_cell_txbytes +=
722 				    mptsinp->inp_mstat.ms_cellular.ts_txbytes;
723 				tcp_ci.tcpci_tcp_info.tcpi_cell_rxbytes +=
724 				    mptsinp->inp_mstat.ms_cellular.ts_rxbytes;
725 			}
726 		}
727 
728 interface_info:
729 		*aux_type = CIAUX_TCP;
730 		if (*aux_len == 0) {
731 			*aux_len = sizeof(tcp_ci);
732 		} else if (aux_data != USER_ADDR_NULL) {
733 			boolean_t create;
734 
735 			/*
736 			 * Finally, old subflows might have been closed - we
737 			 * want this data as well, so grab it from the interface
738 			 * stats.
739 			 */
740 			create = orig_mpts != NULL;
741 
742 			/*
743 			 * When we found a subflow, we are willing to create a stats-index
744 			 * because we have some data to return. If there isn't a subflow,
745 			 * nor anything in the stats, return EINVAL. Because the
746 			 * ifindex belongs to something that doesn't exist.
747 			 */
748 			index = mptcpstats_get_index_by_ifindex(mpte->mpte_itfstats, MPTCP_ITFSTATS_SIZE, (u_short)(*cid), false);
749 			if (index == -1) {
750 				os_log_error(mptcp_log_handle,
751 				    "%s - %lx: Asking for too many ifindex: %u subcount %u, mpts? %s\n",
752 				    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte),
753 				    *cid, mpte->mpte_numflows,
754 				    orig_mpts ? "yes" : "no");
755 
756 				if (orig_mpts == NULL) {
757 					return EINVAL;
758 				}
759 			} else {
760 				struct mptcp_itf_stats *stats;
761 
762 				stats = &mpte->mpte_itfstats[index];
763 
764 				/* Roll the itf-stats into the tcp_info */
765 				tcp_ci.tcpci_tcp_info.tcpi_last_outif = *cid;
766 				tcp_ci.tcpci_tcp_info.tcpi_txbytes +=
767 				    stats->mpis_txbytes;
768 				tcp_ci.tcpci_tcp_info.tcpi_rxbytes +=
769 				    stats->mpis_rxbytes;
770 
771 				tcp_ci.tcpci_tcp_info.tcpi_wifi_txbytes +=
772 				    stats->mpis_wifi_txbytes;
773 				tcp_ci.tcpci_tcp_info.tcpi_wifi_rxbytes +=
774 				    stats->mpis_wifi_rxbytes;
775 
776 				tcp_ci.tcpci_tcp_info.tcpi_wired_txbytes +=
777 				    stats->mpis_wired_txbytes;
778 				tcp_ci.tcpci_tcp_info.tcpi_wired_rxbytes +=
779 				    stats->mpis_wired_rxbytes;
780 
781 				tcp_ci.tcpci_tcp_info.tcpi_cell_txbytes +=
782 				    stats->mpis_cell_txbytes;
783 				tcp_ci.tcpci_tcp_info.tcpi_cell_rxbytes +=
784 				    stats->mpis_cell_rxbytes;
785 			}
786 
787 			*aux_len = min(*aux_len, sizeof(tcp_ci));
788 			error = copyout(&tcp_ci, aux_data, *aux_len);
789 			if (error != 0) {
790 				return error;
791 			}
792 		}
793 	}
794 
795 	return 0;
796 }
797 
798 /*
799  * User-protocol pru_control callback.
800  */
801 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)802 mptcp_usr_control(struct socket *mp_so, u_long cmd,
803     caddr_t __sized_by(IOCPARM_LEN(cmd)) data,
804     struct ifnet *ifp, struct proc *p)
805 {
806 #pragma unused(ifp, p)
807 	struct mppcb *mpp = mpsotomppcb(mp_so);
808 	struct mptses *mpte;
809 	int error = 0;
810 
811 	if (mpp == NULL || mpp->mpp_state == MPPCB_STATE_DEAD) {
812 		error = EINVAL;
813 		goto out;
814 	}
815 	mpte = mptompte(mpp);
816 
817 	switch (cmd) {
818 	case SIOCGASSOCIDS32: {         /* struct so_aidreq32 */
819 		struct so_aidreq32 aidr;
820 		bcopy(data, &aidr, sizeof(aidr));
821 		error = mptcp_getassocids(mpte, &aidr.sar_cnt,
822 		    aidr.sar_aidp);
823 		if (error == 0) {
824 			bcopy(&aidr, data, sizeof(aidr));
825 		}
826 		break;
827 	}
828 
829 	case SIOCGASSOCIDS64: {         /* struct so_aidreq64 */
830 		struct so_aidreq64 aidr;
831 		bcopy(data, &aidr, sizeof(aidr));
832 		error = mptcp_getassocids(mpte, &aidr.sar_cnt,
833 		    (user_addr_t)aidr.sar_aidp);
834 		if (error == 0) {
835 			bcopy(&aidr, data, sizeof(aidr));
836 		}
837 		break;
838 	}
839 
840 	case SIOCGCONNIDS32: {          /* struct so_cidreq32 */
841 		struct so_cidreq32 cidr;
842 		bcopy(data, &cidr, sizeof(cidr));
843 		error = mptcp_getconnids(mpte, cidr.scr_aid, &cidr.scr_cnt,
844 		    cidr.scr_cidp);
845 		if (error == 0) {
846 			bcopy(&cidr, data, sizeof(cidr));
847 		}
848 		break;
849 	}
850 
851 	case SIOCGCONNIDS64: {          /* struct so_cidreq64 */
852 		struct so_cidreq64 cidr;
853 		bcopy(data, &cidr, sizeof(cidr));
854 		error = mptcp_getconnids(mpte, cidr.scr_aid, &cidr.scr_cnt,
855 		    (user_addr_t)cidr.scr_cidp);
856 		if (error == 0) {
857 			bcopy(&cidr, data, sizeof(cidr));
858 		}
859 		break;
860 	}
861 
862 	case SIOCGCONNINFO32: {         /* struct so_cinforeq32 */
863 		struct so_cinforeq32 cifr;
864 		bcopy(data, &cifr, sizeof(cifr));
865 		error = mptcp_getconninfo(mpte, &cifr.scir_cid,
866 		    &cifr.scir_flags, &cifr.scir_ifindex, &cifr.scir_error,
867 		    cifr.scir_src, &cifr.scir_src_len, cifr.scir_dst,
868 		    &cifr.scir_dst_len, &cifr.scir_aux_type, cifr.scir_aux_data,
869 		    &cifr.scir_aux_len);
870 		if (error == 0) {
871 			bcopy(&cifr, data, sizeof(cifr));
872 		}
873 		break;
874 	}
875 
876 	case SIOCGCONNINFO64: {         /* struct so_cinforeq64 */
877 		struct so_cinforeq64 cifr;
878 		bcopy(data, &cifr, sizeof(cifr));
879 		error = mptcp_getconninfo(mpte, &cifr.scir_cid,
880 		    &cifr.scir_flags, &cifr.scir_ifindex, &cifr.scir_error,
881 		    (user_addr_t)cifr.scir_src, &cifr.scir_src_len,
882 		    (user_addr_t)cifr.scir_dst, &cifr.scir_dst_len,
883 		    &cifr.scir_aux_type, (user_addr_t)cifr.scir_aux_data,
884 		    &cifr.scir_aux_len);
885 		if (error == 0) {
886 			bcopy(&cifr, data, sizeof(cifr));
887 		}
888 		break;
889 	}
890 
891 	default:
892 		error = EOPNOTSUPP;
893 		break;
894 	}
895 out:
896 	return error;
897 }
898 
899 static int
mptcp_disconnect(struct mptses * mpte)900 mptcp_disconnect(struct mptses *mpte)
901 {
902 	struct socket *mp_so;
903 	struct mptcb *mp_tp;
904 	int error = 0;
905 
906 	mp_so = mptetoso(mpte);
907 	mp_tp = mpte->mpte_mptcb;
908 
909 	/* if we're not detached, go thru socket state checks */
910 	if (!(mp_so->so_flags & SOF_PCBCLEARING) && !(mp_so->so_flags & SOF_DEFUNCT)) {
911 		if (!(mp_so->so_state & (SS_ISCONNECTED |
912 		    SS_ISCONNECTING))) {
913 			error = ENOTCONN;
914 			goto out;
915 		}
916 		if (mp_so->so_state & SS_ISDISCONNECTING) {
917 			error = EALREADY;
918 			goto out;
919 		}
920 	}
921 
922 	mptcp_cancel_all_timers(mp_tp);
923 	if (mp_tp->mpt_state < MPTCPS_ESTABLISHED) {
924 		mptcp_close(mpte, mp_tp);
925 	} else if (((mp_so->so_options & SO_LINGER) &&
926 	    mp_so->so_linger == 0) ||
927 	    (mp_so->so_flags1 & SOF1_DEFUNCTINPROG)) {
928 		mptcp_drop(mpte, mp_tp, 0);
929 	} else {
930 		soisdisconnecting(mp_so);
931 		sbflush(&mp_so->so_rcv);
932 		if (mptcp_usrclosed(mpte) != NULL) {
933 			mptcp_output(mpte);
934 		}
935 	}
936 
937 	if (error == 0) {
938 		mptcp_subflow_workloop(mpte);
939 	}
940 
941 out:
942 	return error;
943 }
944 
945 /*
946  * Wrapper function to support disconnect on socket
947  */
948 static int
mptcp_usr_disconnect(struct socket * mp_so)949 mptcp_usr_disconnect(struct socket *mp_so)
950 {
951 	return mptcp_disconnect(mpsotompte(mp_so));
952 }
953 
954 /*
955  * User-protocol pru_disconnectx callback.
956  */
957 static int
mptcp_usr_disconnectx(struct socket * mp_so,sae_associd_t aid,sae_connid_t cid)958 mptcp_usr_disconnectx(struct socket *mp_so, sae_associd_t aid, sae_connid_t cid)
959 {
960 	if (aid != SAE_ASSOCID_ANY && aid != SAE_ASSOCID_ALL) {
961 		return EINVAL;
962 	}
963 
964 	if (cid != SAE_CONNID_ANY && cid != SAE_CONNID_ALL) {
965 		return EINVAL;
966 	}
967 
968 	return mptcp_usr_disconnect(mp_so);
969 }
970 
971 void
mptcp_finish_usrclosed(struct mptses * mpte)972 mptcp_finish_usrclosed(struct mptses *mpte)
973 {
974 	struct mptcb *mp_tp = mpte->mpte_mptcb;
975 	struct socket *mp_so = mptetoso(mpte);
976 
977 	if (mp_tp->mpt_state == MPTCPS_CLOSED || mp_tp->mpt_state == MPTCPS_TERMINATE) {
978 		mpte = mptcp_close(mpte, mp_tp);
979 	} else if (mp_tp->mpt_state >= MPTCPS_FIN_WAIT_2) {
980 		soisdisconnected(mp_so);
981 	} else {
982 		struct mptsub *mpts;
983 
984 		TAILQ_FOREACH(mpts, &mpte->mpte_subflows, mpts_entry) {
985 			if ((mp_so->so_state & (SS_CANTRCVMORE | SS_CANTSENDMORE)) ==
986 			    (SS_CANTRCVMORE | SS_CANTSENDMORE)) {
987 				mptcp_subflow_disconnect(mpte, mpts);
988 			} else {
989 				mptcp_subflow_shutdown(mpte, mpts);
990 			}
991 		}
992 	}
993 }
994 
995 /*
996  * User issued close, and wish to trail thru shutdown states.
997  */
998 static struct mptses *
mptcp_usrclosed(struct mptses * mpte)999 mptcp_usrclosed(struct mptses *mpte)
1000 {
1001 	struct mptcb *mp_tp = mpte->mpte_mptcb;
1002 
1003 	mptcp_close_fsm(mp_tp, MPCE_CLOSE);
1004 
1005 	/* Not everything has been acknowledged - don't close the subflows! */
1006 	if (mp_tp->mpt_state != MPTCPS_TERMINATE &&
1007 	    mp_tp->mpt_sndnxt + 1 != mp_tp->mpt_sndmax) {
1008 		return mpte;
1009 	}
1010 
1011 	mptcp_finish_usrclosed(mpte);
1012 
1013 	return mpte;
1014 }
1015 
1016 /*
1017  * After a receive, possible send some update to peer.
1018  */
1019 static int
mptcp_usr_rcvd(struct socket * mp_so,int flags)1020 mptcp_usr_rcvd(struct socket *mp_so, int flags)
1021 {
1022 #pragma unused(flags)
1023 	struct mppcb *mpp = mpsotomppcb(mp_so);
1024 	struct mptses *mpte;
1025 	struct mptsub *mpts;
1026 	int error = 0;
1027 
1028 	if (mpp == NULL || mpp->mpp_state == MPPCB_STATE_DEAD) {
1029 		error = EINVAL;
1030 		goto out;
1031 	}
1032 
1033 	mpte = mptompte(mpp);
1034 
1035 	TAILQ_FOREACH(mpts, &mpte->mpte_subflows, mpts_entry) {
1036 		struct socket *so = mpts->mpts_socket;
1037 
1038 		if (so->so_proto->pr_flags & PR_WANTRCVD && so->so_pcb != NULL) {
1039 			(*so->so_proto->pr_usrreqs->pru_rcvd)(so, 0);
1040 		}
1041 	}
1042 
1043 	error = mptcp_output(mpte);
1044 out:
1045 	return error;
1046 }
1047 
1048 /*
1049  * Do a send by putting data in the output queue.
1050  */
1051 static int
mptcp_usr_send(struct socket * mp_so,int prus_flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct proc * p)1052 mptcp_usr_send(struct socket *mp_so, int prus_flags, struct mbuf *m,
1053     struct sockaddr *nam, struct mbuf *control, struct proc *p)
1054 {
1055 #pragma unused(nam, p)
1056 	struct mppcb *mpp = mpsotomppcb(mp_so);
1057 	struct mptses *mpte;
1058 	int error = 0;
1059 
1060 	if (prus_flags & (PRUS_OOB | PRUS_EOF)) {
1061 		error = EOPNOTSUPP;
1062 		goto out;
1063 	}
1064 
1065 	if (nam != NULL) {
1066 		error = EOPNOTSUPP;
1067 		goto out;
1068 	}
1069 
1070 	if (control != NULL && control->m_len != 0) {
1071 		error = EOPNOTSUPP;
1072 		goto out;
1073 	}
1074 
1075 	if (mpp == NULL || mpp->mpp_state == MPPCB_STATE_DEAD) {
1076 		error = ECONNRESET;
1077 		goto out;
1078 	}
1079 	mpte = mptompte(mpp);
1080 	VERIFY(mpte != NULL);
1081 
1082 	if (!(mp_so->so_state & SS_ISCONNECTED) &&
1083 	    !(mp_so->so_flags1 & SOF1_PRECONNECT_DATA)) {
1084 		error = ENOTCONN;
1085 		goto out;
1086 	}
1087 
1088 	mptcp_insert_dsn(mpp, m);
1089 	VERIFY(mp_so->so_snd.sb_flags & SB_NOCOMPRESS);
1090 	sbappendstream(&mp_so->so_snd, m);
1091 	m = NULL;
1092 
1093 	error = mptcp_output(mpte);
1094 	if (error != 0) {
1095 		goto out;
1096 	}
1097 
1098 	if (mp_so->so_state & SS_ISCONNECTING) {
1099 		if (mp_so->so_state & SS_NBIO) {
1100 			error = EWOULDBLOCK;
1101 		} else {
1102 			error = sbwait(&mp_so->so_snd);
1103 		}
1104 	}
1105 
1106 out:
1107 	if (error) {
1108 		if (m != NULL) {
1109 			m_freem(m);
1110 		}
1111 		if (control != NULL) {
1112 			m_freem(control);
1113 		}
1114 	}
1115 	return error;
1116 }
1117 
1118 /*
1119  * Mark the MPTCP connection as being incapable of further output.
1120  */
1121 static int
mptcp_usr_shutdown(struct socket * mp_so)1122 mptcp_usr_shutdown(struct socket *mp_so)
1123 {
1124 	struct mppcb *mpp = mpsotomppcb(mp_so);
1125 	struct mptses *mpte;
1126 	int error = 0;
1127 
1128 	if (mpp == NULL || mpp->mpp_state == MPPCB_STATE_DEAD) {
1129 		error = EINVAL;
1130 		goto out;
1131 	}
1132 	mpte = mptompte(mpp);
1133 	VERIFY(mpte != NULL);
1134 
1135 	socantsendmore(mp_so);
1136 
1137 	mpte = mptcp_usrclosed(mpte);
1138 	if (mpte != NULL) {
1139 		error = mptcp_output(mpte);
1140 	}
1141 out:
1142 	return error;
1143 }
1144 
1145 /*
1146  * Copy the contents of uio into a properly sized mbuf chain.
1147  */
1148 static int
mptcp_uiotombuf(struct uio * uio,int how,user_ssize_t space,struct mbuf ** top)1149 mptcp_uiotombuf(struct uio *uio, int how, user_ssize_t space, struct mbuf **top)
1150 {
1151 	struct mbuf *m, *mb, *nm = NULL, *mtail = NULL;
1152 	int progress, len, error;
1153 	user_ssize_t resid, tot;
1154 
1155 	VERIFY(top != NULL && *top == NULL);
1156 
1157 	/*
1158 	 * space can be zero or an arbitrary large value bound by
1159 	 * the total data supplied by the uio.
1160 	 */
1161 	resid = uio_resid(uio);
1162 	if (space > 0) {
1163 		tot = MIN(resid, space);
1164 	} else {
1165 		tot = resid;
1166 	}
1167 
1168 	if (tot < 0 || tot > INT_MAX) {
1169 		return EINVAL;
1170 	}
1171 
1172 	len = (int)tot;
1173 	if (len == 0) {
1174 		len = 1;
1175 	}
1176 
1177 	/* Loop and append maximum sized mbufs to the chain tail. */
1178 	while (len > 0) {
1179 		uint32_t m_needed = 1;
1180 
1181 		if (len > MBIGCLBYTES) {
1182 			mb = m_getpackets_internal(&m_needed, 1,
1183 			    how, 1, M16KCLBYTES);
1184 		} else if (len > MCLBYTES) {
1185 			mb = m_getpackets_internal(&m_needed, 1,
1186 			    how, 1, MBIGCLBYTES);
1187 		} else if (len >= (signed)MINCLSIZE) {
1188 			mb = m_getpackets_internal(&m_needed, 1,
1189 			    how, 1, MCLBYTES);
1190 		} else {
1191 			mb = m_gethdr(how, MT_DATA);
1192 		}
1193 
1194 		/* Fail the whole operation if one mbuf can't be allocated. */
1195 		if (mb == NULL) {
1196 			if (nm != NULL) {
1197 				m_freem(nm);
1198 			}
1199 			return ENOBUFS;
1200 		}
1201 
1202 		/* Book keeping. */
1203 		VERIFY(mb->m_flags & M_PKTHDR);
1204 		len -= ((mb->m_flags & M_EXT) ? mb->m_ext.ext_size : MHLEN);
1205 		if (mtail != NULL) {
1206 			mtail->m_next = mb;
1207 		} else {
1208 			nm = mb;
1209 		}
1210 		mtail = mb;
1211 	}
1212 
1213 	m = nm;
1214 
1215 	progress = 0;
1216 	/* Fill all mbufs with uio data and update header information. */
1217 	for (mb = m; mb != NULL; mb = mb->m_next) {
1218 		/* tot >= 0 && tot <= INT_MAX (see above) */
1219 		len = MIN((int)M_TRAILINGSPACE(mb), (int)(tot - progress));
1220 
1221 		error = uiomove(mtod(mb, char *), len, uio);
1222 		if (error != 0) {
1223 			m_freem(m);
1224 			return error;
1225 		}
1226 
1227 		/* each mbuf is M_PKTHDR chained via m_next */
1228 		mb->m_len = len;
1229 		mb->m_pkthdr.len = len;
1230 
1231 		progress += len;
1232 	}
1233 	VERIFY(progress == tot);
1234 	*top = m;
1235 	return 0;
1236 }
1237 
1238 /*
1239  * MPTCP socket protocol-user socket send routine, derived from sosend().
1240  */
1241 static int
mptcp_usr_sosend(struct socket * mp_so,struct sockaddr * addr,struct uio * uio,struct mbuf * top,struct mbuf * control,int flags)1242 mptcp_usr_sosend(struct socket *mp_so, struct sockaddr *addr, struct uio *uio,
1243     struct mbuf *top, struct mbuf *control, int flags)
1244 {
1245 #pragma unused(addr)
1246 	user_ssize_t resid, space;
1247 	int error, sendflags;
1248 	struct proc *p = current_proc();
1249 	int sblocked = 0;
1250 
1251 	/* UIO is required for now, due to per-mbuf M_PKTHDR constrains */
1252 	if (uio == NULL || top != NULL) {
1253 		error = EINVAL;
1254 		goto out;
1255 	}
1256 	resid = uio_resid(uio);
1257 
1258 	socket_lock(mp_so, 1);
1259 	so_update_last_owner_locked(mp_so, p);
1260 	so_update_policy(mp_so);
1261 
1262 	VERIFY(mp_so->so_type == SOCK_STREAM);
1263 	VERIFY(!(mp_so->so_flags & SOF_MP_SUBFLOW));
1264 
1265 	if (flags & (MSG_OOB | MSG_DONTROUTE)) {
1266 		error = EOPNOTSUPP;
1267 		socket_unlock(mp_so, 1);
1268 		goto out;
1269 	}
1270 
1271 	/*
1272 	 * In theory resid should be unsigned.  However, space must be
1273 	 * signed, as it might be less than 0 if we over-committed, and we
1274 	 * must use a signed comparison of space and resid.  On the other
1275 	 * hand, a negative resid causes us to loop sending 0-length
1276 	 * segments to the protocol.
1277 	 */
1278 	if (resid < 0 || resid > INT_MAX ||
1279 	    (flags & MSG_EOR) || control != NULL) {
1280 		error = EINVAL;
1281 		socket_unlock(mp_so, 1);
1282 		goto out;
1283 	}
1284 
1285 	OSIncrementAtomicLong(&p->p_stats->p_ru.ru_msgsnd);
1286 
1287 	do {
1288 		error = sosendcheck(mp_so, NULL, resid, 0, 0, flags,
1289 		    &sblocked);
1290 		if (error != 0) {
1291 			goto release;
1292 		}
1293 
1294 		space = sbspace(&mp_so->so_snd);
1295 		do {
1296 			socket_unlock(mp_so, 0);
1297 			/*
1298 			 * Copy the data from userland into an mbuf chain.
1299 			 */
1300 			error = mptcp_uiotombuf(uio, M_WAITOK, space, &top);
1301 			if (error != 0) {
1302 				socket_lock(mp_so, 0);
1303 				goto release;
1304 			}
1305 			VERIFY(top != NULL);
1306 			space -= resid - uio_resid(uio);
1307 			resid = uio_resid(uio);
1308 			socket_lock(mp_so, 0);
1309 
1310 			/*
1311 			 * Compute flags here, for pru_send and NKEs.
1312 			 */
1313 			sendflags = (resid > 0 && space > 0) ?
1314 			    PRUS_MORETOCOME : 0;
1315 
1316 			/*
1317 			 * Socket filter processing
1318 			 */
1319 			VERIFY(control == NULL);
1320 			error = sflt_data_out(mp_so, NULL, &top, &control, 0);
1321 			if (error != 0) {
1322 				if (error == EJUSTRETURN) {
1323 					error = 0;
1324 					top = NULL;
1325 					/* always free control if any */
1326 				}
1327 				goto release;
1328 			}
1329 			if (control != NULL) {
1330 				m_freem(control);
1331 				control = NULL;
1332 			}
1333 
1334 			/*
1335 			 * Pass data to protocol.
1336 			 */
1337 			error = (*mp_so->so_proto->pr_usrreqs->pru_send)
1338 			    (mp_so, sendflags, top, NULL, NULL, p);
1339 
1340 			top = NULL;
1341 			if (error != 0) {
1342 				goto release;
1343 			}
1344 		} while (resid != 0 && space > 0);
1345 	} while (resid != 0);
1346 
1347 release:
1348 	if (sblocked) {
1349 		sbunlock(&mp_so->so_snd, FALSE); /* will unlock socket */
1350 	} else {
1351 		socket_unlock(mp_so, 1);
1352 	}
1353 out:
1354 	if (top != NULL) {
1355 		m_freem(top);
1356 	}
1357 	if (control != NULL) {
1358 		m_freem(control);
1359 	}
1360 
1361 	soclearfastopen(mp_so);
1362 
1363 	return error;
1364 }
1365 
1366 /*
1367  * Called to filter SOPT_{SET,GET} for SOL_SOCKET level socket options.
1368  * This routine simply indicates to the caller whether or not to proceed
1369  * further with the given socket option.  This is invoked by sosetoptlock()
1370  * and sogetoptlock().
1371  */
1372 static int
mptcp_usr_socheckopt(struct socket * mp_so,struct sockopt * sopt)1373 mptcp_usr_socheckopt(struct socket *mp_so, struct sockopt *sopt)
1374 {
1375 #pragma unused(mp_so)
1376 	int error = 0;
1377 
1378 	VERIFY(sopt->sopt_level == SOL_SOCKET);
1379 
1380 	/*
1381 	 * We could check for sopt_dir (set/get) here, but we'll just
1382 	 * let the caller deal with it as appropriate; therefore the
1383 	 * following is a superset of the socket options which we
1384 	 * allow for set/get.
1385 	 *
1386 	 * XXX: [email protected]
1387 	 *
1388 	 * Need to consider the following cases:
1389 	 *
1390 	 *   a.	Certain socket options don't have a clear definition
1391 	 *	on the expected behavior post connect(2).  At the time
1392 	 *	those options are issued on the MP socket, there may
1393 	 *	be existing subflow sockets that are already connected.
1394 	 */
1395 	switch (sopt->sopt_name) {
1396 	case SO_LINGER:                         /* MP */
1397 	case SO_LINGER_SEC:                     /* MP */
1398 	case SO_TYPE:                           /* MP */
1399 	case SO_NREAD:                          /* MP */
1400 	case SO_NWRITE:                         /* MP */
1401 	case SO_ERROR:                          /* MP */
1402 	case SO_SNDBUF:                         /* MP */
1403 	case SO_RCVBUF:                         /* MP */
1404 	case SO_SNDLOWAT:                       /* MP */
1405 	case SO_RCVLOWAT:                       /* MP */
1406 	case SO_SNDTIMEO:                       /* MP */
1407 	case SO_RCVTIMEO:                       /* MP */
1408 	case SO_NKE:                            /* MP */
1409 	case SO_NOSIGPIPE:                      /* MP */
1410 	case SO_NOADDRERR:                      /* MP */
1411 	case SO_LABEL:                          /* MP */
1412 	case SO_PEERLABEL:                      /* MP */
1413 	case SO_DEFUNCTIT:                      /* MP */
1414 	case SO_DEFUNCTOK:                      /* MP */
1415 	case SO_ISDEFUNCT:                      /* MP */
1416 	case SO_TRAFFIC_CLASS_DBG:              /* MP */
1417 	case SO_DELEGATED:                      /* MP */
1418 	case SO_DELEGATED_UUID:                 /* MP */
1419 #if NECP
1420 	case SO_NECP_ATTRIBUTES:
1421 	case SO_NECP_CLIENTUUID:
1422 #endif /* NECP */
1423 	case SO_MPKL_SEND_INFO:
1424 		/*
1425 		 * Tell the caller that these options are to be processed.
1426 		 */
1427 		break;
1428 
1429 	case SO_DEBUG:                          /* MP + subflow */
1430 	case SO_KEEPALIVE:                      /* MP + subflow */
1431 	case SO_USELOOPBACK:                    /* MP + subflow */
1432 	case SO_RANDOMPORT:                     /* MP + subflow */
1433 	case SO_TRAFFIC_CLASS:                  /* MP + subflow */
1434 	case SO_RECV_TRAFFIC_CLASS:             /* MP + subflow */
1435 	case SO_PRIVILEGED_TRAFFIC_CLASS:       /* MP + subflow */
1436 	case SO_RECV_ANYIF:                     /* MP + subflow */
1437 	case SO_RESTRICTIONS:                   /* MP + subflow */
1438 	case SO_FLUSH:                          /* MP + subflow */
1439 	case SO_NOWAKEFROMSLEEP:
1440 	case SO_NOAPNFALLBK:
1441 	case SO_MARK_CELLFALLBACK:
1442 	case SO_MARK_CELLFALLBACK_UUID:
1443 	case SO_MARK_KNOWN_TRACKER:
1444 	case SO_MARK_KNOWN_TRACKER_NON_APP_INITIATED:
1445 	case SO_MARK_APPROVED_APP_DOMAIN:
1446 	case SO_FALLBACK_MODE:
1447 		/*
1448 		 * Tell the caller that these options are to be processed;
1449 		 * these will also be recorded later by mptcp_setopt().
1450 		 *
1451 		 * NOTE: Only support integer option value for now.
1452 		 */
1453 		if (sopt->sopt_valsize != sizeof(int)) {
1454 			error = EINVAL;
1455 		}
1456 		break;
1457 
1458 	default:
1459 		/*
1460 		 * Tell the caller to stop immediately and return an error.
1461 		 */
1462 		error = ENOPROTOOPT;
1463 		break;
1464 	}
1465 
1466 	return error;
1467 }
1468 
1469 /*
1470  * Issue SOPT_SET for all MPTCP subflows (for integer option values.)
1471  */
1472 static int
mptcp_setopt_apply(struct mptses * mpte,struct mptopt * mpo)1473 mptcp_setopt_apply(struct mptses *mpte, struct mptopt *mpo)
1474 {
1475 	struct socket *mp_so;
1476 	struct mptsub *mpts;
1477 	struct mptopt smpo;
1478 	int error = 0;
1479 
1480 	/* just bail now if this isn't applicable to subflow sockets */
1481 	if (!(mpo->mpo_flags & MPOF_SUBFLOW_OK)) {
1482 		error = ENOPROTOOPT;
1483 		goto out;
1484 	}
1485 
1486 	/*
1487 	 * Skip those that are handled internally; these options
1488 	 * should not have been recorded and marked with the
1489 	 * MPOF_SUBFLOW_OK by mptcp_setopt(), but just in case.
1490 	 */
1491 	if (mpo->mpo_level == SOL_SOCKET &&
1492 	    (mpo->mpo_name == SO_NOSIGPIPE || mpo->mpo_name == SO_NOADDRERR)) {
1493 		error = ENOPROTOOPT;
1494 		goto out;
1495 	}
1496 
1497 	mp_so = mptetoso(mpte);
1498 
1499 	/*
1500 	 * Don't bother going further if there's no subflow; mark the option
1501 	 * with MPOF_INTERIM so that we know whether or not to remove this
1502 	 * option upon encountering an error while issuing it during subflow
1503 	 * socket creation.
1504 	 */
1505 	if (mpte->mpte_numflows == 0) {
1506 		VERIFY(TAILQ_EMPTY(&mpte->mpte_subflows));
1507 		mpo->mpo_flags |= MPOF_INTERIM;
1508 		/* return success */
1509 		goto out;
1510 	}
1511 
1512 	bzero(&smpo, sizeof(smpo));
1513 	smpo.mpo_flags |= MPOF_SUBFLOW_OK;
1514 	smpo.mpo_level = mpo->mpo_level;
1515 	smpo.mpo_name = mpo->mpo_name;
1516 
1517 	/* grab exisiting values in case we need to rollback */
1518 	TAILQ_FOREACH(mpts, &mpte->mpte_subflows, mpts_entry) {
1519 		struct socket *so;
1520 
1521 		mpts->mpts_flags &= ~(MPTSF_SOPT_OLDVAL | MPTSF_SOPT_INPROG);
1522 		mpts->mpts_oldintval = 0;
1523 		smpo.mpo_intval = 0;
1524 		VERIFY(mpts->mpts_socket != NULL);
1525 		so = mpts->mpts_socket;
1526 		if (mptcp_subflow_sogetopt(mpte, so, &smpo) == 0) {
1527 			mpts->mpts_flags |= MPTSF_SOPT_OLDVAL;
1528 			mpts->mpts_oldintval = smpo.mpo_intval;
1529 		}
1530 	}
1531 
1532 	/* apply socket option */
1533 	TAILQ_FOREACH(mpts, &mpte->mpte_subflows, mpts_entry) {
1534 		struct socket *so;
1535 
1536 		mpts->mpts_flags |= MPTSF_SOPT_INPROG;
1537 		VERIFY(mpts->mpts_socket != NULL);
1538 		so = mpts->mpts_socket;
1539 		error = mptcp_subflow_sosetopt(mpte, mpts, mpo);
1540 		if (error != 0) {
1541 			break;
1542 		}
1543 	}
1544 
1545 	/* cleanup, and rollback if needed */
1546 	TAILQ_FOREACH(mpts, &mpte->mpte_subflows, mpts_entry) {
1547 		struct socket *so;
1548 
1549 		if (!(mpts->mpts_flags & MPTSF_SOPT_INPROG)) {
1550 			/* clear in case it's set */
1551 			mpts->mpts_flags &= ~MPTSF_SOPT_OLDVAL;
1552 			mpts->mpts_oldintval = 0;
1553 			continue;
1554 		}
1555 		if (!(mpts->mpts_flags & MPTSF_SOPT_OLDVAL)) {
1556 			mpts->mpts_flags &= ~MPTSF_SOPT_INPROG;
1557 			VERIFY(mpts->mpts_oldintval == 0);
1558 			continue;
1559 		}
1560 		/* error during sosetopt, so roll it back */
1561 		if (error != 0) {
1562 			VERIFY(mpts->mpts_socket != NULL);
1563 			so = mpts->mpts_socket;
1564 			smpo.mpo_intval = mpts->mpts_oldintval;
1565 			mptcp_subflow_sosetopt(mpte, mpts, &smpo);
1566 		}
1567 		mpts->mpts_oldintval = 0;
1568 		mpts->mpts_flags &= ~(MPTSF_SOPT_OLDVAL | MPTSF_SOPT_INPROG);
1569 	}
1570 
1571 out:
1572 	return error;
1573 }
1574 
1575 /*
1576  * Handle SOPT_SET for socket options issued on MP socket.
1577  */
1578 static int
mptcp_setopt(struct mptses * mpte,struct sockopt * sopt)1579 mptcp_setopt(struct mptses *mpte, struct sockopt *sopt)
1580 {
1581 	int error = 0, optval = 0, level, optname, rec = 1;
1582 	struct mptopt smpo, *mpo = NULL;
1583 	struct socket *mp_so;
1584 
1585 	level = sopt->sopt_level;
1586 	optname = sopt->sopt_name;
1587 
1588 	mp_so = mptetoso(mpte);
1589 
1590 	VERIFY(!(mpsotomppcb(mp_so)->mpp_flags & MPP_INSIDE_SETGETOPT));
1591 	mpsotomppcb(mp_so)->mpp_flags |= MPP_INSIDE_SETGETOPT;
1592 
1593 	/*
1594 	 * Record socket options which are applicable to subflow sockets so
1595 	 * that we can replay them for new ones; see mptcp_usr_socheckopt()
1596 	 * for the list of eligible socket-level options.
1597 	 */
1598 	if (level == SOL_SOCKET) {
1599 		switch (optname) {
1600 		case SO_DEBUG:
1601 		case SO_KEEPALIVE:
1602 		case SO_USELOOPBACK:
1603 		case SO_RANDOMPORT:
1604 		case SO_TRAFFIC_CLASS:
1605 		case SO_RECV_TRAFFIC_CLASS:
1606 		case SO_PRIVILEGED_TRAFFIC_CLASS:
1607 		case SO_RECV_ANYIF:
1608 		case SO_RESTRICTIONS:
1609 		case SO_NOWAKEFROMSLEEP:
1610 		case SO_NOAPNFALLBK:
1611 		case SO_MARK_CELLFALLBACK:
1612 		case SO_MARK_KNOWN_TRACKER:
1613 		case SO_MARK_KNOWN_TRACKER_NON_APP_INITIATED:
1614 		case SO_MARK_APPROVED_APP_DOMAIN:
1615 		case SO_FALLBACK_MODE:
1616 			/* record it */
1617 			break;
1618 		case SO_FLUSH:
1619 			/* don't record it */
1620 			rec = 0;
1621 			break;
1622 
1623 		/* Next ones, record at MPTCP-level */
1624 		case SO_DELEGATED:
1625 			error = sooptcopyin(sopt, &mpte->mpte_epid,
1626 			    sizeof(int), sizeof(int));
1627 			if (error != 0) {
1628 				goto err_out;
1629 			}
1630 
1631 			goto out;
1632 		case SO_DELEGATED_UUID:
1633 			error = sooptcopyin(sopt, &mpte->mpte_euuid,
1634 			    sizeof(uuid_t), sizeof(uuid_t));
1635 			if (error != 0) {
1636 				goto err_out;
1637 			}
1638 
1639 			goto out;
1640 #if NECP
1641 		case SO_NECP_CLIENTUUID:
1642 			if (!uuid_is_null(mpsotomppcb(mp_so)->necp_client_uuid)) {
1643 				error = EINVAL;
1644 				goto err_out;
1645 			}
1646 
1647 			error = sooptcopyin(sopt, &mpsotomppcb(mp_so)->necp_client_uuid,
1648 			    sizeof(uuid_t), sizeof(uuid_t));
1649 			if (error != 0) {
1650 				goto err_out;
1651 			}
1652 
1653 			mpsotomppcb(mp_so)->necp_cb = mptcp_session_necp_cb;
1654 			error = necp_client_register_multipath_cb(mp_so->last_pid,
1655 			    mpsotomppcb(mp_so)->necp_client_uuid,
1656 			    mpsotomppcb(mp_so));
1657 			if (error) {
1658 				goto err_out;
1659 			}
1660 
1661 			if (uuid_is_null(mpsotomppcb(mp_so)->necp_client_uuid)) {
1662 				error = EINVAL;
1663 				goto err_out;
1664 			}
1665 
1666 			goto out;
1667 		case SO_NECP_ATTRIBUTES:
1668 			error = necp_set_socket_attributes(&mpsotomppcb(mp_so)->inp_necp_attributes, sopt);
1669 			if (error) {
1670 				goto err_out;
1671 			}
1672 
1673 			goto out;
1674 #endif /* NECP */
1675 		default:
1676 			/* nothing to do; just return */
1677 			goto out;
1678 		}
1679 	} else if (sopt->sopt_level == IPPROTO_IP) {
1680 		switch (optname) {
1681 		case IP_TOS:
1682 			/* eligible; record it */
1683 			break;
1684 		default:
1685 			/* not eligible */
1686 			error = ENOPROTOOPT;
1687 			goto err_out;
1688 		}
1689 	} else if (sopt->sopt_level == IPPROTO_IPV6) {
1690 		switch (optname) {
1691 		case IPV6_TCLASS:
1692 			/* eligible; record it */
1693 			break;
1694 		default:
1695 			/* not eligible */
1696 			error = ENOPROTOOPT;
1697 			goto err_out;
1698 		}
1699 	} else {
1700 		switch (optname) {
1701 		case TCP_NODELAY:
1702 		case TCP_RXT_FINDROP:
1703 		case TCP_KEEPALIVE:
1704 		case TCP_KEEPINTVL:
1705 		case TCP_KEEPCNT:
1706 		case TCP_CONNECTIONTIMEOUT:
1707 		case TCP_RXT_CONNDROPTIME:
1708 		case PERSIST_TIMEOUT:
1709 		case TCP_ADAPTIVE_READ_TIMEOUT:
1710 		case TCP_ADAPTIVE_WRITE_TIMEOUT:
1711 		case TCP_FASTOPEN_FORCE_ENABLE:
1712 			/* eligible; record it */
1713 			break;
1714 		case TCP_NOTSENT_LOWAT:
1715 			/* record at MPTCP level */
1716 			error = sooptcopyin(sopt, &optval, sizeof(optval),
1717 			    sizeof(optval));
1718 			if (error) {
1719 				goto err_out;
1720 			}
1721 			if (optval < 0) {
1722 				error = EINVAL;
1723 				goto err_out;
1724 			} else {
1725 				if (optval == 0) {
1726 					mp_so->so_flags &= ~SOF_NOTSENT_LOWAT;
1727 					error = mptcp_set_notsent_lowat(mpte, 0);
1728 				} else {
1729 					mp_so->so_flags |= SOF_NOTSENT_LOWAT;
1730 					error = mptcp_set_notsent_lowat(mpte,
1731 					    optval);
1732 				}
1733 
1734 				if (error) {
1735 					goto err_out;
1736 				}
1737 			}
1738 			goto out;
1739 		case MPTCP_SERVICE_TYPE:
1740 			/* record at MPTCP level */
1741 			error = sooptcopyin(sopt, &optval, sizeof(optval),
1742 			    sizeof(optval));
1743 			if (error) {
1744 				goto err_out;
1745 			}
1746 			if (optval < 0 || optval >= MPTCP_SVCTYPE_MAX) {
1747 				error = EINVAL;
1748 				goto err_out;
1749 			}
1750 
1751 			if (mptcp_entitlement_check(mp_so, (uint8_t)optval) < 0) {
1752 				error = EACCES;
1753 				goto err_out;
1754 			}
1755 
1756 			mpte->mpte_svctype = (uint8_t)optval;
1757 			mpte->mpte_flags |= MPTE_SVCTYPE_CHECKED;
1758 
1759 			goto out;
1760 		case MPTCP_ALTERNATE_PORT:
1761 			/* record at MPTCP level */
1762 			error = sooptcopyin(sopt, &optval, sizeof(optval),
1763 			    sizeof(optval));
1764 			if (error) {
1765 				goto err_out;
1766 			}
1767 
1768 			if (optval < 0 || optval > UINT16_MAX) {
1769 				error = EINVAL;
1770 				goto err_out;
1771 			}
1772 
1773 			mpte->mpte_alternate_port = (uint16_t)optval;
1774 
1775 			goto out;
1776 		case MPTCP_FORCE_ENABLE:
1777 			/* record at MPTCP level */
1778 			error = sooptcopyin(sopt, &optval, sizeof(optval),
1779 			    sizeof(optval));
1780 			if (error) {
1781 				goto err_out;
1782 			}
1783 
1784 			if (optval < 0 || optval > 1) {
1785 				error = EINVAL;
1786 				goto err_out;
1787 			}
1788 
1789 			if (optval) {
1790 				mpte->mpte_flags |= MPTE_FORCE_ENABLE;
1791 			} else {
1792 				mpte->mpte_flags &= ~MPTE_FORCE_ENABLE;
1793 			}
1794 
1795 			goto out;
1796 		case MPTCP_FORCE_VERSION:
1797 			error = sooptcopyin(sopt, &optval, sizeof(optval),
1798 			    sizeof(optval));
1799 			if (error) {
1800 				goto err_out;
1801 			}
1802 
1803 			if (optval != 0 && optval != 1) {
1804 				error = EINVAL;
1805 				goto err_out;
1806 			}
1807 
1808 			if (optval == 0) {
1809 				mpte->mpte_flags |= MPTE_FORCE_V0;
1810 				mpte->mpte_flags &= ~MPTE_FORCE_V1;
1811 			} else {
1812 				mpte->mpte_flags |= MPTE_FORCE_V1;
1813 				mpte->mpte_flags &= ~MPTE_FORCE_V0;
1814 			}
1815 
1816 			goto out;
1817 		case MPTCP_EXPECTED_PROGRESS_TARGET:
1818 		{
1819 			struct mptcb *mp_tp = mpte->mpte_mptcb;
1820 			uint64_t mach_time_target;
1821 			uint64_t nanoseconds;
1822 
1823 			if (mpte->mpte_svctype != MPTCP_SVCTYPE_TARGET_BASED) {
1824 				os_log(mptcp_log_handle, "%s - %lx: Can't set urgent activity when svctype is %u\n",
1825 				    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), mpte->mpte_svctype);
1826 				error = EINVAL;
1827 				goto err_out;
1828 			}
1829 
1830 			error = sooptcopyin(sopt, &mach_time_target, sizeof(mach_time_target), sizeof(mach_time_target));
1831 			if (error) {
1832 				goto err_out;
1833 			}
1834 
1835 			if (!mptcp_ok_to_create_subflows(mp_tp)) {
1836 				os_log(mptcp_log_handle, "%s - %lx: Not ok to create subflows, state %u flags %#x\n",
1837 				    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), mp_tp->mpt_state, mp_tp->mpt_flags);
1838 				error = EINVAL;
1839 				goto err_out;
1840 			}
1841 
1842 			if (mach_time_target) {
1843 				uint64_t time_now = 0;
1844 				uint64_t time_now_nanoseconds;
1845 
1846 				absolutetime_to_nanoseconds(mach_time_target, &nanoseconds);
1847 				nanoseconds = nanoseconds - (mptcp_expected_progress_headstart * NSEC_PER_MSEC);
1848 
1849 				time_now = mach_continuous_time();
1850 				absolutetime_to_nanoseconds(time_now, &time_now_nanoseconds);
1851 
1852 				nanoseconds_to_absolutetime(nanoseconds, &mach_time_target);
1853 				/* If the timer is already running and it would
1854 				 * fire in less than mptcp_expected_progress_headstart
1855 				 * seconds, then it's not worth canceling it.
1856 				 */
1857 				if (mpte->mpte_time_target &&
1858 				    mpte->mpte_time_target < time_now &&
1859 				    time_now_nanoseconds > nanoseconds - (mptcp_expected_progress_headstart * NSEC_PER_MSEC)) {
1860 					os_log(mptcp_log_handle, "%s - %lx: Not rescheduling timer %llu now %llu target %llu\n",
1861 					    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte),
1862 					    mpte->mpte_time_target,
1863 					    time_now,
1864 					    mach_time_target);
1865 					goto out;
1866 				}
1867 			}
1868 
1869 			mpte->mpte_time_target = mach_time_target;
1870 			mptcp_set_urgency_timer(mpte);
1871 
1872 			goto out;
1873 		}
1874 		default:
1875 			/* not eligible */
1876 			error = ENOPROTOOPT;
1877 			goto err_out;
1878 		}
1879 	}
1880 
1881 	if ((error = sooptcopyin(sopt, &optval, sizeof(optval),
1882 	    sizeof(optval))) != 0) {
1883 		goto err_out;
1884 	}
1885 
1886 	if (rec) {
1887 		/* search for an existing one; if not found, allocate */
1888 		if ((mpo = mptcp_sopt_find(mpte, sopt)) == NULL) {
1889 			mpo = mptcp_sopt_alloc();
1890 		}
1891 
1892 		/* initialize or update, as needed */
1893 		mpo->mpo_intval = optval;
1894 		if (!(mpo->mpo_flags & MPOF_ATTACHED)) {
1895 			mpo->mpo_level = level;
1896 			mpo->mpo_name = optname;
1897 			mptcp_sopt_insert(mpte, mpo);
1898 		}
1899 		/* this can be issued on the subflow socket */
1900 		mpo->mpo_flags |= MPOF_SUBFLOW_OK;
1901 	} else {
1902 		bzero(&smpo, sizeof(smpo));
1903 		mpo = &smpo;
1904 		mpo->mpo_flags |= MPOF_SUBFLOW_OK;
1905 		mpo->mpo_level = level;
1906 		mpo->mpo_name = optname;
1907 		mpo->mpo_intval = optval;
1908 	}
1909 
1910 	/* issue this socket option on existing subflows */
1911 	error = mptcp_setopt_apply(mpte, mpo);
1912 	if (error != 0 && (mpo->mpo_flags & MPOF_ATTACHED)) {
1913 		VERIFY(mpo != &smpo);
1914 		mptcp_sopt_remove(mpte, mpo);
1915 		mptcp_sopt_free(mpo);
1916 	}
1917 	if (mpo == &smpo) {
1918 		mpo->mpo_flags &= ~MPOF_INTERIM;
1919 	}
1920 
1921 	if (error) {
1922 		goto err_out;
1923 	}
1924 
1925 out:
1926 
1927 	mpsotomppcb(mp_so)->mpp_flags &= ~MPP_INSIDE_SETGETOPT;
1928 	return 0;
1929 
1930 err_out:
1931 	os_log_error(mptcp_log_handle, "%s - %lx: sopt %s (%d, %d) val %d can't be issued error %d\n",
1932 	    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte),
1933 	    mptcp_sopt2str(level, optname), level, optname, optval, error);
1934 	mpsotomppcb(mp_so)->mpp_flags &= ~MPP_INSIDE_SETGETOPT;
1935 	return error;
1936 }
1937 
1938 static void
mptcp_fill_info_bytestats(struct tcp_info * ti,struct mptses * mpte)1939 mptcp_fill_info_bytestats(struct tcp_info *ti, struct mptses *mpte)
1940 {
1941 	struct mptsub *mpts;
1942 	int i;
1943 
1944 	TAILQ_FOREACH(mpts, &mpte->mpte_subflows, mpts_entry) {
1945 		const struct inpcb *inp = sotoinpcb(mpts->mpts_socket);
1946 
1947 		if (inp == NULL) {
1948 			continue;
1949 		}
1950 
1951 		ti->tcpi_txbytes += inp->inp_mstat.ms_total.ts_txbytes;
1952 		ti->tcpi_rxbytes += inp->inp_mstat.ms_total.ts_rxbytes;
1953 		ti->tcpi_cell_txbytes += inp->inp_mstat.ms_cellular.ts_txbytes;
1954 		ti->tcpi_cell_rxbytes += inp->inp_mstat.ms_cellular.ts_rxbytes;
1955 		ti->tcpi_wifi_txbytes += inp->inp_mstat.ms_wifi_infra.ts_txbytes +
1956 		    inp->inp_mstat.ms_wifi_non_infra.ts_txbytes;
1957 		ti->tcpi_wifi_rxbytes += inp->inp_mstat.ms_wifi_infra.ts_rxbytes +
1958 		    inp->inp_mstat.ms_wifi_non_infra.ts_rxbytes;
1959 		ti->tcpi_wired_txbytes += inp->inp_mstat.ms_wired.ts_txbytes;
1960 		ti->tcpi_wired_rxbytes += inp->inp_mstat.ms_wired.ts_rxbytes;
1961 	}
1962 
1963 	for (i = 0; i < MPTCP_ITFSTATS_SIZE; i++) {
1964 		struct mptcp_itf_stats *stats = &mpte->mpte_itfstats[i];
1965 
1966 		ti->tcpi_txbytes += stats->mpis_txbytes;
1967 		ti->tcpi_rxbytes += stats->mpis_rxbytes;
1968 
1969 		ti->tcpi_wifi_txbytes += stats->mpis_wifi_txbytes;
1970 		ti->tcpi_wifi_rxbytes += stats->mpis_wifi_rxbytes;
1971 
1972 		ti->tcpi_wired_txbytes += stats->mpis_wired_txbytes;
1973 		ti->tcpi_wired_rxbytes += stats->mpis_wired_rxbytes;
1974 
1975 		ti->tcpi_cell_txbytes += stats->mpis_cell_txbytes;
1976 		ti->tcpi_cell_rxbytes += stats->mpis_cell_rxbytes;
1977 	}
1978 }
1979 
1980 static void
mptcp_fill_info(struct mptses * mpte,struct tcp_info * ti)1981 mptcp_fill_info(struct mptses *mpte, struct tcp_info *ti)
1982 {
1983 	struct mptsub *actsub = mpte->mpte_active_sub;
1984 	struct mptcb *mp_tp = mpte->mpte_mptcb;
1985 	struct tcpcb *acttp = NULL;
1986 
1987 	if (actsub) {
1988 		acttp = sototcpcb(actsub->mpts_socket);
1989 	}
1990 
1991 	bzero(ti, sizeof(*ti));
1992 
1993 	ti->tcpi_state = (uint8_t)mp_tp->mpt_state;
1994 	/* tcpi_options */
1995 	/* tcpi_snd_wscale */
1996 	/* tcpi_rcv_wscale */
1997 	/* tcpi_flags */
1998 	if (acttp) {
1999 		ti->tcpi_rto = acttp->t_timer[TCPT_REXMT] ? acttp->t_rxtcur : 0;
2000 	}
2001 
2002 	/* tcpi_snd_mss */
2003 	/* tcpi_rcv_mss */
2004 	if (acttp) {
2005 		ti->tcpi_rttcur = acttp->t_rttcur;
2006 		ti->tcpi_srtt = acttp->t_srtt >> TCP_RTT_SHIFT;
2007 		ti->tcpi_rttvar = acttp->t_rttvar >> TCP_RTTVAR_SHIFT;
2008 		ti->tcpi_rttbest = acttp->t_rttbest >> TCP_RTT_SHIFT;
2009 		ti->tcpi_rcv_srtt = acttp->rcv_srtt >> TCP_RTT_SHIFT;
2010 	}
2011 	/* tcpi_snd_ssthresh */
2012 	/* tcpi_snd_cwnd */
2013 	/* tcpi_rcv_space */
2014 	ti->tcpi_snd_wnd = mp_tp->mpt_sndwnd;
2015 	ti->tcpi_snd_nxt = (uint32_t)mp_tp->mpt_sndnxt;
2016 	ti->tcpi_rcv_nxt = (uint32_t)mp_tp->mpt_rcvnxt;
2017 	if (acttp) {
2018 		ti->tcpi_last_outif = (acttp->t_inpcb->inp_last_outifp == NULL) ? 0 :
2019 		    acttp->t_inpcb->inp_last_outifp->if_index;
2020 	}
2021 
2022 	mptcp_fill_info_bytestats(ti, mpte);
2023 	/* tcpi_txpackets */
2024 
2025 	/* tcpi_txretransmitbytes */
2026 	/* tcpi_txunacked */
2027 	/* tcpi_rxpackets */
2028 
2029 	/* tcpi_rxduplicatebytes */
2030 	/* tcpi_rxoutoforderbytes */
2031 	/* tcpi_snd_bw */
2032 	/* tcpi_synrexmits */
2033 	/* tcpi_unused1 */
2034 	/* tcpi_unused2 */
2035 	/* tcpi_cell_rxpackets */
2036 
2037 	/* tcpi_cell_txpackets */
2038 
2039 	/* tcpi_wifi_rxpackets */
2040 
2041 	/* tcpi_wifi_txpackets */
2042 
2043 	/* tcpi_wired_rxpackets */
2044 	/* tcpi_wired_txpackets */
2045 	/* tcpi_connstatus */
2046 	/* TFO-stuff */
2047 	/* ECN stuff */
2048 	/* tcpi_ecn_recv_ce */
2049 	/* tcpi_ecn_recv_cwr */
2050 	if (acttp) {
2051 		ti->tcpi_rcvoopack = acttp->t_rcvoopack;
2052 	}
2053 	/* tcpi_pawsdrop */
2054 	/* tcpi_sack_recovery_episode */
2055 	/* tcpi_reordered_pkts */
2056 	/* tcpi_dsack_sent */
2057 	/* tcpi_dsack_recvd */
2058 	/* tcpi_flowhash */
2059 	if (acttp) {
2060 		ti->tcpi_txretransmitpackets = acttp->t_stat.rxmitpkts;
2061 	}
2062 }
2063 
2064 /*
2065  * Handle SOPT_GET for socket options issued on MP socket.
2066  */
2067 static int
mptcp_getopt(struct mptses * mpte,struct sockopt * sopt)2068 mptcp_getopt(struct mptses *mpte, struct sockopt *sopt)
2069 {
2070 	int error = 0, optval = 0;
2071 	struct socket *__single mp_so;
2072 
2073 	mp_so = mptetoso(mpte);
2074 
2075 	VERIFY(!(mpsotomppcb(mp_so)->mpp_flags & MPP_INSIDE_SETGETOPT));
2076 	mpsotomppcb(mp_so)->mpp_flags |= MPP_INSIDE_SETGETOPT;
2077 
2078 	/*
2079 	 * We only handle SOPT_GET for TCP level socket options; we should
2080 	 * not get here for socket level options since they are already
2081 	 * handled at the socket layer.
2082 	 */
2083 	if (sopt->sopt_level != IPPROTO_TCP) {
2084 		error = ENOPROTOOPT;
2085 		goto out;
2086 	}
2087 
2088 	switch (sopt->sopt_name) {
2089 	case PERSIST_TIMEOUT:
2090 		/* Only case for which we have a non-zero default */
2091 		optval = tcp_max_persist_timeout;
2092 		OS_FALLTHROUGH;
2093 	case TCP_NODELAY:
2094 	case TCP_RXT_FINDROP:
2095 	case TCP_KEEPALIVE:
2096 	case TCP_KEEPINTVL:
2097 	case TCP_KEEPCNT:
2098 	case TCP_CONNECTIONTIMEOUT:
2099 	case TCP_RXT_CONNDROPTIME:
2100 	case TCP_ADAPTIVE_READ_TIMEOUT:
2101 	case TCP_ADAPTIVE_WRITE_TIMEOUT:
2102 	case TCP_FASTOPEN_FORCE_ENABLE:
2103 	{
2104 		struct mptopt *__single mpo = mptcp_sopt_find(mpte, sopt);
2105 
2106 		if (mpo != NULL) {
2107 			optval = mpo->mpo_intval;
2108 		}
2109 		break;
2110 	}
2111 
2112 	/* The next ones are stored at the MPTCP-level */
2113 	case TCP_NOTSENT_LOWAT:
2114 		if (mptetoso(mpte)->so_flags & SOF_NOTSENT_LOWAT) {
2115 			optval = mptcp_get_notsent_lowat(mpte);
2116 		} else {
2117 			optval = 0;
2118 		}
2119 		break;
2120 	case TCP_INFO:
2121 	{
2122 		struct tcp_info ti;
2123 
2124 		mptcp_fill_info(mpte, &ti);
2125 		error = sooptcopyout(sopt, &ti, sizeof(struct tcp_info));
2126 
2127 		goto out;
2128 	}
2129 	case MPTCP_SERVICE_TYPE:
2130 		optval = mpte->mpte_svctype;
2131 		break;
2132 	case MPTCP_ALTERNATE_PORT:
2133 		optval = mpte->mpte_alternate_port;
2134 		break;
2135 	case MPTCP_FORCE_ENABLE:
2136 		optval = !!(mpte->mpte_flags & MPTE_FORCE_ENABLE);
2137 		break;
2138 	case MPTCP_FORCE_VERSION:
2139 		if (mpte->mpte_flags & MPTE_FORCE_V0) {
2140 			optval = 0;
2141 		} else if (mpte->mpte_flags & MPTE_FORCE_V1) {
2142 			optval = 1;
2143 		} else {
2144 			optval = -1;
2145 		}
2146 		break;
2147 	case MPTCP_EXPECTED_PROGRESS_TARGET:
2148 		error = sooptcopyout(sopt, &mpte->mpte_time_target, sizeof(mpte->mpte_time_target));
2149 
2150 		goto out;
2151 	default:
2152 		/* not eligible */
2153 		error = ENOPROTOOPT;
2154 		break;
2155 	}
2156 
2157 	if (error == 0) {
2158 		error = sooptcopyout(sopt, &optval, sizeof(int));
2159 	}
2160 
2161 out:
2162 	mpsotomppcb(mp_so)->mpp_flags &= ~MPP_INSIDE_SETGETOPT;
2163 	return error;
2164 }
2165 
2166 /*
2167  * MPTCP SOPT_{SET,GET} socket option handler, for options issued on the MP
2168  * socket, at SOL_SOCKET and IPPROTO_TCP levels.  The former is restricted
2169  * to those that are allowed by mptcp_usr_socheckopt().
2170  */
2171 int
mptcp_ctloutput(struct socket * mp_so,struct sockopt * sopt)2172 mptcp_ctloutput(struct socket *mp_so, struct sockopt *sopt)
2173 {
2174 	struct mppcb *__single mpp = mpsotomppcb(mp_so);
2175 	struct mptses *__single mpte;
2176 	int error = 0;
2177 
2178 	if (mpp == NULL || mpp->mpp_state == MPPCB_STATE_DEAD) {
2179 		error = EINVAL;
2180 		goto out;
2181 	}
2182 	mpte = mptompte(mpp);
2183 	socket_lock_assert_owned(mp_so);
2184 
2185 	/* we only handle socket and TCP-level socket options for MPTCP */
2186 	if (sopt->sopt_level != SOL_SOCKET && sopt->sopt_level != IPPROTO_TCP &&
2187 	    sopt->sopt_level != IPPROTO_IP && sopt->sopt_level != IPPROTO_IPV6) {
2188 		error = EINVAL;
2189 		goto out;
2190 	}
2191 
2192 	switch (sopt->sopt_dir) {
2193 	case SOPT_SET:
2194 		error = mptcp_setopt(mpte, sopt);
2195 		break;
2196 
2197 	case SOPT_GET:
2198 		error = mptcp_getopt(mpte, sopt);
2199 		break;
2200 	}
2201 out:
2202 	return error;
2203 }
2204 
2205 const char *
mptcp_sopt2str(int level,int optname)2206 mptcp_sopt2str(int level, int optname)
2207 {
2208 	switch (level) {
2209 	case SOL_SOCKET:
2210 		switch (optname) {
2211 		case SO_LINGER:
2212 			return "SO_LINGER";
2213 		case SO_LINGER_SEC:
2214 			return "SO_LINGER_SEC";
2215 		case SO_DEBUG:
2216 			return "SO_DEBUG";
2217 		case SO_KEEPALIVE:
2218 			return "SO_KEEPALIVE";
2219 		case SO_USELOOPBACK:
2220 			return "SO_USELOOPBACK";
2221 		case SO_TYPE:
2222 			return "SO_TYPE";
2223 		case SO_NREAD:
2224 			return "SO_NREAD";
2225 		case SO_NWRITE:
2226 			return "SO_NWRITE";
2227 		case SO_ERROR:
2228 			return "SO_ERROR";
2229 		case SO_SNDBUF:
2230 			return "SO_SNDBUF";
2231 		case SO_RCVBUF:
2232 			return "SO_RCVBUF";
2233 		case SO_SNDLOWAT:
2234 			return "SO_SNDLOWAT";
2235 		case SO_RCVLOWAT:
2236 			return "SO_RCVLOWAT";
2237 		case SO_SNDTIMEO:
2238 			return "SO_SNDTIMEO";
2239 		case SO_RCVTIMEO:
2240 			return "SO_RCVTIMEO";
2241 		case SO_NKE:
2242 			return "SO_NKE";
2243 		case SO_NOSIGPIPE:
2244 			return "SO_NOSIGPIPE";
2245 		case SO_NOADDRERR:
2246 			return "SO_NOADDRERR";
2247 		case SO_RESTRICTIONS:
2248 			return "SO_RESTRICTIONS";
2249 		case SO_LABEL:
2250 			return "SO_LABEL";
2251 		case SO_PEERLABEL:
2252 			return "SO_PEERLABEL";
2253 		case SO_RANDOMPORT:
2254 			return "SO_RANDOMPORT";
2255 		case SO_TRAFFIC_CLASS:
2256 			return "SO_TRAFFIC_CLASS";
2257 		case SO_RECV_TRAFFIC_CLASS:
2258 			return "SO_RECV_TRAFFIC_CLASS";
2259 		case SO_TRAFFIC_CLASS_DBG:
2260 			return "SO_TRAFFIC_CLASS_DBG";
2261 		case SO_PRIVILEGED_TRAFFIC_CLASS:
2262 			return "SO_PRIVILEGED_TRAFFIC_CLASS";
2263 		case SO_DEFUNCTIT:
2264 			return "SO_DEFUNCTIT";
2265 		case SO_DEFUNCTOK:
2266 			return "SO_DEFUNCTOK";
2267 		case SO_ISDEFUNCT:
2268 			return "SO_ISDEFUNCT";
2269 		case SO_OPPORTUNISTIC:
2270 			return "SO_OPPORTUNISTIC";
2271 		case SO_FLUSH:
2272 			return "SO_FLUSH";
2273 		case SO_RECV_ANYIF:
2274 			return "SO_RECV_ANYIF";
2275 		case SO_NOWAKEFROMSLEEP:
2276 			return "SO_NOWAKEFROMSLEEP";
2277 		case SO_NOAPNFALLBK:
2278 			return "SO_NOAPNFALLBK";
2279 		case SO_MARK_CELLFALLBACK:
2280 			return "SO_CELLFALLBACK";
2281 		case SO_FALLBACK_MODE:
2282 			return "SO_FALLBACK_MODE";
2283 		case SO_MARK_KNOWN_TRACKER:
2284 			return "SO_MARK_KNOWN_TRACKER";
2285 		case SO_MARK_KNOWN_TRACKER_NON_APP_INITIATED:
2286 			return "SO_MARK_KNOWN_TRACKER_NON_APP_INITIATED";
2287 		case SO_MARK_APPROVED_APP_DOMAIN:
2288 			return "SO_MARK_APPROVED_APP_DOMAIN";
2289 		case SO_DELEGATED:
2290 			return "SO_DELEGATED";
2291 		case SO_DELEGATED_UUID:
2292 			return "SO_DELEGATED_UUID";
2293 #if NECP
2294 		case SO_NECP_ATTRIBUTES:
2295 			return "SO_NECP_ATTRIBUTES";
2296 		case SO_NECP_CLIENTUUID:
2297 			return "SO_NECP_CLIENTUUID";
2298 #endif /* NECP */
2299 		}
2300 
2301 		break;
2302 	case IPPROTO_IP:
2303 		switch (optname) {
2304 		case IP_TOS:
2305 			return "IP_TOS";
2306 		}
2307 
2308 		break;
2309 	case IPPROTO_IPV6:
2310 		switch (optname) {
2311 		case IPV6_TCLASS:
2312 			return "IPV6_TCLASS";
2313 		}
2314 
2315 		break;
2316 	case IPPROTO_TCP:
2317 		switch (optname) {
2318 		case TCP_NODELAY:
2319 			return "TCP_NODELAY";
2320 		case TCP_KEEPALIVE:
2321 			return "TCP_KEEPALIVE";
2322 		case TCP_KEEPINTVL:
2323 			return "TCP_KEEPINTVL";
2324 		case TCP_KEEPCNT:
2325 			return "TCP_KEEPCNT";
2326 		case TCP_CONNECTIONTIMEOUT:
2327 			return "TCP_CONNECTIONTIMEOUT";
2328 		case TCP_RXT_CONNDROPTIME:
2329 			return "TCP_RXT_CONNDROPTIME";
2330 		case PERSIST_TIMEOUT:
2331 			return "PERSIST_TIMEOUT";
2332 		case TCP_NOTSENT_LOWAT:
2333 			return "NOTSENT_LOWAT";
2334 		case TCP_ADAPTIVE_READ_TIMEOUT:
2335 			return "ADAPTIVE_READ_TIMEOUT";
2336 		case TCP_ADAPTIVE_WRITE_TIMEOUT:
2337 			return "ADAPTIVE_WRITE_TIMEOUT";
2338 		case TCP_FASTOPEN_FORCE_ENABLE:
2339 			return "TCP_FASTOPEN_FORCE_ENABLE";
2340 		case MPTCP_SERVICE_TYPE:
2341 			return "MPTCP_SERVICE_TYPE";
2342 		case MPTCP_ALTERNATE_PORT:
2343 			return "MPTCP_ALTERNATE_PORT";
2344 		case MPTCP_FORCE_ENABLE:
2345 			return "MPTCP_FORCE_ENABLE";
2346 		case MPTCP_FORCE_VERSION:
2347 			return "MPTCP_FORCE_VERSION";
2348 		case MPTCP_EXPECTED_PROGRESS_TARGET:
2349 			return "MPTCP_EXPECTED_PROGRESS_TARGET";
2350 		}
2351 
2352 		break;
2353 	}
2354 
2355 	return "unknown";
2356 }
2357 
2358 static int
mptcp_usr_preconnect(struct socket * mp_so)2359 mptcp_usr_preconnect(struct socket *mp_so)
2360 {
2361 	struct mptsub *__single mpts = NULL;
2362 	struct mppcb *__single mpp = mpsotomppcb(mp_so);
2363 	struct mptses *__single mpte;
2364 	struct socket *__single so;
2365 	struct tcpcb *__single tp = NULL;
2366 	int error;
2367 
2368 	mpte = mptompte(mpp);
2369 
2370 	mpts = mptcp_get_subflow(mpte, NULL);
2371 	if (mpts == NULL) {
2372 		os_log_error(mptcp_log_handle, "%s - %lx: invalid preconnect ",
2373 		    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte));
2374 		return EINVAL;
2375 	}
2376 	mpts->mpts_flags &= ~MPTSF_TFO_REQD;
2377 	so = mpts->mpts_socket;
2378 	tp = intotcpcb(sotoinpcb(so));
2379 	tp->t_mpflags &= ~TMPF_TFO_REQUEST;
2380 	error = tcp_output(sototcpcb(so));
2381 
2382 	soclearfastopen(mp_so);
2383 
2384 	return error;
2385 }
2386