2 * This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x.
4 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
5 * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
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.
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/netfilter_ipv4/ip_tables.h>
17 MODULE_LICENSE("GPL");
18 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
19 MODULE_DESCRIPTION("iptables filter table");
21 #define FILTER_VALID_HOOKS ((1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) | (1 << NF_IP_LOCAL_OUT))
26 struct ipt_entry entry;
27 struct ipt_standard_target target;
30 struct ipt_error_target
32 struct ipt_entry_target target;
33 char errorname[IPT_FUNCTION_MAXNAMELEN];
38 struct ipt_entry entry;
39 struct ipt_error_target target;
44 struct ipt_replace repl;
45 struct ipt_standard entries[3];
46 struct ipt_error term;
47 } initial_table __initdata
48 = { { "filter", FILTER_VALID_HOOKS, 4,
49 sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
50 { [NF_IP_LOCAL_IN] = 0,
51 [NF_IP_FORWARD] = sizeof(struct ipt_standard),
52 [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 },
53 { [NF_IP_LOCAL_IN] = 0,
54 [NF_IP_FORWARD] = sizeof(struct ipt_standard),
55 [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 },
59 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
61 sizeof(struct ipt_entry),
62 sizeof(struct ipt_standard),
64 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
67 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
69 sizeof(struct ipt_entry),
70 sizeof(struct ipt_standard),
72 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
75 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
77 sizeof(struct ipt_entry),
78 sizeof(struct ipt_standard),
80 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
84 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
86 sizeof(struct ipt_entry),
87 sizeof(struct ipt_error),
89 { { { { IPT_ALIGN(sizeof(struct ipt_error_target)), IPT_ERROR_TARGET } },
96 static struct ipt_table packet_filter = {
98 .table = &initial_table.repl,
99 .valid_hooks = FILTER_VALID_HOOKS,
100 .lock = RW_LOCK_UNLOCKED,
104 /* The work comes in here from netfilter.c. */
106 ipt_hook(unsigned int hook,
107 struct sk_buff **pskb,
108 const struct net_device *in,
109 const struct net_device *out,
110 int (*okfn)(struct sk_buff *))
112 return ipt_do_table(pskb, hook, in, out, &packet_filter, NULL);
116 ipt_local_out_hook(unsigned int hook,
117 struct sk_buff **pskb,
118 const struct net_device *in,
119 const struct net_device *out,
120 int (*okfn)(struct sk_buff *))
122 /* root is playing with raw sockets. */
123 if ((*pskb)->len < sizeof(struct iphdr)
124 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
126 printk("ipt_hook: happy cracking.\n");
130 return ipt_do_table(pskb, hook, in, out, &packet_filter, NULL);
133 static struct nf_hook_ops ipt_ops[] = {
136 .owner = THIS_MODULE,
138 .hooknum = NF_IP_LOCAL_IN,
139 .priority = NF_IP_PRI_FILTER,
143 .owner = THIS_MODULE,
145 .hooknum = NF_IP_FORWARD,
146 .priority = NF_IP_PRI_FILTER,
149 .hook = ipt_local_out_hook,
150 .owner = THIS_MODULE,
152 .hooknum = NF_IP_LOCAL_OUT,
153 .priority = NF_IP_PRI_FILTER,
157 /* Default to forward because I got too much mail already. */
158 static int forward = NF_ACCEPT;
159 module_param(forward, bool, 0000);
161 static int __init init(void)
165 if (forward < 0 || forward > NF_MAX_VERDICT) {
166 printk("iptables forward must be 0 or 1\n");
170 /* Entry 1 is the FORWARD hook */
171 initial_table.entries[1].target.verdict = -forward - 1;
174 ret = ipt_register_table(&packet_filter);
179 ret = nf_register_hook(&ipt_ops[0]);
183 ret = nf_register_hook(&ipt_ops[1]);
187 ret = nf_register_hook(&ipt_ops[2]);
194 nf_unregister_hook(&ipt_ops[1]);
196 nf_unregister_hook(&ipt_ops[0]);
198 ipt_unregister_table(&packet_filter);
203 static void __exit fini(void)
207 for (i = 0; i < sizeof(ipt_ops)/sizeof(struct nf_hook_ops); i++)
208 nf_unregister_hook(&ipt_ops[i]);
210 ipt_unregister_table(&packet_filter);