Hi,
I want send netfilter messages to user space program.
For this I have written a kernel and user space modules. They are successfully communicating (two way) using netlink sockets.
Then in same kernel module i have registered a hook to catch ICMP packets (which are coming when i ping machine).This also working.
I want send hooks received packets(ICMP/netfilter message) to user space module.
Please help me how can send netfilter message via netlink socket to User space Progarm.
kernel module is :
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Test");
MODULE_DESCRIPTION("Testing Netlink Socket");
static int debug=0;
module_param(debug,int,0);
MODULE_PARM_DESC(debug,"Debug Information");
static char *drop_if = "lo";
static struct sock *nl_sk=NULL;
static struct nf_hook_ops nfho;
static nl_data_ready(struct sock *sk,int len)
{
int err;
struct sk_buff *sk1=NULL;
struct nlmsghdr *nlh=NULL;
u32 pid;
printk("Netlink Socket Got Message\n");
sk1=skb_recv_datagram(nl_sk,0,0,&err);
nlh =(struct nlmsghdr*)(sk1->data);
printk("Kernel Received :%s\n",NLMSG_DATA(nlh));
strcpy(NLMSG_DATA(nlh), "This is ICMP Packet ");
pid = nlh->nlmsg_pid;
NETLINK_CB(sk1).pid =0;
NETLINK_CB(sk1).dst_group =0;
netlink_unicast(nl_sk,sk1,pid,MSG_DONTWAIT);
}
unsigned int hook_func(unsigned int hooknum,struct sk_buff **skb,const struct net_device *in,const struct net_device *out,int (*okfn)(struct sk_buff*))
{
struct sk_buff *sb = *skb;
struct nlmsghdr *nlh=NULL;
static unsigned char *drop_ip = "x7fx00x00x01";
u32 pid;
if(strcmp(in->name,drop_if) == 0)
{
printk("ICMP Packet Receved on %s \n",drop_if);
nlh =(struct nlmsghdr*)(sb->data);
/*
pid = nlh->nlmsg_pid;
NETLINK_CB(sb).pid =0;
NETLINK_CB(sb).dst_group =0;
// netlink_unicast(nl_sk,sb,pid,MSG_DONTWAIT);
*/
return NF_ACCEPT;
}
else
{
return NF_ACCEPT;
}
}
static void netlink_test()
{
nl_sk=netlink_kernel_create(NETLINK_NETFILTER,0,nl _data_ready,NULL,NULL);
if(nl_sk==NULL)
printk("Netlink socket creation fails\n");
else
printk("Netlink socket created\n");
}
static int netkernel_init(void)
{
printk("Initialiaing netlink socket\n");
netlink_test();
printk("Registering Hook\n");
nfho.hook = hook_func;
nfho.hooknum = NF_IP_PRE_ROUTING;
nfho.pf = PF_INET;
nfho.priority = NF_IP_PRI_FIRST;
nf_register_hook(&nfho);
return 0;
}
static int netkernel_exit(void)
{
printk("Good Bye!!!\n");
sock_release(nl_sk->sk_socket);
printk("Unregistering hook\n");
nf_unregister_hook(&nfho);
}
module_init(netkernel_init);
module_exit(netkernel_exit);
Thanks in Adavance
Shashidhara HN