2.4和2.6核心的netfilter差異點

來源:互聯網
上載者:User
本文檔的Copyleft歸yfydz所有,使用GPL發布,可以自由拷貝,轉載,轉載時請保持文檔的完整性,嚴禁用於任何商業用途。
msn: yfydz_no1@hotmail.com

來源:http://yfydz.cublog.cn1. 前言 在2.4和2.6核心中的netfilter基本架構都是相同的,只是在某些細節上有點差異,要移植的話主要看基本的資料結構和函數定義是否有變化,本文比較一下哪些資料結構和函數進行了修改。 以下比較用的是2.4.26和2.6.8.1。2. ip_conntrack.h 描述串連的結構struct ip_conntrack相同,在2.6.8.1中增加了兩個宏定義:/* eg. PROVIDES_CONNTRACK(ftp); */
#define PROVIDES_CONNTRACK(name)                        /
        int needs_ip_conntrack_##name;                  /
        EXPORT_SYMBOL(needs_ip_conntrack_##name)/*. eg. NEEDS_CONNTRACK(ftp); */
#define NEEDS_CONNTRACK(name)                                           /
        extern int needs_ip_conntrack_##name;                           /
        static int *need_ip_conntrack_##name __attribute_used__ = &needs_ip_conntrack_##name3. 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.26extern int ip_conntrack_expect_related(struct ip_conntrack *related_to,
           struct ip_conntrack_expect *exp); in 2.6.8.1extern int ip_conntrack_expect_related(struct ip_conntrack_expect *exp,
           struct ip_conntrack *related_to); 這些改變導致多連線協定(ftp/irc/amanda等)的跟蹤函數需要修改。 在2.6.8.1中增加了一個新函數:
/* Allocate space for an expectation: this is mandatory before calling
   ip_conntrack_expect_related. */
extern struct ip_conntrack_expect *ip_conntrack_expect_alloc(void);4. ip_nat.h基本相同,2.6中去掉了以下宏定義:
/* 2.3.19 (I hope) will define this in linux/netfilter_ipv4.h. */
#ifndef SO_ORIGINAL_DST
#define SO_ORIGINAL_DST 80
#endif5. ip_nat_helper.h在2.6.8.1中沒有了以下的宏定義:
/* Standalone NAT helper, without a conntrack part */
#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
 /* Try to fill in the third arg; return true if possible. */
 int (*pkt_to_tuple)(const void *datah, size_t datalen,
       struct ip_conntrack_tuple *tuple);  /* Returns verdict for packet, or -1 for invalid. */
 int (*packet)(struct ip_conntrack *conntrack,
        struct iphdr *iph, size_t len,
        enum ip_conntrack_info ctinfo); /* Called when a new connection for this protocol found;
  * returns TRUE if it's OK.  If so, packet() called next. */
 int (*new)(struct ip_conntrack *conntrack, struct iphdr *iph,
     size_t len); /* Has to decide if a expectation matches one packet or not */
 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); /* Returns verdict for packet, or -1 for invalid. */
 int (*packet)(struct ip_conntrack *conntrack,
        const struct sk_buff *skb,
        enum ip_conntrack_info ctinfo); /* Called when a new connection for this protocol found;
  * returns TRUE if it's OK.  If so, packet() called next. */
 int (*new)(struct ip_conntrack *conntrack, const struct sk_buff *skb); /* Has to decide if a expectation matches one packet or not */
 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
 /* Do a packet translation according to the ip_nat_proto_manip
  * and manip type. */
 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 /* Do a packet translation according to the ip_nat_proto_manip
  * and manip type.  Return true if succeeded. */
 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.26extern int get_tuple(const struct iphdr *iph, size_t len,
       struct ip_conntrack_tuple *tuple,
       struct ip_conntrack_protocol *protocol);in 2.6.8.1extern 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.26extern 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中使用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.