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.9 2024/09/02 16:15:31 christos Exp $");
37 #endif
38 
39 #include <config.h>
40 
41 #include "netdissect-stdinc.h"
42 
43 #include "netdissect.h"
44 #include "addrtoname.h"
45 #include "extract.h"
46 
47 /* HSRP op code types. */
48 static const char *op_code_str[] = {
49           "hello",
50           "coup",
51           "resign"
52 };
53 
54 /* HSRP states and associated names. */
55 static const struct tok states[] = {
56           {  0, "initial" },
57           {  1, "learn" },
58           {  2, "listen" },
59           {  4, "speak" },
60           {  8, "standby" },
61           { 16, "active" },
62           {  0, NULL }
63 };
64 
65 /*
66  * RFC 2281:
67  *
68  *  0                   1                   2                   3
69  *  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
70  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71  * |   Version     |   Op Code     |     State     |   Hellotime   |
72  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73  * |   Holdtime    |   Priority    |     Group     |   Reserved    |
74  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
75  * |                      Authentication  Data                     |
76  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
77  * |                      Authentication  Data                     |
78  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
79  * |                      Virtual IP Address                       |
80  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
81  */
82 
83 #define HSRP_AUTH_SIZE        8
84 
85 /* HSRP protocol header. */
86 struct hsrp {
87           nd_uint8_t          hsrp_version;
88           nd_uint8_t          hsrp_op_code;
89           nd_uint8_t          hsrp_state;
90           nd_uint8_t          hsrp_hellotime;
91           nd_uint8_t          hsrp_holdtime;
92           nd_uint8_t          hsrp_priority;
93           nd_uint8_t          hsrp_group;
94           nd_uint8_t          hsrp_reserved;
95           nd_byte             hsrp_authdata[HSRP_AUTH_SIZE];
96           nd_ipv4             hsrp_virtaddr;
97 };
98 
99 void
hsrp_print(netdissect_options * ndo,const u_char * bp,u_int len)100 hsrp_print(netdissect_options *ndo, const u_char *bp, u_int len)
101 {
102           const struct hsrp *hp = (const struct hsrp *) bp;
103           uint8_t version;
104 
105           ndo->ndo_protocol = "hsrp";
106           version = GET_U_1(hp->hsrp_version);
107           ND_PRINT("HSRPv%u", version);
108           if (version != 0)
109                     return;
110           ND_PRINT("-");
111           ND_PRINT("%s ",
112                      tok2strary(op_code_str, "unknown (%u)", GET_U_1(hp->hsrp_op_code)));
113           ND_PRINT("%u: ", len);
114           ND_PRINT("state=%s ",
115                      tok2str(states, "Unknown (%u)", GET_U_1(hp->hsrp_state)));
116           ND_PRINT("group=%u ", GET_U_1(hp->hsrp_group));
117           if (GET_U_1(hp->hsrp_reserved) != 0) {
118                     ND_PRINT("[reserved=%u!] ", GET_U_1(hp->hsrp_reserved));
119           }
120           ND_PRINT("addr=%s", GET_IPADDR_STRING(hp->hsrp_virtaddr));
121           if (ndo->ndo_vflag) {
122                     ND_PRINT(" hellotime=");
123                     unsigned_relts_print(ndo, GET_U_1(hp->hsrp_hellotime));
124                     ND_PRINT(" holdtime=");
125                     unsigned_relts_print(ndo, GET_U_1(hp->hsrp_holdtime));
126                     ND_PRINT(" priority=%u", GET_U_1(hp->hsrp_priority));
127                     ND_PRINT(" auth=\"");
128                     /*
129                      * RFC 2281 Section 5.1 does not specify the encoding of
130                      * Authentication Data explicitly, but zero padding can be
131                      * inferred from the "recommended default value".
132                      */
133                     nd_printjnp(ndo, hp->hsrp_authdata, HSRP_AUTH_SIZE);
134                     ND_PRINT("\"");
135           }
136 }
137