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