【Linux】Netfilter的使用和实现


概述

 Netfilter为多种网络协议(IPv4、IPv6、ARP等)各提供了一套钩子函数。

在IPv4中定义了5个钩子函数,这些钩子函数在数据包流经协议栈的5个关键点被调用。

这就像有5个钓鱼台,在每个钓鱼台放了一个鱼钩(钩子函数),把经过的数据包钓上来,

然后根据自定义的规则,来决定数据包的命运:

可以原封不动的放回IPv4协议,继续向上层递交;可以进行修改,再放回IPv4协议;也可以直接丢弃。

Netfilter主要采用连接跟踪(Connection Tracking)、包过滤(Packet Filtering)、地址转换(NAT)、包处理

(Packet Mangling)四种技术。

 

(1) IP层的5个钓鱼台

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. enum nf_inet_hooks {  
  2.     NF_INET_PRE_ROUTING,  
  3.     NF_INET_LOCAL_IN,  
  4.     NF_INET_FORWARD,  
  5.     NF_INET_LOCAL_OUT,  
  6.     NF_INET_POST_ROUTING,  
  7.     NF_INET_NUMHOOKS  
  8. };  

支持的协议类型:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. enum {  
  2.     NFPROTO_UNSPEC = 0,  
  3.     NFPROTO_IPV4 = 2,  
  4.     NFPROTO_ARP = 3,  
  5.     NFPROTO_BRIDGE = 7,  
  6.     NFPROTO_IPV6 = 10,  
  7.     NFPROTO_DECNET = 12,  
  8.     NFPROTO_NUMPROTO,  
  9. };  

 

(2) 钩子函数

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. typedef unsigned int nf_hookfn(unsigned int hooknum,  
  2.                                struct sk_buff *skb,  
  3.                                const struct net_device *in,  
  4.                                const struct net_device *out,  
  5.                                int (*okfn) (struct sk_buff *));  
  6.   
  7. /* 处理函数返回值 */  
  8. #define NF_DROP 0 /* drop the packet, don't continue traversal */  
  9. #define NF_ACCEPT 1 /* continue traversal as normal */  
  10. #define NF_STOLEN 2 /* I've taken over the packet, don't continue traversal */  
  11. #define NF_QUEUE 3 /* queue the packet (usually for userspace handling) */  
  12. #define NF_REPEAT 4 /* call this hook again */  
  13. #define NF_STOP 5  
  14. #define NF_MAX_VERDICT NF_STOP  

 

(3) Netfilter实体

在使用Netfilter时,需要定义一个nf_hook_ops实例。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct nf_hook_ops {  
  2.     struct list_head list;  
  3.     /* User fills in from here down. */  
  4.     nf_hookfn *hook; /* 要注册的钩子函数 */  
  5.     struct module *owner;  
  6.     u_int8_t pf; /* 协议类型 */  
  7.     unsigned int hooknum; /* 哪个钓鱼台 */  
  8.     /* Hooks are ordered in asending priority. */  
  9.     int priority; /* 数值越小,优先级越高 */  
  10. };  
  11. typedef __u8 u_int8_t;  

 

(4) 注册与注销

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /* Functions to register/unregister hook points. */  
  2. int nf_register_hook(struct nf_hook_ops *reg);  
  3. void nf_unregister_hook(struct nf_hook_ops *reg);  

 

实现

 

Netfilter定义了一个全局链表:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];  
  2. EXPORT_SYMBOL(nf_hooks);  
  3. static DEFINE_MUTEX(nf_hook_mutex);  

 

(1) 注册函数

