libnl-tiny: Generic Netlink multicast groups support
authorHauke Mehrtens <hauke.mehrtens@intel.com>
Wed, 29 Jun 2016 21:13:58 +0000 (23:13 +0200)
committerFelix Fietkau <nbd@nbd.name>
Sat, 2 Jul 2016 08:12:04 +0000 (10:12 +0200)
This adds this commit from normal libnl to libnl-tiny:
https://github.com/tgraf/libnl/commit/2dbc1ca76c5b82c40749e609eb83877418abb006

commit 2dbc1ca76c5b82c40749e609eb83877418abb006
Author: dima <dima.ky@gmail.com>
Date:   Wed Oct 13 17:53:34 2010 +0300

    Generic Netlink multicast groups support

    I have a patch against commit d378220c96c3c8b6f27dca33e7d8ba03318f9c2d
    extending libnl with a facility to receive generic netlink messages sent
    to multicast groups.

    Essentially it add one new function genl_ctrl_resolve_grp which
    prototype looks like this
    int genl_ctrl_resolve_grp(struct nl_sock *sk, const char *family_name,
            const char *grp_name)
    It resolves  the family name and the group name to group id. Then
    the returned id can be used in nl_socket_add_membership to subscribe
    to multicast messages.

    Besides that it adds two more functions

    uint32_t nl_socket_get_peer_groups(struct nl_sock *sk)
    void nl_socket_set_peer_groups(struct nl_sock *sk, uint32_t groups)

    allowing to modify the socket peer groups field. So it's possible to
    multicast messages from the user space using the legacy interface.
    Looks like there is no way (or I was not able to find one?) to modify
    the netlink socket destination group from the user space, when the
    group id is greater then 32.

Signed-off-by: Hauke Mehrtens <hauke.mehrtens@intel.com>
Signed-off-by: Felix Fietkau <nbd@nbd.name> [cosmetic style fix]
src/genl_ctrl.c
src/genl_family.c
src/include/netlink-types.h
src/include/netlink/genl/ctrl.h
src/include/netlink/genl/family.h
src/include/netlink/socket.h
src/nl.c

index 13016420d10ce6ffa72f48b2f47902c126dbfa76..0045459b2f1a68cb9840f9a3b5bddd83a5627353 100644 (file)
@@ -45,6 +45,7 @@ static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
        [CTRL_ATTR_HDRSIZE]     = { .type = NLA_U32 },
        [CTRL_ATTR_MAXATTR]     = { .type = NLA_U32 },
        [CTRL_ATTR_OPS]         = { .type = NLA_NESTED },
+       [CTRL_ATTR_MCAST_GROUPS] = { .type = NLA_NESTED },
 };
 
 static struct nla_policy family_op_policy[CTRL_ATTR_OP_MAX+1] = {
@@ -52,6 +53,11 @@ static struct nla_policy family_op_policy[CTRL_ATTR_OP_MAX+1] = {
        [CTRL_ATTR_OP_FLAGS]    = { .type = NLA_U32 },
 };
 
+static struct nla_policy family_grp_policy[CTRL_ATTR_MCAST_GRP_MAX+1] = {
+       [CTRL_ATTR_MCAST_GRP_NAME] = { .type = NLA_STRING },
+       [CTRL_ATTR_MCAST_GRP_ID]   = { .type = NLA_U32 },
+};
+
 static int ctrl_msg_parser(struct nl_cache_ops *ops, struct genl_cmd *cmd,
                           struct genl_info *info, void *arg)
 {
@@ -127,6 +133,40 @@ static int ctrl_msg_parser(struct nl_cache_ops *ops, struct genl_cmd *cmd,
                }
        }
 
+       if (info->attrs[CTRL_ATTR_MCAST_GROUPS]) {
+               struct nlattr *nla, *nla_grps;
+               int remaining;
+
+               nla_grps = info->attrs[CTRL_ATTR_MCAST_GROUPS];
+               nla_for_each_nested(nla, nla_grps, remaining) {
+                       struct nlattr *tb[CTRL_ATTR_MCAST_GRP_MAX+1];
+                       int id;
+                       const char * name;
+
+                       err = nla_parse_nested(tb, CTRL_ATTR_MCAST_GRP_MAX, nla,
+                                              family_grp_policy);
+                       if (err < 0)
+                               goto errout;
+
+                       if (tb[CTRL_ATTR_MCAST_GRP_ID] == NULL) {
+                               err = -NLE_MISSING_ATTR;
+                               goto errout;
+                       }
+                       id = nla_get_u32(tb[CTRL_ATTR_MCAST_GRP_ID]);
+
+                       if (tb[CTRL_ATTR_MCAST_GRP_NAME] == NULL) {
+                               err = -NLE_MISSING_ATTR;
+                               goto errout;
+                       }
+                       name = nla_get_string(tb[CTRL_ATTR_MCAST_GRP_NAME]);
+
+                       err = genl_family_add_grp(family, id, name);
+                       if (err < 0)
+                               goto errout;
+               }
+
+       }
+
        err = pp->pp_cb((struct nl_object *) family, pp);
 errout:
        genl_family_put(family);
@@ -242,6 +282,44 @@ errout:
        return err;
 }
 
