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