]> granicus.if.org Git - libnl/commitdiff
Generic Netlink multicast groups support
authordima <dima.ky@gmail.com>
Wed, 13 Oct 2010 14:53:34 +0000 (17:53 +0300)
committerThomas Graf <tgraf@suug.ch>
Thu, 14 Oct 2010 11:46:02 +0000 (13:46 +0200)
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.

include/linux/genetlink.h
include/netlink-types.h
include/netlink/genl/ctrl.h
include/netlink/genl/family.h
include/netlink/genl/genl.h
include/netlink/socket.h
lib/genl/ctrl.c
lib/genl/family.c
lib/nl.c
lib/socket.c

index f7a93770e1be864cd6f1ddb152784597fdac0101..b834ef6d59fa459ca793a004c311fe363e5a68fa 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef __LINUX_GENERIC_NETLINK_H
 #define __LINUX_GENERIC_NETLINK_H
 
+#include <linux/types.h>
 #include <linux/netlink.h>
 
 #define GENL_NAMSIZ    16      /* length of family name */
@@ -39,6 +40,9 @@ enum {
        CTRL_CMD_NEWOPS,
        CTRL_CMD_DELOPS,
        CTRL_CMD_GETOPS,
+       CTRL_CMD_NEWMCAST_GRP,
+       CTRL_CMD_DELMCAST_GRP,
+       CTRL_CMD_GETMCAST_GRP, /* unused */
        __CTRL_CMD_MAX,
 };
 
@@ -52,6 +56,7 @@ enum {
        CTRL_ATTR_HDRSIZE,
        CTRL_ATTR_MAXATTR,
        CTRL_ATTR_OPS,
+       CTRL_ATTR_MCAST_GROUPS,
        __CTRL_ATTR_MAX,
 };
 
@@ -66,4 +71,13 @@ enum {
 
 #define CTRL_ATTR_OP_MAX (__CTRL_ATTR_OP_MAX - 1)
 
+enum {
+       CTRL_ATTR_MCAST_GRP_UNSPEC,
+       CTRL_ATTR_MCAST_GRP_NAME,
+       CTRL_ATTR_MCAST_GRP_ID,
+       __CTRL_ATTR_MCAST_GRP_MAX,
+};
+
+#define CTRL_ATTR_MCAST_GRP_MAX (__CTRL_ATTR_MCAST_GRP_MAX - 1)
+
 #endif /* __LINUX_GENERIC_NETLINK_H */
index ff699bbecb4811c90c55ed74c2a953d8f414c0b2..1ffb70cf8cbdbd8861bc38d02215d09f8700a220 100644 (file)
@@ -702,6 +702,13 @@ struct genl_family_op
        struct nl_list_head     o_list;
 };
 
+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
 {
        NLHDR_COMMON
@@ -713,6 +720,7 @@ struct genl_family
        uint32_t                gf_maxattr;
 
        struct nl_list_head     gf_ops;
+       struct nl_list_head     gf_mc_grps;
 };
 
 union nfnl_ct_proto
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 74319e59623d15e1bc7d4b1e1df5c2dee7ff9c19..721dc13d621bd49bf6e15839c77c09efb88e9de3 100644 (file)
@@ -42,6 +42,9 @@ extern void                   genl_family_set_maxattr(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 *);
+
 
 #ifdef __cplusplus
 }
index 3f3340cfdc7b8c6cd1f1099cc2f578193e310838..364a4718c6048dbf530deb8fa1df8be43ea98a72 100644 (file)
@@ -21,7 +21,6 @@ extern "C" {
 #endif
 
 extern int             genl_connect(struct nl_sock *);
-
 extern int             genl_send_simple(struct nl_sock *, int, int,
                                         int, int);
 
index 7e71aed793feb3259d4ca6e762e866c96496b2e4..31a36d3df6ba968d43fb9e574213e853f5bb8448 100644 (file)
@@ -37,7 +37,8 @@ extern void           nl_join_groups(struct nl_sock *, int);
 extern uint32_t                nl_socket_get_peer_port(struct nl_sock *);
 extern void            nl_socket_set_peer_port(struct nl_sock *,
                                                        uint32_t);
-
+extern uint32_t        nl_socket_get_peer_groups(struct nl_sock *sk);
+extern void            nl_socket_set_peer_groups(struct nl_sock *sk, uint32_t groups);
 extern struct nl_cb *  nl_socket_get_cb(struct nl_sock *);
 extern void            nl_socket_set_cb(struct nl_sock *,
                                                 struct nl_cb *);
index 13016420d10ce6ffa72f48b2f47902c126dbfa76..387179595f731f18e84dae92699c1978525e5dfa 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)
 {
@@ -126,6 +132,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:
@@ -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 0;
+}
+
+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 4c6c18d35b4ae828467fcd07f59f3345b5744076..687f4b15beda71a2eaa4b916eb5fa071ca388e97 100644 (file)
@@ -39,12 +39,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;
@@ -53,6 +55,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)
@@ -60,6 +68,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) {
@@ -67,6 +76,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;
 }
@@ -93,6 +109,7 @@ static char *ops_flags2str(int flags, char *buf, size_t len)
 
 static void family_dump_details(struct nl_object *obj, struct nl_dump_params *p)
 {
+       struct genl_family_grp *grp;
        struct genl_family *family = (struct genl_family *) obj;
 
        family_dump_line(obj, p);
@@ -118,6 +135,11 @@ static void family_dump_details(struct nl_object *obj, struct nl_dump_params *p)
                        nl_dump(p, "\n");
                }
        }
+
+       nl_list_for_each_entry(grp, &family->gf_mc_grps, list) {
+               nl_dump_line(p, "      grp %s (0x%02x)\n", grp->name, grp->id);
+       }
+
 }
 
 static void family_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
@@ -255,6 +277,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 c453b60903b831d576d65bc3dca6c964f5d4a6f3..13c0514abade7cfa6657e72537ac2736051bcbe4 100644 (file)
--- a/lib/nl.c
+++ b/lib/nl.c
@@ -387,7 +387,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.
  *
index 8083bbb6816a40516e48a864cc46ebfaac33088e..bed53c1303bd2cd479178231ccd516c2a44d5a0b 100644 (file)
@@ -398,6 +398,18 @@ void nl_socket_set_peer_port(struct nl_sock *sk, uint32_t port)
        sk->s_peer.nl_pid = port;
 }
 
+uint32_t nl_socket_get_peer_groups(struct nl_sock *sk)
+{
+       return sk->s_peer.nl_groups;
+}
+
+void nl_socket_set_peer_groups(struct nl_sock *sk, uint32_t groups)
+{
+       sk->s_peer.nl_groups = groups;
+}
+
+
+
 /** @} */
 
 /**