+static int genl_ctrl_grp_by_name(const struct genl_family *family,
+                               const char *grp_name)
+{
+       struct genl_family_grp *grp;
+
+       nl_list_for_each_entry(grp, &family->gf_mc_grps, list) {
+               if (!strcmp(grp->name, grp_name)) {
+                       return grp->id;
+               }
+       }
+
+       return -NLE_OBJ_NOTFOUND;
+}
+
+int genl_ctrl_resolve_grp(struct nl_sock *sk, const char *family_name,
+       const char *grp_name)
+{
+       struct nl_cache *cache;
+       struct genl_family *family;
+       int err;
+
+       if ((err = genl_ctrl_alloc_cache(sk, &cache)) < 0)
+               return err;
+
+       family = genl_ctrl_search_by_name(cache, family_name);
+       if (family == NULL) {
+               err = -NLE_OBJ_NOTFOUND;
+               goto errout;
+       }
+
+       err = genl_ctrl_grp_by_name(family, grp_name);
+       genl_family_put(family);
+errout:
+       nl_cache_free(cache);
+
+       return err;
+}
+
 /** @} */
 
 static struct genl_cmd genl_cmds[] = {
index 88aaad9b772903e88124a164e43a2d4e4d4984b3..221acfa1a7ff9989e9634b66212034ede6aa699e 100644 (file)
@@ -31,12 +31,14 @@ static void family_constructor(struct nl_object *c)
        struct genl_family *family = (struct genl_family *) c;
 
        nl_init_list_head(&family->gf_ops);
+       nl_init_list_head(&family->gf_mc_grps);
 }
 
 static void family_free_data(struct nl_object *c)
 {
        struct genl_family *family = (struct genl_family *) c;
        struct genl_family_op *ops, *tmp;
+       struct genl_family_grp *grp, *t_grp;
 
        if (family == NULL)
                return;
@@ -45,6 +47,12 @@ static void family_free_data(struct nl_object *c)
                nl_list_del(&ops->o_list);
                free(ops);
        }
+
+       nl_list_for_each_entry_safe(grp, t_grp, &family->gf_mc_grps, list) {
+               nl_list_del(&grp->list);
+               free(grp);
+       }
+
 }
 
 static int family_clone(struct nl_object *_dst, struct nl_object *_src)
@@ -52,6 +60,7 @@ static int family_clone(struct nl_object *_dst, struct nl_object *_src)
        struct genl_family *dst = nl_object_priv(_dst);
        struct genl_family *src = nl_object_priv(_src);
        struct genl_family_op *ops;
+       struct genl_family_grp *grp;
        int err;
 
        nl_list_for_each_entry(ops, &src->gf_ops, o_list) {
@@ -59,6 +68,13 @@ static int family_clone(struct nl_object *_dst, struct nl_object *_src)
                if (err < 0)
                        return err;
        }
+
+       nl_list_for_each_entry(grp, &src->gf_mc_grps, list) {
+               err = genl_family_add_grp(dst, grp->id, grp->name);
+               if (err < 0)
+                       return err;
+       }
+
        
        return 0;
 }
