11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Internet Control Message Protocol (ICMPv6) 31da177e4SLinus Torvalds * Linux INET6 implementation 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: 61da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Based on net/ipv4/icmp.c 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * RFC 1885 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 131da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 141da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 151da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds /* 191da177e4SLinus Torvalds * Changes: 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Andi Kleen : exception handling 221da177e4SLinus Torvalds * Andi Kleen add rate limits. never reply to a icmp. 231da177e4SLinus Torvalds * add more length checks and other fixes. 241da177e4SLinus Torvalds * yoshfuji : ensure to sent parameter problem for 251da177e4SLinus Torvalds * fragments. 261da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI: added sysctl for icmp rate limit. 271da177e4SLinus Torvalds * Randy Dunlap and 281da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI: Per-interface statistics support 291da177e4SLinus Torvalds * Kazunori MIYAZAWA @USAGI: change output process to use ip6_append_data 301da177e4SLinus Torvalds */ 311da177e4SLinus Torvalds 32f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 33f3213831SJoe Perches 341da177e4SLinus Torvalds #include <linux/module.h> 351da177e4SLinus Torvalds #include <linux/errno.h> 361da177e4SLinus Torvalds #include <linux/types.h> 371da177e4SLinus Torvalds #include <linux/socket.h> 381da177e4SLinus Torvalds #include <linux/in.h> 391da177e4SLinus Torvalds #include <linux/kernel.h> 401da177e4SLinus Torvalds #include <linux/sockios.h> 411da177e4SLinus Torvalds #include <linux/net.h> 421da177e4SLinus Torvalds #include <linux/skbuff.h> 431da177e4SLinus Torvalds #include <linux/init.h> 44763ecff1SYasuyuki Kozakai #include <linux/netfilter.h> 455a0e3ad6STejun Heo #include <linux/slab.h> 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 481da177e4SLinus Torvalds #include <linux/sysctl.h> 491da177e4SLinus Torvalds #endif 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds #include <linux/inet.h> 521da177e4SLinus Torvalds #include <linux/netdevice.h> 531da177e4SLinus Torvalds #include <linux/icmpv6.h> 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds #include <net/ip.h> 561da177e4SLinus Torvalds #include <net/sock.h> 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds #include <net/ipv6.h> 591da177e4SLinus Torvalds #include <net/ip6_checksum.h> 606d0bfe22SLorenzo Colitti #include <net/ping.h> 611da177e4SLinus Torvalds #include <net/protocol.h> 621da177e4SLinus Torvalds #include <net/raw.h> 631da177e4SLinus Torvalds #include <net/rawv6.h> 641da177e4SLinus Torvalds #include <net/transp_v6.h> 651da177e4SLinus Torvalds #include <net/ip6_route.h> 661da177e4SLinus Torvalds #include <net/addrconf.h> 671da177e4SLinus Torvalds #include <net/icmp.h> 688b7817f3SHerbert Xu #include <net/xfrm.h> 691ed8516fSDenis V. Lunev #include <net/inet_common.h> 70825edac4SHannes Frederic Sowa #include <net/dsfield.h> 71ca254490SDavid Ahern #include <net/l3mdev.h> 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds #include <asm/uaccess.h> 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds /* 761da177e4SLinus Torvalds * The ICMP socket(s). This is the most convenient way to flow control 771da177e4SLinus Torvalds * our ICMP output as well as maintain a clean interface throughout 781da177e4SLinus Torvalds * all layers. All Socketless IP sends will soon be gone. 791da177e4SLinus Torvalds * 801da177e4SLinus Torvalds * On SMP we have one ICMP socket per-cpu. 811da177e4SLinus Torvalds */ 8298c6d1b2SDenis V. Lunev static inline struct sock *icmpv6_sk(struct net *net) 8398c6d1b2SDenis V. Lunev { 8498c6d1b2SDenis V. Lunev return net->ipv6.icmp_sk[smp_processor_id()]; 8598c6d1b2SDenis V. Lunev } 861da177e4SLinus Torvalds 876f809da2SSteffen Klassert static void icmpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 886f809da2SSteffen Klassert u8 type, u8 code, int offset, __be32 info) 896f809da2SSteffen Klassert { 906d0bfe22SLorenzo Colitti /* icmpv6_notify checks 8 bytes can be pulled, icmp6hdr is 8 bytes */ 916d0bfe22SLorenzo Colitti struct icmp6hdr *icmp6 = (struct icmp6hdr *) (skb->data + offset); 926f809da2SSteffen Klassert struct net *net = dev_net(skb->dev); 936f809da2SSteffen Klassert 946f809da2SSteffen Klassert if (type == ICMPV6_PKT_TOOBIG) 956f809da2SSteffen Klassert ip6_update_pmtu(skb, net, info, 0, 0); 966f809da2SSteffen Klassert else if (type == NDISC_REDIRECT) 97b55b76b2SDuan Jiong ip6_redirect(skb, net, skb->dev->ifindex, 0); 986d0bfe22SLorenzo Colitti 996d0bfe22SLorenzo Colitti if (!(type & ICMPV6_INFOMSG_MASK)) 1006d0bfe22SLorenzo Colitti if (icmp6->icmp6_type == ICMPV6_ECHO_REQUEST) 1016d0bfe22SLorenzo Colitti ping_err(skb, offset, info); 1026f809da2SSteffen Klassert } 1036f809da2SSteffen Klassert 104e5bbef20SHerbert Xu static int icmpv6_rcv(struct sk_buff *skb); 1051da177e4SLinus Torvalds 10641135cc8SAlexey Dobriyan static const struct inet6_protocol icmpv6_protocol = { 1071da177e4SLinus Torvalds .handler = icmpv6_rcv, 1086f809da2SSteffen Klassert .err_handler = icmpv6_err, 1098b7817f3SHerbert Xu .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, 1101da177e4SLinus Torvalds }; 1111da177e4SLinus Torvalds 112fdc0bde9SDenis V. Lunev static __inline__ struct sock *icmpv6_xmit_lock(struct net *net) 1131da177e4SLinus Torvalds { 114fdc0bde9SDenis V. Lunev struct sock *sk; 115fdc0bde9SDenis V. Lunev 1161da177e4SLinus Torvalds local_bh_disable(); 1171da177e4SLinus Torvalds 118fdc0bde9SDenis V. Lunev sk = icmpv6_sk(net); 119405666dbSDenis V. Lunev if (unlikely(!spin_trylock(&sk->sk_lock.slock))) { 1201da177e4SLinus Torvalds /* This can happen if the output path (f.e. SIT or 1211da177e4SLinus Torvalds * ip6ip6 tunnel) signals dst_link_failure() for an 1221da177e4SLinus Torvalds * outgoing ICMP6 packet. 1231da177e4SLinus Torvalds */ 1241da177e4SLinus Torvalds local_bh_enable(); 125fdc0bde9SDenis V. Lunev return NULL; 1261da177e4SLinus Torvalds } 127fdc0bde9SDenis V. Lunev return sk; 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 130405666dbSDenis V. Lunev static __inline__ void icmpv6_xmit_unlock(struct sock *sk) 1311da177e4SLinus Torvalds { 132405666dbSDenis V. Lunev spin_unlock_bh(&sk->sk_lock.slock); 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds /* 1361da177e4SLinus Torvalds * Figure out, may we reply to this packet with icmp error. 1371da177e4SLinus Torvalds * 1381da177e4SLinus Torvalds * We do not reply, if: 1391da177e4SLinus Torvalds * - it was icmp error message. 1401da177e4SLinus Torvalds * - it is truncated, so that it is known, that protocol is ICMPV6 1411da177e4SLinus Torvalds * (i.e. in the middle of some exthdr) 1421da177e4SLinus Torvalds * 1431da177e4SLinus Torvalds * --ANK (980726) 1441da177e4SLinus Torvalds */ 1451da177e4SLinus Torvalds 146a50feda5SEric Dumazet static bool is_ineligible(const struct sk_buff *skb) 1471da177e4SLinus Torvalds { 1480660e03fSArnaldo Carvalho de Melo int ptr = (u8 *)(ipv6_hdr(skb) + 1) - skb->data; 1491da177e4SLinus Torvalds int len = skb->len - ptr; 1500660e03fSArnaldo Carvalho de Melo __u8 nexthdr = ipv6_hdr(skb)->nexthdr; 15175f2811cSJesse Gross __be16 frag_off; 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds if (len < 0) 154a50feda5SEric Dumazet return true; 1551da177e4SLinus Torvalds 15675f2811cSJesse Gross ptr = ipv6_skip_exthdr(skb, ptr, &nexthdr, &frag_off); 1571da177e4SLinus Torvalds if (ptr < 0) 158a50feda5SEric Dumazet return false; 1591da177e4SLinus Torvalds if (nexthdr == IPPROTO_ICMPV6) { 1601da177e4SLinus Torvalds u8 _type, *tp; 1611da177e4SLinus Torvalds tp = skb_header_pointer(skb, 1621da177e4SLinus Torvalds ptr+offsetof(struct icmp6hdr, icmp6_type), 1631da177e4SLinus Torvalds sizeof(_type), &_type); 16463159f29SIan Morris if (!tp || !(*tp & ICMPV6_INFOMSG_MASK)) 165a50feda5SEric Dumazet return true; 1661da177e4SLinus Torvalds } 167a50feda5SEric Dumazet return false; 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds /* 1711da177e4SLinus Torvalds * Check the ICMP output rate limit 1721da177e4SLinus Torvalds */ 1734cdf507dSEric Dumazet static bool icmpv6_xrlim_allow(struct sock *sk, u8 type, 1744c9483b2SDavid S. Miller struct flowi6 *fl6) 1751da177e4SLinus Torvalds { 1763b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 1774cdf507dSEric Dumazet struct dst_entry *dst; 17892d86829SDavid S. Miller bool res = false; 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds /* Informational messages are not limited. */ 1811da177e4SLinus Torvalds if (type & ICMPV6_INFOMSG_MASK) 18292d86829SDavid S. Miller return true; 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds /* Do not limit pmtu discovery, it would break it. */ 1851da177e4SLinus Torvalds if (type == ICMPV6_PKT_TOOBIG) 18692d86829SDavid S. Miller return true; 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds /* 1891da177e4SLinus Torvalds * Look up the output route. 1901da177e4SLinus Torvalds * XXX: perhaps the expire for routing entries cloned by 1911da177e4SLinus Torvalds * this lookup should be more aggressive (not longer than timeout). 1921da177e4SLinus Torvalds */ 1934c9483b2SDavid S. Miller dst = ip6_route_output(net, sk, fl6); 1941da177e4SLinus Torvalds if (dst->error) { 1953bd653c8SDenis V. Lunev IP6_INC_STATS(net, ip6_dst_idev(dst), 196a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_OUTNOROUTES); 1971da177e4SLinus Torvalds } else if (dst->dev && (dst->dev->flags&IFF_LOOPBACK)) { 19892d86829SDavid S. Miller res = true; 1991da177e4SLinus Torvalds } else { 2001da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 2019a43b709SBenjamin Thery int tmo = net->ipv6.sysctl.icmpv6_time; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds /* Give more bandwidth to wider prefixes. */ 2041da177e4SLinus Torvalds if (rt->rt6i_dst.plen < 128) 2051da177e4SLinus Torvalds tmo >>= ((128 - rt->rt6i_dst.plen)>>5); 2061da177e4SLinus Torvalds 2074cdf507dSEric Dumazet if (icmp_global_allow()) { 2084cdf507dSEric Dumazet struct inet_peer *peer; 2094cdf507dSEric Dumazet 2104cdf507dSEric Dumazet peer = inet_getpeer_v6(net->ipv6.peers, 211fd0273d7SMartin KaFai Lau &fl6->daddr, 1); 212fbfe95a4SDavid S. Miller res = inet_peer_xrlim_allow(peer, tmo); 2131d861aa4SDavid S. Miller if (peer) 2141d861aa4SDavid S. Miller inet_putpeer(peer); 2151da177e4SLinus Torvalds } 2164cdf507dSEric Dumazet } 2171da177e4SLinus Torvalds dst_release(dst); 2181da177e4SLinus Torvalds return res; 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds /* 2221da177e4SLinus Torvalds * an inline helper for the "simple" if statement below 2231da177e4SLinus Torvalds * checks if parameter problem report is caused by an 2241da177e4SLinus Torvalds * unrecognized IPv6 option that has the Option Type 2251da177e4SLinus Torvalds * highest-order two bits set to 10 2261da177e4SLinus Torvalds */ 2271da177e4SLinus Torvalds 228a50feda5SEric Dumazet static bool opt_unrec(struct sk_buff *skb, __u32 offset) 2291da177e4SLinus Torvalds { 2301da177e4SLinus Torvalds u8 _optval, *op; 2311da177e4SLinus Torvalds 232bbe735e4SArnaldo Carvalho de Melo offset += skb_network_offset(skb); 2331da177e4SLinus Torvalds op = skb_header_pointer(skb, offset, sizeof(_optval), &_optval); 23463159f29SIan Morris if (!op) 235a50feda5SEric Dumazet return true; 2361da177e4SLinus Torvalds return (*op & 0xC0) == 0x80; 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds 2396d0bfe22SLorenzo Colitti int icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6, 2406d0bfe22SLorenzo Colitti struct icmp6hdr *thdr, int len) 2411da177e4SLinus Torvalds { 2421da177e4SLinus Torvalds struct sk_buff *skb; 2431da177e4SLinus Torvalds struct icmp6hdr *icmp6h; 2441da177e4SLinus Torvalds int err = 0; 2451da177e4SLinus Torvalds 246e5d08d71SIan Morris skb = skb_peek(&sk->sk_write_queue); 24763159f29SIan Morris if (!skb) 2481da177e4SLinus Torvalds goto out; 2491da177e4SLinus Torvalds 250cc70ab26SArnaldo Carvalho de Melo icmp6h = icmp6_hdr(skb); 2511da177e4SLinus Torvalds memcpy(icmp6h, thdr, sizeof(struct icmp6hdr)); 2521da177e4SLinus Torvalds icmp6h->icmp6_cksum = 0; 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds if (skb_queue_len(&sk->sk_write_queue) == 1) { 25507f0757aSJoe Perches skb->csum = csum_partial(icmp6h, 2561da177e4SLinus Torvalds sizeof(struct icmp6hdr), skb->csum); 2574c9483b2SDavid S. Miller icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr, 2584c9483b2SDavid S. Miller &fl6->daddr, 2594c9483b2SDavid S. Miller len, fl6->flowi6_proto, 2601da177e4SLinus Torvalds skb->csum); 2611da177e4SLinus Torvalds } else { 262868c86bcSAl Viro __wsum tmp_csum = 0; 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds skb_queue_walk(&sk->sk_write_queue, skb) { 2651da177e4SLinus Torvalds tmp_csum = csum_add(tmp_csum, skb->csum); 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds 26807f0757aSJoe Perches tmp_csum = csum_partial(icmp6h, 2691da177e4SLinus Torvalds sizeof(struct icmp6hdr), tmp_csum); 2704c9483b2SDavid S. Miller icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr, 2714c9483b2SDavid S. Miller &fl6->daddr, 2724c9483b2SDavid S. Miller len, fl6->flowi6_proto, 273868c86bcSAl Viro tmp_csum); 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds ip6_push_pending_frames(sk); 2761da177e4SLinus Torvalds out: 2771da177e4SLinus Torvalds return err; 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds struct icmpv6_msg { 2811da177e4SLinus Torvalds struct sk_buff *skb; 2821da177e4SLinus Torvalds int offset; 283763ecff1SYasuyuki Kozakai uint8_t type; 2841da177e4SLinus Torvalds }; 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds static int icmpv6_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb) 2871da177e4SLinus Torvalds { 2881da177e4SLinus Torvalds struct icmpv6_msg *msg = (struct icmpv6_msg *) from; 2891da177e4SLinus Torvalds struct sk_buff *org_skb = msg->skb; 2905f92a738SAl Viro __wsum csum = 0; 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(org_skb, msg->offset + offset, 2931da177e4SLinus Torvalds to, len, csum); 2941da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, odd); 295763ecff1SYasuyuki Kozakai if (!(msg->type & ICMPV6_INFOMSG_MASK)) 296763ecff1SYasuyuki Kozakai nf_ct_attach(skb, org_skb); 2971da177e4SLinus Torvalds return 0; 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds 30007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 30179383236SMasahide NAKAMURA static void mip6_addr_swap(struct sk_buff *skb) 30279383236SMasahide NAKAMURA { 3030660e03fSArnaldo Carvalho de Melo struct ipv6hdr *iph = ipv6_hdr(skb); 30479383236SMasahide NAKAMURA struct inet6_skb_parm *opt = IP6CB(skb); 30579383236SMasahide NAKAMURA struct ipv6_destopt_hao *hao; 30679383236SMasahide NAKAMURA struct in6_addr tmp; 30779383236SMasahide NAKAMURA int off; 30879383236SMasahide NAKAMURA 30979383236SMasahide NAKAMURA if (opt->dsthao) { 31079383236SMasahide NAKAMURA off = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO); 31179383236SMasahide NAKAMURA if (likely(off >= 0)) { 312d56f90a7SArnaldo Carvalho de Melo hao = (struct ipv6_destopt_hao *) 313d56f90a7SArnaldo Carvalho de Melo (skb_network_header(skb) + off); 3144e3fd7a0SAlexey Dobriyan tmp = iph->saddr; 3154e3fd7a0SAlexey Dobriyan iph->saddr = hao->addr; 3164e3fd7a0SAlexey Dobriyan hao->addr = tmp; 31779383236SMasahide NAKAMURA } 31879383236SMasahide NAKAMURA } 31979383236SMasahide NAKAMURA } 32079383236SMasahide NAKAMURA #else 32179383236SMasahide NAKAMURA static inline void mip6_addr_swap(struct sk_buff *skb) {} 32279383236SMasahide NAKAMURA #endif 32379383236SMasahide NAKAMURA 324e8243534Sstephen hemminger static struct dst_entry *icmpv6_route_lookup(struct net *net, 325e8243534Sstephen hemminger struct sk_buff *skb, 326e8243534Sstephen hemminger struct sock *sk, 327e8243534Sstephen hemminger struct flowi6 *fl6) 328b42835dbSDavid S. Miller { 329b42835dbSDavid S. Miller struct dst_entry *dst, *dst2; 3304c9483b2SDavid S. Miller struct flowi6 fl2; 331b42835dbSDavid S. Miller int err; 332b42835dbSDavid S. Miller 333343d60aaSRoopa Prabhu err = ip6_dst_lookup(net, sk, &dst, fl6); 334b42835dbSDavid S. Miller if (err) 335b42835dbSDavid S. Miller return ERR_PTR(err); 336b42835dbSDavid S. Miller 337b42835dbSDavid S. Miller /* 338b42835dbSDavid S. Miller * We won't send icmp if the destination is known 339b42835dbSDavid S. Miller * anycast. 340b42835dbSDavid S. Miller */ 3412647a9b0SMartin KaFai Lau if (ipv6_anycast_destination(dst, &fl6->daddr)) { 342ba7a46f1SJoe Perches net_dbg_ratelimited("icmp6_send: acast source\n"); 343b42835dbSDavid S. Miller dst_release(dst); 344b42835dbSDavid S. Miller return ERR_PTR(-EINVAL); 345b42835dbSDavid S. Miller } 346b42835dbSDavid S. Miller 347b42835dbSDavid S. Miller /* No need to clone since we're just using its address. */ 348b42835dbSDavid S. Miller dst2 = dst; 349b42835dbSDavid S. Miller 3504c9483b2SDavid S. Miller dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), sk, 0); 351452edd59SDavid S. Miller if (!IS_ERR(dst)) { 352b42835dbSDavid S. Miller if (dst != dst2) 353b42835dbSDavid S. Miller return dst; 354452edd59SDavid S. Miller } else { 355452edd59SDavid S. Miller if (PTR_ERR(dst) == -EPERM) 356b42835dbSDavid S. Miller dst = NULL; 357452edd59SDavid S. Miller else 358452edd59SDavid S. Miller return dst; 359b42835dbSDavid S. Miller } 360b42835dbSDavid S. Miller 3614c9483b2SDavid S. Miller err = xfrm_decode_session_reverse(skb, flowi6_to_flowi(&fl2), AF_INET6); 362b42835dbSDavid S. Miller if (err) 363b42835dbSDavid S. Miller goto relookup_failed; 364b42835dbSDavid S. Miller 365343d60aaSRoopa Prabhu err = ip6_dst_lookup(net, sk, &dst2, &fl2); 366b42835dbSDavid S. Miller if (err) 367b42835dbSDavid S. Miller goto relookup_failed; 368b42835dbSDavid S. Miller 3694c9483b2SDavid S. Miller dst2 = xfrm_lookup(net, dst2, flowi6_to_flowi(&fl2), sk, XFRM_LOOKUP_ICMP); 370452edd59SDavid S. Miller if (!IS_ERR(dst2)) { 371b42835dbSDavid S. Miller dst_release(dst); 372b42835dbSDavid S. Miller dst = dst2; 373452edd59SDavid S. Miller } else { 374452edd59SDavid S. Miller err = PTR_ERR(dst2); 375452edd59SDavid S. Miller if (err == -EPERM) { 376b42835dbSDavid S. Miller dst_release(dst); 377452edd59SDavid S. Miller return dst2; 378452edd59SDavid S. Miller } else 379b42835dbSDavid S. Miller goto relookup_failed; 380b42835dbSDavid S. Miller } 381b42835dbSDavid S. Miller 382b42835dbSDavid S. Miller relookup_failed: 383b42835dbSDavid S. Miller if (dst) 384b42835dbSDavid S. Miller return dst; 385b42835dbSDavid S. Miller return ERR_PTR(err); 386b42835dbSDavid S. Miller } 387b42835dbSDavid S. Miller 3881da177e4SLinus Torvalds /* 3891da177e4SLinus Torvalds * Send an ICMP message in response to a packet in error 3901da177e4SLinus Torvalds */ 391b1cadc1aSEric Dumazet static void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info, 392b1cadc1aSEric Dumazet const struct in6_addr *force_saddr) 3931da177e4SLinus Torvalds { 394c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 3951da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 3960660e03fSArnaldo Carvalho de Melo struct ipv6hdr *hdr = ipv6_hdr(skb); 39784427d53SYOSHIFUJI Hideaki struct sock *sk; 39884427d53SYOSHIFUJI Hideaki struct ipv6_pinfo *np; 399b71d1d42SEric Dumazet const struct in6_addr *saddr = NULL; 4001da177e4SLinus Torvalds struct dst_entry *dst; 4011da177e4SLinus Torvalds struct icmp6hdr tmp_hdr; 4024c9483b2SDavid S. Miller struct flowi6 fl6; 4031da177e4SLinus Torvalds struct icmpv6_msg msg; 404c14ac945SSoheil Hassas Yeganeh struct sockcm_cookie sockc_unused = {0}; 40526879da5SWei Wang struct ipcm6_cookie ipc6; 4061da177e4SLinus Torvalds int iif = 0; 4071da177e4SLinus Torvalds int addr_type = 0; 4081da177e4SLinus Torvalds int len; 4091da177e4SLinus Torvalds int err = 0; 410e110861fSLorenzo Colitti u32 mark = IP6_REPLY_MARK(net, skb->mark); 4111da177e4SLinus Torvalds 41227a884dcSArnaldo Carvalho de Melo if ((u8 *)hdr < skb->head || 41329a3cad5SSimon Horman (skb_network_header(skb) + sizeof(*hdr)) > skb_tail_pointer(skb)) 4141da177e4SLinus Torvalds return; 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds /* 4171da177e4SLinus Torvalds * Make sure we respect the rules 4181da177e4SLinus Torvalds * i.e. RFC 1885 2.4(e) 4195f5624cfSPravin B Shelar * Rule (e.1) is enforced by not using icmp6_send 4201da177e4SLinus Torvalds * in any code that processes icmp errors. 4211da177e4SLinus Torvalds */ 4221da177e4SLinus Torvalds addr_type = ipv6_addr_type(&hdr->daddr); 4231da177e4SLinus Torvalds 424446fab59SFX Le Bail if (ipv6_chk_addr(net, &hdr->daddr, skb->dev, 0) || 425d94c1f92SFX Le Bail ipv6_chk_acast_addr_src(net, skb->dev, &hdr->daddr)) 4261da177e4SLinus Torvalds saddr = &hdr->daddr; 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds /* 4291da177e4SLinus Torvalds * Dest addr check 4301da177e4SLinus Torvalds */ 4311da177e4SLinus Torvalds 4329a6b4b39Szhuyj if (addr_type & IPV6_ADDR_MULTICAST || skb->pkt_type != PACKET_HOST) { 4331da177e4SLinus Torvalds if (type != ICMPV6_PKT_TOOBIG && 4341da177e4SLinus Torvalds !(type == ICMPV6_PARAMPROB && 4351da177e4SLinus Torvalds code == ICMPV6_UNK_OPTION && 4361da177e4SLinus Torvalds (opt_unrec(skb, info)))) 4371da177e4SLinus Torvalds return; 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds saddr = NULL; 4401da177e4SLinus Torvalds } 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds addr_type = ipv6_addr_type(&hdr->saddr); 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds /* 4451da177e4SLinus Torvalds * Source addr check 4461da177e4SLinus Torvalds */ 4471da177e4SLinus Torvalds 448842df073SHannes Frederic Sowa if (__ipv6_addr_needs_scope_id(addr_type)) 4491da177e4SLinus Torvalds iif = skb->dev->ifindex; 4501d2f7b2dSDavid Ahern else 4511d2f7b2dSDavid Ahern iif = l3mdev_master_ifindex(skb->dev); 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds /* 4548de3351eSYOSHIFUJI Hideaki * Must not send error if the source does not uniquely 4558de3351eSYOSHIFUJI Hideaki * identify a single node (RFC2463 Section 2.4). 4568de3351eSYOSHIFUJI Hideaki * We check unspecified / multicast addresses here, 4578de3351eSYOSHIFUJI Hideaki * and anycast addresses will be checked later. 4581da177e4SLinus Torvalds */ 4591da177e4SLinus Torvalds if ((addr_type == IPV6_ADDR_ANY) || (addr_type & IPV6_ADDR_MULTICAST)) { 4604b3418fbSBjørn Mork net_dbg_ratelimited("icmp6_send: addr_any/mcast source [%pI6c > %pI6c]\n", 4614b3418fbSBjørn Mork &hdr->saddr, &hdr->daddr); 4621da177e4SLinus Torvalds return; 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds /* 4661da177e4SLinus Torvalds * Never answer to a ICMP packet. 4671da177e4SLinus Torvalds */ 4681da177e4SLinus Torvalds if (is_ineligible(skb)) { 4694b3418fbSBjørn Mork net_dbg_ratelimited("icmp6_send: no reply to icmp error [%pI6c > %pI6c]\n", 4704b3418fbSBjørn Mork &hdr->saddr, &hdr->daddr); 4711da177e4SLinus Torvalds return; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 47479383236SMasahide NAKAMURA mip6_addr_swap(skb); 47579383236SMasahide NAKAMURA 4764c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 4774c9483b2SDavid S. Miller fl6.flowi6_proto = IPPROTO_ICMPV6; 4784e3fd7a0SAlexey Dobriyan fl6.daddr = hdr->saddr; 479b1cadc1aSEric Dumazet if (force_saddr) 480b1cadc1aSEric Dumazet saddr = force_saddr; 4811da177e4SLinus Torvalds if (saddr) 4824e3fd7a0SAlexey Dobriyan fl6.saddr = *saddr; 483e110861fSLorenzo Colitti fl6.flowi6_mark = mark; 4844c9483b2SDavid S. Miller fl6.flowi6_oif = iif; 4851958b856SDavid S. Miller fl6.fl6_icmp_type = type; 4861958b856SDavid S. Miller fl6.fl6_icmp_code = code; 4874c9483b2SDavid S. Miller security_skb_classify_flow(skb, flowi6_to_flowi(&fl6)); 4881da177e4SLinus Torvalds 489fdc0bde9SDenis V. Lunev sk = icmpv6_xmit_lock(net); 49063159f29SIan Morris if (!sk) 491405666dbSDenis V. Lunev return; 492e110861fSLorenzo Colitti sk->sk_mark = mark; 493fdc0bde9SDenis V. Lunev np = inet6_sk(sk); 494405666dbSDenis V. Lunev 4954c9483b2SDavid S. Miller if (!icmpv6_xrlim_allow(sk, type, &fl6)) 4961da177e4SLinus Torvalds goto out; 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds tmp_hdr.icmp6_type = type; 4991da177e4SLinus Torvalds tmp_hdr.icmp6_code = code; 5001da177e4SLinus Torvalds tmp_hdr.icmp6_cksum = 0; 5011da177e4SLinus Torvalds tmp_hdr.icmp6_pointer = htonl(info); 5021da177e4SLinus Torvalds 5034c9483b2SDavid S. Miller if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) 5044c9483b2SDavid S. Miller fl6.flowi6_oif = np->mcast_oif; 505c4062dfcSErich E. Hoover else if (!fl6.flowi6_oif) 506c4062dfcSErich E. Hoover fl6.flowi6_oif = np->ucast_oif; 5071da177e4SLinus Torvalds 50838b7097bSHannes Frederic Sowa ipc6.tclass = np->tclass; 50938b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel); 51038b7097bSHannes Frederic Sowa 5114c9483b2SDavid S. Miller dst = icmpv6_route_lookup(net, skb, sk, &fl6); 512b42835dbSDavid S. Miller if (IS_ERR(dst)) 5131da177e4SLinus Torvalds goto out; 5148de3351eSYOSHIFUJI Hideaki 51526879da5SWei Wang ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst); 51626879da5SWei Wang ipc6.dontfrag = np->dontfrag; 51726879da5SWei Wang ipc6.opt = NULL; 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds msg.skb = skb; 520bbe735e4SArnaldo Carvalho de Melo msg.offset = skb_network_offset(skb); 521763ecff1SYasuyuki Kozakai msg.type = type; 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds len = skb->len - msg.offset; 5241da177e4SLinus Torvalds len = min_t(unsigned int, len, IPV6_MIN_MTU - sizeof(struct ipv6hdr) - sizeof(struct icmp6hdr)); 5251da177e4SLinus Torvalds if (len < 0) { 5264b3418fbSBjørn Mork net_dbg_ratelimited("icmp: len problem [%pI6c > %pI6c]\n", 5274b3418fbSBjørn Mork &hdr->saddr, &hdr->daddr); 5281da177e4SLinus Torvalds goto out_dst_release; 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds 531cfdf7647SEric Dumazet rcu_read_lock(); 532cfdf7647SEric Dumazet idev = __in6_dev_get(skb->dev); 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds err = ip6_append_data(sk, icmpv6_getfrag, &msg, 5351da177e4SLinus Torvalds len + sizeof(struct icmp6hdr), 53626879da5SWei Wang sizeof(struct icmp6hdr), 53726879da5SWei Wang &ipc6, &fl6, (struct rt6_info *)dst, 53826879da5SWei Wang MSG_DONTWAIT, &sockc_unused); 5391da177e4SLinus Torvalds if (err) { 54043a43b60SHannes Frederic Sowa ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS); 5411da177e4SLinus Torvalds ip6_flush_pending_frames(sk); 542cfdf7647SEric Dumazet } else { 543cfdf7647SEric Dumazet err = icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr, 544cfdf7647SEric Dumazet len + sizeof(struct icmp6hdr)); 5451da177e4SLinus Torvalds } 546cfdf7647SEric Dumazet rcu_read_unlock(); 5471da177e4SLinus Torvalds out_dst_release: 5481da177e4SLinus Torvalds dst_release(dst); 5491da177e4SLinus Torvalds out: 550405666dbSDenis V. Lunev icmpv6_xmit_unlock(sk); 5511da177e4SLinus Torvalds } 5525f5624cfSPravin B Shelar 5535f5624cfSPravin B Shelar /* Slightly more convenient version of icmp6_send. 5545f5624cfSPravin B Shelar */ 5555f5624cfSPravin B Shelar void icmpv6_param_prob(struct sk_buff *skb, u8 code, int pos) 5565f5624cfSPravin B Shelar { 557b1cadc1aSEric Dumazet icmp6_send(skb, ICMPV6_PARAMPROB, code, pos, NULL); 5585f5624cfSPravin B Shelar kfree_skb(skb); 5595f5624cfSPravin B Shelar } 5607159039aSYOSHIFUJI Hideaki 5615fbba8acSEric Dumazet /* Generate icmpv6 with type/code ICMPV6_DEST_UNREACH/ICMPV6_ADDR_UNREACH 5625fbba8acSEric Dumazet * if sufficient data bytes are available 5635fbba8acSEric Dumazet * @nhs is the size of the tunnel header(s) : 5645fbba8acSEric Dumazet * Either an IPv4 header for SIT encap 5655fbba8acSEric Dumazet * an IPv4 header + GRE header for GRE encap 5665fbba8acSEric Dumazet */ 56720e1954fSEric Dumazet int ip6_err_gen_icmpv6_unreach(struct sk_buff *skb, int nhs, int type, 56820e1954fSEric Dumazet unsigned int data_len) 5695fbba8acSEric Dumazet { 5702d7a3b27SEric Dumazet struct in6_addr temp_saddr; 5715fbba8acSEric Dumazet struct rt6_info *rt; 5725fbba8acSEric Dumazet struct sk_buff *skb2; 57320e1954fSEric Dumazet u32 info = 0; 5745fbba8acSEric Dumazet 5755fbba8acSEric Dumazet if (!pskb_may_pull(skb, nhs + sizeof(struct ipv6hdr) + 8)) 5765fbba8acSEric Dumazet return 1; 5775fbba8acSEric Dumazet 57820e1954fSEric Dumazet /* RFC 4884 (partial) support for ICMP extensions */ 57920e1954fSEric Dumazet if (data_len < 128 || (data_len & 7) || skb->len < data_len) 58020e1954fSEric Dumazet data_len = 0; 58120e1954fSEric Dumazet 58220e1954fSEric Dumazet skb2 = data_len ? skb_copy(skb, GFP_ATOMIC) : skb_clone(skb, GFP_ATOMIC); 5835fbba8acSEric Dumazet 5845fbba8acSEric Dumazet if (!skb2) 5855fbba8acSEric Dumazet return 1; 5865fbba8acSEric Dumazet 5875fbba8acSEric Dumazet skb_dst_drop(skb2); 5885fbba8acSEric Dumazet skb_pull(skb2, nhs); 5895fbba8acSEric Dumazet skb_reset_network_header(skb2); 5905fbba8acSEric Dumazet 5915fbba8acSEric Dumazet rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr, NULL, 0, 0); 5925fbba8acSEric Dumazet 5935fbba8acSEric Dumazet if (rt && rt->dst.dev) 5945fbba8acSEric Dumazet skb2->dev = rt->dst.dev; 5955fbba8acSEric Dumazet 5962d7a3b27SEric Dumazet ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, &temp_saddr); 59720e1954fSEric Dumazet 59820e1954fSEric Dumazet if (data_len) { 59920e1954fSEric Dumazet /* RFC 4884 (partial) support : 60020e1954fSEric Dumazet * insert 0 padding at the end, before the extensions 60120e1954fSEric Dumazet */ 60220e1954fSEric Dumazet __skb_push(skb2, nhs); 60320e1954fSEric Dumazet skb_reset_network_header(skb2); 60420e1954fSEric Dumazet memmove(skb2->data, skb2->data + nhs, data_len - nhs); 60520e1954fSEric Dumazet memset(skb2->data + data_len - nhs, 0, nhs); 60620e1954fSEric Dumazet /* RFC 4884 4.5 : Length is measured in 64-bit words, 60720e1954fSEric Dumazet * and stored in reserved[0] 60820e1954fSEric Dumazet */ 60920e1954fSEric Dumazet info = (data_len/8) << 24; 61020e1954fSEric Dumazet } 6112d7a3b27SEric Dumazet if (type == ICMP_TIME_EXCEEDED) 6122d7a3b27SEric Dumazet icmp6_send(skb2, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 61320e1954fSEric Dumazet info, &temp_saddr); 6142d7a3b27SEric Dumazet else 6152d7a3b27SEric Dumazet icmp6_send(skb2, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 61620e1954fSEric Dumazet info, &temp_saddr); 6175fbba8acSEric Dumazet if (rt) 6185fbba8acSEric Dumazet ip6_rt_put(rt); 6195fbba8acSEric Dumazet 6205fbba8acSEric Dumazet kfree_skb(skb2); 6215fbba8acSEric Dumazet 6225fbba8acSEric Dumazet return 0; 6235fbba8acSEric Dumazet } 6245fbba8acSEric Dumazet EXPORT_SYMBOL(ip6_err_gen_icmpv6_unreach); 6255fbba8acSEric Dumazet 6261da177e4SLinus Torvalds static void icmpv6_echo_reply(struct sk_buff *skb) 6271da177e4SLinus Torvalds { 628c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 62984427d53SYOSHIFUJI Hideaki struct sock *sk; 6301da177e4SLinus Torvalds struct inet6_dev *idev; 63184427d53SYOSHIFUJI Hideaki struct ipv6_pinfo *np; 632b71d1d42SEric Dumazet const struct in6_addr *saddr = NULL; 633cc70ab26SArnaldo Carvalho de Melo struct icmp6hdr *icmph = icmp6_hdr(skb); 6341da177e4SLinus Torvalds struct icmp6hdr tmp_hdr; 6354c9483b2SDavid S. Miller struct flowi6 fl6; 6361da177e4SLinus Torvalds struct icmpv6_msg msg; 6371da177e4SLinus Torvalds struct dst_entry *dst; 63826879da5SWei Wang struct ipcm6_cookie ipc6; 6391da177e4SLinus Torvalds int err = 0; 640e110861fSLorenzo Colitti u32 mark = IP6_REPLY_MARK(net, skb->mark); 641c14ac945SSoheil Hassas Yeganeh struct sockcm_cookie sockc_unused = {0}; 6421da177e4SLinus Torvalds 6430660e03fSArnaldo Carvalho de Melo saddr = &ipv6_hdr(skb)->daddr; 6441da177e4SLinus Torvalds 645509aba3bSFX Le Bail if (!ipv6_unicast_destination(skb) && 646ec35b61eSFX Le Bail !(net->ipv6.sysctl.anycast_src_echo_reply && 6472647a9b0SMartin KaFai Lau ipv6_anycast_destination(skb_dst(skb), saddr))) 6481da177e4SLinus Torvalds saddr = NULL; 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds memcpy(&tmp_hdr, icmph, sizeof(tmp_hdr)); 6511da177e4SLinus Torvalds tmp_hdr.icmp6_type = ICMPV6_ECHO_REPLY; 6521da177e4SLinus Torvalds 6534c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 6544c9483b2SDavid S. Miller fl6.flowi6_proto = IPPROTO_ICMPV6; 6554e3fd7a0SAlexey Dobriyan fl6.daddr = ipv6_hdr(skb)->saddr; 6561da177e4SLinus Torvalds if (saddr) 6574e3fd7a0SAlexey Dobriyan fl6.saddr = *saddr; 6589ff74384SDavid Ahern fl6.flowi6_oif = skb->dev->ifindex; 6591958b856SDavid S. Miller fl6.fl6_icmp_type = ICMPV6_ECHO_REPLY; 660e110861fSLorenzo Colitti fl6.flowi6_mark = mark; 6614c9483b2SDavid S. Miller security_skb_classify_flow(skb, flowi6_to_flowi(&fl6)); 6621da177e4SLinus Torvalds 663fdc0bde9SDenis V. Lunev sk = icmpv6_xmit_lock(net); 66463159f29SIan Morris if (!sk) 665405666dbSDenis V. Lunev return; 666e110861fSLorenzo Colitti sk->sk_mark = mark; 667fdc0bde9SDenis V. Lunev np = inet6_sk(sk); 668405666dbSDenis V. Lunev 6694c9483b2SDavid S. Miller if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) 6704c9483b2SDavid S. Miller fl6.flowi6_oif = np->mcast_oif; 671c4062dfcSErich E. Hoover else if (!fl6.flowi6_oif) 672c4062dfcSErich E. Hoover fl6.flowi6_oif = np->ucast_oif; 6731da177e4SLinus Torvalds 674343d60aaSRoopa Prabhu err = ip6_dst_lookup(net, sk, &dst, &fl6); 6751da177e4SLinus Torvalds if (err) 6761da177e4SLinus Torvalds goto out; 6774c9483b2SDavid S. Miller dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), sk, 0); 678452edd59SDavid S. Miller if (IS_ERR(dst)) 679e104411bSPatrick McHardy goto out; 6801da177e4SLinus Torvalds 681cfdf7647SEric Dumazet idev = __in6_dev_get(skb->dev); 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds msg.skb = skb; 6841da177e4SLinus Torvalds msg.offset = 0; 685763ecff1SYasuyuki Kozakai msg.type = ICMPV6_ECHO_REPLY; 6861da177e4SLinus Torvalds 68726879da5SWei Wang ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst); 68826879da5SWei Wang ipc6.tclass = ipv6_get_dsfield(ipv6_hdr(skb)); 68926879da5SWei Wang ipc6.dontfrag = np->dontfrag; 69026879da5SWei Wang ipc6.opt = NULL; 69126879da5SWei Wang 6921da177e4SLinus Torvalds err = ip6_append_data(sk, icmpv6_getfrag, &msg, skb->len + sizeof(struct icmp6hdr), 69326879da5SWei Wang sizeof(struct icmp6hdr), &ipc6, &fl6, 69413b52cd4SBrian Haley (struct rt6_info *)dst, MSG_DONTWAIT, 69526879da5SWei Wang &sockc_unused); 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds if (err) { 698a16292a0SEric Dumazet __ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS); 6991da177e4SLinus Torvalds ip6_flush_pending_frames(sk); 700cfdf7647SEric Dumazet } else { 701cfdf7647SEric Dumazet err = icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr, 702cfdf7647SEric Dumazet skb->len + sizeof(struct icmp6hdr)); 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds dst_release(dst); 7051da177e4SLinus Torvalds out: 706405666dbSDenis V. Lunev icmpv6_xmit_unlock(sk); 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 709b94f1c09SDavid S. Miller void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info) 7101da177e4SLinus Torvalds { 71141135cc8SAlexey Dobriyan const struct inet6_protocol *ipprot; 7121da177e4SLinus Torvalds int inner_offset; 71375f2811cSJesse Gross __be16 frag_off; 714f9242b6bSDavid S. Miller u8 nexthdr; 7157304fe46SDuan Jiong struct net *net = dev_net(skb->dev); 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) 7187304fe46SDuan Jiong goto out; 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds nexthdr = ((struct ipv6hdr *)skb->data)->nexthdr; 7211da177e4SLinus Torvalds if (ipv6_ext_hdr(nexthdr)) { 7221da177e4SLinus Torvalds /* now skip over extension headers */ 72375f2811cSJesse Gross inner_offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), 72475f2811cSJesse Gross &nexthdr, &frag_off); 7251da177e4SLinus Torvalds if (inner_offset < 0) 7267304fe46SDuan Jiong goto out; 7271da177e4SLinus Torvalds } else { 7281da177e4SLinus Torvalds inner_offset = sizeof(struct ipv6hdr); 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds /* Checkin header including 8 bytes of inner protocol header. */ 7321da177e4SLinus Torvalds if (!pskb_may_pull(skb, inner_offset+8)) 7337304fe46SDuan Jiong goto out; 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds /* BUGGG_FUTURE: we should try to parse exthdrs in this packet. 7361da177e4SLinus Torvalds Without this we will not able f.e. to make source routed 7371da177e4SLinus Torvalds pmtu discovery. 7381da177e4SLinus Torvalds Corresponding argument (opt) to notifiers is already added. 7391da177e4SLinus Torvalds --ANK (980726) 7401da177e4SLinus Torvalds */ 7411da177e4SLinus Torvalds 742f9242b6bSDavid S. Miller ipprot = rcu_dereference(inet6_protos[nexthdr]); 7431da177e4SLinus Torvalds if (ipprot && ipprot->err_handler) 7441da177e4SLinus Torvalds ipprot->err_handler(skb, NULL, type, code, inner_offset, info); 7451da177e4SLinus Torvalds 74669d6da0bSPavel Emelyanov raw6_icmp_error(skb, nexthdr, type, code, inner_offset, info); 7477304fe46SDuan Jiong return; 7487304fe46SDuan Jiong 7497304fe46SDuan Jiong out: 750a16292a0SEric Dumazet __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), ICMP6_MIB_INERRORS); 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds /* 7541da177e4SLinus Torvalds * Handle icmp messages 7551da177e4SLinus Torvalds */ 7561da177e4SLinus Torvalds 757e5bbef20SHerbert Xu static int icmpv6_rcv(struct sk_buff *skb) 7581da177e4SLinus Torvalds { 7591da177e4SLinus Torvalds struct net_device *dev = skb->dev; 7601da177e4SLinus Torvalds struct inet6_dev *idev = __in6_dev_get(dev); 761b71d1d42SEric Dumazet const struct in6_addr *saddr, *daddr; 7621da177e4SLinus Torvalds struct icmp6hdr *hdr; 763d5fdd6baSBrian Haley u8 type; 764e3e32170SRick Jones bool success = false; 7651da177e4SLinus Torvalds 766aebcf82cSHerbert Xu if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { 767def8b4faSAlexey Dobriyan struct sec_path *sp = skb_sec_path(skb); 7688b7817f3SHerbert Xu int nh; 7698b7817f3SHerbert Xu 770def8b4faSAlexey Dobriyan if (!(sp && sp->xvec[sp->len - 1]->props.flags & 771aebcf82cSHerbert Xu XFRM_STATE_ICMP)) 772aebcf82cSHerbert Xu goto drop_no_count; 773aebcf82cSHerbert Xu 77481aded24SDavid S. Miller if (!pskb_may_pull(skb, sizeof(*hdr) + sizeof(struct ipv6hdr))) 7758b7817f3SHerbert Xu goto drop_no_count; 7768b7817f3SHerbert Xu 7778b7817f3SHerbert Xu nh = skb_network_offset(skb); 7788b7817f3SHerbert Xu skb_set_network_header(skb, sizeof(*hdr)); 7798b7817f3SHerbert Xu 7808b7817f3SHerbert Xu if (!xfrm6_policy_check_reverse(NULL, XFRM_POLICY_IN, skb)) 7818b7817f3SHerbert Xu goto drop_no_count; 7828b7817f3SHerbert Xu 7838b7817f3SHerbert Xu skb_set_network_header(skb, nh); 7848b7817f3SHerbert Xu } 7858b7817f3SHerbert Xu 786a16292a0SEric Dumazet __ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_INMSGS); 7871da177e4SLinus Torvalds 7880660e03fSArnaldo Carvalho de Melo saddr = &ipv6_hdr(skb)->saddr; 7890660e03fSArnaldo Carvalho de Melo daddr = &ipv6_hdr(skb)->daddr; 7901da177e4SLinus Torvalds 79139471ac8STom Herbert if (skb_checksum_validate(skb, IPPROTO_ICMPV6, ip6_compute_pseudo)) { 792ba7a46f1SJoe Perches net_dbg_ratelimited("ICMPv6 checksum failed [%pI6c > %pI6c]\n", 7930c6ce78aSHarvey Harrison saddr, daddr); 7946a5dc9e5SEric Dumazet goto csum_error; 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds 7978cf22943SHerbert Xu if (!pskb_pull(skb, sizeof(*hdr))) 7988cf22943SHerbert Xu goto discard_it; 7991da177e4SLinus Torvalds 800cc70ab26SArnaldo Carvalho de Melo hdr = icmp6_hdr(skb); 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds type = hdr->icmp6_type; 8031da177e4SLinus Torvalds 804f3832ed2SEric Dumazet ICMP6MSGIN_INC_STATS(dev_net(dev), idev, type); 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds switch (type) { 8071da177e4SLinus Torvalds case ICMPV6_ECHO_REQUEST: 8081da177e4SLinus Torvalds icmpv6_echo_reply(skb); 8091da177e4SLinus Torvalds break; 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds case ICMPV6_ECHO_REPLY: 812e3e32170SRick Jones success = ping_rcv(skb); 8131da177e4SLinus Torvalds break; 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds case ICMPV6_PKT_TOOBIG: 8161da177e4SLinus Torvalds /* BUGGG_FUTURE: if packet contains rthdr, we cannot update 8171da177e4SLinus Torvalds standard destination cache. Seems, only "advanced" 8181da177e4SLinus Torvalds destination cache will allow to solve this problem 8191da177e4SLinus Torvalds --ANK (980726) 8201da177e4SLinus Torvalds */ 8211da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) 8221da177e4SLinus Torvalds goto discard_it; 823cc70ab26SArnaldo Carvalho de Melo hdr = icmp6_hdr(skb); 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds /* 8261da177e4SLinus Torvalds * Drop through to notify 8271da177e4SLinus Torvalds */ 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds case ICMPV6_DEST_UNREACH: 8301da177e4SLinus Torvalds case ICMPV6_TIME_EXCEED: 8311da177e4SLinus Torvalds case ICMPV6_PARAMPROB: 8321da177e4SLinus Torvalds icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu); 8331da177e4SLinus Torvalds break; 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds case NDISC_ROUTER_SOLICITATION: 8361da177e4SLinus Torvalds case NDISC_ROUTER_ADVERTISEMENT: 8371da177e4SLinus Torvalds case NDISC_NEIGHBOUR_SOLICITATION: 8381da177e4SLinus Torvalds case NDISC_NEIGHBOUR_ADVERTISEMENT: 8391da177e4SLinus Torvalds case NDISC_REDIRECT: 8401da177e4SLinus Torvalds ndisc_rcv(skb); 8411da177e4SLinus Torvalds break; 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds case ICMPV6_MGM_QUERY: 8441da177e4SLinus Torvalds igmp6_event_query(skb); 8451da177e4SLinus Torvalds break; 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds case ICMPV6_MGM_REPORT: 8481da177e4SLinus Torvalds igmp6_event_report(skb); 8491da177e4SLinus Torvalds break; 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds case ICMPV6_MGM_REDUCTION: 8521da177e4SLinus Torvalds case ICMPV6_NI_QUERY: 8531da177e4SLinus Torvalds case ICMPV6_NI_REPLY: 8541da177e4SLinus Torvalds case ICMPV6_MLD2_REPORT: 8551da177e4SLinus Torvalds case ICMPV6_DHAAD_REQUEST: 8561da177e4SLinus Torvalds case ICMPV6_DHAAD_REPLY: 8571da177e4SLinus Torvalds case ICMPV6_MOBILE_PREFIX_SOL: 8581da177e4SLinus Torvalds case ICMPV6_MOBILE_PREFIX_ADV: 8591da177e4SLinus Torvalds break; 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds default: 8621da177e4SLinus Torvalds /* informational */ 8631da177e4SLinus Torvalds if (type & ICMPV6_INFOMSG_MASK) 8641da177e4SLinus Torvalds break; 8651da177e4SLinus Torvalds 8664b3418fbSBjørn Mork net_dbg_ratelimited("icmpv6: msg of unknown type [%pI6c > %pI6c]\n", 8674b3418fbSBjørn Mork saddr, daddr); 868ea85a0a2SDavid S. Miller 8691da177e4SLinus Torvalds /* 8701da177e4SLinus Torvalds * error of unknown type. 8711da177e4SLinus Torvalds * must pass to upper level 8721da177e4SLinus Torvalds */ 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu); 8753ff50b79SStephen Hemminger } 8763ff50b79SStephen Hemminger 877e3e32170SRick Jones /* until the v6 path can be better sorted assume failure and 878e3e32170SRick Jones * preserve the status quo behaviour for the rest of the paths to here 879e3e32170SRick Jones */ 880e3e32170SRick Jones if (success) 881e3e32170SRick Jones consume_skb(skb); 882e3e32170SRick Jones else 8831da177e4SLinus Torvalds kfree_skb(skb); 884e3e32170SRick Jones 8851da177e4SLinus Torvalds return 0; 8861da177e4SLinus Torvalds 8876a5dc9e5SEric Dumazet csum_error: 888a16292a0SEric Dumazet __ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_CSUMERRORS); 8891da177e4SLinus Torvalds discard_it: 890a16292a0SEric Dumazet __ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_INERRORS); 8918b7817f3SHerbert Xu drop_no_count: 8921da177e4SLinus Torvalds kfree_skb(skb); 8931da177e4SLinus Torvalds return 0; 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds 8964c9483b2SDavid S. Miller void icmpv6_flow_init(struct sock *sk, struct flowi6 *fl6, 89795e41e93SYOSHIFUJI Hideaki u8 type, 89895e41e93SYOSHIFUJI Hideaki const struct in6_addr *saddr, 89995e41e93SYOSHIFUJI Hideaki const struct in6_addr *daddr, 90095e41e93SYOSHIFUJI Hideaki int oif) 90195e41e93SYOSHIFUJI Hideaki { 9024c9483b2SDavid S. Miller memset(fl6, 0, sizeof(*fl6)); 9034e3fd7a0SAlexey Dobriyan fl6->saddr = *saddr; 9044e3fd7a0SAlexey Dobriyan fl6->daddr = *daddr; 9054c9483b2SDavid S. Miller fl6->flowi6_proto = IPPROTO_ICMPV6; 9061958b856SDavid S. Miller fl6->fl6_icmp_type = type; 9071958b856SDavid S. Miller fl6->fl6_icmp_code = 0; 9084c9483b2SDavid S. Miller fl6->flowi6_oif = oif; 9094c9483b2SDavid S. Miller security_sk_classify_flow(sk, flowi6_to_flowi(fl6)); 91095e41e93SYOSHIFUJI Hideaki } 91195e41e93SYOSHIFUJI Hideaki 91298c6d1b2SDenis V. Lunev static int __net_init icmpv6_sk_init(struct net *net) 9131da177e4SLinus Torvalds { 9141da177e4SLinus Torvalds struct sock *sk; 9151da177e4SLinus Torvalds int err, i, j; 9161da177e4SLinus Torvalds 91798c6d1b2SDenis V. Lunev net->ipv6.icmp_sk = 91898c6d1b2SDenis V. Lunev kzalloc(nr_cpu_ids * sizeof(struct sock *), GFP_KERNEL); 91963159f29SIan Morris if (!net->ipv6.icmp_sk) 92079c91159SDenis V. Lunev return -ENOMEM; 92179c91159SDenis V. Lunev 9226f912042SKAMEZAWA Hiroyuki for_each_possible_cpu(i) { 9231ed8516fSDenis V. Lunev err = inet_ctl_sock_create(&sk, PF_INET6, 9241ed8516fSDenis V. Lunev SOCK_RAW, IPPROTO_ICMPV6, net); 9251da177e4SLinus Torvalds if (err < 0) { 926f3213831SJoe Perches pr_err("Failed to initialize the ICMP6 control socket (err %d)\n", 9271da177e4SLinus Torvalds err); 9281da177e4SLinus Torvalds goto fail; 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds 9311ed8516fSDenis V. Lunev net->ipv6.icmp_sk[i] = sk; 9325c8cafd6SDenis V. Lunev 9331da177e4SLinus Torvalds /* Enough space for 2 64K ICMP packets, including 9341da177e4SLinus Torvalds * sk_buff struct overhead. 9351da177e4SLinus Torvalds */ 93687fb4b7bSEric Dumazet sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024); 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds return 0; 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds fail: 9415c8cafd6SDenis V. Lunev for (j = 0; j < i; j++) 9421ed8516fSDenis V. Lunev inet_ctl_sock_destroy(net->ipv6.icmp_sk[j]); 94398c6d1b2SDenis V. Lunev kfree(net->ipv6.icmp_sk); 9441da177e4SLinus Torvalds return err; 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds 94798c6d1b2SDenis V. Lunev static void __net_exit icmpv6_sk_exit(struct net *net) 9481da177e4SLinus Torvalds { 9491da177e4SLinus Torvalds int i; 9501da177e4SLinus Torvalds 9516f912042SKAMEZAWA Hiroyuki for_each_possible_cpu(i) { 9521ed8516fSDenis V. Lunev inet_ctl_sock_destroy(net->ipv6.icmp_sk[i]); 9531da177e4SLinus Torvalds } 95498c6d1b2SDenis V. Lunev kfree(net->ipv6.icmp_sk); 95598c6d1b2SDenis V. Lunev } 95698c6d1b2SDenis V. Lunev 9578ed7edceSAlexey Dobriyan static struct pernet_operations icmpv6_sk_ops = { 95898c6d1b2SDenis V. Lunev .init = icmpv6_sk_init, 95998c6d1b2SDenis V. Lunev .exit = icmpv6_sk_exit, 96098c6d1b2SDenis V. Lunev }; 96198c6d1b2SDenis V. Lunev 96298c6d1b2SDenis V. Lunev int __init icmpv6_init(void) 96398c6d1b2SDenis V. Lunev { 96498c6d1b2SDenis V. Lunev int err; 96598c6d1b2SDenis V. Lunev 96698c6d1b2SDenis V. Lunev err = register_pernet_subsys(&icmpv6_sk_ops); 96798c6d1b2SDenis V. Lunev if (err < 0) 96898c6d1b2SDenis V. Lunev return err; 96998c6d1b2SDenis V. Lunev 97098c6d1b2SDenis V. Lunev err = -EAGAIN; 97198c6d1b2SDenis V. Lunev if (inet6_add_protocol(&icmpv6_protocol, IPPROTO_ICMPV6) < 0) 97298c6d1b2SDenis V. Lunev goto fail; 9735f5624cfSPravin B Shelar 9745f5624cfSPravin B Shelar err = inet6_register_icmp_sender(icmp6_send); 9755f5624cfSPravin B Shelar if (err) 9765f5624cfSPravin B Shelar goto sender_reg_err; 97798c6d1b2SDenis V. Lunev return 0; 97898c6d1b2SDenis V. Lunev 9795f5624cfSPravin B Shelar sender_reg_err: 9805f5624cfSPravin B Shelar inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6); 98198c6d1b2SDenis V. Lunev fail: 982f3213831SJoe Perches pr_err("Failed to register ICMP6 protocol\n"); 98398c6d1b2SDenis V. Lunev unregister_pernet_subsys(&icmpv6_sk_ops); 98498c6d1b2SDenis V. Lunev return err; 98598c6d1b2SDenis V. Lunev } 98698c6d1b2SDenis V. Lunev 9878ed7edceSAlexey Dobriyan void icmpv6_cleanup(void) 98898c6d1b2SDenis V. Lunev { 9895f5624cfSPravin B Shelar inet6_unregister_icmp_sender(icmp6_send); 99098c6d1b2SDenis V. Lunev unregister_pernet_subsys(&icmpv6_sk_ops); 9911da177e4SLinus Torvalds inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6); 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds 99498c6d1b2SDenis V. Lunev 9959b5b5cffSArjan van de Ven static const struct icmp6_err { 9961da177e4SLinus Torvalds int err; 9971da177e4SLinus Torvalds int fatal; 9981da177e4SLinus Torvalds } tab_unreach[] = { 9991da177e4SLinus Torvalds { /* NOROUTE */ 10001da177e4SLinus Torvalds .err = ENETUNREACH, 10011da177e4SLinus Torvalds .fatal = 0, 10021da177e4SLinus Torvalds }, 10031da177e4SLinus Torvalds { /* ADM_PROHIBITED */ 10041da177e4SLinus Torvalds .err = EACCES, 10051da177e4SLinus Torvalds .fatal = 1, 10061da177e4SLinus Torvalds }, 10071da177e4SLinus Torvalds { /* Was NOT_NEIGHBOUR, now reserved */ 10081da177e4SLinus Torvalds .err = EHOSTUNREACH, 10091da177e4SLinus Torvalds .fatal = 0, 10101da177e4SLinus Torvalds }, 10111da177e4SLinus Torvalds { /* ADDR_UNREACH */ 10121da177e4SLinus Torvalds .err = EHOSTUNREACH, 10131da177e4SLinus Torvalds .fatal = 0, 10141da177e4SLinus Torvalds }, 10151da177e4SLinus Torvalds { /* PORT_UNREACH */ 10161da177e4SLinus Torvalds .err = ECONNREFUSED, 10171da177e4SLinus Torvalds .fatal = 1, 10181da177e4SLinus Torvalds }, 101961e76b17SJiri Bohac { /* POLICY_FAIL */ 102061e76b17SJiri Bohac .err = EACCES, 102161e76b17SJiri Bohac .fatal = 1, 102261e76b17SJiri Bohac }, 102361e76b17SJiri Bohac { /* REJECT_ROUTE */ 102461e76b17SJiri Bohac .err = EACCES, 102561e76b17SJiri Bohac .fatal = 1, 102661e76b17SJiri Bohac }, 10271da177e4SLinus Torvalds }; 10281da177e4SLinus Torvalds 1029d5fdd6baSBrian Haley int icmpv6_err_convert(u8 type, u8 code, int *err) 10301da177e4SLinus Torvalds { 10311da177e4SLinus Torvalds int fatal = 0; 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds *err = EPROTO; 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds switch (type) { 10361da177e4SLinus Torvalds case ICMPV6_DEST_UNREACH: 10371da177e4SLinus Torvalds fatal = 1; 103861e76b17SJiri Bohac if (code < ARRAY_SIZE(tab_unreach)) { 10391da177e4SLinus Torvalds *err = tab_unreach[code].err; 10401da177e4SLinus Torvalds fatal = tab_unreach[code].fatal; 10411da177e4SLinus Torvalds } 10421da177e4SLinus Torvalds break; 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds case ICMPV6_PKT_TOOBIG: 10451da177e4SLinus Torvalds *err = EMSGSIZE; 10461da177e4SLinus Torvalds break; 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds case ICMPV6_PARAMPROB: 10491da177e4SLinus Torvalds *err = EPROTO; 10501da177e4SLinus Torvalds fatal = 1; 10511da177e4SLinus Torvalds break; 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds case ICMPV6_TIME_EXCEED: 10541da177e4SLinus Torvalds *err = EHOSTUNREACH; 10551da177e4SLinus Torvalds break; 10563ff50b79SStephen Hemminger } 10571da177e4SLinus Torvalds 10581da177e4SLinus Torvalds return fatal; 10591da177e4SLinus Torvalds } 10607159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(icmpv6_err_convert); 10617159039aSYOSHIFUJI Hideaki 10621da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 1063e8243534Sstephen hemminger static struct ctl_table ipv6_icmp_table_template[] = { 10641da177e4SLinus Torvalds { 10651da177e4SLinus Torvalds .procname = "ratelimit", 106641a76906SDaniel Lezcano .data = &init_net.ipv6.sysctl.icmpv6_time, 10671da177e4SLinus Torvalds .maxlen = sizeof(int), 10681da177e4SLinus Torvalds .mode = 0644, 10696d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 10701da177e4SLinus Torvalds }, 1071f8572d8fSEric W. Biederman { }, 10721da177e4SLinus Torvalds }; 1073760f2d01SDaniel Lezcano 10742c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_icmp_sysctl_init(struct net *net) 1075760f2d01SDaniel Lezcano { 1076760f2d01SDaniel Lezcano struct ctl_table *table; 1077760f2d01SDaniel Lezcano 1078760f2d01SDaniel Lezcano table = kmemdup(ipv6_icmp_table_template, 1079760f2d01SDaniel Lezcano sizeof(ipv6_icmp_table_template), 1080760f2d01SDaniel Lezcano GFP_KERNEL); 10815ee09105SYOSHIFUJI Hideaki 1082c027aab4SEric W. Biederman if (table) 10835ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.icmpv6_time; 10845ee09105SYOSHIFUJI Hideaki 1085760f2d01SDaniel Lezcano return table; 1086760f2d01SDaniel Lezcano } 10871da177e4SLinus Torvalds #endif 1088