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