blob: 264c292e7dcc479b0020a9c712291fac73ae3a06 [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 }
186 } else if (sk->sk_protocol != IPPROTO_TCP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 break;
188
189 if (sk->sk_state != TCP_ESTABLISHED) {
190 retv = -ENOTCONN;
191 break;
192 }
193
194 if (ipv6_only_sock(sk) ||
Eric Dumazetefe42082013-10-03 15:42:29 -0700195 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 retv = -EADDRNOTAVAIL;
197 break;
198 }
199
200 fl6_free_socklist(sk);
WANG Cong8651be82016-10-19 23:35:12 -0700201 __ipv6_sock_mc_close(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700203 /*
204 * Sock is moving from IPv6 to IPv4 (sk_prot), so
205 * remove it from the refcnt debug socks count in the
206 * original family...
207 */
208 sk_refcnt_debug_dec(sk);
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (sk->sk_protocol == IPPROTO_TCP) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800211 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700213 sock_prot_inuse_add(net, sk->sk_prot, -1);
214 sock_prot_inuse_add(net, &tcp_prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 local_bh_enable();
216 sk->sk_prot = &tcp_prot;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800217 icsk->icsk_af_ops = &ipv4_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 sk->sk_socket->ops = &inet_stream_ops;
219 sk->sk_family = PF_INET;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800220 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 } else {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800222 struct proto *prot = &udp_prot;
223
David S. Millerdb8dac22008-03-06 16:22:02 -0800224 if (sk->sk_protocol == IPPROTO_UDPLITE)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800225 prot = &udplite_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700227 sock_prot_inuse_add(net, sk->sk_prot, -1);
228 sock_prot_inuse_add(net, prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 local_bh_enable();
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800230 sk->sk_prot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 sk->sk_socket->ops = &inet_dgram_ops;
232 sk->sk_family = PF_INET;
233 }
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800234 opt = xchg((__force struct ipv6_txoptions **)&np->opt,
235 NULL);
236 if (opt) {
237 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
238 txopt_put(opt);
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 pktopt = xchg(&np->pktoptions, NULL);
Wei Yongjun800d55f2009-02-23 21:45:33 +0000241 kfree_skb(pktopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700243 /*
244 * ... and add it to the refcnt debug socks count
245 * in the new family. -acme
246 */
247 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 module_put(THIS_MODULE);
249 retv = 0;
250 break;
251 }
252 goto e_inval;
253
254 case IPV6_V6ONLY:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900255 if (optlen < sizeof(int) ||
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000256 inet_sk(sk)->inet_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 goto e_inval;
Eric Dumazet9fe516b2014-06-27 08:36:16 -0700258 sk->sk_ipv6only = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 retv = 0;
260 break;
261
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900262 case IPV6_RECVPKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900263 if (optlen < sizeof(int))
264 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 np->rxopt.bits.rxinfo = valbool;
266 retv = 0;
267 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900268
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900269 case IPV6_2292PKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900270 if (optlen < sizeof(int))
271 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900272 np->rxopt.bits.rxoinfo = valbool;
273 retv = 0;
274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900276 case IPV6_RECVHOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900277 if (optlen < sizeof(int))
278 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 np->rxopt.bits.rxhlim = valbool;
280 retv = 0;
281 break;
282
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900283 case IPV6_2292HOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900284 if (optlen < sizeof(int))
285 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900286 np->rxopt.bits.rxohlim = valbool;
287 retv = 0;
288 break;
289
290 case IPV6_RECVRTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900291 if (optlen < sizeof(int))
292 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900293 np->rxopt.bits.srcrt = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 retv = 0;
295 break;
296
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900297 case IPV6_2292RTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900298 if (optlen < sizeof(int))
299 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900300 np->rxopt.bits.osrcrt = valbool;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900301 retv = 0;
302 break;
303
304 case IPV6_RECVHOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900305 if (optlen < sizeof(int))
306 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 np->rxopt.bits.hopopts = valbool;
308 retv = 0;
309 break;
310
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900311 case IPV6_2292HOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900312 if (optlen < sizeof(int))
313 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900314 np->rxopt.bits.ohopopts = valbool;
315 retv = 0;
316 break;
317
318 case IPV6_RECVDSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900319 if (optlen < sizeof(int))
320 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 np->rxopt.bits.dstopts = valbool;
322 retv = 0;
323 break;
324
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900325 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900326 if (optlen < sizeof(int))
327 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900328 np->rxopt.bits.odstopts = valbool;
329 retv = 0;
330 break;
331
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900332 case IPV6_TCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900333 if (optlen < sizeof(int))
334 goto e_inval;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700335 if (val < -1 || val > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900336 goto e_inval;
Gerrit Renker26ced1e2009-08-09 08:12:49 +0000337 /* RFC 3542, 6.5: default traffic class of 0x0 */
338 if (val == -1)
339 val = 0;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900340 np->tclass = val;
341 retv = 0;
342 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900343
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900344 case IPV6_RECVTCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900345 if (optlen < sizeof(int))
346 goto e_inval;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900347 np->rxopt.bits.rxtclass = valbool;
348 retv = 0;
349 break;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 case IPV6_FLOWINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900352 if (optlen < sizeof(int))
353 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 np->rxopt.bits.rxflow = valbool;
355 retv = 0;
356 break;
357
Brian Haley793b14732010-04-23 11:26:07 +0000358 case IPV6_RECVPATHMTU:
359 if (optlen < sizeof(int))
360 goto e_inval;
361 np->rxopt.bits.rxpmtu = valbool;
362 retv = 0;
363 break;
364
Balazs Scheidler6c468622010-10-21 16:08:28 +0200365 case IPV6_TRANSPARENT:
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000366 if (valbool && !ns_capable(net->user_ns, CAP_NET_ADMIN) &&
367 !ns_capable(net->user_ns, CAP_NET_RAW)) {
Balazs Scheidlerb8894162010-10-23 04:48:14 +0000368 retv = -EPERM;
369 break;
370 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200371 if (optlen < sizeof(int))
372 goto e_inval;
373 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
374 inet_sk(sk)->transparent = valbool;
375 retv = 0;
376 break;
377
Maciej Żenczykowski84e14fe2017-09-26 21:32:42 -0700378 case IPV6_FREEBIND:
379 if (optlen < sizeof(int))
380 goto e_inval;
381 /* we also don't have a separate freebind bit for IPV6 */
382 inet_sk(sk)->freebind = valbool;
383 retv = 0;
384 break;
385
Balazs Scheidler6c468622010-10-21 16:08:28 +0200386 case IPV6_RECVORIGDSTADDR:
387 if (optlen < sizeof(int))
388 goto e_inval;
389 np->rxopt.bits.rxorigdstaddr = valbool;
390 retv = 0;
391 break;
392
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900393 case IPV6_HOPOPTS:
394 case IPV6_RTHDRDSTOPTS:
395 case IPV6_RTHDR:
396 case IPV6_DSTOPTS:
397 {
398 struct ipv6_txoptions *opt;
Paul Moorea9ba23d2018-07-04 09:58:05 -0400399 struct ipv6_opt_hdr *new = NULL;
400
401 /* hop-by-hop / destination options are privileged option */
402 retv = -EPERM;
403 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
404 break;
Shan Weiaea74272008-06-19 16:29:39 -0700405
406 /* remove any sticky options header with a zero option
407 * length, per RFC3542.
408 */
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900409 if (optlen == 0)
Luiz Capitulinocb422c42005-11-14 21:43:36 -0800410 optval = NULL;
Ian Morris63159f22015-03-29 14:00:04 +0100411 else if (!optval)
Yang Hongyangcfb266c2008-08-03 18:16:15 -0700412 goto e_inval;
Shan Weiaea74272008-06-19 16:29:39 -0700413 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
414 optlen & 0x7 || optlen > 8 * 255)
415 goto e_inval;
Paul Moorea9ba23d2018-07-04 09:58:05 -0400416 else {
417 new = memdup_user(optval, optlen);
418 if (IS_ERR(new)) {
419 retv = PTR_ERR(new);
420 break;
421 }
422 if (unlikely(ipv6_optlen(new) > optlen)) {
423 kfree(new);
424 goto e_inval;
425 }
426 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900427
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200428 opt = rcu_dereference_protected(np->opt,
429 lockdep_sock_is_held(sk));
Paul Moorea9ba23d2018-07-04 09:58:05 -0400430 opt = ipv6_renew_options(sk, opt, optname, new);
431 kfree(new);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900432 if (IS_ERR(opt)) {
433 retv = PTR_ERR(opt);
434 break;
435 }
436
437 /* routing header option needs extra check */
Brian Haley6e093d92008-11-12 22:59:21 -0800438 retv = -EINVAL;
Olaf Kirchdfee0a72007-03-09 13:55:38 -0800439 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900440 struct ipv6_rt_hdr *rthdr = opt->srcrt;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700441 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000442#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700443 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800444 if (rthdr->hdrlen != 2 ||
445 rthdr->segments_left != 1)
446 goto sticky_done;
447
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700448 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700449#endif
David Lebruna149e7c2016-11-08 14:59:21 +0100450 case IPV6_SRCRT_TYPE_4:
451 {
452 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)
453 opt->srcrt;
454
455 if (!seg6_validate_srh(srh, optlen))
456 goto sticky_done;
457 break;
458 }
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700459 default:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900460 goto sticky_done;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700461 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900462 }
463
464 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700465 opt = ipv6_update_options(sk, opt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900466sticky_done:
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800467 if (opt) {
468 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
469 txopt_put(opt);
470 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900471 break;
472 }
473
Yang Hongyangb24a2512008-12-16 02:06:23 -0800474 case IPV6_PKTINFO:
475 {
476 struct in6_pktinfo pkt;
477
478 if (optlen == 0)
479 goto e_inval;
Ian Morris63159f22015-03-29 14:00:04 +0100480 else if (optlen < sizeof(struct in6_pktinfo) || !optval)
Yang Hongyangb24a2512008-12-16 02:06:23 -0800481 goto e_inval;
482
Ilpo Järvinen914d1162009-01-04 17:27:31 -0800483 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
Yang Hongyangb24a2512008-12-16 02:06:23 -0800484 retv = -EFAULT;
485 break;
486 }
Mike Manningd839a0e2018-11-07 15:36:08 +0000487 if (!sk_dev_equal_l3scope(sk, pkt.ipi6_ifindex))
Yang Hongyangb24a2512008-12-16 02:06:23 -0800488 goto e_inval;
489
490 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000491 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
Yang Hongyangb24a2512008-12-16 02:06:23 -0800492 retv = 0;
493 break;
494 }
495
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900496 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 {
498 struct ipv6_txoptions *opt = NULL;
499 struct msghdr msg;
David S. Miller4c9483b2011-03-12 16:22:43 -0500500 struct flowi6 fl6;
Wei Wang26879da2016-05-02 21:40:07 -0700501 struct ipcm6_cookie ipc6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
David S. Miller4c9483b2011-03-12 16:22:43 -0500503 memset(&fl6, 0, sizeof(fl6));
504 fl6.flowi6_oif = sk->sk_bound_dev_if;
505 fl6.flowi6_mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 if (optlen == 0)
508 goto update;
509
510 /* 1K is probably excessive
511 * 1K is surely not enough, 2K per standard header is 16K.
512 */
513 retv = -EINVAL;
514 if (optlen > 64*1024)
515 break;
516
517 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
518 retv = -ENOBUFS;
Ian Morris63159f22015-03-29 14:00:04 +0100519 if (!opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 break;
521
522 memset(opt, 0, sizeof(*opt));
Reshetova, Elena0aeea212017-07-04 09:34:54 +0300523 refcount_set(&opt->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 opt->tot_len = sizeof(*opt) + optlen;
525 retv = -EFAULT;
526 if (copy_from_user(opt+1, optval, optlen))
527 goto done;
528
529 msg.msg_controllen = optlen;
Ian Morris67ba4152014-08-24 21:53:10 +0100530 msg.msg_control = (void *)(opt+1);
Wei Wang26879da2016-05-02 21:40:07 -0700531 ipc6.opt = opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Willem de Bruijn5fdaa882018-07-06 10:12:57 -0400533 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (retv)
535 goto done;
536update:
537 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700538 opt = ipv6_update_options(sk, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539done:
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800540 if (opt) {
541 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
542 txopt_put(opt);
543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 break;
545 }
546 case IPV6_UNICAST_HOPS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900547 if (optlen < sizeof(int))
548 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (val > 255 || val < -1)
550 goto e_inval;
551 np->hop_limit = val;
552 retv = 0;
553 break;
554
555 case IPV6_MULTICAST_HOPS:
556 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900557 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900558 if (optlen < sizeof(int))
559 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (val > 255 || val < -1)
561 goto e_inval;
Li Wei2a38e6d2011-11-27 21:33:34 +0000562 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 retv = 0;
564 break;
565
566 case IPV6_MULTICAST_LOOP:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900567 if (optlen < sizeof(int))
568 goto e_inval;
YOSHIFUJI Hideaki28d44882008-06-12 03:14:51 +0900569 if (val != valbool)
570 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 np->mc_loop = valbool;
572 retv = 0;
573 break;
574
Erich E. Hooverc4062df2012-02-08 09:11:08 +0000575 case IPV6_UNICAST_IF:
576 {
577 struct net_device *dev = NULL;
578 int ifindex;
579
580 if (optlen != sizeof(int))
581 goto e_inval;
582
583 ifindex = (__force int)ntohl((__force __be32)val);
584 if (ifindex == 0) {
585 np->ucast_oif = 0;
586 retv = 0;
587 break;
588 }
589
590 dev = dev_get_by_index(net, ifindex);
591 retv = -EADDRNOTAVAIL;
592 if (!dev)
593 break;
594 dev_put(dev);
595
596 retv = -EINVAL;
597 if (sk->sk_bound_dev_if)
598 break;
599
600 np->ucast_oif = ifindex;
601 retv = 0;
602 break;
603 }
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 case IPV6_MULTICAST_IF:
606 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900607 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900608 if (optlen < sizeof(int))
609 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Brian Haley4953f0f2007-10-11 14:39:29 -0700611 if (val) {
Eric Dumazet55b80502009-10-19 06:41:58 +0000612 struct net_device *dev;
David Ahern7bb387c2016-12-29 15:39:37 -0800613 int midx;
Eric Dumazet55b80502009-10-19 06:41:58 +0000614
David Ahern7bb387c2016-12-29 15:39:37 -0800615 rcu_read_lock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700616
David Ahern7bb387c2016-12-29 15:39:37 -0800617 dev = dev_get_by_index_rcu(net, val);
Eric Dumazet55b80502009-10-19 06:41:58 +0000618 if (!dev) {
David Ahern7bb387c2016-12-29 15:39:37 -0800619 rcu_read_unlock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700620 retv = -ENODEV;
621 break;
622 }
David Ahern7bb387c2016-12-29 15:39:37 -0800623 midx = l3mdev_master_ifindex_rcu(dev);
624
625 rcu_read_unlock();
626
627 if (sk->sk_bound_dev_if &&
628 sk->sk_bound_dev_if != val &&
629 (!midx || midx != sk->sk_bound_dev_if))
630 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632 np->mcast_oif = val;
633 retv = 0;
634 break;
635 case IPV6_ADD_MEMBERSHIP:
636 case IPV6_DROP_MEMBERSHIP:
637 {
638 struct ipv6_mreq mreq;
639
Wang Chena28398b2008-04-07 09:42:07 +0800640 if (optlen < sizeof(struct ipv6_mreq))
641 goto e_inval;
642
Flavio Leitnera96fb492007-08-24 22:16:39 -0700643 retv = -EPROTO;
644 if (inet_sk(sk)->is_icsk)
645 break;
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 retv = -EFAULT;
648 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
649 break;
650
651 if (optname == IPV6_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300652 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300654 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 break;
656 }
657 case IPV6_JOIN_ANYCAST:
658 case IPV6_LEAVE_ANYCAST:
659 {
660 struct ipv6_mreq mreq;
661
Wang Chena28398b2008-04-07 09:42:07 +0800662 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 goto e_inval;
664
665 retv = -EFAULT;
666 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
667 break;
668
669 if (optname == IPV6_JOIN_ANYCAST)
670 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
671 else
672 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
673 break;
674 }
Andre Naujoks15033f02018-09-10 10:27:15 +0200675 case IPV6_MULTICAST_ALL:
676 if (optlen < sizeof(int))
677 goto e_inval;
678 np->mc_all = valbool;
679 retv = 0;
680 break;
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 case MCAST_JOIN_GROUP:
683 case MCAST_LEAVE_GROUP:
684 {
685 struct group_req greq;
686 struct sockaddr_in6 *psin6;
687
Wang Chena28398b2008-04-07 09:42:07 +0800688 if (optlen < sizeof(struct group_req))
689 goto e_inval;
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 retv = -EFAULT;
692 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
693 break;
694 if (greq.gr_group.ss_family != AF_INET6) {
695 retv = -EADDRNOTAVAIL;
696 break;
697 }
698 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
699 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300700 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
701 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300703 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
704 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 break;
706 }
707 case MCAST_JOIN_SOURCE_GROUP:
708 case MCAST_LEAVE_SOURCE_GROUP:
709 case MCAST_BLOCK_SOURCE:
710 case MCAST_UNBLOCK_SOURCE:
711 {
712 struct group_source_req greqs;
713 int omode, add;
714
Wang Chena28398b2008-04-07 09:42:07 +0800715 if (optlen < sizeof(struct group_source_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 goto e_inval;
717 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
718 retv = -EFAULT;
719 break;
720 }
721 if (greqs.gsr_group.ss_family != AF_INET6 ||
722 greqs.gsr_source.ss_family != AF_INET6) {
723 retv = -EADDRNOTAVAIL;
724 break;
725 }
726 if (optname == MCAST_BLOCK_SOURCE) {
727 omode = MCAST_EXCLUDE;
728 add = 1;
729 } else if (optname == MCAST_UNBLOCK_SOURCE) {
730 omode = MCAST_EXCLUDE;
731 add = 0;
732 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
733 struct sockaddr_in6 *psin6;
734
735 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
Hangbin Liuc7ea20c2018-07-10 22:41:27 +0800736 retv = ipv6_sock_mc_join_ssm(sk, greqs.gsr_interface,
737 &psin6->sin6_addr,
738 MCAST_INCLUDE);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700739 /* prior join w/ different source is ok */
740 if (retv && retv != -EADDRINUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 break;
742 omode = MCAST_INCLUDE;
743 add = 1;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700744 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 omode = MCAST_INCLUDE;
746 add = 0;
747 }
748 retv = ip6_mc_source(add, omode, sk, &greqs);
749 break;
750 }
751 case MCAST_MSFILTER:
752 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 struct group_filter *gsf;
754
755 if (optlen < GROUP_FILTER_SIZE(0))
756 goto e_inval;
757 if (optlen > sysctl_optmem_max) {
758 retv = -ENOBUFS;
759 break;
760 }
Al Viro43727da2017-05-13 18:22:53 -0400761 gsf = memdup_user(optval, optlen);
762 if (IS_ERR(gsf)) {
763 retv = PTR_ERR(gsf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 break;
765 }
766 /* numsrc >= (4G-140)/128 overflow in 32 bits */
767 if (gsf->gf_numsrc >= 0x1ffffffU ||
768 gsf->gf_numsrc > sysctl_mld_max_msf) {
769 kfree(gsf);
770 retv = -ENOBUFS;
771 break;
772 }
773 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
774 kfree(gsf);
775 retv = -EINVAL;
776 break;
777 }
778 retv = ip6_mc_msfilter(sk, gsf);
779 kfree(gsf);
780
781 break;
782 }
783 case IPV6_ROUTER_ALERT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900784 if (optlen < sizeof(int))
785 goto e_inval;
Denis V. Lunev725a8ff2008-07-19 00:28:58 -0700786 retv = ip6_ra_control(sk, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 break;
Francesco Ruggeri9036b2f2019-03-01 15:31:03 -0800788 case IPV6_ROUTER_ALERT_ISOLATE:
789 if (optlen < sizeof(int))
790 goto e_inval;
791 np->rtalert_isolate = valbool;
792 retv = 0;
793 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 case IPV6_MTU_DISCOVER:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900795 if (optlen < sizeof(int))
796 goto e_inval;
Hannes Frederic Sowa0b952272014-02-26 01:20:43 +0100797 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 goto e_inval;
799 np->pmtudisc = val;
800 retv = 0;
801 break;
802 case IPV6_MTU:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900803 if (optlen < sizeof(int))
804 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (val && val < IPV6_MIN_MTU)
806 goto e_inval;
807 np->frag_size = val;
808 retv = 0;
809 break;
810 case IPV6_RECVERR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900811 if (optlen < sizeof(int))
812 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 np->recverr = valbool;
814 if (!val)
815 skb_queue_purge(&sk->sk_error_queue);
816 retv = 0;
817 break;
818 case IPV6_FLOWINFO_SEND:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900819 if (optlen < sizeof(int))
820 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 np->sndflow = valbool;
822 retv = 0;
823 break;
824 case IPV6_FLOWLABEL_MGR:
825 retv = ipv6_flowlabel_opt(sk, optval, optlen);
826 break;
827 case IPV6_IPSEC_POLICY:
828 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700829 retv = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000830 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700831 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 retv = xfrm_user_policy(sk, optname, optval, optlen);
833 break;
834
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900835 case IPV6_ADDR_PREFERENCES:
836 {
837 unsigned int pref = 0;
838 unsigned int prefmask = ~0;
839
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900840 if (optlen < sizeof(int))
841 goto e_inval;
842
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900843 retv = -EINVAL;
844
845 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
846 switch (val & (IPV6_PREFER_SRC_PUBLIC|
847 IPV6_PREFER_SRC_TMP|
848 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
849 case IPV6_PREFER_SRC_PUBLIC:
850 pref |= IPV6_PREFER_SRC_PUBLIC;
851 break;
852 case IPV6_PREFER_SRC_TMP:
853 pref |= IPV6_PREFER_SRC_TMP;
854 break;
855 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
856 break;
857 case 0:
858 goto pref_skip_pubtmp;
859 default:
860 goto e_inval;
861 }
862
863 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
864 IPV6_PREFER_SRC_TMP);
865pref_skip_pubtmp:
866
867 /* check HOME/COA conflicts */
868 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
869 case IPV6_PREFER_SRC_HOME:
870 break;
871 case IPV6_PREFER_SRC_COA:
872 pref |= IPV6_PREFER_SRC_COA;
873 case 0:
874 goto pref_skip_coa;
875 default:
876 goto e_inval;
877 }
878
879 prefmask &= ~IPV6_PREFER_SRC_COA;
880pref_skip_coa:
881
882 /* check CGA/NONCGA conflicts */
883 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
884 case IPV6_PREFER_SRC_CGA:
885 case IPV6_PREFER_SRC_NONCGA:
886 case 0:
887 break;
888 default:
889 goto e_inval;
890 }
891
892 np->srcprefs = (np->srcprefs & prefmask) | pref;
893 retv = 0;
894
895 break;
896 }
Stephen Hemmingere802af92010-04-22 15:24:53 -0700897 case IPV6_MINHOPCOUNT:
898 if (optlen < sizeof(int))
899 goto e_inval;
900 if (val < 0 || val > 255)
901 goto e_inval;
902 np->min_hopcount = val;
Hannes Frederic Sowad4596ba2012-11-10 19:52:34 +0000903 retv = 0;
Brian Haley793b14732010-04-23 11:26:07 +0000904 break;
905 case IPV6_DONTFRAG:
906 np->dontfrag = valbool;
Stephen Hemmingere802af92010-04-22 15:24:53 -0700907 retv = 0;
908 break;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700909 case IPV6_AUTOFLOWLABEL:
910 np->autoflowlabel = valbool;
Shaohua Li513674b2017-12-20 12:10:21 -0800911 np->autoflowlabel_set = 1;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700912 retv = 0;
913 break;
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -0400914 case IPV6_RECVFRAGSIZE:
915 np->rxopt.bits.recvfragsize = valbool;
916 retv = 0;
917 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300921 if (needs_rtnl)
922 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return retv;
925
926e_inval:
927 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300928 if (needs_rtnl)
929 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return -EINVAL;
931}
932
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800933int ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700934 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800935{
936 int err;
937
938 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
939 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
940
941 if (level != SOL_IPV6)
942 return -ENOPROTOOPT;
943
944 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
945#ifdef CONFIG_NETFILTER
946 /* we need to exclude all possible ENOPROTOOPTs except default case */
947 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +0100948 optname != IPV6_XFRM_POLICY)
949 err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800950#endif
951 return err;
952}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900953EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800954
955#ifdef CONFIG_COMPAT
956int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700957 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800958{
959 int err;
960
961 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800962 if (udp_prot.compat_setsockopt != NULL)
963 return udp_prot.compat_setsockopt(sk, level, optname,
964 optval, optlen);
965 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800966 }
967
968 if (level != SOL_IPV6)
969 return -ENOPROTOOPT;
970
David L Stevensdae50292008-04-27 01:06:07 -0700971 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
972 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
973 ipv6_setsockopt);
974
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800975 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
976#ifdef CONFIG_NETFILTER
977 /* we need to exclude all possible ENOPROTOOPTs except default case */
978 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +0100979 optname != IPV6_XFRM_POLICY)
980 err = compat_nf_setsockopt(sk, PF_INET6, optname, optval,
981 optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800982#endif
983 return err;
984}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800985EXPORT_SYMBOL(compat_ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800986#endif
987
David S. Miller286930792007-03-07 12:50:46 -0800988static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700989 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900990{
David S. Miller286930792007-03-07 12:50:46 -0800991 struct ipv6_opt_hdr *hdr;
992
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700993 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900994 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700995
Ian Morris67ba4152014-08-24 21:53:10 +0100996 switch (optname) {
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700997 case IPV6_HOPOPTS:
998 hdr = opt->hopopt;
999 break;
1000 case IPV6_RTHDRDSTOPTS:
1001 hdr = opt->dst0opt;
1002 break;
1003 case IPV6_RTHDR:
1004 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
1005 break;
1006 case IPV6_DSTOPTS:
1007 hdr = opt->dst1opt;
1008 break;
1009 default:
1010 return -EINVAL; /* should not happen */
1011 }
1012
1013 if (!hdr)
1014 return 0;
David S. Miller286930792007-03-07 12:50:46 -08001015
Chris Wrightd2b02ed2007-03-09 16:19:17 -08001016 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -07001017 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001018 return -EFAULT;
Yang Hongyang95b496b2008-05-28 16:27:28 +08001019 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001020}
1021
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001022static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001023 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 struct ipv6_pinfo *np = inet6_sk(sk);
1026 int len;
1027 int val;
1028
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001029 if (ip6_mroute_opt(optname))
1030 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (get_user(len, optlen))
1033 return -EFAULT;
1034 switch (optname) {
1035 case IPV6_ADDRFORM:
1036 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001037 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 sk->sk_protocol != IPPROTO_TCP)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +09001039 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (sk->sk_state != TCP_ESTABLISHED)
1041 return -ENOTCONN;
1042 val = sk->sk_family;
1043 break;
1044 case MCAST_MSFILTER:
1045 {
1046 struct group_filter gsf;
1047 int err;
1048
1049 if (len < GROUP_FILTER_SIZE(0))
1050 return -EINVAL;
1051 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
1052 return -EFAULT;
YOSHIFUJI Hideaki20c61fbd2008-04-28 14:40:55 +09001053 if (gsf.gf_group.ss_family != AF_INET6)
1054 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 lock_sock(sk);
1056 err = ip6_mc_msfget(sk, &gsf,
1057 (struct group_filter __user *)optval, optlen);
1058 release_sock(sk);
1059 return err;
1060 }
1061
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001062 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 {
1064 struct msghdr msg;
1065 struct sk_buff *skb;
1066
1067 if (sk->sk_type != SOCK_STREAM)
1068 return -ENOPROTOOPT;
1069
1070 msg.msg_control = optval;
1071 msg.msg_controllen = len;
Daniel Baluta98e77432011-08-19 03:19:07 -07001072 msg.msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 lock_sock(sk);
1075 skb = np->pktoptions;
1076 if (skb)
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001077 ip6_datagram_recv_ctl(sk, &msg, skb);
Eric Dumazet1dc7b902015-01-21 03:45:42 -08001078 release_sock(sk);
1079 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (np->rxopt.bits.rxinfo) {
1081 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001082 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1083 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001084 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1086 }
1087 if (np->rxopt.bits.rxhlim) {
1088 int hlim = np->mcast_hops;
1089 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1090 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001091 if (np->rxopt.bits.rxtclass) {
Li RongQingd76ed222014-01-15 17:03:30 +08001092 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1093
Jiri Benc4c507d22012-02-09 09:35:49 +00001094 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1095 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001096 if (np->rxopt.bits.rxoinfo) {
1097 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001098 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1099 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001100 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1101 np->sticky_pktinfo.ipi6_addr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001102 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1103 }
1104 if (np->rxopt.bits.rxohlim) {
1105 int hlim = np->mcast_hops;
1106 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1107 }
Florent Fourcot1397ed32013-12-08 15:46:57 +01001108 if (np->rxopt.bits.rxflow) {
Florent Fourcot68536052013-12-12 17:07:58 +01001109 __be32 flowinfo = np->rcv_flowinfo;
1110
Florent Fourcot1397ed32013-12-08 15:46:57 +01001111 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
1114 len -= msg.msg_controllen;
1115 return put_user(len, optlen);
1116 }
1117 case IPV6_MTU:
1118 {
1119 struct dst_entry *dst;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001120
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001121 val = 0;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001122 rcu_read_lock();
1123 dst = __sk_dst_get(sk);
1124 if (dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 val = dst_mtu(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001126 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (!val)
1128 return -ENOTCONN;
1129 break;
1130 }
1131
1132 case IPV6_V6ONLY:
Eric Dumazet9fe516b2014-06-27 08:36:16 -07001133 val = sk->sk_ipv6only;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 break;
1135
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001136 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 val = np->rxopt.bits.rxinfo;
1138 break;
1139
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001140 case IPV6_2292PKTINFO:
1141 val = np->rxopt.bits.rxoinfo;
1142 break;
1143
1144 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 val = np->rxopt.bits.rxhlim;
1146 break;
1147
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001148 case IPV6_2292HOPLIMIT:
1149 val = np->rxopt.bits.rxohlim;
1150 break;
1151
1152 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 val = np->rxopt.bits.srcrt;
1154 break;
1155
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001156 case IPV6_2292RTHDR:
1157 val = np->rxopt.bits.osrcrt;
1158 break;
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001161 case IPV6_RTHDRDSTOPTS:
1162 case IPV6_RTHDR:
1163 case IPV6_DSTOPTS:
1164 {
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001165 struct ipv6_txoptions *opt;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001166
1167 lock_sock(sk);
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001168 opt = rcu_dereference_protected(np->opt,
1169 lockdep_sock_is_held(sk));
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001170 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001171 release_sock(sk);
Yang Hongyang05335c22008-05-28 16:23:47 +08001172 /* check if ipv6_getsockopt_sticky() returns err code */
1173 if (len < 0)
1174 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001175 return put_user(len, optlen);
1176 }
1177
1178 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 val = np->rxopt.bits.hopopts;
1180 break;
1181
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001182 case IPV6_2292HOPOPTS:
1183 val = np->rxopt.bits.ohopopts;
1184 break;
1185
1186 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 val = np->rxopt.bits.dstopts;
1188 break;
1189
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001190 case IPV6_2292DSTOPTS:
1191 val = np->rxopt.bits.odstopts;
1192 break;
1193
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001194 case IPV6_TCLASS:
1195 val = np->tclass;
1196 break;
1197
1198 case IPV6_RECVTCLASS:
1199 val = np->rxopt.bits.rxtclass;
1200 break;
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 case IPV6_FLOWINFO:
1203 val = np->rxopt.bits.rxflow;
1204 break;
1205
Brian Haley793b14732010-04-23 11:26:07 +00001206 case IPV6_RECVPATHMTU:
1207 val = np->rxopt.bits.rxpmtu;
1208 break;
1209
1210 case IPV6_PATHMTU:
1211 {
1212 struct dst_entry *dst;
1213 struct ip6_mtuinfo mtuinfo;
1214
1215 if (len < sizeof(mtuinfo))
1216 return -EINVAL;
1217
1218 len = sizeof(mtuinfo);
1219 memset(&mtuinfo, 0, sizeof(mtuinfo));
1220
1221 rcu_read_lock();
1222 dst = __sk_dst_get(sk);
1223 if (dst)
1224 mtuinfo.ip6m_mtu = dst_mtu(dst);
1225 rcu_read_unlock();
1226 if (!mtuinfo.ip6m_mtu)
1227 return -ENOTCONN;
1228
1229 if (put_user(len, optlen))
1230 return -EFAULT;
1231 if (copy_to_user(optval, &mtuinfo, len))
1232 return -EFAULT;
1233
1234 return 0;
Brian Haley793b14732010-04-23 11:26:07 +00001235 }
1236
Balazs Scheidler6c468622010-10-21 16:08:28 +02001237 case IPV6_TRANSPARENT:
1238 val = inet_sk(sk)->transparent;
1239 break;
1240
Maciej Żenczykowski84e14fe2017-09-26 21:32:42 -07001241 case IPV6_FREEBIND:
1242 val = inet_sk(sk)->freebind;
1243 break;
1244
Balazs Scheidler6c468622010-10-21 16:08:28 +02001245 case IPV6_RECVORIGDSTADDR:
1246 val = np->rxopt.bits.rxorigdstaddr;
1247 break;
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -08001251 {
1252 struct dst_entry *dst;
1253
1254 if (optname == IPV6_UNICAST_HOPS)
1255 val = np->hop_limit;
1256 else
1257 val = np->mcast_hops;
1258
Eric Dumazetb6c67122010-04-08 23:03:29 +00001259 if (val < 0) {
1260 rcu_read_lock();
1261 dst = __sk_dst_get(sk);
1262 if (dst)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001263 val = ip6_dst_hoplimit(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001264 rcu_read_unlock();
Brian Haleybefffe92006-12-12 17:09:49 -08001265 }
Eric Dumazetb6c67122010-04-08 23:03:29 +00001266
Brian Haleybefffe92006-12-12 17:09:49 -08001267 if (val < 0)
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001268 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 break;
Brian Haleybefffe92006-12-12 17:09:49 -08001270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 case IPV6_MULTICAST_LOOP:
1273 val = np->mc_loop;
1274 break;
1275
1276 case IPV6_MULTICAST_IF:
1277 val = np->mcast_oif;
1278 break;
1279
Andre Naujoks15033f02018-09-10 10:27:15 +02001280 case IPV6_MULTICAST_ALL:
1281 val = np->mc_all;
1282 break;
1283
Erich E. Hooverc4062df2012-02-08 09:11:08 +00001284 case IPV6_UNICAST_IF:
1285 val = (__force int)htonl((__u32) np->ucast_oif);
1286 break;
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 case IPV6_MTU_DISCOVER:
1289 val = np->pmtudisc;
1290 break;
1291
1292 case IPV6_RECVERR:
1293 val = np->recverr;
1294 break;
1295
1296 case IPV6_FLOWINFO_SEND:
1297 val = np->sndflow;
1298 break;
1299
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001300 case IPV6_FLOWLABEL_MGR:
1301 {
1302 struct in6_flowlabel_req freq;
Florent Fourcot46e5f402014-01-17 17:15:04 +01001303 int flags;
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001304
1305 if (len < sizeof(freq))
1306 return -EINVAL;
1307
1308 if (copy_from_user(&freq, optval, sizeof(freq)))
1309 return -EFAULT;
1310
1311 if (freq.flr_action != IPV6_FL_A_GET)
1312 return -EINVAL;
1313
1314 len = sizeof(freq);
Florent Fourcot46e5f402014-01-17 17:15:04 +01001315 flags = freq.flr_flags;
1316
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001317 memset(&freq, 0, sizeof(freq));
1318
Florent Fourcot46e5f402014-01-17 17:15:04 +01001319 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001320 if (val < 0)
1321 return val;
1322
1323 if (put_user(len, optlen))
1324 return -EFAULT;
1325 if (copy_to_user(optval, &freq, len))
1326 return -EFAULT;
1327
1328 return 0;
1329 }
1330
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001331 case IPV6_ADDR_PREFERENCES:
1332 val = 0;
1333
1334 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1335 val |= IPV6_PREFER_SRC_TMP;
1336 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1337 val |= IPV6_PREFER_SRC_PUBLIC;
1338 else {
1339 /* XXX: should we return system default? */
1340 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1341 }
1342
1343 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1344 val |= IPV6_PREFER_SRC_COA;
1345 else
1346 val |= IPV6_PREFER_SRC_HOME;
1347 break;
1348
Stephen Hemmingere802af92010-04-22 15:24:53 -07001349 case IPV6_MINHOPCOUNT:
1350 val = np->min_hopcount;
1351 break;
1352
Brian Haley793b14732010-04-23 11:26:07 +00001353 case IPV6_DONTFRAG:
1354 val = np->dontfrag;
1355 break;
1356
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001357 case IPV6_AUTOFLOWLABEL:
Ben Hutchingse9191ff2018-01-22 20:06:42 +00001358 val = ip6_autoflowlabel(sock_net(sk), np);
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001359 break;
1360
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -04001361 case IPV6_RECVFRAGSIZE:
1362 val = np->rxopt.bits.recvfragsize;
1363 break;
1364
Francesco Ruggeri9036b2f2019-03-01 15:31:03 -08001365 case IPV6_ROUTER_ALERT_ISOLATE:
1366 val = np->rtalert_isolate;
1367 break;
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001370 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372 len = min_t(unsigned int, sizeof(int), len);
Ian Morris67ba4152014-08-24 21:53:10 +01001373 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001375 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return -EFAULT;
1377 return 0;
1378}
1379
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001380int ipv6_getsockopt(struct sock *sk, int level, int optname,
1381 char __user *optval, int __user *optlen)
1382{
1383 int err;
1384
1385 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1386 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1387
Ian Morris67ba4152014-08-24 21:53:10 +01001388 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001389 return -ENOPROTOOPT;
1390
Daniel Baluta98e77432011-08-19 03:19:07 -07001391 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001392#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001393 /* we need to exclude all possible ENOPROTOOPTs except default case */
1394 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001395 int len;
1396
1397 if (get_user(len, optlen))
1398 return -EFAULT;
1399
Paolo Abeni01ea3062018-02-08 12:19:00 +01001400 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001401 if (err >= 0)
1402 err = put_user(len, optlen);
1403 }
1404#endif
1405 return err;
1406}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001407EXPORT_SYMBOL(ipv6_getsockopt);
1408
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001409#ifdef CONFIG_COMPAT
1410int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001411 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001412{
1413 int err;
1414
1415 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001416 if (udp_prot.compat_getsockopt != NULL)
1417 return udp_prot.compat_getsockopt(sk, level, optname,
1418 optval, optlen);
1419 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001420 }
1421
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001422 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001423 return -ENOPROTOOPT;
1424
Pavel Emelyanov80991792008-04-30 14:49:54 -07001425 if (optname == MCAST_MSFILTER)
1426 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1427 ipv6_getsockopt);
1428
Daniel Baluta98e77432011-08-19 03:19:07 -07001429 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1430 MSG_CMSG_COMPAT);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001431#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001432 /* we need to exclude all possible ENOPROTOOPTs except default case */
1433 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001434 int len;
1435
1436 if (get_user(len, optlen))
1437 return -EFAULT;
1438
Paolo Abeni01ea3062018-02-08 12:19:00 +01001439 err = compat_nf_getsockopt(sk, PF_INET6, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001440 if (err >= 0)
1441 err = put_user(len, optlen);
1442 }
1443#endif
1444 return err;
1445}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001446EXPORT_SYMBOL(compat_ipv6_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001447#endif