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 | * |
| 10 | * $Id: ipv6_sockglue.c,v 1.41 2002/02/01 22:01:04 davem Exp $ |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version |
| 15 | * 2 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * FIXME: Make the setsockopt code POSIX compliant: That is |
| 18 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * o Truncate getsockopt returns |
| 20 | * o Return an optlen of the truncated length if need be |
| 21 | * |
| 22 | * Changes: |
| 23 | * David L Stevens <dlstevens@us.ibm.com>: |
| 24 | * - added multicast source filtering API for MLDv2 |
| 25 | */ |
| 26 | |
| 27 | #include <linux/module.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 28 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/errno.h> |
| 30 | #include <linux/types.h> |
| 31 | #include <linux/socket.h> |
| 32 | #include <linux/sockios.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/net.h> |
| 34 | #include <linux/in6.h> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 35 | #include <linux/mroute6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/netdevice.h> |
| 37 | #include <linux/if_arp.h> |
| 38 | #include <linux/init.h> |
| 39 | #include <linux/sysctl.h> |
| 40 | #include <linux/netfilter.h> |
| 41 | |
| 42 | #include <net/sock.h> |
| 43 | #include <net/snmp.h> |
| 44 | #include <net/ipv6.h> |
| 45 | #include <net/ndisc.h> |
| 46 | #include <net/protocol.h> |
| 47 | #include <net/transp_v6.h> |
| 48 | #include <net/ip6_route.h> |
| 49 | #include <net/addrconf.h> |
| 50 | #include <net/inet_common.h> |
| 51 | #include <net/tcp.h> |
| 52 | #include <net/udp.h> |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 53 | #include <net/udplite.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #include <net/xfrm.h> |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 55 | #include <net/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | #include <asm/uaccess.h> |
| 58 | |
Eric Dumazet | ba89966 | 2005-08-26 12:05:31 -0700 | [diff] [blame] | 59 | DEFINE_SNMP_STAT(struct ipstats_mib, ipv6_statistics) __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | struct ip6_ra_chain *ip6_ra_chain; |
| 62 | DEFINE_RWLOCK(ip6_ra_lock); |
| 63 | |
| 64 | int ip6_ra_control(struct sock *sk, int sel, void (*destructor)(struct sock *)) |
| 65 | { |
| 66 | struct ip6_ra_chain *ra, *new_ra, **rap; |
| 67 | |
| 68 | /* RA packet may be delivered ONLY to IPPROTO_RAW socket */ |
| 69 | if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num != IPPROTO_RAW) |
| 70 | return -EINVAL; |
| 71 | |
| 72 | new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL; |
| 73 | |
| 74 | write_lock_bh(&ip6_ra_lock); |
| 75 | for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) { |
| 76 | if (ra->sk == sk) { |
| 77 | if (sel>=0) { |
| 78 | write_unlock_bh(&ip6_ra_lock); |
Jesper Juhl | a51482b | 2005-11-08 09:41:34 -0800 | [diff] [blame] | 79 | kfree(new_ra); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | return -EADDRINUSE; |
| 81 | } |
| 82 | |
| 83 | *rap = ra->next; |
| 84 | write_unlock_bh(&ip6_ra_lock); |
| 85 | |
| 86 | if (ra->destructor) |
| 87 | ra->destructor(sk); |
| 88 | sock_put(sk); |
| 89 | kfree(ra); |
| 90 | return 0; |
| 91 | } |
| 92 | } |
| 93 | if (new_ra == NULL) { |
| 94 | write_unlock_bh(&ip6_ra_lock); |
| 95 | return -ENOBUFS; |
| 96 | } |
| 97 | new_ra->sk = sk; |
| 98 | new_ra->sel = sel; |
| 99 | new_ra->destructor = destructor; |
| 100 | new_ra->next = ra; |
| 101 | *rap = new_ra; |
| 102 | sock_hold(sk); |
| 103 | write_unlock_bh(&ip6_ra_lock); |
| 104 | return 0; |
| 105 | } |
| 106 | |
YOSHIFUJI Hideaki | e7712f1 | 2008-04-13 23:21:52 -0700 | [diff] [blame] | 107 | static |
| 108 | struct ipv6_txoptions *ipv6_update_options(struct sock *sk, |
| 109 | struct ipv6_txoptions *opt) |
| 110 | { |
| 111 | if (inet_sk(sk)->is_icsk) { |
| 112 | if (opt && |
| 113 | !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) && |
| 114 | inet_sk(sk)->daddr != LOOPBACK4_IPV6) { |
| 115 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 116 | icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen; |
| 117 | icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie); |
| 118 | } |
| 119 | opt = xchg(&inet6_sk(sk)->opt, opt); |
| 120 | } else { |
| 121 | write_lock(&sk->sk_dst_lock); |
| 122 | opt = xchg(&inet6_sk(sk)->opt, opt); |
| 123 | write_unlock(&sk->sk_dst_lock); |
| 124 | } |
| 125 | sk_dst_reset(sk); |
| 126 | |
| 127 | return opt; |
| 128 | } |
| 129 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 130 | static int do_ipv6_setsockopt(struct sock *sk, int level, int optname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | char __user *optval, int optlen) |
| 132 | { |
| 133 | struct ipv6_pinfo *np = inet6_sk(sk); |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 134 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | int val, valbool; |
| 136 | int retv = -ENOPROTOOPT; |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | if (optval == NULL) |
| 139 | val=0; |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 140 | else { |
| 141 | if (optlen >= sizeof(int)) { |
| 142 | if (get_user(val, (int __user *) optval)) |
| 143 | return -EFAULT; |
| 144 | } else |
| 145 | val = 0; |
| 146 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | valbool = (val!=0); |
| 149 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 150 | if (ip6_mroute_opt(optname)) |
| 151 | return ip6_mroute_setsockopt(sk, optname, optval, optlen); |
| 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | lock_sock(sk); |
| 154 | |
| 155 | switch (optname) { |
| 156 | |
| 157 | case IPV6_ADDRFORM: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 158 | if (optlen < sizeof(int)) |
| 159 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | if (val == PF_INET) { |
| 161 | struct ipv6_txoptions *opt; |
| 162 | struct sk_buff *pktopt; |
| 163 | |
Denis V. Lunev | 49d074f | 2008-06-04 15:49:06 +0400 | [diff] [blame] | 164 | if (sk->sk_type == SOCK_RAW) |
| 165 | break; |
| 166 | |
Denis V. Lunev | 9596cc82 | 2008-06-04 15:49:08 +0400 | [diff] [blame] | 167 | if (sk->sk_protocol == IPPROTO_UDP || |
| 168 | sk->sk_protocol == IPPROTO_UDPLITE) { |
| 169 | struct udp_sock *up = udp_sk(sk); |
| 170 | if (up->pending == AF_INET6) { |
| 171 | retv = -EBUSY; |
| 172 | break; |
| 173 | } |
| 174 | } else if (sk->sk_protocol != IPPROTO_TCP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | break; |
| 176 | |
| 177 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 178 | retv = -ENOTCONN; |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | if (ipv6_only_sock(sk) || |
Brian Haley | e773e4f | 2007-08-24 23:16:08 -0700 | [diff] [blame] | 183 | !ipv6_addr_v4mapped(&np->daddr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | retv = -EADDRNOTAVAIL; |
| 185 | break; |
| 186 | } |
| 187 | |
| 188 | fl6_free_socklist(sk); |
| 189 | ipv6_sock_mc_close(sk); |
| 190 | |
Arnaldo Carvalho de Melo | e684897 | 2005-08-09 19:45:38 -0700 | [diff] [blame] | 191 | /* |
| 192 | * Sock is moving from IPv6 to IPv4 (sk_prot), so |
| 193 | * remove it from the refcnt debug socks count in the |
| 194 | * original family... |
| 195 | */ |
| 196 | sk_refcnt_debug_dec(sk); |
| 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | if (sk->sk_protocol == IPPROTO_TCP) { |
Arnaldo Carvalho de Melo | d83d846 | 2005-12-13 23:26:10 -0800 | [diff] [blame] | 199 | struct inet_connection_sock *icsk = inet_csk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | local_bh_disable(); |
Pavel Emelyanov | c29a0bc | 2008-03-31 19:41:46 -0700 | [diff] [blame] | 201 | sock_prot_inuse_add(net, sk->sk_prot, -1); |
| 202 | sock_prot_inuse_add(net, &tcp_prot, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | local_bh_enable(); |
| 204 | sk->sk_prot = &tcp_prot; |
Arnaldo Carvalho de Melo | d83d846 | 2005-12-13 23:26:10 -0800 | [diff] [blame] | 205 | icsk->icsk_af_ops = &ipv4_specific; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | sk->sk_socket->ops = &inet_stream_ops; |
| 207 | sk->sk_family = PF_INET; |
Arnaldo Carvalho de Melo | d83d846 | 2005-12-13 23:26:10 -0800 | [diff] [blame] | 208 | tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } else { |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 210 | struct proto *prot = &udp_prot; |
| 211 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 212 | if (sk->sk_protocol == IPPROTO_UDPLITE) |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 213 | prot = &udplite_prot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | local_bh_disable(); |
Pavel Emelyanov | c29a0bc | 2008-03-31 19:41:46 -0700 | [diff] [blame] | 215 | sock_prot_inuse_add(net, sk->sk_prot, -1); |
| 216 | sock_prot_inuse_add(net, prot, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | local_bh_enable(); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 218 | sk->sk_prot = prot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | sk->sk_socket->ops = &inet_dgram_ops; |
| 220 | sk->sk_family = PF_INET; |
| 221 | } |
| 222 | opt = xchg(&np->opt, NULL); |
| 223 | if (opt) |
| 224 | sock_kfree_s(sk, opt, opt->tot_len); |
| 225 | pktopt = xchg(&np->pktoptions, NULL); |
| 226 | if (pktopt) |
| 227 | kfree_skb(pktopt); |
| 228 | |
| 229 | sk->sk_destruct = inet_sock_destruct; |
Arnaldo Carvalho de Melo | e684897 | 2005-08-09 19:45:38 -0700 | [diff] [blame] | 230 | /* |
| 231 | * ... and add it to the refcnt debug socks count |
| 232 | * in the new family. -acme |
| 233 | */ |
| 234 | sk_refcnt_debug_inc(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | module_put(THIS_MODULE); |
| 236 | retv = 0; |
| 237 | break; |
| 238 | } |
| 239 | goto e_inval; |
| 240 | |
| 241 | case IPV6_V6ONLY: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 242 | if (optlen < sizeof(int) || |
| 243 | inet_sk(sk)->num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | goto e_inval; |
| 245 | np->ipv6only = valbool; |
| 246 | retv = 0; |
| 247 | break; |
| 248 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 249 | case IPV6_RECVPKTINFO: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 250 | if (optlen < sizeof(int)) |
| 251 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | np->rxopt.bits.rxinfo = valbool; |
| 253 | retv = 0; |
| 254 | break; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 255 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 256 | case IPV6_2292PKTINFO: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 257 | if (optlen < sizeof(int)) |
| 258 | goto e_inval; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 259 | np->rxopt.bits.rxoinfo = valbool; |
| 260 | retv = 0; |
| 261 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 263 | case IPV6_RECVHOPLIMIT: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 264 | if (optlen < sizeof(int)) |
| 265 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | np->rxopt.bits.rxhlim = valbool; |
| 267 | retv = 0; |
| 268 | break; |
| 269 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 270 | case IPV6_2292HOPLIMIT: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 271 | if (optlen < sizeof(int)) |
| 272 | goto e_inval; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 273 | np->rxopt.bits.rxohlim = valbool; |
| 274 | retv = 0; |
| 275 | break; |
| 276 | |
| 277 | case IPV6_RECVRTHDR: |
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.srcrt = valbool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | retv = 0; |
| 282 | break; |
| 283 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 284 | case IPV6_2292RTHDR: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 285 | if (optlen < sizeof(int)) |
| 286 | goto e_inval; |
YOSHIFUJI Hideaki | 4c75209 | 2007-05-23 13:28:48 +0900 | [diff] [blame] | 287 | np->rxopt.bits.osrcrt = valbool; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 288 | retv = 0; |
| 289 | break; |
| 290 | |
| 291 | case IPV6_RECVHOPOPTS: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 292 | if (optlen < sizeof(int)) |
| 293 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | np->rxopt.bits.hopopts = valbool; |
| 295 | retv = 0; |
| 296 | break; |
| 297 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 298 | case IPV6_2292HOPOPTS: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 299 | if (optlen < sizeof(int)) |
| 300 | goto e_inval; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 301 | np->rxopt.bits.ohopopts = valbool; |
| 302 | retv = 0; |
| 303 | break; |
| 304 | |
| 305 | case IPV6_RECVDSTOPTS: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 306 | if (optlen < sizeof(int)) |
| 307 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | np->rxopt.bits.dstopts = valbool; |
| 309 | retv = 0; |
| 310 | break; |
| 311 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 312 | case IPV6_2292DSTOPTS: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 313 | if (optlen < sizeof(int)) |
| 314 | goto e_inval; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 315 | np->rxopt.bits.odstopts = valbool; |
| 316 | retv = 0; |
| 317 | break; |
| 318 | |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 319 | case IPV6_TCLASS: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 320 | if (optlen < sizeof(int)) |
| 321 | goto e_inval; |
Remi Denis-Courmont | d0ee011 | 2006-09-13 20:08:07 -0700 | [diff] [blame] | 322 | if (val < -1 || val > 0xff) |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 323 | goto e_inval; |
| 324 | np->tclass = val; |
| 325 | retv = 0; |
| 326 | break; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 327 | |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 328 | case IPV6_RECVTCLASS: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 329 | if (optlen < sizeof(int)) |
| 330 | goto e_inval; |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 331 | np->rxopt.bits.rxtclass = valbool; |
| 332 | retv = 0; |
| 333 | break; |
| 334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | case IPV6_FLOWINFO: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 336 | if (optlen < sizeof(int)) |
| 337 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | np->rxopt.bits.rxflow = valbool; |
| 339 | retv = 0; |
| 340 | break; |
| 341 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 342 | case IPV6_HOPOPTS: |
| 343 | case IPV6_RTHDRDSTOPTS: |
| 344 | case IPV6_RTHDR: |
| 345 | case IPV6_DSTOPTS: |
| 346 | { |
| 347 | struct ipv6_txoptions *opt; |
| 348 | if (optlen == 0) |
Luiz Capitulino | cb422c4 | 2005-11-14 21:43:36 -0800 | [diff] [blame] | 349 | optval = NULL; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 350 | |
| 351 | /* hop-by-hop / destination options are privileged option */ |
| 352 | retv = -EPERM; |
| 353 | if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW)) |
| 354 | break; |
| 355 | |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 356 | if (optlen < sizeof(struct ipv6_opt_hdr) || |
| 357 | optlen & 0x7 || optlen > 8 * 255) |
| 358 | goto e_inval; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 359 | |
| 360 | opt = ipv6_renew_options(sk, np->opt, optname, |
| 361 | (struct ipv6_opt_hdr __user *)optval, |
| 362 | optlen); |
| 363 | if (IS_ERR(opt)) { |
| 364 | retv = PTR_ERR(opt); |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | /* routing header option needs extra check */ |
Olaf Kirch | dfee0a7 | 2007-03-09 13:55:38 -0800 | [diff] [blame] | 369 | if (optname == IPV6_RTHDR && opt && opt->srcrt) { |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 370 | struct ipv6_rt_hdr *rthdr = opt->srcrt; |
Masahide NAKAMURA | 280a9d3 | 2006-08-23 19:17:12 -0700 | [diff] [blame] | 371 | switch (rthdr->type) { |
Masahide NAKAMURA | 59fbb3a | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 372 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
Masahide NAKAMURA | 280a9d3 | 2006-08-23 19:17:12 -0700 | [diff] [blame] | 373 | case IPV6_SRCRT_TYPE_2: |
Masahide NAKAMURA | 280a9d3 | 2006-08-23 19:17:12 -0700 | [diff] [blame] | 374 | break; |
YOSHIFUJI Hideaki | bb4dbf9 | 2007-07-10 22:55:49 -0700 | [diff] [blame] | 375 | #endif |
Masahide NAKAMURA | 280a9d3 | 2006-08-23 19:17:12 -0700 | [diff] [blame] | 376 | default: |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 377 | goto sticky_done; |
Masahide NAKAMURA | 280a9d3 | 2006-08-23 19:17:12 -0700 | [diff] [blame] | 378 | } |
| 379 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 380 | if ((rthdr->hdrlen & 1) || |
| 381 | (rthdr->hdrlen >> 1) != rthdr->segments_left) |
| 382 | goto sticky_done; |
| 383 | } |
| 384 | |
| 385 | retv = 0; |
YOSHIFUJI Hideaki | e7712f1 | 2008-04-13 23:21:52 -0700 | [diff] [blame] | 386 | opt = ipv6_update_options(sk, opt); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 387 | sticky_done: |
| 388 | if (opt) |
| 389 | sock_kfree_s(sk, opt, opt->tot_len); |
| 390 | break; |
| 391 | } |
| 392 | |
| 393 | case IPV6_2292PKTOPTIONS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
| 395 | struct ipv6_txoptions *opt = NULL; |
| 396 | struct msghdr msg; |
| 397 | struct flowi fl; |
| 398 | int junk; |
| 399 | |
| 400 | fl.fl6_flowlabel = 0; |
| 401 | fl.oif = sk->sk_bound_dev_if; |
| 402 | |
| 403 | if (optlen == 0) |
| 404 | goto update; |
| 405 | |
| 406 | /* 1K is probably excessive |
| 407 | * 1K is surely not enough, 2K per standard header is 16K. |
| 408 | */ |
| 409 | retv = -EINVAL; |
| 410 | if (optlen > 64*1024) |
| 411 | break; |
| 412 | |
| 413 | opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL); |
| 414 | retv = -ENOBUFS; |
| 415 | if (opt == NULL) |
| 416 | break; |
| 417 | |
| 418 | memset(opt, 0, sizeof(*opt)); |
| 419 | opt->tot_len = sizeof(*opt) + optlen; |
| 420 | retv = -EFAULT; |
| 421 | if (copy_from_user(opt+1, optval, optlen)) |
| 422 | goto done; |
| 423 | |
| 424 | msg.msg_controllen = optlen; |
| 425 | msg.msg_control = (void*)(opt+1); |
| 426 | |
YOSHIFUJI Hideaki | 91e1908 | 2008-06-04 13:02:49 +0900 | [diff] [blame] | 427 | retv = datagram_send_ctl(net, &msg, &fl, opt, &junk, &junk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | if (retv) |
| 429 | goto done; |
| 430 | update: |
| 431 | retv = 0; |
YOSHIFUJI Hideaki | e7712f1 | 2008-04-13 23:21:52 -0700 | [diff] [blame] | 432 | opt = ipv6_update_options(sk, opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | done: |
| 434 | if (opt) |
| 435 | sock_kfree_s(sk, opt, opt->tot_len); |
| 436 | break; |
| 437 | } |
| 438 | case IPV6_UNICAST_HOPS: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 439 | if (optlen < sizeof(int)) |
| 440 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | if (val > 255 || val < -1) |
| 442 | goto e_inval; |
| 443 | np->hop_limit = val; |
| 444 | retv = 0; |
| 445 | break; |
| 446 | |
| 447 | case IPV6_MULTICAST_HOPS: |
| 448 | if (sk->sk_type == SOCK_STREAM) |
| 449 | goto e_inval; |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 450 | if (optlen < sizeof(int)) |
| 451 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | if (val > 255 || val < -1) |
| 453 | goto e_inval; |
| 454 | np->mcast_hops = val; |
| 455 | retv = 0; |
| 456 | break; |
| 457 | |
| 458 | case IPV6_MULTICAST_LOOP: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 459 | if (optlen < sizeof(int)) |
| 460 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | np->mc_loop = valbool; |
| 462 | retv = 0; |
| 463 | break; |
| 464 | |
| 465 | case IPV6_MULTICAST_IF: |
| 466 | if (sk->sk_type == SOCK_STREAM) |
| 467 | goto e_inval; |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 468 | if (optlen < sizeof(int)) |
| 469 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
Brian Haley | 4953f0f | 2007-10-11 14:39:29 -0700 | [diff] [blame] | 471 | if (val) { |
| 472 | if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val) |
| 473 | goto e_inval; |
| 474 | |
Daniel Lezcano | 8a3edd8 | 2008-03-07 11:14:16 -0800 | [diff] [blame] | 475 | if (__dev_get_by_index(net, val) == NULL) { |
Brian Haley | 4953f0f | 2007-10-11 14:39:29 -0700 | [diff] [blame] | 476 | retv = -ENODEV; |
| 477 | break; |
| 478 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
| 480 | np->mcast_oif = val; |
| 481 | retv = 0; |
| 482 | break; |
| 483 | case IPV6_ADD_MEMBERSHIP: |
| 484 | case IPV6_DROP_MEMBERSHIP: |
| 485 | { |
| 486 | struct ipv6_mreq mreq; |
| 487 | |
Wang Chen | a28398b | 2008-04-07 09:42:07 +0800 | [diff] [blame] | 488 | if (optlen < sizeof(struct ipv6_mreq)) |
| 489 | goto e_inval; |
| 490 | |
Flavio Leitner | a96fb49 | 2007-08-24 22:16:39 -0700 | [diff] [blame] | 491 | retv = -EPROTO; |
| 492 | if (inet_sk(sk)->is_icsk) |
| 493 | break; |
| 494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | retv = -EFAULT; |
| 496 | if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq))) |
| 497 | break; |
| 498 | |
| 499 | if (optname == IPV6_ADD_MEMBERSHIP) |
| 500 | retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr); |
| 501 | else |
| 502 | retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr); |
| 503 | break; |
| 504 | } |
| 505 | case IPV6_JOIN_ANYCAST: |
| 506 | case IPV6_LEAVE_ANYCAST: |
| 507 | { |
| 508 | struct ipv6_mreq mreq; |
| 509 | |
Wang Chen | a28398b | 2008-04-07 09:42:07 +0800 | [diff] [blame] | 510 | if (optlen < sizeof(struct ipv6_mreq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | goto e_inval; |
| 512 | |
| 513 | retv = -EFAULT; |
| 514 | if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq))) |
| 515 | break; |
| 516 | |
| 517 | if (optname == IPV6_JOIN_ANYCAST) |
| 518 | retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr); |
| 519 | else |
| 520 | retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr); |
| 521 | break; |
| 522 | } |
| 523 | case MCAST_JOIN_GROUP: |
| 524 | case MCAST_LEAVE_GROUP: |
| 525 | { |
| 526 | struct group_req greq; |
| 527 | struct sockaddr_in6 *psin6; |
| 528 | |
Wang Chen | a28398b | 2008-04-07 09:42:07 +0800 | [diff] [blame] | 529 | if (optlen < sizeof(struct group_req)) |
| 530 | goto e_inval; |
| 531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | retv = -EFAULT; |
| 533 | if (copy_from_user(&greq, optval, sizeof(struct group_req))) |
| 534 | break; |
| 535 | if (greq.gr_group.ss_family != AF_INET6) { |
| 536 | retv = -EADDRNOTAVAIL; |
| 537 | break; |
| 538 | } |
| 539 | psin6 = (struct sockaddr_in6 *)&greq.gr_group; |
| 540 | if (optname == MCAST_JOIN_GROUP) |
| 541 | retv = ipv6_sock_mc_join(sk, greq.gr_interface, |
| 542 | &psin6->sin6_addr); |
| 543 | else |
| 544 | retv = ipv6_sock_mc_drop(sk, greq.gr_interface, |
| 545 | &psin6->sin6_addr); |
| 546 | break; |
| 547 | } |
| 548 | case MCAST_JOIN_SOURCE_GROUP: |
| 549 | case MCAST_LEAVE_SOURCE_GROUP: |
| 550 | case MCAST_BLOCK_SOURCE: |
| 551 | case MCAST_UNBLOCK_SOURCE: |
| 552 | { |
| 553 | struct group_source_req greqs; |
| 554 | int omode, add; |
| 555 | |
Wang Chen | a28398b | 2008-04-07 09:42:07 +0800 | [diff] [blame] | 556 | if (optlen < sizeof(struct group_source_req)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | goto e_inval; |
| 558 | if (copy_from_user(&greqs, optval, sizeof(greqs))) { |
| 559 | retv = -EFAULT; |
| 560 | break; |
| 561 | } |
| 562 | if (greqs.gsr_group.ss_family != AF_INET6 || |
| 563 | greqs.gsr_source.ss_family != AF_INET6) { |
| 564 | retv = -EADDRNOTAVAIL; |
| 565 | break; |
| 566 | } |
| 567 | if (optname == MCAST_BLOCK_SOURCE) { |
| 568 | omode = MCAST_EXCLUDE; |
| 569 | add = 1; |
| 570 | } else if (optname == MCAST_UNBLOCK_SOURCE) { |
| 571 | omode = MCAST_EXCLUDE; |
| 572 | add = 0; |
| 573 | } else if (optname == MCAST_JOIN_SOURCE_GROUP) { |
| 574 | struct sockaddr_in6 *psin6; |
| 575 | |
| 576 | psin6 = (struct sockaddr_in6 *)&greqs.gsr_group; |
| 577 | retv = ipv6_sock_mc_join(sk, greqs.gsr_interface, |
| 578 | &psin6->sin6_addr); |
David L Stevens | c9e3e8b | 2005-06-21 13:58:25 -0700 | [diff] [blame] | 579 | /* prior join w/ different source is ok */ |
| 580 | if (retv && retv != -EADDRINUSE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | break; |
| 582 | omode = MCAST_INCLUDE; |
| 583 | add = 1; |
David L Stevens | c9e3e8b | 2005-06-21 13:58:25 -0700 | [diff] [blame] | 584 | } else /* MCAST_LEAVE_SOURCE_GROUP */ { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | omode = MCAST_INCLUDE; |
| 586 | add = 0; |
| 587 | } |
| 588 | retv = ip6_mc_source(add, omode, sk, &greqs); |
| 589 | break; |
| 590 | } |
| 591 | case MCAST_MSFILTER: |
| 592 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | extern int sysctl_mld_max_msf; |
| 594 | struct group_filter *gsf; |
| 595 | |
| 596 | if (optlen < GROUP_FILTER_SIZE(0)) |
| 597 | goto e_inval; |
| 598 | if (optlen > sysctl_optmem_max) { |
| 599 | retv = -ENOBUFS; |
| 600 | break; |
| 601 | } |
Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 602 | gsf = kmalloc(optlen,GFP_KERNEL); |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 603 | if (!gsf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | retv = -ENOBUFS; |
| 605 | break; |
| 606 | } |
| 607 | retv = -EFAULT; |
| 608 | if (copy_from_user(gsf, optval, optlen)) { |
| 609 | kfree(gsf); |
| 610 | break; |
| 611 | } |
| 612 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ |
| 613 | if (gsf->gf_numsrc >= 0x1ffffffU || |
| 614 | gsf->gf_numsrc > sysctl_mld_max_msf) { |
| 615 | kfree(gsf); |
| 616 | retv = -ENOBUFS; |
| 617 | break; |
| 618 | } |
| 619 | if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) { |
| 620 | kfree(gsf); |
| 621 | retv = -EINVAL; |
| 622 | break; |
| 623 | } |
| 624 | retv = ip6_mc_msfilter(sk, gsf); |
| 625 | kfree(gsf); |
| 626 | |
| 627 | break; |
| 628 | } |
| 629 | case IPV6_ROUTER_ALERT: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 630 | if (optlen < sizeof(int)) |
| 631 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | retv = ip6_ra_control(sk, val, NULL); |
| 633 | break; |
| 634 | case IPV6_MTU_DISCOVER: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 635 | if (optlen < sizeof(int)) |
| 636 | goto e_inval; |
John Heffner | 628a5c5 | 2007-04-20 15:53:27 -0700 | [diff] [blame] | 637 | if (val<0 || val>3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | goto e_inval; |
| 639 | np->pmtudisc = val; |
| 640 | retv = 0; |
| 641 | break; |
| 642 | case IPV6_MTU: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 643 | if (optlen < sizeof(int)) |
| 644 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | if (val && val < IPV6_MIN_MTU) |
| 646 | goto e_inval; |
| 647 | np->frag_size = val; |
| 648 | retv = 0; |
| 649 | break; |
| 650 | case IPV6_RECVERR: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 651 | if (optlen < sizeof(int)) |
| 652 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | np->recverr = valbool; |
| 654 | if (!val) |
| 655 | skb_queue_purge(&sk->sk_error_queue); |
| 656 | retv = 0; |
| 657 | break; |
| 658 | case IPV6_FLOWINFO_SEND: |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 659 | if (optlen < sizeof(int)) |
| 660 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | np->sndflow = valbool; |
| 662 | retv = 0; |
| 663 | break; |
| 664 | case IPV6_FLOWLABEL_MGR: |
| 665 | retv = ipv6_flowlabel_opt(sk, optval, optlen); |
| 666 | break; |
| 667 | case IPV6_IPSEC_POLICY: |
| 668 | case IPV6_XFRM_POLICY: |
Herbert Xu | 6fc0b4a | 2005-08-06 06:33:15 -0700 | [diff] [blame] | 669 | retv = -EPERM; |
| 670 | if (!capable(CAP_NET_ADMIN)) |
| 671 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | retv = xfrm_user_policy(sk, optname, optval, optlen); |
| 673 | break; |
| 674 | |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 675 | case IPV6_ADDR_PREFERENCES: |
| 676 | { |
| 677 | unsigned int pref = 0; |
| 678 | unsigned int prefmask = ~0; |
| 679 | |
YOSHIFUJI Hideaki | b2a9d7c | 2008-04-12 12:59:42 +0900 | [diff] [blame] | 680 | if (optlen < sizeof(int)) |
| 681 | goto e_inval; |
| 682 | |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 683 | retv = -EINVAL; |
| 684 | |
| 685 | /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */ |
| 686 | switch (val & (IPV6_PREFER_SRC_PUBLIC| |
| 687 | IPV6_PREFER_SRC_TMP| |
| 688 | IPV6_PREFER_SRC_PUBTMP_DEFAULT)) { |
| 689 | case IPV6_PREFER_SRC_PUBLIC: |
| 690 | pref |= IPV6_PREFER_SRC_PUBLIC; |
| 691 | break; |
| 692 | case IPV6_PREFER_SRC_TMP: |
| 693 | pref |= IPV6_PREFER_SRC_TMP; |
| 694 | break; |
| 695 | case IPV6_PREFER_SRC_PUBTMP_DEFAULT: |
| 696 | break; |
| 697 | case 0: |
| 698 | goto pref_skip_pubtmp; |
| 699 | default: |
| 700 | goto e_inval; |
| 701 | } |
| 702 | |
| 703 | prefmask &= ~(IPV6_PREFER_SRC_PUBLIC| |
| 704 | IPV6_PREFER_SRC_TMP); |
| 705 | pref_skip_pubtmp: |
| 706 | |
| 707 | /* check HOME/COA conflicts */ |
| 708 | switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) { |
| 709 | case IPV6_PREFER_SRC_HOME: |
| 710 | break; |
| 711 | case IPV6_PREFER_SRC_COA: |
| 712 | pref |= IPV6_PREFER_SRC_COA; |
| 713 | case 0: |
| 714 | goto pref_skip_coa; |
| 715 | default: |
| 716 | goto e_inval; |
| 717 | } |
| 718 | |
| 719 | prefmask &= ~IPV6_PREFER_SRC_COA; |
| 720 | pref_skip_coa: |
| 721 | |
| 722 | /* check CGA/NONCGA conflicts */ |
| 723 | switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) { |
| 724 | case IPV6_PREFER_SRC_CGA: |
| 725 | case IPV6_PREFER_SRC_NONCGA: |
| 726 | case 0: |
| 727 | break; |
| 728 | default: |
| 729 | goto e_inval; |
| 730 | } |
| 731 | |
| 732 | np->srcprefs = (np->srcprefs & prefmask) | pref; |
| 733 | retv = 0; |
| 734 | |
| 735 | break; |
| 736 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | } |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 738 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | release_sock(sk); |
| 740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | return retv; |
| 742 | |
| 743 | e_inval: |
| 744 | release_sock(sk); |
| 745 | return -EINVAL; |
| 746 | } |
| 747 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 748 | int ipv6_setsockopt(struct sock *sk, int level, int optname, |
| 749 | char __user *optval, int optlen) |
| 750 | { |
| 751 | int err; |
| 752 | |
| 753 | if (level == SOL_IP && sk->sk_type != SOCK_RAW) |
| 754 | return udp_prot.setsockopt(sk, level, optname, optval, optlen); |
| 755 | |
| 756 | if (level != SOL_IPV6) |
| 757 | return -ENOPROTOOPT; |
| 758 | |
| 759 | err = do_ipv6_setsockopt(sk, level, optname, optval, optlen); |
| 760 | #ifdef CONFIG_NETFILTER |
| 761 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 762 | if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY && |
| 763 | optname != IPV6_XFRM_POLICY) { |
| 764 | lock_sock(sk); |
| 765 | err = nf_setsockopt(sk, PF_INET6, optname, optval, |
| 766 | optlen); |
| 767 | release_sock(sk); |
| 768 | } |
| 769 | #endif |
| 770 | return err; |
| 771 | } |
| 772 | |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 773 | EXPORT_SYMBOL(ipv6_setsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 774 | |
| 775 | #ifdef CONFIG_COMPAT |
| 776 | int compat_ipv6_setsockopt(struct sock *sk, int level, int optname, |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 777 | char __user *optval, int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 778 | { |
| 779 | int err; |
| 780 | |
| 781 | if (level == SOL_IP && sk->sk_type != SOCK_RAW) { |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 782 | if (udp_prot.compat_setsockopt != NULL) |
| 783 | return udp_prot.compat_setsockopt(sk, level, optname, |
| 784 | optval, optlen); |
| 785 | return udp_prot.setsockopt(sk, level, optname, optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | if (level != SOL_IPV6) |
| 789 | return -ENOPROTOOPT; |
| 790 | |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 791 | if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER) |
| 792 | return compat_mc_setsockopt(sk, level, optname, optval, optlen, |
| 793 | ipv6_setsockopt); |
| 794 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 795 | err = do_ipv6_setsockopt(sk, level, optname, optval, optlen); |
| 796 | #ifdef CONFIG_NETFILTER |
| 797 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 798 | if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY && |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 799 | optname != IPV6_XFRM_POLICY) { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 800 | lock_sock(sk); |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 801 | err = compat_nf_setsockopt(sk, PF_INET6, optname, |
| 802 | optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 803 | release_sock(sk); |
| 804 | } |
| 805 | #endif |
| 806 | return err; |
| 807 | } |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 808 | |
| 809 | EXPORT_SYMBOL(compat_ipv6_setsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 810 | #endif |
| 811 | |
David S. Miller | 28693079 | 2007-03-07 12:50:46 -0800 | [diff] [blame] | 812 | static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt, |
YOSHIFUJI Hideaki | 4c6510a | 2007-03-18 17:35:57 -0700 | [diff] [blame] | 813 | int optname, char __user *optval, int len) |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 814 | { |
David S. Miller | 28693079 | 2007-03-07 12:50:46 -0800 | [diff] [blame] | 815 | struct ipv6_opt_hdr *hdr; |
| 816 | |
YOSHIFUJI Hideaki | 4c6510a | 2007-03-18 17:35:57 -0700 | [diff] [blame] | 817 | if (!opt) |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 818 | return 0; |
YOSHIFUJI Hideaki | 4c6510a | 2007-03-18 17:35:57 -0700 | [diff] [blame] | 819 | |
| 820 | switch(optname) { |
| 821 | case IPV6_HOPOPTS: |
| 822 | hdr = opt->hopopt; |
| 823 | break; |
| 824 | case IPV6_RTHDRDSTOPTS: |
| 825 | hdr = opt->dst0opt; |
| 826 | break; |
| 827 | case IPV6_RTHDR: |
| 828 | hdr = (struct ipv6_opt_hdr *)opt->srcrt; |
| 829 | break; |
| 830 | case IPV6_DSTOPTS: |
| 831 | hdr = opt->dst1opt; |
| 832 | break; |
| 833 | default: |
| 834 | return -EINVAL; /* should not happen */ |
| 835 | } |
| 836 | |
| 837 | if (!hdr) |
| 838 | return 0; |
David S. Miller | 28693079 | 2007-03-07 12:50:46 -0800 | [diff] [blame] | 839 | |
Chris Wright | d2b02ed | 2007-03-09 16:19:17 -0800 | [diff] [blame] | 840 | len = min_t(unsigned int, len, ipv6_optlen(hdr)); |
Ilpo Järvinen | 660adc6 | 2007-08-15 15:07:30 -0700 | [diff] [blame] | 841 | if (copy_to_user(optval, hdr, len)) |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 842 | return -EFAULT; |
Yang Hongyang | 95b496b | 2008-05-28 16:27:28 +0800 | [diff] [blame] | 843 | return len; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 844 | } |
| 845 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 846 | static int do_ipv6_getsockopt(struct sock *sk, int level, int optname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | char __user *optval, int __user *optlen) |
| 848 | { |
| 849 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 850 | int len; |
| 851 | int val; |
| 852 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 853 | if (ip6_mroute_opt(optname)) |
| 854 | return ip6_mroute_getsockopt(sk, optname, optval, optlen); |
| 855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | if (get_user(len, optlen)) |
| 857 | return -EFAULT; |
| 858 | switch (optname) { |
| 859 | case IPV6_ADDRFORM: |
| 860 | if (sk->sk_protocol != IPPROTO_UDP && |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 861 | sk->sk_protocol != IPPROTO_UDPLITE && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | sk->sk_protocol != IPPROTO_TCP) |
| 863 | return -EINVAL; |
| 864 | if (sk->sk_state != TCP_ESTABLISHED) |
| 865 | return -ENOTCONN; |
| 866 | val = sk->sk_family; |
| 867 | break; |
| 868 | case MCAST_MSFILTER: |
| 869 | { |
| 870 | struct group_filter gsf; |
| 871 | int err; |
| 872 | |
| 873 | if (len < GROUP_FILTER_SIZE(0)) |
| 874 | return -EINVAL; |
| 875 | if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) |
| 876 | return -EFAULT; |
YOSHIFUJI Hideaki | 20c61fb | 2008-04-28 14:40:55 +0900 | [diff] [blame^] | 877 | if (gsf.gf_group.ss_family != AF_INET6) |
| 878 | return -EADDRNOTAVAIL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | lock_sock(sk); |
| 880 | err = ip6_mc_msfget(sk, &gsf, |
| 881 | (struct group_filter __user *)optval, optlen); |
| 882 | release_sock(sk); |
| 883 | return err; |
| 884 | } |
| 885 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 886 | case IPV6_2292PKTOPTIONS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | { |
| 888 | struct msghdr msg; |
| 889 | struct sk_buff *skb; |
| 890 | |
| 891 | if (sk->sk_type != SOCK_STREAM) |
| 892 | return -ENOPROTOOPT; |
| 893 | |
| 894 | msg.msg_control = optval; |
| 895 | msg.msg_controllen = len; |
| 896 | msg.msg_flags = 0; |
| 897 | |
| 898 | lock_sock(sk); |
| 899 | skb = np->pktoptions; |
| 900 | if (skb) |
| 901 | atomic_inc(&skb->users); |
| 902 | release_sock(sk); |
| 903 | |
| 904 | if (skb) { |
| 905 | int err = datagram_recv_ctl(sk, &msg, skb); |
| 906 | kfree_skb(skb); |
| 907 | if (err) |
| 908 | return err; |
| 909 | } else { |
| 910 | if (np->rxopt.bits.rxinfo) { |
| 911 | struct in6_pktinfo src_info; |
| 912 | src_info.ipi6_ifindex = np->mcast_oif; |
| 913 | ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr); |
| 914 | put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info); |
| 915 | } |
| 916 | if (np->rxopt.bits.rxhlim) { |
| 917 | int hlim = np->mcast_hops; |
| 918 | put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim); |
| 919 | } |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 920 | if (np->rxopt.bits.rxoinfo) { |
| 921 | struct in6_pktinfo src_info; |
| 922 | src_info.ipi6_ifindex = np->mcast_oif; |
| 923 | ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr); |
| 924 | put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info); |
| 925 | } |
| 926 | if (np->rxopt.bits.rxohlim) { |
| 927 | int hlim = np->mcast_hops; |
| 928 | put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim); |
| 929 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | } |
| 931 | len -= msg.msg_controllen; |
| 932 | return put_user(len, optlen); |
| 933 | } |
| 934 | case IPV6_MTU: |
| 935 | { |
| 936 | struct dst_entry *dst; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 937 | val = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | lock_sock(sk); |
| 939 | dst = sk_dst_get(sk); |
| 940 | if (dst) { |
| 941 | val = dst_mtu(dst); |
| 942 | dst_release(dst); |
| 943 | } |
| 944 | release_sock(sk); |
| 945 | if (!val) |
| 946 | return -ENOTCONN; |
| 947 | break; |
| 948 | } |
| 949 | |
| 950 | case IPV6_V6ONLY: |
| 951 | val = np->ipv6only; |
| 952 | break; |
| 953 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 954 | case IPV6_RECVPKTINFO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | val = np->rxopt.bits.rxinfo; |
| 956 | break; |
| 957 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 958 | case IPV6_2292PKTINFO: |
| 959 | val = np->rxopt.bits.rxoinfo; |
| 960 | break; |
| 961 | |
| 962 | case IPV6_RECVHOPLIMIT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | val = np->rxopt.bits.rxhlim; |
| 964 | break; |
| 965 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 966 | case IPV6_2292HOPLIMIT: |
| 967 | val = np->rxopt.bits.rxohlim; |
| 968 | break; |
| 969 | |
| 970 | case IPV6_RECVRTHDR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | val = np->rxopt.bits.srcrt; |
| 972 | break; |
| 973 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 974 | case IPV6_2292RTHDR: |
| 975 | val = np->rxopt.bits.osrcrt; |
| 976 | break; |
| 977 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | case IPV6_HOPOPTS: |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 979 | case IPV6_RTHDRDSTOPTS: |
| 980 | case IPV6_RTHDR: |
| 981 | case IPV6_DSTOPTS: |
| 982 | { |
| 983 | |
| 984 | lock_sock(sk); |
David S. Miller | 28693079 | 2007-03-07 12:50:46 -0800 | [diff] [blame] | 985 | len = ipv6_getsockopt_sticky(sk, np->opt, |
YOSHIFUJI Hideaki | 4c6510a | 2007-03-18 17:35:57 -0700 | [diff] [blame] | 986 | optname, optval, len); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 987 | release_sock(sk); |
Yang Hongyang | 05335c2 | 2008-05-28 16:23:47 +0800 | [diff] [blame] | 988 | /* check if ipv6_getsockopt_sticky() returns err code */ |
| 989 | if (len < 0) |
| 990 | return len; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 991 | return put_user(len, optlen); |
| 992 | } |
| 993 | |
| 994 | case IPV6_RECVHOPOPTS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | val = np->rxopt.bits.hopopts; |
| 996 | break; |
| 997 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 998 | case IPV6_2292HOPOPTS: |
| 999 | val = np->rxopt.bits.ohopopts; |
| 1000 | break; |
| 1001 | |
| 1002 | case IPV6_RECVDSTOPTS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | val = np->rxopt.bits.dstopts; |
| 1004 | break; |
| 1005 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 1006 | case IPV6_2292DSTOPTS: |
| 1007 | val = np->rxopt.bits.odstopts; |
| 1008 | break; |
| 1009 | |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 1010 | case IPV6_TCLASS: |
| 1011 | val = np->tclass; |
Remi Denis-Courmont | d0ee011 | 2006-09-13 20:08:07 -0700 | [diff] [blame] | 1012 | if (val < 0) |
| 1013 | val = 0; |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 1014 | break; |
| 1015 | |
| 1016 | case IPV6_RECVTCLASS: |
| 1017 | val = np->rxopt.bits.rxtclass; |
| 1018 | break; |
| 1019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | case IPV6_FLOWINFO: |
| 1021 | val = np->rxopt.bits.rxflow; |
| 1022 | break; |
| 1023 | |
| 1024 | case IPV6_UNICAST_HOPS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | case IPV6_MULTICAST_HOPS: |
Brian Haley | befffe9 | 2006-12-12 17:09:49 -0800 | [diff] [blame] | 1026 | { |
| 1027 | struct dst_entry *dst; |
| 1028 | |
| 1029 | if (optname == IPV6_UNICAST_HOPS) |
| 1030 | val = np->hop_limit; |
| 1031 | else |
| 1032 | val = np->mcast_hops; |
| 1033 | |
| 1034 | dst = sk_dst_get(sk); |
| 1035 | if (dst) { |
| 1036 | if (val < 0) |
YOSHIFUJI Hideaki | 6b75d09 | 2008-03-10 06:00:30 -0400 | [diff] [blame] | 1037 | val = ip6_dst_hoplimit(dst); |
Brian Haley | befffe9 | 2006-12-12 17:09:49 -0800 | [diff] [blame] | 1038 | dst_release(dst); |
| 1039 | } |
| 1040 | if (val < 0) |
| 1041 | val = ipv6_devconf.hop_limit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | break; |
Brian Haley | befffe9 | 2006-12-12 17:09:49 -0800 | [diff] [blame] | 1043 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | |
| 1045 | case IPV6_MULTICAST_LOOP: |
| 1046 | val = np->mc_loop; |
| 1047 | break; |
| 1048 | |
| 1049 | case IPV6_MULTICAST_IF: |
| 1050 | val = np->mcast_oif; |
| 1051 | break; |
| 1052 | |
| 1053 | case IPV6_MTU_DISCOVER: |
| 1054 | val = np->pmtudisc; |
| 1055 | break; |
| 1056 | |
| 1057 | case IPV6_RECVERR: |
| 1058 | val = np->recverr; |
| 1059 | break; |
| 1060 | |
| 1061 | case IPV6_FLOWINFO_SEND: |
| 1062 | val = np->sndflow; |
| 1063 | break; |
| 1064 | |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1065 | case IPV6_ADDR_PREFERENCES: |
| 1066 | val = 0; |
| 1067 | |
| 1068 | if (np->srcprefs & IPV6_PREFER_SRC_TMP) |
| 1069 | val |= IPV6_PREFER_SRC_TMP; |
| 1070 | else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC) |
| 1071 | val |= IPV6_PREFER_SRC_PUBLIC; |
| 1072 | else { |
| 1073 | /* XXX: should we return system default? */ |
| 1074 | val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT; |
| 1075 | } |
| 1076 | |
| 1077 | if (np->srcprefs & IPV6_PREFER_SRC_COA) |
| 1078 | val |= IPV6_PREFER_SRC_COA; |
| 1079 | else |
| 1080 | val |= IPV6_PREFER_SRC_HOME; |
| 1081 | break; |
| 1082 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | default: |
Wei Yongjun | cf6fc4a | 2007-12-16 13:39:57 -0800 | [diff] [blame] | 1084 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | } |
| 1086 | len = min_t(unsigned int, sizeof(int), len); |
| 1087 | if(put_user(len, optlen)) |
| 1088 | return -EFAULT; |
| 1089 | if(copy_to_user(optval,&val,len)) |
| 1090 | return -EFAULT; |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1094 | int ipv6_getsockopt(struct sock *sk, int level, int optname, |
| 1095 | char __user *optval, int __user *optlen) |
| 1096 | { |
| 1097 | int err; |
| 1098 | |
| 1099 | if (level == SOL_IP && sk->sk_type != SOCK_RAW) |
| 1100 | return udp_prot.getsockopt(sk, level, optname, optval, optlen); |
| 1101 | |
| 1102 | if(level != SOL_IPV6) |
| 1103 | return -ENOPROTOOPT; |
| 1104 | |
| 1105 | err = do_ipv6_getsockopt(sk, level, optname, optval, optlen); |
| 1106 | #ifdef CONFIG_NETFILTER |
Wei Yongjun | cf6fc4a | 2007-12-16 13:39:57 -0800 | [diff] [blame] | 1107 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 1108 | if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1109 | int len; |
| 1110 | |
| 1111 | if (get_user(len, optlen)) |
| 1112 | return -EFAULT; |
| 1113 | |
| 1114 | lock_sock(sk); |
| 1115 | err = nf_getsockopt(sk, PF_INET6, optname, optval, |
| 1116 | &len); |
| 1117 | release_sock(sk); |
| 1118 | if (err >= 0) |
| 1119 | err = put_user(len, optlen); |
| 1120 | } |
| 1121 | #endif |
| 1122 | return err; |
| 1123 | } |
| 1124 | |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 1125 | EXPORT_SYMBOL(ipv6_getsockopt); |
| 1126 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1127 | #ifdef CONFIG_COMPAT |
| 1128 | 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] | 1129 | char __user *optval, int __user *optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1130 | { |
| 1131 | int err; |
| 1132 | |
| 1133 | if (level == SOL_IP && sk->sk_type != SOCK_RAW) { |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1134 | if (udp_prot.compat_getsockopt != NULL) |
| 1135 | return udp_prot.compat_getsockopt(sk, level, optname, |
| 1136 | optval, optlen); |
| 1137 | return udp_prot.getsockopt(sk, level, optname, optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1140 | if (level != SOL_IPV6) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1141 | return -ENOPROTOOPT; |
| 1142 | |
Pavel Emelyanov | 8099179 | 2008-04-30 14:49:54 -0700 | [diff] [blame] | 1143 | if (optname == MCAST_MSFILTER) |
| 1144 | return compat_mc_getsockopt(sk, level, optname, optval, optlen, |
| 1145 | ipv6_getsockopt); |
| 1146 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1147 | err = do_ipv6_getsockopt(sk, level, optname, optval, optlen); |
| 1148 | #ifdef CONFIG_NETFILTER |
Wei Yongjun | cf6fc4a | 2007-12-16 13:39:57 -0800 | [diff] [blame] | 1149 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 1150 | if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1151 | int len; |
| 1152 | |
| 1153 | if (get_user(len, optlen)) |
| 1154 | return -EFAULT; |
| 1155 | |
| 1156 | lock_sock(sk); |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1157 | err = compat_nf_getsockopt(sk, PF_INET6, |
| 1158 | optname, optval, &len); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1159 | release_sock(sk); |
| 1160 | if (err >= 0) |
| 1161 | err = put_user(len, optlen); |
| 1162 | } |
| 1163 | #endif |
| 1164 | return err; |
| 1165 | } |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1166 | |
| 1167 | EXPORT_SYMBOL(compat_ipv6_getsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1168 | #endif |
| 1169 | |