iputils.c 18.9 KB
Newer Older
K
Kozlov Dmitry 已提交
1 2 3 4 5
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <fcntl.h>
6
#include <pthread.h>
K
Kozlov Dmitry 已提交
7 8 9 10 11 12 13
#include <net/if_arp.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/uio.h>
14 15 16
//#include <linux/if_link.h>
//#include <linux/if_addr.h>
//#include <linux/rtnetlink.h>
17
#include <linux/fib_rules.h>
K
Kozlov Dmitry 已提交
18 19 20

#include "log.h"

21
#include "libnetlink.h"
22
#include "iputils.h"
23
#include "ap_net.h"
24

D
Dmitry Kozlov 已提交
25 26 27 28 29 30 31
#ifdef ACCEL_DP
#define _malloc(x) malloc(x)
#define _free(x) free(x)
#include "init.h"
#include "common.h"
#else
#include "triton.h"
32
#include "memdebug.h"
D
Dmitry Kozlov 已提交
33
#endif
34

K
Kozlov Dmitry 已提交
35 36 37 38 39 40 41 42 43 44
struct arg
{
	iplink_list_func func;
	void *arg;
};

static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
{
	struct ifinfomsg *ifi = NLMSG_DATA(n);
	struct rtattr *tb[IFLA_MAX + 1];
45
	struct rtattr *tb2[IFLA_MAX + 1];
K
Kozlov Dmitry 已提交
46
	struct arg *a = arg;
47
	int vid = 0, iflink = 0;
K
Kozlov Dmitry 已提交
48 49 50 51 52 53 54 55 56 57 58 59

	if (n->nlmsg_type != RTM_NEWLINK)
		return 0;

	if (n->nlmsg_len < NLMSG_LENGTH(sizeof(*ifi)))
		return -1;

	memset(tb, 0, sizeof(tb));
	parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));

	if (tb[IFLA_IFNAME] == NULL)
		return 0;
60

61 62
	if (tb[IFLA_LINKINFO]) {
		parse_rtattr_nested(tb2, IFLA_MAX, tb[IFLA_LINKINFO]);
63
		if (tb2[IFLA_INFO_KIND] && !strcmp(RTA_DATA(tb2[IFLA_INFO_KIND]), "vlan")) {
64 65 66 67 68 69 70
			parse_rtattr_nested(tb2, IFLA_MAX, tb2[IFLA_INFO_DATA]);
			vid = *(uint16_t *)RTA_DATA(tb2[IFLA_VLAN_ID]);
		}
	}

	if (tb[IFLA_LINK])
		iflink = *(int *)RTA_DATA(tb[IFLA_LINK]);
K
Kozlov Dmitry 已提交
71 72
	//printf("%i %s\n", ifi->ifi_index, RTA_DATA(tb[IFLA_IFNAME]));

73
	return a->func(ifi->ifi_index, ifi->ifi_flags, RTA_DATA(tb[IFLA_IFNAME]), iflink, vid, a->arg);
K
Kozlov Dmitry 已提交
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
}

int __export iplink_list(iplink_list_func func, void *arg)
{
	struct rtnl_handle rth;
	struct arg a = { .func = func, .arg = arg };

	if (rtnl_open(&rth, 0)) {
		log_emerg("iplink: cannot open rtnetlink\n");
		return -1;
	}

	if (rtnl_wilddump_request(&rth, AF_PACKET, RTM_GETLINK) < 0) {
		log_emerg("iplink: cannot send dump request\n");
		goto out_err;
	}

	if (rtnl_dump_filter(&rth, store_nlmsg, &a, NULL, NULL) < 0) {
		log_emerg("iplink: dump terminated\n");
		goto out_err;
	}

	rtnl_close(&rth);

	return 0;

out_err:
	rtnl_close(&rth);

	return -1;
}

