xref: /minix/external/bsd/tcpdump/dist/print-vqp.c (revision b636d99d)
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 __RCSID("$NetBSD: print-vqp.c,v 1.4 2014/11/20 03:05:03 christos Exp $");
23 #endif
24 
25 #define NETDISSECT_REWORKED
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 
30 #include <tcpdump-stdinc.h>
31 
32 #include "interface.h"
33 #include "extract.h"
34 #include "addrtoname.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
vqp_print(netdissect_options * ndo,register const u_char * pptr,register u_int len)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     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 
122     /*
123      * Sanity checking of the header.
124      */
125     if (VQP_EXTRACT_VERSION(vqp_common_header->version) != VQP_VERSION) {
126 	ND_PRINT((ndo, "VQP version %u packet not supported",
127                VQP_EXTRACT_VERSION(vqp_common_header->version)));
128 	return;
129     }
130 
131     /* in non-verbose mode just lets print the basic Message Type */
132     if (ndo->ndo_vflag < 1) {
133         ND_PRINT((ndo, "VQPv%u %s Message, error-code %s (%u), length %u",
134                VQP_EXTRACT_VERSION(vqp_common_header->version),
135                tok2str(vqp_msg_type_values, "unknown (%u)",vqp_common_header->msg_type),
136                tok2str(vqp_error_code_values, "unknown (%u)",vqp_common_header->error_code),
137 	       vqp_common_header->error_code,
138                len));
139         return;
140     }
141 
142     /* ok they seem to want to know everything - lets fully decode it */
143     nitems = vqp_common_header->nitems;
144     ND_PRINT((ndo, "\n\tVQPv%u, %s Message, error-code %s (%u), seq 0x%08x, items %u, length %u",
145            VQP_EXTRACT_VERSION(vqp_common_header->version),
146 	   tok2str(vqp_msg_type_values, "unknown (%u)",vqp_common_header->msg_type),
147 	   tok2str(vqp_error_code_values, "unknown (%u)",vqp_common_header->error_code),
148 	   vqp_common_header->error_code,
149            EXTRACT_32BITS(&vqp_common_header->sequence),
150            nitems,
151            len));
152 
153     /* skip VQP Common header */
154     tptr+=sizeof(const struct vqp_common_header_t);
155     tlen-=sizeof(const struct vqp_common_header_t);
156 
157     while (nitems > 0 && tlen > 0) {
158 
159         vqp_obj_tlv = (const struct vqp_obj_tlv_t *)tptr;
160         vqp_obj_type = EXTRACT_32BITS(vqp_obj_tlv->obj_type);
161         vqp_obj_len = EXTRACT_16BITS(vqp_obj_tlv->obj_length);
162         tptr+=sizeof(struct vqp_obj_tlv_t);
163         tlen-=sizeof(struct vqp_obj_tlv_t);
164 
165         ND_PRINT((ndo, "\n\t  %s Object (0x%08x), length %u, value: ",
166                tok2str(vqp_obj_values, "Unknown", vqp_obj_type),
167                vqp_obj_type, vqp_obj_len));
168 
169         /* basic sanity check */
170         if (vqp_obj_type == 0 || vqp_obj_len ==0) {
171             return;
172         }
173 
174         /* did we capture enough for fully decoding the object ? */
175         ND_TCHECK2(*tptr, vqp_obj_len);
176 
177         switch(vqp_obj_type) {
178 	case VQP_OBJ_IP_ADDRESS:
179             ND_PRINT((ndo, "%s (0x%08x)", ipaddr_string(ndo, tptr), EXTRACT_32BITS(tptr)));
180             break;
181             /* those objects have similar semantics - fall through */
182         case VQP_OBJ_PORT_NAME:
183 	case VQP_OBJ_VLAN_NAME:
184 	case VQP_OBJ_VTP_DOMAIN:
185 	case VQP_OBJ_ETHERNET_PKT:
186             safeputs(ndo, tptr, vqp_obj_len);
187             break;
188             /* those objects have similar semantics - fall through */
189 	case VQP_OBJ_MAC_ADDRESS:
190 	case VQP_OBJ_MAC_NULL:
191 	      ND_PRINT((ndo, "%s", etheraddr_string(ndo, tptr)));
192               break;
193         default:
194             if (ndo->ndo_vflag <= 1)
195                 print_unknown_data(ndo,tptr, "\n\t    ", vqp_obj_len);
196             break;
197         }
198 	tptr += vqp_obj_len;
199 	tlen -= vqp_obj_len;
200 	nitems--;
201     }
202     return;
203 trunc:
204     ND_PRINT((ndo, "\n\t[|VQP]"));
205 }
206