projects/linux·blob master · refs

113 lines · 2.9 KiB

1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * xt_iprange - Netfilter module to match IP address ranges
4 *
5 * (C) 2003 Jozsef Kadlecsik <[email protected]>
6 * (C) CC Computer Consultants GmbH, 2008
7 */
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9#include <linux/module.h>
10#include <linux/skbuff.h>
11#include <linux/ip.h>
12#include <linux/ipv6.h>
13#include <linux/netfilter/x_tables.h>
14#include <linux/netfilter/xt_iprange.h>
15
16static bool
17iprange_mt4(const struct sk_buff *skb, struct xt_action_param *par)
18{
19 const struct xt_iprange_mtinfo *info = par->matchinfo;
20 const struct iphdr *iph = ip_hdr(skb);
21 bool m;
22
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);
27 if (m)
28 return false;
29 }
30 if (info->flags & IPRANGE_DST) {
31 m = ntohl(iph->daddr) < ntohl(info->dst_min.ip);
32 m |= ntohl(iph->daddr) > ntohl(info->dst_max.ip);
33 m ^= !!(info->flags & IPRANGE_DST_INV);
34 if (m)
35 return false;
36 }
37 return true;
38}
39
40static inline int
41iprange_ipv6_lt(const struct in6_addr *a, const struct in6_addr *b)
42{
43 unsigned int i;
44
45 for (i = 0; i < 4; ++i) {
46 if (a->s6_addr32[i] != b->s6_addr32[i])
47 return ntohl(a->s6_addr32[i]) < ntohl(b->s6_addr32[i]);
48 }
49
50 return 0;
51}
52
53static bool
54iprange_mt6(const struct sk_buff *skb, struct xt_action_param *par)
55{
56 const struct xt_iprange_mtinfo *info = par->matchinfo;
57 const struct ipv6hdr *iph = ipv6_hdr(skb);
58 bool m;
59
60 if (info->flags & IPRANGE_SRC) {
61 m = iprange_ipv6_lt(&iph->saddr, &info->src_min.in6);
62 m |= iprange_ipv6_lt(&info->src_max.in6, &iph->saddr);
63 m ^= !!(info->flags & IPRANGE_SRC_INV);
64 if (m)
65 return false;
66 }
67 if (info->flags & IPRANGE_DST) {
68 m = iprange_ipv6_lt(&iph->daddr, &info->dst_min.in6);
69 m |= iprange_ipv6_lt(&info->dst_max.in6, &iph->daddr);
70 m ^= !!(info->flags & IPRANGE_DST_INV);
71 if (m)
72 return false;
73 }
74 return true;
75}
76
77static struct xt_match iprange_mt_reg[] __read_mostly = {
78 {
79 .name = "iprange",
80 .revision = 1,
81 .family = NFPROTO_IPV4,
82 .match = iprange_mt4,
83 .matchsize = sizeof(struct xt_iprange_mtinfo),
84 .me = THIS_MODULE,
85 },
86 {
87 .name = "iprange",
88 .revision = 1,
89 .family = NFPROTO_IPV6,
90 .match = iprange_mt6,
91 .matchsize = sizeof(struct xt_iprange_mtinfo),
92 .me = THIS_MODULE,
93 },
94};
95
96static int __init iprange_mt_init(void)
97{
98 return xt_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
99}
100
101static void __exit iprange_mt_exit(void)
102{
103 xt_unregister_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
104}
105
106module_init(iprange_mt_init);
107module_exit(iprange_mt_exit);
108MODULE_LICENSE("GPL");
109MODULE_AUTHOR("Jozsef Kadlecsik <[email protected]>");
110MODULE_AUTHOR("Jan Engelhardt <[email protected]>");
111MODULE_DESCRIPTION("Xtables: arbitrary IPv4 range matching");
112MODULE_ALIAS("ipt_iprange");
113MODULE_ALIAS("ip6t_iprange");