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