]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-icmp.c
Use more the ND_TTEST_1() macro
[tcpdump] / print-icmp.c
index bebf3b7a2b1d3a61284e8d28ac45c57e05503974..a9a72014fbef7151ee52eed22860449397cd4833 100644 (file)
  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
-#define NETDISSECT_REWORKED
+/* \summary: Internet Control Message Protocol (ICMP) printer */
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#include <tcpdump-stdinc.h>
+#include <netdissect-stdinc.h>
 
 #include <stdio.h>
 #include <string.h>
 
-#include "interface.h"
+#include "netdissect.h"
 #include "addrtoname.h"
-#include "extract.h"                   /* must come after interface.h */
+#include "extract.h"
 
 #include "ip.h"
 #include "udp.h"
  * Structure of an icmp header.
  */
 struct icmp {
-       u_int8_t  icmp_type;            /* type of message, see below */
-       u_int8_t  icmp_code;            /* type sub code */
-       u_int16_t icmp_cksum;           /* ones complement cksum of struct */
+       uint8_t  icmp_type;             /* type of message, see below */
+       uint8_t  icmp_code;             /* type sub code */
+       uint16_t icmp_cksum;            /* ones complement cksum of struct */
        union {
-               u_int8_t ih_pptr;                       /* ICMP_PARAMPROB */
+               uint8_t ih_pptr;                        /* ICMP_PARAMPROB */
                struct in_addr ih_gwaddr;       /* ICMP_REDIRECT */
                struct ih_idseq {
-                       u_int16_t icd_id;
-                       u_int16_t icd_seq;
+                       uint16_t icd_id;
+                       uint16_t icd_seq;
                } ih_idseq;
-               u_int32_t ih_void;
+               uint32_t ih_void;
        } icmp_hun;
 #define        icmp_pptr       icmp_hun.ih_pptr
 #define        icmp_gwaddr     icmp_hun.ih_gwaddr
@@ -66,16 +67,16 @@ struct icmp {
 #define        icmp_void       icmp_hun.ih_void
        union {
                struct id_ts {
-                       u_int32_t its_otime;
-                       u_int32_t its_rtime;
-                       u_int32_t its_ttime;
+                       uint32_t its_otime;
+                       uint32_t its_rtime;
+                       uint32_t its_ttime;
                } id_ts;
                struct id_ip  {
                        struct ip idi_ip;
                        /* options and then 64 bits of data */
                } id_ip;
-               u_int32_t id_mask;
-               u_int8_t id_data[1];
+               uint32_t id_mask;
+               uint8_t id_data[1];
        } icmp_dun;
 #define        icmp_otime      icmp_dun.id_ts.its_otime
 #define        icmp_rtime      icmp_dun.id_ts.its_rtime
@@ -98,7 +99,7 @@ struct icmp {
  */
 #define        ICMP_MINLEN     8                               /* abs minimum */
 #define ICMP_EXTD_MINLEN (156 - sizeof (struct ip))     /* draft-bonica-internet-icmp-08 */
-#define        ICMP_TSLEN      (8 + 3 * sizeof (u_int32_t))    /* timestamp */
+#define        ICMP_TSLEN      (8 + 3 * sizeof (uint32_t))     /* timestamp */
 #define        ICMP_MASKLEN    12                              /* address mask */
 #define        ICMP_ADVLENMIN  (8 + sizeof (struct ip) + 8)    /* min */
 #define        ICMP_ADVLEN(p)  (8 + (IP_HL(&(p)->icmp_ip) << 2) + 8)
@@ -145,12 +146,10 @@ struct icmp {
 
 #define        ICMP_MAXTYPE            18
 
-#define        ICMP_INFOTYPE(type) \
-       ((type) == ICMP_ECHOREPLY || (type) == ICMP_ECHO || \
-       (type) == ICMP_ROUTERADVERT || (type) == ICMP_ROUTERSOLICIT || \
-       (type) == ICMP_TSTAMP || (type) == ICMP_TSTAMPREPLY || \
-       (type) == ICMP_IREQ || (type) == ICMP_IREQREPLY || \
-       (type) == ICMP_MASKREQ || (type) == ICMP_MASKREPLY)
+#define ICMP_ERRTYPE(type) \
+       ((type) == ICMP_UNREACH || (type) == ICMP_SOURCEQUENCH || \
+       (type) == ICMP_REDIRECT || (type) == ICMP_TIMXCEED || \
+       (type) == ICMP_PARAMPROB)
 #define        ICMP_MPLS_EXT_TYPE(type) \
        ((type) == ICMP_UNREACH || \
          (type) == ICMP_TIMXCEED || \
@@ -241,20 +240,20 @@ static const struct tok type2str[] = {
 
 /* rfc1191 */
 struct mtu_discovery {
-       u_int16_t unused;
-       u_int16_t nexthopmtu;
+       uint16_t unused;
+       uint16_t nexthopmtu;
 };
 
 /* rfc1256 */
 struct ih_rdiscovery {
-       u_int8_t ird_addrnum;
-       u_int8_t ird_addrsiz;
-       u_int16_t ird_lifetime;
+       uint8_t ird_addrnum;
+       uint8_t ird_addrsiz;
+       uint16_t ird_lifetime;
 };
 
 struct id_rdiscovery {
-       u_int32_t ird_addr;
-       u_int32_t ird_pref;
+       uint32_t ird_addr;
+       uint32_t ird_pref;
 };
 
 /*
@@ -284,22 +283,22 @@ struct id_rdiscovery {
  */
 
 struct icmp_ext_t {
-    u_int8_t icmp_type;
-    u_int8_t icmp_code;
-    u_int8_t icmp_checksum[2];
-    u_int8_t icmp_reserved;
-    u_int8_t icmp_length;
-    u_int8_t icmp_reserved2[2];
-    u_int8_t icmp_ext_legacy_header[128]; /* extension header starts 128 bytes after ICMP header */
-    u_int8_t icmp_ext_version_res[2];
-    u_int8_t icmp_ext_checksum[2];
-    u_int8_t icmp_ext_data[1];
+    uint8_t icmp_type;
+    uint8_t icmp_code;
+    uint8_t icmp_checksum[2];
+    uint8_t icmp_reserved;
+    uint8_t icmp_length;
+    uint8_t icmp_reserved2[2];
+    uint8_t icmp_ext_legacy_header[128]; /* extension header starts 128 bytes after ICMP header */
+    uint8_t icmp_ext_version_res[2];
+    uint8_t icmp_ext_checksum[2];
+    uint8_t icmp_ext_data[1];
 };
 
 struct icmp_mpls_ext_object_header_t {
-    u_int8_t length[2];
-    u_int8_t class_num;
-    u_int8_t ctype;
+    uint8_t length[2];
+    uint8_t class_num;
+    uint8_t ctype;
 };
 
 static const struct tok icmp_mpls_ext_obj_values[] = {
@@ -313,7 +312,8 @@ const char *icmp_tstamp_print(u_int);
 
 /* print the milliseconds since midnight UTC */
 const char *
-icmp_tstamp_print(u_int tstamp) {
+icmp_tstamp_print(u_int tstamp)
+{
     u_int msec,sec,min,hrs;
 
     static char buf[64];
@@ -336,18 +336,19 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
        const struct ip *ip;
        const char *str, *fmt;
        const struct ip *oip;
+       uint8_t ip_proto;
        const struct udphdr *ouh;
-        const u_int8_t *obj_tptr;
-        u_int32_t raw_label;
+        const uint8_t *obj_tptr;
+        uint32_t raw_label;
         const u_char *snapend_save;
        const struct icmp_mpls_ext_object_header_t *icmp_mpls_ext_object_header;
        u_int hlen, dport, mtu, obj_tlen, obj_class_num, obj_ctype;
        char buf[MAXHOSTNAMELEN + 100];
        struct cksum_vec vec[1];
 
-       dp = (struct icmp *)bp;
-        ext_dp = (struct icmp_ext_t *)bp;
-       ip = (struct ip *)bp2;
+       dp = (const struct icmp *)bp;
+        ext_dp = (const struct icmp_ext_t *)bp;
+       ip = (const struct ip *)bp2;
        str = buf;
 
        ND_TCHECK(dp->icmp_code);
@@ -359,8 +360,8 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
                (void)snprintf(buf, sizeof(buf), "echo %s, id %u, seq %u",
                                dp->icmp_type == ICMP_ECHO ?
                                "request" : "reply",
-                               EXTRACT_16BITS(&dp->icmp_id),
-                               EXTRACT_16BITS(&dp->icmp_seq));
+                               EXTRACT_BE_U_2(&dp->icmp_id),
+                               EXTRACT_BE_U_2(&dp->icmp_seq));
                break;
 
        case ICMP_UNREACH:
@@ -368,50 +369,51 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
                switch (dp->icmp_code) {
 
                case ICMP_UNREACH_PROTOCOL:
-                       ND_TCHECK(dp->icmp_ip.ip_p);
+                       ND_TCHECK_1(dp->icmp_ip.ip_p);
                        (void)snprintf(buf, sizeof(buf),
                            "%s protocol %d unreachable",
                            ipaddr_string(ndo, &dp->icmp_ip.ip_dst),
-                           dp->icmp_ip.ip_p);
+                           EXTRACT_U_1(dp->icmp_ip.ip_p));
                        break;
 
                case ICMP_UNREACH_PORT:
                        ND_TCHECK(dp->icmp_ip.ip_p);
                        oip = &dp->icmp_ip;
                        hlen = IP_HL(oip) * 4;
-                       ouh = (struct udphdr *)(((u_char *)oip) + hlen);
+                       ouh = (const struct udphdr *)(((const u_char *)oip) + hlen);
                        ND_TCHECK(ouh->uh_dport);
-                       dport = EXTRACT_16BITS(&ouh->uh_dport);
-                       switch (oip->ip_p) {
+                       dport = EXTRACT_BE_U_2(&ouh->uh_dport);
+                       ip_proto = EXTRACT_U_1(oip->ip_p);
+                       switch (ip_proto) {
 
                        case IPPROTO_TCP:
                                (void)snprintf(buf, sizeof(buf),
                                        "%s tcp port %s unreachable",
                                        ipaddr_string(ndo, &oip->ip_dst),
-                                       tcpport_string(dport));
+                                       tcpport_string(ndo, dport));
                                break;
 
                        case IPPROTO_UDP:
                                (void)snprintf(buf, sizeof(buf),
                                        "%s udp port %s unreachable",
                                        ipaddr_string(ndo, &oip->ip_dst),
-                                       udpport_string(dport));
+                                       udpport_string(ndo, dport));
                                break;
 
                        default:
                                (void)snprintf(buf, sizeof(buf),
-                                       "%s protocol %d port %d unreachable",
+                                       "%s protocol %u port %u unreachable",
                                        ipaddr_string(ndo, &oip->ip_dst),
-                                       oip->ip_p, dport);
+                                       ip_proto, dport);
                                break;
                        }
                        break;
 
                case ICMP_UNREACH_NEEDFRAG:
                    {
-                       register const struct mtu_discovery *mp;
-                       mp = (struct mtu_discovery *)(u_char *)&dp->icmp_void;
-                       mtu = EXTRACT_16BITS(&mp->nexthopmtu);
+                       const struct mtu_discovery *mp;
+                       mp = (const struct mtu_discovery *)(const u_char *)&dp->icmp_void;
+                       mtu = EXTRACT_BE_U_2(&mp->nexthopmtu);
                        if (mtu) {
                                (void)snprintf(buf, sizeof(buf),
                                    "%s unreachable - need to frag (mtu %d)",
@@ -444,18 +446,18 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
 
        case ICMP_ROUTERADVERT:
            {
-               register const struct ih_rdiscovery *ihp;
-               register const struct id_rdiscovery *idp;
+               const struct ih_rdiscovery *ihp;
+               const struct id_rdiscovery *idp;
                u_int lifetime, num, size;
 
                (void)snprintf(buf, sizeof(buf), "router advertisement");
                cp = buf + strlen(buf);
 
-               ihp = (struct ih_rdiscovery *)&dp->icmp_void;
+               ihp = (const struct ih_rdiscovery *)&dp->icmp_void;
                ND_TCHECK(*ihp);
                (void)strncpy(cp, " lifetime ", sizeof(buf) - (cp - buf));
                cp = buf + strlen(buf);
-               lifetime = EXTRACT_16BITS(&ihp->ird_lifetime);
+               lifetime = EXTRACT_BE_U_2(&ihp->ird_lifetime);
                if (lifetime < 60) {
                        (void)snprintf(cp, sizeof(buf) - (cp - buf), "%u",
                            lifetime);
@@ -481,12 +483,12 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
                            " [size %d]", size);
                        break;
                }
-               idp = (struct id_rdiscovery *)&dp->icmp_data;
+               idp = (const struct id_rdiscovery *)&dp->icmp_data;
                while (num-- > 0) {
                        ND_TCHECK(*idp);
                        (void)snprintf(cp, sizeof(buf) - (cp - buf), " {%s %u}",
                            ipaddr_string(ndo, &idp->ird_addr),
-                           EXTRACT_32BITS(&idp->ird_pref));
+                           EXTRACT_BE_U_4(&idp->ird_pref));
                        cp = buf + strlen(buf);
                        ++idp;
                }
@@ -506,7 +508,7 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
                        break;
 
                default:
-                       (void)snprintf(buf, sizeof(buf), "time exceeded-#%d",
+                       (void)snprintf(buf, sizeof(buf), "time exceeded-#%u",
                            dp->icmp_code);
                        break;
                }
@@ -515,40 +517,40 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
        case ICMP_PARAMPROB:
                if (dp->icmp_code)
                        (void)snprintf(buf, sizeof(buf),
-                           "parameter problem - code %d", dp->icmp_code);
+                           "parameter problem - code %u", dp->icmp_code);
                else {
                        ND_TCHECK(dp->icmp_pptr);
                        (void)snprintf(buf, sizeof(buf),
-                           "parameter problem - octet %d", dp->icmp_pptr);
+                           "parameter problem - octet %u", dp->icmp_pptr);
                }
                break;
 
        case ICMP_MASKREPLY:
                ND_TCHECK(dp->icmp_mask);
                (void)snprintf(buf, sizeof(buf), "address mask is 0x%08x",
-                   EXTRACT_32BITS(&dp->icmp_mask));
+                   EXTRACT_BE_U_4(&dp->icmp_mask));
                break;
 
        case ICMP_TSTAMP:
                ND_TCHECK(dp->icmp_seq);
                (void)snprintf(buf, sizeof(buf),
                    "time stamp query id %u seq %u",
-                   EXTRACT_16BITS(&dp->icmp_id),
-                   EXTRACT_16BITS(&dp->icmp_seq));
+                   EXTRACT_BE_U_2(&dp->icmp_id),
+                   EXTRACT_BE_U_2(&dp->icmp_seq));
                break;
 
        case ICMP_TSTAMPREPLY:
                ND_TCHECK(dp->icmp_ttime);
                (void)snprintf(buf, sizeof(buf),
                    "time stamp reply id %u seq %u: org %s",
-                               EXTRACT_16BITS(&dp->icmp_id),
-                               EXTRACT_16BITS(&dp->icmp_seq),
-                               icmp_tstamp_print(EXTRACT_32BITS(&dp->icmp_otime)));
+                               EXTRACT_BE_U_2(&dp->icmp_id),
+                               EXTRACT_BE_U_2(&dp->icmp_seq),
+                               icmp_tstamp_print(EXTRACT_BE_U_4(&dp->icmp_otime)));
 
                 (void)snprintf(buf+strlen(buf),sizeof(buf)-strlen(buf),", recv %s",
-                         icmp_tstamp_print(EXTRACT_32BITS(&dp->icmp_rtime)));
+                         icmp_tstamp_print(EXTRACT_BE_U_4(&dp->icmp_rtime)));
                 (void)snprintf(buf+strlen(buf),sizeof(buf)-strlen(buf),", xmit %s",
-                         icmp_tstamp_print(EXTRACT_32BITS(&dp->icmp_ttime)));
+                         icmp_tstamp_print(EXTRACT_BE_U_4(&dp->icmp_ttime)));
                 break;
 
        default:
@@ -557,14 +559,14 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
        }
        ND_PRINT((ndo, "ICMP %s, length %u", str, plen));
        if (ndo->ndo_vflag && !fragmented) { /* don't attempt checksumming if this is a frag */
-               u_int16_t sum, icmp_sum;
-               struct cksum_vec vec[1];
-               if (ND_TTEST2(*bp, plen)) {
-                       vec[0].ptr = (const u_int8_t *)(void *)dp;
+               if (ND_TTEST_LEN(bp, plen)) {
+                       uint16_t sum;
+
+                       vec[0].ptr = (const uint8_t *)(const void *)dp;
                        vec[0].len = plen;
                        sum = in_cksum(vec, 1);
                        if (sum != 0) {
-                               icmp_sum = EXTRACT_16BITS(&dp->icmp_cksum);
+                               uint16_t icmp_sum = EXTRACT_BE_U_2(&dp->icmp_cksum);
                                ND_PRINT((ndo, " (wrong icmp cksum %x (->%x)!)",
                                             icmp_sum,
                                             in_cksum_shouldbe(icmp_sum, sum)));
@@ -576,13 +578,13 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
          * print the remnants of the IP packet.
          * save the snaplength as this may get overidden in the IP printer.
          */
-       if (ndo->ndo_vflag >= 1 && !ICMP_INFOTYPE(dp->icmp_type)) {
+       if (ndo->ndo_vflag >= 1 && ICMP_ERRTYPE(dp->icmp_type)) {
                bp += 8;
                ND_PRINT((ndo, "\n\t"));
-               ip = (struct ip *)bp;
-               ndo->ndo_snaplen = ndo->ndo_snapend - bp;
+               ip = (const struct ip *)bp;
                 snapend_save = ndo->ndo_snapend;
-               ip_print(ndo, bp, EXTRACT_16BITS(&ip->ip_len));
+               ND_TCHECK_2(&ip->ip_len);
+               ip_print(ndo, bp, EXTRACT_BE_U_2(&ip->ip_len));
                 ndo->ndo_snapend = snapend_save;
        }
 
@@ -599,8 +601,9 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
              * to check if an extension header is present. This is expedient,
              * however not all implementations set the length field proper.
              */
-            if (!ext_dp->icmp_length) {
-                vec[0].ptr = (const u_int8_t *)(void *)&ext_dp->icmp_ext_version_res;
+            if (!ext_dp->icmp_length &&
+                ND_TTEST_LEN(ext_dp->icmp_ext_version_res, plen - ICMP_EXTD_MINLEN)) {
+                vec[0].ptr = (const uint8_t *)(const void *)&ext_dp->icmp_ext_version_res;
                 vec[0].len = plen - ICMP_EXTD_MINLEN;
                 if (in_cksum(vec, 1)) {
                     return;
@@ -620,21 +623,23 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
             }
 
             hlen = plen - ICMP_EXTD_MINLEN;
-            vec[0].ptr = (const u_int8_t *)(void *)&ext_dp->icmp_ext_version_res;
-            vec[0].len = hlen;
-            ND_PRINT((ndo, ", checksum 0x%04x (%scorrect), length %u",
-                   EXTRACT_16BITS(ext_dp->icmp_ext_checksum),
-                   in_cksum(vec, 1) ? "in" : "",
-                   hlen));
+            if (ND_TTEST_LEN(ext_dp->icmp_ext_version_res, hlen)) {
+                vec[0].ptr = (const uint8_t *)(const void *)&ext_dp->icmp_ext_version_res;
+                vec[0].len = hlen;
+                ND_PRINT((ndo, ", checksum 0x%04x (%scorrect), length %u",
+                       EXTRACT_BE_U_2(ext_dp->icmp_ext_checksum),
+                       in_cksum(vec, 1) ? "in" : "",
+                       hlen));
+            }
 
             hlen -= 4; /* subtract common header size */
-            obj_tptr = (u_int8_t *)ext_dp->icmp_ext_data;
+            obj_tptr = (const uint8_t *)ext_dp->icmp_ext_data;
 
             while (hlen > sizeof(struct icmp_mpls_ext_object_header_t)) {
 
-                icmp_mpls_ext_object_header = (struct icmp_mpls_ext_object_header_t *)obj_tptr;
+                icmp_mpls_ext_object_header = (const struct icmp_mpls_ext_object_header_t *)obj_tptr;
                 ND_TCHECK(*icmp_mpls_ext_object_header);
-                obj_tlen = EXTRACT_16BITS(icmp_mpls_ext_object_header->length);
+                obj_tlen = EXTRACT_BE_U_2(icmp_mpls_ext_object_header->length);
                 obj_class_num = icmp_mpls_ext_object_header->class_num;
                 obj_ctype = icmp_mpls_ext_object_header->ctype;
                 obj_tptr += sizeof(struct icmp_mpls_ext_object_header_t);
@@ -658,8 +663,8 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
                 case 1:
                     switch(obj_ctype) {
                     case 1:
-                        ND_TCHECK2(*obj_tptr, 4);
-                        raw_label = EXTRACT_32BITS(obj_tptr);
+                        ND_TCHECK_4(obj_tptr);
+                        raw_label = EXTRACT_BE_U_4(obj_tptr);
                         ND_PRINT((ndo, "\n\t    label %u, exp %u", MPLS_LABEL(raw_label), MPLS_EXP(raw_label)));
                         if (MPLS_STACK(raw_label))
                             ND_PRINT((ndo, ", [S]"));