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