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: |
Maciej Żenczykowski | 6cc7a76 | 2011-10-20 18:21:36 -0400 | [diff] [blame] | 346 | if (valbool && !capable(CAP_NET_ADMIN) && !capable(CAP_NET_RAW)) { |
Balazs Scheidler | b889416 | 2010-10-23 04:48:14 +0000 | [diff] [blame] | 347 | retv = -EPERM; |
| 348 | break; |
| 349 | } |
Balazs Scheidler | 6c46862 | 2010-10-21 16:08:28 +0200 | [diff] [blame] | 350 | if (optlen < sizeof(int)) |
| 351 | goto e_inval; |
| 352 | /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */ |
| 353 | inet_sk(sk)->transparent = valbool; |
| 354 | retv = 0; |
| 355 | break; |
| 356 | |
| 357 | case IPV6_RECVORIGDSTADDR: |
| 358 | if (optlen < sizeof(int)) |
| 359 | goto e_inval; |
| 360 | np->rxopt.bits.rxorigdstaddr = valbool; |
| 361 | retv = 0; |
| 362 | break; |
| 363 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 364 | case IPV6_HOPOPTS: |
| 365 | case IPV6_RTHDRDSTOPTS: |
| 366 | case IPV6_RTHDR: |
| 367 | case IPV6_DSTOPTS: |
| 368 | { |
| 369 | struct ipv6_txoptions *opt; |
Shan Wei | aea7427 | 2008-06-19 16:29:39 -0700 | [diff] [blame] | 370 | |
| 371 | /* remove any sticky options header with a zero option |
| 372 | * length, per RFC3542. |
| 373 | */ |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 374 | if (optlen == 0) |
Luiz Capitulino | cb422c4 | 2005-11-14 21:43:36 -0800 | [diff] [blame] | 375 | optval = NULL; |
Yang Hongyang | cfb266c | 2008-08-03 18:16:15 -0700 | [diff] [blame] | 376 | else if (optval == NULL) |
| 377 | goto e_inval; |
Shan Wei | aea7427 | 2008-06-19 16:29:39 -0700 | [diff] [blame] | 378 | else if (optlen < sizeof(struct ipv6_opt_hdr) || |
| 379 | optlen & 0x7 || optlen > 8 * 255) |
| 380 | goto e_inval; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 381 | |
| 382 | /* hop-by-hop / destination options are privileged option */ |
| 383 | retv = -EPERM; |
| 384 | if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW)) |
| 385 | break; |
| 386 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 387 | opt = ipv6_renew_options(sk, np->opt, optname, |
| 388 | (struct ipv6_opt_hdr __user *)optval, |
| 389 | optlen); |
| 390 | if (IS_ERR(opt)) { |
| 391 | retv = PTR_ERR(opt); |
| 392 | break; |
| 393 | } |
| 394 | |
| 395 | /* routing header option needs extra check */ |
Brian Haley | 6e093d9 | 2008-11-12 22:59:21 -0800 | [diff] [blame] | 396 | retv = -EINVAL; |
Olaf Kirch | dfee0a7 | 2007-03-09 13:55:38 -0800 | [diff] [blame] | 397 | if (optname == IPV6_RTHDR && opt && opt->srcrt) { |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 398 | struct ipv6_rt_hdr *rthdr = opt->srcrt; |
Masahide NAKAMURA | 280a9d3 | 2006-08-23 19:17:12 -0700 | [diff] [blame] | 399 | switch (rthdr->type) { |
Masahide NAKAMURA | 59fbb3a6 | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 400 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
Masahide NAKAMURA | 280a9d3 | 2006-08-23 19:17:12 -0700 | [diff] [blame] | 401 | case IPV6_SRCRT_TYPE_2: |
Brian Haley | 6e093d9 | 2008-11-12 22:59:21 -0800 | [diff] [blame] | 402 | if (rthdr->hdrlen != 2 || |
| 403 | rthdr->segments_left != 1) |
| 404 | goto sticky_done; |
| 405 | |
Masahide NAKAMURA | 280a9d3 | 2006-08-23 19:17:12 -0700 | [diff] [blame] | 406 | break; |
YOSHIFUJI Hideaki | bb4dbf9 | 2007-07-10 22:55:49 -0700 | [diff] [blame] | 407 | #endif |
Masahide NAKAMURA | 280a9d3 | 2006-08-23 19:17:12 -0700 | [diff] [blame] | 408 | default: |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 409 | goto sticky_done; |
Masahide NAKAMURA | 280a9d3 | 2006-08-23 19:17:12 -0700 | [diff] [blame] | 410 | } |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | retv = 0; |
YOSHIFUJI Hideaki | e7712f1 | 2008-04-13 23:21:52 -0700 | [diff] [blame] | 414 | opt = ipv6_update_options(sk, opt); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 415 | sticky_done: |
| 416 | if (opt) |
| 417 | sock_kfree_s(sk, opt, opt->tot_len); |
| 418 | break; |
| 419 | } |
| 420 | |
Yang Hongyang | b24a251 | 2008-12-16 02:06:23 -0800 | [diff] [blame] | 421 | case IPV6_PKTINFO: |
| 422 | { |
| 423 | struct in6_pktinfo pkt; |
| 424 | |
| 425 | if (optlen == 0) |
| 426 | goto e_inval; |
| 427 | else if (optlen < sizeof(struct in6_pktinfo) || optval == NULL) |
| 428 | goto e_inval; |
| 429 | |
Ilpo Järvinen | 914d116 | 2009-01-04 17:27:31 -0800 | [diff] [blame] | 430 | if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) { |
Yang Hongyang | b24a251 | 2008-12-16 02:06:23 -0800 | [diff] [blame] | 431 | retv = -EFAULT; |
| 432 | break; |
| 433 | } |
| 434 | if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if) |
| 435 | goto e_inval; |
| 436 | |
| 437 | np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 438 | np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr; |
Yang Hongyang | b24a251 | 2008-12-16 02:06:23 -0800 | [diff] [blame] | 439 | retv = 0; |
| 440 | break; |
| 441 | } |
| 442 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 443 | case IPV6_2292PKTOPTIONS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
| 445 | struct ipv6_txoptions *opt = NULL; |
| 446 | struct msghdr msg; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 447 | struct flowi6 fl6; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | int junk; |
| 449 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 450 | memset(&fl6, 0, sizeof(fl6)); |
| 451 | fl6.flowi6_oif = sk->sk_bound_dev_if; |
| 452 | fl6.flowi6_mark = sk->sk_mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
| 454 | if (optlen == 0) |
| 455 | goto update; |
| 456 | |
| 457 | /* 1K is probably excessive |
| 458 | * 1K is surely not enough, 2K per standard header is 16K. |
| 459 | */ |
| 460 | retv = -EINVAL; |
| 461 | if (optlen > 64*1024) |
| 462 | break; |
| 463 | |
| 464 | opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL); |
| 465 | retv = -ENOBUFS; |
| 466 | if (opt == NULL) |
| 467 | break; |
| 468 | |
| 469 | memset(opt, 0, sizeof(*opt)); |
| 470 | opt->tot_len = sizeof(*opt) + optlen; |
| 471 | retv = -EFAULT; |
| 472 | if (copy_from_user(opt+1, optval, optlen)) |
| 473 | goto done; |
| 474 | |
| 475 | msg.msg_controllen = optlen; |
| 476 | msg.msg_control = (void*)(opt+1); |
| 477 | |
Maciej Żenczykowski | ec0506d | 2011-08-28 12:35:31 +0000 | [diff] [blame] | 478 | retv = datagram_send_ctl(net, sk, &msg, &fl6, opt, &junk, &junk, |
Brian Haley | 13b52cd | 2010-04-23 11:26:08 +0000 | [diff] [blame] | 479 | &junk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | if (retv) |
| 481 | goto done; |
| 482 | update: |
| 483 | retv = 0; |
YOSHIFUJI Hideaki | e7712f1 | 2008-04-13 23:21:52 -0700 | [diff] [blame] | 484 | opt = ipv6_update_options(sk, opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | done: |
| 486 | if (opt) |
| 487 | sock_kfree_s(sk, opt, opt->tot_len); |
| 488 | break; |
| 489 | } |
| 490 | case IPV6_UNICAST_HOPS: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 491 | if (optlen < sizeof(int)) |
| 492 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | if (val > 255 || val < -1) |
| 494 | goto e_inval; |
| 495 | np->hop_limit = val; |
| 496 | retv = 0; |
| 497 | break; |
| 498 | |
| 499 | case IPV6_MULTICAST_HOPS: |
| 500 | if (sk->sk_type == SOCK_STREAM) |
YOSHIFUJI Hideaki | 1717699 | 2008-06-12 03:27:26 +0900 | [diff] [blame] | 501 | break; |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 502 | if (optlen < sizeof(int)) |
| 503 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | if (val > 255 || val < -1) |
| 505 | goto e_inval; |
Li Wei | 2a38e6d | 2011-11-27 21:33:34 +0000 | [diff] [blame] | 506 | np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | retv = 0; |
| 508 | break; |
| 509 | |
| 510 | case IPV6_MULTICAST_LOOP: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 511 | if (optlen < sizeof(int)) |
| 512 | goto e_inval; |
YOSHIFUJI Hideaki | 28d4488 | 2008-06-12 03:14:51 +0900 | [diff] [blame] | 513 | if (val != valbool) |
| 514 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | np->mc_loop = valbool; |
| 516 | retv = 0; |
| 517 | break; |
| 518 | |
| 519 | case IPV6_MULTICAST_IF: |
| 520 | if (sk->sk_type == SOCK_STREAM) |
YOSHIFUJI Hideaki | 1717699 | 2008-06-12 03:27:26 +0900 | [diff] [blame] | 521 | break; |
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 | |
Brian Haley | 4953f0f | 2007-10-11 14:39:29 -0700 | [diff] [blame] | 525 | if (val) { |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 526 | struct net_device *dev; |
| 527 | |
Brian Haley | 4953f0f | 2007-10-11 14:39:29 -0700 | [diff] [blame] | 528 | if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val) |
| 529 | goto e_inval; |
| 530 | |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 531 | dev = dev_get_by_index(net, val); |
| 532 | if (!dev) { |
Brian Haley | 4953f0f | 2007-10-11 14:39:29 -0700 | [diff] [blame] | 533 | retv = -ENODEV; |
| 534 | break; |
| 535 | } |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 536 | dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
| 538 | np->mcast_oif = val; |
| 539 | retv = 0; |
| 540 | break; |
| 541 | case IPV6_ADD_MEMBERSHIP: |
| 542 | case IPV6_DROP_MEMBERSHIP: |
| 543 | { |
| 544 | struct ipv6_mreq mreq; |
| 545 | |
Wang Chen | a28398b | 2008-04-07 09:42:07 +0800 | [diff] [blame] | 546 | if (optlen < sizeof(struct ipv6_mreq)) |
| 547 | goto e_inval; |
| 548 | |
Flavio Leitner | a96fb49 | 2007-08-24 22:16:39 -0700 | [diff] [blame] | 549 | retv = -EPROTO; |
| 550 | if (inet_sk(sk)->is_icsk) |
| 551 | break; |
| 552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | retv = -EFAULT; |
| 554 | if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq))) |
| 555 | break; |
| 556 | |
| 557 | if (optname == IPV6_ADD_MEMBERSHIP) |
| 558 | retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr); |
| 559 | else |
| 560 | retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr); |
| 561 | break; |
| 562 | } |
| 563 | case IPV6_JOIN_ANYCAST: |
| 564 | case IPV6_LEAVE_ANYCAST: |
| 565 | { |
| 566 | struct ipv6_mreq mreq; |
| 567 | |
Wang Chen | a28398b | 2008-04-07 09:42:07 +0800 | [diff] [blame] | 568 | if (optlen < sizeof(struct ipv6_mreq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | goto e_inval; |
| 570 | |
| 571 | retv = -EFAULT; |
| 572 | if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq))) |
| 573 | break; |
| 574 | |
| 575 | if (optname == IPV6_JOIN_ANYCAST) |
| 576 | retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr); |
| 577 | else |
| 578 | retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr); |
| 579 | break; |
| 580 | } |
| 581 | case MCAST_JOIN_GROUP: |
| 582 | case MCAST_LEAVE_GROUP: |
| 583 | { |
| 584 | struct group_req greq; |
| 585 | struct sockaddr_in6 *psin6; |
| 586 | |
Wang Chen | a28398b | 2008-04-07 09:42:07 +0800 | [diff] [blame] | 587 | if (optlen < sizeof(struct group_req)) |
| 588 | goto e_inval; |
| 589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | retv = -EFAULT; |
| 591 | if (copy_from_user(&greq, optval, sizeof(struct group_req))) |
| 592 | break; |
| 593 | if (greq.gr_group.ss_family != AF_INET6) { |
| 594 | retv = -EADDRNOTAVAIL; |
| 595 | break; |
| 596 | } |
| 597 | psin6 = (struct sockaddr_in6 *)&greq.gr_group; |
| 598 | if (optname == MCAST_JOIN_GROUP) |
| 599 | retv = ipv6_sock_mc_join(sk, greq.gr_interface, |
| 600 | &psin6->sin6_addr); |
| 601 | else |
| 602 | retv = ipv6_sock_mc_drop(sk, greq.gr_interface, |
| 603 | &psin6->sin6_addr); |
| 604 | break; |
| 605 | } |
| 606 | case MCAST_JOIN_SOURCE_GROUP: |
| 607 | case MCAST_LEAVE_SOURCE_GROUP: |
| 608 | case MCAST_BLOCK_SOURCE: |
| 609 | case MCAST_UNBLOCK_SOURCE: |
| 610 | { |
| 611 | struct group_source_req greqs; |
| 612 | int omode, add; |
| 613 | |
Wang Chen | a28398b | 2008-04-07 09:42:07 +0800 | [diff] [blame] | 614 | if (optlen < sizeof(struct group_source_req)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | goto e_inval; |
| 616 | if (copy_from_user(&greqs, optval, sizeof(greqs))) { |
| 617 | retv = -EFAULT; |
| 618 | break; |
| 619 | } |
| 620 | if (greqs.gsr_group.ss_family != AF_INET6 || |
| 621 | greqs.gsr_source.ss_family != AF_INET6) { |
| 622 | retv = -EADDRNOTAVAIL; |
| 623 | break; |
| 624 | } |
| 625 | if (optname == MCAST_BLOCK_SOURCE) { |
| 626 | omode = MCAST_EXCLUDE; |
| 627 | add = 1; |
| 628 | } else if (optname == MCAST_UNBLOCK_SOURCE) { |
| 629 | omode = MCAST_EXCLUDE; |
| 630 | add = 0; |
| 631 | } else if (optname == MCAST_JOIN_SOURCE_GROUP) { |
| 632 | struct sockaddr_in6 *psin6; |
| 633 | |
| 634 | psin6 = (struct sockaddr_in6 *)&greqs.gsr_group; |
| 635 | retv = ipv6_sock_mc_join(sk, greqs.gsr_interface, |
| 636 | &psin6->sin6_addr); |
David L Stevens | c9e3e8b | 2005-06-21 13:58:25 -0700 | [diff] [blame] | 637 | /* prior join w/ different source is ok */ |
| 638 | if (retv && retv != -EADDRINUSE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | break; |
| 640 | omode = MCAST_INCLUDE; |
| 641 | add = 1; |
David L Stevens | c9e3e8b | 2005-06-21 13:58:25 -0700 | [diff] [blame] | 642 | } else /* MCAST_LEAVE_SOURCE_GROUP */ { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | omode = MCAST_INCLUDE; |
| 644 | add = 0; |
| 645 | } |
| 646 | retv = ip6_mc_source(add, omode, sk, &greqs); |
| 647 | break; |
| 648 | } |
| 649 | case MCAST_MSFILTER: |
| 650 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | extern int sysctl_mld_max_msf; |
| 652 | struct group_filter *gsf; |
| 653 | |
| 654 | if (optlen < GROUP_FILTER_SIZE(0)) |
| 655 | goto e_inval; |
| 656 | if (optlen > sysctl_optmem_max) { |
| 657 | retv = -ENOBUFS; |
| 658 | break; |
| 659 | } |
Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 660 | gsf = kmalloc(optlen,GFP_KERNEL); |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 661 | if (!gsf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | retv = -ENOBUFS; |
| 663 | break; |
| 664 | } |
| 665 | retv = -EFAULT; |
| 666 | if (copy_from_user(gsf, optval, optlen)) { |
| 667 | kfree(gsf); |
| 668 | break; |
| 669 | } |
| 670 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ |
| 671 | if (gsf->gf_numsrc >= 0x1ffffffU || |
| 672 | gsf->gf_numsrc > sysctl_mld_max_msf) { |
| 673 | kfree(gsf); |
| 674 | retv = -ENOBUFS; |
| 675 | break; |
| 676 | } |
| 677 | if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) { |
| 678 | kfree(gsf); |
| 679 | retv = -EINVAL; |
| 680 | break; |
| 681 | } |
| 682 | retv = ip6_mc_msfilter(sk, gsf); |
| 683 | kfree(gsf); |
| 684 | |
| 685 | break; |
| 686 | } |
| 687 | case IPV6_ROUTER_ALERT: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 688 | if (optlen < sizeof(int)) |
| 689 | goto e_inval; |
Denis V. Lunev | 725a8ff | 2008-07-19 00:28:58 -0700 | [diff] [blame] | 690 | retv = ip6_ra_control(sk, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | break; |
| 692 | case IPV6_MTU_DISCOVER: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 693 | if (optlen < sizeof(int)) |
| 694 | goto e_inval; |
John Dykstra | 0eae750 | 2009-10-19 21:53:53 -0700 | [diff] [blame] | 695 | if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | goto e_inval; |
| 697 | np->pmtudisc = val; |
| 698 | retv = 0; |
| 699 | break; |
| 700 | case IPV6_MTU: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 701 | if (optlen < sizeof(int)) |
| 702 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | if (val && val < IPV6_MIN_MTU) |
| 704 | goto e_inval; |
| 705 | np->frag_size = val; |
| 706 | retv = 0; |
| 707 | break; |
| 708 | case IPV6_RECVERR: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 709 | if (optlen < sizeof(int)) |
| 710 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | np->recverr = valbool; |
| 712 | if (!val) |
| 713 | skb_queue_purge(&sk->sk_error_queue); |
| 714 | retv = 0; |
| 715 | break; |
| 716 | case IPV6_FLOWINFO_SEND: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 717 | if (optlen < sizeof(int)) |
| 718 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | np->sndflow = valbool; |
| 720 | retv = 0; |
| 721 | break; |
| 722 | case IPV6_FLOWLABEL_MGR: |
| 723 | retv = ipv6_flowlabel_opt(sk, optval, optlen); |
| 724 | break; |
| 725 | case IPV6_IPSEC_POLICY: |
| 726 | case IPV6_XFRM_POLICY: |
Herbert Xu | 6fc0b4a | 2005-08-06 06:33:15 -0700 | [diff] [blame] | 727 | retv = -EPERM; |
| 728 | if (!capable(CAP_NET_ADMIN)) |
| 729 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | retv = xfrm_user_policy(sk, optname, optval, optlen); |
| 731 | break; |
| 732 | |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 733 | case IPV6_ADDR_PREFERENCES: |
| 734 | { |
| 735 | unsigned int pref = 0; |
| 736 | unsigned int prefmask = ~0; |
| 737 | |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 738 | if (optlen < sizeof(int)) |
| 739 | goto e_inval; |
| 740 | |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 741 | retv = -EINVAL; |
| 742 | |
| 743 | /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */ |
| 744 | switch (val & (IPV6_PREFER_SRC_PUBLIC| |
| 745 | IPV6_PREFER_SRC_TMP| |
| 746 | IPV6_PREFER_SRC_PUBTMP_DEFAULT)) { |
| 747 | case IPV6_PREFER_SRC_PUBLIC: |
| 748 | pref |= IPV6_PREFER_SRC_PUBLIC; |
| 749 | break; |
| 750 | case IPV6_PREFER_SRC_TMP: |
| 751 | pref |= IPV6_PREFER_SRC_TMP; |
| 752 | break; |
| 753 | case IPV6_PREFER_SRC_PUBTMP_DEFAULT: |
| 754 | break; |
| 755 | case 0: |
| 756 | goto pref_skip_pubtmp; |
| 757 | default: |
| 758 | goto e_inval; |
| 759 | } |
| 760 | |
| 761 | prefmask &= ~(IPV6_PREFER_SRC_PUBLIC| |
| 762 | IPV6_PREFER_SRC_TMP); |
| 763 | pref_skip_pubtmp: |
| 764 | |
| 765 | /* check HOME/COA conflicts */ |
| 766 | switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) { |
| 767 | case IPV6_PREFER_SRC_HOME: |
| 768 | break; |
| 769 | case IPV6_PREFER_SRC_COA: |
| 770 | pref |= IPV6_PREFER_SRC_COA; |
| 771 | case 0: |
| 772 | goto pref_skip_coa; |
| 773 | default: |
| 774 | goto e_inval; |
| 775 | } |
| 776 | |
| 777 | prefmask &= ~IPV6_PREFER_SRC_COA; |
| 778 | pref_skip_coa: |
| 779 | |
| 780 | /* check CGA/NONCGA conflicts */ |
| 781 | switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) { |
| 782 | case IPV6_PREFER_SRC_CGA: |
| 783 | case IPV6_PREFER_SRC_NONCGA: |
| 784 | case 0: |
| 785 | break; |
| 786 | default: |
| 787 | goto e_inval; |
| 788 | } |
| 789 | |
| 790 | np->srcprefs = (np->srcprefs & prefmask) | pref; |
| 791 | retv = 0; |
| 792 | |
| 793 | break; |
| 794 | } |
Stephen Hemminger | e802af9 | 2010-04-22 15:24:53 -0700 | [diff] [blame] | 795 | case IPV6_MINHOPCOUNT: |
| 796 | if (optlen < sizeof(int)) |
| 797 | goto e_inval; |
| 798 | if (val < 0 || val > 255) |
| 799 | goto e_inval; |
| 800 | np->min_hopcount = val; |
Brian Haley | 793b147 | 2010-04-23 11:26:07 +0000 | [diff] [blame] | 801 | break; |
| 802 | case IPV6_DONTFRAG: |
| 803 | np->dontfrag = valbool; |
Stephen Hemminger | e802af9 | 2010-04-22 15:24:53 -0700 | [diff] [blame] | 804 | retv = 0; |
| 805 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | } |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | release_sock(sk); |
| 809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | return retv; |
| 811 | |
| 812 | e_inval: |
| 813 | release_sock(sk); |
| 814 | return -EINVAL; |
| 815 | } |
| 816 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 817 | int ipv6_setsockopt(struct sock *sk, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 818 | char __user *optval, unsigned int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 819 | { |
| 820 | int err; |
| 821 | |
| 822 | if (level == SOL_IP && sk->sk_type != SOCK_RAW) |
| 823 | return udp_prot.setsockopt(sk, level, optname, optval, optlen); |
| 824 | |
| 825 | if (level != SOL_IPV6) |
| 826 | return -ENOPROTOOPT; |
| 827 | |
| 828 | err = do_ipv6_setsockopt(sk, level, optname, optval, optlen); |
| 829 | #ifdef CONFIG_NETFILTER |
| 830 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 831 | if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY && |
| 832 | optname != IPV6_XFRM_POLICY) { |
| 833 | lock_sock(sk); |
| 834 | err = nf_setsockopt(sk, PF_INET6, optname, optval, |
| 835 | optlen); |
| 836 | release_sock(sk); |
| 837 | } |
| 838 | #endif |
| 839 | return err; |
| 840 | } |
| 841 | |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 842 | EXPORT_SYMBOL(ipv6_setsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 843 | |
| 844 | #ifdef CONFIG_COMPAT |
| 845 | int compat_ipv6_setsockopt(struct sock *sk, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 846 | char __user *optval, unsigned int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 847 | { |
| 848 | int err; |
| 849 | |
| 850 | if (level == SOL_IP && sk->sk_type != SOCK_RAW) { |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 851 | if (udp_prot.compat_setsockopt != NULL) |
| 852 | return udp_prot.compat_setsockopt(sk, level, optname, |
| 853 | optval, optlen); |
| 854 | return udp_prot.setsockopt(sk, level, optname, optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | if (level != SOL_IPV6) |
| 858 | return -ENOPROTOOPT; |
| 859 | |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 860 | if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER) |
| 861 | return compat_mc_setsockopt(sk, level, optname, optval, optlen, |
| 862 | ipv6_setsockopt); |
| 863 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 864 | err = do_ipv6_setsockopt(sk, level, optname, optval, optlen); |
| 865 | #ifdef CONFIG_NETFILTER |
| 866 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 867 | if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY && |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 868 | optname != IPV6_XFRM_POLICY) { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 869 | lock_sock(sk); |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 870 | err = compat_nf_setsockopt(sk, PF_INET6, optname, |
| 871 | optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 872 | release_sock(sk); |
| 873 | } |
| 874 | #endif |
| 875 | return err; |
| 876 | } |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 877 | |
| 878 | EXPORT_SYMBOL(compat_ipv6_setsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 879 | #endif |
| 880 | |
David S. Miller | 28693079 | 2007-03-07 12:50:46 -0800 | [diff] [blame] | 881 | static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt, |
YOSHIFUJI Hideaki | 4c6510a | 2007-03-18 17:35:57 -0700 | [diff] [blame] | 882 | int optname, char __user *optval, int len) |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 883 | { |
David S. Miller | 28693079 | 2007-03-07 12:50:46 -0800 | [diff] [blame] | 884 | struct ipv6_opt_hdr *hdr; |
| 885 | |
YOSHIFUJI Hideaki | 4c6510a | 2007-03-18 17:35:57 -0700 | [diff] [blame] | 886 | if (!opt) |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 887 | return 0; |
YOSHIFUJI Hideaki | 4c6510a | 2007-03-18 17:35:57 -0700 | [diff] [blame] | 888 | |
| 889 | switch(optname) { |
| 890 | case IPV6_HOPOPTS: |
| 891 | hdr = opt->hopopt; |
| 892 | break; |
| 893 | case IPV6_RTHDRDSTOPTS: |
| 894 | hdr = opt->dst0opt; |
| 895 | break; |
| 896 | case IPV6_RTHDR: |
| 897 | hdr = (struct ipv6_opt_hdr *)opt->srcrt; |
| 898 | break; |
| 899 | case IPV6_DSTOPTS: |
| 900 | hdr = opt->dst1opt; |
| 901 | break; |
| 902 | default: |
| 903 | return -EINVAL; /* should not happen */ |
| 904 | } |
| 905 | |
| 906 | if (!hdr) |
| 907 | return 0; |
David S. Miller | 28693079 | 2007-03-07 12:50:46 -0800 | [diff] [blame] | 908 | |
Chris Wright | d2b02ed | 2007-03-09 16:19:17 -0800 | [diff] [blame] | 909 | len = min_t(unsigned int, len, ipv6_optlen(hdr)); |
Ilpo Järvinen | 660adc6 | 2007-08-15 15:07:30 -0700 | [diff] [blame] | 910 | if (copy_to_user(optval, hdr, len)) |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 911 | return -EFAULT; |
Yang Hongyang | 95b496b | 2008-05-28 16:27:28 +0800 | [diff] [blame] | 912 | return len; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 913 | } |
| 914 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 915 | static int do_ipv6_getsockopt(struct sock *sk, int level, int optname, |
Daniel Baluta | 98e7743 | 2011-08-19 03:19:07 -0700 | [diff] [blame] | 916 | char __user *optval, int __user *optlen, unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | { |
| 918 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 919 | int len; |
| 920 | int val; |
| 921 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 922 | if (ip6_mroute_opt(optname)) |
| 923 | return ip6_mroute_getsockopt(sk, optname, optval, optlen); |
| 924 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | if (get_user(len, optlen)) |
| 926 | return -EFAULT; |
| 927 | switch (optname) { |
| 928 | case IPV6_ADDRFORM: |
| 929 | if (sk->sk_protocol != IPPROTO_UDP && |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 930 | sk->sk_protocol != IPPROTO_UDPLITE && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | sk->sk_protocol != IPPROTO_TCP) |
YOSHIFUJI Hideaki | 1717699 | 2008-06-12 03:27:26 +0900 | [diff] [blame] | 932 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | if (sk->sk_state != TCP_ESTABLISHED) |
| 934 | return -ENOTCONN; |
| 935 | val = sk->sk_family; |
| 936 | break; |
| 937 | case MCAST_MSFILTER: |
| 938 | { |
| 939 | struct group_filter gsf; |
| 940 | int err; |
| 941 | |
| 942 | if (len < GROUP_FILTER_SIZE(0)) |
| 943 | return -EINVAL; |
| 944 | if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) |
| 945 | return -EFAULT; |
YOSHIFUJI Hideaki | 20c61fb | 2008-04-28 14:40:55 +0900 | [diff] [blame] | 946 | if (gsf.gf_group.ss_family != AF_INET6) |
| 947 | return -EADDRNOTAVAIL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | lock_sock(sk); |
| 949 | err = ip6_mc_msfget(sk, &gsf, |
| 950 | (struct group_filter __user *)optval, optlen); |
| 951 | release_sock(sk); |
| 952 | return err; |
| 953 | } |
| 954 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 955 | case IPV6_2292PKTOPTIONS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | { |
| 957 | struct msghdr msg; |
| 958 | struct sk_buff *skb; |
| 959 | |
| 960 | if (sk->sk_type != SOCK_STREAM) |
| 961 | return -ENOPROTOOPT; |
| 962 | |
| 963 | msg.msg_control = optval; |
| 964 | msg.msg_controllen = len; |
Daniel Baluta | 98e7743 | 2011-08-19 03:19:07 -0700 | [diff] [blame] | 965 | msg.msg_flags = flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | |
| 967 | lock_sock(sk); |
| 968 | skb = np->pktoptions; |
| 969 | if (skb) |
| 970 | atomic_inc(&skb->users); |
| 971 | release_sock(sk); |
| 972 | |
| 973 | if (skb) { |
| 974 | int err = datagram_recv_ctl(sk, &msg, skb); |
| 975 | kfree_skb(skb); |
| 976 | if (err) |
| 977 | return err; |
| 978 | } else { |
| 979 | if (np->rxopt.bits.rxinfo) { |
| 980 | struct in6_pktinfo src_info; |
Yang Hongyang | f250dcd | 2008-12-16 02:07:45 -0800 | [diff] [blame] | 981 | src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif : |
| 982 | np->sticky_pktinfo.ipi6_ifindex; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 983 | 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] | 984 | put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info); |
| 985 | } |
| 986 | if (np->rxopt.bits.rxhlim) { |
| 987 | int hlim = np->mcast_hops; |
| 988 | put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim); |
| 989 | } |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 990 | if (np->rxopt.bits.rxoinfo) { |
| 991 | struct in6_pktinfo src_info; |
Yang Hongyang | f250dcd | 2008-12-16 02:07:45 -0800 | [diff] [blame] | 992 | src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif : |
| 993 | np->sticky_pktinfo.ipi6_ifindex; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 994 | 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] | 995 | put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info); |
| 996 | } |
| 997 | if (np->rxopt.bits.rxohlim) { |
| 998 | int hlim = np->mcast_hops; |
| 999 | put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim); |
| 1000 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | } |
| 1002 | len -= msg.msg_controllen; |
| 1003 | return put_user(len, optlen); |
| 1004 | } |
| 1005 | case IPV6_MTU: |
| 1006 | { |
| 1007 | struct dst_entry *dst; |
Eric Dumazet | b6c6712 | 2010-04-08 23:03:29 +0000 | [diff] [blame] | 1008 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1009 | val = 0; |
Eric Dumazet | b6c6712 | 2010-04-08 23:03:29 +0000 | [diff] [blame] | 1010 | rcu_read_lock(); |
| 1011 | dst = __sk_dst_get(sk); |
| 1012 | if (dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | val = dst_mtu(dst); |
Eric Dumazet | b6c6712 | 2010-04-08 23:03:29 +0000 | [diff] [blame] | 1014 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | if (!val) |
| 1016 | return -ENOTCONN; |
| 1017 | break; |
| 1018 | } |
| 1019 | |
| 1020 | case IPV6_V6ONLY: |
| 1021 | val = np->ipv6only; |
| 1022 | break; |
| 1023 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1024 | case IPV6_RECVPKTINFO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | val = np->rxopt.bits.rxinfo; |
| 1026 | break; |
| 1027 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1028 | case IPV6_2292PKTINFO: |
| 1029 | val = np->rxopt.bits.rxoinfo; |
| 1030 | break; |
| 1031 | |
| 1032 | case IPV6_RECVHOPLIMIT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | val = np->rxopt.bits.rxhlim; |
| 1034 | break; |
| 1035 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1036 | case IPV6_2292HOPLIMIT: |
| 1037 | val = np->rxopt.bits.rxohlim; |
| 1038 | break; |
| 1039 | |
| 1040 | case IPV6_RECVRTHDR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | val = np->rxopt.bits.srcrt; |
| 1042 | break; |
| 1043 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1044 | case IPV6_2292RTHDR: |
| 1045 | val = np->rxopt.bits.osrcrt; |
| 1046 | break; |
| 1047 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | case IPV6_HOPOPTS: |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1049 | case IPV6_RTHDRDSTOPTS: |
| 1050 | case IPV6_RTHDR: |
| 1051 | case IPV6_DSTOPTS: |
| 1052 | { |
| 1053 | |
| 1054 | lock_sock(sk); |
David S. Miller | 28693079 | 2007-03-07 12:50:46 -0800 | [diff] [blame] | 1055 | len = ipv6_getsockopt_sticky(sk, np->opt, |
YOSHIFUJI Hideaki | 4c6510a | 2007-03-18 17:35:57 -0700 | [diff] [blame] | 1056 | optname, optval, len); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1057 | release_sock(sk); |
Yang Hongyang | 05335c2 | 2008-05-28 16:23:47 +0800 | [diff] [blame] | 1058 | /* check if ipv6_getsockopt_sticky() returns err code */ |
| 1059 | if (len < 0) |
| 1060 | return len; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1061 | return put_user(len, optlen); |
| 1062 | } |
| 1063 | |
| 1064 | case IPV6_RECVHOPOPTS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | val = np->rxopt.bits.hopopts; |
| 1066 | break; |
| 1067 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1068 | case IPV6_2292HOPOPTS: |
| 1069 | val = np->rxopt.bits.ohopopts; |
| 1070 | break; |
| 1071 | |
| 1072 | case IPV6_RECVDSTOPTS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | val = np->rxopt.bits.dstopts; |
| 1074 | break; |
| 1075 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1076 | case IPV6_2292DSTOPTS: |
| 1077 | val = np->rxopt.bits.odstopts; |
| 1078 | break; |
| 1079 | |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 1080 | case IPV6_TCLASS: |
| 1081 | val = np->tclass; |
| 1082 | break; |
| 1083 | |
| 1084 | case IPV6_RECVTCLASS: |
| 1085 | val = np->rxopt.bits.rxtclass; |
| 1086 | break; |
| 1087 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | case IPV6_FLOWINFO: |
| 1089 | val = np->rxopt.bits.rxflow; |
| 1090 | break; |
| 1091 | |
Brian Haley | 793b147 | 2010-04-23 11:26:07 +0000 | [diff] [blame] | 1092 | case IPV6_RECVPATHMTU: |
| 1093 | val = np->rxopt.bits.rxpmtu; |
| 1094 | break; |
| 1095 | |
| 1096 | case IPV6_PATHMTU: |
| 1097 | { |
| 1098 | struct dst_entry *dst; |
| 1099 | struct ip6_mtuinfo mtuinfo; |
| 1100 | |
| 1101 | if (len < sizeof(mtuinfo)) |
| 1102 | return -EINVAL; |
| 1103 | |
| 1104 | len = sizeof(mtuinfo); |
| 1105 | memset(&mtuinfo, 0, sizeof(mtuinfo)); |
| 1106 | |
| 1107 | rcu_read_lock(); |
| 1108 | dst = __sk_dst_get(sk); |
| 1109 | if (dst) |
| 1110 | mtuinfo.ip6m_mtu = dst_mtu(dst); |
| 1111 | rcu_read_unlock(); |
| 1112 | if (!mtuinfo.ip6m_mtu) |
| 1113 | return -ENOTCONN; |
| 1114 | |
| 1115 | if (put_user(len, optlen)) |
| 1116 | return -EFAULT; |
| 1117 | if (copy_to_user(optval, &mtuinfo, len)) |
| 1118 | return -EFAULT; |
| 1119 | |
| 1120 | return 0; |
| 1121 | break; |
| 1122 | } |
| 1123 | |
Balazs Scheidler | 6c46862 | 2010-10-21 16:08:28 +0200 | [diff] [blame] | 1124 | case IPV6_TRANSPARENT: |
| 1125 | val = inet_sk(sk)->transparent; |
| 1126 | break; |
| 1127 | |
| 1128 | case IPV6_RECVORIGDSTADDR: |
| 1129 | val = np->rxopt.bits.rxorigdstaddr; |
| 1130 | break; |
| 1131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | case IPV6_UNICAST_HOPS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | case IPV6_MULTICAST_HOPS: |
Brian Haley | befffe9 | 2006-12-12 17:09:49 -0800 | [diff] [blame] | 1134 | { |
| 1135 | struct dst_entry *dst; |
| 1136 | |
| 1137 | if (optname == IPV6_UNICAST_HOPS) |
| 1138 | val = np->hop_limit; |
| 1139 | else |
| 1140 | val = np->mcast_hops; |
| 1141 | |
Eric Dumazet | b6c6712 | 2010-04-08 23:03:29 +0000 | [diff] [blame] | 1142 | if (val < 0) { |
| 1143 | rcu_read_lock(); |
| 1144 | dst = __sk_dst_get(sk); |
| 1145 | if (dst) |
YOSHIFUJI Hideaki | 6b75d09 | 2008-03-10 06:00:30 -0400 | [diff] [blame] | 1146 | val = ip6_dst_hoplimit(dst); |
Eric Dumazet | b6c6712 | 2010-04-08 23:03:29 +0000 | [diff] [blame] | 1147 | rcu_read_unlock(); |
Brian Haley | befffe9 | 2006-12-12 17:09:49 -0800 | [diff] [blame] | 1148 | } |
Eric Dumazet | b6c6712 | 2010-04-08 23:03:29 +0000 | [diff] [blame] | 1149 | |
Brian Haley | befffe9 | 2006-12-12 17:09:49 -0800 | [diff] [blame] | 1150 | if (val < 0) |
YOSHIFUJI Hideaki | 53b7997 | 2008-07-19 22:35:03 -0700 | [diff] [blame] | 1151 | val = sock_net(sk)->ipv6.devconf_all->hop_limit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | break; |
Brian Haley | befffe9 | 2006-12-12 17:09:49 -0800 | [diff] [blame] | 1153 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | |
| 1155 | case IPV6_MULTICAST_LOOP: |
| 1156 | val = np->mc_loop; |
| 1157 | break; |
| 1158 | |
| 1159 | case IPV6_MULTICAST_IF: |
| 1160 | val = np->mcast_oif; |
| 1161 | break; |
| 1162 | |
| 1163 | case IPV6_MTU_DISCOVER: |
| 1164 | val = np->pmtudisc; |
| 1165 | break; |
| 1166 | |
| 1167 | case IPV6_RECVERR: |
| 1168 | val = np->recverr; |
| 1169 | break; |
| 1170 | |
| 1171 | case IPV6_FLOWINFO_SEND: |
| 1172 | val = np->sndflow; |
| 1173 | break; |
| 1174 | |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1175 | case IPV6_ADDR_PREFERENCES: |
| 1176 | val = 0; |
| 1177 | |
| 1178 | if (np->srcprefs & IPV6_PREFER_SRC_TMP) |
| 1179 | val |= IPV6_PREFER_SRC_TMP; |
| 1180 | else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC) |
| 1181 | val |= IPV6_PREFER_SRC_PUBLIC; |
| 1182 | else { |
| 1183 | /* XXX: should we return system default? */ |
| 1184 | val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT; |
| 1185 | } |
| 1186 | |
| 1187 | if (np->srcprefs & IPV6_PREFER_SRC_COA) |
| 1188 | val |= IPV6_PREFER_SRC_COA; |
| 1189 | else |
| 1190 | val |= IPV6_PREFER_SRC_HOME; |
| 1191 | break; |
| 1192 | |
Stephen Hemminger | e802af9 | 2010-04-22 15:24:53 -0700 | [diff] [blame] | 1193 | case IPV6_MINHOPCOUNT: |
| 1194 | val = np->min_hopcount; |
| 1195 | break; |
| 1196 | |
Brian Haley | 793b147 | 2010-04-23 11:26:07 +0000 | [diff] [blame] | 1197 | case IPV6_DONTFRAG: |
| 1198 | val = np->dontfrag; |
| 1199 | break; |
| 1200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | default: |
Wei Yongjun | cf6fc4a | 2007-12-16 13:39:57 -0800 | [diff] [blame] | 1202 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | } |
| 1204 | len = min_t(unsigned int, sizeof(int), len); |
| 1205 | if(put_user(len, optlen)) |
| 1206 | return -EFAULT; |
| 1207 | if(copy_to_user(optval,&val,len)) |
| 1208 | return -EFAULT; |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1212 | int ipv6_getsockopt(struct sock *sk, int level, int optname, |
| 1213 | char __user *optval, int __user *optlen) |
| 1214 | { |
| 1215 | int err; |
| 1216 | |
| 1217 | if (level == SOL_IP && sk->sk_type != SOCK_RAW) |
| 1218 | return udp_prot.getsockopt(sk, level, optname, optval, optlen); |
| 1219 | |
| 1220 | if(level != SOL_IPV6) |
| 1221 | return -ENOPROTOOPT; |
| 1222 | |
Daniel Baluta | 98e7743 | 2011-08-19 03:19:07 -0700 | [diff] [blame] | 1223 | err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1224 | #ifdef CONFIG_NETFILTER |
Wei Yongjun | cf6fc4a | 2007-12-16 13:39:57 -0800 | [diff] [blame] | 1225 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 1226 | if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1227 | int len; |
| 1228 | |
| 1229 | if (get_user(len, optlen)) |
| 1230 | return -EFAULT; |
| 1231 | |
| 1232 | lock_sock(sk); |
| 1233 | err = nf_getsockopt(sk, PF_INET6, optname, optval, |
| 1234 | &len); |
| 1235 | release_sock(sk); |
| 1236 | if (err >= 0) |
| 1237 | err = put_user(len, optlen); |
| 1238 | } |
| 1239 | #endif |
| 1240 | return err; |
| 1241 | } |
| 1242 | |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 1243 | EXPORT_SYMBOL(ipv6_getsockopt); |
| 1244 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1245 | #ifdef CONFIG_COMPAT |
| 1246 | 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] | 1247 | char __user *optval, int __user *optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1248 | { |
| 1249 | int err; |
| 1250 | |
| 1251 | if (level == SOL_IP && sk->sk_type != SOCK_RAW) { |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1252 | if (udp_prot.compat_getsockopt != NULL) |
| 1253 | return udp_prot.compat_getsockopt(sk, level, optname, |
| 1254 | optval, optlen); |
| 1255 | return udp_prot.getsockopt(sk, level, optname, optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1256 | } |
| 1257 | |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1258 | if (level != SOL_IPV6) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1259 | return -ENOPROTOOPT; |
| 1260 | |
Pavel Emelyanov | 8099179 | 2008-04-30 14:49:54 -0700 | [diff] [blame] | 1261 | if (optname == MCAST_MSFILTER) |
| 1262 | return compat_mc_getsockopt(sk, level, optname, optval, optlen, |
| 1263 | ipv6_getsockopt); |
| 1264 | |
Daniel Baluta | 98e7743 | 2011-08-19 03:19:07 -0700 | [diff] [blame] | 1265 | err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, |
| 1266 | MSG_CMSG_COMPAT); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1267 | #ifdef CONFIG_NETFILTER |
Wei Yongjun | cf6fc4a | 2007-12-16 13:39:57 -0800 | [diff] [blame] | 1268 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 1269 | if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1270 | int len; |
| 1271 | |
| 1272 | if (get_user(len, optlen)) |
| 1273 | return -EFAULT; |
| 1274 | |
| 1275 | lock_sock(sk); |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1276 | err = compat_nf_getsockopt(sk, PF_INET6, |
| 1277 | optname, optval, &len); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1278 | release_sock(sk); |
| 1279 | if (err >= 0) |
| 1280 | err = put_user(len, optlen); |
| 1281 | } |
| 1282 | #endif |
| 1283 | return err; |
| 1284 | } |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1285 | |
| 1286 | EXPORT_SYMBOL(compat_ipv6_getsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1287 | #endif |
| 1288 | |