12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Internet Control Message Protocol (ICMPv6) 41da177e4SLinus Torvalds * Linux INET6 implementation 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Authors: 71da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Based on net/ipv4/icmp.c 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * RFC 1885 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds /* 151da177e4SLinus Torvalds * Changes: 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * Andi Kleen : exception handling 181da177e4SLinus Torvalds * Andi Kleen add rate limits. never reply to a icmp. 191da177e4SLinus Torvalds * add more length checks and other fixes. 201da177e4SLinus Torvalds * yoshfuji : ensure to sent parameter problem for 211da177e4SLinus Torvalds * fragments. 221da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI: added sysctl for icmp rate limit. 231da177e4SLinus Torvalds * Randy Dunlap and 241da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI: Per-interface statistics support 251da177e4SLinus Torvalds * Kazunori MIYAZAWA @USAGI: change output process to use ip6_append_data 261da177e4SLinus Torvalds */ 271da177e4SLinus Torvalds 28f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 29f3213831SJoe Perches 301da177e4SLinus Torvalds #include <linux/module.h> 311da177e4SLinus Torvalds #include <linux/errno.h> 321da177e4SLinus Torvalds #include <linux/types.h> 331da177e4SLinus Torvalds #include <linux/socket.h> 341da177e4SLinus Torvalds #include <linux/in.h> 351da177e4SLinus Torvalds #include <linux/kernel.h> 361da177e4SLinus Torvalds #include <linux/sockios.h> 371da177e4SLinus Torvalds #include <linux/net.h> 381da177e4SLinus Torvalds #include <linux/skbuff.h> 391da177e4SLinus Torvalds #include <linux/init.h> 40763ecff1SYasuyuki Kozakai #include <linux/netfilter.h> 415a0e3ad6STejun Heo #include <linux/slab.h> 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 441da177e4SLinus Torvalds #include <linux/sysctl.h> 451da177e4SLinus Torvalds #endif 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds #include <linux/inet.h> 481da177e4SLinus Torvalds #include <linux/netdevice.h> 491da177e4SLinus Torvalds #include <linux/icmpv6.h> 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds #include <net/ip.h> 521da177e4SLinus Torvalds #include <net/sock.h> 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds #include <net/ipv6.h> 551da177e4SLinus Torvalds #include <net/ip6_checksum.h> 566d0bfe22SLorenzo Colitti #include <net/ping.h> 571da177e4SLinus Torvalds #include <net/protocol.h> 581da177e4SLinus Torvalds #include <net/raw.h> 591da177e4SLinus Torvalds #include <net/rawv6.h> 601da177e4SLinus Torvalds #include <net/transp_v6.h> 611da177e4SLinus Torvalds #include <net/ip6_route.h> 621da177e4SLinus Torvalds #include <net/addrconf.h> 631da177e4SLinus Torvalds #include <net/icmp.h> 648b7817f3SHerbert Xu #include <net/xfrm.h> 651ed8516fSDenis V. Lunev #include <net/inet_common.h> 66825edac4SHannes Frederic Sowa #include <net/dsfield.h> 67ca254490SDavid Ahern #include <net/l3mdev.h> 681da177e4SLinus Torvalds 697c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds /* 721da177e4SLinus Torvalds * The ICMP socket(s). This is the most convenient way to flow control 731da177e4SLinus Torvalds * our ICMP output as well as maintain a clean interface throughout 741da177e4SLinus Torvalds * all layers. All Socketless IP sends will soon be gone. 751da177e4SLinus Torvalds * 761da177e4SLinus Torvalds * On SMP we have one ICMP socket per-cpu. 771da177e4SLinus Torvalds */ 782789c14dSEric Dumazet static struct sock *icmpv6_sk(struct net *net) 7998c6d1b2SDenis V. Lunev { 802789c14dSEric Dumazet return this_cpu_read(*net->ipv6.icmp_sk); 8198c6d1b2SDenis V. Lunev } 821da177e4SLinus Torvalds 8332bbd879SStefano Brivio static int icmpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 846f809da2SSteffen Klassert u8 type, u8 code, int offset, __be32 info) 856f809da2SSteffen Klassert { 866d0bfe22SLorenzo Colitti /* icmpv6_notify checks 8 bytes can be pulled, icmp6hdr is 8 bytes */ 876d0bfe22SLorenzo Colitti struct icmp6hdr *icmp6 = (struct icmp6hdr *) (skb->data + offset); 886f809da2SSteffen Klassert struct net *net = dev_net(skb->dev); 896f809da2SSteffen Klassert 906f809da2SSteffen Klassert if (type == ICMPV6_PKT_TOOBIG) 915f379ef5SGeorg Kohmann ip6_update_pmtu(skb, net, info, skb->dev->ifindex, 0, sock_net_uid(net, NULL)); 926f809da2SSteffen Klassert else if (type == NDISC_REDIRECT) 93e2d118a1SLorenzo Colitti ip6_redirect(skb, net, skb->dev->ifindex, 0, 94e2d118a1SLorenzo Colitti sock_net_uid(net, NULL)); 956d0bfe22SLorenzo Colitti 966d0bfe22SLorenzo Colitti if (!(type & ICMPV6_INFOMSG_MASK)) 976d0bfe22SLorenzo Colitti if (icmp6->icmp6_type == ICMPV6_ECHO_REQUEST) 98dcb94b88SHannes Frederic Sowa ping_err(skb, offset, ntohl(info)); 9932bbd879SStefano Brivio 10032bbd879SStefano Brivio return 0; 1016f809da2SSteffen Klassert } 1026f809da2SSteffen Klassert 103e5bbef20SHerbert Xu static int icmpv6_rcv(struct sk_buff *skb); 1041da177e4SLinus Torvalds 10541135cc8SAlexey Dobriyan static const struct inet6_protocol icmpv6_protocol = { 1061da177e4SLinus Torvalds .handler = icmpv6_rcv, 1076f809da2SSteffen Klassert .err_handler = icmpv6_err, 1088b7817f3SHerbert Xu .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, 1091da177e4SLinus Torvalds }; 1101da177e4SLinus Torvalds 1117ba91ecbSJesper Dangaard Brouer /* Called with BH disabled */ 112fdc0bde9SDenis V. Lunev static __inline__ struct sock *icmpv6_xmit_lock(struct net *net) 1131da177e4SLinus Torvalds { 114fdc0bde9SDenis V. Lunev struct sock *sk; 115fdc0bde9SDenis V. Lunev 116fdc0bde9SDenis V. Lunev sk = icmpv6_sk(net); 117405666dbSDenis V. Lunev if (unlikely(!spin_trylock(&sk->sk_lock.slock))) { 1181da177e4SLinus Torvalds /* This can happen if the output path (f.e. SIT or 1191da177e4SLinus Torvalds * ip6ip6 tunnel) signals dst_link_failure() for an 1201da177e4SLinus Torvalds * outgoing ICMP6 packet. 1211da177e4SLinus Torvalds */ 122fdc0bde9SDenis V. Lunev return NULL; 1231da177e4SLinus Torvalds } 124fdc0bde9SDenis V. Lunev return sk; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 127405666dbSDenis V. Lunev static __inline__ void icmpv6_xmit_unlock(struct sock *sk) 1281da177e4SLinus Torvalds { 1297ba91ecbSJesper Dangaard Brouer spin_unlock(&sk->sk_lock.slock); 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds /* 1331da177e4SLinus Torvalds * Figure out, may we reply to this packet with icmp error. 1341da177e4SLinus Torvalds * 1351da177e4SLinus Torvalds * We do not reply, if: 1361da177e4SLinus Torvalds * - it was icmp error message. 1371da177e4SLinus Torvalds * - it is truncated, so that it is known, that protocol is ICMPV6 1381da177e4SLinus Torvalds * (i.e. in the middle of some exthdr) 1391da177e4SLinus Torvalds * 1401da177e4SLinus Torvalds * --ANK (980726) 1411da177e4SLinus Torvalds */ 1421da177e4SLinus Torvalds 143a50feda5SEric Dumazet static bool is_ineligible(const struct sk_buff *skb) 1441da177e4SLinus Torvalds { 1450660e03fSArnaldo Carvalho de Melo int ptr = (u8 *)(ipv6_hdr(skb) + 1) - skb->data; 1461da177e4SLinus Torvalds int len = skb->len - ptr; 1470660e03fSArnaldo Carvalho de Melo __u8 nexthdr = ipv6_hdr(skb)->nexthdr; 14875f2811cSJesse Gross __be16 frag_off; 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds if (len < 0) 151a50feda5SEric Dumazet return true; 1521da177e4SLinus Torvalds 15375f2811cSJesse Gross ptr = ipv6_skip_exthdr(skb, ptr, &nexthdr, &frag_off); 1541da177e4SLinus Torvalds if (ptr < 0) 155a50feda5SEric Dumazet return false; 1561da177e4SLinus Torvalds if (nexthdr == IPPROTO_ICMPV6) { 1571da177e4SLinus Torvalds u8 _type, *tp; 1581da177e4SLinus Torvalds tp = skb_header_pointer(skb, 1591da177e4SLinus Torvalds ptr+offsetof(struct icmp6hdr, icmp6_type), 1601da177e4SLinus Torvalds sizeof(_type), &_type); 16163159f29SIan Morris if (!tp || !(*tp & ICMPV6_INFOMSG_MASK)) 162a50feda5SEric Dumazet return true; 1631da177e4SLinus Torvalds } 164a50feda5SEric Dumazet return false; 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds 1670bc19985SStephen Suryaputra static bool icmpv6_mask_allow(struct net *net, int type) 168c0303efeSJesper Dangaard Brouer { 1690bc19985SStephen Suryaputra if (type > ICMPV6_MSG_MAX) 170c0303efeSJesper Dangaard Brouer return true; 171c0303efeSJesper Dangaard Brouer 1720bc19985SStephen Suryaputra /* Limit if icmp type is set in ratemask. */ 1730bc19985SStephen Suryaputra if (!test_bit(type, net->ipv6.sysctl.icmpv6_ratemask)) 174c0303efeSJesper Dangaard Brouer return true; 175c0303efeSJesper Dangaard Brouer 176c0303efeSJesper Dangaard Brouer return false; 177c0303efeSJesper Dangaard Brouer } 178c0303efeSJesper Dangaard Brouer 1790bc19985SStephen Suryaputra static bool icmpv6_global_allow(struct net *net, int type) 180c0303efeSJesper Dangaard Brouer { 1810bc19985SStephen Suryaputra if (icmpv6_mask_allow(net, type)) 182c0303efeSJesper Dangaard Brouer return true; 183c0303efeSJesper Dangaard Brouer 184c0303efeSJesper Dangaard Brouer if (icmp_global_allow()) 185c0303efeSJesper Dangaard Brouer return true; 186c0303efeSJesper Dangaard Brouer 187c0303efeSJesper Dangaard Brouer return false; 188c0303efeSJesper Dangaard Brouer } 189c0303efeSJesper Dangaard Brouer 1901da177e4SLinus Torvalds /* 1911da177e4SLinus Torvalds * Check the ICMP output rate limit 1921da177e4SLinus Torvalds */ 1934cdf507dSEric Dumazet static bool icmpv6_xrlim_allow(struct sock *sk, u8 type, 1944c9483b2SDavid S. Miller struct flowi6 *fl6) 1951da177e4SLinus Torvalds { 1963b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 1974cdf507dSEric Dumazet struct dst_entry *dst; 19892d86829SDavid S. Miller bool res = false; 1991da177e4SLinus Torvalds 2000bc19985SStephen Suryaputra if (icmpv6_mask_allow(net, type)) 20192d86829SDavid S. Miller return true; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds /* 2041da177e4SLinus Torvalds * Look up the output route. 2051da177e4SLinus Torvalds * XXX: perhaps the expire for routing entries cloned by 2061da177e4SLinus Torvalds * this lookup should be more aggressive (not longer than timeout). 2071da177e4SLinus Torvalds */ 2084c9483b2SDavid S. Miller dst = ip6_route_output(net, sk, fl6); 2091da177e4SLinus Torvalds if (dst->error) { 2103bd653c8SDenis V. Lunev IP6_INC_STATS(net, ip6_dst_idev(dst), 211a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_OUTNOROUTES); 2121da177e4SLinus Torvalds } else if (dst->dev && (dst->dev->flags&IFF_LOOPBACK)) { 21392d86829SDavid S. Miller res = true; 2141da177e4SLinus Torvalds } else { 2151da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 2169a43b709SBenjamin Thery int tmo = net->ipv6.sysctl.icmpv6_time; 217c0303efeSJesper Dangaard Brouer struct inet_peer *peer; 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds /* Give more bandwidth to wider prefixes. */ 2201da177e4SLinus Torvalds if (rt->rt6i_dst.plen < 128) 2211da177e4SLinus Torvalds tmo >>= ((128 - rt->rt6i_dst.plen)>>5); 2221da177e4SLinus Torvalds 223c0303efeSJesper Dangaard Brouer peer = inet_getpeer_v6(net->ipv6.peers, &fl6->daddr, 1); 224fbfe95a4SDavid S. Miller res = inet_peer_xrlim_allow(peer, tmo); 2251d861aa4SDavid S. Miller if (peer) 2261d861aa4SDavid S. Miller inet_putpeer(peer); 2271da177e4SLinus Torvalds } 2281da177e4SLinus Torvalds dst_release(dst); 2291da177e4SLinus Torvalds return res; 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds /* 2331da177e4SLinus Torvalds * an inline helper for the "simple" if statement below 2341da177e4SLinus Torvalds * checks if parameter problem report is caused by an 2351da177e4SLinus Torvalds * unrecognized IPv6 option that has the Option Type 2361da177e4SLinus Torvalds * highest-order two bits set to 10 2371da177e4SLinus Torvalds */ 2381da177e4SLinus Torvalds 239a50feda5SEric Dumazet static bool opt_unrec(struct sk_buff *skb, __u32 offset) 2401da177e4SLinus Torvalds { 2411da177e4SLinus Torvalds u8 _optval, *op; 2421da177e4SLinus Torvalds 243bbe735e4SArnaldo Carvalho de Melo offset += skb_network_offset(skb); 2441da177e4SLinus Torvalds op = skb_header_pointer(skb, offset, sizeof(_optval), &_optval); 24563159f29SIan Morris if (!op) 246a50feda5SEric Dumazet return true; 2471da177e4SLinus Torvalds return (*op & 0xC0) == 0x80; 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 2504e64b1edSJoe Perches void icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6, 2516d0bfe22SLorenzo Colitti struct icmp6hdr *thdr, int len) 2521da177e4SLinus Torvalds { 2531da177e4SLinus Torvalds struct sk_buff *skb; 2541da177e4SLinus Torvalds struct icmp6hdr *icmp6h; 2551da177e4SLinus Torvalds 256e5d08d71SIan Morris skb = skb_peek(&sk->sk_write_queue); 25763159f29SIan Morris if (!skb) 2584e64b1edSJoe Perches return; 2591da177e4SLinus Torvalds 260cc70ab26SArnaldo Carvalho de Melo icmp6h = icmp6_hdr(skb); 2611da177e4SLinus Torvalds memcpy(icmp6h, thdr, sizeof(struct icmp6hdr)); 2621da177e4SLinus Torvalds icmp6h->icmp6_cksum = 0; 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds if (skb_queue_len(&sk->sk_write_queue) == 1) { 26507f0757aSJoe Perches skb->csum = csum_partial(icmp6h, 2661da177e4SLinus Torvalds sizeof(struct icmp6hdr), skb->csum); 2674c9483b2SDavid S. Miller icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr, 2684c9483b2SDavid S. Miller &fl6->daddr, 2694c9483b2SDavid S. Miller len, fl6->flowi6_proto, 2701da177e4SLinus Torvalds skb->csum); 2711da177e4SLinus Torvalds } else { 272868c86bcSAl Viro __wsum tmp_csum = 0; 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds skb_queue_walk(&sk->sk_write_queue, skb) { 2751da177e4SLinus Torvalds tmp_csum = csum_add(tmp_csum, skb->csum); 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds 27807f0757aSJoe Perches tmp_csum = csum_partial(icmp6h, 2791da177e4SLinus Torvalds sizeof(struct icmp6hdr), tmp_csum); 2804c9483b2SDavid S. Miller icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr, 2814c9483b2SDavid S. Miller &fl6->daddr, 2824c9483b2SDavid S. Miller len, fl6->flowi6_proto, 283868c86bcSAl Viro tmp_csum); 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds ip6_push_pending_frames(sk); 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds struct icmpv6_msg { 2891da177e4SLinus Torvalds struct sk_buff *skb; 2901da177e4SLinus Torvalds int offset; 291763ecff1SYasuyuki Kozakai uint8_t type; 2921da177e4SLinus Torvalds }; 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds static int icmpv6_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb) 2951da177e4SLinus Torvalds { 2961da177e4SLinus Torvalds struct icmpv6_msg *msg = (struct icmpv6_msg *) from; 2971da177e4SLinus Torvalds struct sk_buff *org_skb = msg->skb; 2985f92a738SAl Viro __wsum csum = 0; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(org_skb, msg->offset + offset, 3011da177e4SLinus Torvalds to, len, csum); 3021da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, odd); 303763ecff1SYasuyuki Kozakai if (!(msg->type & ICMPV6_INFOMSG_MASK)) 304763ecff1SYasuyuki Kozakai nf_ct_attach(skb, org_skb); 3051da177e4SLinus Torvalds return 0; 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds 30807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 30979383236SMasahide NAKAMURA static void mip6_addr_swap(struct sk_buff *skb) 31079383236SMasahide NAKAMURA { 3110660e03fSArnaldo Carvalho de Melo struct ipv6hdr *iph = ipv6_hdr(skb); 31279383236SMasahide NAKAMURA struct inet6_skb_parm *opt = IP6CB(skb); 31379383236SMasahide NAKAMURA struct ipv6_destopt_hao *hao; 31479383236SMasahide NAKAMURA struct in6_addr tmp; 31579383236SMasahide NAKAMURA int off; 31679383236SMasahide NAKAMURA 31779383236SMasahide NAKAMURA if (opt->dsthao) { 31879383236SMasahide NAKAMURA off = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO); 31979383236SMasahide NAKAMURA if (likely(off >= 0)) { 320d56f90a7SArnaldo Carvalho de Melo hao = (struct ipv6_destopt_hao *) 321d56f90a7SArnaldo Carvalho de Melo (skb_network_header(skb) + off); 3224e3fd7a0SAlexey Dobriyan tmp = iph->saddr; 3234e3fd7a0SAlexey Dobriyan iph->saddr = hao->addr; 3244e3fd7a0SAlexey Dobriyan hao->addr = tmp; 32579383236SMasahide NAKAMURA } 32679383236SMasahide NAKAMURA } 32779383236SMasahide NAKAMURA } 32879383236SMasahide NAKAMURA #else 32979383236SMasahide NAKAMURA static inline void mip6_addr_swap(struct sk_buff *skb) {} 33079383236SMasahide NAKAMURA #endif 33179383236SMasahide NAKAMURA 332e8243534Sstephen hemminger static struct dst_entry *icmpv6_route_lookup(struct net *net, 333e8243534Sstephen hemminger struct sk_buff *skb, 334e8243534Sstephen hemminger struct sock *sk, 335e8243534Sstephen hemminger struct flowi6 *fl6) 336b42835dbSDavid S. Miller { 337b42835dbSDavid S. Miller struct dst_entry *dst, *dst2; 3384c9483b2SDavid S. Miller struct flowi6 fl2; 339b42835dbSDavid S. Miller int err; 340b42835dbSDavid S. Miller 341343d60aaSRoopa Prabhu err = ip6_dst_lookup(net, sk, &dst, fl6); 342b42835dbSDavid S. Miller if (err) 343b42835dbSDavid S. Miller return ERR_PTR(err); 344b42835dbSDavid S. Miller 345b42835dbSDavid S. Miller /* 346b42835dbSDavid S. Miller * We won't send icmp if the destination is known 347b42835dbSDavid S. Miller * anycast. 348b42835dbSDavid S. Miller */ 3492647a9b0SMartin KaFai Lau if (ipv6_anycast_destination(dst, &fl6->daddr)) { 350ba7a46f1SJoe Perches net_dbg_ratelimited("icmp6_send: acast source\n"); 351b42835dbSDavid S. Miller dst_release(dst); 352b42835dbSDavid S. Miller return ERR_PTR(-EINVAL); 353b42835dbSDavid S. Miller } 354b42835dbSDavid S. Miller 355b42835dbSDavid S. Miller /* No need to clone since we're just using its address. */ 356b42835dbSDavid S. Miller dst2 = dst; 357b42835dbSDavid S. Miller 3584c9483b2SDavid S. Miller dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), sk, 0); 359452edd59SDavid S. Miller if (!IS_ERR(dst)) { 360b42835dbSDavid S. Miller if (dst != dst2) 361b42835dbSDavid S. Miller return dst; 362452edd59SDavid S. Miller } else { 363452edd59SDavid S. Miller if (PTR_ERR(dst) == -EPERM) 364b42835dbSDavid S. Miller dst = NULL; 365452edd59SDavid S. Miller else 366452edd59SDavid S. Miller return dst; 367b42835dbSDavid S. Miller } 368b42835dbSDavid S. Miller 3694c9483b2SDavid S. Miller err = xfrm_decode_session_reverse(skb, flowi6_to_flowi(&fl2), AF_INET6); 370b42835dbSDavid S. Miller if (err) 371b42835dbSDavid S. Miller goto relookup_failed; 372b42835dbSDavid S. Miller 373343d60aaSRoopa Prabhu err = ip6_dst_lookup(net, sk, &dst2, &fl2); 374b42835dbSDavid S. Miller if (err) 375b42835dbSDavid S. Miller goto relookup_failed; 376b42835dbSDavid S. Miller 3774c9483b2SDavid S. Miller dst2 = xfrm_lookup(net, dst2, flowi6_to_flowi(&fl2), sk, XFRM_LOOKUP_ICMP); 378452edd59SDavid S. Miller if (!IS_ERR(dst2)) { 379b42835dbSDavid S. Miller dst_release(dst); 380b42835dbSDavid S. Miller dst = dst2; 381452edd59SDavid S. Miller } else { 382452edd59SDavid S. Miller err = PTR_ERR(dst2); 383452edd59SDavid S. Miller if (err == -EPERM) { 384b42835dbSDavid S. Miller dst_release(dst); 385452edd59SDavid S. Miller return dst2; 386452edd59SDavid S. Miller } else 387b42835dbSDavid S. Miller goto relookup_failed; 388b42835dbSDavid S. Miller } 389b42835dbSDavid S. Miller 390b42835dbSDavid S. Miller relookup_failed: 391b42835dbSDavid S. Miller if (dst) 392b42835dbSDavid S. Miller return dst; 393b42835dbSDavid S. Miller return ERR_PTR(err); 394b42835dbSDavid S. Miller } 395b42835dbSDavid S. Miller 396e1ae5c2eSStephen Suryaputra static struct net_device *icmp6_dev(const struct sk_buff *skb) 3971b70d792SDavid Ahern { 398e1ae5c2eSStephen Suryaputra struct net_device *dev = skb->dev; 3991b70d792SDavid Ahern 4001b70d792SDavid Ahern /* for local traffic to local address, skb dev is the loopback 4011b70d792SDavid Ahern * device. Check if there is a dst attached to the skb and if so 40224b711edSDavid Ahern * get the real device index. Same is needed for replies to a link 40324b711edSDavid Ahern * local address on a device enslaved to an L3 master device 4041b70d792SDavid Ahern */ 405e1ae5c2eSStephen Suryaputra if (unlikely(dev->ifindex == LOOPBACK_IFINDEX || netif_is_l3_master(skb->dev))) { 4061b70d792SDavid Ahern const struct rt6_info *rt6 = skb_rt6_info(skb); 4071b70d792SDavid Ahern 4081b70d792SDavid Ahern if (rt6) 409e1ae5c2eSStephen Suryaputra dev = rt6->rt6i_idev->dev; 4101b70d792SDavid Ahern } 4111b70d792SDavid Ahern 412e1ae5c2eSStephen Suryaputra return dev; 413e1ae5c2eSStephen Suryaputra } 414e1ae5c2eSStephen Suryaputra 415e1ae5c2eSStephen Suryaputra static int icmp6_iif(const struct sk_buff *skb) 416e1ae5c2eSStephen Suryaputra { 417e1ae5c2eSStephen Suryaputra return icmp6_dev(skb)->ifindex; 4181b70d792SDavid Ahern } 4191b70d792SDavid Ahern 4201da177e4SLinus Torvalds /* 4211da177e4SLinus Torvalds * Send an ICMP message in response to a packet in error 4221da177e4SLinus Torvalds */ 423b1cadc1aSEric Dumazet static void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info, 424b1cadc1aSEric Dumazet const struct in6_addr *force_saddr) 4251da177e4SLinus Torvalds { 4261da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 4270660e03fSArnaldo Carvalho de Melo struct ipv6hdr *hdr = ipv6_hdr(skb); 42884427d53SYOSHIFUJI Hideaki struct sock *sk; 4298d933670SEric Dumazet struct net *net; 43084427d53SYOSHIFUJI Hideaki struct ipv6_pinfo *np; 431b71d1d42SEric Dumazet const struct in6_addr *saddr = NULL; 4321da177e4SLinus Torvalds struct dst_entry *dst; 4331da177e4SLinus Torvalds struct icmp6hdr tmp_hdr; 4344c9483b2SDavid S. Miller struct flowi6 fl6; 4351da177e4SLinus Torvalds struct icmpv6_msg msg; 43626879da5SWei Wang struct ipcm6_cookie ipc6; 4371da177e4SLinus Torvalds int iif = 0; 4381da177e4SLinus Torvalds int addr_type = 0; 4391da177e4SLinus Torvalds int len; 4408d933670SEric Dumazet u32 mark; 4411da177e4SLinus Torvalds 44227a884dcSArnaldo Carvalho de Melo if ((u8 *)hdr < skb->head || 44329a3cad5SSimon Horman (skb_network_header(skb) + sizeof(*hdr)) > skb_tail_pointer(skb)) 4441da177e4SLinus Torvalds return; 4451da177e4SLinus Torvalds 4468d933670SEric Dumazet if (!skb->dev) 4478d933670SEric Dumazet return; 4488d933670SEric Dumazet net = dev_net(skb->dev); 4498d933670SEric Dumazet mark = IP6_REPLY_MARK(net, skb->mark); 4501da177e4SLinus Torvalds /* 4511da177e4SLinus Torvalds * Make sure we respect the rules 4521da177e4SLinus Torvalds * i.e. RFC 1885 2.4(e) 4535f5624cfSPravin B Shelar * Rule (e.1) is enforced by not using icmp6_send 4541da177e4SLinus Torvalds * in any code that processes icmp errors. 4551da177e4SLinus Torvalds */ 4561da177e4SLinus Torvalds addr_type = ipv6_addr_type(&hdr->daddr); 4571da177e4SLinus Torvalds 458446fab59SFX Le Bail if (ipv6_chk_addr(net, &hdr->daddr, skb->dev, 0) || 459d94c1f92SFX Le Bail ipv6_chk_acast_addr_src(net, skb->dev, &hdr->daddr)) 4601da177e4SLinus Torvalds saddr = &hdr->daddr; 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds /* 4631da177e4SLinus Torvalds * Dest addr check 4641da177e4SLinus Torvalds */ 4651da177e4SLinus Torvalds 4669a6b4b39Szhuyj if (addr_type & IPV6_ADDR_MULTICAST || skb->pkt_type != PACKET_HOST) { 4671da177e4SLinus Torvalds if (type != ICMPV6_PKT_TOOBIG && 4681da177e4SLinus Torvalds !(type == ICMPV6_PARAMPROB && 4691da177e4SLinus Torvalds code == ICMPV6_UNK_OPTION && 4701da177e4SLinus Torvalds (opt_unrec(skb, info)))) 4711da177e4SLinus Torvalds return; 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds saddr = NULL; 4741da177e4SLinus Torvalds } 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds addr_type = ipv6_addr_type(&hdr->saddr); 4771da177e4SLinus Torvalds 4781da177e4SLinus Torvalds /* 4791da177e4SLinus Torvalds * Source addr check 4801da177e4SLinus Torvalds */ 4811da177e4SLinus Torvalds 4824832c30dSDavid Ahern if (__ipv6_addr_needs_scope_id(addr_type)) { 4831b70d792SDavid Ahern iif = icmp6_iif(skb); 4844832c30dSDavid Ahern } else { 48579dc7e3fSDavid Ahern dst = skb_dst(skb); 48679dc7e3fSDavid Ahern iif = l3mdev_master_ifindex(dst ? dst->dev : skb->dev); 48779dc7e3fSDavid Ahern } 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds /* 4908de3351eSYOSHIFUJI Hideaki * Must not send error if the source does not uniquely 4918de3351eSYOSHIFUJI Hideaki * identify a single node (RFC2463 Section 2.4). 4928de3351eSYOSHIFUJI Hideaki * We check unspecified / multicast addresses here, 4938de3351eSYOSHIFUJI Hideaki * and anycast addresses will be checked later. 4941da177e4SLinus Torvalds */ 4951da177e4SLinus Torvalds if ((addr_type == IPV6_ADDR_ANY) || (addr_type & IPV6_ADDR_MULTICAST)) { 4964b3418fbSBjørn Mork net_dbg_ratelimited("icmp6_send: addr_any/mcast source [%pI6c > %pI6c]\n", 4974b3418fbSBjørn Mork &hdr->saddr, &hdr->daddr); 4981da177e4SLinus Torvalds return; 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds /* 5021da177e4SLinus Torvalds * Never answer to a ICMP packet. 5031da177e4SLinus Torvalds */ 5041da177e4SLinus Torvalds if (is_ineligible(skb)) { 5054b3418fbSBjørn Mork net_dbg_ratelimited("icmp6_send: no reply to icmp error [%pI6c > %pI6c]\n", 5064b3418fbSBjørn Mork &hdr->saddr, &hdr->daddr); 5071da177e4SLinus Torvalds return; 5081da177e4SLinus Torvalds } 5091da177e4SLinus Torvalds 5107ba91ecbSJesper Dangaard Brouer /* Needed by both icmp_global_allow and icmpv6_xmit_lock */ 5117ba91ecbSJesper Dangaard Brouer local_bh_disable(); 5127ba91ecbSJesper Dangaard Brouer 5137ba91ecbSJesper Dangaard Brouer /* Check global sysctl_icmp_msgs_per_sec ratelimit */ 5140bc19985SStephen Suryaputra if (!(skb->dev->flags & IFF_LOOPBACK) && !icmpv6_global_allow(net, type)) 5157ba91ecbSJesper Dangaard Brouer goto out_bh_enable; 5167ba91ecbSJesper Dangaard Brouer 51779383236SMasahide NAKAMURA mip6_addr_swap(skb); 51879383236SMasahide NAKAMURA 519fac6fce9SFrancesco Ruggeri sk = icmpv6_xmit_lock(net); 520fac6fce9SFrancesco Ruggeri if (!sk) 521fac6fce9SFrancesco Ruggeri goto out_bh_enable; 522fac6fce9SFrancesco Ruggeri 5234c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 5244c9483b2SDavid S. Miller fl6.flowi6_proto = IPPROTO_ICMPV6; 5254e3fd7a0SAlexey Dobriyan fl6.daddr = hdr->saddr; 526b1cadc1aSEric Dumazet if (force_saddr) 527b1cadc1aSEric Dumazet saddr = force_saddr; 528fac6fce9SFrancesco Ruggeri if (saddr) { 5294e3fd7a0SAlexey Dobriyan fl6.saddr = *saddr; 530fac6fce9SFrancesco Ruggeri } else { 531fac6fce9SFrancesco Ruggeri /* select a more meaningful saddr from input if */ 532fac6fce9SFrancesco Ruggeri struct net_device *in_netdev; 533fac6fce9SFrancesco Ruggeri 534fac6fce9SFrancesco Ruggeri in_netdev = dev_get_by_index(net, IP6CB(skb)->iif); 535fac6fce9SFrancesco Ruggeri if (in_netdev) { 536fac6fce9SFrancesco Ruggeri ipv6_dev_get_saddr(net, in_netdev, &fl6.daddr, 537fac6fce9SFrancesco Ruggeri inet6_sk(sk)->srcprefs, 538fac6fce9SFrancesco Ruggeri &fl6.saddr); 539fac6fce9SFrancesco Ruggeri dev_put(in_netdev); 540fac6fce9SFrancesco Ruggeri } 541fac6fce9SFrancesco Ruggeri } 542e110861fSLorenzo Colitti fl6.flowi6_mark = mark; 5434c9483b2SDavid S. Miller fl6.flowi6_oif = iif; 5441958b856SDavid S. Miller fl6.fl6_icmp_type = type; 5451958b856SDavid S. Miller fl6.fl6_icmp_code = code; 546e2d118a1SLorenzo Colitti fl6.flowi6_uid = sock_net_uid(net, NULL); 547b4bac172SDavid Ahern fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, NULL); 5484c9483b2SDavid S. Miller security_skb_classify_flow(skb, flowi6_to_flowi(&fl6)); 5491da177e4SLinus Torvalds 550e110861fSLorenzo Colitti sk->sk_mark = mark; 551fdc0bde9SDenis V. Lunev np = inet6_sk(sk); 552405666dbSDenis V. Lunev 5534c9483b2SDavid S. Miller if (!icmpv6_xrlim_allow(sk, type, &fl6)) 5541da177e4SLinus Torvalds goto out; 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds tmp_hdr.icmp6_type = type; 5571da177e4SLinus Torvalds tmp_hdr.icmp6_code = code; 5581da177e4SLinus Torvalds tmp_hdr.icmp6_cksum = 0; 5591da177e4SLinus Torvalds tmp_hdr.icmp6_pointer = htonl(info); 5601da177e4SLinus Torvalds 5614c9483b2SDavid S. Miller if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) 5624c9483b2SDavid S. Miller fl6.flowi6_oif = np->mcast_oif; 563c4062dfcSErich E. Hoover else if (!fl6.flowi6_oif) 564c4062dfcSErich E. Hoover fl6.flowi6_oif = np->ucast_oif; 5651da177e4SLinus Torvalds 566b515430aSWillem de Bruijn ipcm6_init_sk(&ipc6, np); 56738b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel); 56838b7097bSHannes Frederic Sowa 5694c9483b2SDavid S. Miller dst = icmpv6_route_lookup(net, skb, sk, &fl6); 570b42835dbSDavid S. Miller if (IS_ERR(dst)) 5711da177e4SLinus Torvalds goto out; 5728de3351eSYOSHIFUJI Hideaki 57326879da5SWei Wang ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst); 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds msg.skb = skb; 576bbe735e4SArnaldo Carvalho de Melo msg.offset = skb_network_offset(skb); 577763ecff1SYasuyuki Kozakai msg.type = type; 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds len = skb->len - msg.offset; 5801da177e4SLinus Torvalds len = min_t(unsigned int, len, IPV6_MIN_MTU - sizeof(struct ipv6hdr) - sizeof(struct icmp6hdr)); 5811da177e4SLinus Torvalds if (len < 0) { 5824b3418fbSBjørn Mork net_dbg_ratelimited("icmp: len problem [%pI6c > %pI6c]\n", 5834b3418fbSBjørn Mork &hdr->saddr, &hdr->daddr); 5841da177e4SLinus Torvalds goto out_dst_release; 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds 587cfdf7647SEric Dumazet rcu_read_lock(); 588cfdf7647SEric Dumazet idev = __in6_dev_get(skb->dev); 5891da177e4SLinus Torvalds 5904e64b1edSJoe Perches if (ip6_append_data(sk, icmpv6_getfrag, &msg, 5911da177e4SLinus Torvalds len + sizeof(struct icmp6hdr), 59226879da5SWei Wang sizeof(struct icmp6hdr), 59326879da5SWei Wang &ipc6, &fl6, (struct rt6_info *)dst, 5945fdaa88dSWillem de Bruijn MSG_DONTWAIT)) { 59543a43b60SHannes Frederic Sowa ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS); 5961da177e4SLinus Torvalds ip6_flush_pending_frames(sk); 597cfdf7647SEric Dumazet } else { 5984e64b1edSJoe Perches icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr, 599cfdf7647SEric Dumazet len + sizeof(struct icmp6hdr)); 6001da177e4SLinus Torvalds } 601cfdf7647SEric Dumazet rcu_read_unlock(); 6021da177e4SLinus Torvalds out_dst_release: 6031da177e4SLinus Torvalds dst_release(dst); 6041da177e4SLinus Torvalds out: 605405666dbSDenis V. Lunev icmpv6_xmit_unlock(sk); 6067ba91ecbSJesper Dangaard Brouer out_bh_enable: 6077ba91ecbSJesper Dangaard Brouer local_bh_enable(); 6081da177e4SLinus Torvalds } 6095f5624cfSPravin B Shelar 6105f5624cfSPravin B Shelar /* Slightly more convenient version of icmp6_send. 6115f5624cfSPravin B Shelar */ 6125f5624cfSPravin B Shelar void icmpv6_param_prob(struct sk_buff *skb, u8 code, int pos) 6135f5624cfSPravin B Shelar { 614b1cadc1aSEric Dumazet icmp6_send(skb, ICMPV6_PARAMPROB, code, pos, NULL); 6155f5624cfSPravin B Shelar kfree_skb(skb); 6165f5624cfSPravin B Shelar } 6177159039aSYOSHIFUJI Hideaki 6185fbba8acSEric Dumazet /* Generate icmpv6 with type/code ICMPV6_DEST_UNREACH/ICMPV6_ADDR_UNREACH 6195fbba8acSEric Dumazet * if sufficient data bytes are available 6205fbba8acSEric Dumazet * @nhs is the size of the tunnel header(s) : 6215fbba8acSEric Dumazet * Either an IPv4 header for SIT encap 6225fbba8acSEric Dumazet * an IPv4 header + GRE header for GRE encap 6235fbba8acSEric Dumazet */ 62420e1954fSEric Dumazet int ip6_err_gen_icmpv6_unreach(struct sk_buff *skb, int nhs, int type, 62520e1954fSEric Dumazet unsigned int data_len) 6265fbba8acSEric Dumazet { 6272d7a3b27SEric Dumazet struct in6_addr temp_saddr; 6285fbba8acSEric Dumazet struct rt6_info *rt; 6295fbba8acSEric Dumazet struct sk_buff *skb2; 63020e1954fSEric Dumazet u32 info = 0; 6315fbba8acSEric Dumazet 6325fbba8acSEric Dumazet if (!pskb_may_pull(skb, nhs + sizeof(struct ipv6hdr) + 8)) 6335fbba8acSEric Dumazet return 1; 6345fbba8acSEric Dumazet 63520e1954fSEric Dumazet /* RFC 4884 (partial) support for ICMP extensions */ 63620e1954fSEric Dumazet if (data_len < 128 || (data_len & 7) || skb->len < data_len) 63720e1954fSEric Dumazet data_len = 0; 63820e1954fSEric Dumazet 63920e1954fSEric Dumazet skb2 = data_len ? skb_copy(skb, GFP_ATOMIC) : skb_clone(skb, GFP_ATOMIC); 6405fbba8acSEric Dumazet 6415fbba8acSEric Dumazet if (!skb2) 6425fbba8acSEric Dumazet return 1; 6435fbba8acSEric Dumazet 6445fbba8acSEric Dumazet skb_dst_drop(skb2); 6455fbba8acSEric Dumazet skb_pull(skb2, nhs); 6465fbba8acSEric Dumazet skb_reset_network_header(skb2); 6475fbba8acSEric Dumazet 648b75cc8f9SDavid Ahern rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr, NULL, 0, 649b75cc8f9SDavid Ahern skb, 0); 6505fbba8acSEric Dumazet 6515fbba8acSEric Dumazet if (rt && rt->dst.dev) 6525fbba8acSEric Dumazet skb2->dev = rt->dst.dev; 6535fbba8acSEric Dumazet 6542d7a3b27SEric Dumazet ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, &temp_saddr); 65520e1954fSEric Dumazet 65620e1954fSEric Dumazet if (data_len) { 65720e1954fSEric Dumazet /* RFC 4884 (partial) support : 65820e1954fSEric Dumazet * insert 0 padding at the end, before the extensions 65920e1954fSEric Dumazet */ 66020e1954fSEric Dumazet __skb_push(skb2, nhs); 66120e1954fSEric Dumazet skb_reset_network_header(skb2); 66220e1954fSEric Dumazet memmove(skb2->data, skb2->data + nhs, data_len - nhs); 66320e1954fSEric Dumazet memset(skb2->data + data_len - nhs, 0, nhs); 66420e1954fSEric Dumazet /* RFC 4884 4.5 : Length is measured in 64-bit words, 66520e1954fSEric Dumazet * and stored in reserved[0] 66620e1954fSEric Dumazet */ 66720e1954fSEric Dumazet info = (data_len/8) << 24; 66820e1954fSEric Dumazet } 6692d7a3b27SEric Dumazet if (type == ICMP_TIME_EXCEEDED) 6702d7a3b27SEric Dumazet icmp6_send(skb2, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 67120e1954fSEric Dumazet info, &temp_saddr); 6722d7a3b27SEric Dumazet else 6732d7a3b27SEric Dumazet icmp6_send(skb2, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 67420e1954fSEric Dumazet info, &temp_saddr); 6755fbba8acSEric Dumazet if (rt) 6765fbba8acSEric Dumazet ip6_rt_put(rt); 6775fbba8acSEric Dumazet 6785fbba8acSEric Dumazet kfree_skb(skb2); 6795fbba8acSEric Dumazet 6805fbba8acSEric Dumazet return 0; 6815fbba8acSEric Dumazet } 6825fbba8acSEric Dumazet EXPORT_SYMBOL(ip6_err_gen_icmpv6_unreach); 6835fbba8acSEric Dumazet 6841da177e4SLinus Torvalds static void icmpv6_echo_reply(struct sk_buff *skb) 6851da177e4SLinus Torvalds { 686c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 68784427d53SYOSHIFUJI Hideaki struct sock *sk; 6881da177e4SLinus Torvalds struct inet6_dev *idev; 68984427d53SYOSHIFUJI Hideaki struct ipv6_pinfo *np; 690b71d1d42SEric Dumazet const struct in6_addr *saddr = NULL; 691cc70ab26SArnaldo Carvalho de Melo struct icmp6hdr *icmph = icmp6_hdr(skb); 6921da177e4SLinus Torvalds struct icmp6hdr tmp_hdr; 6934c9483b2SDavid S. Miller struct flowi6 fl6; 6941da177e4SLinus Torvalds struct icmpv6_msg msg; 6951da177e4SLinus Torvalds struct dst_entry *dst; 69626879da5SWei Wang struct ipcm6_cookie ipc6; 697e110861fSLorenzo Colitti u32 mark = IP6_REPLY_MARK(net, skb->mark); 6980b03a5caSStephen Suryaputra bool acast; 6991da177e4SLinus Torvalds 70003f1ecccSStephen Suryaputra if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) && 70103f1ecccSStephen Suryaputra net->ipv6.sysctl.icmpv6_echo_ignore_multicast) 70203f1ecccSStephen Suryaputra return; 70303f1ecccSStephen Suryaputra 7040660e03fSArnaldo Carvalho de Melo saddr = &ipv6_hdr(skb)->daddr; 7051da177e4SLinus Torvalds 7060b03a5caSStephen Suryaputra acast = ipv6_anycast_destination(skb_dst(skb), saddr); 7070b03a5caSStephen Suryaputra if (acast && net->ipv6.sysctl.icmpv6_echo_ignore_anycast) 7080b03a5caSStephen Suryaputra return; 7090b03a5caSStephen Suryaputra 710509aba3bSFX Le Bail if (!ipv6_unicast_destination(skb) && 7110b03a5caSStephen Suryaputra !(net->ipv6.sysctl.anycast_src_echo_reply && acast)) 7121da177e4SLinus Torvalds saddr = NULL; 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds memcpy(&tmp_hdr, icmph, sizeof(tmp_hdr)); 7151da177e4SLinus Torvalds tmp_hdr.icmp6_type = ICMPV6_ECHO_REPLY; 7161da177e4SLinus Torvalds 7174c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 718a346abe0SEric Dumazet if (net->ipv6.sysctl.flowlabel_reflect & FLOWLABEL_REFLECT_ICMPV6_ECHO_REPLIES) 719a346abe0SEric Dumazet fl6.flowlabel = ip6_flowlabel(ipv6_hdr(skb)); 720a346abe0SEric Dumazet 7214c9483b2SDavid S. Miller fl6.flowi6_proto = IPPROTO_ICMPV6; 7224e3fd7a0SAlexey Dobriyan fl6.daddr = ipv6_hdr(skb)->saddr; 7231da177e4SLinus Torvalds if (saddr) 7244e3fd7a0SAlexey Dobriyan fl6.saddr = *saddr; 7251b70d792SDavid Ahern fl6.flowi6_oif = icmp6_iif(skb); 7261958b856SDavid S. Miller fl6.fl6_icmp_type = ICMPV6_ECHO_REPLY; 727e110861fSLorenzo Colitti fl6.flowi6_mark = mark; 728e2d118a1SLorenzo Colitti fl6.flowi6_uid = sock_net_uid(net, NULL); 7294c9483b2SDavid S. Miller security_skb_classify_flow(skb, flowi6_to_flowi(&fl6)); 7301da177e4SLinus Torvalds 7317ba91ecbSJesper Dangaard Brouer local_bh_disable(); 732fdc0bde9SDenis V. Lunev sk = icmpv6_xmit_lock(net); 73363159f29SIan Morris if (!sk) 7347ba91ecbSJesper Dangaard Brouer goto out_bh_enable; 735e110861fSLorenzo Colitti sk->sk_mark = mark; 736fdc0bde9SDenis V. Lunev np = inet6_sk(sk); 737405666dbSDenis V. Lunev 7384c9483b2SDavid S. Miller if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) 7394c9483b2SDavid S. Miller fl6.flowi6_oif = np->mcast_oif; 740c4062dfcSErich E. Hoover else if (!fl6.flowi6_oif) 741c4062dfcSErich E. Hoover fl6.flowi6_oif = np->ucast_oif; 7421da177e4SLinus Torvalds 7434e64b1edSJoe Perches if (ip6_dst_lookup(net, sk, &dst, &fl6)) 7441da177e4SLinus Torvalds goto out; 7454c9483b2SDavid S. Miller dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), sk, 0); 746452edd59SDavid S. Miller if (IS_ERR(dst)) 747e104411bSPatrick McHardy goto out; 7481da177e4SLinus Torvalds 7490bc19985SStephen Suryaputra /* Check the ratelimit */ 7500bc19985SStephen Suryaputra if ((!(skb->dev->flags & IFF_LOOPBACK) && !icmpv6_global_allow(net, ICMPV6_ECHO_REPLY)) || 7510bc19985SStephen Suryaputra !icmpv6_xrlim_allow(sk, ICMPV6_ECHO_REPLY, &fl6)) 7520bc19985SStephen Suryaputra goto out_dst_release; 7530bc19985SStephen Suryaputra 754cfdf7647SEric Dumazet idev = __in6_dev_get(skb->dev); 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds msg.skb = skb; 7571da177e4SLinus Torvalds msg.offset = 0; 758763ecff1SYasuyuki Kozakai msg.type = ICMPV6_ECHO_REPLY; 7591da177e4SLinus Torvalds 760b515430aSWillem de Bruijn ipcm6_init_sk(&ipc6, np); 76126879da5SWei Wang ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst); 76226879da5SWei Wang ipc6.tclass = ipv6_get_dsfield(ipv6_hdr(skb)); 76326879da5SWei Wang 7644e64b1edSJoe Perches if (ip6_append_data(sk, icmpv6_getfrag, &msg, 7654e64b1edSJoe Perches skb->len + sizeof(struct icmp6hdr), 76626879da5SWei Wang sizeof(struct icmp6hdr), &ipc6, &fl6, 7675fdaa88dSWillem de Bruijn (struct rt6_info *)dst, MSG_DONTWAIT)) { 768a16292a0SEric Dumazet __ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS); 7691da177e4SLinus Torvalds ip6_flush_pending_frames(sk); 770cfdf7647SEric Dumazet } else { 7714e64b1edSJoe Perches icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr, 772cfdf7647SEric Dumazet skb->len + sizeof(struct icmp6hdr)); 7731da177e4SLinus Torvalds } 7740bc19985SStephen Suryaputra out_dst_release: 7751da177e4SLinus Torvalds dst_release(dst); 7761da177e4SLinus Torvalds out: 777405666dbSDenis V. Lunev icmpv6_xmit_unlock(sk); 7787ba91ecbSJesper Dangaard Brouer out_bh_enable: 7797ba91ecbSJesper Dangaard Brouer local_bh_enable(); 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds 782b94f1c09SDavid S. Miller void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info) 7831da177e4SLinus Torvalds { 78441135cc8SAlexey Dobriyan const struct inet6_protocol *ipprot; 7851da177e4SLinus Torvalds int inner_offset; 78675f2811cSJesse Gross __be16 frag_off; 787f9242b6bSDavid S. Miller u8 nexthdr; 7887304fe46SDuan Jiong struct net *net = dev_net(skb->dev); 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) 7917304fe46SDuan Jiong goto out; 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds nexthdr = ((struct ipv6hdr *)skb->data)->nexthdr; 7941da177e4SLinus Torvalds if (ipv6_ext_hdr(nexthdr)) { 7951da177e4SLinus Torvalds /* now skip over extension headers */ 79675f2811cSJesse Gross inner_offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), 79775f2811cSJesse Gross &nexthdr, &frag_off); 7981da177e4SLinus Torvalds if (inner_offset < 0) 7997304fe46SDuan Jiong goto out; 8001da177e4SLinus Torvalds } else { 8011da177e4SLinus Torvalds inner_offset = sizeof(struct ipv6hdr); 8021da177e4SLinus Torvalds } 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds /* Checkin header including 8 bytes of inner protocol header. */ 8051da177e4SLinus Torvalds if (!pskb_may_pull(skb, inner_offset+8)) 8067304fe46SDuan Jiong goto out; 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds /* BUGGG_FUTURE: we should try to parse exthdrs in this packet. 8091da177e4SLinus Torvalds Without this we will not able f.e. to make source routed 8101da177e4SLinus Torvalds pmtu discovery. 8111da177e4SLinus Torvalds Corresponding argument (opt) to notifiers is already added. 8121da177e4SLinus Torvalds --ANK (980726) 8131da177e4SLinus Torvalds */ 8141da177e4SLinus Torvalds 815f9242b6bSDavid S. Miller ipprot = rcu_dereference(inet6_protos[nexthdr]); 8161da177e4SLinus Torvalds if (ipprot && ipprot->err_handler) 8171da177e4SLinus Torvalds ipprot->err_handler(skb, NULL, type, code, inner_offset, info); 8181da177e4SLinus Torvalds 81969d6da0bSPavel Emelyanov raw6_icmp_error(skb, nexthdr, type, code, inner_offset, info); 8207304fe46SDuan Jiong return; 8217304fe46SDuan Jiong 8227304fe46SDuan Jiong out: 823a16292a0SEric Dumazet __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), ICMP6_MIB_INERRORS); 8241da177e4SLinus Torvalds } 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds /* 8271da177e4SLinus Torvalds * Handle icmp messages 8281da177e4SLinus Torvalds */ 8291da177e4SLinus Torvalds 830e5bbef20SHerbert Xu static int icmpv6_rcv(struct sk_buff *skb) 8311da177e4SLinus Torvalds { 832e6f86b0fSVirgile Jarry struct net *net = dev_net(skb->dev); 833e1ae5c2eSStephen Suryaputra struct net_device *dev = icmp6_dev(skb); 8341da177e4SLinus Torvalds struct inet6_dev *idev = __in6_dev_get(dev); 835b71d1d42SEric Dumazet const struct in6_addr *saddr, *daddr; 8361da177e4SLinus Torvalds struct icmp6hdr *hdr; 837d5fdd6baSBrian Haley u8 type; 838e3e32170SRick Jones bool success = false; 8391da177e4SLinus Torvalds 840aebcf82cSHerbert Xu if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { 841def8b4faSAlexey Dobriyan struct sec_path *sp = skb_sec_path(skb); 8428b7817f3SHerbert Xu int nh; 8438b7817f3SHerbert Xu 844def8b4faSAlexey Dobriyan if (!(sp && sp->xvec[sp->len - 1]->props.flags & 845aebcf82cSHerbert Xu XFRM_STATE_ICMP)) 846aebcf82cSHerbert Xu goto drop_no_count; 847aebcf82cSHerbert Xu 84881aded24SDavid S. Miller if (!pskb_may_pull(skb, sizeof(*hdr) + sizeof(struct ipv6hdr))) 8498b7817f3SHerbert Xu goto drop_no_count; 8508b7817f3SHerbert Xu 8518b7817f3SHerbert Xu nh = skb_network_offset(skb); 8528b7817f3SHerbert Xu skb_set_network_header(skb, sizeof(*hdr)); 8538b7817f3SHerbert Xu 8548b7817f3SHerbert Xu if (!xfrm6_policy_check_reverse(NULL, XFRM_POLICY_IN, skb)) 8558b7817f3SHerbert Xu goto drop_no_count; 8568b7817f3SHerbert Xu 8578b7817f3SHerbert Xu skb_set_network_header(skb, nh); 8588b7817f3SHerbert Xu } 8598b7817f3SHerbert Xu 860a16292a0SEric Dumazet __ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_INMSGS); 8611da177e4SLinus Torvalds 8620660e03fSArnaldo Carvalho de Melo saddr = &ipv6_hdr(skb)->saddr; 8630660e03fSArnaldo Carvalho de Melo daddr = &ipv6_hdr(skb)->daddr; 8641da177e4SLinus Torvalds 86539471ac8STom Herbert if (skb_checksum_validate(skb, IPPROTO_ICMPV6, ip6_compute_pseudo)) { 866ba7a46f1SJoe Perches net_dbg_ratelimited("ICMPv6 checksum failed [%pI6c > %pI6c]\n", 8670c6ce78aSHarvey Harrison saddr, daddr); 8686a5dc9e5SEric Dumazet goto csum_error; 8691da177e4SLinus Torvalds } 8701da177e4SLinus Torvalds 8718cf22943SHerbert Xu if (!pskb_pull(skb, sizeof(*hdr))) 8728cf22943SHerbert Xu goto discard_it; 8731da177e4SLinus Torvalds 874cc70ab26SArnaldo Carvalho de Melo hdr = icmp6_hdr(skb); 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds type = hdr->icmp6_type; 8771da177e4SLinus Torvalds 878f3832ed2SEric Dumazet ICMP6MSGIN_INC_STATS(dev_net(dev), idev, type); 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds switch (type) { 8811da177e4SLinus Torvalds case ICMPV6_ECHO_REQUEST: 882e6f86b0fSVirgile Jarry if (!net->ipv6.sysctl.icmpv6_echo_ignore_all) 8831da177e4SLinus Torvalds icmpv6_echo_reply(skb); 8841da177e4SLinus Torvalds break; 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds case ICMPV6_ECHO_REPLY: 887e3e32170SRick Jones success = ping_rcv(skb); 8881da177e4SLinus Torvalds break; 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds case ICMPV6_PKT_TOOBIG: 8911da177e4SLinus Torvalds /* BUGGG_FUTURE: if packet contains rthdr, we cannot update 8921da177e4SLinus Torvalds standard destination cache. Seems, only "advanced" 8931da177e4SLinus Torvalds destination cache will allow to solve this problem 8941da177e4SLinus Torvalds --ANK (980726) 8951da177e4SLinus Torvalds */ 8961da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) 8971da177e4SLinus Torvalds goto discard_it; 898cc70ab26SArnaldo Carvalho de Melo hdr = icmp6_hdr(skb); 8991da177e4SLinus Torvalds 900275757e6SGustavo A. R. Silva /* to notify */ 901a8eceea8SJoe Perches fallthrough; 9021da177e4SLinus Torvalds case ICMPV6_DEST_UNREACH: 9031da177e4SLinus Torvalds case ICMPV6_TIME_EXCEED: 9041da177e4SLinus Torvalds case ICMPV6_PARAMPROB: 9051da177e4SLinus Torvalds icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu); 9061da177e4SLinus Torvalds break; 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds case NDISC_ROUTER_SOLICITATION: 9091da177e4SLinus Torvalds case NDISC_ROUTER_ADVERTISEMENT: 9101da177e4SLinus Torvalds case NDISC_NEIGHBOUR_SOLICITATION: 9111da177e4SLinus Torvalds case NDISC_NEIGHBOUR_ADVERTISEMENT: 9121da177e4SLinus Torvalds case NDISC_REDIRECT: 9131da177e4SLinus Torvalds ndisc_rcv(skb); 9141da177e4SLinus Torvalds break; 9151da177e4SLinus Torvalds 9161da177e4SLinus Torvalds case ICMPV6_MGM_QUERY: 9171da177e4SLinus Torvalds igmp6_event_query(skb); 9181da177e4SLinus Torvalds break; 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds case ICMPV6_MGM_REPORT: 9211da177e4SLinus Torvalds igmp6_event_report(skb); 9221da177e4SLinus Torvalds break; 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds case ICMPV6_MGM_REDUCTION: 9251da177e4SLinus Torvalds case ICMPV6_NI_QUERY: 9261da177e4SLinus Torvalds case ICMPV6_NI_REPLY: 9271da177e4SLinus Torvalds case ICMPV6_MLD2_REPORT: 9281da177e4SLinus Torvalds case ICMPV6_DHAAD_REQUEST: 9291da177e4SLinus Torvalds case ICMPV6_DHAAD_REPLY: 9301da177e4SLinus Torvalds case ICMPV6_MOBILE_PREFIX_SOL: 9311da177e4SLinus Torvalds case ICMPV6_MOBILE_PREFIX_ADV: 9321da177e4SLinus Torvalds break; 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds default: 9351da177e4SLinus Torvalds /* informational */ 9361da177e4SLinus Torvalds if (type & ICMPV6_INFOMSG_MASK) 9371da177e4SLinus Torvalds break; 9381da177e4SLinus Torvalds 9394b3418fbSBjørn Mork net_dbg_ratelimited("icmpv6: msg of unknown type [%pI6c > %pI6c]\n", 9404b3418fbSBjørn Mork saddr, daddr); 941ea85a0a2SDavid S. Miller 9421da177e4SLinus Torvalds /* 9431da177e4SLinus Torvalds * error of unknown type. 9441da177e4SLinus Torvalds * must pass to upper level 9451da177e4SLinus Torvalds */ 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu); 9483ff50b79SStephen Hemminger } 9493ff50b79SStephen Hemminger 950e3e32170SRick Jones /* until the v6 path can be better sorted assume failure and 951e3e32170SRick Jones * preserve the status quo behaviour for the rest of the paths to here 952e3e32170SRick Jones */ 953e3e32170SRick Jones if (success) 954e3e32170SRick Jones consume_skb(skb); 955e3e32170SRick Jones else 9561da177e4SLinus Torvalds kfree_skb(skb); 957e3e32170SRick Jones 9581da177e4SLinus Torvalds return 0; 9591da177e4SLinus Torvalds 9606a5dc9e5SEric Dumazet csum_error: 961a16292a0SEric Dumazet __ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_CSUMERRORS); 9621da177e4SLinus Torvalds discard_it: 963a16292a0SEric Dumazet __ICMP6_INC_STATS(dev_net(dev), idev, ICMP6_MIB_INERRORS); 9648b7817f3SHerbert Xu drop_no_count: 9651da177e4SLinus Torvalds kfree_skb(skb); 9661da177e4SLinus Torvalds return 0; 9671da177e4SLinus Torvalds } 9681da177e4SLinus Torvalds 9694c9483b2SDavid S. Miller void icmpv6_flow_init(struct sock *sk, struct flowi6 *fl6, 97095e41e93SYOSHIFUJI Hideaki u8 type, 97195e41e93SYOSHIFUJI Hideaki const struct in6_addr *saddr, 97295e41e93SYOSHIFUJI Hideaki const struct in6_addr *daddr, 97395e41e93SYOSHIFUJI Hideaki int oif) 97495e41e93SYOSHIFUJI Hideaki { 9754c9483b2SDavid S. Miller memset(fl6, 0, sizeof(*fl6)); 9764e3fd7a0SAlexey Dobriyan fl6->saddr = *saddr; 9774e3fd7a0SAlexey Dobriyan fl6->daddr = *daddr; 9784c9483b2SDavid S. Miller fl6->flowi6_proto = IPPROTO_ICMPV6; 9791958b856SDavid S. Miller fl6->fl6_icmp_type = type; 9801958b856SDavid S. Miller fl6->fl6_icmp_code = 0; 9814c9483b2SDavid S. Miller fl6->flowi6_oif = oif; 9824c9483b2SDavid S. Miller security_sk_classify_flow(sk, flowi6_to_flowi(fl6)); 98395e41e93SYOSHIFUJI Hideaki } 98495e41e93SYOSHIFUJI Hideaki 9853232a1efSKefeng Wang static void __net_exit icmpv6_sk_exit(struct net *net) 9863232a1efSKefeng Wang { 9873232a1efSKefeng Wang int i; 9883232a1efSKefeng Wang 9893232a1efSKefeng Wang for_each_possible_cpu(i) 99075efc250SKefeng Wang inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv6.icmp_sk, i)); 99175efc250SKefeng Wang free_percpu(net->ipv6.icmp_sk); 9923232a1efSKefeng Wang } 9933232a1efSKefeng Wang 99498c6d1b2SDenis V. Lunev static int __net_init icmpv6_sk_init(struct net *net) 9951da177e4SLinus Torvalds { 9961da177e4SLinus Torvalds struct sock *sk; 9973232a1efSKefeng Wang int err, i; 9981da177e4SLinus Torvalds 99975efc250SKefeng Wang net->ipv6.icmp_sk = alloc_percpu(struct sock *); 100063159f29SIan Morris if (!net->ipv6.icmp_sk) 100179c91159SDenis V. Lunev return -ENOMEM; 100279c91159SDenis V. Lunev 10036f912042SKAMEZAWA Hiroyuki for_each_possible_cpu(i) { 10041ed8516fSDenis V. Lunev err = inet_ctl_sock_create(&sk, PF_INET6, 10051ed8516fSDenis V. Lunev SOCK_RAW, IPPROTO_ICMPV6, net); 10061da177e4SLinus Torvalds if (err < 0) { 1007f3213831SJoe Perches pr_err("Failed to initialize the ICMP6 control socket (err %d)\n", 10081da177e4SLinus Torvalds err); 10091da177e4SLinus Torvalds goto fail; 10101da177e4SLinus Torvalds } 10111da177e4SLinus Torvalds 101275efc250SKefeng Wang *per_cpu_ptr(net->ipv6.icmp_sk, i) = sk; 10135c8cafd6SDenis V. Lunev 10141da177e4SLinus Torvalds /* Enough space for 2 64K ICMP packets, including 10151da177e4SLinus Torvalds * sk_buff struct overhead. 10161da177e4SLinus Torvalds */ 101787fb4b7bSEric Dumazet sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024); 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds return 0; 10201da177e4SLinus Torvalds 10211da177e4SLinus Torvalds fail: 10223232a1efSKefeng Wang icmpv6_sk_exit(net); 10231da177e4SLinus Torvalds return err; 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds 10268ed7edceSAlexey Dobriyan static struct pernet_operations icmpv6_sk_ops = { 102798c6d1b2SDenis V. Lunev .init = icmpv6_sk_init, 102898c6d1b2SDenis V. Lunev .exit = icmpv6_sk_exit, 102998c6d1b2SDenis V. Lunev }; 103098c6d1b2SDenis V. Lunev 103198c6d1b2SDenis V. Lunev int __init icmpv6_init(void) 103298c6d1b2SDenis V. Lunev { 103398c6d1b2SDenis V. Lunev int err; 103498c6d1b2SDenis V. Lunev 103598c6d1b2SDenis V. Lunev err = register_pernet_subsys(&icmpv6_sk_ops); 103698c6d1b2SDenis V. Lunev if (err < 0) 103798c6d1b2SDenis V. Lunev return err; 103898c6d1b2SDenis V. Lunev 103998c6d1b2SDenis V. Lunev err = -EAGAIN; 104098c6d1b2SDenis V. Lunev if (inet6_add_protocol(&icmpv6_protocol, IPPROTO_ICMPV6) < 0) 104198c6d1b2SDenis V. Lunev goto fail; 10425f5624cfSPravin B Shelar 10435f5624cfSPravin B Shelar err = inet6_register_icmp_sender(icmp6_send); 10445f5624cfSPravin B Shelar if (err) 10455f5624cfSPravin B Shelar goto sender_reg_err; 104698c6d1b2SDenis V. Lunev return 0; 104798c6d1b2SDenis V. Lunev 10485f5624cfSPravin B Shelar sender_reg_err: 10495f5624cfSPravin B Shelar inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6); 105098c6d1b2SDenis V. Lunev fail: 1051f3213831SJoe Perches pr_err("Failed to register ICMP6 protocol\n"); 105298c6d1b2SDenis V. Lunev unregister_pernet_subsys(&icmpv6_sk_ops); 105398c6d1b2SDenis V. Lunev return err; 105498c6d1b2SDenis V. Lunev } 105598c6d1b2SDenis V. Lunev 10568ed7edceSAlexey Dobriyan void icmpv6_cleanup(void) 105798c6d1b2SDenis V. Lunev { 10585f5624cfSPravin B Shelar inet6_unregister_icmp_sender(icmp6_send); 105998c6d1b2SDenis V. Lunev unregister_pernet_subsys(&icmpv6_sk_ops); 10601da177e4SLinus Torvalds inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6); 10611da177e4SLinus Torvalds } 10621da177e4SLinus Torvalds 106398c6d1b2SDenis V. Lunev 10649b5b5cffSArjan van de Ven static const struct icmp6_err { 10651da177e4SLinus Torvalds int err; 10661da177e4SLinus Torvalds int fatal; 10671da177e4SLinus Torvalds } tab_unreach[] = { 10681da177e4SLinus Torvalds { /* NOROUTE */ 10691da177e4SLinus Torvalds .err = ENETUNREACH, 10701da177e4SLinus Torvalds .fatal = 0, 10711da177e4SLinus Torvalds }, 10721da177e4SLinus Torvalds { /* ADM_PROHIBITED */ 10731da177e4SLinus Torvalds .err = EACCES, 10741da177e4SLinus Torvalds .fatal = 1, 10751da177e4SLinus Torvalds }, 10761da177e4SLinus Torvalds { /* Was NOT_NEIGHBOUR, now reserved */ 10771da177e4SLinus Torvalds .err = EHOSTUNREACH, 10781da177e4SLinus Torvalds .fatal = 0, 10791da177e4SLinus Torvalds }, 10801da177e4SLinus Torvalds { /* ADDR_UNREACH */ 10811da177e4SLinus Torvalds .err = EHOSTUNREACH, 10821da177e4SLinus Torvalds .fatal = 0, 10831da177e4SLinus Torvalds }, 10841da177e4SLinus Torvalds { /* PORT_UNREACH */ 10851da177e4SLinus Torvalds .err = ECONNREFUSED, 10861da177e4SLinus Torvalds .fatal = 1, 10871da177e4SLinus Torvalds }, 108861e76b17SJiri Bohac { /* POLICY_FAIL */ 108961e76b17SJiri Bohac .err = EACCES, 109061e76b17SJiri Bohac .fatal = 1, 109161e76b17SJiri Bohac }, 109261e76b17SJiri Bohac { /* REJECT_ROUTE */ 109361e76b17SJiri Bohac .err = EACCES, 109461e76b17SJiri Bohac .fatal = 1, 109561e76b17SJiri Bohac }, 10961da177e4SLinus Torvalds }; 10971da177e4SLinus Torvalds 1098d5fdd6baSBrian Haley int icmpv6_err_convert(u8 type, u8 code, int *err) 10991da177e4SLinus Torvalds { 11001da177e4SLinus Torvalds int fatal = 0; 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds *err = EPROTO; 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds switch (type) { 11051da177e4SLinus Torvalds case ICMPV6_DEST_UNREACH: 11061da177e4SLinus Torvalds fatal = 1; 110761e76b17SJiri Bohac if (code < ARRAY_SIZE(tab_unreach)) { 11081da177e4SLinus Torvalds *err = tab_unreach[code].err; 11091da177e4SLinus Torvalds fatal = tab_unreach[code].fatal; 11101da177e4SLinus Torvalds } 11111da177e4SLinus Torvalds break; 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds case ICMPV6_PKT_TOOBIG: 11141da177e4SLinus Torvalds *err = EMSGSIZE; 11151da177e4SLinus Torvalds break; 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds case ICMPV6_PARAMPROB: 11181da177e4SLinus Torvalds *err = EPROTO; 11191da177e4SLinus Torvalds fatal = 1; 11201da177e4SLinus Torvalds break; 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds case ICMPV6_TIME_EXCEED: 11231da177e4SLinus Torvalds *err = EHOSTUNREACH; 11241da177e4SLinus Torvalds break; 11253ff50b79SStephen Hemminger } 11261da177e4SLinus Torvalds 11271da177e4SLinus Torvalds return fatal; 11281da177e4SLinus Torvalds } 11297159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(icmpv6_err_convert); 11307159039aSYOSHIFUJI Hideaki 11311da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 1132e8243534Sstephen hemminger static struct ctl_table ipv6_icmp_table_template[] = { 11331da177e4SLinus Torvalds { 11341da177e4SLinus Torvalds .procname = "ratelimit", 113541a76906SDaniel Lezcano .data = &init_net.ipv6.sysctl.icmpv6_time, 11361da177e4SLinus Torvalds .maxlen = sizeof(int), 11371da177e4SLinus Torvalds .mode = 0644, 11386d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 11391da177e4SLinus Torvalds }, 1140e6f86b0fSVirgile Jarry { 1141e6f86b0fSVirgile Jarry .procname = "echo_ignore_all", 1142e6f86b0fSVirgile Jarry .data = &init_net.ipv6.sysctl.icmpv6_echo_ignore_all, 1143e6f86b0fSVirgile Jarry .maxlen = sizeof(int), 1144e6f86b0fSVirgile Jarry .mode = 0644, 1145e6f86b0fSVirgile Jarry .proc_handler = proc_dointvec, 1146e6f86b0fSVirgile Jarry }, 114703f1ecccSStephen Suryaputra { 114803f1ecccSStephen Suryaputra .procname = "echo_ignore_multicast", 114903f1ecccSStephen Suryaputra .data = &init_net.ipv6.sysctl.icmpv6_echo_ignore_multicast, 115003f1ecccSStephen Suryaputra .maxlen = sizeof(int), 115103f1ecccSStephen Suryaputra .mode = 0644, 115203f1ecccSStephen Suryaputra .proc_handler = proc_dointvec, 115303f1ecccSStephen Suryaputra }, 11540b03a5caSStephen Suryaputra { 11550b03a5caSStephen Suryaputra .procname = "echo_ignore_anycast", 11560b03a5caSStephen Suryaputra .data = &init_net.ipv6.sysctl.icmpv6_echo_ignore_anycast, 11570b03a5caSStephen Suryaputra .maxlen = sizeof(int), 11580b03a5caSStephen Suryaputra .mode = 0644, 11590b03a5caSStephen Suryaputra .proc_handler = proc_dointvec, 11600b03a5caSStephen Suryaputra }, 11610bc19985SStephen Suryaputra { 11620bc19985SStephen Suryaputra .procname = "ratemask", 11630bc19985SStephen Suryaputra .data = &init_net.ipv6.sysctl.icmpv6_ratemask_ptr, 11640bc19985SStephen Suryaputra .maxlen = ICMPV6_MSG_MAX + 1, 11650bc19985SStephen Suryaputra .mode = 0644, 11660bc19985SStephen Suryaputra .proc_handler = proc_do_large_bitmap, 11670bc19985SStephen Suryaputra }, 1168f8572d8fSEric W. Biederman { }, 11691da177e4SLinus Torvalds }; 1170760f2d01SDaniel Lezcano 11712c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_icmp_sysctl_init(struct net *net) 1172760f2d01SDaniel Lezcano { 1173760f2d01SDaniel Lezcano struct ctl_table *table; 1174760f2d01SDaniel Lezcano 1175760f2d01SDaniel Lezcano table = kmemdup(ipv6_icmp_table_template, 1176760f2d01SDaniel Lezcano sizeof(ipv6_icmp_table_template), 1177760f2d01SDaniel Lezcano GFP_KERNEL); 11785ee09105SYOSHIFUJI Hideaki 1179e6f86b0fSVirgile Jarry if (table) { 11805ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.icmpv6_time; 1181e6f86b0fSVirgile Jarry table[1].data = &net->ipv6.sysctl.icmpv6_echo_ignore_all; 118203f1ecccSStephen Suryaputra table[2].data = &net->ipv6.sysctl.icmpv6_echo_ignore_multicast; 11830b03a5caSStephen Suryaputra table[3].data = &net->ipv6.sysctl.icmpv6_echo_ignore_anycast; 11840bc19985SStephen Suryaputra table[4].data = &net->ipv6.sysctl.icmpv6_ratemask_ptr; 1185e6f86b0fSVirgile Jarry } 1186760f2d01SDaniel Lezcano return table; 1187760f2d01SDaniel Lezcano } 11881da177e4SLinus Torvalds #endif 1189