]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-dhcp6.c
On Solaris, for 64-bit builds, use the 64-bit pcap-config.
[tcpdump] / print-dhcp6.c
index 5e8585c0578482f1b281c6b42eca4874855de2f9..6b0dc21335d31c432c3e97ccf77d5a72b695a155 100644 (file)
  */
 
 #ifdef HAVE_CONFIG_H
-#include "config.h"
+#include <config.h>
 #endif
 
-#include <netdissect-stdinc.h>
-
-#include <stdio.h>
-#include <string.h>
+#include "netdissect-stdinc.h"
 
 #include "netdissect.h"
 #include "addrtoname.h"
@@ -105,13 +102,11 @@ static const struct tok dh6_msgtype_str[] = {
 /* DHCP6 base packet format */
 struct dhcp6 {
        union {
-               nd_uint8_t m;
-               nd_uint32_t x;
+               nd_uint8_t msgtype;
+               nd_uint32_t xid;
        } dh6_msgtypexid;
        /* options follow */
 };
-#define dh6_msgtype    dh6_msgtypexid.m
-#define dh6_xid                dh6_msgtypexid.x
 #define DH6_XIDMASK    0x00ffffff
 
 /* DHCPv6 relay messages */
@@ -282,15 +277,15 @@ dhcp6opt_print(netdissect_options *ndo,
 {
        const struct dhcp6opt *dh6o;
        const u_char *tp;
-       size_t i;
+       u_int i;
        uint16_t opttype;
-       size_t optlen;
+       uint16_t optlen;
        uint8_t auth_proto;
        uint8_t auth_alg;
        uint8_t auth_rdm;
        u_int authinfolen, authrealmlen;
-       int remain_len;  /* Length of remaining options */
-       int label_len;   /* Label length */
+       u_int remain_len;  /* Length of remaining options */
+       u_int label_len;   /* Label length */
        uint16_t subopt_code;
        uint16_t subopt_len;
        uint8_t dh6_reconf_type;
@@ -303,10 +298,10 @@ dhcp6opt_print(netdissect_options *ndo,
                        goto trunc;
                dh6o = (const struct dhcp6opt *)cp;
                ND_TCHECK_SIZE(dh6o);
-               optlen = EXTRACT_BE_U_2(&dh6o->dh6opt_len);
+               optlen = GET_BE_U_2(dh6o->dh6opt_len);
                if (ep < cp + sizeof(*dh6o) + optlen)
                        goto trunc;
-               opttype = EXTRACT_BE_U_2(&dh6o->dh6opt_type);
+               opttype = GET_BE_U_2(dh6o->dh6opt_type);
                ND_PRINT(" (%s", tok2str(dh6opt_str, "opt_%u", opttype));
                ND_TCHECK_LEN(cp + sizeof(*dh6o), optlen);
                switch (opttype) {
@@ -318,14 +313,15 @@ dhcp6opt_print(netdissect_options *ndo,
                                break;
                        }
                        tp = (const u_char *)(dh6o + 1);
-                       switch (EXTRACT_BE_U_2(tp)) {
+                       switch (GET_BE_U_2(tp)) {
                        case 1:
                                if (optlen >= 2 + 6) {
                                        ND_PRINT(" hwaddr/time type %u time %u ",
-                                           EXTRACT_BE_U_2(tp + 2),
-                                           EXTRACT_BE_U_4(tp + 4));
+                                           GET_BE_U_2(tp + 2),
+                                           GET_BE_U_4(tp + 4));
                                        for (i = 8; i < optlen; i++)
-                                               ND_PRINT("%02x", EXTRACT_U_1(tp + i));
+                                               ND_PRINT("%02x",
+                                                        GET_U_1(tp + i));
                                        /*(*/
                                        ND_PRINT(")");
                                } else {
@@ -337,7 +333,8 @@ dhcp6opt_print(netdissect_options *ndo,
                                if (optlen >= 2 + 8) {
                                        ND_PRINT(" vid ");
                                        for (i = 2; i < 2 + 8; i++)
-                                               ND_PRINT("%02x", EXTRACT_U_1(tp + i));
+                                               ND_PRINT("%02x",
+                                                        GET_U_1(tp + i));
                                        /*(*/
                                        ND_PRINT(")");
                                } else {
@@ -348,9 +345,10 @@ dhcp6opt_print(netdissect_options *ndo,
                        case 3:
                                if (optlen >= 2 + 2) {
                                        ND_PRINT(" hwaddr type %u ",
-                                           EXTRACT_BE_U_2(tp + 2));
+                                           GET_BE_U_2(tp + 2));
                                        for (i = 4; i < optlen; i++)
-                                               ND_PRINT("%02x", EXTRACT_U_1(tp + i));
+                                               ND_PRINT("%02x",
+                                                        GET_U_1(tp + i));
                                        /*(*/
                                        ND_PRINT(")");
                                } else {
@@ -359,7 +357,7 @@ dhcp6opt_print(netdissect_options *ndo,
                                }
                                break;
                        default:
-                               ND_PRINT(" type %d)", EXTRACT_BE_U_2(tp));
+                               ND_PRINT(" type %u)", GET_BE_U_2(tp));
                                break;
                        }
                        break;
@@ -370,10 +368,10 @@ dhcp6opt_print(netdissect_options *ndo,
                                break;
                        }
                        tp = (const u_char *)(dh6o + 1);
-                       ND_PRINT(" %s", ip6addr_string(ndo, tp));
+                       ND_PRINT(" %s", GET_IP6ADDR_STRING(tp));
                        ND_PRINT(" pltime:%u vltime:%u",
-                           EXTRACT_BE_U_4(tp + 16),
-                           EXTRACT_BE_U_4(tp + 20));
+                           GET_BE_U_4(tp + 16),
+                           GET_BE_U_4(tp + 20));
                        if (optlen > 24) {
                                /* there are sub-options */
                                dhcp6opt_print(ndo, tp + 24, tp + optlen);
@@ -389,7 +387,7 @@ dhcp6opt_print(netdissect_options *ndo,
                        tp = (const u_char *)(dh6o + 1);
                        for (i = 0; i < optlen; i += 2) {
                                ND_PRINT(" %s",
-                                   tok2str(dh6opt_str, "opt_%u", EXTRACT_BE_U_2(tp + i)));
+                                   tok2str(dh6opt_str, "opt_%u", GET_BE_U_2(tp + i)));
                        }
                        ND_PRINT(")");
                        break;
@@ -399,7 +397,7 @@ dhcp6opt_print(netdissect_options *ndo,
                                break;
                        }
                        tp = (const u_char *)(dh6o + 1);
-                       ND_PRINT(" %d)", EXTRACT_U_1(tp));
+                       ND_PRINT(" %u)", GET_U_1(tp));
                        break;
                case DH6OPT_ELAPSED_TIME:
                        if (optlen != 2) {
@@ -407,21 +405,36 @@ dhcp6opt_print(netdissect_options *ndo,
                                break;
                        }
                        tp = (const u_char *)(dh6o + 1);
-                       ND_PRINT(" %d)", EXTRACT_BE_U_2(tp));
+                       ND_PRINT(" %u)", GET_BE_U_2(tp));
                        break;
                case DH6OPT_RELAY_MSG:
+                   {
+                       const u_char *snapend_save;
+
                        ND_PRINT(" (");
                        tp = (const u_char *)(dh6o + 1);
+                       /*
+                        * Update the snapend to the end of the option before
+                        * calling recursively dhcp6_print() for the nested
+                        * packet. Other options may be present after the
+                        * nested DHCPv6 packet. This prevents that, in
+                        * dhcp6_print(), for the nested DHCPv6 packet, the
+                        * remaining length < remaining caplen.
+                        */
+                       snapend_save = ndo->ndo_snapend;
+                       ndo->ndo_snapend = ND_MIN(tp + optlen, ndo->ndo_snapend);
                        dhcp6_print(ndo, tp, optlen);
+                       ndo->ndo_snapend = snapend_save;
                        ND_PRINT(")");
                        break;
+                   }
                case DH6OPT_AUTH:
                        if (optlen < 11) {
                                ND_PRINT(" ?)");
                                break;
                        }
                        tp = (const u_char *)(dh6o + 1);
-                       auth_proto = EXTRACT_U_1(tp);
+                       auth_proto = GET_U_1(tp);
                        switch (auth_proto) {
                        case DH6OPT_AUTHPROTO_DELAYED:
                                ND_PRINT(" proto: delayed");
@@ -430,34 +443,34 @@ dhcp6opt_print(netdissect_options *ndo,
                                ND_PRINT(" proto: reconfigure");
                                break;
                        default:
-                               ND_PRINT(" proto: %d", auth_proto);
+                               ND_PRINT(" proto: %u", auth_proto);
                                break;
                        }
                        tp++;
-                       auth_alg = EXTRACT_U_1(tp);
+                       auth_alg = GET_U_1(tp);
                        switch (auth_alg) {
                        case DH6OPT_AUTHALG_HMACMD5:
                                /* XXX: may depend on the protocol */
                                ND_PRINT(", alg: HMAC-MD5");
                                break;
                        default:
-                               ND_PRINT(", alg: %d", auth_alg);
+                               ND_PRINT(", alg: %u", auth_alg);
                                break;
                        }
                        tp++;
-                       auth_rdm = EXTRACT_U_1(tp);
+                       auth_rdm = GET_U_1(tp);
                        switch (auth_rdm) {
                        case DH6OPT_AUTHRDM_MONOCOUNTER:
                                ND_PRINT(", RDM: mono");
                                break;
                        default:
-                               ND_PRINT(", RDM: %d", auth_rdm);
+                               ND_PRINT(", RDM: %u", auth_rdm);
                                break;
                        }
                        tp++;
                        ND_PRINT(", RD:");
                        for (i = 0; i < 4; i++, tp += 2)
-                               ND_PRINT(" %04x", EXTRACT_BE_U_2(tp));
+                               ND_PRINT(" %04x", GET_BE_U_2(tp));
 
                        /* protocol dependent part */
                        authinfolen = optlen - 11;
@@ -474,19 +487,19 @@ dhcp6opt_print(netdissect_options *ndo,
                                        ND_PRINT(", realm: ");
                                }
                                for (i = 0; i < authrealmlen; i++, tp++)
-                                       ND_PRINT("%02x", EXTRACT_U_1(tp));
-                               ND_PRINT(", key ID: %08x", EXTRACT_BE_U_4(tp));
+                                       ND_PRINT("%02x", GET_U_1(tp));
+                               ND_PRINT(", key ID: %08x", GET_BE_U_4(tp));
                                tp += 4;
                                ND_PRINT(", HMAC-MD5:");
                                for (i = 0; i < 4; i++, tp+= 4)
-                                       ND_PRINT(" %08x", EXTRACT_BE_U_4(tp));
+                                       ND_PRINT(" %08x", GET_BE_U_4(tp));
                                break;
                        case DH6OPT_AUTHPROTO_RECONFIG:
                                if (authinfolen != 17) {
                                        ND_PRINT(" ??");
                                        break;
                                }
-                               switch (EXTRACT_U_1(tp)) {
+                               switch (GET_U_1(tp)) {
                                case DH6OPT_AUTHRECONFIG_KEY:
                                        ND_PRINT(" reconfig-key");
                                        break;
@@ -500,7 +513,7 @@ dhcp6opt_print(netdissect_options *ndo,
                                tp++;
                                ND_PRINT(" value:");
                                for (i = 0; i < 4; i++, tp+= 4)
-                                       ND_PRINT(" %08x", EXTRACT_BE_U_4(tp));
+                                       ND_PRINT(" %08x", GET_BE_U_4(tp));
                                break;
                        default:
                                ND_PRINT(" ??");
@@ -521,7 +534,7 @@ dhcp6opt_print(netdissect_options *ndo,
                        tp = (const u_char *)(dh6o + 1);
                        ND_PRINT(" ");
                        for (i = 0; i < optlen && i < 10; i++)
-                               ND_PRINT("%02x", EXTRACT_U_1(tp + i));
+                               ND_PRINT("%02x", GET_U_1(tp + i));
                        ND_PRINT("...)");
                        break;
                case DH6OPT_RECONF_MSG:
@@ -530,7 +543,7 @@ dhcp6opt_print(netdissect_options *ndo,
                                break;
                        }
                        tp = (const u_char *)(dh6o + 1);
-                       dh6_reconf_type = EXTRACT_U_1(tp);
+                       dh6_reconf_type = GET_U_1(tp);
                        switch (dh6_reconf_type) {
                        case DH6_RENEW:
                                ND_PRINT(" for renew)");
@@ -560,7 +573,7 @@ dhcp6opt_print(netdissect_options *ndo,
                        }
                        tp = (const u_char *)(dh6o + 1);
                        for (i = 0; i < optlen; i += 16)
-                               ND_PRINT(" %s", ip6addr_string(ndo, tp + i));
+                               ND_PRINT(" %s", GET_IP6ADDR_STRING(tp + i));
                        ND_PRINT(")");
                        break;
                case DH6OPT_SIP_SERVER_D:
@@ -568,7 +581,7 @@ dhcp6opt_print(netdissect_options *ndo,
                        tp = (const u_char *)(dh6o + 1);
                        while (tp < cp + sizeof(*dh6o) + optlen) {
                                ND_PRINT(" ");
-                               if ((tp = ns_nprint(ndo, tp, cp + sizeof(*dh6o) + optlen)) == NULL)
+                               if ((tp = fqdn_print(ndo, tp, cp + sizeof(*dh6o) + optlen)) == NULL)
                                        goto trunc;
                        }
                        ND_PRINT(")");
@@ -579,7 +592,7 @@ dhcp6opt_print(netdissect_options *ndo,
                                break;
                        }
                        tp = (const u_char *)(dh6o + 1);
-                       ND_PRINT(" %s)", dhcp6stcode(EXTRACT_BE_U_2(tp)));
+                       ND_PRINT(" %s)", dhcp6stcode(GET_BE_U_2(tp)));
                        break;
                case DH6OPT_IA_NA:
                case DH6OPT_IA_PD:
@@ -589,9 +602,9 @@ dhcp6opt_print(netdissect_options *ndo,
                        }
                        tp = (const u_char *)(dh6o + 1);
                        ND_PRINT(" IAID:%u T1:%u T2:%u",
-                           EXTRACT_BE_U_4(tp),
-                           EXTRACT_BE_U_4(tp + 4),
-                           EXTRACT_BE_U_4(tp + 8));
+                           GET_BE_U_4(tp),
+                           GET_BE_U_4(tp + 4),
+                           GET_BE_U_4(tp + 8));
                        if (optlen > 12) {
                                /* there are sub-options */
                                dhcp6opt_print(ndo, tp + 12, tp + optlen);
@@ -604,7 +617,7 @@ dhcp6opt_print(netdissect_options *ndo,
                                break;
                        }
                        tp = (const u_char *)(dh6o + 1);
-                       ND_PRINT(" IAID:%u", EXTRACT_BE_U_4(tp));
+                       ND_PRINT(" IAID:%u", GET_BE_U_4(tp));
                        if (optlen > 4) {
                                /* there are sub-options */
                                dhcp6opt_print(ndo, tp + 4, tp + optlen);
@@ -617,10 +630,11 @@ dhcp6opt_print(netdissect_options *ndo,
                                break;
                        }
                        tp = (const u_char *)(dh6o + 1);
-                       ND_PRINT(" %s/%d", ip6addr_string(ndo, tp + 9), EXTRACT_U_1(tp + 8));
+                       ND_PRINT(" %s/%u", GET_IP6ADDR_STRING(tp + 9),
+                                GET_U_1(tp + 8));
                        ND_PRINT(" pltime:%u vltime:%u",
-                           EXTRACT_BE_U_4(tp),
-                           EXTRACT_BE_U_4(tp + 4));
+                           GET_BE_U_4(tp),
+                           GET_BE_U_4(tp + 4));
                        if (optlen > 25) {
                                /* there are sub-options */
                                dhcp6opt_print(ndo, tp + 25, tp + optlen);
@@ -634,7 +648,7 @@ dhcp6opt_print(netdissect_options *ndo,
                                break;
                        }
                        tp = (const u_char *)(dh6o + 1);
-                       ND_PRINT(" %d)", EXTRACT_BE_U_4(tp));
+                       ND_PRINT(" %u)", GET_BE_U_4(tp));
                        break;
                case DH6OPT_REMOTE_ID:
                        if (optlen < 4) {
@@ -642,12 +656,12 @@ dhcp6opt_print(netdissect_options *ndo,
                                break;
                        }
                        tp = (const u_char *)(dh6o + 1);
-                       ND_PRINT(" %d ", EXTRACT_BE_U_4(tp));
+                       ND_PRINT(" %u ", GET_BE_U_4(tp));
                        /*
                         * Print hex dump first 10 characters.
                         */
                        for (i = 4; i < optlen && i < 14; i++)
-                               ND_PRINT("%02x", EXTRACT_U_1(tp + i));
+                               ND_PRINT("%02x", GET_U_1(tp + i));
                        ND_PRINT("...)");
                        break;
                case DH6OPT_LQ_QUERY:
@@ -656,7 +670,7 @@ dhcp6opt_print(netdissect_options *ndo,
                                break;
                        }
                        tp = (const u_char *)(dh6o + 1);
-                       dh6_lq_query_type = EXTRACT_U_1(tp);
+                       dh6_lq_query_type = GET_U_1(tp);
                        switch (dh6_lq_query_type) {
                        case 1:
                                ND_PRINT(" by-address");
@@ -668,7 +682,7 @@ dhcp6opt_print(netdissect_options *ndo,
                                ND_PRINT(" type_%u", dh6_lq_query_type);
                                break;
                        }
-                       ND_PRINT(" %s", ip6addr_string(ndo, tp + 1));
+                       ND_PRINT(" %s", GET_IP6ADDR_STRING(tp + 1));
                        if (optlen > 17) {
                                /* there are query-options */
                                dhcp6opt_print(ndo, tp + 17, tp + optlen);
@@ -689,12 +703,12 @@ dhcp6opt_print(netdissect_options *ndo,
                                break;
                        }
                        tp = (const u_char *)(dh6o + 1);
-                       ND_PRINT(" %s ", ip6addr_string(ndo, tp));
+                       ND_PRINT(" %s ", GET_IP6ADDR_STRING(tp));
                        /*
                         * Print hex dump first 10 characters.
                         */
                        for (i = 16; i < optlen && i < 26; i++)
-                               ND_PRINT("%02x", EXTRACT_U_1(tp + i));
+                               ND_PRINT("%02x", GET_U_1(tp + i));
                        ND_PRINT("...)");
                        break;
                case DH6OPT_NTP_SERVER:
@@ -704,13 +718,13 @@ dhcp6opt_print(netdissect_options *ndo,
                        }
                        tp = (const u_char *)(dh6o + 1);
                        while (tp < cp + sizeof(*dh6o) + optlen - 4) {
-                               subopt_code = EXTRACT_BE_U_2(tp);
+                               subopt_code = GET_BE_U_2(tp);
                                tp += 2;
-                               subopt_len = EXTRACT_BE_U_2(tp);
+                               subopt_len = GET_BE_U_2(tp);
                                tp += 2;
                                if (tp + subopt_len > cp + sizeof(*dh6o) + optlen)
                                        goto trunc;
-                               ND_PRINT(" subopt:%d", subopt_code);
+                               ND_PRINT(" subopt:%u", subopt_code);
                                switch (subopt_code) {
                                case DH6OPT_NTP_SUBOPTION_SRV_ADDR:
                                case DH6OPT_NTP_SUBOPTION_MC_ADDR:
@@ -718,11 +732,11 @@ dhcp6opt_print(netdissect_options *ndo,
                                                ND_PRINT(" ?");
                                                break;
                                        }
-                                       ND_PRINT(" %s", ip6addr_string(ndo, tp));
+                                       ND_PRINT(" %s", GET_IP6ADDR_STRING(tp));
                                        break;
                                case DH6OPT_NTP_SUBOPTION_SRV_FQDN:
                                        ND_PRINT(" ");
-                                       if (ns_nprint(ndo, tp, tp + subopt_len) == NULL)
+                                       if (fqdn_print(ndo, tp, tp + subopt_len) == NULL)
                                                goto trunc;
                                        break;
                                default:
@@ -742,14 +756,14 @@ dhcp6opt_print(netdissect_options *ndo,
                        remain_len = optlen;
                        ND_PRINT(" ");
                        /* Encoding is described in section 3.1 of RFC 1035 */
-                       while (remain_len && EXTRACT_U_1(tp)) {
-                               label_len = EXTRACT_U_1(tp);
+                       while (remain_len && GET_U_1(tp)) {
+                               label_len = GET_U_1(tp);
                                tp++;
                                if (label_len < remain_len - 1) {
-                                       (void)fn_printn(ndo, tp, label_len, NULL);
+                                       nd_printjnp(ndo, tp, label_len);
                                        tp += label_len;
                                        remain_len -= (label_len + 1);
-                                       if(EXTRACT_U_1(tp)) ND_PRINT(".");
+                                       if(GET_U_1(tp)) ND_PRINT(".");
                                } else {
                                        ND_PRINT(" ?");
                                        break;
@@ -765,8 +779,8 @@ dhcp6opt_print(netdissect_options *ndo,
                                break;
                        }
                        tp = (const u_char *)(dh6o + 1);
-                       ND_PRINT("=");
-                       (void)fn_printn(ndo, tp, (u_int)optlen, NULL);
+                       ND_PRINT(" ");
+                       nd_printjnp(ndo, tp, optlen);
                        ND_PRINT(")");
                        break;
 
@@ -780,7 +794,7 @@ dhcp6opt_print(netdissect_options *ndo,
        return;
 
 trunc:
-       ND_PRINT("[|dhcp6ext]");
+       nd_print_trunc(ndo);
 }
 
 /*
@@ -797,16 +811,17 @@ dhcp6_print(netdissect_options *ndo,
        const u_char *extp;
        const char *name;
 
+       ndo->ndo_protocol = "dhcp6";
        ND_PRINT("dhcp6");
 
-       ep = (const u_char *)ndo->ndo_snapend;
+       ep = ndo->ndo_snapend;
        if (cp + length < ep)
                ep = cp + length;
 
        dh6 = (const struct dhcp6 *)cp;
        dh6relay = (const struct dhcp6_relay *)cp;
-       ND_TCHECK(dh6->dh6_xid);
-       msgtype = EXTRACT_U_1(dh6->dh6_msgtype);
+       ND_TCHECK_4(dh6->dh6_msgtypexid.xid);
+       msgtype = GET_U_1(dh6->dh6_msgtypexid.msgtype);
        name = tok2str(dh6_msgtype_str, "msgtype-%u", msgtype);
 
        if (!ndo->ndo_vflag) {
@@ -818,19 +833,14 @@ dhcp6_print(netdissect_options *ndo,
 
        ND_PRINT(" %s (", name);        /*)*/
        if (msgtype != DH6_RELAY_FORW && msgtype != DH6_RELAY_REPLY) {
-               ND_PRINT("xid=%x", EXTRACT_BE_U_4(dh6->dh6_xid) & DH6_XIDMASK);
+               ND_PRINT("xid=%x",
+                        GET_BE_U_4(dh6->dh6_msgtypexid.xid) & DH6_XIDMASK);
                extp = (const u_char *)(dh6 + 1);
                dhcp6opt_print(ndo, extp, ep);
        } else {                /* relay messages */
-               struct in6_addr addr6;
-
-               ND_TCHECK(dh6relay->dh6relay_peeraddr);
-
-               memcpy(&addr6, &dh6relay->dh6relay_linkaddr, sizeof (addr6));
-               ND_PRINT("linkaddr=%s", ip6addr_string(ndo, &addr6));
+               ND_PRINT("linkaddr=%s", GET_IP6ADDR_STRING(dh6relay->dh6relay_linkaddr));
 
-               memcpy(&addr6, &dh6relay->dh6relay_peeraddr, sizeof (addr6));
-               ND_PRINT(" peeraddr=%s", ip6addr_string(ndo, &addr6));
+               ND_PRINT(" peeraddr=%s", GET_IP6ADDR_STRING(dh6relay->dh6relay_peeraddr));
 
                dhcp6opt_print(ndo, (const u_char *)(dh6relay + 1), ep);
        }
@@ -839,5 +849,5 @@ dhcp6_print(netdissect_options *ndo,
        return;
 
 trunc:
-       ND_PRINT("[|dhcp6]");
+       nd_print_trunc(ndo);
 }