Netlink 组的使用
static struct netlink_kernel_cfg my_netlink_cfg = {
.input = my_netlink_receive,
.mc_list = my_netlink_groups, // 这是一个包含组 ID 的数组
};
这里,my_netlink_groups 是一个包含组 ID 的数组,通过将套接字绑定到这些组,可以实现一对多的消息分发。
用户层代码:
#include
#include
#include
#include
#include
#include
#define NETLINK_USER 31
struct sockaddr_nl src_addr, dest_addr;
struct nlmsghdr *nlh = NULL;
struct iovec iov;
struct msghdr msg;
void handle_msg(struct nlmsghdr *nlh) {
printf("Netlink Message Header:\n");
printf(" Length: %u\n", nlh->nlmsg_len);
printf(" Type: %u\n", nlh->nlmsg_type);
printf(" Flags: 0x%X\n", nlh->nlmsg_flags);
printf(" Sequence Number: %u\n", nlh->nlmsg_seq);
printf(" PID: %u\n", nlh->nlmsg_pid);
printf("Received message: %s\n", (char *)NLMSG_DATA(nlh));
}
int main() {
int sock_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_USER);
if (sock_fd < 0) {
perror("Error creating socket");
return -1;
}
memset(&src_addr, 0, sizeof(src_addr));
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid(); // Process ID
src_addr.nl_groups = 0; // Unicast
bind(sock_fd, (struct sockaddr *)&src_addr, sizeof(src_addr));
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.nl_family = AF_NETLINK;
dest_addr.nl_pid = 0; // To kernel.如果消息是发送给另一个进程的单播消息,nl_pid是另外一个进程的pid值而nl_groups为零。
dest_addr.nl_groups = 0; // Unicast
// 分配消息缓冲区并设置消息头
nlh = malloc(NLMSG_SPACE(strlen("Hello from user space!") + 1));
strcpy(NLMSG_DATA(nlh), "Hello from user space!");
nlh->nlmsg_len = NLMSG_SPACE(strlen("Hello from user space!") + 1);
nlh->nlmsg_pid = getpid();
nlh->nlmsg_flags = 0;
iov.iov_base = (void *)nlh;
iov.iov_len = nlh->nlmsg_len;
msg.msg_name = (void *)&dest_addr;
msg.msg_namelen = sizeof(dest_addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
// Send a message to the kernel
sendmsg(sock_fd, &msg, 0);
recvmsg(sock_fd, &msg, 0);
handle_msg(nlh);
free(nlh);
close(sock_fd);
return 0;
}
驱动代码:
#include
#include
#include
#define NETLINK_USER 31
struct sock *nl_sk = NULL;
void send_to_user(const char *message, u32 pid) {
struct sk_buff *skb;
struct nlmsghdr *nlh;
int len = strlen(message) + 1;
skb = nlmsg_new(NLMSG_SPACE(len), GFP_KERNEL);
if (!skb) {
printk(KERN_ERR "Failed to allocate skb\n");
return;
}
nlh = nlmsg_put(skb, 0, 0, NLMSG_DONE, len, 0);
strncpy(nlmsg_data(nlh), message, len);
netlink_unicast(nl_sk, skb, pid, MSG_DONTWAIT);
}
void nl_recv_msg(struct sk_buff *skb) {
struct nlmsghdr *nlh = nlmsg_hdr(skb);
/*
Netlink 消息头的数据结构
struct nlmsghdr {
__u32 nlmsg_len; // 消息长度(包括头部长度)
__u16 nlmsg_type; // 消息类型
__u16 nlmsg_flags; // 消息标志
__u32 nlmsg_seq; // 序列号
__u32 nlmsg_pid; // 发送方进程的进程ID
};
*/
printk("-------------驱动收到消息-------------\n");
printk("Netlink Message Header:\n");
printk(" Length: %u\n", nlh->nlmsg_len);
printk(" Type: %u\n", nlh->nlmsg_type);
printk(" Flags: 0x%X\n", nlh->nlmsg_flags);
printk(" Sequence Number: %u\n", nlh->nlmsg_seq);
printk(" PID: %u\n", nlh->nlmsg_pid);
printk("Received message: %s\n", (char *)nlmsg_data(nlh));
int pid = nlh->nlmsg_pid; // Sending process ID
send_to_user("Hello from kernel!", pid);
}
static struct netlink_kernel_cfg cfg = {
.input = nl_recv_msg,//回调函数
};
int init_module() {
nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);
if (!nl_sk) {
printk(KERN_ALERT "Error creating socket.\n");
return -10;
}
printk(KERN_INFO "Netlink module loaded.\n");
return 0;
}
void cleanup_module() {
netlink_kernel_release(nl_sk);
printk(KERN_INFO "Netlink module unloaded.\n");
}
MODULE_LICENSE("GPL");
驱动Makefile:
KERNEL_DIR = /lib/modules/$(shell uname -r)/build
#PWD = $(shell pwd)
obj-m += netlink_socket_dirver.o
all:
make -C $(KERNEL_DIR) M=$(PWD) modules
clean:
make -C $(KERNEL_DIR) M=$(PWD) clean
驱动编译:
@ubuntu:$ make all
make -C /lib/modules/5.4.0-150-generic/build M=/netlink_socket modules
make[1]: Entering directory '/usr/src/linux-headers-5.4.0-150-generic'
CC [M] netlink_socket/netlink_socket_dirver.o
/netlink_socket/netlink_socket_dirver.c: In function ‘nl_recv_msg’:
netlink_socket/netlink_socket_dirver.c:51:5: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
int pid = nlh->nlmsg_pid; // Sending process ID
^~~
Building modules, stage 2.
MODPOST 1 modules
CC [M] netlink_socket/netlink_socket_dirver.mod.o
LD [M] netlink_socket/netlink_socket_dirver.ko
make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-150-generic'
应用层编译:
@ubuntu:netlink_socket$ gcc netlink_socket_user.c -o netlink_socket_user.o
验证执行
驱动加载:
sudo insmod netlink_socket_dirver.ko
dmesg看log:
[890093.196626] -------------驱动收到消息-------------
[890093.196627] Netlink Message Header:
[890093.196627] Length: 40
[890093.196628] Type: 0
[890093.196628] Flags: 0x0
[890093.196628] Sequence Number: 0
[890093.196628] PID: 96003
[890093.196629] Received message: Hello from user space!
应用层打印:
@ubuntu:$ sudo ./netlink_socket_user.o
Netlink Message Header:
Length: 35
Type: 3
Flags: 0x0
Sequence Number: 0
PID: 0
Received message: Hello from kernel!
成功验证
PS:
希望能抛砖引玉