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