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