blob: bd3fb129b3932abc2a51ceb5157a6ba22c632f3b [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 *
10 * $Id: ipv6_sockglue.c,v 1.41 2002/02/01 22:01:04 davem Exp $
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 * FIXME: Make the setsockopt code POSIX compliant: That is
18 *
19 * o Return -EINVAL for setsockopt of short lengths
20 * o Truncate getsockopt returns
21 * o Return an optlen of the truncated length if need be
22 *
23 * Changes:
24 * David L Stevens <dlstevens@us.ibm.com>:
25 * - added multicast source filtering API for MLDv2
26 */
27
28#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080029#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/errno.h>
31#include <linux/types.h>
32#include <linux/socket.h>
33#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/net.h>
35#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090036#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/netdevice.h>
38#include <linux/if_arp.h>
39#include <linux/init.h>
40#include <linux/sysctl.h>
41#include <linux/netfilter.h>
42
43#include <net/sock.h>
44#include <net/snmp.h>
45#include <net/ipv6.h>
46#include <net/ndisc.h>
47#include <net/protocol.h>
48#include <net/transp_v6.h>
49#include <net/ip6_route.h>
50#include <net/addrconf.h>
51#include <net/inet_common.h>
52#include <net/tcp.h>
53#include <net/udp.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080054#include <net/udplite.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/xfrm.h>
56
57#include <asm/uaccess.h>
58
Eric Dumazetba899662005-08-26 12:05:31 -070059DEFINE_SNMP_STAT(struct ipstats_mib, ipv6_statistics) __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061struct ip6_ra_chain *ip6_ra_chain;
62DEFINE_RWLOCK(ip6_ra_lock);
63
64int ip6_ra_control(struct sock *sk, int sel, void (*destructor)(struct sock *))
65{
66 struct ip6_ra_chain *ra, *new_ra, **rap;
67
68 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
69 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num != IPPROTO_RAW)
70 return -EINVAL;
71
72 new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
73
74 write_lock_bh(&ip6_ra_lock);
75 for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
76 if (ra->sk == sk) {
77 if (sel>=0) {
78 write_unlock_bh(&ip6_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -080079 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -EADDRINUSE;
81 }
82
83 *rap = ra->next;
84 write_unlock_bh(&ip6_ra_lock);
85
86 if (ra->destructor)
87 ra->destructor(sk);
88 sock_put(sk);
89 kfree(ra);
90 return 0;
91 }
92 }
93 if (new_ra == NULL) {
94 write_unlock_bh(&ip6_ra_lock);
95 return -ENOBUFS;
96 }
97 new_ra->sk = sk;
98 new_ra->sel = sel;
99 new_ra->destructor = destructor;
100 new_ra->next = ra;
101 *rap = new_ra;
102 sock_hold(sk);
103 write_unlock_bh(&ip6_ra_lock);
104 return 0;
105}
106
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800107static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 char __user *optval, int optlen)
109{
110 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900111 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int val, valbool;
113 int retv = -ENOPROTOOPT;
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (optval == NULL)
116 val=0;
117 else if (get_user(val, (int __user *) optval))
118 return -EFAULT;
119
120 valbool = (val!=0);
121
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900122 if (ip6_mroute_opt(optname))
123 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 lock_sock(sk);
126
127 switch (optname) {
128
129 case IPV6_ADDRFORM:
130 if (val == PF_INET) {
131 struct ipv6_txoptions *opt;
132 struct sk_buff *pktopt;
133
134 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800135 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 sk->sk_protocol != IPPROTO_TCP)
137 break;
138
139 if (sk->sk_state != TCP_ESTABLISHED) {
140 retv = -ENOTCONN;
141 break;
142 }
143
144 if (ipv6_only_sock(sk) ||
Brian Haleye773e4f2007-08-24 23:16:08 -0700145 !ipv6_addr_v4mapped(&np->daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 retv = -EADDRNOTAVAIL;
147 break;
148 }
149
150 fl6_free_socklist(sk);
151 ipv6_sock_mc_close(sk);
152
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700153 /*
154 * Sock is moving from IPv6 to IPv4 (sk_prot), so
155 * remove it from the refcnt debug socks count in the
156 * original family...
157 */
158 sk_refcnt_debug_dec(sk);
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (sk->sk_protocol == IPPROTO_TCP) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800161 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700163 sock_prot_inuse_add(net, sk->sk_prot, -1);
164 sock_prot_inuse_add(net, &tcp_prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 local_bh_enable();
166 sk->sk_prot = &tcp_prot;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800167 icsk->icsk_af_ops = &ipv4_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 sk->sk_socket->ops = &inet_stream_ops;
169 sk->sk_family = PF_INET;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800170 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 } else {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800172 struct proto *prot = &udp_prot;
173
David S. Millerdb8dac22008-03-06 16:22:02 -0800174 if (sk->sk_protocol == IPPROTO_UDPLITE)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800175 prot = &udplite_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700177 sock_prot_inuse_add(net, sk->sk_prot, -1);
178 sock_prot_inuse_add(net, prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 local_bh_enable();
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800180 sk->sk_prot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 sk->sk_socket->ops = &inet_dgram_ops;
182 sk->sk_family = PF_INET;
183 }
184 opt = xchg(&np->opt, NULL);
185 if (opt)
186 sock_kfree_s(sk, opt, opt->tot_len);
187 pktopt = xchg(&np->pktoptions, NULL);
188 if (pktopt)
189 kfree_skb(pktopt);
190
191 sk->sk_destruct = inet_sock_destruct;
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700192 /*
193 * ... and add it to the refcnt debug socks count
194 * in the new family. -acme
195 */
196 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 module_put(THIS_MODULE);
198 retv = 0;
199 break;
200 }
201 goto e_inval;
202
203 case IPV6_V6ONLY:
204 if (inet_sk(sk)->num)
205 goto e_inval;
206 np->ipv6only = valbool;
207 retv = 0;
208 break;
209
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900210 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 np->rxopt.bits.rxinfo = valbool;
212 retv = 0;
213 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900214
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900215 case IPV6_2292PKTINFO:
216 np->rxopt.bits.rxoinfo = valbool;
217 retv = 0;
218 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900220 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 np->rxopt.bits.rxhlim = valbool;
222 retv = 0;
223 break;
224
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900225 case IPV6_2292HOPLIMIT:
226 np->rxopt.bits.rxohlim = valbool;
227 retv = 0;
228 break;
229
230 case IPV6_RECVRTHDR:
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900231 np->rxopt.bits.srcrt = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 retv = 0;
233 break;
234
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900235 case IPV6_2292RTHDR:
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900236 np->rxopt.bits.osrcrt = valbool;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900237 retv = 0;
238 break;
239
240 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 np->rxopt.bits.hopopts = valbool;
242 retv = 0;
243 break;
244
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900245 case IPV6_2292HOPOPTS:
246 np->rxopt.bits.ohopopts = valbool;
247 retv = 0;
248 break;
249
250 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 np->rxopt.bits.dstopts = valbool;
252 retv = 0;
253 break;
254
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900255 case IPV6_2292DSTOPTS:
256 np->rxopt.bits.odstopts = valbool;
257 retv = 0;
258 break;
259
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900260 case IPV6_TCLASS:
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700261 if (val < -1 || val > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900262 goto e_inval;
263 np->tclass = val;
264 retv = 0;
265 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900266
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900267 case IPV6_RECVTCLASS:
268 np->rxopt.bits.rxtclass = valbool;
269 retv = 0;
270 break;
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 case IPV6_FLOWINFO:
273 np->rxopt.bits.rxflow = valbool;
274 retv = 0;
275 break;
276
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900277 case IPV6_HOPOPTS:
278 case IPV6_RTHDRDSTOPTS:
279 case IPV6_RTHDR:
280 case IPV6_DSTOPTS:
281 {
282 struct ipv6_txoptions *opt;
283 if (optlen == 0)
Luiz Capitulinocb422c42005-11-14 21:43:36 -0800284 optval = NULL;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900285
286 /* hop-by-hop / destination options are privileged option */
287 retv = -EPERM;
288 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
289 break;
290
291 retv = -EINVAL;
292 if (optlen & 0x7 || optlen > 8 * 255)
293 break;
294
295 opt = ipv6_renew_options(sk, np->opt, optname,
296 (struct ipv6_opt_hdr __user *)optval,
297 optlen);
298 if (IS_ERR(opt)) {
299 retv = PTR_ERR(opt);
300 break;
301 }
302
303 /* routing header option needs extra check */
Olaf Kirchdfee0a72007-03-09 13:55:38 -0800304 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900305 struct ipv6_rt_hdr *rthdr = opt->srcrt;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700306 switch (rthdr->type) {
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700307#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700308 case IPV6_SRCRT_TYPE_2:
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700309 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700310#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700311 default:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900312 goto sticky_done;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700313 }
314
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900315 if ((rthdr->hdrlen & 1) ||
316 (rthdr->hdrlen >> 1) != rthdr->segments_left)
317 goto sticky_done;
318 }
319
320 retv = 0;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800321 if (inet_sk(sk)->is_icsk) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900322 if (opt) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800323 struct inet_connection_sock *icsk = inet_csk(sk);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900324 if (!((1 << sk->sk_state) &
325 (TCPF_LISTEN | TCPF_CLOSE))
326 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800327 icsk->icsk_ext_hdr_len =
328 opt->opt_flen + opt->opt_nflen;
329 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900330 }
331 }
332 opt = xchg(&np->opt, opt);
333 sk_dst_reset(sk);
334 } else {
335 write_lock(&sk->sk_dst_lock);
336 opt = xchg(&np->opt, opt);
337 write_unlock(&sk->sk_dst_lock);
338 sk_dst_reset(sk);
339 }
340sticky_done:
341 if (opt)
342 sock_kfree_s(sk, opt, opt->tot_len);
343 break;
344 }
345
346 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 {
348 struct ipv6_txoptions *opt = NULL;
349 struct msghdr msg;
350 struct flowi fl;
351 int junk;
352
353 fl.fl6_flowlabel = 0;
354 fl.oif = sk->sk_bound_dev_if;
355
356 if (optlen == 0)
357 goto update;
358
359 /* 1K is probably excessive
360 * 1K is surely not enough, 2K per standard header is 16K.
361 */
362 retv = -EINVAL;
363 if (optlen > 64*1024)
364 break;
365
366 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
367 retv = -ENOBUFS;
368 if (opt == NULL)
369 break;
370
371 memset(opt, 0, sizeof(*opt));
372 opt->tot_len = sizeof(*opt) + optlen;
373 retv = -EFAULT;
374 if (copy_from_user(opt+1, optval, optlen))
375 goto done;
376
377 msg.msg_controllen = optlen;
378 msg.msg_control = (void*)(opt+1);
379
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900380 retv = datagram_send_ctl(&msg, &fl, opt, &junk, &junk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (retv)
382 goto done;
383update:
384 retv = 0;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800385 if (inet_sk(sk)->is_icsk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (opt) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800387 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (!((1 << sk->sk_state) &
389 (TCPF_LISTEN | TCPF_CLOSE))
390 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800391 icsk->icsk_ext_hdr_len =
392 opt->opt_flen + opt->opt_nflen;
393 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395 }
396 opt = xchg(&np->opt, opt);
397 sk_dst_reset(sk);
398 } else {
399 write_lock(&sk->sk_dst_lock);
400 opt = xchg(&np->opt, opt);
401 write_unlock(&sk->sk_dst_lock);
402 sk_dst_reset(sk);
403 }
404
405done:
406 if (opt)
407 sock_kfree_s(sk, opt, opt->tot_len);
408 break;
409 }
410 case IPV6_UNICAST_HOPS:
411 if (val > 255 || val < -1)
412 goto e_inval;
413 np->hop_limit = val;
414 retv = 0;
415 break;
416
417 case IPV6_MULTICAST_HOPS:
418 if (sk->sk_type == SOCK_STREAM)
419 goto e_inval;
420 if (val > 255 || val < -1)
421 goto e_inval;
422 np->mcast_hops = val;
423 retv = 0;
424 break;
425
426 case IPV6_MULTICAST_LOOP:
427 np->mc_loop = valbool;
428 retv = 0;
429 break;
430
431 case IPV6_MULTICAST_IF:
432 if (sk->sk_type == SOCK_STREAM)
433 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Brian Haley4953f0f2007-10-11 14:39:29 -0700435 if (val) {
436 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
437 goto e_inval;
438
Daniel Lezcano8a3edd82008-03-07 11:14:16 -0800439 if (__dev_get_by_index(net, val) == NULL) {
Brian Haley4953f0f2007-10-11 14:39:29 -0700440 retv = -ENODEV;
441 break;
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444 np->mcast_oif = val;
445 retv = 0;
446 break;
447 case IPV6_ADD_MEMBERSHIP:
448 case IPV6_DROP_MEMBERSHIP:
449 {
450 struct ipv6_mreq mreq;
451
Wang Chena28398b2008-04-07 09:42:07 +0800452 if (optlen < sizeof(struct ipv6_mreq))
453 goto e_inval;
454
Flavio Leitnera96fb492007-08-24 22:16:39 -0700455 retv = -EPROTO;
456 if (inet_sk(sk)->is_icsk)
457 break;
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 retv = -EFAULT;
460 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
461 break;
462
463 if (optname == IPV6_ADD_MEMBERSHIP)
464 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
465 else
466 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
467 break;
468 }
469 case IPV6_JOIN_ANYCAST:
470 case IPV6_LEAVE_ANYCAST:
471 {
472 struct ipv6_mreq mreq;
473
Wang Chena28398b2008-04-07 09:42:07 +0800474 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 goto e_inval;
476
477 retv = -EFAULT;
478 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
479 break;
480
481 if (optname == IPV6_JOIN_ANYCAST)
482 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
483 else
484 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
485 break;
486 }
487 case MCAST_JOIN_GROUP:
488 case MCAST_LEAVE_GROUP:
489 {
490 struct group_req greq;
491 struct sockaddr_in6 *psin6;
492
Wang Chena28398b2008-04-07 09:42:07 +0800493 if (optlen < sizeof(struct group_req))
494 goto e_inval;
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 retv = -EFAULT;
497 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
498 break;
499 if (greq.gr_group.ss_family != AF_INET6) {
500 retv = -EADDRNOTAVAIL;
501 break;
502 }
503 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
504 if (optname == MCAST_JOIN_GROUP)
505 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
506 &psin6->sin6_addr);
507 else
508 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
509 &psin6->sin6_addr);
510 break;
511 }
512 case MCAST_JOIN_SOURCE_GROUP:
513 case MCAST_LEAVE_SOURCE_GROUP:
514 case MCAST_BLOCK_SOURCE:
515 case MCAST_UNBLOCK_SOURCE:
516 {
517 struct group_source_req greqs;
518 int omode, add;
519
Wang Chena28398b2008-04-07 09:42:07 +0800520 if (optlen < sizeof(struct group_source_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 goto e_inval;
522 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
523 retv = -EFAULT;
524 break;
525 }
526 if (greqs.gsr_group.ss_family != AF_INET6 ||
527 greqs.gsr_source.ss_family != AF_INET6) {
528 retv = -EADDRNOTAVAIL;
529 break;
530 }
531 if (optname == MCAST_BLOCK_SOURCE) {
532 omode = MCAST_EXCLUDE;
533 add = 1;
534 } else if (optname == MCAST_UNBLOCK_SOURCE) {
535 omode = MCAST_EXCLUDE;
536 add = 0;
537 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
538 struct sockaddr_in6 *psin6;
539
540 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
541 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
542 &psin6->sin6_addr);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700543 /* prior join w/ different source is ok */
544 if (retv && retv != -EADDRINUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 break;
546 omode = MCAST_INCLUDE;
547 add = 1;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700548 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 omode = MCAST_INCLUDE;
550 add = 0;
551 }
552 retv = ip6_mc_source(add, omode, sk, &greqs);
553 break;
554 }
555 case MCAST_MSFILTER:
556 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 extern int sysctl_mld_max_msf;
558 struct group_filter *gsf;
559
560 if (optlen < GROUP_FILTER_SIZE(0))
561 goto e_inval;
562 if (optlen > sysctl_optmem_max) {
563 retv = -ENOBUFS;
564 break;
565 }
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800566 gsf = kmalloc(optlen,GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700567 if (!gsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 retv = -ENOBUFS;
569 break;
570 }
571 retv = -EFAULT;
572 if (copy_from_user(gsf, optval, optlen)) {
573 kfree(gsf);
574 break;
575 }
576 /* numsrc >= (4G-140)/128 overflow in 32 bits */
577 if (gsf->gf_numsrc >= 0x1ffffffU ||
578 gsf->gf_numsrc > sysctl_mld_max_msf) {
579 kfree(gsf);
580 retv = -ENOBUFS;
581 break;
582 }
583 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
584 kfree(gsf);
585 retv = -EINVAL;
586 break;
587 }
588 retv = ip6_mc_msfilter(sk, gsf);
589 kfree(gsf);
590
591 break;
592 }
593 case IPV6_ROUTER_ALERT:
594 retv = ip6_ra_control(sk, val, NULL);
595 break;
596 case IPV6_MTU_DISCOVER:
John Heffner628a5c52007-04-20 15:53:27 -0700597 if (val<0 || val>3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 goto e_inval;
599 np->pmtudisc = val;
600 retv = 0;
601 break;
602 case IPV6_MTU:
603 if (val && val < IPV6_MIN_MTU)
604 goto e_inval;
605 np->frag_size = val;
606 retv = 0;
607 break;
608 case IPV6_RECVERR:
609 np->recverr = valbool;
610 if (!val)
611 skb_queue_purge(&sk->sk_error_queue);
612 retv = 0;
613 break;
614 case IPV6_FLOWINFO_SEND:
615 np->sndflow = valbool;
616 retv = 0;
617 break;
618 case IPV6_FLOWLABEL_MGR:
619 retv = ipv6_flowlabel_opt(sk, optval, optlen);
620 break;
621 case IPV6_IPSEC_POLICY:
622 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700623 retv = -EPERM;
624 if (!capable(CAP_NET_ADMIN))
625 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 retv = xfrm_user_policy(sk, optname, optval, optlen);
627 break;
628
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900629 case IPV6_ADDR_PREFERENCES:
630 {
631 unsigned int pref = 0;
632 unsigned int prefmask = ~0;
633
634 retv = -EINVAL;
635
636 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
637 switch (val & (IPV6_PREFER_SRC_PUBLIC|
638 IPV6_PREFER_SRC_TMP|
639 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
640 case IPV6_PREFER_SRC_PUBLIC:
641 pref |= IPV6_PREFER_SRC_PUBLIC;
642 break;
643 case IPV6_PREFER_SRC_TMP:
644 pref |= IPV6_PREFER_SRC_TMP;
645 break;
646 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
647 break;
648 case 0:
649 goto pref_skip_pubtmp;
650 default:
651 goto e_inval;
652 }
653
654 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
655 IPV6_PREFER_SRC_TMP);
656pref_skip_pubtmp:
657
658 /* check HOME/COA conflicts */
659 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
660 case IPV6_PREFER_SRC_HOME:
661 break;
662 case IPV6_PREFER_SRC_COA:
663 pref |= IPV6_PREFER_SRC_COA;
664 case 0:
665 goto pref_skip_coa;
666 default:
667 goto e_inval;
668 }
669
670 prefmask &= ~IPV6_PREFER_SRC_COA;
671pref_skip_coa:
672
673 /* check CGA/NONCGA conflicts */
674 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
675 case IPV6_PREFER_SRC_CGA:
676 case IPV6_PREFER_SRC_NONCGA:
677 case 0:
678 break;
679 default:
680 goto e_inval;
681 }
682
683 np->srcprefs = (np->srcprefs & prefmask) | pref;
684 retv = 0;
685
686 break;
687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 release_sock(sk);
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return retv;
693
694e_inval:
695 release_sock(sk);
696 return -EINVAL;
697}
698
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800699int ipv6_setsockopt(struct sock *sk, int level, int optname,
700 char __user *optval, int optlen)
701{
702 int err;
703
704 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
705 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
706
707 if (level != SOL_IPV6)
708 return -ENOPROTOOPT;
709
710 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
711#ifdef CONFIG_NETFILTER
712 /* we need to exclude all possible ENOPROTOOPTs except default case */
713 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
714 optname != IPV6_XFRM_POLICY) {
715 lock_sock(sk);
716 err = nf_setsockopt(sk, PF_INET6, optname, optval,
717 optlen);
718 release_sock(sk);
719 }
720#endif
721 return err;
722}
723
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900724EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800725
726#ifdef CONFIG_COMPAT
727int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800728 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800729{
730 int err;
731
732 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800733 if (udp_prot.compat_setsockopt != NULL)
734 return udp_prot.compat_setsockopt(sk, level, optname,
735 optval, optlen);
736 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800737 }
738
739 if (level != SOL_IPV6)
740 return -ENOPROTOOPT;
741
742 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
743#ifdef CONFIG_NETFILTER
744 /* we need to exclude all possible ENOPROTOOPTs except default case */
745 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800746 optname != IPV6_XFRM_POLICY) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800747 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800748 err = compat_nf_setsockopt(sk, PF_INET6, optname,
749 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800750 release_sock(sk);
751 }
752#endif
753 return err;
754}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800755
756EXPORT_SYMBOL(compat_ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800757#endif
758
David S. Miller286930792007-03-07 12:50:46 -0800759static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700760 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900761{
David S. Miller286930792007-03-07 12:50:46 -0800762 struct ipv6_opt_hdr *hdr;
763
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700764 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900765 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700766
767 switch(optname) {
768 case IPV6_HOPOPTS:
769 hdr = opt->hopopt;
770 break;
771 case IPV6_RTHDRDSTOPTS:
772 hdr = opt->dst0opt;
773 break;
774 case IPV6_RTHDR:
775 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
776 break;
777 case IPV6_DSTOPTS:
778 hdr = opt->dst1opt;
779 break;
780 default:
781 return -EINVAL; /* should not happen */
782 }
783
784 if (!hdr)
785 return 0;
David S. Miller286930792007-03-07 12:50:46 -0800786
Chris Wrightd2b02ed2007-03-09 16:19:17 -0800787 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -0700788 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900789 return -EFAULT;
YOSHIFUJI Hideakie5268f12007-03-08 20:48:23 -0800790 return ipv6_optlen(hdr);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900791}
792
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800793static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 char __user *optval, int __user *optlen)
795{
796 struct ipv6_pinfo *np = inet6_sk(sk);
797 int len;
798 int val;
799
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900800 if (ip6_mroute_opt(optname))
801 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (get_user(len, optlen))
804 return -EFAULT;
805 switch (optname) {
806 case IPV6_ADDRFORM:
807 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800808 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 sk->sk_protocol != IPPROTO_TCP)
810 return -EINVAL;
811 if (sk->sk_state != TCP_ESTABLISHED)
812 return -ENOTCONN;
813 val = sk->sk_family;
814 break;
815 case MCAST_MSFILTER:
816 {
817 struct group_filter gsf;
818 int err;
819
820 if (len < GROUP_FILTER_SIZE(0))
821 return -EINVAL;
822 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
823 return -EFAULT;
824 lock_sock(sk);
825 err = ip6_mc_msfget(sk, &gsf,
826 (struct group_filter __user *)optval, optlen);
827 release_sock(sk);
828 return err;
829 }
830
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900831 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 {
833 struct msghdr msg;
834 struct sk_buff *skb;
835
836 if (sk->sk_type != SOCK_STREAM)
837 return -ENOPROTOOPT;
838
839 msg.msg_control = optval;
840 msg.msg_controllen = len;
841 msg.msg_flags = 0;
842
843 lock_sock(sk);
844 skb = np->pktoptions;
845 if (skb)
846 atomic_inc(&skb->users);
847 release_sock(sk);
848
849 if (skb) {
850 int err = datagram_recv_ctl(sk, &msg, skb);
851 kfree_skb(skb);
852 if (err)
853 return err;
854 } else {
855 if (np->rxopt.bits.rxinfo) {
856 struct in6_pktinfo src_info;
857 src_info.ipi6_ifindex = np->mcast_oif;
858 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
859 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
860 }
861 if (np->rxopt.bits.rxhlim) {
862 int hlim = np->mcast_hops;
863 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
864 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900865 if (np->rxopt.bits.rxoinfo) {
866 struct in6_pktinfo src_info;
867 src_info.ipi6_ifindex = np->mcast_oif;
868 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
869 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
870 }
871 if (np->rxopt.bits.rxohlim) {
872 int hlim = np->mcast_hops;
873 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876 len -= msg.msg_controllen;
877 return put_user(len, optlen);
878 }
879 case IPV6_MTU:
880 {
881 struct dst_entry *dst;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900882 val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 lock_sock(sk);
884 dst = sk_dst_get(sk);
885 if (dst) {
886 val = dst_mtu(dst);
887 dst_release(dst);
888 }
889 release_sock(sk);
890 if (!val)
891 return -ENOTCONN;
892 break;
893 }
894
895 case IPV6_V6ONLY:
896 val = np->ipv6only;
897 break;
898
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900899 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 val = np->rxopt.bits.rxinfo;
901 break;
902
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900903 case IPV6_2292PKTINFO:
904 val = np->rxopt.bits.rxoinfo;
905 break;
906
907 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 val = np->rxopt.bits.rxhlim;
909 break;
910
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900911 case IPV6_2292HOPLIMIT:
912 val = np->rxopt.bits.rxohlim;
913 break;
914
915 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 val = np->rxopt.bits.srcrt;
917 break;
918
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900919 case IPV6_2292RTHDR:
920 val = np->rxopt.bits.osrcrt;
921 break;
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900924 case IPV6_RTHDRDSTOPTS:
925 case IPV6_RTHDR:
926 case IPV6_DSTOPTS:
927 {
928
929 lock_sock(sk);
David S. Miller286930792007-03-07 12:50:46 -0800930 len = ipv6_getsockopt_sticky(sk, np->opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700931 optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900932 release_sock(sk);
933 return put_user(len, optlen);
934 }
935
936 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 val = np->rxopt.bits.hopopts;
938 break;
939
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900940 case IPV6_2292HOPOPTS:
941 val = np->rxopt.bits.ohopopts;
942 break;
943
944 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 val = np->rxopt.bits.dstopts;
946 break;
947
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900948 case IPV6_2292DSTOPTS:
949 val = np->rxopt.bits.odstopts;
950 break;
951
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900952 case IPV6_TCLASS:
953 val = np->tclass;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700954 if (val < 0)
955 val = 0;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900956 break;
957
958 case IPV6_RECVTCLASS:
959 val = np->rxopt.bits.rxtclass;
960 break;
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 case IPV6_FLOWINFO:
963 val = np->rxopt.bits.rxflow;
964 break;
965
966 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -0800968 {
969 struct dst_entry *dst;
970
971 if (optname == IPV6_UNICAST_HOPS)
972 val = np->hop_limit;
973 else
974 val = np->mcast_hops;
975
976 dst = sk_dst_get(sk);
977 if (dst) {
978 if (val < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -0400979 val = ip6_dst_hoplimit(dst);
Brian Haleybefffe92006-12-12 17:09:49 -0800980 dst_release(dst);
981 }
982 if (val < 0)
983 val = ipv6_devconf.hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 break;
Brian Haleybefffe92006-12-12 17:09:49 -0800985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 case IPV6_MULTICAST_LOOP:
988 val = np->mc_loop;
989 break;
990
991 case IPV6_MULTICAST_IF:
992 val = np->mcast_oif;
993 break;
994
995 case IPV6_MTU_DISCOVER:
996 val = np->pmtudisc;
997 break;
998
999 case IPV6_RECVERR:
1000 val = np->recverr;
1001 break;
1002
1003 case IPV6_FLOWINFO_SEND:
1004 val = np->sndflow;
1005 break;
1006
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001007 case IPV6_ADDR_PREFERENCES:
1008 val = 0;
1009
1010 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1011 val |= IPV6_PREFER_SRC_TMP;
1012 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1013 val |= IPV6_PREFER_SRC_PUBLIC;
1014 else {
1015 /* XXX: should we return system default? */
1016 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1017 }
1018
1019 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1020 val |= IPV6_PREFER_SRC_COA;
1021 else
1022 val |= IPV6_PREFER_SRC_HOME;
1023 break;
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001026 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028 len = min_t(unsigned int, sizeof(int), len);
1029 if(put_user(len, optlen))
1030 return -EFAULT;
1031 if(copy_to_user(optval,&val,len))
1032 return -EFAULT;
1033 return 0;
1034}
1035
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001036int ipv6_getsockopt(struct sock *sk, int level, int optname,
1037 char __user *optval, int __user *optlen)
1038{
1039 int err;
1040
1041 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1042 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1043
1044 if(level != SOL_IPV6)
1045 return -ENOPROTOOPT;
1046
1047 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1048#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001049 /* we need to exclude all possible ENOPROTOOPTs except default case */
1050 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001051 int len;
1052
1053 if (get_user(len, optlen))
1054 return -EFAULT;
1055
1056 lock_sock(sk);
1057 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1058 &len);
1059 release_sock(sk);
1060 if (err >= 0)
1061 err = put_user(len, optlen);
1062 }
1063#endif
1064 return err;
1065}
1066
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001067EXPORT_SYMBOL(ipv6_getsockopt);
1068
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001069#ifdef CONFIG_COMPAT
1070int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001071 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001072{
1073 int err;
1074
1075 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001076 if (udp_prot.compat_getsockopt != NULL)
1077 return udp_prot.compat_getsockopt(sk, level, optname,
1078 optval, optlen);
1079 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001080 }
1081
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001082 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001083 return -ENOPROTOOPT;
1084
1085 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1086#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001087 /* we need to exclude all possible ENOPROTOOPTs except default case */
1088 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001089 int len;
1090
1091 if (get_user(len, optlen))
1092 return -EFAULT;
1093
1094 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001095 err = compat_nf_getsockopt(sk, PF_INET6,
1096 optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001097 release_sock(sk);
1098 if (err >= 0)
1099 err = put_user(len, optlen);
1100 }
1101#endif
1102 return err;
1103}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001104
1105EXPORT_SYMBOL(compat_ipv6_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001106#endif
1107