blob: a5e25640ae6036e546564cd6255a347b0d68b1ef [file] [log] [blame]
Frédéric Lécaille6d889502017-11-15 14:50:19 +01001/* packet-happp.c
2 * Routines for HAProxy Peers Protocol (HAPPP) dissection
3 * Copyright 2016, Frédéric Lécaille <flecaille@haproxy.com>
4 *
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#include <stdio.h>
Willy Tarreaua1bd1fa2019-03-29 17:26:33 +010025#include <inttypes.h>
Frédéric Lécaille6d889502017-11-15 14:50:19 +010026#include <inttypes.h>
27#include <arpa/inet.h>
28
29#include <config.h>
William Lallemand2be58f72020-04-25 22:03:29 +020030
Frédéric Lécaille6d889502017-11-15 14:50:19 +010031#include <epan/to_str.h>
32#include <epan/packet.h>
33#include <epan/prefs.h>
34#include <epan/conversation.h>
William Lallemand2be58f72020-04-25 22:03:29 +020035#include <epan/strutil.h>
36#include <epan/dissectors/packet-tcp.h>
37#include <epan/tvbuff.h>
38
Frédéric Lécaille9885f4b2021-01-19 14:33:24 +010039#ifndef WITHOUT_WS_VERSION
William Lallemand2be58f72020-04-25 22:03:29 +020040#include <ws_version.h>
Frédéric Lécaille9885f4b2021-01-19 14:33:24 +010041#endif
42
43#ifndef WIRESHARK_VERSION_MAJOR
44#define WIRESHARK_VERSION_MAJOR VERSION_MAJOR
45#endif
46#ifndef WIRESHARK_VERSION_MINOR
47#define WIRESHARK_VERSION_MINOR VERSION_MINOR
48#endif
49#ifndef WIRESHARK_VERSION_MICRO
50#define WIRESHARK_VERSION_MICRO VERSION_MICRO
51#endif
52
53#define HAPP_STR(str) #str
54#define HAPP_XSTR(str) HAPP_STR(str)
William Lallemand2be58f72020-04-25 22:03:29 +020055
56WS_DLL_PUBLIC_DEF const gchar plugin_version[] = "0.0.1";
Frédéric Lécaille9885f4b2021-01-19 14:33:24 +010057WS_DLL_PUBLIC_DEF const gchar plugin_release[] = HAPP_XSTR(WIRESHARK_VERSION_MAJOR.WIRESHARK_VERSION_MINOR);
William Lallemand2be58f72020-04-25 22:03:29 +020058WS_DLL_PUBLIC_DEF const int plugin_want_major = WIRESHARK_VERSION_MAJOR;
59WS_DLL_PUBLIC_DEF const int plugin_want_minor = WIRESHARK_VERSION_MINOR;
60WS_DLL_PUBLIC void plugin_register(void);
61
Frédéric Lécaille6d889502017-11-15 14:50:19 +010062
63#define HAPPP_PROTOCOL "HAProxyS"
64#define HAPPP_MSG_MIN_LEN 2
65
66/* Status messages are the shortest ones (3 digits followed by a LF character) */
67#define STATUS_HANDSHAKE_SUCCEEDED "200"
68#define STATUS_TRY_AGAIN_LATER "300"
69#define STATUS_PROTOCOL_ERROR "501"
70#define STATUS_BAD_VERSION "502"
71#define STATUS_LOCAL_PEER_NAME_MISMATCH "503"
72#define STATUS_REMOTE_PEER_NAME_MISMATCH "504"
73
74#include <stdio.h>
75#include <ctype.h>
76#include <stdarg.h>
77
Frédéric Lécaille6d889502017-11-15 14:50:19 +010078
79#ifdef DEBUG
80static unsigned char dbg_buf[16 << 10];
81
82__attribute__((format (printf, 3, 4)))
83void hexdump(const unsigned char *buf, size_t buflen, const char *title_fmt, ...)
84{
85 size_t i;
86 va_list ap;
87 const unsigned char *p;
88 char str_buf[2 + 1 + 16 + 1 + 1];
89
90 va_start(ap, title_fmt);
91 vfprintf(stderr, title_fmt, ap);
92 va_end(ap);
93
94 p = buf;
95 str_buf[0] = str_buf[1] = ' ';
96 str_buf[2] = '|';
97
98 for (i = 0; i < buflen; i++) {
99 if (!(i & 0xf))
100 fprintf(stderr, "%08X: ", i);
101 fprintf(stderr, " %02x", *p);
102 if (isalnum(*p))
103 str_buf[(i & 0xf) + 3] = *p;
104 else
105 str_buf[(i & 0xf) + 3] = '.';
106 if ((i & 0xf) == 0xf || i == buflen -1) {
107 size_t k;
108
109 for (k = 0; k < (0x10 - (i & 0xf) - 1); k++)
110 fprintf(stderr, " ");
111 str_buf[(i & 0xf) + 4] = '|';
112 str_buf[(i & 0xf) + 5 ] = '\0';
113 fprintf(stderr, "%s\n", str_buf);
114 }
115 p++;
116 }
117}
118
119void hexdump_tvb(tvbuff_t *tvb, const gint offset, size_t len)
120{
121 len = len > sizeof dbg_buf ? sizeof dbg_buf : len;
122 if (tvb_memcpy(tvb, dbg_buf, offset, len)) {
123 hexdump(dbg_buf, len, "tvb buff (%zu bytes):\n", len);
124 } else
125 fprintf(stderr, "tvb buff COPY FAILED\n");
126}
127#endif
128
129/* HAPPP message classes */
130enum {
131 PEER_MSG_CLASS_CONTROL = 0,
132 PEER_MSG_CLASS_ERROR,
133 PEER_MSG_CLASS_STICKTABLE = 0x0a,
134 PEER_MSG_CLASS_RESERVED = 0xff,
135};
136
137enum {
138 CONTROL_CLASS_INDEX,
139 ERROR_CLASS_INDEX,
140 STICK_TABLE_CLASS_INDEX,
141 RESERVED_CLASS_INDEX,
142};
143
144/* Control messages */
145enum {
146 PEER_MSG_CTRL_RESYNCREQ = 0,
147 PEER_MSG_CTRL_RESYNCFINISHED,
148 PEER_MSG_CTRL_RESYNCPARTIAL,
149 PEER_MSG_CTRL_RESYNCCONFIRM,
150};
151
152/* Error messages */
153enum {
154 PEER_MSG_ERR_PROTOCOL = 0,
155 PEER_MSG_ERR_SIZELIMIT,
156};
157
158/* Stick table messages */
159enum {
160 PEER_MSG_STKT_UPDATE = 0x80,
161 PEER_MSG_STKT_INCUPDATE,
162 PEER_MSG_STKT_DEFINE,
163 PEER_MSG_STKT_SWITCH,
164 PEER_MSG_STKT_ACK,
165 PEER_MSG_STKT_UPDATE_TIMED,
166 PEER_MSG_STKT_INCUPDATE_TIMED,
167};
168
169/* This is the different key types of the stick tables.
170 * Same definitions as in HAProxy sources.
171 */
172enum {
173 SMP_T_ANY, /* any type */
174 SMP_T_BOOL, /* boolean */
175 SMP_T_SINT, /* signed 64bits integer type */
176 SMP_T_ADDR, /* ipv4 or ipv6, only used for input type compatibility */
177 SMP_T_IPV4, /* ipv4 type */
178 SMP_T_IPV6, /* ipv6 type */
179 SMP_T_STR, /* char string type */
180 SMP_T_BIN, /* buffer type */
181 SMP_T_METH, /* contain method */
182 SMP_TYPES /* number of types, must always be last */
183};
184
185/* The types of data we can store in a stick table.
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500186 * Same definitions as in HAProxy sources.
Frédéric Lécaille6d889502017-11-15 14:50:19 +0100187 */
188enum {
189 STKT_DT_SERVER_ID, /* the server ID to use with this stream if > 0 */
190 STKT_DT_GPT0, /* General Purpose Flag 0. */
191 STKT_DT_GPC0, /* General Purpose Counter 0 (unsigned 32-bit integer) */
192 STKT_DT_GPC0_RATE, /* General Purpose Counter 0's event rate */
193 STKT_DT_CONN_CNT, /* cumulated number of connections */
194 STKT_DT_CONN_RATE, /* incoming connection rate */
195 STKT_DT_CONN_CUR, /* concurrent number of connections */
196 STKT_DT_SESS_CNT, /* cumulated number of sessions (accepted connections) */
197 STKT_DT_SESS_RATE, /* accepted sessions rate */
198 STKT_DT_HTTP_REQ_CNT, /* cumulated number of incoming HTTP requests */
199 STKT_DT_HTTP_REQ_RATE, /* incoming HTTP request rate */
200 STKT_DT_HTTP_ERR_CNT, /* cumulated number of HTTP requests errors (4xx) */
201 STKT_DT_HTTP_ERR_RATE, /* HTTP request error rate */
202 STKT_DT_BYTES_IN_CNT, /* cumulated bytes count from client to servers */
203 STKT_DT_BYTES_IN_RATE, /* bytes rate from client to servers */
204 STKT_DT_BYTES_OUT_CNT, /* cumulated bytes count from servers to client */
205 STKT_DT_BYTES_OUT_RATE, /* bytes rate from servers to client */
206 STKT_STATIC_DATA_TYPES, /* number of types above */
207};
208
209/* The types of data in stick stored in stick tables.
210 * Same definitions as in HAProxy sources.
211 */
212enum {
213 STD_T_SINT = 0, /* signed int */
214 STD_T_UINT, /* unsigned int */
215 STD_T_ULL, /* unsigned long long */
216 STD_T_FRQP, /* freq_ctr_period structure made of three unsigned int */
217};
218
219/* Prototypes */
220void proto_reg_handoff_happp(void);
221void proto_register_happp(void);
222
223/* Initialize the protocol and registered fields */
224static int proto_happp = -1;
225static int hf_happp_fake = -1;
226static int hf_happp_version = -1;
227static int hf_happp_remotepeerid = -1;
228static int hf_happp_localpeerid = -1;
229static int hf_happp_processpid = -1;
230static int hf_happp_relativepid = -1;
231static int hf_happp_status = -1;
232static int hf_happp_msg = -1;
233static int hf_happp_msg_class = -1;
234static int hf_happp_msg_type = -1;
235static int hf_happp_msg_len = -1;
236static int hf_happp_stkt_def_id = -1;
237static int hf_happp_stkt_def_name_len = -1;
238static int hf_happp_stkt_def_name_value = -1;
239static int hf_happp_stkt_def_key_type = -1;
240static int hf_happp_stkt_def_key_len = -1;
241static int hf_happp_stkt_def_data_types = -1;
242static int hf_happp_stkt_updt_update_id = -1;
243static int hf_happp_stkt_updt_expire = -1;
244static int hf_happp_stkt_updt_key_len = -1;
245static int hf_happp_stkt_updt_key_ipv4_value = -1;
246static int hf_happp_stkt_updt_key_str_value = -1;
247static int hf_happp_stkt_updt_key_int_value = -1;
248static int hf_happp_stkt_updt_key_bytes_value = -1;
249static int hf_happp_stkt_updt_data_server_id = -1;
250static int hf_happp_stkt_updt_data_gpt0 = -1;
251static int hf_happp_stkt_updt_data_gpc0 = -1;
252static int hf_happp_stkt_updt_data_gpc0_rate_curr_tick = -1;
253static int hf_happp_stkt_updt_data_gpc0_rate_curr_ctr = -1;
254static int hf_happp_stkt_updt_data_gpc0_rate_prev_ctr = -1;
255static int hf_happp_stkt_updt_data_conn_cnt = -1;
256static int hf_happp_stkt_updt_data_conn_rate_curr_tick = -1;
257static int hf_happp_stkt_updt_data_conn_rate_curr_ctr = -1;
258static int hf_happp_stkt_updt_data_conn_rate_prev_ctr = -1;
259static int hf_happp_stkt_updt_data_conn_cur = -1;
260static int hf_happp_stkt_updt_data_sess_cnt = -1;
261static int hf_happp_stkt_updt_data_sess_rate_curr_tick = -1;
262static int hf_happp_stkt_updt_data_sess_rate_curr_ctr = -1;
263static int hf_happp_stkt_updt_data_sess_rate_prev_ctr = -1;
264static int hf_happp_stkt_updt_data_http_req_cnt = -1;
265static int hf_happp_stkt_updt_data_http_req_rate_curr_tick = -1;
266static int hf_happp_stkt_updt_data_http_req_rate_curr_ctr = -1;
267static int hf_happp_stkt_updt_data_http_req_rate_prev_ctr= -1;
268static int hf_happp_stkt_updt_data_http_err_cnt = -1;
269static int hf_happp_stkt_updt_data_http_err_rate_curr_tick = -1;
270static int hf_happp_stkt_updt_data_http_err_rate_curr_ctr = -1;
271static int hf_happp_stkt_updt_data_http_err_rate_prev_ctr = -1;
272static int hf_happp_stkt_updt_data_bytes_in_cnt = -1;
273static int hf_happp_stkt_updt_data_bytes_in_rate_curr_tick = -1;
274static int hf_happp_stkt_updt_data_bytes_in_rate_curr_ctr = -1;
275static int hf_happp_stkt_updt_data_bytes_in_rate_prev_ctr = -1;
276static int hf_happp_stkt_updt_data_bytes_out_cnt = -1;
277static int hf_happp_stkt_updt_data_bytes_out_rate_curr_tick = -1;
278static int hf_happp_stkt_updt_data_bytes_out_rate_curr_ctr = -1;
279static int hf_happp_stkt_updt_data_bytes_out_rate_prev_ctr = -1;
280static int hf_happp_stkt_updt_ack_table_id = -1;
281static int hf_happp_stkt_updt_ack_update_id = -1;
282
283struct happp_cv_data_t {
284 /* Same thing for the type of the the stick table keys */
285 uint64_t stkt_key_type;
286
287 /* Same thing for the length of the stick table keys.
288 * Note that this is true only for key types different of SMT_T_STR (strings)
289 * and SMT_T_SINT (signed ints).
290 */
291 uint64_t stkt_key_len;
292
293 /* Same thing for the types of the stick table data */
294 uint64_t stkt_data_types;
295 void *data;
296};
297
298struct hf_stkt_data_type {
299 const char *name;
300 unsigned int type;
301 int *hf_ids[3];
302 size_t hf_ids_len;
303};
304
305struct hf_stkt_data_type hf_stkt_data_types[] = {
306 [STKT_DT_SERVER_ID] = {
307 .name = "server_id",
308 .type = STD_T_SINT,
309 .hf_ids = {
310 &hf_happp_stkt_updt_data_server_id,
311 },
312 .hf_ids_len = 1,
313 },
314 [STKT_DT_GPT0] = {
315 .name = "gpt0",
316 .type = STD_T_UINT,
317 .hf_ids = {
318 &hf_happp_stkt_updt_data_gpt0,
319 },
320 .hf_ids_len = 1,
321 },
322 [STKT_DT_GPC0] = {
323 .name = "gpc0",
324 .type = STD_T_UINT,
325 .hf_ids = {
326 &hf_happp_stkt_updt_data_gpc0,
327 },
328 .hf_ids_len = 1,
329 },
330 [STKT_DT_GPC0_RATE] = {
331 .name = "gpc0_rate",
332 .type = STD_T_FRQP,
333 .hf_ids = {
334 &hf_happp_stkt_updt_data_gpc0_rate_curr_tick,
335 &hf_happp_stkt_updt_data_gpc0_rate_curr_ctr,
336 &hf_happp_stkt_updt_data_gpc0_rate_prev_ctr,
337 },
338 .hf_ids_len = 3,
339 },
340 [STKT_DT_CONN_CNT] = {
341 .name = "conn_cnt",
342 .type = STD_T_UINT,
343 .hf_ids = {
344 &hf_happp_stkt_updt_data_conn_cnt,
345 },
346 .hf_ids_len = 1,
347 },
348 [STKT_DT_CONN_RATE] = {
349 .name = "conn_rate",
350 .type = STD_T_FRQP,
351 .hf_ids = {
352 &hf_happp_stkt_updt_data_conn_rate_curr_tick,
353 &hf_happp_stkt_updt_data_conn_rate_curr_ctr,
354 &hf_happp_stkt_updt_data_conn_rate_prev_ctr,
355 },
356 .hf_ids_len = 3,
357 },
358 [STKT_DT_CONN_CUR] = {
359 .name = "conn_cur",
360 .type = STD_T_UINT,
361 .hf_ids = {
362 &hf_happp_stkt_updt_data_conn_cur,
363 },
364 .hf_ids_len = 1,
365 },
366 [STKT_DT_SESS_CNT] = {
367 .name = "sess_cnt",
368 .type = STD_T_UINT,
369 .hf_ids = {
370 &hf_happp_stkt_updt_data_sess_cnt,
371 },
372 .hf_ids_len = 1,
373 },
374 [STKT_DT_SESS_RATE] = {
375 .name = "sess_rate",
376 .type = STD_T_FRQP,
377 .hf_ids = {
378 &hf_happp_stkt_updt_data_sess_rate_curr_tick,
379 &hf_happp_stkt_updt_data_sess_rate_curr_ctr,
380 &hf_happp_stkt_updt_data_sess_rate_prev_ctr,
381 },
382 .hf_ids_len = 3,
383 },
384 [STKT_DT_HTTP_REQ_CNT] = {
385 .name = "http_req_cnt",
386 .type = STD_T_UINT,
387 .hf_ids = {
388 &hf_happp_stkt_updt_data_http_req_cnt,
389 },
390 .hf_ids_len = 1,
391 },
392 [STKT_DT_HTTP_REQ_RATE] = {
393 .name = "http_req_rate",
394 .type = STD_T_FRQP,
395 .hf_ids = {
396 &hf_happp_stkt_updt_data_http_req_rate_curr_tick,
397 &hf_happp_stkt_updt_data_http_req_rate_curr_ctr,
398 &hf_happp_stkt_updt_data_http_req_rate_prev_ctr,
399 },
400 .hf_ids_len = 3,
401 },
402 [STKT_DT_HTTP_ERR_CNT] = {
403 .name = "http_err_cnt",
404 .type = STD_T_UINT,
405 .hf_ids = {
406 &hf_happp_stkt_updt_data_http_err_cnt,
407 },
408 .hf_ids_len = 1,
409 },
410 [STKT_DT_HTTP_ERR_RATE] = {
411 .name = "http_err_rate",
412 .type = STD_T_FRQP,
413 .hf_ids = {
414 &hf_happp_stkt_updt_data_http_err_rate_curr_tick,
415 &hf_happp_stkt_updt_data_http_err_rate_curr_ctr,
416 &hf_happp_stkt_updt_data_http_err_rate_prev_ctr,
417 },
418 .hf_ids_len = 3,
419 },
420 [STKT_DT_BYTES_IN_CNT] = {
421 .name = "bytes_in_cnt",
422 .type = STD_T_ULL,
423 .hf_ids = {
424 &hf_happp_stkt_updt_data_bytes_in_cnt,
425 },
426 .hf_ids_len = 1,
427 },
428 [STKT_DT_BYTES_IN_RATE] = {
429 .name = "bytes_in_rate",
430 .type = STD_T_FRQP,
431 .hf_ids = {
432 &hf_happp_stkt_updt_data_bytes_in_rate_curr_tick,
433 &hf_happp_stkt_updt_data_bytes_in_rate_curr_ctr,
434 &hf_happp_stkt_updt_data_bytes_in_rate_prev_ctr,
435 },
436 .hf_ids_len = 3,
437 },
438 [STKT_DT_BYTES_OUT_CNT] = {
439 .name = "bytes_out_cnt",
440 .type = STD_T_ULL,
441 .hf_ids = {
442 &hf_happp_stkt_updt_data_bytes_out_cnt,
443 },
444 .hf_ids_len = 1,
445 },
446 [STKT_DT_BYTES_OUT_RATE] = {
447 .name = "bytes_out_rate",
448 .type = STD_T_FRQP,
449 .hf_ids = {
450 &hf_happp_stkt_updt_data_bytes_out_rate_curr_tick,
451 &hf_happp_stkt_updt_data_bytes_out_rate_curr_ctr,
452 &hf_happp_stkt_updt_data_bytes_out_rate_prev_ctr,
453 },
454 .hf_ids_len = 3,
455 },
456};
457
458
459/* Initialize the subtree pointers */
460static gint ett_happp = -1;
461static gint ett_happp_msg = -1;
462
463static dissector_handle_t happp_tcp_handle;
464
465static const char *control_msg_type_str_from_byte(guint8 c);
466static const char *error_msg_type_str_from_byte(guint8 c);
467static const char *stkt_msg_type_str_from_byte(guint8 c);
468
469struct class_def_t {
470 const char *class_str;
471 const char *col_info_str;
472 const char *(*msg_type_str_func)(guint8 c);
473 unsigned int count;
474};
475
476static struct class_def_t class_def_tab[] = {
477 [CONTROL_CLASS_INDEX] = {
478 .class_str = "Control Class Message",
479 .col_info_str = "Ctl",
480 .msg_type_str_func = control_msg_type_str_from_byte,
481 },
482 [ERROR_CLASS_INDEX] = {
483 .class_str = "Error Class Message",
484 .col_info_str = "Err",
485 .msg_type_str_func = error_msg_type_str_from_byte,
486 },
487 [STICK_TABLE_CLASS_INDEX] = {
488 .class_str = "Stick Table Class Message",
489 .col_info_str = "Stkt",
490 .msg_type_str_func = stkt_msg_type_str_from_byte,
491 },
492 [RESERVED_CLASS_INDEX] = {
493 .class_str = "Reserved Class Message",
494 .col_info_str = "Res",
495 }
496};
497
498static int control_class_index_from_byte(guint8 c)
499{
500 switch (c) {
501 case PEER_MSG_CLASS_CONTROL:
502 return CONTROL_CLASS_INDEX;
503 case PEER_MSG_CLASS_ERROR:
504 return ERROR_CLASS_INDEX;
505 case PEER_MSG_CLASS_STICKTABLE:
506 return STICK_TABLE_CLASS_INDEX;
507 case PEER_MSG_CLASS_RESERVED:
508 return RESERVED_CLASS_INDEX;
509 default:
510 return -1;
511 };
512}
513
514static const char *class_str_from_byte(guint8 c)
515{
516 int class_idx;
517
518 class_idx = control_class_index_from_byte(c);
519 if (class_idx == -1)
520 return "N/A";
521
522 return class_def_tab[class_idx].class_str;
523}
524
525static const char *control_msg_type_str_from_byte(guint8 c)
526{
527 switch (c) {
528 case PEER_MSG_CTRL_RESYNCREQ:
529 return "resync. request";
530 case PEER_MSG_CTRL_RESYNCFINISHED:
531 return "resync. finished";
532 case PEER_MSG_CTRL_RESYNCPARTIAL:
533 return "resync. partial";
534 case PEER_MSG_CTRL_RESYNCCONFIRM:
535 return "resync. confirm";
536 default:
537 return "Unknown";
538 }
539}
540
541static const char *stkt_msg_type_str_from_byte(guint8 c)
542{
543 switch (c) {
544 case PEER_MSG_STKT_UPDATE:
545 return "update";
546 case PEER_MSG_STKT_INCUPDATE:
547 return "inc. update";
548 case PEER_MSG_STKT_DEFINE:
549 return "definition";
550 case PEER_MSG_STKT_SWITCH:
551 return "switch";
552 case PEER_MSG_STKT_ACK:
553 return "ack";
554 case PEER_MSG_STKT_UPDATE_TIMED:
555 return "update (with expiration)";
556 case PEER_MSG_STKT_INCUPDATE_TIMED:
557 return "inc. update (with expiration)";
558 default:
559 return "Unknown";
560 }
561}
562
563static const char *error_msg_type_str_from_byte(guint8 c)
564{
565 switch (c) {
566 case PEER_MSG_ERR_PROTOCOL:
567 return "protocol error";
568 case PEER_MSG_ERR_SIZELIMIT:
569 return "limit size error";
570 default:
571 return "Unknown";
572 }
573}
574
575#define MAX_ENC_LEN 10
576static uint64_t intdecode(unsigned char **str, size_t len) {
577 int i = 0;
578 uint64_t ret;
579
580 if (len < 1 || len > MAX_ENC_LEN) {
581 *str = NULL;
582 return 0;
583 }
584
585 ret = *(*str)++;
586 len--;
587 if ((ret & 0xf0) != 0xf0 || !len)
588 return ret;
589
590 do {
591 /* As shifting value may be greater than 8 (size of **str in bits),
592 * uint64_t cast is required.
593 */
594 ret += (uint64_t)**str << (4 + 7 * i++);
595 } while (len-- && (*(*str)++ & 0x80) == 0x80);
596
597 return ret;
598}
599
600static int dissect_happp_handshake_pdu(tvbuff_t *tvb, packet_info *pinfo,
601 proto_tree *happp_tree)
602{
603 int line_len, token_len;
604 gint offset = 0, next_offset;
605 const guchar *line, *line_end, *next_token;
606 size_t protocol_strlen;
607
608 line_len = tvb_find_line_end(tvb, offset, -1, &next_offset, TRUE);
609 /* XXX TO DO */
610 if (line_len == -1)
611 return -1;
612
613 protocol_strlen = strlen(HAPPP_PROTOCOL);
614
615 line = tvb_get_ptr(tvb, offset, line_len);
616 line_end = line + (next_offset - offset);
617 /* The line must contain at least HAPPP_PROTOCOL string followed by a space,
618 * then version string (at least one character) and a '\n' character.
619 */
620 if (line_len >= (int)protocol_strlen + 3 &&
621 !tvb_strncaseeql(tvb, 0, HAPPP_PROTOCOL, protocol_strlen)) {
622 /* This is an Hello message */
623 col_set_str(pinfo->cinfo, COL_INFO, "Hello message");
624
625 token_len = get_token_len(line + protocol_strlen + 1, line_end, &next_token);
626 proto_tree_add_item(happp_tree, hf_happp_version, tvb,
627 offset + protocol_strlen + 1, token_len,
628 ENC_ASCII | ENC_NA);
629
630 offset = next_offset;
631 line_len = tvb_find_line_end(tvb, offset, -1, &next_offset, TRUE);
632 /* XXX TO DO */
633 if (line_len == -1)
634 return -1;
635
636 line = tvb_get_ptr(tvb, offset, line_len);
637 line_end = line + (next_offset - offset);
638 /* Get next token: remotepeerid */
639 token_len = get_token_len(line, line_end, &next_token);
640 if (!token_len)
641 return -1;
642
643 proto_tree_add_item(happp_tree, hf_happp_remotepeerid, tvb, offset,
644 token_len, ENC_ASCII | ENC_NA);
645
646 /* Retrieve next line */
647 offset = next_offset;
648 line_len = tvb_find_line_end(tvb, offset, -1, &next_offset, TRUE);
649 /* XXX TO DO */
650 if (line_len == -1)
651 return -1;
652
653 line = tvb_get_ptr(tvb, offset, line_len);
654 line_end = line + (next_offset - offset);
655 /* Get next token: localpeerid */
656 token_len = get_token_len(line, line_end, &next_token);
657 if (!token_len)
658 return -1;
659
660 proto_tree_add_item(happp_tree, hf_happp_localpeerid, tvb, offset,
661 token_len, ENC_ASCII | ENC_NA);
662 offset += next_token - line;
663 line = next_token;
664
665 /* Get next token: processpid */
666 token_len = get_token_len(line, line_end, &next_token);
667 if (!token_len)
668 return -1;
669
670 proto_tree_add_item(happp_tree, hf_happp_processpid, tvb, offset,
671 token_len, ENC_ASCII | ENC_NA);
672 offset += next_token - line;
673 line = next_token;
674
675 /* Get next token: relativepid */
676 token_len = get_token_len(line, line_end, &next_token);
677 if (!token_len)
678 return -1;
679
680 proto_tree_add_item(happp_tree, hf_happp_relativepid, tvb, offset,
681 token_len, ENC_ASCII | ENC_NA);
682 offset += next_token - line;
683 line = next_token;
684
685 }
686 else if (line_len == 3) {
687 col_set_str(pinfo->cinfo, COL_INFO, "Status message");
688 token_len = get_token_len(line, line_end, &next_token);
689 if (!token_len)
690 return -1;
691
692 proto_tree_add_item(happp_tree, hf_happp_status, tvb, offset,
693 token_len, ENC_ASCII | ENC_NA);
694 }
695
696 return tvb_captured_length(tvb);
697}
698
699/* Reset to zero all statistics counters of class_def_array */
700static void init_class_def_tab(struct class_def_t *class_def_array, size_t size)
701{
702 size_t i;
703
704 for (i = 0; i < size; i++)
705 class_def_array[i].count = 0;
706}
707
708/* Add statistics counting information about HAPPP message classes to
709 * info column (numbers of messages found in an HAPPP PDU by class).
710 */
711static inline void col_info_append_class(packet_info *pinfo, int class_index,
712 int *first_class)
713{
714 if (!class_def_tab[class_index].count)
715 return;
716
717 col_append_fstr(pinfo->cinfo, COL_INFO, "%s%s=%u",
718 *first_class ? "" : " ",
719 class_def_tab[class_index].col_info_str,
720 class_def_tab[class_index].count);
721 class_def_tab[class_index].count = 0;
722 *first_class = 0;
723}
724
725
726static int intdecode_from_tvbuff(tvbuff_t *tvb, uint64_t *dec_val,
727 guint *offset, guint total)
728{
729 unsigned char *p, enc_buf[MAX_ENC_LEN];
730 size_t max_enc_buf_len, left;
731
732 left = total - *offset;
733 max_enc_buf_len = left < sizeof enc_buf ? left : sizeof enc_buf;
734 if (!tvb_memcpy(tvb, enc_buf, *offset, max_enc_buf_len))
735 return -1;
736
737 p = enc_buf;
738 *dec_val = intdecode(&p, max_enc_buf_len);
739 if (!p)
740 return -1;
741
742 *offset += p - enc_buf;
743
744 return 0;
745}
746
747static int add_enc_field_to_happp_tree(int field_id, proto_tree *tree, tvbuff_t *tvb,
748 guint *offset, guint total, uint64_t *val)
749{
750 uint64_t dec_val;
751 size_t dec_val_len;
752 guint saved_offset;
753
754 saved_offset = *offset;
755 if (intdecode_from_tvbuff(tvb, &dec_val, offset, total) < 0)
756 return -1;
757
758 dec_val_len = *offset - saved_offset;
759 proto_tree_add_uint64_format_value(tree, field_id, tvb, saved_offset,
760 dec_val_len, dec_val, "%" PRIu64, dec_val);
761
762 if (val)
763 *val = dec_val;
764
765 return 0;
766}
767
768static int add_int_field_to_happp_tree(int field_id,
769 tvbuff_t *tvb, proto_tree *tree,
770 guint *offset, guint total _U_)
771{
772 uint32_t val;
773
774 if (!tvb_memcpy(tvb, &val, *offset, sizeof val))
775 return -1;
776
777 val = ntohl(val);
778 proto_tree_add_int_format_value(tree, field_id, tvb, *offset,
779 sizeof val, val, "%" PRId32, val);
780 *offset += sizeof val;
781
782 return 0;
783}
784
785static void dissect_happp_stkt_define_msg(tvbuff_t *tvb, packet_info *pinfo _U_,
786 proto_tree *tree, guint offset, guint total)
787{
788 uint64_t dec_val;
789 uint64_t stkt_key_type;
790 uint64_t stkt_key_len;
791 uint64_t stkt_data_types;
792 struct happp_cv_data_t *happp_cv_data;
793 conversation_t *cv;
794
795 if (add_enc_field_to_happp_tree(hf_happp_stkt_def_id, tree,
796 tvb, &offset, total, NULL) < 0 ||
797 add_enc_field_to_happp_tree(hf_happp_stkt_def_name_len, tree,
798 tvb, &offset, total, &dec_val) < 0)
799 return;
800
801 /* Add the stick table name to HAPPP proto tree */
802 proto_tree_add_item(tree, hf_happp_stkt_def_name_value, tvb, offset, dec_val,
803 ENC_ASCII | ENC_NA);
804 offset += dec_val;
805
806 if (add_enc_field_to_happp_tree(hf_happp_stkt_def_key_type, tree,
807 tvb, &offset, total, &stkt_key_type) < 0 ||
808 add_enc_field_to_happp_tree(hf_happp_stkt_def_key_len, tree,
809 tvb, &offset, total, &stkt_key_len) < 0 ||
810 add_enc_field_to_happp_tree(hf_happp_stkt_def_data_types, tree,
811 tvb, &offset, total, &stkt_data_types) < 0)
812 return;
813
814 cv = find_conversation(pinfo->num, &pinfo->src, &pinfo->dst,
815 pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
816 if (!cv)
817 return;
818
819 /*
Joseph Herlantbd0f83f2018-11-09 19:00:24 -0800820 * According to the documentation, it is not our responsibility
Frédéric Lécaille6d889502017-11-15 14:50:19 +0100821 * to free this allocated memory.
822 */
823 happp_cv_data = (struct happp_cv_data_t *)wmem_alloc(wmem_file_scope(),
824 sizeof *happp_cv_data);
825 if (!happp_cv_data)
826 return;
827
828 happp_cv_data->stkt_key_type = stkt_key_type;
829 happp_cv_data->stkt_key_len = stkt_key_len;
830 happp_cv_data->stkt_data_types = stkt_data_types;
831
832 conversation_add_proto_data(cv, proto_happp, happp_cv_data);
833}
834
835static void dissect_happp_stkt_update_msg(tvbuff_t *tvb, packet_info *pinfo _U_,
836 proto_tree *tree, guint offset, guint total,
837 unsigned char msg_type_byte)
838{
839 unsigned int data_type;
840 uint64_t *stkt_key_type;
841 uint64_t *stkt_key_len;
842 struct happp_cv_data_t *happp_cv_data;
843 int has_update_id, has_exp;
844 conversation_t *cv;
845
846 cv = find_conversation(pinfo->num, &pinfo->src, &pinfo->dst,
847 pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
848 if (!cv)
849 return;
850
851 happp_cv_data = (struct happp_cv_data_t *)conversation_get_proto_data(cv, proto_happp);
852 if (!happp_cv_data)
853 return;
854
855 has_update_id = msg_type_byte == PEER_MSG_STKT_UPDATE ||
856 msg_type_byte == PEER_MSG_STKT_UPDATE_TIMED;
857 has_exp = msg_type_byte == PEER_MSG_STKT_UPDATE_TIMED ||
858 msg_type_byte == PEER_MSG_STKT_INCUPDATE_TIMED;
859 /* Add the stick table update ID to HAPPP tree */
860 if (has_update_id &&
861 add_int_field_to_happp_tree(hf_happp_stkt_updt_update_id, tvb, tree,
862 &offset, total) < 0)
863 return;
864
865 if (has_exp &&
866 add_int_field_to_happp_tree(hf_happp_stkt_updt_expire, tvb, tree,
867 &offset, total) < 0)
868 return;
869
870
871 stkt_key_type = &happp_cv_data->stkt_key_type;
872 stkt_key_len = &happp_cv_data->stkt_key_len;
873
874 switch(*stkt_key_type) {
875 case SMP_T_STR:
876 if (add_enc_field_to_happp_tree(hf_happp_stkt_updt_key_len, tree, tvb,
877 &offset, total, stkt_key_len) < 0)
878 return;
879
880 proto_tree_add_item(tree, hf_happp_stkt_updt_key_str_value, tvb,
881 offset, *stkt_key_len, ENC_ASCII | ENC_NA);
882 offset += *stkt_key_len;
883 break;
884 case SMP_T_SINT:
885 if (add_int_field_to_happp_tree(hf_happp_stkt_updt_key_int_value, tvb, tree,
886 &offset, total) < 0)
887 return;
888
889 break;
890 case SMP_T_IPV4:
891 proto_tree_add_ipv4(tree, hf_happp_stkt_updt_key_ipv4_value,
892 tvb, offset, 4, tvb_get_ipv4(tvb, offset));
893 offset += 4;
894 break;
895 default:
896 proto_tree_add_item(tree, hf_happp_stkt_updt_key_bytes_value,
897 tvb, offset, *stkt_key_len, ENC_NA);
898 offset += *stkt_key_len;
899 break;
900 }
901
902 /* Data dissection */
903 for (data_type = 0;
904 data_type < sizeof hf_stkt_data_types / sizeof *hf_stkt_data_types;
905 data_type++) {
906 struct hf_stkt_data_type *hf_stkt_dtype;
907 size_t i;
908
909 if (!(happp_cv_data->stkt_data_types & (1 << data_type)))
910 continue;
911
912 hf_stkt_dtype = &hf_stkt_data_types[data_type];
913
914 for (i = 0; i < hf_stkt_dtype->hf_ids_len; i++)
915 if (add_enc_field_to_happp_tree(*hf_stkt_dtype->hf_ids[i], tree, tvb,
916 &offset, total, NULL) < 0)
917 return;
918 }
919}
920
921static void dissect_happp_stkt_ack_msg(tvbuff_t *tvb, packet_info *pinfo _U_,
922 proto_tree *tree, guint offset, guint total)
923{
924 if (add_enc_field_to_happp_tree(hf_happp_stkt_updt_ack_table_id, tree, tvb,
925 &offset, total, NULL) < 0)
926 return;
927
928 if (add_int_field_to_happp_tree(hf_happp_stkt_updt_ack_update_id, tvb, tree,
929 &offset, total) < 0)
930 return;
931}
932
933static void dissect_happp_stk_msg(tvbuff_t *tvb, packet_info *pinfo _U_,
934 proto_tree *tree, guint8 msg_type_byte,
935 guint offset, guint total)
936{
937 switch (msg_type_byte) {
938 case PEER_MSG_STKT_DEFINE:
939 dissect_happp_stkt_define_msg(tvb, pinfo, tree, offset, total);
940 break;
941 case PEER_MSG_STKT_UPDATE:
942 case PEER_MSG_STKT_INCUPDATE:
943 case PEER_MSG_STKT_UPDATE_TIMED:
944 case PEER_MSG_STKT_INCUPDATE_TIMED:
945 dissect_happp_stkt_update_msg(tvb, pinfo, tree, offset, total, msg_type_byte);
946 break;
947 case PEER_MSG_STKT_ACK:
948 dissect_happp_stkt_ack_msg(tvb, pinfo, tree, offset, total);
949 break;
950 };
951
952}
953
954static void
955dissect_happp_msg(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree,
956 guint8 msg_class_byte, guint8 msg_type_byte,
957 guint *offset, guint total)
958{
959 unsigned char *p, enc_buf[MAX_ENC_LEN];
960 uint64_t dec_msg_len;
961 size_t max_enc_buf_len, left, dec_val_len;
962
963 left = total - *offset;
964 max_enc_buf_len = left < sizeof enc_buf ? left : sizeof enc_buf;
965 if (!tvb_memcpy(tvb, enc_buf, *offset, max_enc_buf_len))
966 return;
967
968 p = enc_buf;
969 dec_msg_len = intdecode(&p, max_enc_buf_len);
970 if (!p)
971 return;
972
973 dec_val_len = p - enc_buf;
974 proto_tree_add_uint64_format_value(tree, hf_happp_msg_len,
975 tvb, *offset, dec_val_len, dec_msg_len,
976 "%" PRIu64, dec_msg_len);
977 *offset += dec_val_len;
978
979 switch (msg_class_byte) {
980 case PEER_MSG_CLASS_STICKTABLE:
981 dissect_happp_stk_msg(tvb, pinfo, tree, msg_type_byte, *offset, total);
982 break;
983 }
984
985 *offset += dec_msg_len;
986}
987
988/* Code to actually dissect the packets */
989static int
990dissect_happp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
991{
992 /* Set up structures needed to add the protocol subtree and manage it */
993 proto_item *item;
994 proto_tree *happp_tree;
995 /* Other misc. local variables. */
996 guint total, offset;
997 int first_message, first_class, curr_class, prev_class;
998 guint8 first_byte;
999 size_t sizeof_class_def_tab;
1000
1001 offset = 0;
1002 first_message = first_class = 1;
1003 total = tvb_reported_length(tvb);
1004
1005 /* create display subtree for the protocol */
1006 item = proto_tree_add_item(tree, proto_happp, tvb, offset, -1, ENC_NA);
1007 happp_tree = proto_item_add_subtree(item, ett_happp);
1008
1009 /* Set the protocol column value */
1010 col_set_str(pinfo->cinfo, COL_PROTOCOL, "happp");
1011
1012 first_byte = (gchar)tvb_get_guint8(tvb, offset);
1013 if (first_byte != PEER_MSG_CLASS_CONTROL &&
1014 first_byte != PEER_MSG_CLASS_ERROR &&
1015 first_byte != PEER_MSG_CLASS_STICKTABLE &&
1016 first_byte != PEER_MSG_CLASS_RESERVED)
1017 return dissect_happp_handshake_pdu(tvb, pinfo, happp_tree);
1018
1019 /* Reset class_def_tab message class counters */
1020 sizeof_class_def_tab = sizeof class_def_tab / sizeof *class_def_tab;
1021 init_class_def_tab(class_def_tab, sizeof_class_def_tab);
1022
1023 prev_class = curr_class = -1;
1024 col_set_str(pinfo->cinfo, COL_INFO, "[");
1025 while (offset < total) {
1026 guint8 msg_class_byte, msg_type_byte;
1027 const char *(*msg_type_str_func)(guint8 c);
1028 struct class_def_t *class_def;
1029
1030 if (first_message) {
1031 msg_class_byte = first_byte;
1032 }
1033 else {
1034 msg_class_byte = tvb_get_guint8(tvb, offset);
1035 }
1036 curr_class = control_class_index_from_byte(msg_class_byte);
1037 if (curr_class == -1)
1038 return -1;
1039
1040 if (first_message) {
1041 prev_class = curr_class;
1042 first_message = 0;
1043 }
1044
1045 class_def = &class_def_tab[curr_class];
1046 class_def->count++;
1047 msg_type_str_func = class_def->msg_type_str_func;
1048
1049 /* Insert a line separator */
1050 proto_tree_add_item(happp_tree, hf_happp_fake, tvb,
1051 offset, 0,
1052 ENC_ASCII | ENC_NA);
1053 proto_tree_add_uint_format_value(happp_tree, hf_happp_msg_class,
1054 tvb, offset++, 1, msg_class_byte,
1055 "%u (%s)", msg_class_byte,
1056 class_str_from_byte(msg_class_byte));
1057 msg_type_byte = tvb_get_guint8(tvb, offset);
1058
1059 /* First byte: message class */
1060 switch (msg_class_byte) {
1061 case PEER_MSG_CLASS_CONTROL:
1062 case PEER_MSG_CLASS_ERROR:
1063 case PEER_MSG_CLASS_STICKTABLE:
1064 /* Second byte: message type in the class */
1065 proto_tree_add_uint_format_value(happp_tree, hf_happp_msg_type,
1066 tvb, offset++, 1, msg_type_byte,
1067 "%u (%s)", msg_type_byte,
1068 msg_type_str_func(msg_type_byte));
1069 break;
1070 case PEER_MSG_CLASS_RESERVED:
1071 col_append_str(pinfo->cinfo, COL_INFO, "NON IMPLEMENTED");
1072 break;
1073 }
1074 if (msg_class_byte >= PEER_MSG_CLASS_STICKTABLE)
1075 dissect_happp_msg(tvb, pinfo, happp_tree,
1076 msg_class_byte, msg_type_byte, &offset, total);
1077
1078 /* Sequentially add counting information to info column about
1079 * number of messages found by class in an HAPPP PDU.
1080 * For instance if an HAPPP PDU contains this sequence of messages:
1081 * 1 Control message - 2 Stick Table messages - 3 Control messages
1082 * column information displays: [Ctl=1 Stkt=2 Ctl=3].
1083 */
1084 if (curr_class != prev_class) {
1085 col_info_append_class(pinfo, prev_class, &first_class);
1086 col_info_append_class(pinfo, curr_class, &first_class);
1087 prev_class = curr_class;
1088 }
1089 else if (offset >= total) {
1090 /* Last message */
1091 col_info_append_class(pinfo, curr_class, &first_class);
1092 }
1093 }
1094 col_append_str(pinfo->cinfo, COL_INFO, "]");
1095
1096 return tvb_captured_length(tvb);
1097}
1098
1099static guint
1100get_happp_msg_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset, void *data _U_)
1101{
1102 guint ret, len, left;
1103 gint next_offset, line_len;
1104 guint8 first_byte;
1105 uint64_t dec_len;
1106 int saved_offset;
1107
1108 /* 0 means there is not enough data to get length. */
1109 ret = 0;
1110
1111 len = tvb_reported_length(tvb);
1112 left = len - offset;
1113 if (left < HAPPP_MSG_MIN_LEN)
1114 goto out;
1115
1116 saved_offset = offset;
1117 first_byte = (gchar)tvb_get_guint8(tvb, offset);
1118 if (first_byte == PEER_MSG_CLASS_CONTROL ||
1119 first_byte == PEER_MSG_CLASS_ERROR ||
1120 first_byte == PEER_MSG_CLASS_RESERVED) {
1121 ret = HAPPP_MSG_MIN_LEN;
1122 } else if (first_byte == PEER_MSG_CLASS_STICKTABLE) {
1123 int soff;
1124
1125 left -= HAPPP_MSG_MIN_LEN;
1126 offset += HAPPP_MSG_MIN_LEN;
1127 soff = offset;
1128 if (intdecode_from_tvbuff(tvb, &dec_len, &offset, len) < 0)
1129 goto out;
1130
1131 left -= offset - soff;
1132 if (left < dec_len)
1133 goto out;
1134
1135 ret = dec_len + offset - saved_offset;
1136 } else {
1137 /* hello message: add line lengths to compute this message length. */
1138 for (;;) {
1139 line_len = tvb_find_line_end(tvb, offset, -1, &next_offset, TRUE);
1140 if (line_len == -1)
1141 break;
1142
1143 ret += line_len + 1;
1144 offset += line_len + 1;
1145 }
1146 }
1147
1148 out:
1149 return ret;
1150 }
1151
1152static int
1153dissect_happp_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
1154{
1155 tcp_dissect_pdus(tvb, pinfo, tree, TRUE,
1156 HAPPP_MSG_MIN_LEN, get_happp_msg_len, dissect_happp_pdu, data);
1157
1158 return tvb_captured_length(tvb);
1159}
1160
1161/* Register the protocol with Wireshark.
1162 *
1163 * This format is require because a script is used to build the C function that
1164 * calls all the protocol registration.
1165 */
1166void
1167proto_register_happp(void)
1168{
1169 /* Setup list of header fields See Section 1.5 of README.dissector for
1170 * details. */
1171 static hf_register_info hf[] = {
1172 {
1173 /* This one is used as separator between HAPPP messages */
1174 &hf_happp_fake,
1175 {
1176 ":-----------------------------------------------", "happp.fake",
1177 FT_STRING, STR_ASCII, NULL, 0, "FAKE", HFILL
1178 }
1179 },
1180 {
1181 &hf_happp_version,
1182 {
1183 "version", "happp.version",
1184 FT_STRING, STR_ASCII, NULL, 0, "version", HFILL
1185 }
1186 },
1187 {
1188 &hf_happp_remotepeerid,
1189 {
1190 "remotepeerid", "happp.remotepeerid",
1191 FT_STRING, STR_ASCII, NULL, 0, "remote peer id", HFILL
1192 }
1193 },
1194 {
1195 &hf_happp_localpeerid,
1196 {
1197 "localpeerid", "happp.localpeerid",
1198 FT_STRING, STR_ASCII, NULL, 0, "local peer id", HFILL
1199 }
1200 },
1201 {
1202 &hf_happp_processpid,
1203 {
1204 "processpid", "happp.processpid",
1205 FT_STRING, STR_ASCII, NULL, 0, "process pid", HFILL
1206 }
1207 },
1208 {
1209 &hf_happp_relativepid,
1210 {
1211 "relativepid", "happp.relativepid",
1212 FT_STRING, STR_ASCII, NULL, 0, "relative pid", HFILL
1213 }
1214 },
1215 {
1216 &hf_happp_status,
1217 {
1218 "status", "happp.status",
1219 FT_STRING, STR_ASCII, NULL, 0, "status message", HFILL
1220 }
1221 },
1222 {
1223 &hf_happp_msg,
1224 {
1225 "message", "happp.msg",
1226 FT_UINT8, BASE_DEC, NULL, 0, NULL, HFILL
1227 }
1228 },
1229 {
1230 &hf_happp_msg_class,
1231 {
1232 "message class", "happp.msg.class",
1233 FT_UINT8, BASE_DEC, NULL, 0, NULL, HFILL
1234 }
1235 },
1236 {
1237 &hf_happp_msg_type,
1238 {
1239 "message type", "happp.msg.type",
1240 FT_UINT8, BASE_DEC, NULL, 0, NULL, HFILL
1241 }
1242 },
1243 {
1244 &hf_happp_msg_len,
1245 {
1246 "message length", "happp.msg.len",
1247 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1248 }
1249 },
1250 {
1251 &hf_happp_stkt_def_id,
1252 {
1253 " ID", "happp.msg.stkt.def.id",
1254 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1255 }
1256 },
1257 {
1258 &hf_happp_stkt_def_name_len,
1259 {
1260 " name length", "happp.msg.stkt.def.name.length",
1261 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1262 }
1263 },
1264 {
1265 &hf_happp_stkt_def_name_value,
1266 {
1267 " name", "happp.msg.stkt.def.name.value",
1268 FT_STRING, STR_ASCII, NULL, 0, NULL, HFILL
1269 }
1270 },
1271 {
1272 &hf_happp_stkt_def_key_type,
1273 {
1274 " key type", "happp.msg.stkt.def.key.type",
1275 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1276 }
1277 },
1278 {
1279 &hf_happp_stkt_def_key_len,
1280 {
1281 " key length", "happp.msg.stkt.def.key.len",
1282 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1283 }
1284 },
1285 {
1286 &hf_happp_stkt_def_data_types,
1287 {
1288 " data types", "happp.msg.stkt.def.data_types",
1289 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1290 }
1291 },
1292 {
1293 &hf_happp_stkt_updt_update_id,
1294 {
1295 " update ID", "happp.msg.stkt.updt.update_id",
1296 FT_INT32, BASE_DEC, NULL, 0, NULL, HFILL
1297 }
1298 },
1299 {
1300 &hf_happp_stkt_updt_expire,
1301 {
1302 " expiration", "happp.msg.stkt.updt.expiration",
1303 FT_INT32, BASE_DEC, NULL, 0, NULL, HFILL
1304 }
1305 },
1306 {
1307 &hf_happp_stkt_updt_key_len,
1308 {
1309 " key length", "happp.msg.stkt.updt.key.len",
1310 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1311 }
1312 },
1313 {
1314 &hf_happp_stkt_updt_key_str_value,
1315 {
1316 " key value", "happp.msg.stkt.updt.key.str.value",
1317 FT_STRING, STR_ASCII, NULL, 0, NULL, HFILL
1318 }
1319 },
1320 {
1321 &hf_happp_stkt_updt_key_int_value,
1322 {
1323 " key value", "happp.msg.stkt.updt.key.int.value",
1324 FT_INT32, BASE_DEC, NULL, 0, NULL, HFILL
1325 }
1326 },
1327 {
1328 &hf_happp_stkt_updt_key_ipv4_value,
1329 {
1330 " key IPv4 value", "happp.msg.stkt.updt.key.ipv4.value",
1331 FT_IPv4, BASE_NONE, NULL, 0, NULL, HFILL
1332 }
1333 },
1334 {
1335 &hf_happp_stkt_updt_key_bytes_value,
1336 {
1337 " key value", "happp.msg.stkt.updt.key.bytes.value",
1338 FT_BYTES, 0, NULL, 0, NULL, HFILL
1339 }
1340 },
1341 {
1342 &hf_happp_stkt_updt_data_server_id,
1343 {
1344 " server_id", "happp.msg.stkt.updt.data.server_id",
1345 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1346 }
1347 },
1348 {
1349 &hf_happp_stkt_updt_data_gpt0,
1350 {
1351 " gpt0", "happp.msg.stkt.updt.data.gpt0",
1352 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1353 }
1354 },
1355 {
1356 &hf_happp_stkt_updt_data_gpc0,
1357 {
1358 " gpc0", "happp.msg.stkt.updt.data.gpc0",
1359 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1360 }
1361 },
1362 {
1363 &hf_happp_stkt_updt_data_gpc0_rate_curr_tick,
1364 {
1365 " gpc0 curr. tick",
1366 "happp.msg.stkt.updt.data.gpc0_rate.curr_tick",
1367 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1368 }
1369 },
1370 {
1371 &hf_happp_stkt_updt_data_gpc0_rate_curr_ctr,
1372 {
1373 " gpc0 curr. ctr.",
1374 "happp.msg.stkt.updt.data.gpc0_rate.curr_ctr",
1375 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1376 }
1377 },
1378 {
1379 &hf_happp_stkt_updt_data_gpc0_rate_prev_ctr,
1380 {
1381 " gpc0 prev. ctr.",
1382 "happp.msg.stkt.updt.data.gpc0_rate.prev_ctr",
1383 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1384 }
1385 },
1386 {
1387 &hf_happp_stkt_updt_data_conn_cnt,
1388 {
1389 " conn_cnt",
1390 "happp.msg.stkt.updt.data.conn_cnt",
1391 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1392 }
1393 },
1394 {
1395 &hf_happp_stkt_updt_data_conn_rate_curr_tick,
1396 {
1397 " conn_rate curr. tick",
1398 "happp.msg.stkt.updt.data.conn_rate.curr_tick",
1399 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1400 }
1401 },
1402 {
1403 &hf_happp_stkt_updt_data_conn_rate_curr_ctr,
1404 {
1405 " conn_rate curr. ctr.",
1406 "happp.msg.stkt.updt.data.conn_rate.curr_ctr",
1407 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1408 }
1409 },
1410 {
1411 &hf_happp_stkt_updt_data_conn_rate_prev_ctr,
1412 {
1413 " conn_rate prev. ctr.",
1414 "happp.msg.stkt.updt.data.conn_rate.prev_ctr",
1415 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1416 }
1417 },
1418 {
1419 &hf_happp_stkt_updt_data_conn_cur,
1420 {
1421 " conn_curr curr. tick",
1422 "happp.msg.stkt.updt.data.conn_cur",
1423 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1424 }
1425 },
1426 {
1427 &hf_happp_stkt_updt_data_sess_cnt,
1428 {
1429 " sess_cnt", "happp.msg.stkt.updt.data.sess_cnt",
1430 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1431 }
1432 },
1433 {
1434 &hf_happp_stkt_updt_data_sess_rate_curr_tick,
1435 {
1436 " sess_rate curr. tick",
1437 "happp.msg.stkt.updt.data.sess_rate.curr_tick",
1438 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1439 }
1440 },
1441 {
1442 &hf_happp_stkt_updt_data_sess_rate_curr_ctr,
1443 {
1444 " sess_rate curr. ctr.",
1445 "happp.msg.stkt.updt.data.sess_rate.curr_ctr",
1446 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1447 }
1448 },
1449 {
1450 &hf_happp_stkt_updt_data_sess_rate_prev_ctr,
1451 {
1452 " sess_rate prev. ctr.",
1453 "happp.msg.stkt.updt.data.sess_rate.prev_ctr",
1454 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1455 }
1456 },
1457 {
1458 &hf_happp_stkt_updt_data_http_req_cnt,
1459 {
1460 " http_req_cnt",
1461 "happp.msg.stkt.updt.data.http_req_cnt",
1462 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1463 }
1464 },
1465 {
1466 &hf_happp_stkt_updt_data_http_req_rate_curr_tick,
1467 {
1468 " http_req_rate curr. tick",
1469 "happp.msg.stkt.updt.data.http_req_rate.curr_tick",
1470 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1471 }
1472 },
1473 {
1474 &hf_happp_stkt_updt_data_http_req_rate_curr_ctr,
1475 {
1476 " http_req_rate curr. ctr.",
1477 "happp.msg.stkt.updt.data.http_req_rate.curr_ctr",
1478 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1479 }
1480 },
1481 {
1482 &hf_happp_stkt_updt_data_http_req_rate_prev_ctr,
1483 {
1484 " http_req_rate prev. ctr.",
1485 "happp.msg.stkt.updt.data.http_req_rate.prev_ctr",
1486 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1487 }
1488 },
1489 {
1490 &hf_happp_stkt_updt_data_http_err_cnt,
1491 {
1492 " http_err_cnt",
1493 "happp.msg.stkt.updt.data.http_err_cnt",
1494 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1495 }
1496 },
1497 {
1498 &hf_happp_stkt_updt_data_http_err_rate_curr_tick,
1499 {
1500 " http_err_rate curr. tick",
1501 "happp.msg.stkt.updt.data.http_err_rate.curr_tick",
1502 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1503 }
1504 },
1505 {
1506 &hf_happp_stkt_updt_data_http_err_rate_curr_ctr,
1507 {
1508 " http_err_rate curr. ctr.",
1509 "happp.msg.stkt.updt.data.http_err_rate.curr_ctr",
1510 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1511 }
1512 },
1513 {
1514 &hf_happp_stkt_updt_data_http_err_rate_prev_ctr,
1515 {
1516 " http_err_rate prev. ctr.",
1517 "happp.msg.stkt.updt.data.http_err_rate.prev_ctr",
1518 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1519 }
1520 },
1521 {
1522 &hf_happp_stkt_updt_data_bytes_in_cnt,
1523 {
1524 " bytes_in_cnt",
1525 "happp.msg.stkt.updt.data.bytes_in_cnt",
1526 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1527 }
1528 },
1529 {
1530 &hf_happp_stkt_updt_data_bytes_in_rate_curr_tick,
1531 {
1532 " bytes_in_rate curr. tick",
1533 "happp.msg.stkt.updt.data.bytes_in_rate.curr_tick",
1534 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1535 }
1536 },
1537 {
1538 &hf_happp_stkt_updt_data_bytes_in_rate_curr_ctr,
1539 {
1540 " bytes_in_rate curr. ctr.",
1541 "happp.msg.stkt.updt.data.bytes_in_rate.curr_ctr",
1542 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1543 }
1544 },
1545 {
1546 &hf_happp_stkt_updt_data_bytes_in_rate_prev_ctr,
1547 {
1548 " bytes_in_rate prev. ctr.",
1549 "happp.msg.stkt.updt.data.bytes_in_rate.prev_ctr",
1550 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1551 }
1552 },
1553 {
1554 &hf_happp_stkt_updt_data_bytes_out_cnt,
1555 {
1556 " bytes_out_cnt",
1557 "happp.msg.stkt.updt.data.bytes_out_cnt",
1558 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1559 }
1560 },
1561 {
1562 &hf_happp_stkt_updt_data_bytes_out_rate_curr_tick,
1563 {
1564 " bytes_out_rate curr. tick",
1565 "happp.msg.stkt.updt.data.bytes_out_rate.curr_tick",
1566 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1567 }
1568 },
1569 {
1570 &hf_happp_stkt_updt_data_bytes_out_rate_curr_ctr,
1571 {
1572 " bytes_out_rate curr. ctr.",
1573 "happp.msg.stkt.updt.data.bytes_out_rate.curr_ctr",
1574 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1575 }
1576 },
1577 {
1578 &hf_happp_stkt_updt_data_bytes_out_rate_prev_ctr,
1579 {
1580 " bytes_out_rate prev. ctr.",
1581 "happp.msg.stkt.updt.data.bytes_out_rate.prev_ctr",
1582 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1583 }
1584 },
1585 {
1586 &hf_happp_stkt_updt_ack_table_id,
1587 {
1588 " remote table Id",
1589 "happp.msg.stkt.updt.ack.table_id",
1590 FT_UINT64, BASE_DEC, NULL, 0, NULL, HFILL
1591 }
1592 },
1593 {
1594 &hf_happp_stkt_updt_ack_update_id,
1595 {
1596 " update Id", "happp.msg.stkt.updt.ack.update_id",
1597 FT_INT32, BASE_DEC, NULL, 0, NULL, HFILL
1598 }
1599 },
1600 };
1601
1602 /* Setup protocol subtree array */
1603 static gint *ett[] = {
1604 &ett_happp,
1605 &ett_happp_msg
1606 };
1607
1608 /* Register the protocol name and description */
1609 proto_happp = proto_register_protocol("HAProxy Peers Protocol", "HAPPP", "happp");
1610
1611 /* Required function calls to register the header fields and subtrees */
1612 proto_register_field_array(proto_happp, hf, array_length(hf));
1613 proto_register_subtree_array(ett, array_length(ett));
1614}
1615
1616static gboolean
1617dissect_happp_heur_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
1618{
1619 size_t proto_strlen;
1620 conversation_t *conversation;
1621
1622 proto_strlen = strlen(HAPPP_PROTOCOL);
1623
1624 if (tvb_captured_length(tvb) < proto_strlen + 1)
1625 return FALSE;
1626
Joseph Herlantbd0f83f2018-11-09 19:00:24 -08001627 /* Check that we received a line beginning with HAPPP_PROTOCOL
Frédéric Lécaille6d889502017-11-15 14:50:19 +01001628 * followed by a space character.
1629 */
1630 if (tvb_strneql(tvb, 0, HAPPP_PROTOCOL, proto_strlen) ||
1631 tvb_get_guint8(tvb, proto_strlen) != ' ')
1632 return FALSE;
1633
1634 conversation = find_or_create_conversation(pinfo);
1635 if (!conversation)
1636 return FALSE;
1637
1638 conversation_set_dissector(conversation, happp_tcp_handle);
1639 dissect_happp_tcp(tvb, pinfo, tree, data);
1640
1641 return TRUE;
1642}
1643
1644/* Simpler form of proto_reg_handoff_happp which can be used if there are
1645 * no prefs-dependent registration function calls. */
1646void
1647proto_reg_handoff_happp(void)
1648{
1649 /* Use create_dissector_handle() to indicate that dissect_happp_tcp()
1650 * returns the number of bytes it dissected (or 0 if it thinks the packet
1651 * does not belong to HAProxy Peers Protocol).
1652 */
1653 happp_tcp_handle = create_dissector_handle(dissect_happp_tcp, proto_happp);
1654 heur_dissector_add("tcp", dissect_happp_heur_tcp, "HAPPP over TCP", "happp_tcp",
1655 proto_happp, HEURISTIC_ENABLE);
1656}
1657
William Lallemand2be58f72020-04-25 22:03:29 +02001658
1659void
1660plugin_register(void)
1661{
1662 static proto_plugin plug;
1663
1664 plug.register_protoinfo = proto_register_happp;
1665 plug.register_handoff = proto_reg_handoff_happp;
1666 proto_register_plugin(&plug);
1667}