libnl 3.10.0
ctrl.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
4 */
5
6/**
7 * @ingroup genl
8 * @defgroup genl_ctrl Controller (Resolver)
9 *
10 * Resolves Generic Netlink family names to numeric identifiers.
11 *
12 * The controller is a component in the kernel that resolves Generic Netlink
13 * family names to their numeric identifiers. This module provides functions
14 * to query the controller to access the resolving functionality.
15 * @{
16 */
17
18#include "nl-default.h"
19
20#include <netlink/netlink.h>
21#include <netlink/genl/genl.h>
22#include <netlink/genl/family.h>
23#include <netlink/genl/mngt.h>
24#include <netlink/genl/ctrl.h>
25#include <netlink/utils.h>
26
27#include "nl-genl.h"
28#include "nl-priv-dynamic-core/nl-core.h"
29#include "nl-priv-dynamic-core/object-api.h"
30
31/** @cond SKIP */
32#define CTRL_VERSION 0x0001
33
34static struct nl_cache_ops genl_ctrl_ops;
35
36static int ctrl_request_update(struct nl_cache *c, struct nl_sock *h)
37{
38 return genl_send_simple(h, GENL_ID_CTRL, CTRL_CMD_GETFAMILY,
39 CTRL_VERSION, NLM_F_DUMP);
40}
41
42static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
45 .maxlen = GENL_NAMSIZ },
46 [CTRL_ATTR_VERSION] = { .type = NLA_U32 },
47 [CTRL_ATTR_HDRSIZE] = { .type = NLA_U32 },
48 [CTRL_ATTR_MAXATTR] = { .type = NLA_U32 },
49 [CTRL_ATTR_OPS] = { .type = NLA_NESTED },
50 [CTRL_ATTR_MCAST_GROUPS] = { .type = NLA_NESTED },
51};
52
54 [CTRL_ATTR_OP_ID] = { .type = NLA_U32 },
55 [CTRL_ATTR_OP_FLAGS] = { .type = NLA_U32 },
56};
57
60 [CTRL_ATTR_MCAST_GRP_ID] = { .type = NLA_U32 },
61};
62
63static int parse_mcast_grps(struct genl_family *family, struct nlattr *grp_attr)
64{
65 struct nlattr *nla;
66 int remaining, err;
67
68 if (!grp_attr)
69 BUG();
70
71 nla_for_each_nested(nla, grp_attr, remaining) {
72 struct nlattr *tb[CTRL_ATTR_MCAST_GRP_MAX+1];
73 int id;
74 const char * name;
75
76 err = nla_parse_nested(tb, CTRL_ATTR_MCAST_GRP_MAX, nla,
77 family_grp_policy);
78 if (err < 0)
79 goto errout;
80
81 if (tb[CTRL_ATTR_MCAST_GRP_ID] == NULL) {
82 err = -NLE_MISSING_ATTR;
83 goto errout;
84 }
85 id = nla_get_u32(tb[CTRL_ATTR_MCAST_GRP_ID]);
86
87 if (tb[CTRL_ATTR_MCAST_GRP_NAME] == NULL) {
88 err = -NLE_MISSING_ATTR;
89 goto errout;
90 }
91 name = nla_get_string(tb[CTRL_ATTR_MCAST_GRP_NAME]);
92
93 err = genl_family_add_grp(family, id, name);
94 if (err < 0)
95 goto errout;
96 }
97
98 err = 0;
99
100errout:
101 return err;
102}
103
104static int ctrl_msg_parser(struct nl_cache_ops *ops, struct genl_cmd *cmd,
105 struct genl_info *info, void *arg)
106{
107 struct genl_family *family;
108 struct nl_parser_param *pp = arg;
109 int err;
110
111 family = genl_family_alloc();
112 if (family == NULL) {
113 err = -NLE_NOMEM;
114 goto errout;
115 }
116
117 if (info->attrs[CTRL_ATTR_FAMILY_NAME] == NULL) {
118 err = -NLE_MISSING_ATTR;
119 goto errout;
120 }
121
122 if (info->attrs[CTRL_ATTR_FAMILY_ID] == NULL) {
123 err = -NLE_MISSING_ATTR;
124 goto errout;
125 }
126
127 family->ce_msgtype = info->nlh->nlmsg_type;
128 genl_family_set_id(family,
129 nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]));
131 nla_get_string(info->attrs[CTRL_ATTR_FAMILY_NAME]));
132
133 if (info->attrs[CTRL_ATTR_VERSION]) {
134 uint32_t version = nla_get_u32(info->attrs[CTRL_ATTR_VERSION]);
135 genl_family_set_version(family, version);
136 }
137
138 if (info->attrs[CTRL_ATTR_HDRSIZE]) {
139 uint32_t hdrsize = nla_get_u32(info->attrs[CTRL_ATTR_HDRSIZE]);
140 genl_family_set_hdrsize(family, hdrsize);
141 }
142
143 if (info->attrs[CTRL_ATTR_MAXATTR]) {
144 uint32_t maxattr = nla_get_u32(info->attrs[CTRL_ATTR_MAXATTR]);
145 genl_family_set_maxattr(family, maxattr);
146 }
147
148 if (info->attrs[CTRL_ATTR_OPS]) {
149 struct nlattr *nla, *nla_ops;
150 int remaining;
151
152 nla_ops = info->attrs[CTRL_ATTR_OPS];
153 nla_for_each_nested(nla, nla_ops, remaining) {
154 struct nlattr *tb[CTRL_ATTR_OP_MAX+1];
155 int flags = 0, id;
156
157 err = nla_parse_nested(tb, CTRL_ATTR_OP_MAX, nla,
158 family_op_policy);
159 if (err < 0)
160 goto errout;
161
162 if (tb[CTRL_ATTR_OP_ID] == NULL) {
163 err = -NLE_MISSING_ATTR;
164 goto errout;
165 }
166
167 id = nla_get_u32(tb[CTRL_ATTR_OP_ID]);
168
169 if (tb[CTRL_ATTR_OP_FLAGS])
170 flags = nla_get_u32(tb[CTRL_ATTR_OP_FLAGS]);
171
172 err = genl_family_add_op(family, id, flags);
173 if (err < 0)
174 goto errout;
175
176 }
177 }
178
179 if (info->attrs[CTRL_ATTR_MCAST_GROUPS]) {
180 err = parse_mcast_grps(family, info->attrs[CTRL_ATTR_MCAST_GROUPS]);
181 if (err < 0)
182 goto errout;
183 }
184
185 err = pp->pp_cb((struct nl_object *) family, pp);
186errout:
187 genl_family_put(family);
188 return err;
189}
190
191/**
192 * process responses from from the query sent by genl_ctrl_probe_by_name
193 * @arg nl_msg Returned message.
194 * @arg name genl_family structure to fill out.
195 *
196 * Process returned messages, filling out the missing informatino in the
197 * genl_family structure
198 *
199 * @return Indicator to keep processing frames or not
200 *
201 */
202static int probe_response(struct nl_msg *msg, void *arg)
203{
204 struct nlattr *tb[CTRL_ATTR_MAX+1];
205 struct nlmsghdr *nlh = nlmsg_hdr(msg);
206 struct genl_family *ret = (struct genl_family *)arg;
207
208 if (genlmsg_parse(nlh, 0, tb, CTRL_ATTR_MAX, ctrl_policy))
209 return NL_SKIP;
210
211 if (tb[CTRL_ATTR_FAMILY_ID])
212 genl_family_set_id(ret, nla_get_u16(tb[CTRL_ATTR_FAMILY_ID]));
213
214 if (tb[CTRL_ATTR_MCAST_GROUPS])
215 if (parse_mcast_grps(ret, tb[CTRL_ATTR_MCAST_GROUPS]) < 0)
216 return NL_SKIP;
217
218 return NL_STOP;
219}
220
221/**
222 * Look up generic netlink family by family name querying the kernel directly
223 * @arg sk Socket.
224 * @arg name Family name.
225 *
226 * Directly query's the kernel for a given family name. The caller will own a
227 * reference on the returned object which needsd to be given back after usage
228 * using genl_family_put.
229 *
230 * Note: This API call differs from genl_ctrl_search_by_name in that it querys
231 * the kernel directly, alowing for module autoload to take place to resolve the
232 * family request. Using an nl_cache prevents that operation
233 *
234 * @return Generic netlink family object or NULL if no match was found.
235 */
236static struct genl_family *genl_ctrl_probe_by_name(struct nl_sock *sk,
237 const char *name)
238{
239 struct nl_msg *msg;
240 struct genl_family *ret;
241 struct nl_cb *cb, *orig;
242 int rc;
243
244 ret = genl_family_alloc();
245 if (!ret)
246 goto out;
247
248 genl_family_set_name(ret, name);
249
250 msg = nlmsg_alloc();
251 if (!msg)
252 goto out_fam_free;
253
254 if (!(orig = nl_socket_get_cb(sk)))
255 goto out_msg_free;
256
257 cb = nl_cb_clone(orig);
258 nl_cb_put(orig);
259 if (!cb)
260 goto out_msg_free;
261
262 if (!genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, GENL_ID_CTRL,
263 0, 0, CTRL_CMD_GETFAMILY, 1)) {
264 BUG();
265 goto out_cb_free;
266 }
267
268 if (nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, name) < 0)
269 goto out_cb_free;
270
271 rc = nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, probe_response,
272 (void *) ret);
273 if (rc < 0)
274 goto out_cb_free;
275
276 rc = nl_send_auto_complete(sk, msg);
277 if (rc < 0)
278 goto out_cb_free;
279
280 rc = nl_recvmsgs(sk, cb);
281 if (rc < 0)
282 goto out_cb_free;
283
284 /* If search was successful, request may be ACKed after data */
285 rc = wait_for_ack(sk);
286 if (rc < 0)
287 goto out_cb_free;
288
289 if (genl_family_get_id(ret) != 0) {
290 nlmsg_free(msg);
291 nl_cb_put(cb);
292 return ret;
293 }
294
295out_cb_free:
296 nl_cb_put(cb);
297out_msg_free:
298 nlmsg_free(msg);
299out_fam_free:
300 genl_family_put(ret);
301 ret = NULL;
302out:
303 return ret;
304}
305
306
307/** @endcond */
308
309/**
310 * @name Controller Cache
311 *
312 * The controller cache allows to keep a local copy of the list of all
313 * kernel side registered Generic Netlink families to quickly resolve
314 * multiple Generic Netlink family names without requiring to communicate
315 * with the kernel for each resolving iteration.
316 *
317 * @{
318 */
319
320/**
321 * Allocate a new controller cache
322 * @arg sk Generic Netlink socket
323 * @arg result Pointer to store resulting cache
324 *
325 * Allocates a new cache mirroring the state of the controller and stores it
326 * in \c *result. The allocated cache will contain a list of all currently
327 * registered kernel side Generic Netlink families. The cache is meant to be
328 * used to resolve family names locally.
329 *
330 * @return 0 on success or a negative error code.
331 */
332int genl_ctrl_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
333{
334 return nl_cache_alloc_and_fill(&genl_ctrl_ops, sk, result);
335}
336
337/**
338 * Search controller cache for a numeric address match
339 * @arg cache Controller cache
340 * @arg id Numeric family identifier.
341 *
342 * Searches a previously allocated controller cache and looks for an entry
343 * that matches the specified numeric family identifier \c id. If a match
344 * is found successfully, the reference count of the matching object is
345 * increased by one before the objet is returned.
346 *
347 * @see genl_ctrl_alloc_cache()
348 * @see genl_ctrl_search_by_name()
349 * @see genl_family_put()
350 *
351 * @return Generic Netlink family object or NULL if no match was found.
352 */
353struct genl_family *genl_ctrl_search(struct nl_cache *cache, int id)
354{
355 struct genl_family *fam;
356
357 if (cache->c_ops != &genl_ctrl_ops)
358 BUG();
359
360 nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
361 if (fam->gf_id == id) {
362 nl_object_get((struct nl_object *) fam);
363 return fam;
364 }
365 }
366
367 return NULL;
368}
369
370/**
371 * Search controller cache for a family name match
372 * @arg cache Controller cache
373 * @arg name Name of Generic Netlink family
374 *
375 * Searches a previously allocated controller cache and looks for an entry
376 * that matches the specified family \c name. If a match is found successfully,
377 * the reference count of the matching object is increased by one before the
378 * objet is returned.
379 *
380 * @see genl_ctrl_alloc_cache()
381 * @see genl_ctrl_search()
382 * @see genl_family_put()
383 *
384 * @return Generic Netlink family object or NULL if no match was found.
385 */
386struct genl_family *genl_ctrl_search_by_name(struct nl_cache *cache,
387 const char *name)
388{
389 struct genl_family *fam;
390
391 if (cache->c_ops != &genl_ctrl_ops)
392 BUG();
393
394 nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
395 if (!strcmp(name, fam->gf_name)) {
396 nl_object_get((struct nl_object *) fam);
397 return fam;
398 }
399 }
400
401 return NULL;
402}
403
404/** @} */
405
406/**
407 * @name Direct Resolvers
408 *
409 * These functions communicate directly with the kernel and do not require
410 * a cache to be kept up to date.
411 *
412 * @{
413 */
414
415/**
416 * Resolve Generic Netlink family name to numeric identifier
417 * @arg sk Generic Netlink socket.
418 * @arg name Name of Generic Netlink family
419 *
420 * Resolves the Generic Netlink family name to the corresponding numeric
421 * family identifier. This function queries the kernel directly, use
422 * genl_ctrl_search_by_name() if you need to resolve multiple names.
423 *
424 * @see genl_ctrl_search_by_name()
425 *
426 * @return The numeric family identifier or a negative error code.
427 */
428int genl_ctrl_resolve(struct nl_sock *sk, const char *name)
429{
430 struct genl_family *family;
431 int err;
432
433 family = genl_ctrl_probe_by_name(sk, name);
434 if (family == NULL) {
435 err = -NLE_OBJ_NOTFOUND;
436 goto errout;
437 }
438
439 err = genl_family_get_id(family);
440 genl_family_put(family);
441errout:
442 return err;
443}
444
445static int genl_ctrl_grp_by_name(const struct genl_family *family,
446 const char *grp_name)
447{
448 struct genl_family_grp *grp;
449
450 nl_list_for_each_entry(grp, &family->gf_mc_grps, list) {
451 if (!strcmp(grp->name, grp_name)) {
452 return grp->id;
453 }
454 }
455
456 return -NLE_OBJ_NOTFOUND;
457}
458
459/**
460 * Resolve Generic Netlink family group name
461 * @arg sk Generic Netlink socket
462 * @arg family_name Name of Generic Netlink family
463 * @arg grp_name Name of group to resolve
464 *
465 * Looks up the family object and resolves the group name to the numeric
466 * group identifier.
467 *
468 * @return Numeric group identifier or a negative error code.
469 */
470int genl_ctrl_resolve_grp(struct nl_sock *sk, const char *family_name,
471 const char *grp_name)
472{
473
474 struct genl_family *family;
475 int err;
476
477 family = genl_ctrl_probe_by_name(sk, family_name);
478 if (family == NULL) {
479 err = -NLE_OBJ_NOTFOUND;
480 goto errout;
481 }
482
483 err = genl_ctrl_grp_by_name(family, grp_name);
484 genl_family_put(family);
485errout:
486 return err;
487}
488
489/** @} */
490
491/** @cond SKIP */
492static struct genl_cmd genl_cmds[] = {
493 {
494 .c_id = CTRL_CMD_NEWFAMILY,
495 .c_name = "NEWFAMILY" ,
496 .c_maxattr = CTRL_ATTR_MAX,
497 .c_attr_policy = ctrl_policy,
498 .c_msg_parser = ctrl_msg_parser,
499 },
500 {
501 .c_id = CTRL_CMD_DELFAMILY,
502 .c_name = "DELFAMILY" ,
503 },
504 {
505 .c_id = CTRL_CMD_GETFAMILY,
506 .c_name = "GETFAMILY" ,
507 },
508 {
509 .c_id = CTRL_CMD_NEWOPS,
510 .c_name = "NEWOPS" ,
511 },
512 {
513 .c_id = CTRL_CMD_DELOPS,
514 .c_name = "DELOPS" ,
515 },
516};
517
518static struct genl_ops genl_ops = {
519 .o_cmds = genl_cmds,
520 .o_ncmds = ARRAY_SIZE(genl_cmds),
521};
522
523extern struct nl_object_ops genl_family_ops;
524
525#define GENL_FAMILY(id, name) \
526 { \
527 { id, NL_ACT_UNSPEC, name }, \
528 END_OF_MSGTYPES_LIST, \
529 }
530
531static struct nl_cache_ops genl_ctrl_ops = {
532 .co_name = "genl/family",
533 .co_hdrsize = GENL_HDRSIZE(0),
534 .co_msgtypes = GENL_FAMILY(GENL_ID_CTRL, "nlctrl"),
535 .co_genl = &genl_ops,
536 .co_protocol = NETLINK_GENERIC,
537 .co_request_update = ctrl_request_update,
538 .co_obj_ops = &genl_family_ops,
539};
540
541static void _nl_init ctrl_init(void)
542{
543 genl_register(&genl_ctrl_ops);
544}
545
546static void _nl_exit ctrl_exit(void)
547{
548 genl_unregister(&genl_ctrl_ops);
549}
550/** @endcond */
551
552/** @} */
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
Definition attr.c:712
uint16_t nla_get_u16(const struct nlattr *nla)
Return payload of 16 bit integer attribute.
Definition attr.c:662
int nla_put_string(struct nl_msg *msg, int attrtype, const char *str)
Add string attribute to netlink message.
Definition attr.c:792
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
Definition attr.c:1035
char * nla_get_string(const struct nlattr *nla)
Return payload of string attribute.
Definition attr.c:803
#define nla_for_each_nested(pos, nla, rem)
Iterate over a stream of nested attributes.
Definition attr.h:324
@ NLA_STRING
NUL terminated character string.
Definition attr.h:39
@ NLA_U16
16 bit integer
Definition attr.h:36
@ NLA_NESTED
Nested attributes.
Definition attr.h:42
@ NLA_U32
32 bit integer
Definition attr.h:37
int nl_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock, struct nl_cache **result)
Allocate new cache and fill it.
Definition cache.c:234
int nl_cb_set(struct nl_cb *cb, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
Set up a callback.
Definition handlers.c:290
struct nl_cb * nl_cb_clone(struct nl_cb *orig)
Clone an existing callback handle.
Definition handlers.c:227
@ NL_STOP
Stop parsing altogether and discard remaining messages.
Definition handlers.h:62
@ NL_SKIP
Skip this message.
Definition handlers.h:60
@ NL_CB_VALID
Message is valid.
Definition handlers.h:89
@ NL_CB_CUSTOM
Customized handler specified by the user.
Definition handlers.h:77
int genl_ctrl_resolve(struct nl_sock *sk, const char *name)
Resolve Generic Netlink family name to numeric identifier.
Definition ctrl.c:428
int genl_ctrl_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
Allocate a new controller cache.
Definition ctrl.c:332
struct genl_family * genl_ctrl_search_by_name(struct nl_cache *cache, const char *name)
Search controller cache for a family name match.
Definition ctrl.c:386
struct genl_family * genl_ctrl_search(struct nl_cache *cache, int id)
Search controller cache for a numeric address match.
Definition ctrl.c:353
int genl_ctrl_resolve_grp(struct nl_sock *sk, const char *family_name, const char *grp_name)
Resolve Generic Netlink family group name.
Definition ctrl.c:470
void genl_family_set_name(struct genl_family *family, const char *name)
Set human readable name.
Definition family.c:266
void genl_family_set_version(struct genl_family *family, uint8_t version)
Set interface version.
Definition family.c:296
struct genl_family * genl_family_alloc(void)
Allocate new Generic Netlink family object.
Definition family.c:189
void genl_family_put(struct genl_family *family)
Release reference on Generic Netlink family object.
Definition family.c:203
unsigned int genl_family_get_id(struct genl_family *family)
Return numeric identifier.
Definition family.c:221
void genl_family_set_id(struct genl_family *family, unsigned int id)
Set the numeric identifier.
Definition family.c:234
int genl_register(struct nl_cache_ops *ops)
Register Generic Netlink family backed cache.
Definition mngt.c:242
void genl_unregister(struct nl_cache_ops *ops)
Unregister cache based Generic Netlink family.
Definition mngt.c:279
void * genlmsg_put(struct nl_msg *msg, uint32_t port, uint32_t seq, int family, int hdrlen, int flags, uint8_t cmd, uint8_t version)
Add Generic Netlink headers to Netlink message.
Definition genl.c:349
int genl_send_simple(struct nl_sock *sk, int family, int cmd, int version, int flags)
Send a Generic Netlink message consisting only of a header.
Definition genl.c:83
int genlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
Parse Generic Netlink message including attributes.
Definition genl.c:192
#define NL_AUTO_PORT
Will cause the netlink port to be set to the port assigned to the netlink icoket ust before sending t...
Definition msg.h:29
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
Definition msg.c:550
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
Definition msg.c:572
struct nl_msg * nlmsg_alloc(void)
Allocate a new netlink message with the default maximum payload size.
Definition msg.c:305
#define NL_AUTO_SEQ
May be used to refer to a sequence number which should be automatically set just before sending the m...
Definition msg.h:40
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
Definition object.c:210
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Definition nl.c:1247
int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
Receive a set of messages from a netlink socket.
Definition nl.c:1077
Definition of a Generic Netlink command.
Definition mngt.h:82
int c_id
Numeric command identifier (required)
Definition mngt.h:84
Informative structure passed on to message parser callbacks.
Definition mngt.h:32
struct nlmsghdr * nlh
Pointer to Netlink message header.
Definition mngt.h:37
struct nlattr ** attrs
Pointer to array of parsed attributes.
Definition mngt.h:46
Definition of a Generic Netlink family.
Definition mngt.h:127
struct genl_cmd * o_cmds
Optional array defining the available Generic Netlink commands.
Definition mngt.h:144
Attribute validation policy.
Definition attr.h:63
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition attr.h:65