2 * netlink-tiny-tk: Wait for a netlink message from user-space, when it
3 * arraives print the process ID of the process which sent the message.
5 * Luiz Fernando N. Capitulino
6 * <lcapitulino@gmail.com>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/mutex.h>
12 #include <linux/net.h>
13 #include <linux/skbuff.h>
14 #include <linux/netlink.h>
17 #define NETLINK_TINY_TK 20 // always check whether this is safe
18 #define LOG_PREFIX "KNETLINK_TINY_TK: "
20 static struct sock
*nl
;
21 static DEFINE_MUTEX(netlink_tiny_tk_mutex
);
23 static void netlink_tiny_tk_input(struct sk_buff
*skb
)
27 mutex_lock(&netlink_tiny_tk_mutex
);
29 if (skb
->len
< sizeof(*nlh
))
33 printk(LOG_PREFIX
"message from: %d\n", nlh
->nlmsg_pid
);
35 mutex_unlock(&netlink_tiny_tk_mutex
);
38 static int __init
netlink_tiny_tk_init(void)
40 nl
= netlink_kernel_create(&init_net
, NETLINK_TINY_TK
, 10,
41 netlink_tiny_tk_input
, NULL
, THIS_MODULE
);
43 printk(KERN_ERR LOG_PREFIX
"cannot create socket\n");
50 static void __exit
netlink_tiny_tk_exit(void)
52 sock_release(nl
->sk_socket
);
55 module_init(netlink_tiny_tk_init
);
56 module_exit(netlink_tiny_tk_exit
);
58 MODULE_LICENSE("GPL");
59 MODULE_AUTHOR("Luiz Capitulino <lcapitulino@gmail.com>");
60 MODULE_DESCRIPTION("Netlink (to kernel) example module");