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