blob: 90442c8366dff2cc2c51893cde795dc765d25afd [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
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200139static int copy_group_source_from_user(struct group_source_req *greqs,
140 void __user *optval, int optlen)
Al Virofcfa0b02020-04-27 11:00:01 -0400141{
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200142 if (in_compat_syscall()) {
143 struct compat_group_source_req gr32;
Al Virofcfa0b02020-04-27 11:00:01 -0400144
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200145 if (optlen < sizeof(gr32))
146 return -EINVAL;
147 if (copy_from_user(&gr32, optval, sizeof(gr32)))
148 return -EFAULT;
149 greqs->gsr_interface = gr32.gsr_interface;
150 greqs->gsr_group = gr32.gsr_group;
151 greqs->gsr_source = gr32.gsr_source;
152 } else {
153 if (optlen < sizeof(*greqs))
154 return -EINVAL;
155 if (copy_from_user(greqs, optval, sizeof(*greqs)))
156 return -EFAULT;
157 }
158
159 return 0;
160}
161
162static int do_ipv6_mcast_group_source(struct sock *sk, int optname,
163 void __user *optval, int optlen)
164{
165 struct group_source_req greqs;
166 int omode, add;
167 int ret;
168
169 ret = copy_group_source_from_user(&greqs, optval, optlen);
170 if (ret)
171 return ret;
172
173 if (greqs.gsr_group.ss_family != AF_INET6 ||
174 greqs.gsr_source.ss_family != AF_INET6)
Al Virofcfa0b02020-04-27 11:00:01 -0400175 return -EADDRNOTAVAIL;
176
177 if (optname == MCAST_BLOCK_SOURCE) {
178 omode = MCAST_EXCLUDE;
179 add = 1;
180 } else if (optname == MCAST_UNBLOCK_SOURCE) {
181 omode = MCAST_EXCLUDE;
182 add = 0;
183 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
184 struct sockaddr_in6 *psin6;
185 int retv;
186
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200187 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
188 retv = ipv6_sock_mc_join_ssm(sk, greqs.gsr_interface,
Al Virofcfa0b02020-04-27 11:00:01 -0400189 &psin6->sin6_addr,
190 MCAST_INCLUDE);
191 /* prior join w/ different source is ok */
192 if (retv && retv != -EADDRINUSE)
193 return retv;
194 omode = MCAST_INCLUDE;
195 add = 1;
196 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
197 omode = MCAST_INCLUDE;
198 add = 0;
199 }
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200200 return ip6_mc_source(add, omode, sk, &greqs);
Al Virofcfa0b02020-04-27 11:00:01 -0400201}
202
Christoph Hellwigca0e65e2020-07-17 08:23:28 +0200203static int ipv6_set_mcast_msfilter(struct sock *sk, void __user *optval,
204 int optlen)
205{
206 struct group_filter *gsf;
207 int ret;
208
209 if (optlen < GROUP_FILTER_SIZE(0))
210 return -EINVAL;
211 if (optlen > sysctl_optmem_max)
212 return -ENOBUFS;
213
214 gsf = memdup_user(optval, optlen);
215 if (IS_ERR(gsf))
216 return PTR_ERR(gsf);
217
218 /* numsrc >= (4G-140)/128 overflow in 32 bits */
219 ret = -ENOBUFS;
220 if (gsf->gf_numsrc >= 0x1ffffffU ||
221 gsf->gf_numsrc > sysctl_mld_max_msf)
222 goto out_free_gsf;
223
224 ret = -EINVAL;
225 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen)
226 goto out_free_gsf;
227
228 ret = ip6_mc_msfilter(sk, gsf, gsf->gf_slist);
229out_free_gsf:
230 kfree(gsf);
231 return ret;
232}
233
Christoph Hellwigca0e65e2020-07-17 08:23:28 +0200234static int compat_ipv6_set_mcast_msfilter(struct sock *sk, void __user *optval,
235 int optlen)
236{
237 const int size0 = offsetof(struct compat_group_filter, gf_slist);
238 struct compat_group_filter *gf32;
239 void *p;
240 int ret;
241 int n;
242
243 if (optlen < size0)
244 return -EINVAL;
245 if (optlen > sysctl_optmem_max - 4)
246 return -ENOBUFS;
247
248 p = kmalloc(optlen + 4, GFP_KERNEL);
249 if (!p)
250 return -ENOMEM;
251
252 gf32 = p + 4; /* we want ->gf_group and ->gf_slist aligned */
253 ret = -EFAULT;
254 if (copy_from_user(gf32, optval, optlen))
255 goto out_free_p;
256
257 /* numsrc >= (4G-140)/128 overflow in 32 bits */
258 ret = -ENOBUFS;
259 n = gf32->gf_numsrc;
260 if (n >= 0x1ffffffU || n > sysctl_mld_max_msf)
261 goto out_free_p;
262
263 ret = -EINVAL;
264 if (offsetof(struct compat_group_filter, gf_slist[n]) > optlen)
265 goto out_free_p;
266
Christoph Hellwigca0e65e2020-07-17 08:23:28 +0200267 ret = ip6_mc_msfilter(sk, &(struct group_filter){
268 .gf_interface = gf32->gf_interface,
269 .gf_group = gf32->gf_group,
270 .gf_fmode = gf32->gf_fmode,
271 .gf_numsrc = gf32->gf_numsrc}, gf32->gf_slist);
Christoph Hellwigca0e65e2020-07-17 08:23:28 +0200272
273out_free_p:
274 kfree(p);
275 return ret;
276}
Christoph Hellwigca0e65e2020-07-17 08:23:28 +0200277
Christoph Hellwigfdf5bdd2020-07-17 08:23:29 +0200278static int ipv6_mcast_join_leave(struct sock *sk, int optname,
279 void __user *optval, int optlen)
280{
281 struct sockaddr_in6 *psin6;
282 struct group_req greq;
283
284 if (optlen < sizeof(greq))
285 return -EINVAL;
286 if (copy_from_user(&greq, optval, sizeof(greq)))
287 return -EFAULT;
288
289 if (greq.gr_group.ss_family != AF_INET6)
290 return -EADDRNOTAVAIL;
291 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
292 if (optname == MCAST_JOIN_GROUP)
293 return ipv6_sock_mc_join(sk, greq.gr_interface,
294 &psin6->sin6_addr);
295 return ipv6_sock_mc_drop(sk, greq.gr_interface, &psin6->sin6_addr);
296}
297
Christoph Hellwigfdf5bdd2020-07-17 08:23:29 +0200298static int compat_ipv6_mcast_join_leave(struct sock *sk, int optname,
299 void __user *optval, int optlen)
300{
301 struct compat_group_req gr32;
302 struct sockaddr_in6 *psin6;
Christoph Hellwigfdf5bdd2020-07-17 08:23:29 +0200303
304 if (optlen < sizeof(gr32))
305 return -EINVAL;
306 if (copy_from_user(&gr32, optval, sizeof(gr32)))
307 return -EFAULT;
308
309 if (gr32.gr_group.ss_family != AF_INET6)
310 return -EADDRNOTAVAIL;
Christoph Hellwigfdf5bdd2020-07-17 08:23:29 +0200311 psin6 = (struct sockaddr_in6 *)&gr32.gr_group;
312 if (optname == MCAST_JOIN_GROUP)
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200313 return ipv6_sock_mc_join(sk, gr32.gr_interface,
Christoph Hellwigfdf5bdd2020-07-17 08:23:29 +0200314 &psin6->sin6_addr);
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200315 return ipv6_sock_mc_drop(sk, gr32.gr_interface, &psin6->sin6_addr);
Christoph Hellwigfdf5bdd2020-07-17 08:23:29 +0200316}
Christoph Hellwigfdf5bdd2020-07-17 08:23:29 +0200317
Christoph Hellwigb84d2b72020-07-23 08:09:02 +0200318static int ipv6_set_opt_hdr(struct sock *sk, int optname, void __user *optval,
319 int optlen)
320{
321 struct ipv6_pinfo *np = inet6_sk(sk);
322 struct ipv6_opt_hdr *new = NULL;
323 struct net *net = sock_net(sk);
324 struct ipv6_txoptions *opt;
325 int err;
326
327 /* hop-by-hop / destination options are privileged option */
328 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
329 return -EPERM;
330
331 /* remove any sticky options header with a zero option
332 * length, per RFC3542.
333 */
334 if (optlen > 0) {
335 if (!optval)
336 return -EINVAL;
337 if (optlen < sizeof(struct ipv6_opt_hdr) ||
338 optlen & 0x7 ||
339 optlen > 8 * 255)
340 return -EINVAL;
341
342 new = memdup_user(optval, optlen);
343 if (IS_ERR(new))
344 return PTR_ERR(new);
345 if (unlikely(ipv6_optlen(new) > optlen)) {
346 kfree(new);
347 return -EINVAL;
348 }
349 }
350
351 opt = rcu_dereference_protected(np->opt, lockdep_sock_is_held(sk));
352 opt = ipv6_renew_options(sk, opt, optname, new);
353 kfree(new);
354 if (IS_ERR(opt))
355 return PTR_ERR(opt);
356
357 /* routing header option needs extra check */
358 err = -EINVAL;
359 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
360 struct ipv6_rt_hdr *rthdr = opt->srcrt;
361 switch (rthdr->type) {
362#if IS_ENABLED(CONFIG_IPV6_MIP6)
363 case IPV6_SRCRT_TYPE_2:
364 if (rthdr->hdrlen != 2 || rthdr->segments_left != 1)
365 goto sticky_done;
366 break;
367#endif
368 case IPV6_SRCRT_TYPE_4:
369 {
370 struct ipv6_sr_hdr *srh =
371 (struct ipv6_sr_hdr *)opt->srcrt;
372
373 if (!seg6_validate_srh(srh, optlen, false))
374 goto sticky_done;
375 break;
376 }
377 default:
378 goto sticky_done;
379 }
380 }
381
382 err = 0;
383 opt = ipv6_update_options(sk, opt);
384sticky_done:
385 if (opt) {
386 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
387 txopt_put(opt);
388 }
389 return err;
390}
391
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800392static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700393 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900396 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 int val, valbool;
398 int retv = -ENOPROTOOPT;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300399 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Ian Morris63159f22015-03-29 14:00:04 +0100401 if (!optval)
Ian Morris67ba4152014-08-24 21:53:10 +0100402 val = 0;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900403 else {
404 if (optlen >= sizeof(int)) {
405 if (get_user(val, (int __user *) optval))
406 return -EFAULT;
407 } else
408 val = 0;
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Ian Morris67ba4152014-08-24 21:53:10 +0100411 valbool = (val != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900413 if (ip6_mroute_opt(optname))
Christoph Hellwigb43c6152020-07-23 08:08:59 +0200414 return ip6_mroute_setsockopt(sk, optname, USER_SOCKPTR(optval),
415 optlen);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900416
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300417 if (needs_rtnl)
418 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 lock_sock(sk);
420
421 switch (optname) {
422
423 case IPV6_ADDRFORM:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900424 if (optlen < sizeof(int))
425 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (val == PF_INET) {
427 struct ipv6_txoptions *opt;
428 struct sk_buff *pktopt;
429
Denis V. Lunev49d074f2008-06-04 15:49:06 +0400430 if (sk->sk_type == SOCK_RAW)
431 break;
432
Denis V. Lunev9596cc822008-06-04 15:49:08 +0400433 if (sk->sk_protocol == IPPROTO_UDP ||
434 sk->sk_protocol == IPPROTO_UDPLITE) {
435 struct udp_sock *up = udp_sk(sk);
436 if (up->pending == AF_INET6) {
437 retv = -EBUSY;
438 break;
439 }
Hangbin Liu79a1f0c2020-06-01 11:55:03 +0800440 } else if (sk->sk_protocol == IPPROTO_TCP) {
441 if (sk->sk_prot != &tcpv6_prot) {
442 retv = -EBUSY;
443 break;
444 }
445 } else {
Eric Dumazetb6f61182020-02-25 11:52:29 -0800446 break;
447 }
Hangbin Liu79a1f0c2020-06-01 11:55:03 +0800448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (sk->sk_state != TCP_ESTABLISHED) {
450 retv = -ENOTCONN;
451 break;
452 }
453
454 if (ipv6_only_sock(sk) ||
Eric Dumazetefe42082013-10-03 15:42:29 -0700455 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 retv = -EADDRNOTAVAIL;
457 break;
458 }
459
460 fl6_free_socklist(sk);
WANG Cong8651be82016-10-19 23:35:12 -0700461 __ipv6_sock_mc_close(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700463 /*
464 * Sock is moving from IPv6 to IPv4 (sk_prot), so
465 * remove it from the refcnt debug socks count in the
466 * original family...
467 */
468 sk_refcnt_debug_dec(sk);
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (sk->sk_protocol == IPPROTO_TCP) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800471 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700473 sock_prot_inuse_add(net, sk->sk_prot, -1);
474 sock_prot_inuse_add(net, &tcp_prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 local_bh_enable();
476 sk->sk_prot = &tcp_prot;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800477 icsk->icsk_af_ops = &ipv4_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 sk->sk_socket->ops = &inet_stream_ops;
479 sk->sk_family = PF_INET;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800480 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 } else {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800482 struct proto *prot = &udp_prot;
483
David S. Millerdb8dac22008-03-06 16:22:02 -0800484 if (sk->sk_protocol == IPPROTO_UDPLITE)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800485 prot = &udplite_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700487 sock_prot_inuse_add(net, sk->sk_prot, -1);
488 sock_prot_inuse_add(net, prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 local_bh_enable();
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800490 sk->sk_prot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 sk->sk_socket->ops = &inet_dgram_ops;
492 sk->sk_family = PF_INET;
493 }
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800494 opt = xchg((__force struct ipv6_txoptions **)&np->opt,
495 NULL);
496 if (opt) {
497 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
498 txopt_put(opt);
499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 pktopt = xchg(&np->pktoptions, NULL);
Wei Yongjun800d55f2009-02-23 21:45:33 +0000501 kfree_skb(pktopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700503 /*
504 * ... and add it to the refcnt debug socks count
505 * in the new family. -acme
506 */
507 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 module_put(THIS_MODULE);
509 retv = 0;
510 break;
511 }
512 goto e_inval;
513
514 case IPV6_V6ONLY:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900515 if (optlen < sizeof(int) ||
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000516 inet_sk(sk)->inet_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 goto e_inval;
Eric Dumazet9fe516b2014-06-27 08:36:16 -0700518 sk->sk_ipv6only = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 retv = 0;
520 break;
521
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900522 case IPV6_RECVPKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900523 if (optlen < sizeof(int))
524 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 np->rxopt.bits.rxinfo = valbool;
526 retv = 0;
527 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900528
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900529 case IPV6_2292PKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900530 if (optlen < sizeof(int))
531 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900532 np->rxopt.bits.rxoinfo = valbool;
533 retv = 0;
534 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900536 case IPV6_RECVHOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900537 if (optlen < sizeof(int))
538 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 np->rxopt.bits.rxhlim = valbool;
540 retv = 0;
541 break;
542
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900543 case IPV6_2292HOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900544 if (optlen < sizeof(int))
545 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900546 np->rxopt.bits.rxohlim = valbool;
547 retv = 0;
548 break;
549
550 case IPV6_RECVRTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900551 if (optlen < sizeof(int))
552 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900553 np->rxopt.bits.srcrt = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 retv = 0;
555 break;
556
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900557 case IPV6_2292RTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900558 if (optlen < sizeof(int))
559 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900560 np->rxopt.bits.osrcrt = valbool;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900561 retv = 0;
562 break;
563
564 case IPV6_RECVHOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900565 if (optlen < sizeof(int))
566 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 np->rxopt.bits.hopopts = valbool;
568 retv = 0;
569 break;
570
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900571 case IPV6_2292HOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900572 if (optlen < sizeof(int))
573 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900574 np->rxopt.bits.ohopopts = valbool;
575 retv = 0;
576 break;
577
578 case IPV6_RECVDSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900579 if (optlen < sizeof(int))
580 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 np->rxopt.bits.dstopts = valbool;
582 retv = 0;
583 break;
584
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900585 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900586 if (optlen < sizeof(int))
587 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900588 np->rxopt.bits.odstopts = valbool;
589 retv = 0;
590 break;
591
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900592 case IPV6_TCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900593 if (optlen < sizeof(int))
594 goto e_inval;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700595 if (val < -1 || val > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900596 goto e_inval;
Gerrit Renker26ced1e2009-08-09 08:12:49 +0000597 /* RFC 3542, 6.5: default traffic class of 0x0 */
598 if (val == -1)
599 val = 0;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900600 np->tclass = val;
601 retv = 0;
602 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900603
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900604 case IPV6_RECVTCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900605 if (optlen < sizeof(int))
606 goto e_inval;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900607 np->rxopt.bits.rxtclass = valbool;
608 retv = 0;
609 break;
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 case IPV6_FLOWINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900612 if (optlen < sizeof(int))
613 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 np->rxopt.bits.rxflow = valbool;
615 retv = 0;
616 break;
617
Brian Haley793b14732010-04-23 11:26:07 +0000618 case IPV6_RECVPATHMTU:
619 if (optlen < sizeof(int))
620 goto e_inval;
621 np->rxopt.bits.rxpmtu = valbool;
622 retv = 0;
623 break;
624
Balazs Scheidler6c468622010-10-21 16:08:28 +0200625 case IPV6_TRANSPARENT:
Maciej Żenczykowski35fc59c2019-11-21 13:19:08 -0800626 if (valbool && !ns_capable(net->user_ns, CAP_NET_RAW) &&
627 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
Balazs Scheidlerb8894162010-10-23 04:48:14 +0000628 retv = -EPERM;
629 break;
630 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200631 if (optlen < sizeof(int))
632 goto e_inval;
633 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
634 inet_sk(sk)->transparent = valbool;
635 retv = 0;
636 break;
637
Maciej Żenczykowski84e14fe2017-09-26 21:32:42 -0700638 case IPV6_FREEBIND:
639 if (optlen < sizeof(int))
640 goto e_inval;
641 /* we also don't have a separate freebind bit for IPV6 */
642 inet_sk(sk)->freebind = valbool;
643 retv = 0;
644 break;
645
Balazs Scheidler6c468622010-10-21 16:08:28 +0200646 case IPV6_RECVORIGDSTADDR:
647 if (optlen < sizeof(int))
648 goto e_inval;
649 np->rxopt.bits.rxorigdstaddr = valbool;
650 retv = 0;
651 break;
652
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900653 case IPV6_HOPOPTS:
654 case IPV6_RTHDRDSTOPTS:
655 case IPV6_RTHDR:
656 case IPV6_DSTOPTS:
Christoph Hellwigb84d2b72020-07-23 08:09:02 +0200657 retv = ipv6_set_opt_hdr(sk, optname, optval, optlen);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900658 break;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900659
Yang Hongyangb24a2512008-12-16 02:06:23 -0800660 case IPV6_PKTINFO:
661 {
662 struct in6_pktinfo pkt;
663
664 if (optlen == 0)
665 goto e_inval;
Ian Morris63159f22015-03-29 14:00:04 +0100666 else if (optlen < sizeof(struct in6_pktinfo) || !optval)
Yang Hongyangb24a2512008-12-16 02:06:23 -0800667 goto e_inval;
668
Ilpo Järvinen914d1162009-01-04 17:27:31 -0800669 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
Yang Hongyangb24a2512008-12-16 02:06:23 -0800670 retv = -EFAULT;
671 break;
672 }
Mike Manningd839a0e2018-11-07 15:36:08 +0000673 if (!sk_dev_equal_l3scope(sk, pkt.ipi6_ifindex))
Yang Hongyangb24a2512008-12-16 02:06:23 -0800674 goto e_inval;
675
676 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000677 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
Yang Hongyangb24a2512008-12-16 02:06:23 -0800678 retv = 0;
679 break;
680 }
681
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900682 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 {
684 struct ipv6_txoptions *opt = NULL;
685 struct msghdr msg;
David S. Miller4c9483b2011-03-12 16:22:43 -0500686 struct flowi6 fl6;
Wei Wang26879da2016-05-02 21:40:07 -0700687 struct ipcm6_cookie ipc6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
David S. Miller4c9483b2011-03-12 16:22:43 -0500689 memset(&fl6, 0, sizeof(fl6));
690 fl6.flowi6_oif = sk->sk_bound_dev_if;
691 fl6.flowi6_mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 if (optlen == 0)
694 goto update;
695
696 /* 1K is probably excessive
697 * 1K is surely not enough, 2K per standard header is 16K.
698 */
699 retv = -EINVAL;
700 if (optlen > 64*1024)
701 break;
702
703 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
704 retv = -ENOBUFS;
Ian Morris63159f22015-03-29 14:00:04 +0100705 if (!opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 break;
707
708 memset(opt, 0, sizeof(*opt));
Reshetova, Elena0aeea212017-07-04 09:34:54 +0300709 refcount_set(&opt->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 opt->tot_len = sizeof(*opt) + optlen;
711 retv = -EFAULT;
712 if (copy_from_user(opt+1, optval, optlen))
713 goto done;
714
715 msg.msg_controllen = optlen;
Ian Morris67ba4152014-08-24 21:53:10 +0100716 msg.msg_control = (void *)(opt+1);
Wei Wang26879da2016-05-02 21:40:07 -0700717 ipc6.opt = opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Willem de Bruijn5fdaa882018-07-06 10:12:57 -0400719 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (retv)
721 goto done;
722update:
723 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700724 opt = ipv6_update_options(sk, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725done:
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800726 if (opt) {
727 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
728 txopt_put(opt);
729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 break;
731 }
732 case IPV6_UNICAST_HOPS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900733 if (optlen < sizeof(int))
734 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (val > 255 || val < -1)
736 goto e_inval;
737 np->hop_limit = val;
738 retv = 0;
739 break;
740
741 case IPV6_MULTICAST_HOPS:
742 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900743 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900744 if (optlen < sizeof(int))
745 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (val > 255 || val < -1)
747 goto e_inval;
Li Wei2a38e6d2011-11-27 21:33:34 +0000748 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 retv = 0;
750 break;
751
752 case IPV6_MULTICAST_LOOP:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900753 if (optlen < sizeof(int))
754 goto e_inval;
YOSHIFUJI Hideaki28d44882008-06-12 03:14:51 +0900755 if (val != valbool)
756 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 np->mc_loop = valbool;
758 retv = 0;
759 break;
760
Erich E. Hooverc4062df2012-02-08 09:11:08 +0000761 case IPV6_UNICAST_IF:
762 {
763 struct net_device *dev = NULL;
764 int ifindex;
765
766 if (optlen != sizeof(int))
767 goto e_inval;
768
769 ifindex = (__force int)ntohl((__force __be32)val);
770 if (ifindex == 0) {
771 np->ucast_oif = 0;
772 retv = 0;
773 break;
774 }
775
776 dev = dev_get_by_index(net, ifindex);
777 retv = -EADDRNOTAVAIL;
778 if (!dev)
779 break;
780 dev_put(dev);
781
782 retv = -EINVAL;
783 if (sk->sk_bound_dev_if)
784 break;
785
786 np->ucast_oif = ifindex;
787 retv = 0;
788 break;
789 }
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 case IPV6_MULTICAST_IF:
792 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900793 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900794 if (optlen < sizeof(int))
795 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Brian Haley4953f0f2007-10-11 14:39:29 -0700797 if (val) {
Eric Dumazet55b80502009-10-19 06:41:58 +0000798 struct net_device *dev;
David Ahern7bb387c2016-12-29 15:39:37 -0800799 int midx;
Eric Dumazet55b80502009-10-19 06:41:58 +0000800
David Ahern7bb387c2016-12-29 15:39:37 -0800801 rcu_read_lock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700802
David Ahern7bb387c2016-12-29 15:39:37 -0800803 dev = dev_get_by_index_rcu(net, val);
Eric Dumazet55b80502009-10-19 06:41:58 +0000804 if (!dev) {
David Ahern7bb387c2016-12-29 15:39:37 -0800805 rcu_read_unlock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700806 retv = -ENODEV;
807 break;
808 }
David Ahern7bb387c2016-12-29 15:39:37 -0800809 midx = l3mdev_master_ifindex_rcu(dev);
810
811 rcu_read_unlock();
812
813 if (sk->sk_bound_dev_if &&
814 sk->sk_bound_dev_if != val &&
815 (!midx || midx != sk->sk_bound_dev_if))
816 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 np->mcast_oif = val;
819 retv = 0;
820 break;
821 case IPV6_ADD_MEMBERSHIP:
822 case IPV6_DROP_MEMBERSHIP:
823 {
824 struct ipv6_mreq mreq;
825
Wang Chena28398b2008-04-07 09:42:07 +0800826 if (optlen < sizeof(struct ipv6_mreq))
827 goto e_inval;
828
Flavio Leitnera96fb492007-08-24 22:16:39 -0700829 retv = -EPROTO;
830 if (inet_sk(sk)->is_icsk)
831 break;
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 retv = -EFAULT;
834 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
835 break;
836
837 if (optname == IPV6_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300838 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300840 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 break;
842 }
843 case IPV6_JOIN_ANYCAST:
844 case IPV6_LEAVE_ANYCAST:
845 {
846 struct ipv6_mreq mreq;
847
Wang Chena28398b2008-04-07 09:42:07 +0800848 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 goto e_inval;
850
851 retv = -EFAULT;
852 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
853 break;
854
855 if (optname == IPV6_JOIN_ANYCAST)
856 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
857 else
858 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
859 break;
860 }
Andre Naujoks15033f02018-09-10 10:27:15 +0200861 case IPV6_MULTICAST_ALL:
862 if (optlen < sizeof(int))
863 goto e_inval;
864 np->mc_all = valbool;
865 retv = 0;
866 break;
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 case MCAST_JOIN_GROUP:
869 case MCAST_LEAVE_GROUP:
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200870 if (in_compat_syscall())
871 retv = compat_ipv6_mcast_join_leave(sk, optname, optval,
872 optlen);
873 else
874 retv = ipv6_mcast_join_leave(sk, optname, optval,
875 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 case MCAST_JOIN_SOURCE_GROUP:
878 case MCAST_LEAVE_SOURCE_GROUP:
879 case MCAST_BLOCK_SOURCE:
880 case MCAST_UNBLOCK_SOURCE:
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200881 retv = do_ipv6_mcast_group_source(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 case MCAST_MSFILTER:
Christoph Hellwig3021ad52020-07-17 08:23:30 +0200884 if (in_compat_syscall())
885 retv = compat_ipv6_set_mcast_msfilter(sk, optval,
886 optlen);
887 else
888 retv = ipv6_set_mcast_msfilter(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 case IPV6_ROUTER_ALERT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900891 if (optlen < sizeof(int))
892 goto e_inval;
Denis V. Lunev725a8ff2008-07-19 00:28:58 -0700893 retv = ip6_ra_control(sk, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 break;
Francesco Ruggeri9036b2f2019-03-01 15:31:03 -0800895 case IPV6_ROUTER_ALERT_ISOLATE:
896 if (optlen < sizeof(int))
897 goto e_inval;
898 np->rtalert_isolate = valbool;
899 retv = 0;
900 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 case IPV6_MTU_DISCOVER:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900902 if (optlen < sizeof(int))
903 goto e_inval;
Hannes Frederic Sowa0b952272014-02-26 01:20:43 +0100904 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 goto e_inval;
906 np->pmtudisc = val;
907 retv = 0;
908 break;
909 case IPV6_MTU:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900910 if (optlen < sizeof(int))
911 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (val && val < IPV6_MIN_MTU)
913 goto e_inval;
914 np->frag_size = val;
915 retv = 0;
916 break;
917 case IPV6_RECVERR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900918 if (optlen < sizeof(int))
919 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 np->recverr = valbool;
921 if (!val)
922 skb_queue_purge(&sk->sk_error_queue);
923 retv = 0;
924 break;
925 case IPV6_FLOWINFO_SEND:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900926 if (optlen < sizeof(int))
927 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 np->sndflow = valbool;
929 retv = 0;
930 break;
931 case IPV6_FLOWLABEL_MGR:
Christoph Hellwig86298282020-07-23 08:09:01 +0200932 retv = ipv6_flowlabel_opt(sk, USER_SOCKPTR(optval), optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 break;
934 case IPV6_IPSEC_POLICY:
935 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700936 retv = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000937 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700938 break;
Christoph Hellwigc6d1b262020-07-23 08:08:51 +0200939 retv = xfrm_user_policy(sk, optname, USER_SOCKPTR(optval),
940 optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 break;
942
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900943 case IPV6_ADDR_PREFERENCES:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900944 if (optlen < sizeof(int))
945 goto e_inval;
Christoph Hellwig18d5ad62020-05-28 07:12:33 +0200946 retv = __ip6_sock_set_addr_preferences(sk, val);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900947 break;
Stephen Hemmingere802af92010-04-22 15:24:53 -0700948 case IPV6_MINHOPCOUNT:
949 if (optlen < sizeof(int))
950 goto e_inval;
951 if (val < 0 || val > 255)
952 goto e_inval;
953 np->min_hopcount = val;
Hannes Frederic Sowad4596ba2012-11-10 19:52:34 +0000954 retv = 0;
Brian Haley793b14732010-04-23 11:26:07 +0000955 break;
956 case IPV6_DONTFRAG:
957 np->dontfrag = valbool;
Stephen Hemmingere802af92010-04-22 15:24:53 -0700958 retv = 0;
959 break;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700960 case IPV6_AUTOFLOWLABEL:
961 np->autoflowlabel = valbool;
Shaohua Li513674b2017-12-20 12:10:21 -0800962 np->autoflowlabel_set = 1;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700963 retv = 0;
964 break;
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -0400965 case IPV6_RECVFRAGSIZE:
966 np->rxopt.bits.recvfragsize = valbool;
967 retv = 0;
968 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300972 if (needs_rtnl)
973 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return retv;
976
977e_inval:
978 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300979 if (needs_rtnl)
980 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return -EINVAL;
982}
983
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800984int ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700985 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800986{
987 int err;
988
989 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
990 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
991
992 if (level != SOL_IPV6)
993 return -ENOPROTOOPT;
994
995 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
996#ifdef CONFIG_NETFILTER
997 /* we need to exclude all possible ENOPROTOOPTs except default case */
998 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +0100999 optname != IPV6_XFRM_POLICY)
Christoph Hellwigc2f12632020-07-23 08:08:54 +02001000 err = nf_setsockopt(sk, PF_INET6, optname, USER_SOCKPTR(optval),
1001 optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001002#endif
1003 return err;
1004}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001005EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001006
David S. Miller286930792007-03-07 12:50:46 -08001007static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001008 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001009{
David S. Miller286930792007-03-07 12:50:46 -08001010 struct ipv6_opt_hdr *hdr;
1011
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001012 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001013 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001014
Ian Morris67ba4152014-08-24 21:53:10 +01001015 switch (optname) {
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001016 case IPV6_HOPOPTS:
1017 hdr = opt->hopopt;
1018 break;
1019 case IPV6_RTHDRDSTOPTS:
1020 hdr = opt->dst0opt;
1021 break;
1022 case IPV6_RTHDR:
1023 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
1024 break;
1025 case IPV6_DSTOPTS:
1026 hdr = opt->dst1opt;
1027 break;
1028 default:
1029 return -EINVAL; /* should not happen */
1030 }
1031
1032 if (!hdr)
1033 return 0;
David S. Miller286930792007-03-07 12:50:46 -08001034
Chris Wrightd2b02ed2007-03-09 16:19:17 -08001035 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -07001036 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001037 return -EFAULT;
Yang Hongyang95b496b2008-05-28 16:27:28 +08001038 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001039}
1040
Christoph Hellwigd5541e82020-07-17 08:23:27 +02001041static int ipv6_get_msfilter(struct sock *sk, void __user *optval,
1042 int __user *optlen, int len)
1043{
1044 const int size0 = offsetof(struct group_filter, gf_slist);
1045 struct group_filter __user *p = optval;
1046 struct group_filter gsf;
1047 int num;
1048 int err;
1049
1050 if (len < size0)
1051 return -EINVAL;
1052 if (copy_from_user(&gsf, p, size0))
1053 return -EFAULT;
1054 if (gsf.gf_group.ss_family != AF_INET6)
1055 return -EADDRNOTAVAIL;
1056 num = gsf.gf_numsrc;
1057 lock_sock(sk);
1058 err = ip6_mc_msfget(sk, &gsf, p->gf_slist);
1059 if (!err) {
1060 if (num > gsf.gf_numsrc)
1061 num = gsf.gf_numsrc;
1062 if (put_user(GROUP_FILTER_SIZE(num), optlen) ||
1063 copy_to_user(p, &gsf, size0))
1064 err = -EFAULT;
1065 }
1066 release_sock(sk);
1067 return err;
1068}
1069
Christoph Hellwigd5541e82020-07-17 08:23:27 +02001070static int compat_ipv6_get_msfilter(struct sock *sk, void __user *optval,
1071 int __user *optlen)
1072{
1073 const int size0 = offsetof(struct compat_group_filter, gf_slist);
1074 struct compat_group_filter __user *p = optval;
1075 struct compat_group_filter gf32;
1076 struct group_filter gf;
1077 int len, err;
1078 int num;
1079
1080 if (get_user(len, optlen))
1081 return -EFAULT;
1082 if (len < size0)
1083 return -EINVAL;
1084
1085 if (copy_from_user(&gf32, p, size0))
1086 return -EFAULT;
1087 gf.gf_interface = gf32.gf_interface;
1088 gf.gf_fmode = gf32.gf_fmode;
1089 num = gf.gf_numsrc = gf32.gf_numsrc;
1090 gf.gf_group = gf32.gf_group;
1091
1092 if (gf.gf_group.ss_family != AF_INET6)
1093 return -EADDRNOTAVAIL;
1094
1095 lock_sock(sk);
1096 err = ip6_mc_msfget(sk, &gf, p->gf_slist);
1097 release_sock(sk);
1098 if (err)
1099 return err;
1100 if (num > gf.gf_numsrc)
1101 num = gf.gf_numsrc;
1102 len = GROUP_FILTER_SIZE(num) - (sizeof(gf)-sizeof(gf32));
1103 if (put_user(len, optlen) ||
1104 put_user(gf.gf_fmode, &p->gf_fmode) ||
1105 put_user(gf.gf_numsrc, &p->gf_numsrc))
1106 return -EFAULT;
1107 return 0;
1108}
Christoph Hellwigd5541e82020-07-17 08:23:27 +02001109
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001110static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001111 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
1113 struct ipv6_pinfo *np = inet6_sk(sk);
1114 int len;
1115 int val;
1116
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001117 if (ip6_mroute_opt(optname))
1118 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (get_user(len, optlen))
1121 return -EFAULT;
1122 switch (optname) {
1123 case IPV6_ADDRFORM:
1124 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001125 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 sk->sk_protocol != IPPROTO_TCP)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +09001127 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (sk->sk_state != TCP_ESTABLISHED)
1129 return -ENOTCONN;
1130 val = sk->sk_family;
1131 break;
1132 case MCAST_MSFILTER:
Christoph Hellwig3021ad52020-07-17 08:23:30 +02001133 if (in_compat_syscall())
1134 return compat_ipv6_get_msfilter(sk, optval, optlen);
Christoph Hellwigd5541e82020-07-17 08:23:27 +02001135 return ipv6_get_msfilter(sk, optval, optlen, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001136 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 {
1138 struct msghdr msg;
1139 struct sk_buff *skb;
1140
1141 if (sk->sk_type != SOCK_STREAM)
1142 return -ENOPROTOOPT;
1143
1144 msg.msg_control = optval;
1145 msg.msg_controllen = len;
Daniel Baluta98e77432011-08-19 03:19:07 -07001146 msg.msg_flags = flags;
Christoph Hellwig1b2f08d2020-05-13 21:36:41 +02001147 msg.msg_control_is_user = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 lock_sock(sk);
1150 skb = np->pktoptions;
1151 if (skb)
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001152 ip6_datagram_recv_ctl(sk, &msg, skb);
Eric Dumazet1dc7b902015-01-21 03:45:42 -08001153 release_sock(sk);
1154 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (np->rxopt.bits.rxinfo) {
1156 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001157 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1158 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001159 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1161 }
1162 if (np->rxopt.bits.rxhlim) {
1163 int hlim = np->mcast_hops;
1164 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1165 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001166 if (np->rxopt.bits.rxtclass) {
Li RongQingd76ed222014-01-15 17:03:30 +08001167 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1168
Jiri Benc4c507d22012-02-09 09:35:49 +00001169 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1170 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001171 if (np->rxopt.bits.rxoinfo) {
1172 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001173 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1174 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001175 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1176 np->sticky_pktinfo.ipi6_addr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001177 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1178 }
1179 if (np->rxopt.bits.rxohlim) {
1180 int hlim = np->mcast_hops;
1181 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1182 }
Florent Fourcot1397ed32013-12-08 15:46:57 +01001183 if (np->rxopt.bits.rxflow) {
Florent Fourcot68536052013-12-12 17:07:58 +01001184 __be32 flowinfo = np->rcv_flowinfo;
1185
Florent Fourcot1397ed32013-12-08 15:46:57 +01001186 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189 len -= msg.msg_controllen;
1190 return put_user(len, optlen);
1191 }
1192 case IPV6_MTU:
1193 {
1194 struct dst_entry *dst;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001195
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001196 val = 0;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001197 rcu_read_lock();
1198 dst = __sk_dst_get(sk);
1199 if (dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 val = dst_mtu(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001201 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (!val)
1203 return -ENOTCONN;
1204 break;
1205 }
1206
1207 case IPV6_V6ONLY:
Eric Dumazet9fe516b2014-06-27 08:36:16 -07001208 val = sk->sk_ipv6only;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 break;
1210
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001211 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 val = np->rxopt.bits.rxinfo;
1213 break;
1214
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001215 case IPV6_2292PKTINFO:
1216 val = np->rxopt.bits.rxoinfo;
1217 break;
1218
1219 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 val = np->rxopt.bits.rxhlim;
1221 break;
1222
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001223 case IPV6_2292HOPLIMIT:
1224 val = np->rxopt.bits.rxohlim;
1225 break;
1226
1227 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 val = np->rxopt.bits.srcrt;
1229 break;
1230
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001231 case IPV6_2292RTHDR:
1232 val = np->rxopt.bits.osrcrt;
1233 break;
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001236 case IPV6_RTHDRDSTOPTS:
1237 case IPV6_RTHDR:
1238 case IPV6_DSTOPTS:
1239 {
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001240 struct ipv6_txoptions *opt;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001241
1242 lock_sock(sk);
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001243 opt = rcu_dereference_protected(np->opt,
1244 lockdep_sock_is_held(sk));
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001245 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001246 release_sock(sk);
Yang Hongyang05335c22008-05-28 16:23:47 +08001247 /* check if ipv6_getsockopt_sticky() returns err code */
1248 if (len < 0)
1249 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001250 return put_user(len, optlen);
1251 }
1252
1253 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 val = np->rxopt.bits.hopopts;
1255 break;
1256
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001257 case IPV6_2292HOPOPTS:
1258 val = np->rxopt.bits.ohopopts;
1259 break;
1260
1261 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 val = np->rxopt.bits.dstopts;
1263 break;
1264
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001265 case IPV6_2292DSTOPTS:
1266 val = np->rxopt.bits.odstopts;
1267 break;
1268
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001269 case IPV6_TCLASS:
1270 val = np->tclass;
1271 break;
1272
1273 case IPV6_RECVTCLASS:
1274 val = np->rxopt.bits.rxtclass;
1275 break;
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 case IPV6_FLOWINFO:
1278 val = np->rxopt.bits.rxflow;
1279 break;
1280
Brian Haley793b14732010-04-23 11:26:07 +00001281 case IPV6_RECVPATHMTU:
1282 val = np->rxopt.bits.rxpmtu;
1283 break;
1284
1285 case IPV6_PATHMTU:
1286 {
1287 struct dst_entry *dst;
1288 struct ip6_mtuinfo mtuinfo;
1289
1290 if (len < sizeof(mtuinfo))
1291 return -EINVAL;
1292
1293 len = sizeof(mtuinfo);
1294 memset(&mtuinfo, 0, sizeof(mtuinfo));
1295
1296 rcu_read_lock();
1297 dst = __sk_dst_get(sk);
1298 if (dst)
1299 mtuinfo.ip6m_mtu = dst_mtu(dst);
1300 rcu_read_unlock();
1301 if (!mtuinfo.ip6m_mtu)
1302 return -ENOTCONN;
1303
1304 if (put_user(len, optlen))
1305 return -EFAULT;
1306 if (copy_to_user(optval, &mtuinfo, len))
1307 return -EFAULT;
1308
1309 return 0;
Brian Haley793b14732010-04-23 11:26:07 +00001310 }
1311
Balazs Scheidler6c468622010-10-21 16:08:28 +02001312 case IPV6_TRANSPARENT:
1313 val = inet_sk(sk)->transparent;
1314 break;
1315
Maciej Żenczykowski84e14fe2017-09-26 21:32:42 -07001316 case IPV6_FREEBIND:
1317 val = inet_sk(sk)->freebind;
1318 break;
1319
Balazs Scheidler6c468622010-10-21 16:08:28 +02001320 case IPV6_RECVORIGDSTADDR:
1321 val = np->rxopt.bits.rxorigdstaddr;
1322 break;
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -08001326 {
1327 struct dst_entry *dst;
1328
1329 if (optname == IPV6_UNICAST_HOPS)
1330 val = np->hop_limit;
1331 else
1332 val = np->mcast_hops;
1333
Eric Dumazetb6c67122010-04-08 23:03:29 +00001334 if (val < 0) {
1335 rcu_read_lock();
1336 dst = __sk_dst_get(sk);
1337 if (dst)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001338 val = ip6_dst_hoplimit(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001339 rcu_read_unlock();
Brian Haleybefffe92006-12-12 17:09:49 -08001340 }
Eric Dumazetb6c67122010-04-08 23:03:29 +00001341
Brian Haleybefffe92006-12-12 17:09:49 -08001342 if (val < 0)
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001343 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 break;
Brian Haleybefffe92006-12-12 17:09:49 -08001345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 case IPV6_MULTICAST_LOOP:
1348 val = np->mc_loop;
1349 break;
1350
1351 case IPV6_MULTICAST_IF:
1352 val = np->mcast_oif;
1353 break;
1354
Andre Naujoks15033f02018-09-10 10:27:15 +02001355 case IPV6_MULTICAST_ALL:
1356 val = np->mc_all;
1357 break;
1358
Erich E. Hooverc4062df2012-02-08 09:11:08 +00001359 case IPV6_UNICAST_IF:
1360 val = (__force int)htonl((__u32) np->ucast_oif);
1361 break;
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 case IPV6_MTU_DISCOVER:
1364 val = np->pmtudisc;
1365 break;
1366
1367 case IPV6_RECVERR:
1368 val = np->recverr;
1369 break;
1370
1371 case IPV6_FLOWINFO_SEND:
1372 val = np->sndflow;
1373 break;
1374
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001375 case IPV6_FLOWLABEL_MGR:
1376 {
1377 struct in6_flowlabel_req freq;
Florent Fourcot46e5f402014-01-17 17:15:04 +01001378 int flags;
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001379
1380 if (len < sizeof(freq))
1381 return -EINVAL;
1382
1383 if (copy_from_user(&freq, optval, sizeof(freq)))
1384 return -EFAULT;
1385
1386 if (freq.flr_action != IPV6_FL_A_GET)
1387 return -EINVAL;
1388
1389 len = sizeof(freq);
Florent Fourcot46e5f402014-01-17 17:15:04 +01001390 flags = freq.flr_flags;
1391
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001392 memset(&freq, 0, sizeof(freq));
1393
Florent Fourcot46e5f402014-01-17 17:15:04 +01001394 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001395 if (val < 0)
1396 return val;
1397
1398 if (put_user(len, optlen))
1399 return -EFAULT;
1400 if (copy_to_user(optval, &freq, len))
1401 return -EFAULT;
1402
1403 return 0;
1404 }
1405
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001406 case IPV6_ADDR_PREFERENCES:
1407 val = 0;
1408
1409 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1410 val |= IPV6_PREFER_SRC_TMP;
1411 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1412 val |= IPV6_PREFER_SRC_PUBLIC;
1413 else {
1414 /* XXX: should we return system default? */
1415 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1416 }
1417
1418 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1419 val |= IPV6_PREFER_SRC_COA;
1420 else
1421 val |= IPV6_PREFER_SRC_HOME;
1422 break;
1423
Stephen Hemmingere802af92010-04-22 15:24:53 -07001424 case IPV6_MINHOPCOUNT:
1425 val = np->min_hopcount;
1426 break;
1427
Brian Haley793b14732010-04-23 11:26:07 +00001428 case IPV6_DONTFRAG:
1429 val = np->dontfrag;
1430 break;
1431
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001432 case IPV6_AUTOFLOWLABEL:
Ben Hutchingse9191ff2018-01-22 20:06:42 +00001433 val = ip6_autoflowlabel(sock_net(sk), np);
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001434 break;
1435
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -04001436 case IPV6_RECVFRAGSIZE:
1437 val = np->rxopt.bits.recvfragsize;
1438 break;
1439
Francesco Ruggeri9036b2f2019-03-01 15:31:03 -08001440 case IPV6_ROUTER_ALERT_ISOLATE:
1441 val = np->rtalert_isolate;
1442 break;
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001445 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447 len = min_t(unsigned int, sizeof(int), len);
Ian Morris67ba4152014-08-24 21:53:10 +01001448 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001450 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return -EFAULT;
1452 return 0;
1453}
1454
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001455int ipv6_getsockopt(struct sock *sk, int level, int optname,
1456 char __user *optval, int __user *optlen)
1457{
1458 int err;
1459
1460 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1461 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1462
Ian Morris67ba4152014-08-24 21:53:10 +01001463 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001464 return -ENOPROTOOPT;
1465
Daniel Baluta98e77432011-08-19 03:19:07 -07001466 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001467#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001468 /* we need to exclude all possible ENOPROTOOPTs except default case */
1469 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001470 int len;
1471
1472 if (get_user(len, optlen))
1473 return -EFAULT;
1474
Paolo Abeni01ea3062018-02-08 12:19:00 +01001475 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001476 if (err >= 0)
1477 err = put_user(len, optlen);
1478 }
1479#endif
1480 return err;
1481}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001482EXPORT_SYMBOL(ipv6_getsockopt);