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 33 #include <sys/cdefs.h> 34 #ifndef lint 35 __RCSID("$NetBSD: print-openflow.c,v 1.6 2024/09/02 16:15:32 christos Exp $"); 36 #endif 37 38 /* \summary: version-independent OpenFlow printer */ 39 40 #include <config.h> 41 42 #include "netdissect-stdinc.h" 43 44 #define ND_LONGJMP_FROM_TCHECK 45 #include "netdissect.h" 46 #include "extract.h" 47 #include "openflow.h" 48 #include "oui.h" 49 50 51 static const struct tok ofver_str[] = { 52 { OF_VER_1_0, "1.0" }, 53 { OF_VER_1_1, "1.1" }, 54 { OF_VER_1_2, "1.2" }, 55 { OF_VER_1_3, "1.3" }, 56 { OF_VER_1_4, "1.4" }, 57 { OF_VER_1_5, "1.5" }, 58 { 0, NULL } 59 }; 60 61 const struct tok onf_exp_str[] = { 62 { ONF_EXP_ONF, "ONF Extensions" }, 63 { ONF_EXP_BUTE, "Budapest University of Technology and Economics" }, 64 { ONF_EXP_NOVIFLOW, "NoviFlow" }, 65 { ONF_EXP_L3, "L3+ Extensions, Vendor Neutral" }, 66 { ONF_EXP_L4L7, "L4-L7 Extensions" }, 67 { ONF_EXP_WMOB, "Wireless and Mobility Extensions" }, 68 { ONF_EXP_FABS, "Forwarding Abstractions Extensions" }, 69 { ONF_EXP_OTRANS, "Optical Transport Extensions" }, 70 { ONF_EXP_NBLNCTU, "Network Benchmarking Lab, NCTU" }, 71 { ONF_EXP_MPCE, "Mobile Packet Core Extensions" }, 72 { ONF_EXP_MPLSTPSPTN, "MPLS-TP OpenFlow Extensions for SPTN" }, 73 { 0, NULL } 74 }; 75 76 const char * 77 of_vendor_name(const uint32_t vendor) 78 { 79 const struct tok *table = (vendor & 0xff000000) == 0 ? oui_values : onf_exp_str; 80 return tok2str(table, "unknown", vendor); 81 } 82 83 void 84 of_bitmap_print(netdissect_options *ndo, 85 const struct tok *t, const uint32_t v, const uint32_t u) 86 { 87 /* Assigned bits? */ 88 if (v & ~u) 89 ND_PRINT(" (%s)", bittok2str(t, "", v)); 90 /* Unassigned bits? */ 91 if (v & u) 92 ND_PRINT(" (bogus)"); 93 } 94 95 void 96 of_data_print(netdissect_options *ndo, 97 const u_char *cp, const u_int len) 98 { 99 if (len == 0) 100 return; 101 /* data */ 102 ND_PRINT("\n\t data (%u octets)", len); 103 if (ndo->ndo_vflag >= 2) 104 hex_and_ascii_print(ndo, "\n\t ", cp, len); 105 else 106 ND_TCHECK_LEN(cp, len); 107 } 108 109 static void 110 of_message_print(netdissect_options *ndo, 111 const u_char *cp, uint16_t len, 112 const struct of_msgtypeinfo *mti) 113 { 114 /* 115 * Here "cp" and "len" stand for the message part beyond the common 116 * OpenFlow 1.0 header, if any. 117 * 118 * Most message types are longer than just the header, and the length 119 * constraints may be complex. When possible, validate the constraint 120 * completely here (REQ_FIXLEN), otherwise check that the message is 121 * long enough to begin the decoding (REQ_MINLEN) and have the 122 * type-specific function do any remaining validation. 123 */ 124 125 if (!mti) 126 goto tcheck_remainder; 127 128 if ((mti->req_what == REQ_FIXLEN && len != mti->req_value) || 129 (mti->req_what == REQ_MINLEN && len < mti->req_value)) 130 goto invalid; 131 132 if (!ndo->ndo_vflag || !mti->decoder) 133 goto tcheck_remainder; 134 135 mti->decoder(ndo, cp, len); 136 return; 137 138 invalid: 139 nd_print_invalid(ndo); 140 tcheck_remainder: 141 ND_TCHECK_LEN(cp, len); 142 } 143 144 /* Print a TCP segment worth of OpenFlow messages presuming the segment begins 145 * on a message boundary. */ 146 void 147 openflow_print(netdissect_options *ndo, const u_char *cp, u_int len) 148 { 149 ndo->ndo_protocol = "openflow"; 150 ND_PRINT(": OpenFlow"); 151 while (len) { 152 /* Print a single OpenFlow message. */ 153 uint8_t version, type; 154 uint16_t length; 155 const struct of_msgtypeinfo *mti; 156 157 /* version */ 158 version = GET_U_1(cp); 159 OF_FWD(1); 160 ND_PRINT("\n\tversion %s", 161 tok2str(ofver_str, "unknown (0x%02x)", version)); 162 /* type */ 163 if (len < 1) 164 goto partial_header; 165 type = GET_U_1(cp); 166 OF_FWD(1); 167 mti = 168 version == OF_VER_1_0 ? of10_identify_msgtype(type) : 169 version == OF_VER_1_3 ? of13_identify_msgtype(type) : 170 NULL; 171 if (mti && mti->name) 172 ND_PRINT(", type %s", mti->name); 173 else 174 ND_PRINT(", type unknown (0x%02x)", type); 175 /* length */ 176 if (len < 2) 177 goto partial_header; 178 length = GET_BE_U_2(cp); 179 OF_FWD(2); 180 ND_PRINT(", length %u%s", length, 181 length < OF_HEADER_FIXLEN ? " (too short!)" : ""); 182 /* xid */ 183 if (len < 4) 184 goto partial_header; 185 ND_PRINT(", xid 0x%08x", GET_BE_U_4(cp)); 186 OF_FWD(4); 187 188 /* 189 * When a TCP packet can contain several protocol messages, 190 * and at the same time a protocol message can span several 191 * TCP packets, decoding an incomplete message at the end of 192 * a TCP packet requires attention to detail in this loop. 193 * 194 * Message length includes the header length and a message 195 * always includes the basic header. A message length underrun 196 * fails decoding of the rest of the current packet. At the 197 * same time, try decoding as much of the current message as 198 * possible even when it does not end within the current TCP 199 * segment. 200 * 201 * Specifically, to try to process the message body in this 202 * iteration do NOT require the header "length" to be small 203 * enough for the full declared OpenFlow message to fit into 204 * the remainder of the declared TCP segment, same as the full 205 * declared TCP segment is not required to fit into the 206 * captured packet buffer. 207 * 208 * But DO require the same at the end of this iteration to 209 * decrement "len" and to proceed to the next iteration. 210 * (Ideally the declared TCP payload end will be at or after 211 * the captured packet buffer end, but stay safe even when 212 * that's somehow not the case.) 213 */ 214 if (length < OF_HEADER_FIXLEN) 215 goto invalid; 216 217 of_message_print(ndo, cp, length - OF_HEADER_FIXLEN, mti); 218 if (length - OF_HEADER_FIXLEN > len) 219 break; 220 OF_FWD(length - OF_HEADER_FIXLEN); 221 } /* while (len) */ 222 return; 223 224 partial_header: 225 ND_PRINT(" (end of TCP payload)"); 226 ND_TCHECK_LEN(cp, len); 227 return; 228 invalid: /* fail the current packet */ 229 nd_print_invalid(ndo); 230 ND_TCHECK_LEN(cp, len); 231 } 232