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