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