25 #include <netlink-private/genl.h> 26 #include <netlink/netlink.h> 27 #include <netlink/genl/genl.h> 28 #include <netlink/genl/family.h> 29 #include <netlink/genl/mngt.h> 30 #include <netlink/genl/ctrl.h> 31 #include <netlink/utils.h> 34 #define CTRL_VERSION 0x0001 36 static struct nl_cache_ops genl_ctrl_ops;
38 static int ctrl_request_update(
struct nl_cache *c,
struct nl_sock *h)
41 CTRL_VERSION, NLM_F_DUMP);
44 static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
46 [CTRL_ATTR_FAMILY_NAME] = { .type =
NLA_STRING,
47 .maxlen = GENL_NAMSIZ },
48 [CTRL_ATTR_VERSION] = { .type =
NLA_U32 },
49 [CTRL_ATTR_HDRSIZE] = { .type =
NLA_U32 },
50 [CTRL_ATTR_MAXATTR] = { .type =
NLA_U32 },
52 [CTRL_ATTR_MCAST_GROUPS] = { .type =
NLA_NESTED },
55 static struct nla_policy family_op_policy[CTRL_ATTR_OP_MAX+1] = {
57 [CTRL_ATTR_OP_FLAGS] = { .type =
NLA_U32 },
60 static struct nla_policy family_grp_policy[CTRL_ATTR_MCAST_GRP_MAX+1] = {
62 [CTRL_ATTR_MCAST_GRP_ID] = { .type =
NLA_U32 },
65 static int parse_mcast_grps(
struct genl_family *family,
struct nlattr *grp_attr)
74 struct nlattr *tb[CTRL_ATTR_MCAST_GRP_MAX+1];
83 if (tb[CTRL_ATTR_MCAST_GRP_ID] == NULL) {
84 err = -NLE_MISSING_ATTR;
89 if (tb[CTRL_ATTR_MCAST_GRP_NAME] == NULL) {
90 err = -NLE_MISSING_ATTR;
95 err = genl_family_add_grp(family,
id, name);
106 static int ctrl_msg_parser(
struct nl_cache_ops *ops,
struct genl_cmd *cmd,
109 struct genl_family *family;
110 struct nl_parser_param *pp = arg;
114 if (family == NULL) {
119 if (info->
attrs[CTRL_ATTR_FAMILY_NAME] == NULL) {
120 err = -NLE_MISSING_ATTR;
124 if (info->
attrs[CTRL_ATTR_FAMILY_ID] == NULL) {
125 err = -NLE_MISSING_ATTR;
129 family->ce_msgtype = info->
nlh->nlmsg_type;
135 if (info->
attrs[CTRL_ATTR_VERSION]) {
140 if (info->
attrs[CTRL_ATTR_HDRSIZE]) {
142 genl_family_set_hdrsize(family, hdrsize);
145 if (info->
attrs[CTRL_ATTR_MAXATTR]) {
147 genl_family_set_maxattr(family, maxattr);
150 if (info->
attrs[CTRL_ATTR_OPS]) {
151 struct nlattr *nla, *nla_ops;
154 nla_ops = info->
attrs[CTRL_ATTR_OPS];
156 struct nlattr *tb[CTRL_ATTR_OP_MAX+1];
164 if (tb[CTRL_ATTR_OP_ID] == NULL) {
165 err = -NLE_MISSING_ATTR;
171 if (tb[CTRL_ATTR_OP_FLAGS])
174 err = genl_family_add_op(family,
id, flags);
181 if (info->
attrs[CTRL_ATTR_MCAST_GROUPS]) {
182 err = parse_mcast_grps(family, info->
attrs[CTRL_ATTR_MCAST_GROUPS]);
187 err = pp->pp_cb((
struct nl_object *) family, pp);
204 static int probe_response(
struct nl_msg *msg,
void *arg)
206 struct nlattr *tb[CTRL_ATTR_MAX+1];
208 struct genl_family *ret = (
struct genl_family *)arg;
213 if (tb[CTRL_ATTR_FAMILY_ID])
216 if (tb[CTRL_ATTR_MCAST_GROUPS])
217 if (parse_mcast_grps(ret, tb[CTRL_ATTR_MCAST_GROUPS]) < 0)
238 static struct genl_family *genl_ctrl_probe_by_name(
struct nl_sock *sk,
242 struct genl_family *ret;
243 struct nl_cb *cb, *orig;
256 if (!(orig = nl_socket_get_cb(sk)))
265 0, 0, CTRL_CMD_GETFAMILY, 1)) {
287 rc = wait_for_ack(sk);
357 struct genl_family *fam;
359 if (cache->c_ops != &genl_ctrl_ops)
362 nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
363 if (fam->gf_id ==
id) {
391 struct genl_family *fam;
393 if (cache->c_ops != &genl_ctrl_ops)
396 nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
397 if (!strcmp(name, fam->gf_name)) {
432 struct genl_family *family;
435 family = genl_ctrl_probe_by_name(sk, name);
436 if (family == NULL) {
437 err = -NLE_OBJ_NOTFOUND;
447 static int genl_ctrl_grp_by_name(
const struct genl_family *family,
448 const char *grp_name)
450 struct genl_family_grp *grp;
452 nl_list_for_each_entry(grp, &family->gf_mc_grps, list) {
453 if (!strcmp(grp->name, grp_name)) {
458 return -NLE_OBJ_NOTFOUND;
473 const char *grp_name)
476 struct genl_family *family;
479 family = genl_ctrl_probe_by_name(sk, family_name);
480 if (family == NULL) {
481 err = -NLE_OBJ_NOTFOUND;
485 err = genl_ctrl_grp_by_name(family, grp_name);
494 static struct genl_cmd genl_cmds[] = {
496 .
c_id = CTRL_CMD_NEWFAMILY,
497 .c_name =
"NEWFAMILY" ,
498 .c_maxattr = CTRL_ATTR_MAX,
499 .c_attr_policy = ctrl_policy,
500 .c_msg_parser = ctrl_msg_parser,
503 .c_id = CTRL_CMD_DELFAMILY,
504 .c_name =
"DELFAMILY" ,
507 .c_id = CTRL_CMD_GETFAMILY,
508 .c_name =
"GETFAMILY" ,
511 .c_id = CTRL_CMD_NEWOPS,
515 .c_id = CTRL_CMD_DELOPS,
522 .o_ncmds = ARRAY_SIZE(genl_cmds),
525 extern struct nl_object_ops genl_family_ops;
527 static struct nl_cache_ops genl_ctrl_ops = {
528 .co_name =
"genl/family",
529 .co_hdrsize = GENL_HDRSIZE(0),
530 .co_msgtypes = GENL_FAMILY(GENL_ID_CTRL,
"nlctrl"),
532 .co_protocol = NETLINK_GENERIC,
533 .co_request_update = ctrl_request_update,
534 .co_obj_ops = &genl_family_ops,
537 static void __init ctrl_init(
void)
542 static void __exit ctrl_exit(
void)
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
#define NL_AUTO_PORT
Will cause the netlink port to be set to the port assigned to the netlink icoket ust before sending t...
int genl_register(struct nl_cache_ops *ops)
Register Generic Netlink family backed cache.
int genlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
Parse Generic Netlink message including attributes.
struct genl_cmd * o_cmds
Optional array defining the available Generic Netlink commands.
Definition of a Generic Netlink command.
Customized handler specified by the user.
Attribute validation policy.
#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.
struct nl_msg * nlmsg_alloc(void)
Allocate a new netlink message with the default maximum payload size.
int genl_ctrl_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
Allocate a new controller cache.
char * nla_get_string(const struct nlattr *nla)
Return payload of string attribute.
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
void genl_family_set_version(struct genl_family *family, uint8_t version)
Set interface version.
Stop parsing altogether and discard remaining messages.
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.
void genl_family_set_name(struct genl_family *family, const char *name)
Set human readable name.
NUL terminated character string.
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.
struct nl_cb * nl_cb_clone(struct nl_cb *orig)
Clone an existing callback handle.
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.
int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
Receive a set of messages from a netlink socket.
void genl_family_set_id(struct genl_family *family, unsigned int id)
Set the numeric identifier.
int genl_ctrl_resolve_grp(struct nl_sock *sk, const char *family_name, const char *grp_name)
Resolve Generic Netlink family group name.
struct genl_family * genl_family_alloc(void)
Allocate new Generic Netlink family object.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
int c_id
Numeric command identifier (required)
struct genl_family * genl_ctrl_search(struct nl_cache *cache, int id)
Search controller cache for a numeric address match.
void genl_unregister(struct nl_cache_ops *ops)
Unregister cache based Generic Netlink family.
int genl_ctrl_resolve(struct nl_sock *sk, const char *name)
Resolve Generic Netlink family name to numeric identifier.
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
struct genl_family * genl_ctrl_search_by_name(struct nl_cache *cache, const char *name)
Search controller cache for a family name match.
Definition of a Generic Netlink family.
#define nla_for_each_nested(pos, nla, rem)
Iterate over a stream of nested attributes.
Informative structure passed on to message parser callbacks.
void genl_family_put(struct genl_family *family)
Release reference on Generic Netlink family object.
struct nlmsghdr * nlh
Pointer to Netlink message header.
uint16_t type
Type of attribute or NLA_UNSPEC.
uint16_t nla_get_u16(const struct nlattr *nla)
Return payload of 16 bit integer attribute.
unsigned int genl_family_get_id(struct genl_family *family)
Return numeric identifier.
struct nlattr ** attrs
Pointer to array of parsed attributes.
int nla_put_string(struct nl_msg *msg, int attrtype, const char *str)
Add string attribute to netlink message.
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.