blob: a1ae4ba4d5aa047ec6efc81ad12328fa1d09aa51 [file] [log] [blame]
JP Abgrall53f17a92014-02-12 14:02:41 -08001/*
2 * Copyright (c) 1998-2007 The TCPDUMP project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that: (1) source code
6 * distributions retain the above copyright notice and this paragraph
7 * in its entirety, and (2) distributions including binary code include
8 * the above copyright notice and this paragraph in its entirety in
9 * the documentation or other materials provided with the distribution.
10 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
11 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
12 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13 * FOR A PARTICULAR PURPOSE.
14 *
JP Abgrall53f17a92014-02-12 14:02:41 -080015 * Original code by Carles Kishimoto <carles.kishimoto@gmail.com>
16 */
17
Elliott Hughese2e3bd12017-05-15 10:59:29 -070018/* \summary: Dynamic Trunking Protocol (DTP) printer */
19
JP Abgrall53f17a92014-02-12 14:02:41 -080020#ifdef HAVE_CONFIG_H
Elliott Hughes820eced2021-08-20 18:00:50 -070021#include <config.h>
JP Abgrall53f17a92014-02-12 14:02:41 -080022#endif
23
Elliott Hughes820eced2021-08-20 18:00:50 -070024#include "netdissect-stdinc.h"
JP Abgrall53f17a92014-02-12 14:02:41 -080025
Elliott Hughes820eced2021-08-20 18:00:50 -070026#define ND_LONGJMP_FROM_TCHECK
Elliott Hughese2e3bd12017-05-15 10:59:29 -070027#include "netdissect.h"
JP Abgrall53f17a92014-02-12 14:02:41 -080028#include "addrtoname.h"
Elliott Hughes892a68b2015-10-19 14:43:53 -070029#include "extract.h"
JP Abgrall53f17a92014-02-12 14:02:41 -080030
Elliott Hughese2e3bd12017-05-15 10:59:29 -070031
JP Abgrall53f17a92014-02-12 14:02:41 -080032#define DTP_HEADER_LEN 1
33#define DTP_DOMAIN_TLV 0x0001
34#define DTP_STATUS_TLV 0x0002
35#define DTP_DTP_TYPE_TLV 0x0003
36#define DTP_NEIGHBOR_TLV 0x0004
37
38static const struct tok dtp_tlv_values[] = {
Elliott Hughes820eced2021-08-20 18:00:50 -070039 { DTP_DOMAIN_TLV, "Domain" },
40 { DTP_STATUS_TLV, "Status" },
41 { DTP_DTP_TYPE_TLV, "DTP type" },
42 { DTP_NEIGHBOR_TLV, "Neighbor" },
JP Abgrall53f17a92014-02-12 14:02:41 -080043 { 0, NULL}
44};
45
46void
Elliott Hughes820eced2021-08-20 18:00:50 -070047dtp_print(netdissect_options *ndo, const u_char *tptr, u_int length)
JP Abgrall53f17a92014-02-12 14:02:41 -080048{
Elliott Hughes820eced2021-08-20 18:00:50 -070049 ndo->ndo_protocol = "dtp";
50 if (length < DTP_HEADER_LEN) {
51 ND_PRINT("[zero packet length]");
52 goto invalid;
53 }
JP Abgrall53f17a92014-02-12 14:02:41 -080054
Elliott Hughes820eced2021-08-20 18:00:50 -070055 ND_PRINT("DTPv%u, length %u",
56 GET_U_1(tptr),
57 length);
JP Abgrall53f17a92014-02-12 14:02:41 -080058
59 /*
60 * In non-verbose mode, just print version.
61 */
Elliott Hughes892a68b2015-10-19 14:43:53 -070062 if (ndo->ndo_vflag < 1) {
JP Abgrall53f17a92014-02-12 14:02:41 -080063 return;
64 }
65
66 tptr += DTP_HEADER_LEN;
Elliott Hughes820eced2021-08-20 18:00:50 -070067 length -= DTP_HEADER_LEN;
JP Abgrall53f17a92014-02-12 14:02:41 -080068
Elliott Hughes820eced2021-08-20 18:00:50 -070069 while (length) {
70 uint16_t type, len;
JP Abgrall53f17a92014-02-12 14:02:41 -080071
Elliott Hughes820eced2021-08-20 18:00:50 -070072 if (length < 4) {
73 ND_PRINT("[%u bytes remaining]", length);
74 goto invalid;
75 }
76 type = GET_BE_U_2(tptr);
77 len = GET_BE_U_2(tptr + 2);
Elliott Hughese2e3bd12017-05-15 10:59:29 -070078 /* XXX: should not be but sometimes it is, see the test captures */
79 if (type == 0)
JP Abgrall53f17a92014-02-12 14:02:41 -080080 return;
Elliott Hughes820eced2021-08-20 18:00:50 -070081 ND_PRINT("\n\t%s (0x%04x) TLV, length %u",
JP Abgrall53f17a92014-02-12 14:02:41 -080082 tok2str(dtp_tlv_values, "Unknown", type),
Elliott Hughes820eced2021-08-20 18:00:50 -070083 type, len);
JP Abgrall53f17a92014-02-12 14:02:41 -080084
Elliott Hughese2e3bd12017-05-15 10:59:29 -070085 /* infinite loop check */
Elliott Hughes820eced2021-08-20 18:00:50 -070086 if (len < 4 || len > length) {
87 ND_PRINT("[invalid TLV length %u]", len);
Elliott Hughese2e3bd12017-05-15 10:59:29 -070088 goto invalid;
Elliott Hughes820eced2021-08-20 18:00:50 -070089 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -070090
JP Abgrall53f17a92014-02-12 14:02:41 -080091 switch (type) {
92 case DTP_DOMAIN_TLV:
Elliott Hughes820eced2021-08-20 18:00:50 -070093 ND_PRINT(", ");
94 nd_printjnp(ndo, tptr+4, len-4);
JP Abgrall53f17a92014-02-12 14:02:41 -080095 break;
96
Elliott Hughes892a68b2015-10-19 14:43:53 -070097 case DTP_STATUS_TLV:
JP Abgrall53f17a92014-02-12 14:02:41 -080098 case DTP_DTP_TYPE_TLV:
Elliott Hughes820eced2021-08-20 18:00:50 -070099 if (len != 5)
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700100 goto invalid;
Elliott Hughes820eced2021-08-20 18:00:50 -0700101 ND_PRINT(", 0x%x", GET_U_1(tptr + 4));
JP Abgrall53f17a92014-02-12 14:02:41 -0800102 break;
103
104 case DTP_NEIGHBOR_TLV:
Elliott Hughes820eced2021-08-20 18:00:50 -0700105 if (len != 10)
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700106 goto invalid;
Elliott Hughes820eced2021-08-20 18:00:50 -0700107 ND_PRINT(", %s", GET_ETHERADDR_STRING(tptr+4));
JP Abgrall53f17a92014-02-12 14:02:41 -0800108 break;
109
110 default:
Elliott Hughes820eced2021-08-20 18:00:50 -0700111 ND_TCHECK_LEN(tptr, len);
JP Abgrall53f17a92014-02-12 14:02:41 -0800112 break;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700113 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800114 tptr += len;
Elliott Hughes820eced2021-08-20 18:00:50 -0700115 length -= len;
JP Abgrall53f17a92014-02-12 14:02:41 -0800116 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800117 return;
118
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700119 invalid:
Elliott Hughes820eced2021-08-20 18:00:50 -0700120 nd_print_invalid(ndo);
121 ND_TCHECK_LEN(tptr, length);
JP Abgrall53f17a92014-02-12 14:02:41 -0800122}