blob: db6fdc1498aa077c54bbcc1e0e5acf5839867533 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * o Truncate getsockopt returns
20 * o Return an optlen of the truncated length if need be
21 *
22 * Changes:
23 * David L Stevens <dlstevens@us.ibm.com>:
24 * - added multicast source filtering API for MLDv2
25 */
26
27#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080028#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/errno.h>
30#include <linux/types.h>
31#include <linux/socket.h>
32#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/net.h>
34#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090035#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/netdevice.h>
37#include <linux/if_arp.h>
38#include <linux/init.h>
39#include <linux/sysctl.h>
40#include <linux/netfilter.h>
41
42#include <net/sock.h>
43#include <net/snmp.h>
44#include <net/ipv6.h>
45#include <net/ndisc.h>
46#include <net/protocol.h>
47#include <net/transp_v6.h>
48#include <net/ip6_route.h>
49#include <net/addrconf.h>
50#include <net/inet_common.h>
51#include <net/tcp.h>
52#include <net/udp.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080053#include <net/udplite.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <net/xfrm.h>
David L Stevensdae50292008-04-27 01:06:07 -070055#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
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
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700107static
108struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
109 struct ipv6_txoptions *opt)
110{
111 if (inet_sk(sk)->is_icsk) {
112 if (opt &&
113 !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
114 inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
115 struct inet_connection_sock *icsk = inet_csk(sk);
116 icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
117 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
118 }
119 opt = xchg(&inet6_sk(sk)->opt, opt);
120 } else {
121 write_lock(&sk->sk_dst_lock);
122 opt = xchg(&inet6_sk(sk)->opt, opt);
123 write_unlock(&sk->sk_dst_lock);
124 }
125 sk_dst_reset(sk);
126
127 return opt;
128}
129
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800130static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 char __user *optval, int optlen)
132{
133 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900134 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 int val, valbool;
136 int retv = -ENOPROTOOPT;
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (optval == NULL)
139 val=0;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900140 else {
141 if (optlen >= sizeof(int)) {
142 if (get_user(val, (int __user *) optval))
143 return -EFAULT;
144 } else
145 val = 0;
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 valbool = (val!=0);
149
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900150 if (ip6_mroute_opt(optname))
151 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 lock_sock(sk);
154
155 switch (optname) {
156
157 case IPV6_ADDRFORM:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900158 if (optlen < sizeof(int))
159 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (val == PF_INET) {
161 struct ipv6_txoptions *opt;
162 struct sk_buff *pktopt;
163
164 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800165 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 sk->sk_protocol != IPPROTO_TCP)
167 break;
168
169 if (sk->sk_state != TCP_ESTABLISHED) {
170 retv = -ENOTCONN;
171 break;
172 }
173
174 if (ipv6_only_sock(sk) ||
Brian Haleye773e4f2007-08-24 23:16:08 -0700175 !ipv6_addr_v4mapped(&np->daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 retv = -EADDRNOTAVAIL;
177 break;
178 }
179
180 fl6_free_socklist(sk);
181 ipv6_sock_mc_close(sk);
182
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700183 /*
184 * Sock is moving from IPv6 to IPv4 (sk_prot), so
185 * remove it from the refcnt debug socks count in the
186 * original family...
187 */
188 sk_refcnt_debug_dec(sk);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (sk->sk_protocol == IPPROTO_TCP) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800191 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700193 sock_prot_inuse_add(net, sk->sk_prot, -1);
194 sock_prot_inuse_add(net, &tcp_prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 local_bh_enable();
196 sk->sk_prot = &tcp_prot;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800197 icsk->icsk_af_ops = &ipv4_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 sk->sk_socket->ops = &inet_stream_ops;
199 sk->sk_family = PF_INET;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800200 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 } else {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800202 struct proto *prot = &udp_prot;
203
David S. Millerdb8dac22008-03-06 16:22:02 -0800204 if (sk->sk_protocol == IPPROTO_UDPLITE)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800205 prot = &udplite_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700207 sock_prot_inuse_add(net, sk->sk_prot, -1);
208 sock_prot_inuse_add(net, prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 local_bh_enable();
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800210 sk->sk_prot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 sk->sk_socket->ops = &inet_dgram_ops;
212 sk->sk_family = PF_INET;
213 }
214 opt = xchg(&np->opt, NULL);
215 if (opt)
216 sock_kfree_s(sk, opt, opt->tot_len);
217 pktopt = xchg(&np->pktoptions, NULL);
218 if (pktopt)
219 kfree_skb(pktopt);
220
221 sk->sk_destruct = inet_sock_destruct;
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700222 /*
223 * ... and add it to the refcnt debug socks count
224 * in the new family. -acme
225 */
226 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 module_put(THIS_MODULE);
228 retv = 0;
229 break;
230 }
231 goto e_inval;
232
233 case IPV6_V6ONLY:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900234 if (optlen < sizeof(int) ||
235 inet_sk(sk)->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 goto e_inval;
237 np->ipv6only = valbool;
238 retv = 0;
239 break;
240
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900241 case IPV6_RECVPKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900242 if (optlen < sizeof(int))
243 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 np->rxopt.bits.rxinfo = valbool;
245 retv = 0;
246 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900247
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900248 case IPV6_2292PKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900249 if (optlen < sizeof(int))
250 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900251 np->rxopt.bits.rxoinfo = valbool;
252 retv = 0;
253 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900255 case IPV6_RECVHOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900256 if (optlen < sizeof(int))
257 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 np->rxopt.bits.rxhlim = valbool;
259 retv = 0;
260 break;
261
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900262 case IPV6_2292HOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900263 if (optlen < sizeof(int))
264 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900265 np->rxopt.bits.rxohlim = valbool;
266 retv = 0;
267 break;
268
269 case IPV6_RECVRTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900270 if (optlen < sizeof(int))
271 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900272 np->rxopt.bits.srcrt = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 retv = 0;
274 break;
275
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900276 case IPV6_2292RTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900277 if (optlen < sizeof(int))
278 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900279 np->rxopt.bits.osrcrt = valbool;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900280 retv = 0;
281 break;
282
283 case IPV6_RECVHOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900284 if (optlen < sizeof(int))
285 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 np->rxopt.bits.hopopts = valbool;
287 retv = 0;
288 break;
289
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900290 case IPV6_2292HOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900291 if (optlen < sizeof(int))
292 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900293 np->rxopt.bits.ohopopts = valbool;
294 retv = 0;
295 break;
296
297 case IPV6_RECVDSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900298 if (optlen < sizeof(int))
299 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 np->rxopt.bits.dstopts = valbool;
301 retv = 0;
302 break;
303
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900304 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900305 if (optlen < sizeof(int))
306 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900307 np->rxopt.bits.odstopts = valbool;
308 retv = 0;
309 break;
310
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900311 case IPV6_TCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900312 if (optlen < sizeof(int))
313 goto e_inval;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700314 if (val < -1 || val > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900315 goto e_inval;
316 np->tclass = val;
317 retv = 0;
318 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900319
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900320 case IPV6_RECVTCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900321 if (optlen < sizeof(int))
322 goto e_inval;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900323 np->rxopt.bits.rxtclass = valbool;
324 retv = 0;
325 break;
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 case IPV6_FLOWINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900328 if (optlen < sizeof(int))
329 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 np->rxopt.bits.rxflow = valbool;
331 retv = 0;
332 break;
333
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900334 case IPV6_HOPOPTS:
335 case IPV6_RTHDRDSTOPTS:
336 case IPV6_RTHDR:
337 case IPV6_DSTOPTS:
338 {
339 struct ipv6_txoptions *opt;
340 if (optlen == 0)
Luiz Capitulinocb422c42005-11-14 21:43:36 -0800341 optval = NULL;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900342
343 /* hop-by-hop / destination options are privileged option */
344 retv = -EPERM;
345 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
346 break;
347
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900348 if (optlen < sizeof(struct ipv6_opt_hdr) ||
349 optlen & 0x7 || optlen > 8 * 255)
350 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900351
352 opt = ipv6_renew_options(sk, np->opt, optname,
353 (struct ipv6_opt_hdr __user *)optval,
354 optlen);
355 if (IS_ERR(opt)) {
356 retv = PTR_ERR(opt);
357 break;
358 }
359
360 /* routing header option needs extra check */
Olaf Kirchdfee0a72007-03-09 13:55:38 -0800361 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900362 struct ipv6_rt_hdr *rthdr = opt->srcrt;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700363 switch (rthdr->type) {
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700364#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700365 case IPV6_SRCRT_TYPE_2:
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700366 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700367#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700368 default:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900369 goto sticky_done;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700370 }
371
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900372 if ((rthdr->hdrlen & 1) ||
373 (rthdr->hdrlen >> 1) != rthdr->segments_left)
374 goto sticky_done;
375 }
376
377 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700378 opt = ipv6_update_options(sk, opt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900379sticky_done:
380 if (opt)
381 sock_kfree_s(sk, opt, opt->tot_len);
382 break;
383 }
384
385 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 {
387 struct ipv6_txoptions *opt = NULL;
388 struct msghdr msg;
389 struct flowi fl;
390 int junk;
391
392 fl.fl6_flowlabel = 0;
393 fl.oif = sk->sk_bound_dev_if;
394
395 if (optlen == 0)
396 goto update;
397
398 /* 1K is probably excessive
399 * 1K is surely not enough, 2K per standard header is 16K.
400 */
401 retv = -EINVAL;
402 if (optlen > 64*1024)
403 break;
404
405 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
406 retv = -ENOBUFS;
407 if (opt == NULL)
408 break;
409
410 memset(opt, 0, sizeof(*opt));
411 opt->tot_len = sizeof(*opt) + optlen;
412 retv = -EFAULT;
413 if (copy_from_user(opt+1, optval, optlen))
414 goto done;
415
416 msg.msg_controllen = optlen;
417 msg.msg_control = (void*)(opt+1);
418
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900419 retv = datagram_send_ctl(&msg, &fl, opt, &junk, &junk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (retv)
421 goto done;
422update:
423 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700424 opt = ipv6_update_options(sk, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425done:
426 if (opt)
427 sock_kfree_s(sk, opt, opt->tot_len);
428 break;
429 }
430 case IPV6_UNICAST_HOPS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900431 if (optlen < sizeof(int))
432 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (val > 255 || val < -1)
434 goto e_inval;
435 np->hop_limit = val;
436 retv = 0;
437 break;
438
439 case IPV6_MULTICAST_HOPS:
440 if (sk->sk_type == SOCK_STREAM)
441 goto e_inval;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900442 if (optlen < sizeof(int))
443 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (val > 255 || val < -1)
445 goto e_inval;
446 np->mcast_hops = val;
447 retv = 0;
448 break;
449
450 case IPV6_MULTICAST_LOOP:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900451 if (optlen < sizeof(int))
452 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 np->mc_loop = valbool;
454 retv = 0;
455 break;
456
457 case IPV6_MULTICAST_IF:
458 if (sk->sk_type == SOCK_STREAM)
459 goto e_inval;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900460 if (optlen < sizeof(int))
461 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Brian Haley4953f0f2007-10-11 14:39:29 -0700463 if (val) {
464 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
465 goto e_inval;
466
Daniel Lezcano8a3edd82008-03-07 11:14:16 -0800467 if (__dev_get_by_index(net, val) == NULL) {
Brian Haley4953f0f2007-10-11 14:39:29 -0700468 retv = -ENODEV;
469 break;
470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472 np->mcast_oif = val;
473 retv = 0;
474 break;
475 case IPV6_ADD_MEMBERSHIP:
476 case IPV6_DROP_MEMBERSHIP:
477 {
478 struct ipv6_mreq mreq;
479
Wang Chena28398b2008-04-07 09:42:07 +0800480 if (optlen < sizeof(struct ipv6_mreq))
481 goto e_inval;
482
Flavio Leitnera96fb492007-08-24 22:16:39 -0700483 retv = -EPROTO;
484 if (inet_sk(sk)->is_icsk)
485 break;
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 retv = -EFAULT;
488 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
489 break;
490
491 if (optname == IPV6_ADD_MEMBERSHIP)
492 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
493 else
494 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
495 break;
496 }
497 case IPV6_JOIN_ANYCAST:
498 case IPV6_LEAVE_ANYCAST:
499 {
500 struct ipv6_mreq mreq;
501
Wang Chena28398b2008-04-07 09:42:07 +0800502 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 goto e_inval;
504
505 retv = -EFAULT;
506 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
507 break;
508
509 if (optname == IPV6_JOIN_ANYCAST)
510 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
511 else
512 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
513 break;
514 }
515 case MCAST_JOIN_GROUP:
516 case MCAST_LEAVE_GROUP:
517 {
518 struct group_req greq;
519 struct sockaddr_in6 *psin6;
520
Wang Chena28398b2008-04-07 09:42:07 +0800521 if (optlen < sizeof(struct group_req))
522 goto e_inval;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 retv = -EFAULT;
525 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
526 break;
527 if (greq.gr_group.ss_family != AF_INET6) {
528 retv = -EADDRNOTAVAIL;
529 break;
530 }
531 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
532 if (optname == MCAST_JOIN_GROUP)
533 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
534 &psin6->sin6_addr);
535 else
536 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
537 &psin6->sin6_addr);
538 break;
539 }
540 case MCAST_JOIN_SOURCE_GROUP:
541 case MCAST_LEAVE_SOURCE_GROUP:
542 case MCAST_BLOCK_SOURCE:
543 case MCAST_UNBLOCK_SOURCE:
544 {
545 struct group_source_req greqs;
546 int omode, add;
547
Wang Chena28398b2008-04-07 09:42:07 +0800548 if (optlen < sizeof(struct group_source_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 goto e_inval;
550 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
551 retv = -EFAULT;
552 break;
553 }
554 if (greqs.gsr_group.ss_family != AF_INET6 ||
555 greqs.gsr_source.ss_family != AF_INET6) {
556 retv = -EADDRNOTAVAIL;
557 break;
558 }
559 if (optname == MCAST_BLOCK_SOURCE) {
560 omode = MCAST_EXCLUDE;
561 add = 1;
562 } else if (optname == MCAST_UNBLOCK_SOURCE) {
563 omode = MCAST_EXCLUDE;
564 add = 0;
565 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
566 struct sockaddr_in6 *psin6;
567
568 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
569 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
570 &psin6->sin6_addr);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700571 /* prior join w/ different source is ok */
572 if (retv && retv != -EADDRINUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 break;
574 omode = MCAST_INCLUDE;
575 add = 1;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700576 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 omode = MCAST_INCLUDE;
578 add = 0;
579 }
580 retv = ip6_mc_source(add, omode, sk, &greqs);
581 break;
582 }
583 case MCAST_MSFILTER:
584 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 extern int sysctl_mld_max_msf;
586 struct group_filter *gsf;
587
588 if (optlen < GROUP_FILTER_SIZE(0))
589 goto e_inval;
590 if (optlen > sysctl_optmem_max) {
591 retv = -ENOBUFS;
592 break;
593 }
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800594 gsf = kmalloc(optlen,GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700595 if (!gsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 retv = -ENOBUFS;
597 break;
598 }
599 retv = -EFAULT;
600 if (copy_from_user(gsf, optval, optlen)) {
601 kfree(gsf);
602 break;
603 }
604 /* numsrc >= (4G-140)/128 overflow in 32 bits */
605 if (gsf->gf_numsrc >= 0x1ffffffU ||
606 gsf->gf_numsrc > sysctl_mld_max_msf) {
607 kfree(gsf);
608 retv = -ENOBUFS;
609 break;
610 }
611 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
612 kfree(gsf);
613 retv = -EINVAL;
614 break;
615 }
616 retv = ip6_mc_msfilter(sk, gsf);
617 kfree(gsf);
618
619 break;
620 }
621 case IPV6_ROUTER_ALERT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900622 if (optlen < sizeof(int))
623 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 retv = ip6_ra_control(sk, val, NULL);
625 break;
626 case IPV6_MTU_DISCOVER:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900627 if (optlen < sizeof(int))
628 goto e_inval;
John Heffner628a5c52007-04-20 15:53:27 -0700629 if (val<0 || val>3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 goto e_inval;
631 np->pmtudisc = val;
632 retv = 0;
633 break;
634 case IPV6_MTU:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900635 if (optlen < sizeof(int))
636 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (val && val < IPV6_MIN_MTU)
638 goto e_inval;
639 np->frag_size = val;
640 retv = 0;
641 break;
642 case IPV6_RECVERR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900643 if (optlen < sizeof(int))
644 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 np->recverr = valbool;
646 if (!val)
647 skb_queue_purge(&sk->sk_error_queue);
648 retv = 0;
649 break;
650 case IPV6_FLOWINFO_SEND:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900651 if (optlen < sizeof(int))
652 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 np->sndflow = valbool;
654 retv = 0;
655 break;
656 case IPV6_FLOWLABEL_MGR:
657 retv = ipv6_flowlabel_opt(sk, optval, optlen);
658 break;
659 case IPV6_IPSEC_POLICY:
660 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700661 retv = -EPERM;
662 if (!capable(CAP_NET_ADMIN))
663 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 retv = xfrm_user_policy(sk, optname, optval, optlen);
665 break;
666
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900667 case IPV6_ADDR_PREFERENCES:
668 {
669 unsigned int pref = 0;
670 unsigned int prefmask = ~0;
671
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900672 if (optlen < sizeof(int))
673 goto e_inval;
674
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900675 retv = -EINVAL;
676
677 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
678 switch (val & (IPV6_PREFER_SRC_PUBLIC|
679 IPV6_PREFER_SRC_TMP|
680 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
681 case IPV6_PREFER_SRC_PUBLIC:
682 pref |= IPV6_PREFER_SRC_PUBLIC;
683 break;
684 case IPV6_PREFER_SRC_TMP:
685 pref |= IPV6_PREFER_SRC_TMP;
686 break;
687 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
688 break;
689 case 0:
690 goto pref_skip_pubtmp;
691 default:
692 goto e_inval;
693 }
694
695 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
696 IPV6_PREFER_SRC_TMP);
697pref_skip_pubtmp:
698
699 /* check HOME/COA conflicts */
700 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
701 case IPV6_PREFER_SRC_HOME:
702 break;
703 case IPV6_PREFER_SRC_COA:
704 pref |= IPV6_PREFER_SRC_COA;
705 case 0:
706 goto pref_skip_coa;
707 default:
708 goto e_inval;
709 }
710
711 prefmask &= ~IPV6_PREFER_SRC_COA;
712pref_skip_coa:
713
714 /* check CGA/NONCGA conflicts */
715 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
716 case IPV6_PREFER_SRC_CGA:
717 case IPV6_PREFER_SRC_NONCGA:
718 case 0:
719 break;
720 default:
721 goto e_inval;
722 }
723
724 np->srcprefs = (np->srcprefs & prefmask) | pref;
725 retv = 0;
726
727 break;
728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 release_sock(sk);
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return retv;
734
735e_inval:
736 release_sock(sk);
737 return -EINVAL;
738}
739
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800740int ipv6_setsockopt(struct sock *sk, int level, int optname,
741 char __user *optval, int optlen)
742{
743 int err;
744
745 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
746 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
747
748 if (level != SOL_IPV6)
749 return -ENOPROTOOPT;
750
751 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
752#ifdef CONFIG_NETFILTER
753 /* we need to exclude all possible ENOPROTOOPTs except default case */
754 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
755 optname != IPV6_XFRM_POLICY) {
756 lock_sock(sk);
757 err = nf_setsockopt(sk, PF_INET6, optname, optval,
758 optlen);
759 release_sock(sk);
760 }
761#endif
762 return err;
763}
764
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900765EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800766
767#ifdef CONFIG_COMPAT
768int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800769 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800770{
771 int err;
772
773 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800774 if (udp_prot.compat_setsockopt != NULL)
775 return udp_prot.compat_setsockopt(sk, level, optname,
776 optval, optlen);
777 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800778 }
779
780 if (level != SOL_IPV6)
781 return -ENOPROTOOPT;
782
David L Stevensdae50292008-04-27 01:06:07 -0700783 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
784 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
785 ipv6_setsockopt);
786
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800787 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
788#ifdef CONFIG_NETFILTER
789 /* we need to exclude all possible ENOPROTOOPTs except default case */
790 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800791 optname != IPV6_XFRM_POLICY) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800792 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800793 err = compat_nf_setsockopt(sk, PF_INET6, optname,
794 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800795 release_sock(sk);
796 }
797#endif
798 return err;
799}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800800
801EXPORT_SYMBOL(compat_ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800802#endif
803
David S. Miller286930792007-03-07 12:50:46 -0800804static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700805 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900806{
David S. Miller286930792007-03-07 12:50:46 -0800807 struct ipv6_opt_hdr *hdr;
808
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700809 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900810 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700811
812 switch(optname) {
813 case IPV6_HOPOPTS:
814 hdr = opt->hopopt;
815 break;
816 case IPV6_RTHDRDSTOPTS:
817 hdr = opt->dst0opt;
818 break;
819 case IPV6_RTHDR:
820 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
821 break;
822 case IPV6_DSTOPTS:
823 hdr = opt->dst1opt;
824 break;
825 default:
826 return -EINVAL; /* should not happen */
827 }
828
829 if (!hdr)
830 return 0;
David S. Miller286930792007-03-07 12:50:46 -0800831
Chris Wrightd2b02ed2007-03-09 16:19:17 -0800832 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -0700833 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900834 return -EFAULT;
YOSHIFUJI Hideakie5268f12007-03-08 20:48:23 -0800835 return ipv6_optlen(hdr);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900836}
837
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800838static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 char __user *optval, int __user *optlen)
840{
841 struct ipv6_pinfo *np = inet6_sk(sk);
842 int len;
843 int val;
844
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900845 if (ip6_mroute_opt(optname))
846 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (get_user(len, optlen))
849 return -EFAULT;
850 switch (optname) {
851 case IPV6_ADDRFORM:
852 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800853 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 sk->sk_protocol != IPPROTO_TCP)
855 return -EINVAL;
856 if (sk->sk_state != TCP_ESTABLISHED)
857 return -ENOTCONN;
858 val = sk->sk_family;
859 break;
860 case MCAST_MSFILTER:
861 {
862 struct group_filter gsf;
863 int err;
864
865 if (len < GROUP_FILTER_SIZE(0))
866 return -EINVAL;
867 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
868 return -EFAULT;
869 lock_sock(sk);
870 err = ip6_mc_msfget(sk, &gsf,
871 (struct group_filter __user *)optval, optlen);
872 release_sock(sk);
873 return err;
874 }
875
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900876 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 {
878 struct msghdr msg;
879 struct sk_buff *skb;
880
881 if (sk->sk_type != SOCK_STREAM)
882 return -ENOPROTOOPT;
883
884 msg.msg_control = optval;
885 msg.msg_controllen = len;
886 msg.msg_flags = 0;
887
888 lock_sock(sk);
889 skb = np->pktoptions;
890 if (skb)
891 atomic_inc(&skb->users);
892 release_sock(sk);
893
894 if (skb) {
895 int err = datagram_recv_ctl(sk, &msg, skb);
896 kfree_skb(skb);
897 if (err)
898 return err;
899 } else {
900 if (np->rxopt.bits.rxinfo) {
901 struct in6_pktinfo src_info;
902 src_info.ipi6_ifindex = np->mcast_oif;
903 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
904 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
905 }
906 if (np->rxopt.bits.rxhlim) {
907 int hlim = np->mcast_hops;
908 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
909 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900910 if (np->rxopt.bits.rxoinfo) {
911 struct in6_pktinfo src_info;
912 src_info.ipi6_ifindex = np->mcast_oif;
913 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
914 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
915 }
916 if (np->rxopt.bits.rxohlim) {
917 int hlim = np->mcast_hops;
918 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921 len -= msg.msg_controllen;
922 return put_user(len, optlen);
923 }
924 case IPV6_MTU:
925 {
926 struct dst_entry *dst;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900927 val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 lock_sock(sk);
929 dst = sk_dst_get(sk);
930 if (dst) {
931 val = dst_mtu(dst);
932 dst_release(dst);
933 }
934 release_sock(sk);
935 if (!val)
936 return -ENOTCONN;
937 break;
938 }
939
940 case IPV6_V6ONLY:
941 val = np->ipv6only;
942 break;
943
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900944 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 val = np->rxopt.bits.rxinfo;
946 break;
947
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900948 case IPV6_2292PKTINFO:
949 val = np->rxopt.bits.rxoinfo;
950 break;
951
952 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 val = np->rxopt.bits.rxhlim;
954 break;
955
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900956 case IPV6_2292HOPLIMIT:
957 val = np->rxopt.bits.rxohlim;
958 break;
959
960 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 val = np->rxopt.bits.srcrt;
962 break;
963
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900964 case IPV6_2292RTHDR:
965 val = np->rxopt.bits.osrcrt;
966 break;
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900969 case IPV6_RTHDRDSTOPTS:
970 case IPV6_RTHDR:
971 case IPV6_DSTOPTS:
972 {
973
974 lock_sock(sk);
David S. Miller286930792007-03-07 12:50:46 -0800975 len = ipv6_getsockopt_sticky(sk, np->opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700976 optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900977 release_sock(sk);
978 return put_user(len, optlen);
979 }
980
981 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 val = np->rxopt.bits.hopopts;
983 break;
984
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900985 case IPV6_2292HOPOPTS:
986 val = np->rxopt.bits.ohopopts;
987 break;
988
989 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 val = np->rxopt.bits.dstopts;
991 break;
992
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900993 case IPV6_2292DSTOPTS:
994 val = np->rxopt.bits.odstopts;
995 break;
996
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900997 case IPV6_TCLASS:
998 val = np->tclass;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700999 if (val < 0)
1000 val = 0;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001001 break;
1002
1003 case IPV6_RECVTCLASS:
1004 val = np->rxopt.bits.rxtclass;
1005 break;
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 case IPV6_FLOWINFO:
1008 val = np->rxopt.bits.rxflow;
1009 break;
1010
1011 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -08001013 {
1014 struct dst_entry *dst;
1015
1016 if (optname == IPV6_UNICAST_HOPS)
1017 val = np->hop_limit;
1018 else
1019 val = np->mcast_hops;
1020
1021 dst = sk_dst_get(sk);
1022 if (dst) {
1023 if (val < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001024 val = ip6_dst_hoplimit(dst);
Brian Haleybefffe92006-12-12 17:09:49 -08001025 dst_release(dst);
1026 }
1027 if (val < 0)
1028 val = ipv6_devconf.hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 break;
Brian Haleybefffe92006-12-12 17:09:49 -08001030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 case IPV6_MULTICAST_LOOP:
1033 val = np->mc_loop;
1034 break;
1035
1036 case IPV6_MULTICAST_IF:
1037 val = np->mcast_oif;
1038 break;
1039
1040 case IPV6_MTU_DISCOVER:
1041 val = np->pmtudisc;
1042 break;
1043
1044 case IPV6_RECVERR:
1045 val = np->recverr;
1046 break;
1047
1048 case IPV6_FLOWINFO_SEND:
1049 val = np->sndflow;
1050 break;
1051
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001052 case IPV6_ADDR_PREFERENCES:
1053 val = 0;
1054
1055 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1056 val |= IPV6_PREFER_SRC_TMP;
1057 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1058 val |= IPV6_PREFER_SRC_PUBLIC;
1059 else {
1060 /* XXX: should we return system default? */
1061 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1062 }
1063
1064 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1065 val |= IPV6_PREFER_SRC_COA;
1066 else
1067 val |= IPV6_PREFER_SRC_HOME;
1068 break;
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001071 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073 len = min_t(unsigned int, sizeof(int), len);
1074 if(put_user(len, optlen))
1075 return -EFAULT;
1076 if(copy_to_user(optval,&val,len))
1077 return -EFAULT;
1078 return 0;
1079}
1080
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001081int ipv6_getsockopt(struct sock *sk, int level, int optname,
1082 char __user *optval, int __user *optlen)
1083{
1084 int err;
1085
1086 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1087 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1088
1089 if(level != SOL_IPV6)
1090 return -ENOPROTOOPT;
1091
1092 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1093#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001094 /* we need to exclude all possible ENOPROTOOPTs except default case */
1095 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001096 int len;
1097
1098 if (get_user(len, optlen))
1099 return -EFAULT;
1100
1101 lock_sock(sk);
1102 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1103 &len);
1104 release_sock(sk);
1105 if (err >= 0)
1106 err = put_user(len, optlen);
1107 }
1108#endif
1109 return err;
1110}
1111
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001112EXPORT_SYMBOL(ipv6_getsockopt);
1113
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001114#ifdef CONFIG_COMPAT
1115int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001116 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001117{
1118 int err;
1119
1120 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001121 if (udp_prot.compat_getsockopt != NULL)
1122 return udp_prot.compat_getsockopt(sk, level, optname,
1123 optval, optlen);
1124 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001125 }
1126
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001127 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001128 return -ENOPROTOOPT;
1129
1130 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1131#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001132 /* we need to exclude all possible ENOPROTOOPTs except default case */
1133 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001134 int len;
1135
1136 if (get_user(len, optlen))
1137 return -EFAULT;
1138
1139 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001140 err = compat_nf_getsockopt(sk, PF_INET6,
1141 optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001142 release_sock(sk);
1143 if (err >= 0)
1144 err = put_user(len, optlen);
1145 }
1146#endif
1147 return err;
1148}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001149
1150EXPORT_SYMBOL(compat_ipv6_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001151#endif
1152