print-nsh.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* Copyright (c) 2015, bugyo
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * 1. Redistributions of source code must retain the above copyright notice,
  7. * this list of conditions and the following disclaimer.
  8. * 2. Redistributions in binary form must reproduce the above copyright notice,
  9. * this list of conditions and the following disclaimer in the documentation
  10. * and/or other materials provided with the distribution.
  11. *
  12. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  13. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  16. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. /* \summary: Network Service Header (NSH) printer */
  24. /* specification: draft-ietf-sfc-nsh-01 */
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #include <netdissect-stdinc.h>
  29. #include "netdissect.h"
  30. #include "extract.h"
  31. static const char tstr[] = " [|NSH]";
  32. static const struct tok nsh_flags [] = {
  33. { 0x20, "O" },
  34. { 0x10, "C" },
  35. { 0, NULL }
  36. };
  37. #define NSH_BASE_HDR_LEN 4
  38. #define NSH_SERVICE_PATH_HDR_LEN 4
  39. #define NSH_HDR_WORD_SIZE 4U
  40. void
  41. nsh_print(netdissect_options *ndo, const u_char *bp, u_int len)
  42. {
  43. int n, vn;
  44. uint8_t ver;
  45. uint8_t flags;
  46. uint8_t length;
  47. uint8_t md_type;
  48. uint8_t next_protocol;
  49. uint32_t service_path_id;
  50. uint8_t service_index;
  51. uint32_t ctx;
  52. uint16_t tlv_class;
  53. uint8_t tlv_type;
  54. uint8_t tlv_len;
  55. u_int next_len;
  56. /* print Base Header and Service Path Header */
  57. if (len < NSH_BASE_HDR_LEN + NSH_SERVICE_PATH_HDR_LEN)
  58. goto trunc;
  59. ND_TCHECK2(*bp, NSH_BASE_HDR_LEN + NSH_SERVICE_PATH_HDR_LEN);
  60. ver = (uint8_t)(*bp >> 6);
  61. flags = *bp;
  62. bp += 1;
  63. length = *bp;
  64. bp += 1;
  65. md_type = *bp;
  66. bp += 1;
  67. next_protocol = *bp;
  68. bp += 1;
  69. service_path_id = EXTRACT_24BITS(bp);
  70. bp += 3;
  71. service_index = *bp;
  72. bp += 1;
  73. ND_PRINT((ndo, "NSH, "));
  74. if (ndo->ndo_vflag > 1) {
  75. ND_PRINT((ndo, "ver %d, ", ver));
  76. }
  77. ND_PRINT((ndo, "flags [%s], ", bittok2str_nosep(nsh_flags, "none", flags)));
  78. if (ndo->ndo_vflag > 2) {
  79. ND_PRINT((ndo, "length %d, ", length));
  80. ND_PRINT((ndo, "md type 0x%x, ", md_type));
  81. }
  82. if (ndo->ndo_vflag > 1) {
  83. ND_PRINT((ndo, "next-protocol 0x%x, ", next_protocol));
  84. }
  85. ND_PRINT((ndo, "service-path-id 0x%06x, ", service_path_id));
  86. ND_PRINT((ndo, "service-index 0x%x", service_index));
  87. /* Make sure we have all the headers */
  88. if (len < length * NSH_HDR_WORD_SIZE)
  89. goto trunc;
  90. ND_TCHECK2(*bp, length * NSH_HDR_WORD_SIZE);
  91. /*
  92. * length includes the lengths of the Base and Service Path headers.
  93. * That means it must be at least 2.
  94. */
  95. if (length < 2)
  96. goto trunc;
  97. /*
  98. * Print, or skip, the Context Headers.
  99. * (length - 2) is the length of those headers.
  100. */
  101. if (ndo->ndo_vflag > 2) {
  102. if (md_type == 0x01) {
  103. for (n = 0; n < length - 2; n++) {
  104. ctx = EXTRACT_32BITS(bp);
  105. bp += NSH_HDR_WORD_SIZE;
  106. ND_PRINT((ndo, "\n Context[%02d]: 0x%08x", n, ctx));
  107. }
  108. }
  109. else if (md_type == 0x02) {
  110. n = 0;
  111. while (n < length - 2) {
  112. tlv_class = EXTRACT_16BITS(bp);
  113. bp += 2;
  114. tlv_type = *bp;
  115. bp += 1;
  116. tlv_len = *bp;
  117. bp += 1;
  118. ND_PRINT((ndo, "\n TLV Class %d, Type %d, Len %d",
  119. tlv_class, tlv_type, tlv_len));
  120. n += 1;
  121. if (length - 2 < n + tlv_len) {
  122. ND_PRINT((ndo, " ERROR: invalid-tlv-length"));
  123. return;
  124. }
  125. for (vn = 0; vn < tlv_len; vn++) {
  126. ctx = EXTRACT_32BITS(bp);
  127. bp += NSH_HDR_WORD_SIZE;
  128. ND_PRINT((ndo, "\n Value[%02d]: 0x%08x", vn, ctx));
  129. }
  130. n += tlv_len;
  131. }
  132. }
  133. else {
  134. ND_PRINT((ndo, "ERROR: unknown-next-protocol"));
  135. return;
  136. }
  137. }
  138. else {
  139. bp += (length - 2) * NSH_HDR_WORD_SIZE;
  140. }
  141. ND_PRINT((ndo, ndo->ndo_vflag ? "\n " : ": "));
  142. /* print Next Protocol */
  143. next_len = len - length * NSH_HDR_WORD_SIZE;
  144. switch (next_protocol) {
  145. case 0x1:
  146. ip_print(ndo, bp, next_len);
  147. break;
  148. case 0x2:
  149. ip6_print(ndo, bp, next_len);
  150. break;
  151. case 0x3:
  152. ether_print(ndo, bp, next_len, ndo->ndo_snapend - bp, NULL, NULL);
  153. break;
  154. default:
  155. ND_PRINT((ndo, "ERROR: unknown-next-protocol"));
  156. return;
  157. }
  158. return;
  159. trunc:
  160. ND_PRINT((ndo, "%s", tstr));
  161. }