blob: adbfed6adf11cfa1fbfcaa0a188d31d51be1ecf9 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * IPv6 BSD socket options interface
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09007 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Based on linux/net/ipv4/ip_sockglue.c
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * FIXME: Make the setsockopt code POSIX compliant: That is
12 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * o Truncate getsockopt returns
14 * o Return an optlen of the truncated length if need be
15 *
16 * Changes:
17 * David L Stevens <dlstevens@us.ibm.com>:
18 * - added multicast source filtering API for MLDv2
19 */
20
21#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080022#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/errno.h>
24#include <linux/types.h>
25#include <linux/socket.h>
26#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/net.h>
28#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090029#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/netdevice.h>
31#include <linux/if_arp.h>
32#include <linux/init.h>
33#include <linux/sysctl.h>
34#include <linux/netfilter.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <net/sock.h>
38#include <net/snmp.h>
39#include <net/ipv6.h>
40#include <net/ndisc.h>
41#include <net/protocol.h>
42#include <net/transp_v6.h>
43#include <net/ip6_route.h>
44#include <net/addrconf.h>
45#include <net/inet_common.h>
46#include <net/tcp.h>
47#include <net/udp.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080048#include <net/udplite.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <net/xfrm.h>
David L Stevensdae50292008-04-27 01:06:07 -070050#include <net/compat.h>
David Lebruna149e7c2016-11-08 14:59:21 +010051#include <net/seg6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080053#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055struct ip6_ra_chain *ip6_ra_chain;
56DEFINE_RWLOCK(ip6_ra_lock);
57
Denis V. Lunev725a8ff2008-07-19 00:28:58 -070058int ip6_ra_control(struct sock *sk, int sel)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 struct ip6_ra_chain *ra, *new_ra, **rap;
61
62 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
Eric Dumazetc720c7e82009-10-15 06:30:45 +000063 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num != IPPROTO_RAW)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +090064 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Ian Morris67ba4152014-08-24 21:53:10 +010066 new_ra = (sel >= 0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
Gen Zhang95baa602019-05-24 11:19:46 +080067 if (sel >= 0 && !new_ra)
68 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 write_lock_bh(&ip6_ra_lock);
Ian Morris67ba4152014-08-24 21:53:10 +010071 for (rap = &ip6_ra_chain; (ra = *rap) != NULL; rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (ra->sk == sk) {
Ian Morris67ba4152014-08-24 21:53:10 +010073 if (sel >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 write_unlock_bh(&ip6_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -080075 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return -EADDRINUSE;
77 }
78
79 *rap = ra->next;
80 write_unlock_bh(&ip6_ra_lock);
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 sock_put(sk);
83 kfree(ra);
84 return 0;
85 }
86 }
Ian Morris63159f22015-03-29 14:00:04 +010087 if (!new_ra) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 write_unlock_bh(&ip6_ra_lock);
89 return -ENOBUFS;
90 }
91 new_ra->sk = sk;
92 new_ra->sel = sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 new_ra->next = ra;
94 *rap = new_ra;
95 sock_hold(sk);
96 write_unlock_bh(&ip6_ra_lock);
97 return 0;
98}
99
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700100struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
101 struct ipv6_txoptions *opt)
102{
103 if (inet_sk(sk)->is_icsk) {
104 if (opt &&
105 !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000106 inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) {
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700107 struct inet_connection_sock *icsk = inet_csk(sk);
108 icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
109 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
110 }
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700111 }
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800112 opt = xchg((__force struct ipv6_txoptions **)&inet6_sk(sk)->opt,
113 opt);
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700114 sk_dst_reset(sk);
115
116 return opt;
117}
118
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300119static bool setsockopt_needs_rtnl(int optname)
120{
121 switch (optname) {
WANG Cong8651be82016-10-19 23:35:12 -0700122 case IPV6_ADDRFORM:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300123 case IPV6_ADD_MEMBERSHIP:
124 case IPV6_DROP_MEMBERSHIP:
Marcelo Ricardo Leitnerc4a68532015-03-20 11:37:17 -0300125 case IPV6_JOIN_ANYCAST:
126 case IPV6_LEAVE_ANYCAST:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300127 case MCAST_JOIN_GROUP:
128 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300129 case MCAST_JOIN_SOURCE_GROUP:
130 case MCAST_LEAVE_SOURCE_GROUP:
131 case MCAST_BLOCK_SOURCE:
132 case MCAST_UNBLOCK_SOURCE:
133 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300134 return true;
135 }
136 return false;
137}
138
Al Virofcfa0b02020-04-27 11:00:01 -0400139static int do_ipv6_mcast_group_source(struct sock *sk, int optname,
140 struct group_source_req *greqs)
141{
142 int omode, add;
143
144 if (greqs->gsr_group.ss_family != AF_INET6 ||
145 greqs->gsr_source.ss_family != AF_INET6)
146 return -EADDRNOTAVAIL;
147
148 if (optname == MCAST_BLOCK_SOURCE) {
149 omode = MCAST_EXCLUDE;
150 add = 1;
151 } else if (optname == MCAST_UNBLOCK_SOURCE) {
152 omode = MCAST_EXCLUDE;
153 add = 0;
154 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
155 struct sockaddr_in6 *psin6;
156 int retv;
157
158 psin6 = (struct sockaddr_in6 *)&greqs->gsr_group;
159 retv = ipv6_sock_mc_join_ssm(sk, greqs->gsr_interface,
160 &psin6->sin6_addr,
161 MCAST_INCLUDE);
162 /* prior join w/ different source is ok */
163 if (retv && retv != -EADDRINUSE)
164 return retv;
165 omode = MCAST_INCLUDE;
166 add = 1;
167 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
168 omode = MCAST_INCLUDE;
169 add = 0;
170 }
171 return ip6_mc_source(add, omode, sk, greqs);
172}
173
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800174static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700175 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900178 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 int val, valbool;
180 int retv = -ENOPROTOOPT;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300181 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Ian Morris63159f22015-03-29 14:00:04 +0100183 if (!optval)
Ian Morris67ba4152014-08-24 21:53:10 +0100184 val = 0;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900185 else {
186 if (optlen >= sizeof(int)) {
187 if (get_user(val, (int __user *) optval))
188 return -EFAULT;
189 } else
190 val = 0;
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Ian Morris67ba4152014-08-24 21:53:10 +0100193 valbool = (val != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900195 if (ip6_mroute_opt(optname))
196 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
197
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300198 if (needs_rtnl)
199 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 lock_sock(sk);
201
202 switch (optname) {
203
204 case IPV6_ADDRFORM:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900205 if (optlen < sizeof(int))
206 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (val == PF_INET) {
208 struct ipv6_txoptions *opt;
209 struct sk_buff *pktopt;
210
Denis V. Lunev49d074f2008-06-04 15:49:06 +0400211 if (sk->sk_type == SOCK_RAW)
212 break;
213
Denis V. Lunev9596cc822008-06-04 15:49:08 +0400214 if (sk->sk_protocol == IPPROTO_UDP ||
215 sk->sk_protocol == IPPROTO_UDPLITE) {
216 struct udp_sock *up = udp_sk(sk);
217 if (up->pending == AF_INET6) {
218 retv = -EBUSY;
219 break;
220 }
John Haxby82c9ae42020-04-18 16:30:49 +0100221 }
222 if (sk->sk_protocol == IPPROTO_TCP &&
223 sk->sk_prot != &tcpv6_prot) {
224 retv = -EBUSY;
Eric Dumazetb6f61182020-02-25 11:52:29 -0800225 break;
226 }
John Haxby82c9ae42020-04-18 16:30:49 +0100227 if (sk->sk_protocol != IPPROTO_TCP)
228 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (sk->sk_state != TCP_ESTABLISHED) {
230 retv = -ENOTCONN;
231 break;
232 }
233
234 if (ipv6_only_sock(sk) ||
Eric Dumazetefe42082013-10-03 15:42:29 -0700235 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 retv = -EADDRNOTAVAIL;
237 break;
238 }
239
240 fl6_free_socklist(sk);
WANG Cong8651be82016-10-19 23:35:12 -0700241 __ipv6_sock_mc_close(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700243 /*
244 * Sock is moving from IPv6 to IPv4 (sk_prot), so
245 * remove it from the refcnt debug socks count in the
246 * original family...
247 */
248 sk_refcnt_debug_dec(sk);
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (sk->sk_protocol == IPPROTO_TCP) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800251 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700253 sock_prot_inuse_add(net, sk->sk_prot, -1);
254 sock_prot_inuse_add(net, &tcp_prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 local_bh_enable();
256 sk->sk_prot = &tcp_prot;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800257 icsk->icsk_af_ops = &ipv4_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 sk->sk_socket->ops = &inet_stream_ops;
259 sk->sk_family = PF_INET;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800260 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 } else {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800262 struct proto *prot = &udp_prot;
263
David S. Millerdb8dac22008-03-06 16:22:02 -0800264 if (sk->sk_protocol == IPPROTO_UDPLITE)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800265 prot = &udplite_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700267 sock_prot_inuse_add(net, sk->sk_prot, -1);
268 sock_prot_inuse_add(net, prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 local_bh_enable();
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800270 sk->sk_prot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 sk->sk_socket->ops = &inet_dgram_ops;
272 sk->sk_family = PF_INET;
273 }
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800274 opt = xchg((__force struct ipv6_txoptions **)&np->opt,
275 NULL);
276 if (opt) {
277 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
278 txopt_put(opt);
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 pktopt = xchg(&np->pktoptions, NULL);
Wei Yongjun800d55f2009-02-23 21:45:33 +0000281 kfree_skb(pktopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700283 /*
284 * ... and add it to the refcnt debug socks count
285 * in the new family. -acme
286 */
287 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 module_put(THIS_MODULE);
289 retv = 0;
290 break;
291 }
292 goto e_inval;
293
294 case IPV6_V6ONLY:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900295 if (optlen < sizeof(int) ||
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000296 inet_sk(sk)->inet_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 goto e_inval;
Eric Dumazet9fe516b2014-06-27 08:36:16 -0700298 sk->sk_ipv6only = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 retv = 0;
300 break;
301
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900302 case IPV6_RECVPKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900303 if (optlen < sizeof(int))
304 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 np->rxopt.bits.rxinfo = valbool;
306 retv = 0;
307 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900308
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900309 case IPV6_2292PKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900310 if (optlen < sizeof(int))
311 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900312 np->rxopt.bits.rxoinfo = valbool;
313 retv = 0;
314 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900316 case IPV6_RECVHOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900317 if (optlen < sizeof(int))
318 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 np->rxopt.bits.rxhlim = valbool;
320 retv = 0;
321 break;
322
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900323 case IPV6_2292HOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900324 if (optlen < sizeof(int))
325 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900326 np->rxopt.bits.rxohlim = valbool;
327 retv = 0;
328 break;
329
330 case IPV6_RECVRTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900331 if (optlen < sizeof(int))
332 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900333 np->rxopt.bits.srcrt = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 retv = 0;
335 break;
336
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900337 case IPV6_2292RTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900338 if (optlen < sizeof(int))
339 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900340 np->rxopt.bits.osrcrt = valbool;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900341 retv = 0;
342 break;
343
344 case IPV6_RECVHOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900345 if (optlen < sizeof(int))
346 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 np->rxopt.bits.hopopts = valbool;
348 retv = 0;
349 break;
350
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900351 case IPV6_2292HOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900352 if (optlen < sizeof(int))
353 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900354 np->rxopt.bits.ohopopts = valbool;
355 retv = 0;
356 break;
357
358 case IPV6_RECVDSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900359 if (optlen < sizeof(int))
360 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 np->rxopt.bits.dstopts = valbool;
362 retv = 0;
363 break;
364
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900365 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900366 if (optlen < sizeof(int))
367 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900368 np->rxopt.bits.odstopts = valbool;
369 retv = 0;
370 break;
371
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900372 case IPV6_TCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900373 if (optlen < sizeof(int))
374 goto e_inval;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700375 if (val < -1 || val > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900376 goto e_inval;
Gerrit Renker26ced1e2009-08-09 08:12:49 +0000377 /* RFC 3542, 6.5: default traffic class of 0x0 */
378 if (val == -1)
379 val = 0;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900380 np->tclass = val;
381 retv = 0;
382 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900383
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900384 case IPV6_RECVTCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900385 if (optlen < sizeof(int))
386 goto e_inval;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900387 np->rxopt.bits.rxtclass = valbool;
388 retv = 0;
389 break;
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 case IPV6_FLOWINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900392 if (optlen < sizeof(int))
393 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 np->rxopt.bits.rxflow = valbool;
395 retv = 0;
396 break;
397
Brian Haley793b14732010-04-23 11:26:07 +0000398 case IPV6_RECVPATHMTU:
399 if (optlen < sizeof(int))
400 goto e_inval;
401 np->rxopt.bits.rxpmtu = valbool;
402 retv = 0;
403 break;
404
Balazs Scheidler6c468622010-10-21 16:08:28 +0200405 case IPV6_TRANSPARENT:
Maciej Żenczykowski35fc59c2019-11-21 13:19:08 -0800406 if (valbool && !ns_capable(net->user_ns, CAP_NET_RAW) &&
407 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
Balazs Scheidlerb8894162010-10-23 04:48:14 +0000408 retv = -EPERM;
409 break;
410 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200411 if (optlen < sizeof(int))
412 goto e_inval;
413 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
414 inet_sk(sk)->transparent = valbool;
415 retv = 0;
416 break;
417
Maciej Żenczykowski84e14fe2017-09-26 21:32:42 -0700418 case IPV6_FREEBIND:
419 if (optlen < sizeof(int))
420 goto e_inval;
421 /* we also don't have a separate freebind bit for IPV6 */
422 inet_sk(sk)->freebind = valbool;
423 retv = 0;
424 break;
425
Balazs Scheidler6c468622010-10-21 16:08:28 +0200426 case IPV6_RECVORIGDSTADDR:
427 if (optlen < sizeof(int))
428 goto e_inval;
429 np->rxopt.bits.rxorigdstaddr = valbool;
430 retv = 0;
431 break;
432
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900433 case IPV6_HOPOPTS:
434 case IPV6_RTHDRDSTOPTS:
435 case IPV6_RTHDR:
436 case IPV6_DSTOPTS:
437 {
438 struct ipv6_txoptions *opt;
Paul Moorea9ba23d2018-07-04 09:58:05 -0400439 struct ipv6_opt_hdr *new = NULL;
440
441 /* hop-by-hop / destination options are privileged option */
442 retv = -EPERM;
443 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
444 break;
Shan Weiaea74272008-06-19 16:29:39 -0700445
446 /* remove any sticky options header with a zero option
447 * length, per RFC3542.
448 */
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900449 if (optlen == 0)
Luiz Capitulinocb422c42005-11-14 21:43:36 -0800450 optval = NULL;
Ian Morris63159f22015-03-29 14:00:04 +0100451 else if (!optval)
Yang Hongyangcfb266c2008-08-03 18:16:15 -0700452 goto e_inval;
Shan Weiaea74272008-06-19 16:29:39 -0700453 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
454 optlen & 0x7 || optlen > 8 * 255)
455 goto e_inval;
Paul Moorea9ba23d2018-07-04 09:58:05 -0400456 else {
457 new = memdup_user(optval, optlen);
458 if (IS_ERR(new)) {
459 retv = PTR_ERR(new);
460 break;
461 }
462 if (unlikely(ipv6_optlen(new) > optlen)) {
463 kfree(new);
464 goto e_inval;
465 }
466 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900467
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200468 opt = rcu_dereference_protected(np->opt,
469 lockdep_sock_is_held(sk));
Paul Moorea9ba23d2018-07-04 09:58:05 -0400470 opt = ipv6_renew_options(sk, opt, optname, new);
471 kfree(new);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900472 if (IS_ERR(opt)) {
473 retv = PTR_ERR(opt);
474 break;
475 }
476
477 /* routing header option needs extra check */
Brian Haley6e093d92008-11-12 22:59:21 -0800478 retv = -EINVAL;
Olaf Kirchdfee0a72007-03-09 13:55:38 -0800479 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900480 struct ipv6_rt_hdr *rthdr = opt->srcrt;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700481 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000482#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700483 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800484 if (rthdr->hdrlen != 2 ||
485 rthdr->segments_left != 1)
486 goto sticky_done;
487
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700488 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700489#endif
David Lebruna149e7c2016-11-08 14:59:21 +0100490 case IPV6_SRCRT_TYPE_4:
491 {
492 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)
493 opt->srcrt;
494
495 if (!seg6_validate_srh(srh, optlen))
496 goto sticky_done;
497 break;
498 }
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700499 default:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900500 goto sticky_done;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700501 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900502 }
503
504 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700505 opt = ipv6_update_options(sk, opt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900506sticky_done:
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800507 if (opt) {
508 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
509 txopt_put(opt);
510 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900511 break;
512 }
513
Yang Hongyangb24a2512008-12-16 02:06:23 -0800514 case IPV6_PKTINFO:
515 {
516 struct in6_pktinfo pkt;
517
518 if (optlen == 0)
519 goto e_inval;
Ian Morris63159f22015-03-29 14:00:04 +0100520 else if (optlen < sizeof(struct in6_pktinfo) || !optval)
Yang Hongyangb24a2512008-12-16 02:06:23 -0800521 goto e_inval;
522
Ilpo Järvinen914d1162009-01-04 17:27:31 -0800523 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
Yang Hongyangb24a2512008-12-16 02:06:23 -0800524 retv = -EFAULT;
525 break;
526 }
Mike Manningd839a0e2018-11-07 15:36:08 +0000527 if (!sk_dev_equal_l3scope(sk, pkt.ipi6_ifindex))
Yang Hongyangb24a2512008-12-16 02:06:23 -0800528 goto e_inval;
529
530 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000531 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
Yang Hongyangb24a2512008-12-16 02:06:23 -0800532 retv = 0;
533 break;
534 }
535
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900536 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 {
538 struct ipv6_txoptions *opt = NULL;
539 struct msghdr msg;
David S. Miller4c9483b2011-03-12 16:22:43 -0500540 struct flowi6 fl6;
Wei Wang26879da2016-05-02 21:40:07 -0700541 struct ipcm6_cookie ipc6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
David S. Miller4c9483b2011-03-12 16:22:43 -0500543 memset(&fl6, 0, sizeof(fl6));
544 fl6.flowi6_oif = sk->sk_bound_dev_if;
545 fl6.flowi6_mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 if (optlen == 0)
548 goto update;
549
550 /* 1K is probably excessive
551 * 1K is surely not enough, 2K per standard header is 16K.
552 */
553 retv = -EINVAL;
554 if (optlen > 64*1024)
555 break;
556
557 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
558 retv = -ENOBUFS;
Ian Morris63159f22015-03-29 14:00:04 +0100559 if (!opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 break;
561
562 memset(opt, 0, sizeof(*opt));
Reshetova, Elena0aeea212017-07-04 09:34:54 +0300563 refcount_set(&opt->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 opt->tot_len = sizeof(*opt) + optlen;
565 retv = -EFAULT;
566 if (copy_from_user(opt+1, optval, optlen))
567 goto done;
568
569 msg.msg_controllen = optlen;
Ian Morris67ba4152014-08-24 21:53:10 +0100570 msg.msg_control = (void *)(opt+1);
Wei Wang26879da2016-05-02 21:40:07 -0700571 ipc6.opt = opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Willem de Bruijn5fdaa882018-07-06 10:12:57 -0400573 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (retv)
575 goto done;
576update:
577 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700578 opt = ipv6_update_options(sk, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579done:
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800580 if (opt) {
581 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
582 txopt_put(opt);
583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 break;
585 }
586 case IPV6_UNICAST_HOPS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900587 if (optlen < sizeof(int))
588 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (val > 255 || val < -1)
590 goto e_inval;
591 np->hop_limit = val;
592 retv = 0;
593 break;
594
595 case IPV6_MULTICAST_HOPS:
596 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900597 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900598 if (optlen < sizeof(int))
599 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (val > 255 || val < -1)
601 goto e_inval;
Li Wei2a38e6d2011-11-27 21:33:34 +0000602 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 retv = 0;
604 break;
605
606 case IPV6_MULTICAST_LOOP:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900607 if (optlen < sizeof(int))
608 goto e_inval;
YOSHIFUJI Hideaki28d44882008-06-12 03:14:51 +0900609 if (val != valbool)
610 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 np->mc_loop = valbool;
612 retv = 0;
613 break;
614
Erich E. Hooverc4062df2012-02-08 09:11:08 +0000615 case IPV6_UNICAST_IF:
616 {
617 struct net_device *dev = NULL;
618 int ifindex;
619
620 if (optlen != sizeof(int))
621 goto e_inval;
622
623 ifindex = (__force int)ntohl((__force __be32)val);
624 if (ifindex == 0) {
625 np->ucast_oif = 0;
626 retv = 0;
627 break;
628 }
629
630 dev = dev_get_by_index(net, ifindex);
631 retv = -EADDRNOTAVAIL;
632 if (!dev)
633 break;
634 dev_put(dev);
635
636 retv = -EINVAL;
637 if (sk->sk_bound_dev_if)
638 break;
639
640 np->ucast_oif = ifindex;
641 retv = 0;
642 break;
643 }
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 case IPV6_MULTICAST_IF:
646 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900647 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900648 if (optlen < sizeof(int))
649 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Brian Haley4953f0f2007-10-11 14:39:29 -0700651 if (val) {
Eric Dumazet55b80502009-10-19 06:41:58 +0000652 struct net_device *dev;
David Ahern7bb387c2016-12-29 15:39:37 -0800653 int midx;
Eric Dumazet55b80502009-10-19 06:41:58 +0000654
David Ahern7bb387c2016-12-29 15:39:37 -0800655 rcu_read_lock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700656
David Ahern7bb387c2016-12-29 15:39:37 -0800657 dev = dev_get_by_index_rcu(net, val);
Eric Dumazet55b80502009-10-19 06:41:58 +0000658 if (!dev) {
David Ahern7bb387c2016-12-29 15:39:37 -0800659 rcu_read_unlock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700660 retv = -ENODEV;
661 break;
662 }
David Ahern7bb387c2016-12-29 15:39:37 -0800663 midx = l3mdev_master_ifindex_rcu(dev);
664
665 rcu_read_unlock();
666
667 if (sk->sk_bound_dev_if &&
668 sk->sk_bound_dev_if != val &&
669 (!midx || midx != sk->sk_bound_dev_if))
670 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672 np->mcast_oif = val;
673 retv = 0;
674 break;
675 case IPV6_ADD_MEMBERSHIP:
676 case IPV6_DROP_MEMBERSHIP:
677 {
678 struct ipv6_mreq mreq;
679
Wang Chena28398b2008-04-07 09:42:07 +0800680 if (optlen < sizeof(struct ipv6_mreq))
681 goto e_inval;
682
Flavio Leitnera96fb492007-08-24 22:16:39 -0700683 retv = -EPROTO;
684 if (inet_sk(sk)->is_icsk)
685 break;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 retv = -EFAULT;
688 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
689 break;
690
691 if (optname == IPV6_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300692 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300694 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 break;
696 }
697 case IPV6_JOIN_ANYCAST:
698 case IPV6_LEAVE_ANYCAST:
699 {
700 struct ipv6_mreq mreq;
701
Wang Chena28398b2008-04-07 09:42:07 +0800702 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 goto e_inval;
704
705 retv = -EFAULT;
706 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
707 break;
708
709 if (optname == IPV6_JOIN_ANYCAST)
710 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
711 else
712 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
713 break;
714 }
Andre Naujoks15033f02018-09-10 10:27:15 +0200715 case IPV6_MULTICAST_ALL:
716 if (optlen < sizeof(int))
717 goto e_inval;
718 np->mc_all = valbool;
719 retv = 0;
720 break;
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 case MCAST_JOIN_GROUP:
723 case MCAST_LEAVE_GROUP:
724 {
725 struct group_req greq;
726 struct sockaddr_in6 *psin6;
727
Wang Chena28398b2008-04-07 09:42:07 +0800728 if (optlen < sizeof(struct group_req))
729 goto e_inval;
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 retv = -EFAULT;
732 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
733 break;
734 if (greq.gr_group.ss_family != AF_INET6) {
735 retv = -EADDRNOTAVAIL;
736 break;
737 }
738 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
739 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300740 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
741 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300743 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
744 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 break;
746 }
747 case MCAST_JOIN_SOURCE_GROUP:
748 case MCAST_LEAVE_SOURCE_GROUP:
749 case MCAST_BLOCK_SOURCE:
750 case MCAST_UNBLOCK_SOURCE:
751 {
752 struct group_source_req greqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Wang Chena28398b2008-04-07 09:42:07 +0800754 if (optlen < sizeof(struct group_source_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 goto e_inval;
756 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
757 retv = -EFAULT;
758 break;
759 }
Al Virofcfa0b02020-04-27 11:00:01 -0400760 retv = do_ipv6_mcast_group_source(sk, optname, &greqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 break;
762 }
763 case MCAST_MSFILTER:
764 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 struct group_filter *gsf;
766
767 if (optlen < GROUP_FILTER_SIZE(0))
768 goto e_inval;
769 if (optlen > sysctl_optmem_max) {
770 retv = -ENOBUFS;
771 break;
772 }
Al Viro43727da2017-05-13 18:22:53 -0400773 gsf = memdup_user(optval, optlen);
774 if (IS_ERR(gsf)) {
775 retv = PTR_ERR(gsf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 break;
777 }
778 /* numsrc >= (4G-140)/128 overflow in 32 bits */
779 if (gsf->gf_numsrc >= 0x1ffffffU ||
780 gsf->gf_numsrc > sysctl_mld_max_msf) {
781 kfree(gsf);
782 retv = -ENOBUFS;
783 break;
784 }
785 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
786 kfree(gsf);
787 retv = -EINVAL;
788 break;
789 }
Al Virod59eb172020-03-30 15:43:10 -0400790 retv = ip6_mc_msfilter(sk, gsf, gsf->gf_slist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 kfree(gsf);
792
793 break;
794 }
795 case IPV6_ROUTER_ALERT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900796 if (optlen < sizeof(int))
797 goto e_inval;
Denis V. Lunev725a8ff2008-07-19 00:28:58 -0700798 retv = ip6_ra_control(sk, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 break;
Francesco Ruggeri9036b2f2019-03-01 15:31:03 -0800800 case IPV6_ROUTER_ALERT_ISOLATE:
801 if (optlen < sizeof(int))
802 goto e_inval;
803 np->rtalert_isolate = valbool;
804 retv = 0;
805 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 case IPV6_MTU_DISCOVER:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900807 if (optlen < sizeof(int))
808 goto e_inval;
Hannes Frederic Sowa0b952272014-02-26 01:20:43 +0100809 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 goto e_inval;
811 np->pmtudisc = val;
812 retv = 0;
813 break;
814 case IPV6_MTU:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900815 if (optlen < sizeof(int))
816 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (val && val < IPV6_MIN_MTU)
818 goto e_inval;
819 np->frag_size = val;
820 retv = 0;
821 break;
822 case IPV6_RECVERR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900823 if (optlen < sizeof(int))
824 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 np->recverr = valbool;
826 if (!val)
827 skb_queue_purge(&sk->sk_error_queue);
828 retv = 0;
829 break;
830 case IPV6_FLOWINFO_SEND:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900831 if (optlen < sizeof(int))
832 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 np->sndflow = valbool;
834 retv = 0;
835 break;
836 case IPV6_FLOWLABEL_MGR:
837 retv = ipv6_flowlabel_opt(sk, optval, optlen);
838 break;
839 case IPV6_IPSEC_POLICY:
840 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700841 retv = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000842 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700843 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 retv = xfrm_user_policy(sk, optname, optval, optlen);
845 break;
846
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900847 case IPV6_ADDR_PREFERENCES:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900848 if (optlen < sizeof(int))
849 goto e_inval;
Christoph Hellwig18d5ad62020-05-28 07:12:33 +0200850 retv = __ip6_sock_set_addr_preferences(sk, val);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900851 break;
Stephen Hemmingere802af92010-04-22 15:24:53 -0700852 case IPV6_MINHOPCOUNT:
853 if (optlen < sizeof(int))
854 goto e_inval;
855 if (val < 0 || val > 255)
856 goto e_inval;
857 np->min_hopcount = val;
Hannes Frederic Sowad4596ba2012-11-10 19:52:34 +0000858 retv = 0;
Brian Haley793b14732010-04-23 11:26:07 +0000859 break;
860 case IPV6_DONTFRAG:
861 np->dontfrag = valbool;
Stephen Hemmingere802af92010-04-22 15:24:53 -0700862 retv = 0;
863 break;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700864 case IPV6_AUTOFLOWLABEL:
865 np->autoflowlabel = valbool;
Shaohua Li513674b2017-12-20 12:10:21 -0800866 np->autoflowlabel_set = 1;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700867 retv = 0;
868 break;
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -0400869 case IPV6_RECVFRAGSIZE:
870 np->rxopt.bits.recvfragsize = valbool;
871 retv = 0;
872 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300876 if (needs_rtnl)
877 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return retv;
880
881e_inval:
882 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300883 if (needs_rtnl)
884 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return -EINVAL;
886}
887
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800888int ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700889 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800890{
891 int err;
892
893 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
894 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
895
896 if (level != SOL_IPV6)
897 return -ENOPROTOOPT;
898
899 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
900#ifdef CONFIG_NETFILTER
901 /* we need to exclude all possible ENOPROTOOPTs except default case */
902 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +0100903 optname != IPV6_XFRM_POLICY)
904 err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800905#endif
906 return err;
907}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900908EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800909
910#ifdef CONFIG_COMPAT
911int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700912 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800913{
914 int err;
915
916 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800917 if (udp_prot.compat_setsockopt != NULL)
918 return udp_prot.compat_setsockopt(sk, level, optname,
919 optval, optlen);
920 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800921 }
922
923 if (level != SOL_IPV6)
924 return -ENOPROTOOPT;
925
Al Viroe9c375f2020-05-09 21:16:36 -0400926 switch (optname) {
927 case MCAST_JOIN_GROUP:
928 case MCAST_LEAVE_GROUP:
Al Viro2f984f12020-04-26 19:56:22 -0400929 {
930 struct compat_group_req __user *gr32 = (void __user *)optval;
931 struct group_req greq;
932 struct sockaddr_in6 *psin6 = (struct sockaddr_in6 *)&greq.gr_group;
933
934 if (optlen < sizeof(struct compat_group_req))
935 return -EINVAL;
936
937 if (get_user(greq.gr_interface, &gr32->gr_interface) ||
938 copy_from_user(&greq.gr_group, &gr32->gr_group,
939 sizeof(greq.gr_group)))
940 return -EFAULT;
941
942 if (greq.gr_group.ss_family != AF_INET6)
943 return -EADDRNOTAVAIL;
944
945 rtnl_lock();
946 lock_sock(sk);
947 if (optname == MCAST_JOIN_GROUP)
948 err = ipv6_sock_mc_join(sk, greq.gr_interface,
949 &psin6->sin6_addr);
950 else
951 err = ipv6_sock_mc_drop(sk, greq.gr_interface,
952 &psin6->sin6_addr);
953 release_sock(sk);
954 rtnl_unlock();
955 return err;
956 }
Al Viroe9c375f2020-05-09 21:16:36 -0400957 case MCAST_JOIN_SOURCE_GROUP:
958 case MCAST_LEAVE_SOURCE_GROUP:
959 case MCAST_BLOCK_SOURCE:
960 case MCAST_UNBLOCK_SOURCE:
Al Virob212c322020-04-27 11:37:02 -0400961 {
962 struct compat_group_source_req __user *gsr32 = (void __user *)optval;
963 struct group_source_req greqs;
964
965 if (optlen < sizeof(struct compat_group_source_req))
966 return -EINVAL;
967
968 if (get_user(greqs.gsr_interface, &gsr32->gsr_interface) ||
969 copy_from_user(&greqs.gsr_group, &gsr32->gsr_group,
970 sizeof(greqs.gsr_group)) ||
971 copy_from_user(&greqs.gsr_source, &gsr32->gsr_source,
972 sizeof(greqs.gsr_source)))
973 return -EFAULT;
974
975 rtnl_lock();
976 lock_sock(sk);
977 err = do_ipv6_mcast_group_source(sk, optname, &greqs);
978 release_sock(sk);
979 rtnl_unlock();
980 return err;
981 }
Al Viro168a2cc2020-03-30 16:40:04 -0400982 case MCAST_MSFILTER:
983 {
984 const int size0 = offsetof(struct compat_group_filter, gf_slist);
985 struct compat_group_filter *gf32;
986 void *p;
987 int n;
988
989 if (optlen < size0)
990 return -EINVAL;
991 if (optlen > sysctl_optmem_max - 4)
992 return -ENOBUFS;
993
994 p = kmalloc(optlen + 4, GFP_KERNEL);
995 if (!p)
996 return -ENOMEM;
997
998 gf32 = p + 4; /* we want ->gf_group and ->gf_slist aligned */
999 if (copy_from_user(gf32, optval, optlen)) {
1000 err = -EFAULT;
1001 goto mc_msf_out;
1002 }
1003
1004 n = gf32->gf_numsrc;
1005 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1006 if (n >= 0x1ffffffU ||
1007 n > sysctl_mld_max_msf) {
1008 err = -ENOBUFS;
1009 goto mc_msf_out;
1010 }
1011 if (offsetof(struct compat_group_filter, gf_slist[n]) > optlen) {
1012 err = -EINVAL;
1013 goto mc_msf_out;
1014 }
1015
1016 rtnl_lock();
1017 lock_sock(sk);
1018 err = ip6_mc_msfilter(sk, &(struct group_filter){
1019 .gf_interface = gf32->gf_interface,
1020 .gf_group = gf32->gf_group,
1021 .gf_fmode = gf32->gf_fmode,
1022 .gf_numsrc = gf32->gf_numsrc}, gf32->gf_slist);
1023 release_sock(sk);
1024 rtnl_unlock();
1025mc_msf_out:
1026 kfree(p);
1027 return err;
1028 }
Al Viroe9c375f2020-05-09 21:16:36 -04001029 }
David L Stevensdae50292008-04-27 01:06:07 -07001030
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001031 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
1032#ifdef CONFIG_NETFILTER
1033 /* we need to exclude all possible ENOPROTOOPTs except default case */
1034 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +01001035 optname != IPV6_XFRM_POLICY)
1036 err = compat_nf_setsockopt(sk, PF_INET6, optname, optval,
1037 optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001038#endif
1039 return err;
1040}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001041EXPORT_SYMBOL(compat_ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001042#endif
1043
David S. Miller286930792007-03-07 12:50:46 -08001044static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001045 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001046{
David S. Miller286930792007-03-07 12:50:46 -08001047 struct ipv6_opt_hdr *hdr;
1048
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001049 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001050 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001051
Ian Morris67ba4152014-08-24 21:53:10 +01001052 switch (optname) {
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001053 case IPV6_HOPOPTS:
1054 hdr = opt->hopopt;
1055 break;
1056 case IPV6_RTHDRDSTOPTS:
1057 hdr = opt->dst0opt;
1058 break;
1059 case IPV6_RTHDR:
1060 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
1061 break;
1062 case IPV6_DSTOPTS:
1063 hdr = opt->dst1opt;
1064 break;
1065 default:
1066 return -EINVAL; /* should not happen */
1067 }
1068
1069 if (!hdr)
1070 return 0;
David S. Miller286930792007-03-07 12:50:46 -08001071
Chris Wrightd2b02ed2007-03-09 16:19:17 -08001072 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -07001073 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001074 return -EFAULT;
Yang Hongyang95b496b2008-05-28 16:27:28 +08001075 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001076}
1077
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001078static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001079 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 struct ipv6_pinfo *np = inet6_sk(sk);
1082 int len;
1083 int val;
1084
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001085 if (ip6_mroute_opt(optname))
1086 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (get_user(len, optlen))
1089 return -EFAULT;
1090 switch (optname) {
1091 case IPV6_ADDRFORM:
1092 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001093 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 sk->sk_protocol != IPPROTO_TCP)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +09001095 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (sk->sk_state != TCP_ESTABLISHED)
1097 return -ENOTCONN;
1098 val = sk->sk_family;
1099 break;
1100 case MCAST_MSFILTER:
1101 {
Al Viro931ca7a2020-03-29 17:18:30 -04001102 struct group_filter __user *p = (void __user *)optval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 struct group_filter gsf;
Al Viro931ca7a2020-03-29 17:18:30 -04001104 const int size0 = offsetof(struct group_filter, gf_slist);
1105 int num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 int err;
1107
Al Viro931ca7a2020-03-29 17:18:30 -04001108 if (len < size0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return -EINVAL;
Al Viro931ca7a2020-03-29 17:18:30 -04001110 if (copy_from_user(&gsf, p, size0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return -EFAULT;
YOSHIFUJI Hideaki20c61fbd2008-04-28 14:40:55 +09001112 if (gsf.gf_group.ss_family != AF_INET6)
1113 return -EADDRNOTAVAIL;
Al Viro931ca7a2020-03-29 17:18:30 -04001114 num = gsf.gf_numsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 lock_sock(sk);
Al Viro931ca7a2020-03-29 17:18:30 -04001116 err = ip6_mc_msfget(sk, &gsf, p->gf_slist);
1117 if (!err) {
1118 if (num > gsf.gf_numsrc)
1119 num = gsf.gf_numsrc;
1120 if (put_user(GROUP_FILTER_SIZE(num), optlen) ||
1121 copy_to_user(p, &gsf, size0))
1122 err = -EFAULT;
1123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 release_sock(sk);
1125 return err;
1126 }
1127
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001128 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 {
1130 struct msghdr msg;
1131 struct sk_buff *skb;
1132
1133 if (sk->sk_type != SOCK_STREAM)
1134 return -ENOPROTOOPT;
1135
1136 msg.msg_control = optval;
1137 msg.msg_controllen = len;
Daniel Baluta98e77432011-08-19 03:19:07 -07001138 msg.msg_flags = flags;
Christoph Hellwig1b2f08d2020-05-13 21:36:41 +02001139 msg.msg_control_is_user = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 lock_sock(sk);
1142 skb = np->pktoptions;
1143 if (skb)
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001144 ip6_datagram_recv_ctl(sk, &msg, skb);
Eric Dumazet1dc7b902015-01-21 03:45:42 -08001145 release_sock(sk);
1146 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (np->rxopt.bits.rxinfo) {
1148 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001149 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1150 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001151 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1153 }
1154 if (np->rxopt.bits.rxhlim) {
1155 int hlim = np->mcast_hops;
1156 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1157 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001158 if (np->rxopt.bits.rxtclass) {
Li RongQingd76ed222014-01-15 17:03:30 +08001159 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1160
Jiri Benc4c507d22012-02-09 09:35:49 +00001161 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1162 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001163 if (np->rxopt.bits.rxoinfo) {
1164 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001165 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1166 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001167 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1168 np->sticky_pktinfo.ipi6_addr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001169 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1170 }
1171 if (np->rxopt.bits.rxohlim) {
1172 int hlim = np->mcast_hops;
1173 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1174 }
Florent Fourcot1397ed32013-12-08 15:46:57 +01001175 if (np->rxopt.bits.rxflow) {
Florent Fourcot68536052013-12-12 17:07:58 +01001176 __be32 flowinfo = np->rcv_flowinfo;
1177
Florent Fourcot1397ed32013-12-08 15:46:57 +01001178 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181 len -= msg.msg_controllen;
1182 return put_user(len, optlen);
1183 }
1184 case IPV6_MTU:
1185 {
1186 struct dst_entry *dst;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001187
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001188 val = 0;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001189 rcu_read_lock();
1190 dst = __sk_dst_get(sk);
1191 if (dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 val = dst_mtu(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001193 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (!val)
1195 return -ENOTCONN;
1196 break;
1197 }
1198
1199 case IPV6_V6ONLY:
Eric Dumazet9fe516b2014-06-27 08:36:16 -07001200 val = sk->sk_ipv6only;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 break;
1202
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001203 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 val = np->rxopt.bits.rxinfo;
1205 break;
1206
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001207 case IPV6_2292PKTINFO:
1208 val = np->rxopt.bits.rxoinfo;
1209 break;
1210
1211 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 val = np->rxopt.bits.rxhlim;
1213 break;
1214
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001215 case IPV6_2292HOPLIMIT:
1216 val = np->rxopt.bits.rxohlim;
1217 break;
1218
1219 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 val = np->rxopt.bits.srcrt;
1221 break;
1222
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001223 case IPV6_2292RTHDR:
1224 val = np->rxopt.bits.osrcrt;
1225 break;
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001228 case IPV6_RTHDRDSTOPTS:
1229 case IPV6_RTHDR:
1230 case IPV6_DSTOPTS:
1231 {
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001232 struct ipv6_txoptions *opt;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001233
1234 lock_sock(sk);
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001235 opt = rcu_dereference_protected(np->opt,
1236 lockdep_sock_is_held(sk));
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001237 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001238 release_sock(sk);
Yang Hongyang05335c22008-05-28 16:23:47 +08001239 /* check if ipv6_getsockopt_sticky() returns err code */
1240 if (len < 0)
1241 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001242 return put_user(len, optlen);
1243 }
1244
1245 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 val = np->rxopt.bits.hopopts;
1247 break;
1248
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001249 case IPV6_2292HOPOPTS:
1250 val = np->rxopt.bits.ohopopts;
1251 break;
1252
1253 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 val = np->rxopt.bits.dstopts;
1255 break;
1256
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001257 case IPV6_2292DSTOPTS:
1258 val = np->rxopt.bits.odstopts;
1259 break;
1260
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001261 case IPV6_TCLASS:
1262 val = np->tclass;
1263 break;
1264
1265 case IPV6_RECVTCLASS:
1266 val = np->rxopt.bits.rxtclass;
1267 break;
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 case IPV6_FLOWINFO:
1270 val = np->rxopt.bits.rxflow;
1271 break;
1272
Brian Haley793b14732010-04-23 11:26:07 +00001273 case IPV6_RECVPATHMTU:
1274 val = np->rxopt.bits.rxpmtu;
1275 break;
1276
1277 case IPV6_PATHMTU:
1278 {
1279 struct dst_entry *dst;
1280 struct ip6_mtuinfo mtuinfo;
1281
1282 if (len < sizeof(mtuinfo))
1283 return -EINVAL;
1284
1285 len = sizeof(mtuinfo);
1286 memset(&mtuinfo, 0, sizeof(mtuinfo));
1287
1288 rcu_read_lock();
1289 dst = __sk_dst_get(sk);
1290 if (dst)
1291 mtuinfo.ip6m_mtu = dst_mtu(dst);
1292 rcu_read_unlock();
1293 if (!mtuinfo.ip6m_mtu)
1294 return -ENOTCONN;
1295
1296 if (put_user(len, optlen))
1297 return -EFAULT;
1298 if (copy_to_user(optval, &mtuinfo, len))
1299 return -EFAULT;
1300
1301 return 0;
Brian Haley793b14732010-04-23 11:26:07 +00001302 }
1303
Balazs Scheidler6c468622010-10-21 16:08:28 +02001304 case IPV6_TRANSPARENT:
1305 val = inet_sk(sk)->transparent;
1306 break;
1307
Maciej Żenczykowski84e14fe2017-09-26 21:32:42 -07001308 case IPV6_FREEBIND:
1309 val = inet_sk(sk)->freebind;
1310 break;
1311
Balazs Scheidler6c468622010-10-21 16:08:28 +02001312 case IPV6_RECVORIGDSTADDR:
1313 val = np->rxopt.bits.rxorigdstaddr;
1314 break;
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -08001318 {
1319 struct dst_entry *dst;
1320
1321 if (optname == IPV6_UNICAST_HOPS)
1322 val = np->hop_limit;
1323 else
1324 val = np->mcast_hops;
1325
Eric Dumazetb6c67122010-04-08 23:03:29 +00001326 if (val < 0) {
1327 rcu_read_lock();
1328 dst = __sk_dst_get(sk);
1329 if (dst)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001330 val = ip6_dst_hoplimit(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001331 rcu_read_unlock();
Brian Haleybefffe92006-12-12 17:09:49 -08001332 }
Eric Dumazetb6c67122010-04-08 23:03:29 +00001333
Brian Haleybefffe92006-12-12 17:09:49 -08001334 if (val < 0)
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001335 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 break;
Brian Haleybefffe92006-12-12 17:09:49 -08001337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 case IPV6_MULTICAST_LOOP:
1340 val = np->mc_loop;
1341 break;
1342
1343 case IPV6_MULTICAST_IF:
1344 val = np->mcast_oif;
1345 break;
1346
Andre Naujoks15033f02018-09-10 10:27:15 +02001347 case IPV6_MULTICAST_ALL:
1348 val = np->mc_all;
1349 break;
1350
Erich E. Hooverc4062df2012-02-08 09:11:08 +00001351 case IPV6_UNICAST_IF:
1352 val = (__force int)htonl((__u32) np->ucast_oif);
1353 break;
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 case IPV6_MTU_DISCOVER:
1356 val = np->pmtudisc;
1357 break;
1358
1359 case IPV6_RECVERR:
1360 val = np->recverr;
1361 break;
1362
1363 case IPV6_FLOWINFO_SEND:
1364 val = np->sndflow;
1365 break;
1366
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001367 case IPV6_FLOWLABEL_MGR:
1368 {
1369 struct in6_flowlabel_req freq;
Florent Fourcot46e5f402014-01-17 17:15:04 +01001370 int flags;
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001371
1372 if (len < sizeof(freq))
1373 return -EINVAL;
1374
1375 if (copy_from_user(&freq, optval, sizeof(freq)))
1376 return -EFAULT;
1377
1378 if (freq.flr_action != IPV6_FL_A_GET)
1379 return -EINVAL;
1380
1381 len = sizeof(freq);
Florent Fourcot46e5f402014-01-17 17:15:04 +01001382 flags = freq.flr_flags;
1383
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001384 memset(&freq, 0, sizeof(freq));
1385
Florent Fourcot46e5f402014-01-17 17:15:04 +01001386 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001387 if (val < 0)
1388 return val;
1389
1390 if (put_user(len, optlen))
1391 return -EFAULT;
1392 if (copy_to_user(optval, &freq, len))
1393 return -EFAULT;
1394
1395 return 0;
1396 }
1397
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001398 case IPV6_ADDR_PREFERENCES:
1399 val = 0;
1400
1401 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1402 val |= IPV6_PREFER_SRC_TMP;
1403 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1404 val |= IPV6_PREFER_SRC_PUBLIC;
1405 else {
1406 /* XXX: should we return system default? */
1407 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1408 }
1409
1410 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1411 val |= IPV6_PREFER_SRC_COA;
1412 else
1413 val |= IPV6_PREFER_SRC_HOME;
1414 break;
1415
Stephen Hemmingere802af92010-04-22 15:24:53 -07001416 case IPV6_MINHOPCOUNT:
1417 val = np->min_hopcount;
1418 break;
1419
Brian Haley793b14732010-04-23 11:26:07 +00001420 case IPV6_DONTFRAG:
1421 val = np->dontfrag;
1422 break;
1423
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001424 case IPV6_AUTOFLOWLABEL:
Ben Hutchingse9191ff2018-01-22 20:06:42 +00001425 val = ip6_autoflowlabel(sock_net(sk), np);
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001426 break;
1427
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -04001428 case IPV6_RECVFRAGSIZE:
1429 val = np->rxopt.bits.recvfragsize;
1430 break;
1431
Francesco Ruggeri9036b2f2019-03-01 15:31:03 -08001432 case IPV6_ROUTER_ALERT_ISOLATE:
1433 val = np->rtalert_isolate;
1434 break;
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001437 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439 len = min_t(unsigned int, sizeof(int), len);
Ian Morris67ba4152014-08-24 21:53:10 +01001440 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001442 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return -EFAULT;
1444 return 0;
1445}
1446
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001447int ipv6_getsockopt(struct sock *sk, int level, int optname,
1448 char __user *optval, int __user *optlen)
1449{
1450 int err;
1451
1452 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1453 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1454
Ian Morris67ba4152014-08-24 21:53:10 +01001455 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001456 return -ENOPROTOOPT;
1457
Daniel Baluta98e77432011-08-19 03:19:07 -07001458 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001459#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001460 /* we need to exclude all possible ENOPROTOOPTs except default case */
1461 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001462 int len;
1463
1464 if (get_user(len, optlen))
1465 return -EFAULT;
1466
Paolo Abeni01ea3062018-02-08 12:19:00 +01001467 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001468 if (err >= 0)
1469 err = put_user(len, optlen);
1470 }
1471#endif
1472 return err;
1473}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001474EXPORT_SYMBOL(ipv6_getsockopt);
1475
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001476#ifdef CONFIG_COMPAT
1477int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001478 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001479{
1480 int err;
1481
1482 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001483 if (udp_prot.compat_getsockopt != NULL)
1484 return udp_prot.compat_getsockopt(sk, level, optname,
1485 optval, optlen);
1486 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001487 }
1488
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001489 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001490 return -ENOPROTOOPT;
1491
Al Viro0dfe6582020-03-29 22:08:59 -04001492 if (optname == MCAST_MSFILTER) {
1493 const int size0 = offsetof(struct compat_group_filter, gf_slist);
1494 struct compat_group_filter __user *p = (void __user *)optval;
1495 struct compat_group_filter gf32;
1496 struct group_filter gf;
1497 int ulen, err;
1498 int num;
1499
1500 if (get_user(ulen, optlen))
1501 return -EFAULT;
1502
1503 if (ulen < size0)
1504 return -EINVAL;
1505
1506 if (copy_from_user(&gf32, p, size0))
1507 return -EFAULT;
1508
1509 gf.gf_interface = gf32.gf_interface;
1510 gf.gf_fmode = gf32.gf_fmode;
1511 num = gf.gf_numsrc = gf32.gf_numsrc;
1512 gf.gf_group = gf32.gf_group;
1513
1514 if (gf.gf_group.ss_family != AF_INET6)
1515 return -EADDRNOTAVAIL;
1516 lock_sock(sk);
1517 err = ip6_mc_msfget(sk, &gf, p->gf_slist);
1518 release_sock(sk);
1519 if (err)
1520 return err;
1521 if (num > gf.gf_numsrc)
1522 num = gf.gf_numsrc;
1523 ulen = GROUP_FILTER_SIZE(num) - (sizeof(gf)-sizeof(gf32));
1524 if (put_user(ulen, optlen) ||
1525 put_user(gf.gf_fmode, &p->gf_fmode) ||
1526 put_user(gf.gf_numsrc, &p->gf_numsrc))
1527 return -EFAULT;
1528 return 0;
1529 }
Pavel Emelyanov80991792008-04-30 14:49:54 -07001530
Daniel Baluta98e77432011-08-19 03:19:07 -07001531 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1532 MSG_CMSG_COMPAT);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001533#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001534 /* we need to exclude all possible ENOPROTOOPTs except default case */
1535 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001536 int len;
1537
1538 if (get_user(len, optlen))
1539 return -EFAULT;
1540
Paolo Abeni01ea3062018-02-08 12:19:00 +01001541 err = compat_nf_getsockopt(sk, PF_INET6, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001542 if (err >= 0)
1543 err = put_user(len, optlen);
1544 }
1545#endif
1546 return err;
1547}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001548EXPORT_SYMBOL(compat_ipv6_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001549#endif