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