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
|
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/ip.h>
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#define NLMSG_TAIL(nmsg) \
((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
#define NDA_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
#define NDA_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct ndmsg))
struct netlink_fd {
int fd;
__u32 seq;
};
static void netlink_parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
{
memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
while (RTA_OK(rta, len)) {
if (rta->rta_type <= max)
tb[rta->rta_type] = rta;
rta = RTA_NEXT(rta,len);
}
}
static int netlink_add_rtattr_l(struct nlmsghdr *n, int maxlen, int type,
const void *data, int alen)
{
int len = RTA_LENGTH(alen);
struct rtattr *rta;
if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen)
return FALSE;
rta = NLMSG_TAIL(n);
rta->rta_type = type;
rta->rta_len = len;
memcpy(RTA_DATA(rta), data, alen);
n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
return TRUE;
}
static int netlink_add_rtaddr_l(struct nlmsghdr *n, int maxlen, int type,
const struct sockaddr *addr)
{
switch (addr->sa_family) {
case AF_INET: {
struct sockaddr_in *sin = (struct sockaddr_in *) addr;
return netlink_add_rtattr_l(n, maxlen, type, &sin->sin_addr,
sizeof(sin->sin_addr));
}
default:
return FALSE;
}
}
static void netlink_close(struct netlink_fd *fd)
{
if (fd->fd >= 0) {
close(fd->fd);
fd->fd = 0;
}
}
static int netlink_open(struct netlink_fd *fd)
{
int buf = 16 * 1024;
fd->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
fd->seq = time(NULL);
if (fd->fd < 0) {
perror("Cannot open netlink socket");
return FALSE;
}
fcntl(fd->fd, F_SETFD, FD_CLOEXEC);
if (setsockopt(fd->fd, SOL_SOCKET, SO_SNDBUF, &buf, sizeof(buf)) < 0) {
perror("SO_SNDBUF");
goto error;
}
if (setsockopt(fd->fd, SOL_SOCKET, SO_RCVBUF, &buf, sizeof(buf)) < 0) {
perror("SO_RCVBUF");
goto error;
}
return TRUE;
error:
netlink_close(fd);
return FALSE;
}
static int netlink_receive(struct netlink_fd *fd, struct nlmsghdr *reply)
{
struct sockaddr_nl nladdr;
struct iovec iov;
struct msghdr msg = {
.msg_name = &nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = &iov,
.msg_iovlen = 1,
};
int got_reply = FALSE, len;
char buf[16*1024];
iov.iov_base = buf;
while (!got_reply) {
int status;
struct nlmsghdr *h;
iov.iov_len = sizeof(buf);
status = recvmsg(fd->fd, &msg, MSG_DONTWAIT);
if (status < 0) {
if (errno == EINTR)
continue;
if (errno == EAGAIN)
return reply == NULL;
fprintf(stderr, "Netlink overrun\n");
continue;
}
if (status == 0) {
fprintf(stderr, "Netlink returned EOF\n");
return FALSE;
}
h = (struct nlmsghdr *) buf;
while (NLMSG_OK(h, status)) {
if (reply != NULL &&
h->nlmsg_seq == reply->nlmsg_seq) {
len = h->nlmsg_len;
if (len > reply->nlmsg_len) {
fprintf(stderr, "Netlink message "
"truncated\n");
len = reply->nlmsg_len;
}
memcpy(reply, h, len);
got_reply = TRUE;
} else if (h->nlmsg_type != NLMSG_DONE) {
fprintf(stderr,
"Unknown NLmsg: 0x%08x, len %d\n",
h->nlmsg_type, h->nlmsg_len);
}
h = NLMSG_NEXT(h, status);
}
}
return TRUE;
}
static int netlink_send(struct netlink_fd *fd, struct nlmsghdr *req)
{
struct sockaddr_nl nladdr;
struct iovec iov = {
.iov_base = (void*) req,
.iov_len = req->nlmsg_len
};
struct msghdr msg = {
.msg_name = &nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = &iov,
.msg_iovlen = 1,
};
int status;
memset(&nladdr, 0, sizeof(nladdr));
nladdr.nl_family = AF_NETLINK;
req->nlmsg_seq = ++fd->seq;
status = sendmsg(fd->fd, &msg, 0);
if (status < 0) {
fprintf(stderr, "Cannot talk to rtnetlink\n");
return FALSE;
}
return TRUE;
}
static int netlink_talk(struct nlmsghdr *req, size_t replysize,
struct nlmsghdr *reply)
{
struct netlink_fd fd;
int ret = FALSE;
if (!netlink_open(&fd))
return FALSE;
if (reply == NULL)
req->nlmsg_flags |= NLM_F_ACK;
if (!netlink_send(&fd, req))
goto out;
if (reply != NULL) {
reply->nlmsg_len = replysize;
ret = netlink_receive(&fd, reply);
} else {
ret = TRUE;
}
out:
netlink_close(&fd);
return ret;
}
int netlink_route_get(struct sockaddr *dst, u_int16_t *mtu, char *ifname)
{
struct {
struct nlmsghdr n;
union {
struct rtmsg r;
struct ifinfomsg i;
};
char buf[1024];
} req;
struct rtmsg *r = NLMSG_DATA(&req.n);
struct rtattr *rta[RTA_MAX+1];
struct rtattr *rtax[RTAX_MAX+1];
struct rtattr *ifla[IFLA_MAX+1];
int index;
memset(&req, 0, sizeof(req));
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
req.n.nlmsg_flags = NLM_F_REQUEST;
req.n.nlmsg_type = RTM_GETROUTE;
req.r.rtm_family = dst->sa_family;
netlink_add_rtaddr_l(&req.n, sizeof(req), RTA_DST, dst);
req.r.rtm_dst_len = 32;
if (!netlink_talk(&req.n, sizeof(req), &req.n))
return FALSE;
netlink_parse_rtattr(rta, RTA_MAX, RTM_RTA(r),
RTM_PAYLOAD(&req.n));
if (mtu != NULL) {
if (rta[RTA_METRICS] == NULL)
return FALSE;
netlink_parse_rtattr(rtax, RTAX_MAX,
RTA_DATA(rta[RTA_METRICS]),
RTA_PAYLOAD(rta[RTA_METRICS]));
if (rtax[RTAX_MTU] == NULL)
return FALSE;
*mtu = *(int*) RTA_DATA(rtax[RTAX_MTU]);
}
if (ifname != NULL) {
if (rta[RTA_OIF] == NULL)
return FALSE;
index = *(int*) RTA_DATA(rta[RTA_OIF]);
memset(&req, 0, sizeof(req));
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
req.n.nlmsg_flags = NLM_F_REQUEST;
req.n.nlmsg_type = RTM_GETLINK;
req.i.ifi_index = index;
if (!netlink_talk(&req.n, sizeof(req), &req.n))
return FALSE;
netlink_parse_rtattr(ifla, IFLA_MAX, IFLA_RTA(r),
IFLA_PAYLOAD(&req.n));
if (ifla[IFLA_IFNAME] == NULL)
return FALSE;
memcpy(ifname, RTA_DATA(ifla[IFLA_IFNAME]),
RTA_PAYLOAD(ifla[IFLA_IFNAME]));
}
return TRUE;
}
|