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