Advertisement
xeritt

Netfilter module linux kernel 4.15

Nov 16th, 2018
240
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. Basic netfilter module
  3. for Linux kernel(4.8 and 4.15) work!
  4.  
  5. Makefile
  6. obj-m += nf.o
  7. all:
  8.     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
  9. clean:
  10.     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
  11. */
  12.  
  13. #include<linux/kernel.h>
  14. #include<linux/module.h>
  15. #include<linux/netfilter_ipv4.h>
  16. #include<linux/skbuff.h>
  17. #include<linux/ip.h>
  18. #include<linux/inet.h>
  19.  
  20. //#include<linux/netfilter.h>
  21. //#include<linux/udp.h>
  22. //#include<linux/icmp.h>
  23.  
  24. MODULE_AUTHOR("Nick Chi");
  25. MODULE_DESCRIPTION("Basic netfilter module");
  26. MODULE_LICENSE("GPL");
  27.  
  28. static struct nf_hook_ops nfin;
  29.  
  30. static unsigned int hook_func_in(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
  31.  
  32. {
  33.     struct ethhdr *eth;
  34.     struct iphdr *ip_header;
  35.  
  36.     eth = (struct ethhdr*)skb_mac_header(skb);
  37.     ip_header = (struct iphdr *)skb_network_header(skb);
  38.     printk(KERN_INFO "src mac %pM, dst mac %pM\n", eth->h_source, eth->h_dest);
  39.     printk(KERN_INFO "src IP addr: %pI4\n", &ip_header->saddr);
  40.     return NF_ACCEPT;
  41. }
  42. static int __init init_main(void)
  43. {
  44.     nfin.hook     = hook_func_in;
  45.     nfin.hooknum  = NF_INET_PRE_ROUTING;
  46.     nfin.pf       = PF_INET;
  47.     nfin.priority = NF_IP_PRI_FIRST;
  48.     nf_register_net_hook(&init_net, &nfin);
  49.    
  50.     return 0;
  51. }
  52.  
  53.  
  54.  
  55. static void __exit cleanup_main(void)
  56. {
  57.     nf_unregister_net_hook(&init_net, &nfin);
  58.  
  59. }
  60. module_init(init_main);
  61. module_exit(cleanup_main);
Advertisement
RAW Paste Data Copied
Advertisement