iptables 杂谈ACCEPT和RETURN

发布时间 2023-11-28 14:44:36作者: 付时凡

iptables 杂谈ACCEPT和RETURN

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

实验

这里是实验的情况:

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

    -N my_rule_1
    -N my_rule_2
    -A my_rule_1 -j ACCEPT
    -A my_rule_1 -j RETURN
    -A my_rule_2 -j ACCEPT
    -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         
      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会跳过当前的链,走后面的规则链。

结论

  1. ACCEPT,并不会走后面的规则链,即跳过当前的链
  2. RETURN,会跳过当前的链,走后面的规则链