Re: Generic Netlink

From: Edouard Thuleau
Date: Mon Nov 12 2007 - 11:59:14 EST


Oops, I forget it :

#include <linux/kernel.h>
#include <linux/module.h>
#include <net/sock.h>
#include <linux/netlink.h>
#include <linux/version.h>

#include <linux/rtnetlink.h>
#include <net/genetlink.h>

enum {
DOC_EXMPL_C_UNSPEC = 0,
DOC_EXMPL_C_ECHO,
__DOC_EXMPL_C_MAX,
};
#define DOC_EXMPL_C_MAX (__DOC_EXMPL_C_MAX - 1)

enum {
DOC_EXMPL_A_UNSPEC = 0,
DOC_EXMPL_A_MSG,
__DOC_EXMPL_A_MAX,
};
#define DOC_EXMPL_A_MAX (__DOC_EXMPL_A_MAX - 1)

static struct genl_family doc_exmpl_gnl_family = {
.id = GENL_ID_GENERATE,
.name = "DOC_EXMPL",
.version = 1,
.maxattr = DOC_EXMPL_A_MAX,
};

static struct nla_policy doc_exmpl_genl_policy[DOC_EXMPL_A_MAX+1]
__read_mostly = {
[DOC_EXMPL_A_MSG] = { .type = NLA_NUL_STRING },
};

static int doc_exmpl_echo(struct sk_buff *skb, struct genl_info *info)
{
return 0;
}

static struct genl_ops doc_exmpl_gnl_ops_echo = {
.cmd = DOC_EXMPL_C_ECHO,
.policy = doc_exmpl_genl_policy,
.doit = doc_exmpl_echo,
};

int send(void) {
struct sk_buff *skb;
int rc = 0;
void *msg_head;

skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
if (skb == NULL) {
printk("error on genlmsg_new\n");
return -ENOMEM;
}

/* create the message headers */
msg_head = genlmsg_put(skb, 0, 0, &doc_exmpl_gnl_family, 0, DOC_EXMPL_C_ECHO);
if (msg_head == NULL) {
printk("error on genlmsg_put\n");
rc = -ENOMEM;
goto failure;
}

/* add a DOC_EXMPL_A_MSG attribute */
rc = nla_put_string(skb, DOC_EXMPL_A_MSG, "Generic Netlink Rocks");
if (rc != 0) {
printk("error on nla_put_string\n");
goto failure;
}

/* finalize the message */
genlmsg_end(skb, msg_head);

rc = genlmsg_multicast(skb, 0, 1, 0);
if (rc != 0) {
printk("error on genlmsg_multicast %i\n", rc);
goto failure;
}

return rc;

failure:
printk("Failure\n");
nlmsg_free(skb);
genl_unregister_family(&doc_exmpl_gnl_family);
return rc;

}

int init_mod(void) {
int err = 0;

err = genl_register_family(&doc_exmpl_gnl_family);
if (err < 0) {
printk("Error register family\n");
return err;
}

printk("Family id : %i\n", doc_exmpl_gnl_family.id);

if((err = genl_register_ops(&doc_exmpl_gnl_family,
&doc_exmpl_gnl_ops_echo)) < 0) {
genl_unregister_family(&doc_exmpl_gnl_family);
printk("error register ops \n");
return err;
}

return send();
}

static void __exit exit_mod(void) {
genl_unregister_family(&doc_exmpl_gnl_family);

printk(KERN_INFO "Goodbye\n");
}

module_init(init_mod);
module_exit(exit_mod);

MODULE_LICENSE("GPL");


On Nov 12, 2007 5:53 PM, Charlie Brady <charlieb@xxxxxxxxxxxxxxxxxx> wrote:
>
> On Mon, 12 Nov 2007, Edouard Thuleau wrote:
>
> > I tried to reproduce the example of the doc (look in the attached
> > file) but it doesn't work.
>
> There is no attached file.
>
-
To unsubscribe from this list: send the line "unsubscribe linux-net" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html