• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
__mptcp_tcp_fallback(struct mptcp_sock * msk)19 static struct sock *__mptcp_tcp_fallback(struct mptcp_sock *msk)
20 {
21 	sock_owned_by_me((const struct sock *)msk);
22 
23 	if (likely(!__mptcp_check_fallback(msk)))
24 		return NULL;
25 
26 	return msk->first;
27 }
28 
sockopt_seq_reset(const struct sock * sk)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 
sockopt_seq_inc(struct mptcp_sock * msk)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 
mptcp_get_int_option(struct mptcp_sock * msk,sockptr_t optval,unsigned int optlen,int * val)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 
mptcp_sol_socket_sync_intval(struct mptcp_sock * msk,int optname,int val)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 				WRITE_ONCE(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 
mptcp_sol_socket_intval(struct mptcp_sock * msk,int optname,int val)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 
mptcp_so_incoming_cpu(struct mptcp_sock * msk,int val)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 
mptcp_setsockopt_sol_socket_tstamp(struct mptcp_sock * msk,int optname,int val)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 
mptcp_setsockopt_sol_socket_int(struct mptcp_sock * msk,int optname,sockptr_t optval,unsigned int optlen)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 
mptcp_setsockopt_sol_socket_timestamping(struct mptcp_sock * msk,int optname,sockptr_t optval,unsigned int optlen)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 
mptcp_setsockopt_sol_socket_linger(struct mptcp_sock * msk,sockptr_t optval,unsigned int optlen)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 
mptcp_setsockopt_sol_socket(struct mptcp_sock * msk,int optname,sockptr_t optval,unsigned int optlen)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 (!ssock) {
305 			release_sock(sk);
306 			return -EINVAL;
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_PASSSEC:
359 	case SO_RXQ_OVFL:
360 	case SO_WIFI_STATUS:
361 	case SO_NOFCS:
362 	case SO_SELECT_ERR_QUEUE:
363 		return 0;
364 	}
365 
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
370 	 *
371 	 * SO_ATTACH_REUSEPORT_EBPF is not supported, at it checks
372 	 * explicitly the sk_protocol field
373 	 *
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
378 	 *
379 	 * SO_ZEROCOPY is currently unsupported, TODO in sndmsg
380 	 * SO_TXTIME is currently unsupported
381 	 */
382 
383 	return -EOPNOTSUPP;
384 }
385 
mptcp_setsockopt_v6(struct mptcp_sock * msk,int optname,sockptr_t optval,unsigned int optlen)386 static int mptcp_setsockopt_v6(struct mptcp_sock *msk, int optname,
387 			       sockptr_t optval, unsigned int optlen)
388 {
389 	struct sock *sk = (struct sock *)msk;
390 	int ret = -EOPNOTSUPP;
391 	struct socket *ssock;
392 
393 	switch (optname) {
394 	case IPV6_V6ONLY:
395 	case IPV6_TRANSPARENT:
396 	case IPV6_FREEBIND:
397 		lock_sock(sk);
398 		ssock = __mptcp_nmpc_socket(msk);
399 		if (!ssock) {
400 			release_sock(sk);
401 			return -EINVAL;
402 		}
403 
404 		ret = tcp_setsockopt(ssock->sk, SOL_IPV6, optname, optval, optlen);
405 		if (ret != 0) {
406 			release_sock(sk);
407 			return ret;
408 		}
409 
410 		sockopt_seq_inc(msk);
411 
412 		switch (optname) {
413 		case IPV6_V6ONLY:
414 			sk->sk_ipv6only = ssock->sk->sk_ipv6only;
415 			break;
416 		case IPV6_TRANSPARENT:
417 			inet_sk(sk)->transparent = inet_sk(ssock->sk)->transparent;
418 			break;
419 		case IPV6_FREEBIND:
420 			inet_sk(sk)->freebind = inet_sk(ssock->sk)->freebind;
421 			break;
422 		}
423 
424 		release_sock(sk);
425 		break;
426 	}
427 
428 	return ret;
429 }
430 
mptcp_supported_sockopt(int level,int optname)431 static bool mptcp_supported_sockopt(int level, int optname)
432 {
433 	if (level == SOL_IP) {
434 		switch (optname) {
435 		/* should work fine */
436 		case IP_FREEBIND:
437 		case IP_TRANSPARENT:
438 
439 		/* the following are control cmsg related */
440 		case IP_PKTINFO:
441 		case IP_RECVTTL:
442 		case IP_RECVTOS:
443 		case IP_RECVOPTS:
444 		case IP_RETOPTS:
445 		case IP_PASSSEC:
446 		case IP_RECVORIGDSTADDR:
447 		case IP_CHECKSUM:
448 		case IP_RECVFRAGSIZE:
449 
450 		/* common stuff that need some love */
451 		case IP_TOS:
452 		case IP_TTL:
453 		case IP_BIND_ADDRESS_NO_PORT:
454 		case IP_MTU_DISCOVER:
455 		case IP_RECVERR:
456 
457 		/* possibly less common may deserve some love */
458 		case IP_MINTTL:
459 
460 		/* the following is apparently a no-op for plain TCP */
461 		case IP_RECVERR_RFC4884:
462 			return true;
463 		}
464 
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
473 		 * with mcast stuff
474 		 */
475 		/* IP_IPSEC_POLICY, IP_XFRM_POLICY are nut supported, unrelated here */
476 		return false;
477 	}
478 	if (level == SOL_IPV6) {
479 		switch (optname) {
480 		case IPV6_V6ONLY:
481 
482 		/* the following are control cmsg related */
483 		case IPV6_RECVPKTINFO:
484 		case IPV6_2292PKTINFO:
485 		case IPV6_RECVHOPLIMIT:
486 		case IPV6_2292HOPLIMIT:
487 		case IPV6_RECVRTHDR:
488 		case IPV6_2292RTHDR:
489 		case IPV6_RECVHOPOPTS:
490 		case IPV6_2292HOPOPTS:
491 		case IPV6_RECVDSTOPTS:
492 		case IPV6_2292DSTOPTS:
493 		case IPV6_RECVTCLASS:
494 		case IPV6_FLOWINFO:
495 		case IPV6_RECVPATHMTU:
496 		case IPV6_RECVORIGDSTADDR:
497 		case IPV6_RECVFRAGSIZE:
498 
499 		/* the following ones need some love but are quite common */
500 		case IPV6_TCLASS:
501 		case IPV6_TRANSPARENT:
502 		case IPV6_FREEBIND:
503 		case IPV6_PKTINFO:
504 		case IPV6_2292PKTOPTIONS:
505 		case IPV6_UNICAST_HOPS:
506 		case IPV6_MTU_DISCOVER:
507 		case IPV6_MTU:
508 		case IPV6_RECVERR:
509 		case IPV6_FLOWINFO_SEND:
510 		case IPV6_FLOWLABEL_MGR:
511 		case IPV6_MINHOPCOUNT:
512 		case IPV6_DONTFRAG:
513 		case IPV6_AUTOFLOWLABEL:
514 
515 		/* the following one is a no-op for plain TCP */
516 		case IPV6_RECVERR_RFC4884:
517 			return true;
518 		}
519 
520 		/* IPV6_HOPOPTS, IPV6_RTHDRDSTOPTS, IPV6_RTHDR, IPV6_DSTOPTS are
521 		 * not supported
522 		 */
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
530 		 */
531 		/* IPV6_ROUTER_ALERT, IPV6_ROUTER_ALERT_ISOLATE are not supported, since are evil */
532 
533 		/* IPV6_IPSEC_POLICY, IPV6_XFRM_POLICY are not supported */
534 		/* IPV6_ADDR_PREFERENCES is not supported, we must be careful with subflows */
535 		return false;
536 	}
537 	if (level == SOL_TCP) {
538 		switch (optname) {
539 		/* the following are no-op or should work just fine */
540 		case TCP_THIN_DUPACK:
541 		case TCP_DEFER_ACCEPT:
542 
543 		/* the following need some love */
544 		case TCP_MAXSEG:
545 		case TCP_NODELAY:
546 		case TCP_THIN_LINEAR_TIMEOUTS:
547 		case TCP_CONGESTION:
548 		case TCP_CORK:
549 		case TCP_KEEPIDLE:
550 		case TCP_KEEPINTVL:
551 		case TCP_KEEPCNT:
552 		case TCP_SYNCNT:
553 		case TCP_SAVE_SYN:
554 		case TCP_LINGER2:
555 		case TCP_WINDOW_CLAMP:
556 		case TCP_QUICKACK:
557 		case TCP_USER_TIMEOUT:
558 		case TCP_TIMESTAMP:
559 		case TCP_NOTSENT_LOWAT:
560 		case TCP_TX_DELAY:
561 		case TCP_INQ:
562 		case TCP_FASTOPEN_CONNECT:
563 			return true;
564 		}
565 
566 		/* TCP_MD5SIG, TCP_MD5SIG_EXT are not supported, MD5 is not compatible with MPTCP */
567 
568 		/* TCP_REPAIR, TCP_REPAIR_QUEUE, TCP_QUEUE_SEQ, TCP_REPAIR_OPTIONS,
569 		 * TCP_REPAIR_WINDOW are not supported, better avoid this mess
570 		 */
571 		/* TCP_FASTOPEN_KEY, TCP_FASTOPEN, TCP_FASTOPEN_NO_COOKIE,
572 		 * are not supported fastopen is currently unsupported
573 		 */
574 	}
575 	return false;
576 }
577 
mptcp_setsockopt_sol_tcp_congestion(struct mptcp_sock * msk,sockptr_t optval,unsigned int optlen)578 static int mptcp_setsockopt_sol_tcp_congestion(struct mptcp_sock *msk, sockptr_t optval,
579 					       unsigned int optlen)
580 {
581 	struct mptcp_subflow_context *subflow;
582 	struct sock *sk = (struct sock *)msk;
583 	char name[TCP_CA_NAME_MAX];
584 	bool cap_net_admin;
585 	int ret;
586 
587 	if (optlen < 1)
588 		return -EINVAL;
589 
590 	ret = strncpy_from_sockptr(name, optval,
591 				   min_t(long, TCP_CA_NAME_MAX - 1, optlen));
592 	if (ret < 0)
593 		return -EFAULT;
594 
595 	name[ret] = 0;
596 
597 	cap_net_admin = ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN);
598 
599 	ret = 0;
600 	lock_sock(sk);
601 	sockopt_seq_inc(msk);
602 	mptcp_for_each_subflow(msk, subflow) {
603 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
604 		int err;
605 
606 		lock_sock(ssk);
607 		err = tcp_set_congestion_control(ssk, name, true, cap_net_admin);
608 		if (err < 0 && ret == 0)
609 			ret = err;
610 		subflow->setsockopt_seq = msk->setsockopt_seq;
611 		release_sock(ssk);
612 	}
613 
614 	if (ret == 0)
615 		strcpy(msk->ca_name, name);
616 
617 	release_sock(sk);
618 	return ret;
619 }
620 
mptcp_setsockopt_sol_tcp_cork(struct mptcp_sock * msk,sockptr_t optval,unsigned int optlen)621 static int mptcp_setsockopt_sol_tcp_cork(struct mptcp_sock *msk, sockptr_t optval,
622 					 unsigned int optlen)
623 {
624 	struct mptcp_subflow_context *subflow;
625 	struct sock *sk = (struct sock *)msk;
626 	int val;
627 
628 	if (optlen < sizeof(int))
629 		return -EINVAL;
630 
631 	if (copy_from_sockptr(&val, optval, sizeof(val)))
632 		return -EFAULT;
633 
634 	lock_sock(sk);
635 	sockopt_seq_inc(msk);
636 	msk->cork = !!val;
637 	mptcp_for_each_subflow(msk, subflow) {
638 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
639 
640 		lock_sock(ssk);
641 		__tcp_sock_set_cork(ssk, !!val);
642 		release_sock(ssk);
643 	}
644 	if (!val)
645 		mptcp_check_and_set_pending(sk);
646 	release_sock(sk);
647 
648 	return 0;
649 }
650 
mptcp_setsockopt_sol_tcp_nodelay(struct mptcp_sock * msk,sockptr_t optval,unsigned int optlen)651 static int mptcp_setsockopt_sol_tcp_nodelay(struct mptcp_sock *msk, sockptr_t optval,
652 					    unsigned int optlen)
653 {
654 	struct mptcp_subflow_context *subflow;
655 	struct sock *sk = (struct sock *)msk;
656 	int val;
657 
658 	if (optlen < sizeof(int))
659 		return -EINVAL;
660 
661 	if (copy_from_sockptr(&val, optval, sizeof(val)))
662 		return -EFAULT;
663 
664 	lock_sock(sk);
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);
669 
670 		lock_sock(ssk);
671 		__tcp_sock_set_nodelay(ssk, !!val);
672 		release_sock(ssk);
673 	}
674 	if (val)
675 		mptcp_check_and_set_pending(sk);
676 	release_sock(sk);
677 
678 	return 0;
679 }
680 
mptcp_setsockopt_sol_ip_set_transparent(struct mptcp_sock * msk,int optname,sockptr_t optval,unsigned int optlen)681 static int mptcp_setsockopt_sol_ip_set_transparent(struct mptcp_sock *msk, int optname,
682 						   sockptr_t optval, unsigned int optlen)
683 {
684 	struct sock *sk = (struct sock *)msk;
685 	struct inet_sock *issk;
686 	struct socket *ssock;
687 	int err;
688 
689 	err = ip_setsockopt(sk, SOL_IP, optname, optval, optlen);
690 	if (err != 0)
691 		return err;
692 
693 	lock_sock(sk);
694 
695 	ssock = __mptcp_nmpc_socket(msk);
696 	if (!ssock) {
697 		release_sock(sk);
698 		return -EINVAL;
699 	}
700 
701 	issk = inet_sk(ssock->sk);
702 
703 	switch (optname) {
704 	case IP_FREEBIND:
705 		issk->freebind = inet_sk(sk)->freebind;
706 		break;
707 	case IP_TRANSPARENT:
708 		issk->transparent = inet_sk(sk)->transparent;
709 		break;
710 	default:
711 		release_sock(sk);
712 		WARN_ON_ONCE(1);
713 		return -EOPNOTSUPP;
714 	}
715 
716 	sockopt_seq_inc(msk);
717 	release_sock(sk);
718 	return 0;
719 }
720 
mptcp_setsockopt_v4_set_tos(struct mptcp_sock * msk,int optname,sockptr_t optval,unsigned int optlen)721 static int mptcp_setsockopt_v4_set_tos(struct mptcp_sock *msk, int optname,
722 				       sockptr_t optval, unsigned int optlen)
723 {
724 	struct mptcp_subflow_context *subflow;
725 	struct sock *sk = (struct sock *)msk;
726 	int err, val;
727 
728 	err = ip_setsockopt(sk, SOL_IP, optname, optval, optlen);
729 
730 	if (err != 0)
731 		return err;
732 
733 	lock_sock(sk);
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);
738 		bool slow;
739 
740 		slow = lock_sock_fast(ssk);
741 		__ip_sock_set_tos(ssk, val);
742 		unlock_sock_fast(ssk, slow);
743 	}
744 	release_sock(sk);
745 
746 	return err;
747 }
748 
mptcp_setsockopt_v4(struct mptcp_sock * msk,int optname,sockptr_t optval,unsigned int optlen)749 static int mptcp_setsockopt_v4(struct mptcp_sock *msk, int optname,
750 			       sockptr_t optval, unsigned int optlen)
751 {
752 	switch (optname) {
753 	case IP_FREEBIND:
754 	case IP_TRANSPARENT:
755 		return mptcp_setsockopt_sol_ip_set_transparent(msk, optname, optval, optlen);
756 	case IP_TOS:
757 		return mptcp_setsockopt_v4_set_tos(msk, optname, optval, optlen);
758 	}
759 
760 	return -EOPNOTSUPP;
761 }
762 
mptcp_setsockopt_sol_tcp_defer(struct mptcp_sock * msk,sockptr_t optval,unsigned int optlen)763 static int mptcp_setsockopt_sol_tcp_defer(struct mptcp_sock *msk, sockptr_t optval,
764 					  unsigned int optlen)
765 {
766 	struct socket *listener;
767 
768 	listener = __mptcp_nmpc_socket(msk);
769 	if (!listener)
770 		return 0; /* TCP_DEFER_ACCEPT does not fail */
771 
772 	return tcp_setsockopt(listener->sk, SOL_TCP, TCP_DEFER_ACCEPT, optval, optlen);
773 }
774 
mptcp_setsockopt_first_sf_only(struct mptcp_sock * msk,int level,int optname,sockptr_t optval,unsigned int optlen)775 static int mptcp_setsockopt_first_sf_only(struct mptcp_sock *msk, int level, int optname,
776 					  sockptr_t optval, unsigned int optlen)
777 {
778 	struct sock *sk = (struct sock *)msk;
779 	struct socket *sock;
780 	int ret = -EINVAL;
781 
782 	/* Limit to first subflow, before the connection establishment */
783 	lock_sock(sk);
784 	sock = __mptcp_nmpc_socket(msk);
785 	if (!sock)
786 		goto unlock;
787 
788 	ret = tcp_setsockopt(sock->sk, level, optname, optval, optlen);
789 
790 unlock:
791 	release_sock(sk);
792 	return ret;
793 }
794 
mptcp_setsockopt_sol_tcp(struct mptcp_sock * msk,int optname,sockptr_t optval,unsigned int optlen)795 static int mptcp_setsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
796 				    sockptr_t optval, unsigned int optlen)
797 {
798 	struct sock *sk = (void *)msk;
799 	int ret, val;
800 
801 	switch (optname) {
802 	case TCP_INQ:
803 		ret = mptcp_get_int_option(msk, optval, optlen, &val);
804 		if (ret)
805 			return ret;
806 		if (val < 0 || val > 1)
807 			return -EINVAL;
808 
809 		lock_sock(sk);
810 		msk->recvmsg_inq = !!val;
811 		release_sock(sk);
812 		return 0;
813 	case TCP_ULP:
814 		return -EOPNOTSUPP;
815 	case TCP_CONGESTION:
816 		return mptcp_setsockopt_sol_tcp_congestion(msk, optval, optlen);
817 	case TCP_CORK:
818 		return mptcp_setsockopt_sol_tcp_cork(msk, optval, optlen);
819 	case TCP_NODELAY:
820 		return mptcp_setsockopt_sol_tcp_nodelay(msk, optval, optlen);
821 	case TCP_DEFER_ACCEPT:
822 		return mptcp_setsockopt_sol_tcp_defer(msk, optval, optlen);
823 	case TCP_FASTOPEN_CONNECT:
824 		return mptcp_setsockopt_first_sf_only(msk, SOL_TCP, optname,
825 						      optval, optlen);
826 	}
827 
828 	return -EOPNOTSUPP;
829 }
830 
mptcp_setsockopt(struct sock * sk,int level,int optname,sockptr_t optval,unsigned int optlen)831 int mptcp_setsockopt(struct sock *sk, int level, int optname,
832 		     sockptr_t optval, unsigned int optlen)
833 {
834 	struct mptcp_sock *msk = mptcp_sk(sk);
835 	struct sock *ssk;
836 
837 	pr_debug("msk=%p", msk);
838 
839 	if (level == SOL_SOCKET)
840 		return mptcp_setsockopt_sol_socket(msk, optname, optval, optlen);
841 
842 	if (!mptcp_supported_sockopt(level, optname))
843 		return -ENOPROTOOPT;
844 
845 	/* @@ the meaning of setsockopt() when the socket is connected and
846 	 * there are multiple subflows is not yet defined. It is up to the
847 	 * MPTCP-level socket to configure the subflows until the subflow
848 	 * is in TCP fallback, when TCP socket options are passed through
849 	 * to the one remaining subflow.
850 	 */
851 	lock_sock(sk);
852 	ssk = __mptcp_tcp_fallback(msk);
853 	release_sock(sk);
854 	if (ssk)
855 		return tcp_setsockopt(ssk, level, optname, optval, optlen);
856 
857 	if (level == SOL_IP)
858 		return mptcp_setsockopt_v4(msk, optname, optval, optlen);
859 
860 	if (level == SOL_IPV6)
861 		return mptcp_setsockopt_v6(msk, optname, optval, optlen);
862 
863 	if (level == SOL_TCP)
864 		return mptcp_setsockopt_sol_tcp(msk, optname, optval, optlen);
865 
866 	return -EOPNOTSUPP;
867 }
868 
mptcp_getsockopt_first_sf_only(struct mptcp_sock * msk,int level,int optname,char __user * optval,int __user * optlen)869 static int mptcp_getsockopt_first_sf_only(struct mptcp_sock *msk, int level, int optname,
870 					  char __user *optval, int __user *optlen)
871 {
872 	struct sock *sk = (struct sock *)msk;
873 	struct socket *ssock;
874 	int ret = -EINVAL;
875 	struct sock *ssk;
876 
877 	lock_sock(sk);
878 	ssk = msk->first;
879 	if (ssk) {
880 		ret = tcp_getsockopt(ssk, level, optname, optval, optlen);
881 		goto out;
882 	}
883 
884 	ssock = __mptcp_nmpc_socket(msk);
885 	if (!ssock)
886 		goto out;
887 
888 	ret = tcp_getsockopt(ssock->sk, level, optname, optval, optlen);
889 
890 out:
891 	release_sock(sk);
892 	return ret;
893 }
894 
mptcp_diag_fill_info(struct mptcp_sock * msk,struct mptcp_info * info)895 void mptcp_diag_fill_info(struct mptcp_sock *msk, struct mptcp_info *info)
896 {
897 	u32 flags = 0;
898 	u8 val;
899 
900 	memset(info, 0, sizeof(*info));
901 
902 	info->mptcpi_subflows = READ_ONCE(msk->pm.subflows);
903 	info->mptcpi_add_addr_signal = READ_ONCE(msk->pm.add_addr_signaled);
904 	info->mptcpi_add_addr_accepted = READ_ONCE(msk->pm.add_addr_accepted);
905 	info->mptcpi_local_addr_used = READ_ONCE(msk->pm.local_addr_used);
906 	info->mptcpi_subflows_max = mptcp_pm_get_subflows_max(msk);
907 	val = mptcp_pm_get_add_addr_signal_max(msk);
908 	info->mptcpi_add_addr_signal_max = val;
909 	val = mptcp_pm_get_add_addr_accept_max(msk);
910 	info->mptcpi_add_addr_accepted_max = val;
911 	info->mptcpi_local_addr_max = mptcp_pm_get_local_addr_max(msk);
912 	if (test_bit(MPTCP_FALLBACK_DONE, &msk->flags))
913 		flags |= MPTCP_INFO_FLAG_FALLBACK;
914 	if (READ_ONCE(msk->can_ack))
915 		flags |= MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED;
916 	info->mptcpi_flags = flags;
917 	info->mptcpi_token = READ_ONCE(msk->token);
918 	info->mptcpi_write_seq = READ_ONCE(msk->write_seq);
919 	info->mptcpi_snd_una = READ_ONCE(msk->snd_una);
920 	info->mptcpi_rcv_nxt = READ_ONCE(msk->ack_seq);
921 	info->mptcpi_csum_enabled = READ_ONCE(msk->csum_enabled);
922 }
923 EXPORT_SYMBOL_GPL(mptcp_diag_fill_info);
924 
mptcp_getsockopt_info(struct mptcp_sock * msk,char __user * optval,int __user * optlen)925 static int mptcp_getsockopt_info(struct mptcp_sock *msk, char __user *optval, int __user *optlen)
926 {
927 	struct mptcp_info m_info;
928 	int len;
929 
930 	if (get_user(len, optlen))
931 		return -EFAULT;
932 
933 	len = min_t(unsigned int, len, sizeof(struct mptcp_info));
934 
935 	mptcp_diag_fill_info(msk, &m_info);
936 
937 	if (put_user(len, optlen))
938 		return -EFAULT;
939 
940 	if (copy_to_user(optval, &m_info, len))
941 		return -EFAULT;
942 
943 	return 0;
944 }
945 
mptcp_put_subflow_data(struct mptcp_subflow_data * sfd,char __user * optval,u32 copied,int __user * optlen)946 static int mptcp_put_subflow_data(struct mptcp_subflow_data *sfd,
947 				  char __user *optval,
948 				  u32 copied,
949 				  int __user *optlen)
950 {
951 	u32 copylen = min_t(u32, sfd->size_subflow_data, sizeof(*sfd));
952 
953 	if (copied)
954 		copied += sfd->size_subflow_data;
955 	else
956 		copied = copylen;
957 
958 	if (put_user(copied, optlen))
959 		return -EFAULT;
960 
961 	if (copy_to_user(optval, sfd, copylen))
962 		return -EFAULT;
963 
964 	return 0;
965 }
966 
mptcp_get_subflow_data(struct mptcp_subflow_data * sfd,char __user * optval,int __user * optlen)967 static int mptcp_get_subflow_data(struct mptcp_subflow_data *sfd,
968 				  char __user *optval, int __user *optlen)
969 {
970 	int len, copylen;
971 
972 	if (get_user(len, optlen))
973 		return -EFAULT;
974 
975 	/* if mptcp_subflow_data size is changed, need to adjust
976 	 * this function to deal with programs using old version.
977 	 */
978 	BUILD_BUG_ON(sizeof(*sfd) != MIN_INFO_OPTLEN_SIZE);
979 
980 	if (len < MIN_INFO_OPTLEN_SIZE)
981 		return -EINVAL;
982 
983 	memset(sfd, 0, sizeof(*sfd));
984 
985 	copylen = min_t(unsigned int, len, sizeof(*sfd));
986 	if (copy_from_user(sfd, optval, copylen))
987 		return -EFAULT;
988 
989 	/* size_subflow_data is u32, but len is signed */
990 	if (sfd->size_subflow_data > INT_MAX ||
991 	    sfd->size_user > INT_MAX)
992 		return -EINVAL;
993 
994 	if (sfd->size_subflow_data < MIN_INFO_OPTLEN_SIZE ||
995 	    sfd->size_subflow_data > len)
996 		return -EINVAL;
997 
998 	if (sfd->num_subflows || sfd->size_kernel)
999 		return -EINVAL;
1000 
1001 	return len - sfd->size_subflow_data;
1002 }
1003 
mptcp_getsockopt_tcpinfo(struct mptcp_sock * msk,char __user * optval,int __user * optlen)1004 static int mptcp_getsockopt_tcpinfo(struct mptcp_sock *msk, char __user *optval,
1005 				    int __user *optlen)
1006 {
1007 	struct mptcp_subflow_context *subflow;
1008 	struct sock *sk = &msk->sk.icsk_inet.sk;
1009 	unsigned int sfcount = 0, copied = 0;
1010 	struct mptcp_subflow_data sfd;
1011 	char __user *infoptr;
1012 	int len;
1013 
1014 	len = mptcp_get_subflow_data(&sfd, optval, optlen);
1015 	if (len < 0)
1016 		return len;
1017 
1018 	sfd.size_kernel = sizeof(struct tcp_info);
1019 	sfd.size_user = min_t(unsigned int, sfd.size_user,
1020 			      sizeof(struct tcp_info));
1021 
1022 	infoptr = optval + sfd.size_subflow_data;
1023 
1024 	lock_sock(sk);
1025 
1026 	mptcp_for_each_subflow(msk, subflow) {
1027 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1028 
1029 		++sfcount;
1030 
1031 		if (len && len >= sfd.size_user) {
1032 			struct tcp_info info;
1033 
1034 			tcp_get_info(ssk, &info);
1035 
1036 			if (copy_to_user(infoptr, &info, sfd.size_user)) {
1037 				release_sock(sk);
1038 				return -EFAULT;
1039 			}
1040 
1041 			infoptr += sfd.size_user;
1042 			copied += sfd.size_user;
1043 			len -= sfd.size_user;
1044 		}
1045 	}
1046 
1047 	release_sock(sk);
1048 
1049 	sfd.num_subflows = sfcount;
1050 
1051 	if (mptcp_put_subflow_data(&sfd, optval, copied, optlen))
1052 		return -EFAULT;
1053 
1054 	return 0;
1055 }
1056 
mptcp_get_sub_addrs(const struct sock * sk,struct mptcp_subflow_addrs * a)1057 static void mptcp_get_sub_addrs(const struct sock *sk, struct mptcp_subflow_addrs *a)
1058 {
1059 	struct inet_sock *inet = inet_sk(sk);
1060 
1061 	memset(a, 0, sizeof(*a));
1062 
1063 	if (sk->sk_family == AF_INET) {
1064 		a->sin_local.sin_family = AF_INET;
1065 		a->sin_local.sin_port = inet->inet_sport;
1066 		a->sin_local.sin_addr.s_addr = inet->inet_rcv_saddr;
1067 
1068 		if (!a->sin_local.sin_addr.s_addr)
1069 			a->sin_local.sin_addr.s_addr = inet->inet_saddr;
1070 
1071 		a->sin_remote.sin_family = AF_INET;
1072 		a->sin_remote.sin_port = inet->inet_dport;
1073 		a->sin_remote.sin_addr.s_addr = inet->inet_daddr;
1074 #if IS_ENABLED(CONFIG_IPV6)
1075 	} else if (sk->sk_family == AF_INET6) {
1076 		const struct ipv6_pinfo *np = inet6_sk(sk);
1077 
1078 		if (WARN_ON_ONCE(!np))
1079 			return;
1080 
1081 		a->sin6_local.sin6_family = AF_INET6;
1082 		a->sin6_local.sin6_port = inet->inet_sport;
1083 
1084 		if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
1085 			a->sin6_local.sin6_addr = np->saddr;
1086 		else
1087 			a->sin6_local.sin6_addr = sk->sk_v6_rcv_saddr;
1088 
1089 		a->sin6_remote.sin6_family = AF_INET6;
1090 		a->sin6_remote.sin6_port = inet->inet_dport;
1091 		a->sin6_remote.sin6_addr = sk->sk_v6_daddr;
1092 #endif
1093 	}
1094 }
1095 
mptcp_getsockopt_subflow_addrs(struct mptcp_sock * msk,char __user * optval,int __user * optlen)1096 static int mptcp_getsockopt_subflow_addrs(struct mptcp_sock *msk, char __user *optval,
1097 					  int __user *optlen)
1098 {
1099 	struct sock *sk = &msk->sk.icsk_inet.sk;
1100 	struct mptcp_subflow_context *subflow;
1101 	unsigned int sfcount = 0, copied = 0;
1102 	struct mptcp_subflow_data sfd;
1103 	char __user *addrptr;
1104 	int len;
1105 
1106 	len = mptcp_get_subflow_data(&sfd, optval, optlen);
1107 	if (len < 0)
1108 		return len;
1109 
1110 	sfd.size_kernel = sizeof(struct mptcp_subflow_addrs);
1111 	sfd.size_user = min_t(unsigned int, sfd.size_user,
1112 			      sizeof(struct mptcp_subflow_addrs));
1113 
1114 	addrptr = optval + sfd.size_subflow_data;
1115 
1116 	lock_sock(sk);
1117 
1118 	mptcp_for_each_subflow(msk, subflow) {
1119 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1120 
1121 		++sfcount;
1122 
1123 		if (len && len >= sfd.size_user) {
1124 			struct mptcp_subflow_addrs a;
1125 
1126 			mptcp_get_sub_addrs(ssk, &a);
1127 
1128 			if (copy_to_user(addrptr, &a, sfd.size_user)) {
1129 				release_sock(sk);
1130 				return -EFAULT;
1131 			}
1132 
1133 			addrptr += sfd.size_user;
1134 			copied += sfd.size_user;
1135 			len -= sfd.size_user;
1136 		}
1137 	}
1138 
1139 	release_sock(sk);
1140 
1141 	sfd.num_subflows = sfcount;
1142 
1143 	if (mptcp_put_subflow_data(&sfd, optval, copied, optlen))
1144 		return -EFAULT;
1145 
1146 	return 0;
1147 }
1148 
mptcp_put_int_option(struct mptcp_sock * msk,char __user * optval,int __user * optlen,int val)1149 static int mptcp_put_int_option(struct mptcp_sock *msk, char __user *optval,
1150 				int __user *optlen, int val)
1151 {
1152 	int len;
1153 
1154 	if (get_user(len, optlen))
1155 		return -EFAULT;
1156 	if (len < 0)
1157 		return -EINVAL;
1158 
1159 	if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
1160 		unsigned char ucval = (unsigned char)val;
1161 
1162 		len = 1;
1163 		if (put_user(len, optlen))
1164 			return -EFAULT;
1165 		if (copy_to_user(optval, &ucval, 1))
1166 			return -EFAULT;
1167 	} else {
1168 		len = min_t(unsigned int, len, sizeof(int));
1169 		if (put_user(len, optlen))
1170 			return -EFAULT;
1171 		if (copy_to_user(optval, &val, len))
1172 			return -EFAULT;
1173 	}
1174 
1175 	return 0;
1176 }
1177 
mptcp_getsockopt_sol_tcp(struct mptcp_sock * msk,int optname,char __user * optval,int __user * optlen)1178 static int mptcp_getsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
1179 				    char __user *optval, int __user *optlen)
1180 {
1181 	switch (optname) {
1182 	case TCP_ULP:
1183 	case TCP_CONGESTION:
1184 	case TCP_INFO:
1185 	case TCP_CC_INFO:
1186 	case TCP_DEFER_ACCEPT:
1187 	case TCP_FASTOPEN_CONNECT:
1188 		return mptcp_getsockopt_first_sf_only(msk, SOL_TCP, optname,
1189 						      optval, optlen);
1190 	case TCP_INQ:
1191 		return mptcp_put_int_option(msk, optval, optlen, msk->recvmsg_inq);
1192 	case TCP_CORK:
1193 		return mptcp_put_int_option(msk, optval, optlen, msk->cork);
1194 	case TCP_NODELAY:
1195 		return mptcp_put_int_option(msk, optval, optlen, msk->nodelay);
1196 	}
1197 	return -EOPNOTSUPP;
1198 }
1199 
mptcp_getsockopt_v4(struct mptcp_sock * msk,int optname,char __user * optval,int __user * optlen)1200 static int mptcp_getsockopt_v4(struct mptcp_sock *msk, int optname,
1201 			       char __user *optval, int __user *optlen)
1202 {
1203 	struct sock *sk = (void *)msk;
1204 
1205 	switch (optname) {
1206 	case IP_TOS:
1207 		return mptcp_put_int_option(msk, optval, optlen, inet_sk(sk)->tos);
1208 	}
1209 
1210 	return -EOPNOTSUPP;
1211 }
1212 
mptcp_getsockopt_sol_mptcp(struct mptcp_sock * msk,int optname,char __user * optval,int __user * optlen)1213 static int mptcp_getsockopt_sol_mptcp(struct mptcp_sock *msk, int optname,
1214 				      char __user *optval, int __user *optlen)
1215 {
1216 	switch (optname) {
1217 	case MPTCP_INFO:
1218 		return mptcp_getsockopt_info(msk, optval, optlen);
1219 	case MPTCP_TCPINFO:
1220 		return mptcp_getsockopt_tcpinfo(msk, optval, optlen);
1221 	case MPTCP_SUBFLOW_ADDRS:
1222 		return mptcp_getsockopt_subflow_addrs(msk, optval, optlen);
1223 	}
1224 
1225 	return -EOPNOTSUPP;
1226 }
1227 
mptcp_getsockopt(struct sock * sk,int level,int optname,char __user * optval,int __user * option)1228 int mptcp_getsockopt(struct sock *sk, int level, int optname,
1229 		     char __user *optval, int __user *option)
1230 {
1231 	struct mptcp_sock *msk = mptcp_sk(sk);
1232 	struct sock *ssk;
1233 
1234 	pr_debug("msk=%p", msk);
1235 
1236 	/* @@ the meaning of setsockopt() when the socket is connected and
1237 	 * there are multiple subflows is not yet defined. It is up to the
1238 	 * MPTCP-level socket to configure the subflows until the subflow
1239 	 * is in TCP fallback, when socket options are passed through
1240 	 * to the one remaining subflow.
1241 	 */
1242 	lock_sock(sk);
1243 	ssk = __mptcp_tcp_fallback(msk);
1244 	release_sock(sk);
1245 	if (ssk)
1246 		return tcp_getsockopt(ssk, level, optname, optval, option);
1247 
1248 	if (level == SOL_IP)
1249 		return mptcp_getsockopt_v4(msk, optname, optval, option);
1250 	if (level == SOL_TCP)
1251 		return mptcp_getsockopt_sol_tcp(msk, optname, optval, option);
1252 	if (level == SOL_MPTCP)
1253 		return mptcp_getsockopt_sol_mptcp(msk, optname, optval, option);
1254 	return -EOPNOTSUPP;
1255 }
1256 
sync_socket_options(struct mptcp_sock * msk,struct sock * ssk)1257 static void sync_socket_options(struct mptcp_sock *msk, struct sock *ssk)
1258 {
1259 	static const unsigned int tx_rx_locks = SOCK_RCVBUF_LOCK | SOCK_SNDBUF_LOCK;
1260 	struct sock *sk = (struct sock *)msk;
1261 
1262 	if (ssk->sk_prot->keepalive) {
1263 		if (sock_flag(sk, SOCK_KEEPOPEN))
1264 			ssk->sk_prot->keepalive(ssk, 1);
1265 		else
1266 			ssk->sk_prot->keepalive(ssk, 0);
1267 	}
1268 
1269 	ssk->sk_priority = sk->sk_priority;
1270 	ssk->sk_bound_dev_if = sk->sk_bound_dev_if;
1271 	ssk->sk_incoming_cpu = sk->sk_incoming_cpu;
1272 	__ip_sock_set_tos(ssk, inet_sk(sk)->tos);
1273 
1274 	if (sk->sk_userlocks & tx_rx_locks) {
1275 		ssk->sk_userlocks |= sk->sk_userlocks & tx_rx_locks;
1276 		if (sk->sk_userlocks & SOCK_SNDBUF_LOCK)
1277 			WRITE_ONCE(ssk->sk_sndbuf, sk->sk_sndbuf);
1278 		if (sk->sk_userlocks & SOCK_RCVBUF_LOCK)
1279 			WRITE_ONCE(ssk->sk_rcvbuf, sk->sk_rcvbuf);
1280 	}
1281 
1282 	if (sock_flag(sk, SOCK_LINGER)) {
1283 		ssk->sk_lingertime = sk->sk_lingertime;
1284 		sock_set_flag(ssk, SOCK_LINGER);
1285 	} else {
1286 		sock_reset_flag(ssk, SOCK_LINGER);
1287 	}
1288 
1289 	if (sk->sk_mark != ssk->sk_mark) {
1290 		ssk->sk_mark = sk->sk_mark;
1291 		sk_dst_reset(ssk);
1292 	}
1293 
1294 	sock_valbool_flag(ssk, SOCK_DBG, sock_flag(sk, SOCK_DBG));
1295 
1296 	if (inet_csk(sk)->icsk_ca_ops != inet_csk(ssk)->icsk_ca_ops)
1297 		tcp_set_congestion_control(ssk, msk->ca_name, false, true);
1298 	__tcp_sock_set_cork(ssk, !!msk->cork);
1299 	__tcp_sock_set_nodelay(ssk, !!msk->nodelay);
1300 
1301 	inet_sk(ssk)->transparent = inet_sk(sk)->transparent;
1302 	inet_sk(ssk)->freebind = inet_sk(sk)->freebind;
1303 }
1304 
__mptcp_sockopt_sync(struct mptcp_sock * msk,struct sock * ssk)1305 static void __mptcp_sockopt_sync(struct mptcp_sock *msk, struct sock *ssk)
1306 {
1307 	bool slow = lock_sock_fast(ssk);
1308 
1309 	sync_socket_options(msk, ssk);
1310 
1311 	unlock_sock_fast(ssk, slow);
1312 }
1313 
mptcp_sockopt_sync(struct mptcp_sock * msk,struct sock * ssk)1314 void mptcp_sockopt_sync(struct mptcp_sock *msk, struct sock *ssk)
1315 {
1316 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1317 
1318 	msk_owned_by_me(msk);
1319 
1320 	if (READ_ONCE(subflow->setsockopt_seq) != msk->setsockopt_seq) {
1321 		__mptcp_sockopt_sync(msk, ssk);
1322 
1323 		subflow->setsockopt_seq = msk->setsockopt_seq;
1324 	}
1325 }
1326 
mptcp_sockopt_sync_locked(struct mptcp_sock * msk,struct sock * ssk)1327 void mptcp_sockopt_sync_locked(struct mptcp_sock *msk, struct sock *ssk)
1328 {
1329 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1330 
1331 	msk_owned_by_me(msk);
1332 
1333 	if (READ_ONCE(subflow->setsockopt_seq) != msk->setsockopt_seq) {
1334 		sync_socket_options(msk, ssk);
1335 
1336 		subflow->setsockopt_seq = msk->setsockopt_seq;
1337 	}
1338 }
1339