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