xref: /freebsd/contrib/tcpdump/print-vtp.c (revision 076ad2f8)
1 /*
2  * Copyright (c) 1998-2007 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  * Reference documentation:
16  *  http://www.cisco.com/en/US/tech/tk389/tk689/technologies_tech_note09186a0080094c52.shtml
17  *  http://www.cisco.com/warp/public/473/21.html
18  *  http://www.cisco.com/univercd/cc/td/doc/product/lan/trsrb/frames.htm
19  *
20  * Original code ode by Carles Kishimoto <carles.kishimoto@gmail.com>
21  */
22 
23 /* \summary: Cisco VLAN Trunking Protocol (VTP) printer */
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 "addrtoname.h"
33 #include "extract.h"
34 
35 #define VTP_HEADER_LEN			36
36 #define	VTP_DOMAIN_NAME_LEN		32
37 #define	VTP_MD5_DIGEST_LEN		16
38 #define VTP_UPDATE_TIMESTAMP_LEN	12
39 #define VTP_VLAN_INFO_OFFSET		12
40 
41 #define VTP_SUMMARY_ADV			0x01
42 #define VTP_SUBSET_ADV			0x02
43 #define VTP_ADV_REQUEST			0x03
44 #define VTP_JOIN_MESSAGE		0x04
45 
46 struct vtp_vlan_ {
47     uint8_t  len;
48     uint8_t  status;
49     uint8_t  type;
50     uint8_t  name_len;
51     uint16_t vlanid;
52     uint16_t mtu;
53     uint32_t index;
54 };
55 
56 static const struct tok vtp_message_type_values[] = {
57     { VTP_SUMMARY_ADV, "Summary advertisement"},
58     { VTP_SUBSET_ADV, "Subset advertisement"},
59     { VTP_ADV_REQUEST, "Advertisement request"},
60     { VTP_JOIN_MESSAGE, "Join message"},
61     { 0, NULL }
62 };
63 
64 static const struct tok vtp_header_values[] = {
65     { 0x01, "Followers"}, /* On Summary advertisement, 3rd byte is Followers */
66     { 0x02, "Seq number"}, /* On Subset  advertisement, 3rd byte is Sequence number */
67     { 0x03, "Rsvd"}, /* On Adver. requests 3rd byte is Rsvd */
68     { 0x04, "Rsvd"}, /* On Adver. requests 3rd byte is Rsvd */
69     { 0, NULL }
70 };
71 
72 static const struct tok vtp_vlan_type_values[] = {
73     { 0x01, "Ethernet"},
74     { 0x02, "FDDI"},
75     { 0x03, "TrCRF"},
76     { 0x04, "FDDI-net"},
77     { 0x05, "TrBRF"},
78     { 0, NULL }
79 };
80 
81 static const struct tok vtp_vlan_status[] = {
82     { 0x00, "Operational"},
83     { 0x01, "Suspended"},
84     { 0, NULL }
85 };
86 
87 #define VTP_VLAN_SOURCE_ROUTING_RING_NUMBER      0x01
88 #define VTP_VLAN_SOURCE_ROUTING_BRIDGE_NUMBER    0x02
89 #define VTP_VLAN_STP_TYPE                        0x03
90 #define VTP_VLAN_PARENT_VLAN                     0x04
91 #define VTP_VLAN_TRANS_BRIDGED_VLAN              0x05
92 #define VTP_VLAN_PRUNING                         0x06
93 #define VTP_VLAN_BRIDGE_TYPE                     0x07
94 #define VTP_VLAN_ARP_HOP_COUNT                   0x08
95 #define VTP_VLAN_STE_HOP_COUNT                   0x09
96 #define VTP_VLAN_BACKUP_CRF_MODE                 0x0A
97 
98 static const struct tok vtp_vlan_tlv_values[] = {
99     { VTP_VLAN_SOURCE_ROUTING_RING_NUMBER, "Source-Routing Ring Number TLV"},
100     { VTP_VLAN_SOURCE_ROUTING_BRIDGE_NUMBER, "Source-Routing Bridge Number TLV"},
101     { VTP_VLAN_STP_TYPE, "STP type TLV"},
102     { VTP_VLAN_PARENT_VLAN, "Parent VLAN TLV"},
103     { VTP_VLAN_TRANS_BRIDGED_VLAN, "Translationally bridged VLANs TLV"},
104     { VTP_VLAN_PRUNING, "Pruning TLV"},
105     { VTP_VLAN_BRIDGE_TYPE, "Bridge Type TLV"},
106     { VTP_VLAN_ARP_HOP_COUNT, "Max ARP Hop Count TLV"},
107     { VTP_VLAN_STE_HOP_COUNT, "Max STE Hop Count TLV"},
108     { VTP_VLAN_BACKUP_CRF_MODE, "Backup CRF Mode TLV"},
109     { 0,                                  NULL }
110 };
111 
112 static const struct tok vtp_stp_type_values[] = {
113     { 1, "SRT"},
114     { 2, "SRB"},
115     { 3, "Auto"},
116     { 0, NULL }
117 };
118 
119 void
120 vtp_print (netdissect_options *ndo,
121            const u_char *pptr, u_int length)
122 {
123     int type, len, tlv_len, tlv_value, mgmtd_len;
124     const u_char *tptr;
125     const struct vtp_vlan_ *vtp_vlan;
126 
127     if (length < VTP_HEADER_LEN)
128         goto trunc;
129 
130     tptr = pptr;
131 
132     ND_TCHECK2(*tptr, VTP_HEADER_LEN);
133 
134     type = *(tptr+1);
135     ND_PRINT((ndo, "VTPv%u, Message %s (0x%02x), length %u",
136 	   *tptr,
137 	   tok2str(vtp_message_type_values,"Unknown message type", type),
138 	   type,
139 	   length));
140 
141     /* In non-verbose mode, just print version and message type */
142     if (ndo->ndo_vflag < 1) {
143         return;
144     }
145 
146     /* verbose mode print all fields */
147     ND_PRINT((ndo, "\n\tDomain name: "));
148     mgmtd_len = *(tptr + 3);
149     if (mgmtd_len < 1 ||  mgmtd_len > 32) {
150 	ND_PRINT((ndo, " [invalid MgmtD Len %d]", mgmtd_len));
151 	return;
152     }
153     fn_printzp(ndo, tptr + 4, mgmtd_len, NULL);
154     ND_PRINT((ndo, ", %s: %u",
155 	   tok2str(vtp_header_values, "Unknown", type),
156 	   *(tptr+2)));
157 
158     tptr += VTP_HEADER_LEN;
159 
160     switch (type) {
161 
162     case VTP_SUMMARY_ADV:
163 
164 	/*
165 	 *  SUMMARY ADVERTISEMENT
166 	 *
167 	 *  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
168 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
169 	 *  |     Version   |     Code      |    Followers  |    MgmtD Len  |
170 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
171 	 *  |       Management Domain Name  (zero-padded to 32 bytes)       |
172 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
173 	 *  |                    Configuration revision number              |
174 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
175 	 *  |                  Updater Identity IP address                  |
176 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
177 	 *  |                    Update Timestamp (12 bytes)                |
178 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
179 	 *  |                        MD5 digest (16 bytes)                  |
180 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
181 	 *
182 	 */
183 
184 	ND_TCHECK2(*tptr, 8);
185 	ND_PRINT((ndo, "\n\t  Config Rev %x, Updater %s",
186 	       EXTRACT_32BITS(tptr),
187 	       ipaddr_string(ndo, tptr+4)));
188 	tptr += 8;
189 	ND_TCHECK2(*tptr, VTP_UPDATE_TIMESTAMP_LEN);
190 	ND_PRINT((ndo, ", Timestamp 0x%08x 0x%08x 0x%08x",
191 	       EXTRACT_32BITS(tptr),
192 	       EXTRACT_32BITS(tptr + 4),
193 	       EXTRACT_32BITS(tptr + 8)));
194 	tptr += VTP_UPDATE_TIMESTAMP_LEN;
195 	ND_TCHECK2(*tptr, VTP_MD5_DIGEST_LEN);
196 	ND_PRINT((ndo, ", MD5 digest: %08x%08x%08x%08x",
197 	       EXTRACT_32BITS(tptr),
198 	       EXTRACT_32BITS(tptr + 4),
199 	       EXTRACT_32BITS(tptr + 8),
200 	       EXTRACT_32BITS(tptr + 12)));
201 	tptr += VTP_MD5_DIGEST_LEN;
202 	break;
203 
204     case VTP_SUBSET_ADV:
205 
206 	/*
207 	 *  SUBSET ADVERTISEMENT
208 	 *
209 	 *  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
210 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
211 	 *  |     Version   |     Code      |   Seq number  |    MgmtD Len  |
212 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
213 	 *  |       Management Domain Name  (zero-padded to 32 bytes)       |
214 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
215 	 *  |                    Configuration revision number              |
216 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217 	 *  |                         VLAN info field 1                     |
218 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
219 	 *  |                         ................                      |
220 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
221 	 *  |                         VLAN info field N                     |
222 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
223 	 *
224 	 */
225 
226 	ND_PRINT((ndo, ", Config Rev %x", EXTRACT_32BITS(tptr)));
227 
228 	/*
229 	 *  VLAN INFORMATION
230 	 *  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
231 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
232 	 *  | V info len    |    Status     |  VLAN type    | VLAN name len |
233 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
234 	 *  |       ISL vlan id             |            MTU size           |
235 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
236 	 *  |                     802.10 index (SAID)                       |
237 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
238 	 *  |                         VLAN name                             |
239 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
240 	 *
241 	 */
242 
243 	tptr += 4;
244 	while (tptr < (pptr+length)) {
245 
246 	    len = *tptr;
247 	    if (len == 0)
248 		break;
249 
250 	    ND_TCHECK2(*tptr, len);
251 
252 	    vtp_vlan = (const struct vtp_vlan_*)tptr;
253 	    ND_TCHECK(*vtp_vlan);
254 	    ND_PRINT((ndo, "\n\tVLAN info status %s, type %s, VLAN-id %u, MTU %u, SAID 0x%08x, Name ",
255 		   tok2str(vtp_vlan_status,"Unknown",vtp_vlan->status),
256 		   tok2str(vtp_vlan_type_values,"Unknown",vtp_vlan->type),
257 		   EXTRACT_16BITS(&vtp_vlan->vlanid),
258 		   EXTRACT_16BITS(&vtp_vlan->mtu),
259 		   EXTRACT_32BITS(&vtp_vlan->index)));
260 	    fn_printzp(ndo, tptr + VTP_VLAN_INFO_OFFSET, vtp_vlan->name_len, NULL);
261 
262             /*
263              * Vlan names are aligned to 32-bit boundaries.
264              */
265             len  -= VTP_VLAN_INFO_OFFSET + 4*((vtp_vlan->name_len + 3)/4);
266             tptr += VTP_VLAN_INFO_OFFSET + 4*((vtp_vlan->name_len + 3)/4);
267 
268             /* TLV information follows */
269 
270             while (len > 0) {
271 
272                 /*
273                  * Cisco specs says 2 bytes for type + 2 bytes for length, take only 1
274                  * See: http://www.cisco.com/univercd/cc/td/doc/product/lan/trsrb/frames.htm
275                  */
276                 type = *tptr;
277                 tlv_len = *(tptr+1);
278 
279                 ND_PRINT((ndo, "\n\t\t%s (0x%04x) TLV",
280                        tok2str(vtp_vlan_tlv_values, "Unknown", type),
281                        type));
282 
283                 /*
284                  * infinite loop check
285                  */
286                 if (type == 0 || tlv_len == 0) {
287                     return;
288                 }
289 
290                 ND_TCHECK2(*tptr, tlv_len * 2 +2);
291 
292                 tlv_value = EXTRACT_16BITS(tptr+2);
293 
294                 switch (type) {
295                 case VTP_VLAN_STE_HOP_COUNT:
296                     ND_PRINT((ndo, ", %u", tlv_value));
297                     break;
298 
299                 case VTP_VLAN_PRUNING:
300                     ND_PRINT((ndo, ", %s (%u)",
301                            tlv_value == 1 ? "Enabled" : "Disabled",
302                            tlv_value));
303                     break;
304 
305                 case VTP_VLAN_STP_TYPE:
306                     ND_PRINT((ndo, ", %s (%u)",
307                            tok2str(vtp_stp_type_values, "Unknown", tlv_value),
308                            tlv_value));
309                     break;
310 
311                 case VTP_VLAN_BRIDGE_TYPE:
312                     ND_PRINT((ndo, ", %s (%u)",
313                            tlv_value == 1 ? "SRB" : "SRT",
314                            tlv_value));
315                     break;
316 
317                 case VTP_VLAN_BACKUP_CRF_MODE:
318                     ND_PRINT((ndo, ", %s (%u)",
319                            tlv_value == 1 ? "Backup" : "Not backup",
320                            tlv_value));
321                     break;
322 
323                     /*
324                      * FIXME those are the defined TLVs that lack a decoder
325                      * you are welcome to contribute code ;-)
326                      */
327 
328                 case VTP_VLAN_SOURCE_ROUTING_RING_NUMBER:
329                 case VTP_VLAN_SOURCE_ROUTING_BRIDGE_NUMBER:
330                 case VTP_VLAN_PARENT_VLAN:
331                 case VTP_VLAN_TRANS_BRIDGED_VLAN:
332                 case VTP_VLAN_ARP_HOP_COUNT:
333                 default:
334 		    print_unknown_data(ndo, tptr, "\n\t\t  ", 2 + tlv_len*2);
335                     break;
336                 }
337                 len -= 2 + tlv_len*2;
338                 tptr += 2 + tlv_len*2;
339             }
340 	}
341 	break;
342 
343     case VTP_ADV_REQUEST:
344 
345 	/*
346 	 *  ADVERTISEMENT REQUEST
347 	 *
348 	 *  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
349 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
350 	 *  |     Version   |     Code      |   Reserved    |    MgmtD Len  |
351 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
352 	 *  |       Management Domain Name  (zero-padded to 32 bytes)       |
353 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
354 	 *  |                          Start value                          |
355 	 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
356 	 *
357 	 */
358 
359 	ND_TCHECK2(*tptr, 4);
360 	ND_PRINT((ndo, "\n\tStart value: %u", EXTRACT_32BITS(tptr)));
361 	break;
362 
363     case VTP_JOIN_MESSAGE:
364 
365 	/* FIXME - Could not find message format */
366 	break;
367 
368     default:
369 	break;
370     }
371 
372     return;
373 
374  trunc:
375     ND_PRINT((ndo, "[|vtp]"));
376 }
377 
378 /*
379  * Local Variables:
380  * c-style: whitesmith
381  * c-basic-offset: 4
382  * End:
383  */
384