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