Guest User

Untitled

a guest
May 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. //v2 netfilter hooks example
  2. //For any packet, get the ip header and check the protocol field
  3. //if the protocol number equal to UDP (17), log in var/log/messages
  4. //default action of module to let all packets through
  5.  
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/netfilter.h>
  9. #include <linux/netfilter_ipv4.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/udp.h>
  12. #include <linux/ip.h>
  13.  
  14. static struct nf_hook_ops nfho; //net filter hook option struct
  15. struct sk_buff *sock_buff;
  16. struct udphdr *udp_header; //udp header struct (not used)
  17. struct iphdr *ip_header; //ip header struct
  18.  
  19. static unsigned int hook_func(unsigned int hooknum,
  20. struct sk_buff **skb,
  21. const struct net_device *in,
  22. const struct net_device *out,
  23. int (*okfn)(struct sk_buff *))
  24. {
  25. sock_buff = *skb;
  26. ip_header = (struct iphdr *)skb_network_header(sock_buff); //grab network header using accessor
  27. if(!sock_buff) { return NF_ACCEPT;}
  28. if (ip_header->protocol==17) {
  29. udp_header = (struct udphdr *)skb_transport_header(sock_buff); //grab transport header
  30. printk(KERN_INFO "got udp packet \n"); //log we’ve got udp packet to /var/log/messages
  31. return NF_DROP;
  32. }
  33. return NF_ACCEPT;
  34. }
  35.  
  36. static int __init init_main(void)
  37. {
  38. nfho.hook = hook_func;
  39. nfho.hooknum = 1;
  40. nfho.pf = PF_INET;
  41. nfho.priority = NF_IP_PRI_FIRST;
  42. nf_register_hook(&nfho);
  43. return 0;
  44. }
  45.  
  46. static void __exit cleanup_main(void)
  47. {
  48. nf_unregister_hook(&nfho);
  49. }
  50.  
  51. module_init(init_main);
  52. module_exit(cleanup_main);
Add Comment
Please, Sign In to add comment