]> The Tcpdump Group git mirrors - tcpdump/blob - print-bfd.c
Fix spaces
[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 #define BFD_FLAG_AUTH 0x04
146
147 static const struct tok bfd_v0_flag_values[] = {
148 { 0x80, "I Hear You" },
149 { 0x40, "Demand" },
150 { 0x20, "Poll" },
151 { 0x10, "Final" },
152 { 0x08, "Control Plane Independent" },
153 { BFD_FLAG_AUTH, "Authentication Present" },
154 { 0x02, "Reserved" },
155 { 0x01, "Reserved" },
156 { 0, NULL }
157 };
158
159 static const struct tok bfd_v1_flag_values[] = {
160 { 0x20, "Poll" },
161 { 0x10, "Final" },
162 { 0x08, "Control Plane Independent" },
163 { BFD_FLAG_AUTH, "Authentication Present" },
164 { 0x02, "Demand" },
165 { 0x01, "Multipoint" },
166 { 0, NULL }
167 };
168
169 static const struct tok bfd_v1_state_values[] = {
170 { 0, "AdminDown" },
171 { 1, "Down" },
172 { 2, "Init" },
173 { 3, "Up" },
174 { 0, NULL }
175 };
176
177 static int
178 auth_print(netdissect_options *ndo, const u_char *pptr)
179 {
180 const struct bfd_auth_header_t *bfd_auth_header;
181 uint8_t auth_type, auth_len;
182 int i;
183
184 pptr += sizeof (struct bfd_header_t);
185 bfd_auth_header = (const struct bfd_auth_header_t *)pptr;
186 ND_TCHECK_SIZE(bfd_auth_header);
187 auth_type = EXTRACT_U_1(bfd_auth_header->auth_type);
188 auth_len = EXTRACT_U_1(bfd_auth_header->auth_len);
189 ND_PRINT("\n\tAuthentication: %s (%u), length: %u",
190 tok2str(bfd_v1_authentication_values,"Unknown",auth_type),
191 auth_type, auth_len);
192 pptr += 2;
193 ND_PRINT("\n\t Auth Key ID: %u", EXTRACT_U_1(pptr));
194
195 switch(auth_type) {
196 case AUTH_PASSWORD:
197 /*
198 * Simple Password Authentication Section Format
199 *
200 * 0 1 2 3
201 * 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
202 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
203 * | Auth Type | Auth Len | Auth Key ID | Password... |
204 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
205 * | ... |
206 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
207 */
208 if (auth_len < AUTH_PASSWORD_FIELD_MIN_LEN ||
209 auth_len > AUTH_PASSWORD_FIELD_MAX_LEN) {
210 ND_PRINT("[invalid length %u]",
211 auth_len);
212 break;
213 }
214 pptr++;
215 ND_PRINT(", Password: ");
216 /* the length is equal to the password length plus three */
217 if (nd_printn(ndo, pptr, auth_len - 3,
218 ndo->ndo_snapend))
219 goto trunc;
220 break;
221 case AUTH_MD5:
222 case AUTH_MET_MD5:
223 /*
224 * Keyed MD5 and Meticulous Keyed MD5 Authentication Section Format
225 *
226 * 0 1 2 3
227 * 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
228 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
229 * | Auth Type | Auth Len | Auth Key ID | Reserved |
230 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
231 * | Sequence Number |
232 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
233 * | Auth Key/Digest... |
234 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
235 * | ... |
236 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
237 */
238 if (auth_len != AUTH_MD5_FIELD_LEN) {
239 ND_PRINT("[invalid length %u]",
240 auth_len);
241 break;
242 }
243 pptr += 2;
244 ND_TCHECK_4(pptr);
245 ND_PRINT(", Sequence Number: 0x%08x", EXTRACT_BE_U_4(pptr));
246 pptr += 4;
247 ND_TCHECK_LEN(pptr, AUTH_MD5_HASH_LEN);
248 ND_PRINT("\n\t Digest: ");
249 for(i = 0; i < AUTH_MD5_HASH_LEN; i++)
250 ND_PRINT("%02x", EXTRACT_U_1(pptr + i));
251 break;
252 case AUTH_SHA1:
253 case AUTH_MET_SHA1:
254 /*
255 * Keyed SHA1 and Meticulous Keyed SHA1 Authentication Section Format
256 *
257 * 0 1 2 3
258 * 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
259 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
260 * | Auth Type | Auth Len | Auth Key ID | Reserved |
261 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
262 * | Sequence Number |
263 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
264 * | Auth Key/Hash... |
265 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
266 * | ... |
267 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
268 */
269 if (auth_len != AUTH_SHA1_FIELD_LEN) {
270 ND_PRINT("[invalid length %u]",
271 auth_len);
272 break;
273 }
274 pptr += 2;
275 ND_TCHECK_4(pptr);
276 ND_PRINT(", Sequence Number: 0x%08x", EXTRACT_BE_U_4(pptr));
277 pptr += 4;
278 ND_TCHECK_LEN(pptr, AUTH_SHA1_HASH_LEN);
279 ND_PRINT("\n\t Hash: ");
280 for(i = 0; i < AUTH_SHA1_HASH_LEN; i++)
281 ND_PRINT("%02x", EXTRACT_U_1(pptr + i));
282 break;
283 }
284 return 0;
285
286 trunc:
287 return 1;
288 }
289
290 void
291 bfd_print(netdissect_options *ndo, const u_char *pptr,
292 u_int len, u_int port)
293 {
294 ndo->ndo_protocol = "bfd";
295 if (port == BFD_CONTROL_PORT) {
296 /*
297 * Control packet.
298 */
299 const struct bfd_header_t *bfd_header;
300 uint8_t version_diag;
301 uint8_t version = 0;
302 uint8_t flags;
303
304 bfd_header = (const struct bfd_header_t *)pptr;
305 ND_TCHECK_SIZE(bfd_header);
306 version_diag = EXTRACT_U_1(bfd_header->version_diag);
307 version = BFD_EXTRACT_VERSION(version_diag);
308 flags = EXTRACT_U_1(bfd_header->flags);
309
310 switch (version) {
311
312 /* BFDv0 */
313 case 0:
314 if (ndo->ndo_vflag < 1)
315 {
316 ND_PRINT("BFDv0, Control, Flags: [%s], length: %u",
317 bittok2str(bfd_v0_flag_values, "none", flags),
318 len);
319 return;
320 }
321
322 ND_PRINT("BFDv0, length: %u\n\tControl, Flags: [%s], Diagnostic: %s (0x%02x)",
323 len,
324 bittok2str(bfd_v0_flag_values, "none", flags),
325 tok2str(bfd_diag_values,"unknown",BFD_EXTRACT_DIAG(version_diag)),
326 BFD_EXTRACT_DIAG(version_diag));
327
328 ND_PRINT("\n\tDetection Timer Multiplier: %u (%u ms Detection time), BFD Length: %u",
329 EXTRACT_U_1(bfd_header->detect_time_multiplier),
330 EXTRACT_U_1(bfd_header->detect_time_multiplier) * EXTRACT_BE_U_4(bfd_header->desired_min_tx_interval)/1000,
331 EXTRACT_U_1(bfd_header->length));
332
333
334 ND_PRINT("\n\tMy Discriminator: 0x%08x", EXTRACT_BE_U_4(bfd_header->my_discriminator));
335 ND_PRINT(", Your Discriminator: 0x%08x", EXTRACT_BE_U_4(bfd_header->your_discriminator));
336 ND_PRINT("\n\t Desired min Tx Interval: %4u ms", EXTRACT_BE_U_4(bfd_header->desired_min_tx_interval)/1000);
337 ND_PRINT("\n\t Required min Rx Interval: %4u ms", EXTRACT_BE_U_4(bfd_header->required_min_rx_interval)/1000);
338 ND_PRINT("\n\t Required min Echo Interval: %4u ms", EXTRACT_BE_U_4(bfd_header->required_min_echo_interval)/1000);
339
340 if (flags & BFD_FLAG_AUTH) {
341 if (auth_print(ndo, pptr))
342 goto trunc;
343 }
344 break;
345
346 /* BFDv1 */
347 case 1:
348 if (ndo->ndo_vflag < 1)
349 {
350 ND_PRINT("BFDv1, Control, State %s, Flags: [%s], length: %u",
351 tok2str(bfd_v1_state_values, "unknown (%u)", (flags & 0xc0) >> 6),
352 bittok2str(bfd_v1_flag_values, "none", flags & 0x3f),
353 len);
354 return;
355 }
356
357 ND_PRINT("BFDv1, length: %u\n\tControl, State %s, Flags: [%s], Diagnostic: %s (0x%02x)",
358 len,
359 tok2str(bfd_v1_state_values, "unknown (%u)", (flags & 0xc0) >> 6),
360 bittok2str(bfd_v1_flag_values, "none", flags & 0x3f),
361 tok2str(bfd_diag_values,"unknown",BFD_EXTRACT_DIAG(version_diag)),
362 BFD_EXTRACT_DIAG(version_diag));
363
364 ND_PRINT("\n\tDetection Timer Multiplier: %u (%u ms Detection time), BFD Length: %u",
365 EXTRACT_U_1(bfd_header->detect_time_multiplier),
366 EXTRACT_U_1(bfd_header->detect_time_multiplier) * EXTRACT_BE_U_4(bfd_header->desired_min_tx_interval)/1000,
367 EXTRACT_U_1(bfd_header->length));
368
369
370 ND_PRINT("\n\tMy Discriminator: 0x%08x", EXTRACT_BE_U_4(bfd_header->my_discriminator));
371 ND_PRINT(", Your Discriminator: 0x%08x", EXTRACT_BE_U_4(bfd_header->your_discriminator));
372 ND_PRINT("\n\t Desired min Tx Interval: %4u ms", EXTRACT_BE_U_4(bfd_header->desired_min_tx_interval)/1000);
373 ND_PRINT("\n\t Required min Rx Interval: %4u ms", EXTRACT_BE_U_4(bfd_header->required_min_rx_interval)/1000);
374 ND_PRINT("\n\t Required min Echo Interval: %4u ms", EXTRACT_BE_U_4(bfd_header->required_min_echo_interval)/1000);
375
376 if (flags & BFD_FLAG_AUTH) {
377 if (auth_print(ndo, pptr))
378 goto trunc;
379 }
380 break;
381
382 default:
383 ND_PRINT("BFDv%u, Control, length: %u",
384 version,
385 len);
386 if (ndo->ndo_vflag >= 1) {
387 if(!print_unknown_data(ndo, pptr,"\n\t",len))
388 return;
389 }
390 break;
391 }
392 } else if (port == BFD_ECHO_PORT) {
393 /*
394 * Echo packet.
395 */
396 ND_PRINT("BFD, Echo, length: %u",
397 len);
398 if (ndo->ndo_vflag >= 1) {
399 if(!print_unknown_data(ndo, pptr,"\n\t",len))
400 return;
401 }
402 } else {
403 /*
404 * Unknown packet type.
405 */
406 ND_PRINT("BFD, unknown (%u), length: %u",
407 port,
408 len);
409 if (ndo->ndo_vflag >= 1) {
410 if(!print_unknown_data(ndo, pptr,"\n\t",len))
411 return;
412 }
413 }
414 return;
415
416 trunc:
417 nd_print_trunc(ndo);
418 }