1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2021, Red Hat.
7 #define pr_fmt(fmt) "MPTCP: " fmt
9 #include <linux/kernel.h>
10 #include <linux/module.h>
12 #include <net/protocol.h>
14 #include <net/mptcp.h>
17 #define MIN_INFO_OPTLEN_SIZE 16
19 static struct sock *__mptcp_tcp_fallback(struct mptcp_sock *msk)
21 sock_owned_by_me((const struct sock *)msk);
23 if (likely(!__mptcp_check_fallback(msk)))
29 static u32 sockopt_seq_reset(const struct sock *sk)
33 /* Highbits contain state. Allows to distinguish sockopt_seq
34 * of listener and established:
36 * sockopt(s0) - seq is 1
37 * s1 = accept(s0) - s1 inherits seq 1 if listener sk (s0)
38 * sockopt(s0) - seq increments to 2 on s0
39 * sockopt(s1) // seq increments to 2 on s1 (different option)
40 * new ssk completes join, inherits options from s0 // seq 2
41 * Needs sync from mptcp join logic, but ssk->seq == msk->seq
43 * Set High order bits to sk_state so ssk->seq == msk->seq test
47 return (u32)sk->sk_state << 24u;
50 static void sockopt_seq_inc(struct mptcp_sock *msk)
52 u32 seq = (msk->setsockopt_seq + 1) & 0x00ffffff;
54 msk->setsockopt_seq = sockopt_seq_reset((struct sock *)msk) + seq;
57 static int mptcp_get_int_option(struct mptcp_sock *msk, sockptr_t optval,
58 unsigned int optlen, int *val)
60 if (optlen < sizeof(int))
63 if (copy_from_sockptr(val, optval, sizeof(*val)))
69 static void mptcp_sol_socket_sync_intval(struct mptcp_sock *msk, int optname, int val)
71 struct mptcp_subflow_context *subflow;
72 struct sock *sk = (struct sock *)msk;
77 mptcp_for_each_subflow(msk, subflow) {
78 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
79 bool slow = lock_sock_fast(ssk);
83 sock_valbool_flag(ssk, SOCK_DBG, !!val);
86 if (ssk->sk_prot->keepalive)
87 ssk->sk_prot->keepalive(ssk, !!val);
88 sock_valbool_flag(ssk, SOCK_KEEPOPEN, !!val);
91 ssk->sk_priority = val;
95 ssk->sk_userlocks |= SOCK_SNDBUF_LOCK;
96 WRITE_ONCE(ssk->sk_sndbuf, sk->sk_sndbuf);
100 ssk->sk_userlocks |= SOCK_RCVBUF_LOCK;
101 WRITE_ONCE(ssk->sk_rcvbuf, sk->sk_rcvbuf);
104 if (READ_ONCE(ssk->sk_mark) != sk->sk_mark) {
105 ssk->sk_mark = sk->sk_mark;
109 case SO_INCOMING_CPU:
110 WRITE_ONCE(ssk->sk_incoming_cpu, val);
114 subflow->setsockopt_seq = msk->setsockopt_seq;
115 unlock_sock_fast(ssk, slow);
121 static int mptcp_sol_socket_intval(struct mptcp_sock *msk, int optname, int val)
123 sockptr_t optval = KERNEL_SOCKPTR(&val);
124 struct sock *sk = (struct sock *)msk;
127 ret = sock_setsockopt(sk->sk_socket, SOL_SOCKET, optname,
128 optval, sizeof(val));
132 mptcp_sol_socket_sync_intval(msk, optname, val);
136 static void mptcp_so_incoming_cpu(struct mptcp_sock *msk, int val)
138 struct sock *sk = (struct sock *)msk;
140 WRITE_ONCE(sk->sk_incoming_cpu, val);
142 mptcp_sol_socket_sync_intval(msk, SO_INCOMING_CPU, val);
145 static int mptcp_setsockopt_sol_socket_tstamp(struct mptcp_sock *msk, int optname, int val)
147 sockptr_t optval = KERNEL_SOCKPTR(&val);
148 struct mptcp_subflow_context *subflow;
149 struct sock *sk = (struct sock *)msk;
152 ret = sock_setsockopt(sk->sk_socket, SOL_SOCKET, optname,
153 optval, sizeof(val));
158 mptcp_for_each_subflow(msk, subflow) {
159 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
160 bool slow = lock_sock_fast(ssk);
162 sock_set_timestamp(sk, optname, !!val);
163 unlock_sock_fast(ssk, slow);
170 static int mptcp_setsockopt_sol_socket_int(struct mptcp_sock *msk, int optname,
176 ret = mptcp_get_int_option(msk, optval, optlen, &val);
182 mptcp_sol_socket_sync_intval(msk, optname, val);
191 return mptcp_sol_socket_intval(msk, optname, val);
192 case SO_INCOMING_CPU:
193 mptcp_so_incoming_cpu(msk, val);
195 case SO_TIMESTAMP_OLD:
196 case SO_TIMESTAMP_NEW:
197 case SO_TIMESTAMPNS_OLD:
198 case SO_TIMESTAMPNS_NEW:
199 return mptcp_setsockopt_sol_socket_tstamp(msk, optname, val);
205 static int mptcp_setsockopt_sol_socket_timestamping(struct mptcp_sock *msk,
210 struct mptcp_subflow_context *subflow;
211 struct sock *sk = (struct sock *)msk;
212 struct so_timestamping timestamping;
215 if (optlen == sizeof(timestamping)) {
216 if (copy_from_sockptr(×tamping, optval,
217 sizeof(timestamping)))
219 } else if (optlen == sizeof(int)) {
220 memset(×tamping, 0, sizeof(timestamping));
222 if (copy_from_sockptr(×tamping.flags, optval, sizeof(int)))
228 ret = sock_setsockopt(sk->sk_socket, SOL_SOCKET, optname,
229 KERNEL_SOCKPTR(×tamping),
230 sizeof(timestamping));
236 mptcp_for_each_subflow(msk, subflow) {
237 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
238 bool slow = lock_sock_fast(ssk);
240 sock_set_timestamping(sk, optname, timestamping);
241 unlock_sock_fast(ssk, slow);
249 static int mptcp_setsockopt_sol_socket_linger(struct mptcp_sock *msk, sockptr_t optval,
252 struct mptcp_subflow_context *subflow;
253 struct sock *sk = (struct sock *)msk;
258 if (optlen < sizeof(ling))
261 if (copy_from_sockptr(&ling, optval, sizeof(ling)))
264 kopt = KERNEL_SOCKPTR(&ling);
265 ret = sock_setsockopt(sk->sk_socket, SOL_SOCKET, SO_LINGER, kopt, sizeof(ling));
270 sockopt_seq_inc(msk);
271 mptcp_for_each_subflow(msk, subflow) {
272 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
273 bool slow = lock_sock_fast(ssk);
276 sock_reset_flag(ssk, SOCK_LINGER);
278 ssk->sk_lingertime = sk->sk_lingertime;
279 sock_set_flag(ssk, SOCK_LINGER);
282 subflow->setsockopt_seq = msk->setsockopt_seq;
283 unlock_sock_fast(ssk, slow);
290 static int mptcp_setsockopt_sol_socket(struct mptcp_sock *msk, int optname,
291 sockptr_t optval, unsigned int optlen)
293 struct sock *sk = (struct sock *)msk;
294 struct socket *ssock;
300 case SO_BINDTODEVICE:
301 case SO_BINDTOIFINDEX:
303 ssock = __mptcp_nmpc_socket(msk);
309 ret = sock_setsockopt(ssock, SOL_SOCKET, optname, optval, optlen);
311 if (optname == SO_REUSEPORT)
312 sk->sk_reuseport = ssock->sk->sk_reuseport;
313 else if (optname == SO_REUSEADDR)
314 sk->sk_reuse = ssock->sk->sk_reuse;
315 else if (optname == SO_BINDTODEVICE)
316 sk->sk_bound_dev_if = ssock->sk->sk_bound_dev_if;
317 else if (optname == SO_BINDTOIFINDEX)
318 sk->sk_bound_dev_if = ssock->sk->sk_bound_dev_if;
329 case SO_INCOMING_CPU:
331 case SO_TIMESTAMP_OLD:
332 case SO_TIMESTAMP_NEW:
333 case SO_TIMESTAMPNS_OLD:
334 case SO_TIMESTAMPNS_NEW:
335 return mptcp_setsockopt_sol_socket_int(msk, optname, optval,
337 case SO_TIMESTAMPING_OLD:
338 case SO_TIMESTAMPING_NEW:
339 return mptcp_setsockopt_sol_socket_timestamping(msk, optname,
342 return mptcp_setsockopt_sol_socket_linger(msk, optval, optlen);
344 case SO_RCVTIMEO_OLD:
345 case SO_RCVTIMEO_NEW:
346 case SO_SNDTIMEO_OLD:
347 case SO_SNDTIMEO_NEW:
349 case SO_PREFER_BUSY_POLL:
350 case SO_BUSY_POLL_BUDGET:
351 /* No need to copy: only relevant for msk */
352 return sock_setsockopt(sk->sk_socket, SOL_SOCKET, optname, optval, optlen);
362 case SO_SELECT_ERR_QUEUE:
366 /* SO_OOBINLINE is not supported, let's avoid the related mess
367 * SO_ATTACH_FILTER, SO_ATTACH_BPF, SO_ATTACH_REUSEPORT_CBPF,
368 * SO_DETACH_REUSEPORT_BPF, SO_DETACH_FILTER, SO_LOCK_FILTER,
369 * we must be careful with subflows
371 * SO_ATTACH_REUSEPORT_EBPF is not supported, at it checks
372 * explicitly the sk_protocol field
374 * SO_PEEK_OFF is unsupported, as it is for plain TCP
375 * SO_MAX_PACING_RATE is unsupported, we must be careful with subflows
376 * SO_CNX_ADVICE is currently unsupported, could possibly be relevant,
377 * but likely needs careful design
379 * SO_ZEROCOPY is currently unsupported, TODO in sndmsg
380 * SO_TXTIME is currently unsupported
386 static int mptcp_setsockopt_v6(struct mptcp_sock *msk, int optname,
387 sockptr_t optval, unsigned int optlen)
389 struct sock *sk = (struct sock *)msk;
390 int ret = -EOPNOTSUPP;
391 struct socket *ssock;
395 case IPV6_TRANSPARENT:
398 ssock = __mptcp_nmpc_socket(msk);
404 ret = tcp_setsockopt(ssock->sk, SOL_IPV6, optname, optval, optlen);
410 sockopt_seq_inc(msk);
414 sk->sk_ipv6only = ssock->sk->sk_ipv6only;
416 case IPV6_TRANSPARENT:
417 inet_sk(sk)->transparent = inet_sk(ssock->sk)->transparent;
420 inet_sk(sk)->freebind = inet_sk(ssock->sk)->freebind;
431 static bool mptcp_supported_sockopt(int level, int optname)
433 if (level == SOL_IP) {
435 /* should work fine */
439 /* the following are control cmsg related */
446 case IP_RECVORIGDSTADDR:
448 case IP_RECVFRAGSIZE:
450 /* common stuff that need some love */
453 case IP_BIND_ADDRESS_NO_PORT:
454 case IP_MTU_DISCOVER:
457 /* possibly less common may deserve some love */
460 /* the following is apparently a no-op for plain TCP */
461 case IP_RECVERR_RFC4884:
465 /* IP_OPTIONS is not supported, needs subflow care */
466 /* IP_HDRINCL, IP_NODEFRAG are not supported, RAW specific */
467 /* IP_MULTICAST_TTL, IP_MULTICAST_LOOP, IP_UNICAST_IF,
468 * IP_ADD_MEMBERSHIP, IP_ADD_SOURCE_MEMBERSHIP, IP_DROP_MEMBERSHIP,
469 * IP_DROP_SOURCE_MEMBERSHIP, IP_BLOCK_SOURCE, IP_UNBLOCK_SOURCE,
470 * MCAST_JOIN_GROUP, MCAST_LEAVE_GROUP MCAST_JOIN_SOURCE_GROUP,
471 * MCAST_LEAVE_SOURCE_GROUP, MCAST_BLOCK_SOURCE, MCAST_UNBLOCK_SOURCE,
472 * MCAST_MSFILTER, IP_MULTICAST_ALL are not supported, better not deal
475 /* IP_IPSEC_POLICY, IP_XFRM_POLICY are nut supported, unrelated here */
478 if (level == SOL_IPV6) {
482 /* the following are control cmsg related */
483 case IPV6_RECVPKTINFO:
484 case IPV6_2292PKTINFO:
485 case IPV6_RECVHOPLIMIT:
486 case IPV6_2292HOPLIMIT:
489 case IPV6_RECVHOPOPTS:
490 case IPV6_2292HOPOPTS:
491 case IPV6_RECVDSTOPTS:
492 case IPV6_2292DSTOPTS:
493 case IPV6_RECVTCLASS:
495 case IPV6_RECVPATHMTU:
496 case IPV6_RECVORIGDSTADDR:
497 case IPV6_RECVFRAGSIZE:
499 /* the following ones need some love but are quite common */
501 case IPV6_TRANSPARENT:
504 case IPV6_2292PKTOPTIONS:
505 case IPV6_UNICAST_HOPS:
506 case IPV6_MTU_DISCOVER:
509 case IPV6_FLOWINFO_SEND:
510 case IPV6_FLOWLABEL_MGR:
511 case IPV6_MINHOPCOUNT:
513 case IPV6_AUTOFLOWLABEL:
515 /* the following one is a no-op for plain TCP */
516 case IPV6_RECVERR_RFC4884:
520 /* IPV6_HOPOPTS, IPV6_RTHDRDSTOPTS, IPV6_RTHDR, IPV6_DSTOPTS are
523 /* IPV6_MULTICAST_HOPS, IPV6_MULTICAST_LOOP, IPV6_UNICAST_IF,
524 * IPV6_MULTICAST_IF, IPV6_ADDRFORM,
525 * IPV6_ADD_MEMBERSHIP, IPV6_DROP_MEMBERSHIP, IPV6_JOIN_ANYCAST,
526 * IPV6_LEAVE_ANYCAST, IPV6_MULTICAST_ALL, MCAST_JOIN_GROUP, MCAST_LEAVE_GROUP,
527 * MCAST_JOIN_SOURCE_GROUP, MCAST_LEAVE_SOURCE_GROUP,
528 * MCAST_BLOCK_SOURCE, MCAST_UNBLOCK_SOURCE, MCAST_MSFILTER
529 * are not supported better not deal with mcast
531 /* IPV6_ROUTER_ALERT, IPV6_ROUTER_ALERT_ISOLATE are not supported, since are evil */
533 /* IPV6_IPSEC_POLICY, IPV6_XFRM_POLICY are not supported */
534 /* IPV6_ADDR_PREFERENCES is not supported, we must be careful with subflows */
537 if (level == SOL_TCP) {
539 /* the following are no-op or should work just fine */
540 case TCP_THIN_DUPACK:
541 case TCP_DEFER_ACCEPT:
543 /* the following need some love */
546 case TCP_THIN_LINEAR_TIMEOUTS:
555 case TCP_WINDOW_CLAMP:
557 case TCP_USER_TIMEOUT:
559 case TCP_NOTSENT_LOWAT:
562 case TCP_FASTOPEN_CONNECT:
566 /* TCP_MD5SIG, TCP_MD5SIG_EXT are not supported, MD5 is not compatible with MPTCP */
568 /* TCP_REPAIR, TCP_REPAIR_QUEUE, TCP_QUEUE_SEQ, TCP_REPAIR_OPTIONS,
569 * TCP_REPAIR_WINDOW are not supported, better avoid this mess
571 /* TCP_FASTOPEN_KEY, TCP_FASTOPEN, TCP_FASTOPEN_NO_COOKIE,
572 * are not supported fastopen is currently unsupported
578 static int mptcp_setsockopt_sol_tcp_congestion(struct mptcp_sock *msk, sockptr_t optval,
581 struct mptcp_subflow_context *subflow;
582 struct sock *sk = (struct sock *)msk;
583 char name[TCP_CA_NAME_MAX];
590 ret = strncpy_from_sockptr(name, optval,
591 min_t(long, TCP_CA_NAME_MAX - 1, optlen));
597 cap_net_admin = ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN);
601 sockopt_seq_inc(msk);
602 mptcp_for_each_subflow(msk, subflow) {
603 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
607 err = tcp_set_congestion_control(ssk, name, true, cap_net_admin);
608 if (err < 0 && ret == 0)
610 subflow->setsockopt_seq = msk->setsockopt_seq;
615 strcpy(msk->ca_name, name);
621 static int mptcp_setsockopt_sol_tcp_cork(struct mptcp_sock *msk, sockptr_t optval,
624 struct mptcp_subflow_context *subflow;
625 struct sock *sk = (struct sock *)msk;
628 if (optlen < sizeof(int))
631 if (copy_from_sockptr(&val, optval, sizeof(val)))
635 sockopt_seq_inc(msk);
637 mptcp_for_each_subflow(msk, subflow) {
638 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
641 __tcp_sock_set_cork(ssk, !!val);
645 mptcp_check_and_set_pending(sk);
651 static int mptcp_setsockopt_sol_tcp_nodelay(struct mptcp_sock *msk, sockptr_t optval,
654 struct mptcp_subflow_context *subflow;
655 struct sock *sk = (struct sock *)msk;
658 if (optlen < sizeof(int))
661 if (copy_from_sockptr(&val, optval, sizeof(val)))
665 sockopt_seq_inc(msk);
666 msk->nodelay = !!val;
667 mptcp_for_each_subflow(msk, subflow) {
668 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
671 __tcp_sock_set_nodelay(ssk, !!val);
675 mptcp_check_and_set_pending(sk);
681 static int mptcp_setsockopt_sol_ip_set_transparent(struct mptcp_sock *msk, int optname,
682 sockptr_t optval, unsigned int optlen)
684 struct sock *sk = (struct sock *)msk;
685 struct inet_sock *issk;
686 struct socket *ssock;
689 err = ip_setsockopt(sk, SOL_IP, optname, optval, optlen);
695 ssock = __mptcp_nmpc_socket(msk);
701 issk = inet_sk(ssock->sk);
705 issk->freebind = inet_sk(sk)->freebind;
708 issk->transparent = inet_sk(sk)->transparent;
716 sockopt_seq_inc(msk);
721 static int mptcp_setsockopt_v4_set_tos(struct mptcp_sock *msk, int optname,
722 sockptr_t optval, unsigned int optlen)
724 struct mptcp_subflow_context *subflow;
725 struct sock *sk = (struct sock *)msk;
728 err = ip_setsockopt(sk, SOL_IP, optname, optval, optlen);
734 sockopt_seq_inc(msk);
735 val = inet_sk(sk)->tos;
736 mptcp_for_each_subflow(msk, subflow) {
737 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
739 __ip_sock_set_tos(ssk, val);
746 static int mptcp_setsockopt_v4(struct mptcp_sock *msk, int optname,
747 sockptr_t optval, unsigned int optlen)
752 return mptcp_setsockopt_sol_ip_set_transparent(msk, optname, optval, optlen);
754 return mptcp_setsockopt_v4_set_tos(msk, optname, optval, optlen);
760 static int mptcp_setsockopt_sol_tcp_defer(struct mptcp_sock *msk, sockptr_t optval,
763 struct socket *listener;
765 listener = __mptcp_nmpc_socket(msk);
767 return 0; /* TCP_DEFER_ACCEPT does not fail */
769 return tcp_setsockopt(listener->sk, SOL_TCP, TCP_DEFER_ACCEPT, optval, optlen);
772 static int mptcp_setsockopt_sol_tcp_fastopen_connect(struct mptcp_sock *msk, sockptr_t optval,
777 /* Limit to first subflow */
778 sock = __mptcp_nmpc_socket(msk);
782 return tcp_setsockopt(sock->sk, SOL_TCP, TCP_FASTOPEN_CONNECT, optval, optlen);
785 static int mptcp_setsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
786 sockptr_t optval, unsigned int optlen)
788 struct sock *sk = (void *)msk;
793 ret = mptcp_get_int_option(msk, optval, optlen, &val);
796 if (val < 0 || val > 1)
800 msk->recvmsg_inq = !!val;
806 return mptcp_setsockopt_sol_tcp_congestion(msk, optval, optlen);
808 return mptcp_setsockopt_sol_tcp_cork(msk, optval, optlen);
810 return mptcp_setsockopt_sol_tcp_nodelay(msk, optval, optlen);
811 case TCP_DEFER_ACCEPT:
812 return mptcp_setsockopt_sol_tcp_defer(msk, optval, optlen);
813 case TCP_FASTOPEN_CONNECT:
814 return mptcp_setsockopt_sol_tcp_fastopen_connect(msk, optval, optlen);
820 int mptcp_setsockopt(struct sock *sk, int level, int optname,
821 sockptr_t optval, unsigned int optlen)
823 struct mptcp_sock *msk = mptcp_sk(sk);
826 pr_debug("msk=%p", msk);
828 if (level == SOL_SOCKET)
829 return mptcp_setsockopt_sol_socket(msk, optname, optval, optlen);
831 if (!mptcp_supported_sockopt(level, optname))
834 /* @@ the meaning of setsockopt() when the socket is connected and
835 * there are multiple subflows is not yet defined. It is up to the
836 * MPTCP-level socket to configure the subflows until the subflow
837 * is in TCP fallback, when TCP socket options are passed through
838 * to the one remaining subflow.
841 ssk = __mptcp_tcp_fallback(msk);
844 return tcp_setsockopt(ssk, level, optname, optval, optlen);
847 return mptcp_setsockopt_v4(msk, optname, optval, optlen);
849 if (level == SOL_IPV6)
850 return mptcp_setsockopt_v6(msk, optname, optval, optlen);
852 if (level == SOL_TCP)
853 return mptcp_setsockopt_sol_tcp(msk, optname, optval, optlen);
858 static int mptcp_getsockopt_first_sf_only(struct mptcp_sock *msk, int level, int optname,
859 char __user *optval, int __user *optlen)
861 struct sock *sk = (struct sock *)msk;
862 struct socket *ssock;
869 ret = tcp_getsockopt(ssk, level, optname, optval, optlen);
873 ssock = __mptcp_nmpc_socket(msk);
877 ret = tcp_getsockopt(ssock->sk, level, optname, optval, optlen);
884 void mptcp_diag_fill_info(struct mptcp_sock *msk, struct mptcp_info *info)
889 memset(info, 0, sizeof(*info));
891 info->mptcpi_subflows = READ_ONCE(msk->pm.subflows);
892 info->mptcpi_add_addr_signal = READ_ONCE(msk->pm.add_addr_signaled);
893 info->mptcpi_add_addr_accepted = READ_ONCE(msk->pm.add_addr_accepted);
894 info->mptcpi_local_addr_used = READ_ONCE(msk->pm.local_addr_used);
895 info->mptcpi_subflows_max = mptcp_pm_get_subflows_max(msk);
896 val = mptcp_pm_get_add_addr_signal_max(msk);
897 info->mptcpi_add_addr_signal_max = val;
898 val = mptcp_pm_get_add_addr_accept_max(msk);
899 info->mptcpi_add_addr_accepted_max = val;
900 info->mptcpi_local_addr_max = mptcp_pm_get_local_addr_max(msk);
901 if (test_bit(MPTCP_FALLBACK_DONE, &msk->flags))
902 flags |= MPTCP_INFO_FLAG_FALLBACK;
903 if (READ_ONCE(msk->can_ack))
904 flags |= MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED;
905 info->mptcpi_flags = flags;
906 info->mptcpi_token = READ_ONCE(msk->token);
907 info->mptcpi_write_seq = READ_ONCE(msk->write_seq);
908 info->mptcpi_snd_una = READ_ONCE(msk->snd_una);
909 info->mptcpi_rcv_nxt = READ_ONCE(msk->ack_seq);
910 info->mptcpi_csum_enabled = READ_ONCE(msk->csum_enabled);
912 EXPORT_SYMBOL_GPL(mptcp_diag_fill_info);
914 static int mptcp_getsockopt_info(struct mptcp_sock *msk, char __user *optval, int __user *optlen)
916 struct mptcp_info m_info;
919 if (get_user(len, optlen))
922 len = min_t(unsigned int, len, sizeof(struct mptcp_info));
924 mptcp_diag_fill_info(msk, &m_info);
926 if (put_user(len, optlen))
929 if (copy_to_user(optval, &m_info, len))
935 static int mptcp_put_subflow_data(struct mptcp_subflow_data *sfd,
940 u32 copylen = min_t(u32, sfd->size_subflow_data, sizeof(*sfd));
943 copied += sfd->size_subflow_data;
947 if (put_user(copied, optlen))
950 if (copy_to_user(optval, sfd, copylen))
956 static int mptcp_get_subflow_data(struct mptcp_subflow_data *sfd,
957 char __user *optval, int __user *optlen)
961 if (get_user(len, optlen))
964 /* if mptcp_subflow_data size is changed, need to adjust
965 * this function to deal with programs using old version.
967 BUILD_BUG_ON(sizeof(*sfd) != MIN_INFO_OPTLEN_SIZE);
969 if (len < MIN_INFO_OPTLEN_SIZE)
972 memset(sfd, 0, sizeof(*sfd));
974 copylen = min_t(unsigned int, len, sizeof(*sfd));
975 if (copy_from_user(sfd, optval, copylen))
978 /* size_subflow_data is u32, but len is signed */
979 if (sfd->size_subflow_data > INT_MAX ||
980 sfd->size_user > INT_MAX)
983 if (sfd->size_subflow_data < MIN_INFO_OPTLEN_SIZE ||
984 sfd->size_subflow_data > len)
987 if (sfd->num_subflows || sfd->size_kernel)
990 return len - sfd->size_subflow_data;
993 static int mptcp_getsockopt_tcpinfo(struct mptcp_sock *msk, char __user *optval,
996 struct mptcp_subflow_context *subflow;
997 struct sock *sk = &msk->sk.icsk_inet.sk;
998 unsigned int sfcount = 0, copied = 0;
999 struct mptcp_subflow_data sfd;
1000 char __user *infoptr;
1003 len = mptcp_get_subflow_data(&sfd, optval, optlen);
1007 sfd.size_kernel = sizeof(struct tcp_info);
1008 sfd.size_user = min_t(unsigned int, sfd.size_user,
1009 sizeof(struct tcp_info));
1011 infoptr = optval + sfd.size_subflow_data;
1015 mptcp_for_each_subflow(msk, subflow) {
1016 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1020 if (len && len >= sfd.size_user) {
1021 struct tcp_info info;
1023 tcp_get_info(ssk, &info);
1025 if (copy_to_user(infoptr, &info, sfd.size_user)) {
1030 infoptr += sfd.size_user;
1031 copied += sfd.size_user;
1032 len -= sfd.size_user;
1038 sfd.num_subflows = sfcount;
1040 if (mptcp_put_subflow_data(&sfd, optval, copied, optlen))
1046 static void mptcp_get_sub_addrs(const struct sock *sk, struct mptcp_subflow_addrs *a)
1048 struct inet_sock *inet = inet_sk(sk);
1050 memset(a, 0, sizeof(*a));
1052 if (sk->sk_family == AF_INET) {
1053 a->sin_local.sin_family = AF_INET;
1054 a->sin_local.sin_port = inet->inet_sport;
1055 a->sin_local.sin_addr.s_addr = inet->inet_rcv_saddr;
1057 if (!a->sin_local.sin_addr.s_addr)
1058 a->sin_local.sin_addr.s_addr = inet->inet_saddr;
1060 a->sin_remote.sin_family = AF_INET;
1061 a->sin_remote.sin_port = inet->inet_dport;
1062 a->sin_remote.sin_addr.s_addr = inet->inet_daddr;
1063 #if IS_ENABLED(CONFIG_IPV6)
1064 } else if (sk->sk_family == AF_INET6) {
1065 const struct ipv6_pinfo *np = inet6_sk(sk);
1067 if (WARN_ON_ONCE(!np))
1070 a->sin6_local.sin6_family = AF_INET6;
1071 a->sin6_local.sin6_port = inet->inet_sport;
1073 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
1074 a->sin6_local.sin6_addr = np->saddr;
1076 a->sin6_local.sin6_addr = sk->sk_v6_rcv_saddr;
1078 a->sin6_remote.sin6_family = AF_INET6;
1079 a->sin6_remote.sin6_port = inet->inet_dport;
1080 a->sin6_remote.sin6_addr = sk->sk_v6_daddr;
1085 static int mptcp_getsockopt_subflow_addrs(struct mptcp_sock *msk, char __user *optval,
1088 struct sock *sk = &msk->sk.icsk_inet.sk;
1089 struct mptcp_subflow_context *subflow;
1090 unsigned int sfcount = 0, copied = 0;
1091 struct mptcp_subflow_data sfd;
1092 char __user *addrptr;
1095 len = mptcp_get_subflow_data(&sfd, optval, optlen);
1099 sfd.size_kernel = sizeof(struct mptcp_subflow_addrs);
1100 sfd.size_user = min_t(unsigned int, sfd.size_user,
1101 sizeof(struct mptcp_subflow_addrs));
1103 addrptr = optval + sfd.size_subflow_data;
1107 mptcp_for_each_subflow(msk, subflow) {
1108 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1112 if (len && len >= sfd.size_user) {
1113 struct mptcp_subflow_addrs a;
1115 mptcp_get_sub_addrs(ssk, &a);
1117 if (copy_to_user(addrptr, &a, sfd.size_user)) {
1122 addrptr += sfd.size_user;
1123 copied += sfd.size_user;
1124 len -= sfd.size_user;
1130 sfd.num_subflows = sfcount;
1132 if (mptcp_put_subflow_data(&sfd, optval, copied, optlen))
1138 static int mptcp_put_int_option(struct mptcp_sock *msk, char __user *optval,
1139 int __user *optlen, int val)
1143 if (get_user(len, optlen))
1148 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
1149 unsigned char ucval = (unsigned char)val;
1152 if (put_user(len, optlen))
1154 if (copy_to_user(optval, &ucval, 1))
1157 len = min_t(unsigned int, len, sizeof(int));
1158 if (put_user(len, optlen))
1160 if (copy_to_user(optval, &val, len))
1167 static int mptcp_getsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
1168 char __user *optval, int __user *optlen)
1172 case TCP_CONGESTION:
1175 case TCP_DEFER_ACCEPT:
1176 case TCP_FASTOPEN_CONNECT:
1177 return mptcp_getsockopt_first_sf_only(msk, SOL_TCP, optname,
1180 return mptcp_put_int_option(msk, optval, optlen, msk->recvmsg_inq);
1182 return mptcp_put_int_option(msk, optval, optlen, msk->cork);
1184 return mptcp_put_int_option(msk, optval, optlen, msk->nodelay);
1189 static int mptcp_getsockopt_v4(struct mptcp_sock *msk, int optname,
1190 char __user *optval, int __user *optlen)
1192 struct sock *sk = (void *)msk;
1196 return mptcp_put_int_option(msk, optval, optlen, inet_sk(sk)->tos);
1202 static int mptcp_getsockopt_sol_mptcp(struct mptcp_sock *msk, int optname,
1203 char __user *optval, int __user *optlen)
1207 return mptcp_getsockopt_info(msk, optval, optlen);
1209 return mptcp_getsockopt_tcpinfo(msk, optval, optlen);
1210 case MPTCP_SUBFLOW_ADDRS:
1211 return mptcp_getsockopt_subflow_addrs(msk, optval, optlen);
1217 int mptcp_getsockopt(struct sock *sk, int level, int optname,
1218 char __user *optval, int __user *option)
1220 struct mptcp_sock *msk = mptcp_sk(sk);
1223 pr_debug("msk=%p", msk);
1225 /* @@ the meaning of setsockopt() when the socket is connected and
1226 * there are multiple subflows is not yet defined. It is up to the
1227 * MPTCP-level socket to configure the subflows until the subflow
1228 * is in TCP fallback, when socket options are passed through
1229 * to the one remaining subflow.
1232 ssk = __mptcp_tcp_fallback(msk);
1235 return tcp_getsockopt(ssk, level, optname, optval, option);
1237 if (level == SOL_IP)
1238 return mptcp_getsockopt_v4(msk, optname, optval, option);
1239 if (level == SOL_TCP)
1240 return mptcp_getsockopt_sol_tcp(msk, optname, optval, option);
1241 if (level == SOL_MPTCP)
1242 return mptcp_getsockopt_sol_mptcp(msk, optname, optval, option);
1246 static void sync_socket_options(struct mptcp_sock *msk, struct sock *ssk)
1248 static const unsigned int tx_rx_locks = SOCK_RCVBUF_LOCK | SOCK_SNDBUF_LOCK;
1249 struct sock *sk = (struct sock *)msk;
1251 if (ssk->sk_prot->keepalive) {
1252 if (sock_flag(sk, SOCK_KEEPOPEN))
1253 ssk->sk_prot->keepalive(ssk, 1);
1255 ssk->sk_prot->keepalive(ssk, 0);
1258 ssk->sk_priority = sk->sk_priority;
1259 ssk->sk_bound_dev_if = sk->sk_bound_dev_if;
1260 ssk->sk_incoming_cpu = sk->sk_incoming_cpu;
1261 __ip_sock_set_tos(ssk, inet_sk(sk)->tos);
1263 if (sk->sk_userlocks & tx_rx_locks) {
1264 ssk->sk_userlocks |= sk->sk_userlocks & tx_rx_locks;
1265 if (sk->sk_userlocks & SOCK_SNDBUF_LOCK)
1266 WRITE_ONCE(ssk->sk_sndbuf, sk->sk_sndbuf);
1267 if (sk->sk_userlocks & SOCK_RCVBUF_LOCK)
1268 WRITE_ONCE(ssk->sk_rcvbuf, sk->sk_rcvbuf);
1271 if (sock_flag(sk, SOCK_LINGER)) {
1272 ssk->sk_lingertime = sk->sk_lingertime;
1273 sock_set_flag(ssk, SOCK_LINGER);
1275 sock_reset_flag(ssk, SOCK_LINGER);
1278 if (sk->sk_mark != ssk->sk_mark) {
1279 ssk->sk_mark = sk->sk_mark;
1283 sock_valbool_flag(ssk, SOCK_DBG, sock_flag(sk, SOCK_DBG));
1285 if (inet_csk(sk)->icsk_ca_ops != inet_csk(ssk)->icsk_ca_ops)
1286 tcp_set_congestion_control(ssk, msk->ca_name, false, true);
1287 __tcp_sock_set_cork(ssk, !!msk->cork);
1288 __tcp_sock_set_nodelay(ssk, !!msk->nodelay);
1290 inet_sk(ssk)->transparent = inet_sk(sk)->transparent;
1291 inet_sk(ssk)->freebind = inet_sk(sk)->freebind;
1294 static void __mptcp_sockopt_sync(struct mptcp_sock *msk, struct sock *ssk)
1296 bool slow = lock_sock_fast(ssk);
1298 sync_socket_options(msk, ssk);
1300 unlock_sock_fast(ssk, slow);
1303 void mptcp_sockopt_sync(struct mptcp_sock *msk, struct sock *ssk)
1305 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1307 msk_owned_by_me(msk);
1309 if (READ_ONCE(subflow->setsockopt_seq) != msk->setsockopt_seq) {
1310 __mptcp_sockopt_sync(msk, ssk);
1312 subflow->setsockopt_seq = msk->setsockopt_seq;
1316 void mptcp_sockopt_sync_locked(struct mptcp_sock *msk, struct sock *ssk)
1318 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1320 msk_owned_by_me(msk);
1322 if (READ_ONCE(subflow->setsockopt_seq) != msk->setsockopt_seq) {
1323 sync_socket_options(msk, ssk);
1325 subflow->setsockopt_seq = msk->setsockopt_seq;