blob: a7bee6a9133505b1ab095b86ab0b72ac7f7418e0 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <net/sock.h>
42#include <net/snmp.h>
43#include <net/ipv6.h>
44#include <net/ndisc.h>
45#include <net/protocol.h>
46#include <net/transp_v6.h>
47#include <net/ip6_route.h>
48#include <net/addrconf.h>
49#include <net/inet_common.h>
50#include <net/tcp.h>
51#include <net/udp.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080052#include <net/udplite.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <net/xfrm.h>
David L Stevensdae50292008-04-27 01:06:07 -070054#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#include <asm/uaccess.h>
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058struct ip6_ra_chain *ip6_ra_chain;
59DEFINE_RWLOCK(ip6_ra_lock);
60
Denis V. Lunev725a8ff2008-07-19 00:28:58 -070061int ip6_ra_control(struct sock *sk, int sel)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 struct ip6_ra_chain *ra, *new_ra, **rap;
64
65 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
Eric Dumazetc720c7e2009-10-15 06:30:45 +000066 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num != IPPROTO_RAW)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +090067 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
70
71 write_lock_bh(&ip6_ra_lock);
72 for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
73 if (ra->sk == sk) {
74 if (sel>=0) {
75 write_unlock_bh(&ip6_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -080076 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return -EADDRINUSE;
78 }
79
80 *rap = ra->next;
81 write_unlock_bh(&ip6_ra_lock);
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 sock_put(sk);
84 kfree(ra);
85 return 0;
86 }
87 }
88 if (new_ra == NULL) {
89 write_unlock_bh(&ip6_ra_lock);
90 return -ENOBUFS;
91 }
92 new_ra->sk = sk;
93 new_ra->sel = sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 new_ra->next = ra;
95 *rap = new_ra;
96 sock_hold(sk);
97 write_unlock_bh(&ip6_ra_lock);
98 return 0;
99}
100
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700101static
102struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
103 struct ipv6_txoptions *opt)
104{
105 if (inet_sk(sk)->is_icsk) {
106 if (opt &&
107 !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000108 inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) {
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700109 struct inet_connection_sock *icsk = inet_csk(sk);
110 icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
111 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
112 }
113 opt = xchg(&inet6_sk(sk)->opt, opt);
114 } else {
Eric Dumazetb6c67122010-04-08 23:03:29 +0000115 spin_lock(&sk->sk_dst_lock);
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700116 opt = xchg(&inet6_sk(sk)->opt, opt);
Eric Dumazetb6c67122010-04-08 23:03:29 +0000117 spin_unlock(&sk->sk_dst_lock);
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700118 }
119 sk_dst_reset(sk);
120
121 return opt;
122}
123
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800124static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700125 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900128 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 int val, valbool;
130 int retv = -ENOPROTOOPT;
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (optval == NULL)
133 val=0;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900134 else {
135 if (optlen >= sizeof(int)) {
136 if (get_user(val, (int __user *) optval))
137 return -EFAULT;
138 } else
139 val = 0;
140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 valbool = (val!=0);
143
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900144 if (ip6_mroute_opt(optname))
145 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 lock_sock(sk);
148
149 switch (optname) {
150
151 case IPV6_ADDRFORM:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900152 if (optlen < sizeof(int))
153 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (val == PF_INET) {
155 struct ipv6_txoptions *opt;
156 struct sk_buff *pktopt;
157
Denis V. Lunev49d074f2008-06-04 15:49:06 +0400158 if (sk->sk_type == SOCK_RAW)
159 break;
160
Denis V. Lunev9596cc822008-06-04 15:49:08 +0400161 if (sk->sk_protocol == IPPROTO_UDP ||
162 sk->sk_protocol == IPPROTO_UDPLITE) {
163 struct udp_sock *up = udp_sk(sk);
164 if (up->pending == AF_INET6) {
165 retv = -EBUSY;
166 break;
167 }
168 } else if (sk->sk_protocol != IPPROTO_TCP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 break;
170
171 if (sk->sk_state != TCP_ESTABLISHED) {
172 retv = -ENOTCONN;
173 break;
174 }
175
176 if (ipv6_only_sock(sk) ||
Brian Haleye773e4f2007-08-24 23:16:08 -0700177 !ipv6_addr_v4mapped(&np->daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 retv = -EADDRNOTAVAIL;
179 break;
180 }
181
182 fl6_free_socklist(sk);
183 ipv6_sock_mc_close(sk);
184
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700185 /*
186 * Sock is moving from IPv6 to IPv4 (sk_prot), so
187 * remove it from the refcnt debug socks count in the
188 * original family...
189 */
190 sk_refcnt_debug_dec(sk);
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (sk->sk_protocol == IPPROTO_TCP) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800193 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700195 sock_prot_inuse_add(net, sk->sk_prot, -1);
196 sock_prot_inuse_add(net, &tcp_prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 local_bh_enable();
198 sk->sk_prot = &tcp_prot;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800199 icsk->icsk_af_ops = &ipv4_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 sk->sk_socket->ops = &inet_stream_ops;
201 sk->sk_family = PF_INET;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800202 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 } else {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800204 struct proto *prot = &udp_prot;
205
David S. Millerdb8dac22008-03-06 16:22:02 -0800206 if (sk->sk_protocol == IPPROTO_UDPLITE)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800207 prot = &udplite_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 local_bh_disable();
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700209 sock_prot_inuse_add(net, sk->sk_prot, -1);
210 sock_prot_inuse_add(net, prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 local_bh_enable();
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800212 sk->sk_prot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 sk->sk_socket->ops = &inet_dgram_ops;
214 sk->sk_family = PF_INET;
215 }
216 opt = xchg(&np->opt, NULL);
217 if (opt)
218 sock_kfree_s(sk, opt, opt->tot_len);
219 pktopt = xchg(&np->pktoptions, NULL);
Wei Yongjun800d55f2009-02-23 21:45:33 +0000220 kfree_skb(pktopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 sk->sk_destruct = inet_sock_destruct;
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700223 /*
224 * ... and add it to the refcnt debug socks count
225 * in the new family. -acme
226 */
227 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 module_put(THIS_MODULE);
229 retv = 0;
230 break;
231 }
232 goto e_inval;
233
234 case IPV6_V6ONLY:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900235 if (optlen < sizeof(int) ||
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000236 inet_sk(sk)->inet_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 goto e_inval;
238 np->ipv6only = valbool;
239 retv = 0;
240 break;
241
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900242 case IPV6_RECVPKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900243 if (optlen < sizeof(int))
244 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 np->rxopt.bits.rxinfo = valbool;
246 retv = 0;
247 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900248
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900249 case IPV6_2292PKTINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900250 if (optlen < sizeof(int))
251 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900252 np->rxopt.bits.rxoinfo = valbool;
253 retv = 0;
254 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900256 case IPV6_RECVHOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900257 if (optlen < sizeof(int))
258 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 np->rxopt.bits.rxhlim = valbool;
260 retv = 0;
261 break;
262
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900263 case IPV6_2292HOPLIMIT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900264 if (optlen < sizeof(int))
265 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900266 np->rxopt.bits.rxohlim = valbool;
267 retv = 0;
268 break;
269
270 case IPV6_RECVRTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900271 if (optlen < sizeof(int))
272 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900273 np->rxopt.bits.srcrt = valbool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 retv = 0;
275 break;
276
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900277 case IPV6_2292RTHDR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900278 if (optlen < sizeof(int))
279 goto e_inval;
YOSHIFUJI Hideaki4c752092007-05-23 13:28:48 +0900280 np->rxopt.bits.osrcrt = valbool;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900281 retv = 0;
282 break;
283
284 case IPV6_RECVHOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900285 if (optlen < sizeof(int))
286 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 np->rxopt.bits.hopopts = valbool;
288 retv = 0;
289 break;
290
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900291 case IPV6_2292HOPOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900292 if (optlen < sizeof(int))
293 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900294 np->rxopt.bits.ohopopts = valbool;
295 retv = 0;
296 break;
297
298 case IPV6_RECVDSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900299 if (optlen < sizeof(int))
300 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 np->rxopt.bits.dstopts = valbool;
302 retv = 0;
303 break;
304
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900305 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900306 if (optlen < sizeof(int))
307 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900308 np->rxopt.bits.odstopts = valbool;
309 retv = 0;
310 break;
311
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900312 case IPV6_TCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900313 if (optlen < sizeof(int))
314 goto e_inval;
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700315 if (val < -1 || val > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900316 goto e_inval;
Gerrit Renker26ced1e2009-08-09 08:12:49 +0000317 /* RFC 3542, 6.5: default traffic class of 0x0 */
318 if (val == -1)
319 val = 0;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900320 np->tclass = val;
321 retv = 0;
322 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900323
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900324 case IPV6_RECVTCLASS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900325 if (optlen < sizeof(int))
326 goto e_inval;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900327 np->rxopt.bits.rxtclass = valbool;
328 retv = 0;
329 break;
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 case IPV6_FLOWINFO:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900332 if (optlen < sizeof(int))
333 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 np->rxopt.bits.rxflow = valbool;
335 retv = 0;
336 break;
337
Brian Haley793b14732010-04-23 11:26:07 +0000338 case IPV6_RECVPATHMTU:
339 if (optlen < sizeof(int))
340 goto e_inval;
341 np->rxopt.bits.rxpmtu = valbool;
342 retv = 0;
343 break;
344
Balazs Scheidler6c468622010-10-21 16:08:28 +0200345 case IPV6_TRANSPARENT:
Maciej Żenczykowski6cc7a762011-10-20 18:21:36 -0400346 if (valbool && !capable(CAP_NET_ADMIN) && !capable(CAP_NET_RAW)) {
Balazs Scheidlerb8894162010-10-23 04:48:14 +0000347 retv = -EPERM;
348 break;
349 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200350 if (optlen < sizeof(int))
351 goto e_inval;
352 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
353 inet_sk(sk)->transparent = valbool;
354 retv = 0;
355 break;
356
357 case IPV6_RECVORIGDSTADDR:
358 if (optlen < sizeof(int))
359 goto e_inval;
360 np->rxopt.bits.rxorigdstaddr = valbool;
361 retv = 0;
362 break;
363
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900364 case IPV6_HOPOPTS:
365 case IPV6_RTHDRDSTOPTS:
366 case IPV6_RTHDR:
367 case IPV6_DSTOPTS:
368 {
369 struct ipv6_txoptions *opt;
Shan Weiaea74272008-06-19 16:29:39 -0700370
371 /* remove any sticky options header with a zero option
372 * length, per RFC3542.
373 */
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900374 if (optlen == 0)
Luiz Capitulinocb422c42005-11-14 21:43:36 -0800375 optval = NULL;
Yang Hongyangcfb266c2008-08-03 18:16:15 -0700376 else if (optval == NULL)
377 goto e_inval;
Shan Weiaea74272008-06-19 16:29:39 -0700378 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
379 optlen & 0x7 || optlen > 8 * 255)
380 goto e_inval;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900381
382 /* hop-by-hop / destination options are privileged option */
383 retv = -EPERM;
384 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
385 break;
386
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900387 opt = ipv6_renew_options(sk, np->opt, optname,
388 (struct ipv6_opt_hdr __user *)optval,
389 optlen);
390 if (IS_ERR(opt)) {
391 retv = PTR_ERR(opt);
392 break;
393 }
394
395 /* routing header option needs extra check */
Brian Haley6e093d92008-11-12 22:59:21 -0800396 retv = -EINVAL;
Olaf Kirchdfee0a72007-03-09 13:55:38 -0800397 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900398 struct ipv6_rt_hdr *rthdr = opt->srcrt;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700399 switch (rthdr->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000400#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700401 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800402 if (rthdr->hdrlen != 2 ||
403 rthdr->segments_left != 1)
404 goto sticky_done;
405
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700406 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700407#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700408 default:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900409 goto sticky_done;
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700410 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900411 }
412
413 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700414 opt = ipv6_update_options(sk, opt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900415sticky_done:
416 if (opt)
417 sock_kfree_s(sk, opt, opt->tot_len);
418 break;
419 }
420
Yang Hongyangb24a2512008-12-16 02:06:23 -0800421 case IPV6_PKTINFO:
422 {
423 struct in6_pktinfo pkt;
424
425 if (optlen == 0)
426 goto e_inval;
427 else if (optlen < sizeof(struct in6_pktinfo) || optval == NULL)
428 goto e_inval;
429
Ilpo Järvinen914d1162009-01-04 17:27:31 -0800430 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
Yang Hongyangb24a2512008-12-16 02:06:23 -0800431 retv = -EFAULT;
432 break;
433 }
434 if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if)
435 goto e_inval;
436
437 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000438 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
Yang Hongyangb24a2512008-12-16 02:06:23 -0800439 retv = 0;
440 break;
441 }
442
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900443 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 {
445 struct ipv6_txoptions *opt = NULL;
446 struct msghdr msg;
David S. Miller4c9483b2011-03-12 16:22:43 -0500447 struct flowi6 fl6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int junk;
449
David S. Miller4c9483b2011-03-12 16:22:43 -0500450 memset(&fl6, 0, sizeof(fl6));
451 fl6.flowi6_oif = sk->sk_bound_dev_if;
452 fl6.flowi6_mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 if (optlen == 0)
455 goto update;
456
457 /* 1K is probably excessive
458 * 1K is surely not enough, 2K per standard header is 16K.
459 */
460 retv = -EINVAL;
461 if (optlen > 64*1024)
462 break;
463
464 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
465 retv = -ENOBUFS;
466 if (opt == NULL)
467 break;
468
469 memset(opt, 0, sizeof(*opt));
470 opt->tot_len = sizeof(*opt) + optlen;
471 retv = -EFAULT;
472 if (copy_from_user(opt+1, optval, optlen))
473 goto done;
474
475 msg.msg_controllen = optlen;
476 msg.msg_control = (void*)(opt+1);
477
Maciej Żenczykowskiec0506d2011-08-28 12:35:31 +0000478 retv = datagram_send_ctl(net, sk, &msg, &fl6, opt, &junk, &junk,
Brian Haley13b52cd2010-04-23 11:26:08 +0000479 &junk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (retv)
481 goto done;
482update:
483 retv = 0;
YOSHIFUJI Hideakie7712f12008-04-13 23:21:52 -0700484 opt = ipv6_update_options(sk, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485done:
486 if (opt)
487 sock_kfree_s(sk, opt, opt->tot_len);
488 break;
489 }
490 case IPV6_UNICAST_HOPS:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900491 if (optlen < sizeof(int))
492 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (val > 255 || val < -1)
494 goto e_inval;
495 np->hop_limit = val;
496 retv = 0;
497 break;
498
499 case IPV6_MULTICAST_HOPS:
500 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900501 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900502 if (optlen < sizeof(int))
503 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (val > 255 || val < -1)
505 goto e_inval;
Li Wei2a38e6d2011-11-27 21:33:34 +0000506 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 retv = 0;
508 break;
509
510 case IPV6_MULTICAST_LOOP:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900511 if (optlen < sizeof(int))
512 goto e_inval;
YOSHIFUJI Hideaki28d44882008-06-12 03:14:51 +0900513 if (val != valbool)
514 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 np->mc_loop = valbool;
516 retv = 0;
517 break;
518
Erich E. Hooverc4062df2012-02-08 09:11:08 +0000519 case IPV6_UNICAST_IF:
520 {
521 struct net_device *dev = NULL;
522 int ifindex;
523
524 if (optlen != sizeof(int))
525 goto e_inval;
526
527 ifindex = (__force int)ntohl((__force __be32)val);
528 if (ifindex == 0) {
529 np->ucast_oif = 0;
530 retv = 0;
531 break;
532 }
533
534 dev = dev_get_by_index(net, ifindex);
535 retv = -EADDRNOTAVAIL;
536 if (!dev)
537 break;
538 dev_put(dev);
539
540 retv = -EINVAL;
541 if (sk->sk_bound_dev_if)
542 break;
543
544 np->ucast_oif = ifindex;
545 retv = 0;
546 break;
547 }
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 case IPV6_MULTICAST_IF:
550 if (sk->sk_type == SOCK_STREAM)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900551 break;
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900552 if (optlen < sizeof(int))
553 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Brian Haley4953f0f2007-10-11 14:39:29 -0700555 if (val) {
Eric Dumazet55b80502009-10-19 06:41:58 +0000556 struct net_device *dev;
557
Brian Haley4953f0f2007-10-11 14:39:29 -0700558 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
559 goto e_inval;
560
Eric Dumazet55b80502009-10-19 06:41:58 +0000561 dev = dev_get_by_index(net, val);
562 if (!dev) {
Brian Haley4953f0f2007-10-11 14:39:29 -0700563 retv = -ENODEV;
564 break;
565 }
Eric Dumazet55b80502009-10-19 06:41:58 +0000566 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568 np->mcast_oif = val;
569 retv = 0;
570 break;
571 case IPV6_ADD_MEMBERSHIP:
572 case IPV6_DROP_MEMBERSHIP:
573 {
574 struct ipv6_mreq mreq;
575
Wang Chena28398b2008-04-07 09:42:07 +0800576 if (optlen < sizeof(struct ipv6_mreq))
577 goto e_inval;
578
Flavio Leitnera96fb492007-08-24 22:16:39 -0700579 retv = -EPROTO;
580 if (inet_sk(sk)->is_icsk)
581 break;
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 retv = -EFAULT;
584 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
585 break;
586
587 if (optname == IPV6_ADD_MEMBERSHIP)
588 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
589 else
590 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
591 break;
592 }
593 case IPV6_JOIN_ANYCAST:
594 case IPV6_LEAVE_ANYCAST:
595 {
596 struct ipv6_mreq mreq;
597
Wang Chena28398b2008-04-07 09:42:07 +0800598 if (optlen < sizeof(struct ipv6_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 goto e_inval;
600
601 retv = -EFAULT;
602 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
603 break;
604
605 if (optname == IPV6_JOIN_ANYCAST)
606 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
607 else
608 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
609 break;
610 }
611 case MCAST_JOIN_GROUP:
612 case MCAST_LEAVE_GROUP:
613 {
614 struct group_req greq;
615 struct sockaddr_in6 *psin6;
616
Wang Chena28398b2008-04-07 09:42:07 +0800617 if (optlen < sizeof(struct group_req))
618 goto e_inval;
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 retv = -EFAULT;
621 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
622 break;
623 if (greq.gr_group.ss_family != AF_INET6) {
624 retv = -EADDRNOTAVAIL;
625 break;
626 }
627 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
628 if (optname == MCAST_JOIN_GROUP)
629 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
630 &psin6->sin6_addr);
631 else
632 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
633 &psin6->sin6_addr);
634 break;
635 }
636 case MCAST_JOIN_SOURCE_GROUP:
637 case MCAST_LEAVE_SOURCE_GROUP:
638 case MCAST_BLOCK_SOURCE:
639 case MCAST_UNBLOCK_SOURCE:
640 {
641 struct group_source_req greqs;
642 int omode, add;
643
Wang Chena28398b2008-04-07 09:42:07 +0800644 if (optlen < sizeof(struct group_source_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 goto e_inval;
646 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
647 retv = -EFAULT;
648 break;
649 }
650 if (greqs.gsr_group.ss_family != AF_INET6 ||
651 greqs.gsr_source.ss_family != AF_INET6) {
652 retv = -EADDRNOTAVAIL;
653 break;
654 }
655 if (optname == MCAST_BLOCK_SOURCE) {
656 omode = MCAST_EXCLUDE;
657 add = 1;
658 } else if (optname == MCAST_UNBLOCK_SOURCE) {
659 omode = MCAST_EXCLUDE;
660 add = 0;
661 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
662 struct sockaddr_in6 *psin6;
663
664 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
665 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
666 &psin6->sin6_addr);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700667 /* prior join w/ different source is ok */
668 if (retv && retv != -EADDRINUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 break;
670 omode = MCAST_INCLUDE;
671 add = 1;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700672 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 omode = MCAST_INCLUDE;
674 add = 0;
675 }
676 retv = ip6_mc_source(add, omode, sk, &greqs);
677 break;
678 }
679 case MCAST_MSFILTER:
680 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 struct group_filter *gsf;
682
683 if (optlen < GROUP_FILTER_SIZE(0))
684 goto e_inval;
685 if (optlen > sysctl_optmem_max) {
686 retv = -ENOBUFS;
687 break;
688 }
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800689 gsf = kmalloc(optlen,GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700690 if (!gsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 retv = -ENOBUFS;
692 break;
693 }
694 retv = -EFAULT;
695 if (copy_from_user(gsf, optval, optlen)) {
696 kfree(gsf);
697 break;
698 }
699 /* numsrc >= (4G-140)/128 overflow in 32 bits */
700 if (gsf->gf_numsrc >= 0x1ffffffU ||
701 gsf->gf_numsrc > sysctl_mld_max_msf) {
702 kfree(gsf);
703 retv = -ENOBUFS;
704 break;
705 }
706 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
707 kfree(gsf);
708 retv = -EINVAL;
709 break;
710 }
711 retv = ip6_mc_msfilter(sk, gsf);
712 kfree(gsf);
713
714 break;
715 }
716 case IPV6_ROUTER_ALERT:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900717 if (optlen < sizeof(int))
718 goto e_inval;
Denis V. Lunev725a8ff2008-07-19 00:28:58 -0700719 retv = ip6_ra_control(sk, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 break;
721 case IPV6_MTU_DISCOVER:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900722 if (optlen < sizeof(int))
723 goto e_inval;
John Dykstra0eae7502009-10-19 21:53:53 -0700724 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 goto e_inval;
726 np->pmtudisc = val;
727 retv = 0;
728 break;
729 case IPV6_MTU:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900730 if (optlen < sizeof(int))
731 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (val && val < IPV6_MIN_MTU)
733 goto e_inval;
734 np->frag_size = val;
735 retv = 0;
736 break;
737 case IPV6_RECVERR:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900738 if (optlen < sizeof(int))
739 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 np->recverr = valbool;
741 if (!val)
742 skb_queue_purge(&sk->sk_error_queue);
743 retv = 0;
744 break;
745 case IPV6_FLOWINFO_SEND:
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900746 if (optlen < sizeof(int))
747 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 np->sndflow = valbool;
749 retv = 0;
750 break;
751 case IPV6_FLOWLABEL_MGR:
752 retv = ipv6_flowlabel_opt(sk, optval, optlen);
753 break;
754 case IPV6_IPSEC_POLICY:
755 case IPV6_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700756 retv = -EPERM;
757 if (!capable(CAP_NET_ADMIN))
758 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 retv = xfrm_user_policy(sk, optname, optval, optlen);
760 break;
761
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900762 case IPV6_ADDR_PREFERENCES:
763 {
764 unsigned int pref = 0;
765 unsigned int prefmask = ~0;
766
YOSHIFUJI Hideakib2a9d7c2008-04-12 12:59:42 +0900767 if (optlen < sizeof(int))
768 goto e_inval;
769
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900770 retv = -EINVAL;
771
772 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
773 switch (val & (IPV6_PREFER_SRC_PUBLIC|
774 IPV6_PREFER_SRC_TMP|
775 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
776 case IPV6_PREFER_SRC_PUBLIC:
777 pref |= IPV6_PREFER_SRC_PUBLIC;
778 break;
779 case IPV6_PREFER_SRC_TMP:
780 pref |= IPV6_PREFER_SRC_TMP;
781 break;
782 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
783 break;
784 case 0:
785 goto pref_skip_pubtmp;
786 default:
787 goto e_inval;
788 }
789
790 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
791 IPV6_PREFER_SRC_TMP);
792pref_skip_pubtmp:
793
794 /* check HOME/COA conflicts */
795 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
796 case IPV6_PREFER_SRC_HOME:
797 break;
798 case IPV6_PREFER_SRC_COA:
799 pref |= IPV6_PREFER_SRC_COA;
800 case 0:
801 goto pref_skip_coa;
802 default:
803 goto e_inval;
804 }
805
806 prefmask &= ~IPV6_PREFER_SRC_COA;
807pref_skip_coa:
808
809 /* check CGA/NONCGA conflicts */
810 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
811 case IPV6_PREFER_SRC_CGA:
812 case IPV6_PREFER_SRC_NONCGA:
813 case 0:
814 break;
815 default:
816 goto e_inval;
817 }
818
819 np->srcprefs = (np->srcprefs & prefmask) | pref;
820 retv = 0;
821
822 break;
823 }
Stephen Hemmingere802af92010-04-22 15:24:53 -0700824 case IPV6_MINHOPCOUNT:
825 if (optlen < sizeof(int))
826 goto e_inval;
827 if (val < 0 || val > 255)
828 goto e_inval;
829 np->min_hopcount = val;
Brian Haley793b14732010-04-23 11:26:07 +0000830 break;
831 case IPV6_DONTFRAG:
832 np->dontfrag = valbool;
Stephen Hemmingere802af92010-04-22 15:24:53 -0700833 retv = 0;
834 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 release_sock(sk);
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return retv;
840
841e_inval:
842 release_sock(sk);
843 return -EINVAL;
844}
845
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800846int ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700847 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800848{
849 int err;
850
851 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
852 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
853
854 if (level != SOL_IPV6)
855 return -ENOPROTOOPT;
856
857 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
858#ifdef CONFIG_NETFILTER
859 /* we need to exclude all possible ENOPROTOOPTs except default case */
860 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
861 optname != IPV6_XFRM_POLICY) {
862 lock_sock(sk);
863 err = nf_setsockopt(sk, PF_INET6, optname, optval,
864 optlen);
865 release_sock(sk);
866 }
867#endif
868 return err;
869}
870
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900871EXPORT_SYMBOL(ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800872
873#ifdef CONFIG_COMPAT
874int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700875 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800876{
877 int err;
878
879 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800880 if (udp_prot.compat_setsockopt != NULL)
881 return udp_prot.compat_setsockopt(sk, level, optname,
882 optval, optlen);
883 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800884 }
885
886 if (level != SOL_IPV6)
887 return -ENOPROTOOPT;
888
David L Stevensdae50292008-04-27 01:06:07 -0700889 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
890 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
891 ipv6_setsockopt);
892
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800893 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
894#ifdef CONFIG_NETFILTER
895 /* we need to exclude all possible ENOPROTOOPTs except default case */
896 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800897 optname != IPV6_XFRM_POLICY) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800898 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800899 err = compat_nf_setsockopt(sk, PF_INET6, optname,
900 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800901 release_sock(sk);
902 }
903#endif
904 return err;
905}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800906
907EXPORT_SYMBOL(compat_ipv6_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800908#endif
909
David S. Miller286930792007-03-07 12:50:46 -0800910static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700911 int optname, char __user *optval, int len)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900912{
David S. Miller286930792007-03-07 12:50:46 -0800913 struct ipv6_opt_hdr *hdr;
914
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700915 if (!opt)
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900916 return 0;
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -0700917
918 switch(optname) {
919 case IPV6_HOPOPTS:
920 hdr = opt->hopopt;
921 break;
922 case IPV6_RTHDRDSTOPTS:
923 hdr = opt->dst0opt;
924 break;
925 case IPV6_RTHDR:
926 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
927 break;
928 case IPV6_DSTOPTS:
929 hdr = opt->dst1opt;
930 break;
931 default:
932 return -EINVAL; /* should not happen */
933 }
934
935 if (!hdr)
936 return 0;
David S. Miller286930792007-03-07 12:50:46 -0800937
Chris Wrightd2b02ed2007-03-09 16:19:17 -0800938 len = min_t(unsigned int, len, ipv6_optlen(hdr));
Ilpo Järvinen660adc62007-08-15 15:07:30 -0700939 if (copy_to_user(optval, hdr, len))
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900940 return -EFAULT;
Yang Hongyang95b496b2008-05-28 16:27:28 +0800941 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900942}
943
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800944static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +0000945 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 struct ipv6_pinfo *np = inet6_sk(sk);
948 int len;
949 int val;
950
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900951 if (ip6_mroute_opt(optname))
952 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (get_user(len, optlen))
955 return -EFAULT;
956 switch (optname) {
957 case IPV6_ADDRFORM:
958 if (sk->sk_protocol != IPPROTO_UDP &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800959 sk->sk_protocol != IPPROTO_UDPLITE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 sk->sk_protocol != IPPROTO_TCP)
YOSHIFUJI Hideaki17176992008-06-12 03:27:26 +0900961 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (sk->sk_state != TCP_ESTABLISHED)
963 return -ENOTCONN;
964 val = sk->sk_family;
965 break;
966 case MCAST_MSFILTER:
967 {
968 struct group_filter gsf;
969 int err;
970
971 if (len < GROUP_FILTER_SIZE(0))
972 return -EINVAL;
973 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
974 return -EFAULT;
YOSHIFUJI Hideaki20c61fb2008-04-28 14:40:55 +0900975 if (gsf.gf_group.ss_family != AF_INET6)
976 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 lock_sock(sk);
978 err = ip6_mc_msfget(sk, &gsf,
979 (struct group_filter __user *)optval, optlen);
980 release_sock(sk);
981 return err;
982 }
983
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900984 case IPV6_2292PKTOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 {
986 struct msghdr msg;
987 struct sk_buff *skb;
988
989 if (sk->sk_type != SOCK_STREAM)
990 return -ENOPROTOOPT;
991
992 msg.msg_control = optval;
993 msg.msg_controllen = len;
Daniel Baluta98e77432011-08-19 03:19:07 -0700994 msg.msg_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 lock_sock(sk);
997 skb = np->pktoptions;
998 if (skb)
999 atomic_inc(&skb->users);
1000 release_sock(sk);
1001
1002 if (skb) {
1003 int err = datagram_recv_ctl(sk, &msg, skb);
1004 kfree_skb(skb);
1005 if (err)
1006 return err;
1007 } else {
1008 if (np->rxopt.bits.rxinfo) {
1009 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001010 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1011 np->sticky_pktinfo.ipi6_ifindex;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001012 src_info.ipi6_addr = np->mcast_oif ? np->daddr : np->sticky_pktinfo.ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1014 }
1015 if (np->rxopt.bits.rxhlim) {
1016 int hlim = np->mcast_hops;
1017 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1018 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001019 if (np->rxopt.bits.rxtclass) {
1020 int tclass = np->rcv_tclass;
1021 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1022 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001023 if (np->rxopt.bits.rxoinfo) {
1024 struct in6_pktinfo src_info;
Yang Hongyangf250dcd2008-12-16 02:07:45 -08001025 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1026 np->sticky_pktinfo.ipi6_ifindex;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001027 src_info.ipi6_addr = np->mcast_oif ? np->daddr : np->sticky_pktinfo.ipi6_addr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001028 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1029 }
1030 if (np->rxopt.bits.rxohlim) {
1031 int hlim = np->mcast_hops;
1032 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035 len -= msg.msg_controllen;
1036 return put_user(len, optlen);
1037 }
1038 case IPV6_MTU:
1039 {
1040 struct dst_entry *dst;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001041
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001042 val = 0;
Eric Dumazetb6c67122010-04-08 23:03:29 +00001043 rcu_read_lock();
1044 dst = __sk_dst_get(sk);
1045 if (dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 val = dst_mtu(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001047 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (!val)
1049 return -ENOTCONN;
1050 break;
1051 }
1052
1053 case IPV6_V6ONLY:
1054 val = np->ipv6only;
1055 break;
1056
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001057 case IPV6_RECVPKTINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 val = np->rxopt.bits.rxinfo;
1059 break;
1060
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001061 case IPV6_2292PKTINFO:
1062 val = np->rxopt.bits.rxoinfo;
1063 break;
1064
1065 case IPV6_RECVHOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 val = np->rxopt.bits.rxhlim;
1067 break;
1068
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001069 case IPV6_2292HOPLIMIT:
1070 val = np->rxopt.bits.rxohlim;
1071 break;
1072
1073 case IPV6_RECVRTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 val = np->rxopt.bits.srcrt;
1075 break;
1076
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001077 case IPV6_2292RTHDR:
1078 val = np->rxopt.bits.osrcrt;
1079 break;
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001082 case IPV6_RTHDRDSTOPTS:
1083 case IPV6_RTHDR:
1084 case IPV6_DSTOPTS:
1085 {
1086
1087 lock_sock(sk);
David S. Miller286930792007-03-07 12:50:46 -08001088 len = ipv6_getsockopt_sticky(sk, np->opt,
YOSHIFUJI Hideaki4c6510a2007-03-18 17:35:57 -07001089 optname, optval, len);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001090 release_sock(sk);
Yang Hongyang05335c22008-05-28 16:23:47 +08001091 /* check if ipv6_getsockopt_sticky() returns err code */
1092 if (len < 0)
1093 return len;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001094 return put_user(len, optlen);
1095 }
1096
1097 case IPV6_RECVHOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 val = np->rxopt.bits.hopopts;
1099 break;
1100
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001101 case IPV6_2292HOPOPTS:
1102 val = np->rxopt.bits.ohopopts;
1103 break;
1104
1105 case IPV6_RECVDSTOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 val = np->rxopt.bits.dstopts;
1107 break;
1108
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001109 case IPV6_2292DSTOPTS:
1110 val = np->rxopt.bits.odstopts;
1111 break;
1112
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001113 case IPV6_TCLASS:
1114 val = np->tclass;
1115 break;
1116
1117 case IPV6_RECVTCLASS:
1118 val = np->rxopt.bits.rxtclass;
1119 break;
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 case IPV6_FLOWINFO:
1122 val = np->rxopt.bits.rxflow;
1123 break;
1124
Brian Haley793b14732010-04-23 11:26:07 +00001125 case IPV6_RECVPATHMTU:
1126 val = np->rxopt.bits.rxpmtu;
1127 break;
1128
1129 case IPV6_PATHMTU:
1130 {
1131 struct dst_entry *dst;
1132 struct ip6_mtuinfo mtuinfo;
1133
1134 if (len < sizeof(mtuinfo))
1135 return -EINVAL;
1136
1137 len = sizeof(mtuinfo);
1138 memset(&mtuinfo, 0, sizeof(mtuinfo));
1139
1140 rcu_read_lock();
1141 dst = __sk_dst_get(sk);
1142 if (dst)
1143 mtuinfo.ip6m_mtu = dst_mtu(dst);
1144 rcu_read_unlock();
1145 if (!mtuinfo.ip6m_mtu)
1146 return -ENOTCONN;
1147
1148 if (put_user(len, optlen))
1149 return -EFAULT;
1150 if (copy_to_user(optval, &mtuinfo, len))
1151 return -EFAULT;
1152
1153 return 0;
1154 break;
1155 }
1156
Balazs Scheidler6c468622010-10-21 16:08:28 +02001157 case IPV6_TRANSPARENT:
1158 val = inet_sk(sk)->transparent;
1159 break;
1160
1161 case IPV6_RECVORIGDSTADDR:
1162 val = np->rxopt.bits.rxorigdstaddr;
1163 break;
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 case IPV6_UNICAST_HOPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 case IPV6_MULTICAST_HOPS:
Brian Haleybefffe92006-12-12 17:09:49 -08001167 {
1168 struct dst_entry *dst;
1169
1170 if (optname == IPV6_UNICAST_HOPS)
1171 val = np->hop_limit;
1172 else
1173 val = np->mcast_hops;
1174
Eric Dumazetb6c67122010-04-08 23:03:29 +00001175 if (val < 0) {
1176 rcu_read_lock();
1177 dst = __sk_dst_get(sk);
1178 if (dst)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -04001179 val = ip6_dst_hoplimit(dst);
Eric Dumazetb6c67122010-04-08 23:03:29 +00001180 rcu_read_unlock();
Brian Haleybefffe92006-12-12 17:09:49 -08001181 }
Eric Dumazetb6c67122010-04-08 23:03:29 +00001182
Brian Haleybefffe92006-12-12 17:09:49 -08001183 if (val < 0)
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001184 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 break;
Brian Haleybefffe92006-12-12 17:09:49 -08001186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 case IPV6_MULTICAST_LOOP:
1189 val = np->mc_loop;
1190 break;
1191
1192 case IPV6_MULTICAST_IF:
1193 val = np->mcast_oif;
1194 break;
1195
Erich E. Hooverc4062df2012-02-08 09:11:08 +00001196 case IPV6_UNICAST_IF:
1197 val = (__force int)htonl((__u32) np->ucast_oif);
1198 break;
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 case IPV6_MTU_DISCOVER:
1201 val = np->pmtudisc;
1202 break;
1203
1204 case IPV6_RECVERR:
1205 val = np->recverr;
1206 break;
1207
1208 case IPV6_FLOWINFO_SEND:
1209 val = np->sndflow;
1210 break;
1211
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001212 case IPV6_ADDR_PREFERENCES:
1213 val = 0;
1214
1215 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1216 val |= IPV6_PREFER_SRC_TMP;
1217 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1218 val |= IPV6_PREFER_SRC_PUBLIC;
1219 else {
1220 /* XXX: should we return system default? */
1221 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1222 }
1223
1224 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1225 val |= IPV6_PREFER_SRC_COA;
1226 else
1227 val |= IPV6_PREFER_SRC_HOME;
1228 break;
1229
Stephen Hemmingere802af92010-04-22 15:24:53 -07001230 case IPV6_MINHOPCOUNT:
1231 val = np->min_hopcount;
1232 break;
1233
Brian Haley793b14732010-04-23 11:26:07 +00001234 case IPV6_DONTFRAG:
1235 val = np->dontfrag;
1236 break;
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 default:
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001239 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
1241 len = min_t(unsigned int, sizeof(int), len);
1242 if(put_user(len, optlen))
1243 return -EFAULT;
1244 if(copy_to_user(optval,&val,len))
1245 return -EFAULT;
1246 return 0;
1247}
1248
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001249int ipv6_getsockopt(struct sock *sk, int level, int optname,
1250 char __user *optval, int __user *optlen)
1251{
1252 int err;
1253
1254 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1255 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1256
1257 if(level != SOL_IPV6)
1258 return -ENOPROTOOPT;
1259
Daniel Baluta98e77432011-08-19 03:19:07 -07001260 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001261#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001262 /* we need to exclude all possible ENOPROTOOPTs except default case */
1263 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001264 int len;
1265
1266 if (get_user(len, optlen))
1267 return -EFAULT;
1268
1269 lock_sock(sk);
1270 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1271 &len);
1272 release_sock(sk);
1273 if (err >= 0)
1274 err = put_user(len, optlen);
1275 }
1276#endif
1277 return err;
1278}
1279
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001280EXPORT_SYMBOL(ipv6_getsockopt);
1281
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001282#ifdef CONFIG_COMPAT
1283int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001284 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001285{
1286 int err;
1287
1288 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001289 if (udp_prot.compat_getsockopt != NULL)
1290 return udp_prot.compat_getsockopt(sk, level, optname,
1291 optval, optlen);
1292 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001293 }
1294
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001295 if (level != SOL_IPV6)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001296 return -ENOPROTOOPT;
1297
Pavel Emelyanov80991792008-04-30 14:49:54 -07001298 if (optname == MCAST_MSFILTER)
1299 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1300 ipv6_getsockopt);
1301
Daniel Baluta98e77432011-08-19 03:19:07 -07001302 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1303 MSG_CMSG_COMPAT);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001304#ifdef CONFIG_NETFILTER
Wei Yongjuncf6fc4a2007-12-16 13:39:57 -08001305 /* we need to exclude all possible ENOPROTOOPTs except default case */
1306 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001307 int len;
1308
1309 if (get_user(len, optlen))
1310 return -EFAULT;
1311
1312 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001313 err = compat_nf_getsockopt(sk, PF_INET6,
1314 optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001315 release_sock(sk);
1316 if (err >= 0)
1317 err = put_user(len, optlen);
1318 }
1319#endif
1320 return err;
1321}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001322
1323EXPORT_SYMBOL(compat_ipv6_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001324#endif
1325