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> 351da177e4SLinus Torvalds #include <asm/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> 5359fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) 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 66b673e4dfSPavel Emelyanov static struct raw_hashinfo raw_v6_hashinfo = { 67938b93adSRobert P. J. Day .lock = __RW_LOCK_UNLOCKED(raw_v6_hashinfo.lock), 68b673e4dfSPavel Emelyanov }; 691da177e4SLinus Torvalds 70be185884SPavel Emelyanov static struct sock *__raw_v6_lookup(struct net *net, struct sock *sk, 71b71d1d42SEric Dumazet unsigned short num, const struct in6_addr *loc_addr, 72b71d1d42SEric Dumazet const struct in6_addr *rmt_addr, int dif) 731da177e4SLinus Torvalds { 741da177e4SLinus Torvalds struct hlist_node *node; 75a50feda5SEric Dumazet bool is_multicast = ipv6_addr_is_multicast(loc_addr); 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds sk_for_each_from(sk, node) 78c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num == num) { 791da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 801da177e4SLinus Torvalds 81878628fbSYOSHIFUJI Hideaki if (!net_eq(sock_net(sk), net)) 82be185884SPavel Emelyanov continue; 83be185884SPavel Emelyanov 841da177e4SLinus Torvalds if (!ipv6_addr_any(&np->daddr) && 851da177e4SLinus Torvalds !ipv6_addr_equal(&np->daddr, rmt_addr)) 861da177e4SLinus Torvalds continue; 871da177e4SLinus Torvalds 880bd1b59bSAndrew McDonald if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif) 890bd1b59bSAndrew McDonald continue; 900bd1b59bSAndrew McDonald 911da177e4SLinus Torvalds if (!ipv6_addr_any(&np->rcv_saddr)) { 921da177e4SLinus Torvalds if (ipv6_addr_equal(&np->rcv_saddr, loc_addr)) 931da177e4SLinus Torvalds goto found; 941da177e4SLinus Torvalds if (is_multicast && 951da177e4SLinus Torvalds inet6_mc_check(sk, loc_addr, rmt_addr)) 961da177e4SLinus Torvalds goto found; 971da177e4SLinus Torvalds continue; 981da177e4SLinus Torvalds } 991da177e4SLinus Torvalds goto found; 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds sk = NULL; 1021da177e4SLinus Torvalds found: 1031da177e4SLinus Torvalds return sk; 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds /* 1071da177e4SLinus Torvalds * 0 - deliver 1081da177e4SLinus Torvalds * 1 - block 1091da177e4SLinus Torvalds */ 1101da177e4SLinus Torvalds static __inline__ int icmpv6_filter(struct sock *sk, struct sk_buff *skb) 1111da177e4SLinus Torvalds { 1121da177e4SLinus Torvalds struct icmp6hdr *icmph; 1131da177e4SLinus Torvalds struct raw6_sock *rp = raw6_sk(sk); 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds if (pskb_may_pull(skb, sizeof(struct icmp6hdr))) { 1161da177e4SLinus Torvalds __u32 *data = &rp->filter.data[0]; 1171da177e4SLinus Torvalds int bit_nr; 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds icmph = (struct icmp6hdr *) skb->data; 1201da177e4SLinus Torvalds bit_nr = icmph->icmp6_type; 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds return (data[bit_nr >> 5] & (1 << (bit_nr & 31))) != 0; 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds return 0; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 12759fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) 128f2eda47dSEric Dumazet typedef int mh_filter_t(struct sock *sock, struct sk_buff *skb); 12959fbb3a6SMasahide NAKAMURA 130f2eda47dSEric Dumazet static mh_filter_t __rcu *mh_filter __read_mostly; 131f2eda47dSEric Dumazet 132f2eda47dSEric Dumazet int rawv6_mh_filter_register(mh_filter_t filter) 13359fbb3a6SMasahide NAKAMURA { 134cf778b00SEric Dumazet rcu_assign_pointer(mh_filter, filter); 13559fbb3a6SMasahide NAKAMURA return 0; 13659fbb3a6SMasahide NAKAMURA } 13759fbb3a6SMasahide NAKAMURA EXPORT_SYMBOL(rawv6_mh_filter_register); 13859fbb3a6SMasahide NAKAMURA 139f2eda47dSEric Dumazet int rawv6_mh_filter_unregister(mh_filter_t filter) 14059fbb3a6SMasahide NAKAMURA { 141a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(mh_filter, NULL); 14259fbb3a6SMasahide NAKAMURA synchronize_rcu(); 14359fbb3a6SMasahide NAKAMURA return 0; 14459fbb3a6SMasahide NAKAMURA } 14559fbb3a6SMasahide NAKAMURA EXPORT_SYMBOL(rawv6_mh_filter_unregister); 14659fbb3a6SMasahide NAKAMURA 14759fbb3a6SMasahide NAKAMURA #endif 14859fbb3a6SMasahide NAKAMURA 1491da177e4SLinus Torvalds /* 1501da177e4SLinus Torvalds * demultiplex raw sockets. 1511da177e4SLinus Torvalds * (should consider queueing the skb in the sock receive_queue 1521da177e4SLinus Torvalds * without calling rawv6.c) 1531da177e4SLinus Torvalds * 1541da177e4SLinus Torvalds * Caller owns SKB so we must make clones. 1551da177e4SLinus Torvalds */ 156a50feda5SEric Dumazet static bool ipv6_raw_deliver(struct sk_buff *skb, int nexthdr) 1571da177e4SLinus Torvalds { 158b71d1d42SEric Dumazet const struct in6_addr *saddr; 159b71d1d42SEric Dumazet const struct in6_addr *daddr; 1601da177e4SLinus Torvalds struct sock *sk; 161a50feda5SEric Dumazet bool delivered = false; 1621da177e4SLinus Torvalds __u8 hash; 163be185884SPavel Emelyanov struct net *net; 1641da177e4SLinus Torvalds 1650660e03fSArnaldo Carvalho de Melo saddr = &ipv6_hdr(skb)->saddr; 1661da177e4SLinus Torvalds daddr = saddr + 1; 1671da177e4SLinus Torvalds 168f9242b6bSDavid S. Miller hash = nexthdr & (RAW_HTABLE_SIZE - 1); 1691da177e4SLinus Torvalds 170b673e4dfSPavel Emelyanov read_lock(&raw_v6_hashinfo.lock); 171b673e4dfSPavel Emelyanov sk = sk_head(&raw_v6_hashinfo.ht[hash]); 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds if (sk == NULL) 1741da177e4SLinus Torvalds goto out; 1751da177e4SLinus Torvalds 176c346dca1SYOSHIFUJI Hideaki net = dev_net(skb->dev); 177be185884SPavel Emelyanov sk = __raw_v6_lookup(net, sk, nexthdr, daddr, saddr, IP6CB(skb)->iif); 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds while (sk) { 1807be96f76SMasahide NAKAMURA int filtered; 1817be96f76SMasahide NAKAMURA 182a50feda5SEric Dumazet delivered = true; 1837be96f76SMasahide NAKAMURA switch (nexthdr) { 1847be96f76SMasahide NAKAMURA case IPPROTO_ICMPV6: 1857be96f76SMasahide NAKAMURA filtered = icmpv6_filter(sk, skb); 1867be96f76SMasahide NAKAMURA break; 18759fbb3a6SMasahide NAKAMURA 18859fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) 1897be96f76SMasahide NAKAMURA case IPPROTO_MH: 19059fbb3a6SMasahide NAKAMURA { 1917be96f76SMasahide NAKAMURA /* XXX: To validate MH only once for each packet, 1927be96f76SMasahide NAKAMURA * this is placed here. It should be after checking 1937be96f76SMasahide NAKAMURA * xfrm policy, however it doesn't. The checking xfrm 1947be96f76SMasahide NAKAMURA * policy is placed in rawv6_rcv() because it is 1957be96f76SMasahide NAKAMURA * required for each socket. 1967be96f76SMasahide NAKAMURA */ 197f2eda47dSEric Dumazet mh_filter_t *filter; 19859fbb3a6SMasahide NAKAMURA 19959fbb3a6SMasahide NAKAMURA filter = rcu_dereference(mh_filter); 200f2eda47dSEric Dumazet filtered = filter ? (*filter)(sk, skb) : 0; 2017be96f76SMasahide NAKAMURA break; 20259fbb3a6SMasahide NAKAMURA } 2037be96f76SMasahide NAKAMURA #endif 2047be96f76SMasahide NAKAMURA default: 2057be96f76SMasahide NAKAMURA filtered = 0; 2067be96f76SMasahide NAKAMURA break; 2077be96f76SMasahide NAKAMURA } 2087be96f76SMasahide NAKAMURA 2097be96f76SMasahide NAKAMURA if (filtered < 0) 2107be96f76SMasahide NAKAMURA break; 2117be96f76SMasahide NAKAMURA if (filtered == 0) { 2121da177e4SLinus Torvalds struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC); 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds /* Not releasing hash table! */ 2159fb9cbb1SYasuyuki Kozakai if (clone) { 2169fb9cbb1SYasuyuki Kozakai nf_reset(clone); 2171da177e4SLinus Torvalds rawv6_rcv(sk, clone); 2181da177e4SLinus Torvalds } 2199fb9cbb1SYasuyuki Kozakai } 220be185884SPavel Emelyanov sk = __raw_v6_lookup(net, sk_next(sk), nexthdr, daddr, saddr, 2212dac4b96SYOSHIFUJI Hideaki IP6CB(skb)->iif); 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds out: 224b673e4dfSPavel Emelyanov read_unlock(&raw_v6_hashinfo.lock); 225d13964f4SPatrick McHardy return delivered; 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 228a50feda5SEric Dumazet bool raw6_local_deliver(struct sk_buff *skb, int nexthdr) 22969d6da0bSPavel Emelyanov { 23069d6da0bSPavel Emelyanov struct sock *raw_sk; 23169d6da0bSPavel Emelyanov 232f9242b6bSDavid S. Miller raw_sk = sk_head(&raw_v6_hashinfo.ht[nexthdr & (RAW_HTABLE_SIZE - 1)]); 23369d6da0bSPavel Emelyanov if (raw_sk && !ipv6_raw_deliver(skb, nexthdr)) 23469d6da0bSPavel Emelyanov raw_sk = NULL; 23569d6da0bSPavel Emelyanov 23669d6da0bSPavel Emelyanov return raw_sk != NULL; 23769d6da0bSPavel Emelyanov } 23869d6da0bSPavel Emelyanov 2391da177e4SLinus Torvalds /* This cleans up af_inet6 a bit. -DaveM */ 2401da177e4SLinus Torvalds static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len) 2411da177e4SLinus Torvalds { 2421da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 2431da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 2441da177e4SLinus Torvalds struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr; 245e69a4adcSAl Viro __be32 v4addr = 0; 2461da177e4SLinus Torvalds int addr_type; 2471da177e4SLinus Torvalds int err; 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds if (addr_len < SIN6_LEN_RFC2133) 2501da177e4SLinus Torvalds return -EINVAL; 2511da177e4SLinus Torvalds addr_type = ipv6_addr_type(&addr->sin6_addr); 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds /* Raw sockets are IPv6 only */ 2541da177e4SLinus Torvalds if (addr_type == IPV6_ADDR_MAPPED) 255fd5c0027SEric Dumazet return -EADDRNOTAVAIL; 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds lock_sock(sk); 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds err = -EINVAL; 2601da177e4SLinus Torvalds if (sk->sk_state != TCP_CLOSE) 2611da177e4SLinus Torvalds goto out; 2621da177e4SLinus Torvalds 263fd5c0027SEric Dumazet rcu_read_lock(); 2641da177e4SLinus Torvalds /* Check if the address belongs to the host. */ 2651da177e4SLinus Torvalds if (addr_type != IPV6_ADDR_ANY) { 2661da177e4SLinus Torvalds struct net_device *dev = NULL; 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_LINKLOCAL) { 2691da177e4SLinus Torvalds if (addr_len >= sizeof(struct sockaddr_in6) && 2701da177e4SLinus Torvalds addr->sin6_scope_id) { 2711da177e4SLinus Torvalds /* Override any existing binding, if another 2721da177e4SLinus Torvalds * one is supplied by user. 2731da177e4SLinus Torvalds */ 2741da177e4SLinus Torvalds sk->sk_bound_dev_if = addr->sin6_scope_id; 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds /* Binding to link-local address requires an interface */ 2781da177e4SLinus Torvalds if (!sk->sk_bound_dev_if) 279fd5c0027SEric Dumazet goto out_unlock; 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds err = -ENODEV; 282fd5c0027SEric Dumazet dev = dev_get_by_index_rcu(sock_net(sk), 283fd5c0027SEric Dumazet sk->sk_bound_dev_if); 284fd5c0027SEric Dumazet if (!dev) 285fd5c0027SEric Dumazet goto out_unlock; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds /* ipv4 addr of the socket is invalid. Only the 2891da177e4SLinus Torvalds * unspecified and mapped address have a v4 equivalent. 2901da177e4SLinus Torvalds */ 2911da177e4SLinus Torvalds v4addr = LOOPBACK4_IPV6; 2921da177e4SLinus Torvalds if (!(addr_type & IPV6_ADDR_MULTICAST)) { 2931da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 2943b1e0a65SYOSHIFUJI Hideaki if (!ipv6_chk_addr(sock_net(sk), &addr->sin6_addr, 295bfeade08SDaniel Lezcano dev, 0)) { 296fd5c0027SEric Dumazet goto out_unlock; 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds 301c720c7e8SEric Dumazet inet->inet_rcv_saddr = inet->inet_saddr = v4addr; 3024e3fd7a0SAlexey Dobriyan np->rcv_saddr = addr->sin6_addr; 3031da177e4SLinus Torvalds if (!(addr_type & IPV6_ADDR_MULTICAST)) 3044e3fd7a0SAlexey Dobriyan np->saddr = addr->sin6_addr; 3051da177e4SLinus Torvalds err = 0; 306fd5c0027SEric Dumazet out_unlock: 307fd5c0027SEric Dumazet rcu_read_unlock(); 3081da177e4SLinus Torvalds out: 3091da177e4SLinus Torvalds release_sock(sk); 3101da177e4SLinus Torvalds return err; 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds 31369d6da0bSPavel Emelyanov static void rawv6_err(struct sock *sk, struct sk_buff *skb, 3141da177e4SLinus Torvalds struct inet6_skb_parm *opt, 315d5fdd6baSBrian Haley u8 type, u8 code, int offset, __be32 info) 3161da177e4SLinus Torvalds { 3171da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 3181da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 3191da177e4SLinus Torvalds int err; 3201da177e4SLinus Torvalds int harderr; 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds /* Report error on raw socket, if: 3231da177e4SLinus Torvalds 1. User requested recverr. 3241da177e4SLinus Torvalds 2. Socket is connected (otherwise the error indication 3251da177e4SLinus Torvalds is useless without recverr and error is hard. 3261da177e4SLinus Torvalds */ 3271da177e4SLinus Torvalds if (!np->recverr && sk->sk_state != TCP_ESTABLISHED) 3281da177e4SLinus Torvalds return; 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds harderr = icmpv6_err_convert(type, code, &err); 33181aded24SDavid S. Miller if (type == ICMPV6_PKT_TOOBIG) { 33281aded24SDavid S. Miller ip6_sk_update_pmtu(skb, sk, info); 3331da177e4SLinus Torvalds harderr = (np->pmtudisc == IPV6_PMTUDISC_DO); 33481aded24SDavid S. Miller } 335ec18d9a2SDavid S. Miller if (type == NDISC_REDIRECT) 336ec18d9a2SDavid S. Miller ip6_sk_redirect(skb, sk); 3371da177e4SLinus Torvalds if (np->recverr) { 3381da177e4SLinus Torvalds u8 *payload = skb->data; 3391da177e4SLinus Torvalds if (!inet->hdrincl) 3401da177e4SLinus Torvalds payload += offset; 3411da177e4SLinus Torvalds ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload); 3421da177e4SLinus Torvalds } 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds if (np->recverr || harderr) { 3451da177e4SLinus Torvalds sk->sk_err = err; 3461da177e4SLinus Torvalds sk->sk_error_report(sk); 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds 35069d6da0bSPavel Emelyanov void raw6_icmp_error(struct sk_buff *skb, int nexthdr, 351d5fdd6baSBrian Haley u8 type, u8 code, int inner_offset, __be32 info) 35269d6da0bSPavel Emelyanov { 35369d6da0bSPavel Emelyanov struct sock *sk; 35469d6da0bSPavel Emelyanov int hash; 355b71d1d42SEric Dumazet const struct in6_addr *saddr, *daddr; 356be185884SPavel Emelyanov struct net *net; 35769d6da0bSPavel Emelyanov 358b673e4dfSPavel Emelyanov hash = nexthdr & (RAW_HTABLE_SIZE - 1); 35969d6da0bSPavel Emelyanov 360b673e4dfSPavel Emelyanov read_lock(&raw_v6_hashinfo.lock); 361b673e4dfSPavel Emelyanov sk = sk_head(&raw_v6_hashinfo.ht[hash]); 36269d6da0bSPavel Emelyanov if (sk != NULL) { 36305f175cdSYOSHIFUJI Hideaki /* Note: ipv6_hdr(skb) != skb->data */ 364b71d1d42SEric Dumazet const struct ipv6hdr *ip6h = (const struct ipv6hdr *)skb->data; 36505f175cdSYOSHIFUJI Hideaki saddr = &ip6h->saddr; 36605f175cdSYOSHIFUJI Hideaki daddr = &ip6h->daddr; 367c346dca1SYOSHIFUJI Hideaki net = dev_net(skb->dev); 36869d6da0bSPavel Emelyanov 369be185884SPavel Emelyanov while ((sk = __raw_v6_lookup(net, sk, nexthdr, saddr, daddr, 37069d6da0bSPavel Emelyanov IP6CB(skb)->iif))) { 37169d6da0bSPavel Emelyanov rawv6_err(sk, skb, NULL, type, code, 37269d6da0bSPavel Emelyanov inner_offset, info); 37369d6da0bSPavel Emelyanov sk = sk_next(sk); 37469d6da0bSPavel Emelyanov } 37569d6da0bSPavel Emelyanov } 376b673e4dfSPavel Emelyanov read_unlock(&raw_v6_hashinfo.lock); 37769d6da0bSPavel Emelyanov } 37869d6da0bSPavel Emelyanov 3791da177e4SLinus Torvalds static inline int rawv6_rcv_skb(struct sock *sk, struct sk_buff *skb) 3801da177e4SLinus Torvalds { 38133d480ceSEric Dumazet if ((raw6_sk(sk)->checksum || rcu_access_pointer(sk->sk_filter)) && 382fb286bb2SHerbert Xu skb_checksum_complete(skb)) { 383a92aa318SWang Chen atomic_inc(&sk->sk_drops); 3841da177e4SLinus Torvalds kfree_skb(skb); 3853cc76caaSYang Hongyang return NET_RX_DROP; 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds /* Charge it to the socket. */ 389d826eb14SEric Dumazet skb_dst_drop(skb); 390d826eb14SEric Dumazet if (sock_queue_rcv_skb(sk, skb) < 0) { 3911da177e4SLinus Torvalds kfree_skb(skb); 3923cc76caaSYang Hongyang return NET_RX_DROP; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds return 0; 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds /* 3991da177e4SLinus Torvalds * This is next to useless... 4001da177e4SLinus Torvalds * if we demultiplex in network layer we don't need the extra call 4011da177e4SLinus Torvalds * just to queue the skb... 4021da177e4SLinus Torvalds * maybe we could have the network decide upon a hint if it 4031da177e4SLinus Torvalds * should call raw_rcv for demultiplexing 4041da177e4SLinus Torvalds */ 4051da177e4SLinus Torvalds int rawv6_rcv(struct sock *sk, struct sk_buff *skb) 4061da177e4SLinus Torvalds { 4071da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 4081da177e4SLinus Torvalds struct raw6_sock *rp = raw6_sk(sk); 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) { 411a92aa318SWang Chen atomic_inc(&sk->sk_drops); 4121da177e4SLinus Torvalds kfree_skb(skb); 4131da177e4SLinus Torvalds return NET_RX_DROP; 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds if (!rp->checksum) 4171da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_UNNECESSARY; 4181da177e4SLinus Torvalds 41984fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_COMPLETE) { 420d56f90a7SArnaldo Carvalho de Melo skb_postpull_rcsum(skb, skb_network_header(skb), 421cfe1fc77SArnaldo Carvalho de Melo skb_network_header_len(skb)); 4220660e03fSArnaldo Carvalho de Melo if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 4230660e03fSArnaldo Carvalho de Melo &ipv6_hdr(skb)->daddr, 424c720c7e8SEric Dumazet skb->len, inet->inet_num, skb->csum)) 425fb286bb2SHerbert Xu skb->ip_summed = CHECKSUM_UNNECESSARY; 4261da177e4SLinus Torvalds } 42760476372SHerbert Xu if (!skb_csum_unnecessary(skb)) 4280660e03fSArnaldo Carvalho de Melo skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 4290660e03fSArnaldo Carvalho de Melo &ipv6_hdr(skb)->daddr, 4300660e03fSArnaldo Carvalho de Melo skb->len, 431c720c7e8SEric Dumazet inet->inet_num, 0)); 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds if (inet->hdrincl) { 434fb286bb2SHerbert Xu if (skb_checksum_complete(skb)) { 435a92aa318SWang Chen atomic_inc(&sk->sk_drops); 4361da177e4SLinus Torvalds kfree_skb(skb); 4373cc76caaSYang Hongyang return NET_RX_DROP; 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds rawv6_rcv_skb(sk, skb); 4421da177e4SLinus Torvalds return 0; 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds /* 4471da177e4SLinus Torvalds * This should be easy, if there is something there 4481da177e4SLinus Torvalds * we return it, otherwise we block. 4491da177e4SLinus Torvalds */ 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds static int rawv6_recvmsg(struct kiocb *iocb, struct sock *sk, 4521da177e4SLinus Torvalds struct msghdr *msg, size_t len, 4531da177e4SLinus Torvalds int noblock, int flags, int *addr_len) 4541da177e4SLinus Torvalds { 4551da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 4561da177e4SLinus Torvalds struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)msg->msg_name; 4571da177e4SLinus Torvalds struct sk_buff *skb; 4581da177e4SLinus Torvalds size_t copied; 4591da177e4SLinus Torvalds int err; 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds if (flags & MSG_OOB) 4621da177e4SLinus Torvalds return -EOPNOTSUPP; 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds if (addr_len) 4651da177e4SLinus Torvalds *addr_len=sizeof(*sin6); 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds if (flags & MSG_ERRQUEUE) 4681da177e4SLinus Torvalds return ipv6_recv_error(sk, msg, len); 4691da177e4SLinus Torvalds 4704b340ae2SBrian Haley if (np->rxpmtu && np->rxopt.bits.rxpmtu) 4714b340ae2SBrian Haley return ipv6_recv_rxpmtu(sk, msg, len); 4724b340ae2SBrian Haley 4731da177e4SLinus Torvalds skb = skb_recv_datagram(sk, flags, noblock, &err); 4741da177e4SLinus Torvalds if (!skb) 4751da177e4SLinus Torvalds goto out; 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds copied = skb->len; 4781da177e4SLinus Torvalds if (copied > len) { 4791da177e4SLinus Torvalds copied = len; 4801da177e4SLinus Torvalds msg->msg_flags |= MSG_TRUNC; 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds 48360476372SHerbert Xu if (skb_csum_unnecessary(skb)) { 4841da177e4SLinus Torvalds err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); 4851da177e4SLinus Torvalds } else if (msg->msg_flags&MSG_TRUNC) { 486fb286bb2SHerbert Xu if (__skb_checksum_complete(skb)) 4871da177e4SLinus Torvalds goto csum_copy_err; 4881da177e4SLinus Torvalds err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); 4891da177e4SLinus Torvalds } else { 4901da177e4SLinus Torvalds err = skb_copy_and_csum_datagram_iovec(skb, 0, msg->msg_iov); 4911da177e4SLinus Torvalds if (err == -EINVAL) 4921da177e4SLinus Torvalds goto csum_copy_err; 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds if (err) 4951da177e4SLinus Torvalds goto out_free; 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds /* Copy the address. */ 4981da177e4SLinus Torvalds if (sin6) { 4991da177e4SLinus Torvalds sin6->sin6_family = AF_INET6; 500f59fc7f3STetsuo Handa sin6->sin6_port = 0; 5014e3fd7a0SAlexey Dobriyan sin6->sin6_addr = ipv6_hdr(skb)->saddr; 5021da177e4SLinus Torvalds sin6->sin6_flowinfo = 0; 5031da177e4SLinus Torvalds sin6->sin6_scope_id = 0; 5041da177e4SLinus Torvalds if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) 5051da177e4SLinus Torvalds sin6->sin6_scope_id = IP6CB(skb)->iif; 5061da177e4SLinus Torvalds } 5071da177e4SLinus Torvalds 5083b885787SNeil Horman sock_recv_ts_and_drops(msg, sk, skb); 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds if (np->rxopt.all) 5111da177e4SLinus Torvalds datagram_recv_ctl(sk, msg, skb); 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds err = copied; 5141da177e4SLinus Torvalds if (flags & MSG_TRUNC) 5151da177e4SLinus Torvalds err = skb->len; 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds out_free: 5181da177e4SLinus Torvalds skb_free_datagram(sk, skb); 5191da177e4SLinus Torvalds out: 5201da177e4SLinus Torvalds return err; 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds csum_copy_err: 5233305b80cSHerbert Xu skb_kill_datagram(sk, skb, flags); 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds /* Error for blocking case is chosen to masquerade 5261da177e4SLinus Torvalds as some normal condition. 5271da177e4SLinus Torvalds */ 5281da177e4SLinus Torvalds err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH; 5293305b80cSHerbert Xu goto out; 5301da177e4SLinus Torvalds } 5311da177e4SLinus Torvalds 5324c9483b2SDavid S. Miller static int rawv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6, 533357b40a1SHerbert Xu struct raw6_sock *rp) 5341da177e4SLinus Torvalds { 5351da177e4SLinus Torvalds struct sk_buff *skb; 5361da177e4SLinus Torvalds int err = 0; 537357b40a1SHerbert Xu int offset; 538357b40a1SHerbert Xu int len; 539679a8738SHerbert Xu int total_len; 540868c86bcSAl Viro __wsum tmp_csum; 541868c86bcSAl Viro __sum16 csum; 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds if (!rp->checksum) 5441da177e4SLinus Torvalds goto send; 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds if ((skb = skb_peek(&sk->sk_write_queue)) == NULL) 5471da177e4SLinus Torvalds goto out; 5481da177e4SLinus Torvalds 549357b40a1SHerbert Xu offset = rp->offset; 550299b0767SSteffen Klassert total_len = inet_sk(sk)->cork.base.length; 551679a8738SHerbert Xu if (offset >= total_len - 1) { 5521da177e4SLinus Torvalds err = -EINVAL; 553357b40a1SHerbert Xu ip6_flush_pending_frames(sk); 5541da177e4SLinus Torvalds goto out; 5551da177e4SLinus Torvalds } 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds /* should be check HW csum miyazawa */ 5581da177e4SLinus Torvalds if (skb_queue_len(&sk->sk_write_queue) == 1) { 5591da177e4SLinus Torvalds /* 5601da177e4SLinus Torvalds * Only one fragment on the socket. 5611da177e4SLinus Torvalds */ 5621da177e4SLinus Torvalds tmp_csum = skb->csum; 5631da177e4SLinus Torvalds } else { 564357b40a1SHerbert Xu struct sk_buff *csum_skb = NULL; 5651da177e4SLinus Torvalds tmp_csum = 0; 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds skb_queue_walk(&sk->sk_write_queue, skb) { 5681da177e4SLinus Torvalds tmp_csum = csum_add(tmp_csum, skb->csum); 569357b40a1SHerbert Xu 570357b40a1SHerbert Xu if (csum_skb) 571357b40a1SHerbert Xu continue; 572357b40a1SHerbert Xu 573ea2ae17dSArnaldo Carvalho de Melo len = skb->len - skb_transport_offset(skb); 574357b40a1SHerbert Xu if (offset >= len) { 575357b40a1SHerbert Xu offset -= len; 576357b40a1SHerbert Xu continue; 5771da177e4SLinus Torvalds } 578357b40a1SHerbert Xu 579357b40a1SHerbert Xu csum_skb = skb; 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds 582357b40a1SHerbert Xu skb = csum_skb; 583357b40a1SHerbert Xu } 584357b40a1SHerbert Xu 585ea2ae17dSArnaldo Carvalho de Melo offset += skb_transport_offset(skb); 586357b40a1SHerbert Xu if (skb_copy_bits(skb, offset, &csum, 2)) 587357b40a1SHerbert Xu BUG(); 588357b40a1SHerbert Xu 5891da177e4SLinus Torvalds /* in case cksum was not initialized */ 590357b40a1SHerbert Xu if (unlikely(csum)) 5915f92a738SAl Viro tmp_csum = csum_sub(tmp_csum, csum_unfold(csum)); 5921da177e4SLinus Torvalds 5934c9483b2SDavid S. Miller csum = csum_ipv6_magic(&fl6->saddr, &fl6->daddr, 5944c9483b2SDavid S. Miller total_len, fl6->flowi6_proto, tmp_csum); 5951da177e4SLinus Torvalds 5964c9483b2SDavid S. Miller if (csum == 0 && fl6->flowi6_proto == IPPROTO_UDP) 597f6ab0288SAl Viro csum = CSUM_MANGLED_0; 598357b40a1SHerbert Xu 599357b40a1SHerbert Xu if (skb_store_bits(skb, offset, &csum, 2)) 600357b40a1SHerbert Xu BUG(); 601357b40a1SHerbert Xu 6021da177e4SLinus Torvalds send: 6031da177e4SLinus Torvalds err = ip6_push_pending_frames(sk); 6041da177e4SLinus Torvalds out: 6051da177e4SLinus Torvalds return err; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds static int rawv6_send_hdrinc(struct sock *sk, void *from, int length, 6094c9483b2SDavid S. Miller struct flowi6 *fl6, struct dst_entry **dstp, 6101da177e4SLinus Torvalds unsigned int flags) 6111da177e4SLinus Torvalds { 6123320da89SHerbert Xu struct ipv6_pinfo *np = inet6_sk(sk); 6131da177e4SLinus Torvalds struct ipv6hdr *iph; 6141da177e4SLinus Torvalds struct sk_buff *skb; 6151da177e4SLinus Torvalds int err; 6161789a640SEric Dumazet struct rt6_info *rt = (struct rt6_info *)*dstp; 617a7ae1992SHerbert Xu int hlen = LL_RESERVED_SPACE(rt->dst.dev); 618a7ae1992SHerbert Xu int tlen = rt->dst.dev->needed_tailroom; 6191da177e4SLinus Torvalds 620d8d1f30bSChangli Gao if (length > rt->dst.dev->mtu) { 6214c9483b2SDavid S. Miller ipv6_local_error(sk, EMSGSIZE, fl6, rt->dst.dev->mtu); 6221da177e4SLinus Torvalds return -EMSGSIZE; 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds if (flags&MSG_PROBE) 6251da177e4SLinus Torvalds goto out; 6261da177e4SLinus Torvalds 627f5184d26SJohannes Berg skb = sock_alloc_send_skb(sk, 628a7ae1992SHerbert Xu length + hlen + tlen + 15, 6291da177e4SLinus Torvalds flags & MSG_DONTWAIT, &err); 6301da177e4SLinus Torvalds if (skb == NULL) 6311da177e4SLinus Torvalds goto error; 632a7ae1992SHerbert Xu skb_reserve(skb, hlen); 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds skb->priority = sk->sk_priority; 6354a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 636d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 6371789a640SEric Dumazet *dstp = NULL; 6381da177e4SLinus Torvalds 6391ced98e8SArnaldo Carvalho de Melo skb_put(skb, length); 6401ced98e8SArnaldo Carvalho de Melo skb_reset_network_header(skb); 6410660e03fSArnaldo Carvalho de Melo iph = ipv6_hdr(skb); 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 6441da177e4SLinus Torvalds 645b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 6461da177e4SLinus Torvalds err = memcpy_fromiovecend((void *)iph, from, 0, length); 6471da177e4SLinus Torvalds if (err) 6481da177e4SLinus Torvalds goto error_fault; 6491da177e4SLinus Torvalds 650edf391ffSNeil Horman IP6_UPD_PO_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len); 651b2e0b385SJan Engelhardt err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, 652d8d1f30bSChangli Gao rt->dst.dev, dst_output); 6531da177e4SLinus Torvalds if (err > 0) 6546ce9e7b5SEric Dumazet err = net_xmit_errno(err); 6551da177e4SLinus Torvalds if (err) 6561da177e4SLinus Torvalds goto error; 6571da177e4SLinus Torvalds out: 6581da177e4SLinus Torvalds return 0; 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds error_fault: 6611da177e4SLinus Torvalds err = -EFAULT; 6621da177e4SLinus Torvalds kfree_skb(skb); 6631da177e4SLinus Torvalds error: 6643bd653c8SDenis V. Lunev IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS); 6656ce9e7b5SEric Dumazet if (err == -ENOBUFS && !np->recverr) 6666ce9e7b5SEric Dumazet err = 0; 6671da177e4SLinus Torvalds return err; 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds 6704c9483b2SDavid S. Miller static int rawv6_probe_proto_opt(struct flowi6 *fl6, struct msghdr *msg) 6711da177e4SLinus Torvalds { 6721da177e4SLinus Torvalds struct iovec *iov; 6731da177e4SLinus Torvalds u8 __user *type = NULL; 6741da177e4SLinus Torvalds u8 __user *code = NULL; 6756e8f4d48SMasahide NAKAMURA u8 len = 0; 6761da177e4SLinus Torvalds int probed = 0; 6771da177e4SLinus Torvalds int i; 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds if (!msg->msg_iov) 680a27b58feSHeiko Carstens return 0; 6811da177e4SLinus Torvalds 6821da177e4SLinus Torvalds for (i = 0; i < msg->msg_iovlen; i++) { 6831da177e4SLinus Torvalds iov = &msg->msg_iov[i]; 6841da177e4SLinus Torvalds if (!iov) 6851da177e4SLinus Torvalds continue; 6861da177e4SLinus Torvalds 6874c9483b2SDavid S. Miller switch (fl6->flowi6_proto) { 6881da177e4SLinus Torvalds case IPPROTO_ICMPV6: 6891da177e4SLinus Torvalds /* check if one-byte field is readable or not. */ 6901da177e4SLinus Torvalds if (iov->iov_base && iov->iov_len < 1) 6911da177e4SLinus Torvalds break; 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds if (!type) { 6941da177e4SLinus Torvalds type = iov->iov_base; 6951da177e4SLinus Torvalds /* check if code field is readable or not. */ 6961da177e4SLinus Torvalds if (iov->iov_len > 1) 6971da177e4SLinus Torvalds code = type + 1; 6981da177e4SLinus Torvalds } else if (!code) 6991da177e4SLinus Torvalds code = iov->iov_base; 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds if (type && code) { 7021958b856SDavid S. Miller if (get_user(fl6->fl6_icmp_type, type) || 7031958b856SDavid S. Miller get_user(fl6->fl6_icmp_code, code)) 704a27b58feSHeiko Carstens return -EFAULT; 7051da177e4SLinus Torvalds probed = 1; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds break; 7086e8f4d48SMasahide NAKAMURA case IPPROTO_MH: 7096e8f4d48SMasahide NAKAMURA if (iov->iov_base && iov->iov_len < 1) 7106e8f4d48SMasahide NAKAMURA break; 7116e8f4d48SMasahide NAKAMURA /* check if type field is readable or not. */ 7126e8f4d48SMasahide NAKAMURA if (iov->iov_len > 2 - len) { 7136e8f4d48SMasahide NAKAMURA u8 __user *p = iov->iov_base; 7141958b856SDavid S. Miller if (get_user(fl6->fl6_mh_type, &p[2 - len])) 715a27b58feSHeiko Carstens return -EFAULT; 7166e8f4d48SMasahide NAKAMURA probed = 1; 7176e8f4d48SMasahide NAKAMURA } else 7186e8f4d48SMasahide NAKAMURA len += iov->iov_len; 7196e8f4d48SMasahide NAKAMURA 7206e8f4d48SMasahide NAKAMURA break; 7211da177e4SLinus Torvalds default: 7221da177e4SLinus Torvalds probed = 1; 7231da177e4SLinus Torvalds break; 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds if (probed) 7261da177e4SLinus Torvalds break; 7271da177e4SLinus Torvalds } 728a27b58feSHeiko Carstens return 0; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk, 7321da177e4SLinus Torvalds struct msghdr *msg, size_t len) 7331da177e4SLinus Torvalds { 7341da177e4SLinus Torvalds struct ipv6_txoptions opt_space; 7351da177e4SLinus Torvalds struct sockaddr_in6 * sin6 = (struct sockaddr_in6 *) msg->msg_name; 73620c59de2SArnaud Ebalard struct in6_addr *daddr, *final_p, final; 7371da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 7381da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 7391da177e4SLinus Torvalds struct raw6_sock *rp = raw6_sk(sk); 7401da177e4SLinus Torvalds struct ipv6_txoptions *opt = NULL; 7411da177e4SLinus Torvalds struct ip6_flowlabel *flowlabel = NULL; 7421da177e4SLinus Torvalds struct dst_entry *dst = NULL; 7434c9483b2SDavid S. Miller struct flowi6 fl6; 7441da177e4SLinus Torvalds int addr_len = msg->msg_namelen; 7451da177e4SLinus Torvalds int hlimit = -1; 74641a1f8eaSYOSHIFUJI Hideaki int tclass = -1; 74713b52cd4SBrian Haley int dontfrag = -1; 7481da177e4SLinus Torvalds u16 proto; 7491da177e4SLinus Torvalds int err; 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds /* Rough check on arithmetic overflow, 752b59e139bSYOSHIFUJI Hideaki better check is made in ip6_append_data(). 7531da177e4SLinus Torvalds */ 754b59e139bSYOSHIFUJI Hideaki if (len > INT_MAX) 7551da177e4SLinus Torvalds return -EMSGSIZE; 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds /* Mirror BSD error message compatibility */ 7581da177e4SLinus Torvalds if (msg->msg_flags & MSG_OOB) 7591da177e4SLinus Torvalds return -EOPNOTSUPP; 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds /* 7621da177e4SLinus Torvalds * Get and verify the address. 7631da177e4SLinus Torvalds */ 7644c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 7651da177e4SLinus Torvalds 7664c9483b2SDavid S. Miller fl6.flowi6_mark = sk->sk_mark; 7674a19ec58SLaszlo Attila Toth 7681da177e4SLinus Torvalds if (sin6) { 7691da177e4SLinus Torvalds if (addr_len < SIN6_LEN_RFC2133) 7701da177e4SLinus Torvalds return -EINVAL; 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds if (sin6->sin6_family && sin6->sin6_family != AF_INET6) 773a02cec21SEric Dumazet return -EAFNOSUPPORT; 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds /* port is the proto value [0..255] carried in nexthdr */ 7761da177e4SLinus Torvalds proto = ntohs(sin6->sin6_port); 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds if (!proto) 779c720c7e8SEric Dumazet proto = inet->inet_num; 780c720c7e8SEric Dumazet else if (proto != inet->inet_num) 781a02cec21SEric Dumazet return -EINVAL; 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds if (proto > 255) 784a02cec21SEric Dumazet return -EINVAL; 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds daddr = &sin6->sin6_addr; 7871da177e4SLinus Torvalds if (np->sndflow) { 7884c9483b2SDavid S. Miller fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; 7894c9483b2SDavid S. Miller if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) { 7904c9483b2SDavid S. Miller flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 7911da177e4SLinus Torvalds if (flowlabel == NULL) 7921da177e4SLinus Torvalds return -EINVAL; 7931da177e4SLinus Torvalds daddr = &flowlabel->dst; 7941da177e4SLinus Torvalds } 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds /* 7981da177e4SLinus Torvalds * Otherwise it will be difficult to maintain 7991da177e4SLinus Torvalds * sk->sk_dst_cache. 8001da177e4SLinus Torvalds */ 8011da177e4SLinus Torvalds if (sk->sk_state == TCP_ESTABLISHED && 8021da177e4SLinus Torvalds ipv6_addr_equal(daddr, &np->daddr)) 8031da177e4SLinus Torvalds daddr = &np->daddr; 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds if (addr_len >= sizeof(struct sockaddr_in6) && 8061da177e4SLinus Torvalds sin6->sin6_scope_id && 8071da177e4SLinus Torvalds ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL) 8084c9483b2SDavid S. Miller fl6.flowi6_oif = sin6->sin6_scope_id; 8091da177e4SLinus Torvalds } else { 8101da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED) 8111da177e4SLinus Torvalds return -EDESTADDRREQ; 8121da177e4SLinus Torvalds 813c720c7e8SEric Dumazet proto = inet->inet_num; 8141da177e4SLinus Torvalds daddr = &np->daddr; 8154c9483b2SDavid S. Miller fl6.flowlabel = np->flow_label; 8161da177e4SLinus Torvalds } 8171da177e4SLinus Torvalds 8184c9483b2SDavid S. Miller if (fl6.flowi6_oif == 0) 8194c9483b2SDavid S. Miller fl6.flowi6_oif = sk->sk_bound_dev_if; 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds if (msg->msg_controllen) { 8221da177e4SLinus Torvalds opt = &opt_space; 8231da177e4SLinus Torvalds memset(opt, 0, sizeof(struct ipv6_txoptions)); 8241da177e4SLinus Torvalds opt->tot_len = sizeof(struct ipv6_txoptions); 8251da177e4SLinus Torvalds 826ec0506dbSMaciej Żenczykowski err = datagram_send_ctl(sock_net(sk), sk, msg, &fl6, opt, 827ec0506dbSMaciej Żenczykowski &hlimit, &tclass, &dontfrag); 8281da177e4SLinus Torvalds if (err < 0) { 8291da177e4SLinus Torvalds fl6_sock_release(flowlabel); 8301da177e4SLinus Torvalds return err; 8311da177e4SLinus Torvalds } 8324c9483b2SDavid S. Miller if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { 8334c9483b2SDavid S. Miller flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 8341da177e4SLinus Torvalds if (flowlabel == NULL) 8351da177e4SLinus Torvalds return -EINVAL; 8361da177e4SLinus Torvalds } 8371da177e4SLinus Torvalds if (!(opt->opt_nflen|opt->opt_flen)) 8381da177e4SLinus Torvalds opt = NULL; 8391da177e4SLinus Torvalds } 8401da177e4SLinus Torvalds if (opt == NULL) 8411da177e4SLinus Torvalds opt = np->opt; 842df9890c3SYOSHIFUJI Hideaki if (flowlabel) 8431da177e4SLinus Torvalds opt = fl6_merge_options(&opt_space, flowlabel, opt); 844df9890c3SYOSHIFUJI Hideaki opt = ipv6_fixup_options(&opt_space, opt); 8451da177e4SLinus Torvalds 8464c9483b2SDavid S. Miller fl6.flowi6_proto = proto; 8474c9483b2SDavid S. Miller err = rawv6_probe_proto_opt(&fl6, msg); 848a27b58feSHeiko Carstens if (err) 849a27b58feSHeiko Carstens goto out; 8501da177e4SLinus Torvalds 851876c7f41SBrian Haley if (!ipv6_addr_any(daddr)) 8524e3fd7a0SAlexey Dobriyan fl6.daddr = *daddr; 853876c7f41SBrian Haley else 8544c9483b2SDavid S. Miller fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */ 8554c9483b2SDavid S. Miller if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr)) 8564e3fd7a0SAlexey Dobriyan fl6.saddr = np->saddr; 8571da177e4SLinus Torvalds 8584c9483b2SDavid S. Miller final_p = fl6_update_dst(&fl6, opt, &final); 8591da177e4SLinus Torvalds 8604c9483b2SDavid S. Miller if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) 8614c9483b2SDavid S. Miller fl6.flowi6_oif = np->mcast_oif; 862c4062dfcSErich E. Hoover else if (!fl6.flowi6_oif) 863c4062dfcSErich E. Hoover fl6.flowi6_oif = np->ucast_oif; 8644c9483b2SDavid S. Miller security_sk_classify_flow(sk, flowi6_to_flowi(&fl6)); 8651da177e4SLinus Torvalds 8664c9483b2SDavid S. Miller dst = ip6_dst_lookup_flow(sk, &fl6, final_p, true); 86768d0c6d3SDavid S. Miller if (IS_ERR(dst)) { 86868d0c6d3SDavid S. Miller err = PTR_ERR(dst); 8691da177e4SLinus Torvalds goto out; 87014e50e57SDavid S. Miller } 8711da177e4SLinus Torvalds if (hlimit < 0) { 8724c9483b2SDavid S. Miller if (ipv6_addr_is_multicast(&fl6.daddr)) 8731da177e4SLinus Torvalds hlimit = np->mcast_hops; 8741da177e4SLinus Torvalds else 8751da177e4SLinus Torvalds hlimit = np->hop_limit; 8761da177e4SLinus Torvalds if (hlimit < 0) 8776b75d090SYOSHIFUJI Hideaki hlimit = ip6_dst_hoplimit(dst); 8781da177e4SLinus Torvalds } 8791da177e4SLinus Torvalds 88041a1f8eaSYOSHIFUJI Hideaki if (tclass < 0) 881e651f03aSGerrit Renker tclass = np->tclass; 88241a1f8eaSYOSHIFUJI Hideaki 88313b52cd4SBrian Haley if (dontfrag < 0) 88413b52cd4SBrian Haley dontfrag = np->dontfrag; 88513b52cd4SBrian Haley 8861da177e4SLinus Torvalds if (msg->msg_flags&MSG_CONFIRM) 8871da177e4SLinus Torvalds goto do_confirm; 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds back_from_confirm: 8901789a640SEric Dumazet if (inet->hdrincl) 8914c9483b2SDavid S. Miller err = rawv6_send_hdrinc(sk, msg->msg_iov, len, &fl6, &dst, msg->msg_flags); 8921789a640SEric Dumazet else { 8931da177e4SLinus Torvalds lock_sock(sk); 89441a1f8eaSYOSHIFUJI Hideaki err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov, 8954c9483b2SDavid S. Miller len, 0, hlimit, tclass, opt, &fl6, (struct rt6_info*)dst, 89613b52cd4SBrian Haley msg->msg_flags, dontfrag); 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds if (err) 8991da177e4SLinus Torvalds ip6_flush_pending_frames(sk); 9001da177e4SLinus Torvalds else if (!(msg->msg_flags & MSG_MORE)) 9014c9483b2SDavid S. Miller err = rawv6_push_pending_frames(sk, &fl6, rp); 9023ef9d943SYOSHIFUJI Hideaki release_sock(sk); 9031da177e4SLinus Torvalds } 9041da177e4SLinus Torvalds done: 9056d3e85ecSNicolas DICHTEL dst_release(dst); 9061da177e4SLinus Torvalds out: 9071da177e4SLinus Torvalds fl6_sock_release(flowlabel); 9081da177e4SLinus Torvalds return err<0?err:len; 9091da177e4SLinus Torvalds do_confirm: 9101da177e4SLinus Torvalds dst_confirm(dst); 9111da177e4SLinus Torvalds if (!(msg->msg_flags & MSG_PROBE) || len) 9121da177e4SLinus Torvalds goto back_from_confirm; 9131da177e4SLinus Torvalds err = 0; 9141da177e4SLinus Torvalds goto done; 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds static int rawv6_seticmpfilter(struct sock *sk, int level, int optname, 9181da177e4SLinus Torvalds char __user *optval, int optlen) 9191da177e4SLinus Torvalds { 9201da177e4SLinus Torvalds switch (optname) { 9211da177e4SLinus Torvalds case ICMPV6_FILTER: 9221da177e4SLinus Torvalds if (optlen > sizeof(struct icmp6_filter)) 9231da177e4SLinus Torvalds optlen = sizeof(struct icmp6_filter); 9241da177e4SLinus Torvalds if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen)) 9251da177e4SLinus Torvalds return -EFAULT; 9261da177e4SLinus Torvalds return 0; 9271da177e4SLinus Torvalds default: 9281da177e4SLinus Torvalds return -ENOPROTOOPT; 9293ff50b79SStephen Hemminger } 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds return 0; 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds static int rawv6_geticmpfilter(struct sock *sk, int level, int optname, 9351da177e4SLinus Torvalds char __user *optval, int __user *optlen) 9361da177e4SLinus Torvalds { 9371da177e4SLinus Torvalds int len; 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds switch (optname) { 9401da177e4SLinus Torvalds case ICMPV6_FILTER: 9411da177e4SLinus Torvalds if (get_user(len, optlen)) 9421da177e4SLinus Torvalds return -EFAULT; 9431da177e4SLinus Torvalds if (len < 0) 9441da177e4SLinus Torvalds return -EINVAL; 9451da177e4SLinus Torvalds if (len > sizeof(struct icmp6_filter)) 9461da177e4SLinus Torvalds len = sizeof(struct icmp6_filter); 9471da177e4SLinus Torvalds if (put_user(len, optlen)) 9481da177e4SLinus Torvalds return -EFAULT; 9491da177e4SLinus Torvalds if (copy_to_user(optval, &raw6_sk(sk)->filter, len)) 9501da177e4SLinus Torvalds return -EFAULT; 9511da177e4SLinus Torvalds return 0; 9521da177e4SLinus Torvalds default: 9531da177e4SLinus Torvalds return -ENOPROTOOPT; 9543ff50b79SStephen Hemminger } 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds return 0; 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds 9603fdadf7dSDmitry Mishin static int do_rawv6_setsockopt(struct sock *sk, int level, int optname, 961b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 9621da177e4SLinus Torvalds { 9631da177e4SLinus Torvalds struct raw6_sock *rp = raw6_sk(sk); 9641da177e4SLinus Torvalds int val; 9651da177e4SLinus Torvalds 9661da177e4SLinus Torvalds if (get_user(val, (int __user *)optval)) 9671da177e4SLinus Torvalds return -EFAULT; 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds switch (optname) { 9701da177e4SLinus Torvalds case IPV6_CHECKSUM: 971c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num == IPPROTO_ICMPV6 && 9721a98d05fSYOSHIFUJI Hideaki level == IPPROTO_IPV6) { 9731a98d05fSYOSHIFUJI Hideaki /* 9741a98d05fSYOSHIFUJI Hideaki * RFC3542 tells that IPV6_CHECKSUM socket 9751a98d05fSYOSHIFUJI Hideaki * option in the IPPROTO_IPV6 level is not 9761a98d05fSYOSHIFUJI Hideaki * allowed on ICMPv6 sockets. 9771a98d05fSYOSHIFUJI Hideaki * If you want to set it, use IPPROTO_RAW 9781a98d05fSYOSHIFUJI Hideaki * level IPV6_CHECKSUM socket option 9791a98d05fSYOSHIFUJI Hideaki * (Linux extension). 9801a98d05fSYOSHIFUJI Hideaki */ 9811a98d05fSYOSHIFUJI Hideaki return -EINVAL; 9821a98d05fSYOSHIFUJI Hideaki } 9831a98d05fSYOSHIFUJI Hideaki 9841da177e4SLinus Torvalds /* You may get strange result with a positive odd offset; 9851da177e4SLinus Torvalds RFC2292bis agrees with me. */ 9861da177e4SLinus Torvalds if (val > 0 && (val&1)) 987a02cec21SEric Dumazet return -EINVAL; 9881da177e4SLinus Torvalds if (val < 0) { 9891da177e4SLinus Torvalds rp->checksum = 0; 9901da177e4SLinus Torvalds } else { 9911da177e4SLinus Torvalds rp->checksum = 1; 9921da177e4SLinus Torvalds rp->offset = val; 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds return 0; 9961da177e4SLinus Torvalds 9971da177e4SLinus Torvalds default: 998a02cec21SEric Dumazet return -ENOPROTOOPT; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds 10023fdadf7dSDmitry Mishin static int rawv6_setsockopt(struct sock *sk, int level, int optname, 1003b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 10041da177e4SLinus Torvalds { 10051da177e4SLinus Torvalds switch (level) { 10061da177e4SLinus Torvalds case SOL_RAW: 10071da177e4SLinus Torvalds break; 10081da177e4SLinus Torvalds 10091da177e4SLinus Torvalds case SOL_ICMPV6: 1010c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6) 10111da177e4SLinus Torvalds return -EOPNOTSUPP; 1012207ec0abSJoe Perches return rawv6_seticmpfilter(sk, level, optname, optval, optlen); 10131da177e4SLinus Torvalds case SOL_IPV6: 10141da177e4SLinus Torvalds if (optname == IPV6_CHECKSUM) 10151da177e4SLinus Torvalds break; 10161da177e4SLinus Torvalds default: 1017207ec0abSJoe Perches return ipv6_setsockopt(sk, level, optname, optval, optlen); 10183ff50b79SStephen Hemminger } 10193ff50b79SStephen Hemminger 10203fdadf7dSDmitry Mishin return do_rawv6_setsockopt(sk, level, optname, optval, optlen); 10213fdadf7dSDmitry Mishin } 10223fdadf7dSDmitry Mishin 10233fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 10243fdadf7dSDmitry Mishin static int compat_rawv6_setsockopt(struct sock *sk, int level, int optname, 1025b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 10263fdadf7dSDmitry Mishin { 10273fdadf7dSDmitry Mishin switch (level) { 10283fdadf7dSDmitry Mishin case SOL_RAW: 10293fdadf7dSDmitry Mishin break; 10303fdadf7dSDmitry Mishin case SOL_ICMPV6: 1031c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6) 10323fdadf7dSDmitry Mishin return -EOPNOTSUPP; 1033543d9cfeSArnaldo Carvalho de Melo return rawv6_seticmpfilter(sk, level, optname, optval, optlen); 10343fdadf7dSDmitry Mishin case SOL_IPV6: 10353fdadf7dSDmitry Mishin if (optname == IPV6_CHECKSUM) 10363fdadf7dSDmitry Mishin break; 10373fdadf7dSDmitry Mishin default: 1038543d9cfeSArnaldo Carvalho de Melo return compat_ipv6_setsockopt(sk, level, optname, 1039543d9cfeSArnaldo Carvalho de Melo optval, optlen); 10403ff50b79SStephen Hemminger } 10413fdadf7dSDmitry Mishin return do_rawv6_setsockopt(sk, level, optname, optval, optlen); 10423fdadf7dSDmitry Mishin } 10433fdadf7dSDmitry Mishin #endif 10443fdadf7dSDmitry Mishin 10453fdadf7dSDmitry Mishin static int do_rawv6_getsockopt(struct sock *sk, int level, int optname, 10463fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 10473fdadf7dSDmitry Mishin { 10483fdadf7dSDmitry Mishin struct raw6_sock *rp = raw6_sk(sk); 10493fdadf7dSDmitry Mishin int val, len; 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds if (get_user(len,optlen)) 10521da177e4SLinus Torvalds return -EFAULT; 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds switch (optname) { 10551da177e4SLinus Torvalds case IPV6_CHECKSUM: 10561a98d05fSYOSHIFUJI Hideaki /* 10571a98d05fSYOSHIFUJI Hideaki * We allow getsockopt() for IPPROTO_IPV6-level 10581a98d05fSYOSHIFUJI Hideaki * IPV6_CHECKSUM socket option on ICMPv6 sockets 10591a98d05fSYOSHIFUJI Hideaki * since RFC3542 is silent about it. 10601a98d05fSYOSHIFUJI Hideaki */ 10611da177e4SLinus Torvalds if (rp->checksum == 0) 10621da177e4SLinus Torvalds val = -1; 10631da177e4SLinus Torvalds else 10641da177e4SLinus Torvalds val = rp->offset; 10651da177e4SLinus Torvalds break; 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds default: 10681da177e4SLinus Torvalds return -ENOPROTOOPT; 10691da177e4SLinus Torvalds } 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds len = min_t(unsigned int, sizeof(int), len); 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds if (put_user(len, optlen)) 10741da177e4SLinus Torvalds return -EFAULT; 10751da177e4SLinus Torvalds if (copy_to_user(optval,&val,len)) 10761da177e4SLinus Torvalds return -EFAULT; 10771da177e4SLinus Torvalds return 0; 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds 10803fdadf7dSDmitry Mishin static int rawv6_getsockopt(struct sock *sk, int level, int optname, 10813fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 10823fdadf7dSDmitry Mishin { 10833fdadf7dSDmitry Mishin switch (level) { 10843fdadf7dSDmitry Mishin case SOL_RAW: 10853fdadf7dSDmitry Mishin break; 10863fdadf7dSDmitry Mishin 10873fdadf7dSDmitry Mishin case SOL_ICMPV6: 1088c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6) 10893fdadf7dSDmitry Mishin return -EOPNOTSUPP; 1090207ec0abSJoe Perches return rawv6_geticmpfilter(sk, level, optname, optval, optlen); 10913fdadf7dSDmitry Mishin case SOL_IPV6: 10923fdadf7dSDmitry Mishin if (optname == IPV6_CHECKSUM) 10933fdadf7dSDmitry Mishin break; 10943fdadf7dSDmitry Mishin default: 1095207ec0abSJoe Perches return ipv6_getsockopt(sk, level, optname, optval, optlen); 10963ff50b79SStephen Hemminger } 10973ff50b79SStephen Hemminger 10983fdadf7dSDmitry Mishin return do_rawv6_getsockopt(sk, level, optname, optval, optlen); 10993fdadf7dSDmitry Mishin } 11003fdadf7dSDmitry Mishin 11013fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 11023fdadf7dSDmitry Mishin static int compat_rawv6_getsockopt(struct sock *sk, int level, int optname, 11033fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 11043fdadf7dSDmitry Mishin { 11053fdadf7dSDmitry Mishin switch (level) { 11063fdadf7dSDmitry Mishin case SOL_RAW: 11073fdadf7dSDmitry Mishin break; 11083fdadf7dSDmitry Mishin case SOL_ICMPV6: 1109c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6) 11103fdadf7dSDmitry Mishin return -EOPNOTSUPP; 1111543d9cfeSArnaldo Carvalho de Melo return rawv6_geticmpfilter(sk, level, optname, optval, optlen); 11123fdadf7dSDmitry Mishin case SOL_IPV6: 11133fdadf7dSDmitry Mishin if (optname == IPV6_CHECKSUM) 11143fdadf7dSDmitry Mishin break; 11153fdadf7dSDmitry Mishin default: 1116543d9cfeSArnaldo Carvalho de Melo return compat_ipv6_getsockopt(sk, level, optname, 1117543d9cfeSArnaldo Carvalho de Melo optval, optlen); 11183ff50b79SStephen Hemminger } 11193fdadf7dSDmitry Mishin return do_rawv6_getsockopt(sk, level, optname, optval, optlen); 11203fdadf7dSDmitry Mishin } 11213fdadf7dSDmitry Mishin #endif 11223fdadf7dSDmitry Mishin 11231da177e4SLinus Torvalds static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg) 11241da177e4SLinus Torvalds { 11251da177e4SLinus Torvalds switch (cmd) { 1126207ec0abSJoe Perches case SIOCOUTQ: { 112731e6d363SEric Dumazet int amount = sk_wmem_alloc_get(sk); 112831e6d363SEric Dumazet 11291da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 11301da177e4SLinus Torvalds } 1131207ec0abSJoe Perches case SIOCINQ: { 11321da177e4SLinus Torvalds struct sk_buff *skb; 11331da177e4SLinus Torvalds int amount = 0; 11341da177e4SLinus Torvalds 1135e0f9f858SHerbert Xu spin_lock_bh(&sk->sk_receive_queue.lock); 11361da177e4SLinus Torvalds skb = skb_peek(&sk->sk_receive_queue); 11371da177e4SLinus Torvalds if (skb != NULL) 113827a884dcSArnaldo Carvalho de Melo amount = skb->tail - skb->transport_header; 1139e0f9f858SHerbert Xu spin_unlock_bh(&sk->sk_receive_queue.lock); 11401da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds default: 11447bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 11457bc570c8SYOSHIFUJI Hideaki return ip6mr_ioctl(sk, cmd, (void __user *)arg); 11467bc570c8SYOSHIFUJI Hideaki #else 11471da177e4SLinus Torvalds return -ENOIOCTLCMD; 11487bc570c8SYOSHIFUJI Hideaki #endif 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds } 11511da177e4SLinus Torvalds 1152e2d57766SDavid S. Miller #ifdef CONFIG_COMPAT 1153e2d57766SDavid S. Miller static int compat_rawv6_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg) 1154e2d57766SDavid S. Miller { 1155e2d57766SDavid S. Miller switch (cmd) { 1156e2d57766SDavid S. Miller case SIOCOUTQ: 1157e2d57766SDavid S. Miller case SIOCINQ: 1158e2d57766SDavid S. Miller return -ENOIOCTLCMD; 1159e2d57766SDavid S. Miller default: 1160e2d57766SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 1161e2d57766SDavid S. Miller return ip6mr_compat_ioctl(sk, cmd, compat_ptr(arg)); 1162e2d57766SDavid S. Miller #else 1163e2d57766SDavid S. Miller return -ENOIOCTLCMD; 1164e2d57766SDavid S. Miller #endif 1165e2d57766SDavid S. Miller } 1166e2d57766SDavid S. Miller } 1167e2d57766SDavid S. Miller #endif 1168e2d57766SDavid S. Miller 11691da177e4SLinus Torvalds static void rawv6_close(struct sock *sk, long timeout) 11701da177e4SLinus Torvalds { 1171c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num == IPPROTO_RAW) 1172725a8ff0SDenis V. Lunev ip6_ra_control(sk, -1); 11737bc570c8SYOSHIFUJI Hideaki ip6mr_sk_done(sk); 11741da177e4SLinus Torvalds sk_common_release(sk); 11751da177e4SLinus Torvalds } 11761da177e4SLinus Torvalds 11777d06b2e0SBrian Haley static void raw6_destroy(struct sock *sk) 117822dd4850SDenis V. Lunev { 117922dd4850SDenis V. Lunev lock_sock(sk); 118022dd4850SDenis V. Lunev ip6_flush_pending_frames(sk); 118122dd4850SDenis V. Lunev release_sock(sk); 1182f23d60deSDavid S. Miller 11837d06b2e0SBrian Haley inet6_destroy_sock(sk); 118422dd4850SDenis V. Lunev } 118522dd4850SDenis V. Lunev 11861da177e4SLinus Torvalds static int rawv6_init_sk(struct sock *sk) 11871da177e4SLinus Torvalds { 11881da177e4SLinus Torvalds struct raw6_sock *rp = raw6_sk(sk); 1189f48d5ff1SMasahide NAKAMURA 1190c720c7e8SEric Dumazet switch (inet_sk(sk)->inet_num) { 1191f48d5ff1SMasahide NAKAMURA case IPPROTO_ICMPV6: 11921da177e4SLinus Torvalds rp->checksum = 1; 11931da177e4SLinus Torvalds rp->offset = 2; 1194f48d5ff1SMasahide NAKAMURA break; 1195f48d5ff1SMasahide NAKAMURA case IPPROTO_MH: 1196f48d5ff1SMasahide NAKAMURA rp->checksum = 1; 1197f48d5ff1SMasahide NAKAMURA rp->offset = 4; 1198f48d5ff1SMasahide NAKAMURA break; 1199f48d5ff1SMasahide NAKAMURA default: 1200f48d5ff1SMasahide NAKAMURA break; 12011da177e4SLinus Torvalds } 1202a02cec21SEric Dumazet return 0; 12031da177e4SLinus Torvalds } 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds struct proto rawv6_prot = { 12061da177e4SLinus Torvalds .name = "RAWv6", 12071da177e4SLinus Torvalds .owner = THIS_MODULE, 12081da177e4SLinus Torvalds .close = rawv6_close, 120922dd4850SDenis V. Lunev .destroy = raw6_destroy, 12101da177e4SLinus Torvalds .connect = ip6_datagram_connect, 12111da177e4SLinus Torvalds .disconnect = udp_disconnect, 12121da177e4SLinus Torvalds .ioctl = rawv6_ioctl, 12131da177e4SLinus Torvalds .init = rawv6_init_sk, 12141da177e4SLinus Torvalds .setsockopt = rawv6_setsockopt, 12151da177e4SLinus Torvalds .getsockopt = rawv6_getsockopt, 12161da177e4SLinus Torvalds .sendmsg = rawv6_sendmsg, 12171da177e4SLinus Torvalds .recvmsg = rawv6_recvmsg, 12181da177e4SLinus Torvalds .bind = rawv6_bind, 12191da177e4SLinus Torvalds .backlog_rcv = rawv6_rcv_skb, 1220fc8717baSPavel Emelyanov .hash = raw_hash_sk, 1221fc8717baSPavel Emelyanov .unhash = raw_unhash_sk, 12221da177e4SLinus Torvalds .obj_size = sizeof(struct raw6_sock), 1223fc8717baSPavel Emelyanov .h.raw_hash = &raw_v6_hashinfo, 1224543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 1225543d9cfeSArnaldo Carvalho de Melo .compat_setsockopt = compat_rawv6_setsockopt, 1226543d9cfeSArnaldo Carvalho de Melo .compat_getsockopt = compat_rawv6_getsockopt, 1227e2d57766SDavid S. Miller .compat_ioctl = compat_rawv6_ioctl, 1228543d9cfeSArnaldo Carvalho de Melo #endif 12291da177e4SLinus Torvalds }; 12301da177e4SLinus Torvalds 12311da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 12321da177e4SLinus Torvalds static void raw6_sock_seq_show(struct seq_file *seq, struct sock *sp, int i) 12331da177e4SLinus Torvalds { 12341da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sp); 1235b71d1d42SEric Dumazet const struct in6_addr *dest, *src; 12361da177e4SLinus Torvalds __u16 destp, srcp; 12371da177e4SLinus Torvalds 12381da177e4SLinus Torvalds dest = &np->daddr; 12391da177e4SLinus Torvalds src = &np->rcv_saddr; 12401da177e4SLinus Torvalds destp = 0; 1241c720c7e8SEric Dumazet srcp = inet_sk(sp)->inet_num; 12421da177e4SLinus Torvalds seq_printf(seq, 12431da177e4SLinus Torvalds "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X " 124471338aa7SDan Rosenberg "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d\n", 12451da177e4SLinus Torvalds i, 12461da177e4SLinus Torvalds src->s6_addr32[0], src->s6_addr32[1], 12471da177e4SLinus Torvalds src->s6_addr32[2], src->s6_addr32[3], srcp, 12481da177e4SLinus Torvalds dest->s6_addr32[0], dest->s6_addr32[1], 12491da177e4SLinus Torvalds dest->s6_addr32[2], dest->s6_addr32[3], destp, 12501da177e4SLinus Torvalds sp->sk_state, 125131e6d363SEric Dumazet sk_wmem_alloc_get(sp), 125231e6d363SEric Dumazet sk_rmem_alloc_get(sp), 12531da177e4SLinus Torvalds 0, 0L, 0, 1254a7cb5a49SEric W. Biederman from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)), 1255a7cb5a49SEric W. Biederman 0, 12561da177e4SLinus Torvalds sock_i_ino(sp), 1257a92aa318SWang Chen atomic_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops)); 12581da177e4SLinus Torvalds } 12591da177e4SLinus Torvalds 12601da177e4SLinus Torvalds static int raw6_seq_show(struct seq_file *seq, void *v) 12611da177e4SLinus Torvalds { 12621da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 12631da177e4SLinus Torvalds seq_printf(seq, 12641da177e4SLinus Torvalds " sl " 12651da177e4SLinus Torvalds "local_address " 12661da177e4SLinus Torvalds "remote_address " 12671da177e4SLinus Torvalds "st tx_queue rx_queue tr tm->when retrnsmt" 1268cb61cb9bSEric Dumazet " uid timeout inode ref pointer drops\n"); 12691da177e4SLinus Torvalds else 127042a73808SPavel Emelyanov raw6_sock_seq_show(seq, v, raw_seq_private(seq)->bucket); 12711da177e4SLinus Torvalds return 0; 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds 127456b3d975SPhilippe De Muyter static const struct seq_operations raw6_seq_ops = { 127542a73808SPavel Emelyanov .start = raw_seq_start, 127642a73808SPavel Emelyanov .next = raw_seq_next, 127742a73808SPavel Emelyanov .stop = raw_seq_stop, 12781da177e4SLinus Torvalds .show = raw6_seq_show, 12791da177e4SLinus Torvalds }; 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds static int raw6_seq_open(struct inode *inode, struct file *file) 12821da177e4SLinus Torvalds { 12833046d767SDenis V. Lunev return raw_seq_open(inode, file, &raw_v6_hashinfo, &raw6_seq_ops); 12841da177e4SLinus Torvalds } 12851da177e4SLinus Torvalds 12869a32144eSArjan van de Ven static const struct file_operations raw6_seq_fops = { 12871da177e4SLinus Torvalds .owner = THIS_MODULE, 12881da177e4SLinus Torvalds .open = raw6_seq_open, 12891da177e4SLinus Torvalds .read = seq_read, 12901da177e4SLinus Torvalds .llseek = seq_lseek, 1291f51d599fSPavel Emelyanov .release = seq_release_net, 12921da177e4SLinus Torvalds }; 12931da177e4SLinus Torvalds 12942c8c1e72SAlexey Dobriyan static int __net_init raw6_init_net(struct net *net) 1295a308da16SPavel Emelyanov { 1296a308da16SPavel Emelyanov if (!proc_net_fops_create(net, "raw6", S_IRUGO, &raw6_seq_fops)) 1297a308da16SPavel Emelyanov return -ENOMEM; 1298a308da16SPavel Emelyanov 1299a308da16SPavel Emelyanov return 0; 1300a308da16SPavel Emelyanov } 1301a308da16SPavel Emelyanov 13022c8c1e72SAlexey Dobriyan static void __net_exit raw6_exit_net(struct net *net) 1303a308da16SPavel Emelyanov { 1304a308da16SPavel Emelyanov proc_net_remove(net, "raw6"); 1305a308da16SPavel Emelyanov } 1306a308da16SPavel Emelyanov 1307a308da16SPavel Emelyanov static struct pernet_operations raw6_net_ops = { 1308a308da16SPavel Emelyanov .init = raw6_init_net, 1309a308da16SPavel Emelyanov .exit = raw6_exit_net, 1310a308da16SPavel Emelyanov }; 1311a308da16SPavel Emelyanov 13121da177e4SLinus Torvalds int __init raw6_proc_init(void) 13131da177e4SLinus Torvalds { 1314a308da16SPavel Emelyanov return register_pernet_subsys(&raw6_net_ops); 13151da177e4SLinus Torvalds } 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds void raw6_proc_exit(void) 13181da177e4SLinus Torvalds { 1319a308da16SPavel Emelyanov unregister_pernet_subsys(&raw6_net_ops); 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 13227f4e4868SDaniel Lezcano 13237f4e4868SDaniel Lezcano /* Same as inet6_dgram_ops, sans udp_poll. */ 13247f4e4868SDaniel Lezcano static const struct proto_ops inet6_sockraw_ops = { 13257f4e4868SDaniel Lezcano .family = PF_INET6, 13267f4e4868SDaniel Lezcano .owner = THIS_MODULE, 13277f4e4868SDaniel Lezcano .release = inet6_release, 13287f4e4868SDaniel Lezcano .bind = inet6_bind, 13297f4e4868SDaniel Lezcano .connect = inet_dgram_connect, /* ok */ 13307f4e4868SDaniel Lezcano .socketpair = sock_no_socketpair, /* a do nothing */ 13317f4e4868SDaniel Lezcano .accept = sock_no_accept, /* a do nothing */ 13327f4e4868SDaniel Lezcano .getname = inet6_getname, 13337f4e4868SDaniel Lezcano .poll = datagram_poll, /* ok */ 13347f4e4868SDaniel Lezcano .ioctl = inet6_ioctl, /* must change */ 13357f4e4868SDaniel Lezcano .listen = sock_no_listen, /* ok */ 13367f4e4868SDaniel Lezcano .shutdown = inet_shutdown, /* ok */ 13377f4e4868SDaniel Lezcano .setsockopt = sock_common_setsockopt, /* ok */ 13387f4e4868SDaniel Lezcano .getsockopt = sock_common_getsockopt, /* ok */ 13397f4e4868SDaniel Lezcano .sendmsg = inet_sendmsg, /* ok */ 13407f4e4868SDaniel Lezcano .recvmsg = sock_common_recvmsg, /* ok */ 13417f4e4868SDaniel Lezcano .mmap = sock_no_mmap, 13427f4e4868SDaniel Lezcano .sendpage = sock_no_sendpage, 13437f4e4868SDaniel Lezcano #ifdef CONFIG_COMPAT 13447f4e4868SDaniel Lezcano .compat_setsockopt = compat_sock_common_setsockopt, 13457f4e4868SDaniel Lezcano .compat_getsockopt = compat_sock_common_getsockopt, 13467f4e4868SDaniel Lezcano #endif 13477f4e4868SDaniel Lezcano }; 13487f4e4868SDaniel Lezcano 13497f4e4868SDaniel Lezcano static struct inet_protosw rawv6_protosw = { 13507f4e4868SDaniel Lezcano .type = SOCK_RAW, 13517f4e4868SDaniel Lezcano .protocol = IPPROTO_IP, /* wild card */ 13527f4e4868SDaniel Lezcano .prot = &rawv6_prot, 13537f4e4868SDaniel Lezcano .ops = &inet6_sockraw_ops, 13547f4e4868SDaniel Lezcano .no_check = UDP_CSUM_DEFAULT, 13557f4e4868SDaniel Lezcano .flags = INET_PROTOSW_REUSE, 13567f4e4868SDaniel Lezcano }; 13577f4e4868SDaniel Lezcano 13587f4e4868SDaniel Lezcano int __init rawv6_init(void) 13597f4e4868SDaniel Lezcano { 13607f4e4868SDaniel Lezcano int ret; 13617f4e4868SDaniel Lezcano 13627f4e4868SDaniel Lezcano ret = inet6_register_protosw(&rawv6_protosw); 13637f4e4868SDaniel Lezcano if (ret) 13647f4e4868SDaniel Lezcano goto out; 13657f4e4868SDaniel Lezcano out: 13667f4e4868SDaniel Lezcano return ret; 13677f4e4868SDaniel Lezcano } 13687f4e4868SDaniel Lezcano 136909f7709fSDaniel Lezcano void rawv6_exit(void) 13707f4e4868SDaniel Lezcano { 13717f4e4868SDaniel Lezcano inet6_unregister_protosw(&rawv6_protosw); 13727f4e4868SDaniel Lezcano } 1373