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