2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
|
|
|
|
* Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/moduleparam.h>
|
|
|
|
#include <linux/netfilter_ipv4/ip_tables.h>
|
2007-03-13 02:09:15 +03:00
|
|
|
#include <net/ip.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
|
|
|
|
MODULE_DESCRIPTION("iptables filter table");
|
|
|
|
|
2007-11-20 05:53:30 +03:00
|
|
|
#define FILTER_VALID_HOOKS ((1 << NF_INET_LOCAL_IN) | \
|
|
|
|
(1 << NF_INET_FORWARD) | \
|
|
|
|
(1 << NF_INET_LOCAL_OUT))
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
static struct
|
|
|
|
{
|
|
|
|
struct ipt_replace repl;
|
|
|
|
struct ipt_standard entries[3];
|
|
|
|
struct ipt_error term;
|
2008-01-31 15:03:23 +03:00
|
|
|
} initial_table __net_initdata = {
|
2007-05-11 01:14:16 +04:00
|
|
|
.repl = {
|
|
|
|
.name = "filter",
|
|
|
|
.valid_hooks = FILTER_VALID_HOOKS,
|
|
|
|
.num_entries = 4,
|
|
|
|
.size = sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
|
|
|
|
.hook_entry = {
|
2007-11-20 05:53:30 +03:00
|
|
|
[NF_INET_LOCAL_IN] = 0,
|
|
|
|
[NF_INET_FORWARD] = sizeof(struct ipt_standard),
|
|
|
|
[NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2,
|
2007-05-11 01:14:16 +04:00
|
|
|
},
|
|
|
|
.underflow = {
|
2007-11-20 05:53:30 +03:00
|
|
|
[NF_INET_LOCAL_IN] = 0,
|
|
|
|
[NF_INET_FORWARD] = sizeof(struct ipt_standard),
|
|
|
|
[NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2,
|
2007-05-11 01:14:16 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
.entries = {
|
|
|
|
IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_IN */
|
|
|
|
IPT_STANDARD_INIT(NF_ACCEPT), /* FORWARD */
|
|
|
|
IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */
|
|
|
|
},
|
|
|
|
.term = IPT_ERROR_INIT, /* ERROR */
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
|
|
|
|
2008-01-31 15:03:23 +03:00
|
|
|
static struct xt_table packet_filter = {
|
2005-04-17 02:20:36 +04:00
|
|
|
.name = "filter",
|
|
|
|
.valid_hooks = FILTER_VALID_HOOKS,
|
2008-04-14 11:56:03 +04:00
|
|
|
.lock = __RW_LOCK_UNLOCKED(packet_filter.lock),
|
[NETFILTER] x_tables: Abstraction layer for {ip,ip6,arp}_tables
This monster-patch tries to do the best job for unifying the data
structures and backend interfaces for the three evil clones ip_tables,
ip6_tables and arp_tables. In an ideal world we would never have
allowed this kind of copy+paste programming... but well, our world
isn't (yet?) ideal.
o introduce a new x_tables module
o {ip,arp,ip6}_tables depend on this x_tables module
o registration functions for tables, matches and targets are only
wrappers around x_tables provided functions
o all matches/targets that are used from ip_tables and ip6_tables
are now implemented as xt_FOOBAR.c files and provide module aliases
to ipt_FOOBAR and ip6t_FOOBAR
o header files for xt_matches are in include/linux/netfilter/,
include/linux/netfilter_{ipv4,ipv6} contains compatibility wrappers
around the xt_FOOBAR.h headers
Based on this patchset we're going to further unify the code,
gradually getting rid of all the layer 3 specific assumptions.
Signed-off-by: Harald Welte <laforge@netfilter.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-01-13 00:30:04 +03:00
|
|
|
.me = THIS_MODULE,
|
|
|
|
.af = AF_INET,
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* The work comes in here from netfilter.c. */
|
[NETFILTER]: ip_tables: per-netns FILTER/MANGLE/RAW tables for real
Commit 9335f047fe61587ec82ff12fbb1220bcfdd32006 aka
"[NETFILTER]: ip_tables: per-netns FILTER, MANGLE, RAW"
added per-netns _view_ of iptables rules. They were shown to user, but
ignored by filtering code. Now that it's possible to at least ping loopback,
per-netns tables can affect filtering decisions.
netns is taken in case of
PRE_ROUTING, LOCAL_IN -- from in device,
POST_ROUTING, LOCAL_OUT -- from out device,
FORWARD -- from in device which should be equal to out device's netns.
This code is relatively new, so BUG_ON was plugged.
Wrappers were added to a) keep code the same from CONFIG_NET_NS=n users
(overwhelming majority), b) consolidate code in one place -- similar
changes will be done in ipv6 and arp netfilter code.
Signed-off-by: Alexey Dobriyan <adobriyan@sw.ru>
Signed-off-by: Patrick McHardy <kaber@trash.net>
2008-04-14 11:56:02 +04:00
|
|
|
static unsigned int
|
|
|
|
ipt_local_in_hook(unsigned int hook,
|
|
|
|
struct sk_buff *skb,
|
|
|
|
const struct net_device *in,
|
|
|
|
const struct net_device *out,
|
|
|
|
int (*okfn)(struct sk_buff *))
|
|
|
|
{
|
|
|
|
return ipt_do_table(skb, hook, in, out,
|
|
|
|
nf_local_in_net(in, out)->ipv4.iptable_filter);
|
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
static unsigned int
|
|
|
|
ipt_hook(unsigned int hook,
|
2007-10-15 11:53:15 +04:00
|
|
|
struct sk_buff *skb,
|
2005-04-17 02:20:36 +04:00
|
|
|
const struct net_device *in,
|
|
|
|
const struct net_device *out,
|
|
|
|
int (*okfn)(struct sk_buff *))
|
|
|
|
{
|
[NETFILTER]: ip_tables: per-netns FILTER/MANGLE/RAW tables for real
Commit 9335f047fe61587ec82ff12fbb1220bcfdd32006 aka
"[NETFILTER]: ip_tables: per-netns FILTER, MANGLE, RAW"
added per-netns _view_ of iptables rules. They were shown to user, but
ignored by filtering code. Now that it's possible to at least ping loopback,
per-netns tables can affect filtering decisions.
netns is taken in case of
PRE_ROUTING, LOCAL_IN -- from in device,
POST_ROUTING, LOCAL_OUT -- from out device,
FORWARD -- from in device which should be equal to out device's netns.
This code is relatively new, so BUG_ON was plugged.
Wrappers were added to a) keep code the same from CONFIG_NET_NS=n users
(overwhelming majority), b) consolidate code in one place -- similar
changes will be done in ipv6 and arp netfilter code.
Signed-off-by: Alexey Dobriyan <adobriyan@sw.ru>
Signed-off-by: Patrick McHardy <kaber@trash.net>
2008-04-14 11:56:02 +04:00
|
|
|
return ipt_do_table(skb, hook, in, out,
|
|
|
|
nf_forward_net(in, out)->ipv4.iptable_filter);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int
|
|
|
|
ipt_local_out_hook(unsigned int hook,
|
2007-10-15 11:53:15 +04:00
|
|
|
struct sk_buff *skb,
|
2005-04-17 02:20:36 +04:00
|
|
|
const struct net_device *in,
|
|
|
|
const struct net_device *out,
|
|
|
|
int (*okfn)(struct sk_buff *))
|
|
|
|
{
|
|
|
|
/* root is playing with raw sockets. */
|
2007-10-15 11:53:15 +04:00
|
|
|
if (skb->len < sizeof(struct iphdr) ||
|
|
|
|
ip_hdrlen(skb) < sizeof(struct iphdr)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
if (net_ratelimit())
|
2007-05-11 01:16:56 +04:00
|
|
|
printk("iptable_filter: ignoring short SOCK_RAW "
|
|
|
|
"packet.\n");
|
2005-04-17 02:20:36 +04:00
|
|
|
return NF_ACCEPT;
|
|
|
|
}
|
|
|
|
|
[NETFILTER]: ip_tables: per-netns FILTER/MANGLE/RAW tables for real
Commit 9335f047fe61587ec82ff12fbb1220bcfdd32006 aka
"[NETFILTER]: ip_tables: per-netns FILTER, MANGLE, RAW"
added per-netns _view_ of iptables rules. They were shown to user, but
ignored by filtering code. Now that it's possible to at least ping loopback,
per-netns tables can affect filtering decisions.
netns is taken in case of
PRE_ROUTING, LOCAL_IN -- from in device,
POST_ROUTING, LOCAL_OUT -- from out device,
FORWARD -- from in device which should be equal to out device's netns.
This code is relatively new, so BUG_ON was plugged.
Wrappers were added to a) keep code the same from CONFIG_NET_NS=n users
(overwhelming majority), b) consolidate code in one place -- similar
changes will be done in ipv6 and arp netfilter code.
Signed-off-by: Alexey Dobriyan <adobriyan@sw.ru>
Signed-off-by: Patrick McHardy <kaber@trash.net>
2008-04-14 11:56:02 +04:00
|
|
|
return ipt_do_table(skb, hook, in, out,
|
|
|
|
nf_local_out_net(in, out)->ipv4.iptable_filter);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2007-12-05 12:23:00 +03:00
|
|
|
static struct nf_hook_ops ipt_ops[] __read_mostly = {
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
[NETFILTER]: ip_tables: per-netns FILTER/MANGLE/RAW tables for real
Commit 9335f047fe61587ec82ff12fbb1220bcfdd32006 aka
"[NETFILTER]: ip_tables: per-netns FILTER, MANGLE, RAW"
added per-netns _view_ of iptables rules. They were shown to user, but
ignored by filtering code. Now that it's possible to at least ping loopback,
per-netns tables can affect filtering decisions.
netns is taken in case of
PRE_ROUTING, LOCAL_IN -- from in device,
POST_ROUTING, LOCAL_OUT -- from out device,
FORWARD -- from in device which should be equal to out device's netns.
This code is relatively new, so BUG_ON was plugged.
Wrappers were added to a) keep code the same from CONFIG_NET_NS=n users
(overwhelming majority), b) consolidate code in one place -- similar
changes will be done in ipv6 and arp netfilter code.
Signed-off-by: Alexey Dobriyan <adobriyan@sw.ru>
Signed-off-by: Patrick McHardy <kaber@trash.net>
2008-04-14 11:56:02 +04:00
|
|
|
.hook = ipt_local_in_hook,
|
2005-04-17 02:20:36 +04:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.pf = PF_INET,
|
2007-11-20 05:53:30 +03:00
|
|
|
.hooknum = NF_INET_LOCAL_IN,
|
2005-04-17 02:20:36 +04:00
|
|
|
.priority = NF_IP_PRI_FILTER,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.hook = ipt_hook,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.pf = PF_INET,
|
2007-11-20 05:53:30 +03:00
|
|
|
.hooknum = NF_INET_FORWARD,
|
2005-04-17 02:20:36 +04:00
|
|
|
.priority = NF_IP_PRI_FILTER,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.hook = ipt_local_out_hook,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.pf = PF_INET,
|
2007-11-20 05:53:30 +03:00
|
|
|
.hooknum = NF_INET_LOCAL_OUT,
|
2005-04-17 02:20:36 +04:00
|
|
|
.priority = NF_IP_PRI_FILTER,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Default to forward because I got too much mail already. */
|
|
|
|
static int forward = NF_ACCEPT;
|
|
|
|
module_param(forward, bool, 0000);
|
|
|
|
|
2008-01-31 15:03:23 +03:00
|
|
|
static int __net_init iptable_filter_net_init(struct net *net)
|
|
|
|
{
|
|
|
|
/* Register table */
|
|
|
|
net->ipv4.iptable_filter =
|
|
|
|
ipt_register_table(net, &packet_filter, &initial_table.repl);
|
|
|
|
if (IS_ERR(net->ipv4.iptable_filter))
|
|
|
|
return PTR_ERR(net->ipv4.iptable_filter);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __net_exit iptable_filter_net_exit(struct net *net)
|
|
|
|
{
|
|
|
|
ipt_unregister_table(net->ipv4.iptable_filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct pernet_operations iptable_filter_net_ops = {
|
|
|
|
.init = iptable_filter_net_init,
|
|
|
|
.exit = iptable_filter_net_exit,
|
|
|
|
};
|
|
|
|
|
2006-03-29 04:37:06 +04:00
|
|
|
static int __init iptable_filter_init(void)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (forward < 0 || forward > NF_MAX_VERDICT) {
|
|
|
|
printk("iptables forward must be 0 or 1\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Entry 1 is the FORWARD hook */
|
|
|
|
initial_table.entries[1].target.verdict = -forward - 1;
|
|
|
|
|
2008-01-31 15:03:23 +03:00
|
|
|
ret = register_pernet_subsys(&iptable_filter_net_ops);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/* Register hooks */
|
2006-04-07 01:09:49 +04:00
|
|
|
ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
|
2005-04-17 02:20:36 +04:00
|
|
|
if (ret < 0)
|
|
|
|
goto cleanup_table;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
cleanup_table:
|
2008-01-31 15:03:23 +03:00
|
|
|
unregister_pernet_subsys(&iptable_filter_net_ops);
|
2005-04-17 02:20:36 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-03-29 04:37:06 +04:00
|
|
|
static void __exit iptable_filter_fini(void)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2006-04-07 01:09:49 +04:00
|
|
|
nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
|
2008-01-31 15:03:23 +03:00
|
|
|
unregister_pernet_subsys(&iptable_filter_net_ops);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2006-03-29 04:37:06 +04:00
|
|
|
module_init(iptable_filter_init);
|
|
|
|
module_exit(iptable_filter_fini);
|