]> The Tcpdump Group git mirrors - tcpdump/blob - print-udld.c
Printers must include 'netdissect.h', not 'interface.h'
[tcpdump] / print-udld.c
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 * UNIDIRECTIONAL LINK DETECTION (UDLD) as per
16 * http://www.ietf.org/internet-drafts/draft-foschiano-udld-02.txt
17 *
18 * Original code by Carles Kishimoto <carles.kishimoto@gmail.com>
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <tcpdump-stdinc.h>
26
27 #include "netdissect.h"
28 #include "extract.h"
29
30 #define UDLD_HEADER_LEN 4
31 #define UDLD_DEVICE_ID_TLV 0x0001
32 #define UDLD_PORT_ID_TLV 0x0002
33 #define UDLD_ECHO_TLV 0x0003
34 #define UDLD_MESSAGE_INTERVAL_TLV 0x0004
35 #define UDLD_TIMEOUT_INTERVAL_TLV 0x0005
36 #define UDLD_DEVICE_NAME_TLV 0x0006
37 #define UDLD_SEQ_NUMBER_TLV 0x0007
38
39 static const struct tok udld_tlv_values[] = {
40 { UDLD_DEVICE_ID_TLV, "Device-ID TLV"},
41 { UDLD_PORT_ID_TLV, "Port-ID TLV"},
42 { UDLD_ECHO_TLV, "Echo TLV"},
43 { UDLD_MESSAGE_INTERVAL_TLV, "Message Interval TLV"},
44 { UDLD_TIMEOUT_INTERVAL_TLV, "Timeout Interval TLV"},
45 { UDLD_DEVICE_NAME_TLV, "Device Name TLV"},
46 { UDLD_SEQ_NUMBER_TLV,"Sequence Number TLV"},
47 { 0, NULL}
48 };
49
50 static const struct tok udld_code_values[] = {
51 { 0x00, "Reserved"},
52 { 0x01, "Probe message"},
53 { 0x02, "Echo message"},
54 { 0x03, "Flush message"},
55 { 0, NULL}
56 };
57
58 static const struct tok udld_flags_values[] = {
59 { 0x00, "RT"},
60 { 0x01, "RSY"},
61 { 0, NULL}
62 };
63
64 /*
65 *
66 * 0 1 2 3
67 * 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
68 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69 * | Ver | Opcode | Flags | Checksum |
70 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71 * | List of TLVs (variable length list) |
72 * | ... |
73 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74 *
75 */
76
77 #define UDLD_EXTRACT_VERSION(x) (((x)&0xe0)>>5)
78 #define UDLD_EXTRACT_OPCODE(x) ((x)&0x1f)
79
80 void
81 udld_print (netdissect_options *ndo, const u_char *pptr, u_int length)
82 {
83 int code, type, len;
84 const u_char *tptr;
85
86 if (length < UDLD_HEADER_LEN)
87 goto trunc;
88
89 tptr = pptr;
90
91 ND_TCHECK2(*tptr, UDLD_HEADER_LEN);
92
93 code = UDLD_EXTRACT_OPCODE(*tptr);
94
95 ND_PRINT((ndo, "UDLDv%u, Code %s (%x), Flags [%s] (0x%02x), length %u",
96 UDLD_EXTRACT_VERSION(*tptr),
97 tok2str(udld_code_values, "Reserved", code),
98 code,
99 bittok2str(udld_flags_values, "none", *(tptr+1)),
100 *(tptr+1),
101 length));
102
103 /*
104 * In non-verbose mode, just print version and opcode type
105 */
106 if (ndo->ndo_vflag < 1) {
107 return;
108 }
109
110 ND_PRINT((ndo, "\n\tChecksum 0x%04x (unverified)", EXTRACT_16BITS(tptr+2)));
111
112 tptr += UDLD_HEADER_LEN;
113
114 while (tptr < (pptr+length)) {
115
116 ND_TCHECK2(*tptr, 4);
117
118 type = EXTRACT_16BITS(tptr);
119 len = EXTRACT_16BITS(tptr+2);
120 len -= 4;
121 tptr += 4;
122
123 /* infinite loop check */
124 if (type == 0 || len == 0) {
125 return;
126 }
127
128 ND_PRINT((ndo, "\n\t%s (0x%04x) TLV, length %u",
129 tok2str(udld_tlv_values, "Unknown", type),
130 type, len));
131
132 switch (type) {
133 case UDLD_DEVICE_ID_TLV:
134 case UDLD_PORT_ID_TLV:
135 case UDLD_ECHO_TLV:
136 case UDLD_DEVICE_NAME_TLV:
137 ND_PRINT((ndo, ", %s", tptr));
138 break;
139
140 case UDLD_MESSAGE_INTERVAL_TLV:
141 case UDLD_TIMEOUT_INTERVAL_TLV:
142 ND_PRINT((ndo, ", %us", (*tptr)));
143 break;
144
145 case UDLD_SEQ_NUMBER_TLV:
146 ND_PRINT((ndo, ", %u", EXTRACT_32BITS(tptr)));
147 break;
148
149 default:
150 break;
151 }
152 tptr += len;
153 }
154
155 return;
156
157 trunc:
158 ND_PRINT((ndo, "[|udld]"));
159 }
160
161 /*
162 * Local Variables:
163 * c-style: whitesmith
164 * c-basic-offset: 4
165 * End:
166 */