@@ -119,6 +135,23 @@ int genl_family_add_op(struct genl_family *family, int id, int flags)
        return 0;
 }
 
+int genl_family_add_grp(struct genl_family *family, uint32_t id,
+                       const char *name)
+{
+       struct genl_family_grp *grp;
+
+       grp = calloc(1, sizeof(*grp));
+       if (grp == NULL)
+               return -NLE_NOMEM;
+
+       grp->id = id;
+       strncpy(grp->name, name, GENL_NAMSIZ - 1);
+
+       nl_list_add_tail(&grp->list, &family->gf_mc_grps);
+
+       return 0;
+}
+
 /** @} */
 
 /** @cond SKIP */
index 0d005935c3eed5caadaf7366aec66eb06fc2a564..65f7f6c0b858b1d6d3b108db94d8f20dba8684f6 100644 (file)
@@ -69,6 +69,13 @@ struct nl_addr
 #define GENL_OP_HAS_DOIT       2
 #define GENL_OP_HAS_DUMPIT     4
 
+struct genl_family_grp {
+       struct genl_family      *family;        /* private */
+       struct nl_list_head     list;           /* private */
+       char                    name[GENL_NAMSIZ];
+       u_int32_t               id;
+};
+
 struct genl_family_op
 {
        uint32_t                o_id;
index 1ae62f4480d6caa4e9da9a7e1980d19591e5ccd6..26a0a9967b47dfe8ab503e92369ad7712eb425e5 100644 (file)
@@ -29,6 +29,9 @@ extern struct genl_family *   genl_ctrl_search_by_name(struct nl_cache *,
                                                         const char *);
 extern int                     genl_ctrl_resolve(struct nl_sock *,
                                                  const char *);
+extern int                     genl_ctrl_resolve_grp(struct nl_sock *sk,
+                                                     const char *family,
+                                                     const char *grp);
 
 #ifdef __cplusplus
 }
index 0257cbe2734ba9e938db1e3f3d9d1427948ba1eb..b7cf48fb78044e27ed8dd792c8558a213cb6c343 100644 (file)
@@ -39,6 +39,7 @@ struct genl_family
        uint32_t                gf_maxattr;
 
        struct nl_list_head     gf_ops;
+       struct nl_list_head     gf_mc_grps;
 };
 
 
@@ -47,6 +48,9 @@ extern void                   genl_family_put(struct genl_family *);
 
 extern int                     genl_family_add_op(struct genl_family *,
                                                   int, int);
+extern int                     genl_family_add_grp(struct genl_family *,
+                                       uint32_t , const char *);
+
 
 /**
  * @name Attributes
index 9f7f4220aa5cd5f222fb52f3af449306e67988ed..bcb934c50b45b38383d8a60f503faa67167af537 100644 (file)
@@ -166,6 +166,16 @@ static inline void nl_socket_disable_msg_peek(struct nl_sock *sk)
        sk->s_flags &= ~NL_MSG_PEEK;
 }
 
+static inline uint32_t nl_socket_get_peer_groups(struct nl_sock *sk)
+{
+       return sk->s_peer.nl_groups;
+}
+
+static inline void nl_socket_set_peer_groups(struct nl_sock *sk, uint32_t groups)
+{
+       sk->s_peer.nl_groups = groups;
+}
+
 /**
  * @name Callback Handler
  * @{
index ea7d96898ba8f094dfa846e3f6a30798b316ff25..2fb866eca903910738e666e6b235a52552ce8736 100644 (file)
--- a/src/nl.c
+++ b/src/nl.c
@@ -366,7 +366,7 @@ errout:
  * Receives a netlink message, allocates a buffer in \c *buf and
  * stores the message content. The peer's netlink address is stored
  * in \c *nla. The caller is responsible for freeing the buffer allocated
- * in \c *buf if a positive value is returned.  Interruped system calls
+ * in \c *buf if a positive value is returned.  Interrupted system calls
  * are handled by repeating the read. The input buffer size is determined
  * by peeking before the actual read is done.
  *