netfilter: xtables: make use of caller family rather than target family
[deliverable/linux.git] / net / netfilter / xt_iprange.c
CommitLineData
1da177e4 1/*
f72e25a8 2 * xt_iprange - Netfilter module to match IP address ranges
1da177e4 3 *
f72e25a8 4 * (C) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1a50c5a1 5 * (C) CC Computer Consultants GmbH, 2008
1da177e4 6 *
f72e25a8
JE
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
1da177e4
LT
10 */
11#include <linux/module.h>
12#include <linux/skbuff.h>
13#include <linux/ip.h>
1a50c5a1 14#include <linux/ipv6.h>
6709dbbb 15#include <linux/netfilter/x_tables.h>
5da621f1 16#include <linux/netfilter/xt_iprange.h>
1da177e4 17
1a50c5a1 18static bool
f7108a20 19iprange_mt4(const struct sk_buff *skb, const struct xt_match_param *par)
1a50c5a1 20{
f7108a20 21 const struct xt_iprange_mtinfo *info = par->matchinfo;
1a50c5a1
JE
22 const struct iphdr *iph = ip_hdr(skb);
23 bool m;
24
25 if (info->flags & IPRANGE_SRC) {
26 m = ntohl(iph->saddr) < ntohl(info->src_min.ip);
27 m |= ntohl(iph->saddr) > ntohl(info->src_max.ip);
6def1eb4 28 m ^= !!(info->flags & IPRANGE_SRC_INV);
1a50c5a1 29 if (m) {
14d5e834
HH
30 pr_debug("src IP %pI4 NOT in range %s%pI4-%pI4\n",
31 &iph->saddr,
1a50c5a1 32 (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "",
14d5e834
HH
33 &info->src_max.ip,
34 &info->src_max.ip);
1a50c5a1
JE
35 return false;
36 }
37 }
38 if (info->flags & IPRANGE_DST) {
39 m = ntohl(iph->daddr) < ntohl(info->dst_min.ip);
40 m |= ntohl(iph->daddr) > ntohl(info->dst_max.ip);
6def1eb4 41 m ^= !!(info->flags & IPRANGE_DST_INV);
1a50c5a1 42 if (m) {
14d5e834
HH
43 pr_debug("dst IP %pI4 NOT in range %s%pI4-%pI4\n",
44 &iph->daddr,
1a50c5a1 45 (info->flags & IPRANGE_DST_INV) ? "(INV) " : "",
14d5e834
HH
46 &info->dst_min.ip,
47 &info->dst_max.ip);
1a50c5a1
JE
48 return false;
49 }
50 }
51 return true;
52}
53
54static inline int
55iprange_ipv6_sub(const struct in6_addr *a, const struct in6_addr *b)
56{
57 unsigned int i;
58 int r;
59
60 for (i = 0; i < 4; ++i) {
27ecb1ff 61 r = ntohl(a->s6_addr32[i]) - ntohl(b->s6_addr32[i]);
1a50c5a1
JE
62 if (r != 0)
63 return r;
64 }
65
66 return 0;
67}
68
69static bool
f7108a20 70iprange_mt6(const struct sk_buff *skb, const struct xt_match_param *par)
1a50c5a1 71{
f7108a20 72 const struct xt_iprange_mtinfo *info = par->matchinfo;
1a50c5a1
JE
73 const struct ipv6hdr *iph = ipv6_hdr(skb);
74 bool m;
75
76 if (info->flags & IPRANGE_SRC) {
77 m = iprange_ipv6_sub(&iph->saddr, &info->src_min.in6) < 0;
78 m |= iprange_ipv6_sub(&iph->saddr, &info->src_max.in6) > 0;
6def1eb4 79 m ^= !!(info->flags & IPRANGE_SRC_INV);
1a50c5a1
JE
80 if (m)
81 return false;
82 }
83 if (info->flags & IPRANGE_DST) {
84 m = iprange_ipv6_sub(&iph->daddr, &info->dst_min.in6) < 0;
85 m |= iprange_ipv6_sub(&iph->daddr, &info->dst_max.in6) > 0;
6def1eb4 86 m ^= !!(info->flags & IPRANGE_DST_INV);
1a50c5a1
JE
87 if (m)
88 return false;
89 }
90 return true;
91}
92
93static struct xt_match iprange_mt_reg[] __read_mostly = {
1a50c5a1
JE
94 {
95 .name = "iprange",
96 .revision = 1,
ee999d8b 97 .family = NFPROTO_IPV4,
1a50c5a1
JE
98 .match = iprange_mt4,
99 .matchsize = sizeof(struct xt_iprange_mtinfo),
100 .me = THIS_MODULE,
101 },
102 {
103 .name = "iprange",
104 .revision = 1,
ee999d8b 105 .family = NFPROTO_IPV6,
1a50c5a1
JE
106 .match = iprange_mt6,
107 .matchsize = sizeof(struct xt_iprange_mtinfo),
108 .me = THIS_MODULE,
109 },
1da177e4
LT
110};
111
d3c5ee6d 112static int __init iprange_mt_init(void)
1da177e4 113{
1a50c5a1 114 return xt_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
1da177e4
LT
115}
116
d3c5ee6d 117static void __exit iprange_mt_exit(void)
1da177e4 118{
1a50c5a1 119 xt_unregister_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
1da177e4
LT
120}
121
d3c5ee6d
JE
122module_init(iprange_mt_init);
123module_exit(iprange_mt_exit);
f72e25a8 124MODULE_LICENSE("GPL");
36d4084d
JE
125MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
126MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
f72e25a8 127MODULE_DESCRIPTION("Xtables: arbitrary IPv4 range matching");
01b7a314
PO
128MODULE_ALIAS("ipt_iprange");
129MODULE_ALIAS("ip6t_iprange");
This page took 0.497611 seconds and 5 git commands to generate.