[問題] netfilter修改目的ip位址問題.
linux kernel:2.6.32
問題:自己做簡單的kernel module,想把目的地的ip位址重導向(單純想練習)
linux pc eth ip: 192.168.1.130
client eth ip: 192.168.1.3
如client ping 192.168.1.130則導向 192.168.1.2
這個module hook在NF_INET_PRE_ROUTING
以下為重導向的程式碼
static unsigned int myfunc(unsigned int hook,
struct sk_buff **pskb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *)){
struct nf_nat_range range = {
.flags = IP_NAT_RANGE_MAP_IPS | IP_NAT_RANGE_PROTO_SPECIFIED,
.min_ip = in_aton("192.168.1.2"),
.max_ip = in_aton("192.168.1.2"),
};
struct iphdr *iph = ip_hdr(pskb);
if( iph->saddr ==in_aton("192.168.1.3")) {
if(iph->protocol == IPPROTO_ICMP){
struct nf_conn *ct;
enum ip_conntrack_info ctinfo;
ct = nf_ct_get((pskb), &ctinfo);
if(!ct){
printk("ct is null\n");
return NF_ACCEPT;
}
if ( test_bit(IPS_DST_NAT_DONE_BIT, &(ct->status)) ){
printk("IPS_DST_NAT_DONE_BIT is set\n");
return NF_ACCEPT;
}
printk("nf_nat_setup_info\n");
return nf_nat_setup_info(ct, &range, hook);
}
}
}//end myfunc()
hook函式為myfunc()
hook在NF_INET_PRE_ROUTING
但是實測結果卻ping不通,cat /proc/net/ip_conntrack 的結果也有問題.
錯誤的結果如下
icmp 1 22
src=192.168.1.3 dst=192.168.1.130 type=8 code=0 id=512 packets=1 bytes=60
[UNREPLIED]
src=192.168.1.130 dst=192.168.1.2 type=0 code=0 id=0 packets=0 bytes=0
mark=0 secmark=0 use=2
但如果移除此module,
iptables -t nat -A PREROUTING -i eth2 -d 192.168.1.130 -j DNAT
--to-destination 192.168.1.2
這樣測試,就很正常,結果也符合預期
cat /proc/net/ip_conntrack 結果正常,如下,封包有redirect到192.168.1.2
icmp 1 29
src=192.168.1.3 dst=192.168.1.130 type=8 code=0 id=512 packets=8 bytes=480
src=192.168.1.2 dst=192.168.1.3 type=0 code=0 id=512 packets=7 bytes=420
mark=0 secmark=0 use=2
請問是module裡漏掉了什麼處理嗎?
※ 編輯: jasonkey123 來自: 114.33.12.243 (03/16 02:36)
推
03/16 17:44, , 1F
03/16 17:44, 1F
→
03/16 19:54, , 2F
03/16 19:54, 2F
推
03/17 00:06, , 3F
03/17 00:06, 3F
→
03/17 00:55, , 4F
03/17 00:55, 4F
推
03/17 18:53, , 5F
03/17 18:53, 5F
→
03/17 18:53, , 6F
03/17 18:53, 6F
→
03/17 18:55, , 7F
03/17 18:55, 7F
→
03/17 18:57, , 8F
03/17 18:57, 8F
※ 編輯: jasonkey123 來自: 220.133.3.213 (03/17 21:36)
→
03/17 21:50, , 9F
03/17 21:50, 9F
LinuxDev 近期熱門文章
PTT數位生活區 即時熱門文章