简体   繁体   中英

How many netlink protocols are allowed to create?

I'm testing the following netlink example codes (kernel version 3.3.4) and found that module insertion would fail if NETLINK_PROTOCOL is set to a number greater than 31. module insert will be successful if NETLINK_PROTOCOL is set to 1,2,3,5,17,19,21-31

Does this mean that only 32 netlink protocols are allowed to create?

#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");

Yes. While not specifically documented, the

static inline struct sock *
 netlink_kernel_create(struct net *net, int unit, struct netlink_kernel_cfg *cfg);

Must have a unit that's < MAX_LINKS , which as of kernel version 3.15 is 32.


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM