Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * IPv6 BSD socket options interface | |
1ab1457c | 4 | * Linux INET6 implementation |
1da177e4 LT |
5 | * |
6 | * Authors: | |
1ab1457c | 7 | * Pedro Roque <roque@di.fc.ul.pt> |
1da177e4 LT |
8 | * |
9 | * Based on linux/net/ipv4/ip_sockglue.c | |
10 | * | |
1da177e4 LT |
11 | * FIXME: Make the setsockopt code POSIX compliant: That is |
12 | * | |
1da177e4 LT |
13 | * o Truncate getsockopt returns |
14 | * o Return an optlen of the truncated length if need be | |
15 | * | |
16 | * Changes: | |
17 | * David L Stevens <dlstevens@us.ibm.com>: | |
18 | * - added multicast source filtering API for MLDv2 | |
19 | */ | |
20 | ||
21 | #include <linux/module.h> | |
4fc268d2 | 22 | #include <linux/capability.h> |
1da177e4 LT |
23 | #include <linux/errno.h> |
24 | #include <linux/types.h> | |
25 | #include <linux/socket.h> | |
26 | #include <linux/sockios.h> | |
1da177e4 LT |
27 | #include <linux/net.h> |
28 | #include <linux/in6.h> | |
7bc570c8 | 29 | #include <linux/mroute6.h> |
1da177e4 LT |
30 | #include <linux/netdevice.h> |
31 | #include <linux/if_arp.h> | |
32 | #include <linux/init.h> | |
33 | #include <linux/sysctl.h> | |
34 | #include <linux/netfilter.h> | |
5a0e3ad6 | 35 | #include <linux/slab.h> |
1da177e4 LT |
36 | |
37 | #include <net/sock.h> | |
38 | #include <net/snmp.h> | |
39 | #include <net/ipv6.h> | |
40 | #include <net/ndisc.h> | |
41 | #include <net/protocol.h> | |
42 | #include <net/transp_v6.h> | |
43 | #include <net/ip6_route.h> | |
44 | #include <net/addrconf.h> | |
45 | #include <net/inet_common.h> | |
46 | #include <net/tcp.h> | |
47 | #include <net/udp.h> | |
ba4e58ec | 48 | #include <net/udplite.h> |
1da177e4 | 49 | #include <net/xfrm.h> |
dae50295 | 50 | #include <net/compat.h> |
a149e7c7 | 51 | #include <net/seg6.h> |
1da177e4 | 52 | |
7c0f6ba6 | 53 | #include <linux/uaccess.h> |
1da177e4 | 54 | |
1da177e4 LT |
55 | struct ip6_ra_chain *ip6_ra_chain; |
56 | DEFINE_RWLOCK(ip6_ra_lock); | |
57 | ||
790eb673 ED |
58 | DEFINE_STATIC_KEY_FALSE(ip6_min_hopcount); |
59 | ||
725a8ff0 | 60 | int ip6_ra_control(struct sock *sk, int sel) |
1da177e4 LT |
61 | { |
62 | struct ip6_ra_chain *ra, *new_ra, **rap; | |
63 | ||
64 | /* RA packet may be delivered ONLY to IPPROTO_RAW socket */ | |
c720c7e8 | 65 | if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num != IPPROTO_RAW) |
1717699c | 66 | return -ENOPROTOOPT; |
1da177e4 | 67 | |
67ba4152 | 68 | new_ra = (sel >= 0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL; |
95baa60a GZ |
69 | if (sel >= 0 && !new_ra) |
70 | return -ENOMEM; | |
1da177e4 LT |
71 | |
72 | write_lock_bh(&ip6_ra_lock); | |
67ba4152 | 73 | for (rap = &ip6_ra_chain; (ra = *rap) != NULL; rap = &ra->next) { |
1da177e4 | 74 | if (ra->sk == sk) { |
67ba4152 | 75 | if (sel >= 0) { |
1da177e4 | 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 | } | |
63159f29 | 89 | if (!new_ra) { |
1da177e4 LT |
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 | 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 | } | |
e7712f1a | 113 | } |
45f6fad8 ED |
114 | opt = xchg((__force struct ipv6_txoptions **)&inet6_sk(sk)->opt, |
115 | opt); | |
e7712f1a YH |
116 | sk_dst_reset(sk); |
117 | ||
118 | return opt; | |
119 | } | |
120 | ||
baf606d9 MRL |
121 | static bool setsockopt_needs_rtnl(int optname) |
122 | { | |
123 | switch (optname) { | |
8651be8f | 124 | case IPV6_ADDRFORM: |
baf606d9 MRL |
125 | case IPV6_ADD_MEMBERSHIP: |
126 | case IPV6_DROP_MEMBERSHIP: | |
c4a6853d MRL |
127 | case IPV6_JOIN_ANYCAST: |
128 | case IPV6_LEAVE_ANYCAST: | |
baf606d9 MRL |
129 | case MCAST_JOIN_GROUP: |
130 | case MCAST_LEAVE_GROUP: | |
54ff9ef3 MRL |
131 | case MCAST_JOIN_SOURCE_GROUP: |
132 | case MCAST_LEAVE_SOURCE_GROUP: | |
133 | case MCAST_BLOCK_SOURCE: | |
134 | case MCAST_UNBLOCK_SOURCE: | |
135 | case MCAST_MSFILTER: | |
baf606d9 MRL |
136 | return true; |
137 | } | |
138 | return false; | |
139 | } | |
140 | ||
894cfbc0 CH |
141 | static int copy_group_source_from_sockptr(struct group_source_req *greqs, |
142 | sockptr_t optval, int optlen) | |
3021ad52 CH |
143 | { |
144 | if (in_compat_syscall()) { | |
145 | struct compat_group_source_req gr32; | |
146 | ||
147 | if (optlen < sizeof(gr32)) | |
148 | return -EINVAL; | |
894cfbc0 | 149 | if (copy_from_sockptr(&gr32, optval, sizeof(gr32))) |
3021ad52 CH |
150 | return -EFAULT; |
151 | greqs->gsr_interface = gr32.gsr_interface; | |
152 | greqs->gsr_group = gr32.gsr_group; | |
153 | greqs->gsr_source = gr32.gsr_source; | |
154 | } else { | |
155 | if (optlen < sizeof(*greqs)) | |
156 | return -EINVAL; | |
894cfbc0 | 157 | if (copy_from_sockptr(greqs, optval, sizeof(*greqs))) |
3021ad52 CH |
158 | return -EFAULT; |
159 | } | |
160 | ||
161 | return 0; | |
162 | } | |
163 | ||
fcfa0b09 | 164 | static int do_ipv6_mcast_group_source(struct sock *sk, int optname, |
894cfbc0 | 165 | sockptr_t optval, int optlen) |
fcfa0b09 | 166 | { |
3021ad52 | 167 | struct group_source_req greqs; |
fcfa0b09 | 168 | int omode, add; |
3021ad52 CH |
169 | int ret; |
170 | ||
894cfbc0 | 171 | ret = copy_group_source_from_sockptr(&greqs, optval, optlen); |
3021ad52 CH |
172 | if (ret) |
173 | return ret; | |
fcfa0b09 | 174 | |
3021ad52 CH |
175 | if (greqs.gsr_group.ss_family != AF_INET6 || |
176 | greqs.gsr_source.ss_family != AF_INET6) | |
fcfa0b09 AV |
177 | return -EADDRNOTAVAIL; |
178 | ||
179 | if (optname == MCAST_BLOCK_SOURCE) { | |
180 | omode = MCAST_EXCLUDE; | |
181 | add = 1; | |
182 | } else if (optname == MCAST_UNBLOCK_SOURCE) { | |
183 | omode = MCAST_EXCLUDE; | |
184 | add = 0; | |
185 | } else if (optname == MCAST_JOIN_SOURCE_GROUP) { | |
186 | struct sockaddr_in6 *psin6; | |
187 | int retv; | |
188 | ||
3021ad52 CH |
189 | psin6 = (struct sockaddr_in6 *)&greqs.gsr_group; |
190 | retv = ipv6_sock_mc_join_ssm(sk, greqs.gsr_interface, | |
fcfa0b09 AV |
191 | &psin6->sin6_addr, |
192 | MCAST_INCLUDE); | |
193 | /* prior join w/ different source is ok */ | |
194 | if (retv && retv != -EADDRINUSE) | |
195 | return retv; | |
196 | omode = MCAST_INCLUDE; | |
197 | add = 1; | |
198 | } else /* MCAST_LEAVE_SOURCE_GROUP */ { | |
199 | omode = MCAST_INCLUDE; | |
200 | add = 0; | |
201 | } | |
3021ad52 | 202 | return ip6_mc_source(add, omode, sk, &greqs); |
fcfa0b09 AV |
203 | } |
204 | ||
894cfbc0 | 205 | static int ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval, |
ca0e65eb CH |
206 | int optlen) |
207 | { | |
208 | struct group_filter *gsf; | |
209 | int ret; | |
210 | ||
211 | if (optlen < GROUP_FILTER_SIZE(0)) | |
212 | return -EINVAL; | |
7de6d09f | 213 | if (optlen > READ_ONCE(sysctl_optmem_max)) |
ca0e65eb CH |
214 | return -ENOBUFS; |
215 | ||
894cfbc0 | 216 | gsf = memdup_sockptr(optval, optlen); |
ca0e65eb CH |
217 | if (IS_ERR(gsf)) |
218 | return PTR_ERR(gsf); | |
219 | ||
220 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ | |
221 | ret = -ENOBUFS; | |
222 | if (gsf->gf_numsrc >= 0x1ffffffU || | |
223 | gsf->gf_numsrc > sysctl_mld_max_msf) | |
224 | goto out_free_gsf; | |
225 | ||
226 | ret = -EINVAL; | |
227 | if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) | |
228 | goto out_free_gsf; | |
229 | ||
db243b79 | 230 | ret = ip6_mc_msfilter(sk, gsf, gsf->gf_slist_flex); |
ca0e65eb CH |
231 | out_free_gsf: |
232 | kfree(gsf); | |
233 | return ret; | |
234 | } | |
235 | ||
894cfbc0 | 236 | static int compat_ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval, |
ca0e65eb CH |
237 | int optlen) |
238 | { | |
db243b79 | 239 | const int size0 = offsetof(struct compat_group_filter, gf_slist_flex); |
ca0e65eb CH |
240 | struct compat_group_filter *gf32; |
241 | void *p; | |
242 | int ret; | |
243 | int n; | |
244 | ||
245 | if (optlen < size0) | |
246 | return -EINVAL; | |
7de6d09f | 247 | if (optlen > READ_ONCE(sysctl_optmem_max) - 4) |
ca0e65eb CH |
248 | return -ENOBUFS; |
249 | ||
250 | p = kmalloc(optlen + 4, GFP_KERNEL); | |
251 | if (!p) | |
252 | return -ENOMEM; | |
253 | ||
db243b79 | 254 | gf32 = p + 4; /* we want ->gf_group and ->gf_slist_flex aligned */ |
ca0e65eb | 255 | ret = -EFAULT; |
894cfbc0 | 256 | if (copy_from_sockptr(gf32, optval, optlen)) |
ca0e65eb CH |
257 | goto out_free_p; |
258 | ||
259 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ | |
260 | ret = -ENOBUFS; | |
261 | n = gf32->gf_numsrc; | |
262 | if (n >= 0x1ffffffU || n > sysctl_mld_max_msf) | |
263 | goto out_free_p; | |
264 | ||
265 | ret = -EINVAL; | |
db243b79 | 266 | if (offsetof(struct compat_group_filter, gf_slist_flex[n]) > optlen) |
ca0e65eb CH |
267 | goto out_free_p; |
268 | ||
ca0e65eb CH |
269 | ret = ip6_mc_msfilter(sk, &(struct group_filter){ |
270 | .gf_interface = gf32->gf_interface, | |
271 | .gf_group = gf32->gf_group, | |
272 | .gf_fmode = gf32->gf_fmode, | |
db243b79 | 273 | .gf_numsrc = gf32->gf_numsrc}, gf32->gf_slist_flex); |
ca0e65eb CH |
274 | |
275 | out_free_p: | |
276 | kfree(p); | |
277 | return ret; | |
278 | } | |
ca0e65eb | 279 | |
fdf5bdd8 | 280 | static int ipv6_mcast_join_leave(struct sock *sk, int optname, |
894cfbc0 | 281 | sockptr_t optval, int optlen) |
fdf5bdd8 CH |
282 | { |
283 | struct sockaddr_in6 *psin6; | |
284 | struct group_req greq; | |
285 | ||
286 | if (optlen < sizeof(greq)) | |
287 | return -EINVAL; | |
894cfbc0 | 288 | if (copy_from_sockptr(&greq, optval, sizeof(greq))) |
fdf5bdd8 CH |
289 | return -EFAULT; |
290 | ||
291 | if (greq.gr_group.ss_family != AF_INET6) | |
292 | return -EADDRNOTAVAIL; | |
293 | psin6 = (struct sockaddr_in6 *)&greq.gr_group; | |
294 | if (optname == MCAST_JOIN_GROUP) | |
295 | return ipv6_sock_mc_join(sk, greq.gr_interface, | |
296 | &psin6->sin6_addr); | |
297 | return ipv6_sock_mc_drop(sk, greq.gr_interface, &psin6->sin6_addr); | |
298 | } | |
299 | ||
fdf5bdd8 | 300 | static int compat_ipv6_mcast_join_leave(struct sock *sk, int optname, |
894cfbc0 | 301 | sockptr_t optval, int optlen) |
fdf5bdd8 CH |
302 | { |
303 | struct compat_group_req gr32; | |
304 | struct sockaddr_in6 *psin6; | |
fdf5bdd8 CH |
305 | |
306 | if (optlen < sizeof(gr32)) | |
307 | return -EINVAL; | |
894cfbc0 | 308 | if (copy_from_sockptr(&gr32, optval, sizeof(gr32))) |
fdf5bdd8 CH |
309 | return -EFAULT; |
310 | ||
311 | if (gr32.gr_group.ss_family != AF_INET6) | |
312 | return -EADDRNOTAVAIL; | |
fdf5bdd8 CH |
313 | psin6 = (struct sockaddr_in6 *)&gr32.gr_group; |
314 | if (optname == MCAST_JOIN_GROUP) | |
3021ad52 | 315 | return ipv6_sock_mc_join(sk, gr32.gr_interface, |
fdf5bdd8 | 316 | &psin6->sin6_addr); |
3021ad52 | 317 | return ipv6_sock_mc_drop(sk, gr32.gr_interface, &psin6->sin6_addr); |
fdf5bdd8 | 318 | } |
fdf5bdd8 | 319 | |
894cfbc0 | 320 | static int ipv6_set_opt_hdr(struct sock *sk, int optname, sockptr_t optval, |
b84d2b73 CH |
321 | int optlen) |
322 | { | |
323 | struct ipv6_pinfo *np = inet6_sk(sk); | |
324 | struct ipv6_opt_hdr *new = NULL; | |
325 | struct net *net = sock_net(sk); | |
326 | struct ipv6_txoptions *opt; | |
327 | int err; | |
328 | ||
329 | /* hop-by-hop / destination options are privileged option */ | |
40cd308e | 330 | if (optname != IPV6_RTHDR && !sockopt_ns_capable(net->user_ns, CAP_NET_RAW)) |
b84d2b73 CH |
331 | return -EPERM; |
332 | ||
333 | /* remove any sticky options header with a zero option | |
334 | * length, per RFC3542. | |
335 | */ | |
336 | if (optlen > 0) { | |
894cfbc0 | 337 | if (sockptr_is_null(optval)) |
b84d2b73 CH |
338 | return -EINVAL; |
339 | if (optlen < sizeof(struct ipv6_opt_hdr) || | |
340 | optlen & 0x7 || | |
341 | optlen > 8 * 255) | |
342 | return -EINVAL; | |
343 | ||
894cfbc0 | 344 | new = memdup_sockptr(optval, optlen); |
b84d2b73 CH |
345 | if (IS_ERR(new)) |
346 | return PTR_ERR(new); | |
347 | if (unlikely(ipv6_optlen(new) > optlen)) { | |
348 | kfree(new); | |
349 | return -EINVAL; | |
350 | } | |
351 | } | |
352 | ||
353 | opt = rcu_dereference_protected(np->opt, lockdep_sock_is_held(sk)); | |
354 | opt = ipv6_renew_options(sk, opt, optname, new); | |
355 | kfree(new); | |
356 | if (IS_ERR(opt)) | |
357 | return PTR_ERR(opt); | |
358 | ||
359 | /* routing header option needs extra check */ | |
360 | err = -EINVAL; | |
361 | if (optname == IPV6_RTHDR && opt && opt->srcrt) { | |
362 | struct ipv6_rt_hdr *rthdr = opt->srcrt; | |
363 | switch (rthdr->type) { | |
364 | #if IS_ENABLED(CONFIG_IPV6_MIP6) | |
365 | case IPV6_SRCRT_TYPE_2: | |
366 | if (rthdr->hdrlen != 2 || rthdr->segments_left != 1) | |
367 | goto sticky_done; | |
368 | break; | |
369 | #endif | |
370 | case IPV6_SRCRT_TYPE_4: | |
371 | { | |
372 | struct ipv6_sr_hdr *srh = | |
373 | (struct ipv6_sr_hdr *)opt->srcrt; | |
374 | ||
375 | if (!seg6_validate_srh(srh, optlen, false)) | |
376 | goto sticky_done; | |
377 | break; | |
378 | } | |
379 | default: | |
380 | goto sticky_done; | |
381 | } | |
382 | } | |
383 | ||
384 | err = 0; | |
385 | opt = ipv6_update_options(sk, opt); | |
386 | sticky_done: | |
387 | if (opt) { | |
388 | atomic_sub(opt->tot_len, &sk->sk_omem_alloc); | |
389 | txopt_put(opt); | |
390 | } | |
391 | return err; | |
392 | } | |
393 | ||
75b64b68 MKL |
394 | int do_ipv6_setsockopt(struct sock *sk, int level, int optname, |
395 | sockptr_t optval, unsigned int optlen) | |
1da177e4 LT |
396 | { |
397 | struct ipv6_pinfo *np = inet6_sk(sk); | |
3b1e0a65 | 398 | struct net *net = sock_net(sk); |
1da177e4 LT |
399 | int val, valbool; |
400 | int retv = -ENOPROTOOPT; | |
baf606d9 | 401 | bool needs_rtnl = setsockopt_needs_rtnl(optname); |
1da177e4 | 402 | |
894cfbc0 | 403 | if (sockptr_is_null(optval)) |
67ba4152 | 404 | val = 0; |
b2a9d7c2 YH |
405 | else { |
406 | if (optlen >= sizeof(int)) { | |
894cfbc0 | 407 | if (copy_from_sockptr(&val, optval, sizeof(val))) |
b2a9d7c2 YH |
408 | return -EFAULT; |
409 | } else | |
410 | val = 0; | |
411 | } | |
1da177e4 | 412 | |
67ba4152 | 413 | valbool = (val != 0); |
1da177e4 | 414 | |
7bc570c8 | 415 | if (ip6_mroute_opt(optname)) |
894cfbc0 | 416 | return ip6_mroute_setsockopt(sk, optname, optval, optlen); |
7bc570c8 | 417 | |
baf606d9 MRL |
418 | if (needs_rtnl) |
419 | rtnl_lock(); | |
40cd308e | 420 | sockopt_lock_sock(sk); |
1da177e4 | 421 | |
3c52c6bb KI |
422 | /* Another thread has converted the socket into IPv4 with |
423 | * IPV6_ADDRFORM concurrently. | |
424 | */ | |
425 | if (unlikely(sk->sk_family != AF_INET6)) | |
426 | goto unlock; | |
427 | ||
1da177e4 LT |
428 | switch (optname) { |
429 | ||
430 | case IPV6_ADDRFORM: | |
b2a9d7c2 YH |
431 | if (optlen < sizeof(int)) |
432 | goto e_inval; | |
1da177e4 | 433 | if (val == PF_INET) { |
49d074f4 DL |
434 | if (sk->sk_type == SOCK_RAW) |
435 | break; | |
436 | ||
9596cc82 DL |
437 | if (sk->sk_protocol == IPPROTO_UDP || |
438 | sk->sk_protocol == IPPROTO_UDPLITE) { | |
439 | struct udp_sock *up = udp_sk(sk); | |
440 | if (up->pending == AF_INET6) { | |
441 | retv = -EBUSY; | |
442 | break; | |
443 | } | |
79a1f0cc HL |
444 | } else if (sk->sk_protocol == IPPROTO_TCP) { |
445 | if (sk->sk_prot != &tcpv6_prot) { | |
446 | retv = -EBUSY; | |
447 | break; | |
448 | } | |
449 | } else { | |
b6f61189 ED |
450 | break; |
451 | } | |
79a1f0cc | 452 | |
1da177e4 LT |
453 | if (sk->sk_state != TCP_ESTABLISHED) { |
454 | retv = -ENOTCONN; | |
455 | break; | |
456 | } | |
457 | ||
458 | if (ipv6_only_sock(sk) || | |
efe4208f | 459 | !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) { |
1da177e4 LT |
460 | retv = -EADDRNOTAVAIL; |
461 | break; | |
462 | } | |
463 | ||
8651be8f | 464 | __ipv6_sock_mc_close(sk); |
8c0de6e9 | 465 | __ipv6_sock_ac_close(sk); |
1da177e4 LT |
466 | |
467 | if (sk->sk_protocol == IPPROTO_TCP) { | |
d83d8461 | 468 | struct inet_connection_sock *icsk = inet_csk(sk); |
b3cb764a | 469 | |
c29a0bc4 PE |
470 | sock_prot_inuse_add(net, sk->sk_prot, -1); |
471 | sock_prot_inuse_add(net, &tcp_prot, 1); | |
b3cb764a | 472 | |
364f997b | 473 | /* Paired with READ_ONCE(sk->sk_prot) in inet6_stream_ops */ |
086d4905 | 474 | WRITE_ONCE(sk->sk_prot, &tcp_prot); |
f49cd2f4 KI |
475 | /* Paired with READ_ONCE() in tcp_(get|set)sockopt() */ |
476 | WRITE_ONCE(icsk->icsk_af_ops, &ipv4_specific); | |
1da177e4 LT |
477 | sk->sk_socket->ops = &inet_stream_ops; |
478 | sk->sk_family = PF_INET; | |
d83d8461 | 479 | tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); |
1da177e4 | 480 | } else { |
ba4e58ec GR |
481 | struct proto *prot = &udp_prot; |
482 | ||
db8dac20 | 483 | if (sk->sk_protocol == IPPROTO_UDPLITE) |
ba4e58ec | 484 | prot = &udplite_prot; |
b3cb764a | 485 | |
c29a0bc4 PE |
486 | sock_prot_inuse_add(net, sk->sk_prot, -1); |
487 | sock_prot_inuse_add(net, prot, 1); | |
b3cb764a | 488 | |
364f997b | 489 | /* Paired with READ_ONCE(sk->sk_prot) in inet6_dgram_ops */ |
086d4905 | 490 | WRITE_ONCE(sk->sk_prot, prot); |
1da177e4 LT |
491 | sk->sk_socket->ops = &inet_dgram_ops; |
492 | sk->sk_family = PF_INET; | |
493 | } | |
21985f43 KI |
494 | |
495 | /* Disable all options not to allocate memory anymore, | |
496 | * but there is still a race. See the lockless path | |
497 | * in udpv6_sendmsg() and ipv6_local_rxpmtu(). | |
498 | */ | |
499 | np->rxopt.all = 0; | |
500 | ||
501 | inet6_cleanup_sock(sk); | |
1da177e4 | 502 | |
1da177e4 LT |
503 | module_put(THIS_MODULE); |
504 | retv = 0; | |
505 | break; | |
506 | } | |
507 | goto e_inval; | |
508 | ||
509 | case IPV6_V6ONLY: | |
b2a9d7c2 | 510 | if (optlen < sizeof(int) || |
c720c7e8 | 511 | inet_sk(sk)->inet_num) |
1da177e4 | 512 | goto e_inval; |
9fe516ba | 513 | sk->sk_ipv6only = valbool; |
1da177e4 LT |
514 | retv = 0; |
515 | break; | |
516 | ||
333fad53 | 517 | case IPV6_RECVPKTINFO: |
b2a9d7c2 YH |
518 | if (optlen < sizeof(int)) |
519 | goto e_inval; | |
1da177e4 LT |
520 | np->rxopt.bits.rxinfo = valbool; |
521 | retv = 0; | |
522 | break; | |
1ab1457c | 523 | |
333fad53 | 524 | case IPV6_2292PKTINFO: |
b2a9d7c2 YH |
525 | if (optlen < sizeof(int)) |
526 | goto e_inval; | |
333fad53 YH |
527 | np->rxopt.bits.rxoinfo = valbool; |
528 | retv = 0; | |
529 | break; | |
1da177e4 | 530 | |
333fad53 | 531 | case IPV6_RECVHOPLIMIT: |
b2a9d7c2 YH |
532 | if (optlen < sizeof(int)) |
533 | goto e_inval; | |
1da177e4 LT |
534 | np->rxopt.bits.rxhlim = valbool; |
535 | retv = 0; | |
536 | break; | |
537 | ||
333fad53 | 538 | case IPV6_2292HOPLIMIT: |
b2a9d7c2 YH |
539 | if (optlen < sizeof(int)) |
540 | goto e_inval; | |
333fad53 YH |
541 | np->rxopt.bits.rxohlim = valbool; |
542 | retv = 0; | |
543 | break; | |
544 | ||
545 | case IPV6_RECVRTHDR: | |
b2a9d7c2 YH |
546 | if (optlen < sizeof(int)) |
547 | goto e_inval; | |
4c752098 | 548 | np->rxopt.bits.srcrt = valbool; |
1da177e4 LT |
549 | retv = 0; |
550 | break; | |
551 | ||
333fad53 | 552 | case IPV6_2292RTHDR: |
b2a9d7c2 YH |
553 | if (optlen < sizeof(int)) |
554 | goto e_inval; | |
4c752098 | 555 | np->rxopt.bits.osrcrt = valbool; |
333fad53 YH |
556 | retv = 0; |
557 | break; | |
558 | ||
559 | case IPV6_RECVHOPOPTS: | |
b2a9d7c2 YH |
560 | if (optlen < sizeof(int)) |
561 | goto e_inval; | |
1da177e4 LT |
562 | np->rxopt.bits.hopopts = valbool; |
563 | retv = 0; | |
564 | break; | |
565 | ||
333fad53 | 566 | case IPV6_2292HOPOPTS: |
b2a9d7c2 YH |
567 | if (optlen < sizeof(int)) |
568 | goto e_inval; | |
333fad53 YH |
569 | np->rxopt.bits.ohopopts = valbool; |
570 | retv = 0; | |
571 | break; | |
572 | ||
573 | case IPV6_RECVDSTOPTS: | |
b2a9d7c2 YH |
574 | if (optlen < sizeof(int)) |
575 | goto e_inval; | |
1da177e4 LT |
576 | np->rxopt.bits.dstopts = valbool; |
577 | retv = 0; | |
578 | break; | |
579 | ||
333fad53 | 580 | case IPV6_2292DSTOPTS: |
b2a9d7c2 YH |
581 | if (optlen < sizeof(int)) |
582 | goto e_inval; | |
333fad53 YH |
583 | np->rxopt.bits.odstopts = valbool; |
584 | retv = 0; | |
585 | break; | |
586 | ||
41a1f8ea | 587 | case IPV6_TCLASS: |
b2a9d7c2 YH |
588 | if (optlen < sizeof(int)) |
589 | goto e_inval; | |
d0ee011f | 590 | if (val < -1 || val > 0xff) |
41a1f8ea | 591 | goto e_inval; |
26ced1e4 GR |
592 | /* RFC 3542, 6.5: default traffic class of 0x0 */ |
593 | if (val == -1) | |
594 | val = 0; | |
9f7b3a69 MŻ |
595 | if (sk->sk_type == SOCK_STREAM) { |
596 | val &= ~INET_ECN_MASK; | |
597 | val |= np->tclass & INET_ECN_MASK; | |
598 | } | |
305e95bb MŻ |
599 | if (np->tclass != val) { |
600 | np->tclass = val; | |
601 | sk_dst_reset(sk); | |
602 | } | |
41a1f8ea YH |
603 | retv = 0; |
604 | break; | |
1ab1457c | 605 | |
41a1f8ea | 606 | case IPV6_RECVTCLASS: |
b2a9d7c2 YH |
607 | if (optlen < sizeof(int)) |
608 | goto e_inval; | |
41a1f8ea YH |
609 | np->rxopt.bits.rxtclass = valbool; |
610 | retv = 0; | |
611 | break; | |
612 | ||
1da177e4 | 613 | case IPV6_FLOWINFO: |
b2a9d7c2 YH |
614 | if (optlen < sizeof(int)) |
615 | goto e_inval; | |
1da177e4 LT |
616 | np->rxopt.bits.rxflow = valbool; |
617 | retv = 0; | |
618 | break; | |
619 | ||
793b1473 BH |
620 | case IPV6_RECVPATHMTU: |
621 | if (optlen < sizeof(int)) | |
622 | goto e_inval; | |
623 | np->rxopt.bits.rxpmtu = valbool; | |
624 | retv = 0; | |
625 | break; | |
626 | ||
6c468622 | 627 | case IPV6_TRANSPARENT: |
40cd308e MKL |
628 | if (valbool && !sockopt_ns_capable(net->user_ns, CAP_NET_RAW) && |
629 | !sockopt_ns_capable(net->user_ns, CAP_NET_ADMIN)) { | |
b889416b BS |
630 | retv = -EPERM; |
631 | break; | |
632 | } | |
6c468622 BS |
633 | if (optlen < sizeof(int)) |
634 | goto e_inval; | |
635 | /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */ | |
636 | inet_sk(sk)->transparent = valbool; | |
637 | retv = 0; | |
638 | break; | |
639 | ||
84e14fe3 MŻ |
640 | case IPV6_FREEBIND: |
641 | if (optlen < sizeof(int)) | |
642 | goto e_inval; | |
643 | /* we also don't have a separate freebind bit for IPV6 */ | |
644 | inet_sk(sk)->freebind = valbool; | |
645 | retv = 0; | |
646 | break; | |
647 | ||
6c468622 BS |
648 | case IPV6_RECVORIGDSTADDR: |
649 | if (optlen < sizeof(int)) | |
650 | goto e_inval; | |
651 | np->rxopt.bits.rxorigdstaddr = valbool; | |
652 | retv = 0; | |
653 | break; | |
654 | ||
333fad53 YH |
655 | case IPV6_HOPOPTS: |
656 | case IPV6_RTHDRDSTOPTS: | |
657 | case IPV6_RTHDR: | |
658 | case IPV6_DSTOPTS: | |
b84d2b73 | 659 | retv = ipv6_set_opt_hdr(sk, optname, optval, optlen); |
333fad53 | 660 | break; |
333fad53 | 661 | |
b24a2516 YH |
662 | case IPV6_PKTINFO: |
663 | { | |
664 | struct in6_pktinfo pkt; | |
665 | ||
666 | if (optlen == 0) | |
667 | goto e_inval; | |
894cfbc0 CH |
668 | else if (optlen < sizeof(struct in6_pktinfo) || |
669 | sockptr_is_null(optval)) | |
b24a2516 YH |
670 | goto e_inval; |
671 | ||
894cfbc0 CH |
672 | if (copy_from_sockptr(&pkt, optval, sizeof(pkt))) { |
673 | retv = -EFAULT; | |
674 | break; | |
b24a2516 | 675 | } |
d839a0eb | 676 | if (!sk_dev_equal_l3scope(sk, pkt.ipi6_ifindex)) |
b24a2516 YH |
677 | goto e_inval; |
678 | ||
679 | np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex; | |
4e3fd7a0 | 680 | np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr; |
b24a2516 YH |
681 | retv = 0; |
682 | break; | |
683 | } | |
684 | ||
333fad53 | 685 | case IPV6_2292PKTOPTIONS: |
1da177e4 LT |
686 | { |
687 | struct ipv6_txoptions *opt = NULL; | |
688 | struct msghdr msg; | |
4c9483b2 | 689 | struct flowi6 fl6; |
26879da5 | 690 | struct ipcm6_cookie ipc6; |
1da177e4 | 691 | |
4c9483b2 DM |
692 | memset(&fl6, 0, sizeof(fl6)); |
693 | fl6.flowi6_oif = sk->sk_bound_dev_if; | |
694 | fl6.flowi6_mark = sk->sk_mark; | |
1da177e4 LT |
695 | |
696 | if (optlen == 0) | |
697 | goto update; | |
698 | ||
699 | /* 1K is probably excessive | |
700 | * 1K is surely not enough, 2K per standard header is 16K. | |
701 | */ | |
702 | retv = -EINVAL; | |
703 | if (optlen > 64*1024) | |
704 | break; | |
705 | ||
706 | opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL); | |
707 | retv = -ENOBUFS; | |
63159f29 | 708 | if (!opt) |
1da177e4 LT |
709 | break; |
710 | ||
711 | memset(opt, 0, sizeof(*opt)); | |
0aeea21a | 712 | refcount_set(&opt->refcnt, 1); |
1da177e4 LT |
713 | opt->tot_len = sizeof(*opt) + optlen; |
714 | retv = -EFAULT; | |
894cfbc0 | 715 | if (copy_from_sockptr(opt + 1, optval, optlen)) |
1da177e4 LT |
716 | goto done; |
717 | ||
718 | msg.msg_controllen = optlen; | |
b6d85cf5 | 719 | msg.msg_control_is_user = false; |
67ba4152 | 720 | msg.msg_control = (void *)(opt+1); |
26879da5 | 721 | ipc6.opt = opt; |
1da177e4 | 722 | |
5fdaa88d | 723 | retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6); |
1da177e4 LT |
724 | if (retv) |
725 | goto done; | |
726 | update: | |
727 | retv = 0; | |
e7712f1a | 728 | opt = ipv6_update_options(sk, opt); |
1da177e4 | 729 | done: |
45f6fad8 ED |
730 | if (opt) { |
731 | atomic_sub(opt->tot_len, &sk->sk_omem_alloc); | |
732 | txopt_put(opt); | |
733 | } | |
1da177e4 LT |
734 | break; |
735 | } | |
736 | case IPV6_UNICAST_HOPS: | |
b2a9d7c2 YH |
737 | if (optlen < sizeof(int)) |
738 | goto e_inval; | |
1da177e4 LT |
739 | if (val > 255 || val < -1) |
740 | goto e_inval; | |
741 | np->hop_limit = val; | |
742 | retv = 0; | |
743 | break; | |
744 | ||
745 | case IPV6_MULTICAST_HOPS: | |
746 | if (sk->sk_type == SOCK_STREAM) | |
1717699c | 747 | break; |
b2a9d7c2 YH |
748 | if (optlen < sizeof(int)) |
749 | goto e_inval; | |
1da177e4 LT |
750 | if (val > 255 || val < -1) |
751 | goto e_inval; | |
2a38e6d5 | 752 | np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val); |
1da177e4 LT |
753 | retv = 0; |
754 | break; | |
755 | ||
756 | case IPV6_MULTICAST_LOOP: | |
b2a9d7c2 YH |
757 | if (optlen < sizeof(int)) |
758 | goto e_inval; | |
28d44882 YH |
759 | if (val != valbool) |
760 | goto e_inval; | |
1da177e4 LT |
761 | np->mc_loop = valbool; |
762 | retv = 0; | |
763 | break; | |
764 | ||
c4062dfc EH |
765 | case IPV6_UNICAST_IF: |
766 | { | |
767 | struct net_device *dev = NULL; | |
768 | int ifindex; | |
769 | ||
770 | if (optlen != sizeof(int)) | |
771 | goto e_inval; | |
772 | ||
773 | ifindex = (__force int)ntohl((__force __be32)val); | |
774 | if (ifindex == 0) { | |
775 | np->ucast_oif = 0; | |
776 | retv = 0; | |
777 | break; | |
778 | } | |
779 | ||
780 | dev = dev_get_by_index(net, ifindex); | |
781 | retv = -EADDRNOTAVAIL; | |
782 | if (!dev) | |
783 | break; | |
784 | dev_put(dev); | |
785 | ||
786 | retv = -EINVAL; | |
787 | if (sk->sk_bound_dev_if) | |
788 | break; | |
789 | ||
790 | np->ucast_oif = ifindex; | |
791 | retv = 0; | |
792 | break; | |
793 | } | |
794 | ||
1da177e4 LT |
795 | case IPV6_MULTICAST_IF: |
796 | if (sk->sk_type == SOCK_STREAM) | |
1717699c | 797 | break; |
b2a9d7c2 YH |
798 | if (optlen < sizeof(int)) |
799 | goto e_inval; | |
1da177e4 | 800 | |
4953f0fc | 801 | if (val) { |
55b80503 | 802 | struct net_device *dev; |
7bb387c5 | 803 | int midx; |
55b80503 | 804 | |
7bb387c5 | 805 | rcu_read_lock(); |
4953f0fc | 806 | |
7bb387c5 | 807 | dev = dev_get_by_index_rcu(net, val); |
55b80503 | 808 | if (!dev) { |
7bb387c5 | 809 | rcu_read_unlock(); |
4953f0fc BH |
810 | retv = -ENODEV; |
811 | break; | |
812 | } | |
7bb387c5 DA |
813 | midx = l3mdev_master_ifindex_rcu(dev); |
814 | ||
815 | rcu_read_unlock(); | |
816 | ||
817 | if (sk->sk_bound_dev_if && | |
818 | sk->sk_bound_dev_if != val && | |
819 | (!midx || midx != sk->sk_bound_dev_if)) | |
820 | goto e_inval; | |
1da177e4 LT |
821 | } |
822 | np->mcast_oif = val; | |
823 | retv = 0; | |
824 | break; | |
825 | case IPV6_ADD_MEMBERSHIP: | |
826 | case IPV6_DROP_MEMBERSHIP: | |
827 | { | |
828 | struct ipv6_mreq mreq; | |
829 | ||
a28398ba WC |
830 | if (optlen < sizeof(struct ipv6_mreq)) |
831 | goto e_inval; | |
832 | ||
a96fb49b FL |
833 | retv = -EPROTO; |
834 | if (inet_sk(sk)->is_icsk) | |
835 | break; | |
836 | ||
1da177e4 | 837 | retv = -EFAULT; |
894cfbc0 | 838 | if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq))) |
1da177e4 LT |
839 | break; |
840 | ||
841 | if (optname == IPV6_ADD_MEMBERSHIP) | |
54ff9ef3 | 842 | retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr); |
1da177e4 | 843 | else |
54ff9ef3 | 844 | retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr); |
1da177e4 LT |
845 | break; |
846 | } | |
847 | case IPV6_JOIN_ANYCAST: | |
848 | case IPV6_LEAVE_ANYCAST: | |
849 | { | |
850 | struct ipv6_mreq mreq; | |
851 | ||
a28398ba | 852 | if (optlen < sizeof(struct ipv6_mreq)) |
1da177e4 LT |
853 | goto e_inval; |
854 | ||
855 | retv = -EFAULT; | |
894cfbc0 | 856 | if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq))) |
1da177e4 LT |
857 | break; |
858 | ||
859 | if (optname == IPV6_JOIN_ANYCAST) | |
860 | retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr); | |
861 | else | |
862 | retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr); | |
863 | break; | |
864 | } | |
15033f04 AN |
865 | case IPV6_MULTICAST_ALL: |
866 | if (optlen < sizeof(int)) | |
867 | goto e_inval; | |
868 | np->mc_all = valbool; | |
869 | retv = 0; | |
870 | break; | |
871 | ||
1da177e4 LT |
872 | case MCAST_JOIN_GROUP: |
873 | case MCAST_LEAVE_GROUP: | |
3021ad52 CH |
874 | if (in_compat_syscall()) |
875 | retv = compat_ipv6_mcast_join_leave(sk, optname, optval, | |
876 | optlen); | |
877 | else | |
878 | retv = ipv6_mcast_join_leave(sk, optname, optval, | |
879 | optlen); | |
1da177e4 | 880 | break; |
1da177e4 LT |
881 | case MCAST_JOIN_SOURCE_GROUP: |
882 | case MCAST_LEAVE_SOURCE_GROUP: | |
883 | case MCAST_BLOCK_SOURCE: | |
884 | case MCAST_UNBLOCK_SOURCE: | |
3021ad52 | 885 | retv = do_ipv6_mcast_group_source(sk, optname, optval, optlen); |
1da177e4 | 886 | break; |
1da177e4 | 887 | case MCAST_MSFILTER: |
3021ad52 CH |
888 | if (in_compat_syscall()) |
889 | retv = compat_ipv6_set_mcast_msfilter(sk, optval, | |
890 | optlen); | |
891 | else | |
892 | retv = ipv6_set_mcast_msfilter(sk, optval, optlen); | |
1da177e4 | 893 | break; |
1da177e4 | 894 | case IPV6_ROUTER_ALERT: |
b2a9d7c2 YH |
895 | if (optlen < sizeof(int)) |
896 | goto e_inval; | |
725a8ff0 | 897 | retv = ip6_ra_control(sk, val); |
1da177e4 | 898 | break; |
9036b2fe FR |
899 | case IPV6_ROUTER_ALERT_ISOLATE: |
900 | if (optlen < sizeof(int)) | |
901 | goto e_inval; | |
902 | np->rtalert_isolate = valbool; | |
903 | retv = 0; | |
904 | break; | |
1da177e4 | 905 | case IPV6_MTU_DISCOVER: |
b2a9d7c2 YH |
906 | if (optlen < sizeof(int)) |
907 | goto e_inval; | |
0b95227a | 908 | if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT) |
1da177e4 LT |
909 | goto e_inval; |
910 | np->pmtudisc = val; | |
911 | retv = 0; | |
912 | break; | |
913 | case IPV6_MTU: | |
b2a9d7c2 YH |
914 | if (optlen < sizeof(int)) |
915 | goto e_inval; | |
1da177e4 LT |
916 | if (val && val < IPV6_MIN_MTU) |
917 | goto e_inval; | |
918 | np->frag_size = val; | |
919 | retv = 0; | |
920 | break; | |
921 | case IPV6_RECVERR: | |
b2a9d7c2 YH |
922 | if (optlen < sizeof(int)) |
923 | goto e_inval; | |
1da177e4 LT |
924 | np->recverr = valbool; |
925 | if (!val) | |
926 | skb_queue_purge(&sk->sk_error_queue); | |
927 | retv = 0; | |
928 | break; | |
929 | case IPV6_FLOWINFO_SEND: | |
b2a9d7c2 YH |
930 | if (optlen < sizeof(int)) |
931 | goto e_inval; | |
1da177e4 LT |
932 | np->sndflow = valbool; |
933 | retv = 0; | |
934 | break; | |
935 | case IPV6_FLOWLABEL_MGR: | |
894cfbc0 | 936 | retv = ipv6_flowlabel_opt(sk, optval, optlen); |
1da177e4 LT |
937 | break; |
938 | case IPV6_IPSEC_POLICY: | |
939 | case IPV6_XFRM_POLICY: | |
6fc0b4a7 | 940 | retv = -EPERM; |
40cd308e | 941 | if (!sockopt_ns_capable(net->user_ns, CAP_NET_ADMIN)) |
6fc0b4a7 | 942 | break; |
894cfbc0 | 943 | retv = xfrm_user_policy(sk, optname, optval, optlen); |
1da177e4 LT |
944 | break; |
945 | ||
7cbca67c | 946 | case IPV6_ADDR_PREFERENCES: |
b2a9d7c2 YH |
947 | if (optlen < sizeof(int)) |
948 | goto e_inval; | |
18d5ad62 | 949 | retv = __ip6_sock_set_addr_preferences(sk, val); |
7cbca67c | 950 | break; |
e802af9c SH |
951 | case IPV6_MINHOPCOUNT: |
952 | if (optlen < sizeof(int)) | |
953 | goto e_inval; | |
954 | if (val < 0 || val > 255) | |
955 | goto e_inval; | |
790eb673 ED |
956 | |
957 | if (val) | |
958 | static_branch_enable(&ip6_min_hopcount); | |
959 | ||
cc17c3c8 ED |
960 | /* tcp_v6_err() and tcp_v6_rcv() might read min_hopcount |
961 | * while we are changing it. | |
962 | */ | |
963 | WRITE_ONCE(np->min_hopcount, val); | |
d4596bad | 964 | retv = 0; |
793b1473 BH |
965 | break; |
966 | case IPV6_DONTFRAG: | |
967 | np->dontfrag = valbool; | |
e802af9c SH |
968 | retv = 0; |
969 | break; | |
cb1ce2ef TH |
970 | case IPV6_AUTOFLOWLABEL: |
971 | np->autoflowlabel = valbool; | |
513674b5 | 972 | np->autoflowlabel_set = 1; |
cb1ce2ef TH |
973 | retv = 0; |
974 | break; | |
0cc0aa61 WB |
975 | case IPV6_RECVFRAGSIZE: |
976 | np->rxopt.bits.recvfragsize = valbool; | |
977 | retv = 0; | |
978 | break; | |
01370434 WB |
979 | case IPV6_RECVERR_RFC4884: |
980 | if (optlen < sizeof(int)) | |
981 | goto e_inval; | |
982 | if (val < 0 || val > 1) | |
983 | goto e_inval; | |
984 | np->recverr_rfc4884 = valbool; | |
985 | retv = 0; | |
986 | break; | |
1da177e4 | 987 | } |
7cbca67c | 988 | |
3c52c6bb | 989 | unlock: |
40cd308e | 990 | sockopt_release_sock(sk); |
baf606d9 MRL |
991 | if (needs_rtnl) |
992 | rtnl_unlock(); | |
1da177e4 | 993 | |
1da177e4 LT |
994 | return retv; |
995 | ||
996 | e_inval: | |
b45a337f KI |
997 | retv = -EINVAL; |
998 | goto unlock; | |
1da177e4 LT |
999 | } |
1000 | ||
a7b75c5a CH |
1001 | int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, |
1002 | unsigned int optlen) | |
3fdadf7d DM |
1003 | { |
1004 | int err; | |
1005 | ||
1006 | if (level == SOL_IP && sk->sk_type != SOCK_RAW) | |
1007 | return udp_prot.setsockopt(sk, level, optname, optval, optlen); | |
1008 | ||
1009 | if (level != SOL_IPV6) | |
1010 | return -ENOPROTOOPT; | |
1011 | ||
a7b75c5a | 1012 | err = do_ipv6_setsockopt(sk, level, optname, optval, optlen); |
3fdadf7d DM |
1013 | #ifdef CONFIG_NETFILTER |
1014 | /* we need to exclude all possible ENOPROTOOPTs except default case */ | |
1015 | if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY && | |
3f34cfae | 1016 | optname != IPV6_XFRM_POLICY) |
a7b75c5a | 1017 | err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen); |
3fdadf7d DM |
1018 | #endif |
1019 | return err; | |
1020 | } | |
7159039a | 1021 | EXPORT_SYMBOL(ipv6_setsockopt); |
3fdadf7d | 1022 | |
28693079 | 1023 | static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt, |
6dadbe4b | 1024 | int optname, sockptr_t optval, int len) |
333fad53 | 1025 | { |
28693079 DM |
1026 | struct ipv6_opt_hdr *hdr; |
1027 | ||
4c6510a7 YH |
1028 | if (!opt) |
1029 | return 0; | |
1030 | ||
67ba4152 | 1031 | switch (optname) { |
4c6510a7 YH |
1032 | case IPV6_HOPOPTS: |
1033 | hdr = opt->hopopt; | |
1034 | break; | |
1035 | case IPV6_RTHDRDSTOPTS: | |
1036 | hdr = opt->dst0opt; | |
1037 | break; | |
1038 | case IPV6_RTHDR: | |
1039 | hdr = (struct ipv6_opt_hdr *)opt->srcrt; | |
1040 | break; | |
1041 | case IPV6_DSTOPTS: | |
1042 | hdr = opt->dst1opt; | |
1043 | break; | |
1044 | default: | |
1045 | return -EINVAL; /* should not happen */ | |
1046 | } | |
1047 | ||
1048 | if (!hdr) | |
333fad53 | 1049 | return 0; |
28693079 | 1050 | |
d2b02ed9 | 1051 | len = min_t(unsigned int, len, ipv6_optlen(hdr)); |
6dadbe4b | 1052 | if (copy_to_sockptr(optval, hdr, len)) |
333fad53 | 1053 | return -EFAULT; |
95b496b6 | 1054 | return len; |
333fad53 YH |
1055 | } |
1056 | ||
6dadbe4b MKL |
1057 | static int ipv6_get_msfilter(struct sock *sk, sockptr_t optval, |
1058 | sockptr_t optlen, int len) | |
d5541e85 | 1059 | { |
db243b79 | 1060 | const int size0 = offsetof(struct group_filter, gf_slist_flex); |
d5541e85 CH |
1061 | struct group_filter gsf; |
1062 | int num; | |
1063 | int err; | |
1064 | ||
1065 | if (len < size0) | |
1066 | return -EINVAL; | |
6dadbe4b | 1067 | if (copy_from_sockptr(&gsf, optval, size0)) |
d5541e85 CH |
1068 | return -EFAULT; |
1069 | if (gsf.gf_group.ss_family != AF_INET6) | |
1070 | return -EADDRNOTAVAIL; | |
1071 | num = gsf.gf_numsrc; | |
0f95f7d4 | 1072 | sockopt_lock_sock(sk); |
6dadbe4b | 1073 | err = ip6_mc_msfget(sk, &gsf, optval, size0); |
d5541e85 CH |
1074 | if (!err) { |
1075 | if (num > gsf.gf_numsrc) | |
1076 | num = gsf.gf_numsrc; | |
6dadbe4b MKL |
1077 | len = GROUP_FILTER_SIZE(num); |
1078 | if (copy_to_sockptr(optlen, &len, sizeof(int)) || | |
1079 | copy_to_sockptr(optval, &gsf, size0)) | |
d5541e85 CH |
1080 | err = -EFAULT; |
1081 | } | |
0f95f7d4 | 1082 | sockopt_release_sock(sk); |
d5541e85 CH |
1083 | return err; |
1084 | } | |
1085 | ||
6dadbe4b MKL |
1086 | static int compat_ipv6_get_msfilter(struct sock *sk, sockptr_t optval, |
1087 | sockptr_t optlen, int len) | |
d5541e85 | 1088 | { |
db243b79 | 1089 | const int size0 = offsetof(struct compat_group_filter, gf_slist_flex); |
d5541e85 CH |
1090 | struct compat_group_filter gf32; |
1091 | struct group_filter gf; | |
9c3f9707 | 1092 | int err; |
d5541e85 CH |
1093 | int num; |
1094 | ||
d5541e85 CH |
1095 | if (len < size0) |
1096 | return -EINVAL; | |
1097 | ||
6dadbe4b | 1098 | if (copy_from_sockptr(&gf32, optval, size0)) |
d5541e85 CH |
1099 | return -EFAULT; |
1100 | gf.gf_interface = gf32.gf_interface; | |
1101 | gf.gf_fmode = gf32.gf_fmode; | |
1102 | num = gf.gf_numsrc = gf32.gf_numsrc; | |
1103 | gf.gf_group = gf32.gf_group; | |
1104 | ||
1105 | if (gf.gf_group.ss_family != AF_INET6) | |
1106 | return -EADDRNOTAVAIL; | |
1107 | ||
0f95f7d4 | 1108 | sockopt_lock_sock(sk); |
6dadbe4b | 1109 | err = ip6_mc_msfget(sk, &gf, optval, size0); |
0f95f7d4 | 1110 | sockopt_release_sock(sk); |
d5541e85 CH |
1111 | if (err) |
1112 | return err; | |
1113 | if (num > gf.gf_numsrc) | |
1114 | num = gf.gf_numsrc; | |
1115 | len = GROUP_FILTER_SIZE(num) - (sizeof(gf)-sizeof(gf32)); | |
6dadbe4b MKL |
1116 | if (copy_to_sockptr(optlen, &len, sizeof(int)) || |
1117 | copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_fmode), | |
1118 | &gf.gf_fmode, sizeof(gf32.gf_fmode)) || | |
1119 | copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_numsrc), | |
1120 | &gf.gf_numsrc, sizeof(gf32.gf_numsrc))) | |
d5541e85 CH |
1121 | return -EFAULT; |
1122 | return 0; | |
1123 | } | |
d5541e85 | 1124 | |
38566ec0 MKL |
1125 | int do_ipv6_getsockopt(struct sock *sk, int level, int optname, |
1126 | sockptr_t optval, sockptr_t optlen) | |
1da177e4 LT |
1127 | { |
1128 | struct ipv6_pinfo *np = inet6_sk(sk); | |
1129 | int len; | |
1130 | int val; | |
1131 | ||
7bc570c8 YH |
1132 | if (ip6_mroute_opt(optname)) |
1133 | return ip6_mroute_getsockopt(sk, optname, optval, optlen); | |
1134 | ||
6dadbe4b | 1135 | if (copy_from_sockptr(&len, optlen, sizeof(int))) |
1da177e4 LT |
1136 | return -EFAULT; |
1137 | switch (optname) { | |
1138 | case IPV6_ADDRFORM: | |
1139 | if (sk->sk_protocol != IPPROTO_UDP && | |
ba4e58ec | 1140 | sk->sk_protocol != IPPROTO_UDPLITE && |
1da177e4 | 1141 | sk->sk_protocol != IPPROTO_TCP) |
1717699c | 1142 | return -ENOPROTOOPT; |
1da177e4 LT |
1143 | if (sk->sk_state != TCP_ESTABLISHED) |
1144 | return -ENOTCONN; | |
1145 | val = sk->sk_family; | |
1146 | break; | |
1147 | case MCAST_MSFILTER: | |
3021ad52 | 1148 | if (in_compat_syscall()) |
9c3f9707 | 1149 | return compat_ipv6_get_msfilter(sk, optval, optlen, len); |
d5541e85 | 1150 | return ipv6_get_msfilter(sk, optval, optlen, len); |
333fad53 | 1151 | case IPV6_2292PKTOPTIONS: |
1da177e4 LT |
1152 | { |
1153 | struct msghdr msg; | |
1154 | struct sk_buff *skb; | |
1155 | ||
1156 | if (sk->sk_type != SOCK_STREAM) | |
1157 | return -ENOPROTOOPT; | |
1158 | ||
6dadbe4b MKL |
1159 | if (optval.is_kernel) { |
1160 | msg.msg_control_is_user = false; | |
1161 | msg.msg_control = optval.kernel; | |
1162 | } else { | |
1163 | msg.msg_control_is_user = true; | |
1164 | msg.msg_control_user = optval.user; | |
1165 | } | |
1da177e4 | 1166 | msg.msg_controllen = len; |
75f23979 | 1167 | msg.msg_flags = 0; |
1da177e4 | 1168 | |
0f95f7d4 | 1169 | sockopt_lock_sock(sk); |
1da177e4 LT |
1170 | skb = np->pktoptions; |
1171 | if (skb) | |
4b261c75 | 1172 | ip6_datagram_recv_ctl(sk, &msg, skb); |
0f95f7d4 | 1173 | sockopt_release_sock(sk); |
1dc7b90f | 1174 | if (!skb) { |
1da177e4 LT |
1175 | if (np->rxopt.bits.rxinfo) { |
1176 | struct in6_pktinfo src_info; | |
f250dcda YH |
1177 | src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif : |
1178 | np->sticky_pktinfo.ipi6_ifindex; | |
efe4208f | 1179 | src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr; |
1da177e4 LT |
1180 | put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info); |
1181 | } | |
1182 | if (np->rxopt.bits.rxhlim) { | |
1183 | int hlim = np->mcast_hops; | |
1184 | put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim); | |
1185 | } | |
4c507d28 | 1186 | if (np->rxopt.bits.rxtclass) { |
d76ed22b LR |
1187 | int tclass = (int)ip6_tclass(np->rcv_flowinfo); |
1188 | ||
4c507d28 JB |
1189 | put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass); |
1190 | } | |
333fad53 YH |
1191 | if (np->rxopt.bits.rxoinfo) { |
1192 | struct in6_pktinfo src_info; | |
f250dcda YH |
1193 | src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif : |
1194 | np->sticky_pktinfo.ipi6_ifindex; | |
efe4208f ED |
1195 | src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : |
1196 | np->sticky_pktinfo.ipi6_addr; | |
333fad53 YH |
1197 | put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info); |
1198 | } | |
1199 | if (np->rxopt.bits.rxohlim) { | |
1200 | int hlim = np->mcast_hops; | |
1201 | put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim); | |
1202 | } | |
1397ed35 | 1203 | if (np->rxopt.bits.rxflow) { |
68536053 FF |
1204 | __be32 flowinfo = np->rcv_flowinfo; |
1205 | ||
1397ed35 FF |
1206 | put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo); |
1207 | } | |
1da177e4 LT |
1208 | } |
1209 | len -= msg.msg_controllen; | |
6dadbe4b | 1210 | return copy_to_sockptr(optlen, &len, sizeof(int)); |
1da177e4 LT |
1211 | } |
1212 | case IPV6_MTU: | |
1213 | { | |
1214 | struct dst_entry *dst; | |
b6c6712a | 1215 | |
1ab1457c | 1216 | val = 0; |
b6c6712a ED |
1217 | rcu_read_lock(); |
1218 | dst = __sk_dst_get(sk); | |
1219 | if (dst) | |
1da177e4 | 1220 | val = dst_mtu(dst); |
b6c6712a | 1221 | rcu_read_unlock(); |
1da177e4 LT |
1222 | if (!val) |
1223 | return -ENOTCONN; | |
1224 | break; | |
1225 | } | |
1226 | ||
1227 | case IPV6_V6ONLY: | |
9fe516ba | 1228 | val = sk->sk_ipv6only; |
1da177e4 LT |
1229 | break; |
1230 | ||
333fad53 | 1231 | case IPV6_RECVPKTINFO: |
1da177e4 LT |
1232 | val = np->rxopt.bits.rxinfo; |
1233 | break; | |
1234 | ||
333fad53 YH |
1235 | case IPV6_2292PKTINFO: |
1236 | val = np->rxopt.bits.rxoinfo; | |
1237 | break; | |
1238 | ||
1239 | case IPV6_RECVHOPLIMIT: | |
1da177e4 LT |
1240 | val = np->rxopt.bits.rxhlim; |
1241 | break; | |
1242 | ||
333fad53 YH |
1243 | case IPV6_2292HOPLIMIT: |
1244 | val = np->rxopt.bits.rxohlim; | |
1245 | break; | |
1246 | ||
1247 | case IPV6_RECVRTHDR: | |
1da177e4 LT |
1248 | val = np->rxopt.bits.srcrt; |
1249 | break; | |
1250 | ||
333fad53 YH |
1251 | case IPV6_2292RTHDR: |
1252 | val = np->rxopt.bits.osrcrt; | |
1253 | break; | |
1254 | ||
1da177e4 | 1255 | case IPV6_HOPOPTS: |
333fad53 YH |
1256 | case IPV6_RTHDRDSTOPTS: |
1257 | case IPV6_RTHDR: | |
1258 | case IPV6_DSTOPTS: | |
1259 | { | |
45f6fad8 | 1260 | struct ipv6_txoptions *opt; |
333fad53 | 1261 | |
0f95f7d4 | 1262 | sockopt_lock_sock(sk); |
1e1d04e6 HFS |
1263 | opt = rcu_dereference_protected(np->opt, |
1264 | lockdep_sock_is_held(sk)); | |
45f6fad8 | 1265 | len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len); |
0f95f7d4 | 1266 | sockopt_release_sock(sk); |
05335c22 YH |
1267 | /* check if ipv6_getsockopt_sticky() returns err code */ |
1268 | if (len < 0) | |
1269 | return len; | |
6dadbe4b | 1270 | return copy_to_sockptr(optlen, &len, sizeof(int)); |
333fad53 YH |
1271 | } |
1272 | ||
1273 | case IPV6_RECVHOPOPTS: | |
1da177e4 LT |
1274 | val = np->rxopt.bits.hopopts; |
1275 | break; | |
1276 | ||
333fad53 YH |
1277 | case IPV6_2292HOPOPTS: |
1278 | val = np->rxopt.bits.ohopopts; | |
1279 | break; | |
1280 | ||
1281 | case IPV6_RECVDSTOPTS: | |
1da177e4 LT |
1282 | val = np->rxopt.bits.dstopts; |
1283 | break; | |
1284 | ||
333fad53 YH |
1285 | case IPV6_2292DSTOPTS: |
1286 | val = np->rxopt.bits.odstopts; | |
1287 | break; | |
1288 | ||
41a1f8ea YH |
1289 | case IPV6_TCLASS: |
1290 | val = np->tclass; | |
1291 | break; | |
1292 | ||
1293 | case IPV6_RECVTCLASS: | |
1294 | val = np->rxopt.bits.rxtclass; | |
1295 | break; | |
1296 | ||
1da177e4 LT |
1297 | case IPV6_FLOWINFO: |
1298 | val = np->rxopt.bits.rxflow; | |
1299 | break; | |
1300 | ||
793b1473 BH |
1301 | case IPV6_RECVPATHMTU: |
1302 | val = np->rxopt.bits.rxpmtu; | |
1303 | break; | |
1304 | ||
1305 | case IPV6_PATHMTU: | |
1306 | { | |
1307 | struct dst_entry *dst; | |
1308 | struct ip6_mtuinfo mtuinfo; | |
1309 | ||
1310 | if (len < sizeof(mtuinfo)) | |
1311 | return -EINVAL; | |
1312 | ||
1313 | len = sizeof(mtuinfo); | |
1314 | memset(&mtuinfo, 0, sizeof(mtuinfo)); | |
1315 | ||
1316 | rcu_read_lock(); | |
1317 | dst = __sk_dst_get(sk); | |
1318 | if (dst) | |
1319 | mtuinfo.ip6m_mtu = dst_mtu(dst); | |
1320 | rcu_read_unlock(); | |
1321 | if (!mtuinfo.ip6m_mtu) | |
1322 | return -ENOTCONN; | |
1323 | ||
6dadbe4b | 1324 | if (copy_to_sockptr(optlen, &len, sizeof(int))) |
793b1473 | 1325 | return -EFAULT; |
6dadbe4b | 1326 | if (copy_to_sockptr(optval, &mtuinfo, len)) |
793b1473 BH |
1327 | return -EFAULT; |
1328 | ||
1329 | return 0; | |
793b1473 BH |
1330 | } |
1331 | ||
6c468622 BS |
1332 | case IPV6_TRANSPARENT: |
1333 | val = inet_sk(sk)->transparent; | |
1334 | break; | |
1335 | ||
84e14fe3 MŻ |
1336 | case IPV6_FREEBIND: |
1337 | val = inet_sk(sk)->freebind; | |
1338 | break; | |
1339 | ||
6c468622 BS |
1340 | case IPV6_RECVORIGDSTADDR: |
1341 | val = np->rxopt.bits.rxorigdstaddr; | |
1342 | break; | |
1343 | ||
1da177e4 | 1344 | case IPV6_UNICAST_HOPS: |
1da177e4 | 1345 | case IPV6_MULTICAST_HOPS: |
befffe90 BH |
1346 | { |
1347 | struct dst_entry *dst; | |
1348 | ||
1349 | if (optname == IPV6_UNICAST_HOPS) | |
1350 | val = np->hop_limit; | |
1351 | else | |
1352 | val = np->mcast_hops; | |
1353 | ||
b6c6712a ED |
1354 | if (val < 0) { |
1355 | rcu_read_lock(); | |
1356 | dst = __sk_dst_get(sk); | |
1357 | if (dst) | |
6b75d090 | 1358 | val = ip6_dst_hoplimit(dst); |
b6c6712a | 1359 | rcu_read_unlock(); |
befffe90 | 1360 | } |
b6c6712a | 1361 | |
befffe90 | 1362 | if (val < 0) |
53b7997f | 1363 | val = sock_net(sk)->ipv6.devconf_all->hop_limit; |
1da177e4 | 1364 | break; |
befffe90 | 1365 | } |
1da177e4 LT |
1366 | |
1367 | case IPV6_MULTICAST_LOOP: | |
1368 | val = np->mc_loop; | |
1369 | break; | |
1370 | ||
1371 | case IPV6_MULTICAST_IF: | |
1372 | val = np->mcast_oif; | |
1373 | break; | |
1374 | ||
15033f04 AN |
1375 | case IPV6_MULTICAST_ALL: |
1376 | val = np->mc_all; | |
1377 | break; | |
1378 | ||
c4062dfc EH |
1379 | case IPV6_UNICAST_IF: |
1380 | val = (__force int)htonl((__u32) np->ucast_oif); | |
1381 | break; | |
1382 | ||
1da177e4 LT |
1383 | case IPV6_MTU_DISCOVER: |
1384 | val = np->pmtudisc; | |
1385 | break; | |
1386 | ||
1387 | case IPV6_RECVERR: | |
1388 | val = np->recverr; | |
1389 | break; | |
1390 | ||
1391 | case IPV6_FLOWINFO_SEND: | |
1392 | val = np->sndflow; | |
1393 | break; | |
1394 | ||
3fdfa5ff FF |
1395 | case IPV6_FLOWLABEL_MGR: |
1396 | { | |
1397 | struct in6_flowlabel_req freq; | |
46e5f401 | 1398 | int flags; |
3fdfa5ff FF |
1399 | |
1400 | if (len < sizeof(freq)) | |
1401 | return -EINVAL; | |
1402 | ||
6dadbe4b | 1403 | if (copy_from_sockptr(&freq, optval, sizeof(freq))) |
3fdfa5ff FF |
1404 | return -EFAULT; |
1405 | ||
1406 | if (freq.flr_action != IPV6_FL_A_GET) | |
1407 | return -EINVAL; | |
1408 | ||
1409 | len = sizeof(freq); | |
46e5f401 FF |
1410 | flags = freq.flr_flags; |
1411 | ||
3fdfa5ff FF |
1412 | memset(&freq, 0, sizeof(freq)); |
1413 | ||
46e5f401 | 1414 | val = ipv6_flowlabel_opt_get(sk, &freq, flags); |
3fdfa5ff FF |
1415 | if (val < 0) |
1416 | return val; | |
1417 | ||
6dadbe4b | 1418 | if (copy_to_sockptr(optlen, &len, sizeof(int))) |
3fdfa5ff | 1419 | return -EFAULT; |
6dadbe4b | 1420 | if (copy_to_sockptr(optval, &freq, len)) |
3fdfa5ff FF |
1421 | return -EFAULT; |
1422 | ||
1423 | return 0; | |
1424 | } | |
1425 | ||
7cbca67c YH |
1426 | case IPV6_ADDR_PREFERENCES: |
1427 | val = 0; | |
1428 | ||
1429 | if (np->srcprefs & IPV6_PREFER_SRC_TMP) | |
1430 | val |= IPV6_PREFER_SRC_TMP; | |
1431 | else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC) | |
1432 | val |= IPV6_PREFER_SRC_PUBLIC; | |
1433 | else { | |
1434 | /* XXX: should we return system default? */ | |
1435 | val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT; | |
1436 | } | |
1437 | ||
1438 | if (np->srcprefs & IPV6_PREFER_SRC_COA) | |
1439 | val |= IPV6_PREFER_SRC_COA; | |
1440 | else | |
1441 | val |= IPV6_PREFER_SRC_HOME; | |
1442 | break; | |
e802af9c SH |
1443 | |
1444 | case IPV6_MINHOPCOUNT: | |
1445 | val = np->min_hopcount; | |
1446 | break; | |
793b1473 BH |
1447 | |
1448 | case IPV6_DONTFRAG: | |
1449 | val = np->dontfrag; | |
1450 | break; | |
7cbca67c | 1451 | |
cb1ce2ef | 1452 | case IPV6_AUTOFLOWLABEL: |
e9191ffb | 1453 | val = ip6_autoflowlabel(sock_net(sk), np); |
cb1ce2ef TH |
1454 | break; |
1455 | ||
0cc0aa61 WB |
1456 | case IPV6_RECVFRAGSIZE: |
1457 | val = np->rxopt.bits.recvfragsize; | |
1458 | break; | |
1459 | ||
9036b2fe FR |
1460 | case IPV6_ROUTER_ALERT_ISOLATE: |
1461 | val = np->rtalert_isolate; | |
1462 | break; | |
1463 | ||
01370434 WB |
1464 | case IPV6_RECVERR_RFC4884: |
1465 | val = np->recverr_rfc4884; | |
1466 | break; | |
1467 | ||
1da177e4 | 1468 | default: |
cf6fc4a9 | 1469 | return -ENOPROTOOPT; |
1da177e4 LT |
1470 | } |
1471 | len = min_t(unsigned int, sizeof(int), len); | |
6dadbe4b | 1472 | if (copy_to_sockptr(optlen, &len, sizeof(int))) |
1da177e4 | 1473 | return -EFAULT; |
6dadbe4b | 1474 | if (copy_to_sockptr(optval, &val, len)) |
1da177e4 LT |
1475 | return -EFAULT; |
1476 | return 0; | |
1477 | } | |
1478 | ||
3fdadf7d DM |
1479 | int ipv6_getsockopt(struct sock *sk, int level, int optname, |
1480 | char __user *optval, int __user *optlen) | |
1481 | { | |
1482 | int err; | |
1483 | ||
1484 | if (level == SOL_IP && sk->sk_type != SOCK_RAW) | |
1485 | return udp_prot.getsockopt(sk, level, optname, optval, optlen); | |
1486 | ||
67ba4152 | 1487 | if (level != SOL_IPV6) |
3fdadf7d DM |
1488 | return -ENOPROTOOPT; |
1489 | ||
6dadbe4b MKL |
1490 | err = do_ipv6_getsockopt(sk, level, optname, |
1491 | USER_SOCKPTR(optval), USER_SOCKPTR(optlen)); | |
3fdadf7d | 1492 | #ifdef CONFIG_NETFILTER |
cf6fc4a9 WY |
1493 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
1494 | if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) { | |
3fdadf7d DM |
1495 | int len; |
1496 | ||
1497 | if (get_user(len, optlen)) | |
1498 | return -EFAULT; | |
1499 | ||
01ea306f | 1500 | err = nf_getsockopt(sk, PF_INET6, optname, optval, &len); |
3fdadf7d DM |
1501 | if (err >= 0) |
1502 | err = put_user(len, optlen); | |
1503 | } | |
1504 | #endif | |
1505 | return err; | |
1506 | } | |
7159039a | 1507 | EXPORT_SYMBOL(ipv6_getsockopt); |