-
Notifications
You must be signed in to change notification settings - Fork 9
/
vrrp_arp.c
196 lines (159 loc) · 4.5 KB
/
vrrp_arp.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
/*
* vrrp_arp.c
*
* 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/>.
*/
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <linux/if_ether.h> // ETH_P_IP = 0x0800, ETH_P_IPV6 = 0x86DD
#include <net/ethernet.h>
#include <netinet/ip.h>
#include <net/if_arp.h>
#include "log.h"
#include "vrrp.h"
#include "vrrp_net.h"
#define ETHDR_SIZE sizeof(struct ether_header)
/**
* ether_header vrrp_arp_eth
*/
static struct ether_header vrrp_arp_eth = {
.ether_dhost = {0xff, 0xff, 0xff,
0xff, 0xff, 0xff},
.ether_shost = {0x00,
0x00,
0x5e,
0x00,
0x01,
0x00}, /* vrrp->vrid */
};
#define IP_ALEN 4
/**
* arphdr_eth - ARP header
*/
struct arphdr_eth {
unsigned char ar_sha[ETH_ALEN]; /* Sender hardware address */
unsigned char ar_sip[IP_ALEN]; /* Sender IP address */
unsigned char ar_tha[ETH_ALEN]; /* Target hardware address */
unsigned char ar_tip[IP_ALEN]; /* Target IP address */
};
/**
* vrrp_arp_eth_build()
*/
static int vrrp_arp_eth_build(struct iovec *iov, const uint8_t vrid)
{
iov->iov_base = malloc(sizeof(struct ether_header));
struct ether_header *hdr = iov->iov_base;
if (hdr == NULL) {
log_error("vrid %d :: malloc - %m", vrid);
return -1;
}
memcpy(hdr, &vrrp_arp_eth, sizeof(struct ether_header));
hdr->ether_shost[5] = vrid;
hdr->ether_type = htons(ETHERTYPE_ARP);
iov->iov_len = ETHDR_SIZE;
return 0;
}
/**
* vrrp_arp_send() - Send arp gratuitous for each vip
*/
int vrrp_arp_send(struct vrrp_net *vnet)
{
struct vrrp_ip *vip_ptr = NULL;
/* we have to send one arp pkt by vip */
list_for_each_entry_reverse(vip_ptr, &vnet->vip_list, iplist) {
vrrp_net_send(vnet, vip_ptr->__topology,
ARRAY_SIZE(vip_ptr->__topology));
}
return 0;
}
/**
* vrrp_arp_build() - Build arp header
*/
static int vrrp_arp_build(struct iovec *iov, const uint8_t vrid)
{
iov->iov_base = malloc(sizeof(struct arphdr));
struct arphdr *arph = iov->iov_base;
if (arph == NULL) {
log_error("vrid %d :: malloc - %m", vrid);
return -1;
}
arph->ar_hrd = htons(ARPHRD_ETHER); /* Format of hardware address */
arph->ar_pro = htons(ETHERTYPE_IP); /* Format of protocol address */
arph->ar_hln = ETH_ALEN; /* Length of hardware address */
arph->ar_pln = IP_ALEN; /* Length of protocol address */
arph->ar_op = htons(ARPOP_REQUEST); /* ARP opcode (command) */
iov->iov_len = sizeof(struct arphdr);
return 0;
}
/**
* vrrp_arp_vrrp_build() - VRRP arp payload
*/
static int vrrp_arp_vrrp_build(struct iovec *iov, struct vrrp_ip *vip,
struct vrrp_net *vnet)
{
iov->iov_base = malloc(sizeof(struct arphdr_eth));
struct arphdr_eth *arpeth = iov->iov_base;
if (arpeth == NULL) {
log_error("vrid %d :: malloc - %m", vnet->vrid);
return -1;
}
arpeth->ar_sha[0] = 0x00;
arpeth->ar_sha[1] = 0x00;
arpeth->ar_sha[2] = 0x5e;
arpeth->ar_sha[3] = 0x00;
arpeth->ar_sha[4] = 0x01;
arpeth->ar_sha[5] = vnet->vrid;
memcpy(arpeth->ar_sip, &vip->ip_addr.s_addr, IP_ALEN);
memset(arpeth->ar_tha, 0xFF, ETH_ALEN);
memcpy(arpeth->ar_tip, &arpeth->ar_sip, IP_ALEN);
iov->iov_len = sizeof(struct arphdr_eth);
return 0;
}
/**
* vrrp_arp_init()
*/
int vrrp_arp_init(struct vrrp_net *vnet)
{
int status = -1;
/* we have to build one arp pkt by vip */
struct vrrp_ip *vip_ptr = NULL;
list_for_each_entry_reverse(vip_ptr, &vnet->vip_list, iplist) {
status =
vrrp_arp_eth_build(&vip_ptr->__topology[0], vnet->vrid);
status |= vrrp_arp_build(&vip_ptr->__topology[1], vnet->vrid);
status |=
vrrp_arp_vrrp_build(&vip_ptr->__topology[2], vip_ptr, vnet);
}
return status;
}
/**
* vrrp_arp_cleanup()
*/
void vrrp_arp_cleanup(struct vrrp_net *vnet)
{
/* clean arp buffer for each vrrp_ip addr */
struct vrrp_ip *vip_ptr = NULL;
list_for_each_entry(vip_ptr, &vnet->vip_list, iplist) {
/* clean iovec */
for (int i = 0; i < 3; ++i) {
struct iovec *iov = &vip_ptr->__topology[i];
free(iov->iov_base);
}
}
}