11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * RAW sockets for IPv6 31da177e4SLinus Torvalds * Linux INET6 implementation 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: 61da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Adapted from linux/net/ipv4/raw.c 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * Fixes: 111da177e4SLinus Torvalds * Hideaki YOSHIFUJI : sin6_scope_id support 121da177e4SLinus Torvalds * YOSHIFUJI,H.@USAGI : raw checksum (RFC2292(bis) compliance) 131da177e4SLinus Torvalds * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 161da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 171da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 181da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 191da177e4SLinus Torvalds */ 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds #include <linux/errno.h> 221da177e4SLinus Torvalds #include <linux/types.h> 231da177e4SLinus Torvalds #include <linux/socket.h> 245a0e3ad6STejun Heo #include <linux/slab.h> 251da177e4SLinus Torvalds #include <linux/sockios.h> 261da177e4SLinus Torvalds #include <linux/net.h> 271da177e4SLinus Torvalds #include <linux/in6.h> 281da177e4SLinus Torvalds #include <linux/netdevice.h> 291da177e4SLinus Torvalds #include <linux/if_arp.h> 301da177e4SLinus Torvalds #include <linux/icmpv6.h> 311da177e4SLinus Torvalds #include <linux/netfilter.h> 321da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 333305b80cSHerbert Xu #include <linux/skbuff.h> 34e2d57766SDavid S. Miller #include <linux/compat.h> 3529778becSAlex W Slater #include <linux/uaccess.h> 361da177e4SLinus Torvalds #include <asm/ioctls.h> 371da177e4SLinus Torvalds 38457c4cbcSEric W. Biederman #include <net/net_namespace.h> 391da177e4SLinus Torvalds #include <net/ip.h> 401da177e4SLinus Torvalds #include <net/sock.h> 411da177e4SLinus Torvalds #include <net/snmp.h> 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #include <net/ipv6.h> 441da177e4SLinus Torvalds #include <net/ndisc.h> 451da177e4SLinus Torvalds #include <net/protocol.h> 461da177e4SLinus Torvalds #include <net/ip6_route.h> 471da177e4SLinus Torvalds #include <net/ip6_checksum.h> 481da177e4SLinus Torvalds #include <net/addrconf.h> 491da177e4SLinus Torvalds #include <net/transp_v6.h> 501da177e4SLinus Torvalds #include <net/udp.h> 511da177e4SLinus Torvalds #include <net/inet_common.h> 52c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h> 5307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 547be96f76SMasahide NAKAMURA #include <net/mip6.h> 557be96f76SMasahide NAKAMURA #endif 567bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 571da177e4SLinus Torvalds 58b673e4dfSPavel Emelyanov #include <net/raw.h> 591da177e4SLinus Torvalds #include <net/rawv6.h> 601da177e4SLinus Torvalds #include <net/xfrm.h> 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds #include <linux/proc_fs.h> 631da177e4SLinus Torvalds #include <linux/seq_file.h> 64bc3b2d7fSPaul Gortmaker #include <linux/export.h> 651da177e4SLinus Torvalds 66d1c53c8eSWerner Almesberger #define ICMPV6_HDRLEN 4 /* ICMPv6 header, RFC 4443 Section 2.1 */ 67d1c53c8eSWerner Almesberger 68432490f9SCyrill Gorcunov struct raw_hashinfo raw_v6_hashinfo = { 69938b93adSRobert P. J. Day .lock = __RW_LOCK_UNLOCKED(raw_v6_hashinfo.lock), 70b673e4dfSPavel Emelyanov }; 71432490f9SCyrill Gorcunov EXPORT_SYMBOL_GPL(raw_v6_hashinfo); 721da177e4SLinus Torvalds 73432490f9SCyrill Gorcunov struct sock *__raw_v6_lookup(struct net *net, struct sock *sk, 74b71d1d42SEric Dumazet unsigned short num, const struct in6_addr *loc_addr, 755108ab4bSDavid Ahern const struct in6_addr *rmt_addr, int dif, int sdif) 761da177e4SLinus Torvalds { 77a50feda5SEric Dumazet bool is_multicast = ipv6_addr_is_multicast(loc_addr); 781da177e4SLinus Torvalds 79b67bfe0dSSasha Levin sk_for_each_from(sk) 80c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num == num) { 811da177e4SLinus Torvalds 82878628fbSYOSHIFUJI Hideaki if (!net_eq(sock_net(sk), net)) 83be185884SPavel Emelyanov continue; 84be185884SPavel Emelyanov 85efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_daddr) && 86efe4208fSEric Dumazet !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) 871da177e4SLinus Torvalds continue; 881da177e4SLinus Torvalds 895108ab4bSDavid Ahern if (sk->sk_bound_dev_if && 905108ab4bSDavid Ahern sk->sk_bound_dev_if != dif && 915108ab4bSDavid Ahern sk->sk_bound_dev_if != sdif) 920bd1b59bSAndrew McDonald continue; 930bd1b59bSAndrew McDonald 94efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) { 95efe4208fSEric Dumazet if (ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)) 961da177e4SLinus Torvalds goto found; 971da177e4SLinus Torvalds if (is_multicast && 981da177e4SLinus Torvalds inet6_mc_check(sk, loc_addr, rmt_addr)) 991da177e4SLinus Torvalds goto found; 1001da177e4SLinus Torvalds continue; 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds goto found; 1031da177e4SLinus Torvalds } 1041da177e4SLinus Torvalds sk = NULL; 1051da177e4SLinus Torvalds found: 1061da177e4SLinus Torvalds return sk; 1071da177e4SLinus Torvalds } 108432490f9SCyrill Gorcunov EXPORT_SYMBOL_GPL(__raw_v6_lookup); 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds /* 1111da177e4SLinus Torvalds * 0 - deliver 1121da177e4SLinus Torvalds * 1 - block 1131da177e4SLinus Torvalds */ 1141b05c4b5SEric Dumazet static int icmpv6_filter(const struct sock *sk, const struct sk_buff *skb) 1151da177e4SLinus Torvalds { 1169cc08af3SWerner Almesberger struct icmp6hdr _hdr; 1171b05c4b5SEric Dumazet const struct icmp6hdr *hdr; 1181da177e4SLinus Torvalds 119d1c53c8eSWerner Almesberger /* We require only the four bytes of the ICMPv6 header, not any 120d1c53c8eSWerner Almesberger * additional bytes of message body in "struct icmp6hdr". 121d1c53c8eSWerner Almesberger */ 1221b05c4b5SEric Dumazet hdr = skb_header_pointer(skb, skb_transport_offset(skb), 123d1c53c8eSWerner Almesberger ICMPV6_HDRLEN, &_hdr); 1241b05c4b5SEric Dumazet if (hdr) { 1251b05c4b5SEric Dumazet const __u32 *data = &raw6_sk(sk)->filter.data[0]; 1261b05c4b5SEric Dumazet unsigned int type = hdr->icmp6_type; 1271da177e4SLinus Torvalds 1281b05c4b5SEric Dumazet return (data[type >> 5] & (1U << (type & 31))) != 0; 1291da177e4SLinus Torvalds } 1301b05c4b5SEric Dumazet return 1; 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 13307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 134f2eda47dSEric Dumazet typedef int mh_filter_t(struct sock *sock, struct sk_buff *skb); 13559fbb3a6SMasahide NAKAMURA 136f2eda47dSEric Dumazet static mh_filter_t __rcu *mh_filter __read_mostly; 137f2eda47dSEric Dumazet 138f2eda47dSEric Dumazet int rawv6_mh_filter_register(mh_filter_t filter) 13959fbb3a6SMasahide NAKAMURA { 140cf778b00SEric Dumazet rcu_assign_pointer(mh_filter, filter); 14159fbb3a6SMasahide NAKAMURA return 0; 14259fbb3a6SMasahide NAKAMURA } 14359fbb3a6SMasahide NAKAMURA EXPORT_SYMBOL(rawv6_mh_filter_register); 14459fbb3a6SMasahide NAKAMURA 145f2eda47dSEric Dumazet int rawv6_mh_filter_unregister(mh_filter_t filter) 14659fbb3a6SMasahide NAKAMURA { 147a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(mh_filter, NULL); 14859fbb3a6SMasahide NAKAMURA synchronize_rcu(); 14959fbb3a6SMasahide NAKAMURA return 0; 15059fbb3a6SMasahide NAKAMURA } 15159fbb3a6SMasahide NAKAMURA EXPORT_SYMBOL(rawv6_mh_filter_unregister); 15259fbb3a6SMasahide NAKAMURA 15359fbb3a6SMasahide NAKAMURA #endif 15459fbb3a6SMasahide NAKAMURA 1551da177e4SLinus Torvalds /* 1561da177e4SLinus Torvalds * demultiplex raw sockets. 1571da177e4SLinus Torvalds * (should consider queueing the skb in the sock receive_queue 1581da177e4SLinus Torvalds * without calling rawv6.c) 1591da177e4SLinus Torvalds * 1601da177e4SLinus Torvalds * Caller owns SKB so we must make clones. 1611da177e4SLinus Torvalds */ 162a50feda5SEric Dumazet static bool ipv6_raw_deliver(struct sk_buff *skb, int nexthdr) 1631da177e4SLinus Torvalds { 164b71d1d42SEric Dumazet const struct in6_addr *saddr; 165b71d1d42SEric Dumazet const struct in6_addr *daddr; 1661da177e4SLinus Torvalds struct sock *sk; 167a50feda5SEric Dumazet bool delivered = false; 1681da177e4SLinus Torvalds __u8 hash; 169be185884SPavel Emelyanov struct net *net; 1701da177e4SLinus Torvalds 1710660e03fSArnaldo Carvalho de Melo saddr = &ipv6_hdr(skb)->saddr; 1721da177e4SLinus Torvalds daddr = saddr + 1; 1731da177e4SLinus Torvalds 174f9242b6bSDavid S. Miller hash = nexthdr & (RAW_HTABLE_SIZE - 1); 1751da177e4SLinus Torvalds 176b673e4dfSPavel Emelyanov read_lock(&raw_v6_hashinfo.lock); 177b673e4dfSPavel Emelyanov sk = sk_head(&raw_v6_hashinfo.ht[hash]); 1781da177e4SLinus Torvalds 17963159f29SIan Morris if (!sk) 1801da177e4SLinus Torvalds goto out; 1811da177e4SLinus Torvalds 182c346dca1SYOSHIFUJI Hideaki net = dev_net(skb->dev); 1835108ab4bSDavid Ahern sk = __raw_v6_lookup(net, sk, nexthdr, daddr, saddr, 1845108ab4bSDavid Ahern inet6_iif(skb), inet6_sdif(skb)); 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds while (sk) { 1877be96f76SMasahide NAKAMURA int filtered; 1887be96f76SMasahide NAKAMURA 189a50feda5SEric Dumazet delivered = true; 1907be96f76SMasahide NAKAMURA switch (nexthdr) { 1917be96f76SMasahide NAKAMURA case IPPROTO_ICMPV6: 1927be96f76SMasahide NAKAMURA filtered = icmpv6_filter(sk, skb); 1937be96f76SMasahide NAKAMURA break; 19459fbb3a6SMasahide NAKAMURA 19507a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 1967be96f76SMasahide NAKAMURA case IPPROTO_MH: 19759fbb3a6SMasahide NAKAMURA { 1987be96f76SMasahide NAKAMURA /* XXX: To validate MH only once for each packet, 1997be96f76SMasahide NAKAMURA * this is placed here. It should be after checking 2007be96f76SMasahide NAKAMURA * xfrm policy, however it doesn't. The checking xfrm 2017be96f76SMasahide NAKAMURA * policy is placed in rawv6_rcv() because it is 2027be96f76SMasahide NAKAMURA * required for each socket. 2037be96f76SMasahide NAKAMURA */ 204f2eda47dSEric Dumazet mh_filter_t *filter; 20559fbb3a6SMasahide NAKAMURA 20659fbb3a6SMasahide NAKAMURA filter = rcu_dereference(mh_filter); 207f2eda47dSEric Dumazet filtered = filter ? (*filter)(sk, skb) : 0; 2087be96f76SMasahide NAKAMURA break; 20959fbb3a6SMasahide NAKAMURA } 2107be96f76SMasahide NAKAMURA #endif 2117be96f76SMasahide NAKAMURA default: 2127be96f76SMasahide NAKAMURA filtered = 0; 2137be96f76SMasahide NAKAMURA break; 2147be96f76SMasahide NAKAMURA } 2157be96f76SMasahide NAKAMURA 2167be96f76SMasahide NAKAMURA if (filtered < 0) 2177be96f76SMasahide NAKAMURA break; 2187be96f76SMasahide NAKAMURA if (filtered == 0) { 2191da177e4SLinus Torvalds struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC); 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds /* Not releasing hash table! */ 2229fb9cbb1SYasuyuki Kozakai if (clone) { 2239fb9cbb1SYasuyuki Kozakai nf_reset(clone); 2241da177e4SLinus Torvalds rawv6_rcv(sk, clone); 2251da177e4SLinus Torvalds } 2269fb9cbb1SYasuyuki Kozakai } 227be185884SPavel Emelyanov sk = __raw_v6_lookup(net, sk_next(sk), nexthdr, daddr, saddr, 2285108ab4bSDavid Ahern inet6_iif(skb), inet6_sdif(skb)); 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds out: 231b673e4dfSPavel Emelyanov read_unlock(&raw_v6_hashinfo.lock); 232d13964f4SPatrick McHardy return delivered; 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds 235a50feda5SEric Dumazet bool raw6_local_deliver(struct sk_buff *skb, int nexthdr) 23669d6da0bSPavel Emelyanov { 23769d6da0bSPavel Emelyanov struct sock *raw_sk; 23869d6da0bSPavel Emelyanov 239f9242b6bSDavid S. Miller raw_sk = sk_head(&raw_v6_hashinfo.ht[nexthdr & (RAW_HTABLE_SIZE - 1)]); 24069d6da0bSPavel Emelyanov if (raw_sk && !ipv6_raw_deliver(skb, nexthdr)) 24169d6da0bSPavel Emelyanov raw_sk = NULL; 24269d6da0bSPavel Emelyanov 24369d6da0bSPavel Emelyanov return raw_sk != NULL; 24469d6da0bSPavel Emelyanov } 24569d6da0bSPavel Emelyanov 2461da177e4SLinus Torvalds /* This cleans up af_inet6 a bit. -DaveM */ 2471da177e4SLinus Torvalds static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len) 2481da177e4SLinus Torvalds { 2491da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 2501da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 2511da177e4SLinus Torvalds struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr; 252e69a4adcSAl Viro __be32 v4addr = 0; 2531da177e4SLinus Torvalds int addr_type; 2541da177e4SLinus Torvalds int err; 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds if (addr_len < SIN6_LEN_RFC2133) 2571da177e4SLinus Torvalds return -EINVAL; 25882b276cdSHannes Frederic Sowa 25982b276cdSHannes Frederic Sowa if (addr->sin6_family != AF_INET6) 26082b276cdSHannes Frederic Sowa return -EINVAL; 26182b276cdSHannes Frederic Sowa 2621da177e4SLinus Torvalds addr_type = ipv6_addr_type(&addr->sin6_addr); 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds /* Raw sockets are IPv6 only */ 2651da177e4SLinus Torvalds if (addr_type == IPV6_ADDR_MAPPED) 266fd5c0027SEric Dumazet return -EADDRNOTAVAIL; 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds lock_sock(sk); 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds err = -EINVAL; 2711da177e4SLinus Torvalds if (sk->sk_state != TCP_CLOSE) 2721da177e4SLinus Torvalds goto out; 2731da177e4SLinus Torvalds 274fd5c0027SEric Dumazet rcu_read_lock(); 2751da177e4SLinus Torvalds /* Check if the address belongs to the host. */ 2761da177e4SLinus Torvalds if (addr_type != IPV6_ADDR_ANY) { 2771da177e4SLinus Torvalds struct net_device *dev = NULL; 2781da177e4SLinus Torvalds 279842df073SHannes Frederic Sowa if (__ipv6_addr_needs_scope_id(addr_type)) { 2801da177e4SLinus Torvalds if (addr_len >= sizeof(struct sockaddr_in6) && 2811da177e4SLinus Torvalds addr->sin6_scope_id) { 2821da177e4SLinus Torvalds /* Override any existing binding, if another 2831da177e4SLinus Torvalds * one is supplied by user. 2841da177e4SLinus Torvalds */ 2851da177e4SLinus Torvalds sk->sk_bound_dev_if = addr->sin6_scope_id; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds /* Binding to link-local address requires an interface */ 2891da177e4SLinus Torvalds if (!sk->sk_bound_dev_if) 290fd5c0027SEric Dumazet goto out_unlock; 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds err = -ENODEV; 293fd5c0027SEric Dumazet dev = dev_get_by_index_rcu(sock_net(sk), 294fd5c0027SEric Dumazet sk->sk_bound_dev_if); 295fd5c0027SEric Dumazet if (!dev) 296fd5c0027SEric Dumazet goto out_unlock; 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds /* ipv4 addr of the socket is invalid. Only the 3001da177e4SLinus Torvalds * unspecified and mapped address have a v4 equivalent. 3011da177e4SLinus Torvalds */ 3021da177e4SLinus Torvalds v4addr = LOOPBACK4_IPV6; 30335a256feSTom Herbert if (!(addr_type & IPV6_ADDR_MULTICAST) && 30435a256feSTom Herbert !sock_net(sk)->ipv6.sysctl.ip_nonlocal_bind) { 3051da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 3063b1e0a65SYOSHIFUJI Hideaki if (!ipv6_chk_addr(sock_net(sk), &addr->sin6_addr, 307bfeade08SDaniel Lezcano dev, 0)) { 308fd5c0027SEric Dumazet goto out_unlock; 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds 313c720c7e8SEric Dumazet inet->inet_rcv_saddr = inet->inet_saddr = v4addr; 314efe4208fSEric Dumazet sk->sk_v6_rcv_saddr = addr->sin6_addr; 3151da177e4SLinus Torvalds if (!(addr_type & IPV6_ADDR_MULTICAST)) 3164e3fd7a0SAlexey Dobriyan np->saddr = addr->sin6_addr; 3171da177e4SLinus Torvalds err = 0; 318fd5c0027SEric Dumazet out_unlock: 319fd5c0027SEric Dumazet rcu_read_unlock(); 3201da177e4SLinus Torvalds out: 3211da177e4SLinus Torvalds release_sock(sk); 3221da177e4SLinus Torvalds return err; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 32569d6da0bSPavel Emelyanov static void rawv6_err(struct sock *sk, struct sk_buff *skb, 3261da177e4SLinus Torvalds struct inet6_skb_parm *opt, 327d5fdd6baSBrian Haley u8 type, u8 code, int offset, __be32 info) 3281da177e4SLinus Torvalds { 3291da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 3301da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 3311da177e4SLinus Torvalds int err; 3321da177e4SLinus Torvalds int harderr; 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds /* Report error on raw socket, if: 3351da177e4SLinus Torvalds 1. User requested recverr. 3361da177e4SLinus Torvalds 2. Socket is connected (otherwise the error indication 3371da177e4SLinus Torvalds is useless without recverr and error is hard. 3381da177e4SLinus Torvalds */ 3391da177e4SLinus Torvalds if (!np->recverr && sk->sk_state != TCP_ESTABLISHED) 3401da177e4SLinus Torvalds return; 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds harderr = icmpv6_err_convert(type, code, &err); 34381aded24SDavid S. Miller if (type == ICMPV6_PKT_TOOBIG) { 34481aded24SDavid S. Miller ip6_sk_update_pmtu(skb, sk, info); 3451da177e4SLinus Torvalds harderr = (np->pmtudisc == IPV6_PMTUDISC_DO); 34681aded24SDavid S. Miller } 3478d65b119SDuan Jiong if (type == NDISC_REDIRECT) { 348ec18d9a2SDavid S. Miller ip6_sk_redirect(skb, sk); 3498d65b119SDuan Jiong return; 3508d65b119SDuan Jiong } 3511da177e4SLinus Torvalds if (np->recverr) { 3521da177e4SLinus Torvalds u8 *payload = skb->data; 3531da177e4SLinus Torvalds if (!inet->hdrincl) 3541da177e4SLinus Torvalds payload += offset; 3551da177e4SLinus Torvalds ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload); 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds if (np->recverr || harderr) { 3591da177e4SLinus Torvalds sk->sk_err = err; 3601da177e4SLinus Torvalds sk->sk_error_report(sk); 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds 36469d6da0bSPavel Emelyanov void raw6_icmp_error(struct sk_buff *skb, int nexthdr, 365d5fdd6baSBrian Haley u8 type, u8 code, int inner_offset, __be32 info) 36669d6da0bSPavel Emelyanov { 36769d6da0bSPavel Emelyanov struct sock *sk; 36869d6da0bSPavel Emelyanov int hash; 369b71d1d42SEric Dumazet const struct in6_addr *saddr, *daddr; 370be185884SPavel Emelyanov struct net *net; 37169d6da0bSPavel Emelyanov 372b673e4dfSPavel Emelyanov hash = nexthdr & (RAW_HTABLE_SIZE - 1); 37369d6da0bSPavel Emelyanov 374b673e4dfSPavel Emelyanov read_lock(&raw_v6_hashinfo.lock); 375b673e4dfSPavel Emelyanov sk = sk_head(&raw_v6_hashinfo.ht[hash]); 37653b24b8fSIan Morris if (sk) { 37705f175cdSYOSHIFUJI Hideaki /* Note: ipv6_hdr(skb) != skb->data */ 378b71d1d42SEric Dumazet const struct ipv6hdr *ip6h = (const struct ipv6hdr *)skb->data; 37905f175cdSYOSHIFUJI Hideaki saddr = &ip6h->saddr; 38005f175cdSYOSHIFUJI Hideaki daddr = &ip6h->daddr; 381c346dca1SYOSHIFUJI Hideaki net = dev_net(skb->dev); 38269d6da0bSPavel Emelyanov 383be185884SPavel Emelyanov while ((sk = __raw_v6_lookup(net, sk, nexthdr, saddr, daddr, 3845108ab4bSDavid Ahern inet6_iif(skb), inet6_iif(skb)))) { 38569d6da0bSPavel Emelyanov rawv6_err(sk, skb, NULL, type, code, 38669d6da0bSPavel Emelyanov inner_offset, info); 38769d6da0bSPavel Emelyanov sk = sk_next(sk); 38869d6da0bSPavel Emelyanov } 38969d6da0bSPavel Emelyanov } 390b673e4dfSPavel Emelyanov read_unlock(&raw_v6_hashinfo.lock); 39169d6da0bSPavel Emelyanov } 39269d6da0bSPavel Emelyanov 3931da177e4SLinus Torvalds static inline int rawv6_rcv_skb(struct sock *sk, struct sk_buff *skb) 3941da177e4SLinus Torvalds { 39533d480ceSEric Dumazet if ((raw6_sk(sk)->checksum || rcu_access_pointer(sk->sk_filter)) && 396fb286bb2SHerbert Xu skb_checksum_complete(skb)) { 397a92aa318SWang Chen atomic_inc(&sk->sk_drops); 3981da177e4SLinus Torvalds kfree_skb(skb); 3993cc76caaSYang Hongyang return NET_RX_DROP; 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds /* Charge it to the socket. */ 403d826eb14SEric Dumazet skb_dst_drop(skb); 404d826eb14SEric Dumazet if (sock_queue_rcv_skb(sk, skb) < 0) { 4051da177e4SLinus Torvalds kfree_skb(skb); 4063cc76caaSYang Hongyang return NET_RX_DROP; 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds return 0; 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds /* 4131da177e4SLinus Torvalds * This is next to useless... 4141da177e4SLinus Torvalds * if we demultiplex in network layer we don't need the extra call 4151da177e4SLinus Torvalds * just to queue the skb... 4161da177e4SLinus Torvalds * maybe we could have the network decide upon a hint if it 4171da177e4SLinus Torvalds * should call raw_rcv for demultiplexing 4181da177e4SLinus Torvalds */ 4191da177e4SLinus Torvalds int rawv6_rcv(struct sock *sk, struct sk_buff *skb) 4201da177e4SLinus Torvalds { 4211da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 4221da177e4SLinus Torvalds struct raw6_sock *rp = raw6_sk(sk); 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) { 425a92aa318SWang Chen atomic_inc(&sk->sk_drops); 4261da177e4SLinus Torvalds kfree_skb(skb); 4271da177e4SLinus Torvalds return NET_RX_DROP; 4281da177e4SLinus Torvalds } 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds if (!rp->checksum) 4311da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_UNNECESSARY; 4321da177e4SLinus Torvalds 43384fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_COMPLETE) { 434d56f90a7SArnaldo Carvalho de Melo skb_postpull_rcsum(skb, skb_network_header(skb), 435cfe1fc77SArnaldo Carvalho de Melo skb_network_header_len(skb)); 4360660e03fSArnaldo Carvalho de Melo if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 4370660e03fSArnaldo Carvalho de Melo &ipv6_hdr(skb)->daddr, 438c720c7e8SEric Dumazet skb->len, inet->inet_num, skb->csum)) 439fb286bb2SHerbert Xu skb->ip_summed = CHECKSUM_UNNECESSARY; 4401da177e4SLinus Torvalds } 44160476372SHerbert Xu if (!skb_csum_unnecessary(skb)) 4420660e03fSArnaldo Carvalho de Melo skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 4430660e03fSArnaldo Carvalho de Melo &ipv6_hdr(skb)->daddr, 4440660e03fSArnaldo Carvalho de Melo skb->len, 445c720c7e8SEric Dumazet inet->inet_num, 0)); 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds if (inet->hdrincl) { 448fb286bb2SHerbert Xu if (skb_checksum_complete(skb)) { 449a92aa318SWang Chen atomic_inc(&sk->sk_drops); 4501da177e4SLinus Torvalds kfree_skb(skb); 4513cc76caaSYang Hongyang return NET_RX_DROP; 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds rawv6_rcv_skb(sk, skb); 4561da177e4SLinus Torvalds return 0; 4571da177e4SLinus Torvalds } 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds /* 4611da177e4SLinus Torvalds * This should be easy, if there is something there 4621da177e4SLinus Torvalds * we return it, otherwise we block. 4631da177e4SLinus Torvalds */ 4641da177e4SLinus Torvalds 4651b784140SYing Xue static int rawv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 4661da177e4SLinus Torvalds int noblock, int flags, int *addr_len) 4671da177e4SLinus Torvalds { 4681da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 469342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 4701da177e4SLinus Torvalds struct sk_buff *skb; 4711da177e4SLinus Torvalds size_t copied; 4721da177e4SLinus Torvalds int err; 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds if (flags & MSG_OOB) 4751da177e4SLinus Torvalds return -EOPNOTSUPP; 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds if (flags & MSG_ERRQUEUE) 47885fbaa75SHannes Frederic Sowa return ipv6_recv_error(sk, msg, len, addr_len); 4791da177e4SLinus Torvalds 4804b340ae2SBrian Haley if (np->rxpmtu && np->rxopt.bits.rxpmtu) 48185fbaa75SHannes Frederic Sowa return ipv6_recv_rxpmtu(sk, msg, len, addr_len); 4824b340ae2SBrian Haley 4831da177e4SLinus Torvalds skb = skb_recv_datagram(sk, flags, noblock, &err); 4841da177e4SLinus Torvalds if (!skb) 4851da177e4SLinus Torvalds goto out; 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds copied = skb->len; 4881da177e4SLinus Torvalds if (copied > len) { 4891da177e4SLinus Torvalds copied = len; 4901da177e4SLinus Torvalds msg->msg_flags |= MSG_TRUNC; 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds 49360476372SHerbert Xu if (skb_csum_unnecessary(skb)) { 49451f3d02bSDavid S. Miller err = skb_copy_datagram_msg(skb, 0, msg, copied); 4951da177e4SLinus Torvalds } else if (msg->msg_flags&MSG_TRUNC) { 496fb286bb2SHerbert Xu if (__skb_checksum_complete(skb)) 4971da177e4SLinus Torvalds goto csum_copy_err; 49851f3d02bSDavid S. Miller err = skb_copy_datagram_msg(skb, 0, msg, copied); 4991da177e4SLinus Torvalds } else { 500227158dbSAl Viro err = skb_copy_and_csum_datagram_msg(skb, 0, msg); 5011da177e4SLinus Torvalds if (err == -EINVAL) 5021da177e4SLinus Torvalds goto csum_copy_err; 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds if (err) 5051da177e4SLinus Torvalds goto out_free; 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds /* Copy the address. */ 5081da177e4SLinus Torvalds if (sin6) { 5091da177e4SLinus Torvalds sin6->sin6_family = AF_INET6; 510f59fc7f3STetsuo Handa sin6->sin6_port = 0; 5114e3fd7a0SAlexey Dobriyan sin6->sin6_addr = ipv6_hdr(skb)->saddr; 5121da177e4SLinus Torvalds sin6->sin6_flowinfo = 0; 513842df073SHannes Frederic Sowa sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr, 5144330487aSDuan Jiong inet6_iif(skb)); 515bceaa902SHannes Frederic Sowa *addr_len = sizeof(*sin6); 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds 5183b885787SNeil Horman sock_recv_ts_and_drops(msg, sk, skb); 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds if (np->rxopt.all) 52173df66f8STom Parkin ip6_datagram_recv_ctl(sk, msg, skb); 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds err = copied; 5241da177e4SLinus Torvalds if (flags & MSG_TRUNC) 5251da177e4SLinus Torvalds err = skb->len; 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds out_free: 5281da177e4SLinus Torvalds skb_free_datagram(sk, skb); 5291da177e4SLinus Torvalds out: 5301da177e4SLinus Torvalds return err; 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds csum_copy_err: 5333305b80cSHerbert Xu skb_kill_datagram(sk, skb, flags); 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds /* Error for blocking case is chosen to masquerade 5361da177e4SLinus Torvalds as some normal condition. 5371da177e4SLinus Torvalds */ 5381da177e4SLinus Torvalds err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH; 5393305b80cSHerbert Xu goto out; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 5424c9483b2SDavid S. Miller static int rawv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6, 543357b40a1SHerbert Xu struct raw6_sock *rp) 5441da177e4SLinus Torvalds { 5451da177e4SLinus Torvalds struct sk_buff *skb; 5461da177e4SLinus Torvalds int err = 0; 547357b40a1SHerbert Xu int offset; 548357b40a1SHerbert Xu int len; 549679a8738SHerbert Xu int total_len; 550868c86bcSAl Viro __wsum tmp_csum; 551868c86bcSAl Viro __sum16 csum; 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds if (!rp->checksum) 5541da177e4SLinus Torvalds goto send; 5551da177e4SLinus Torvalds 55643728fa5SFabian Frederick skb = skb_peek(&sk->sk_write_queue); 55743728fa5SFabian Frederick if (!skb) 5581da177e4SLinus Torvalds goto out; 5591da177e4SLinus Torvalds 560357b40a1SHerbert Xu offset = rp->offset; 561299b0767SSteffen Klassert total_len = inet_sk(sk)->cork.base.length; 562679a8738SHerbert Xu if (offset >= total_len - 1) { 5631da177e4SLinus Torvalds err = -EINVAL; 564357b40a1SHerbert Xu ip6_flush_pending_frames(sk); 5651da177e4SLinus Torvalds goto out; 5661da177e4SLinus Torvalds } 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds /* should be check HW csum miyazawa */ 5691da177e4SLinus Torvalds if (skb_queue_len(&sk->sk_write_queue) == 1) { 5701da177e4SLinus Torvalds /* 5711da177e4SLinus Torvalds * Only one fragment on the socket. 5721da177e4SLinus Torvalds */ 5731da177e4SLinus Torvalds tmp_csum = skb->csum; 5741da177e4SLinus Torvalds } else { 575357b40a1SHerbert Xu struct sk_buff *csum_skb = NULL; 5761da177e4SLinus Torvalds tmp_csum = 0; 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds skb_queue_walk(&sk->sk_write_queue, skb) { 5791da177e4SLinus Torvalds tmp_csum = csum_add(tmp_csum, skb->csum); 580357b40a1SHerbert Xu 581357b40a1SHerbert Xu if (csum_skb) 582357b40a1SHerbert Xu continue; 583357b40a1SHerbert Xu 584ea2ae17dSArnaldo Carvalho de Melo len = skb->len - skb_transport_offset(skb); 585357b40a1SHerbert Xu if (offset >= len) { 586357b40a1SHerbert Xu offset -= len; 587357b40a1SHerbert Xu continue; 5881da177e4SLinus Torvalds } 589357b40a1SHerbert Xu 590357b40a1SHerbert Xu csum_skb = skb; 5911da177e4SLinus Torvalds } 5921da177e4SLinus Torvalds 593357b40a1SHerbert Xu skb = csum_skb; 594357b40a1SHerbert Xu } 595357b40a1SHerbert Xu 596ea2ae17dSArnaldo Carvalho de Melo offset += skb_transport_offset(skb); 597a98f9175SDave Jones err = skb_copy_bits(skb, offset, &csum, 2); 598a98f9175SDave Jones if (err < 0) { 599a98f9175SDave Jones ip6_flush_pending_frames(sk); 600a98f9175SDave Jones goto out; 601a98f9175SDave Jones } 602357b40a1SHerbert Xu 6031da177e4SLinus Torvalds /* in case cksum was not initialized */ 604357b40a1SHerbert Xu if (unlikely(csum)) 6055f92a738SAl Viro tmp_csum = csum_sub(tmp_csum, csum_unfold(csum)); 6061da177e4SLinus Torvalds 6074c9483b2SDavid S. Miller csum = csum_ipv6_magic(&fl6->saddr, &fl6->daddr, 6084c9483b2SDavid S. Miller total_len, fl6->flowi6_proto, tmp_csum); 6091da177e4SLinus Torvalds 6104c9483b2SDavid S. Miller if (csum == 0 && fl6->flowi6_proto == IPPROTO_UDP) 611f6ab0288SAl Viro csum = CSUM_MANGLED_0; 612357b40a1SHerbert Xu 6138242fc33SHimangi Saraogi BUG_ON(skb_store_bits(skb, offset, &csum, 2)); 614357b40a1SHerbert Xu 6151da177e4SLinus Torvalds send: 6161da177e4SLinus Torvalds err = ip6_push_pending_frames(sk); 6171da177e4SLinus Torvalds out: 6181da177e4SLinus Torvalds return err; 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds 621c3c1a7dbSAl Viro static int rawv6_send_hdrinc(struct sock *sk, struct msghdr *msg, int length, 6224c9483b2SDavid S. Miller struct flowi6 *fl6, struct dst_entry **dstp, 623a818f75eSJesus Sanchez-Palencia unsigned int flags, const struct sockcm_cookie *sockc) 6241da177e4SLinus Torvalds { 6253320da89SHerbert Xu struct ipv6_pinfo *np = inet6_sk(sk); 626adb28c9dSEric W. Biederman struct net *net = sock_net(sk); 6271da177e4SLinus Torvalds struct ipv6hdr *iph; 6281da177e4SLinus Torvalds struct sk_buff *skb; 6291da177e4SLinus Torvalds int err; 6301789a640SEric Dumazet struct rt6_info *rt = (struct rt6_info *)*dstp; 631a7ae1992SHerbert Xu int hlen = LL_RESERVED_SPACE(rt->dst.dev); 632a7ae1992SHerbert Xu int tlen = rt->dst.dev->needed_tailroom; 6331da177e4SLinus Torvalds 634d8d1f30bSChangli Gao if (length > rt->dst.dev->mtu) { 6354c9483b2SDavid S. Miller ipv6_local_error(sk, EMSGSIZE, fl6, rt->dst.dev->mtu); 6361da177e4SLinus Torvalds return -EMSGSIZE; 6371da177e4SLinus Torvalds } 63886f4c90aSAlexander Potapenko if (length < sizeof(struct ipv6hdr)) 63986f4c90aSAlexander Potapenko return -EINVAL; 6401da177e4SLinus Torvalds if (flags&MSG_PROBE) 6411da177e4SLinus Torvalds goto out; 6421da177e4SLinus Torvalds 643f5184d26SJohannes Berg skb = sock_alloc_send_skb(sk, 644a7ae1992SHerbert Xu length + hlen + tlen + 15, 6451da177e4SLinus Torvalds flags & MSG_DONTWAIT, &err); 64663159f29SIan Morris if (!skb) 6471da177e4SLinus Torvalds goto error; 648a7ae1992SHerbert Xu skb_reserve(skb, hlen); 6491da177e4SLinus Torvalds 6509c9c9ad5SHannes Frederic Sowa skb->protocol = htons(ETH_P_IPV6); 6511da177e4SLinus Torvalds skb->priority = sk->sk_priority; 6524a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 653a818f75eSJesus Sanchez-Palencia skb->tstamp = sockc->transmit_time; 6541da177e4SLinus Torvalds 6551ced98e8SArnaldo Carvalho de Melo skb_put(skb, length); 6561ced98e8SArnaldo Carvalho de Melo skb_reset_network_header(skb); 6570660e03fSArnaldo Carvalho de Melo iph = ipv6_hdr(skb); 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 6601da177e4SLinus Torvalds 6610dec879fSJulian Anastasov if (flags & MSG_CONFIRM) 6620dec879fSJulian Anastasov skb_set_dst_pending_confirm(skb, 1); 6630dec879fSJulian Anastasov 664b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 66521226abbSAl Viro err = memcpy_from_msg(iph, msg, length); 666a688caa3SWei Wang if (err) { 667a688caa3SWei Wang err = -EFAULT; 668a688caa3SWei Wang kfree_skb(skb); 669a688caa3SWei Wang goto error; 670a688caa3SWei Wang } 671a688caa3SWei Wang 672a688caa3SWei Wang skb_dst_set(skb, &rt->dst); 673a688caa3SWei Wang *dstp = NULL; 6741da177e4SLinus Torvalds 675a8e3e1a9SDavid Ahern /* if egress device is enslaved to an L3 master device pass the 676a8e3e1a9SDavid Ahern * skb to its handler for processing 677a8e3e1a9SDavid Ahern */ 678a8e3e1a9SDavid Ahern skb = l3mdev_ip6_out(sk, skb); 679a8e3e1a9SDavid Ahern if (unlikely(!skb)) 680a8e3e1a9SDavid Ahern return 0; 681a8e3e1a9SDavid Ahern 682a688caa3SWei Wang /* Acquire rcu_read_lock() in case we need to use rt->rt6i_idev 683a688caa3SWei Wang * in the error path. Since skb has been freed, the dst could 684a688caa3SWei Wang * have been queued for deletion. 685a688caa3SWei Wang */ 686a688caa3SWei Wang rcu_read_lock(); 687adb28c9dSEric W. Biederman IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len); 68829a26a56SEric W. Biederman err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb, 68913206b6bSEric W. Biederman NULL, rt->dst.dev, dst_output); 6901da177e4SLinus Torvalds if (err > 0) 6916ce9e7b5SEric Dumazet err = net_xmit_errno(err); 692a688caa3SWei Wang if (err) { 693a688caa3SWei Wang IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS); 694a688caa3SWei Wang rcu_read_unlock(); 695a688caa3SWei Wang goto error_check; 696a688caa3SWei Wang } 697a688caa3SWei Wang rcu_read_unlock(); 6981da177e4SLinus Torvalds out: 6991da177e4SLinus Torvalds return 0; 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds error: 702adb28c9dSEric W. Biederman IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS); 703a688caa3SWei Wang error_check: 7046ce9e7b5SEric Dumazet if (err == -ENOBUFS && !np->recverr) 7056ce9e7b5SEric Dumazet err = 0; 7061da177e4SLinus Torvalds return err; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 70919e3c66bSAl Viro struct raw6_frag_vec { 71019e3c66bSAl Viro struct msghdr *msg; 71119e3c66bSAl Viro int hlen; 71219e3c66bSAl Viro char c[4]; 71319e3c66bSAl Viro }; 71419e3c66bSAl Viro 71519e3c66bSAl Viro static int rawv6_probe_proto_opt(struct raw6_frag_vec *rfv, struct flowi6 *fl6) 7161da177e4SLinus Torvalds { 71719e3c66bSAl Viro int err = 0; 7184c9483b2SDavid S. Miller switch (fl6->flowi6_proto) { 7191da177e4SLinus Torvalds case IPPROTO_ICMPV6: 72019e3c66bSAl Viro rfv->hlen = 2; 72119e3c66bSAl Viro err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen); 72219e3c66bSAl Viro if (!err) { 72319e3c66bSAl Viro fl6->fl6_icmp_type = rfv->c[0]; 72419e3c66bSAl Viro fl6->fl6_icmp_code = rfv->c[1]; 7251da177e4SLinus Torvalds } 7261da177e4SLinus Torvalds break; 7276e8f4d48SMasahide NAKAMURA case IPPROTO_MH: 72819e3c66bSAl Viro rfv->hlen = 4; 72919e3c66bSAl Viro err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen); 73019e3c66bSAl Viro if (!err) 73119e3c66bSAl Viro fl6->fl6_mh_type = rfv->c[2]; 73219e3c66bSAl Viro } 73319e3c66bSAl Viro return err; 73419e3c66bSAl Viro } 7356e8f4d48SMasahide NAKAMURA 73619e3c66bSAl Viro static int raw6_getfrag(void *from, char *to, int offset, int len, int odd, 73719e3c66bSAl Viro struct sk_buff *skb) 73819e3c66bSAl Viro { 73919e3c66bSAl Viro struct raw6_frag_vec *rfv = from; 74019e3c66bSAl Viro 74119e3c66bSAl Viro if (offset < rfv->hlen) { 74219e3c66bSAl Viro int copy = min(rfv->hlen - offset, len); 74319e3c66bSAl Viro 74419e3c66bSAl Viro if (skb->ip_summed == CHECKSUM_PARTIAL) 74519e3c66bSAl Viro memcpy(to, rfv->c + offset, copy); 74619e3c66bSAl Viro else 74719e3c66bSAl Viro skb->csum = csum_block_add( 74819e3c66bSAl Viro skb->csum, 74919e3c66bSAl Viro csum_partial_copy_nocheck(rfv->c + offset, 75019e3c66bSAl Viro to, copy, 0), 75119e3c66bSAl Viro odd); 75219e3c66bSAl Viro 75319e3c66bSAl Viro odd = 0; 75419e3c66bSAl Viro offset += copy; 75519e3c66bSAl Viro to += copy; 75619e3c66bSAl Viro len -= copy; 75719e3c66bSAl Viro 75819e3c66bSAl Viro if (!len) 759a27b58feSHeiko Carstens return 0; 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds 76219e3c66bSAl Viro offset -= rfv->hlen; 76319e3c66bSAl Viro 764f69e6d13SAl Viro return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb); 76519e3c66bSAl Viro } 76619e3c66bSAl Viro 7671b784140SYing Xue static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 7681da177e4SLinus Torvalds { 76945f6fad8SEric Dumazet struct ipv6_txoptions *opt_to_free = NULL; 7701da177e4SLinus Torvalds struct ipv6_txoptions opt_space; 771342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 77220c59de2SArnaud Ebalard struct in6_addr *daddr, *final_p, final; 7731da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 7741da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 7751da177e4SLinus Torvalds struct raw6_sock *rp = raw6_sk(sk); 7761da177e4SLinus Torvalds struct ipv6_txoptions *opt = NULL; 7771da177e4SLinus Torvalds struct ip6_flowlabel *flowlabel = NULL; 7781da177e4SLinus Torvalds struct dst_entry *dst = NULL; 77919e3c66bSAl Viro struct raw6_frag_vec rfv; 7804c9483b2SDavid S. Miller struct flowi6 fl6; 78126879da5SWei Wang struct ipcm6_cookie ipc6; 7821da177e4SLinus Torvalds int addr_len = msg->msg_namelen; 7831da177e4SLinus Torvalds u16 proto; 7841da177e4SLinus Torvalds int err; 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds /* Rough check on arithmetic overflow, 787b59e139bSYOSHIFUJI Hideaki better check is made in ip6_append_data(). 7881da177e4SLinus Torvalds */ 789b59e139bSYOSHIFUJI Hideaki if (len > INT_MAX) 7901da177e4SLinus Torvalds return -EMSGSIZE; 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds /* Mirror BSD error message compatibility */ 7931da177e4SLinus Torvalds if (msg->msg_flags & MSG_OOB) 7941da177e4SLinus Torvalds return -EOPNOTSUPP; 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds /* 7971da177e4SLinus Torvalds * Get and verify the address. 7981da177e4SLinus Torvalds */ 7994c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 8001da177e4SLinus Torvalds 8014c9483b2SDavid S. Miller fl6.flowi6_mark = sk->sk_mark; 802e2d118a1SLorenzo Colitti fl6.flowi6_uid = sk->sk_uid; 8034a19ec58SLaszlo Attila Toth 804b515430aSWillem de Bruijn ipcm6_init(&ipc6); 8055fdaa88dSWillem de Bruijn ipc6.sockc.tsflags = sk->sk_tsflags; 80626879da5SWei Wang 8071da177e4SLinus Torvalds if (sin6) { 8081da177e4SLinus Torvalds if (addr_len < SIN6_LEN_RFC2133) 8091da177e4SLinus Torvalds return -EINVAL; 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds if (sin6->sin6_family && sin6->sin6_family != AF_INET6) 812a02cec21SEric Dumazet return -EAFNOSUPPORT; 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds /* port is the proto value [0..255] carried in nexthdr */ 8151da177e4SLinus Torvalds proto = ntohs(sin6->sin6_port); 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds if (!proto) 818c720c7e8SEric Dumazet proto = inet->inet_num; 819c720c7e8SEric Dumazet else if (proto != inet->inet_num) 820a02cec21SEric Dumazet return -EINVAL; 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds if (proto > 255) 823a02cec21SEric Dumazet return -EINVAL; 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds daddr = &sin6->sin6_addr; 8261da177e4SLinus Torvalds if (np->sndflow) { 8274c9483b2SDavid S. Miller fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; 8284c9483b2SDavid S. Miller if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) { 8294c9483b2SDavid S. Miller flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 83063159f29SIan Morris if (!flowlabel) 8311da177e4SLinus Torvalds return -EINVAL; 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds /* 8361da177e4SLinus Torvalds * Otherwise it will be difficult to maintain 8371da177e4SLinus Torvalds * sk->sk_dst_cache. 8381da177e4SLinus Torvalds */ 8391da177e4SLinus Torvalds if (sk->sk_state == TCP_ESTABLISHED && 840efe4208fSEric Dumazet ipv6_addr_equal(daddr, &sk->sk_v6_daddr)) 841efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds if (addr_len >= sizeof(struct sockaddr_in6) && 8441da177e4SLinus Torvalds sin6->sin6_scope_id && 845842df073SHannes Frederic Sowa __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr))) 8464c9483b2SDavid S. Miller fl6.flowi6_oif = sin6->sin6_scope_id; 8471da177e4SLinus Torvalds } else { 8481da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED) 8491da177e4SLinus Torvalds return -EDESTADDRREQ; 8501da177e4SLinus Torvalds 851c720c7e8SEric Dumazet proto = inet->inet_num; 852efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 8534c9483b2SDavid S. Miller fl6.flowlabel = np->flow_label; 8541da177e4SLinus Torvalds } 8551da177e4SLinus Torvalds 8564c9483b2SDavid S. Miller if (fl6.flowi6_oif == 0) 8574c9483b2SDavid S. Miller fl6.flowi6_oif = sk->sk_bound_dev_if; 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds if (msg->msg_controllen) { 8601da177e4SLinus Torvalds opt = &opt_space; 8611da177e4SLinus Torvalds memset(opt, 0, sizeof(struct ipv6_txoptions)); 8621da177e4SLinus Torvalds opt->tot_len = sizeof(struct ipv6_txoptions); 86326879da5SWei Wang ipc6.opt = opt; 8641da177e4SLinus Torvalds 8655fdaa88dSWillem de Bruijn err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6); 8661da177e4SLinus Torvalds if (err < 0) { 8671da177e4SLinus Torvalds fl6_sock_release(flowlabel); 8681da177e4SLinus Torvalds return err; 8691da177e4SLinus Torvalds } 8704c9483b2SDavid S. Miller if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { 8714c9483b2SDavid S. Miller flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 87263159f29SIan Morris if (!flowlabel) 8731da177e4SLinus Torvalds return -EINVAL; 8741da177e4SLinus Torvalds } 8751da177e4SLinus Torvalds if (!(opt->opt_nflen|opt->opt_flen)) 8761da177e4SLinus Torvalds opt = NULL; 8771da177e4SLinus Torvalds } 87845f6fad8SEric Dumazet if (!opt) { 87945f6fad8SEric Dumazet opt = txopt_get(np); 88045f6fad8SEric Dumazet opt_to_free = opt; 88145f6fad8SEric Dumazet } 882df9890c3SYOSHIFUJI Hideaki if (flowlabel) 8831da177e4SLinus Torvalds opt = fl6_merge_options(&opt_space, flowlabel, opt); 884df9890c3SYOSHIFUJI Hideaki opt = ipv6_fixup_options(&opt_space, opt); 8851da177e4SLinus Torvalds 8864c9483b2SDavid S. Miller fl6.flowi6_proto = proto; 88719e3c66bSAl Viro rfv.msg = msg; 88819e3c66bSAl Viro rfv.hlen = 0; 88919e3c66bSAl Viro err = rawv6_probe_proto_opt(&rfv, &fl6); 890a27b58feSHeiko Carstens if (err) 891a27b58feSHeiko Carstens goto out; 8921da177e4SLinus Torvalds 893876c7f41SBrian Haley if (!ipv6_addr_any(daddr)) 8944e3fd7a0SAlexey Dobriyan fl6.daddr = *daddr; 895876c7f41SBrian Haley else 8964c9483b2SDavid S. Miller fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */ 8974c9483b2SDavid S. Miller if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr)) 8984e3fd7a0SAlexey Dobriyan fl6.saddr = np->saddr; 8991da177e4SLinus Torvalds 9004c9483b2SDavid S. Miller final_p = fl6_update_dst(&fl6, opt, &final); 9011da177e4SLinus Torvalds 9024c9483b2SDavid S. Miller if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) 9034c9483b2SDavid S. Miller fl6.flowi6_oif = np->mcast_oif; 904c4062dfcSErich E. Hoover else if (!fl6.flowi6_oif) 905c4062dfcSErich E. Hoover fl6.flowi6_oif = np->ucast_oif; 9064c9483b2SDavid S. Miller security_sk_classify_flow(sk, flowi6_to_flowi(&fl6)); 9071da177e4SLinus Torvalds 90848e8aa6eSMartin KaFai Lau if (inet->hdrincl) 90948e8aa6eSMartin KaFai Lau fl6.flowi6_flags |= FLOWI_FLAG_KNOWN_NH; 91048e8aa6eSMartin KaFai Lau 91138b7097bSHannes Frederic Sowa if (ipc6.tclass < 0) 91238b7097bSHannes Frederic Sowa ipc6.tclass = np->tclass; 91338b7097bSHannes Frederic Sowa 91438b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel); 91538b7097bSHannes Frederic Sowa 9160e0d44abSSteffen Klassert dst = ip6_dst_lookup_flow(sk, &fl6, final_p); 91768d0c6d3SDavid S. Miller if (IS_ERR(dst)) { 91868d0c6d3SDavid S. Miller err = PTR_ERR(dst); 9191da177e4SLinus Torvalds goto out; 92014e50e57SDavid S. Miller } 92126879da5SWei Wang if (ipc6.hlimit < 0) 92226879da5SWei Wang ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst); 9231da177e4SLinus Torvalds 92426879da5SWei Wang if (ipc6.dontfrag < 0) 92526879da5SWei Wang ipc6.dontfrag = np->dontfrag; 92613b52cd4SBrian Haley 9271da177e4SLinus Torvalds if (msg->msg_flags&MSG_CONFIRM) 9281da177e4SLinus Torvalds goto do_confirm; 9291da177e4SLinus Torvalds 9301da177e4SLinus Torvalds back_from_confirm: 9311789a640SEric Dumazet if (inet->hdrincl) 932a818f75eSJesus Sanchez-Palencia err = rawv6_send_hdrinc(sk, msg, len, &fl6, &dst, 9335fdaa88dSWillem de Bruijn msg->msg_flags, &ipc6.sockc); 9341789a640SEric Dumazet else { 93526879da5SWei Wang ipc6.opt = opt; 9361da177e4SLinus Torvalds lock_sock(sk); 93719e3c66bSAl Viro err = ip6_append_data(sk, raw6_getfrag, &rfv, 93826879da5SWei Wang len, 0, &ipc6, &fl6, (struct rt6_info *)dst, 9395fdaa88dSWillem de Bruijn msg->msg_flags); 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds if (err) 9421da177e4SLinus Torvalds ip6_flush_pending_frames(sk); 9431da177e4SLinus Torvalds else if (!(msg->msg_flags & MSG_MORE)) 9444c9483b2SDavid S. Miller err = rawv6_push_pending_frames(sk, &fl6, rp); 9453ef9d943SYOSHIFUJI Hideaki release_sock(sk); 9461da177e4SLinus Torvalds } 9471da177e4SLinus Torvalds done: 9486d3e85ecSNicolas DICHTEL dst_release(dst); 9491da177e4SLinus Torvalds out: 9501da177e4SLinus Torvalds fl6_sock_release(flowlabel); 95145f6fad8SEric Dumazet txopt_put(opt_to_free); 9521da177e4SLinus Torvalds return err < 0 ? err : len; 9531da177e4SLinus Torvalds do_confirm: 9540dec879fSJulian Anastasov if (msg->msg_flags & MSG_PROBE) 9550dec879fSJulian Anastasov dst_confirm_neigh(dst, &fl6.daddr); 9561da177e4SLinus Torvalds if (!(msg->msg_flags & MSG_PROBE) || len) 9571da177e4SLinus Torvalds goto back_from_confirm; 9581da177e4SLinus Torvalds err = 0; 9591da177e4SLinus Torvalds goto done; 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds static int rawv6_seticmpfilter(struct sock *sk, int level, int optname, 9631da177e4SLinus Torvalds char __user *optval, int optlen) 9641da177e4SLinus Torvalds { 9651da177e4SLinus Torvalds switch (optname) { 9661da177e4SLinus Torvalds case ICMPV6_FILTER: 9671da177e4SLinus Torvalds if (optlen > sizeof(struct icmp6_filter)) 9681da177e4SLinus Torvalds optlen = sizeof(struct icmp6_filter); 9691da177e4SLinus Torvalds if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen)) 9701da177e4SLinus Torvalds return -EFAULT; 9711da177e4SLinus Torvalds return 0; 9721da177e4SLinus Torvalds default: 9731da177e4SLinus Torvalds return -ENOPROTOOPT; 9743ff50b79SStephen Hemminger } 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds return 0; 9771da177e4SLinus Torvalds } 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds static int rawv6_geticmpfilter(struct sock *sk, int level, int optname, 9801da177e4SLinus Torvalds char __user *optval, int __user *optlen) 9811da177e4SLinus Torvalds { 9821da177e4SLinus Torvalds int len; 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds switch (optname) { 9851da177e4SLinus Torvalds case ICMPV6_FILTER: 9861da177e4SLinus Torvalds if (get_user(len, optlen)) 9871da177e4SLinus Torvalds return -EFAULT; 9881da177e4SLinus Torvalds if (len < 0) 9891da177e4SLinus Torvalds return -EINVAL; 9901da177e4SLinus Torvalds if (len > sizeof(struct icmp6_filter)) 9911da177e4SLinus Torvalds len = sizeof(struct icmp6_filter); 9921da177e4SLinus Torvalds if (put_user(len, optlen)) 9931da177e4SLinus Torvalds return -EFAULT; 9941da177e4SLinus Torvalds if (copy_to_user(optval, &raw6_sk(sk)->filter, len)) 9951da177e4SLinus Torvalds return -EFAULT; 9961da177e4SLinus Torvalds return 0; 9971da177e4SLinus Torvalds default: 9981da177e4SLinus Torvalds return -ENOPROTOOPT; 9993ff50b79SStephen Hemminger } 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds return 0; 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds 10041da177e4SLinus Torvalds 10053fdadf7dSDmitry Mishin static int do_rawv6_setsockopt(struct sock *sk, int level, int optname, 1006b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 10071da177e4SLinus Torvalds { 10081da177e4SLinus Torvalds struct raw6_sock *rp = raw6_sk(sk); 10091da177e4SLinus Torvalds int val; 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds if (get_user(val, (int __user *)optval)) 10121da177e4SLinus Torvalds return -EFAULT; 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds switch (optname) { 1015715f504bSHannes Frederic Sowa case IPV6_HDRINCL: 1016715f504bSHannes Frederic Sowa if (sk->sk_type != SOCK_RAW) 1017715f504bSHannes Frederic Sowa return -EINVAL; 1018715f504bSHannes Frederic Sowa inet_sk(sk)->hdrincl = !!val; 1019715f504bSHannes Frederic Sowa return 0; 10201da177e4SLinus Torvalds case IPV6_CHECKSUM: 1021c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num == IPPROTO_ICMPV6 && 10221a98d05fSYOSHIFUJI Hideaki level == IPPROTO_IPV6) { 10231a98d05fSYOSHIFUJI Hideaki /* 10241a98d05fSYOSHIFUJI Hideaki * RFC3542 tells that IPV6_CHECKSUM socket 10251a98d05fSYOSHIFUJI Hideaki * option in the IPPROTO_IPV6 level is not 10261a98d05fSYOSHIFUJI Hideaki * allowed on ICMPv6 sockets. 10271a98d05fSYOSHIFUJI Hideaki * If you want to set it, use IPPROTO_RAW 10281a98d05fSYOSHIFUJI Hideaki * level IPV6_CHECKSUM socket option 10291a98d05fSYOSHIFUJI Hideaki * (Linux extension). 10301a98d05fSYOSHIFUJI Hideaki */ 10311a98d05fSYOSHIFUJI Hideaki return -EINVAL; 10321a98d05fSYOSHIFUJI Hideaki } 10331a98d05fSYOSHIFUJI Hideaki 10341da177e4SLinus Torvalds /* You may get strange result with a positive odd offset; 10351da177e4SLinus Torvalds RFC2292bis agrees with me. */ 10361da177e4SLinus Torvalds if (val > 0 && (val&1)) 1037a02cec21SEric Dumazet return -EINVAL; 10381da177e4SLinus Torvalds if (val < 0) { 10391da177e4SLinus Torvalds rp->checksum = 0; 10401da177e4SLinus Torvalds } else { 10411da177e4SLinus Torvalds rp->checksum = 1; 10421da177e4SLinus Torvalds rp->offset = val; 10431da177e4SLinus Torvalds } 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds return 0; 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds default: 1048a02cec21SEric Dumazet return -ENOPROTOOPT; 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds } 10511da177e4SLinus Torvalds 10523fdadf7dSDmitry Mishin static int rawv6_setsockopt(struct sock *sk, int level, int optname, 1053b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 10541da177e4SLinus Torvalds { 10551da177e4SLinus Torvalds switch (level) { 10561da177e4SLinus Torvalds case SOL_RAW: 10571da177e4SLinus Torvalds break; 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds case SOL_ICMPV6: 1060c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6) 10611da177e4SLinus Torvalds return -EOPNOTSUPP; 1062207ec0abSJoe Perches return rawv6_seticmpfilter(sk, level, optname, optval, optlen); 10631da177e4SLinus Torvalds case SOL_IPV6: 1064715f504bSHannes Frederic Sowa if (optname == IPV6_CHECKSUM || 1065715f504bSHannes Frederic Sowa optname == IPV6_HDRINCL) 10661da177e4SLinus Torvalds break; 1067275757e6SGustavo A. R. Silva /* fall through */ 10681da177e4SLinus Torvalds default: 1069207ec0abSJoe Perches return ipv6_setsockopt(sk, level, optname, optval, optlen); 10703ff50b79SStephen Hemminger } 10713ff50b79SStephen Hemminger 10723fdadf7dSDmitry Mishin return do_rawv6_setsockopt(sk, level, optname, optval, optlen); 10733fdadf7dSDmitry Mishin } 10743fdadf7dSDmitry Mishin 10753fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 10763fdadf7dSDmitry Mishin static int compat_rawv6_setsockopt(struct sock *sk, int level, int optname, 1077b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 10783fdadf7dSDmitry Mishin { 10793fdadf7dSDmitry Mishin switch (level) { 10803fdadf7dSDmitry Mishin case SOL_RAW: 10813fdadf7dSDmitry Mishin break; 10823fdadf7dSDmitry Mishin case SOL_ICMPV6: 1083c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6) 10843fdadf7dSDmitry Mishin return -EOPNOTSUPP; 1085543d9cfeSArnaldo Carvalho de Melo return rawv6_seticmpfilter(sk, level, optname, optval, optlen); 10863fdadf7dSDmitry Mishin case SOL_IPV6: 1087715f504bSHannes Frederic Sowa if (optname == IPV6_CHECKSUM || 1088715f504bSHannes Frederic Sowa optname == IPV6_HDRINCL) 10893fdadf7dSDmitry Mishin break; 1090275757e6SGustavo A. R. Silva /* fall through */ 10913fdadf7dSDmitry Mishin default: 1092543d9cfeSArnaldo Carvalho de Melo return compat_ipv6_setsockopt(sk, level, optname, 1093543d9cfeSArnaldo Carvalho de Melo optval, optlen); 10943ff50b79SStephen Hemminger } 10953fdadf7dSDmitry Mishin return do_rawv6_setsockopt(sk, level, optname, optval, optlen); 10963fdadf7dSDmitry Mishin } 10973fdadf7dSDmitry Mishin #endif 10983fdadf7dSDmitry Mishin 10993fdadf7dSDmitry Mishin static int do_rawv6_getsockopt(struct sock *sk, int level, int optname, 11003fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 11013fdadf7dSDmitry Mishin { 11023fdadf7dSDmitry Mishin struct raw6_sock *rp = raw6_sk(sk); 11033fdadf7dSDmitry Mishin int val, len; 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds if (get_user(len, optlen)) 11061da177e4SLinus Torvalds return -EFAULT; 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds switch (optname) { 1109715f504bSHannes Frederic Sowa case IPV6_HDRINCL: 1110715f504bSHannes Frederic Sowa val = inet_sk(sk)->hdrincl; 1111715f504bSHannes Frederic Sowa break; 11121da177e4SLinus Torvalds case IPV6_CHECKSUM: 11131a98d05fSYOSHIFUJI Hideaki /* 11141a98d05fSYOSHIFUJI Hideaki * We allow getsockopt() for IPPROTO_IPV6-level 11151a98d05fSYOSHIFUJI Hideaki * IPV6_CHECKSUM socket option on ICMPv6 sockets 11161a98d05fSYOSHIFUJI Hideaki * since RFC3542 is silent about it. 11171a98d05fSYOSHIFUJI Hideaki */ 11181da177e4SLinus Torvalds if (rp->checksum == 0) 11191da177e4SLinus Torvalds val = -1; 11201da177e4SLinus Torvalds else 11211da177e4SLinus Torvalds val = rp->offset; 11221da177e4SLinus Torvalds break; 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds default: 11251da177e4SLinus Torvalds return -ENOPROTOOPT; 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds len = min_t(unsigned int, sizeof(int), len); 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds if (put_user(len, optlen)) 11311da177e4SLinus Torvalds return -EFAULT; 11321da177e4SLinus Torvalds if (copy_to_user(optval, &val, len)) 11331da177e4SLinus Torvalds return -EFAULT; 11341da177e4SLinus Torvalds return 0; 11351da177e4SLinus Torvalds } 11361da177e4SLinus Torvalds 11373fdadf7dSDmitry Mishin static int rawv6_getsockopt(struct sock *sk, int level, int optname, 11383fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 11393fdadf7dSDmitry Mishin { 11403fdadf7dSDmitry Mishin switch (level) { 11413fdadf7dSDmitry Mishin case SOL_RAW: 11423fdadf7dSDmitry Mishin break; 11433fdadf7dSDmitry Mishin 11443fdadf7dSDmitry Mishin case SOL_ICMPV6: 1145c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6) 11463fdadf7dSDmitry Mishin return -EOPNOTSUPP; 1147207ec0abSJoe Perches return rawv6_geticmpfilter(sk, level, optname, optval, optlen); 11483fdadf7dSDmitry Mishin case SOL_IPV6: 1149715f504bSHannes Frederic Sowa if (optname == IPV6_CHECKSUM || 1150715f504bSHannes Frederic Sowa optname == IPV6_HDRINCL) 11513fdadf7dSDmitry Mishin break; 1152275757e6SGustavo A. R. Silva /* fall through */ 11533fdadf7dSDmitry Mishin default: 1154207ec0abSJoe Perches return ipv6_getsockopt(sk, level, optname, optval, optlen); 11553ff50b79SStephen Hemminger } 11563ff50b79SStephen Hemminger 11573fdadf7dSDmitry Mishin return do_rawv6_getsockopt(sk, level, optname, optval, optlen); 11583fdadf7dSDmitry Mishin } 11593fdadf7dSDmitry Mishin 11603fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 11613fdadf7dSDmitry Mishin static int compat_rawv6_getsockopt(struct sock *sk, int level, int optname, 11623fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 11633fdadf7dSDmitry Mishin { 11643fdadf7dSDmitry Mishin switch (level) { 11653fdadf7dSDmitry Mishin case SOL_RAW: 11663fdadf7dSDmitry Mishin break; 11673fdadf7dSDmitry Mishin case SOL_ICMPV6: 1168c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6) 11693fdadf7dSDmitry Mishin return -EOPNOTSUPP; 1170543d9cfeSArnaldo Carvalho de Melo return rawv6_geticmpfilter(sk, level, optname, optval, optlen); 11713fdadf7dSDmitry Mishin case SOL_IPV6: 1172715f504bSHannes Frederic Sowa if (optname == IPV6_CHECKSUM || 1173715f504bSHannes Frederic Sowa optname == IPV6_HDRINCL) 11743fdadf7dSDmitry Mishin break; 1175275757e6SGustavo A. R. Silva /* fall through */ 11763fdadf7dSDmitry Mishin default: 1177543d9cfeSArnaldo Carvalho de Melo return compat_ipv6_getsockopt(sk, level, optname, 1178543d9cfeSArnaldo Carvalho de Melo optval, optlen); 11793ff50b79SStephen Hemminger } 11803fdadf7dSDmitry Mishin return do_rawv6_getsockopt(sk, level, optname, optval, optlen); 11813fdadf7dSDmitry Mishin } 11823fdadf7dSDmitry Mishin #endif 11833fdadf7dSDmitry Mishin 11841da177e4SLinus Torvalds static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg) 11851da177e4SLinus Torvalds { 11861da177e4SLinus Torvalds switch (cmd) { 1187207ec0abSJoe Perches case SIOCOUTQ: { 118831e6d363SEric Dumazet int amount = sk_wmem_alloc_get(sk); 118931e6d363SEric Dumazet 11901da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 11911da177e4SLinus Torvalds } 1192207ec0abSJoe Perches case SIOCINQ: { 11931da177e4SLinus Torvalds struct sk_buff *skb; 11941da177e4SLinus Torvalds int amount = 0; 11951da177e4SLinus Torvalds 1196e0f9f858SHerbert Xu spin_lock_bh(&sk->sk_receive_queue.lock); 11971da177e4SLinus Torvalds skb = skb_peek(&sk->sk_receive_queue); 119853b24b8fSIan Morris if (skb) 1199105f5528SJamie Bainbridge amount = skb->len; 1200e0f9f858SHerbert Xu spin_unlock_bh(&sk->sk_receive_queue.lock); 12011da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 12021da177e4SLinus Torvalds } 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds default: 12057bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 12067bc570c8SYOSHIFUJI Hideaki return ip6mr_ioctl(sk, cmd, (void __user *)arg); 12077bc570c8SYOSHIFUJI Hideaki #else 12081da177e4SLinus Torvalds return -ENOIOCTLCMD; 12097bc570c8SYOSHIFUJI Hideaki #endif 12101da177e4SLinus Torvalds } 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds 1213e2d57766SDavid S. Miller #ifdef CONFIG_COMPAT 1214e2d57766SDavid S. Miller static int compat_rawv6_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg) 1215e2d57766SDavid S. Miller { 1216e2d57766SDavid S. Miller switch (cmd) { 1217e2d57766SDavid S. Miller case SIOCOUTQ: 1218e2d57766SDavid S. Miller case SIOCINQ: 1219e2d57766SDavid S. Miller return -ENOIOCTLCMD; 1220e2d57766SDavid S. Miller default: 1221e2d57766SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 1222e2d57766SDavid S. Miller return ip6mr_compat_ioctl(sk, cmd, compat_ptr(arg)); 1223e2d57766SDavid S. Miller #else 1224e2d57766SDavid S. Miller return -ENOIOCTLCMD; 1225e2d57766SDavid S. Miller #endif 1226e2d57766SDavid S. Miller } 1227e2d57766SDavid S. Miller } 1228e2d57766SDavid S. Miller #endif 1229e2d57766SDavid S. Miller 12301da177e4SLinus Torvalds static void rawv6_close(struct sock *sk, long timeout) 12311da177e4SLinus Torvalds { 1232c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num == IPPROTO_RAW) 1233725a8ff0SDenis V. Lunev ip6_ra_control(sk, -1); 12347bc570c8SYOSHIFUJI Hideaki ip6mr_sk_done(sk); 12351da177e4SLinus Torvalds sk_common_release(sk); 12361da177e4SLinus Torvalds } 12371da177e4SLinus Torvalds 12387d06b2e0SBrian Haley static void raw6_destroy(struct sock *sk) 123922dd4850SDenis V. Lunev { 124022dd4850SDenis V. Lunev lock_sock(sk); 124122dd4850SDenis V. Lunev ip6_flush_pending_frames(sk); 124222dd4850SDenis V. Lunev release_sock(sk); 1243f23d60deSDavid S. Miller 12447d06b2e0SBrian Haley inet6_destroy_sock(sk); 124522dd4850SDenis V. Lunev } 124622dd4850SDenis V. Lunev 12471da177e4SLinus Torvalds static int rawv6_init_sk(struct sock *sk) 12481da177e4SLinus Torvalds { 12491da177e4SLinus Torvalds struct raw6_sock *rp = raw6_sk(sk); 1250f48d5ff1SMasahide NAKAMURA 1251c720c7e8SEric Dumazet switch (inet_sk(sk)->inet_num) { 1252f48d5ff1SMasahide NAKAMURA case IPPROTO_ICMPV6: 12531da177e4SLinus Torvalds rp->checksum = 1; 12541da177e4SLinus Torvalds rp->offset = 2; 1255f48d5ff1SMasahide NAKAMURA break; 1256f48d5ff1SMasahide NAKAMURA case IPPROTO_MH: 1257f48d5ff1SMasahide NAKAMURA rp->checksum = 1; 1258f48d5ff1SMasahide NAKAMURA rp->offset = 4; 1259f48d5ff1SMasahide NAKAMURA break; 1260f48d5ff1SMasahide NAKAMURA default: 1261f48d5ff1SMasahide NAKAMURA break; 12621da177e4SLinus Torvalds } 1263a02cec21SEric Dumazet return 0; 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds struct proto rawv6_prot = { 12671da177e4SLinus Torvalds .name = "RAWv6", 12681da177e4SLinus Torvalds .owner = THIS_MODULE, 12691da177e4SLinus Torvalds .close = rawv6_close, 127022dd4850SDenis V. Lunev .destroy = raw6_destroy, 127182b276cdSHannes Frederic Sowa .connect = ip6_datagram_connect_v6_only, 1272286c72deSEric Dumazet .disconnect = __udp_disconnect, 12731da177e4SLinus Torvalds .ioctl = rawv6_ioctl, 12741da177e4SLinus Torvalds .init = rawv6_init_sk, 12751da177e4SLinus Torvalds .setsockopt = rawv6_setsockopt, 12761da177e4SLinus Torvalds .getsockopt = rawv6_getsockopt, 12771da177e4SLinus Torvalds .sendmsg = rawv6_sendmsg, 12781da177e4SLinus Torvalds .recvmsg = rawv6_recvmsg, 12791da177e4SLinus Torvalds .bind = rawv6_bind, 12801da177e4SLinus Torvalds .backlog_rcv = rawv6_rcv_skb, 1281fc8717baSPavel Emelyanov .hash = raw_hash_sk, 1282fc8717baSPavel Emelyanov .unhash = raw_unhash_sk, 12831da177e4SLinus Torvalds .obj_size = sizeof(struct raw6_sock), 12848c2bc895SDavid Windsor .useroffset = offsetof(struct raw6_sock, filter), 12858c2bc895SDavid Windsor .usersize = sizeof_field(struct raw6_sock, filter), 1286fc8717baSPavel Emelyanov .h.raw_hash = &raw_v6_hashinfo, 1287543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 1288543d9cfeSArnaldo Carvalho de Melo .compat_setsockopt = compat_rawv6_setsockopt, 1289543d9cfeSArnaldo Carvalho de Melo .compat_getsockopt = compat_rawv6_getsockopt, 1290e2d57766SDavid S. Miller .compat_ioctl = compat_rawv6_ioctl, 1291543d9cfeSArnaldo Carvalho de Melo #endif 1292432490f9SCyrill Gorcunov .diag_destroy = raw_abort, 12931da177e4SLinus Torvalds }; 12941da177e4SLinus Torvalds 12951da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 12961da177e4SLinus Torvalds static int raw6_seq_show(struct seq_file *seq, void *v) 12971da177e4SLinus Torvalds { 129817ef66afSLorenzo Colitti if (v == SEQ_START_TOKEN) { 129917ef66afSLorenzo Colitti seq_puts(seq, IPV6_SEQ_DGRAM_HEADER); 130017ef66afSLorenzo Colitti } else { 130117ef66afSLorenzo Colitti struct sock *sp = v; 130217ef66afSLorenzo Colitti __u16 srcp = inet_sk(sp)->inet_num; 130317ef66afSLorenzo Colitti ip6_dgram_sock_seq_show(seq, v, srcp, 0, 130417ef66afSLorenzo Colitti raw_seq_private(seq)->bucket); 130517ef66afSLorenzo Colitti } 13061da177e4SLinus Torvalds return 0; 13071da177e4SLinus Torvalds } 13081da177e4SLinus Torvalds 130956b3d975SPhilippe De Muyter static const struct seq_operations raw6_seq_ops = { 131042a73808SPavel Emelyanov .start = raw_seq_start, 131142a73808SPavel Emelyanov .next = raw_seq_next, 131242a73808SPavel Emelyanov .stop = raw_seq_stop, 13131da177e4SLinus Torvalds .show = raw6_seq_show, 13141da177e4SLinus Torvalds }; 13151da177e4SLinus Torvalds 13162c8c1e72SAlexey Dobriyan static int __net_init raw6_init_net(struct net *net) 1317a308da16SPavel Emelyanov { 1318c3506372SChristoph Hellwig if (!proc_create_net_data("raw6", 0444, net->proc_net, &raw6_seq_ops, 1319c3506372SChristoph Hellwig sizeof(struct raw_iter_state), &raw_v6_hashinfo)) 1320a308da16SPavel Emelyanov return -ENOMEM; 1321a308da16SPavel Emelyanov 1322a308da16SPavel Emelyanov return 0; 1323a308da16SPavel Emelyanov } 1324a308da16SPavel Emelyanov 13252c8c1e72SAlexey Dobriyan static void __net_exit raw6_exit_net(struct net *net) 1326a308da16SPavel Emelyanov { 1327ece31ffdSGao feng remove_proc_entry("raw6", net->proc_net); 1328a308da16SPavel Emelyanov } 1329a308da16SPavel Emelyanov 1330a308da16SPavel Emelyanov static struct pernet_operations raw6_net_ops = { 1331a308da16SPavel Emelyanov .init = raw6_init_net, 1332a308da16SPavel Emelyanov .exit = raw6_exit_net, 1333a308da16SPavel Emelyanov }; 1334a308da16SPavel Emelyanov 13351da177e4SLinus Torvalds int __init raw6_proc_init(void) 13361da177e4SLinus Torvalds { 1337a308da16SPavel Emelyanov return register_pernet_subsys(&raw6_net_ops); 13381da177e4SLinus Torvalds } 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds void raw6_proc_exit(void) 13411da177e4SLinus Torvalds { 1342a308da16SPavel Emelyanov unregister_pernet_subsys(&raw6_net_ops); 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 13457f4e4868SDaniel Lezcano 1346a11e1d43SLinus Torvalds /* Same as inet6_dgram_ops, sans udp_poll. */ 134777d4b1d3SEric Dumazet const struct proto_ops inet6_sockraw_ops = { 13487f4e4868SDaniel Lezcano .family = PF_INET6, 13497f4e4868SDaniel Lezcano .owner = THIS_MODULE, 13507f4e4868SDaniel Lezcano .release = inet6_release, 13517f4e4868SDaniel Lezcano .bind = inet6_bind, 13527f4e4868SDaniel Lezcano .connect = inet_dgram_connect, /* ok */ 13537f4e4868SDaniel Lezcano .socketpair = sock_no_socketpair, /* a do nothing */ 13547f4e4868SDaniel Lezcano .accept = sock_no_accept, /* a do nothing */ 13557f4e4868SDaniel Lezcano .getname = inet6_getname, 1356a11e1d43SLinus Torvalds .poll = datagram_poll, /* ok */ 13577f4e4868SDaniel Lezcano .ioctl = inet6_ioctl, /* must change */ 13587f4e4868SDaniel Lezcano .listen = sock_no_listen, /* ok */ 13597f4e4868SDaniel Lezcano .shutdown = inet_shutdown, /* ok */ 13607f4e4868SDaniel Lezcano .setsockopt = sock_common_setsockopt, /* ok */ 13617f4e4868SDaniel Lezcano .getsockopt = sock_common_getsockopt, /* ok */ 13627f4e4868SDaniel Lezcano .sendmsg = inet_sendmsg, /* ok */ 13637f4e4868SDaniel Lezcano .recvmsg = sock_common_recvmsg, /* ok */ 13647f4e4868SDaniel Lezcano .mmap = sock_no_mmap, 13657f4e4868SDaniel Lezcano .sendpage = sock_no_sendpage, 13667f4e4868SDaniel Lezcano #ifdef CONFIG_COMPAT 13677f4e4868SDaniel Lezcano .compat_setsockopt = compat_sock_common_setsockopt, 13687f4e4868SDaniel Lezcano .compat_getsockopt = compat_sock_common_getsockopt, 13697f4e4868SDaniel Lezcano #endif 13707f4e4868SDaniel Lezcano }; 13717f4e4868SDaniel Lezcano 13727f4e4868SDaniel Lezcano static struct inet_protosw rawv6_protosw = { 13737f4e4868SDaniel Lezcano .type = SOCK_RAW, 13747f4e4868SDaniel Lezcano .protocol = IPPROTO_IP, /* wild card */ 13757f4e4868SDaniel Lezcano .prot = &rawv6_prot, 13767f4e4868SDaniel Lezcano .ops = &inet6_sockraw_ops, 13777f4e4868SDaniel Lezcano .flags = INET_PROTOSW_REUSE, 13787f4e4868SDaniel Lezcano }; 13797f4e4868SDaniel Lezcano 13807f4e4868SDaniel Lezcano int __init rawv6_init(void) 13817f4e4868SDaniel Lezcano { 13823d2f6d41SJulia Lawall return inet6_register_protosw(&rawv6_protosw); 13837f4e4868SDaniel Lezcano } 13847f4e4868SDaniel Lezcano 138509f7709fSDaniel Lezcano void rawv6_exit(void) 13867f4e4868SDaniel Lezcano { 13877f4e4868SDaniel Lezcano inet6_unregister_protosw(&rawv6_protosw); 13887f4e4868SDaniel Lezcano } 1389