xref: /dflybsd-src/contrib/tcpdump/print-vrrp.c (revision 59c07fbdf8168fa08c76c515186d561b5a92690c)
141c99275SPeter Avalos /*
241c99275SPeter Avalos  * Copyright (c) 2000 William C. Fenner.
341c99275SPeter Avalos  *                All rights reserved.
441c99275SPeter Avalos  *
541c99275SPeter Avalos  * Kevin Steves <ks@hp.se> July 2000
641c99275SPeter Avalos  * Modified to:
741c99275SPeter Avalos  * - print version, type string and packet length
841c99275SPeter Avalos  * - print IP address count if > 1 (-v)
941c99275SPeter Avalos  * - verify checksum (-v)
1041c99275SPeter Avalos  * - print authentication string (-v)
1141c99275SPeter Avalos  *
1241c99275SPeter Avalos  * Redistribution and use in source and binary forms, with or without
1341c99275SPeter Avalos  * modification, are permitted provided that: (1) source code
1441c99275SPeter Avalos  * distributions retain the above copyright notice and this paragraph
1541c99275SPeter Avalos  * in its entirety, and (2) distributions including binary code include
1641c99275SPeter Avalos  * the above copyright notice and this paragraph in its entirety in
1741c99275SPeter Avalos  * the documentation or other materials provided with the distribution.
1841c99275SPeter Avalos  * The name of William C. Fenner may not be used to endorse or
1941c99275SPeter Avalos  * promote products derived from this software without specific prior
2041c99275SPeter Avalos  * written permission.  THIS SOFTWARE IS PROVIDED ``AS IS'' AND
2141c99275SPeter Avalos  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
2241c99275SPeter Avalos  * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2341c99275SPeter Avalos  * FOR A PARTICULAR PURPOSE.
2441c99275SPeter Avalos  */
2541c99275SPeter Avalos 
26411677aeSAaron LI /* \summary: Virtual Router Redundancy Protocol (VRRP) printer */
2741c99275SPeter Avalos 
2841c99275SPeter Avalos #ifdef HAVE_CONFIG_H
29*ed775ee7SAntonio Huete Jimenez #include <config.h>
3041c99275SPeter Avalos #endif
3141c99275SPeter Avalos 
32*ed775ee7SAntonio Huete Jimenez #include "netdissect-stdinc.h"
3341c99275SPeter Avalos 
34411677aeSAaron LI #include "netdissect.h"
3541c99275SPeter Avalos #include "extract.h"
3641c99275SPeter Avalos #include "addrtoname.h"
3741c99275SPeter Avalos 
38411677aeSAaron LI #include "ip.h"
39411677aeSAaron LI #include "ipproto.h"
4041c99275SPeter Avalos /*
41411677aeSAaron LI  * RFC 2338 (VRRP v2):
42411677aeSAaron LI  *
4341c99275SPeter Avalos  *     0                   1                   2                   3
4441c99275SPeter Avalos  *     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
4541c99275SPeter Avalos  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
4641c99275SPeter Avalos  *    |Version| Type  | Virtual Rtr ID|   Priority    | Count IP Addrs|
4741c99275SPeter Avalos  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
4841c99275SPeter Avalos  *    |   Auth Type   |   Adver Int   |          Checksum             |
4941c99275SPeter Avalos  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
5041c99275SPeter Avalos  *    |                         IP Address (1)                        |
5141c99275SPeter Avalos  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
5241c99275SPeter Avalos  *    |                            .                                  |
5341c99275SPeter Avalos  *    |                            .                                  |
5441c99275SPeter Avalos  *    |                            .                                  |
5541c99275SPeter Avalos  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
5641c99275SPeter Avalos  *    |                         IP Address (n)                        |
5741c99275SPeter Avalos  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
5841c99275SPeter Avalos  *    |                     Authentication Data (1)                   |
5941c99275SPeter Avalos  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
6041c99275SPeter Avalos  *    |                     Authentication Data (2)                   |
6141c99275SPeter Avalos  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62411677aeSAaron LI  *
63411677aeSAaron LI  *
64411677aeSAaron LI  * RFC 5798 (VRRP v3):
65411677aeSAaron LI  *
66411677aeSAaron LI  *    0                   1                   2                   3
67411677aeSAaron LI  *    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
68411677aeSAaron LI  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69411677aeSAaron LI  *    |                    IPv4 Fields or IPv6 Fields                 |
70411677aeSAaron LI  *   ...                                                             ...
71411677aeSAaron LI  *    |                                                               |
72411677aeSAaron LI  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73411677aeSAaron LI  *    |Version| Type  | Virtual Rtr ID|   Priority    |Count IPvX Addr|
74411677aeSAaron LI  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
75411677aeSAaron LI  *    |(rsvd) |     Max Adver Int     |          Checksum             |
76411677aeSAaron LI  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
77411677aeSAaron LI  *    |                                                               |
78411677aeSAaron LI  *    +                                                               +
79411677aeSAaron LI  *    |                       IPvX Address(es)                        |
80411677aeSAaron LI  *    +                                                               +
81411677aeSAaron LI  *    |                                                               |
82411677aeSAaron LI  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
8341c99275SPeter Avalos  */
8441c99275SPeter Avalos 
8541c99275SPeter Avalos /* Type */
8641c99275SPeter Avalos #define	VRRP_TYPE_ADVERTISEMENT	1
8741c99275SPeter Avalos 
8841c99275SPeter Avalos static const struct tok type2str[] = {
8941c99275SPeter Avalos 	{ VRRP_TYPE_ADVERTISEMENT,	"Advertisement"	},
9041c99275SPeter Avalos 	{ 0,				NULL		}
9141c99275SPeter Avalos };
9241c99275SPeter Avalos 
9341c99275SPeter Avalos /* Auth Type */
9441c99275SPeter Avalos #define	VRRP_AUTH_NONE		0
9541c99275SPeter Avalos #define	VRRP_AUTH_SIMPLE	1
9641c99275SPeter Avalos #define	VRRP_AUTH_AH		2
9741c99275SPeter Avalos 
9841c99275SPeter Avalos static const struct tok auth2str[] = {
9941c99275SPeter Avalos 	{ VRRP_AUTH_NONE,		"none"		},
10041c99275SPeter Avalos 	{ VRRP_AUTH_SIMPLE,		"simple"	},
10141c99275SPeter Avalos 	{ VRRP_AUTH_AH,			"ah"		},
10241c99275SPeter Avalos 	{ 0,				NULL		}
10341c99275SPeter Avalos };
10441c99275SPeter Avalos 
10541c99275SPeter Avalos void
vrrp_print(netdissect_options * ndo,const u_char * bp,u_int len,const u_char * bp2,int ttl)106411677aeSAaron LI vrrp_print(netdissect_options *ndo,
107*ed775ee7SAntonio Huete Jimenez            const u_char *bp, u_int len,
108*ed775ee7SAntonio Huete Jimenez            const u_char *bp2, int ttl)
10941c99275SPeter Avalos {
110411677aeSAaron LI 	int version, type, auth_type = VRRP_AUTH_NONE; /* keep compiler happy */
11141c99275SPeter Avalos 	const char *type_s;
11241c99275SPeter Avalos 
113*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_protocol = "vrrp";
114*ed775ee7SAntonio Huete Jimenez 	version = (GET_U_1(bp) & 0xf0) >> 4;
115*ed775ee7SAntonio Huete Jimenez 	type = GET_U_1(bp) & 0x0f;
11641c99275SPeter Avalos 	type_s = tok2str(type2str, "unknown type (%u)", type);
117*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("VRRPv%u, %s", version, type_s);
11841c99275SPeter Avalos 	if (ttl != 255)
119*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(", (ttl %u)", ttl);
120411677aeSAaron LI 	if (version < 2 || version > 3 || type != VRRP_TYPE_ADVERTISEMENT)
12141c99275SPeter Avalos 		return;
122*ed775ee7SAntonio Huete Jimenez 	ND_PRINT(", vrid %u, prio %u", GET_U_1(bp + 1), GET_U_1(bp + 2));
123411677aeSAaron LI 
124411677aeSAaron LI 	if (version == 2) {
125*ed775ee7SAntonio Huete Jimenez 		auth_type = GET_U_1(bp + 4);
126*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(", authtype %s", tok2str(auth2str, NULL, auth_type));
127*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(", intvl %us, length %u", GET_U_1(bp + 5), len);
128411677aeSAaron LI 	} else { /* version == 3 */
129*ed775ee7SAntonio Huete Jimenez 		uint16_t intvl = (GET_U_1(bp + 4) & 0x0f) << 8 | GET_U_1(bp + 5);
130*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(", intvl %ucs, length %u", intvl, len);
131411677aeSAaron LI 	}
132411677aeSAaron LI 
133411677aeSAaron LI 	if (ndo->ndo_vflag) {
134*ed775ee7SAntonio Huete Jimenez 		u_int naddrs = GET_U_1(bp + 3);
135*ed775ee7SAntonio Huete Jimenez 		u_int i;
13641c99275SPeter Avalos 		char c;
13741c99275SPeter Avalos 
138*ed775ee7SAntonio Huete Jimenez 		if (version == 2 && ND_TTEST_LEN(bp, len)) {
13927bfbee1SPeter Avalos 			struct cksum_vec vec[1];
14027bfbee1SPeter Avalos 
14127bfbee1SPeter Avalos 			vec[0].ptr = bp;
14227bfbee1SPeter Avalos 			vec[0].len = len;
143*ed775ee7SAntonio Huete Jimenez 			if (in_cksum(vec, 1))
144*ed775ee7SAntonio Huete Jimenez 				ND_PRINT(", (bad vrrp cksum %x)",
145*ed775ee7SAntonio Huete Jimenez 					GET_BE_U_2(bp + 6));
146411677aeSAaron LI 		}
147411677aeSAaron LI 
148*ed775ee7SAntonio Huete Jimenez 		if (version == 3 && ND_TTEST_LEN(bp, len)) {
149411677aeSAaron LI 			uint16_t cksum = nextproto4_cksum(ndo, (const struct ip *)bp2, bp,
150411677aeSAaron LI 				len, len, IPPROTO_VRRP);
151*ed775ee7SAntonio Huete Jimenez 			if (cksum)
152*ed775ee7SAntonio Huete Jimenez 				ND_PRINT(", (bad vrrp cksum %x)",
153*ed775ee7SAntonio Huete Jimenez 					GET_BE_U_2(bp + 6));
154411677aeSAaron LI 		}
155411677aeSAaron LI 
156*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(", addrs");
15741c99275SPeter Avalos 		if (naddrs > 1)
158*ed775ee7SAntonio Huete Jimenez 			ND_PRINT("(%u)", naddrs);
159*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(":");
16041c99275SPeter Avalos 		c = ' ';
16141c99275SPeter Avalos 		bp += 8;
16241c99275SPeter Avalos 		for (i = 0; i < naddrs; i++) {
163*ed775ee7SAntonio Huete Jimenez 			ND_PRINT("%c%s", c, GET_IPADDR_STRING(bp));
16441c99275SPeter Avalos 			c = ',';
16541c99275SPeter Avalos 			bp += 4;
16641c99275SPeter Avalos 		}
167411677aeSAaron LI 		if (version == 2 && auth_type == VRRP_AUTH_SIMPLE) { /* simple text password */
168*ed775ee7SAntonio Huete Jimenez 			ND_PRINT(" auth \"");
169*ed775ee7SAntonio Huete Jimenez 			/*
170*ed775ee7SAntonio Huete Jimenez 			 * RFC 2338 Section 5.3.10: "If the configured authentication string
171*ed775ee7SAntonio Huete Jimenez 			 * is shorter than 8 bytes, the remaining space MUST be zero-filled.
172*ed775ee7SAntonio Huete Jimenez 			 */
173*ed775ee7SAntonio Huete Jimenez 			nd_printjnp(ndo, bp, 8);
174*ed775ee7SAntonio Huete Jimenez 			ND_PRINT("\"");
17541c99275SPeter Avalos 		}
17641c99275SPeter Avalos 	}
17741c99275SPeter Avalos }
178