Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * IP tables module for matching the value of the TTL
0004  * (C) 2000,2001 by Harald Welte <laforge@netfilter.org>
0005  *
0006  * Hop Limit matching module
0007  * (C) 2001-2002 Maciej Soltysiak <solt@dns.toxicfilms.tv>
0008  */
0009 
0010 #include <linux/ip.h>
0011 #include <linux/ipv6.h>
0012 #include <linux/module.h>
0013 #include <linux/skbuff.h>
0014 
0015 #include <linux/netfilter/x_tables.h>
0016 #include <linux/netfilter_ipv4/ipt_ttl.h>
0017 #include <linux/netfilter_ipv6/ip6t_hl.h>
0018 
0019 MODULE_AUTHOR("Maciej Soltysiak <solt@dns.toxicfilms.tv>");
0020 MODULE_DESCRIPTION("Xtables: Hoplimit/TTL field match");
0021 MODULE_LICENSE("GPL");
0022 MODULE_ALIAS("ipt_ttl");
0023 MODULE_ALIAS("ip6t_hl");
0024 
0025 static bool ttl_mt(const struct sk_buff *skb, struct xt_action_param *par)
0026 {
0027     const struct ipt_ttl_info *info = par->matchinfo;
0028     const u8 ttl = ip_hdr(skb)->ttl;
0029 
0030     switch (info->mode) {
0031     case IPT_TTL_EQ:
0032         return ttl == info->ttl;
0033     case IPT_TTL_NE:
0034         return ttl != info->ttl;
0035     case IPT_TTL_LT:
0036         return ttl < info->ttl;
0037     case IPT_TTL_GT:
0038         return ttl > info->ttl;
0039     }
0040 
0041     return false;
0042 }
0043 
0044 static bool hl_mt6(const struct sk_buff *skb, struct xt_action_param *par)
0045 {
0046     const struct ip6t_hl_info *info = par->matchinfo;
0047     const struct ipv6hdr *ip6h = ipv6_hdr(skb);
0048 
0049     switch (info->mode) {
0050     case IP6T_HL_EQ:
0051         return ip6h->hop_limit == info->hop_limit;
0052     case IP6T_HL_NE:
0053         return ip6h->hop_limit != info->hop_limit;
0054     case IP6T_HL_LT:
0055         return ip6h->hop_limit < info->hop_limit;
0056     case IP6T_HL_GT:
0057         return ip6h->hop_limit > info->hop_limit;
0058     }
0059 
0060     return false;
0061 }
0062 
0063 static struct xt_match hl_mt_reg[] __read_mostly = {
0064     {
0065         .name       = "ttl",
0066         .revision   = 0,
0067         .family     = NFPROTO_IPV4,
0068         .match      = ttl_mt,
0069         .matchsize  = sizeof(struct ipt_ttl_info),
0070         .me         = THIS_MODULE,
0071     },
0072     {
0073         .name       = "hl",
0074         .revision   = 0,
0075         .family     = NFPROTO_IPV6,
0076         .match      = hl_mt6,
0077         .matchsize  = sizeof(struct ip6t_hl_info),
0078         .me         = THIS_MODULE,
0079     },
0080 };
0081 
0082 static int __init hl_mt_init(void)
0083 {
0084     return xt_register_matches(hl_mt_reg, ARRAY_SIZE(hl_mt_reg));
0085 }
0086 
0087 static void __exit hl_mt_exit(void)
0088 {
0089     xt_unregister_matches(hl_mt_reg, ARRAY_SIZE(hl_mt_reg));
0090 }
0091 
0092 module_init(hl_mt_init);
0093 module_exit(hl_mt_exit);