]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipv6/ipv6_sockglue.c
[IPV6] MROUTE: Do not call ipv6_find_idev() directly.
[mirror_ubuntu-bionic-kernel.git] / net / ipv6 / ipv6_sockglue.c
CommitLineData
1da177e4
LT
1/*
2 * IPv6 BSD socket options interface
1ab1457c 3 * Linux INET6 implementation
1da177e4
LT
4 *
5 * Authors:
1ab1457c 6 * Pedro Roque <roque@di.fc.ul.pt>
1da177e4
LT
7 *
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 *
1da177e4
LT
19 * 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>
4fc268d2 28#include <linux/capability.h>
1da177e4
LT
29#include <linux/errno.h>
30#include <linux/types.h>
31#include <linux/socket.h>
32#include <linux/sockios.h>
1da177e4
LT
33#include <linux/net.h>
34#include <linux/in6.h>
7bc570c8 35#include <linux/mroute6.h>
1da177e4
LT
36#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>
ba4e58ec 53#include <net/udplite.h>
1da177e4
LT
54#include <net/xfrm.h>
55
56#include <asm/uaccess.h>
57
ba89966c 58DEFINE_SNMP_STAT(struct ipstats_mib, ipv6_statistics) __read_mostly;
1da177e4 59
1da177e4
LT
60struct 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);
a51482bd 78 kfree(new_ra);
1da177e4
LT
79 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
3fdadf7d 106static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
1da177e4
LT
107 char __user *optval, int optlen)
108{
109 struct ipv6_pinfo *np = inet6_sk(sk);
3b1e0a65 110 struct net *net = sock_net(sk);
1da177e4
LT
111 int val, valbool;
112 int retv = -ENOPROTOOPT;
113
1da177e4
LT
114 if (optval == NULL)
115 val=0;
b2a9d7c2
YH
116 else {
117 if (optlen >= sizeof(int)) {
118 if (get_user(val, (int __user *) optval))
119 return -EFAULT;
120 } else
121 val = 0;
122 }
1da177e4
LT
123
124 valbool = (val!=0);
125
7bc570c8
YH
126 if (ip6_mroute_opt(optname))
127 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
128
1da177e4
LT
129 lock_sock(sk);
130
131 switch (optname) {
132
133 case IPV6_ADDRFORM:
b2a9d7c2
YH
134 if (optlen < sizeof(int))
135 goto e_inval;
1da177e4
LT
136 if (val == PF_INET) {
137 struct ipv6_txoptions *opt;
138 struct sk_buff *pktopt;
139
140 if (sk->sk_protocol != IPPROTO_UDP &&
ba4e58ec 141 sk->sk_protocol != IPPROTO_UDPLITE &&
1da177e4
LT
142 sk->sk_protocol != IPPROTO_TCP)
143 break;
144
145 if (sk->sk_state != TCP_ESTABLISHED) {
146 retv = -ENOTCONN;
147 break;
148 }
149
150 if (ipv6_only_sock(sk) ||
e773e4fa 151 !ipv6_addr_v4mapped(&np->daddr)) {
1da177e4
LT
152 retv = -EADDRNOTAVAIL;
153 break;
154 }
155
156 fl6_free_socklist(sk);
157 ipv6_sock_mc_close(sk);
158
e6848976
ACM
159 /*
160 * Sock is moving from IPv6 to IPv4 (sk_prot), so
161 * remove it from the refcnt debug socks count in the
162 * original family...
163 */
164 sk_refcnt_debug_dec(sk);
165
1da177e4 166 if (sk->sk_protocol == IPPROTO_TCP) {
d83d8461 167 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 168 local_bh_disable();
c29a0bc4
PE
169 sock_prot_inuse_add(net, sk->sk_prot, -1);
170 sock_prot_inuse_add(net, &tcp_prot, 1);
1da177e4
LT
171 local_bh_enable();
172 sk->sk_prot = &tcp_prot;
d83d8461 173 icsk->icsk_af_ops = &ipv4_specific;
1da177e4
LT
174 sk->sk_socket->ops = &inet_stream_ops;
175 sk->sk_family = PF_INET;
d83d8461 176 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
1da177e4 177 } else {
ba4e58ec
GR
178 struct proto *prot = &udp_prot;
179
db8dac20 180 if (sk->sk_protocol == IPPROTO_UDPLITE)
ba4e58ec 181 prot = &udplite_prot;
1da177e4 182 local_bh_disable();
c29a0bc4
PE
183 sock_prot_inuse_add(net, sk->sk_prot, -1);
184 sock_prot_inuse_add(net, prot, 1);
1da177e4 185 local_bh_enable();
ba4e58ec 186 sk->sk_prot = prot;
1da177e4
LT
187 sk->sk_socket->ops = &inet_dgram_ops;
188 sk->sk_family = PF_INET;
189 }
190 opt = xchg(&np->opt, NULL);
191 if (opt)
192 sock_kfree_s(sk, opt, opt->tot_len);
193 pktopt = xchg(&np->pktoptions, NULL);
194 if (pktopt)
195 kfree_skb(pktopt);
196
197 sk->sk_destruct = inet_sock_destruct;
e6848976
ACM
198 /*
199 * ... and add it to the refcnt debug socks count
200 * in the new family. -acme
201 */
202 sk_refcnt_debug_inc(sk);
1da177e4
LT
203 module_put(THIS_MODULE);
204 retv = 0;
205 break;
206 }
207 goto e_inval;
208
209 case IPV6_V6ONLY:
b2a9d7c2
YH
210 if (optlen < sizeof(int) ||
211 inet_sk(sk)->num)
1da177e4
LT
212 goto e_inval;
213 np->ipv6only = valbool;
214 retv = 0;
215 break;
216
333fad53 217 case IPV6_RECVPKTINFO:
b2a9d7c2
YH
218 if (optlen < sizeof(int))
219 goto e_inval;
1da177e4
LT
220 np->rxopt.bits.rxinfo = valbool;
221 retv = 0;
222 break;
1ab1457c 223
333fad53 224 case IPV6_2292PKTINFO:
b2a9d7c2
YH
225 if (optlen < sizeof(int))
226 goto e_inval;
333fad53
YH
227 np->rxopt.bits.rxoinfo = valbool;
228 retv = 0;
229 break;
1da177e4 230
333fad53 231 case IPV6_RECVHOPLIMIT:
b2a9d7c2
YH
232 if (optlen < sizeof(int))
233 goto e_inval;
1da177e4
LT
234 np->rxopt.bits.rxhlim = valbool;
235 retv = 0;
236 break;
237
333fad53 238 case IPV6_2292HOPLIMIT:
b2a9d7c2
YH
239 if (optlen < sizeof(int))
240 goto e_inval;
333fad53
YH
241 np->rxopt.bits.rxohlim = valbool;
242 retv = 0;
243 break;
244
245 case IPV6_RECVRTHDR:
b2a9d7c2
YH
246 if (optlen < sizeof(int))
247 goto e_inval;
4c752098 248 np->rxopt.bits.srcrt = valbool;
1da177e4
LT
249 retv = 0;
250 break;
251
333fad53 252 case IPV6_2292RTHDR:
b2a9d7c2
YH
253 if (optlen < sizeof(int))
254 goto e_inval;
4c752098 255 np->rxopt.bits.osrcrt = valbool;
333fad53
YH
256 retv = 0;
257 break;
258
259 case IPV6_RECVHOPOPTS:
b2a9d7c2
YH
260 if (optlen < sizeof(int))
261 goto e_inval;
1da177e4
LT
262 np->rxopt.bits.hopopts = valbool;
263 retv = 0;
264 break;
265
333fad53 266 case IPV6_2292HOPOPTS:
b2a9d7c2
YH
267 if (optlen < sizeof(int))
268 goto e_inval;
333fad53
YH
269 np->rxopt.bits.ohopopts = valbool;
270 retv = 0;
271 break;
272
273 case IPV6_RECVDSTOPTS:
b2a9d7c2
YH
274 if (optlen < sizeof(int))
275 goto e_inval;
1da177e4
LT
276 np->rxopt.bits.dstopts = valbool;
277 retv = 0;
278 break;
279
333fad53 280 case IPV6_2292DSTOPTS:
b2a9d7c2
YH
281 if (optlen < sizeof(int))
282 goto e_inval;
333fad53
YH
283 np->rxopt.bits.odstopts = valbool;
284 retv = 0;
285 break;
286
41a1f8ea 287 case IPV6_TCLASS:
b2a9d7c2
YH
288 if (optlen < sizeof(int))
289 goto e_inval;
d0ee011f 290 if (val < -1 || val > 0xff)
41a1f8ea
YH
291 goto e_inval;
292 np->tclass = val;
293 retv = 0;
294 break;
1ab1457c 295
41a1f8ea 296 case IPV6_RECVTCLASS:
b2a9d7c2
YH
297 if (optlen < sizeof(int))
298 goto e_inval;
41a1f8ea
YH
299 np->rxopt.bits.rxtclass = valbool;
300 retv = 0;
301 break;
302
1da177e4 303 case IPV6_FLOWINFO:
b2a9d7c2
YH
304 if (optlen < sizeof(int))
305 goto e_inval;
1da177e4
LT
306 np->rxopt.bits.rxflow = valbool;
307 retv = 0;
308 break;
309
333fad53
YH
310 case IPV6_HOPOPTS:
311 case IPV6_RTHDRDSTOPTS:
312 case IPV6_RTHDR:
313 case IPV6_DSTOPTS:
314 {
315 struct ipv6_txoptions *opt;
316 if (optlen == 0)
cb422c46 317 optval = NULL;
333fad53
YH
318
319 /* hop-by-hop / destination options are privileged option */
320 retv = -EPERM;
321 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
322 break;
323
b2a9d7c2
YH
324 if (optlen < sizeof(struct ipv6_opt_hdr) ||
325 optlen & 0x7 || optlen > 8 * 255)
326 goto e_inval;
333fad53
YH
327
328 opt = ipv6_renew_options(sk, np->opt, optname,
329 (struct ipv6_opt_hdr __user *)optval,
330 optlen);
331 if (IS_ERR(opt)) {
332 retv = PTR_ERR(opt);
333 break;
334 }
335
336 /* routing header option needs extra check */
dfee0a72 337 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
333fad53 338 struct ipv6_rt_hdr *rthdr = opt->srcrt;
280a9d34 339 switch (rthdr->type) {
59fbb3a6 340#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
280a9d34 341 case IPV6_SRCRT_TYPE_2:
280a9d34 342 break;
bb4dbf9e 343#endif
280a9d34 344 default:
333fad53 345 goto sticky_done;
280a9d34
MN
346 }
347
333fad53
YH
348 if ((rthdr->hdrlen & 1) ||
349 (rthdr->hdrlen >> 1) != rthdr->segments_left)
350 goto sticky_done;
351 }
352
353 retv = 0;
d83d8461 354 if (inet_sk(sk)->is_icsk) {
333fad53 355 if (opt) {
d83d8461 356 struct inet_connection_sock *icsk = inet_csk(sk);
333fad53
YH
357 if (!((1 << sk->sk_state) &
358 (TCPF_LISTEN | TCPF_CLOSE))
359 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
d83d8461
ACM
360 icsk->icsk_ext_hdr_len =
361 opt->opt_flen + opt->opt_nflen;
362 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
333fad53
YH
363 }
364 }
365 opt = xchg(&np->opt, opt);
366 sk_dst_reset(sk);
367 } else {
368 write_lock(&sk->sk_dst_lock);
369 opt = xchg(&np->opt, opt);
370 write_unlock(&sk->sk_dst_lock);
371 sk_dst_reset(sk);
372 }
373sticky_done:
374 if (opt)
375 sock_kfree_s(sk, opt, opt->tot_len);
376 break;
377 }
378
379 case IPV6_2292PKTOPTIONS:
1da177e4
LT
380 {
381 struct ipv6_txoptions *opt = NULL;
382 struct msghdr msg;
383 struct flowi fl;
384 int junk;
385
386 fl.fl6_flowlabel = 0;
387 fl.oif = sk->sk_bound_dev_if;
388
389 if (optlen == 0)
390 goto update;
391
392 /* 1K is probably excessive
393 * 1K is surely not enough, 2K per standard header is 16K.
394 */
395 retv = -EINVAL;
396 if (optlen > 64*1024)
397 break;
398
399 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
400 retv = -ENOBUFS;
401 if (opt == NULL)
402 break;
403
404 memset(opt, 0, sizeof(*opt));
405 opt->tot_len = sizeof(*opt) + optlen;
406 retv = -EFAULT;
407 if (copy_from_user(opt+1, optval, optlen))
408 goto done;
409
410 msg.msg_controllen = optlen;
411 msg.msg_control = (void*)(opt+1);
412
41a1f8ea 413 retv = datagram_send_ctl(&msg, &fl, opt, &junk, &junk);
1da177e4
LT
414 if (retv)
415 goto done;
416update:
417 retv = 0;
d83d8461 418 if (inet_sk(sk)->is_icsk) {
1da177e4 419 if (opt) {
d83d8461 420 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4
LT
421 if (!((1 << sk->sk_state) &
422 (TCPF_LISTEN | TCPF_CLOSE))
423 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
d83d8461
ACM
424 icsk->icsk_ext_hdr_len =
425 opt->opt_flen + opt->opt_nflen;
426 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
1da177e4
LT
427 }
428 }
429 opt = xchg(&np->opt, opt);
430 sk_dst_reset(sk);
431 } else {
432 write_lock(&sk->sk_dst_lock);
433 opt = xchg(&np->opt, opt);
434 write_unlock(&sk->sk_dst_lock);
435 sk_dst_reset(sk);
436 }
437
438done:
439 if (opt)
440 sock_kfree_s(sk, opt, opt->tot_len);
441 break;
442 }
443 case IPV6_UNICAST_HOPS:
b2a9d7c2
YH
444 if (optlen < sizeof(int))
445 goto e_inval;
1da177e4
LT
446 if (val > 255 || val < -1)
447 goto e_inval;
448 np->hop_limit = val;
449 retv = 0;
450 break;
451
452 case IPV6_MULTICAST_HOPS:
453 if (sk->sk_type == SOCK_STREAM)
454 goto e_inval;
b2a9d7c2
YH
455 if (optlen < sizeof(int))
456 goto e_inval;
1da177e4
LT
457 if (val > 255 || val < -1)
458 goto e_inval;
459 np->mcast_hops = val;
460 retv = 0;
461 break;
462
463 case IPV6_MULTICAST_LOOP:
b2a9d7c2
YH
464 if (optlen < sizeof(int))
465 goto e_inval;
1da177e4
LT
466 np->mc_loop = valbool;
467 retv = 0;
468 break;
469
470 case IPV6_MULTICAST_IF:
471 if (sk->sk_type == SOCK_STREAM)
472 goto e_inval;
b2a9d7c2
YH
473 if (optlen < sizeof(int))
474 goto e_inval;
1da177e4 475
4953f0fc
BH
476 if (val) {
477 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
478 goto e_inval;
479
8a3edd80 480 if (__dev_get_by_index(net, val) == NULL) {
4953f0fc
BH
481 retv = -ENODEV;
482 break;
483 }
1da177e4
LT
484 }
485 np->mcast_oif = val;
486 retv = 0;
487 break;
488 case IPV6_ADD_MEMBERSHIP:
489 case IPV6_DROP_MEMBERSHIP:
490 {
491 struct ipv6_mreq mreq;
492
a28398ba
WC
493 if (optlen < sizeof(struct ipv6_mreq))
494 goto e_inval;
495
a96fb49b
FL
496 retv = -EPROTO;
497 if (inet_sk(sk)->is_icsk)
498 break;
499
1da177e4
LT
500 retv = -EFAULT;
501 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
502 break;
503
504 if (optname == IPV6_ADD_MEMBERSHIP)
505 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
506 else
507 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
508 break;
509 }
510 case IPV6_JOIN_ANYCAST:
511 case IPV6_LEAVE_ANYCAST:
512 {
513 struct ipv6_mreq mreq;
514
a28398ba 515 if (optlen < sizeof(struct ipv6_mreq))
1da177e4
LT
516 goto e_inval;
517
518 retv = -EFAULT;
519 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
520 break;
521
522 if (optname == IPV6_JOIN_ANYCAST)
523 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
524 else
525 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
526 break;
527 }
528 case MCAST_JOIN_GROUP:
529 case MCAST_LEAVE_GROUP:
530 {
531 struct group_req greq;
532 struct sockaddr_in6 *psin6;
533
a28398ba
WC
534 if (optlen < sizeof(struct group_req))
535 goto e_inval;
536
1da177e4
LT
537 retv = -EFAULT;
538 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
539 break;
540 if (greq.gr_group.ss_family != AF_INET6) {
541 retv = -EADDRNOTAVAIL;
542 break;
543 }
544 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
545 if (optname == MCAST_JOIN_GROUP)
546 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
547 &psin6->sin6_addr);
548 else
549 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
550 &psin6->sin6_addr);
551 break;
552 }
553 case MCAST_JOIN_SOURCE_GROUP:
554 case MCAST_LEAVE_SOURCE_GROUP:
555 case MCAST_BLOCK_SOURCE:
556 case MCAST_UNBLOCK_SOURCE:
557 {
558 struct group_source_req greqs;
559 int omode, add;
560
a28398ba 561 if (optlen < sizeof(struct group_source_req))
1da177e4
LT
562 goto e_inval;
563 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
564 retv = -EFAULT;
565 break;
566 }
567 if (greqs.gsr_group.ss_family != AF_INET6 ||
568 greqs.gsr_source.ss_family != AF_INET6) {
569 retv = -EADDRNOTAVAIL;
570 break;
571 }
572 if (optname == MCAST_BLOCK_SOURCE) {
573 omode = MCAST_EXCLUDE;
574 add = 1;
575 } else if (optname == MCAST_UNBLOCK_SOURCE) {
576 omode = MCAST_EXCLUDE;
577 add = 0;
578 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
579 struct sockaddr_in6 *psin6;
580
581 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
582 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
583 &psin6->sin6_addr);
c9e3e8b6
DS
584 /* prior join w/ different source is ok */
585 if (retv && retv != -EADDRINUSE)
1da177e4
LT
586 break;
587 omode = MCAST_INCLUDE;
588 add = 1;
c9e3e8b6 589 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1da177e4
LT
590 omode = MCAST_INCLUDE;
591 add = 0;
592 }
593 retv = ip6_mc_source(add, omode, sk, &greqs);
594 break;
595 }
596 case MCAST_MSFILTER:
597 {
1da177e4
LT
598 extern int sysctl_mld_max_msf;
599 struct group_filter *gsf;
600
601 if (optlen < GROUP_FILTER_SIZE(0))
602 goto e_inval;
603 if (optlen > sysctl_optmem_max) {
604 retv = -ENOBUFS;
605 break;
606 }
8b3a7005 607 gsf = kmalloc(optlen,GFP_KERNEL);
cfcabdcc 608 if (!gsf) {
1da177e4
LT
609 retv = -ENOBUFS;
610 break;
611 }
612 retv = -EFAULT;
613 if (copy_from_user(gsf, optval, optlen)) {
614 kfree(gsf);
615 break;
616 }
617 /* numsrc >= (4G-140)/128 overflow in 32 bits */
618 if (gsf->gf_numsrc >= 0x1ffffffU ||
619 gsf->gf_numsrc > sysctl_mld_max_msf) {
620 kfree(gsf);
621 retv = -ENOBUFS;
622 break;
623 }
624 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
625 kfree(gsf);
626 retv = -EINVAL;
627 break;
628 }
629 retv = ip6_mc_msfilter(sk, gsf);
630 kfree(gsf);
631
632 break;
633 }
634 case IPV6_ROUTER_ALERT:
b2a9d7c2
YH
635 if (optlen < sizeof(int))
636 goto e_inval;
1da177e4
LT
637 retv = ip6_ra_control(sk, val, NULL);
638 break;
639 case IPV6_MTU_DISCOVER:
b2a9d7c2
YH
640 if (optlen < sizeof(int))
641 goto e_inval;
628a5c56 642 if (val<0 || val>3)
1da177e4
LT
643 goto e_inval;
644 np->pmtudisc = val;
645 retv = 0;
646 break;
647 case IPV6_MTU:
b2a9d7c2
YH
648 if (optlen < sizeof(int))
649 goto e_inval;
1da177e4
LT
650 if (val && val < IPV6_MIN_MTU)
651 goto e_inval;
652 np->frag_size = val;
653 retv = 0;
654 break;
655 case IPV6_RECVERR:
b2a9d7c2
YH
656 if (optlen < sizeof(int))
657 goto e_inval;
1da177e4
LT
658 np->recverr = valbool;
659 if (!val)
660 skb_queue_purge(&sk->sk_error_queue);
661 retv = 0;
662 break;
663 case IPV6_FLOWINFO_SEND:
b2a9d7c2
YH
664 if (optlen < sizeof(int))
665 goto e_inval;
1da177e4
LT
666 np->sndflow = valbool;
667 retv = 0;
668 break;
669 case IPV6_FLOWLABEL_MGR:
670 retv = ipv6_flowlabel_opt(sk, optval, optlen);
671 break;
672 case IPV6_IPSEC_POLICY:
673 case IPV6_XFRM_POLICY:
6fc0b4a7
HX
674 retv = -EPERM;
675 if (!capable(CAP_NET_ADMIN))
676 break;
1da177e4
LT
677 retv = xfrm_user_policy(sk, optname, optval, optlen);
678 break;
679
7cbca67c
YH
680 case IPV6_ADDR_PREFERENCES:
681 {
682 unsigned int pref = 0;
683 unsigned int prefmask = ~0;
684
b2a9d7c2
YH
685 if (optlen < sizeof(int))
686 goto e_inval;
687
7cbca67c
YH
688 retv = -EINVAL;
689
690 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
691 switch (val & (IPV6_PREFER_SRC_PUBLIC|
692 IPV6_PREFER_SRC_TMP|
693 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
694 case IPV6_PREFER_SRC_PUBLIC:
695 pref |= IPV6_PREFER_SRC_PUBLIC;
696 break;
697 case IPV6_PREFER_SRC_TMP:
698 pref |= IPV6_PREFER_SRC_TMP;
699 break;
700 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
701 break;
702 case 0:
703 goto pref_skip_pubtmp;
704 default:
705 goto e_inval;
706 }
707
708 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
709 IPV6_PREFER_SRC_TMP);
710pref_skip_pubtmp:
711
712 /* check HOME/COA conflicts */
713 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
714 case IPV6_PREFER_SRC_HOME:
715 break;
716 case IPV6_PREFER_SRC_COA:
717 pref |= IPV6_PREFER_SRC_COA;
718 case 0:
719 goto pref_skip_coa;
720 default:
721 goto e_inval;
722 }
723
724 prefmask &= ~IPV6_PREFER_SRC_COA;
725pref_skip_coa:
726
727 /* check CGA/NONCGA conflicts */
728 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
729 case IPV6_PREFER_SRC_CGA:
730 case IPV6_PREFER_SRC_NONCGA:
731 case 0:
732 break;
733 default:
734 goto e_inval;
735 }
736
737 np->srcprefs = (np->srcprefs & prefmask) | pref;
738 retv = 0;
739
740 break;
741 }
1da177e4 742 }
7cbca67c 743
1da177e4
LT
744 release_sock(sk);
745
1da177e4
LT
746 return retv;
747
748e_inval:
749 release_sock(sk);
750 return -EINVAL;
751}
752
3fdadf7d
DM
753int ipv6_setsockopt(struct sock *sk, int level, int optname,
754 char __user *optval, int optlen)
755{
756 int err;
757
758 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
759 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
760
761 if (level != SOL_IPV6)
762 return -ENOPROTOOPT;
763
764 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
765#ifdef CONFIG_NETFILTER
766 /* we need to exclude all possible ENOPROTOOPTs except default case */
767 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
768 optname != IPV6_XFRM_POLICY) {
769 lock_sock(sk);
770 err = nf_setsockopt(sk, PF_INET6, optname, optval,
771 optlen);
772 release_sock(sk);
773 }
774#endif
775 return err;
776}
777
7159039a 778EXPORT_SYMBOL(ipv6_setsockopt);
3fdadf7d
DM
779
780#ifdef CONFIG_COMPAT
781int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
543d9cfe 782 char __user *optval, int optlen)
3fdadf7d
DM
783{
784 int err;
785
786 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
543d9cfe
ACM
787 if (udp_prot.compat_setsockopt != NULL)
788 return udp_prot.compat_setsockopt(sk, level, optname,
789 optval, optlen);
790 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
3fdadf7d
DM
791 }
792
793 if (level != SOL_IPV6)
794 return -ENOPROTOOPT;
795
796 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
797#ifdef CONFIG_NETFILTER
798 /* we need to exclude all possible ENOPROTOOPTs except default case */
799 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
543d9cfe 800 optname != IPV6_XFRM_POLICY) {
3fdadf7d 801 lock_sock(sk);
543d9cfe
ACM
802 err = compat_nf_setsockopt(sk, PF_INET6, optname,
803 optval, optlen);
3fdadf7d
DM
804 release_sock(sk);
805 }
806#endif
807 return err;
808}
543d9cfe
ACM
809
810EXPORT_SYMBOL(compat_ipv6_setsockopt);
3fdadf7d
DM
811#endif
812
28693079 813static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
4c6510a7 814 int optname, char __user *optval, int len)
333fad53 815{
28693079
DM
816 struct ipv6_opt_hdr *hdr;
817
4c6510a7
YH
818 if (!opt)
819 return 0;
820
821 switch(optname) {
822 case IPV6_HOPOPTS:
823 hdr = opt->hopopt;
824 break;
825 case IPV6_RTHDRDSTOPTS:
826 hdr = opt->dst0opt;
827 break;
828 case IPV6_RTHDR:
829 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
830 break;
831 case IPV6_DSTOPTS:
832 hdr = opt->dst1opt;
833 break;
834 default:
835 return -EINVAL; /* should not happen */
836 }
837
838 if (!hdr)
333fad53 839 return 0;
28693079 840
d2b02ed9 841 len = min_t(unsigned int, len, ipv6_optlen(hdr));
660adc6e 842 if (copy_to_user(optval, hdr, len))
333fad53 843 return -EFAULT;
e5268f12 844 return ipv6_optlen(hdr);
333fad53
YH
845}
846
3fdadf7d 847static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
1da177e4
LT
848 char __user *optval, int __user *optlen)
849{
850 struct ipv6_pinfo *np = inet6_sk(sk);
851 int len;
852 int val;
853
7bc570c8
YH
854 if (ip6_mroute_opt(optname))
855 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
856
1da177e4
LT
857 if (get_user(len, optlen))
858 return -EFAULT;
859 switch (optname) {
860 case IPV6_ADDRFORM:
861 if (sk->sk_protocol != IPPROTO_UDP &&
ba4e58ec 862 sk->sk_protocol != IPPROTO_UDPLITE &&
1da177e4
LT
863 sk->sk_protocol != IPPROTO_TCP)
864 return -EINVAL;
865 if (sk->sk_state != TCP_ESTABLISHED)
866 return -ENOTCONN;
867 val = sk->sk_family;
868 break;
869 case MCAST_MSFILTER:
870 {
871 struct group_filter gsf;
872 int err;
873
874 if (len < GROUP_FILTER_SIZE(0))
875 return -EINVAL;
876 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
877 return -EFAULT;
878 lock_sock(sk);
879 err = ip6_mc_msfget(sk, &gsf,
880 (struct group_filter __user *)optval, optlen);
881 release_sock(sk);
882 return err;
883 }
884
333fad53 885 case IPV6_2292PKTOPTIONS:
1da177e4
LT
886 {
887 struct msghdr msg;
888 struct sk_buff *skb;
889
890 if (sk->sk_type != SOCK_STREAM)
891 return -ENOPROTOOPT;
892
893 msg.msg_control = optval;
894 msg.msg_controllen = len;
895 msg.msg_flags = 0;
896
897 lock_sock(sk);
898 skb = np->pktoptions;
899 if (skb)
900 atomic_inc(&skb->users);
901 release_sock(sk);
902
903 if (skb) {
904 int err = datagram_recv_ctl(sk, &msg, skb);
905 kfree_skb(skb);
906 if (err)
907 return err;
908 } else {
909 if (np->rxopt.bits.rxinfo) {
910 struct in6_pktinfo src_info;
911 src_info.ipi6_ifindex = np->mcast_oif;
912 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
913 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
914 }
915 if (np->rxopt.bits.rxhlim) {
916 int hlim = np->mcast_hops;
917 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
918 }
333fad53
YH
919 if (np->rxopt.bits.rxoinfo) {
920 struct in6_pktinfo src_info;
921 src_info.ipi6_ifindex = np->mcast_oif;
922 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
923 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
924 }
925 if (np->rxopt.bits.rxohlim) {
926 int hlim = np->mcast_hops;
927 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
928 }
1da177e4
LT
929 }
930 len -= msg.msg_controllen;
931 return put_user(len, optlen);
932 }
933 case IPV6_MTU:
934 {
935 struct dst_entry *dst;
1ab1457c 936 val = 0;
1da177e4
LT
937 lock_sock(sk);
938 dst = sk_dst_get(sk);
939 if (dst) {
940 val = dst_mtu(dst);
941 dst_release(dst);
942 }
943 release_sock(sk);
944 if (!val)
945 return -ENOTCONN;
946 break;
947 }
948
949 case IPV6_V6ONLY:
950 val = np->ipv6only;
951 break;
952
333fad53 953 case IPV6_RECVPKTINFO:
1da177e4
LT
954 val = np->rxopt.bits.rxinfo;
955 break;
956
333fad53
YH
957 case IPV6_2292PKTINFO:
958 val = np->rxopt.bits.rxoinfo;
959 break;
960
961 case IPV6_RECVHOPLIMIT:
1da177e4
LT
962 val = np->rxopt.bits.rxhlim;
963 break;
964
333fad53
YH
965 case IPV6_2292HOPLIMIT:
966 val = np->rxopt.bits.rxohlim;
967 break;
968
969 case IPV6_RECVRTHDR:
1da177e4
LT
970 val = np->rxopt.bits.srcrt;
971 break;
972
333fad53
YH
973 case IPV6_2292RTHDR:
974 val = np->rxopt.bits.osrcrt;
975 break;
976
1da177e4 977 case IPV6_HOPOPTS:
333fad53
YH
978 case IPV6_RTHDRDSTOPTS:
979 case IPV6_RTHDR:
980 case IPV6_DSTOPTS:
981 {
982
983 lock_sock(sk);
28693079 984 len = ipv6_getsockopt_sticky(sk, np->opt,
4c6510a7 985 optname, optval, len);
333fad53
YH
986 release_sock(sk);
987 return put_user(len, optlen);
988 }
989
990 case IPV6_RECVHOPOPTS:
1da177e4
LT
991 val = np->rxopt.bits.hopopts;
992 break;
993
333fad53
YH
994 case IPV6_2292HOPOPTS:
995 val = np->rxopt.bits.ohopopts;
996 break;
997
998 case IPV6_RECVDSTOPTS:
1da177e4
LT
999 val = np->rxopt.bits.dstopts;
1000 break;
1001
333fad53
YH
1002 case IPV6_2292DSTOPTS:
1003 val = np->rxopt.bits.odstopts;
1004 break;
1005
41a1f8ea
YH
1006 case IPV6_TCLASS:
1007 val = np->tclass;
d0ee011f
RDC
1008 if (val < 0)
1009 val = 0;
41a1f8ea
YH
1010 break;
1011
1012 case IPV6_RECVTCLASS:
1013 val = np->rxopt.bits.rxtclass;
1014 break;
1015
1da177e4
LT
1016 case IPV6_FLOWINFO:
1017 val = np->rxopt.bits.rxflow;
1018 break;
1019
1020 case IPV6_UNICAST_HOPS:
1da177e4 1021 case IPV6_MULTICAST_HOPS:
befffe90
BH
1022 {
1023 struct dst_entry *dst;
1024
1025 if (optname == IPV6_UNICAST_HOPS)
1026 val = np->hop_limit;
1027 else
1028 val = np->mcast_hops;
1029
1030 dst = sk_dst_get(sk);
1031 if (dst) {
1032 if (val < 0)
6b75d090 1033 val = ip6_dst_hoplimit(dst);
befffe90
BH
1034 dst_release(dst);
1035 }
1036 if (val < 0)
1037 val = ipv6_devconf.hop_limit;
1da177e4 1038 break;
befffe90 1039 }
1da177e4
LT
1040
1041 case IPV6_MULTICAST_LOOP:
1042 val = np->mc_loop;
1043 break;
1044
1045 case IPV6_MULTICAST_IF:
1046 val = np->mcast_oif;
1047 break;
1048
1049 case IPV6_MTU_DISCOVER:
1050 val = np->pmtudisc;
1051 break;
1052
1053 case IPV6_RECVERR:
1054 val = np->recverr;
1055 break;
1056
1057 case IPV6_FLOWINFO_SEND:
1058 val = np->sndflow;
1059 break;
1060
7cbca67c
YH
1061 case IPV6_ADDR_PREFERENCES:
1062 val = 0;
1063
1064 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1065 val |= IPV6_PREFER_SRC_TMP;
1066 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1067 val |= IPV6_PREFER_SRC_PUBLIC;
1068 else {
1069 /* XXX: should we return system default? */
1070 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1071 }
1072
1073 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1074 val |= IPV6_PREFER_SRC_COA;
1075 else
1076 val |= IPV6_PREFER_SRC_HOME;
1077 break;
1078
1da177e4 1079 default:
cf6fc4a9 1080 return -ENOPROTOOPT;
1da177e4
LT
1081 }
1082 len = min_t(unsigned int, sizeof(int), len);
1083 if(put_user(len, optlen))
1084 return -EFAULT;
1085 if(copy_to_user(optval,&val,len))
1086 return -EFAULT;
1087 return 0;
1088}
1089
3fdadf7d
DM
1090int ipv6_getsockopt(struct sock *sk, int level, int optname,
1091 char __user *optval, int __user *optlen)
1092{
1093 int err;
1094
1095 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1096 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1097
1098 if(level != SOL_IPV6)
1099 return -ENOPROTOOPT;
1100
1101 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1102#ifdef CONFIG_NETFILTER
cf6fc4a9
WY
1103 /* we need to exclude all possible ENOPROTOOPTs except default case */
1104 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
3fdadf7d
DM
1105 int len;
1106
1107 if (get_user(len, optlen))
1108 return -EFAULT;
1109
1110 lock_sock(sk);
1111 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1112 &len);
1113 release_sock(sk);
1114 if (err >= 0)
1115 err = put_user(len, optlen);
1116 }
1117#endif
1118 return err;
1119}
1120
7159039a
YH
1121EXPORT_SYMBOL(ipv6_getsockopt);
1122
3fdadf7d
DM
1123#ifdef CONFIG_COMPAT
1124int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
543d9cfe 1125 char __user *optval, int __user *optlen)
3fdadf7d
DM
1126{
1127 int err;
1128
1129 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
543d9cfe
ACM
1130 if (udp_prot.compat_getsockopt != NULL)
1131 return udp_prot.compat_getsockopt(sk, level, optname,
1132 optval, optlen);
1133 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
3fdadf7d
DM
1134 }
1135
543d9cfe 1136 if (level != SOL_IPV6)
3fdadf7d
DM
1137 return -ENOPROTOOPT;
1138
1139 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1140#ifdef CONFIG_NETFILTER
cf6fc4a9
WY
1141 /* we need to exclude all possible ENOPROTOOPTs except default case */
1142 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
3fdadf7d
DM
1143 int len;
1144
1145 if (get_user(len, optlen))
1146 return -EFAULT;
1147
1148 lock_sock(sk);
543d9cfe
ACM
1149 err = compat_nf_getsockopt(sk, PF_INET6,
1150 optname, optval, &len);
3fdadf7d
DM
1151 release_sock(sk);
1152 if (err >= 0)
1153 err = put_user(len, optlen);
1154 }
1155#endif
1156 return err;
1157}
543d9cfe
ACM
1158
1159EXPORT_SYMBOL(compat_ipv6_getsockopt);
3fdadf7d
DM
1160#endif
1161