blob: 645d6210557104d721ba75f3bfc7131520abc892 [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 */
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>
22#include <linux/if.h>
23#include <linux/netdevice.h>
24#include <linux/inetdevice.h>
25#include <linux/proc_fs.h>
26#include <net/sock.h>
27
28#include "nf_internals.h"
29
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. */
35struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
36EXPORT_SYMBOL(nf_hooks);
37static DEFINE_SPINLOCK(nf_hook_lock);
38
39int nf_register_hook(struct nf_hook_ops *reg)
40{
41 struct list_head *i;
42
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)
46 break;
47 }
48 list_add_rcu(&reg->list, i->prev);
49 spin_unlock_bh(&nf_hook_lock);
50
51 synchronize_net();
52 return 0;
53}
54EXPORT_SYMBOL(nf_register_hook);
55
56void nf_unregister_hook(struct nf_hook_ops *reg)
57{
58 spin_lock_bh(&nf_hook_lock);
59 list_del_rcu(&reg->list);
60 spin_unlock_bh(&nf_hook_lock);
61
62 synchronize_net();
63}
64EXPORT_SYMBOL(nf_unregister_hook);
65
Patrick McHardy972d1cb2006-04-06 14:09:12 -070066int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
67{
68 unsigned int i;
69 int err = 0;
70
71 for (i = 0; i < n; i++) {
72 err = nf_register_hook(&reg[i]);
73 if (err)
74 goto err;
75 }
76 return err;
77
78err:
79 if (i > 0)
80 nf_unregister_hooks(reg, i);
81 return err;
82}
83EXPORT_SYMBOL(nf_register_hooks);
84
85void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
86{
87 unsigned int i;
88
89 for (i = 0; i < n; i++)
90 nf_unregister_hook(&reg[i]);
91}
92EXPORT_SYMBOL(nf_unregister_hooks);
93
Harald Weltef6ebe772005-08-09 20:21:49 -070094unsigned int nf_iterate(struct list_head *head,
95 struct sk_buff **skb,
96 int hook,
97 const struct net_device *indev,
98 const struct net_device *outdev,
99 struct list_head **i,
100 int (*okfn)(struct sk_buff *),
101 int hook_thresh)
102{
103 unsigned int verdict;
104
105 /*
106 * The caller must not block between calls to this
107 * function because of risk of continuing from deleted element.
108 */
109 list_for_each_continue_rcu(*i, head) {
110 struct nf_hook_ops *elem = (struct nf_hook_ops *)*i;
111
112 if (hook_thresh > elem->priority)
113 continue;
114
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)
121 > NF_MAX_VERDICT)) {
122 NFDEBUG("Evil return from %p(%u).\n",
123 elem->hook, hook);
124 continue;
125 }
126#endif
127 if (verdict != NF_REPEAT)
128 return verdict;
129 *i = (*i)->prev;
130 }
131 }
132 return NF_ACCEPT;
133}
134
135
136/* Returns 1 if okfn() needs to be executed by the caller,
137 * -EPERM for NF_DROP, 0 otherwise. */
138int 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 *),
142 int hook_thresh)
143{
144 struct list_head *elem;
145 unsigned int verdict;
146 int ret = 0;
147
148 /* We may already have this, but read-locks nest anyway */
149 rcu_read_lock();
150
151 elem = &nf_hooks[pf][hook];
152next_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) {
156 ret = 1;
157 goto unlock;
158 } else if (verdict == NF_DROP) {
159 kfree_skb(*pskb);
160 ret = -EPERM;
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))
165 goto next_hook;
166 }
167unlock:
168 rcu_read_unlock();
169 return ret;
170}
171EXPORT_SYMBOL(nf_hook_slow);
172
173
174int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len)
175{
176 struct sk_buff *nskb;
177
178 if (writable_len > (*pskb)->len)
179 return 0;
180
181 /* Not exclusive use of packet? Must copy. */
182 if (skb_shared(*pskb) || skb_cloned(*pskb))
183 goto copy_skb;
184
185 return pskb_may_pull(*pskb, writable_len);
186
187copy_skb:
188 nskb = skb_copy(*pskb, GFP_ATOMIC);
189 if (!nskb)
190 return 0;
191 BUG_ON(skb_is_nonlinear(nskb));
192
193 /* Rest of kernel will get very unhappy if we pass it a
194 suddenly-orphaned skbuff */
195 if ((*pskb)->sk)
196 skb_set_owner_w(nskb, (*pskb)->sk);
197 kfree_skb(*pskb);
198 *pskb = nskb;
199 return 1;
200}
201EXPORT_SYMBOL(skb_make_writable);
202
203
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. */
207void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
208EXPORT_SYMBOL(ip_ct_attach);
209
210void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
211{
212 void (*attach)(struct sk_buff *, struct sk_buff *);
213
214 if (skb->nfct && (attach = ip_ct_attach) != NULL) {
215 mb(); /* Just to be sure: must be read before executing this */
216 attach(new, skb);
217 }
218}
219EXPORT_SYMBOL(nf_ct_attach);
220
221#ifdef CONFIG_PROC_FS
222struct proc_dir_entry *proc_net_netfilter;
223EXPORT_SYMBOL(proc_net_netfilter);
224#endif
225
226void __init netfilter_init(void)
227{
228 int i, h;
229 for (i = 0; i < NPROTO; i++) {
230 for (h = 0; h < NF_MAX_HOOKS; h++)
231 INIT_LIST_HEAD(&nf_hooks[i][h]);
232 }
233
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");
238#endif
239
240 if (netfilter_queue_init() < 0)
241 panic("cannot initialize nf_queue");
242 if (netfilter_log_init() < 0)
243 panic("cannot initialize nf_log");
244}