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