注册函数会把nf_hook_ops放入nf_hooks相应的位置中。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. int nf_register_hook(struct nf_hook_ops *reg)  
  2. {  
  3.     struct nf_hook_ops *elem;  
  4.     int err;  
  5.   
  6.     err = mutex_lock_interruptible(&nf_hook_mutex);  
  7.     if (err < 0)  
  8.         return err;  
  9.   
  10.     list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {  
  11.         if (reg->priority < elem->priority)  
  12.             break;  
  13.     }  
  14.   
  15.     list_add_rcu(&reg->list, elem->list.prev); /* 把netfilter实例添加到队列中 */  
  16.     mutex_unlock(&nf_hook_mutex);  
  17.     return 0;  
  18. }  

 

(2) 注销函数

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. void nf_unregister_hook(struct nf_hook_ops *reg)  
  2. {  
  3.     mutex_lock(&nf_hook_mutex);  
  4.     list_del_rcu(&reg->list); /* 把netfilter实例从队列中删除 */  
  5.     mutex_unlock(&nf_hook_mutex);  
  6.     synchronize_net();  
  7. }  

 

(3) 内核接口

内核的Netfilter钩子函数调用:

NF_HOOK

    |--> NF_HOOK_THRESH

               |--> nf_hook_thresh

                         |--> nf_hook_slow

                                   |--> nf_iterate

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. static inline int NF_HOOK(uint8_t pf, unsigned int hook, struct sk_buff *skb,  
  2.     struct net_device *in, struct net_device *out, int (*okfn)(struct sk_buff *))  
  3. {  
  4.     /* INT_MIN表示要调用钓鱼台的所有钩子函数 */  
  5.     return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN);   
  6. }  
  7.   
  8. static inline int NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sk_buff *skb,  
  9.     struct net_device *in, struct net_device *out, int (*okfn)(struct sk_buff *), int thresh)  
  10. {  
  11.     int ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, thresh);  
  12.     if (ret == 1)  
  13.         ret = okfn(skb); /* 如果skb没被处理掉,调用此函数 */  
  14.   
  15.     return ret;  
  16. }  
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * nf_hook_thresh - call a netfilter hook 
  3.  * Returns 1 if the hook has allowed the packet to pass. 
  4.  * The function okfn must be invoked by the caller in this case. 
  5.  * Any other return value indicates the packet has been consumed by the hook. 
  6.  */  
  7. static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook, struct sk_buff *skb,  
  8.     struct net_device *indev, struct net_device *outdev, int (*okfn)(struct sk_buff *), int thresh)  
  9. {  
  10. #ifndef CONFIG_NETFILTER_DEBUG  
  11.     /* 如果协议pf的hook点上没有已注册的nf_hook_ops实例,直接返回1 */  
  12.     if (list_empty(&nf_hooks[pf][hook]))  
  13.         return 1;  
  14. #endif  
  15.   
  16.     return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);  
  17. }  
  18.    
  19. /* Returns 1 if okfn() needs to be executed by the caller, -EPERM for NF_DROP, 0 otherwise. */  
  20. int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb, struct net_device *indev,  
  21.     struct net_device *outdev, int (*okfn)(struct sk_buff *), int hook_thresh)  
  22. {  
  23.     struct list_head *elem;  
  24.     unsigned int verdict;  
  25.     int ret = 0;  
  26.   
  27.     /* We may already have this, but read-locks nest anyway */  
  28.     rcu_read_lock();  
  29.   
  30.     elem = &nf_hooks[pf][hook];  
  31. next_hook:  
  32.     verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev, outdev, &elem, okfn, hook_thresh);  
  33.   
  34.     if (verdict == NF_ACCEPT || verdict == NF_STOP) {  
  35.         ret = 1;  
  36.     } else if (verdict == NF_DROP) {  
  37.         kfree_skb(skb);  
  38.         ret = -EPERM;  
  39.     } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {  
  40.         if (! nf_queue(skb, elem, ph, hook, indev, outdev, okfn, verdict >> NF_VERDICT_BITS))  
  41.             goto next_hook;  
  42.     }  
  43.   
  44.     rcu_read_unlock();  
  45.   
  46.     return ret;  
  47. }  
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. unsigned int nf_iterate(struct list_head *head, struct sk_buff *skb, unsigned int hook,  
  2.     const struct net_device *indev, const struct net_device *outdev, struct list_head **i,  
  3.     int (*okfn)(struct sk_buff *), int hook_thresh)  
  4. {  
  5.     unsigned int verdict;  
  6.   
  7.     /*  
  8.      * The caller must not block between calls to this function because of risk of 
  9.      * continuing from deleted element. 
  10.      */  
  11.     list_for_each_continue_rcu(*i, head) {  
  12.         struct nf_hook_ops *elem = (struct nf_hook_ops *) *i;  
  13.   
  14.         /* 优先级>=hook_thresh的都会被执行 */  
  15.         if (hook_thresh > elem_priority)  
  16.             continue;  
  17.   
  18.         verdict = elem->hook(hook, skb, indev, outdev, okfn); /* 已注册的执行函数 */  
  19.   
  20.         if (verdict != NF_ACCEPT) {  
  21. #ifdef CONFIG_NETFILTER_DEBUG  
  22.             if (unlikely((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT)) {  
  23.                 NFDEBUG("Evil return from %p(%u).\n", elem->hook, hook);  
  24.                 continue;  
  25.             }  
  26. #endif  
  27.   
  28.             if (verdict != NF_REPEAT)  
  29.                 return verdict;  
  30.             *i = (*i)->prev;  
  31.         }  
  32.     }  
  33.   
  34.     return NF_ACCEPT;  
  35. }  

 

使用

 

以下是一个简单的模块,加载到一个HTTP服务器上。

通过在PRE_ROUTING处注册my_hookfn,改变接收数据包的源IP为8.8.8.8(Google DNS server)。

当客户端向服务器发送一个请求时,肯定收不到服务器的响应:)

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <linux/netfilter.h>  
  2. #include <linux/init.h>  
  3. #include <linux/module.h>  
  4. #include <linux/netfilter_ipv4.h>  
  5. #include <linux/ip.h>  
  6. #include <linux/inet.h>  
  7.   
  8. /** 
  9.  * Hook function to be called. 
  10.  * We modify the packet's src IP. 
  11.  */  
  12. unsigned int my_hookfn(unsigned int hooknum,  
  13.     struct sk_buff *skb,  
  14.     const struct net_device *in,  
  15.     const struct net_device *out,  
  16.     int (*okfn)(struct sk_buff *))  
  17. {  
  18.     struct iphdr *iph;  
  19.     iph = ip_hdr(skb);  
  20.   
  21.     /* log the original src IP */  
  22.     printk(KERN_INFO"src IP %pI4\n", &iph->saddr);  
  23.   
  24.     /* modify the packet's src IP */  
  25.     iph->saddr = in_aton("8.8.8.8");  
  26.   
  27.     return NF_ACCEPT;  
  28. }  
  29.   
  30. /* A netfilter instance to use */  
  31. static struct nf_hook_ops nfho = {  
  32.     .hook = my_hookfn,  
  33.     .pf = PF_INET,  
  34.     .hooknum = NF_INET_PRE_ROUTING,  
  35.     .priority = NF_IP_PRI_FIRST,  
  36.     .owner = THIS_MODULE,  
  37. };  
  38.   
  39. static int __init sknf_init(void)  
  40. {  
  41.     if (nf_register_hook(&nfho)) {  
  42.         printk(KERN_ERR"nf_register_hook() failed\n");  
  43.         return -1;  
  44.     }  
  45.     return 0;  
  46. }  
  47.   
  48. static void __exit sknf_exit(void)  
  49. {  
  50.     nf_unregister_hook(&nfho);  
  51. }  
  52.   
  53. module_init(sknf_init);  
  54. module_exit(sknf_exit);  
  55. MODULE_AUTHOR("zhangsk");  
  56. MODULE_LICENSE("GPL");  
0
0
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值