blob: a733803a710cf1f94ec05466adf388ab0fb346e6 [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
Eric Dumazet790eb672021-10-25 09:48:22 -070058DEFINE_STATIC_KEY_FALSE(ip6_min_hopcount);
59
Denis V. Lunev725a8ff2008-07-19 00:28:58 -070060int ip6_ra_control(struct sock *sk, int sel)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 struct ip6_ra_chain *ra, *new_ra, **rap;
63
64 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
Eric Dumazetc720c7e82009-10-15 06:30:45 +000065 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num != IPPROTO_RAW)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +090066 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Ian Morris67ba4152014-08-24 21:53:10 +010068 new_ra = (sel >= 0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
Gen Zhang95baa602019-05-24 11:19:46 +080069 if (sel >= 0 && !new_ra)
70 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 write_lock_bh(&ip6_ra_lock);
Ian Morris67ba4152014-08-24 21:53:10 +010073 for (rap = &ip6_ra_chain; (ra = *rap) != NULL; rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (ra->sk == sk) {
Ian Morris67ba4152014-08-24 21:53:10 +010075 if (sel >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 write_unlock_bh(&ip6_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -080077 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return -EADDRINUSE;
79 }
80
81 *rap = ra->next;
82 write_unlock_bh(&ip6_ra_lock);
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 sock_put(sk);
85 kfree(ra);
86 return 0;
87 }
88 }
Ian Morris63159f22015-03-29 14:00:04 +010089 if (!new_ra) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 write_unlock_bh(&ip6_ra_lock);
91 return -ENOBUFS;
92 }
93 new_ra->sk = sk;
94 new_ra->sel = sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 new_ra->next = ra;
96 *rap = new_ra;
97 sock_hold(sk);
98 write_unlock_bh(&ip6_ra_lock);
99 return 0;
100}
101
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700102struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
103 struct ipv6_txoptions *opt)
104{
105 if (inet_sk(sk)->is_icsk) {
106 if (opt &&
107 !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000108 inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) {
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700109 struct inet_connection_sock *icsk = inet_csk(sk);
110 icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
111 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
112 }
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700113 }
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800114 opt = xchg((__force struct ipv6_txoptions **)&inet6_sk(sk)->opt,
115 opt);
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700116 sk_dst_reset(sk);
117
118 return opt;
119}
120
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300121static bool setsockopt_needs_rtnl(int optname)
122{
123 switch (optname) {
WANG Cong8651be82016-10-19 23:35:12 -0700124 case IPV6_ADDRFORM:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300125 case IPV6_ADD_MEMBERSHIP:
126 case IPV6_DROP_MEMBERSHIP:
Marcelo Ricardo Leitnerc4a68532015-03-20 11:37:17 -0300127 case IPV6_JOIN_ANYCAST:
128 case IPV6_LEAVE_ANYCAST:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300129 case MCAST_JOIN_GROUP:
130 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300131 case MCAST_JOIN_SOURCE_GROUP:
132 case MCAST_LEAVE_SOURCE_GROUP:
133 case MCAST_BLOCK_SOURCE:
134 case MCAST_UNBLOCK_SOURCE:
135 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300136 return true;
137 }
138 return false;
139}
140
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200141static int copy_group_source_from_sockptr(struct group_source_req *greqs,
142 sockptr_t optval, int optlen)
Al Virofcfa0b02020-04-27 11:00:01 -0400143{
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200144 if (in_compat_syscall()) {
145 struct compat_group_source_req gr32;
Al Virofcfa0b02020-04-27 11:00:01 -0400146
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200147 if (optlen < sizeof(gr32))
148 return -EINVAL;
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200149 if (copy_from_sockptr(&gr32, optval, sizeof(gr32)))
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200150 return -EFAULT;
151 greqs->gsr_interface = gr32.gsr_interface;
152 greqs->gsr_group = gr32.gsr_group;
153 greqs->gsr_source = gr32.gsr_source;
154 } else {
155 if (optlen < sizeof(*greqs))
156 return -EINVAL;
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200157 if (copy_from_sockptr(greqs, optval, sizeof(*greqs)))
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200158 return -EFAULT;
159 }
160
161 return 0;
162}
163
164static int do_ipv6_mcast_group_source(struct sock *sk, int optname,
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200165 sockptr_t optval, int optlen)
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200166{
167 struct group_source_req greqs;
168 int omode, add;
169 int ret;
170
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200171 ret = copy_group_source_from_sockptr(&greqs, optval, optlen);
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200172 if (ret)
173 return ret;
174
175 if (greqs.gsr_group.ss_family != AF_INET6 ||
176 greqs.gsr_source.ss_family != AF_INET6)
Al Virofcfa0b02020-04-27 11:00:01 -0400177 return -EADDRNOTAVAIL;
178
179 if (optname == MCAST_BLOCK_SOURCE) {
180 omode = MCAST_EXCLUDE;
181 add = 1;
182 } else if (optname == MCAST_UNBLOCK_SOURCE) {
183 omode = MCAST_EXCLUDE;
184 add = 0;
185 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
186 struct sockaddr_in6 *psin6;
187 int retv;
188
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200189 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
190 retv = ipv6_sock_mc_join_ssm(sk, greqs.gsr_interface,
Al Virofcfa0b02020-04-27 11:00:01 -0400191 &psin6->sin6_addr,
192 MCAST_INCLUDE);
193 /* prior join w/ different source is ok */
194 if (retv && retv != -EADDRINUSE)
195 return retv;
196 omode = MCAST_INCLUDE;
197 add = 1;
198 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
199 omode = MCAST_INCLUDE;
200 add = 0;
201 }
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200202 return ip6_mc_source(add, omode, sk, &greqs);
Al Virofcfa0b02020-04-27 11:00:01 -0400203}
204
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200205static int ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
Christoph Hellwigca0e65e2020-07-17 08:23:28 +0200206 int optlen)
207{
208 struct group_filter *gsf;
209 int ret;
210
211 if (optlen < GROUP_FILTER_SIZE(0))
212 return -EINVAL;
213 if (optlen > sysctl_optmem_max)
214 return -ENOBUFS;
215
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200216 gsf = memdup_sockptr(optval, optlen);
Christoph Hellwigca0e65e2020-07-17 08:23:28 +0200217 if (IS_ERR(gsf))
218 return PTR_ERR(gsf);
219
220 /* numsrc >= (4G-140)/128 overflow in 32 bits */
221 ret = -ENOBUFS;
222 if (gsf->gf_numsrc >= 0x1ffffffU ||
223 gsf->gf_numsrc > sysctl_mld_max_msf)
224 goto out_free_gsf;
225
226 ret = -EINVAL;
227 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen)
228 goto out_free_gsf;
229
Gustavo A. R. Silvadb243b72021-08-04 15:45:36 -0500230 ret = ip6_mc_msfilter(sk, gsf, gsf->gf_slist_flex);
Christoph Hellwigca0e65e2020-07-17 08:23:28 +0200231out_free_gsf:
232 kfree(gsf);
233 return ret;
234}
235
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200236static int compat_ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
Christoph Hellwigca0e65e2020-07-17 08:23:28 +0200237 int optlen)
238{
Gustavo A. R. Silvadb243b72021-08-04 15:45:36 -0500239 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
Christoph Hellwigca0e65e2020-07-17 08:23:28 +0200240 struct compat_group_filter *gf32;
241 void *p;
242 int ret;
243 int n;
244
245 if (optlen < size0)
246 return -EINVAL;
247 if (optlen > sysctl_optmem_max - 4)
248 return -ENOBUFS;
249
250 p = kmalloc(optlen + 4, GFP_KERNEL);
251 if (!p)
252 return -ENOMEM;
253
Gustavo A. R. Silvadb243b72021-08-04 15:45:36 -0500254 gf32 = p + 4; /* we want ->gf_group and ->gf_slist_flex aligned */
Christoph Hellwigca0e65e2020-07-17 08:23:28 +0200255 ret = -EFAULT;
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200256 if (copy_from_sockptr(gf32, optval, optlen))
Christoph Hellwigca0e65e2020-07-17 08:23:28 +0200257 goto out_free_p;
258
259 /* numsrc >= (4G-140)/128 overflow in 32 bits */
260 ret = -ENOBUFS;
261 n = gf32->gf_numsrc;
262 if (n >= 0x1ffffffU || n > sysctl_mld_max_msf)
263 goto out_free_p;
264
265 ret = -EINVAL;
Gustavo A. R. Silvadb243b72021-08-04 15:45:36 -0500266 if (offsetof(struct compat_group_filter, gf_slist_flex[n]) > optlen)
Christoph Hellwigca0e65e2020-07-17 08:23:28 +0200267 goto out_free_p;
268
Christoph Hellwigca0e65e2020-07-17 08:23:28 +0200269 ret = ip6_mc_msfilter(sk, &(struct group_filter){
270 .gf_interface = gf32->gf_interface,
271 .gf_group = gf32->gf_group,
272 .gf_fmode = gf32->gf_fmode,
Gustavo A. R. Silvadb243b72021-08-04 15:45:36 -0500273 .gf_numsrc = gf32->gf_numsrc}, gf32->gf_slist_flex);
Christoph Hellwigca0e65e2020-07-17 08:23:28 +0200274
275out_free_p:
276 kfree(p);
277 return ret;
278}
Christoph Hellwigca0e65e2020-07-17 08:23:28 +0200279
Christoph Hellwigfdf5bdd2020-07-17 08:23:29 +0200280static int ipv6_mcast_join_leave(struct sock *sk, int optname,
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200281 sockptr_t optval, int optlen)
Christoph Hellwigfdf5bdd2020-07-17 08:23:29 +0200282{
283 struct sockaddr_in6 *psin6;
284 struct group_req greq;
285
286 if (optlen < sizeof(greq))
287 return -EINVAL;
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200288 if (copy_from_sockptr(&greq, optval, sizeof(greq)))
Christoph Hellwigfdf5bdd2020-07-17 08:23:29 +0200289 return -EFAULT;
290
291 if (greq.gr_group.ss_family != AF_INET6)
292 return -EADDRNOTAVAIL;
293 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
294 if (optname == MCAST_JOIN_GROUP)
295 return ipv6_sock_mc_join(sk, greq.gr_interface,
296 &psin6->sin6_addr);
297 return ipv6_sock_mc_drop(sk, greq.gr_interface, &psin6->sin6_addr);
298}
299
Christoph Hellwigfdf5bdd2020-07-17 08:23:29 +0200300static int compat_ipv6_mcast_join_leave(struct sock *sk, int optname,
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200301 sockptr_t optval, int optlen)
Christoph Hellwigfdf5bdd2020-07-17 08:23:29 +0200302{
303 struct compat_group_req gr32;
304 struct sockaddr_in6 *psin6;
Christoph Hellwigfdf5bdd2020-07-17 08:23:29 +0200305
306 if (optlen < sizeof(gr32))
307 return -EINVAL;
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200308 if (copy_from_sockptr(&gr32, optval, sizeof(gr32)))
Christoph Hellwigfdf5bdd2020-07-17 08:23:29 +0200309 return -EFAULT;
310
311 if (gr32.gr_group.ss_family != AF_INET6)
312 return -EADDRNOTAVAIL;
Christoph Hellwigfdf5bdd2020-07-17 08:23:29 +0200313 psin6 = (struct sockaddr_in6 *)&gr32.gr_group;
314 if (optname == MCAST_JOIN_GROUP)
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200315 return ipv6_sock_mc_join(sk, gr32.gr_interface,
Christoph Hellwigfdf5bdd2020-07-17 08:23:29 +0200316 &psin6->sin6_addr);
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200317 return ipv6_sock_mc_drop(sk, gr32.gr_interface, &psin6->sin6_addr);
Christoph Hellwigfdf5bdd2020-07-17 08:23:29 +0200318}
Christoph Hellwigfdf5bdd2020-07-17 08:23:29 +0200319
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200320static int ipv6_set_opt_hdr(struct sock *sk, int optname, sockptr_t optval,
Christoph Hellwigb84d2b72020-07-23 08:09:02 +0200321 int optlen)
322{
323 struct ipv6_pinfo *np = inet6_sk(sk);
324 struct ipv6_opt_hdr *new = NULL;
325 struct net *net = sock_net(sk);
326 struct ipv6_txoptions *opt;
327 int err;
328
329 /* hop-by-hop / destination options are privileged option */
330 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
331 return -EPERM;
332
333 /* remove any sticky options header with a zero option
334 * length, per RFC3542.
335 */
336 if (optlen > 0) {
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200337 if (sockptr_is_null(optval))
Christoph Hellwigb84d2b72020-07-23 08:09:02 +0200338 return -EINVAL;
339 if (optlen < sizeof(struct ipv6_opt_hdr) ||
340 optlen & 0x7 ||
341 optlen > 8 * 255)
342 return -EINVAL;
343
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200344 new = memdup_sockptr(optval, optlen);
Christoph Hellwigb84d2b72020-07-23 08:09:02 +0200345 if (IS_ERR(new))
346 return PTR_ERR(new);
347 if (unlikely(ipv6_optlen(new) > optlen)) {
348 kfree(new);
349 return -EINVAL;
350 }
351 }
352
353 opt = rcu_dereference_protected(np->opt, lockdep_sock_is_held(sk));
354 opt = ipv6_renew_options(sk, opt, optname, new);
355 kfree(new);
356 if (IS_ERR(opt))
357 return PTR_ERR(opt);
358
359 /* routing header option needs extra check */
360 err = -EINVAL;
361 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
362 struct ipv6_rt_hdr *rthdr = opt->srcrt;
363 switch (rthdr->type) {
364#if IS_ENABLED(CONFIG_IPV6_MIP6)
365 case IPV6_SRCRT_TYPE_2:
366 if (rthdr->hdrlen != 2 || rthdr->segments_left != 1)
367 goto sticky_done;
368 break;
369#endif
370 case IPV6_SRCRT_TYPE_4:
371 {
372 struct ipv6_sr_hdr *srh =
373 (struct ipv6_sr_hdr *)opt->srcrt;
374
375 if (!seg6_validate_srh(srh, optlen, false))
376 goto sticky_done;
377 break;
378 }
379 default:
380 goto sticky_done;
381 }
382 }
383
384 err = 0;
385 opt = ipv6_update_options(sk, opt);
386sticky_done:
387 if (opt) {
388 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
389 txopt_put(opt);
390 }
391 return err;
392}
393
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800394static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200395 sockptr_t optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900398 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 int val, valbool;
400 int retv = -ENOPROTOOPT;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300401 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200403 if (sockptr_is_null(optval))
Ian Morris67ba4152014-08-24 21:53:10 +0100404 val = 0;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900405 else {
406 if (optlen >= sizeof(int)) {
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200407 if (copy_from_sockptr(&val, optval, sizeof(val)))
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900408 return -EFAULT;
409 } else
410 val = 0;
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Ian Morris67ba4152014-08-24 21:53:10 +0100413 valbool = (val != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900415 if (ip6_mroute_opt(optname))
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200416 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900417
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300418 if (needs_rtnl)
419 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 lock_sock(sk);
421
422 switch (optname) {
423
424 case IPV6_ADDRFORM:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900425 if (optlen < sizeof(int))
426 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (val == PF_INET) {
428 struct ipv6_txoptions *opt;
429 struct sk_buff *pktopt;
430
Denis V. Lunev49d074f2008-06-04 15:49:06 +0400431 if (sk->sk_type == SOCK_RAW)
432 break;
433
Denis V. Lunev9596cc822008-06-04 15:49:08 +0400434 if (sk->sk_protocol == IPPROTO_UDP ||
435 sk->sk_protocol == IPPROTO_UDPLITE) {
436 struct udp_sock *up = udp_sk(sk);
437 if (up->pending == AF_INET6) {
438 retv = -EBUSY;
439 break;
440 }
Hangbin Liu79a1f0c2020-06-01 11:55:03 +0800441 } else if (sk->sk_protocol == IPPROTO_TCP) {
442 if (sk->sk_prot != &tcpv6_prot) {
443 retv = -EBUSY;
444 break;
445 }
446 } else {
Eric Dumazetb6f61182020-02-25 11:52:29 -0800447 break;
448 }
Hangbin Liu79a1f0c2020-06-01 11:55:03 +0800449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (sk->sk_state != TCP_ESTABLISHED) {
451 retv = -ENOTCONN;
452 break;
453 }
454
455 if (ipv6_only_sock(sk) ||
Eric Dumazetefe42082013-10-03 15:42:29 -0700456 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 retv = -EADDRNOTAVAIL;
458 break;
459 }
460
461 fl6_free_socklist(sk);
WANG Cong8651be82016-10-19 23:35:12 -0700462 __ipv6_sock_mc_close(sk);
Cong Wang8c0de6e2020-07-25 15:40:53 -0700463 __ipv6_sock_ac_close(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700465 /*
466 * Sock is moving from IPv6 to IPv4 (sk_prot), so
467 * remove it from the refcnt debug socks count in the
468 * original family...
469 */
470 sk_refcnt_debug_dec(sk);
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (sk->sk_protocol == IPPROTO_TCP) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800473 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetb3cb7642021-11-15 09:11:50 -0800474
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700475 sock_prot_inuse_add(net, sk->sk_prot, -1);
476 sock_prot_inuse_add(net, &tcp_prot, 1);
Eric Dumazetb3cb7642021-11-15 09:11:50 -0800477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 sk->sk_prot = &tcp_prot;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800479 icsk->icsk_af_ops = &ipv4_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 sk->sk_socket->ops = &inet_stream_ops;
481 sk->sk_family = PF_INET;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800482 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 } else {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800484 struct proto *prot = &udp_prot;
485
David S. Millerdb8dac22008-03-06 16:22:02 -0800486 if (sk->sk_protocol == IPPROTO_UDPLITE)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800487 prot = &udplite_prot;
Eric Dumazetb3cb7642021-11-15 09:11:50 -0800488
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700489 sock_prot_inuse_add(net, sk->sk_prot, -1);
490 sock_prot_inuse_add(net, prot, 1);
Eric Dumazetb3cb7642021-11-15 09:11:50 -0800491
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800492 sk->sk_prot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 sk->sk_socket->ops = &inet_dgram_ops;
494 sk->sk_family = PF_INET;
495 }
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800496 opt = xchg((__force struct ipv6_txoptions **)&np->opt,
497 NULL);
498 if (opt) {
499 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
500 txopt_put(opt);
501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 pktopt = xchg(&np->pktoptions, NULL);
Wei Yongjun800d55f2009-02-23 21:45:33 +0000503 kfree_skb(pktopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700505 /*
506 * ... and add it to the refcnt debug socks count
507 * in the new family. -acme
508 */
509 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 module_put(THIS_MODULE);
511 retv = 0;
512 break;
513 }
514 goto e_inval;
515
516 case IPV6_V6ONLY:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900517 if (optlen < sizeof(int) ||
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000518 inet_sk(sk)->inet_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto e_inval;
Eric Dumazet9fe516b2014-06-27 08:36:16 -0700520 sk->sk_ipv6only = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 retv = 0;
522 break;
523
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900524 case IPV6_RECVPKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900525 if (optlen < sizeof(int))
526 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 np->rxopt.bits.rxinfo = valbool;
528 retv = 0;
529 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900530
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900531 case IPV6_2292PKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900532 if (optlen < sizeof(int))
533 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900534 np->rxopt.bits.rxoinfo = valbool;
535 retv = 0;
536 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900538 case IPV6_RECVHOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900539 if (optlen < sizeof(int))
540 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 np->rxopt.bits.rxhlim = valbool;
542 retv = 0;
543 break;
544
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900545 case IPV6_2292HOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900546 if (optlen < sizeof(int))
547 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900548 np->rxopt.bits.rxohlim = valbool;
549 retv = 0;
550 break;
551
552 case IPV6_RECVRTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900553 if (optlen < sizeof(int))
554 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900555 np->rxopt.bits.srcrt = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 retv = 0;
557 break;
558
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900559 case IPV6_2292RTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900560 if (optlen < sizeof(int))
561 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900562 np->rxopt.bits.osrcrt = valbool;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900563 retv = 0;
564 break;
565
566 case IPV6_RECVHOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900567 if (optlen < sizeof(int))
568 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 np->rxopt.bits.hopopts = valbool;
570 retv = 0;
571 break;
572
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900573 case IPV6_2292HOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900574 if (optlen < sizeof(int))
575 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900576 np->rxopt.bits.ohopopts = valbool;
577 retv = 0;
578 break;
579
580 case IPV6_RECVDSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900581 if (optlen < sizeof(int))
582 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 np->rxopt.bits.dstopts = valbool;
584 retv = 0;
585 break;
586
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900587 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900588 if (optlen < sizeof(int))
589 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900590 np->rxopt.bits.odstopts = valbool;
591 retv = 0;
592 break;
593
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900594 case IPV6_TCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900595 if (optlen < sizeof(int))
596 goto e_inval;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700597 if (val < -1 || val > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900598 goto e_inval;
Gerrit Renker26ced1e2009-08-09 08:12:49 +0000599 /* RFC 3542, 6.5: default traffic class of 0x0 */
600 if (val == -1)
601 val = 0;
Maciej Żenczykowski9f7b3a62021-11-23 14:31:54 -0800602 if (sk->sk_type == SOCK_STREAM) {
603 val &= ~INET_ECN_MASK;
604 val |= np->tclass & INET_ECN_MASK;
605 }
Maciej Żenczykowski305e95b2021-11-23 14:32:08 -0800606 if (np->tclass != val) {
607 np->tclass = val;
608 sk_dst_reset(sk);
609 }
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900610 retv = 0;
611 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900612
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900613 case IPV6_RECVTCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900614 if (optlen < sizeof(int))
615 goto e_inval;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900616 np->rxopt.bits.rxtclass = valbool;
617 retv = 0;
618 break;
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 case IPV6_FLOWINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900621 if (optlen < sizeof(int))
622 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 np->rxopt.bits.rxflow = valbool;
624 retv = 0;
625 break;
626
Brian Haley793b14732010-04-23 11:26:07 +0000627 case IPV6_RECVPATHMTU:
628 if (optlen < sizeof(int))
629 goto e_inval;
630 np->rxopt.bits.rxpmtu = valbool;
631 retv = 0;
632 break;
633
Balazs Scheidler6c468622010-10-21 16:08:28 +0200634 case IPV6_TRANSPARENT:
Maciej Żenczykowski35fc59c2019-11-21 13:19:08 -0800635 if (valbool && !ns_capable(net->user_ns, CAP_NET_RAW) &&
636 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
Balazs Scheidlerb8894162010-10-23 04:48:14 +0000637 retv = -EPERM;
638 break;
639 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200640 if (optlen < sizeof(int))
641 goto e_inval;
642 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
643 inet_sk(sk)->transparent = valbool;
644 retv = 0;
645 break;
646
Maciej Żenczykowski84e14fe2017-09-26 21:32:42 -0700647 case IPV6_FREEBIND:
648 if (optlen < sizeof(int))
649 goto e_inval;
650 /* we also don't have a separate freebind bit for IPV6 */
651 inet_sk(sk)->freebind = valbool;
652 retv = 0;
653 break;
654
Balazs Scheidler6c468622010-10-21 16:08:28 +0200655 case IPV6_RECVORIGDSTADDR:
656 if (optlen < sizeof(int))
657 goto e_inval;
658 np->rxopt.bits.rxorigdstaddr = valbool;
659 retv = 0;
660 break;
661
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900662 case IPV6_HOPOPTS:
663 case IPV6_RTHDRDSTOPTS:
664 case IPV6_RTHDR:
665 case IPV6_DSTOPTS:
Christoph Hellwigb84d2b72020-07-23 08:09:02 +0200666 retv = ipv6_set_opt_hdr(sk, optname, optval, optlen);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900667 break;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900668
Yang Hongyangb24a2512008-12-16 02:06:23 -0800669 case IPV6_PKTINFO:
670 {
671 struct in6_pktinfo pkt;
672
673 if (optlen == 0)
674 goto e_inval;
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200675 else if (optlen < sizeof(struct in6_pktinfo) ||
676 sockptr_is_null(optval))
Yang Hongyangb24a2512008-12-16 02:06:23 -0800677 goto e_inval;
678
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200679 if (copy_from_sockptr(&pkt, optval, sizeof(pkt))) {
680 retv = -EFAULT;
681 break;
Yang Hongyangb24a2512008-12-16 02:06:23 -0800682 }
Mike Manningd839a0e2018-11-07 15:36:08 +0000683 if (!sk_dev_equal_l3scope(sk, pkt.ipi6_ifindex))
Yang Hongyangb24a2512008-12-16 02:06:23 -0800684 goto e_inval;
685
686 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000687 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
Yang Hongyangb24a2512008-12-16 02:06:23 -0800688 retv = 0;
689 break;
690 }
691
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900692 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 {
694 struct ipv6_txoptions *opt = NULL;
695 struct msghdr msg;
David S. Miller4c9483b2011-03-12 16:22:43 -0500696 struct flowi6 fl6;
Wei Wang26879da2016-05-02 21:40:07 -0700697 struct ipcm6_cookie ipc6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
David S. Miller4c9483b2011-03-12 16:22:43 -0500699 memset(&fl6, 0, sizeof(fl6));
700 fl6.flowi6_oif = sk->sk_bound_dev_if;
701 fl6.flowi6_mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 if (optlen == 0)
704 goto update;
705
706 /* 1K is probably excessive
707 * 1K is surely not enough, 2K per standard header is 16K.
708 */
709 retv = -EINVAL;
710 if (optlen > 64*1024)
711 break;
712
713 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
714 retv = -ENOBUFS;
Ian Morris63159f22015-03-29 14:00:04 +0100715 if (!opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 break;
717
718 memset(opt, 0, sizeof(*opt));
Reshetova, Elena0aeea212017-07-04 09:34:54 +0300719 refcount_set(&opt->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 opt->tot_len = sizeof(*opt) + optlen;
721 retv = -EFAULT;
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200722 if (copy_from_sockptr(opt + 1, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 goto done;
724
725 msg.msg_controllen = optlen;
Ian Morris67ba4152014-08-24 21:53:10 +0100726 msg.msg_control = (void *)(opt+1);
Wei Wang26879da2016-05-02 21:40:07 -0700727 ipc6.opt = opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Willem de Bruijn5fdaa882018-07-06 10:12:57 -0400729 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (retv)
731 goto done;
732update:
733 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700734 opt = ipv6_update_options(sk, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735done:
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800736 if (opt) {
737 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
738 txopt_put(opt);
739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 break;
741 }
742 case IPV6_UNICAST_HOPS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900743 if (optlen < sizeof(int))
744 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (val > 255 || val < -1)
746 goto e_inval;
747 np->hop_limit = val;
748 retv = 0;
749 break;
750
751 case IPV6_MULTICAST_HOPS:
752 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900753 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900754 if (optlen < sizeof(int))
755 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (val > 255 || val < -1)
757 goto e_inval;
Li Wei2a38e6d2011-11-27 21:33:34 +0000758 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 retv = 0;
760 break;
761
762 case IPV6_MULTICAST_LOOP:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900763 if (optlen < sizeof(int))
764 goto e_inval;
YOSHIFUJI Hideaki28d44882008-06-12 03:14:51 +0900765 if (val != valbool)
766 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 np->mc_loop = valbool;
768 retv = 0;
769 break;
770
Erich E. Hooverc4062df2012-02-08 09:11:08 +0000771 case IPV6_UNICAST_IF:
772 {
773 struct net_device *dev = NULL;
774 int ifindex;
775
776 if (optlen != sizeof(int))
777 goto e_inval;
778
779 ifindex = (__force int)ntohl((__force __be32)val);
780 if (ifindex == 0) {
781 np->ucast_oif = 0;
782 retv = 0;
783 break;
784 }
785
786 dev = dev_get_by_index(net, ifindex);
787 retv = -EADDRNOTAVAIL;
788 if (!dev)
789 break;
790 dev_put(dev);
791
792 retv = -EINVAL;
793 if (sk->sk_bound_dev_if)
794 break;
795
796 np->ucast_oif = ifindex;
797 retv = 0;
798 break;
799 }
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 case IPV6_MULTICAST_IF:
802 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900803 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900804 if (optlen < sizeof(int))
805 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Brian Haley4953f0f2007-10-11 14:39:29 -0700807 if (val) {
Eric Dumazet55b80502009-10-19 06:41:58 +0000808 struct net_device *dev;
David Ahern7bb387c2016-12-29 15:39:37 -0800809 int midx;
Eric Dumazet55b80502009-10-19 06:41:58 +0000810
David Ahern7bb387c2016-12-29 15:39:37 -0800811 rcu_read_lock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700812
David Ahern7bb387c2016-12-29 15:39:37 -0800813 dev = dev_get_by_index_rcu(net, val);
Eric Dumazet55b80502009-10-19 06:41:58 +0000814 if (!dev) {
David Ahern7bb387c2016-12-29 15:39:37 -0800815 rcu_read_unlock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700816 retv = -ENODEV;
817 break;
818 }
David Ahern7bb387c2016-12-29 15:39:37 -0800819 midx = l3mdev_master_ifindex_rcu(dev);
820
821 rcu_read_unlock();
822
823 if (sk->sk_bound_dev_if &&
824 sk->sk_bound_dev_if != val &&
825 (!midx || midx != sk->sk_bound_dev_if))
826 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828 np->mcast_oif = val;
829 retv = 0;
830 break;
831 case IPV6_ADD_MEMBERSHIP:
832 case IPV6_DROP_MEMBERSHIP:
833 {
834 struct ipv6_mreq mreq;
835
Wang Chena28398b2008-04-07 09:42:07 +0800836 if (optlen < sizeof(struct ipv6_mreq))
837 goto e_inval;
838
Flavio Leitnera96fb492007-08-24 22:16:39 -0700839 retv = -EPROTO;
840 if (inet_sk(sk)->is_icsk)
841 break;
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 retv = -EFAULT;
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200844 if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 break;
846
847 if (optname == IPV6_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300848 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300850 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 break;
852 }
853 case IPV6_JOIN_ANYCAST:
854 case IPV6_LEAVE_ANYCAST:
855 {
856 struct ipv6_mreq mreq;
857
Wang Chena28398b2008-04-07 09:42:07 +0800858 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 goto e_inval;
860
861 retv = -EFAULT;
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200862 if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 break;
864
865 if (optname == IPV6_JOIN_ANYCAST)
866 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
867 else
868 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
869 break;
870 }
Andre Naujoks15033f02018-09-10 10:27:15 +0200871 case IPV6_MULTICAST_ALL:
872 if (optlen < sizeof(int))
873 goto e_inval;
874 np->mc_all = valbool;
875 retv = 0;
876 break;
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 case MCAST_JOIN_GROUP:
879 case MCAST_LEAVE_GROUP:
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200880 if (in_compat_syscall())
881 retv = compat_ipv6_mcast_join_leave(sk, optname, optval,
882 optlen);
883 else
884 retv = ipv6_mcast_join_leave(sk, optname, optval,
885 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 case MCAST_JOIN_SOURCE_GROUP:
888 case MCAST_LEAVE_SOURCE_GROUP:
889 case MCAST_BLOCK_SOURCE:
890 case MCAST_UNBLOCK_SOURCE:
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200891 retv = do_ipv6_mcast_group_source(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 case MCAST_MSFILTER:
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200894 if (in_compat_syscall())
895 retv = compat_ipv6_set_mcast_msfilter(sk, optval,
896 optlen);
897 else
898 retv = ipv6_set_mcast_msfilter(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 case IPV6_ROUTER_ALERT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900901 if (optlen < sizeof(int))
902 goto e_inval;
Denis V. Lunev725a8ff2008-07-19 00:28:58 -0700903 retv = ip6_ra_control(sk, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 break;
Francesco Ruggeri9036b2f2019-03-01 15:31:03 -0800905 case IPV6_ROUTER_ALERT_ISOLATE:
906 if (optlen < sizeof(int))
907 goto e_inval;
908 np->rtalert_isolate = valbool;
909 retv = 0;
910 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 case IPV6_MTU_DISCOVER:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900912 if (optlen < sizeof(int))
913 goto e_inval;
Hannes Frederic Sowa0b952272014-02-26 01:20:43 +0100914 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 goto e_inval;
916 np->pmtudisc = val;
917 retv = 0;
918 break;
919 case IPV6_MTU:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900920 if (optlen < sizeof(int))
921 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (val && val < IPV6_MIN_MTU)
923 goto e_inval;
924 np->frag_size = val;
925 retv = 0;
926 break;
927 case IPV6_RECVERR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900928 if (optlen < sizeof(int))
929 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 np->recverr = valbool;
931 if (!val)
932 skb_queue_purge(&sk->sk_error_queue);
933 retv = 0;
934 break;
935 case IPV6_FLOWINFO_SEND:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900936 if (optlen < sizeof(int))
937 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 np->sndflow = valbool;
939 retv = 0;
940 break;
941 case IPV6_FLOWLABEL_MGR:
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200942 retv = ipv6_flowlabel_opt(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 break;
944 case IPV6_IPSEC_POLICY:
945 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700946 retv = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000947 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700948 break;
Christoph Hellwig894cfbc2020-07-23 08:09:03 +0200949 retv = xfrm_user_policy(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 break;
951
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900952 case IPV6_ADDR_PREFERENCES:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900953 if (optlen < sizeof(int))
954 goto e_inval;
Christoph Hellwig18d5ad62020-05-28 07:12:33 +0200955 retv = __ip6_sock_set_addr_preferences(sk, val);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900956 break;
Stephen Hemmingere802af92010-04-22 15:24:53 -0700957 case IPV6_MINHOPCOUNT:
958 if (optlen < sizeof(int))
959 goto e_inval;
960 if (val < 0 || val > 255)
961 goto e_inval;
Eric Dumazet790eb672021-10-25 09:48:22 -0700962
963 if (val)
964 static_branch_enable(&ip6_min_hopcount);
965
Eric Dumazetcc17c3c2021-10-25 09:48:21 -0700966 /* tcp_v6_err() and tcp_v6_rcv() might read min_hopcount
967 * while we are changing it.
968 */
969 WRITE_ONCE(np->min_hopcount, val);
Hannes Frederic Sowad4596ba2012-11-10 19:52:34 +0000970 retv = 0;
Brian Haley793b14732010-04-23 11:26:07 +0000971 break;
972 case IPV6_DONTFRAG:
973 np->dontfrag = valbool;
Stephen Hemmingere802af92010-04-22 15:24:53 -0700974 retv = 0;
975 break;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700976 case IPV6_AUTOFLOWLABEL:
977 np->autoflowlabel = valbool;
Shaohua Li513674b2017-12-20 12:10:21 -0800978 np->autoflowlabel_set = 1;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700979 retv = 0;
980 break;
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -0400981 case IPV6_RECVFRAGSIZE:
982 np->rxopt.bits.recvfragsize = valbool;
983 retv = 0;
984 break;
Willem de Bruijn01370432020-07-24 09:03:10 -0400985 case IPV6_RECVERR_RFC4884:
986 if (optlen < sizeof(int))
987 goto e_inval;
988 if (val < 0 || val > 1)
989 goto e_inval;
990 np->recverr_rfc4884 = valbool;
991 retv = 0;
992 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300996 if (needs_rtnl)
997 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return retv;
1000
1001e_inval:
1002 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -03001003 if (needs_rtnl)
1004 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return -EINVAL;
1006}
1007
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001008int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1009 unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001010{
1011 int err;
1012
1013 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1014 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
1015
1016 if (level != SOL_IPV6)
1017 return -ENOPROTOOPT;
1018
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001019 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001020#ifdef CONFIG_NETFILTER
1021 /* we need to exclude all possible ENOPROTOOPTs except default case */
1022 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +01001023 optname != IPV6_XFRM_POLICY)
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001024 err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001025#endif
1026 return err;
1027}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001028EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001029
David S. Miller286930792007-03-07 12:50:46 -08001030static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001031 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001032{
David S. Miller286930792007-03-07 12:50:46 -08001033 struct ipv6_opt_hdr *hdr;
1034
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001035 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001036 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001037
Ian Morris67ba4152014-08-24 21:53:10 +01001038 switch (optname) {
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001039 case IPV6_HOPOPTS:
1040 hdr = opt->hopopt;
1041 break;
1042 case IPV6_RTHDRDSTOPTS:
1043 hdr = opt->dst0opt;
1044 break;
1045 case IPV6_RTHDR:
1046 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
1047 break;
1048 case IPV6_DSTOPTS:
1049 hdr = opt->dst1opt;
1050 break;
1051 default:
1052 return -EINVAL; /* should not happen */
1053 }
1054
1055 if (!hdr)
1056 return 0;
David S. Miller286930792007-03-07 12:50:46 -08001057
Chris Wrightd2b02ed2007-03-09 16:19:17 -08001058 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -07001059 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001060 return -EFAULT;
Yang Hongyang95b496b2008-05-28 16:27:28 +08001061 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001062}
1063
Christoph Hellwigd5541e82020-07-17 08:23:27 +02001064static int ipv6_get_msfilter(struct sock *sk, void __user *optval,
1065 int __user *optlen, int len)
1066{
Gustavo A. R. Silvadb243b72021-08-04 15:45:36 -05001067 const int size0 = offsetof(struct group_filter, gf_slist_flex);
Christoph Hellwigd5541e82020-07-17 08:23:27 +02001068 struct group_filter __user *p = optval;
1069 struct group_filter gsf;
1070 int num;
1071 int err;
1072
1073 if (len < size0)
1074 return -EINVAL;
1075 if (copy_from_user(&gsf, p, size0))
1076 return -EFAULT;
1077 if (gsf.gf_group.ss_family != AF_INET6)
1078 return -EADDRNOTAVAIL;
1079 num = gsf.gf_numsrc;
1080 lock_sock(sk);
Gustavo A. R. Silvadb243b72021-08-04 15:45:36 -05001081 err = ip6_mc_msfget(sk, &gsf, p->gf_slist_flex);
Christoph Hellwigd5541e82020-07-17 08:23:27 +02001082 if (!err) {
1083 if (num > gsf.gf_numsrc)
1084 num = gsf.gf_numsrc;
1085 if (put_user(GROUP_FILTER_SIZE(num), optlen) ||
1086 copy_to_user(p, &gsf, size0))
1087 err = -EFAULT;
1088 }
1089 release_sock(sk);
1090 return err;
1091}
1092
Christoph Hellwigd5541e82020-07-17 08:23:27 +02001093static int compat_ipv6_get_msfilter(struct sock *sk, void __user *optval,
1094 int __user *optlen)
1095{
Gustavo A. R. Silvadb243b72021-08-04 15:45:36 -05001096 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
Christoph Hellwigd5541e82020-07-17 08:23:27 +02001097 struct compat_group_filter __user *p = optval;
1098 struct compat_group_filter gf32;
1099 struct group_filter gf;
1100 int len, err;
1101 int num;
1102
1103 if (get_user(len, optlen))
1104 return -EFAULT;
1105 if (len < size0)
1106 return -EINVAL;
1107
1108 if (copy_from_user(&gf32, p, size0))
1109 return -EFAULT;
1110 gf.gf_interface = gf32.gf_interface;
1111 gf.gf_fmode = gf32.gf_fmode;
1112 num = gf.gf_numsrc = gf32.gf_numsrc;
1113 gf.gf_group = gf32.gf_group;
1114
1115 if (gf.gf_group.ss_family != AF_INET6)
1116 return -EADDRNOTAVAIL;
1117
1118 lock_sock(sk);
Gustavo A. R. Silvadb243b72021-08-04 15:45:36 -05001119 err = ip6_mc_msfget(sk, &gf, p->gf_slist_flex);
Christoph Hellwigd5541e82020-07-17 08:23:27 +02001120 release_sock(sk);
1121 if (err)
1122 return err;
1123 if (num > gf.gf_numsrc)
1124 num = gf.gf_numsrc;
1125 len = GROUP_FILTER_SIZE(num) - (sizeof(gf)-sizeof(gf32));
1126 if (put_user(len, optlen) ||
1127 put_user(gf.gf_fmode, &p->gf_fmode) ||
1128 put_user(gf.gf_numsrc, &p->gf_numsrc))
1129 return -EFAULT;
1130 return 0;
1131}
Christoph Hellwigd5541e82020-07-17 08:23:27 +02001132
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001133static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001134 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 struct ipv6_pinfo *np = inet6_sk(sk);
1137 int len;
1138 int val;
1139
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001140 if (ip6_mroute_opt(optname))
1141 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (get_user(len, optlen))
1144 return -EFAULT;
1145 switch (optname) {
1146 case IPV6_ADDRFORM:
1147 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001148 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 sk->sk_protocol != IPPROTO_TCP)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +09001150 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (sk->sk_state != TCP_ESTABLISHED)
1152 return -ENOTCONN;
1153 val = sk->sk_family;
1154 break;
1155 case MCAST_MSFILTER:
Christoph Hellwig3021ad52020-07-17 08:23:30 +02001156 if (in_compat_syscall())
1157 return compat_ipv6_get_msfilter(sk, optval, optlen);
Christoph Hellwigd5541e82020-07-17 08:23:27 +02001158 return ipv6_get_msfilter(sk, optval, optlen, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001159 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 {
1161 struct msghdr msg;
1162 struct sk_buff *skb;
1163
1164 if (sk->sk_type != SOCK_STREAM)
1165 return -ENOPROTOOPT;
1166
Lukas Bulwahn9e393942020-11-27 10:34:21 +01001167 msg.msg_control_user = optval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 msg.msg_controllen = len;
Daniel Baluta98e77432011-08-19 03:19:07 -07001169 msg.msg_flags = flags;
Christoph Hellwig1b2f08d2020-05-13 21:36:41 +02001170 msg.msg_control_is_user = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 lock_sock(sk);
1173 skb = np->pktoptions;
1174 if (skb)
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001175 ip6_datagram_recv_ctl(sk, &msg, skb);
Eric Dumazet1dc7b902015-01-21 03:45:42 -08001176 release_sock(sk);
1177 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (np->rxopt.bits.rxinfo) {
1179 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001180 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1181 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001182 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1184 }
1185 if (np->rxopt.bits.rxhlim) {
1186 int hlim = np->mcast_hops;
1187 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1188 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001189 if (np->rxopt.bits.rxtclass) {
Li RongQingd76ed222014-01-15 17:03:30 +08001190 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1191
Jiri Benc4c507d22012-02-09 09:35:49 +00001192 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1193 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001194 if (np->rxopt.bits.rxoinfo) {
1195 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001196 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1197 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001198 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1199 np->sticky_pktinfo.ipi6_addr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001200 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1201 }
1202 if (np->rxopt.bits.rxohlim) {
1203 int hlim = np->mcast_hops;
1204 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1205 }
Florent Fourcot1397ed32013-12-08 15:46:57 +01001206 if (np->rxopt.bits.rxflow) {
Florent Fourcot68536052013-12-12 17:07:58 +01001207 __be32 flowinfo = np->rcv_flowinfo;
1208
Florent Fourcot1397ed32013-12-08 15:46:57 +01001209 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212 len -= msg.msg_controllen;
1213 return put_user(len, optlen);
1214 }
1215 case IPV6_MTU:
1216 {
1217 struct dst_entry *dst;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001218
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001219 val = 0;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001220 rcu_read_lock();
1221 dst = __sk_dst_get(sk);
1222 if (dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 val = dst_mtu(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001224 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (!val)
1226 return -ENOTCONN;
1227 break;
1228 }
1229
1230 case IPV6_V6ONLY:
Eric Dumazet9fe516b2014-06-27 08:36:16 -07001231 val = sk->sk_ipv6only;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 break;
1233
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001234 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 val = np->rxopt.bits.rxinfo;
1236 break;
1237
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001238 case IPV6_2292PKTINFO:
1239 val = np->rxopt.bits.rxoinfo;
1240 break;
1241
1242 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 val = np->rxopt.bits.rxhlim;
1244 break;
1245
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001246 case IPV6_2292HOPLIMIT:
1247 val = np->rxopt.bits.rxohlim;
1248 break;
1249
1250 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 val = np->rxopt.bits.srcrt;
1252 break;
1253
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001254 case IPV6_2292RTHDR:
1255 val = np->rxopt.bits.osrcrt;
1256 break;
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001259 case IPV6_RTHDRDSTOPTS:
1260 case IPV6_RTHDR:
1261 case IPV6_DSTOPTS:
1262 {
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001263 struct ipv6_txoptions *opt;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001264
1265 lock_sock(sk);
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001266 opt = rcu_dereference_protected(np->opt,
1267 lockdep_sock_is_held(sk));
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001268 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001269 release_sock(sk);
Yang Hongyang05335c22008-05-28 16:23:47 +08001270 /* check if ipv6_getsockopt_sticky() returns err code */
1271 if (len < 0)
1272 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001273 return put_user(len, optlen);
1274 }
1275
1276 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 val = np->rxopt.bits.hopopts;
1278 break;
1279
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001280 case IPV6_2292HOPOPTS:
1281 val = np->rxopt.bits.ohopopts;
1282 break;
1283
1284 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 val = np->rxopt.bits.dstopts;
1286 break;
1287
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001288 case IPV6_2292DSTOPTS:
1289 val = np->rxopt.bits.odstopts;
1290 break;
1291
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001292 case IPV6_TCLASS:
1293 val = np->tclass;
1294 break;
1295
1296 case IPV6_RECVTCLASS:
1297 val = np->rxopt.bits.rxtclass;
1298 break;
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 case IPV6_FLOWINFO:
1301 val = np->rxopt.bits.rxflow;
1302 break;
1303
Brian Haley793b14732010-04-23 11:26:07 +00001304 case IPV6_RECVPATHMTU:
1305 val = np->rxopt.bits.rxpmtu;
1306 break;
1307
1308 case IPV6_PATHMTU:
1309 {
1310 struct dst_entry *dst;
1311 struct ip6_mtuinfo mtuinfo;
1312
1313 if (len < sizeof(mtuinfo))
1314 return -EINVAL;
1315
1316 len = sizeof(mtuinfo);
1317 memset(&mtuinfo, 0, sizeof(mtuinfo));
1318
1319 rcu_read_lock();
1320 dst = __sk_dst_get(sk);
1321 if (dst)
1322 mtuinfo.ip6m_mtu = dst_mtu(dst);
1323 rcu_read_unlock();
1324 if (!mtuinfo.ip6m_mtu)
1325 return -ENOTCONN;
1326
1327 if (put_user(len, optlen))
1328 return -EFAULT;
1329 if (copy_to_user(optval, &mtuinfo, len))
1330 return -EFAULT;
1331
1332 return 0;
Brian Haley793b14732010-04-23 11:26:07 +00001333 }
1334
Balazs Scheidler6c468622010-10-21 16:08:28 +02001335 case IPV6_TRANSPARENT:
1336 val = inet_sk(sk)->transparent;
1337 break;
1338
Maciej Żenczykowski84e14fe2017-09-26 21:32:42 -07001339 case IPV6_FREEBIND:
1340 val = inet_sk(sk)->freebind;
1341 break;
1342
Balazs Scheidler6c468622010-10-21 16:08:28 +02001343 case IPV6_RECVORIGDSTADDR:
1344 val = np->rxopt.bits.rxorigdstaddr;
1345 break;
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -08001349 {
1350 struct dst_entry *dst;
1351
1352 if (optname == IPV6_UNICAST_HOPS)
1353 val = np->hop_limit;
1354 else
1355 val = np->mcast_hops;
1356
Eric Dumazetb6c67122010-04-08 23:03:29 +00001357 if (val < 0) {
1358 rcu_read_lock();
1359 dst = __sk_dst_get(sk);
1360 if (dst)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001361 val = ip6_dst_hoplimit(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001362 rcu_read_unlock();
Brian Haleybefffe92006-12-12 17:09:49 -08001363 }
Eric Dumazetb6c67122010-04-08 23:03:29 +00001364
Brian Haleybefffe92006-12-12 17:09:49 -08001365 if (val < 0)
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001366 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 break;
Brian Haleybefffe92006-12-12 17:09:49 -08001368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 case IPV6_MULTICAST_LOOP:
1371 val = np->mc_loop;
1372 break;
1373
1374 case IPV6_MULTICAST_IF:
1375 val = np->mcast_oif;
1376 break;
1377
Andre Naujoks15033f02018-09-10 10:27:15 +02001378 case IPV6_MULTICAST_ALL:
1379 val = np->mc_all;
1380 break;
1381
Erich E. Hooverc4062df2012-02-08 09:11:08 +00001382 case IPV6_UNICAST_IF:
1383 val = (__force int)htonl((__u32) np->ucast_oif);
1384 break;
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 case IPV6_MTU_DISCOVER:
1387 val = np->pmtudisc;
1388 break;
1389
1390 case IPV6_RECVERR:
1391 val = np->recverr;
1392 break;
1393
1394 case IPV6_FLOWINFO_SEND:
1395 val = np->sndflow;
1396 break;
1397
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001398 case IPV6_FLOWLABEL_MGR:
1399 {
1400 struct in6_flowlabel_req freq;
Florent Fourcot46e5f402014-01-17 17:15:04 +01001401 int flags;
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001402
1403 if (len < sizeof(freq))
1404 return -EINVAL;
1405
1406 if (copy_from_user(&freq, optval, sizeof(freq)))
1407 return -EFAULT;
1408
1409 if (freq.flr_action != IPV6_FL_A_GET)
1410 return -EINVAL;
1411
1412 len = sizeof(freq);
Florent Fourcot46e5f402014-01-17 17:15:04 +01001413 flags = freq.flr_flags;
1414
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001415 memset(&freq, 0, sizeof(freq));
1416
Florent Fourcot46e5f402014-01-17 17:15:04 +01001417 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001418 if (val < 0)
1419 return val;
1420
1421 if (put_user(len, optlen))
1422 return -EFAULT;
1423 if (copy_to_user(optval, &freq, len))
1424 return -EFAULT;
1425
1426 return 0;
1427 }
1428
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001429 case IPV6_ADDR_PREFERENCES:
1430 val = 0;
1431
1432 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1433 val |= IPV6_PREFER_SRC_TMP;
1434 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1435 val |= IPV6_PREFER_SRC_PUBLIC;
1436 else {
1437 /* XXX: should we return system default? */
1438 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1439 }
1440
1441 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1442 val |= IPV6_PREFER_SRC_COA;
1443 else
1444 val |= IPV6_PREFER_SRC_HOME;
1445 break;
1446
Stephen Hemmingere802af92010-04-22 15:24:53 -07001447 case IPV6_MINHOPCOUNT:
1448 val = np->min_hopcount;
1449 break;
1450
Brian Haley793b14732010-04-23 11:26:07 +00001451 case IPV6_DONTFRAG:
1452 val = np->dontfrag;
1453 break;
1454
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001455 case IPV6_AUTOFLOWLABEL:
Ben Hutchingse9191ff2018-01-22 20:06:42 +00001456 val = ip6_autoflowlabel(sock_net(sk), np);
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001457 break;
1458
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -04001459 case IPV6_RECVFRAGSIZE:
1460 val = np->rxopt.bits.recvfragsize;
1461 break;
1462
Francesco Ruggeri9036b2f2019-03-01 15:31:03 -08001463 case IPV6_ROUTER_ALERT_ISOLATE:
1464 val = np->rtalert_isolate;
1465 break;
1466
Willem de Bruijn01370432020-07-24 09:03:10 -04001467 case IPV6_RECVERR_RFC4884:
1468 val = np->recverr_rfc4884;
1469 break;
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001472 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
1474 len = min_t(unsigned int, sizeof(int), len);
Ian Morris67ba4152014-08-24 21:53:10 +01001475 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001477 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return -EFAULT;
1479 return 0;
1480}
1481
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001482int ipv6_getsockopt(struct sock *sk, int level, int optname,
1483 char __user *optval, int __user *optlen)
1484{
1485 int err;
1486
1487 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1488 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1489
Ian Morris67ba4152014-08-24 21:53:10 +01001490 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001491 return -ENOPROTOOPT;
1492
Daniel Baluta98e77432011-08-19 03:19:07 -07001493 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001494#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001495 /* we need to exclude all possible ENOPROTOOPTs except default case */
1496 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001497 int len;
1498
1499 if (get_user(len, optlen))
1500 return -EFAULT;
1501
Paolo Abeni01ea3062018-02-08 12:19:00 +01001502 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001503 if (err >= 0)
1504 err = put_user(len, optlen);
1505 }
1506#endif
1507 return err;
1508}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001509EXPORT_SYMBOL(ipv6_getsockopt);