iputils.c 15.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

D
Dmitry Kozlov 已提交
156
int __export iplink_vlan_add(const char *ifname, int ifindex, int vid)
157 158 159 160
{
	struct iplink_req {
		struct nlmsghdr n;
		struct ifinfomsg i;
161
		char buf[4096];
162 163
	} req;
	struct rtattr *linkinfo, *data;
164 165
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
166

167 168 169
	if (!rth)
		return -1;

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

172 173 174 175
	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;
176

177 178
	addattr_l(&req.n, 4096, IFLA_LINK, &ifindex, 4);
	addattr_l(&req.n, 4096, IFLA_IFNAME, ifname, strlen(ifname));
179

180
	linkinfo = NLMSG_TAIL(&req.n);
181 182
	addattr_l(&req.n, 4096, IFLA_LINKINFO, NULL, 0);
	addattr_l(&req.n, 4096, IFLA_INFO_KIND, "vlan", 4);
183 184

	data = NLMSG_TAIL(&req.n);
185 186
	addattr_l(&req.n, 4096, IFLA_INFO_DATA, NULL, 0);
	addattr_l(&req.n, 4096, IFLA_VLAN_ID, &vid, 2);
187
	data->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)data;
188

189 190 191
	linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo;

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

194 195 196
	net->rtnl_put(rth);

	return r;
197 198
}

D
Dmitry Kozlov 已提交
199
int __export iplink_vlan_del(int ifindex)
200 201 202 203
{
	struct iplink_req {
		struct nlmsghdr n;
		struct ifinfomsg i;
204
		char buf[4096];
205 206
	} req;
	struct rtattr *linkinfo;
207 208
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
209

210 211 212
	if (!rth)
		return -1;

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

215 216 217 218 219
	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;
220

221
	linkinfo = NLMSG_TAIL(&req.n);
222 223
	addattr_l(&req.n, 4096, IFLA_LINKINFO, NULL, 0);
	addattr_l(&req.n, 4096, IFLA_INFO_KIND, "vlan", 4);
224 225

	/*data = NLMSG_TAIL(&req.n);
226
	addattr_l(&req.n, 4096, IFLA_VLAN_ID, &vid, 2);
227
	data->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)data;*/
228

229 230 231
	linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo;

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

234 235 236
	net->rtnl_put(rth);

	return r;
237
}
238

239
int __export iplink_vlan_get_vid(int ifindex, int *iflink)
240 241 242 243 244 245 246 247 248
{
	struct iplink_req {
		struct nlmsghdr n;
		struct ifinfomsg i;
		char buf[4096];
	} req;
	struct ifinfomsg *ifi;
	int len;
	struct rtattr *tb[IFLA_MAX + 1];
249 250
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
251 252 253 254 255 256 257 258 259 260 261 262 263

	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)
264
		goto out;
265 266

	if (req.n.nlmsg_type != RTM_NEWLINK)
267
		goto out;
268 269 270 271 272 273 274

	ifi = NLMSG_DATA(&req.n);

	len = req.n.nlmsg_len;

	len -= NLMSG_LENGTH(sizeof(*ifi));
	if (len < 0)
275
		goto out;
276 277 278 279

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

	if (!tb[IFLA_LINKINFO])
280
		goto out;
281

282 283 284
	if (iflink && tb[IFLA_LINK])
		*iflink = *(int *)RTA_DATA(tb[IFLA_LINK]);

285 286 287
	parse_rtattr_nested(tb, IFLA_MAX, tb[IFLA_LINKINFO]);

	if (strcmp(RTA_DATA(tb[IFLA_INFO_KIND]), "vlan"))
288
		goto out;
289 290

	parse_rtattr_nested(tb, IFLA_MAX, tb[IFLA_INFO_DATA]);
291 292 293 294 295 296
	r = *(uint16_t *)RTA_DATA(tb[IFLA_VLAN_ID]);

out:
	net->rtnl_put(rth);

	return r;
297 298 299
}


300 301 302 303 304
int __export ipaddr_add(int ifindex, in_addr_t addr, int mask)
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct ifaddrmsg i;
305
		char buf[4096];
306
	} req;
307 308
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
309

310 311 312
	if (!rth)
		return -1;

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

315 316 317 318 319 320 321 322 323 324
	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)
325
		r = -1;
326

327 328 329
	net->rtnl_put(rth);

	return r;
330 331
}

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

342 343 344
	if (!rth)
		return -1;

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

347 348 349 350 351
	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;
352
	req.i.ifa_prefixlen = mask;
353 354 355 356

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

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

359 360 361
	net->rtnl_put(rth);

	return r;
362 363
}

