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