先说一下nat中之前容易忽略的几点总结
a. original方向报文根据规则做了NAT转换,那么reply方向一定是根据连接跟踪conntrack表项保存的tuple做转换
NF_INET_PRE_ROUTING的DNAT和NF_INET_POST_ROUTING
的SNAT是成对工作的。
比如在NF_INET_PRE_ROUTING设置了DNAT规则,original方向的
数据包会在NF_INET_PRE_ROUTING做DNAT,那么reply的数据
包一定是在NF_INET_POST_ROUTING做SNAT(根据conntrack的
记录,不是根据规则)。
相反的,如果在NF_INET_POST_ROUTING设置了SNAT规则,
original方向的数据包会在NF_INET_POST_ROUTING做SNAT那
么reply的数据包一定是在NF_INET_PRE_ROUTING做DNAT(根据
conntrack的记录,并不是根据规则)。
NF_INET_LOCAL_OUT的DNAT和NF_INET_LOCAL_IN的SNAT也是如此。
b. 不管 SNAT 还是 DNAT,original方向的tuple是不变的。
//SNAT 规则
iptables -A POSTROUTING -t nat -s 10.10.10.8 -j SNAT --to-source 192.168.10.2
//SNAT前的tuple
original tuple:10.10.10.8:2222 -> 192.168.10.4:22
reply tuple: 192.168.10.4:22 ->10.10.10.8:2222
//SNAT后的tuple,只有reply的变了
original tuple:10.10.10.8:2222 -> 192.168.10.4:22
reply tuple: 192.168.10.4:22 ->192.168.10.2:2222
//DNAT
iptables -A OUTPUT -t nat -d 192.168.10.4 -j DNAT --to-destination 10.10.10.12
//DNAT前的tuple
original tuple:10.10.10.8:2222 -> 192.168.10.4:22
reply tuple: 192.168.10.4:22 ->10.10.10.8:2222
//DNAT后的tuple,只有reply tuple变了
original tuple:10.10.10.8:2222 -> 192.168.10.4:22
reply tuple: 10.10.10.12:22 ->10.10.10.8:2222
c. 只有数据流的首包才需要查找NAT规则进行匹配,不管有没有匹配到NAT规则,都会将数据流对应的ct->status的IPS_SRC_NAT_DONE和IPS_DST_NAT_DONE位置1,对于后续数据包,只要判断ct->status的这两位设置了,就可以跳过匹配NAT规则流程。当然对于匹配到NAT规则需要做转换的数据包,其ct->status还会将IPS_SRC_NAT和IPS_DST_NAT位置1。
d. netfilter OUTPUT链与路由的位置关系
先查标准路由,再在output链中做DNAT(nf_nat_ipv4_local_fn),再调用ip_route_me_harder重新查找路由,因为目的ip可能已经变了。
route---->output(DNAT)---->reroute
注册nat hook函数
module_init(iptable_nat_init);
static int __init iptable_nat_init(void)
{
//注册pernet函数,其init函数会初始化nf_nat_ipv4_table
register_pernet_subsys(&iptable_nat_net_ops);
//注册nat的hook函数
nf_register_hooks(nf_nat_ipv4_ops, ARRAY_SIZE(nf_nat_ipv4_ops));
return 0;
}
在iptable_nat_net_init中,生成默认的四个规则
static struct pernet_operations iptable_nat_net_ops = {
.init = iptable_nat_net_init,
.exit = iptable_nat_net_exit,
};
static const struct xt_table nf_nat_ipv4_table = {
.name = "nat",
.valid_hooks = (1 << NF_INET_PRE_ROUTING) |
(1 << NF_INET_POST_ROUTING) |
(1 << NF_INET_LOCAL_OUT) |
(1 << NF_INET_LOCAL_IN),
.me = THIS_MODULE,
.af = NFPROTO_IPV4,
};
static int __net_init iptable_nat_net_init(struct net *net)
{
struct ipt_replace *repl;
repl = ipt_alloc_initial_table(&nf_nat_ipv4_table);
net->ipv4.nat_table = ipt_register_table(net, &nf_nat_ipv4_table, repl);
kfree(repl);
return PTR_ERR_OR_ZERO(net->ipv4.nat_table);
}
NAT在四个hook点生效,分别对应四条链,每条链默认有一条rule,内存布局如下,初始化时没有用户添加的rule,可暂时忽略图中用户添加的rule部分,所以hook_entry和underflow指向同样的位置。
注册nat的hook函数
static struct nf_hook_ops nf_nat_ipv4_ops[] __read_mostly = {
/* Before packet filtering, change destination */
{
.hook = iptable_nat_ipv4_in,
.owner = THIS_MODULE,
.pf = NFPROTO_IPV4,
.hooknum = NF_INET_PRE_ROUTING,
.priority = NF_IP_PRI_NAT_DST,
},
/* After packet filtering, change source */
{
.hook = iptable_nat_ipv4_out,
.owner = THIS_MODULE,
.pf = NFPROTO_IPV4,
.hooknum = NF_INET_POST_ROUTING,
.priority = NF_IP_PRI_NAT_SRC,
},
/* Before packet filtering, change destination */
{
.hook = iptable_nat_ipv4_local_fn,
.owner = THIS_MODULE,
.pf = NFPROTO_IPV4,
.hooknum = NF_INET_LOCAL_OUT,
.priority = NF_IP_PRI_NAT_DST,
},
/* After packet filtering, change source */
{
.hook = iptable_nat_ipv4_fn,
.owner = THIS_MODULE,
.pf = NFPROTO_IPV4,
.hooknum = NF_INET_LOCAL_IN,
.priority = NF_IP_PRI_NAT_SRC,
},
};
注册的四个hook函数在netfilter中的位置如下
四个hook函数都会调用nf_nat_ipv4_fn,此函数实现了nat的核心功能,对于数据包的ctinfo为IP_CT_RELATED,IP_CT_RELATED_REPLY和IP_CT_NEW状态的查找nat表,因为这些状态表示是数据流的首包,如果查到nat规则,则执行target,将ct reply的tuple根据规则进行修改。在nf_nat_packet中根据tuple信息对报文做nat转换。
NF_INET_PRE_ROUTING 的dst nat和 NF_INET_POST_ROUTING 的src nat是成对工作的。
比如在NF_INET_PRE_ROUTING 设置了dst nat规则,reply的数据包一定是在NF_INET_POST_ROUTING 做src nat(根据conntrack的记录,不是根据规则)。
相反的,如果在NF_INET_POST_ROUTING 设置了src nat规则,那么reply的数据包一定是在NF_INET_PRE_ROUTING 做dst nat(根据conntrack的记录,并不是根据规则)。
NF_INET_LOCAL_OUT的dst nat和NF_INET_LOCAL_IN的src nat也是成对工作的。
nat处理流程
以 iptable_nat_ipv4_out 为例
static unsigned int iptable_nat_ipv4_out(const struct nf_hook_ops *ops,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
//iptable_nat_do_chain调用ipt_do_table在net->ipv4.nat_table
//中匹配规则,并执行SNAT或者DNAT等target
return nf_nat_ipv4_out(ops, skb, in, out, iptable_nat_do_chain);
}
//做个报文长度检查后执行核心函数nf_nat_ipv4_fn
unsigned int
nf_nat_ipv4_out(const struct nf_hook_ops *ops, struct sk_buff *skb,
const struct net_device *in, const struct net_device *out,
unsigned int (*do_chain)(const struct nf_hook_ops *ops,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
struct nf_conn *ct))
{
unsigned int ret;
/* root is playing with raw sockets. */
if (skb->len < sizeof(struct iphdr) ||
ip_hdrlen(skb) < sizeof(struct iphdr))
return NF_ACCEPT;
//nat处理核心函数,看下面的注释
ret = nf_nat_ipv4_fn(ops, skb, in, out, do_chain);
...
return ret;
}
ct->status的位IPS_SRC_NAT_DONE和IPS_DST_NAT_DONE用来判断属于此ct的数据包是否经过NAT处理了。经过NAT处理指的是经过匹配NAT规则的流程,但是不一定匹配到NAT规则。对于匹配不到NAT规则的数据流,后续数据包也不需要再查找NAT规则,所以根据IPS_SRC_NAT_DONE和IPS_DST_NAT_DONE这两位是否置1来判断。
而ct->status的位IPS_SRC_NAT和IPS_DST_NAT不止说明数据流经过NAT规则处理,还说明属于此ct的数据包需要根据NAT规则修改数据包。
unsigned int
nf_nat_ipv4_fn(const struct nf_hook_ops *ops, struct sk_buff *skb,
const struct net_device *in, const struct net_device *out,
unsigned int (*do_chain)(const struct nf_hook_ops *ops,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
struct nf_conn *ct))
{
struct nf_conn *ct;
enum ip_conntrack_info ctinfo;
struct nf_conn_nat *nat;
/* maniptype == SRC for postrouting. */
//根据hook点判断出需要做SNAT还是DNAT。
//在NF_INET_POST_ROUTING和NF_INET_LOCAL_IN做SNAT
//在NF_INET_PRE_ROUTING和NF_INET_LOCAL_OUT做DNAT
enum nf_nat_manip_type maniptype = HOOK2MANIP(ops->hooknum);
/* We never see fragments: conntrack defrags on pre-routing
* and local-out, and nf_nat_out protects post-routing.
*/
NF_CT_ASSERT(!ip_is_fragment(ip_hdr(skb)));
ct = nf_ct_get(skb, &ctinfo);
/* Can't track? It's not due to stress, or conntrack would
* have dropped it. Hence it's the user's responsibilty to
* packet filter it out, or implement conntrack/NAT for that
* protocol. 8) --RR
*/
if (!ct)
return NF_ACCEPT;
/* Don't try to NAT if this packet is not conntracked */
//如果不是track的包不做处理
if (nf_ct_is_untracked(ct))
return NF_ACCEPT;
//将nat添加到ct扩展空间
nat = nf_ct_nat_ext_add(ct);
if (nat == NULL)
return NF_ACCEPT;
//根据ctinfo处理
switch (ctinfo) {
case IP_CT_RELATED:
case IP_CT_RELATED_REPLY:
if (ip_hdr(skb)->protocol == IPPROTO_ICMP) {
if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
ops->hooknum))
return NF_DROP;
else
return NF_ACCEPT;
}
/* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
case IP_CT_NEW:
/* Seen it before? This can happen for loopback, retrans,
* or local packets.
*/
//如果ct->status的IPS_SRC_NAT_DONE或者
//IPS_DST_NAT_DONE位没有置1,则说明报文还没有经过nat处理
if (!nf_nat_initialized(ct, maniptype)) {
unsigned int ret;
//do_chain为函数iptable_nat_do_chain,其调用
//ipt_do_table在net->ipv4.nat_table中匹配规则,并
//执行snat,dnat等target,核心函数是
//nf_nat_setup_info用来对匹配nat规则的数据包执行
//nat处理,并将ct->status的IPS_SRC_NAT_DONE
//或者IPS_DST_NAT_DONE位置1,如果确实需要修
//改数据包,则将ct->status的IPS_DST_NAT或者
//IPS_SRC_NAT位置1
ret = do_chain(ops, skb, in, out, ct);
if (ret != NF_ACCEPT)
return ret;
//再次判断是否经过nat处理。
//如果nat target处理过了,则break
if (nf_nat_initialized(ct, HOOK2MANIP(ops->hooknum)))
break;
//对于那些没有匹配到nat规则的数据包,会使用数据
//包中的ip构造一个range,也执行
//nf_nat_setup_info,但是因为range的ip和数据包ip
//是一样的,所以不会设置IPS_DST_NAT或者
//IPS_SRC_NAT。
ret = nf_nat_alloc_null_binding(ct, ops->hooknum);
if (ret != NF_ACCEPT)
return ret;
} else {
pr_debug("Already setup manip %s for ct %p\n",
maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST",
ct);
if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, out))
goto oif_changed;
}
break;
default:
/* ESTABLISHED */
NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
ctinfo == IP_CT_ESTABLISHED_REPLY);
if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, out))
goto oif_changed;
}
//不管有没有匹配到nat规则,都会经过此函数处理。
//根据ct->status的IPS_DST_NAT或者IPS_SRC_NAT,
//判断需不需要修改数据包。
return nf_nat_packet(ct, ctinfo, ops->hooknum, skb);
oif_changed:
nf_ct_kill_acct(ct, ctinfo, skb);
return NF_DROP;
}
匹配上nat规则的数据包执行nat target,以snat为例
static unsigned int
xt_snat_target_v0(struct sk_buff *skb, const struct xt_action_param *par)
{
const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
struct nf_nat_range range;
enum ip_conntrack_info ctinfo;
struct nf_conn *ct;
ct = nf_ct_get(skb, &ctinfo);
NF_CT_ASSERT(ct != NULL &&
(ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
ctinfo == IP_CT_RELATED_REPLY));
//将nat规则的参数转换到range中
xt_nat_convert_range(&range, &mr->range[0]);
//根据range修改tuple
return nf_nat_setup_info(ct, &range, NF_NAT_MANIP_SRC);
}
//将nat规则的参数转换到range中
static void xt_nat_convert_range(struct nf_nat_range *dst,
const struct nf_nat_ipv4_range *src)
{
memset(&dst->min_addr, 0, sizeof(dst->min_addr));
memset(&dst->max_addr, 0, sizeof(dst->max_addr));
dst->flags = src->flags;
dst->min_addr.ip = src->min_ip;
dst->max_addr.ip = src->max_ip;
dst->min_proto = src->min;
dst->max_proto = src->max;
}
//核心函数,根据target的参数获取ip或者port,并修改ct的reply tuple,original tuple是不变的。
unsigned int
nf_nat_setup_info(struct nf_conn *ct,
const struct nf_nat_range *range,
enum nf_nat_manip_type maniptype)
{
struct net *net = nf_ct_net(ct);
struct nf_conntrack_tuple curr_tuple, new_tuple;
struct nf_conn_nat *nat;
/* nat helper or nfctnetlink also setup binding */
nat = nf_ct_nat_ext_add(ct);
if (nat == NULL)
return NF_ACCEPT;
NF_CT_ASSERT(maniptype == NF_NAT_MANIP_SRC ||
maniptype == NF_NAT_MANIP_DST);
BUG_ON(nf_nat_initialized(ct, maniptype));
/* What we've got will look like inverse of reply. Normally
* this is what is in the conntrack, except for prior
* manipulations (future optimization: if num_manips == 0,
* orig_tp = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
*/
nf_ct_invert_tuplepr(&curr_tuple,
&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
//根据range获取唯一的tuple
get_unique_tuple(&new_tuple, &curr_tuple, range, ct, maniptype);
//如果新的tuple和当前tuple不一样,说明确实需要做nat
if (!nf_ct_tuple_equal(&new_tuple, &curr_tuple)) {
struct nf_conntrack_tuple reply;
/* Alter conntrack table so will recognize replies. */
//获取新tuple的invert tuple
nf_ct_invert_tuplepr(&reply, &new_tuple);
//修改ct中reply方向的tuple为invert的tuple。original的tuple是不变的
nf_conntrack_alter_reply(ct, &reply);
//设置下面的标志位,说明确定需要修改数据包
/* Non-atomic: we own this at the moment. */
if (maniptype == NF_NAT_MANIP_SRC)
ct->status |= IPS_SRC_NAT;
else
ct->status |= IPS_DST_NAT;
if (nfct_help(ct))
nfct_seqadj_ext_add(ct);
}
if (maniptype == NF_NAT_MANIP_SRC) {
unsigned int srchash;
srchash = hash_by_src(net, nf_ct_zone(ct),
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
spin_lock_bh(&nf_nat_lock);
/* nf_conntrack_alter_reply might re-allocate extension aera */
nat = nfct_nat(ct);
nat->ct = ct;
hlist_add_head_rcu(&nat->bysource,
&net->ct.nat_bysource[srchash]);
spin_unlock_bh(&nf_nat_lock);
}
/* It's done. */
if (maniptype == NF_NAT_MANIP_DST)
ct->status |= IPS_DST_NAT_DONE;
else
ct->status |= IPS_SRC_NAT_DONE;
return NF_ACCEPT;
}
根据ct->status的IPS_DST_NAT或者IPS_SRC_NAT,
判断需不需要修改数据包。
/* Do packet manipulations according to nf_nat_setup_info. */
unsigned int nf_nat_packet(struct nf_conn *ct,
enum ip_conntrack_info ctinfo,
unsigned int hooknum,
struct sk_buff *skb)
{
const struct nf_nat_l3proto *l3proto;
const struct nf_nat_l4proto *l4proto;
enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
unsigned long statusbit;
enum nf_nat_manip_type mtype = HOOK2MANIP(hooknum);
if (mtype == NF_NAT_MANIP_SRC)
statusbit = IPS_SRC_NAT;
else
statusbit = IPS_DST_NAT;
//original方向的数据包做SNAT,那reply方向的数据包肯定需要
//做DNAT。比如在original方向会在POSTROUTING hook点对
//数据包做SNAT,并且设置ct->status为IPS_SRC_NAT,那么
//reply方向的DNAT应该是在PREROUTING做的,但是不可能
//在PREROUTING上也设置一条DNAT规则,那DNAT如何做
//呢?实现方法就是通过这里的两个if。上面的if中,因为hook点
//是PREROUTING,所以mtype为NF_NAT_MANIP_DST,
//statusbit为IPS_DST_NAT,又根据下面这个if,如果是reply方
//向,则对statusbit做异或操作,statusbit结果是
//IPS_SRC_NAT,ct->status也为IPS_SRC_NAT,这样才会继
//续执行,根据mtype NF_NAT_MANIP_DST修改数据包的目的ip。
/* Invert if this is reply dir. */
if (dir == IP_CT_DIR_REPLY)
statusbit ^= IPS_NAT_MASK;
/* Non-atomic: these bits don't change. */
if (ct->status & statusbit) {
struct nf_conntrack_tuple target;
//获取当前方向的反方向的tuple的revert tuple
/* We are aiming to look like inverse of other direction. */
nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple);
l3proto = __nf_nat_l3proto_find(target.src.l3num);
l4proto = __nf_nat_l4proto_find(target.src.l3num,
target.dst.protonum);
//根据上面获取的tuple和mtype类型修改数据包
if (!l3proto->manip_pkt(skb, 0, l4proto, &target, mtype))
return NF_DROP;
}
return NF_ACCEPT;
}
举例说明
- snat
client:10.10.10.8
server:192.168.10.4
client访问server时,在client上有一条SNAT规则如下:
iptables -A POSTROUTING -t nat -s 10.10.10.8 -j SNAT --to-source 192.168.10.2
在client主机上,数据流如下:10.10.10.8:2222 > 192.168.10.4:22
经过conntrack模块后,ct的tuple如下:
original tuple:10.10.10.8:2222 -> 192.168.10.4:22
reply tuple: 192.168.10.4:22 ->10.10.10.8:2222
经过POSTROUING的nat hook点时,查找到这条SNAT规则,执行其target,设置ct->status为IPS_SRC_NAT,复制一份original的tuple,将此tuple源ip换成--to-source指定的ip,再将此tuple revert后的值赋值给reply的tuple,此时ct的tuple如下,original的tuple是没变化的,reply的目的ip变成 --to-source指定的ip
original tuple:10.10.10.8:2222 -> 192.168.10.4:22
reply tuple: 192.168.10.4:22 ->192.168.10.2:2222
修改完ct的reply tuple后,在函数nf_nat_packet中,mtype为NF_NAT_MANIP_SRC,所以statusbit = IPS_SRC_NAT,dir为IP_CT_DIR_ORIGINAL,所以ct->status & statusbit 为1,所以需要修改数据包。
首先获取reply方向tuple的revert tuple:192.168.10.2:2222->192.168.10.4:22,
调用 manip_pkt 修改数据包,因为mtype为NF_NAT_MANIP_SRC,所以修改源ip为 192.168.10.2
if (ct->status & statusbit) {
struct nf_conntrack_tuple target;
/* We are aiming to look like inverse of other direction. */
nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple);
l3proto = __nf_nat_l3proto_find(target.src.l3num);
l4proto = __nf_nat_l4proto_find(target.src.l3num,
target.dst.protonum);
if (!l3proto->manip_pkt(skb, 0, l4proto, &target, mtype))
return NF_DROP;
}
所以最后从client发出去的数据流为:192.168.10.2:2222 -> 192.168.10.4:22。
从server返回的数据流为:192.168.10.4:22 -> 192.168.10.2:2222
在client上经过conntrack时,可以查找到ct表项,并且为reply方向,所以设置ctinfo = IP_CT_ESTABLISHED_REPLY,并且设置 ct->status 为 IPS_SEEN_REPLY_BIT。
然后在PREROUTING的nat hook点,此时ctinfo为IP_CT_ESTABLISHED_REPLY,所以不用查找nat表,直接执行nf_nat_packet,
此函数中,mtype为NF_NAT_MANIP_DST,所以statusbit = IPS_DST_NAT,dir为IP_CT_DIR_REPLY,所以执行 statusbit ^= IPS_NAT_MASK 后 statusbit为IPS_SRC_NAT,所以ct->status & statusbit 为1,所以需要修改数据包。
首先获取orginal方向tuple的revert tuple:192.168.10.4:22->10.10.10.8:2222,
调用 manip_pkt 修改数据包,因为mtype为NF_NAT_MANIP_DST,所以修改目的ip为 10.10.10.8.
最后进入client的数据流为:192.168.10.4:22 -> 10.10.10.8:2222.
- dnat
client:10.10.10.8
server:10.10.10.12
client访问server时,在client上有一条DNAT规则如下:
iptables -A OUTPUT -t nat -d 192.168.10.4 -j DNAT --to-destination 10.10.10.12
在client主机上,数据流如下:10.10.10.8:2222 > 192.168.10.4:22
经过conntrack模块后,ct的tuple如下:
original tuple:10.10.10.8:2222 -> 192.168.10.4:22
reply tuple: 192.168.10.4:22 ->10.10.10.8:2222
经过OUTPUT的nat hook点时,查找到这条DNAT规则,执行其target,设置ct->status为IPS_DST_NAT,复制一份original的tuple,将此tuple目的ip换成--to-destination指定的ip,再将此tuple revert后的值赋值给reply的tuple,此时ct的tuple如下,original的tuple是没变化的,reply的源ip变成 --to-destination指定的ip
original tuple:10.10.10.8:2222 -> 192.168.10.4:22
reply tuple: 10.10.10.12:22 ->10.10.10.8:2222
修改完ct的reply tuple后,在函数nf_nat_packet中,mtype为NF_NAT_MANIP_DST,所以statusbit = IPS_DST_NAT,dir为IP_CT_DIR_ORIGINAL,statusbit 仍然为IPS_DST_NAT,所以ct->status & statusbit 的结果为1,所以需要修改数据包。
首先获取reply方向tuple的revert tuple:10.10.10.8:2222->10.10.10.12:22,
调用 manip_pkt 修改数据包,因为mtype为NF_NAT_MANIP_DST,所以修改目的ip为 10.10.10.12
if (ct->status & statusbit) {
struct nf_conntrack_tuple target;
/* We are aiming to look like inverse of other direction. */
nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple);
l3proto = __nf_nat_l3proto_find(target.src.l3num);
l4proto = __nf_nat_l4proto_find(target.src.l3num,
target.dst.protonum);
if (!l3proto->manip_pkt(skb, 0, l4proto, &target, mtype))
return NF_DROP;
}
所以最后从client发出去的数据流为:10.10.10.8:2222 -> 10.10.10.12:22。
从server返回的数据流为:10.10.10.12:22 -> 10.10.10.8:2222
在client上经过conntrack时,可以查找到ct表项,并且为reply方向,所以设置ctinfo = IP_CT_ESTABLISHED_REPLY,并且设置 ct->status 为 IPS_SEEN_REPLY_BIT。
然后在INPUT的nat hook点,此时ctinfo为IP_CT_ESTABLISHED_REPLY,所以不用查找nat表,直接执行nf_nat_packet,
此函数中,mtype为NF_NAT_MANIP_SRC,所以statusbit = IPS_SRC_NAT,dir为IP_CT_DIR_REPLY,所以执行 statusbit ^= IPS_NAT_MASK 后 statusbit为IPS_DST_NAT,所以ct->status & statusbit 为1,所以需要修改数据包。
首先获取orginal方向tuple的revert tuple:192.168.10.4:22->10.10.10.8:2222,
调用 manip_pkt 修改数据包,因为mtype为NF_NAT_MANIP_SRC,所以修改目的ip为 192.168.10.4
最后进入client的数据流为:192.168.10.4:22 -> 10.10.10.8:2222.