print-openflow.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * This module implements printing of the very basic (version-independent)
  3. * OpenFlow header and iteration over OpenFlow messages. It is intended for
  4. * dispatching of version-specific OpenFlow message decoding.
  5. *
  6. *
  7. * Copyright (c) 2013 The TCPDUMP project
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  29. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /* \summary: version-independent OpenFlow printer */
  33. #ifdef HAVE_CONFIG_H
  34. #include "config.h"
  35. #endif
  36. #include <netdissect-stdinc.h>
  37. #include "netdissect.h"
  38. #include "extract.h"
  39. #include "openflow.h"
  40. #include "oui.h"
  41. static const char tstr[] = " [|openflow]";
  42. #define OF_VER_1_0 0x01
  43. const struct tok onf_exp_str[] = {
  44. { ONF_EXP_ONF, "ONF Extensions" },
  45. { ONF_EXP_BUTE, "Budapest University of Technology and Economics" },
  46. { ONF_EXP_NOVIFLOW, "NoviFlow" },
  47. { ONF_EXP_L3, "L3+ Extensions, Vendor Neutral" },
  48. { ONF_EXP_L4L7, "L4-L7 Extensions" },
  49. { ONF_EXP_WMOB, "Wireless and Mobility Extensions" },
  50. { ONF_EXP_FABS, "Forwarding Abstractions Extensions" },
  51. { ONF_EXP_OTRANS, "Optical Transport Extensions" },
  52. { 0, NULL }
  53. };
  54. const char *
  55. of_vendor_name(const uint32_t vendor)
  56. {
  57. const struct tok *table = (vendor & 0xff000000) == 0 ? oui_values : onf_exp_str;
  58. return tok2str(table, "unknown", vendor);
  59. }
  60. static void
  61. of_header_print(netdissect_options *ndo, const uint8_t version, const uint8_t type,
  62. const uint16_t length, const uint32_t xid)
  63. {
  64. ND_PRINT((ndo, "\n\tversion unknown (0x%02x), type 0x%02x, length %u, xid 0x%08x",
  65. version, type, length, xid));
  66. }
  67. /* Print a single OpenFlow message. */
  68. static const u_char *
  69. of_header_body_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
  70. {
  71. uint8_t version, type;
  72. uint16_t length;
  73. uint32_t xid;
  74. if (ep < cp + OF_HEADER_LEN)
  75. goto invalid;
  76. /* version */
  77. ND_TCHECK2(*cp, 1);
  78. version = *cp;
  79. cp += 1;
  80. /* type */
  81. ND_TCHECK2(*cp, 1);
  82. type = *cp;
  83. cp += 1;
  84. /* length */
  85. ND_TCHECK2(*cp, 2);
  86. length = EXTRACT_16BITS(cp);
  87. cp += 2;
  88. /* xid */
  89. ND_TCHECK2(*cp, 4);
  90. xid = EXTRACT_32BITS(cp);
  91. cp += 4;
  92. /* Message length includes the header length and a message always includes
  93. * the basic header. A message length underrun fails decoding of the rest of
  94. * the current packet. At the same time, try decoding as much of the current
  95. * message as possible even when it does not end within the current TCP
  96. * segment. */
  97. if (length < OF_HEADER_LEN) {
  98. of_header_print(ndo, version, type, length, xid);
  99. goto invalid;
  100. }
  101. /* Decode known protocol versions further without printing the header (the
  102. * type decoding is version-specific. */
  103. switch (version) {
  104. case OF_VER_1_0:
  105. return of10_header_body_print(ndo, cp, ep, type, length, xid);
  106. default:
  107. of_header_print(ndo, version, type, length, xid);
  108. ND_TCHECK2(*cp, length - OF_HEADER_LEN);
  109. return cp + length - OF_HEADER_LEN; /* done with current message */
  110. }
  111. invalid: /* fail current packet */
  112. ND_PRINT((ndo, "%s", istr));
  113. ND_TCHECK2(*cp, ep - cp);
  114. return ep;
  115. trunc:
  116. ND_PRINT((ndo, "%s", tstr));
  117. return ep;
  118. }
  119. /* Print a TCP segment worth of OpenFlow messages presuming the segment begins
  120. * on a message boundary. */
  121. void
  122. openflow_print(netdissect_options *ndo, const u_char *cp, const u_int len)
  123. {
  124. const u_char *ep = cp + len;
  125. ND_PRINT((ndo, ": OpenFlow"));
  126. while (cp < ep)
  127. cp = of_header_body_print(ndo, cp, ep);
  128. }