106 107 108 109 110
int __export iplink_get_stats(int ifindex, struct rtnl_link_stats *stats)
{
	struct iplink_req {
		struct nlmsghdr n;
		struct ifinfomsg i;
111
		char buf[4096];
112 113 114 115
	} req;
	struct ifinfomsg *ifi;
	int len;
	struct rtattr *tb[IFLA_MAX + 1];
116 117
	struct rtnl_handle *rth = net->rtnl_get();
	int r = -1;
118

119 120 121
	if (!rth)
		return -1;

122
	memset(&req, 0, sizeof(req) - 4096);
123

124 125 126 127 128 129 130
	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
	req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
	req.n.nlmsg_type = RTM_GETLINK;
	req.i.ifi_family = AF_PACKET;
	req.i.ifi_index = ifindex;

	if (rtnl_talk(rth, &req.n, 0, 0, &req.n, NULL, NULL, 0) < 0)
131
		goto out;
132 133

	if (req.n.nlmsg_type != RTM_NEWLINK)
134
		goto out;
135 136 137 138 139 140 141

	ifi = NLMSG_DATA(&req.n);

	len = req.n.nlmsg_len;

	len -= NLMSG_LENGTH(sizeof(*ifi));
	if (len < 0)
142
		goto out;
143 144

	parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
145
	if (tb[IFLA_STATS]) {
146
		memcpy(stats, RTA_DATA(tb[IFLA_STATS]), sizeof(*stats));
147 148
		r = 0;
	}
149

150 151 152 153
out:
	net->rtnl_put(rth);

	return r;
154
}
155

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
int __export iplink_set_mtu(int ifindex, int mtu)
{
	struct iplink_req {
		struct nlmsghdr n;
		struct ifinfomsg i;
		char buf[1024];
	} req;
	struct rtnl_handle *rth = net->rtnl_get();
	int r;

	if (!rth)
		return -1;

	memset(&req, 0, sizeof(req) - 1024);

	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
172
	req.n.nlmsg_flags = NLM_F_REQUEST;
173 174 175 176 177 178 179 180 181 182 183 184 185
	req.n.nlmsg_type = RTM_SETLINK;
	req.i.ifi_family = AF_UNSPEC;
	req.i.ifi_index = ifindex;

	addattr_l(&req.n, 1024, IFLA_MTU, &mtu, 4);

	r = rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0);

	net->rtnl_put(rth);

	return r;
}

D
Dmitry Kozlov 已提交
186
int __export iplink_vlan_add(const char *ifname, int ifindex, int vid)
187 188 189 190
{
	struct iplink_req {
		struct nlmsghdr n;
		struct ifinfomsg i;
191
		char buf[4096];
192 193
	} req;
	struct rtattr *linkinfo, *data;
194 195
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
196

197 198 199
	if (!rth)
		return -1;

200
	memset(&req, 0, sizeof(req) - 4096);
201

202
	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
203
	req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
204 205
	req.n.nlmsg_type = RTM_NEWLINK;
	req.i.ifi_family = AF_UNSPEC;
206

207 208
	addattr_l(&req.n, 4096, IFLA_LINK, &ifindex, 4);
	addattr_l(&req.n, 4096, IFLA_IFNAME, ifname, strlen(ifname));
209

210
	linkinfo = NLMSG_TAIL(&req.n);
211 212
	addattr_l(&req.n, 4096, IFLA_LINKINFO, NULL, 0);
	addattr_l(&req.n, 4096, IFLA_INFO_KIND, "vlan", 4);
213 214

	data = NLMSG_TAIL(&req.n);
215 216
	addattr_l(&req.n, 4096, IFLA_INFO_DATA, NULL, 0);
	addattr_l(&req.n, 4096, IFLA_VLAN_ID, &vid, 2);
217
	data->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)data;
218

219 220 221
	linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo;

	if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0)
222
		r = -1;
223

224 225 226
	net->rtnl_put(rth);

	return r;
227 228
}

