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

/**
 * @ingroup genl
 * @defgroup genl_mngt Family and Command Registration
 *
 * Registering Generic Netlink Families and Commands
 *
 * @{
 */

#include <netlink-private/genl.h>
#include <netlink/netlink.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/mngt.h>
#include <netlink/genl/family.h>
#include <netlink/genl/ctrl.h>
#include <netlink/utils.h>

#include "netlink-private/utils.h"

/** @cond SKIP */

static NL_LIST_HEAD(genl_ops_list);

static struct genl_cmd *lookup_cmd(struct genl_ops *ops, int cmd_id)
{
	struct genl_cmd *cmd;
	int i;

	for (i = 0; i < ops->o_ncmds; i++) {
		cmd = &ops->o_cmds[i];
		if (cmd->c_id == cmd_id)
			return cmd;
	}

	return NULL;
}

static int cmd_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *nlh,
                          struct genl_ops *ops, struct nl_cache_ops *cache_ops, void *arg)
{
	_nl_auto_free struct nlattr **tb_free = NULL;
	int err;
	struct genlmsghdr *ghdr;
	struct genl_cmd *cmd;
	struct nlattr **tb;

	ghdr = genlmsg_hdr(nlh);

	if (!(cmd = lookup_cmd(ops, ghdr->cmd)))
		return -NLE_MSGTYPE_NOSUPPORT;

	if (cmd->c_msg_parser == NULL)
		return -NLE_OPNOTSUPP;

	tb = _nl_malloc_maybe_a (300, (((size_t) cmd->c_maxattr) + 1u) * sizeof (struct nlattr *), &tb_free);
	if (!tb)
		return -NLE_NOMEM;

	err = nlmsg_parse(nlh,
	                  GENL_HDRSIZE(ops->o_hdrsize),
	                  tb,
	                  cmd->c_maxattr,
	                  cmd->c_attr_policy);
	if (err < 0)
		return err;

	{
		struct genl_info info = {
			.who     = who,
			.nlh     = nlh,
			.genlhdr = ghdr,
			.userhdr = genlmsg_user_hdr(ghdr),
			.attrs   = tb,
		};

		return cmd->c_msg_parser(cache_ops, cmd, &info, arg);
	}
}

static int genl_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
			   struct nlmsghdr *nlh, struct nl_parser_param *pp)
{
	if (ops->co_genl == NULL)
		BUG();

	return cmd_msg_parser(who, nlh, ops->co_genl, ops, pp);
}

static struct genl_ops *lookup_family(int family)
{
	struct genl_ops *ops;

	nl_list_for_each_entry(ops, &genl_ops_list, o_list) {
		if (ops->o_id == family)
			return ops;
	}

	return NULL;
}

static struct genl_ops *lookup_family_by_name(const char *name)
{
	struct genl_ops *ops;

	nl_list_for_each_entry(ops, &genl_ops_list, o_list) {
		if (!strcmp(ops->o_name, name))
			return ops;
	}

	return NULL;
}

char *genl_op2name(int family, int op, char *buf, size_t len)
{
	struct genl_ops *ops;
	int i;

	if ((ops = lookup_family(family))) {
		for (i = 0; i < ops->o_ncmds; i++) {
			struct genl_cmd *cmd;
			cmd = &ops->o_cmds[i];

			if (cmd->c_id == op) {
				_nl_strncpy_trunc(buf, cmd->c_name, len);
				return buf;
			}
		}
	}

	_nl_strncpy_trunc(buf, "unknown", len);
	return NULL;
}

/** @endcond */

/**
 * @name Registration
 * @{
 */

/**
 * Register Generic Netlink family and associated commands
 * @arg ops		Generic Netlink family definition
 *
 * Registers the specified Generic Netlink family definition together with
 * all associated commands. After registration, received Generic Netlink
 * messages can be passed to genl_handle_msg() which will validate the
 * messages, look for a matching command and call the respective callback
 * function automatically.
 *
 * @note Consider using genl_register() if the family is used to implement a
 *       cacheable type.
 *
 * @see genl_unregister_family();
 * @see genl_register();
 *
 * @return 0 on success or a negative error code.
 */
int genl_register_family(struct genl_ops *ops)
{
	if (!ops->o_name)
		return -NLE_INVAL;

	if (ops->o_cmds && ops->o_ncmds <= 0)
		return -NLE_INVAL;

	if (ops->o_id && lookup_family(ops->o_id))
		return -NLE_EXIST;

	if (lookup_family_by_name(ops->o_name))
		return -NLE_EXIST;

	nl_list_add_tail(&ops->o_list, &genl_ops_list);

	return 0;
}

/**
 * Unregister Generic Netlink family
 * @arg ops		Generic Netlink family definition
 *
 * Unregisters a family and all associated commands that were previously
 * registered using genl_register_family().
 *
 * @see genl_register_family()
 *
 * @return 0 on success or a negative error code.
 */
int genl_unregister_family(struct genl_ops *ops)
{
	nl_list_del(&ops->o_list);

	return 0;
}

