]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-mptcp.c
Use more the ND_TTEST_1() macro
[tcpdump] / print-mptcp.c
index a37a59d9a7fa57293a4027f93e8848f27744bda7..db9d7bd5644d0c0e490a367e760807910218d095 100644 (file)
  * SUCH DAMAGE.
  */
 
+/* \summary: Multipath TCP (MPTCP) printer */
+
+/* specification: RFC 6824 */
+
 #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"
 #include "addrtoname.h"
 
@@ -174,7 +178,7 @@ mp_capable_print(netdissect_options *ndo,
 {
         const struct mp_capable *mpc = (const struct mp_capable *) opt;
 
-        if (!(opt_len == 12 && flags & TH_SYN) &&
+        if (!(opt_len == 12 && (flags & TH_SYN)) &&
             !(opt_len == 20 && (flags & (TH_SYN | TH_ACK)) == TH_ACK))
                 return 0;
 
@@ -185,9 +189,9 @@ mp_capable_print(netdissect_options *ndo,
 
         if (mpc->flags & MP_CAPABLE_C)
                 ND_PRINT((ndo, " csum"));
-        ND_PRINT((ndo, " {0x%" PRIx64, EXTRACT_64BITS(mpc->sender_key)));
+        ND_PRINT((ndo, " {0x%" PRIx64, EXTRACT_BE_U_8(mpc->sender_key)));
         if (opt_len == 20) /* ACK */
-                ND_PRINT((ndo, ",0x%" PRIx64, EXTRACT_64BITS(mpc->receiver_key)));
+                ND_PRINT((ndo, ",0x%" PRIx64, EXTRACT_BE_U_8(mpc->receiver_key)));
         ND_PRINT((ndo, "}"));
         return 1;
 }
@@ -198,9 +202,9 @@ mp_join_print(netdissect_options *ndo,
 {
         const struct mp_join *mpj = (const struct mp_join *) opt;
 
-        if (!(opt_len == 12 && flags & TH_SYN) &&
+        if (!(opt_len == 12 && (flags & TH_SYN)) &&
             !(opt_len == 16 && (flags & (TH_SYN | TH_ACK)) == (TH_SYN | TH_ACK)) &&
-            !(opt_len == 24 && flags & TH_ACK))
+            !(opt_len == 24 && (flags & TH_ACK)))
                 return 0;
 
         if (opt_len != 24) {
@@ -212,13 +216,13 @@ mp_join_print(netdissect_options *ndo,
         switch (opt_len) {
         case 12: /* SYN */
                 ND_PRINT((ndo, " token 0x%x" " nonce 0x%x",
-                        EXTRACT_32BITS(mpj->u.syn.token),
-                        EXTRACT_32BITS(mpj->u.syn.nonce)));
+                        EXTRACT_BE_U_4(mpj->u.syn.token),
+                        EXTRACT_BE_U_4(mpj->u.syn.nonce)));
                 break;
         case 16: /* SYN/ACK */
                 ND_PRINT((ndo, " hmac 0x%" PRIx64 " nonce 0x%x",
-                        EXTRACT_64BITS(mpj->u.synack.mac),
-                        EXTRACT_32BITS(mpj->u.synack.nonce)));
+                        EXTRACT_BE_U_8(mpj->u.synack.mac),
+                        EXTRACT_BE_U_4(mpj->u.synack.nonce)));
                 break;
         case 24: {/* ACK */
                 size_t i;
@@ -232,76 +236,92 @@ mp_join_print(netdissect_options *ndo,
         return 1;
 }
 
-static u_int mp_dss_len(const  struct mp_dss *m, int csum)
-{
-        u_int len;
-
-        len = 4;
-        if (m->flags & MP_DSS_A) {
-                /* Ack present - 4 or 8 octets */
-                len += (m->flags & MP_DSS_a) ? 8 : 4;
-        }
-        if (m->flags & MP_DSS_M) {
-                /*
-                 * Data Sequence Number (DSN), Subflow Sequence Number (SSN),
-                 * Data-Level Length present, and Checksum possibly present.
-                 * All but the Checksum are 10 bytes if the m flag is
-                 * clear (4-byte DSN) and 14 bytes if the m flag is set
-                 * (8-byte DSN).
-                 */
-                len += (m->flags & MP_DSS_m) ? 14 : 10;
-
-                /*
-                 * The Checksum is present only if negotiated.
-                 */
-                if (csum)
-                        len += 2;
-       }
-       return len;
-}
-
 static int
 mp_dss_print(netdissect_options *ndo,
              const u_char *opt, u_int opt_len, u_char flags)
 {
         const struct mp_dss *mdss = (const struct mp_dss *) opt;
 
-        if ((opt_len != mp_dss_len(mdss, 1) &&
-             opt_len != mp_dss_len(mdss, 0)) || flags & TH_SYN)
+        /* We need the flags, at a minimum. */
+        if (opt_len < 4)
+                return 0;
+
+        if (flags & TH_SYN)
                 return 0;
 
         if (mdss->flags & MP_DSS_F)
                 ND_PRINT((ndo, " fin"));
 
         opt += 4;
+        opt_len -= 4;
         if (mdss->flags & MP_DSS_A) {
+                /* Ack present */
                 ND_PRINT((ndo, " ack "));
+                /*
+                 * If the a flag is set, we have an 8-byte ack; if it's
+                 * clear, we have a 4-byte ack.
+                 */
                 if (mdss->flags & MP_DSS_a) {
-                        ND_PRINT((ndo, "%" PRIu64, EXTRACT_64BITS(opt)));
+                        if (opt_len < 8)
+                                return 0;
+                        ND_PRINT((ndo, "%" PRIu64, EXTRACT_BE_U_8(opt)));
                         opt += 8;
+                        opt_len -= 8;
                 } else {
-                        ND_PRINT((ndo, "%u", EXTRACT_32BITS(opt)));
+                        if (opt_len < 4)
+                                return 0;
+                        ND_PRINT((ndo, "%u", EXTRACT_BE_U_4(opt)));
                         opt += 4;
+                        opt_len -= 4;
                 }
         }
 
         if (mdss->flags & MP_DSS_M) {
+                /*
+                 * Data Sequence Number (DSN), Subflow Sequence Number (SSN),
+                 * Data-Level Length present, and Checksum possibly present.
+                 */
                 ND_PRINT((ndo, " seq "));
+               /*
+                 * If the m flag is set, we have an 8-byte NDS; if it's clear,
+                 * we have a 4-byte DSN.
+                 */
                 if (mdss->flags & MP_DSS_m) {
-                        ND_PRINT((ndo, "%" PRIu64, EXTRACT_64BITS(opt)));
+                        if (opt_len < 8)
+                                return 0;
+                        ND_PRINT((ndo, "%" PRIu64, EXTRACT_BE_U_8(opt)));
                         opt += 8;
+                        opt_len -= 8;
                 } else {
-                        ND_PRINT((ndo, "%u", EXTRACT_32BITS(opt)));
+                        if (opt_len < 4)
+                                return 0;
+                        ND_PRINT((ndo, "%u", EXTRACT_BE_U_4(opt)));
                         opt += 4;
+                        opt_len -= 4;
                 }
-                ND_PRINT((ndo, " subseq %u", EXTRACT_32BITS(opt)));
+                if (opt_len < 4)
+                        return 0;
+                ND_PRINT((ndo, " subseq %u", EXTRACT_BE_U_4(opt)));
                 opt += 4;
-                ND_PRINT((ndo, " len %u", EXTRACT_16BITS(opt)));
+                opt_len -= 4;
+                if (opt_len < 2)
+                        return 0;
+                ND_PRINT((ndo, " len %u", EXTRACT_BE_U_2(opt)));
                 opt += 2;
+                opt_len -= 2;
 
-                if (opt_len == mp_dss_len(mdss, 1))
-                        ND_PRINT((ndo, " csum 0x%x", EXTRACT_16BITS(opt)));
+                /*
+                 * The Checksum is present only if negotiated.
+                 * If there are at least 2 bytes left, process the next 2
+                 * bytes as the Checksum.
+                 */
+                if (opt_len >= 2) {
+                        ND_PRINT((ndo, " csum 0x%x", EXTRACT_BE_U_2(opt)));
+                        opt_len -= 2;
+                }
         }
+        if (opt_len != 0)
+                return 0;
         return 1;
 }
 
@@ -321,14 +341,12 @@ add_addr_print(netdissect_options *ndo,
         case 4:
                 ND_PRINT((ndo, " %s", ipaddr_string(ndo, add_addr->u.v4.addr)));
                 if (opt_len == 10)
-                        ND_PRINT((ndo, ":%u", EXTRACT_16BITS(add_addr->u.v4.port)));
+                        ND_PRINT((ndo, ":%u", EXTRACT_BE_U_2(add_addr->u.v4.port)));
                 break;
         case 6:
-#ifdef INET6
                 ND_PRINT((ndo, " %s", ip6addr_string(ndo, add_addr->u.v6.addr)));
-#endif
                 if (opt_len == 22)
-                        ND_PRINT((ndo, ":%u", EXTRACT_16BITS(add_addr->u.v6.port)));
+                        ND_PRINT((ndo, ":%u", EXTRACT_BE_U_2(add_addr->u.v6.port)));
                 break;
         default:
                 return 0;
@@ -349,8 +367,10 @@ remove_addr_print(netdissect_options *ndo,
 
         opt_len -= 3;
         ND_PRINT((ndo, " id"));
-        while (opt_len--)
-                ND_PRINT((ndo, " %u", *addr_id++));
+        while (opt_len--) {
+                ND_PRINT((ndo, " %u", EXTRACT_U_1(addr_id)));
+                addr_id++;
+        }
         return 1;
 }
 
@@ -380,7 +400,7 @@ mp_fail_print(netdissect_options *ndo,
         if (opt_len != 12)
                 return 0;
 
-        ND_PRINT((ndo, " seq %" PRIu64, EXTRACT_64BITS(opt + 4)));
+        ND_PRINT((ndo, " seq %" PRIu64, EXTRACT_BE_U_8(opt + 4)));
         return 1;
 }
 
@@ -391,7 +411,7 @@ mp_fast_close_print(netdissect_options *ndo,
         if (opt_len != 12)
                 return 0;
 
-        ND_PRINT((ndo, " key 0x%" PRIx64, EXTRACT_64BITS(opt + 4)));
+        ND_PRINT((ndo, " key 0x%" PRIx64, EXTRACT_BE_U_8(opt + 4)));
         return 1;
 }