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