荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: ykwch.bbs@bbs.cqupt.edu.cn (心蓝--雨下), 信区: Linux
标  题: 防火墙技术分析讲义--三
发信站: 幽幽黄桷兰 (Sat Apr  5 15:03:14 2003)
转信站: SZU!news.tiaozhan.com!news.happynet.org!CQUPT

三 Linux下防火墙的实现之二(2.4内核):
3.1
        A Packet Traversing the Netfilter System:
        --->PRE------>[ROUTE]--->FWD---------->POST------>
               Conntrack    |       Filter   ^    NAT (Src)
               Mangle       |                |    Conntrack
               NAT (Dst)    |             [ROUTE]
               (QDisc)      v                |
                            IN Filter       OUT Conntrack
                            |  Conntrack     ^  Mangle
                            |                |  NAT (Dst)
                            v                |  Filter
3.2 例子
## Insert connection-tracking modules (not needed if built into kernel).
# insmod ip_conntrack
# insmod ip_conntrack_ftp
## Create chain which blocks new connections, except if coming from inside.
# iptables -N block
# iptables -A block -m state --state ESTABLISHED,RELATED -j ACCEPT
# iptables -A block -m state --state NEW -i ! ppp0 -j ACCEPT
# iptables -A block -j DROP
## Jump to that chain from INPUT and FORWARD chains.
# iptables -A INPUT -j block
# iptables -A FORWARD -j block
3.3 规则的描述
        一条规则分为三部分:
        struct ipt_entry        //主要用来匹配IP头
        struct ip_match        //额外的匹配(tcp头,mac地址等)
        struct ip_target        //除缺省的动作外(如ACCEPT,DROP),可以增加
新的(如REJECT)。
3.4 代码提炼
ip_input.c:
        /*
        *         Main IP Receive routine.
        */
        int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packe
t_type *pt)
        {
        ...
        return NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, dev, NULL,
                               ip_rcv_finish);
        ...
        }
netfilter.h:
        #ifdef CONFIG_NETFILTER
        #define NF_HOOK(pf, hook, skb, indev, outdev, okfn)
       \
        (list_empty(&nf_hooks[(pf)][(hook)])
        \
        ? (okfn)(skb)
         \
        : nf_hook_slow((pf), (hook), (skb), (indev), (outdev), (okfn)))
        #else /* !CONFIG_NETFILTER */
        #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
        #endif /*CONFIG_NETFILTER*/
大的框架:"HOOK表":
        struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];        //netfilter.
c
        通过nf_register_hook和nf_unregister_hook完成添加删除工作,nf_iterate
负责执行hook上的函数。
        增加用户自定义的HOOK,参见【8】,【10】:
重要流程(建议结合netfilter hacking howto 4.1.3来看):
/* Returns one of the generic firewall policies, like NF_ACCEPT. */
unsigned int
ipt_do_table(struct sk_buff **pskb,
             unsigned int hook,
             const struct net_device *in,
             const struct net_device *out,
             struct ipt_table *table,
             void *userdata)
{
        struct ipt_entry *e;
        struct ipt_entry_target *t;
        unsigned int verdict = NF_DROP;
        table_base = (void *)table->private->entries
                + TABLE_OFFSET(table->private,
                               cpu_number_map(smp_processor_id()));
        e = get_entry(table_base, table->private->hook_entry[hook]);
        ...
        ip_packet_match(ip, indev, outdev, &e->ip, offset);
        ...
        IPT_MATCH_ITERATE(e, do_match, *pskb, in, out, offset, protohdr, dat
alen, &hotdrop)
        ...
        t = ipt_get_target(e);
        ...
        verdict = t->u.kernel.target->target(pskb, hook, in, out, t->data, u
serdata);//非标准的target走这一步
        ...
        return verdict;
}
要加强对这段话的理解(netfilter hacking howto 4.1节) :
>iptables does not register with any netfilter hooks: it relies on
>other modules to do that and feed it the packets as appropriate; a
>module must register the netfilter hooks and ip_tables separately, and
>provide the mechanism to call ip_tables when the hook is reached.
--
蒹葭苍苍白露为霜所谓伊人在水一方溯洄从之道阻且长
溯游从之宛在水中央
蒹葭萋萋白露未晞所谓伊人在水之湄溯洄从之道阻且跻
溯游从之宛在水中坻
蒹葭采采白露未已所谓伊人在水之涘溯洄从之道阻且右
溯游从之宛在水中沚
※ 来源:·幽幽黄桷兰 bbs.cqupt.edu.cn·[FROM: BlueCrystal]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店