xref: /freebsd/contrib/tcpdump/print-vqp.c (revision 0bff6a5af8cb6d8e5123f8b667df78cac885dbb7)
1a5779b6eSRui Paulo /*
2a5779b6eSRui Paulo  * Copyright (c) 1998-2006 The TCPDUMP project
3a5779b6eSRui Paulo  *
4a5779b6eSRui Paulo  * Redistribution and use in source and binary forms, with or without
5a5779b6eSRui Paulo  * modification, are permitted provided that: (1) source code
6a5779b6eSRui Paulo  * distributions retain the above copyright notice and this paragraph
7a5779b6eSRui Paulo  * in its entirety, and (2) distributions including binary code include
8a5779b6eSRui Paulo  * the above copyright notice and this paragraph in its entirety in
9a5779b6eSRui Paulo  * the documentation or other materials provided with the distribution.
10a5779b6eSRui Paulo  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
11a5779b6eSRui Paulo  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
12a5779b6eSRui Paulo  * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13a5779b6eSRui Paulo  * FOR A PARTICULAR PURPOSE.
14a5779b6eSRui Paulo  *
15a5779b6eSRui Paulo  * Original code by Carles Kishimoto <Carles.Kishimoto@bsc.es>
16a5779b6eSRui Paulo  */
17a5779b6eSRui Paulo 
183340d773SGleb Smirnoff /* \summary: Cisco VLAN Query Protocol (VQP) printer */
193340d773SGleb Smirnoff 
20a5779b6eSRui Paulo #ifdef HAVE_CONFIG_H
21a5779b6eSRui Paulo #include "config.h"
22a5779b6eSRui Paulo #endif
23a5779b6eSRui Paulo 
243340d773SGleb Smirnoff #include <netdissect-stdinc.h>
25a5779b6eSRui Paulo 
263340d773SGleb Smirnoff #include "netdissect.h"
27a5779b6eSRui Paulo #include "extract.h"
28a5779b6eSRui Paulo #include "addrtoname.h"
29*0bff6a5aSEd Maste #include "ether.h"
30a5779b6eSRui Paulo 
31a5779b6eSRui Paulo #define VQP_VERSION            		1
32a5779b6eSRui Paulo #define VQP_EXTRACT_VERSION(x) ((x)&0xFF)
33a5779b6eSRui Paulo 
34a5779b6eSRui Paulo /*
35a5779b6eSRui Paulo  * VQP common header
36a5779b6eSRui Paulo  *
37a5779b6eSRui Paulo  *  0                   1                   2                   3
38a5779b6eSRui Paulo  *  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
39a5779b6eSRui Paulo  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40a5779b6eSRui Paulo  * |   Constant    | Packet type   |  Error Code   |    nitems     |
41a5779b6eSRui Paulo  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42a5779b6eSRui Paulo  * |                Packet Sequence Number (4 bytes)               |
43a5779b6eSRui Paulo  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44a5779b6eSRui Paulo  */
45a5779b6eSRui Paulo 
46a5779b6eSRui Paulo struct vqp_common_header_t {
473c602fabSXin LI     uint8_t version;
483c602fabSXin LI     uint8_t msg_type;
493c602fabSXin LI     uint8_t error_code;
503c602fabSXin LI     uint8_t nitems;
513c602fabSXin LI     uint8_t sequence[4];
52a5779b6eSRui Paulo };
53a5779b6eSRui Paulo 
54a5779b6eSRui Paulo struct vqp_obj_tlv_t {
553c602fabSXin LI     uint8_t obj_type[4];
563c602fabSXin LI     uint8_t obj_length[2];
57a5779b6eSRui Paulo };
58a5779b6eSRui Paulo 
59a5779b6eSRui Paulo #define VQP_OBJ_REQ_JOIN_PORT  0x01
60a5779b6eSRui Paulo #define VQP_OBJ_RESP_VLAN      0x02
61a5779b6eSRui Paulo #define VQP_OBJ_REQ_RECONFIRM  0x03
62a5779b6eSRui Paulo #define VQP_OBJ_RESP_RECONFIRM 0x04
63a5779b6eSRui Paulo 
64a5779b6eSRui Paulo static const struct tok vqp_msg_type_values[] = {
65a5779b6eSRui Paulo     { VQP_OBJ_REQ_JOIN_PORT, "Request, Join Port"},
66a5779b6eSRui Paulo     { VQP_OBJ_RESP_VLAN, "Response, VLAN"},
67a5779b6eSRui Paulo     { VQP_OBJ_REQ_RECONFIRM, "Request, Reconfirm"},
68a5779b6eSRui Paulo     { VQP_OBJ_RESP_RECONFIRM, "Response, Reconfirm"},
69a5779b6eSRui Paulo     { 0, NULL}
70a5779b6eSRui Paulo };
71a5779b6eSRui Paulo 
72a5779b6eSRui Paulo static const struct tok vqp_error_code_values[] = {
73a5779b6eSRui Paulo     { 0x00, "No error"},
74a5779b6eSRui Paulo     { 0x03, "Access denied"},
75a5779b6eSRui Paulo     { 0x04, "Shutdown port"},
76a5779b6eSRui Paulo     { 0x05, "Wrong VTP domain"},
77a5779b6eSRui Paulo     { 0, NULL}
78a5779b6eSRui Paulo };
79a5779b6eSRui Paulo 
80a5779b6eSRui Paulo /* FIXME the heading 0x0c looks ugly - those must be flags etc. */
81a5779b6eSRui Paulo #define VQP_OBJ_IP_ADDRESS    0x0c01
82a5779b6eSRui Paulo #define VQP_OBJ_PORT_NAME     0x0c02
83a5779b6eSRui Paulo #define VQP_OBJ_VLAN_NAME     0x0c03
84a5779b6eSRui Paulo #define VQP_OBJ_VTP_DOMAIN    0x0c04
85a5779b6eSRui Paulo #define VQP_OBJ_ETHERNET_PKT  0x0c05
86a5779b6eSRui Paulo #define VQP_OBJ_MAC_NULL      0x0c06
87a5779b6eSRui Paulo #define VQP_OBJ_MAC_ADDRESS   0x0c08
88a5779b6eSRui Paulo 
89a5779b6eSRui Paulo static const struct tok vqp_obj_values[] = {
90a5779b6eSRui Paulo     { VQP_OBJ_IP_ADDRESS, "Client IP Address" },
91a5779b6eSRui Paulo     { VQP_OBJ_PORT_NAME, "Port Name" },
92a5779b6eSRui Paulo     { VQP_OBJ_VLAN_NAME, "VLAN Name" },
93a5779b6eSRui Paulo     { VQP_OBJ_VTP_DOMAIN, "VTP Domain" },
94a5779b6eSRui Paulo     { VQP_OBJ_ETHERNET_PKT, "Ethernet Packet" },
95a5779b6eSRui Paulo     { VQP_OBJ_MAC_NULL, "MAC Null" },
96a5779b6eSRui Paulo     { VQP_OBJ_MAC_ADDRESS, "MAC Address" },
97a5779b6eSRui Paulo     { 0, NULL}
98a5779b6eSRui Paulo };
99a5779b6eSRui Paulo 
100a5779b6eSRui Paulo void
1013c602fabSXin LI vqp_print(netdissect_options *ndo, register const u_char *pptr, register u_int len)
102a5779b6eSRui Paulo {
103a5779b6eSRui Paulo     const struct vqp_common_header_t *vqp_common_header;
104a5779b6eSRui Paulo     const struct vqp_obj_tlv_t *vqp_obj_tlv;
105a5779b6eSRui Paulo 
106a5779b6eSRui Paulo     const u_char *tptr;
1073c602fabSXin LI     uint16_t vqp_obj_len;
1083c602fabSXin LI     uint32_t vqp_obj_type;
109*0bff6a5aSEd Maste     u_int tlen;
1103c602fabSXin LI     uint8_t nitems;
111a5779b6eSRui Paulo 
112a5779b6eSRui Paulo     tptr=pptr;
113a5779b6eSRui Paulo     tlen = len;
114a5779b6eSRui Paulo     vqp_common_header = (const struct vqp_common_header_t *)pptr;
1153c602fabSXin LI     ND_TCHECK(*vqp_common_header);
116*0bff6a5aSEd Maste     if (sizeof(struct vqp_common_header_t) > tlen)
117*0bff6a5aSEd Maste         goto trunc;
118a5779b6eSRui Paulo 
119a5779b6eSRui Paulo     /*
120a5779b6eSRui Paulo      * Sanity checking of the header.
121a5779b6eSRui Paulo      */
122a5779b6eSRui Paulo     if (VQP_EXTRACT_VERSION(vqp_common_header->version) != VQP_VERSION) {
1233c602fabSXin LI 	ND_PRINT((ndo, "VQP version %u packet not supported",
1243c602fabSXin LI                VQP_EXTRACT_VERSION(vqp_common_header->version)));
125a5779b6eSRui Paulo 	return;
126a5779b6eSRui Paulo     }
127a5779b6eSRui Paulo 
128a5779b6eSRui Paulo     /* in non-verbose mode just lets print the basic Message Type */
1293c602fabSXin LI     if (ndo->ndo_vflag < 1) {
1303c602fabSXin LI         ND_PRINT((ndo, "VQPv%u %s Message, error-code %s (%u), length %u",
131a5779b6eSRui Paulo                VQP_EXTRACT_VERSION(vqp_common_header->version),
132a5779b6eSRui Paulo                tok2str(vqp_msg_type_values, "unknown (%u)",vqp_common_header->msg_type),
133a5779b6eSRui Paulo                tok2str(vqp_error_code_values, "unknown (%u)",vqp_common_header->error_code),
134a5779b6eSRui Paulo 	       vqp_common_header->error_code,
1353c602fabSXin LI                len));
136a5779b6eSRui Paulo         return;
137a5779b6eSRui Paulo     }
138a5779b6eSRui Paulo 
139a5779b6eSRui Paulo     /* ok they seem to want to know everything - lets fully decode it */
140a5779b6eSRui Paulo     nitems = vqp_common_header->nitems;
1413c602fabSXin LI     ND_PRINT((ndo, "\n\tVQPv%u, %s Message, error-code %s (%u), seq 0x%08x, items %u, length %u",
142a5779b6eSRui Paulo            VQP_EXTRACT_VERSION(vqp_common_header->version),
143a5779b6eSRui Paulo 	   tok2str(vqp_msg_type_values, "unknown (%u)",vqp_common_header->msg_type),
144a5779b6eSRui Paulo 	   tok2str(vqp_error_code_values, "unknown (%u)",vqp_common_header->error_code),
145a5779b6eSRui Paulo 	   vqp_common_header->error_code,
146a5779b6eSRui Paulo            EXTRACT_32BITS(&vqp_common_header->sequence),
147a5779b6eSRui Paulo            nitems,
1483c602fabSXin LI            len));
149a5779b6eSRui Paulo 
150a5779b6eSRui Paulo     /* skip VQP Common header */
151a5779b6eSRui Paulo     tptr+=sizeof(const struct vqp_common_header_t);
152a5779b6eSRui Paulo     tlen-=sizeof(const struct vqp_common_header_t);
153a5779b6eSRui Paulo 
154a5779b6eSRui Paulo     while (nitems > 0 && tlen > 0) {
155a5779b6eSRui Paulo 
156a5779b6eSRui Paulo         vqp_obj_tlv = (const struct vqp_obj_tlv_t *)tptr;
157*0bff6a5aSEd Maste         ND_TCHECK(*vqp_obj_tlv);
158*0bff6a5aSEd Maste         if (sizeof(struct vqp_obj_tlv_t) > tlen)
159*0bff6a5aSEd Maste             goto trunc;
160a5779b6eSRui Paulo         vqp_obj_type = EXTRACT_32BITS(vqp_obj_tlv->obj_type);
161a5779b6eSRui Paulo         vqp_obj_len = EXTRACT_16BITS(vqp_obj_tlv->obj_length);
162a5779b6eSRui Paulo         tptr+=sizeof(struct vqp_obj_tlv_t);
163a5779b6eSRui Paulo         tlen-=sizeof(struct vqp_obj_tlv_t);
164a5779b6eSRui Paulo 
1653c602fabSXin LI         ND_PRINT((ndo, "\n\t  %s Object (0x%08x), length %u, value: ",
166a5779b6eSRui Paulo                tok2str(vqp_obj_values, "Unknown", vqp_obj_type),
1673c602fabSXin LI                vqp_obj_type, vqp_obj_len));
168a5779b6eSRui Paulo 
169a5779b6eSRui Paulo         /* basic sanity check */
170a5779b6eSRui Paulo         if (vqp_obj_type == 0 || vqp_obj_len ==0) {
171a5779b6eSRui Paulo             return;
172a5779b6eSRui Paulo         }
173a5779b6eSRui Paulo 
174a5779b6eSRui Paulo         /* did we capture enough for fully decoding the object ? */
1753c602fabSXin LI         ND_TCHECK2(*tptr, vqp_obj_len);
176*0bff6a5aSEd Maste         if (vqp_obj_len > tlen)
177*0bff6a5aSEd Maste             goto trunc;
178a5779b6eSRui Paulo 
179a5779b6eSRui Paulo         switch(vqp_obj_type) {
180a5779b6eSRui Paulo 	case VQP_OBJ_IP_ADDRESS:
181*0bff6a5aSEd Maste             if (vqp_obj_len != 4)
182*0bff6a5aSEd Maste                 goto trunc;
1833c602fabSXin LI             ND_PRINT((ndo, "%s (0x%08x)", ipaddr_string(ndo, tptr), EXTRACT_32BITS(tptr)));
184a5779b6eSRui Paulo             break;
185a5779b6eSRui Paulo             /* those objects have similar semantics - fall through */
186a5779b6eSRui Paulo         case VQP_OBJ_PORT_NAME:
187a5779b6eSRui Paulo 	case VQP_OBJ_VLAN_NAME:
188a5779b6eSRui Paulo 	case VQP_OBJ_VTP_DOMAIN:
189a5779b6eSRui Paulo 	case VQP_OBJ_ETHERNET_PKT:
1903c602fabSXin LI             safeputs(ndo, tptr, vqp_obj_len);
191a5779b6eSRui Paulo             break;
192a5779b6eSRui Paulo             /* those objects have similar semantics - fall through */
193a5779b6eSRui Paulo 	case VQP_OBJ_MAC_ADDRESS:
194a5779b6eSRui Paulo 	case VQP_OBJ_MAC_NULL:
195*0bff6a5aSEd Maste             if (vqp_obj_len != ETHER_ADDR_LEN)
196*0bff6a5aSEd Maste                 goto trunc;
1973c602fabSXin LI 	      ND_PRINT((ndo, "%s", etheraddr_string(ndo, tptr)));
198a5779b6eSRui Paulo               break;
199a5779b6eSRui Paulo         default:
2003c602fabSXin LI             if (ndo->ndo_vflag <= 1)
2013c602fabSXin LI                 print_unknown_data(ndo,tptr, "\n\t    ", vqp_obj_len);
202a5779b6eSRui Paulo             break;
203a5779b6eSRui Paulo         }
204a5779b6eSRui Paulo 	tptr += vqp_obj_len;
205a5779b6eSRui Paulo 	tlen -= vqp_obj_len;
206a5779b6eSRui Paulo 	nitems--;
207a5779b6eSRui Paulo     }
208a5779b6eSRui Paulo     return;
209a5779b6eSRui Paulo trunc:
2103c602fabSXin LI     ND_PRINT((ndo, "\n\t[|VQP]"));
211a5779b6eSRui Paulo }
212