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