checkAPIs.pl: support for new-style dissectors in check_hf_entries
[metze/wireshark/wip.git] / epan / dissectors / packet-openflow.c
1 /* packet-openflow.c
2  * Routines for OpenFlow dissection
3  * Copyright 2013, Anders Broman <anders.broman@ericsson.com>
4  * Copyright 2013, Zoltan Lajos Kis <zoltan.lajos.kis@ericsson.com>
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  *
12  * Ref https://www.opennetworking.org/sdn-resources/onf-specifications/openflow
13  */
14
15 #include "config.h"
16
17 #include <epan/packet.h>
18 #include <epan/expert.h>
19 #include <epan/prefs.h>
20
21 #include "packet-tcp.h"
22
23 void proto_register_openflow(void);
24 void proto_reg_handoff_openflow(void);
25
26 #define OFP_LEGACY_PORT 6633
27 #define OFP_LEGACY2_PORT 6634
28 #define OFP_IANA_PORT 6653
29 static int g_openflow_port = OFP_IANA_PORT;
30
31 static dissector_handle_t openflow_handle;
32 static dissector_handle_t openflow_v1_handle;
33 static dissector_handle_t openflow_v4_handle;
34 static dissector_handle_t openflow_v5_handle;
35 static dissector_handle_t openflow_v6_handle;
36
37 /* Initialize the protocol and registered fields */
38 static int proto_openflow = -1;
39 static int hf_openflow_version = -1;
40
41 static expert_field ei_openflow_version = EI_INIT;
42
43 static gboolean openflow_desegment = TRUE;
44
45 #define OFP_VERSION_1_0 1
46 #define OFP_VERSION_1_1 2
47 #define OFP_VERSION_1_2 3
48 #define OFP_VERSION_1_3 4
49 #define OFP_VERSION_1_4 5
50 #define OFP_VERSION_1_5 6
51
52 static const value_string openflow_version_values[] = {
53     { OFP_VERSION_1_0, "1.0" },
54     { OFP_VERSION_1_1, "1.1" },
55     { OFP_VERSION_1_2, "1.2" },
56     { OFP_VERSION_1_3, "1.3" },
57     { OFP_VERSION_1_4, "1.4" },
58     { OFP_VERSION_1_5, "1.5" },
59     { 0, NULL }
60 };
61
62 static guint
63 get_openflow_pdu_length(packet_info *pinfo _U_, tvbuff_t *tvb,
64                         int offset, void *data _U_)
65 {
66     return tvb_get_ntohs(tvb, offset + 2);
67 }
68
69 static int
70 dissect_openflow_tcp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
71 {
72     guint offset = 0;
73     guint8 version;
74     proto_item* ti;
75
76     version = tvb_get_guint8(tvb, 0);
77     /* Set the Protocol column to the constant string of openflow */
78     col_set_str(pinfo->cinfo, COL_PROTOCOL, "OpenFlow");
79     col_clear(pinfo->cinfo,COL_INFO);
80
81     switch(version){
82     case OFP_VERSION_1_0:
83         call_dissector(openflow_v1_handle, tvb, pinfo, tree);
84         break;
85     case OFP_VERSION_1_3:
86         call_dissector(openflow_v4_handle, tvb, pinfo, tree);
87         break;
88     case OFP_VERSION_1_4:
89         call_dissector(openflow_v5_handle, tvb, pinfo, tree);
90         break;
91     case OFP_VERSION_1_5:
92         call_dissector(openflow_v6_handle, tvb, pinfo, tree);
93         break;
94     default:
95         ti = proto_tree_add_item(tree, hf_openflow_version, tvb, offset, 1, ENC_BIG_ENDIAN);
96         expert_add_info(pinfo, ti, &ei_openflow_version);
97         break;
98     }
99     return tvb_reported_length(tvb);
100 }
101
102 #define OFP_HEADER_LEN  8
103 static int
104 dissect_openflow(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
105 {
106     tcp_dissect_pdus(tvb, pinfo, tree, openflow_desegment, OFP_HEADER_LEN,
107                      get_openflow_pdu_length, dissect_openflow_tcp_pdu, data);
108     return tvb_captured_length(tvb);
109 }
110
111 static gboolean
112 dissect_openflow_heur(tvbuff_t *tvb, packet_info *pinfo,
113                      proto_tree *tree, void *data)
114 {
115     conversation_t *conversation = NULL;
116
117     if ((pinfo->destport != OFP_LEGACY_PORT) &&
118         (pinfo->destport != OFP_LEGACY2_PORT) &&
119         (pinfo->destport != OFP_IANA_PORT) &&
120         (pinfo->destport != (guint32)g_openflow_port)) {
121         return FALSE;
122     }
123
124     conversation = find_or_create_conversation(pinfo);
125     conversation_set_dissector(conversation, openflow_handle);
126
127     dissect_openflow(tvb, pinfo, tree, data);
128     return TRUE;
129 }
130
131 static void
132 apply_openflow_prefs(void)
133 {
134     /* Openflow uses the port preference for heuristics */
135     g_openflow_port = prefs_get_uint_value("openflow", "tcp.port");
136 }
137
138 /*
139  * Register the protocol with Wireshark.
140  */
141 void
142 proto_register_openflow(void)
143 {
144     static hf_register_info hf[] = {
145         { &hf_openflow_version,
146             { "Version", "openflow.version",
147                FT_UINT8, BASE_HEX, VALS(openflow_version_values), 0x7f,
148                NULL, HFILL }
149         }
150     };
151
152     static ei_register_info ei[] = {
153         { &ei_openflow_version, { "openflow.version.unknown", PI_UNDECODED, PI_WARN, "Unsupported version not dissected", EXPFILL }},
154     };
155
156     module_t *openflow_module;
157     expert_module_t* expert_openflow;
158
159     /* Register the protocol name and description */
160     proto_openflow = proto_register_protocol("OpenFlow",
161             "OpenFlow", "openflow");
162
163     openflow_handle = register_dissector("openflow", dissect_openflow, proto_openflow);
164
165     /* Required function calls to register the header fields and subtrees */
166     proto_register_field_array(proto_openflow, hf, array_length(hf));
167     expert_openflow = expert_register_protocol(proto_openflow);
168     expert_register_field_array(expert_openflow, ei, array_length(ei));
169
170     openflow_module = prefs_register_protocol(proto_openflow, apply_openflow_prefs);
171
172     /* Register heuristic preference */
173     prefs_register_obsolete_preference(openflow_module, "heuristic");
174
175     /* Register desegment preference */
176     prefs_register_bool_preference(openflow_module, "desegment",
177                                   "Reassemble OpenFlow messages spanning multiple TCP segments",
178                                   "Whether the OpenFlow dissector should reassemble messages spanning multiple TCP segments."
179                                   " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
180                                   &openflow_desegment);
181 }
182
183 void
184 proto_reg_handoff_openflow(void)
185 {
186     heur_dissector_add("tcp", dissect_openflow_heur, "OpenFlow over TCP", "openflow_tcp", proto_openflow, HEURISTIC_ENABLE);
187
188     dissector_add_uint_with_preference("tcp.port", OFP_IANA_PORT, openflow_handle);
189
190     openflow_v1_handle = find_dissector_add_dependency("openflow_v1", proto_openflow);
191     openflow_v4_handle = find_dissector_add_dependency("openflow_v4", proto_openflow);
192     openflow_v5_handle = find_dissector_add_dependency("openflow_v5", proto_openflow);
193     openflow_v6_handle = find_dissector_add_dependency("openflow_v6", proto_openflow);
194 }
195
196 /*
197  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
198  *
199  * Local variables:
200  * c-basic-offset: 4
201  * tab-width: 8
202  * indent-tabs-mode: nil
203  * End:
204  *
205  * vi: set shiftwidth=4 tabstop=8 expandtab:
206  * :indentSize=4:tabSize=8:noTabs=true:
207  */