D
Dmitry Kozlov 已提交
229
int __export iplink_vlan_del(int ifindex)
230 231 232 233
{
	struct iplink_req {
		struct nlmsghdr n;
		struct ifinfomsg i;
234
		char buf[4096];
235 236
	} req;
	struct rtattr *linkinfo;
237 238
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
239

240 241 242
	if (!rth)
		return -1;

243
	memset(&req, 0, sizeof(req) - 4096);
244

245
	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
246
	req.n.nlmsg_flags = NLM_F_REQUEST;
247 248 249
	req.n.nlmsg_type = RTM_DELLINK;
	req.i.ifi_family = AF_UNSPEC;
	req.i.ifi_index = ifindex;
250

251
	linkinfo = NLMSG_TAIL(&req.n);
252 253
	addattr_l(&req.n, 4096, IFLA_LINKINFO, NULL, 0);
	addattr_l(&req.n, 4096, IFLA_INFO_KIND, "vlan", 4);
254 255

	/*data = NLMSG_TAIL(&req.n);
256
	addattr_l(&req.n, 4096, IFLA_VLAN_ID, &vid, 2);
257
	data->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)data;*/
258

259 260 261
	linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo;

	if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0)
262
		r = -1;
263

264 265 266
	net->rtnl_put(rth);

	return r;
267
}
268

269
int __export iplink_vlan_get_vid(int ifindex, int *iflink)
270 271 272 273 274 275 276 277 278
{
	struct iplink_req {
		struct nlmsghdr n;
		struct ifinfomsg i;
		char buf[4096];
	} req;
	struct ifinfomsg *ifi;
	int len;
	struct rtattr *tb[IFLA_MAX + 1];
279 280
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
281 282 283 284 285 286 287 288 289 290 291 292 293

	if (!rth)
		return -1;

	memset(&req, 0, sizeof(req) - 4096);

	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
	req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
	req.n.nlmsg_type = RTM_GETLINK;
	req.i.ifi_family = AF_PACKET;
	req.i.ifi_index = ifindex;

	if (rtnl_talk(rth, &req.n, 0, 0, &req.n, NULL, NULL, 0) < 0)
294
		goto out;
295 296

	if (req.n.nlmsg_type != RTM_NEWLINK)
297
		goto out;
298 299 300 301 302 303 304

	ifi = NLMSG_DATA(&req.n);

	len = req.n.nlmsg_len;

	len -= NLMSG_LENGTH(sizeof(*ifi));
	if (len < 0)
305
		goto out;
306 307 308 309

	parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);

	if (!tb[IFLA_LINKINFO])
310
		goto out;
311

312 313 314
	if (iflink && tb[IFLA_LINK])
		*iflink = *(int *)RTA_DATA(tb[IFLA_LINK]);

315 316 317
	parse_rtattr_nested(tb, IFLA_MAX, tb[IFLA_LINKINFO]);

	if (strcmp(RTA_DATA(tb[IFLA_INFO_KIND]), "vlan"))
318
		goto out;
319 320

	parse_rtattr_nested(tb, IFLA_MAX, tb[IFLA_INFO_DATA]);
321 322 323 324 325 326
	r = *(uint16_t *)RTA_DATA(tb[IFLA_VLAN_ID]);

out:
	net->rtnl_put(rth);

	return r;
327 328 329
}


330 331 332 333 334
int __export ipaddr_add(int ifindex, in_addr_t addr, int mask)
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct ifaddrmsg i;
335
		char buf[4096];
336
	} req;
337 338
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
339

340 341 342
	if (!rth)
		return -1;

343
	memset(&req, 0, sizeof(req) - 4096);
344

345 346 347 348 349 350 351 352 353 354
	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
	req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
	req.n.nlmsg_type = RTM_NEWADDR;
	req.i.ifa_family = AF_INET;
	req.i.ifa_index = ifindex;
	req.i.ifa_prefixlen = mask;

	addattr32(&req.n, sizeof(req), IFA_LOCAL, addr);

	if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0)
