]> git.proxmox.com Git - mirror_frr.git/blame - vrrpd/vrrp_packet.h
vrrpd: add debugging knobs
[mirror_frr.git] / vrrpd / vrrp_packet.h
CommitLineData
5435a2bf 1/*
63d4bd12
QY
2 * VRRP packet crafting.
3 * Copyright (C) 2018-2019 Cumulus Networks, Inc.
4 * Quentin Young
5435a2bf
QY
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
63d4bd12
QY
20#ifndef __VRRP_PACKET_H__
21#define __VRRP_PACKET_H__
22
5435a2bf
QY
23#include <zebra.h>
24
63d4bd12
QY
25#include "lib/ipaddr.h"
26#include "lib/memory.h"
27#include "lib/prefix.h"
5435a2bf 28
5435a2bf
QY
29#define VRRP_TYPE_ADVERTISEMENT 1
30
91188ca6
QY
31extern const char *vrrp_packet_names[16];
32
ef4cc1eb
QY
33/*
34 * Shared header for VRRPv2/v3 packets.
35 */
36struct vrrp_hdr {
3eca3857
QY
37 /*
38 * H L H L
39 * 0000 0000
40 * ver type
41 */
42 uint8_t vertype;
5435a2bf
QY
43 uint8_t vrid;
44 uint8_t priority;
ef4cc1eb
QY
45 uint8_t naddr;
46 union {
47 struct {
48 uint8_t auth_type;
49 /* advertisement interval (in sec) */
50 uint8_t adver_int;
51 } v2;
52 struct {
3eca3857
QY
53 /*
54 * advertisement interval (in centiseconds)
55 * H L H L
56 * 0000 000000000000
57 * rsvd adver_int
58 */
59 uint16_t adver_int;
ef4cc1eb
QY
60 } v3;
61 };
62 uint16_t chksum;
91188ca6
QY
63} __attribute__((packed));
64
65#define VRRP_HDR_SIZE sizeof(struct vrrp_hdr)
ef4cc1eb
QY
66
67struct vrrp_pkt {
68 struct vrrp_hdr hdr;
91188ca6
QY
69 /*
70 * When used, this is actually an array of one or the other, not an
71 * array of union. If N v4 addresses are stored then
72 * sizeof(addrs) == N * sizeof(struct in_addr).
73 */
5435a2bf
QY
74 union {
75 struct in_addr v4;
76 struct in6_addr v6;
77 } addrs[];
91188ca6
QY
78} __attribute__((packed));
79
80#define VRRP_PKT_SIZE(_f, _naddr) \
81 ({ \
82 size_t _asz = ((_f) == AF_INET) ? sizeof(struct in_addr) \
83 : sizeof(struct in6_addr); \
84 sizeof(struct vrrp_hdr) + (_asz * (_naddr)); \
85 })
86
87#define VRRP_MIN_PKT_SIZE_V4 VRRP_PKT_SIZE(AF_INET, 1)
88#define VRRP_MAX_PKT_SIZE_V4 VRRP_PKT_SIZE(AF_INET, 255)
89#define VRRP_MIN_PKT_SIZE_V6 VRRP_PKT_SIZE(AF_INET6, 1)
90#define VRRP_MAX_PKT_SIZE_V6 VRRP_PKT_SIZE(AF_INET6, 255)
91
92#define VRRP_MIN_PKT_SIZE VRRP_MIN_PKT_SIZE_V4
93#define VRRP_MAX_PKT_SIZE VRRP_MAX_PKT_SIZE_V6
5435a2bf
QY
94
95/*
d9e01e1c 96 * Builds a VRRP ADVERTISEMENT packet.
3eca3857
QY
97 *
98 * pkt
99 * Pointer to store pointer to result buffer in
100 *
d9e01e1c
QY
101 * src
102 * Source address packet will be transmitted from. This is needed to compute
103 * the VRRP checksum. The returned packet must be sent in an IP datagram with
104 * the source address equal to this field, or the checksum will be invalid.
105 *
106 * version
107 * VRRP version; must be 2 or 3
108 *
3eca3857
QY
109 * vrid
110 * Virtual Router Identifier
111 *
112 * prio
113 * Virtual Router Priority
114 *
115 * max_adver_int
116 * time between ADVERTISEMENTs
117 *
118 * v6
119 * whether 'ips' is an array of v4 or v6 addresses
120 *
121 * numip
122 * number of IPvX addresses in 'ips'
123 *
124 * ips
125 * array of pointer to either struct in_addr (v6 = false) or struct in6_addr
126 * (v6 = true)
5435a2bf 127 */
d9e01e1c
QY
128ssize_t vrrp_pkt_adver_build(struct vrrp_pkt **pkt, struct ipaddr *src,
129 uint8_t version, uint8_t vrid, uint8_t prio,
130 uint16_t max_adver_int, uint8_t numip,
131 struct ipaddr **ips);
91188ca6
QY
132
133/*
d9e01e1c 134 * Dumps a VRRP ADVERTISEMENT packet to a string.
91188ca6
QY
135 *
136 * Currently only dumps the header.
137 *
138 * buf
139 * Buffer to store string representation
140 *
141 * buflen
142 * Size of buf
143 *
144 * pkt
145 * Packet to dump to a string
146 *
147 * Returns:
148 * # bytes written to buf
149 */
d9e01e1c 150size_t vrrp_pkt_adver_dump(char *buf, size_t buflen, struct vrrp_pkt *pkt);
91188ca6
QY
151
152
153/*
154 * Parses a VRRP packet, checking for illegal or invalid data.
155 *
8cb3d803
QY
156 * This function parses both VRRPv2 and VRRPv3 packets. Which version is
157 * expected is determined by the version argument. For example, if version is 3
158 * and the received packet has version field 2 it will fail to parse.
159 *
160 * Note that this function only checks whether the packet itself is a valid
161 * VRRP packet. It is up to the caller to validate whether the VRID is correct,
162 * priority and timer values are correct, etc.
91188ca6
QY
163 *
164 * family
165 * Address family of received packet
166 *
8cb3d803
QY
167 * version
168 * VRRP version to use for validation
169 *
91188ca6
QY
170 * m
171 * msghdr containing results of recvmsg() on VRRP router socket
172 *
173 * read
8cb3d803 174 * Return value of recvmsg() on VRRP router socket; must be non-negative
91188ca6 175 *
d04bb25a
QY
176 * src
177 * Pointer to struct ipaddr to store address of datagram sender
178 *
91188ca6
QY
179 * pkt
180 * Pointer to pointer to set to location of VRRP packet within buf
181 *
182 * errmsg
183 * Buffer to store human-readable error message in case of error; may be
184 * NULL, in which case no message will be stored
185 *
186 * errmsg_len
187 * Size of errmsg
188 *
189 * Returns:
190 * Size of VRRP packet, or -1 upon error
191 */
8cb3d803
QY
192ssize_t vrrp_pkt_parse_datagram(int family, int version, struct msghdr *m,
193 size_t read, struct ipaddr *src,
194 struct vrrp_pkt **pkt, char *errmsg,
195 size_t errmsg_len);
63d4bd12
QY
196
197#endif /* __VRRP_PACKET_H__ */