blob: 06de9d0e1f6b653384279180612b2b0b69df1f66 [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>
55
56#include <asm/uaccess.h>
57
Eric Dumazetba899662005-08-26 12:05:31 -070058DEFINE_SNMP_STAT(struct ipstats_mib, ipv6_statistics) __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060struct ip6_ra_chain *ip6_ra_chain;
61DEFINE_RWLOCK(ip6_ra_lock);
62
63int ip6_ra_control(struct sock *sk, int sel, void (*destructor)(struct sock *))
64{
65 struct ip6_ra_chain *ra, *new_ra, **rap;
66
67 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
68 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num != IPPROTO_RAW)
69 return -EINVAL;
70
71 new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
72
73 write_lock_bh(&ip6_ra_lock);
74 for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
75 if (ra->sk == sk) {
76 if (sel>=0) {
77 write_unlock_bh(&ip6_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -080078 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return -EADDRINUSE;
80 }
81
82 *rap = ra->next;
83 write_unlock_bh(&ip6_ra_lock);
84
85 if (ra->destructor)
86 ra->destructor(sk);
87 sock_put(sk);
88 kfree(ra);
89 return 0;
90 }
91 }
92 if (new_ra == NULL) {
93 write_unlock_bh(&ip6_ra_lock);
94 return -ENOBUFS;
95 }
96 new_ra->sk = sk;
97 new_ra->sel = sel;
98 new_ra->destructor = destructor;
99 new_ra->next = ra;
100 *rap = new_ra;
101 sock_hold(sk);
102 write_unlock_bh(&ip6_ra_lock);
103 return 0;
104}
105
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700106static
107struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
108 struct ipv6_txoptions *opt)
109{
110 if (inet_sk(sk)->is_icsk) {
111 if (opt &&
112 !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
113 inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
114 struct inet_connection_sock *icsk = inet_csk(sk);
115 icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
116 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
117 }
118 opt = xchg(&inet6_sk(sk)->opt, opt);
119 } else {
120 write_lock(&sk->sk_dst_lock);
121 opt = xchg(&inet6_sk(sk)->opt, opt);
122 write_unlock(&sk->sk_dst_lock);
123 }
124 sk_dst_reset(sk);
125
126 return opt;
127}
128
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800129static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 char __user *optval, int optlen)
131{
132 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900133 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int val, valbool;
135 int retv = -ENOPROTOOPT;
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (optval == NULL)
138 val=0;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900139 else {
140 if (optlen >= sizeof(int)) {
141 if (get_user(val, (int __user *) optval))
142 return -EFAULT;
143 } else
144 val = 0;
145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 valbool = (val!=0);
148
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900149 if (ip6_mroute_opt(optname))
150 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 lock_sock(sk);
153
154 switch (optname) {
155
156 case IPV6_ADDRFORM:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900157 if (optlen < sizeof(int))
158 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (val == PF_INET) {
160 struct ipv6_txoptions *opt;
161 struct sk_buff *pktopt;
162
163 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800164 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 sk->sk_protocol != IPPROTO_TCP)
166 break;
167
168 if (sk->sk_state != TCP_ESTABLISHED) {
169 retv = -ENOTCONN;
170 break;
171 }
172
173 if (ipv6_only_sock(sk) ||
Brian Haleye773e4f2007-08-24 23:16:08 -0700174 !ipv6_addr_v4mapped(&np->daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 retv = -EADDRNOTAVAIL;
176 break;
177 }
178
179 fl6_free_socklist(sk);
180 ipv6_sock_mc_close(sk);
181
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700182 /*
183 * Sock is moving from IPv6 to IPv4 (sk_prot), so
184 * remove it from the refcnt debug socks count in the
185 * original family...
186 */
187 sk_refcnt_debug_dec(sk);
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (sk->sk_protocol == IPPROTO_TCP) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800190 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700192 sock_prot_inuse_add(net, sk->sk_prot, -1);
193 sock_prot_inuse_add(net, &tcp_prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 local_bh_enable();
195 sk->sk_prot = &tcp_prot;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800196 icsk->icsk_af_ops = &ipv4_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 sk->sk_socket->ops = &inet_stream_ops;
198 sk->sk_family = PF_INET;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800199 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 } else {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800201 struct proto *prot = &udp_prot;
202
David S. Millerdb8dac22008-03-06 16:22:02 -0800203 if (sk->sk_protocol == IPPROTO_UDPLITE)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800204 prot = &udplite_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700206 sock_prot_inuse_add(net, sk->sk_prot, -1);
207 sock_prot_inuse_add(net, prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 local_bh_enable();
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800209 sk->sk_prot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 sk->sk_socket->ops = &inet_dgram_ops;
211 sk->sk_family = PF_INET;
212 }
213 opt = xchg(&np->opt, NULL);
214 if (opt)
215 sock_kfree_s(sk, opt, opt->tot_len);
216 pktopt = xchg(&np->pktoptions, NULL);
217 if (pktopt)
218 kfree_skb(pktopt);
219
220 sk->sk_destruct = inet_sock_destruct;
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700221 /*
222 * ... and add it to the refcnt debug socks count
223 * in the new family. -acme
224 */
225 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 module_put(THIS_MODULE);
227 retv = 0;
228 break;
229 }
230 goto e_inval;
231
232 case IPV6_V6ONLY:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900233 if (optlen < sizeof(int) ||
234 inet_sk(sk)->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 goto e_inval;
236 np->ipv6only = valbool;
237 retv = 0;
238 break;
239
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900240 case IPV6_RECVPKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900241 if (optlen < sizeof(int))
242 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 np->rxopt.bits.rxinfo = valbool;
244 retv = 0;
245 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900246
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900247 case IPV6_2292PKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900248 if (optlen < sizeof(int))
249 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900250 np->rxopt.bits.rxoinfo = valbool;
251 retv = 0;
252 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900254 case IPV6_RECVHOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900255 if (optlen < sizeof(int))
256 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 np->rxopt.bits.rxhlim = valbool;
258 retv = 0;
259 break;
260
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900261 case IPV6_2292HOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900262 if (optlen < sizeof(int))
263 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900264 np->rxopt.bits.rxohlim = valbool;
265 retv = 0;
266 break;
267
268 case IPV6_RECVRTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900269 if (optlen < sizeof(int))
270 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900271 np->rxopt.bits.srcrt = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 retv = 0;
273 break;
274
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900275 case IPV6_2292RTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900276 if (optlen < sizeof(int))
277 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900278 np->rxopt.bits.osrcrt = valbool;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900279 retv = 0;
280 break;
281
282 case IPV6_RECVHOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900283 if (optlen < sizeof(int))
284 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 np->rxopt.bits.hopopts = valbool;
286 retv = 0;
287 break;
288
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900289 case IPV6_2292HOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900290 if (optlen < sizeof(int))
291 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900292 np->rxopt.bits.ohopopts = valbool;
293 retv = 0;
294 break;
295
296 case IPV6_RECVDSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900297 if (optlen < sizeof(int))
298 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 np->rxopt.bits.dstopts = valbool;
300 retv = 0;
301 break;
302
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900303 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900304 if (optlen < sizeof(int))
305 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900306 np->rxopt.bits.odstopts = valbool;
307 retv = 0;
308 break;
309
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900310 case IPV6_TCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900311 if (optlen < sizeof(int))
312 goto e_inval;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700313 if (val < -1 || val > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900314 goto e_inval;
315 np->tclass = val;
316 retv = 0;
317 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900318
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900319 case IPV6_RECVTCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900320 if (optlen < sizeof(int))
321 goto e_inval;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900322 np->rxopt.bits.rxtclass = valbool;
323 retv = 0;
324 break;
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 case IPV6_FLOWINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900327 if (optlen < sizeof(int))
328 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 np->rxopt.bits.rxflow = valbool;
330 retv = 0;
331 break;
332
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900333 case IPV6_HOPOPTS:
334 case IPV6_RTHDRDSTOPTS:
335 case IPV6_RTHDR:
336 case IPV6_DSTOPTS:
337 {
338 struct ipv6_txoptions *opt;
339 if (optlen == 0)
Luiz Capitulinocb422c42005-11-14 21:43:36 -0800340 optval = NULL;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900341
342 /* hop-by-hop / destination options are privileged option */
343 retv = -EPERM;
344 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
345 break;
346
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900347 if (optlen < sizeof(struct ipv6_opt_hdr) ||
348 optlen & 0x7 || optlen > 8 * 255)
349 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900350
351 opt = ipv6_renew_options(sk, np->opt, optname,
352 (struct ipv6_opt_hdr __user *)optval,
353 optlen);
354 if (IS_ERR(opt)) {
355 retv = PTR_ERR(opt);
356 break;
357 }
358
359 /* routing header option needs extra check */
Olaf Kirchdfee0a72007-03-09 13:55:38 -0800360 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900361 struct ipv6_rt_hdr *rthdr = opt->srcrt;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700362 switch (rthdr->type) {
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700363#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700364 case IPV6_SRCRT_TYPE_2:
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700365 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700366#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700367 default:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900368 goto sticky_done;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700369 }
370
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900371 if ((rthdr->hdrlen & 1) ||
372 (rthdr->hdrlen >> 1) != rthdr->segments_left)
373 goto sticky_done;
374 }
375
376 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700377 opt = ipv6_update_options(sk, opt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900378sticky_done:
379 if (opt)
380 sock_kfree_s(sk, opt, opt->tot_len);
381 break;
382 }
383
384 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 {
386 struct ipv6_txoptions *opt = NULL;
387 struct msghdr msg;
388 struct flowi fl;
389 int junk;
390
391 fl.fl6_flowlabel = 0;
392 fl.oif = sk->sk_bound_dev_if;
393
394 if (optlen == 0)
395 goto update;
396
397 /* 1K is probably excessive
398 * 1K is surely not enough, 2K per standard header is 16K.
399 */
400 retv = -EINVAL;
401 if (optlen > 64*1024)
402 break;
403
404 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
405 retv = -ENOBUFS;
406 if (opt == NULL)
407 break;
408
409 memset(opt, 0, sizeof(*opt));
410 opt->tot_len = sizeof(*opt) + optlen;
411 retv = -EFAULT;
412 if (copy_from_user(opt+1, optval, optlen))
413 goto done;
414
415 msg.msg_controllen = optlen;
416 msg.msg_control = (void*)(opt+1);
417
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900418 retv = datagram_send_ctl(&msg, &fl, opt, &junk, &junk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (retv)
420 goto done;
421update:
422 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700423 opt = ipv6_update_options(sk, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424done:
425 if (opt)
426 sock_kfree_s(sk, opt, opt->tot_len);
427 break;
428 }
429 case IPV6_UNICAST_HOPS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900430 if (optlen < sizeof(int))
431 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (val > 255 || val < -1)
433 goto e_inval;
434 np->hop_limit = val;
435 retv = 0;
436 break;
437
438 case IPV6_MULTICAST_HOPS:
439 if (sk->sk_type == SOCK_STREAM)
440 goto e_inval;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900441 if (optlen < sizeof(int))
442 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (val > 255 || val < -1)
444 goto e_inval;
445 np->mcast_hops = val;
446 retv = 0;
447 break;
448
449 case IPV6_MULTICAST_LOOP:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900450 if (optlen < sizeof(int))
451 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 np->mc_loop = valbool;
453 retv = 0;
454 break;
455
456 case IPV6_MULTICAST_IF:
457 if (sk->sk_type == SOCK_STREAM)
458 goto e_inval;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900459 if (optlen < sizeof(int))
460 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Brian Haley4953f0f2007-10-11 14:39:29 -0700462 if (val) {
463 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
464 goto e_inval;
465
Daniel Lezcano8a3edd82008-03-07 11:14:16 -0800466 if (__dev_get_by_index(net, val) == NULL) {
Brian Haley4953f0f2007-10-11 14:39:29 -0700467 retv = -ENODEV;
468 break;
469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471 np->mcast_oif = val;
472 retv = 0;
473 break;
474 case IPV6_ADD_MEMBERSHIP:
475 case IPV6_DROP_MEMBERSHIP:
476 {
477 struct ipv6_mreq mreq;
478
Wang Chena28398b2008-04-07 09:42:07 +0800479 if (optlen < sizeof(struct ipv6_mreq))
480 goto e_inval;
481
Flavio Leitnera96fb492007-08-24 22:16:39 -0700482 retv = -EPROTO;
483 if (inet_sk(sk)->is_icsk)
484 break;
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 retv = -EFAULT;
487 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
488 break;
489
490 if (optname == IPV6_ADD_MEMBERSHIP)
491 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
492 else
493 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
494 break;
495 }
496 case IPV6_JOIN_ANYCAST:
497 case IPV6_LEAVE_ANYCAST:
498 {
499 struct ipv6_mreq mreq;
500
Wang Chena28398b2008-04-07 09:42:07 +0800501 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 goto e_inval;
503
504 retv = -EFAULT;
505 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
506 break;
507
508 if (optname == IPV6_JOIN_ANYCAST)
509 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
510 else
511 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
512 break;
513 }
514 case MCAST_JOIN_GROUP:
515 case MCAST_LEAVE_GROUP:
516 {
517 struct group_req greq;
518 struct sockaddr_in6 *psin6;
519
Wang Chena28398b2008-04-07 09:42:07 +0800520 if (optlen < sizeof(struct group_req))
521 goto e_inval;
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 retv = -EFAULT;
524 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
525 break;
526 if (greq.gr_group.ss_family != AF_INET6) {
527 retv = -EADDRNOTAVAIL;
528 break;
529 }
530 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
531 if (optname == MCAST_JOIN_GROUP)
532 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
533 &psin6->sin6_addr);
534 else
535 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
536 &psin6->sin6_addr);
537 break;
538 }
539 case MCAST_JOIN_SOURCE_GROUP:
540 case MCAST_LEAVE_SOURCE_GROUP:
541 case MCAST_BLOCK_SOURCE:
542 case MCAST_UNBLOCK_SOURCE:
543 {
544 struct group_source_req greqs;
545 int omode, add;
546
Wang Chena28398b2008-04-07 09:42:07 +0800547 if (optlen < sizeof(struct group_source_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 goto e_inval;
549 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
550 retv = -EFAULT;
551 break;
552 }
553 if (greqs.gsr_group.ss_family != AF_INET6 ||
554 greqs.gsr_source.ss_family != AF_INET6) {
555 retv = -EADDRNOTAVAIL;
556 break;
557 }
558 if (optname == MCAST_BLOCK_SOURCE) {
559 omode = MCAST_EXCLUDE;
560 add = 1;
561 } else if (optname == MCAST_UNBLOCK_SOURCE) {
562 omode = MCAST_EXCLUDE;
563 add = 0;
564 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
565 struct sockaddr_in6 *psin6;
566
567 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
568 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
569 &psin6->sin6_addr);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700570 /* prior join w/ different source is ok */
571 if (retv && retv != -EADDRINUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 break;
573 omode = MCAST_INCLUDE;
574 add = 1;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700575 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 omode = MCAST_INCLUDE;
577 add = 0;
578 }
579 retv = ip6_mc_source(add, omode, sk, &greqs);
580 break;
581 }
582 case MCAST_MSFILTER:
583 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 extern int sysctl_mld_max_msf;
585 struct group_filter *gsf;
586
587 if (optlen < GROUP_FILTER_SIZE(0))
588 goto e_inval;
589 if (optlen > sysctl_optmem_max) {
590 retv = -ENOBUFS;
591 break;
592 }
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800593 gsf = kmalloc(optlen,GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700594 if (!gsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 retv = -ENOBUFS;
596 break;
597 }
598 retv = -EFAULT;
599 if (copy_from_user(gsf, optval, optlen)) {
600 kfree(gsf);
601 break;
602 }
603 /* numsrc >= (4G-140)/128 overflow in 32 bits */
604 if (gsf->gf_numsrc >= 0x1ffffffU ||
605 gsf->gf_numsrc > sysctl_mld_max_msf) {
606 kfree(gsf);
607 retv = -ENOBUFS;
608 break;
609 }
610 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
611 kfree(gsf);
612 retv = -EINVAL;
613 break;
614 }
615 retv = ip6_mc_msfilter(sk, gsf);
616 kfree(gsf);
617
618 break;
619 }
620 case IPV6_ROUTER_ALERT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900621 if (optlen < sizeof(int))
622 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 retv = ip6_ra_control(sk, val, NULL);
624 break;
625 case IPV6_MTU_DISCOVER:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900626 if (optlen < sizeof(int))
627 goto e_inval;
John Heffner628a5c52007-04-20 15:53:27 -0700628 if (val<0 || val>3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 goto e_inval;
630 np->pmtudisc = val;
631 retv = 0;
632 break;
633 case IPV6_MTU:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900634 if (optlen < sizeof(int))
635 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (val && val < IPV6_MIN_MTU)
637 goto e_inval;
638 np->frag_size = val;
639 retv = 0;
640 break;
641 case IPV6_RECVERR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900642 if (optlen < sizeof(int))
643 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 np->recverr = valbool;
645 if (!val)
646 skb_queue_purge(&sk->sk_error_queue);
647 retv = 0;
648 break;
649 case IPV6_FLOWINFO_SEND:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900650 if (optlen < sizeof(int))
651 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 np->sndflow = valbool;
653 retv = 0;
654 break;
655 case IPV6_FLOWLABEL_MGR:
656 retv = ipv6_flowlabel_opt(sk, optval, optlen);
657 break;
658 case IPV6_IPSEC_POLICY:
659 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700660 retv = -EPERM;
661 if (!capable(CAP_NET_ADMIN))
662 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 retv = xfrm_user_policy(sk, optname, optval, optlen);
664 break;
665
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900666 case IPV6_ADDR_PREFERENCES:
667 {
668 unsigned int pref = 0;
669 unsigned int prefmask = ~0;
670
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900671 if (optlen < sizeof(int))
672 goto e_inval;
673
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900674 retv = -EINVAL;
675
676 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
677 switch (val & (IPV6_PREFER_SRC_PUBLIC|
678 IPV6_PREFER_SRC_TMP|
679 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
680 case IPV6_PREFER_SRC_PUBLIC:
681 pref |= IPV6_PREFER_SRC_PUBLIC;
682 break;
683 case IPV6_PREFER_SRC_TMP:
684 pref |= IPV6_PREFER_SRC_TMP;
685 break;
686 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
687 break;
688 case 0:
689 goto pref_skip_pubtmp;
690 default:
691 goto e_inval;
692 }
693
694 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
695 IPV6_PREFER_SRC_TMP);
696pref_skip_pubtmp:
697
698 /* check HOME/COA conflicts */
699 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
700 case IPV6_PREFER_SRC_HOME:
701 break;
702 case IPV6_PREFER_SRC_COA:
703 pref |= IPV6_PREFER_SRC_COA;
704 case 0:
705 goto pref_skip_coa;
706 default:
707 goto e_inval;
708 }
709
710 prefmask &= ~IPV6_PREFER_SRC_COA;
711pref_skip_coa:
712
713 /* check CGA/NONCGA conflicts */
714 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
715 case IPV6_PREFER_SRC_CGA:
716 case IPV6_PREFER_SRC_NONCGA:
717 case 0:
718 break;
719 default:
720 goto e_inval;
721 }
722
723 np->srcprefs = (np->srcprefs & prefmask) | pref;
724 retv = 0;
725
726 break;
727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 release_sock(sk);
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return retv;
733
734e_inval:
735 release_sock(sk);
736 return -EINVAL;
737}
738
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800739int ipv6_setsockopt(struct sock *sk, int level, int optname,
740 char __user *optval, int optlen)
741{
742 int err;
743
744 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
745 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
746
747 if (level != SOL_IPV6)
748 return -ENOPROTOOPT;
749
750 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
751#ifdef CONFIG_NETFILTER
752 /* we need to exclude all possible ENOPROTOOPTs except default case */
753 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
754 optname != IPV6_XFRM_POLICY) {
755 lock_sock(sk);
756 err = nf_setsockopt(sk, PF_INET6, optname, optval,
757 optlen);
758 release_sock(sk);
759 }
760#endif
761 return err;
762}
763
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900764EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800765
766#ifdef CONFIG_COMPAT
767int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800768 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800769{
770 int err;
771
772 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800773 if (udp_prot.compat_setsockopt != NULL)
774 return udp_prot.compat_setsockopt(sk, level, optname,
775 optval, optlen);
776 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800777 }
778
779 if (level != SOL_IPV6)
780 return -ENOPROTOOPT;
781
782 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
783#ifdef CONFIG_NETFILTER
784 /* we need to exclude all possible ENOPROTOOPTs except default case */
785 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800786 optname != IPV6_XFRM_POLICY) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800787 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800788 err = compat_nf_setsockopt(sk, PF_INET6, optname,
789 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800790 release_sock(sk);
791 }
792#endif
793 return err;
794}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800795
796EXPORT_SYMBOL(compat_ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800797#endif
798
David S. Miller286930792007-03-07 12:50:46 -0800799static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700800 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900801{
David S. Miller286930792007-03-07 12:50:46 -0800802 struct ipv6_opt_hdr *hdr;
803
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700804 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900805 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700806
807 switch(optname) {
808 case IPV6_HOPOPTS:
809 hdr = opt->hopopt;
810 break;
811 case IPV6_RTHDRDSTOPTS:
812 hdr = opt->dst0opt;
813 break;
814 case IPV6_RTHDR:
815 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
816 break;
817 case IPV6_DSTOPTS:
818 hdr = opt->dst1opt;
819 break;
820 default:
821 return -EINVAL; /* should not happen */
822 }
823
824 if (!hdr)
825 return 0;
David S. Miller286930792007-03-07 12:50:46 -0800826
Chris Wrightd2b02ed2007-03-09 16:19:17 -0800827 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -0700828 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900829 return -EFAULT;
YOSHIFUJI Hideakie5268f12007-03-08 20:48:23 -0800830 return ipv6_optlen(hdr);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900831}
832
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800833static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 char __user *optval, int __user *optlen)
835{
836 struct ipv6_pinfo *np = inet6_sk(sk);
837 int len;
838 int val;
839
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900840 if (ip6_mroute_opt(optname))
841 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (get_user(len, optlen))
844 return -EFAULT;
845 switch (optname) {
846 case IPV6_ADDRFORM:
847 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800848 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 sk->sk_protocol != IPPROTO_TCP)
850 return -EINVAL;
851 if (sk->sk_state != TCP_ESTABLISHED)
852 return -ENOTCONN;
853 val = sk->sk_family;
854 break;
855 case MCAST_MSFILTER:
856 {
857 struct group_filter gsf;
858 int err;
859
860 if (len < GROUP_FILTER_SIZE(0))
861 return -EINVAL;
862 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
863 return -EFAULT;
864 lock_sock(sk);
865 err = ip6_mc_msfget(sk, &gsf,
866 (struct group_filter __user *)optval, optlen);
867 release_sock(sk);
868 return err;
869 }
870
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900871 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 {
873 struct msghdr msg;
874 struct sk_buff *skb;
875
876 if (sk->sk_type != SOCK_STREAM)
877 return -ENOPROTOOPT;
878
879 msg.msg_control = optval;
880 msg.msg_controllen = len;
881 msg.msg_flags = 0;
882
883 lock_sock(sk);
884 skb = np->pktoptions;
885 if (skb)
886 atomic_inc(&skb->users);
887 release_sock(sk);
888
889 if (skb) {
890 int err = datagram_recv_ctl(sk, &msg, skb);
891 kfree_skb(skb);
892 if (err)
893 return err;
894 } else {
895 if (np->rxopt.bits.rxinfo) {
896 struct in6_pktinfo src_info;
897 src_info.ipi6_ifindex = np->mcast_oif;
898 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
899 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
900 }
901 if (np->rxopt.bits.rxhlim) {
902 int hlim = np->mcast_hops;
903 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
904 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900905 if (np->rxopt.bits.rxoinfo) {
906 struct in6_pktinfo src_info;
907 src_info.ipi6_ifindex = np->mcast_oif;
908 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
909 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
910 }
911 if (np->rxopt.bits.rxohlim) {
912 int hlim = np->mcast_hops;
913 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916 len -= msg.msg_controllen;
917 return put_user(len, optlen);
918 }
919 case IPV6_MTU:
920 {
921 struct dst_entry *dst;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900922 val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 lock_sock(sk);
924 dst = sk_dst_get(sk);
925 if (dst) {
926 val = dst_mtu(dst);
927 dst_release(dst);
928 }
929 release_sock(sk);
930 if (!val)
931 return -ENOTCONN;
932 break;
933 }
934
935 case IPV6_V6ONLY:
936 val = np->ipv6only;
937 break;
938
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900939 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 val = np->rxopt.bits.rxinfo;
941 break;
942
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900943 case IPV6_2292PKTINFO:
944 val = np->rxopt.bits.rxoinfo;
945 break;
946
947 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 val = np->rxopt.bits.rxhlim;
949 break;
950
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900951 case IPV6_2292HOPLIMIT:
952 val = np->rxopt.bits.rxohlim;
953 break;
954
955 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 val = np->rxopt.bits.srcrt;
957 break;
958
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900959 case IPV6_2292RTHDR:
960 val = np->rxopt.bits.osrcrt;
961 break;
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900964 case IPV6_RTHDRDSTOPTS:
965 case IPV6_RTHDR:
966 case IPV6_DSTOPTS:
967 {
968
969 lock_sock(sk);
David S. Miller286930792007-03-07 12:50:46 -0800970 len = ipv6_getsockopt_sticky(sk, np->opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700971 optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900972 release_sock(sk);
973 return put_user(len, optlen);
974 }
975
976 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 val = np->rxopt.bits.hopopts;
978 break;
979
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900980 case IPV6_2292HOPOPTS:
981 val = np->rxopt.bits.ohopopts;
982 break;
983
984 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 val = np->rxopt.bits.dstopts;
986 break;
987
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900988 case IPV6_2292DSTOPTS:
989 val = np->rxopt.bits.odstopts;
990 break;
991
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900992 case IPV6_TCLASS:
993 val = np->tclass;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700994 if (val < 0)
995 val = 0;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900996 break;
997
998 case IPV6_RECVTCLASS:
999 val = np->rxopt.bits.rxtclass;
1000 break;
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 case IPV6_FLOWINFO:
1003 val = np->rxopt.bits.rxflow;
1004 break;
1005
1006 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -08001008 {
1009 struct dst_entry *dst;
1010
1011 if (optname == IPV6_UNICAST_HOPS)
1012 val = np->hop_limit;
1013 else
1014 val = np->mcast_hops;
1015
1016 dst = sk_dst_get(sk);
1017 if (dst) {
1018 if (val < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001019 val = ip6_dst_hoplimit(dst);
Brian Haleybefffe92006-12-12 17:09:49 -08001020 dst_release(dst);
1021 }
1022 if (val < 0)
1023 val = ipv6_devconf.hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 break;
Brian Haleybefffe92006-12-12 17:09:49 -08001025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 case IPV6_MULTICAST_LOOP:
1028 val = np->mc_loop;
1029 break;
1030
1031 case IPV6_MULTICAST_IF:
1032 val = np->mcast_oif;
1033 break;
1034
1035 case IPV6_MTU_DISCOVER:
1036 val = np->pmtudisc;
1037 break;
1038
1039 case IPV6_RECVERR:
1040 val = np->recverr;
1041 break;
1042
1043 case IPV6_FLOWINFO_SEND:
1044 val = np->sndflow;
1045 break;
1046
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001047 case IPV6_ADDR_PREFERENCES:
1048 val = 0;
1049
1050 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1051 val |= IPV6_PREFER_SRC_TMP;
1052 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1053 val |= IPV6_PREFER_SRC_PUBLIC;
1054 else {
1055 /* XXX: should we return system default? */
1056 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1057 }
1058
1059 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1060 val |= IPV6_PREFER_SRC_COA;
1061 else
1062 val |= IPV6_PREFER_SRC_HOME;
1063 break;
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001066 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068 len = min_t(unsigned int, sizeof(int), len);
1069 if(put_user(len, optlen))
1070 return -EFAULT;
1071 if(copy_to_user(optval,&val,len))
1072 return -EFAULT;
1073 return 0;
1074}
1075
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001076int ipv6_getsockopt(struct sock *sk, int level, int optname,
1077 char __user *optval, int __user *optlen)
1078{
1079 int err;
1080
1081 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1082 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1083
1084 if(level != SOL_IPV6)
1085 return -ENOPROTOOPT;
1086
1087 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1088#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001089 /* we need to exclude all possible ENOPROTOOPTs except default case */
1090 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001091 int len;
1092
1093 if (get_user(len, optlen))
1094 return -EFAULT;
1095
1096 lock_sock(sk);
1097 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1098 &len);
1099 release_sock(sk);
1100 if (err >= 0)
1101 err = put_user(len, optlen);
1102 }
1103#endif
1104 return err;
1105}
1106
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001107EXPORT_SYMBOL(ipv6_getsockopt);
1108
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001109#ifdef CONFIG_COMPAT
1110int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001111 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001112{
1113 int err;
1114
1115 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001116 if (udp_prot.compat_getsockopt != NULL)
1117 return udp_prot.compat_getsockopt(sk, level, optname,
1118 optval, optlen);
1119 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001120 }
1121
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001122 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001123 return -ENOPROTOOPT;
1124
1125 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1126#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001127 /* we need to exclude all possible ENOPROTOOPTs except default case */
1128 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001129 int len;
1130
1131 if (get_user(len, optlen))
1132 return -EFAULT;
1133
1134 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001135 err = compat_nf_getsockopt(sk, PF_INET6,
1136 optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001137 release_sock(sk);
1138 if (err >= 0)
1139 err = put_user(len, optlen);
1140 }
1141#endif
1142 return err;
1143}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001144
1145EXPORT_SYMBOL(compat_ipv6_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001146#endif
1147