summaryrefslogtreecommitdiff
path: root/lib/route/link/vrf.c
blob: c4edd3ef1917d03425c82abd7158c6147ea048f0 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* SPDX-License-Identifier: LGPL-2.1-only */
/*
 * Copyright (c) 2015 Cumulus Networks. All rights reserved.
 * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
 */

/**
 * @ingroup link
 * @defgroup vrf VRF
 * Virtual Routing and Forwarding link module
 *
 * @details
 * \b Link Type Name: "vrf"
 *
 * @route_doc{link_vrf, VRF Documentation}
 *
 * @{
 */

#include <netlink-private/netlink.h>
#include <netlink/netlink.h>
#include <netlink/attr.h>
#include <netlink/utils.h>
#include <netlink/object.h>
#include <netlink/route/rtnl.h>
#include <netlink-private/route/link/api.h>
#include <netlink/route/link/vrf.h>

#include <linux/if_link.h>
#include <linux-private/linux/rtnetlink.h>

#define VRF_TABLE_ID_MAX  RT_TABLE_MAX

/** @cond SKIP */
#define VRF_HAS_TABLE_ID    (1<<0)

struct vrf_info {
	uint32_t        table_id;
	uint32_t        vi_mask;
};

/** @endcond */

static struct nla_policy vrf_policy[IFLA_VRF_MAX + 1] = {
	[IFLA_VRF_TABLE]         = { .type = NLA_U32 },
};

static int vrf_alloc(struct rtnl_link *link)
{
	struct vrf_info *vi;

	if (link->l_info) {
		memset(link->l_info, 0, sizeof (*vi));
		return 0;
	}

	if ((vi = calloc(1, sizeof(*vi))) == NULL)
		return -NLE_NOMEM;

	link->l_info = vi;

	return 0;
}

static int vrf_parse(struct rtnl_link *link, struct nlattr *data,
		     struct nlattr *xstats)
{
	struct nlattr *tb[IFLA_VRF_MAX+1];
	struct vrf_info *vi;
	int err;

	NL_DBG(3, "Parsing VRF link info");

	if ((err = nla_parse_nested(tb, IFLA_VRF_MAX, data, vrf_policy)) < 0)
		goto errout;

	if ((err = vrf_alloc(link)) < 0)
		goto errout;

	vi = link->l_info;

	if (tb[IFLA_VRF_TABLE]) {
		vi->table_id = nla_get_u32(tb[IFLA_VRF_TABLE]);
		vi->vi_mask |= VRF_HAS_TABLE_ID;
	}

	err = 0;

errout:
	return err;
}

static void vrf_free(struct rtnl_link *link)
{
	free(link->l_info);
	link->l_info = NULL;
}

static int vrf_clone(struct rtnl_link *dst, struct rtnl_link *src)
{
	struct vrf_info *vdst, *vsrc = src->l_info;
	int err;

	dst->l_info = NULL;
	if ((err = rtnl_link_set_type(dst, "vrf")) < 0)
		return err;
	vdst = dst->l_info;

	BUG_ON(!vdst || !vsrc);

	memcpy(vdst, vsrc, sizeof(struct vrf_info));

	return 0;
}

static int vrf_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
{
	struct vrf_info *vi = link->l_info;
	struct nlattr *data;

	if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
		return -NLE_NOMEM;

	if (vi->vi_mask & VRF_HAS_TABLE_ID) {
		NLA_PUT_U32(msg, IFLA_VRF_TABLE, vi->table_id);
	}

	nla_nest_end(msg, data);

nla_put_failure:

	return 0;
}

static void vrf_dump(struct rtnl_link *link, struct nl_dump_params *p)
{
	struct vrf_info *vi = link->l_info;

	if (vi->vi_mask & VRF_HAS_TABLE_ID) {
		nl_dump(p, "table-id %u", vi->table_id);
	}
}

static struct rtnl_link_info_ops vrf_info_ops = {
	.io_name                = "vrf",
	.io_alloc               = vrf_alloc,
	.io_parse               = vrf_parse,
	.io_dump = {
		[NL_DUMP_LINE]     = vrf_dump,
		[NL_DUMP_DETAILS]  = vrf_dump,
	},
	.io_clone               = vrf_clone,
	.io_put_attrs           = vrf_put_attrs,
	.io_free                = vrf_free,
};

/** @cond SKIP */
#define IS_VRF_LINK_ASSERT(link) \
	if ((link)->l_info_ops != &vrf_info_ops) { \
		APPBUG("Link is not a VRF link. set type \"vrf\" first."); \
		return -NLE_OPNOTSUPP; \
	}
/** @endcond */

/**
 * @name VRF Object
 * @{
 */

/**
 * Allocate link object of type VRF
 *
 * @return Allocated link object or NULL.
 */
struct rtnl_link *rtnl_link_vrf_alloc(void)
{
	struct rtnl_link *link;

	if (!(link = rtnl_link_alloc()))
		return NULL;

	if (rtnl_link_set_type(link, "vrf") < 0) {
		rtnl_link_put(link);
		return NULL;
	}

	return link;
}

/**
 * Check if link is a VRF link
 * @arg link           Link object
 *
 * @return True if link is a VRF link, otherwise false is returned.
 */
int rtnl_link_is_vrf(struct rtnl_link *link)
{
	return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "vrf");
}

/**
 * Get VRF table id
 * @arg link           Link object
 * @arg id             Pointer to store table identifier
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_vrf_get_tableid(struct rtnl_link *link, uint32_t *id)
{
	struct vrf_info *vi = link->l_info;

	IS_VRF_LINK_ASSERT(link);
	if(!id)
		return -NLE_INVAL;

	if (vi->vi_mask & VRF_HAS_TABLE_ID)
		*id = vi->table_id;
	else
		return -NLE_AGAIN;

	return 0;
}

/**
 * Set VRF table id
 * @arg link           Link object
 * @arg id             Table identifier associated with VRF link
 *
 * @return 0 on success or a negative error code
 */
int rtnl_link_vrf_set_tableid(struct rtnl_link *link, uint32_t id)
{
	struct vrf_info *vi = link->l_info;

	IS_VRF_LINK_ASSERT(link);

	_NL_STATIC_ASSERT(VRF_TABLE_ID_MAX == UINT32_MAX);

	vi->table_id = id;
	vi->vi_mask |= VRF_HAS_TABLE_ID;

	return 0;
}

/** @} */

static void __init vrf_init(void)
{
	rtnl_link_register_info(&vrf_info_ops);
}

static void __exit vrf_exit(void)
{
	rtnl_link_unregister_info(&vrf_info_ops);
}

/** @} */