blob: b46626cddd933022d796c2f4e0f54ddd2c1ae8e1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jan Engelhardtf72e25a2008-01-14 23:42:47 -08002 * xt_iprange - Netfilter module to match IP address ranges
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Jan Engelhardtf72e25a2008-01-14 23:42:47 -08004 * (C) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -08005 * (C) CC Computer Consultants GmbH, 2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Jan Engelhardtf72e25a2008-01-14 23:42:47 -08007 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/skbuff.h>
14#include <linux/ip.h>
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080015#include <linux/ipv6.h>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080016#include <linux/netfilter/x_tables.h>
Jan Engelhardt5da621f2008-02-07 17:57:11 -080017#include <linux/netfilter/xt_iprange.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080019static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020020iprange_mt4(const struct sk_buff *skb, struct xt_action_param *par)
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080021{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020022 const struct xt_iprange_mtinfo *info = par->matchinfo;
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080023 const struct iphdr *iph = ip_hdr(skb);
24 bool m;
25
26 if (info->flags & IPRANGE_SRC) {
27 m = ntohl(iph->saddr) < ntohl(info->src_min.ip);
28 m |= ntohl(iph->saddr) > ntohl(info->src_max.ip);
Alexey Dobriyan6def1eb2008-10-20 03:32:21 -070029 m ^= !!(info->flags & IPRANGE_SRC_INV);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080030 if (m) {
Harvey Harrison14d5e832008-10-31 00:54:29 -070031 pr_debug("src IP %pI4 NOT in range %s%pI4-%pI4\n",
32 &iph->saddr,
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080033 (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "",
Thomas Jacob705ca142011-01-27 10:56:32 +010034 &info->src_min.ip,
Harvey Harrison14d5e832008-10-31 00:54:29 -070035 &info->src_max.ip);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080036 return false;
37 }
38 }
39 if (info->flags & IPRANGE_DST) {
40 m = ntohl(iph->daddr) < ntohl(info->dst_min.ip);
41 m |= ntohl(iph->daddr) > ntohl(info->dst_max.ip);
Alexey Dobriyan6def1eb2008-10-20 03:32:21 -070042 m ^= !!(info->flags & IPRANGE_DST_INV);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080043 if (m) {
Harvey Harrison14d5e832008-10-31 00:54:29 -070044 pr_debug("dst IP %pI4 NOT in range %s%pI4-%pI4\n",
45 &iph->daddr,
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080046 (info->flags & IPRANGE_DST_INV) ? "(INV) " : "",
Harvey Harrison14d5e832008-10-31 00:54:29 -070047 &info->dst_min.ip,
48 &info->dst_max.ip);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080049 return false;
50 }
51 }
52 return true;
53}
54
55static inline int
Thomas Jacob08b51942011-01-24 21:35:36 +010056iprange_ipv6_lt(const struct in6_addr *a, const struct in6_addr *b)
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080057{
58 unsigned int i;
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080059
60 for (i = 0; i < 4; ++i) {
Thomas Jacob08b51942011-01-24 21:35:36 +010061 if (a->s6_addr32[i] != b->s6_addr32[i])
62 return ntohl(a->s6_addr32[i]) < ntohl(b->s6_addr32[i]);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080063 }
64
65 return 0;
66}
67
68static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020069iprange_mt6(const struct sk_buff *skb, struct xt_action_param *par)
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080070{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020071 const struct xt_iprange_mtinfo *info = par->matchinfo;
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080072 const struct ipv6hdr *iph = ipv6_hdr(skb);
73 bool m;
74
75 if (info->flags & IPRANGE_SRC) {
Thomas Jacob08b51942011-01-24 21:35:36 +010076 m = iprange_ipv6_lt(&iph->saddr, &info->src_min.in6);
77 m |= iprange_ipv6_lt(&info->src_max.in6, &iph->saddr);
Alexey Dobriyan6def1eb2008-10-20 03:32:21 -070078 m ^= !!(info->flags & IPRANGE_SRC_INV);
Thomas Jacob6a4ddef2011-01-28 19:33:13 +010079 if (m) {
80 pr_debug("src IP %pI6 NOT in range %s%pI6-%pI6\n",
81 &iph->saddr,
82 (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "",
83 &info->src_min.in6,
84 &info->src_max.in6);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080085 return false;
Thomas Jacob6a4ddef2011-01-28 19:33:13 +010086 }
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080087 }
88 if (info->flags & IPRANGE_DST) {
Thomas Jacob08b51942011-01-24 21:35:36 +010089 m = iprange_ipv6_lt(&iph->daddr, &info->dst_min.in6);
90 m |= iprange_ipv6_lt(&info->dst_max.in6, &iph->daddr);
Alexey Dobriyan6def1eb2008-10-20 03:32:21 -070091 m ^= !!(info->flags & IPRANGE_DST_INV);
Thomas Jacob6a4ddef2011-01-28 19:33:13 +010092 if (m) {
93 pr_debug("dst IP %pI6 NOT in range %s%pI6-%pI6\n",
94 &iph->daddr,
95 (info->flags & IPRANGE_DST_INV) ? "(INV) " : "",
96 &info->dst_min.in6,
97 &info->dst_max.in6);
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -080098 return false;
Thomas Jacob6a4ddef2011-01-28 19:33:13 +010099 }
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800100 }
101 return true;
102}
103
104static struct xt_match iprange_mt_reg[] __read_mostly = {
105 {
106 .name = "iprange",
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800107 .revision = 1,
Jan Engelhardtee999d82008-10-08 11:35:01 +0200108 .family = NFPROTO_IPV4,
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800109 .match = iprange_mt4,
110 .matchsize = sizeof(struct xt_iprange_mtinfo),
111 .me = THIS_MODULE,
112 },
113 {
114 .name = "iprange",
115 .revision = 1,
Jan Engelhardtee999d82008-10-08 11:35:01 +0200116 .family = NFPROTO_IPV6,
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800117 .match = iprange_mt6,
118 .matchsize = sizeof(struct xt_iprange_mtinfo),
119 .me = THIS_MODULE,
120 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800123static int __init iprange_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800125 return xt_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800128static void __exit iprange_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Jan Engelhardt1a50c5a2008-01-14 23:43:03 -0800130 xt_unregister_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800133module_init(iprange_mt_init);
134module_exit(iprange_mt_exit);
Jan Engelhardtf72e25a2008-01-14 23:42:47 -0800135MODULE_LICENSE("GPL");
Jan Engelhardt36d4084d2009-06-12 18:58:19 +0200136MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
137MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
Jan Engelhardtf72e25a2008-01-14 23:42:47 -0800138MODULE_DESCRIPTION("Xtables: arbitrary IPv4 range matching");
Phil Oester01b7a312008-05-13 23:27:48 -0700139MODULE_ALIAS("ipt_iprange");
140MODULE_ALIAS("ip6t_iprange");