xref: /netbsd-src/external/bsd/tcpdump/dist/print-openflow.c (revision cc576e1d8e4f4078fd4e81238abca9fca216f6ec)
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 #include <sys/cdefs.h>
34 #ifndef lint
35 __RCSID("$NetBSD: print-openflow.c,v 1.2 2017/01/24 23:29:14 christos Exp $");
36 #endif
37 
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41 
42 #include <netdissect-stdinc.h>
43 
44 #include "netdissect.h"
45 #include "extract.h"
46 #include "openflow.h"
47 #include "oui.h"
48 
49 static const char tstr[] = " [|openflow]";
50 
51 #define OF_VER_1_0    0x01
52 
53 const struct tok onf_exp_str[] = {
54 	{ ONF_EXP_ONF,               "ONF Extensions"                                  },
55 	{ ONF_EXP_BUTE,              "Budapest University of Technology and Economics" },
56 	{ ONF_EXP_NOVIFLOW,          "NoviFlow"                                        },
57 	{ ONF_EXP_L3,                "L3+ Extensions, Vendor Neutral"                  },
58 	{ ONF_EXP_L4L7,              "L4-L7 Extensions"                                },
59 	{ ONF_EXP_WMOB,              "Wireless and Mobility Extensions"                },
60 	{ ONF_EXP_FABS,              "Forwarding Abstractions Extensions"              },
61 	{ ONF_EXP_OTRANS,            "Optical Transport Extensions"                    },
62 	{ 0, NULL }
63 };
64 
65 const char *
66 of_vendor_name(const uint32_t vendor)
67 {
68 	const struct tok *table = (vendor & 0xff000000) == 0 ? oui_values : onf_exp_str;
69 	return tok2str(table, "unknown", vendor);
70 }
71 
72 static void
73 of_header_print(netdissect_options *ndo, const uint8_t version, const uint8_t type,
74                       const uint16_t length, const uint32_t xid)
75 {
76 	ND_PRINT((ndo, "\n\tversion unknown (0x%02x), type 0x%02x, length %u, xid 0x%08x",
77 	       version, type, length, xid));
78 }
79 
80 /* Print a single OpenFlow message. */
81 static const u_char *
82 of_header_body_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
83 {
84 	uint8_t version, type;
85 	uint16_t length;
86 	uint32_t xid;
87 
88 	if (ep < cp + OF_HEADER_LEN)
89 		goto invalid;
90 	/* version */
91 	ND_TCHECK2(*cp, 1);
92 	version = *cp;
93 	cp += 1;
94 	/* type */
95 	ND_TCHECK2(*cp, 1);
96 	type = *cp;
97 	cp += 1;
98 	/* length */
99 	ND_TCHECK2(*cp, 2);
100 	length = EXTRACT_16BITS(cp);
101 	cp += 2;
102 	/* xid */
103 	ND_TCHECK2(*cp, 4);
104 	xid = EXTRACT_32BITS(cp);
105 	cp += 4;
106 	/* Message length includes the header length and a message always includes
107 	 * the basic header. A message length underrun fails decoding of the rest of
108 	 * the current packet. At the same time, try decoding as much of the current
109 	 * message as possible even when it does not end within the current TCP
110 	 * segment. */
111 	if (length < OF_HEADER_LEN) {
112 		of_header_print(ndo, version, type, length, xid);
113 		goto invalid;
114 	}
115 	/* Decode known protocol versions further without printing the header (the
116 	 * type decoding is version-specific. */
117 	switch (version) {
118 	case OF_VER_1_0:
119 		return of10_header_body_print(ndo, cp, ep, type, length, xid);
120 	default:
121 		of_header_print(ndo, version, type, length, xid);
122 		ND_TCHECK2(*cp, length - OF_HEADER_LEN);
123 		return cp + length - OF_HEADER_LEN; /* done with current message */
124 	}
125 
126 invalid: /* fail current packet */
127 	ND_PRINT((ndo, "%s", istr));
128 	ND_TCHECK2(*cp, ep - cp);
129 	return ep;
130 trunc:
131 	ND_PRINT((ndo, "%s", tstr));
132 	return ep;
133 }
134 
135 /* Print a TCP segment worth of OpenFlow messages presuming the segment begins
136  * on a message boundary. */
137 void
138 openflow_print(netdissect_options *ndo, const u_char *cp, const u_int len)
139 {
140 	const u_char *ep = cp + len;
141 
142 	ND_PRINT((ndo, ": OpenFlow"));
143 	while (cp < ep)
144 		cp = of_header_body_print(ndo, cp, ep);
145 }
146