xref: /netbsd-src/external/bsd/tcpdump/dist/print-hsrp.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
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 /* Cisco Hot Standby Router Protocol (HSRP). */
33 
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: print-hsrp.c,v 1.7 2017/02/05 04:05:05 spz 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 
48 /* HSRP op code types. */
49 static const char *op_code_str[] = {
50 	"hello",
51 	"coup",
52 	"resign"
53 };
54 
55 /* HSRP states and associated names. */
56 static const struct tok states[] = {
57 	{  0, "initial" },
58 	{  1, "learn" },
59 	{  2, "listen" },
60 	{  4, "speak" },
61 	{  8, "standby" },
62 	{ 16, "active" },
63 	{  0, NULL }
64 };
65 
66 /*
67  * RFC 2281:
68  *
69  *  0                   1                   2                   3
70  *  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
71  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72  * |   Version     |   Op Code     |     State     |   Hellotime   |
73  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74  * |   Holdtime    |   Priority    |     Group     |   Reserved    |
75  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76  * |                      Authentication  Data                     |
77  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
78  * |                      Authentication  Data                     |
79  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
80  * |                      Virtual IP Address                       |
81  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
82  */
83 
84 #define HSRP_AUTH_SIZE	8
85 
86 /* HSRP protocol header. */
87 struct hsrp {
88 	uint8_t		hsrp_version;
89 	uint8_t		hsrp_op_code;
90 	uint8_t		hsrp_state;
91 	uint8_t		hsrp_hellotime;
92 	uint8_t		hsrp_holdtime;
93 	uint8_t		hsrp_priority;
94 	uint8_t		hsrp_group;
95 	uint8_t		hsrp_reserved;
96 	uint8_t		hsrp_authdata[HSRP_AUTH_SIZE];
97 	struct in_addr	hsrp_virtaddr;
98 };
99 
100 void
101 hsrp_print(netdissect_options *ndo, register const uint8_t *bp, register u_int len)
102 {
103 	const struct hsrp *hp = (const struct hsrp *) bp;
104 
105 	ND_TCHECK(hp->hsrp_version);
106 	ND_PRINT((ndo, "HSRPv%d", hp->hsrp_version));
107 	if (hp->hsrp_version != 0)
108 		return;
109 	ND_TCHECK(hp->hsrp_op_code);
110 	ND_PRINT((ndo, "-"));
111 	ND_PRINT((ndo, "%s ", tok2strary(op_code_str, "unknown (%d)", hp->hsrp_op_code)));
112 	ND_PRINT((ndo, "%d: ", len));
113 	ND_TCHECK(hp->hsrp_state);
114 	ND_PRINT((ndo, "state=%s ", tok2str(states, "Unknown (%d)", hp->hsrp_state)));
115 	ND_TCHECK(hp->hsrp_group);
116 	ND_PRINT((ndo, "group=%d ", hp->hsrp_group));
117 	ND_TCHECK(hp->hsrp_reserved);
118 	if (hp->hsrp_reserved != 0) {
119 		ND_PRINT((ndo, "[reserved=%d!] ", hp->hsrp_reserved));
120 	}
121 	ND_TCHECK(hp->hsrp_virtaddr);
122 	ND_PRINT((ndo, "addr=%s", ipaddr_string(ndo, &hp->hsrp_virtaddr)));
123 	if (ndo->ndo_vflag) {
124 		ND_PRINT((ndo, " hellotime="));
125 		unsigned_relts_print(ndo, hp->hsrp_hellotime);
126 		ND_PRINT((ndo, " holdtime="));
127 		unsigned_relts_print(ndo, hp->hsrp_holdtime);
128 		ND_PRINT((ndo, " priority=%d", hp->hsrp_priority));
129 		ND_PRINT((ndo, " auth=\""));
130 		if (fn_printn(ndo, hp->hsrp_authdata, sizeof(hp->hsrp_authdata),
131 		    ndo->ndo_snapend)) {
132 			ND_PRINT((ndo, "\""));
133 			goto trunc;
134 		}
135 		ND_PRINT((ndo, "\""));
136 	}
137 	return;
138 trunc:
139 	ND_PRINT((ndo, "[|hsrp]"));
140 }
141