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