blob: dacf3cee00273f303e67ce0d81092f976fad2f2f [file] [log] [blame]
Paolo Abeni0abdde82021-04-15 16:44:51 -07001// 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
Florian Westphal06f15ce2021-09-17 16:33:20 -070017#define MIN_INFO_OPTLEN_SIZE 16
18
Paolo Abeni0abdde82021-04-15 16:44:51 -070019static 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
Florian Westphaldf00b082021-04-15 16:44:54 -070029static 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
Florian Westphal1b3e7ed2021-04-15 16:44:55 -070050static 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
57static 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
69static 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) {
Florian Westphala03c99b2021-04-15 16:45:00 -070082 case SO_DEBUG:
83 sock_valbool_flag(ssk, SOCK_DBG, !!val);
84 break;
Florian Westphal1b3e7ed2021-04-15 16:44:55 -070085 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;
Florian Westphal5d0a6bc82d2021-04-15 16:44:56 -070093 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;
Florian Westphal36704412021-04-15 16:44:58 -0700103 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;
Florian Westphal6f0d7192021-04-15 16:44:59 -0700109 case SO_INCOMING_CPU:
110 WRITE_ONCE(ssk->sk_incoming_cpu, val);
111 break;
Florian Westphal1b3e7ed2021-04-15 16:44:55 -0700112 }
113
114 subflow->setsockopt_seq = msk->setsockopt_seq;
115 unlock_sock_fast(ssk, slow);
116 }
117
118 release_sock(sk);
119}
120
121static 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
Florian Westphal6f0d7192021-04-15 16:44:59 -0700136static 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
Florian Westphal9061f242021-06-03 16:24:29 -0700145static 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
Yangbo Lu6c9a0a02021-06-30 16:11:58 +0800162 sock_set_timestamp(sk, optname, !!val);
Florian Westphal9061f242021-06-03 16:24:29 -0700163 unlock_sock_fast(ssk, slow);
164 }
165
166 release_sock(sk);
167 return 0;
168}
169
Florian Westphal1b3e7ed2021-04-15 16:44:55 -0700170static int mptcp_setsockopt_sol_socket_int(struct mptcp_sock *msk, int optname,
Yangbo Lu6c9a0a02021-06-30 16:11:58 +0800171 sockptr_t optval,
172 unsigned int optlen)
Florian Westphal1b3e7ed2021-04-15 16:44:55 -0700173{
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;
Florian Westphala03c99b2021-04-15 16:45:00 -0700184 case SO_DEBUG:
Florian Westphal36704412021-04-15 16:44:58 -0700185 case SO_MARK:
Florian Westphal1b3e7ed2021-04-15 16:44:55 -0700186 case SO_PRIORITY:
Florian Westphal5d0a6bc82d2021-04-15 16:44:56 -0700187 case SO_SNDBUF:
188 case SO_SNDBUFFORCE:
189 case SO_RCVBUF:
190 case SO_RCVBUFFORCE:
Florian Westphal1b3e7ed2021-04-15 16:44:55 -0700191 return mptcp_sol_socket_intval(msk, optname, val);
Florian Westphal6f0d7192021-04-15 16:44:59 -0700192 case SO_INCOMING_CPU:
193 mptcp_so_incoming_cpu(msk, val);
194 return 0;
Florian Westphal9061f242021-06-03 16:24:29 -0700195 case SO_TIMESTAMP_OLD:
196 case SO_TIMESTAMP_NEW:
197 case SO_TIMESTAMPNS_OLD:
198 case SO_TIMESTAMPNS_NEW:
Florian Westphal9061f242021-06-03 16:24:29 -0700199 return mptcp_setsockopt_sol_socket_tstamp(msk, optname, val);
Florian Westphal1b3e7ed2021-04-15 16:44:55 -0700200 }
201
202 return -ENOPROTOOPT;
203}
204
Yangbo Lu6c9a0a02021-06-30 16:11:58 +0800205static 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;
Yangbo Lud4631262021-06-30 16:11:59 +0800212 struct so_timestamping timestamping;
213 int ret;
Yangbo Lu6c9a0a02021-06-30 16:11:58 +0800214
Yangbo Lud4631262021-06-30 16:11:59 +0800215 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 }
Yangbo Lu6c9a0a02021-06-30 16:11:58 +0800227
228 ret = sock_setsockopt(sk->sk_socket, SOL_SOCKET, optname,
Yangbo Lud4631262021-06-30 16:11:59 +0800229 KERNEL_SOCKPTR(&timestamping),
230 sizeof(timestamping));
Yangbo Lu6c9a0a02021-06-30 16:11:58 +0800231 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
Yangbo Lud4631262021-06-30 16:11:59 +0800240 sock_set_timestamping(sk, optname, timestamping);
Yangbo Lu6c9a0a02021-06-30 16:11:58 +0800241 unlock_sock_fast(ssk, slow);
242 }
243
244 release_sock(sk);
245
246 return 0;
247}
248
Florian Westphal268b1232021-04-15 16:44:57 -0700249static 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
Paolo Abeni0abdde82021-04-15 16:44:51 -0700290static 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:
Florian Westphal5d0a6bc82d2021-04-15 16:44:56 -0700300 case SO_BINDTODEVICE:
301 case SO_BINDTOIFINDEX:
Paolo Abeni0abdde82021-04-15 16:44:51 -0700302 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;
Florian Westphal5d0a6bc82d2021-04-15 16:44:56 -0700315 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;
Paolo Abeni0abdde82021-04-15 16:44:51 -0700319 }
320 release_sock(sk);
321 return ret;
Florian Westphal1b3e7ed2021-04-15 16:44:55 -0700322 case SO_KEEPALIVE:
323 case SO_PRIORITY:
Florian Westphal5d0a6bc82d2021-04-15 16:44:56 -0700324 case SO_SNDBUF:
325 case SO_SNDBUFFORCE:
326 case SO_RCVBUF:
327 case SO_RCVBUFFORCE:
Florian Westphal36704412021-04-15 16:44:58 -0700328 case SO_MARK:
Florian Westphal6f0d7192021-04-15 16:44:59 -0700329 case SO_INCOMING_CPU:
Florian Westphala03c99b2021-04-15 16:45:00 -0700330 case SO_DEBUG:
Florian Westphal9061f242021-06-03 16:24:29 -0700331 case SO_TIMESTAMP_OLD:
332 case SO_TIMESTAMP_NEW:
333 case SO_TIMESTAMPNS_OLD:
334 case SO_TIMESTAMPNS_NEW:
Yangbo Lu6c9a0a02021-06-30 16:11:58 +0800335 return mptcp_setsockopt_sol_socket_int(msk, optname, optval,
336 optlen);
Florian Westphal9061f242021-06-03 16:24:29 -0700337 case SO_TIMESTAMPING_OLD:
338 case SO_TIMESTAMPING_NEW:
Yangbo Lu6c9a0a02021-06-30 16:11:58 +0800339 return mptcp_setsockopt_sol_socket_timestamping(msk, optname,
340 optval, optlen);
Florian Westphal268b1232021-04-15 16:44:57 -0700341 case SO_LINGER:
342 return mptcp_setsockopt_sol_socket_linger(msk, optval, optlen);
Florian Westphal7a009a72021-06-03 16:24:30 -0700343 case SO_RCVLOWAT:
344 case SO_RCVTIMEO_OLD:
345 case SO_RCVTIMEO_NEW:
346 case SO_BUSY_POLL:
347 case SO_PREFER_BUSY_POLL:
348 case SO_BUSY_POLL_BUDGET:
349 /* No need to copy: only relevant for msk */
350 return sock_setsockopt(sk->sk_socket, SOL_SOCKET, optname, optval, optlen);
Florian Westphala03c99b2021-04-15 16:45:00 -0700351 case SO_NO_CHECK:
352 case SO_DONTROUTE:
353 case SO_BROADCAST:
354 case SO_BSDCOMPAT:
355 case SO_PASSCRED:
356 case SO_PASSSEC:
357 case SO_RXQ_OVFL:
358 case SO_WIFI_STATUS:
359 case SO_NOFCS:
360 case SO_SELECT_ERR_QUEUE:
361 return 0;
Paolo Abeni0abdde82021-04-15 16:44:51 -0700362 }
363
Florian Westphal7a009a72021-06-03 16:24:30 -0700364 /* SO_OOBINLINE is not supported, let's avoid the related mess
365 * SO_ATTACH_FILTER, SO_ATTACH_BPF, SO_ATTACH_REUSEPORT_CBPF,
366 * SO_DETACH_REUSEPORT_BPF, SO_DETACH_FILTER, SO_LOCK_FILTER,
367 * we must be careful with subflows
368 *
369 * SO_ATTACH_REUSEPORT_EBPF is not supported, at it checks
370 * explicitly the sk_protocol field
371 *
372 * SO_PEEK_OFF is unsupported, as it is for plain TCP
373 * SO_MAX_PACING_RATE is unsupported, we must be careful with subflows
374 * SO_CNX_ADVICE is currently unsupported, could possibly be relevant,
375 * but likely needs careful design
376 *
377 * SO_ZEROCOPY is currently unsupported, TODO in sndmsg
378 * SO_TXTIME is currently unsupported
379 */
380
381 return -EOPNOTSUPP;
Paolo Abeni0abdde82021-04-15 16:44:51 -0700382}
383
384static int mptcp_setsockopt_v6(struct mptcp_sock *msk, int optname,
385 sockptr_t optval, unsigned int optlen)
386{
387 struct sock *sk = (struct sock *)msk;
388 int ret = -EOPNOTSUPP;
389 struct socket *ssock;
390
391 switch (optname) {
392 case IPV6_V6ONLY:
Florian Westphalc9406a22021-11-19 12:41:36 -0800393 case IPV6_TRANSPARENT:
394 case IPV6_FREEBIND:
Paolo Abeni0abdde82021-04-15 16:44:51 -0700395 lock_sock(sk);
396 ssock = __mptcp_nmpc_socket(msk);
397 if (!ssock) {
398 release_sock(sk);
399 return -EINVAL;
400 }
401
402 ret = tcp_setsockopt(ssock->sk, SOL_IPV6, optname, optval, optlen);
Florian Westphalc9406a22021-11-19 12:41:36 -0800403 if (ret != 0) {
404 release_sock(sk);
405 return ret;
406 }
407
408 sockopt_seq_inc(msk);
409
410 switch (optname) {
411 case IPV6_V6ONLY:
Paolo Abeni0abdde82021-04-15 16:44:51 -0700412 sk->sk_ipv6only = ssock->sk->sk_ipv6only;
Florian Westphalc9406a22021-11-19 12:41:36 -0800413 break;
414 case IPV6_TRANSPARENT:
415 inet_sk(sk)->transparent = inet_sk(ssock->sk)->transparent;
416 break;
417 case IPV6_FREEBIND:
418 inet_sk(sk)->freebind = inet_sk(ssock->sk)->freebind;
419 break;
420 }
Paolo Abeni0abdde82021-04-15 16:44:51 -0700421
422 release_sock(sk);
423 break;
424 }
425
426 return ret;
427}
428
Paolo Abenid9e4c122021-04-15 16:44:52 -0700429static bool mptcp_supported_sockopt(int level, int optname)
430{
Paolo Abenid9e4c122021-04-15 16:44:52 -0700431 if (level == SOL_IP) {
432 switch (optname) {
433 /* should work fine */
434 case IP_FREEBIND:
435 case IP_TRANSPARENT:
436
437 /* the following are control cmsg related */
438 case IP_PKTINFO:
439 case IP_RECVTTL:
440 case IP_RECVTOS:
441 case IP_RECVOPTS:
442 case IP_RETOPTS:
443 case IP_PASSSEC:
444 case IP_RECVORIGDSTADDR:
445 case IP_CHECKSUM:
446 case IP_RECVFRAGSIZE:
447
448 /* common stuff that need some love */
449 case IP_TOS:
450 case IP_TTL:
451 case IP_BIND_ADDRESS_NO_PORT:
452 case IP_MTU_DISCOVER:
453 case IP_RECVERR:
454
455 /* possibly less common may deserve some love */
456 case IP_MINTTL:
457
458 /* the following is apparently a no-op for plain TCP */
459 case IP_RECVERR_RFC4884:
460 return true;
461 }
462
463 /* IP_OPTIONS is not supported, needs subflow care */
464 /* IP_HDRINCL, IP_NODEFRAG are not supported, RAW specific */
465 /* IP_MULTICAST_TTL, IP_MULTICAST_LOOP, IP_UNICAST_IF,
466 * IP_ADD_MEMBERSHIP, IP_ADD_SOURCE_MEMBERSHIP, IP_DROP_MEMBERSHIP,
467 * IP_DROP_SOURCE_MEMBERSHIP, IP_BLOCK_SOURCE, IP_UNBLOCK_SOURCE,
468 * MCAST_JOIN_GROUP, MCAST_LEAVE_GROUP MCAST_JOIN_SOURCE_GROUP,
469 * MCAST_LEAVE_SOURCE_GROUP, MCAST_BLOCK_SOURCE, MCAST_UNBLOCK_SOURCE,
470 * MCAST_MSFILTER, IP_MULTICAST_ALL are not supported, better not deal
471 * with mcast stuff
472 */
473 /* IP_IPSEC_POLICY, IP_XFRM_POLICY are nut supported, unrelated here */
474 return false;
475 }
476 if (level == SOL_IPV6) {
477 switch (optname) {
478 case IPV6_V6ONLY:
479
480 /* the following are control cmsg related */
481 case IPV6_RECVPKTINFO:
482 case IPV6_2292PKTINFO:
483 case IPV6_RECVHOPLIMIT:
484 case IPV6_2292HOPLIMIT:
485 case IPV6_RECVRTHDR:
486 case IPV6_2292RTHDR:
487 case IPV6_RECVHOPOPTS:
488 case IPV6_2292HOPOPTS:
489 case IPV6_RECVDSTOPTS:
490 case IPV6_2292DSTOPTS:
491 case IPV6_RECVTCLASS:
492 case IPV6_FLOWINFO:
493 case IPV6_RECVPATHMTU:
494 case IPV6_RECVORIGDSTADDR:
495 case IPV6_RECVFRAGSIZE:
496
497 /* the following ones need some love but are quite common */
498 case IPV6_TCLASS:
499 case IPV6_TRANSPARENT:
500 case IPV6_FREEBIND:
501 case IPV6_PKTINFO:
502 case IPV6_2292PKTOPTIONS:
503 case IPV6_UNICAST_HOPS:
504 case IPV6_MTU_DISCOVER:
505 case IPV6_MTU:
506 case IPV6_RECVERR:
507 case IPV6_FLOWINFO_SEND:
508 case IPV6_FLOWLABEL_MGR:
509 case IPV6_MINHOPCOUNT:
510 case IPV6_DONTFRAG:
511 case IPV6_AUTOFLOWLABEL:
512
513 /* the following one is a no-op for plain TCP */
514 case IPV6_RECVERR_RFC4884:
515 return true;
516 }
517
518 /* IPV6_HOPOPTS, IPV6_RTHDRDSTOPTS, IPV6_RTHDR, IPV6_DSTOPTS are
519 * not supported
520 */
521 /* IPV6_MULTICAST_HOPS, IPV6_MULTICAST_LOOP, IPV6_UNICAST_IF,
522 * IPV6_MULTICAST_IF, IPV6_ADDRFORM,
523 * IPV6_ADD_MEMBERSHIP, IPV6_DROP_MEMBERSHIP, IPV6_JOIN_ANYCAST,
524 * IPV6_LEAVE_ANYCAST, IPV6_MULTICAST_ALL, MCAST_JOIN_GROUP, MCAST_LEAVE_GROUP,
525 * MCAST_JOIN_SOURCE_GROUP, MCAST_LEAVE_SOURCE_GROUP,
526 * MCAST_BLOCK_SOURCE, MCAST_UNBLOCK_SOURCE, MCAST_MSFILTER
527 * are not supported better not deal with mcast
528 */
529 /* IPV6_ROUTER_ALERT, IPV6_ROUTER_ALERT_ISOLATE are not supported, since are evil */
530
531 /* IPV6_IPSEC_POLICY, IPV6_XFRM_POLICY are not supported */
532 /* IPV6_ADDR_PREFERENCES is not supported, we must be careful with subflows */
533 return false;
534 }
535 if (level == SOL_TCP) {
536 switch (optname) {
537 /* the following are no-op or should work just fine */
538 case TCP_THIN_DUPACK:
539 case TCP_DEFER_ACCEPT:
540
541 /* the following need some love */
542 case TCP_MAXSEG:
543 case TCP_NODELAY:
544 case TCP_THIN_LINEAR_TIMEOUTS:
545 case TCP_CONGESTION:
Paolo Abenid9e4c122021-04-15 16:44:52 -0700546 case TCP_CORK:
547 case TCP_KEEPIDLE:
548 case TCP_KEEPINTVL:
549 case TCP_KEEPCNT:
550 case TCP_SYNCNT:
551 case TCP_SAVE_SYN:
552 case TCP_LINGER2:
553 case TCP_WINDOW_CLAMP:
554 case TCP_QUICKACK:
555 case TCP_USER_TIMEOUT:
556 case TCP_TIMESTAMP:
557 case TCP_NOTSENT_LOWAT:
558 case TCP_TX_DELAY:
Florian Westphal2c9e7762021-12-03 14:35:32 -0800559 case TCP_INQ:
Paolo Abenid9e4c122021-04-15 16:44:52 -0700560 return true;
561 }
562
563 /* TCP_MD5SIG, TCP_MD5SIG_EXT are not supported, MD5 is not compatible with MPTCP */
564
565 /* TCP_REPAIR, TCP_REPAIR_QUEUE, TCP_QUEUE_SEQ, TCP_REPAIR_OPTIONS,
566 * TCP_REPAIR_WINDOW are not supported, better avoid this mess
567 */
568 /* TCP_FASTOPEN_KEY, TCP_FASTOPEN TCP_FASTOPEN_CONNECT, TCP_FASTOPEN_NO_COOKIE,
569 * are not supported fastopen is currently unsupported
570 */
Paolo Abenid9e4c122021-04-15 16:44:52 -0700571 }
572 return false;
573}
574
Florian Westphalaa1fbd92021-04-15 16:45:01 -0700575static int mptcp_setsockopt_sol_tcp_congestion(struct mptcp_sock *msk, sockptr_t optval,
576 unsigned int optlen)
577{
578 struct mptcp_subflow_context *subflow;
579 struct sock *sk = (struct sock *)msk;
580 char name[TCP_CA_NAME_MAX];
581 bool cap_net_admin;
582 int ret;
583
584 if (optlen < 1)
585 return -EINVAL;
586
587 ret = strncpy_from_sockptr(name, optval,
588 min_t(long, TCP_CA_NAME_MAX - 1, optlen));
589 if (ret < 0)
590 return -EFAULT;
591
592 name[ret] = 0;
593
594 cap_net_admin = ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN);
595
596 ret = 0;
597 lock_sock(sk);
598 sockopt_seq_inc(msk);
599 mptcp_for_each_subflow(msk, subflow) {
600 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
601 int err;
602
603 lock_sock(ssk);
604 err = tcp_set_congestion_control(ssk, name, true, cap_net_admin);
605 if (err < 0 && ret == 0)
606 ret = err;
607 subflow->setsockopt_seq = msk->setsockopt_seq;
608 release_sock(ssk);
609 }
610
611 if (ret == 0)
Paolo Abeni20b57592021-05-25 14:23:10 -0700612 strcpy(msk->ca_name, name);
Florian Westphalaa1fbd92021-04-15 16:45:01 -0700613
614 release_sock(sk);
615 return ret;
616}
617
Maxim Galaganov4f6e14b2021-12-03 14:35:41 -0800618static int mptcp_setsockopt_sol_tcp_cork(struct mptcp_sock *msk, sockptr_t optval,
619 unsigned int optlen)
620{
621 struct mptcp_subflow_context *subflow;
622 struct sock *sk = (struct sock *)msk;
623 int val;
624
625 if (optlen < sizeof(int))
626 return -EINVAL;
627
628 if (copy_from_sockptr(&val, optval, sizeof(val)))
629 return -EFAULT;
630
631 lock_sock(sk);
632 sockopt_seq_inc(msk);
633 msk->cork = !!val;
634 mptcp_for_each_subflow(msk, subflow) {
635 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
636
637 lock_sock(ssk);
638 __tcp_sock_set_cork(ssk, !!val);
639 release_sock(ssk);
640 }
641 if (!val)
642 mptcp_check_and_set_pending(sk);
643 release_sock(sk);
644
645 return 0;
646}
647
648static int mptcp_setsockopt_sol_tcp_nodelay(struct mptcp_sock *msk, sockptr_t optval,
649 unsigned int optlen)
650{
651 struct mptcp_subflow_context *subflow;
652 struct sock *sk = (struct sock *)msk;
653 int val;
654
655 if (optlen < sizeof(int))
656 return -EINVAL;
657
658 if (copy_from_sockptr(&val, optval, sizeof(val)))
659 return -EFAULT;
660
661 lock_sock(sk);
662 sockopt_seq_inc(msk);
663 msk->nodelay = !!val;
664 mptcp_for_each_subflow(msk, subflow) {
665 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
666
667 lock_sock(ssk);
668 __tcp_sock_set_nodelay(ssk, !!val);
669 release_sock(ssk);
670 }
671 if (val)
672 mptcp_check_and_set_pending(sk);
673 release_sock(sk);
674
675 return 0;
676}
677
Florian Westphalc9406a22021-11-19 12:41:36 -0800678static int mptcp_setsockopt_sol_ip_set_transparent(struct mptcp_sock *msk, int optname,
679 sockptr_t optval, unsigned int optlen)
680{
681 struct sock *sk = (struct sock *)msk;
682 struct inet_sock *issk;
683 struct socket *ssock;
684 int err;
685
686 err = ip_setsockopt(sk, SOL_IP, optname, optval, optlen);
687 if (err != 0)
688 return err;
689
690 lock_sock(sk);
691
692 ssock = __mptcp_nmpc_socket(msk);
693 if (!ssock) {
694 release_sock(sk);
695 return -EINVAL;
696 }
697
698 issk = inet_sk(ssock->sk);
699
700 switch (optname) {
701 case IP_FREEBIND:
702 issk->freebind = inet_sk(sk)->freebind;
703 break;
704 case IP_TRANSPARENT:
705 issk->transparent = inet_sk(sk)->transparent;
706 break;
707 default:
708 release_sock(sk);
709 WARN_ON_ONCE(1);
710 return -EOPNOTSUPP;
711 }
712
713 sockopt_seq_inc(msk);
714 release_sock(sk);
715 return 0;
716}
717
Poorva Sonparoteffcacff2021-11-19 12:41:35 -0800718static int mptcp_setsockopt_v4_set_tos(struct mptcp_sock *msk, int optname,
719 sockptr_t optval, unsigned int optlen)
720{
721 struct mptcp_subflow_context *subflow;
722 struct sock *sk = (struct sock *)msk;
723 int err, val;
724
725 err = ip_setsockopt(sk, SOL_IP, optname, optval, optlen);
726
727 if (err != 0)
728 return err;
729
730 lock_sock(sk);
731 sockopt_seq_inc(msk);
732 val = inet_sk(sk)->tos;
733 mptcp_for_each_subflow(msk, subflow) {
734 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
735
736 __ip_sock_set_tos(ssk, val);
737 }
738 release_sock(sk);
739
740 return err;
741}
742
743static int mptcp_setsockopt_v4(struct mptcp_sock *msk, int optname,
744 sockptr_t optval, unsigned int optlen)
745{
746 switch (optname) {
Florian Westphalc9406a22021-11-19 12:41:36 -0800747 case IP_FREEBIND:
748 case IP_TRANSPARENT:
749 return mptcp_setsockopt_sol_ip_set_transparent(msk, optname, optval, optlen);
Poorva Sonparoteffcacff2021-11-19 12:41:35 -0800750 case IP_TOS:
751 return mptcp_setsockopt_v4_set_tos(msk, optname, optval, optlen);
752 }
753
754 return -EOPNOTSUPP;
755}
756
Florian Westphalaa1fbd92021-04-15 16:45:01 -0700757static int mptcp_setsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
758 sockptr_t optval, unsigned int optlen)
759{
Florian Westphal2c9e7762021-12-03 14:35:32 -0800760 struct sock *sk = (void *)msk;
761 int ret, val;
762
Florian Westphalaa1fbd92021-04-15 16:45:01 -0700763 switch (optname) {
Florian Westphal2c9e7762021-12-03 14:35:32 -0800764 case TCP_INQ:
765 ret = mptcp_get_int_option(msk, optval, optlen, &val);
766 if (ret)
767 return ret;
768 if (val < 0 || val > 1)
769 return -EINVAL;
770
771 lock_sock(sk);
772 msk->recvmsg_inq = !!val;
773 release_sock(sk);
774 return 0;
Florian Westphalaa1fbd92021-04-15 16:45:01 -0700775 case TCP_ULP:
776 return -EOPNOTSUPP;
777 case TCP_CONGESTION:
778 return mptcp_setsockopt_sol_tcp_congestion(msk, optval, optlen);
Maxim Galaganov4f6e14b2021-12-03 14:35:41 -0800779 case TCP_CORK:
780 return mptcp_setsockopt_sol_tcp_cork(msk, optval, optlen);
781 case TCP_NODELAY:
782 return mptcp_setsockopt_sol_tcp_nodelay(msk, optval, optlen);
Florian Westphalaa1fbd92021-04-15 16:45:01 -0700783 }
784
785 return -EOPNOTSUPP;
786}
787
Paolo Abeni0abdde82021-04-15 16:44:51 -0700788int mptcp_setsockopt(struct sock *sk, int level, int optname,
789 sockptr_t optval, unsigned int optlen)
790{
791 struct mptcp_sock *msk = mptcp_sk(sk);
792 struct sock *ssk;
793
794 pr_debug("msk=%p", msk);
795
Paolo Abeni0abdde82021-04-15 16:44:51 -0700796 if (level == SOL_SOCKET)
797 return mptcp_setsockopt_sol_socket(msk, optname, optval, optlen);
798
Florian Westphal7a009a72021-06-03 16:24:30 -0700799 if (!mptcp_supported_sockopt(level, optname))
800 return -ENOPROTOOPT;
801
Paolo Abeni0abdde82021-04-15 16:44:51 -0700802 /* @@ the meaning of setsockopt() when the socket is connected and
803 * there are multiple subflows is not yet defined. It is up to the
804 * MPTCP-level socket to configure the subflows until the subflow
805 * is in TCP fallback, when TCP socket options are passed through
806 * to the one remaining subflow.
807 */
808 lock_sock(sk);
809 ssk = __mptcp_tcp_fallback(msk);
810 release_sock(sk);
811 if (ssk)
812 return tcp_setsockopt(ssk, level, optname, optval, optlen);
813
Poorva Sonparoteffcacff2021-11-19 12:41:35 -0800814 if (level == SOL_IP)
815 return mptcp_setsockopt_v4(msk, optname, optval, optlen);
816
Paolo Abeni0abdde82021-04-15 16:44:51 -0700817 if (level == SOL_IPV6)
818 return mptcp_setsockopt_v6(msk, optname, optval, optlen);
819
Florian Westphalaa1fbd92021-04-15 16:45:01 -0700820 if (level == SOL_TCP)
821 return mptcp_setsockopt_sol_tcp(msk, optname, optval, optlen);
822
823 return -EOPNOTSUPP;
824}
825
826static int mptcp_getsockopt_first_sf_only(struct mptcp_sock *msk, int level, int optname,
827 char __user *optval, int __user *optlen)
828{
829 struct sock *sk = (struct sock *)msk;
830 struct socket *ssock;
831 int ret = -EINVAL;
832 struct sock *ssk;
833
834 lock_sock(sk);
835 ssk = msk->first;
836 if (ssk) {
837 ret = tcp_getsockopt(ssk, level, optname, optval, optlen);
838 goto out;
839 }
840
841 ssock = __mptcp_nmpc_socket(msk);
842 if (!ssock)
843 goto out;
844
845 ret = tcp_getsockopt(ssock->sk, level, optname, optval, optlen);
846
847out:
848 release_sock(sk);
849 return ret;
850}
851
Florian Westphal61bc6e82021-09-17 16:33:18 -0700852void mptcp_diag_fill_info(struct mptcp_sock *msk, struct mptcp_info *info)
853{
854 struct sock *sk = &msk->sk.icsk_inet.sk;
Florian Westphal61bc6e82021-09-17 16:33:18 -0700855 u32 flags = 0;
Florian Westphal55c42fa2021-09-17 16:33:19 -0700856 bool slow;
Florian Westphal61bc6e82021-09-17 16:33:18 -0700857 u8 val;
858
Florian Westphal55c42fa2021-09-17 16:33:19 -0700859 memset(info, 0, sizeof(*info));
860
861 slow = lock_sock_fast(sk);
862
Florian Westphal61bc6e82021-09-17 16:33:18 -0700863 info->mptcpi_subflows = READ_ONCE(msk->pm.subflows);
864 info->mptcpi_add_addr_signal = READ_ONCE(msk->pm.add_addr_signaled);
865 info->mptcpi_add_addr_accepted = READ_ONCE(msk->pm.add_addr_accepted);
866 info->mptcpi_local_addr_used = READ_ONCE(msk->pm.local_addr_used);
867 info->mptcpi_subflows_max = mptcp_pm_get_subflows_max(msk);
868 val = mptcp_pm_get_add_addr_signal_max(msk);
869 info->mptcpi_add_addr_signal_max = val;
870 val = mptcp_pm_get_add_addr_accept_max(msk);
871 info->mptcpi_add_addr_accepted_max = val;
872 info->mptcpi_local_addr_max = mptcp_pm_get_local_addr_max(msk);
873 if (test_bit(MPTCP_FALLBACK_DONE, &msk->flags))
874 flags |= MPTCP_INFO_FLAG_FALLBACK;
875 if (READ_ONCE(msk->can_ack))
876 flags |= MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED;
877 info->mptcpi_flags = flags;
878 info->mptcpi_token = READ_ONCE(msk->token);
879 info->mptcpi_write_seq = READ_ONCE(msk->write_seq);
880 info->mptcpi_snd_una = READ_ONCE(msk->snd_una);
881 info->mptcpi_rcv_nxt = READ_ONCE(msk->ack_seq);
882 info->mptcpi_csum_enabled = READ_ONCE(msk->csum_enabled);
883
884 unlock_sock_fast(sk, slow);
885}
886EXPORT_SYMBOL_GPL(mptcp_diag_fill_info);
887
Florian Westphal55c42fa2021-09-17 16:33:19 -0700888static int mptcp_getsockopt_info(struct mptcp_sock *msk, char __user *optval, int __user *optlen)
889{
890 struct mptcp_info m_info;
891 int len;
892
893 if (get_user(len, optlen))
894 return -EFAULT;
895
896 len = min_t(unsigned int, len, sizeof(struct mptcp_info));
897
898 mptcp_diag_fill_info(msk, &m_info);
899
900 if (put_user(len, optlen))
901 return -EFAULT;
902
903 if (copy_to_user(optval, &m_info, len))
904 return -EFAULT;
905
906 return 0;
907}
908
Florian Westphal06f15ce2021-09-17 16:33:20 -0700909static int mptcp_put_subflow_data(struct mptcp_subflow_data *sfd,
910 char __user *optval,
911 u32 copied,
912 int __user *optlen)
913{
914 u32 copylen = min_t(u32, sfd->size_subflow_data, sizeof(*sfd));
915
916 if (copied)
917 copied += sfd->size_subflow_data;
918 else
919 copied = copylen;
920
921 if (put_user(copied, optlen))
922 return -EFAULT;
923
924 if (copy_to_user(optval, sfd, copylen))
925 return -EFAULT;
926
927 return 0;
928}
929
930static int mptcp_get_subflow_data(struct mptcp_subflow_data *sfd,
931 char __user *optval, int __user *optlen)
932{
933 int len, copylen;
934
935 if (get_user(len, optlen))
936 return -EFAULT;
937
938 /* if mptcp_subflow_data size is changed, need to adjust
939 * this function to deal with programs using old version.
940 */
941 BUILD_BUG_ON(sizeof(*sfd) != MIN_INFO_OPTLEN_SIZE);
942
943 if (len < MIN_INFO_OPTLEN_SIZE)
944 return -EINVAL;
945
946 memset(sfd, 0, sizeof(*sfd));
947
948 copylen = min_t(unsigned int, len, sizeof(*sfd));
949 if (copy_from_user(sfd, optval, copylen))
950 return -EFAULT;
951
952 /* size_subflow_data is u32, but len is signed */
953 if (sfd->size_subflow_data > INT_MAX ||
954 sfd->size_user > INT_MAX)
955 return -EINVAL;
956
957 if (sfd->size_subflow_data < MIN_INFO_OPTLEN_SIZE ||
958 sfd->size_subflow_data > len)
959 return -EINVAL;
960
961 if (sfd->num_subflows || sfd->size_kernel)
962 return -EINVAL;
963
964 return len - sfd->size_subflow_data;
965}
966
967static int mptcp_getsockopt_tcpinfo(struct mptcp_sock *msk, char __user *optval,
968 int __user *optlen)
969{
970 struct mptcp_subflow_context *subflow;
971 struct sock *sk = &msk->sk.icsk_inet.sk;
972 unsigned int sfcount = 0, copied = 0;
973 struct mptcp_subflow_data sfd;
974 char __user *infoptr;
975 int len;
976
977 len = mptcp_get_subflow_data(&sfd, optval, optlen);
978 if (len < 0)
979 return len;
980
981 sfd.size_kernel = sizeof(struct tcp_info);
982 sfd.size_user = min_t(unsigned int, sfd.size_user,
983 sizeof(struct tcp_info));
984
985 infoptr = optval + sfd.size_subflow_data;
986
987 lock_sock(sk);
988
989 mptcp_for_each_subflow(msk, subflow) {
990 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
991
992 ++sfcount;
993
994 if (len && len >= sfd.size_user) {
995 struct tcp_info info;
996
997 tcp_get_info(ssk, &info);
998
999 if (copy_to_user(infoptr, &info, sfd.size_user)) {
1000 release_sock(sk);
1001 return -EFAULT;
1002 }
1003
1004 infoptr += sfd.size_user;
1005 copied += sfd.size_user;
1006 len -= sfd.size_user;
1007 }
1008 }
1009
1010 release_sock(sk);
1011
1012 sfd.num_subflows = sfcount;
1013
1014 if (mptcp_put_subflow_data(&sfd, optval, copied, optlen))
1015 return -EFAULT;
1016
1017 return 0;
1018}
1019
Florian Westphalc11c5902021-09-17 16:33:21 -07001020static void mptcp_get_sub_addrs(const struct sock *sk, struct mptcp_subflow_addrs *a)
1021{
1022 struct inet_sock *inet = inet_sk(sk);
1023
1024 memset(a, 0, sizeof(*a));
1025
1026 if (sk->sk_family == AF_INET) {
1027 a->sin_local.sin_family = AF_INET;
1028 a->sin_local.sin_port = inet->inet_sport;
1029 a->sin_local.sin_addr.s_addr = inet->inet_rcv_saddr;
1030
1031 if (!a->sin_local.sin_addr.s_addr)
1032 a->sin_local.sin_addr.s_addr = inet->inet_saddr;
1033
1034 a->sin_remote.sin_family = AF_INET;
1035 a->sin_remote.sin_port = inet->inet_dport;
1036 a->sin_remote.sin_addr.s_addr = inet->inet_daddr;
1037#if IS_ENABLED(CONFIG_IPV6)
1038 } else if (sk->sk_family == AF_INET6) {
1039 const struct ipv6_pinfo *np = inet6_sk(sk);
1040
Tim Gardner29211e72021-10-15 16:05:50 -07001041 if (WARN_ON_ONCE(!np))
1042 return;
1043
Florian Westphalc11c5902021-09-17 16:33:21 -07001044 a->sin6_local.sin6_family = AF_INET6;
1045 a->sin6_local.sin6_port = inet->inet_sport;
1046
1047 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
1048 a->sin6_local.sin6_addr = np->saddr;
1049 else
1050 a->sin6_local.sin6_addr = sk->sk_v6_rcv_saddr;
1051
1052 a->sin6_remote.sin6_family = AF_INET6;
1053 a->sin6_remote.sin6_port = inet->inet_dport;
1054 a->sin6_remote.sin6_addr = sk->sk_v6_daddr;
1055#endif
1056 }
1057}
1058
1059static int mptcp_getsockopt_subflow_addrs(struct mptcp_sock *msk, char __user *optval,
1060 int __user *optlen)
1061{
1062 struct sock *sk = &msk->sk.icsk_inet.sk;
1063 struct mptcp_subflow_context *subflow;
1064 unsigned int sfcount = 0, copied = 0;
1065 struct mptcp_subflow_data sfd;
1066 char __user *addrptr;
1067 int len;
1068
1069 len = mptcp_get_subflow_data(&sfd, optval, optlen);
1070 if (len < 0)
1071 return len;
1072
1073 sfd.size_kernel = sizeof(struct mptcp_subflow_addrs);
1074 sfd.size_user = min_t(unsigned int, sfd.size_user,
1075 sizeof(struct mptcp_subflow_addrs));
1076
1077 addrptr = optval + sfd.size_subflow_data;
1078
1079 lock_sock(sk);
1080
1081 mptcp_for_each_subflow(msk, subflow) {
1082 struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
1083
1084 ++sfcount;
1085
1086 if (len && len >= sfd.size_user) {
1087 struct mptcp_subflow_addrs a;
1088
1089 mptcp_get_sub_addrs(ssk, &a);
1090
1091 if (copy_to_user(addrptr, &a, sfd.size_user)) {
1092 release_sock(sk);
1093 return -EFAULT;
1094 }
1095
1096 addrptr += sfd.size_user;
1097 copied += sfd.size_user;
1098 len -= sfd.size_user;
1099 }
1100 }
1101
1102 release_sock(sk);
1103
1104 sfd.num_subflows = sfcount;
1105
1106 if (mptcp_put_subflow_data(&sfd, optval, copied, optlen))
1107 return -EFAULT;
1108
1109 return 0;
1110}
1111
Florian Westphal2c9e7762021-12-03 14:35:32 -08001112static int mptcp_put_int_option(struct mptcp_sock *msk, char __user *optval,
1113 int __user *optlen, int val)
1114{
1115 int len;
1116
1117 if (get_user(len, optlen))
1118 return -EFAULT;
Florian Westphal2c9e7762021-12-03 14:35:32 -08001119 if (len < 0)
1120 return -EINVAL;
1121
Florian Westphal3b1e21e2021-12-03 14:35:37 -08001122 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
1123 unsigned char ucval = (unsigned char)val;
1124
1125 len = 1;
1126 if (put_user(len, optlen))
1127 return -EFAULT;
1128 if (copy_to_user(optval, &ucval, 1))
1129 return -EFAULT;
1130 } else {
1131 len = min_t(unsigned int, len, sizeof(int));
1132 if (put_user(len, optlen))
1133 return -EFAULT;
1134 if (copy_to_user(optval, &val, len))
1135 return -EFAULT;
1136 }
Florian Westphal2c9e7762021-12-03 14:35:32 -08001137
1138 return 0;
1139}
1140
Florian Westphalaa1fbd92021-04-15 16:45:01 -07001141static int mptcp_getsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
1142 char __user *optval, int __user *optlen)
1143{
1144 switch (optname) {
1145 case TCP_ULP:
1146 case TCP_CONGESTION:
1147 case TCP_INFO:
1148 case TCP_CC_INFO:
1149 return mptcp_getsockopt_first_sf_only(msk, SOL_TCP, optname,
1150 optval, optlen);
Florian Westphal2c9e7762021-12-03 14:35:32 -08001151 case TCP_INQ:
1152 return mptcp_put_int_option(msk, optval, optlen, msk->recvmsg_inq);
Maxim Galaganov4f6e14b2021-12-03 14:35:41 -08001153 case TCP_CORK:
1154 return mptcp_put_int_option(msk, optval, optlen, msk->cork);
1155 case TCP_NODELAY:
1156 return mptcp_put_int_option(msk, optval, optlen, msk->nodelay);
Florian Westphalaa1fbd92021-04-15 16:45:01 -07001157 }
Paolo Abeni0abdde82021-04-15 16:44:51 -07001158 return -EOPNOTSUPP;
1159}
1160
Florian Westphal3b1e21e2021-12-03 14:35:37 -08001161static int mptcp_getsockopt_v4(struct mptcp_sock *msk, int optname,
1162 char __user *optval, int __user *optlen)
1163{
1164 struct sock *sk = (void *)msk;
1165
1166 switch (optname) {
1167 case IP_TOS:
1168 return mptcp_put_int_option(msk, optval, optlen, inet_sk(sk)->tos);
1169 }
1170
1171 return -EOPNOTSUPP;
1172}
1173
Florian Westphal55c42fa2021-09-17 16:33:19 -07001174static int mptcp_getsockopt_sol_mptcp(struct mptcp_sock *msk, int optname,
1175 char __user *optval, int __user *optlen)
1176{
1177 switch (optname) {
1178 case MPTCP_INFO:
1179 return mptcp_getsockopt_info(msk, optval, optlen);
Florian Westphal06f15ce2021-09-17 16:33:20 -07001180 case MPTCP_TCPINFO:
1181 return mptcp_getsockopt_tcpinfo(msk, optval, optlen);
Florian Westphalc11c5902021-09-17 16:33:21 -07001182 case MPTCP_SUBFLOW_ADDRS:
1183 return mptcp_getsockopt_subflow_addrs(msk, optval, optlen);
Florian Westphal55c42fa2021-09-17 16:33:19 -07001184 }
1185
1186 return -EOPNOTSUPP;
1187}
1188
Paolo Abeni0abdde82021-04-15 16:44:51 -07001189int mptcp_getsockopt(struct sock *sk, int level, int optname,
1190 char __user *optval, int __user *option)
1191{
1192 struct mptcp_sock *msk = mptcp_sk(sk);
1193 struct sock *ssk;
1194
1195 pr_debug("msk=%p", msk);
1196
1197 /* @@ the meaning of setsockopt() when the socket is connected and
1198 * there are multiple subflows is not yet defined. It is up to the
1199 * MPTCP-level socket to configure the subflows until the subflow
1200 * is in TCP fallback, when socket options are passed through
1201 * to the one remaining subflow.
1202 */
1203 lock_sock(sk);
1204 ssk = __mptcp_tcp_fallback(msk);
1205 release_sock(sk);
1206 if (ssk)
1207 return tcp_getsockopt(ssk, level, optname, optval, option);
1208
Florian Westphal3b1e21e2021-12-03 14:35:37 -08001209 if (level == SOL_IP)
1210 return mptcp_getsockopt_v4(msk, optname, optval, option);
Florian Westphalaa1fbd92021-04-15 16:45:01 -07001211 if (level == SOL_TCP)
1212 return mptcp_getsockopt_sol_tcp(msk, optname, optval, option);
Florian Westphal55c42fa2021-09-17 16:33:19 -07001213 if (level == SOL_MPTCP)
1214 return mptcp_getsockopt_sol_mptcp(msk, optname, optval, option);
Paolo Abeni0abdde82021-04-15 16:44:51 -07001215 return -EOPNOTSUPP;
1216}
1217
Florian Westphal1b3e7ed2021-04-15 16:44:55 -07001218static void sync_socket_options(struct mptcp_sock *msk, struct sock *ssk)
1219{
Florian Westphal5d0a6bc82d2021-04-15 16:44:56 -07001220 static const unsigned int tx_rx_locks = SOCK_RCVBUF_LOCK | SOCK_SNDBUF_LOCK;
Florian Westphal1b3e7ed2021-04-15 16:44:55 -07001221 struct sock *sk = (struct sock *)msk;
1222
1223 if (ssk->sk_prot->keepalive) {
1224 if (sock_flag(sk, SOCK_KEEPOPEN))
1225 ssk->sk_prot->keepalive(ssk, 1);
1226 else
1227 ssk->sk_prot->keepalive(ssk, 0);
1228 }
1229
1230 ssk->sk_priority = sk->sk_priority;
Florian Westphal5d0a6bc82d2021-04-15 16:44:56 -07001231 ssk->sk_bound_dev_if = sk->sk_bound_dev_if;
1232 ssk->sk_incoming_cpu = sk->sk_incoming_cpu;
Poorva Sonparoteffcacff2021-11-19 12:41:35 -08001233 __ip_sock_set_tos(ssk, inet_sk(sk)->tos);
Florian Westphal5d0a6bc82d2021-04-15 16:44:56 -07001234
1235 if (sk->sk_userlocks & tx_rx_locks) {
1236 ssk->sk_userlocks |= sk->sk_userlocks & tx_rx_locks;
1237 if (sk->sk_userlocks & SOCK_SNDBUF_LOCK)
1238 WRITE_ONCE(ssk->sk_sndbuf, sk->sk_sndbuf);
1239 if (sk->sk_userlocks & SOCK_RCVBUF_LOCK)
1240 WRITE_ONCE(ssk->sk_rcvbuf, sk->sk_rcvbuf);
1241 }
1242
1243 if (sock_flag(sk, SOCK_LINGER)) {
1244 ssk->sk_lingertime = sk->sk_lingertime;
1245 sock_set_flag(ssk, SOCK_LINGER);
1246 } else {
1247 sock_reset_flag(ssk, SOCK_LINGER);
1248 }
1249
1250 if (sk->sk_mark != ssk->sk_mark) {
1251 ssk->sk_mark = sk->sk_mark;
1252 sk_dst_reset(ssk);
1253 }
1254
1255 sock_valbool_flag(ssk, SOCK_DBG, sock_flag(sk, SOCK_DBG));
1256
1257 if (inet_csk(sk)->icsk_ca_ops != inet_csk(ssk)->icsk_ca_ops)
Paolo Abeni20b57592021-05-25 14:23:10 -07001258 tcp_set_congestion_control(ssk, msk->ca_name, false, true);
Maxim Galaganov4f6e14b2021-12-03 14:35:41 -08001259 __tcp_sock_set_cork(ssk, !!msk->cork);
1260 __tcp_sock_set_nodelay(ssk, !!msk->nodelay);
Florian Westphalc9406a22021-11-19 12:41:36 -08001261
1262 inet_sk(ssk)->transparent = inet_sk(sk)->transparent;
1263 inet_sk(ssk)->freebind = inet_sk(sk)->freebind;
Florian Westphal1b3e7ed2021-04-15 16:44:55 -07001264}
1265
Florian Westphaldf00b082021-04-15 16:44:54 -07001266static void __mptcp_sockopt_sync(struct mptcp_sock *msk, struct sock *ssk)
1267{
Florian Westphal1b3e7ed2021-04-15 16:44:55 -07001268 bool slow = lock_sock_fast(ssk);
1269
1270 sync_socket_options(msk, ssk);
1271
1272 unlock_sock_fast(ssk, slow);
Florian Westphaldf00b082021-04-15 16:44:54 -07001273}
1274
Florian Westphal78962482021-04-15 16:44:53 -07001275void mptcp_sockopt_sync(struct mptcp_sock *msk, struct sock *ssk)
1276{
Florian Westphaldf00b082021-04-15 16:44:54 -07001277 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
1278
Florian Westphal78962482021-04-15 16:44:53 -07001279 msk_owned_by_me(msk);
Florian Westphaldf00b082021-04-15 16:44:54 -07001280
1281 if (READ_ONCE(subflow->setsockopt_seq) != msk->setsockopt_seq) {
1282 __mptcp_sockopt_sync(msk, ssk);
1283
1284 subflow->setsockopt_seq = msk->setsockopt_seq;
1285 }
Florian Westphal78962482021-04-15 16:44:53 -07001286}
1287
Paolo Abeni3e501492022-01-06 16:20:25 -08001288void mptcp_sockopt_sync_locked(struct mptcp_sock *msk, struct sock *ssk)
Florian Westphal78962482021-04-15 16:44:53 -07001289{
Paolo Abeni3e501492022-01-06 16:20:25 -08001290 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
Florian Westphal78962482021-04-15 16:44:53 -07001291
Paolo Abeni3e501492022-01-06 16:20:25 -08001292 msk_owned_by_me(msk);
Florian Westphal78962482021-04-15 16:44:53 -07001293
Paolo Abeni3e501492022-01-06 16:20:25 -08001294 if (READ_ONCE(subflow->setsockopt_seq) != msk->setsockopt_seq) {
1295 sync_socket_options(msk, ssk);
Florian Westphal78962482021-04-15 16:44:53 -07001296
Paolo Abeni3e501492022-01-06 16:20:25 -08001297 subflow->setsockopt_seq = msk->setsockopt_seq;
Florian Westphal78962482021-04-15 16:44:53 -07001298 }
1299}