blob: 96e3f603c8d86429b22e054e040fd21e670260d9 [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
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800139static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700140 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900143 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int val, valbool;
145 int retv = -ENOPROTOOPT;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300146 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Ian Morris63159f22015-03-29 14:00:04 +0100148 if (!optval)
Ian Morris67ba4152014-08-24 21:53:10 +0100149 val = 0;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900150 else {
151 if (optlen >= sizeof(int)) {
152 if (get_user(val, (int __user *) optval))
153 return -EFAULT;
154 } else
155 val = 0;
156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Ian Morris67ba4152014-08-24 21:53:10 +0100158 valbool = (val != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900160 if (ip6_mroute_opt(optname))
161 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
162
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300163 if (needs_rtnl)
164 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 lock_sock(sk);
166
167 switch (optname) {
168
169 case IPV6_ADDRFORM:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900170 if (optlen < sizeof(int))
171 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (val == PF_INET) {
173 struct ipv6_txoptions *opt;
174 struct sk_buff *pktopt;
175
Denis V. Lunev49d074f2008-06-04 15:49:06 +0400176 if (sk->sk_type == SOCK_RAW)
177 break;
178
Denis V. Lunev9596cc822008-06-04 15:49:08 +0400179 if (sk->sk_protocol == IPPROTO_UDP ||
180 sk->sk_protocol == IPPROTO_UDPLITE) {
181 struct udp_sock *up = udp_sk(sk);
182 if (up->pending == AF_INET6) {
183 retv = -EBUSY;
184 break;
185 }
John Haxby82c9ae42020-04-18 16:30:49 +0100186 }
187 if (sk->sk_protocol == IPPROTO_TCP &&
188 sk->sk_prot != &tcpv6_prot) {
189 retv = -EBUSY;
Eric Dumazetb6f61182020-02-25 11:52:29 -0800190 break;
191 }
John Haxby82c9ae42020-04-18 16:30:49 +0100192 if (sk->sk_protocol != IPPROTO_TCP)
193 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (sk->sk_state != TCP_ESTABLISHED) {
195 retv = -ENOTCONN;
196 break;
197 }
198
199 if (ipv6_only_sock(sk) ||
Eric Dumazetefe42082013-10-03 15:42:29 -0700200 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 retv = -EADDRNOTAVAIL;
202 break;
203 }
204
205 fl6_free_socklist(sk);
WANG Cong8651be82016-10-19 23:35:12 -0700206 __ipv6_sock_mc_close(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700208 /*
209 * Sock is moving from IPv6 to IPv4 (sk_prot), so
210 * remove it from the refcnt debug socks count in the
211 * original family...
212 */
213 sk_refcnt_debug_dec(sk);
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (sk->sk_protocol == IPPROTO_TCP) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800216 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700218 sock_prot_inuse_add(net, sk->sk_prot, -1);
219 sock_prot_inuse_add(net, &tcp_prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 local_bh_enable();
221 sk->sk_prot = &tcp_prot;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800222 icsk->icsk_af_ops = &ipv4_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 sk->sk_socket->ops = &inet_stream_ops;
224 sk->sk_family = PF_INET;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800225 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 } else {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800227 struct proto *prot = &udp_prot;
228
David S. Millerdb8dac22008-03-06 16:22:02 -0800229 if (sk->sk_protocol == IPPROTO_UDPLITE)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800230 prot = &udplite_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700232 sock_prot_inuse_add(net, sk->sk_prot, -1);
233 sock_prot_inuse_add(net, prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 local_bh_enable();
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800235 sk->sk_prot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 sk->sk_socket->ops = &inet_dgram_ops;
237 sk->sk_family = PF_INET;
238 }
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800239 opt = xchg((__force struct ipv6_txoptions **)&np->opt,
240 NULL);
241 if (opt) {
242 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
243 txopt_put(opt);
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 pktopt = xchg(&np->pktoptions, NULL);
Wei Yongjun800d55f2009-02-23 21:45:33 +0000246 kfree_skb(pktopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700248 /*
249 * ... and add it to the refcnt debug socks count
250 * in the new family. -acme
251 */
252 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 module_put(THIS_MODULE);
254 retv = 0;
255 break;
256 }
257 goto e_inval;
258
259 case IPV6_V6ONLY:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900260 if (optlen < sizeof(int) ||
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000261 inet_sk(sk)->inet_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 goto e_inval;
Eric Dumazet9fe516b2014-06-27 08:36:16 -0700263 sk->sk_ipv6only = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 retv = 0;
265 break;
266
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900267 case IPV6_RECVPKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900268 if (optlen < sizeof(int))
269 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 np->rxopt.bits.rxinfo = valbool;
271 retv = 0;
272 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900273
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900274 case IPV6_2292PKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900275 if (optlen < sizeof(int))
276 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900277 np->rxopt.bits.rxoinfo = valbool;
278 retv = 0;
279 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900281 case IPV6_RECVHOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900282 if (optlen < sizeof(int))
283 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 np->rxopt.bits.rxhlim = valbool;
285 retv = 0;
286 break;
287
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900288 case IPV6_2292HOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900289 if (optlen < sizeof(int))
290 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900291 np->rxopt.bits.rxohlim = valbool;
292 retv = 0;
293 break;
294
295 case IPV6_RECVRTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900296 if (optlen < sizeof(int))
297 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900298 np->rxopt.bits.srcrt = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 retv = 0;
300 break;
301
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900302 case IPV6_2292RTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900303 if (optlen < sizeof(int))
304 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900305 np->rxopt.bits.osrcrt = valbool;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900306 retv = 0;
307 break;
308
309 case IPV6_RECVHOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900310 if (optlen < sizeof(int))
311 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 np->rxopt.bits.hopopts = valbool;
313 retv = 0;
314 break;
315
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900316 case IPV6_2292HOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900317 if (optlen < sizeof(int))
318 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900319 np->rxopt.bits.ohopopts = valbool;
320 retv = 0;
321 break;
322
323 case IPV6_RECVDSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900324 if (optlen < sizeof(int))
325 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 np->rxopt.bits.dstopts = valbool;
327 retv = 0;
328 break;
329
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900330 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900331 if (optlen < sizeof(int))
332 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900333 np->rxopt.bits.odstopts = valbool;
334 retv = 0;
335 break;
336
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900337 case IPV6_TCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900338 if (optlen < sizeof(int))
339 goto e_inval;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700340 if (val < -1 || val > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900341 goto e_inval;
Gerrit Renker26ced1e2009-08-09 08:12:49 +0000342 /* RFC 3542, 6.5: default traffic class of 0x0 */
343 if (val == -1)
344 val = 0;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900345 np->tclass = val;
346 retv = 0;
347 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900348
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900349 case IPV6_RECVTCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900350 if (optlen < sizeof(int))
351 goto e_inval;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900352 np->rxopt.bits.rxtclass = valbool;
353 retv = 0;
354 break;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 case IPV6_FLOWINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900357 if (optlen < sizeof(int))
358 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 np->rxopt.bits.rxflow = valbool;
360 retv = 0;
361 break;
362
Brian Haley793b14732010-04-23 11:26:07 +0000363 case IPV6_RECVPATHMTU:
364 if (optlen < sizeof(int))
365 goto e_inval;
366 np->rxopt.bits.rxpmtu = valbool;
367 retv = 0;
368 break;
369
Balazs Scheidler6c468622010-10-21 16:08:28 +0200370 case IPV6_TRANSPARENT:
Maciej Żenczykowski35fc59c2019-11-21 13:19:08 -0800371 if (valbool && !ns_capable(net->user_ns, CAP_NET_RAW) &&
372 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
Balazs Scheidlerb8894162010-10-23 04:48:14 +0000373 retv = -EPERM;
374 break;
375 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200376 if (optlen < sizeof(int))
377 goto e_inval;
378 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
379 inet_sk(sk)->transparent = valbool;
380 retv = 0;
381 break;
382
Maciej Żenczykowski84e14fe2017-09-26 21:32:42 -0700383 case IPV6_FREEBIND:
384 if (optlen < sizeof(int))
385 goto e_inval;
386 /* we also don't have a separate freebind bit for IPV6 */
387 inet_sk(sk)->freebind = valbool;
388 retv = 0;
389 break;
390
Balazs Scheidler6c468622010-10-21 16:08:28 +0200391 case IPV6_RECVORIGDSTADDR:
392 if (optlen < sizeof(int))
393 goto e_inval;
394 np->rxopt.bits.rxorigdstaddr = valbool;
395 retv = 0;
396 break;
397
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900398 case IPV6_HOPOPTS:
399 case IPV6_RTHDRDSTOPTS:
400 case IPV6_RTHDR:
401 case IPV6_DSTOPTS:
402 {
403 struct ipv6_txoptions *opt;
Paul Moorea9ba23d2018-07-04 09:58:05 -0400404 struct ipv6_opt_hdr *new = NULL;
405
406 /* hop-by-hop / destination options are privileged option */
407 retv = -EPERM;
408 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
409 break;
Shan Weiaea74272008-06-19 16:29:39 -0700410
411 /* remove any sticky options header with a zero option
412 * length, per RFC3542.
413 */
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900414 if (optlen == 0)
Luiz Capitulinocb422c42005-11-14 21:43:36 -0800415 optval = NULL;
Ian Morris63159f22015-03-29 14:00:04 +0100416 else if (!optval)
Yang Hongyangcfb266c2008-08-03 18:16:15 -0700417 goto e_inval;
Shan Weiaea74272008-06-19 16:29:39 -0700418 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
419 optlen & 0x7 || optlen > 8 * 255)
420 goto e_inval;
Paul Moorea9ba23d2018-07-04 09:58:05 -0400421 else {
422 new = memdup_user(optval, optlen);
423 if (IS_ERR(new)) {
424 retv = PTR_ERR(new);
425 break;
426 }
427 if (unlikely(ipv6_optlen(new) > optlen)) {
428 kfree(new);
429 goto e_inval;
430 }
431 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900432
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200433 opt = rcu_dereference_protected(np->opt,
434 lockdep_sock_is_held(sk));
Paul Moorea9ba23d2018-07-04 09:58:05 -0400435 opt = ipv6_renew_options(sk, opt, optname, new);
436 kfree(new);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900437 if (IS_ERR(opt)) {
438 retv = PTR_ERR(opt);
439 break;
440 }
441
442 /* routing header option needs extra check */
Brian Haley6e093d92008-11-12 22:59:21 -0800443 retv = -EINVAL;
Olaf Kirchdfee0a72007-03-09 13:55:38 -0800444 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900445 struct ipv6_rt_hdr *rthdr = opt->srcrt;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700446 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000447#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700448 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800449 if (rthdr->hdrlen != 2 ||
450 rthdr->segments_left != 1)
451 goto sticky_done;
452
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700453 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700454#endif
David Lebruna149e7c2016-11-08 14:59:21 +0100455 case IPV6_SRCRT_TYPE_4:
456 {
457 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)
458 opt->srcrt;
459
460 if (!seg6_validate_srh(srh, optlen))
461 goto sticky_done;
462 break;
463 }
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700464 default:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900465 goto sticky_done;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700466 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900467 }
468
469 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700470 opt = ipv6_update_options(sk, opt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900471sticky_done:
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800472 if (opt) {
473 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
474 txopt_put(opt);
475 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900476 break;
477 }
478
Yang Hongyangb24a2512008-12-16 02:06:23 -0800479 case IPV6_PKTINFO:
480 {
481 struct in6_pktinfo pkt;
482
483 if (optlen == 0)
484 goto e_inval;
Ian Morris63159f22015-03-29 14:00:04 +0100485 else if (optlen < sizeof(struct in6_pktinfo) || !optval)
Yang Hongyangb24a2512008-12-16 02:06:23 -0800486 goto e_inval;
487
Ilpo Järvinen914d1162009-01-04 17:27:31 -0800488 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
Yang Hongyangb24a2512008-12-16 02:06:23 -0800489 retv = -EFAULT;
490 break;
491 }
Mike Manningd839a0e2018-11-07 15:36:08 +0000492 if (!sk_dev_equal_l3scope(sk, pkt.ipi6_ifindex))
Yang Hongyangb24a2512008-12-16 02:06:23 -0800493 goto e_inval;
494
495 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000496 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
Yang Hongyangb24a2512008-12-16 02:06:23 -0800497 retv = 0;
498 break;
499 }
500
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900501 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 {
503 struct ipv6_txoptions *opt = NULL;
504 struct msghdr msg;
David S. Miller4c9483b2011-03-12 16:22:43 -0500505 struct flowi6 fl6;
Wei Wang26879da2016-05-02 21:40:07 -0700506 struct ipcm6_cookie ipc6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
David S. Miller4c9483b2011-03-12 16:22:43 -0500508 memset(&fl6, 0, sizeof(fl6));
509 fl6.flowi6_oif = sk->sk_bound_dev_if;
510 fl6.flowi6_mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 if (optlen == 0)
513 goto update;
514
515 /* 1K is probably excessive
516 * 1K is surely not enough, 2K per standard header is 16K.
517 */
518 retv = -EINVAL;
519 if (optlen > 64*1024)
520 break;
521
522 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
523 retv = -ENOBUFS;
Ian Morris63159f22015-03-29 14:00:04 +0100524 if (!opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 break;
526
527 memset(opt, 0, sizeof(*opt));
Reshetova, Elena0aeea212017-07-04 09:34:54 +0300528 refcount_set(&opt->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 opt->tot_len = sizeof(*opt) + optlen;
530 retv = -EFAULT;
531 if (copy_from_user(opt+1, optval, optlen))
532 goto done;
533
534 msg.msg_controllen = optlen;
Ian Morris67ba4152014-08-24 21:53:10 +0100535 msg.msg_control = (void *)(opt+1);
Wei Wang26879da2016-05-02 21:40:07 -0700536 ipc6.opt = opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Willem de Bruijn5fdaa882018-07-06 10:12:57 -0400538 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (retv)
540 goto done;
541update:
542 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700543 opt = ipv6_update_options(sk, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544done:
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800545 if (opt) {
546 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
547 txopt_put(opt);
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 break;
550 }
551 case IPV6_UNICAST_HOPS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900552 if (optlen < sizeof(int))
553 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (val > 255 || val < -1)
555 goto e_inval;
556 np->hop_limit = val;
557 retv = 0;
558 break;
559
560 case IPV6_MULTICAST_HOPS:
561 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900562 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900563 if (optlen < sizeof(int))
564 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (val > 255 || val < -1)
566 goto e_inval;
Li Wei2a38e6d2011-11-27 21:33:34 +0000567 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 retv = 0;
569 break;
570
571 case IPV6_MULTICAST_LOOP:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900572 if (optlen < sizeof(int))
573 goto e_inval;
YOSHIFUJI Hideaki28d44882008-06-12 03:14:51 +0900574 if (val != valbool)
575 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 np->mc_loop = valbool;
577 retv = 0;
578 break;
579
Erich E. Hooverc4062df2012-02-08 09:11:08 +0000580 case IPV6_UNICAST_IF:
581 {
582 struct net_device *dev = NULL;
583 int ifindex;
584
585 if (optlen != sizeof(int))
586 goto e_inval;
587
588 ifindex = (__force int)ntohl((__force __be32)val);
589 if (ifindex == 0) {
590 np->ucast_oif = 0;
591 retv = 0;
592 break;
593 }
594
595 dev = dev_get_by_index(net, ifindex);
596 retv = -EADDRNOTAVAIL;
597 if (!dev)
598 break;
599 dev_put(dev);
600
601 retv = -EINVAL;
602 if (sk->sk_bound_dev_if)
603 break;
604
605 np->ucast_oif = ifindex;
606 retv = 0;
607 break;
608 }
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 case IPV6_MULTICAST_IF:
611 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900612 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900613 if (optlen < sizeof(int))
614 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Brian Haley4953f0f2007-10-11 14:39:29 -0700616 if (val) {
Eric Dumazet55b80502009-10-19 06:41:58 +0000617 struct net_device *dev;
David Ahern7bb387c2016-12-29 15:39:37 -0800618 int midx;
Eric Dumazet55b80502009-10-19 06:41:58 +0000619
David Ahern7bb387c2016-12-29 15:39:37 -0800620 rcu_read_lock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700621
David Ahern7bb387c2016-12-29 15:39:37 -0800622 dev = dev_get_by_index_rcu(net, val);
Eric Dumazet55b80502009-10-19 06:41:58 +0000623 if (!dev) {
David Ahern7bb387c2016-12-29 15:39:37 -0800624 rcu_read_unlock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700625 retv = -ENODEV;
626 break;
627 }
David Ahern7bb387c2016-12-29 15:39:37 -0800628 midx = l3mdev_master_ifindex_rcu(dev);
629
630 rcu_read_unlock();
631
632 if (sk->sk_bound_dev_if &&
633 sk->sk_bound_dev_if != val &&
634 (!midx || midx != sk->sk_bound_dev_if))
635 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637 np->mcast_oif = val;
638 retv = 0;
639 break;
640 case IPV6_ADD_MEMBERSHIP:
641 case IPV6_DROP_MEMBERSHIP:
642 {
643 struct ipv6_mreq mreq;
644
Wang Chena28398b2008-04-07 09:42:07 +0800645 if (optlen < sizeof(struct ipv6_mreq))
646 goto e_inval;
647
Flavio Leitnera96fb492007-08-24 22:16:39 -0700648 retv = -EPROTO;
649 if (inet_sk(sk)->is_icsk)
650 break;
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 retv = -EFAULT;
653 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
654 break;
655
656 if (optname == IPV6_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300657 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300659 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 break;
661 }
662 case IPV6_JOIN_ANYCAST:
663 case IPV6_LEAVE_ANYCAST:
664 {
665 struct ipv6_mreq mreq;
666
Wang Chena28398b2008-04-07 09:42:07 +0800667 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 goto e_inval;
669
670 retv = -EFAULT;
671 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
672 break;
673
674 if (optname == IPV6_JOIN_ANYCAST)
675 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
676 else
677 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
678 break;
679 }
Andre Naujoks15033f02018-09-10 10:27:15 +0200680 case IPV6_MULTICAST_ALL:
681 if (optlen < sizeof(int))
682 goto e_inval;
683 np->mc_all = valbool;
684 retv = 0;
685 break;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 case MCAST_JOIN_GROUP:
688 case MCAST_LEAVE_GROUP:
689 {
690 struct group_req greq;
691 struct sockaddr_in6 *psin6;
692
Wang Chena28398b2008-04-07 09:42:07 +0800693 if (optlen < sizeof(struct group_req))
694 goto e_inval;
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 retv = -EFAULT;
697 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
698 break;
699 if (greq.gr_group.ss_family != AF_INET6) {
700 retv = -EADDRNOTAVAIL;
701 break;
702 }
703 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
704 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300705 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
706 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300708 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
709 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 break;
711 }
712 case MCAST_JOIN_SOURCE_GROUP:
713 case MCAST_LEAVE_SOURCE_GROUP:
714 case MCAST_BLOCK_SOURCE:
715 case MCAST_UNBLOCK_SOURCE:
716 {
717 struct group_source_req greqs;
718 int omode, add;
719
Wang Chena28398b2008-04-07 09:42:07 +0800720 if (optlen < sizeof(struct group_source_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 goto e_inval;
722 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
723 retv = -EFAULT;
724 break;
725 }
726 if (greqs.gsr_group.ss_family != AF_INET6 ||
727 greqs.gsr_source.ss_family != AF_INET6) {
728 retv = -EADDRNOTAVAIL;
729 break;
730 }
731 if (optname == MCAST_BLOCK_SOURCE) {
732 omode = MCAST_EXCLUDE;
733 add = 1;
734 } else if (optname == MCAST_UNBLOCK_SOURCE) {
735 omode = MCAST_EXCLUDE;
736 add = 0;
737 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
738 struct sockaddr_in6 *psin6;
739
740 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
Hangbin Liuc7ea20c2018-07-10 22:41:27 +0800741 retv = ipv6_sock_mc_join_ssm(sk, greqs.gsr_interface,
742 &psin6->sin6_addr,
743 MCAST_INCLUDE);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700744 /* prior join w/ different source is ok */
745 if (retv && retv != -EADDRINUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 break;
747 omode = MCAST_INCLUDE;
748 add = 1;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700749 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 omode = MCAST_INCLUDE;
751 add = 0;
752 }
753 retv = ip6_mc_source(add, omode, sk, &greqs);
754 break;
755 }
756 case MCAST_MSFILTER:
757 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 struct group_filter *gsf;
759
760 if (optlen < GROUP_FILTER_SIZE(0))
761 goto e_inval;
762 if (optlen > sysctl_optmem_max) {
763 retv = -ENOBUFS;
764 break;
765 }
Al Viro43727da2017-05-13 18:22:53 -0400766 gsf = memdup_user(optval, optlen);
767 if (IS_ERR(gsf)) {
768 retv = PTR_ERR(gsf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 break;
770 }
771 /* numsrc >= (4G-140)/128 overflow in 32 bits */
772 if (gsf->gf_numsrc >= 0x1ffffffU ||
773 gsf->gf_numsrc > sysctl_mld_max_msf) {
774 kfree(gsf);
775 retv = -ENOBUFS;
776 break;
777 }
778 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
779 kfree(gsf);
780 retv = -EINVAL;
781 break;
782 }
783 retv = ip6_mc_msfilter(sk, gsf);
784 kfree(gsf);
785
786 break;
787 }
788 case IPV6_ROUTER_ALERT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900789 if (optlen < sizeof(int))
790 goto e_inval;
Denis V. Lunev725a8ff2008-07-19 00:28:58 -0700791 retv = ip6_ra_control(sk, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 break;
Francesco Ruggeri9036b2f2019-03-01 15:31:03 -0800793 case IPV6_ROUTER_ALERT_ISOLATE:
794 if (optlen < sizeof(int))
795 goto e_inval;
796 np->rtalert_isolate = valbool;
797 retv = 0;
798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 case IPV6_MTU_DISCOVER:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900800 if (optlen < sizeof(int))
801 goto e_inval;
Hannes Frederic Sowa0b952272014-02-26 01:20:43 +0100802 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 goto e_inval;
804 np->pmtudisc = val;
805 retv = 0;
806 break;
807 case IPV6_MTU:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900808 if (optlen < sizeof(int))
809 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (val && val < IPV6_MIN_MTU)
811 goto e_inval;
812 np->frag_size = val;
813 retv = 0;
814 break;
815 case IPV6_RECVERR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900816 if (optlen < sizeof(int))
817 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 np->recverr = valbool;
819 if (!val)
820 skb_queue_purge(&sk->sk_error_queue);
821 retv = 0;
822 break;
823 case IPV6_FLOWINFO_SEND:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900824 if (optlen < sizeof(int))
825 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 np->sndflow = valbool;
827 retv = 0;
828 break;
829 case IPV6_FLOWLABEL_MGR:
830 retv = ipv6_flowlabel_opt(sk, optval, optlen);
831 break;
832 case IPV6_IPSEC_POLICY:
833 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700834 retv = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000835 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700836 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 retv = xfrm_user_policy(sk, optname, optval, optlen);
838 break;
839
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900840 case IPV6_ADDR_PREFERENCES:
841 {
842 unsigned int pref = 0;
843 unsigned int prefmask = ~0;
844
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900845 if (optlen < sizeof(int))
846 goto e_inval;
847
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900848 retv = -EINVAL;
849
850 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
851 switch (val & (IPV6_PREFER_SRC_PUBLIC|
852 IPV6_PREFER_SRC_TMP|
853 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
854 case IPV6_PREFER_SRC_PUBLIC:
855 pref |= IPV6_PREFER_SRC_PUBLIC;
856 break;
857 case IPV6_PREFER_SRC_TMP:
858 pref |= IPV6_PREFER_SRC_TMP;
859 break;
860 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
861 break;
862 case 0:
863 goto pref_skip_pubtmp;
864 default:
865 goto e_inval;
866 }
867
868 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
869 IPV6_PREFER_SRC_TMP);
870pref_skip_pubtmp:
871
872 /* check HOME/COA conflicts */
873 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
874 case IPV6_PREFER_SRC_HOME:
875 break;
876 case IPV6_PREFER_SRC_COA:
877 pref |= IPV6_PREFER_SRC_COA;
878 case 0:
879 goto pref_skip_coa;
880 default:
881 goto e_inval;
882 }
883
884 prefmask &= ~IPV6_PREFER_SRC_COA;
885pref_skip_coa:
886
887 /* check CGA/NONCGA conflicts */
888 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
889 case IPV6_PREFER_SRC_CGA:
890 case IPV6_PREFER_SRC_NONCGA:
891 case 0:
892 break;
893 default:
894 goto e_inval;
895 }
896
897 np->srcprefs = (np->srcprefs & prefmask) | pref;
898 retv = 0;
899
900 break;
901 }
Stephen Hemmingere802af92010-04-22 15:24:53 -0700902 case IPV6_MINHOPCOUNT:
903 if (optlen < sizeof(int))
904 goto e_inval;
905 if (val < 0 || val > 255)
906 goto e_inval;
907 np->min_hopcount = val;
Hannes Frederic Sowad4596ba2012-11-10 19:52:34 +0000908 retv = 0;
Brian Haley793b14732010-04-23 11:26:07 +0000909 break;
910 case IPV6_DONTFRAG:
911 np->dontfrag = valbool;
Stephen Hemmingere802af92010-04-22 15:24:53 -0700912 retv = 0;
913 break;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700914 case IPV6_AUTOFLOWLABEL:
915 np->autoflowlabel = valbool;
Shaohua Li513674b2017-12-20 12:10:21 -0800916 np->autoflowlabel_set = 1;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700917 retv = 0;
918 break;
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -0400919 case IPV6_RECVFRAGSIZE:
920 np->rxopt.bits.recvfragsize = valbool;
921 retv = 0;
922 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300926 if (needs_rtnl)
927 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return retv;
930
931e_inval:
932 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300933 if (needs_rtnl)
934 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return -EINVAL;
936}
937
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800938int ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700939 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800940{
941 int err;
942
943 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
944 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
945
946 if (level != SOL_IPV6)
947 return -ENOPROTOOPT;
948
949 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
950#ifdef CONFIG_NETFILTER
951 /* we need to exclude all possible ENOPROTOOPTs except default case */
952 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +0100953 optname != IPV6_XFRM_POLICY)
954 err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800955#endif
956 return err;
957}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900958EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800959
960#ifdef CONFIG_COMPAT
961int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700962 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800963{
964 int err;
965
966 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800967 if (udp_prot.compat_setsockopt != NULL)
968 return udp_prot.compat_setsockopt(sk, level, optname,
969 optval, optlen);
970 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800971 }
972
973 if (level != SOL_IPV6)
974 return -ENOPROTOOPT;
975
Al Viroe9c375f2020-05-09 21:16:36 -0400976 switch (optname) {
977 case MCAST_JOIN_GROUP:
978 case MCAST_LEAVE_GROUP:
979 case MCAST_JOIN_SOURCE_GROUP:
980 case MCAST_LEAVE_SOURCE_GROUP:
981 case MCAST_BLOCK_SOURCE:
982 case MCAST_UNBLOCK_SOURCE:
983 case MCAST_MSFILTER:
David L Stevensdae50292008-04-27 01:06:07 -0700984 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
985 ipv6_setsockopt);
Al Viroe9c375f2020-05-09 21:16:36 -0400986 }
David L Stevensdae50292008-04-27 01:06:07 -0700987
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800988 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
989#ifdef CONFIG_NETFILTER
990 /* we need to exclude all possible ENOPROTOOPTs except default case */
991 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +0100992 optname != IPV6_XFRM_POLICY)
993 err = compat_nf_setsockopt(sk, PF_INET6, optname, optval,
994 optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800995#endif
996 return err;
997}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800998EXPORT_SYMBOL(compat_ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800999#endif
1000
David S. Miller286930792007-03-07 12:50:46 -08001001static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001002 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001003{
David S. Miller286930792007-03-07 12:50:46 -08001004 struct ipv6_opt_hdr *hdr;
1005
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001006 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001007 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001008
Ian Morris67ba4152014-08-24 21:53:10 +01001009 switch (optname) {
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001010 case IPV6_HOPOPTS:
1011 hdr = opt->hopopt;
1012 break;
1013 case IPV6_RTHDRDSTOPTS:
1014 hdr = opt->dst0opt;
1015 break;
1016 case IPV6_RTHDR:
1017 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
1018 break;
1019 case IPV6_DSTOPTS:
1020 hdr = opt->dst1opt;
1021 break;
1022 default:
1023 return -EINVAL; /* should not happen */
1024 }
1025
1026 if (!hdr)
1027 return 0;
David S. Miller286930792007-03-07 12:50:46 -08001028
Chris Wrightd2b02ed2007-03-09 16:19:17 -08001029 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -07001030 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001031 return -EFAULT;
Yang Hongyang95b496b2008-05-28 16:27:28 +08001032 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001033}
1034
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001035static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001036 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 struct ipv6_pinfo *np = inet6_sk(sk);
1039 int len;
1040 int val;
1041
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001042 if (ip6_mroute_opt(optname))
1043 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (get_user(len, optlen))
1046 return -EFAULT;
1047 switch (optname) {
1048 case IPV6_ADDRFORM:
1049 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001050 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 sk->sk_protocol != IPPROTO_TCP)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +09001052 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (sk->sk_state != TCP_ESTABLISHED)
1054 return -ENOTCONN;
1055 val = sk->sk_family;
1056 break;
1057 case MCAST_MSFILTER:
1058 {
1059 struct group_filter gsf;
1060 int err;
1061
1062 if (len < GROUP_FILTER_SIZE(0))
1063 return -EINVAL;
1064 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
1065 return -EFAULT;
YOSHIFUJI Hideaki20c61fbd2008-04-28 14:40:55 +09001066 if (gsf.gf_group.ss_family != AF_INET6)
1067 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 lock_sock(sk);
1069 err = ip6_mc_msfget(sk, &gsf,
1070 (struct group_filter __user *)optval, optlen);
1071 release_sock(sk);
1072 return err;
1073 }
1074
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001075 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 {
1077 struct msghdr msg;
1078 struct sk_buff *skb;
1079
1080 if (sk->sk_type != SOCK_STREAM)
1081 return -ENOPROTOOPT;
1082
1083 msg.msg_control = optval;
1084 msg.msg_controllen = len;
Daniel Baluta98e77432011-08-19 03:19:07 -07001085 msg.msg_flags = flags;
Christoph Hellwig1b2f08d2020-05-13 21:36:41 +02001086 msg.msg_control_is_user = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 lock_sock(sk);
1089 skb = np->pktoptions;
1090 if (skb)
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001091 ip6_datagram_recv_ctl(sk, &msg, skb);
Eric Dumazet1dc7b902015-01-21 03:45:42 -08001092 release_sock(sk);
1093 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (np->rxopt.bits.rxinfo) {
1095 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001096 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1097 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001098 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1100 }
1101 if (np->rxopt.bits.rxhlim) {
1102 int hlim = np->mcast_hops;
1103 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1104 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001105 if (np->rxopt.bits.rxtclass) {
Li RongQingd76ed222014-01-15 17:03:30 +08001106 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1107
Jiri Benc4c507d22012-02-09 09:35:49 +00001108 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1109 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001110 if (np->rxopt.bits.rxoinfo) {
1111 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001112 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1113 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001114 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1115 np->sticky_pktinfo.ipi6_addr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001116 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1117 }
1118 if (np->rxopt.bits.rxohlim) {
1119 int hlim = np->mcast_hops;
1120 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1121 }
Florent Fourcot1397ed32013-12-08 15:46:57 +01001122 if (np->rxopt.bits.rxflow) {
Florent Fourcot68536052013-12-12 17:07:58 +01001123 __be32 flowinfo = np->rcv_flowinfo;
1124
Florent Fourcot1397ed32013-12-08 15:46:57 +01001125 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128 len -= msg.msg_controllen;
1129 return put_user(len, optlen);
1130 }
1131 case IPV6_MTU:
1132 {
1133 struct dst_entry *dst;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001134
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001135 val = 0;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001136 rcu_read_lock();
1137 dst = __sk_dst_get(sk);
1138 if (dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 val = dst_mtu(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001140 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (!val)
1142 return -ENOTCONN;
1143 break;
1144 }
1145
1146 case IPV6_V6ONLY:
Eric Dumazet9fe516b2014-06-27 08:36:16 -07001147 val = sk->sk_ipv6only;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 break;
1149
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001150 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 val = np->rxopt.bits.rxinfo;
1152 break;
1153
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001154 case IPV6_2292PKTINFO:
1155 val = np->rxopt.bits.rxoinfo;
1156 break;
1157
1158 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 val = np->rxopt.bits.rxhlim;
1160 break;
1161
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001162 case IPV6_2292HOPLIMIT:
1163 val = np->rxopt.bits.rxohlim;
1164 break;
1165
1166 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 val = np->rxopt.bits.srcrt;
1168 break;
1169
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001170 case IPV6_2292RTHDR:
1171 val = np->rxopt.bits.osrcrt;
1172 break;
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001175 case IPV6_RTHDRDSTOPTS:
1176 case IPV6_RTHDR:
1177 case IPV6_DSTOPTS:
1178 {
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001179 struct ipv6_txoptions *opt;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001180
1181 lock_sock(sk);
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001182 opt = rcu_dereference_protected(np->opt,
1183 lockdep_sock_is_held(sk));
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001184 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001185 release_sock(sk);
Yang Hongyang05335c22008-05-28 16:23:47 +08001186 /* check if ipv6_getsockopt_sticky() returns err code */
1187 if (len < 0)
1188 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001189 return put_user(len, optlen);
1190 }
1191
1192 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 val = np->rxopt.bits.hopopts;
1194 break;
1195
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001196 case IPV6_2292HOPOPTS:
1197 val = np->rxopt.bits.ohopopts;
1198 break;
1199
1200 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 val = np->rxopt.bits.dstopts;
1202 break;
1203
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001204 case IPV6_2292DSTOPTS:
1205 val = np->rxopt.bits.odstopts;
1206 break;
1207
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001208 case IPV6_TCLASS:
1209 val = np->tclass;
1210 break;
1211
1212 case IPV6_RECVTCLASS:
1213 val = np->rxopt.bits.rxtclass;
1214 break;
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 case IPV6_FLOWINFO:
1217 val = np->rxopt.bits.rxflow;
1218 break;
1219
Brian Haley793b14732010-04-23 11:26:07 +00001220 case IPV6_RECVPATHMTU:
1221 val = np->rxopt.bits.rxpmtu;
1222 break;
1223
1224 case IPV6_PATHMTU:
1225 {
1226 struct dst_entry *dst;
1227 struct ip6_mtuinfo mtuinfo;
1228
1229 if (len < sizeof(mtuinfo))
1230 return -EINVAL;
1231
1232 len = sizeof(mtuinfo);
1233 memset(&mtuinfo, 0, sizeof(mtuinfo));
1234
1235 rcu_read_lock();
1236 dst = __sk_dst_get(sk);
1237 if (dst)
1238 mtuinfo.ip6m_mtu = dst_mtu(dst);
1239 rcu_read_unlock();
1240 if (!mtuinfo.ip6m_mtu)
1241 return -ENOTCONN;
1242
1243 if (put_user(len, optlen))
1244 return -EFAULT;
1245 if (copy_to_user(optval, &mtuinfo, len))
1246 return -EFAULT;
1247
1248 return 0;
Brian Haley793b14732010-04-23 11:26:07 +00001249 }
1250
Balazs Scheidler6c468622010-10-21 16:08:28 +02001251 case IPV6_TRANSPARENT:
1252 val = inet_sk(sk)->transparent;
1253 break;
1254
Maciej Żenczykowski84e14fe2017-09-26 21:32:42 -07001255 case IPV6_FREEBIND:
1256 val = inet_sk(sk)->freebind;
1257 break;
1258
Balazs Scheidler6c468622010-10-21 16:08:28 +02001259 case IPV6_RECVORIGDSTADDR:
1260 val = np->rxopt.bits.rxorigdstaddr;
1261 break;
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -08001265 {
1266 struct dst_entry *dst;
1267
1268 if (optname == IPV6_UNICAST_HOPS)
1269 val = np->hop_limit;
1270 else
1271 val = np->mcast_hops;
1272
Eric Dumazetb6c67122010-04-08 23:03:29 +00001273 if (val < 0) {
1274 rcu_read_lock();
1275 dst = __sk_dst_get(sk);
1276 if (dst)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001277 val = ip6_dst_hoplimit(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001278 rcu_read_unlock();
Brian Haleybefffe92006-12-12 17:09:49 -08001279 }
Eric Dumazetb6c67122010-04-08 23:03:29 +00001280
Brian Haleybefffe92006-12-12 17:09:49 -08001281 if (val < 0)
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001282 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 break;
Brian Haleybefffe92006-12-12 17:09:49 -08001284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 case IPV6_MULTICAST_LOOP:
1287 val = np->mc_loop;
1288 break;
1289
1290 case IPV6_MULTICAST_IF:
1291 val = np->mcast_oif;
1292 break;
1293
Andre Naujoks15033f02018-09-10 10:27:15 +02001294 case IPV6_MULTICAST_ALL:
1295 val = np->mc_all;
1296 break;
1297
Erich E. Hooverc4062df2012-02-08 09:11:08 +00001298 case IPV6_UNICAST_IF:
1299 val = (__force int)htonl((__u32) np->ucast_oif);
1300 break;
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 case IPV6_MTU_DISCOVER:
1303 val = np->pmtudisc;
1304 break;
1305
1306 case IPV6_RECVERR:
1307 val = np->recverr;
1308 break;
1309
1310 case IPV6_FLOWINFO_SEND:
1311 val = np->sndflow;
1312 break;
1313
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001314 case IPV6_FLOWLABEL_MGR:
1315 {
1316 struct in6_flowlabel_req freq;
Florent Fourcot46e5f402014-01-17 17:15:04 +01001317 int flags;
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001318
1319 if (len < sizeof(freq))
1320 return -EINVAL;
1321
1322 if (copy_from_user(&freq, optval, sizeof(freq)))
1323 return -EFAULT;
1324
1325 if (freq.flr_action != IPV6_FL_A_GET)
1326 return -EINVAL;
1327
1328 len = sizeof(freq);
Florent Fourcot46e5f402014-01-17 17:15:04 +01001329 flags = freq.flr_flags;
1330
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001331 memset(&freq, 0, sizeof(freq));
1332
Florent Fourcot46e5f402014-01-17 17:15:04 +01001333 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001334 if (val < 0)
1335 return val;
1336
1337 if (put_user(len, optlen))
1338 return -EFAULT;
1339 if (copy_to_user(optval, &freq, len))
1340 return -EFAULT;
1341
1342 return 0;
1343 }
1344
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001345 case IPV6_ADDR_PREFERENCES:
1346 val = 0;
1347
1348 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1349 val |= IPV6_PREFER_SRC_TMP;
1350 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1351 val |= IPV6_PREFER_SRC_PUBLIC;
1352 else {
1353 /* XXX: should we return system default? */
1354 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1355 }
1356
1357 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1358 val |= IPV6_PREFER_SRC_COA;
1359 else
1360 val |= IPV6_PREFER_SRC_HOME;
1361 break;
1362
Stephen Hemmingere802af92010-04-22 15:24:53 -07001363 case IPV6_MINHOPCOUNT:
1364 val = np->min_hopcount;
1365 break;
1366
Brian Haley793b14732010-04-23 11:26:07 +00001367 case IPV6_DONTFRAG:
1368 val = np->dontfrag;
1369 break;
1370
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001371 case IPV6_AUTOFLOWLABEL:
Ben Hutchingse9191ff2018-01-22 20:06:42 +00001372 val = ip6_autoflowlabel(sock_net(sk), np);
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001373 break;
1374
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -04001375 case IPV6_RECVFRAGSIZE:
1376 val = np->rxopt.bits.recvfragsize;
1377 break;
1378
Francesco Ruggeri9036b2f2019-03-01 15:31:03 -08001379 case IPV6_ROUTER_ALERT_ISOLATE:
1380 val = np->rtalert_isolate;
1381 break;
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001384 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386 len = min_t(unsigned int, sizeof(int), len);
Ian Morris67ba4152014-08-24 21:53:10 +01001387 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001389 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return -EFAULT;
1391 return 0;
1392}
1393
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001394int ipv6_getsockopt(struct sock *sk, int level, int optname,
1395 char __user *optval, int __user *optlen)
1396{
1397 int err;
1398
1399 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1400 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1401
Ian Morris67ba4152014-08-24 21:53:10 +01001402 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001403 return -ENOPROTOOPT;
1404
Daniel Baluta98e77432011-08-19 03:19:07 -07001405 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001406#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001407 /* we need to exclude all possible ENOPROTOOPTs except default case */
1408 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001409 int len;
1410
1411 if (get_user(len, optlen))
1412 return -EFAULT;
1413
Paolo Abeni01ea3062018-02-08 12:19:00 +01001414 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001415 if (err >= 0)
1416 err = put_user(len, optlen);
1417 }
1418#endif
1419 return err;
1420}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001421EXPORT_SYMBOL(ipv6_getsockopt);
1422
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001423#ifdef CONFIG_COMPAT
1424int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001425 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001426{
1427 int err;
1428
1429 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001430 if (udp_prot.compat_getsockopt != NULL)
1431 return udp_prot.compat_getsockopt(sk, level, optname,
1432 optval, optlen);
1433 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001434 }
1435
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001436 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001437 return -ENOPROTOOPT;
1438
Pavel Emelyanov80991792008-04-30 14:49:54 -07001439 if (optname == MCAST_MSFILTER)
1440 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1441 ipv6_getsockopt);
1442
Daniel Baluta98e77432011-08-19 03:19:07 -07001443 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1444 MSG_CMSG_COMPAT);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001445#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001446 /* we need to exclude all possible ENOPROTOOPTs except default case */
1447 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001448 int len;
1449
1450 if (get_user(len, optlen))
1451 return -EFAULT;
1452
Paolo Abeni01ea3062018-02-08 12:19:00 +01001453 err = compat_nf_getsockopt(sk, PF_INET6, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001454 if (err >= 0)
1455 err = put_user(len, optlen);
1456 }
1457#endif
1458 return err;
1459}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001460EXPORT_SYMBOL(compat_ipv6_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001461#endif