blob: 40f2b9f611a289fef11f442961d5ed2b71798d7c [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
Jan Engelhardt35aad0f2009-08-24 14:56:30 +020012static const struct
Linus Torvalds1da177e2005-04-16 15:20:36 -070013{
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
Jan Engelhardt35aad0f2009-08-24 14:56:30 +020039static const struct xt_table packet_raw = {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090040 .name = "raw",
41 .valid_hooks = RAW_VALID_HOOKS,
Harald Welte2e4e6a12006-01-12 13:30:04 -080042 .me = THIS_MODULE,
Jan Engelhardtf88e6a82009-06-13 06:25:44 +020043 .af = NFPROTO_IPV4,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
45
46/* The work comes in here from netfilter.c. */
47static unsigned int
48ipt_hook(unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -070049 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 const struct net_device *in,
51 const struct net_device *out,
52 int (*okfn)(struct sk_buff *))
53{
Alexey Dobriyan666953df2008-04-14 09:56:02 +020054 return ipt_do_table(skb, hook, in, out,
Alexey Dobriyan48dc7862008-10-08 11:35:01 +020055 dev_net(in)->ipv4.iptable_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Patrick McHardy802169a2007-05-10 14:17:36 -070058static unsigned int
59ipt_local_hook(unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -070060 struct sk_buff *skb,
Patrick McHardy802169a2007-05-10 14:17:36 -070061 const struct net_device *in,
62 const struct net_device *out,
63 int (*okfn)(struct sk_buff *))
64{
65 /* root is playing with raw sockets. */
Herbert Xu3db05fe2007-10-15 00:53:15 -070066 if (skb->len < sizeof(struct iphdr) ||
Patrick McHardy88843102009-01-12 00:06:00 +000067 ip_hdrlen(skb) < sizeof(struct iphdr))
Patrick McHardy802169a2007-05-10 14:17:36 -070068 return NF_ACCEPT;
Alexey Dobriyan666953df2008-04-14 09:56:02 +020069 return ipt_do_table(skb, hook, in, out,
Alexey Dobriyan48dc7862008-10-08 11:35:01 +020070 dev_net(out)->ipv4.iptable_raw);
Patrick McHardy802169a2007-05-10 14:17:36 -070071}
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* 'raw' is the very first table. */
Patrick McHardy19994142007-12-05 01:23:00 -080074static struct nf_hook_ops ipt_ops[] __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 {
Patrick McHardy964ddaa2006-04-06 14:09:49 -070076 .hook = ipt_hook,
Jan Engelhardt24c232d2009-06-13 06:20:29 +020077 .pf = NFPROTO_IPV4,
Patrick McHardy6e23ae22007-11-19 18:53:30 -080078 .hooknum = NF_INET_PRE_ROUTING,
Patrick McHardy964ddaa2006-04-06 14:09:49 -070079 .priority = NF_IP_PRI_RAW,
80 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 },
82 {
Patrick McHardy802169a2007-05-10 14:17:36 -070083 .hook = ipt_local_hook,
Jan Engelhardt24c232d2009-06-13 06:20:29 +020084 .pf = NFPROTO_IPV4,
Patrick McHardy6e23ae22007-11-19 18:53:30 -080085 .hooknum = NF_INET_LOCAL_OUT,
Patrick McHardy964ddaa2006-04-06 14:09:49 -070086 .priority = NF_IP_PRI_RAW,
87 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 },
89};
90
Alexey Dobriyan9335f042008-01-31 04:03:23 -080091static int __net_init iptable_raw_net_init(struct net *net)
92{
93 /* Register table */
94 net->ipv4.iptable_raw =
95 ipt_register_table(net, &packet_raw, &initial_table.repl);
96 if (IS_ERR(net->ipv4.iptable_raw))
97 return PTR_ERR(net->ipv4.iptable_raw);
98 return 0;
99}
100
101static void __net_exit iptable_raw_net_exit(struct net *net)
102{
Alexey Dobriyanf54e9362010-01-18 08:25:47 +0100103 ipt_unregister_table(net, net->ipv4.iptable_raw);
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800104}
105
106static struct pernet_operations iptable_raw_net_ops = {
107 .init = iptable_raw_net_init,
108 .exit = iptable_raw_net_exit,
109};
110
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800111static int __init iptable_raw_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 int ret;
114
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800115 ret = register_pernet_subsys(&iptable_raw_net_ops);
116 if (ret < 0)
117 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 /* Register hooks */
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700120 ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (ret < 0)
122 goto cleanup_table;
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return ret;
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 cleanup_table:
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800127 unregister_pernet_subsys(&iptable_raw_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return ret;
129}
130
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800131static void __exit iptable_raw_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700133 nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800134 unregister_pernet_subsys(&iptable_raw_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800137module_init(iptable_raw_init);
138module_exit(iptable_raw_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139MODULE_LICENSE("GPL");