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