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