-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnlinline.h
523 lines (482 loc) · 16.6 KB
/
nlinline.h
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
* nlinline: compact library of inline function providing the most common
* network configuration operations.
*
* Copyright (C) 2019 Renzo Davoli <[email protected]> VirtualSquare team.
*
* nlinline is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef NLINLINE_H
#define NLINLINE_H
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_link.h>
#include <linux/if_addr.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
static inline int nlinline_if_nametoindex(const char *ifname);
static inline int nlinline_linksetupdown(unsigned int ifindex, int updown);
static inline int nlinline_linksetaddr(unsigned int ifindex, void *macaddr);
static inline int nlinline_linkgetaddr(unsigned int ifindex, void *macaddr);
static inline int nlinline_linksetmtu(unsigned int ifindex, unsigned int mtu);
static inline int nlinline_ipaddr_add(int family, void *addr, int prefixlen, unsigned int ifindex);
static inline int nlinline_ipaddr_del(int family, void *addr, int prefixlen, unsigned int ifindex);
static inline int nlinline_iproute_add(int family, void *dst_addr, int dst_prefixlen, void *gw_addr, unsigned int ifindex);
static inline int nlinline_iproute_del(int family, void *dst_addr, int dst_prefixlen, void *gw_addr, unsigned int ifindex);
struct nl_iplink_data {
int tag;
int len;
const void *data;
};
#define nl_iplink_strdata(t,s) &(struct nl_iplink_data) {(t), strlen(s) + 1, (s)}, 1
static inline int nlinline_iplink_add(const char *ifname, unsigned int ifindex, const char *type,
struct nl_iplink_data *ifd, int nifd);
static inline int nlinline_iplink_del(const char *ifname, unsigned int ifindex);
static inline int nl_addrdata2prefix(unsigned char prefixlen, unsigned char flags, unsigned char scope);
static inline int nl_routedata2prefix(unsigned char prefixlen, unsigned char type, unsigned char scope);
#ifndef __NLINLINE_PLUSTYPE
#define __PLUSARG
#define __PLUSF
#define __PLUS
#define __nlinline_if_nametoindex nlinline_if_nametoindex
#define __nlinline_linksetupdown nlinline_linksetupdown
#define __nlinline_linksetaddr nlinline_linksetaddr
#define __nlinline_linkgetaddr nlinline_linkgetaddr
#define __nlinline_linksetmtu nlinline_linksetmtu
#define __nlinline_ipaddr_add nlinline_ipaddr_add
#define __nlinline_ipaddr_del nlinline_ipaddr_del
#define __nlinline_iproute_add nlinline_iproute_add
#define __nlinline_iproute_del nlinline_iproute_del
#define __nlinline_iplink_add nlinline_iplink_add
#define __nlinline_iplink_del nlinline_iplink_del
#define __nlinline_nldialog nlinline_nldialog
#else
#define __PLUSARG __NLINLINE_PLUSTYPE *__stack,
#define __PLUSF __stack->
#define __PLUS __stack,
#endif
/**************************
* Implementation
**************************/
static inline int nlinline_family2addrlen(int family) {
switch (family) {
case AF_INET: return 4;
case AF_INET6: return 16;
default: return 0;
__default: return 0;
}
}
#define NLINLINE_ADDRDATA2PREFIX_MAGIC 0x70
#define NLINLINE_ROUTEDATA2PREFIX_MAGIC 0x60
static inline int nl_addrdata2prefix(unsigned char prefixlen, unsigned char flags, unsigned char scope) {
return NLINLINE_ADDRDATA2PREFIX_MAGIC << 24 | flags << 16 | scope << 8 | prefixlen;
}
static inline int nl_routedata2prefix(unsigned char prefixlen, unsigned char type, unsigned char scope) {
return NLINLINE_ROUTEDATA2PREFIX_MAGIC << 24 | type << 16 | scope << 8 | prefixlen;
}
static inline int __nlinline_geterror(__PLUSARG int fd) {
struct {
struct nlmsghdr h;
union {
struct nlmsgerr e;
struct ifinfomsg i;
};
} msg;
int replylen = __PLUSF recv(fd, &msg, sizeof(msg), 0);
if (replylen < 0)
return -1;
if (replylen <= sizeof(msg.h))
return errno = EFAULT, -1;
switch (msg.h.nlmsg_type) {
case NLMSG_ERROR: if (msg.e.error >= 0)
return msg.e.error;
else
return errno = -msg.e.error, -1;
case RTM_NEWLINK: return msg.i.ifi_index;
default: return errno = EFAULT, -1;
}
}
static inline int __nlinline_open_send(__PLUSARG void *msg) {
struct nlmsghdr *nlmsg = msg;
struct sockaddr_nl sanl = {AF_NETLINK, 0, 0, 0};
int fd;
#ifdef __NLINLINE_PLUSTYPE
if (__PLUSF msocket)
fd = __PLUSF msocket(__PLUSF mstack, AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
else
#endif
fd = __PLUSF socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
if (fd < 0)
return fd;
if (__PLUSF bind(fd, (struct sockaddr *) &sanl, sizeof(struct sockaddr_nl)) < 0)
return __PLUSF close(fd), -1;
if (__PLUSF send(fd, msg, nlmsg->nlmsg_len, 0) < 0)
return __PLUSF close(fd), -1;
return fd;
}
static inline int __nlinline_nldialog(__PLUSARG void *msg) {
int ret_value;
int fd = __nlinline_open_send(__PLUS msg);
if (fd < 0)
return fd;
ret_value = __nlinline_geterror(__PLUS fd);
__PLUSF close(fd);
return ret_value;
}
static inline int __nlinline_if_nametoindex(__PLUSARG const char *ifname) {
struct {
struct nlmsghdr h;
struct ifinfomsg i;
struct nlattr a;
char ifname[IFNAMSIZ];
} msg = {
.h.nlmsg_len = sizeof(msg.h) + sizeof(msg.i),
.h.nlmsg_type = RTM_GETLINK,
.h.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
.h.nlmsg_seq = 1,
.a.nla_type = IFLA_IFNAME,
};
int namelen = snprintf(msg.ifname, IFNAMSIZ, "%s", ifname);
msg.a.nla_len = sizeof(msg.a) + namelen + 1;
msg.h.nlmsg_len += (msg.a.nla_len + 3) & ~3;
return __nlinline_nldialog(__PLUS &msg);
}
static inline int __nlinline_linksetupdown(__PLUSARG unsigned int ifindex, int updown) {
struct {
struct nlmsghdr h;
struct ifinfomsg i;
} msg = {
.h.nlmsg_len = sizeof(msg),
.h.nlmsg_type = RTM_SETLINK,
.h.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
.h.nlmsg_seq = 1,
.i.ifi_index = ifindex,
.i.ifi_flags = (updown) ? IFF_UP : 0,
.i.ifi_change=IFF_UP };
return __nlinline_nldialog(__PLUS &msg);
}
struct __nlinline_macaddr {
unsigned char byte[6];
};
struct __nlinline_macattr {
struct nlattr h;
struct __nlinline_macaddr addr;
};
static inline int __nlinline_linksetaddr(__PLUSARG unsigned int ifindex, void *macaddr) {
struct {
struct nlmsghdr h;
struct ifinfomsg i;
struct __nlinline_macattr mac;
} msg = {
.h.nlmsg_len = sizeof(msg),
.h.nlmsg_type = RTM_NEWLINK,
.h.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
.h.nlmsg_seq = 1,
.i.ifi_index = ifindex,
.mac.h.nla_len = sizeof(struct __nlinline_macattr),
.mac.h.nla_type = IFLA_ADDRESS,
.mac.addr = *((struct __nlinline_macaddr *) macaddr)
};
return __nlinline_nldialog(__PLUS &msg);
}
static inline int __nlinline_linkgetaddr(__PLUSARG unsigned int ifindex, void *macaddr) {
struct {
struct nlmsghdr h;
struct ifinfomsg i;
unsigned char attrs[];
} *reply, msg = {
.h.nlmsg_len = sizeof(msg),
.h.nlmsg_type = RTM_GETLINK,
.h.nlmsg_flags = NLM_F_REQUEST,
.h.nlmsg_seq = 1,
.i.ifi_index = ifindex,
};
struct sockaddr_nl sanl = {AF_NETLINK, 0, 0, 0};
int ret_value;
int fd = __nlinline_open_send(__PLUS &msg);
if (fd < 0)
return fd;
if ((ret_value = __PLUSF recv(fd, NULL, 0, MSG_PEEK|MSG_TRUNC)) < 0)
return close(fd), -1;
unsigned char buf[ret_value];
if ((ret_value = __PLUSF recv(fd, buf, ret_value, 0)) < 0)
return __PLUSF close(fd), -1;
reply = (void *) buf;
if (ret_value <= sizeof(msg.h) || ret_value < reply->h.nlmsg_len)
return errno = EFAULT, __PLUSF close(fd), -1;
if (reply->h.nlmsg_type != RTM_NEWLINK)
return errno = ENODEV, __PLUSF close(fd), -1;
unsigned char *limit = buf + reply->h.nlmsg_len;
unsigned char *scan = reply->attrs;
while (scan < limit) {
struct nlattr *attr = (void *) scan;
if (attr->nla_type == IFLA_ADDRESS && attr->nla_len >= 6) {
memcpy(macaddr, attr + 1, 6);
return __PLUSF close(fd), 0;
}
scan += NLMSG_ALIGN(attr->nla_len);
}
__PLUSF close(fd);
return errno = ENOENT, -1;
}
struct __nlinline_u32 {
struct nlattr h;
__u32 value;
};
static inline int __nlinline_linksetmtu(__PLUSARG unsigned int ifindex, unsigned int mtu) {
struct {
struct nlmsghdr h;
struct ifinfomsg i;
struct __nlinline_u32 mtu;
} msg = {
.h.nlmsg_len = sizeof(msg),
.h.nlmsg_type = RTM_NEWLINK,
.h.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
.h.nlmsg_seq = 1,
.i.ifi_index = ifindex,
.mtu.h.nla_len = sizeof(struct __nlinline_u32),
.mtu.h.nla_type = IFLA_MTU,
.mtu.value = mtu,
};
return __nlinline_nldialog(__PLUS &msg);
}
struct __nlinline_ipv4addr {
unsigned char byte[4];
};
struct __nlinline_ipv6addr {
unsigned char byte[16];
};
struct __nlinline_ipv4attr {
struct nlattr h;
struct __nlinline_ipv4addr addr;
};
struct __nlinline_ipv6attr {
struct nlattr h;
struct __nlinline_ipv6addr addr;
};
static inline int __nlinline_ipaddr(__PLUSARG
int request, int xflags, int family, void *addr, int prefixlen, unsigned int ifindex) {
int addrlen = nlinline_family2addrlen(family);
if (addrlen == 0)
return errno = EINVAL, -1;
else {
struct {
struct nlmsghdr h;
struct ifaddrmsg i;
union {
struct __nlinline_ipv4attr a4[2];
struct __nlinline_ipv6attr a6[2];
};
} msg = {
.h.nlmsg_len = sizeof(msg.h) + sizeof(msg.i),
.h.nlmsg_type = request,
.h.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | xflags,
.h.nlmsg_seq = 1,
.i.ifa_family = family,
.i.ifa_prefixlen = prefixlen,
.i.ifa_scope = RT_SCOPE_UNIVERSE,
.i.ifa_index = ifindex};
if (prefixlen >> 24 == NLINLINE_ADDRDATA2PREFIX_MAGIC) {
msg.i.ifa_scope = prefixlen >> 8;
msg.i.ifa_flags = prefixlen >> 16;
}
if (addrlen == 4) {
msg.a4[0].h.nla_len = msg.a4[1].h.nla_len = sizeof(struct nlattr) + addrlen;
msg.a4[0].h.nla_type = IFA_LOCAL;
msg.a4[1].h.nla_type = IFA_ADDRESS;
msg.a4[0].addr = msg.a4[1].addr = *((struct __nlinline_ipv4addr *) addr);
msg.h.nlmsg_len += 2 * sizeof(msg.a4[0]);
} else {
msg.a6[0].h.nla_len = msg.a6[1].h.nla_len = sizeof(struct nlattr) + addrlen;
msg.a6[0].h.nla_type = IFA_LOCAL;
msg.a6[1].h.nla_type = IFA_ADDRESS;
msg.a6[0].addr = msg.a6[1].addr = *((struct __nlinline_ipv6addr *) addr);
msg.h.nlmsg_len += 2 * sizeof(msg.a6[0]);
}
return __nlinline_nldialog(__PLUS &msg);
}
}
static inline int __nlinline_ipaddr_add(__PLUSARG
int family, void *addr, int prefixlen, unsigned int ifindex) {
return __nlinline_ipaddr(__PLUS
RTM_NEWADDR, NLM_F_EXCL | NLM_F_CREATE, family, addr, prefixlen, ifindex);
}
static inline int __nlinline_ipaddr_del(__PLUSARG
int family, void *addr, int prefixlen, unsigned int ifindex) {
return __nlinline_ipaddr(__PLUS
RTM_DELADDR, 0, family, addr, prefixlen, ifindex);
}
static inline int __nlinline_iproute(__PLUSARG
int request, int xflags, int family, void *dst_addr, int dst_prefixlen, void *gw_addr, unsigned int ifindex) {
int addrlen = nlinline_family2addrlen(family);
if (addrlen == 0)
return errno = EINVAL, -1;
else {
struct {
struct nlmsghdr h;
struct rtmsg r;
struct __nlinline_u32 oif;
union {
struct __nlinline_ipv4attr a4[2];
struct __nlinline_ipv6attr a6[2];
};
} msg = {
.h.nlmsg_len = sizeof(msg.h) + sizeof(msg.r) + sizeof(msg.oif),
.h.nlmsg_type = request,
.h.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | xflags,
.h.nlmsg_seq = 1,
.r.rtm_family = family,
.r.rtm_dst_len = dst_prefixlen,
.r.rtm_table = RT_TABLE_MAIN,
.r.rtm_protocol = RTPROT_BOOT,
.r.rtm_scope = RT_SCOPE_UNIVERSE,
.r.rtm_type = RTN_UNICAST,
.oif.h.nla_type = (ifindex == 0) ? RTA_UNSPEC : RTA_OIF,
.oif.h.nla_len = sizeof(msg.oif),
.oif.value = ifindex,
};
if (dst_prefixlen >> 24 == NLINLINE_ROUTEDATA2PREFIX_MAGIC) {
msg.r.rtm_scope = dst_prefixlen >> 8;
msg.r.rtm_type = dst_prefixlen >> 16;
}
int nattr = 0;
if (addrlen == 4) {
if (dst_prefixlen > 0) {
msg.a4[nattr].h.nla_len = sizeof(msg.a4[0]);
msg.a4[nattr].h.nla_type = RTA_DST;
msg.a4[nattr].addr = *((struct __nlinline_ipv4addr *)dst_addr);
nattr++;
}
if (gw_addr != NULL) {
msg.a4[nattr].h.nla_len = sizeof(msg.a4[0]);
msg.a4[nattr].h.nla_type = RTA_GATEWAY;
msg.a4[nattr].addr = *((struct __nlinline_ipv4addr *)gw_addr);
nattr++;
}
msg.h.nlmsg_len += nattr * sizeof(msg.a4[0]);
} else {
if (dst_prefixlen > 0) {
msg.a6[nattr].h.nla_len = sizeof(msg.a6[0]);
msg.a6[nattr].h.nla_type = RTA_DST;
msg.a6[nattr].addr = *((struct __nlinline_ipv6addr *)dst_addr);
nattr++;
}
if (gw_addr != NULL) {
msg.a6[nattr].h.nla_len = sizeof(msg.a6[0]);
msg.a6[nattr].h.nla_type = RTA_GATEWAY;
msg.a6[nattr].addr = *((struct __nlinline_ipv6addr *)gw_addr);
nattr++;
}
msg.h.nlmsg_len += nattr * sizeof(msg.a6[0]);
}
return __nlinline_nldialog(__PLUS &msg);
}
}
static inline int __nlinline_iproute_add(__PLUSARG
int family, void *dst_addr, int dst_prefixlen, void *gw_addr, unsigned int ifindex) {
return __nlinline_iproute(__PLUS
RTM_NEWROUTE, NLM_F_EXCL | NLM_F_CREATE, family, dst_addr, dst_prefixlen, gw_addr, ifindex);
}
static inline int __nlinline_iproute_del(__PLUSARG
int family, void *dst_addr, int dst_prefixlen, void *gw_addr, unsigned int ifindex) {
return __nlinline_iproute(__PLUS
RTM_DELROUTE, 0, family, dst_addr, dst_prefixlen, gw_addr, ifindex);
}
static inline int __nlinline_add_attr(void *buf, unsigned int type, const void *data, int datalen) {
int attrlen = sizeof(struct nlattr) + datalen;
if (buf) {
struct nlattr *attr = buf;
attr->nla_len = attrlen;
attr->nla_type = type;
memcpy(attr + 1, data, datalen);
}
return (attrlen + 3) & ~3;
}
static inline int __nlinline_add_strattr(void *buf, unsigned int type, const char *s) {
if (s)
return __nlinline_add_attr(buf, type, s, strlen(s) + 1);
else
return 0;
}
#define IFLA_VDE_VNL 1
/* [IFLA_IFNAME...] [IFLA_NEW_IFINDEX ""] [IFLA_LINKINFO [IFLA_INFO_KIND ...] [IFLA_INFO_DATA [..ifd.. */
static inline int __nlinline_iplink_add(__PLUSARG const char *ifname, unsigned int ifindex, const char *type, struct nl_iplink_data *ifd, int nifd) {
int msglen = sizeof(struct nlmsghdr) + sizeof(struct ifinfomsg) + sizeof(struct nlattr) +
(ifname ? __nlinline_add_strattr(NULL, IFLA_IFNAME, ifname) : 0) +
__nlinline_add_strattr(NULL, IFLA_INFO_KIND, type) +
((ifindex == -1) ? __nlinline_add_strattr(NULL, IFLA_NEW_IFINDEX, "") : 0) +
(nifd > 0 ? sizeof(struct nlattr) : 0);
for (int i = 0; i < nifd; i++)
msglen += __nlinline_add_attr(NULL, ifd[i].tag, ifd[i].data, ifd[i].len);
unsigned char msgbuf[msglen];
unsigned char *rawmsg = msgbuf;
struct {
struct nlmsghdr h;
struct ifinfomsg i;
} *msg = (void *) rawmsg;
struct nlattr *infohdr;
memset(msgbuf, 0, msglen);
msg->h.nlmsg_len = msglen;
msg->h.nlmsg_type = RTM_NEWLINK;
msg->h.nlmsg_flags = NLM_F_EXCL | NLM_F_CREATE | NLM_F_REQUEST | NLM_F_ACK;
msg->h.nlmsg_seq = 1;
msg->i.ifi_index = ifindex == -1 ? 0 : ifindex;
rawmsg += sizeof(*msg);
if (ifname)
rawmsg += __nlinline_add_strattr(rawmsg, IFLA_IFNAME, ifname);
if (ifindex == -1)
rawmsg += __nlinline_add_strattr(rawmsg, IFLA_NEW_IFINDEX, "");
infohdr = (void *) rawmsg;
rawmsg += sizeof(*infohdr);
infohdr->nla_type = IFLA_LINKINFO;
rawmsg += __nlinline_add_strattr(rawmsg, IFLA_INFO_KIND, type);
if (nifd > 0) {
struct nlattr *datahdr = (void *) rawmsg;
rawmsg += sizeof(*datahdr);
datahdr->nla_type = IFLA_INFO_DATA;
for (int i = 0; i < nifd; i++)
rawmsg += __nlinline_add_attr(rawmsg, ifd[i].tag, ifd[i].data, ifd[i].len);
datahdr->nla_len = rawmsg - (unsigned char *)datahdr;
}
infohdr->nla_len = rawmsg - (unsigned char *)infohdr;
return __nlinline_nldialog(__PLUS &msgbuf);
}
static inline int __nlinline_iplink_del(__PLUSARG const char *ifname, unsigned int ifindex) {
struct {
struct nlmsghdr h;
struct ifinfomsg i;
struct nlattr a;
char ifname[IFNAMSIZ];
} msg = {
.h.nlmsg_len = sizeof(msg.h) + sizeof(msg.i),
.h.nlmsg_type = RTM_DELLINK,
.h.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
.h.nlmsg_seq = 1,
.i.ifi_index = ifindex,
.a.nla_type = IFLA_IFNAME,
};
if (ifname) {
int namelen = snprintf(msg.ifname, IFNAMSIZ, "%s", ifname);
msg.a.nla_len = sizeof(msg.a) + namelen + 1;
msg.h.nlmsg_len += (msg.a.nla_len + 3) & ~3;
}
return __nlinline_nldialog(__PLUS &msg);
}
#endif