]> The Tcpdump Group git mirrors - tcpdump/blob - print-bfd.c
remove redundant ND_TCHECK, let GET_ routines handle checks
[tcpdump] / print-bfd.c
1 /*
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that: (1) source code
4 * distributions retain the above copyright notice and this paragraph
5 * in its entirety, and (2) distributions including binary code include
6 * the above copyright notice and this paragraph in its entirety in
7 * the documentation or other materials provided with the distribution.
8 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
9 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
10 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
11 * FOR A PARTICULAR PURPOSE.
12 *
13 * Original code by Hannes Gredler (hannes@gredler.at)
14 */
15
16 /* \summary: Bidirectional Forwarding Detection (BFD) printer */
17
18 /*
19 * specification: draft-ietf-bfd-base-01 for version 0,
20 * RFC 5880 for version 1, and RFC 5881
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "netdissect-stdinc.h"
28
29 #include "netdissect.h"
30 #include "extract.h"
31
32 #include "udp.h"
33
34 /*
35 * Control packet, BFDv0, draft-ietf-bfd-base-01
36 *
37 * 0 1 2 3
38 * 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
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 * |Vers | Diag |H|D|P|F|C|A|Rsv| Detect Mult | Length |
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * | My Discriminator |
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * | Your Discriminator |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | Desired Min TX Interval |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 * | Required Min RX Interval |
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 * | Required Min Echo RX Interval |
51 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 */
53
54 /*
55 * Control packet, BFDv1, RFC 5880
56 *
57 * 0 1 2 3
58 * 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
59 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 * |Vers | Diag |Sta|P|F|C|A|D|M| Detect Mult | Length |
61 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62 * | My Discriminator |
63 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 * | Your Discriminator |
65 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66 * | Desired Min TX Interval |
67 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68 * | Required Min RX Interval |
69 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70 * | Required Min Echo RX Interval |
71 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72 */
73
74 struct bfd_header_t {
75 nd_uint8_t version_diag;
76 nd_uint8_t flags;
77 nd_uint8_t detect_time_multiplier;
78 nd_uint8_t length;
79 nd_uint32_t my_discriminator;
80 nd_uint32_t your_discriminator;
81 nd_uint32_t desired_min_tx_interval;
82 nd_uint32_t required_min_rx_interval;
83 nd_uint32_t required_min_echo_interval;
84 };
85
86 /*
87 * An optional Authentication Header may be present
88 *
89 * 0 1 2 3
90 * 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
91 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92 * | Auth Type | Auth Len | Authentication Data... |
93 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 */
95
96 struct bfd_auth_header_t {
97 nd_uint8_t auth_type;
98 nd_uint8_t auth_len;
99 nd_uint8_t auth_data;
100 nd_uint8_t dummy; /* minimum 4 bytes */
101 };
102
103 enum auth_type {
104 AUTH_PASSWORD = 1,
105 AUTH_MD5 = 2,
106 AUTH_MET_MD5 = 3,
107 AUTH_SHA1 = 4,
108 AUTH_MET_SHA1 = 5
109 };
110
111 static const struct tok bfd_v1_authentication_values[] = {
112 { AUTH_PASSWORD, "Simple Password" },
113 { AUTH_MD5, "Keyed MD5" },
114 { AUTH_MET_MD5, "Meticulous Keyed MD5" },
115 { AUTH_SHA1, "Keyed SHA1" },
116 { AUTH_MET_SHA1, "Meticulous Keyed SHA1" },
117 { 0, NULL }
118 };
119
120 enum auth_length {
121 AUTH_PASSWORD_FIELD_MIN_LEN = 4, /* header + password min: 3 + 1 */
122 AUTH_PASSWORD_FIELD_MAX_LEN = 19, /* header + password max: 3 + 16 */
123 AUTH_MD5_FIELD_LEN = 24,
124 AUTH_MD5_HASH_LEN = 16,
125 AUTH_SHA1_FIELD_LEN = 28,
126 AUTH_SHA1_HASH_LEN = 20
127 };
128
129 #define BFD_EXTRACT_VERSION(x) (((x)&0xe0)>>5)
130 #define BFD_EXTRACT_DIAG(x) ((x)&0x1f)
131
132 static const struct tok bfd_diag_values[] = {
133 { 0, "No Diagnostic" },
134 { 1, "Control Detection Time Expired" },
135 { 2, "Echo Function Failed" },
136 { 3, "Neighbor Signaled Session Down" },
137 { 4, "Forwarding Plane Reset" },
138 { 5, "Path Down" },
139 { 6, "Concatenated Path Down" },
140 { 7, "Administratively Down" },
141 { 8, "Reverse Concatenated Path Down" },
142 { 0, NULL }
143 };
144
145 static const struct tok bfd_port_values[] = {
146 { BFD_CONTROL_PORT, "Control" },
147 { BFD_MULTIHOP_PORT, "Multihop" },
148 { BFD_LAG_PORT, "Lag" },
149 { 0, NULL }
150 };
151
152 #define BFD_FLAG_AUTH 0x04
153
154 static const struct tok bfd_v0_flag_values[] = {
155 { 0x80, "I Hear You" },
156 { 0x40, "Demand" },
157 { 0x20, "Poll" },
158 { 0x10, "Final" },
159 { 0x08, "Control Plane Independent" },
160 { BFD_FLAG_AUTH, "Authentication Present" },
161 { 0x02, "Reserved" },
162 { 0x01, "Reserved" },
163 { 0, NULL }
164 };
165
166 static const struct tok bfd_v1_flag_values[] = {
167 { 0x20, "Poll" },
168 { 0x10, "Final" },
169 { 0x08, "Control Plane Independent" },
170 { BFD_FLAG_AUTH, "Authentication Present" },
171 { 0x02, "Demand" },
172 { 0x01, "Multipoint" },
173 { 0, NULL }
174 };
175
176 static const struct tok bfd_v1_state_values[] = {
177 { 0, "AdminDown" },
178 { 1, "Down" },
179 { 2, "Init" },
180 { 3, "Up" },
181 { 0, NULL }
182 };
183
184 static int
185 auth_print(netdissect_options *ndo, const u_char *pptr)
186 {
187 const struct bfd_auth_header_t *bfd_auth_header;
188 uint8_t auth_type, auth_len;
189 int i;
190
191 pptr += sizeof (struct bfd_header_t);
192 bfd_auth_header = (const struct bfd_auth_header_t *)pptr;
193 ND_TCHECK_SIZE(bfd_auth_header);
194 auth_type = GET_U_1(bfd_auth_header->auth_type);
195 auth_len = GET_U_1(bfd_auth_header->auth_len);
196 ND_PRINT("\n\tAuthentication: %s (%u), length: %u",
197 tok2str(bfd_v1_authentication_values,"Unknown",auth_type),
198 auth_type, auth_len);
199 pptr += 2;
200 ND_PRINT("\n\t Auth Key ID: %u", GET_U_1(pptr));
201
202 switch(auth_type) {
203 case AUTH_PASSWORD:
204 /*
205 * Simple Password Authentication Section Format
206 *
207 * 0 1 2 3
208 * 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
209 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
210 * | Auth Type | Auth Len | Auth Key ID | Password... |
211 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
212 * | ... |
213 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
214 */
215 if (auth_len < AUTH_PASSWORD_FIELD_MIN_LEN ||
216 auth_len > AUTH_PASSWORD_FIELD_MAX_LEN) {
217 ND_PRINT("[invalid length %u]",
218 auth_len);
219 break;
220 }
221 pptr++;
222 ND_PRINT(", Password: ");
223 /* the length is equal to the password length plus three */
224 if (nd_printn(ndo, pptr, auth_len - 3,
225 ndo->ndo_snapend))
226 goto trunc;
227 break;
228 case AUTH_MD5:
229 case AUTH_MET_MD5:
230 /*
231 * Keyed MD5 and Meticulous Keyed MD5 Authentication Section Format
232 *
233 * 0 1 2 3
234 * 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
235 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
236 * | Auth Type | Auth Len | Auth Key ID | Reserved |
237 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
238 * | Sequence Number |
239 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
240 * | Auth Key/Digest... |
241 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
242 * | ... |
243 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
244 */
245 if (auth_len != AUTH_MD5_FIELD_LEN) {
246 ND_PRINT("[invalid length %u]",
247 auth_len);
248 break;
249 }
250 pptr += 2;
251 ND_PRINT(", Sequence Number: 0x%08x", GET_BE_U_4(pptr));
252 pptr += 4;
253 ND_TCHECK_LEN(pptr, AUTH_MD5_HASH_LEN);
254 ND_PRINT("\n\t Digest: ");
255 for(i = 0; i < AUTH_MD5_HASH_LEN; i++)
256 ND_PRINT("%02x", GET_U_1(pptr + i));
257 break;
258 case AUTH_SHA1:
259 case AUTH_MET_SHA1:
260 /*
261 * Keyed SHA1 and Meticulous Keyed SHA1 Authentication Section Format
262 *
263 * 0 1 2 3
264 * 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
265 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
266 * | Auth Type | Auth Len | Auth Key ID | Reserved |
267 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
268 * | Sequence Number |
269 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
270 * | Auth Key/Hash... |
271 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
272 * | ... |
273 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
274 */
275 if (auth_len != AUTH_SHA1_FIELD_LEN) {
276 ND_PRINT("[invalid length %u]",
277 auth_len);
278 break;
279 }
280 pptr += 2;
281 ND_PRINT(", Sequence Number: 0x%08x", GET_BE_U_4(pptr));
282 pptr += 4;
283 ND_TCHECK_LEN(pptr, AUTH_SHA1_HASH_LEN);
284 ND_PRINT("\n\t Hash: ");
285 for(i = 0; i < AUTH_SHA1_HASH_LEN; i++)
286 ND_PRINT("%02x", GET_U_1(pptr + i));
287 break;
288 }
289 return 0;
290
291 trunc:
292 return 1;
293 }
294
295 void
296 bfd_print(netdissect_options *ndo, const u_char *pptr,
297 u_int len, u_int port)
298 {
299 ndo->ndo_protocol = "bfd";
300 if (port == BFD_CONTROL_PORT ||
301 port == BFD_MULTIHOP_PORT ||
302 port == BFD_LAG_PORT) {
303 /*
304 * Control packet.
305 */
306 const struct bfd_header_t *bfd_header;
307 uint8_t version_diag;
308 uint8_t version = 0;
309 uint8_t flags;
310
311 bfd_header = (const struct bfd_header_t *)pptr;
312 ND_TCHECK_SIZE(bfd_header);
313 version_diag = GET_U_1(bfd_header->version_diag);
314 version = BFD_EXTRACT_VERSION(version_diag);
315 flags = GET_U_1(bfd_header->flags);
316
317 switch (version) {
318
319 /* BFDv0 */
320 case 0:
321 if (ndo->ndo_vflag < 1)
322 {
323 ND_PRINT("BFDv0, Control, Flags: [%s], length: %u",
324 bittok2str(bfd_v0_flag_values, "none", flags),
325 len);
326 return;
327 }
328
329 ND_PRINT("BFDv0, length: %u\n\tControl, Flags: [%s], Diagnostic: %s (0x%02x)",
330 len,
331 bittok2str(bfd_v0_flag_values, "none", flags),
332 tok2str(bfd_diag_values,"unknown",BFD_EXTRACT_DIAG(version_diag)),
333 BFD_EXTRACT_DIAG(version_diag));
334
335 ND_PRINT("\n\tDetection Timer Multiplier: %u (%u ms Detection time), BFD Length: %u",
336 GET_U_1(bfd_header->detect_time_multiplier),
337 GET_U_1(bfd_header->detect_time_multiplier) * GET_BE_U_4(bfd_header->desired_min_tx_interval)/1000,
338 GET_U_1(bfd_header->length));
339
340
341 ND_PRINT("\n\tMy Discriminator: 0x%08x",
342 GET_BE_U_4(bfd_header->my_discriminator));
343 ND_PRINT(", Your Discriminator: 0x%08x",
344 GET_BE_U_4(bfd_header->your_discriminator));
345 ND_PRINT("\n\t Desired min Tx Interval: %4u ms",
346 GET_BE_U_4(bfd_header->desired_min_tx_interval)/1000);
347 ND_PRINT("\n\t Required min Rx Interval: %4u ms",
348 GET_BE_U_4(bfd_header->required_min_rx_interval)/1000);
349 ND_PRINT("\n\t Required min Echo Interval: %4u ms",
350 GET_BE_U_4(bfd_header->required_min_echo_interval)/1000);
351
352 if (flags & BFD_FLAG_AUTH) {
353 if (auth_print(ndo, pptr))
354 goto trunc;
355 }
356 break;
357
358 /* BFDv1 */
359 case 1:
360 if (ndo->ndo_vflag < 1)
361 {
362 ND_PRINT("BFDv1, %s, State %s, Flags: [%s], length: %u",
363 tok2str(bfd_port_values, "unknown (%u)", port),
364 tok2str(bfd_v1_state_values, "unknown (%u)", (flags & 0xc0) >> 6),
365 bittok2str(bfd_v1_flag_values, "none", flags & 0x3f),
366 len);
367 return;
368 }
369
370 ND_PRINT("BFDv1, length: %u\n\t%s, State %s, Flags: [%s], Diagnostic: %s (0x%02x)",
371 len,
372 tok2str(bfd_port_values, "unknown (%u)", port),
373 tok2str(bfd_v1_state_values, "unknown (%u)", (flags & 0xc0) >> 6),
374 bittok2str(bfd_v1_flag_values, "none", flags & 0x3f),
375 tok2str(bfd_diag_values,"unknown",BFD_EXTRACT_DIAG(version_diag)),
376 BFD_EXTRACT_DIAG(version_diag));
377
378 ND_PRINT("\n\tDetection Timer Multiplier: %u (%u ms Detection time), BFD Length: %u",
379 GET_U_1(bfd_header->detect_time_multiplier),
380 GET_U_1(bfd_header->detect_time_multiplier) * GET_BE_U_4(bfd_header->desired_min_tx_interval)/1000,
381 GET_U_1(bfd_header->length));
382
383
384 ND_PRINT("\n\tMy Discriminator: 0x%08x",
385 GET_BE_U_4(bfd_header->my_discriminator));
386 ND_PRINT(", Your Discriminator: 0x%08x",
387 GET_BE_U_4(bfd_header->your_discriminator));
388 ND_PRINT("\n\t Desired min Tx Interval: %4u ms",
389 GET_BE_U_4(bfd_header->desired_min_tx_interval)/1000);
390 ND_PRINT("\n\t Required min Rx Interval: %4u ms",
391 GET_BE_U_4(bfd_header->required_min_rx_interval)/1000);
392 ND_PRINT("\n\t Required min Echo Interval: %4u ms",
393 GET_BE_U_4(bfd_header->required_min_echo_interval)/1000);
394
395 if (flags & BFD_FLAG_AUTH) {
396 if (auth_print(ndo, pptr))
397 goto trunc;
398 }
399 break;
400
401 default:
402 ND_PRINT("BFDv%u, Control, length: %u",
403 version,
404 len);
405 if (ndo->ndo_vflag >= 1) {
406 if(!print_unknown_data(ndo, pptr,"\n\t",len))
407 return;
408 }
409 break;
410 }
411 } else if (port == BFD_ECHO_PORT) {
412 /*
413 * Echo packet.
414 */
415 ND_PRINT("BFD, Echo, length: %u",
416 len);
417 if (ndo->ndo_vflag >= 1) {
418 if(!print_unknown_data(ndo, pptr,"\n\t",len))
419 return;
420 }
421 } else {
422 /*
423 * Unknown packet type.
424 */
425 ND_PRINT("BFD, unknown (%u), length: %u",
426 port,
427 len);
428 if (ndo->ndo_vflag >= 1) {
429 if(!print_unknown_data(ndo, pptr,"\n\t",len))
430 return;
431 }
432 }
433 return;
434
435 trunc:
436 nd_print_trunc(ndo);
437 }