12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * RAW sockets for IPv6 41da177e4SLinus Torvalds * Linux INET6 implementation 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Authors: 71da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Adapted from linux/net/ipv4/raw.c 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Fixes: 121da177e4SLinus Torvalds * Hideaki YOSHIFUJI : sin6_scope_id support 131da177e4SLinus Torvalds * YOSHIFUJI,H.@USAGI : raw checksum (RFC2292(bis) compliance) 141da177e4SLinus Torvalds * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <linux/errno.h> 181da177e4SLinus Torvalds #include <linux/types.h> 191da177e4SLinus Torvalds #include <linux/socket.h> 205a0e3ad6STejun Heo #include <linux/slab.h> 211da177e4SLinus Torvalds #include <linux/sockios.h> 221da177e4SLinus Torvalds #include <linux/net.h> 231da177e4SLinus Torvalds #include <linux/in6.h> 241da177e4SLinus Torvalds #include <linux/netdevice.h> 251da177e4SLinus Torvalds #include <linux/if_arp.h> 261da177e4SLinus Torvalds #include <linux/icmpv6.h> 271da177e4SLinus Torvalds #include <linux/netfilter.h> 281da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 293305b80cSHerbert Xu #include <linux/skbuff.h> 30e2d57766SDavid S. Miller #include <linux/compat.h> 3129778becSAlex W Slater #include <linux/uaccess.h> 321da177e4SLinus Torvalds #include <asm/ioctls.h> 331da177e4SLinus Torvalds 34457c4cbcSEric W. Biederman #include <net/net_namespace.h> 351da177e4SLinus Torvalds #include <net/ip.h> 361da177e4SLinus Torvalds #include <net/sock.h> 371da177e4SLinus Torvalds #include <net/snmp.h> 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #include <net/ipv6.h> 401da177e4SLinus Torvalds #include <net/ndisc.h> 411da177e4SLinus Torvalds #include <net/protocol.h> 421da177e4SLinus Torvalds #include <net/ip6_route.h> 431da177e4SLinus Torvalds #include <net/ip6_checksum.h> 441da177e4SLinus Torvalds #include <net/addrconf.h> 451da177e4SLinus Torvalds #include <net/transp_v6.h> 461da177e4SLinus Torvalds #include <net/udp.h> 471da177e4SLinus Torvalds #include <net/inet_common.h> 48c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h> 4907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 507be96f76SMasahide NAKAMURA #include <net/mip6.h> 517be96f76SMasahide NAKAMURA #endif 527bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 531da177e4SLinus Torvalds 54b673e4dfSPavel Emelyanov #include <net/raw.h> 551da177e4SLinus Torvalds #include <net/rawv6.h> 561da177e4SLinus Torvalds #include <net/xfrm.h> 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds #include <linux/proc_fs.h> 591da177e4SLinus Torvalds #include <linux/seq_file.h> 60bc3b2d7fSPaul Gortmaker #include <linux/export.h> 611da177e4SLinus Torvalds 62d1c53c8eSWerner Almesberger #define ICMPV6_HDRLEN 4 /* ICMPv6 header, RFC 4443 Section 2.1 */ 63d1c53c8eSWerner Almesberger 640daf07e5SEric Dumazet struct raw_hashinfo raw_v6_hashinfo; 65432490f9SCyrill Gorcunov EXPORT_SYMBOL_GPL(raw_v6_hashinfo); 661da177e4SLinus Torvalds 67db6af4fdSEric Dumazet bool raw_v6_match(struct net *net, const struct sock *sk, unsigned short num, 68ba44f818SEric Dumazet const struct in6_addr *loc_addr, 695108ab4bSDavid Ahern const struct in6_addr *rmt_addr, int dif, int sdif) 701da177e4SLinus Torvalds { 71ba44f818SEric Dumazet if (inet_sk(sk)->inet_num != num || 72ba44f818SEric Dumazet !net_eq(sock_net(sk), net) || 73ba44f818SEric Dumazet (!ipv6_addr_any(&sk->sk_v6_daddr) && 74ba44f818SEric Dumazet !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) || 75ba44f818SEric Dumazet !raw_sk_bound_dev_eq(net, sk->sk_bound_dev_if, 767055420fSDuncan Eastoe dif, sdif)) 77ba44f818SEric Dumazet return false; 780bd1b59bSAndrew McDonald 79ba44f818SEric Dumazet if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) || 80ba44f818SEric Dumazet ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr) || 81ba44f818SEric Dumazet (ipv6_addr_is_multicast(loc_addr) && 82ba44f818SEric Dumazet inet6_mc_check(sk, loc_addr, rmt_addr))) 83ba44f818SEric Dumazet return true; 84ba44f818SEric Dumazet 85ba44f818SEric Dumazet return false; 861da177e4SLinus Torvalds } 87ba44f818SEric Dumazet EXPORT_SYMBOL_GPL(raw_v6_match); 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds /* 901da177e4SLinus Torvalds * 0 - deliver 911da177e4SLinus Torvalds * 1 - block 921da177e4SLinus Torvalds */ 931b05c4b5SEric Dumazet static int icmpv6_filter(const struct sock *sk, const struct sk_buff *skb) 941da177e4SLinus Torvalds { 959cc08af3SWerner Almesberger struct icmp6hdr _hdr; 961b05c4b5SEric Dumazet const struct icmp6hdr *hdr; 971da177e4SLinus Torvalds 98d1c53c8eSWerner Almesberger /* We require only the four bytes of the ICMPv6 header, not any 99d1c53c8eSWerner Almesberger * additional bytes of message body in "struct icmp6hdr". 100d1c53c8eSWerner Almesberger */ 1011b05c4b5SEric Dumazet hdr = skb_header_pointer(skb, skb_transport_offset(skb), 102d1c53c8eSWerner Almesberger ICMPV6_HDRLEN, &_hdr); 1031b05c4b5SEric Dumazet if (hdr) { 1041b05c4b5SEric Dumazet const __u32 *data = &raw6_sk(sk)->filter.data[0]; 1051b05c4b5SEric Dumazet unsigned int type = hdr->icmp6_type; 1061da177e4SLinus Torvalds 1071b05c4b5SEric Dumazet return (data[type >> 5] & (1U << (type & 31))) != 0; 1081da177e4SLinus Torvalds } 1091b05c4b5SEric Dumazet return 1; 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 11207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 113f2eda47dSEric Dumazet typedef int mh_filter_t(struct sock *sock, struct sk_buff *skb); 11459fbb3a6SMasahide NAKAMURA 115f2eda47dSEric Dumazet static mh_filter_t __rcu *mh_filter __read_mostly; 116f2eda47dSEric Dumazet 117f2eda47dSEric Dumazet int rawv6_mh_filter_register(mh_filter_t filter) 11859fbb3a6SMasahide NAKAMURA { 119cf778b00SEric Dumazet rcu_assign_pointer(mh_filter, filter); 12059fbb3a6SMasahide NAKAMURA return 0; 12159fbb3a6SMasahide NAKAMURA } 12259fbb3a6SMasahide NAKAMURA EXPORT_SYMBOL(rawv6_mh_filter_register); 12359fbb3a6SMasahide NAKAMURA 124f2eda47dSEric Dumazet int rawv6_mh_filter_unregister(mh_filter_t filter) 12559fbb3a6SMasahide NAKAMURA { 126a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(mh_filter, NULL); 12759fbb3a6SMasahide NAKAMURA synchronize_rcu(); 12859fbb3a6SMasahide NAKAMURA return 0; 12959fbb3a6SMasahide NAKAMURA } 13059fbb3a6SMasahide NAKAMURA EXPORT_SYMBOL(rawv6_mh_filter_unregister); 13159fbb3a6SMasahide NAKAMURA 13259fbb3a6SMasahide NAKAMURA #endif 13359fbb3a6SMasahide NAKAMURA 1341da177e4SLinus Torvalds /* 1351da177e4SLinus Torvalds * demultiplex raw sockets. 1361da177e4SLinus Torvalds * (should consider queueing the skb in the sock receive_queue 1371da177e4SLinus Torvalds * without calling rawv6.c) 1381da177e4SLinus Torvalds * 1391da177e4SLinus Torvalds * Caller owns SKB so we must make clones. 1401da177e4SLinus Torvalds */ 141a50feda5SEric Dumazet static bool ipv6_raw_deliver(struct sk_buff *skb, int nexthdr) 1421da177e4SLinus Torvalds { 143ba44f818SEric Dumazet struct net *net = dev_net(skb->dev); 1440daf07e5SEric Dumazet struct hlist_nulls_head *hlist; 1450daf07e5SEric Dumazet struct hlist_nulls_node *hnode; 146b71d1d42SEric Dumazet const struct in6_addr *saddr; 147b71d1d42SEric Dumazet const struct in6_addr *daddr; 1481da177e4SLinus Torvalds struct sock *sk; 149a50feda5SEric Dumazet bool delivered = false; 1501da177e4SLinus Torvalds __u8 hash; 1511da177e4SLinus Torvalds 1520660e03fSArnaldo Carvalho de Melo saddr = &ipv6_hdr(skb)->saddr; 1531da177e4SLinus Torvalds daddr = saddr + 1; 1541da177e4SLinus Torvalds 1556579f5baSEric Dumazet hash = raw_hashfunc(net, nexthdr); 1560daf07e5SEric Dumazet hlist = &raw_v6_hashinfo.ht[hash]; 1570daf07e5SEric Dumazet rcu_read_lock(); 158f289c02bSKuniyuki Iwashima sk_nulls_for_each(sk, hnode, hlist) { 1597be96f76SMasahide NAKAMURA int filtered; 1607be96f76SMasahide NAKAMURA 161ba44f818SEric Dumazet if (!raw_v6_match(net, sk, nexthdr, daddr, saddr, 162ba44f818SEric Dumazet inet6_iif(skb), inet6_sdif(skb))) 163ba44f818SEric Dumazet continue; 164a50feda5SEric Dumazet delivered = true; 1657be96f76SMasahide NAKAMURA switch (nexthdr) { 1667be96f76SMasahide NAKAMURA case IPPROTO_ICMPV6: 1677be96f76SMasahide NAKAMURA filtered = icmpv6_filter(sk, skb); 1687be96f76SMasahide NAKAMURA break; 16959fbb3a6SMasahide NAKAMURA 17007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 1717be96f76SMasahide NAKAMURA case IPPROTO_MH: 17259fbb3a6SMasahide NAKAMURA { 1737be96f76SMasahide NAKAMURA /* XXX: To validate MH only once for each packet, 1747be96f76SMasahide NAKAMURA * this is placed here. It should be after checking 1757be96f76SMasahide NAKAMURA * xfrm policy, however it doesn't. The checking xfrm 1767be96f76SMasahide NAKAMURA * policy is placed in rawv6_rcv() because it is 1777be96f76SMasahide NAKAMURA * required for each socket. 1787be96f76SMasahide NAKAMURA */ 179f2eda47dSEric Dumazet mh_filter_t *filter; 18059fbb3a6SMasahide NAKAMURA 18159fbb3a6SMasahide NAKAMURA filter = rcu_dereference(mh_filter); 182f2eda47dSEric Dumazet filtered = filter ? (*filter)(sk, skb) : 0; 1837be96f76SMasahide NAKAMURA break; 18459fbb3a6SMasahide NAKAMURA } 1857be96f76SMasahide NAKAMURA #endif 1867be96f76SMasahide NAKAMURA default: 1877be96f76SMasahide NAKAMURA filtered = 0; 1887be96f76SMasahide NAKAMURA break; 1897be96f76SMasahide NAKAMURA } 1907be96f76SMasahide NAKAMURA 1917be96f76SMasahide NAKAMURA if (filtered < 0) 1927be96f76SMasahide NAKAMURA break; 1937be96f76SMasahide NAKAMURA if (filtered == 0) { 1941da177e4SLinus Torvalds struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC); 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds /* Not releasing hash table! */ 197*b0e214d2SMadhu Koriginja if (clone) 1981da177e4SLinus Torvalds rawv6_rcv(sk, clone); 1991da177e4SLinus Torvalds } 2009fb9cbb1SYasuyuki Kozakai } 2010daf07e5SEric Dumazet rcu_read_unlock(); 202d13964f4SPatrick McHardy return delivered; 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 205a50feda5SEric Dumazet bool raw6_local_deliver(struct sk_buff *skb, int nexthdr) 20669d6da0bSPavel Emelyanov { 207ba44f818SEric Dumazet return ipv6_raw_deliver(skb, nexthdr); 20869d6da0bSPavel Emelyanov } 20969d6da0bSPavel Emelyanov 2101da177e4SLinus Torvalds /* This cleans up af_inet6 a bit. -DaveM */ 2111da177e4SLinus Torvalds static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len) 2121da177e4SLinus Torvalds { 2131da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 2141da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 2151da177e4SLinus Torvalds struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr; 216e69a4adcSAl Viro __be32 v4addr = 0; 2171da177e4SLinus Torvalds int addr_type; 2181da177e4SLinus Torvalds int err; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds if (addr_len < SIN6_LEN_RFC2133) 2211da177e4SLinus Torvalds return -EINVAL; 22282b276cdSHannes Frederic Sowa 22382b276cdSHannes Frederic Sowa if (addr->sin6_family != AF_INET6) 22482b276cdSHannes Frederic Sowa return -EINVAL; 22582b276cdSHannes Frederic Sowa 2261da177e4SLinus Torvalds addr_type = ipv6_addr_type(&addr->sin6_addr); 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds /* Raw sockets are IPv6 only */ 2291da177e4SLinus Torvalds if (addr_type == IPV6_ADDR_MAPPED) 230fd5c0027SEric Dumazet return -EADDRNOTAVAIL; 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds lock_sock(sk); 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds err = -EINVAL; 2351da177e4SLinus Torvalds if (sk->sk_state != TCP_CLOSE) 2361da177e4SLinus Torvalds goto out; 2371da177e4SLinus Torvalds 238fd5c0027SEric Dumazet rcu_read_lock(); 2391da177e4SLinus Torvalds /* Check if the address belongs to the host. */ 2401da177e4SLinus Torvalds if (addr_type != IPV6_ADDR_ANY) { 2411da177e4SLinus Torvalds struct net_device *dev = NULL; 2421da177e4SLinus Torvalds 243842df073SHannes Frederic Sowa if (__ipv6_addr_needs_scope_id(addr_type)) { 2441da177e4SLinus Torvalds if (addr_len >= sizeof(struct sockaddr_in6) && 2451da177e4SLinus Torvalds addr->sin6_scope_id) { 2461da177e4SLinus Torvalds /* Override any existing binding, if another 2471da177e4SLinus Torvalds * one is supplied by user. 2481da177e4SLinus Torvalds */ 2491da177e4SLinus Torvalds sk->sk_bound_dev_if = addr->sin6_scope_id; 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds /* Binding to link-local address requires an interface */ 2531da177e4SLinus Torvalds if (!sk->sk_bound_dev_if) 254fd5c0027SEric Dumazet goto out_unlock; 25572f7cfabSMike Manning } 2561da177e4SLinus Torvalds 25772f7cfabSMike Manning if (sk->sk_bound_dev_if) { 2581da177e4SLinus Torvalds err = -ENODEV; 259fd5c0027SEric Dumazet dev = dev_get_by_index_rcu(sock_net(sk), 260fd5c0027SEric Dumazet sk->sk_bound_dev_if); 261fd5c0027SEric Dumazet if (!dev) 262fd5c0027SEric Dumazet goto out_unlock; 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds /* ipv4 addr of the socket is invalid. Only the 2661da177e4SLinus Torvalds * unspecified and mapped address have a v4 equivalent. 2671da177e4SLinus Torvalds */ 2681da177e4SLinus Torvalds v4addr = LOOPBACK4_IPV6; 26935a256feSTom Herbert if (!(addr_type & IPV6_ADDR_MULTICAST) && 270630e4576SMaciej Żenczykowski !ipv6_can_nonlocal_bind(sock_net(sk), inet)) { 2711da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 2723b1e0a65SYOSHIFUJI Hideaki if (!ipv6_chk_addr(sock_net(sk), &addr->sin6_addr, 273bfeade08SDaniel Lezcano dev, 0)) { 274fd5c0027SEric Dumazet goto out_unlock; 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds 279c720c7e8SEric Dumazet inet->inet_rcv_saddr = inet->inet_saddr = v4addr; 280efe4208fSEric Dumazet sk->sk_v6_rcv_saddr = addr->sin6_addr; 2811da177e4SLinus Torvalds if (!(addr_type & IPV6_ADDR_MULTICAST)) 2824e3fd7a0SAlexey Dobriyan np->saddr = addr->sin6_addr; 2831da177e4SLinus Torvalds err = 0; 284fd5c0027SEric Dumazet out_unlock: 285fd5c0027SEric Dumazet rcu_read_unlock(); 2861da177e4SLinus Torvalds out: 2871da177e4SLinus Torvalds release_sock(sk); 2881da177e4SLinus Torvalds return err; 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds 29169d6da0bSPavel Emelyanov static void rawv6_err(struct sock *sk, struct sk_buff *skb, 2921da177e4SLinus Torvalds struct inet6_skb_parm *opt, 293d5fdd6baSBrian Haley u8 type, u8 code, int offset, __be32 info) 2941da177e4SLinus Torvalds { 2951da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 2961da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 2971da177e4SLinus Torvalds int err; 2981da177e4SLinus Torvalds int harderr; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds /* Report error on raw socket, if: 3011da177e4SLinus Torvalds 1. User requested recverr. 3021da177e4SLinus Torvalds 2. Socket is connected (otherwise the error indication 3031da177e4SLinus Torvalds is useless without recverr and error is hard. 3041da177e4SLinus Torvalds */ 3051da177e4SLinus Torvalds if (!np->recverr && sk->sk_state != TCP_ESTABLISHED) 3061da177e4SLinus Torvalds return; 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds harderr = icmpv6_err_convert(type, code, &err); 30981aded24SDavid S. Miller if (type == ICMPV6_PKT_TOOBIG) { 31081aded24SDavid S. Miller ip6_sk_update_pmtu(skb, sk, info); 3111da177e4SLinus Torvalds harderr = (np->pmtudisc == IPV6_PMTUDISC_DO); 31281aded24SDavid S. Miller } 3138d65b119SDuan Jiong if (type == NDISC_REDIRECT) { 314ec18d9a2SDavid S. Miller ip6_sk_redirect(skb, sk); 3158d65b119SDuan Jiong return; 3168d65b119SDuan Jiong } 3171da177e4SLinus Torvalds if (np->recverr) { 3181da177e4SLinus Torvalds u8 *payload = skb->data; 3191da177e4SLinus Torvalds if (!inet->hdrincl) 3201da177e4SLinus Torvalds payload += offset; 3211da177e4SLinus Torvalds ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload); 3221da177e4SLinus Torvalds } 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds if (np->recverr || harderr) { 3251da177e4SLinus Torvalds sk->sk_err = err; 326e3ae2365SAlexander Aring sk_error_report(sk); 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds } 3291da177e4SLinus Torvalds 33069d6da0bSPavel Emelyanov void raw6_icmp_error(struct sk_buff *skb, int nexthdr, 331d5fdd6baSBrian Haley u8 type, u8 code, int inner_offset, __be32 info) 33269d6da0bSPavel Emelyanov { 333ba44f818SEric Dumazet struct net *net = dev_net(skb->dev); 3340daf07e5SEric Dumazet struct hlist_nulls_head *hlist; 3350daf07e5SEric Dumazet struct hlist_nulls_node *hnode; 33669d6da0bSPavel Emelyanov struct sock *sk; 33769d6da0bSPavel Emelyanov int hash; 33869d6da0bSPavel Emelyanov 3396579f5baSEric Dumazet hash = raw_hashfunc(net, nexthdr); 3400daf07e5SEric Dumazet hlist = &raw_v6_hashinfo.ht[hash]; 3410daf07e5SEric Dumazet rcu_read_lock(); 342f289c02bSKuniyuki Iwashima sk_nulls_for_each(sk, hnode, hlist) { 34305f175cdSYOSHIFUJI Hideaki /* Note: ipv6_hdr(skb) != skb->data */ 344b71d1d42SEric Dumazet const struct ipv6hdr *ip6h = (const struct ipv6hdr *)skb->data; 34569d6da0bSPavel Emelyanov 346ba44f818SEric Dumazet if (!raw_v6_match(net, sk, nexthdr, &ip6h->saddr, &ip6h->daddr, 347ba44f818SEric Dumazet inet6_iif(skb), inet6_iif(skb))) 348ba44f818SEric Dumazet continue; 349ba44f818SEric Dumazet rawv6_err(sk, skb, NULL, type, code, inner_offset, info); 35069d6da0bSPavel Emelyanov } 3510daf07e5SEric Dumazet rcu_read_unlock(); 35269d6da0bSPavel Emelyanov } 35369d6da0bSPavel Emelyanov 3541da177e4SLinus Torvalds static inline int rawv6_rcv_skb(struct sock *sk, struct sk_buff *skb) 3551da177e4SLinus Torvalds { 3568d8ebd77SEric Dumazet enum skb_drop_reason reason; 3578d8ebd77SEric Dumazet 35833d480ceSEric Dumazet if ((raw6_sk(sk)->checksum || rcu_access_pointer(sk->sk_filter)) && 359fb286bb2SHerbert Xu skb_checksum_complete(skb)) { 360a92aa318SWang Chen atomic_inc(&sk->sk_drops); 3618d8ebd77SEric Dumazet kfree_skb_reason(skb, SKB_DROP_REASON_SKB_CSUM); 3623cc76caaSYang Hongyang return NET_RX_DROP; 3631da177e4SLinus Torvalds } 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds /* Charge it to the socket. */ 366d826eb14SEric Dumazet skb_dst_drop(skb); 3678d8ebd77SEric Dumazet if (sock_queue_rcv_skb_reason(sk, skb, &reason) < 0) { 3688d8ebd77SEric Dumazet kfree_skb_reason(skb, reason); 3693cc76caaSYang Hongyang return NET_RX_DROP; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds return 0; 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds /* 3761da177e4SLinus Torvalds * This is next to useless... 3771da177e4SLinus Torvalds * if we demultiplex in network layer we don't need the extra call 3781da177e4SLinus Torvalds * just to queue the skb... 3791da177e4SLinus Torvalds * maybe we could have the network decide upon a hint if it 3801da177e4SLinus Torvalds * should call raw_rcv for demultiplexing 3811da177e4SLinus Torvalds */ 3821da177e4SLinus Torvalds int rawv6_rcv(struct sock *sk, struct sk_buff *skb) 3831da177e4SLinus Torvalds { 3841da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 3851da177e4SLinus Torvalds struct raw6_sock *rp = raw6_sk(sk); 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) { 388a92aa318SWang Chen atomic_inc(&sk->sk_drops); 3898d8ebd77SEric Dumazet kfree_skb_reason(skb, SKB_DROP_REASON_XFRM_POLICY); 3901da177e4SLinus Torvalds return NET_RX_DROP; 3911da177e4SLinus Torvalds } 392*b0e214d2SMadhu Koriginja nf_reset_ct(skb); 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds if (!rp->checksum) 3951da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_UNNECESSARY; 3961da177e4SLinus Torvalds 39784fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_COMPLETE) { 398d56f90a7SArnaldo Carvalho de Melo skb_postpull_rcsum(skb, skb_network_header(skb), 399cfe1fc77SArnaldo Carvalho de Melo skb_network_header_len(skb)); 4000660e03fSArnaldo Carvalho de Melo if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 4010660e03fSArnaldo Carvalho de Melo &ipv6_hdr(skb)->daddr, 402c720c7e8SEric Dumazet skb->len, inet->inet_num, skb->csum)) 403fb286bb2SHerbert Xu skb->ip_summed = CHECKSUM_UNNECESSARY; 4041da177e4SLinus Torvalds } 40560476372SHerbert Xu if (!skb_csum_unnecessary(skb)) 4060660e03fSArnaldo Carvalho de Melo skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 4070660e03fSArnaldo Carvalho de Melo &ipv6_hdr(skb)->daddr, 4080660e03fSArnaldo Carvalho de Melo skb->len, 409c720c7e8SEric Dumazet inet->inet_num, 0)); 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds if (inet->hdrincl) { 412fb286bb2SHerbert Xu if (skb_checksum_complete(skb)) { 413a92aa318SWang Chen atomic_inc(&sk->sk_drops); 4148d8ebd77SEric Dumazet kfree_skb_reason(skb, SKB_DROP_REASON_SKB_CSUM); 4153cc76caaSYang Hongyang return NET_RX_DROP; 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds rawv6_rcv_skb(sk, skb); 4201da177e4SLinus Torvalds return 0; 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds /* 4251da177e4SLinus Torvalds * This should be easy, if there is something there 4261da177e4SLinus Torvalds * we return it, otherwise we block. 4271da177e4SLinus Torvalds */ 4281da177e4SLinus Torvalds 4291b784140SYing Xue static int rawv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 430ec095263SOliver Hartkopp int flags, int *addr_len) 4311da177e4SLinus Torvalds { 4321da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 433342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 4341da177e4SLinus Torvalds struct sk_buff *skb; 4351da177e4SLinus Torvalds size_t copied; 4361da177e4SLinus Torvalds int err; 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds if (flags & MSG_OOB) 4391da177e4SLinus Torvalds return -EOPNOTSUPP; 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds if (flags & MSG_ERRQUEUE) 44285fbaa75SHannes Frederic Sowa return ipv6_recv_error(sk, msg, len, addr_len); 4431da177e4SLinus Torvalds 4444b340ae2SBrian Haley if (np->rxpmtu && np->rxopt.bits.rxpmtu) 44585fbaa75SHannes Frederic Sowa return ipv6_recv_rxpmtu(sk, msg, len, addr_len); 4464b340ae2SBrian Haley 447f4b41f06SOliver Hartkopp skb = skb_recv_datagram(sk, flags, &err); 4481da177e4SLinus Torvalds if (!skb) 4491da177e4SLinus Torvalds goto out; 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds copied = skb->len; 4521da177e4SLinus Torvalds if (copied > len) { 4531da177e4SLinus Torvalds copied = len; 4541da177e4SLinus Torvalds msg->msg_flags |= MSG_TRUNC; 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds 45760476372SHerbert Xu if (skb_csum_unnecessary(skb)) { 45851f3d02bSDavid S. Miller err = skb_copy_datagram_msg(skb, 0, msg, copied); 4591da177e4SLinus Torvalds } else if (msg->msg_flags&MSG_TRUNC) { 460fb286bb2SHerbert Xu if (__skb_checksum_complete(skb)) 4611da177e4SLinus Torvalds goto csum_copy_err; 46251f3d02bSDavid S. Miller err = skb_copy_datagram_msg(skb, 0, msg, copied); 4631da177e4SLinus Torvalds } else { 464227158dbSAl Viro err = skb_copy_and_csum_datagram_msg(skb, 0, msg); 4651da177e4SLinus Torvalds if (err == -EINVAL) 4661da177e4SLinus Torvalds goto csum_copy_err; 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds if (err) 4691da177e4SLinus Torvalds goto out_free; 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds /* Copy the address. */ 4721da177e4SLinus Torvalds if (sin6) { 4731da177e4SLinus Torvalds sin6->sin6_family = AF_INET6; 474f59fc7f3STetsuo Handa sin6->sin6_port = 0; 4754e3fd7a0SAlexey Dobriyan sin6->sin6_addr = ipv6_hdr(skb)->saddr; 4761da177e4SLinus Torvalds sin6->sin6_flowinfo = 0; 477842df073SHannes Frederic Sowa sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr, 4784330487aSDuan Jiong inet6_iif(skb)); 479bceaa902SHannes Frederic Sowa *addr_len = sizeof(*sin6); 4801da177e4SLinus Torvalds } 4811da177e4SLinus Torvalds 4826fd1d51cSErin MacNeil sock_recv_cmsgs(msg, sk, skb); 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds if (np->rxopt.all) 48573df66f8STom Parkin ip6_datagram_recv_ctl(sk, msg, skb); 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds err = copied; 4881da177e4SLinus Torvalds if (flags & MSG_TRUNC) 4891da177e4SLinus Torvalds err = skb->len; 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds out_free: 4921da177e4SLinus Torvalds skb_free_datagram(sk, skb); 4931da177e4SLinus Torvalds out: 4941da177e4SLinus Torvalds return err; 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds csum_copy_err: 4973305b80cSHerbert Xu skb_kill_datagram(sk, skb, flags); 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds /* Error for blocking case is chosen to masquerade 5001da177e4SLinus Torvalds as some normal condition. 5011da177e4SLinus Torvalds */ 5021da177e4SLinus Torvalds err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH; 5033305b80cSHerbert Xu goto out; 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds 5064c9483b2SDavid S. Miller static int rawv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6, 507357b40a1SHerbert Xu struct raw6_sock *rp) 5081da177e4SLinus Torvalds { 509cb3e9864SHerbert Xu struct ipv6_txoptions *opt; 5101da177e4SLinus Torvalds struct sk_buff *skb; 5111da177e4SLinus Torvalds int err = 0; 512357b40a1SHerbert Xu int offset; 513357b40a1SHerbert Xu int len; 514679a8738SHerbert Xu int total_len; 515868c86bcSAl Viro __wsum tmp_csum; 516868c86bcSAl Viro __sum16 csum; 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds if (!rp->checksum) 5191da177e4SLinus Torvalds goto send; 5201da177e4SLinus Torvalds 52143728fa5SFabian Frederick skb = skb_peek(&sk->sk_write_queue); 52243728fa5SFabian Frederick if (!skb) 5231da177e4SLinus Torvalds goto out; 5241da177e4SLinus Torvalds 525357b40a1SHerbert Xu offset = rp->offset; 526299b0767SSteffen Klassert total_len = inet_sk(sk)->cork.base.length; 527cb3e9864SHerbert Xu opt = inet6_sk(sk)->cork.opt; 528cb3e9864SHerbert Xu total_len -= opt ? opt->opt_flen : 0; 529cb3e9864SHerbert Xu 530679a8738SHerbert Xu if (offset >= total_len - 1) { 5311da177e4SLinus Torvalds err = -EINVAL; 532357b40a1SHerbert Xu ip6_flush_pending_frames(sk); 5331da177e4SLinus Torvalds goto out; 5341da177e4SLinus Torvalds } 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds /* should be check HW csum miyazawa */ 5371da177e4SLinus Torvalds if (skb_queue_len(&sk->sk_write_queue) == 1) { 5381da177e4SLinus Torvalds /* 5391da177e4SLinus Torvalds * Only one fragment on the socket. 5401da177e4SLinus Torvalds */ 5411da177e4SLinus Torvalds tmp_csum = skb->csum; 5421da177e4SLinus Torvalds } else { 543357b40a1SHerbert Xu struct sk_buff *csum_skb = NULL; 5441da177e4SLinus Torvalds tmp_csum = 0; 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds skb_queue_walk(&sk->sk_write_queue, skb) { 5471da177e4SLinus Torvalds tmp_csum = csum_add(tmp_csum, skb->csum); 548357b40a1SHerbert Xu 549357b40a1SHerbert Xu if (csum_skb) 550357b40a1SHerbert Xu continue; 551357b40a1SHerbert Xu 552ea2ae17dSArnaldo Carvalho de Melo len = skb->len - skb_transport_offset(skb); 553357b40a1SHerbert Xu if (offset >= len) { 554357b40a1SHerbert Xu offset -= len; 555357b40a1SHerbert Xu continue; 5561da177e4SLinus Torvalds } 557357b40a1SHerbert Xu 558357b40a1SHerbert Xu csum_skb = skb; 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 561357b40a1SHerbert Xu skb = csum_skb; 562357b40a1SHerbert Xu } 563357b40a1SHerbert Xu 564ea2ae17dSArnaldo Carvalho de Melo offset += skb_transport_offset(skb); 565a98f9175SDave Jones err = skb_copy_bits(skb, offset, &csum, 2); 566a98f9175SDave Jones if (err < 0) { 567a98f9175SDave Jones ip6_flush_pending_frames(sk); 568a98f9175SDave Jones goto out; 569a98f9175SDave Jones } 570357b40a1SHerbert Xu 5711da177e4SLinus Torvalds /* in case cksum was not initialized */ 572357b40a1SHerbert Xu if (unlikely(csum)) 5735f92a738SAl Viro tmp_csum = csum_sub(tmp_csum, csum_unfold(csum)); 5741da177e4SLinus Torvalds 5754c9483b2SDavid S. Miller csum = csum_ipv6_magic(&fl6->saddr, &fl6->daddr, 5764c9483b2SDavid S. Miller total_len, fl6->flowi6_proto, tmp_csum); 5771da177e4SLinus Torvalds 5784c9483b2SDavid S. Miller if (csum == 0 && fl6->flowi6_proto == IPPROTO_UDP) 579f6ab0288SAl Viro csum = CSUM_MANGLED_0; 580357b40a1SHerbert Xu 5818242fc33SHimangi Saraogi BUG_ON(skb_store_bits(skb, offset, &csum, 2)); 582357b40a1SHerbert Xu 5831da177e4SLinus Torvalds send: 5841da177e4SLinus Torvalds err = ip6_push_pending_frames(sk); 5851da177e4SLinus Torvalds out: 5861da177e4SLinus Torvalds return err; 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds 589c3c1a7dbSAl Viro static int rawv6_send_hdrinc(struct sock *sk, struct msghdr *msg, int length, 5904c9483b2SDavid S. Miller struct flowi6 *fl6, struct dst_entry **dstp, 591a818f75eSJesus Sanchez-Palencia unsigned int flags, const struct sockcm_cookie *sockc) 5921da177e4SLinus Torvalds { 5933320da89SHerbert Xu struct ipv6_pinfo *np = inet6_sk(sk); 594adb28c9dSEric W. Biederman struct net *net = sock_net(sk); 5951da177e4SLinus Torvalds struct ipv6hdr *iph; 5961da177e4SLinus Torvalds struct sk_buff *skb; 5971da177e4SLinus Torvalds int err; 5981789a640SEric Dumazet struct rt6_info *rt = (struct rt6_info *)*dstp; 599a7ae1992SHerbert Xu int hlen = LL_RESERVED_SPACE(rt->dst.dev); 600a7ae1992SHerbert Xu int tlen = rt->dst.dev->needed_tailroom; 6011da177e4SLinus Torvalds 602d8d1f30bSChangli Gao if (length > rt->dst.dev->mtu) { 6034c9483b2SDavid S. Miller ipv6_local_error(sk, EMSGSIZE, fl6, rt->dst.dev->mtu); 6041da177e4SLinus Torvalds return -EMSGSIZE; 6051da177e4SLinus Torvalds } 60686f4c90aSAlexander Potapenko if (length < sizeof(struct ipv6hdr)) 60786f4c90aSAlexander Potapenko return -EINVAL; 6081da177e4SLinus Torvalds if (flags&MSG_PROBE) 6091da177e4SLinus Torvalds goto out; 6101da177e4SLinus Torvalds 611f5184d26SJohannes Berg skb = sock_alloc_send_skb(sk, 612a7ae1992SHerbert Xu length + hlen + tlen + 15, 6131da177e4SLinus Torvalds flags & MSG_DONTWAIT, &err); 61463159f29SIan Morris if (!skb) 6151da177e4SLinus Torvalds goto error; 616a7ae1992SHerbert Xu skb_reserve(skb, hlen); 6171da177e4SLinus Torvalds 6189c9c9ad5SHannes Frederic Sowa skb->protocol = htons(ETH_P_IPV6); 6191da177e4SLinus Torvalds skb->priority = sk->sk_priority; 620c6af0c22SWillem de Bruijn skb->mark = sockc->mark; 621a818f75eSJesus Sanchez-Palencia skb->tstamp = sockc->transmit_time; 6221da177e4SLinus Torvalds 6231ced98e8SArnaldo Carvalho de Melo skb_put(skb, length); 6241ced98e8SArnaldo Carvalho de Melo skb_reset_network_header(skb); 6250660e03fSArnaldo Carvalho de Melo iph = ipv6_hdr(skb); 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 6281da177e4SLinus Torvalds 6298f932f76SWillem de Bruijn skb_setup_tx_timestamp(skb, sockc->tsflags); 630fbfb2321SWillem de Bruijn 6310dec879fSJulian Anastasov if (flags & MSG_CONFIRM) 6320dec879fSJulian Anastasov skb_set_dst_pending_confirm(skb, 1); 6330dec879fSJulian Anastasov 634b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 63521226abbSAl Viro err = memcpy_from_msg(iph, msg, length); 636a688caa3SWei Wang if (err) { 637a688caa3SWei Wang err = -EFAULT; 638a688caa3SWei Wang kfree_skb(skb); 639a688caa3SWei Wang goto error; 640a688caa3SWei Wang } 641a688caa3SWei Wang 642a688caa3SWei Wang skb_dst_set(skb, &rt->dst); 643a688caa3SWei Wang *dstp = NULL; 6441da177e4SLinus Torvalds 645a8e3e1a9SDavid Ahern /* if egress device is enslaved to an L3 master device pass the 646a8e3e1a9SDavid Ahern * skb to its handler for processing 647a8e3e1a9SDavid Ahern */ 648a8e3e1a9SDavid Ahern skb = l3mdev_ip6_out(sk, skb); 649a8e3e1a9SDavid Ahern if (unlikely(!skb)) 650a8e3e1a9SDavid Ahern return 0; 651a8e3e1a9SDavid Ahern 652a688caa3SWei Wang /* Acquire rcu_read_lock() in case we need to use rt->rt6i_idev 653a688caa3SWei Wang * in the error path. Since skb has been freed, the dst could 654a688caa3SWei Wang * have been queued for deletion. 655a688caa3SWei Wang */ 656a688caa3SWei Wang rcu_read_lock(); 657adb28c9dSEric W. Biederman IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len); 65829a26a56SEric W. Biederman err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb, 65913206b6bSEric W. Biederman NULL, rt->dst.dev, dst_output); 6601da177e4SLinus Torvalds if (err > 0) 6616ce9e7b5SEric Dumazet err = net_xmit_errno(err); 662a688caa3SWei Wang if (err) { 663a688caa3SWei Wang IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS); 664a688caa3SWei Wang rcu_read_unlock(); 665a688caa3SWei Wang goto error_check; 666a688caa3SWei Wang } 667a688caa3SWei Wang rcu_read_unlock(); 6681da177e4SLinus Torvalds out: 6691da177e4SLinus Torvalds return 0; 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds error: 672adb28c9dSEric W. Biederman IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS); 673a688caa3SWei Wang error_check: 6746ce9e7b5SEric Dumazet if (err == -ENOBUFS && !np->recverr) 6756ce9e7b5SEric Dumazet err = 0; 6761da177e4SLinus Torvalds return err; 6771da177e4SLinus Torvalds } 6781da177e4SLinus Torvalds 67919e3c66bSAl Viro struct raw6_frag_vec { 68019e3c66bSAl Viro struct msghdr *msg; 68119e3c66bSAl Viro int hlen; 68219e3c66bSAl Viro char c[4]; 68319e3c66bSAl Viro }; 68419e3c66bSAl Viro 68519e3c66bSAl Viro static int rawv6_probe_proto_opt(struct raw6_frag_vec *rfv, struct flowi6 *fl6) 6861da177e4SLinus Torvalds { 68719e3c66bSAl Viro int err = 0; 6884c9483b2SDavid S. Miller switch (fl6->flowi6_proto) { 6891da177e4SLinus Torvalds case IPPROTO_ICMPV6: 69019e3c66bSAl Viro rfv->hlen = 2; 69119e3c66bSAl Viro err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen); 69219e3c66bSAl Viro if (!err) { 69319e3c66bSAl Viro fl6->fl6_icmp_type = rfv->c[0]; 69419e3c66bSAl Viro fl6->fl6_icmp_code = rfv->c[1]; 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds break; 6976e8f4d48SMasahide NAKAMURA case IPPROTO_MH: 69819e3c66bSAl Viro rfv->hlen = 4; 69919e3c66bSAl Viro err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen); 70019e3c66bSAl Viro if (!err) 70119e3c66bSAl Viro fl6->fl6_mh_type = rfv->c[2]; 70219e3c66bSAl Viro } 70319e3c66bSAl Viro return err; 70419e3c66bSAl Viro } 7056e8f4d48SMasahide NAKAMURA 70619e3c66bSAl Viro static int raw6_getfrag(void *from, char *to, int offset, int len, int odd, 70719e3c66bSAl Viro struct sk_buff *skb) 70819e3c66bSAl Viro { 70919e3c66bSAl Viro struct raw6_frag_vec *rfv = from; 71019e3c66bSAl Viro 71119e3c66bSAl Viro if (offset < rfv->hlen) { 71219e3c66bSAl Viro int copy = min(rfv->hlen - offset, len); 71319e3c66bSAl Viro 71419e3c66bSAl Viro if (skb->ip_summed == CHECKSUM_PARTIAL) 71519e3c66bSAl Viro memcpy(to, rfv->c + offset, copy); 71619e3c66bSAl Viro else 71719e3c66bSAl Viro skb->csum = csum_block_add( 71819e3c66bSAl Viro skb->csum, 71919e3c66bSAl Viro csum_partial_copy_nocheck(rfv->c + offset, 720cc44c17bSAl Viro to, copy), 72119e3c66bSAl Viro odd); 72219e3c66bSAl Viro 72319e3c66bSAl Viro odd = 0; 72419e3c66bSAl Viro offset += copy; 72519e3c66bSAl Viro to += copy; 72619e3c66bSAl Viro len -= copy; 72719e3c66bSAl Viro 72819e3c66bSAl Viro if (!len) 729a27b58feSHeiko Carstens return 0; 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds 73219e3c66bSAl Viro offset -= rfv->hlen; 73319e3c66bSAl Viro 734f69e6d13SAl Viro return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb); 73519e3c66bSAl Viro } 73619e3c66bSAl Viro 7371b784140SYing Xue static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 7381da177e4SLinus Torvalds { 73945f6fad8SEric Dumazet struct ipv6_txoptions *opt_to_free = NULL; 7401da177e4SLinus Torvalds struct ipv6_txoptions opt_space; 741342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 74220c59de2SArnaud Ebalard struct in6_addr *daddr, *final_p, final; 7431da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 7441da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 7451da177e4SLinus Torvalds struct raw6_sock *rp = raw6_sk(sk); 7461da177e4SLinus Torvalds struct ipv6_txoptions *opt = NULL; 7471da177e4SLinus Torvalds struct ip6_flowlabel *flowlabel = NULL; 7481da177e4SLinus Torvalds struct dst_entry *dst = NULL; 74919e3c66bSAl Viro struct raw6_frag_vec rfv; 7504c9483b2SDavid S. Miller struct flowi6 fl6; 75126879da5SWei Wang struct ipcm6_cookie ipc6; 7521da177e4SLinus Torvalds int addr_len = msg->msg_namelen; 75359e3e4b5SOlivier Matz int hdrincl; 7541da177e4SLinus Torvalds u16 proto; 7551da177e4SLinus Torvalds int err; 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds /* Rough check on arithmetic overflow, 758b59e139bSYOSHIFUJI Hideaki better check is made in ip6_append_data(). 7591da177e4SLinus Torvalds */ 760b59e139bSYOSHIFUJI Hideaki if (len > INT_MAX) 7611da177e4SLinus Torvalds return -EMSGSIZE; 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds /* Mirror BSD error message compatibility */ 7641da177e4SLinus Torvalds if (msg->msg_flags & MSG_OOB) 7651da177e4SLinus Torvalds return -EOPNOTSUPP; 7661da177e4SLinus Torvalds 76759e3e4b5SOlivier Matz /* hdrincl should be READ_ONCE(inet->hdrincl) 76859e3e4b5SOlivier Matz * but READ_ONCE() doesn't work with bit fields. 76959e3e4b5SOlivier Matz * Doing this indirectly yields the same result. 77059e3e4b5SOlivier Matz */ 77159e3e4b5SOlivier Matz hdrincl = inet->hdrincl; 77259e3e4b5SOlivier Matz hdrincl = READ_ONCE(hdrincl); 77359e3e4b5SOlivier Matz 7741da177e4SLinus Torvalds /* 7751da177e4SLinus Torvalds * Get and verify the address. 7761da177e4SLinus Torvalds */ 7774c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 7781da177e4SLinus Torvalds 7794c9483b2SDavid S. Miller fl6.flowi6_mark = sk->sk_mark; 780e2d118a1SLorenzo Colitti fl6.flowi6_uid = sk->sk_uid; 7814a19ec58SLaszlo Attila Toth 782b515430aSWillem de Bruijn ipcm6_init(&ipc6); 7835fdaa88dSWillem de Bruijn ipc6.sockc.tsflags = sk->sk_tsflags; 784c6af0c22SWillem de Bruijn ipc6.sockc.mark = sk->sk_mark; 78526879da5SWei Wang 7861da177e4SLinus Torvalds if (sin6) { 7871da177e4SLinus Torvalds if (addr_len < SIN6_LEN_RFC2133) 7881da177e4SLinus Torvalds return -EINVAL; 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds if (sin6->sin6_family && sin6->sin6_family != AF_INET6) 791a02cec21SEric Dumazet return -EAFNOSUPPORT; 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds /* port is the proto value [0..255] carried in nexthdr */ 7941da177e4SLinus Torvalds proto = ntohs(sin6->sin6_port); 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds if (!proto) 797c720c7e8SEric Dumazet proto = inet->inet_num; 798c720c7e8SEric Dumazet else if (proto != inet->inet_num) 799a02cec21SEric Dumazet return -EINVAL; 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds if (proto > 255) 802a02cec21SEric Dumazet return -EINVAL; 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds daddr = &sin6->sin6_addr; 8051da177e4SLinus Torvalds if (np->sndflow) { 8064c9483b2SDavid S. Miller fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; 8074c9483b2SDavid S. Miller if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) { 8084c9483b2SDavid S. Miller flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 80959c820b2SWillem de Bruijn if (IS_ERR(flowlabel)) 8101da177e4SLinus Torvalds return -EINVAL; 8111da177e4SLinus Torvalds } 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds /* 8151da177e4SLinus Torvalds * Otherwise it will be difficult to maintain 8161da177e4SLinus Torvalds * sk->sk_dst_cache. 8171da177e4SLinus Torvalds */ 8181da177e4SLinus Torvalds if (sk->sk_state == TCP_ESTABLISHED && 819efe4208fSEric Dumazet ipv6_addr_equal(daddr, &sk->sk_v6_daddr)) 820efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds if (addr_len >= sizeof(struct sockaddr_in6) && 8231da177e4SLinus Torvalds sin6->sin6_scope_id && 824842df073SHannes Frederic Sowa __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr))) 8254c9483b2SDavid S. Miller fl6.flowi6_oif = sin6->sin6_scope_id; 8261da177e4SLinus Torvalds } else { 8271da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED) 8281da177e4SLinus Torvalds return -EDESTADDRREQ; 8291da177e4SLinus Torvalds 830c720c7e8SEric Dumazet proto = inet->inet_num; 831efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 8324c9483b2SDavid S. Miller fl6.flowlabel = np->flow_label; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 8354c9483b2SDavid S. Miller if (fl6.flowi6_oif == 0) 8364c9483b2SDavid S. Miller fl6.flowi6_oif = sk->sk_bound_dev_if; 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds if (msg->msg_controllen) { 8391da177e4SLinus Torvalds opt = &opt_space; 8401da177e4SLinus Torvalds memset(opt, 0, sizeof(struct ipv6_txoptions)); 8411da177e4SLinus Torvalds opt->tot_len = sizeof(struct ipv6_txoptions); 84226879da5SWei Wang ipc6.opt = opt; 8431da177e4SLinus Torvalds 8445fdaa88dSWillem de Bruijn err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6); 8451da177e4SLinus Torvalds if (err < 0) { 8461da177e4SLinus Torvalds fl6_sock_release(flowlabel); 8471da177e4SLinus Torvalds return err; 8481da177e4SLinus Torvalds } 8494c9483b2SDavid S. Miller if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { 8504c9483b2SDavid S. Miller flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 85159c820b2SWillem de Bruijn if (IS_ERR(flowlabel)) 8521da177e4SLinus Torvalds return -EINVAL; 8531da177e4SLinus Torvalds } 8541da177e4SLinus Torvalds if (!(opt->opt_nflen|opt->opt_flen)) 8551da177e4SLinus Torvalds opt = NULL; 8561da177e4SLinus Torvalds } 85745f6fad8SEric Dumazet if (!opt) { 85845f6fad8SEric Dumazet opt = txopt_get(np); 85945f6fad8SEric Dumazet opt_to_free = opt; 86045f6fad8SEric Dumazet } 861df9890c3SYOSHIFUJI Hideaki if (flowlabel) 8621da177e4SLinus Torvalds opt = fl6_merge_options(&opt_space, flowlabel, opt); 863df9890c3SYOSHIFUJI Hideaki opt = ipv6_fixup_options(&opt_space, opt); 8641da177e4SLinus Torvalds 8654c9483b2SDavid S. Miller fl6.flowi6_proto = proto; 866c6af0c22SWillem de Bruijn fl6.flowi6_mark = ipc6.sockc.mark; 867b9aa52c4SOlivier Matz 868b9aa52c4SOlivier Matz if (!hdrincl) { 86919e3c66bSAl Viro rfv.msg = msg; 87019e3c66bSAl Viro rfv.hlen = 0; 87119e3c66bSAl Viro err = rawv6_probe_proto_opt(&rfv, &fl6); 872a27b58feSHeiko Carstens if (err) 873a27b58feSHeiko Carstens goto out; 874b9aa52c4SOlivier Matz } 8751da177e4SLinus Torvalds 876876c7f41SBrian Haley if (!ipv6_addr_any(daddr)) 8774e3fd7a0SAlexey Dobriyan fl6.daddr = *daddr; 878876c7f41SBrian Haley else 8794c9483b2SDavid S. Miller fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */ 8804c9483b2SDavid S. Miller if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr)) 8814e3fd7a0SAlexey Dobriyan fl6.saddr = np->saddr; 8821da177e4SLinus Torvalds 8834c9483b2SDavid S. Miller final_p = fl6_update_dst(&fl6, opt, &final); 8841da177e4SLinus Torvalds 8854c9483b2SDavid S. Miller if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) 8864c9483b2SDavid S. Miller fl6.flowi6_oif = np->mcast_oif; 887c4062dfcSErich E. Hoover else if (!fl6.flowi6_oif) 888c4062dfcSErich E. Hoover fl6.flowi6_oif = np->ucast_oif; 8893df98d79SPaul Moore security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6)); 8901da177e4SLinus Torvalds 89159e3e4b5SOlivier Matz if (hdrincl) 89248e8aa6eSMartin KaFai Lau fl6.flowi6_flags |= FLOWI_FLAG_KNOWN_NH; 89348e8aa6eSMartin KaFai Lau 89438b7097bSHannes Frederic Sowa if (ipc6.tclass < 0) 89538b7097bSHannes Frederic Sowa ipc6.tclass = np->tclass; 89638b7097bSHannes Frederic Sowa 89738b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel); 89838b7097bSHannes Frederic Sowa 899c4e85f73SSabrina Dubroca dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p); 90068d0c6d3SDavid S. Miller if (IS_ERR(dst)) { 90168d0c6d3SDavid S. Miller err = PTR_ERR(dst); 9021da177e4SLinus Torvalds goto out; 90314e50e57SDavid S. Miller } 90426879da5SWei Wang if (ipc6.hlimit < 0) 90526879da5SWei Wang ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst); 9061da177e4SLinus Torvalds 90726879da5SWei Wang if (ipc6.dontfrag < 0) 90826879da5SWei Wang ipc6.dontfrag = np->dontfrag; 90913b52cd4SBrian Haley 9101da177e4SLinus Torvalds if (msg->msg_flags&MSG_CONFIRM) 9111da177e4SLinus Torvalds goto do_confirm; 9121da177e4SLinus Torvalds 9131da177e4SLinus Torvalds back_from_confirm: 91459e3e4b5SOlivier Matz if (hdrincl) 915a818f75eSJesus Sanchez-Palencia err = rawv6_send_hdrinc(sk, msg, len, &fl6, &dst, 9165fdaa88dSWillem de Bruijn msg->msg_flags, &ipc6.sockc); 9171789a640SEric Dumazet else { 91826879da5SWei Wang ipc6.opt = opt; 9191da177e4SLinus Torvalds lock_sock(sk); 92019e3c66bSAl Viro err = ip6_append_data(sk, raw6_getfrag, &rfv, 92126879da5SWei Wang len, 0, &ipc6, &fl6, (struct rt6_info *)dst, 9225fdaa88dSWillem de Bruijn msg->msg_flags); 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds if (err) 9251da177e4SLinus Torvalds ip6_flush_pending_frames(sk); 9261da177e4SLinus Torvalds else if (!(msg->msg_flags & MSG_MORE)) 9274c9483b2SDavid S. Miller err = rawv6_push_pending_frames(sk, &fl6, rp); 9283ef9d943SYOSHIFUJI Hideaki release_sock(sk); 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds done: 9316d3e85ecSNicolas DICHTEL dst_release(dst); 9321da177e4SLinus Torvalds out: 9331da177e4SLinus Torvalds fl6_sock_release(flowlabel); 93445f6fad8SEric Dumazet txopt_put(opt_to_free); 9351da177e4SLinus Torvalds return err < 0 ? err : len; 9361da177e4SLinus Torvalds do_confirm: 9370dec879fSJulian Anastasov if (msg->msg_flags & MSG_PROBE) 9380dec879fSJulian Anastasov dst_confirm_neigh(dst, &fl6.daddr); 9391da177e4SLinus Torvalds if (!(msg->msg_flags & MSG_PROBE) || len) 9401da177e4SLinus Torvalds goto back_from_confirm; 9411da177e4SLinus Torvalds err = 0; 9421da177e4SLinus Torvalds goto done; 9431da177e4SLinus Torvalds } 9441da177e4SLinus Torvalds 9451da177e4SLinus Torvalds static int rawv6_seticmpfilter(struct sock *sk, int level, int optname, 946a7b75c5aSChristoph Hellwig sockptr_t optval, int optlen) 9471da177e4SLinus Torvalds { 9481da177e4SLinus Torvalds switch (optname) { 9491da177e4SLinus Torvalds case ICMPV6_FILTER: 9501da177e4SLinus Torvalds if (optlen > sizeof(struct icmp6_filter)) 9511da177e4SLinus Torvalds optlen = sizeof(struct icmp6_filter); 952a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&raw6_sk(sk)->filter, optval, optlen)) 9531da177e4SLinus Torvalds return -EFAULT; 9541da177e4SLinus Torvalds return 0; 9551da177e4SLinus Torvalds default: 9561da177e4SLinus Torvalds return -ENOPROTOOPT; 9573ff50b79SStephen Hemminger } 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds return 0; 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds static int rawv6_geticmpfilter(struct sock *sk, int level, int optname, 9631da177e4SLinus Torvalds char __user *optval, int __user *optlen) 9641da177e4SLinus Torvalds { 9651da177e4SLinus Torvalds int len; 9661da177e4SLinus Torvalds 9671da177e4SLinus Torvalds switch (optname) { 9681da177e4SLinus Torvalds case ICMPV6_FILTER: 9691da177e4SLinus Torvalds if (get_user(len, optlen)) 9701da177e4SLinus Torvalds return -EFAULT; 9711da177e4SLinus Torvalds if (len < 0) 9721da177e4SLinus Torvalds return -EINVAL; 9731da177e4SLinus Torvalds if (len > sizeof(struct icmp6_filter)) 9741da177e4SLinus Torvalds len = sizeof(struct icmp6_filter); 9751da177e4SLinus Torvalds if (put_user(len, optlen)) 9761da177e4SLinus Torvalds return -EFAULT; 9771da177e4SLinus Torvalds if (copy_to_user(optval, &raw6_sk(sk)->filter, len)) 9781da177e4SLinus Torvalds return -EFAULT; 9791da177e4SLinus Torvalds return 0; 9801da177e4SLinus Torvalds default: 9811da177e4SLinus Torvalds return -ENOPROTOOPT; 9823ff50b79SStephen Hemminger } 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds return 0; 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds 9883fdadf7dSDmitry Mishin static int do_rawv6_setsockopt(struct sock *sk, int level, int optname, 989a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int optlen) 9901da177e4SLinus Torvalds { 9911da177e4SLinus Torvalds struct raw6_sock *rp = raw6_sk(sk); 9921da177e4SLinus Torvalds int val; 9931da177e4SLinus Torvalds 994fb7bc920STamir Duberstein if (optlen < sizeof(val)) 995fb7bc920STamir Duberstein return -EINVAL; 996fb7bc920STamir Duberstein 997a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&val, optval, sizeof(val))) 9981da177e4SLinus Torvalds return -EFAULT; 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds switch (optname) { 1001715f504bSHannes Frederic Sowa case IPV6_HDRINCL: 1002715f504bSHannes Frederic Sowa if (sk->sk_type != SOCK_RAW) 1003715f504bSHannes Frederic Sowa return -EINVAL; 1004715f504bSHannes Frederic Sowa inet_sk(sk)->hdrincl = !!val; 1005715f504bSHannes Frederic Sowa return 0; 10061da177e4SLinus Torvalds case IPV6_CHECKSUM: 1007c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num == IPPROTO_ICMPV6 && 10081a98d05fSYOSHIFUJI Hideaki level == IPPROTO_IPV6) { 10091a98d05fSYOSHIFUJI Hideaki /* 10101a98d05fSYOSHIFUJI Hideaki * RFC3542 tells that IPV6_CHECKSUM socket 10111a98d05fSYOSHIFUJI Hideaki * option in the IPPROTO_IPV6 level is not 10121a98d05fSYOSHIFUJI Hideaki * allowed on ICMPv6 sockets. 10131a98d05fSYOSHIFUJI Hideaki * If you want to set it, use IPPROTO_RAW 10141a98d05fSYOSHIFUJI Hideaki * level IPV6_CHECKSUM socket option 10151a98d05fSYOSHIFUJI Hideaki * (Linux extension). 10161a98d05fSYOSHIFUJI Hideaki */ 10171a98d05fSYOSHIFUJI Hideaki return -EINVAL; 10181a98d05fSYOSHIFUJI Hideaki } 10191a98d05fSYOSHIFUJI Hideaki 10201da177e4SLinus Torvalds /* You may get strange result with a positive odd offset; 10211da177e4SLinus Torvalds RFC2292bis agrees with me. */ 10221da177e4SLinus Torvalds if (val > 0 && (val&1)) 1023a02cec21SEric Dumazet return -EINVAL; 10241da177e4SLinus Torvalds if (val < 0) { 10251da177e4SLinus Torvalds rp->checksum = 0; 10261da177e4SLinus Torvalds } else { 10271da177e4SLinus Torvalds rp->checksum = 1; 10281da177e4SLinus Torvalds rp->offset = val; 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds return 0; 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds default: 1034a02cec21SEric Dumazet return -ENOPROTOOPT; 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 10383fdadf7dSDmitry Mishin static int rawv6_setsockopt(struct sock *sk, int level, int optname, 1039a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int optlen) 10401da177e4SLinus Torvalds { 10411da177e4SLinus Torvalds switch (level) { 10421da177e4SLinus Torvalds case SOL_RAW: 10431da177e4SLinus Torvalds break; 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds case SOL_ICMPV6: 1046c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6) 10471da177e4SLinus Torvalds return -EOPNOTSUPP; 1048207ec0abSJoe Perches return rawv6_seticmpfilter(sk, level, optname, optval, optlen); 10491da177e4SLinus Torvalds case SOL_IPV6: 1050715f504bSHannes Frederic Sowa if (optname == IPV6_CHECKSUM || 1051715f504bSHannes Frederic Sowa optname == IPV6_HDRINCL) 10521da177e4SLinus Torvalds break; 1053a8eceea8SJoe Perches fallthrough; 10541da177e4SLinus Torvalds default: 1055207ec0abSJoe Perches return ipv6_setsockopt(sk, level, optname, optval, optlen); 10563ff50b79SStephen Hemminger } 10573ff50b79SStephen Hemminger 10583fdadf7dSDmitry Mishin return do_rawv6_setsockopt(sk, level, optname, optval, optlen); 10593fdadf7dSDmitry Mishin } 10603fdadf7dSDmitry Mishin 10613fdadf7dSDmitry Mishin static int do_rawv6_getsockopt(struct sock *sk, int level, int optname, 10623fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 10633fdadf7dSDmitry Mishin { 10643fdadf7dSDmitry Mishin struct raw6_sock *rp = raw6_sk(sk); 10653fdadf7dSDmitry Mishin int val, len; 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds if (get_user(len, optlen)) 10681da177e4SLinus Torvalds return -EFAULT; 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds switch (optname) { 1071715f504bSHannes Frederic Sowa case IPV6_HDRINCL: 1072715f504bSHannes Frederic Sowa val = inet_sk(sk)->hdrincl; 1073715f504bSHannes Frederic Sowa break; 10741da177e4SLinus Torvalds case IPV6_CHECKSUM: 10751a98d05fSYOSHIFUJI Hideaki /* 10761a98d05fSYOSHIFUJI Hideaki * We allow getsockopt() for IPPROTO_IPV6-level 10771a98d05fSYOSHIFUJI Hideaki * IPV6_CHECKSUM socket option on ICMPv6 sockets 10781a98d05fSYOSHIFUJI Hideaki * since RFC3542 is silent about it. 10791a98d05fSYOSHIFUJI Hideaki */ 10801da177e4SLinus Torvalds if (rp->checksum == 0) 10811da177e4SLinus Torvalds val = -1; 10821da177e4SLinus Torvalds else 10831da177e4SLinus Torvalds val = rp->offset; 10841da177e4SLinus Torvalds break; 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds default: 10871da177e4SLinus Torvalds return -ENOPROTOOPT; 10881da177e4SLinus Torvalds } 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds len = min_t(unsigned int, sizeof(int), len); 10911da177e4SLinus Torvalds 10921da177e4SLinus Torvalds if (put_user(len, optlen)) 10931da177e4SLinus Torvalds return -EFAULT; 10941da177e4SLinus Torvalds if (copy_to_user(optval, &val, len)) 10951da177e4SLinus Torvalds return -EFAULT; 10961da177e4SLinus Torvalds return 0; 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds 10993fdadf7dSDmitry Mishin static int rawv6_getsockopt(struct sock *sk, int level, int optname, 11003fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 11013fdadf7dSDmitry Mishin { 11023fdadf7dSDmitry Mishin switch (level) { 11033fdadf7dSDmitry Mishin case SOL_RAW: 11043fdadf7dSDmitry Mishin break; 11053fdadf7dSDmitry Mishin 11063fdadf7dSDmitry Mishin case SOL_ICMPV6: 1107c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6) 11083fdadf7dSDmitry Mishin return -EOPNOTSUPP; 1109207ec0abSJoe Perches return rawv6_geticmpfilter(sk, level, optname, optval, optlen); 11103fdadf7dSDmitry Mishin case SOL_IPV6: 1111715f504bSHannes Frederic Sowa if (optname == IPV6_CHECKSUM || 1112715f504bSHannes Frederic Sowa optname == IPV6_HDRINCL) 11133fdadf7dSDmitry Mishin break; 1114a8eceea8SJoe Perches fallthrough; 11153fdadf7dSDmitry Mishin default: 1116207ec0abSJoe Perches return ipv6_getsockopt(sk, level, optname, optval, optlen); 11173ff50b79SStephen Hemminger } 11183ff50b79SStephen Hemminger 11193fdadf7dSDmitry Mishin return do_rawv6_getsockopt(sk, level, optname, optval, optlen); 11203fdadf7dSDmitry Mishin } 11213fdadf7dSDmitry Mishin 11221da177e4SLinus Torvalds static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg) 11231da177e4SLinus Torvalds { 11241da177e4SLinus Torvalds switch (cmd) { 1125207ec0abSJoe Perches case SIOCOUTQ: { 112631e6d363SEric Dumazet int amount = sk_wmem_alloc_get(sk); 112731e6d363SEric Dumazet 11281da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 11291da177e4SLinus Torvalds } 1130207ec0abSJoe Perches case SIOCINQ: { 11311da177e4SLinus Torvalds struct sk_buff *skb; 11321da177e4SLinus Torvalds int amount = 0; 11331da177e4SLinus Torvalds 1134e0f9f858SHerbert Xu spin_lock_bh(&sk->sk_receive_queue.lock); 11351da177e4SLinus Torvalds skb = skb_peek(&sk->sk_receive_queue); 113653b24b8fSIan Morris if (skb) 1137105f5528SJamie Bainbridge amount = skb->len; 1138e0f9f858SHerbert Xu spin_unlock_bh(&sk->sk_receive_queue.lock); 11391da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 11401da177e4SLinus Torvalds } 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds default: 11437bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 11447bc570c8SYOSHIFUJI Hideaki return ip6mr_ioctl(sk, cmd, (void __user *)arg); 11457bc570c8SYOSHIFUJI Hideaki #else 11461da177e4SLinus Torvalds return -ENOIOCTLCMD; 11477bc570c8SYOSHIFUJI Hideaki #endif 11481da177e4SLinus Torvalds } 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds 1151e2d57766SDavid S. Miller #ifdef CONFIG_COMPAT 1152e2d57766SDavid S. Miller static int compat_rawv6_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg) 1153e2d57766SDavid S. Miller { 1154e2d57766SDavid S. Miller switch (cmd) { 1155e2d57766SDavid S. Miller case SIOCOUTQ: 1156e2d57766SDavid S. Miller case SIOCINQ: 1157e2d57766SDavid S. Miller return -ENOIOCTLCMD; 1158e2d57766SDavid S. Miller default: 1159e2d57766SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 1160e2d57766SDavid S. Miller return ip6mr_compat_ioctl(sk, cmd, compat_ptr(arg)); 1161e2d57766SDavid S. Miller #else 1162e2d57766SDavid S. Miller return -ENOIOCTLCMD; 1163e2d57766SDavid S. Miller #endif 1164e2d57766SDavid S. Miller } 1165e2d57766SDavid S. Miller } 1166e2d57766SDavid S. Miller #endif 1167e2d57766SDavid S. Miller 11681da177e4SLinus Torvalds static void rawv6_close(struct sock *sk, long timeout) 11691da177e4SLinus Torvalds { 1170c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num == IPPROTO_RAW) 1171725a8ff0SDenis V. Lunev ip6_ra_control(sk, -1); 11727bc570c8SYOSHIFUJI Hideaki ip6mr_sk_done(sk); 11731da177e4SLinus Torvalds sk_common_release(sk); 11741da177e4SLinus Torvalds } 11751da177e4SLinus Torvalds 11767d06b2e0SBrian Haley static void raw6_destroy(struct sock *sk) 117722dd4850SDenis V. Lunev { 117822dd4850SDenis V. Lunev lock_sock(sk); 117922dd4850SDenis V. Lunev ip6_flush_pending_frames(sk); 118022dd4850SDenis V. Lunev release_sock(sk); 118122dd4850SDenis V. Lunev } 118222dd4850SDenis V. Lunev 11831da177e4SLinus Torvalds static int rawv6_init_sk(struct sock *sk) 11841da177e4SLinus Torvalds { 11851da177e4SLinus Torvalds struct raw6_sock *rp = raw6_sk(sk); 1186f48d5ff1SMasahide NAKAMURA 1187c720c7e8SEric Dumazet switch (inet_sk(sk)->inet_num) { 1188f48d5ff1SMasahide NAKAMURA case IPPROTO_ICMPV6: 11891da177e4SLinus Torvalds rp->checksum = 1; 11901da177e4SLinus Torvalds rp->offset = 2; 1191f48d5ff1SMasahide NAKAMURA break; 1192f48d5ff1SMasahide NAKAMURA case IPPROTO_MH: 1193f48d5ff1SMasahide NAKAMURA rp->checksum = 1; 1194f48d5ff1SMasahide NAKAMURA rp->offset = 4; 1195f48d5ff1SMasahide NAKAMURA break; 1196f48d5ff1SMasahide NAKAMURA default: 1197f48d5ff1SMasahide NAKAMURA break; 11981da177e4SLinus Torvalds } 1199a02cec21SEric Dumazet return 0; 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds 12021da177e4SLinus Torvalds struct proto rawv6_prot = { 12031da177e4SLinus Torvalds .name = "RAWv6", 12041da177e4SLinus Torvalds .owner = THIS_MODULE, 12051da177e4SLinus Torvalds .close = rawv6_close, 120622dd4850SDenis V. Lunev .destroy = raw6_destroy, 120782b276cdSHannes Frederic Sowa .connect = ip6_datagram_connect_v6_only, 1208286c72deSEric Dumazet .disconnect = __udp_disconnect, 12091da177e4SLinus Torvalds .ioctl = rawv6_ioctl, 12101da177e4SLinus Torvalds .init = rawv6_init_sk, 12111da177e4SLinus Torvalds .setsockopt = rawv6_setsockopt, 12121da177e4SLinus Torvalds .getsockopt = rawv6_getsockopt, 12131da177e4SLinus Torvalds .sendmsg = rawv6_sendmsg, 12141da177e4SLinus Torvalds .recvmsg = rawv6_recvmsg, 12151da177e4SLinus Torvalds .bind = rawv6_bind, 12161da177e4SLinus Torvalds .backlog_rcv = rawv6_rcv_skb, 1217fc8717baSPavel Emelyanov .hash = raw_hash_sk, 1218fc8717baSPavel Emelyanov .unhash = raw_unhash_sk, 12191da177e4SLinus Torvalds .obj_size = sizeof(struct raw6_sock), 12208c2bc895SDavid Windsor .useroffset = offsetof(struct raw6_sock, filter), 12218c2bc895SDavid Windsor .usersize = sizeof_field(struct raw6_sock, filter), 1222fc8717baSPavel Emelyanov .h.raw_hash = &raw_v6_hashinfo, 1223543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 1224e2d57766SDavid S. Miller .compat_ioctl = compat_rawv6_ioctl, 1225543d9cfeSArnaldo Carvalho de Melo #endif 1226432490f9SCyrill Gorcunov .diag_destroy = raw_abort, 12271da177e4SLinus Torvalds }; 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 12301da177e4SLinus Torvalds static int raw6_seq_show(struct seq_file *seq, void *v) 12311da177e4SLinus Torvalds { 123217ef66afSLorenzo Colitti if (v == SEQ_START_TOKEN) { 123317ef66afSLorenzo Colitti seq_puts(seq, IPV6_SEQ_DGRAM_HEADER); 123417ef66afSLorenzo Colitti } else { 123517ef66afSLorenzo Colitti struct sock *sp = v; 123617ef66afSLorenzo Colitti __u16 srcp = inet_sk(sp)->inet_num; 123717ef66afSLorenzo Colitti ip6_dgram_sock_seq_show(seq, v, srcp, 0, 123817ef66afSLorenzo Colitti raw_seq_private(seq)->bucket); 123917ef66afSLorenzo Colitti } 12401da177e4SLinus Torvalds return 0; 12411da177e4SLinus Torvalds } 12421da177e4SLinus Torvalds 124356b3d975SPhilippe De Muyter static const struct seq_operations raw6_seq_ops = { 124442a73808SPavel Emelyanov .start = raw_seq_start, 124542a73808SPavel Emelyanov .next = raw_seq_next, 124642a73808SPavel Emelyanov .stop = raw_seq_stop, 12471da177e4SLinus Torvalds .show = raw6_seq_show, 12481da177e4SLinus Torvalds }; 12491da177e4SLinus Torvalds 12502c8c1e72SAlexey Dobriyan static int __net_init raw6_init_net(struct net *net) 1251a308da16SPavel Emelyanov { 1252c3506372SChristoph Hellwig if (!proc_create_net_data("raw6", 0444, net->proc_net, &raw6_seq_ops, 1253c3506372SChristoph Hellwig sizeof(struct raw_iter_state), &raw_v6_hashinfo)) 1254a308da16SPavel Emelyanov return -ENOMEM; 1255a308da16SPavel Emelyanov 1256a308da16SPavel Emelyanov return 0; 1257a308da16SPavel Emelyanov } 1258a308da16SPavel Emelyanov 12592c8c1e72SAlexey Dobriyan static void __net_exit raw6_exit_net(struct net *net) 1260a308da16SPavel Emelyanov { 1261ece31ffdSGao feng remove_proc_entry("raw6", net->proc_net); 1262a308da16SPavel Emelyanov } 1263a308da16SPavel Emelyanov 1264a308da16SPavel Emelyanov static struct pernet_operations raw6_net_ops = { 1265a308da16SPavel Emelyanov .init = raw6_init_net, 1266a308da16SPavel Emelyanov .exit = raw6_exit_net, 1267a308da16SPavel Emelyanov }; 1268a308da16SPavel Emelyanov 12691da177e4SLinus Torvalds int __init raw6_proc_init(void) 12701da177e4SLinus Torvalds { 1271a308da16SPavel Emelyanov return register_pernet_subsys(&raw6_net_ops); 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds void raw6_proc_exit(void) 12751da177e4SLinus Torvalds { 1276a308da16SPavel Emelyanov unregister_pernet_subsys(&raw6_net_ops); 12771da177e4SLinus Torvalds } 12781da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 12797f4e4868SDaniel Lezcano 1280a11e1d43SLinus Torvalds /* Same as inet6_dgram_ops, sans udp_poll. */ 128177d4b1d3SEric Dumazet const struct proto_ops inet6_sockraw_ops = { 12827f4e4868SDaniel Lezcano .family = PF_INET6, 12837f4e4868SDaniel Lezcano .owner = THIS_MODULE, 12847f4e4868SDaniel Lezcano .release = inet6_release, 12857f4e4868SDaniel Lezcano .bind = inet6_bind, 12867f4e4868SDaniel Lezcano .connect = inet_dgram_connect, /* ok */ 12877f4e4868SDaniel Lezcano .socketpair = sock_no_socketpair, /* a do nothing */ 12887f4e4868SDaniel Lezcano .accept = sock_no_accept, /* a do nothing */ 12897f4e4868SDaniel Lezcano .getname = inet6_getname, 1290a11e1d43SLinus Torvalds .poll = datagram_poll, /* ok */ 12917f4e4868SDaniel Lezcano .ioctl = inet6_ioctl, /* must change */ 1292c7cbdbf2SArnd Bergmann .gettstamp = sock_gettstamp, 12937f4e4868SDaniel Lezcano .listen = sock_no_listen, /* ok */ 12947f4e4868SDaniel Lezcano .shutdown = inet_shutdown, /* ok */ 12957f4e4868SDaniel Lezcano .setsockopt = sock_common_setsockopt, /* ok */ 12967f4e4868SDaniel Lezcano .getsockopt = sock_common_getsockopt, /* ok */ 12977f4e4868SDaniel Lezcano .sendmsg = inet_sendmsg, /* ok */ 12987f4e4868SDaniel Lezcano .recvmsg = sock_common_recvmsg, /* ok */ 12997f4e4868SDaniel Lezcano .mmap = sock_no_mmap, 13007f4e4868SDaniel Lezcano .sendpage = sock_no_sendpage, 13017f4e4868SDaniel Lezcano #ifdef CONFIG_COMPAT 13023986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 13037f4e4868SDaniel Lezcano #endif 13047f4e4868SDaniel Lezcano }; 13057f4e4868SDaniel Lezcano 13067f4e4868SDaniel Lezcano static struct inet_protosw rawv6_protosw = { 13077f4e4868SDaniel Lezcano .type = SOCK_RAW, 13087f4e4868SDaniel Lezcano .protocol = IPPROTO_IP, /* wild card */ 13097f4e4868SDaniel Lezcano .prot = &rawv6_prot, 13107f4e4868SDaniel Lezcano .ops = &inet6_sockraw_ops, 13117f4e4868SDaniel Lezcano .flags = INET_PROTOSW_REUSE, 13127f4e4868SDaniel Lezcano }; 13137f4e4868SDaniel Lezcano 13147f4e4868SDaniel Lezcano int __init rawv6_init(void) 13157f4e4868SDaniel Lezcano { 13163d2f6d41SJulia Lawall return inet6_register_protosw(&rawv6_protosw); 13177f4e4868SDaniel Lezcano } 13187f4e4868SDaniel Lezcano 131909f7709fSDaniel Lezcano void rawv6_exit(void) 13207f4e4868SDaniel Lezcano { 13217f4e4868SDaniel Lezcano inet6_unregister_protosw(&rawv6_protosw); 13227f4e4868SDaniel Lezcano } 1323