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