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>
60e4129440SAndrew Lunn #include <net/seg6.h>
611da177e4SLinus Torvalds #include <net/transp_v6.h>
621da177e4SLinus Torvalds #include <net/ip6_route.h>
631da177e4SLinus Torvalds #include <net/addrconf.h>
641da177e4SLinus Torvalds #include <net/icmp.h>
658b7817f3SHerbert Xu #include <net/xfrm.h>
661ed8516fSDenis V. Lunev #include <net/inet_common.h>
67825edac4SHannes Frederic Sowa #include <net/dsfield.h>
68ca254490SDavid Ahern #include <net/l3mdev.h>
691da177e4SLinus Torvalds
707c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
711da177e4SLinus Torvalds
726a17b961SEric Dumazet static DEFINE_PER_CPU(struct sock *, ipv6_icmp_sk);
731da177e4SLinus Torvalds
icmpv6_err(struct sk_buff * skb,struct inet6_skb_parm * opt,u8 type,u8 code,int offset,__be32 info)7432bbd879SStefano Brivio static int icmpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
756f809da2SSteffen Klassert u8 type, u8 code, int offset, __be32 info)
766f809da2SSteffen Klassert {
776d0bfe22SLorenzo Colitti /* icmpv6_notify checks 8 bytes can be pulled, icmp6hdr is 8 bytes */
786d0bfe22SLorenzo Colitti struct icmp6hdr *icmp6 = (struct icmp6hdr *) (skb->data + offset);
79*463a4559SEric Dumazet struct net *net = dev_net_rcu(skb->dev);
806f809da2SSteffen Klassert
816f809da2SSteffen Klassert if (type == ICMPV6_PKT_TOOBIG)
825f379ef5SGeorg Kohmann ip6_update_pmtu(skb, net, info, skb->dev->ifindex, 0, sock_net_uid(net, NULL));
836f809da2SSteffen Klassert else if (type == NDISC_REDIRECT)
84e2d118a1SLorenzo Colitti ip6_redirect(skb, net, skb->dev->ifindex, 0,
85e2d118a1SLorenzo Colitti sock_net_uid(net, NULL));
866d0bfe22SLorenzo Colitti
876d0bfe22SLorenzo Colitti if (!(type & ICMPV6_INFOMSG_MASK))
886d0bfe22SLorenzo Colitti if (icmp6->icmp6_type == ICMPV6_ECHO_REQUEST)
89dcb94b88SHannes Frederic Sowa ping_err(skb, offset, ntohl(info));
9032bbd879SStefano Brivio
9132bbd879SStefano Brivio return 0;
926f809da2SSteffen Klassert }
936f809da2SSteffen Klassert
94e5bbef20SHerbert Xu static int icmpv6_rcv(struct sk_buff *skb);
951da177e4SLinus Torvalds
9641135cc8SAlexey Dobriyan static const struct inet6_protocol icmpv6_protocol = {
971da177e4SLinus Torvalds .handler = icmpv6_rcv,
986f809da2SSteffen Klassert .err_handler = icmpv6_err,
998b7817f3SHerbert Xu .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1001da177e4SLinus Torvalds };
1011da177e4SLinus Torvalds
1027ba91ecbSJesper Dangaard Brouer /* Called with BH disabled */
icmpv6_xmit_lock(struct net * net)1036a17b961SEric Dumazet static struct sock *icmpv6_xmit_lock(struct net *net)
1041da177e4SLinus Torvalds {
105fdc0bde9SDenis V. Lunev struct sock *sk;
106fdc0bde9SDenis V. Lunev
1076a17b961SEric Dumazet sk = this_cpu_read(ipv6_icmp_sk);
108405666dbSDenis V. Lunev if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
1091da177e4SLinus Torvalds /* This can happen if the output path (f.e. SIT or
1101da177e4SLinus Torvalds * ip6ip6 tunnel) signals dst_link_failure() for an
1111da177e4SLinus Torvalds * outgoing ICMP6 packet.
1121da177e4SLinus Torvalds */
113fdc0bde9SDenis V. Lunev return NULL;
1141da177e4SLinus Torvalds }
1156a17b961SEric Dumazet sock_net_set(sk, net);
116fdc0bde9SDenis V. Lunev return sk;
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds
icmpv6_xmit_unlock(struct sock * sk)1196a17b961SEric Dumazet static void icmpv6_xmit_unlock(struct sock *sk)
1201da177e4SLinus Torvalds {
1216a17b961SEric Dumazet sock_net_set(sk, &init_net);
1227ba91ecbSJesper Dangaard Brouer spin_unlock(&sk->sk_lock.slock);
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds
1251da177e4SLinus Torvalds /*
1261da177e4SLinus Torvalds * Figure out, may we reply to this packet with icmp error.
1271da177e4SLinus Torvalds *
1281da177e4SLinus Torvalds * We do not reply, if:
1291da177e4SLinus Torvalds * - it was icmp error message.
1301da177e4SLinus Torvalds * - it is truncated, so that it is known, that protocol is ICMPV6
1311da177e4SLinus Torvalds * (i.e. in the middle of some exthdr)
1321da177e4SLinus Torvalds *
1331da177e4SLinus Torvalds * --ANK (980726)
1341da177e4SLinus Torvalds */
1351da177e4SLinus Torvalds
is_ineligible(const struct sk_buff * skb)136a50feda5SEric Dumazet static bool is_ineligible(const struct sk_buff *skb)
1371da177e4SLinus Torvalds {
1380660e03fSArnaldo Carvalho de Melo int ptr = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
1391da177e4SLinus Torvalds int len = skb->len - ptr;
1400660e03fSArnaldo Carvalho de Melo __u8 nexthdr = ipv6_hdr(skb)->nexthdr;
14175f2811cSJesse Gross __be16 frag_off;
1421da177e4SLinus Torvalds
1431da177e4SLinus Torvalds if (len < 0)
144a50feda5SEric Dumazet return true;
1451da177e4SLinus Torvalds
14675f2811cSJesse Gross ptr = ipv6_skip_exthdr(skb, ptr, &nexthdr, &frag_off);
1471da177e4SLinus Torvalds if (ptr < 0)
148a50feda5SEric Dumazet return false;
1491da177e4SLinus Torvalds if (nexthdr == IPPROTO_ICMPV6) {
1501da177e4SLinus Torvalds u8 _type, *tp;
1511da177e4SLinus Torvalds tp = skb_header_pointer(skb,
1521da177e4SLinus Torvalds ptr+offsetof(struct icmp6hdr, icmp6_type),
1531da177e4SLinus Torvalds sizeof(_type), &_type);
1542efdaaafSHangbin Liu
1552efdaaafSHangbin Liu /* Based on RFC 8200, Section 4.5 Fragment Header, return
1562efdaaafSHangbin Liu * false if this is a fragment packet with no icmp header info.
1572efdaaafSHangbin Liu */
1582efdaaafSHangbin Liu if (!tp && frag_off != 0)
1592efdaaafSHangbin Liu return false;
1602efdaaafSHangbin Liu else if (!tp || !(*tp & ICMPV6_INFOMSG_MASK))
161a50feda5SEric Dumazet return true;
1621da177e4SLinus Torvalds }
163a50feda5SEric Dumazet return false;
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds
icmpv6_mask_allow(struct net * net,int type)1660bc19985SStephen Suryaputra static bool icmpv6_mask_allow(struct net *net, int type)
167c0303efeSJesper Dangaard Brouer {
1680bc19985SStephen Suryaputra if (type > ICMPV6_MSG_MAX)
169c0303efeSJesper Dangaard Brouer return true;
170c0303efeSJesper Dangaard Brouer
1710bc19985SStephen Suryaputra /* Limit if icmp type is set in ratemask. */
1720bc19985SStephen Suryaputra if (!test_bit(type, net->ipv6.sysctl.icmpv6_ratemask))
173c0303efeSJesper Dangaard Brouer return true;
174c0303efeSJesper Dangaard Brouer
175c0303efeSJesper Dangaard Brouer return false;
176c0303efeSJesper Dangaard Brouer }
177c0303efeSJesper Dangaard Brouer
icmpv6_global_allow(struct net * net,int type,bool * apply_ratelimit)178662ec522SEric Dumazet static bool icmpv6_global_allow(struct net *net, int type,
179662ec522SEric Dumazet bool *apply_ratelimit)
180c0303efeSJesper Dangaard Brouer {
1810bc19985SStephen Suryaputra if (icmpv6_mask_allow(net, type))
182c0303efeSJesper Dangaard Brouer return true;
183c0303efeSJesper Dangaard Brouer
184662ec522SEric Dumazet if (icmp_global_allow()) {
185662ec522SEric Dumazet *apply_ratelimit = true;
186c0303efeSJesper Dangaard Brouer return true;
187662ec522SEric Dumazet }
188d0941130SJamie Bainbridge __ICMP_INC_STATS(net, ICMP_MIB_RATELIMITGLOBAL);
189c0303efeSJesper Dangaard Brouer return false;
190c0303efeSJesper Dangaard Brouer }
191c0303efeSJesper Dangaard Brouer
1921da177e4SLinus Torvalds /*
1931da177e4SLinus Torvalds * Check the ICMP output rate limit
1941da177e4SLinus Torvalds */
icmpv6_xrlim_allow(struct sock * sk,u8 type,struct flowi6 * fl6,bool apply_ratelimit)1954cdf507dSEric Dumazet static bool icmpv6_xrlim_allow(struct sock *sk, u8 type,
196662ec522SEric Dumazet struct flowi6 *fl6, bool apply_ratelimit)
1971da177e4SLinus Torvalds {
1983b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk);
1994cdf507dSEric Dumazet struct dst_entry *dst;
20092d86829SDavid S. Miller bool res = false;
2011da177e4SLinus Torvalds
202662ec522SEric Dumazet if (!apply_ratelimit)
20392d86829SDavid S. Miller return true;
2041da177e4SLinus Torvalds
2051da177e4SLinus Torvalds /*
2061da177e4SLinus Torvalds * Look up the output route.
2071da177e4SLinus Torvalds * XXX: perhaps the expire for routing entries cloned by
2081da177e4SLinus Torvalds * this lookup should be more aggressive (not longer than timeout).
2091da177e4SLinus Torvalds */
2104c9483b2SDavid S. Miller dst = ip6_route_output(net, sk, fl6);
2111da177e4SLinus Torvalds if (dst->error) {
2123bd653c8SDenis V. Lunev IP6_INC_STATS(net, ip6_dst_idev(dst),
213a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_OUTNOROUTES);
2141da177e4SLinus Torvalds } else if (dst->dev && (dst->dev->flags&IFF_LOOPBACK)) {
21592d86829SDavid S. Miller res = true;
2161da177e4SLinus Torvalds } else {
217797a4c1fSEric Dumazet struct rt6_info *rt = dst_rt6_info(dst);
2189a43b709SBenjamin Thery int tmo = net->ipv6.sysctl.icmpv6_time;
219c0303efeSJesper Dangaard Brouer struct inet_peer *peer;
2201da177e4SLinus Torvalds
2211da177e4SLinus Torvalds /* Give more bandwidth to wider prefixes. */
2221da177e4SLinus Torvalds if (rt->rt6i_dst.plen < 128)
2231da177e4SLinus Torvalds tmo >>= ((128 - rt->rt6i_dst.plen)>>5);
2241da177e4SLinus Torvalds
225280fb099SEric Dumazet rcu_read_lock();
226fdaa6b3cSEric Dumazet peer = inet_getpeer_v6(net->ipv6.peers, &fl6->daddr);
227fbfe95a4SDavid S. Miller res = inet_peer_xrlim_allow(peer, tmo);
228280fb099SEric Dumazet rcu_read_unlock();
2291da177e4SLinus Torvalds }
230d0941130SJamie Bainbridge if (!res)
231d0941130SJamie Bainbridge __ICMP6_INC_STATS(net, ip6_dst_idev(dst),
232d0941130SJamie Bainbridge ICMP6_MIB_RATELIMITHOST);
233662ec522SEric Dumazet else
234662ec522SEric Dumazet icmp_global_consume();
2351da177e4SLinus Torvalds dst_release(dst);
2361da177e4SLinus Torvalds return res;
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds
icmpv6_rt_has_prefsrc(struct sock * sk,u8 type,struct flowi6 * fl6)239b93cfb9cSTim Stallard static bool icmpv6_rt_has_prefsrc(struct sock *sk, u8 type,
240b93cfb9cSTim Stallard struct flowi6 *fl6)
241b93cfb9cSTim Stallard {
242b93cfb9cSTim Stallard struct net *net = sock_net(sk);
243b93cfb9cSTim Stallard struct dst_entry *dst;
244b93cfb9cSTim Stallard bool res = false;
245b93cfb9cSTim Stallard
246b93cfb9cSTim Stallard dst = ip6_route_output(net, sk, fl6);
247b93cfb9cSTim Stallard if (!dst->error) {
248797a4c1fSEric Dumazet struct rt6_info *rt = dst_rt6_info(dst);
249b93cfb9cSTim Stallard struct in6_addr prefsrc;
250b93cfb9cSTim Stallard
251b93cfb9cSTim Stallard rt6_get_prefsrc(rt, &prefsrc);
252b93cfb9cSTim Stallard res = !ipv6_addr_any(&prefsrc);
253b93cfb9cSTim Stallard }
254b93cfb9cSTim Stallard dst_release(dst);
255b93cfb9cSTim Stallard return res;
256b93cfb9cSTim Stallard }
257b93cfb9cSTim Stallard
2581da177e4SLinus Torvalds /*
2591da177e4SLinus Torvalds * an inline helper for the "simple" if statement below
2601da177e4SLinus Torvalds * checks if parameter problem report is caused by an
2611da177e4SLinus Torvalds * unrecognized IPv6 option that has the Option Type
2621da177e4SLinus Torvalds * highest-order two bits set to 10
2631da177e4SLinus Torvalds */
2641da177e4SLinus Torvalds
opt_unrec(struct sk_buff * skb,__u32 offset)265a50feda5SEric Dumazet static bool opt_unrec(struct sk_buff *skb, __u32 offset)
2661da177e4SLinus Torvalds {
2671da177e4SLinus Torvalds u8 _optval, *op;
2681da177e4SLinus Torvalds
269bbe735e4SArnaldo Carvalho de Melo offset += skb_network_offset(skb);
2701da177e4SLinus Torvalds op = skb_header_pointer(skb, offset, sizeof(_optval), &_optval);
27163159f29SIan Morris if (!op)
272a50feda5SEric Dumazet return true;
2731da177e4SLinus Torvalds return (*op & 0xC0) == 0x80;
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds
icmpv6_push_pending_frames(struct sock * sk,struct flowi6 * fl6,struct icmp6hdr * thdr,int len)2764e64b1edSJoe Perches void icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
2776d0bfe22SLorenzo Colitti struct icmp6hdr *thdr, int len)
2781da177e4SLinus Torvalds {
2791da177e4SLinus Torvalds struct sk_buff *skb;
2801da177e4SLinus Torvalds struct icmp6hdr *icmp6h;
2811da177e4SLinus Torvalds
282e5d08d71SIan Morris skb = skb_peek(&sk->sk_write_queue);
28363159f29SIan Morris if (!skb)
2844e64b1edSJoe Perches return;
2851da177e4SLinus Torvalds
286cc70ab26SArnaldo Carvalho de Melo icmp6h = icmp6_hdr(skb);
2871da177e4SLinus Torvalds memcpy(icmp6h, thdr, sizeof(struct icmp6hdr));
2881da177e4SLinus Torvalds icmp6h->icmp6_cksum = 0;
2891da177e4SLinus Torvalds
2901da177e4SLinus Torvalds if (skb_queue_len(&sk->sk_write_queue) == 1) {
29107f0757aSJoe Perches skb->csum = csum_partial(icmp6h,
2921da177e4SLinus Torvalds sizeof(struct icmp6hdr), skb->csum);
2934c9483b2SDavid S. Miller icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr,
2944c9483b2SDavid S. Miller &fl6->daddr,
2954c9483b2SDavid S. Miller len, fl6->flowi6_proto,
2961da177e4SLinus Torvalds skb->csum);
2971da177e4SLinus Torvalds } else {
298868c86bcSAl Viro __wsum tmp_csum = 0;
2991da177e4SLinus Torvalds
3001da177e4SLinus Torvalds skb_queue_walk(&sk->sk_write_queue, skb) {
3011da177e4SLinus Torvalds tmp_csum = csum_add(tmp_csum, skb->csum);
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds
30407f0757aSJoe Perches tmp_csum = csum_partial(icmp6h,
3051da177e4SLinus Torvalds sizeof(struct icmp6hdr), tmp_csum);
3064c9483b2SDavid S. Miller icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr,
3074c9483b2SDavid S. Miller &fl6->daddr,
3084c9483b2SDavid S. Miller len, fl6->flowi6_proto,
309868c86bcSAl Viro tmp_csum);
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds ip6_push_pending_frames(sk);
3121da177e4SLinus Torvalds }
3131da177e4SLinus Torvalds
3141da177e4SLinus Torvalds struct icmpv6_msg {
3151da177e4SLinus Torvalds struct sk_buff *skb;
3161da177e4SLinus Torvalds int offset;
317763ecff1SYasuyuki Kozakai uint8_t type;
3181da177e4SLinus Torvalds };
3191da177e4SLinus Torvalds
icmpv6_getfrag(void * from,char * to,int offset,int len,int odd,struct sk_buff * skb)3201da177e4SLinus Torvalds static int icmpv6_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
3211da177e4SLinus Torvalds {
3221da177e4SLinus Torvalds struct icmpv6_msg *msg = (struct icmpv6_msg *) from;
3231da177e4SLinus Torvalds struct sk_buff *org_skb = msg->skb;
3248d5930dfSAl Viro __wsum csum;
3251da177e4SLinus Torvalds
3261da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(org_skb, msg->offset + offset,
3278d5930dfSAl Viro to, len);
3281da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, odd);
329763ecff1SYasuyuki Kozakai if (!(msg->type & ICMPV6_INFOMSG_MASK))
330763ecff1SYasuyuki Kozakai nf_ct_attach(skb, org_skb);
3311da177e4SLinus Torvalds return 0;
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds
33407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
mip6_addr_swap(struct sk_buff * skb,const struct inet6_skb_parm * opt)335ee576c47SJason A. Donenfeld static void mip6_addr_swap(struct sk_buff *skb, const struct inet6_skb_parm *opt)
33679383236SMasahide NAKAMURA {
3370660e03fSArnaldo Carvalho de Melo struct ipv6hdr *iph = ipv6_hdr(skb);
33879383236SMasahide NAKAMURA struct ipv6_destopt_hao *hao;
33979383236SMasahide NAKAMURA int off;
34079383236SMasahide NAKAMURA
34179383236SMasahide NAKAMURA if (opt->dsthao) {
34279383236SMasahide NAKAMURA off = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
34379383236SMasahide NAKAMURA if (likely(off >= 0)) {
344d56f90a7SArnaldo Carvalho de Melo hao = (struct ipv6_destopt_hao *)
345d56f90a7SArnaldo Carvalho de Melo (skb_network_header(skb) + off);
346bc617613SJiapeng Chong swap(iph->saddr, hao->addr);
34779383236SMasahide NAKAMURA }
34879383236SMasahide NAKAMURA }
34979383236SMasahide NAKAMURA }
35079383236SMasahide NAKAMURA #else
mip6_addr_swap(struct sk_buff * skb,const struct inet6_skb_parm * opt)351ee576c47SJason A. Donenfeld static inline void mip6_addr_swap(struct sk_buff *skb, const struct inet6_skb_parm *opt) {}
35279383236SMasahide NAKAMURA #endif
35379383236SMasahide NAKAMURA
icmpv6_route_lookup(struct net * net,struct sk_buff * skb,struct sock * sk,struct flowi6 * fl6)354e8243534Sstephen hemminger static struct dst_entry *icmpv6_route_lookup(struct net *net,
355e8243534Sstephen hemminger struct sk_buff *skb,
356e8243534Sstephen hemminger struct sock *sk,
357e8243534Sstephen hemminger struct flowi6 *fl6)
358b42835dbSDavid S. Miller {
359b42835dbSDavid S. Miller struct dst_entry *dst, *dst2;
3604c9483b2SDavid S. Miller struct flowi6 fl2;
361b42835dbSDavid S. Miller int err;
362b42835dbSDavid S. Miller
363343d60aaSRoopa Prabhu err = ip6_dst_lookup(net, sk, &dst, fl6);
364b42835dbSDavid S. Miller if (err)
365b42835dbSDavid S. Miller return ERR_PTR(err);
366b42835dbSDavid S. Miller
367b42835dbSDavid S. Miller /*
368b42835dbSDavid S. Miller * We won't send icmp if the destination is known
3697ab75456SMahesh Bandewar * anycast unless we need to treat anycast as unicast.
370b42835dbSDavid S. Miller */
3717ab75456SMahesh Bandewar if (!READ_ONCE(net->ipv6.sysctl.icmpv6_error_anycast_as_unicast) &&
3727ab75456SMahesh Bandewar ipv6_anycast_destination(dst, &fl6->daddr)) {
373ba7a46f1SJoe Perches net_dbg_ratelimited("icmp6_send: acast source\n");
374b42835dbSDavid S. Miller dst_release(dst);
375b42835dbSDavid S. Miller return ERR_PTR(-EINVAL);
376b42835dbSDavid S. Miller }
377b42835dbSDavid S. Miller
378b42835dbSDavid S. Miller /* No need to clone since we're just using its address. */
379b42835dbSDavid S. Miller dst2 = dst;
380b42835dbSDavid S. Miller
3814c9483b2SDavid S. Miller dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), sk, 0);
382452edd59SDavid S. Miller if (!IS_ERR(dst)) {
383b42835dbSDavid S. Miller if (dst != dst2)
384b42835dbSDavid S. Miller return dst;
385452edd59SDavid S. Miller } else {
386452edd59SDavid S. Miller if (PTR_ERR(dst) == -EPERM)
387b42835dbSDavid S. Miller dst = NULL;
388452edd59SDavid S. Miller else
389452edd59SDavid S. Miller return dst;
390b42835dbSDavid S. Miller }
391b42835dbSDavid S. Miller
3924c9483b2SDavid S. Miller err = xfrm_decode_session_reverse(skb, flowi6_to_flowi(&fl2), AF_INET6);
393b42835dbSDavid S. Miller if (err)
394b42835dbSDavid S. Miller goto relookup_failed;
395b42835dbSDavid S. Miller
396343d60aaSRoopa Prabhu err = ip6_dst_lookup(net, sk, &dst2, &fl2);
397b42835dbSDavid S. Miller if (err)
398b42835dbSDavid S. Miller goto relookup_failed;
399b42835dbSDavid S. Miller
4004c9483b2SDavid S. Miller dst2 = xfrm_lookup(net, dst2, flowi6_to_flowi(&fl2), sk, XFRM_LOOKUP_ICMP);
401452edd59SDavid S. Miller if (!IS_ERR(dst2)) {
402b42835dbSDavid S. Miller dst_release(dst);
403b42835dbSDavid S. Miller dst = dst2;
404452edd59SDavid S. Miller } else {
405452edd59SDavid S. Miller err = PTR_ERR(dst2);
406452edd59SDavid S. Miller if (err == -EPERM) {
407b42835dbSDavid S. Miller dst_release(dst);
408452edd59SDavid S. Miller return dst2;
409452edd59SDavid S. Miller } else
410b42835dbSDavid S. Miller goto relookup_failed;
411b42835dbSDavid S. Miller }
412b42835dbSDavid S. Miller
413b42835dbSDavid S. Miller relookup_failed:
414b42835dbSDavid S. Miller if (dst)
415b42835dbSDavid S. Miller return dst;
416b42835dbSDavid S. Miller return ERR_PTR(err);
417b42835dbSDavid S. Miller }
418b42835dbSDavid S. Miller
icmp6_dev(const struct sk_buff * skb)419e1ae5c2eSStephen Suryaputra static struct net_device *icmp6_dev(const struct sk_buff *skb)
4201b70d792SDavid Ahern {
421e1ae5c2eSStephen Suryaputra struct net_device *dev = skb->dev;
4221b70d792SDavid Ahern
4231b70d792SDavid Ahern /* for local traffic to local address, skb dev is the loopback
4241b70d792SDavid Ahern * device. Check if there is a dst attached to the skb and if so
42524b711edSDavid Ahern * get the real device index. Same is needed for replies to a link
42624b711edSDavid Ahern * local address on a device enslaved to an L3 master device
4271b70d792SDavid Ahern */
428e1ae5c2eSStephen Suryaputra if (unlikely(dev->ifindex == LOOPBACK_IFINDEX || netif_is_l3_master(skb->dev))) {
4291b70d792SDavid Ahern const struct rt6_info *rt6 = skb_rt6_info(skb);
4301b70d792SDavid Ahern
4312aaa8a15SKuniyuki Iwashima /* The destination could be an external IP in Ext Hdr (SRv6, RPL, etc.),
4322aaa8a15SKuniyuki Iwashima * and ip6_null_entry could be set to skb if no route is found.
4332aaa8a15SKuniyuki Iwashima */
4342aaa8a15SKuniyuki Iwashima if (rt6 && rt6->rt6i_idev)
435e1ae5c2eSStephen Suryaputra dev = rt6->rt6i_idev->dev;
4361b70d792SDavid Ahern }
4371b70d792SDavid Ahern
438e1ae5c2eSStephen Suryaputra return dev;
439e1ae5c2eSStephen Suryaputra }
440e1ae5c2eSStephen Suryaputra
icmp6_iif(const struct sk_buff * skb)441e1ae5c2eSStephen Suryaputra static int icmp6_iif(const struct sk_buff *skb)
442e1ae5c2eSStephen Suryaputra {
443e1ae5c2eSStephen Suryaputra return icmp6_dev(skb)->ifindex;
4441b70d792SDavid Ahern }
4451b70d792SDavid Ahern
4461da177e4SLinus Torvalds /*
4471da177e4SLinus Torvalds * Send an ICMP message in response to a packet in error
4481da177e4SLinus Torvalds */
icmp6_send(struct sk_buff * skb,u8 type,u8 code,__u32 info,const struct in6_addr * force_saddr,const struct inet6_skb_parm * parm)449cc7a21b6SEric Dumazet void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info,
450ee576c47SJason A. Donenfeld const struct in6_addr *force_saddr,
451ee576c47SJason A. Donenfeld const struct inet6_skb_parm *parm)
4521da177e4SLinus Torvalds {
4531da177e4SLinus Torvalds struct inet6_dev *idev = NULL;
4540660e03fSArnaldo Carvalho de Melo struct ipv6hdr *hdr = ipv6_hdr(skb);
45584427d53SYOSHIFUJI Hideaki struct sock *sk;
4568d933670SEric Dumazet struct net *net;
45784427d53SYOSHIFUJI Hideaki struct ipv6_pinfo *np;
458b71d1d42SEric Dumazet const struct in6_addr *saddr = NULL;
459662ec522SEric Dumazet bool apply_ratelimit = false;
4601da177e4SLinus Torvalds struct dst_entry *dst;
4611da177e4SLinus Torvalds struct icmp6hdr tmp_hdr;
4624c9483b2SDavid S. Miller struct flowi6 fl6;
4631da177e4SLinus Torvalds struct icmpv6_msg msg;
46426879da5SWei Wang struct ipcm6_cookie ipc6;
4651da177e4SLinus Torvalds int iif = 0;
4661da177e4SLinus Torvalds int addr_type = 0;
4671da177e4SLinus Torvalds int len;
4688d933670SEric Dumazet u32 mark;
4691da177e4SLinus Torvalds
47027a884dcSArnaldo Carvalho de Melo if ((u8 *)hdr < skb->head ||
47129a3cad5SSimon Horman (skb_network_header(skb) + sizeof(*hdr)) > skb_tail_pointer(skb))
4721da177e4SLinus Torvalds return;
4731da177e4SLinus Torvalds
4748d933670SEric Dumazet if (!skb->dev)
4758d933670SEric Dumazet return;
476*463a4559SEric Dumazet
477*463a4559SEric Dumazet rcu_read_lock();
478*463a4559SEric Dumazet
479*463a4559SEric Dumazet net = dev_net_rcu(skb->dev);
4808d933670SEric Dumazet mark = IP6_REPLY_MARK(net, skb->mark);
4811da177e4SLinus Torvalds /*
4821da177e4SLinus Torvalds * Make sure we respect the rules
4831da177e4SLinus Torvalds * i.e. RFC 1885 2.4(e)
4845f5624cfSPravin B Shelar * Rule (e.1) is enforced by not using icmp6_send
4851da177e4SLinus Torvalds * in any code that processes icmp errors.
4861da177e4SLinus Torvalds */
4871da177e4SLinus Torvalds addr_type = ipv6_addr_type(&hdr->daddr);
4881da177e4SLinus Torvalds
489446fab59SFX Le Bail if (ipv6_chk_addr(net, &hdr->daddr, skb->dev, 0) ||
490d94c1f92SFX Le Bail ipv6_chk_acast_addr_src(net, skb->dev, &hdr->daddr))
4911da177e4SLinus Torvalds saddr = &hdr->daddr;
4921da177e4SLinus Torvalds
4931da177e4SLinus Torvalds /*
4941da177e4SLinus Torvalds * Dest addr check
4951da177e4SLinus Torvalds */
4961da177e4SLinus Torvalds
4979a6b4b39Szhuyj if (addr_type & IPV6_ADDR_MULTICAST || skb->pkt_type != PACKET_HOST) {
4981da177e4SLinus Torvalds if (type != ICMPV6_PKT_TOOBIG &&
4991da177e4SLinus Torvalds !(type == ICMPV6_PARAMPROB &&
5001da177e4SLinus Torvalds code == ICMPV6_UNK_OPTION &&
5011da177e4SLinus Torvalds (opt_unrec(skb, info))))
502*463a4559SEric Dumazet goto out;
5031da177e4SLinus Torvalds
5041da177e4SLinus Torvalds saddr = NULL;
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds
5071da177e4SLinus Torvalds addr_type = ipv6_addr_type(&hdr->saddr);
5081da177e4SLinus Torvalds
5091da177e4SLinus Torvalds /*
5101da177e4SLinus Torvalds * Source addr check
5111da177e4SLinus Torvalds */
5121da177e4SLinus Torvalds
5134832c30dSDavid Ahern if (__ipv6_addr_needs_scope_id(addr_type)) {
5141b70d792SDavid Ahern iif = icmp6_iif(skb);
5154832c30dSDavid Ahern } else {
516272928d1SMathieu Desnoyers /*
517272928d1SMathieu Desnoyers * The source device is used for looking up which routing table
518272928d1SMathieu Desnoyers * to use for sending an ICMP error.
519272928d1SMathieu Desnoyers */
520272928d1SMathieu Desnoyers iif = l3mdev_master_ifindex(skb->dev);
52179dc7e3fSDavid Ahern }
5221da177e4SLinus Torvalds
5231da177e4SLinus Torvalds /*
5248de3351eSYOSHIFUJI Hideaki * Must not send error if the source does not uniquely
5258de3351eSYOSHIFUJI Hideaki * identify a single node (RFC2463 Section 2.4).
5268de3351eSYOSHIFUJI Hideaki * We check unspecified / multicast addresses here,
5278de3351eSYOSHIFUJI Hideaki * and anycast addresses will be checked later.
5281da177e4SLinus Torvalds */
5291da177e4SLinus Torvalds if ((addr_type == IPV6_ADDR_ANY) || (addr_type & IPV6_ADDR_MULTICAST)) {
5304b3418fbSBjørn Mork net_dbg_ratelimited("icmp6_send: addr_any/mcast source [%pI6c > %pI6c]\n",
5314b3418fbSBjørn Mork &hdr->saddr, &hdr->daddr);
532*463a4559SEric Dumazet goto out;
5331da177e4SLinus Torvalds }
5341da177e4SLinus Torvalds
5351da177e4SLinus Torvalds /*
5361da177e4SLinus Torvalds * Never answer to a ICMP packet.
5371da177e4SLinus Torvalds */
5381da177e4SLinus Torvalds if (is_ineligible(skb)) {
5394b3418fbSBjørn Mork net_dbg_ratelimited("icmp6_send: no reply to icmp error [%pI6c > %pI6c]\n",
5404b3418fbSBjørn Mork &hdr->saddr, &hdr->daddr);
541*463a4559SEric Dumazet goto out;
5421da177e4SLinus Torvalds }
5431da177e4SLinus Torvalds
544662ec522SEric Dumazet /* Needed by both icmpv6_global_allow and icmpv6_xmit_lock */
5457ba91ecbSJesper Dangaard Brouer local_bh_disable();
5467ba91ecbSJesper Dangaard Brouer
5477ba91ecbSJesper Dangaard Brouer /* Check global sysctl_icmp_msgs_per_sec ratelimit */
548662ec522SEric Dumazet if (!(skb->dev->flags & IFF_LOOPBACK) &&
549662ec522SEric Dumazet !icmpv6_global_allow(net, type, &apply_ratelimit))
5507ba91ecbSJesper Dangaard Brouer goto out_bh_enable;
5517ba91ecbSJesper Dangaard Brouer
552ee576c47SJason A. Donenfeld mip6_addr_swap(skb, parm);
55379383236SMasahide NAKAMURA
554fac6fce9SFrancesco Ruggeri sk = icmpv6_xmit_lock(net);
555fac6fce9SFrancesco Ruggeri if (!sk)
556fac6fce9SFrancesco Ruggeri goto out_bh_enable;
557fac6fce9SFrancesco Ruggeri
5584c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6));
5594c9483b2SDavid S. Miller fl6.flowi6_proto = IPPROTO_ICMPV6;
5604e3fd7a0SAlexey Dobriyan fl6.daddr = hdr->saddr;
561b1cadc1aSEric Dumazet if (force_saddr)
562b1cadc1aSEric Dumazet saddr = force_saddr;
563fac6fce9SFrancesco Ruggeri if (saddr) {
5644e3fd7a0SAlexey Dobriyan fl6.saddr = *saddr;
565b93cfb9cSTim Stallard } else if (!icmpv6_rt_has_prefsrc(sk, type, &fl6)) {
566fac6fce9SFrancesco Ruggeri /* select a more meaningful saddr from input if */
567fac6fce9SFrancesco Ruggeri struct net_device *in_netdev;
568fac6fce9SFrancesco Ruggeri
569ee576c47SJason A. Donenfeld in_netdev = dev_get_by_index(net, parm->iif);
570fac6fce9SFrancesco Ruggeri if (in_netdev) {
571fac6fce9SFrancesco Ruggeri ipv6_dev_get_saddr(net, in_netdev, &fl6.daddr,
572fac6fce9SFrancesco Ruggeri inet6_sk(sk)->srcprefs,
573fac6fce9SFrancesco Ruggeri &fl6.saddr);
574fac6fce9SFrancesco Ruggeri dev_put(in_netdev);
575fac6fce9SFrancesco Ruggeri }
576fac6fce9SFrancesco Ruggeri }
577e110861fSLorenzo Colitti fl6.flowi6_mark = mark;
5784c9483b2SDavid S. Miller fl6.flowi6_oif = iif;
5791958b856SDavid S. Miller fl6.fl6_icmp_type = type;
5801958b856SDavid S. Miller fl6.fl6_icmp_code = code;
581e2d118a1SLorenzo Colitti fl6.flowi6_uid = sock_net_uid(net, NULL);
582b4bac172SDavid Ahern fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, NULL);
5833df98d79SPaul Moore security_skb_classify_flow(skb, flowi6_to_flowi_common(&fl6));
5841da177e4SLinus Torvalds
585fdc0bde9SDenis V. Lunev np = inet6_sk(sk);
586405666dbSDenis V. Lunev
587662ec522SEric Dumazet if (!icmpv6_xrlim_allow(sk, type, &fl6, apply_ratelimit))
588*463a4559SEric Dumazet goto out_unlock;
5891da177e4SLinus Torvalds
5901da177e4SLinus Torvalds tmp_hdr.icmp6_type = type;
5911da177e4SLinus Torvalds tmp_hdr.icmp6_code = code;
5921da177e4SLinus Torvalds tmp_hdr.icmp6_cksum = 0;
5931da177e4SLinus Torvalds tmp_hdr.icmp6_pointer = htonl(info);
5941da177e4SLinus Torvalds
5954c9483b2SDavid S. Miller if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
5964c9483b2SDavid S. Miller fl6.flowi6_oif = np->mcast_oif;
597c4062dfcSErich E. Hoover else if (!fl6.flowi6_oif)
598c4062dfcSErich E. Hoover fl6.flowi6_oif = np->ucast_oif;
5991da177e4SLinus Torvalds
600b515430aSWillem de Bruijn ipcm6_init_sk(&ipc6, np);
6010da7536fSWillem de Bruijn ipc6.sockc.mark = mark;
60238b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
60338b7097bSHannes Frederic Sowa
6044c9483b2SDavid S. Miller dst = icmpv6_route_lookup(net, skb, sk, &fl6);
605b42835dbSDavid S. Miller if (IS_ERR(dst))
606*463a4559SEric Dumazet goto out_unlock;
6078de3351eSYOSHIFUJI Hideaki
60826879da5SWei Wang ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
6091da177e4SLinus Torvalds
6101da177e4SLinus Torvalds msg.skb = skb;
611bbe735e4SArnaldo Carvalho de Melo msg.offset = skb_network_offset(skb);
612763ecff1SYasuyuki Kozakai msg.type = type;
6131da177e4SLinus Torvalds
6141da177e4SLinus Torvalds len = skb->len - msg.offset;
6151da177e4SLinus Torvalds len = min_t(unsigned int, len, IPV6_MIN_MTU - sizeof(struct ipv6hdr) - sizeof(struct icmp6hdr));
6161da177e4SLinus Torvalds if (len < 0) {
6174b3418fbSBjørn Mork net_dbg_ratelimited("icmp: len problem [%pI6c > %pI6c]\n",
6184b3418fbSBjørn Mork &hdr->saddr, &hdr->daddr);
6191da177e4SLinus Torvalds goto out_dst_release;
6201da177e4SLinus Torvalds }
6211da177e4SLinus Torvalds
622cfdf7647SEric Dumazet idev = __in6_dev_get(skb->dev);
6231da177e4SLinus Torvalds
6244e64b1edSJoe Perches if (ip6_append_data(sk, icmpv6_getfrag, &msg,
6251da177e4SLinus Torvalds len + sizeof(struct icmp6hdr),
62626879da5SWei Wang sizeof(struct icmp6hdr),
627797a4c1fSEric Dumazet &ipc6, &fl6, dst_rt6_info(dst),
6285fdaa88dSWillem de Bruijn MSG_DONTWAIT)) {
62943a43b60SHannes Frederic Sowa ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS);
6301da177e4SLinus Torvalds ip6_flush_pending_frames(sk);
631cfdf7647SEric Dumazet } else {
6324e64b1edSJoe Perches icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr,
633cfdf7647SEric Dumazet len + sizeof(struct icmp6hdr));
6341da177e4SLinus Torvalds }
635*463a4559SEric Dumazet
6361da177e4SLinus Torvalds out_dst_release:
6371da177e4SLinus Torvalds dst_release(dst);
638*463a4559SEric Dumazet out_unlock:
639405666dbSDenis V. Lunev icmpv6_xmit_unlock(sk);
6407ba91ecbSJesper Dangaard Brouer out_bh_enable:
6417ba91ecbSJesper Dangaard Brouer local_bh_enable();
642*463a4559SEric Dumazet out:
643*463a4559SEric Dumazet rcu_read_unlock();
6441da177e4SLinus Torvalds }
645cc7a21b6SEric Dumazet EXPORT_SYMBOL(icmp6_send);
6465f5624cfSPravin B Shelar
6471ad6d548SMenglong Dong /* Slightly more convenient version of icmp6_send with drop reasons.
6485f5624cfSPravin B Shelar */
icmpv6_param_prob_reason(struct sk_buff * skb,u8 code,int pos,enum skb_drop_reason reason)6491ad6d548SMenglong Dong void icmpv6_param_prob_reason(struct sk_buff *skb, u8 code, int pos,
6501ad6d548SMenglong Dong enum skb_drop_reason reason)
6515f5624cfSPravin B Shelar {
652ee576c47SJason A. Donenfeld icmp6_send(skb, ICMPV6_PARAMPROB, code, pos, NULL, IP6CB(skb));
6531ad6d548SMenglong Dong kfree_skb_reason(skb, reason);
6545f5624cfSPravin B Shelar }
6557159039aSYOSHIFUJI Hideaki
6565fbba8acSEric Dumazet /* Generate icmpv6 with type/code ICMPV6_DEST_UNREACH/ICMPV6_ADDR_UNREACH
6575fbba8acSEric Dumazet * if sufficient data bytes are available
6585fbba8acSEric Dumazet * @nhs is the size of the tunnel header(s) :
6595fbba8acSEric Dumazet * Either an IPv4 header for SIT encap
6605fbba8acSEric Dumazet * an IPv4 header + GRE header for GRE encap
6615fbba8acSEric Dumazet */
ip6_err_gen_icmpv6_unreach(struct sk_buff * skb,int nhs,int type,unsigned int data_len)66220e1954fSEric Dumazet int ip6_err_gen_icmpv6_unreach(struct sk_buff *skb, int nhs, int type,
66320e1954fSEric Dumazet unsigned int data_len)
6645fbba8acSEric Dumazet {
6652d7a3b27SEric Dumazet struct in6_addr temp_saddr;
6665fbba8acSEric Dumazet struct rt6_info *rt;
6675fbba8acSEric Dumazet struct sk_buff *skb2;
66820e1954fSEric Dumazet u32 info = 0;
6695fbba8acSEric Dumazet
6705fbba8acSEric Dumazet if (!pskb_may_pull(skb, nhs + sizeof(struct ipv6hdr) + 8))
6715fbba8acSEric Dumazet return 1;
6725fbba8acSEric Dumazet
67320e1954fSEric Dumazet /* RFC 4884 (partial) support for ICMP extensions */
67420e1954fSEric Dumazet if (data_len < 128 || (data_len & 7) || skb->len < data_len)
67520e1954fSEric Dumazet data_len = 0;
67620e1954fSEric Dumazet
67720e1954fSEric Dumazet skb2 = data_len ? skb_copy(skb, GFP_ATOMIC) : skb_clone(skb, GFP_ATOMIC);
6785fbba8acSEric Dumazet
6795fbba8acSEric Dumazet if (!skb2)
6805fbba8acSEric Dumazet return 1;
6815fbba8acSEric Dumazet
6825fbba8acSEric Dumazet skb_dst_drop(skb2);
6835fbba8acSEric Dumazet skb_pull(skb2, nhs);
6845fbba8acSEric Dumazet skb_reset_network_header(skb2);
6855fbba8acSEric Dumazet
686*463a4559SEric Dumazet rt = rt6_lookup(dev_net_rcu(skb->dev), &ipv6_hdr(skb2)->saddr,
687*463a4559SEric Dumazet NULL, 0, skb, 0);
6885fbba8acSEric Dumazet
6895fbba8acSEric Dumazet if (rt && rt->dst.dev)
6905fbba8acSEric Dumazet skb2->dev = rt->dst.dev;
6915fbba8acSEric Dumazet
6922d7a3b27SEric Dumazet ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, &temp_saddr);
69320e1954fSEric Dumazet
69420e1954fSEric Dumazet if (data_len) {
69520e1954fSEric Dumazet /* RFC 4884 (partial) support :
69620e1954fSEric Dumazet * insert 0 padding at the end, before the extensions
69720e1954fSEric Dumazet */
69820e1954fSEric Dumazet __skb_push(skb2, nhs);
69920e1954fSEric Dumazet skb_reset_network_header(skb2);
70020e1954fSEric Dumazet memmove(skb2->data, skb2->data + nhs, data_len - nhs);
70120e1954fSEric Dumazet memset(skb2->data + data_len - nhs, 0, nhs);
70220e1954fSEric Dumazet /* RFC 4884 4.5 : Length is measured in 64-bit words,
70320e1954fSEric Dumazet * and stored in reserved[0]
70420e1954fSEric Dumazet */
70520e1954fSEric Dumazet info = (data_len/8) << 24;
70620e1954fSEric Dumazet }
7072d7a3b27SEric Dumazet if (type == ICMP_TIME_EXCEEDED)
7082d7a3b27SEric Dumazet icmp6_send(skb2, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
709ee576c47SJason A. Donenfeld info, &temp_saddr, IP6CB(skb2));
7102d7a3b27SEric Dumazet else
7112d7a3b27SEric Dumazet icmp6_send(skb2, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH,
712ee576c47SJason A. Donenfeld info, &temp_saddr, IP6CB(skb2));
7135fbba8acSEric Dumazet if (rt)
7145fbba8acSEric Dumazet ip6_rt_put(rt);
7155fbba8acSEric Dumazet
7165fbba8acSEric Dumazet kfree_skb(skb2);
7175fbba8acSEric Dumazet
7185fbba8acSEric Dumazet return 0;
7195fbba8acSEric Dumazet }
7205fbba8acSEric Dumazet EXPORT_SYMBOL(ip6_err_gen_icmpv6_unreach);
7215fbba8acSEric Dumazet
icmpv6_echo_reply(struct sk_buff * skb)722ac03694bSEric Dumazet static enum skb_drop_reason icmpv6_echo_reply(struct sk_buff *skb)
7231da177e4SLinus Torvalds {
724*463a4559SEric Dumazet struct net *net = dev_net_rcu(skb->dev);
72584427d53SYOSHIFUJI Hideaki struct sock *sk;
7261da177e4SLinus Torvalds struct inet6_dev *idev;
72784427d53SYOSHIFUJI Hideaki struct ipv6_pinfo *np;
728b71d1d42SEric Dumazet const struct in6_addr *saddr = NULL;
729cc70ab26SArnaldo Carvalho de Melo struct icmp6hdr *icmph = icmp6_hdr(skb);
730662ec522SEric Dumazet bool apply_ratelimit = false;
7311da177e4SLinus Torvalds struct icmp6hdr tmp_hdr;
7324c9483b2SDavid S. Miller struct flowi6 fl6;
7331da177e4SLinus Torvalds struct icmpv6_msg msg;
7341da177e4SLinus Torvalds struct dst_entry *dst;
73526879da5SWei Wang struct ipcm6_cookie ipc6;
736e110861fSLorenzo Colitti u32 mark = IP6_REPLY_MARK(net, skb->mark);
737ac03694bSEric Dumazet SKB_DR(reason);
7380b03a5caSStephen Suryaputra bool acast;
7391fd07f33SAndreas Roeseler u8 type;
7401da177e4SLinus Torvalds
74103f1ecccSStephen Suryaputra if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) &&
74203f1ecccSStephen Suryaputra net->ipv6.sysctl.icmpv6_echo_ignore_multicast)
743ac03694bSEric Dumazet return reason;
74403f1ecccSStephen Suryaputra
7450660e03fSArnaldo Carvalho de Melo saddr = &ipv6_hdr(skb)->daddr;
7461da177e4SLinus Torvalds
7470b03a5caSStephen Suryaputra acast = ipv6_anycast_destination(skb_dst(skb), saddr);
7480b03a5caSStephen Suryaputra if (acast && net->ipv6.sysctl.icmpv6_echo_ignore_anycast)
749ac03694bSEric Dumazet return reason;
7500b03a5caSStephen Suryaputra
751509aba3bSFX Le Bail if (!ipv6_unicast_destination(skb) &&
7520b03a5caSStephen Suryaputra !(net->ipv6.sysctl.anycast_src_echo_reply && acast))
7531da177e4SLinus Torvalds saddr = NULL;
7541da177e4SLinus Torvalds
7551fd07f33SAndreas Roeseler if (icmph->icmp6_type == ICMPV6_EXT_ECHO_REQUEST)
7561fd07f33SAndreas Roeseler type = ICMPV6_EXT_ECHO_REPLY;
7571fd07f33SAndreas Roeseler else
7581fd07f33SAndreas Roeseler type = ICMPV6_ECHO_REPLY;
7591fd07f33SAndreas Roeseler
7601da177e4SLinus Torvalds memcpy(&tmp_hdr, icmph, sizeof(tmp_hdr));
7611fd07f33SAndreas Roeseler tmp_hdr.icmp6_type = type;
7621da177e4SLinus Torvalds
7634c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6));
764a346abe0SEric Dumazet if (net->ipv6.sysctl.flowlabel_reflect & FLOWLABEL_REFLECT_ICMPV6_ECHO_REPLIES)
765a346abe0SEric Dumazet fl6.flowlabel = ip6_flowlabel(ipv6_hdr(skb));
766a346abe0SEric Dumazet
7674c9483b2SDavid S. Miller fl6.flowi6_proto = IPPROTO_ICMPV6;
7684e3fd7a0SAlexey Dobriyan fl6.daddr = ipv6_hdr(skb)->saddr;
7691da177e4SLinus Torvalds if (saddr)
7704e3fd7a0SAlexey Dobriyan fl6.saddr = *saddr;
7711b70d792SDavid Ahern fl6.flowi6_oif = icmp6_iif(skb);
7721fd07f33SAndreas Roeseler fl6.fl6_icmp_type = type;
773e110861fSLorenzo Colitti fl6.flowi6_mark = mark;
774e2d118a1SLorenzo Colitti fl6.flowi6_uid = sock_net_uid(net, NULL);
7753df98d79SPaul Moore security_skb_classify_flow(skb, flowi6_to_flowi_common(&fl6));
7761da177e4SLinus Torvalds
7777ba91ecbSJesper Dangaard Brouer local_bh_disable();
778fdc0bde9SDenis V. Lunev sk = icmpv6_xmit_lock(net);
77963159f29SIan Morris if (!sk)
7807ba91ecbSJesper Dangaard Brouer goto out_bh_enable;
781fdc0bde9SDenis V. Lunev np = inet6_sk(sk);
782405666dbSDenis V. Lunev
7834c9483b2SDavid S. Miller if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
7844c9483b2SDavid S. Miller fl6.flowi6_oif = np->mcast_oif;
785c4062dfcSErich E. Hoover else if (!fl6.flowi6_oif)
786c4062dfcSErich E. Hoover fl6.flowi6_oif = np->ucast_oif;
7871da177e4SLinus Torvalds
7884e64b1edSJoe Perches if (ip6_dst_lookup(net, sk, &dst, &fl6))
7891da177e4SLinus Torvalds goto out;
7904c9483b2SDavid S. Miller dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), sk, 0);
791452edd59SDavid S. Miller if (IS_ERR(dst))
792e104411bSPatrick McHardy goto out;
7931da177e4SLinus Torvalds
7940bc19985SStephen Suryaputra /* Check the ratelimit */
795662ec522SEric Dumazet if ((!(skb->dev->flags & IFF_LOOPBACK) &&
796662ec522SEric Dumazet !icmpv6_global_allow(net, ICMPV6_ECHO_REPLY, &apply_ratelimit)) ||
797662ec522SEric Dumazet !icmpv6_xrlim_allow(sk, ICMPV6_ECHO_REPLY, &fl6, apply_ratelimit))
7980bc19985SStephen Suryaputra goto out_dst_release;
7990bc19985SStephen Suryaputra
800cfdf7647SEric Dumazet idev = __in6_dev_get(skb->dev);
8011da177e4SLinus Torvalds
8021da177e4SLinus Torvalds msg.skb = skb;
8031da177e4SLinus Torvalds msg.offset = 0;
8041fd07f33SAndreas Roeseler msg.type = type;
8051da177e4SLinus Torvalds
806b515430aSWillem de Bruijn ipcm6_init_sk(&ipc6, np);
80726879da5SWei Wang ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
80826879da5SWei Wang ipc6.tclass = ipv6_get_dsfield(ipv6_hdr(skb));
8090da7536fSWillem de Bruijn ipc6.sockc.mark = mark;
81026879da5SWei Wang
8111fd07f33SAndreas Roeseler if (icmph->icmp6_type == ICMPV6_EXT_ECHO_REQUEST)
8121fd07f33SAndreas Roeseler if (!icmp_build_probe(skb, (struct icmphdr *)&tmp_hdr))
8131fd07f33SAndreas Roeseler goto out_dst_release;
8141fd07f33SAndreas Roeseler
8154e64b1edSJoe Perches if (ip6_append_data(sk, icmpv6_getfrag, &msg,
8164e64b1edSJoe Perches skb->len + sizeof(struct icmp6hdr),
81726879da5SWei Wang sizeof(struct icmp6hdr), &ipc6, &fl6,
818797a4c1fSEric Dumazet dst_rt6_info(dst), MSG_DONTWAIT)) {
819a16292a0SEric Dumazet __ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS);
8201da177e4SLinus Torvalds ip6_flush_pending_frames(sk);
821cfdf7647SEric Dumazet } else {
8224e64b1edSJoe Perches icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr,
823cfdf7647SEric Dumazet skb->len + sizeof(struct icmp6hdr));
824ac03694bSEric Dumazet reason = SKB_CONSUMED;
8251da177e4SLinus Torvalds }
8260bc19985SStephen Suryaputra out_dst_release:
8271da177e4SLinus Torvalds dst_release(dst);
8281da177e4SLinus Torvalds out:
829405666dbSDenis V. Lunev icmpv6_xmit_unlock(sk);
8307ba91ecbSJesper Dangaard Brouer out_bh_enable:
8317ba91ecbSJesper Dangaard Brouer local_bh_enable();
832ac03694bSEric Dumazet return reason;
8331da177e4SLinus Torvalds }
8341da177e4SLinus Torvalds
icmpv6_notify(struct sk_buff * skb,u8 type,u8 code,__be32 info)83530c89badSEric Dumazet enum skb_drop_reason icmpv6_notify(struct sk_buff *skb, u8 type,
83630c89badSEric Dumazet u8 code, __be32 info)
8371da177e4SLinus Torvalds {
838e4129440SAndrew Lunn struct inet6_skb_parm *opt = IP6CB(skb);
839*463a4559SEric Dumazet struct net *net = dev_net_rcu(skb->dev);
84041135cc8SAlexey Dobriyan const struct inet6_protocol *ipprot;
84130c89badSEric Dumazet enum skb_drop_reason reason;
8421da177e4SLinus Torvalds int inner_offset;
84375f2811cSJesse Gross __be16 frag_off;
844f9242b6bSDavid S. Miller u8 nexthdr;
8451da177e4SLinus Torvalds
84630c89badSEric Dumazet reason = pskb_may_pull_reason(skb, sizeof(struct ipv6hdr));
84730c89badSEric Dumazet if (reason != SKB_NOT_DROPPED_YET)
8487304fe46SDuan Jiong goto out;
8491da177e4SLinus Torvalds
850e4129440SAndrew Lunn seg6_icmp_srh(skb, opt);
851e4129440SAndrew Lunn
8521da177e4SLinus Torvalds nexthdr = ((struct ipv6hdr *)skb->data)->nexthdr;
8531da177e4SLinus Torvalds if (ipv6_ext_hdr(nexthdr)) {
8541da177e4SLinus Torvalds /* now skip over extension headers */
85575f2811cSJesse Gross inner_offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
85675f2811cSJesse Gross &nexthdr, &frag_off);
85730c89badSEric Dumazet if (inner_offset < 0) {
85830c89badSEric Dumazet SKB_DR_SET(reason, IPV6_BAD_EXTHDR);
8597304fe46SDuan Jiong goto out;
86030c89badSEric Dumazet }
8611da177e4SLinus Torvalds } else {
8621da177e4SLinus Torvalds inner_offset = sizeof(struct ipv6hdr);
8631da177e4SLinus Torvalds }
8641da177e4SLinus Torvalds
8651da177e4SLinus Torvalds /* Checkin header including 8 bytes of inner protocol header. */
86630c89badSEric Dumazet reason = pskb_may_pull_reason(skb, inner_offset + 8);
86730c89badSEric Dumazet if (reason != SKB_NOT_DROPPED_YET)
8687304fe46SDuan Jiong goto out;
8691da177e4SLinus Torvalds
8701da177e4SLinus Torvalds /* BUGGG_FUTURE: we should try to parse exthdrs in this packet.
8711da177e4SLinus Torvalds Without this we will not able f.e. to make source routed
8721da177e4SLinus Torvalds pmtu discovery.
8731da177e4SLinus Torvalds Corresponding argument (opt) to notifiers is already added.
8741da177e4SLinus Torvalds --ANK (980726)
8751da177e4SLinus Torvalds */
8761da177e4SLinus Torvalds
877f9242b6bSDavid S. Miller ipprot = rcu_dereference(inet6_protos[nexthdr]);
8781da177e4SLinus Torvalds if (ipprot && ipprot->err_handler)
879e4129440SAndrew Lunn ipprot->err_handler(skb, opt, type, code, inner_offset, info);
8801da177e4SLinus Torvalds
88169d6da0bSPavel Emelyanov raw6_icmp_error(skb, nexthdr, type, code, inner_offset, info);
88230c89badSEric Dumazet return SKB_CONSUMED;
8837304fe46SDuan Jiong
8847304fe46SDuan Jiong out:
885a16292a0SEric Dumazet __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), ICMP6_MIB_INERRORS);
88630c89badSEric Dumazet return reason;
8871da177e4SLinus Torvalds }
8881da177e4SLinus Torvalds
8891da177e4SLinus Torvalds /*
8901da177e4SLinus Torvalds * Handle icmp messages
8911da177e4SLinus Torvalds */
8921da177e4SLinus Torvalds
icmpv6_rcv(struct sk_buff * skb)893e5bbef20SHerbert Xu static int icmpv6_rcv(struct sk_buff *skb)
8941da177e4SLinus Torvalds {
895b384c95aSMenglong Dong enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
896*463a4559SEric Dumazet struct net *net = dev_net_rcu(skb->dev);
897e1ae5c2eSStephen Suryaputra struct net_device *dev = icmp6_dev(skb);
8981da177e4SLinus Torvalds struct inet6_dev *idev = __in6_dev_get(dev);
899b71d1d42SEric Dumazet const struct in6_addr *saddr, *daddr;
9001da177e4SLinus Torvalds struct icmp6hdr *hdr;
901d5fdd6baSBrian Haley u8 type;
9021da177e4SLinus Torvalds
903aebcf82cSHerbert Xu if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
904def8b4faSAlexey Dobriyan struct sec_path *sp = skb_sec_path(skb);
9058b7817f3SHerbert Xu int nh;
9068b7817f3SHerbert Xu
907def8b4faSAlexey Dobriyan if (!(sp && sp->xvec[sp->len - 1]->props.flags &
908b384c95aSMenglong Dong XFRM_STATE_ICMP)) {
909b384c95aSMenglong Dong reason = SKB_DROP_REASON_XFRM_POLICY;
910aebcf82cSHerbert Xu goto drop_no_count;
911b384c95aSMenglong Dong }
912aebcf82cSHerbert Xu
91381aded24SDavid S. Miller if (!pskb_may_pull(skb, sizeof(*hdr) + sizeof(struct ipv6hdr)))
9148b7817f3SHerbert Xu goto drop_no_count;
9158b7817f3SHerbert Xu
9168b7817f3SHerbert Xu nh = skb_network_offset(skb);
9178b7817f3SHerbert Xu skb_set_network_header(skb, sizeof(*hdr));
9188b7817f3SHerbert Xu
919b384c95aSMenglong Dong if (!xfrm6_policy_check_reverse(NULL, XFRM_POLICY_IN,
920b384c95aSMenglong Dong skb)) {
921b384c95aSMenglong Dong reason = SKB_DROP_REASON_XFRM_POLICY;
9228b7817f3SHerbert Xu goto drop_no_count;
923b384c95aSMenglong Dong }
9248b7817f3SHerbert Xu
9258b7817f3SHerbert Xu skb_set_network_header(skb, nh);
9268b7817f3SHerbert Xu }
9278b7817f3SHerbert Xu
928*463a4559SEric Dumazet __ICMP6_INC_STATS(dev_net_rcu(dev), idev, ICMP6_MIB_INMSGS);
9291da177e4SLinus Torvalds
9300660e03fSArnaldo Carvalho de Melo saddr = &ipv6_hdr(skb)->saddr;
9310660e03fSArnaldo Carvalho de Melo daddr = &ipv6_hdr(skb)->daddr;
9321da177e4SLinus Torvalds
93339471ac8STom Herbert if (skb_checksum_validate(skb, IPPROTO_ICMPV6, ip6_compute_pseudo)) {
934ba7a46f1SJoe Perches net_dbg_ratelimited("ICMPv6 checksum failed [%pI6c > %pI6c]\n",
9350c6ce78aSHarvey Harrison saddr, daddr);
9366a5dc9e5SEric Dumazet goto csum_error;
9371da177e4SLinus Torvalds }
9381da177e4SLinus Torvalds
9398cf22943SHerbert Xu if (!pskb_pull(skb, sizeof(*hdr)))
9408cf22943SHerbert Xu goto discard_it;
9411da177e4SLinus Torvalds
942cc70ab26SArnaldo Carvalho de Melo hdr = icmp6_hdr(skb);
9431da177e4SLinus Torvalds
9441da177e4SLinus Torvalds type = hdr->icmp6_type;
9451da177e4SLinus Torvalds
946*463a4559SEric Dumazet ICMP6MSGIN_INC_STATS(dev_net_rcu(dev), idev, type);
9471da177e4SLinus Torvalds
9481da177e4SLinus Torvalds switch (type) {
9491da177e4SLinus Torvalds case ICMPV6_ECHO_REQUEST:
950e6f86b0fSVirgile Jarry if (!net->ipv6.sysctl.icmpv6_echo_ignore_all)
951ac03694bSEric Dumazet reason = icmpv6_echo_reply(skb);
9521da177e4SLinus Torvalds break;
9531fd07f33SAndreas Roeseler case ICMPV6_EXT_ECHO_REQUEST:
9541fd07f33SAndreas Roeseler if (!net->ipv6.sysctl.icmpv6_echo_ignore_all &&
9554a2f7083SKuniyuki Iwashima READ_ONCE(net->ipv4.sysctl_icmp_echo_enable_probe))
956ac03694bSEric Dumazet reason = icmpv6_echo_reply(skb);
9571fd07f33SAndreas Roeseler break;
9581da177e4SLinus Torvalds
9591da177e4SLinus Torvalds case ICMPV6_ECHO_REPLY:
960b384c95aSMenglong Dong reason = ping_rcv(skb);
9611da177e4SLinus Torvalds break;
9621da177e4SLinus Torvalds
96331433202SAndreas Roeseler case ICMPV6_EXT_ECHO_REPLY:
964b384c95aSMenglong Dong reason = ping_rcv(skb);
96531433202SAndreas Roeseler break;
96631433202SAndreas Roeseler
9671da177e4SLinus Torvalds case ICMPV6_PKT_TOOBIG:
9681da177e4SLinus Torvalds /* BUGGG_FUTURE: if packet contains rthdr, we cannot update
9691da177e4SLinus Torvalds standard destination cache. Seems, only "advanced"
9701da177e4SLinus Torvalds destination cache will allow to solve this problem
9711da177e4SLinus Torvalds --ANK (980726)
9721da177e4SLinus Torvalds */
9731da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
9741da177e4SLinus Torvalds goto discard_it;
975cc70ab26SArnaldo Carvalho de Melo hdr = icmp6_hdr(skb);
9761da177e4SLinus Torvalds
977275757e6SGustavo A. R. Silva /* to notify */
978a8eceea8SJoe Perches fallthrough;
9791da177e4SLinus Torvalds case ICMPV6_DEST_UNREACH:
9801da177e4SLinus Torvalds case ICMPV6_TIME_EXCEED:
9811da177e4SLinus Torvalds case ICMPV6_PARAMPROB:
98230c89badSEric Dumazet reason = icmpv6_notify(skb, type, hdr->icmp6_code,
98330c89badSEric Dumazet hdr->icmp6_mtu);
9841da177e4SLinus Torvalds break;
9851da177e4SLinus Torvalds
9861da177e4SLinus Torvalds case NDISC_ROUTER_SOLICITATION:
9871da177e4SLinus Torvalds case NDISC_ROUTER_ADVERTISEMENT:
9881da177e4SLinus Torvalds case NDISC_NEIGHBOUR_SOLICITATION:
9891da177e4SLinus Torvalds case NDISC_NEIGHBOUR_ADVERTISEMENT:
9901da177e4SLinus Torvalds case NDISC_REDIRECT:
991545dbcd1SEric Dumazet reason = ndisc_rcv(skb);
9921da177e4SLinus Torvalds break;
9931da177e4SLinus Torvalds
9941da177e4SLinus Torvalds case ICMPV6_MGM_QUERY:
9951da177e4SLinus Torvalds igmp6_event_query(skb);
996f185de28STaehee Yoo return 0;
9971da177e4SLinus Torvalds
9981da177e4SLinus Torvalds case ICMPV6_MGM_REPORT:
9991da177e4SLinus Torvalds igmp6_event_report(skb);
1000f185de28STaehee Yoo return 0;
10011da177e4SLinus Torvalds
10021da177e4SLinus Torvalds case ICMPV6_MGM_REDUCTION:
10031da177e4SLinus Torvalds case ICMPV6_NI_QUERY:
10041da177e4SLinus Torvalds case ICMPV6_NI_REPLY:
10051da177e4SLinus Torvalds case ICMPV6_MLD2_REPORT:
10061da177e4SLinus Torvalds case ICMPV6_DHAAD_REQUEST:
10071da177e4SLinus Torvalds case ICMPV6_DHAAD_REPLY:
10081da177e4SLinus Torvalds case ICMPV6_MOBILE_PREFIX_SOL:
10091da177e4SLinus Torvalds case ICMPV6_MOBILE_PREFIX_ADV:
10101da177e4SLinus Torvalds break;
10111da177e4SLinus Torvalds
10121da177e4SLinus Torvalds default:
10131da177e4SLinus Torvalds /* informational */
10141da177e4SLinus Torvalds if (type & ICMPV6_INFOMSG_MASK)
10151da177e4SLinus Torvalds break;
10161da177e4SLinus Torvalds
10174b3418fbSBjørn Mork net_dbg_ratelimited("icmpv6: msg of unknown type [%pI6c > %pI6c]\n",
10184b3418fbSBjørn Mork saddr, daddr);
1019ea85a0a2SDavid S. Miller
10201da177e4SLinus Torvalds /*
10211da177e4SLinus Torvalds * error of unknown type.
10221da177e4SLinus Torvalds * must pass to upper level
10231da177e4SLinus Torvalds */
10241da177e4SLinus Torvalds
102530c89badSEric Dumazet reason = icmpv6_notify(skb, type, hdr->icmp6_code,
102630c89badSEric Dumazet hdr->icmp6_mtu);
10273ff50b79SStephen Hemminger }
10283ff50b79SStephen Hemminger
1029e3e32170SRick Jones /* until the v6 path can be better sorted assume failure and
1030e3e32170SRick Jones * preserve the status quo behaviour for the rest of the paths to here
1031e3e32170SRick Jones */
1032b384c95aSMenglong Dong if (reason)
1033b384c95aSMenglong Dong kfree_skb_reason(skb, reason);
1034e3e32170SRick Jones else
1035b384c95aSMenglong Dong consume_skb(skb);
1036e3e32170SRick Jones
10371da177e4SLinus Torvalds return 0;
10381da177e4SLinus Torvalds
10396a5dc9e5SEric Dumazet csum_error:
1040b384c95aSMenglong Dong reason = SKB_DROP_REASON_ICMP_CSUM;
1041*463a4559SEric Dumazet __ICMP6_INC_STATS(dev_net_rcu(dev), idev, ICMP6_MIB_CSUMERRORS);
10421da177e4SLinus Torvalds discard_it:
1043*463a4559SEric Dumazet __ICMP6_INC_STATS(dev_net_rcu(dev), idev, ICMP6_MIB_INERRORS);
10448b7817f3SHerbert Xu drop_no_count:
1045b384c95aSMenglong Dong kfree_skb_reason(skb, reason);
10461da177e4SLinus Torvalds return 0;
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds
icmpv6_flow_init(const struct sock * sk,struct flowi6 * fl6,u8 type,const struct in6_addr * saddr,const struct in6_addr * daddr,int oif)10495bc67a85SGuillaume Nault void icmpv6_flow_init(const struct sock *sk, struct flowi6 *fl6, u8 type,
105095e41e93SYOSHIFUJI Hideaki const struct in6_addr *saddr,
10515bc67a85SGuillaume Nault const struct in6_addr *daddr, int oif)
105295e41e93SYOSHIFUJI Hideaki {
10534c9483b2SDavid S. Miller memset(fl6, 0, sizeof(*fl6));
10544e3fd7a0SAlexey Dobriyan fl6->saddr = *saddr;
10554e3fd7a0SAlexey Dobriyan fl6->daddr = *daddr;
10564c9483b2SDavid S. Miller fl6->flowi6_proto = IPPROTO_ICMPV6;
10571958b856SDavid S. Miller fl6->fl6_icmp_type = type;
10581958b856SDavid S. Miller fl6->fl6_icmp_code = 0;
10594c9483b2SDavid S. Miller fl6->flowi6_oif = oif;
10603df98d79SPaul Moore security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
106195e41e93SYOSHIFUJI Hideaki }
106295e41e93SYOSHIFUJI Hideaki
icmpv6_init(void)10636a17b961SEric Dumazet int __init icmpv6_init(void)
10641da177e4SLinus Torvalds {
10651da177e4SLinus Torvalds struct sock *sk;
10663232a1efSKefeng Wang int err, i;
10671da177e4SLinus Torvalds
10686f912042SKAMEZAWA Hiroyuki for_each_possible_cpu(i) {
10691ed8516fSDenis V. Lunev err = inet_ctl_sock_create(&sk, PF_INET6,
10706a17b961SEric Dumazet SOCK_RAW, IPPROTO_ICMPV6, &init_net);
10711da177e4SLinus Torvalds if (err < 0) {
1072f3213831SJoe Perches pr_err("Failed to initialize the ICMP6 control socket (err %d)\n",
10731da177e4SLinus Torvalds err);
10746a17b961SEric Dumazet return err;
10751da177e4SLinus Torvalds }
10761da177e4SLinus Torvalds
10776a17b961SEric Dumazet per_cpu(ipv6_icmp_sk, i) = sk;
10785c8cafd6SDenis V. Lunev
10791da177e4SLinus Torvalds /* Enough space for 2 64K ICMP packets, including
10801da177e4SLinus Torvalds * sk_buff struct overhead.
10811da177e4SLinus Torvalds */
108287fb4b7bSEric Dumazet sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
10831da177e4SLinus Torvalds }
108498c6d1b2SDenis V. Lunev
108598c6d1b2SDenis V. Lunev err = -EAGAIN;
108698c6d1b2SDenis V. Lunev if (inet6_add_protocol(&icmpv6_protocol, IPPROTO_ICMPV6) < 0)
108798c6d1b2SDenis V. Lunev goto fail;
10885f5624cfSPravin B Shelar
10895f5624cfSPravin B Shelar err = inet6_register_icmp_sender(icmp6_send);
10905f5624cfSPravin B Shelar if (err)
10915f5624cfSPravin B Shelar goto sender_reg_err;
109298c6d1b2SDenis V. Lunev return 0;
109398c6d1b2SDenis V. Lunev
10945f5624cfSPravin B Shelar sender_reg_err:
10955f5624cfSPravin B Shelar inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
109698c6d1b2SDenis V. Lunev fail:
1097f3213831SJoe Perches pr_err("Failed to register ICMP6 protocol\n");
109898c6d1b2SDenis V. Lunev return err;
109998c6d1b2SDenis V. Lunev }
110098c6d1b2SDenis V. Lunev
icmpv6_cleanup(void)11018ed7edceSAlexey Dobriyan void icmpv6_cleanup(void)
110298c6d1b2SDenis V. Lunev {
11035f5624cfSPravin B Shelar inet6_unregister_icmp_sender(icmp6_send);
11041da177e4SLinus Torvalds inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds
110798c6d1b2SDenis V. Lunev
11089b5b5cffSArjan van de Ven static const struct icmp6_err {
11091da177e4SLinus Torvalds int err;
11101da177e4SLinus Torvalds int fatal;
11111da177e4SLinus Torvalds } tab_unreach[] = {
11121da177e4SLinus Torvalds { /* NOROUTE */
11131da177e4SLinus Torvalds .err = ENETUNREACH,
11141da177e4SLinus Torvalds .fatal = 0,
11151da177e4SLinus Torvalds },
11161da177e4SLinus Torvalds { /* ADM_PROHIBITED */
11171da177e4SLinus Torvalds .err = EACCES,
11181da177e4SLinus Torvalds .fatal = 1,
11191da177e4SLinus Torvalds },
11201da177e4SLinus Torvalds { /* Was NOT_NEIGHBOUR, now reserved */
11211da177e4SLinus Torvalds .err = EHOSTUNREACH,
11221da177e4SLinus Torvalds .fatal = 0,
11231da177e4SLinus Torvalds },
11241da177e4SLinus Torvalds { /* ADDR_UNREACH */
11251da177e4SLinus Torvalds .err = EHOSTUNREACH,
11261da177e4SLinus Torvalds .fatal = 0,
11271da177e4SLinus Torvalds },
11281da177e4SLinus Torvalds { /* PORT_UNREACH */
11291da177e4SLinus Torvalds .err = ECONNREFUSED,
11301da177e4SLinus Torvalds .fatal = 1,
11311da177e4SLinus Torvalds },
113261e76b17SJiri Bohac { /* POLICY_FAIL */
113361e76b17SJiri Bohac .err = EACCES,
113461e76b17SJiri Bohac .fatal = 1,
113561e76b17SJiri Bohac },
113661e76b17SJiri Bohac { /* REJECT_ROUTE */
113761e76b17SJiri Bohac .err = EACCES,
113861e76b17SJiri Bohac .fatal = 1,
113961e76b17SJiri Bohac },
11401da177e4SLinus Torvalds };
11411da177e4SLinus Torvalds
icmpv6_err_convert(u8 type,u8 code,int * err)1142d5fdd6baSBrian Haley int icmpv6_err_convert(u8 type, u8 code, int *err)
11431da177e4SLinus Torvalds {
11441da177e4SLinus Torvalds int fatal = 0;
11451da177e4SLinus Torvalds
11461da177e4SLinus Torvalds *err = EPROTO;
11471da177e4SLinus Torvalds
11481da177e4SLinus Torvalds switch (type) {
11491da177e4SLinus Torvalds case ICMPV6_DEST_UNREACH:
11501da177e4SLinus Torvalds fatal = 1;
115161e76b17SJiri Bohac if (code < ARRAY_SIZE(tab_unreach)) {
11521da177e4SLinus Torvalds *err = tab_unreach[code].err;
11531da177e4SLinus Torvalds fatal = tab_unreach[code].fatal;
11541da177e4SLinus Torvalds }
11551da177e4SLinus Torvalds break;
11561da177e4SLinus Torvalds
11571da177e4SLinus Torvalds case ICMPV6_PKT_TOOBIG:
11581da177e4SLinus Torvalds *err = EMSGSIZE;
11591da177e4SLinus Torvalds break;
11601da177e4SLinus Torvalds
11611da177e4SLinus Torvalds case ICMPV6_PARAMPROB:
11621da177e4SLinus Torvalds *err = EPROTO;
11631da177e4SLinus Torvalds fatal = 1;
11641da177e4SLinus Torvalds break;
11651da177e4SLinus Torvalds
11661da177e4SLinus Torvalds case ICMPV6_TIME_EXCEED:
11671da177e4SLinus Torvalds *err = EHOSTUNREACH;
11681da177e4SLinus Torvalds break;
11693ff50b79SStephen Hemminger }
11701da177e4SLinus Torvalds
11711da177e4SLinus Torvalds return fatal;
11721da177e4SLinus Torvalds }
11737159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(icmpv6_err_convert);
11747159039aSYOSHIFUJI Hideaki
11751da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
1176e8243534Sstephen hemminger static struct ctl_table ipv6_icmp_table_template[] = {
11771da177e4SLinus Torvalds {
11781da177e4SLinus Torvalds .procname = "ratelimit",
117941a76906SDaniel Lezcano .data = &init_net.ipv6.sysctl.icmpv6_time,
11801da177e4SLinus Torvalds .maxlen = sizeof(int),
11811da177e4SLinus Torvalds .mode = 0644,
11826d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies,
11831da177e4SLinus Torvalds },
1184e6f86b0fSVirgile Jarry {
1185e6f86b0fSVirgile Jarry .procname = "echo_ignore_all",
1186e6f86b0fSVirgile Jarry .data = &init_net.ipv6.sysctl.icmpv6_echo_ignore_all,
1187a6175633SEric Dumazet .maxlen = sizeof(u8),
1188e6f86b0fSVirgile Jarry .mode = 0644,
1189a6175633SEric Dumazet .proc_handler = proc_dou8vec_minmax,
1190e6f86b0fSVirgile Jarry },
119103f1ecccSStephen Suryaputra {
119203f1ecccSStephen Suryaputra .procname = "echo_ignore_multicast",
119303f1ecccSStephen Suryaputra .data = &init_net.ipv6.sysctl.icmpv6_echo_ignore_multicast,
1194a6175633SEric Dumazet .maxlen = sizeof(u8),
119503f1ecccSStephen Suryaputra .mode = 0644,
1196a6175633SEric Dumazet .proc_handler = proc_dou8vec_minmax,
119703f1ecccSStephen Suryaputra },
11980b03a5caSStephen Suryaputra {
11990b03a5caSStephen Suryaputra .procname = "echo_ignore_anycast",
12000b03a5caSStephen Suryaputra .data = &init_net.ipv6.sysctl.icmpv6_echo_ignore_anycast,
1201a6175633SEric Dumazet .maxlen = sizeof(u8),
12020b03a5caSStephen Suryaputra .mode = 0644,
1203a6175633SEric Dumazet .proc_handler = proc_dou8vec_minmax,
12040b03a5caSStephen Suryaputra },
12050bc19985SStephen Suryaputra {
12060bc19985SStephen Suryaputra .procname = "ratemask",
12070bc19985SStephen Suryaputra .data = &init_net.ipv6.sysctl.icmpv6_ratemask_ptr,
12080bc19985SStephen Suryaputra .maxlen = ICMPV6_MSG_MAX + 1,
12090bc19985SStephen Suryaputra .mode = 0644,
12100bc19985SStephen Suryaputra .proc_handler = proc_do_large_bitmap,
12110bc19985SStephen Suryaputra },
12127ab75456SMahesh Bandewar {
12137ab75456SMahesh Bandewar .procname = "error_anycast_as_unicast",
12147ab75456SMahesh Bandewar .data = &init_net.ipv6.sysctl.icmpv6_error_anycast_as_unicast,
12157ab75456SMahesh Bandewar .maxlen = sizeof(u8),
12167ab75456SMahesh Bandewar .mode = 0644,
12177ab75456SMahesh Bandewar .proc_handler = proc_dou8vec_minmax,
12187ab75456SMahesh Bandewar .extra1 = SYSCTL_ZERO,
12197ab75456SMahesh Bandewar .extra2 = SYSCTL_ONE,
12207ab75456SMahesh Bandewar },
1221f8572d8fSEric W. Biederman { },
12221da177e4SLinus Torvalds };
1223760f2d01SDaniel Lezcano
ipv6_icmp_sysctl_init(struct net * net)12242c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_icmp_sysctl_init(struct net *net)
1225760f2d01SDaniel Lezcano {
1226760f2d01SDaniel Lezcano struct ctl_table *table;
1227760f2d01SDaniel Lezcano
1228760f2d01SDaniel Lezcano table = kmemdup(ipv6_icmp_table_template,
1229760f2d01SDaniel Lezcano sizeof(ipv6_icmp_table_template),
1230760f2d01SDaniel Lezcano GFP_KERNEL);
12315ee09105SYOSHIFUJI Hideaki
1232e6f86b0fSVirgile Jarry if (table) {
12335ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.icmpv6_time;
1234e6f86b0fSVirgile Jarry table[1].data = &net->ipv6.sysctl.icmpv6_echo_ignore_all;
123503f1ecccSStephen Suryaputra table[2].data = &net->ipv6.sysctl.icmpv6_echo_ignore_multicast;
12360b03a5caSStephen Suryaputra table[3].data = &net->ipv6.sysctl.icmpv6_echo_ignore_anycast;
12370bc19985SStephen Suryaputra table[4].data = &net->ipv6.sysctl.icmpv6_ratemask_ptr;
12387ab75456SMahesh Bandewar table[5].data = &net->ipv6.sysctl.icmpv6_error_anycast_as_unicast;
1239e6f86b0fSVirgile Jarry }
1240760f2d01SDaniel Lezcano return table;
1241760f2d01SDaniel Lezcano }
1242c899710fSJoel Granados
ipv6_icmp_sysctl_table_size(void)1243c899710fSJoel Granados size_t ipv6_icmp_sysctl_table_size(void)
1244c899710fSJoel Granados {
1245c899710fSJoel Granados return ARRAY_SIZE(ipv6_icmp_table_template);
1246c899710fSJoel Granados }
12471da177e4SLinus Torvalds #endif
1248