Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x. | |
3 | * | |
4 | * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling | |
5 | * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org> | |
6 | * | |
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 | * | |
11 | */ | |
12 | ||
13 | #include <linux/module.h> | |
14 | #include <linux/moduleparam.h> | |
15 | #include <linux/netfilter_ipv4/ip_tables.h> | |
c9bdd4b5 | 16 | #include <net/ip.h> |
1da177e4 LT |
17 | |
18 | MODULE_LICENSE("GPL"); | |
19 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); | |
20 | MODULE_DESCRIPTION("iptables filter table"); | |
21 | ||
6e23ae2a PM |
22 | #define FILTER_VALID_HOOKS ((1 << NF_INET_LOCAL_IN) | \ |
23 | (1 << NF_INET_FORWARD) | \ | |
24 | (1 << NF_INET_LOCAL_OUT)) | |
1da177e4 LT |
25 | |
26 | static struct | |
27 | { | |
28 | struct ipt_replace repl; | |
29 | struct ipt_standard entries[3]; | |
30 | struct ipt_error term; | |
9335f047 | 31 | } initial_table __net_initdata = { |
3c2ad469 PM |
32 | .repl = { |
33 | .name = "filter", | |
34 | .valid_hooks = FILTER_VALID_HOOKS, | |
35 | .num_entries = 4, | |
36 | .size = sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error), | |
37 | .hook_entry = { | |
6e23ae2a PM |
38 | [NF_INET_LOCAL_IN] = 0, |
39 | [NF_INET_FORWARD] = sizeof(struct ipt_standard), | |
40 | [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2, | |
3c2ad469 PM |
41 | }, |
42 | .underflow = { | |
6e23ae2a PM |
43 | [NF_INET_LOCAL_IN] = 0, |
44 | [NF_INET_FORWARD] = sizeof(struct ipt_standard), | |
45 | [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2, | |
3c2ad469 PM |
46 | }, |
47 | }, | |
48 | .entries = { | |
49 | IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_IN */ | |
50 | IPT_STANDARD_INIT(NF_ACCEPT), /* FORWARD */ | |
51 | IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */ | |
52 | }, | |
53 | .term = IPT_ERROR_INIT, /* ERROR */ | |
1da177e4 LT |
54 | }; |
55 | ||
9335f047 | 56 | static struct xt_table packet_filter = { |
1da177e4 LT |
57 | .name = "filter", |
58 | .valid_hooks = FILTER_VALID_HOOKS, | |
2e4e6a17 | 59 | .me = THIS_MODULE, |
f88e6a8a | 60 | .af = NFPROTO_IPV4, |
1da177e4 LT |
61 | }; |
62 | ||
63 | /* The work comes in here from netfilter.c. */ | |
666953df AD |
64 | static unsigned int |
65 | ipt_local_in_hook(unsigned int hook, | |
66 | struct sk_buff *skb, | |
67 | const struct net_device *in, | |
68 | const struct net_device *out, | |
69 | int (*okfn)(struct sk_buff *)) | |
70 | { | |
71 | return ipt_do_table(skb, hook, in, out, | |
48dc7865 | 72 | dev_net(in)->ipv4.iptable_filter); |
666953df AD |
73 | } |
74 | ||
1da177e4 LT |
75 | static unsigned int |
76 | ipt_hook(unsigned int hook, | |
3db05fea | 77 | struct sk_buff *skb, |
1da177e4 LT |
78 | const struct net_device *in, |
79 | const struct net_device *out, | |
80 | int (*okfn)(struct sk_buff *)) | |
81 | { | |
666953df | 82 | return ipt_do_table(skb, hook, in, out, |
48dc7865 | 83 | dev_net(in)->ipv4.iptable_filter); |
1da177e4 LT |
84 | } |
85 | ||
86 | static unsigned int | |
87 | ipt_local_out_hook(unsigned int hook, | |
3db05fea | 88 | struct sk_buff *skb, |
1da177e4 LT |
89 | const struct net_device *in, |
90 | const struct net_device *out, | |
91 | int (*okfn)(struct sk_buff *)) | |
92 | { | |
93 | /* root is playing with raw sockets. */ | |
3db05fea | 94 | if (skb->len < sizeof(struct iphdr) || |
88843104 | 95 | ip_hdrlen(skb) < sizeof(struct iphdr)) |
1da177e4 | 96 | return NF_ACCEPT; |
666953df | 97 | return ipt_do_table(skb, hook, in, out, |
48dc7865 | 98 | dev_net(out)->ipv4.iptable_filter); |
1da177e4 LT |
99 | } |
100 | ||
1999414a | 101 | static struct nf_hook_ops ipt_ops[] __read_mostly = { |
1da177e4 | 102 | { |
666953df | 103 | .hook = ipt_local_in_hook, |
1da177e4 | 104 | .owner = THIS_MODULE, |
24c232d8 | 105 | .pf = NFPROTO_IPV4, |
6e23ae2a | 106 | .hooknum = NF_INET_LOCAL_IN, |
1da177e4 LT |
107 | .priority = NF_IP_PRI_FILTER, |
108 | }, | |
109 | { | |
110 | .hook = ipt_hook, | |
111 | .owner = THIS_MODULE, | |
24c232d8 | 112 | .pf = NFPROTO_IPV4, |
6e23ae2a | 113 | .hooknum = NF_INET_FORWARD, |
1da177e4 LT |
114 | .priority = NF_IP_PRI_FILTER, |
115 | }, | |
116 | { | |
117 | .hook = ipt_local_out_hook, | |
118 | .owner = THIS_MODULE, | |
24c232d8 | 119 | .pf = NFPROTO_IPV4, |
6e23ae2a | 120 | .hooknum = NF_INET_LOCAL_OUT, |
1da177e4 LT |
121 | .priority = NF_IP_PRI_FILTER, |
122 | }, | |
123 | }; | |
124 | ||
125 | /* Default to forward because I got too much mail already. */ | |
126 | static int forward = NF_ACCEPT; | |
127 | module_param(forward, bool, 0000); | |
128 | ||
9335f047 AD |
129 | static int __net_init iptable_filter_net_init(struct net *net) |
130 | { | |
131 | /* Register table */ | |
132 | net->ipv4.iptable_filter = | |
133 | ipt_register_table(net, &packet_filter, &initial_table.repl); | |
134 | if (IS_ERR(net->ipv4.iptable_filter)) | |
135 | return PTR_ERR(net->ipv4.iptable_filter); | |
136 | return 0; | |
137 | } | |
138 | ||
139 | static void __net_exit iptable_filter_net_exit(struct net *net) | |
140 | { | |
141 | ipt_unregister_table(net->ipv4.iptable_filter); | |
142 | } | |
143 | ||
144 | static struct pernet_operations iptable_filter_net_ops = { | |
145 | .init = iptable_filter_net_init, | |
146 | .exit = iptable_filter_net_exit, | |
147 | }; | |
148 | ||
65b4b4e8 | 149 | static int __init iptable_filter_init(void) |
1da177e4 LT |
150 | { |
151 | int ret; | |
152 | ||
153 | if (forward < 0 || forward > NF_MAX_VERDICT) { | |
154 | printk("iptables forward must be 0 or 1\n"); | |
155 | return -EINVAL; | |
156 | } | |
157 | ||
158 | /* Entry 1 is the FORWARD hook */ | |
159 | initial_table.entries[1].target.verdict = -forward - 1; | |
160 | ||
9335f047 AD |
161 | ret = register_pernet_subsys(&iptable_filter_net_ops); |
162 | if (ret < 0) | |
163 | return ret; | |
1da177e4 LT |
164 | |
165 | /* Register hooks */ | |
964ddaa1 | 166 | ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops)); |
1da177e4 LT |
167 | if (ret < 0) |
168 | goto cleanup_table; | |
169 | ||
1da177e4 LT |
170 | return ret; |
171 | ||
1da177e4 | 172 | cleanup_table: |
9335f047 | 173 | unregister_pernet_subsys(&iptable_filter_net_ops); |
1da177e4 LT |
174 | return ret; |
175 | } | |
176 | ||
65b4b4e8 | 177 | static void __exit iptable_filter_fini(void) |
1da177e4 | 178 | { |
964ddaa1 | 179 | nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops)); |
9335f047 | 180 | unregister_pernet_subsys(&iptable_filter_net_ops); |
1da177e4 LT |
181 | } |
182 | ||
65b4b4e8 AM |
183 | module_init(iptable_filter_init); |
184 | module_exit(iptable_filter_fini); |