iputils.c 17.4 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185
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));
	req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
	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 203 204 205
	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
	req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL;
	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 246 247 248 249
	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
	req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
	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
}

D
Dmitry Kozlov 已提交
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;
D
Dmitry Kozlov 已提交
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)
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 = ifindex ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE;
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 493
	addattr32(&req.n, sizeof(req), RTA_DST, dst);

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

496 497 498
	net->rtnl_put(rth);

	return r;
499 500
}

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

511 512 513
	if (!rth)
		return -1;

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

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

	addattr32(&req.n, sizeof(req), RTA_DST, dst);
527 528 529

	if (ifindex)
		addattr32(&req.n, sizeof(req), RTA_OIF, ifindex);
530 531

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

534 535 536
	net->rtnl_put(rth);

	return r;
537 538
}

539 540 541 542 543 544 545
int __export ip6route_add(int ifindex, struct in6_addr *dst, int pref_len, int proto)
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct rtmsg i;
		char buf[4096];
	} req;
546 547
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
548

549 550 551 552
	if (!rth)
		return -1;

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

554 555 556 557 558 559 560 561 562 563 564 565 566 567
	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;
	req.i.rtm_scope = RT_SCOPE_LINK;
	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));
	addattr32(&req.n, sizeof(req), RTA_OIF, ifindex);

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

570 571 572
	net->rtnl_put(rth);

	return r;
573 574 575 576 577 578 579 580 581
}

int __export ip6route_del(int ifindex, struct in6_addr *dst, int pref_len)
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct rtmsg i;
		char buf[4096];
	} req;
582 583
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
584 585 586 587 588

	if (!rth)
		return -1;

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

590 591 592 593 594 595 596 597 598 599 600 601
	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
	req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
	req.n.nlmsg_type = RTM_DELROUTE;
	req.i.rtm_family = AF_INET6;
	req.i.rtm_table = RT_TABLE_MAIN;
	req.i.rtm_scope = RT_SCOPE_LINK;
	req.i.rtm_type = RTN_UNICAST;
	req.i.rtm_dst_len = pref_len;

	addattr_l(&req.n, sizeof(req), RTA_DST, dst, sizeof(*dst));

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

604 605 606
	net->rtnl_put(rth);

	return r;
607 608
}

609 610 611 612 613 614 615
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;
616 617
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634

	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)
635
		r = -1;
636

637 638 639
	net->rtnl_put(rth);

	return r;
640 641 642 643 644 645 646 647 648
}

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;
649 650
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666

	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_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)
667
		r = -1;
668

669 670 671
	net->rtnl_put(rth);

	return r;
672 673
}

674
in_addr_t __export iproute_get(in_addr_t dst, in_addr_t *gw)
675 676
{
	struct ipaddr_req {
677 678
		struct nlmsghdr n;
		struct rtmsg r;
679
		char buf[4096];
680 681 682 683 684
	} req;
	struct rtmsg *r;
	struct rtattr *tb[RTA_MAX+1];
	int len;
	in_addr_t res = 0;
685
	struct rtnl_handle *rth = net->rtnl_get();
686 687 688

	if (gw)
		*gw = 0;
689 690 691 692

	if (!rth)
		return -1;

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

695 696 697 698 699 700 701 702 703 704 705 706
	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;

707
	addattr32(&req.n, 4096, RTA_DST, dst);
708 709 710 711 712 713 714 715

	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;
716

717 718 719 720 721 722 723 724 725 726 727 728
	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);
729

730 731
	if (tb[RTA_PREFSRC])
		res = *(uint32_t *)RTA_DATA(tb[RTA_PREFSRC]);
732 733 734 735

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

736
out:
737 738
	net->rtnl_put(rth);

739 740 741 742 743 744
	return res;
}

int __export iprule_add(uint32_t addr, int table)
{
	struct {
745 746
		struct nlmsghdr n;
		struct rtmsg i;
747
		char buf[4096];
748
	} req;
749 750
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
751

752 753 754
	if (!rth)
		return -1;

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

757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
	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)
772
		r = -1;
773

774 775 776
	net->rtnl_put(rth);

	return r;
777 778 779 780 781 782 783
}

int __export iprule_del(uint32_t addr, int table)
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct rtmsg i;
784
		char buf[4096];
785
	} req;
786 787
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
788

789 790 791
	if (!rth)
		return -1;

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

794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
	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)
809
		r = -1;
810

811
	net->rtnl_put(rth);
812

813
	return r;
814
}