355
		r = -1;
356

357 358 359
	net->rtnl_put(rth);

	return r;
360 361
}

362
int __export ipaddr_add_peer(int ifindex, in_addr_t addr, in_addr_t peer_addr)
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct ifaddrmsg i;
		char buf[4096];
	} req;
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;

	if (!rth)
		return -1;

	memset(&req, 0, sizeof(req) - 4096);

	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
	req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
	req.n.nlmsg_type = RTM_NEWADDR;
	req.i.ifa_family = AF_INET;
	req.i.ifa_index = ifindex;
382
	req.i.ifa_prefixlen = 32;
383 384 385 386 387 388 389 390 391 392 393 394

	addattr32(&req.n, sizeof(req), IFA_LOCAL, addr);
	addattr32(&req.n, sizeof(req), IFA_ADDRESS, peer_addr);

	if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0)
		r = -1;

	net->rtnl_put(rth);

	return r;
}

395
int __export ipaddr_del(int ifindex, in_addr_t addr, int mask)
396 397 398 399
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct ifaddrmsg i;
400
		char buf[4096];
401
	} req;
402 403
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
404

405 406 407
	if (!rth)
		return -1;

408
	memset(&req, 0, sizeof(req) - 4096);
409

410 411 412 413 414
	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
	req.n.nlmsg_flags = NLM_F_REQUEST;
	req.n.nlmsg_type = RTM_DELADDR;
	req.i.ifa_family = AF_INET;
	req.i.ifa_index = ifindex;
415
	req.i.ifa_prefixlen = mask;
416 417 418 419

	addattr32(&req.n, sizeof(req), IFA_LOCAL, addr);

	if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0)
420
		r = -1;
421

422 423 424
	net->rtnl_put(rth);

	return r;
425 426
}

D
Dmitry Kozlov 已提交
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
int __export ipaddr_del_peer(int ifindex, in_addr_t addr, in_addr_t peer)
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct ifaddrmsg i;
		char buf[4096];
	} req;
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;

	if (!rth)
		return -1;

	memset(&req, 0, sizeof(req) - 4096);

	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
	req.n.nlmsg_flags = NLM_F_REQUEST;
	req.n.nlmsg_type = RTM_DELADDR;
	req.i.ifa_family = AF_INET;
	req.i.ifa_index = ifindex;
	req.i.ifa_prefixlen = 32;

	addattr32(&req.n, sizeof(req), IFA_LOCAL, addr);
	addattr32(&req.n, sizeof(req), IFA_ADDRESS, peer);

	if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0)
		r = -1;

	net->rtnl_put(rth);

	return r;
}

460
int __export iproute_add(int ifindex, in_addr_t src, in_addr_t dst, in_addr_t gw, int proto, int mask, uint32_t prio)
461 462 463 464
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct rtmsg i;
465
		char buf[4096];
466
	} req;
467 468
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
469

470 471 472
	if (!rth)
		return -1;

473
	memset(&req, 0, sizeof(req) - 4096);
474

475 476 477 478 479
	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
	req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
	req.n.nlmsg_type = RTM_NEWROUTE;
	req.i.rtm_family = AF_INET;
	req.i.rtm_table = RT_TABLE_MAIN;
480
	req.i.rtm_scope = gw ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
481
	req.i.rtm_protocol = proto;
482
	req.i.rtm_type = RTN_UNICAST;
483
	req.i.rtm_dst_len = mask;
484

485 486
	if (ifindex)
		addattr32(&req.n, sizeof(req), RTA_OIF, ifindex);
K
Kozlov Dmitry 已提交
487 488
	if (src)
		addattr32(&req.n, sizeof(req), RTA_PREFSRC, src);
489 490
	if (gw)
		addattr32(&req.n, sizeof(req), RTA_GATEWAY, gw);
491 492
	if (prio)
		addattr32(&req.n, sizeof(req), RTA_PRIORITY, prio);
