]> The Tcpdump Group git mirrors - tcpdump/blob - print-openflow.c
Change -z command help text to -z postrotate-command
[tcpdump] / print-openflow.c
1 /*
2 * This module implements printing of the very basic (version-independent)
3 * OpenFlow header and iteration over OpenFlow messages. It is intended for
4 * dispatching of version-specific OpenFlow message decoding.
5 *
6 *
7 * Copyright (c) 2013 The TCPDUMP project
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <netdissect-stdinc.h>
38
39 #include "netdissect.h"
40 #include "extract.h"
41 #include "openflow.h"
42 #include "oui.h"
43
44 static const char tstr[] = " [|openflow]";
45
46 #define OF_VER_1_0 0x01
47
48 const struct tok onf_exp_str[] = {
49 { ONF_EXP_ONF, "ONF Extensions" },
50 { ONF_EXP_BUTE, "Budapest University of Technology and Economics" },
51 { ONF_EXP_NOVIFLOW, "NoviFlow" },
52 { ONF_EXP_L3, "L3+ Extensions, Vendor Neutral" },
53 { ONF_EXP_L4L7, "L4-L7 Extensions" },
54 { ONF_EXP_WMOB, "Wireless and Mobility Extensions" },
55 { ONF_EXP_FABS, "Forwarding Abstractions Extensions" },
56 { ONF_EXP_OTRANS, "Optical Transport Extensions" },
57 { 0, NULL }
58 };
59
60 const char *
61 of_vendor_name(const uint32_t vendor)
62 {
63 const struct tok *table = (vendor & 0xff000000) == 0 ? oui_values : onf_exp_str;
64 return tok2str(table, "unknown", vendor);
65 }
66
67 static void
68 of_header_print(netdissect_options *ndo, const uint8_t version, const uint8_t type,
69 const uint16_t length, const uint32_t xid)
70 {
71 ND_PRINT((ndo, "\n\tversion unknown (0x%02x), type 0x%02x, length %u, xid 0x%08x",
72 version, type, length, xid));
73 }
74
75 /* Print a single OpenFlow message. */
76 static const u_char *
77 of_header_body_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
78 {
79 uint8_t version, type;
80 uint16_t length;
81 uint32_t xid;
82
83 if (ep < cp + OF_HEADER_LEN)
84 goto invalid;
85 /* version */
86 ND_TCHECK2(*cp, 1);
87 version = *cp;
88 cp += 1;
89 /* type */
90 ND_TCHECK2(*cp, 1);
91 type = *cp;
92 cp += 1;
93 /* length */
94 ND_TCHECK2(*cp, 2);
95 length = EXTRACT_16BITS(cp);
96 cp += 2;
97 /* xid */
98 ND_TCHECK2(*cp, 4);
99 xid = EXTRACT_32BITS(cp);
100 cp += 4;
101 /* Message length includes the header length and a message always includes
102 * the basic header. A message length underrun fails decoding of the rest of
103 * the current packet. At the same time, try decoding as much of the current
104 * message as possible even when it does not end within the current TCP
105 * segment. */
106 if (length < OF_HEADER_LEN) {
107 of_header_print(ndo, version, type, length, xid);
108 goto invalid;
109 }
110 /* Decode known protocol versions further without printing the header (the
111 * type decoding is version-specific. */
112 switch (version) {
113 case OF_VER_1_0:
114 return of10_header_body_print(ndo, cp, ep, type, length, xid);
115 default:
116 of_header_print(ndo, version, type, length, xid);
117 ND_TCHECK2(*cp, length - OF_HEADER_LEN);
118 return cp + length - OF_HEADER_LEN; /* done with current message */
119 }
120
121 invalid: /* fail current packet */
122 ND_PRINT((ndo, "%s", istr));
123 ND_TCHECK2(*cp, ep - cp);
124 return ep;
125 trunc:
126 ND_PRINT((ndo, "%s", tstr));
127 return ep;
128 }
129
130 /* Print a TCP segment worth of OpenFlow messages presuming the segment begins
131 * on a message boundary. */
132 void
133 openflow_print(netdissect_options *ndo, const u_char *cp, const u_int len)
134 {
135 const u_char *ep = cp + len;
136
137 ND_PRINT((ndo, ": OpenFlow"));
138 while (cp < ep)
139 cp = of_header_body_print(ndo, cp, ep);
140 }