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@juniper.net)
14 */
15
16#ifndef lint
17static const char rcsid[] _U_ =
18    "@(#) $Header: /tcpdump/master/tcpdump/print-bfd.c,v 1.10 2006-02-02 06:35:52 hannes Exp $";
19#endif
20
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include <tcpdump-stdinc.h>
26
27#include <stdio.h>
28#include <stdlib.h>
29
30#include "interface.h"
31#include "extract.h"
32#include "addrtoname.h"
33
34#include "udp.h"
35
36/*
37 * Control packet, BFDv0, draft-katz-ward-bfd-01.txt
38 *
39 *     0                   1                   2                   3
40 *     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
41 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 *    |Vers |  Diag   |H|D|P|F| Rsvd  |  Detect Mult  |    Length     |
43 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 *    |                       My Discriminator                        |
45 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 *    |                      Your Discriminator                       |
47 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 *    |                    Desired Min TX Interval                    |
49 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 *    |                   Required Min RX Interval                    |
51 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 *    |                 Required Min Echo RX Interval                 |
53 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 */
55
56/*
57 *  Control packet, BFDv1, draft-ietf-bfd-base-02.txt
58 *
59 *     0                   1                   2                   3
60 *     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
61 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62 *    |Vers |  Diag   |Sta|P|F|C|A|D|R|  Detect Mult  |    Length     |
63 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 *    |                       My Discriminator                        |
65 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66 *    |                      Your Discriminator                       |
67 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68 *    |                    Desired Min TX Interval                    |
69 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70 *    |                   Required Min RX Interval                    |
71 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72 *    |                 Required Min Echo RX Interval                 |
73 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74 */
75
76struct bfd_header_t {
77    u_int8_t version_diag;
78    u_int8_t flags;
79    u_int8_t detect_time_multiplier;
80    u_int8_t length;
81    u_int8_t my_discriminator[4];
82    u_int8_t your_discriminator[4];
83    u_int8_t desired_min_tx_interval[4];
84    u_int8_t required_min_rx_interval[4];
85    u_int8_t required_min_echo_interval[4];
86};
87
88/*
89 *    An optional Authentication Header may be present
90 *
91 *     0                   1                   2                   3
92 *     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
93 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 *    |   Auth Type   |   Auth Len    |    Authentication Data...     |
95 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 */
97
98struct bfd_auth_header_t {
99    u_int8_t auth_type;
100    u_int8_t auth_len;
101    u_int8_t auth_data;
102};
103
104static const struct tok bfd_v1_authentication_values[] = {
105    { 0,        "Reserved" },
106    { 1,        "Simple Password" },
107    { 2,        "Keyed MD5" },
108    { 3,        "Meticulous Keyed MD5" },
109    { 4,        "Keyed SHA1" },
110    { 5,        "Meticulous Keyed SHA1" },
111    { 0, NULL }
112};
113
114#define	BFD_EXTRACT_VERSION(x) (((x)&0xe0)>>5)
115#define	BFD_EXTRACT_DIAG(x)     ((x)&0x1f)
116
117static const struct tok bfd_port_values[] = {
118    { BFD_CONTROL_PORT, "Control" },
119    { BFD_ECHO_PORT,    "Echo" },
120    { 0, NULL }
121};
122
123
124static const struct tok bfd_diag_values[] = {
125    { 0, "No Diagnostic" },
126    { 1, "Control Detection Time Expired" },
127    { 2, "Echo Function Failed" },
128    { 3, "Neighbor Signaled Session Down" },
129    { 4, "Forwarding Plane Reset" },
130    { 5, "Path Down" },
131    { 6, "Concatenated Path Down" },
132    { 7, "Administratively Down" },
133    { 8, "Reverse Concatenated Path Down" },
134    { 0, NULL }
135};
136
137static const struct tok bfd_v0_flag_values[] = {
138    { 0x80,	"I Hear You" },
139    { 0x40,	"Demand" },
140    { 0x20,	"Poll" },
141    { 0x10,	"Final" },
142    { 0x08,	"Reserved" },
143    { 0x04,	"Reserved" },
144    { 0x02,	"Reserved" },
145    { 0x01,	"Reserved" },
146    { 0, NULL }
147};
148
149#define BFD_FLAG_AUTH 0x04
150
151static const struct tok bfd_v1_flag_values[] = {
152    { 0x20, "Poll" },
153    { 0x10, "Final" },
154    { 0x08, "Control Plane Independent" },
155    { BFD_FLAG_AUTH, "Authentication Present" },
156    { 0x02, "Demand" },
157    { 0x01, "Reserved" },
158    { 0, NULL }
159};
160
161static const struct tok bfd_v1_state_values[] = {
162    { 0, "AdminDown" },
163    { 1, "Down" },
164    { 2, "Init" },
165    { 3, "Up" },
166    { 0, NULL }
167};
168
169void
170bfd_print(register const u_char *pptr, register u_int len, register u_int port)
171{
172        const struct bfd_header_t *bfd_header;
173        const struct bfd_auth_header_t *bfd_auth_header;
174        u_int8_t version = 0;
175
176        bfd_header = (const struct bfd_header_t *)pptr;
177        if (port == BFD_CONTROL_PORT) {
178            TCHECK(*bfd_header);
179            version = BFD_EXTRACT_VERSION(bfd_header->version_diag);
180        } else if (port == BFD_ECHO_PORT) {
181            /* Echo is BFD v1 only */
182            version = 1;
183        }
184        switch ((port << 8) | version) {
185
186            /* BFDv0 */
187        case (BFD_CONTROL_PORT << 8):
188            if (vflag < 1 )
189            {
190                printf("BFDv%u, %s, Flags: [%s], length: %u",
191                       version,
192                       tok2str(bfd_port_values, "unknown (%u)", port),
193                       bittok2str(bfd_v0_flag_values, "none", bfd_header->flags),
194                       len);
195                return;
196            }
197
198            printf("BFDv%u, length: %u\n\t%s, Flags: [%s], Diagnostic: %s (0x%02x)",
199                   version,
200                   len,
201                   tok2str(bfd_port_values, "unknown (%u)", port),
202                   bittok2str(bfd_v0_flag_values, "none", bfd_header->flags),
203                   tok2str(bfd_diag_values,"unknown",BFD_EXTRACT_DIAG(bfd_header->version_diag)),
204                   BFD_EXTRACT_DIAG(bfd_header->version_diag));
205
206            printf("\n\tDetection Timer Multiplier: %u (%u ms Detection time), BFD Length: %u",
207                   bfd_header->detect_time_multiplier,
208                   bfd_header->detect_time_multiplier * EXTRACT_32BITS(bfd_header->desired_min_tx_interval)/1000,
209                   bfd_header->length);
210
211
212            printf("\n\tMy Discriminator: 0x%08x", EXTRACT_32BITS(bfd_header->my_discriminator));
213            printf(", Your Discriminator: 0x%08x", EXTRACT_32BITS(bfd_header->your_discriminator));
214            printf("\n\t  Desired min Tx Interval:    %4u ms", EXTRACT_32BITS(bfd_header->desired_min_tx_interval)/1000);
215            printf("\n\t  Required min Rx Interval:   %4u ms", EXTRACT_32BITS(bfd_header->required_min_rx_interval)/1000);
216            printf("\n\t  Required min Echo Interval: %4u ms", EXTRACT_32BITS(bfd_header->required_min_echo_interval)/1000);
217            break;
218
219            /* BFDv1 */
220        case (BFD_CONTROL_PORT << 8 | 1):
221            if (vflag < 1 )
222            {
223                printf("BFDv%u, %s, State %s, Flags: [%s], length: %u",
224                       version,
225                       tok2str(bfd_port_values, "unknown (%u)", port),
226                       tok2str(bfd_v1_state_values, "unknown (%u)", (bfd_header->flags & 0xc0) >> 6),
227                       bittok2str(bfd_v1_flag_values, "none", bfd_header->flags & 0x3f),
228                       len);
229                return;
230            }
231
232            printf("BFDv%u, length: %u\n\t%s, State %s, Flags: [%s], Diagnostic: %s (0x%02x)",
233                   version,
234                   len,
235                   tok2str(bfd_port_values, "unknown (%u)", port),
236                   tok2str(bfd_v1_state_values, "unknown (%u)", (bfd_header->flags & 0xc0) >> 6),
237                   bittok2str(bfd_v1_flag_values, "none", bfd_header->flags & 0x3f),
238                   tok2str(bfd_diag_values,"unknown",BFD_EXTRACT_DIAG(bfd_header->version_diag)),
239                   BFD_EXTRACT_DIAG(bfd_header->version_diag));
240
241            printf("\n\tDetection Timer Multiplier: %u (%u ms Detection time), BFD Length: %u",
242                   bfd_header->detect_time_multiplier,
243                   bfd_header->detect_time_multiplier * EXTRACT_32BITS(bfd_header->desired_min_tx_interval)/1000,
244                   bfd_header->length);
245
246
247            printf("\n\tMy Discriminator: 0x%08x", EXTRACT_32BITS(bfd_header->my_discriminator));
248            printf(", Your Discriminator: 0x%08x", EXTRACT_32BITS(bfd_header->your_discriminator));
249            printf("\n\t  Desired min Tx Interval:    %4u ms", EXTRACT_32BITS(bfd_header->desired_min_tx_interval)/1000);
250            printf("\n\t  Required min Rx Interval:   %4u ms", EXTRACT_32BITS(bfd_header->required_min_rx_interval)/1000);
251            printf("\n\t  Required min Echo Interval: %4u ms", EXTRACT_32BITS(bfd_header->required_min_echo_interval)/1000);
252
253            if (bfd_header->flags & BFD_FLAG_AUTH) {
254                pptr += sizeof (const struct bfd_header_t);
255                bfd_auth_header = (const struct bfd_auth_header_t *)pptr;
256                TCHECK2(*bfd_auth_header, sizeof(const struct bfd_auth_header_t));
257                printf("\n\t%s (%u) Authentication, length %u present",
258                       tok2str(bfd_v1_authentication_values,"Unknown",bfd_auth_header->auth_type),
259                       bfd_auth_header->auth_type,
260                       bfd_auth_header->auth_len);
261            }
262            break;
263
264            /* BFDv0 */
265        case (BFD_ECHO_PORT << 8): /* not yet supported - fall through */
266            /* BFDv1 */
267        case (BFD_ECHO_PORT << 8 | 1):
268
269        default:
270            printf("BFD, %s, length: %u",
271                   tok2str(bfd_port_values, "unknown (%u)", port),
272                   len);
273            if (vflag >= 1) {
274                if(!print_unknown_data(pptr,"\n\t",len))
275                    return;
276            }
277            break;
278        }
279        return;
280
281trunc:
282        printf("[|BFD]");
283}
284