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