Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * IPv6 BSD socket options interface |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 4 | * Linux INET6 implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * Authors: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 7 | * Pedro Roque <roque@di.fc.ul.pt> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * Based on linux/net/ipv4/ip_sockglue.c |
| 10 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * FIXME: Make the setsockopt code POSIX compliant: That is |
| 12 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 22 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/errno.h> |
| 24 | #include <linux/types.h> |
| 25 | #include <linux/socket.h> |
| 26 | #include <linux/sockios.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/net.h> |
| 28 | #include <linux/in6.h> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 29 | #include <linux/mroute6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 48 | #include <net/udplite.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <net/xfrm.h> |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 50 | #include <net/compat.h> |
David Lebrun | a149e7c | 2016-11-08 14:59:21 +0100 | [diff] [blame] | 51 | #include <net/seg6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 53 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | struct ip6_ra_chain *ip6_ra_chain; |
| 56 | DEFINE_RWLOCK(ip6_ra_lock); |
| 57 | |
Eric Dumazet | 790eb67 | 2021-10-25 09:48:22 -0700 | [diff] [blame] | 58 | DEFINE_STATIC_KEY_FALSE(ip6_min_hopcount); |
| 59 | |
Denis V. Lunev | 725a8ff | 2008-07-19 00:28:58 -0700 | [diff] [blame] | 60 | int ip6_ra_control(struct sock *sk, int sel) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
| 62 | struct ip6_ra_chain *ra, *new_ra, **rap; |
| 63 | |
| 64 | /* RA packet may be delivered ONLY to IPPROTO_RAW socket */ |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 65 | if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num != IPPROTO_RAW) |
YOSHIFUJI Hideaki | 1717699 | 2008-06-12 03:27:26 +0900 | [diff] [blame] | 66 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 68 | new_ra = (sel >= 0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL; |
Gen Zhang | 95baa60 | 2019-05-24 11:19:46 +0800 | [diff] [blame] | 69 | if (sel >= 0 && !new_ra) |
| 70 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | write_lock_bh(&ip6_ra_lock); |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 73 | for (rap = &ip6_ra_chain; (ra = *rap) != NULL; rap = &ra->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | if (ra->sk == sk) { |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 75 | if (sel >= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | write_unlock_bh(&ip6_ra_lock); |
Jesper Juhl | a51482b | 2005-11-08 09:41:34 -0800 | [diff] [blame] | 77 | kfree(new_ra); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | return -EADDRINUSE; |
| 79 | } |
| 80 | |
| 81 | *rap = ra->next; |
| 82 | write_unlock_bh(&ip6_ra_lock); |
| 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | sock_put(sk); |
| 85 | kfree(ra); |
| 86 | return 0; |
| 87 | } |
| 88 | } |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 89 | if (!new_ra) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | write_unlock_bh(&ip6_ra_lock); |
| 91 | return -ENOBUFS; |
| 92 | } |
| 93 | new_ra->sk = sk; |
| 94 | new_ra->sel = sel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
YOSHIFUJI Hideaki | e7712f1 | 2008-04-13 23:21:52 -0700 | [diff] [blame] | 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)) && |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 108 | inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) { |
YOSHIFUJI Hideaki | e7712f1 | 2008-04-13 23:21:52 -0700 | [diff] [blame] | 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 | } |
YOSHIFUJI Hideaki | e7712f1 | 2008-04-13 23:21:52 -0700 | [diff] [blame] | 113 | } |
Eric Dumazet | 45f6fad | 2015-11-29 19:37:57 -0800 | [diff] [blame] | 114 | opt = xchg((__force struct ipv6_txoptions **)&inet6_sk(sk)->opt, |
| 115 | opt); |
YOSHIFUJI Hideaki | e7712f1 | 2008-04-13 23:21:52 -0700 | [diff] [blame] | 116 | sk_dst_reset(sk); |
| 117 | |
| 118 | return opt; |
| 119 | } |
| 120 | |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 121 | static bool setsockopt_needs_rtnl(int optname) |
| 122 | { |
| 123 | switch (optname) { |
WANG Cong | 8651be8 | 2016-10-19 23:35:12 -0700 | [diff] [blame] | 124 | case IPV6_ADDRFORM: |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 125 | case IPV6_ADD_MEMBERSHIP: |
| 126 | case IPV6_DROP_MEMBERSHIP: |
Marcelo Ricardo Leitner | c4a6853 | 2015-03-20 11:37:17 -0300 | [diff] [blame] | 127 | case IPV6_JOIN_ANYCAST: |
| 128 | case IPV6_LEAVE_ANYCAST: |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 129 | case MCAST_JOIN_GROUP: |
| 130 | case MCAST_LEAVE_GROUP: |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 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: |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | return false; |
| 139 | } |
| 140 | |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 141 | static int copy_group_source_from_sockptr(struct group_source_req *greqs, |
| 142 | sockptr_t optval, int optlen) |
Al Viro | fcfa0b0 | 2020-04-27 11:00:01 -0400 | [diff] [blame] | 143 | { |
Christoph Hellwig | 3021ad5 | 2020-07-17 08:23:30 +0200 | [diff] [blame] | 144 | if (in_compat_syscall()) { |
| 145 | struct compat_group_source_req gr32; |
Al Viro | fcfa0b0 | 2020-04-27 11:00:01 -0400 | [diff] [blame] | 146 | |
Christoph Hellwig | 3021ad5 | 2020-07-17 08:23:30 +0200 | [diff] [blame] | 147 | if (optlen < sizeof(gr32)) |
| 148 | return -EINVAL; |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 149 | if (copy_from_sockptr(&gr32, optval, sizeof(gr32))) |
Christoph Hellwig | 3021ad5 | 2020-07-17 08:23:30 +0200 | [diff] [blame] | 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; |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 157 | if (copy_from_sockptr(greqs, optval, sizeof(*greqs))) |
Christoph Hellwig | 3021ad5 | 2020-07-17 08:23:30 +0200 | [diff] [blame] | 158 | return -EFAULT; |
| 159 | } |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int do_ipv6_mcast_group_source(struct sock *sk, int optname, |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 165 | sockptr_t optval, int optlen) |
Christoph Hellwig | 3021ad5 | 2020-07-17 08:23:30 +0200 | [diff] [blame] | 166 | { |
| 167 | struct group_source_req greqs; |
| 168 | int omode, add; |
| 169 | int ret; |
| 170 | |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 171 | ret = copy_group_source_from_sockptr(&greqs, optval, optlen); |
Christoph Hellwig | 3021ad5 | 2020-07-17 08:23:30 +0200 | [diff] [blame] | 172 | if (ret) |
| 173 | return ret; |
| 174 | |
| 175 | if (greqs.gsr_group.ss_family != AF_INET6 || |
| 176 | greqs.gsr_source.ss_family != AF_INET6) |
Al Viro | fcfa0b0 | 2020-04-27 11:00:01 -0400 | [diff] [blame] | 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 | |
Christoph Hellwig | 3021ad5 | 2020-07-17 08:23:30 +0200 | [diff] [blame] | 189 | psin6 = (struct sockaddr_in6 *)&greqs.gsr_group; |
| 190 | retv = ipv6_sock_mc_join_ssm(sk, greqs.gsr_interface, |
Al Viro | fcfa0b0 | 2020-04-27 11:00:01 -0400 | [diff] [blame] | 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 | } |
Christoph Hellwig | 3021ad5 | 2020-07-17 08:23:30 +0200 | [diff] [blame] | 202 | return ip6_mc_source(add, omode, sk, &greqs); |
Al Viro | fcfa0b0 | 2020-04-27 11:00:01 -0400 | [diff] [blame] | 203 | } |
| 204 | |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 205 | static int ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval, |
Christoph Hellwig | ca0e65e | 2020-07-17 08:23:28 +0200 | [diff] [blame] | 206 | int optlen) |
| 207 | { |
| 208 | struct group_filter *gsf; |
| 209 | int ret; |
| 210 | |
| 211 | if (optlen < GROUP_FILTER_SIZE(0)) |
| 212 | return -EINVAL; |
| 213 | if (optlen > sysctl_optmem_max) |
| 214 | return -ENOBUFS; |
| 215 | |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 216 | gsf = memdup_sockptr(optval, optlen); |
Christoph Hellwig | ca0e65e | 2020-07-17 08:23:28 +0200 | [diff] [blame] | 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 | |
Gustavo A. R. Silva | db243b7 | 2021-08-04 15:45:36 -0500 | [diff] [blame] | 230 | ret = ip6_mc_msfilter(sk, gsf, gsf->gf_slist_flex); |
Christoph Hellwig | ca0e65e | 2020-07-17 08:23:28 +0200 | [diff] [blame] | 231 | out_free_gsf: |
| 232 | kfree(gsf); |
| 233 | return ret; |
| 234 | } |
| 235 | |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 236 | static int compat_ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval, |
Christoph Hellwig | ca0e65e | 2020-07-17 08:23:28 +0200 | [diff] [blame] | 237 | int optlen) |
| 238 | { |
Gustavo A. R. Silva | db243b7 | 2021-08-04 15:45:36 -0500 | [diff] [blame] | 239 | const int size0 = offsetof(struct compat_group_filter, gf_slist_flex); |
Christoph Hellwig | ca0e65e | 2020-07-17 08:23:28 +0200 | [diff] [blame] | 240 | struct compat_group_filter *gf32; |
| 241 | void *p; |
| 242 | int ret; |
| 243 | int n; |
| 244 | |
| 245 | if (optlen < size0) |
| 246 | return -EINVAL; |
| 247 | if (optlen > sysctl_optmem_max - 4) |
| 248 | return -ENOBUFS; |
| 249 | |
| 250 | p = kmalloc(optlen + 4, GFP_KERNEL); |
| 251 | if (!p) |
| 252 | return -ENOMEM; |
| 253 | |
Gustavo A. R. Silva | db243b7 | 2021-08-04 15:45:36 -0500 | [diff] [blame] | 254 | gf32 = p + 4; /* we want ->gf_group and ->gf_slist_flex aligned */ |
Christoph Hellwig | ca0e65e | 2020-07-17 08:23:28 +0200 | [diff] [blame] | 255 | ret = -EFAULT; |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 256 | if (copy_from_sockptr(gf32, optval, optlen)) |
Christoph Hellwig | ca0e65e | 2020-07-17 08:23:28 +0200 | [diff] [blame] | 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; |
Gustavo A. R. Silva | db243b7 | 2021-08-04 15:45:36 -0500 | [diff] [blame] | 266 | if (offsetof(struct compat_group_filter, gf_slist_flex[n]) > optlen) |
Christoph Hellwig | ca0e65e | 2020-07-17 08:23:28 +0200 | [diff] [blame] | 267 | goto out_free_p; |
| 268 | |
Christoph Hellwig | ca0e65e | 2020-07-17 08:23:28 +0200 | [diff] [blame] | 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, |
Gustavo A. R. Silva | db243b7 | 2021-08-04 15:45:36 -0500 | [diff] [blame] | 273 | .gf_numsrc = gf32->gf_numsrc}, gf32->gf_slist_flex); |
Christoph Hellwig | ca0e65e | 2020-07-17 08:23:28 +0200 | [diff] [blame] | 274 | |
| 275 | out_free_p: |
| 276 | kfree(p); |
| 277 | return ret; |
| 278 | } |
Christoph Hellwig | ca0e65e | 2020-07-17 08:23:28 +0200 | [diff] [blame] | 279 | |
Christoph Hellwig | fdf5bdd | 2020-07-17 08:23:29 +0200 | [diff] [blame] | 280 | static int ipv6_mcast_join_leave(struct sock *sk, int optname, |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 281 | sockptr_t optval, int optlen) |
Christoph Hellwig | fdf5bdd | 2020-07-17 08:23:29 +0200 | [diff] [blame] | 282 | { |
| 283 | struct sockaddr_in6 *psin6; |
| 284 | struct group_req greq; |
| 285 | |
| 286 | if (optlen < sizeof(greq)) |
| 287 | return -EINVAL; |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 288 | if (copy_from_sockptr(&greq, optval, sizeof(greq))) |
Christoph Hellwig | fdf5bdd | 2020-07-17 08:23:29 +0200 | [diff] [blame] | 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 | |
Christoph Hellwig | fdf5bdd | 2020-07-17 08:23:29 +0200 | [diff] [blame] | 300 | static int compat_ipv6_mcast_join_leave(struct sock *sk, int optname, |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 301 | sockptr_t optval, int optlen) |
Christoph Hellwig | fdf5bdd | 2020-07-17 08:23:29 +0200 | [diff] [blame] | 302 | { |
| 303 | struct compat_group_req gr32; |
| 304 | struct sockaddr_in6 *psin6; |
Christoph Hellwig | fdf5bdd | 2020-07-17 08:23:29 +0200 | [diff] [blame] | 305 | |
| 306 | if (optlen < sizeof(gr32)) |
| 307 | return -EINVAL; |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 308 | if (copy_from_sockptr(&gr32, optval, sizeof(gr32))) |
Christoph Hellwig | fdf5bdd | 2020-07-17 08:23:29 +0200 | [diff] [blame] | 309 | return -EFAULT; |
| 310 | |
| 311 | if (gr32.gr_group.ss_family != AF_INET6) |
| 312 | return -EADDRNOTAVAIL; |
Christoph Hellwig | fdf5bdd | 2020-07-17 08:23:29 +0200 | [diff] [blame] | 313 | psin6 = (struct sockaddr_in6 *)&gr32.gr_group; |
| 314 | if (optname == MCAST_JOIN_GROUP) |
Christoph Hellwig | 3021ad5 | 2020-07-17 08:23:30 +0200 | [diff] [blame] | 315 | return ipv6_sock_mc_join(sk, gr32.gr_interface, |
Christoph Hellwig | fdf5bdd | 2020-07-17 08:23:29 +0200 | [diff] [blame] | 316 | &psin6->sin6_addr); |
Christoph Hellwig | 3021ad5 | 2020-07-17 08:23:30 +0200 | [diff] [blame] | 317 | return ipv6_sock_mc_drop(sk, gr32.gr_interface, &psin6->sin6_addr); |
Christoph Hellwig | fdf5bdd | 2020-07-17 08:23:29 +0200 | [diff] [blame] | 318 | } |
Christoph Hellwig | fdf5bdd | 2020-07-17 08:23:29 +0200 | [diff] [blame] | 319 | |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 320 | static int ipv6_set_opt_hdr(struct sock *sk, int optname, sockptr_t optval, |
Christoph Hellwig | b84d2b7 | 2020-07-23 08:09:02 +0200 | [diff] [blame] | 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 */ |
| 330 | if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW)) |
| 331 | return -EPERM; |
| 332 | |
| 333 | /* remove any sticky options header with a zero option |
| 334 | * length, per RFC3542. |
| 335 | */ |
| 336 | if (optlen > 0) { |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 337 | if (sockptr_is_null(optval)) |
Christoph Hellwig | b84d2b7 | 2020-07-23 08:09:02 +0200 | [diff] [blame] | 338 | return -EINVAL; |
| 339 | if (optlen < sizeof(struct ipv6_opt_hdr) || |
| 340 | optlen & 0x7 || |
| 341 | optlen > 8 * 255) |
| 342 | return -EINVAL; |
| 343 | |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 344 | new = memdup_sockptr(optval, optlen); |
Christoph Hellwig | b84d2b7 | 2020-07-23 08:09:02 +0200 | [diff] [blame] | 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 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 394 | static int do_ipv6_setsockopt(struct sock *sk, int level, int optname, |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 395 | sockptr_t optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | { |
| 397 | struct ipv6_pinfo *np = inet6_sk(sk); |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 398 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | int val, valbool; |
| 400 | int retv = -ENOPROTOOPT; |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 401 | bool needs_rtnl = setsockopt_needs_rtnl(optname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 403 | if (sockptr_is_null(optval)) |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 404 | val = 0; |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 405 | else { |
| 406 | if (optlen >= sizeof(int)) { |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 407 | if (copy_from_sockptr(&val, optval, sizeof(val))) |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 408 | return -EFAULT; |
| 409 | } else |
| 410 | val = 0; |
| 411 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 413 | valbool = (val != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 415 | if (ip6_mroute_opt(optname)) |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 416 | return ip6_mroute_setsockopt(sk, optname, optval, optlen); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 417 | |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 418 | if (needs_rtnl) |
| 419 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | lock_sock(sk); |
| 421 | |
| 422 | switch (optname) { |
| 423 | |
| 424 | case IPV6_ADDRFORM: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 425 | if (optlen < sizeof(int)) |
| 426 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | if (val == PF_INET) { |
| 428 | struct ipv6_txoptions *opt; |
| 429 | struct sk_buff *pktopt; |
| 430 | |
Denis V. Lunev | 49d074f | 2008-06-04 15:49:06 +0400 | [diff] [blame] | 431 | if (sk->sk_type == SOCK_RAW) |
| 432 | break; |
| 433 | |
Denis V. Lunev | 9596cc82 | 2008-06-04 15:49:08 +0400 | [diff] [blame] | 434 | if (sk->sk_protocol == IPPROTO_UDP || |
| 435 | sk->sk_protocol == IPPROTO_UDPLITE) { |
| 436 | struct udp_sock *up = udp_sk(sk); |
| 437 | if (up->pending == AF_INET6) { |
| 438 | retv = -EBUSY; |
| 439 | break; |
| 440 | } |
Hangbin Liu | 79a1f0c | 2020-06-01 11:55:03 +0800 | [diff] [blame] | 441 | } else if (sk->sk_protocol == IPPROTO_TCP) { |
| 442 | if (sk->sk_prot != &tcpv6_prot) { |
| 443 | retv = -EBUSY; |
| 444 | break; |
| 445 | } |
| 446 | } else { |
Eric Dumazet | b6f6118 | 2020-02-25 11:52:29 -0800 | [diff] [blame] | 447 | break; |
| 448 | } |
Hangbin Liu | 79a1f0c | 2020-06-01 11:55:03 +0800 | [diff] [blame] | 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 451 | retv = -ENOTCONN; |
| 452 | break; |
| 453 | } |
| 454 | |
| 455 | if (ipv6_only_sock(sk) || |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 456 | !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | retv = -EADDRNOTAVAIL; |
| 458 | break; |
| 459 | } |
| 460 | |
| 461 | fl6_free_socklist(sk); |
WANG Cong | 8651be8 | 2016-10-19 23:35:12 -0700 | [diff] [blame] | 462 | __ipv6_sock_mc_close(sk); |
Cong Wang | 8c0de6e | 2020-07-25 15:40:53 -0700 | [diff] [blame] | 463 | __ipv6_sock_ac_close(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Arnaldo Carvalho de Melo | e684897 | 2005-08-09 19:45:38 -0700 | [diff] [blame] | 465 | /* |
| 466 | * Sock is moving from IPv6 to IPv4 (sk_prot), so |
| 467 | * remove it from the refcnt debug socks count in the |
| 468 | * original family... |
| 469 | */ |
| 470 | sk_refcnt_debug_dec(sk); |
| 471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | if (sk->sk_protocol == IPPROTO_TCP) { |
Arnaldo Carvalho de Melo | d83d846 | 2005-12-13 23:26:10 -0800 | [diff] [blame] | 473 | struct inet_connection_sock *icsk = inet_csk(sk); |
Eric Dumazet | b3cb764 | 2021-11-15 09:11:50 -0800 | [diff] [blame] | 474 | |
Pavel Emelyanov | c29a0bc | 2008-03-31 19:41:46 -0700 | [diff] [blame] | 475 | sock_prot_inuse_add(net, sk->sk_prot, -1); |
| 476 | sock_prot_inuse_add(net, &tcp_prot, 1); |
Eric Dumazet | b3cb764 | 2021-11-15 09:11:50 -0800 | [diff] [blame] | 477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | sk->sk_prot = &tcp_prot; |
Arnaldo Carvalho de Melo | d83d846 | 2005-12-13 23:26:10 -0800 | [diff] [blame] | 479 | icsk->icsk_af_ops = &ipv4_specific; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | sk->sk_socket->ops = &inet_stream_ops; |
| 481 | sk->sk_family = PF_INET; |
Arnaldo Carvalho de Melo | d83d846 | 2005-12-13 23:26:10 -0800 | [diff] [blame] | 482 | tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | } else { |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 484 | struct proto *prot = &udp_prot; |
| 485 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 486 | if (sk->sk_protocol == IPPROTO_UDPLITE) |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 487 | prot = &udplite_prot; |
Eric Dumazet | b3cb764 | 2021-11-15 09:11:50 -0800 | [diff] [blame] | 488 | |
Pavel Emelyanov | c29a0bc | 2008-03-31 19:41:46 -0700 | [diff] [blame] | 489 | sock_prot_inuse_add(net, sk->sk_prot, -1); |
| 490 | sock_prot_inuse_add(net, prot, 1); |
Eric Dumazet | b3cb764 | 2021-11-15 09:11:50 -0800 | [diff] [blame] | 491 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 492 | sk->sk_prot = prot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | sk->sk_socket->ops = &inet_dgram_ops; |
| 494 | sk->sk_family = PF_INET; |
| 495 | } |
Eric Dumazet | 45f6fad | 2015-11-29 19:37:57 -0800 | [diff] [blame] | 496 | opt = xchg((__force struct ipv6_txoptions **)&np->opt, |
| 497 | NULL); |
| 498 | if (opt) { |
| 499 | atomic_sub(opt->tot_len, &sk->sk_omem_alloc); |
| 500 | txopt_put(opt); |
| 501 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | pktopt = xchg(&np->pktoptions, NULL); |
Wei Yongjun | 800d55f | 2009-02-23 21:45:33 +0000 | [diff] [blame] | 503 | kfree_skb(pktopt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
Arnaldo Carvalho de Melo | e684897 | 2005-08-09 19:45:38 -0700 | [diff] [blame] | 505 | /* |
| 506 | * ... and add it to the refcnt debug socks count |
| 507 | * in the new family. -acme |
| 508 | */ |
| 509 | sk_refcnt_debug_inc(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | module_put(THIS_MODULE); |
| 511 | retv = 0; |
| 512 | break; |
| 513 | } |
| 514 | goto e_inval; |
| 515 | |
| 516 | case IPV6_V6ONLY: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 517 | if (optlen < sizeof(int) || |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 518 | inet_sk(sk)->inet_num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | goto e_inval; |
Eric Dumazet | 9fe516b | 2014-06-27 08:36:16 -0700 | [diff] [blame] | 520 | sk->sk_ipv6only = valbool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | retv = 0; |
| 522 | break; |
| 523 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 524 | case IPV6_RECVPKTINFO: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 525 | if (optlen < sizeof(int)) |
| 526 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | np->rxopt.bits.rxinfo = valbool; |
| 528 | retv = 0; |
| 529 | break; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 530 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 531 | case IPV6_2292PKTINFO: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 532 | if (optlen < sizeof(int)) |
| 533 | goto e_inval; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 534 | np->rxopt.bits.rxoinfo = valbool; |
| 535 | retv = 0; |
| 536 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 538 | case IPV6_RECVHOPLIMIT: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 539 | if (optlen < sizeof(int)) |
| 540 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | np->rxopt.bits.rxhlim = valbool; |
| 542 | retv = 0; |
| 543 | break; |
| 544 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 545 | case IPV6_2292HOPLIMIT: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 546 | if (optlen < sizeof(int)) |
| 547 | goto e_inval; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 548 | np->rxopt.bits.rxohlim = valbool; |
| 549 | retv = 0; |
| 550 | break; |
| 551 | |
| 552 | case IPV6_RECVRTHDR: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 553 | if (optlen < sizeof(int)) |
| 554 | goto e_inval; |
YOSHIFUJI Hideaki | 4c75209 | 2007-05-23 13:28:48 +0900 | [diff] [blame] | 555 | np->rxopt.bits.srcrt = valbool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | retv = 0; |
| 557 | break; |
| 558 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 559 | case IPV6_2292RTHDR: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 560 | if (optlen < sizeof(int)) |
| 561 | goto e_inval; |
YOSHIFUJI Hideaki | 4c75209 | 2007-05-23 13:28:48 +0900 | [diff] [blame] | 562 | np->rxopt.bits.osrcrt = valbool; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 563 | retv = 0; |
| 564 | break; |
| 565 | |
| 566 | case IPV6_RECVHOPOPTS: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 567 | if (optlen < sizeof(int)) |
| 568 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | np->rxopt.bits.hopopts = valbool; |
| 570 | retv = 0; |
| 571 | break; |
| 572 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 573 | case IPV6_2292HOPOPTS: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 574 | if (optlen < sizeof(int)) |
| 575 | goto e_inval; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 576 | np->rxopt.bits.ohopopts = valbool; |
| 577 | retv = 0; |
| 578 | break; |
| 579 | |
| 580 | case IPV6_RECVDSTOPTS: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 581 | if (optlen < sizeof(int)) |
| 582 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | np->rxopt.bits.dstopts = valbool; |
| 584 | retv = 0; |
| 585 | break; |
| 586 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 587 | case IPV6_2292DSTOPTS: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 588 | if (optlen < sizeof(int)) |
| 589 | goto e_inval; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 590 | np->rxopt.bits.odstopts = valbool; |
| 591 | retv = 0; |
| 592 | break; |
| 593 | |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 594 | case IPV6_TCLASS: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 595 | if (optlen < sizeof(int)) |
| 596 | goto e_inval; |
Remi Denis-Courmont | d0ee011 | 2006-09-13 20:08:07 -0700 | [diff] [blame] | 597 | if (val < -1 || val > 0xff) |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 598 | goto e_inval; |
Gerrit Renker | 26ced1e | 2009-08-09 08:12:49 +0000 | [diff] [blame] | 599 | /* RFC 3542, 6.5: default traffic class of 0x0 */ |
| 600 | if (val == -1) |
| 601 | val = 0; |
Maciej Żenczykowski | 9f7b3a6 | 2021-11-23 14:31:54 -0800 | [diff] [blame] | 602 | if (sk->sk_type == SOCK_STREAM) { |
| 603 | val &= ~INET_ECN_MASK; |
| 604 | val |= np->tclass & INET_ECN_MASK; |
| 605 | } |
Maciej Żenczykowski | 305e95b | 2021-11-23 14:32:08 -0800 | [diff] [blame] | 606 | if (np->tclass != val) { |
| 607 | np->tclass = val; |
| 608 | sk_dst_reset(sk); |
| 609 | } |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 610 | retv = 0; |
| 611 | break; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 612 | |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 613 | case IPV6_RECVTCLASS: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 614 | if (optlen < sizeof(int)) |
| 615 | goto e_inval; |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 616 | np->rxopt.bits.rxtclass = valbool; |
| 617 | retv = 0; |
| 618 | break; |
| 619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | case IPV6_FLOWINFO: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 621 | if (optlen < sizeof(int)) |
| 622 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | np->rxopt.bits.rxflow = valbool; |
| 624 | retv = 0; |
| 625 | break; |
| 626 | |
Brian Haley | 793b1473 | 2010-04-23 11:26:07 +0000 | [diff] [blame] | 627 | case IPV6_RECVPATHMTU: |
| 628 | if (optlen < sizeof(int)) |
| 629 | goto e_inval; |
| 630 | np->rxopt.bits.rxpmtu = valbool; |
| 631 | retv = 0; |
| 632 | break; |
| 633 | |
Balazs Scheidler | 6c46862 | 2010-10-21 16:08:28 +0200 | [diff] [blame] | 634 | case IPV6_TRANSPARENT: |
Maciej Żenczykowski | 35fc59c | 2019-11-21 13:19:08 -0800 | [diff] [blame] | 635 | if (valbool && !ns_capable(net->user_ns, CAP_NET_RAW) && |
| 636 | !ns_capable(net->user_ns, CAP_NET_ADMIN)) { |
Balazs Scheidler | b889416 | 2010-10-23 04:48:14 +0000 | [diff] [blame] | 637 | retv = -EPERM; |
| 638 | break; |
| 639 | } |
Balazs Scheidler | 6c46862 | 2010-10-21 16:08:28 +0200 | [diff] [blame] | 640 | if (optlen < sizeof(int)) |
| 641 | goto e_inval; |
| 642 | /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */ |
| 643 | inet_sk(sk)->transparent = valbool; |
| 644 | retv = 0; |
| 645 | break; |
| 646 | |
Maciej Żenczykowski | 84e14fe | 2017-09-26 21:32:42 -0700 | [diff] [blame] | 647 | case IPV6_FREEBIND: |
| 648 | if (optlen < sizeof(int)) |
| 649 | goto e_inval; |
| 650 | /* we also don't have a separate freebind bit for IPV6 */ |
| 651 | inet_sk(sk)->freebind = valbool; |
| 652 | retv = 0; |
| 653 | break; |
| 654 | |
Balazs Scheidler | 6c46862 | 2010-10-21 16:08:28 +0200 | [diff] [blame] | 655 | case IPV6_RECVORIGDSTADDR: |
| 656 | if (optlen < sizeof(int)) |
| 657 | goto e_inval; |
| 658 | np->rxopt.bits.rxorigdstaddr = valbool; |
| 659 | retv = 0; |
| 660 | break; |
| 661 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 662 | case IPV6_HOPOPTS: |
| 663 | case IPV6_RTHDRDSTOPTS: |
| 664 | case IPV6_RTHDR: |
| 665 | case IPV6_DSTOPTS: |
Christoph Hellwig | b84d2b7 | 2020-07-23 08:09:02 +0200 | [diff] [blame] | 666 | retv = ipv6_set_opt_hdr(sk, optname, optval, optlen); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 667 | break; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 668 | |
Yang Hongyang | b24a251 | 2008-12-16 02:06:23 -0800 | [diff] [blame] | 669 | case IPV6_PKTINFO: |
| 670 | { |
| 671 | struct in6_pktinfo pkt; |
| 672 | |
| 673 | if (optlen == 0) |
| 674 | goto e_inval; |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 675 | else if (optlen < sizeof(struct in6_pktinfo) || |
| 676 | sockptr_is_null(optval)) |
Yang Hongyang | b24a251 | 2008-12-16 02:06:23 -0800 | [diff] [blame] | 677 | goto e_inval; |
| 678 | |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 679 | if (copy_from_sockptr(&pkt, optval, sizeof(pkt))) { |
| 680 | retv = -EFAULT; |
| 681 | break; |
Yang Hongyang | b24a251 | 2008-12-16 02:06:23 -0800 | [diff] [blame] | 682 | } |
Mike Manning | d839a0e | 2018-11-07 15:36:08 +0000 | [diff] [blame] | 683 | if (!sk_dev_equal_l3scope(sk, pkt.ipi6_ifindex)) |
Yang Hongyang | b24a251 | 2008-12-16 02:06:23 -0800 | [diff] [blame] | 684 | goto e_inval; |
| 685 | |
| 686 | np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 687 | np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr; |
Yang Hongyang | b24a251 | 2008-12-16 02:06:23 -0800 | [diff] [blame] | 688 | retv = 0; |
| 689 | break; |
| 690 | } |
| 691 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 692 | case IPV6_2292PKTOPTIONS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
| 694 | struct ipv6_txoptions *opt = NULL; |
| 695 | struct msghdr msg; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 696 | struct flowi6 fl6; |
Wei Wang | 26879da | 2016-05-02 21:40:07 -0700 | [diff] [blame] | 697 | struct ipcm6_cookie ipc6; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 699 | memset(&fl6, 0, sizeof(fl6)); |
| 700 | fl6.flowi6_oif = sk->sk_bound_dev_if; |
| 701 | fl6.flowi6_mark = sk->sk_mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | |
| 703 | if (optlen == 0) |
| 704 | goto update; |
| 705 | |
| 706 | /* 1K is probably excessive |
| 707 | * 1K is surely not enough, 2K per standard header is 16K. |
| 708 | */ |
| 709 | retv = -EINVAL; |
| 710 | if (optlen > 64*1024) |
| 711 | break; |
| 712 | |
| 713 | opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL); |
| 714 | retv = -ENOBUFS; |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 715 | if (!opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | break; |
| 717 | |
| 718 | memset(opt, 0, sizeof(*opt)); |
Reshetova, Elena | 0aeea21 | 2017-07-04 09:34:54 +0300 | [diff] [blame] | 719 | refcount_set(&opt->refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | opt->tot_len = sizeof(*opt) + optlen; |
| 721 | retv = -EFAULT; |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 722 | if (copy_from_sockptr(opt + 1, optval, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | goto done; |
| 724 | |
| 725 | msg.msg_controllen = optlen; |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 726 | msg.msg_control = (void *)(opt+1); |
Wei Wang | 26879da | 2016-05-02 21:40:07 -0700 | [diff] [blame] | 727 | ipc6.opt = opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
Willem de Bruijn | 5fdaa88 | 2018-07-06 10:12:57 -0400 | [diff] [blame] | 729 | retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | if (retv) |
| 731 | goto done; |
| 732 | update: |
| 733 | retv = 0; |
YOSHIFUJI Hideaki | e7712f1 | 2008-04-13 23:21:52 -0700 | [diff] [blame] | 734 | opt = ipv6_update_options(sk, opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | done: |
Eric Dumazet | 45f6fad | 2015-11-29 19:37:57 -0800 | [diff] [blame] | 736 | if (opt) { |
| 737 | atomic_sub(opt->tot_len, &sk->sk_omem_alloc); |
| 738 | txopt_put(opt); |
| 739 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | break; |
| 741 | } |
| 742 | case IPV6_UNICAST_HOPS: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 743 | if (optlen < sizeof(int)) |
| 744 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | if (val > 255 || val < -1) |
| 746 | goto e_inval; |
| 747 | np->hop_limit = val; |
| 748 | retv = 0; |
| 749 | break; |
| 750 | |
| 751 | case IPV6_MULTICAST_HOPS: |
| 752 | if (sk->sk_type == SOCK_STREAM) |
YOSHIFUJI Hideaki | 1717699 | 2008-06-12 03:27:26 +0900 | [diff] [blame] | 753 | break; |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 754 | if (optlen < sizeof(int)) |
| 755 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | if (val > 255 || val < -1) |
| 757 | goto e_inval; |
Li Wei | 2a38e6d | 2011-11-27 21:33:34 +0000 | [diff] [blame] | 758 | np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | retv = 0; |
| 760 | break; |
| 761 | |
| 762 | case IPV6_MULTICAST_LOOP: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 763 | if (optlen < sizeof(int)) |
| 764 | goto e_inval; |
YOSHIFUJI Hideaki | 28d4488 | 2008-06-12 03:14:51 +0900 | [diff] [blame] | 765 | if (val != valbool) |
| 766 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | np->mc_loop = valbool; |
| 768 | retv = 0; |
| 769 | break; |
| 770 | |
Erich E. Hoover | c4062df | 2012-02-08 09:11:08 +0000 | [diff] [blame] | 771 | case IPV6_UNICAST_IF: |
| 772 | { |
| 773 | struct net_device *dev = NULL; |
| 774 | int ifindex; |
| 775 | |
| 776 | if (optlen != sizeof(int)) |
| 777 | goto e_inval; |
| 778 | |
| 779 | ifindex = (__force int)ntohl((__force __be32)val); |
| 780 | if (ifindex == 0) { |
| 781 | np->ucast_oif = 0; |
| 782 | retv = 0; |
| 783 | break; |
| 784 | } |
| 785 | |
| 786 | dev = dev_get_by_index(net, ifindex); |
| 787 | retv = -EADDRNOTAVAIL; |
| 788 | if (!dev) |
| 789 | break; |
| 790 | dev_put(dev); |
| 791 | |
| 792 | retv = -EINVAL; |
| 793 | if (sk->sk_bound_dev_if) |
| 794 | break; |
| 795 | |
| 796 | np->ucast_oif = ifindex; |
| 797 | retv = 0; |
| 798 | break; |
| 799 | } |
| 800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | case IPV6_MULTICAST_IF: |
| 802 | if (sk->sk_type == SOCK_STREAM) |
YOSHIFUJI Hideaki | 1717699 | 2008-06-12 03:27:26 +0900 | [diff] [blame] | 803 | break; |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 804 | if (optlen < sizeof(int)) |
| 805 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | |
Brian Haley | 4953f0f | 2007-10-11 14:39:29 -0700 | [diff] [blame] | 807 | if (val) { |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 808 | struct net_device *dev; |
David Ahern | 7bb387c | 2016-12-29 15:39:37 -0800 | [diff] [blame] | 809 | int midx; |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 810 | |
David Ahern | 7bb387c | 2016-12-29 15:39:37 -0800 | [diff] [blame] | 811 | rcu_read_lock(); |
Brian Haley | 4953f0f | 2007-10-11 14:39:29 -0700 | [diff] [blame] | 812 | |
David Ahern | 7bb387c | 2016-12-29 15:39:37 -0800 | [diff] [blame] | 813 | dev = dev_get_by_index_rcu(net, val); |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 814 | if (!dev) { |
David Ahern | 7bb387c | 2016-12-29 15:39:37 -0800 | [diff] [blame] | 815 | rcu_read_unlock(); |
Brian Haley | 4953f0f | 2007-10-11 14:39:29 -0700 | [diff] [blame] | 816 | retv = -ENODEV; |
| 817 | break; |
| 818 | } |
David Ahern | 7bb387c | 2016-12-29 15:39:37 -0800 | [diff] [blame] | 819 | midx = l3mdev_master_ifindex_rcu(dev); |
| 820 | |
| 821 | rcu_read_unlock(); |
| 822 | |
| 823 | if (sk->sk_bound_dev_if && |
| 824 | sk->sk_bound_dev_if != val && |
| 825 | (!midx || midx != sk->sk_bound_dev_if)) |
| 826 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | } |
| 828 | np->mcast_oif = val; |
| 829 | retv = 0; |
| 830 | break; |
| 831 | case IPV6_ADD_MEMBERSHIP: |
| 832 | case IPV6_DROP_MEMBERSHIP: |
| 833 | { |
| 834 | struct ipv6_mreq mreq; |
| 835 | |
Wang Chen | a28398b | 2008-04-07 09:42:07 +0800 | [diff] [blame] | 836 | if (optlen < sizeof(struct ipv6_mreq)) |
| 837 | goto e_inval; |
| 838 | |
Flavio Leitner | a96fb49 | 2007-08-24 22:16:39 -0700 | [diff] [blame] | 839 | retv = -EPROTO; |
| 840 | if (inet_sk(sk)->is_icsk) |
| 841 | break; |
| 842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | retv = -EFAULT; |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 844 | if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | break; |
| 846 | |
| 847 | if (optname == IPV6_ADD_MEMBERSHIP) |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 848 | retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | else |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 850 | retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | break; |
| 852 | } |
| 853 | case IPV6_JOIN_ANYCAST: |
| 854 | case IPV6_LEAVE_ANYCAST: |
| 855 | { |
| 856 | struct ipv6_mreq mreq; |
| 857 | |
Wang Chen | a28398b | 2008-04-07 09:42:07 +0800 | [diff] [blame] | 858 | if (optlen < sizeof(struct ipv6_mreq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | goto e_inval; |
| 860 | |
| 861 | retv = -EFAULT; |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 862 | if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | break; |
| 864 | |
| 865 | if (optname == IPV6_JOIN_ANYCAST) |
| 866 | retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr); |
| 867 | else |
| 868 | retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr); |
| 869 | break; |
| 870 | } |
Andre Naujoks | 15033f0 | 2018-09-10 10:27:15 +0200 | [diff] [blame] | 871 | case IPV6_MULTICAST_ALL: |
| 872 | if (optlen < sizeof(int)) |
| 873 | goto e_inval; |
| 874 | np->mc_all = valbool; |
| 875 | retv = 0; |
| 876 | break; |
| 877 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | case MCAST_JOIN_GROUP: |
| 879 | case MCAST_LEAVE_GROUP: |
Christoph Hellwig | 3021ad5 | 2020-07-17 08:23:30 +0200 | [diff] [blame] | 880 | if (in_compat_syscall()) |
| 881 | retv = compat_ipv6_mcast_join_leave(sk, optname, optval, |
| 882 | optlen); |
| 883 | else |
| 884 | retv = ipv6_mcast_join_leave(sk, optname, optval, |
| 885 | optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | case MCAST_JOIN_SOURCE_GROUP: |
| 888 | case MCAST_LEAVE_SOURCE_GROUP: |
| 889 | case MCAST_BLOCK_SOURCE: |
| 890 | case MCAST_UNBLOCK_SOURCE: |
Christoph Hellwig | 3021ad5 | 2020-07-17 08:23:30 +0200 | [diff] [blame] | 891 | retv = do_ipv6_mcast_group_source(sk, optname, optval, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | case MCAST_MSFILTER: |
Christoph Hellwig | 3021ad5 | 2020-07-17 08:23:30 +0200 | [diff] [blame] | 894 | if (in_compat_syscall()) |
| 895 | retv = compat_ipv6_set_mcast_msfilter(sk, optval, |
| 896 | optlen); |
| 897 | else |
| 898 | retv = ipv6_set_mcast_msfilter(sk, optval, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | case IPV6_ROUTER_ALERT: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 901 | if (optlen < sizeof(int)) |
| 902 | goto e_inval; |
Denis V. Lunev | 725a8ff | 2008-07-19 00:28:58 -0700 | [diff] [blame] | 903 | retv = ip6_ra_control(sk, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | break; |
Francesco Ruggeri | 9036b2f | 2019-03-01 15:31:03 -0800 | [diff] [blame] | 905 | case IPV6_ROUTER_ALERT_ISOLATE: |
| 906 | if (optlen < sizeof(int)) |
| 907 | goto e_inval; |
| 908 | np->rtalert_isolate = valbool; |
| 909 | retv = 0; |
| 910 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | case IPV6_MTU_DISCOVER: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 912 | if (optlen < sizeof(int)) |
| 913 | goto e_inval; |
Hannes Frederic Sowa | 0b95227 | 2014-02-26 01:20:43 +0100 | [diff] [blame] | 914 | if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | goto e_inval; |
| 916 | np->pmtudisc = val; |
| 917 | retv = 0; |
| 918 | break; |
| 919 | case IPV6_MTU: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 920 | if (optlen < sizeof(int)) |
| 921 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | if (val && val < IPV6_MIN_MTU) |
| 923 | goto e_inval; |
| 924 | np->frag_size = val; |
| 925 | retv = 0; |
| 926 | break; |
| 927 | case IPV6_RECVERR: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 928 | if (optlen < sizeof(int)) |
| 929 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | np->recverr = valbool; |
| 931 | if (!val) |
| 932 | skb_queue_purge(&sk->sk_error_queue); |
| 933 | retv = 0; |
| 934 | break; |
| 935 | case IPV6_FLOWINFO_SEND: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 936 | if (optlen < sizeof(int)) |
| 937 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | np->sndflow = valbool; |
| 939 | retv = 0; |
| 940 | break; |
| 941 | case IPV6_FLOWLABEL_MGR: |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 942 | retv = ipv6_flowlabel_opt(sk, optval, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | break; |
| 944 | case IPV6_IPSEC_POLICY: |
| 945 | case IPV6_XFRM_POLICY: |
Herbert Xu | 6fc0b4a | 2005-08-06 06:33:15 -0700 | [diff] [blame] | 946 | retv = -EPERM; |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 947 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Herbert Xu | 6fc0b4a | 2005-08-06 06:33:15 -0700 | [diff] [blame] | 948 | break; |
Christoph Hellwig | 894cfbc | 2020-07-23 08:09:03 +0200 | [diff] [blame] | 949 | retv = xfrm_user_policy(sk, optname, optval, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | break; |
| 951 | |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 952 | case IPV6_ADDR_PREFERENCES: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 953 | if (optlen < sizeof(int)) |
| 954 | goto e_inval; |
Christoph Hellwig | 18d5ad6 | 2020-05-28 07:12:33 +0200 | [diff] [blame] | 955 | retv = __ip6_sock_set_addr_preferences(sk, val); |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 956 | break; |
Stephen Hemminger | e802af9 | 2010-04-22 15:24:53 -0700 | [diff] [blame] | 957 | case IPV6_MINHOPCOUNT: |
| 958 | if (optlen < sizeof(int)) |
| 959 | goto e_inval; |
| 960 | if (val < 0 || val > 255) |
| 961 | goto e_inval; |
Eric Dumazet | 790eb67 | 2021-10-25 09:48:22 -0700 | [diff] [blame] | 962 | |
| 963 | if (val) |
| 964 | static_branch_enable(&ip6_min_hopcount); |
| 965 | |
Eric Dumazet | cc17c3c | 2021-10-25 09:48:21 -0700 | [diff] [blame] | 966 | /* tcp_v6_err() and tcp_v6_rcv() might read min_hopcount |
| 967 | * while we are changing it. |
| 968 | */ |
| 969 | WRITE_ONCE(np->min_hopcount, val); |
Hannes Frederic Sowa | d4596ba | 2012-11-10 19:52:34 +0000 | [diff] [blame] | 970 | retv = 0; |
Brian Haley | 793b1473 | 2010-04-23 11:26:07 +0000 | [diff] [blame] | 971 | break; |
| 972 | case IPV6_DONTFRAG: |
| 973 | np->dontfrag = valbool; |
Stephen Hemminger | e802af9 | 2010-04-22 15:24:53 -0700 | [diff] [blame] | 974 | retv = 0; |
| 975 | break; |
Tom Herbert | cb1ce2e | 2014-07-01 21:33:10 -0700 | [diff] [blame] | 976 | case IPV6_AUTOFLOWLABEL: |
| 977 | np->autoflowlabel = valbool; |
Shaohua Li | 513674b | 2017-12-20 12:10:21 -0800 | [diff] [blame] | 978 | np->autoflowlabel_set = 1; |
Tom Herbert | cb1ce2e | 2014-07-01 21:33:10 -0700 | [diff] [blame] | 979 | retv = 0; |
| 980 | break; |
Willem de Bruijn | 0cc0aa61 | 2016-11-02 11:02:17 -0400 | [diff] [blame] | 981 | case IPV6_RECVFRAGSIZE: |
| 982 | np->rxopt.bits.recvfragsize = valbool; |
| 983 | retv = 0; |
| 984 | break; |
Willem de Bruijn | 0137043 | 2020-07-24 09:03:10 -0400 | [diff] [blame] | 985 | case IPV6_RECVERR_RFC4884: |
| 986 | if (optlen < sizeof(int)) |
| 987 | goto e_inval; |
| 988 | if (val < 0 || val > 1) |
| 989 | goto e_inval; |
| 990 | np->recverr_rfc4884 = valbool; |
| 991 | retv = 0; |
| 992 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | } |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | release_sock(sk); |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 996 | if (needs_rtnl) |
| 997 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | return retv; |
| 1000 | |
| 1001 | e_inval: |
| 1002 | release_sock(sk); |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 1003 | if (needs_rtnl) |
| 1004 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | return -EINVAL; |
| 1006 | } |
| 1007 | |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 1008 | int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, |
| 1009 | unsigned int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1010 | { |
| 1011 | int err; |
| 1012 | |
| 1013 | if (level == SOL_IP && sk->sk_type != SOCK_RAW) |
| 1014 | return udp_prot.setsockopt(sk, level, optname, optval, optlen); |
| 1015 | |
| 1016 | if (level != SOL_IPV6) |
| 1017 | return -ENOPROTOOPT; |
| 1018 | |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 1019 | err = do_ipv6_setsockopt(sk, level, optname, optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1020 | #ifdef CONFIG_NETFILTER |
| 1021 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 1022 | if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY && |
Paolo Abeni | 3f34cfa | 2018-01-30 19:01:40 +0100 | [diff] [blame] | 1023 | optname != IPV6_XFRM_POLICY) |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 1024 | err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1025 | #endif |
| 1026 | return err; |
| 1027 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 1028 | EXPORT_SYMBOL(ipv6_setsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1029 | |
David S. Miller | 28693079 | 2007-03-07 12:50:46 -0800 | [diff] [blame] | 1030 | static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt, |
YOSHIFUJI Hideaki | 4c6510a | 2007-03-18 17:35:57 -0700 | [diff] [blame] | 1031 | int optname, char __user *optval, int len) |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1032 | { |
David S. Miller | 28693079 | 2007-03-07 12:50:46 -0800 | [diff] [blame] | 1033 | struct ipv6_opt_hdr *hdr; |
| 1034 | |
YOSHIFUJI Hideaki | 4c6510a | 2007-03-18 17:35:57 -0700 | [diff] [blame] | 1035 | if (!opt) |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1036 | return 0; |
YOSHIFUJI Hideaki | 4c6510a | 2007-03-18 17:35:57 -0700 | [diff] [blame] | 1037 | |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1038 | switch (optname) { |
YOSHIFUJI Hideaki | 4c6510a | 2007-03-18 17:35:57 -0700 | [diff] [blame] | 1039 | case IPV6_HOPOPTS: |
| 1040 | hdr = opt->hopopt; |
| 1041 | break; |
| 1042 | case IPV6_RTHDRDSTOPTS: |
| 1043 | hdr = opt->dst0opt; |
| 1044 | break; |
| 1045 | case IPV6_RTHDR: |
| 1046 | hdr = (struct ipv6_opt_hdr *)opt->srcrt; |
| 1047 | break; |
| 1048 | case IPV6_DSTOPTS: |
| 1049 | hdr = opt->dst1opt; |
| 1050 | break; |
| 1051 | default: |
| 1052 | return -EINVAL; /* should not happen */ |
| 1053 | } |
| 1054 | |
| 1055 | if (!hdr) |
| 1056 | return 0; |
David S. Miller | 28693079 | 2007-03-07 12:50:46 -0800 | [diff] [blame] | 1057 | |
Chris Wright | d2b02ed | 2007-03-09 16:19:17 -0800 | [diff] [blame] | 1058 | len = min_t(unsigned int, len, ipv6_optlen(hdr)); |
Ilpo Järvinen | 660adc6 | 2007-08-15 15:07:30 -0700 | [diff] [blame] | 1059 | if (copy_to_user(optval, hdr, len)) |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1060 | return -EFAULT; |
Yang Hongyang | 95b496b | 2008-05-28 16:27:28 +0800 | [diff] [blame] | 1061 | return len; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1062 | } |
| 1063 | |
Christoph Hellwig | d5541e8 | 2020-07-17 08:23:27 +0200 | [diff] [blame] | 1064 | static int ipv6_get_msfilter(struct sock *sk, void __user *optval, |
| 1065 | int __user *optlen, int len) |
| 1066 | { |
Gustavo A. R. Silva | db243b7 | 2021-08-04 15:45:36 -0500 | [diff] [blame] | 1067 | const int size0 = offsetof(struct group_filter, gf_slist_flex); |
Christoph Hellwig | d5541e8 | 2020-07-17 08:23:27 +0200 | [diff] [blame] | 1068 | struct group_filter __user *p = optval; |
| 1069 | struct group_filter gsf; |
| 1070 | int num; |
| 1071 | int err; |
| 1072 | |
| 1073 | if (len < size0) |
| 1074 | return -EINVAL; |
| 1075 | if (copy_from_user(&gsf, p, size0)) |
| 1076 | return -EFAULT; |
| 1077 | if (gsf.gf_group.ss_family != AF_INET6) |
| 1078 | return -EADDRNOTAVAIL; |
| 1079 | num = gsf.gf_numsrc; |
| 1080 | lock_sock(sk); |
Gustavo A. R. Silva | db243b7 | 2021-08-04 15:45:36 -0500 | [diff] [blame] | 1081 | err = ip6_mc_msfget(sk, &gsf, p->gf_slist_flex); |
Christoph Hellwig | d5541e8 | 2020-07-17 08:23:27 +0200 | [diff] [blame] | 1082 | if (!err) { |
| 1083 | if (num > gsf.gf_numsrc) |
| 1084 | num = gsf.gf_numsrc; |
| 1085 | if (put_user(GROUP_FILTER_SIZE(num), optlen) || |
| 1086 | copy_to_user(p, &gsf, size0)) |
| 1087 | err = -EFAULT; |
| 1088 | } |
| 1089 | release_sock(sk); |
| 1090 | return err; |
| 1091 | } |
| 1092 | |
Christoph Hellwig | d5541e8 | 2020-07-17 08:23:27 +0200 | [diff] [blame] | 1093 | static int compat_ipv6_get_msfilter(struct sock *sk, void __user *optval, |
| 1094 | int __user *optlen) |
| 1095 | { |
Gustavo A. R. Silva | db243b7 | 2021-08-04 15:45:36 -0500 | [diff] [blame] | 1096 | const int size0 = offsetof(struct compat_group_filter, gf_slist_flex); |
Christoph Hellwig | d5541e8 | 2020-07-17 08:23:27 +0200 | [diff] [blame] | 1097 | struct compat_group_filter __user *p = optval; |
| 1098 | struct compat_group_filter gf32; |
| 1099 | struct group_filter gf; |
| 1100 | int len, err; |
| 1101 | int num; |
| 1102 | |
| 1103 | if (get_user(len, optlen)) |
| 1104 | return -EFAULT; |
| 1105 | if (len < size0) |
| 1106 | return -EINVAL; |
| 1107 | |
| 1108 | if (copy_from_user(&gf32, p, size0)) |
| 1109 | return -EFAULT; |
| 1110 | gf.gf_interface = gf32.gf_interface; |
| 1111 | gf.gf_fmode = gf32.gf_fmode; |
| 1112 | num = gf.gf_numsrc = gf32.gf_numsrc; |
| 1113 | gf.gf_group = gf32.gf_group; |
| 1114 | |
| 1115 | if (gf.gf_group.ss_family != AF_INET6) |
| 1116 | return -EADDRNOTAVAIL; |
| 1117 | |
| 1118 | lock_sock(sk); |
Gustavo A. R. Silva | db243b7 | 2021-08-04 15:45:36 -0500 | [diff] [blame] | 1119 | err = ip6_mc_msfget(sk, &gf, p->gf_slist_flex); |
Christoph Hellwig | d5541e8 | 2020-07-17 08:23:27 +0200 | [diff] [blame] | 1120 | release_sock(sk); |
| 1121 | if (err) |
| 1122 | return err; |
| 1123 | if (num > gf.gf_numsrc) |
| 1124 | num = gf.gf_numsrc; |
| 1125 | len = GROUP_FILTER_SIZE(num) - (sizeof(gf)-sizeof(gf32)); |
| 1126 | if (put_user(len, optlen) || |
| 1127 | put_user(gf.gf_fmode, &p->gf_fmode) || |
| 1128 | put_user(gf.gf_numsrc, &p->gf_numsrc)) |
| 1129 | return -EFAULT; |
| 1130 | return 0; |
| 1131 | } |
Christoph Hellwig | d5541e8 | 2020-07-17 08:23:27 +0200 | [diff] [blame] | 1132 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1133 | static int do_ipv6_getsockopt(struct sock *sk, int level, int optname, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1134 | char __user *optval, int __user *optlen, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | { |
| 1136 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 1137 | int len; |
| 1138 | int val; |
| 1139 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1140 | if (ip6_mroute_opt(optname)) |
| 1141 | return ip6_mroute_getsockopt(sk, optname, optval, optlen); |
| 1142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | if (get_user(len, optlen)) |
| 1144 | return -EFAULT; |
| 1145 | switch (optname) { |
| 1146 | case IPV6_ADDRFORM: |
| 1147 | if (sk->sk_protocol != IPPROTO_UDP && |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1148 | sk->sk_protocol != IPPROTO_UDPLITE && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | sk->sk_protocol != IPPROTO_TCP) |
YOSHIFUJI Hideaki | 1717699 | 2008-06-12 03:27:26 +0900 | [diff] [blame] | 1150 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | if (sk->sk_state != TCP_ESTABLISHED) |
| 1152 | return -ENOTCONN; |
| 1153 | val = sk->sk_family; |
| 1154 | break; |
| 1155 | case MCAST_MSFILTER: |
Christoph Hellwig | 3021ad5 | 2020-07-17 08:23:30 +0200 | [diff] [blame] | 1156 | if (in_compat_syscall()) |
| 1157 | return compat_ipv6_get_msfilter(sk, optval, optlen); |
Christoph Hellwig | d5541e8 | 2020-07-17 08:23:27 +0200 | [diff] [blame] | 1158 | return ipv6_get_msfilter(sk, optval, optlen, len); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1159 | case IPV6_2292PKTOPTIONS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | { |
| 1161 | struct msghdr msg; |
| 1162 | struct sk_buff *skb; |
| 1163 | |
| 1164 | if (sk->sk_type != SOCK_STREAM) |
| 1165 | return -ENOPROTOOPT; |
| 1166 | |
Lukas Bulwahn | 9e39394 | 2020-11-27 10:34:21 +0100 | [diff] [blame] | 1167 | msg.msg_control_user = optval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | msg.msg_controllen = len; |
Daniel Baluta | 98e7743 | 2011-08-19 03:19:07 -0700 | [diff] [blame] | 1169 | msg.msg_flags = flags; |
Christoph Hellwig | 1b2f08d | 2020-05-13 21:36:41 +0200 | [diff] [blame] | 1170 | msg.msg_control_is_user = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | |
| 1172 | lock_sock(sk); |
| 1173 | skb = np->pktoptions; |
| 1174 | if (skb) |
Hannes Frederic Sowa | 4b261c75 | 2014-01-20 03:43:08 +0100 | [diff] [blame] | 1175 | ip6_datagram_recv_ctl(sk, &msg, skb); |
Eric Dumazet | 1dc7b90 | 2015-01-21 03:45:42 -0800 | [diff] [blame] | 1176 | release_sock(sk); |
| 1177 | if (!skb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | if (np->rxopt.bits.rxinfo) { |
| 1179 | struct in6_pktinfo src_info; |
Yang Hongyang | f250dcd | 2008-12-16 02:07:45 -0800 | [diff] [blame] | 1180 | src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif : |
| 1181 | np->sticky_pktinfo.ipi6_ifindex; |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 1182 | src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info); |
| 1184 | } |
| 1185 | if (np->rxopt.bits.rxhlim) { |
| 1186 | int hlim = np->mcast_hops; |
| 1187 | put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim); |
| 1188 | } |
Jiri Benc | 4c507d2 | 2012-02-09 09:35:49 +0000 | [diff] [blame] | 1189 | if (np->rxopt.bits.rxtclass) { |
Li RongQing | d76ed22 | 2014-01-15 17:03:30 +0800 | [diff] [blame] | 1190 | int tclass = (int)ip6_tclass(np->rcv_flowinfo); |
| 1191 | |
Jiri Benc | 4c507d2 | 2012-02-09 09:35:49 +0000 | [diff] [blame] | 1192 | put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass); |
| 1193 | } |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1194 | if (np->rxopt.bits.rxoinfo) { |
| 1195 | struct in6_pktinfo src_info; |
Yang Hongyang | f250dcd | 2008-12-16 02:07:45 -0800 | [diff] [blame] | 1196 | src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif : |
| 1197 | np->sticky_pktinfo.ipi6_ifindex; |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 1198 | src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : |
| 1199 | np->sticky_pktinfo.ipi6_addr; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1200 | put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info); |
| 1201 | } |
| 1202 | if (np->rxopt.bits.rxohlim) { |
| 1203 | int hlim = np->mcast_hops; |
| 1204 | put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim); |
| 1205 | } |
Florent Fourcot | 1397ed3 | 2013-12-08 15:46:57 +0100 | [diff] [blame] | 1206 | if (np->rxopt.bits.rxflow) { |
Florent Fourcot | 6853605 | 2013-12-12 17:07:58 +0100 | [diff] [blame] | 1207 | __be32 flowinfo = np->rcv_flowinfo; |
| 1208 | |
Florent Fourcot | 1397ed3 | 2013-12-08 15:46:57 +0100 | [diff] [blame] | 1209 | put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo); |
| 1210 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | } |
| 1212 | len -= msg.msg_controllen; |
| 1213 | return put_user(len, optlen); |
| 1214 | } |
| 1215 | case IPV6_MTU: |
| 1216 | { |
| 1217 | struct dst_entry *dst; |
Eric Dumazet | b6c6712 | 2010-04-08 23:03:29 +0000 | [diff] [blame] | 1218 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1219 | val = 0; |
Eric Dumazet | b6c6712 | 2010-04-08 23:03:29 +0000 | [diff] [blame] | 1220 | rcu_read_lock(); |
| 1221 | dst = __sk_dst_get(sk); |
| 1222 | if (dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | val = dst_mtu(dst); |
Eric Dumazet | b6c6712 | 2010-04-08 23:03:29 +0000 | [diff] [blame] | 1224 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | if (!val) |
| 1226 | return -ENOTCONN; |
| 1227 | break; |
| 1228 | } |
| 1229 | |
| 1230 | case IPV6_V6ONLY: |
Eric Dumazet | 9fe516b | 2014-06-27 08:36:16 -0700 | [diff] [blame] | 1231 | val = sk->sk_ipv6only; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | break; |
| 1233 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1234 | case IPV6_RECVPKTINFO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | val = np->rxopt.bits.rxinfo; |
| 1236 | break; |
| 1237 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1238 | case IPV6_2292PKTINFO: |
| 1239 | val = np->rxopt.bits.rxoinfo; |
| 1240 | break; |
| 1241 | |
| 1242 | case IPV6_RECVHOPLIMIT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | val = np->rxopt.bits.rxhlim; |
| 1244 | break; |
| 1245 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1246 | case IPV6_2292HOPLIMIT: |
| 1247 | val = np->rxopt.bits.rxohlim; |
| 1248 | break; |
| 1249 | |
| 1250 | case IPV6_RECVRTHDR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | val = np->rxopt.bits.srcrt; |
| 1252 | break; |
| 1253 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1254 | case IPV6_2292RTHDR: |
| 1255 | val = np->rxopt.bits.osrcrt; |
| 1256 | break; |
| 1257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | case IPV6_HOPOPTS: |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1259 | case IPV6_RTHDRDSTOPTS: |
| 1260 | case IPV6_RTHDR: |
| 1261 | case IPV6_DSTOPTS: |
| 1262 | { |
Eric Dumazet | 45f6fad | 2015-11-29 19:37:57 -0800 | [diff] [blame] | 1263 | struct ipv6_txoptions *opt; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1264 | |
| 1265 | lock_sock(sk); |
Hannes Frederic Sowa | 1e1d04e | 2016-04-05 17:10:15 +0200 | [diff] [blame] | 1266 | opt = rcu_dereference_protected(np->opt, |
| 1267 | lockdep_sock_is_held(sk)); |
Eric Dumazet | 45f6fad | 2015-11-29 19:37:57 -0800 | [diff] [blame] | 1268 | len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1269 | release_sock(sk); |
Yang Hongyang | 05335c2 | 2008-05-28 16:23:47 +0800 | [diff] [blame] | 1270 | /* check if ipv6_getsockopt_sticky() returns err code */ |
| 1271 | if (len < 0) |
| 1272 | return len; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1273 | return put_user(len, optlen); |
| 1274 | } |
| 1275 | |
| 1276 | case IPV6_RECVHOPOPTS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | val = np->rxopt.bits.hopopts; |
| 1278 | break; |
| 1279 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1280 | case IPV6_2292HOPOPTS: |
| 1281 | val = np->rxopt.bits.ohopopts; |
| 1282 | break; |
| 1283 | |
| 1284 | case IPV6_RECVDSTOPTS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | val = np->rxopt.bits.dstopts; |
| 1286 | break; |
| 1287 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1288 | case IPV6_2292DSTOPTS: |
| 1289 | val = np->rxopt.bits.odstopts; |
| 1290 | break; |
| 1291 | |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 1292 | case IPV6_TCLASS: |
| 1293 | val = np->tclass; |
| 1294 | break; |
| 1295 | |
| 1296 | case IPV6_RECVTCLASS: |
| 1297 | val = np->rxopt.bits.rxtclass; |
| 1298 | break; |
| 1299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | case IPV6_FLOWINFO: |
| 1301 | val = np->rxopt.bits.rxflow; |
| 1302 | break; |
| 1303 | |
Brian Haley | 793b1473 | 2010-04-23 11:26:07 +0000 | [diff] [blame] | 1304 | case IPV6_RECVPATHMTU: |
| 1305 | val = np->rxopt.bits.rxpmtu; |
| 1306 | break; |
| 1307 | |
| 1308 | case IPV6_PATHMTU: |
| 1309 | { |
| 1310 | struct dst_entry *dst; |
| 1311 | struct ip6_mtuinfo mtuinfo; |
| 1312 | |
| 1313 | if (len < sizeof(mtuinfo)) |
| 1314 | return -EINVAL; |
| 1315 | |
| 1316 | len = sizeof(mtuinfo); |
| 1317 | memset(&mtuinfo, 0, sizeof(mtuinfo)); |
| 1318 | |
| 1319 | rcu_read_lock(); |
| 1320 | dst = __sk_dst_get(sk); |
| 1321 | if (dst) |
| 1322 | mtuinfo.ip6m_mtu = dst_mtu(dst); |
| 1323 | rcu_read_unlock(); |
| 1324 | if (!mtuinfo.ip6m_mtu) |
| 1325 | return -ENOTCONN; |
| 1326 | |
| 1327 | if (put_user(len, optlen)) |
| 1328 | return -EFAULT; |
| 1329 | if (copy_to_user(optval, &mtuinfo, len)) |
| 1330 | return -EFAULT; |
| 1331 | |
| 1332 | return 0; |
Brian Haley | 793b1473 | 2010-04-23 11:26:07 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Balazs Scheidler | 6c46862 | 2010-10-21 16:08:28 +0200 | [diff] [blame] | 1335 | case IPV6_TRANSPARENT: |
| 1336 | val = inet_sk(sk)->transparent; |
| 1337 | break; |
| 1338 | |
Maciej Żenczykowski | 84e14fe | 2017-09-26 21:32:42 -0700 | [diff] [blame] | 1339 | case IPV6_FREEBIND: |
| 1340 | val = inet_sk(sk)->freebind; |
| 1341 | break; |
| 1342 | |
Balazs Scheidler | 6c46862 | 2010-10-21 16:08:28 +0200 | [diff] [blame] | 1343 | case IPV6_RECVORIGDSTADDR: |
| 1344 | val = np->rxopt.bits.rxorigdstaddr; |
| 1345 | break; |
| 1346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | case IPV6_UNICAST_HOPS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | case IPV6_MULTICAST_HOPS: |
Brian Haley | befffe9 | 2006-12-12 17:09:49 -0800 | [diff] [blame] | 1349 | { |
| 1350 | struct dst_entry *dst; |
| 1351 | |
| 1352 | if (optname == IPV6_UNICAST_HOPS) |
| 1353 | val = np->hop_limit; |
| 1354 | else |
| 1355 | val = np->mcast_hops; |
| 1356 | |
Eric Dumazet | b6c6712 | 2010-04-08 23:03:29 +0000 | [diff] [blame] | 1357 | if (val < 0) { |
| 1358 | rcu_read_lock(); |
| 1359 | dst = __sk_dst_get(sk); |
| 1360 | if (dst) |
YOSHIFUJI Hideaki | 6b75d09 | 2008-03-10 06:00:30 -0400 | [diff] [blame] | 1361 | val = ip6_dst_hoplimit(dst); |
Eric Dumazet | b6c6712 | 2010-04-08 23:03:29 +0000 | [diff] [blame] | 1362 | rcu_read_unlock(); |
Brian Haley | befffe9 | 2006-12-12 17:09:49 -0800 | [diff] [blame] | 1363 | } |
Eric Dumazet | b6c6712 | 2010-04-08 23:03:29 +0000 | [diff] [blame] | 1364 | |
Brian Haley | befffe9 | 2006-12-12 17:09:49 -0800 | [diff] [blame] | 1365 | if (val < 0) |
YOSHIFUJI Hideaki | 53b7997 | 2008-07-19 22:35:03 -0700 | [diff] [blame] | 1366 | val = sock_net(sk)->ipv6.devconf_all->hop_limit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | break; |
Brian Haley | befffe9 | 2006-12-12 17:09:49 -0800 | [diff] [blame] | 1368 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | |
| 1370 | case IPV6_MULTICAST_LOOP: |
| 1371 | val = np->mc_loop; |
| 1372 | break; |
| 1373 | |
| 1374 | case IPV6_MULTICAST_IF: |
| 1375 | val = np->mcast_oif; |
| 1376 | break; |
| 1377 | |
Andre Naujoks | 15033f0 | 2018-09-10 10:27:15 +0200 | [diff] [blame] | 1378 | case IPV6_MULTICAST_ALL: |
| 1379 | val = np->mc_all; |
| 1380 | break; |
| 1381 | |
Erich E. Hoover | c4062df | 2012-02-08 09:11:08 +0000 | [diff] [blame] | 1382 | case IPV6_UNICAST_IF: |
| 1383 | val = (__force int)htonl((__u32) np->ucast_oif); |
| 1384 | break; |
| 1385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | case IPV6_MTU_DISCOVER: |
| 1387 | val = np->pmtudisc; |
| 1388 | break; |
| 1389 | |
| 1390 | case IPV6_RECVERR: |
| 1391 | val = np->recverr; |
| 1392 | break; |
| 1393 | |
| 1394 | case IPV6_FLOWINFO_SEND: |
| 1395 | val = np->sndflow; |
| 1396 | break; |
| 1397 | |
Florent Fourcot | 3fdfa5f | 2013-11-07 17:53:12 +0100 | [diff] [blame] | 1398 | case IPV6_FLOWLABEL_MGR: |
| 1399 | { |
| 1400 | struct in6_flowlabel_req freq; |
Florent Fourcot | 46e5f40 | 2014-01-17 17:15:04 +0100 | [diff] [blame] | 1401 | int flags; |
Florent Fourcot | 3fdfa5f | 2013-11-07 17:53:12 +0100 | [diff] [blame] | 1402 | |
| 1403 | if (len < sizeof(freq)) |
| 1404 | return -EINVAL; |
| 1405 | |
| 1406 | if (copy_from_user(&freq, optval, sizeof(freq))) |
| 1407 | return -EFAULT; |
| 1408 | |
| 1409 | if (freq.flr_action != IPV6_FL_A_GET) |
| 1410 | return -EINVAL; |
| 1411 | |
| 1412 | len = sizeof(freq); |
Florent Fourcot | 46e5f40 | 2014-01-17 17:15:04 +0100 | [diff] [blame] | 1413 | flags = freq.flr_flags; |
| 1414 | |
Florent Fourcot | 3fdfa5f | 2013-11-07 17:53:12 +0100 | [diff] [blame] | 1415 | memset(&freq, 0, sizeof(freq)); |
| 1416 | |
Florent Fourcot | 46e5f40 | 2014-01-17 17:15:04 +0100 | [diff] [blame] | 1417 | val = ipv6_flowlabel_opt_get(sk, &freq, flags); |
Florent Fourcot | 3fdfa5f | 2013-11-07 17:53:12 +0100 | [diff] [blame] | 1418 | if (val < 0) |
| 1419 | return val; |
| 1420 | |
| 1421 | if (put_user(len, optlen)) |
| 1422 | return -EFAULT; |
| 1423 | if (copy_to_user(optval, &freq, len)) |
| 1424 | return -EFAULT; |
| 1425 | |
| 1426 | return 0; |
| 1427 | } |
| 1428 | |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1429 | case IPV6_ADDR_PREFERENCES: |
| 1430 | val = 0; |
| 1431 | |
| 1432 | if (np->srcprefs & IPV6_PREFER_SRC_TMP) |
| 1433 | val |= IPV6_PREFER_SRC_TMP; |
| 1434 | else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC) |
| 1435 | val |= IPV6_PREFER_SRC_PUBLIC; |
| 1436 | else { |
| 1437 | /* XXX: should we return system default? */ |
| 1438 | val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT; |
| 1439 | } |
| 1440 | |
| 1441 | if (np->srcprefs & IPV6_PREFER_SRC_COA) |
| 1442 | val |= IPV6_PREFER_SRC_COA; |
| 1443 | else |
| 1444 | val |= IPV6_PREFER_SRC_HOME; |
| 1445 | break; |
| 1446 | |
Stephen Hemminger | e802af9 | 2010-04-22 15:24:53 -0700 | [diff] [blame] | 1447 | case IPV6_MINHOPCOUNT: |
| 1448 | val = np->min_hopcount; |
| 1449 | break; |
| 1450 | |
Brian Haley | 793b1473 | 2010-04-23 11:26:07 +0000 | [diff] [blame] | 1451 | case IPV6_DONTFRAG: |
| 1452 | val = np->dontfrag; |
| 1453 | break; |
| 1454 | |
Tom Herbert | cb1ce2e | 2014-07-01 21:33:10 -0700 | [diff] [blame] | 1455 | case IPV6_AUTOFLOWLABEL: |
Ben Hutchings | e9191ff | 2018-01-22 20:06:42 +0000 | [diff] [blame] | 1456 | val = ip6_autoflowlabel(sock_net(sk), np); |
Tom Herbert | cb1ce2e | 2014-07-01 21:33:10 -0700 | [diff] [blame] | 1457 | break; |
| 1458 | |
Willem de Bruijn | 0cc0aa61 | 2016-11-02 11:02:17 -0400 | [diff] [blame] | 1459 | case IPV6_RECVFRAGSIZE: |
| 1460 | val = np->rxopt.bits.recvfragsize; |
| 1461 | break; |
| 1462 | |
Francesco Ruggeri | 9036b2f | 2019-03-01 15:31:03 -0800 | [diff] [blame] | 1463 | case IPV6_ROUTER_ALERT_ISOLATE: |
| 1464 | val = np->rtalert_isolate; |
| 1465 | break; |
| 1466 | |
Willem de Bruijn | 0137043 | 2020-07-24 09:03:10 -0400 | [diff] [blame] | 1467 | case IPV6_RECVERR_RFC4884: |
| 1468 | val = np->recverr_rfc4884; |
| 1469 | break; |
| 1470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | default: |
Wei Yongjun | cf6fc4a | 2007-12-16 13:39:57 -0800 | [diff] [blame] | 1472 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | } |
| 1474 | len = min_t(unsigned int, sizeof(int), len); |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1475 | if (put_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | return -EFAULT; |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1477 | if (copy_to_user(optval, &val, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | return -EFAULT; |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1482 | int ipv6_getsockopt(struct sock *sk, int level, int optname, |
| 1483 | char __user *optval, int __user *optlen) |
| 1484 | { |
| 1485 | int err; |
| 1486 | |
| 1487 | if (level == SOL_IP && sk->sk_type != SOCK_RAW) |
| 1488 | return udp_prot.getsockopt(sk, level, optname, optval, optlen); |
| 1489 | |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1490 | if (level != SOL_IPV6) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1491 | return -ENOPROTOOPT; |
| 1492 | |
Daniel Baluta | 98e7743 | 2011-08-19 03:19:07 -0700 | [diff] [blame] | 1493 | err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1494 | #ifdef CONFIG_NETFILTER |
Wei Yongjun | cf6fc4a | 2007-12-16 13:39:57 -0800 | [diff] [blame] | 1495 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 1496 | if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1497 | int len; |
| 1498 | |
| 1499 | if (get_user(len, optlen)) |
| 1500 | return -EFAULT; |
| 1501 | |
Paolo Abeni | 01ea306 | 2018-02-08 12:19:00 +0100 | [diff] [blame] | 1502 | err = nf_getsockopt(sk, PF_INET6, optname, optval, &len); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1503 | if (err >= 0) |
| 1504 | err = put_user(len, optlen); |
| 1505 | } |
| 1506 | #endif |
| 1507 | return err; |
| 1508 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 1509 | EXPORT_SYMBOL(ipv6_getsockopt); |