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

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

K
Kozlov Dmitry 已提交
34 35 36 37 38 39
struct arg
{
	iplink_list_func func;
	void *arg;
};

40 41 42 43 44 45 46 47 48
static pthread_key_t rth_key;
static __thread struct rtnl_handle *rth;

static void open_rth(void)
{
	rth = _malloc(sizeof(*rth));

	if (!rth)
		return;
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	memset(rth, 0, sizeof(*rth));

	if (rtnl_open(rth, 0)) {
		log_ppp_error("radius: cannot open rtnetlink\n");
		_free(rth);
		rth = NULL;
		return;
	}

	pthread_setspecific(rth_key, rth);
}

static void free_rth(void *arg)
{
	struct rtnl_handle *rth = arg;

	rtnl_close(rth);

	_free(rth);
}

K
Kozlov Dmitry 已提交
71 72 73 74
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];
75
	struct rtattr *tb2[IFLA_MAX + 1];
K
Kozlov Dmitry 已提交
76
	struct arg *a = arg;
77
	int vid = 0, iflink = 0;
K
Kozlov Dmitry 已提交
78 79 80 81 82 83 84 85 86 87 88 89

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

91 92
	if (tb[IFLA_LINKINFO]) {
		parse_rtattr_nested(tb2, IFLA_MAX, tb[IFLA_LINKINFO]);
93
		if (tb2[IFLA_INFO_KIND] && !strcmp(RTA_DATA(tb2[IFLA_INFO_KIND]), "vlan")) {
94 95 96 97 98 99 100
			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 已提交
101 102
	//printf("%i %s\n", ifi->ifi_index, RTA_DATA(tb[IFLA_IFNAME]));

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

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

136 137 138 139 140
int __export iplink_get_stats(int ifindex, struct rtnl_link_stats *stats)
{
	struct iplink_req {
		struct nlmsghdr n;
		struct ifinfomsg i;
141
		char buf[4096];
142 143 144 145 146 147 148
	} req;
	struct ifinfomsg *ifi;
	int len;
	struct rtattr *tb[IFLA_MAX + 1];

	if (!rth)
		open_rth();
149

150 151 152
	if (!rth)
		return -1;

153
	memset(&req, 0, sizeof(req) - 4096);
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
	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)
		return -1;

	if (req.n.nlmsg_type != RTM_NEWLINK)
		return -1;

	ifi = NLMSG_DATA(&req.n);

	len = req.n.nlmsg_len;

	len -= NLMSG_LENGTH(sizeof(*ifi));
	if (len < 0)
		return -1;

	parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
	if (tb[IFLA_STATS])
		memcpy(stats, RTA_DATA(tb[IFLA_STATS]), sizeof(*stats));
	else
		return -1;
180

181 182
	return 0;
}
183

D
Dmitry Kozlov 已提交
184
int __export iplink_vlan_add(const char *ifname, int ifindex, int vid)
185 186 187 188
{
	struct iplink_req {
		struct nlmsghdr n;
		struct ifinfomsg i;
189
		char buf[4096];
190 191 192 193 194
	} req;
	struct rtattr *linkinfo, *data;

	if (!rth)
		open_rth();
195

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

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

201 202 203 204
	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;
205

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

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

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

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)
		return -1;
222

223 224 225
	return 0;
}

D
Dmitry Kozlov 已提交
226
int __export iplink_vlan_del(int ifindex)
227 228 229 230
{
	struct iplink_req {
		struct nlmsghdr n;
		struct ifinfomsg i;
231
		char buf[4096];
232 233 234 235 236
	} req;
	struct rtattr *linkinfo;

	if (!rth)
		open_rth();
237

238 239 240
	if (!rth)
		return -1;

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

243 244 245 246 247
	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;
248

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

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

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

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

262 263
	return 0;
}
264

265
int __export iplink_vlan_get_vid(int ifindex, int *iflink)
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
{
	struct iplink_req {
		struct nlmsghdr n;
		struct ifinfomsg i;
		char buf[4096];
	} req;
	struct ifinfomsg *ifi;
	int len;
	struct rtattr *tb[IFLA_MAX + 1];

	if (!rth)
		open_rth();

	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)
		return -1;

	if (req.n.nlmsg_type != RTM_NEWLINK)
		return -1;

	ifi = NLMSG_DATA(&req.n);

	len = req.n.nlmsg_len;

	len -= NLMSG_LENGTH(sizeof(*ifi));
	if (len < 0)
		return -1;

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

	if (!tb[IFLA_LINKINFO])
		return 0;

309 310 311
	if (iflink && tb[IFLA_LINK])
		*iflink = *(int *)RTA_DATA(tb[IFLA_LINK]);

312 313 314 315 316 317 318 319 320 321
	parse_rtattr_nested(tb, IFLA_MAX, tb[IFLA_LINKINFO]);

	if (strcmp(RTA_DATA(tb[IFLA_INFO_KIND]), "vlan"))
		return 0;

	parse_rtattr_nested(tb, IFLA_MAX, tb[IFLA_INFO_DATA]);
	return *(uint16_t *)RTA_DATA(tb[IFLA_VLAN_ID]);
}


322 323 324 325 326
int __export ipaddr_add(int ifindex, in_addr_t addr, int mask)
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct ifaddrmsg i;
327
		char buf[4096];
328 329 330 331
	} req;

	if (!rth)
		open_rth();
332

333 334 335
	if (!rth)
		return -1;

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

338 339 340 341 342 343 344 345 346 347 348
	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)
		return -1;
349

350 351 352
	return 0;
}

353
int __export ipaddr_del(int ifindex, in_addr_t addr, int mask)
354 355 356 357
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct ifaddrmsg i;
358
		char buf[4096];
359 360 361 362
	} req;

	if (!rth)
		open_rth();
363

364 365 366
	if (!rth)
		return -1;

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

369 370 371 372 373
	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;
374
	req.i.ifa_prefixlen = mask;
375 376 377 378 379

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

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

381 382 383
	return 0;
}

384
int __export iproute_add(int ifindex, in_addr_t src, in_addr_t dst, in_addr_t gw, int proto, int mask)
385 386 387 388
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct rtmsg i;
389
		char buf[4096];
390 391 392 393
	} req;

	if (!rth)
		open_rth();
394

395 396 397
	if (!rth)
		return -1;

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

400 401 402 403 404
	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;
405
	req.i.rtm_scope = ifindex ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE;
406
	req.i.rtm_protocol = proto;
407
	req.i.rtm_type = RTN_UNICAST;
408
	req.i.rtm_dst_len = mask;
409

410 411
	if (ifindex)
		addattr32(&req.n, sizeof(req), RTA_OIF, ifindex);
K
Kozlov Dmitry 已提交
412 413
	if (src)
		addattr32(&req.n, sizeof(req), RTA_PREFSRC, src);
414 415
	if (gw)
		addattr32(&req.n, sizeof(req), RTA_GATEWAY, gw);
416 417 418 419
	addattr32(&req.n, sizeof(req), RTA_DST, dst);

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

421 422 423
	return 0;
}

424
int __export iproute_del(int ifindex, in_addr_t dst, int proto, int mask)
425 426 427 428
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct rtmsg i;
429
		char buf[4096];
430 431 432 433
	} req;

	if (!rth)
		open_rth();
434

435 436 437
	if (!rth)
		return -1;

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

440 441 442 443 444
	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;
445
	req.i.rtm_scope = ifindex ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE;
446
	req.i.rtm_protocol = proto;
447
	req.i.rtm_type = RTN_UNICAST;
448
	req.i.rtm_dst_len = mask;
449 450

	addattr32(&req.n, sizeof(req), RTA_DST, dst);
451 452 453

	if (ifindex)
		addattr32(&req.n, sizeof(req), RTA_OIF, ifindex);
454 455 456

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

458 459 460
	return 0;
}

461 462 463 464 465 466 467 468 469 470
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;

	if (!rth)
		open_rth();
471

472 473 474 475
	if (!rth)
		return -1;

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

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
	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)
		return -1;
492

493
	return 0;
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
}

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;

	if (!rth)
		open_rth();

	if (!rth)
		return -1;

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

512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
	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)
		return -1;

	return 0;
527 528
}

529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
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;

	if (!rth)
		open_rth();

	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)
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
		return -1;

	return 0;
}

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;

	if (!rth)
		open_rth();

	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)
587 588 589 590 591
		return -1;

	return 0;
}

592
in_addr_t __export iproute_get(in_addr_t dst, in_addr_t *gw)
593 594
{
	struct ipaddr_req {
595 596
		struct nlmsghdr n;
		struct rtmsg r;
597
		char buf[4096];
598 599 600 601 602
	} req;
	struct rtmsg *r;
	struct rtattr *tb[RTA_MAX+1];
	int len;
	in_addr_t res = 0;
603 604 605

	if (gw)
		*gw = 0;
606 607 608

	if (!rth)
		open_rth();
609

610 611 612
	if (!rth)
		return -1;

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

615 616 617 618 619 620 621 622 623 624 625 626
	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;

627
	addattr32(&req.n, 4096, RTA_DST, dst);
628 629 630 631 632 633 634 635

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

637 638 639 640 641 642 643 644 645 646 647 648
	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);
649

650 651
	if (tb[RTA_PREFSRC])
		res = *(uint32_t *)RTA_DATA(tb[RTA_PREFSRC]);
652 653 654 655

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

656 657 658 659 660 661 662
out:
	return res;
}

int __export iprule_add(uint32_t addr, int table)
{
	struct {
663 664
		struct nlmsghdr n;
		struct rtmsg i;
665
		char buf[4096];
666 667 668 669
	} req;

	if (!rth)
		open_rth();
670

671 672 673
	if (!rth)
		return -1;

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

676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
	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)
		return -1;
692

693 694 695 696 697 698 699 700
	return 0;
}

int __export iprule_del(uint32_t addr, int table)
{
	struct ipaddr_req {
		struct nlmsghdr n;
		struct rtmsg i;
701
		char buf[4096];
702 703 704 705
	} req;

	if (!rth)
		open_rth();
706

707 708 709
	if (!rth)
		return -1;

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

712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
	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)
		return -1;
728

729 730
	return 0;
}
731 732


733 734 735 736 737 738
static void init(void)
{
	pthread_key_create(&rth_key, free_rth);
}

DEFINE_INIT(100, init);