]>
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> | |
5a0e3ad6 | 39 | #include <linux/slab.h> |
1da177e4 LT |
40 | |
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> | |
ba4e58ec | 52 | #include <net/udplite.h> |
1da177e4 | 53 | #include <net/xfrm.h> |
dae50295 | 54 | #include <net/compat.h> |
1da177e4 LT |
55 | |
56 | #include <asm/uaccess.h> | |
57 | ||
1da177e4 LT |
58 | struct ip6_ra_chain *ip6_ra_chain; |
59 | DEFINE_RWLOCK(ip6_ra_lock); | |
60 | ||
725a8ff0 | 61 | int ip6_ra_control(struct sock *sk, int sel) |
1da177e4 LT |
62 | { |
63 | struct ip6_ra_chain *ra, *new_ra, **rap; | |
64 | ||
65 | /* RA packet may be delivered ONLY to IPPROTO_RAW socket */ | |
c720c7e8 | 66 | if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num != IPPROTO_RAW) |
1717699c | 67 | return -ENOPROTOOPT; |
1da177e4 LT |
68 | |
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); | |
a51482bd | 76 | kfree(new_ra); |
1da177e4 LT |
77 | return -EADDRINUSE; |
78 | } | |
79 | ||
80 | *rap = ra->next; | |
81 | write_unlock_bh(&ip6_ra_lock); | |
82 | ||
1da177e4 LT |
83 | 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; | |
1da177e4 LT |
94 | new_ra->next = ra; |
95 | *rap = new_ra; | |
96 | sock_hold(sk); | |
97 | write_unlock_bh(&ip6_ra_lock); | |
98 | return 0; | |
99 | } | |
100 | ||
e7712f1a YH |
101 | static |
102 | struct 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)) && | |
c720c7e8 | 108 | inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) { |
e7712f1a YH |
109 | 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 { | |
b6c6712a | 115 | spin_lock(&sk->sk_dst_lock); |
e7712f1a | 116 | opt = xchg(&inet6_sk(sk)->opt, opt); |
b6c6712a | 117 | spin_unlock(&sk->sk_dst_lock); |
e7712f1a YH |
118 | } |
119 | sk_dst_reset(sk); | |
120 | ||
121 | return opt; | |
122 | } | |
123 | ||
3fdadf7d | 124 | static int do_ipv6_setsockopt(struct sock *sk, int level, int optname, |
b7058842 | 125 | char __user *optval, unsigned int optlen) |
1da177e4 LT |
126 | { |
127 | struct ipv6_pinfo *np = inet6_sk(sk); | |
3b1e0a65 | 128 | struct net *net = sock_net(sk); |
1da177e4 LT |
129 | int val, valbool; |
130 | int retv = -ENOPROTOOPT; | |
131 | ||
1da177e4 LT |
132 | if (optval == NULL) |
133 | val=0; | |
b2a9d7c2 YH |
134 | else { |
135 | if (optlen >= sizeof(int)) { | |
136 | if (get_user(val, (int __user *) optval)) | |
137 | return -EFAULT; | |
138 | } else | |
139 | val = 0; | |
140 | } | |
1da177e4 LT |
141 | |
142 | valbool = (val!=0); | |
143 | ||
7bc570c8 YH |
144 | if (ip6_mroute_opt(optname)) |
145 | return ip6_mroute_setsockopt(sk, optname, optval, optlen); | |
146 | ||
1da177e4 LT |
147 | lock_sock(sk); |
148 | ||
149 | switch (optname) { | |
150 | ||
151 | case IPV6_ADDRFORM: | |
b2a9d7c2 YH |
152 | if (optlen < sizeof(int)) |
153 | goto e_inval; | |
1da177e4 LT |
154 | if (val == PF_INET) { |
155 | struct ipv6_txoptions *opt; | |
156 | struct sk_buff *pktopt; | |
157 | ||
49d074f4 DL |
158 | if (sk->sk_type == SOCK_RAW) |
159 | break; | |
160 | ||
9596cc82 DL |
161 | 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) | |
1da177e4 LT |
169 | break; |
170 | ||
171 | if (sk->sk_state != TCP_ESTABLISHED) { | |
172 | retv = -ENOTCONN; | |
173 | break; | |
174 | } | |
175 | ||
176 | if (ipv6_only_sock(sk) || | |
efe4208f | 177 | !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) { |
1da177e4 LT |
178 | retv = -EADDRNOTAVAIL; |
179 | break; | |
180 | } | |
181 | ||
182 | fl6_free_socklist(sk); | |
183 | ipv6_sock_mc_close(sk); | |
184 | ||
e6848976 ACM |
185 | /* |
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 | ||
1da177e4 | 192 | if (sk->sk_protocol == IPPROTO_TCP) { |
d83d8461 | 193 | struct inet_connection_sock *icsk = inet_csk(sk); |
1da177e4 | 194 | local_bh_disable(); |
c29a0bc4 PE |
195 | sock_prot_inuse_add(net, sk->sk_prot, -1); |
196 | sock_prot_inuse_add(net, &tcp_prot, 1); | |
1da177e4 LT |
197 | local_bh_enable(); |
198 | sk->sk_prot = &tcp_prot; | |
d83d8461 | 199 | icsk->icsk_af_ops = &ipv4_specific; |
1da177e4 LT |
200 | sk->sk_socket->ops = &inet_stream_ops; |
201 | sk->sk_family = PF_INET; | |
d83d8461 | 202 | tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); |
1da177e4 | 203 | } else { |
ba4e58ec GR |
204 | struct proto *prot = &udp_prot; |
205 | ||
db8dac20 | 206 | if (sk->sk_protocol == IPPROTO_UDPLITE) |
ba4e58ec | 207 | prot = &udplite_prot; |
1da177e4 | 208 | local_bh_disable(); |
c29a0bc4 PE |
209 | sock_prot_inuse_add(net, sk->sk_prot, -1); |
210 | sock_prot_inuse_add(net, prot, 1); | |
1da177e4 | 211 | local_bh_enable(); |
ba4e58ec | 212 | sk->sk_prot = prot; |
1da177e4 LT |
213 | 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); | |
800d55f1 | 220 | kfree_skb(pktopt); |
1da177e4 LT |
221 | |
222 | sk->sk_destruct = inet_sock_destruct; | |
e6848976 ACM |
223 | /* |
224 | * ... and add it to the refcnt debug socks count | |
225 | * in the new family. -acme | |
226 | */ | |
227 | sk_refcnt_debug_inc(sk); | |
1da177e4 LT |
228 | module_put(THIS_MODULE); |
229 | retv = 0; | |
230 | break; | |
231 | } | |
232 | goto e_inval; | |
233 | ||
234 | case IPV6_V6ONLY: | |
b2a9d7c2 | 235 | if (optlen < sizeof(int) || |
c720c7e8 | 236 | inet_sk(sk)->inet_num) |
1da177e4 | 237 | goto e_inval; |
9fe516ba | 238 | sk->sk_ipv6only = valbool; |
1da177e4 LT |
239 | retv = 0; |
240 | break; | |
241 | ||
333fad53 | 242 | case IPV6_RECVPKTINFO: |
b2a9d7c2 YH |
243 | if (optlen < sizeof(int)) |
244 | goto e_inval; | |
1da177e4 LT |
245 | np->rxopt.bits.rxinfo = valbool; |
246 | retv = 0; | |
247 | break; | |
1ab1457c | 248 | |
333fad53 | 249 | case IPV6_2292PKTINFO: |
b2a9d7c2 YH |
250 | if (optlen < sizeof(int)) |
251 | goto e_inval; | |
333fad53 YH |
252 | np->rxopt.bits.rxoinfo = valbool; |
253 | retv = 0; | |
254 | break; | |
1da177e4 | 255 | |
333fad53 | 256 | case IPV6_RECVHOPLIMIT: |
b2a9d7c2 YH |
257 | if (optlen < sizeof(int)) |
258 | goto e_inval; | |
1da177e4 LT |
259 | np->rxopt.bits.rxhlim = valbool; |
260 | retv = 0; | |
261 | break; | |
262 | ||
333fad53 | 263 | case IPV6_2292HOPLIMIT: |
b2a9d7c2 YH |
264 | if (optlen < sizeof(int)) |
265 | goto e_inval; | |
333fad53 YH |
266 | np->rxopt.bits.rxohlim = valbool; |
267 | retv = 0; | |
268 | break; | |
269 | ||
270 | case IPV6_RECVRTHDR: | |
b2a9d7c2 YH |
271 | if (optlen < sizeof(int)) |
272 | goto e_inval; | |
4c752098 | 273 | np->rxopt.bits.srcrt = valbool; |
1da177e4 LT |
274 | retv = 0; |
275 | break; | |
276 | ||
333fad53 | 277 | case IPV6_2292RTHDR: |
b2a9d7c2 YH |
278 | if (optlen < sizeof(int)) |
279 | goto e_inval; | |
4c752098 | 280 | np->rxopt.bits.osrcrt = valbool; |
333fad53 YH |
281 | retv = 0; |
282 | break; | |
283 | ||
284 | case IPV6_RECVHOPOPTS: | |
b2a9d7c2 YH |
285 | if (optlen < sizeof(int)) |
286 | goto e_inval; | |
1da177e4 LT |
287 | np->rxopt.bits.hopopts = valbool; |
288 | retv = 0; | |
289 | break; | |
290 | ||
333fad53 | 291 | case IPV6_2292HOPOPTS: |
b2a9d7c2 YH |
292 | if (optlen < sizeof(int)) |
293 | goto e_inval; | |
333fad53 YH |
294 | np->rxopt.bits.ohopopts = valbool; |
295 | retv = 0; | |
296 | break; | |
297 | ||
298 | case IPV6_RECVDSTOPTS: | |
b2a9d7c2 YH |
299 | if (optlen < sizeof(int)) |
300 | goto e_inval; | |
1da177e4 LT |
301 | np->rxopt.bits.dstopts = valbool; |
302 | retv = 0; | |
303 | break; | |
304 | ||
333fad53 | 305 | case IPV6_2292DSTOPTS: |
b2a9d7c2 YH |
306 | if (optlen < sizeof(int)) |
307 | goto e_inval; | |
333fad53 YH |
308 | np->rxopt.bits.odstopts = valbool; |
309 | retv = 0; | |
310 | break; | |
311 | ||
41a1f8ea | 312 | case IPV6_TCLASS: |
b2a9d7c2 YH |
313 | if (optlen < sizeof(int)) |
314 | goto e_inval; | |
d0ee011f | 315 | if (val < -1 || val > 0xff) |
41a1f8ea | 316 | goto e_inval; |
26ced1e4 GR |
317 | /* RFC 3542, 6.5: default traffic class of 0x0 */ |
318 | if (val == -1) | |
319 | val = 0; | |
41a1f8ea YH |
320 | np->tclass = val; |
321 | retv = 0; | |
322 | break; | |
1ab1457c | 323 | |
41a1f8ea | 324 | case IPV6_RECVTCLASS: |
b2a9d7c2 YH |
325 | if (optlen < sizeof(int)) |
326 | goto e_inval; | |
41a1f8ea YH |
327 | np->rxopt.bits.rxtclass = valbool; |
328 | retv = 0; | |
329 | break; | |
330 | ||
1da177e4 | 331 | case IPV6_FLOWINFO: |
b2a9d7c2 YH |
332 | if (optlen < sizeof(int)) |
333 | goto e_inval; | |
1da177e4 LT |
334 | np->rxopt.bits.rxflow = valbool; |
335 | retv = 0; | |
336 | break; | |
337 | ||
793b1473 BH |
338 | case IPV6_RECVPATHMTU: |
339 | if (optlen < sizeof(int)) | |
340 | goto e_inval; | |
341 | np->rxopt.bits.rxpmtu = valbool; | |
342 | retv = 0; | |
343 | break; | |
344 | ||
6c468622 | 345 | case IPV6_TRANSPARENT: |
af31f412 EB |
346 | if (valbool && !ns_capable(net->user_ns, CAP_NET_ADMIN) && |
347 | !ns_capable(net->user_ns, CAP_NET_RAW)) { | |
b889416b BS |
348 | retv = -EPERM; |
349 | break; | |
350 | } | |
6c468622 BS |
351 | if (optlen < sizeof(int)) |
352 | goto e_inval; | |
353 | /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */ | |
354 | inet_sk(sk)->transparent = valbool; | |
355 | retv = 0; | |
356 | break; | |
357 | ||
358 | case IPV6_RECVORIGDSTADDR: | |
359 | if (optlen < sizeof(int)) | |
360 | goto e_inval; | |
361 | np->rxopt.bits.rxorigdstaddr = valbool; | |
362 | retv = 0; | |
363 | break; | |
364 | ||
333fad53 YH |
365 | case IPV6_HOPOPTS: |
366 | case IPV6_RTHDRDSTOPTS: | |
367 | case IPV6_RTHDR: | |
368 | case IPV6_DSTOPTS: | |
369 | { | |
370 | struct ipv6_txoptions *opt; | |
aea7427f SW |
371 | |
372 | /* remove any sticky options header with a zero option | |
373 | * length, per RFC3542. | |
374 | */ | |
333fad53 | 375 | if (optlen == 0) |
cb422c46 | 376 | optval = NULL; |
cfb266c0 YH |
377 | else if (optval == NULL) |
378 | goto e_inval; | |
aea7427f SW |
379 | else if (optlen < sizeof(struct ipv6_opt_hdr) || |
380 | optlen & 0x7 || optlen > 8 * 255) | |
381 | goto e_inval; | |
333fad53 YH |
382 | |
383 | /* hop-by-hop / destination options are privileged option */ | |
384 | retv = -EPERM; | |
af31f412 | 385 | if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW)) |
333fad53 YH |
386 | break; |
387 | ||
333fad53 YH |
388 | opt = ipv6_renew_options(sk, np->opt, optname, |
389 | (struct ipv6_opt_hdr __user *)optval, | |
390 | optlen); | |
391 | if (IS_ERR(opt)) { | |
392 | retv = PTR_ERR(opt); | |
393 | break; | |
394 | } | |
395 | ||
396 | /* routing header option needs extra check */ | |
6e093d9d | 397 | retv = -EINVAL; |
dfee0a72 | 398 | if (optname == IPV6_RTHDR && opt && opt->srcrt) { |
333fad53 | 399 | struct ipv6_rt_hdr *rthdr = opt->srcrt; |
280a9d34 | 400 | switch (rthdr->type) { |
07a93626 | 401 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
280a9d34 | 402 | case IPV6_SRCRT_TYPE_2: |
6e093d9d BH |
403 | if (rthdr->hdrlen != 2 || |
404 | rthdr->segments_left != 1) | |
405 | goto sticky_done; | |
406 | ||
280a9d34 | 407 | break; |
bb4dbf9e | 408 | #endif |
280a9d34 | 409 | default: |
333fad53 | 410 | goto sticky_done; |
280a9d34 | 411 | } |
333fad53 YH |
412 | } |
413 | ||
414 | retv = 0; | |
e7712f1a | 415 | opt = ipv6_update_options(sk, opt); |
333fad53 YH |
416 | sticky_done: |
417 | if (opt) | |
418 | sock_kfree_s(sk, opt, opt->tot_len); | |
419 | break; | |
420 | } | |
421 | ||
b24a2516 YH |
422 | case IPV6_PKTINFO: |
423 | { | |
424 | struct in6_pktinfo pkt; | |
425 | ||
426 | if (optlen == 0) | |
427 | goto e_inval; | |
428 | else if (optlen < sizeof(struct in6_pktinfo) || optval == NULL) | |
429 | goto e_inval; | |
430 | ||
914d1164 | 431 | if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) { |
b24a2516 YH |
432 | retv = -EFAULT; |
433 | break; | |
434 | } | |
435 | if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if) | |
436 | goto e_inval; | |
437 | ||
438 | np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex; | |
4e3fd7a0 | 439 | np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr; |
b24a2516 YH |
440 | retv = 0; |
441 | break; | |
442 | } | |
443 | ||
333fad53 | 444 | case IPV6_2292PKTOPTIONS: |
1da177e4 LT |
445 | { |
446 | struct ipv6_txoptions *opt = NULL; | |
447 | struct msghdr msg; | |
4c9483b2 | 448 | struct flowi6 fl6; |
1da177e4 LT |
449 | int junk; |
450 | ||
4c9483b2 DM |
451 | memset(&fl6, 0, sizeof(fl6)); |
452 | fl6.flowi6_oif = sk->sk_bound_dev_if; | |
453 | fl6.flowi6_mark = sk->sk_mark; | |
1da177e4 LT |
454 | |
455 | if (optlen == 0) | |
456 | goto update; | |
457 | ||
458 | /* 1K is probably excessive | |
459 | * 1K is surely not enough, 2K per standard header is 16K. | |
460 | */ | |
461 | retv = -EINVAL; | |
462 | if (optlen > 64*1024) | |
463 | break; | |
464 | ||
465 | opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL); | |
466 | retv = -ENOBUFS; | |
467 | if (opt == NULL) | |
468 | break; | |
469 | ||
470 | memset(opt, 0, sizeof(*opt)); | |
471 | opt->tot_len = sizeof(*opt) + optlen; | |
472 | retv = -EFAULT; | |
473 | if (copy_from_user(opt+1, optval, optlen)) | |
474 | goto done; | |
475 | ||
476 | msg.msg_controllen = optlen; | |
477 | msg.msg_control = (void*)(opt+1); | |
478 | ||
73df66f8 TP |
479 | retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, opt, &junk, |
480 | &junk, &junk); | |
1da177e4 LT |
481 | if (retv) |
482 | goto done; | |
483 | update: | |
484 | retv = 0; | |
e7712f1a | 485 | opt = ipv6_update_options(sk, opt); |
1da177e4 LT |
486 | done: |
487 | if (opt) | |
488 | sock_kfree_s(sk, opt, opt->tot_len); | |
489 | break; | |
490 | } | |
491 | case IPV6_UNICAST_HOPS: | |
b2a9d7c2 YH |
492 | if (optlen < sizeof(int)) |
493 | goto e_inval; | |
1da177e4 LT |
494 | if (val > 255 || val < -1) |
495 | goto e_inval; | |
496 | np->hop_limit = val; | |
497 | retv = 0; | |
498 | break; | |
499 | ||
500 | case IPV6_MULTICAST_HOPS: | |
501 | if (sk->sk_type == SOCK_STREAM) | |
1717699c | 502 | break; |
b2a9d7c2 YH |
503 | if (optlen < sizeof(int)) |
504 | goto e_inval; | |
1da177e4 LT |
505 | if (val > 255 || val < -1) |
506 | goto e_inval; | |
2a38e6d5 | 507 | np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val); |
1da177e4 LT |
508 | retv = 0; |
509 | break; | |
510 | ||
511 | case IPV6_MULTICAST_LOOP: | |
b2a9d7c2 YH |
512 | if (optlen < sizeof(int)) |
513 | goto e_inval; | |
28d44882 YH |
514 | if (val != valbool) |
515 | goto e_inval; | |
1da177e4 LT |
516 | np->mc_loop = valbool; |
517 | retv = 0; | |
518 | break; | |
519 | ||
c4062dfc EH |
520 | case IPV6_UNICAST_IF: |
521 | { | |
522 | struct net_device *dev = NULL; | |
523 | int ifindex; | |
524 | ||
525 | if (optlen != sizeof(int)) | |
526 | goto e_inval; | |
527 | ||
528 | ifindex = (__force int)ntohl((__force __be32)val); | |
529 | if (ifindex == 0) { | |
530 | np->ucast_oif = 0; | |
531 | retv = 0; | |
532 | break; | |
533 | } | |
534 | ||
535 | dev = dev_get_by_index(net, ifindex); | |
536 | retv = -EADDRNOTAVAIL; | |
537 | if (!dev) | |
538 | break; | |
539 | dev_put(dev); | |
540 | ||
541 | retv = -EINVAL; | |
542 | if (sk->sk_bound_dev_if) | |
543 | break; | |
544 | ||
545 | np->ucast_oif = ifindex; | |
546 | retv = 0; | |
547 | break; | |
548 | } | |
549 | ||
1da177e4 LT |
550 | case IPV6_MULTICAST_IF: |
551 | if (sk->sk_type == SOCK_STREAM) | |
1717699c | 552 | break; |
b2a9d7c2 YH |
553 | if (optlen < sizeof(int)) |
554 | goto e_inval; | |
1da177e4 | 555 | |
4953f0fc | 556 | if (val) { |
55b80503 ED |
557 | struct net_device *dev; |
558 | ||
4953f0fc BH |
559 | if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val) |
560 | goto e_inval; | |
561 | ||
55b80503 ED |
562 | dev = dev_get_by_index(net, val); |
563 | if (!dev) { | |
4953f0fc BH |
564 | retv = -ENODEV; |
565 | break; | |
566 | } | |
55b80503 | 567 | dev_put(dev); |
1da177e4 LT |
568 | } |
569 | np->mcast_oif = val; | |
570 | retv = 0; | |
571 | break; | |
572 | case IPV6_ADD_MEMBERSHIP: | |
573 | case IPV6_DROP_MEMBERSHIP: | |
574 | { | |
575 | struct ipv6_mreq mreq; | |
576 | ||
a28398ba WC |
577 | if (optlen < sizeof(struct ipv6_mreq)) |
578 | goto e_inval; | |
579 | ||
a96fb49b FL |
580 | retv = -EPROTO; |
581 | if (inet_sk(sk)->is_icsk) | |
582 | break; | |
583 | ||
1da177e4 LT |
584 | retv = -EFAULT; |
585 | if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq))) | |
586 | break; | |
587 | ||
588 | if (optname == IPV6_ADD_MEMBERSHIP) | |
589 | retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr); | |
590 | else | |
591 | retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr); | |
592 | break; | |
593 | } | |
594 | case IPV6_JOIN_ANYCAST: | |
595 | case IPV6_LEAVE_ANYCAST: | |
596 | { | |
597 | struct ipv6_mreq mreq; | |
598 | ||
a28398ba | 599 | if (optlen < sizeof(struct ipv6_mreq)) |
1da177e4 LT |
600 | goto e_inval; |
601 | ||
602 | retv = -EFAULT; | |
603 | if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq))) | |
604 | break; | |
605 | ||
606 | if (optname == IPV6_JOIN_ANYCAST) | |
607 | retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr); | |
608 | else | |
609 | retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr); | |
610 | break; | |
611 | } | |
612 | case MCAST_JOIN_GROUP: | |
613 | case MCAST_LEAVE_GROUP: | |
614 | { | |
615 | struct group_req greq; | |
616 | struct sockaddr_in6 *psin6; | |
617 | ||
a28398ba WC |
618 | if (optlen < sizeof(struct group_req)) |
619 | goto e_inval; | |
620 | ||
1da177e4 LT |
621 | retv = -EFAULT; |
622 | if (copy_from_user(&greq, optval, sizeof(struct group_req))) | |
623 | break; | |
624 | if (greq.gr_group.ss_family != AF_INET6) { | |
625 | retv = -EADDRNOTAVAIL; | |
626 | break; | |
627 | } | |
628 | psin6 = (struct sockaddr_in6 *)&greq.gr_group; | |
629 | if (optname == MCAST_JOIN_GROUP) | |
630 | retv = ipv6_sock_mc_join(sk, greq.gr_interface, | |
631 | &psin6->sin6_addr); | |
632 | else | |
633 | retv = ipv6_sock_mc_drop(sk, greq.gr_interface, | |
634 | &psin6->sin6_addr); | |
635 | break; | |
636 | } | |
637 | case MCAST_JOIN_SOURCE_GROUP: | |
638 | case MCAST_LEAVE_SOURCE_GROUP: | |
639 | case MCAST_BLOCK_SOURCE: | |
640 | case MCAST_UNBLOCK_SOURCE: | |
641 | { | |
642 | struct group_source_req greqs; | |
643 | int omode, add; | |
644 | ||
a28398ba | 645 | if (optlen < sizeof(struct group_source_req)) |
1da177e4 LT |
646 | goto e_inval; |
647 | if (copy_from_user(&greqs, optval, sizeof(greqs))) { | |
648 | retv = -EFAULT; | |
649 | break; | |
650 | } | |
651 | if (greqs.gsr_group.ss_family != AF_INET6 || | |
652 | greqs.gsr_source.ss_family != AF_INET6) { | |
653 | retv = -EADDRNOTAVAIL; | |
654 | break; | |
655 | } | |
656 | if (optname == MCAST_BLOCK_SOURCE) { | |
657 | omode = MCAST_EXCLUDE; | |
658 | add = 1; | |
659 | } else if (optname == MCAST_UNBLOCK_SOURCE) { | |
660 | omode = MCAST_EXCLUDE; | |
661 | add = 0; | |
662 | } else if (optname == MCAST_JOIN_SOURCE_GROUP) { | |
663 | struct sockaddr_in6 *psin6; | |
664 | ||
665 | psin6 = (struct sockaddr_in6 *)&greqs.gsr_group; | |
666 | retv = ipv6_sock_mc_join(sk, greqs.gsr_interface, | |
667 | &psin6->sin6_addr); | |
c9e3e8b6 DS |
668 | /* prior join w/ different source is ok */ |
669 | if (retv && retv != -EADDRINUSE) | |
1da177e4 LT |
670 | break; |
671 | omode = MCAST_INCLUDE; | |
672 | add = 1; | |
c9e3e8b6 | 673 | } else /* MCAST_LEAVE_SOURCE_GROUP */ { |
1da177e4 LT |
674 | omode = MCAST_INCLUDE; |
675 | add = 0; | |
676 | } | |
677 | retv = ip6_mc_source(add, omode, sk, &greqs); | |
678 | break; | |
679 | } | |
680 | case MCAST_MSFILTER: | |
681 | { | |
1da177e4 LT |
682 | struct group_filter *gsf; |
683 | ||
684 | if (optlen < GROUP_FILTER_SIZE(0)) | |
685 | goto e_inval; | |
686 | if (optlen > sysctl_optmem_max) { | |
687 | retv = -ENOBUFS; | |
688 | break; | |
689 | } | |
8b3a7005 | 690 | gsf = kmalloc(optlen,GFP_KERNEL); |
cfcabdcc | 691 | if (!gsf) { |
1da177e4 LT |
692 | retv = -ENOBUFS; |
693 | break; | |
694 | } | |
695 | retv = -EFAULT; | |
696 | if (copy_from_user(gsf, optval, optlen)) { | |
697 | kfree(gsf); | |
698 | break; | |
699 | } | |
700 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ | |
701 | if (gsf->gf_numsrc >= 0x1ffffffU || | |
702 | gsf->gf_numsrc > sysctl_mld_max_msf) { | |
703 | kfree(gsf); | |
704 | retv = -ENOBUFS; | |
705 | break; | |
706 | } | |
707 | if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) { | |
708 | kfree(gsf); | |
709 | retv = -EINVAL; | |
710 | break; | |
711 | } | |
712 | retv = ip6_mc_msfilter(sk, gsf); | |
713 | kfree(gsf); | |
714 | ||
715 | break; | |
716 | } | |
717 | case IPV6_ROUTER_ALERT: | |
b2a9d7c2 YH |
718 | if (optlen < sizeof(int)) |
719 | goto e_inval; | |
725a8ff0 | 720 | retv = ip6_ra_control(sk, val); |
1da177e4 LT |
721 | break; |
722 | case IPV6_MTU_DISCOVER: | |
b2a9d7c2 YH |
723 | if (optlen < sizeof(int)) |
724 | goto e_inval; | |
0b95227a | 725 | if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT) |
1da177e4 LT |
726 | goto e_inval; |
727 | np->pmtudisc = val; | |
728 | retv = 0; | |
729 | break; | |
730 | case IPV6_MTU: | |
b2a9d7c2 YH |
731 | if (optlen < sizeof(int)) |
732 | goto e_inval; | |
1da177e4 LT |
733 | if (val && val < IPV6_MIN_MTU) |
734 | goto e_inval; | |
735 | np->frag_size = val; | |
736 | retv = 0; | |
737 | break; | |
738 | case IPV6_RECVERR: | |
b2a9d7c2 YH |
739 | if (optlen < sizeof(int)) |
740 | goto e_inval; | |
1da177e4 LT |
741 | np->recverr = valbool; |
742 | if (!val) | |
743 | skb_queue_purge(&sk->sk_error_queue); | |
744 | retv = 0; | |
745 | break; | |
746 | case IPV6_FLOWINFO_SEND: | |
b2a9d7c2 YH |
747 | if (optlen < sizeof(int)) |
748 | goto e_inval; | |
1da177e4 LT |
749 | np->sndflow = valbool; |
750 | retv = 0; | |
751 | break; | |
752 | case IPV6_FLOWLABEL_MGR: | |
753 | retv = ipv6_flowlabel_opt(sk, optval, optlen); | |
754 | break; | |
755 | case IPV6_IPSEC_POLICY: | |
756 | case IPV6_XFRM_POLICY: | |
6fc0b4a7 | 757 | retv = -EPERM; |
af31f412 | 758 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
6fc0b4a7 | 759 | break; |
1da177e4 LT |
760 | retv = xfrm_user_policy(sk, optname, optval, optlen); |
761 | break; | |
762 | ||
7cbca67c YH |
763 | case IPV6_ADDR_PREFERENCES: |
764 | { | |
765 | unsigned int pref = 0; | |
766 | unsigned int prefmask = ~0; | |
767 | ||
b2a9d7c2 YH |
768 | if (optlen < sizeof(int)) |
769 | goto e_inval; | |
770 | ||
7cbca67c YH |
771 | retv = -EINVAL; |
772 | ||
773 | /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */ | |
774 | switch (val & (IPV6_PREFER_SRC_PUBLIC| | |
775 | IPV6_PREFER_SRC_TMP| | |
776 | IPV6_PREFER_SRC_PUBTMP_DEFAULT)) { | |
777 | case IPV6_PREFER_SRC_PUBLIC: | |
778 | pref |= IPV6_PREFER_SRC_PUBLIC; | |
779 | break; | |
780 | case IPV6_PREFER_SRC_TMP: | |
781 | pref |= IPV6_PREFER_SRC_TMP; | |
782 | break; | |
783 | case IPV6_PREFER_SRC_PUBTMP_DEFAULT: | |
784 | break; | |
785 | case 0: | |
786 | goto pref_skip_pubtmp; | |
787 | default: | |
788 | goto e_inval; | |
789 | } | |
790 | ||
791 | prefmask &= ~(IPV6_PREFER_SRC_PUBLIC| | |
792 | IPV6_PREFER_SRC_TMP); | |
793 | pref_skip_pubtmp: | |
794 | ||
795 | /* check HOME/COA conflicts */ | |
796 | switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) { | |
797 | case IPV6_PREFER_SRC_HOME: | |
798 | break; | |
799 | case IPV6_PREFER_SRC_COA: | |
800 | pref |= IPV6_PREFER_SRC_COA; | |
801 | case 0: | |
802 | goto pref_skip_coa; | |
803 | default: | |
804 | goto e_inval; | |
805 | } | |
806 | ||
807 | prefmask &= ~IPV6_PREFER_SRC_COA; | |
808 | pref_skip_coa: | |
809 | ||
810 | /* check CGA/NONCGA conflicts */ | |
811 | switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) { | |
812 | case IPV6_PREFER_SRC_CGA: | |
813 | case IPV6_PREFER_SRC_NONCGA: | |
814 | case 0: | |
815 | break; | |
816 | default: | |
817 | goto e_inval; | |
818 | } | |
819 | ||
820 | np->srcprefs = (np->srcprefs & prefmask) | pref; | |
821 | retv = 0; | |
822 | ||
823 | break; | |
824 | } | |
e802af9c SH |
825 | case IPV6_MINHOPCOUNT: |
826 | if (optlen < sizeof(int)) | |
827 | goto e_inval; | |
828 | if (val < 0 || val > 255) | |
829 | goto e_inval; | |
830 | np->min_hopcount = val; | |
d4596bad | 831 | retv = 0; |
793b1473 BH |
832 | break; |
833 | case IPV6_DONTFRAG: | |
834 | np->dontfrag = valbool; | |
e802af9c SH |
835 | retv = 0; |
836 | break; | |
1da177e4 | 837 | } |
7cbca67c | 838 | |
1da177e4 LT |
839 | release_sock(sk); |
840 | ||
1da177e4 LT |
841 | return retv; |
842 | ||
843 | e_inval: | |
844 | release_sock(sk); | |
845 | return -EINVAL; | |
846 | } | |
847 | ||
3fdadf7d | 848 | int ipv6_setsockopt(struct sock *sk, int level, int optname, |
b7058842 | 849 | char __user *optval, unsigned int optlen) |
3fdadf7d DM |
850 | { |
851 | int err; | |
852 | ||
853 | if (level == SOL_IP && sk->sk_type != SOCK_RAW) | |
854 | return udp_prot.setsockopt(sk, level, optname, optval, optlen); | |
855 | ||
856 | if (level != SOL_IPV6) | |
857 | return -ENOPROTOOPT; | |
858 | ||
859 | err = do_ipv6_setsockopt(sk, level, optname, optval, optlen); | |
860 | #ifdef CONFIG_NETFILTER | |
861 | /* we need to exclude all possible ENOPROTOOPTs except default case */ | |
862 | if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY && | |
863 | optname != IPV6_XFRM_POLICY) { | |
864 | lock_sock(sk); | |
865 | err = nf_setsockopt(sk, PF_INET6, optname, optval, | |
866 | optlen); | |
867 | release_sock(sk); | |
868 | } | |
869 | #endif | |
870 | return err; | |
871 | } | |
872 | ||
7159039a | 873 | EXPORT_SYMBOL(ipv6_setsockopt); |
3fdadf7d DM |
874 | |
875 | #ifdef CONFIG_COMPAT | |
876 | int compat_ipv6_setsockopt(struct sock *sk, int level, int optname, | |
b7058842 | 877 | char __user *optval, unsigned int optlen) |
3fdadf7d DM |
878 | { |
879 | int err; | |
880 | ||
881 | if (level == SOL_IP && sk->sk_type != SOCK_RAW) { | |
543d9cfe ACM |
882 | if (udp_prot.compat_setsockopt != NULL) |
883 | return udp_prot.compat_setsockopt(sk, level, optname, | |
884 | optval, optlen); | |
885 | return udp_prot.setsockopt(sk, level, optname, optval, optlen); | |
3fdadf7d DM |
886 | } |
887 | ||
888 | if (level != SOL_IPV6) | |
889 | return -ENOPROTOOPT; | |
890 | ||
dae50295 DS |
891 | if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER) |
892 | return compat_mc_setsockopt(sk, level, optname, optval, optlen, | |
893 | ipv6_setsockopt); | |
894 | ||
3fdadf7d DM |
895 | err = do_ipv6_setsockopt(sk, level, optname, optval, optlen); |
896 | #ifdef CONFIG_NETFILTER | |
897 | /* we need to exclude all possible ENOPROTOOPTs except default case */ | |
898 | if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY && | |
543d9cfe | 899 | optname != IPV6_XFRM_POLICY) { |
3fdadf7d | 900 | lock_sock(sk); |
543d9cfe ACM |
901 | err = compat_nf_setsockopt(sk, PF_INET6, optname, |
902 | optval, optlen); | |
3fdadf7d DM |
903 | release_sock(sk); |
904 | } | |
905 | #endif | |
906 | return err; | |
907 | } | |
543d9cfe ACM |
908 | |
909 | EXPORT_SYMBOL(compat_ipv6_setsockopt); | |
3fdadf7d DM |
910 | #endif |
911 | ||
28693079 | 912 | static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt, |
4c6510a7 | 913 | int optname, char __user *optval, int len) |
333fad53 | 914 | { |
28693079 DM |
915 | struct ipv6_opt_hdr *hdr; |
916 | ||
4c6510a7 YH |
917 | if (!opt) |
918 | return 0; | |
919 | ||
920 | switch(optname) { | |
921 | case IPV6_HOPOPTS: | |
922 | hdr = opt->hopopt; | |
923 | break; | |
924 | case IPV6_RTHDRDSTOPTS: | |
925 | hdr = opt->dst0opt; | |
926 | break; | |
927 | case IPV6_RTHDR: | |
928 | hdr = (struct ipv6_opt_hdr *)opt->srcrt; | |
929 | break; | |
930 | case IPV6_DSTOPTS: | |
931 | hdr = opt->dst1opt; | |
932 | break; | |
933 | default: | |
934 | return -EINVAL; /* should not happen */ | |
935 | } | |
936 | ||
937 | if (!hdr) | |
333fad53 | 938 | return 0; |
28693079 | 939 | |
d2b02ed9 | 940 | len = min_t(unsigned int, len, ipv6_optlen(hdr)); |
660adc6e | 941 | if (copy_to_user(optval, hdr, len)) |
333fad53 | 942 | return -EFAULT; |
95b496b6 | 943 | return len; |
333fad53 YH |
944 | } |
945 | ||
3fdadf7d | 946 | static int do_ipv6_getsockopt(struct sock *sk, int level, int optname, |
95c96174 | 947 | char __user *optval, int __user *optlen, unsigned int flags) |
1da177e4 LT |
948 | { |
949 | struct ipv6_pinfo *np = inet6_sk(sk); | |
950 | int len; | |
951 | int val; | |
952 | ||
7bc570c8 YH |
953 | if (ip6_mroute_opt(optname)) |
954 | return ip6_mroute_getsockopt(sk, optname, optval, optlen); | |
955 | ||
1da177e4 LT |
956 | if (get_user(len, optlen)) |
957 | return -EFAULT; | |
958 | switch (optname) { | |
959 | case IPV6_ADDRFORM: | |
960 | if (sk->sk_protocol != IPPROTO_UDP && | |
ba4e58ec | 961 | sk->sk_protocol != IPPROTO_UDPLITE && |
1da177e4 | 962 | sk->sk_protocol != IPPROTO_TCP) |
1717699c | 963 | return -ENOPROTOOPT; |
1da177e4 LT |
964 | if (sk->sk_state != TCP_ESTABLISHED) |
965 | return -ENOTCONN; | |
966 | val = sk->sk_family; | |
967 | break; | |
968 | case MCAST_MSFILTER: | |
969 | { | |
970 | struct group_filter gsf; | |
971 | int err; | |
972 | ||
973 | if (len < GROUP_FILTER_SIZE(0)) | |
974 | return -EINVAL; | |
975 | if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) | |
976 | return -EFAULT; | |
20c61fbd YH |
977 | if (gsf.gf_group.ss_family != AF_INET6) |
978 | return -EADDRNOTAVAIL; | |
1da177e4 LT |
979 | lock_sock(sk); |
980 | err = ip6_mc_msfget(sk, &gsf, | |
981 | (struct group_filter __user *)optval, optlen); | |
982 | release_sock(sk); | |
983 | return err; | |
984 | } | |
985 | ||
333fad53 | 986 | case IPV6_2292PKTOPTIONS: |
1da177e4 LT |
987 | { |
988 | struct msghdr msg; | |
989 | struct sk_buff *skb; | |
990 | ||
991 | if (sk->sk_type != SOCK_STREAM) | |
992 | return -ENOPROTOOPT; | |
993 | ||
994 | msg.msg_control = optval; | |
995 | msg.msg_controllen = len; | |
98e77438 | 996 | msg.msg_flags = flags; |
1da177e4 LT |
997 | |
998 | lock_sock(sk); | |
999 | skb = np->pktoptions; | |
1000 | if (skb) | |
1001 | atomic_inc(&skb->users); | |
1002 | release_sock(sk); | |
1003 | ||
1004 | if (skb) { | |
4b261c75 | 1005 | ip6_datagram_recv_ctl(sk, &msg, skb); |
1da177e4 | 1006 | kfree_skb(skb); |
1da177e4 LT |
1007 | } else { |
1008 | if (np->rxopt.bits.rxinfo) { | |
1009 | struct in6_pktinfo src_info; | |
f250dcda YH |
1010 | src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif : |
1011 | np->sticky_pktinfo.ipi6_ifindex; | |
efe4208f | 1012 | src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr; |
1da177e4 LT |
1013 | 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 | } | |
4c507d28 | 1019 | if (np->rxopt.bits.rxtclass) { |
d76ed22b LR |
1020 | int tclass = (int)ip6_tclass(np->rcv_flowinfo); |
1021 | ||
4c507d28 JB |
1022 | put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass); |
1023 | } | |
333fad53 YH |
1024 | if (np->rxopt.bits.rxoinfo) { |
1025 | struct in6_pktinfo src_info; | |
f250dcda YH |
1026 | src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif : |
1027 | np->sticky_pktinfo.ipi6_ifindex; | |
efe4208f ED |
1028 | src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : |
1029 | np->sticky_pktinfo.ipi6_addr; | |
333fad53 YH |
1030 | put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info); |
1031 | } | |
1032 | if (np->rxopt.bits.rxohlim) { | |
1033 | int hlim = np->mcast_hops; | |
1034 | put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim); | |
1035 | } | |
1397ed35 | 1036 | if (np->rxopt.bits.rxflow) { |
68536053 FF |
1037 | __be32 flowinfo = np->rcv_flowinfo; |
1038 | ||
1397ed35 FF |
1039 | put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo); |
1040 | } | |
1da177e4 LT |
1041 | } |
1042 | len -= msg.msg_controllen; | |
1043 | return put_user(len, optlen); | |
1044 | } | |
1045 | case IPV6_MTU: | |
1046 | { | |
1047 | struct dst_entry *dst; | |
b6c6712a | 1048 | |
1ab1457c | 1049 | val = 0; |
b6c6712a ED |
1050 | rcu_read_lock(); |
1051 | dst = __sk_dst_get(sk); | |
1052 | if (dst) | |
1da177e4 | 1053 | val = dst_mtu(dst); |
b6c6712a | 1054 | rcu_read_unlock(); |
1da177e4 LT |
1055 | if (!val) |
1056 | return -ENOTCONN; | |
1057 | break; | |
1058 | } | |
1059 | ||
1060 | case IPV6_V6ONLY: | |
9fe516ba | 1061 | val = sk->sk_ipv6only; |
1da177e4 LT |
1062 | break; |
1063 | ||
333fad53 | 1064 | case IPV6_RECVPKTINFO: |
1da177e4 LT |
1065 | val = np->rxopt.bits.rxinfo; |
1066 | break; | |
1067 | ||
333fad53 YH |
1068 | case IPV6_2292PKTINFO: |
1069 | val = np->rxopt.bits.rxoinfo; | |
1070 | break; | |
1071 | ||
1072 | case IPV6_RECVHOPLIMIT: | |
1da177e4 LT |
1073 | val = np->rxopt.bits.rxhlim; |
1074 | break; | |
1075 | ||
333fad53 YH |
1076 | case IPV6_2292HOPLIMIT: |
1077 | val = np->rxopt.bits.rxohlim; | |
1078 | break; | |
1079 | ||
1080 | case IPV6_RECVRTHDR: | |
1da177e4 LT |
1081 | val = np->rxopt.bits.srcrt; |
1082 | break; | |
1083 | ||
333fad53 YH |
1084 | case IPV6_2292RTHDR: |
1085 | val = np->rxopt.bits.osrcrt; | |
1086 | break; | |
1087 | ||
1da177e4 | 1088 | case IPV6_HOPOPTS: |
333fad53 YH |
1089 | case IPV6_RTHDRDSTOPTS: |
1090 | case IPV6_RTHDR: | |
1091 | case IPV6_DSTOPTS: | |
1092 | { | |
1093 | ||
1094 | lock_sock(sk); | |
28693079 | 1095 | len = ipv6_getsockopt_sticky(sk, np->opt, |
4c6510a7 | 1096 | optname, optval, len); |
333fad53 | 1097 | release_sock(sk); |
05335c22 YH |
1098 | /* check if ipv6_getsockopt_sticky() returns err code */ |
1099 | if (len < 0) | |
1100 | return len; | |
333fad53 YH |
1101 | return put_user(len, optlen); |
1102 | } | |
1103 | ||
1104 | case IPV6_RECVHOPOPTS: | |
1da177e4 LT |
1105 | val = np->rxopt.bits.hopopts; |
1106 | break; | |
1107 | ||
333fad53 YH |
1108 | case IPV6_2292HOPOPTS: |
1109 | val = np->rxopt.bits.ohopopts; | |
1110 | break; | |
1111 | ||
1112 | case IPV6_RECVDSTOPTS: | |
1da177e4 LT |
1113 | val = np->rxopt.bits.dstopts; |
1114 | break; | |
1115 | ||
333fad53 YH |
1116 | case IPV6_2292DSTOPTS: |
1117 | val = np->rxopt.bits.odstopts; | |
1118 | break; | |
1119 | ||
41a1f8ea YH |
1120 | case IPV6_TCLASS: |
1121 | val = np->tclass; | |
1122 | break; | |
1123 | ||
1124 | case IPV6_RECVTCLASS: | |
1125 | val = np->rxopt.bits.rxtclass; | |
1126 | break; | |
1127 | ||
1da177e4 LT |
1128 | case IPV6_FLOWINFO: |
1129 | val = np->rxopt.bits.rxflow; | |
1130 | break; | |
1131 | ||
793b1473 BH |
1132 | case IPV6_RECVPATHMTU: |
1133 | val = np->rxopt.bits.rxpmtu; | |
1134 | break; | |
1135 | ||
1136 | case IPV6_PATHMTU: | |
1137 | { | |
1138 | struct dst_entry *dst; | |
1139 | struct ip6_mtuinfo mtuinfo; | |
1140 | ||
1141 | if (len < sizeof(mtuinfo)) | |
1142 | return -EINVAL; | |
1143 | ||
1144 | len = sizeof(mtuinfo); | |
1145 | memset(&mtuinfo, 0, sizeof(mtuinfo)); | |
1146 | ||
1147 | rcu_read_lock(); | |
1148 | dst = __sk_dst_get(sk); | |
1149 | if (dst) | |
1150 | mtuinfo.ip6m_mtu = dst_mtu(dst); | |
1151 | rcu_read_unlock(); | |
1152 | if (!mtuinfo.ip6m_mtu) | |
1153 | return -ENOTCONN; | |
1154 | ||
1155 | if (put_user(len, optlen)) | |
1156 | return -EFAULT; | |
1157 | if (copy_to_user(optval, &mtuinfo, len)) | |
1158 | return -EFAULT; | |
1159 | ||
1160 | return 0; | |
1161 | break; | |
1162 | } | |
1163 | ||
6c468622 BS |
1164 | case IPV6_TRANSPARENT: |
1165 | val = inet_sk(sk)->transparent; | |
1166 | break; | |
1167 | ||
1168 | case IPV6_RECVORIGDSTADDR: | |
1169 | val = np->rxopt.bits.rxorigdstaddr; | |
1170 | break; | |
1171 | ||
1da177e4 | 1172 | case IPV6_UNICAST_HOPS: |
1da177e4 | 1173 | case IPV6_MULTICAST_HOPS: |
befffe90 BH |
1174 | { |
1175 | struct dst_entry *dst; | |
1176 | ||
1177 | if (optname == IPV6_UNICAST_HOPS) | |
1178 | val = np->hop_limit; | |
1179 | else | |
1180 | val = np->mcast_hops; | |
1181 | ||
b6c6712a ED |
1182 | if (val < 0) { |
1183 | rcu_read_lock(); | |
1184 | dst = __sk_dst_get(sk); | |
1185 | if (dst) | |
6b75d090 | 1186 | val = ip6_dst_hoplimit(dst); |
b6c6712a | 1187 | rcu_read_unlock(); |
befffe90 | 1188 | } |
b6c6712a | 1189 | |
befffe90 | 1190 | if (val < 0) |
53b7997f | 1191 | val = sock_net(sk)->ipv6.devconf_all->hop_limit; |
1da177e4 | 1192 | break; |
befffe90 | 1193 | } |
1da177e4 LT |
1194 | |
1195 | case IPV6_MULTICAST_LOOP: | |
1196 | val = np->mc_loop; | |
1197 | break; | |
1198 | ||
1199 | case IPV6_MULTICAST_IF: | |
1200 | val = np->mcast_oif; | |
1201 | break; | |
1202 | ||
c4062dfc EH |
1203 | case IPV6_UNICAST_IF: |
1204 | val = (__force int)htonl((__u32) np->ucast_oif); | |
1205 | break; | |
1206 | ||
1da177e4 LT |
1207 | case IPV6_MTU_DISCOVER: |
1208 | val = np->pmtudisc; | |
1209 | break; | |
1210 | ||
1211 | case IPV6_RECVERR: | |
1212 | val = np->recverr; | |
1213 | break; | |
1214 | ||
1215 | case IPV6_FLOWINFO_SEND: | |
1216 | val = np->sndflow; | |
1217 | break; | |
1218 | ||
3fdfa5ff FF |
1219 | case IPV6_FLOWLABEL_MGR: |
1220 | { | |
1221 | struct in6_flowlabel_req freq; | |
46e5f401 | 1222 | int flags; |
3fdfa5ff FF |
1223 | |
1224 | if (len < sizeof(freq)) | |
1225 | return -EINVAL; | |
1226 | ||
1227 | if (copy_from_user(&freq, optval, sizeof(freq))) | |
1228 | return -EFAULT; | |
1229 | ||
1230 | if (freq.flr_action != IPV6_FL_A_GET) | |
1231 | return -EINVAL; | |
1232 | ||
1233 | len = sizeof(freq); | |
46e5f401 FF |
1234 | flags = freq.flr_flags; |
1235 | ||
3fdfa5ff FF |
1236 | memset(&freq, 0, sizeof(freq)); |
1237 | ||
46e5f401 | 1238 | val = ipv6_flowlabel_opt_get(sk, &freq, flags); |
3fdfa5ff FF |
1239 | if (val < 0) |
1240 | return val; | |
1241 | ||
1242 | if (put_user(len, optlen)) | |
1243 | return -EFAULT; | |
1244 | if (copy_to_user(optval, &freq, len)) | |
1245 | return -EFAULT; | |
1246 | ||
1247 | return 0; | |
1248 | } | |
1249 | ||
7cbca67c YH |
1250 | case IPV6_ADDR_PREFERENCES: |
1251 | val = 0; | |
1252 | ||
1253 | if (np->srcprefs & IPV6_PREFER_SRC_TMP) | |
1254 | val |= IPV6_PREFER_SRC_TMP; | |
1255 | else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC) | |
1256 | val |= IPV6_PREFER_SRC_PUBLIC; | |
1257 | else { | |
1258 | /* XXX: should we return system default? */ | |
1259 | val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT; | |
1260 | } | |
1261 | ||
1262 | if (np->srcprefs & IPV6_PREFER_SRC_COA) | |
1263 | val |= IPV6_PREFER_SRC_COA; | |
1264 | else | |
1265 | val |= IPV6_PREFER_SRC_HOME; | |
1266 | break; | |
e802af9c SH |
1267 | |
1268 | case IPV6_MINHOPCOUNT: | |
1269 | val = np->min_hopcount; | |
1270 | break; | |
793b1473 BH |
1271 | |
1272 | case IPV6_DONTFRAG: | |
1273 | val = np->dontfrag; | |
1274 | break; | |
7cbca67c | 1275 | |
1da177e4 | 1276 | default: |
cf6fc4a9 | 1277 | return -ENOPROTOOPT; |
1da177e4 LT |
1278 | } |
1279 | len = min_t(unsigned int, sizeof(int), len); | |
1280 | if(put_user(len, optlen)) | |
1281 | return -EFAULT; | |
1282 | if(copy_to_user(optval,&val,len)) | |
1283 | return -EFAULT; | |
1284 | return 0; | |
1285 | } | |
1286 | ||
3fdadf7d DM |
1287 | int ipv6_getsockopt(struct sock *sk, int level, int optname, |
1288 | char __user *optval, int __user *optlen) | |
1289 | { | |
1290 | int err; | |
1291 | ||
1292 | if (level == SOL_IP && sk->sk_type != SOCK_RAW) | |
1293 | return udp_prot.getsockopt(sk, level, optname, optval, optlen); | |
1294 | ||
1295 | if(level != SOL_IPV6) | |
1296 | return -ENOPROTOOPT; | |
1297 | ||
98e77438 | 1298 | err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0); |
3fdadf7d | 1299 | #ifdef CONFIG_NETFILTER |
cf6fc4a9 WY |
1300 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
1301 | if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) { | |
3fdadf7d DM |
1302 | int len; |
1303 | ||
1304 | if (get_user(len, optlen)) | |
1305 | return -EFAULT; | |
1306 | ||
1307 | lock_sock(sk); | |
1308 | err = nf_getsockopt(sk, PF_INET6, optname, optval, | |
1309 | &len); | |
1310 | release_sock(sk); | |
1311 | if (err >= 0) | |
1312 | err = put_user(len, optlen); | |
1313 | } | |
1314 | #endif | |
1315 | return err; | |
1316 | } | |
1317 | ||
7159039a YH |
1318 | EXPORT_SYMBOL(ipv6_getsockopt); |
1319 | ||
3fdadf7d DM |
1320 | #ifdef CONFIG_COMPAT |
1321 | int compat_ipv6_getsockopt(struct sock *sk, int level, int optname, | |
543d9cfe | 1322 | char __user *optval, int __user *optlen) |
3fdadf7d DM |
1323 | { |
1324 | int err; | |
1325 | ||
1326 | if (level == SOL_IP && sk->sk_type != SOCK_RAW) { | |
543d9cfe ACM |
1327 | if (udp_prot.compat_getsockopt != NULL) |
1328 | return udp_prot.compat_getsockopt(sk, level, optname, | |
1329 | optval, optlen); | |
1330 | return udp_prot.getsockopt(sk, level, optname, optval, optlen); | |
3fdadf7d DM |
1331 | } |
1332 | ||
543d9cfe | 1333 | if (level != SOL_IPV6) |
3fdadf7d DM |
1334 | return -ENOPROTOOPT; |
1335 | ||
80991790 PE |
1336 | if (optname == MCAST_MSFILTER) |
1337 | return compat_mc_getsockopt(sk, level, optname, optval, optlen, | |
1338 | ipv6_getsockopt); | |
1339 | ||
98e77438 DB |
1340 | err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, |
1341 | MSG_CMSG_COMPAT); | |
3fdadf7d | 1342 | #ifdef CONFIG_NETFILTER |
cf6fc4a9 WY |
1343 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
1344 | if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) { | |
3fdadf7d DM |
1345 | int len; |
1346 | ||
1347 | if (get_user(len, optlen)) | |
1348 | return -EFAULT; | |
1349 | ||
1350 | lock_sock(sk); | |
543d9cfe ACM |
1351 | err = compat_nf_getsockopt(sk, PF_INET6, |
1352 | optname, optval, &len); | |
3fdadf7d DM |
1353 | release_sock(sk); |
1354 | if (err >= 0) | |
1355 | err = put_user(len, optlen); | |
1356 | } | |
1357 | #endif | |
1358 | return err; | |
1359 | } | |
543d9cfe ACM |
1360 | |
1361 | EXPORT_SYMBOL(compat_ipv6_getsockopt); | |
3fdadf7d DM |
1362 | #endif |
1363 |