]> The Tcpdump Group git mirrors - tcpdump/blob - print-openflow.c
Add initial bounds check, get rid of union aodv.
[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 #define NETDISSECT_REWORKED
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include <tcpdump-stdinc.h>
39
40 #include "interface.h"
41 #include "extract.h"
42 #include "openflow.h"
43
44 static const char tstr[] = " [|openflow]";
45 static const char cstr[] = " (corrupt)";
46
47 #define OF_VER_1_0 0x01
48
49 static void
50 of_header_print(netdissect_options *ndo, const uint8_t version, const uint8_t type,
51 const uint16_t length, const uint32_t xid) {
52 ND_PRINT((ndo, "\n\tversion unknown (0x%02x), type 0x%02x, length %u, xid 0x%08x",
53 version, type, length, xid));
54 }
55
56 /* Print a single OpenFlow message. */
57 static const u_char *
58 of_header_body_print(netdissect_options *ndo, const u_char *cp, const u_char *ep) {
59 uint8_t version, type;
60 uint16_t length;
61 uint32_t xid;
62
63 if (ep < cp + OF_HEADER_LEN)
64 goto corrupt;
65 /* version */
66 ND_TCHECK2(*cp, 1);
67 version = *cp;
68 cp += 1;
69 /* type */
70 ND_TCHECK2(*cp, 1);
71 type = *cp;
72 cp += 1;
73 /* length */
74 ND_TCHECK2(*cp, 2);
75 length = EXTRACT_16BITS(cp);
76 cp += 2;
77 /* xid */
78 ND_TCHECK2(*cp, 4);
79 xid = EXTRACT_32BITS(cp);
80 cp += 4;
81 /* Message length includes the header length and a message always includes
82 * the basic header. A message length underrun fails decoding of the rest of
83 * the current packet. At the same time, try decoding as much of the current
84 * message as possible even when it does not end within the current TCP
85 * segment. */
86 if (length < OF_HEADER_LEN) {
87 of_header_print(ndo, version, type, length, xid);
88 goto corrupt;
89 }
90 /* Decode known protocol versions further without printing the header (the
91 * type decoding is version-specific. */
92 switch (version) {
93 case OF_VER_1_0:
94 return of10_header_body_print(ndo, cp, ep, type, length, xid);
95 default:
96 of_header_print(ndo, version, type, length, xid);
97 ND_TCHECK2(*cp, length - OF_HEADER_LEN);
98 return cp + length - OF_HEADER_LEN; /* done with current message */
99 }
100
101 corrupt: /* fail current packet */
102 ND_PRINT((ndo, "%s", cstr));
103 ND_TCHECK2(*cp, ep - cp);
104 return ep;
105 trunc:
106 ND_PRINT((ndo, "%s", tstr));
107 return ep;
108 }
109
110 /* Print a TCP segment worth of OpenFlow messages presuming the segment begins
111 * on a message boundary. */
112 void
113 openflow_print(netdissect_options *ndo, const u_char *cp, const u_int len) {
114 const u_char *ep = cp + len;
115
116 ND_PRINT((ndo, ": OpenFlow"));
117 while (cp < ep)
118 cp = of_header_body_print(ndo, cp, ep);
119 }