blob: 2c843ff5e3a92d8489a2e920129eed4984aba11b [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * IPv6 BSD socket options interface
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09007 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Based on linux/net/ipv4/ip_sockglue.c
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * FIXME: Make the setsockopt code POSIX compliant: That is
12 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * o Truncate getsockopt returns
14 * o Return an optlen of the truncated length if need be
15 *
16 * Changes:
17 * David L Stevens <dlstevens@us.ibm.com>:
18 * - added multicast source filtering API for MLDv2
19 */
20
21#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080022#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/errno.h>
24#include <linux/types.h>
25#include <linux/socket.h>
26#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/net.h>
28#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090029#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/netdevice.h>
31#include <linux/if_arp.h>
32#include <linux/init.h>
33#include <linux/sysctl.h>
34#include <linux/netfilter.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <net/sock.h>
38#include <net/snmp.h>
39#include <net/ipv6.h>
40#include <net/ndisc.h>
41#include <net/protocol.h>
42#include <net/transp_v6.h>
43#include <net/ip6_route.h>
44#include <net/addrconf.h>
45#include <net/inet_common.h>
46#include <net/tcp.h>
47#include <net/udp.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080048#include <net/udplite.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <net/xfrm.h>
David L Stevensdae50292008-04-27 01:06:07 -070050#include <net/compat.h>
David Lebruna149e7c2016-11-08 14:59:21 +010051#include <net/seg6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080053#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055struct ip6_ra_chain *ip6_ra_chain;
56DEFINE_RWLOCK(ip6_ra_lock);
57
Denis V. Lunev725a8ff2008-07-19 00:28:58 -070058int ip6_ra_control(struct sock *sk, int sel)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 struct ip6_ra_chain *ra, *new_ra, **rap;
61
62 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
Eric Dumazetc720c7e82009-10-15 06:30:45 +000063 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num != IPPROTO_RAW)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +090064 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Ian Morris67ba4152014-08-24 21:53:10 +010066 new_ra = (sel >= 0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
Gen Zhang95baa602019-05-24 11:19:46 +080067 if (sel >= 0 && !new_ra)
68 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 write_lock_bh(&ip6_ra_lock);
Ian Morris67ba4152014-08-24 21:53:10 +010071 for (rap = &ip6_ra_chain; (ra = *rap) != NULL; rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (ra->sk == sk) {
Ian Morris67ba4152014-08-24 21:53:10 +010073 if (sel >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 write_unlock_bh(&ip6_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -080075 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return -EADDRINUSE;
77 }
78
79 *rap = ra->next;
80 write_unlock_bh(&ip6_ra_lock);
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 sock_put(sk);
83 kfree(ra);
84 return 0;
85 }
86 }
Ian Morris63159f22015-03-29 14:00:04 +010087 if (!new_ra) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 write_unlock_bh(&ip6_ra_lock);
89 return -ENOBUFS;
90 }
91 new_ra->sk = sk;
92 new_ra->sel = sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 new_ra->next = ra;
94 *rap = new_ra;
95 sock_hold(sk);
96 write_unlock_bh(&ip6_ra_lock);
97 return 0;
98}
99
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700100struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
101 struct ipv6_txoptions *opt)
102{
103 if (inet_sk(sk)->is_icsk) {
104 if (opt &&
105 !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000106 inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) {
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700107 struct inet_connection_sock *icsk = inet_csk(sk);
108 icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
109 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
110 }
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700111 }
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800112 opt = xchg((__force struct ipv6_txoptions **)&inet6_sk(sk)->opt,
113 opt);
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700114 sk_dst_reset(sk);
115
116 return opt;
117}
118
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300119static bool setsockopt_needs_rtnl(int optname)
120{
121 switch (optname) {
WANG Cong8651be82016-10-19 23:35:12 -0700122 case IPV6_ADDRFORM:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300123 case IPV6_ADD_MEMBERSHIP:
124 case IPV6_DROP_MEMBERSHIP:
Marcelo Ricardo Leitnerc4a68532015-03-20 11:37:17 -0300125 case IPV6_JOIN_ANYCAST:
126 case IPV6_LEAVE_ANYCAST:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300127 case MCAST_JOIN_GROUP:
128 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300129 case MCAST_JOIN_SOURCE_GROUP:
130 case MCAST_LEAVE_SOURCE_GROUP:
131 case MCAST_BLOCK_SOURCE:
132 case MCAST_UNBLOCK_SOURCE:
133 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300134 return true;
135 }
136 return false;
137}
138
Al Virofcfa0b02020-04-27 11:00:01 -0400139static int do_ipv6_mcast_group_source(struct sock *sk, int optname,
140 struct group_source_req *greqs)
141{
142 int omode, add;
143
144 if (greqs->gsr_group.ss_family != AF_INET6 ||
145 greqs->gsr_source.ss_family != AF_INET6)
146 return -EADDRNOTAVAIL;
147
148 if (optname == MCAST_BLOCK_SOURCE) {
149 omode = MCAST_EXCLUDE;
150 add = 1;
151 } else if (optname == MCAST_UNBLOCK_SOURCE) {
152 omode = MCAST_EXCLUDE;
153 add = 0;
154 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
155 struct sockaddr_in6 *psin6;
156 int retv;
157
158 psin6 = (struct sockaddr_in6 *)&greqs->gsr_group;
159 retv = ipv6_sock_mc_join_ssm(sk, greqs->gsr_interface,
160 &psin6->sin6_addr,
161 MCAST_INCLUDE);
162 /* prior join w/ different source is ok */
163 if (retv && retv != -EADDRINUSE)
164 return retv;
165 omode = MCAST_INCLUDE;
166 add = 1;
167 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
168 omode = MCAST_INCLUDE;
169 add = 0;
170 }
171 return ip6_mc_source(add, omode, sk, greqs);
172}
173
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800174static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700175 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900178 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 int val, valbool;
180 int retv = -ENOPROTOOPT;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300181 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Ian Morris63159f22015-03-29 14:00:04 +0100183 if (!optval)
Ian Morris67ba4152014-08-24 21:53:10 +0100184 val = 0;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900185 else {
186 if (optlen >= sizeof(int)) {
187 if (get_user(val, (int __user *) optval))
188 return -EFAULT;
189 } else
190 val = 0;
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Ian Morris67ba4152014-08-24 21:53:10 +0100193 valbool = (val != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900195 if (ip6_mroute_opt(optname))
196 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
197
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300198 if (needs_rtnl)
199 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 lock_sock(sk);
201
202 switch (optname) {
203
204 case IPV6_ADDRFORM:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900205 if (optlen < sizeof(int))
206 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (val == PF_INET) {
208 struct ipv6_txoptions *opt;
209 struct sk_buff *pktopt;
210
Denis V. Lunev49d074f2008-06-04 15:49:06 +0400211 if (sk->sk_type == SOCK_RAW)
212 break;
213
Denis V. Lunev9596cc822008-06-04 15:49:08 +0400214 if (sk->sk_protocol == IPPROTO_UDP ||
215 sk->sk_protocol == IPPROTO_UDPLITE) {
216 struct udp_sock *up = udp_sk(sk);
217 if (up->pending == AF_INET6) {
218 retv = -EBUSY;
219 break;
220 }
Hangbin Liu79a1f0c2020-06-01 11:55:03 +0800221 } else if (sk->sk_protocol == IPPROTO_TCP) {
222 if (sk->sk_prot != &tcpv6_prot) {
223 retv = -EBUSY;
224 break;
225 }
226 } else {
Eric Dumazetb6f61182020-02-25 11:52:29 -0800227 break;
228 }
Hangbin Liu79a1f0c2020-06-01 11:55:03 +0800229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (sk->sk_state != TCP_ESTABLISHED) {
231 retv = -ENOTCONN;
232 break;
233 }
234
235 if (ipv6_only_sock(sk) ||
Eric Dumazetefe42082013-10-03 15:42:29 -0700236 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 retv = -EADDRNOTAVAIL;
238 break;
239 }
240
241 fl6_free_socklist(sk);
WANG Cong8651be82016-10-19 23:35:12 -0700242 __ipv6_sock_mc_close(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700244 /*
245 * Sock is moving from IPv6 to IPv4 (sk_prot), so
246 * remove it from the refcnt debug socks count in the
247 * original family...
248 */
249 sk_refcnt_debug_dec(sk);
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (sk->sk_protocol == IPPROTO_TCP) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800252 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700254 sock_prot_inuse_add(net, sk->sk_prot, -1);
255 sock_prot_inuse_add(net, &tcp_prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 local_bh_enable();
257 sk->sk_prot = &tcp_prot;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800258 icsk->icsk_af_ops = &ipv4_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 sk->sk_socket->ops = &inet_stream_ops;
260 sk->sk_family = PF_INET;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800261 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 } else {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800263 struct proto *prot = &udp_prot;
264
David S. Millerdb8dac22008-03-06 16:22:02 -0800265 if (sk->sk_protocol == IPPROTO_UDPLITE)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800266 prot = &udplite_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700268 sock_prot_inuse_add(net, sk->sk_prot, -1);
269 sock_prot_inuse_add(net, prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 local_bh_enable();
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800271 sk->sk_prot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 sk->sk_socket->ops = &inet_dgram_ops;
273 sk->sk_family = PF_INET;
274 }
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800275 opt = xchg((__force struct ipv6_txoptions **)&np->opt,
276 NULL);
277 if (opt) {
278 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
279 txopt_put(opt);
280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 pktopt = xchg(&np->pktoptions, NULL);
Wei Yongjun800d55f2009-02-23 21:45:33 +0000282 kfree_skb(pktopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700284 /*
285 * ... and add it to the refcnt debug socks count
286 * in the new family. -acme
287 */
288 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 module_put(THIS_MODULE);
290 retv = 0;
291 break;
292 }
293 goto e_inval;
294
295 case IPV6_V6ONLY:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900296 if (optlen < sizeof(int) ||
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000297 inet_sk(sk)->inet_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 goto e_inval;
Eric Dumazet9fe516b2014-06-27 08:36:16 -0700299 sk->sk_ipv6only = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 retv = 0;
301 break;
302
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900303 case IPV6_RECVPKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900304 if (optlen < sizeof(int))
305 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 np->rxopt.bits.rxinfo = valbool;
307 retv = 0;
308 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900309
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900310 case IPV6_2292PKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900311 if (optlen < sizeof(int))
312 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900313 np->rxopt.bits.rxoinfo = valbool;
314 retv = 0;
315 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900317 case IPV6_RECVHOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900318 if (optlen < sizeof(int))
319 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 np->rxopt.bits.rxhlim = valbool;
321 retv = 0;
322 break;
323
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900324 case IPV6_2292HOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900325 if (optlen < sizeof(int))
326 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900327 np->rxopt.bits.rxohlim = valbool;
328 retv = 0;
329 break;
330
331 case IPV6_RECVRTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900332 if (optlen < sizeof(int))
333 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900334 np->rxopt.bits.srcrt = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 retv = 0;
336 break;
337
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900338 case IPV6_2292RTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900339 if (optlen < sizeof(int))
340 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900341 np->rxopt.bits.osrcrt = valbool;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900342 retv = 0;
343 break;
344
345 case IPV6_RECVHOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900346 if (optlen < sizeof(int))
347 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 np->rxopt.bits.hopopts = valbool;
349 retv = 0;
350 break;
351
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900352 case IPV6_2292HOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900353 if (optlen < sizeof(int))
354 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900355 np->rxopt.bits.ohopopts = valbool;
356 retv = 0;
357 break;
358
359 case IPV6_RECVDSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900360 if (optlen < sizeof(int))
361 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 np->rxopt.bits.dstopts = valbool;
363 retv = 0;
364 break;
365
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900366 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900367 if (optlen < sizeof(int))
368 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900369 np->rxopt.bits.odstopts = valbool;
370 retv = 0;
371 break;
372
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900373 case IPV6_TCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900374 if (optlen < sizeof(int))
375 goto e_inval;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700376 if (val < -1 || val > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900377 goto e_inval;
Gerrit Renker26ced1e2009-08-09 08:12:49 +0000378 /* RFC 3542, 6.5: default traffic class of 0x0 */
379 if (val == -1)
380 val = 0;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900381 np->tclass = val;
382 retv = 0;
383 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900384
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900385 case IPV6_RECVTCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900386 if (optlen < sizeof(int))
387 goto e_inval;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900388 np->rxopt.bits.rxtclass = valbool;
389 retv = 0;
390 break;
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 case IPV6_FLOWINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900393 if (optlen < sizeof(int))
394 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 np->rxopt.bits.rxflow = valbool;
396 retv = 0;
397 break;
398
Brian Haley793b14732010-04-23 11:26:07 +0000399 case IPV6_RECVPATHMTU:
400 if (optlen < sizeof(int))
401 goto e_inval;
402 np->rxopt.bits.rxpmtu = valbool;
403 retv = 0;
404 break;
405
Balazs Scheidler6c468622010-10-21 16:08:28 +0200406 case IPV6_TRANSPARENT:
Maciej Żenczykowski35fc59c2019-11-21 13:19:08 -0800407 if (valbool && !ns_capable(net->user_ns, CAP_NET_RAW) &&
408 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
Balazs Scheidlerb8894162010-10-23 04:48:14 +0000409 retv = -EPERM;
410 break;
411 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200412 if (optlen < sizeof(int))
413 goto e_inval;
414 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
415 inet_sk(sk)->transparent = valbool;
416 retv = 0;
417 break;
418
Maciej Żenczykowski84e14fe2017-09-26 21:32:42 -0700419 case IPV6_FREEBIND:
420 if (optlen < sizeof(int))
421 goto e_inval;
422 /* we also don't have a separate freebind bit for IPV6 */
423 inet_sk(sk)->freebind = valbool;
424 retv = 0;
425 break;
426
Balazs Scheidler6c468622010-10-21 16:08:28 +0200427 case IPV6_RECVORIGDSTADDR:
428 if (optlen < sizeof(int))
429 goto e_inval;
430 np->rxopt.bits.rxorigdstaddr = valbool;
431 retv = 0;
432 break;
433
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900434 case IPV6_HOPOPTS:
435 case IPV6_RTHDRDSTOPTS:
436 case IPV6_RTHDR:
437 case IPV6_DSTOPTS:
438 {
439 struct ipv6_txoptions *opt;
Paul Moorea9ba23d2018-07-04 09:58:05 -0400440 struct ipv6_opt_hdr *new = NULL;
441
442 /* hop-by-hop / destination options are privileged option */
443 retv = -EPERM;
444 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
445 break;
Shan Weiaea74272008-06-19 16:29:39 -0700446
447 /* remove any sticky options header with a zero option
448 * length, per RFC3542.
449 */
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900450 if (optlen == 0)
Luiz Capitulinocb422c42005-11-14 21:43:36 -0800451 optval = NULL;
Ian Morris63159f22015-03-29 14:00:04 +0100452 else if (!optval)
Yang Hongyangcfb266c2008-08-03 18:16:15 -0700453 goto e_inval;
Shan Weiaea74272008-06-19 16:29:39 -0700454 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
455 optlen & 0x7 || optlen > 8 * 255)
456 goto e_inval;
Paul Moorea9ba23d2018-07-04 09:58:05 -0400457 else {
458 new = memdup_user(optval, optlen);
459 if (IS_ERR(new)) {
460 retv = PTR_ERR(new);
461 break;
462 }
463 if (unlikely(ipv6_optlen(new) > optlen)) {
464 kfree(new);
465 goto e_inval;
466 }
467 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900468
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200469 opt = rcu_dereference_protected(np->opt,
470 lockdep_sock_is_held(sk));
Paul Moorea9ba23d2018-07-04 09:58:05 -0400471 opt = ipv6_renew_options(sk, opt, optname, new);
472 kfree(new);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900473 if (IS_ERR(opt)) {
474 retv = PTR_ERR(opt);
475 break;
476 }
477
478 /* routing header option needs extra check */
Brian Haley6e093d92008-11-12 22:59:21 -0800479 retv = -EINVAL;
Olaf Kirchdfee0a72007-03-09 13:55:38 -0800480 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900481 struct ipv6_rt_hdr *rthdr = opt->srcrt;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700482 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000483#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700484 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800485 if (rthdr->hdrlen != 2 ||
486 rthdr->segments_left != 1)
487 goto sticky_done;
488
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700489 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700490#endif
David Lebruna149e7c2016-11-08 14:59:21 +0100491 case IPV6_SRCRT_TYPE_4:
492 {
493 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)
494 opt->srcrt;
495
496 if (!seg6_validate_srh(srh, optlen))
497 goto sticky_done;
498 break;
499 }
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700500 default:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900501 goto sticky_done;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700502 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900503 }
504
505 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700506 opt = ipv6_update_options(sk, opt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900507sticky_done:
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800508 if (opt) {
509 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
510 txopt_put(opt);
511 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900512 break;
513 }
514
Yang Hongyangb24a2512008-12-16 02:06:23 -0800515 case IPV6_PKTINFO:
516 {
517 struct in6_pktinfo pkt;
518
519 if (optlen == 0)
520 goto e_inval;
Ian Morris63159f22015-03-29 14:00:04 +0100521 else if (optlen < sizeof(struct in6_pktinfo) || !optval)
Yang Hongyangb24a2512008-12-16 02:06:23 -0800522 goto e_inval;
523
Ilpo Järvinen914d1162009-01-04 17:27:31 -0800524 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
Yang Hongyangb24a2512008-12-16 02:06:23 -0800525 retv = -EFAULT;
526 break;
527 }
Mike Manningd839a0e2018-11-07 15:36:08 +0000528 if (!sk_dev_equal_l3scope(sk, pkt.ipi6_ifindex))
Yang Hongyangb24a2512008-12-16 02:06:23 -0800529 goto e_inval;
530
531 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000532 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
Yang Hongyangb24a2512008-12-16 02:06:23 -0800533 retv = 0;
534 break;
535 }
536
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900537 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 {
539 struct ipv6_txoptions *opt = NULL;
540 struct msghdr msg;
David S. Miller4c9483b2011-03-12 16:22:43 -0500541 struct flowi6 fl6;
Wei Wang26879da2016-05-02 21:40:07 -0700542 struct ipcm6_cookie ipc6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
David S. Miller4c9483b2011-03-12 16:22:43 -0500544 memset(&fl6, 0, sizeof(fl6));
545 fl6.flowi6_oif = sk->sk_bound_dev_if;
546 fl6.flowi6_mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 if (optlen == 0)
549 goto update;
550
551 /* 1K is probably excessive
552 * 1K is surely not enough, 2K per standard header is 16K.
553 */
554 retv = -EINVAL;
555 if (optlen > 64*1024)
556 break;
557
558 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
559 retv = -ENOBUFS;
Ian Morris63159f22015-03-29 14:00:04 +0100560 if (!opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 break;
562
563 memset(opt, 0, sizeof(*opt));
Reshetova, Elena0aeea212017-07-04 09:34:54 +0300564 refcount_set(&opt->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 opt->tot_len = sizeof(*opt) + optlen;
566 retv = -EFAULT;
567 if (copy_from_user(opt+1, optval, optlen))
568 goto done;
569
570 msg.msg_controllen = optlen;
Ian Morris67ba4152014-08-24 21:53:10 +0100571 msg.msg_control = (void *)(opt+1);
Wei Wang26879da2016-05-02 21:40:07 -0700572 ipc6.opt = opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Willem de Bruijn5fdaa882018-07-06 10:12:57 -0400574 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (retv)
576 goto done;
577update:
578 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700579 opt = ipv6_update_options(sk, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580done:
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800581 if (opt) {
582 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
583 txopt_put(opt);
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 break;
586 }
587 case IPV6_UNICAST_HOPS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900588 if (optlen < sizeof(int))
589 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (val > 255 || val < -1)
591 goto e_inval;
592 np->hop_limit = val;
593 retv = 0;
594 break;
595
596 case IPV6_MULTICAST_HOPS:
597 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900598 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900599 if (optlen < sizeof(int))
600 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (val > 255 || val < -1)
602 goto e_inval;
Li Wei2a38e6d2011-11-27 21:33:34 +0000603 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 retv = 0;
605 break;
606
607 case IPV6_MULTICAST_LOOP:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900608 if (optlen < sizeof(int))
609 goto e_inval;
YOSHIFUJI Hideaki28d44882008-06-12 03:14:51 +0900610 if (val != valbool)
611 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 np->mc_loop = valbool;
613 retv = 0;
614 break;
615
Erich E. Hooverc4062df2012-02-08 09:11:08 +0000616 case IPV6_UNICAST_IF:
617 {
618 struct net_device *dev = NULL;
619 int ifindex;
620
621 if (optlen != sizeof(int))
622 goto e_inval;
623
624 ifindex = (__force int)ntohl((__force __be32)val);
625 if (ifindex == 0) {
626 np->ucast_oif = 0;
627 retv = 0;
628 break;
629 }
630
631 dev = dev_get_by_index(net, ifindex);
632 retv = -EADDRNOTAVAIL;
633 if (!dev)
634 break;
635 dev_put(dev);
636
637 retv = -EINVAL;
638 if (sk->sk_bound_dev_if)
639 break;
640
641 np->ucast_oif = ifindex;
642 retv = 0;
643 break;
644 }
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 case IPV6_MULTICAST_IF:
647 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900648 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900649 if (optlen < sizeof(int))
650 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Brian Haley4953f0f2007-10-11 14:39:29 -0700652 if (val) {
Eric Dumazet55b80502009-10-19 06:41:58 +0000653 struct net_device *dev;
David Ahern7bb387c2016-12-29 15:39:37 -0800654 int midx;
Eric Dumazet55b80502009-10-19 06:41:58 +0000655
David Ahern7bb387c2016-12-29 15:39:37 -0800656 rcu_read_lock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700657
David Ahern7bb387c2016-12-29 15:39:37 -0800658 dev = dev_get_by_index_rcu(net, val);
Eric Dumazet55b80502009-10-19 06:41:58 +0000659 if (!dev) {
David Ahern7bb387c2016-12-29 15:39:37 -0800660 rcu_read_unlock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700661 retv = -ENODEV;
662 break;
663 }
David Ahern7bb387c2016-12-29 15:39:37 -0800664 midx = l3mdev_master_ifindex_rcu(dev);
665
666 rcu_read_unlock();
667
668 if (sk->sk_bound_dev_if &&
669 sk->sk_bound_dev_if != val &&
670 (!midx || midx != sk->sk_bound_dev_if))
671 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673 np->mcast_oif = val;
674 retv = 0;
675 break;
676 case IPV6_ADD_MEMBERSHIP:
677 case IPV6_DROP_MEMBERSHIP:
678 {
679 struct ipv6_mreq mreq;
680
Wang Chena28398b2008-04-07 09:42:07 +0800681 if (optlen < sizeof(struct ipv6_mreq))
682 goto e_inval;
683
Flavio Leitnera96fb492007-08-24 22:16:39 -0700684 retv = -EPROTO;
685 if (inet_sk(sk)->is_icsk)
686 break;
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 retv = -EFAULT;
689 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
690 break;
691
692 if (optname == IPV6_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300693 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300695 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 break;
697 }
698 case IPV6_JOIN_ANYCAST:
699 case IPV6_LEAVE_ANYCAST:
700 {
701 struct ipv6_mreq mreq;
702
Wang Chena28398b2008-04-07 09:42:07 +0800703 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 goto e_inval;
705
706 retv = -EFAULT;
707 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
708 break;
709
710 if (optname == IPV6_JOIN_ANYCAST)
711 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
712 else
713 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
714 break;
715 }
Andre Naujoks15033f02018-09-10 10:27:15 +0200716 case IPV6_MULTICAST_ALL:
717 if (optlen < sizeof(int))
718 goto e_inval;
719 np->mc_all = valbool;
720 retv = 0;
721 break;
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 case MCAST_JOIN_GROUP:
724 case MCAST_LEAVE_GROUP:
725 {
726 struct group_req greq;
727 struct sockaddr_in6 *psin6;
728
Wang Chena28398b2008-04-07 09:42:07 +0800729 if (optlen < sizeof(struct group_req))
730 goto e_inval;
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 retv = -EFAULT;
733 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
734 break;
735 if (greq.gr_group.ss_family != AF_INET6) {
736 retv = -EADDRNOTAVAIL;
737 break;
738 }
739 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
740 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300741 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
742 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300744 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
745 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 break;
747 }
748 case MCAST_JOIN_SOURCE_GROUP:
749 case MCAST_LEAVE_SOURCE_GROUP:
750 case MCAST_BLOCK_SOURCE:
751 case MCAST_UNBLOCK_SOURCE:
752 {
753 struct group_source_req greqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Wang Chena28398b2008-04-07 09:42:07 +0800755 if (optlen < sizeof(struct group_source_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 goto e_inval;
757 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
758 retv = -EFAULT;
759 break;
760 }
Al Virofcfa0b02020-04-27 11:00:01 -0400761 retv = do_ipv6_mcast_group_source(sk, optname, &greqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 break;
763 }
764 case MCAST_MSFILTER:
765 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct group_filter *gsf;
767
768 if (optlen < GROUP_FILTER_SIZE(0))
769 goto e_inval;
770 if (optlen > sysctl_optmem_max) {
771 retv = -ENOBUFS;
772 break;
773 }
Al Viro43727da2017-05-13 18:22:53 -0400774 gsf = memdup_user(optval, optlen);
775 if (IS_ERR(gsf)) {
776 retv = PTR_ERR(gsf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 break;
778 }
779 /* numsrc >= (4G-140)/128 overflow in 32 bits */
780 if (gsf->gf_numsrc >= 0x1ffffffU ||
781 gsf->gf_numsrc > sysctl_mld_max_msf) {
782 kfree(gsf);
783 retv = -ENOBUFS;
784 break;
785 }
786 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
787 kfree(gsf);
788 retv = -EINVAL;
789 break;
790 }
Al Virod59eb172020-03-30 15:43:10 -0400791 retv = ip6_mc_msfilter(sk, gsf, gsf->gf_slist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 kfree(gsf);
793
794 break;
795 }
796 case IPV6_ROUTER_ALERT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900797 if (optlen < sizeof(int))
798 goto e_inval;
Denis V. Lunev725a8ff2008-07-19 00:28:58 -0700799 retv = ip6_ra_control(sk, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 break;
Francesco Ruggeri9036b2f2019-03-01 15:31:03 -0800801 case IPV6_ROUTER_ALERT_ISOLATE:
802 if (optlen < sizeof(int))
803 goto e_inval;
804 np->rtalert_isolate = valbool;
805 retv = 0;
806 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 case IPV6_MTU_DISCOVER:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900808 if (optlen < sizeof(int))
809 goto e_inval;
Hannes Frederic Sowa0b952272014-02-26 01:20:43 +0100810 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 goto e_inval;
812 np->pmtudisc = val;
813 retv = 0;
814 break;
815 case IPV6_MTU:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900816 if (optlen < sizeof(int))
817 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (val && val < IPV6_MIN_MTU)
819 goto e_inval;
820 np->frag_size = val;
821 retv = 0;
822 break;
823 case IPV6_RECVERR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900824 if (optlen < sizeof(int))
825 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 np->recverr = valbool;
827 if (!val)
828 skb_queue_purge(&sk->sk_error_queue);
829 retv = 0;
830 break;
831 case IPV6_FLOWINFO_SEND:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900832 if (optlen < sizeof(int))
833 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 np->sndflow = valbool;
835 retv = 0;
836 break;
837 case IPV6_FLOWLABEL_MGR:
838 retv = ipv6_flowlabel_opt(sk, optval, optlen);
839 break;
840 case IPV6_IPSEC_POLICY:
841 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700842 retv = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000843 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700844 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 retv = xfrm_user_policy(sk, optname, optval, optlen);
846 break;
847
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900848 case IPV6_ADDR_PREFERENCES:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900849 if (optlen < sizeof(int))
850 goto e_inval;
Christoph Hellwig18d5ad62020-05-28 07:12:33 +0200851 retv = __ip6_sock_set_addr_preferences(sk, val);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900852 break;
Stephen Hemmingere802af92010-04-22 15:24:53 -0700853 case IPV6_MINHOPCOUNT:
854 if (optlen < sizeof(int))
855 goto e_inval;
856 if (val < 0 || val > 255)
857 goto e_inval;
858 np->min_hopcount = val;
Hannes Frederic Sowad4596ba2012-11-10 19:52:34 +0000859 retv = 0;
Brian Haley793b14732010-04-23 11:26:07 +0000860 break;
861 case IPV6_DONTFRAG:
862 np->dontfrag = valbool;
Stephen Hemmingere802af92010-04-22 15:24:53 -0700863 retv = 0;
864 break;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700865 case IPV6_AUTOFLOWLABEL:
866 np->autoflowlabel = valbool;
Shaohua Li513674b2017-12-20 12:10:21 -0800867 np->autoflowlabel_set = 1;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700868 retv = 0;
869 break;
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -0400870 case IPV6_RECVFRAGSIZE:
871 np->rxopt.bits.recvfragsize = valbool;
872 retv = 0;
873 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300877 if (needs_rtnl)
878 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return retv;
881
882e_inval:
883 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300884 if (needs_rtnl)
885 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return -EINVAL;
887}
888
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800889int ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700890 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800891{
892 int err;
893
894 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
895 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
896
897 if (level != SOL_IPV6)
898 return -ENOPROTOOPT;
899
900 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
901#ifdef CONFIG_NETFILTER
902 /* we need to exclude all possible ENOPROTOOPTs except default case */
903 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +0100904 optname != IPV6_XFRM_POLICY)
905 err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800906#endif
907 return err;
908}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900909EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800910
911#ifdef CONFIG_COMPAT
912int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700913 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800914{
915 int err;
916
917 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800918 if (udp_prot.compat_setsockopt != NULL)
919 return udp_prot.compat_setsockopt(sk, level, optname,
920 optval, optlen);
921 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800922 }
923
924 if (level != SOL_IPV6)
925 return -ENOPROTOOPT;
926
Al Viroe9c375f2020-05-09 21:16:36 -0400927 switch (optname) {
928 case MCAST_JOIN_GROUP:
929 case MCAST_LEAVE_GROUP:
Al Viro2f984f12020-04-26 19:56:22 -0400930 {
931 struct compat_group_req __user *gr32 = (void __user *)optval;
932 struct group_req greq;
933 struct sockaddr_in6 *psin6 = (struct sockaddr_in6 *)&greq.gr_group;
934
935 if (optlen < sizeof(struct compat_group_req))
936 return -EINVAL;
937
938 if (get_user(greq.gr_interface, &gr32->gr_interface) ||
939 copy_from_user(&greq.gr_group, &gr32->gr_group,
940 sizeof(greq.gr_group)))
941 return -EFAULT;
942
943 if (greq.gr_group.ss_family != AF_INET6)
944 return -EADDRNOTAVAIL;
945
946 rtnl_lock();
947 lock_sock(sk);
948 if (optname == MCAST_JOIN_GROUP)
949 err = ipv6_sock_mc_join(sk, greq.gr_interface,
950 &psin6->sin6_addr);
951 else
952 err = ipv6_sock_mc_drop(sk, greq.gr_interface,
953 &psin6->sin6_addr);
954 release_sock(sk);
955 rtnl_unlock();
956 return err;
957 }
Al Viroe9c375f2020-05-09 21:16:36 -0400958 case MCAST_JOIN_SOURCE_GROUP:
959 case MCAST_LEAVE_SOURCE_GROUP:
960 case MCAST_BLOCK_SOURCE:
961 case MCAST_UNBLOCK_SOURCE:
Al Virob212c322020-04-27 11:37:02 -0400962 {
963 struct compat_group_source_req __user *gsr32 = (void __user *)optval;
964 struct group_source_req greqs;
965
966 if (optlen < sizeof(struct compat_group_source_req))
967 return -EINVAL;
968
969 if (get_user(greqs.gsr_interface, &gsr32->gsr_interface) ||
970 copy_from_user(&greqs.gsr_group, &gsr32->gsr_group,
971 sizeof(greqs.gsr_group)) ||
972 copy_from_user(&greqs.gsr_source, &gsr32->gsr_source,
973 sizeof(greqs.gsr_source)))
974 return -EFAULT;
975
976 rtnl_lock();
977 lock_sock(sk);
978 err = do_ipv6_mcast_group_source(sk, optname, &greqs);
979 release_sock(sk);
980 rtnl_unlock();
981 return err;
982 }
Al Viro168a2cc2020-03-30 16:40:04 -0400983 case MCAST_MSFILTER:
984 {
985 const int size0 = offsetof(struct compat_group_filter, gf_slist);
986 struct compat_group_filter *gf32;
987 void *p;
988 int n;
989
990 if (optlen < size0)
991 return -EINVAL;
992 if (optlen > sysctl_optmem_max - 4)
993 return -ENOBUFS;
994
995 p = kmalloc(optlen + 4, GFP_KERNEL);
996 if (!p)
997 return -ENOMEM;
998
999 gf32 = p + 4; /* we want ->gf_group and ->gf_slist aligned */
1000 if (copy_from_user(gf32, optval, optlen)) {
1001 err = -EFAULT;
1002 goto mc_msf_out;
1003 }
1004
1005 n = gf32->gf_numsrc;
1006 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1007 if (n >= 0x1ffffffU ||
1008 n > sysctl_mld_max_msf) {
1009 err = -ENOBUFS;
1010 goto mc_msf_out;
1011 }
1012 if (offsetof(struct compat_group_filter, gf_slist[n]) > optlen) {
1013 err = -EINVAL;
1014 goto mc_msf_out;
1015 }
1016
1017 rtnl_lock();
1018 lock_sock(sk);
1019 err = ip6_mc_msfilter(sk, &(struct group_filter){
1020 .gf_interface = gf32->gf_interface,
1021 .gf_group = gf32->gf_group,
1022 .gf_fmode = gf32->gf_fmode,
1023 .gf_numsrc = gf32->gf_numsrc}, gf32->gf_slist);
1024 release_sock(sk);
1025 rtnl_unlock();
1026mc_msf_out:
1027 kfree(p);
1028 return err;
1029 }
Al Viroe9c375f2020-05-09 21:16:36 -04001030 }
David L Stevensdae50292008-04-27 01:06:07 -07001031
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001032 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
1033#ifdef CONFIG_NETFILTER
1034 /* we need to exclude all possible ENOPROTOOPTs except default case */
1035 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +01001036 optname != IPV6_XFRM_POLICY)
1037 err = compat_nf_setsockopt(sk, PF_INET6, optname, optval,
1038 optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001039#endif
1040 return err;
1041}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001042EXPORT_SYMBOL(compat_ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001043#endif
1044
David S. Miller286930792007-03-07 12:50:46 -08001045static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001046 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001047{
David S. Miller286930792007-03-07 12:50:46 -08001048 struct ipv6_opt_hdr *hdr;
1049
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001050 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001051 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001052
Ian Morris67ba4152014-08-24 21:53:10 +01001053 switch (optname) {
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001054 case IPV6_HOPOPTS:
1055 hdr = opt->hopopt;
1056 break;
1057 case IPV6_RTHDRDSTOPTS:
1058 hdr = opt->dst0opt;
1059 break;
1060 case IPV6_RTHDR:
1061 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
1062 break;
1063 case IPV6_DSTOPTS:
1064 hdr = opt->dst1opt;
1065 break;
1066 default:
1067 return -EINVAL; /* should not happen */
1068 }
1069
1070 if (!hdr)
1071 return 0;
David S. Miller286930792007-03-07 12:50:46 -08001072
Chris Wrightd2b02ed2007-03-09 16:19:17 -08001073 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -07001074 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001075 return -EFAULT;
Yang Hongyang95b496b2008-05-28 16:27:28 +08001076 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001077}
1078
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001079static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001080 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
1082 struct ipv6_pinfo *np = inet6_sk(sk);
1083 int len;
1084 int val;
1085
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001086 if (ip6_mroute_opt(optname))
1087 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (get_user(len, optlen))
1090 return -EFAULT;
1091 switch (optname) {
1092 case IPV6_ADDRFORM:
1093 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001094 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 sk->sk_protocol != IPPROTO_TCP)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +09001096 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (sk->sk_state != TCP_ESTABLISHED)
1098 return -ENOTCONN;
1099 val = sk->sk_family;
1100 break;
1101 case MCAST_MSFILTER:
1102 {
Al Viro931ca7a2020-03-29 17:18:30 -04001103 struct group_filter __user *p = (void __user *)optval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 struct group_filter gsf;
Al Viro931ca7a2020-03-29 17:18:30 -04001105 const int size0 = offsetof(struct group_filter, gf_slist);
1106 int num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 int err;
1108
Al Viro931ca7a2020-03-29 17:18:30 -04001109 if (len < size0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return -EINVAL;
Al Viro931ca7a2020-03-29 17:18:30 -04001111 if (copy_from_user(&gsf, p, size0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return -EFAULT;
YOSHIFUJI Hideaki20c61fbd2008-04-28 14:40:55 +09001113 if (gsf.gf_group.ss_family != AF_INET6)
1114 return -EADDRNOTAVAIL;
Al Viro931ca7a2020-03-29 17:18:30 -04001115 num = gsf.gf_numsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 lock_sock(sk);
Al Viro931ca7a2020-03-29 17:18:30 -04001117 err = ip6_mc_msfget(sk, &gsf, p->gf_slist);
1118 if (!err) {
1119 if (num > gsf.gf_numsrc)
1120 num = gsf.gf_numsrc;
1121 if (put_user(GROUP_FILTER_SIZE(num), optlen) ||
1122 copy_to_user(p, &gsf, size0))
1123 err = -EFAULT;
1124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 release_sock(sk);
1126 return err;
1127 }
1128
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001129 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 {
1131 struct msghdr msg;
1132 struct sk_buff *skb;
1133
1134 if (sk->sk_type != SOCK_STREAM)
1135 return -ENOPROTOOPT;
1136
1137 msg.msg_control = optval;
1138 msg.msg_controllen = len;
Daniel Baluta98e77432011-08-19 03:19:07 -07001139 msg.msg_flags = flags;
Christoph Hellwig1b2f08d2020-05-13 21:36:41 +02001140 msg.msg_control_is_user = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 lock_sock(sk);
1143 skb = np->pktoptions;
1144 if (skb)
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001145 ip6_datagram_recv_ctl(sk, &msg, skb);
Eric Dumazet1dc7b902015-01-21 03:45:42 -08001146 release_sock(sk);
1147 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (np->rxopt.bits.rxinfo) {
1149 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001150 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1151 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001152 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1154 }
1155 if (np->rxopt.bits.rxhlim) {
1156 int hlim = np->mcast_hops;
1157 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1158 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001159 if (np->rxopt.bits.rxtclass) {
Li RongQingd76ed222014-01-15 17:03:30 +08001160 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1161
Jiri Benc4c507d22012-02-09 09:35:49 +00001162 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1163 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001164 if (np->rxopt.bits.rxoinfo) {
1165 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001166 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1167 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001168 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1169 np->sticky_pktinfo.ipi6_addr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001170 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1171 }
1172 if (np->rxopt.bits.rxohlim) {
1173 int hlim = np->mcast_hops;
1174 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1175 }
Florent Fourcot1397ed32013-12-08 15:46:57 +01001176 if (np->rxopt.bits.rxflow) {
Florent Fourcot68536052013-12-12 17:07:58 +01001177 __be32 flowinfo = np->rcv_flowinfo;
1178
Florent Fourcot1397ed32013-12-08 15:46:57 +01001179 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182 len -= msg.msg_controllen;
1183 return put_user(len, optlen);
1184 }
1185 case IPV6_MTU:
1186 {
1187 struct dst_entry *dst;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001188
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001189 val = 0;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001190 rcu_read_lock();
1191 dst = __sk_dst_get(sk);
1192 if (dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 val = dst_mtu(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001194 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (!val)
1196 return -ENOTCONN;
1197 break;
1198 }
1199
1200 case IPV6_V6ONLY:
Eric Dumazet9fe516b2014-06-27 08:36:16 -07001201 val = sk->sk_ipv6only;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 break;
1203
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001204 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 val = np->rxopt.bits.rxinfo;
1206 break;
1207
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001208 case IPV6_2292PKTINFO:
1209 val = np->rxopt.bits.rxoinfo;
1210 break;
1211
1212 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 val = np->rxopt.bits.rxhlim;
1214 break;
1215
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001216 case IPV6_2292HOPLIMIT:
1217 val = np->rxopt.bits.rxohlim;
1218 break;
1219
1220 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 val = np->rxopt.bits.srcrt;
1222 break;
1223
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001224 case IPV6_2292RTHDR:
1225 val = np->rxopt.bits.osrcrt;
1226 break;
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001229 case IPV6_RTHDRDSTOPTS:
1230 case IPV6_RTHDR:
1231 case IPV6_DSTOPTS:
1232 {
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001233 struct ipv6_txoptions *opt;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001234
1235 lock_sock(sk);
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001236 opt = rcu_dereference_protected(np->opt,
1237 lockdep_sock_is_held(sk));
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001238 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001239 release_sock(sk);
Yang Hongyang05335c22008-05-28 16:23:47 +08001240 /* check if ipv6_getsockopt_sticky() returns err code */
1241 if (len < 0)
1242 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001243 return put_user(len, optlen);
1244 }
1245
1246 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 val = np->rxopt.bits.hopopts;
1248 break;
1249
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001250 case IPV6_2292HOPOPTS:
1251 val = np->rxopt.bits.ohopopts;
1252 break;
1253
1254 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 val = np->rxopt.bits.dstopts;
1256 break;
1257
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001258 case IPV6_2292DSTOPTS:
1259 val = np->rxopt.bits.odstopts;
1260 break;
1261
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001262 case IPV6_TCLASS:
1263 val = np->tclass;
1264 break;
1265
1266 case IPV6_RECVTCLASS:
1267 val = np->rxopt.bits.rxtclass;
1268 break;
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 case IPV6_FLOWINFO:
1271 val = np->rxopt.bits.rxflow;
1272 break;
1273
Brian Haley793b14732010-04-23 11:26:07 +00001274 case IPV6_RECVPATHMTU:
1275 val = np->rxopt.bits.rxpmtu;
1276 break;
1277
1278 case IPV6_PATHMTU:
1279 {
1280 struct dst_entry *dst;
1281 struct ip6_mtuinfo mtuinfo;
1282
1283 if (len < sizeof(mtuinfo))
1284 return -EINVAL;
1285
1286 len = sizeof(mtuinfo);
1287 memset(&mtuinfo, 0, sizeof(mtuinfo));
1288
1289 rcu_read_lock();
1290 dst = __sk_dst_get(sk);
1291 if (dst)
1292 mtuinfo.ip6m_mtu = dst_mtu(dst);
1293 rcu_read_unlock();
1294 if (!mtuinfo.ip6m_mtu)
1295 return -ENOTCONN;
1296
1297 if (put_user(len, optlen))
1298 return -EFAULT;
1299 if (copy_to_user(optval, &mtuinfo, len))
1300 return -EFAULT;
1301
1302 return 0;
Brian Haley793b14732010-04-23 11:26:07 +00001303 }
1304
Balazs Scheidler6c468622010-10-21 16:08:28 +02001305 case IPV6_TRANSPARENT:
1306 val = inet_sk(sk)->transparent;
1307 break;
1308
Maciej Żenczykowski84e14fe2017-09-26 21:32:42 -07001309 case IPV6_FREEBIND:
1310 val = inet_sk(sk)->freebind;
1311 break;
1312
Balazs Scheidler6c468622010-10-21 16:08:28 +02001313 case IPV6_RECVORIGDSTADDR:
1314 val = np->rxopt.bits.rxorigdstaddr;
1315 break;
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -08001319 {
1320 struct dst_entry *dst;
1321
1322 if (optname == IPV6_UNICAST_HOPS)
1323 val = np->hop_limit;
1324 else
1325 val = np->mcast_hops;
1326
Eric Dumazetb6c67122010-04-08 23:03:29 +00001327 if (val < 0) {
1328 rcu_read_lock();
1329 dst = __sk_dst_get(sk);
1330 if (dst)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001331 val = ip6_dst_hoplimit(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001332 rcu_read_unlock();
Brian Haleybefffe92006-12-12 17:09:49 -08001333 }
Eric Dumazetb6c67122010-04-08 23:03:29 +00001334
Brian Haleybefffe92006-12-12 17:09:49 -08001335 if (val < 0)
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001336 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 break;
Brian Haleybefffe92006-12-12 17:09:49 -08001338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 case IPV6_MULTICAST_LOOP:
1341 val = np->mc_loop;
1342 break;
1343
1344 case IPV6_MULTICAST_IF:
1345 val = np->mcast_oif;
1346 break;
1347
Andre Naujoks15033f02018-09-10 10:27:15 +02001348 case IPV6_MULTICAST_ALL:
1349 val = np->mc_all;
1350 break;
1351
Erich E. Hooverc4062df2012-02-08 09:11:08 +00001352 case IPV6_UNICAST_IF:
1353 val = (__force int)htonl((__u32) np->ucast_oif);
1354 break;
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 case IPV6_MTU_DISCOVER:
1357 val = np->pmtudisc;
1358 break;
1359
1360 case IPV6_RECVERR:
1361 val = np->recverr;
1362 break;
1363
1364 case IPV6_FLOWINFO_SEND:
1365 val = np->sndflow;
1366 break;
1367
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001368 case IPV6_FLOWLABEL_MGR:
1369 {
1370 struct in6_flowlabel_req freq;
Florent Fourcot46e5f402014-01-17 17:15:04 +01001371 int flags;
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001372
1373 if (len < sizeof(freq))
1374 return -EINVAL;
1375
1376 if (copy_from_user(&freq, optval, sizeof(freq)))
1377 return -EFAULT;
1378
1379 if (freq.flr_action != IPV6_FL_A_GET)
1380 return -EINVAL;
1381
1382 len = sizeof(freq);
Florent Fourcot46e5f402014-01-17 17:15:04 +01001383 flags = freq.flr_flags;
1384
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001385 memset(&freq, 0, sizeof(freq));
1386
Florent Fourcot46e5f402014-01-17 17:15:04 +01001387 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001388 if (val < 0)
1389 return val;
1390
1391 if (put_user(len, optlen))
1392 return -EFAULT;
1393 if (copy_to_user(optval, &freq, len))
1394 return -EFAULT;
1395
1396 return 0;
1397 }
1398
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001399 case IPV6_ADDR_PREFERENCES:
1400 val = 0;
1401
1402 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1403 val |= IPV6_PREFER_SRC_TMP;
1404 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1405 val |= IPV6_PREFER_SRC_PUBLIC;
1406 else {
1407 /* XXX: should we return system default? */
1408 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1409 }
1410
1411 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1412 val |= IPV6_PREFER_SRC_COA;
1413 else
1414 val |= IPV6_PREFER_SRC_HOME;
1415 break;
1416
Stephen Hemmingere802af92010-04-22 15:24:53 -07001417 case IPV6_MINHOPCOUNT:
1418 val = np->min_hopcount;
1419 break;
1420
Brian Haley793b14732010-04-23 11:26:07 +00001421 case IPV6_DONTFRAG:
1422 val = np->dontfrag;
1423 break;
1424
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001425 case IPV6_AUTOFLOWLABEL:
Ben Hutchingse9191ff2018-01-22 20:06:42 +00001426 val = ip6_autoflowlabel(sock_net(sk), np);
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001427 break;
1428
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -04001429 case IPV6_RECVFRAGSIZE:
1430 val = np->rxopt.bits.recvfragsize;
1431 break;
1432
Francesco Ruggeri9036b2f2019-03-01 15:31:03 -08001433 case IPV6_ROUTER_ALERT_ISOLATE:
1434 val = np->rtalert_isolate;
1435 break;
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001438 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440 len = min_t(unsigned int, sizeof(int), len);
Ian Morris67ba4152014-08-24 21:53:10 +01001441 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001443 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return -EFAULT;
1445 return 0;
1446}
1447
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001448int ipv6_getsockopt(struct sock *sk, int level, int optname,
1449 char __user *optval, int __user *optlen)
1450{
1451 int err;
1452
1453 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1454 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1455
Ian Morris67ba4152014-08-24 21:53:10 +01001456 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001457 return -ENOPROTOOPT;
1458
Daniel Baluta98e77432011-08-19 03:19:07 -07001459 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001460#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001461 /* we need to exclude all possible ENOPROTOOPTs except default case */
1462 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001463 int len;
1464
1465 if (get_user(len, optlen))
1466 return -EFAULT;
1467
Paolo Abeni01ea3062018-02-08 12:19:00 +01001468 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001469 if (err >= 0)
1470 err = put_user(len, optlen);
1471 }
1472#endif
1473 return err;
1474}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001475EXPORT_SYMBOL(ipv6_getsockopt);
1476
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001477#ifdef CONFIG_COMPAT
1478int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001479 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001480{
1481 int err;
1482
1483 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001484 if (udp_prot.compat_getsockopt != NULL)
1485 return udp_prot.compat_getsockopt(sk, level, optname,
1486 optval, optlen);
1487 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001488 }
1489
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001490 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001491 return -ENOPROTOOPT;
1492
Al Viro0dfe6582020-03-29 22:08:59 -04001493 if (optname == MCAST_MSFILTER) {
1494 const int size0 = offsetof(struct compat_group_filter, gf_slist);
1495 struct compat_group_filter __user *p = (void __user *)optval;
1496 struct compat_group_filter gf32;
1497 struct group_filter gf;
1498 int ulen, err;
1499 int num;
1500
1501 if (get_user(ulen, optlen))
1502 return -EFAULT;
1503
1504 if (ulen < size0)
1505 return -EINVAL;
1506
1507 if (copy_from_user(&gf32, p, size0))
1508 return -EFAULT;
1509
1510 gf.gf_interface = gf32.gf_interface;
1511 gf.gf_fmode = gf32.gf_fmode;
1512 num = gf.gf_numsrc = gf32.gf_numsrc;
1513 gf.gf_group = gf32.gf_group;
1514
1515 if (gf.gf_group.ss_family != AF_INET6)
1516 return -EADDRNOTAVAIL;
1517 lock_sock(sk);
1518 err = ip6_mc_msfget(sk, &gf, p->gf_slist);
1519 release_sock(sk);
1520 if (err)
1521 return err;
1522 if (num > gf.gf_numsrc)
1523 num = gf.gf_numsrc;
1524 ulen = GROUP_FILTER_SIZE(num) - (sizeof(gf)-sizeof(gf32));
1525 if (put_user(ulen, optlen) ||
1526 put_user(gf.gf_fmode, &p->gf_fmode) ||
1527 put_user(gf.gf_numsrc, &p->gf_numsrc))
1528 return -EFAULT;
1529 return 0;
1530 }
Pavel Emelyanov80991792008-04-30 14:49:54 -07001531
Daniel Baluta98e77432011-08-19 03:19:07 -07001532 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1533 MSG_CMSG_COMPAT);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001534#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001535 /* we need to exclude all possible ENOPROTOOPTs except default case */
1536 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001537 int len;
1538
1539 if (get_user(len, optlen))
1540 return -EFAULT;
1541
Paolo Abeni01ea3062018-02-08 12:19:00 +01001542 err = compat_nf_getsockopt(sk, PF_INET6, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001543 if (err >= 0)
1544 err = put_user(len, optlen);
1545 }
1546#endif
1547 return err;
1548}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001549EXPORT_SYMBOL(compat_ipv6_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001550#endif