493 494 495
	addattr32(&req.n, sizeof(req), RTA_DST, dst);

	if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0)
496
		r = -1;
497

498 499 500
	net->rtnl_put(rth);

	return r;
501 502
}

503
int __export iproute_del(int ifindex, in_addr_t src, in_addr_t dst, in_addr_t gw, int proto, int mask, uint32_t prio)
504 505 506 507
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct rtmsg i;
508
		char buf[4096];
509
	} req;
510 511
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
512

513 514 515
	if (!rth)
		return -1;

516
	memset(&req, 0, sizeof(req) - 4096);
517

518
	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
519
	req.n.nlmsg_flags = NLM_F_REQUEST;
520 521 522
	req.n.nlmsg_type = RTM_DELROUTE;
	req.i.rtm_family = AF_INET;
	req.i.rtm_table = RT_TABLE_MAIN;
523
	req.i.rtm_scope = gw ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
524
	req.i.rtm_protocol = proto;
525
	req.i.rtm_type = RTN_UNICAST;
526
	req.i.rtm_dst_len = mask;
527

528 529
	if (ifindex)
		addattr32(&req.n, sizeof(req), RTA_OIF, ifindex);
530 531 532 533
	if (src)
		addattr32(&req.n, sizeof(req), RTA_PREFSRC, src);
	if (gw)
		addattr32(&req.n, sizeof(req), RTA_GATEWAY, gw);
534 535
	if (prio)
		addattr32(&req.n, sizeof(req), RTA_PRIORITY, prio);
536
	addattr32(&req.n, sizeof(req), RTA_DST, dst);
537 538

	if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0)
539
		r = -1;
540

541 542 543
	net->rtnl_put(rth);

	return r;
544 545
}

546
int __export ip6route_add(int ifindex, const struct in6_addr *dst, int pref_len, const struct in6_addr *gw, int proto, uint32_t prio)
547 548 549 550 551 552
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct rtmsg i;
		char buf[4096];
	} req;
553 554
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
555

556 557 558 559
	if (!rth)
		return -1;

	memset(&req, 0, sizeof(req) - 4096);
560

561 562 563 564 565
	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
	req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
	req.n.nlmsg_type = RTM_NEWROUTE;
	req.i.rtm_family = AF_INET6;
	req.i.rtm_table = RT_TABLE_MAIN;
566
	req.i.rtm_scope = RT_SCOPE_UNIVERSE;
567 568 569 570 571
	req.i.rtm_protocol = proto;
	req.i.rtm_type = RTN_UNICAST;
	req.i.rtm_dst_len = pref_len;

	addattr_l(&req.n, sizeof(req), RTA_DST, dst, sizeof(*dst));
572 573 574 575 576 577
	if (ifindex)
		addattr32(&req.n, sizeof(req), RTA_OIF, ifindex);
	if (gw)
		addattr_l(&req.n, sizeof(req), RTA_GATEWAY, gw, sizeof(*gw));
	if (prio)
		addattr32(&req.n, sizeof(req), RTA_PRIORITY, prio);
578 579

	if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0)
580
		r = -1;
581

582 583 584
	net->rtnl_put(rth);

	return r;
585 586
}

587
int __export ip6route_del(int ifindex, const struct in6_addr *dst, int pref_len, const struct in6_addr *gw, int proto, uint32_t prio)
588 589 590 591 592 593
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct rtmsg i;
		char buf[4096];
	} req;
594 595
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
596 597 598 599 600

	if (!rth)
		return -1;

	memset(&req, 0, sizeof(req) - 4096);
601

602
	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
603
	req.n.nlmsg_flags = NLM_F_REQUEST;
604 605 606
	req.n.nlmsg_type = RTM_DELROUTE;
	req.i.rtm_family = AF_INET6;
	req.i.rtm_table = RT_TABLE_MAIN;
