aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-openflow.c
blob: ab0ddf92eee62c35b1dbe2c7ab39ea8047022e47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* packet-openflow.c
 * Routines for OpenFlow dissection
 * Copyright 2013, Anders Broman <anders.broman@ericsson.com>
 * Copyright 2013, Zoltan Lajos Kis <zoltan.lajos.kis@ericsson.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Ref https://www.opennetworking.org/sdn-resources/onf-specifications/openflow
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/expert.h>
#include <epan/prefs.h>

#include "packet-tcp.h"

void proto_register_openflow(void);
void proto_reg_handoff_openflow(void);

#define OFP_LEGACY_PORT 6633
#define OFP_LEGACY2_PORT 6634
#define OFP_IANA_PORT 6653
static int g_openflow_port = OFP_IANA_PORT;
static gboolean openflow_heur_enabled = TRUE;

static dissector_handle_t openflow_handle;
static dissector_handle_t openflow_v1_handle;
static dissector_handle_t openflow_v4_handle;
static dissector_handle_t openflow_v5_handle;

/* Initialize the protocol and registered fields */
static int proto_openflow = -1;
static int hf_openflow_version = -1;

static expert_field ei_openflow_version = EI_INIT;

static gboolean openflow_desegment = TRUE;

#define OFP_VERSION_1_0 1
#define OFP_VERSION_1_1 2
#define OFP_VERSION_1_2 3
#define OFP_VERSION_1_3 4
#define OFP_VERSION_1_4 5

static const value_string openflow_version_values[] = {
    { 0x01, "1.0" },
    { 0x02, "1.1" },
    { 0x03, "1.2" },
    { 0x04, "1.3" },
    { 0x05, "1.4" },
    { 0, NULL }
};

static guint
get_openflow_pdu_length(packet_info *pinfo _U_, tvbuff_t *tvb,
                        int offset, void *data _U_)
{
    return tvb_get_ntohs(tvb, offset + 2);
}

static int
dissect_openflow_tcp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
    guint offset = 0;
    guint8 version;
    proto_item* ti;

    version = tvb_get_guint8(tvb, 0);
    /* Set the Protocol column to the constant string of openflow */
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "OpenFlow");
    col_clear(pinfo->cinfo,COL_INFO);

    switch(version){
    case OFP_VERSION_1_0:
        call_dissector(openflow_v1_handle, tvb, pinfo, tree);
        break;
    case OFP_VERSION_1_3:
        call_dissector(openflow_v4_handle, tvb, pinfo, tree);
        break;
    case OFP_VERSION_1_4:
        call_dissector(openflow_v5_handle, tvb, pinfo, tree);
        break;
    default:
        ti = proto_tree_add_item(tree, hf_openflow_version, tvb, offset, 1, ENC_BIG_ENDIAN);
        expert_add_info(pinfo, ti, &ei_openflow_version);
        break;
    }
    return tvb_reported_length(tvb);
}

#define OFP_HEADER_LEN  8
static int
dissect_openflow(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
    tcp_dissect_pdus(tvb, pinfo, tree, openflow_desegment, OFP_HEADER_LEN,
                     get_openflow_pdu_length, dissect_openflow_tcp_pdu, data);
    return tvb_captured_length(tvb);
}

static gboolean
dissect_openflow_heur(tvbuff_t *tvb, packet_info *pinfo,
                     proto_tree *tree, void *data)
{
    conversation_t *conversation = NULL;

    if (!openflow_heur_enabled) {
        return FALSE;
    }

    if ((pinfo->destport != OFP_LEGACY_PORT) &&
        (pinfo->destport != OFP_LEGACY2_PORT) &&
        (pinfo->destport != OFP_IANA_PORT) &&
        (pinfo->destport != (guint32)g_openflow_port)) {
        return FALSE;
    }

    conversation = find_or_create_conversation(pinfo);
    conversation_set_dissector(conversation, openflow_handle);

    dissect_openflow(tvb, pinfo, tree, data);
    return TRUE;
}

/*
 * Register the protocol with Wireshark.
 */
void
proto_register_openflow(void)
{
    static hf_register_info hf[] = {
        { &hf_openflow_version,
            { "Version", "openflow.version",
               FT_UINT8, BASE_HEX, VALS(openflow_version_values), 0x7f,
               NULL, HFILL }
        }
    };

    static ei_register_info ei[] = {
        { &ei_openflow_version, { "openflow.version.unknown", PI_UNDECODED, PI_WARN, "Unsupported version not dissected", EXPFILL }},
    };

    module_t *openflow_module;
    expert_module_t* expert_openflow;

    /* Register the protocol name and description */
    proto_openflow = proto_register_protocol("OpenFlow",
            "OpenFlow", "openflow");

    new_register_dissector("openflow", dissect_openflow, proto_openflow);

    /* Required function calls to register the header fields and subtrees */
    proto_register_field_array(proto_openflow, hf, array_length(hf));
    expert_openflow = expert_register_protocol(proto_openflow);
    expert_register_field_array(expert_openflow, ei, array_length(ei));

    openflow_module = prefs_register_protocol(proto_openflow, proto_reg_handoff_openflow);

    /* Register port preference */
    prefs_register_uint_preference(openflow_module, "tcp.port", "OpenFlow TCP port",
                                   "OpenFlow TCP port (6653 is the IANA assigned port)",
                                   10, &g_openflow_port);

    /* Register heuristic preference */
    prefs_register_bool_preference(openflow_module, "heuristic",
                                   "Try to decode OpenFlow on other common ports",
                                   "Try to decode OpenFlow on several common "
                                   "ports in addition to the one supplied by "
                                   "user above (6653 is the IANA assigned port).",
                                   &openflow_heur_enabled);

    /* Register desegment preference */
    prefs_register_bool_preference(openflow_module, "desegment",
                                  "Reassemble OpenFlow messages spanning multiple TCP segments",
                                  "Whether the OpenFlow dissector should reassemble messages spanning multiple TCP segments."
                                  " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
                                  &openflow_desegment);
}

void
proto_reg_handoff_openflow(void)
{
    static gboolean initialized = FALSE;
    static int currentPort;

    if (!initialized) {
        openflow_handle = new_create_dissector_handle(dissect_openflow, proto_openflow);
        heur_dissector_add("tcp", dissect_openflow_heur, proto_openflow);
        initialized = TRUE;
    } else {
        dissector_delete_uint("tcp.port", currentPort, openflow_handle);
    }

    currentPort = g_openflow_port;

    dissector_add_uint("tcp.port", currentPort, openflow_handle);

    openflow_v1_handle = find_dissector("openflow_v1");
    openflow_v4_handle = find_dissector("openflow_v4");
    openflow_v5_handle = find_dissector("openflow_v5");
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */