2

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

我得到的错误是:

绑定:没有这样的文件或目录

./a.out:无法绑定套接字(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;

}
4

2 回答 2

1

Netlink 套接字绑定对你是什么用户很敏感——如果你没有以“root”身份运行有问题的程序,至少在 RedHat 6 上,我已经看到它们确实会失败。

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

于 2015-04-01T23:21:59.507 回答
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");
于 2016-05-09T09:47:36.700 回答