607
	req.i.rtm_scope = RT_SCOPE_UNIVERSE;
608
	req.i.rtm_protocol = proto;
609 610 611 612
	req.i.rtm_type = RTN_UNICAST;
	req.i.rtm_dst_len = pref_len;

	addattr_l(&req.n, sizeof(req), RTA_DST, dst, sizeof(*dst));
613 614 615 616 617 618
	if (ifindex)
		addattr32(&req.n, sizeof(req), RTA_OIF, ifindex);
	if (gw)
		addattr_l(&req.n, sizeof(req), RTA_GATEWAY, gw, sizeof(*gw));
	if (prio)
		addattr32(&req.n, sizeof(req), RTA_PRIORITY, prio);
619 620

	if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0)
621
		r = -1;
622

623 624 625
	net->rtnl_put(rth);

	return r;
626 627
}

628 629 630 631 632 633 634
int __export ip6addr_add(int ifindex, struct in6_addr *addr, int prefix_len)
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct ifaddrmsg i;
		char buf[4096];
	} req;
635 636
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653

	if (!rth)
		return -1;

	memset(&req, 0, sizeof(req) - 4096);

	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
	req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
	req.n.nlmsg_type = RTM_NEWADDR;
	req.i.ifa_family = AF_INET6;
	req.i.ifa_index = ifindex;
	req.i.ifa_prefixlen = prefix_len;
	req.i.ifa_flags = IFA_F_NODAD;

	addattr_l(&req.n, sizeof(req), IFA_ADDRESS, addr, 16);

	if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0)
654
		r = -1;
655

656 657 658
	net->rtnl_put(rth);

	return r;
659 660
}

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
int __export ip6addr_add_peer(int ifindex, struct in6_addr *addr, struct in6_addr *peer)
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct ifaddrmsg i;
		char buf[4096];
	} req;
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;

	if (!rth)
		return -1;

	memset(&req, 0, sizeof(req) - 4096);

	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
	req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
	req.n.nlmsg_type = RTM_NEWADDR;
	req.i.ifa_family = AF_INET6;
	req.i.ifa_index = ifindex;
	req.i.ifa_prefixlen = 128;
	req.i.ifa_flags = IFA_F_NODAD;

	addattr_l(&req.n, sizeof(req), IFA_LOCAL, addr, 16);
	addattr_l(&req.n, sizeof(req), IFA_ADDRESS, peer, 16);

	if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0)
		r = -1;

	net->rtnl_put(rth);

	return r;
}

695 696 697 698 699 700 701
int __export ip6addr_del(int ifindex, struct in6_addr *addr, int prefix_len)
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct ifaddrmsg i;
		char buf[4096];
	} req;
702 703
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
704 705 706 707 708 709 710

	if (!rth)
		return -1;

	memset(&req, 0, sizeof(req) - 4096);

	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
711
	req.n.nlmsg_flags = NLM_F_REQUEST;
712 713 714 715 716 717 718 719
	req.n.nlmsg_type = RTM_DELADDR;
	req.i.ifa_family = AF_INET6;
	req.i.ifa_index = ifindex;
	req.i.ifa_prefixlen = prefix_len;

	addattr_l(&req.n, sizeof(req), IFA_ADDRESS, addr, 16);

	if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0)
720
		r = -1;
721

722 723 724
	net->rtnl_put(rth);

	return r;
725 726
}

