xref: /freebsd/contrib/tcpdump/print-udld.c (revision 87b759f0fa1f7554d50ce640c40138512bbded44)
1 /*
2  * Copyright (c) 1998-2007 The TCPDUMP project
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that: (1) source code
6  * distributions retain the above copyright notice and this paragraph
7  * in its entirety, and (2) distributions including binary code include
8  * the above copyright notice and this paragraph in its entirety in
9  * the documentation or other materials provided with the distribution.
10  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
11  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
12  * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13  * FOR A PARTICULAR PURPOSE.
14  *
15  * Original code by Carles Kishimoto <carles.kishimoto@gmail.com>
16  */
17 
18 /* \summary: Cisco UniDirectional Link Detection (UDLD) protocol printer */
19 
20 /* specification: RFC 5171 */
21 
22 #include <config.h>
23 
24 #include "netdissect-stdinc.h"
25 
26 #define ND_LONGJMP_FROM_TCHECK
27 #include "netdissect.h"
28 #include "extract.h"
29 
30 
31 #define UDLD_HEADER_LEN			4
32 #define UDLD_TLV_HEADER_LEN		4
33 #define UDLD_DEVICE_ID_TLV		0x0001
34 #define UDLD_PORT_ID_TLV		0x0002
35 #define UDLD_ECHO_TLV			0x0003
36 #define UDLD_MESSAGE_INTERVAL_TLV	0x0004
37 #define UDLD_TIMEOUT_INTERVAL_TLV	0x0005
38 #define UDLD_DEVICE_NAME_TLV		0x0006
39 #define UDLD_SEQ_NUMBER_TLV		0x0007
40 
41 static const struct tok udld_tlv_values[] = {
42     { UDLD_DEVICE_ID_TLV, "Device-ID TLV"},
43     { UDLD_PORT_ID_TLV, "Port-ID TLV"},
44     { UDLD_ECHO_TLV, "Echo TLV"},
45     { UDLD_MESSAGE_INTERVAL_TLV, "Message Interval TLV"},
46     { UDLD_TIMEOUT_INTERVAL_TLV, "Timeout Interval TLV"},
47     { UDLD_DEVICE_NAME_TLV, "Device Name TLV"},
48     { UDLD_SEQ_NUMBER_TLV,"Sequence Number TLV"},
49     { 0, NULL}
50 };
51 
52 static const struct tok udld_code_values[] = {
53     { 0x00, "Reserved"},
54     { 0x01, "Probe message"},
55     { 0x02, "Echo message"},
56     { 0x03, "Flush message"},
57     { 0, NULL}
58 };
59 
60 static const struct tok udld_flags_bitmap_str[] = {
61     { 1U << 0, "RT"    },
62     { 1U << 1, "RSY"   },
63     { 1U << 2, "MBZ-2" },
64     { 1U << 3, "MBZ-3" },
65     { 1U << 4, "MBZ-4" },
66     { 1U << 5, "MBZ-5" },
67     { 1U << 6, "MBZ-6" },
68     { 1U << 7, "MBZ-7" },
69     { 0, NULL}
70 };
71 
72 /*
73  * UDLD's Protocol Data Unit format:
74  *
75  *  0                   1                   2                   3
76  *  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
77  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
78  * | Ver | Opcode  |     Flags     |           Checksum            |
79  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
80  * |               List of TLVs (variable length list)             |
81  * |                              ...                              |
82  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83  *
84  * TLV format:
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  * |             TYPE              |            LENGTH             |
90  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91  * |                             VALUE                             |
92  * |                              ...                              |
93  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94  *
95  * LENGTH: Length in bytes of the Type, Length, and Value fields.
96  */
97 
98 #define	UDLD_EXTRACT_VERSION(x) (((x)&0xe0)>>5)
99 #define	UDLD_EXTRACT_OPCODE(x) ((x)&0x1f)
100 
101 void
102 udld_print(netdissect_options *ndo,
103            const u_char *tptr, u_int length)
104 {
105     uint8_t ver, code, flags;
106 
107     ndo->ndo_protocol = "udld";
108     if (length < UDLD_HEADER_LEN)
109         goto invalid;
110 
111     ver = UDLD_EXTRACT_VERSION(GET_U_1(tptr));
112     code = UDLD_EXTRACT_OPCODE(GET_U_1(tptr));
113     tptr += 1;
114     length -= 1;
115 
116     flags = GET_U_1(tptr);
117     tptr += 1;
118     length -= 1;
119 
120     ND_PRINT("UDLDv%u, Code %s (%x), Flags [%s] (0x%02x), length %u",
121            ver,
122            tok2str(udld_code_values, "Reserved", code),
123            code,
124            bittok2str(udld_flags_bitmap_str, "none", flags),
125            flags,
126            length + 2);
127 
128     /*
129      * In non-verbose mode, just print version and opcode type
130      */
131     if (ndo->ndo_vflag < 1) {
132         goto tcheck_remainder;
133     }
134 
135     ND_PRINT("\n\tChecksum 0x%04x (unverified)", GET_BE_U_2(tptr));
136     tptr += 2;
137     length -= 2;
138 
139     while (length) {
140         uint16_t type, len;
141 
142         if (length < UDLD_TLV_HEADER_LEN)
143             goto invalid;
144 
145 	type = GET_BE_U_2(tptr);
146         tptr += 2;
147         length -= 2;
148 
149         len  = GET_BE_U_2(tptr);
150         tptr += 2;
151         length -= 2;
152 
153         ND_PRINT("\n\t%s (0x%04x) TLV, length %u",
154                tok2str(udld_tlv_values, "Unknown", type),
155                type, len);
156 
157         /* infinite loop check */
158         if (len <= UDLD_TLV_HEADER_LEN)
159             goto invalid;
160 
161         len -= UDLD_TLV_HEADER_LEN;
162         if (length < len)
163             goto invalid;
164 
165         switch (type) {
166         case UDLD_DEVICE_ID_TLV:
167         case UDLD_PORT_ID_TLV:
168         case UDLD_DEVICE_NAME_TLV:
169             ND_PRINT(", ");
170             nd_printjnp(ndo, tptr, len);
171             break;
172 
173         case UDLD_ECHO_TLV:
174             ND_PRINT(", ");
175             (void)nd_printn(ndo, tptr, len, NULL);
176             break;
177 
178         case UDLD_MESSAGE_INTERVAL_TLV:
179         case UDLD_TIMEOUT_INTERVAL_TLV:
180             if (len != 1)
181                 goto invalid;
182             ND_PRINT(", %us", (GET_U_1(tptr)));
183             break;
184 
185         case UDLD_SEQ_NUMBER_TLV:
186             if (len != 4)
187                 goto invalid;
188             ND_PRINT(", %u", GET_BE_U_4(tptr));
189             break;
190 
191         default:
192             ND_TCHECK_LEN(tptr, len);
193             break;
194         }
195         tptr += len;
196         length -= len;
197     }
198 
199     return;
200 
201 invalid:
202     nd_print_invalid(ndo);
203 tcheck_remainder:
204     ND_TCHECK_LEN(tptr, length);
205 }
206