首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >允许创建多少个netlink协议?

允许创建多少个netlink协议?
EN

Stack Overflow用户
提问于 2014-07-03 07:11:26
回答 1查看 146关注 0票数 0

我正在测试以下netlink示例代码(内核版本3.3.4),发现如果NETLINK_PROTOCOL设置为大于31的数字,模块插入将失败。如果NETLINK_PROTOCOL设置为1,2,3,5,17,19,21-31,模块插入将是成功的

这是否意味着只允许创建32个netlink协议?

代码语言:javascript
复制
#include <linux/module.h>
#include <net/sock.h>
#include <linux/netlink.h>
#include <linux/skbuff.h>

static int NETLINK_PROTOCOL = 31;
module_param(NETLINK_PROTOCOL, int, S_IRUGO);

struct sock *nl_sk = NULL;


static void hello_nl_recv_msg(struct sk_buff *skb) {

    struct nlmsghdr *nlh;
    int pid;
    struct sk_buff *skb_out;
    int msg_size;
    char *msg = "Hello from kernel";
    int res;

    printk(KERN_INFO "Entering: %s\n", __FUNCTION__);

    msg_size = strlen(msg);

    nlh = (struct nlmsghdr*)skb->data;

    printk( KERN_INFO "Netlink received msg payload: %s\n",
        (char*)nlmsg_data(nlh));

    pid = nlh->nlmsg_pid; /*pid of sending process */

    skb_out = nlmsg_new(msg_size,0);

    if ( !skb_out ) {

    printk(KERN_ERR "Failed to allocate new skb\n");
    return;
    }

    nlh = nlmsg_put(skb_out,0,0,NLMSG_DONE,msg_size,0);

    NETLINK_CB(skb_out).dst_group = 0; /* not in mcast group */
    strncpy(nlmsg_data(nlh),msg,msg_size);

    res = nlmsg_unicast(nl_sk,skb_out,pid);


    if (res < 0) {

    printk(KERN_INFO "Error while sending bak to user\n");
    }
}


static int __init hello_init(void)
{
    printk("Entering: %s\n", __FUNCTION__);

    nl_sk = netlink_kernel_create( &init_net,
                   NETLINK_PROTOCOL, 0,
                   hello_nl_recv_msg,
                   NULL, THIS_MODULE);
    if(!nl_sk) {

    printk(KERN_ALERT "Error creating socket.\n");
    return -10;

    }

    return 0;
}

static void __exit hello_exit(void) {

    printk(KERN_INFO "exiting hello module\n");
    netlink_kernel_release(nl_sk);

}


module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-03 07:23:33

是。虽然没有具体的文档,但是

代码语言:javascript
复制
static inline struct sock *
 netlink_kernel_create(struct net *net, int unit, struct netlink_kernel_cfg *cfg);

必须有一个< 链接的单元,从内核版本3.15到32。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24547246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档