727
in_addr_t __export iproute_get(in_addr_t dst, in_addr_t *gw)
728 729
{
	struct ipaddr_req {
730 731
		struct nlmsghdr n;
		struct rtmsg r;
732
		char buf[4096];
733 734 735 736 737
	} req;
	struct rtmsg *r;
	struct rtattr *tb[RTA_MAX+1];
	int len;
	in_addr_t res = 0;
738
	struct rtnl_handle *rth = net->rtnl_get();
739 740 741

	if (gw)
		*gw = 0;
742 743 744 745

	if (!rth)
		return -1;

746
	memset(&req, 0, sizeof(req) - 4096);
747

748 749 750 751 752 753 754 755 756 757 758 759
	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 = AF_INET;
	req.r.rtm_table = 0;
	req.r.rtm_protocol = 0;
	req.r.rtm_scope = 0;
	req.r.rtm_type = 0;
	req.r.rtm_tos = 0;
	req.r.rtm_src_len = 0;
	req.r.rtm_dst_len = 32;

760
	addattr32(&req.n, 4096, RTA_DST, dst);
761 762 763 764 765 766 767 768

	if (rtnl_talk(rth, &req.n, 0, 0, &req.n, NULL, NULL, 0) < 0) {
		log_error("failed to detect route to server\n");
		goto out;
	}

	r = NLMSG_DATA(&req.n);
	len = req.n.nlmsg_len;
769

770 771 772 773 774 775 776 777 778 779 780 781
	if (req.n.nlmsg_type != RTM_NEWROUTE) {
		log_error("failed to detect route to server (wrong netlink message type)");
		goto out;
	}

	len -= NLMSG_LENGTH(sizeof(*r));
	if (len < 0) {
		log_error("failed to detect route to server (wrong netlink message length)");
		goto out;
	}

	parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
782

783 784
	if (tb[RTA_PREFSRC])
		res = *(uint32_t *)RTA_DATA(tb[RTA_PREFSRC]);
785 786 787 788

	if (gw && tb[RTA_GATEWAY])
		*gw = *(uint32_t *)RTA_DATA(tb[RTA_GATEWAY]);

789
out:
790 791
	net->rtnl_put(rth);

792 793 794 795 796 797
	return res;
}

int __export iprule_add(uint32_t addr, int table)
{
	struct {
798 799
		struct nlmsghdr n;
		struct rtmsg i;
800
		char buf[4096];
801
	} req;
802 803
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
804

805 806 807
	if (!rth)
		return -1;

808
	memset(&req, 0, sizeof(req) - 4096);
809

810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
	req.n.nlmsg_flags = NLM_F_REQUEST;
	req.n.nlmsg_type = RTM_NEWRULE;
	req.i.rtm_family = AF_INET;
	req.i.rtm_table = table < 256 ? table : RT_TABLE_UNSPEC;
	req.i.rtm_scope = RT_SCOPE_UNIVERSE;
	req.i.rtm_protocol = RTPROT_BOOT;
	req.i.rtm_type = RTN_UNICAST;
	req.i.rtm_src_len = 32;

	addattr32(&req.n, sizeof(req), FRA_SRC, addr);
	if (table >= 256)
		addattr32(&req.n, sizeof(req), FRA_TABLE, table);

	if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0)
825
		r = -1;
826

827 828 829
	net->rtnl_put(rth);

	return r;
830 831 832 833 834 835 836
}

int __export iprule_del(uint32_t addr, int table)
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct rtmsg i;
837
		char buf[4096];
838
	} req;
839 840
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
841

842 843 844
	if (!rth)
		return -1;

845
	memset(&req, 0, sizeof(req) - 4096);
846

847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
	req.n.nlmsg_flags = NLM_F_REQUEST;
	req.n.nlmsg_type = RTM_DELRULE;
	req.i.rtm_family = AF_INET;
	req.i.rtm_table = table < 256 ? table : RT_TABLE_UNSPEC;
	req.i.rtm_scope = RT_SCOPE_UNIVERSE;
	req.i.rtm_protocol = RTPROT_BOOT;
	req.i.rtm_type = RTN_UNICAST;
	req.i.rtm_src_len = 32;

	addattr32(&req.n, sizeof(req), FRA_SRC, addr);
	if (table >= 256)
		addattr32(&req.n, sizeof(req), FRA_TABLE, table);

	if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0)
862
		r = -1;
863

864
	net->rtnl_put(rth);
865

866
	return r;
867
}