blob: 973e215c3114a3f4b5af51dc44e593b0168e2ef3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 BSD socket options interface
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Based on linux/net/ipv4/ip_sockglue.c
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 * FIXME: Make the setsockopt code POSIX compliant: That is
16 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * o Truncate getsockopt returns
18 * o Return an optlen of the truncated length if need be
19 *
20 * Changes:
21 * David L Stevens <dlstevens@us.ibm.com>:
22 * - added multicast source filtering API for MLDv2
23 */
24
25#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080026#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/types.h>
29#include <linux/socket.h>
30#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/net.h>
32#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090033#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/netdevice.h>
35#include <linux/if_arp.h>
36#include <linux/init.h>
37#include <linux/sysctl.h>
38#include <linux/netfilter.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <net/sock.h>
42#include <net/snmp.h>
43#include <net/ipv6.h>
44#include <net/ndisc.h>
45#include <net/protocol.h>
46#include <net/transp_v6.h>
47#include <net/ip6_route.h>
48#include <net/addrconf.h>
49#include <net/inet_common.h>
50#include <net/tcp.h>
51#include <net/udp.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080052#include <net/udplite.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <net/xfrm.h>
David L Stevensdae50292008-04-27 01:06:07 -070054#include <net/compat.h>
David Lebruna149e7c2016-11-08 14:59:21 +010055#include <net/seg6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080057#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059struct ip6_ra_chain *ip6_ra_chain;
60DEFINE_RWLOCK(ip6_ra_lock);
61
Denis V. Lunev725a8ff2008-07-19 00:28:58 -070062int ip6_ra_control(struct sock *sk, int sel)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 struct ip6_ra_chain *ra, *new_ra, **rap;
65
66 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
Eric Dumazetc720c7e82009-10-15 06:30:45 +000067 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num != IPPROTO_RAW)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +090068 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Ian Morris67ba4152014-08-24 21:53:10 +010070 new_ra = (sel >= 0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 write_lock_bh(&ip6_ra_lock);
Ian Morris67ba4152014-08-24 21:53:10 +010073 for (rap = &ip6_ra_chain; (ra = *rap) != NULL; rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (ra->sk == sk) {
Ian Morris67ba4152014-08-24 21:53:10 +010075 if (sel >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 write_unlock_bh(&ip6_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -080077 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return -EADDRINUSE;
79 }
80
81 *rap = ra->next;
82 write_unlock_bh(&ip6_ra_lock);
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 sock_put(sk);
85 kfree(ra);
86 return 0;
87 }
88 }
Ian Morris63159f22015-03-29 14:00:04 +010089 if (!new_ra) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 write_unlock_bh(&ip6_ra_lock);
91 return -ENOBUFS;
92 }
93 new_ra->sk = sk;
94 new_ra->sel = sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 new_ra->next = ra;
96 *rap = new_ra;
97 sock_hold(sk);
98 write_unlock_bh(&ip6_ra_lock);
99 return 0;
100}
101
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700102struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
103 struct ipv6_txoptions *opt)
104{
105 if (inet_sk(sk)->is_icsk) {
106 if (opt &&
107 !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000108 inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) {
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700109 struct inet_connection_sock *icsk = inet_csk(sk);
110 icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
111 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
112 }
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700113 }
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800114 opt = xchg((__force struct ipv6_txoptions **)&inet6_sk(sk)->opt,
115 opt);
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700116 sk_dst_reset(sk);
117
118 return opt;
119}
120
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300121static bool setsockopt_needs_rtnl(int optname)
122{
123 switch (optname) {
WANG Cong8651be82016-10-19 23:35:12 -0700124 case IPV6_ADDRFORM:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300125 case IPV6_ADD_MEMBERSHIP:
126 case IPV6_DROP_MEMBERSHIP:
Marcelo Ricardo Leitnerc4a68532015-03-20 11:37:17 -0300127 case IPV6_JOIN_ANYCAST:
128 case IPV6_LEAVE_ANYCAST:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300129 case MCAST_JOIN_GROUP:
130 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300131 case MCAST_JOIN_SOURCE_GROUP:
132 case MCAST_LEAVE_SOURCE_GROUP:
133 case MCAST_BLOCK_SOURCE:
134 case MCAST_UNBLOCK_SOURCE:
135 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300136 return true;
137 }
138 return false;
139}
140
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800141static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700142 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900145 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 int val, valbool;
147 int retv = -ENOPROTOOPT;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300148 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Ian Morris63159f22015-03-29 14:00:04 +0100150 if (!optval)
Ian Morris67ba4152014-08-24 21:53:10 +0100151 val = 0;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900152 else {
153 if (optlen >= sizeof(int)) {
154 if (get_user(val, (int __user *) optval))
155 return -EFAULT;
156 } else
157 val = 0;
158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Ian Morris67ba4152014-08-24 21:53:10 +0100160 valbool = (val != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900162 if (ip6_mroute_opt(optname))
163 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
164
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300165 if (needs_rtnl)
166 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 lock_sock(sk);
168
169 switch (optname) {
170
171 case IPV6_ADDRFORM:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900172 if (optlen < sizeof(int))
173 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (val == PF_INET) {
175 struct ipv6_txoptions *opt;
176 struct sk_buff *pktopt;
177
Denis V. Lunev49d074f2008-06-04 15:49:06 +0400178 if (sk->sk_type == SOCK_RAW)
179 break;
180
Denis V. Lunev9596cc822008-06-04 15:49:08 +0400181 if (sk->sk_protocol == IPPROTO_UDP ||
182 sk->sk_protocol == IPPROTO_UDPLITE) {
183 struct udp_sock *up = udp_sk(sk);
184 if (up->pending == AF_INET6) {
185 retv = -EBUSY;
186 break;
187 }
188 } else if (sk->sk_protocol != IPPROTO_TCP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 break;
190
191 if (sk->sk_state != TCP_ESTABLISHED) {
192 retv = -ENOTCONN;
193 break;
194 }
195
196 if (ipv6_only_sock(sk) ||
Eric Dumazetefe42082013-10-03 15:42:29 -0700197 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 retv = -EADDRNOTAVAIL;
199 break;
200 }
201
202 fl6_free_socklist(sk);
WANG Cong8651be82016-10-19 23:35:12 -0700203 __ipv6_sock_mc_close(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700205 /*
206 * Sock is moving from IPv6 to IPv4 (sk_prot), so
207 * remove it from the refcnt debug socks count in the
208 * original family...
209 */
210 sk_refcnt_debug_dec(sk);
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (sk->sk_protocol == IPPROTO_TCP) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800213 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700215 sock_prot_inuse_add(net, sk->sk_prot, -1);
216 sock_prot_inuse_add(net, &tcp_prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 local_bh_enable();
218 sk->sk_prot = &tcp_prot;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800219 icsk->icsk_af_ops = &ipv4_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 sk->sk_socket->ops = &inet_stream_ops;
221 sk->sk_family = PF_INET;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800222 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 } else {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800224 struct proto *prot = &udp_prot;
225
David S. Millerdb8dac22008-03-06 16:22:02 -0800226 if (sk->sk_protocol == IPPROTO_UDPLITE)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800227 prot = &udplite_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700229 sock_prot_inuse_add(net, sk->sk_prot, -1);
230 sock_prot_inuse_add(net, prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 local_bh_enable();
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800232 sk->sk_prot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 sk->sk_socket->ops = &inet_dgram_ops;
234 sk->sk_family = PF_INET;
235 }
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800236 opt = xchg((__force struct ipv6_txoptions **)&np->opt,
237 NULL);
238 if (opt) {
239 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
240 txopt_put(opt);
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 pktopt = xchg(&np->pktoptions, NULL);
Wei Yongjun800d55f2009-02-23 21:45:33 +0000243 kfree_skb(pktopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700245 /*
246 * ... and add it to the refcnt debug socks count
247 * in the new family. -acme
248 */
249 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 module_put(THIS_MODULE);
251 retv = 0;
252 break;
253 }
254 goto e_inval;
255
256 case IPV6_V6ONLY:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900257 if (optlen < sizeof(int) ||
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000258 inet_sk(sk)->inet_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 goto e_inval;
Eric Dumazet9fe516b2014-06-27 08:36:16 -0700260 sk->sk_ipv6only = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 retv = 0;
262 break;
263
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900264 case IPV6_RECVPKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900265 if (optlen < sizeof(int))
266 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 np->rxopt.bits.rxinfo = valbool;
268 retv = 0;
269 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900270
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900271 case IPV6_2292PKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900272 if (optlen < sizeof(int))
273 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900274 np->rxopt.bits.rxoinfo = valbool;
275 retv = 0;
276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900278 case IPV6_RECVHOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900279 if (optlen < sizeof(int))
280 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 np->rxopt.bits.rxhlim = valbool;
282 retv = 0;
283 break;
284
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900285 case IPV6_2292HOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900286 if (optlen < sizeof(int))
287 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900288 np->rxopt.bits.rxohlim = valbool;
289 retv = 0;
290 break;
291
292 case IPV6_RECVRTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900293 if (optlen < sizeof(int))
294 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900295 np->rxopt.bits.srcrt = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 retv = 0;
297 break;
298
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900299 case IPV6_2292RTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900300 if (optlen < sizeof(int))
301 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900302 np->rxopt.bits.osrcrt = valbool;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900303 retv = 0;
304 break;
305
306 case IPV6_RECVHOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900307 if (optlen < sizeof(int))
308 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 np->rxopt.bits.hopopts = valbool;
310 retv = 0;
311 break;
312
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900313 case IPV6_2292HOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900314 if (optlen < sizeof(int))
315 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900316 np->rxopt.bits.ohopopts = valbool;
317 retv = 0;
318 break;
319
320 case IPV6_RECVDSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900321 if (optlen < sizeof(int))
322 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 np->rxopt.bits.dstopts = valbool;
324 retv = 0;
325 break;
326
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900327 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900328 if (optlen < sizeof(int))
329 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900330 np->rxopt.bits.odstopts = valbool;
331 retv = 0;
332 break;
333
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900334 case IPV6_TCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900335 if (optlen < sizeof(int))
336 goto e_inval;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700337 if (val < -1 || val > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900338 goto e_inval;
Gerrit Renker26ced1e2009-08-09 08:12:49 +0000339 /* RFC 3542, 6.5: default traffic class of 0x0 */
340 if (val == -1)
341 val = 0;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900342 np->tclass = val;
343 retv = 0;
344 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900345
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900346 case IPV6_RECVTCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900347 if (optlen < sizeof(int))
348 goto e_inval;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900349 np->rxopt.bits.rxtclass = valbool;
350 retv = 0;
351 break;
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 case IPV6_FLOWINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900354 if (optlen < sizeof(int))
355 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 np->rxopt.bits.rxflow = valbool;
357 retv = 0;
358 break;
359
Brian Haley793b14732010-04-23 11:26:07 +0000360 case IPV6_RECVPATHMTU:
361 if (optlen < sizeof(int))
362 goto e_inval;
363 np->rxopt.bits.rxpmtu = valbool;
364 retv = 0;
365 break;
366
Balazs Scheidler6c468622010-10-21 16:08:28 +0200367 case IPV6_TRANSPARENT:
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000368 if (valbool && !ns_capable(net->user_ns, CAP_NET_ADMIN) &&
369 !ns_capable(net->user_ns, CAP_NET_RAW)) {
Balazs Scheidlerb8894162010-10-23 04:48:14 +0000370 retv = -EPERM;
371 break;
372 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200373 if (optlen < sizeof(int))
374 goto e_inval;
375 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
376 inet_sk(sk)->transparent = valbool;
377 retv = 0;
378 break;
379
Maciej Żenczykowski84e14fe2017-09-26 21:32:42 -0700380 case IPV6_FREEBIND:
381 if (optlen < sizeof(int))
382 goto e_inval;
383 /* we also don't have a separate freebind bit for IPV6 */
384 inet_sk(sk)->freebind = valbool;
385 retv = 0;
386 break;
387
Balazs Scheidler6c468622010-10-21 16:08:28 +0200388 case IPV6_RECVORIGDSTADDR:
389 if (optlen < sizeof(int))
390 goto e_inval;
391 np->rxopt.bits.rxorigdstaddr = valbool;
392 retv = 0;
393 break;
394
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900395 case IPV6_HOPOPTS:
396 case IPV6_RTHDRDSTOPTS:
397 case IPV6_RTHDR:
398 case IPV6_DSTOPTS:
399 {
400 struct ipv6_txoptions *opt;
Paul Moorea9ba23d2018-07-04 09:58:05 -0400401 struct ipv6_opt_hdr *new = NULL;
402
403 /* hop-by-hop / destination options are privileged option */
404 retv = -EPERM;
405 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
406 break;
Shan Weiaea74272008-06-19 16:29:39 -0700407
408 /* remove any sticky options header with a zero option
409 * length, per RFC3542.
410 */
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900411 if (optlen == 0)
Luiz Capitulinocb422c42005-11-14 21:43:36 -0800412 optval = NULL;
Ian Morris63159f22015-03-29 14:00:04 +0100413 else if (!optval)
Yang Hongyangcfb266c2008-08-03 18:16:15 -0700414 goto e_inval;
Shan Weiaea74272008-06-19 16:29:39 -0700415 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
416 optlen & 0x7 || optlen > 8 * 255)
417 goto e_inval;
Paul Moorea9ba23d2018-07-04 09:58:05 -0400418 else {
419 new = memdup_user(optval, optlen);
420 if (IS_ERR(new)) {
421 retv = PTR_ERR(new);
422 break;
423 }
424 if (unlikely(ipv6_optlen(new) > optlen)) {
425 kfree(new);
426 goto e_inval;
427 }
428 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900429
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200430 opt = rcu_dereference_protected(np->opt,
431 lockdep_sock_is_held(sk));
Paul Moorea9ba23d2018-07-04 09:58:05 -0400432 opt = ipv6_renew_options(sk, opt, optname, new);
433 kfree(new);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900434 if (IS_ERR(opt)) {
435 retv = PTR_ERR(opt);
436 break;
437 }
438
439 /* routing header option needs extra check */
Brian Haley6e093d92008-11-12 22:59:21 -0800440 retv = -EINVAL;
Olaf Kirchdfee0a72007-03-09 13:55:38 -0800441 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900442 struct ipv6_rt_hdr *rthdr = opt->srcrt;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700443 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000444#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700445 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800446 if (rthdr->hdrlen != 2 ||
447 rthdr->segments_left != 1)
448 goto sticky_done;
449
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700450 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700451#endif
David Lebruna149e7c2016-11-08 14:59:21 +0100452 case IPV6_SRCRT_TYPE_4:
453 {
454 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)
455 opt->srcrt;
456
457 if (!seg6_validate_srh(srh, optlen))
458 goto sticky_done;
459 break;
460 }
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700461 default:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900462 goto sticky_done;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700463 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900464 }
465
466 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700467 opt = ipv6_update_options(sk, opt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900468sticky_done:
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800469 if (opt) {
470 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
471 txopt_put(opt);
472 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900473 break;
474 }
475
Yang Hongyangb24a2512008-12-16 02:06:23 -0800476 case IPV6_PKTINFO:
477 {
478 struct in6_pktinfo pkt;
479
480 if (optlen == 0)
481 goto e_inval;
Ian Morris63159f22015-03-29 14:00:04 +0100482 else if (optlen < sizeof(struct in6_pktinfo) || !optval)
Yang Hongyangb24a2512008-12-16 02:06:23 -0800483 goto e_inval;
484
Ilpo Järvinen914d1162009-01-04 17:27:31 -0800485 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
Yang Hongyangb24a2512008-12-16 02:06:23 -0800486 retv = -EFAULT;
487 break;
488 }
Mike Manningd839a0e2018-11-07 15:36:08 +0000489 if (!sk_dev_equal_l3scope(sk, pkt.ipi6_ifindex))
Yang Hongyangb24a2512008-12-16 02:06:23 -0800490 goto e_inval;
491
492 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000493 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
Yang Hongyangb24a2512008-12-16 02:06:23 -0800494 retv = 0;
495 break;
496 }
497
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900498 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 {
500 struct ipv6_txoptions *opt = NULL;
501 struct msghdr msg;
David S. Miller4c9483b2011-03-12 16:22:43 -0500502 struct flowi6 fl6;
Wei Wang26879da2016-05-02 21:40:07 -0700503 struct ipcm6_cookie ipc6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
David S. Miller4c9483b2011-03-12 16:22:43 -0500505 memset(&fl6, 0, sizeof(fl6));
506 fl6.flowi6_oif = sk->sk_bound_dev_if;
507 fl6.flowi6_mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 if (optlen == 0)
510 goto update;
511
512 /* 1K is probably excessive
513 * 1K is surely not enough, 2K per standard header is 16K.
514 */
515 retv = -EINVAL;
516 if (optlen > 64*1024)
517 break;
518
519 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
520 retv = -ENOBUFS;
Ian Morris63159f22015-03-29 14:00:04 +0100521 if (!opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 break;
523
524 memset(opt, 0, sizeof(*opt));
Reshetova, Elena0aeea212017-07-04 09:34:54 +0300525 refcount_set(&opt->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 opt->tot_len = sizeof(*opt) + optlen;
527 retv = -EFAULT;
528 if (copy_from_user(opt+1, optval, optlen))
529 goto done;
530
531 msg.msg_controllen = optlen;
Ian Morris67ba4152014-08-24 21:53:10 +0100532 msg.msg_control = (void *)(opt+1);
Wei Wang26879da2016-05-02 21:40:07 -0700533 ipc6.opt = opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Willem de Bruijn5fdaa882018-07-06 10:12:57 -0400535 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (retv)
537 goto done;
538update:
539 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700540 opt = ipv6_update_options(sk, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541done:
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800542 if (opt) {
543 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
544 txopt_put(opt);
545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 break;
547 }
548 case IPV6_UNICAST_HOPS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900549 if (optlen < sizeof(int))
550 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (val > 255 || val < -1)
552 goto e_inval;
553 np->hop_limit = val;
554 retv = 0;
555 break;
556
557 case IPV6_MULTICAST_HOPS:
558 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900559 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900560 if (optlen < sizeof(int))
561 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (val > 255 || val < -1)
563 goto e_inval;
Li Wei2a38e6d2011-11-27 21:33:34 +0000564 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 retv = 0;
566 break;
567
568 case IPV6_MULTICAST_LOOP:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900569 if (optlen < sizeof(int))
570 goto e_inval;
YOSHIFUJI Hideaki28d44882008-06-12 03:14:51 +0900571 if (val != valbool)
572 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 np->mc_loop = valbool;
574 retv = 0;
575 break;
576
Erich E. Hooverc4062df2012-02-08 09:11:08 +0000577 case IPV6_UNICAST_IF:
578 {
579 struct net_device *dev = NULL;
580 int ifindex;
581
582 if (optlen != sizeof(int))
583 goto e_inval;
584
585 ifindex = (__force int)ntohl((__force __be32)val);
586 if (ifindex == 0) {
587 np->ucast_oif = 0;
588 retv = 0;
589 break;
590 }
591
592 dev = dev_get_by_index(net, ifindex);
593 retv = -EADDRNOTAVAIL;
594 if (!dev)
595 break;
596 dev_put(dev);
597
598 retv = -EINVAL;
599 if (sk->sk_bound_dev_if)
600 break;
601
602 np->ucast_oif = ifindex;
603 retv = 0;
604 break;
605 }
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 case IPV6_MULTICAST_IF:
608 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900609 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900610 if (optlen < sizeof(int))
611 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Brian Haley4953f0f2007-10-11 14:39:29 -0700613 if (val) {
Eric Dumazet55b80502009-10-19 06:41:58 +0000614 struct net_device *dev;
David Ahern7bb387c2016-12-29 15:39:37 -0800615 int midx;
Eric Dumazet55b80502009-10-19 06:41:58 +0000616
David Ahern7bb387c2016-12-29 15:39:37 -0800617 rcu_read_lock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700618
David Ahern7bb387c2016-12-29 15:39:37 -0800619 dev = dev_get_by_index_rcu(net, val);
Eric Dumazet55b80502009-10-19 06:41:58 +0000620 if (!dev) {
David Ahern7bb387c2016-12-29 15:39:37 -0800621 rcu_read_unlock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700622 retv = -ENODEV;
623 break;
624 }
David Ahern7bb387c2016-12-29 15:39:37 -0800625 midx = l3mdev_master_ifindex_rcu(dev);
626
627 rcu_read_unlock();
628
629 if (sk->sk_bound_dev_if &&
630 sk->sk_bound_dev_if != val &&
631 (!midx || midx != sk->sk_bound_dev_if))
632 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634 np->mcast_oif = val;
635 retv = 0;
636 break;
637 case IPV6_ADD_MEMBERSHIP:
638 case IPV6_DROP_MEMBERSHIP:
639 {
640 struct ipv6_mreq mreq;
641
Wang Chena28398b2008-04-07 09:42:07 +0800642 if (optlen < sizeof(struct ipv6_mreq))
643 goto e_inval;
644
Flavio Leitnera96fb492007-08-24 22:16:39 -0700645 retv = -EPROTO;
646 if (inet_sk(sk)->is_icsk)
647 break;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 retv = -EFAULT;
650 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
651 break;
652
653 if (optname == IPV6_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300654 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300656 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 break;
658 }
659 case IPV6_JOIN_ANYCAST:
660 case IPV6_LEAVE_ANYCAST:
661 {
662 struct ipv6_mreq mreq;
663
Wang Chena28398b2008-04-07 09:42:07 +0800664 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 goto e_inval;
666
667 retv = -EFAULT;
668 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
669 break;
670
671 if (optname == IPV6_JOIN_ANYCAST)
672 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
673 else
674 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
675 break;
676 }
Andre Naujoks15033f02018-09-10 10:27:15 +0200677 case IPV6_MULTICAST_ALL:
678 if (optlen < sizeof(int))
679 goto e_inval;
680 np->mc_all = valbool;
681 retv = 0;
682 break;
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 case MCAST_JOIN_GROUP:
685 case MCAST_LEAVE_GROUP:
686 {
687 struct group_req greq;
688 struct sockaddr_in6 *psin6;
689
Wang Chena28398b2008-04-07 09:42:07 +0800690 if (optlen < sizeof(struct group_req))
691 goto e_inval;
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 retv = -EFAULT;
694 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
695 break;
696 if (greq.gr_group.ss_family != AF_INET6) {
697 retv = -EADDRNOTAVAIL;
698 break;
699 }
700 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
701 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300702 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
703 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300705 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
706 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 break;
708 }
709 case MCAST_JOIN_SOURCE_GROUP:
710 case MCAST_LEAVE_SOURCE_GROUP:
711 case MCAST_BLOCK_SOURCE:
712 case MCAST_UNBLOCK_SOURCE:
713 {
714 struct group_source_req greqs;
715 int omode, add;
716
Wang Chena28398b2008-04-07 09:42:07 +0800717 if (optlen < sizeof(struct group_source_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 goto e_inval;
719 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
720 retv = -EFAULT;
721 break;
722 }
723 if (greqs.gsr_group.ss_family != AF_INET6 ||
724 greqs.gsr_source.ss_family != AF_INET6) {
725 retv = -EADDRNOTAVAIL;
726 break;
727 }
728 if (optname == MCAST_BLOCK_SOURCE) {
729 omode = MCAST_EXCLUDE;
730 add = 1;
731 } else if (optname == MCAST_UNBLOCK_SOURCE) {
732 omode = MCAST_EXCLUDE;
733 add = 0;
734 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
735 struct sockaddr_in6 *psin6;
736
737 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
Hangbin Liuc7ea20c2018-07-10 22:41:27 +0800738 retv = ipv6_sock_mc_join_ssm(sk, greqs.gsr_interface,
739 &psin6->sin6_addr,
740 MCAST_INCLUDE);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700741 /* prior join w/ different source is ok */
742 if (retv && retv != -EADDRINUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 break;
744 omode = MCAST_INCLUDE;
745 add = 1;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700746 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 omode = MCAST_INCLUDE;
748 add = 0;
749 }
750 retv = ip6_mc_source(add, omode, sk, &greqs);
751 break;
752 }
753 case MCAST_MSFILTER:
754 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 struct group_filter *gsf;
756
757 if (optlen < GROUP_FILTER_SIZE(0))
758 goto e_inval;
759 if (optlen > sysctl_optmem_max) {
760 retv = -ENOBUFS;
761 break;
762 }
Al Viro43727da2017-05-13 18:22:53 -0400763 gsf = memdup_user(optval, optlen);
764 if (IS_ERR(gsf)) {
765 retv = PTR_ERR(gsf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 break;
767 }
768 /* numsrc >= (4G-140)/128 overflow in 32 bits */
769 if (gsf->gf_numsrc >= 0x1ffffffU ||
770 gsf->gf_numsrc > sysctl_mld_max_msf) {
771 kfree(gsf);
772 retv = -ENOBUFS;
773 break;
774 }
775 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
776 kfree(gsf);
777 retv = -EINVAL;
778 break;
779 }
780 retv = ip6_mc_msfilter(sk, gsf);
781 kfree(gsf);
782
783 break;
784 }
785 case IPV6_ROUTER_ALERT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900786 if (optlen < sizeof(int))
787 goto e_inval;
Denis V. Lunev725a8ff2008-07-19 00:28:58 -0700788 retv = ip6_ra_control(sk, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 break;
790 case IPV6_MTU_DISCOVER:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900791 if (optlen < sizeof(int))
792 goto e_inval;
Hannes Frederic Sowa0b952272014-02-26 01:20:43 +0100793 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 goto e_inval;
795 np->pmtudisc = val;
796 retv = 0;
797 break;
798 case IPV6_MTU:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900799 if (optlen < sizeof(int))
800 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (val && val < IPV6_MIN_MTU)
802 goto e_inval;
803 np->frag_size = val;
804 retv = 0;
805 break;
806 case IPV6_RECVERR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900807 if (optlen < sizeof(int))
808 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 np->recverr = valbool;
810 if (!val)
811 skb_queue_purge(&sk->sk_error_queue);
812 retv = 0;
813 break;
814 case IPV6_FLOWINFO_SEND:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900815 if (optlen < sizeof(int))
816 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 np->sndflow = valbool;
818 retv = 0;
819 break;
820 case IPV6_FLOWLABEL_MGR:
821 retv = ipv6_flowlabel_opt(sk, optval, optlen);
822 break;
823 case IPV6_IPSEC_POLICY:
824 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700825 retv = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000826 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700827 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 retv = xfrm_user_policy(sk, optname, optval, optlen);
829 break;
830
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900831 case IPV6_ADDR_PREFERENCES:
832 {
833 unsigned int pref = 0;
834 unsigned int prefmask = ~0;
835
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900836 if (optlen < sizeof(int))
837 goto e_inval;
838
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900839 retv = -EINVAL;
840
841 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
842 switch (val & (IPV6_PREFER_SRC_PUBLIC|
843 IPV6_PREFER_SRC_TMP|
844 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
845 case IPV6_PREFER_SRC_PUBLIC:
846 pref |= IPV6_PREFER_SRC_PUBLIC;
847 break;
848 case IPV6_PREFER_SRC_TMP:
849 pref |= IPV6_PREFER_SRC_TMP;
850 break;
851 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
852 break;
853 case 0:
854 goto pref_skip_pubtmp;
855 default:
856 goto e_inval;
857 }
858
859 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
860 IPV6_PREFER_SRC_TMP);
861pref_skip_pubtmp:
862
863 /* check HOME/COA conflicts */
864 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
865 case IPV6_PREFER_SRC_HOME:
866 break;
867 case IPV6_PREFER_SRC_COA:
868 pref |= IPV6_PREFER_SRC_COA;
869 case 0:
870 goto pref_skip_coa;
871 default:
872 goto e_inval;
873 }
874
875 prefmask &= ~IPV6_PREFER_SRC_COA;
876pref_skip_coa:
877
878 /* check CGA/NONCGA conflicts */
879 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
880 case IPV6_PREFER_SRC_CGA:
881 case IPV6_PREFER_SRC_NONCGA:
882 case 0:
883 break;
884 default:
885 goto e_inval;
886 }
887
888 np->srcprefs = (np->srcprefs & prefmask) | pref;
889 retv = 0;
890
891 break;
892 }
Stephen Hemmingere802af92010-04-22 15:24:53 -0700893 case IPV6_MINHOPCOUNT:
894 if (optlen < sizeof(int))
895 goto e_inval;
896 if (val < 0 || val > 255)
897 goto e_inval;
898 np->min_hopcount = val;
Hannes Frederic Sowad4596ba2012-11-10 19:52:34 +0000899 retv = 0;
Brian Haley793b14732010-04-23 11:26:07 +0000900 break;
901 case IPV6_DONTFRAG:
902 np->dontfrag = valbool;
Stephen Hemmingere802af92010-04-22 15:24:53 -0700903 retv = 0;
904 break;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700905 case IPV6_AUTOFLOWLABEL:
906 np->autoflowlabel = valbool;
Shaohua Li513674b2017-12-20 12:10:21 -0800907 np->autoflowlabel_set = 1;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700908 retv = 0;
909 break;
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -0400910 case IPV6_RECVFRAGSIZE:
911 np->rxopt.bits.recvfragsize = valbool;
912 retv = 0;
913 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300917 if (needs_rtnl)
918 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return retv;
921
922e_inval:
923 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300924 if (needs_rtnl)
925 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return -EINVAL;
927}
928
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800929int ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700930 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800931{
932 int err;
933
934 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
935 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
936
937 if (level != SOL_IPV6)
938 return -ENOPROTOOPT;
939
940 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
941#ifdef CONFIG_NETFILTER
942 /* we need to exclude all possible ENOPROTOOPTs except default case */
943 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +0100944 optname != IPV6_XFRM_POLICY)
945 err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800946#endif
947 return err;
948}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900949EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800950
951#ifdef CONFIG_COMPAT
952int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700953 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800954{
955 int err;
956
957 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800958 if (udp_prot.compat_setsockopt != NULL)
959 return udp_prot.compat_setsockopt(sk, level, optname,
960 optval, optlen);
961 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800962 }
963
964 if (level != SOL_IPV6)
965 return -ENOPROTOOPT;
966
David L Stevensdae50292008-04-27 01:06:07 -0700967 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
968 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
969 ipv6_setsockopt);
970
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800971 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
972#ifdef CONFIG_NETFILTER
973 /* we need to exclude all possible ENOPROTOOPTs except default case */
974 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +0100975 optname != IPV6_XFRM_POLICY)
976 err = compat_nf_setsockopt(sk, PF_INET6, optname, optval,
977 optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800978#endif
979 return err;
980}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800981EXPORT_SYMBOL(compat_ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800982#endif
983
David S. Miller286930792007-03-07 12:50:46 -0800984static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700985 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900986{
David S. Miller286930792007-03-07 12:50:46 -0800987 struct ipv6_opt_hdr *hdr;
988
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700989 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900990 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700991
Ian Morris67ba4152014-08-24 21:53:10 +0100992 switch (optname) {
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700993 case IPV6_HOPOPTS:
994 hdr = opt->hopopt;
995 break;
996 case IPV6_RTHDRDSTOPTS:
997 hdr = opt->dst0opt;
998 break;
999 case IPV6_RTHDR:
1000 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
1001 break;
1002 case IPV6_DSTOPTS:
1003 hdr = opt->dst1opt;
1004 break;
1005 default:
1006 return -EINVAL; /* should not happen */
1007 }
1008
1009 if (!hdr)
1010 return 0;
David S. Miller286930792007-03-07 12:50:46 -08001011
Chris Wrightd2b02ed2007-03-09 16:19:17 -08001012 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -07001013 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001014 return -EFAULT;
Yang Hongyang95b496b2008-05-28 16:27:28 +08001015 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001016}
1017
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001018static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001019 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 struct ipv6_pinfo *np = inet6_sk(sk);
1022 int len;
1023 int val;
1024
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001025 if (ip6_mroute_opt(optname))
1026 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (get_user(len, optlen))
1029 return -EFAULT;
1030 switch (optname) {
1031 case IPV6_ADDRFORM:
1032 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001033 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 sk->sk_protocol != IPPROTO_TCP)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +09001035 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (sk->sk_state != TCP_ESTABLISHED)
1037 return -ENOTCONN;
1038 val = sk->sk_family;
1039 break;
1040 case MCAST_MSFILTER:
1041 {
1042 struct group_filter gsf;
1043 int err;
1044
1045 if (len < GROUP_FILTER_SIZE(0))
1046 return -EINVAL;
1047 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
1048 return -EFAULT;
YOSHIFUJI Hideaki20c61fbd2008-04-28 14:40:55 +09001049 if (gsf.gf_group.ss_family != AF_INET6)
1050 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 lock_sock(sk);
1052 err = ip6_mc_msfget(sk, &gsf,
1053 (struct group_filter __user *)optval, optlen);
1054 release_sock(sk);
1055 return err;
1056 }
1057
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001058 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 {
1060 struct msghdr msg;
1061 struct sk_buff *skb;
1062
1063 if (sk->sk_type != SOCK_STREAM)
1064 return -ENOPROTOOPT;
1065
1066 msg.msg_control = optval;
1067 msg.msg_controllen = len;
Daniel Baluta98e77432011-08-19 03:19:07 -07001068 msg.msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 lock_sock(sk);
1071 skb = np->pktoptions;
1072 if (skb)
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001073 ip6_datagram_recv_ctl(sk, &msg, skb);
Eric Dumazet1dc7b902015-01-21 03:45:42 -08001074 release_sock(sk);
1075 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (np->rxopt.bits.rxinfo) {
1077 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001078 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1079 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001080 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1082 }
1083 if (np->rxopt.bits.rxhlim) {
1084 int hlim = np->mcast_hops;
1085 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1086 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001087 if (np->rxopt.bits.rxtclass) {
Li RongQingd76ed222014-01-15 17:03:30 +08001088 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1089
Jiri Benc4c507d22012-02-09 09:35:49 +00001090 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1091 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001092 if (np->rxopt.bits.rxoinfo) {
1093 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001094 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1095 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001096 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1097 np->sticky_pktinfo.ipi6_addr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001098 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1099 }
1100 if (np->rxopt.bits.rxohlim) {
1101 int hlim = np->mcast_hops;
1102 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1103 }
Florent Fourcot1397ed32013-12-08 15:46:57 +01001104 if (np->rxopt.bits.rxflow) {
Florent Fourcot68536052013-12-12 17:07:58 +01001105 __be32 flowinfo = np->rcv_flowinfo;
1106
Florent Fourcot1397ed32013-12-08 15:46:57 +01001107 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110 len -= msg.msg_controllen;
1111 return put_user(len, optlen);
1112 }
1113 case IPV6_MTU:
1114 {
1115 struct dst_entry *dst;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001116
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001117 val = 0;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001118 rcu_read_lock();
1119 dst = __sk_dst_get(sk);
1120 if (dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 val = dst_mtu(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001122 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (!val)
1124 return -ENOTCONN;
1125 break;
1126 }
1127
1128 case IPV6_V6ONLY:
Eric Dumazet9fe516b2014-06-27 08:36:16 -07001129 val = sk->sk_ipv6only;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 break;
1131
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001132 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 val = np->rxopt.bits.rxinfo;
1134 break;
1135
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001136 case IPV6_2292PKTINFO:
1137 val = np->rxopt.bits.rxoinfo;
1138 break;
1139
1140 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 val = np->rxopt.bits.rxhlim;
1142 break;
1143
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001144 case IPV6_2292HOPLIMIT:
1145 val = np->rxopt.bits.rxohlim;
1146 break;
1147
1148 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 val = np->rxopt.bits.srcrt;
1150 break;
1151
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001152 case IPV6_2292RTHDR:
1153 val = np->rxopt.bits.osrcrt;
1154 break;
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001157 case IPV6_RTHDRDSTOPTS:
1158 case IPV6_RTHDR:
1159 case IPV6_DSTOPTS:
1160 {
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001161 struct ipv6_txoptions *opt;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001162
1163 lock_sock(sk);
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001164 opt = rcu_dereference_protected(np->opt,
1165 lockdep_sock_is_held(sk));
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001166 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001167 release_sock(sk);
Yang Hongyang05335c22008-05-28 16:23:47 +08001168 /* check if ipv6_getsockopt_sticky() returns err code */
1169 if (len < 0)
1170 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001171 return put_user(len, optlen);
1172 }
1173
1174 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 val = np->rxopt.bits.hopopts;
1176 break;
1177
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001178 case IPV6_2292HOPOPTS:
1179 val = np->rxopt.bits.ohopopts;
1180 break;
1181
1182 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 val = np->rxopt.bits.dstopts;
1184 break;
1185
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001186 case IPV6_2292DSTOPTS:
1187 val = np->rxopt.bits.odstopts;
1188 break;
1189
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001190 case IPV6_TCLASS:
1191 val = np->tclass;
1192 break;
1193
1194 case IPV6_RECVTCLASS:
1195 val = np->rxopt.bits.rxtclass;
1196 break;
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 case IPV6_FLOWINFO:
1199 val = np->rxopt.bits.rxflow;
1200 break;
1201
Brian Haley793b14732010-04-23 11:26:07 +00001202 case IPV6_RECVPATHMTU:
1203 val = np->rxopt.bits.rxpmtu;
1204 break;
1205
1206 case IPV6_PATHMTU:
1207 {
1208 struct dst_entry *dst;
1209 struct ip6_mtuinfo mtuinfo;
1210
1211 if (len < sizeof(mtuinfo))
1212 return -EINVAL;
1213
1214 len = sizeof(mtuinfo);
1215 memset(&mtuinfo, 0, sizeof(mtuinfo));
1216
1217 rcu_read_lock();
1218 dst = __sk_dst_get(sk);
1219 if (dst)
1220 mtuinfo.ip6m_mtu = dst_mtu(dst);
1221 rcu_read_unlock();
1222 if (!mtuinfo.ip6m_mtu)
1223 return -ENOTCONN;
1224
1225 if (put_user(len, optlen))
1226 return -EFAULT;
1227 if (copy_to_user(optval, &mtuinfo, len))
1228 return -EFAULT;
1229
1230 return 0;
Brian Haley793b14732010-04-23 11:26:07 +00001231 }
1232
Balazs Scheidler6c468622010-10-21 16:08:28 +02001233 case IPV6_TRANSPARENT:
1234 val = inet_sk(sk)->transparent;
1235 break;
1236
Maciej Żenczykowski84e14fe2017-09-26 21:32:42 -07001237 case IPV6_FREEBIND:
1238 val = inet_sk(sk)->freebind;
1239 break;
1240
Balazs Scheidler6c468622010-10-21 16:08:28 +02001241 case IPV6_RECVORIGDSTADDR:
1242 val = np->rxopt.bits.rxorigdstaddr;
1243 break;
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -08001247 {
1248 struct dst_entry *dst;
1249
1250 if (optname == IPV6_UNICAST_HOPS)
1251 val = np->hop_limit;
1252 else
1253 val = np->mcast_hops;
1254
Eric Dumazetb6c67122010-04-08 23:03:29 +00001255 if (val < 0) {
1256 rcu_read_lock();
1257 dst = __sk_dst_get(sk);
1258 if (dst)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001259 val = ip6_dst_hoplimit(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001260 rcu_read_unlock();
Brian Haleybefffe92006-12-12 17:09:49 -08001261 }
Eric Dumazetb6c67122010-04-08 23:03:29 +00001262
Brian Haleybefffe92006-12-12 17:09:49 -08001263 if (val < 0)
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001264 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 break;
Brian Haleybefffe92006-12-12 17:09:49 -08001266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 case IPV6_MULTICAST_LOOP:
1269 val = np->mc_loop;
1270 break;
1271
1272 case IPV6_MULTICAST_IF:
1273 val = np->mcast_oif;
1274 break;
1275
Andre Naujoks15033f02018-09-10 10:27:15 +02001276 case IPV6_MULTICAST_ALL:
1277 val = np->mc_all;
1278 break;
1279
Erich E. Hooverc4062df2012-02-08 09:11:08 +00001280 case IPV6_UNICAST_IF:
1281 val = (__force int)htonl((__u32) np->ucast_oif);
1282 break;
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 case IPV6_MTU_DISCOVER:
1285 val = np->pmtudisc;
1286 break;
1287
1288 case IPV6_RECVERR:
1289 val = np->recverr;
1290 break;
1291
1292 case IPV6_FLOWINFO_SEND:
1293 val = np->sndflow;
1294 break;
1295
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001296 case IPV6_FLOWLABEL_MGR:
1297 {
1298 struct in6_flowlabel_req freq;
Florent Fourcot46e5f402014-01-17 17:15:04 +01001299 int flags;
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001300
1301 if (len < sizeof(freq))
1302 return -EINVAL;
1303
1304 if (copy_from_user(&freq, optval, sizeof(freq)))
1305 return -EFAULT;
1306
1307 if (freq.flr_action != IPV6_FL_A_GET)
1308 return -EINVAL;
1309
1310 len = sizeof(freq);
Florent Fourcot46e5f402014-01-17 17:15:04 +01001311 flags = freq.flr_flags;
1312
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001313 memset(&freq, 0, sizeof(freq));
1314
Florent Fourcot46e5f402014-01-17 17:15:04 +01001315 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001316 if (val < 0)
1317 return val;
1318
1319 if (put_user(len, optlen))
1320 return -EFAULT;
1321 if (copy_to_user(optval, &freq, len))
1322 return -EFAULT;
1323
1324 return 0;
1325 }
1326
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001327 case IPV6_ADDR_PREFERENCES:
1328 val = 0;
1329
1330 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1331 val |= IPV6_PREFER_SRC_TMP;
1332 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1333 val |= IPV6_PREFER_SRC_PUBLIC;
1334 else {
1335 /* XXX: should we return system default? */
1336 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1337 }
1338
1339 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1340 val |= IPV6_PREFER_SRC_COA;
1341 else
1342 val |= IPV6_PREFER_SRC_HOME;
1343 break;
1344
Stephen Hemmingere802af92010-04-22 15:24:53 -07001345 case IPV6_MINHOPCOUNT:
1346 val = np->min_hopcount;
1347 break;
1348
Brian Haley793b14732010-04-23 11:26:07 +00001349 case IPV6_DONTFRAG:
1350 val = np->dontfrag;
1351 break;
1352
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001353 case IPV6_AUTOFLOWLABEL:
Ben Hutchingse9191ff2018-01-22 20:06:42 +00001354 val = ip6_autoflowlabel(sock_net(sk), np);
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001355 break;
1356
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -04001357 case IPV6_RECVFRAGSIZE:
1358 val = np->rxopt.bits.recvfragsize;
1359 break;
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001362 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364 len = min_t(unsigned int, sizeof(int), len);
Ian Morris67ba4152014-08-24 21:53:10 +01001365 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001367 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return -EFAULT;
1369 return 0;
1370}
1371
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001372int ipv6_getsockopt(struct sock *sk, int level, int optname,
1373 char __user *optval, int __user *optlen)
1374{
1375 int err;
1376
1377 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1378 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1379
Ian Morris67ba4152014-08-24 21:53:10 +01001380 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001381 return -ENOPROTOOPT;
1382
Daniel Baluta98e77432011-08-19 03:19:07 -07001383 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001384#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001385 /* we need to exclude all possible ENOPROTOOPTs except default case */
1386 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001387 int len;
1388
1389 if (get_user(len, optlen))
1390 return -EFAULT;
1391
Paolo Abeni01ea3062018-02-08 12:19:00 +01001392 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001393 if (err >= 0)
1394 err = put_user(len, optlen);
1395 }
1396#endif
1397 return err;
1398}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001399EXPORT_SYMBOL(ipv6_getsockopt);
1400
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001401#ifdef CONFIG_COMPAT
1402int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001403 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001404{
1405 int err;
1406
1407 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001408 if (udp_prot.compat_getsockopt != NULL)
1409 return udp_prot.compat_getsockopt(sk, level, optname,
1410 optval, optlen);
1411 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001412 }
1413
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001414 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001415 return -ENOPROTOOPT;
1416
Pavel Emelyanov80991792008-04-30 14:49:54 -07001417 if (optname == MCAST_MSFILTER)
1418 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1419 ipv6_getsockopt);
1420
Daniel Baluta98e77432011-08-19 03:19:07 -07001421 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1422 MSG_CMSG_COMPAT);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001423#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001424 /* we need to exclude all possible ENOPROTOOPTs except default case */
1425 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001426 int len;
1427
1428 if (get_user(len, optlen))
1429 return -EFAULT;
1430
Paolo Abeni01ea3062018-02-08 12:19:00 +01001431 err = compat_nf_getsockopt(sk, PF_INET6, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001432 if (err >= 0)
1433 err = put_user(len, optlen);
1434 }
1435#endif
1436 return err;
1437}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001438EXPORT_SYMBOL(compat_ipv6_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001439#endif