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