Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 | 2 | /* |
f72e25a8 | 3 | * xt_iprange - Netfilter module to match IP address ranges |
1da177e4 | 4 | * |
f72e25a8 | 5 | * (C) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> |
1a50c5a1 | 6 | * (C) CC Computer Consultants GmbH, 2008 |
1da177e4 | 7 | */ |
ff67e4e4 | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
1da177e4 LT |
9 | #include <linux/module.h> |
10 | #include <linux/skbuff.h> | |
11 | #include <linux/ip.h> | |
1a50c5a1 | 12 | #include <linux/ipv6.h> |
6709dbbb | 13 | #include <linux/netfilter/x_tables.h> |
5da621f1 | 14 | #include <linux/netfilter/xt_iprange.h> |
1da177e4 | 15 | |
1a50c5a1 | 16 | static bool |
62fc8051 | 17 | iprange_mt4(const struct sk_buff *skb, struct xt_action_param *par) |
1a50c5a1 | 18 | { |
f7108a20 | 19 | const struct xt_iprange_mtinfo *info = par->matchinfo; |
1a50c5a1 JE |
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); | |
6def1eb4 | 26 | m ^= !!(info->flags & IPRANGE_SRC_INV); |
1a50c5a1 | 27 | if (m) { |
14d5e834 HH |
28 | pr_debug("src IP %pI4 NOT in range %s%pI4-%pI4\n", |
29 | &iph->saddr, | |
1a50c5a1 | 30 | (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "", |
705ca147 | 31 | &info->src_min.ip, |
14d5e834 | 32 | &info->src_max.ip); |
1a50c5a1 JE |
33 | return false; |
34 | } | |
35 | } | |
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); | |
6def1eb4 | 39 | m ^= !!(info->flags & IPRANGE_DST_INV); |
1a50c5a1 | 40 | if (m) { |
14d5e834 HH |
41 | pr_debug("dst IP %pI4 NOT in range %s%pI4-%pI4\n", |
42 | &iph->daddr, | |
1a50c5a1 | 43 | (info->flags & IPRANGE_DST_INV) ? "(INV) " : "", |
14d5e834 HH |
44 | &info->dst_min.ip, |
45 | &info->dst_max.ip); | |
1a50c5a1 JE |
46 | return false; |
47 | } | |
48 | } | |
49 | return true; | |
50 | } | |
51 | ||
52 | static inline int | |
08b5194b | 53 | iprange_ipv6_lt(const struct in6_addr *a, const struct in6_addr *b) |
1a50c5a1 JE |
54 | { |
55 | unsigned int i; | |
1a50c5a1 JE |
56 | |
57 | for (i = 0; i < 4; ++i) { | |
08b5194b TJ |
58 | if (a->s6_addr32[i] != b->s6_addr32[i]) |
59 | return ntohl(a->s6_addr32[i]) < ntohl(b->s6_addr32[i]); | |
1a50c5a1 JE |
60 | } |
61 | ||
62 | return 0; | |
63 | } | |
64 | ||
65 | static bool | |
62fc8051 | 66 | iprange_mt6(const struct sk_buff *skb, struct xt_action_param *par) |
1a50c5a1 | 67 | { |
f7108a20 | 68 | const struct xt_iprange_mtinfo *info = par->matchinfo; |
1a50c5a1 JE |
69 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
70 | bool m; | |
71 | ||
72 | if (info->flags & IPRANGE_SRC) { | |
08b5194b TJ |
73 | m = iprange_ipv6_lt(&iph->saddr, &info->src_min.in6); |
74 | m |= iprange_ipv6_lt(&info->src_max.in6, &iph->saddr); | |
6def1eb4 | 75 | m ^= !!(info->flags & IPRANGE_SRC_INV); |
6a4ddef2 TJ |
76 | if (m) { |
77 | pr_debug("src IP %pI6 NOT in range %s%pI6-%pI6\n", | |
78 | &iph->saddr, | |
79 | (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "", | |
80 | &info->src_min.in6, | |
81 | &info->src_max.in6); | |
1a50c5a1 | 82 | return false; |
6a4ddef2 | 83 | } |
1a50c5a1 JE |
84 | } |
85 | if (info->flags & IPRANGE_DST) { | |
08b5194b TJ |
86 | m = iprange_ipv6_lt(&iph->daddr, &info->dst_min.in6); |
87 | m |= iprange_ipv6_lt(&info->dst_max.in6, &iph->daddr); | |
6def1eb4 | 88 | m ^= !!(info->flags & IPRANGE_DST_INV); |
6a4ddef2 TJ |
89 | if (m) { |
90 | pr_debug("dst IP %pI6 NOT in range %s%pI6-%pI6\n", | |
91 | &iph->daddr, | |
92 | (info->flags & IPRANGE_DST_INV) ? "(INV) " : "", | |
93 | &info->dst_min.in6, | |
94 | &info->dst_max.in6); | |
1a50c5a1 | 95 | return false; |
6a4ddef2 | 96 | } |
1a50c5a1 JE |
97 | } |
98 | return true; | |
99 | } | |
100 | ||
101 | static struct xt_match iprange_mt_reg[] __read_mostly = { | |
1a50c5a1 JE |
102 | { |
103 | .name = "iprange", | |
104 | .revision = 1, | |
ee999d8b | 105 | .family = NFPROTO_IPV4, |
1a50c5a1 JE |
106 | .match = iprange_mt4, |
107 | .matchsize = sizeof(struct xt_iprange_mtinfo), | |
108 | .me = THIS_MODULE, | |
109 | }, | |
110 | { | |
111 | .name = "iprange", | |
112 | .revision = 1, | |
ee999d8b | 113 | .family = NFPROTO_IPV6, |
1a50c5a1 JE |
114 | .match = iprange_mt6, |
115 | .matchsize = sizeof(struct xt_iprange_mtinfo), | |
116 | .me = THIS_MODULE, | |
117 | }, | |
1da177e4 LT |
118 | }; |
119 | ||
d3c5ee6d | 120 | static int __init iprange_mt_init(void) |
1da177e4 | 121 | { |
1a50c5a1 | 122 | return xt_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg)); |
1da177e4 LT |
123 | } |
124 | ||
d3c5ee6d | 125 | static void __exit iprange_mt_exit(void) |
1da177e4 | 126 | { |
1a50c5a1 | 127 | xt_unregister_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg)); |
1da177e4 LT |
128 | } |
129 | ||
d3c5ee6d JE |
130 | module_init(iprange_mt_init); |
131 | module_exit(iprange_mt_exit); | |
f72e25a8 | 132 | MODULE_LICENSE("GPL"); |
36d4084d JE |
133 | MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); |
134 | MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>"); | |
f72e25a8 | 135 | MODULE_DESCRIPTION("Xtables: arbitrary IPv4 range matching"); |
01b7a314 PO |
136 | MODULE_ALIAS("ipt_iprange"); |
137 | MODULE_ALIAS("ip6t_iprange"); |