24 #include <netlink-private/genl.h> 25 #include <netlink/netlink.h> 26 #include <netlink/genl/genl.h> 27 #include <netlink/genl/family.h> 28 #include <netlink/genl/mngt.h> 29 #include <netlink/genl/ctrl.h> 30 #include <netlink/utils.h> 33 #define CTRL_VERSION 0x0001 35 static struct nl_cache_ops genl_ctrl_ops;
37 static int ctrl_request_update(
struct nl_cache *c,
struct nl_sock *h)
40 CTRL_VERSION, NLM_F_DUMP);
43 static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
45 [CTRL_ATTR_FAMILY_NAME] = { .type =
NLA_STRING,
46 .maxlen = GENL_NAMSIZ },
47 [CTRL_ATTR_VERSION] = { .type =
NLA_U32 },
48 [CTRL_ATTR_HDRSIZE] = { .type =
NLA_U32 },
49 [CTRL_ATTR_MAXATTR] = { .type =
NLA_U32 },
51 [CTRL_ATTR_MCAST_GROUPS] = { .type =
NLA_NESTED },
54 static struct nla_policy family_op_policy[CTRL_ATTR_OP_MAX+1] = {
56 [CTRL_ATTR_OP_FLAGS] = { .type =
NLA_U32 },
59 static struct nla_policy family_grp_policy[CTRL_ATTR_MCAST_GRP_MAX+1] = {
61 [CTRL_ATTR_MCAST_GRP_ID] = { .type =
NLA_U32 },
64 static int parse_mcast_grps(
struct genl_family *family,
struct nlattr *grp_attr)
73 struct nlattr *tb[CTRL_ATTR_MCAST_GRP_MAX+1];
82 if (tb[CTRL_ATTR_MCAST_GRP_ID] == NULL) {
83 err = -NLE_MISSING_ATTR;
88 if (tb[CTRL_ATTR_MCAST_GRP_NAME] == NULL) {
89 err = -NLE_MISSING_ATTR;
94 err = genl_family_add_grp(family,
id, name);
105 static int ctrl_msg_parser(
struct nl_cache_ops *ops,
struct genl_cmd *cmd,
108 struct genl_family *family;
109 struct nl_parser_param *pp = arg;
113 if (family == NULL) {
118 if (info->
attrs[CTRL_ATTR_FAMILY_NAME] == NULL) {
119 err = -NLE_MISSING_ATTR;
123 if (info->
attrs[CTRL_ATTR_FAMILY_ID] == NULL) {
124 err = -NLE_MISSING_ATTR;
128 family->ce_msgtype = info->
nlh->nlmsg_type;
134 if (info->
attrs[CTRL_ATTR_VERSION]) {
139 if (info->
attrs[CTRL_ATTR_HDRSIZE]) {
141 genl_family_set_hdrsize(family, hdrsize);
144 if (info->
attrs[CTRL_ATTR_MAXATTR]) {
146 genl_family_set_maxattr(family, maxattr);
149 if (info->
attrs[CTRL_ATTR_OPS]) {
150 struct nlattr *nla, *nla_ops;
153 nla_ops = info->
attrs[CTRL_ATTR_OPS];
155 struct nlattr *tb[CTRL_ATTR_OP_MAX+1];
163 if (tb[CTRL_ATTR_OP_ID] == NULL) {
164 err = -NLE_MISSING_ATTR;
170 if (tb[CTRL_ATTR_OP_FLAGS])
173 err = genl_family_add_op(family,
id, flags);
180 if (info->
attrs[CTRL_ATTR_MCAST_GROUPS]) {
181 err = parse_mcast_grps(family, info->
attrs[CTRL_ATTR_MCAST_GROUPS]);
186 err = pp->pp_cb((
struct nl_object *) family, pp);
203 static int probe_response(
struct nl_msg *msg,
void *arg)
205 struct nlattr *tb[CTRL_ATTR_MAX+1];
207 struct genl_family *ret = (
struct genl_family *)arg;
212 if (tb[CTRL_ATTR_FAMILY_ID])
215 if (tb[CTRL_ATTR_MCAST_GROUPS])
216 if (parse_mcast_grps(ret, tb[CTRL_ATTR_MCAST_GROUPS]) < 0)
237 static struct genl_family *genl_ctrl_probe_by_name(
struct nl_sock *sk,
241 struct genl_family *ret;
242 struct nl_cb *cb, *orig;
255 if (!(orig = nl_socket_get_cb(sk)))
264 0, 0, CTRL_CMD_GETFAMILY, 1)) {
286 rc = wait_for_ack(sk);
356 struct genl_family *fam;
358 if (cache->c_ops != &genl_ctrl_ops)
361 nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
362 if (fam->gf_id ==
id) {
390 struct genl_family *fam;
392 if (cache->c_ops != &genl_ctrl_ops)
395 nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
396 if (!strcmp(name, fam->gf_name)) {
431 struct genl_family *family;
434 family = genl_ctrl_probe_by_name(sk, name);
435 if (family == NULL) {
436 err = -NLE_OBJ_NOTFOUND;
446 static int genl_ctrl_grp_by_name(
const struct genl_family *family,
447 const char *grp_name)
449 struct genl_family_grp *grp;
451 nl_list_for_each_entry(grp, &family->gf_mc_grps, list) {
452 if (!strcmp(grp->name, grp_name)) {
457 return -NLE_OBJ_NOTFOUND;
472 const char *grp_name)
475 struct genl_family *family;
478 family = genl_ctrl_probe_by_name(sk, family_name);
479 if (family == NULL) {
480 err = -NLE_OBJ_NOTFOUND;
484 err = genl_ctrl_grp_by_name(family, grp_name);
493 static struct genl_cmd genl_cmds[] = {
495 .
c_id = CTRL_CMD_NEWFAMILY,
496 .c_name =
"NEWFAMILY" ,
497 .c_maxattr = CTRL_ATTR_MAX,
498 .c_attr_policy = ctrl_policy,
499 .c_msg_parser = ctrl_msg_parser,
502 .c_id = CTRL_CMD_DELFAMILY,
503 .c_name =
"DELFAMILY" ,
506 .c_id = CTRL_CMD_GETFAMILY,
507 .c_name =
"GETFAMILY" ,
510 .c_id = CTRL_CMD_NEWOPS,
514 .c_id = CTRL_CMD_DELOPS,
521 .o_ncmds = ARRAY_SIZE(genl_cmds),
524 extern struct nl_object_ops genl_family_ops;
526 static struct nl_cache_ops genl_ctrl_ops = {
527 .co_name =
"genl/family",
528 .co_hdrsize = GENL_HDRSIZE(0),
529 .co_msgtypes = GENL_FAMILY(GENL_ID_CTRL,
"nlctrl"),
530 .co_genl = &genl_ops,
531 .co_protocol = NETLINK_GENERIC,
532 .co_request_update = ctrl_request_update,
533 .co_obj_ops = &genl_family_ops,
536 static void __init ctrl_init(
void)
541 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.
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 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 nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, struct nla_policy *policy)
Create attribute index based on nested attribute.
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.
int genlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, struct nla_policy *policy)
Parse Generic Netlink message including 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.