xref: /freebsd/contrib/tcpdump/print-openflow-1.3.c (revision 7ab1a32cd43cbae61ad4dd435d6a482bbf61cb52)
1 /*
2  * This module implements decoding of OpenFlow protocol version 1.3 (wire
3  * protocol 0x04). It is based on the implementation conventions explained in
4  * print-openflow-1.0.c.
5  *
6  * [OF13] https://www.opennetworking.org/wp-content/uploads/2014/10/openflow-switch-v1.3.5.pdf
7  *
8  * Copyright (c) 2020 The TCPDUMP project
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /* \summary: OpenFlow protocol version 1.3 printer */
35 
36 #include <config.h>
37 
38 #include "netdissect-stdinc.h"
39 
40 #define ND_LONGJMP_FROM_TCHECK
41 #include "netdissect.h"
42 #include "extract.h"
43 #include "addrtoname.h"
44 #include "openflow.h"
45 
46 #define OFPT_HELLO                     0U
47 #define OFPT_ERROR                     1U
48 #define OFPT_ECHO_REQUEST              2U
49 #define OFPT_ECHO_REPLY                3U
50 #define OFPT_EXPERIMENTER              4U
51 #define OFPT_FEATURES_REQUEST          5U
52 #define OFPT_FEATURES_REPLY            6U
53 #define OFPT_GET_CONFIG_REQUEST        7U
54 #define OFPT_GET_CONFIG_REPLY          8U
55 #define OFPT_SET_CONFIG                9U
56 #define OFPT_PACKET_IN                10U
57 #define OFPT_FLOW_REMOVED             11U
58 #define OFPT_PORT_STATUS              12U
59 #define OFPT_PACKET_OUT               13U
60 #define OFPT_FLOW_MOD                 14U
61 #define OFPT_GROUP_MOD                15U
62 #define OFPT_PORT_MOD                 16U
63 #define OFPT_TABLE_MOD                17U
64 #define OFPT_MULTIPART_REQUEST        18U
65 #define OFPT_MULTIPART_REPLY          19U
66 #define OFPT_BARRIER_REQUEST          20U
67 #define OFPT_BARRIER_REPLY            21U
68 #define OFPT_QUEUE_GET_CONFIG_REQUEST 22U
69 #define OFPT_QUEUE_GET_CONFIG_REPLY   23U
70 #define OFPT_ROLE_REQUEST             24U
71 #define OFPT_ROLE_REPLY               25U
72 #define OFPT_GET_ASYNC_REQUEST        26U
73 #define OFPT_GET_ASYNC_REPLY          27U
74 #define OFPT_SET_ASYNC                28U
75 #define OFPT_METER_MOD                29U
76 #define OFPT_MAX                      OFPT_METER_MOD
77 
78 #define OFPC_FLOW_STATS   (1U <<0)
79 #define OFPC_TABLE_STATS  (1U <<1)
80 #define OFPC_PORT_STATS   (1U <<2)
81 #define OFPC_GROUP_STATS  (1U <<3)
82 #define OFPC_IP_REASM     (1U <<5)
83 #define OFPC_QUEUE_STATS  (1U <<6)
84 #define OFPC_PORT_BLOCKED (1U <<8)
85 static const struct tok ofp_capabilities_bm[] = {
86 	{ OFPC_FLOW_STATS,   "FLOW_STATS"   },
87 	{ OFPC_TABLE_STATS,  "TABLE_STATS"  },
88 	{ OFPC_PORT_STATS,   "PORT_STATS"   },
89 	{ OFPC_GROUP_STATS,  "GROUP_STATS"  },
90 	{ OFPC_IP_REASM,     "IP_REASM"     },
91 	{ OFPC_QUEUE_STATS,  "QUEUE_STATS"  },
92 	{ OFPC_PORT_BLOCKED, "PORT_BLOCKED" },
93 	{ 0, NULL }
94 };
95 #define OFPCAP_U (~(OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
96                     OFPC_GROUP_STATS | OFPC_IP_REASM | OFPC_QUEUE_STATS | \
97                     OFPC_PORT_BLOCKED))
98 
99 #define OFPC_FRAG_NORMAL 0U
100 #define OFPC_FRAG_DROP   1U
101 #define OFPC_FRAG_REASM  2U
102 static const struct tok ofp_config_str[] = {
103 	{ OFPC_FRAG_NORMAL, "FRAG_NORMAL" },
104 	{ OFPC_FRAG_DROP,   "FRAG_DROP"   },
105 	{ OFPC_FRAG_REASM,  "FRAG_REASM"  },
106 	{ 0, NULL }
107 };
108 
109 #define OFPTT_MAX 0xfeU
110 #define OFPTT_ALL 0xffU
111 static const struct tok ofptt_str[] = {
112 	{ OFPTT_MAX, "MAX" },
113 	{ OFPTT_ALL, "ALL" },
114 	{ 0, NULL },
115 };
116 
117 #define OFPCML_MAX       0xffe5U
118 #define OFPCML_NO_BUFFER 0xffffU
119 static const struct tok ofpcml_str[] = {
120 	{ OFPCML_MAX,       "MAX"       },
121 	{ OFPCML_NO_BUFFER, "NO_BUFFER" },
122 	{ 0, NULL }
123 };
124 
125 #define OFPPC_PORT_DOWN    (1U <<0)
126 #define OFPPC_NO_RECV      (1U <<2)
127 #define OFPPC_NO_FWD       (1U <<5)
128 #define OFPPC_NO_PACKET_IN (1U <<6)
129 static const struct tok ofppc_bm[] = {
130 	{ OFPPC_PORT_DOWN,    "PORT_DOWN"    },
131 	{ OFPPC_NO_RECV,      "NO_RECV"      },
132 	{ OFPPC_NO_FWD,       "NO_FWD"       },
133 	{ OFPPC_NO_PACKET_IN, "NO_PACKET_IN" },
134 	{ 0, NULL }
135 };
136 #define OFPPC_U (~(OFPPC_PORT_DOWN | OFPPC_NO_RECV | OFPPC_NO_FWD | \
137                    OFPPC_NO_PACKET_IN))
138 
139 #define OFPPS_LINK_DOWN   (1U << 0)
140 #define OFPPS_BLOCKED     (1U << 1)
141 #define OFPPS_LIVE        (1U << 2)
142 static const struct tok ofpps_bm[] = {
143 	{ OFPPS_LINK_DOWN, "LINK_DOWN" },
144 	{ OFPPS_BLOCKED,   "BLOCKED"   },
145 	{ OFPPS_LIVE,      "LIVE"      },
146 	{ 0, NULL }
147 };
148 #define OFPPS_U (~(OFPPS_LINK_DOWN | OFPPS_BLOCKED | OFPPS_LIVE))
149 
150 #define OFPPF_10MB_HD    (1U <<  0)
151 #define OFPPF_10MB_FD    (1U <<  1)
152 #define OFPPF_100MB_HD   (1U <<  2)
153 #define OFPPF_100MB_FD   (1U <<  3)
154 #define OFPPF_1GB_HD     (1U <<  4)
155 #define OFPPF_1GB_FD     (1U <<  5)
156 #define OFPPF_10GB_FD    (1U <<  6)
157 #define OFPPF_40GB_FD    (1U <<  7)
158 #define OFPPF_100GB_FD   (1U <<  8)
159 #define OFPPF_1TB_FD     (1U <<  9)
160 #define OFPPF_OTHER      (1U << 10)
161 #define OFPPF_COPPER     (1U << 11)
162 #define OFPPF_FIBER      (1U << 12)
163 #define OFPPF_AUTONEG    (1U << 13)
164 #define OFPPF_PAUSE      (1U << 14)
165 #define OFPPF_PAUSE_ASYM (1U << 15)
166 static const struct tok ofppf_bm[] = {
167 	{ OFPPF_10MB_HD,    "10MB_HD"    },
168 	{ OFPPF_10MB_FD,    "10MB_FD"    },
169 	{ OFPPF_100MB_HD,   "100MB_HD"   },
170 	{ OFPPF_100MB_FD,   "100MB_FD"   },
171 	{ OFPPF_1GB_HD,     "1GB_HD"     },
172 	{ OFPPF_1GB_FD,     "1GB_FD"     },
173 	{ OFPPF_10GB_FD,    "10GB_FD"    },
174 	{ OFPPF_40GB_FD,    "40GB_FD"    },
175 	{ OFPPF_100GB_FD,   "100GB_FD"   },
176 	{ OFPPF_1TB_FD,     "1TB_FD"     },
177 	{ OFPPF_OTHER,      "OTHER"      },
178 	{ OFPPF_COPPER,     "COPPER"     },
179 	{ OFPPF_FIBER,      "FIBER"      },
180 	{ OFPPF_AUTONEG,    "AUTONEG"    },
181 	{ OFPPF_PAUSE,      "PAUSE"      },
182 	{ OFPPF_PAUSE_ASYM, "PAUSE_ASYM" },
183 	{ 0, NULL }
184 };
185 #define OFPPF_U (~(OFPPF_10MB_HD | OFPPF_10MB_FD | OFPPF_100MB_HD | \
186                    OFPPF_100MB_FD | OFPPF_1GB_HD | OFPPF_1GB_FD | \
187                    OFPPF_10GB_FD | OFPPF_40GB_FD | OFPPF_100GB_FD | \
188                    OFPPF_1TB_FD | OFPPF_OTHER | OFPPF_COPPER | OFPPF_FIBER | \
189                    OFPPF_AUTONEG | OFPPF_PAUSE | OFPPF_PAUSE_ASYM))
190 
191 #define OFPHET_VERSIONBITMAP 1U
192 static const struct tok ofphet_str[] = {
193 	{ OFPHET_VERSIONBITMAP, "VERSIONBITMAP" },
194 	{ 0, NULL }
195 };
196 
197 #define OFPP_MAX        0xffffff00U
198 #define OFPP_IN_PORT    0xfffffff8U
199 #define OFPP_TABLE      0xfffffff9U
200 #define OFPP_NORMAL     0xfffffffaU
201 #define OFPP_FLOOD      0xfffffffbU
202 #define OFPP_ALL        0xfffffffcU
203 #define OFPP_CONTROLLER 0xfffffffdU
204 #define OFPP_LOCAL      0xfffffffeU
205 #define OFPP_ANY        0xffffffffU
206 static const struct tok ofpp_str[] = {
207 	{ OFPP_MAX,        "MAX"        },
208 	{ OFPP_IN_PORT,    "IN_PORT"    },
209 	{ OFPP_TABLE,      "TABLE"      },
210 	{ OFPP_NORMAL,     "NORMAL"     },
211 	{ OFPP_FLOOD,      "FLOOD"      },
212 	{ OFPP_ALL,        "ALL"        },
213 	{ OFPP_CONTROLLER, "CONTROLLER" },
214 	{ OFPP_LOCAL,      "LOCAL"      },
215 	{ OFPP_ANY,        "ANY"        },
216 	{ 0, NULL }
217 };
218 
219 #define OFPCR_ROLE_NOCHANGE 0U
220 #define OFPCR_ROLE_EQUAL    1U
221 #define OFPCR_ROLE_MASTER   2U
222 #define OFPCR_ROLE_SLAVE    3U
223 static const struct tok ofpcr_str[] = {
224 	{ OFPCR_ROLE_NOCHANGE, "NOCHANGE" },
225 	{ OFPCR_ROLE_EQUAL,    "EQUAL"    },
226 	{ OFPCR_ROLE_MASTER,   "MASTER"   },
227 	{ OFPCR_ROLE_SLAVE,    "SLAVE"    },
228 	{ 0, NULL }
229 };
230 
231 #define OF_BIT_VER_1_0 (1U << (OF_VER_1_0 - 1))
232 #define OF_BIT_VER_1_1 (1U << (OF_VER_1_1 - 1))
233 #define OF_BIT_VER_1_2 (1U << (OF_VER_1_2 - 1))
234 #define OF_BIT_VER_1_3 (1U << (OF_VER_1_3 - 1))
235 #define OF_BIT_VER_1_4 (1U << (OF_VER_1_4 - 1))
236 #define OF_BIT_VER_1_5 (1U << (OF_VER_1_5 - 1))
237 static const struct tok ofverbm_str[] = {
238 	{ OF_BIT_VER_1_0, "1.0" },
239 	{ OF_BIT_VER_1_1, "1.1" },
240 	{ OF_BIT_VER_1_2, "1.2" },
241 	{ OF_BIT_VER_1_3, "1.3" },
242 	{ OF_BIT_VER_1_4, "1.4" },
243 	{ OF_BIT_VER_1_5, "1.5" },
244 	{ 0, NULL }
245 };
246 #define OF_BIT_VER_U (~(OF_BIT_VER_1_0 | OF_BIT_VER_1_1 | OF_BIT_VER_1_2 | \
247                         OF_BIT_VER_1_3 | OF_BIT_VER_1_4 | OF_BIT_VER_1_5))
248 
249 #define OFPR_NO_MATCH    0U
250 #define OFPR_ACTION      1U
251 #define OFPR_INVALID_TTL 2U
252 #if 0 /* for OFPT_PACKET_IN */
253 static const struct tok ofpr_str[] = {
254 	{ OFPR_NO_MATCH,    "NO_MATCH"         },
255 	{ OFPR_ACTION,      "ACTION"           },
256 	{ OFPR_INVALID_TTL, "OFPR_INVALID_TTL" },
257 	{ 0, NULL }
258 };
259 #endif
260 
261 #define ASYNC_OFPR_NO_MATCH    (1U << OFPR_NO_MATCH   )
262 #define ASYNC_OFPR_ACTION      (1U << OFPR_ACTION     )
263 #define ASYNC_OFPR_INVALID_TTL (1U << OFPR_INVALID_TTL)
264 static const struct tok async_ofpr_bm[] = {
265 	{ ASYNC_OFPR_NO_MATCH,    "NO_MATCH"    },
266 	{ ASYNC_OFPR_ACTION,      "ACTION"      },
267 	{ ASYNC_OFPR_INVALID_TTL, "INVALID_TTL" },
268 	{ 0, NULL }
269 };
270 #define ASYNC_OFPR_U (~(ASYNC_OFPR_NO_MATCH | ASYNC_OFPR_ACTION | \
271                         ASYNC_OFPR_INVALID_TTL))
272 
273 #define OFPPR_ADD    0U
274 #define OFPPR_DELETE 1U
275 #define OFPPR_MODIFY 2U
276 static const struct tok ofppr_str[] = {
277 	{ OFPPR_ADD,    "ADD"    },
278 	{ OFPPR_DELETE, "DELETE" },
279 	{ OFPPR_MODIFY, "MODIFY" },
280 	{ 0, NULL }
281 };
282 
283 #define ASYNC_OFPPR_ADD    (1U << OFPPR_ADD   )
284 #define ASYNC_OFPPR_DELETE (1U << OFPPR_DELETE)
285 #define ASYNC_OFPPR_MODIFY (1U << OFPPR_MODIFY)
286 static const struct tok async_ofppr_bm[] = {
287 	{ ASYNC_OFPPR_ADD,    "ADD"    },
288 	{ ASYNC_OFPPR_DELETE, "DELETE" },
289 	{ ASYNC_OFPPR_MODIFY, "MODIFY" },
290 	{ 0, NULL }
291 };
292 #define ASYNC_OFPPR_U (~(ASYNC_OFPPR_ADD | ASYNC_OFPPR_DELETE | \
293                          ASYNC_OFPPR_MODIFY))
294 
295 #define OFPET_HELLO_FAILED           0U
296 #define OFPET_BAD_REQUEST            1U
297 #define OFPET_BAD_ACTION             2U
298 #define OFPET_BAD_INSTRUCTION        3U
299 #define OFPET_BAD_MATCH              4U
300 #define OFPET_FLOW_MOD_FAILED        5U
301 #define OFPET_GROUP_MOD_FAILED       6U
302 #define OFPET_PORT_MOD_FAILED        7U
303 #define OFPET_TABLE_MOD_FAILED       8U
304 #define OFPET_QUEUE_OP_FAILED        9U
305 #define OFPET_SWITCH_CONFIG_FAILED  10U
306 #define OFPET_ROLE_REQUEST_FAILED   11U
307 #define OFPET_METER_MOD_FAILED      12U
308 #define OFPET_TABLE_FEATURES_FAILED 13U
309 #define OFPET_EXPERIMENTER          0xffffU /* a special case */
310 static const struct tok ofpet_str[] = {
311 	{ OFPET_HELLO_FAILED,          "HELLO_FAILED"          },
312 	{ OFPET_BAD_REQUEST,           "BAD_REQUEST"           },
313 	{ OFPET_BAD_ACTION,            "BAD_ACTION"            },
314 	{ OFPET_BAD_INSTRUCTION,       "BAD_INSTRUCTION"       },
315 	{ OFPET_BAD_MATCH,             "BAD_MATCH"             },
316 	{ OFPET_FLOW_MOD_FAILED,       "FLOW_MOD_FAILED"       },
317 	{ OFPET_GROUP_MOD_FAILED,      "GROUP_MOD_FAILED"      },
318 	{ OFPET_PORT_MOD_FAILED,       "PORT_MOD_FAILED"       },
319 	{ OFPET_TABLE_MOD_FAILED,      "TABLE_MOD_FAILED"      },
320 	{ OFPET_QUEUE_OP_FAILED,       "QUEUE_OP_FAILED"       },
321 	{ OFPET_SWITCH_CONFIG_FAILED,  "SWITCH_CONFIG_FAILED"  },
322 	{ OFPET_ROLE_REQUEST_FAILED,   "ROLE_REQUEST_FAILED"   },
323 	{ OFPET_METER_MOD_FAILED,      "METER_MOD_FAILED"      },
324 	{ OFPET_TABLE_FEATURES_FAILED, "TABLE_FEATURES_FAILED" },
325 	{ OFPET_EXPERIMENTER,          "EXPERIMENTER"          },
326 	{ 0, NULL }
327 };
328 
329 #define OFPHFC_INCOMPATIBLE 0U
330 #define OFPHFC_EPERM        1U
331 static const struct tok ofphfc_str[] = {
332 	{ OFPHFC_INCOMPATIBLE, "INCOMPATIBLE" },
333 	{ OFPHFC_EPERM,        "EPERM"        },
334 	{ 0, NULL }
335 };
336 
337 #define OFPBRC_BAD_VERSION                0U
338 #define OFPBRC_BAD_TYPE                   1U
339 #define OFPBRC_BAD_MULTIPART              2U
340 #define OFPBRC_BAD_EXPERIMENTER           3U
341 #define OFPBRC_BAD_EXP_TYPE               4U
342 #define OFPBRC_EPERM                      5U
343 #define OFPBRC_BAD_LEN                    6U
344 #define OFPBRC_BUFFER_EMPTY               7U
345 #define OFPBRC_BUFFER_UNKNOWN             8U
346 #define OFPBRC_BAD_TABLE_ID               9U
347 #define OFPBRC_IS_SLAVE                  10U
348 #define OFPBRC_BAD_PORT                  11U
349 #define OFPBRC_BAD_PACKET                12U
350 #define OFPBRC_MULTIPART_BUFFER_OVERFLOW 13U
351 static const struct tok ofpbrc_str[] = {
352 	{ OFPBRC_BAD_VERSION,               "BAD_VERSION"               },
353 	{ OFPBRC_BAD_TYPE,                  "BAD_TYPE"                  },
354 	{ OFPBRC_BAD_MULTIPART,             "BAD_MULTIPART"             },
355 	{ OFPBRC_BAD_EXPERIMENTER,          "BAD_EXPERIMENTER"          },
356 	{ OFPBRC_BAD_EXP_TYPE,              "BAD_EXP_TYPE"              },
357 	{ OFPBRC_EPERM,                     "EPERM"                     },
358 	{ OFPBRC_BAD_LEN,                   "BAD_LEN"                   },
359 	{ OFPBRC_BUFFER_EMPTY,              "BUFFER_EMPTY"              },
360 	{ OFPBRC_BUFFER_UNKNOWN,            "BUFFER_UNKNOWN"            },
361 	{ OFPBRC_BAD_TABLE_ID,              "BAD_TABLE_ID"              },
362 	{ OFPBRC_IS_SLAVE,                  "IS_SLAVE"                  },
363 	{ OFPBRC_BAD_PORT,                  "BAD_PORT"                  },
364 	{ OFPBRC_BAD_PACKET,                "BAD_PACKET"                },
365 	{ OFPBRC_MULTIPART_BUFFER_OVERFLOW, "MULTIPART_BUFFER_OVERFLOW" },
366 	{ 0, NULL }
367 };
368 
369 #define OFPBAC_BAD_TYPE            0U
370 #define OFPBAC_BAD_LEN             1U
371 #define OFPBAC_BAD_EXPERIMENTER    2U
372 #define OFPBAC_BAD_EXP_TYPE        3U
373 #define OFPBAC_BAD_OUT_PORT        4U
374 #define OFPBAC_BAD_ARGUMENT        5U
375 #define OFPBAC_EPERM               6U
376 #define OFPBAC_TOO_MANY            7U
377 #define OFPBAC_BAD_QUEUE           8U
378 #define OFPBAC_BAD_OUT_GROUP       9U
379 #define OFPBAC_MATCH_INCONSISTENT 10U
380 #define OFPBAC_UNSUPPORTED_ORDER  11U
381 #define OFPBAC_BAD_TAG            12U
382 #define OFPBAC_BAD_SET_TYPE       13U
383 #define OFPBAC_BAD_SET_LEN        14U
384 #define OFPBAC_BAD_SET_ARGUMENT   15U
385 static const struct tok ofpbac_str[] = {
386 	{ OFPBAC_BAD_TYPE,           "BAD_TYPE"           },
387 	{ OFPBAC_BAD_LEN,            "BAD_LEN"            },
388 	{ OFPBAC_BAD_EXPERIMENTER,   "BAD_EXPERIMENTER"   },
389 	{ OFPBAC_BAD_EXP_TYPE,       "BAD_EXP_TYPE"       },
390 	{ OFPBAC_BAD_OUT_PORT,       "BAD_OUT_PORT"       },
391 	{ OFPBAC_BAD_ARGUMENT,       "BAD_ARGUMENT"       },
392 	{ OFPBAC_EPERM,              "EPERM"              },
393 	{ OFPBAC_TOO_MANY,           "TOO_MANY"           },
394 	{ OFPBAC_BAD_QUEUE,          "BAD_QUEUE"          },
395 	{ OFPBAC_BAD_OUT_GROUP,      "BAD_OUT_GROUP"      },
396 	{ OFPBAC_MATCH_INCONSISTENT, "MATCH_INCONSISTENT" },
397 	{ OFPBAC_UNSUPPORTED_ORDER,  "UNSUPPORTED_ORDER"  },
398 	{ OFPBAC_BAD_TAG,            "BAD_TAG"            },
399 	{ OFPBAC_BAD_SET_TYPE,       "BAD_SET_TYPE"       },
400 	{ OFPBAC_BAD_SET_LEN,        "BAD_SET_LEN"        },
401 	{ OFPBAC_BAD_SET_ARGUMENT,   "BAD_SET_ARGUMENT"   },
402 	{ 0, NULL }
403 };
404 
405 #define OFPBIC_UNKNOWN_INST        0U
406 #define OFPBIC_UNSUP_INST          1U
407 #define OFPBIC_BAD_TABLE_ID        2U
408 #define OFPBIC_UNSUP_METADATA      3U
409 #define OFPBIC_UNSUP_METADATA_MASK 4U
410 #define OFPBIC_BAD_EXPERIMENTER    5U
411 #define OFPBIC_BAD_EXP_TYPE        6U
412 #define OFPBIC_BAD_LEN             7U
413 #define OFPBIC_EPERM               8U
414 static const struct tok ofpbic_str[] = {
415 	{ OFPBIC_UNKNOWN_INST,        "UNKNOWN_INST"        },
416 	{ OFPBIC_UNSUP_INST,          "UNSUP_INST"          },
417 	{ OFPBIC_BAD_TABLE_ID,        "BAD_TABLE_ID"        },
418 	{ OFPBIC_UNSUP_METADATA,      "UNSUP_METADATA"      },
419 	{ OFPBIC_UNSUP_METADATA_MASK, "UNSUP_METADATA_MASK" },
420 	{ OFPBIC_BAD_EXPERIMENTER,    "BAD_EXPERIMENTER"    },
421 	{ OFPBIC_BAD_EXP_TYPE,        "BAD_EXP_TYPE"        },
422 	{ OFPBIC_BAD_LEN,             "BAD_LEN"             },
423 	{ OFPBIC_EPERM,               "EPERM"               },
424 	{ 0, NULL }
425 };
426 
427 #define OFPBMC_BAD_TYPE          0U
428 #define OFPBMC_BAD_LEN           1U
429 #define OFPBMC_BAD_TAG           2U
430 #define OFPBMC_BAD_DL_ADDR_MASK  3U
431 #define OFPBMC_BAD_NW_ADDR_MASK  4U
432 #define OFPBMC_BAD_WILDCARDS     5U
433 #define OFPBMC_BAD_FIELD         6U
434 #define OFPBMC_BAD_VALUE         7U
435 #define OFPBMC_BAD_MASK          8U
436 #define OFPBMC_BAD_PREREQ        9U
437 #define OFPBMC_DUP_FIELD        10U
438 #define OFPBMC_EPERM            11U
439 static const struct tok ofpbmc_str[] = {
440 	{ OFPBMC_BAD_TYPE,         "BAD_TYPE"         },
441 	{ OFPBMC_BAD_LEN,          "BAD_LEN"          },
442 	{ OFPBMC_BAD_TAG,          "BAD_TAG"          },
443 	{ OFPBMC_BAD_DL_ADDR_MASK, "BAD_DL_ADDR_MASK" },
444 	{ OFPBMC_BAD_NW_ADDR_MASK, "BAD_NW_ADDR_MASK" },
445 	{ OFPBMC_BAD_WILDCARDS,    "BAD_WILDCARDS"    },
446 	{ OFPBMC_BAD_FIELD,        "BAD_FIELD"        },
447 	{ OFPBMC_BAD_VALUE,        "BAD_VALUE"        },
448 	{ OFPBMC_BAD_MASK,         "BAD_MASK"         },
449 	{ OFPBMC_BAD_PREREQ,       "BAD_PREREQ"       },
450 	{ OFPBMC_DUP_FIELD,        "DUP_FIELD"        },
451 	{ OFPBMC_EPERM,            "EPERM"            },
452 	{ 0, NULL }
453 };
454 
455 #define OFPFMFC_UNKNOWN      0U
456 #define OFPFMFC_TABLE_FULL   1U
457 #define OFPFMFC_BAD_TABLE_ID 2U
458 #define OFPFMFC_OVERLAP      3U
459 #define OFPFMFC_EPERM        4U
460 #define OFPFMFC_BAD_TIMEOUT  5U
461 #define OFPFMFC_BAD_COMMAND  6U
462 #define OFPFMFC_BAD_FLAGS    7U
463 static const struct tok ofpfmfc_str[] = {
464 	{ OFPFMFC_UNKNOWN,      "UNKNOWN"      },
465 	{ OFPFMFC_TABLE_FULL,   "TABLE_FULL"   },
466 	{ OFPFMFC_BAD_TABLE_ID, "BAD_TABLE_ID" },
467 	{ OFPFMFC_OVERLAP,      "OVERLAP"      },
468 	{ OFPFMFC_EPERM,        "EPERM"        },
469 	{ OFPFMFC_BAD_TIMEOUT,  "BAD_TIMEOUT"  },
470 	{ OFPFMFC_BAD_COMMAND,  "BAD_COMMAND"  },
471 	{ OFPFMFC_BAD_FLAGS,    "BAD_FLAGS"    },
472 	{ 0, NULL }
473 };
474 
475 #define OFPGMFC_GROUP_EXISTS          0U
476 #define OFPGMFC_INVALID_GROUP         1U
477 #define OFPGMFC_WEIGHT_UNSUPPORTED    2U
478 #define OFPGMFC_OUT_OF_GROUPS         3U
479 #define OFPGMFC_OUT_OF_BUCKETS        4U
480 #define OFPGMFC_CHAINING_UNSUPPORTED  5U
481 #define OFPGMFC_WATCH_UNSUPPORTED     6U
482 #define OFPGMFC_LOOP                  7U
483 #define OFPGMFC_UNKNOWN_GROUP         8U
484 #define OFPGMFC_CHAINED_GROUP         9U
485 #define OFPGMFC_BAD_TYPE             10U
486 #define OFPGMFC_BAD_COMMAND          11U
487 #define OFPGMFC_BAD_BUCKET           12U
488 #define OFPGMFC_BAD_MATCH            13U
489 #define OFPGMFC_EPERM                14U
490 static const struct tok ofpgmfc_str[] = {
491 	{ OFPGMFC_GROUP_EXISTS,         "GROUP_EXISTS"         },
492 	{ OFPGMFC_INVALID_GROUP,        "INVALID_GROUP"        },
493 	{ OFPGMFC_WEIGHT_UNSUPPORTED,   "WEIGHT_UNSUPPORTED"   },
494 	{ OFPGMFC_OUT_OF_GROUPS,        "OUT_OF_GROUPS"        },
495 	{ OFPGMFC_OUT_OF_BUCKETS,       "OUT_OF_BUCKETS"       },
496 	{ OFPGMFC_CHAINING_UNSUPPORTED, "CHAINING_UNSUPPORTED" },
497 	{ OFPGMFC_WATCH_UNSUPPORTED,    "WATCH_UNSUPPORTED"    },
498 	{ OFPGMFC_LOOP,                 "LOOP"                 },
499 	{ OFPGMFC_UNKNOWN_GROUP,        "UNKNOWN_GROUP"        },
500 	{ OFPGMFC_CHAINED_GROUP,        "CHAINED_GROUP"        },
501 	{ OFPGMFC_BAD_TYPE,             "BAD_TYPE"             },
502 	{ OFPGMFC_BAD_COMMAND,          "BAD_COMMAND"          },
503 	{ OFPGMFC_BAD_BUCKET,           "BAD_BUCKET"           },
504 	{ OFPGMFC_BAD_MATCH,            "BAD_MATCH"            },
505 	{ OFPGMFC_EPERM,                "EPERM"                },
506 	{ 0, NULL }
507 };
508 
509 #define OFPPMFC_BAD_PORT      0U
510 #define OFPPMFC_BAD_HW_ADDR   1U
511 #define OFPPMFC_BAD_CONFIG    2U
512 #define OFPPMFC_BAD_ADVERTISE 3U
513 #define OFPPMFC_EPERM         4U
514 static const struct tok ofppmfc_str[] = {
515 	{ OFPPMFC_BAD_PORT,      "BAD_PORT"      },
516 	{ OFPPMFC_BAD_HW_ADDR,   "BAD_HW_ADDR"   },
517 	{ OFPPMFC_BAD_CONFIG,    "BAD_CONFIG"    },
518 	{ OFPPMFC_BAD_ADVERTISE, "BAD_ADVERTISE" },
519 	{ OFPPMFC_EPERM,         "EPERM"         },
520 	{ 0, NULL }
521 };
522 
523 #define OFPTMFC_BAD_TABLE  0U
524 #define OFPTMFC_BAD_CONFIG 1U
525 #define OFPTMFC_EPERM      2U
526 static const struct tok ofptmfc_str[] = {
527 	{ OFPTMFC_BAD_TABLE,  "BAD_TABLE"  },
528 	{ OFPTMFC_BAD_CONFIG, "BAD_CONFIG" },
529 	{ OFPTMFC_EPERM,      "EPERM"      },
530 	{ 0, NULL }
531 };
532 
533 #define OFPQOFC_BAD_PORT  0U
534 #define OFPQOFC_BAD_QUEUE 1U
535 #define OFPQOFC_EPERM     2U
536 static const struct tok ofpqofc_str[] = {
537 	{ OFPQOFC_BAD_PORT,  "BAD_PORT"  },
538 	{ OFPQOFC_BAD_QUEUE, "BAD_QUEUE" },
539 	{ OFPQOFC_EPERM,     "EPERM"     },
540 	{ 0, NULL }
541 };
542 
543 #define OFPSCFC_BAD_FLAGS 0U
544 #define OFPSCFC_BAD_LEN   1U
545 #define OFPSCFC_EPERM     2U
546 static const struct tok ofpscfc_str[] = {
547 	{ OFPSCFC_BAD_FLAGS, "BAD_FLAGS" },
548 	{ OFPSCFC_BAD_LEN,   "BAD_LEN"   },
549 	{ OFPSCFC_EPERM,     "EPERM"     },
550 	{ 0, NULL }
551 };
552 
553 #define OFPRRFC_STALE    0U
554 #define OFPRRFC_UNSUP    1U
555 #define OFPRRFC_BAD_ROLE 2U
556 static const struct tok ofprrfc_str[] = {
557 	{ OFPRRFC_STALE,    "STALE"    },
558 	{ OFPRRFC_UNSUP,    "UNSUP"    },
559 	{ OFPRRFC_BAD_ROLE, "BAD_ROLE" },
560 	{ 0, NULL }
561 };
562 
563 #define OFPMMFC_UNKNOWN         0U
564 #define OFPMMFC_METER_EXISTS    1U
565 #define OFPMMFC_INVALID_METER   2U
566 #define OFPMMFC_UNKNOWN_METER   3U
567 #define OFPMMFC_BAD_COMMAND     4U
568 #define OFPMMFC_BAD_FLAGS       5U
569 #define OFPMMFC_BAD_RATE        6U
570 #define OFPMMFC_BAD_BURST       7U
571 #define OFPMMFC_BAD_BAND        8U
572 #define OFPMMFC_BAD_BAND_VALUE  9U
573 #define OFPMMFC_OUT_OF_METERS  10U
574 #define OFPMMFC_OUT_OF_BANDS   11U
575 static const struct tok ofpmmfc_str[] = {
576 	{ OFPMMFC_UNKNOWN,        "UNKNOWN"        },
577 	{ OFPMMFC_METER_EXISTS,   "METER_EXISTS"   },
578 	{ OFPMMFC_INVALID_METER,  "INVALID_METER"  },
579 	{ OFPMMFC_UNKNOWN_METER,  "UNKNOWN_METER"  },
580 	{ OFPMMFC_BAD_COMMAND,    "BAD_COMMAND"    },
581 	{ OFPMMFC_BAD_FLAGS,      "BAD_FLAGS"      },
582 	{ OFPMMFC_BAD_RATE,       "BAD_RATE"       },
583 	{ OFPMMFC_BAD_BURST,      "BAD_BURST"      },
584 	{ OFPMMFC_BAD_BAND,       "BAD_BAND"       },
585 	{ OFPMMFC_BAD_BAND_VALUE, "BAD_BAND_VALUE" },
586 	{ OFPMMFC_OUT_OF_METERS,  "OUT_OF_METERS"  },
587 	{ OFPMMFC_OUT_OF_BANDS,   "OUT_OF_BANDS"   },
588 	{ 0, NULL }
589 };
590 
591 #define OFPTFFC_BAD_TABLE    0U
592 #define OFPTFFC_BAD_METADATA 1U
593 #define OFPTFFC_BAD_TYPE     2U
594 #define OFPTFFC_BAD_LEN      3U
595 #define OFPTFFC_BAD_ARGUMENT 4U
596 #define OFPTFFC_EPERM        5U
597 static const struct tok ofptffc_str[] = {
598 	{ OFPTFFC_BAD_TABLE,    "BAD_TABLE"    },
599 	{ OFPTFFC_BAD_METADATA, "BAD_METADATA" },
600 	{ OFPTFFC_BAD_TYPE,     "BAD_TYPE"     },
601 	{ OFPTFFC_BAD_LEN,      "BAD_LEN"      },
602 	{ OFPTFFC_BAD_ARGUMENT, "BAD_ARGUMENT" },
603 	{ OFPTFFC_EPERM,        "EPERM"        },
604 	{ 0, NULL }
605 };
606 
607 static const struct uint_tokary of13_ofpet2tokary[] = {
608 	{ OFPET_HELLO_FAILED,          ofphfc_str  },
609 	{ OFPET_BAD_REQUEST,           ofpbrc_str  },
610 	{ OFPET_BAD_ACTION,            ofpbac_str  },
611 	{ OFPET_BAD_INSTRUCTION,       ofpbic_str  },
612 	{ OFPET_BAD_MATCH,             ofpbmc_str  },
613 	{ OFPET_FLOW_MOD_FAILED,       ofpfmfc_str },
614 	{ OFPET_GROUP_MOD_FAILED,      ofpgmfc_str },
615 	{ OFPET_PORT_MOD_FAILED,       ofppmfc_str },
616 	{ OFPET_TABLE_MOD_FAILED,      ofptmfc_str },
617 	{ OFPET_QUEUE_OP_FAILED,       ofpqofc_str },
618 	{ OFPET_SWITCH_CONFIG_FAILED,  ofpscfc_str },
619 	{ OFPET_ROLE_REQUEST_FAILED,   ofprrfc_str },
620 	{ OFPET_METER_MOD_FAILED,      ofpmmfc_str },
621 	{ OFPET_TABLE_FEATURES_FAILED, ofptffc_str },
622 	{ OFPET_EXPERIMENTER,          NULL        }, /* defines no codes */
623 	/* uint2tokary() does not use array termination. */
624 };
625 
626 /* lengths (fixed or minimal) of particular message types, where not 0 */
627 #define OF_ERROR_MSG_MINLEN                   (12U - OF_HEADER_FIXLEN)
628 #define OF_FEATURES_REPLY_FIXLEN              (32U - OF_HEADER_FIXLEN)
629 #define OF_PORT_MOD_FIXLEN                    (40U - OF_HEADER_FIXLEN)
630 #define OF_SWITCH_CONFIG_MSG_FIXLEN           (12U - OF_HEADER_FIXLEN)
631 #define OF_TABLE_MOD_FIXLEN                   (16U - OF_HEADER_FIXLEN)
632 #define OF_QUEUE_GET_CONFIG_REQUEST_FIXLEN    (16U - OF_HEADER_FIXLEN)
633 #define OF_ROLE_MSG_FIXLEN                    (24U - OF_HEADER_FIXLEN)
634 #define OF_ASYNC_MSG_FIXLEN                   (32U - OF_HEADER_FIXLEN)
635 #define OF_PORT_STATUS_FIXLEN                 (80U - OF_HEADER_FIXLEN)
636 #define OF_EXPERIMENTER_MSG_MINLEN            (16U - OF_HEADER_FIXLEN)
637 
638 /* lengths (fixed or minimal) of particular protocol structures */
639 #define OF_HELLO_ELEM_MINSIZE                 4U
640 
641 /* miscellaneous constants from [OF13] */
642 #define OFP_MAX_PORT_NAME_LEN                 16U
643 
644 /* [OF13] Section 7.2.1 */
645 static void
646 of13_port_print(netdissect_options *ndo,
647                 const u_char *cp)
648 {
649 	/* port_no */
650 	ND_PRINT("\n\t  port_no %s",
651 		 tok2str(ofpp_str, "%u", GET_BE_U_4(cp)));
652 	cp += 4;
653 	/* pad */
654 	cp += 4;
655 	/* hw_addr */
656 	ND_PRINT(", hw_addr %s", GET_ETHERADDR_STRING(cp));
657 	cp += MAC_ADDR_LEN;
658 	/* pad2 */
659 	cp += 2;
660 	/* name */
661 	ND_PRINT(", name '");
662 	nd_printjnp(ndo, cp, OFP_MAX_PORT_NAME_LEN);
663 	ND_PRINT("'");
664 	cp += OFP_MAX_PORT_NAME_LEN;
665 
666 	if (ndo->ndo_vflag < 2) {
667 		ND_TCHECK_LEN(cp, 32);
668 		return;
669 	}
670 
671 	/* config */
672 	ND_PRINT("\n\t   config 0x%08x", GET_BE_U_4(cp));
673 	of_bitmap_print(ndo, ofppc_bm, GET_BE_U_4(cp), OFPPC_U);
674 	cp += 4;
675 	/* state */
676 	ND_PRINT("\n\t   state 0x%08x", GET_BE_U_4(cp));
677 	of_bitmap_print(ndo, ofpps_bm, GET_BE_U_4(cp), OFPPS_U);
678 	cp += 4;
679 	/* curr */
680 	ND_PRINT("\n\t   curr 0x%08x", GET_BE_U_4(cp));
681 	of_bitmap_print(ndo, ofppf_bm, GET_BE_U_4(cp), OFPPF_U);
682 	cp += 4;
683 	/* advertised */
684 	ND_PRINT("\n\t   advertised 0x%08x", GET_BE_U_4(cp));
685 	of_bitmap_print(ndo, ofppf_bm, GET_BE_U_4(cp), OFPPF_U);
686 	cp += 4;
687 	/* supported */
688 	ND_PRINT("\n\t   supported 0x%08x", GET_BE_U_4(cp));
689 	of_bitmap_print(ndo, ofppf_bm, GET_BE_U_4(cp), OFPPF_U);
690 	cp += 4;
691 	/* peer */
692 	ND_PRINT("\n\t   peer 0x%08x", GET_BE_U_4(cp));
693 	of_bitmap_print(ndo, ofppf_bm, GET_BE_U_4(cp), OFPPF_U);
694 	cp += 4;
695 	/* curr_speed */
696 	ND_PRINT("\n\t   curr_speed %ukbps", GET_BE_U_4(cp));
697 	cp += 4;
698 	/* max_speed */
699 	ND_PRINT("\n\t   max_speed %ukbps", GET_BE_U_4(cp));
700 }
701 
702 /* [OF13] Section 7.3.1 */
703 static void
704 of13_features_reply_print(netdissect_options *ndo,
705                           const u_char *cp, u_int len _U_)
706 {
707 	/* datapath_id */
708 	ND_PRINT("\n\t dpid 0x%016" PRIx64, GET_BE_U_8(cp));
709 	cp += 8;
710 	/* n_buffers */
711 	ND_PRINT(", n_buffers %u", GET_BE_U_4(cp));
712 	cp += 4;
713 	/* n_tables */
714 	ND_PRINT(", n_tables %u", GET_U_1(cp));
715 	cp += 1;
716 	/* auxiliary_id */
717 	ND_PRINT(", auxiliary_id %u", GET_U_1(cp));
718 	cp += 1;
719 	/* pad */
720 	cp += 2;
721 	/* capabilities */
722 	ND_PRINT("\n\t capabilities 0x%08x", GET_BE_U_4(cp));
723 	of_bitmap_print(ndo, ofp_capabilities_bm, GET_BE_U_4(cp), OFPCAP_U);
724 	cp += 4;
725 	/* reserved */
726 	ND_TCHECK_4(cp);
727 }
728 
729 /* [OF13] Section 7.3.2 */
730 static void
731 of13_switch_config_msg_print(netdissect_options *ndo,
732                              const u_char *cp, u_int len _U_)
733 {
734 	/* flags */
735 	ND_PRINT("\n\t flags %s",
736 	         tok2str(ofp_config_str, "invalid (0x%04x)", GET_BE_U_2(cp)));
737 	cp += 2;
738 	/* miss_send_len */
739 	ND_PRINT(", miss_send_len %s",
740 	         tok2str(ofpcml_str, "%u", GET_BE_U_2(cp)));
741 }
742 
743 /* [OF13] Section 7.3.3 */
744 static void
745 of13_table_mod_print(netdissect_options *ndo,
746                      const u_char *cp, u_int len _U_)
747 {
748 	/* table_id */
749 	ND_PRINT("\n\t table_id %s", tok2str(ofptt_str, "%u", GET_U_1(cp)));
750 	cp += 1;
751 	/* pad */
752 	cp += 3;
753 	/* config */
754 	ND_PRINT(", config 0x%08x", GET_BE_U_4(cp));
755 }
756 
757 /* [OF13] Section 7.3.9 */
758 static void
759 of13_role_msg_print(netdissect_options *ndo,
760                     const u_char *cp, u_int len _U_)
761 {
762 	/* role */
763 	ND_PRINT("\n\t role %s",
764 	         tok2str(ofpcr_str, "invalid (0x%08x)", GET_BE_U_4(cp)));
765 	cp += 4;
766 	/* pad */
767 	cp += 4;
768 	/* generation_id */
769 	ND_PRINT(", generation_id 0x%016" PRIx64, GET_BE_U_8(cp));
770 }
771 
772 /* [OF13] Section 7.3.10 */
773 static void
774 of13_async_msg_print(netdissect_options *ndo,
775                     const u_char *cp, u_int len _U_)
776 {
777 	/* packet_in_mask[0] */
778 	ND_PRINT("\n\t packet_in_mask[EM] 0x%08x", GET_BE_U_4(cp));
779 	of_bitmap_print(ndo, async_ofpr_bm, GET_BE_U_4(cp), ASYNC_OFPR_U);
780 	cp += 4;
781 	/* packet_in_mask[1] */
782 	ND_PRINT("\n\t packet_in_mask[S] 0x%08x", GET_BE_U_4(cp));
783 	of_bitmap_print(ndo, async_ofpr_bm, GET_BE_U_4(cp), ASYNC_OFPR_U);
784 	cp += 4;
785 	/* port_status_mask[0] */
786 	ND_PRINT("\n\t port_status_mask[EM] 0x%08x", GET_BE_U_4(cp));
787 	of_bitmap_print(ndo, async_ofppr_bm, GET_BE_U_4(cp), ASYNC_OFPPR_U);
788 	cp += 4;
789 	/* port_status_mask[1] */
790 	ND_PRINT("\n\t port_status_mask[S] 0x%08x", GET_BE_U_4(cp));
791 	of_bitmap_print(ndo, async_ofppr_bm, GET_BE_U_4(cp), ASYNC_OFPPR_U);
792 	cp += 4;
793 	/* flow_removed_mask[0] */
794 	ND_PRINT("\n\t flow_removed_mask[EM] 0x%08x", GET_BE_U_4(cp));
795 	of_bitmap_print(ndo, async_ofppr_bm, GET_BE_U_4(cp), ASYNC_OFPPR_U);
796 	cp += 4;
797 	/* flow_removed_mask[1] */
798 	ND_PRINT("\n\t flow_removed_mask[S] 0x%08x", GET_BE_U_4(cp));
799 	of_bitmap_print(ndo, async_ofppr_bm, GET_BE_U_4(cp), ASYNC_OFPPR_U);
800 }
801 
802 /* [OF13] Section 7.3.4.3 */
803 static void
804 of13_port_mod_print(netdissect_options *ndo,
805                     const u_char *cp, u_int len _U_)
806 {
807 	/* port_no */
808 	ND_PRINT("\n\t port_no %s", tok2str(ofpp_str, "%u", GET_BE_U_4(cp)));
809 	cp += 4;
810 	/* pad */
811 	cp += 4;
812 	/* hw_addr */
813 	ND_PRINT(", hw_addr %s", GET_ETHERADDR_STRING(cp));
814 	cp += MAC_ADDR_LEN;
815 	/* pad2 */
816 	cp += 2;
817 	/* config */
818 	ND_PRINT("\n\t  config 0x%08x", GET_BE_U_4(cp));
819 	of_bitmap_print(ndo, ofppc_bm, GET_BE_U_4(cp), OFPPC_U);
820 	cp += 4;
821 	/* mask */
822 	ND_PRINT("\n\t  mask 0x%08x", GET_BE_U_4(cp));
823 	of_bitmap_print(ndo, ofppc_bm, GET_BE_U_4(cp), OFPPC_U);
824 	cp += 4;
825 	/* advertise */
826 	ND_PRINT("\n\t  advertise 0x%08x", GET_BE_U_4(cp));
827 	of_bitmap_print(ndo, ofppf_bm, GET_BE_U_4(cp), OFPPF_U);
828 	cp += 4;
829 	/* pad3 */
830 	/* Always the last field, check bounds. */
831 	ND_TCHECK_4(cp);
832 }
833 
834 /* [OF13] Section 7.4.3 */
835 static void
836 of13_port_status_print(netdissect_options *ndo,
837                        const u_char *cp, u_int len _U_)
838 {
839 	/* reason */
840 	ND_PRINT("\n\t reason %s",
841 	         tok2str(ofppr_str, "invalid (0x02x)", GET_U_1(cp)));
842 	cp += 1;
843 	/* pad */
844 	cp += 7;
845 	/* desc */
846 	of13_port_print(ndo, cp);
847 }
848 
849 /* [OF13] Section 7.5.1 */
850 static void
851 of13_hello_elements_print(netdissect_options *ndo,
852                           const u_char *cp, u_int len)
853 {
854 	while (len) {
855 		uint16_t type, bmlen;
856 
857 		ND_PRINT("\n\t");
858 		ND_ICHECKMSG_U("remaining length", len, <, OF_HELLO_ELEM_MINSIZE);
859 		/* type */
860 		type = GET_BE_U_2(cp);
861 		OF_FWD(2);
862 		ND_PRINT(" type %s",
863 		         tok2str(ofphet_str, "unknown (0x%04x)", type));
864 		/* length */
865 		bmlen = GET_BE_U_2(cp);
866 		OF_FWD(2);
867 		ND_PRINT(", length %u", bmlen);
868 		/* cp is OF_HELLO_ELEM_MINSIZE bytes in */
869 		ND_ICHECKMSG_U("bitmap length", bmlen, <, OF_HELLO_ELEM_MINSIZE);
870 		ND_ICHECKMSG_U("bitmap length", bmlen, >, OF_HELLO_ELEM_MINSIZE + len);
871 		switch (type) {
872 		case OFPHET_VERSIONBITMAP:
873 			/*
874 			 * The specification obviously overprovisions the space
875 			 * for version bitmaps in this element ("ofp versions
876 			 * 32 to 63 are encoded in the second bitmap and so
877 			 * on"). Keep this code simple for now and recognize
878 			 * only a single bitmap with no padding.
879 			 */
880 			if (bmlen == OF_HELLO_ELEM_MINSIZE + 4) {
881 				uint32_t bitmap = GET_BE_U_4(cp);
882 				ND_PRINT(", bitmap 0x%08x", bitmap);
883 				of_bitmap_print(ndo, ofverbm_str, bitmap,
884 				                OF_BIT_VER_U);
885 			} else {
886 				ND_PRINT(" (bogus)");
887 				ND_TCHECK_LEN(cp, bmlen - OF_HELLO_ELEM_MINSIZE);
888 			}
889 			break;
890 		default:
891 			ND_TCHECK_LEN(cp, bmlen - OF_HELLO_ELEM_MINSIZE);
892 		}
893 		OF_FWD(bmlen - OF_HELLO_ELEM_MINSIZE);
894 	}
895 	return;
896 
897 invalid:
898 	nd_print_invalid(ndo);
899 	ND_TCHECK_LEN(cp, len);
900 }
901 
902 /* [OF13] Section 7.5.4 */
903 static void
904 of13_experimenter_message_print(netdissect_options *ndo,
905                                 const u_char *cp, u_int len)
906 {
907 	uint32_t experimenter;
908 
909 	/* experimenter */
910 	experimenter = GET_BE_U_4(cp);
911 	OF_FWD(4);
912 	ND_PRINT("\n\t experimenter 0x%08x (%s)", experimenter,
913 	         of_vendor_name(experimenter));
914 	/* exp_type */
915 	ND_PRINT(", exp_type 0x%08x", GET_BE_U_4(cp));
916 	OF_FWD(4);
917 	/* data */
918 	of_data_print(ndo, cp, len);
919 }
920 
921 /* [OF13] Section 7.3.6 */
922 static void
923 of13_queue_get_config_request_print(netdissect_options *ndo,
924                                     const u_char *cp, u_int len _U_)
925 {
926 	/* port */
927 	ND_PRINT("\n\t port %s", tok2str(ofpp_str, "%u", GET_BE_U_4(cp)));
928 	cp += 4;
929 	/* pad */
930 	/* Always the last field, check bounds. */
931 	ND_TCHECK_4(cp);
932 }
933 
934 /* [OF13] Section 7.4.4 */
935 static void
936 of13_error_print(netdissect_options *ndo,
937                  const u_char *cp, u_int len)
938 {
939 	uint16_t type, code;
940 	const struct tok *code_str;
941 
942 	/* type */
943 	type = GET_BE_U_2(cp);
944 	OF_FWD(2);
945 	ND_PRINT("\n\t type %s", tok2str(ofpet_str, "invalid (0x%04x)", type));
946 	/* code */
947 	code = GET_BE_U_2(cp);
948 	OF_FWD(2);
949 	code_str = uint2tokary(of13_ofpet2tokary, type);
950 	if (code_str != NULL)
951 		ND_PRINT(", code %s",
952 		         tok2str(code_str, "invalid (0x%04x)", code));
953 	else
954 		ND_PRINT(", code invalid (0x%04x)", code);
955 	/* data */
956 	of_data_print(ndo, cp, len);
957 }
958 
959 static const struct of_msgtypeinfo of13_msgtypeinfo[OFPT_MAX + 1] = {
960 	/*
961 	 * [OF13] Section 7.5.1
962 	 * n * variable-size data units.
963 	 */
964 	{
965 		"HELLO",                    of13_hello_elements_print,
966 		REQ_MINLEN,                 0
967 	},
968 	/*
969 	 * [OF13] Section 7.4.4
970 	 * A fixed-size message body and variable-size data.
971 	 */
972 	{
973 		"ERROR",                    of13_error_print,
974 		REQ_MINLEN,                 OF_ERROR_MSG_MINLEN
975 	},
976 	/*
977 	 * [OF13] Section 7.5.2
978 	 * Variable-size data.
979 	 */
980 	{
981 		"ECHO_REQUEST",             of_data_print,
982 		REQ_MINLEN,                 0
983 	},
984 	/*
985 	 * [OF13] Section 7.5.3
986 	 * Variable-size data.
987 	 */
988 	{
989 		"ECHO_REPLY",               of_data_print,
990 		REQ_MINLEN,                 0
991 	},
992 	/*
993 	 * [OF13] Section 7.5.4
994 	 * A fixed-size message body and variable-size data.
995 	 */
996 	{
997 		"EXPERIMENTER",             of13_experimenter_message_print,
998 		REQ_MINLEN,                 OF_EXPERIMENTER_MSG_MINLEN
999 	},
1000 	/*
1001 	 * [OF13] Section 7.3.1
1002 	 * No message body.
1003 	 */
1004 	{
1005 		"FEATURES_REQUEST",         NULL,
1006 		REQ_FIXLEN,                 0
1007 	},
1008 	/*
1009 	 * [OF13] Section 7.3.1
1010 	 * A fixed-size message body.
1011 	 */
1012 	{
1013 		"FEATURES_REPLY",           of13_features_reply_print,
1014 		REQ_FIXLEN,                 OF_FEATURES_REPLY_FIXLEN
1015 	},
1016 	/*
1017 	 * [OF13] Section 7.3.2
1018 	 * No message body.
1019 	 */
1020 	{
1021 		"GET_CONFIG_REQUEST",       NULL,
1022 		REQ_FIXLEN,                 0
1023 	},
1024 	/*
1025 	 * [OF13] Section 7.3.2
1026 	 * A fixed-size message body.
1027 	 */
1028 	{
1029 		"GET_CONFIG_REPLY",         of13_switch_config_msg_print,
1030 		REQ_FIXLEN,                 OF_SWITCH_CONFIG_MSG_FIXLEN
1031 	},
1032 	/*
1033 	 * [OF13] Section 7.3.2
1034 	 * A fixed-size message body.
1035 	 */
1036 	{
1037 		"SET_CONFIG",               of13_switch_config_msg_print,
1038 		REQ_FIXLEN,                 OF_SWITCH_CONFIG_MSG_FIXLEN
1039 	},
1040 	/*
1041 	 * [OF13] Section 7.4.1
1042 	 * (to be done)
1043 	 */
1044 	{
1045 		"PACKET_IN",                NULL,
1046 		REQ_NONE,                   0
1047 	},
1048 	/*
1049 	 * [OF13] Section 7.4.2
1050 	 * (to be done)
1051 	 */
1052 	{
1053 		"FLOW_REMOVED",             NULL,
1054 		REQ_NONE,                   0
1055 	},
1056 	/*
1057 	 * [OF13] Section 7.4.3
1058 	 * A fixed-size message body.
1059 	 */
1060 	{
1061 		"PORT_STATUS",              of13_port_status_print,
1062 		REQ_FIXLEN,                 OF_PORT_STATUS_FIXLEN
1063 	},
1064 	/*
1065 	 * [OF13] Section 7.3.7
1066 	 * (to be done)
1067 	 */
1068 	{
1069 		"PACKET_OUT",               NULL,
1070 		REQ_NONE,                   0
1071 	},
1072 	/*
1073 	 * [OF13] Section 7.3.4.1
1074 	 * (to be done)
1075 	 */
1076 	{
1077 		"FLOW_MOD",                 NULL,
1078 		REQ_NONE,                   0
1079 	},
1080 	/*
1081 	 * [OF13] Section 7.3.4.2
1082 	 * (to be done)
1083 	 */
1084 	{
1085 		"GROUP_MOD",                NULL,
1086 		REQ_NONE,                   0
1087 	},
1088 	/*
1089 	 * [OF13] Section 7.3.4.3
1090 	 * A fixed-size message body.
1091 	 */
1092 	{
1093 		"PORT_MOD",                 of13_port_mod_print,
1094 		REQ_FIXLEN,                 OF_PORT_MOD_FIXLEN
1095 	},
1096 	/*
1097 	 * [OF13] Section 7.3.3
1098 	 * A fixed-size message body.
1099 	 */
1100 	{
1101 		"TABLE_MOD",                of13_table_mod_print,
1102 		REQ_FIXLEN,                 OF_TABLE_MOD_FIXLEN
1103 	},
1104 	/*
1105 	 * [OF13] Section 7.3.5
1106 	 * (to be done)
1107 	 */
1108 	{
1109 		"MULTIPART_REQUEST",        NULL,
1110 		REQ_NONE,                   0
1111 	},
1112 	/*
1113 	 * [OF13] Section 7.3.5
1114 	 * (to be done)
1115 	 */
1116 	{
1117 		"MULTIPART_REPLY",          NULL,
1118 		REQ_NONE,                   0
1119 	},
1120 	/*
1121 	 * [OF13] Section 7.3.8
1122 	 * No message body.
1123 	 */
1124 	{
1125 		"BARRIER_REQUEST",          NULL,
1126 		REQ_FIXLEN,                 0
1127 	},
1128 	/*
1129 	 * [OF13] Section 7.3.8
1130 	 * No message body.
1131 	 */
1132 	{
1133 		"BARRIER_REPLY",            NULL,
1134 		REQ_FIXLEN,                 0
1135 	},
1136 	/*
1137 	 * [OF13] Section 7.3.6
1138 	 * A fixed-size message body.
1139 	 */
1140 	{
1141 		"QUEUE_GET_CONFIG_REQUEST", of13_queue_get_config_request_print,
1142 		REQ_FIXLEN,                 OF_QUEUE_GET_CONFIG_REQUEST_FIXLEN
1143 	},
1144 	/*
1145 	 * [OF13] Section 7.3.6
1146 	 * (to be done)
1147 	 */
1148 	{
1149 		"QUEUE_GET_CONFIG_REPLY",   NULL,
1150 		REQ_NONE,                   0
1151 	},
1152 	/*
1153 	 * [OF13] Section 7.3.9
1154 	 * A fixed-size message body.
1155 	 */
1156 	{
1157 		"ROLE_REQUEST",             of13_role_msg_print,
1158 		REQ_FIXLEN,                 OF_ROLE_MSG_FIXLEN
1159 	},
1160 	/*
1161 	 * [OF13] Section 7.3.9
1162 	 * A fixed-size message body.
1163 	 */
1164 	{
1165 		"ROLE_REPLY",               of13_role_msg_print,
1166 		REQ_FIXLEN,                 OF_ROLE_MSG_FIXLEN
1167 	},
1168 	/*
1169 	 * [OF13] Section 7.3.10
1170 	 * No message body.
1171 	 */
1172 	{
1173 		"GET_ASYNC_REQUEST",        NULL,
1174 		REQ_FIXLEN,                 0
1175 	},
1176 	/*
1177 	 * [OF13] Section 7.3.10
1178 	 * A fixed-size message body.
1179 	 */
1180 	{
1181 		"GET_ASYNC_REPLY",          of13_async_msg_print,
1182 		REQ_FIXLEN,                 OF_ASYNC_MSG_FIXLEN
1183 	},
1184 	/*
1185 	 * [OF13] Section 7.3.10
1186 	 * A fixed-size message body.
1187 	 */
1188 	{
1189 		"SET_ASYNC",                of13_async_msg_print,
1190 		REQ_FIXLEN,                 OF_ASYNC_MSG_FIXLEN
1191 	},
1192 	/*
1193 	 * [OF13] Section 7.3.4.4
1194 	 * (to be done)
1195 	 */
1196 	{
1197 		"METER_MOD",                NULL,
1198 		REQ_NONE,                   0
1199 	},
1200 };
1201 
1202 const struct of_msgtypeinfo *
1203 of13_identify_msgtype(const uint8_t type)
1204 {
1205 	return type <= OFPT_MAX ? &of13_msgtypeinfo[type] : NULL;
1206 }
1207