]> The Tcpdump Group git mirrors - tcpdump/blob - print-bfd.c
CI: Add warning exemptions for Sun C (suncc-5.14) on Solaris 10
[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 #include <config.h>
24
25 #include "netdissect-stdinc.h"
26
27 #define ND_LONGJMP_FROM_TCHECK
28 #include "netdissect.h"
29 #include "extract.h"
30
31 #include "udp.h"
32
33 /*
34 * Control packet, BFDv0, draft-ietf-bfd-base-01
35 *
36 * 0 1 2 3
37 * 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
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * |Vers | Diag |H|D|P|F|C|A|Rsv| Detect Mult | Length |
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | My Discriminator |
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * | Your Discriminator |
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * | Desired Min TX Interval |
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 * | Required Min RX Interval |
48 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 * | Required Min Echo RX Interval |
50 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 */
52
53 /*
54 * Control packet, BFDv1, RFC 5880
55 *
56 * 0 1 2 3
57 * 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
58 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 * |Vers | Diag |Sta|P|F|C|A|D|M| Detect Mult | Length |
60 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 * | My Discriminator |
62 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 * | Your Discriminator |
64 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65 * | Desired Min TX Interval |
66 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67 * | Required Min RX Interval |
68 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69 * | Required Min Echo RX Interval |
70 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71 */
72
73 struct bfd_header_t {
74 nd_uint8_t version_diag;
75 nd_uint8_t flags;
76 nd_uint8_t detect_time_multiplier;
77 nd_uint8_t length;
78 nd_uint32_t my_discriminator;
79 nd_uint32_t your_discriminator;
80 nd_uint32_t desired_min_tx_interval;
81 nd_uint32_t required_min_rx_interval;
82 nd_uint32_t required_min_echo_interval;
83 };
84
85 /*
86 * An optional Authentication Header may be present
87 *
88 * 0 1 2 3
89 * 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
90 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 * | Auth Type | Auth Len | Authentication Data... |
92 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 */
94
95 struct bfd_auth_header_t {
96 nd_uint8_t auth_type;
97 nd_uint8_t auth_len;
98 nd_uint8_t auth_data;
99 nd_uint8_t dummy; /* minimum 4 bytes */
100 };
101
102 enum auth_type {
103 AUTH_PASSWORD = 1,
104 AUTH_MD5 = 2,
105 AUTH_MET_MD5 = 3,
106 AUTH_SHA1 = 4,
107 AUTH_MET_SHA1 = 5
108 };
109
110 static const struct tok bfd_v1_authentication_values[] = {
111 { AUTH_PASSWORD, "Simple Password" },
112 { AUTH_MD5, "Keyed MD5" },
113 { AUTH_MET_MD5, "Meticulous Keyed MD5" },
114 { AUTH_SHA1, "Keyed SHA1" },
115 { AUTH_MET_SHA1, "Meticulous Keyed SHA1" },
116 { 0, NULL }
117 };
118
119 enum auth_length {
120 AUTH_PASSWORD_FIELD_MIN_LEN = 4, /* header + password min: 3 + 1 */
121 AUTH_PASSWORD_FIELD_MAX_LEN = 19, /* header + password max: 3 + 16 */
122 AUTH_MD5_FIELD_LEN = 24,
123 AUTH_MD5_HASH_LEN = 16,
124 AUTH_SHA1_FIELD_LEN = 28,
125 AUTH_SHA1_HASH_LEN = 20
126 };
127
128 #define BFD_EXTRACT_VERSION(x) (((x)&0xe0)>>5)
129 #define BFD_EXTRACT_DIAG(x) ((x)&0x1f)
130
131 static const struct tok bfd_diag_values[] = {
132 { 0, "No Diagnostic" },
133 { 1, "Control Detection Time Expired" },
134 { 2, "Echo Function Failed" },
135 { 3, "Neighbor Signaled Session Down" },
136 { 4, "Forwarding Plane Reset" },
137 { 5, "Path Down" },
138 { 6, "Concatenated Path Down" },
139 { 7, "Administratively Down" },
140 { 8, "Reverse Concatenated Path Down" },
141 { 0, NULL }
142 };
143
144 static const struct tok bfd_port_values[] = {
145 { BFD_CONTROL_PORT, "Control" },
146 { BFD_MULTIHOP_PORT, "Multihop" },
147 { BFD_LAG_PORT, "LAG" },
148 { SBFD_PORT, "S-BFD" },
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 void
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 nd_printjn(ndo, pptr, auth_len - 3);
225 break;
226 case AUTH_MD5:
227 case AUTH_MET_MD5:
228 /*
229 * Keyed MD5 and Meticulous Keyed MD5 Authentication Section Format
230 *
231 * 0 1 2 3
232 * 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
233 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
234 * | Auth Type | Auth Len | Auth Key ID | Reserved |
235 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
236 * | Sequence Number |
237 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
238 * | Auth Key/Digest... |
239 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
240 * | ... |
241 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
242 */
243 if (auth_len != AUTH_MD5_FIELD_LEN) {
244 ND_PRINT("[invalid length %u]",
245 auth_len);
246 break;
247 }
248 pptr += 2;
249 ND_PRINT(", Sequence Number: 0x%08x", GET_BE_U_4(pptr));
250 pptr += 4;
251 ND_TCHECK_LEN(pptr, AUTH_MD5_HASH_LEN);
252 ND_PRINT("\n\t Digest: ");
253 for(i = 0; i < AUTH_MD5_HASH_LEN; i++)
254 ND_PRINT("%02x", GET_U_1(pptr + i));
255 break;
256 case AUTH_SHA1:
257 case AUTH_MET_SHA1:
258 /*
259 * Keyed SHA1 and Meticulous Keyed SHA1 Authentication Section Format
260 *
261 * 0 1 2 3
262 * 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
263 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
264 * | Auth Type | Auth Len | Auth Key ID | Reserved |
265 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
266 * | Sequence Number |
267 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
268 * | Auth Key/Hash... |
269 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
270 * | ... |
271 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
272 */
273 if (auth_len != AUTH_SHA1_FIELD_LEN) {
274 ND_PRINT("[invalid length %u]",
275 auth_len);
276 break;
277 }
278 pptr += 2;
279 ND_PRINT(", Sequence Number: 0x%08x", GET_BE_U_4(pptr));
280 pptr += 4;
281 ND_TCHECK_LEN(pptr, AUTH_SHA1_HASH_LEN);
282 ND_PRINT("\n\t Hash: ");
283 for(i = 0; i < AUTH_SHA1_HASH_LEN; i++)
284 ND_PRINT("%02x", GET_U_1(pptr + i));
285 break;
286 }
287 }
288
289 void
290 bfd_print(netdissect_options *ndo, const u_char *pptr,
291 u_int len, u_int port)
292 {
293 ndo->ndo_protocol = "bfd";
294 if (port == BFD_CONTROL_PORT ||
295 port == BFD_MULTIHOP_PORT ||
296 port == BFD_LAG_PORT ||
297 port == SBFD_PORT) {
298 /*
299 * Control packet.
300 */
301 const struct bfd_header_t *bfd_header;
302 uint8_t version_diag;
303 uint8_t version = 0;
304 uint8_t flags;
305
306 bfd_header = (const struct bfd_header_t *)pptr;
307 ND_TCHECK_SIZE(bfd_header);
308 version_diag = GET_U_1(bfd_header->version_diag);
309 version = BFD_EXTRACT_VERSION(version_diag);
310 flags = GET_U_1(bfd_header->flags);
311
312 switch (version) {
313
314 /* BFDv0 */
315 case 0:
316 if (ndo->ndo_vflag < 1) {
317 ND_PRINT("BFDv0, Control, Flags: [%s], length: %u",
318 bittok2str(bfd_v0_flag_values, "none", flags),
319 len);
320 return;
321 }
322
323 ND_PRINT("BFDv0, length: %u\n\tControl, Flags: [%s], Diagnostic: %s (0x%02x)",
324 len,
325 bittok2str(bfd_v0_flag_values, "none", flags),
326 tok2str(bfd_diag_values,"unknown",BFD_EXTRACT_DIAG(version_diag)),
327 BFD_EXTRACT_DIAG(version_diag));
328
329 ND_PRINT("\n\tDetection Timer Multiplier: %u (%u ms Detection time), BFD Length: %u",
330 GET_U_1(bfd_header->detect_time_multiplier),
331 GET_U_1(bfd_header->detect_time_multiplier) * GET_BE_U_4(bfd_header->desired_min_tx_interval)/1000,
332 GET_U_1(bfd_header->length));
333
334
335 ND_PRINT("\n\tMy Discriminator: 0x%08x",
336 GET_BE_U_4(bfd_header->my_discriminator));
337 ND_PRINT(", Your Discriminator: 0x%08x",
338 GET_BE_U_4(bfd_header->your_discriminator));
339 ND_PRINT("\n\t Desired min Tx Interval: %4u ms",
340 GET_BE_U_4(bfd_header->desired_min_tx_interval)/1000);
341 ND_PRINT("\n\t Required min Rx Interval: %4u ms",
342 GET_BE_U_4(bfd_header->required_min_rx_interval)/1000);
343 ND_PRINT("\n\t Required min Echo Interval: %4u ms",
344 GET_BE_U_4(bfd_header->required_min_echo_interval)/1000);
345
346 if (flags & BFD_FLAG_AUTH) {
347 auth_print(ndo, pptr);
348 }
349 break;
350
351 /* BFDv1 */
352 case 1:
353 if (ndo->ndo_vflag < 1) {
354 ND_PRINT("BFDv1, %s, State %s, Flags: [%s], length: %u",
355 tok2str(bfd_port_values, "unknown (%u)", port),
356 tok2str(bfd_v1_state_values, "unknown (%u)", (flags & 0xc0) >> 6),
357 bittok2str(bfd_v1_flag_values, "none", flags & 0x3f),
358 len);
359 return;
360 }
361
362 ND_PRINT("BFDv1, length: %u\n\t%s, State %s, Flags: [%s], Diagnostic: %s (0x%02x)",
363 len,
364 tok2str(bfd_port_values, "unknown (%u)", port),
365 tok2str(bfd_v1_state_values, "unknown (%u)", (flags & 0xc0) >> 6),
366 bittok2str(bfd_v1_flag_values, "none", flags & 0x3f),
367 tok2str(bfd_diag_values,"unknown",BFD_EXTRACT_DIAG(version_diag)),
368 BFD_EXTRACT_DIAG(version_diag));
369
370 ND_PRINT("\n\tDetection Timer Multiplier: %u (%u ms Detection time), BFD Length: %u",
371 GET_U_1(bfd_header->detect_time_multiplier),
372 GET_U_1(bfd_header->detect_time_multiplier) * GET_BE_U_4(bfd_header->desired_min_tx_interval)/1000,
373 GET_U_1(bfd_header->length));
374
375
376 ND_PRINT("\n\tMy Discriminator: 0x%08x",
377 GET_BE_U_4(bfd_header->my_discriminator));
378 ND_PRINT(", Your Discriminator: 0x%08x",
379 GET_BE_U_4(bfd_header->your_discriminator));
380 ND_PRINT("\n\t Desired min Tx Interval: %4u ms",
381 GET_BE_U_4(bfd_header->desired_min_tx_interval)/1000);
382 ND_PRINT("\n\t Required min Rx Interval: %4u ms",
383 GET_BE_U_4(bfd_header->required_min_rx_interval)/1000);
384 ND_PRINT("\n\t Required min Echo Interval: %4u ms",
385 GET_BE_U_4(bfd_header->required_min_echo_interval)/1000);
386
387 if (flags & BFD_FLAG_AUTH) {
388 auth_print(ndo, pptr);
389 }
390 break;
391
392 default:
393 ND_PRINT("BFDv%u, Control, length: %u",
394 version,
395 len);
396 if (ndo->ndo_vflag >= 1) {
397 if(!print_unknown_data(ndo, pptr,"\n\t",len))
398 return;
399 }
400 break;
401 }
402 } else if (port == BFD_ECHO_PORT) {
403 /*
404 * Echo packet.
405 */
406 ND_PRINT("BFD, Echo, length: %u",
407 len);
408 if (ndo->ndo_vflag >= 1) {
409 if(!print_unknown_data(ndo, pptr,"\n\t",len))
410 return;
411 }
412 } else {
413 /*
414 * Unknown packet type.
415 */
416 ND_PRINT("BFD, unknown (%u), length: %u",
417 port,
418 len);
419 if (ndo->ndo_vflag >= 1) {
420 if(!print_unknown_data(ndo, pptr,"\n\t",len))
421 return;
422 }
423 }
424 }