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