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> |
| 29 | #include <linux/config.h> |
| 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 | |
| 60 | static struct packet_type ipv6_packet_type = { |
| 61 | .type = __constant_htons(ETH_P_IPV6), |
| 62 | .func = ipv6_rcv, |
| 63 | }; |
| 64 | |
| 65 | struct ip6_ra_chain *ip6_ra_chain; |
| 66 | DEFINE_RWLOCK(ip6_ra_lock); |
| 67 | |
| 68 | int ip6_ra_control(struct sock *sk, int sel, void (*destructor)(struct sock *)) |
| 69 | { |
| 70 | struct ip6_ra_chain *ra, *new_ra, **rap; |
| 71 | |
| 72 | /* RA packet may be delivered ONLY to IPPROTO_RAW socket */ |
| 73 | if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num != IPPROTO_RAW) |
| 74 | return -EINVAL; |
| 75 | |
| 76 | new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL; |
| 77 | |
| 78 | write_lock_bh(&ip6_ra_lock); |
| 79 | for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) { |
| 80 | if (ra->sk == sk) { |
| 81 | if (sel>=0) { |
| 82 | write_unlock_bh(&ip6_ra_lock); |
| 83 | if (new_ra) |
| 84 | kfree(new_ra); |
| 85 | return -EADDRINUSE; |
| 86 | } |
| 87 | |
| 88 | *rap = ra->next; |
| 89 | write_unlock_bh(&ip6_ra_lock); |
| 90 | |
| 91 | if (ra->destructor) |
| 92 | ra->destructor(sk); |
| 93 | sock_put(sk); |
| 94 | kfree(ra); |
| 95 | return 0; |
| 96 | } |
| 97 | } |
| 98 | if (new_ra == NULL) { |
| 99 | write_unlock_bh(&ip6_ra_lock); |
| 100 | return -ENOBUFS; |
| 101 | } |
| 102 | new_ra->sk = sk; |
| 103 | new_ra->sel = sel; |
| 104 | new_ra->destructor = destructor; |
| 105 | new_ra->next = ra; |
| 106 | *rap = new_ra; |
| 107 | sock_hold(sk); |
| 108 | write_unlock_bh(&ip6_ra_lock); |
| 109 | return 0; |
| 110 | } |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | int ipv6_setsockopt(struct sock *sk, int level, int optname, |
| 113 | char __user *optval, int optlen) |
| 114 | { |
| 115 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 116 | int val, valbool; |
| 117 | int retv = -ENOPROTOOPT; |
| 118 | |
| 119 | if (level == SOL_IP && sk->sk_type != SOCK_RAW) |
| 120 | return udp_prot.setsockopt(sk, level, optname, optval, optlen); |
| 121 | |
| 122 | if(level!=SOL_IPV6) |
| 123 | goto out; |
| 124 | |
| 125 | if (optval == NULL) |
| 126 | val=0; |
| 127 | else if (get_user(val, (int __user *) optval)) |
| 128 | return -EFAULT; |
| 129 | |
| 130 | valbool = (val!=0); |
| 131 | |
| 132 | lock_sock(sk); |
| 133 | |
| 134 | switch (optname) { |
| 135 | |
| 136 | case IPV6_ADDRFORM: |
| 137 | if (val == PF_INET) { |
| 138 | struct ipv6_txoptions *opt; |
| 139 | struct sk_buff *pktopt; |
| 140 | |
| 141 | if (sk->sk_protocol != IPPROTO_UDP && |
| 142 | sk->sk_protocol != IPPROTO_TCP) |
| 143 | break; |
| 144 | |
| 145 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 146 | retv = -ENOTCONN; |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | if (ipv6_only_sock(sk) || |
| 151 | !(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) { |
| 152 | retv = -EADDRNOTAVAIL; |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | fl6_free_socklist(sk); |
| 157 | ipv6_sock_mc_close(sk); |
| 158 | |
Arnaldo Carvalho de Melo | e684897 | 2005-08-09 19:45:38 -0700 | [diff] [blame] | 159 | /* |
| 160 | * Sock is moving from IPv6 to IPv4 (sk_prot), so |
| 161 | * remove it from the refcnt debug socks count in the |
| 162 | * original family... |
| 163 | */ |
| 164 | sk_refcnt_debug_dec(sk); |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | if (sk->sk_protocol == IPPROTO_TCP) { |
| 167 | struct tcp_sock *tp = tcp_sk(sk); |
| 168 | |
| 169 | local_bh_disable(); |
| 170 | sock_prot_dec_use(sk->sk_prot); |
| 171 | sock_prot_inc_use(&tcp_prot); |
| 172 | local_bh_enable(); |
| 173 | sk->sk_prot = &tcp_prot; |
| 174 | tp->af_specific = &ipv4_specific; |
| 175 | sk->sk_socket->ops = &inet_stream_ops; |
| 176 | sk->sk_family = PF_INET; |
| 177 | tcp_sync_mss(sk, tp->pmtu_cookie); |
| 178 | } else { |
| 179 | local_bh_disable(); |
| 180 | sock_prot_dec_use(sk->sk_prot); |
| 181 | sock_prot_inc_use(&udp_prot); |
| 182 | local_bh_enable(); |
| 183 | sk->sk_prot = &udp_prot; |
| 184 | sk->sk_socket->ops = &inet_dgram_ops; |
| 185 | sk->sk_family = PF_INET; |
| 186 | } |
| 187 | opt = xchg(&np->opt, NULL); |
| 188 | if (opt) |
| 189 | sock_kfree_s(sk, opt, opt->tot_len); |
| 190 | pktopt = xchg(&np->pktoptions, NULL); |
| 191 | if (pktopt) |
| 192 | kfree_skb(pktopt); |
| 193 | |
| 194 | sk->sk_destruct = inet_sock_destruct; |
Arnaldo Carvalho de Melo | e684897 | 2005-08-09 19:45:38 -0700 | [diff] [blame] | 195 | /* |
| 196 | * ... and add it to the refcnt debug socks count |
| 197 | * in the new family. -acme |
| 198 | */ |
| 199 | sk_refcnt_debug_inc(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | module_put(THIS_MODULE); |
| 201 | retv = 0; |
| 202 | break; |
| 203 | } |
| 204 | goto e_inval; |
| 205 | |
| 206 | case IPV6_V6ONLY: |
| 207 | if (inet_sk(sk)->num) |
| 208 | goto e_inval; |
| 209 | np->ipv6only = valbool; |
| 210 | retv = 0; |
| 211 | break; |
| 212 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 213 | case IPV6_RECVPKTINFO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | np->rxopt.bits.rxinfo = valbool; |
| 215 | retv = 0; |
| 216 | break; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 217 | |
| 218 | case IPV6_2292PKTINFO: |
| 219 | np->rxopt.bits.rxoinfo = valbool; |
| 220 | retv = 0; |
| 221 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 223 | case IPV6_RECVHOPLIMIT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | np->rxopt.bits.rxhlim = valbool; |
| 225 | retv = 0; |
| 226 | break; |
| 227 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 228 | case IPV6_2292HOPLIMIT: |
| 229 | np->rxopt.bits.rxohlim = valbool; |
| 230 | retv = 0; |
| 231 | break; |
| 232 | |
| 233 | case IPV6_RECVRTHDR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | if (val < 0 || val > 2) |
| 235 | goto e_inval; |
| 236 | np->rxopt.bits.srcrt = val; |
| 237 | retv = 0; |
| 238 | break; |
| 239 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 240 | case IPV6_2292RTHDR: |
| 241 | if (val < 0 || val > 2) |
| 242 | goto e_inval; |
| 243 | np->rxopt.bits.osrcrt = val; |
| 244 | retv = 0; |
| 245 | break; |
| 246 | |
| 247 | case IPV6_RECVHOPOPTS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | np->rxopt.bits.hopopts = valbool; |
| 249 | retv = 0; |
| 250 | break; |
| 251 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 252 | case IPV6_2292HOPOPTS: |
| 253 | np->rxopt.bits.ohopopts = valbool; |
| 254 | retv = 0; |
| 255 | break; |
| 256 | |
| 257 | case IPV6_RECVDSTOPTS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | np->rxopt.bits.dstopts = valbool; |
| 259 | retv = 0; |
| 260 | break; |
| 261 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 262 | case IPV6_2292DSTOPTS: |
| 263 | np->rxopt.bits.odstopts = valbool; |
| 264 | retv = 0; |
| 265 | break; |
| 266 | |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame^] | 267 | case IPV6_TCLASS: |
| 268 | if (val < 0 || val > 0xff) |
| 269 | goto e_inval; |
| 270 | np->tclass = val; |
| 271 | retv = 0; |
| 272 | break; |
| 273 | |
| 274 | case IPV6_RECVTCLASS: |
| 275 | np->rxopt.bits.rxtclass = valbool; |
| 276 | retv = 0; |
| 277 | break; |
| 278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | case IPV6_FLOWINFO: |
| 280 | np->rxopt.bits.rxflow = valbool; |
| 281 | retv = 0; |
| 282 | break; |
| 283 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 284 | case IPV6_HOPOPTS: |
| 285 | case IPV6_RTHDRDSTOPTS: |
| 286 | case IPV6_RTHDR: |
| 287 | case IPV6_DSTOPTS: |
| 288 | { |
| 289 | struct ipv6_txoptions *opt; |
| 290 | if (optlen == 0) |
| 291 | optval = 0; |
| 292 | |
| 293 | /* hop-by-hop / destination options are privileged option */ |
| 294 | retv = -EPERM; |
| 295 | if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW)) |
| 296 | break; |
| 297 | |
| 298 | retv = -EINVAL; |
| 299 | if (optlen & 0x7 || optlen > 8 * 255) |
| 300 | break; |
| 301 | |
| 302 | opt = ipv6_renew_options(sk, np->opt, optname, |
| 303 | (struct ipv6_opt_hdr __user *)optval, |
| 304 | optlen); |
| 305 | if (IS_ERR(opt)) { |
| 306 | retv = PTR_ERR(opt); |
| 307 | break; |
| 308 | } |
| 309 | |
| 310 | /* routing header option needs extra check */ |
| 311 | if (optname == IPV6_RTHDR && opt->srcrt) { |
| 312 | struct ipv6_rt_hdr *rthdr = opt->srcrt; |
| 313 | if (rthdr->type) |
| 314 | goto sticky_done; |
| 315 | if ((rthdr->hdrlen & 1) || |
| 316 | (rthdr->hdrlen >> 1) != rthdr->segments_left) |
| 317 | goto sticky_done; |
| 318 | } |
| 319 | |
| 320 | retv = 0; |
| 321 | if (sk->sk_type == SOCK_STREAM) { |
| 322 | if (opt) { |
| 323 | struct tcp_sock *tp = tcp_sk(sk); |
| 324 | if (!((1 << sk->sk_state) & |
| 325 | (TCPF_LISTEN | TCPF_CLOSE)) |
| 326 | && inet_sk(sk)->daddr != LOOPBACK4_IPV6) { |
| 327 | tp->ext_header_len = opt->opt_flen + opt->opt_nflen; |
| 328 | tcp_sync_mss(sk, tp->pmtu_cookie); |
| 329 | } |
| 330 | } |
| 331 | opt = xchg(&np->opt, opt); |
| 332 | sk_dst_reset(sk); |
| 333 | } else { |
| 334 | write_lock(&sk->sk_dst_lock); |
| 335 | opt = xchg(&np->opt, opt); |
| 336 | write_unlock(&sk->sk_dst_lock); |
| 337 | sk_dst_reset(sk); |
| 338 | } |
| 339 | sticky_done: |
| 340 | if (opt) |
| 341 | sock_kfree_s(sk, opt, opt->tot_len); |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | case IPV6_2292PKTOPTIONS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
| 347 | struct ipv6_txoptions *opt = NULL; |
| 348 | struct msghdr msg; |
| 349 | struct flowi fl; |
| 350 | int junk; |
| 351 | |
| 352 | fl.fl6_flowlabel = 0; |
| 353 | fl.oif = sk->sk_bound_dev_if; |
| 354 | |
| 355 | if (optlen == 0) |
| 356 | goto update; |
| 357 | |
| 358 | /* 1K is probably excessive |
| 359 | * 1K is surely not enough, 2K per standard header is 16K. |
| 360 | */ |
| 361 | retv = -EINVAL; |
| 362 | if (optlen > 64*1024) |
| 363 | break; |
| 364 | |
| 365 | opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL); |
| 366 | retv = -ENOBUFS; |
| 367 | if (opt == NULL) |
| 368 | break; |
| 369 | |
| 370 | memset(opt, 0, sizeof(*opt)); |
| 371 | opt->tot_len = sizeof(*opt) + optlen; |
| 372 | retv = -EFAULT; |
| 373 | if (copy_from_user(opt+1, optval, optlen)) |
| 374 | goto done; |
| 375 | |
| 376 | msg.msg_controllen = optlen; |
| 377 | msg.msg_control = (void*)(opt+1); |
| 378 | |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame^] | 379 | retv = datagram_send_ctl(&msg, &fl, opt, &junk, &junk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | if (retv) |
| 381 | goto done; |
| 382 | update: |
| 383 | retv = 0; |
| 384 | if (sk->sk_type == SOCK_STREAM) { |
| 385 | if (opt) { |
| 386 | struct tcp_sock *tp = tcp_sk(sk); |
| 387 | if (!((1 << sk->sk_state) & |
| 388 | (TCPF_LISTEN | TCPF_CLOSE)) |
| 389 | && inet_sk(sk)->daddr != LOOPBACK4_IPV6) { |
| 390 | tp->ext_header_len = opt->opt_flen + opt->opt_nflen; |
| 391 | tcp_sync_mss(sk, tp->pmtu_cookie); |
| 392 | } |
| 393 | } |
| 394 | opt = xchg(&np->opt, opt); |
| 395 | sk_dst_reset(sk); |
| 396 | } else { |
| 397 | write_lock(&sk->sk_dst_lock); |
| 398 | opt = xchg(&np->opt, opt); |
| 399 | write_unlock(&sk->sk_dst_lock); |
| 400 | sk_dst_reset(sk); |
| 401 | } |
| 402 | |
| 403 | done: |
| 404 | if (opt) |
| 405 | sock_kfree_s(sk, opt, opt->tot_len); |
| 406 | break; |
| 407 | } |
| 408 | case IPV6_UNICAST_HOPS: |
| 409 | if (val > 255 || val < -1) |
| 410 | goto e_inval; |
| 411 | np->hop_limit = val; |
| 412 | retv = 0; |
| 413 | break; |
| 414 | |
| 415 | case IPV6_MULTICAST_HOPS: |
| 416 | if (sk->sk_type == SOCK_STREAM) |
| 417 | goto e_inval; |
| 418 | if (val > 255 || val < -1) |
| 419 | goto e_inval; |
| 420 | np->mcast_hops = val; |
| 421 | retv = 0; |
| 422 | break; |
| 423 | |
| 424 | case IPV6_MULTICAST_LOOP: |
| 425 | np->mc_loop = valbool; |
| 426 | retv = 0; |
| 427 | break; |
| 428 | |
| 429 | case IPV6_MULTICAST_IF: |
| 430 | if (sk->sk_type == SOCK_STREAM) |
| 431 | goto e_inval; |
| 432 | if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val) |
| 433 | goto e_inval; |
| 434 | |
| 435 | if (__dev_get_by_index(val) == NULL) { |
| 436 | retv = -ENODEV; |
| 437 | break; |
| 438 | } |
| 439 | np->mcast_oif = val; |
| 440 | retv = 0; |
| 441 | break; |
| 442 | case IPV6_ADD_MEMBERSHIP: |
| 443 | case IPV6_DROP_MEMBERSHIP: |
| 444 | { |
| 445 | struct ipv6_mreq mreq; |
| 446 | |
| 447 | retv = -EFAULT; |
| 448 | if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq))) |
| 449 | break; |
| 450 | |
| 451 | if (optname == IPV6_ADD_MEMBERSHIP) |
| 452 | retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr); |
| 453 | else |
| 454 | retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr); |
| 455 | break; |
| 456 | } |
| 457 | case IPV6_JOIN_ANYCAST: |
| 458 | case IPV6_LEAVE_ANYCAST: |
| 459 | { |
| 460 | struct ipv6_mreq mreq; |
| 461 | |
| 462 | if (optlen != sizeof(struct ipv6_mreq)) |
| 463 | goto e_inval; |
| 464 | |
| 465 | retv = -EFAULT; |
| 466 | if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq))) |
| 467 | break; |
| 468 | |
| 469 | if (optname == IPV6_JOIN_ANYCAST) |
| 470 | retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr); |
| 471 | else |
| 472 | retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr); |
| 473 | break; |
| 474 | } |
| 475 | case MCAST_JOIN_GROUP: |
| 476 | case MCAST_LEAVE_GROUP: |
| 477 | { |
| 478 | struct group_req greq; |
| 479 | struct sockaddr_in6 *psin6; |
| 480 | |
| 481 | retv = -EFAULT; |
| 482 | if (copy_from_user(&greq, optval, sizeof(struct group_req))) |
| 483 | break; |
| 484 | if (greq.gr_group.ss_family != AF_INET6) { |
| 485 | retv = -EADDRNOTAVAIL; |
| 486 | break; |
| 487 | } |
| 488 | psin6 = (struct sockaddr_in6 *)&greq.gr_group; |
| 489 | if (optname == MCAST_JOIN_GROUP) |
| 490 | retv = ipv6_sock_mc_join(sk, greq.gr_interface, |
| 491 | &psin6->sin6_addr); |
| 492 | else |
| 493 | retv = ipv6_sock_mc_drop(sk, greq.gr_interface, |
| 494 | &psin6->sin6_addr); |
| 495 | break; |
| 496 | } |
| 497 | case MCAST_JOIN_SOURCE_GROUP: |
| 498 | case MCAST_LEAVE_SOURCE_GROUP: |
| 499 | case MCAST_BLOCK_SOURCE: |
| 500 | case MCAST_UNBLOCK_SOURCE: |
| 501 | { |
| 502 | struct group_source_req greqs; |
| 503 | int omode, add; |
| 504 | |
| 505 | if (optlen != sizeof(struct group_source_req)) |
| 506 | goto e_inval; |
| 507 | if (copy_from_user(&greqs, optval, sizeof(greqs))) { |
| 508 | retv = -EFAULT; |
| 509 | break; |
| 510 | } |
| 511 | if (greqs.gsr_group.ss_family != AF_INET6 || |
| 512 | greqs.gsr_source.ss_family != AF_INET6) { |
| 513 | retv = -EADDRNOTAVAIL; |
| 514 | break; |
| 515 | } |
| 516 | if (optname == MCAST_BLOCK_SOURCE) { |
| 517 | omode = MCAST_EXCLUDE; |
| 518 | add = 1; |
| 519 | } else if (optname == MCAST_UNBLOCK_SOURCE) { |
| 520 | omode = MCAST_EXCLUDE; |
| 521 | add = 0; |
| 522 | } else if (optname == MCAST_JOIN_SOURCE_GROUP) { |
| 523 | struct sockaddr_in6 *psin6; |
| 524 | |
| 525 | psin6 = (struct sockaddr_in6 *)&greqs.gsr_group; |
| 526 | retv = ipv6_sock_mc_join(sk, greqs.gsr_interface, |
| 527 | &psin6->sin6_addr); |
David L Stevens | c9e3e8b | 2005-06-21 13:58:25 -0700 | [diff] [blame] | 528 | /* prior join w/ different source is ok */ |
| 529 | if (retv && retv != -EADDRINUSE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | break; |
| 531 | omode = MCAST_INCLUDE; |
| 532 | add = 1; |
David L Stevens | c9e3e8b | 2005-06-21 13:58:25 -0700 | [diff] [blame] | 533 | } else /* MCAST_LEAVE_SOURCE_GROUP */ { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | omode = MCAST_INCLUDE; |
| 535 | add = 0; |
| 536 | } |
| 537 | retv = ip6_mc_source(add, omode, sk, &greqs); |
| 538 | break; |
| 539 | } |
| 540 | case MCAST_MSFILTER: |
| 541 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | extern int sysctl_mld_max_msf; |
| 543 | struct group_filter *gsf; |
| 544 | |
| 545 | if (optlen < GROUP_FILTER_SIZE(0)) |
| 546 | goto e_inval; |
| 547 | if (optlen > sysctl_optmem_max) { |
| 548 | retv = -ENOBUFS; |
| 549 | break; |
| 550 | } |
| 551 | gsf = (struct group_filter *)kmalloc(optlen,GFP_KERNEL); |
| 552 | if (gsf == 0) { |
| 553 | retv = -ENOBUFS; |
| 554 | break; |
| 555 | } |
| 556 | retv = -EFAULT; |
| 557 | if (copy_from_user(gsf, optval, optlen)) { |
| 558 | kfree(gsf); |
| 559 | break; |
| 560 | } |
| 561 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ |
| 562 | if (gsf->gf_numsrc >= 0x1ffffffU || |
| 563 | gsf->gf_numsrc > sysctl_mld_max_msf) { |
| 564 | kfree(gsf); |
| 565 | retv = -ENOBUFS; |
| 566 | break; |
| 567 | } |
| 568 | if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) { |
| 569 | kfree(gsf); |
| 570 | retv = -EINVAL; |
| 571 | break; |
| 572 | } |
| 573 | retv = ip6_mc_msfilter(sk, gsf); |
| 574 | kfree(gsf); |
| 575 | |
| 576 | break; |
| 577 | } |
| 578 | case IPV6_ROUTER_ALERT: |
| 579 | retv = ip6_ra_control(sk, val, NULL); |
| 580 | break; |
| 581 | case IPV6_MTU_DISCOVER: |
| 582 | if (val<0 || val>2) |
| 583 | goto e_inval; |
| 584 | np->pmtudisc = val; |
| 585 | retv = 0; |
| 586 | break; |
| 587 | case IPV6_MTU: |
| 588 | if (val && val < IPV6_MIN_MTU) |
| 589 | goto e_inval; |
| 590 | np->frag_size = val; |
| 591 | retv = 0; |
| 592 | break; |
| 593 | case IPV6_RECVERR: |
| 594 | np->recverr = valbool; |
| 595 | if (!val) |
| 596 | skb_queue_purge(&sk->sk_error_queue); |
| 597 | retv = 0; |
| 598 | break; |
| 599 | case IPV6_FLOWINFO_SEND: |
| 600 | np->sndflow = valbool; |
| 601 | retv = 0; |
| 602 | break; |
| 603 | case IPV6_FLOWLABEL_MGR: |
| 604 | retv = ipv6_flowlabel_opt(sk, optval, optlen); |
| 605 | break; |
| 606 | case IPV6_IPSEC_POLICY: |
| 607 | case IPV6_XFRM_POLICY: |
Herbert Xu | 6fc0b4a | 2005-08-06 06:33:15 -0700 | [diff] [blame] | 608 | retv = -EPERM; |
| 609 | if (!capable(CAP_NET_ADMIN)) |
| 610 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | retv = xfrm_user_policy(sk, optname, optval, optlen); |
| 612 | break; |
| 613 | |
| 614 | #ifdef CONFIG_NETFILTER |
| 615 | default: |
| 616 | retv = nf_setsockopt(sk, PF_INET6, optname, optval, |
| 617 | optlen); |
| 618 | break; |
| 619 | #endif |
| 620 | |
| 621 | } |
| 622 | release_sock(sk); |
| 623 | |
| 624 | out: |
| 625 | return retv; |
| 626 | |
| 627 | e_inval: |
| 628 | release_sock(sk); |
| 629 | return -EINVAL; |
| 630 | } |
| 631 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 632 | int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_opt_hdr *hdr, |
| 633 | char __user *optval, int len) |
| 634 | { |
| 635 | if (!hdr) |
| 636 | return 0; |
| 637 | len = min_t(int, len, ipv6_optlen(hdr)); |
| 638 | if (copy_to_user(optval, hdr, ipv6_optlen(hdr))) |
| 639 | return -EFAULT; |
| 640 | return len; |
| 641 | } |
| 642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | int ipv6_getsockopt(struct sock *sk, int level, int optname, |
| 644 | char __user *optval, int __user *optlen) |
| 645 | { |
| 646 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 647 | int len; |
| 648 | int val; |
| 649 | |
| 650 | if (level == SOL_IP && sk->sk_type != SOCK_RAW) |
| 651 | return udp_prot.getsockopt(sk, level, optname, optval, optlen); |
| 652 | if(level!=SOL_IPV6) |
| 653 | return -ENOPROTOOPT; |
| 654 | if (get_user(len, optlen)) |
| 655 | return -EFAULT; |
| 656 | switch (optname) { |
| 657 | case IPV6_ADDRFORM: |
| 658 | if (sk->sk_protocol != IPPROTO_UDP && |
| 659 | sk->sk_protocol != IPPROTO_TCP) |
| 660 | return -EINVAL; |
| 661 | if (sk->sk_state != TCP_ESTABLISHED) |
| 662 | return -ENOTCONN; |
| 663 | val = sk->sk_family; |
| 664 | break; |
| 665 | case MCAST_MSFILTER: |
| 666 | { |
| 667 | struct group_filter gsf; |
| 668 | int err; |
| 669 | |
| 670 | if (len < GROUP_FILTER_SIZE(0)) |
| 671 | return -EINVAL; |
| 672 | if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) |
| 673 | return -EFAULT; |
| 674 | lock_sock(sk); |
| 675 | err = ip6_mc_msfget(sk, &gsf, |
| 676 | (struct group_filter __user *)optval, optlen); |
| 677 | release_sock(sk); |
| 678 | return err; |
| 679 | } |
| 680 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 681 | case IPV6_2292PKTOPTIONS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | { |
| 683 | struct msghdr msg; |
| 684 | struct sk_buff *skb; |
| 685 | |
| 686 | if (sk->sk_type != SOCK_STREAM) |
| 687 | return -ENOPROTOOPT; |
| 688 | |
| 689 | msg.msg_control = optval; |
| 690 | msg.msg_controllen = len; |
| 691 | msg.msg_flags = 0; |
| 692 | |
| 693 | lock_sock(sk); |
| 694 | skb = np->pktoptions; |
| 695 | if (skb) |
| 696 | atomic_inc(&skb->users); |
| 697 | release_sock(sk); |
| 698 | |
| 699 | if (skb) { |
| 700 | int err = datagram_recv_ctl(sk, &msg, skb); |
| 701 | kfree_skb(skb); |
| 702 | if (err) |
| 703 | return err; |
| 704 | } else { |
| 705 | if (np->rxopt.bits.rxinfo) { |
| 706 | struct in6_pktinfo src_info; |
| 707 | src_info.ipi6_ifindex = np->mcast_oif; |
| 708 | ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr); |
| 709 | put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info); |
| 710 | } |
| 711 | if (np->rxopt.bits.rxhlim) { |
| 712 | int hlim = np->mcast_hops; |
| 713 | put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim); |
| 714 | } |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 715 | if (np->rxopt.bits.rxoinfo) { |
| 716 | struct in6_pktinfo src_info; |
| 717 | src_info.ipi6_ifindex = np->mcast_oif; |
| 718 | ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr); |
| 719 | put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info); |
| 720 | } |
| 721 | if (np->rxopt.bits.rxohlim) { |
| 722 | int hlim = np->mcast_hops; |
| 723 | put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim); |
| 724 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | } |
| 726 | len -= msg.msg_controllen; |
| 727 | return put_user(len, optlen); |
| 728 | } |
| 729 | case IPV6_MTU: |
| 730 | { |
| 731 | struct dst_entry *dst; |
| 732 | val = 0; |
| 733 | lock_sock(sk); |
| 734 | dst = sk_dst_get(sk); |
| 735 | if (dst) { |
| 736 | val = dst_mtu(dst); |
| 737 | dst_release(dst); |
| 738 | } |
| 739 | release_sock(sk); |
| 740 | if (!val) |
| 741 | return -ENOTCONN; |
| 742 | break; |
| 743 | } |
| 744 | |
| 745 | case IPV6_V6ONLY: |
| 746 | val = np->ipv6only; |
| 747 | break; |
| 748 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 749 | case IPV6_RECVPKTINFO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | val = np->rxopt.bits.rxinfo; |
| 751 | break; |
| 752 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 753 | case IPV6_2292PKTINFO: |
| 754 | val = np->rxopt.bits.rxoinfo; |
| 755 | break; |
| 756 | |
| 757 | case IPV6_RECVHOPLIMIT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | val = np->rxopt.bits.rxhlim; |
| 759 | break; |
| 760 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 761 | case IPV6_2292HOPLIMIT: |
| 762 | val = np->rxopt.bits.rxohlim; |
| 763 | break; |
| 764 | |
| 765 | case IPV6_RECVRTHDR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | val = np->rxopt.bits.srcrt; |
| 767 | break; |
| 768 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 769 | case IPV6_2292RTHDR: |
| 770 | val = np->rxopt.bits.osrcrt; |
| 771 | break; |
| 772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | case IPV6_HOPOPTS: |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 774 | case IPV6_RTHDRDSTOPTS: |
| 775 | case IPV6_RTHDR: |
| 776 | case IPV6_DSTOPTS: |
| 777 | { |
| 778 | |
| 779 | lock_sock(sk); |
| 780 | len = ipv6_getsockopt_sticky(sk, np->opt->hopopt, |
| 781 | optval, len); |
| 782 | release_sock(sk); |
| 783 | return put_user(len, optlen); |
| 784 | } |
| 785 | |
| 786 | case IPV6_RECVHOPOPTS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | val = np->rxopt.bits.hopopts; |
| 788 | break; |
| 789 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 790 | case IPV6_2292HOPOPTS: |
| 791 | val = np->rxopt.bits.ohopopts; |
| 792 | break; |
| 793 | |
| 794 | case IPV6_RECVDSTOPTS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | val = np->rxopt.bits.dstopts; |
| 796 | break; |
| 797 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 798 | case IPV6_2292DSTOPTS: |
| 799 | val = np->rxopt.bits.odstopts; |
| 800 | break; |
| 801 | |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame^] | 802 | case IPV6_TCLASS: |
| 803 | val = np->tclass; |
| 804 | break; |
| 805 | |
| 806 | case IPV6_RECVTCLASS: |
| 807 | val = np->rxopt.bits.rxtclass; |
| 808 | break; |
| 809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | case IPV6_FLOWINFO: |
| 811 | val = np->rxopt.bits.rxflow; |
| 812 | break; |
| 813 | |
| 814 | case IPV6_UNICAST_HOPS: |
| 815 | val = np->hop_limit; |
| 816 | break; |
| 817 | |
| 818 | case IPV6_MULTICAST_HOPS: |
| 819 | val = np->mcast_hops; |
| 820 | break; |
| 821 | |
| 822 | case IPV6_MULTICAST_LOOP: |
| 823 | val = np->mc_loop; |
| 824 | break; |
| 825 | |
| 826 | case IPV6_MULTICAST_IF: |
| 827 | val = np->mcast_oif; |
| 828 | break; |
| 829 | |
| 830 | case IPV6_MTU_DISCOVER: |
| 831 | val = np->pmtudisc; |
| 832 | break; |
| 833 | |
| 834 | case IPV6_RECVERR: |
| 835 | val = np->recverr; |
| 836 | break; |
| 837 | |
| 838 | case IPV6_FLOWINFO_SEND: |
| 839 | val = np->sndflow; |
| 840 | break; |
| 841 | |
| 842 | default: |
| 843 | #ifdef CONFIG_NETFILTER |
| 844 | lock_sock(sk); |
| 845 | val = nf_getsockopt(sk, PF_INET6, optname, optval, |
| 846 | &len); |
| 847 | release_sock(sk); |
| 848 | if (val >= 0) |
| 849 | val = put_user(len, optlen); |
| 850 | return val; |
| 851 | #else |
| 852 | return -EINVAL; |
| 853 | #endif |
| 854 | } |
| 855 | len = min_t(unsigned int, sizeof(int), len); |
| 856 | if(put_user(len, optlen)) |
| 857 | return -EFAULT; |
| 858 | if(copy_to_user(optval,&val,len)) |
| 859 | return -EFAULT; |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | void __init ipv6_packet_init(void) |
| 864 | { |
| 865 | dev_add_pack(&ipv6_packet_type); |
| 866 | } |
| 867 | |
| 868 | void ipv6_packet_cleanup(void) |
| 869 | { |
| 870 | dev_remove_pack(&ipv6_packet_type); |
| 871 | } |