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