linux - 如何在新的 netlink 系列中使用 sockets netlink

标签 linux linux-kernel netlink

我在将 netlink 套接字与新的 netlink 系列一起使用时遇到了一些问题……尤其是对于组。我在内核空间中使用 netlink_broadcastnlmsg_multicast 方法向用户空间发送消息。例如,我有一个带有 defs 的 header :

 #define NETLINK_MYFAMILY 20
 #define NL_MYGRP         2

和一个打开套接字并绑定(bind)到网络链接地址的进程:

int sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_MYFAMILY);
...
struct sockaddr_nl nl_addr;
memset(&nl_addr, 0, sizeof(struct sockaddr_nl));
nl_addr.nl_family = AF_NETLINK;
nl_addr.nl_pid = getpid();
nl_addr.nl_groups = NL_MYGRP;

int r = bind(sock, (struct sockaddr *)&nl_addr, sizeof(struct sockaddr_nl));
...

在内核空间中我调用:

struct sock *s = netlink_kernel_create(&init_net,
                                       NETLINK_MYFAMILY,
                                       NL_MYGRP,
                                       recv_cb,
                                       NULL, THIS_MODULE);

其中 recv_cb 是进程从用户空间发送消息时调用的回调。

现在我尝试向用户空间发送消息:

netlink_broadcast(s, skb, 0, NL_MYGRP, GFP_ATOMIC);

只有 NL_MYGRP 为 1 时,我才能正确读取进程中的消息。我无法找出问题所在...所有 netlink 系列都在 linux/netlink 中指定。 h 和数字 20 不存在,所以我认为它可以用来指定我的新家庭。怎么了?谢谢大家。

最佳答案

20 已被 NETLINK_RDMA 使用。为什么不直接使用 NETLINK_USERSOCK 作为您的 netlink 系列?

#define NETLINK_ROUTE       0   /* Routing/device hook              */
#define NETLINK_UNUSED      1   /* Unused number                */
#define NETLINK_USERSOCK    2   /* Reserved for user mode socket protocols  */
#define NETLINK_FIREWALL    3   /* Unused number, formerly ip_queue     */
#define NETLINK_SOCK_DIAG   4   /* socket monitoring                */
#define NETLINK_NFLOG       5   /* netfilter/iptables ULOG */
#define NETLINK_XFRM        6   /* ipsec */
#define NETLINK_SELINUX     7   /* SELinux event notifications */

#define NETLINK_ISCSI       8   /* Open-iSCSI */
#define NETLINK_AUDIT       9   /* auditing */
#define NETLINK_FIB_LOOKUP  10  
#define NETLINK_CONNECTOR   11
#define NETLINK_NETFILTER   12  /* netfilter subsystem */
#define NETLINK_IP6_FW      13
#define NETLINK_DNRTMSG     14  /* DECnet routing messages */
#define NETLINK_KOBJECT_UEVENT  15  /* Kernel messages to userspace */
#define NETLINK_GENERIC     16

/* leave room for NETLINK_DM (DM Events) */
#define NETLINK_SCSITRANSPORT   18  /* SCSI Transports */
#define NETLINK_ECRYPTFS    19
#define NETLINK_RDMA        20
#define NETLINK_CRYPTO      21  /* Crypto layer */

#define NETLINK_INET_DIAG   NETLINK_SOCK_DIAG

#define MAX_LINKS 32     

关于linux - 如何在新的 netlink 系列中使用 sockets netlink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9490651/

相关文章:

linux - 在不同的输出线上进行操作

linux - 如何找出交换了哪些 NL 消息

c - 允许创建多少个 netlink 协议(protocol)?

Linux netlink 多播路由更新

c++ - 静态链接整个程序

linux - 我想读取 "PUB"字符串,如果找到则读取下一个 50 个字符并追加到 excel 文件中

c - fgets() 在什么情况下会卡住?

linux - Linux内核版本之间有什么区别

基本数据类型的 copy_to_user() 和 copy_from_user()

command-line - `mknod` 的反义词是什么?