blob: fabe3ba1bddcb0ed92f9f0172ddff946786796e6 [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;
Shan Weiaea74272008-06-19 16:29:39 -0700401
402 /* remove any sticky options header with a zero option
403 * length, per RFC3542.
404 */
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900405 if (optlen == 0)
Luiz Capitulinocb422c42005-11-14 21:43:36 -0800406 optval = NULL;
Ian Morris63159f22015-03-29 14:00:04 +0100407 else if (!optval)
Yang Hongyangcfb266c2008-08-03 18:16:15 -0700408 goto e_inval;
Shan Weiaea74272008-06-19 16:29:39 -0700409 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
410 optlen & 0x7 || optlen > 8 * 255)
411 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900412
413 /* hop-by-hop / destination options are privileged option */
414 retv = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000415 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900416 break;
417
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200418 opt = rcu_dereference_protected(np->opt,
419 lockdep_sock_is_held(sk));
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800420 opt = ipv6_renew_options(sk, opt, optname,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900421 (struct ipv6_opt_hdr __user *)optval,
422 optlen);
423 if (IS_ERR(opt)) {
424 retv = PTR_ERR(opt);
425 break;
426 }
427
428 /* routing header option needs extra check */
Brian Haley6e093d92008-11-12 22:59:21 -0800429 retv = -EINVAL;
Olaf Kirchdfee0a72007-03-09 13:55:38 -0800430 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900431 struct ipv6_rt_hdr *rthdr = opt->srcrt;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700432 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000433#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700434 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800435 if (rthdr->hdrlen != 2 ||
436 rthdr->segments_left != 1)
437 goto sticky_done;
438
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700439 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700440#endif
David Lebruna149e7c2016-11-08 14:59:21 +0100441 case IPV6_SRCRT_TYPE_4:
442 {
443 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)
444 opt->srcrt;
445
446 if (!seg6_validate_srh(srh, optlen))
447 goto sticky_done;
448 break;
449 }
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700450 default:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900451 goto sticky_done;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700452 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900453 }
454
455 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700456 opt = ipv6_update_options(sk, opt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900457sticky_done:
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800458 if (opt) {
459 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
460 txopt_put(opt);
461 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900462 break;
463 }
464
Yang Hongyangb24a2512008-12-16 02:06:23 -0800465 case IPV6_PKTINFO:
466 {
467 struct in6_pktinfo pkt;
468
469 if (optlen == 0)
470 goto e_inval;
Ian Morris63159f22015-03-29 14:00:04 +0100471 else if (optlen < sizeof(struct in6_pktinfo) || !optval)
Yang Hongyangb24a2512008-12-16 02:06:23 -0800472 goto e_inval;
473
Ilpo Järvinen914d1162009-01-04 17:27:31 -0800474 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
Yang Hongyangb24a2512008-12-16 02:06:23 -0800475 retv = -EFAULT;
476 break;
477 }
478 if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if)
479 goto e_inval;
480
481 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000482 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
Yang Hongyangb24a2512008-12-16 02:06:23 -0800483 retv = 0;
484 break;
485 }
486
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900487 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 {
489 struct ipv6_txoptions *opt = NULL;
490 struct msghdr msg;
David S. Miller4c9483b2011-03-12 16:22:43 -0500491 struct flowi6 fl6;
Wei Wang26879da2016-05-02 21:40:07 -0700492 struct ipcm6_cookie ipc6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
David S. Miller4c9483b2011-03-12 16:22:43 -0500494 memset(&fl6, 0, sizeof(fl6));
495 fl6.flowi6_oif = sk->sk_bound_dev_if;
496 fl6.flowi6_mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if (optlen == 0)
499 goto update;
500
501 /* 1K is probably excessive
502 * 1K is surely not enough, 2K per standard header is 16K.
503 */
504 retv = -EINVAL;
505 if (optlen > 64*1024)
506 break;
507
508 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
509 retv = -ENOBUFS;
Ian Morris63159f22015-03-29 14:00:04 +0100510 if (!opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 break;
512
513 memset(opt, 0, sizeof(*opt));
Reshetova, Elena0aeea212017-07-04 09:34:54 +0300514 refcount_set(&opt->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 opt->tot_len = sizeof(*opt) + optlen;
516 retv = -EFAULT;
517 if (copy_from_user(opt+1, optval, optlen))
518 goto done;
519
520 msg.msg_controllen = optlen;
Ian Morris67ba4152014-08-24 21:53:10 +0100521 msg.msg_control = (void *)(opt+1);
Wei Wang26879da2016-05-02 21:40:07 -0700522 ipc6.opt = opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Willem de Bruijn5fdaa882018-07-06 10:12:57 -0400524 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (retv)
526 goto done;
527update:
528 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700529 opt = ipv6_update_options(sk, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530done:
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800531 if (opt) {
532 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
533 txopt_put(opt);
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 break;
536 }
537 case IPV6_UNICAST_HOPS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900538 if (optlen < sizeof(int))
539 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (val > 255 || val < -1)
541 goto e_inval;
542 np->hop_limit = val;
543 retv = 0;
544 break;
545
546 case IPV6_MULTICAST_HOPS:
547 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900548 break;
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;
Li Wei2a38e6d2011-11-27 21:33:34 +0000553 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 retv = 0;
555 break;
556
557 case IPV6_MULTICAST_LOOP:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900558 if (optlen < sizeof(int))
559 goto e_inval;
YOSHIFUJI Hideaki28d44882008-06-12 03:14:51 +0900560 if (val != valbool)
561 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 np->mc_loop = valbool;
563 retv = 0;
564 break;
565
Erich E. Hooverc4062df2012-02-08 09:11:08 +0000566 case IPV6_UNICAST_IF:
567 {
568 struct net_device *dev = NULL;
569 int ifindex;
570
571 if (optlen != sizeof(int))
572 goto e_inval;
573
574 ifindex = (__force int)ntohl((__force __be32)val);
575 if (ifindex == 0) {
576 np->ucast_oif = 0;
577 retv = 0;
578 break;
579 }
580
581 dev = dev_get_by_index(net, ifindex);
582 retv = -EADDRNOTAVAIL;
583 if (!dev)
584 break;
585 dev_put(dev);
586
587 retv = -EINVAL;
588 if (sk->sk_bound_dev_if)
589 break;
590
591 np->ucast_oif = ifindex;
592 retv = 0;
593 break;
594 }
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 case IPV6_MULTICAST_IF:
597 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900598 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900599 if (optlen < sizeof(int))
600 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Brian Haley4953f0f2007-10-11 14:39:29 -0700602 if (val) {
Eric Dumazet55b80502009-10-19 06:41:58 +0000603 struct net_device *dev;
David Ahern7bb387c2016-12-29 15:39:37 -0800604 int midx;
Eric Dumazet55b80502009-10-19 06:41:58 +0000605
David Ahern7bb387c2016-12-29 15:39:37 -0800606 rcu_read_lock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700607
David Ahern7bb387c2016-12-29 15:39:37 -0800608 dev = dev_get_by_index_rcu(net, val);
Eric Dumazet55b80502009-10-19 06:41:58 +0000609 if (!dev) {
David Ahern7bb387c2016-12-29 15:39:37 -0800610 rcu_read_unlock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700611 retv = -ENODEV;
612 break;
613 }
David Ahern7bb387c2016-12-29 15:39:37 -0800614 midx = l3mdev_master_ifindex_rcu(dev);
615
616 rcu_read_unlock();
617
618 if (sk->sk_bound_dev_if &&
619 sk->sk_bound_dev_if != val &&
620 (!midx || midx != sk->sk_bound_dev_if))
621 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623 np->mcast_oif = val;
624 retv = 0;
625 break;
626 case IPV6_ADD_MEMBERSHIP:
627 case IPV6_DROP_MEMBERSHIP:
628 {
629 struct ipv6_mreq mreq;
630
Wang Chena28398b2008-04-07 09:42:07 +0800631 if (optlen < sizeof(struct ipv6_mreq))
632 goto e_inval;
633
Flavio Leitnera96fb492007-08-24 22:16:39 -0700634 retv = -EPROTO;
635 if (inet_sk(sk)->is_icsk)
636 break;
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 retv = -EFAULT;
639 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
640 break;
641
642 if (optname == IPV6_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300643 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300645 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 break;
647 }
648 case IPV6_JOIN_ANYCAST:
649 case IPV6_LEAVE_ANYCAST:
650 {
651 struct ipv6_mreq mreq;
652
Wang Chena28398b2008-04-07 09:42:07 +0800653 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 goto e_inval;
655
656 retv = -EFAULT;
657 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
658 break;
659
660 if (optname == IPV6_JOIN_ANYCAST)
661 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
662 else
663 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
664 break;
665 }
666 case MCAST_JOIN_GROUP:
667 case MCAST_LEAVE_GROUP:
668 {
669 struct group_req greq;
670 struct sockaddr_in6 *psin6;
671
Wang Chena28398b2008-04-07 09:42:07 +0800672 if (optlen < sizeof(struct group_req))
673 goto e_inval;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 retv = -EFAULT;
676 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
677 break;
678 if (greq.gr_group.ss_family != AF_INET6) {
679 retv = -EADDRNOTAVAIL;
680 break;
681 }
682 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
683 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300684 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
685 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300687 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
688 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 break;
690 }
691 case MCAST_JOIN_SOURCE_GROUP:
692 case MCAST_LEAVE_SOURCE_GROUP:
693 case MCAST_BLOCK_SOURCE:
694 case MCAST_UNBLOCK_SOURCE:
695 {
696 struct group_source_req greqs;
697 int omode, add;
698
Wang Chena28398b2008-04-07 09:42:07 +0800699 if (optlen < sizeof(struct group_source_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 goto e_inval;
701 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
702 retv = -EFAULT;
703 break;
704 }
705 if (greqs.gsr_group.ss_family != AF_INET6 ||
706 greqs.gsr_source.ss_family != AF_INET6) {
707 retv = -EADDRNOTAVAIL;
708 break;
709 }
710 if (optname == MCAST_BLOCK_SOURCE) {
711 omode = MCAST_EXCLUDE;
712 add = 1;
713 } else if (optname == MCAST_UNBLOCK_SOURCE) {
714 omode = MCAST_EXCLUDE;
715 add = 0;
716 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
717 struct sockaddr_in6 *psin6;
718
719 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300720 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
721 &psin6->sin6_addr);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700722 /* prior join w/ different source is ok */
723 if (retv && retv != -EADDRINUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 break;
725 omode = MCAST_INCLUDE;
726 add = 1;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700727 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 omode = MCAST_INCLUDE;
729 add = 0;
730 }
731 retv = ip6_mc_source(add, omode, sk, &greqs);
732 break;
733 }
734 case MCAST_MSFILTER:
735 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 struct group_filter *gsf;
737
738 if (optlen < GROUP_FILTER_SIZE(0))
739 goto e_inval;
740 if (optlen > sysctl_optmem_max) {
741 retv = -ENOBUFS;
742 break;
743 }
Al Viro43727da2017-05-13 18:22:53 -0400744 gsf = memdup_user(optval, optlen);
745 if (IS_ERR(gsf)) {
746 retv = PTR_ERR(gsf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 break;
748 }
749 /* numsrc >= (4G-140)/128 overflow in 32 bits */
750 if (gsf->gf_numsrc >= 0x1ffffffU ||
751 gsf->gf_numsrc > sysctl_mld_max_msf) {
752 kfree(gsf);
753 retv = -ENOBUFS;
754 break;
755 }
756 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
757 kfree(gsf);
758 retv = -EINVAL;
759 break;
760 }
761 retv = ip6_mc_msfilter(sk, gsf);
762 kfree(gsf);
763
764 break;
765 }
766 case IPV6_ROUTER_ALERT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900767 if (optlen < sizeof(int))
768 goto e_inval;
Denis V. Lunev725a8ff2008-07-19 00:28:58 -0700769 retv = ip6_ra_control(sk, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 break;
771 case IPV6_MTU_DISCOVER:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900772 if (optlen < sizeof(int))
773 goto e_inval;
Hannes Frederic Sowa0b952272014-02-26 01:20:43 +0100774 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 goto e_inval;
776 np->pmtudisc = val;
777 retv = 0;
778 break;
779 case IPV6_MTU:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900780 if (optlen < sizeof(int))
781 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (val && val < IPV6_MIN_MTU)
783 goto e_inval;
784 np->frag_size = val;
785 retv = 0;
786 break;
787 case IPV6_RECVERR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900788 if (optlen < sizeof(int))
789 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 np->recverr = valbool;
791 if (!val)
792 skb_queue_purge(&sk->sk_error_queue);
793 retv = 0;
794 break;
795 case IPV6_FLOWINFO_SEND:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900796 if (optlen < sizeof(int))
797 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 np->sndflow = valbool;
799 retv = 0;
800 break;
801 case IPV6_FLOWLABEL_MGR:
802 retv = ipv6_flowlabel_opt(sk, optval, optlen);
803 break;
804 case IPV6_IPSEC_POLICY:
805 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700806 retv = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000807 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700808 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 retv = xfrm_user_policy(sk, optname, optval, optlen);
810 break;
811
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900812 case IPV6_ADDR_PREFERENCES:
813 {
814 unsigned int pref = 0;
815 unsigned int prefmask = ~0;
816
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900817 if (optlen < sizeof(int))
818 goto e_inval;
819
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900820 retv = -EINVAL;
821
822 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
823 switch (val & (IPV6_PREFER_SRC_PUBLIC|
824 IPV6_PREFER_SRC_TMP|
825 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
826 case IPV6_PREFER_SRC_PUBLIC:
827 pref |= IPV6_PREFER_SRC_PUBLIC;
828 break;
829 case IPV6_PREFER_SRC_TMP:
830 pref |= IPV6_PREFER_SRC_TMP;
831 break;
832 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
833 break;
834 case 0:
835 goto pref_skip_pubtmp;
836 default:
837 goto e_inval;
838 }
839
840 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
841 IPV6_PREFER_SRC_TMP);
842pref_skip_pubtmp:
843
844 /* check HOME/COA conflicts */
845 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
846 case IPV6_PREFER_SRC_HOME:
847 break;
848 case IPV6_PREFER_SRC_COA:
849 pref |= IPV6_PREFER_SRC_COA;
850 case 0:
851 goto pref_skip_coa;
852 default:
853 goto e_inval;
854 }
855
856 prefmask &= ~IPV6_PREFER_SRC_COA;
857pref_skip_coa:
858
859 /* check CGA/NONCGA conflicts */
860 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
861 case IPV6_PREFER_SRC_CGA:
862 case IPV6_PREFER_SRC_NONCGA:
863 case 0:
864 break;
865 default:
866 goto e_inval;
867 }
868
869 np->srcprefs = (np->srcprefs & prefmask) | pref;
870 retv = 0;
871
872 break;
873 }
Stephen Hemmingere802af92010-04-22 15:24:53 -0700874 case IPV6_MINHOPCOUNT:
875 if (optlen < sizeof(int))
876 goto e_inval;
877 if (val < 0 || val > 255)
878 goto e_inval;
879 np->min_hopcount = val;
Hannes Frederic Sowad4596ba2012-11-10 19:52:34 +0000880 retv = 0;
Brian Haley793b14732010-04-23 11:26:07 +0000881 break;
882 case IPV6_DONTFRAG:
883 np->dontfrag = valbool;
Stephen Hemmingere802af92010-04-22 15:24:53 -0700884 retv = 0;
885 break;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700886 case IPV6_AUTOFLOWLABEL:
887 np->autoflowlabel = valbool;
Shaohua Li513674b2017-12-20 12:10:21 -0800888 np->autoflowlabel_set = 1;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700889 retv = 0;
890 break;
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -0400891 case IPV6_RECVFRAGSIZE:
892 np->rxopt.bits.recvfragsize = valbool;
893 retv = 0;
894 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300898 if (needs_rtnl)
899 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return retv;
902
903e_inval:
904 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300905 if (needs_rtnl)
906 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return -EINVAL;
908}
909
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800910int ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700911 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800912{
913 int err;
914
915 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
916 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
917
918 if (level != SOL_IPV6)
919 return -ENOPROTOOPT;
920
921 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
922#ifdef CONFIG_NETFILTER
923 /* we need to exclude all possible ENOPROTOOPTs except default case */
924 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +0100925 optname != IPV6_XFRM_POLICY)
926 err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800927#endif
928 return err;
929}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900930EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800931
932#ifdef CONFIG_COMPAT
933int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700934 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800935{
936 int err;
937
938 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800939 if (udp_prot.compat_setsockopt != NULL)
940 return udp_prot.compat_setsockopt(sk, level, optname,
941 optval, optlen);
942 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800943 }
944
945 if (level != SOL_IPV6)
946 return -ENOPROTOOPT;
947
David L Stevensdae50292008-04-27 01:06:07 -0700948 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
949 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
950 ipv6_setsockopt);
951
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800952 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
953#ifdef CONFIG_NETFILTER
954 /* we need to exclude all possible ENOPROTOOPTs except default case */
955 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Paolo Abeni3f34cfa2018-01-30 19:01:40 +0100956 optname != IPV6_XFRM_POLICY)
957 err = compat_nf_setsockopt(sk, PF_INET6, optname, optval,
958 optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800959#endif
960 return err;
961}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800962EXPORT_SYMBOL(compat_ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800963#endif
964
David S. Miller286930792007-03-07 12:50:46 -0800965static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700966 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900967{
David S. Miller286930792007-03-07 12:50:46 -0800968 struct ipv6_opt_hdr *hdr;
969
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700970 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900971 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700972
Ian Morris67ba4152014-08-24 21:53:10 +0100973 switch (optname) {
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700974 case IPV6_HOPOPTS:
975 hdr = opt->hopopt;
976 break;
977 case IPV6_RTHDRDSTOPTS:
978 hdr = opt->dst0opt;
979 break;
980 case IPV6_RTHDR:
981 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
982 break;
983 case IPV6_DSTOPTS:
984 hdr = opt->dst1opt;
985 break;
986 default:
987 return -EINVAL; /* should not happen */
988 }
989
990 if (!hdr)
991 return 0;
David S. Miller286930792007-03-07 12:50:46 -0800992
Chris Wrightd2b02ed2007-03-09 16:19:17 -0800993 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -0700994 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900995 return -EFAULT;
Yang Hongyang95b496b2008-05-28 16:27:28 +0800996 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900997}
998
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800999static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001000 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 struct ipv6_pinfo *np = inet6_sk(sk);
1003 int len;
1004 int val;
1005
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001006 if (ip6_mroute_opt(optname))
1007 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (get_user(len, optlen))
1010 return -EFAULT;
1011 switch (optname) {
1012 case IPV6_ADDRFORM:
1013 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001014 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 sk->sk_protocol != IPPROTO_TCP)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +09001016 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (sk->sk_state != TCP_ESTABLISHED)
1018 return -ENOTCONN;
1019 val = sk->sk_family;
1020 break;
1021 case MCAST_MSFILTER:
1022 {
1023 struct group_filter gsf;
1024 int err;
1025
1026 if (len < GROUP_FILTER_SIZE(0))
1027 return -EINVAL;
1028 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
1029 return -EFAULT;
YOSHIFUJI Hideaki20c61fbd2008-04-28 14:40:55 +09001030 if (gsf.gf_group.ss_family != AF_INET6)
1031 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 lock_sock(sk);
1033 err = ip6_mc_msfget(sk, &gsf,
1034 (struct group_filter __user *)optval, optlen);
1035 release_sock(sk);
1036 return err;
1037 }
1038
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001039 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 {
1041 struct msghdr msg;
1042 struct sk_buff *skb;
1043
1044 if (sk->sk_type != SOCK_STREAM)
1045 return -ENOPROTOOPT;
1046
1047 msg.msg_control = optval;
1048 msg.msg_controllen = len;
Daniel Baluta98e77432011-08-19 03:19:07 -07001049 msg.msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 lock_sock(sk);
1052 skb = np->pktoptions;
1053 if (skb)
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001054 ip6_datagram_recv_ctl(sk, &msg, skb);
Eric Dumazet1dc7b902015-01-21 03:45:42 -08001055 release_sock(sk);
1056 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (np->rxopt.bits.rxinfo) {
1058 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001059 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1060 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001061 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1063 }
1064 if (np->rxopt.bits.rxhlim) {
1065 int hlim = np->mcast_hops;
1066 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1067 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001068 if (np->rxopt.bits.rxtclass) {
Li RongQingd76ed222014-01-15 17:03:30 +08001069 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1070
Jiri Benc4c507d22012-02-09 09:35:49 +00001071 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1072 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001073 if (np->rxopt.bits.rxoinfo) {
1074 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001075 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1076 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001077 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1078 np->sticky_pktinfo.ipi6_addr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001079 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1080 }
1081 if (np->rxopt.bits.rxohlim) {
1082 int hlim = np->mcast_hops;
1083 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1084 }
Florent Fourcot1397ed32013-12-08 15:46:57 +01001085 if (np->rxopt.bits.rxflow) {
Florent Fourcot68536052013-12-12 17:07:58 +01001086 __be32 flowinfo = np->rcv_flowinfo;
1087
Florent Fourcot1397ed32013-12-08 15:46:57 +01001088 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091 len -= msg.msg_controllen;
1092 return put_user(len, optlen);
1093 }
1094 case IPV6_MTU:
1095 {
1096 struct dst_entry *dst;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001097
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001098 val = 0;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001099 rcu_read_lock();
1100 dst = __sk_dst_get(sk);
1101 if (dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 val = dst_mtu(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001103 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (!val)
1105 return -ENOTCONN;
1106 break;
1107 }
1108
1109 case IPV6_V6ONLY:
Eric Dumazet9fe516b2014-06-27 08:36:16 -07001110 val = sk->sk_ipv6only;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 break;
1112
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001113 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 val = np->rxopt.bits.rxinfo;
1115 break;
1116
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001117 case IPV6_2292PKTINFO:
1118 val = np->rxopt.bits.rxoinfo;
1119 break;
1120
1121 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 val = np->rxopt.bits.rxhlim;
1123 break;
1124
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001125 case IPV6_2292HOPLIMIT:
1126 val = np->rxopt.bits.rxohlim;
1127 break;
1128
1129 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 val = np->rxopt.bits.srcrt;
1131 break;
1132
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001133 case IPV6_2292RTHDR:
1134 val = np->rxopt.bits.osrcrt;
1135 break;
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001138 case IPV6_RTHDRDSTOPTS:
1139 case IPV6_RTHDR:
1140 case IPV6_DSTOPTS:
1141 {
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001142 struct ipv6_txoptions *opt;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001143
1144 lock_sock(sk);
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001145 opt = rcu_dereference_protected(np->opt,
1146 lockdep_sock_is_held(sk));
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001147 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001148 release_sock(sk);
Yang Hongyang05335c22008-05-28 16:23:47 +08001149 /* check if ipv6_getsockopt_sticky() returns err code */
1150 if (len < 0)
1151 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001152 return put_user(len, optlen);
1153 }
1154
1155 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 val = np->rxopt.bits.hopopts;
1157 break;
1158
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001159 case IPV6_2292HOPOPTS:
1160 val = np->rxopt.bits.ohopopts;
1161 break;
1162
1163 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 val = np->rxopt.bits.dstopts;
1165 break;
1166
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001167 case IPV6_2292DSTOPTS:
1168 val = np->rxopt.bits.odstopts;
1169 break;
1170
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001171 case IPV6_TCLASS:
1172 val = np->tclass;
1173 break;
1174
1175 case IPV6_RECVTCLASS:
1176 val = np->rxopt.bits.rxtclass;
1177 break;
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 case IPV6_FLOWINFO:
1180 val = np->rxopt.bits.rxflow;
1181 break;
1182
Brian Haley793b14732010-04-23 11:26:07 +00001183 case IPV6_RECVPATHMTU:
1184 val = np->rxopt.bits.rxpmtu;
1185 break;
1186
1187 case IPV6_PATHMTU:
1188 {
1189 struct dst_entry *dst;
1190 struct ip6_mtuinfo mtuinfo;
1191
1192 if (len < sizeof(mtuinfo))
1193 return -EINVAL;
1194
1195 len = sizeof(mtuinfo);
1196 memset(&mtuinfo, 0, sizeof(mtuinfo));
1197
1198 rcu_read_lock();
1199 dst = __sk_dst_get(sk);
1200 if (dst)
1201 mtuinfo.ip6m_mtu = dst_mtu(dst);
1202 rcu_read_unlock();
1203 if (!mtuinfo.ip6m_mtu)
1204 return -ENOTCONN;
1205
1206 if (put_user(len, optlen))
1207 return -EFAULT;
1208 if (copy_to_user(optval, &mtuinfo, len))
1209 return -EFAULT;
1210
1211 return 0;
Brian Haley793b14732010-04-23 11:26:07 +00001212 }
1213
Balazs Scheidler6c468622010-10-21 16:08:28 +02001214 case IPV6_TRANSPARENT:
1215 val = inet_sk(sk)->transparent;
1216 break;
1217
Maciej Żenczykowski84e14fe2017-09-26 21:32:42 -07001218 case IPV6_FREEBIND:
1219 val = inet_sk(sk)->freebind;
1220 break;
1221
Balazs Scheidler6c468622010-10-21 16:08:28 +02001222 case IPV6_RECVORIGDSTADDR:
1223 val = np->rxopt.bits.rxorigdstaddr;
1224 break;
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -08001228 {
1229 struct dst_entry *dst;
1230
1231 if (optname == IPV6_UNICAST_HOPS)
1232 val = np->hop_limit;
1233 else
1234 val = np->mcast_hops;
1235
Eric Dumazetb6c67122010-04-08 23:03:29 +00001236 if (val < 0) {
1237 rcu_read_lock();
1238 dst = __sk_dst_get(sk);
1239 if (dst)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001240 val = ip6_dst_hoplimit(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001241 rcu_read_unlock();
Brian Haleybefffe92006-12-12 17:09:49 -08001242 }
Eric Dumazetb6c67122010-04-08 23:03:29 +00001243
Brian Haleybefffe92006-12-12 17:09:49 -08001244 if (val < 0)
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001245 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 break;
Brian Haleybefffe92006-12-12 17:09:49 -08001247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 case IPV6_MULTICAST_LOOP:
1250 val = np->mc_loop;
1251 break;
1252
1253 case IPV6_MULTICAST_IF:
1254 val = np->mcast_oif;
1255 break;
1256
Erich E. Hooverc4062df2012-02-08 09:11:08 +00001257 case IPV6_UNICAST_IF:
1258 val = (__force int)htonl((__u32) np->ucast_oif);
1259 break;
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 case IPV6_MTU_DISCOVER:
1262 val = np->pmtudisc;
1263 break;
1264
1265 case IPV6_RECVERR:
1266 val = np->recverr;
1267 break;
1268
1269 case IPV6_FLOWINFO_SEND:
1270 val = np->sndflow;
1271 break;
1272
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001273 case IPV6_FLOWLABEL_MGR:
1274 {
1275 struct in6_flowlabel_req freq;
Florent Fourcot46e5f402014-01-17 17:15:04 +01001276 int flags;
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001277
1278 if (len < sizeof(freq))
1279 return -EINVAL;
1280
1281 if (copy_from_user(&freq, optval, sizeof(freq)))
1282 return -EFAULT;
1283
1284 if (freq.flr_action != IPV6_FL_A_GET)
1285 return -EINVAL;
1286
1287 len = sizeof(freq);
Florent Fourcot46e5f402014-01-17 17:15:04 +01001288 flags = freq.flr_flags;
1289
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001290 memset(&freq, 0, sizeof(freq));
1291
Florent Fourcot46e5f402014-01-17 17:15:04 +01001292 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001293 if (val < 0)
1294 return val;
1295
1296 if (put_user(len, optlen))
1297 return -EFAULT;
1298 if (copy_to_user(optval, &freq, len))
1299 return -EFAULT;
1300
1301 return 0;
1302 }
1303
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001304 case IPV6_ADDR_PREFERENCES:
1305 val = 0;
1306
1307 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1308 val |= IPV6_PREFER_SRC_TMP;
1309 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1310 val |= IPV6_PREFER_SRC_PUBLIC;
1311 else {
1312 /* XXX: should we return system default? */
1313 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1314 }
1315
1316 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1317 val |= IPV6_PREFER_SRC_COA;
1318 else
1319 val |= IPV6_PREFER_SRC_HOME;
1320 break;
1321
Stephen Hemmingere802af92010-04-22 15:24:53 -07001322 case IPV6_MINHOPCOUNT:
1323 val = np->min_hopcount;
1324 break;
1325
Brian Haley793b14732010-04-23 11:26:07 +00001326 case IPV6_DONTFRAG:
1327 val = np->dontfrag;
1328 break;
1329
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001330 case IPV6_AUTOFLOWLABEL:
Ben Hutchingse9191ff2018-01-22 20:06:42 +00001331 val = ip6_autoflowlabel(sock_net(sk), np);
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001332 break;
1333
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -04001334 case IPV6_RECVFRAGSIZE:
1335 val = np->rxopt.bits.recvfragsize;
1336 break;
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001339 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341 len = min_t(unsigned int, sizeof(int), len);
Ian Morris67ba4152014-08-24 21:53:10 +01001342 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001344 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return -EFAULT;
1346 return 0;
1347}
1348
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001349int ipv6_getsockopt(struct sock *sk, int level, int optname,
1350 char __user *optval, int __user *optlen)
1351{
1352 int err;
1353
1354 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1355 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1356
Ian Morris67ba4152014-08-24 21:53:10 +01001357 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001358 return -ENOPROTOOPT;
1359
Daniel Baluta98e77432011-08-19 03:19:07 -07001360 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001361#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001362 /* we need to exclude all possible ENOPROTOOPTs except default case */
1363 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001364 int len;
1365
1366 if (get_user(len, optlen))
1367 return -EFAULT;
1368
Paolo Abeni01ea3062018-02-08 12:19:00 +01001369 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001370 if (err >= 0)
1371 err = put_user(len, optlen);
1372 }
1373#endif
1374 return err;
1375}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001376EXPORT_SYMBOL(ipv6_getsockopt);
1377
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001378#ifdef CONFIG_COMPAT
1379int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001380 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001381{
1382 int err;
1383
1384 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001385 if (udp_prot.compat_getsockopt != NULL)
1386 return udp_prot.compat_getsockopt(sk, level, optname,
1387 optval, optlen);
1388 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001389 }
1390
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001391 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001392 return -ENOPROTOOPT;
1393
Pavel Emelyanov80991792008-04-30 14:49:54 -07001394 if (optname == MCAST_MSFILTER)
1395 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1396 ipv6_getsockopt);
1397
Daniel Baluta98e77432011-08-19 03:19:07 -07001398 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1399 MSG_CMSG_COMPAT);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001400#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001401 /* we need to exclude all possible ENOPROTOOPTs except default case */
1402 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001403 int len;
1404
1405 if (get_user(len, optlen))
1406 return -EFAULT;
1407
Paolo Abeni01ea3062018-02-08 12:19:00 +01001408 err = compat_nf_getsockopt(sk, PF_INET6, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001409 if (err >= 0)
1410 err = put_user(len, optlen);
1411 }
1412#endif
1413 return err;
1414}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001415EXPORT_SYMBOL(compat_ipv6_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001416#endif