]>
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_packet.h"
32 /* clang-format off */
33 const char *vrrp_packet_names
[16] = {
35 [VRRP_TYPE_ADVERTISEMENT
] = "ADVERTISEMENT",
53 ssize_t
vrrp_pkt_build(struct vrrp_pkt
**pkt
, struct ipaddr
*src
, uint8_t vrid
,
54 uint8_t prio
, uint16_t max_adver_int
, uint8_t numip
,
57 bool v6
= IS_IPADDR_V6(ips
[0]);
59 size_t addrsz
= v6
? sizeof(struct in6_addr
) : sizeof(struct in_addr
);
60 size_t pktsize
= VRRP_PKT_SIZE(v6
? AF_INET6
: AF_INET
, numip
);
61 *pkt
= XCALLOC(MTYPE_TMP
, pktsize
);
63 (*pkt
)->hdr
.vertype
|= VRRP_VERSION
<< 4;
64 (*pkt
)->hdr
.vertype
|= VRRP_TYPE_ADVERTISEMENT
;
65 (*pkt
)->hdr
.vrid
= vrid
;
66 (*pkt
)->hdr
.priority
= prio
;
67 (*pkt
)->hdr
.naddr
= numip
;
68 (*pkt
)->hdr
.v3
.adver_int
= htons(max_adver_int
);
70 uint8_t *aptr
= (void *)(*pkt
)->addrs
;
72 for (int i
= 0; i
< numip
; i
++) {
73 memcpy(aptr
, &ips
[i
]->ip
.addr
, addrsz
);
77 (*pkt
)->hdr
.chksum
= 0;
80 struct ipv6_ph ph
= {};
81 ph
.src
= src
->ipaddr_v6
;
82 inet_pton(AF_INET6
, VRRP_MCASTV6_GROUP_STR
, &ph
.dst
);
83 ph
.ulpl
= htons(pktsize
);
85 (*pkt
)->hdr
.chksum
= in_cksum_with_ph6(&ph
, *pkt
, pktsize
);
87 struct ipv4_ph ph
= {};
88 ph
.src
= src
->ipaddr_v4
;
89 inet_pton(AF_INET
, VRRP_MCASTV4_GROUP_STR
, &ph
.dst
);
91 ph
.len
= htons(pktsize
);
92 (*pkt
)->hdr
.chksum
= in_cksum_with_ph4(&ph
, *pkt
, pktsize
);
98 size_t vrrp_pkt_dump(char *buf
, size_t buflen
, struct vrrp_pkt
*pkt
)
105 struct vrrp_hdr
*hdr
= &pkt
->hdr
;
108 snprintf(tmpbuf
, sizeof(tmpbuf
), "Version: %u\n", (hdr
->vertype
>> 4));
109 rs
+= strlcat(buf
, tmpbuf
, buflen
);
110 snprintf(tmpbuf
, sizeof(tmpbuf
), "Type: %u (%s)\n",
111 (hdr
->vertype
& 0x0F),
112 vrrp_packet_names
[(hdr
->vertype
& 0x0F)]);
113 rs
+= strlcat(buf
, tmpbuf
, buflen
);
114 snprintf(tmpbuf
, sizeof(tmpbuf
), "VRID: %u\n", hdr
->vrid
);
115 rs
+= strlcat(buf
, tmpbuf
, buflen
);
116 snprintf(tmpbuf
, sizeof(tmpbuf
), "Priority: %u\n", hdr
->priority
);
117 rs
+= strlcat(buf
, tmpbuf
, buflen
);
118 snprintf(tmpbuf
, sizeof(tmpbuf
), "Count IPvX: %u\n", hdr
->naddr
);
119 rs
+= strlcat(buf
, tmpbuf
, buflen
);
120 snprintf(tmpbuf
, sizeof(tmpbuf
), "Max Adver Int: %u\n",
121 ntohs(hdr
->v3
.adver_int
));
122 rs
+= strlcat(buf
, tmpbuf
, buflen
);
123 snprintf(tmpbuf
, sizeof(tmpbuf
), "Checksum: %x\n", ntohs(hdr
->chksum
));
124 rs
+= strlcat(buf
, tmpbuf
, buflen
);
129 ssize_t
vrrp_parse_datagram(int family
, struct msghdr
*m
, size_t read
,
130 struct vrrp_pkt
**pkt
, char *errmsg
,
133 /* Source (MAC & IP), Dest (MAC & IP) TTL validation done by kernel */
134 size_t addrsz
= (family
== AF_INET
) ? sizeof(struct in_addr
)
135 : sizeof(struct in6_addr
);
138 uint8_t *buf
= m
->msg_iov
->iov_base
;
140 #define VRRP_PKT_VCHECK(cond, _f, ...) \
144 snprintf(errmsg, errmsg_len, (_f), \
150 /* IPvX header check */
152 if (family
== AF_INET
) {
154 read
>= sizeof(struct ip
),
155 "Datagram not large enough to contain IP header");
157 struct ip
*ip
= (struct ip
*)buf
;
159 /* IP total length check */
161 ntohs(ip
->ip_len
) == read
,
162 "IPv4 packet length field does not match # received bytes; %u != %lu",
163 ntohs(ip
->ip_len
), read
);
166 VRRP_PKT_VCHECK(ip
->ip_ttl
== 255,
167 "IPv4 TTL is %" PRIu8
"; should be 255",
170 *pkt
= (struct vrrp_pkt
*)(buf
+ (ip
->ip_hl
<< 2));
171 pktsize
= read
- (ip
->ip_hl
<< 2);
173 /* IP empty packet check */
174 VRRP_PKT_VCHECK(pktsize
> 0, "IPv4 packet has no payload");
175 } else if (family
== AF_INET6
) {
177 for (c
= CMSG_FIRSTHDR(m
); c
!= NULL
; CMSG_NXTHDR(m
, c
)) {
178 if (c
->cmsg_level
== IPPROTO_IPV6
179 && c
->cmsg_type
== IPV6_HOPLIMIT
)
183 VRRP_PKT_VCHECK(!!c
, "IPv6 Hop Limit not received");
185 uint8_t *hoplimit
= CMSG_DATA(c
);
186 VRRP_PKT_VCHECK(*hoplimit
== 255,
187 "IPv6 Hop Limit is %" PRIu8
"; should be 255",
190 *pkt
= (struct vrrp_pkt
*)buf
;
193 assert(!"Unknown address family");
197 size_t minsize
= (family
== AF_INET
) ? VRRP_MIN_PKT_SIZE_V4
198 : VRRP_MIN_PKT_SIZE_V6
;
199 size_t maxsize
= (family
== AF_INET
) ? VRRP_MAX_PKT_SIZE_V4
200 : VRRP_MAX_PKT_SIZE_V6
;
201 VRRP_PKT_VCHECK(pktsize
>= minsize
,
202 "VRRP packet is undersized (%lu < %lu)", pktsize
,
204 VRRP_PKT_VCHECK(pktsize
<= maxsize
,
205 "VRRP packet is oversized (%lu > %lu)", pktsize
,
208 VRRP_PKT_VCHECK(((*pkt
)->hdr
.vertype
>> 4) != 2, "VRPPv2 unsupported");
209 VRRP_PKT_VCHECK(((*pkt
)->hdr
.vertype
>> 4) == 3, "Bad version %u",
210 (*pkt
)->hdr
.vertype
>> 4);
212 VRRP_PKT_VCHECK(((*pkt
)->hdr
.vertype
& 0x0F) == 1, "Bad type %u",
213 (*pkt
)->hdr
.vertype
& 0x0f);
214 /* # addresses check */
215 size_t ves
= VRRP_PKT_SIZE(family
, (*pkt
)->hdr
.naddr
);
216 VRRP_PKT_VCHECK(pktsize
== ves
, "Packet has incorrect # addresses");
217 /* FIXME: checksum check */
220 /* Addresses check */
221 char vbuf
[INET6_ADDRSTRLEN
];
222 uint8_t *p
= (uint8_t *)(*pkt
)->addrs
;
223 for (uint8_t i
= 0; i
< (*pkt
)->hdr
.naddr
; i
++) {
224 VRRP_PKT_VCHECK(inet_ntop(family
, p
, vbuf
, sizeof(vbuf
)),
225 "Bad IP address, #%u", i
);
229 /* Everything checks out */