xref: /netbsd-src/external/bsd/tcpdump/dist/print-hsrp.c (revision ccd9df534e375a4366c5b55f23782053c7a98d82)
1 /*
2  * Copyright (C) 2001 Julian Cowley
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /* \summary: Cisco Hot Standby Router Protocol (HSRP) printer */
31 
32 /* specification: RFC 2281 for version 1 */
33 
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: print-hsrp.c,v 1.8 2023/08/17 20:19:40 christos Exp $");
37 #endif
38 
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42 
43 #include "netdissect-stdinc.h"
44 
45 #include "netdissect.h"
46 #include "addrtoname.h"
47 #include "extract.h"
48 
49 /* HSRP op code types. */
50 static const char *op_code_str[] = {
51 	"hello",
52 	"coup",
53 	"resign"
54 };
55 
56 /* HSRP states and associated names. */
57 static const struct tok states[] = {
58 	{  0, "initial" },
59 	{  1, "learn" },
60 	{  2, "listen" },
61 	{  4, "speak" },
62 	{  8, "standby" },
63 	{ 16, "active" },
64 	{  0, NULL }
65 };
66 
67 /*
68  * RFC 2281:
69  *
70  *  0                   1                   2                   3
71  *  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
72  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73  * |   Version     |   Op Code     |     State     |   Hellotime   |
74  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
75  * |   Holdtime    |   Priority    |     Group     |   Reserved    |
76  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
77  * |                      Authentication  Data                     |
78  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
79  * |                      Authentication  Data                     |
80  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
81  * |                      Virtual IP Address                       |
82  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83  */
84 
85 #define HSRP_AUTH_SIZE	8
86 
87 /* HSRP protocol header. */
88 struct hsrp {
89 	nd_uint8_t	hsrp_version;
90 	nd_uint8_t	hsrp_op_code;
91 	nd_uint8_t	hsrp_state;
92 	nd_uint8_t	hsrp_hellotime;
93 	nd_uint8_t	hsrp_holdtime;
94 	nd_uint8_t	hsrp_priority;
95 	nd_uint8_t	hsrp_group;
96 	nd_uint8_t	hsrp_reserved;
97 	nd_byte		hsrp_authdata[HSRP_AUTH_SIZE];
98 	nd_ipv4		hsrp_virtaddr;
99 };
100 
101 void
102 hsrp_print(netdissect_options *ndo, const u_char *bp, u_int len)
103 {
104 	const struct hsrp *hp = (const struct hsrp *) bp;
105 	uint8_t version;
106 
107 	ndo->ndo_protocol = "hsrp";
108 	version = GET_U_1(hp->hsrp_version);
109 	ND_PRINT("HSRPv%u", version);
110 	if (version != 0)
111 		return;
112 	ND_PRINT("-");
113 	ND_PRINT("%s ",
114 		 tok2strary(op_code_str, "unknown (%u)", GET_U_1(hp->hsrp_op_code)));
115 	ND_PRINT("%u: ", len);
116 	ND_PRINT("state=%s ",
117 		 tok2str(states, "Unknown (%u)", GET_U_1(hp->hsrp_state)));
118 	ND_PRINT("group=%u ", GET_U_1(hp->hsrp_group));
119 	if (GET_U_1(hp->hsrp_reserved) != 0) {
120 		ND_PRINT("[reserved=%u!] ", GET_U_1(hp->hsrp_reserved));
121 	}
122 	ND_PRINT("addr=%s", GET_IPADDR_STRING(hp->hsrp_virtaddr));
123 	if (ndo->ndo_vflag) {
124 		ND_PRINT(" hellotime=");
125 		unsigned_relts_print(ndo, GET_U_1(hp->hsrp_hellotime));
126 		ND_PRINT(" holdtime=");
127 		unsigned_relts_print(ndo, GET_U_1(hp->hsrp_holdtime));
128 		ND_PRINT(" priority=%u", GET_U_1(hp->hsrp_priority));
129 		ND_PRINT(" auth=\"");
130 		/*
131 		 * RFC 2281 Section 5.1 does not specify the encoding of
132 		 * Authentication Data explicitly, but zero padding can be
133 		 * inferred from the "recommended default value".
134 		 */
135 		nd_printjnp(ndo, hp->hsrp_authdata, HSRP_AUTH_SIZE);
136 		ND_PRINT("\"");
137 	}
138 }
139