xref: /netbsd-src/external/bsd/tcpdump/dist/print-vqp.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
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.6 2017/02/05 04:05:05 spz 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 
35 #define VQP_VERSION            		1
36 #define VQP_EXTRACT_VERSION(x) ((x)&0xFF)
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     uint8_t version;
52     uint8_t msg_type;
53     uint8_t error_code;
54     uint8_t nitems;
55     uint8_t sequence[4];
56 };
57 
58 struct vqp_obj_tlv_t {
59     uint8_t obj_type[4];
60     uint8_t obj_length[2];
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, register const u_char *pptr, register 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     uint16_t vqp_obj_len;
112     uint32_t vqp_obj_type;
113     int tlen;
114     uint8_t nitems;
115 
116     tptr=pptr;
117     tlen = len;
118     vqp_common_header = (const struct vqp_common_header_t *)pptr;
119     ND_TCHECK(*vqp_common_header);
120 
121     /*
122      * Sanity checking of the header.
123      */
124     if (VQP_EXTRACT_VERSION(vqp_common_header->version) != VQP_VERSION) {
125 	ND_PRINT((ndo, "VQP version %u packet not supported",
126                VQP_EXTRACT_VERSION(vqp_common_header->version)));
127 	return;
128     }
129 
130     /* in non-verbose mode just lets print the basic Message Type */
131     if (ndo->ndo_vflag < 1) {
132         ND_PRINT((ndo, "VQPv%u %s Message, error-code %s (%u), length %u",
133                VQP_EXTRACT_VERSION(vqp_common_header->version),
134                tok2str(vqp_msg_type_values, "unknown (%u)",vqp_common_header->msg_type),
135                tok2str(vqp_error_code_values, "unknown (%u)",vqp_common_header->error_code),
136 	       vqp_common_header->error_code,
137                len));
138         return;
139     }
140 
141     /* ok they seem to want to know everything - lets fully decode it */
142     nitems = vqp_common_header->nitems;
143     ND_PRINT((ndo, "\n\tVQPv%u, %s Message, error-code %s (%u), seq 0x%08x, items %u, length %u",
144            VQP_EXTRACT_VERSION(vqp_common_header->version),
145 	   tok2str(vqp_msg_type_values, "unknown (%u)",vqp_common_header->msg_type),
146 	   tok2str(vqp_error_code_values, "unknown (%u)",vqp_common_header->error_code),
147 	   vqp_common_header->error_code,
148            EXTRACT_32BITS(&vqp_common_header->sequence),
149            nitems,
150            len));
151 
152     /* skip VQP Common header */
153     tptr+=sizeof(const struct vqp_common_header_t);
154     tlen-=sizeof(const struct vqp_common_header_t);
155 
156     while (nitems > 0 && tlen > 0) {
157 
158         vqp_obj_tlv = (const struct vqp_obj_tlv_t *)tptr;
159         vqp_obj_type = EXTRACT_32BITS(vqp_obj_tlv->obj_type);
160         vqp_obj_len = EXTRACT_16BITS(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_TCHECK2(*tptr, vqp_obj_len);
175 
176         switch(vqp_obj_type) {
177 	case VQP_OBJ_IP_ADDRESS:
178             ND_PRINT((ndo, "%s (0x%08x)", ipaddr_string(ndo, tptr), EXTRACT_32BITS(tptr)));
179             break;
180             /* those objects have similar semantics - fall through */
181         case VQP_OBJ_PORT_NAME:
182 	case VQP_OBJ_VLAN_NAME:
183 	case VQP_OBJ_VTP_DOMAIN:
184 	case VQP_OBJ_ETHERNET_PKT:
185             safeputs(ndo, tptr, vqp_obj_len);
186             break;
187             /* those objects have similar semantics - fall through */
188 	case VQP_OBJ_MAC_ADDRESS:
189 	case VQP_OBJ_MAC_NULL:
190 	      ND_PRINT((ndo, "%s", etheraddr_string(ndo, tptr)));
191               break;
192         default:
193             if (ndo->ndo_vflag <= 1)
194                 print_unknown_data(ndo,tptr, "\n\t    ", vqp_obj_len);
195             break;
196         }
197 	tptr += vqp_obj_len;
198 	tlen -= vqp_obj_len;
199 	nitems--;
200     }
201     return;
202 trunc:
203     ND_PRINT((ndo, "\n\t[|VQP]"));
204 }
205