HL目标帮助信息如下,可对IPv6报文的hoplimit进行设置、减少或者增加。
# ip6tables --jump HL -h
HL target options
--hl-set value Set HL to <value 0-255>
--hl-dec value Decrement HL by <value 1-255>
--hl-inc value Increment HL by <value 1-255>
如下在主机3ffe::2上配置策略,丢弃IPv6头部hoplimit值等于2的报文。。在另外的主机上执行ping操作,指定ttl为2,此时由于ttl等于策略配置的值2,ping不通。
# ip6tables -t filter -A INPUT -m hl --hl-eq 2 -j DROP
在主机3ffe::2上配置策略,将IPv6头部hl值增加3。此时在执行ping操作,由于ttl在增加之后为5,未匹配以上丢弃策略,ping通。
# iptables -t mangle -A PREROUTING -j TTL --ttl-inc 3
#
# iptables -L -t mangle -n -v
Chain PREROUTING (policy ACCEPT 147 packets, 19766 bytes)
pkts bytes target prot opt in out source destination
147 19766 TTL all -- * * 0.0.0.0/0 0.0.0.0/0 TTL increment by 3
HL目标
函数xt_register_targets注册TTL目标结构hl_tg_reg。
static struct xt_target hl_tg_reg[] __read_mostly = {
{
.name = "HL",
.revision = 0,
.family = NFPROTO_IPV6,
.target = hl_tg6,
.targetsize = sizeof(struct ip6t_HL_info),
.table = "mangle",
.checkentry = hl_tg6_check,
.me = THIS_MODULE,
},
};
static int __init hl_tg_init(void)
{
return xt_register_targets(hl_tg_reg, ARRAY_SIZE(hl_tg_reg));
参数检查函数如下,目前仅支持hl设置,增加和减少三种模式,其它模式则非法。如果模式为增加或者减少hl值,那么hl的增加或减少量值不能为0。
static int hl_tg6_check(const struct xt_tgchk_param *par)
{
const struct ip6t_HL_info *info = par->targinfo;
if (info->mode > IP6T_HL_MAXMODE)
return -EINVAL;
if (info->mode != IP6T_HL_SET && info->hop_limit == 0)
return -EINVAL;
return 0;
根据不同的模式,计算新的nl值。对于增加模式,如果新的hl值大于255,设定为255。对于减少模式,如果新的ttl值小于0,设定为0。最后,使用新的hl值填充IPv6头部的hop_limit字段。
65 static unsigned int
66 hl_tg6(struct sk_buff *skb, const struct xt_action_param *par)
67 {
68 struct ipv6hdr *ip6h;
69 const struct ip6t_HL_info *info = par->targinfo;
71
72 if (skb_ensure_writable(skb, sizeof(*ip6h)))
73 return NF_DROP;
75 ip6h = ipv6_hdr(skb);
76
77 switch (info->mode) {
78 case IP6T_HL_SET:
79 new_hl = info->hop_limit;
80 break;
81 case IP6T_HL_INC:
82 new_hl = ip6h->hop_limit + info->hop_limit;
83 if (new_hl > 255)
84 new_hl = 255;
85 break;
86 case IP6T_HL_DEC:
87 new_hl = ip6h->hop_limit - info->hop_limit;
88 if (new_hl < 0)
89 new_hl = 0;
90 break;
91 default:
92 new_hl = ip6h->hop_limit;
93 break;
94 }
96 ip6h->hop_limit = new_hl;
内核版本 5.10