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