blob: 63e6956917c9cf20ee74968de3a7f03c1b48c849 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#include <asm/uaccess.h>
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058struct ip6_ra_chain *ip6_ra_chain;
59DEFINE_RWLOCK(ip6_ra_lock);
60
Denis V. Lunev725a8ff2008-07-19 00:28:58 -070061int ip6_ra_control(struct sock *sk, int sel)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 struct ip6_ra_chain *ra, *new_ra, **rap;
64
65 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
Eric Dumazetc720c7e2009-10-15 06:30:45 +000066 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num != IPPROTO_RAW)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +090067 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Ian Morris67ba4152014-08-24 21:53:10 +010069 new_ra = (sel >= 0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 write_lock_bh(&ip6_ra_lock);
Ian Morris67ba4152014-08-24 21:53:10 +010072 for (rap = &ip6_ra_chain; (ra = *rap) != NULL; rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (ra->sk == sk) {
Ian Morris67ba4152014-08-24 21:53:10 +010074 if (sel >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 write_unlock_bh(&ip6_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -080076 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return -EADDRINUSE;
78 }
79
80 *rap = ra->next;
81 write_unlock_bh(&ip6_ra_lock);
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 sock_put(sk);
84 kfree(ra);
85 return 0;
86 }
87 }
Ian Morris63159f22015-03-29 14:00:04 +010088 if (!new_ra) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 write_unlock_bh(&ip6_ra_lock);
90 return -ENOBUFS;
91 }
92 new_ra->sk = sk;
93 new_ra->sel = sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 new_ra->next = ra;
95 *rap = new_ra;
96 sock_hold(sk);
97 write_unlock_bh(&ip6_ra_lock);
98 return 0;
99}
100
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700101static
102struct 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 Dumazetc720c7e2009-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 }
Duan Jiong86fe8f82014-12-03 10:29:40 +0800114 opt = xchg(&inet6_sk(sk)->opt, opt);
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700115 sk_dst_reset(sk);
116
117 return opt;
118}
119
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300120static bool setsockopt_needs_rtnl(int optname)
121{
122 switch (optname) {
123 case IPV6_ADD_MEMBERSHIP:
124 case IPV6_DROP_MEMBERSHIP:
Marcelo Ricardo Leitnerc4a68532015-03-20 11:37:17 -0300125 case IPV6_JOIN_ANYCAST:
126 case IPV6_LEAVE_ANYCAST:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300127 case MCAST_JOIN_GROUP:
128 case MCAST_LEAVE_GROUP:
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300129 case MCAST_JOIN_SOURCE_GROUP:
130 case MCAST_LEAVE_SOURCE_GROUP:
131 case MCAST_BLOCK_SOURCE:
132 case MCAST_UNBLOCK_SOURCE:
133 case MCAST_MSFILTER:
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300134 return true;
135 }
136 return false;
137}
138
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800139static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700140 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900143 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int val, valbool;
145 int retv = -ENOPROTOOPT;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300146 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Ian Morris63159f22015-03-29 14:00:04 +0100148 if (!optval)
Ian Morris67ba4152014-08-24 21:53:10 +0100149 val = 0;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900150 else {
151 if (optlen >= sizeof(int)) {
152 if (get_user(val, (int __user *) optval))
153 return -EFAULT;
154 } else
155 val = 0;
156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Ian Morris67ba4152014-08-24 21:53:10 +0100158 valbool = (val != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900160 if (ip6_mroute_opt(optname))
161 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
162
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300163 if (needs_rtnl)
164 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 lock_sock(sk);
166
167 switch (optname) {
168
169 case IPV6_ADDRFORM:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900170 if (optlen < sizeof(int))
171 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (val == PF_INET) {
173 struct ipv6_txoptions *opt;
174 struct sk_buff *pktopt;
175
Denis V. Lunev49d074f2008-06-04 15:49:06 +0400176 if (sk->sk_type == SOCK_RAW)
177 break;
178
Denis V. Lunev9596cc822008-06-04 15:49:08 +0400179 if (sk->sk_protocol == IPPROTO_UDP ||
180 sk->sk_protocol == IPPROTO_UDPLITE) {
181 struct udp_sock *up = udp_sk(sk);
182 if (up->pending == AF_INET6) {
183 retv = -EBUSY;
184 break;
185 }
186 } else if (sk->sk_protocol != IPPROTO_TCP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 break;
188
189 if (sk->sk_state != TCP_ESTABLISHED) {
190 retv = -ENOTCONN;
191 break;
192 }
193
194 if (ipv6_only_sock(sk) ||
Eric Dumazetefe42082013-10-03 15:42:29 -0700195 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 retv = -EADDRNOTAVAIL;
197 break;
198 }
199
200 fl6_free_socklist(sk);
201 ipv6_sock_mc_close(sk);
202
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700203 /*
204 * Sock is moving from IPv6 to IPv4 (sk_prot), so
205 * remove it from the refcnt debug socks count in the
206 * original family...
207 */
208 sk_refcnt_debug_dec(sk);
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (sk->sk_protocol == IPPROTO_TCP) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800211 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700213 sock_prot_inuse_add(net, sk->sk_prot, -1);
214 sock_prot_inuse_add(net, &tcp_prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 local_bh_enable();
216 sk->sk_prot = &tcp_prot;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800217 icsk->icsk_af_ops = &ipv4_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 sk->sk_socket->ops = &inet_stream_ops;
219 sk->sk_family = PF_INET;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800220 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 } else {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800222 struct proto *prot = &udp_prot;
223
David S. Millerdb8dac22008-03-06 16:22:02 -0800224 if (sk->sk_protocol == IPPROTO_UDPLITE)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800225 prot = &udplite_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700227 sock_prot_inuse_add(net, sk->sk_prot, -1);
228 sock_prot_inuse_add(net, prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 local_bh_enable();
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800230 sk->sk_prot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 sk->sk_socket->ops = &inet_dgram_ops;
232 sk->sk_family = PF_INET;
233 }
234 opt = xchg(&np->opt, NULL);
235 if (opt)
236 sock_kfree_s(sk, opt, opt->tot_len);
237 pktopt = xchg(&np->pktoptions, NULL);
Wei Yongjun800d55f2009-02-23 21:45:33 +0000238 kfree_skb(pktopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 sk->sk_destruct = inet_sock_destruct;
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700241 /*
242 * ... and add it to the refcnt debug socks count
243 * in the new family. -acme
244 */
245 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 module_put(THIS_MODULE);
247 retv = 0;
248 break;
249 }
250 goto e_inval;
251
252 case IPV6_V6ONLY:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900253 if (optlen < sizeof(int) ||
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000254 inet_sk(sk)->inet_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 goto e_inval;
Eric Dumazet9fe516b2014-06-27 08:36:16 -0700256 sk->sk_ipv6only = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 retv = 0;
258 break;
259
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900260 case IPV6_RECVPKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900261 if (optlen < sizeof(int))
262 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 np->rxopt.bits.rxinfo = valbool;
264 retv = 0;
265 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900266
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900267 case IPV6_2292PKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900268 if (optlen < sizeof(int))
269 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900270 np->rxopt.bits.rxoinfo = valbool;
271 retv = 0;
272 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900274 case IPV6_RECVHOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900275 if (optlen < sizeof(int))
276 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 np->rxopt.bits.rxhlim = valbool;
278 retv = 0;
279 break;
280
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900281 case IPV6_2292HOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900282 if (optlen < sizeof(int))
283 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900284 np->rxopt.bits.rxohlim = valbool;
285 retv = 0;
286 break;
287
288 case IPV6_RECVRTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900289 if (optlen < sizeof(int))
290 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900291 np->rxopt.bits.srcrt = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 retv = 0;
293 break;
294
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900295 case IPV6_2292RTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900296 if (optlen < sizeof(int))
297 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900298 np->rxopt.bits.osrcrt = valbool;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900299 retv = 0;
300 break;
301
302 case IPV6_RECVHOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900303 if (optlen < sizeof(int))
304 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 np->rxopt.bits.hopopts = valbool;
306 retv = 0;
307 break;
308
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900309 case IPV6_2292HOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900310 if (optlen < sizeof(int))
311 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900312 np->rxopt.bits.ohopopts = valbool;
313 retv = 0;
314 break;
315
316 case IPV6_RECVDSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900317 if (optlen < sizeof(int))
318 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 np->rxopt.bits.dstopts = valbool;
320 retv = 0;
321 break;
322
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900323 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900324 if (optlen < sizeof(int))
325 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900326 np->rxopt.bits.odstopts = valbool;
327 retv = 0;
328 break;
329
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900330 case IPV6_TCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900331 if (optlen < sizeof(int))
332 goto e_inval;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700333 if (val < -1 || val > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900334 goto e_inval;
Gerrit Renker26ced1e2009-08-09 08:12:49 +0000335 /* RFC 3542, 6.5: default traffic class of 0x0 */
336 if (val == -1)
337 val = 0;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900338 np->tclass = val;
339 retv = 0;
340 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900341
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900342 case IPV6_RECVTCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900343 if (optlen < sizeof(int))
344 goto e_inval;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900345 np->rxopt.bits.rxtclass = valbool;
346 retv = 0;
347 break;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 case IPV6_FLOWINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900350 if (optlen < sizeof(int))
351 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 np->rxopt.bits.rxflow = valbool;
353 retv = 0;
354 break;
355
Brian Haley793b1472010-04-23 11:26:07 +0000356 case IPV6_RECVPATHMTU:
357 if (optlen < sizeof(int))
358 goto e_inval;
359 np->rxopt.bits.rxpmtu = valbool;
360 retv = 0;
361 break;
362
Balazs Scheidler6c468622010-10-21 16:08:28 +0200363 case IPV6_TRANSPARENT:
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000364 if (valbool && !ns_capable(net->user_ns, CAP_NET_ADMIN) &&
365 !ns_capable(net->user_ns, CAP_NET_RAW)) {
Balazs Scheidlerb8894162010-10-23 04:48:14 +0000366 retv = -EPERM;
367 break;
368 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200369 if (optlen < sizeof(int))
370 goto e_inval;
371 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
372 inet_sk(sk)->transparent = valbool;
373 retv = 0;
374 break;
375
376 case IPV6_RECVORIGDSTADDR:
377 if (optlen < sizeof(int))
378 goto e_inval;
379 np->rxopt.bits.rxorigdstaddr = valbool;
380 retv = 0;
381 break;
382
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900383 case IPV6_HOPOPTS:
384 case IPV6_RTHDRDSTOPTS:
385 case IPV6_RTHDR:
386 case IPV6_DSTOPTS:
387 {
388 struct ipv6_txoptions *opt;
Shan Weiaea74272008-06-19 16:29:39 -0700389
390 /* remove any sticky options header with a zero option
391 * length, per RFC3542.
392 */
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900393 if (optlen == 0)
Luiz Capitulinocb422c42005-11-14 21:43:36 -0800394 optval = NULL;
Ian Morris63159f22015-03-29 14:00:04 +0100395 else if (!optval)
Yang Hongyangcfb266c2008-08-03 18:16:15 -0700396 goto e_inval;
Shan Weiaea74272008-06-19 16:29:39 -0700397 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
398 optlen & 0x7 || optlen > 8 * 255)
399 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900400
401 /* hop-by-hop / destination options are privileged option */
402 retv = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000403 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900404 break;
405
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900406 opt = ipv6_renew_options(sk, np->opt, optname,
407 (struct ipv6_opt_hdr __user *)optval,
408 optlen);
409 if (IS_ERR(opt)) {
410 retv = PTR_ERR(opt);
411 break;
412 }
413
414 /* routing header option needs extra check */
Brian Haley6e093d92008-11-12 22:59:21 -0800415 retv = -EINVAL;
Olaf Kirchdfee0a72007-03-09 13:55:38 -0800416 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900417 struct ipv6_rt_hdr *rthdr = opt->srcrt;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700418 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000419#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700420 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800421 if (rthdr->hdrlen != 2 ||
422 rthdr->segments_left != 1)
423 goto sticky_done;
424
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700425 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700426#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700427 default:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900428 goto sticky_done;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700429 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900430 }
431
432 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700433 opt = ipv6_update_options(sk, opt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900434sticky_done:
435 if (opt)
436 sock_kfree_s(sk, opt, opt->tot_len);
437 break;
438 }
439
Yang Hongyangb24a2512008-12-16 02:06:23 -0800440 case IPV6_PKTINFO:
441 {
442 struct in6_pktinfo pkt;
443
444 if (optlen == 0)
445 goto e_inval;
Ian Morris63159f22015-03-29 14:00:04 +0100446 else if (optlen < sizeof(struct in6_pktinfo) || !optval)
Yang Hongyangb24a2512008-12-16 02:06:23 -0800447 goto e_inval;
448
Ilpo Järvinen914d1162009-01-04 17:27:31 -0800449 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
Yang Hongyangb24a2512008-12-16 02:06:23 -0800450 retv = -EFAULT;
451 break;
452 }
453 if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if)
454 goto e_inval;
455
456 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000457 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
Yang Hongyangb24a2512008-12-16 02:06:23 -0800458 retv = 0;
459 break;
460 }
461
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900462 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 {
464 struct ipv6_txoptions *opt = NULL;
465 struct msghdr msg;
David S. Miller4c9483b2011-03-12 16:22:43 -0500466 struct flowi6 fl6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 int junk;
468
David S. Miller4c9483b2011-03-12 16:22:43 -0500469 memset(&fl6, 0, sizeof(fl6));
470 fl6.flowi6_oif = sk->sk_bound_dev_if;
471 fl6.flowi6_mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 if (optlen == 0)
474 goto update;
475
476 /* 1K is probably excessive
477 * 1K is surely not enough, 2K per standard header is 16K.
478 */
479 retv = -EINVAL;
480 if (optlen > 64*1024)
481 break;
482
483 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
484 retv = -ENOBUFS;
Ian Morris63159f22015-03-29 14:00:04 +0100485 if (!opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487
488 memset(opt, 0, sizeof(*opt));
489 opt->tot_len = sizeof(*opt) + optlen;
490 retv = -EFAULT;
491 if (copy_from_user(opt+1, optval, optlen))
492 goto done;
493
494 msg.msg_controllen = optlen;
Ian Morris67ba4152014-08-24 21:53:10 +0100495 msg.msg_control = (void *)(opt+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Tom Parkin73df66f2013-01-31 01:02:24 +0000497 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, opt, &junk,
498 &junk, &junk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (retv)
500 goto done;
501update:
502 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700503 opt = ipv6_update_options(sk, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504done:
505 if (opt)
506 sock_kfree_s(sk, opt, opt->tot_len);
507 break;
508 }
509 case IPV6_UNICAST_HOPS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900510 if (optlen < sizeof(int))
511 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (val > 255 || val < -1)
513 goto e_inval;
514 np->hop_limit = val;
515 retv = 0;
516 break;
517
518 case IPV6_MULTICAST_HOPS:
519 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900520 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900521 if (optlen < sizeof(int))
522 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (val > 255 || val < -1)
524 goto e_inval;
Li Wei2a38e6d2011-11-27 21:33:34 +0000525 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 retv = 0;
527 break;
528
529 case IPV6_MULTICAST_LOOP:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900530 if (optlen < sizeof(int))
531 goto e_inval;
YOSHIFUJI Hideaki28d44882008-06-12 03:14:51 +0900532 if (val != valbool)
533 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 np->mc_loop = valbool;
535 retv = 0;
536 break;
537
Erich E. Hooverc4062df2012-02-08 09:11:08 +0000538 case IPV6_UNICAST_IF:
539 {
540 struct net_device *dev = NULL;
541 int ifindex;
542
543 if (optlen != sizeof(int))
544 goto e_inval;
545
546 ifindex = (__force int)ntohl((__force __be32)val);
547 if (ifindex == 0) {
548 np->ucast_oif = 0;
549 retv = 0;
550 break;
551 }
552
553 dev = dev_get_by_index(net, ifindex);
554 retv = -EADDRNOTAVAIL;
555 if (!dev)
556 break;
557 dev_put(dev);
558
559 retv = -EINVAL;
560 if (sk->sk_bound_dev_if)
561 break;
562
563 np->ucast_oif = ifindex;
564 retv = 0;
565 break;
566 }
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 case IPV6_MULTICAST_IF:
569 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900570 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900571 if (optlen < sizeof(int))
572 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Brian Haley4953f0f2007-10-11 14:39:29 -0700574 if (val) {
Eric Dumazet55b80502009-10-19 06:41:58 +0000575 struct net_device *dev;
576
Brian Haley4953f0f2007-10-11 14:39:29 -0700577 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
578 goto e_inval;
579
Eric Dumazet55b80502009-10-19 06:41:58 +0000580 dev = dev_get_by_index(net, val);
581 if (!dev) {
Brian Haley4953f0f2007-10-11 14:39:29 -0700582 retv = -ENODEV;
583 break;
584 }
Eric Dumazet55b80502009-10-19 06:41:58 +0000585 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587 np->mcast_oif = val;
588 retv = 0;
589 break;
590 case IPV6_ADD_MEMBERSHIP:
591 case IPV6_DROP_MEMBERSHIP:
592 {
593 struct ipv6_mreq mreq;
594
Wang Chena28398b2008-04-07 09:42:07 +0800595 if (optlen < sizeof(struct ipv6_mreq))
596 goto e_inval;
597
Flavio Leitnera96fb492007-08-24 22:16:39 -0700598 retv = -EPROTO;
599 if (inet_sk(sk)->is_icsk)
600 break;
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 retv = -EFAULT;
603 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
604 break;
605
606 if (optname == IPV6_ADD_MEMBERSHIP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300607 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300609 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 break;
611 }
612 case IPV6_JOIN_ANYCAST:
613 case IPV6_LEAVE_ANYCAST:
614 {
615 struct ipv6_mreq mreq;
616
Wang Chena28398b2008-04-07 09:42:07 +0800617 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 goto e_inval;
619
620 retv = -EFAULT;
621 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
622 break;
623
624 if (optname == IPV6_JOIN_ANYCAST)
625 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
626 else
627 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
628 break;
629 }
630 case MCAST_JOIN_GROUP:
631 case MCAST_LEAVE_GROUP:
632 {
633 struct group_req greq;
634 struct sockaddr_in6 *psin6;
635
Wang Chena28398b2008-04-07 09:42:07 +0800636 if (optlen < sizeof(struct group_req))
637 goto e_inval;
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 retv = -EFAULT;
640 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
641 break;
642 if (greq.gr_group.ss_family != AF_INET6) {
643 retv = -EADDRNOTAVAIL;
644 break;
645 }
646 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
647 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300648 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
649 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300651 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
652 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 break;
654 }
655 case MCAST_JOIN_SOURCE_GROUP:
656 case MCAST_LEAVE_SOURCE_GROUP:
657 case MCAST_BLOCK_SOURCE:
658 case MCAST_UNBLOCK_SOURCE:
659 {
660 struct group_source_req greqs;
661 int omode, add;
662
Wang Chena28398b2008-04-07 09:42:07 +0800663 if (optlen < sizeof(struct group_source_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 goto e_inval;
665 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
666 retv = -EFAULT;
667 break;
668 }
669 if (greqs.gsr_group.ss_family != AF_INET6 ||
670 greqs.gsr_source.ss_family != AF_INET6) {
671 retv = -EADDRNOTAVAIL;
672 break;
673 }
674 if (optname == MCAST_BLOCK_SOURCE) {
675 omode = MCAST_EXCLUDE;
676 add = 1;
677 } else if (optname == MCAST_UNBLOCK_SOURCE) {
678 omode = MCAST_EXCLUDE;
679 add = 0;
680 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
681 struct sockaddr_in6 *psin6;
682
683 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300684 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
685 &psin6->sin6_addr);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700686 /* prior join w/ different source is ok */
687 if (retv && retv != -EADDRINUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 break;
689 omode = MCAST_INCLUDE;
690 add = 1;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700691 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 omode = MCAST_INCLUDE;
693 add = 0;
694 }
695 retv = ip6_mc_source(add, omode, sk, &greqs);
696 break;
697 }
698 case MCAST_MSFILTER:
699 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct group_filter *gsf;
701
702 if (optlen < GROUP_FILTER_SIZE(0))
703 goto e_inval;
704 if (optlen > sysctl_optmem_max) {
705 retv = -ENOBUFS;
706 break;
707 }
Ian Morris67ba4152014-08-24 21:53:10 +0100708 gsf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700709 if (!gsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 retv = -ENOBUFS;
711 break;
712 }
713 retv = -EFAULT;
714 if (copy_from_user(gsf, optval, optlen)) {
715 kfree(gsf);
716 break;
717 }
718 /* numsrc >= (4G-140)/128 overflow in 32 bits */
719 if (gsf->gf_numsrc >= 0x1ffffffU ||
720 gsf->gf_numsrc > sysctl_mld_max_msf) {
721 kfree(gsf);
722 retv = -ENOBUFS;
723 break;
724 }
725 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
726 kfree(gsf);
727 retv = -EINVAL;
728 break;
729 }
730 retv = ip6_mc_msfilter(sk, gsf);
731 kfree(gsf);
732
733 break;
734 }
735 case IPV6_ROUTER_ALERT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900736 if (optlen < sizeof(int))
737 goto e_inval;
Denis V. Lunev725a8ff2008-07-19 00:28:58 -0700738 retv = ip6_ra_control(sk, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 break;
740 case IPV6_MTU_DISCOVER:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900741 if (optlen < sizeof(int))
742 goto e_inval;
Hannes Frederic Sowa0b952272014-02-26 01:20:43 +0100743 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 goto e_inval;
745 np->pmtudisc = val;
746 retv = 0;
747 break;
748 case IPV6_MTU:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900749 if (optlen < sizeof(int))
750 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (val && val < IPV6_MIN_MTU)
752 goto e_inval;
753 np->frag_size = val;
754 retv = 0;
755 break;
756 case IPV6_RECVERR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900757 if (optlen < sizeof(int))
758 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 np->recverr = valbool;
760 if (!val)
761 skb_queue_purge(&sk->sk_error_queue);
762 retv = 0;
763 break;
764 case IPV6_FLOWINFO_SEND:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900765 if (optlen < sizeof(int))
766 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 np->sndflow = valbool;
768 retv = 0;
769 break;
770 case IPV6_FLOWLABEL_MGR:
771 retv = ipv6_flowlabel_opt(sk, optval, optlen);
772 break;
773 case IPV6_IPSEC_POLICY:
774 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700775 retv = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000776 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700777 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 retv = xfrm_user_policy(sk, optname, optval, optlen);
779 break;
780
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900781 case IPV6_ADDR_PREFERENCES:
782 {
783 unsigned int pref = 0;
784 unsigned int prefmask = ~0;
785
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900786 if (optlen < sizeof(int))
787 goto e_inval;
788
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900789 retv = -EINVAL;
790
791 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
792 switch (val & (IPV6_PREFER_SRC_PUBLIC|
793 IPV6_PREFER_SRC_TMP|
794 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
795 case IPV6_PREFER_SRC_PUBLIC:
796 pref |= IPV6_PREFER_SRC_PUBLIC;
797 break;
798 case IPV6_PREFER_SRC_TMP:
799 pref |= IPV6_PREFER_SRC_TMP;
800 break;
801 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
802 break;
803 case 0:
804 goto pref_skip_pubtmp;
805 default:
806 goto e_inval;
807 }
808
809 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
810 IPV6_PREFER_SRC_TMP);
811pref_skip_pubtmp:
812
813 /* check HOME/COA conflicts */
814 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
815 case IPV6_PREFER_SRC_HOME:
816 break;
817 case IPV6_PREFER_SRC_COA:
818 pref |= IPV6_PREFER_SRC_COA;
819 case 0:
820 goto pref_skip_coa;
821 default:
822 goto e_inval;
823 }
824
825 prefmask &= ~IPV6_PREFER_SRC_COA;
826pref_skip_coa:
827
828 /* check CGA/NONCGA conflicts */
829 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
830 case IPV6_PREFER_SRC_CGA:
831 case IPV6_PREFER_SRC_NONCGA:
832 case 0:
833 break;
834 default:
835 goto e_inval;
836 }
837
838 np->srcprefs = (np->srcprefs & prefmask) | pref;
839 retv = 0;
840
841 break;
842 }
Stephen Hemmingere802af92010-04-22 15:24:53 -0700843 case IPV6_MINHOPCOUNT:
844 if (optlen < sizeof(int))
845 goto e_inval;
846 if (val < 0 || val > 255)
847 goto e_inval;
848 np->min_hopcount = val;
Hannes Frederic Sowad4596ba2012-11-10 19:52:34 +0000849 retv = 0;
Brian Haley793b1472010-04-23 11:26:07 +0000850 break;
851 case IPV6_DONTFRAG:
852 np->dontfrag = valbool;
Stephen Hemmingere802af92010-04-22 15:24:53 -0700853 retv = 0;
854 break;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700855 case IPV6_AUTOFLOWLABEL:
856 np->autoflowlabel = valbool;
857 retv = 0;
858 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300862 if (needs_rtnl)
863 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return retv;
866
867e_inval:
868 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300869 if (needs_rtnl)
870 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return -EINVAL;
872}
873
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800874int ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700875 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800876{
877 int err;
878
879 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
880 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
881
882 if (level != SOL_IPV6)
883 return -ENOPROTOOPT;
884
885 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
886#ifdef CONFIG_NETFILTER
887 /* we need to exclude all possible ENOPROTOOPTs except default case */
888 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
889 optname != IPV6_XFRM_POLICY) {
890 lock_sock(sk);
891 err = nf_setsockopt(sk, PF_INET6, optname, optval,
892 optlen);
893 release_sock(sk);
894 }
895#endif
896 return err;
897}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900898EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800899
900#ifdef CONFIG_COMPAT
901int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700902 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800903{
904 int err;
905
906 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800907 if (udp_prot.compat_setsockopt != NULL)
908 return udp_prot.compat_setsockopt(sk, level, optname,
909 optval, optlen);
910 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800911 }
912
913 if (level != SOL_IPV6)
914 return -ENOPROTOOPT;
915
David L Stevensdae50292008-04-27 01:06:07 -0700916 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
917 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
918 ipv6_setsockopt);
919
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800920 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
921#ifdef CONFIG_NETFILTER
922 /* we need to exclude all possible ENOPROTOOPTs except default case */
923 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800924 optname != IPV6_XFRM_POLICY) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800925 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800926 err = compat_nf_setsockopt(sk, PF_INET6, optname,
927 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800928 release_sock(sk);
929 }
930#endif
931 return err;
932}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800933EXPORT_SYMBOL(compat_ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800934#endif
935
David S. Miller286930792007-03-07 12:50:46 -0800936static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700937 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900938{
David S. Miller286930792007-03-07 12:50:46 -0800939 struct ipv6_opt_hdr *hdr;
940
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700941 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900942 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700943
Ian Morris67ba4152014-08-24 21:53:10 +0100944 switch (optname) {
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700945 case IPV6_HOPOPTS:
946 hdr = opt->hopopt;
947 break;
948 case IPV6_RTHDRDSTOPTS:
949 hdr = opt->dst0opt;
950 break;
951 case IPV6_RTHDR:
952 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
953 break;
954 case IPV6_DSTOPTS:
955 hdr = opt->dst1opt;
956 break;
957 default:
958 return -EINVAL; /* should not happen */
959 }
960
961 if (!hdr)
962 return 0;
David S. Miller286930792007-03-07 12:50:46 -0800963
Chris Wrightd2b02ed2007-03-09 16:19:17 -0800964 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -0700965 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900966 return -EFAULT;
Yang Hongyang95b496b2008-05-28 16:27:28 +0800967 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900968}
969
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800970static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +0000971 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 struct ipv6_pinfo *np = inet6_sk(sk);
974 int len;
975 int val;
976
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900977 if (ip6_mroute_opt(optname))
978 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (get_user(len, optlen))
981 return -EFAULT;
982 switch (optname) {
983 case IPV6_ADDRFORM:
984 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800985 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 sk->sk_protocol != IPPROTO_TCP)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900987 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (sk->sk_state != TCP_ESTABLISHED)
989 return -ENOTCONN;
990 val = sk->sk_family;
991 break;
992 case MCAST_MSFILTER:
993 {
994 struct group_filter gsf;
995 int err;
996
997 if (len < GROUP_FILTER_SIZE(0))
998 return -EINVAL;
999 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
1000 return -EFAULT;
YOSHIFUJI Hideaki20c61fb2008-04-28 14:40:55 +09001001 if (gsf.gf_group.ss_family != AF_INET6)
1002 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 lock_sock(sk);
1004 err = ip6_mc_msfget(sk, &gsf,
1005 (struct group_filter __user *)optval, optlen);
1006 release_sock(sk);
1007 return err;
1008 }
1009
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001010 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 {
1012 struct msghdr msg;
1013 struct sk_buff *skb;
1014
1015 if (sk->sk_type != SOCK_STREAM)
1016 return -ENOPROTOOPT;
1017
1018 msg.msg_control = optval;
1019 msg.msg_controllen = len;
Daniel Baluta98e77432011-08-19 03:19:07 -07001020 msg.msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 lock_sock(sk);
1023 skb = np->pktoptions;
1024 if (skb)
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +01001025 ip6_datagram_recv_ctl(sk, &msg, skb);
Eric Dumazet1dc7b902015-01-21 03:45:42 -08001026 release_sock(sk);
1027 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (np->rxopt.bits.rxinfo) {
1029 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001030 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1031 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001032 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1034 }
1035 if (np->rxopt.bits.rxhlim) {
1036 int hlim = np->mcast_hops;
1037 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1038 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001039 if (np->rxopt.bits.rxtclass) {
Li RongQingd76ed222014-01-15 17:03:30 +08001040 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1041
Jiri Benc4c507d22012-02-09 09:35:49 +00001042 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1043 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001044 if (np->rxopt.bits.rxoinfo) {
1045 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001046 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1047 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001048 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1049 np->sticky_pktinfo.ipi6_addr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001050 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1051 }
1052 if (np->rxopt.bits.rxohlim) {
1053 int hlim = np->mcast_hops;
1054 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1055 }
Florent Fourcot1397ed32013-12-08 15:46:57 +01001056 if (np->rxopt.bits.rxflow) {
Florent Fourcot68536052013-12-12 17:07:58 +01001057 __be32 flowinfo = np->rcv_flowinfo;
1058
Florent Fourcot1397ed32013-12-08 15:46:57 +01001059 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062 len -= msg.msg_controllen;
1063 return put_user(len, optlen);
1064 }
1065 case IPV6_MTU:
1066 {
1067 struct dst_entry *dst;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001068
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001069 val = 0;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001070 rcu_read_lock();
1071 dst = __sk_dst_get(sk);
1072 if (dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 val = dst_mtu(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001074 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (!val)
1076 return -ENOTCONN;
1077 break;
1078 }
1079
1080 case IPV6_V6ONLY:
Eric Dumazet9fe516b2014-06-27 08:36:16 -07001081 val = sk->sk_ipv6only;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 break;
1083
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001084 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 val = np->rxopt.bits.rxinfo;
1086 break;
1087
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001088 case IPV6_2292PKTINFO:
1089 val = np->rxopt.bits.rxoinfo;
1090 break;
1091
1092 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 val = np->rxopt.bits.rxhlim;
1094 break;
1095
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001096 case IPV6_2292HOPLIMIT:
1097 val = np->rxopt.bits.rxohlim;
1098 break;
1099
1100 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 val = np->rxopt.bits.srcrt;
1102 break;
1103
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001104 case IPV6_2292RTHDR:
1105 val = np->rxopt.bits.osrcrt;
1106 break;
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001109 case IPV6_RTHDRDSTOPTS:
1110 case IPV6_RTHDR:
1111 case IPV6_DSTOPTS:
1112 {
1113
1114 lock_sock(sk);
David S. Miller286930792007-03-07 12:50:46 -08001115 len = ipv6_getsockopt_sticky(sk, np->opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001116 optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001117 release_sock(sk);
Yang Hongyang05335c22008-05-28 16:23:47 +08001118 /* check if ipv6_getsockopt_sticky() returns err code */
1119 if (len < 0)
1120 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001121 return put_user(len, optlen);
1122 }
1123
1124 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 val = np->rxopt.bits.hopopts;
1126 break;
1127
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001128 case IPV6_2292HOPOPTS:
1129 val = np->rxopt.bits.ohopopts;
1130 break;
1131
1132 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 val = np->rxopt.bits.dstopts;
1134 break;
1135
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001136 case IPV6_2292DSTOPTS:
1137 val = np->rxopt.bits.odstopts;
1138 break;
1139
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001140 case IPV6_TCLASS:
1141 val = np->tclass;
1142 break;
1143
1144 case IPV6_RECVTCLASS:
1145 val = np->rxopt.bits.rxtclass;
1146 break;
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 case IPV6_FLOWINFO:
1149 val = np->rxopt.bits.rxflow;
1150 break;
1151
Brian Haley793b1472010-04-23 11:26:07 +00001152 case IPV6_RECVPATHMTU:
1153 val = np->rxopt.bits.rxpmtu;
1154 break;
1155
1156 case IPV6_PATHMTU:
1157 {
1158 struct dst_entry *dst;
1159 struct ip6_mtuinfo mtuinfo;
1160
1161 if (len < sizeof(mtuinfo))
1162 return -EINVAL;
1163
1164 len = sizeof(mtuinfo);
1165 memset(&mtuinfo, 0, sizeof(mtuinfo));
1166
1167 rcu_read_lock();
1168 dst = __sk_dst_get(sk);
1169 if (dst)
1170 mtuinfo.ip6m_mtu = dst_mtu(dst);
1171 rcu_read_unlock();
1172 if (!mtuinfo.ip6m_mtu)
1173 return -ENOTCONN;
1174
1175 if (put_user(len, optlen))
1176 return -EFAULT;
1177 if (copy_to_user(optval, &mtuinfo, len))
1178 return -EFAULT;
1179
1180 return 0;
Brian Haley793b1472010-04-23 11:26:07 +00001181 }
1182
Balazs Scheidler6c468622010-10-21 16:08:28 +02001183 case IPV6_TRANSPARENT:
1184 val = inet_sk(sk)->transparent;
1185 break;
1186
1187 case IPV6_RECVORIGDSTADDR:
1188 val = np->rxopt.bits.rxorigdstaddr;
1189 break;
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -08001193 {
1194 struct dst_entry *dst;
1195
1196 if (optname == IPV6_UNICAST_HOPS)
1197 val = np->hop_limit;
1198 else
1199 val = np->mcast_hops;
1200
Eric Dumazetb6c67122010-04-08 23:03:29 +00001201 if (val < 0) {
1202 rcu_read_lock();
1203 dst = __sk_dst_get(sk);
1204 if (dst)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001205 val = ip6_dst_hoplimit(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001206 rcu_read_unlock();
Brian Haleybefffe92006-12-12 17:09:49 -08001207 }
Eric Dumazetb6c67122010-04-08 23:03:29 +00001208
Brian Haleybefffe92006-12-12 17:09:49 -08001209 if (val < 0)
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001210 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 break;
Brian Haleybefffe92006-12-12 17:09:49 -08001212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 case IPV6_MULTICAST_LOOP:
1215 val = np->mc_loop;
1216 break;
1217
1218 case IPV6_MULTICAST_IF:
1219 val = np->mcast_oif;
1220 break;
1221
Erich E. Hooverc4062df2012-02-08 09:11:08 +00001222 case IPV6_UNICAST_IF:
1223 val = (__force int)htonl((__u32) np->ucast_oif);
1224 break;
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 case IPV6_MTU_DISCOVER:
1227 val = np->pmtudisc;
1228 break;
1229
1230 case IPV6_RECVERR:
1231 val = np->recverr;
1232 break;
1233
1234 case IPV6_FLOWINFO_SEND:
1235 val = np->sndflow;
1236 break;
1237
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001238 case IPV6_FLOWLABEL_MGR:
1239 {
1240 struct in6_flowlabel_req freq;
Florent Fourcot46e5f402014-01-17 17:15:04 +01001241 int flags;
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001242
1243 if (len < sizeof(freq))
1244 return -EINVAL;
1245
1246 if (copy_from_user(&freq, optval, sizeof(freq)))
1247 return -EFAULT;
1248
1249 if (freq.flr_action != IPV6_FL_A_GET)
1250 return -EINVAL;
1251
1252 len = sizeof(freq);
Florent Fourcot46e5f402014-01-17 17:15:04 +01001253 flags = freq.flr_flags;
1254
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001255 memset(&freq, 0, sizeof(freq));
1256
Florent Fourcot46e5f402014-01-17 17:15:04 +01001257 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001258 if (val < 0)
1259 return val;
1260
1261 if (put_user(len, optlen))
1262 return -EFAULT;
1263 if (copy_to_user(optval, &freq, len))
1264 return -EFAULT;
1265
1266 return 0;
1267 }
1268
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001269 case IPV6_ADDR_PREFERENCES:
1270 val = 0;
1271
1272 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1273 val |= IPV6_PREFER_SRC_TMP;
1274 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1275 val |= IPV6_PREFER_SRC_PUBLIC;
1276 else {
1277 /* XXX: should we return system default? */
1278 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1279 }
1280
1281 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1282 val |= IPV6_PREFER_SRC_COA;
1283 else
1284 val |= IPV6_PREFER_SRC_HOME;
1285 break;
1286
Stephen Hemmingere802af92010-04-22 15:24:53 -07001287 case IPV6_MINHOPCOUNT:
1288 val = np->min_hopcount;
1289 break;
1290
Brian Haley793b1472010-04-23 11:26:07 +00001291 case IPV6_DONTFRAG:
1292 val = np->dontfrag;
1293 break;
1294
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001295 case IPV6_AUTOFLOWLABEL:
1296 val = np->autoflowlabel;
1297 break;
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001300 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302 len = min_t(unsigned int, sizeof(int), len);
Ian Morris67ba4152014-08-24 21:53:10 +01001303 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001305 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return -EFAULT;
1307 return 0;
1308}
1309
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001310int ipv6_getsockopt(struct sock *sk, int level, int optname,
1311 char __user *optval, int __user *optlen)
1312{
1313 int err;
1314
1315 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1316 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1317
Ian Morris67ba4152014-08-24 21:53:10 +01001318 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001319 return -ENOPROTOOPT;
1320
Daniel Baluta98e77432011-08-19 03:19:07 -07001321 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001322#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001323 /* we need to exclude all possible ENOPROTOOPTs except default case */
1324 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001325 int len;
1326
1327 if (get_user(len, optlen))
1328 return -EFAULT;
1329
1330 lock_sock(sk);
1331 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1332 &len);
1333 release_sock(sk);
1334 if (err >= 0)
1335 err = put_user(len, optlen);
1336 }
1337#endif
1338 return err;
1339}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001340EXPORT_SYMBOL(ipv6_getsockopt);
1341
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001342#ifdef CONFIG_COMPAT
1343int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001344 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001345{
1346 int err;
1347
1348 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001349 if (udp_prot.compat_getsockopt != NULL)
1350 return udp_prot.compat_getsockopt(sk, level, optname,
1351 optval, optlen);
1352 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001353 }
1354
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001355 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001356 return -ENOPROTOOPT;
1357
Pavel Emelyanov80991792008-04-30 14:49:54 -07001358 if (optname == MCAST_MSFILTER)
1359 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1360 ipv6_getsockopt);
1361
Daniel Baluta98e77432011-08-19 03:19:07 -07001362 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1363 MSG_CMSG_COMPAT);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001364#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001365 /* we need to exclude all possible ENOPROTOOPTs except default case */
1366 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001367 int len;
1368
1369 if (get_user(len, optlen))
1370 return -EFAULT;
1371
1372 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001373 err = compat_nf_getsockopt(sk, PF_INET6,
1374 optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001375 release_sock(sk);
1376 if (err >= 0)
1377 err = put_user(len, optlen);
1378 }
1379#endif
1380 return err;
1381}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001382EXPORT_SYMBOL(compat_ipv6_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001383#endif
1384