-
Notifications
You must be signed in to change notification settings - Fork 9
/
vrrp_ip6.c
346 lines (292 loc) · 8.33 KB
/
vrrp_ip6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* vrrp_ip6.c - IPv6 helpers functions
*
* Copyright (C) 2014 Arnaud Andre
*
* This file is part of uvrrpd.
*
* uvrrpd is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* uvrrpd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with uvrrpd. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_IP6
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <arpa/inet.h>
/* ifreq + ioctl */
#include <sys/ioctl.h>
#include <net/if.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netdb.h> /* NI_MAXHOST */
#include "vrrp_ipx.h"
#include "vrrp_net.h"
#include "log.h"
#include "common.h"
#define VRRP_MGROUP6 "ff02::12"
#define IP6HDR_SIZE sizeof(struct ip6_hdr)
/**
* pshdr_ip6 - pseudo header IPv6
*/
struct pshdr_ip6 {
struct in6_addr saddr;
struct in6_addr daddr;
uint32_t len;
uint8_t zeros[3];
uint8_t next_header;
};
/**
* vrrp_ip6_search_vip() - search one vip in vip list
* if vip is found
* set found = 1
* _vip_ptr point to vip in vnet->vip_list
*/
#define vrrp_ip6_search_vip(vnet, _vip_ptr, _addr, found) \
do { \
list_for_each_entry_reverse(_vip_ptr, &vnet->vip_list, iplist) { \
if (memcmp(&(_vip_ptr->ip_addr6), _addr, sizeof(struct in6_addr)) == 0) { \
found = 1; \
break; \
}\
} \
} while(0)
/**
* vrrp_ip6_setsockopt() - Set socket option
* used to find ancillary data in recvmsg()
* see vrrp_ip6_recv()
*/
static int vrrp_ip6_setsockopt(int socket, int vrid)
{
int on = 1;
/* IPV6_RECVPKTINFO */
if (setsockopt
(socket, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on)) < 0) {
log_error("vrid %d :: setsockopt - %m", vrid);
return -1;
}
/* IPV6_RECVHOPLIMIT */
if (setsockopt
(socket, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on, sizeof(on)) < 0) {
log_error("vrid %d :: setsockopt - %m", vrid);
return -1;
}
return 0;
}
/**
* vrrp_net_join_mgroup6() - join IPv6 VRRP multicast group
*/
static int vrrp_ip6_mgroup(struct vrrp_net *vnet)
{
/* Join VRRP multicast group */
struct ipv6_mreq group = { IN6ADDR_ANY_INIT, 0 };
struct in6_addr group_addr = IN6ADDR_ANY_INIT;
if (inet_pton(AF_INET6, VRRP_MGROUP6, &group_addr) < 0) {
log_error("vrid %d :: inet_pton - %m", vnet->vrid);
return -1;
}
memcpy(&group.ipv6mr_multiaddr, &group_addr, sizeof(struct in6_addr));
group.ipv6mr_interface = if_nametoindex(vnet->vif.ifname);
if (setsockopt
(vnet->socket, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &group,
sizeof(struct ipv6_mreq)) < 0) {
log_error("vrid %d :: setsockopt - %m", vnet->vrid);
return -1;
}
return 0;
}
/**
* vrrp_ip6_cmp() - Compare VIP list between received vrrpkt and our instance
* Return 0 if the list is the same,
* the number of differente VIP else
*/
static int vrrp_ip6_viplist_cmp(struct vrrp_net *vnet, struct vrrphdr *vrrpkt)
{
uint32_t *vip_addr =
(uint32_t *) ((unsigned char *) vrrpkt + VRRP_PKTHDR_SIZE);
uint32_t pos = 0;
int naddr = 0;
int ndiff = 0;
while (naddr < vnet->naddr) {
/* vip in vrrpkt */
struct in6_addr *vip = (struct in6_addr *) (vip_addr + pos);
/* search in vrrp_ip list */
struct vrrp_ip *vip_ptr = NULL;
int found = 0;
vrrp_ip6_search_vip(vnet, vip_ptr, vip->s6_addr, found);
if (!found) {
char host[NI_MAXHOST];
log_warning
("vrid %d :: Invalid pkt - Virtual IPv6 address unexpected %s",
vnet->vrid, inet_ntop(vnet->family, vip, host,
sizeof(host)));
++ndiff;
}
pos += 4;
++naddr;
}
return ndiff;
}
/**
* find_ancillary() - search & find IPv6 ancillary data after
* receiving data in a struct msghdr msg (recvmsg())
*/
static inline void *find_ancillary(struct msghdr *msg, int cmsg_type)
{
struct cmsghdr *cmsg = NULL;
for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
cmsg = CMSG_NXTHDR(msg, cmsg)) {
if ((cmsg->cmsg_level == IPPROTO_IPV6)
&& (cmsg->cmsg_type == cmsg_type)) {
return (CMSG_DATA(cmsg));
}
}
return NULL;
}
/**
* vrrp_ip6_recv() - Fill vrrp_ipx_header from received pkt
*/
static int vrrp_ip6_recv(int sock_fd, struct vrrp_recv *recv,
unsigned char *buf, ssize_t buf_size, int *payload_pos)
{
ssize_t len;
/* IPv6 raw sockets return no IP header. We must query
* src/dest via socket options/ancillary data */
struct msghdr msg;
struct sockaddr_in6 src;
struct iovec iov;
uint8_t ancillary[64];
msg.msg_name = &src;
msg.msg_namelen = sizeof(src);
iov.iov_base = buf;
iov.iov_len = buf_size;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = ancillary;
msg.msg_controllen = sizeof(ancillary);
msg.msg_flags = 0;
len = recvmsg(sock_fd, &msg, 0);
if (len < 0) {
log_error("recvmsg - %m");
return -1;
}
/* src address */
memcpy(&recv->ip_saddr6, &src.sin6_addr, sizeof(struct in6_addr));
/* hoplimit */
uint8_t *opt = find_ancillary(&msg, IPV6_HOPLIMIT);
if (opt == NULL) {
log_error("recvmsg - unknown hop limit");
return -1;
}
recv->header.ttl = *(int *) opt;
/* dst address */
opt = find_ancillary(&msg, IPV6_PKTINFO);
if (opt == NULL) {
log_error("recvmsg - unknown dst address");
return -1;
}
struct in6_pktinfo *pktinfo = (struct in6_pktinfo *) opt;
memcpy(&recv->ip_daddr6, &pktinfo->ipi6_addr, sizeof(struct in6_addr));
/* other options */
recv->header.len = sizeof(struct ip6_hdr);
recv->header.totlen = recv->header.len + len;
/* kludge, we force it since we have no way to read it in recvmsg().
* But we have set IPPROTO_VRRP in vrrp_net_socket() */
recv->header.proto = IPPROTO_VRRP;
/* buf is directly filled with VRRP adv
* no need to skip IPv6 header */
*payload_pos = 0;
return len;
}
/**
* vrrp_ip6_getsize() - return the current size of vrrp instance
*/
static int vrrp_ip6_getsize(const struct vrrp_net *vnet)
{
return sizeof(struct vrrphdr) + vnet->naddr * sizeof(struct in6_addr);
}
/**
* vrrp_ip6_chksum() - compute VRRP adv chksum
*/
uint16_t vrrp_ip6_chksum(const struct vrrp_net *vnet, struct vrrphdr *pkt,
union vrrp_ipx_addr *ipx_saddr,
union vrrp_ipx_addr *ipx_daddr)
{
/* reset chksum */
pkt->chksum = 0;
const struct iovec *iov_iph = &vnet->__adv[1];
const struct ip6_hdr *iph = iov_iph->iov_base;
/* pseudo_header ipv6 */
struct pshdr_ip6 psh = { IN6ADDR_ANY_INIT,
IN6ADDR_ANY_INIT,
0,
{0, 0, 0},
0
};
if ((ipx_saddr != NULL) && (ipx_daddr != NULL)) {
memcpy(&psh.saddr, &ipx_saddr->addr6, sizeof(struct in6_addr));
memcpy(&psh.daddr, &ipx_daddr->addr6, sizeof(struct in6_addr));
}
else {
memcpy(&psh.saddr, &iph->ip6_src, sizeof(struct in6_addr));
memcpy(&psh.daddr, &iph->ip6_dst, sizeof(struct in6_addr));
}
bzero(&psh.zeros, sizeof(psh.zeros));
psh.next_header = IPPROTO_VRRP;
psh.len = htons(vrrp_ip6_getsize(vnet));
uint32_t psh_size = sizeof(struct pshdr_ip6) + vrrp_ip6_getsize(vnet);
unsigned short buf[psh_size / sizeof(short)];
memcpy(buf, &psh, sizeof(struct pshdr_ip6));
memcpy(buf + sizeof(struct pshdr_ip6) / sizeof(short), pkt,
vrrp_ip6_getsize(vnet));
return cksum(buf, psh_size);
}
/**
* vrrp_ip6_ntop() - network to string representation
*/
static const char *vrrp_ip6_ntop(union vrrp_ipx_addr *ipx, char *dst)
{
return inet_ntop(AF_INET6, &ipx->addr6, dst, INET6_ADDRSTRLEN);
}
/**
* vrrp_ip6_pton() - string representation to network
*/
static int vrrp_ip6_pton(union vrrp_ipx_addr *dst, const char *src)
{
return inet_pton(AF_INET6, src, &dst->addr6);
}
/**
* vrrp_ip6_cmp() - compare two vipx
*/
int vrrp_ip6_cmp(union vrrp_ipx_addr *s1, union vrrp_ipx_addr *s2)
{
return memcmp(&s1->addr6, &s2->addr6, sizeof(struct in6_addr));
}
/* exported VRRP_IP6 helper */
struct vrrp_ipx VRRP_IP6 = {
.family = AF_INET6,
.setsockopt = vrrp_ip6_setsockopt,
.mgroup = vrrp_ip6_mgroup,
.recv = vrrp_ip6_recv,
.cmp = vrrp_ip6_cmp,
.chksum = vrrp_ip6_chksum,
.getsize = vrrp_ip6_getsize,
.viplist_cmp = vrrp_ip6_viplist_cmp,
.ipx_pton = vrrp_ip6_pton,
.ipx_ntop = vrrp_ip6_ntop
};
#endif /* HAVE_IP6 */