-
Notifications
You must be signed in to change notification settings - Fork 14
/
net.c
356 lines (283 loc) · 7.52 KB
/
net.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
347
348
349
350
351
352
353
354
355
356
#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <linux/if_ether.h>
#include <ifaddrs.h>
#include <syslog.h>
#include "net.h"
#include "fdb.h"
#include "error.h"
#include "sockaddrmacro.h"
/* 802.1q Ether Header */
struct ether_vlan_header {
u_int8_t ether_dhost[ETH_ALEN];
u_int8_t ether_shost[ETH_ALEN];
struct vlan {
u_int16_t vlan_tpid;
u_int16_t vlan_tci;
} ether_vlan;
u_int16_t ether_type;
};
void
send_etherflame_from_vxlan_to_local (struct vxlan_instance * vins,
struct ether_header * ether, int len)
{
if (write (vins->tap_sock, ether, len) < 0) {
error_warn("Write etherflame to local network failed");
}
return;
}
void
send_etherflame_from_local_to_vxlan (struct vxlan_instance * vins,
struct ether_header * ether, int len)
{
struct vxlan_hdr vhdr;
struct fdb_entry * entry;
struct msghdr mhdr;
struct iovec iov[2];
/* Forwarding */
memset (&vhdr, 0, sizeof (vhdr));
vhdr.vxlan_flags = VXLAN_VALIDFLAG;
memcpy (vhdr.vxlan_vni, &(vins->vni), VXLAN_VNISIZE);
iov[0].iov_base = &vhdr;
iov[0].iov_len = sizeof (vhdr);
iov[1].iov_base = ether;
iov[1].iov_len = len;
mhdr.msg_iov = iov;
mhdr.msg_iovlen = 2;
mhdr.msg_controllen = 0;
if ((entry = fdb_search_entry (vins->fdb, ether->ether_dhost)) == NULL) {
mhdr.msg_name = &vxlan.mcast_addr;
mhdr.msg_namelen = sizeof (vxlan.mcast_addr);
if (sendmsg (vxlan.udp_sock, &mhdr, 0) < 0)
error_warn ("sendmsg to multicast failed %s",
strerror (errno));
} else {
EXTRACT_PORT (entry->vtep_addr) = htons (VXLAN_PORT_BASE);
mhdr.msg_name = &entry->vtep_addr;
mhdr.msg_namelen = sizeof (entry->vtep_addr);
if (sendmsg (vxlan.udp_sock, &mhdr, 0) < 0)
error_warn ("sendmsg to unicast failed : %s",
strerror (errno));
}
return;
}
struct in_addr
getifaddr (char * dev)
{
int fd;
struct ifreq ifr;
struct sockaddr_in * addr;
fd = socket (AF_INET, SOCK_DGRAM, 0);
memset (&ifr, 0, sizeof (ifr));
strncpy (ifr.ifr_name, dev, IFNAMSIZ - 1);
if (ioctl (fd, SIOCGIFADDR, &ifr) < 0)
err (EXIT_FAILURE, "can not get interface \"%s\" info", dev);
close (fd);
addr = (struct sockaddr_in *) &(ifr.ifr_addr);
return addr->sin_addr;
}
struct in6_addr
getifaddr6 (char * dev)
{
int fd;
struct ifreq ifr;
struct sockaddr_in6 * addr6;
fd = socket (AF_INET, SOCK_DGRAM, 0);
memset (&ifr, 0, sizeof (ifr));
strncpy (ifr.ifr_name, dev, IFNAMSIZ - 1);
ifr.ifr_addr.sa_family = AF_INET6;
if (ioctl (fd, SIOCGIFADDR, &ifr) < 0)
err (EXIT_FAILURE, "can not get interface \"%s\" info", dev);
close (fd);
addr6 = (struct sockaddr_in6 *) &(ifr.ifr_addr);
return addr6->sin6_addr;
}
void
set_ipv4_multicast_join_and_iface (int socket, struct in_addr maddr, char * ifname)
{
char cmaddr[16];
struct ip_mreq mreq;
memset (&mreq, 0, sizeof (mreq));
mreq.imr_multiaddr = maddr;
mreq.imr_interface = getifaddr (ifname);
if (setsockopt (socket,
IPPROTO_IP,
IP_ADD_MEMBERSHIP,
(char *)&mreq, sizeof (mreq)) < 0) {
inet_ntop (AF_INET, &maddr, cmaddr, sizeof (cmaddr));
err (EXIT_FAILURE, "can not join multicast %s", cmaddr);
}
if (setsockopt (socket,
IPPROTO_IP,
IP_MULTICAST_IF,
(char *)&mreq.imr_interface,
sizeof (mreq.imr_interface)) < 0)
err (EXIT_FAILURE, "can not set multicast interface");
return;
}
void
set_ipv6_multicast_join_and_iface (int socket, struct in6_addr maddr, char * ifname)
{
struct ipv6_mreq mreq6;
char cmaddr[48];
memset (&mreq6, 0, sizeof (mreq6));
mreq6.ipv6mr_multiaddr = maddr;
mreq6.ipv6mr_interface = if_nametoindex (ifname);
if (mreq6.ipv6mr_interface == 0)
err (EXIT_FAILURE, "invalid interface \"%s\"", ifname);
if (setsockopt (socket,
IPPROTO_IPV6,
IPV6_ADD_MEMBERSHIP,
(char *)&mreq6, sizeof (mreq6)) < 0) {
inet_ntop (AF_INET6, &maddr, cmaddr, sizeof (cmaddr));
err (EXIT_FAILURE, "can not join multicast %s", cmaddr);
}
if (setsockopt (socket,
IPPROTO_IPV6,
IPV6_MULTICAST_IF,
(char *)&mreq6.ipv6mr_interface,
sizeof (mreq6.ipv6mr_interface)) < 0) {
err (EXIT_FAILURE,
"can not set multicast interface \"%s\"",
if_indextoname (mreq6.ipv6mr_interface, ifname));
}
return;
}
void
set_ipv4_multicast_loop (int socket, int stat)
{
if (setsockopt (socket,
IPPROTO_IP,
IP_MULTICAST_LOOP,
(char *)&stat, sizeof (stat)) < 0)
err (EXIT_FAILURE, "can not set off multicast loop");
return;
}
void
set_ipv6_multicast_loop (int socket, int stat)
{
if (setsockopt (socket,
IPPROTO_IPV6,
IPV6_MULTICAST_LOOP,
(char *)&stat, sizeof (stat)) < 0)
err (EXIT_FAILURE, "can not set off multicast loop");
return;
}
void
set_ipv4_multicast_ttl (int socket, int ttl)
{
if (setsockopt (socket,
IPPROTO_IP,
IP_MULTICAST_TTL,
(char *)&ttl, sizeof (ttl)) < 0)
err (EXIT_FAILURE, "can not set mutlicsat ttl");
return;
}
void
set_ipv6_multicast_ttl (int socket, int ttl)
{
if (setsockopt (socket,
IPPROTO_IPV6,
IPV6_MULTICAST_HOPS,
(char *)&ttl, sizeof (ttl)) < 0)
err (EXIT_FAILURE, "can not set ttl");
return;
}
void
bind_ipv4_inaddrany (int socket, int port)
{
struct sockaddr_in saddr_in;
saddr_in.sin_family = AF_INET;
saddr_in.sin_port = htons (port);
saddr_in.sin_addr.s_addr = INADDR_ANY;
if (bind (socket, (struct sockaddr *)&saddr_in, sizeof (saddr_in)) < 0)
err (EXIT_FAILURE, "can not bind");
return;
}
void
bind_ipv6_inaddrany (int socket, int port)
{
struct sockaddr_in6 saddr_in6;
saddr_in6.sin6_family = AF_INET6;
saddr_in6.sin6_port = htons (port);
saddr_in6.sin6_addr = in6addr_any;
if (bind (socket, (struct sockaddr *)&saddr_in6, sizeof (saddr_in6)) < 0)
err (EXIT_FAILURE, "can not bind");
return;
}
void
bind_ipv6_addr (int socket, struct in6_addr addr6, int port)
{
struct sockaddr_in6 saddr6;
memset (&saddr6, 0, sizeof (saddr6));
saddr6.sin6_family = AF_INET6;
saddr6.sin6_port = htons (port);
saddr6.sin6_addr = addr6;
if (bind (socket, (struct sockaddr *)&saddr6, sizeof (saddr6)) != 0) {
err (EXIT_FAILURE, "can not bind IPv6 addr");
}
return;
}
void
set_ipv6_pktinfo (int socket, int stat)
{
if (setsockopt (socket,
IPPROTO_IPV6,
IPV6_PKTINFO,
&stat,
sizeof (stat)) < 0)
err (EXIT_FAILURE, "can not set IPV6_PKTINFO");
return;
}
int
ifaddr (int ai_family , char * dev, void * dest)
{
struct ifaddrs * ifa;
struct ifaddrs * ifa_list;
struct sockaddr_in * saddr_in;
struct sockaddr_in6 * saddr_in6;
if (getifaddrs (&ifa_list) != 0) {
err (EXIT_FAILURE, "getifaddrs");
}
for (ifa = ifa_list; ifa != NULL; ifa = ifa->ifa_next) {
if (strncmp (dev, ifa->ifa_name, IFNAMSIZ) != 0)
continue;
if (ai_family != ifa->ifa_addr->sa_family)
continue;
switch (ifa->ifa_addr->sa_family) {
case AF_INET :
if (ifa->ifa_addr->sa_family != AF_INET)
continue;
saddr_in = (struct sockaddr_in *)(ifa->ifa_addr);
memcpy (dest,
&saddr_in->sin_addr,
sizeof (struct in_addr));
freeifaddrs (ifa_list);
return 1;
case AF_INET6 :
if (ifa->ifa_addr->sa_family != AF_INET6)
continue;
if (IN6_IS_ADDR_LINKLOCAL (&(((struct sockaddr_in6 *)
ifa->ifa_addr)->sin6_addr)))
continue;
saddr_in6 = (struct sockaddr_in6 *)(ifa->ifa_addr);
memcpy (dest,
&saddr_in6->sin6_addr,
sizeof (struct in6_addr));
freeifaddrs (ifa_list);
return 1;
}
}
freeifaddrs (ifa_list);
return -1;
}