xref: /openbmc/linux/net/mptcp/sockopt.c (revision 500e1340)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Multipath TCP
3  *
4  * Copyright (c) 2021, Red Hat.
5  */
6 
7 #define pr_fmt(fmt) "MPTCP: " fmt
8 
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <net/sock.h>
12 #include <net/protocol.h>
13 #include <net/tcp.h>
14 #include <net/mptcp.h>
15 #include "protocol.h"
16 
17 #define MIN_INFO_OPTLEN_SIZE	16
18 
19 static struct sock *__mptcp_tcp_fallback(struct mptcp_sock *msk)
20 {
21 	msk_owned_by_me(msk);
22 
23 	if (likely(!__mptcp_check_fallback(msk)))
24 		return NULL;
25 
26 	return msk->first;
27 }
28 
29 static u32 sockopt_seq_reset(const struct sock *sk)
30 {
31 	sock_owned_by_me(sk);
32 
33 	/* Highbits contain state.  Allows to distinguish sockopt_seq
34 	 * of listener and established:
35 	 * s0 = new_listener()
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
42 	 *
43 	 * Set High order bits to sk_state so ssk->seq == msk->seq test
44 	 * will fail.
45 	 */
46 
47 	return (u32)sk->sk_state << 24u;
48 }
49 
50 static void sockopt_seq_inc(struct mptcp_sock *msk)
51 {
52 	u32 seq = (msk->setsockopt_seq + 1) & 0x00ffffff;
53 
54 	msk->setsockopt_seq = sockopt_seq_reset((struct sock *)msk) + seq;
55 }
56 
57 static int mptcp_get_int_option(struct mptcp_sock *msk, sockptr_t optval,
58 				unsigned int optlen, int *val)
59 {
60 	if (optlen < sizeof(int))
61 		return -EINVAL;
62 
63 	if (copy_from_sockptr(val, optval, sizeof(*val)))
64 		return -EFAULT;
65 
66 	return 0;
67 }
68 
69 static void mptcp_sol_socket_sync_intval(struct mptcp_sock *msk, int optname, int val)
70 {
71 	struct mptcp_subflow_context *subflow;
72 	struct sock *sk = (struct sock *)msk;
73 
74 	lock_sock(sk);
75 	sockopt_seq_inc(msk);
76 
77 	mptcp_for_each_subflow(msk, subflow) {
78 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
79 		bool slow = lock_sock_fast(ssk);
80 
81 		switch (optname) {
82 		case SO_DEBUG:
83 			sock_valbool_flag(ssk, SOCK_DBG, !!val);
84 			break;
85 		case SO_KEEPALIVE:
86 			if (ssk->sk_prot->keepalive)
87 				ssk->sk_prot->keepalive(ssk, !!val);
88 			sock_valbool_flag(ssk, SOCK_KEEPOPEN, !!val);
89 			break;
90 		case SO_PRIORITY:
91 			ssk->sk_priority = val;
92 			break;
93 		case SO_SNDBUF:
94 		case SO_SNDBUFFORCE:
95 			ssk->sk_userlocks |= SOCK_SNDBUF_LOCK;
96 			WRITE_ONCE(ssk->sk_sndbuf, sk->sk_sndbuf);
97 			break;
98 		case SO_RCVBUF:
99 		case SO_RCVBUFFORCE:
100 			ssk->sk_userlocks |= SOCK_RCVBUF_LOCK;
101 			WRITE_ONCE(ssk->sk_rcvbuf, sk->sk_rcvbuf);
102 			break;
103 		case SO_MARK:
104 			if (READ_ONCE(ssk->sk_mark) != sk->sk_mark) {
105 				ssk->sk_mark = sk->sk_mark;
106 				sk_dst_reset(ssk);
107 			}
108 			break;
109 		case SO_INCOMING_CPU:
110 			WRITE_ONCE(ssk->sk_incoming_cpu, val);
111 			break;
112 		}
113 
114 		subflow->setsockopt_seq = msk->setsockopt_seq;
115 		unlock_sock_fast(ssk, slow);
116 	}
117 
118 	release_sock(sk);
119 }
120 
121 static int mptcp_sol_socket_intval(struct mptcp_sock *msk, int optname, int val)
122 {
123 	sockptr_t optval = KERNEL_SOCKPTR(&val);
124 	struct sock *sk = (struct sock *)msk;
125 	int ret;
126 
127 	ret = sock_setsockopt(sk->sk_socket, SOL_SOCKET, optname,
128 			      optval, sizeof(val));
129 	if (ret)
130 		return ret;
131 
132 	mptcp_sol_socket_sync_intval(msk, optname, val);
133 	return 0;
134 }
135 
136 static void mptcp_so_incoming_cpu(struct mptcp_sock *msk, int val)
137 {
138 	struct sock *sk = (struct sock *)msk;
139 
140 	WRITE_ONCE(sk->sk_incoming_cpu, val);
141 
142 	mptcp_sol_socket_sync_intval(msk, SO_INCOMING_CPU, val);
143 }
144 
145 static int mptcp_setsockopt_sol_socket_tstamp(struct mptcp_sock *msk, int optname, int val)
146 {
147 	sockptr_t optval = KERNEL_SOCKPTR(&val);
148 	struct mptcp_subflow_context *subflow;
149 	struct sock *sk = (struct sock *)msk;
150 	int ret;
151 
152 	ret = sock_setsockopt(sk->sk_socket, SOL_SOCKET, optname,
153 			      optval, sizeof(val));
154 	if (ret)
155 		return ret;
156 
157 	lock_sock(sk);
158 	mptcp_for_each_subflow(msk, subflow) {
159 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
160 		bool slow = lock_sock_fast(ssk);
161 
162 		sock_set_timestamp(sk, optname, !!val);
163 		unlock_sock_fast(ssk, slow);
164 	}
165 
166 	release_sock(sk);
167 	return 0;
168 }
169 
170 static int mptcp_setsockopt_sol_socket_int(struct mptcp_sock *msk, int optname,
171 					   sockptr_t optval,
172 					   unsigned int optlen)
173 {
174 	int val, ret;
175 
176 	ret = mptcp_get_int_option(msk, optval, optlen, &val);
177 	if (ret)
178 		return ret;
179 
180 	switch (optname) {
181 	case SO_KEEPALIVE:
182 		mptcp_sol_socket_sync_intval(msk, optname, val);
183 		return 0;
184 	case SO_DEBUG:
185 	case SO_MARK:
186 	case SO_PRIORITY:
187 	case SO_SNDBUF:
188 	case SO_SNDBUFFORCE:
189 	case SO_RCVBUF:
190 	case SO_RCVBUFFORCE:
191 		return mptcp_sol_socket_intval(msk, optname, val);
192 	case SO_INCOMING_CPU:
193 		mptcp_so_incoming_cpu(msk, val);
194 		return 0;
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);
200 	}
201 
202 	return -ENOPROTOOPT;
203 }
204 
205 static int mptcp_setsockopt_sol_socket_timestamping(struct mptcp_sock *msk,
206 						    int optname,
207 						    sockptr_t optval,
208 						    unsigned int optlen)
209 {
210 	struct mptcp_subflow_context *subflow;
211 	struct sock *sk = (struct sock *)msk;
212 	struct so_timestamping timestamping;
213 	int ret;
214 
215 	if (optlen == sizeof(timestamping)) {
216 		if (copy_from_sockptr(&timestamping, optval,
217 				      sizeof(timestamping)))
218 			return -EFAULT;
219 	} else if (optlen == sizeof(int)) {
220 		memset(&timestamping, 0, sizeof(timestamping));
221 
222 		if (copy_from_sockptr(&timestamping.flags, optval, sizeof(int)))
223 			return -EFAULT;
224 	} else {
225 		return -EINVAL;
226 	}
227 
228 	ret = sock_setsockopt(sk->sk_socket, SOL_SOCKET, optname,
229 			      KERNEL_SOCKPTR(&timestamping),
230 			      sizeof(timestamping));
231 	if (ret)
232 		return ret;
233 
234 	lock_sock(sk);
235 
236 	mptcp_for_each_subflow(msk, subflow) {
237 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
238 		bool slow = lock_sock_fast(ssk);
239 
240 		sock_set_timestamping(sk, optname, timestamping);
241 		unlock_sock_fast(ssk, slow);
242 	}
243 
244 	release_sock(sk);
245 
246 	return 0;
247 }
248 
249 static int mptcp_setsockopt_sol_socket_linger(struct mptcp_sock *msk, sockptr_t optval,
250 					      unsigned int optlen)
251 {
252 	struct mptcp_subflow_context *subflow;
253 	struct sock *sk = (struct sock *)msk;
254 	struct linger ling;
255 	sockptr_t kopt;
256 	int ret;
257 
258 	if (optlen < sizeof(ling))
259 		return -EINVAL;
260 
261 	if (copy_from_sockptr(&ling, optval, sizeof(ling)))
262 		return -EFAULT;
263 
264 	kopt = KERNEL_SOCKPTR(&ling);
265 	ret = sock_setsockopt(sk->sk_socket, SOL_SOCKET, SO_LINGER, kopt, sizeof(ling));
266 	if (ret)
267 		return ret;
268 
269 	lock_sock(sk);
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);
274 
275 		if (!ling.l_onoff) {
276 			sock_reset_flag(ssk, SOCK_LINGER);
277 		} else {
278 			ssk->sk_lingertime = sk->sk_lingertime;
279 			sock_set_flag(ssk, SOCK_LINGER);
280 		}
281 
282 		subflow->setsockopt_seq = msk->setsockopt_seq;
283 		unlock_sock_fast(ssk, slow);
284 	}
285 
286 	release_sock(sk);
287 	return 0;
288 }
289 
290 static int mptcp_setsockopt_sol_socket(struct mptcp_sock *msk, int optname,
291 				       sockptr_t optval, unsigned int optlen)
292 {
293 	struct sock *sk = (struct sock *)msk;
294 	struct socket *ssock;
295 	int ret;
296 
297 	switch (optname) {
298 	case SO_REUSEPORT:
299 	case SO_REUSEADDR:
300 	case SO_BINDTODEVICE:
301 	case SO_BINDTOIFINDEX:
302 		lock_sock(sk);
303 		ssock = __mptcp_nmpc_socket(msk);
304 		if (IS_ERR(ssock)) {
305 			release_sock(sk);
306 			return PTR_ERR(ssock);
307 		}
308 
309 		ret = sock_setsockopt(ssock, SOL_SOCKET, optname, optval, optlen);
310 		if (ret == 0) {
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;
319 		}
320 		release_sock(sk);
321 		return ret;
322 	case SO_KEEPALIVE:
323 	case SO_PRIORITY:
324 	case SO_SNDBUF:
325 	case SO_SNDBUFFORCE:
326 	case SO_RCVBUF:
327 	case SO_RCVBUFFORCE:
328 	case SO_MARK:
329 	case SO_INCOMING_CPU:
330 	case SO_DEBUG:
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,
336 						       optlen);
337 	case SO_TIMESTAMPING_OLD:
338 	case SO_TIMESTAMPING_NEW:
339 		return mptcp_setsockopt_sol_socket_timestamping(msk, optname,
340 								optval, optlen);
341 	case SO_LINGER:
342 		return mptcp_setsockopt_sol_socket_linger(msk, optval, optlen);
343 	case SO_RCVLOWAT:
344 	case SO_RCVTIMEO_OLD:
345 	case SO_RCVTIMEO_NEW:
346 	case SO_SNDTIMEO_OLD:
347 	case SO_SNDTIMEO_NEW:
348 	case SO_BUSY_POLL:
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);
353 	case SO_NO_CHECK:
354 	case SO_DONTROUTE:
355 	case SO_BROADCAST:
356 	case SO_BSDCOMPAT:
357 	case SO_PASSCRED:
358 	case SO_PASSPIDFD:
359 	case SO_PASSSEC:
360 	case SO_RXQ_OVFL:
361 	case SO_WIFI_STATUS:
362 	case SO_NOFCS:
363 	case SO_SELECT_ERR_QUEUE:
364 		return 0;
365 	}
366 
367 	/* SO_OOBINLINE is not supported, let's avoid the related mess
368 	 * SO_ATTACH_FILTER, SO_ATTACH_BPF, SO_ATTACH_REUSEPORT_CBPF,
369 	 * SO_DETACH_REUSEPORT_BPF, SO_DETACH_FILTER, SO_LOCK_FILTER,
370 	 * we must be careful with subflows
371 	 *
372 	 * SO_ATTACH_REUSEPORT_EBPF is not supported, at it checks
373 	 * explicitly the sk_protocol field
374 	 *
375 	 * SO_PEEK_OFF is unsupported, as it is for plain TCP
376 	 * SO_MAX_PACING_RATE is unsupported, we must be careful with subflows
377 	 * SO_CNX_ADVICE is currently unsupported, could possibly be relevant,
378 	 * but likely needs careful design
379 	 *
380 	 * SO_ZEROCOPY is currently unsupported, TODO in sndmsg
381 	 * SO_TXTIME is currently unsupported
382 	 */
383 
384 	return -EOPNOTSUPP;
385 }
386 
387 static int mptcp_setsockopt_v6(struct mptcp_sock *msk, int optname,
388 			       sockptr_t optval, unsigned int optlen)
389 {
390 	struct sock *sk = (struct sock *)msk;
391 	int ret = -EOPNOTSUPP;
392 	struct socket *ssock;
393 
394 	switch (optname) {
395 	case IPV6_V6ONLY:
396 	case IPV6_TRANSPARENT:
397 	case IPV6_FREEBIND:
398 		lock_sock(sk);
399 		ssock = __mptcp_nmpc_socket(msk);
400 		if (IS_ERR(ssock)) {
401 			release_sock(sk);
402 			return PTR_ERR(ssock);
403 		}
404 
405 		ret = tcp_setsockopt(ssock->sk, SOL_IPV6, optname, optval, optlen);
406 		if (ret != 0) {
407 			release_sock(sk);
408 			return ret;
409 		}
410 
411 		sockopt_seq_inc(msk);
412 
413 		switch (optname) {
414 		case IPV6_V6ONLY:
415 			sk->sk_ipv6only = ssock->sk->sk_ipv6only;
416 			break;
417 		case IPV6_TRANSPARENT:
418 			inet_sk(sk)->transparent = inet_sk(ssock->sk)->transparent;
419 			break;
420 		case IPV6_FREEBIND:
421 			inet_sk(sk)->freebind = inet_sk(ssock->sk)->freebind;
422 			break;
423 		}
424 
425 		release_sock(sk);
426 		break;
427 	}
428 
429 	return ret;
430 }
431 
432 static bool mptcp_supported_sockopt(int level, int optname)
433 {
434 	if (level == SOL_IP) {
435 		switch (optname) {
436 		/* should work fine */
437 		case IP_FREEBIND:
438 		case IP_TRANSPARENT:
439 
440 		/* the following are control cmsg related */
441 		case IP_PKTINFO:
442 		case IP_RECVTTL:
443 		case IP_RECVTOS:
444 		case IP_RECVOPTS:
445 		case IP_RETOPTS:
446 		case IP_PASSSEC:
447 		case IP_RECVORIGDSTADDR:
448 		case IP_CHECKSUM:
449 		case IP_RECVFRAGSIZE:
450 
451 		/* common stuff that need some love */
452 		case IP_TOS:
453 		case IP_TTL:
454 		case IP_BIND_ADDRESS_NO_PORT:
455 		case IP_MTU_DISCOVER:
456 		case IP_RECVERR:
457 
458 		/* possibly less common may deserve some love */
459 		case IP_MINTTL:
460 
461 		/* the following is apparently a no-op for plain TCP */
462 		case IP_RECVERR_RFC4884:
463 			return true;
464 		}
465 
466 		/* IP_OPTIONS is not supported, needs subflow care */
467 		/* IP_HDRINCL, IP_NODEFRAG are not supported, RAW specific */
468 		/* IP_MULTICAST_TTL, IP_MULTICAST_LOOP, IP_UNICAST_IF,
469 		 * IP_ADD_MEMBERSHIP, IP_ADD_SOURCE_MEMBERSHIP, IP_DROP_MEMBERSHIP,
470 		 * IP_DROP_SOURCE_MEMBERSHIP, IP_BLOCK_SOURCE, IP_UNBLOCK_SOURCE,
471 		 * MCAST_JOIN_GROUP, MCAST_LEAVE_GROUP MCAST_JOIN_SOURCE_GROUP,
472 		 * MCAST_LEAVE_SOURCE_GROUP, MCAST_BLOCK_SOURCE, MCAST_UNBLOCK_SOURCE,
473 		 * MCAST_MSFILTER, IP_MULTICAST_ALL are not supported, better not deal
474 		 * with mcast stuff
475 		 */
476 		/* IP_IPSEC_POLICY, IP_XFRM_POLICY are nut supported, unrelated here */
477 		return false;
478 	}
479 	if (level == SOL_IPV6) {
480 		switch (optname) {
481 		case IPV6_V6ONLY:
482 
483 		/* the following are control cmsg related */
484 		case IPV6_RECVPKTINFO:
485 		case IPV6_2292PKTINFO:
486 		case IPV6_RECVHOPLIMIT:
487 		case IPV6_2292HOPLIMIT:
488 		case IPV6_RECVRTHDR:
489 		case IPV6_2292RTHDR:
490 		case IPV6_RECVHOPOPTS:
491 		case IPV6_2292HOPOPTS:
492 		case IPV6_RECVDSTOPTS:
493 		case IPV6_2292DSTOPTS:
494 		case IPV6_RECVTCLASS:
495 		case IPV6_FLOWINFO:
496 		case IPV6_RECVPATHMTU:
497 		case IPV6_RECVORIGDSTADDR:
498 		case IPV6_RECVFRAGSIZE:
499 
500 		/* the following ones need some love but are quite common */
501 		case IPV6_TCLASS:
502 		case IPV6_TRANSPARENT:
503 		case IPV6_FREEBIND:
504 		case IPV6_PKTINFO:
505 		case IPV6_2292PKTOPTIONS:
506 		case IPV6_UNICAST_HOPS:
507 		case IPV6_MTU_DISCOVER:
508 		case IPV6_MTU:
509 		case IPV6_RECVERR:
510 		case IPV6_FLOWINFO_SEND:
511 		case IPV6_FLOWLABEL_MGR:
512 		case IPV6_MINHOPCOUNT:
513 		case IPV6_DONTFRAG:
514 		case IPV6_AUTOFLOWLABEL:
515 
516 		/* the following one is a no-op for plain TCP */
517 		case IPV6_RECVERR_RFC4884:
518 			return true;
519 		}
520 
521 		/* IPV6_HOPOPTS, IPV6_RTHDRDSTOPTS, IPV6_RTHDR, IPV6_DSTOPTS are
522 		 * not supported
523 		 */
524 		/* IPV6_MULTICAST_HOPS, IPV6_MULTICAST_LOOP, IPV6_UNICAST_IF,
525 		 * IPV6_MULTICAST_IF, IPV6_ADDRFORM,
526 		 * IPV6_ADD_MEMBERSHIP, IPV6_DROP_MEMBERSHIP, IPV6_JOIN_ANYCAST,
527 		 * IPV6_LEAVE_ANYCAST, IPV6_MULTICAST_ALL, MCAST_JOIN_GROUP, MCAST_LEAVE_GROUP,
528 		 * MCAST_JOIN_SOURCE_GROUP, MCAST_LEAVE_SOURCE_GROUP,
529 		 * MCAST_BLOCK_SOURCE, MCAST_UNBLOCK_SOURCE, MCAST_MSFILTER
530 		 * are not supported better not deal with mcast
531 		 */
532 		/* IPV6_ROUTER_ALERT, IPV6_ROUTER_ALERT_ISOLATE are not supported, since are evil */
533 
534 		/* IPV6_IPSEC_POLICY, IPV6_XFRM_POLICY are not supported */
535 		/* IPV6_ADDR_PREFERENCES is not supported, we must be careful with subflows */
536 		return false;
537 	}
538 	if (level == SOL_TCP) {
539 		switch (optname) {
540 		/* the following are no-op or should work just fine */
541 		case TCP_THIN_DUPACK:
542 		case TCP_DEFER_ACCEPT:
543 
544 		/* the following need some love */
545 		case TCP_MAXSEG:
546 		case TCP_NODELAY:
547 		case TCP_THIN_LINEAR_TIMEOUTS:
548 		case TCP_CONGESTION:
549 		case TCP_CORK:
550 		case TCP_KEEPIDLE:
551 		case TCP_KEEPINTVL:
552 		case TCP_KEEPCNT:
553 		case TCP_SYNCNT:
554 		case TCP_SAVE_SYN:
555 		case TCP_LINGER2:
556 		case TCP_WINDOW_CLAMP:
557 		case TCP_QUICKACK:
558 		case TCP_USER_TIMEOUT:
559 		case TCP_TIMESTAMP:
560 		case TCP_NOTSENT_LOWAT:
561 		case TCP_TX_DELAY:
562 		case TCP_INQ:
563 		case TCP_FASTOPEN:
564 		case TCP_FASTOPEN_CONNECT:
565 		case TCP_FASTOPEN_KEY:
566 		case TCP_FASTOPEN_NO_COOKIE:
567 			return true;
568 		}
569 
570 		/* TCP_MD5SIG, TCP_MD5SIG_EXT are not supported, MD5 is not compatible with MPTCP */
571 
572 		/* TCP_REPAIR, TCP_REPAIR_QUEUE, TCP_QUEUE_SEQ, TCP_REPAIR_OPTIONS,
573 		 * TCP_REPAIR_WINDOW are not supported, better avoid this mess
574 		 */
575 	}
576 	return false;
577 }
578 
579 static int mptcp_setsockopt_sol_tcp_congestion(struct mptcp_sock *msk, sockptr_t optval,
580 					       unsigned int optlen)
581 {
582 	struct mptcp_subflow_context *subflow;
583 	struct sock *sk = (struct sock *)msk;
584 	char name[TCP_CA_NAME_MAX];
585 	bool cap_net_admin;
586 	int ret;
587 
588 	if (optlen < 1)
589 		return -EINVAL;
590 
591 	ret = strncpy_from_sockptr(name, optval,
592 				   min_t(long, TCP_CA_NAME_MAX - 1, optlen));
593 	if (ret < 0)
594 		return -EFAULT;
595 
596 	name[ret] = 0;
597 
598 	cap_net_admin = ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN);
599 
600 	ret = 0;
601 	lock_sock(sk);
602 	sockopt_seq_inc(msk);
603 	mptcp_for_each_subflow(msk, subflow) {
604 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
605 		int err;
606 
607 		lock_sock(ssk);
608 		err = tcp_set_congestion_control(ssk, name, true, cap_net_admin);
609 		if (err < 0 && ret == 0)
610 			ret = err;
611 		subflow->setsockopt_seq = msk->setsockopt_seq;
612 		release_sock(ssk);
613 	}
614 
615 	if (ret == 0)
616 		strcpy(msk->ca_name, name);
617 
618 	release_sock(sk);
619 	return ret;
620 }
621 
622 static int mptcp_setsockopt_sol_tcp_cork(struct mptcp_sock *msk, sockptr_t optval,
623 					 unsigned int optlen)
624 {
625 	struct mptcp_subflow_context *subflow;
626 	struct sock *sk = (struct sock *)msk;
627 	int val;
628 
629 	if (optlen < sizeof(int))
630 		return -EINVAL;
631 
632 	if (copy_from_sockptr(&val, optval, sizeof(val)))
633 		return -EFAULT;
634 
635 	lock_sock(sk);
636 	sockopt_seq_inc(msk);
637 	msk->cork = !!val;
638 	mptcp_for_each_subflow(msk, subflow) {
639 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
640 
641 		lock_sock(ssk);
642 		__tcp_sock_set_cork(ssk, !!val);
643 		release_sock(ssk);
644 	}
645 	if (!val)
646 		mptcp_check_and_set_pending(sk);
647 	release_sock(sk);
648 
649 	return 0;
650 }
651 
652 static int mptcp_setsockopt_sol_tcp_nodelay(struct mptcp_sock *msk, sockptr_t optval,
653 					    unsigned int optlen)
654 {
655 	struct mptcp_subflow_context *subflow;
656 	struct sock *sk = (struct sock *)msk;
657 	int val;
658 
659 	if (optlen < sizeof(int))
660 		return -EINVAL;
661 
662 	if (copy_from_sockptr(&val, optval, sizeof(val)))
663 		return -EFAULT;
664 
665 	lock_sock(sk);
666 	sockopt_seq_inc(msk);
667 	msk->nodelay = !!val;
668 	mptcp_for_each_subflow(msk, subflow) {
669 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
670 
671 		lock_sock(ssk);
672 		__tcp_sock_set_nodelay(ssk, !!val);
673 		release_sock(ssk);
674 	}
675 	if (val)
676 		mptcp_check_and_set_pending(sk);
677 	release_sock(sk);
678 
679 	return 0;
680 }
681 
682 static int mptcp_setsockopt_sol_ip_set_transparent(struct mptcp_sock *msk, int optname,
683 						   sockptr_t optval, unsigned int optlen)
684 {
685 	struct sock *sk = (struct sock *)msk;
686 	struct inet_sock *issk;
687 	struct socket *ssock;
688 	int err;
689 
690 	err = ip_setsockopt(sk, SOL_IP, optname, optval, optlen);
691 	if (err != 0)
692 		return err;
693 
694 	lock_sock(sk);
695 
696 	ssock = __mptcp_nmpc_socket(msk);
697 	if (IS_ERR(ssock)) {
698 		release_sock(sk);
699 		return PTR_ERR(ssock);
700 	}
701 
702 	issk = inet_sk(ssock->sk);
703 
704 	switch (optname) {
705 	case IP_FREEBIND:
706 		issk->freebind = inet_sk(sk)->freebind;
707 		break;
708 	case IP_TRANSPARENT:
709 		issk->transparent = inet_sk(sk)->transparent;
710 		break;
711 	default:
712 		release_sock(sk);
713 		WARN_ON_ONCE(1);
714 		return -EOPNOTSUPP;
715 	}
716 
717 	sockopt_seq_inc(msk);
718 	release_sock(sk);
719 	return 0;
720 }
721 
722 static int mptcp_setsockopt_v4_set_tos(struct mptcp_sock *msk, int optname,
723 				       sockptr_t optval, unsigned int optlen)
724 {
725 	struct mptcp_subflow_context *subflow;
726 	struct sock *sk = (struct sock *)msk;
727 	int err, val;
728 
729 	err = ip_setsockopt(sk, SOL_IP, optname, optval, optlen);
730 
731 	if (err != 0)
732 		return err;
733 
734 	lock_sock(sk);
735 	sockopt_seq_inc(msk);
736 	val = inet_sk(sk)->tos;
737 	mptcp_for_each_subflow(msk, subflow) {
738 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
739 
740 		__ip_sock_set_tos(ssk, val);
741 	}
742 	release_sock(sk);
743 
744 	return 0;
745 }
746 
747 static int mptcp_setsockopt_v4(struct mptcp_sock *msk, int optname,
748 			       sockptr_t optval, unsigned int optlen)
749 {
750 	switch (optname) {
751 	case IP_FREEBIND:
752 	case IP_TRANSPARENT:
753 		return mptcp_setsockopt_sol_ip_set_transparent(msk, optname, optval, optlen);
754 	case IP_TOS:
755 		return mptcp_setsockopt_v4_set_tos(msk, optname, optval, optlen);
756 	}
757 
758 	return -EOPNOTSUPP;
759 }
760 
761 static int mptcp_setsockopt_first_sf_only(struct mptcp_sock *msk, int level, int optname,
762 					  sockptr_t optval, unsigned int optlen)
763 {
764 	struct sock *sk = (struct sock *)msk;
765 	struct socket *sock;
766 	int ret;
767 
768 	/* Limit to first subflow, before the connection establishment */
769 	lock_sock(sk);
770 	sock = __mptcp_nmpc_socket(msk);
771 	if (IS_ERR(sock)) {
772 		ret = PTR_ERR(sock);
773 		goto unlock;
774 	}
775 
776 	ret = tcp_setsockopt(sock->sk, level, optname, optval, optlen);
777 
778 unlock:
779 	release_sock(sk);
780 	return ret;
781 }
782 
783 static int mptcp_setsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
784 				    sockptr_t optval, unsigned int optlen)
785 {
786 	struct sock *sk = (void *)msk;
787 	int ret, val;
788 
789 	switch (optname) {
790 	case TCP_INQ:
791 		ret = mptcp_get_int_option(msk, optval, optlen, &val);
792 		if (ret)
793 			return ret;
794 		if (val < 0 || val > 1)
795 			return -EINVAL;
796 
797 		lock_sock(sk);
798 		msk->recvmsg_inq = !!val;
799 		release_sock(sk);
800 		return 0;
801 	case TCP_ULP:
802 		return -EOPNOTSUPP;
803 	case TCP_CONGESTION:
804 		return mptcp_setsockopt_sol_tcp_congestion(msk, optval, optlen);
805 	case TCP_CORK:
806 		return mptcp_setsockopt_sol_tcp_cork(msk, optval, optlen);
807 	case TCP_NODELAY:
808 		return mptcp_setsockopt_sol_tcp_nodelay(msk, optval, optlen);
809 	case TCP_DEFER_ACCEPT:
810 		/* See tcp.c: TCP_DEFER_ACCEPT does not fail */
811 		mptcp_setsockopt_first_sf_only(msk, SOL_TCP, optname, optval, optlen);
812 		return 0;
813 	case TCP_FASTOPEN:
814 	case TCP_FASTOPEN_CONNECT:
815 	case TCP_FASTOPEN_KEY:
816 	case TCP_FASTOPEN_NO_COOKIE:
817 		return mptcp_setsockopt_first_sf_only(msk, SOL_TCP, optname,
818 						      optval, optlen);
819 	}
820 
821 	return -EOPNOTSUPP;
822 }
823 
824 int mptcp_setsockopt(struct sock *sk, int level, int optname,
825 		     sockptr_t optval, unsigned int optlen)
826 {
827 	struct mptcp_sock *msk = mptcp_sk(sk);
828 	struct sock *ssk;
829 
830 	pr_debug("msk=%p", msk);
831 
832 	if (level == SOL_SOCKET)
833 		return mptcp_setsockopt_sol_socket(msk, optname, optval, optlen);
834 
835 	if (!mptcp_supported_sockopt(level, optname))
836 		return -ENOPROTOOPT;
837 
838 	/* @@ the meaning of setsockopt() when the socket is connected and
839 	 * there are multiple subflows is not yet defined. It is up to the
840 	 * MPTCP-level socket to configure the subflows until the subflow
841 	 * is in TCP fallback, when TCP socket options are passed through
842 	 * to the one remaining subflow.
843 	 */
844 	lock_sock(sk);
845 	ssk = __mptcp_tcp_fallback(msk);
846 	release_sock(sk);
847 	if (ssk)
848 		return tcp_setsockopt(ssk, level, optname, optval, optlen);
849 
850 	if (level == SOL_IP)
851 		return mptcp_setsockopt_v4(msk, optname, optval, optlen);
852 
853 	if (level == SOL_IPV6)
854 		return mptcp_setsockopt_v6(msk, optname, optval, optlen);
855 
856 	if (level == SOL_TCP)
857 		return mptcp_setsockopt_sol_tcp(msk, optname, optval, optlen);
858 
859 	return -EOPNOTSUPP;
860 }
861 
862 static int mptcp_getsockopt_first_sf_only(struct mptcp_sock *msk, int level, int optname,
863 					  char __user *optval, int __user *optlen)
864 {
865 	struct sock *sk = (struct sock *)msk;
866 	struct socket *ssock;
867 	int ret;
868 	struct sock *ssk;
869 
870 	lock_sock(sk);
871 	ssk = msk->first;
872 	if (ssk) {
873 		ret = tcp_getsockopt(ssk, level, optname, optval, optlen);
874 		goto out;
875 	}
876 
877 	ssock = __mptcp_nmpc_socket(msk);
878 	if (IS_ERR(ssock)) {
879 		ret = PTR_ERR(ssock);
880 		goto out;
881 	}
882 
883 	ret = tcp_getsockopt(ssock->sk, level, optname, optval, optlen);
884 
885 out:
886 	release_sock(sk);
887 	return ret;
888 }
889 
890 void mptcp_diag_fill_info(struct mptcp_sock *msk, struct mptcp_info *info)
891 {
892 	u32 flags = 0;
893 
894 	memset(info, 0, sizeof(*info));
895 
896 	info->mptcpi_subflows = READ_ONCE(msk->pm.subflows);
897 	info->mptcpi_add_addr_signal = READ_ONCE(msk->pm.add_addr_signaled);
898 	info->mptcpi_add_addr_accepted = READ_ONCE(msk->pm.add_addr_accepted);
899 	info->mptcpi_local_addr_used = READ_ONCE(msk->pm.local_addr_used);
900 
901 	/* The following limits only make sense for the in-kernel PM */
902 	if (mptcp_pm_is_kernel(msk)) {
903 		info->mptcpi_subflows_max =
904 			mptcp_pm_get_subflows_max(msk);
905 		info->mptcpi_add_addr_signal_max =
906 			mptcp_pm_get_add_addr_signal_max(msk);
907 		info->mptcpi_add_addr_accepted_max =
908 			mptcp_pm_get_add_addr_accept_max(msk);
909 		info->mptcpi_local_addr_max =
910 			mptcp_pm_get_local_addr_max(msk);
911 	}
912 
913 	if (test_bit(MPTCP_FALLBACK_DONE, &msk->flags))
914 		flags |= MPTCP_INFO_FLAG_FALLBACK;
915 	if (READ_ONCE(msk->can_ack))
916 		flags |= MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED;
917 	info->mptcpi_flags = flags;
918 	info->mptcpi_token = READ_ONCE(msk->token);
919 	info->mptcpi_write_seq = READ_ONCE(msk->write_seq);
920 	info->mptcpi_snd_una = READ_ONCE(msk->snd_una);
921 	info->mptcpi_rcv_nxt = READ_ONCE(msk->ack_seq);
922 	info->mptcpi_csum_enabled = READ_ONCE(msk->csum_enabled);
923 }
924 EXPORT_SYMBOL_GPL(mptcp_diag_fill_info);
925 
926 static int mptcp_getsockopt_info(struct mptcp_sock *msk, char __user *optval, int __user *optlen)
927 {
928 	struct mptcp_info m_info;
929 	int len;
930 
931 	if (get_user(len, optlen))
932 		return -EFAULT;
933 
934 	len = min_t(unsigned int, len, sizeof(struct mptcp_info));
935 
936 	mptcp_diag_fill_info(msk, &m_info);
937 
938 	if (put_user(len, optlen))
939 		return -EFAULT;
940 
941 	if (copy_to_user(optval, &m_info, len))
942 		return -EFAULT;
943 
944 	return 0;
945 }
946 
947 static int mptcp_put_subflow_data(struct mptcp_subflow_data *sfd,
948 				  char __user *optval,
949 				  u32 copied,
950 				  int __user *optlen)
951 {
952 	u32 copylen = min_t(u32, sfd->size_subflow_data, sizeof(*sfd));
953 
954 	if (copied)
955 		copied += sfd->size_subflow_data;
956 	else
957 		copied = copylen;
958 
959 	if (put_user(copied, optlen))
960 		return -EFAULT;
961 
962 	if (copy_to_user(optval, sfd, copylen))
963 		return -EFAULT;
964 
965 	return 0;
966 }
967 
968 static int mptcp_get_subflow_data(struct mptcp_subflow_data *sfd,
969 				  char __user *optval, int __user *optlen)
970 {
971 	int len, copylen;
972 
973 	if (get_user(len, optlen))
974 		return -EFAULT;
975 
976 	/* if mptcp_subflow_data size is changed, need to adjust
977 	 * this function to deal with programs using old version.
978 	 */
979 	BUILD_BUG_ON(sizeof(*sfd) != MIN_INFO_OPTLEN_SIZE);
980 
981 	if (len < MIN_INFO_OPTLEN_SIZE)
982 		return -EINVAL;
983 
984 	memset(sfd, 0, sizeof(*sfd));
985 
986 	copylen = min_t(unsigned int, len, sizeof(*sfd));
987 	if (copy_from_user(sfd, optval, copylen))
988 		return -EFAULT;
989 
990 	/* size_subflow_data is u32, but len is signed */
991 	if (sfd->size_subflow_data > INT_MAX ||
992 	    sfd->size_user > INT_MAX)
993 		return -EINVAL;
994 
995 	if (sfd->size_subflow_data < MIN_INFO_OPTLEN_SIZE ||
996 	    sfd->size_subflow_data > len)
997 		return -EINVAL;
998 
999 	if (sfd->num_subflows || sfd->size_kernel)
1000 		return -EINVAL;
1001 
1002 	return len - sfd->size_subflow_data;
1003 }
1004 
1005 static int mptcp_getsockopt_tcpinfo(struct mptcp_sock *msk, char __user *optval,
1006 				    int __user *optlen)
1007 {
1008 	struct mptcp_subflow_context *subflow;
1009 	struct sock *sk = (struct sock *)msk;
1010 	unsigned int sfcount = 0, copied = 0;
1011 	struct mptcp_subflow_data sfd;
1012 	char __user *infoptr;
1013 	int len;
1014 
1015 	len = mptcp_get_subflow_data(&sfd, optval, optlen);
1016 	if (len < 0)
1017 		return len;
1018 
1019 	sfd.size_kernel = sizeof(struct tcp_info);
1020 	sfd.size_user = min_t(unsigned int, sfd.size_user,
1021 			      sizeof(struct tcp_info));
1022 
1023 	infoptr = optval + sfd.size_subflow_data;
1024 
1025 	lock_sock(sk);
1026 
1027 	mptcp_for_each_subflow(msk, subflow) {
1028 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1029 
1030 		++sfcount;
1031 
1032 		if (len && len >= sfd.size_user) {
1033 			struct tcp_info info;
1034 
1035 			tcp_get_info(ssk, &info);
1036 
1037 			if (copy_to_user(infoptr, &info, sfd.size_user)) {
1038 				release_sock(sk);
1039 				return -EFAULT;
1040 			}
1041 
1042 			infoptr += sfd.size_user;
1043 			copied += sfd.size_user;
1044 			len -= sfd.size_user;
1045 		}
1046 	}
1047 
1048 	release_sock(sk);
1049 
1050 	sfd.num_subflows = sfcount;
1051 
1052 	if (mptcp_put_subflow_data(&sfd, optval, copied, optlen))
1053 		return -EFAULT;
1054 
1055 	return 0;
1056 }
1057 
1058 static void mptcp_get_sub_addrs(const struct sock *sk, struct mptcp_subflow_addrs *a)
1059 {
1060 	const struct inet_sock *inet = inet_sk(sk);
1061 
1062 	memset(a, 0, sizeof(*a));
1063 
1064 	if (sk->sk_family == AF_INET) {
1065 		a->sin_local.sin_family = AF_INET;
1066 		a->sin_local.sin_port = inet->inet_sport;
1067 		a->sin_local.sin_addr.s_addr = inet->inet_rcv_saddr;
1068 
1069 		if (!a->sin_local.sin_addr.s_addr)
1070 			a->sin_local.sin_addr.s_addr = inet->inet_saddr;
1071 
1072 		a->sin_remote.sin_family = AF_INET;
1073 		a->sin_remote.sin_port = inet->inet_dport;
1074 		a->sin_remote.sin_addr.s_addr = inet->inet_daddr;
1075 #if IS_ENABLED(CONFIG_IPV6)
1076 	} else if (sk->sk_family == AF_INET6) {
1077 		const struct ipv6_pinfo *np = inet6_sk(sk);
1078 
1079 		if (WARN_ON_ONCE(!np))
1080 			return;
1081 
1082 		a->sin6_local.sin6_family = AF_INET6;
1083 		a->sin6_local.sin6_port = inet->inet_sport;
1084 
1085 		if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
1086 			a->sin6_local.sin6_addr = np->saddr;
1087 		else
1088 			a->sin6_local.sin6_addr = sk->sk_v6_rcv_saddr;
1089 
1090 		a->sin6_remote.sin6_family = AF_INET6;
1091 		a->sin6_remote.sin6_port = inet->inet_dport;
1092 		a->sin6_remote.sin6_addr = sk->sk_v6_daddr;
1093 #endif
1094 	}
1095 }
1096 
1097 static int mptcp_getsockopt_subflow_addrs(struct mptcp_sock *msk, char __user *optval,
1098 					  int __user *optlen)
1099 {
1100 	struct mptcp_subflow_context *subflow;
1101 	struct sock *sk = (struct sock *)msk;
1102 	unsigned int sfcount = 0, copied = 0;
1103 	struct mptcp_subflow_data sfd;
1104 	char __user *addrptr;
1105 	int len;
1106 
1107 	len = mptcp_get_subflow_data(&sfd, optval, optlen);
1108 	if (len < 0)
1109 		return len;
1110 
1111 	sfd.size_kernel = sizeof(struct mptcp_subflow_addrs);
1112 	sfd.size_user = min_t(unsigned int, sfd.size_user,
1113 			      sizeof(struct mptcp_subflow_addrs));
1114 
1115 	addrptr = optval + sfd.size_subflow_data;
1116 
1117 	lock_sock(sk);
1118 
1119 	mptcp_for_each_subflow(msk, subflow) {
1120 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1121 
1122 		++sfcount;
1123 
1124 		if (len && len >= sfd.size_user) {
1125 			struct mptcp_subflow_addrs a;
1126 
1127 			mptcp_get_sub_addrs(ssk, &a);
1128 
1129 			if (copy_to_user(addrptr, &a, sfd.size_user)) {
1130 				release_sock(sk);
1131 				return -EFAULT;
1132 			}
1133 
1134 			addrptr += sfd.size_user;
1135 			copied += sfd.size_user;
1136 			len -= sfd.size_user;
1137 		}
1138 	}
1139 
1140 	release_sock(sk);
1141 
1142 	sfd.num_subflows = sfcount;
1143 
1144 	if (mptcp_put_subflow_data(&sfd, optval, copied, optlen))
1145 		return -EFAULT;
1146 
1147 	return 0;
1148 }
1149 
1150 static int mptcp_put_int_option(struct mptcp_sock *msk, char __user *optval,
1151 				int __user *optlen, int val)
1152 {
1153 	int len;
1154 
1155 	if (get_user(len, optlen))
1156 		return -EFAULT;
1157 	if (len < 0)
1158 		return -EINVAL;
1159 
1160 	if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
1161 		unsigned char ucval = (unsigned char)val;
1162 
1163 		len = 1;
1164 		if (put_user(len, optlen))
1165 			return -EFAULT;
1166 		if (copy_to_user(optval, &ucval, 1))
1167 			return -EFAULT;
1168 	} else {
1169 		len = min_t(unsigned int, len, sizeof(int));
1170 		if (put_user(len, optlen))
1171 			return -EFAULT;
1172 		if (copy_to_user(optval, &val, len))
1173 			return -EFAULT;
1174 	}
1175 
1176 	return 0;
1177 }
1178 
1179 static int mptcp_getsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
1180 				    char __user *optval, int __user *optlen)
1181 {
1182 	switch (optname) {
1183 	case TCP_ULP:
1184 	case TCP_CONGESTION:
1185 	case TCP_INFO:
1186 	case TCP_CC_INFO:
1187 	case TCP_DEFER_ACCEPT:
1188 	case TCP_FASTOPEN:
1189 	case TCP_FASTOPEN_CONNECT:
1190 	case TCP_FASTOPEN_KEY:
1191 	case TCP_FASTOPEN_NO_COOKIE:
1192 		return mptcp_getsockopt_first_sf_only(msk, SOL_TCP, optname,
1193 						      optval, optlen);
1194 	case TCP_INQ:
1195 		return mptcp_put_int_option(msk, optval, optlen, msk->recvmsg_inq);
1196 	case TCP_CORK:
1197 		return mptcp_put_int_option(msk, optval, optlen, msk->cork);
1198 	case TCP_NODELAY:
1199 		return mptcp_put_int_option(msk, optval, optlen, msk->nodelay);
1200 	}
1201 	return -EOPNOTSUPP;
1202 }
1203 
1204 static int mptcp_getsockopt_v4(struct mptcp_sock *msk, int optname,
1205 			       char __user *optval, int __user *optlen)
1206 {
1207 	struct sock *sk = (void *)msk;
1208 
1209 	switch (optname) {
1210 	case IP_TOS:
1211 		return mptcp_put_int_option(msk, optval, optlen, inet_sk(sk)->tos);
1212 	}
1213 
1214 	return -EOPNOTSUPP;
1215 }
1216 
1217 static int mptcp_getsockopt_sol_mptcp(struct mptcp_sock *msk, int optname,
1218 				      char __user *optval, int __user *optlen)
1219 {
1220 	switch (optname) {
1221 	case MPTCP_INFO:
1222 		return mptcp_getsockopt_info(msk, optval, optlen);
1223 	case MPTCP_TCPINFO:
1224 		return mptcp_getsockopt_tcpinfo(msk, optval, optlen);
1225 	case MPTCP_SUBFLOW_ADDRS:
1226 		return mptcp_getsockopt_subflow_addrs(msk, optval, optlen);
1227 	}
1228 
1229 	return -EOPNOTSUPP;
1230 }
1231 
1232 int mptcp_getsockopt(struct sock *sk, int level, int optname,
1233 		     char __user *optval, int __user *option)
1234 {
1235 	struct mptcp_sock *msk = mptcp_sk(sk);
1236 	struct sock *ssk;
1237 
1238 	pr_debug("msk=%p", msk);
1239 
1240 	/* @@ the meaning of setsockopt() when the socket is connected and
1241 	 * there are multiple subflows is not yet defined. It is up to the
1242 	 * MPTCP-level socket to configure the subflows until the subflow
1243 	 * is in TCP fallback, when socket options are passed through
1244 	 * to the one remaining subflow.
1245 	 */
1246 	lock_sock(sk);
1247 	ssk = __mptcp_tcp_fallback(msk);
1248 	release_sock(sk);
1249 	if (ssk)
1250 		return tcp_getsockopt(ssk, level, optname, optval, option);
1251 
1252 	if (level == SOL_IP)
1253 		return mptcp_getsockopt_v4(msk, optname, optval, option);
1254 	if (level == SOL_TCP)
1255 		return mptcp_getsockopt_sol_tcp(msk, optname, optval, option);
1256 	if (level == SOL_MPTCP)
1257 		return mptcp_getsockopt_sol_mptcp(msk, optname, optval, option);
1258 	return -EOPNOTSUPP;
1259 }
1260 
1261 static void sync_socket_options(struct mptcp_sock *msk, struct sock *ssk)
1262 {
1263 	static const unsigned int tx_rx_locks = SOCK_RCVBUF_LOCK | SOCK_SNDBUF_LOCK;
1264 	struct sock *sk = (struct sock *)msk;
1265 
1266 	if (ssk->sk_prot->keepalive) {
1267 		if (sock_flag(sk, SOCK_KEEPOPEN))
1268 			ssk->sk_prot->keepalive(ssk, 1);
1269 		else
1270 			ssk->sk_prot->keepalive(ssk, 0);
1271 	}
1272 
1273 	ssk->sk_priority = sk->sk_priority;
1274 	ssk->sk_bound_dev_if = sk->sk_bound_dev_if;
1275 	ssk->sk_incoming_cpu = sk->sk_incoming_cpu;
1276 	ssk->sk_ipv6only = sk->sk_ipv6only;
1277 	__ip_sock_set_tos(ssk, inet_sk(sk)->tos);
1278 
1279 	if (sk->sk_userlocks & tx_rx_locks) {
1280 		ssk->sk_userlocks |= sk->sk_userlocks & tx_rx_locks;
1281 		if (sk->sk_userlocks & SOCK_SNDBUF_LOCK)
1282 			WRITE_ONCE(ssk->sk_sndbuf, sk->sk_sndbuf);
1283 		if (sk->sk_userlocks & SOCK_RCVBUF_LOCK)
1284 			WRITE_ONCE(ssk->sk_rcvbuf, sk->sk_rcvbuf);
1285 	}
1286 
1287 	if (sock_flag(sk, SOCK_LINGER)) {
1288 		ssk->sk_lingertime = sk->sk_lingertime;
1289 		sock_set_flag(ssk, SOCK_LINGER);
1290 	} else {
1291 		sock_reset_flag(ssk, SOCK_LINGER);
1292 	}
1293 
1294 	if (sk->sk_mark != ssk->sk_mark) {
1295 		ssk->sk_mark = sk->sk_mark;
1296 		sk_dst_reset(ssk);
1297 	}
1298 
1299 	sock_valbool_flag(ssk, SOCK_DBG, sock_flag(sk, SOCK_DBG));
1300 
1301 	if (inet_csk(sk)->icsk_ca_ops != inet_csk(ssk)->icsk_ca_ops)
1302 		tcp_set_congestion_control(ssk, msk->ca_name, false, true);
1303 	__tcp_sock_set_cork(ssk, !!msk->cork);
1304 	__tcp_sock_set_nodelay(ssk, !!msk->nodelay);
1305 
1306 	inet_sk(ssk)->transparent = inet_sk(sk)->transparent;
1307 	inet_sk(ssk)->freebind = inet_sk(sk)->freebind;
1308 }
1309 
1310 static void __mptcp_sockopt_sync(struct mptcp_sock *msk, struct sock *ssk)
1311 {
1312 	bool slow = lock_sock_fast(ssk);
1313 
1314 	sync_socket_options(msk, ssk);
1315 
1316 	unlock_sock_fast(ssk, slow);
1317 }
1318 
1319 void mptcp_sockopt_sync(struct mptcp_sock *msk, struct sock *ssk)
1320 {
1321 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1322 
1323 	msk_owned_by_me(msk);
1324 
1325 	if (READ_ONCE(subflow->setsockopt_seq) != msk->setsockopt_seq) {
1326 		__mptcp_sockopt_sync(msk, ssk);
1327 
1328 		subflow->setsockopt_seq = msk->setsockopt_seq;
1329 	}
1330 }
1331 
1332 void mptcp_sockopt_sync_locked(struct mptcp_sock *msk, struct sock *ssk)
1333 {
1334 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1335 
1336 	msk_owned_by_me(msk);
1337 
1338 	if (READ_ONCE(subflow->setsockopt_seq) != msk->setsockopt_seq) {
1339 		sync_socket_options(msk, ssk);
1340 
1341 		subflow->setsockopt_seq = msk->setsockopt_seq;
1342 	}
1343 }
1344