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