xref: /openbmc/linux/net/mptcp/sockopt.c (revision a10c3d5f)
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 #define MIN_FULL_INFO_OPTLEN_SIZE	40
19 
20 static struct sock *__mptcp_tcp_fallback(struct mptcp_sock *msk)
21 {
22 	msk_owned_by_me(msk);
23 
24 	if (likely(!__mptcp_check_fallback(msk)))
25 		return NULL;
26 
27 	return msk->first;
28 }
29 
30 static u32 sockopt_seq_reset(const struct sock *sk)
31 {
32 	sock_owned_by_me(sk);
33 
34 	/* Highbits contain state.  Allows to distinguish sockopt_seq
35 	 * of listener and established:
36 	 * s0 = new_listener()
37 	 * sockopt(s0) - seq is 1
38 	 * s1 = accept(s0) - s1 inherits seq 1 if listener sk (s0)
39 	 * sockopt(s0) - seq increments to 2 on s0
40 	 * sockopt(s1) // seq increments to 2 on s1 (different option)
41 	 * new ssk completes join, inherits options from s0 // seq 2
42 	 * Needs sync from mptcp join logic, but ssk->seq == msk->seq
43 	 *
44 	 * Set High order bits to sk_state so ssk->seq == msk->seq test
45 	 * will fail.
46 	 */
47 
48 	return (u32)sk->sk_state << 24u;
49 }
50 
51 static void sockopt_seq_inc(struct mptcp_sock *msk)
52 {
53 	u32 seq = (msk->setsockopt_seq + 1) & 0x00ffffff;
54 
55 	msk->setsockopt_seq = sockopt_seq_reset((struct sock *)msk) + seq;
56 }
57 
58 static int mptcp_get_int_option(struct mptcp_sock *msk, sockptr_t optval,
59 				unsigned int optlen, int *val)
60 {
61 	if (optlen < sizeof(int))
62 		return -EINVAL;
63 
64 	if (copy_from_sockptr(val, optval, sizeof(*val)))
65 		return -EFAULT;
66 
67 	return 0;
68 }
69 
70 static void mptcp_sol_socket_sync_intval(struct mptcp_sock *msk, int optname, int val)
71 {
72 	struct mptcp_subflow_context *subflow;
73 	struct sock *sk = (struct sock *)msk;
74 
75 	lock_sock(sk);
76 	sockopt_seq_inc(msk);
77 
78 	mptcp_for_each_subflow(msk, subflow) {
79 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
80 		bool slow = lock_sock_fast(ssk);
81 
82 		switch (optname) {
83 		case SO_DEBUG:
84 			sock_valbool_flag(ssk, SOCK_DBG, !!val);
85 			break;
86 		case SO_KEEPALIVE:
87 			if (ssk->sk_prot->keepalive)
88 				ssk->sk_prot->keepalive(ssk, !!val);
89 			sock_valbool_flag(ssk, SOCK_KEEPOPEN, !!val);
90 			break;
91 		case SO_PRIORITY:
92 			ssk->sk_priority = val;
93 			break;
94 		case SO_SNDBUF:
95 		case SO_SNDBUFFORCE:
96 			ssk->sk_userlocks |= SOCK_SNDBUF_LOCK;
97 			WRITE_ONCE(ssk->sk_sndbuf, sk->sk_sndbuf);
98 			mptcp_subflow_ctx(ssk)->cached_sndbuf = sk->sk_sndbuf;
99 			break;
100 		case SO_RCVBUF:
101 		case SO_RCVBUFFORCE:
102 			ssk->sk_userlocks |= SOCK_RCVBUF_LOCK;
103 			WRITE_ONCE(ssk->sk_rcvbuf, sk->sk_rcvbuf);
104 			break;
105 		case SO_MARK:
106 			if (READ_ONCE(ssk->sk_mark) != sk->sk_mark) {
107 				WRITE_ONCE(ssk->sk_mark, sk->sk_mark);
108 				sk_dst_reset(ssk);
109 			}
110 			break;
111 		case SO_INCOMING_CPU:
112 			WRITE_ONCE(ssk->sk_incoming_cpu, val);
113 			break;
114 		}
115 
116 		subflow->setsockopt_seq = msk->setsockopt_seq;
117 		unlock_sock_fast(ssk, slow);
118 	}
119 
120 	release_sock(sk);
121 }
122 
123 static int mptcp_sol_socket_intval(struct mptcp_sock *msk, int optname, int val)
124 {
125 	sockptr_t optval = KERNEL_SOCKPTR(&val);
126 	struct sock *sk = (struct sock *)msk;
127 	int ret;
128 
129 	ret = sock_setsockopt(sk->sk_socket, SOL_SOCKET, optname,
130 			      optval, sizeof(val));
131 	if (ret)
132 		return ret;
133 
134 	mptcp_sol_socket_sync_intval(msk, optname, val);
135 	return 0;
136 }
137 
138 static void mptcp_so_incoming_cpu(struct mptcp_sock *msk, int val)
139 {
140 	struct sock *sk = (struct sock *)msk;
141 
142 	WRITE_ONCE(sk->sk_incoming_cpu, val);
143 
144 	mptcp_sol_socket_sync_intval(msk, SO_INCOMING_CPU, val);
145 }
146 
147 static int mptcp_setsockopt_sol_socket_tstamp(struct mptcp_sock *msk, int optname, int val)
148 {
149 	sockptr_t optval = KERNEL_SOCKPTR(&val);
150 	struct mptcp_subflow_context *subflow;
151 	struct sock *sk = (struct sock *)msk;
152 	int ret;
153 
154 	ret = sock_setsockopt(sk->sk_socket, SOL_SOCKET, optname,
155 			      optval, sizeof(val));
156 	if (ret)
157 		return ret;
158 
159 	lock_sock(sk);
160 	mptcp_for_each_subflow(msk, subflow) {
161 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
162 		bool slow = lock_sock_fast(ssk);
163 
164 		sock_set_timestamp(sk, optname, !!val);
165 		unlock_sock_fast(ssk, slow);
166 	}
167 
168 	release_sock(sk);
169 	return 0;
170 }
171 
172 static int mptcp_setsockopt_sol_socket_int(struct mptcp_sock *msk, int optname,
173 					   sockptr_t optval,
174 					   unsigned int optlen)
175 {
176 	int val, ret;
177 
178 	ret = mptcp_get_int_option(msk, optval, optlen, &val);
179 	if (ret)
180 		return ret;
181 
182 	switch (optname) {
183 	case SO_KEEPALIVE:
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 sock *ssk;
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 		ssk = __mptcp_nmpc_sk(msk);
304 		if (IS_ERR(ssk)) {
305 			release_sock(sk);
306 			return PTR_ERR(ssk);
307 		}
308 
309 		ret = sk_setsockopt(ssk, SOL_SOCKET, optname, optval, optlen);
310 		if (ret == 0) {
311 			if (optname == SO_REUSEPORT)
312 				sk->sk_reuseport = ssk->sk_reuseport;
313 			else if (optname == SO_REUSEADDR)
314 				sk->sk_reuse = ssk->sk_reuse;
315 			else if (optname == SO_BINDTODEVICE)
316 				sk->sk_bound_dev_if = ssk->sk_bound_dev_if;
317 			else if (optname == SO_BINDTOIFINDEX)
318 				sk->sk_bound_dev_if = ssk->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 sock *ssk;
393 
394 	switch (optname) {
395 	case IPV6_V6ONLY:
396 	case IPV6_TRANSPARENT:
397 	case IPV6_FREEBIND:
398 		lock_sock(sk);
399 		ssk = __mptcp_nmpc_sk(msk);
400 		if (IS_ERR(ssk)) {
401 			release_sock(sk);
402 			return PTR_ERR(ssk);
403 		}
404 
405 		ret = tcp_setsockopt(ssk, 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 = ssk->sk_ipv6only;
416 			break;
417 		case IPV6_TRANSPARENT:
418 			inet_assign_bit(TRANSPARENT, sk,
419 					inet_test_bit(TRANSPARENT, ssk));
420 			break;
421 		case IPV6_FREEBIND:
422 			inet_assign_bit(FREEBIND, sk,
423 					inet_test_bit(FREEBIND, ssk));
424 			break;
425 		}
426 
427 		release_sock(sk);
428 		break;
429 	}
430 
431 	return ret;
432 }
433 
434 static bool mptcp_supported_sockopt(int level, int optname)
435 {
436 	if (level == SOL_IP) {
437 		switch (optname) {
438 		/* should work fine */
439 		case IP_FREEBIND:
440 		case IP_TRANSPARENT:
441 
442 		/* the following are control cmsg related */
443 		case IP_PKTINFO:
444 		case IP_RECVTTL:
445 		case IP_RECVTOS:
446 		case IP_RECVOPTS:
447 		case IP_RETOPTS:
448 		case IP_PASSSEC:
449 		case IP_RECVORIGDSTADDR:
450 		case IP_CHECKSUM:
451 		case IP_RECVFRAGSIZE:
452 
453 		/* common stuff that need some love */
454 		case IP_TOS:
455 		case IP_TTL:
456 		case IP_BIND_ADDRESS_NO_PORT:
457 		case IP_MTU_DISCOVER:
458 		case IP_RECVERR:
459 
460 		/* possibly less common may deserve some love */
461 		case IP_MINTTL:
462 
463 		/* the following is apparently a no-op for plain TCP */
464 		case IP_RECVERR_RFC4884:
465 			return true;
466 		}
467 
468 		/* IP_OPTIONS is not supported, needs subflow care */
469 		/* IP_HDRINCL, IP_NODEFRAG are not supported, RAW specific */
470 		/* IP_MULTICAST_TTL, IP_MULTICAST_LOOP, IP_UNICAST_IF,
471 		 * IP_ADD_MEMBERSHIP, IP_ADD_SOURCE_MEMBERSHIP, IP_DROP_MEMBERSHIP,
472 		 * IP_DROP_SOURCE_MEMBERSHIP, IP_BLOCK_SOURCE, IP_UNBLOCK_SOURCE,
473 		 * MCAST_JOIN_GROUP, MCAST_LEAVE_GROUP MCAST_JOIN_SOURCE_GROUP,
474 		 * MCAST_LEAVE_SOURCE_GROUP, MCAST_BLOCK_SOURCE, MCAST_UNBLOCK_SOURCE,
475 		 * MCAST_MSFILTER, IP_MULTICAST_ALL are not supported, better not deal
476 		 * with mcast stuff
477 		 */
478 		/* IP_IPSEC_POLICY, IP_XFRM_POLICY are nut supported, unrelated here */
479 		return false;
480 	}
481 	if (level == SOL_IPV6) {
482 		switch (optname) {
483 		case IPV6_V6ONLY:
484 
485 		/* the following are control cmsg related */
486 		case IPV6_RECVPKTINFO:
487 		case IPV6_2292PKTINFO:
488 		case IPV6_RECVHOPLIMIT:
489 		case IPV6_2292HOPLIMIT:
490 		case IPV6_RECVRTHDR:
491 		case IPV6_2292RTHDR:
492 		case IPV6_RECVHOPOPTS:
493 		case IPV6_2292HOPOPTS:
494 		case IPV6_RECVDSTOPTS:
495 		case IPV6_2292DSTOPTS:
496 		case IPV6_RECVTCLASS:
497 		case IPV6_FLOWINFO:
498 		case IPV6_RECVPATHMTU:
499 		case IPV6_RECVORIGDSTADDR:
500 		case IPV6_RECVFRAGSIZE:
501 
502 		/* the following ones need some love but are quite common */
503 		case IPV6_TCLASS:
504 		case IPV6_TRANSPARENT:
505 		case IPV6_FREEBIND:
506 		case IPV6_PKTINFO:
507 		case IPV6_2292PKTOPTIONS:
508 		case IPV6_UNICAST_HOPS:
509 		case IPV6_MTU_DISCOVER:
510 		case IPV6_MTU:
511 		case IPV6_RECVERR:
512 		case IPV6_FLOWINFO_SEND:
513 		case IPV6_FLOWLABEL_MGR:
514 		case IPV6_MINHOPCOUNT:
515 		case IPV6_DONTFRAG:
516 		case IPV6_AUTOFLOWLABEL:
517 
518 		/* the following one is a no-op for plain TCP */
519 		case IPV6_RECVERR_RFC4884:
520 			return true;
521 		}
522 
523 		/* IPV6_HOPOPTS, IPV6_RTHDRDSTOPTS, IPV6_RTHDR, IPV6_DSTOPTS are
524 		 * not supported
525 		 */
526 		/* IPV6_MULTICAST_HOPS, IPV6_MULTICAST_LOOP, IPV6_UNICAST_IF,
527 		 * IPV6_MULTICAST_IF, IPV6_ADDRFORM,
528 		 * IPV6_ADD_MEMBERSHIP, IPV6_DROP_MEMBERSHIP, IPV6_JOIN_ANYCAST,
529 		 * IPV6_LEAVE_ANYCAST, IPV6_MULTICAST_ALL, MCAST_JOIN_GROUP, MCAST_LEAVE_GROUP,
530 		 * MCAST_JOIN_SOURCE_GROUP, MCAST_LEAVE_SOURCE_GROUP,
531 		 * MCAST_BLOCK_SOURCE, MCAST_UNBLOCK_SOURCE, MCAST_MSFILTER
532 		 * are not supported better not deal with mcast
533 		 */
534 		/* IPV6_ROUTER_ALERT, IPV6_ROUTER_ALERT_ISOLATE are not supported, since are evil */
535 
536 		/* IPV6_IPSEC_POLICY, IPV6_XFRM_POLICY are not supported */
537 		/* IPV6_ADDR_PREFERENCES is not supported, we must be careful with subflows */
538 		return false;
539 	}
540 	if (level == SOL_TCP) {
541 		switch (optname) {
542 		/* the following are no-op or should work just fine */
543 		case TCP_THIN_DUPACK:
544 		case TCP_DEFER_ACCEPT:
545 
546 		/* the following need some love */
547 		case TCP_MAXSEG:
548 		case TCP_NODELAY:
549 		case TCP_THIN_LINEAR_TIMEOUTS:
550 		case TCP_CONGESTION:
551 		case TCP_CORK:
552 		case TCP_KEEPIDLE:
553 		case TCP_KEEPINTVL:
554 		case TCP_KEEPCNT:
555 		case TCP_SYNCNT:
556 		case TCP_SAVE_SYN:
557 		case TCP_LINGER2:
558 		case TCP_WINDOW_CLAMP:
559 		case TCP_QUICKACK:
560 		case TCP_USER_TIMEOUT:
561 		case TCP_TIMESTAMP:
562 		case TCP_NOTSENT_LOWAT:
563 		case TCP_TX_DELAY:
564 		case TCP_INQ:
565 		case TCP_FASTOPEN:
566 		case TCP_FASTOPEN_CONNECT:
567 		case TCP_FASTOPEN_KEY:
568 		case TCP_FASTOPEN_NO_COOKIE:
569 			return true;
570 		}
571 
572 		/* TCP_MD5SIG, TCP_MD5SIG_EXT are not supported, MD5 is not compatible with MPTCP */
573 
574 		/* TCP_REPAIR, TCP_REPAIR_QUEUE, TCP_QUEUE_SEQ, TCP_REPAIR_OPTIONS,
575 		 * TCP_REPAIR_WINDOW are not supported, better avoid this mess
576 		 */
577 	}
578 	return false;
579 }
580 
581 static int mptcp_setsockopt_sol_tcp_congestion(struct mptcp_sock *msk, sockptr_t optval,
582 					       unsigned int optlen)
583 {
584 	struct mptcp_subflow_context *subflow;
585 	struct sock *sk = (struct sock *)msk;
586 	char name[TCP_CA_NAME_MAX];
587 	bool cap_net_admin;
588 	int ret;
589 
590 	if (optlen < 1)
591 		return -EINVAL;
592 
593 	ret = strncpy_from_sockptr(name, optval,
594 				   min_t(long, TCP_CA_NAME_MAX - 1, optlen));
595 	if (ret < 0)
596 		return -EFAULT;
597 
598 	name[ret] = 0;
599 
600 	cap_net_admin = ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN);
601 
602 	ret = 0;
603 	lock_sock(sk);
604 	sockopt_seq_inc(msk);
605 	mptcp_for_each_subflow(msk, subflow) {
606 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
607 		int err;
608 
609 		lock_sock(ssk);
610 		err = tcp_set_congestion_control(ssk, name, true, cap_net_admin);
611 		if (err < 0 && ret == 0)
612 			ret = err;
613 		subflow->setsockopt_seq = msk->setsockopt_seq;
614 		release_sock(ssk);
615 	}
616 
617 	if (ret == 0)
618 		strcpy(msk->ca_name, name);
619 
620 	release_sock(sk);
621 	return ret;
622 }
623 
624 static int mptcp_setsockopt_sol_tcp_cork(struct mptcp_sock *msk, sockptr_t optval,
625 					 unsigned int optlen)
626 {
627 	struct mptcp_subflow_context *subflow;
628 	struct sock *sk = (struct sock *)msk;
629 	int val;
630 
631 	if (optlen < sizeof(int))
632 		return -EINVAL;
633 
634 	if (copy_from_sockptr(&val, optval, sizeof(val)))
635 		return -EFAULT;
636 
637 	lock_sock(sk);
638 	sockopt_seq_inc(msk);
639 	msk->cork = !!val;
640 	mptcp_for_each_subflow(msk, subflow) {
641 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
642 
643 		lock_sock(ssk);
644 		__tcp_sock_set_cork(ssk, !!val);
645 		release_sock(ssk);
646 	}
647 	if (!val)
648 		mptcp_check_and_set_pending(sk);
649 	release_sock(sk);
650 
651 	return 0;
652 }
653 
654 static int mptcp_setsockopt_sol_tcp_nodelay(struct mptcp_sock *msk, sockptr_t optval,
655 					    unsigned int optlen)
656 {
657 	struct mptcp_subflow_context *subflow;
658 	struct sock *sk = (struct sock *)msk;
659 	int val;
660 
661 	if (optlen < sizeof(int))
662 		return -EINVAL;
663 
664 	if (copy_from_sockptr(&val, optval, sizeof(val)))
665 		return -EFAULT;
666 
667 	lock_sock(sk);
668 	sockopt_seq_inc(msk);
669 	msk->nodelay = !!val;
670 	mptcp_for_each_subflow(msk, subflow) {
671 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
672 
673 		lock_sock(ssk);
674 		__tcp_sock_set_nodelay(ssk, !!val);
675 		release_sock(ssk);
676 	}
677 	if (val)
678 		mptcp_check_and_set_pending(sk);
679 	release_sock(sk);
680 
681 	return 0;
682 }
683 
684 static int mptcp_setsockopt_sol_ip_set_transparent(struct mptcp_sock *msk, int optname,
685 						   sockptr_t optval, unsigned int optlen)
686 {
687 	struct sock *sk = (struct sock *)msk;
688 	struct sock *ssk;
689 	int err;
690 
691 	err = ip_setsockopt(sk, SOL_IP, optname, optval, optlen);
692 	if (err != 0)
693 		return err;
694 
695 	lock_sock(sk);
696 
697 	ssk = __mptcp_nmpc_sk(msk);
698 	if (IS_ERR(ssk)) {
699 		release_sock(sk);
700 		return PTR_ERR(ssk);
701 	}
702 
703 	switch (optname) {
704 	case IP_FREEBIND:
705 		inet_assign_bit(FREEBIND, ssk, inet_test_bit(FREEBIND, sk));
706 		break;
707 	case IP_TRANSPARENT:
708 		inet_assign_bit(TRANSPARENT, ssk,
709 				inet_test_bit(TRANSPARENT, sk));
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 		bool slow;
740 
741 		slow = lock_sock_fast(ssk);
742 		__ip_sock_set_tos(ssk, val);
743 		unlock_sock_fast(ssk, slow);
744 	}
745 	release_sock(sk);
746 
747 	return 0;
748 }
749 
750 static int mptcp_setsockopt_v4(struct mptcp_sock *msk, int optname,
751 			       sockptr_t optval, unsigned int optlen)
752 {
753 	switch (optname) {
754 	case IP_FREEBIND:
755 	case IP_TRANSPARENT:
756 		return mptcp_setsockopt_sol_ip_set_transparent(msk, optname, optval, optlen);
757 	case IP_TOS:
758 		return mptcp_setsockopt_v4_set_tos(msk, optname, optval, optlen);
759 	}
760 
761 	return -EOPNOTSUPP;
762 }
763 
764 static int mptcp_setsockopt_first_sf_only(struct mptcp_sock *msk, int level, int optname,
765 					  sockptr_t optval, unsigned int optlen)
766 {
767 	struct sock *sk = (struct sock *)msk;
768 	struct sock *ssk;
769 	int ret;
770 
771 	/* Limit to first subflow, before the connection establishment */
772 	lock_sock(sk);
773 	ssk = __mptcp_nmpc_sk(msk);
774 	if (IS_ERR(ssk)) {
775 		ret = PTR_ERR(ssk);
776 		goto unlock;
777 	}
778 
779 	ret = tcp_setsockopt(ssk, level, optname, optval, optlen);
780 
781 unlock:
782 	release_sock(sk);
783 	return ret;
784 }
785 
786 static int mptcp_setsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
787 				    sockptr_t optval, unsigned int optlen)
788 {
789 	struct sock *sk = (void *)msk;
790 	int ret, val;
791 
792 	switch (optname) {
793 	case TCP_INQ:
794 		ret = mptcp_get_int_option(msk, optval, optlen, &val);
795 		if (ret)
796 			return ret;
797 		if (val < 0 || val > 1)
798 			return -EINVAL;
799 
800 		lock_sock(sk);
801 		msk->recvmsg_inq = !!val;
802 		release_sock(sk);
803 		return 0;
804 	case TCP_ULP:
805 		return -EOPNOTSUPP;
806 	case TCP_CONGESTION:
807 		return mptcp_setsockopt_sol_tcp_congestion(msk, optval, optlen);
808 	case TCP_CORK:
809 		return mptcp_setsockopt_sol_tcp_cork(msk, optval, optlen);
810 	case TCP_NODELAY:
811 		return mptcp_setsockopt_sol_tcp_nodelay(msk, optval, optlen);
812 	case TCP_DEFER_ACCEPT:
813 		/* See tcp.c: TCP_DEFER_ACCEPT does not fail */
814 		mptcp_setsockopt_first_sf_only(msk, SOL_TCP, optname, optval, optlen);
815 		return 0;
816 	case TCP_FASTOPEN:
817 	case TCP_FASTOPEN_CONNECT:
818 	case TCP_FASTOPEN_KEY:
819 	case TCP_FASTOPEN_NO_COOKIE:
820 		return mptcp_setsockopt_first_sf_only(msk, SOL_TCP, optname,
821 						      optval, optlen);
822 	}
823 
824 	return -EOPNOTSUPP;
825 }
826 
827 int mptcp_setsockopt(struct sock *sk, int level, int optname,
828 		     sockptr_t optval, unsigned int optlen)
829 {
830 	struct mptcp_sock *msk = mptcp_sk(sk);
831 	struct sock *ssk;
832 
833 	pr_debug("msk=%p", msk);
834 
835 	if (level == SOL_SOCKET)
836 		return mptcp_setsockopt_sol_socket(msk, optname, optval, optlen);
837 
838 	if (!mptcp_supported_sockopt(level, optname))
839 		return -ENOPROTOOPT;
840 
841 	/* @@ the meaning of setsockopt() when the socket is connected and
842 	 * there are multiple subflows is not yet defined. It is up to the
843 	 * MPTCP-level socket to configure the subflows until the subflow
844 	 * is in TCP fallback, when TCP socket options are passed through
845 	 * to the one remaining subflow.
846 	 */
847 	lock_sock(sk);
848 	ssk = __mptcp_tcp_fallback(msk);
849 	release_sock(sk);
850 	if (ssk)
851 		return tcp_setsockopt(ssk, level, optname, optval, optlen);
852 
853 	if (level == SOL_IP)
854 		return mptcp_setsockopt_v4(msk, optname, optval, optlen);
855 
856 	if (level == SOL_IPV6)
857 		return mptcp_setsockopt_v6(msk, optname, optval, optlen);
858 
859 	if (level == SOL_TCP)
860 		return mptcp_setsockopt_sol_tcp(msk, optname, optval, optlen);
861 
862 	return -EOPNOTSUPP;
863 }
864 
865 static int mptcp_getsockopt_first_sf_only(struct mptcp_sock *msk, int level, int optname,
866 					  char __user *optval, int __user *optlen)
867 {
868 	struct sock *sk = (struct sock *)msk;
869 	struct sock *ssk;
870 	int ret;
871 
872 	lock_sock(sk);
873 	ssk = msk->first;
874 	if (ssk) {
875 		ret = tcp_getsockopt(ssk, level, optname, optval, optlen);
876 		goto out;
877 	}
878 
879 	ssk = __mptcp_nmpc_sk(msk);
880 	if (IS_ERR(ssk)) {
881 		ret = PTR_ERR(ssk);
882 		goto out;
883 	}
884 
885 	ret = tcp_getsockopt(ssk, level, optname, optval, optlen);
886 
887 out:
888 	release_sock(sk);
889 	return ret;
890 }
891 
892 void mptcp_diag_fill_info(struct mptcp_sock *msk, struct mptcp_info *info)
893 {
894 	struct sock *sk = (struct sock *)msk;
895 	u32 flags = 0;
896 	bool slow;
897 
898 	memset(info, 0, sizeof(*info));
899 
900 	info->mptcpi_subflows = READ_ONCE(msk->pm.subflows);
901 	info->mptcpi_add_addr_signal = READ_ONCE(msk->pm.add_addr_signaled);
902 	info->mptcpi_add_addr_accepted = READ_ONCE(msk->pm.add_addr_accepted);
903 	info->mptcpi_local_addr_used = READ_ONCE(msk->pm.local_addr_used);
904 
905 	if (inet_sk_state_load(sk) == TCP_LISTEN)
906 		return;
907 
908 	/* The following limits only make sense for the in-kernel PM */
909 	if (mptcp_pm_is_kernel(msk)) {
910 		info->mptcpi_subflows_max =
911 			mptcp_pm_get_subflows_max(msk);
912 		info->mptcpi_add_addr_signal_max =
913 			mptcp_pm_get_add_addr_signal_max(msk);
914 		info->mptcpi_add_addr_accepted_max =
915 			mptcp_pm_get_add_addr_accept_max(msk);
916 		info->mptcpi_local_addr_max =
917 			mptcp_pm_get_local_addr_max(msk);
918 	}
919 
920 	if (test_bit(MPTCP_FALLBACK_DONE, &msk->flags))
921 		flags |= MPTCP_INFO_FLAG_FALLBACK;
922 	if (READ_ONCE(msk->can_ack))
923 		flags |= MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED;
924 	info->mptcpi_flags = flags;
925 	mptcp_data_lock(sk);
926 	info->mptcpi_snd_una = msk->snd_una;
927 	info->mptcpi_rcv_nxt = msk->ack_seq;
928 	info->mptcpi_bytes_acked = msk->bytes_acked;
929 	mptcp_data_unlock(sk);
930 
931 	slow = lock_sock_fast(sk);
932 	info->mptcpi_csum_enabled = msk->csum_enabled;
933 	info->mptcpi_token = msk->token;
934 	info->mptcpi_write_seq = msk->write_seq;
935 	info->mptcpi_retransmits = inet_csk(sk)->icsk_retransmits;
936 	info->mptcpi_bytes_sent = msk->bytes_sent;
937 	info->mptcpi_bytes_received = msk->bytes_received;
938 	info->mptcpi_bytes_retrans = msk->bytes_retrans;
939 	unlock_sock_fast(sk, slow);
940 }
941 EXPORT_SYMBOL_GPL(mptcp_diag_fill_info);
942 
943 static int mptcp_getsockopt_info(struct mptcp_sock *msk, char __user *optval, int __user *optlen)
944 {
945 	struct mptcp_info m_info;
946 	int len;
947 
948 	if (get_user(len, optlen))
949 		return -EFAULT;
950 
951 	len = min_t(unsigned int, len, sizeof(struct mptcp_info));
952 
953 	mptcp_diag_fill_info(msk, &m_info);
954 
955 	if (put_user(len, optlen))
956 		return -EFAULT;
957 
958 	if (copy_to_user(optval, &m_info, len))
959 		return -EFAULT;
960 
961 	return 0;
962 }
963 
964 static int mptcp_put_subflow_data(struct mptcp_subflow_data *sfd,
965 				  char __user *optval,
966 				  u32 copied,
967 				  int __user *optlen)
968 {
969 	u32 copylen = min_t(u32, sfd->size_subflow_data, sizeof(*sfd));
970 
971 	if (copied)
972 		copied += sfd->size_subflow_data;
973 	else
974 		copied = copylen;
975 
976 	if (put_user(copied, optlen))
977 		return -EFAULT;
978 
979 	if (copy_to_user(optval, sfd, copylen))
980 		return -EFAULT;
981 
982 	return 0;
983 }
984 
985 static int mptcp_get_subflow_data(struct mptcp_subflow_data *sfd,
986 				  char __user *optval,
987 				  int __user *optlen)
988 {
989 	int len, copylen;
990 
991 	if (get_user(len, optlen))
992 		return -EFAULT;
993 
994 	/* if mptcp_subflow_data size is changed, need to adjust
995 	 * this function to deal with programs using old version.
996 	 */
997 	BUILD_BUG_ON(sizeof(*sfd) != MIN_INFO_OPTLEN_SIZE);
998 
999 	if (len < MIN_INFO_OPTLEN_SIZE)
1000 		return -EINVAL;
1001 
1002 	memset(sfd, 0, sizeof(*sfd));
1003 
1004 	copylen = min_t(unsigned int, len, sizeof(*sfd));
1005 	if (copy_from_user(sfd, optval, copylen))
1006 		return -EFAULT;
1007 
1008 	/* size_subflow_data is u32, but len is signed */
1009 	if (sfd->size_subflow_data > INT_MAX ||
1010 	    sfd->size_user > INT_MAX)
1011 		return -EINVAL;
1012 
1013 	if (sfd->size_subflow_data < MIN_INFO_OPTLEN_SIZE ||
1014 	    sfd->size_subflow_data > len)
1015 		return -EINVAL;
1016 
1017 	if (sfd->num_subflows || sfd->size_kernel)
1018 		return -EINVAL;
1019 
1020 	return len - sfd->size_subflow_data;
1021 }
1022 
1023 static int mptcp_getsockopt_tcpinfo(struct mptcp_sock *msk, char __user *optval,
1024 				    int __user *optlen)
1025 {
1026 	struct mptcp_subflow_context *subflow;
1027 	struct sock *sk = (struct sock *)msk;
1028 	unsigned int sfcount = 0, copied = 0;
1029 	struct mptcp_subflow_data sfd;
1030 	char __user *infoptr;
1031 	int len;
1032 
1033 	len = mptcp_get_subflow_data(&sfd, optval, optlen);
1034 	if (len < 0)
1035 		return len;
1036 
1037 	sfd.size_kernel = sizeof(struct tcp_info);
1038 	sfd.size_user = min_t(unsigned int, sfd.size_user,
1039 			      sizeof(struct tcp_info));
1040 
1041 	infoptr = optval + sfd.size_subflow_data;
1042 
1043 	lock_sock(sk);
1044 
1045 	mptcp_for_each_subflow(msk, subflow) {
1046 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1047 
1048 		++sfcount;
1049 
1050 		if (len && len >= sfd.size_user) {
1051 			struct tcp_info info;
1052 
1053 			tcp_get_info(ssk, &info);
1054 
1055 			if (copy_to_user(infoptr, &info, sfd.size_user)) {
1056 				release_sock(sk);
1057 				return -EFAULT;
1058 			}
1059 
1060 			infoptr += sfd.size_user;
1061 			copied += sfd.size_user;
1062 			len -= sfd.size_user;
1063 		}
1064 	}
1065 
1066 	release_sock(sk);
1067 
1068 	sfd.num_subflows = sfcount;
1069 
1070 	if (mptcp_put_subflow_data(&sfd, optval, copied, optlen))
1071 		return -EFAULT;
1072 
1073 	return 0;
1074 }
1075 
1076 static void mptcp_get_sub_addrs(const struct sock *sk, struct mptcp_subflow_addrs *a)
1077 {
1078 	const struct inet_sock *inet = inet_sk(sk);
1079 
1080 	memset(a, 0, sizeof(*a));
1081 
1082 	if (sk->sk_family == AF_INET) {
1083 		a->sin_local.sin_family = AF_INET;
1084 		a->sin_local.sin_port = inet->inet_sport;
1085 		a->sin_local.sin_addr.s_addr = inet->inet_rcv_saddr;
1086 
1087 		if (!a->sin_local.sin_addr.s_addr)
1088 			a->sin_local.sin_addr.s_addr = inet->inet_saddr;
1089 
1090 		a->sin_remote.sin_family = AF_INET;
1091 		a->sin_remote.sin_port = inet->inet_dport;
1092 		a->sin_remote.sin_addr.s_addr = inet->inet_daddr;
1093 #if IS_ENABLED(CONFIG_IPV6)
1094 	} else if (sk->sk_family == AF_INET6) {
1095 		const struct ipv6_pinfo *np = inet6_sk(sk);
1096 
1097 		if (WARN_ON_ONCE(!np))
1098 			return;
1099 
1100 		a->sin6_local.sin6_family = AF_INET6;
1101 		a->sin6_local.sin6_port = inet->inet_sport;
1102 
1103 		if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
1104 			a->sin6_local.sin6_addr = np->saddr;
1105 		else
1106 			a->sin6_local.sin6_addr = sk->sk_v6_rcv_saddr;
1107 
1108 		a->sin6_remote.sin6_family = AF_INET6;
1109 		a->sin6_remote.sin6_port = inet->inet_dport;
1110 		a->sin6_remote.sin6_addr = sk->sk_v6_daddr;
1111 #endif
1112 	}
1113 }
1114 
1115 static int mptcp_getsockopt_subflow_addrs(struct mptcp_sock *msk, char __user *optval,
1116 					  int __user *optlen)
1117 {
1118 	struct mptcp_subflow_context *subflow;
1119 	struct sock *sk = (struct sock *)msk;
1120 	unsigned int sfcount = 0, copied = 0;
1121 	struct mptcp_subflow_data sfd;
1122 	char __user *addrptr;
1123 	int len;
1124 
1125 	len = mptcp_get_subflow_data(&sfd, optval, optlen);
1126 	if (len < 0)
1127 		return len;
1128 
1129 	sfd.size_kernel = sizeof(struct mptcp_subflow_addrs);
1130 	sfd.size_user = min_t(unsigned int, sfd.size_user,
1131 			      sizeof(struct mptcp_subflow_addrs));
1132 
1133 	addrptr = optval + sfd.size_subflow_data;
1134 
1135 	lock_sock(sk);
1136 
1137 	mptcp_for_each_subflow(msk, subflow) {
1138 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1139 
1140 		++sfcount;
1141 
1142 		if (len && len >= sfd.size_user) {
1143 			struct mptcp_subflow_addrs a;
1144 
1145 			mptcp_get_sub_addrs(ssk, &a);
1146 
1147 			if (copy_to_user(addrptr, &a, sfd.size_user)) {
1148 				release_sock(sk);
1149 				return -EFAULT;
1150 			}
1151 
1152 			addrptr += sfd.size_user;
1153 			copied += sfd.size_user;
1154 			len -= sfd.size_user;
1155 		}
1156 	}
1157 
1158 	release_sock(sk);
1159 
1160 	sfd.num_subflows = sfcount;
1161 
1162 	if (mptcp_put_subflow_data(&sfd, optval, copied, optlen))
1163 		return -EFAULT;
1164 
1165 	return 0;
1166 }
1167 
1168 static int mptcp_get_full_info(struct mptcp_full_info *mfi,
1169 			       char __user *optval,
1170 			       int __user *optlen)
1171 {
1172 	int len;
1173 
1174 	BUILD_BUG_ON(offsetof(struct mptcp_full_info, mptcp_info) !=
1175 		     MIN_FULL_INFO_OPTLEN_SIZE);
1176 
1177 	if (get_user(len, optlen))
1178 		return -EFAULT;
1179 
1180 	if (len < MIN_FULL_INFO_OPTLEN_SIZE)
1181 		return -EINVAL;
1182 
1183 	memset(mfi, 0, sizeof(*mfi));
1184 	if (copy_from_user(mfi, optval, MIN_FULL_INFO_OPTLEN_SIZE))
1185 		return -EFAULT;
1186 
1187 	if (mfi->size_tcpinfo_kernel ||
1188 	    mfi->size_sfinfo_kernel ||
1189 	    mfi->num_subflows)
1190 		return -EINVAL;
1191 
1192 	if (mfi->size_sfinfo_user > INT_MAX ||
1193 	    mfi->size_tcpinfo_user > INT_MAX)
1194 		return -EINVAL;
1195 
1196 	return len - MIN_FULL_INFO_OPTLEN_SIZE;
1197 }
1198 
1199 static int mptcp_put_full_info(struct mptcp_full_info *mfi,
1200 			       char __user *optval,
1201 			       u32 copylen,
1202 			       int __user *optlen)
1203 {
1204 	copylen += MIN_FULL_INFO_OPTLEN_SIZE;
1205 	if (put_user(copylen, optlen))
1206 		return -EFAULT;
1207 
1208 	if (copy_to_user(optval, mfi, copylen))
1209 		return -EFAULT;
1210 	return 0;
1211 }
1212 
1213 static int mptcp_getsockopt_full_info(struct mptcp_sock *msk, char __user *optval,
1214 				      int __user *optlen)
1215 {
1216 	unsigned int sfcount = 0, copylen = 0;
1217 	struct mptcp_subflow_context *subflow;
1218 	struct sock *sk = (struct sock *)msk;
1219 	void __user *tcpinfoptr, *sfinfoptr;
1220 	struct mptcp_full_info mfi;
1221 	int len;
1222 
1223 	len = mptcp_get_full_info(&mfi, optval, optlen);
1224 	if (len < 0)
1225 		return len;
1226 
1227 	/* don't bother filling the mptcp info if there is not enough
1228 	 * user-space-provided storage
1229 	 */
1230 	if (len > 0) {
1231 		mptcp_diag_fill_info(msk, &mfi.mptcp_info);
1232 		copylen += min_t(unsigned int, len, sizeof(struct mptcp_info));
1233 	}
1234 
1235 	mfi.size_tcpinfo_kernel = sizeof(struct tcp_info);
1236 	mfi.size_tcpinfo_user = min_t(unsigned int, mfi.size_tcpinfo_user,
1237 				      sizeof(struct tcp_info));
1238 	sfinfoptr = u64_to_user_ptr(mfi.subflow_info);
1239 	mfi.size_sfinfo_kernel = sizeof(struct mptcp_subflow_info);
1240 	mfi.size_sfinfo_user = min_t(unsigned int, mfi.size_sfinfo_user,
1241 				     sizeof(struct mptcp_subflow_info));
1242 	tcpinfoptr = u64_to_user_ptr(mfi.tcp_info);
1243 
1244 	lock_sock(sk);
1245 	mptcp_for_each_subflow(msk, subflow) {
1246 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1247 		struct mptcp_subflow_info sfinfo;
1248 		struct tcp_info tcp_info;
1249 
1250 		if (sfcount++ >= mfi.size_arrays_user)
1251 			continue;
1252 
1253 		/* fetch addr/tcp_info only if the user space buffers
1254 		 * are wide enough
1255 		 */
1256 		memset(&sfinfo, 0, sizeof(sfinfo));
1257 		sfinfo.id = subflow->subflow_id;
1258 		if (mfi.size_sfinfo_user >
1259 		    offsetof(struct mptcp_subflow_info, addrs))
1260 			mptcp_get_sub_addrs(ssk, &sfinfo.addrs);
1261 		if (copy_to_user(sfinfoptr, &sfinfo, mfi.size_sfinfo_user))
1262 			goto fail_release;
1263 
1264 		if (mfi.size_tcpinfo_user) {
1265 			tcp_get_info(ssk, &tcp_info);
1266 			if (copy_to_user(tcpinfoptr, &tcp_info,
1267 					 mfi.size_tcpinfo_user))
1268 				goto fail_release;
1269 		}
1270 
1271 		tcpinfoptr += mfi.size_tcpinfo_user;
1272 		sfinfoptr += mfi.size_sfinfo_user;
1273 	}
1274 	release_sock(sk);
1275 
1276 	mfi.num_subflows = sfcount;
1277 	if (mptcp_put_full_info(&mfi, optval, copylen, optlen))
1278 		return -EFAULT;
1279 
1280 	return 0;
1281 
1282 fail_release:
1283 	release_sock(sk);
1284 	return -EFAULT;
1285 }
1286 
1287 static int mptcp_put_int_option(struct mptcp_sock *msk, char __user *optval,
1288 				int __user *optlen, int val)
1289 {
1290 	int len;
1291 
1292 	if (get_user(len, optlen))
1293 		return -EFAULT;
1294 	if (len < 0)
1295 		return -EINVAL;
1296 
1297 	if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
1298 		unsigned char ucval = (unsigned char)val;
1299 
1300 		len = 1;
1301 		if (put_user(len, optlen))
1302 			return -EFAULT;
1303 		if (copy_to_user(optval, &ucval, 1))
1304 			return -EFAULT;
1305 	} else {
1306 		len = min_t(unsigned int, len, sizeof(int));
1307 		if (put_user(len, optlen))
1308 			return -EFAULT;
1309 		if (copy_to_user(optval, &val, len))
1310 			return -EFAULT;
1311 	}
1312 
1313 	return 0;
1314 }
1315 
1316 static int mptcp_getsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
1317 				    char __user *optval, int __user *optlen)
1318 {
1319 	switch (optname) {
1320 	case TCP_ULP:
1321 	case TCP_CONGESTION:
1322 	case TCP_INFO:
1323 	case TCP_CC_INFO:
1324 	case TCP_DEFER_ACCEPT:
1325 	case TCP_FASTOPEN:
1326 	case TCP_FASTOPEN_CONNECT:
1327 	case TCP_FASTOPEN_KEY:
1328 	case TCP_FASTOPEN_NO_COOKIE:
1329 		return mptcp_getsockopt_first_sf_only(msk, SOL_TCP, optname,
1330 						      optval, optlen);
1331 	case TCP_INQ:
1332 		return mptcp_put_int_option(msk, optval, optlen, msk->recvmsg_inq);
1333 	case TCP_CORK:
1334 		return mptcp_put_int_option(msk, optval, optlen, msk->cork);
1335 	case TCP_NODELAY:
1336 		return mptcp_put_int_option(msk, optval, optlen, msk->nodelay);
1337 	}
1338 	return -EOPNOTSUPP;
1339 }
1340 
1341 static int mptcp_getsockopt_v4(struct mptcp_sock *msk, int optname,
1342 			       char __user *optval, int __user *optlen)
1343 {
1344 	struct sock *sk = (void *)msk;
1345 
1346 	switch (optname) {
1347 	case IP_TOS:
1348 		return mptcp_put_int_option(msk, optval, optlen, inet_sk(sk)->tos);
1349 	}
1350 
1351 	return -EOPNOTSUPP;
1352 }
1353 
1354 static int mptcp_getsockopt_sol_mptcp(struct mptcp_sock *msk, int optname,
1355 				      char __user *optval, int __user *optlen)
1356 {
1357 	switch (optname) {
1358 	case MPTCP_INFO:
1359 		return mptcp_getsockopt_info(msk, optval, optlen);
1360 	case MPTCP_FULL_INFO:
1361 		return mptcp_getsockopt_full_info(msk, optval, optlen);
1362 	case MPTCP_TCPINFO:
1363 		return mptcp_getsockopt_tcpinfo(msk, optval, optlen);
1364 	case MPTCP_SUBFLOW_ADDRS:
1365 		return mptcp_getsockopt_subflow_addrs(msk, optval, optlen);
1366 	}
1367 
1368 	return -EOPNOTSUPP;
1369 }
1370 
1371 int mptcp_getsockopt(struct sock *sk, int level, int optname,
1372 		     char __user *optval, int __user *option)
1373 {
1374 	struct mptcp_sock *msk = mptcp_sk(sk);
1375 	struct sock *ssk;
1376 
1377 	pr_debug("msk=%p", msk);
1378 
1379 	/* @@ the meaning of setsockopt() when the socket is connected and
1380 	 * there are multiple subflows is not yet defined. It is up to the
1381 	 * MPTCP-level socket to configure the subflows until the subflow
1382 	 * is in TCP fallback, when socket options are passed through
1383 	 * to the one remaining subflow.
1384 	 */
1385 	lock_sock(sk);
1386 	ssk = __mptcp_tcp_fallback(msk);
1387 	release_sock(sk);
1388 	if (ssk)
1389 		return tcp_getsockopt(ssk, level, optname, optval, option);
1390 
1391 	if (level == SOL_IP)
1392 		return mptcp_getsockopt_v4(msk, optname, optval, option);
1393 	if (level == SOL_TCP)
1394 		return mptcp_getsockopt_sol_tcp(msk, optname, optval, option);
1395 	if (level == SOL_MPTCP)
1396 		return mptcp_getsockopt_sol_mptcp(msk, optname, optval, option);
1397 	return -EOPNOTSUPP;
1398 }
1399 
1400 static void sync_socket_options(struct mptcp_sock *msk, struct sock *ssk)
1401 {
1402 	static const unsigned int tx_rx_locks = SOCK_RCVBUF_LOCK | SOCK_SNDBUF_LOCK;
1403 	struct sock *sk = (struct sock *)msk;
1404 
1405 	if (ssk->sk_prot->keepalive) {
1406 		if (sock_flag(sk, SOCK_KEEPOPEN))
1407 			ssk->sk_prot->keepalive(ssk, 1);
1408 		else
1409 			ssk->sk_prot->keepalive(ssk, 0);
1410 	}
1411 
1412 	ssk->sk_priority = sk->sk_priority;
1413 	ssk->sk_bound_dev_if = sk->sk_bound_dev_if;
1414 	ssk->sk_incoming_cpu = sk->sk_incoming_cpu;
1415 	ssk->sk_ipv6only = sk->sk_ipv6only;
1416 	__ip_sock_set_tos(ssk, inet_sk(sk)->tos);
1417 
1418 	if (sk->sk_userlocks & tx_rx_locks) {
1419 		ssk->sk_userlocks |= sk->sk_userlocks & tx_rx_locks;
1420 		if (sk->sk_userlocks & SOCK_SNDBUF_LOCK) {
1421 			WRITE_ONCE(ssk->sk_sndbuf, sk->sk_sndbuf);
1422 			mptcp_subflow_ctx(ssk)->cached_sndbuf = sk->sk_sndbuf;
1423 		}
1424 		if (sk->sk_userlocks & SOCK_RCVBUF_LOCK)
1425 			WRITE_ONCE(ssk->sk_rcvbuf, sk->sk_rcvbuf);
1426 	}
1427 
1428 	if (sock_flag(sk, SOCK_LINGER)) {
1429 		ssk->sk_lingertime = sk->sk_lingertime;
1430 		sock_set_flag(ssk, SOCK_LINGER);
1431 	} else {
1432 		sock_reset_flag(ssk, SOCK_LINGER);
1433 	}
1434 
1435 	if (sk->sk_mark != ssk->sk_mark) {
1436 		ssk->sk_mark = sk->sk_mark;
1437 		sk_dst_reset(ssk);
1438 	}
1439 
1440 	sock_valbool_flag(ssk, SOCK_DBG, sock_flag(sk, SOCK_DBG));
1441 
1442 	if (inet_csk(sk)->icsk_ca_ops != inet_csk(ssk)->icsk_ca_ops)
1443 		tcp_set_congestion_control(ssk, msk->ca_name, false, true);
1444 	__tcp_sock_set_cork(ssk, !!msk->cork);
1445 	__tcp_sock_set_nodelay(ssk, !!msk->nodelay);
1446 
1447 	inet_assign_bit(TRANSPARENT, ssk, inet_test_bit(TRANSPARENT, sk));
1448 	inet_assign_bit(FREEBIND, ssk, inet_test_bit(FREEBIND, sk));
1449 }
1450 
1451 static void __mptcp_sockopt_sync(struct mptcp_sock *msk, struct sock *ssk)
1452 {
1453 	bool slow = lock_sock_fast(ssk);
1454 
1455 	sync_socket_options(msk, ssk);
1456 
1457 	unlock_sock_fast(ssk, slow);
1458 }
1459 
1460 void mptcp_sockopt_sync(struct mptcp_sock *msk, struct sock *ssk)
1461 {
1462 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1463 
1464 	msk_owned_by_me(msk);
1465 
1466 	if (READ_ONCE(subflow->setsockopt_seq) != msk->setsockopt_seq) {
1467 		__mptcp_sockopt_sync(msk, ssk);
1468 
1469 		subflow->setsockopt_seq = msk->setsockopt_seq;
1470 	}
1471 }
1472 
1473 void mptcp_sockopt_sync_locked(struct mptcp_sock *msk, struct sock *ssk)
1474 {
1475 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1476 
1477 	msk_owned_by_me(msk);
1478 
1479 	if (READ_ONCE(subflow->setsockopt_seq) != msk->setsockopt_seq) {
1480 		sync_socket_options(msk, ssk);
1481 
1482 		subflow->setsockopt_seq = msk->setsockopt_seq;
1483 	}
1484 }
1485