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