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