xref: /aosp_15_r20/external/tcpdump/print-openflow.c (revision 05b00f6010a2396e3db2409989fc67270046269f)
1*05b00f60SXin Li /*
2*05b00f60SXin Li  * This module implements printing of the very basic (version-independent)
3*05b00f60SXin Li  * OpenFlow header and iteration over OpenFlow messages. It is intended for
4*05b00f60SXin Li  * dispatching of version-specific OpenFlow message decoding.
5*05b00f60SXin Li  *
6*05b00f60SXin Li  *
7*05b00f60SXin Li  * Copyright (c) 2013 The TCPDUMP project
8*05b00f60SXin Li  * All rights reserved.
9*05b00f60SXin Li  *
10*05b00f60SXin Li  * Redistribution and use in source and binary forms, with or without
11*05b00f60SXin Li  * modification, are permitted provided that the following conditions
12*05b00f60SXin Li  * are met:
13*05b00f60SXin Li  * 1. Redistributions of source code must retain the above copyright
14*05b00f60SXin Li  *    notice, this list of conditions and the following disclaimer.
15*05b00f60SXin Li  * 2. Redistributions in binary form must reproduce the above copyright
16*05b00f60SXin Li  *    notice, this list of conditions and the following disclaimer in the
17*05b00f60SXin Li  *    documentation and/or other materials provided with the distribution.
18*05b00f60SXin Li  *
19*05b00f60SXin Li  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*05b00f60SXin Li  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*05b00f60SXin Li  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*05b00f60SXin Li  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*05b00f60SXin Li  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24*05b00f60SXin Li  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25*05b00f60SXin Li  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26*05b00f60SXin Li  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27*05b00f60SXin Li  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*05b00f60SXin Li  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29*05b00f60SXin Li  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30*05b00f60SXin Li  * POSSIBILITY OF SUCH DAMAGE.
31*05b00f60SXin Li  */
32*05b00f60SXin Li 
33*05b00f60SXin Li /* \summary: version-independent OpenFlow printer */
34*05b00f60SXin Li 
35*05b00f60SXin Li #ifdef HAVE_CONFIG_H
36*05b00f60SXin Li #include <config.h>
37*05b00f60SXin Li #endif
38*05b00f60SXin Li 
39*05b00f60SXin Li #include "netdissect-stdinc.h"
40*05b00f60SXin Li 
41*05b00f60SXin Li #define ND_LONGJMP_FROM_TCHECK
42*05b00f60SXin Li #include "netdissect.h"
43*05b00f60SXin Li #include "extract.h"
44*05b00f60SXin Li #include "openflow.h"
45*05b00f60SXin Li #include "oui.h"
46*05b00f60SXin Li 
47*05b00f60SXin Li 
48*05b00f60SXin Li static const struct tok ofver_str[] = {
49*05b00f60SXin Li 	{ OF_VER_1_0,	"1.0" },
50*05b00f60SXin Li 	{ OF_VER_1_1,	"1.1" },
51*05b00f60SXin Li 	{ OF_VER_1_2,	"1.2" },
52*05b00f60SXin Li 	{ OF_VER_1_3,	"1.3" },
53*05b00f60SXin Li 	{ OF_VER_1_4,	"1.4" },
54*05b00f60SXin Li 	{ OF_VER_1_5,	"1.5" },
55*05b00f60SXin Li 	{ 0, NULL }
56*05b00f60SXin Li };
57*05b00f60SXin Li 
58*05b00f60SXin Li const struct tok onf_exp_str[] = {
59*05b00f60SXin Li 	{ ONF_EXP_ONF,               "ONF Extensions"                                  },
60*05b00f60SXin Li 	{ ONF_EXP_BUTE,              "Budapest University of Technology and Economics" },
61*05b00f60SXin Li 	{ ONF_EXP_NOVIFLOW,          "NoviFlow"                                        },
62*05b00f60SXin Li 	{ ONF_EXP_L3,                "L3+ Extensions, Vendor Neutral"                  },
63*05b00f60SXin Li 	{ ONF_EXP_L4L7,              "L4-L7 Extensions"                                },
64*05b00f60SXin Li 	{ ONF_EXP_WMOB,              "Wireless and Mobility Extensions"                },
65*05b00f60SXin Li 	{ ONF_EXP_FABS,              "Forwarding Abstractions Extensions"              },
66*05b00f60SXin Li 	{ ONF_EXP_OTRANS,            "Optical Transport Extensions"                    },
67*05b00f60SXin Li 	{ ONF_EXP_NBLNCTU,           "Network Benchmarking Lab, NCTU"                  },
68*05b00f60SXin Li 	{ ONF_EXP_MPCE,              "Mobile Packet Core Extensions"                   },
69*05b00f60SXin Li 	{ ONF_EXP_MPLSTPSPTN,        "MPLS-TP OpenFlow Extensions for SPTN"            },
70*05b00f60SXin Li 	{ 0, NULL }
71*05b00f60SXin Li };
72*05b00f60SXin Li 
73*05b00f60SXin Li const char *
of_vendor_name(const uint32_t vendor)74*05b00f60SXin Li of_vendor_name(const uint32_t vendor)
75*05b00f60SXin Li {
76*05b00f60SXin Li 	const struct tok *table = (vendor & 0xff000000) == 0 ? oui_values : onf_exp_str;
77*05b00f60SXin Li 	return tok2str(table, "unknown", vendor);
78*05b00f60SXin Li }
79*05b00f60SXin Li 
80*05b00f60SXin Li void
of_bitmap_print(netdissect_options * ndo,const struct tok * t,const uint32_t v,const uint32_t u)81*05b00f60SXin Li of_bitmap_print(netdissect_options *ndo,
82*05b00f60SXin Li                 const struct tok *t, const uint32_t v, const uint32_t u)
83*05b00f60SXin Li {
84*05b00f60SXin Li 	/* Assigned bits? */
85*05b00f60SXin Li 	if (v & ~u)
86*05b00f60SXin Li 		ND_PRINT(" (%s)", bittok2str(t, "", v));
87*05b00f60SXin Li 	/* Unassigned bits? */
88*05b00f60SXin Li 	if (v & u)
89*05b00f60SXin Li 		ND_PRINT(" (bogus)");
90*05b00f60SXin Li }
91*05b00f60SXin Li 
92*05b00f60SXin Li void
of_data_print(netdissect_options * ndo,const u_char * cp,const u_int len)93*05b00f60SXin Li of_data_print(netdissect_options *ndo,
94*05b00f60SXin Li               const u_char *cp, const u_int len)
95*05b00f60SXin Li {
96*05b00f60SXin Li 	if (len == 0)
97*05b00f60SXin Li 		return;
98*05b00f60SXin Li 	/* data */
99*05b00f60SXin Li 	ND_PRINT("\n\t data (%u octets)", len);
100*05b00f60SXin Li 	if (ndo->ndo_vflag >= 2)
101*05b00f60SXin Li 		hex_and_ascii_print(ndo, "\n\t  ", cp, len);
102*05b00f60SXin Li 	else
103*05b00f60SXin Li 		ND_TCHECK_LEN(cp, len);
104*05b00f60SXin Li }
105*05b00f60SXin Li 
106*05b00f60SXin Li static void
of_message_print(netdissect_options * ndo,const u_char * cp,uint16_t len,const struct of_msgtypeinfo * mti)107*05b00f60SXin Li of_message_print(netdissect_options *ndo,
108*05b00f60SXin Li                  const u_char *cp, uint16_t len,
109*05b00f60SXin Li                  const struct of_msgtypeinfo *mti)
110*05b00f60SXin Li {
111*05b00f60SXin Li 	/*
112*05b00f60SXin Li 	 * Here "cp" and "len" stand for the message part beyond the common
113*05b00f60SXin Li 	 * OpenFlow 1.0 header, if any.
114*05b00f60SXin Li 	 *
115*05b00f60SXin Li 	 * Most message types are longer than just the header, and the length
116*05b00f60SXin Li 	 * constraints may be complex. When possible, validate the constraint
117*05b00f60SXin Li 	 * completely here (REQ_FIXLEN), otherwise check that the message is
118*05b00f60SXin Li 	 * long enough to begin the decoding (REQ_MINLEN) and have the
119*05b00f60SXin Li 	 * type-specific function do any remaining validation.
120*05b00f60SXin Li 	 */
121*05b00f60SXin Li 
122*05b00f60SXin Li 	if (!mti)
123*05b00f60SXin Li 		goto tcheck_remainder;
124*05b00f60SXin Li 
125*05b00f60SXin Li 	if ((mti->req_what == REQ_FIXLEN && len != mti->req_value) ||
126*05b00f60SXin Li 	    (mti->req_what == REQ_MINLEN && len <  mti->req_value))
127*05b00f60SXin Li 		goto invalid;
128*05b00f60SXin Li 
129*05b00f60SXin Li 	if (!ndo->ndo_vflag || !mti->decoder)
130*05b00f60SXin Li 		goto tcheck_remainder;
131*05b00f60SXin Li 
132*05b00f60SXin Li 	mti->decoder(ndo, cp, len);
133*05b00f60SXin Li 	return;
134*05b00f60SXin Li 
135*05b00f60SXin Li invalid:
136*05b00f60SXin Li 	nd_print_invalid(ndo);
137*05b00f60SXin Li tcheck_remainder:
138*05b00f60SXin Li 	ND_TCHECK_LEN(cp, len);
139*05b00f60SXin Li }
140*05b00f60SXin Li 
141*05b00f60SXin Li /* Print a TCP segment worth of OpenFlow messages presuming the segment begins
142*05b00f60SXin Li  * on a message boundary. */
143*05b00f60SXin Li void
openflow_print(netdissect_options * ndo,const u_char * cp,u_int len)144*05b00f60SXin Li openflow_print(netdissect_options *ndo, const u_char *cp, u_int len)
145*05b00f60SXin Li {
146*05b00f60SXin Li 	ndo->ndo_protocol = "openflow";
147*05b00f60SXin Li 	ND_PRINT(": OpenFlow");
148*05b00f60SXin Li 	while (len) {
149*05b00f60SXin Li 		/* Print a single OpenFlow message. */
150*05b00f60SXin Li 		uint8_t version, type;
151*05b00f60SXin Li 		uint16_t length;
152*05b00f60SXin Li 		const struct of_msgtypeinfo *mti;
153*05b00f60SXin Li 
154*05b00f60SXin Li 		/* version */
155*05b00f60SXin Li 		version = GET_U_1(cp);
156*05b00f60SXin Li 		OF_FWD(1);
157*05b00f60SXin Li 		ND_PRINT("\n\tversion %s",
158*05b00f60SXin Li 		         tok2str(ofver_str, "unknown (0x%02x)", version));
159*05b00f60SXin Li 		/* type */
160*05b00f60SXin Li 		if (len < 1)
161*05b00f60SXin Li 			goto partial_header;
162*05b00f60SXin Li 		type = GET_U_1(cp);
163*05b00f60SXin Li 		OF_FWD(1);
164*05b00f60SXin Li 		mti =
165*05b00f60SXin Li 			version == OF_VER_1_0 ? of10_identify_msgtype(type) :
166*05b00f60SXin Li 			version == OF_VER_1_3 ? of13_identify_msgtype(type) :
167*05b00f60SXin Li 			NULL;
168*05b00f60SXin Li 		if (mti && mti->name)
169*05b00f60SXin Li 			ND_PRINT(", type %s", mti->name);
170*05b00f60SXin Li 		else
171*05b00f60SXin Li 			ND_PRINT(", type unknown (0x%02x)", type);
172*05b00f60SXin Li 		/* length */
173*05b00f60SXin Li 		if (len < 2)
174*05b00f60SXin Li 			goto partial_header;
175*05b00f60SXin Li 		length = GET_BE_U_2(cp);
176*05b00f60SXin Li 		OF_FWD(2);
177*05b00f60SXin Li 		ND_PRINT(", length %u%s", length,
178*05b00f60SXin Li 		         length < OF_HEADER_FIXLEN ? " (too short!)" : "");
179*05b00f60SXin Li 		/* xid */
180*05b00f60SXin Li 		if (len < 4)
181*05b00f60SXin Li 			goto partial_header;
182*05b00f60SXin Li 		ND_PRINT(", xid 0x%08x", GET_BE_U_4(cp));
183*05b00f60SXin Li 		OF_FWD(4);
184*05b00f60SXin Li 
185*05b00f60SXin Li 		/*
186*05b00f60SXin Li 		 * When a TCP packet can contain several protocol messages,
187*05b00f60SXin Li 		 * and at the same time a protocol message can span several
188*05b00f60SXin Li 		 * TCP packets, decoding an incomplete message at the end of
189*05b00f60SXin Li 		 * a TCP packet requires attention to detail in this loop.
190*05b00f60SXin Li 		 *
191*05b00f60SXin Li 		 * Message length includes the header length and a message
192*05b00f60SXin Li 		 * always includes the basic header. A message length underrun
193*05b00f60SXin Li 		 * fails decoding of the rest of the current packet. At the
194*05b00f60SXin Li 		 * same time, try decoding as much of the current message as
195*05b00f60SXin Li 		 * possible even when it does not end within the current TCP
196*05b00f60SXin Li 		 * segment.
197*05b00f60SXin Li 		 *
198*05b00f60SXin Li 		 * Specifically, to try to process the message body in this
199*05b00f60SXin Li 		 * iteration do NOT require the header "length" to be small
200*05b00f60SXin Li 		 * enough for the full declared OpenFlow message to fit into
201*05b00f60SXin Li 		 * the remainder of the declared TCP segment, same as the full
202*05b00f60SXin Li 		 * declared TCP segment is not required to fit into the
203*05b00f60SXin Li 		 * captured packet buffer.
204*05b00f60SXin Li 		 *
205*05b00f60SXin Li 		 * But DO require the same at the end of this iteration to
206*05b00f60SXin Li 		 * decrement "len" and to proceed to the next iteration.
207*05b00f60SXin Li 		 * (Ideally the declared TCP payload end will be at or after
208*05b00f60SXin Li 		 * the captured packet buffer end, but stay safe even when
209*05b00f60SXin Li 		 * that's somehow not the case.)
210*05b00f60SXin Li 		 */
211*05b00f60SXin Li 		if (length < OF_HEADER_FIXLEN)
212*05b00f60SXin Li 			goto invalid;
213*05b00f60SXin Li 
214*05b00f60SXin Li 		of_message_print(ndo, cp, length - OF_HEADER_FIXLEN, mti);
215*05b00f60SXin Li 		if (length - OF_HEADER_FIXLEN > len)
216*05b00f60SXin Li 			break;
217*05b00f60SXin Li 		OF_FWD(length - OF_HEADER_FIXLEN);
218*05b00f60SXin Li 	} /* while (len) */
219*05b00f60SXin Li 	return;
220*05b00f60SXin Li 
221*05b00f60SXin Li partial_header:
222*05b00f60SXin Li 	ND_PRINT(" (end of TCP payload)");
223*05b00f60SXin Li 	ND_TCHECK_LEN(cp, len);
224*05b00f60SXin Li 	return;
225*05b00f60SXin Li invalid: /* fail the current packet */
226*05b00f60SXin Li 	nd_print_invalid(ndo);
227*05b00f60SXin Li 	ND_TCHECK_LEN(cp, len);
228*05b00f60SXin Li }
229