]>
git.proxmox.com Git - mirror_frr.git/blob - vrrpd/vrrp_packet.c
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
21 #include <netinet/in.h>
22 #include <netinet/ip.h>
23 #include <netinet/ip6.h>
25 #include "lib/checksum.h"
26 #include "lib/ipaddr.h"
27 #include "lib/memory.h"
30 #include "vrrp_debug.h"
31 #include "vrrp_memory.h"
32 #include "vrrp_packet.h"
34 /* clang-format off */
35 const char *vrrp_packet_names
[16] = {
37 [VRRP_TYPE_ADVERTISEMENT
] = "ADVERTISEMENT",
56 * Compute the VRRP checksum.
58 * Checksum is not set in the packet, just computed.
61 * VRRP packet, fully filled out except for checksum field.
67 * IP address that pkt will be transmitted from.
70 * VRRP checksum in network byte order.
72 static uint16_t vrrp_pkt_checksum(struct vrrp_pkt
*pkt
, size_t pktsize
,
76 bool v6
= (src
->ipa_type
== IPADDR_V6
);
78 uint16_t chksum_pre
= pkt
->hdr
.chksum
;
82 struct ipv6_ph ph
= {};
83 ph
.src
= src
->ipaddr_v6
;
84 inet_pton(AF_INET6
, VRRP_MCASTV6_GROUP_STR
, &ph
.dst
);
85 ph
.ulpl
= htons(pktsize
);
87 chksum
= in_cksum_with_ph6(&ph
, pkt
, pktsize
);
88 } else if (!v6
&& ((pkt
->hdr
.vertype
>> 4) == 3)) {
89 struct ipv4_ph ph
= {};
90 ph
.src
= src
->ipaddr_v4
;
91 inet_pton(AF_INET
, VRRP_MCASTV4_GROUP_STR
, &ph
.dst
);
93 ph
.len
= htons(pktsize
);
94 chksum
= in_cksum_with_ph4(&ph
, pkt
, pktsize
);
95 } else if (!v6
&& ((pkt
->hdr
.vertype
>> 4) == 2)) {
96 chksum
= in_cksum(pkt
, pktsize
);
98 assert(!"Invalid VRRP protocol version");
101 pkt
->hdr
.chksum
= chksum_pre
;
106 ssize_t
vrrp_pkt_adver_build(struct vrrp_pkt
**pkt
, struct ipaddr
*src
,
107 uint8_t version
, uint8_t vrid
, uint8_t prio
,
108 uint16_t max_adver_int
, uint8_t numip
,
114 assert(version
>= 2 && version
<= 3);
115 assert(!(version
== 2 && v6
));
118 v6
= IS_IPADDR_V6(ips
[0]);
119 addrsz
= IPADDRSZ(ips
[0]);
122 size_t pktsize
= VRRP_PKT_SIZE(v6
? AF_INET6
: AF_INET
, numip
);
123 *pkt
= XCALLOC(MTYPE_VRRP_PKT
, pktsize
);
125 (*pkt
)->hdr
.vertype
|= version
<< 4;
126 (*pkt
)->hdr
.vertype
|= VRRP_TYPE_ADVERTISEMENT
;
127 (*pkt
)->hdr
.vrid
= vrid
;
128 (*pkt
)->hdr
.priority
= prio
;
129 (*pkt
)->hdr
.naddr
= numip
;
131 (*pkt
)->hdr
.v3
.adver_int
= htons(max_adver_int
);
132 else if (version
== 2) {
133 (*pkt
)->hdr
.v2
.auth_type
= 0;
134 (*pkt
)->hdr
.v2
.adver_int
= MAX(max_adver_int
/ 100, 1);
137 uint8_t *aptr
= (void *)(*pkt
)->addrs
;
139 for (int i
= 0; i
< numip
; i
++) {
140 memcpy(aptr
, &ips
[i
]->ip
.addr
, addrsz
);
144 (*pkt
)->hdr
.chksum
= vrrp_pkt_checksum(*pkt
, pktsize
, src
);
149 size_t vrrp_pkt_adver_dump(char *buf
, size_t buflen
, struct vrrp_pkt
*pkt
)
156 struct vrrp_hdr
*hdr
= &pkt
->hdr
;
159 snprintf(tmpbuf
, sizeof(tmpbuf
), "Version: %u\n", (hdr
->vertype
>> 4));
160 rs
+= strlcat(buf
, tmpbuf
, buflen
);
161 snprintf(tmpbuf
, sizeof(tmpbuf
), "Type: %u (%s)\n",
162 (hdr
->vertype
& 0x0F),
163 vrrp_packet_names
[(hdr
->vertype
& 0x0F)]);
164 rs
+= strlcat(buf
, tmpbuf
, buflen
);
165 snprintf(tmpbuf
, sizeof(tmpbuf
), "VRID: %u\n", hdr
->vrid
);
166 rs
+= strlcat(buf
, tmpbuf
, buflen
);
167 snprintf(tmpbuf
, sizeof(tmpbuf
), "Priority: %u\n", hdr
->priority
);
168 rs
+= strlcat(buf
, tmpbuf
, buflen
);
169 snprintf(tmpbuf
, sizeof(tmpbuf
), "Count IPvX: %u\n", hdr
->naddr
);
170 rs
+= strlcat(buf
, tmpbuf
, buflen
);
171 snprintf(tmpbuf
, sizeof(tmpbuf
), "Max Adver Int: %u\n",
172 ntohs(hdr
->v3
.adver_int
));
173 rs
+= strlcat(buf
, tmpbuf
, buflen
);
174 snprintf(tmpbuf
, sizeof(tmpbuf
), "Checksum: %x\n", ntohs(hdr
->chksum
));
175 rs
+= strlcat(buf
, tmpbuf
, buflen
);
180 ssize_t
vrrp_pkt_parse_datagram(int family
, int version
, struct msghdr
*m
,
181 size_t read
, struct ipaddr
*src
,
182 struct vrrp_pkt
**pkt
, char *errmsg
,
185 /* Source (MAC & IP), Dest (MAC & IP) TTL validation done by kernel */
186 size_t addrsz
= (family
== AF_INET
) ? sizeof(struct in_addr
)
187 : sizeof(struct in6_addr
);
190 uint8_t *buf
= m
->msg_iov
->iov_base
;
192 #define VRRP_PKT_VCHECK(cond, _f, ...) \
196 snprintf(errmsg, errmsg_len, (_f), \
202 /* IPvX header check */
204 if (family
== AF_INET
) {
206 read
>= sizeof(struct ip
),
207 "Datagram not large enough to contain IP header");
209 struct ip
*ip
= (struct ip
*)buf
;
211 /* IP total length check */
213 ntohs(ip
->ip_len
) == read
,
214 "IPv4 packet length field does not match # received bytes; %u != %lu",
215 ntohs(ip
->ip_len
), read
);
218 VRRP_PKT_VCHECK(ip
->ip_ttl
== 255,
219 "IPv4 TTL is %" PRIu8
"; should be 255",
222 *pkt
= (struct vrrp_pkt
*)(buf
+ (ip
->ip_hl
<< 2));
223 pktsize
= read
- (ip
->ip_hl
<< 2);
225 /* IP empty packet check */
226 VRRP_PKT_VCHECK(pktsize
> 0, "IPv4 packet has no payload");
228 /* Extract source address */
229 struct sockaddr_in
*sa
= m
->msg_name
;
230 src
->ipa_type
= IPADDR_V4
;
231 src
->ipaddr_v4
= sa
->sin_addr
;
232 } else if (family
== AF_INET6
) {
234 for (c
= CMSG_FIRSTHDR(m
); c
!= NULL
; CMSG_NXTHDR(m
, c
)) {
235 if (c
->cmsg_level
== IPPROTO_IPV6
236 && c
->cmsg_type
== IPV6_HOPLIMIT
)
240 VRRP_PKT_VCHECK(!!c
, "IPv6 Hop Limit not received");
242 uint8_t *hoplimit
= CMSG_DATA(c
);
243 VRRP_PKT_VCHECK(*hoplimit
== 255,
244 "IPv6 Hop Limit is %" PRIu8
"; should be 255",
247 *pkt
= (struct vrrp_pkt
*)buf
;
250 /* Extract source address */
251 struct sockaddr_in6
*sa
= m
->msg_name
;
252 src
->ipa_type
= IPADDR_V6
;
253 memcpy(&src
->ipaddr_v6
, &sa
->sin6_addr
,
254 sizeof(struct in6_addr
));
256 assert(!"Unknown address family");
260 size_t minsize
= (family
== AF_INET
) ? VRRP_MIN_PKT_SIZE_V4
261 : VRRP_MIN_PKT_SIZE_V6
;
262 size_t maxsize
= (family
== AF_INET
) ? VRRP_MAX_PKT_SIZE_V4
263 : VRRP_MAX_PKT_SIZE_V6
;
264 VRRP_PKT_VCHECK(pktsize
>= minsize
,
265 "VRRP packet is undersized (%zu < %zu)", pktsize
,
267 VRRP_PKT_VCHECK(pktsize
<= maxsize
,
268 "VRRP packet is oversized (%zu > %zu)", pktsize
,
272 uint8_t pktver
= (*pkt
)->hdr
.vertype
>> 4;
273 VRRP_PKT_VCHECK(pktver
== version
, "Bad version %u", pktver
);
276 uint16_t chksum
= vrrp_pkt_checksum(*pkt
, pktsize
, src
);
277 VRRP_PKT_VCHECK((*pkt
)->hdr
.chksum
== chksum
,
278 "Bad VRRP checksum %" PRIx16
"; should be %" PRIx16
"",
279 (*pkt
)->hdr
.chksum
, chksum
);
282 VRRP_PKT_VCHECK(((*pkt
)->hdr
.vertype
& 0x0F) == 1, "Bad type %" PRIu8
,
283 (*pkt
)->hdr
.vertype
& 0x0f);
285 /* # addresses check */
286 size_t ves
= VRRP_PKT_SIZE(family
, (*pkt
)->hdr
.naddr
);
287 VRRP_PKT_VCHECK(pktsize
== ves
, "Packet has incorrect # addresses");
289 /* auth type check */
291 VRRP_PKT_VCHECK((*pkt
)->hdr
.v2
.auth_type
== 0,
292 "Bad authentication type %" PRIu8
,
293 (*pkt
)->hdr
.v2
.auth_type
);
295 /* Addresses check */
296 char vbuf
[INET6_ADDRSTRLEN
];
297 uint8_t *p
= (uint8_t *)(*pkt
)->addrs
;
298 for (uint8_t i
= 0; i
< (*pkt
)->hdr
.naddr
; i
++) {
299 VRRP_PKT_VCHECK(inet_ntop(family
, p
, vbuf
, sizeof(vbuf
)),
300 "Bad IP address, #%" PRIu8
, i
);
304 /* Everything checks out */