From: Mikado <mikado4vn@yahoo.com>
To: linux-kernel@vger.kernel.org, linux-c-programming@vger.kernel.org
Subject: Netlink socket problem
Date: Fri, 6 Jan 2006 23:03:19 -0800 (PST) [thread overview]
Message-ID: <20060107070319.17141.qmail@web53707.mail.yahoo.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 3877 bytes --]
Hi,
I write a kernel module and a userspace client. They use netlink sockets to communicate with each
other. The source codes are attached below. Inserting the module is ok but when I ran the client,
my kernel crashed after the client received first message from the module. Is there anything wrong
in my codes?
Thank you.
=== nltest.c - kernel module ===
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <net/sock.h>
#include <linux/socket.h>
#include <linux/net.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/skbuff.h>
#define NETLINK_VFW 18
#define VFW_GROUP 0
#define MSG_SIZE NLMSG_SPACE(1024)
static struct sock *nl_sk = NULL;
static void nltest_rcv(struct sock *sk, int len)
{
struct sk_buff *nl_skb;
struct nlmsghdr *nl_hdr;
int pid;
while ((nl_skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
nl_hdr = (struct nlmsghdr *)nl_skb->data;
pid = nl_hdr->nlmsg_pid;
printk(KERN_ALERT "nltest: message from user (pid = %d) = %s\n", pid, (char
*)NLMSG_DATA(nl_hdr));
nl_skb = alloc_skb(MSG_SIZE, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
skb_put(nl_skb, MSG_SIZE);
nl_hdr = (struct nlmsghdr *)nl_skb->data;
nl_hdr->nlmsg_len = MSG_SIZE;
nl_hdr->nlmsg_pid = pid;
nl_hdr->nlmsg_flags = 0;
strcpy(NLMSG_DATA(nl_hdr), "hello user abcd1234");
NETLINK_CB(nl_skb).pid = 0;
NETLINK_CB(nl_skb).dst_pid = pid;
NETLINK_CB(nl_skb).dst_group = VFW_GROUP;
netlink_unicast(nl_sk, nl_skb, pid, 0);
kfree_skb(nl_skb);
}
}
static int __init nltest_init(void)
{
printk(KERN_ALERT "nltest: init\n");
nl_sk = netlink_kernel_create(NETLINK_VFW, VFW_GROUP, nltest_rcv, THIS_MODULE);
if (!nl_sk) {
printk(KERN_ALERT "nltest: netlink_kernel_create() failed\n");
return -1;
}
return 0;
}
static void __exit nltest_exit(void)
{
printk(KERN_ALERT "nltest: exit\n");
sock_release(nl_sk->sk_socket);
return;
}
module_init(nltest_init);
module_exit(nltest_exit);
MODULE_DESCRIPTION("Netlink Test");
MODULE_LICENSE("GPL");
=== nlclient.c - userspace client ===
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <asm/types.h>
#include <linux/netlink.h>
#define NETLINK_VFW 18
#define VFW_GROUP 0
#define MSG_SIZE NLMSG_SPACE(1024)
int main(void)
{
int nl_sd;
struct sockaddr_nl src_addr;
struct nlmsghdr *nl_hdr;
unsigned char buf[MSG_SIZE];
int ret;
memset(&src_addr, 0, sizeof(struct sockaddr_nl));
memset(buf, 0, MSG_SIZE);
nl_sd = socket(PF_NETLINK, SOCK_RAW, NETLINK_VFW);
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid();
src_addr.nl_groups = VFW_GROUP;
bind(nl_sd, (struct sockaddr *)&src_addr, sizeof(struct sockaddr));
nl_hdr = (struct nlmsghdr *)buf;
nl_hdr->nlmsg_len = MSG_SIZE;
nl_hdr->nlmsg_pid = getpid();
nl_hdr->nlmsg_flags = 0;
strcpy(NLMSG_DATA(nl_hdr), "hello kernel");
ret = send(nl_sd, buf, MSG_SIZE, 0);
printf("send ret = %d\n", ret);
if (ret == -1)
return ret;
while (1) {
ret = recv(nl_sd, buf, MSG_SIZE, 0);
printf("message from kernel = %s\n", (char *)NLMSG_DATA(nl_hdr));
}
return 0;
}
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
[-- Attachment #2: 938020532-nltest.c --]
[-- Type: application/octet-stream, Size: 1718 bytes --]
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <net/sock.h>
#include <linux/socket.h>
#include <linux/net.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/skbuff.h>
#define NETLINK_VFW 18
#define VFW_GROUP 0
#define MSG_SIZE NLMSG_SPACE(1024)
static struct sock *nl_sk = NULL;
static void nltest_rcv(struct sock *sk, int len)
{
struct sk_buff *nl_skb;
struct nlmsghdr *nl_hdr;
int pid;
while ((nl_skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
nl_hdr = (struct nlmsghdr *)nl_skb->data;
pid = nl_hdr->nlmsg_pid;
printk(KERN_ALERT "nltest: message from user (pid = %d) = %s\n", pid, (char *)NLMSG_DATA(nl_hdr));
nl_skb = alloc_skb(MSG_SIZE, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
skb_put(nl_skb, MSG_SIZE);
nl_hdr = (struct nlmsghdr *)nl_skb->data;
nl_hdr->nlmsg_len = MSG_SIZE;
nl_hdr->nlmsg_pid = pid;
nl_hdr->nlmsg_flags = 0;
strcpy(NLMSG_DATA(nl_hdr), "hello user abcd1234");
NETLINK_CB(nl_skb).pid = 0;
NETLINK_CB(nl_skb).dst_pid = pid;
NETLINK_CB(nl_skb).dst_group = VFW_GROUP;
netlink_unicast(nl_sk, nl_skb, pid, 0);
kfree_skb(nl_skb);
}
}
static int __init nltest_init(void)
{
printk(KERN_ALERT "nltest: init\n");
nl_sk = netlink_kernel_create(NETLINK_VFW, VFW_GROUP, nltest_rcv, THIS_MODULE);
if (!nl_sk) {
printk(KERN_ALERT "nltest: netlink_kernel_create() failed\n");
return -1;
}
return 0;
}
static void __exit nltest_exit(void)
{
printk(KERN_ALERT "nltest: exit\n");
sock_release(nl_sk->sk_socket);
return;
}
module_init(nltest_init);
module_exit(nltest_exit);
MODULE_DESCRIPTION("Netlink Test");
MODULE_AUTHOR("Nguyen Minh Nhat <ngmnhat@gmail.com>");
MODULE_LICENSE("GPL");
[-- Attachment #3: 3903798118-nlclient.c --]
[-- Type: application/octet-stream, Size: 1159 bytes --]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <asm/types.h>
#include <linux/netlink.h>
#define NETLINK_VFW 18
#define VFW_GROUP 0
#define MSG_SIZE NLMSG_SPACE(1024)
int main(void);
int main(void)
{
int nl_sd;
struct sockaddr_nl src_addr;
struct nlmsghdr *nl_hdr;
unsigned char buf[MSG_SIZE];
int ret;
memset(&src_addr, 0, sizeof(struct sockaddr_nl));
memset(buf, 0, MSG_SIZE);
nl_sd = socket(PF_NETLINK, SOCK_RAW, NETLINK_VFW);
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid();
src_addr.nl_groups = VFW_GROUP;
bind(nl_sd, (struct sockaddr *)&src_addr, sizeof(struct sockaddr));
nl_hdr = (struct nlmsghdr *)buf;
nl_hdr->nlmsg_len = MSG_SIZE;
nl_hdr->nlmsg_pid = getpid();
nl_hdr->nlmsg_flags = 0;
strcpy(NLMSG_DATA(nl_hdr), "hello kernel");
ret = send(nl_sd, buf, MSG_SIZE, 0);
printf("send ret = %d\n", ret);
if (ret == -1)
return ret;
while (1) {
ret = recv(nl_sd, buf, MSG_SIZE, 0);
printf("message from kernel = %s\n", (char *)NLMSG_DATA(nl_hdr));
}
return 0;
}
reply other threads:[~2006-01-07 7:03 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20060107070319.17141.qmail@web53707.mail.yahoo.com \
--to=mikado4vn@yahoo.com \
--cc=linux-c-programming@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).