blob: aec1d9e5727894e9f282116dbc2f71c976d46980 [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: Cisco UniDirectional Link Detection (UDLD) protocol printer */
19
20/* specification: RFC 5171 */
21
JP Abgrall53f17a92014-02-12 14:02:41 -080022#ifdef HAVE_CONFIG_H
Elliott Hughes820eced2021-08-20 18:00:50 -070023#include <config.h>
JP Abgrall53f17a92014-02-12 14:02:41 -080024#endif
25
Elliott Hughes820eced2021-08-20 18:00:50 -070026#include "netdissect-stdinc.h"
JP Abgrall53f17a92014-02-12 14:02:41 -080027
Elliott Hughes820eced2021-08-20 18:00:50 -070028#define ND_LONGJMP_FROM_TCHECK
Elliott Hughese2e3bd12017-05-15 10:59:29 -070029#include "netdissect.h"
Elliott Hughes892a68b2015-10-19 14:43:53 -070030#include "extract.h"
JP Abgrall53f17a92014-02-12 14:02:41 -080031
Elliott Hughese2e3bd12017-05-15 10:59:29 -070032
JP Abgrall53f17a92014-02-12 14:02:41 -080033#define UDLD_HEADER_LEN 4
Elliott Hughes820eced2021-08-20 18:00:50 -070034#define UDLD_TLV_HEADER_LEN 4
JP Abgrall53f17a92014-02-12 14:02:41 -080035#define UDLD_DEVICE_ID_TLV 0x0001
36#define UDLD_PORT_ID_TLV 0x0002
37#define UDLD_ECHO_TLV 0x0003
38#define UDLD_MESSAGE_INTERVAL_TLV 0x0004
39#define UDLD_TIMEOUT_INTERVAL_TLV 0x0005
40#define UDLD_DEVICE_NAME_TLV 0x0006
41#define UDLD_SEQ_NUMBER_TLV 0x0007
42
43static const struct tok udld_tlv_values[] = {
44 { UDLD_DEVICE_ID_TLV, "Device-ID TLV"},
45 { UDLD_PORT_ID_TLV, "Port-ID TLV"},
46 { UDLD_ECHO_TLV, "Echo TLV"},
47 { UDLD_MESSAGE_INTERVAL_TLV, "Message Interval TLV"},
48 { UDLD_TIMEOUT_INTERVAL_TLV, "Timeout Interval TLV"},
49 { UDLD_DEVICE_NAME_TLV, "Device Name TLV"},
50 { UDLD_SEQ_NUMBER_TLV,"Sequence Number TLV"},
51 { 0, NULL}
52};
53
54static const struct tok udld_code_values[] = {
55 { 0x00, "Reserved"},
56 { 0x01, "Probe message"},
57 { 0x02, "Echo message"},
58 { 0x03, "Flush message"},
59 { 0, NULL}
60};
61
Elliott Hughes820eced2021-08-20 18:00:50 -070062static const struct tok udld_flags_bitmap_str[] = {
63 { 1U << 0, "RT" },
64 { 1U << 1, "RSY" },
65 { 1U << 2, "MBZ-2" },
66 { 1U << 3, "MBZ-3" },
67 { 1U << 4, "MBZ-4" },
68 { 1U << 5, "MBZ-5" },
69 { 1U << 6, "MBZ-6" },
70 { 1U << 7, "MBZ-7" },
JP Abgrall53f17a92014-02-12 14:02:41 -080071 { 0, NULL}
72};
73
74/*
Elliott Hughese2e3bd12017-05-15 10:59:29 -070075 * UDLD's Protocol Data Unit format:
JP Abgrall53f17a92014-02-12 14:02:41 -080076 *
Elliott Hughese2e3bd12017-05-15 10:59:29 -070077 * 0 1 2 3
78 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
Elliott Hughes892a68b2015-10-19 14:43:53 -070079 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
80 * | Ver | Opcode | Flags | Checksum |
81 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
82 * | List of TLVs (variable length list) |
83 * | ... |
84 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
JP Abgrall53f17a92014-02-12 14:02:41 -080085 *
Elliott Hughese2e3bd12017-05-15 10:59:29 -070086 * TLV format:
87 *
88 * 0 1 2 3
89 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
90 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 * | TYPE | LENGTH |
92 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 * | VALUE |
94 * | ... |
95 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 *
97 * LENGTH: Length in bytes of the Type, Length, and Value fields.
JP Abgrall53f17a92014-02-12 14:02:41 -080098 */
99
Elliott Hughes892a68b2015-10-19 14:43:53 -0700100#define UDLD_EXTRACT_VERSION(x) (((x)&0xe0)>>5)
101#define UDLD_EXTRACT_OPCODE(x) ((x)&0x1f)
JP Abgrall53f17a92014-02-12 14:02:41 -0800102
103void
Elliott Hughes820eced2021-08-20 18:00:50 -0700104udld_print(netdissect_options *ndo,
105 const u_char *tptr, u_int length)
JP Abgrall53f17a92014-02-12 14:02:41 -0800106{
Elliott Hughes820eced2021-08-20 18:00:50 -0700107 uint8_t ver, code, flags;
JP Abgrall53f17a92014-02-12 14:02:41 -0800108
Elliott Hughes820eced2021-08-20 18:00:50 -0700109 ndo->ndo_protocol = "udld";
JP Abgrall53f17a92014-02-12 14:02:41 -0800110 if (length < UDLD_HEADER_LEN)
Elliott Hughes820eced2021-08-20 18:00:50 -0700111 goto invalid;
JP Abgrall53f17a92014-02-12 14:02:41 -0800112
Elliott Hughes820eced2021-08-20 18:00:50 -0700113 ver = UDLD_EXTRACT_VERSION(GET_U_1(tptr));
114 code = UDLD_EXTRACT_OPCODE(GET_U_1(tptr));
115 tptr += 1;
116 length -= 1;
JP Abgrall53f17a92014-02-12 14:02:41 -0800117
Elliott Hughes820eced2021-08-20 18:00:50 -0700118 flags = GET_U_1(tptr);
119 tptr += 1;
120 length -= 1;
JP Abgrall53f17a92014-02-12 14:02:41 -0800121
Elliott Hughes820eced2021-08-20 18:00:50 -0700122 ND_PRINT("UDLDv%u, Code %s (%x), Flags [%s] (0x%02x), length %u",
123 ver,
JP Abgrall53f17a92014-02-12 14:02:41 -0800124 tok2str(udld_code_values, "Reserved", code),
125 code,
Elliott Hughes820eced2021-08-20 18:00:50 -0700126 bittok2str(udld_flags_bitmap_str, "none", flags),
127 flags,
128 length + 2);
JP Abgrall53f17a92014-02-12 14:02:41 -0800129
130 /*
131 * In non-verbose mode, just print version and opcode type
132 */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700133 if (ndo->ndo_vflag < 1) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700134 goto tcheck_remainder;
JP Abgrall53f17a92014-02-12 14:02:41 -0800135 }
136
Elliott Hughes820eced2021-08-20 18:00:50 -0700137 ND_PRINT("\n\tChecksum 0x%04x (unverified)", GET_BE_U_2(tptr));
138 tptr += 2;
139 length -= 2;
JP Abgrall53f17a92014-02-12 14:02:41 -0800140
Elliott Hughes820eced2021-08-20 18:00:50 -0700141 while (length) {
142 uint16_t type, len;
JP Abgrall53f17a92014-02-12 14:02:41 -0800143
Elliott Hughes820eced2021-08-20 18:00:50 -0700144 if (length < UDLD_TLV_HEADER_LEN)
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700145 goto invalid;
146
Elliott Hughes820eced2021-08-20 18:00:50 -0700147 type = GET_BE_U_2(tptr);
148 tptr += 2;
149 length -= 2;
150
151 len = GET_BE_U_2(tptr);
152 tptr += 2;
153 length -= 2;
154
155 ND_PRINT("\n\t%s (0x%04x) TLV, length %u",
156 tok2str(udld_tlv_values, "Unknown", type),
157 type, len);
158
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700159 /* infinite loop check */
Elliott Hughes820eced2021-08-20 18:00:50 -0700160 if (len <= UDLD_TLV_HEADER_LEN)
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700161 goto invalid;
162
Elliott Hughes820eced2021-08-20 18:00:50 -0700163 len -= UDLD_TLV_HEADER_LEN;
164 if (length < len)
165 goto invalid;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700166
JP Abgrall53f17a92014-02-12 14:02:41 -0800167 switch (type) {
168 case UDLD_DEVICE_ID_TLV:
169 case UDLD_PORT_ID_TLV:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700170 case UDLD_DEVICE_NAME_TLV:
Elliott Hughes820eced2021-08-20 18:00:50 -0700171 ND_PRINT(", ");
172 nd_printjnp(ndo, tptr, len);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700173 break;
174
175 case UDLD_ECHO_TLV:
Elliott Hughes820eced2021-08-20 18:00:50 -0700176 ND_PRINT(", ");
177 (void)nd_printn(ndo, tptr, len, NULL);
JP Abgrall53f17a92014-02-12 14:02:41 -0800178 break;
179
Elliott Hughes892a68b2015-10-19 14:43:53 -0700180 case UDLD_MESSAGE_INTERVAL_TLV:
JP Abgrall53f17a92014-02-12 14:02:41 -0800181 case UDLD_TIMEOUT_INTERVAL_TLV:
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700182 if (len != 1)
183 goto invalid;
Elliott Hughes820eced2021-08-20 18:00:50 -0700184 ND_PRINT(", %us", (GET_U_1(tptr)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800185 break;
186
187 case UDLD_SEQ_NUMBER_TLV:
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700188 if (len != 4)
189 goto invalid;
Elliott Hughes820eced2021-08-20 18:00:50 -0700190 ND_PRINT(", %u", GET_BE_U_4(tptr));
JP Abgrall53f17a92014-02-12 14:02:41 -0800191 break;
192
193 default:
Elliott Hughes820eced2021-08-20 18:00:50 -0700194 ND_TCHECK_LEN(tptr, len);
JP Abgrall53f17a92014-02-12 14:02:41 -0800195 break;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700196 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800197 tptr += len;
Elliott Hughes820eced2021-08-20 18:00:50 -0700198 length -= len;
JP Abgrall53f17a92014-02-12 14:02:41 -0800199 }
200
201 return;
202
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700203invalid:
Elliott Hughes820eced2021-08-20 18:00:50 -0700204 nd_print_invalid(ndo);
205tcheck_remainder:
206 ND_TCHECK_LEN(tptr, length);
JP Abgrall53f17a92014-02-12 14:02:41 -0800207}