-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnetlink_kernel_try1.c
More file actions
58 lines (48 loc) · 1.19 KB
/
Copy pathnetlink_kernel_try1.c
File metadata and controls
58 lines (48 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netlink.h>
#include <net/sock.h>
#define NETLINK_TEST 17
#define MSG_LEN NLMSG_SPACE(1024)
static struct sock *nl_sk;
static void nl_data_ready(struct sock *sk, int len)
{
struct sk_buff *nl_skb;
struct nlmsghdr *nl_hdr;
int pid;
printk ("Inside nl_data_ready\n");
nl_skb = skb_dequeue (&sk->sk_receive_queue);
if (nl_skb != NULL)
{
/* Copying data from userspace */
nl_hdr = (struct nlmsghdr *)nl_skb->data;
pid = nl_hdr->nlmsg_pid;
printk ("Netlink user has message.....\n");
printk ("PID : %d\n", pid);
printk ("Message : %s\n", (char *)NLMSG_DATA(nl_hdr));
}
else
printk ("Netlink has no message...........\n");
}
static void netlink_test (void)
{
nl_sk = netlink_kernel_create(NETLINK_TEST, 0, nl_data_ready,
NULL, THIS_MODULE);
if (nl_sk == NULL)
{
printk ("Unable to create kernel socket\n");
}
printk ("Waiting for data\n");
}
static int __init register_link (void)
{
printk ("Netlink module is registered\n");
netlink_test ();
return 0;
}
static void __exit unregister_link (void)
{
printk ("netlink module is unregistered\n");
}
module_init (register_link);
module_exit (unregister_link);