blob: db8bff0fb86dc711f1d422ba188a41f85a33d32f [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 Tothe2cf5ec2007-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 */
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/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
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +010026static inline bool match_type(struct net *net, const struct net_device *dev,
27 __be32 addr, u_int16_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +010029 return !!(mask & (1 << inet_dev_addr_type(net, dev, addr)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070030}
31
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080032static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020033addrtype_mt_v0(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +010035 struct net *net = dev_net(par->in ? par->in : par->out);
Jan Engelhardtf7108a22008-10-08 11:35:18 +020036 const struct ipt_addrtype_info *info = par->matchinfo;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070037 const struct iphdr *iph = ip_hdr(skb);
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070038 bool ret = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40 if (info->source)
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +010041 ret &= match_type(net, NULL, iph->saddr, info->source) ^
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -080042 info->invert_source;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 if (info->dest)
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +010044 ret &= match_type(net, NULL, iph->daddr, info->dest) ^
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -080045 info->invert_dest;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 return ret;
48}
49
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -080050static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020051addrtype_mt_v1(const struct sk_buff *skb, struct xt_action_param *par)
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -080052{
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +010053 struct net *net = dev_net(par->in ? par->in : par->out);
Jan Engelhardtf7108a22008-10-08 11:35:18 +020054 const struct ipt_addrtype_info_v1 *info = par->matchinfo;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -080055 const struct iphdr *iph = ip_hdr(skb);
56 const struct net_device *dev = NULL;
57 bool ret = true;
58
59 if (info->flags & IPT_ADDRTYPE_LIMIT_IFACE_IN)
Jan Engelhardtf7108a22008-10-08 11:35:18 +020060 dev = par->in;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -080061 else if (info->flags & IPT_ADDRTYPE_LIMIT_IFACE_OUT)
Jan Engelhardtf7108a22008-10-08 11:35:18 +020062 dev = par->out;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -080063
64 if (info->source)
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +010065 ret &= match_type(net, dev, iph->saddr, info->source) ^
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -080066 (info->flags & IPT_ADDRTYPE_INVERT_SOURCE);
67 if (ret && info->dest)
Alexey Dobriyand4ec52b2008-11-04 14:21:48 +010068 ret &= match_type(net, dev, iph->daddr, info->dest) ^
Anders Grafström46faec92008-08-18 21:29:57 -070069 !!(info->flags & IPT_ADDRTYPE_INVERT_DEST);
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -080070 return ret;
71}
72
Jan Engelhardtb0f38452010-03-19 17:16:42 +010073static int addrtype_mt_checkentry_v1(const struct xt_mtchk_param *par)
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -080074{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020075 struct ipt_addrtype_info_v1 *info = par->matchinfo;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -080076
77 if (info->flags & IPT_ADDRTYPE_LIMIT_IFACE_IN &&
78 info->flags & IPT_ADDRTYPE_LIMIT_IFACE_OUT) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010079 pr_info("both incoming and outgoing "
80 "interface limitation cannot be selected\n");
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010081 return -EINVAL;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -080082 }
83
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020084 if (par->hook_mask & ((1 << NF_INET_PRE_ROUTING) |
85 (1 << NF_INET_LOCAL_IN)) &&
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -080086 info->flags & IPT_ADDRTYPE_LIMIT_IFACE_OUT) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010087 pr_info("output interface limitation "
88 "not valid in PREROUTING and INPUT\n");
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010089 return -EINVAL;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -080090 }
91
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020092 if (par->hook_mask & ((1 << NF_INET_POST_ROUTING) |
93 (1 << NF_INET_LOCAL_OUT)) &&
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -080094 info->flags & IPT_ADDRTYPE_LIMIT_IFACE_IN) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010095 pr_info("input interface limitation "
96 "not valid in POSTROUTING and OUTPUT\n");
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010097 return -EINVAL;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -080098 }
99
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100100 return 0;
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800101}
102
103static struct xt_match addrtype_mt_reg[] __read_mostly = {
104 {
105 .name = "addrtype",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200106 .family = NFPROTO_IPV4,
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800107 .match = addrtype_mt_v0,
108 .matchsize = sizeof(struct ipt_addrtype_info),
109 .me = THIS_MODULE
110 },
111 {
112 .name = "addrtype",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200113 .family = NFPROTO_IPV4,
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800114 .revision = 1,
115 .match = addrtype_mt_v1,
116 .checkentry = addrtype_mt_checkentry_v1,
117 .matchsize = sizeof(struct ipt_addrtype_info_v1),
118 .me = THIS_MODULE
119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800122static int __init addrtype_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800124 return xt_register_matches(addrtype_mt_reg,
125 ARRAY_SIZE(addrtype_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800128static void __exit addrtype_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Laszlo Attila Tothe2cf5ec2007-12-04 23:30:18 -0800130 xt_unregister_matches(addrtype_mt_reg, ARRAY_SIZE(addrtype_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800133module_init(addrtype_mt_init);
134module_exit(addrtype_mt_exit);