|
楼主 |
发表于 2007-7-21 15:37:20
|
显示全部楼层
linux防火墙实现技术比较(2)
四 规则
4.0 综述
4.1 ipchains
man ipfw可以看到这一段的详细解释。关键数据结构如下:
规则链用ip_chain结构来表示,缺省有input,ouptput,forward三条链。在配置规则的时候,也可以添加新的链。每条链事实上就是一组相关的规则,以链表的形式存储。
struct ip_chain
{
ip_chainlabel label; /* Defines the label for each block */
struct ip_chain *next; /* Pointer to next block */
struct ip_fwkernel *chain; /* Pointer to first rule in block */
__u32 refcount; /* Number of refernces to block */
int policy; /* Default rule for chain. Only *
* used in built in chains */
struct ip_reent reent[0]; /* Actually several of these */
};
每条规则用一个ip_fwkernel结构表示:
struct ip_fwkernel
{
struct ip_fw ipfw;
struct ip_fwkernel *next; /* where to go next if current
* rule doesn't match */
struct ip_chain *branch; /* which branch to jump to if
* current rule matches */
int simplebranch; /* Use this if branch == NULL */
struct ip_counters counters[0]; /* Actually several of these */
};
ip_fwkernel中的一个重要部分就是ip_fw,用来表示待匹配的数据包消息:
struct ip_fw
{
struct in_addr fw_src, fw_dst; /* Source and destination IP addr */
struct in_addr fw_smsk, fw_dmsk; /* Mask for src and dest IP addr */
__u32 fw_mark; /* ID to stamp on packet */
__u16 fw_proto; /* Protocol, 0 = ANY */
__u16 fw_flg; /* Flags word */
__u16 fw_invflg; /* Inverse flags */
__u16 fw_spts[2]; /* Source port range. */
__u16 fw_dpts[2]; /* Destination port range. */
__u16 fw_redirpt; /* Port to redirect to. */
__u16 fw_outputsize; /* Max amount to output to
NETLINK */
char fw_vianame[IFNAMSIZ]; /* name of interface "via" */
__u8 fw_tosand, fw_tosxor; /* Revised packet priority */
};
2.2内核中网络包与规则的实际匹配在ip_fw_check中进行。
4.2 iptables
一条规则分为三部分:
struct ipt_entry file://主要用来匹配IP头
struct ip_match file://额外的匹配(tcp头,mac地址等)
struct ip_target file://除缺省的动作外(如ACCEPT,DROP),可以增加新的(如REJECT)。
man iptable:
>A firewall rule specifies criteria for a packet, and a
>target. If the packet does not match, the next rule in
>the chain is the examined; if it does match, then the next
>rule is specified by the value of the target, which can be
>the name of a user-defined chain, or one of the special
>values ACCEPT, DROP, QUEUE, or RETURN.
2.4内核中网络包与规则的实际匹配在ip_do_table中进行。这段代码的流程在
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, datalen, &hotdrop)
...
t = ipt_get_target(e);
...
verdict = t->u.kernel.target->target(pskb, hook, in, out, t->data, userdata);//非标准的target走这一步
...
return verdict;
}
流程:
--->NF_HOOK();(/include/linux/netfilter.h)
--->nf_hook_slow;(/net/core/netfilter.c)
--->nf_iterate();(/net/core/netfilter.c)
--->然后运行登记的函数;如果你希望有一套ipt_entry结构规则,并将它放到table里,你此时便可调用ipt_do_table来匹配。
在2.4内核中,规则本身也是可扩展的,体现可自己定义并添加新的ip_match和ip_target上。
4.2 FW1
未作分析。 |
|