JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 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 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 33 | /* \summary: version-independent OpenFlow printer */ |
| 34 | |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 35 | #ifdef HAVE_CONFIG_H |
| 36 | #include "config.h" |
| 37 | #endif |
| 38 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 39 | #include <netdissect-stdinc.h> |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 40 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 41 | #include "netdissect.h" |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 42 | #include "extract.h" |
| 43 | #include "openflow.h" |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 44 | #include "oui.h" |
| 45 | |
| 46 | static const char tstr[] = " [|openflow]"; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 47 | |
| 48 | #define OF_VER_1_0 0x01 |
| 49 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 50 | const struct tok onf_exp_str[] = { |
| 51 | { ONF_EXP_ONF, "ONF Extensions" }, |
| 52 | { ONF_EXP_BUTE, "Budapest University of Technology and Economics" }, |
| 53 | { ONF_EXP_NOVIFLOW, "NoviFlow" }, |
| 54 | { ONF_EXP_L3, "L3+ Extensions, Vendor Neutral" }, |
| 55 | { ONF_EXP_L4L7, "L4-L7 Extensions" }, |
| 56 | { ONF_EXP_WMOB, "Wireless and Mobility Extensions" }, |
| 57 | { ONF_EXP_FABS, "Forwarding Abstractions Extensions" }, |
| 58 | { ONF_EXP_OTRANS, "Optical Transport Extensions" }, |
| 59 | { 0, NULL } |
| 60 | }; |
| 61 | |
| 62 | const char * |
| 63 | of_vendor_name(const uint32_t vendor) |
| 64 | { |
| 65 | const struct tok *table = (vendor & 0xff000000) == 0 ? oui_values : onf_exp_str; |
| 66 | return tok2str(table, "unknown", vendor); |
| 67 | } |
| 68 | |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 69 | static void |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 70 | of_header_print(netdissect_options *ndo, const uint8_t version, const uint8_t type, |
| 71 | const uint16_t length, const uint32_t xid) |
| 72 | { |
| 73 | ND_PRINT((ndo, "\n\tversion unknown (0x%02x), type 0x%02x, length %u, xid 0x%08x", |
| 74 | version, type, length, xid)); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /* Print a single OpenFlow message. */ |
| 78 | static const u_char * |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 79 | of_header_body_print(netdissect_options *ndo, const u_char *cp, const u_char *ep) |
| 80 | { |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 81 | uint8_t version, type; |
| 82 | uint16_t length; |
| 83 | uint32_t xid; |
| 84 | |
| 85 | if (ep < cp + OF_HEADER_LEN) |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 86 | goto invalid; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 87 | /* version */ |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 88 | ND_TCHECK2(*cp, 1); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 89 | version = *cp; |
| 90 | cp += 1; |
| 91 | /* type */ |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 92 | ND_TCHECK2(*cp, 1); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 93 | type = *cp; |
| 94 | cp += 1; |
| 95 | /* length */ |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 96 | ND_TCHECK2(*cp, 2); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 97 | length = EXTRACT_16BITS(cp); |
| 98 | cp += 2; |
| 99 | /* xid */ |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 100 | ND_TCHECK2(*cp, 4); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 101 | xid = EXTRACT_32BITS(cp); |
| 102 | cp += 4; |
| 103 | /* Message length includes the header length and a message always includes |
| 104 | * the basic header. A message length underrun fails decoding of the rest of |
| 105 | * the current packet. At the same time, try decoding as much of the current |
| 106 | * message as possible even when it does not end within the current TCP |
| 107 | * segment. */ |
| 108 | if (length < OF_HEADER_LEN) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 109 | of_header_print(ndo, version, type, length, xid); |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 110 | goto invalid; |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 111 | } |
| 112 | /* Decode known protocol versions further without printing the header (the |
| 113 | * type decoding is version-specific. */ |
| 114 | switch (version) { |
| 115 | case OF_VER_1_0: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 116 | return of10_header_body_print(ndo, cp, ep, type, length, xid); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 117 | default: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 118 | of_header_print(ndo, version, type, length, xid); |
| 119 | ND_TCHECK2(*cp, length - OF_HEADER_LEN); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 120 | return cp + length - OF_HEADER_LEN; /* done with current message */ |
| 121 | } |
| 122 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 123 | invalid: /* fail current packet */ |
| 124 | ND_PRINT((ndo, "%s", istr)); |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 125 | ND_TCHECK2(*cp, ep - cp); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 126 | return ep; |
| 127 | trunc: |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 128 | ND_PRINT((ndo, "%s", tstr)); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 129 | return ep; |
| 130 | } |
| 131 | |
| 132 | /* Print a TCP segment worth of OpenFlow messages presuming the segment begins |
| 133 | * on a message boundary. */ |
| 134 | void |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 135 | openflow_print(netdissect_options *ndo, const u_char *cp, const u_int len) |
| 136 | { |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 137 | const u_char *ep = cp + len; |
| 138 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 139 | ND_PRINT((ndo, ": OpenFlow")); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 140 | while (cp < ep) |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 141 | cp = of_header_body_print(ndo, cp, ep); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 142 | } |