天天看點

2.4和2.6核心的netfilter差異點

本文檔的Copyleft歸yfydz所有,使用GPL釋出,可以自由拷貝,轉載,轉載時請保持文檔的完整性,嚴禁用于任何商業用途。

msn: [email protected]

來源: http://yfydz.cublog.cn

1. 前言   在2.4和2.6核心中的netfilter基本架構都是相同的,隻是在某些細節上有點差異,要移植的話主要看基本的資料結構和函數定義是否有變化,本文比較一下哪些資料結構和函數進行了修改。   以下比較用的是2.4.26和2.6.8.1。

2. ip_conntrack.h   描述連接配接的結構struct ip_conntrack相同,在2.6.8.1中增加了兩個宏定義:

#define PROVIDES_CONNTRACK(name)                        /

        int needs_ip_conntrack_##name;                  /

        EXPORT_SYMBOL(needs_ip_conntrack_##name)

#define NEEDS_CONNTRACK(name)                                           /

        extern int needs_ip_conntrack_##name;                           /

        static int *need_ip_conntrack_##name __attribute_used__ = &needs_ip_conntrack_##name

3. ip_conntrack_helper.h   在struct ip_conntrack_helper結構中,成員函數help()的參數定義有所改變:   in 2.4.26:  int (*help)(const struct iphdr *, size_t len,

      struct ip_conntrack *ct,

      enum ip_conntrack_info conntrackinfo);   in 2.6.8.1  int (*help)(struct sk_buff *skb,

      struct ip_conntrack *ct,

      enum ip_conntrack_info conntrackinfo);   在2.4中參數是IP頭指針和IP包長,在2.6中隻用struct sk_buff了,這兩個參數都可以提取出: iphdr = skb->nh.iph;

len = skb->len;   另外函數ip_conntrack_expect_related()的參數位置在2.4和2.6中颠倒了:   in 2.4.26 extern int ip_conntrack_expect_related(struct ip_conntrack *related_to,

           struct ip_conntrack_expect *exp);   in 2.6.8.1 extern int ip_conntrack_expect_related(struct ip_conntrack_expect *exp,

           struct ip_conntrack *related_to);   這些改變導緻多連接配接協定(ftp/irc/amanda等)的跟蹤函數需要修改。   在2.6.8.1中增加了一個新函數:

extern struct ip_conntrack_expect *ip_conntrack_expect_alloc(void);

4. ip_nat.h

基本相同,2.6中去掉了以下宏定義:

#ifndef SO_ORIGINAL_DST

#define SO_ORIGINAL_DST 80

#endif

5. ip_nat_helper.h

在2.6.8.1中沒有了以下的宏定義:

#define IP_NAT_HELPER_F_STANDALONE 0x02

在2.6.8.1中沒有了以下的函數定義:

extern void ip_nat_delete_sack(struct sk_buff *skb);

6. ip_conntrack_protocol.h   在結構struct ip_conntrack_protocol中多個結構函數參數發生變化: in 2.4.26

 int (*pkt_to_tuple)(const void *datah, size_t datalen,

       struct ip_conntrack_tuple *tuple);    

 int (*packet)(struct ip_conntrack *conntrack,

        struct iphdr *iph, size_t len,

        enum ip_conntrack_info ctinfo);  

 int (*new)(struct ip_conntrack *conntrack, struct iphdr *iph,

     size_t len);  

 int (*exp_matches_pkt)(struct ip_conntrack_expect *exp,

          struct sk_buff **pskb);   in 2.6.8.1  int (*pkt_to_tuple)(const struct sk_buff *skb,

      unsigned int dataoff,

      struct ip_conntrack_tuple *tuple);  

 int (*packet)(struct ip_conntrack *conntrack,

        const struct sk_buff *skb,

        enum ip_conntrack_info ctinfo);  

 int (*new)(struct ip_conntrack *conntrack, const struct sk_buff *skb);  

 int (*exp_matches_pkt)(struct ip_conntrack_expect *exp,

          const struct sk_buff *skb);   總的看,差别就是直接将資料包指針struct sk_buff傳遞到函數,而不是具體的資料頭指針和長度,這些都由函數自己從資料包中解析。

這些改變導緻每個IP上層協定(TCP/UDP/ICMP)跟蹤的相關函數需要修改。

7. ip_nat_protocol.h   在結構struct ip_nat_protocol中manip_pkt()結構函數參數發生變化:

in 2.4.26

 void (*manip_pkt)(struct iphdr *iph, size_t len,

     const struct ip_conntrack_manip *manip,

     enum ip_nat_manip_type maniptype);

in 2.6.8.1  

 int (*manip_pkt)(struct sk_buff **pskb,

    unsigned int hdroff,

    const struct ip_conntrack_manip *manip,

    enum ip_nat_manip_type maniptype);

差别就是直接将資料包指針struct sk_buff傳遞到函數,而不是具體的資料頭指針和長度,這些都由函數自己從資料包中解析。

這些改變導緻每個IP上層協定(TCP/UDP/ICMP)NAT的相關函數需要修改。

8. ip_tables.h   在struct ipt_match中的match()結構函數參數有所改變: in 2.4.26  int (*match)(const struct sk_buff *skb,

       const struct net_device *in,

       const struct net_device *out,

       const void *matchinfo,

       int offset,

       const void *hdr,

       u_int16_t datalen,

       int *hotdrop);

in 2.6.8.1  int (*match)(const struct sk_buff *skb,

       const struct net_device *in,

       const struct net_device *out,

       const void *matchinfo,

       int offset,

       int *hotdrop);

在2.6中去掉了資料頭和資料長度參數,這些都可以通過struct sk_buff *skb參數擷取。這個改變導緻所有比對子產品的match()函數需要修改。

在struct ipt_target中的參數倒是沒有改變,但結構函數順序發生了改變,這導緻不是通過“.name = function”方式定義的所有目标子產品結構參數順序需要修改。

9. ip_conntrack_core.h   一個常用函數get_tuple()參數發生了變化:   in 2.4.26 extern int get_tuple(const struct iphdr *iph, size_t len,

       struct ip_conntrack_tuple *tuple,

       struct ip_conntrack_protocol *protocol);

in 2.6.8.1 extern int get_tuple(const struct iphdr *iph,

       const struct sk_buff *skb,

       unsigned int dataoff,

       struct ip_conntrack_tuple *tuple,

       const struct ip_conntrack_protocol *protocol);

2.8 ip_nat_core.h   函數icmp_reply_translation()的參數稍有改變:   in 2.4.26 extern unsigned int icmp_reply_translation(struct sk_buff *skb,

        struct ip_conntrack *conntrack,

        unsigned int hooknum,

        int dir);   in 2.6.8.1

extern int icmp_reply_translation(struct sk_buff **pskb,

      struct ip_conntrack *conntrack,

      unsigned int hooknum,

      int dir);  

10. 結論   netfilter從2.4到2.6更新得不多,2.4下的子產品隻需要少量修改就可以在2.6中使用。

繼續閱讀