blob: 86e28a75267f4dac4a72a4c02413ab6c99945a60 [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)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +090070 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
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
Denis V. Lunev49d074f2008-06-04 15:49:06 +0400164 if (sk->sk_type == SOCK_RAW)
165 break;
166
Denis V. Lunev9596cc822008-06-04 15:49:08 +0400167 if (sk->sk_protocol == IPPROTO_UDP ||
168 sk->sk_protocol == IPPROTO_UDPLITE) {
169 struct udp_sock *up = udp_sk(sk);
170 if (up->pending == AF_INET6) {
171 retv = -EBUSY;
172 break;
173 }
174 } else if (sk->sk_protocol != IPPROTO_TCP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 break;
176
177 if (sk->sk_state != TCP_ESTABLISHED) {
178 retv = -ENOTCONN;
179 break;
180 }
181
182 if (ipv6_only_sock(sk) ||
Brian Haleye773e4f2007-08-24 23:16:08 -0700183 !ipv6_addr_v4mapped(&np->daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 retv = -EADDRNOTAVAIL;
185 break;
186 }
187
188 fl6_free_socklist(sk);
189 ipv6_sock_mc_close(sk);
190
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700191 /*
192 * Sock is moving from IPv6 to IPv4 (sk_prot), so
193 * remove it from the refcnt debug socks count in the
194 * original family...
195 */
196 sk_refcnt_debug_dec(sk);
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (sk->sk_protocol == IPPROTO_TCP) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800199 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700201 sock_prot_inuse_add(net, sk->sk_prot, -1);
202 sock_prot_inuse_add(net, &tcp_prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 local_bh_enable();
204 sk->sk_prot = &tcp_prot;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800205 icsk->icsk_af_ops = &ipv4_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 sk->sk_socket->ops = &inet_stream_ops;
207 sk->sk_family = PF_INET;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800208 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 } else {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800210 struct proto *prot = &udp_prot;
211
David S. Millerdb8dac22008-03-06 16:22:02 -0800212 if (sk->sk_protocol == IPPROTO_UDPLITE)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800213 prot = &udplite_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700215 sock_prot_inuse_add(net, sk->sk_prot, -1);
216 sock_prot_inuse_add(net, prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 local_bh_enable();
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800218 sk->sk_prot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 sk->sk_socket->ops = &inet_dgram_ops;
220 sk->sk_family = PF_INET;
221 }
222 opt = xchg(&np->opt, NULL);
223 if (opt)
224 sock_kfree_s(sk, opt, opt->tot_len);
225 pktopt = xchg(&np->pktoptions, NULL);
226 if (pktopt)
227 kfree_skb(pktopt);
228
229 sk->sk_destruct = inet_sock_destruct;
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700230 /*
231 * ... and add it to the refcnt debug socks count
232 * in the new family. -acme
233 */
234 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 module_put(THIS_MODULE);
236 retv = 0;
237 break;
238 }
239 goto e_inval;
240
241 case IPV6_V6ONLY:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900242 if (optlen < sizeof(int) ||
243 inet_sk(sk)->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 goto e_inval;
245 np->ipv6only = valbool;
246 retv = 0;
247 break;
248
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900249 case IPV6_RECVPKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900250 if (optlen < sizeof(int))
251 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 np->rxopt.bits.rxinfo = valbool;
253 retv = 0;
254 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900255
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900256 case IPV6_2292PKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900257 if (optlen < sizeof(int))
258 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900259 np->rxopt.bits.rxoinfo = valbool;
260 retv = 0;
261 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900263 case IPV6_RECVHOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900264 if (optlen < sizeof(int))
265 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 np->rxopt.bits.rxhlim = valbool;
267 retv = 0;
268 break;
269
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900270 case IPV6_2292HOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900271 if (optlen < sizeof(int))
272 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900273 np->rxopt.bits.rxohlim = valbool;
274 retv = 0;
275 break;
276
277 case IPV6_RECVRTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900278 if (optlen < sizeof(int))
279 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900280 np->rxopt.bits.srcrt = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 retv = 0;
282 break;
283
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900284 case IPV6_2292RTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900285 if (optlen < sizeof(int))
286 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900287 np->rxopt.bits.osrcrt = valbool;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900288 retv = 0;
289 break;
290
291 case IPV6_RECVHOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900292 if (optlen < sizeof(int))
293 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 np->rxopt.bits.hopopts = valbool;
295 retv = 0;
296 break;
297
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900298 case IPV6_2292HOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900299 if (optlen < sizeof(int))
300 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900301 np->rxopt.bits.ohopopts = valbool;
302 retv = 0;
303 break;
304
305 case IPV6_RECVDSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900306 if (optlen < sizeof(int))
307 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 np->rxopt.bits.dstopts = valbool;
309 retv = 0;
310 break;
311
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900312 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900313 if (optlen < sizeof(int))
314 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900315 np->rxopt.bits.odstopts = valbool;
316 retv = 0;
317 break;
318
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900319 case IPV6_TCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900320 if (optlen < sizeof(int))
321 goto e_inval;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700322 if (val < -1 || val > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900323 goto e_inval;
324 np->tclass = val;
325 retv = 0;
326 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900327
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900328 case IPV6_RECVTCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900329 if (optlen < sizeof(int))
330 goto e_inval;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900331 np->rxopt.bits.rxtclass = valbool;
332 retv = 0;
333 break;
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 case IPV6_FLOWINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900336 if (optlen < sizeof(int))
337 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 np->rxopt.bits.rxflow = valbool;
339 retv = 0;
340 break;
341
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900342 case IPV6_HOPOPTS:
343 case IPV6_RTHDRDSTOPTS:
344 case IPV6_RTHDR:
345 case IPV6_DSTOPTS:
346 {
347 struct ipv6_txoptions *opt;
Shan Weiaea74272008-06-19 16:29:39 -0700348
349 /* remove any sticky options header with a zero option
350 * length, per RFC3542.
351 */
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900352 if (optlen == 0)
Luiz Capitulinocb422c42005-11-14 21:43:36 -0800353 optval = NULL;
Shan Weiaea74272008-06-19 16:29:39 -0700354 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
355 optlen & 0x7 || optlen > 8 * 255)
356 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900357
358 /* hop-by-hop / destination options are privileged option */
359 retv = -EPERM;
360 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
361 break;
362
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900363 opt = ipv6_renew_options(sk, np->opt, optname,
364 (struct ipv6_opt_hdr __user *)optval,
365 optlen);
366 if (IS_ERR(opt)) {
367 retv = PTR_ERR(opt);
368 break;
369 }
370
371 /* routing header option needs extra check */
Olaf Kirchdfee0a72007-03-09 13:55:38 -0800372 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900373 struct ipv6_rt_hdr *rthdr = opt->srcrt;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700374 switch (rthdr->type) {
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700375#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700376 case IPV6_SRCRT_TYPE_2:
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700377 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700378#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700379 default:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900380 goto sticky_done;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700381 }
382
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900383 if ((rthdr->hdrlen & 1) ||
384 (rthdr->hdrlen >> 1) != rthdr->segments_left)
385 goto sticky_done;
386 }
387
388 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700389 opt = ipv6_update_options(sk, opt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900390sticky_done:
391 if (opt)
392 sock_kfree_s(sk, opt, opt->tot_len);
393 break;
394 }
395
396 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 {
398 struct ipv6_txoptions *opt = NULL;
399 struct msghdr msg;
400 struct flowi fl;
401 int junk;
402
403 fl.fl6_flowlabel = 0;
404 fl.oif = sk->sk_bound_dev_if;
405
406 if (optlen == 0)
407 goto update;
408
409 /* 1K is probably excessive
410 * 1K is surely not enough, 2K per standard header is 16K.
411 */
412 retv = -EINVAL;
413 if (optlen > 64*1024)
414 break;
415
416 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
417 retv = -ENOBUFS;
418 if (opt == NULL)
419 break;
420
421 memset(opt, 0, sizeof(*opt));
422 opt->tot_len = sizeof(*opt) + optlen;
423 retv = -EFAULT;
424 if (copy_from_user(opt+1, optval, optlen))
425 goto done;
426
427 msg.msg_controllen = optlen;
428 msg.msg_control = (void*)(opt+1);
429
YOSHIFUJI Hideaki91e19082008-06-04 13:02:49 +0900430 retv = datagram_send_ctl(net, &msg, &fl, opt, &junk, &junk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (retv)
432 goto done;
433update:
434 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700435 opt = ipv6_update_options(sk, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436done:
437 if (opt)
438 sock_kfree_s(sk, opt, opt->tot_len);
439 break;
440 }
441 case IPV6_UNICAST_HOPS:
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->hop_limit = val;
447 retv = 0;
448 break;
449
450 case IPV6_MULTICAST_HOPS:
451 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900452 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900453 if (optlen < sizeof(int))
454 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (val > 255 || val < -1)
456 goto e_inval;
457 np->mcast_hops = val;
458 retv = 0;
459 break;
460
461 case IPV6_MULTICAST_LOOP:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900462 if (optlen < sizeof(int))
463 goto e_inval;
YOSHIFUJI Hideaki28d44882008-06-12 03:14:51 +0900464 if (val != valbool)
465 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 np->mc_loop = valbool;
467 retv = 0;
468 break;
469
470 case IPV6_MULTICAST_IF:
471 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900472 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900473 if (optlen < sizeof(int))
474 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Brian Haley4953f0f2007-10-11 14:39:29 -0700476 if (val) {
477 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
478 goto e_inval;
479
Daniel Lezcano8a3edd82008-03-07 11:14:16 -0800480 if (__dev_get_by_index(net, val) == NULL) {
Brian Haley4953f0f2007-10-11 14:39:29 -0700481 retv = -ENODEV;
482 break;
483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485 np->mcast_oif = val;
486 retv = 0;
487 break;
488 case IPV6_ADD_MEMBERSHIP:
489 case IPV6_DROP_MEMBERSHIP:
490 {
491 struct ipv6_mreq mreq;
492
Wang Chena28398b2008-04-07 09:42:07 +0800493 if (optlen < sizeof(struct ipv6_mreq))
494 goto e_inval;
495
Flavio Leitnera96fb492007-08-24 22:16:39 -0700496 retv = -EPROTO;
497 if (inet_sk(sk)->is_icsk)
498 break;
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 retv = -EFAULT;
501 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
502 break;
503
504 if (optname == IPV6_ADD_MEMBERSHIP)
505 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
506 else
507 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
508 break;
509 }
510 case IPV6_JOIN_ANYCAST:
511 case IPV6_LEAVE_ANYCAST:
512 {
513 struct ipv6_mreq mreq;
514
Wang Chena28398b2008-04-07 09:42:07 +0800515 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto e_inval;
517
518 retv = -EFAULT;
519 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
520 break;
521
522 if (optname == IPV6_JOIN_ANYCAST)
523 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
524 else
525 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
526 break;
527 }
528 case MCAST_JOIN_GROUP:
529 case MCAST_LEAVE_GROUP:
530 {
531 struct group_req greq;
532 struct sockaddr_in6 *psin6;
533
Wang Chena28398b2008-04-07 09:42:07 +0800534 if (optlen < sizeof(struct group_req))
535 goto e_inval;
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 retv = -EFAULT;
538 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
539 break;
540 if (greq.gr_group.ss_family != AF_INET6) {
541 retv = -EADDRNOTAVAIL;
542 break;
543 }
544 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
545 if (optname == MCAST_JOIN_GROUP)
546 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
547 &psin6->sin6_addr);
548 else
549 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
550 &psin6->sin6_addr);
551 break;
552 }
553 case MCAST_JOIN_SOURCE_GROUP:
554 case MCAST_LEAVE_SOURCE_GROUP:
555 case MCAST_BLOCK_SOURCE:
556 case MCAST_UNBLOCK_SOURCE:
557 {
558 struct group_source_req greqs;
559 int omode, add;
560
Wang Chena28398b2008-04-07 09:42:07 +0800561 if (optlen < sizeof(struct group_source_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto e_inval;
563 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
564 retv = -EFAULT;
565 break;
566 }
567 if (greqs.gsr_group.ss_family != AF_INET6 ||
568 greqs.gsr_source.ss_family != AF_INET6) {
569 retv = -EADDRNOTAVAIL;
570 break;
571 }
572 if (optname == MCAST_BLOCK_SOURCE) {
573 omode = MCAST_EXCLUDE;
574 add = 1;
575 } else if (optname == MCAST_UNBLOCK_SOURCE) {
576 omode = MCAST_EXCLUDE;
577 add = 0;
578 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
579 struct sockaddr_in6 *psin6;
580
581 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
582 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
583 &psin6->sin6_addr);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700584 /* prior join w/ different source is ok */
585 if (retv && retv != -EADDRINUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 break;
587 omode = MCAST_INCLUDE;
588 add = 1;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700589 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 omode = MCAST_INCLUDE;
591 add = 0;
592 }
593 retv = ip6_mc_source(add, omode, sk, &greqs);
594 break;
595 }
596 case MCAST_MSFILTER:
597 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 extern int sysctl_mld_max_msf;
599 struct group_filter *gsf;
600
601 if (optlen < GROUP_FILTER_SIZE(0))
602 goto e_inval;
603 if (optlen > sysctl_optmem_max) {
604 retv = -ENOBUFS;
605 break;
606 }
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800607 gsf = kmalloc(optlen,GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700608 if (!gsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 retv = -ENOBUFS;
610 break;
611 }
612 retv = -EFAULT;
613 if (copy_from_user(gsf, optval, optlen)) {
614 kfree(gsf);
615 break;
616 }
617 /* numsrc >= (4G-140)/128 overflow in 32 bits */
618 if (gsf->gf_numsrc >= 0x1ffffffU ||
619 gsf->gf_numsrc > sysctl_mld_max_msf) {
620 kfree(gsf);
621 retv = -ENOBUFS;
622 break;
623 }
624 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
625 kfree(gsf);
626 retv = -EINVAL;
627 break;
628 }
629 retv = ip6_mc_msfilter(sk, gsf);
630 kfree(gsf);
631
632 break;
633 }
634 case IPV6_ROUTER_ALERT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900635 if (optlen < sizeof(int))
636 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 retv = ip6_ra_control(sk, val, NULL);
638 break;
639 case IPV6_MTU_DISCOVER:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900640 if (optlen < sizeof(int))
641 goto e_inval;
John Heffner628a5c52007-04-20 15:53:27 -0700642 if (val<0 || val>3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 goto e_inval;
644 np->pmtudisc = val;
645 retv = 0;
646 break;
647 case IPV6_MTU:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900648 if (optlen < sizeof(int))
649 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (val && val < IPV6_MIN_MTU)
651 goto e_inval;
652 np->frag_size = val;
653 retv = 0;
654 break;
655 case IPV6_RECVERR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900656 if (optlen < sizeof(int))
657 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 np->recverr = valbool;
659 if (!val)
660 skb_queue_purge(&sk->sk_error_queue);
661 retv = 0;
662 break;
663 case IPV6_FLOWINFO_SEND:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900664 if (optlen < sizeof(int))
665 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 np->sndflow = valbool;
667 retv = 0;
668 break;
669 case IPV6_FLOWLABEL_MGR:
670 retv = ipv6_flowlabel_opt(sk, optval, optlen);
671 break;
672 case IPV6_IPSEC_POLICY:
673 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700674 retv = -EPERM;
675 if (!capable(CAP_NET_ADMIN))
676 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 retv = xfrm_user_policy(sk, optname, optval, optlen);
678 break;
679
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900680 case IPV6_ADDR_PREFERENCES:
681 {
682 unsigned int pref = 0;
683 unsigned int prefmask = ~0;
684
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900685 if (optlen < sizeof(int))
686 goto e_inval;
687
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900688 retv = -EINVAL;
689
690 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
691 switch (val & (IPV6_PREFER_SRC_PUBLIC|
692 IPV6_PREFER_SRC_TMP|
693 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
694 case IPV6_PREFER_SRC_PUBLIC:
695 pref |= IPV6_PREFER_SRC_PUBLIC;
696 break;
697 case IPV6_PREFER_SRC_TMP:
698 pref |= IPV6_PREFER_SRC_TMP;
699 break;
700 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
701 break;
702 case 0:
703 goto pref_skip_pubtmp;
704 default:
705 goto e_inval;
706 }
707
708 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
709 IPV6_PREFER_SRC_TMP);
710pref_skip_pubtmp:
711
712 /* check HOME/COA conflicts */
713 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
714 case IPV6_PREFER_SRC_HOME:
715 break;
716 case IPV6_PREFER_SRC_COA:
717 pref |= IPV6_PREFER_SRC_COA;
718 case 0:
719 goto pref_skip_coa;
720 default:
721 goto e_inval;
722 }
723
724 prefmask &= ~IPV6_PREFER_SRC_COA;
725pref_skip_coa:
726
727 /* check CGA/NONCGA conflicts */
728 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
729 case IPV6_PREFER_SRC_CGA:
730 case IPV6_PREFER_SRC_NONCGA:
731 case 0:
732 break;
733 default:
734 goto e_inval;
735 }
736
737 np->srcprefs = (np->srcprefs & prefmask) | pref;
738 retv = 0;
739
740 break;
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 release_sock(sk);
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return retv;
747
748e_inval:
749 release_sock(sk);
750 return -EINVAL;
751}
752
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800753int ipv6_setsockopt(struct sock *sk, int level, int optname,
754 char __user *optval, int optlen)
755{
756 int err;
757
758 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
759 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
760
761 if (level != SOL_IPV6)
762 return -ENOPROTOOPT;
763
764 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
765#ifdef CONFIG_NETFILTER
766 /* we need to exclude all possible ENOPROTOOPTs except default case */
767 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
768 optname != IPV6_XFRM_POLICY) {
769 lock_sock(sk);
770 err = nf_setsockopt(sk, PF_INET6, optname, optval,
771 optlen);
772 release_sock(sk);
773 }
774#endif
775 return err;
776}
777
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900778EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800779
780#ifdef CONFIG_COMPAT
781int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800782 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800783{
784 int err;
785
786 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800787 if (udp_prot.compat_setsockopt != NULL)
788 return udp_prot.compat_setsockopt(sk, level, optname,
789 optval, optlen);
790 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800791 }
792
793 if (level != SOL_IPV6)
794 return -ENOPROTOOPT;
795
David L Stevensdae50292008-04-27 01:06:07 -0700796 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
797 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
798 ipv6_setsockopt);
799
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800800 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
801#ifdef CONFIG_NETFILTER
802 /* we need to exclude all possible ENOPROTOOPTs except default case */
803 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800804 optname != IPV6_XFRM_POLICY) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800805 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800806 err = compat_nf_setsockopt(sk, PF_INET6, optname,
807 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800808 release_sock(sk);
809 }
810#endif
811 return err;
812}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800813
814EXPORT_SYMBOL(compat_ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800815#endif
816
David S. Miller286930792007-03-07 12:50:46 -0800817static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700818 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900819{
David S. Miller286930792007-03-07 12:50:46 -0800820 struct ipv6_opt_hdr *hdr;
821
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700822 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900823 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700824
825 switch(optname) {
826 case IPV6_HOPOPTS:
827 hdr = opt->hopopt;
828 break;
829 case IPV6_RTHDRDSTOPTS:
830 hdr = opt->dst0opt;
831 break;
832 case IPV6_RTHDR:
833 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
834 break;
835 case IPV6_DSTOPTS:
836 hdr = opt->dst1opt;
837 break;
838 default:
839 return -EINVAL; /* should not happen */
840 }
841
842 if (!hdr)
843 return 0;
David S. Miller286930792007-03-07 12:50:46 -0800844
Chris Wrightd2b02ed2007-03-09 16:19:17 -0800845 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -0700846 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900847 return -EFAULT;
Yang Hongyang95b496b2008-05-28 16:27:28 +0800848 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900849}
850
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800851static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 char __user *optval, int __user *optlen)
853{
854 struct ipv6_pinfo *np = inet6_sk(sk);
855 int len;
856 int val;
857
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900858 if (ip6_mroute_opt(optname))
859 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (get_user(len, optlen))
862 return -EFAULT;
863 switch (optname) {
864 case IPV6_ADDRFORM:
865 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800866 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 sk->sk_protocol != IPPROTO_TCP)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900868 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (sk->sk_state != TCP_ESTABLISHED)
870 return -ENOTCONN;
871 val = sk->sk_family;
872 break;
873 case MCAST_MSFILTER:
874 {
875 struct group_filter gsf;
876 int err;
877
878 if (len < GROUP_FILTER_SIZE(0))
879 return -EINVAL;
880 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
881 return -EFAULT;
YOSHIFUJI Hideaki20c61fb2008-04-28 14:40:55 +0900882 if (gsf.gf_group.ss_family != AF_INET6)
883 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 lock_sock(sk);
885 err = ip6_mc_msfget(sk, &gsf,
886 (struct group_filter __user *)optval, optlen);
887 release_sock(sk);
888 return err;
889 }
890
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900891 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 {
893 struct msghdr msg;
894 struct sk_buff *skb;
895
896 if (sk->sk_type != SOCK_STREAM)
897 return -ENOPROTOOPT;
898
899 msg.msg_control = optval;
900 msg.msg_controllen = len;
901 msg.msg_flags = 0;
902
903 lock_sock(sk);
904 skb = np->pktoptions;
905 if (skb)
906 atomic_inc(&skb->users);
907 release_sock(sk);
908
909 if (skb) {
910 int err = datagram_recv_ctl(sk, &msg, skb);
911 kfree_skb(skb);
912 if (err)
913 return err;
914 } else {
915 if (np->rxopt.bits.rxinfo) {
916 struct in6_pktinfo src_info;
917 src_info.ipi6_ifindex = np->mcast_oif;
918 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
919 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
920 }
921 if (np->rxopt.bits.rxhlim) {
922 int hlim = np->mcast_hops;
923 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
924 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900925 if (np->rxopt.bits.rxoinfo) {
926 struct in6_pktinfo src_info;
927 src_info.ipi6_ifindex = np->mcast_oif;
928 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
929 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
930 }
931 if (np->rxopt.bits.rxohlim) {
932 int hlim = np->mcast_hops;
933 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936 len -= msg.msg_controllen;
937 return put_user(len, optlen);
938 }
939 case IPV6_MTU:
940 {
941 struct dst_entry *dst;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900942 val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 lock_sock(sk);
944 dst = sk_dst_get(sk);
945 if (dst) {
946 val = dst_mtu(dst);
947 dst_release(dst);
948 }
949 release_sock(sk);
950 if (!val)
951 return -ENOTCONN;
952 break;
953 }
954
955 case IPV6_V6ONLY:
956 val = np->ipv6only;
957 break;
958
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900959 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 val = np->rxopt.bits.rxinfo;
961 break;
962
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900963 case IPV6_2292PKTINFO:
964 val = np->rxopt.bits.rxoinfo;
965 break;
966
967 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 val = np->rxopt.bits.rxhlim;
969 break;
970
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900971 case IPV6_2292HOPLIMIT:
972 val = np->rxopt.bits.rxohlim;
973 break;
974
975 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 val = np->rxopt.bits.srcrt;
977 break;
978
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900979 case IPV6_2292RTHDR:
980 val = np->rxopt.bits.osrcrt;
981 break;
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900984 case IPV6_RTHDRDSTOPTS:
985 case IPV6_RTHDR:
986 case IPV6_DSTOPTS:
987 {
988
989 lock_sock(sk);
David S. Miller286930792007-03-07 12:50:46 -0800990 len = ipv6_getsockopt_sticky(sk, np->opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700991 optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900992 release_sock(sk);
Yang Hongyang05335c22008-05-28 16:23:47 +0800993 /* check if ipv6_getsockopt_sticky() returns err code */
994 if (len < 0)
995 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900996 return put_user(len, optlen);
997 }
998
999 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 val = np->rxopt.bits.hopopts;
1001 break;
1002
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001003 case IPV6_2292HOPOPTS:
1004 val = np->rxopt.bits.ohopopts;
1005 break;
1006
1007 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 val = np->rxopt.bits.dstopts;
1009 break;
1010
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001011 case IPV6_2292DSTOPTS:
1012 val = np->rxopt.bits.odstopts;
1013 break;
1014
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001015 case IPV6_TCLASS:
1016 val = np->tclass;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -07001017 if (val < 0)
1018 val = 0;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001019 break;
1020
1021 case IPV6_RECVTCLASS:
1022 val = np->rxopt.bits.rxtclass;
1023 break;
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 case IPV6_FLOWINFO:
1026 val = np->rxopt.bits.rxflow;
1027 break;
1028
1029 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -08001031 {
1032 struct dst_entry *dst;
1033
1034 if (optname == IPV6_UNICAST_HOPS)
1035 val = np->hop_limit;
1036 else
1037 val = np->mcast_hops;
1038
1039 dst = sk_dst_get(sk);
1040 if (dst) {
1041 if (val < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001042 val = ip6_dst_hoplimit(dst);
Brian Haleybefffe92006-12-12 17:09:49 -08001043 dst_release(dst);
1044 }
1045 if (val < 0)
1046 val = ipv6_devconf.hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 break;
Brian Haleybefffe92006-12-12 17:09:49 -08001048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 case IPV6_MULTICAST_LOOP:
1051 val = np->mc_loop;
1052 break;
1053
1054 case IPV6_MULTICAST_IF:
1055 val = np->mcast_oif;
1056 break;
1057
1058 case IPV6_MTU_DISCOVER:
1059 val = np->pmtudisc;
1060 break;
1061
1062 case IPV6_RECVERR:
1063 val = np->recverr;
1064 break;
1065
1066 case IPV6_FLOWINFO_SEND:
1067 val = np->sndflow;
1068 break;
1069
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001070 case IPV6_ADDR_PREFERENCES:
1071 val = 0;
1072
1073 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1074 val |= IPV6_PREFER_SRC_TMP;
1075 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1076 val |= IPV6_PREFER_SRC_PUBLIC;
1077 else {
1078 /* XXX: should we return system default? */
1079 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1080 }
1081
1082 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1083 val |= IPV6_PREFER_SRC_COA;
1084 else
1085 val |= IPV6_PREFER_SRC_HOME;
1086 break;
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001089 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091 len = min_t(unsigned int, sizeof(int), len);
1092 if(put_user(len, optlen))
1093 return -EFAULT;
1094 if(copy_to_user(optval,&val,len))
1095 return -EFAULT;
1096 return 0;
1097}
1098
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001099int ipv6_getsockopt(struct sock *sk, int level, int optname,
1100 char __user *optval, int __user *optlen)
1101{
1102 int err;
1103
1104 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1105 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1106
1107 if(level != SOL_IPV6)
1108 return -ENOPROTOOPT;
1109
1110 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1111#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001112 /* we need to exclude all possible ENOPROTOOPTs except default case */
1113 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001114 int len;
1115
1116 if (get_user(len, optlen))
1117 return -EFAULT;
1118
1119 lock_sock(sk);
1120 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1121 &len);
1122 release_sock(sk);
1123 if (err >= 0)
1124 err = put_user(len, optlen);
1125 }
1126#endif
1127 return err;
1128}
1129
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001130EXPORT_SYMBOL(ipv6_getsockopt);
1131
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001132#ifdef CONFIG_COMPAT
1133int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001134 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001135{
1136 int err;
1137
1138 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001139 if (udp_prot.compat_getsockopt != NULL)
1140 return udp_prot.compat_getsockopt(sk, level, optname,
1141 optval, optlen);
1142 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001143 }
1144
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001145 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001146 return -ENOPROTOOPT;
1147
Pavel Emelyanov80991792008-04-30 14:49:54 -07001148 if (optname == MCAST_MSFILTER)
1149 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1150 ipv6_getsockopt);
1151
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001152 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1153#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001154 /* we need to exclude all possible ENOPROTOOPTs except default case */
1155 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001156 int len;
1157
1158 if (get_user(len, optlen))
1159 return -EFAULT;
1160
1161 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001162 err = compat_nf_getsockopt(sk, PF_INET6,
1163 optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001164 release_sock(sk);
1165 if (err >= 0)
1166 err = put_user(len, optlen);
1167 }
1168#endif
1169 return err;
1170}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001171
1172EXPORT_SYMBOL(compat_ipv6_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001173#endif
1174