xref: /netbsd-src/external/bsd/tcpdump/dist/print-dtp.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
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: Dynamic Trunking Protocol (DTP) printer */
19 
20 #include <sys/cdefs.h>
21 #ifndef lint
22 __RCSID("$NetBSD: print-dtp.c,v 1.3 2017/02/05 04:05:05 spz Exp $");
23 #endif
24 
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 
29 #include <netdissect-stdinc.h>
30 
31 #include "netdissect.h"
32 #include "addrtoname.h"
33 #include "extract.h"
34 
35 static const char tstr[] = " [|dtp]";
36 
37 #define DTP_HEADER_LEN			1
38 #define DTP_DOMAIN_TLV			0x0001
39 #define DTP_STATUS_TLV			0x0002
40 #define DTP_DTP_TYPE_TLV		0x0003
41 #define DTP_NEIGHBOR_TLV		0x0004
42 
43 static const struct tok dtp_tlv_values[] = {
44     { DTP_DOMAIN_TLV, "Domain TLV"},
45     { DTP_STATUS_TLV, "Status TLV"},
46     { DTP_DTP_TYPE_TLV, "DTP type TLV"},
47     { DTP_NEIGHBOR_TLV, "Neighbor TLV"},
48     { 0, NULL}
49 };
50 
51 void
52 dtp_print (netdissect_options *ndo, const u_char *pptr, u_int length)
53 {
54     int type, len;
55     const u_char *tptr;
56 
57     if (length < DTP_HEADER_LEN)
58         goto trunc;
59 
60     tptr = pptr;
61 
62     ND_TCHECK2(*tptr, DTP_HEADER_LEN);
63 
64     ND_PRINT((ndo, "DTPv%u, length %u",
65            (*tptr),
66            length));
67 
68     /*
69      * In non-verbose mode, just print version.
70      */
71     if (ndo->ndo_vflag < 1) {
72 	return;
73     }
74 
75     tptr += DTP_HEADER_LEN;
76 
77     while (tptr < (pptr+length)) {
78 
79         ND_TCHECK2(*tptr, 4);
80 	type = EXTRACT_16BITS(tptr);
81         len  = EXTRACT_16BITS(tptr+2);
82        /* XXX: should not be but sometimes it is, see the test captures */
83         if (type == 0)
84             return;
85         ND_PRINT((ndo, "\n\t%s (0x%04x) TLV, length %u",
86                tok2str(dtp_tlv_values, "Unknown", type),
87                type, len));
88 
89         /* infinite loop check */
90         if (len < 4)
91             goto invalid;
92         ND_TCHECK2(*tptr, len);
93 
94         switch (type) {
95 	case DTP_DOMAIN_TLV:
96 		ND_PRINT((ndo, ", "));
97 		fn_printzp(ndo, tptr+4, len-4, pptr+length);
98 		break;
99 
100 	case DTP_STATUS_TLV:
101 	case DTP_DTP_TYPE_TLV:
102                 if (len < 5)
103                     goto invalid;
104                 ND_PRINT((ndo, ", 0x%x", *(tptr+4)));
105                 break;
106 
107 	case DTP_NEIGHBOR_TLV:
108                 if (len < 10)
109                     goto invalid;
110                 ND_PRINT((ndo, ", %s", etheraddr_string(ndo, tptr+4)));
111                 break;
112 
113         default:
114             break;
115         }
116         tptr += len;
117     }
118 
119     return;
120 
121  invalid:
122     ND_PRINT((ndo, "%s", istr));
123     return;
124  trunc:
125     ND_PRINT((ndo, "%s", tstr));
126 }
127 
128 /*
129  * Local Variables:
130  * c-style: whitesmith
131  * c-basic-offset: 4
132  * End:
133  */
134