]> The Tcpdump Group git mirrors - tcpdump/blob - print-udld.c
Add the ndo_protocol field in the netdissect_options structure
[tcpdump] / print-udld.c
1 /*
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 *
15 * Original code by Carles Kishimoto <carles.kishimoto@gmail.com>
16 */
17
18 /* \summary: Cisco UniDirectional Link Detection (UDLD) protocol printer */
19
20 /* specification: RFC 5171 */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "netdissect-stdinc.h"
27
28 #include "netdissect.h"
29 #include "extract.h"
30
31 static const char tstr[] = " [|udld]";
32
33 #define UDLD_HEADER_LEN 4
34 #define UDLD_DEVICE_ID_TLV 0x0001
35 #define UDLD_PORT_ID_TLV 0x0002
36 #define UDLD_ECHO_TLV 0x0003
37 #define UDLD_MESSAGE_INTERVAL_TLV 0x0004
38 #define UDLD_TIMEOUT_INTERVAL_TLV 0x0005
39 #define UDLD_DEVICE_NAME_TLV 0x0006
40 #define UDLD_SEQ_NUMBER_TLV 0x0007
41
42 static const struct tok udld_tlv_values[] = {
43 { UDLD_DEVICE_ID_TLV, "Device-ID TLV"},
44 { UDLD_PORT_ID_TLV, "Port-ID TLV"},
45 { UDLD_ECHO_TLV, "Echo TLV"},
46 { UDLD_MESSAGE_INTERVAL_TLV, "Message Interval TLV"},
47 { UDLD_TIMEOUT_INTERVAL_TLV, "Timeout Interval TLV"},
48 { UDLD_DEVICE_NAME_TLV, "Device Name TLV"},
49 { UDLD_SEQ_NUMBER_TLV,"Sequence Number TLV"},
50 { 0, NULL}
51 };
52
53 static const struct tok udld_code_values[] = {
54 { 0x00, "Reserved"},
55 { 0x01, "Probe message"},
56 { 0x02, "Echo message"},
57 { 0x03, "Flush message"},
58 { 0, NULL}
59 };
60
61 static const struct tok udld_flags_values[] = {
62 { 0x00, "RT"},
63 { 0x01, "RSY"},
64 { 0, NULL}
65 };
66
67 /*
68 * UDLD's Protocol Data Unit format:
69 *
70 * 0 1 2 3
71 * 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
72 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73 * | Ver | Opcode | Flags | Checksum |
74 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
75 * | List of TLVs (variable length list) |
76 * | ... |
77 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
78 *
79 * TLV format:
80 *
81 * 0 1 2 3
82 * 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
83 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
84 * | TYPE | LENGTH |
85 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86 * | VALUE |
87 * | ... |
88 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
89 *
90 * LENGTH: Length in bytes of the Type, Length, and Value fields.
91 */
92
93 #define UDLD_EXTRACT_VERSION(x) (((x)&0xe0)>>5)
94 #define UDLD_EXTRACT_OPCODE(x) ((x)&0x1f)
95
96 void
97 udld_print (netdissect_options *ndo, const u_char *pptr, u_int length)
98 {
99 int code, type, len;
100 const u_char *tptr;
101
102 ndo->ndo_protocol = "udld";
103 if (length < UDLD_HEADER_LEN)
104 goto trunc;
105
106 tptr = pptr;
107
108 ND_TCHECK_LEN(tptr, UDLD_HEADER_LEN);
109
110 code = UDLD_EXTRACT_OPCODE(EXTRACT_U_1(tptr));
111
112 ND_PRINT("UDLDv%u, Code %s (%x), Flags [%s] (0x%02x), length %u",
113 UDLD_EXTRACT_VERSION(EXTRACT_U_1(tptr)),
114 tok2str(udld_code_values, "Reserved", code),
115 code,
116 bittok2str(udld_flags_values, "none", EXTRACT_U_1((tptr + 1))),
117 EXTRACT_U_1((tptr + 1)),
118 length);
119
120 /*
121 * In non-verbose mode, just print version and opcode type
122 */
123 if (ndo->ndo_vflag < 1) {
124 return;
125 }
126
127 ND_PRINT("\n\tChecksum 0x%04x (unverified)", EXTRACT_BE_U_2(tptr + 2));
128
129 tptr += UDLD_HEADER_LEN;
130
131 while (tptr < (pptr+length)) {
132
133 ND_TCHECK_4(tptr);
134 type = EXTRACT_BE_U_2(tptr);
135 len = EXTRACT_BE_U_2(tptr + 2);
136
137 ND_PRINT("\n\t%s (0x%04x) TLV, length %u",
138 tok2str(udld_tlv_values, "Unknown", type),
139 type, len);
140
141 if (type == 0)
142 goto invalid;
143
144 /* infinite loop check */
145 if (len <= 4)
146 goto invalid;
147
148 len -= 4;
149 tptr += 4;
150
151 ND_TCHECK_LEN(tptr, len);
152
153 switch (type) {
154 case UDLD_DEVICE_ID_TLV:
155 case UDLD_PORT_ID_TLV:
156 case UDLD_DEVICE_NAME_TLV:
157 ND_PRINT(", ");
158 fn_printzp(ndo, tptr, len, NULL);
159 break;
160
161 case UDLD_ECHO_TLV:
162 ND_PRINT(", ");
163 (void)fn_printn(ndo, tptr, len, NULL);
164 break;
165
166 case UDLD_MESSAGE_INTERVAL_TLV:
167 case UDLD_TIMEOUT_INTERVAL_TLV:
168 if (len != 1)
169 goto invalid;
170 ND_PRINT(", %us", (EXTRACT_U_1(tptr)));
171 break;
172
173 case UDLD_SEQ_NUMBER_TLV:
174 if (len != 4)
175 goto invalid;
176 ND_PRINT(", %u", EXTRACT_BE_U_4(tptr));
177 break;
178
179 default:
180 break;
181 }
182 tptr += len;
183 }
184
185 return;
186
187 invalid:
188 ND_PRINT("%s", istr);
189 return;
190 trunc:
191 ND_PRINT("%s", tstr);
192 }
193
194 /*
195 * Local Variables:
196 * c-style: whitesmith
197 * c-basic-offset: 4
198 * End:
199 */