]> The Tcpdump Group git mirrors - tcpdump/blob - print-vqp.c
Merge branch 'master' into master
[tcpdump] / print-vqp.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 * Original code by Carles Kishimoto <Carles.Kishimoto@bsc.es>
16 */
17
18 /* \summary: Cisco VLAN Query Protocol (VQP) printer */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <netdissect-stdinc.h>
25
26 #include "netdissect.h"
27 #include "extract.h"
28 #include "addrtoname.h"
29
30 #define VQP_VERSION 1
31 #define VQP_EXTRACT_VERSION(x) ((x)&0xFF)
32
33 /*
34 * VQP common header
35 *
36 * 0 1 2 3
37 * 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
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * | Constant | Packet type | Error Code | nitems |
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | Packet Sequence Number (4 bytes) |
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 */
44
45 struct vqp_common_header_t {
46 uint8_t version;
47 uint8_t msg_type;
48 uint8_t error_code;
49 uint8_t nitems;
50 uint8_t sequence[4];
51 };
52
53 struct vqp_obj_tlv_t {
54 uint8_t obj_type[4];
55 uint8_t obj_length[2];
56 };
57
58 #define VQP_OBJ_REQ_JOIN_PORT 0x01
59 #define VQP_OBJ_RESP_VLAN 0x02
60 #define VQP_OBJ_REQ_RECONFIRM 0x03
61 #define VQP_OBJ_RESP_RECONFIRM 0x04
62
63 static const struct tok vqp_msg_type_values[] = {
64 { VQP_OBJ_REQ_JOIN_PORT, "Request, Join Port"},
65 { VQP_OBJ_RESP_VLAN, "Response, VLAN"},
66 { VQP_OBJ_REQ_RECONFIRM, "Request, Reconfirm"},
67 { VQP_OBJ_RESP_RECONFIRM, "Response, Reconfirm"},
68 { 0, NULL}
69 };
70
71 static const struct tok vqp_error_code_values[] = {
72 { 0x00, "No error"},
73 { 0x03, "Access denied"},
74 { 0x04, "Shutdown port"},
75 { 0x05, "Wrong VTP domain"},
76 { 0, NULL}
77 };
78
79 /* FIXME the heading 0x0c looks ugly - those must be flags etc. */
80 #define VQP_OBJ_IP_ADDRESS 0x0c01
81 #define VQP_OBJ_PORT_NAME 0x0c02
82 #define VQP_OBJ_VLAN_NAME 0x0c03
83 #define VQP_OBJ_VTP_DOMAIN 0x0c04
84 #define VQP_OBJ_ETHERNET_PKT 0x0c05
85 #define VQP_OBJ_MAC_NULL 0x0c06
86 #define VQP_OBJ_MAC_ADDRESS 0x0c08
87
88 static const struct tok vqp_obj_values[] = {
89 { VQP_OBJ_IP_ADDRESS, "Client IP Address" },
90 { VQP_OBJ_PORT_NAME, "Port Name" },
91 { VQP_OBJ_VLAN_NAME, "VLAN Name" },
92 { VQP_OBJ_VTP_DOMAIN, "VTP Domain" },
93 { VQP_OBJ_ETHERNET_PKT, "Ethernet Packet" },
94 { VQP_OBJ_MAC_NULL, "MAC Null" },
95 { VQP_OBJ_MAC_ADDRESS, "MAC Address" },
96 { 0, NULL}
97 };
98
99 void
100 vqp_print(netdissect_options *ndo, const u_char *pptr, u_int len)
101 {
102 const struct vqp_common_header_t *vqp_common_header;
103 const struct vqp_obj_tlv_t *vqp_obj_tlv;
104
105 const u_char *tptr;
106 uint16_t vqp_obj_len;
107 uint32_t vqp_obj_type;
108 u_int tlen;
109 uint8_t nitems;
110
111 tptr=pptr;
112 tlen = len;
113 vqp_common_header = (const struct vqp_common_header_t *)pptr;
114 ND_TCHECK(*vqp_common_header);
115 if (sizeof(struct vqp_common_header_t) > tlen)
116 goto trunc;
117
118 /*
119 * Sanity checking of the header.
120 */
121 if (VQP_EXTRACT_VERSION(vqp_common_header->version) != VQP_VERSION) {
122 ND_PRINT((ndo, "VQP version %u packet not supported",
123 VQP_EXTRACT_VERSION(vqp_common_header->version)));
124 return;
125 }
126
127 /* in non-verbose mode just lets print the basic Message Type */
128 if (ndo->ndo_vflag < 1) {
129 ND_PRINT((ndo, "VQPv%u %s Message, error-code %s (%u), length %u",
130 VQP_EXTRACT_VERSION(vqp_common_header->version),
131 tok2str(vqp_msg_type_values, "unknown (%u)",vqp_common_header->msg_type),
132 tok2str(vqp_error_code_values, "unknown (%u)",vqp_common_header->error_code),
133 vqp_common_header->error_code,
134 len));
135 return;
136 }
137
138 /* ok they seem to want to know everything - lets fully decode it */
139 nitems = vqp_common_header->nitems;
140 ND_PRINT((ndo, "\n\tVQPv%u, %s Message, error-code %s (%u), seq 0x%08x, items %u, length %u",
141 VQP_EXTRACT_VERSION(vqp_common_header->version),
142 tok2str(vqp_msg_type_values, "unknown (%u)",vqp_common_header->msg_type),
143 tok2str(vqp_error_code_values, "unknown (%u)",vqp_common_header->error_code),
144 vqp_common_header->error_code,
145 EXTRACT_BE_U_4(&vqp_common_header->sequence),
146 nitems,
147 len));
148
149 /* skip VQP Common header */
150 tptr+=sizeof(struct vqp_common_header_t);
151 tlen-=sizeof(struct vqp_common_header_t);
152
153 while (nitems > 0 && tlen > 0) {
154
155 vqp_obj_tlv = (const struct vqp_obj_tlv_t *)tptr;
156 ND_TCHECK(*vqp_obj_tlv);
157 if (sizeof(struct vqp_obj_tlv_t) > tlen)
158 goto trunc;
159 vqp_obj_type = EXTRACT_BE_U_4(vqp_obj_tlv->obj_type);
160 vqp_obj_len = EXTRACT_BE_U_2(vqp_obj_tlv->obj_length);
161 tptr+=sizeof(struct vqp_obj_tlv_t);
162 tlen-=sizeof(struct vqp_obj_tlv_t);
163
164 ND_PRINT((ndo, "\n\t %s Object (0x%08x), length %u, value: ",
165 tok2str(vqp_obj_values, "Unknown", vqp_obj_type),
166 vqp_obj_type, vqp_obj_len));
167
168 /* basic sanity check */
169 if (vqp_obj_type == 0 || vqp_obj_len ==0) {
170 return;
171 }
172
173 /* did we capture enough for fully decoding the object ? */
174 ND_TCHECK_LEN(tptr, vqp_obj_len);
175 if (vqp_obj_len > tlen)
176 goto trunc;
177
178 switch(vqp_obj_type) {
179 case VQP_OBJ_IP_ADDRESS:
180 if (vqp_obj_len != 4)
181 goto trunc;
182 ND_PRINT((ndo, "%s (0x%08x)", ipaddr_string(ndo, tptr), EXTRACT_BE_U_4(tptr)));
183 break;
184 /* those objects have similar semantics - fall through */
185 case VQP_OBJ_PORT_NAME:
186 case VQP_OBJ_VLAN_NAME:
187 case VQP_OBJ_VTP_DOMAIN:
188 case VQP_OBJ_ETHERNET_PKT:
189 safeputs(ndo, tptr, vqp_obj_len);
190 break;
191 /* those objects have similar semantics - fall through */
192 case VQP_OBJ_MAC_ADDRESS:
193 case VQP_OBJ_MAC_NULL:
194 if (vqp_obj_len != MAC_ADDR_LEN)
195 goto trunc;
196 ND_PRINT((ndo, "%s", etheraddr_string(ndo, tptr)));
197 break;
198 default:
199 if (ndo->ndo_vflag <= 1)
200 print_unknown_data(ndo,tptr, "\n\t ", vqp_obj_len);
201 break;
202 }
203 tptr += vqp_obj_len;
204 tlen -= vqp_obj_len;
205 nitems--;
206 }
207 return;
208 trunc:
209 ND_PRINT((ndo, "\n\t[|VQP]"));
210 }