blob: 17f9e1cbc73ba620c0c36fe8c9de457b394698bb [file] [log] [blame]
Harald Weltef6ebe772005-08-09 20:21:49 -07001/* netfilter.c: look after the filters for various protocols.
2 * Heavily influenced by the old firewall.c by David Bonn and Alan Cox.
3 *
4 * Thanks to Rob `CmdrTaco' Malda for not influencing this code in any
5 * way.
6 *
7 * Rusty Russell (C)2000 -- This code is GPL.
8 *
9 * February 2000: Modified by James Morris to have 1 queue per protocol.
10 * 15-Mar-2000: Added NF_REPEAT --RR.
11 * 08-May-2003: Internal logging interface added by Jozsef Kadlecsik.
12 */
Harald Weltef6ebe772005-08-09 20:21:49 -070013#include <linux/kernel.h>
14#include <linux/netfilter.h>
15#include <net/protocol.h>
16#include <linux/init.h>
17#include <linux/skbuff.h>
18#include <linux/wait.h>
19#include <linux/module.h>
20#include <linux/interrupt.h>
21#include <linux/if.h>
22#include <linux/netdevice.h>
23#include <linux/inetdevice.h>
24#include <linux/proc_fs.h>
25#include <net/sock.h>
26
27#include "nf_internals.h"
28
Patrick McHardybce80322006-04-06 14:18:09 -070029static DEFINE_SPINLOCK(afinfo_lock);
30
31struct nf_afinfo *nf_afinfo[NPROTO];
32EXPORT_SYMBOL(nf_afinfo);
33
34int nf_register_afinfo(struct nf_afinfo *afinfo)
35{
36 spin_lock(&afinfo_lock);
37 rcu_assign_pointer(nf_afinfo[afinfo->family], afinfo);
38 spin_unlock(&afinfo_lock);
39 return 0;
40}
41EXPORT_SYMBOL_GPL(nf_register_afinfo);
42
43void nf_unregister_afinfo(struct nf_afinfo *afinfo)
44{
45 spin_lock(&afinfo_lock);
46 rcu_assign_pointer(nf_afinfo[afinfo->family], NULL);
47 spin_unlock(&afinfo_lock);
48 synchronize_rcu();
49}
50EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
51
Harald Weltef6ebe772005-08-09 20:21:49 -070052/* In this code, we can be waiting indefinitely for userspace to
53 * service a packet if a hook returns NF_QUEUE. We could keep a count
54 * of skbuffs queued for userspace, and not deregister a hook unless
55 * this is zero, but that sucks. Now, we simply check when the
56 * packets come back: if the hook is gone, the packet is discarded. */
57struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
58EXPORT_SYMBOL(nf_hooks);
59static DEFINE_SPINLOCK(nf_hook_lock);
60
61int nf_register_hook(struct nf_hook_ops *reg)
62{
63 struct list_head *i;
64
65 spin_lock_bh(&nf_hook_lock);
66 list_for_each(i, &nf_hooks[reg->pf][reg->hooknum]) {
67 if (reg->priority < ((struct nf_hook_ops *)i)->priority)
68 break;
69 }
70 list_add_rcu(&reg->list, i->prev);
71 spin_unlock_bh(&nf_hook_lock);
72
73 synchronize_net();
74 return 0;
75}
76EXPORT_SYMBOL(nf_register_hook);
77
78void nf_unregister_hook(struct nf_hook_ops *reg)
79{
80 spin_lock_bh(&nf_hook_lock);
81 list_del_rcu(&reg->list);
82 spin_unlock_bh(&nf_hook_lock);
83
84 synchronize_net();
85}
86EXPORT_SYMBOL(nf_unregister_hook);
87
Patrick McHardy972d1cb2006-04-06 14:09:12 -070088int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
89{
90 unsigned int i;
91 int err = 0;
92
93 for (i = 0; i < n; i++) {
94 err = nf_register_hook(&reg[i]);
95 if (err)
96 goto err;
97 }
98 return err;
99
100err:
101 if (i > 0)
102 nf_unregister_hooks(reg, i);
103 return err;
104}
105EXPORT_SYMBOL(nf_register_hooks);
106
107void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
108{
109 unsigned int i;
110
111 for (i = 0; i < n; i++)
112 nf_unregister_hook(&reg[i]);
113}
114EXPORT_SYMBOL(nf_unregister_hooks);
115
Harald Weltef6ebe772005-08-09 20:21:49 -0700116unsigned int nf_iterate(struct list_head *head,
117 struct sk_buff **skb,
118 int hook,
119 const struct net_device *indev,
120 const struct net_device *outdev,
121 struct list_head **i,
122 int (*okfn)(struct sk_buff *),
123 int hook_thresh)
124{
125 unsigned int verdict;
126
127 /*
128 * The caller must not block between calls to this
129 * function because of risk of continuing from deleted element.
130 */
131 list_for_each_continue_rcu(*i, head) {
132 struct nf_hook_ops *elem = (struct nf_hook_ops *)*i;
133
134 if (hook_thresh > elem->priority)
135 continue;
136
137 /* Optimization: we don't need to hold module
138 reference here, since function can't sleep. --RR */
139 verdict = elem->hook(hook, skb, indev, outdev, okfn);
140 if (verdict != NF_ACCEPT) {
141#ifdef CONFIG_NETFILTER_DEBUG
142 if (unlikely((verdict & NF_VERDICT_MASK)
143 > NF_MAX_VERDICT)) {
144 NFDEBUG("Evil return from %p(%u).\n",
145 elem->hook, hook);
146 continue;
147 }
148#endif
149 if (verdict != NF_REPEAT)
150 return verdict;
151 *i = (*i)->prev;
152 }
153 }
154 return NF_ACCEPT;
155}
156
157
158/* Returns 1 if okfn() needs to be executed by the caller,
159 * -EPERM for NF_DROP, 0 otherwise. */
160int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb,
161 struct net_device *indev,
162 struct net_device *outdev,
163 int (*okfn)(struct sk_buff *),
164 int hook_thresh)
165{
166 struct list_head *elem;
167 unsigned int verdict;
168 int ret = 0;
169
170 /* We may already have this, but read-locks nest anyway */
171 rcu_read_lock();
172
173 elem = &nf_hooks[pf][hook];
174next_hook:
175 verdict = nf_iterate(&nf_hooks[pf][hook], pskb, hook, indev,
176 outdev, &elem, okfn, hook_thresh);
177 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
178 ret = 1;
179 goto unlock;
180 } else if (verdict == NF_DROP) {
181 kfree_skb(*pskb);
182 ret = -EPERM;
183 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
184 NFDEBUG("nf_hook: Verdict = QUEUE.\n");
Patrick McHardy394f5452006-08-05 00:58:52 -0700185 if (!nf_queue(*pskb, elem, pf, hook, indev, outdev, okfn,
Harald Weltef6ebe772005-08-09 20:21:49 -0700186 verdict >> NF_VERDICT_BITS))
187 goto next_hook;
188 }
189unlock:
190 rcu_read_unlock();
191 return ret;
192}
193EXPORT_SYMBOL(nf_hook_slow);
194
195
196int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len)
197{
198 struct sk_buff *nskb;
199
200 if (writable_len > (*pskb)->len)
201 return 0;
202
203 /* Not exclusive use of packet? Must copy. */
204 if (skb_shared(*pskb) || skb_cloned(*pskb))
205 goto copy_skb;
206
207 return pskb_may_pull(*pskb, writable_len);
208
209copy_skb:
210 nskb = skb_copy(*pskb, GFP_ATOMIC);
211 if (!nskb)
212 return 0;
213 BUG_ON(skb_is_nonlinear(nskb));
214
215 /* Rest of kernel will get very unhappy if we pass it a
216 suddenly-orphaned skbuff */
217 if ((*pskb)->sk)
218 skb_set_owner_w(nskb, (*pskb)->sk);
219 kfree_skb(*pskb);
220 *pskb = nskb;
221 return 1;
222}
223EXPORT_SYMBOL(skb_make_writable);
224
Al Viro43bc0ca2006-11-14 21:43:23 -0800225void nf_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
226 __be32 from, __be32 to, int pseudohdr)
Patrick McHardy4cf411d2006-08-05 00:58:33 -0700227{
Al Viro43bc0ca2006-11-14 21:43:23 -0800228 __be32 diff[] = { ~from, to };
Patrick McHardy4cf411d2006-08-05 00:58:33 -0700229 if (skb->ip_summed != CHECKSUM_PARTIAL) {
Al Viro43bc0ca2006-11-14 21:43:23 -0800230 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
231 ~csum_unfold(*sum)));
Patrick McHardy4cf411d2006-08-05 00:58:33 -0700232 if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
Al Viro43bc0ca2006-11-14 21:43:23 -0800233 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
234 ~skb->csum);
Patrick McHardy4cf411d2006-08-05 00:58:33 -0700235 } else if (pseudohdr)
Al Viro43bc0ca2006-11-14 21:43:23 -0800236 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
237 csum_unfold(*sum)));
Patrick McHardy4cf411d2006-08-05 00:58:33 -0700238}
Al Viro43bc0ca2006-11-14 21:43:23 -0800239EXPORT_SYMBOL(nf_proto_csum_replace4);
Harald Weltef6ebe772005-08-09 20:21:49 -0700240
241/* This does not belong here, but locally generated errors need it if connection
242 tracking in use: without this, connection may not be in hash table, and hence
243 manufactured ICMP or RST packets will not be associated with it. */
244void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
245EXPORT_SYMBOL(ip_ct_attach);
246
247void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
248{
249 void (*attach)(struct sk_buff *, struct sk_buff *);
250
251 if (skb->nfct && (attach = ip_ct_attach) != NULL) {
252 mb(); /* Just to be sure: must be read before executing this */
253 attach(new, skb);
254 }
255}
256EXPORT_SYMBOL(nf_ct_attach);
257
258#ifdef CONFIG_PROC_FS
259struct proc_dir_entry *proc_net_netfilter;
260EXPORT_SYMBOL(proc_net_netfilter);
261#endif
262
263void __init netfilter_init(void)
264{
265 int i, h;
266 for (i = 0; i < NPROTO; i++) {
267 for (h = 0; h < NF_MAX_HOOKS; h++)
268 INIT_LIST_HEAD(&nf_hooks[i][h]);
269 }
270
271#ifdef CONFIG_PROC_FS
272 proc_net_netfilter = proc_mkdir("netfilter", proc_net);
273 if (!proc_net_netfilter)
274 panic("cannot create netfilter proc entry");
275#endif
276
277 if (netfilter_queue_init() < 0)
278 panic("cannot initialize nf_queue");
279 if (netfilter_log_init() < 0)
280 panic("cannot initialize nf_log");
281}