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