blob: d31df0f4bc9aa3ce017dc798a2af3be61a7a5c9a [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);
Wei Yongjun800d55f2009-02-23 21:45:33 +0000221 kfree_skb(pktopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 sk->sk_destruct = inet_sock_destruct;
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700224 /*
225 * ... and add it to the refcnt debug socks count
226 * in the new family. -acme
227 */
228 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 module_put(THIS_MODULE);
230 retv = 0;
231 break;
232 }
233 goto e_inval;
234
235 case IPV6_V6ONLY:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900236 if (optlen < sizeof(int) ||
237 inet_sk(sk)->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 goto e_inval;
239 np->ipv6only = valbool;
240 retv = 0;
241 break;
242
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900243 case IPV6_RECVPKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900244 if (optlen < sizeof(int))
245 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 np->rxopt.bits.rxinfo = valbool;
247 retv = 0;
248 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900249
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900250 case IPV6_2292PKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900251 if (optlen < sizeof(int))
252 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900253 np->rxopt.bits.rxoinfo = valbool;
254 retv = 0;
255 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900257 case IPV6_RECVHOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900258 if (optlen < sizeof(int))
259 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 np->rxopt.bits.rxhlim = valbool;
261 retv = 0;
262 break;
263
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900264 case IPV6_2292HOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900265 if (optlen < sizeof(int))
266 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900267 np->rxopt.bits.rxohlim = valbool;
268 retv = 0;
269 break;
270
271 case IPV6_RECVRTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900272 if (optlen < sizeof(int))
273 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900274 np->rxopt.bits.srcrt = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 retv = 0;
276 break;
277
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900278 case IPV6_2292RTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900279 if (optlen < sizeof(int))
280 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900281 np->rxopt.bits.osrcrt = valbool;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900282 retv = 0;
283 break;
284
285 case IPV6_RECVHOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900286 if (optlen < sizeof(int))
287 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 np->rxopt.bits.hopopts = valbool;
289 retv = 0;
290 break;
291
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900292 case IPV6_2292HOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900293 if (optlen < sizeof(int))
294 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900295 np->rxopt.bits.ohopopts = valbool;
296 retv = 0;
297 break;
298
299 case IPV6_RECVDSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900300 if (optlen < sizeof(int))
301 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 np->rxopt.bits.dstopts = valbool;
303 retv = 0;
304 break;
305
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900306 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900307 if (optlen < sizeof(int))
308 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900309 np->rxopt.bits.odstopts = valbool;
310 retv = 0;
311 break;
312
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900313 case IPV6_TCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900314 if (optlen < sizeof(int))
315 goto e_inval;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700316 if (val < -1 || val > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900317 goto e_inval;
318 np->tclass = val;
319 retv = 0;
320 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900321
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900322 case IPV6_RECVTCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900323 if (optlen < sizeof(int))
324 goto e_inval;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900325 np->rxopt.bits.rxtclass = valbool;
326 retv = 0;
327 break;
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 case IPV6_FLOWINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900330 if (optlen < sizeof(int))
331 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 np->rxopt.bits.rxflow = valbool;
333 retv = 0;
334 break;
335
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900336 case IPV6_HOPOPTS:
337 case IPV6_RTHDRDSTOPTS:
338 case IPV6_RTHDR:
339 case IPV6_DSTOPTS:
340 {
341 struct ipv6_txoptions *opt;
Shan Weiaea74272008-06-19 16:29:39 -0700342
343 /* remove any sticky options header with a zero option
344 * length, per RFC3542.
345 */
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900346 if (optlen == 0)
Luiz Capitulinocb422c42005-11-14 21:43:36 -0800347 optval = NULL;
Yang Hongyangcfb266c2008-08-03 18:16:15 -0700348 else if (optval == NULL)
349 goto e_inval;
Shan Weiaea74272008-06-19 16:29:39 -0700350 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
351 optlen & 0x7 || optlen > 8 * 255)
352 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900353
354 /* hop-by-hop / destination options are privileged option */
355 retv = -EPERM;
356 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
357 break;
358
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900359 opt = ipv6_renew_options(sk, np->opt, optname,
360 (struct ipv6_opt_hdr __user *)optval,
361 optlen);
362 if (IS_ERR(opt)) {
363 retv = PTR_ERR(opt);
364 break;
365 }
366
367 /* routing header option needs extra check */
Brian Haley6e093d92008-11-12 22:59:21 -0800368 retv = -EINVAL;
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 NAKAMURA59fbb3a2007-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:
Brian Haley6e093d92008-11-12 22:59:21 -0800374 if (rthdr->hdrlen != 2 ||
375 rthdr->segments_left != 1)
376 goto sticky_done;
377
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700378 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700379#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700380 default:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900381 goto sticky_done;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700382 }
383
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900384 if ((rthdr->hdrlen & 1) ||
385 (rthdr->hdrlen >> 1) != rthdr->segments_left)
386 goto sticky_done;
387 }
388
389 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700390 opt = ipv6_update_options(sk, opt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900391sticky_done:
392 if (opt)
393 sock_kfree_s(sk, opt, opt->tot_len);
394 break;
395 }
396
Yang Hongyangb24a2512008-12-16 02:06:23 -0800397 case IPV6_PKTINFO:
398 {
399 struct in6_pktinfo pkt;
400
401 if (optlen == 0)
402 goto e_inval;
403 else if (optlen < sizeof(struct in6_pktinfo) || optval == NULL)
404 goto e_inval;
405
Ilpo Järvinen914d1162009-01-04 17:27:31 -0800406 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
Yang Hongyangb24a2512008-12-16 02:06:23 -0800407 retv = -EFAULT;
408 break;
409 }
410 if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if)
411 goto e_inval;
412
413 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
414 ipv6_addr_copy(&np->sticky_pktinfo.ipi6_addr, &pkt.ipi6_addr);
415 retv = 0;
416 break;
417 }
418
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900419 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 {
421 struct ipv6_txoptions *opt = NULL;
422 struct msghdr msg;
423 struct flowi fl;
424 int junk;
425
426 fl.fl6_flowlabel = 0;
427 fl.oif = sk->sk_bound_dev_if;
428
429 if (optlen == 0)
430 goto update;
431
432 /* 1K is probably excessive
433 * 1K is surely not enough, 2K per standard header is 16K.
434 */
435 retv = -EINVAL;
436 if (optlen > 64*1024)
437 break;
438
439 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
440 retv = -ENOBUFS;
441 if (opt == NULL)
442 break;
443
444 memset(opt, 0, sizeof(*opt));
445 opt->tot_len = sizeof(*opt) + optlen;
446 retv = -EFAULT;
447 if (copy_from_user(opt+1, optval, optlen))
448 goto done;
449
450 msg.msg_controllen = optlen;
451 msg.msg_control = (void*)(opt+1);
452
YOSHIFUJI Hideaki91e19082008-06-04 13:02:49 +0900453 retv = datagram_send_ctl(net, &msg, &fl, opt, &junk, &junk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (retv)
455 goto done;
456update:
457 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700458 opt = ipv6_update_options(sk, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459done:
460 if (opt)
461 sock_kfree_s(sk, opt, opt->tot_len);
462 break;
463 }
464 case IPV6_UNICAST_HOPS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900465 if (optlen < sizeof(int))
466 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (val > 255 || val < -1)
468 goto e_inval;
469 np->hop_limit = val;
470 retv = 0;
471 break;
472
473 case IPV6_MULTICAST_HOPS:
474 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900475 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900476 if (optlen < sizeof(int))
477 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (val > 255 || val < -1)
479 goto e_inval;
480 np->mcast_hops = val;
481 retv = 0;
482 break;
483
484 case IPV6_MULTICAST_LOOP:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900485 if (optlen < sizeof(int))
486 goto e_inval;
YOSHIFUJI Hideaki28d44882008-06-12 03:14:51 +0900487 if (val != valbool)
488 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 np->mc_loop = valbool;
490 retv = 0;
491 break;
492
493 case IPV6_MULTICAST_IF:
494 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900495 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900496 if (optlen < sizeof(int))
497 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Brian Haley4953f0f2007-10-11 14:39:29 -0700499 if (val) {
500 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
501 goto e_inval;
502
Daniel Lezcano8a3edd82008-03-07 11:14:16 -0800503 if (__dev_get_by_index(net, val) == NULL) {
Brian Haley4953f0f2007-10-11 14:39:29 -0700504 retv = -ENODEV;
505 break;
506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 np->mcast_oif = val;
509 retv = 0;
510 break;
511 case IPV6_ADD_MEMBERSHIP:
512 case IPV6_DROP_MEMBERSHIP:
513 {
514 struct ipv6_mreq mreq;
515
Wang Chena28398b2008-04-07 09:42:07 +0800516 if (optlen < sizeof(struct ipv6_mreq))
517 goto e_inval;
518
Flavio Leitnera96fb492007-08-24 22:16:39 -0700519 retv = -EPROTO;
520 if (inet_sk(sk)->is_icsk)
521 break;
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 retv = -EFAULT;
524 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
525 break;
526
527 if (optname == IPV6_ADD_MEMBERSHIP)
528 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
529 else
530 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
531 break;
532 }
533 case IPV6_JOIN_ANYCAST:
534 case IPV6_LEAVE_ANYCAST:
535 {
536 struct ipv6_mreq mreq;
537
Wang Chena28398b2008-04-07 09:42:07 +0800538 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto e_inval;
540
541 retv = -EFAULT;
542 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
543 break;
544
545 if (optname == IPV6_JOIN_ANYCAST)
546 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
547 else
548 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
549 break;
550 }
551 case MCAST_JOIN_GROUP:
552 case MCAST_LEAVE_GROUP:
553 {
554 struct group_req greq;
555 struct sockaddr_in6 *psin6;
556
Wang Chena28398b2008-04-07 09:42:07 +0800557 if (optlen < sizeof(struct group_req))
558 goto e_inval;
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 retv = -EFAULT;
561 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
562 break;
563 if (greq.gr_group.ss_family != AF_INET6) {
564 retv = -EADDRNOTAVAIL;
565 break;
566 }
567 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
568 if (optname == MCAST_JOIN_GROUP)
569 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
570 &psin6->sin6_addr);
571 else
572 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
573 &psin6->sin6_addr);
574 break;
575 }
576 case MCAST_JOIN_SOURCE_GROUP:
577 case MCAST_LEAVE_SOURCE_GROUP:
578 case MCAST_BLOCK_SOURCE:
579 case MCAST_UNBLOCK_SOURCE:
580 {
581 struct group_source_req greqs;
582 int omode, add;
583
Wang Chena28398b2008-04-07 09:42:07 +0800584 if (optlen < sizeof(struct group_source_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 goto e_inval;
586 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
587 retv = -EFAULT;
588 break;
589 }
590 if (greqs.gsr_group.ss_family != AF_INET6 ||
591 greqs.gsr_source.ss_family != AF_INET6) {
592 retv = -EADDRNOTAVAIL;
593 break;
594 }
595 if (optname == MCAST_BLOCK_SOURCE) {
596 omode = MCAST_EXCLUDE;
597 add = 1;
598 } else if (optname == MCAST_UNBLOCK_SOURCE) {
599 omode = MCAST_EXCLUDE;
600 add = 0;
601 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
602 struct sockaddr_in6 *psin6;
603
604 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
605 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
606 &psin6->sin6_addr);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700607 /* prior join w/ different source is ok */
608 if (retv && retv != -EADDRINUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 break;
610 omode = MCAST_INCLUDE;
611 add = 1;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700612 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 omode = MCAST_INCLUDE;
614 add = 0;
615 }
616 retv = ip6_mc_source(add, omode, sk, &greqs);
617 break;
618 }
619 case MCAST_MSFILTER:
620 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 extern int sysctl_mld_max_msf;
622 struct group_filter *gsf;
623
624 if (optlen < GROUP_FILTER_SIZE(0))
625 goto e_inval;
626 if (optlen > sysctl_optmem_max) {
627 retv = -ENOBUFS;
628 break;
629 }
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800630 gsf = kmalloc(optlen,GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700631 if (!gsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 retv = -ENOBUFS;
633 break;
634 }
635 retv = -EFAULT;
636 if (copy_from_user(gsf, optval, optlen)) {
637 kfree(gsf);
638 break;
639 }
640 /* numsrc >= (4G-140)/128 overflow in 32 bits */
641 if (gsf->gf_numsrc >= 0x1ffffffU ||
642 gsf->gf_numsrc > sysctl_mld_max_msf) {
643 kfree(gsf);
644 retv = -ENOBUFS;
645 break;
646 }
647 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
648 kfree(gsf);
649 retv = -EINVAL;
650 break;
651 }
652 retv = ip6_mc_msfilter(sk, gsf);
653 kfree(gsf);
654
655 break;
656 }
657 case IPV6_ROUTER_ALERT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900658 if (optlen < sizeof(int))
659 goto e_inval;
Denis V. Lunev725a8ff2008-07-19 00:28:58 -0700660 retv = ip6_ra_control(sk, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 break;
662 case IPV6_MTU_DISCOVER:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900663 if (optlen < sizeof(int))
664 goto e_inval;
John Heffner628a5c52007-04-20 15:53:27 -0700665 if (val<0 || val>3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 goto e_inval;
667 np->pmtudisc = val;
668 retv = 0;
669 break;
670 case IPV6_MTU:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900671 if (optlen < sizeof(int))
672 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (val && val < IPV6_MIN_MTU)
674 goto e_inval;
675 np->frag_size = val;
676 retv = 0;
677 break;
678 case IPV6_RECVERR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900679 if (optlen < sizeof(int))
680 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 np->recverr = valbool;
682 if (!val)
683 skb_queue_purge(&sk->sk_error_queue);
684 retv = 0;
685 break;
686 case IPV6_FLOWINFO_SEND:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900687 if (optlen < sizeof(int))
688 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 np->sndflow = valbool;
690 retv = 0;
691 break;
692 case IPV6_FLOWLABEL_MGR:
693 retv = ipv6_flowlabel_opt(sk, optval, optlen);
694 break;
695 case IPV6_IPSEC_POLICY:
696 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700697 retv = -EPERM;
698 if (!capable(CAP_NET_ADMIN))
699 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 retv = xfrm_user_policy(sk, optname, optval, optlen);
701 break;
702
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900703 case IPV6_ADDR_PREFERENCES:
704 {
705 unsigned int pref = 0;
706 unsigned int prefmask = ~0;
707
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900708 if (optlen < sizeof(int))
709 goto e_inval;
710
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900711 retv = -EINVAL;
712
713 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
714 switch (val & (IPV6_PREFER_SRC_PUBLIC|
715 IPV6_PREFER_SRC_TMP|
716 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
717 case IPV6_PREFER_SRC_PUBLIC:
718 pref |= IPV6_PREFER_SRC_PUBLIC;
719 break;
720 case IPV6_PREFER_SRC_TMP:
721 pref |= IPV6_PREFER_SRC_TMP;
722 break;
723 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
724 break;
725 case 0:
726 goto pref_skip_pubtmp;
727 default:
728 goto e_inval;
729 }
730
731 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
732 IPV6_PREFER_SRC_TMP);
733pref_skip_pubtmp:
734
735 /* check HOME/COA conflicts */
736 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
737 case IPV6_PREFER_SRC_HOME:
738 break;
739 case IPV6_PREFER_SRC_COA:
740 pref |= IPV6_PREFER_SRC_COA;
741 case 0:
742 goto pref_skip_coa;
743 default:
744 goto e_inval;
745 }
746
747 prefmask &= ~IPV6_PREFER_SRC_COA;
748pref_skip_coa:
749
750 /* check CGA/NONCGA conflicts */
751 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
752 case IPV6_PREFER_SRC_CGA:
753 case IPV6_PREFER_SRC_NONCGA:
754 case 0:
755 break;
756 default:
757 goto e_inval;
758 }
759
760 np->srcprefs = (np->srcprefs & prefmask) | pref;
761 retv = 0;
762
763 break;
764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 release_sock(sk);
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return retv;
770
771e_inval:
772 release_sock(sk);
773 return -EINVAL;
774}
775
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800776int ipv6_setsockopt(struct sock *sk, int level, int optname,
777 char __user *optval, int optlen)
778{
779 int err;
780
781 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
782 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
783
784 if (level != SOL_IPV6)
785 return -ENOPROTOOPT;
786
787 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
788#ifdef CONFIG_NETFILTER
789 /* we need to exclude all possible ENOPROTOOPTs except default case */
790 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
791 optname != IPV6_XFRM_POLICY) {
792 lock_sock(sk);
793 err = nf_setsockopt(sk, PF_INET6, optname, optval,
794 optlen);
795 release_sock(sk);
796 }
797#endif
798 return err;
799}
800
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900801EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800802
803#ifdef CONFIG_COMPAT
804int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800805 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800806{
807 int err;
808
809 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800810 if (udp_prot.compat_setsockopt != NULL)
811 return udp_prot.compat_setsockopt(sk, level, optname,
812 optval, optlen);
813 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800814 }
815
816 if (level != SOL_IPV6)
817 return -ENOPROTOOPT;
818
David L Stevensdae50292008-04-27 01:06:07 -0700819 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
820 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
821 ipv6_setsockopt);
822
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800823 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
824#ifdef CONFIG_NETFILTER
825 /* we need to exclude all possible ENOPROTOOPTs except default case */
826 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800827 optname != IPV6_XFRM_POLICY) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800828 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800829 err = compat_nf_setsockopt(sk, PF_INET6, optname,
830 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800831 release_sock(sk);
832 }
833#endif
834 return err;
835}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800836
837EXPORT_SYMBOL(compat_ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800838#endif
839
David S. Miller286930792007-03-07 12:50:46 -0800840static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700841 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900842{
David S. Miller286930792007-03-07 12:50:46 -0800843 struct ipv6_opt_hdr *hdr;
844
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700845 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900846 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700847
848 switch(optname) {
849 case IPV6_HOPOPTS:
850 hdr = opt->hopopt;
851 break;
852 case IPV6_RTHDRDSTOPTS:
853 hdr = opt->dst0opt;
854 break;
855 case IPV6_RTHDR:
856 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
857 break;
858 case IPV6_DSTOPTS:
859 hdr = opt->dst1opt;
860 break;
861 default:
862 return -EINVAL; /* should not happen */
863 }
864
865 if (!hdr)
866 return 0;
David S. Miller286930792007-03-07 12:50:46 -0800867
Chris Wrightd2b02ed2007-03-09 16:19:17 -0800868 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -0700869 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900870 return -EFAULT;
Yang Hongyang95b496b2008-05-28 16:27:28 +0800871 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900872}
873
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800874static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 char __user *optval, int __user *optlen)
876{
877 struct ipv6_pinfo *np = inet6_sk(sk);
878 int len;
879 int val;
880
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900881 if (ip6_mroute_opt(optname))
882 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (get_user(len, optlen))
885 return -EFAULT;
886 switch (optname) {
887 case IPV6_ADDRFORM:
888 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800889 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 sk->sk_protocol != IPPROTO_TCP)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900891 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (sk->sk_state != TCP_ESTABLISHED)
893 return -ENOTCONN;
894 val = sk->sk_family;
895 break;
896 case MCAST_MSFILTER:
897 {
898 struct group_filter gsf;
899 int err;
900
901 if (len < GROUP_FILTER_SIZE(0))
902 return -EINVAL;
903 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
904 return -EFAULT;
YOSHIFUJI Hideaki20c61fb2008-04-28 14:40:55 +0900905 if (gsf.gf_group.ss_family != AF_INET6)
906 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 lock_sock(sk);
908 err = ip6_mc_msfget(sk, &gsf,
909 (struct group_filter __user *)optval, optlen);
910 release_sock(sk);
911 return err;
912 }
913
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900914 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 {
916 struct msghdr msg;
917 struct sk_buff *skb;
918
919 if (sk->sk_type != SOCK_STREAM)
920 return -ENOPROTOOPT;
921
922 msg.msg_control = optval;
923 msg.msg_controllen = len;
924 msg.msg_flags = 0;
925
926 lock_sock(sk);
927 skb = np->pktoptions;
928 if (skb)
929 atomic_inc(&skb->users);
930 release_sock(sk);
931
932 if (skb) {
933 int err = datagram_recv_ctl(sk, &msg, skb);
934 kfree_skb(skb);
935 if (err)
936 return err;
937 } else {
938 if (np->rxopt.bits.rxinfo) {
939 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -0800940 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
941 np->sticky_pktinfo.ipi6_ifindex;
942 np->mcast_oif? ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr) :
943 ipv6_addr_copy(&src_info.ipi6_addr, &(np->sticky_pktinfo.ipi6_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
945 }
946 if (np->rxopt.bits.rxhlim) {
947 int hlim = np->mcast_hops;
948 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
949 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900950 if (np->rxopt.bits.rxoinfo) {
951 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -0800952 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
953 np->sticky_pktinfo.ipi6_ifindex;
954 np->mcast_oif? ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr) :
955 ipv6_addr_copy(&src_info.ipi6_addr, &(np->sticky_pktinfo.ipi6_addr));
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900956 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
957 }
958 if (np->rxopt.bits.rxohlim) {
959 int hlim = np->mcast_hops;
960 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963 len -= msg.msg_controllen;
964 return put_user(len, optlen);
965 }
966 case IPV6_MTU:
967 {
968 struct dst_entry *dst;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900969 val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 lock_sock(sk);
971 dst = sk_dst_get(sk);
972 if (dst) {
973 val = dst_mtu(dst);
974 dst_release(dst);
975 }
976 release_sock(sk);
977 if (!val)
978 return -ENOTCONN;
979 break;
980 }
981
982 case IPV6_V6ONLY:
983 val = np->ipv6only;
984 break;
985
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900986 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 val = np->rxopt.bits.rxinfo;
988 break;
989
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900990 case IPV6_2292PKTINFO:
991 val = np->rxopt.bits.rxoinfo;
992 break;
993
994 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 val = np->rxopt.bits.rxhlim;
996 break;
997
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900998 case IPV6_2292HOPLIMIT:
999 val = np->rxopt.bits.rxohlim;
1000 break;
1001
1002 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 val = np->rxopt.bits.srcrt;
1004 break;
1005
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001006 case IPV6_2292RTHDR:
1007 val = np->rxopt.bits.osrcrt;
1008 break;
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001011 case IPV6_RTHDRDSTOPTS:
1012 case IPV6_RTHDR:
1013 case IPV6_DSTOPTS:
1014 {
1015
1016 lock_sock(sk);
David S. Miller286930792007-03-07 12:50:46 -08001017 len = ipv6_getsockopt_sticky(sk, np->opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001018 optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001019 release_sock(sk);
Yang Hongyang05335c22008-05-28 16:23:47 +08001020 /* check if ipv6_getsockopt_sticky() returns err code */
1021 if (len < 0)
1022 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001023 return put_user(len, optlen);
1024 }
1025
1026 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 val = np->rxopt.bits.hopopts;
1028 break;
1029
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001030 case IPV6_2292HOPOPTS:
1031 val = np->rxopt.bits.ohopopts;
1032 break;
1033
1034 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 val = np->rxopt.bits.dstopts;
1036 break;
1037
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001038 case IPV6_2292DSTOPTS:
1039 val = np->rxopt.bits.odstopts;
1040 break;
1041
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001042 case IPV6_TCLASS:
1043 val = np->tclass;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -07001044 if (val < 0)
1045 val = 0;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001046 break;
1047
1048 case IPV6_RECVTCLASS:
1049 val = np->rxopt.bits.rxtclass;
1050 break;
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 case IPV6_FLOWINFO:
1053 val = np->rxopt.bits.rxflow;
1054 break;
1055
1056 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -08001058 {
1059 struct dst_entry *dst;
1060
1061 if (optname == IPV6_UNICAST_HOPS)
1062 val = np->hop_limit;
1063 else
1064 val = np->mcast_hops;
1065
1066 dst = sk_dst_get(sk);
1067 if (dst) {
1068 if (val < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001069 val = ip6_dst_hoplimit(dst);
Brian Haleybefffe92006-12-12 17:09:49 -08001070 dst_release(dst);
1071 }
1072 if (val < 0)
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001073 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 break;
Brian Haleybefffe92006-12-12 17:09:49 -08001075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 case IPV6_MULTICAST_LOOP:
1078 val = np->mc_loop;
1079 break;
1080
1081 case IPV6_MULTICAST_IF:
1082 val = np->mcast_oif;
1083 break;
1084
1085 case IPV6_MTU_DISCOVER:
1086 val = np->pmtudisc;
1087 break;
1088
1089 case IPV6_RECVERR:
1090 val = np->recverr;
1091 break;
1092
1093 case IPV6_FLOWINFO_SEND:
1094 val = np->sndflow;
1095 break;
1096
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001097 case IPV6_ADDR_PREFERENCES:
1098 val = 0;
1099
1100 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1101 val |= IPV6_PREFER_SRC_TMP;
1102 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1103 val |= IPV6_PREFER_SRC_PUBLIC;
1104 else {
1105 /* XXX: should we return system default? */
1106 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1107 }
1108
1109 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1110 val |= IPV6_PREFER_SRC_COA;
1111 else
1112 val |= IPV6_PREFER_SRC_HOME;
1113 break;
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001116 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118 len = min_t(unsigned int, sizeof(int), len);
1119 if(put_user(len, optlen))
1120 return -EFAULT;
1121 if(copy_to_user(optval,&val,len))
1122 return -EFAULT;
1123 return 0;
1124}
1125
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001126int ipv6_getsockopt(struct sock *sk, int level, int optname,
1127 char __user *optval, int __user *optlen)
1128{
1129 int err;
1130
1131 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1132 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1133
1134 if(level != SOL_IPV6)
1135 return -ENOPROTOOPT;
1136
1137 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1138#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001139 /* we need to exclude all possible ENOPROTOOPTs except default case */
1140 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001141 int len;
1142
1143 if (get_user(len, optlen))
1144 return -EFAULT;
1145
1146 lock_sock(sk);
1147 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1148 &len);
1149 release_sock(sk);
1150 if (err >= 0)
1151 err = put_user(len, optlen);
1152 }
1153#endif
1154 return err;
1155}
1156
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001157EXPORT_SYMBOL(ipv6_getsockopt);
1158
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001159#ifdef CONFIG_COMPAT
1160int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001161 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001162{
1163 int err;
1164
1165 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001166 if (udp_prot.compat_getsockopt != NULL)
1167 return udp_prot.compat_getsockopt(sk, level, optname,
1168 optval, optlen);
1169 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001170 }
1171
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001172 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001173 return -ENOPROTOOPT;
1174
Pavel Emelyanov80991792008-04-30 14:49:54 -07001175 if (optname == MCAST_MSFILTER)
1176 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1177 ipv6_getsockopt);
1178
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001179 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1180#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001181 /* we need to exclude all possible ENOPROTOOPTs except default case */
1182 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001183 int len;
1184
1185 if (get_user(len, optlen))
1186 return -EFAULT;
1187
1188 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001189 err = compat_nf_getsockopt(sk, PF_INET6,
1190 optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001191 release_sock(sk);
1192 if (err >= 0)
1193 err = put_user(len, optlen);
1194 }
1195#endif
1196 return err;
1197}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001198
1199EXPORT_SYMBOL(compat_ipv6_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001200#endif
1201