summaryrefslogtreecommitdiff
path: root/lib/route/link/api.c
blob: cd2c42bb3d80026b79f7c88d863a0b84a4db6ea0 (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
258
259
260
261
262
263
264
265
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* SPDX-License-Identifier: LGPL-2.1-only */
/*
 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
 */

/**
 * @ingroup link
 * @defgroup link_API Link Modules API
 * @brief API for modules implementing specific link types/semantics.
 *
 * @par 1) Registering/Unregistering a new link info type
 * @code
 * static struct rtnl_link_info_ops vlan_info_ops = {
 * 	.io_name		= "vlan",
 * 	.io_alloc		= vlan_alloc,
 * 	.io_parse		= vlan_parse,
 * 	.io_dump[NL_DUMP_BRIEF]	= vlan_dump_brief,
 * 	.io_dump[NL_DUMP_FULL]	= vlan_dump_full,
 * 	.io_free		= vlan_free,
 * };
 *
 * static void __init vlan_init(void)
 * {
 * 	rtnl_link_register_info(&vlan_info_ops);
 * }
 *
 * static void __exit vlan_exit(void)
 * {
 * 	rtnl_link_unregister_info(&vlan_info_ops);
 * }
 * @endcode
 *
 * @{
 */

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

static NL_LIST_HEAD(info_ops);

/* lock protecting info_ops and af_ops */
static NL_RW_LOCK(info_lock);

static struct rtnl_link_info_ops *__rtnl_link_info_ops_lookup(const char *name)
{
	struct rtnl_link_info_ops *ops;

	nl_list_for_each_entry(ops, &info_ops, io_list)
		if (!strcmp(ops->io_name, name))
			return ops;

	return NULL;
}

/**
 * @name Link Info Modules
 * @{
 */

/**
 * Return operations of a specific link info type
 * @arg name		Name of link info type.
 *
 * @note The returned pointer must be given back using rtnl_link_info_ops_put()
 *
 * @return Pointer to operations or NULL if unavailable.
 */
struct rtnl_link_info_ops *rtnl_link_info_ops_lookup(const char *name)
{
	struct rtnl_link_info_ops *ops;

	nl_write_lock(&info_lock);
	if ((ops = __rtnl_link_info_ops_lookup(name)))
		ops->io_refcnt++;
	nl_write_unlock(&info_lock);

	return ops;
}

/**
 * Take reference to a set of operations.
 * @arg ops		Link info operations.
 */
void rtnl_link_info_ops_get(struct rtnl_link_info_ops *ops)
{
	if (!ops)
		return;

	nl_write_lock(&info_lock);
	ops->io_refcnt++;
	nl_write_unlock(&info_lock);
}

/**
 * Give back reference to a set of operations.
 * @arg ops		Link info operations.
 */
void rtnl_link_info_ops_put(struct rtnl_link_info_ops *ops)
{
	if (!ops)
		return;

	nl_write_lock(&info_lock);
	_nl_assert(ops->io_refcnt > 0);
	ops->io_refcnt--;
	nl_write_unlock(&info_lock);
}

/**
 * Register operations for a link info type
 * @arg ops		Link info operations
 *
 * This function must be called by modules implementing a specific link
 * info type. It will make the operations implemented by the module
 * available for everyone else.
 *
 * @return 0 on success or a negative error code.
 * @return -NLE_INVAL Link info name not specified.
 * @return -NLE_EXIST Operations for address family already registered.
 */
int rtnl_link_register_info(struct rtnl_link_info_ops *ops)
{
	int err = 0;

	if (ops->io_name == NULL)
		return -NLE_INVAL;

	nl_write_lock(&info_lock);
	if (__rtnl_link_info_ops_lookup(ops->io_name)) {
		err = -NLE_EXIST;
		goto errout;
	}

	NL_DBG(1, "Registered link info operations %s\n", ops->io_name);

	nl_list_add_tail(&ops->io_list, &info_ops);
errout:
	nl_write_unlock(&info_lock);

	return err;
}

/**
 * Unregister operations for a link info type
 * @arg ops		Link info operations
 *
 * This function must be called if a module implementing a specific link
 * info type is unloaded or becomes unavailable. It must provide a
 * set of operations which have previously been registered using
 * rtnl_link_register_info().
 *
 * @return 0 on success or a negative error code
 * @return _NLE_OPNOTSUPP Link info operations not registered.
 * @return -NLE_BUSY Link info operations still in use.
 */
int rtnl_link_unregister_info(struct rtnl_link_info_ops *ops)
{
	struct rtnl_link_info_ops *t;
	int err = -NLE_OPNOTSUPP;

	nl_write_lock(&info_lock);

	nl_list_for_each_entry(t, &info_ops, io_list) {
		if (t == ops) {
			_nl_assert(t->io_refcnt >= 0);
			if (t->io_refcnt > 0) {
				err = -NLE_BUSY;
				goto errout;
			}

			nl_list_del(&t->io_list);

			NL_DBG(1, "Unregistered link info operations %s\n",
				ops->io_name);
			err = 0;
			goto errout;
		}
	}

errout:
	nl_write_unlock(&info_lock);

	return err;
}

/** @} */

/**
 * @name Link Address Family Modules
 * @{
 */

static struct rtnl_link_af_ops *af_ops[AF_MAX];

/**
 * Return operations of a specific link address family
 * @arg family		Address family
 *
 * @note The returned pointer must be given back using rtnl_link_af_ops_put()
 *
 * @return Pointer to operations or NULL if unavailable.
 */
struct rtnl_link_af_ops *rtnl_link_af_ops_lookup(const unsigned int family)
{
	if (family == AF_UNSPEC || family >= AF_MAX)
		return NULL;

	nl_write_lock(&info_lock);
	if (af_ops[family])
		af_ops[family]->ao_refcnt++;
	nl_write_unlock(&info_lock);

	return af_ops[family];
}

/**
 * Give back reference to a set of operations.
 * @arg ops		Address family operations.
 */
void rtnl_link_af_ops_put(struct rtnl_link_af_ops *ops)
{
	if (ops) {
		nl_write_lock(&info_lock);
		ops->ao_refcnt--;
		nl_write_unlock(&info_lock);
	}
}

/**
 * Allocate and return data buffer for link address family modules
 * @arg link		Link object
 * @arg ops		Address family operations
 *
 * This function must be called by link address family modules in all
 * cases where the API does not provide the data buffer as argument
 * already. This typically includes set functions the module provides.
 * Calling this function is strictly required to ensure proper allocation
 * of the buffer upon first use. Link objects will NOT proactively
 * allocate a data buffer for each registered link address family.
 *
 * @return Pointer to data buffer or NULL on error.
 */
void *rtnl_link_af_alloc(struct rtnl_link *link,
			 const struct rtnl_link_af_ops *ops)
{
	int family;

	if (!link || !ops)
		BUG();

	family = ops->ao_family;

	if (!link->l_af_data[family]) {
		if (!ops->ao_alloc)
			BUG();
		
		link->l_af_data[family] = ops->ao_alloc(link);
		if (!link->l_af_data[family])
			return NULL;
	}

	return link->l_af_data[family];
}

/**
 * Return data buffer for link address family modules
 * @arg link		Link object
 * @arg ops		Address family operations
 *
 * This function returns a pointer to the data buffer for the specified link
 * address family module or NULL if the buffer was not allocated yet. This
 * function is typically used by get functions of modules which are not
 * interested in having the data buffer allocated if no values have been set
 * yet.
 *
 * @return Pointer to data buffer or NULL on error.
 */
void *rtnl_link_af_data(const struct rtnl_link *link,
			const struct rtnl_link_af_ops *ops)
{
	if (!link || !ops)
		BUG();

	return link->l_af_data[ops->ao_family];
}

/**
 * Register operations for a link address family
 * @arg ops		Address family operations
 *
 * This function must be called by modules implementing a specific link
 * address family. It will make the operations implemented by the module
 * available for everyone else.
 *
 * @return 0 on success or a negative error code.
 * @return -NLE_INVAL Address family is out of range (0..AF_MAX)
 * @return -NLE_EXIST Operations for address family already registered.
 */
int rtnl_link_af_register(struct rtnl_link_af_ops *ops)
{
	int err = 0;

	if (ops->ao_family == AF_UNSPEC || ops->ao_family >= AF_MAX)
		return -NLE_INVAL;

	nl_write_lock(&info_lock);
	if (af_ops[ops->ao_family]) {
		err = -NLE_EXIST;
		goto errout;
	}

	ops->ao_refcnt = 0;
	af_ops[ops->ao_family] = ops;

	NL_DBG(1, "Registered link address family operations %u\n",
		ops->ao_family);

errout:
	nl_write_unlock(&info_lock);

	return err;
}

/**
 * Unregister operations for a link address family
 * @arg ops		Address family operations
 *
 * This function must be called if a module implementing a specific link
 * address family is unloaded or becomes unavailable. It must provide a
 * set of operations which have previously been registered using
 * rtnl_link_af_register().
 *
 * @return 0 on success or a negative error code
 * @return -NLE_INVAL ops is NULL
 * @return -NLE_OBJ_NOTFOUND Address family operations not registered.
 * @return -NLE_BUSY Address family operations still in use.
 */
int rtnl_link_af_unregister(struct rtnl_link_af_ops *ops)
{
	int err = -NLE_INVAL;

	if (!ops)
		return err;

	nl_write_lock(&info_lock);
	if (!af_ops[ops->ao_family]) {
		err = -NLE_OBJ_NOTFOUND;
		goto errout;
	}

	if (ops->ao_refcnt > 0) {
		err = -NLE_BUSY;
		goto errout;
	}

	af_ops[ops->ao_family] = NULL;

	NL_DBG(1, "Unregistered link address family operations %u\n",
		ops->ao_family);

errout:
	nl_write_unlock(&info_lock);

	return err;
}

/**
 * Compare af data for a link address family
 * @arg a		Link object a
 * @arg b		Link object b
 * @arg family		af data family
 *
 * This function will compare af_data between two links
 * a and b of family given by arg family
 *
 * @return 0 if address family specific data matches or is not present
 * or != 0 if it mismatches.
 */
int rtnl_link_af_data_compare(struct rtnl_link *a, struct rtnl_link *b,
			      int family)
{
	struct rtnl_link_af_ops *af_ops;
	int ret = 0;

	if (!a->l_af_data[family] && !b->l_af_data[family])
		return 0;

	if (!a->l_af_data[family] || !b->l_af_data[family])
		return ~0;

	af_ops = rtnl_link_af_ops_lookup(family);
	if (!af_ops)
		return ~0;

	if (af_ops->ao_compare == NULL) {
		ret = ~0;
		goto out;
	}

	ret = af_ops->ao_compare(a, b, family, ~0, 0);

out:
	rtnl_link_af_ops_put(af_ops);

	return ret;
}

/**
 * Compare link info data
 * @arg a              Link object a
 * @arg b              Link object b
 *
 * This function will compare link_info data between two links
 * a and b
 *
 * @return 0 if link_info data matches or is not present
 * or != 0 if it mismatches.
 */
int rtnl_link_info_data_compare(struct rtnl_link *a, struct rtnl_link *b, int flags)
{
	if (a->l_info_ops != b->l_info_ops)
		return ~0;

	if (!a->l_info_ops || !a->l_info_ops->io_compare)
		return 0;

	return a->l_info_ops->io_compare(a, b, flags);
}

/** @} */

/** @} */