本文共 3816 字,大约阅读时间需要 12 分钟。
本示例实现了一个通过Netlink通信协议在内核中发送消息到应用层的模块。以下是内核模块的主要实现代码:
#include#include #include #include #include #include #include #define NETLINK_TEST 17#define MAX_MSGSIZE 1024void sendnlmsg(char *message);struct sock *nl_sk = NULL;static struct nf_hook_ops nfho_marker1;unsigned int hook_mark1(unsigned int hooknum, struct sk_buff *skb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *)){ return NF_ACCEPT;}void sendnlmsg(char *message){ struct sk_buff *skb; struct nlmsghdr *nlh; int len = NLMSG_SPACE(MAX_MSGSIZE); int slen = 0; if (!message || !nl_sk) return; skb = alloc_skb(len, GFP_ATOMIC); if (!skb) printk(KERN_ERR "my_net_link: alloc_skb Error.\n"); return; slen = stringlength(message); nlh = nlmsg_put(skb, 0, 0, 0, MAX_MSGSIZE, 0); NETLINK_CB(skb).pid = 0; NETLINK_CB(skb).dst_group = 1; memcpy(NLMSG_DATA(nlh), "this is from kernel\n", 21); printk("my_net_link: send message '%s'\n", (char *)NLMSG_DATA(nlh)); netlink_broadcast(nl_sk, skb, 0, 1, GFP_ATOMIC); return;}int netlink_init(void){ int i = 10; struct completion cmpl; nfho_marker1.hook = hook_mark1; nfho_marker1.hooknum = NF_INET_PRE_ROUTING; nfho_marker1.pf = PF_INET; nfho_marker1.priority = 11; nf_register_hook(&nfho_marker1); nl_sk = netlink_kernel_create(&init_net, NETLINK_TEST, 1, NULL, NULL, THIS_MODULE); if (!nl_sk) { printk("my_net_link: create netlink socket error.\n"); return 1; } printk("my_net_link: create netlink socket ok.\n"); return 0;}static void netlink_exit(void){ if (nl_sk != NULL) { sock_release(nl_sk->sk_socket); } nf_unregister_hook(&nfho_marker1); printk("my_net_link: self module exited\n");}module_init(netlink_init);module_exit(netlink_exit);MODULE_LICENSE("GPL");
在应用层,示例展示了如何通过Netlink接收内核模块发送的消息。以下是应用层的主要实现代码:
#include#include #include #include #include #include #include #include #include #include #include #define NETLINK_TEST 17#define MAX_PAYLOAD 1024int main(int argc, char *argv){ struct sockaddr_nl src_addr, dest_addr; struct nlmsghdr *nlh = NULL; struct iovec iov; struct msghdr msg; int sock_fd, retval; int fd; FILE *logfile_p; sock_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_TEST); if (sock_fd == -1) { printf("error getting socket: %s", strerror(errno)); return -1; } memset(&src_addr, 0, sizeof(src_addr)); src_addr.nl_family = PF_NETLINK; src_addr.nl_pid = getpid(); src_addr.nl_groups = 1; retval = bind(sock_fd, (struct sockaddr *)&src_addr, sizeof(src_addr)); if (retval < 0) { printf("bind failed: %s", strerror(errno)); close(sock_fd); return -1; } nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD)); if (!nlh) { printf("malloc nlmsghdr error!\n"); close(sock_fd); return -1; } memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD)); iov.iov_base = (void *)nlh; iov.iov_len = NLMSG_SPACE(MAX_PAYLOAD); memset(&msg, 0, sizeof(msg)); memset(&dest_addr, 0, sizeof(dest_addr)); msg.msg_name = (void *)&dest_addr; msg.msg_namelen = sizeof(dest_addr); msg.msg_iov = &iov; msg.msg_iovlen = 1; logfile_p = fopen("/var/log/access", "a+"); if (logfile_p == NULL) { printf("open error\n"); return -1; } while (1) { retval = recvmsg(sock_fd, &msg, 0); printf("Received message: %s\n", NLMSG_DATA(nlh)); value = fputs("write log\n", logfile_p); printf("value = %u\n", value); } close(sock_fd); fclose(logfile_p); return 0;}
模块的编译可以通过以下Makefile进行:
obj-m += netlink.oKERNELDIR = /lib/modules/$(shell uname -r)/buildPWD := $(shell pwd)default: $(MAKE) -C $(KERNELDIR) M=$(PWD) modulesclean: $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
本文展示了一个通过Netlink通信协议在内核与应用层之间实现消息传递的示例。内核模块负责发送消息,应用层则接收并处理这些消息。整个实现过程涵盖了Netlink的基本使用方法、内核模块的编写以及应用层的接收逻辑。
转载地址:http://wdcfk.baihongyu.com/