xref: /freebsd/contrib/tcpdump/print-vrrp.c (revision 09e8dea79366f1e5b3a73e8a271b26e4b6bf2e6a)
1 /*
2  * Copyright (c) 2000 William C. Fenner.
3  *                All rights reserved.
4  *
5  * Kevin Steves <ks@hp.se> July 2000
6  * Modified to:
7  * - print version, type string and packet length
8  * - print IP address count if > 1 (-v)
9  * - verify checksum (-v)
10  * - print authentication string (-v)
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that: (1) source code
14  * distributions retain the above copyright notice and this paragraph
15  * in its entirety, and (2) distributions including binary code include
16  * the above copyright notice and this paragraph in its entirety in
17  * the documentation or other materials provided with the distribution.
18  * The name of William C. Fenner may not be used to endorse or
19  * promote products derived from this software without specific prior
20  * written permission.  THIS SOFTWARE IS PROVIDED ``AS IS'' AND
21  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
22  * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE.
24  */
25 
26 #ifndef lint
27 static const char rcsid[] =
28     "@(#) $Header: /tcpdump/master/tcpdump/print-vrrp.c,v 1.5 2001/07/23 22:27:30 fenner Exp $";
29 #endif
30 
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 
39 #include <netinet/in.h>
40 
41 #include "interface.h"
42 #include "extract.h"
43 #include "addrtoname.h"
44 
45 /*
46  * RFC 2338:
47  *     0                   1                   2                   3
48  *     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
49  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50  *    |Version| Type  | Virtual Rtr ID|   Priority    | Count IP Addrs|
51  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52  *    |   Auth Type   |   Adver Int   |          Checksum             |
53  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54  *    |                         IP Address (1)                        |
55  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56  *    |                            .                                  |
57  *    |                            .                                  |
58  *    |                            .                                  |
59  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60  *    |                         IP Address (n)                        |
61  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62  *    |                     Authentication Data (1)                   |
63  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64  *    |                     Authentication Data (2)                   |
65  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66  */
67 
68 /* Type */
69 #define	VRRP_TYPE_ADVERTISEMENT	1
70 
71 static const struct tok type2str[] = {
72 	{ VRRP_TYPE_ADVERTISEMENT,	"advertisement"	},
73 	{ 0,				NULL		}
74 };
75 
76 /* Auth Type */
77 #define	VRRP_AUTH_NONE		0
78 #define	VRRP_AUTH_SIMPLE	1
79 #define	VRRP_AUTH_AH		2
80 
81 static const struct tok auth2str[] = {
82 	{ VRRP_AUTH_NONE,		"none"		},
83 	{ VRRP_AUTH_SIMPLE,		"simple"	},
84 	{ VRRP_AUTH_AH,			"ah"		},
85 	{ 0,				NULL		}
86 };
87 
88 void
89 vrrp_print(register const u_char *bp, register u_int len, int ttl)
90 {
91 	int version, type, auth_type;
92 	const char *type_s;
93 
94 	TCHECK(bp[0]);
95 	version = (bp[0] & 0xf0) >> 4;
96 	type = bp[0] & 0x0f;
97 	type_s = tok2str(type2str, "type#%d", type);
98 	printf("VRRPv%d-%s %d: ", version, type_s, len);
99 	if (ttl != 255)
100 		printf("[ttl=%d!] ", ttl);
101 	if (version != 2 || type != VRRP_TYPE_ADVERTISEMENT)
102 		return;
103 	TCHECK(bp[2]);
104 	printf("vrid=%d prio=%d", bp[1], bp[2]);
105 	TCHECK(bp[5]);
106 	auth_type = bp[4];
107 	printf(" authtype=%s", tok2str(auth2str, NULL, auth_type));
108 	printf(" intvl=%d", bp[5]);
109 	if (vflag) {
110 		int naddrs = bp[3];
111 		int i;
112 		char c;
113 
114 		if (TTEST2(bp[0], len) && in_cksum((const u_short*)bp, len, 0))
115 			printf(" (bad vrrp cksum %x!)",
116 				EXTRACT_16BITS(&bp[6]));
117 		printf(" addrs");
118 		if (naddrs > 1)
119 			printf("(%d)", naddrs);
120 		printf(":");
121 		c = ' ';
122 		bp += 8;
123 		for (i = 0; i < naddrs; i++) {
124 			TCHECK(bp[3]);
125 			printf("%c%s", c, ipaddr_string(bp));
126 			c = ',';
127 			bp += 4;
128 		}
129 		if (auth_type == VRRP_AUTH_SIMPLE) { /* simple text password */
130 			TCHECK(bp[7]);
131 			printf(" auth \"");
132 			fn_printn(bp, 8, NULL);
133 			printf("\"");
134 		}
135 	}
136 	return;
137 trunc:
138 	printf("[|vrrp]");
139 }
140