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