]> The Tcpdump Group git mirrors - tcpdump/blob - print-openflow.c
OpenFlow 1.3: Add initial partial support.
[tcpdump] / print-openflow.c
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 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "netdissect-stdinc.h"
40
41 #define ND_LONGJMP_FROM_TCHECK
42 #include "netdissect.h"
43 #include "extract.h"
44 #include "openflow.h"
45 #include "oui.h"
46
47
48 #define OF_VER_1_0 0x01
49 #define OF_VER_1_1 0x02
50 #define OF_VER_1_2 0x03
51 #define OF_VER_1_3 0x04
52 #define OF_VER_1_4 0x05
53 #define OF_VER_1_5 0x06
54
55 static const struct tok ofver_str[] = {
56 { OF_VER_1_0, "1.0" },
57 { OF_VER_1_1, "1.1" },
58 { OF_VER_1_2, "1.2" },
59 { OF_VER_1_3, "1.3" },
60 { OF_VER_1_4, "1.4" },
61 { OF_VER_1_5, "1.5" },
62 { 0, NULL }
63 };
64
65 const struct tok onf_exp_str[] = {
66 { ONF_EXP_ONF, "ONF Extensions" },
67 { ONF_EXP_BUTE, "Budapest University of Technology and Economics" },
68 { ONF_EXP_NOVIFLOW, "NoviFlow" },
69 { ONF_EXP_L3, "L3+ Extensions, Vendor Neutral" },
70 { ONF_EXP_L4L7, "L4-L7 Extensions" },
71 { ONF_EXP_WMOB, "Wireless and Mobility Extensions" },
72 { ONF_EXP_FABS, "Forwarding Abstractions Extensions" },
73 { ONF_EXP_OTRANS, "Optical Transport Extensions" },
74 { 0, NULL }
75 };
76
77 const char *
78 of_vendor_name(const uint32_t vendor)
79 {
80 const struct tok *table = (vendor & 0xff000000) == 0 ? oui_values : onf_exp_str;
81 return tok2str(table, "unknown", vendor);
82 }
83
84 void
85 of_data_print(netdissect_options *ndo,
86 const u_char *cp, const u_int len)
87 {
88 if (len == 0)
89 return;
90 /* data */
91 ND_PRINT("\n\t data (%u octets)", len);
92 if (ndo->ndo_vflag >= 2)
93 hex_and_ascii_print(ndo, "\n\t ", cp, len);
94 else
95 ND_TCHECK_LEN(cp, len);
96 }
97
98 /* Print a TCP segment worth of OpenFlow messages presuming the segment begins
99 * on a message boundary. */
100 void
101 openflow_print(netdissect_options *ndo, const u_char *cp, u_int len)
102 {
103 ndo->ndo_protocol = "openflow";
104 ND_PRINT(": OpenFlow");
105 while (len) {
106 /* Print a single OpenFlow message. */
107 uint8_t version, type;
108 uint16_t length;
109 void (*decoder)(struct netdissect_options *,
110 const u_char *, u_int16_t, const uint8_t) = NULL;
111
112 /* version */
113 version = GET_U_1(cp);
114 OF_FWD(1);
115 ND_PRINT("\n\tversion %s",
116 tok2str(ofver_str, "unknown (0x%02x)", version));
117 /* type */
118 if (len < 1)
119 goto partial_header;
120 type = GET_U_1(cp);
121 OF_FWD(1);
122 switch (version) {
123 case OF_VER_1_0:
124 ND_PRINT(", type %s", of10_msgtype_str(type));
125 decoder = of10_message_print;
126 break;
127 case OF_VER_1_3:
128 ND_PRINT(", type %s", of13_msgtype_str(type));
129 decoder = of13_message_print;
130 break;
131 default:
132 ND_PRINT(", type unknown (0x%02x)", type);
133 }
134 /* length */
135 if (len < 2)
136 goto partial_header;
137 length = GET_BE_U_2(cp);
138 OF_FWD(2);
139 ND_PRINT(", length %u%s", length,
140 length < OF_HEADER_FIXLEN ? " (too short!)" : "");
141 /* xid */
142 if (len < 4)
143 goto partial_header;
144 ND_PRINT(", xid 0x%08x", GET_BE_U_4(cp));
145 OF_FWD(4);
146
147 /*
148 * When a TCP packet can contain several protocol messages,
149 * and at the same time a protocol message can span several
150 * TCP packets, decoding an incomplete message at the end of
151 * a TCP packet requires attention to detail in this loop.
152 *
153 * Message length includes the header length and a message
154 * always includes the basic header. A message length underrun
155 * fails decoding of the rest of the current packet. At the
156 * same time, try decoding as much of the current message as
157 * possible even when it does not end within the current TCP
158 * segment.
159 *
160 * Specifically, to try to process the message body in this
161 * iteration do NOT require the header "length" to be small
162 * enough for the full declared OpenFlow message to fit into
163 * the remainder of the declared TCP segment, same as the full
164 * declared TCP segment is not required to fit into the
165 * captured packet buffer.
166 *
167 * But DO require the same at the end of this iteration to
168 * decrement "len" and to proceed to the next iteration.
169 * (Ideally the declared TCP payload end will be at or after
170 * the captured packet buffer end, but stay safe even when
171 * that's somehow not the case.)
172 */
173 if (length < OF_HEADER_FIXLEN)
174 goto invalid;
175
176 if (decoder != NULL)
177 decoder(ndo, cp, length - OF_HEADER_FIXLEN, type);
178 else
179 ND_TCHECK_LEN(cp, length - OF_HEADER_FIXLEN);
180 if (length - OF_HEADER_FIXLEN > len)
181 break;
182 OF_FWD(length - OF_HEADER_FIXLEN);
183 } /* while (len) */
184 return;
185
186 partial_header:
187 ND_PRINT(" (end of TCP payload)");
188 ND_TCHECK_LEN(cp, len);
189 return;
190 invalid: /* fail the current packet */
191 nd_print_invalid(ndo);
192 ND_TCHECK_LEN(cp, len);
193 }