blob: 7f65d18333e3ad195f8f03e6accb86c89612dd55 [file] [log] [blame]
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 'raw' table, which is the very first hooked in at PRE_ROUTING and LOCAL_OUT .
3 *
4 * Copyright (C) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5 */
6#include <linux/module.h>
7#include <linux/netfilter_ipv4/ip_tables.h>
Patrick McHardy802169a2007-05-10 14:17:36 -07008#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Patrick McHardy6e23ae22007-11-19 18:53:30 -080010#define RAW_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12static struct
13{
14 struct ipt_replace repl;
15 struct ipt_standard entries[2];
16 struct ipt_error term;
Alexey Dobriyan9335f042008-01-31 04:03:23 -080017} initial_table __net_initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 .repl = {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090019 .name = "raw",
20 .valid_hooks = RAW_VALID_HOOKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 .num_entries = 3,
22 .size = sizeof(struct ipt_standard) * 2 + sizeof(struct ipt_error),
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090023 .hook_entry = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -080024 [NF_INET_PRE_ROUTING] = 0,
25 [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard)
Patrick McHardy3c2ad462007-05-10 14:14:16 -070026 },
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090027 .underflow = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -080028 [NF_INET_PRE_ROUTING] = 0,
29 [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard)
Patrick McHardy3c2ad462007-05-10 14:14:16 -070030 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 },
32 .entries = {
Patrick McHardy3c2ad462007-05-10 14:14:16 -070033 IPT_STANDARD_INIT(NF_ACCEPT), /* PRE_ROUTING */
34 IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 },
Patrick McHardy3c2ad462007-05-10 14:14:16 -070036 .term = IPT_ERROR_INIT, /* ERROR */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
Alexey Dobriyan9335f042008-01-31 04:03:23 -080039static struct xt_table packet_raw = {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090040 .name = "raw",
41 .valid_hooks = RAW_VALID_HOOKS,
Robert P. J. Dayfdccecd2008-04-14 09:56:03 +020042 .lock = __RW_LOCK_UNLOCKED(packet_raw.lock),
Harald Welte2e4e6a12006-01-12 13:30:04 -080043 .me = THIS_MODULE,
44 .af = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045};
46
47/* The work comes in here from netfilter.c. */
48static unsigned int
49ipt_hook(unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -070050 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 const struct net_device *in,
52 const struct net_device *out,
53 int (*okfn)(struct sk_buff *))
54{
Alexey Dobriyan666953df2008-04-14 09:56:02 +020055 return ipt_do_table(skb, hook, in, out,
Alexey Dobriyan48dc7862008-10-08 11:35:01 +020056 dev_net(in)->ipv4.iptable_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
Patrick McHardy802169a2007-05-10 14:17:36 -070059static unsigned int
60ipt_local_hook(unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -070061 struct sk_buff *skb,
Patrick McHardy802169a2007-05-10 14:17:36 -070062 const struct net_device *in,
63 const struct net_device *out,
64 int (*okfn)(struct sk_buff *))
65{
66 /* root is playing with raw sockets. */
Herbert Xu3db05fe2007-10-15 00:53:15 -070067 if (skb->len < sizeof(struct iphdr) ||
Patrick McHardy88843102009-01-12 00:06:00 +000068 ip_hdrlen(skb) < sizeof(struct iphdr))
Patrick McHardy802169a2007-05-10 14:17:36 -070069 return NF_ACCEPT;
Alexey Dobriyan666953df2008-04-14 09:56:02 +020070 return ipt_do_table(skb, hook, in, out,
Alexey Dobriyan48dc7862008-10-08 11:35:01 +020071 dev_net(out)->ipv4.iptable_raw);
Patrick McHardy802169a2007-05-10 14:17:36 -070072}
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/* 'raw' is the very first table. */
Patrick McHardy19994142007-12-05 01:23:00 -080075static struct nf_hook_ops ipt_ops[] __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 {
Patrick McHardy964ddaa2006-04-06 14:09:49 -070077 .hook = ipt_hook,
78 .pf = PF_INET,
Patrick McHardy6e23ae22007-11-19 18:53:30 -080079 .hooknum = NF_INET_PRE_ROUTING,
Patrick McHardy964ddaa2006-04-06 14:09:49 -070080 .priority = NF_IP_PRI_RAW,
81 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 },
83 {
Patrick McHardy802169a2007-05-10 14:17:36 -070084 .hook = ipt_local_hook,
Patrick McHardy964ddaa2006-04-06 14:09:49 -070085 .pf = PF_INET,
Patrick McHardy6e23ae22007-11-19 18:53:30 -080086 .hooknum = NF_INET_LOCAL_OUT,
Patrick McHardy964ddaa2006-04-06 14:09:49 -070087 .priority = NF_IP_PRI_RAW,
88 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 },
90};
91
Alexey Dobriyan9335f042008-01-31 04:03:23 -080092static int __net_init iptable_raw_net_init(struct net *net)
93{
94 /* Register table */
95 net->ipv4.iptable_raw =
96 ipt_register_table(net, &packet_raw, &initial_table.repl);
97 if (IS_ERR(net->ipv4.iptable_raw))
98 return PTR_ERR(net->ipv4.iptable_raw);
99 return 0;
100}
101
102static void __net_exit iptable_raw_net_exit(struct net *net)
103{
104 ipt_unregister_table(net->ipv4.iptable_raw);
105}
106
107static struct pernet_operations iptable_raw_net_ops = {
108 .init = iptable_raw_net_init,
109 .exit = iptable_raw_net_exit,
110};
111
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800112static int __init iptable_raw_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 int ret;
115
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800116 ret = register_pernet_subsys(&iptable_raw_net_ops);
117 if (ret < 0)
118 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 /* Register hooks */
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700121 ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (ret < 0)
123 goto cleanup_table;
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return ret;
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 cleanup_table:
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800128 unregister_pernet_subsys(&iptable_raw_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return ret;
130}
131
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800132static void __exit iptable_raw_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700134 nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800135 unregister_pernet_subsys(&iptable_raw_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800138module_init(iptable_raw_init);
139module_exit(iptable_raw_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140MODULE_LICENSE("GPL");