blob: f2b731df8d7735a129a8619a4753dd0ab2c76d0d [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 Dumazetc720c7e82009-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 }
88 if (new_ra == NULL) {
89 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 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 }
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:
125 case MCAST_JOIN_GROUP:
126 case MCAST_LEAVE_GROUP:
127 return true;
128 }
129 return false;
130}
131
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800132static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700133 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900136 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int val, valbool;
138 int retv = -ENOPROTOOPT;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300139 bool needs_rtnl = setsockopt_needs_rtnl(optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (optval == NULL)
Ian Morris67ba4152014-08-24 21:53:10 +0100142 val = 0;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900143 else {
144 if (optlen >= sizeof(int)) {
145 if (get_user(val, (int __user *) optval))
146 return -EFAULT;
147 } else
148 val = 0;
149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Ian Morris67ba4152014-08-24 21:53:10 +0100151 valbool = (val != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900153 if (ip6_mroute_opt(optname))
154 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
155
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300156 if (needs_rtnl)
157 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 lock_sock(sk);
159
160 switch (optname) {
161
162 case IPV6_ADDRFORM:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900163 if (optlen < sizeof(int))
164 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (val == PF_INET) {
166 struct ipv6_txoptions *opt;
167 struct sk_buff *pktopt;
168
Denis V. Lunev49d074f2008-06-04 15:49:06 +0400169 if (sk->sk_type == SOCK_RAW)
170 break;
171
Denis V. Lunev9596cc822008-06-04 15:49:08 +0400172 if (sk->sk_protocol == IPPROTO_UDP ||
173 sk->sk_protocol == IPPROTO_UDPLITE) {
174 struct udp_sock *up = udp_sk(sk);
175 if (up->pending == AF_INET6) {
176 retv = -EBUSY;
177 break;
178 }
179 } else if (sk->sk_protocol != IPPROTO_TCP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 break;
181
182 if (sk->sk_state != TCP_ESTABLISHED) {
183 retv = -ENOTCONN;
184 break;
185 }
186
187 if (ipv6_only_sock(sk) ||
Eric Dumazetefe42082013-10-03 15:42:29 -0700188 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 retv = -EADDRNOTAVAIL;
190 break;
191 }
192
193 fl6_free_socklist(sk);
194 ipv6_sock_mc_close(sk);
195
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700196 /*
197 * Sock is moving from IPv6 to IPv4 (sk_prot), so
198 * remove it from the refcnt debug socks count in the
199 * original family...
200 */
201 sk_refcnt_debug_dec(sk);
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (sk->sk_protocol == IPPROTO_TCP) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800204 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700206 sock_prot_inuse_add(net, sk->sk_prot, -1);
207 sock_prot_inuse_add(net, &tcp_prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 local_bh_enable();
209 sk->sk_prot = &tcp_prot;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800210 icsk->icsk_af_ops = &ipv4_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 sk->sk_socket->ops = &inet_stream_ops;
212 sk->sk_family = PF_INET;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800213 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 } else {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800215 struct proto *prot = &udp_prot;
216
David S. Millerdb8dac22008-03-06 16:22:02 -0800217 if (sk->sk_protocol == IPPROTO_UDPLITE)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800218 prot = &udplite_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700220 sock_prot_inuse_add(net, sk->sk_prot, -1);
221 sock_prot_inuse_add(net, prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 local_bh_enable();
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800223 sk->sk_prot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 sk->sk_socket->ops = &inet_dgram_ops;
225 sk->sk_family = PF_INET;
226 }
227 opt = xchg(&np->opt, NULL);
228 if (opt)
229 sock_kfree_s(sk, opt, opt->tot_len);
230 pktopt = xchg(&np->pktoptions, NULL);
Wei Yongjun800d55f2009-02-23 21:45:33 +0000231 kfree_skb(pktopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 sk->sk_destruct = inet_sock_destruct;
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700234 /*
235 * ... and add it to the refcnt debug socks count
236 * in the new family. -acme
237 */
238 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 module_put(THIS_MODULE);
240 retv = 0;
241 break;
242 }
243 goto e_inval;
244
245 case IPV6_V6ONLY:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900246 if (optlen < sizeof(int) ||
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000247 inet_sk(sk)->inet_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto e_inval;
Eric Dumazet9fe516b2014-06-27 08:36:16 -0700249 sk->sk_ipv6only = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 retv = 0;
251 break;
252
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900253 case IPV6_RECVPKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900254 if (optlen < sizeof(int))
255 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 np->rxopt.bits.rxinfo = valbool;
257 retv = 0;
258 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900259
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900260 case IPV6_2292PKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900261 if (optlen < sizeof(int))
262 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900263 np->rxopt.bits.rxoinfo = valbool;
264 retv = 0;
265 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900267 case IPV6_RECVHOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900268 if (optlen < sizeof(int))
269 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 np->rxopt.bits.rxhlim = valbool;
271 retv = 0;
272 break;
273
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900274 case IPV6_2292HOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900275 if (optlen < sizeof(int))
276 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900277 np->rxopt.bits.rxohlim = valbool;
278 retv = 0;
279 break;
280
281 case IPV6_RECVRTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900282 if (optlen < sizeof(int))
283 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900284 np->rxopt.bits.srcrt = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 retv = 0;
286 break;
287
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900288 case IPV6_2292RTHDR:
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.osrcrt = valbool;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900292 retv = 0;
293 break;
294
295 case IPV6_RECVHOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900296 if (optlen < sizeof(int))
297 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 np->rxopt.bits.hopopts = valbool;
299 retv = 0;
300 break;
301
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900302 case IPV6_2292HOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900303 if (optlen < sizeof(int))
304 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900305 np->rxopt.bits.ohopopts = valbool;
306 retv = 0;
307 break;
308
309 case IPV6_RECVDSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900310 if (optlen < sizeof(int))
311 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 np->rxopt.bits.dstopts = valbool;
313 retv = 0;
314 break;
315
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900316 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900317 if (optlen < sizeof(int))
318 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900319 np->rxopt.bits.odstopts = valbool;
320 retv = 0;
321 break;
322
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900323 case IPV6_TCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900324 if (optlen < sizeof(int))
325 goto e_inval;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700326 if (val < -1 || val > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900327 goto e_inval;
Gerrit Renker26ced1e2009-08-09 08:12:49 +0000328 /* RFC 3542, 6.5: default traffic class of 0x0 */
329 if (val == -1)
330 val = 0;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900331 np->tclass = val;
332 retv = 0;
333 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900334
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900335 case IPV6_RECVTCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900336 if (optlen < sizeof(int))
337 goto e_inval;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900338 np->rxopt.bits.rxtclass = valbool;
339 retv = 0;
340 break;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 case IPV6_FLOWINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900343 if (optlen < sizeof(int))
344 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 np->rxopt.bits.rxflow = valbool;
346 retv = 0;
347 break;
348
Brian Haley793b14732010-04-23 11:26:07 +0000349 case IPV6_RECVPATHMTU:
350 if (optlen < sizeof(int))
351 goto e_inval;
352 np->rxopt.bits.rxpmtu = valbool;
353 retv = 0;
354 break;
355
Balazs Scheidler6c468622010-10-21 16:08:28 +0200356 case IPV6_TRANSPARENT:
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000357 if (valbool && !ns_capable(net->user_ns, CAP_NET_ADMIN) &&
358 !ns_capable(net->user_ns, CAP_NET_RAW)) {
Balazs Scheidlerb8894162010-10-23 04:48:14 +0000359 retv = -EPERM;
360 break;
361 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200362 if (optlen < sizeof(int))
363 goto e_inval;
364 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
365 inet_sk(sk)->transparent = valbool;
366 retv = 0;
367 break;
368
369 case IPV6_RECVORIGDSTADDR:
370 if (optlen < sizeof(int))
371 goto e_inval;
372 np->rxopt.bits.rxorigdstaddr = valbool;
373 retv = 0;
374 break;
375
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900376 case IPV6_HOPOPTS:
377 case IPV6_RTHDRDSTOPTS:
378 case IPV6_RTHDR:
379 case IPV6_DSTOPTS:
380 {
381 struct ipv6_txoptions *opt;
Shan Weiaea74272008-06-19 16:29:39 -0700382
383 /* remove any sticky options header with a zero option
384 * length, per RFC3542.
385 */
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900386 if (optlen == 0)
Luiz Capitulinocb422c42005-11-14 21:43:36 -0800387 optval = NULL;
Yang Hongyangcfb266c2008-08-03 18:16:15 -0700388 else if (optval == NULL)
389 goto e_inval;
Shan Weiaea74272008-06-19 16:29:39 -0700390 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
391 optlen & 0x7 || optlen > 8 * 255)
392 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900393
394 /* hop-by-hop / destination options are privileged option */
395 retv = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000396 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900397 break;
398
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900399 opt = ipv6_renew_options(sk, np->opt, optname,
400 (struct ipv6_opt_hdr __user *)optval,
401 optlen);
402 if (IS_ERR(opt)) {
403 retv = PTR_ERR(opt);
404 break;
405 }
406
407 /* routing header option needs extra check */
Brian Haley6e093d92008-11-12 22:59:21 -0800408 retv = -EINVAL;
Olaf Kirchdfee0a72007-03-09 13:55:38 -0800409 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900410 struct ipv6_rt_hdr *rthdr = opt->srcrt;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700411 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000412#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700413 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800414 if (rthdr->hdrlen != 2 ||
415 rthdr->segments_left != 1)
416 goto sticky_done;
417
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700418 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700419#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700420 default:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900421 goto sticky_done;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700422 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900423 }
424
425 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700426 opt = ipv6_update_options(sk, opt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900427sticky_done:
428 if (opt)
429 sock_kfree_s(sk, opt, opt->tot_len);
430 break;
431 }
432
Yang Hongyangb24a2512008-12-16 02:06:23 -0800433 case IPV6_PKTINFO:
434 {
435 struct in6_pktinfo pkt;
436
437 if (optlen == 0)
438 goto e_inval;
439 else if (optlen < sizeof(struct in6_pktinfo) || optval == NULL)
440 goto e_inval;
441
Ilpo Järvinen914d1162009-01-04 17:27:31 -0800442 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
Yang Hongyangb24a2512008-12-16 02:06:23 -0800443 retv = -EFAULT;
444 break;
445 }
446 if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if)
447 goto e_inval;
448
449 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000450 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
Yang Hongyangb24a2512008-12-16 02:06:23 -0800451 retv = 0;
452 break;
453 }
454
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900455 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 {
457 struct ipv6_txoptions *opt = NULL;
458 struct msghdr msg;
David S. Miller4c9483b2011-03-12 16:22:43 -0500459 struct flowi6 fl6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 int junk;
461
David S. Miller4c9483b2011-03-12 16:22:43 -0500462 memset(&fl6, 0, sizeof(fl6));
463 fl6.flowi6_oif = sk->sk_bound_dev_if;
464 fl6.flowi6_mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 if (optlen == 0)
467 goto update;
468
469 /* 1K is probably excessive
470 * 1K is surely not enough, 2K per standard header is 16K.
471 */
472 retv = -EINVAL;
473 if (optlen > 64*1024)
474 break;
475
476 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
477 retv = -ENOBUFS;
478 if (opt == NULL)
479 break;
480
481 memset(opt, 0, sizeof(*opt));
482 opt->tot_len = sizeof(*opt) + optlen;
483 retv = -EFAULT;
484 if (copy_from_user(opt+1, optval, optlen))
485 goto done;
486
487 msg.msg_controllen = optlen;
Ian Morris67ba4152014-08-24 21:53:10 +0100488 msg.msg_control = (void *)(opt+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Tom Parkin73df66f2013-01-31 01:02:24 +0000490 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, opt, &junk,
491 &junk, &junk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (retv)
493 goto done;
494update:
495 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700496 opt = ipv6_update_options(sk, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497done:
498 if (opt)
499 sock_kfree_s(sk, opt, opt->tot_len);
500 break;
501 }
502 case IPV6_UNICAST_HOPS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900503 if (optlen < sizeof(int))
504 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (val > 255 || val < -1)
506 goto e_inval;
507 np->hop_limit = val;
508 retv = 0;
509 break;
510
511 case IPV6_MULTICAST_HOPS:
512 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900513 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900514 if (optlen < sizeof(int))
515 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (val > 255 || val < -1)
517 goto e_inval;
Li Wei2a38e6d2011-11-27 21:33:34 +0000518 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 retv = 0;
520 break;
521
522 case IPV6_MULTICAST_LOOP:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900523 if (optlen < sizeof(int))
524 goto e_inval;
YOSHIFUJI Hideaki28d44882008-06-12 03:14:51 +0900525 if (val != valbool)
526 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 np->mc_loop = valbool;
528 retv = 0;
529 break;
530
Erich E. Hooverc4062df2012-02-08 09:11:08 +0000531 case IPV6_UNICAST_IF:
532 {
533 struct net_device *dev = NULL;
534 int ifindex;
535
536 if (optlen != sizeof(int))
537 goto e_inval;
538
539 ifindex = (__force int)ntohl((__force __be32)val);
540 if (ifindex == 0) {
541 np->ucast_oif = 0;
542 retv = 0;
543 break;
544 }
545
546 dev = dev_get_by_index(net, ifindex);
547 retv = -EADDRNOTAVAIL;
548 if (!dev)
549 break;
550 dev_put(dev);
551
552 retv = -EINVAL;
553 if (sk->sk_bound_dev_if)
554 break;
555
556 np->ucast_oif = ifindex;
557 retv = 0;
558 break;
559 }
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 case IPV6_MULTICAST_IF:
562 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900563 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900564 if (optlen < sizeof(int))
565 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Brian Haley4953f0f2007-10-11 14:39:29 -0700567 if (val) {
Eric Dumazet55b80502009-10-19 06:41:58 +0000568 struct net_device *dev;
569
Brian Haley4953f0f2007-10-11 14:39:29 -0700570 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
571 goto e_inval;
572
Eric Dumazet55b80502009-10-19 06:41:58 +0000573 dev = dev_get_by_index(net, val);
574 if (!dev) {
Brian Haley4953f0f2007-10-11 14:39:29 -0700575 retv = -ENODEV;
576 break;
577 }
Eric Dumazet55b80502009-10-19 06:41:58 +0000578 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580 np->mcast_oif = val;
581 retv = 0;
582 break;
583 case IPV6_ADD_MEMBERSHIP:
584 case IPV6_DROP_MEMBERSHIP:
585 {
586 struct ipv6_mreq mreq;
587
Wang Chena28398b2008-04-07 09:42:07 +0800588 if (optlen < sizeof(struct ipv6_mreq))
589 goto e_inval;
590
Flavio Leitnera96fb492007-08-24 22:16:39 -0700591 retv = -EPROTO;
592 if (inet_sk(sk)->is_icsk)
593 break;
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 retv = -EFAULT;
596 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
597 break;
598
599 if (optname == IPV6_ADD_MEMBERSHIP)
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300600 retv = __ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 else
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300602 retv = __ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 break;
604 }
605 case IPV6_JOIN_ANYCAST:
606 case IPV6_LEAVE_ANYCAST:
607 {
608 struct ipv6_mreq mreq;
609
Wang Chena28398b2008-04-07 09:42:07 +0800610 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 goto e_inval;
612
613 retv = -EFAULT;
614 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
615 break;
616
617 if (optname == IPV6_JOIN_ANYCAST)
618 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
619 else
620 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
621 break;
622 }
623 case MCAST_JOIN_GROUP:
624 case MCAST_LEAVE_GROUP:
625 {
626 struct group_req greq;
627 struct sockaddr_in6 *psin6;
628
Wang Chena28398b2008-04-07 09:42:07 +0800629 if (optlen < sizeof(struct group_req))
630 goto e_inval;
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 retv = -EFAULT;
633 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
634 break;
635 if (greq.gr_group.ss_family != AF_INET6) {
636 retv = -EADDRNOTAVAIL;
637 break;
638 }
639 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
640 if (optname == MCAST_JOIN_GROUP)
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300641 retv = __ipv6_sock_mc_join(sk, greq.gr_interface,
642 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 else
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300644 retv = __ipv6_sock_mc_drop(sk, greq.gr_interface,
645 &psin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 break;
647 }
648 case MCAST_JOIN_SOURCE_GROUP:
649 case MCAST_LEAVE_SOURCE_GROUP:
650 case MCAST_BLOCK_SOURCE:
651 case MCAST_UNBLOCK_SOURCE:
652 {
653 struct group_source_req greqs;
654 int omode, add;
655
Wang Chena28398b2008-04-07 09:42:07 +0800656 if (optlen < sizeof(struct group_source_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 goto e_inval;
658 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
659 retv = -EFAULT;
660 break;
661 }
662 if (greqs.gsr_group.ss_family != AF_INET6 ||
663 greqs.gsr_source.ss_family != AF_INET6) {
664 retv = -EADDRNOTAVAIL;
665 break;
666 }
667 if (optname == MCAST_BLOCK_SOURCE) {
668 omode = MCAST_EXCLUDE;
669 add = 1;
670 } else if (optname == MCAST_UNBLOCK_SOURCE) {
671 omode = MCAST_EXCLUDE;
672 add = 0;
673 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
674 struct sockaddr_in6 *psin6;
675
676 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300677 retv = __ipv6_sock_mc_join(sk, greqs.gsr_interface,
678 &psin6->sin6_addr);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700679 /* prior join w/ different source is ok */
680 if (retv && retv != -EADDRINUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 break;
682 omode = MCAST_INCLUDE;
683 add = 1;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700684 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 omode = MCAST_INCLUDE;
686 add = 0;
687 }
688 retv = ip6_mc_source(add, omode, sk, &greqs);
689 break;
690 }
691 case MCAST_MSFILTER:
692 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct group_filter *gsf;
694
695 if (optlen < GROUP_FILTER_SIZE(0))
696 goto e_inval;
697 if (optlen > sysctl_optmem_max) {
698 retv = -ENOBUFS;
699 break;
700 }
Ian Morris67ba4152014-08-24 21:53:10 +0100701 gsf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700702 if (!gsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 retv = -ENOBUFS;
704 break;
705 }
706 retv = -EFAULT;
707 if (copy_from_user(gsf, optval, optlen)) {
708 kfree(gsf);
709 break;
710 }
711 /* numsrc >= (4G-140)/128 overflow in 32 bits */
712 if (gsf->gf_numsrc >= 0x1ffffffU ||
713 gsf->gf_numsrc > sysctl_mld_max_msf) {
714 kfree(gsf);
715 retv = -ENOBUFS;
716 break;
717 }
718 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
719 kfree(gsf);
720 retv = -EINVAL;
721 break;
722 }
723 retv = ip6_mc_msfilter(sk, gsf);
724 kfree(gsf);
725
726 break;
727 }
728 case IPV6_ROUTER_ALERT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900729 if (optlen < sizeof(int))
730 goto e_inval;
Denis V. Lunev725a8ff2008-07-19 00:28:58 -0700731 retv = ip6_ra_control(sk, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 break;
733 case IPV6_MTU_DISCOVER:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900734 if (optlen < sizeof(int))
735 goto e_inval;
Hannes Frederic Sowa0b952272014-02-26 01:20:43 +0100736 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 goto e_inval;
738 np->pmtudisc = val;
739 retv = 0;
740 break;
741 case IPV6_MTU:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900742 if (optlen < sizeof(int))
743 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (val && val < IPV6_MIN_MTU)
745 goto e_inval;
746 np->frag_size = val;
747 retv = 0;
748 break;
749 case IPV6_RECVERR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900750 if (optlen < sizeof(int))
751 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 np->recverr = valbool;
753 if (!val)
754 skb_queue_purge(&sk->sk_error_queue);
755 retv = 0;
756 break;
757 case IPV6_FLOWINFO_SEND:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900758 if (optlen < sizeof(int))
759 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 np->sndflow = valbool;
761 retv = 0;
762 break;
763 case IPV6_FLOWLABEL_MGR:
764 retv = ipv6_flowlabel_opt(sk, optval, optlen);
765 break;
766 case IPV6_IPSEC_POLICY:
767 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700768 retv = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000769 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700770 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 retv = xfrm_user_policy(sk, optname, optval, optlen);
772 break;
773
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900774 case IPV6_ADDR_PREFERENCES:
775 {
776 unsigned int pref = 0;
777 unsigned int prefmask = ~0;
778
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900779 if (optlen < sizeof(int))
780 goto e_inval;
781
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900782 retv = -EINVAL;
783
784 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
785 switch (val & (IPV6_PREFER_SRC_PUBLIC|
786 IPV6_PREFER_SRC_TMP|
787 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
788 case IPV6_PREFER_SRC_PUBLIC:
789 pref |= IPV6_PREFER_SRC_PUBLIC;
790 break;
791 case IPV6_PREFER_SRC_TMP:
792 pref |= IPV6_PREFER_SRC_TMP;
793 break;
794 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
795 break;
796 case 0:
797 goto pref_skip_pubtmp;
798 default:
799 goto e_inval;
800 }
801
802 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
803 IPV6_PREFER_SRC_TMP);
804pref_skip_pubtmp:
805
806 /* check HOME/COA conflicts */
807 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
808 case IPV6_PREFER_SRC_HOME:
809 break;
810 case IPV6_PREFER_SRC_COA:
811 pref |= IPV6_PREFER_SRC_COA;
812 case 0:
813 goto pref_skip_coa;
814 default:
815 goto e_inval;
816 }
817
818 prefmask &= ~IPV6_PREFER_SRC_COA;
819pref_skip_coa:
820
821 /* check CGA/NONCGA conflicts */
822 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
823 case IPV6_PREFER_SRC_CGA:
824 case IPV6_PREFER_SRC_NONCGA:
825 case 0:
826 break;
827 default:
828 goto e_inval;
829 }
830
831 np->srcprefs = (np->srcprefs & prefmask) | pref;
832 retv = 0;
833
834 break;
835 }
Stephen Hemmingere802af92010-04-22 15:24:53 -0700836 case IPV6_MINHOPCOUNT:
837 if (optlen < sizeof(int))
838 goto e_inval;
839 if (val < 0 || val > 255)
840 goto e_inval;
841 np->min_hopcount = val;
Hannes Frederic Sowad4596ba2012-11-10 19:52:34 +0000842 retv = 0;
Brian Haley793b14732010-04-23 11:26:07 +0000843 break;
844 case IPV6_DONTFRAG:
845 np->dontfrag = valbool;
Stephen Hemmingere802af92010-04-22 15:24:53 -0700846 retv = 0;
847 break;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700848 case IPV6_AUTOFLOWLABEL:
849 np->autoflowlabel = valbool;
850 retv = 0;
851 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 release_sock(sk);
Marcelo Ricardo Leitnerbaf606d2015-03-18 14:50:42 -0300855 if (needs_rtnl)
856 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return retv;
859
860e_inval:
861 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 return -EINVAL;
865}
866
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800867int ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700868 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800869{
870 int err;
871
872 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
873 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
874
875 if (level != SOL_IPV6)
876 return -ENOPROTOOPT;
877
878 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
879#ifdef CONFIG_NETFILTER
880 /* we need to exclude all possible ENOPROTOOPTs except default case */
881 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
882 optname != IPV6_XFRM_POLICY) {
883 lock_sock(sk);
884 err = nf_setsockopt(sk, PF_INET6, optname, optval,
885 optlen);
886 release_sock(sk);
887 }
888#endif
889 return err;
890}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900891EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800892
893#ifdef CONFIG_COMPAT
894int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700895 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800896{
897 int err;
898
899 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800900 if (udp_prot.compat_setsockopt != NULL)
901 return udp_prot.compat_setsockopt(sk, level, optname,
902 optval, optlen);
903 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800904 }
905
906 if (level != SOL_IPV6)
907 return -ENOPROTOOPT;
908
David L Stevensdae50292008-04-27 01:06:07 -0700909 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
910 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
911 ipv6_setsockopt);
912
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800913 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
914#ifdef CONFIG_NETFILTER
915 /* we need to exclude all possible ENOPROTOOPTs except default case */
916 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800917 optname != IPV6_XFRM_POLICY) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800918 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800919 err = compat_nf_setsockopt(sk, PF_INET6, optname,
920 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800921 release_sock(sk);
922 }
923#endif
924 return err;
925}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800926EXPORT_SYMBOL(compat_ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800927#endif
928
David S. Miller286930792007-03-07 12:50:46 -0800929static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700930 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900931{
David S. Miller286930792007-03-07 12:50:46 -0800932 struct ipv6_opt_hdr *hdr;
933
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700934 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900935 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700936
Ian Morris67ba4152014-08-24 21:53:10 +0100937 switch (optname) {
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700938 case IPV6_HOPOPTS:
939 hdr = opt->hopopt;
940 break;
941 case IPV6_RTHDRDSTOPTS:
942 hdr = opt->dst0opt;
943 break;
944 case IPV6_RTHDR:
945 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
946 break;
947 case IPV6_DSTOPTS:
948 hdr = opt->dst1opt;
949 break;
950 default:
951 return -EINVAL; /* should not happen */
952 }
953
954 if (!hdr)
955 return 0;
David S. Miller286930792007-03-07 12:50:46 -0800956
Chris Wrightd2b02ed2007-03-09 16:19:17 -0800957 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -0700958 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900959 return -EFAULT;
Yang Hongyang95b496b2008-05-28 16:27:28 +0800960 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900961}
962
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800963static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +0000964 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 struct ipv6_pinfo *np = inet6_sk(sk);
967 int len;
968 int val;
969
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900970 if (ip6_mroute_opt(optname))
971 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (get_user(len, optlen))
974 return -EFAULT;
975 switch (optname) {
976 case IPV6_ADDRFORM:
977 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800978 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 sk->sk_protocol != IPPROTO_TCP)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900980 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (sk->sk_state != TCP_ESTABLISHED)
982 return -ENOTCONN;
983 val = sk->sk_family;
984 break;
985 case MCAST_MSFILTER:
986 {
987 struct group_filter gsf;
988 int err;
989
990 if (len < GROUP_FILTER_SIZE(0))
991 return -EINVAL;
992 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
993 return -EFAULT;
YOSHIFUJI Hideaki20c61fbd2008-04-28 14:40:55 +0900994 if (gsf.gf_group.ss_family != AF_INET6)
995 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 lock_sock(sk);
997 err = ip6_mc_msfget(sk, &gsf,
998 (struct group_filter __user *)optval, optlen);
999 release_sock(sk);
1000 return err;
1001 }
1002
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001003 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 {
1005 struct msghdr msg;
1006 struct sk_buff *skb;
1007
1008 if (sk->sk_type != SOCK_STREAM)
1009 return -ENOPROTOOPT;
1010
1011 msg.msg_control = optval;
1012 msg.msg_controllen = len;
Daniel Baluta98e77432011-08-19 03:19:07 -07001013 msg.msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 lock_sock(sk);
1016 skb = np->pktoptions;
1017 if (skb)
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001018 ip6_datagram_recv_ctl(sk, &msg, skb);
Eric Dumazet1dc7b902015-01-21 03:45:42 -08001019 release_sock(sk);
1020 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (np->rxopt.bits.rxinfo) {
1022 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001023 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1024 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001025 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1027 }
1028 if (np->rxopt.bits.rxhlim) {
1029 int hlim = np->mcast_hops;
1030 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1031 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001032 if (np->rxopt.bits.rxtclass) {
Li RongQingd76ed222014-01-15 17:03:30 +08001033 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1034
Jiri Benc4c507d22012-02-09 09:35:49 +00001035 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1036 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001037 if (np->rxopt.bits.rxoinfo) {
1038 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001039 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1040 np->sticky_pktinfo.ipi6_ifindex;
Eric Dumazetefe42082013-10-03 15:42:29 -07001041 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1042 np->sticky_pktinfo.ipi6_addr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001043 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1044 }
1045 if (np->rxopt.bits.rxohlim) {
1046 int hlim = np->mcast_hops;
1047 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1048 }
Florent Fourcot1397ed32013-12-08 15:46:57 +01001049 if (np->rxopt.bits.rxflow) {
Florent Fourcot68536052013-12-12 17:07:58 +01001050 __be32 flowinfo = np->rcv_flowinfo;
1051
Florent Fourcot1397ed32013-12-08 15:46:57 +01001052 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055 len -= msg.msg_controllen;
1056 return put_user(len, optlen);
1057 }
1058 case IPV6_MTU:
1059 {
1060 struct dst_entry *dst;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001061
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001062 val = 0;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001063 rcu_read_lock();
1064 dst = __sk_dst_get(sk);
1065 if (dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 val = dst_mtu(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001067 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (!val)
1069 return -ENOTCONN;
1070 break;
1071 }
1072
1073 case IPV6_V6ONLY:
Eric Dumazet9fe516b2014-06-27 08:36:16 -07001074 val = sk->sk_ipv6only;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 break;
1076
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001077 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 val = np->rxopt.bits.rxinfo;
1079 break;
1080
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001081 case IPV6_2292PKTINFO:
1082 val = np->rxopt.bits.rxoinfo;
1083 break;
1084
1085 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 val = np->rxopt.bits.rxhlim;
1087 break;
1088
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001089 case IPV6_2292HOPLIMIT:
1090 val = np->rxopt.bits.rxohlim;
1091 break;
1092
1093 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 val = np->rxopt.bits.srcrt;
1095 break;
1096
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001097 case IPV6_2292RTHDR:
1098 val = np->rxopt.bits.osrcrt;
1099 break;
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001102 case IPV6_RTHDRDSTOPTS:
1103 case IPV6_RTHDR:
1104 case IPV6_DSTOPTS:
1105 {
1106
1107 lock_sock(sk);
David S. Miller286930792007-03-07 12:50:46 -08001108 len = ipv6_getsockopt_sticky(sk, np->opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001109 optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001110 release_sock(sk);
Yang Hongyang05335c22008-05-28 16:23:47 +08001111 /* check if ipv6_getsockopt_sticky() returns err code */
1112 if (len < 0)
1113 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001114 return put_user(len, optlen);
1115 }
1116
1117 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 val = np->rxopt.bits.hopopts;
1119 break;
1120
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001121 case IPV6_2292HOPOPTS:
1122 val = np->rxopt.bits.ohopopts;
1123 break;
1124
1125 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 val = np->rxopt.bits.dstopts;
1127 break;
1128
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001129 case IPV6_2292DSTOPTS:
1130 val = np->rxopt.bits.odstopts;
1131 break;
1132
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001133 case IPV6_TCLASS:
1134 val = np->tclass;
1135 break;
1136
1137 case IPV6_RECVTCLASS:
1138 val = np->rxopt.bits.rxtclass;
1139 break;
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 case IPV6_FLOWINFO:
1142 val = np->rxopt.bits.rxflow;
1143 break;
1144
Brian Haley793b14732010-04-23 11:26:07 +00001145 case IPV6_RECVPATHMTU:
1146 val = np->rxopt.bits.rxpmtu;
1147 break;
1148
1149 case IPV6_PATHMTU:
1150 {
1151 struct dst_entry *dst;
1152 struct ip6_mtuinfo mtuinfo;
1153
1154 if (len < sizeof(mtuinfo))
1155 return -EINVAL;
1156
1157 len = sizeof(mtuinfo);
1158 memset(&mtuinfo, 0, sizeof(mtuinfo));
1159
1160 rcu_read_lock();
1161 dst = __sk_dst_get(sk);
1162 if (dst)
1163 mtuinfo.ip6m_mtu = dst_mtu(dst);
1164 rcu_read_unlock();
1165 if (!mtuinfo.ip6m_mtu)
1166 return -ENOTCONN;
1167
1168 if (put_user(len, optlen))
1169 return -EFAULT;
1170 if (copy_to_user(optval, &mtuinfo, len))
1171 return -EFAULT;
1172
1173 return 0;
Brian Haley793b14732010-04-23 11:26:07 +00001174 }
1175
Balazs Scheidler6c468622010-10-21 16:08:28 +02001176 case IPV6_TRANSPARENT:
1177 val = inet_sk(sk)->transparent;
1178 break;
1179
1180 case IPV6_RECVORIGDSTADDR:
1181 val = np->rxopt.bits.rxorigdstaddr;
1182 break;
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -08001186 {
1187 struct dst_entry *dst;
1188
1189 if (optname == IPV6_UNICAST_HOPS)
1190 val = np->hop_limit;
1191 else
1192 val = np->mcast_hops;
1193
Eric Dumazetb6c67122010-04-08 23:03:29 +00001194 if (val < 0) {
1195 rcu_read_lock();
1196 dst = __sk_dst_get(sk);
1197 if (dst)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001198 val = ip6_dst_hoplimit(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001199 rcu_read_unlock();
Brian Haleybefffe92006-12-12 17:09:49 -08001200 }
Eric Dumazetb6c67122010-04-08 23:03:29 +00001201
Brian Haleybefffe92006-12-12 17:09:49 -08001202 if (val < 0)
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001203 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 break;
Brian Haleybefffe92006-12-12 17:09:49 -08001205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 case IPV6_MULTICAST_LOOP:
1208 val = np->mc_loop;
1209 break;
1210
1211 case IPV6_MULTICAST_IF:
1212 val = np->mcast_oif;
1213 break;
1214
Erich E. Hooverc4062df2012-02-08 09:11:08 +00001215 case IPV6_UNICAST_IF:
1216 val = (__force int)htonl((__u32) np->ucast_oif);
1217 break;
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 case IPV6_MTU_DISCOVER:
1220 val = np->pmtudisc;
1221 break;
1222
1223 case IPV6_RECVERR:
1224 val = np->recverr;
1225 break;
1226
1227 case IPV6_FLOWINFO_SEND:
1228 val = np->sndflow;
1229 break;
1230
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001231 case IPV6_FLOWLABEL_MGR:
1232 {
1233 struct in6_flowlabel_req freq;
Florent Fourcot46e5f402014-01-17 17:15:04 +01001234 int flags;
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001235
1236 if (len < sizeof(freq))
1237 return -EINVAL;
1238
1239 if (copy_from_user(&freq, optval, sizeof(freq)))
1240 return -EFAULT;
1241
1242 if (freq.flr_action != IPV6_FL_A_GET)
1243 return -EINVAL;
1244
1245 len = sizeof(freq);
Florent Fourcot46e5f402014-01-17 17:15:04 +01001246 flags = freq.flr_flags;
1247
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001248 memset(&freq, 0, sizeof(freq));
1249
Florent Fourcot46e5f402014-01-17 17:15:04 +01001250 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +01001251 if (val < 0)
1252 return val;
1253
1254 if (put_user(len, optlen))
1255 return -EFAULT;
1256 if (copy_to_user(optval, &freq, len))
1257 return -EFAULT;
1258
1259 return 0;
1260 }
1261
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001262 case IPV6_ADDR_PREFERENCES:
1263 val = 0;
1264
1265 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1266 val |= IPV6_PREFER_SRC_TMP;
1267 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1268 val |= IPV6_PREFER_SRC_PUBLIC;
1269 else {
1270 /* XXX: should we return system default? */
1271 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1272 }
1273
1274 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1275 val |= IPV6_PREFER_SRC_COA;
1276 else
1277 val |= IPV6_PREFER_SRC_HOME;
1278 break;
1279
Stephen Hemmingere802af92010-04-22 15:24:53 -07001280 case IPV6_MINHOPCOUNT:
1281 val = np->min_hopcount;
1282 break;
1283
Brian Haley793b14732010-04-23 11:26:07 +00001284 case IPV6_DONTFRAG:
1285 val = np->dontfrag;
1286 break;
1287
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001288 case IPV6_AUTOFLOWLABEL:
1289 val = np->autoflowlabel;
1290 break;
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001293 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
1295 len = min_t(unsigned int, sizeof(int), len);
Ian Morris67ba4152014-08-24 21:53:10 +01001296 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return -EFAULT;
Ian Morris67ba4152014-08-24 21:53:10 +01001298 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return -EFAULT;
1300 return 0;
1301}
1302
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001303int ipv6_getsockopt(struct sock *sk, int level, int optname,
1304 char __user *optval, int __user *optlen)
1305{
1306 int err;
1307
1308 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1309 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1310
Ian Morris67ba4152014-08-24 21:53:10 +01001311 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001312 return -ENOPROTOOPT;
1313
Daniel Baluta98e77432011-08-19 03:19:07 -07001314 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001315#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001316 /* we need to exclude all possible ENOPROTOOPTs except default case */
1317 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001318 int len;
1319
1320 if (get_user(len, optlen))
1321 return -EFAULT;
1322
1323 lock_sock(sk);
1324 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1325 &len);
1326 release_sock(sk);
1327 if (err >= 0)
1328 err = put_user(len, optlen);
1329 }
1330#endif
1331 return err;
1332}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001333EXPORT_SYMBOL(ipv6_getsockopt);
1334
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001335#ifdef CONFIG_COMPAT
1336int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001337 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001338{
1339 int err;
1340
1341 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001342 if (udp_prot.compat_getsockopt != NULL)
1343 return udp_prot.compat_getsockopt(sk, level, optname,
1344 optval, optlen);
1345 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001346 }
1347
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001348 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001349 return -ENOPROTOOPT;
1350
Pavel Emelyanov80991792008-04-30 14:49:54 -07001351 if (optname == MCAST_MSFILTER)
1352 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1353 ipv6_getsockopt);
1354
Daniel Baluta98e77432011-08-19 03:19:07 -07001355 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1356 MSG_CMSG_COMPAT);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001357#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001358 /* we need to exclude all possible ENOPROTOOPTs except default case */
1359 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001360 int len;
1361
1362 if (get_user(len, optlen))
1363 return -EFAULT;
1364
1365 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001366 err = compat_nf_getsockopt(sk, PF_INET6,
1367 optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001368 release_sock(sk);
1369 if (err >= 0)
1370 err = put_user(len, optlen);
1371 }
1372#endif
1373 return err;
1374}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001375EXPORT_SYMBOL(compat_ipv6_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001376#endif
1377