blob: e60995e4c20c449e5c76d737153013925fcc6d68 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * iptables module to match inet_addr_type() of an ip.
3 *
4 * Copyright (c) 2004 Patrick McHardy <kaber@trash.net>
Laszlo Attila Tothe2cf5ecb2007-12-04 23:30:18 -08005 * (C) 2007 Laszlo Attila Toth <panther@balabit.hu>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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.
10 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/skbuff.h>
15#include <linux/netdevice.h>
16#include <linux/ip.h>
17#include <net/route.h>
18
19#include <linux/netfilter_ipv4/ipt_addrtype.h>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080020#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22MODULE_LICENSE("GPL");
23MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080024MODULE_DESCRIPTION("Xtables: address type match for IPv4");
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Laszlo Attila Tothe2cf5ecb2007-12-04 23:30:18 -080026static inline bool match_type(const struct net_device *dev, __be32 addr,
27 u_int16_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Eric W. Biederman6b175b22008-01-10 03:25:28 -080029 return !!(mask & (1 << inet_dev_addr_type(&init_net, dev, addr)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070030}
31
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080032static bool
Jan Engelhardtf7108a202008-10-08 11:35:18 +020033addrtype_mt_v0(const struct sk_buff *skb, const struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Jan Engelhardtf7108a202008-10-08 11:35:18 +020035 const struct ipt_addrtype_info *info = par->matchinfo;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070036 const struct iphdr *iph = ip_hdr(skb);
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070037 bool ret = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39 if (info->source)
Laszlo Attila Tothe2cf5ecb2007-12-04 23:30:18 -080040 ret &= match_type(NULL, iph->saddr, info->source) ^
41 info->invert_source;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 if (info->dest)
Laszlo Attila Tothe2cf5ecb2007-12-04 23:30:18 -080043 ret &= match_type(NULL, iph->daddr, info->dest) ^
44 info->invert_dest;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return ret;
47}
48
Laszlo Attila Tothe2cf5ecb2007-12-04 23:30:18 -080049static bool
Jan Engelhardtf7108a202008-10-08 11:35:18 +020050addrtype_mt_v1(const struct sk_buff *skb, const struct xt_match_param *par)
Laszlo Attila Tothe2cf5ecb2007-12-04 23:30:18 -080051{
Jan Engelhardtf7108a202008-10-08 11:35:18 +020052 const struct ipt_addrtype_info_v1 *info = par->matchinfo;
Laszlo Attila Tothe2cf5ecb2007-12-04 23:30:18 -080053 const struct iphdr *iph = ip_hdr(skb);
54 const struct net_device *dev = NULL;
55 bool ret = true;
56
57 if (info->flags & IPT_ADDRTYPE_LIMIT_IFACE_IN)
Jan Engelhardtf7108a202008-10-08 11:35:18 +020058 dev = par->in;
Laszlo Attila Tothe2cf5ecb2007-12-04 23:30:18 -080059 else if (info->flags & IPT_ADDRTYPE_LIMIT_IFACE_OUT)
Jan Engelhardtf7108a202008-10-08 11:35:18 +020060 dev = par->out;
Laszlo Attila Tothe2cf5ecb2007-12-04 23:30:18 -080061
62 if (info->source)
63 ret &= match_type(dev, iph->saddr, info->source) ^
64 (info->flags & IPT_ADDRTYPE_INVERT_SOURCE);
65 if (ret && info->dest)
66 ret &= match_type(dev, iph->daddr, info->dest) ^
Anders Grafström46faec92008-08-18 21:29:57 -070067 !!(info->flags & IPT_ADDRTYPE_INVERT_DEST);
Laszlo Attila Tothe2cf5ecb2007-12-04 23:30:18 -080068 return ret;
69}
70
71static bool
72addrtype_mt_checkentry_v1(const char *tablename, const void *ip_void,
73 const struct xt_match *match, void *matchinfo,
74 unsigned int hook_mask)
75{
76 struct ipt_addrtype_info_v1 *info = matchinfo;
77
78 if (info->flags & IPT_ADDRTYPE_LIMIT_IFACE_IN &&
79 info->flags & IPT_ADDRTYPE_LIMIT_IFACE_OUT) {
80 printk(KERN_ERR "ipt_addrtype: both incoming and outgoing "
81 "interface limitation cannot be selected\n");
82 return false;
83 }
84
85 if (hook_mask & (1 << NF_INET_PRE_ROUTING | 1 << NF_INET_LOCAL_IN) &&
86 info->flags & IPT_ADDRTYPE_LIMIT_IFACE_OUT) {
87 printk(KERN_ERR "ipt_addrtype: output interface limitation "
88 "not valid in PRE_ROUTING and INPUT\n");
89 return false;
90 }
91
92 if (hook_mask & (1 << NF_INET_POST_ROUTING | 1 << NF_INET_LOCAL_OUT) &&
93 info->flags & IPT_ADDRTYPE_LIMIT_IFACE_IN) {
94 printk(KERN_ERR "ipt_addrtype: input interface limitation "
95 "not valid in POST_ROUTING and OUTPUT\n");
96 return false;
97 }
98
99 return true;
100}
101
102static struct xt_match addrtype_mt_reg[] __read_mostly = {
103 {
104 .name = "addrtype",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200105 .family = NFPROTO_IPV4,
Laszlo Attila Tothe2cf5ecb2007-12-04 23:30:18 -0800106 .match = addrtype_mt_v0,
107 .matchsize = sizeof(struct ipt_addrtype_info),
108 .me = THIS_MODULE
109 },
110 {
111 .name = "addrtype",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200112 .family = NFPROTO_IPV4,
Laszlo Attila Tothe2cf5ecb2007-12-04 23:30:18 -0800113 .revision = 1,
114 .match = addrtype_mt_v1,
115 .checkentry = addrtype_mt_checkentry_v1,
116 .matchsize = sizeof(struct ipt_addrtype_info_v1),
117 .me = THIS_MODULE
118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800121static int __init addrtype_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Laszlo Attila Tothe2cf5ecb2007-12-04 23:30:18 -0800123 return xt_register_matches(addrtype_mt_reg,
124 ARRAY_SIZE(addrtype_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800127static void __exit addrtype_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Laszlo Attila Tothe2cf5ecb2007-12-04 23:30:18 -0800129 xt_unregister_matches(addrtype_mt_reg, ARRAY_SIZE(addrtype_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800132module_init(addrtype_mt_init);
133module_exit(addrtype_mt_exit);