1127668Sbms/*
2127668Sbms * Redistribution and use in source and binary forms, with or without
3127668Sbms * modification, are permitted provided that: (1) source code
4127668Sbms * distributions retain the above copyright notice and this paragraph
5127668Sbms * in its entirety, and (2) distributions including binary code include
6127668Sbms * the above copyright notice and this paragraph in its entirety in
7127668Sbms * the documentation or other materials provided with the distribution.
8127668Sbms * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
9127668Sbms * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
10127668Sbms * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
11127668Sbms * FOR A PARTICULAR PURPOSE.
12127668Sbms *
13127668Sbms * Original code by Hannes Gredler (hannes@juniper.net)
14127668Sbms */
15127668Sbms
16127668Sbms#ifndef lint
17127668Sbmsstatic const char rcsid[] _U_ =
18190207Srpaulo    "@(#) $Header: /tcpdump/master/tcpdump/print-bfd.c,v 1.10 2006-02-02 06:35:52 hannes Exp $";
19127668Sbms#endif
20127668Sbms
21127668Sbms#ifdef HAVE_CONFIG_H
22127668Sbms#include "config.h"
23127668Sbms#endif
24127668Sbms
25127668Sbms#include <tcpdump-stdinc.h>
26127668Sbms
27127668Sbms#include <stdio.h>
28127668Sbms#include <stdlib.h>
29127668Sbms
30127668Sbms#include "interface.h"
31127668Sbms#include "extract.h"
32127668Sbms#include "addrtoname.h"
33127668Sbms
34127668Sbms#include "udp.h"
35127668Sbms
36127668Sbms/*
37147899Ssam * Control packet, BFDv0, draft-katz-ward-bfd-01.txt
38127668Sbms *
39127668Sbms *     0                   1                   2                   3
40127668Sbms *     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
41127668Sbms *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42127668Sbms *    |Vers |  Diag   |H|D|P|F| Rsvd  |  Detect Mult  |    Length     |
43127668Sbms *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44127668Sbms *    |                       My Discriminator                        |
45127668Sbms *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46127668Sbms *    |                      Your Discriminator                       |
47127668Sbms *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48127668Sbms *    |                    Desired Min TX Interval                    |
49127668Sbms *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50127668Sbms *    |                   Required Min RX Interval                    |
51127668Sbms *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52127668Sbms *    |                 Required Min Echo RX Interval                 |
53127668Sbms *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54127668Sbms */
55127668Sbms
56147899Ssam/*
57147899Ssam *  Control packet, BFDv1, draft-ietf-bfd-base-02.txt
58147899Ssam *
59147899Ssam *     0                   1                   2                   3
60147899Ssam *     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
61147899Ssam *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62147899Ssam *    |Vers |  Diag   |Sta|P|F|C|A|D|R|  Detect Mult  |    Length     |
63147899Ssam *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64147899Ssam *    |                       My Discriminator                        |
65147899Ssam *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66147899Ssam *    |                      Your Discriminator                       |
67147899Ssam *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68147899Ssam *    |                    Desired Min TX Interval                    |
69147899Ssam *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70147899Ssam *    |                   Required Min RX Interval                    |
71147899Ssam *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72147899Ssam *    |                 Required Min Echo RX Interval                 |
73147899Ssam *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74147899Ssam */
75147899Ssam
76127668Sbmsstruct bfd_header_t {
77127668Sbms    u_int8_t version_diag;
78127668Sbms    u_int8_t flags;
79127668Sbms    u_int8_t detect_time_multiplier;
80127668Sbms    u_int8_t length;
81127668Sbms    u_int8_t my_discriminator[4];
82127668Sbms    u_int8_t your_discriminator[4];
83127668Sbms    u_int8_t desired_min_tx_interval[4];
84127668Sbms    u_int8_t required_min_rx_interval[4];
85127668Sbms    u_int8_t required_min_echo_interval[4];
86127668Sbms};
87127668Sbms
88147899Ssam/*
89147899Ssam *    An optional Authentication Header may be present
90147899Ssam *
91147899Ssam *     0                   1                   2                   3
92147899Ssam *     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
93147899Ssam *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94147899Ssam *    |   Auth Type   |   Auth Len    |    Authentication Data...     |
95147899Ssam *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96147899Ssam */
97147899Ssam
98147899Ssamstruct bfd_auth_header_t {
99147899Ssam    u_int8_t auth_type;
100147899Ssam    u_int8_t auth_len;
101147899Ssam    u_int8_t auth_data;
102147899Ssam};
103147899Ssam
104147899Ssamstatic const struct tok bfd_v1_authentication_values[] = {
105147899Ssam    { 0,        "Reserved" },
106147899Ssam    { 1,        "Simple Password" },
107147899Ssam    { 2,        "Keyed MD5" },
108147899Ssam    { 3,        "Meticulous Keyed MD5" },
109147899Ssam    { 4,        "Keyed SHA1" },
110147899Ssam    { 5,        "Meticulous Keyed SHA1" },
111147899Ssam    { 0, NULL }
112147899Ssam};
113147899Ssam
114127668Sbms#define	BFD_EXTRACT_VERSION(x) (((x)&0xe0)>>5)
115127668Sbms#define	BFD_EXTRACT_DIAG(x)     ((x)&0x1f)
116127668Sbms
117127668Sbmsstatic const struct tok bfd_port_values[] = {
118127668Sbms    { BFD_CONTROL_PORT, "Control" },
119127668Sbms    { BFD_ECHO_PORT,    "Echo" },
120127668Sbms    { 0, NULL }
121127668Sbms};
122127668Sbms
123127668Sbms
124127668Sbmsstatic const struct tok bfd_diag_values[] = {
125127668Sbms    { 0, "No Diagnostic" },
126127668Sbms    { 1, "Control Detection Time Expired" },
127127668Sbms    { 2, "Echo Function Failed" },
128127668Sbms    { 3, "Neighbor Signaled Session Down" },
129127668Sbms    { 4, "Forwarding Plane Reset" },
130127668Sbms    { 5, "Path Down" },
131127668Sbms    { 6, "Concatenated Path Down" },
132127668Sbms    { 7, "Administratively Down" },
133147899Ssam    { 8, "Reverse Concatenated Path Down" },
134127668Sbms    { 0, NULL }
135127668Sbms};
136127668Sbms
137147899Ssamstatic const struct tok bfd_v0_flag_values[] = {
138127668Sbms    { 0x80,	"I Hear You" },
139127668Sbms    { 0x40,	"Demand" },
140127668Sbms    { 0x20,	"Poll" },
141127668Sbms    { 0x10,	"Final" },
142127668Sbms    { 0x08,	"Reserved" },
143127668Sbms    { 0x04,	"Reserved" },
144127668Sbms    { 0x02,	"Reserved" },
145127668Sbms    { 0x01,	"Reserved" },
146127668Sbms    { 0, NULL }
147127668Sbms};
148127668Sbms
149147899Ssam#define BFD_FLAG_AUTH 0x04
150147899Ssam
151147899Ssamstatic const struct tok bfd_v1_flag_values[] = {
152147899Ssam    { 0x20, "Poll" },
153147899Ssam    { 0x10, "Final" },
154147899Ssam    { 0x08, "Control Plane Independent" },
155147899Ssam    { BFD_FLAG_AUTH, "Authentication Present" },
156147899Ssam    { 0x02, "Demand" },
157147899Ssam    { 0x01, "Reserved" },
158147899Ssam    { 0, NULL }
159147899Ssam};
160147899Ssam
161147899Ssamstatic const struct tok bfd_v1_state_values[] = {
162147899Ssam    { 0, "AdminDown" },
163147899Ssam    { 1, "Down" },
164147899Ssam    { 2, "Init" },
165147899Ssam    { 3, "Up" },
166147899Ssam    { 0, NULL }
167147899Ssam};
168147899Ssam
169127668Sbmsvoid
170127668Sbmsbfd_print(register const u_char *pptr, register u_int len, register u_int port)
171127668Sbms{
172127668Sbms        const struct bfd_header_t *bfd_header;
173147899Ssam        const struct bfd_auth_header_t *bfd_auth_header;
174214478Srpaulo        u_int8_t version = 0;
175127668Sbms
176127668Sbms        bfd_header = (const struct bfd_header_t *)pptr;
177214478Srpaulo        if (port == BFD_CONTROL_PORT) {
178214478Srpaulo            TCHECK(*bfd_header);
179214478Srpaulo            version = BFD_EXTRACT_VERSION(bfd_header->version_diag);
180214478Srpaulo        } else if (port == BFD_ECHO_PORT) {
181214478Srpaulo            /* Echo is BFD v1 only */
182214478Srpaulo            version = 1;
183214478Srpaulo        }
184214478Srpaulo        switch ((port << 8) | version) {
185127668Sbms
186147899Ssam            /* BFDv0 */
187147899Ssam        case (BFD_CONTROL_PORT << 8):
188127668Sbms            if (vflag < 1 )
189127668Sbms            {
190127668Sbms                printf("BFDv%u, %s, Flags: [%s], length: %u",
191147899Ssam                       version,
192127668Sbms                       tok2str(bfd_port_values, "unknown (%u)", port),
193147899Ssam                       bittok2str(bfd_v0_flag_values, "none", bfd_header->flags),
194127668Sbms                       len);
195127668Sbms                return;
196127668Sbms            }
197127668Sbms
198127668Sbms            printf("BFDv%u, length: %u\n\t%s, Flags: [%s], Diagnostic: %s (0x%02x)",
199147899Ssam                   version,
200127668Sbms                   len,
201127668Sbms                   tok2str(bfd_port_values, "unknown (%u)", port),
202147899Ssam                   bittok2str(bfd_v0_flag_values, "none", bfd_header->flags),
203127668Sbms                   tok2str(bfd_diag_values,"unknown",BFD_EXTRACT_DIAG(bfd_header->version_diag)),
204147899Ssam                   BFD_EXTRACT_DIAG(bfd_header->version_diag));
205127668Sbms
206127668Sbms            printf("\n\tDetection Timer Multiplier: %u (%u ms Detection time), BFD Length: %u",
207127668Sbms                   bfd_header->detect_time_multiplier,
208127668Sbms                   bfd_header->detect_time_multiplier * EXTRACT_32BITS(bfd_header->desired_min_tx_interval)/1000,
209127668Sbms                   bfd_header->length);
210127668Sbms
211127668Sbms
212127668Sbms            printf("\n\tMy Discriminator: 0x%08x", EXTRACT_32BITS(bfd_header->my_discriminator));
213127668Sbms            printf(", Your Discriminator: 0x%08x", EXTRACT_32BITS(bfd_header->your_discriminator));
214127668Sbms            printf("\n\t  Desired min Tx Interval:    %4u ms", EXTRACT_32BITS(bfd_header->desired_min_tx_interval)/1000);
215127668Sbms            printf("\n\t  Required min Rx Interval:   %4u ms", EXTRACT_32BITS(bfd_header->required_min_rx_interval)/1000);
216127668Sbms            printf("\n\t  Required min Echo Interval: %4u ms", EXTRACT_32BITS(bfd_header->required_min_echo_interval)/1000);
217127668Sbms            break;
218127668Sbms
219147899Ssam            /* BFDv1 */
220147899Ssam        case (BFD_CONTROL_PORT << 8 | 1):
221147899Ssam            if (vflag < 1 )
222147899Ssam            {
223147899Ssam                printf("BFDv%u, %s, State %s, Flags: [%s], length: %u",
224147899Ssam                       version,
225147899Ssam                       tok2str(bfd_port_values, "unknown (%u)", port),
226172683Smlaier                       tok2str(bfd_v1_state_values, "unknown (%u)", (bfd_header->flags & 0xc0) >> 6),
227147899Ssam                       bittok2str(bfd_v1_flag_values, "none", bfd_header->flags & 0x3f),
228147899Ssam                       len);
229147899Ssam                return;
230147899Ssam            }
231147899Ssam
232147899Ssam            printf("BFDv%u, length: %u\n\t%s, State %s, Flags: [%s], Diagnostic: %s (0x%02x)",
233147899Ssam                   version,
234147899Ssam                   len,
235147899Ssam                   tok2str(bfd_port_values, "unknown (%u)", port),
236147899Ssam                   tok2str(bfd_v1_state_values, "unknown (%u)", (bfd_header->flags & 0xc0) >> 6),
237147899Ssam                   bittok2str(bfd_v1_flag_values, "none", bfd_header->flags & 0x3f),
238147899Ssam                   tok2str(bfd_diag_values,"unknown",BFD_EXTRACT_DIAG(bfd_header->version_diag)),
239147899Ssam                   BFD_EXTRACT_DIAG(bfd_header->version_diag));
240147899Ssam
241147899Ssam            printf("\n\tDetection Timer Multiplier: %u (%u ms Detection time), BFD Length: %u",
242147899Ssam                   bfd_header->detect_time_multiplier,
243147899Ssam                   bfd_header->detect_time_multiplier * EXTRACT_32BITS(bfd_header->desired_min_tx_interval)/1000,
244147899Ssam                   bfd_header->length);
245127668Sbms
246147899Ssam
247147899Ssam            printf("\n\tMy Discriminator: 0x%08x", EXTRACT_32BITS(bfd_header->my_discriminator));
248147899Ssam            printf(", Your Discriminator: 0x%08x", EXTRACT_32BITS(bfd_header->your_discriminator));
249147899Ssam            printf("\n\t  Desired min Tx Interval:    %4u ms", EXTRACT_32BITS(bfd_header->desired_min_tx_interval)/1000);
250147899Ssam            printf("\n\t  Required min Rx Interval:   %4u ms", EXTRACT_32BITS(bfd_header->required_min_rx_interval)/1000);
251147899Ssam            printf("\n\t  Required min Echo Interval: %4u ms", EXTRACT_32BITS(bfd_header->required_min_echo_interval)/1000);
252147899Ssam
253147899Ssam            if (bfd_header->flags & BFD_FLAG_AUTH) {
254147899Ssam                pptr += sizeof (const struct bfd_header_t);
255147899Ssam                bfd_auth_header = (const struct bfd_auth_header_t *)pptr;
256147899Ssam                TCHECK2(*bfd_auth_header, sizeof(const struct bfd_auth_header_t));
257147899Ssam                printf("\n\t%s (%u) Authentication, length %u present",
258147899Ssam                       tok2str(bfd_v1_authentication_values,"Unknown",bfd_auth_header->auth_type),
259147899Ssam                       bfd_auth_header->auth_type,
260147899Ssam                       bfd_auth_header->auth_len);
261147899Ssam            }
262147899Ssam            break;
263147899Ssam
264147899Ssam            /* BFDv0 */
265147899Ssam        case (BFD_ECHO_PORT << 8): /* not yet supported - fall through */
266147899Ssam            /* BFDv1 */
267147899Ssam        case (BFD_ECHO_PORT << 8 | 1):
268147899Ssam
269127668Sbms        default:
270127668Sbms            printf("BFD, %s, length: %u",
271127668Sbms                   tok2str(bfd_port_values, "unknown (%u)", port),
272127668Sbms                   len);
273127668Sbms            if (vflag >= 1) {
274127668Sbms                if(!print_unknown_data(pptr,"\n\t",len))
275127668Sbms                    return;
276127668Sbms            }
277127668Sbms            break;
278127668Sbms        }
279127668Sbms        return;
280127668Sbms
281127668Sbmstrunc:
282127668Sbms        printf("[|BFD]");
283127668Sbms}
284