icmp: add helpers to recognize ICMP error packets
authorMatteo Croce <mcroce@redhat.com>
Sat, 2 Nov 2019 00:12:03 +0000 (01:12 +0100)
committerDavid S. Miller <davem@davemloft.net>
Tue, 5 Nov 2019 22:03:11 +0000 (14:03 -0800)
Add two helper functions, one for IPv4 and one for IPv6, to recognize
the ICMP packets which are error responses.
This packets are special because they have as payload the original
header of the packet which generated it (RFC 792 says at least 8 bytes,
but Linux actually includes much more than that).

Signed-off-by: Matteo Croce <mcroce@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/icmp.h
include/linux/icmpv6.h

index 2d8aaf7d4b9e2da9f883d18bda399f956723ce0f..81ca84ce3119247400601059921899511e3ff9f1 100644 (file)
@@ -20,4 +20,19 @@ static inline struct icmphdr *icmp_hdr(const struct sk_buff *skb)
 {
        return (struct icmphdr *)skb_transport_header(skb);
 }
+
+static inline bool icmp_is_err(int type)
+{
+       switch (type) {
+       case ICMP_DEST_UNREACH:
+       case ICMP_SOURCE_QUENCH:
+       case ICMP_REDIRECT:
+       case ICMP_TIME_EXCEEDED:
+       case ICMP_PARAMETERPROB:
+               return true;
+       }
+
+       return false;
+}
+
 #endif /* _LINUX_ICMP_H */
index a8f888976137820e9b6a94a5d6bf37619073d19a..ef1cbb5f454f7aa105db534ecc1ddbb56df333ce 100644 (file)
@@ -46,4 +46,18 @@ extern void                          icmpv6_flow_init(struct sock *sk,
                                                         const struct in6_addr *saddr,
                                                         const struct in6_addr *daddr,
                                                         int oif);
+
+static inline bool icmpv6_is_err(int type)
+{
+       switch (type) {
+       case ICMPV6_DEST_UNREACH:
+       case ICMPV6_PKT_TOOBIG:
+       case ICMPV6_TIME_EXCEED:
+       case ICMPV6_PARAMPROB:
+               return true;
+       }
+
+       return false;
+}
+
 #endif