Skip to content

Instantly share code, notes, and snippets.

@gsathya
Created September 20, 2013 20:12
Show Gist options
  • Save gsathya/6643161 to your computer and use it in GitHub Desktop.
Save gsathya/6643161 to your computer and use it in GitHub Desktop.
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/skbuff.h>
#include <linux/netfilter_ipv4.h>
#include <linux/udp.h>
#include <linux/ip.h>
#include <linux/netdevice.h>
static char *interface = "eth0";
/* net filter hook option struct */
static struct nf_hook_ops netfilter_ops;
struct sk_buff *sock_buff;
/* udp header struct (not used) */
struct udphdr *udp_header;
/* ip header struct */
struct iphdr *ip_header;
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 *))
{
if(strcmp(in->name, interface) == 0){
sock_buff = *skb;
/* grab network header using accessor */
ip_header = (struct iphdr *)skb_network_header(sock_buff);
if(!sock_buff) { return NF_ACCEPT;}
// Print the IP address
printk("src IP addr:=%d.%d.%d.%d:%d\n", NIPQUAD(ip_headr->saddr));
return NF_ACCEPT;
}
return NF_ACCEPT;
}
static int main(void)
{
netfilter_ops.hook = hook_func;
netfilter_ops.hooknum = NF_INET_PRE_ROUTING;
netfilter_ops.pf = PF_INET;
netfilter_ops.priority = NF_IP_PRI_FIRST;
nf_register_hook(&netfilter_ops);
return 0;
}
static void cleanup(void)
{
nf_unregister_hook(&netfilter_ops);
}
module_init(main);
module_exit(cleanup)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment