1 // SPDX-License-Identifier: GPL-2.0-only
3 * TTL modification target for IP tables
6 * Hop Limit modification target for ip6tables
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
13 #include <linux/ipv6.h>
14 #include <net/checksum.h>
16 #include <linux/netfilter/x_tables.h>
17 #include <linux/netfilter_ipv4/ipt_TTL.h>
18 #include <linux/netfilter_ipv6/ip6t_HL.h>
22 MODULE_DESCRIPTION("Xtables: Hoplimit/TTL Limit field modification target");
23 MODULE_LICENSE("GPL");
26 ttl_tg(struct sk_buff *skb, const struct xt_action_param *par)
29 const struct ipt_TTL_info *info = par->targinfo;
32 if (skb_ensure_writable(skb, sizeof(*iph)))
42 new_ttl = iph->ttl + info->ttl;
47 new_ttl = iph->ttl - info->ttl;
56 if (new_ttl != iph->ttl) {
57 csum_replace2(&iph->check, htons(iph->ttl << 8),
66 hl_tg6(struct sk_buff *skb, const struct xt_action_param *par)
69 const struct ip6t_HL_info *info = par->targinfo;
72 if (skb_ensure_writable(skb, sizeof(*ip6h)))
79 new_hl = info->hop_limit;
82 new_hl = ip6h->hop_limit + info->hop_limit;
87 new_hl = ip6h->hop_limit - info->hop_limit;
92 new_hl = ip6h->hop_limit;
96 ip6h->hop_limit = new_hl;
101 static int ttl_tg_check(const struct xt_tgchk_param *par)
103 const struct ipt_TTL_info *info = par->targinfo;
105 if (info->mode > IPT_TTL_MAXMODE)
107 if (info->mode != IPT_TTL_SET && info->ttl == 0)
112 static int hl_tg6_check(const struct xt_tgchk_param *par)
114 const struct ip6t_HL_info *info = par->targinfo;
116 if (info->mode > IP6T_HL_MAXMODE)
118 if (info->mode != IP6T_HL_SET && info->hop_limit == 0)
123 static struct xt_target hl_tg_reg[] __read_mostly = {
127 .family = NFPROTO_IPV4,
129 .targetsize = sizeof(struct ipt_TTL_info),
131 .checkentry = ttl_tg_check,
137 .family = NFPROTO_IPV6,
139 .targetsize = sizeof(struct ip6t_HL_info),
141 .checkentry = hl_tg6_check,
146 static int __init hl_tg_init(void)
148 return xt_register_targets(hl_tg_reg, ARRAY_SIZE(hl_tg_reg));
151 static void __exit hl_tg_exit(void)
153 xt_unregister_targets(hl_tg_reg, ARRAY_SIZE(hl_tg_reg));
156 module_init(hl_tg_init);
157 module_exit(hl_tg_exit);
158 MODULE_ALIAS("ipt_TTL");
159 MODULE_ALIAS("ip6t_HL");