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