blob: 4e5eac301f91835a9bebb5b77a1c5a48c8cd9023 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 BSD socket options interface
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Based on linux/net/ipv4/ip_sockglue.c
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 * FIXME: Make the setsockopt code POSIX compliant: That is
16 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * o Truncate getsockopt returns
18 * o Return an optlen of the truncated length if need be
19 *
20 * Changes:
21 * David L Stevens <dlstevens@us.ibm.com>:
22 * - added multicast source filtering API for MLDv2
23 */
24
25#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080026#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/types.h>
29#include <linux/socket.h>
30#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/net.h>
32#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090033#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/netdevice.h>
35#include <linux/if_arp.h>
36#include <linux/init.h>
37#include <linux/sysctl.h>
38#include <linux/netfilter.h>
39
40#include <net/sock.h>
41#include <net/snmp.h>
42#include <net/ipv6.h>
43#include <net/ndisc.h>
44#include <net/protocol.h>
45#include <net/transp_v6.h>
46#include <net/ip6_route.h>
47#include <net/addrconf.h>
48#include <net/inet_common.h>
49#include <net/tcp.h>
50#include <net/udp.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080051#include <net/udplite.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <net/xfrm.h>
David L Stevensdae50292008-04-27 01:06:07 -070053#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#include <asm/uaccess.h>
56
Eric Dumazetba899662005-08-26 12:05:31 -070057DEFINE_SNMP_STAT(struct ipstats_mib, ipv6_statistics) __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059struct ip6_ra_chain *ip6_ra_chain;
60DEFINE_RWLOCK(ip6_ra_lock);
61
Denis V. Lunev725a8ff2008-07-19 00:28:58 -070062int ip6_ra_control(struct sock *sk, int sel)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 struct ip6_ra_chain *ra, *new_ra, **rap;
65
66 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
67 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num != IPPROTO_RAW)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +090068 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
71
72 write_lock_bh(&ip6_ra_lock);
73 for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
74 if (ra->sk == sk) {
75 if (sel>=0) {
76 write_unlock_bh(&ip6_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -080077 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return -EADDRINUSE;
79 }
80
81 *rap = ra->next;
82 write_unlock_bh(&ip6_ra_lock);
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 sock_put(sk);
85 kfree(ra);
86 return 0;
87 }
88 }
89 if (new_ra == NULL) {
90 write_unlock_bh(&ip6_ra_lock);
91 return -ENOBUFS;
92 }
93 new_ra->sk = sk;
94 new_ra->sel = sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 new_ra->next = ra;
96 *rap = new_ra;
97 sock_hold(sk);
98 write_unlock_bh(&ip6_ra_lock);
99 return 0;
100}
101
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700102static
103struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
104 struct ipv6_txoptions *opt)
105{
106 if (inet_sk(sk)->is_icsk) {
107 if (opt &&
108 !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
109 inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
110 struct inet_connection_sock *icsk = inet_csk(sk);
111 icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
112 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
113 }
114 opt = xchg(&inet6_sk(sk)->opt, opt);
115 } else {
116 write_lock(&sk->sk_dst_lock);
117 opt = xchg(&inet6_sk(sk)->opt, opt);
118 write_unlock(&sk->sk_dst_lock);
119 }
120 sk_dst_reset(sk);
121
122 return opt;
123}
124
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800125static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 char __user *optval, int optlen)
127{
128 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900129 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 int val, valbool;
131 int retv = -ENOPROTOOPT;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (optval == NULL)
134 val=0;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900135 else {
136 if (optlen >= sizeof(int)) {
137 if (get_user(val, (int __user *) optval))
138 return -EFAULT;
139 } else
140 val = 0;
141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 valbool = (val!=0);
144
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900145 if (ip6_mroute_opt(optname))
146 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 lock_sock(sk);
149
150 switch (optname) {
151
152 case IPV6_ADDRFORM:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900153 if (optlen < sizeof(int))
154 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (val == PF_INET) {
156 struct ipv6_txoptions *opt;
157 struct sk_buff *pktopt;
158
Denis V. Lunev49d074f2008-06-04 15:49:06 +0400159 if (sk->sk_type == SOCK_RAW)
160 break;
161
Denis V. Lunev9596cc822008-06-04 15:49:08 +0400162 if (sk->sk_protocol == IPPROTO_UDP ||
163 sk->sk_protocol == IPPROTO_UDPLITE) {
164 struct udp_sock *up = udp_sk(sk);
165 if (up->pending == AF_INET6) {
166 retv = -EBUSY;
167 break;
168 }
169 } else if (sk->sk_protocol != IPPROTO_TCP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 break;
171
172 if (sk->sk_state != TCP_ESTABLISHED) {
173 retv = -ENOTCONN;
174 break;
175 }
176
177 if (ipv6_only_sock(sk) ||
Brian Haleye773e4f2007-08-24 23:16:08 -0700178 !ipv6_addr_v4mapped(&np->daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 retv = -EADDRNOTAVAIL;
180 break;
181 }
182
183 fl6_free_socklist(sk);
184 ipv6_sock_mc_close(sk);
185
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700186 /*
187 * Sock is moving from IPv6 to IPv4 (sk_prot), so
188 * remove it from the refcnt debug socks count in the
189 * original family...
190 */
191 sk_refcnt_debug_dec(sk);
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (sk->sk_protocol == IPPROTO_TCP) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800194 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700196 sock_prot_inuse_add(net, sk->sk_prot, -1);
197 sock_prot_inuse_add(net, &tcp_prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 local_bh_enable();
199 sk->sk_prot = &tcp_prot;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800200 icsk->icsk_af_ops = &ipv4_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 sk->sk_socket->ops = &inet_stream_ops;
202 sk->sk_family = PF_INET;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800203 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 } else {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800205 struct proto *prot = &udp_prot;
206
David S. Millerdb8dac22008-03-06 16:22:02 -0800207 if (sk->sk_protocol == IPPROTO_UDPLITE)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800208 prot = &udplite_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700210 sock_prot_inuse_add(net, sk->sk_prot, -1);
211 sock_prot_inuse_add(net, prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 local_bh_enable();
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800213 sk->sk_prot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 sk->sk_socket->ops = &inet_dgram_ops;
215 sk->sk_family = PF_INET;
216 }
217 opt = xchg(&np->opt, NULL);
218 if (opt)
219 sock_kfree_s(sk, opt, opt->tot_len);
220 pktopt = xchg(&np->pktoptions, NULL);
221 if (pktopt)
222 kfree_skb(pktopt);
223
224 sk->sk_destruct = inet_sock_destruct;
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700225 /*
226 * ... and add it to the refcnt debug socks count
227 * in the new family. -acme
228 */
229 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 module_put(THIS_MODULE);
231 retv = 0;
232 break;
233 }
234 goto e_inval;
235
236 case IPV6_V6ONLY:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900237 if (optlen < sizeof(int) ||
238 inet_sk(sk)->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 goto e_inval;
240 np->ipv6only = valbool;
241 retv = 0;
242 break;
243
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900244 case IPV6_RECVPKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900245 if (optlen < sizeof(int))
246 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 np->rxopt.bits.rxinfo = valbool;
248 retv = 0;
249 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900250
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900251 case IPV6_2292PKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900252 if (optlen < sizeof(int))
253 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900254 np->rxopt.bits.rxoinfo = valbool;
255 retv = 0;
256 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900258 case IPV6_RECVHOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900259 if (optlen < sizeof(int))
260 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 np->rxopt.bits.rxhlim = valbool;
262 retv = 0;
263 break;
264
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900265 case IPV6_2292HOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900266 if (optlen < sizeof(int))
267 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900268 np->rxopt.bits.rxohlim = valbool;
269 retv = 0;
270 break;
271
272 case IPV6_RECVRTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900273 if (optlen < sizeof(int))
274 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900275 np->rxopt.bits.srcrt = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 retv = 0;
277 break;
278
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900279 case IPV6_2292RTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900280 if (optlen < sizeof(int))
281 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900282 np->rxopt.bits.osrcrt = valbool;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900283 retv = 0;
284 break;
285
286 case IPV6_RECVHOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900287 if (optlen < sizeof(int))
288 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 np->rxopt.bits.hopopts = valbool;
290 retv = 0;
291 break;
292
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900293 case IPV6_2292HOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900294 if (optlen < sizeof(int))
295 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900296 np->rxopt.bits.ohopopts = valbool;
297 retv = 0;
298 break;
299
300 case IPV6_RECVDSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900301 if (optlen < sizeof(int))
302 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 np->rxopt.bits.dstopts = valbool;
304 retv = 0;
305 break;
306
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900307 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900308 if (optlen < sizeof(int))
309 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900310 np->rxopt.bits.odstopts = valbool;
311 retv = 0;
312 break;
313
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900314 case IPV6_TCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900315 if (optlen < sizeof(int))
316 goto e_inval;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700317 if (val < -1 || val > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900318 goto e_inval;
319 np->tclass = val;
320 retv = 0;
321 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900322
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900323 case IPV6_RECVTCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900324 if (optlen < sizeof(int))
325 goto e_inval;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900326 np->rxopt.bits.rxtclass = valbool;
327 retv = 0;
328 break;
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 case IPV6_FLOWINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900331 if (optlen < sizeof(int))
332 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 np->rxopt.bits.rxflow = valbool;
334 retv = 0;
335 break;
336
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900337 case IPV6_HOPOPTS:
338 case IPV6_RTHDRDSTOPTS:
339 case IPV6_RTHDR:
340 case IPV6_DSTOPTS:
341 {
342 struct ipv6_txoptions *opt;
Shan Weiaea74272008-06-19 16:29:39 -0700343
344 /* remove any sticky options header with a zero option
345 * length, per RFC3542.
346 */
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900347 if (optlen == 0)
Luiz Capitulinocb422c42005-11-14 21:43:36 -0800348 optval = NULL;
Yang Hongyangcfb266c2008-08-03 18:16:15 -0700349 else if (optval == NULL)
350 goto e_inval;
Shan Weiaea74272008-06-19 16:29:39 -0700351 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
352 optlen & 0x7 || optlen > 8 * 255)
353 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900354
355 /* hop-by-hop / destination options are privileged option */
356 retv = -EPERM;
357 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
358 break;
359
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900360 opt = ipv6_renew_options(sk, np->opt, optname,
361 (struct ipv6_opt_hdr __user *)optval,
362 optlen);
363 if (IS_ERR(opt)) {
364 retv = PTR_ERR(opt);
365 break;
366 }
367
368 /* routing header option needs extra check */
Olaf Kirchdfee0a72007-03-09 13:55:38 -0800369 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900370 struct ipv6_rt_hdr *rthdr = opt->srcrt;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700371 switch (rthdr->type) {
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700372#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700373 case IPV6_SRCRT_TYPE_2:
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700374 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700375#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700376 default:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900377 goto sticky_done;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700378 }
379
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900380 if ((rthdr->hdrlen & 1) ||
381 (rthdr->hdrlen >> 1) != rthdr->segments_left)
382 goto sticky_done;
383 }
384
385 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700386 opt = ipv6_update_options(sk, opt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900387sticky_done:
388 if (opt)
389 sock_kfree_s(sk, opt, opt->tot_len);
390 break;
391 }
392
393 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 {
395 struct ipv6_txoptions *opt = NULL;
396 struct msghdr msg;
397 struct flowi fl;
398 int junk;
399
400 fl.fl6_flowlabel = 0;
401 fl.oif = sk->sk_bound_dev_if;
402
403 if (optlen == 0)
404 goto update;
405
406 /* 1K is probably excessive
407 * 1K is surely not enough, 2K per standard header is 16K.
408 */
409 retv = -EINVAL;
410 if (optlen > 64*1024)
411 break;
412
413 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
414 retv = -ENOBUFS;
415 if (opt == NULL)
416 break;
417
418 memset(opt, 0, sizeof(*opt));
419 opt->tot_len = sizeof(*opt) + optlen;
420 retv = -EFAULT;
421 if (copy_from_user(opt+1, optval, optlen))
422 goto done;
423
424 msg.msg_controllen = optlen;
425 msg.msg_control = (void*)(opt+1);
426
YOSHIFUJI Hideaki91e19082008-06-04 13:02:49 +0900427 retv = datagram_send_ctl(net, &msg, &fl, opt, &junk, &junk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (retv)
429 goto done;
430update:
431 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700432 opt = ipv6_update_options(sk, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433done:
434 if (opt)
435 sock_kfree_s(sk, opt, opt->tot_len);
436 break;
437 }
438 case IPV6_UNICAST_HOPS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900439 if (optlen < sizeof(int))
440 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (val > 255 || val < -1)
442 goto e_inval;
443 np->hop_limit = val;
444 retv = 0;
445 break;
446
447 case IPV6_MULTICAST_HOPS:
448 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900449 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900450 if (optlen < sizeof(int))
451 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (val > 255 || val < -1)
453 goto e_inval;
454 np->mcast_hops = val;
455 retv = 0;
456 break;
457
458 case IPV6_MULTICAST_LOOP:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900459 if (optlen < sizeof(int))
460 goto e_inval;
YOSHIFUJI Hideaki28d44882008-06-12 03:14:51 +0900461 if (val != valbool)
462 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 np->mc_loop = valbool;
464 retv = 0;
465 break;
466
467 case IPV6_MULTICAST_IF:
468 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900469 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900470 if (optlen < sizeof(int))
471 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Brian Haley4953f0f2007-10-11 14:39:29 -0700473 if (val) {
474 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
475 goto e_inval;
476
Daniel Lezcano8a3edd82008-03-07 11:14:16 -0800477 if (__dev_get_by_index(net, val) == NULL) {
Brian Haley4953f0f2007-10-11 14:39:29 -0700478 retv = -ENODEV;
479 break;
480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482 np->mcast_oif = val;
483 retv = 0;
484 break;
485 case IPV6_ADD_MEMBERSHIP:
486 case IPV6_DROP_MEMBERSHIP:
487 {
488 struct ipv6_mreq mreq;
489
Wang Chena28398b2008-04-07 09:42:07 +0800490 if (optlen < sizeof(struct ipv6_mreq))
491 goto e_inval;
492
Flavio Leitnera96fb492007-08-24 22:16:39 -0700493 retv = -EPROTO;
494 if (inet_sk(sk)->is_icsk)
495 break;
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 retv = -EFAULT;
498 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
499 break;
500
501 if (optname == IPV6_ADD_MEMBERSHIP)
502 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
503 else
504 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
505 break;
506 }
507 case IPV6_JOIN_ANYCAST:
508 case IPV6_LEAVE_ANYCAST:
509 {
510 struct ipv6_mreq mreq;
511
Wang Chena28398b2008-04-07 09:42:07 +0800512 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto e_inval;
514
515 retv = -EFAULT;
516 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
517 break;
518
519 if (optname == IPV6_JOIN_ANYCAST)
520 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
521 else
522 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
523 break;
524 }
525 case MCAST_JOIN_GROUP:
526 case MCAST_LEAVE_GROUP:
527 {
528 struct group_req greq;
529 struct sockaddr_in6 *psin6;
530
Wang Chena28398b2008-04-07 09:42:07 +0800531 if (optlen < sizeof(struct group_req))
532 goto e_inval;
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 retv = -EFAULT;
535 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
536 break;
537 if (greq.gr_group.ss_family != AF_INET6) {
538 retv = -EADDRNOTAVAIL;
539 break;
540 }
541 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
542 if (optname == MCAST_JOIN_GROUP)
543 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
544 &psin6->sin6_addr);
545 else
546 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
547 &psin6->sin6_addr);
548 break;
549 }
550 case MCAST_JOIN_SOURCE_GROUP:
551 case MCAST_LEAVE_SOURCE_GROUP:
552 case MCAST_BLOCK_SOURCE:
553 case MCAST_UNBLOCK_SOURCE:
554 {
555 struct group_source_req greqs;
556 int omode, add;
557
Wang Chena28398b2008-04-07 09:42:07 +0800558 if (optlen < sizeof(struct group_source_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto e_inval;
560 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
561 retv = -EFAULT;
562 break;
563 }
564 if (greqs.gsr_group.ss_family != AF_INET6 ||
565 greqs.gsr_source.ss_family != AF_INET6) {
566 retv = -EADDRNOTAVAIL;
567 break;
568 }
569 if (optname == MCAST_BLOCK_SOURCE) {
570 omode = MCAST_EXCLUDE;
571 add = 1;
572 } else if (optname == MCAST_UNBLOCK_SOURCE) {
573 omode = MCAST_EXCLUDE;
574 add = 0;
575 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
576 struct sockaddr_in6 *psin6;
577
578 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
579 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
580 &psin6->sin6_addr);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700581 /* prior join w/ different source is ok */
582 if (retv && retv != -EADDRINUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 break;
584 omode = MCAST_INCLUDE;
585 add = 1;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700586 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 omode = MCAST_INCLUDE;
588 add = 0;
589 }
590 retv = ip6_mc_source(add, omode, sk, &greqs);
591 break;
592 }
593 case MCAST_MSFILTER:
594 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 extern int sysctl_mld_max_msf;
596 struct group_filter *gsf;
597
598 if (optlen < GROUP_FILTER_SIZE(0))
599 goto e_inval;
600 if (optlen > sysctl_optmem_max) {
601 retv = -ENOBUFS;
602 break;
603 }
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800604 gsf = kmalloc(optlen,GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700605 if (!gsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 retv = -ENOBUFS;
607 break;
608 }
609 retv = -EFAULT;
610 if (copy_from_user(gsf, optval, optlen)) {
611 kfree(gsf);
612 break;
613 }
614 /* numsrc >= (4G-140)/128 overflow in 32 bits */
615 if (gsf->gf_numsrc >= 0x1ffffffU ||
616 gsf->gf_numsrc > sysctl_mld_max_msf) {
617 kfree(gsf);
618 retv = -ENOBUFS;
619 break;
620 }
621 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
622 kfree(gsf);
623 retv = -EINVAL;
624 break;
625 }
626 retv = ip6_mc_msfilter(sk, gsf);
627 kfree(gsf);
628
629 break;
630 }
631 case IPV6_ROUTER_ALERT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900632 if (optlen < sizeof(int))
633 goto e_inval;
Denis V. Lunev725a8ff2008-07-19 00:28:58 -0700634 retv = ip6_ra_control(sk, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 break;
636 case IPV6_MTU_DISCOVER:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900637 if (optlen < sizeof(int))
638 goto e_inval;
John Heffner628a5c52007-04-20 15:53:27 -0700639 if (val<0 || val>3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 goto e_inval;
641 np->pmtudisc = val;
642 retv = 0;
643 break;
644 case IPV6_MTU:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900645 if (optlen < sizeof(int))
646 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (val && val < IPV6_MIN_MTU)
648 goto e_inval;
649 np->frag_size = val;
650 retv = 0;
651 break;
652 case IPV6_RECVERR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900653 if (optlen < sizeof(int))
654 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 np->recverr = valbool;
656 if (!val)
657 skb_queue_purge(&sk->sk_error_queue);
658 retv = 0;
659 break;
660 case IPV6_FLOWINFO_SEND:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900661 if (optlen < sizeof(int))
662 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 np->sndflow = valbool;
664 retv = 0;
665 break;
666 case IPV6_FLOWLABEL_MGR:
667 retv = ipv6_flowlabel_opt(sk, optval, optlen);
668 break;
669 case IPV6_IPSEC_POLICY:
670 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700671 retv = -EPERM;
672 if (!capable(CAP_NET_ADMIN))
673 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 retv = xfrm_user_policy(sk, optname, optval, optlen);
675 break;
676
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900677 case IPV6_ADDR_PREFERENCES:
678 {
679 unsigned int pref = 0;
680 unsigned int prefmask = ~0;
681
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900682 if (optlen < sizeof(int))
683 goto e_inval;
684
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900685 retv = -EINVAL;
686
687 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
688 switch (val & (IPV6_PREFER_SRC_PUBLIC|
689 IPV6_PREFER_SRC_TMP|
690 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
691 case IPV6_PREFER_SRC_PUBLIC:
692 pref |= IPV6_PREFER_SRC_PUBLIC;
693 break;
694 case IPV6_PREFER_SRC_TMP:
695 pref |= IPV6_PREFER_SRC_TMP;
696 break;
697 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
698 break;
699 case 0:
700 goto pref_skip_pubtmp;
701 default:
702 goto e_inval;
703 }
704
705 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
706 IPV6_PREFER_SRC_TMP);
707pref_skip_pubtmp:
708
709 /* check HOME/COA conflicts */
710 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
711 case IPV6_PREFER_SRC_HOME:
712 break;
713 case IPV6_PREFER_SRC_COA:
714 pref |= IPV6_PREFER_SRC_COA;
715 case 0:
716 goto pref_skip_coa;
717 default:
718 goto e_inval;
719 }
720
721 prefmask &= ~IPV6_PREFER_SRC_COA;
722pref_skip_coa:
723
724 /* check CGA/NONCGA conflicts */
725 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
726 case IPV6_PREFER_SRC_CGA:
727 case IPV6_PREFER_SRC_NONCGA:
728 case 0:
729 break;
730 default:
731 goto e_inval;
732 }
733
734 np->srcprefs = (np->srcprefs & prefmask) | pref;
735 retv = 0;
736
737 break;
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 release_sock(sk);
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return retv;
744
745e_inval:
746 release_sock(sk);
747 return -EINVAL;
748}
749
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800750int ipv6_setsockopt(struct sock *sk, int level, int optname,
751 char __user *optval, int optlen)
752{
753 int err;
754
755 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
756 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
757
758 if (level != SOL_IPV6)
759 return -ENOPROTOOPT;
760
761 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
762#ifdef CONFIG_NETFILTER
763 /* we need to exclude all possible ENOPROTOOPTs except default case */
764 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
765 optname != IPV6_XFRM_POLICY) {
766 lock_sock(sk);
767 err = nf_setsockopt(sk, PF_INET6, optname, optval,
768 optlen);
769 release_sock(sk);
770 }
771#endif
772 return err;
773}
774
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900775EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800776
777#ifdef CONFIG_COMPAT
778int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800779 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800780{
781 int err;
782
783 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800784 if (udp_prot.compat_setsockopt != NULL)
785 return udp_prot.compat_setsockopt(sk, level, optname,
786 optval, optlen);
787 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800788 }
789
790 if (level != SOL_IPV6)
791 return -ENOPROTOOPT;
792
David L Stevensdae50292008-04-27 01:06:07 -0700793 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
794 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
795 ipv6_setsockopt);
796
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800797 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
798#ifdef CONFIG_NETFILTER
799 /* we need to exclude all possible ENOPROTOOPTs except default case */
800 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800801 optname != IPV6_XFRM_POLICY) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800802 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800803 err = compat_nf_setsockopt(sk, PF_INET6, optname,
804 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800805 release_sock(sk);
806 }
807#endif
808 return err;
809}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800810
811EXPORT_SYMBOL(compat_ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800812#endif
813
David S. Miller286930792007-03-07 12:50:46 -0800814static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700815 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900816{
David S. Miller286930792007-03-07 12:50:46 -0800817 struct ipv6_opt_hdr *hdr;
818
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700819 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900820 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700821
822 switch(optname) {
823 case IPV6_HOPOPTS:
824 hdr = opt->hopopt;
825 break;
826 case IPV6_RTHDRDSTOPTS:
827 hdr = opt->dst0opt;
828 break;
829 case IPV6_RTHDR:
830 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
831 break;
832 case IPV6_DSTOPTS:
833 hdr = opt->dst1opt;
834 break;
835 default:
836 return -EINVAL; /* should not happen */
837 }
838
839 if (!hdr)
840 return 0;
David S. Miller286930792007-03-07 12:50:46 -0800841
Chris Wrightd2b02ed2007-03-09 16:19:17 -0800842 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -0700843 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900844 return -EFAULT;
Yang Hongyang95b496b2008-05-28 16:27:28 +0800845 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900846}
847
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800848static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 char __user *optval, int __user *optlen)
850{
851 struct ipv6_pinfo *np = inet6_sk(sk);
852 int len;
853 int val;
854
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900855 if (ip6_mroute_opt(optname))
856 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (get_user(len, optlen))
859 return -EFAULT;
860 switch (optname) {
861 case IPV6_ADDRFORM:
862 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800863 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 sk->sk_protocol != IPPROTO_TCP)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900865 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (sk->sk_state != TCP_ESTABLISHED)
867 return -ENOTCONN;
868 val = sk->sk_family;
869 break;
870 case MCAST_MSFILTER:
871 {
872 struct group_filter gsf;
873 int err;
874
875 if (len < GROUP_FILTER_SIZE(0))
876 return -EINVAL;
877 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
878 return -EFAULT;
YOSHIFUJI Hideaki20c61fb2008-04-28 14:40:55 +0900879 if (gsf.gf_group.ss_family != AF_INET6)
880 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 lock_sock(sk);
882 err = ip6_mc_msfget(sk, &gsf,
883 (struct group_filter __user *)optval, optlen);
884 release_sock(sk);
885 return err;
886 }
887
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900888 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 {
890 struct msghdr msg;
891 struct sk_buff *skb;
892
893 if (sk->sk_type != SOCK_STREAM)
894 return -ENOPROTOOPT;
895
896 msg.msg_control = optval;
897 msg.msg_controllen = len;
898 msg.msg_flags = 0;
899
900 lock_sock(sk);
901 skb = np->pktoptions;
902 if (skb)
903 atomic_inc(&skb->users);
904 release_sock(sk);
905
906 if (skb) {
907 int err = datagram_recv_ctl(sk, &msg, skb);
908 kfree_skb(skb);
909 if (err)
910 return err;
911 } else {
912 if (np->rxopt.bits.rxinfo) {
913 struct in6_pktinfo src_info;
Yang Hongyang13601cd2008-08-17 23:21:52 -0700914 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif : sk->sk_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
916 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
917 }
918 if (np->rxopt.bits.rxhlim) {
919 int hlim = np->mcast_hops;
920 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
921 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900922 if (np->rxopt.bits.rxoinfo) {
923 struct in6_pktinfo src_info;
Yang Hongyang13601cd2008-08-17 23:21:52 -0700924 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif : sk->sk_bound_dev_if;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900925 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
926 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
927 }
928 if (np->rxopt.bits.rxohlim) {
929 int hlim = np->mcast_hops;
930 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933 len -= msg.msg_controllen;
934 return put_user(len, optlen);
935 }
936 case IPV6_MTU:
937 {
938 struct dst_entry *dst;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900939 val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 lock_sock(sk);
941 dst = sk_dst_get(sk);
942 if (dst) {
943 val = dst_mtu(dst);
944 dst_release(dst);
945 }
946 release_sock(sk);
947 if (!val)
948 return -ENOTCONN;
949 break;
950 }
951
952 case IPV6_V6ONLY:
953 val = np->ipv6only;
954 break;
955
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900956 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 val = np->rxopt.bits.rxinfo;
958 break;
959
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900960 case IPV6_2292PKTINFO:
961 val = np->rxopt.bits.rxoinfo;
962 break;
963
964 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 val = np->rxopt.bits.rxhlim;
966 break;
967
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900968 case IPV6_2292HOPLIMIT:
969 val = np->rxopt.bits.rxohlim;
970 break;
971
972 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 val = np->rxopt.bits.srcrt;
974 break;
975
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900976 case IPV6_2292RTHDR:
977 val = np->rxopt.bits.osrcrt;
978 break;
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900981 case IPV6_RTHDRDSTOPTS:
982 case IPV6_RTHDR:
983 case IPV6_DSTOPTS:
984 {
985
986 lock_sock(sk);
David S. Miller286930792007-03-07 12:50:46 -0800987 len = ipv6_getsockopt_sticky(sk, np->opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700988 optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900989 release_sock(sk);
Yang Hongyang05335c22008-05-28 16:23:47 +0800990 /* check if ipv6_getsockopt_sticky() returns err code */
991 if (len < 0)
992 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900993 return put_user(len, optlen);
994 }
995
996 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 val = np->rxopt.bits.hopopts;
998 break;
999
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001000 case IPV6_2292HOPOPTS:
1001 val = np->rxopt.bits.ohopopts;
1002 break;
1003
1004 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 val = np->rxopt.bits.dstopts;
1006 break;
1007
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001008 case IPV6_2292DSTOPTS:
1009 val = np->rxopt.bits.odstopts;
1010 break;
1011
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001012 case IPV6_TCLASS:
1013 val = np->tclass;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -07001014 if (val < 0)
1015 val = 0;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001016 break;
1017
1018 case IPV6_RECVTCLASS:
1019 val = np->rxopt.bits.rxtclass;
1020 break;
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 case IPV6_FLOWINFO:
1023 val = np->rxopt.bits.rxflow;
1024 break;
1025
1026 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -08001028 {
1029 struct dst_entry *dst;
1030
1031 if (optname == IPV6_UNICAST_HOPS)
1032 val = np->hop_limit;
1033 else
1034 val = np->mcast_hops;
1035
1036 dst = sk_dst_get(sk);
1037 if (dst) {
1038 if (val < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001039 val = ip6_dst_hoplimit(dst);
Brian Haleybefffe92006-12-12 17:09:49 -08001040 dst_release(dst);
1041 }
1042 if (val < 0)
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001043 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 break;
Brian Haleybefffe92006-12-12 17:09:49 -08001045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 case IPV6_MULTICAST_LOOP:
1048 val = np->mc_loop;
1049 break;
1050
1051 case IPV6_MULTICAST_IF:
1052 val = np->mcast_oif;
1053 break;
1054
1055 case IPV6_MTU_DISCOVER:
1056 val = np->pmtudisc;
1057 break;
1058
1059 case IPV6_RECVERR:
1060 val = np->recverr;
1061 break;
1062
1063 case IPV6_FLOWINFO_SEND:
1064 val = np->sndflow;
1065 break;
1066
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001067 case IPV6_ADDR_PREFERENCES:
1068 val = 0;
1069
1070 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1071 val |= IPV6_PREFER_SRC_TMP;
1072 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1073 val |= IPV6_PREFER_SRC_PUBLIC;
1074 else {
1075 /* XXX: should we return system default? */
1076 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1077 }
1078
1079 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1080 val |= IPV6_PREFER_SRC_COA;
1081 else
1082 val |= IPV6_PREFER_SRC_HOME;
1083 break;
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001086 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088 len = min_t(unsigned int, sizeof(int), len);
1089 if(put_user(len, optlen))
1090 return -EFAULT;
1091 if(copy_to_user(optval,&val,len))
1092 return -EFAULT;
1093 return 0;
1094}
1095
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001096int ipv6_getsockopt(struct sock *sk, int level, int optname,
1097 char __user *optval, int __user *optlen)
1098{
1099 int err;
1100
1101 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1102 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1103
1104 if(level != SOL_IPV6)
1105 return -ENOPROTOOPT;
1106
1107 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1108#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001109 /* we need to exclude all possible ENOPROTOOPTs except default case */
1110 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001111 int len;
1112
1113 if (get_user(len, optlen))
1114 return -EFAULT;
1115
1116 lock_sock(sk);
1117 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1118 &len);
1119 release_sock(sk);
1120 if (err >= 0)
1121 err = put_user(len, optlen);
1122 }
1123#endif
1124 return err;
1125}
1126
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001127EXPORT_SYMBOL(ipv6_getsockopt);
1128
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001129#ifdef CONFIG_COMPAT
1130int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001131 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001132{
1133 int err;
1134
1135 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001136 if (udp_prot.compat_getsockopt != NULL)
1137 return udp_prot.compat_getsockopt(sk, level, optname,
1138 optval, optlen);
1139 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001140 }
1141
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001142 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001143 return -ENOPROTOOPT;
1144
Pavel Emelyanov80991792008-04-30 14:49:54 -07001145 if (optname == MCAST_MSFILTER)
1146 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1147 ipv6_getsockopt);
1148
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001149 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1150#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001151 /* we need to exclude all possible ENOPROTOOPTs except default case */
1152 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001153 int len;
1154
1155 if (get_user(len, optlen))
1156 return -EFAULT;
1157
1158 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001159 err = compat_nf_getsockopt(sk, PF_INET6,
1160 optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001161 release_sock(sk);
1162 if (err >= 0)
1163 err = put_user(len, optlen);
1164 }
1165#endif
1166 return err;
1167}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001168
1169EXPORT_SYMBOL(compat_ipv6_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001170#endif
1171