]>
git.proxmox.com Git - mirror_frr.git/blob - vrrpd/vrrp_packet.h
2 * VRRP packet crafting.
3 * Copyright (C) 2018-2019 Cumulus Networks, Inc.
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)
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
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
20 #ifndef __VRRP_PACKET_H__
21 #define __VRRP_PACKET_H__
25 #include "lib/ipaddr.h"
26 #include "lib/memory.h"
27 #include "lib/prefix.h"
29 #define VRRP_TYPE_ADVERTISEMENT 1
32 * Shared header for VRRPv2/v3 packets.
47 /* advertisement interval (in sec) */
52 * advertisement interval (in centiseconds)
61 } __attribute__((packed
));
63 #define VRRP_HDR_SIZE sizeof(struct vrrp_hdr)
68 * When used, this is actually an array of one or the other, not an
69 * array of union. If N v4 addresses are stored then
70 * sizeof(addrs) == N * sizeof(struct in_addr).
72 * Under v2, the last 2 entries in this array are the authentication
73 * data fields. We don't support auth in v2 so these are always just 8
80 } __attribute__((packed
));
82 #define VRRP_PKT_SIZE(_f, _ver, _naddr) \
84 size_t _asz = ((_f) == AF_INET) ? sizeof(struct in_addr) \
85 : sizeof(struct in6_addr); \
86 size_t _auth = 2 * sizeof(uint32_t) * (3 - (_ver)); \
87 sizeof(struct vrrp_hdr) + (_asz * (_naddr)) + _auth; \
90 #define VRRP_MIN_PKT_SIZE_V4 VRRP_PKT_SIZE(AF_INET, 3, 1)
91 #define VRRP_MAX_PKT_SIZE_V4 VRRP_PKT_SIZE(AF_INET, 2, 255)
92 #define VRRP_MIN_PKT_SIZE_V6 VRRP_PKT_SIZE(AF_INET6, 3, 1)
93 #define VRRP_MAX_PKT_SIZE_V6 VRRP_PKT_SIZE(AF_INET6, 3, 255)
95 #define VRRP_MIN_PKT_SIZE VRRP_MIN_PKT_SIZE_V4
96 #define VRRP_MAX_PKT_SIZE VRRP_MAX_PKT_SIZE_V6
99 * Builds a VRRP ADVERTISEMENT packet.
102 * Pointer to store pointer to result buffer in
105 * Source address packet will be transmitted from. This is needed to compute
106 * the VRRP checksum. The returned packet must be sent in an IP datagram with
107 * the source address equal to this field, or the checksum will be invalid.
110 * VRRP version; must be 2 or 3
113 * Virtual Router Identifier
116 * Virtual Router Priority
119 * time between ADVERTISEMENTs
122 * whether 'ips' is an array of v4 or v6 addresses
125 * number of IPvX addresses in 'ips'
128 * array of pointer to either struct in_addr (v6 = false) or struct in6_addr
131 ssize_t
vrrp_pkt_adver_build(struct vrrp_pkt
**pkt
, struct ipaddr
*src
,
132 uint8_t version
, uint8_t vrid
, uint8_t prio
,
133 uint16_t max_adver_int
, uint8_t numip
,
134 struct ipaddr
**ips
);
136 /* free memory allocated by vrrp_pkt_adver_build's pkt arg */
137 void vrrp_pkt_free(struct vrrp_pkt
*pkt
);
140 * Dumps a VRRP ADVERTISEMENT packet to a string.
142 * Currently only dumps the header.
145 * Buffer to store string representation
151 * Packet to dump to a string
154 * # bytes written to buf
156 size_t vrrp_pkt_adver_dump(char *buf
, size_t buflen
, struct vrrp_pkt
*pkt
);
160 * Parses a VRRP packet, checking for illegal or invalid data.
162 * This function parses both VRRPv2 and VRRPv3 packets. Which version is
163 * expected is determined by the version argument. For example, if version is 3
164 * and the received packet has version field 2 it will fail to parse.
166 * Note that this function only checks whether the packet itself is a valid
167 * VRRP packet. It is up to the caller to validate whether the VRID is correct,
168 * priority and timer values are correct, etc.
171 * Address family of received packet
174 * VRRP version to use for validation
177 * msghdr containing results of recvmsg() on VRRP router socket
180 * Return value of recvmsg() on VRRP router socket; must be non-negative
183 * Pointer to struct ipaddr to store address of datagram sender
186 * Pointer to pointer to set to location of VRRP packet within buf
189 * Buffer to store human-readable error message in case of error; may be
190 * NULL, in which case no message will be stored
196 * Size of VRRP packet, or -1 upon error
198 ssize_t
vrrp_pkt_parse_datagram(int family
, int version
, struct msghdr
*m
,
199 size_t read
, struct ipaddr
*src
,
200 struct vrrp_pkt
**pkt
, char *errmsg
,
203 #endif /* __VRRP_PACKET_H__ */