blob: a531ba032b85da42d3b5eefcbd9e2e624b5a2868 [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
245 sk->sk_destruct = inet_sock_destruct;
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700246 /*
247 * ... and add it to the refcnt debug socks count
248 * in the new family. -acme
249 */
250 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 module_put(THIS_MODULE);
252 retv = 0;
253 break;
254 }
255 goto e_inval;
256
257 case IPV6_V6ONLY:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900258 if (optlen < sizeof(int) ||
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000259 inet_sk(sk)->inet_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 goto e_inval;
Eric Dumazet9fe516b2014-06-27 08:36:16 -0700261 sk->sk_ipv6only = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 retv = 0;
263 break;
264
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900265 case IPV6_RECVPKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900266 if (optlen < sizeof(int))
267 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 np->rxopt.bits.rxinfo = valbool;
269 retv = 0;
270 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900271
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900272 case IPV6_2292PKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900273 if (optlen < sizeof(int))
274 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900275 np->rxopt.bits.rxoinfo = valbool;
276 retv = 0;
277 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900279 case IPV6_RECVHOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900280 if (optlen < sizeof(int))
281 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 np->rxopt.bits.rxhlim = valbool;
283 retv = 0;
284 break;
285
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900286 case IPV6_2292HOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900287 if (optlen < sizeof(int))
288 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900289 np->rxopt.bits.rxohlim = valbool;
290 retv = 0;
291 break;
292
293 case IPV6_RECVRTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900294 if (optlen < sizeof(int))
295 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900296 np->rxopt.bits.srcrt = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 retv = 0;
298 break;
299
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900300 case IPV6_2292RTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900301 if (optlen < sizeof(int))
302 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900303 np->rxopt.bits.osrcrt = valbool;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900304 retv = 0;
305 break;
306
307 case IPV6_RECVHOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900308 if (optlen < sizeof(int))
309 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 np->rxopt.bits.hopopts = valbool;
311 retv = 0;
312 break;
313
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900314 case IPV6_2292HOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900315 if (optlen < sizeof(int))
316 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900317 np->rxopt.bits.ohopopts = valbool;
318 retv = 0;
319 break;
320
321 case IPV6_RECVDSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900322 if (optlen < sizeof(int))
323 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 np->rxopt.bits.dstopts = valbool;
325 retv = 0;
326 break;
327
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900328 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900329 if (optlen < sizeof(int))
330 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900331 np->rxopt.bits.odstopts = valbool;
332 retv = 0;
333 break;
334
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900335 case IPV6_TCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900336 if (optlen < sizeof(int))
337 goto e_inval;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700338 if (val < -1 || val > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900339 goto e_inval;
Gerrit Renker26ced1e2009-08-09 08:12:49 +0000340 /* RFC 3542, 6.5: default traffic class of 0x0 */
341 if (val == -1)
342 val = 0;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900343 np->tclass = val;
344 retv = 0;
345 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900346
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900347 case IPV6_RECVTCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900348 if (optlen < sizeof(int))
349 goto e_inval;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900350 np->rxopt.bits.rxtclass = valbool;
351 retv = 0;
352 break;
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 case IPV6_FLOWINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900355 if (optlen < sizeof(int))
356 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 np->rxopt.bits.rxflow = valbool;
358 retv = 0;
359 break;
360
Brian Haley793b14732010-04-23 11:26:07 +0000361 case IPV6_RECVPATHMTU:
362 if (optlen < sizeof(int))
363 goto e_inval;
364 np->rxopt.bits.rxpmtu = valbool;
365 retv = 0;
366 break;
367
Balazs Scheidler6c468622010-10-21 16:08:28 +0200368 case IPV6_TRANSPARENT:
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000369 if (valbool && !ns_capable(net->user_ns, CAP_NET_ADMIN) &&
370 !ns_capable(net->user_ns, CAP_NET_RAW)) {
Balazs Scheidlerb8894162010-10-23 04:48:14 +0000371 retv = -EPERM;
372 break;
373 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200374 if (optlen < sizeof(int))
375 goto e_inval;
376 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
377 inet_sk(sk)->transparent = valbool;
378 retv = 0;
379 break;
380
381 case IPV6_RECVORIGDSTADDR:
382 if (optlen < sizeof(int))
383 goto e_inval;
384 np->rxopt.bits.rxorigdstaddr = valbool;
385 retv = 0;
386 break;
387
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900388 case IPV6_HOPOPTS:
389 case IPV6_RTHDRDSTOPTS:
390 case IPV6_RTHDR:
391 case IPV6_DSTOPTS:
392 {
393 struct ipv6_txoptions *opt;
Shan Weiaea74272008-06-19 16:29:39 -0700394
395 /* remove any sticky options header with a zero option
396 * length, per RFC3542.
397 */
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900398 if (optlen == 0)
Luiz Capitulinocb422c42005-11-14 21:43:36 -0800399 optval = NULL;
Ian Morris63159f22015-03-29 14:00:04 +0100400 else if (!optval)
Yang Hongyangcfb266c2008-08-03 18:16:15 -0700401 goto e_inval;
Shan Weiaea74272008-06-19 16:29:39 -0700402 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
403 optlen & 0x7 || optlen > 8 * 255)
404 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900405
406 /* hop-by-hop / destination options are privileged option */
407 retv = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000408 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900409 break;
410
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200411 opt = rcu_dereference_protected(np->opt,
412 lockdep_sock_is_held(sk));
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800413 opt = ipv6_renew_options(sk, opt, optname,
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900414 (struct ipv6_opt_hdr __user *)optval,
415 optlen);
416 if (IS_ERR(opt)) {
417 retv = PTR_ERR(opt);
418 break;
419 }
420
421 /* routing header option needs extra check */
Brian Haley6e093d92008-11-12 22:59:21 -0800422 retv = -EINVAL;
Olaf Kirchdfee0a72007-03-09 13:55:38 -0800423 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900424 struct ipv6_rt_hdr *rthdr = opt->srcrt;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700425 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000426#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700427 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800428 if (rthdr->hdrlen != 2 ||
429 rthdr->segments_left != 1)
430 goto sticky_done;
431
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700432 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700433#endif
David Lebruna149e7c2016-11-08 14:59:21 +0100434 case IPV6_SRCRT_TYPE_4:
435 {
436 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)
437 opt->srcrt;
438
439 if (!seg6_validate_srh(srh, optlen))
440 goto sticky_done;
441 break;
442 }
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700443 default:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900444 goto sticky_done;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700445 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900446 }
447
448 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700449 opt = ipv6_update_options(sk, opt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900450sticky_done:
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800451 if (opt) {
452 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
453 txopt_put(opt);
454 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900455 break;
456 }
457
Yang Hongyangb24a2512008-12-16 02:06:23 -0800458 case IPV6_PKTINFO:
459 {
460 struct in6_pktinfo pkt;
461
462 if (optlen == 0)
463 goto e_inval;
Ian Morris63159f22015-03-29 14:00:04 +0100464 else if (optlen < sizeof(struct in6_pktinfo) || !optval)
Yang Hongyangb24a2512008-12-16 02:06:23 -0800465 goto e_inval;
466
Ilpo Järvinen914d1162009-01-04 17:27:31 -0800467 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
Yang Hongyangb24a2512008-12-16 02:06:23 -0800468 retv = -EFAULT;
469 break;
470 }
471 if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if)
472 goto e_inval;
473
474 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000475 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
Yang Hongyangb24a2512008-12-16 02:06:23 -0800476 retv = 0;
477 break;
478 }
479
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900480 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 {
482 struct ipv6_txoptions *opt = NULL;
483 struct msghdr msg;
David S. Miller4c9483b2011-03-12 16:22:43 -0500484 struct flowi6 fl6;
Soheil Hassas Yeganehad1e46a2016-04-02 23:08:11 -0400485 struct sockcm_cookie sockc_junk;
Wei Wang26879da2016-05-02 21:40:07 -0700486 struct ipcm6_cookie ipc6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
David S. Miller4c9483b2011-03-12 16:22:43 -0500488 memset(&fl6, 0, sizeof(fl6));
489 fl6.flowi6_oif = sk->sk_bound_dev_if;
490 fl6.flowi6_mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if (optlen == 0)
493 goto update;
494
495 /* 1K is probably excessive
496 * 1K is surely not enough, 2K per standard header is 16K.
497 */
498 retv = -EINVAL;
499 if (optlen > 64*1024)
500 break;
501
502 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
503 retv = -ENOBUFS;
Ian Morris63159f22015-03-29 14:00:04 +0100504 if (!opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 break;
506
507 memset(opt, 0, sizeof(*opt));
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800508 atomic_set(&opt->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 opt->tot_len = sizeof(*opt) + optlen;
510 retv = -EFAULT;
511 if (copy_from_user(opt+1, optval, optlen))
512 goto done;
513
514 msg.msg_controllen = optlen;
Ian Morris67ba4152014-08-24 21:53:10 +0100515 msg.msg_control = (void *)(opt+1);
Wei Wang26879da2016-05-02 21:40:07 -0700516 ipc6.opt = opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Wei Wang26879da2016-05-02 21:40:07 -0700518 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6, &sockc_junk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (retv)
520 goto done;
521update:
522 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700523 opt = ipv6_update_options(sk, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524done:
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800525 if (opt) {
526 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
527 txopt_put(opt);
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 break;
530 }
531 case IPV6_UNICAST_HOPS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900532 if (optlen < sizeof(int))
533 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (val > 255 || val < -1)
535 goto e_inval;
536 np->hop_limit = val;
537 retv = 0;
538 break;
539
540 case IPV6_MULTICAST_HOPS:
541 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900542 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900543 if (optlen < sizeof(int))
544 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (val > 255 || val < -1)
546 goto e_inval;
Li Wei2a38e6d2011-11-27 21:33:34 +0000547 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 retv = 0;
549 break;
550
551 case IPV6_MULTICAST_LOOP:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900552 if (optlen < sizeof(int))
553 goto e_inval;
YOSHIFUJI Hideaki28d44882008-06-12 03:14:51 +0900554 if (val != valbool)
555 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 np->mc_loop = valbool;
557 retv = 0;
558 break;
559
Erich E. Hooverc4062df2012-02-08 09:11:08 +0000560 case IPV6_UNICAST_IF:
561 {
562 struct net_device *dev = NULL;
563 int ifindex;
564
565 if (optlen != sizeof(int))
566 goto e_inval;
567
568 ifindex = (__force int)ntohl((__force __be32)val);
569 if (ifindex == 0) {
570 np->ucast_oif = 0;
571 retv = 0;
572 break;
573 }
574
575 dev = dev_get_by_index(net, ifindex);
576 retv = -EADDRNOTAVAIL;
577 if (!dev)
578 break;
579 dev_put(dev);
580
581 retv = -EINVAL;
582 if (sk->sk_bound_dev_if)
583 break;
584
585 np->ucast_oif = ifindex;
586 retv = 0;
587 break;
588 }
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 case IPV6_MULTICAST_IF:
591 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900592 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900593 if (optlen < sizeof(int))
594 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Brian Haley4953f0f2007-10-11 14:39:29 -0700596 if (val) {
Eric Dumazet55b80502009-10-19 06:41:58 +0000597 struct net_device *dev;
David Ahern7bb387c2016-12-29 15:39:37 -0800598 int midx;
Eric Dumazet55b80502009-10-19 06:41:58 +0000599
David Ahern7bb387c2016-12-29 15:39:37 -0800600 rcu_read_lock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700601
David Ahern7bb387c2016-12-29 15:39:37 -0800602 dev = dev_get_by_index_rcu(net, val);
Eric Dumazet55b80502009-10-19 06:41:58 +0000603 if (!dev) {
David Ahern7bb387c2016-12-29 15:39:37 -0800604 rcu_read_unlock();
Brian Haley4953f0f2007-10-11 14:39:29 -0700605 retv = -ENODEV;
606 break;
607 }
David Ahern7bb387c2016-12-29 15:39:37 -0800608 midx = l3mdev_master_ifindex_rcu(dev);
609
610 rcu_read_unlock();
611
612 if (sk->sk_bound_dev_if &&
613 sk->sk_bound_dev_if != val &&
614 (!midx || midx != sk->sk_bound_dev_if))
615 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617 np->mcast_oif = val;
618 retv = 0;
619 break;
620 case IPV6_ADD_MEMBERSHIP:
621 case IPV6_DROP_MEMBERSHIP:
622 {
623 struct ipv6_mreq mreq;
624
Wang Chena28398b2008-04-07 09:42:07 +0800625 if (optlen < sizeof(struct ipv6_mreq))
626 goto e_inval;
627
Flavio Leitnera96fb492007-08-24 22:16:39 -0700628 retv = -EPROTO;
629 if (inet_sk(sk)->is_icsk)
630 break;
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 retv = -EFAULT;
633 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
634 break;
635
636 if (optname == IPV6_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300637 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300639 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 break;
641 }
642 case IPV6_JOIN_ANYCAST:
643 case IPV6_LEAVE_ANYCAST:
644 {
645 struct ipv6_mreq mreq;
646
Wang Chena28398b2008-04-07 09:42:07 +0800647 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 goto e_inval;
649
650 retv = -EFAULT;
651 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
652 break;
653
654 if (optname == IPV6_JOIN_ANYCAST)
655 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
656 else
657 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
658 break;
659 }
660 case MCAST_JOIN_GROUP:
661 case MCAST_LEAVE_GROUP:
662 {
663 struct group_req greq;
664 struct sockaddr_in6 *psin6;
665
Wang Chena28398b2008-04-07 09:42:07 +0800666 if (optlen < sizeof(struct group_req))
667 goto e_inval;
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 retv = -EFAULT;
670 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
671 break;
672 if (greq.gr_group.ss_family != AF_INET6) {
673 retv = -EADDRNOTAVAIL;
674 break;
675 }
676 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
677 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300678 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
679 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300681 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
682 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 break;
684 }
685 case MCAST_JOIN_SOURCE_GROUP:
686 case MCAST_LEAVE_SOURCE_GROUP:
687 case MCAST_BLOCK_SOURCE:
688 case MCAST_UNBLOCK_SOURCE:
689 {
690 struct group_source_req greqs;
691 int omode, add;
692
Wang Chena28398b2008-04-07 09:42:07 +0800693 if (optlen < sizeof(struct group_source_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 goto e_inval;
695 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
696 retv = -EFAULT;
697 break;
698 }
699 if (greqs.gsr_group.ss_family != AF_INET6 ||
700 greqs.gsr_source.ss_family != AF_INET6) {
701 retv = -EADDRNOTAVAIL;
702 break;
703 }
704 if (optname == MCAST_BLOCK_SOURCE) {
705 omode = MCAST_EXCLUDE;
706 add = 1;
707 } else if (optname == MCAST_UNBLOCK_SOURCE) {
708 omode = MCAST_EXCLUDE;
709 add = 0;
710 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
711 struct sockaddr_in6 *psin6;
712
713 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300714 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
715 &psin6->sin6_addr);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700716 /* prior join w/ different source is ok */
717 if (retv && retv != -EADDRINUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 break;
719 omode = MCAST_INCLUDE;
720 add = 1;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700721 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 omode = MCAST_INCLUDE;
723 add = 0;
724 }
725 retv = ip6_mc_source(add, omode, sk, &greqs);
726 break;
727 }
728 case MCAST_MSFILTER:
729 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 struct group_filter *gsf;
731
732 if (optlen < GROUP_FILTER_SIZE(0))
733 goto e_inval;
734 if (optlen > sysctl_optmem_max) {
735 retv = -ENOBUFS;
736 break;
737 }
Ian Morris67ba4152014-08-24 21:53:10 +0100738 gsf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700739 if (!gsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 retv = -ENOBUFS;
741 break;
742 }
743 retv = -EFAULT;
744 if (copy_from_user(gsf, optval, optlen)) {
745 kfree(gsf);
746 break;
747 }
748 /* numsrc >= (4G-140)/128 overflow in 32 bits */
749 if (gsf->gf_numsrc >= 0x1ffffffU ||
750 gsf->gf_numsrc > sysctl_mld_max_msf) {
751 kfree(gsf);
752 retv = -ENOBUFS;
753 break;
754 }
755 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
756 kfree(gsf);
757 retv = -EINVAL;
758 break;
759 }
760 retv = ip6_mc_msfilter(sk, gsf);
761 kfree(gsf);
762
763 break;
764 }
765 case IPV6_ROUTER_ALERT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900766 if (optlen < sizeof(int))
767 goto e_inval;
Denis V. Lunev725a8ff2008-07-19 00:28:58 -0700768 retv = ip6_ra_control(sk, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 break;
770 case IPV6_MTU_DISCOVER:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900771 if (optlen < sizeof(int))
772 goto e_inval;
Hannes Frederic Sowa0b952272014-02-26 01:20:43 +0100773 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 goto e_inval;
775 np->pmtudisc = val;
776 retv = 0;
777 break;
778 case IPV6_MTU:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900779 if (optlen < sizeof(int))
780 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (val && val < IPV6_MIN_MTU)
782 goto e_inval;
783 np->frag_size = val;
784 retv = 0;
785 break;
786 case IPV6_RECVERR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900787 if (optlen < sizeof(int))
788 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 np->recverr = valbool;
790 if (!val)
791 skb_queue_purge(&sk->sk_error_queue);
792 retv = 0;
793 break;
794 case IPV6_FLOWINFO_SEND:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900795 if (optlen < sizeof(int))
796 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 np->sndflow = valbool;
798 retv = 0;
799 break;
800 case IPV6_FLOWLABEL_MGR:
801 retv = ipv6_flowlabel_opt(sk, optval, optlen);
802 break;
803 case IPV6_IPSEC_POLICY:
804 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700805 retv = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000806 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 retv = xfrm_user_policy(sk, optname, optval, optlen);
809 break;
810
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900811 case IPV6_ADDR_PREFERENCES:
812 {
813 unsigned int pref = 0;
814 unsigned int prefmask = ~0;
815
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900816 if (optlen < sizeof(int))
817 goto e_inval;
818
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900819 retv = -EINVAL;
820
821 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
822 switch (val & (IPV6_PREFER_SRC_PUBLIC|
823 IPV6_PREFER_SRC_TMP|
824 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
825 case IPV6_PREFER_SRC_PUBLIC:
826 pref |= IPV6_PREFER_SRC_PUBLIC;
827 break;
828 case IPV6_PREFER_SRC_TMP:
829 pref |= IPV6_PREFER_SRC_TMP;
830 break;
831 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
832 break;
833 case 0:
834 goto pref_skip_pubtmp;
835 default:
836 goto e_inval;
837 }
838
839 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
840 IPV6_PREFER_SRC_TMP);
841pref_skip_pubtmp:
842
843 /* check HOME/COA conflicts */
844 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
845 case IPV6_PREFER_SRC_HOME:
846 break;
847 case IPV6_PREFER_SRC_COA:
848 pref |= IPV6_PREFER_SRC_COA;
849 case 0:
850 goto pref_skip_coa;
851 default:
852 goto e_inval;
853 }
854
855 prefmask &= ~IPV6_PREFER_SRC_COA;
856pref_skip_coa:
857
858 /* check CGA/NONCGA conflicts */
859 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
860 case IPV6_PREFER_SRC_CGA:
861 case IPV6_PREFER_SRC_NONCGA:
862 case 0:
863 break;
864 default:
865 goto e_inval;
866 }
867
868 np->srcprefs = (np->srcprefs & prefmask) | pref;
869 retv = 0;
870
871 break;
872 }
Stephen Hemmingere802af92010-04-22 15:24:53 -0700873 case IPV6_MINHOPCOUNT:
874 if (optlen < sizeof(int))
875 goto e_inval;
876 if (val < 0 || val > 255)
877 goto e_inval;
878 np->min_hopcount = val;
Hannes Frederic Sowad4596ba2012-11-10 19:52:34 +0000879 retv = 0;
Brian Haley793b14732010-04-23 11:26:07 +0000880 break;
881 case IPV6_DONTFRAG:
882 np->dontfrag = valbool;
Stephen Hemmingere802af92010-04-22 15:24:53 -0700883 retv = 0;
884 break;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700885 case IPV6_AUTOFLOWLABEL:
886 np->autoflowlabel = valbool;
887 retv = 0;
888 break;
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -0400889 case IPV6_RECVFRAGSIZE:
890 np->rxopt.bits.recvfragsize = valbool;
891 retv = 0;
892 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300896 if (needs_rtnl)
897 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return retv;
900
901e_inval:
902 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300903 if (needs_rtnl)
904 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return -EINVAL;
906}
907
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800908int ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700909 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800910{
911 int err;
912
913 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
914 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
915
916 if (level != SOL_IPV6)
917 return -ENOPROTOOPT;
918
919 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
920#ifdef CONFIG_NETFILTER
921 /* we need to exclude all possible ENOPROTOOPTs except default case */
922 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
923 optname != IPV6_XFRM_POLICY) {
924 lock_sock(sk);
925 err = nf_setsockopt(sk, PF_INET6, optname, optval,
926 optlen);
927 release_sock(sk);
928 }
929#endif
930 return err;
931}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900932EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800933
934#ifdef CONFIG_COMPAT
935int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700936 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800937{
938 int err;
939
940 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800941 if (udp_prot.compat_setsockopt != NULL)
942 return udp_prot.compat_setsockopt(sk, level, optname,
943 optval, optlen);
944 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800945 }
946
947 if (level != SOL_IPV6)
948 return -ENOPROTOOPT;
949
David L Stevensdae50292008-04-27 01:06:07 -0700950 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
951 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
952 ipv6_setsockopt);
953
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800954 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
955#ifdef CONFIG_NETFILTER
956 /* we need to exclude all possible ENOPROTOOPTs except default case */
957 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800958 optname != IPV6_XFRM_POLICY) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800959 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800960 err = compat_nf_setsockopt(sk, PF_INET6, optname,
961 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800962 release_sock(sk);
963 }
964#endif
965 return err;
966}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800967EXPORT_SYMBOL(compat_ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800968#endif
969
David S. Miller286930792007-03-07 12:50:46 -0800970static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700971 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900972{
David S. Miller286930792007-03-07 12:50:46 -0800973 struct ipv6_opt_hdr *hdr;
974
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700975 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900976 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700977
Ian Morris67ba4152014-08-24 21:53:10 +0100978 switch (optname) {
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700979 case IPV6_HOPOPTS:
980 hdr = opt->hopopt;
981 break;
982 case IPV6_RTHDRDSTOPTS:
983 hdr = opt->dst0opt;
984 break;
985 case IPV6_RTHDR:
986 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
987 break;
988 case IPV6_DSTOPTS:
989 hdr = opt->dst1opt;
990 break;
991 default:
992 return -EINVAL; /* should not happen */
993 }
994
995 if (!hdr)
996 return 0;
David S. Miller286930792007-03-07 12:50:46 -0800997
Chris Wrightd2b02ed2007-03-09 16:19:17 -0800998 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -0700999 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001000 return -EFAULT;
Yang Hongyang95b496b2008-05-28 16:27:28 +08001001 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001002}
1003
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001004static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001005 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 struct ipv6_pinfo *np = inet6_sk(sk);
1008 int len;
1009 int val;
1010
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001011 if (ip6_mroute_opt(optname))
1012 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (get_user(len, optlen))
1015 return -EFAULT;
1016 switch (optname) {
1017 case IPV6_ADDRFORM:
1018 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001019 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 sk->sk_protocol != IPPROTO_TCP)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +09001021 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (sk->sk_state != TCP_ESTABLISHED)
1023 return -ENOTCONN;
1024 val = sk->sk_family;
1025 break;
1026 case MCAST_MSFILTER:
1027 {
1028 struct group_filter gsf;
1029 int err;
1030
1031 if (len < GROUP_FILTER_SIZE(0))
1032 return -EINVAL;
1033 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
1034 return -EFAULT;
YOSHIFUJI Hideaki20c61fbd2008-04-28 14:40:55 +09001035 if (gsf.gf_group.ss_family != AF_INET6)
1036 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 lock_sock(sk);
1038 err = ip6_mc_msfget(sk, &gsf,
1039 (struct group_filter __user *)optval, optlen);
1040 release_sock(sk);
1041 return err;
1042 }
1043
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001044 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 {
1046 struct msghdr msg;
1047 struct sk_buff *skb;
1048
1049 if (sk->sk_type != SOCK_STREAM)
1050 return -ENOPROTOOPT;
1051
1052 msg.msg_control = optval;
1053 msg.msg_controllen = len;
Daniel Baluta98e77432011-08-19 03:19:07 -07001054 msg.msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 lock_sock(sk);
1057 skb = np->pktoptions;
1058 if (skb)
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001059 ip6_datagram_recv_ctl(sk, &msg, skb);
Eric Dumazet1dc7b902015-01-21 03:45:42 -08001060 release_sock(sk);
1061 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (np->rxopt.bits.rxinfo) {
1063 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001064 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1065 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001066 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1068 }
1069 if (np->rxopt.bits.rxhlim) {
1070 int hlim = np->mcast_hops;
1071 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1072 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001073 if (np->rxopt.bits.rxtclass) {
Li RongQingd76ed222014-01-15 17:03:30 +08001074 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1075
Jiri Benc4c507d22012-02-09 09:35:49 +00001076 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1077 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001078 if (np->rxopt.bits.rxoinfo) {
1079 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001080 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1081 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001082 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1083 np->sticky_pktinfo.ipi6_addr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001084 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1085 }
1086 if (np->rxopt.bits.rxohlim) {
1087 int hlim = np->mcast_hops;
1088 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1089 }
Florent Fourcot1397ed32013-12-08 15:46:57 +01001090 if (np->rxopt.bits.rxflow) {
Florent Fourcot68536052013-12-12 17:07:58 +01001091 __be32 flowinfo = np->rcv_flowinfo;
1092
Florent Fourcot1397ed32013-12-08 15:46:57 +01001093 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096 len -= msg.msg_controllen;
1097 return put_user(len, optlen);
1098 }
1099 case IPV6_MTU:
1100 {
1101 struct dst_entry *dst;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001102
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001103 val = 0;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001104 rcu_read_lock();
1105 dst = __sk_dst_get(sk);
1106 if (dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 val = dst_mtu(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001108 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (!val)
1110 return -ENOTCONN;
1111 break;
1112 }
1113
1114 case IPV6_V6ONLY:
Eric Dumazet9fe516b2014-06-27 08:36:16 -07001115 val = sk->sk_ipv6only;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 break;
1117
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001118 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 val = np->rxopt.bits.rxinfo;
1120 break;
1121
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001122 case IPV6_2292PKTINFO:
1123 val = np->rxopt.bits.rxoinfo;
1124 break;
1125
1126 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 val = np->rxopt.bits.rxhlim;
1128 break;
1129
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001130 case IPV6_2292HOPLIMIT:
1131 val = np->rxopt.bits.rxohlim;
1132 break;
1133
1134 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 val = np->rxopt.bits.srcrt;
1136 break;
1137
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001138 case IPV6_2292RTHDR:
1139 val = np->rxopt.bits.osrcrt;
1140 break;
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001143 case IPV6_RTHDRDSTOPTS:
1144 case IPV6_RTHDR:
1145 case IPV6_DSTOPTS:
1146 {
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001147 struct ipv6_txoptions *opt;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001148
1149 lock_sock(sk);
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +02001150 opt = rcu_dereference_protected(np->opt,
1151 lockdep_sock_is_held(sk));
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001152 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001153 release_sock(sk);
Yang Hongyang05335c22008-05-28 16:23:47 +08001154 /* check if ipv6_getsockopt_sticky() returns err code */
1155 if (len < 0)
1156 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001157 return put_user(len, optlen);
1158 }
1159
1160 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 val = np->rxopt.bits.hopopts;
1162 break;
1163
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001164 case IPV6_2292HOPOPTS:
1165 val = np->rxopt.bits.ohopopts;
1166 break;
1167
1168 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 val = np->rxopt.bits.dstopts;
1170 break;
1171
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001172 case IPV6_2292DSTOPTS:
1173 val = np->rxopt.bits.odstopts;
1174 break;
1175
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001176 case IPV6_TCLASS:
1177 val = np->tclass;
1178 break;
1179
1180 case IPV6_RECVTCLASS:
1181 val = np->rxopt.bits.rxtclass;
1182 break;
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 case IPV6_FLOWINFO:
1185 val = np->rxopt.bits.rxflow;
1186 break;
1187
Brian Haley793b14732010-04-23 11:26:07 +00001188 case IPV6_RECVPATHMTU:
1189 val = np->rxopt.bits.rxpmtu;
1190 break;
1191
1192 case IPV6_PATHMTU:
1193 {
1194 struct dst_entry *dst;
1195 struct ip6_mtuinfo mtuinfo;
1196
1197 if (len < sizeof(mtuinfo))
1198 return -EINVAL;
1199
1200 len = sizeof(mtuinfo);
1201 memset(&mtuinfo, 0, sizeof(mtuinfo));
1202
1203 rcu_read_lock();
1204 dst = __sk_dst_get(sk);
1205 if (dst)
1206 mtuinfo.ip6m_mtu = dst_mtu(dst);
1207 rcu_read_unlock();
1208 if (!mtuinfo.ip6m_mtu)
1209 return -ENOTCONN;
1210
1211 if (put_user(len, optlen))
1212 return -EFAULT;
1213 if (copy_to_user(optval, &mtuinfo, len))
1214 return -EFAULT;
1215
1216 return 0;
Brian Haley793b14732010-04-23 11:26:07 +00001217 }
1218
Balazs Scheidler6c468622010-10-21 16:08:28 +02001219 case IPV6_TRANSPARENT:
1220 val = inet_sk(sk)->transparent;
1221 break;
1222
1223 case IPV6_RECVORIGDSTADDR:
1224 val = np->rxopt.bits.rxorigdstaddr;
1225 break;
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -08001229 {
1230 struct dst_entry *dst;
1231
1232 if (optname == IPV6_UNICAST_HOPS)
1233 val = np->hop_limit;
1234 else
1235 val = np->mcast_hops;
1236
Eric Dumazetb6c67122010-04-08 23:03:29 +00001237 if (val < 0) {
1238 rcu_read_lock();
1239 dst = __sk_dst_get(sk);
1240 if (dst)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001241 val = ip6_dst_hoplimit(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001242 rcu_read_unlock();
Brian Haleybefffe92006-12-12 17:09:49 -08001243 }
Eric Dumazetb6c67122010-04-08 23:03:29 +00001244
Brian Haleybefffe92006-12-12 17:09:49 -08001245 if (val < 0)
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001246 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 break;
Brian Haleybefffe92006-12-12 17:09:49 -08001248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 case IPV6_MULTICAST_LOOP:
1251 val = np->mc_loop;
1252 break;
1253
1254 case IPV6_MULTICAST_IF:
1255 val = np->mcast_oif;
1256 break;
1257
Erich E. Hooverc4062df2012-02-08 09:11:08 +00001258 case IPV6_UNICAST_IF:
1259 val = (__force int)htonl((__u32) np->ucast_oif);
1260 break;
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 case IPV6_MTU_DISCOVER:
1263 val = np->pmtudisc;
1264 break;
1265
1266 case IPV6_RECVERR:
1267 val = np->recverr;
1268 break;
1269
1270 case IPV6_FLOWINFO_SEND:
1271 val = np->sndflow;
1272 break;
1273
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001274 case IPV6_FLOWLABEL_MGR:
1275 {
1276 struct in6_flowlabel_req freq;
Florent Fourcot46e5f402014-01-17 17:15:04 +01001277 int flags;
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001278
1279 if (len < sizeof(freq))
1280 return -EINVAL;
1281
1282 if (copy_from_user(&freq, optval, sizeof(freq)))
1283 return -EFAULT;
1284
1285 if (freq.flr_action != IPV6_FL_A_GET)
1286 return -EINVAL;
1287
1288 len = sizeof(freq);
Florent Fourcot46e5f402014-01-17 17:15:04 +01001289 flags = freq.flr_flags;
1290
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001291 memset(&freq, 0, sizeof(freq));
1292
Florent Fourcot46e5f402014-01-17 17:15:04 +01001293 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001294 if (val < 0)
1295 return val;
1296
1297 if (put_user(len, optlen))
1298 return -EFAULT;
1299 if (copy_to_user(optval, &freq, len))
1300 return -EFAULT;
1301
1302 return 0;
1303 }
1304
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001305 case IPV6_ADDR_PREFERENCES:
1306 val = 0;
1307
1308 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1309 val |= IPV6_PREFER_SRC_TMP;
1310 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1311 val |= IPV6_PREFER_SRC_PUBLIC;
1312 else {
1313 /* XXX: should we return system default? */
1314 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1315 }
1316
1317 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1318 val |= IPV6_PREFER_SRC_COA;
1319 else
1320 val |= IPV6_PREFER_SRC_HOME;
1321 break;
1322
Stephen Hemmingere802af92010-04-22 15:24:53 -07001323 case IPV6_MINHOPCOUNT:
1324 val = np->min_hopcount;
1325 break;
1326
Brian Haley793b14732010-04-23 11:26:07 +00001327 case IPV6_DONTFRAG:
1328 val = np->dontfrag;
1329 break;
1330
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001331 case IPV6_AUTOFLOWLABEL:
1332 val = np->autoflowlabel;
1333 break;
1334
Willem de Bruijn0cc0aa612016-11-02 11:02:17 -04001335 case IPV6_RECVFRAGSIZE:
1336 val = np->rxopt.bits.recvfragsize;
1337 break;
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001340 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342 len = min_t(unsigned int, sizeof(int), len);
Ian Morris67ba4152014-08-24 21:53:10 +01001343 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001345 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return -EFAULT;
1347 return 0;
1348}
1349
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001350int ipv6_getsockopt(struct sock *sk, int level, int optname,
1351 char __user *optval, int __user *optlen)
1352{
1353 int err;
1354
1355 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1356 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1357
Ian Morris67ba4152014-08-24 21:53:10 +01001358 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001359 return -ENOPROTOOPT;
1360
Daniel Baluta98e77432011-08-19 03:19:07 -07001361 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001362#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001363 /* we need to exclude all possible ENOPROTOOPTs except default case */
1364 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001365 int len;
1366
1367 if (get_user(len, optlen))
1368 return -EFAULT;
1369
1370 lock_sock(sk);
1371 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1372 &len);
1373 release_sock(sk);
1374 if (err >= 0)
1375 err = put_user(len, optlen);
1376 }
1377#endif
1378 return err;
1379}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001380EXPORT_SYMBOL(ipv6_getsockopt);
1381
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001382#ifdef CONFIG_COMPAT
1383int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001384 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001385{
1386 int err;
1387
1388 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001389 if (udp_prot.compat_getsockopt != NULL)
1390 return udp_prot.compat_getsockopt(sk, level, optname,
1391 optval, optlen);
1392 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001393 }
1394
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001395 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001396 return -ENOPROTOOPT;
1397
Pavel Emelyanov80991792008-04-30 14:49:54 -07001398 if (optname == MCAST_MSFILTER)
1399 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1400 ipv6_getsockopt);
1401
Daniel Baluta98e77432011-08-19 03:19:07 -07001402 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1403 MSG_CMSG_COMPAT);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001404#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001405 /* we need to exclude all possible ENOPROTOOPTs except default case */
1406 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001407 int len;
1408
1409 if (get_user(len, optlen))
1410 return -EFAULT;
1411
1412 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001413 err = compat_nf_getsockopt(sk, PF_INET6,
1414 optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001415 release_sock(sk);
1416 if (err >= 0)
1417 err = put_user(len, optlen);
1418 }
1419#endif
1420 return err;
1421}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001422EXPORT_SYMBOL(compat_ipv6_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001423#endif
1424