]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-udld.c
Use more the EXTRACT_U_1() macro (31/n)
[tcpdump] / print-udld.c
index 15e2bf6fae492f864fae592fe021f2e8a4c6001e..94c325667699b792f59fee37123e3cdf5ecb10f7 100644 (file)
  * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  * FOR A PARTICULAR PURPOSE.
  *
- * UNIDIRECTIONAL LINK DETECTION (UDLD) as per
- * http://www.ietf.org/internet-drafts/draft-foschiano-udld-02.txt
- *
  * Original code by Carles Kishimoto <[email protected]>
  */
 
-#define NETDISSECT_REWORKED
+/* \summary: Cisco UniDirectional Link Detection (UDLD) protocol printer */
+
+/* specification: RFC 5171 */
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#include <tcpdump-stdinc.h>
+#include <netdissect-stdinc.h>
 
-#include "interface.h"
+#include "netdissect.h"
 #include "extract.h"
 
+static const char tstr[] = " [|udld]";
+
 #define UDLD_HEADER_LEN                        4
 #define UDLD_DEVICE_ID_TLV             0x0001
 #define UDLD_PORT_ID_TLV               0x0002
@@ -63,9 +65,10 @@ static const struct tok udld_flags_values[] = {
 };
 
 /*
+ * UDLD's Protocol Data Unit format:
  *
- * 0                   1                   2                   3
- * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+ *  0                   1                   2                   3
+ *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  * | Ver | Opcode  |     Flags     |           Checksum            |
  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
@@ -73,6 +76,18 @@ static const struct tok udld_flags_values[] = {
  * |                              ...                              |
  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  *
+ * TLV format:
+ *
+ *  0                   1                   2                   3
+ *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |             TYPE              |            LENGTH             |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |                             VALUE                             |
+ * |                              ...                              |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ *
+ * LENGTH: Length in bytes of the Type, Length, and Value fields.
  */
 
 #define        UDLD_EXTRACT_VERSION(x) (((x)&0xe0)>>5)
@@ -91,14 +106,14 @@ udld_print (netdissect_options *ndo, const u_char *pptr, u_int length)
 
     ND_TCHECK2(*tptr, UDLD_HEADER_LEN);
 
-    code = UDLD_EXTRACT_OPCODE(*tptr);
+    code = UDLD_EXTRACT_OPCODE(EXTRACT_U_1(tptr));
 
     ND_PRINT((ndo, "UDLDv%u, Code %s (%x), Flags [%s] (0x%02x), length %u",
-           UDLD_EXTRACT_VERSION(*tptr),
+           UDLD_EXTRACT_VERSION(EXTRACT_U_1(tptr)),
            tok2str(udld_code_values, "Reserved", code),
            code,
-           bittok2str(udld_flags_values, "none", *(tptr+1)),
-           *(tptr+1),
+           bittok2str(udld_flags_values, "none", EXTRACT_U_1((tptr + 1))),
+           EXTRACT_U_1((tptr + 1)),
            length));
 
     /*
@@ -108,43 +123,56 @@ udld_print (netdissect_options *ndo, const u_char *pptr, u_int length)
        return;
     }
 
-    ND_PRINT((ndo, "\n\tChecksum 0x%04x (unverified)", EXTRACT_16BITS(tptr+2)));
+    ND_PRINT((ndo, "\n\tChecksum 0x%04x (unverified)", EXTRACT_BE_U_2(tptr + 2)));
 
     tptr += UDLD_HEADER_LEN;
 
     while (tptr < (pptr+length)) {
 
-        ND_TCHECK2(*tptr, 4);
-
-       type = EXTRACT_16BITS(tptr);
-        len  = EXTRACT_16BITS(tptr+2);
-        len -= 4;
-        tptr += 4;
-
-        /* infinite loop check */
-        if (type == 0 || len == 0) {
-            return;
-        }
+        ND_TCHECK_4(tptr);
+       type = EXTRACT_BE_U_2(tptr);
+        len  = EXTRACT_BE_U_2(tptr + 2);
 
         ND_PRINT((ndo, "\n\t%s (0x%04x) TLV, length %u",
                tok2str(udld_tlv_values, "Unknown", type),
                type, len));
 
+        if (type == 0)
+            goto invalid;
+
+        /* infinite loop check */
+        if (len <= 4)
+            goto invalid;
+
+        len -= 4;
+        tptr += 4;
+
+        ND_TCHECK2(*tptr, len);
+
         switch (type) {
         case UDLD_DEVICE_ID_TLV:
         case UDLD_PORT_ID_TLV:
-        case UDLD_ECHO_TLV:
         case UDLD_DEVICE_NAME_TLV:
-            ND_PRINT((ndo, ", %s", tptr));
+            ND_PRINT((ndo, ", "));
+            fn_printzp(ndo, tptr, len, NULL);
+            break;
+
+        case UDLD_ECHO_TLV:
+            ND_PRINT((ndo, ", "));
+            (void)fn_printn(ndo, tptr, len, NULL);
             break;
 
         case UDLD_MESSAGE_INTERVAL_TLV:
         case UDLD_TIMEOUT_INTERVAL_TLV:
-            ND_PRINT((ndo, ", %us", (*tptr)));
+            if (len != 1)
+                goto invalid;
+            ND_PRINT((ndo, ", %us", (EXTRACT_U_1(tptr))));
             break;
 
         case UDLD_SEQ_NUMBER_TLV:
-            ND_PRINT((ndo, ", %u", EXTRACT_32BITS(tptr)));
+            if (len != 4)
+                goto invalid;
+            ND_PRINT((ndo, ", %u", EXTRACT_BE_U_4(tptr)));
             break;
 
         default:
@@ -155,8 +183,11 @@ udld_print (netdissect_options *ndo, const u_char *pptr, u_int length)
 
     return;
 
- trunc:
-    ND_PRINT((ndo, "[|udld]"));
+invalid:
+    ND_PRINT((ndo, "%s", istr));
+    return;
+trunc:
+    ND_PRINT((ndo, "%s", tstr));
 }
 
 /*