forked from lxc/libresource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_route.c
415 lines (382 loc) · 9.27 KB
/
net_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
#ifndef _RESOURCE_H
#include "resource.h"
#endif
#include <net/if.h>
#include <sys/socket.h>
#include <linux/rtnetlink.h>
#include <linux/if_link.h>
#include <linux/if_arp.h>
#include <linux/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include "resource_impl.h"
static int count, ecount;
static int connect_route()
{
struct sockaddr_nl nl_saddr;
int err, net_sock;
net_sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
if (net_sock == -1) {
err = errno;
eprintf("Error in socket(), errno %d\n", err);
errno = err;
return -1;
}
bzero(&nl_saddr, sizeof(nl_saddr));
nl_saddr.nl_family = AF_NETLINK;
err = bind(net_sock, (struct sockaddr *)&nl_saddr, sizeof(nl_saddr));
if (err == -1) {
err = errno;
eprintf("Error in bind(), errno %d\n", err);
close(net_sock);
errno = err;
return -1;
}
return net_sock;
}
static int send_route_req(int net_sock)
{
int err;
struct route {
struct nlmsghdr nl_hdr;
struct rtmsg rt;
} req;
memset(&req, 0, sizeof(req));
req.nl_hdr.nlmsg_type = RTM_GETROUTE;
req.nl_hdr.nlmsg_len = sizeof(req);
req.nl_hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
req.nl_hdr.nlmsg_seq = 0;
req.rt.rtm_family = AF_INET;
err = send(net_sock, &req, req.nl_hdr.nlmsg_len, 0);
if (err == -1) {
err = errno;
eprintf("Error in request send(), errno %d\n", err);
errno = err;
return -1;
}
return 0;
}
#ifdef TESTING
#define IPA_F "%hhu.%hhu.%hhu.%hhu"
#define IPA_V(str) str[0], str[1], str[2], str[3]
static inline bool is_zero(char *str, int len)
{
for (int i = 0; i < len; i++)
if (str[i] != 0)
return false;
return true;
}
static inline const char *scope_str(int scope)
{
switch (scope) {
case RT_SCOPE_UNIVERSE: return "universe";
case RT_SCOPE_SITE: return "site";
case RT_SCOPE_LINK: return "link";
case RT_SCOPE_HOST: return "host";
case RT_SCOPE_NOWHERE: return "nowhere";
default: return "invalid";
}
}
static inline const char *proto_str(int proto)
{
switch (proto) {
case RTPROT_DHCP: return "dhcp";
case RTPROT_KERNEL: return "kernel";
case RTPROT_STATIC: return "static";
case RTPROT_BGP: return "bgp";
case RTPROT_ISIS: return "isis";
case RTPROT_OSPF: return "ospf";
case RTPROT_RIP: return "rip";
case RTPROT_EIGRP: return "eigrp";
default: return "other";
}
}
static void print_rt_info(struct rt_info *rt, FILE *fp)
{
char ifname[IF_NAMESIZE];
if (rt->family == AF_INET) {
if (rt->dst_prefix_len == 0) {
fprintf(fp, "default");
} else {
fprintf(fp, IPA_F, IPA_V(rt->dest));
fprintf(fp, "/%02hhu", rt->dst_prefix_len);
}
if (!is_zero(rt->gate, MAX_BYTES_IPV4)) {
fprintf(fp, " via ");
fprintf(fp, IPA_F, IPA_V(rt->gate));
}
fprintf(fp, " dev %s", if_indextoname(rt->index, ifname));
fprintf(fp, " proto %s", proto_str(rt->protocol));
if (rt->scope != RT_SCOPE_UNIVERSE)
fprintf(fp, " scope %s", scope_str(rt->scope));
if (!is_zero(rt->prefsrc, MAX_BYTES_IPV4)) {
fprintf(fp, " src ");
fprintf(fp, IPA_F, IPA_V(rt->prefsrc));
}
fprintf(fp, " metric %d \n", rt->metric);
}
}
#endif
static int get_attr(struct rtattr *at[], struct rt_info *rt, struct rtmsg *m)
{
bzero(rt, sizeof(*rt));
rt->family = m->rtm_family;
rt->protocol = m->rtm_protocol;
rt->scope = m->rtm_scope;
if (at[RTA_TABLE])
rt->table = *(int *)RTA_DATA(at[RTA_TABLE]);
if (at[RTA_OIF])
rt->index = *(int *)RTA_DATA(at[RTA_OIF]);
if (at[RTA_DST]) {
if (m->rtm_family == AF_INET) {
memcpy(rt->dest, RTA_DATA(at[RTA_DST]), MAX_BYTES_IPV4);
rt->dst_prefix_len = m->rtm_dst_len;
}
if (m->rtm_family == AF_INET6) {
memcpy(rt->dest, RTA_DATA(at[RTA_DST]), MAX_BYTES_IPV6);
rt->dst_prefix_len = m->rtm_dst_len;
}
}
if (at[RTA_SRC]) {
if (m->rtm_family == AF_INET) {
#ifdef PRINTLOGS
printf("RTA_SRC for AF_INET!!\n");
#endif
}
if (m->rtm_family == AF_INET6) {
memcpy(rt->src, RTA_DATA(at[RTA_SRC]), MAX_BYTES_IPV6);
rt->src_prefix_len = m->rtm_src_len;
}
}
if (at[RTA_PREFSRC]) {
if (m->rtm_family == AF_INET)
memcpy(rt->prefsrc, RTA_DATA(at[RTA_PREFSRC]), MAX_BYTES_IPV4);
if (m->rtm_family == AF_INET6)
memcpy(rt->prefsrc, RTA_DATA(at[RTA_PREFSRC]), MAX_BYTES_IPV6);
}
if (at[RTA_GATEWAY]) {
if (m->rtm_family == AF_INET)
memcpy(rt->gate, RTA_DATA(at[RTA_GATEWAY]), MAX_BYTES_IPV4);
if (m->rtm_family == AF_INET6)
memcpy(rt->gate, RTA_DATA(at[RTA_GATEWAY]), MAX_BYTES_IPV6);
}
if (at[RTA_PRIORITY])
rt->metric = *(int *)RTA_DATA(at[RTA_PRIORITY]);
return 0;
}
static int parse_attr(struct rtattr *at[], struct rtattr *rta, int len)
{
memset(at, 0, sizeof(struct rtattr *) * (RTA_MAX + 1));
while (RTA_OK(rta, len)) {
if (rta->rta_type <= RTA_MAX)
at[rta->rta_type] = rta;
rta = RTA_NEXT(rta, len);
}
return 0;
}
static int get_route_len(int net_sock)
{
int recvl, err;
struct msghdr msg;
struct iovec iov;
struct sockaddr_nl nladdr;
memset(&msg, 0, sizeof(msg));
memset(&iov, 0, sizeof(iov));
msg.msg_name = &nladdr;
msg.msg_namelen = sizeof(nladdr);
iov.iov_base = NULL;
iov.iov_len = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
recvl = recvmsg(net_sock, &msg, MSG_PEEK | MSG_TRUNC);
if (recvl < 0) {
err = errno;
eprintf("Error in get length recvmsg(), errno %d\n", err);
errno = err;
return -1;
}
#ifdef PRINTLOGS
printf("received len %d\n",recvl);
#endif
return recvl;
}
static int get_max_routes(int len)
{
int header_size, max_routes;
header_size = (sizeof(struct nlmsghdr)) + (sizeof(struct rtmsg));
max_routes = len/header_size;
return max_routes;
}
static int handle_route_resp(int net_sock, void **out)
{
int err, recvl, len, max_routes, rt_size;
int rtind = 0, total_rt_size = 0;
struct iovec iov;
struct msghdr msg;
struct nlmsghdr *r;
struct nlmsgerr *nlmsg_err;
struct rtmsg *rt;
struct rtattr *at[RTA_MAX + 1];
struct rt_info *routes = NULL, *iroutes = NULL;
struct sockaddr_nl nladdr;
#ifdef TESTING
FILE *fp = NULL;
#endif
memset(&msg, 0, sizeof(msg));
msg.msg_name = &nladdr;
msg.msg_namelen = sizeof(nladdr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
while(1) {
len = get_route_len(net_sock);
if (len == -1) {
if (routes)
free(routes);
return -1;
}
char *buf = (char *)malloc(len);
if (!buf) {
if (routes)
free(routes);
return -1;
}
memset(&iov, 0, sizeof(iov));
iov.iov_base = buf;
iov.iov_len = len;
max_routes = get_max_routes(len);
rt_size = max_routes * sizeof(struct rt_info);
total_rt_size += rt_size;
void *tmp = (struct rt_info *) realloc(routes, total_rt_size);
if (!tmp) {
if (routes)
free(routes);
free(buf);
return -1;
}
routes = tmp;
iroutes = routes + rtind;
recvl = recvmsg(net_sock, &msg, 0);
if (recvl < 0) {
err = errno;
free(routes);
free(buf);
eprintf("Error in response recvmsg(), errno %d\n", err);
errno = err;
return -1;
}
#ifdef PRINTLOGS
printf("received len %d\n",recvl);
#endif
r = (struct nlmsghdr *) buf;
while (NLMSG_OK(r, recvl)) {
if (r->nlmsg_type == NLMSG_ERROR) {
nlmsg_err = (struct nlmsgerr *)NLMSG_DATA(r);
eprintf("NLMSG_ERROR error: %d\n",
nlmsg_err->error);
ecount++;
free(buf);
free(routes);
errno = nlmsg_err->error;
return -1;
} else if (r->nlmsg_type == NLMSG_DONE) {
#ifdef PRINTLOGS
printf("DONE\n");
#endif
free(buf);
*(struct rt_info **)out = routes;
#ifdef TESTING
if (fp)
fclose(fp);
#endif
return rtind;
}
rt = (struct rtmsg *)NLMSG_DATA(r);
len = r->nlmsg_len;
#ifdef PRINTLOGS
printf("r->nlmsg_type is %d (RTM_NEWROUTE = 24)\n", r->nlmsg_type);
printf("Received rtmsg, len %d\n", len);
printf("family: %hhu\n", rt->rtm_family);
printf("rtm_dst_len %hhu\n",rt->rtm_dst_len);
printf("rtm_src_len %hhu\n",rt->rtm_src_len);
printf("rtm_type: %hhu\n", rt->rtm_type);
printf("rtm_flags: %u\n",rt->rtm_flags);
printf("rtm_protocol: %hhu\n",rt->rtm_protocol);
#endif
len -= NLMSG_LENGTH(sizeof(*rt));
if (len < 0) {
eprintf("nlmsg_len incorrect");
errno = EINVAL;
free(buf);
free(routes);
return -1;
}
#ifdef PRINTLOGS
printf("len after sub %lu is %d\n",sizeof(*rt), len);
#endif
parse_attr(at, RTM_RTA(rt), len);
if (rt->rtm_family == AF_INET || rt->rtm_family == AF_INET6) {
get_attr(at, iroutes, rt);
#ifdef TESTING
fp = fopen ("./route_info.txt", "w");
print_rt_info(iroutes, fp);
#endif
iroutes++;
rtind++;
} else {
#ifdef PRINTLOGS
printf("Family is %d\n",rt->rtm_family);
#endif
}
r = NLMSG_NEXT(r, recvl);
count++;
}
free(buf);
#ifdef PRINTLOGS
printf("No. of routes: %d\n",rtind);
#endif
}
*(struct rt_info **)out = routes;
#ifdef TESTING
if (fp)
fclose(fp);
#endif
return rtind;
}
int get_net_route(void **out)
{
int err, net_sock;
net_sock = connect_route();
if (net_sock == -1)
return -1;
err = send_route_req(net_sock);
if (err == -1) {
close(net_sock);
return -1;
}
err = handle_route_resp(net_sock, out);
if (err == -1) {
close(net_sock);
return -1;
}
return err;
}
/* Get resource information related to network route */
int getrouteinfo(int res_id, void *out, size_t sz, void **hint, int flags)
{
int len;
switch (res_id) {
case RES_NET_ROUTE_ALL:
len = get_net_route(hint);
break;
default:
eprintf("Resource Id is invalid");
errno = EINVAL;
return -1;
}
return len;
}