iptables 杂谈ACCEPT和RETURN

iptables 杂谈ACCEPT和RETURN

这两个目标,确实比较模糊。

实验

这里是实验的情况:

  1. 新建两个iptables的规则链,并且相连,如果是ACCEPT:

    -N my_rule_1
    -N my_rule_2
    -A my_rule_1 -j ACCEPT
    -A my_rule_2 -j ACCEPT
    -A parental_ctrl_0 -j my_rule_1
    -A parental_ctrl_0 -j my_rule_2
    

    查看运行的数据:

    root@openwrt:/lib/parental_control# iptables -vnL my_rule_1; iptables -vnL my_rule_2
    Chain my_rule_1 (1 references)
     pkts bytes target     prot opt in     out     source               destination         
      377 66935 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           
        0     0 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    Chain my_rule_2 (1 references)
     pkts bytes target     prot opt in     out     source               destination         
        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           
        0     0 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0
    

    可见,只有第一条链的数据包有增长,而后面的数据包不会增长。说明:ACCEPT后,并不会走后面的规则链,即跳过当前的链,在这里,起码从parental_ctrl_0跳走了,甚至可能是从FORWARD跳走了。

  2. 如果是RETURN:

    root@openwrt:/lib/parental_control# iptables -S |grep app
    -N my_rule_1
    -N my_rule_2
    -A my_rule_1 -j RETURN
    -A my_rule_2 -j RETURN
    -A parental_ctrl_0 -j my_rule_1
    -A parental_ctrl_0 -j my_rule_2
    

    再看看统计的情况:

    root@openwrt:/lib/parental_control# iptables -vnL my_rule_1; iptables -vnL my_rule_2
    Chain my_rule_1 (1 references)
     pkts bytes target     prot opt in     out     source               destination         
      280 60176 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    Chain my_rule_2 (1 references)
     pkts bytes target     prot opt in     out     source               destination         
      280 60176 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0
    

    可以看到,两条链的数据包是完全相同的。也就说明,RETURN会从当前的链返回,然后继续走后面的规则链,不会从hook点跳走。

结论

  1. ACCEPT,并不会走后面的规则链,即跳过当前的hook点
  2. RETURN,会从当前的链继续,走后面的规则链,不跳过hook点
posted @ 2023-11-28 14:43  付时凡  阅读(754)  评论(0)    收藏  举报