18#include "nl-default.h"
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>
28#include "nl-priv-dynamic-core/nl-core.h"
29#include "nl-priv-dynamic-core/object-api.h"
32#define CTRL_VERSION 0x0001
34static struct nl_cache_ops genl_ctrl_ops;
36static int ctrl_request_update(
struct nl_cache *c,
struct nl_sock *h)
39 CTRL_VERSION, NLM_F_DUMP);
72 struct nlattr *tb[CTRL_ATTR_MCAST_GRP_MAX+1];
81 if (tb[CTRL_ATTR_MCAST_GRP_ID] == NULL) {
82 err = -NLE_MISSING_ATTR;
87 if (tb[CTRL_ATTR_MCAST_GRP_NAME] == NULL) {
88 err = -NLE_MISSING_ATTR;
93 err = genl_family_add_grp(family,
id, name);
104static int ctrl_msg_parser(
struct nl_cache_ops *ops,
struct genl_cmd *cmd,
108 struct nl_parser_param *pp = arg;
112 if (family == NULL) {
117 if (info->
attrs[CTRL_ATTR_FAMILY_NAME] == NULL) {
118 err = -NLE_MISSING_ATTR;
122 if (info->
attrs[CTRL_ATTR_FAMILY_ID] == NULL) {
123 err = -NLE_MISSING_ATTR;
127 family->ce_msgtype = info->
nlh->nlmsg_type;
133 if (info->
attrs[CTRL_ATTR_VERSION]) {
138 if (info->
attrs[CTRL_ATTR_HDRSIZE]) {
140 genl_family_set_hdrsize(family, hdrsize);
143 if (info->
attrs[CTRL_ATTR_MAXATTR]) {
145 genl_family_set_maxattr(family, maxattr);
148 if (info->
attrs[CTRL_ATTR_OPS]) {
149 struct nlattr *nla, *nla_ops;
152 nla_ops = info->
attrs[CTRL_ATTR_OPS];
154 struct nlattr *tb[CTRL_ATTR_OP_MAX+1];
162 if (tb[CTRL_ATTR_OP_ID] == NULL) {
163 err = -NLE_MISSING_ATTR;
169 if (tb[CTRL_ATTR_OP_FLAGS])
172 err = genl_family_add_op(family,
id, flags);
179 if (info->
attrs[CTRL_ATTR_MCAST_GROUPS]) {
180 err = parse_mcast_grps(family, info->
attrs[CTRL_ATTR_MCAST_GROUPS]);
185 err = pp->pp_cb((
struct nl_object *) family, pp);
202static int probe_response(
struct nl_msg *msg,
void *arg)
204 struct nlattr *tb[CTRL_ATTR_MAX+1];
211 if (tb[CTRL_ATTR_FAMILY_ID])
214 if (tb[CTRL_ATTR_MCAST_GROUPS])
215 if (parse_mcast_grps(ret, tb[CTRL_ATTR_MCAST_GROUPS]) < 0)
236static struct genl_family *genl_ctrl_probe_by_name(
struct nl_sock *sk,
241 struct nl_cb *cb, *orig;
254 if (!(orig = nl_socket_get_cb(sk)))
263 0, 0, CTRL_CMD_GETFAMILY, 1)) {
285 rc = wait_for_ack(sk);
357 if (cache->c_ops != &genl_ctrl_ops)
360 nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
361 if (fam->gf_id ==
id) {
391 if (cache->c_ops != &genl_ctrl_ops)
394 nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
395 if (!strcmp(name, fam->gf_name)) {
433 family = genl_ctrl_probe_by_name(sk, name);
434 if (family == NULL) {
435 err = -NLE_OBJ_NOTFOUND;
445static int genl_ctrl_grp_by_name(
const struct genl_family *family,
446 const char *grp_name)
450 nl_list_for_each_entry(grp, &family->gf_mc_grps, list) {
451 if (!strcmp(grp->name, grp_name)) {
456 return -NLE_OBJ_NOTFOUND;
471 const char *grp_name)
477 family = genl_ctrl_probe_by_name(sk, family_name);
478 if (family == NULL) {
479 err = -NLE_OBJ_NOTFOUND;
483 err = genl_ctrl_grp_by_name(family, grp_name);
492static struct genl_cmd genl_cmds[] = {
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,
501 .c_id = CTRL_CMD_DELFAMILY,
502 .c_name =
"DELFAMILY" ,
505 .c_id = CTRL_CMD_GETFAMILY,
506 .c_name =
"GETFAMILY" ,
509 .c_id = CTRL_CMD_NEWOPS,
513 .c_id = CTRL_CMD_DELOPS,
520 .o_ncmds = ARRAY_SIZE(genl_cmds),
523extern struct nl_object_ops genl_family_ops;
525#define GENL_FAMILY(id, name) \
527 { id, NL_ACT_UNSPEC, name }, \
528 END_OF_MSGTYPES_LIST, \
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"),
536 .co_protocol = NETLINK_GENERIC,
537 .co_request_update = ctrl_request_update,
538 .co_obj_ops = &genl_family_ops,
541static void _nl_init ctrl_init(
void)
546static void _nl_exit ctrl_exit(
void)
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
uint16_t nla_get_u16(const struct nlattr *nla)
Return payload of 16 bit integer attribute.
int nla_put_string(struct nl_msg *msg, int attrtype, const char *str)
Add string attribute to netlink message.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
char * nla_get_string(const struct nlattr *nla)
Return payload of string attribute.
#define nla_for_each_nested(pos, nla, rem)
Iterate over a stream of nested attributes.
@ NLA_STRING
NUL terminated character string.
@ NLA_NESTED
Nested attributes.
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.
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.
struct nl_cb * nl_cb_clone(struct nl_cb *orig)
Clone an existing callback handle.
@ NL_STOP
Stop parsing altogether and discard remaining messages.
@ NL_SKIP
Skip this message.
@ NL_CB_VALID
Message is valid.
@ NL_CB_CUSTOM
Customized handler specified by the user.
int genl_ctrl_resolve(struct nl_sock *sk, const char *name)
Resolve Generic Netlink family name to numeric identifier.
int genl_ctrl_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
Allocate a new controller cache.
struct genl_family * genl_ctrl_search_by_name(struct nl_cache *cache, const char *name)
Search controller cache for a family name match.
struct genl_family * genl_ctrl_search(struct nl_cache *cache, int id)
Search controller cache for a numeric address match.
int genl_ctrl_resolve_grp(struct nl_sock *sk, const char *family_name, const char *grp_name)
Resolve Generic Netlink family group name.
void genl_family_set_name(struct genl_family *family, const char *name)
Set human readable name.
void genl_family_set_version(struct genl_family *family, uint8_t version)
Set interface version.
struct genl_family * genl_family_alloc(void)
Allocate new Generic Netlink family object.
void genl_family_put(struct genl_family *family)
Release reference on Generic Netlink family object.
unsigned int genl_family_get_id(struct genl_family *family)
Return numeric identifier.
void genl_family_set_id(struct genl_family *family, unsigned int id)
Set the numeric identifier.
int genl_register(struct nl_cache_ops *ops)
Register Generic Netlink family backed cache.
void genl_unregister(struct nl_cache_ops *ops)
Unregister cache based Generic Netlink family.
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.
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.
int genlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
Parse Generic Netlink message including attributes.
#define NL_AUTO_PORT
Will cause the netlink port to be set to the port assigned to the netlink icoket ust before sending t...
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
struct nl_msg * nlmsg_alloc(void)
Allocate a new netlink message with the default maximum payload size.
#define NL_AUTO_SEQ
May be used to refer to a sequence number which should be automatically set just before sending the m...
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
Receive a set of messages from a netlink socket.
Definition of a Generic Netlink command.
int c_id
Numeric command identifier (required)
Informative structure passed on to message parser callbacks.
struct nlmsghdr * nlh
Pointer to Netlink message header.
struct nlattr ** attrs
Pointer to array of parsed attributes.
Definition of a Generic Netlink family.
struct genl_cmd * o_cmds
Optional array defining the available Generic Netlink commands.
Attribute validation policy.
uint16_t type
Type of attribute or NLA_UNSPEC.