-
Notifications
You must be signed in to change notification settings - Fork 2
/
6brd.c
348 lines (306 loc) · 8.96 KB
/
6brd.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
/**
* Copyright (C) 2012-2013 Steven Barth <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License v2 as published by
* the Free Software Foundation.
*
* This program 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.
*
*/
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <syslog.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/ip6.h>
#include <netpacket/packet.h>
#include <linux/netlink.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include "nloop.h"
#include "6brd.h"
struct config config = { .log_level = LOG_ERR };
struct interface *interfaces;
static int ioctl_sock;
static int scan_args (int argc, char **argv) {
int i, j, l;
for (i = 1; i < argc; ++i) {
l = strlen (argv[i]);
if (!l) goto argerr;
else if (argv[i][0] == '-') {
if (l == 1) goto argerr;
for (j = 1; j < l; ++j) switch (argv[i][j]) {
case 'f':
config.foreground = true;
break;
case 'v':
switch (config.log_level) {
case LOG_ERR: config.log_level = LOG_NOTICE; break;
case LOG_NOTICE: config.log_level = LOG_DEBUG; break;
}
break;
default:
goto argerr;
}
} else break;
}
if (argc - i < 2) goto argerr;
if ((interfaces = calloc (argc - i, sizeof (struct interface))) == NULL) {
fprintf (stderr, "Failed to allocate memory for interfaces\n");
return 2;
}
for (; i < argc; ++i, ++config.cnt) {
struct interface *iface = interfaces + config.cnt;
iface->learn_routes = 1;
l = strlen (argv[i]);
for (j = 0; j < l; ++j) {
if (argv[i][j] == '~') iface->external = 1;
else if (argv[i][j] == '!') iface->learn_routes = 0;
else break;
}
if (j >= l) {
free (interfaces);
goto argerr;
}
iface->ifname = argv[i] + j;
for (j = 0; j < config.cnt; ++j) if (!strcmp (
interfaces[j].ifname, iface->ifname
)) {
fprintf (stderr, "Duplicate interface %s\n", iface->ifname);
free (interfaces);
return 2;
}
if ((iface->ifindex = if_nametoindex (iface->ifname)) <= 0) {
fprintf (
stderr, "Failed to get interface index for %s\n",
iface->ifname
);
free (interfaces);
return 2;
}
}
return 0;
argerr:
fprintf (
stderr,
"%s [-f] [-v ...] [~][!]iface1 [~][!]iface2 [[~][!]iface3 ...]\n"
" -f: foreground and send log messages to stderr\n"
" -v: increase verbosity by 1, at most 2 increments\n"
" ~: only proxy DAD messages for the specified interface\n"
" !: do not learn routes for neighbours on the interface\n",
argv[0]
);
return 1;
}
int main(int argc, char **argv)
{
int i;
if ((i = scan_args (argc, argv)) != 0) return i;
if (getuid () != 0) {
fprintf (stderr, "Must be run as root!\n");
return 2;
}
if (!config.foreground) {
openlog ("odhcpd", LOG_PID, LOG_DAEMON);
if (daemon (0, 0)) do_log
(LOG_ERR, "Failed to daemonize: %s", strerror (errno));
}
uloop_init();
ioctl_sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (ioctl_sock < 0)
return 4;
if (netlink_init())
return 4;
if (ndp_init())
return 4;
for (i = 0; i < config.cnt; ++i)
ndp_setup_interface (interfaces + i, 1);
uloop_run ();
for (i = 0; i < config.cnt; ++i)
ndp_setup_interface (interfaces + i, 0);
return 0;
}
void do_log (int priority, const char *format, ...) {
if (priority > config.log_level) return;
va_list args;
va_start (args, format);
if (config.foreground) {
vfprintf (stderr, format, args);
fprintf (stderr, "\n");
} else vsyslog (priority, format, args);
va_end (args);
return;
}
/* Read IPv6 MAC for interface */
int odhcpd_get_mac(const struct interface *iface, uint8_t mac[6])
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name) - 1);
if (ioctl(ioctl_sock, SIOCGIFHWADDR, &ifr) < 0)
return -1;
memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
return 0;
}
/* Forwards a packet on a specific interface */
ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
struct iovec *iov, size_t iov_len,
const struct interface *iface)
{
/* Construct headers */
uint8_t cmsg_buf[CMSG_SPACE(sizeof(struct in6_pktinfo))] = {0};
struct msghdr msg = {
.msg_name = (void *) dest,
.msg_namelen = sizeof(*dest),
.msg_iov = iov,
.msg_iovlen = iov_len,
.msg_control = cmsg_buf,
.msg_controllen = sizeof(cmsg_buf),
.msg_flags = 0
};
/* Set control data (define destination interface) */
struct cmsghdr *chdr = CMSG_FIRSTHDR(&msg);
chdr->cmsg_level = IPPROTO_IPV6;
chdr->cmsg_type = IPV6_PKTINFO;
chdr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
struct in6_pktinfo *pktinfo = (struct in6_pktinfo*)CMSG_DATA(chdr);
pktinfo->ipi6_ifindex = iface->ifindex;
/* Also set scope ID if link-local */
if (IN6_IS_ADDR_LINKLOCAL(&dest->sin6_addr)
|| IN6_IS_ADDR_MC_LINKLOCAL(&dest->sin6_addr))
dest->sin6_scope_id = iface->ifindex;
char ipbuf[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &dest->sin6_addr, ipbuf, sizeof(ipbuf));
ssize_t sent = sendmsg(socket, &msg, MSG_DONTWAIT);
if (sent < 0)
do_log (LOG_NOTICE, "Failed to send to %s%%%s: %s",
ipbuf, iface->ifname, strerror (errno));
else
do_log (LOG_DEBUG, "Sent %li bytes to %s%%%s",
(long)sent, ipbuf, iface->ifname);
return sent;
}
struct interface* odhcpd_get_interface_by_index(int ifindex)
{
for (int i = 0; i < config.cnt; ++i) {
if (interfaces[i].ifindex == ifindex) return interfaces + i;
}
return NULL;
}
struct interface* odhcpd_get_interface_by_name(const char *name)
{
for (int i = 0; i < config.cnt; ++i) {
if (!strcmp (interfaces[i].ifname, name)) return interfaces + i;
}
return NULL;
}
/* Convenience function to receive and do basic validation of packets */
static void odhcpd_receive_packets(struct uloop_fd *u, unsigned int events)
{
struct odhcpd_event *e = container_of(u, struct odhcpd_event, uloop);
uint8_t data_buf[8192], cmsg_buf[128];
union {
struct sockaddr_in6 in6;
struct sockaddr_in in;
struct sockaddr_ll ll;
struct sockaddr_nl nl;
} addr;
if (u->error) {
int ret = -1;
socklen_t ret_len = sizeof(ret);
u->error = 0;
if (e->handle_error && getsockopt(u->fd, SOL_SOCKET, SO_ERROR, &ret, &ret_len) == 0)
e->handle_error(e, ret);
}
if (e->recv_msgs) {
e->recv_msgs(e);
return;
}
while (1) {
struct iovec iov = {data_buf, sizeof(data_buf)};
struct msghdr msg = {
.msg_name = (void *) &addr,
.msg_namelen = sizeof(addr),
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = cmsg_buf,
.msg_controllen = sizeof(cmsg_buf),
.msg_flags = 0
};
ssize_t len = recvmsg(u->fd, &msg, MSG_DONTWAIT);
if (len < 0) {
if (errno == EAGAIN)
break;
else
continue;
}
/* Extract destination interface */
int destiface = 0;
int *hlim = NULL;
struct in6_pktinfo *pktinfo;
struct in_pktinfo *pkt4info;
for (struct cmsghdr *ch = CMSG_FIRSTHDR(&msg); ch != NULL; ch = CMSG_NXTHDR(&msg, ch)) {
if (ch->cmsg_level == IPPROTO_IPV6 &&
ch->cmsg_type == IPV6_PKTINFO) {
pktinfo = (struct in6_pktinfo*)CMSG_DATA(ch);
destiface = pktinfo->ipi6_ifindex;
} else if (ch->cmsg_level == IPPROTO_IP &&
ch->cmsg_type == IP_PKTINFO) {
pkt4info = (struct in_pktinfo*)CMSG_DATA(ch);
destiface = pkt4info->ipi_ifindex;
} else if (ch->cmsg_level == IPPROTO_IPV6 &&
ch->cmsg_type == IPV6_HOPLIMIT) {
hlim = (int*)CMSG_DATA(ch);
}
}
/* Check hoplimit if received */
if (hlim && *hlim != 255)
continue;
/* Detect interface for packet sockets */
if (addr.ll.sll_family == AF_PACKET)
destiface = addr.ll.sll_ifindex;
char ipbuf[INET6_ADDRSTRLEN] = "kernel";
if (addr.ll.sll_family == AF_PACKET &&
len >= (ssize_t)sizeof(struct ip6_hdr))
inet_ntop(AF_INET6, &data_buf[8], ipbuf, sizeof(ipbuf));
else if (addr.in6.sin6_family == AF_INET6)
inet_ntop(AF_INET6, &addr.in6.sin6_addr, ipbuf, sizeof(ipbuf));
else if (addr.in.sin_family == AF_INET)
inet_ntop(AF_INET, &addr.in.sin_addr, ipbuf, sizeof(ipbuf));
/* From netlink */
if (addr.nl.nl_family == AF_NETLINK) {
do_log (LOG_DEBUG, "Received %li Bytes from %s%%%s", (long)len,
ipbuf, "netlink");
e->handle_dgram(&addr, data_buf, len, NULL);
return;
} else if (destiface != 0) {
for (int i = 0; i < config.cnt; ++i) {
struct interface *iface = interfaces + i;
if (iface->ifindex != destiface) continue;
do_log (LOG_DEBUG, "Received %li Bytes from %s%%%s", (long)len,
ipbuf, iface->ifname);
e->handle_dgram(&addr, data_buf, len, iface);
}
}
}
}
/* Register events for the multiplexer */
int odhcpd_register(struct odhcpd_event *event)
{
event->uloop.cb = odhcpd_receive_packets;
return uloop_fd_add(&event->uloop, ULOOP_READ |
((event->handle_error) ? ULOOP_ERROR_CB : 0));
}
int odhcpd_deregister(struct odhcpd_event *event)
{
event->uloop.cb = NULL;
return uloop_fd_delete(&event->uloop);
}