364
int __export iproute_add(int ifindex, in_addr_t src, in_addr_t dst, in_addr_t gw, int proto, int mask)
365 366 367 368
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct rtmsg i;
369
		char buf[4096];
370
	} req;
371 372
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
373

374 375 376
	if (!rth)
		return -1;

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

379 380 381 382 383
	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;
384
	req.i.rtm_scope = ifindex ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE;
385
	req.i.rtm_protocol = proto;
386
	req.i.rtm_type = RTN_UNICAST;
387
	req.i.rtm_dst_len = mask;
388

389 390
	if (ifindex)
		addattr32(&req.n, sizeof(req), RTA_OIF, ifindex);
K
Kozlov Dmitry 已提交
391 392
	if (src)
		addattr32(&req.n, sizeof(req), RTA_PREFSRC, src);
393 394
	if (gw)
		addattr32(&req.n, sizeof(req), RTA_GATEWAY, gw);
395 396 397
	addattr32(&req.n, sizeof(req), RTA_DST, dst);

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

400 401 402
	net->rtnl_put(rth);

	return r;
403 404
}

405
int __export iproute_del(int ifindex, in_addr_t dst, int proto, int mask)
406 407 408 409
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct rtmsg i;
410
		char buf[4096];
411
	} req;
412 413
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
414

415 416 417
	if (!rth)
		return -1;

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

420 421 422 423 424
	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;
425
	req.i.rtm_scope = ifindex ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE;
426
	req.i.rtm_protocol = proto;
427
	req.i.rtm_type = RTN_UNICAST;
428
	req.i.rtm_dst_len = mask;
429 430

	addattr32(&req.n, sizeof(req), RTA_DST, dst);
431 432 433

	if (ifindex)
		addattr32(&req.n, sizeof(req), RTA_OIF, ifindex);
434 435

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

438 439 440
	net->rtnl_put(rth);

	return r;
441 442
}

443 444 445 446 447 448 449
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;
450 451
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
452

453 454 455 456
	if (!rth)
		return -1;

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

458 459 460 461 462 463 464 465 466 467 468 469 470 471
	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)
472
		r = -1;
473

474 475 476
	net->rtnl_put(rth);

	return r;
477 478 479 480 481 482 483 484 485
}

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;
486 487
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
488 489 490 491 492

	if (!rth)
		return -1;

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

494 495 496 497 498 499 500 501 502 503 504 505
	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)
506
		r = -1;
507

508 509 510
	net->rtnl_put(rth);

	return r;
511 512
}

513 514 515 516 517 518 519
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;
520 521
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538

	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)
539
		r = -1;
540

541 542 543
	net->rtnl_put(rth);

	return r;
544 545 546 547 548 549 550 551 552
}

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;
553 554
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570

	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)
571
		r = -1;
572

573 574 575
	net->rtnl_put(rth);

	return r;
576 577
}

578
in_addr_t __export iproute_get(in_addr_t dst, in_addr_t *gw)
579 580
{
	struct ipaddr_req {
581 582
		struct nlmsghdr n;
		struct rtmsg r;
583
		char buf[4096];
584 585 586 587 588
	} req;
	struct rtmsg *r;
	struct rtattr *tb[RTA_MAX+1];
	int len;
	in_addr_t res = 0;
589
	struct rtnl_handle *rth = net->rtnl_get();
590 591 592

	if (gw)
		*gw = 0;
593 594 595 596

	if (!rth)
		return -1;

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

599 600 601 602 603 604 605 606 607 608 609 610
	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;

611
	addattr32(&req.n, 4096, RTA_DST, dst);
612 613 614 615 616 617 618 619

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

621 622 623 624 625 626 627 628 629 630 631 632
	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);
633

634 635
	if (tb[RTA_PREFSRC])
		res = *(uint32_t *)RTA_DATA(tb[RTA_PREFSRC]);
636 637 638 639

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

640
out:
641 642
	net->rtnl_put(rth);

643 644 645 646 647 648
	return res;
}

int __export iprule_add(uint32_t addr, int table)
{
	struct {
649 650
		struct nlmsghdr n;
		struct rtmsg i;
651
		char buf[4096];
652
	} req;
653 654
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
655

656 657 658
	if (!rth)
		return -1;

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

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
	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)
676
		r = -1;
677

678 679 680
	net->rtnl_put(rth);

	return r;
681 682 683 684 685 686 687
}

int __export iprule_del(uint32_t addr, int table)
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct rtmsg i;
688
		char buf[4096];
689
	} req;
690 691
	struct rtnl_handle *rth = net->rtnl_get();
	int r = 0;
692

693 694 695
	if (!rth)
		return -1;

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

698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
	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)
713
		r = -1;
714

715
	net->rtnl_put(rth);
716

717
	return r;
718
}