thanks for your article it is very useful . i try to create communication socket beetwin Kernel module and user land program . i used you proposed code . but it is not worked correctly . i compile my module and userland code correctly but there is no communication between them .please , It would be nice if you could add working or at least compilable examples.
best regard'sM.taghiloo
/* Working version of the Netlink Socket code from Linux Journal's Kernel Korner */#include#include#include#include#include#include
#define MAX_PAYLOAD 1024struct sockaddr_nl src_addr, dst_addr;struct nlmsghdr *nlh = NULL;struct msghdr msg;struct iovec iov;int sock_fd;
int main(){sock_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_NITRO);
memset(&src_addr, 0, sizeof(src_addr));src_addr.nl_family = AF_NETLINK;src_addr.nl_pid = getpid();src_addr.nl_groups = 0; // no multicastbind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr));
memset(&dst_addr, 0, sizeof(dst_addr));dst_addr.nl_family = AF_NETLINK;dst_addr.nl_pid = 0; // 0 means kerneldst_addr.nl_groups = 0; // no multicast
nlh = (struct nlhmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
/* Fill the netlink message header */nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);nlh->nlmsg_pid = getpid();nlh->nlmsg_flags = 0;
strcpy(NLMSG_DATA(nlh), "Yoo-hoo, Mr. Kernel!");
iov.iov_base = (void *)nlh;iov.iov_len = nlh->nlmsg_len;
msg.msg_name = (void *)&dst_addr;msg.msg_namelen = sizeof(dst_addr);msg.msg_iov = &iov;msg.msg_iovlen = 1;
sendmsg(sock_fd, &msg, 0);
/* Read message from kernel */memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));recvmsg(sock_fd, &msg, 0);printf("Received message payload: %s\n", NLMSG_DATA(nlh));
close(sock_fd);
return (EXIT_SUCCESS);
}
/* The Linux Journal Kernel Korner -- Working, compiling version of the kernel code */#include#include#include#include#include#include#include
MODULE_LICENSE("GPL");MODULE_AUTHOR("Daniel Purcell");MODULE_DESCRIPTION("Kernel Korner's working versinon of netlink sockets");
// Note: Debug is not implementedstatic int debug = 0;
module_param(debug, int, 0);MODULE_PARM_DESC(debug, "Debug information (default 0)");
static struct sock *nl_sk = NULL;
static void nl_data_ready (struct sock *sk, int len){wake_up_interruptible(sk->sk_sleep);}
static void netlink_test(){struct sk_buff *skb = NULL;struct nlmsghdr *nlh = NULL;int err;u32 pid;
nl_sk = netlink_kernel_create(NETLINK_NITRO, nl_data_ready);skb = skb_recv_datagram(nl_sk, 0, 0, &err);
nlh = (struct nlmsghdr *)skb->data;printk(KERN_INFO "%s: received netlink message payload: %s\n", __FUNCTION__, NLMSG_DATA(nlh));
pid = nlh->nlmsg_pid;NETLINK_CB(skb).groups = 0;NETLINK_CB(skb).pid = 0;NETLINK_CB(skb).dst_pid = pid;NETLINK_CB(skb).dst_groups = 0;netlink_unicast(nl_sk, skb, pid, MSG_DONTWAIT);sock_release(nl_sk->sk_socket);}
static int __init my_module_init(void){printk(KERN_INFO "Initializing Netlink Socket");netlink_test();return 0;}
static void __exit my_module_exit(void){printk(KERN_INFO "Goodbye");}
module_init(my_module_init);module_exit(my_module_exit);
Hello,The article is very clear and understood. It describes the advantages of using netlink sockets. I suppose it might be very useful in inter processes / threads communication in user-space application. But regarding the kernel space, there are disadvantages such as:1. Kernel recompiling, because it requires netlink.h update.2. Because it's running in the context of sendmsg prosses, the trivial ioctl is preferred just in the reason that it's not so sophisticated.Any comments are very welcome,Regards,Michael
I hear that netlink provides support for communication within two different subsystems of the kernel. Wish this article had covered that.
RP
It would be nice if you could add working or at least compilable examples.
thanks,-M
netlink is implemented as a device like /dev/netlink on 2.4.20-8open,read,write functions from userland to /dev/netlink actually map to socket calls.
The kernel-sidecode for netlink is under /usr/src/linux-2.4/net/netlink/netlink_dev.cIf you wish to customize, you can change the NETLINK_MAJOR to a number you like (check major.h) and compile the module separrately with a makefile like
export KERN_NAME = linux-2.4.20-8CFLAGS = -I /usr/src/$(KERN_NAME)/include -D__KERNEL__
netlink_dev1.o: netlink_dev1.c
I type all the source code as above article in FC4(2.6.11-1.1369_FC4-i686 kernel).
kernel code error:for "sk->sk_sleep" and "sock_release(nl_sk->sk_socket)":dereferencing pointer to incomplete type
user code error:on line"nlh->nlmsg_len=NLMSG_SPACE(MAX_PAYLOAD)"syntax error before '=' token
what's the reason?? Help me please
Inclued the following line at top of the program.
#include
ThanksChinmaya
Inclued the following line at top of the program. #include net/sock.h.
Powered by: IT博客 Copyright © Enjoy Life