print-loopback.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2014 The TCPDUMP project
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  17. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  18. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  20. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  24. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /* \summary: Loopback Protocol printer */
  28. /*
  29. * originally defined as the Ethernet Configuration Testing Protocol.
  30. * specification: http://www.mit.edu/people/jhawk/ctp.pdf
  31. */
  32. #ifdef HAVE_CONFIG_H
  33. #include "config.h"
  34. #endif
  35. #include <netdissect-stdinc.h>
  36. #include "netdissect.h"
  37. #include "extract.h"
  38. #include "ether.h"
  39. #include "addrtoname.h"
  40. static const char tstr[] = " [|loopback]";
  41. #define LOOPBACK_REPLY 1
  42. #define LOOPBACK_FWDDATA 2
  43. static const struct tok fcode_str[] = {
  44. { LOOPBACK_REPLY, "Reply" },
  45. { LOOPBACK_FWDDATA, "Forward Data" },
  46. { 0, NULL }
  47. };
  48. static void
  49. loopback_message_print(netdissect_options *ndo, const u_char *cp, const u_int len)
  50. {
  51. const u_char *ep = cp + len;
  52. uint16_t function;
  53. if (len < 2)
  54. goto invalid;
  55. /* function */
  56. ND_TCHECK2(*cp, 2);
  57. function = EXTRACT_LE_16BITS(cp);
  58. cp += 2;
  59. ND_PRINT((ndo, ", %s", tok2str(fcode_str, " invalid (%u)", function)));
  60. switch (function) {
  61. case LOOPBACK_REPLY:
  62. if (len < 4)
  63. goto invalid;
  64. /* receipt number */
  65. ND_TCHECK2(*cp, 2);
  66. ND_PRINT((ndo, ", receipt number %u", EXTRACT_LE_16BITS(cp)));
  67. cp += 2;
  68. /* data */
  69. ND_PRINT((ndo, ", data (%u octets)", len - 4));
  70. ND_TCHECK2(*cp, len - 4);
  71. break;
  72. case LOOPBACK_FWDDATA:
  73. if (len < 8)
  74. goto invalid;
  75. /* forwarding address */
  76. ND_TCHECK2(*cp, ETHER_ADDR_LEN);
  77. ND_PRINT((ndo, ", forwarding address %s", etheraddr_string(ndo, cp)));
  78. cp += ETHER_ADDR_LEN;
  79. /* data */
  80. ND_PRINT((ndo, ", data (%u octets)", len - 8));
  81. ND_TCHECK2(*cp, len - 8);
  82. break;
  83. default:
  84. ND_TCHECK2(*cp, len - 2);
  85. break;
  86. }
  87. return;
  88. invalid:
  89. ND_PRINT((ndo, "%s", istr));
  90. ND_TCHECK2(*cp, ep - cp);
  91. return;
  92. trunc:
  93. ND_PRINT((ndo, "%s", tstr));
  94. }
  95. void
  96. loopback_print(netdissect_options *ndo, const u_char *cp, const u_int len)
  97. {
  98. const u_char *ep = cp + len;
  99. uint16_t skipCount;
  100. ND_PRINT((ndo, "Loopback"));
  101. if (len < 2)
  102. goto invalid;
  103. /* skipCount */
  104. ND_TCHECK2(*cp, 2);
  105. skipCount = EXTRACT_LE_16BITS(cp);
  106. cp += 2;
  107. ND_PRINT((ndo, ", skipCount %u", skipCount));
  108. if (skipCount % 8)
  109. ND_PRINT((ndo, " (bogus)"));
  110. if (skipCount > len - 2)
  111. goto invalid;
  112. loopback_message_print(ndo, cp + skipCount, len - 2 - skipCount);
  113. return;
  114. invalid:
  115. ND_PRINT((ndo, "%s", istr));
  116. ND_TCHECK2(*cp, ep - cp);
  117. return;
  118. trunc:
  119. ND_PRINT((ndo, "%s", tstr));
  120. }