Net Link Linux用户代码绑定套接字调用总是失败,因为多播组ID(非零值)

时间:2013-04-11 14:33:59

标签: linux sockets linux-kernel netlink

您正在尝试实现网络链接用户代码和内核代码,每个东西都适用于单播(src_addr.nl_groups = 0;)。对于mulicast,用户代码绑定调用总是因非零src_addr.nl_groups值而失败。真的不确定为多播提供什么价值以及如何进一步处理。我检查了内核源代码树中netlink_broadcast的用法,所以我在这里放了相同的组ID值(RTMGRP_LINK)。对于单播我在互联网上找到了很多帮助但是对于多播我不这么认为。所以请帮我进一步。

得到的错误是:

bind:没有这样的文件或目录

./ a.out:无法绑定socket(3)和错误:-1:没有这样的文件或目录

#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#define NETLINK_TEST 28
#define GROUP_IB 1
#define MAX_PAYLOAD 1024
struct sockaddr_nl src_addr, dst_addr;
struct nlmsghdr *nlh = NULL;
struct msghdr msg;
struct iovec iov;
int sock_fd;


int main(int argc, char ** argv)
{
    int err;
    sock_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_TEST);
    if (sock_fd<0) {
            char s[BUFSIZ];
            sprintf( s, "%s: can't assign fd for socket", argv[0] );
            perror(s);
            return -1;
    }

    memset(&src_addr, 0, sizeof(src_addr));
    src_addr.nl_family = AF_NETLINK;
    src_addr.nl_pid = getpid();
    src_addr.nl_groups = 0; // Unicast
    //src_addr.nl_groups = RTMGRP_LINK; /* Multicast, bind call always fails for non zero values */

    err = bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr));
    perror("bind");
    if (err<0) {
            char s[BUFSIZ];
            sprintf( s, "%s: can't bind socket (%d)and err : %d", argv[0], sock_fd,err );
            perror(s);
            return -1;
    }

    memset(&dst_addr, 0, sizeof(dst_addr));
    nlh = (struct nlhmsghdr *) malloc(NLMSG_SPACE(MAX_PAYLOAD));
    memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));

    iov.iov_base = (void *)nlh;
    iov.iov_len = NLMSG_SPACE(MAX_PAYLOAD);
    msg.msg_name = (void *)&dst_addr;
    msg.msg_namelen = sizeof(dst_addr);
    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;


    printf("pid : %d\n Waiting for messages from kernel...\n",getpid());
    recvmsg(sock_fd, &msg, 0);
    printf("Message : %s\n", NLMSG_DATA(nlh));
    close(sock_fd);

    return 0;

}

2 个答案:

答案 0 :(得分:1)

Netlink套接字绑定对用户是敏感的 - 如果你没有以“root”的形式运行有问题的程序,我已经看到它们可靠地失败了,至少在RedHat 6上是这样。

尝试以root身份运行,然后再更改逻辑。如果您在正常操作中遇到相同的故障,那么您知道它不是(必然)权限问题。

答案 1 :(得分:1)

问题是

sock_fd = socket(AF_NETLINK,SOCK_RAW,NETLINK_TEST);

你的内核模块是否定义了NETLINK_TEST系列?内核模块可能必须支持您自己的系列,并且应使用nlmsg_multicast()将消息发布到适当的组中 RTMGRP_LINK是在NETLINK_ROUTE中定义的组。

此示例代码是多播

的示例
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <sys/socket.h>
 #include <linux/netlink.h>
 #include <unistd.h>

 #define MYPROTO NETLINK_USERSOCK
 #define MYMGRP 21

 int open_netlink(void)
 {
    int sock;
    struct sockaddr_nl addr;
    int group = MYMGRP;

    sock = socket(AF_NETLINK, SOCK_RAW, MYPROTO);
    if (sock < 0) {
        printf("sock < 0.\n");
        return sock;
    }

    memset((void *) &addr, 0, sizeof(addr));
    addr.nl_family = AF_NETLINK;
    addr.nl_pid = getpid();

    /* addr.nl_groups = MYMGRP; */

    if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
        printf("bind < 0.\n");
        return -1;
    }

    if (setsockopt(sock, 270, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group)) < 0) {
        printf("setsockopt < 0\n");
        return -1;
    }

    return sock;
}

void read_event(int sock)
{
    struct sockaddr_nl nladdr;
    struct msghdr msg;
    struct iovec iov;
    char buffer[65536];
    int ret;

    iov.iov_base = (void *) buffer;
    iov.iov_len = sizeof(buffer);
    msg.msg_name = (void *) &(nladdr);
    msg.msg_namelen = sizeof(nladdr);
    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;

    printf("Ok, listening.\n");
    ret = recvmsg(sock, &msg, 0);
    if (ret < 0)
        printf("ret < 0.\n");
    else
        printf("Received message payload: %s\n", NLMSG_DATA((struct nlmsghdr *) &buffer));
}

int main(int argc, char *argv[])
{
    int nls;

    nls = open_netlink();
    if (nls < 0)
        return nls;

    while (1)
        read_event(nls);

    return 0;
}

内核模块:

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


#define MYPROTO NETLINK_USERSOCK

#define MYGRP 21

static struct sock *nl_sk = NULL;

static void send_to_user(void)
{
    struct sk_buff *skb;
    struct nlmsghdr *nlh;
    char *msg = "Hello from kernel";
    int msg_size = strlen(msg) + 1;
    int res;

    pr_info("Creating skb.\n");
    skb = nlmsg_new(NLMSG_ALIGN(msg_size + 1), GFP_KERNEL);
    if (!skb) {
        pr_err("Allocation failure.\n");
        return;
    }

    nlh = nlmsg_put(skb, 0, 1, NLMSG_DONE, msg_size + 1, 0);
    strcpy(nlmsg_data(nlh), msg);

    pr_info("Sending skb.\n");
    res = nlmsg_multicast(nl_sk, skb, 0, MYGRP, GFP_KERNEL);
    if (res < 0)
        pr_info("nlmsg_multicast() error: %d\n", res);
    else
        pr_info("Success.\n");
}

static int __init hello_init(void)
{
    pr_info("Inserting hello module.\n");

    nl_sk = netlink_kernel_create(&init_net, MYPROTO, NULL);
    if (!nl_sk) {
        pr_err("Error creating socket.\n");
        return -10;
    }

    send_to_user();

    netlink_kernel_release(nl_sk);
    return 0;
}

static void __exit hello_exit(void)
{
    pr_info("Exiting hello module.\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");