summaryrefslogtreecommitdiff
path: root/lib/route/rtnl.c
blob: f28ebf3632166ac58f0c578dcb16d134ba24ef2c (plain)
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
/* SPDX-License-Identifier: LGPL-2.1-only */
/*
 * Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
 */

/**
 * @defgroup rtnl Routing Library (libnl-route)
 * @{
 */

#include <netlink-private/netlink.h>
#include <netlink/netlink.h>
#include <netlink/utils.h>
#include <netlink/route/rtnl.h>

/**
 * @name Sending
 * @{
 */

/**
 * Send routing netlink request message
 * @arg sk		Netlink socket.
 * @arg type		Netlink message type.
 * @arg family		Address family.
 * @arg flags		Additional netlink message flags.
 *
 * Fills out a routing netlink request message and sends it out
 * using nl_send_simple().
 *
 * @return 0 on success or a negative error code. Due to a bug in older
 * version of the library, this function returned the number of bytes sent.
 * Treat any non-negative number as success.
 */
int nl_rtgen_request(struct nl_sock *sk, int type, int family, int flags)
{
	int err;
	struct rtgenmsg gmsg = {
		.rtgen_family = family,
	};

	err = nl_send_simple(sk, type, flags, &gmsg, sizeof(gmsg));

	return err >= 0 ? 0 : err;
}

/** @} */

/**
 * @name Routing Type Translations
 * @{
 */

static const struct trans_tbl rtntypes[] = {
	__ADD(RTN_UNSPEC,unspec),
	__ADD(RTN_UNICAST,unicast),
	__ADD(RTN_LOCAL,local),
	__ADD(RTN_BROADCAST,broadcast),
	__ADD(RTN_ANYCAST,anycast),
	__ADD(RTN_MULTICAST,multicast),
	__ADD(RTN_BLACKHOLE,blackhole),
	__ADD(RTN_UNREACHABLE,unreachable),
	__ADD(RTN_PROHIBIT,prohibit),
	__ADD(RTN_THROW,throw),
	__ADD(RTN_NAT,nat),
	__ADD(RTN_XRESOLVE,xresolve),
};

char *nl_rtntype2str(int type, char *buf, size_t size)
{
	return __type2str(type, buf, size, rtntypes, ARRAY_SIZE(rtntypes));
}

int nl_str2rtntype(const char *name)
{
	return __str2type(name, rtntypes, ARRAY_SIZE(rtntypes));
}

/** @} */

/**
 * @name Scope Translations
 * @{
 */

static const struct trans_tbl scopes[] = {
	__ADD(255,nowhere),
	__ADD(254,host),
	__ADD(253,link),
	__ADD(200,site),
	__ADD(0,global),
};

char *rtnl_scope2str(int scope, char *buf, size_t size)
{
	return __type2str(scope, buf, size, scopes, ARRAY_SIZE(scopes));
}

int rtnl_str2scope(const char *name)
{
	return __str2type(name, scopes, ARRAY_SIZE(scopes));
}

/** @} */

/**
 * @name Realms Translations
 * @{
 */

char * rtnl_realms2str(uint32_t realms, char *buf, size_t len)
{
	int from = RTNL_REALM_FROM(realms);
	int to = RTNL_REALM_TO(realms);

	snprintf(buf, len, "%d/%d", from, to);

	return buf;
}

/** @} */

/** @} */