]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-icmp6.c
Handle very large -f files by rejecting them.
[tcpdump] / print-icmp6.c
index 4584f5a473bbd957e1276560960174053c75616c..5cef0efabd5fe78fd0ef92b218eb518f24fcf597 100644 (file)
  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
+/* \summary: IPv6 Internet Control Message Protocol (ICMPv6) printer */
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#ifdef INET6
-
-#include <tcpdump-stdinc.h>
+#include <netdissect-stdinc.h>
 
 #include <stdio.h>
 #include <string.h>
 
-#include "interface.h"
+#include "netdissect.h"
 #include "addrtoname.h"
+#include "addrtostr.h"
 #include "extract.h"
 
 #include "ip6.h"
 #include "udp.h"
 #include "ah.h"
 
+static const char icmp6_tstr[] = " [|icmp6]";
+static const char rpl_tstr[] = " [|rpl]";
+static const char mldv2_tstr[] = " [|mldv2]";
+
 /*     NetBSD: icmp6.h,v 1.13 2000/08/03 16:30:37 itojun Exp   */
 /*     $KAME: icmp6.h,v 1.22 2000/08/03 15:25:16 jinmei Exp $  */
 
@@ -283,13 +288,13 @@ struct nd_opt_hdr {               /* Neighbor discovery option header */
 #define ND_OPT_DNSSL                   31
 
 struct nd_opt_prefix_info {    /* prefix information */
-       uint8_t         nd_opt_pi_type;
-       uint8_t         nd_opt_pi_len;
-       uint8_t         nd_opt_pi_prefix_len;
-       uint8_t         nd_opt_pi_flags_reserved;
-       uint8_t         nd_opt_pi_valid_time[4];
-       uint8_t         nd_opt_pi_preferred_time[4];
-       uint8_t         nd_opt_pi_reserved2[4];
+       nd_uint8_t              nd_opt_pi_type;
+       nd_uint8_t              nd_opt_pi_len;
+       nd_uint8_t              nd_opt_pi_prefix_len;
+       nd_uint8_t              nd_opt_pi_flags_reserved;
+       nd_uint32_t             nd_opt_pi_valid_time;
+       nd_uint32_t             nd_opt_pi_preferred_time;
+       nd_uint32_t             nd_opt_pi_reserved2;
        struct in6_addr nd_opt_pi_prefix;
 };
 
@@ -621,14 +626,14 @@ print_lladdr(netdissect_options *ndo, const uint8_t *p, size_t l)
        }
 }
 
