blob: 568ca4187cd101e745988ee262f79431ef8d28cc [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 }
489 if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if)
490 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;
Soheil Hassas Yeganehad1e46a2016-04-02 23:08:11 -0400503 struct sockcm_cookie sockc_junk;
Wei Wang26879da2016-05-02 21:40:07 -0700504 struct ipcm6_cookie ipc6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
David S. Miller4c9483b2011-03-12 16:22:43 -0500506 memset(&fl6, 0, sizeof(fl6));
507 fl6.flowi6_oif = sk->sk_bound_dev_if;
508 fl6.flowi6_mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (optlen == 0)
511 goto update;
512
513 /* 1K is probably excessive
514 * 1K is surely not enough, 2K per standard header is 16K.
515 */
516 retv = -EINVAL;
517 if (optlen > 64*1024)
518 break;
519
520 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
521 retv = -ENOBUFS;
Ian Morris63159f22015-03-29 14:00:04 +0100522 if (!opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 break;
524
525 memset(opt, 0, sizeof(*opt));
Reshetova, Elena0aeea212017-07-04 09:34:54 +0300526 refcount_set(&opt->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 opt->tot_len = sizeof(*opt) + optlen;
528 retv = -EFAULT;
529 if (copy_from_user(opt+1, optval, optlen))
530 goto done;
531
532 msg.msg_controllen = optlen;
Ian Morris67ba4152014-08-24 21:53:10 +0100533 msg.msg_control = (void *)(opt+1);
Wei Wang26879da2016-05-02 21:40:07 -0700534 ipc6.opt = opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Wei Wang26879da2016-05-02 21:40:07 -0700536 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6, &sockc_junk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (retv)
538 goto done;
539update:
540 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700541 opt = ipv6_update_options(sk, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542done:
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800543 if (opt) {
544 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
545 txopt_put(opt);
546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 break;
548 }
549 case IPV6_UNICAST_HOPS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900550 if (optlen < sizeof(int))
551 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (val > 255 || val < -1)
553 goto e_inval;
554 np->hop_limit = val;
555 retv = 0;
556 break;
557
558 case IPV6_MULTICAST_HOPS:
559 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900560 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900561 if (optlen < sizeof(int))
562 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (val > 255 || val < -1)
564 goto e_inval;
Li Wei2a38e6d2011-11-27 21:33:34 +0000565 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 retv = 0;
567 break;
568
569 case IPV6_MULTICAST_LOOP:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900570 if (optlen < sizeof(int))
571 goto e_inval;
YOSHIFUJI Hideaki28d44882008-06-12 03:14:51 +0900572 if (val != valbool)
573 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 np->mc_loop = valbool;
575 retv = 0;
576 break;
577
Erich E. Hooverc4062df2012-02-08 09:11:08 +0000578 case IPV6_UNICAST_IF:
579 {
580 struct net_device *dev = NULL;
581 int ifindex;
582
583 if (optlen != sizeof(int))
584 goto e_inval;
585
586 ifindex = (__force int)ntohl((__force __be32)val);
587 if (ifindex == 0) {
588 np->ucast_oif = 0;
589 retv = 0;
590 break;
591 }
592
593 dev = dev_get_by_index(net, ifindex);
594 retv = -EADDRNOTAVAIL;
595 if (!dev)
596 break;
597 dev_put(dev);
598
599 retv = -EINVAL;
600 if (sk->sk_bound_dev_if)
601 break;
602
603 np->ucast_oif = ifindex;
604 retv = 0;
605 break;
606 }
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 case IPV6_MULTICAST_IF:
609 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900610 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900611 if (optlen < sizeof(int))
612 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Brian Haley4953f0f2007-10-11 14:39:29 -0700614 if (val) {
Eric Dumazet55b80502009-10-19 06:41:58 +0000615 struct net_device *dev;
David Ahern7bb387c2016-12-29 15:39:37 -0800616 int midx;
Eric Dumazet55b80502009-10-19 06:41:58 +0000617
David Ahern7bb387c2016-12-29 15:39:37 -0800618 rcu_read_lock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700619
David Ahern7bb387c2016-12-29 15:39:37 -0800620 dev = dev_get_by_index_rcu(net, val);
Eric Dumazet55b80502009-10-19 06:41:58 +0000621 if (!dev) {
David Ahern7bb387c2016-12-29 15:39:37 -0800622 rcu_read_unlock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700623 retv = -ENODEV;
624 break;
625 }
David Ahern7bb387c2016-12-29 15:39:37 -0800626 midx = l3mdev_master_ifindex_rcu(dev);
627
628 rcu_read_unlock();
629
630 if (sk->sk_bound_dev_if &&
631 sk->sk_bound_dev_if != val &&
632 (!midx || midx != sk->sk_bound_dev_if))
633 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635 np->mcast_oif = val;
636 retv = 0;
637 break;
638 case IPV6_ADD_MEMBERSHIP:
639 case IPV6_DROP_MEMBERSHIP:
640 {
641 struct ipv6_mreq mreq;
642
Wang Chena28398b2008-04-07 09:42:07 +0800643 if (optlen < sizeof(struct ipv6_mreq))
644 goto e_inval;
645
Flavio Leitnera96fb492007-08-24 22:16:39 -0700646 retv = -EPROTO;
647 if (inet_sk(sk)->is_icsk)
648 break;
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 retv = -EFAULT;
651 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
652 break;
653
654 if (optname == IPV6_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300655 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300657 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
659 }
660 case IPV6_JOIN_ANYCAST:
661 case IPV6_LEAVE_ANYCAST:
662 {
663 struct ipv6_mreq mreq;
664
Wang Chena28398b2008-04-07 09:42:07 +0800665 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 goto e_inval;
667
668 retv = -EFAULT;
669 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
670 break;
671
672 if (optname == IPV6_JOIN_ANYCAST)
673 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
674 else
675 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
676 break;
677 }
678 case MCAST_JOIN_GROUP:
679 case MCAST_LEAVE_GROUP:
680 {
681 struct group_req greq;
682 struct sockaddr_in6 *psin6;
683
Wang Chena28398b2008-04-07 09:42:07 +0800684 if (optlen < sizeof(struct group_req))
685 goto e_inval;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 retv = -EFAULT;
688 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
689 break;
690 if (greq.gr_group.ss_family != AF_INET6) {
691 retv = -EADDRNOTAVAIL;
692 break;
693 }
694 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
695 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300696 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
697 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300699 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
700 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
702 }
703 case MCAST_JOIN_SOURCE_GROUP:
704 case MCAST_LEAVE_SOURCE_GROUP:
705 case MCAST_BLOCK_SOURCE:
706 case MCAST_UNBLOCK_SOURCE:
707 {
708 struct group_source_req greqs;
709 int omode, add;
710
Wang Chena28398b2008-04-07 09:42:07 +0800711 if (optlen < sizeof(struct group_source_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 goto e_inval;
713 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
714 retv = -EFAULT;
715 break;
716 }
717 if (greqs.gsr_group.ss_family != AF_INET6 ||
718 greqs.gsr_source.ss_family != AF_INET6) {
719 retv = -EADDRNOTAVAIL;
720 break;
721 }
722 if (optname == MCAST_BLOCK_SOURCE) {
723 omode = MCAST_EXCLUDE;
724 add = 1;
725 } else if (optname == MCAST_UNBLOCK_SOURCE) {
726 omode = MCAST_EXCLUDE;
727 add = 0;
728 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
729 struct sockaddr_in6 *psin6;
730
731 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
Hangbin Liuc7ea20c2018-07-10 22:41:27 +0800732 retv = ipv6_sock_mc_join_ssm(sk, greqs.gsr_interface,
733 &psin6->sin6_addr,
734 MCAST_INCLUDE);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700735 /* prior join w/ different source is ok */
736 if (retv && retv != -EADDRINUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 break;
738 omode = MCAST_INCLUDE;
739 add = 1;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700740 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 omode = MCAST_INCLUDE;
742 add = 0;
743 }
744 retv = ip6_mc_source(add, omode, sk, &greqs);
745 break;
746 }
747 case MCAST_MSFILTER:
748 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 struct group_filter *gsf;
750
751 if (optlen < GROUP_FILTER_SIZE(0))
752 goto e_inval;
753 if (optlen > sysctl_optmem_max) {
754 retv = -ENOBUFS;
755 break;
756 }
Al Viro43727da2017-05-13 18:22:53 -0400757 gsf = memdup_user(optval, optlen);
758 if (IS_ERR(gsf)) {
759 retv = PTR_ERR(gsf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 break;
761 }
762 /* numsrc >= (4G-140)/128 overflow in 32 bits */
763 if (gsf->gf_numsrc >= 0x1ffffffU ||
764 gsf->gf_numsrc > sysctl_mld_max_msf) {
765 kfree(gsf);
766 retv = -ENOBUFS;
767 break;
768 }
769 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
770 kfree(gsf);
771 retv = -EINVAL;
772 break;
773 }
774 retv = ip6_mc_msfilter(sk, gsf);
775 kfree(gsf);
776
777 break;
778 }
779 case IPV6_ROUTER_ALERT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900780 if (optlen < sizeof(int))
781 goto e_inval;
Denis V. Lunev725a8ff2008-07-19 00:28:58 -0700782 retv = ip6_ra_control(sk, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 break;
784 case IPV6_MTU_DISCOVER:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900785 if (optlen < sizeof(int))
786 goto e_inval;
Hannes Frederic Sowa0b952272014-02-26 01:20:43 +0100787 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 goto e_inval;
789 np->pmtudisc = val;
790 retv = 0;
791 break;
792 case IPV6_MTU:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900793 if (optlen < sizeof(int))
794 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (val && val < IPV6_MIN_MTU)
796 goto e_inval;
797 np->frag_size = val;
798 retv = 0;
799 break;
800 case IPV6_RECVERR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900801 if (optlen < sizeof(int))
802 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 np->recverr = valbool;
804 if (!val)
805 skb_queue_purge(&sk->sk_error_queue);
806 retv = 0;
807 break;
808 case IPV6_FLOWINFO_SEND:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900809 if (optlen < sizeof(int))
810 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 np->sndflow = valbool;
812 retv = 0;
813 break;
814 case IPV6_FLOWLABEL_MGR:
815 retv = ipv6_flowlabel_opt(sk, optval, optlen);
816 break;
817 case IPV6_IPSEC_POLICY:
818 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700819 retv = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000820 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700821 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 retv = xfrm_user_policy(sk, optname, optval, optlen);
823 break;
824
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900825 case IPV6_ADDR_PREFERENCES:
826 {
827 unsigned int pref = 0;
828 unsigned int prefmask = ~0;
829
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900830 if (optlen < sizeof(int))
831 goto e_inval;
832
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900833 retv = -EINVAL;
834
835 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
836 switch (val & (IPV6_PREFER_SRC_PUBLIC|
837 IPV6_PREFER_SRC_TMP|
838 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
839 case IPV6_PREFER_SRC_PUBLIC:
840 pref |= IPV6_PREFER_SRC_PUBLIC;
841 break;
842 case IPV6_PREFER_SRC_TMP:
843 pref |= IPV6_PREFER_SRC_TMP;
844 break;
845 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
846 break;
847 case 0:
848 goto pref_skip_pubtmp;
849 default:
850 goto e_inval;
851 }
852
853 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
854 IPV6_PREFER_SRC_TMP);
855pref_skip_pubtmp:
856
857 /* check HOME/COA conflicts */
858 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
859 case IPV6_PREFER_SRC_HOME:
860 break;
861 case IPV6_PREFER_SRC_COA:
862 pref |= IPV6_PREFER_SRC_COA;
863 case 0:
864 goto pref_skip_coa;
865 default:
866 goto e_inval;
867 }
868
869 prefmask &= ~IPV6_PREFER_SRC_COA;
870pref_skip_coa:
871
872 /* check CGA/NONCGA conflicts */
873 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
874 case IPV6_PREFER_SRC_CGA:
875 case IPV6_PREFER_SRC_NONCGA:
876 case 0:
877 break;
878 default:
879 goto e_inval;
880 }
881
882 np->srcprefs = (np->srcprefs & prefmask) | pref;
883 retv = 0;
884
885 break;
886 }
Stephen Hemmingere802af92010-04-22 15:24:53 -0700887 case IPV6_MINHOPCOUNT:
888 if (optlen < sizeof(int))
889 goto e_inval;
890 if (val < 0 || val > 255)
891 goto e_inval;
892 np->min_hopcount = val;
Hannes Frederic Sowad4596ba2012-11-10 19:52:34 +0000893 retv = 0;
Brian Haley793b14732010-04-23 11:26:07 +0000894 break;
895 case IPV6_DONTFRAG:
896 np->dontfrag = valbool;
Stephen Hemmingere802af92010-04-22 15:24:53 -0700897 retv = 0;
898 break;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700899 case IPV6_AUTOFLOWLABEL:
900 np->autoflowlabel = valbool;
Shaohua Li513674b2017-12-20 12:10:21 -0800901 np->autoflowlabel_set = 1;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700902 retv = 0;
903 break;
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -0400904 case IPV6_RECVFRAGSIZE:
905 np->rxopt.bits.recvfragsize = valbool;
906 retv = 0;
907 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300911 if (needs_rtnl)
912 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return retv;
915
916e_inval:
917 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300918 if (needs_rtnl)
919 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return -EINVAL;
921}
922
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800923int ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700924 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800925{
926 int err;
927
928 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
929 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
930
931 if (level != SOL_IPV6)
932 return -ENOPROTOOPT;
933
934 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
935#ifdef CONFIG_NETFILTER
936 /* we need to exclude all possible ENOPROTOOPTs except default case */
937 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +0100938 optname != IPV6_XFRM_POLICY)
939 err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800940#endif
941 return err;
942}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900943EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800944
945#ifdef CONFIG_COMPAT
946int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700947 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800948{
949 int err;
950
951 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800952 if (udp_prot.compat_setsockopt != NULL)
953 return udp_prot.compat_setsockopt(sk, level, optname,
954 optval, optlen);
955 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800956 }
957
958 if (level != SOL_IPV6)
959 return -ENOPROTOOPT;
960
David L Stevensdae50292008-04-27 01:06:07 -0700961 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
962 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
963 ipv6_setsockopt);
964
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800965 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
966#ifdef CONFIG_NETFILTER
967 /* we need to exclude all possible ENOPROTOOPTs except default case */
968 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +0100969 optname != IPV6_XFRM_POLICY)
970 err = compat_nf_setsockopt(sk, PF_INET6, optname, optval,
971 optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800972#endif
973 return err;
974}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800975EXPORT_SYMBOL(compat_ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800976#endif
977
David S. Miller286930792007-03-07 12:50:46 -0800978static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700979 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900980{
David S. Miller286930792007-03-07 12:50:46 -0800981 struct ipv6_opt_hdr *hdr;
982
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700983 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900984 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700985
Ian Morris67ba4152014-08-24 21:53:10 +0100986 switch (optname) {
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700987 case IPV6_HOPOPTS:
988 hdr = opt->hopopt;
989 break;
990 case IPV6_RTHDRDSTOPTS:
991 hdr = opt->dst0opt;
992 break;
993 case IPV6_RTHDR:
994 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
995 break;
996 case IPV6_DSTOPTS:
997 hdr = opt->dst1opt;
998 break;
999 default:
1000 return -EINVAL; /* should not happen */
1001 }
1002
1003 if (!hdr)
1004 return 0;
David S. Miller286930792007-03-07 12:50:46 -08001005
Chris Wrightd2b02ed2007-03-09 16:19:17 -08001006 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -07001007 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001008 return -EFAULT;
Yang Hongyang95b496b2008-05-28 16:27:28 +08001009 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001010}
1011
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001012static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001013 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 struct ipv6_pinfo *np = inet6_sk(sk);
1016 int len;
1017 int val;
1018
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001019 if (ip6_mroute_opt(optname))
1020 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (get_user(len, optlen))
1023 return -EFAULT;
1024 switch (optname) {
1025 case IPV6_ADDRFORM:
1026 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001027 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 sk->sk_protocol != IPPROTO_TCP)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +09001029 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (sk->sk_state != TCP_ESTABLISHED)
1031 return -ENOTCONN;
1032 val = sk->sk_family;
1033 break;
1034 case MCAST_MSFILTER:
1035 {
1036 struct group_filter gsf;
1037 int err;
1038
1039 if (len < GROUP_FILTER_SIZE(0))
1040 return -EINVAL;
1041 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
1042 return -EFAULT;
YOSHIFUJI Hideaki20c61fbd2008-04-28 14:40:55 +09001043 if (gsf.gf_group.ss_family != AF_INET6)
1044 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 lock_sock(sk);
1046 err = ip6_mc_msfget(sk, &gsf,
1047 (struct group_filter __user *)optval, optlen);
1048 release_sock(sk);
1049 return err;
1050 }
1051
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001052 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 {
1054 struct msghdr msg;
1055 struct sk_buff *skb;
1056
1057 if (sk->sk_type != SOCK_STREAM)
1058 return -ENOPROTOOPT;
1059
1060 msg.msg_control = optval;
1061 msg.msg_controllen = len;
Daniel Baluta98e77432011-08-19 03:19:07 -07001062 msg.msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 lock_sock(sk);
1065 skb = np->pktoptions;
1066 if (skb)
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001067 ip6_datagram_recv_ctl(sk, &msg, skb);
Eric Dumazet1dc7b902015-01-21 03:45:42 -08001068 release_sock(sk);
1069 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (np->rxopt.bits.rxinfo) {
1071 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001072 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1073 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001074 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1076 }
1077 if (np->rxopt.bits.rxhlim) {
1078 int hlim = np->mcast_hops;
1079 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1080 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001081 if (np->rxopt.bits.rxtclass) {
Li RongQingd76ed222014-01-15 17:03:30 +08001082 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1083
Jiri Benc4c507d22012-02-09 09:35:49 +00001084 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1085 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001086 if (np->rxopt.bits.rxoinfo) {
1087 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001088 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1089 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001090 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1091 np->sticky_pktinfo.ipi6_addr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001092 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1093 }
1094 if (np->rxopt.bits.rxohlim) {
1095 int hlim = np->mcast_hops;
1096 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1097 }
Florent Fourcot1397ed32013-12-08 15:46:57 +01001098 if (np->rxopt.bits.rxflow) {
Florent Fourcot68536052013-12-12 17:07:58 +01001099 __be32 flowinfo = np->rcv_flowinfo;
1100
Florent Fourcot1397ed32013-12-08 15:46:57 +01001101 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104 len -= msg.msg_controllen;
1105 return put_user(len, optlen);
1106 }
1107 case IPV6_MTU:
1108 {
1109 struct dst_entry *dst;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001110
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001111 val = 0;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001112 rcu_read_lock();
1113 dst = __sk_dst_get(sk);
1114 if (dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 val = dst_mtu(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001116 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (!val)
1118 return -ENOTCONN;
1119 break;
1120 }
1121
1122 case IPV6_V6ONLY:
Eric Dumazet9fe516b2014-06-27 08:36:16 -07001123 val = sk->sk_ipv6only;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 break;
1125
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001126 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 val = np->rxopt.bits.rxinfo;
1128 break;
1129
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001130 case IPV6_2292PKTINFO:
1131 val = np->rxopt.bits.rxoinfo;
1132 break;
1133
1134 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 val = np->rxopt.bits.rxhlim;
1136 break;
1137
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001138 case IPV6_2292HOPLIMIT:
1139 val = np->rxopt.bits.rxohlim;
1140 break;
1141
1142 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 val = np->rxopt.bits.srcrt;
1144 break;
1145
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001146 case IPV6_2292RTHDR:
1147 val = np->rxopt.bits.osrcrt;
1148 break;
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001151 case IPV6_RTHDRDSTOPTS:
1152 case IPV6_RTHDR:
1153 case IPV6_DSTOPTS:
1154 {
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001155 struct ipv6_txoptions *opt;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001156
1157 lock_sock(sk);
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001158 opt = rcu_dereference_protected(np->opt,
1159 lockdep_sock_is_held(sk));
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001160 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001161 release_sock(sk);
Yang Hongyang05335c22008-05-28 16:23:47 +08001162 /* check if ipv6_getsockopt_sticky() returns err code */
1163 if (len < 0)
1164 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001165 return put_user(len, optlen);
1166 }
1167
1168 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 val = np->rxopt.bits.hopopts;
1170 break;
1171
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001172 case IPV6_2292HOPOPTS:
1173 val = np->rxopt.bits.ohopopts;
1174 break;
1175
1176 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 val = np->rxopt.bits.dstopts;
1178 break;
1179
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001180 case IPV6_2292DSTOPTS:
1181 val = np->rxopt.bits.odstopts;
1182 break;
1183
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001184 case IPV6_TCLASS:
1185 val = np->tclass;
1186 break;
1187
1188 case IPV6_RECVTCLASS:
1189 val = np->rxopt.bits.rxtclass;
1190 break;
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 case IPV6_FLOWINFO:
1193 val = np->rxopt.bits.rxflow;
1194 break;
1195
Brian Haley793b14732010-04-23 11:26:07 +00001196 case IPV6_RECVPATHMTU:
1197 val = np->rxopt.bits.rxpmtu;
1198 break;
1199
1200 case IPV6_PATHMTU:
1201 {
1202 struct dst_entry *dst;
1203 struct ip6_mtuinfo mtuinfo;
1204
1205 if (len < sizeof(mtuinfo))
1206 return -EINVAL;
1207
1208 len = sizeof(mtuinfo);
1209 memset(&mtuinfo, 0, sizeof(mtuinfo));
1210
1211 rcu_read_lock();
1212 dst = __sk_dst_get(sk);
1213 if (dst)
1214 mtuinfo.ip6m_mtu = dst_mtu(dst);
1215 rcu_read_unlock();
1216 if (!mtuinfo.ip6m_mtu)
1217 return -ENOTCONN;
1218
1219 if (put_user(len, optlen))
1220 return -EFAULT;
1221 if (copy_to_user(optval, &mtuinfo, len))
1222 return -EFAULT;
1223
1224 return 0;
Brian Haley793b14732010-04-23 11:26:07 +00001225 }
1226
Balazs Scheidler6c468622010-10-21 16:08:28 +02001227 case IPV6_TRANSPARENT:
1228 val = inet_sk(sk)->transparent;
1229 break;
1230
Maciej Żenczykowski84e14fe2017-09-26 21:32:42 -07001231 case IPV6_FREEBIND:
1232 val = inet_sk(sk)->freebind;
1233 break;
1234
Balazs Scheidler6c468622010-10-21 16:08:28 +02001235 case IPV6_RECVORIGDSTADDR:
1236 val = np->rxopt.bits.rxorigdstaddr;
1237 break;
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -08001241 {
1242 struct dst_entry *dst;
1243
1244 if (optname == IPV6_UNICAST_HOPS)
1245 val = np->hop_limit;
1246 else
1247 val = np->mcast_hops;
1248
Eric Dumazetb6c67122010-04-08 23:03:29 +00001249 if (val < 0) {
1250 rcu_read_lock();
1251 dst = __sk_dst_get(sk);
1252 if (dst)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001253 val = ip6_dst_hoplimit(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001254 rcu_read_unlock();
Brian Haleybefffe92006-12-12 17:09:49 -08001255 }
Eric Dumazetb6c67122010-04-08 23:03:29 +00001256
Brian Haleybefffe92006-12-12 17:09:49 -08001257 if (val < 0)
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001258 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 break;
Brian Haleybefffe92006-12-12 17:09:49 -08001260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 case IPV6_MULTICAST_LOOP:
1263 val = np->mc_loop;
1264 break;
1265
1266 case IPV6_MULTICAST_IF:
1267 val = np->mcast_oif;
1268 break;
1269
Erich E. Hooverc4062df2012-02-08 09:11:08 +00001270 case IPV6_UNICAST_IF:
1271 val = (__force int)htonl((__u32) np->ucast_oif);
1272 break;
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 case IPV6_MTU_DISCOVER:
1275 val = np->pmtudisc;
1276 break;
1277
1278 case IPV6_RECVERR:
1279 val = np->recverr;
1280 break;
1281
1282 case IPV6_FLOWINFO_SEND:
1283 val = np->sndflow;
1284 break;
1285
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001286 case IPV6_FLOWLABEL_MGR:
1287 {
1288 struct in6_flowlabel_req freq;
Florent Fourcot46e5f402014-01-17 17:15:04 +01001289 int flags;
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001290
1291 if (len < sizeof(freq))
1292 return -EINVAL;
1293
1294 if (copy_from_user(&freq, optval, sizeof(freq)))
1295 return -EFAULT;
1296
1297 if (freq.flr_action != IPV6_FL_A_GET)
1298 return -EINVAL;
1299
1300 len = sizeof(freq);
Florent Fourcot46e5f402014-01-17 17:15:04 +01001301 flags = freq.flr_flags;
1302
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001303 memset(&freq, 0, sizeof(freq));
1304
Florent Fourcot46e5f402014-01-17 17:15:04 +01001305 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001306 if (val < 0)
1307 return val;
1308
1309 if (put_user(len, optlen))
1310 return -EFAULT;
1311 if (copy_to_user(optval, &freq, len))
1312 return -EFAULT;
1313
1314 return 0;
1315 }
1316
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001317 case IPV6_ADDR_PREFERENCES:
1318 val = 0;
1319
1320 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1321 val |= IPV6_PREFER_SRC_TMP;
1322 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1323 val |= IPV6_PREFER_SRC_PUBLIC;
1324 else {
1325 /* XXX: should we return system default? */
1326 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1327 }
1328
1329 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1330 val |= IPV6_PREFER_SRC_COA;
1331 else
1332 val |= IPV6_PREFER_SRC_HOME;
1333 break;
1334
Stephen Hemmingere802af92010-04-22 15:24:53 -07001335 case IPV6_MINHOPCOUNT:
1336 val = np->min_hopcount;
1337 break;
1338
Brian Haley793b14732010-04-23 11:26:07 +00001339 case IPV6_DONTFRAG:
1340 val = np->dontfrag;
1341 break;
1342
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001343 case IPV6_AUTOFLOWLABEL:
Ben Hutchingse9191ff2018-01-22 20:06:42 +00001344 val = ip6_autoflowlabel(sock_net(sk), np);
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001345 break;
1346
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -04001347 case IPV6_RECVFRAGSIZE:
1348 val = np->rxopt.bits.recvfragsize;
1349 break;
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001352 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354 len = min_t(unsigned int, sizeof(int), len);
Ian Morris67ba4152014-08-24 21:53:10 +01001355 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001357 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return -EFAULT;
1359 return 0;
1360}
1361
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001362int ipv6_getsockopt(struct sock *sk, int level, int optname,
1363 char __user *optval, int __user *optlen)
1364{
1365 int err;
1366
1367 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1368 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1369
Ian Morris67ba4152014-08-24 21:53:10 +01001370 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001371 return -ENOPROTOOPT;
1372
Daniel Baluta98e77432011-08-19 03:19:07 -07001373 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001374#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001375 /* we need to exclude all possible ENOPROTOOPTs except default case */
1376 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001377 int len;
1378
1379 if (get_user(len, optlen))
1380 return -EFAULT;
1381
Paolo Abeni01ea3062018-02-08 12:19:00 +01001382 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001383 if (err >= 0)
1384 err = put_user(len, optlen);
1385 }
1386#endif
1387 return err;
1388}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001389EXPORT_SYMBOL(ipv6_getsockopt);
1390
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001391#ifdef CONFIG_COMPAT
1392int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001393 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001394{
1395 int err;
1396
1397 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001398 if (udp_prot.compat_getsockopt != NULL)
1399 return udp_prot.compat_getsockopt(sk, level, optname,
1400 optval, optlen);
1401 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001402 }
1403
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001404 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001405 return -ENOPROTOOPT;
1406
Pavel Emelyanov80991792008-04-30 14:49:54 -07001407 if (optname == MCAST_MSFILTER)
1408 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1409 ipv6_getsockopt);
1410
Daniel Baluta98e77432011-08-19 03:19:07 -07001411 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1412 MSG_CMSG_COMPAT);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001413#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001414 /* we need to exclude all possible ENOPROTOOPTs except default case */
1415 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001416 int len;
1417
1418 if (get_user(len, optlen))
1419 return -EFAULT;
1420
Paolo Abeni01ea3062018-02-08 12:19:00 +01001421 err = compat_nf_getsockopt(sk, PF_INET6, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001422 if (err >= 0)
1423 err = put_user(len, optlen);
1424 }
1425#endif
1426 return err;
1427}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001428EXPORT_SYMBOL(compat_ipv6_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001429#endif