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