1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f72e25a8SJan Engelhardt /*
3f72e25a8SJan Engelhardt * xt_iprange - Netfilter module to match IP address ranges
4f72e25a8SJan Engelhardt *
5f72e25a8SJan Engelhardt * (C) 2003 Jozsef Kadlecsik <kadlec@netfilter.org>
61a50c5a1SJan Engelhardt * (C) CC Computer Consultants GmbH, 2008
7f72e25a8SJan Engelhardt */
8ff67e4e4SJan Engelhardt #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9f72e25a8SJan Engelhardt #include <linux/module.h>
10f72e25a8SJan Engelhardt #include <linux/skbuff.h>
11f72e25a8SJan Engelhardt #include <linux/ip.h>
121a50c5a1SJan Engelhardt #include <linux/ipv6.h>
13f72e25a8SJan Engelhardt #include <linux/netfilter/x_tables.h>
145da621f1SJan Engelhardt #include <linux/netfilter/xt_iprange.h>
15f72e25a8SJan Engelhardt
161a50c5a1SJan Engelhardt static bool
iprange_mt4(const struct sk_buff * skb,struct xt_action_param * par)1762fc8051SJan Engelhardt iprange_mt4(const struct sk_buff *skb, struct xt_action_param *par)
181a50c5a1SJan Engelhardt {
19f7108a20SJan Engelhardt const struct xt_iprange_mtinfo *info = par->matchinfo;
201a50c5a1SJan Engelhardt const struct iphdr *iph = ip_hdr(skb);
211a50c5a1SJan Engelhardt bool m;
221a50c5a1SJan Engelhardt
231a50c5a1SJan Engelhardt if (info->flags & IPRANGE_SRC) {
241a50c5a1SJan Engelhardt m = ntohl(iph->saddr) < ntohl(info->src_min.ip);
251a50c5a1SJan Engelhardt m |= ntohl(iph->saddr) > ntohl(info->src_max.ip);
266def1eb4SAlexey Dobriyan m ^= !!(info->flags & IPRANGE_SRC_INV);
271a50c5a1SJan Engelhardt if (m) {
2814d5e834SHarvey Harrison pr_debug("src IP %pI4 NOT in range %s%pI4-%pI4\n",
2914d5e834SHarvey Harrison &iph->saddr,
301a50c5a1SJan Engelhardt (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "",
31705ca147SThomas Jacob &info->src_min.ip,
3214d5e834SHarvey Harrison &info->src_max.ip);
331a50c5a1SJan Engelhardt return false;
341a50c5a1SJan Engelhardt }
351a50c5a1SJan Engelhardt }
361a50c5a1SJan Engelhardt if (info->flags & IPRANGE_DST) {
371a50c5a1SJan Engelhardt m = ntohl(iph->daddr) < ntohl(info->dst_min.ip);
381a50c5a1SJan Engelhardt m |= ntohl(iph->daddr) > ntohl(info->dst_max.ip);
396def1eb4SAlexey Dobriyan m ^= !!(info->flags & IPRANGE_DST_INV);
401a50c5a1SJan Engelhardt if (m) {
4114d5e834SHarvey Harrison pr_debug("dst IP %pI4 NOT in range %s%pI4-%pI4\n",
4214d5e834SHarvey Harrison &iph->daddr,
431a50c5a1SJan Engelhardt (info->flags & IPRANGE_DST_INV) ? "(INV) " : "",
4414d5e834SHarvey Harrison &info->dst_min.ip,
4514d5e834SHarvey Harrison &info->dst_max.ip);
461a50c5a1SJan Engelhardt return false;
471a50c5a1SJan Engelhardt }
481a50c5a1SJan Engelhardt }
491a50c5a1SJan Engelhardt return true;
501a50c5a1SJan Engelhardt }
511a50c5a1SJan Engelhardt
521a50c5a1SJan Engelhardt static inline int
iprange_ipv6_lt(const struct in6_addr * a,const struct in6_addr * b)5308b5194bSThomas Jacob iprange_ipv6_lt(const struct in6_addr *a, const struct in6_addr *b)
541a50c5a1SJan Engelhardt {
551a50c5a1SJan Engelhardt unsigned int i;
561a50c5a1SJan Engelhardt
571a50c5a1SJan Engelhardt for (i = 0; i < 4; ++i) {
5808b5194bSThomas Jacob if (a->s6_addr32[i] != b->s6_addr32[i])
5908b5194bSThomas Jacob return ntohl(a->s6_addr32[i]) < ntohl(b->s6_addr32[i]);
601a50c5a1SJan Engelhardt }
611a50c5a1SJan Engelhardt
621a50c5a1SJan Engelhardt return 0;
631a50c5a1SJan Engelhardt }
641a50c5a1SJan Engelhardt
651a50c5a1SJan Engelhardt static bool
iprange_mt6(const struct sk_buff * skb,struct xt_action_param * par)6662fc8051SJan Engelhardt iprange_mt6(const struct sk_buff *skb, struct xt_action_param *par)
671a50c5a1SJan Engelhardt {
68f7108a20SJan Engelhardt const struct xt_iprange_mtinfo *info = par->matchinfo;
691a50c5a1SJan Engelhardt const struct ipv6hdr *iph = ipv6_hdr(skb);
701a50c5a1SJan Engelhardt bool m;
711a50c5a1SJan Engelhardt
721a50c5a1SJan Engelhardt if (info->flags & IPRANGE_SRC) {
7308b5194bSThomas Jacob m = iprange_ipv6_lt(&iph->saddr, &info->src_min.in6);
7408b5194bSThomas Jacob m |= iprange_ipv6_lt(&info->src_max.in6, &iph->saddr);
756def1eb4SAlexey Dobriyan m ^= !!(info->flags & IPRANGE_SRC_INV);
766a4ddef2SThomas Jacob if (m) {
776a4ddef2SThomas Jacob pr_debug("src IP %pI6 NOT in range %s%pI6-%pI6\n",
786a4ddef2SThomas Jacob &iph->saddr,
796a4ddef2SThomas Jacob (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "",
806a4ddef2SThomas Jacob &info->src_min.in6,
816a4ddef2SThomas Jacob &info->src_max.in6);
821a50c5a1SJan Engelhardt return false;
831a50c5a1SJan Engelhardt }
846a4ddef2SThomas Jacob }
851a50c5a1SJan Engelhardt if (info->flags & IPRANGE_DST) {
8608b5194bSThomas Jacob m = iprange_ipv6_lt(&iph->daddr, &info->dst_min.in6);
8708b5194bSThomas Jacob m |= iprange_ipv6_lt(&info->dst_max.in6, &iph->daddr);
886def1eb4SAlexey Dobriyan m ^= !!(info->flags & IPRANGE_DST_INV);
896a4ddef2SThomas Jacob if (m) {
906a4ddef2SThomas Jacob pr_debug("dst IP %pI6 NOT in range %s%pI6-%pI6\n",
916a4ddef2SThomas Jacob &iph->daddr,
926a4ddef2SThomas Jacob (info->flags & IPRANGE_DST_INV) ? "(INV) " : "",
936a4ddef2SThomas Jacob &info->dst_min.in6,
946a4ddef2SThomas Jacob &info->dst_max.in6);
951a50c5a1SJan Engelhardt return false;
961a50c5a1SJan Engelhardt }
976a4ddef2SThomas Jacob }
981a50c5a1SJan Engelhardt return true;
991a50c5a1SJan Engelhardt }
1001a50c5a1SJan Engelhardt
1011a50c5a1SJan Engelhardt static struct xt_match iprange_mt_reg[] __read_mostly = {
1021a50c5a1SJan Engelhardt {
103f72e25a8SJan Engelhardt .name = "iprange",
1041a50c5a1SJan Engelhardt .revision = 1,
105ee999d8bSJan Engelhardt .family = NFPROTO_IPV4,
1061a50c5a1SJan Engelhardt .match = iprange_mt4,
1071a50c5a1SJan Engelhardt .matchsize = sizeof(struct xt_iprange_mtinfo),
1081a50c5a1SJan Engelhardt .me = THIS_MODULE,
1091a50c5a1SJan Engelhardt },
1101a50c5a1SJan Engelhardt {
1111a50c5a1SJan Engelhardt .name = "iprange",
1121a50c5a1SJan Engelhardt .revision = 1,
113ee999d8bSJan Engelhardt .family = NFPROTO_IPV6,
1141a50c5a1SJan Engelhardt .match = iprange_mt6,
1151a50c5a1SJan Engelhardt .matchsize = sizeof(struct xt_iprange_mtinfo),
1161a50c5a1SJan Engelhardt .me = THIS_MODULE,
1171a50c5a1SJan Engelhardt },
118f72e25a8SJan Engelhardt };
119f72e25a8SJan Engelhardt
iprange_mt_init(void)120f72e25a8SJan Engelhardt static int __init iprange_mt_init(void)
121f72e25a8SJan Engelhardt {
1221a50c5a1SJan Engelhardt return xt_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
123f72e25a8SJan Engelhardt }
124f72e25a8SJan Engelhardt
iprange_mt_exit(void)125f72e25a8SJan Engelhardt static void __exit iprange_mt_exit(void)
126f72e25a8SJan Engelhardt {
1271a50c5a1SJan Engelhardt xt_unregister_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
128f72e25a8SJan Engelhardt }
129f72e25a8SJan Engelhardt
130f72e25a8SJan Engelhardt module_init(iprange_mt_init);
131f72e25a8SJan Engelhardt module_exit(iprange_mt_exit);
132f72e25a8SJan Engelhardt MODULE_LICENSE("GPL");
13336d4084dSJan Engelhardt MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
13436d4084dSJan Engelhardt MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
135f72e25a8SJan Engelhardt MODULE_DESCRIPTION("Xtables: arbitrary IPv4 range matching");
13601b7a314SPhil Oester MODULE_ALIAS("ipt_iprange");
13701b7a314SPhil Oester MODULE_ALIAS("ip6t_iprange");
138