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