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