-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.c
301 lines (231 loc) · 6.06 KB
/
main.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
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <limits.h>
#include <string.h>
#include <net/if.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <signal.h>
#include <netdb.h>
#include <net/if.h>
#include <netinet/in.h>
#include <syslog.h>
#include "common.h"
#include "sockaddrmacro.h"
#include "error.h"
#include "net.h"
#include "fdb.h"
#include "iftap.h"
#include "vxlan.h"
#include "control.h"
struct vxlan vxlan;
void process_vxlan (void);
void debug_print_vhdr (struct vxlan_hdr * vhdr);
void debug_print_ether (struct ether_header * ether);
void
usage (void)
{
printf ("\n"
" Usage\n"
"\n"
" vxland -m [MCASTADDR] -i [INTERFACE]"
"\n"
"\t -m : Multicast Address(v4/v6)\n"
"\t -i : Multicast Interface\n"
"\t -e : Print Error Massage to STDOUT\n"
"\t -d : Daemon Mode\n"
"\t -h : Print Usage (this message)\n"
"\n"
" vxland is forwarding daemon. Please configure using vxlanctl.\n"
"\n"
);
return;
}
void
cleanup (void)
{
int n, vins_num;
struct vxlan_instance ** vins_list;
/* stop control thread */
pthread_cancel (vxlan.control_tid);
/* stop all vxlan instance */
vins_list = (struct vxlan_instance **)
create_list_from_hash (&vxlan.vins_tuple, &vins_num);
for (n = 0; n < vins_num; n++) {
destroy_vxlan_instance (vins_list[n]);
}
destroy_hash (&vxlan.vins_tuple);
/* close sockets */
close (vxlan.udp_sock);
close (vxlan.control_sock);
return;
}
void
sig_cleanup (int signal)
{
cleanup ();
}
int
main (int argc, char * argv[])
{
int ch;
int d_flag = 0, err_flag = 0;
extern int opterr;
extern char * optarg;
struct addrinfo hints, *res;
char mcast_caddr[40] = "";
char vxlan_if_name[IFNAMSIZ] = "";
memset (&vxlan, 0, sizeof (vxlan));
init_hash (&vxlan.vins_tuple, VXLAN_VNISIZE);
while ((ch = getopt (argc, argv, "ehm:di:")) != -1) {
switch (ch) {
case 'e' :
err_flag = 1;
break;
case 'm' :
strncpy (mcast_caddr, optarg, sizeof (mcast_caddr));
break;
case 'i' :
strcpy (vxlan_if_name, optarg);
break;
case 'd' :
d_flag = 1;
break;
case 'h' :
usage ();
return 0;
default :
usage ();
return -1;
}
}
if (d_flag > 0) {
if (daemon (1, err_flag) < 0)
err (EXIT_FAILURE, "failed to run as a daemon");
}
/* Create UDP Mulciast Socket */
vxlan.port = VXLAN_PORT_BASE;
memset (&hints, 0, sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
if (getaddrinfo (mcast_caddr, VXLAN_CPORT, &hints, &res) != 0) {
error_quit ("Invalid Multicast Address \"%s\"", mcast_caddr);
}
if ((vxlan.udp_sock = socket (res->ai_family,
res->ai_socktype,
res->ai_protocol)) < 0)
err (EXIT_FAILURE, "can not create socket");
memcpy (&vxlan.mcast_addr, res->ai_addr, res->ai_addrlen);
freeaddrinfo (res);
switch (((struct sockaddr *)&vxlan.mcast_addr)->sa_family) {
case AF_INET :
bind_ipv4_inaddrany (vxlan.udp_sock, vxlan.port);
set_ipv4_multicast_join_and_iface (vxlan.udp_sock,
((struct sockaddr_in *)
&vxlan.mcast_addr)->sin_addr,
vxlan_if_name);
set_ipv4_multicast_loop (vxlan.udp_sock, 0);
set_ipv4_multicast_ttl (vxlan.udp_sock, VXLAN_MCAST_TTL);
break;
case AF_INET6 :
bind_ipv6_inaddrany (vxlan.udp_sock, vxlan.port);
set_ipv6_multicast_join_and_iface (vxlan.udp_sock,
((struct sockaddr_in6 *)
&vxlan.mcast_addr)->sin6_addr,
vxlan_if_name);
set_ipv6_multicast_loop (vxlan.udp_sock, 0);
set_ipv6_multicast_ttl (vxlan.udp_sock, VXLAN_MCAST_TTL);
break;
default :
error_quit ("unkown protocol family");
}
((struct sockaddr_in *)&vxlan.mcast_addr)->sin_port = htons (vxlan.port);
/* Start Control Thread */
init_vxlan_control ();
/* Enable syslog */
if (err_flag == 0) {
openlog (VXLAN_LOGNAME,
LOG_CONS | LOG_PERROR,
VXLAN_LOGFACILITY);
error_enable_syslog();
syslog (LOG_INFO, "vxlan start");
}
/* set signal handler */
if (atexit (cleanup) < 0)
err (EXIT_FAILURE, "failed to register exit hook");
/*
if (signal (SIGINT, sig_cleanup) == SIG_ERR)
err (EXIT_FAILURE, "failed to register SIGINT hook");
*/
process_vxlan ();
return 0;
}
void
process_vxlan (void)
{
int len;
char buf[VXLAN_PACKET_BUF_LEN];
fd_set fds;
struct vxlan_hdr * vhdr;
struct ether_header * ether;
struct sockaddr_storage sa_str;
struct vxlan_instance * vins;
socklen_t s_t = sizeof (sa_str);
memset (buf, 0, sizeof (buf));
/* From Internet */
while (1) {
FD_ZERO (&fds);
FD_SET (vxlan.udp_sock, &fds);
pselect (vxlan.udp_sock + 1, &fds, NULL, NULL, NULL, NULL);
if (!FD_ISSET (vxlan.udp_sock, &fds))
break;
memset (&sa_str, 0, sizeof (sa_str));
if ((len = recvfrom (vxlan.udp_sock, buf, sizeof (buf), 0,
(struct sockaddr *)&sa_str, &s_t)) < 0)
continue;
vhdr = (struct vxlan_hdr *) buf;
if ((vins = search_hash (&vxlan.vins_tuple, vhdr->vxlan_vni)) == NULL) {
error_warn ("invalid VNI %02x%02x%02x",
vhdr->vxlan_vni[0],
vhdr->vxlan_vni[1],
vhdr->vxlan_vni[2]);
continue;
}
ether = (struct ether_header *) (buf + sizeof (struct vxlan_hdr));
process_fdb_etherflame_from_vxlan (vins, ether, &sa_str);
send_etherflame_from_vxlan_to_local (vins, ether,
len - sizeof (struct vxlan_hdr));
}
return;
}
void
debug_print_vhdr (struct vxlan_hdr * vhdr)
{
printf ("vxlan header\n");
printf ("Flag : %u\n", vhdr->vxlan_flags);
printf ("VNI : %u%u%u\n",
vhdr->vxlan_vni[0],
vhdr->vxlan_vni[1],
vhdr->vxlan_vni[2]);
printf ("\n");
return;
}
void
debug_print_ether (struct ether_header * ether)
{
printf ("Mac\n");
printf ("DST : %02x:%02x:%02x:%02x:%02x:%02x\n",
ether->ether_dhost[0], ether->ether_dhost[1],
ether->ether_dhost[2], ether->ether_dhost[3], ether->ether_dhost[4],
ether->ether_dhost[5]);
printf ("SRC : %02x:%02x:%02x:%02x:%02x:%02x\n",
ether->ether_shost[0], ether->ether_shost[1],
ether->ether_shost[2], ether->ether_shost[3], ether->ether_shost[4],
ether->ether_shost[5]);
return;
}