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