-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.c
416 lines (355 loc) · 9.33 KB
/
route.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <linux/fib_rules.h>
#include "relayd.h"
static struct uloop_fd rtnl_sock;
static unsigned int rtnl_seq, rtnl_dump_seq;
int route_table = 16800;
static void rtnl_flush(void)
{
int fd;
fd = open("/proc/sys/net/ipv4/route/flush", O_WRONLY);
if (fd < 0)
return;
write(fd, "-1", 2);
close(fd);
}
enum {
RULE_F_ADD = (1 << 0),
RULE_F_DEFGW_WORKAROUND = (1 << 1),
};
static int get_route_table(struct relayd_interface *rif)
{
if (rif)
return rif->rt_table;
else
return local_route_table;
}
static void
rtnl_rule_request(struct relayd_interface *rif, int flags)
{
static struct {
struct nlmsghdr nl;
struct rtmsg rt;
struct {
struct rtattr rta;
int table;
} __packed table;
struct {
struct rtattr rta;
char ifname[IFNAMSIZ + 1];
} __packed dev;
} __packed req = {
.rt = {
.rtm_family = AF_INET,
.rtm_table = RT_TABLE_UNSPEC,
.rtm_scope = RT_SCOPE_UNIVERSE,
.rtm_protocol = RTPROT_BOOT,
},
.table.rta = {
.rta_type = FRA_TABLE,
.rta_len = sizeof(req.table),
},
};
const char *ifname = "lo";
int padding = sizeof(req.dev.ifname);
if (rif)
ifname = rif->ifname;
if (!(flags & RULE_F_DEFGW_WORKAROUND)) {
req.dev.rta.rta_type = FRA_IFNAME;
padding -= strlen(ifname) + 1;
strcpy(req.dev.ifname, ifname);
req.dev.rta.rta_len = sizeof(req.dev.rta) + strlen(ifname) + 1;
} else {
uint32_t val = 1;
req.dev.rta.rta_type = FRA_PRIORITY;
req.dev.rta.rta_len = sizeof(req.dev.rta) + sizeof(uint32_t);
padding -= sizeof(uint32_t);
memcpy(&req.dev.ifname, &val, sizeof(val));
}
req.table.table = get_route_table(rif);
req.nl.nlmsg_len = sizeof(req) - padding;
req.nl.nlmsg_flags = NLM_F_REQUEST;
if (flags & RULE_F_ADD) {
req.nl.nlmsg_type = RTM_NEWRULE;
req.nl.nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
req.rt.rtm_type = RTN_UNICAST;
} else {
req.nl.nlmsg_type = RTM_DELRULE;
req.rt.rtm_type = RTN_UNSPEC;
}
send(rtnl_sock.fd, &req, req.nl.nlmsg_len, 0);
rtnl_flush();
}
struct rtnl_addr {
struct rtattr rta;
uint8_t ipaddr[4];
} __packed;
static struct rtnl_addr *
rtnl_add_addr(struct rtnl_addr *addr, int *len, int type, const uint8_t *ipaddr)
{
addr->rta.rta_type = type;
memcpy(addr->ipaddr, ipaddr, 4);
*len += sizeof(*addr);
return addr + 1;
}
static void
rtnl_route_request(struct relayd_interface *rif, struct relayd_host *host,
struct relayd_route *route, bool add)
{
static struct {
struct nlmsghdr nl;
struct rtmsg rt;
struct {
struct rtattr rta;
int table;
} __packed table;
struct {
struct rtattr rta;
int ifindex;
} __packed dev;
struct rtnl_addr addr[3];
} __packed req = {
.rt = {
.rtm_family = AF_INET,
.rtm_dst_len = 32,
.rtm_table = RT_TABLE_MAIN,
},
.table.rta = {
.rta_type = RTA_TABLE,
.rta_len = sizeof(req.table),
},
.dev.rta = {
.rta_type = RTA_OIF,
.rta_len = sizeof(req.dev),
},
.addr[0].rta.rta_len = sizeof(struct rtnl_addr),
.addr[1].rta.rta_len = sizeof(struct rtnl_addr),
.addr[2].rta.rta_len = sizeof(struct rtnl_addr),
};
int pktlen = sizeof(req) - sizeof(req.addr);
struct rtnl_addr *addr = &req.addr[0];
const char *ifname = "loopback";
req.dev.ifindex = host->rif->sll.sll_ifindex;
req.table.table = get_route_table(rif);
req.nl.nlmsg_flags = NLM_F_REQUEST;
if (add) {
req.nl.nlmsg_type = RTM_NEWROUTE;
req.nl.nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;
req.rt.rtm_protocol = RTPROT_BOOT;
if (route) {
req.rt.rtm_scope = RT_SCOPE_UNIVERSE;
} else {
req.rt.rtm_scope = RT_SCOPE_LINK;
}
req.rt.rtm_type = RTN_UNICAST;
} else {
req.nl.nlmsg_type = RTM_DELROUTE;
req.rt.rtm_scope = RT_SCOPE_NOWHERE;
}
if (rif)
ifname = rif->ifname;
if (route) {
DPRINTF(2, "%s: add route to "IP_FMT"/%d via "IP_FMT" (%s)\n", ifname,
IP_BUF(route->dest), route->mask, IP_BUF(host->ipaddr),
host->rif->ifname);
req.rt.rtm_dst_len = route->mask;
if (route->mask)
addr = rtnl_add_addr(addr, &pktlen, RTA_DST, route->dest);
addr = rtnl_add_addr(addr, &pktlen, RTA_GATEWAY, host->ipaddr);
} else {
DPRINTF(2, "%s: add host route to "IP_FMT" (%s)\n", ifname,
IP_BUF(host->ipaddr), host->rif->ifname);
addr = rtnl_add_addr(addr, &pktlen, RTA_DST, host->ipaddr);
req.rt.rtm_dst_len = 32;
}
/* local route */
if (!rif)
addr = rtnl_add_addr(addr, &pktlen, RTA_PREFSRC, local_addr);
req.nl.nlmsg_len = pktlen;
if (route)
rtnl_rule_request(rif, RULE_F_DEFGW_WORKAROUND | RULE_F_ADD);
send(rtnl_sock.fd, &req, pktlen, 0);
if (route)
rtnl_rule_request(rif, RULE_F_DEFGW_WORKAROUND);
rtnl_flush();
}
void
rtnl_route_set(struct relayd_host *host, struct relayd_route *route, bool add)
{
struct relayd_interface *rif;
list_for_each_entry(rif, &interfaces, list) {
if (rif == host->rif)
continue;
rtnl_route_request(rif, host, route, add);
}
if (local_route_table)
rtnl_route_request(NULL, host, route, add);
}
void relayd_add_interface_routes(struct relayd_interface *rif)
{
rif->rt_table = route_table++;
rtnl_rule_request(rif, RULE_F_ADD);
}
void relayd_del_interface_routes(struct relayd_interface *rif)
{
rtnl_rule_request(rif, 0);
}
#ifndef NDA_RTA
#define NDA_RTA(r) \
((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
#endif
static void rtnl_parse_newneigh(struct nlmsghdr *h)
{
struct relayd_interface *rif = NULL;
struct ndmsg *r = NLMSG_DATA(h);
const uint8_t *lladdr = NULL;
const uint8_t *ipaddr = NULL;
struct rtattr *rta;
int len;
if (r->ndm_family != AF_INET)
return;
list_for_each_entry(rif, &interfaces, list) {
if (rif->sll.sll_ifindex == r->ndm_ifindex)
goto found_interface;
}
return;
found_interface:
len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
for (rta = NDA_RTA(r); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
switch(rta->rta_type) {
case NDA_LLADDR:
lladdr = RTA_DATA(rta);
break;
case NDA_DST:
ipaddr = RTA_DATA(rta);
break;
default:
break;
}
}
if (!lladdr || !ipaddr || (r->ndm_state & (NUD_INCOMPLETE|NUD_FAILED)))
return;
if (!memcmp(lladdr, "\x00\x00\x00\x00\x00\x00", ETH_ALEN))
return;
DPRINTF(1, "%s: Found ARP cache entry for host "IP_FMT" ("MAC_FMT")\n",
rif->ifname, IP_BUF(ipaddr), MAC_BUF(lladdr));
relayd_refresh_host(rif, lladdr, ipaddr);
}
static void rtnl_parse_packet(void *data, int len)
{
struct nlmsghdr *h;
for (h = data; NLMSG_OK(h, len); h = NLMSG_NEXT(h, len)) {
if (h->nlmsg_type == NLMSG_DONE ||
h->nlmsg_type == NLMSG_ERROR)
return;
if (h->nlmsg_seq != rtnl_dump_seq)
continue;
if (h->nlmsg_type == RTM_NEWNEIGH)
rtnl_parse_newneigh(h);
}
}
static void rtnl_cb(struct uloop_fd *fd, unsigned int events)
{
struct sockaddr_nl nladdr;
static uint8_t buf[16384];
struct iovec iov = {
.iov_base = buf,
.iov_len = sizeof(buf),
};
struct msghdr msg = {
.msg_name = &nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = &iov,
.msg_iovlen = 1,
};
do {
int len;
len = recvmsg(rtnl_sock.fd, &msg, 0);
if (len < 0) {
if (errno == EINTR)
continue;
return;
}
if (!len)
break;
if (nladdr.nl_pid != 0)
continue;
rtnl_parse_packet(buf, len);
} while (1);
}
static void rtnl_dump_request(int nlmsg_type)
{
static struct {
struct nlmsghdr nlh;
struct rtgenmsg g;
} req = {
.nlh = {
.nlmsg_len = sizeof(req),
.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,
.nlmsg_pid = 0,
},
.g.rtgen_family = AF_INET,
};
req.nlh.nlmsg_type = nlmsg_type;
req.nlh.nlmsg_seq = rtnl_seq;
send(rtnl_sock.fd, &req, sizeof(req), 0);
rtnl_seq++;
}
int relayd_rtnl_init(void)
{
struct sockaddr_nl snl_local;
const int one = 1;
rtnl_sock.fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (rtnl_sock.fd < 0) {
perror("socket(AF_NETLINK)");
return -1;
}
snl_local.nl_family = AF_NETLINK;
/*START: MUSTAFA: CHANGES TO "RESOLVE ADDRESS ALREADY IN USE" ISSUE */
printf("setting nl_pid = %d\n",getpid());
snl_local.nl_pid = getpid();
if(setsockopt(rtnl_sock.fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) {
perror("setsockopt(AF_NETLINK:rtnl_sock.fd)");
}
/*END: MUSTAFA: CHANGES TO "RESOLVE ADDRESS ALREADY IN USE" ISSUE */
if (bind(rtnl_sock.fd, (struct sockaddr *) &snl_local, sizeof(struct sockaddr_nl)) < 0) {
perror("PID:::AF_NETLINK:rtnl_sock.fd bind");
close(rtnl_sock.fd);
return -1;
}
rtnl_sock.cb = rtnl_cb;
uloop_fd_add(&rtnl_sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
rtnl_seq = time(NULL);
rtnl_dump_seq = rtnl_seq;
rtnl_dump_request(RTM_GETNEIGH);
rtnl_rule_request(NULL, RULE_F_ADD);
return 0;
}
void relayd_rtnl_done(void)
{
rtnl_rule_request(NULL, 0);
uloop_fd_delete(&rtnl_sock);
close(rtnl_sock.fd);
}