-static int icmp6_cksum(const struct ip6_hdr *ip6, const struct icmp6_hdr *icp,
-       u_int len)
+static int icmp6_cksum(netdissect_options *ndo, const struct ip6_hdr *ip6,
+       const struct icmp6_hdr *icp, u_int len)
 {
-       return nextproto6_cksum(ip6, (const uint8_t *)(const void *)icp, len, len,
+       return nextproto6_cksum(ndo, ip6, (const uint8_t *)(const void *)icp, len, len,
                                IPPROTO_ICMPV6);
 }
 
-const struct tok rpl_mop_values[] = {
+static const struct tok rpl_mop_values[] = {
         { RPL_DIO_NONSTORING,         "nonstoring"},
         { RPL_DIO_STORING,            "storing"},
         { RPL_DIO_NONSTORING_MULTICAST, "nonstoring-multicast"},
@@ -636,7 +641,7 @@ const struct tok rpl_mop_values[] = {
         { 0, NULL},
 };
 
-const struct tok rpl_subopt_values[] = {
+static const struct tok rpl_subopt_values[] = {
         { RPL_OPT_PAD0, "pad0"},
         { RPL_OPT_PADN, "padN"},
         { RPL_DIO_METRICS, "metrics"},
@@ -649,23 +654,6 @@ const struct tok rpl_subopt_values[] = {
         { 0, NULL},
 };
 
-static void
-rpl_format_dagid(char dagid_str[65], const u_char *dagid)
-{
-        char *d = dagid_str;
-        int  i;
-
-        for(i=0;i<16;i++) {
-                if(isprint(dagid[i])) {
-                        *d++ = dagid[i];
-                } else {
-                        snprintf(d,5,"0x%02x", dagid[i]); /* 4 + null char */
-                        d += 4;
-                }
-        }
-        *d++ = '\0';
-}
-
 static void
 rpl_dio_printopt(netdissect_options *ndo,
                  const struct rpl_dio_genoption *opt,
@@ -686,7 +674,7 @@ rpl_dio_printopt(netdissect_options *ndo,
                         ND_PRINT((ndo, " opt:pad0"));
                 } else {
                         ND_PRINT((ndo, " opt:%s len:%u ",
-                                  tok2str(rpl_subopt_values, "%subopt:%u", opt->rpl_dio_type),
+                                  tok2str(rpl_subopt_values, "subopt:%u", opt->rpl_dio_type),
                                   optlen));
                         if(ndo->ndo_vflag > 2) {
                                 unsigned int paylen = opt->rpl_dio_len;
@@ -699,10 +687,11 @@ rpl_dio_printopt(netdissect_options *ndo,
                 }
                 opt = (const struct rpl_dio_genoption *)(((const char *)opt) + optlen);
                 length -= optlen;
+                ND_TCHECK(opt->rpl_dio_len);
         }
         return;
 trunc:
-       ND_PRINT((ndo," [|truncated]"));
+       ND_PRINT((ndo, "%s", rpl_tstr));
        return;
 }
 
@@ -711,10 +700,10 @@ rpl_dio_print(netdissect_options *ndo,
               const u_char *bp, u_int length)
 {
         const struct nd_rpl_dio *dio = (const struct nd_rpl_dio *)bp;
-        char dagid_str[65];
+        const char *dagid_str;
 
         ND_TCHECK(*dio);
-        rpl_format_dagid(dagid_str, dio->rpl_dagid);
+        dagid_str = ip6addr_string (ndo, dio->rpl_dagid);
 
         ND_PRINT((ndo, " [dagid:%s,seq:%u,instance:%u,rank:%u,%smop:%s,prf:%u]",
                   dagid_str,
@@ -731,7 +720,7 @@ rpl_dio_print(netdissect_options *ndo,
         }
        return;
 trunc:
-       ND_PRINT((ndo," [|truncated]"));
+       ND_PRINT((ndo, "%s", rpl_tstr));
        return;
 }
 
@@ -740,20 +729,19 @@ rpl_dao_print(netdissect_options *ndo,
               const u_char *bp, u_int length)
 {
         const struct nd_rpl_dao *dao = (const struct nd_rpl_dao *)bp;
-        char dagid_str[65];
+        const char *dagid_str = "<elided>";
 
         ND_TCHECK(*dao);
         if (length < ND_RPL_DAO_MIN_LEN)
                goto tooshort;
 
-        strcpy(dagid_str,"<elided>");
         bp += ND_RPL_DAO_MIN_LEN;
         length -= ND_RPL_DAO_MIN_LEN;
         if(RPL_DAO_D(dao->rpl_flags)) {
                 ND_TCHECK2(dao->rpl_dagid, DAGID_LEN);
                 if (length < DAGID_LEN)
                        goto tooshort;
-                rpl_format_dagid(dagid_str, dao->rpl_dagid);
+                dagid_str = ip6addr_string (ndo, dao->rpl_dagid);
                 bp += DAGID_LEN;
                 length -= DAGID_LEN;
         }
@@ -773,7 +761,7 @@ rpl_dao_print(netdissect_options *ndo,
        return;
 
 trunc:
-       ND_PRINT((ndo," [|truncated]"));
+       ND_PRINT((ndo, "%s", rpl_tstr));
        return;
 
 tooshort:
@@ -786,20 +774,19 @@ rpl_daoack_print(netdissect_options *ndo,
                  const u_char *bp, u_int length)
 {
         const struct nd_rpl_daoack *daoack = (const struct nd_rpl_daoack *)bp;
-        char dagid_str[65];
+        const char *dagid_str = "<elided>";
 
         ND_TCHECK2(*daoack, ND_RPL_DAOACK_MIN_LEN);
         if (length < ND_RPL_DAOACK_MIN_LEN)
                goto tooshort;
 
-        strcpy(dagid_str,"<elided>");
         bp += ND_RPL_DAOACK_MIN_LEN;
         length -= ND_RPL_DAOACK_MIN_LEN;
         if(RPL_DAOACK_D(daoack->rpl_flags)) {
-                ND_TCHECK2(daoack->rpl_dagid, 16);
+                ND_TCHECK2(daoack->rpl_dagid, DAGID_LEN);
                 if (length < DAGID_LEN)
                        goto tooshort;
-                rpl_format_dagid(dagid_str, daoack->rpl_dagid);
+                dagid_str = ip6addr_string (ndo, daoack->rpl_dagid);
                 bp += DAGID_LEN;
                 length -= DAGID_LEN;
         }
@@ -818,7 +805,7 @@ rpl_daoack_print(netdissect_options *ndo,
        return;
 
 trunc:
-       ND_PRINT((ndo," [|dao-truncated]"));
+       ND_PRINT((ndo, "%s", rpl_tstr));
        return;
 
 tooshort:
@@ -877,7 +864,7 @@ rpl_print(netdissect_options *ndo,
 
 #if 0
 trunc:
-       ND_PRINT((ndo," [|truncated]"));
+       ND_PRINT((ndo, "%s", rpl_tstr));
        return;
 #endif
 
@@ -909,7 +896,7 @@ icmp6_print(netdissect_options *ndo,
 
                if (ND_TTEST2(bp[0], length)) {
                        udp_sum = EXTRACT_16BITS(&dp->icmp6_cksum);
-                       sum = icmp6_cksum(ip, dp, length);
+                       sum = icmp6_cksum(ndo, ip, dp, length);
                        if (sum != 0)
                                ND_PRINT((ndo,"[bad icmp6 cksum 0x%04x -> 0x%04x!] ",
                                                 udp_sum,
@@ -957,12 +944,12 @@ icmp6_print(netdissect_options *ndo,
                        case IPPROTO_TCP:
                                ND_PRINT((ndo,", %s tcp port %s",
                                        ip6addr_string(ndo, &oip->ip6_dst),
-                                          tcpport_string(dport)));
+                                          tcpport_string(ndo, dport)));
                                break;
                        case IPPROTO_UDP:
                                ND_PRINT((ndo,", %s udp port %s",
                                        ip6addr_string(ndo, &oip->ip6_dst),
-                                          udpport_string(dport)));
+                                          udpport_string(ndo, dport)));
                                break;
                        default:
                                ND_PRINT((ndo,", %s protocol %d port %d unreachable",
@@ -1052,7 +1039,7 @@ icmp6_print(netdissect_options *ndo,
                        p = (const struct nd_router_advert *)dp;
                        ND_TCHECK(p->nd_ra_retransmit);
                        ND_PRINT((ndo,"\n\thop limit %u, Flags [%s]" \
-                                  ", pref %s, router lifetime %us, reachable time %us, retrans time %us",
+                                  ", pref %s, router lifetime %us, reachable time %ums, retrans timer %ums",
                                   (u_int)p->nd_ra_curhoplimit,
                                   bittok2str(icmp6_opt_ra_flag_values,"none",(p->nd_ra_flags_reserved)),
                                   get_rtpref(p->nd_ra_flags_reserved),
@@ -1100,10 +1087,10 @@ icmp6_print(netdissect_options *ndo,
        case ND_REDIRECT:
 #define RDR(i) ((const struct nd_redirect *)(i))
                          ND_TCHECK(RDR(dp)->nd_rd_dst);
-                         ND_PRINT((ndo,", %s", getname6(ndo, (const u_char *)&RDR(dp)->nd_rd_dst)));
+                         ND_PRINT((ndo,", %s", ip6addr_string(ndo, &RDR(dp)->nd_rd_dst)));
                ND_TCHECK(RDR(dp)->nd_rd_target);
                ND_PRINT((ndo," to %s",
-                          getname6(ndo, (const u_char*)&RDR(dp)->nd_rd_target)));
+                          ip6addr_string(ndo, &RDR(dp)->nd_rd_target)));
 #define REDIRECTLEN 40
                if (ndo->ndo_vflag) {
                        icmp6_opt_print(ndo, (const u_char *)dp + REDIRECTLEN,
@@ -1149,6 +1136,7 @@ icmp6_print(netdissect_options *ndo,
                if (ndo->ndo_vflag) {
                        ND_TCHECK(dp->icmp6_data16[0]);
                        ND_PRINT((ndo,", id 0x%04x", EXTRACT_16BITS(&dp->icmp6_data16[0])));
+                       ND_TCHECK(dp->icmp6_data16[1]);
                        if (dp->icmp6_data16[1] & 0xc0)
                                ND_PRINT((ndo," "));
                        if (dp->icmp6_data16[1] & 0x80)
@@ -1174,7 +1162,7 @@ icmp6_print(netdissect_options *ndo,
                 ND_PRINT((ndo,", length %u", length));
        return;
 trunc:
-       ND_PRINT((ndo, "[|icmp6]"));
+       ND_PRINT((ndo, "%s", icmp6_tstr));
 }
 
 static const struct udphdr *
@@ -1398,8 +1386,8 @@ icmp6_opt_print(netdissect_options *ndo, const u_char *bp, int resid)
        }
        return;
 
- trunc:
-       ND_PRINT((ndo, "[ndp opt]"));
+trunc:
+       ND_PRINT((ndo, "%s", icmp6_tstr));
        return;
 #undef ECHECK
 }
@@ -1474,7 +1462,7 @@ mldv2_report_print(netdissect_options *ndo, const u_char *bp, u_int len)
     }
     return;
 trunc:
-    ND_PRINT((ndo,"[|icmp6]"));
+    ND_PRINT((ndo, "%s", mldv2_tstr));
     return;
 }
 
@@ -1540,7 +1528,7 @@ mldv2_query_print(netdissect_options *ndo, const u_char *bp, u_int len)
     ND_PRINT((ndo,"]"));
     return;
 trunc:
-    ND_PRINT((ndo,"[|icmp6]"));
+    ND_PRINT((ndo, "%s", mldv2_tstr));
     return;
 }
 
@@ -1669,7 +1657,7 @@ icmp6_nodeinfo_print(netdissect_options *ndo, u_int icmp6len, const u_char *bp,
                                break;
                        }
                        ND_PRINT((ndo,", subject=%s",
-                                  getname6(ndo, (const u_char *)(ni6 + 1))));
+                                  ip6addr_string(ndo, ni6 + 1)));
                        break;
                case ICMP6_NI_SUBJ_FQDN:
                        ND_PRINT((ndo,", subject=DNS name"));
@@ -1697,7 +1685,7 @@ icmp6_nodeinfo_print(netdissect_options *ndo, u_int icmp6len, const u_char *bp,
                                break;
                        }
                        ND_PRINT((ndo,", subject=%s",
-                                  getname(ndo, (const u_char *)(ni6 + 1))));
+                                  ipaddr_string(ndo, ni6 + 1)));
                        break;
                default:
                        ND_PRINT((ndo,", unknown subject"));
@@ -1716,6 +1704,7 @@ icmp6_nodeinfo_print(netdissect_options *ndo, u_int icmp6len, const u_char *bp,
 
                needcomma = 0;
 
+               ND_TCHECK2(*dp, sizeof(*ni6));
                ni6 = (const struct icmp6_nodeinfo *)dp;
                ND_PRINT((ndo," node information reply"));
                ND_PRINT((ndo," ("));   /*)*/
@@ -1770,6 +1759,7 @@ icmp6_nodeinfo_print(netdissect_options *ndo, u_int icmp6len, const u_char *bp,
                                ND_PRINT((ndo,", "));
                        ND_PRINT((ndo,"DNS name"));
                        cp = (const u_char *)(ni6 + 1) + 4;
+                       ND_TCHECK(cp[0]);
                        if (cp[0] == ep - cp - 1) {
                                /* icmp-name-lookup-03, pascal string */
                                if (ndo->ndo_vflag)
@@ -1794,7 +1784,7 @@ icmp6_nodeinfo_print(netdissect_options *ndo, u_int icmp6len, const u_char *bp,
                        while (i < siz) {
                                if (i + sizeof(struct in6_addr) + sizeof(int32_t) > siz)
                                        break;
-                               ND_PRINT((ndo," %s", getname6(ndo, bp + i)));
+                               ND_PRINT((ndo," %s", ip6addr_string(ndo, bp + i)));
                                i += sizeof(struct in6_addr);
                                ND_PRINT((ndo,"(%d)", (int32_t)EXTRACT_32BITS(bp + i)));
                                i += sizeof(int32_t);
@@ -1825,7 +1815,7 @@ icmp6_nodeinfo_print(netdissect_options *ndo, u_int icmp6len, const u_char *bp,
        return;
 
 trunc:
-       ND_PRINT((ndo, "[|icmp6]"));
+       ND_PRINT((ndo, "%s", icmp6_tstr));
 }
 
 static void
@@ -1903,7 +1893,7 @@ icmp6_rrenum_print(netdissect_options *ndo, const u_char *bp, const u_char *ep)
                        ND_PRINT((ndo,",min=%u", match->rpm_minlen));
                        ND_PRINT((ndo,",max=%u", match->rpm_maxlen));
                }
-               if (inet_ntop(AF_INET6, &match->rpm_prefix, hbuf, sizeof(hbuf)))
+               if (addrtostr6(&match->rpm_prefix, hbuf, sizeof(hbuf)))
                        ND_PRINT((ndo,",%s/%u", hbuf, match->rpm_matchlen));
                else
                        ND_PRINT((ndo,",?/%u", match->rpm_matchlen));
@@ -1946,8 +1936,7 @@ icmp6_rrenum_print(netdissect_options *ndo, const u_char *bp, const u_char *ep)
                                        ND_PRINT((ndo,"pltime=%u,",
                                                   EXTRACT_32BITS(&use->rpu_pltime)));
                        }
-                       if (inet_ntop(AF_INET6, &use->rpu_prefix, hbuf,
-                           sizeof(hbuf)))
+                       if (addrtostr6(&use->rpu_prefix, hbuf, sizeof(hbuf)))
                                ND_PRINT((ndo,"%s/%u/%u", hbuf, use->rpu_uselen,
                                           use->rpu_keeplen));
                        else
@@ -1961,11 +1950,9 @@ icmp6_rrenum_print(netdissect_options *ndo, const u_char *bp, const u_char *ep)
        return;
 
 trunc:
-       ND_PRINT((ndo,"[|icmp6]"));
+       ND_PRINT((ndo, "%s", icmp6_tstr));
 }
 
-#endif /* INET6 */
-
 /*
  * Local Variables:
  * c-style: whitesmith