/**
 * Run a received message through the demultiplexer
 * @arg msg		Generic Netlink message
 * @arg arg		Argument passed on to the message handler callback
 *
 * @return 0 on success or a negative error code.
 */
int genl_handle_msg(struct nl_msg *msg, void *arg)
{
	struct nlmsghdr *nlh = nlmsg_hdr(msg);
	struct genl_ops *ops;

	if (!genlmsg_valid_hdr(nlh, 0))
		return -NLE_INVAL;

	if (!(ops = lookup_family(nlh->nlmsg_type)))
		return -NLE_MSGTYPE_NOSUPPORT;

	return cmd_msg_parser(nlmsg_get_src(msg), nlh, ops, NULL, arg);
}

/** @} */

/**
 * @name Registration of Cache Operations
 * @{
 */

/**
 * Register Generic Netlink family backed cache
 * @arg ops		Cache operations definition
 *
 * Same as genl_register_family() but additionally registers the specified
 * cache operations using nl_cache_mngt_register() and associates it with
 * the Generic Netlink family.
 *
 * @see genl_register_family()
 *
 * @return 0 on success or a negative error code.
 */
int genl_register(struct nl_cache_ops *ops)
{
	int err;

	if (ops->co_protocol != NETLINK_GENERIC) {
		err = -NLE_PROTO_MISMATCH;
		goto errout;
	}

	if (ops->co_hdrsize < GENL_HDRSIZE(0)) {
		err = -NLE_INVAL;
		goto errout;
	}

	if (ops->co_genl == NULL) {
		err = -NLE_INVAL;
		goto errout;
	}

	ops->co_genl->o_cache_ops = ops;
	ops->co_genl->o_hdrsize = ops->co_hdrsize - GENL_HDRLEN;
	ops->co_genl->o_name = ops->co_msgtypes[0].mt_name;
	ops->co_genl->o_id = ops->co_msgtypes[0].mt_id;
	ops->co_msg_parser = genl_msg_parser;

	if ((err = genl_register_family(ops->co_genl)) < 0)
		goto errout;

	err = nl_cache_mngt_register(ops);
errout:
	return err;
}

/**
 * Unregister cache based Generic Netlink family
 * @arg ops		Cache operations definition
 */
void genl_unregister(struct nl_cache_ops *ops)
{
	if (!ops)
		return;

	nl_cache_mngt_unregister(ops);

	genl_unregister_family(ops->co_genl);
}

/** @} */

/** @cond SKIP */
static int __genl_ops_resolve(struct nl_cache *ctrl, struct genl_ops *ops)
{
	struct genl_family *family;

	family = genl_ctrl_search_by_name(ctrl, ops->o_name);
	if (family != NULL) {
		ops->o_id = genl_family_get_id(family);

		if (ops->o_cache_ops)
			ops->o_cache_ops->co_msgtypes[0].mt_id = ops->o_id;

		genl_family_put(family);

		return 0;
	}

	return -NLE_OBJ_NOTFOUND;
}

int genl_resolve_id(struct genl_ops *ops)
{
	struct nl_sock *sk;
	int err = 0;

	/* Check if resolved already */
	if (ops->o_id != 0)
		return 0;

	if (!ops->o_name)
		return -NLE_INVAL;

	if (!(sk = nl_socket_alloc()))
		return -NLE_NOMEM;

	if ((err = genl_connect(sk)) < 0)
		goto errout_free;

	err = genl_ops_resolve(sk, ops);

errout_free:
	nl_socket_free(sk);

	return err;
}
/** @endcond */

/**
 * @name Resolving the name of registered families
 * @{
 */

/**
 * Resolve a single Generic Netlink family
 * @arg sk		Generic Netlink socket
 * @arg ops		Generic Netlink family definition
 *
 * Resolves the family name to its numeric identifier.
 *
 * @return 0 on success or a negative error code.
 */
int genl_ops_resolve(struct nl_sock *sk, struct genl_ops *ops)
{
	struct nl_cache *ctrl;
	int err;

	if ((err = genl_ctrl_alloc_cache(sk, &ctrl)) < 0)
		goto errout;

	err = __genl_ops_resolve(ctrl, ops);

	nl_cache_free(ctrl);
errout:
	return err;
}

/**
 * Resolve all registered Generic Netlink families
 * @arg sk		Generic Netlink socket
 *
 * Walks through all local Generic Netlink families that have been registered
 * using genl_register() and resolves the name of each family to the
 * corresponding numeric identifier.
 *
 * @see genl_register()
 * @see genl_ops_resolve()
 *
 * @return 0 on success or a negative error code.
 */
int genl_mngt_resolve(struct nl_sock *sk)
{
	struct nl_cache *ctrl;
	struct genl_ops *ops;
	int err = 0;

	if ((err = genl_ctrl_alloc_cache(sk, &ctrl)) < 0)
		goto errout;

	nl_list_for_each_entry(ops, &genl_ops_list, o_list) {
		err = __genl_ops_resolve(ctrl, ops);
	}

	nl_cache_free(ctrl);
errout:
	return err;
}

/** @} */

/** @} */