1 // SPDX-License-Identifier: GPL-2.0-only
3 * xt_iprange - Netfilter module to match IP address ranges
5 * (C) 2003 Jozsef Kadlecsik <kadlec@netfilter.org>
6 * (C) CC Computer Consultants GmbH, 2008
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/module.h>
10 #include <linux/skbuff.h>
12 #include <linux/ipv6.h>
13 #include <linux/netfilter/x_tables.h>
14 #include <linux/netfilter/xt_iprange.h>
17 iprange_mt4(const struct sk_buff
*skb
, struct xt_action_param
*par
)
19 const struct xt_iprange_mtinfo
*info
= par
->matchinfo
;
20 const struct iphdr
*iph
= ip_hdr(skb
);
23 if (info
->flags
& IPRANGE_SRC
) {
24 m
= ntohl(iph
->saddr
) < ntohl(info
->src_min
.ip
);
25 m
|= ntohl(iph
->saddr
) > ntohl(info
->src_max
.ip
);
26 m
^= !!(info
->flags
& IPRANGE_SRC_INV
);
28 pr_debug("src IP %pI4 NOT in range %s%pI4-%pI4\n",
30 (info
->flags
& IPRANGE_SRC_INV
) ? "(INV) " : "",
36 if (info
->flags
& IPRANGE_DST
) {
37 m
= ntohl(iph
->daddr
) < ntohl(info
->dst_min
.ip
);
38 m
|= ntohl(iph
->daddr
) > ntohl(info
->dst_max
.ip
);
39 m
^= !!(info
->flags
& IPRANGE_DST_INV
);
41 pr_debug("dst IP %pI4 NOT in range %s%pI4-%pI4\n",
43 (info
->flags
& IPRANGE_DST_INV
) ? "(INV) " : "",
53 iprange_ipv6_lt(const struct in6_addr
*a
, const struct in6_addr
*b
)
57 for (i
= 0; i
< 4; ++i
) {
58 if (a
->s6_addr32
[i
] != b
->s6_addr32
[i
])
59 return ntohl(a
->s6_addr32
[i
]) < ntohl(b
->s6_addr32
[i
]);
66 iprange_mt6(const struct sk_buff
*skb
, struct xt_action_param
*par
)
68 const struct xt_iprange_mtinfo
*info
= par
->matchinfo
;
69 const struct ipv6hdr
*iph
= ipv6_hdr(skb
);
72 if (info
->flags
& IPRANGE_SRC
) {
73 m
= iprange_ipv6_lt(&iph
->saddr
, &info
->src_min
.in6
);
74 m
|= iprange_ipv6_lt(&info
->src_max
.in6
, &iph
->saddr
);
75 m
^= !!(info
->flags
& IPRANGE_SRC_INV
);
77 pr_debug("src IP %pI6 NOT in range %s%pI6-%pI6\n",
79 (info
->flags
& IPRANGE_SRC_INV
) ? "(INV) " : "",
85 if (info
->flags
& IPRANGE_DST
) {
86 m
= iprange_ipv6_lt(&iph
->daddr
, &info
->dst_min
.in6
);
87 m
|= iprange_ipv6_lt(&info
->dst_max
.in6
, &iph
->daddr
);
88 m
^= !!(info
->flags
& IPRANGE_DST_INV
);
90 pr_debug("dst IP %pI6 NOT in range %s%pI6-%pI6\n",
92 (info
->flags
& IPRANGE_DST_INV
) ? "(INV) " : "",
101 static struct xt_match iprange_mt_reg
[] __read_mostly
= {
105 .family
= NFPROTO_IPV4
,
106 .match
= iprange_mt4
,
107 .matchsize
= sizeof(struct xt_iprange_mtinfo
),
113 .family
= NFPROTO_IPV6
,
114 .match
= iprange_mt6
,
115 .matchsize
= sizeof(struct xt_iprange_mtinfo
),
120 static int __init
iprange_mt_init(void)
122 return xt_register_matches(iprange_mt_reg
, ARRAY_SIZE(iprange_mt_reg
));
125 static void __exit
iprange_mt_exit(void)
127 xt_unregister_matches(iprange_mt_reg
, ARRAY_SIZE(iprange_mt_reg
));
130 module_init(iprange_mt_init
);
131 module_exit(iprange_mt_exit
);
132 MODULE_LICENSE("GPL");
133 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
134 MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
135 MODULE_DESCRIPTION("Xtables: arbitrary IPv4 range matching");
136 MODULE_ALIAS("ipt_iprange");
137 MODULE_ALIAS("ip6t_iprange");