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