]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/tcpdump/print-bfd.c
ping: fix data type of a variable for a packet sequence number
[FreeBSD/FreeBSD.git] / contrib / 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 /* specification: RFC 5880 (for version 1) and RFC 5881 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <netdissect-stdinc.h>
25
26 #include "netdissect.h"
27 #include "extract.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, RFC 5880
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|M|  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     uint8_t version_diag;
73     uint8_t flags;
74     uint8_t detect_time_multiplier;
75     uint8_t length;
76     uint8_t my_discriminator[4];
77     uint8_t your_discriminator[4];
78     uint8_t desired_min_tx_interval[4];
79     uint8_t required_min_rx_interval[4];
80     uint8_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     uint8_t auth_type;
95     uint8_t auth_len;
96     uint8_t auth_data;
97     uint8_t dummy; /* minimun 4 bytes */
98 };
99
100 enum auth_type {
101     AUTH_PASSWORD = 1,
102     AUTH_MD5      = 2,
103     AUTH_MET_MD5  = 3,
104     AUTH_SHA1     = 4,
105     AUTH_MET_SHA1 = 5
106 };
107
108 static const struct tok bfd_v1_authentication_values[] = {
109     { AUTH_PASSWORD, "Simple Password" },
110     { AUTH_MD5,      "Keyed MD5" },
111     { AUTH_MET_MD5,  "Meticulous Keyed MD5" },
112     { AUTH_SHA1,     "Keyed SHA1" },
113     { AUTH_MET_SHA1, "Meticulous Keyed SHA1" },
114     { 0, NULL }
115 };
116
117 enum auth_length {
118     AUTH_PASSWORD_FIELD_MIN_LEN = 4,  /* header + password min: 3 + 1 */
119     AUTH_PASSWORD_FIELD_MAX_LEN = 19, /* header + password max: 3 + 16 */
120     AUTH_MD5_FIELD_LEN  = 24,
121     AUTH_MD5_HASH_LEN   = 16,
122     AUTH_SHA1_FIELD_LEN = 28,
123     AUTH_SHA1_HASH_LEN  = 20
124 };
125
126 #define BFD_EXTRACT_VERSION(x) (((x)&0xe0)>>5)
127 #define BFD_EXTRACT_DIAG(x)     ((x)&0x1f)
128
129 static const struct tok bfd_port_values[] = {
130     { BFD_CONTROL_PORT, "Control" },
131     { BFD_ECHO_PORT,    "Echo" },
132     { 0, NULL }
133 };
134
135 static const struct tok bfd_diag_values[] = {
136     { 0, "No Diagnostic" },
137     { 1, "Control Detection Time Expired" },
138     { 2, "Echo Function Failed" },
139     { 3, "Neighbor Signaled Session Down" },
140     { 4, "Forwarding Plane Reset" },
141     { 5, "Path Down" },
142     { 6, "Concatenated Path Down" },
143     { 7, "Administratively Down" },
144     { 8, "Reverse Concatenated Path Down" },
145     { 0, NULL }
146 };
147
148 static const struct tok bfd_v0_flag_values[] = {
149     { 0x80, "I Hear You" },
150     { 0x40, "Demand" },
151     { 0x20, "Poll" },
152     { 0x10, "Final" },
153     { 0x08, "Reserved" },
154     { 0x04, "Reserved" },
155     { 0x02, "Reserved" },
156     { 0x01, "Reserved" },
157     { 0, NULL }
158 };
159
160 #define BFD_FLAG_AUTH 0x04
161
162 static const struct tok bfd_v1_flag_values[] = {
163     { 0x20, "Poll" },
164     { 0x10, "Final" },
165     { 0x08, "Control Plane Independent" },
166     { BFD_FLAG_AUTH, "Authentication Present" },
167     { 0x02, "Demand" },
168     { 0x01, "Multipoint" },
169     { 0, NULL }
170 };
171
172 static const struct tok bfd_v1_state_values[] = {
173     { 0, "AdminDown" },
174     { 1, "Down" },
175     { 2, "Init" },
176     { 3, "Up" },
177     { 0, NULL }
178 };
179
180 static int
181 auth_print(netdissect_options *ndo, register const u_char *pptr)
182 {
183         const struct bfd_auth_header_t *bfd_auth_header;
184         int i;
185
186         pptr += sizeof (const struct bfd_header_t);
187         bfd_auth_header = (const struct bfd_auth_header_t *)pptr;
188         ND_TCHECK(*bfd_auth_header);
189         ND_PRINT((ndo, "\n\tAuthentication: %s (%u), length: %u",
190                  tok2str(bfd_v1_authentication_values,"Unknown",bfd_auth_header->auth_type),
191                  bfd_auth_header->auth_type,
192                  bfd_auth_header->auth_len));
193                 pptr += 2;
194                 ND_PRINT((ndo, "\n\t  Auth Key ID: %d", *pptr));
195
196         switch(bfd_auth_header->auth_type) {
197             case AUTH_PASSWORD:
198 /*
199  *    Simple Password Authentication Section Format
200  *
201  *     0                   1                   2                   3
202  *     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
203  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
204  *    |   Auth Type   |   Auth Len    |  Auth Key ID  |  Password...  |
205  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
206  *    |                              ...                              |
207  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
208  */
209                 if (bfd_auth_header->auth_len < AUTH_PASSWORD_FIELD_MIN_LEN ||
210                     bfd_auth_header->auth_len > AUTH_PASSWORD_FIELD_MAX_LEN) {
211                     ND_PRINT((ndo, "[invalid length %d]",
212                              bfd_auth_header->auth_len));
213                     break;
214                 }
215                 pptr++;
216                 ND_PRINT((ndo, ", Password: "));
217                 /* the length is equal to the password length plus three */
218                 if (fn_printn(ndo, pptr, bfd_auth_header->auth_len - 3,
219                               ndo->ndo_snapend))
220                     goto trunc;
221                 break;
222             case AUTH_MD5:
223             case AUTH_MET_MD5:
224 /*
225  *    Keyed MD5 and Meticulous Keyed MD5 Authentication Section Format
226  *
227  *     0                   1                   2                   3
228  *     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
229  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
230  *    |   Auth Type   |   Auth Len    |  Auth Key ID  |   Reserved    |
231  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
232  *    |                        Sequence Number                        |
233  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
234  *    |                      Auth Key/Digest...                       |
235  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
236  *    |                              ...                              |
237  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
238  */
239                 if (bfd_auth_header->auth_len != AUTH_MD5_FIELD_LEN) {
240                     ND_PRINT((ndo, "[invalid length %d]",
241                              bfd_auth_header->auth_len));
242                     break;
243                 }
244                 pptr += 2;
245                 ND_TCHECK2(*pptr, 4);
246                 ND_PRINT((ndo, ", Sequence Number: 0x%08x", EXTRACT_32BITS(pptr)));
247                 pptr += 4;
248                 ND_TCHECK2(*pptr, AUTH_MD5_HASH_LEN);
249                 ND_PRINT((ndo, "\n\t  Digest: "));
250                 for(i = 0; i < AUTH_MD5_HASH_LEN; i++)
251                     ND_PRINT((ndo, "%02x", pptr[i]));
252                 break;
253             case AUTH_SHA1:
254             case AUTH_MET_SHA1:
255 /*
256  *    Keyed SHA1 and Meticulous Keyed SHA1 Authentication Section Format
257  *
258  *     0                   1                   2                   3
259  *     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
260  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
261  *    |   Auth Type   |   Auth Len    |  Auth Key ID  |   Reserved    |
262  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
263  *    |                        Sequence Number                        |
264  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
265  *    |                       Auth Key/Hash...                        |
266  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
267  *    |                              ...                              |
268  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
269  */
270                 if (bfd_auth_header->auth_len != AUTH_SHA1_FIELD_LEN) {
271                     ND_PRINT((ndo, "[invalid length %d]",
272                              bfd_auth_header->auth_len));
273                     break;
274                 }
275                 pptr += 2;
276                 ND_TCHECK2(*pptr, 4);
277                 ND_PRINT((ndo, ", Sequence Number: 0x%08x", EXTRACT_32BITS(pptr)));
278                 pptr += 4;
279                 ND_TCHECK2(*pptr, AUTH_SHA1_HASH_LEN);
280                 ND_PRINT((ndo, "\n\t  Hash: "));
281                 for(i = 0; i < AUTH_SHA1_HASH_LEN; i++)
282                     ND_PRINT((ndo, "%02x", pptr[i]));
283                 break;
284         }
285         return 0;
286
287 trunc:
288         return 1;
289 }
290
291 void
292 bfd_print(netdissect_options *ndo, register const u_char *pptr,
293           register u_int len, register u_int port)
294 {
295         const struct bfd_header_t *bfd_header;
296         uint8_t version = 0;
297
298         bfd_header = (const struct bfd_header_t *)pptr;
299         if (port == BFD_CONTROL_PORT) {
300             ND_TCHECK(*bfd_header);
301             version = BFD_EXTRACT_VERSION(bfd_header->version_diag);
302         } else if (port == BFD_ECHO_PORT) {
303             /* Echo is BFD v1 only */
304             version = 1;
305         }
306         switch ((port << 8) | version) {
307
308             /* BFDv0 */
309         case (BFD_CONTROL_PORT << 8):
310             if (ndo->ndo_vflag < 1)
311             {
312                 ND_PRINT((ndo, "BFDv%u, %s, Flags: [%s], length: %u",
313                        version,
314                        tok2str(bfd_port_values, "unknown (%u)", port),
315                        bittok2str(bfd_v0_flag_values, "none", bfd_header->flags),
316                        len));
317                 return;
318             }
319
320             ND_PRINT((ndo, "BFDv%u, length: %u\n\t%s, Flags: [%s], Diagnostic: %s (0x%02x)",
321                    version,
322                    len,
323                    tok2str(bfd_port_values, "unknown (%u)", port),
324                    bittok2str(bfd_v0_flag_values, "none", bfd_header->flags),
325                    tok2str(bfd_diag_values,"unknown",BFD_EXTRACT_DIAG(bfd_header->version_diag)),
326                    BFD_EXTRACT_DIAG(bfd_header->version_diag)));
327
328             ND_PRINT((ndo, "\n\tDetection Timer Multiplier: %u (%u ms Detection time), BFD Length: %u",
329                    bfd_header->detect_time_multiplier,
330                    bfd_header->detect_time_multiplier * EXTRACT_32BITS(bfd_header->desired_min_tx_interval)/1000,
331                    bfd_header->length));
332
333
334             ND_PRINT((ndo, "\n\tMy Discriminator: 0x%08x", EXTRACT_32BITS(bfd_header->my_discriminator)));
335             ND_PRINT((ndo, ", Your Discriminator: 0x%08x", EXTRACT_32BITS(bfd_header->your_discriminator)));
336             ND_PRINT((ndo, "\n\t  Desired min Tx Interval:    %4u ms", EXTRACT_32BITS(bfd_header->desired_min_tx_interval)/1000));
337             ND_PRINT((ndo, "\n\t  Required min Rx Interval:   %4u ms", EXTRACT_32BITS(bfd_header->required_min_rx_interval)/1000));
338             ND_PRINT((ndo, "\n\t  Required min Echo Interval: %4u ms", EXTRACT_32BITS(bfd_header->required_min_echo_interval)/1000));
339             break;
340
341             /* BFDv1 */
342         case (BFD_CONTROL_PORT << 8 | 1):
343             if (ndo->ndo_vflag < 1)
344             {
345                 ND_PRINT((ndo, "BFDv%u, %s, State %s, Flags: [%s], length: %u",
346                        version,
347                        tok2str(bfd_port_values, "unknown (%u)", port),
348                        tok2str(bfd_v1_state_values, "unknown (%u)", (bfd_header->flags & 0xc0) >> 6),
349                        bittok2str(bfd_v1_flag_values, "none", bfd_header->flags & 0x3f),
350                        len));
351                 return;
352             }
353
354             ND_PRINT((ndo, "BFDv%u, length: %u\n\t%s, State %s, Flags: [%s], Diagnostic: %s (0x%02x)",
355                    version,
356                    len,
357                    tok2str(bfd_port_values, "unknown (%u)", port),
358                    tok2str(bfd_v1_state_values, "unknown (%u)", (bfd_header->flags & 0xc0) >> 6),
359                    bittok2str(bfd_v1_flag_values, "none", bfd_header->flags & 0x3f),
360                    tok2str(bfd_diag_values,"unknown",BFD_EXTRACT_DIAG(bfd_header->version_diag)),
361                    BFD_EXTRACT_DIAG(bfd_header->version_diag)));
362
363             ND_PRINT((ndo, "\n\tDetection Timer Multiplier: %u (%u ms Detection time), BFD Length: %u",
364                    bfd_header->detect_time_multiplier,
365                    bfd_header->detect_time_multiplier * EXTRACT_32BITS(bfd_header->desired_min_tx_interval)/1000,
366                    bfd_header->length));
367
368
369             ND_PRINT((ndo, "\n\tMy Discriminator: 0x%08x", EXTRACT_32BITS(bfd_header->my_discriminator)));
370             ND_PRINT((ndo, ", Your Discriminator: 0x%08x", EXTRACT_32BITS(bfd_header->your_discriminator)));
371             ND_PRINT((ndo, "\n\t  Desired min Tx Interval:    %4u ms", EXTRACT_32BITS(bfd_header->desired_min_tx_interval)/1000));
372             ND_PRINT((ndo, "\n\t  Required min Rx Interval:   %4u ms", EXTRACT_32BITS(bfd_header->required_min_rx_interval)/1000));
373             ND_PRINT((ndo, "\n\t  Required min Echo Interval: %4u ms", EXTRACT_32BITS(bfd_header->required_min_echo_interval)/1000));
374
375             if (bfd_header->flags & BFD_FLAG_AUTH) {
376                 if (auth_print(ndo, pptr))
377                     goto trunc;
378             }
379             break;
380
381             /* BFDv0 */
382         case (BFD_ECHO_PORT << 8): /* not yet supported - fall through */
383             /* BFDv1 */
384         case (BFD_ECHO_PORT << 8 | 1):
385
386         default:
387             ND_PRINT((ndo, "BFD, %s, length: %u",
388                    tok2str(bfd_port_values, "unknown (%u)", port),
389                    len));
390             if (ndo->ndo_vflag >= 1) {
391                     if(!print_unknown_data(ndo, pptr,"\n\t",len))
392                             return;
393             }
394             break;
395         }
396         return;
397
398 trunc:
399         ND_PRINT((ndo, "[|BFD]"));
400 }
401 /*
402  * Local Variables:
403  * c-style: whitesmith
404  * c-basic-offset: 8
405  * End:
406  */