blob: 716603f05c02b6a3762a2c523915d03545a15138 [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>
Patrick McHardyd486dd12007-02-12 11:09:55 -080025#include <linux/mutex.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070026#include <net/sock.h>
27
28#include "nf_internals.h"
29
Patrick McHardyd486dd12007-02-12 11:09:55 -080030static DEFINE_MUTEX(afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070031
Martin Josefssone2b76062006-11-29 02:35:04 +010032struct nf_afinfo *nf_afinfo[NPROTO] __read_mostly;
Patrick McHardybce80322006-04-06 14:18:09 -070033EXPORT_SYMBOL(nf_afinfo);
34
35int nf_register_afinfo(struct nf_afinfo *afinfo)
36{
Patrick McHardyd486dd12007-02-12 11:09:55 -080037 int err;
38
39 err = mutex_lock_interruptible(&afinfo_mutex);
40 if (err < 0)
41 return err;
Patrick McHardybce80322006-04-06 14:18:09 -070042 rcu_assign_pointer(nf_afinfo[afinfo->family], afinfo);
Patrick McHardyd486dd12007-02-12 11:09:55 -080043 mutex_unlock(&afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070044 return 0;
45}
46EXPORT_SYMBOL_GPL(nf_register_afinfo);
47
48void nf_unregister_afinfo(struct nf_afinfo *afinfo)
49{
Patrick McHardyd486dd12007-02-12 11:09:55 -080050 mutex_lock(&afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070051 rcu_assign_pointer(nf_afinfo[afinfo->family], NULL);
Patrick McHardyd486dd12007-02-12 11:09:55 -080052 mutex_unlock(&afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070053 synchronize_rcu();
54}
55EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
56
Harald Weltef6ebe772005-08-09 20:21:49 -070057/* In this code, we can be waiting indefinitely for userspace to
58 * service a packet if a hook returns NF_QUEUE. We could keep a count
59 * of skbuffs queued for userspace, and not deregister a hook unless
60 * this is zero, but that sucks. Now, we simply check when the
61 * packets come back: if the hook is gone, the packet is discarded. */
Martin Josefssone2b76062006-11-29 02:35:04 +010062struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS] __read_mostly;
Harald Weltef6ebe772005-08-09 20:21:49 -070063EXPORT_SYMBOL(nf_hooks);
64static DEFINE_SPINLOCK(nf_hook_lock);
65
66int nf_register_hook(struct nf_hook_ops *reg)
67{
68 struct list_head *i;
69
70 spin_lock_bh(&nf_hook_lock);
71 list_for_each(i, &nf_hooks[reg->pf][reg->hooknum]) {
72 if (reg->priority < ((struct nf_hook_ops *)i)->priority)
73 break;
74 }
75 list_add_rcu(&reg->list, i->prev);
76 spin_unlock_bh(&nf_hook_lock);
Harald Weltef6ebe772005-08-09 20:21:49 -070077 return 0;
78}
79EXPORT_SYMBOL(nf_register_hook);
80
81void nf_unregister_hook(struct nf_hook_ops *reg)
82{
83 spin_lock_bh(&nf_hook_lock);
84 list_del_rcu(&reg->list);
85 spin_unlock_bh(&nf_hook_lock);
86
87 synchronize_net();
88}
89EXPORT_SYMBOL(nf_unregister_hook);
90
Patrick McHardy972d1cb2006-04-06 14:09:12 -070091int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
92{
93 unsigned int i;
94 int err = 0;
95
96 for (i = 0; i < n; i++) {
97 err = nf_register_hook(&reg[i]);
98 if (err)
99 goto err;
100 }
101 return err;
102
103err:
104 if (i > 0)
105 nf_unregister_hooks(reg, i);
106 return err;
107}
108EXPORT_SYMBOL(nf_register_hooks);
109
110void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
111{
112 unsigned int i;
113
114 for (i = 0; i < n; i++)
115 nf_unregister_hook(&reg[i]);
116}
117EXPORT_SYMBOL(nf_unregister_hooks);
118
Harald Weltef6ebe772005-08-09 20:21:49 -0700119unsigned int nf_iterate(struct list_head *head,
120 struct sk_buff **skb,
121 int hook,
122 const struct net_device *indev,
123 const struct net_device *outdev,
124 struct list_head **i,
125 int (*okfn)(struct sk_buff *),
126 int hook_thresh)
127{
128 unsigned int verdict;
129
130 /*
131 * The caller must not block between calls to this
132 * function because of risk of continuing from deleted element.
133 */
134 list_for_each_continue_rcu(*i, head) {
135 struct nf_hook_ops *elem = (struct nf_hook_ops *)*i;
136
137 if (hook_thresh > elem->priority)
138 continue;
139
140 /* Optimization: we don't need to hold module
141 reference here, since function can't sleep. --RR */
142 verdict = elem->hook(hook, skb, indev, outdev, okfn);
143 if (verdict != NF_ACCEPT) {
144#ifdef CONFIG_NETFILTER_DEBUG
145 if (unlikely((verdict & NF_VERDICT_MASK)
146 > NF_MAX_VERDICT)) {
147 NFDEBUG("Evil return from %p(%u).\n",
148 elem->hook, hook);
149 continue;
150 }
151#endif
152 if (verdict != NF_REPEAT)
153 return verdict;
154 *i = (*i)->prev;
155 }
156 }
157 return NF_ACCEPT;
158}
159
160
161/* Returns 1 if okfn() needs to be executed by the caller,
162 * -EPERM for NF_DROP, 0 otherwise. */
163int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb,
164 struct net_device *indev,
165 struct net_device *outdev,
166 int (*okfn)(struct sk_buff *),
167 int hook_thresh)
168{
169 struct list_head *elem;
170 unsigned int verdict;
171 int ret = 0;
172
173 /* We may already have this, but read-locks nest anyway */
174 rcu_read_lock();
175
176 elem = &nf_hooks[pf][hook];
177next_hook:
178 verdict = nf_iterate(&nf_hooks[pf][hook], pskb, hook, indev,
179 outdev, &elem, okfn, hook_thresh);
180 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
181 ret = 1;
182 goto unlock;
183 } else if (verdict == NF_DROP) {
184 kfree_skb(*pskb);
185 ret = -EPERM;
186 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
187 NFDEBUG("nf_hook: Verdict = QUEUE.\n");
Patrick McHardy394f5452006-08-05 00:58:52 -0700188 if (!nf_queue(*pskb, elem, pf, hook, indev, outdev, okfn,
Harald Weltef6ebe772005-08-09 20:21:49 -0700189 verdict >> NF_VERDICT_BITS))
190 goto next_hook;
191 }
192unlock:
193 rcu_read_unlock();
194 return ret;
195}
196EXPORT_SYMBOL(nf_hook_slow);
197
198
199int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len)
200{
201 struct sk_buff *nskb;
202
203 if (writable_len > (*pskb)->len)
204 return 0;
205
206 /* Not exclusive use of packet? Must copy. */
207 if (skb_shared(*pskb) || skb_cloned(*pskb))
208 goto copy_skb;
209
210 return pskb_may_pull(*pskb, writable_len);
211
212copy_skb:
213 nskb = skb_copy(*pskb, GFP_ATOMIC);
214 if (!nskb)
215 return 0;
216 BUG_ON(skb_is_nonlinear(nskb));
217
218 /* Rest of kernel will get very unhappy if we pass it a
219 suddenly-orphaned skbuff */
220 if ((*pskb)->sk)
221 skb_set_owner_w(nskb, (*pskb)->sk);
222 kfree_skb(*pskb);
223 *pskb = nskb;
224 return 1;
225}
226EXPORT_SYMBOL(skb_make_writable);
227
Al Viro43bc0ca2006-11-14 21:43:23 -0800228void nf_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
229 __be32 from, __be32 to, int pseudohdr)
Patrick McHardy4cf411d2006-08-05 00:58:33 -0700230{
Al Viro43bc0ca2006-11-14 21:43:23 -0800231 __be32 diff[] = { ~from, to };
Patrick McHardy4cf411d2006-08-05 00:58:33 -0700232 if (skb->ip_summed != CHECKSUM_PARTIAL) {
Al Viro43bc0ca2006-11-14 21:43:23 -0800233 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
234 ~csum_unfold(*sum)));
Patrick McHardy4cf411d2006-08-05 00:58:33 -0700235 if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
Al Viro43bc0ca2006-11-14 21:43:23 -0800236 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
237 ~skb->csum);
Patrick McHardy4cf411d2006-08-05 00:58:33 -0700238 } else if (pseudohdr)
Al Viro43bc0ca2006-11-14 21:43:23 -0800239 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
240 csum_unfold(*sum)));
Patrick McHardy4cf411d2006-08-05 00:58:33 -0700241}
Al Viro43bc0ca2006-11-14 21:43:23 -0800242EXPORT_SYMBOL(nf_proto_csum_replace4);
Harald Weltef6ebe772005-08-09 20:21:49 -0700243
244/* This does not belong here, but locally generated errors need it if connection
245 tracking in use: without this, connection may not be in hash table, and hence
246 manufactured ICMP or RST packets will not be associated with it. */
247void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
248EXPORT_SYMBOL(ip_ct_attach);
249
250void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
251{
252 void (*attach)(struct sk_buff *, struct sk_buff *);
253
Patrick McHardyc3a47ab2007-02-12 11:09:19 -0800254 if (skb->nfct) {
255 rcu_read_lock();
256 attach = rcu_dereference(ip_ct_attach);
257 if (attach)
258 attach(new, skb);
259 rcu_read_unlock();
Harald Weltef6ebe772005-08-09 20:21:49 -0700260 }
261}
262EXPORT_SYMBOL(nf_ct_attach);
263
264#ifdef CONFIG_PROC_FS
265struct proc_dir_entry *proc_net_netfilter;
266EXPORT_SYMBOL(proc_net_netfilter);
267#endif
268
269void __init netfilter_init(void)
270{
271 int i, h;
272 for (i = 0; i < NPROTO; i++) {
273 for (h = 0; h < NF_MAX_HOOKS; h++)
274 INIT_LIST_HEAD(&nf_hooks[i][h]);
275 }
276
277#ifdef CONFIG_PROC_FS
278 proc_net_netfilter = proc_mkdir("netfilter", proc_net);
279 if (!proc_net_netfilter)
280 panic("cannot create netfilter proc entry");
281#endif
282
283 if (netfilter_queue_init() < 0)
284 panic("cannot initialize nf_queue");
285 if (netfilter_log_init() < 0)
286 panic("cannot initialize nf_log");
287}