1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2003-2012 Thomas Graf <[email protected]>
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
34 static struct nl_cache_ops genl_ctrl_ops;
35
ctrl_request_update(struct nl_cache * c,struct nl_sock * h)36 static 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
42 static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
43 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
44 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_STRING,
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
53 static struct nla_policy family_op_policy[CTRL_ATTR_OP_MAX+1] = {
54 [CTRL_ATTR_OP_ID] = { .type = NLA_U32 },
55 [CTRL_ATTR_OP_FLAGS] = { .type = NLA_U32 },
56 };
57
58 static struct nla_policy family_grp_policy[CTRL_ATTR_MCAST_GRP_MAX+1] = {
59 [CTRL_ATTR_MCAST_GRP_NAME] = { .type = NLA_STRING },
60 [CTRL_ATTR_MCAST_GRP_ID] = { .type = NLA_U32 },
61 };
62
parse_mcast_grps(struct genl_family * family,struct nlattr * grp_attr)63 static 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
100 errout:
101 return err;
102 }
103
ctrl_msg_parser(struct nl_cache_ops * ops,struct genl_cmd * cmd,struct genl_info * info,void * arg)104 static 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]));
130 genl_family_set_name(family,
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);
186 errout:
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 */
probe_response(struct nl_msg * msg,void * arg)202 static 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 */
genl_ctrl_probe_by_name(struct nl_sock * sk,const char * name)236 static 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
295 out_cb_free:
296 nl_cb_put(cb);
297 out_msg_free:
298 nlmsg_free(msg);
299 out_fam_free:
300 genl_family_put(ret);
301 ret = NULL;
302 out:
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 */
genl_ctrl_alloc_cache(struct nl_sock * sk,struct nl_cache ** result)332 int 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 */
genl_ctrl_search(struct nl_cache * cache,int id)353 struct 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 */
genl_ctrl_search_by_name(struct nl_cache * cache,const char * name)386 struct 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 */
genl_ctrl_resolve(struct nl_sock * sk,const char * name)428 int 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);
441 errout:
442 return err;
443 }
444
genl_ctrl_grp_by_name(const struct genl_family * family,const char * grp_name)445 static 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 */
genl_ctrl_resolve_grp(struct nl_sock * sk,const char * family_name,const char * grp_name)470 int 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);
485 errout:
486 return err;
487 }
488
489 /** @} */
490
491 /** @cond SKIP */
492 static 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
518 static struct genl_ops genl_ops = {
519 .o_cmds = genl_cmds,
520 .o_ncmds = ARRAY_SIZE(genl_cmds),
521 };
522
523 extern 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
531 static 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
ctrl_init(void)541 static void _nl_init ctrl_init(void)
542 {
543 genl_register(&genl_ctrl_ops);
544 }
545
ctrl_exit(void)546 static void _nl_exit ctrl_exit(void)
547 {
548 genl_unregister(&genl_ctrl_ops);
549 }
550 /** @endcond */
551
552 /** @} */
553