]> The Tcpdump Group git mirrors - tcpdump/blob - print-cfm.c
From Jun Kuriyama:
[tcpdump] / print-cfm.c
1 /*
2 * Copyright (c) 1998-2006 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 * Support for the IEEE Connectivity Fault Management Protocols as per 802.1ag.
16 *
17 * Original code by Hannes Gredler (hannes@juniper.net)
18 */
19
20 #ifndef lint
21 static const char rcsid[] _U_ =
22 "@(#) $Header: /tcpdump/master/tcpdump/print-cfm.c,v 1.2 2006-10-20 18:07:55 hannes Exp $";
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <tcpdump-stdinc.h>
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "interface.h"
36 #include "extract.h"
37 #include "ether.h"
38 #include "addrtoname.h"
39
40 struct cfm_common_header_t {
41 u_int8_t mdlevel_version;
42 u_int8_t opcode;
43 u_int8_t flags;
44 u_int8_t first_tlv_offset;
45 };
46
47 #define CFM_VERSION 0
48 #define CFM_EXTRACT_VERSION(x) (((x)&0x1f))
49 #define CFM_EXTRACT_MD_LEVEL(x) (((x)&0xe0)>>5)
50
51 #define CFM_OPCODE_CCM 1
52 #define CFM_OPCODE_LBR 2
53 #define CFM_OPCODE_LBM 3
54 #define CFM_OPCODE_LTR 4
55 #define CFM_OPCODE_LTM 5
56
57 static const struct tok cfm_opcode_values[] = {
58 { CFM_OPCODE_CCM, "Continouity Check Message"},
59 { CFM_OPCODE_LBR, "Loopback Reply"},
60 { CFM_OPCODE_LBM, "Loopback Message"},
61 { CFM_OPCODE_LTR, "Linktrace Reply"},
62 { CFM_OPCODE_LTM, "Linktrace Message"},
63 { 0, NULL}
64 };
65
66 /*
67 * Message Formats.
68 */
69 struct cfm_ccm_t {
70 u_int8_t sequence[4];
71 u_int8_t ma_epi[2];
72 u_int8_t md_nameformat;
73 u_int8_t md_namelength;
74 u_int8_t md_name[46]; /* md name and short ma name */
75 u_int8_t reserved_itu[16];
76 u_int8_t reserved[6];
77 };
78
79 /*
80 * Timer Bases for the CCM Interval field.
81 * Expressed in units of seconds.
82 */
83 const float ccm_interval_base[8] = {0, 0.003333, 0.01, 0.1, 1, 10, 60, 600};
84 #define CCM_INTERVAL_MIN_MULTIPLIER 3.25
85 #define CCM_INTERVAL_MAX_MULTIPLIER 3.5
86
87 #define CFM_CCM_RDI_FLAG 0x80
88 #define CFM_EXTRACT_CCM_INTERVAL(x) (((x)&0x07))
89
90 #define CFM_CCM_MD_FORMAT_8021 0
91 #define CFM_CCM_MD_FORMAT_NONE 1
92 #define CFM_CCM_MD_FORMAT_DNS 2
93 #define CFM_CCM_MD_FORMAT_MAC 3
94 #define CFM_CCM_MD_FORMAT_CHAR 4
95
96 static const struct tok cfm_md_nameformat_values[] = {
97 { CFM_CCM_MD_FORMAT_8021, "IEEE 802.1"},
98 { CFM_CCM_MD_FORMAT_NONE, "No MD Name present"},
99 { CFM_CCM_MD_FORMAT_DNS, "DNS string"},
100 { CFM_CCM_MD_FORMAT_MAC, "MAC + 16Bit Integer"},
101 { CFM_CCM_MD_FORMAT_CHAR, "Character string"},
102 { 0, NULL}
103 };
104
105 #define CFM_CCM_MA_FORMAT_8021 0
106 #define CFM_CCM_MA_FORMAT_VID 1
107 #define CFM_CCM_MA_FORMAT_CHAR 2
108 #define CFM_CCM_MA_FORMAT_INT 3
109 #define CFM_CCM_MA_FORMAT_VPN 4
110
111 static const struct tok cfm_ma_nameformat_values[] = {
112 { CFM_CCM_MA_FORMAT_8021, "IEEE 802.1"},
113 { CFM_CCM_MA_FORMAT_VID, "Primary VID"},
114 { CFM_CCM_MA_FORMAT_CHAR, "Character string"},
115 { CFM_CCM_MA_FORMAT_INT, "16Bit Integer"},
116 { CFM_CCM_MA_FORMAT_VPN, "RFC2685 VPN-ID"},
117 { 0, NULL}
118 };
119
120 struct cfm_lbm_t {
121 u_int8_t transaction_id[4];
122 u_int8_t reserved[4];
123 };
124
125 struct cfm_ltm_t {
126 u_int8_t transaction_id[4];
127 u_int8_t egress_id[8];
128 u_int8_t ttl;
129 u_int8_t original_mac[ETHER_ADDR_LEN];
130 u_int8_t target_mac[ETHER_ADDR_LEN];
131 u_int8_t reserved[3];
132 };
133
134 struct cfm_ltr_t {
135 u_int8_t transaction_id[4];
136 u_int8_t last_egress_id[8];
137 u_int8_t next_egress_id[8];
138 u_int8_t ttl;
139 u_int8_t replay_action;
140 u_int8_t reserved[6];
141 };
142
143 #define CFM_TLV_END 0
144 #define CFM_TLV_SENDER_ID 1
145 #define CFM_TLV_PORT_STATUS 2
146 #define CFM_TLV_INTERFACE_STATUS 3
147 #define CFM_TLV_DATA 4
148 #define CFM_TLV_REPLY_INGRESS 5
149 #define CFM_TLV_REPLY_EGRESS 6
150 #define CFM_TLV_PRIVATE 31
151
152 static const struct tok cfm_tlv_values[] = {
153 { CFM_TLV_END, "End"},
154 { CFM_TLV_SENDER_ID, "Sender ID"},
155 { CFM_TLV_PORT_STATUS, "Port status"},
156 { CFM_TLV_INTERFACE_STATUS, "Interface status"},
157 { CFM_TLV_DATA, "Data"},
158 { CFM_TLV_REPLY_INGRESS, "Reply Ingress"},
159 { CFM_TLV_REPLY_EGRESS, "Reply Egress"},
160 { CFM_TLV_PRIVATE, "Organization Specific"},
161 { 0, NULL}
162 };
163
164 /*
165 * TLVs
166 */
167
168 struct cfm_tlv_header_t {
169 u_int8_t type;
170 u_int8_t length[2];
171 };
172
173 /* FIXME define TLV formats */
174
175 static const struct tok cfm_tlv_port_status_values[] = {
176 { 1, "Blocked"},
177 { 2, "Up"},
178 { 0, NULL}
179 };
180
181 static const struct tok cfm_tlv_interface_status_values[] = {
182 { 1, "Up"},
183 { 2, "Down"},
184 { 3, "Testing"},
185 { 5, "Dormant"},
186 { 6, "not present"},
187 { 7, "lower Layer down"},
188 { 0, NULL}
189 };
190
191 void
192 cfm_print(register const u_char *pptr, register u_int length) {
193
194 const struct cfm_common_header_t *cfm_common_header;
195 const struct cfm_tlv_header_t *cfm_tlv_header;
196 const u_int8_t *tptr, *ma_name, *ma_nameformat, *ma_namelength;
197 u_int tlen, cfm_tlv_len, cfm_tlv_type, ccm_interval;
198
199
200 union {
201 const struct cfm_ccm_t *cfm_ccm;
202 const struct cfm_lbm_t *cfm_lbm;
203 const struct cfm_ltm_t *cfm_ltm;
204 const struct cfm_ltr_t *cfm_ltr;
205 } msg_ptr;
206
207 tptr=pptr;
208 cfm_common_header = (const struct cfm_common_header_t *)pptr;
209 TCHECK(*cfm_common_header);
210
211 /*
212 * Sanity checking of the header.
213 */
214 if (CFM_EXTRACT_VERSION(cfm_common_header->mdlevel_version) != CFM_VERSION) {
215 printf("CFMv%u not supported, length %u",
216 CFM_EXTRACT_VERSION(cfm_common_header->mdlevel_version), length);
217 return;
218 }
219
220 printf("CFMv%u %s, length %u",
221 CFM_EXTRACT_VERSION(cfm_common_header->mdlevel_version),
222 tok2str(cfm_opcode_values, "unknown (%u)", cfm_common_header->opcode),
223 length);
224
225 /*
226 * In non-verbose mode just lets print the opcode and bail.
227 */
228 if (vflag < 1) {
229 return;
230 }
231
232 printf("\n\tMD Level %u, First TLV offset %u",
233 CFM_EXTRACT_MD_LEVEL(cfm_common_header->mdlevel_version),
234 cfm_common_header->first_tlv_offset);
235
236 tptr += sizeof(const struct cfm_common_header_t);
237 tlen = length - sizeof(struct cfm_common_header_t);
238
239 switch (cfm_common_header->opcode) {
240 case CFM_OPCODE_CCM:
241 msg_ptr.cfm_ccm = (const struct cfm_ccm_t *)tptr;
242
243 ccm_interval = CFM_EXTRACT_CCM_INTERVAL(cfm_common_header->flags);
244 printf(", Flags [CCM Interval %u%s]",
245 ccm_interval,
246 cfm_common_header->flags & CFM_CCM_RDI_FLAG ?
247 ", RDI" : "");
248
249 /*
250 * Resolve the CCM interval field.
251 */
252 if (ccm_interval) {
253 printf("\n\t CCM Interval %.3fs"
254 ", min CCM Lifetime %.3fs, max CCM Lifetime %.3fs",
255 ccm_interval_base[ccm_interval],
256 ccm_interval_base[ccm_interval] * CCM_INTERVAL_MIN_MULTIPLIER,
257 ccm_interval_base[ccm_interval] * CCM_INTERVAL_MAX_MULTIPLIER);
258 }
259
260 printf("\n\t Sequence Number 0x%08x, MA-End-Point-ID 0x%04x",
261 EXTRACT_32BITS(msg_ptr.cfm_ccm->sequence),
262 EXTRACT_16BITS(msg_ptr.cfm_ccm->ma_epi));
263
264
265 /*
266 * Resolve the MD fields.
267 */
268 printf("\n\t MD Name Format %s (%u), MD Name length %u",
269 tok2str(cfm_md_nameformat_values, "Unknown",
270 msg_ptr.cfm_ccm->md_nameformat),
271 msg_ptr.cfm_ccm->md_nameformat,
272 msg_ptr.cfm_ccm->md_namelength);
273
274 if (msg_ptr.cfm_ccm->md_nameformat != CFM_CCM_MD_FORMAT_NONE) {
275 printf("\n\t MD Name: ");
276 switch (msg_ptr.cfm_ccm->md_nameformat) {
277 case CFM_CCM_MD_FORMAT_DNS:
278 case CFM_CCM_MD_FORMAT_CHAR:
279 safeputs(msg_ptr.cfm_ccm->md_name, msg_ptr.cfm_ccm->md_namelength);
280 break;
281
282 /* FIXME add printers for those MD formats - hexdump for now */
283 case CFM_CCM_MA_FORMAT_8021:
284 case CFM_CCM_MD_FORMAT_MAC:
285 default:
286 print_unknown_data(msg_ptr.cfm_ccm->md_name, "\n\t ",
287 msg_ptr.cfm_ccm->md_namelength);
288 }
289 }
290
291
292 /*
293 * Resolve the MA fields.
294 */
295 ma_nameformat = msg_ptr.cfm_ccm->md_name + msg_ptr.cfm_ccm->md_namelength;
296 ma_namelength = msg_ptr.cfm_ccm->md_name + msg_ptr.cfm_ccm->md_namelength + 1;
297 ma_name = msg_ptr.cfm_ccm->md_name + msg_ptr.cfm_ccm->md_namelength + 2;
298
299 printf("\n\t MA Name-Format %s (%u), MA name length %u",
300 tok2str(cfm_ma_nameformat_values, "Unknown",
301 *ma_nameformat),
302 *ma_nameformat,
303 *ma_namelength);
304
305 printf("\n\t MA Name: ");
306 switch (*ma_nameformat) {
307 case CFM_CCM_MA_FORMAT_CHAR:
308 safeputs(ma_name, *ma_namelength);
309 break;
310
311 /* FIXME add printers for those MA formats - hexdump for now */
312 case CFM_CCM_MA_FORMAT_8021:
313 case CFM_CCM_MA_FORMAT_VID:
314 case CFM_CCM_MA_FORMAT_INT:
315 case CFM_CCM_MA_FORMAT_VPN:
316 default:
317 print_unknown_data(ma_name, "\n\t ", *ma_namelength);
318 }
319 break;
320
321 /*
322 * No message decoder yet.
323 * Hexdump everything up until the start of the TLVs
324 */
325 case CFM_OPCODE_LBR:
326 case CFM_OPCODE_LBM:
327 case CFM_OPCODE_LTR:
328 case CFM_OPCODE_LTM:
329 default:
330 if (tlen > cfm_common_header->first_tlv_offset) {
331 print_unknown_data(tptr, "\n\t ",
332 tlen - cfm_common_header->first_tlv_offset);
333 }
334 break;
335 }
336
337 /*
338 * Sanity check for not walking off.
339 */
340 if (tlen <= cfm_common_header->first_tlv_offset) {
341 return;
342 }
343
344 tptr += cfm_common_header->first_tlv_offset;
345 tlen -= cfm_common_header->first_tlv_offset;
346
347 while (tlen > 0) {
348 cfm_tlv_header = (const struct cfm_tlv_header_t *)tptr;
349
350 /* Enough to read the tlv type ? */
351 TCHECK2(*tptr, 1);
352 cfm_tlv_type=cfm_tlv_header->type;
353
354 if (cfm_tlv_type != CFM_TLV_END) {
355 /* did we capture enough for fully decoding the object header ? */
356 TCHECK2(*tptr, sizeof(struct cfm_tlv_header_t));
357 cfm_tlv_len=EXTRACT_16BITS(&cfm_tlv_header->length);
358 } else {
359 cfm_tlv_len = 0;
360 }
361
362 printf("\n\t%s TLV (0x%02x), length %u",
363 tok2str(cfm_tlv_values,
364 "Unknown",
365 cfm_tlv_type),
366 cfm_tlv_type,
367 cfm_tlv_len);
368
369 /* sanity check for not walking off and infinite loop check. */
370 if ((cfm_tlv_type != CFM_TLV_END) &&
371 ((cfm_tlv_len + sizeof(struct cfm_tlv_header_t) > tlen) ||
372 (!cfm_tlv_len))) {
373 print_unknown_data(tptr,"\n\t ",tlen);
374 return;
375 }
376
377 tptr += sizeof(struct cfm_tlv_header_t);
378 tlen -= sizeof(struct cfm_tlv_header_t);
379
380 /* did we capture enough for fully decoding the object ? */
381 if (cfm_tlv_type != CFM_TLV_END) {
382 TCHECK2(*tptr, cfm_tlv_len);
383 }
384
385 switch(cfm_tlv_type) {
386 case CFM_TLV_END:
387 /* we are done - bail out */
388 return;
389 case CFM_TLV_PORT_STATUS:
390 printf(", Status: %s (%u)",
391 tok2str(cfm_tlv_port_status_values, "Unknown", *tptr),
392 *tptr);
393 break;
394 case CFM_TLV_INTERFACE_STATUS:
395 printf(", Status: %s (%u)",
396 tok2str(cfm_tlv_interface_status_values, "Unknown", *tptr),
397 *tptr);
398 break;
399
400 /*
401 * FIXME those are the defined TLVs that lack a decoder
402 * you are welcome to contribute code ;-)
403 */
404
405 case CFM_TLV_SENDER_ID:
406 case CFM_TLV_DATA:
407 case CFM_TLV_REPLY_INGRESS:
408 case CFM_TLV_REPLY_EGRESS:
409 case CFM_TLV_PRIVATE:
410 default:
411 if (vflag <= 1)
412 print_unknown_data(tptr,"\n\t ",cfm_tlv_len);
413 break;
414 }
415 /* do we want to see an additional hexdump ? */
416 if (vflag > 1)
417 print_unknown_data(tptr, "\n\t ", cfm_tlv_len);
418
419 tptr+=cfm_tlv_len;
420 tlen-=cfm_tlv_len;
421 }
422 return;
423 trunc:
424 printf("\n\t\t packet exceeded snapshot");
425 }