1 /* netfilter.c: look after the filters for various protocols.
2 * Heavily influenced by the old firewall.c by David Bonn and Alan Cox.
4 * Thanks to Rob `CmdrTaco' Malda for not influencing this code in any
7 * Rusty Russell (C)2000 -- This code is GPL.
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.
13 #include <linux/config.h>
14 #include <linux/kernel.h>
15 #include <linux/netfilter.h>
16 #include <net/protocol.h>
17 #include <linux/init.h>
18 #include <linux/skbuff.h>
19 #include <linux/wait.h>
20 #include <linux/module.h>
21 #include <linux/interrupt.h>
23 #include <linux/netdevice.h>
24 #include <linux/inetdevice.h>
25 #include <linux/proc_fs.h>
28 #include "nf_internals.h"
30 /* In this code, we can be waiting indefinitely for userspace to
31 * service a packet if a hook returns NF_QUEUE. We could keep a count
32 * of skbuffs queued for userspace, and not deregister a hook unless
33 * this is zero, but that sucks. Now, we simply check when the
34 * packets come back: if the hook is gone, the packet is discarded. */
35 struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
36 EXPORT_SYMBOL(nf_hooks);
37 static DEFINE_SPINLOCK(nf_hook_lock);
39 int nf_register_hook(struct nf_hook_ops *reg)
43 spin_lock_bh(&nf_hook_lock);
44 list_for_each(i, &nf_hooks[reg->pf][reg->hooknum]) {
45 if (reg->priority < ((struct nf_hook_ops *)i)->priority)
48 list_add_rcu(®->list, i->prev);
49 spin_unlock_bh(&nf_hook_lock);
54 EXPORT_SYMBOL(nf_register_hook);
56 void nf_unregister_hook(struct nf_hook_ops *reg)
58 spin_lock_bh(&nf_hook_lock);
59 list_del_rcu(®->list);
60 spin_unlock_bh(&nf_hook_lock);
64 EXPORT_SYMBOL(nf_unregister_hook);
66 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
71 for (i = 0; i < n; i++) {
72 err = nf_register_hook(®[i]);
80 nf_unregister_hooks(reg, i);
83 EXPORT_SYMBOL(nf_register_hooks);
85 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
89 for (i = 0; i < n; i++)
90 nf_unregister_hook(®[i]);
92 EXPORT_SYMBOL(nf_unregister_hooks);
94 unsigned int nf_iterate(struct list_head *head,
97 const struct net_device *indev,
98 const struct net_device *outdev,
100 int (*okfn)(struct sk_buff *),
103 unsigned int verdict;
106 * The caller must not block between calls to this
107 * function because of risk of continuing from deleted element.
109 list_for_each_continue_rcu(*i, head) {
110 struct nf_hook_ops *elem = (struct nf_hook_ops *)*i;
112 if (hook_thresh > elem->priority)
115 /* Optimization: we don't need to hold module
116 reference here, since function can't sleep. --RR */
117 verdict = elem->hook(hook, skb, indev, outdev, okfn);
118 if (verdict != NF_ACCEPT) {
119 #ifdef CONFIG_NETFILTER_DEBUG
120 if (unlikely((verdict & NF_VERDICT_MASK)
122 NFDEBUG("Evil return from %p(%u).\n",
127 if (verdict != NF_REPEAT)
136 /* Returns 1 if okfn() needs to be executed by the caller,
137 * -EPERM for NF_DROP, 0 otherwise. */
138 int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb,
139 struct net_device *indev,
140 struct net_device *outdev,
141 int (*okfn)(struct sk_buff *),
144 struct list_head *elem;
145 unsigned int verdict;
148 /* We may already have this, but read-locks nest anyway */
151 elem = &nf_hooks[pf][hook];
153 verdict = nf_iterate(&nf_hooks[pf][hook], pskb, hook, indev,
154 outdev, &elem, okfn, hook_thresh);
155 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
158 } else if (verdict == NF_DROP) {
161 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
162 NFDEBUG("nf_hook: Verdict = QUEUE.\n");
163 if (!nf_queue(pskb, elem, pf, hook, indev, outdev, okfn,
164 verdict >> NF_VERDICT_BITS))
171 EXPORT_SYMBOL(nf_hook_slow);
174 int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len)
176 struct sk_buff *nskb;
178 if (writable_len > (*pskb)->len)
181 /* Not exclusive use of packet? Must copy. */
182 if (skb_shared(*pskb) || skb_cloned(*pskb))
185 return pskb_may_pull(*pskb, writable_len);
188 nskb = skb_copy(*pskb, GFP_ATOMIC);
191 BUG_ON(skb_is_nonlinear(nskb));
193 /* Rest of kernel will get very unhappy if we pass it a
194 suddenly-orphaned skbuff */
196 skb_set_owner_w(nskb, (*pskb)->sk);
201 EXPORT_SYMBOL(skb_make_writable);
204 /* This does not belong here, but locally generated errors need it if connection
205 tracking in use: without this, connection may not be in hash table, and hence
206 manufactured ICMP or RST packets will not be associated with it. */
207 void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
208 EXPORT_SYMBOL(ip_ct_attach);
210 void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
212 void (*attach)(struct sk_buff *, struct sk_buff *);
214 if (skb->nfct && (attach = ip_ct_attach) != NULL) {
215 mb(); /* Just to be sure: must be read before executing this */
219 EXPORT_SYMBOL(nf_ct_attach);
221 #ifdef CONFIG_PROC_FS
222 struct proc_dir_entry *proc_net_netfilter;
223 EXPORT_SYMBOL(proc_net_netfilter);
226 void __init netfilter_init(void)
229 for (i = 0; i < NPROTO; i++) {
230 for (h = 0; h < NF_MAX_HOOKS; h++)
231 INIT_LIST_HEAD(&nf_hooks[i][h]);
234 #ifdef CONFIG_PROC_FS
235 proc_net_netfilter = proc_mkdir("netfilter", proc_net);
236 if (!proc_net_netfilter)
237 panic("cannot create netfilter proc entry");
240 if (netfilter_queue_init() < 0)
241 panic("cannot initialize nf_queue");
242 if (netfilter_log_init() < 0)
243 panic("cannot initialize nf_log");