print-vxlan-gpe.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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: Generic Protocol Extension for VXLAN (VXLAN GPE) printer */
  24. /* specification: draft-ietf-nvo3-vxlan-gpe-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[] = " [|VXLAN-GPE]";
  32. static const struct tok vxlan_gpe_flags [] = {
  33. { 0x08, "I" },
  34. { 0x04, "P" },
  35. { 0x01, "O" },
  36. { 0, NULL }
  37. };
  38. #define VXLAN_GPE_HDR_LEN 8
  39. /*
  40. * VXLAN GPE header, draft-ietf-nvo3-vxlan-gpe-01
  41. * Generic Protocol Extension for VXLAN
  42. *
  43. * 0 1 2 3
  44. * 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
  45. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  46. * |R|R|Ver|I|P|R|O| Reserved |Next Protocol |
  47. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  48. * | VXLAN Network Identifier (VNI) | Reserved |
  49. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  50. */
  51. void
  52. vxlan_gpe_print(netdissect_options *ndo, const u_char *bp, u_int len)
  53. {
  54. uint8_t flags;
  55. uint8_t next_protocol;
  56. uint32_t vni;
  57. if (len < VXLAN_GPE_HDR_LEN)
  58. goto trunc;
  59. ND_TCHECK2(*bp, VXLAN_GPE_HDR_LEN);
  60. flags = *bp;
  61. bp += 3;
  62. next_protocol = *bp;
  63. bp += 1;
  64. vni = EXTRACT_24BITS(bp);
  65. bp += 4;
  66. ND_PRINT((ndo, "VXLAN-GPE, "));
  67. ND_PRINT((ndo, "flags [%s], ",
  68. bittok2str_nosep(vxlan_gpe_flags, "none", flags)));
  69. ND_PRINT((ndo, "vni %u", vni));
  70. ND_PRINT((ndo, ndo->ndo_vflag ? "\n " : ": "));
  71. switch (next_protocol) {
  72. case 0x1:
  73. ip_print(ndo, bp, len - 8);
  74. break;
  75. case 0x2:
  76. ip6_print(ndo, bp, len - 8);
  77. break;
  78. case 0x3:
  79. ether_print(ndo, bp, len - 8, ndo->ndo_snapend - bp, NULL, NULL);
  80. break;
  81. case 0x4:
  82. nsh_print(ndo, bp, len - 8);
  83. break;
  84. case 0x5:
  85. mpls_print(ndo, bp, len - 8);
  86. break;
  87. default:
  88. ND_PRINT((ndo, "ERROR: unknown-next-protocol"));
  89. return;
  90. }
  91. return;
  92. trunc:
  93. ND_PRINT((ndo, "%s", tstr));
  94. }