]> The Tcpdump Group git mirrors - tcpdump/blob - print-openflow-1.0.c
NDOize print-ascii.c further
[tcpdump] / print-openflow-1.0.c
1 /*
2 * This module implements decoding of OpenFlow protocol version 1.0 (wire
3 * protocol 0x01). The decoder implements terse (default), detailed (-v) and
4 * full (-vv) output formats and, as much as each format implies, detects and
5 * tries to work around sizing anomalies inside the messages. The decoder marks
6 * up bogus values of selected message fields and decodes partially captured
7 * messages up to the snapshot end. It is based on the specification below:
8 *
9 * [OF10] http://www.openflow.org/documents/openflow-spec-v1.0.0.pdf
10 *
11 * Decoding of Ethernet frames nested in OFPT_PACKET_IN and OFPT_PACKET_OUT
12 * messages is done only when the verbosity level set by command-line argument
13 * is "-vvv" or higher. In that case the verbosity level is temporarily
14 * decremented by 3 during the nested frame decoding. For example, running
15 * tcpdump with "-vvvv" will do full decoding of OpenFlow and "-v" decoding of
16 * the nested frames.
17 *
18 *
19 * Copyright (c) 2013 The TCPDUMP project
20 * All rights reserved.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
34 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
35 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
36 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
37 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
41 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGE.
43 */
44
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include <tcpdump-stdinc.h>
50
51 #include "interface.h"
52 #include "extract.h"
53 #include "addrtoname.h"
54 #include "ether.h"
55 #include "ethertype.h"
56 #include "ipproto.h"
57 #include "openflow.h"
58
59 static const char tstr[] = " [|openflow]";
60 static const char cstr[] = " (corrupt)";
61
62 #define OFPT_HELLO 0x00
63 #define OFPT_ERROR 0x01
64 #define OFPT_ECHO_REQUEST 0x02
65 #define OFPT_ECHO_REPLY 0x03
66 #define OFPT_VENDOR 0x04
67 #define OFPT_FEATURES_REQUEST 0x05
68 #define OFPT_FEATURES_REPLY 0x06
69 #define OFPT_GET_CONFIG_REQUEST 0x07
70 #define OFPT_GET_CONFIG_REPLY 0x08
71 #define OFPT_SET_CONFIG 0x09
72 #define OFPT_PACKET_IN 0x0a
73 #define OFPT_FLOW_REMOVED 0x0b
74 #define OFPT_PORT_STATUS 0x0c
75 #define OFPT_PACKET_OUT 0x0d
76 #define OFPT_FLOW_MOD 0x0e
77 #define OFPT_PORT_MOD 0x0f
78 #define OFPT_STATS_REQUEST 0x10
79 #define OFPT_STATS_REPLY 0x11
80 #define OFPT_BARRIER_REQUEST 0x12
81 #define OFPT_BARRIER_REPLY 0x13
82 #define OFPT_QUEUE_GET_CONFIG_REQUEST 0x14
83 #define OFPT_QUEUE_GET_CONFIG_REPLY 0x15
84 static const struct tok ofpt_str[] = {
85 { OFPT_HELLO, "HELLO" },
86 { OFPT_ERROR, "ERROR" },
87 { OFPT_ECHO_REQUEST, "ECHO_REQUEST" },
88 { OFPT_ECHO_REPLY, "ECHO_REPLY" },
89 { OFPT_VENDOR, "VENDOR" },
90 { OFPT_FEATURES_REQUEST, "FEATURES_REQUEST" },
91 { OFPT_FEATURES_REPLY, "FEATURES_REPLY" },
92 { OFPT_GET_CONFIG_REQUEST, "GET_CONFIG_REQUEST" },
93 { OFPT_GET_CONFIG_REPLY, "GET_CONFIG_REPLY" },
94 { OFPT_SET_CONFIG, "SET_CONFIG" },
95 { OFPT_PACKET_IN, "PACKET_IN" },
96 { OFPT_FLOW_REMOVED, "FLOW_REMOVED" },
97 { OFPT_PORT_STATUS, "PORT_STATUS" },
98 { OFPT_PACKET_OUT, "PACKET_OUT" },
99 { OFPT_FLOW_MOD, "FLOW_MOD" },
100 { OFPT_PORT_MOD, "PORT_MOD" },
101 { OFPT_STATS_REQUEST, "STATS_REQUEST" },
102 { OFPT_STATS_REPLY, "STATS_REPLY" },
103 { OFPT_BARRIER_REQUEST, "BARRIER_REQUEST" },
104 { OFPT_BARRIER_REPLY, "BARRIER_REPLY" },
105 { OFPT_QUEUE_GET_CONFIG_REQUEST, "QUEUE_GET_CONFIG_REQUEST" },
106 { OFPT_QUEUE_GET_CONFIG_REPLY, "QUEUE_GET_CONFIG_REPLY" },
107 { 0, NULL }
108 };
109
110 #define OFPPC_PORT_DOWN (1 << 0)
111 #define OFPPC_NO_STP (1 << 1)
112 #define OFPPC_NO_RECV (1 << 2)
113 #define OFPPC_NO_RECV_STP (1 << 3)
114 #define OFPPC_NO_FLOOD (1 << 4)
115 #define OFPPC_NO_FWD (1 << 5)
116 #define OFPPC_NO_PACKET_IN (1 << 6)
117 static const struct tok ofppc_bm[] = {
118 { OFPPC_PORT_DOWN, "PORT_DOWN" },
119 { OFPPC_NO_STP, "NO_STP" },
120 { OFPPC_NO_RECV, "NO_RECV" },
121 { OFPPC_NO_RECV_STP, "NO_RECV_STP" },
122 { OFPPC_NO_FLOOD, "NO_FLOOD" },
123 { OFPPC_NO_FWD, "NO_FWD" },
124 { OFPPC_NO_PACKET_IN, "NO_PACKET_IN" },
125 { 0, NULL }
126 };
127 #define OFPPC_U (~(OFPPC_PORT_DOWN | OFPPC_NO_STP | OFPPC_NO_RECV | \
128 OFPPC_NO_RECV_STP | OFPPC_NO_FLOOD | OFPPC_NO_FWD | \
129 OFPPC_NO_PACKET_IN))
130
131 #define OFPPS_LINK_DOWN (1 << 0)
132 #define OFPPS_STP_LISTEN (0 << 8)
133 #define OFPPS_STP_LEARN (1 << 8)
134 #define OFPPS_STP_FORWARD (2 << 8)
135 #define OFPPS_STP_BLOCK (3 << 8)
136 #define OFPPS_STP_MASK (3 << 8)
137 static const struct tok ofpps_bm[] = {
138 { OFPPS_LINK_DOWN, "LINK_DOWN" },
139 { OFPPS_STP_LISTEN, "STP_LISTEN" },
140 { OFPPS_STP_LEARN, "STP_LEARN" },
141 { OFPPS_STP_FORWARD, "STP_FORWARD" },
142 { OFPPS_STP_BLOCK, "STP_BLOCK" },
143 { 0, NULL }
144 };
145 #define OFPPS_U (~(OFPPS_LINK_DOWN | OFPPS_STP_LISTEN | OFPPS_STP_LEARN | \
146 OFPPS_STP_FORWARD | OFPPS_STP_BLOCK))
147
148 #define OFPP_MAX 0xff00
149 #define OFPP_IN_PORT 0xfff8
150 #define OFPP_TABLE 0xfff9
151 #define OFPP_NORMAL 0xfffa
152 #define OFPP_FLOOD 0xfffb
153 #define OFPP_ALL 0xfffc
154 #define OFPP_CONTROLLER 0xfffd
155 #define OFPP_LOCAL 0xfffe
156 #define OFPP_NONE 0xffff
157 static const struct tok ofpp_str[] = {
158 { OFPP_MAX, "MAX" },
159 { OFPP_IN_PORT, "IN_PORT" },
160 { OFPP_TABLE, "TABLE" },
161 { OFPP_NORMAL, "NORMAL" },
162 { OFPP_FLOOD, "FLOOD" },
163 { OFPP_ALL, "ALL" },
164 { OFPP_CONTROLLER, "CONTROLLER" },
165 { OFPP_LOCAL, "LOCAL" },
166 { OFPP_NONE, "NONE" },
167 { 0, NULL }
168 };
169
170 #define OFPPF_10MB_HD (1 << 0)
171 #define OFPPF_10MB_FD (1 << 1)
172 #define OFPPF_100MB_HD (1 << 2)
173 #define OFPPF_100MB_FD (1 << 3)
174 #define OFPPF_1GB_HD (1 << 4)
175 #define OFPPF_1GB_FD (1 << 5)
176 #define OFPPF_10GB_FD (1 << 6)
177 #define OFPPF_COPPER (1 << 7)
178 #define OFPPF_FIBER (1 << 8)
179 #define OFPPF_AUTONEG (1 << 9)
180 #define OFPPF_PAUSE (1 << 10)
181 #define OFPPF_PAUSE_ASYM (1 << 11)
182 static const struct tok ofppf_bm[] = {
183 { OFPPF_10MB_HD, "10MB_HD" },
184 { OFPPF_10MB_FD, "10MB_FD" },
185 { OFPPF_100MB_HD, "100MB_HD" },
186 { OFPPF_100MB_FD, "100MB_FD" },
187 { OFPPF_1GB_HD, "1GB_HD" },
188 { OFPPF_1GB_FD, "1GB_FD" },
189 { OFPPF_10GB_FD, "10GB_FD" },
190 { OFPPF_COPPER, "COPPER" },
191 { OFPPF_FIBER, "FIBER" },
192 { OFPPF_AUTONEG, "AUTONEG" },
193 { OFPPF_PAUSE, "PAUSE" },
194 { OFPPF_PAUSE_ASYM, "PAUSE_ASYM" },
195 { 0, NULL }
196 };
197 #define OFPPF_U (~(OFPPF_10MB_HD | OFPPF_10MB_FD | OFPPF_100MB_HD | \
198 OFPPF_100MB_FD | OFPPF_1GB_HD | OFPPF_1GB_FD | \
199 OFPPF_10GB_FD | OFPPF_COPPER | OFPPF_FIBER | \
200 OFPPF_AUTONEG | OFPPF_PAUSE | OFPPF_PAUSE_ASYM))
201
202 #define OFPQT_NONE 0x0000
203 #define OFPQT_MIN_RATE 0x0001
204 static const struct tok ofpqt_str[] = {
205 { OFPQT_NONE, "NONE" },
206 { OFPQT_MIN_RATE, "MIN_RATE" },
207 { 0, NULL }
208 };
209
210 #define OFPFW_IN_PORT (1 << 0)
211 #define OFPFW_DL_VLAN (1 << 1)
212 #define OFPFW_DL_SRC (1 << 2)
213 #define OFPFW_DL_DST (1 << 3)
214 #define OFPFW_DL_TYPE (1 << 4)
215 #define OFPFW_NW_PROTO (1 << 5)
216 #define OFPFW_TP_SRC (1 << 6)
217 #define OFPFW_TP_DST (1 << 7)
218 #define OFPFW_NW_SRC_SHIFT 8
219 #define OFPFW_NW_SRC_BITS 6
220 #define OFPFW_NW_SRC_MASK (((1 << OFPFW_NW_SRC_BITS) - 1) << OFPFW_NW_SRC_SHIFT)
221 #define OFPFW_NW_DST_SHIFT 14
222 #define OFPFW_NW_DST_BITS 6
223 #define OFPFW_NW_DST_MASK (((1 << OFPFW_NW_DST_BITS) - 1) << OFPFW_NW_DST_SHIFT)
224 #define OFPFW_DL_VLAN_PCP (1 << 20)
225 #define OFPFW_NW_TOS (1 << 21)
226 #define OFPFW_ALL ((1 << 22) - 1)
227 static const struct tok ofpfw_bm[] = {
228 { OFPFW_IN_PORT, "IN_PORT" },
229 { OFPFW_DL_VLAN, "DL_VLAN" },
230 { OFPFW_DL_SRC, "DL_SRC" },
231 { OFPFW_DL_DST, "DL_DST" },
232 { OFPFW_DL_TYPE, "DL_TYPE" },
233 { OFPFW_NW_PROTO, "NW_PROTO" },
234 { OFPFW_TP_SRC, "TP_SRC" },
235 { OFPFW_TP_DST, "TP_DST" },
236 { OFPFW_DL_VLAN_PCP, "DL_VLAN_PCP" },
237 { OFPFW_NW_TOS, "NW_TOS" },
238 { 0, NULL }
239 };
240 /* The above array does not include bits 8~13 (OFPFW_NW_SRC_*) and 14~19
241 * (OFPFW_NW_DST_*), which are not a part of the bitmap and require decoding
242 * other than that of tok2str(). The macro below includes these bits such that
243 * they are not reported as bogus in the decoding. */
244 #define OFPFW_U (~(OFPFW_ALL))
245
246 #define OFPAT_OUTPUT 0x0000
247 #define OFPAT_SET_VLAN_VID 0x0001
248 #define OFPAT_SET_VLAN_PCP 0x0002
249 #define OFPAT_STRIP_VLAN 0x0003
250 #define OFPAT_SET_DL_SRC 0x0004
251 #define OFPAT_SET_DL_DST 0x0005
252 #define OFPAT_SET_NW_SRC 0x0006
253 #define OFPAT_SET_NW_DST 0x0007
254 #define OFPAT_SET_NW_TOS 0x0008
255 #define OFPAT_SET_TP_SRC 0x0009
256 #define OFPAT_SET_TP_DST 0x000a
257 #define OFPAT_ENQUEUE 0x000b
258 #define OFPAT_VENDOR 0xffff
259 static const struct tok ofpat_str[] = {
260 { OFPAT_OUTPUT, "OUTPUT" },
261 { OFPAT_SET_VLAN_VID, "SET_VLAN_VID" },
262 { OFPAT_SET_VLAN_PCP, "SET_VLAN_PCP" },
263 { OFPAT_STRIP_VLAN, "STRIP_VLAN" },
264 { OFPAT_SET_DL_SRC, "SET_DL_SRC" },
265 { OFPAT_SET_DL_DST, "SET_DL_DST" },
266 { OFPAT_SET_NW_SRC, "SET_NW_SRC" },
267 { OFPAT_SET_NW_DST, "SET_NW_DST" },
268 { OFPAT_SET_NW_TOS, "SET_NW_TOS" },
269 { OFPAT_SET_TP_SRC, "SET_TP_SRC" },
270 { OFPAT_SET_TP_DST, "SET_TP_DST" },
271 { OFPAT_ENQUEUE, "ENQUEUE" },
272 { OFPAT_VENDOR, "VENDOR" },
273 { 0, NULL }
274 };
275
276 /* bit-shifted, w/o vendor action */
277 static const struct tok ofpat_bm[] = {
278 { 1 << OFPAT_OUTPUT, "OUTPUT" },
279 { 1 << OFPAT_SET_VLAN_VID, "SET_VLAN_VID" },
280 { 1 << OFPAT_SET_VLAN_PCP, "SET_VLAN_PCP" },
281 { 1 << OFPAT_STRIP_VLAN, "STRIP_VLAN" },
282 { 1 << OFPAT_SET_DL_SRC, "SET_DL_SRC" },
283 { 1 << OFPAT_SET_DL_DST, "SET_DL_DST" },
284 { 1 << OFPAT_SET_NW_SRC, "SET_NW_SRC" },
285 { 1 << OFPAT_SET_NW_DST, "SET_NW_DST" },
286 { 1 << OFPAT_SET_NW_TOS, "SET_NW_TOS" },
287 { 1 << OFPAT_SET_TP_SRC, "SET_TP_SRC" },
288 { 1 << OFPAT_SET_TP_DST, "SET_TP_DST" },
289 { 1 << OFPAT_ENQUEUE, "ENQUEUE" },
290 { 0, NULL }
291 };
292 #define OFPAT_U (~(1 << OFPAT_OUTPUT | 1 << OFPAT_SET_VLAN_VID | \
293 1 << OFPAT_SET_VLAN_PCP | 1 << OFPAT_STRIP_VLAN | \
294 1 << OFPAT_SET_DL_SRC | 1 << OFPAT_SET_DL_DST | \
295 1 << OFPAT_SET_NW_SRC | 1 << OFPAT_SET_NW_DST | \
296 1 << OFPAT_SET_NW_TOS | 1 << OFPAT_SET_TP_SRC | \
297 1 << OFPAT_SET_TP_DST | 1 << OFPAT_ENQUEUE))
298
299 #define OFPC_FLOW_STATS (1 << 0)
300 #define OFPC_TABLE_STATS (1 << 1)
301 #define OFPC_PORT_STATS (1 << 2)
302 #define OFPC_STP (1 << 3)
303 #define OFPC_RESERVED (1 << 4)
304 #define OFPC_IP_REASM (1 << 5)
305 #define OFPC_QUEUE_STATS (1 << 6)
306 #define OFPC_ARP_MATCH_IP (1 << 7)
307 static const struct tok ofp_capabilities_bm[] = {
308 { OFPC_FLOW_STATS, "FLOW_STATS" },
309 { OFPC_TABLE_STATS, "TABLE_STATS" },
310 { OFPC_PORT_STATS, "PORT_STATS" },
311 { OFPC_STP, "STP" },
312 { OFPC_RESERVED, "RESERVED" }, /* not in the mask below */
313 { OFPC_IP_REASM, "IP_REASM" },
314 { OFPC_QUEUE_STATS, "QUEUE_STATS" },
315 { OFPC_ARP_MATCH_IP, "ARP_MATCH_IP" },
316 { 0, NULL }
317 };
318 #define OFPCAP_U (~(OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
319 OFPC_STP | OFPC_IP_REASM | OFPC_QUEUE_STATS | \
320 OFPC_ARP_MATCH_IP))
321
322 #define OFPC_FRAG_NORMAL 0x0000
323 #define OFPC_FRAG_DROP 0x0001
324 #define OFPC_FRAG_REASM 0x0002
325 #define OFPC_FRAG_MASK 0x0003
326 static const struct tok ofp_config_str[] = {
327 { OFPC_FRAG_NORMAL, "FRAG_NORMAL" },
328 { OFPC_FRAG_DROP, "FRAG_DROP" },
329 { OFPC_FRAG_REASM, "FRAG_REASM" },
330 { 0, NULL }
331 };
332
333 #define OFPFC_ADD 0x0000
334 #define OFPFC_MODIFY 0x0001
335 #define OFPFC_MODIFY_STRICT 0x0002
336 #define OFPFC_DELETE 0x0003
337 #define OFPFC_DELETE_STRICT 0x0004
338 static const struct tok ofpfc_str[] = {
339 { OFPFC_ADD, "ADD" },
340 { OFPFC_MODIFY, "MODIFY" },
341 { OFPFC_MODIFY_STRICT, "MODIFY_STRICT" },
342 { OFPFC_DELETE, "DELETE" },
343 { OFPFC_DELETE_STRICT, "DELETE_STRICT" },
344 { 0, NULL }
345 };
346
347 static const struct tok bufferid_str[] = {
348 { 0xffffffff, "NONE" },
349 { 0, NULL }
350 };
351
352 #define OFPFF_SEND_FLOW_REM (1 << 0)
353 #define OFPFF_CHECK_OVERLAP (1 << 1)
354 #define OFPFF_EMERG (1 << 2)
355 static const struct tok ofpff_bm[] = {
356 { OFPFF_SEND_FLOW_REM, "SEND_FLOW_REM" },
357 { OFPFF_CHECK_OVERLAP, "CHECK_OVERLAP" },
358 { OFPFF_EMERG, "EMERG" },
359 { 0, NULL }
360 };
361 #define OFPFF_U (~(OFPFF_SEND_FLOW_REM | OFPFF_CHECK_OVERLAP | OFPFF_EMERG))
362
363 #define OFPST_DESC 0x0000
364 #define OFPST_FLOW 0x0001
365 #define OFPST_AGGREGATE 0x0002
366 #define OFPST_TABLE 0x0003
367 #define OFPST_PORT 0x0004
368 #define OFPST_QUEUE 0x0005
369 #define OFPST_VENDOR 0xffff
370 static const struct tok ofpst_str[] = {
371 { OFPST_DESC, "DESC" },
372 { OFPST_FLOW, "FLOW" },
373 { OFPST_AGGREGATE, "AGGREGATE" },
374 { OFPST_TABLE, "TABLE" },
375 { OFPST_PORT, "PORT" },
376 { OFPST_QUEUE, "QUEUE" },
377 { OFPST_VENDOR, "VENDOR" },
378 { 0, NULL }
379 };
380
381 static const struct tok tableid_str[] = {
382 { 0xfe, "EMERG" },
383 { 0xff, "ALL" },
384 { 0, NULL }
385 };
386
387 #define OFPQ_ALL 0xffffffff
388 static const struct tok ofpq_str[] = {
389 { OFPQ_ALL, "ALL" },
390 { 0, NULL }
391 };
392
393 #define OFPSF_REPLY_MORE 0x0001
394 static const struct tok ofpsf_reply_bm[] = {
395 { OFPSF_REPLY_MORE, "MORE" },
396 { 0, NULL }
397 };
398 #define OFPSF_REPLY_U (~(OFPSF_REPLY_MORE))
399
400 #define OFPR_NO_MATCH 0x00
401 #define OFPR_ACTION 0x01
402 static const struct tok ofpr_str[] = {
403 { OFPR_NO_MATCH, "NO_MATCH" },
404 { OFPR_ACTION, "ACTION" },
405 { 0, NULL }
406 };
407
408 #define OFPRR_IDLE_TIMEOUT 0x00
409 #define OFPRR_HARD_TIMEOUT 0x01
410 #define OFPRR_DELETE 0x02
411 static const struct tok ofprr_str[] = {
412 { OFPRR_IDLE_TIMEOUT, "IDLE_TIMEOUT" },
413 { OFPRR_HARD_TIMEOUT, "HARD_TIMEOUT" },
414 { OFPRR_DELETE, "DELETE" },
415 { 0, NULL }
416 };
417
418 #define OFPPR_ADD 0x00
419 #define OFPPR_DELETE 0x01
420 #define OFPPR_MODIFY 0x02
421 static const struct tok ofppr_str[] = {
422 { OFPPR_ADD, "ADD" },
423 { OFPPR_DELETE, "DELETE" },
424 { OFPPR_MODIFY, "MODIFY" },
425 { 0, NULL }
426 };
427
428 #define OFPET_HELLO_FAILED 0x0000
429 #define OFPET_BAD_REQUEST 0x0001
430 #define OFPET_BAD_ACTION 0x0002
431 #define OFPET_FLOW_MOD_FAILED 0x0003
432 #define OFPET_PORT_MOD_FAILED 0x0004
433 #define OFPET_QUEUE_OP_FAILED 0x0005
434 static const struct tok ofpet_str[] = {
435 { OFPET_HELLO_FAILED, "HELLO_FAILED" },
436 { OFPET_BAD_REQUEST, "BAD_REQUEST" },
437 { OFPET_BAD_ACTION, "BAD_ACTION" },
438 { OFPET_FLOW_MOD_FAILED, "FLOW_MOD_FAILED" },
439 { OFPET_PORT_MOD_FAILED, "PORT_MOD_FAILED" },
440 { OFPET_QUEUE_OP_FAILED, "QUEUE_OP_FAILED" },
441 { 0, NULL }
442 };
443
444 #define OFPHFC_INCOMPATIBLE 0x0000
445 #define OFPHFC_EPERM 0x0001
446 static const struct tok ofphfc_str[] = {
447 { OFPHFC_INCOMPATIBLE, "INCOMPATIBLE" },
448 { OFPHFC_EPERM, "EPERM" },
449 { 0, NULL }
450 };
451
452 #define OFPBRC_BAD_VERSION 0x0000
453 #define OFPBRC_BAD_TYPE 0x0001
454 #define OFPBRC_BAD_STAT 0x0002
455 #define OFPBRC_BAD_VENDOR 0x0003
456 #define OFPBRC_BAD_SUBTYPE 0x0004
457 #define OFPBRC_EPERM 0x0005
458 #define OFPBRC_BAD_LEN 0x0006
459 #define OFPBRC_BUFFER_EMPTY 0x0007
460 #define OFPBRC_BUFFER_UNKNOWN 0x0008
461 static const struct tok ofpbrc_str[] = {
462 { OFPBRC_BAD_VERSION, "BAD_VERSION" },
463 { OFPBRC_BAD_TYPE, "BAD_TYPE" },
464 { OFPBRC_BAD_STAT, "BAD_STAT" },
465 { OFPBRC_BAD_VENDOR, "BAD_VENDOR" },
466 { OFPBRC_BAD_SUBTYPE, "BAD_SUBTYPE" },
467 { OFPBRC_EPERM, "EPERM" },
468 { OFPBRC_BAD_LEN, "BAD_LEN" },
469 { OFPBRC_BUFFER_EMPTY, "BUFFER_EMPTY" },
470 { OFPBRC_BUFFER_UNKNOWN, "BUFFER_UNKNOWN" },
471 { 0, NULL }
472 };
473
474 #define OFPBAC_BAD_TYPE 0x0000
475 #define OFPBAC_BAD_LEN 0x0001
476 #define OFPBAC_BAD_VENDOR 0x0002
477 #define OFPBAC_BAD_VENDOR_TYPE 0x0003
478 #define OFPBAC_BAD_OUT_PORT 0x0004
479 #define OFPBAC_BAD_ARGUMENT 0x0005
480 #define OFPBAC_EPERM 0x0006
481 #define OFPBAC_TOO_MANY 0x0007
482 #define OFPBAC_BAD_QUEUE 0x0008
483 static const struct tok ofpbac_str[] = {
484 { OFPBAC_BAD_TYPE, "BAD_TYPE" },
485 { OFPBAC_BAD_LEN, "BAD_LEN" },
486 { OFPBAC_BAD_VENDOR, "BAD_VENDOR" },
487 { OFPBAC_BAD_VENDOR_TYPE, "BAD_VENDOR_TYPE" },
488 { OFPBAC_BAD_OUT_PORT, "BAD_OUT_PORT" },
489 { OFPBAC_BAD_ARGUMENT, "BAD_ARGUMENT" },
490 { OFPBAC_EPERM, "EPERM" },
491 { OFPBAC_TOO_MANY, "TOO_MANY" },
492 { OFPBAC_BAD_QUEUE, "BAD_QUEUE" },
493 { 0, NULL }
494 };
495
496 #define OFPFMFC_ALL_TABLES_FULL 0x0000
497 #define OFPFMFC_OVERLAP 0x0001
498 #define OFPFMFC_EPERM 0x0002
499 #define OFPFMFC_BAD_EMERG_TIMEOUT 0x0003
500 #define OFPFMFC_BAD_COMMAND 0x0004
501 #define OFPFMFC_UNSUPPORTED 0x0005
502 static const struct tok ofpfmfc_str[] = {
503 { OFPFMFC_ALL_TABLES_FULL, "ALL_TABLES_FULL" },
504 { OFPFMFC_OVERLAP, "OVERLAP" },
505 { OFPFMFC_EPERM, "EPERM" },
506 { OFPFMFC_BAD_EMERG_TIMEOUT, "BAD_EMERG_TIMEOUT" },
507 { OFPFMFC_BAD_COMMAND, "BAD_COMMAND" },
508 { OFPFMFC_UNSUPPORTED, "UNSUPPORTED" },
509 { 0, NULL }
510 };
511
512 #define OFPPMFC_BAD_PORT 0x0000
513 #define OFPPMFC_BAD_HW_ADDR 0x0001
514 static const struct tok ofppmfc_str[] = {
515 { OFPPMFC_BAD_PORT, "BAD_PORT" },
516 { OFPPMFC_BAD_HW_ADDR, "BAD_HW_ADDR" },
517 { 0, NULL }
518 };
519
520 #define OFPQOFC_BAD_PORT 0x0000
521 #define OFPQOFC_BAD_QUEUE 0x0001
522 #define OFPQOFC_EPERM 0x0002
523 static const struct tok ofpqofc_str[] = {
524 { OFPQOFC_BAD_PORT, "BAD_PORT" },
525 { OFPQOFC_BAD_QUEUE, "BAD_QUEUE" },
526 { OFPQOFC_EPERM, "EPERM" },
527 { 0, NULL }
528 };
529
530 static const struct tok empty_str[] = {
531 { 0, NULL }
532 };
533
534 /* lengths (fixed or minimal) of particular protocol structures */
535 #define OF_SWITCH_CONFIG_LEN 12
536 #define OF_PHY_PORT_LEN 48
537 #define OF_SWITCH_FEATURES_LEN 32
538 #define OF_PORT_STATUS_LEN 64
539 #define OF_PORT_MOD_LEN 32
540 #define OF_PACKET_IN_LEN 20
541 #define OF_ACTION_OUTPUT_LEN 8
542 #define OF_ACTION_VLAN_VID_LEN 8
543 #define OF_ACTION_VLAN_PCP_LEN 8
544 #define OF_ACTION_DL_ADDR_LEN 16
545 #define OF_ACTION_NW_ADDR_LEN 8
546 #define OF_ACTION_TP_PORT_LEN 8
547 #define OF_ACTION_NW_TOS_LEN 8
548 #define OF_ACTION_VENDOR_HEADER_LEN 8
549 #define OF_ACTION_HEADER_LEN 8
550 #define OF_PACKET_OUT_LEN 16
551 #define OF_MATCH_LEN 40
552 #define OF_FLOW_MOD_LEN 72
553 #define OF_FLOW_REMOVED_LEN 88
554 #define OF_ERROR_MSG_LEN 12
555 #define OF_STATS_REQUEST_LEN 12
556 #define OF_STATS_REPLY_LEN 12
557 #define OF_DESC_STATS_LEN 1056
558 #define OF_FLOW_STATS_REQUEST_LEN 44
559 #define OF_FLOW_STATS_LEN 88
560 #define OF_AGGREGATE_STATS_REQUEST_LEN 44
561 #define OF_AGGREGATE_STATS_REPLY_LEN 24
562 #define OF_TABLE_STATS_LEN 64
563 #define OF_PORT_STATS_REQUEST_LEN 8
564 #define OF_PORT_STATS_LEN 104
565 #define OF_VENDOR_HEADER_LEN 12
566 #define OF_QUEUE_PROP_HEADER_LEN 8
567 #define OF_QUEUE_PROP_MIN_RATE_LEN 16
568 #define OF_PACKET_QUEUE_LEN 8
569 #define OF_QUEUE_GET_CONFIG_REQUEST_LEN 12
570 #define OF_QUEUE_GET_CONFIG_REPLY_LEN 16
571 #define OF_ACTION_ENQUEUE_LEN 16
572 #define OF_QUEUE_STATS_REQUEST_LEN 8
573 #define OF_QUEUE_STATS_LEN 32
574
575 /* miscellaneous constants from [OF10] */
576 #define OFP_MAX_TABLE_NAME_LEN 32
577 #define OFP_MAX_PORT_NAME_LEN 16
578 #define DESC_STR_LEN 256
579 #define SERIAL_NUM_LEN 32
580 #define OFP_VLAN_NONE 0xffff
581
582 static const char *
583 vlan_str(const uint16_t vid) {
584 static char buf[sizeof("65535 (bogus)")];
585 const char *fmt;
586
587 if (vid == OFP_VLAN_NONE)
588 return "NONE";
589 fmt = (vid > 0 && vid < 0x0fff) ? "%u" : "%u (bogus)";
590 snprintf(buf, sizeof(buf), fmt, vid);
591 return buf;
592 }
593
594 static const char *
595 pcp_str(const uint8_t pcp) {
596 static char buf[sizeof("255 (bogus)")];
597 snprintf(buf, sizeof(buf), pcp <= 7 ? "%u" : "%u (bogus)", pcp);
598 return buf;
599 }
600
601 static void
602 of10_bitmap_print(const struct tok *t, const uint32_t v, const uint32_t u) {
603 const char *sep = " (";
604
605 if (v == 0)
606 return;
607 /* assigned bits */
608 for (; t->s != NULL; t++)
609 if (v & t->v) {
610 printf("%s%s", sep, t->s);
611 sep = ", ";
612 }
613 /* unassigned bits? */
614 printf(v & u ? ") (bogus)" : ")");
615 }
616
617 static const u_char *
618 of10_data_print(const u_char *cp, const u_char *ep, const u_int len) {
619 if (len == 0)
620 return cp;
621 /* data */
622 printf("\n\t data (%u octets)", len);
623 TCHECK2(*cp, len);
624 if (vflag >= 2)
625 hex_and_ascii_print(gndo, "\n\t ", cp, len);
626 return cp + len;
627
628 trunc:
629 printf("%s", tstr);
630 return ep;
631 }
632
633 /* Vendor ID is mandatory, data is optional. */
634 static const u_char *
635 of10_vendor_data_print(const u_char *cp, const u_char *ep, const u_int len) {
636 if (len < 4)
637 goto corrupt;
638 /* vendor */
639 TCHECK2(*cp, 4);
640 printf(", vendor 0x%08x", EXTRACT_32BITS(cp));
641 cp += 4;
642 /* data */
643 return of10_data_print(cp, ep, len - 4);
644
645 corrupt: /* skip the undersized data */
646 printf("%s", cstr);
647 TCHECK2(*cp, len);
648 return cp + len;
649 trunc:
650 printf("%s", tstr);
651 return ep;
652 }
653
654 static const u_char *
655 of10_packet_data_print(const u_char *cp, const u_char *ep, const u_int len) {
656 if (len == 0)
657 return cp;
658 /* data */
659 printf("\n\t data (%u octets)", len);
660 if (vflag < 3)
661 return cp + len;
662 TCHECK2(*cp, len);
663 vflag -= 3;
664 printf(", frame decoding below\n");
665 ether_print(gndo, cp, len, snapend - cp, NULL, NULL);
666 vflag += 3;
667 return cp + len;
668
669 trunc:
670 printf("%s", tstr);
671 return ep;
672 }
673
674 /* [OF10] Section 5.2.1 */
675 static const u_char *
676 of10_phy_ports_print(const u_char *cp, const u_char *ep, u_int len) {
677 const u_char *cp0 = cp;
678 const u_int len0 = len;
679
680 while (len) {
681 if (len < OF_PHY_PORT_LEN)
682 goto corrupt;
683 /* port_no */
684 TCHECK2(*cp, 2);
685 printf("\n\t port_no %s", tok2str(ofpp_str, "%u", EXTRACT_16BITS(cp)));
686 cp += 2;
687 /* hw_addr */
688 TCHECK2(*cp, ETHER_ADDR_LEN);
689 printf(", hw_addr %s", etheraddr_string(cp));
690 cp += ETHER_ADDR_LEN;
691 /* name */
692 TCHECK2(*cp, OFP_MAX_PORT_NAME_LEN);
693 printf(", name '");
694 fn_print(cp, cp + OFP_MAX_PORT_NAME_LEN);
695 printf("'");
696 cp += OFP_MAX_PORT_NAME_LEN;
697
698 if (vflag < 2) {
699 TCHECK2(*cp, 24);
700 cp += 24;
701 goto next_port;
702 }
703 /* config */
704 TCHECK2(*cp, 4);
705 printf("\n\t config 0x%08x", EXTRACT_32BITS(cp));
706 of10_bitmap_print(ofppc_bm, EXTRACT_32BITS(cp), OFPPC_U);
707 cp += 4;
708 /* state */
709 TCHECK2(*cp, 4);
710 printf("\n\t state 0x%08x", EXTRACT_32BITS(cp));
711 of10_bitmap_print(ofpps_bm, EXTRACT_32BITS(cp), OFPPS_U);
712 cp += 4;
713 /* curr */
714 TCHECK2(*cp, 4);
715 printf("\n\t curr 0x%08x", EXTRACT_32BITS(cp));
716 of10_bitmap_print(ofppf_bm, EXTRACT_32BITS(cp), OFPPF_U);
717 cp += 4;
718 /* advertised */
719 TCHECK2(*cp, 4);
720 printf("\n\t advertised 0x%08x", EXTRACT_32BITS(cp));
721 of10_bitmap_print(ofppf_bm, EXTRACT_32BITS(cp), OFPPF_U);
722 cp += 4;
723 /* supported */
724 TCHECK2(*cp, 4);
725 printf("\n\t supported 0x%08x", EXTRACT_32BITS(cp));
726 of10_bitmap_print(ofppf_bm, EXTRACT_32BITS(cp), OFPPF_U);
727 cp += 4;
728 /* peer */
729 TCHECK2(*cp, 4);
730 printf("\n\t peer 0x%08x", EXTRACT_32BITS(cp));
731 of10_bitmap_print(ofppf_bm, EXTRACT_32BITS(cp), OFPPF_U);
732 cp += 4;
733 next_port:
734 len -= OF_PHY_PORT_LEN;
735 } /* while */
736 return cp;
737
738 corrupt: /* skip the undersized trailing data */
739 printf("%s", cstr);
740 TCHECK2(*cp0, len0);
741 return cp0 + len0;
742 trunc:
743 printf("%s", tstr);
744 return ep;
745 }
746
747 /* [OF10] Section 5.2.2 */
748 static const u_char *
749 of10_queue_props_print(const u_char *cp, const u_char *ep, u_int len) {
750 const u_char *cp0 = cp;
751 const u_int len0 = len;
752 uint16_t property, plen, rate;
753
754 while (len) {
755 u_char plen_bogus = 0, skip = 0;
756
757 if (len < OF_QUEUE_PROP_HEADER_LEN)
758 goto corrupt;
759 /* property */
760 TCHECK2(*cp, 2);
761 property = EXTRACT_16BITS(cp);
762 cp += 2;
763 printf("\n\t property %s", tok2str(ofpqt_str, "invalid (0x%04x)", property));
764 /* len */
765 TCHECK2(*cp, 2);
766 plen = EXTRACT_16BITS(cp);
767 cp += 2;
768 printf(", len %u", plen);
769 if (plen < OF_QUEUE_PROP_HEADER_LEN || plen > len)
770 goto corrupt;
771 /* pad */
772 TCHECK2(*cp, 4);
773 cp += 4;
774 /* property-specific constraints and decoding */
775 switch (property) {
776 case OFPQT_NONE:
777 plen_bogus = plen != OF_QUEUE_PROP_HEADER_LEN;
778 break;
779 case OFPQT_MIN_RATE:
780 plen_bogus = plen != OF_QUEUE_PROP_MIN_RATE_LEN;
781 break;
782 default:
783 skip = 1;
784 }
785 if (plen_bogus) {
786 printf(" (bogus)");
787 skip = 1;
788 }
789 if (skip) {
790 TCHECK2(*cp, plen - 4);
791 cp += plen - 4;
792 goto next_property;
793 }
794 if (property == OFPQT_MIN_RATE) { /* the only case of property decoding */
795 /* rate */
796 TCHECK2(*cp, 2);
797 rate = EXTRACT_16BITS(cp);
798 cp += 2;
799 if (rate > 1000)
800 printf(", rate disabled");
801 else
802 printf(", rate %u.%u%%", rate / 10, rate % 10);
803 /* pad */
804 TCHECK2(*cp, 6);
805 cp += 6;
806 }
807 next_property:
808 len -= plen;
809 } /* while */
810 return cp;
811
812 corrupt: /* skip the rest of queue properties */
813 printf("%s", cstr);
814 TCHECK2(*cp0, len0);
815 return cp0 + len0;
816 trunc:
817 printf("%s", tstr);
818 return ep;
819 }
820
821 /* ibid */
822 static const u_char *
823 of10_queues_print(const u_char *cp, const u_char *ep, u_int len) {
824 const u_char *cp0 = cp;
825 const u_int len0 = len;
826 uint16_t desclen;
827
828 while (len) {
829 if (len < OF_PACKET_QUEUE_LEN)
830 goto corrupt;
831 /* queue_id */
832 TCHECK2(*cp, 4);
833 printf("\n\t queue_id %u", EXTRACT_32BITS(cp));
834 cp += 4;
835 /* len */
836 TCHECK2(*cp, 2);
837 desclen = EXTRACT_16BITS(cp);
838 cp += 2;
839 printf(", len %u", desclen);
840 if (desclen < OF_PACKET_QUEUE_LEN || desclen > len)
841 goto corrupt;
842 /* pad */
843 TCHECK2(*cp, 2);
844 cp += 2;
845 /* properties */
846 if (vflag < 2) {
847 TCHECK2(*cp, desclen - OF_PACKET_QUEUE_LEN);
848 cp += desclen - OF_PACKET_QUEUE_LEN;
849 goto next_queue;
850 }
851 if (ep == (cp = of10_queue_props_print(cp, ep, desclen - OF_PACKET_QUEUE_LEN)))
852 return ep; /* end of snapshot */
853 next_queue:
854 len -= desclen;
855 } /* while */
856 return cp;
857
858 corrupt: /* skip the rest of queues */
859 printf("%s", cstr);
860 TCHECK2(*cp0, len0);
861 return cp0 + len0;
862 trunc:
863 printf("%s", tstr);
864 return ep;
865 }
866
867 /* [OF10] Section 5.2.3 */
868 static const u_char *
869 of10_match_print(const char *pfx, const u_char *cp, const u_char *ep) {
870 uint32_t wildcards;
871 uint16_t dl_type;
872 uint8_t nw_proto;
873 u_char nw_bits;
874 const char *field_name;
875
876 /* wildcards */
877 TCHECK2(*cp, 4);
878 wildcards = EXTRACT_32BITS(cp);
879 if (wildcards & OFPFW_U)
880 printf("%swildcards 0x%08x (bogus)", pfx, wildcards);
881 cp += 4;
882 /* in_port */
883 TCHECK2(*cp, 2);
884 if (! (wildcards & OFPFW_IN_PORT))
885 printf("%smatch in_port %s", pfx, tok2str(ofpp_str, "%u", EXTRACT_16BITS(cp)));
886 cp += 2;
887 /* dl_src */
888 TCHECK2(*cp, ETHER_ADDR_LEN);
889 if (! (wildcards & OFPFW_DL_SRC))
890 printf("%smatch dl_src %s", pfx, etheraddr_string(cp));
891 cp += ETHER_ADDR_LEN;
892 /* dl_dst */
893 TCHECK2(*cp, ETHER_ADDR_LEN);
894 if (! (wildcards & OFPFW_DL_DST))
895 printf("%smatch dl_dst %s", pfx, etheraddr_string(cp));
896 cp += ETHER_ADDR_LEN;
897 /* dl_vlan */
898 TCHECK2(*cp, 2);
899 if (! (wildcards & OFPFW_DL_VLAN))
900 printf("%smatch dl_vlan %s", pfx, vlan_str(EXTRACT_16BITS(cp)));
901 cp += 2;
902 /* dl_vlan_pcp */
903 TCHECK2(*cp, 1);
904 if (! (wildcards & OFPFW_DL_VLAN_PCP))
905 printf("%smatch dl_vlan_pcp %s", pfx, pcp_str(*cp));
906 cp += 1;
907 /* pad1 */
908 TCHECK2(*cp, 1);
909 cp += 1;
910 /* dl_type */
911 TCHECK2(*cp, 2);
912 dl_type = EXTRACT_16BITS(cp);
913 cp += 2;
914 if (! (wildcards & OFPFW_DL_TYPE))
915 printf("%smatch dl_type 0x%04x", pfx, dl_type);
916 /* nw_tos */
917 TCHECK2(*cp, 1);
918 if (! (wildcards & OFPFW_NW_TOS))
919 printf("%smatch nw_tos 0x%02x", pfx, *cp);
920 cp += 1;
921 /* nw_proto */
922 TCHECK2(*cp, 1);
923 nw_proto = *cp;
924 cp += 1;
925 if (! (wildcards & OFPFW_NW_PROTO)) {
926 field_name = ! (wildcards & OFPFW_DL_TYPE) && dl_type == ETHERTYPE_ARP
927 ? "arp_opcode" : "nw_proto";
928 printf("%smatch %s %u", pfx, field_name, nw_proto);
929 }
930 /* pad2 */
931 TCHECK2(*cp, 2);
932 cp += 2;
933 /* nw_src */
934 TCHECK2(*cp, 4);
935 nw_bits = (wildcards & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT;
936 if (nw_bits < 32)
937 printf("%smatch nw_src %s/%u", pfx, ipaddr_string(cp), 32 - nw_bits);
938 cp += 4;
939 /* nw_dst */
940 TCHECK2(*cp, 4);
941 nw_bits = (wildcards & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT;
942 if (nw_bits < 32)
943 printf("%smatch nw_dst %s/%u", pfx, ipaddr_string(cp), 32 - nw_bits);
944 cp += 4;
945 /* tp_src */
946 TCHECK2(*cp, 2);
947 if (! (wildcards & OFPFW_TP_SRC)) {
948 field_name = ! (wildcards & OFPFW_DL_TYPE) && dl_type == ETHERTYPE_IP
949 && ! (wildcards & OFPFW_NW_PROTO) && nw_proto == IPPROTO_ICMP
950 ? "icmp_type" : "tp_src";
951 printf("%smatch %s %u", pfx, field_name, EXTRACT_16BITS(cp));
952 }
953 cp += 2;
954 /* tp_dst */
955 TCHECK2(*cp, 2);
956 if (! (wildcards & OFPFW_TP_DST)) {
957 field_name = ! (wildcards & OFPFW_DL_TYPE) && dl_type == ETHERTYPE_IP
958 && ! (wildcards & OFPFW_NW_PROTO) && nw_proto == IPPROTO_ICMP
959 ? "icmp_code" : "tp_dst";
960 printf("%smatch %s %u", pfx, field_name, EXTRACT_16BITS(cp));
961 }
962 return cp + 2;
963
964 trunc:
965 printf("%s", tstr);
966 return ep;
967 }
968
969 /* [OF10] Section 5.2.4 */
970 static const u_char *
971 of10_actions_print(const char *pfx, const u_char *cp, const u_char *ep,
972 u_int len) {
973 const u_char *cp0 = cp;
974 const u_int len0 = len;
975 uint16_t type, alen, output_port;
976
977 while (len) {
978 u_char alen_bogus = 0, skip = 0;
979
980 if (len < OF_ACTION_HEADER_LEN)
981 goto corrupt;
982 /* type */
983 TCHECK2(*cp, 2);
984 type = EXTRACT_16BITS(cp);
985 cp += 2;
986 printf("%saction type %s", pfx, tok2str(ofpat_str, "invalid (0x%04x)", type));
987 /* length */
988 TCHECK2(*cp, 2);
989 alen = EXTRACT_16BITS(cp);
990 cp += 2;
991 printf(", len %u", alen);
992 /* On action size underrun/overrun skip the rest of the action list. */
993 if (alen < OF_ACTION_HEADER_LEN || alen > len)
994 goto corrupt;
995 /* On action size inappropriate for the given type or invalid type just skip
996 * the current action, as the basic length constraint has been met. */
997 switch (type) {
998 case OFPAT_OUTPUT:
999 case OFPAT_SET_VLAN_VID:
1000 case OFPAT_SET_VLAN_PCP:
1001 case OFPAT_STRIP_VLAN:
1002 case OFPAT_SET_NW_SRC:
1003 case OFPAT_SET_NW_DST:
1004 case OFPAT_SET_NW_TOS:
1005 case OFPAT_SET_TP_SRC:
1006 case OFPAT_SET_TP_DST:
1007 alen_bogus = alen != 8;
1008 break;
1009 case OFPAT_SET_DL_SRC:
1010 case OFPAT_SET_DL_DST:
1011 case OFPAT_ENQUEUE:
1012 alen_bogus = alen != 16;
1013 break;
1014 case OFPAT_VENDOR:
1015 alen_bogus = alen % 8 != 0; /* already >= 8 so far */
1016 break;
1017 default:
1018 skip = 1;
1019 }
1020 if (alen_bogus) {
1021 printf(" (bogus)");
1022 skip = 1;
1023 }
1024 if (skip) {
1025 TCHECK2(*cp, alen - 4);
1026 cp += alen - 4;
1027 goto next_action;
1028 }
1029 /* OK to decode the rest of the action structure */
1030 switch (type) {
1031 case OFPAT_OUTPUT:
1032 /* port */
1033 TCHECK2(*cp, 2);
1034 output_port = EXTRACT_16BITS(cp);
1035 cp += 2;
1036 printf(", port %s", tok2str(ofpp_str, "%u", output_port));
1037 /* max_len */
1038 TCHECK2(*cp, 2);
1039 if (output_port == OFPP_CONTROLLER)
1040 printf(", max_len %u", EXTRACT_16BITS(cp));
1041 cp += 2;
1042 break;
1043 case OFPAT_SET_VLAN_VID:
1044 /* vlan_vid */
1045 TCHECK2(*cp, 2);
1046 printf(", vlan_vid %s", vlan_str(EXTRACT_16BITS(cp)));
1047 cp += 2;
1048 /* pad */
1049 TCHECK2(*cp, 2);
1050 cp += 2;
1051 break;
1052 case OFPAT_SET_VLAN_PCP:
1053 /* vlan_pcp */
1054 TCHECK2(*cp, 1);
1055 printf(", vlan_pcp %s", pcp_str(*cp));
1056 cp += 1;
1057 /* pad */
1058 TCHECK2(*cp, 3);
1059 cp += 3;
1060 break;
1061 case OFPAT_SET_DL_SRC:
1062 case OFPAT_SET_DL_DST:
1063 /* dl_addr */
1064 TCHECK2(*cp, ETHER_ADDR_LEN);
1065 printf(", dl_addr %s", etheraddr_string(cp));
1066 cp += ETHER_ADDR_LEN;
1067 /* pad */
1068 TCHECK2(*cp, 6);
1069 cp += 6;
1070 break;
1071 case OFPAT_SET_NW_SRC:
1072 case OFPAT_SET_NW_DST:
1073 /* nw_addr */
1074 TCHECK2(*cp, 4);
1075 printf(", nw_addr %s", ipaddr_string(cp));
1076 cp += 4;
1077 break;
1078 case OFPAT_SET_NW_TOS:
1079 /* nw_tos */
1080 TCHECK2(*cp, 1);
1081 printf(", nw_tos 0x%02x", *cp);
1082 cp += 1;
1083 /* pad */
1084 TCHECK2(*cp, 3);
1085 cp += 3;
1086 break;
1087 case OFPAT_SET_TP_SRC:
1088 case OFPAT_SET_TP_DST:
1089 /* nw_tos */
1090 TCHECK2(*cp, 2);
1091 printf(", tp_port %u", EXTRACT_16BITS(cp));
1092 cp += 2;
1093 /* pad */
1094 TCHECK2(*cp, 2);
1095 cp += 2;
1096 break;
1097 case OFPAT_ENQUEUE:
1098 /* port */
1099 TCHECK2(*cp, 2);
1100 printf(", port %s", tok2str(ofpp_str, "%u", EXTRACT_16BITS(cp)));
1101 cp += 2;
1102 /* pad */
1103 TCHECK2(*cp, 6);
1104 cp += 6;
1105 /* queue_id */
1106 TCHECK2(*cp, 4);
1107 printf(", queue_id %s", tok2str(ofpq_str, "%u", EXTRACT_32BITS(cp)));
1108 cp += 4;
1109 break;
1110 case OFPAT_VENDOR:
1111 if (ep == (cp = of10_vendor_data_print(cp, ep, alen - 4)))
1112 return ep; /* end of snapshot */
1113 break;
1114 case OFPAT_STRIP_VLAN:
1115 /* pad */
1116 TCHECK2(*cp, 4);
1117 cp += 4;
1118 break;
1119 } /* switch */
1120 next_action:
1121 len -= alen;
1122 } /* while */
1123 return cp;
1124
1125 corrupt: /* skip the rest of actions */
1126 printf("%s", cstr);
1127 TCHECK2(*cp0, len0);
1128 return cp0 + len0;
1129 trunc:
1130 printf("%s", tstr);
1131 return ep;
1132 }
1133
1134 /* [OF10] Section 5.3.1 */
1135 static const u_char *
1136 of10_features_reply_print(const u_char *cp, const u_char *ep, const u_int len) {
1137 /* datapath_id */
1138 TCHECK2(*cp, 8);
1139 printf("\n\t dpid 0x%016" PRIx64, EXTRACT_64BITS(cp));
1140 cp += 8;
1141 /* n_buffers */
1142 TCHECK2(*cp, 4);
1143 printf(", n_buffers %u", EXTRACT_32BITS(cp));
1144 cp += 4;
1145 /* n_tables */
1146 TCHECK2(*cp, 1);
1147 printf(", n_tables %u", *cp);
1148 cp += 1;
1149 /* pad */
1150 TCHECK2(*cp, 3);
1151 cp += 3;
1152 /* capabilities */
1153 TCHECK2(*cp, 4);
1154 printf("\n\t capabilities 0x%08x", EXTRACT_32BITS(cp));
1155 of10_bitmap_print(ofp_capabilities_bm, EXTRACT_32BITS(cp), OFPCAP_U);
1156 cp += 4;
1157 /* actions */
1158 TCHECK2(*cp, 4);
1159 printf("\n\t actions 0x%08x", EXTRACT_32BITS(cp));
1160 of10_bitmap_print(ofpat_bm, EXTRACT_32BITS(cp), OFPAT_U);
1161 cp += 4;
1162 /* ports */
1163 return of10_phy_ports_print(cp, ep, len - OF_SWITCH_FEATURES_LEN);
1164
1165 trunc:
1166 printf("%s", tstr);
1167 return ep;
1168 }
1169
1170 /* [OF10] Section 5.3.3 */
1171 static const u_char *
1172 of10_flow_mod_print(const u_char *cp, const u_char *ep, const u_int len) {
1173 uint16_t command;
1174
1175 /* match */
1176 if (ep == (cp = of10_match_print("\n\t ", cp, ep)))
1177 return ep; /* end of snapshot */
1178 /* cookie */
1179 TCHECK2(*cp, 8);
1180 printf("\n\t cookie 0x%016" PRIx64, EXTRACT_64BITS(cp));
1181 cp += 8;
1182 /* command */
1183 TCHECK2(*cp, 2);
1184 command = EXTRACT_16BITS(cp);
1185 printf(", command %s", tok2str(ofpfc_str, "invalid (0x%04x)", command));
1186 cp += 2;
1187 /* idle_timeout */
1188 TCHECK2(*cp, 2);
1189 if (EXTRACT_16BITS(cp))
1190 printf(", idle_timeout %u", EXTRACT_16BITS(cp));
1191 cp += 2;
1192 /* hard_timeout */
1193 TCHECK2(*cp, 2);
1194 if (EXTRACT_16BITS(cp))
1195 printf(", hard_timeout %u", EXTRACT_16BITS(cp));
1196 cp += 2;
1197 /* priority */
1198 TCHECK2(*cp, 2);
1199 if (EXTRACT_16BITS(cp))
1200 printf(", priority %u", EXTRACT_16BITS(cp));
1201 cp += 2;
1202 /* buffer_id */
1203 TCHECK2(*cp, 4);
1204 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
1205 command == OFPFC_MODIFY_STRICT)
1206 printf(", buffer_id %s", tok2str(bufferid_str, "0x%08x", EXTRACT_32BITS(cp)));
1207 cp += 4;
1208 /* out_port */
1209 TCHECK2(*cp, 2);
1210 if (command == OFPFC_DELETE || command == OFPFC_DELETE_STRICT)
1211 printf(", out_port %s", tok2str(ofpp_str, "%u", EXTRACT_16BITS(cp)));
1212 cp += 2;
1213 /* flags */
1214 TCHECK2(*cp, 2);
1215 printf(", flags 0x%04x", EXTRACT_16BITS(cp));
1216 of10_bitmap_print(ofpff_bm, EXTRACT_16BITS(cp), OFPFF_U);
1217 cp += 2;
1218 /* actions */
1219 return of10_actions_print("\n\t ", cp, ep, len - OF_FLOW_MOD_LEN);
1220
1221 trunc:
1222 printf("%s", tstr);
1223 return ep;
1224 }
1225
1226 /* ibid */
1227 static const u_char *
1228 of10_port_mod_print(const u_char *cp, const u_char *ep) {
1229 /* port_no */
1230 TCHECK2(*cp, 2);
1231 printf("\n\t port_no %s", tok2str(ofpp_str, "%u", EXTRACT_16BITS(cp)));
1232 cp += 2;
1233 /* hw_addr */
1234 TCHECK2(*cp, ETHER_ADDR_LEN);
1235 printf(", hw_addr %s", etheraddr_string(cp));
1236 cp += ETHER_ADDR_LEN;
1237 /* config */
1238 TCHECK2(*cp, 4);
1239 printf("\n\t config 0x%08x", EXTRACT_32BITS(cp));
1240 of10_bitmap_print(ofppc_bm, EXTRACT_32BITS(cp), OFPPC_U);
1241 cp += 4;
1242 /* mask */
1243 TCHECK2(*cp, 4);
1244 printf("\n\t mask 0x%08x", EXTRACT_32BITS(cp));
1245 of10_bitmap_print(ofppc_bm, EXTRACT_32BITS(cp), OFPPC_U);
1246 cp += 4;
1247 /* advertise */
1248 TCHECK2(*cp, 4);
1249 printf("\n\t advertise 0x%08x", EXTRACT_32BITS(cp));
1250 of10_bitmap_print(ofppf_bm, EXTRACT_32BITS(cp), OFPPF_U);
1251 cp += 4;
1252 /* pad */
1253 TCHECK2(*cp, 4);
1254 return cp + 4;
1255
1256 trunc:
1257 printf("%s", tstr);
1258 return ep;
1259 }
1260
1261 /* [OF10] Section 5.3.5 */
1262 static const u_char *
1263 of10_stats_request_print(const u_char *cp, const u_char *ep, u_int len) {
1264 const u_char *cp0 = cp;
1265 const u_int len0 = len;
1266 uint16_t type;
1267
1268 /* type */
1269 TCHECK2(*cp, 2);
1270 type = EXTRACT_16BITS(cp);
1271 cp += 2;
1272 printf("\n\t type %s", tok2str(ofpst_str, "invalid (0x%04x)", type));
1273 /* flags */
1274 TCHECK2(*cp, 2);
1275 printf(", flags 0x%04x", EXTRACT_16BITS(cp));
1276 if (EXTRACT_16BITS(cp))
1277 printf(" (bogus)");
1278 cp += 2;
1279 /* type-specific body of one of fixed lengths */
1280 len -= OF_STATS_REQUEST_LEN;
1281 switch(type) {
1282 case OFPST_DESC:
1283 case OFPST_TABLE:
1284 if (len)
1285 goto corrupt;
1286 return cp;
1287 case OFPST_FLOW:
1288 case OFPST_AGGREGATE:
1289 if (len != OF_FLOW_STATS_REQUEST_LEN)
1290 goto corrupt;
1291 /* match */
1292 if (ep == (cp = of10_match_print("\n\t ", cp, ep)))
1293 return ep; /* end of snapshot */
1294 /* table_id */
1295 TCHECK2(*cp, 1);
1296 printf("\n\t table_id %s", tok2str(tableid_str, "%u", *cp));
1297 cp += 1;
1298 /* pad */
1299 TCHECK2(*cp, 1);
1300 cp += 1;
1301 /* out_port */
1302 TCHECK2(*cp, 2);
1303 printf(", out_port %s", tok2str(ofpp_str, "%u", EXTRACT_16BITS(cp)));
1304 return cp + 2;
1305 case OFPST_PORT:
1306 if (len != OF_PORT_STATS_REQUEST_LEN)
1307 goto corrupt;
1308 /* port_no */
1309 TCHECK2(*cp, 2);
1310 printf("\n\t port_no %s", tok2str(ofpp_str, "%u", EXTRACT_16BITS(cp)));
1311 cp += 2;
1312 /* pad */
1313 TCHECK2(*cp, 6);
1314 return cp + 6;
1315 case OFPST_QUEUE:
1316 if (len != OF_QUEUE_STATS_REQUEST_LEN)
1317 goto corrupt;
1318 /* port_no */
1319 TCHECK2(*cp, 2);
1320 printf("\n\t port_no %s", tok2str(ofpp_str, "%u", EXTRACT_16BITS(cp)));
1321 cp += 2;
1322 /* pad */
1323 TCHECK2(*cp, 2);
1324 cp += 2;
1325 /* queue_id */
1326 TCHECK2(*cp, 4);
1327 printf(", queue_id %s", tok2str(ofpq_str, "%u", EXTRACT_32BITS(cp)));
1328 return cp + 4;
1329 case OFPST_VENDOR:
1330 return of10_vendor_data_print(cp, ep, len);
1331 }
1332 return cp;
1333
1334 corrupt: /* skip the message body */
1335 printf("%s", cstr);
1336 TCHECK2(*cp0, len0);
1337 return cp0 + len0;
1338 trunc:
1339 printf("%s", tstr);
1340 return ep;
1341 }
1342
1343 /* ibid */
1344 static const u_char *
1345 of10_desc_stats_reply_print(const u_char *cp, const u_char *ep, const u_int len) {
1346 if (len != OF_DESC_STATS_LEN)
1347 goto corrupt;
1348 /* mfr_desc */
1349 TCHECK2(*cp, DESC_STR_LEN);
1350 printf("\n\t mfr_desc '");
1351 fn_print(cp, cp + DESC_STR_LEN);
1352 printf("'");
1353 cp += DESC_STR_LEN;
1354 /* hw_desc */
1355 TCHECK2(*cp, DESC_STR_LEN);
1356 printf("\n\t hw_desc '");
1357 fn_print(cp, cp + DESC_STR_LEN);
1358 printf("'");
1359 cp += DESC_STR_LEN;
1360 /* sw_desc */
1361 TCHECK2(*cp, DESC_STR_LEN);
1362 printf("\n\t sw_desc '");
1363 fn_print(cp, cp + DESC_STR_LEN);
1364 printf("'");
1365 cp += DESC_STR_LEN;
1366 /* serial_num */
1367 TCHECK2(*cp, SERIAL_NUM_LEN);
1368 printf("\n\t serial_num '");
1369 fn_print(cp, cp + SERIAL_NUM_LEN);
1370 printf("'");
1371 cp += SERIAL_NUM_LEN;
1372 /* dp_desc */
1373 TCHECK2(*cp, DESC_STR_LEN);
1374 printf("\n\t dp_desc '");
1375 fn_print(cp, cp + DESC_STR_LEN);
1376 printf("'");
1377 return cp + DESC_STR_LEN;
1378
1379 corrupt: /* skip the message body */
1380 printf("%s", cstr);
1381 TCHECK2(*cp, len);
1382 return cp + len;
1383 trunc:
1384 printf("%s", tstr);
1385 return ep;
1386 }
1387
1388 /* ibid */
1389 static const u_char *
1390 of10_flow_stats_reply_print(const u_char *cp, const u_char *ep, u_int len) {
1391 const u_char *cp0 = cp;
1392 const u_int len0 = len;
1393 uint16_t entry_len;
1394
1395 while (len) {
1396 if (len < OF_FLOW_STATS_LEN)
1397 goto corrupt;
1398 /* length */
1399 TCHECK2(*cp, 2);
1400 entry_len = EXTRACT_16BITS(cp);
1401 printf("\n\t length %u", entry_len);
1402 if (entry_len < OF_FLOW_STATS_LEN || entry_len > len)
1403 goto corrupt;
1404 cp += 2;
1405 /* table_id */
1406 TCHECK2(*cp, 1);
1407 printf(", table_id %s", tok2str(tableid_str, "%u", *cp));
1408 cp += 1;
1409 /* pad */
1410 TCHECK2(*cp, 1);
1411 cp += 1;
1412 /* match */
1413 if (ep == (cp = of10_match_print("\n\t ", cp, ep)))
1414 return ep; /* end of snapshot */
1415 /* duration_sec */
1416 TCHECK2(*cp, 4);
1417 printf("\n\t duration_sec %u", EXTRACT_32BITS(cp));
1418 cp += 4;
1419 /* duration_nsec */
1420 TCHECK2(*cp, 4);
1421 printf(", duration_nsec %u", EXTRACT_32BITS(cp));
1422 cp += 4;
1423 /* priority */
1424 TCHECK2(*cp, 2);
1425 printf(", priority %u", EXTRACT_16BITS(cp));
1426 cp += 2;
1427 /* idle_timeout */
1428 TCHECK2(*cp, 2);
1429 printf(", idle_timeout %u", EXTRACT_16BITS(cp));
1430 cp += 2;
1431 /* hard_timeout */
1432 TCHECK2(*cp, 2);
1433 printf(", hard_timeout %u", EXTRACT_16BITS(cp));
1434 cp += 2;
1435 /* pad2 */
1436 TCHECK2(*cp, 6);
1437 cp += 6;
1438 /* cookie */
1439 TCHECK2(*cp, 8);
1440 printf(", cookie 0x%016" PRIx64, EXTRACT_64BITS(cp));
1441 cp += 8;
1442 /* packet_count */
1443 TCHECK2(*cp, 8);
1444 printf(", packet_count %" PRIu64, EXTRACT_64BITS(cp));
1445 cp += 8;
1446 /* byte_count */
1447 TCHECK2(*cp, 8);
1448 printf(", byte_count %" PRIu64, EXTRACT_64BITS(cp));
1449 cp += 8;
1450 /* actions */
1451 if (ep == (cp = of10_actions_print("\n\t ", cp, ep, entry_len - OF_FLOW_STATS_LEN)))
1452 return ep; /* end of snapshot */
1453
1454 len -= entry_len;
1455 } /* while */
1456 return cp;
1457
1458 corrupt: /* skip the rest of flow statistics entries */
1459 printf("%s", cstr);
1460 TCHECK2(*cp0, len0);
1461 return cp0 + len0;
1462 trunc:
1463 printf("%s", tstr);
1464 return ep;
1465 }
1466
1467 /* ibid */
1468 static const u_char *
1469 of10_aggregate_stats_reply_print(const u_char *cp, const u_char *ep,
1470 const u_int len) {
1471 if (len != OF_AGGREGATE_STATS_REPLY_LEN)
1472 goto corrupt;
1473 /* packet_count */
1474 TCHECK2(*cp, 8);
1475 printf("\n\t packet_count %" PRIu64, EXTRACT_64BITS(cp));
1476 cp += 8;
1477 /* byte_count */
1478 TCHECK2(*cp, 8);
1479 printf(", byte_count %" PRIu64, EXTRACT_64BITS(cp));
1480 cp += 8;
1481 /* flow_count */
1482 TCHECK2(*cp, 4);
1483 printf(", flow_count %u", EXTRACT_32BITS(cp));
1484 cp += 4;
1485 /* pad */
1486 TCHECK2(*cp, 4);
1487 return cp + 4;
1488
1489 corrupt: /* skip the message body */
1490 printf("%s", cstr);
1491 TCHECK2(*cp, len);
1492 return cp + len;
1493 trunc:
1494 printf("%s", tstr);
1495 return ep;
1496 }
1497
1498 /* ibid */
1499 static const u_char *
1500 of10_table_stats_reply_print(const u_char *cp, const u_char *ep, u_int len) {
1501 const u_char *cp0 = cp;
1502 const u_int len0 = len;
1503
1504 while (len) {
1505 if (len < OF_TABLE_STATS_LEN)
1506 goto corrupt;
1507 /* table_id */
1508 TCHECK2(*cp, 1);
1509 printf("\n\t table_id %s", tok2str(tableid_str, "%u", *cp));
1510 cp += 1;
1511 /* pad */
1512 TCHECK2(*cp, 3);
1513 cp += 3;
1514 /* name */
1515 TCHECK2(*cp, OFP_MAX_TABLE_NAME_LEN);
1516 printf(", name '");
1517 fn_print(cp, cp + OFP_MAX_TABLE_NAME_LEN);
1518 printf("'");
1519 cp += OFP_MAX_TABLE_NAME_LEN;
1520 /* wildcards */
1521 TCHECK2(*cp, 4);
1522 printf("\n\t wildcards 0x%08x", EXTRACT_32BITS(cp));
1523 of10_bitmap_print(ofpfw_bm, EXTRACT_32BITS(cp), OFPFW_U);
1524 cp += 4;
1525 /* max_entries */
1526 TCHECK2(*cp, 4);
1527 printf("\n\t max_entries %u", EXTRACT_32BITS(cp));
1528 cp += 4;
1529 /* active_count */
1530 TCHECK2(*cp, 4);
1531 printf(", active_count %u", EXTRACT_32BITS(cp));
1532 cp += 4;
1533 /* lookup_count */
1534 TCHECK2(*cp, 8);
1535 printf(", lookup_count %" PRIu64, EXTRACT_64BITS(cp));
1536 cp += 8;
1537 /* matched_count */
1538 TCHECK2(*cp, 8);
1539 printf(", matched_count %" PRIu64, EXTRACT_64BITS(cp));
1540 cp += 8;
1541
1542 len -= OF_TABLE_STATS_LEN;
1543 } /* while */
1544 return cp;
1545
1546 corrupt: /* skip the undersized trailing data */
1547 printf("%s", cstr);
1548 TCHECK2(*cp0, len0);
1549 return cp0 + len0;
1550 trunc:
1551 printf("%s", tstr);
1552 return ep;
1553 }
1554
1555 /* ibid */
1556 static const u_char *
1557 of10_port_stats_reply_print(const u_char *cp, const u_char *ep, u_int len) {
1558 const u_char *cp0 = cp;
1559 const u_int len0 = len;
1560
1561 while (len) {
1562 if (len < OF_PORT_STATS_LEN)
1563 goto corrupt;
1564 /* port_no */
1565 TCHECK2(*cp, 2);
1566 printf("\n\t port_no %s", tok2str(ofpp_str, "%u", EXTRACT_16BITS(cp)));
1567 cp += 2;
1568 if (vflag < 2) {
1569 TCHECK2(*cp, OF_PORT_STATS_LEN - 2);
1570 cp += OF_PORT_STATS_LEN - 2;
1571 goto next_port;
1572 }
1573 /* pad */
1574 TCHECK2(*cp, 6);
1575 cp += 6;
1576 /* rx_packets */
1577 TCHECK2(*cp, 8);
1578 printf(", rx_packets %" PRIu64, EXTRACT_64BITS(cp));
1579 cp += 8;
1580 /* tx_packets */
1581 TCHECK2(*cp, 8);
1582 printf(", tx_packets %" PRIu64, EXTRACT_64BITS(cp));
1583 cp += 8;
1584 /* rx_bytes */
1585 TCHECK2(*cp, 8);
1586 printf(", rx_bytes %" PRIu64, EXTRACT_64BITS(cp));
1587 cp += 8;
1588 /* tx_bytes */
1589 TCHECK2(*cp, 8);
1590 printf(", tx_bytes %" PRIu64, EXTRACT_64BITS(cp));
1591 cp += 8;
1592 /* rx_dropped */
1593 TCHECK2(*cp, 8);
1594 printf(", rx_dropped %" PRIu64, EXTRACT_64BITS(cp));
1595 cp += 8;
1596 /* tx_dropped */
1597 TCHECK2(*cp, 8);
1598 printf(", tx_dropped %" PRIu64, EXTRACT_64BITS(cp));
1599 cp += 8;
1600 /* rx_errors */
1601 TCHECK2(*cp, 8);
1602 printf(", rx_errors %" PRIu64, EXTRACT_64BITS(cp));
1603 cp += 8;
1604 /* tx_errors */
1605 TCHECK2(*cp, 8);
1606 printf(", tx_errors %" PRIu64, EXTRACT_64BITS(cp));
1607 cp += 8;
1608 /* rx_frame_err */
1609 TCHECK2(*cp, 8);
1610 printf(", rx_frame_err %" PRIu64, EXTRACT_64BITS(cp));
1611 cp += 8;
1612 /* rx_over_err */
1613 TCHECK2(*cp, 8);
1614 printf(", rx_over_err %" PRIu64, EXTRACT_64BITS(cp));
1615 cp += 8;
1616 /* rx_crc_err */
1617 TCHECK2(*cp, 8);
1618 printf(", rx_crc_err %" PRIu64, EXTRACT_64BITS(cp));
1619 cp += 8;
1620 /* collisions */
1621 TCHECK2(*cp, 8);
1622 printf(", collisions %" PRIu64, EXTRACT_64BITS(cp));
1623 cp += 8;
1624 next_port:
1625 len -= OF_PORT_STATS_LEN;
1626 } /* while */
1627 return cp;
1628
1629 corrupt: /* skip the undersized trailing data */
1630 printf("%s", cstr);
1631 TCHECK2(*cp0, len0);
1632 return cp0 + len0;
1633 trunc:
1634 printf("%s", tstr);
1635 return ep;
1636 }
1637
1638 /* ibid */
1639 static const u_char *
1640 of10_queue_stats_reply_print(const u_char *cp, const u_char *ep, u_int len) {
1641 const u_char *cp0 = cp;
1642 const u_int len0 = len;
1643
1644 while (len) {
1645 if (len < OF_QUEUE_STATS_LEN)
1646 goto corrupt;
1647 /* port_no */
1648 TCHECK2(*cp, 2);
1649 printf("\n\t port_no %s", tok2str(ofpp_str, "%u", EXTRACT_16BITS(cp)));
1650 cp += 2;
1651 /* pad */
1652 TCHECK2(*cp, 2);
1653 cp += 2;
1654 /* queue_id */
1655 TCHECK2(*cp, 4);
1656 printf(", queue_id %u", EXTRACT_32BITS(cp));
1657 cp += 4;
1658 /* tx_bytes */
1659 TCHECK2(*cp, 8);
1660 printf(", tx_bytes %" PRIu64, EXTRACT_64BITS(cp));
1661 cp += 8;
1662 /* tx_packets */
1663 TCHECK2(*cp, 8);
1664 printf(", tx_packets %" PRIu64, EXTRACT_64BITS(cp));
1665 cp += 8;
1666 /* tx_errors */
1667 TCHECK2(*cp, 8);
1668 printf(", tx_errors %" PRIu64, EXTRACT_64BITS(cp));
1669 cp += 8;
1670
1671 len -= OF_QUEUE_STATS_LEN;
1672 } /* while */
1673 return cp;
1674
1675 corrupt: /* skip the undersized trailing data */
1676 printf("%s", cstr);
1677 TCHECK2(*cp0, len0);
1678 return cp0 + len0;
1679 trunc:
1680 printf("%s", tstr);
1681 return ep;
1682 }
1683
1684 /* ibid */
1685 static const u_char *
1686 of10_stats_reply_print(const u_char *cp, const u_char *ep, const u_int len) {
1687 const u_char *cp0 = cp;
1688 uint16_t type;
1689
1690 /* type */
1691 TCHECK2(*cp, 2);
1692 type = EXTRACT_16BITS(cp);
1693 printf("\n\t type %s", tok2str(ofpst_str, "invalid (0x%04x)", type));
1694 cp += 2;
1695 /* flags */
1696 TCHECK2(*cp, 2);
1697 printf(", flags 0x%04x", EXTRACT_16BITS(cp));
1698 of10_bitmap_print(ofpsf_reply_bm, EXTRACT_16BITS(cp), OFPSF_REPLY_U);
1699 cp += 2;
1700
1701 if (vflag > 0) {
1702 const u_char *(*decoder)(const u_char *, const u_char *, u_int) =
1703 type == OFPST_DESC ? of10_desc_stats_reply_print :
1704 type == OFPST_FLOW ? of10_flow_stats_reply_print :
1705 type == OFPST_AGGREGATE ? of10_aggregate_stats_reply_print :
1706 type == OFPST_TABLE ? of10_table_stats_reply_print :
1707 type == OFPST_PORT ? of10_port_stats_reply_print :
1708 type == OFPST_QUEUE ? of10_queue_stats_reply_print :
1709 type == OFPST_VENDOR ? of10_vendor_data_print :
1710 NULL;
1711 if (decoder != NULL)
1712 return decoder(cp, ep, len - OF_STATS_REPLY_LEN);
1713 }
1714 TCHECK2(*cp0, len);
1715 return cp0 + len;
1716
1717 trunc:
1718 printf("%s", tstr);
1719 return ep;
1720 }
1721
1722 /* [OF10] Section 5.3.6 */
1723 static const u_char *
1724 of10_packet_out_print(const u_char *cp, const u_char *ep, const u_int len) {
1725 const u_char *cp0 = cp;
1726 const u_int len0 = len;
1727 uint16_t actions_len;
1728
1729 /* buffer_id */
1730 TCHECK2(*cp, 4);
1731 printf("\n\t buffer_id 0x%08x", EXTRACT_32BITS(cp));
1732 cp += 4;
1733 /* in_port */
1734 TCHECK2(*cp, 2);
1735 printf(", in_port %s", tok2str(ofpp_str, "%u", EXTRACT_16BITS(cp)));
1736 cp += 2;
1737 /* actions_len */
1738 TCHECK2(*cp, 2);
1739 actions_len = EXTRACT_16BITS(cp);
1740 cp += 2;
1741 if (actions_len > len - OF_PACKET_OUT_LEN)
1742 goto corrupt;
1743 /* actions */
1744 if (ep == (cp = of10_actions_print("\n\t ", cp, ep, actions_len)))
1745 return ep; /* end of snapshot */
1746 /* data */
1747 return of10_packet_data_print(cp, ep, len - OF_PACKET_OUT_LEN - actions_len);
1748
1749 corrupt: /* skip the rest of the message body */
1750 printf("%s", cstr);
1751 TCHECK2(*cp0, len0);
1752 return cp0 + len0;
1753 trunc:
1754 printf("%s", tstr);
1755 return ep;
1756 }
1757
1758 /* [OF10] Section 5.4.1 */
1759 static const u_char *
1760 of10_packet_in_print(const u_char *cp, const u_char *ep, const u_int len) {
1761 /* buffer_id */
1762 TCHECK2(*cp, 4);
1763 printf("\n\t buffer_id %s", tok2str(bufferid_str, "0x%08x", EXTRACT_32BITS(cp)));
1764 cp += 4;
1765 /* total_len */
1766 TCHECK2(*cp, 2);
1767 printf(", total_len %u", EXTRACT_16BITS(cp));
1768 cp += 2;
1769 /* in_port */
1770 TCHECK2(*cp, 2);
1771 printf(", in_port %s", tok2str(ofpp_str, "%u", EXTRACT_16BITS(cp)));
1772 cp += 2;
1773 /* reason */
1774 TCHECK2(*cp, 1);
1775 printf(", reason %s", tok2str(ofpr_str, "invalid (0x%02x)", *cp));
1776 cp += 1;
1777 /* pad */
1778 TCHECK2(*cp, 1);
1779 cp += 1;
1780 /* data */
1781 /* 2 mock octets count in OF_PACKET_IN_LEN but not in len */
1782 return of10_packet_data_print(cp, ep, len - (OF_PACKET_IN_LEN - 2));
1783
1784 trunc:
1785 printf("%s", tstr);
1786 return ep;
1787 }
1788
1789 /* [OF10] Section 5.4.2 */
1790 static const u_char *
1791 of10_flow_removed_print(const u_char *cp, const u_char *ep) {
1792 /* match */
1793 if (ep == (cp = of10_match_print("\n\t ", cp, ep)))
1794 return ep; /* end of snapshot */
1795 /* cookie */
1796 TCHECK2(*cp, 8);
1797 printf("\n\t cookie 0x%016" PRIx64, EXTRACT_64BITS(cp));
1798 cp += 8;
1799 /* priority */
1800 TCHECK2(*cp, 2);
1801 if (EXTRACT_16BITS(cp))
1802 printf(", priority %u", EXTRACT_16BITS(cp));
1803 cp += 2;
1804 /* reason */
1805 TCHECK2(*cp, 1);
1806 printf(", reason %s", tok2str(ofprr_str, "unknown (0x%02x)", *cp));
1807 cp += 1;
1808 /* pad */
1809 TCHECK2(*cp, 1);
1810 cp += 1;
1811 /* duration_sec */
1812 TCHECK2(*cp, 4);
1813 printf(", duration_sec %u", EXTRACT_32BITS(cp));
1814 cp += 4;
1815 /* duration_nsec */
1816 TCHECK2(*cp, 4);
1817 printf(", duration_nsec %u", EXTRACT_32BITS(cp));
1818 cp += 4;
1819 /* idle_timeout */
1820 TCHECK2(*cp, 2);
1821 if (EXTRACT_16BITS(cp))
1822 printf(", idle_timeout %u", EXTRACT_16BITS(cp));
1823 cp += 2;
1824 /* pad2 */
1825 TCHECK2(*cp, 2);
1826 cp += 2;
1827 /* packet_count */
1828 TCHECK2(*cp, 8);
1829 printf(", packet_count %" PRIu64, EXTRACT_64BITS(cp));
1830 cp += 8;
1831 /* byte_count */
1832 TCHECK2(*cp, 8);
1833 printf(", byte_count %" PRIu64, EXTRACT_64BITS(cp));
1834 return cp + 8;
1835
1836 trunc:
1837 printf("%s", tstr);
1838 return ep;
1839 }
1840
1841 /* [OF10] Section 5.4.4 */
1842 static const u_char *
1843 of10_error_print(const u_char *cp, const u_char *ep, const u_int len) {
1844 uint16_t type;
1845 const struct tok *code_str;
1846
1847 /* type */
1848 TCHECK2(*cp, 2);
1849 type = EXTRACT_16BITS(cp);
1850 cp += 2;
1851 printf("\n\t type %s", tok2str(ofpet_str, "invalid (0x%04x)", type));
1852 /* code */
1853 TCHECK2(*cp, 2);
1854 code_str =
1855 type == OFPET_HELLO_FAILED ? ofphfc_str :
1856 type == OFPET_BAD_REQUEST ? ofpbrc_str :
1857 type == OFPET_BAD_ACTION ? ofpbac_str :
1858 type == OFPET_FLOW_MOD_FAILED ? ofpfmfc_str :
1859 type == OFPET_PORT_MOD_FAILED ? ofppmfc_str :
1860 type == OFPET_QUEUE_OP_FAILED ? ofpqofc_str :
1861 empty_str;
1862 printf(", code %s", tok2str(code_str, "invalid (0x%04x)", EXTRACT_16BITS(cp)));
1863 cp += 2;
1864 /* data */
1865 return of10_data_print(cp, ep, len - OF_ERROR_MSG_LEN);
1866
1867 trunc:
1868 printf("%s", tstr);
1869 return ep;
1870 }
1871
1872 const u_char *
1873 of10_header_body_print(const u_char *cp, const u_char *ep, const uint8_t type,
1874 const uint16_t len, const uint32_t xid) {
1875 const u_char *cp0 = cp;
1876 const u_int len0 = len;
1877 /* Thus far message length is not less than the basic header size, but most
1878 * message types have additional assorted constraints on the length. Wherever
1879 * possible, check that message length meets the constraint, in remaining
1880 * cases check that the length is OK to begin decoding and leave any final
1881 * verification up to a lower-layer function. When the current message is
1882 * corrupt, proceed to the next message. */
1883
1884 /* [OF10] Section 5.1 */
1885 printf("\n\tversion 1.0, type %s, length %u, xid 0x%08x",
1886 tok2str(ofpt_str, "invalid (0x%02x)", type), len, xid);
1887 switch (type) {
1888 /* OpenFlow header only. */
1889 case OFPT_FEATURES_REQUEST: /* [OF10] Section 5.3.1 */
1890 case OFPT_GET_CONFIG_REQUEST: /* [OF10] Section 5.3.2 */
1891 case OFPT_BARRIER_REQUEST: /* [OF10] Section 5.3.7 */
1892 case OFPT_BARRIER_REPLY: /* ibid */
1893 if (len != OF_HEADER_LEN)
1894 goto corrupt;
1895 break;
1896
1897 /* OpenFlow header and fixed-size message body. */
1898 case OFPT_SET_CONFIG: /* [OF10] Section 5.3.2 */
1899 case OFPT_GET_CONFIG_REPLY: /* ibid */
1900 if (len != OF_SWITCH_CONFIG_LEN)
1901 goto corrupt;
1902 if (vflag < 1)
1903 goto next_message;
1904 /* flags */
1905 TCHECK2(*cp, 2);
1906 printf("\n\t flags %s", tok2str(ofp_config_str, "invalid (0x%04x)", EXTRACT_16BITS(cp)));
1907 cp += 2;
1908 /* miss_send_len */
1909 TCHECK2(*cp, 2);
1910 printf(", miss_send_len %u", EXTRACT_16BITS(cp));
1911 return cp + 2;
1912 case OFPT_PORT_MOD:
1913 if (len != OF_PORT_MOD_LEN)
1914 goto corrupt;
1915 if (vflag < 1)
1916 goto next_message;
1917 return of10_port_mod_print(cp, ep);
1918 case OFPT_QUEUE_GET_CONFIG_REQUEST: /* [OF10] Section 5.3.4 */
1919 if (len != OF_QUEUE_GET_CONFIG_REQUEST_LEN)
1920 goto corrupt;
1921 if (vflag < 1)
1922 goto next_message;
1923 /* port */
1924 TCHECK2(*cp, 2);
1925 printf("\n\t port_no %s", tok2str(ofpp_str, "%u", EXTRACT_16BITS(cp)));
1926 cp += 2;
1927 /* pad */
1928 TCHECK2(*cp, 2);
1929 return cp + 2;
1930 case OFPT_FLOW_REMOVED:
1931 if (len != OF_FLOW_REMOVED_LEN)
1932 goto corrupt;
1933 if (vflag < 1)
1934 goto next_message;
1935 return of10_flow_removed_print(cp, ep);
1936 case OFPT_PORT_STATUS: /* [OF10] Section 5.4.3 */
1937 if (len != OF_PORT_STATUS_LEN)
1938 goto corrupt;
1939 if (vflag < 1)
1940 goto next_message;
1941 /* reason */
1942 TCHECK2(*cp, 1);
1943 printf("\n\t reason %s", tok2str(ofppr_str, "invalid (0x%02x)", *cp));
1944 cp += 1;
1945 /* pad */
1946 TCHECK2(*cp, 7);
1947 cp += 7;
1948 /* desc */
1949 return of10_phy_ports_print(cp, ep, OF_PHY_PORT_LEN);
1950
1951 /* OpenFlow header, fixed-size message body and n * fixed-size data units. */
1952 case OFPT_FEATURES_REPLY:
1953 if (len < OF_SWITCH_FEATURES_LEN)
1954 goto corrupt;
1955 if (vflag < 1)
1956 goto next_message;
1957 return of10_features_reply_print(cp, ep, len);
1958
1959 /* OpenFlow header and variable-size data. */
1960 case OFPT_HELLO: /* [OF10] Section 5.5.1 */
1961 case OFPT_ECHO_REQUEST: /* [OF10] Section 5.5.2 */
1962 case OFPT_ECHO_REPLY: /* [OF10] Section 5.5.3 */
1963 if (vflag < 1)
1964 goto next_message;
1965 return of10_data_print(cp, ep, len - OF_HEADER_LEN);
1966
1967 /* OpenFlow header, fixed-size message body and variable-size data. */
1968 case OFPT_ERROR:
1969 if (len < OF_ERROR_MSG_LEN)
1970 goto corrupt;
1971 if (vflag < 1)
1972 goto next_message;
1973 return of10_error_print(cp, ep, len);
1974 case OFPT_VENDOR:
1975 /* [OF10] Section 5.5.4 */
1976 if (len < OF_VENDOR_HEADER_LEN)
1977 goto corrupt;
1978 if (vflag < 1)
1979 goto next_message;
1980 return of10_vendor_data_print(cp, ep, len - OF_HEADER_LEN);
1981 case OFPT_PACKET_IN:
1982 /* 2 mock octets count in OF_PACKET_IN_LEN but not in len */
1983 if (len < OF_PACKET_IN_LEN - 2)
1984 goto corrupt;
1985 if (vflag < 1)
1986 goto next_message;
1987 return of10_packet_in_print(cp, ep, len);
1988
1989 /* a. OpenFlow header. */
1990 /* b. OpenFlow header and one of the fixed-size message bodies. */
1991 /* c. OpenFlow header, fixed-size message body and variable-size data. */
1992 case OFPT_STATS_REQUEST:
1993 if (len < OF_STATS_REQUEST_LEN)
1994 goto corrupt;
1995 if (vflag < 1)
1996 goto next_message;
1997 return of10_stats_request_print(cp, ep, len);
1998
1999 /* a. OpenFlow header and fixed-size message body. */
2000 /* b. OpenFlow header and n * fixed-size data units. */
2001 /* c. OpenFlow header and n * variable-size data units. */
2002 /* d. OpenFlow header, fixed-size message body and variable-size data. */
2003 case OFPT_STATS_REPLY:
2004 if (len < OF_STATS_REPLY_LEN)
2005 goto corrupt;
2006 if (vflag < 1)
2007 goto next_message;
2008 return of10_stats_reply_print(cp, ep, len);
2009
2010 /* OpenFlow header and n * variable-size data units and variable-size data. */
2011 case OFPT_PACKET_OUT:
2012 if (len < OF_PACKET_OUT_LEN)
2013 goto corrupt;
2014 if (vflag < 1)
2015 goto next_message;
2016 return of10_packet_out_print(cp, ep, len);
2017
2018 /* OpenFlow header, fixed-size message body and n * variable-size data units. */
2019 case OFPT_FLOW_MOD:
2020 if (len < OF_FLOW_MOD_LEN)
2021 goto corrupt;
2022 if (vflag < 1)
2023 goto next_message;
2024 return of10_flow_mod_print(cp, ep, len);
2025
2026 /* OpenFlow header, fixed-size message body and n * variable-size data units. */
2027 case OFPT_QUEUE_GET_CONFIG_REPLY: /* [OF10] Section 5.3.4 */
2028 if (len < OF_QUEUE_GET_CONFIG_REPLY_LEN)
2029 goto corrupt;
2030 if (vflag < 1)
2031 goto next_message;
2032 /* port */
2033 TCHECK2(*cp, 2);
2034 printf("\n\t port_no %s", tok2str(ofpp_str, "%u", EXTRACT_16BITS(cp)));
2035 cp += 2;
2036 /* pad */
2037 TCHECK2(*cp, 6);
2038 cp += 6;
2039 /* queues */
2040 return of10_queues_print(cp, ep, len - OF_QUEUE_GET_CONFIG_REPLY_LEN);
2041 } /* switch (type) */
2042 goto next_message;
2043
2044 corrupt: /* skip the message body */
2045 printf("%s", cstr);
2046 next_message:
2047 TCHECK2(*cp0, len0 - OF_HEADER_LEN);
2048 return cp0 + len0 - OF_HEADER_LEN;
2049 trunc:
2050 printf("%s", tstr);
2051 return ep;
2052 }