blob: 292fa28146fb33cfe5beb77e70c95a4f8caa96e2 [file] [log] [blame]
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001/* netfilter.c: look after the filters for various protocols.
Harald Weltef6ebe772005-08-09 20:21:49 -07002 * 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.
Harald Weltef6ebe772005-08-09 20:21:49 -07008 */
Harald Weltef6ebe772005-08-09 20:21:49 -07009#include <linux/kernel.h>
10#include <linux/netfilter.h>
11#include <net/protocol.h>
12#include <linux/init.h>
13#include <linux/skbuff.h>
14#include <linux/wait.h>
15#include <linux/module.h>
16#include <linux/interrupt.h>
17#include <linux/if.h>
18#include <linux/netdevice.h>
19#include <linux/inetdevice.h>
20#include <linux/proc_fs.h>
Patrick McHardyd486dd12007-02-12 11:09:55 -080021#include <linux/mutex.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020022#include <net/net_namespace.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070023#include <net/sock.h>
24
25#include "nf_internals.h"
26
Patrick McHardyd486dd12007-02-12 11:09:55 -080027static DEFINE_MUTEX(afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070028
Patrick McHardy1e796fd2007-12-17 22:42:27 -080029const struct nf_afinfo *nf_afinfo[NPROTO] __read_mostly;
Patrick McHardybce80322006-04-06 14:18:09 -070030EXPORT_SYMBOL(nf_afinfo);
31
Patrick McHardy1e796fd2007-12-17 22:42:27 -080032int nf_register_afinfo(const struct nf_afinfo *afinfo)
Patrick McHardybce80322006-04-06 14:18:09 -070033{
Patrick McHardyd486dd12007-02-12 11:09:55 -080034 int err;
35
36 err = mutex_lock_interruptible(&afinfo_mutex);
37 if (err < 0)
38 return err;
Patrick McHardybce80322006-04-06 14:18:09 -070039 rcu_assign_pointer(nf_afinfo[afinfo->family], afinfo);
Patrick McHardyd486dd12007-02-12 11:09:55 -080040 mutex_unlock(&afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070041 return 0;
42}
43EXPORT_SYMBOL_GPL(nf_register_afinfo);
44
Patrick McHardy1e796fd2007-12-17 22:42:27 -080045void nf_unregister_afinfo(const struct nf_afinfo *afinfo)
Patrick McHardybce80322006-04-06 14:18:09 -070046{
Patrick McHardyd486dd12007-02-12 11:09:55 -080047 mutex_lock(&afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070048 rcu_assign_pointer(nf_afinfo[afinfo->family], NULL);
Patrick McHardyd486dd12007-02-12 11:09:55 -080049 mutex_unlock(&afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070050 synchronize_rcu();
51}
52EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
53
Martin Josefssone2b76062006-11-29 02:35:04 +010054struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS] __read_mostly;
Harald Weltef6ebe772005-08-09 20:21:49 -070055EXPORT_SYMBOL(nf_hooks);
Patrick McHardyfd706d62007-02-12 11:10:14 -080056static DEFINE_MUTEX(nf_hook_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070057
58int nf_register_hook(struct nf_hook_ops *reg)
59{
Li Zefan4c610972007-12-04 23:22:26 -080060 struct nf_hook_ops *elem;
Patrick McHardyfd706d62007-02-12 11:10:14 -080061 int err;
Harald Weltef6ebe772005-08-09 20:21:49 -070062
Patrick McHardyfd706d62007-02-12 11:10:14 -080063 err = mutex_lock_interruptible(&nf_hook_mutex);
64 if (err < 0)
65 return err;
Li Zefan4c610972007-12-04 23:22:26 -080066 list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {
67 if (reg->priority < elem->priority)
Harald Weltef6ebe772005-08-09 20:21:49 -070068 break;
69 }
Li Zefan4c610972007-12-04 23:22:26 -080070 list_add_rcu(&reg->list, elem->list.prev);
Patrick McHardyfd706d62007-02-12 11:10:14 -080071 mutex_unlock(&nf_hook_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070072 return 0;
73}
74EXPORT_SYMBOL(nf_register_hook);
75
76void nf_unregister_hook(struct nf_hook_ops *reg)
77{
Patrick McHardyfd706d62007-02-12 11:10:14 -080078 mutex_lock(&nf_hook_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070079 list_del_rcu(&reg->list);
Patrick McHardyfd706d62007-02-12 11:10:14 -080080 mutex_unlock(&nf_hook_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070081
82 synchronize_net();
83}
84EXPORT_SYMBOL(nf_unregister_hook);
85
Patrick McHardy972d1cb2006-04-06 14:09:12 -070086int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
87{
88 unsigned int i;
89 int err = 0;
90
91 for (i = 0; i < n; i++) {
92 err = nf_register_hook(&reg[i]);
93 if (err)
94 goto err;
95 }
96 return err;
97
98err:
99 if (i > 0)
100 nf_unregister_hooks(reg, i);
101 return err;
102}
103EXPORT_SYMBOL(nf_register_hooks);
104
105void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
106{
107 unsigned int i;
108
109 for (i = 0; i < n; i++)
110 nf_unregister_hook(&reg[i]);
111}
112EXPORT_SYMBOL(nf_unregister_hooks);
113
Harald Weltef6ebe772005-08-09 20:21:49 -0700114unsigned int nf_iterate(struct list_head *head,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700115 struct sk_buff *skb,
Harald Weltef6ebe772005-08-09 20:21:49 -0700116 int hook,
117 const struct net_device *indev,
118 const struct net_device *outdev,
119 struct list_head **i,
120 int (*okfn)(struct sk_buff *),
121 int hook_thresh)
122{
123 unsigned int verdict;
124
125 /*
126 * The caller must not block between calls to this
127 * function because of risk of continuing from deleted element.
128 */
129 list_for_each_continue_rcu(*i, head) {
130 struct nf_hook_ops *elem = (struct nf_hook_ops *)*i;
131
132 if (hook_thresh > elem->priority)
133 continue;
134
135 /* Optimization: we don't need to hold module
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800136 reference here, since function can't sleep. --RR */
Harald Weltef6ebe772005-08-09 20:21:49 -0700137 verdict = elem->hook(hook, skb, indev, outdev, okfn);
138 if (verdict != NF_ACCEPT) {
139#ifdef CONFIG_NETFILTER_DEBUG
140 if (unlikely((verdict & NF_VERDICT_MASK)
141 > NF_MAX_VERDICT)) {
142 NFDEBUG("Evil return from %p(%u).\n",
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800143 elem->hook, hook);
Harald Weltef6ebe772005-08-09 20:21:49 -0700144 continue;
145 }
146#endif
147 if (verdict != NF_REPEAT)
148 return verdict;
149 *i = (*i)->prev;
150 }
151 }
152 return NF_ACCEPT;
153}
154
155
156/* Returns 1 if okfn() needs to be executed by the caller,
157 * -EPERM for NF_DROP, 0 otherwise. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700158int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb,
Harald Weltef6ebe772005-08-09 20:21:49 -0700159 struct net_device *indev,
160 struct net_device *outdev,
161 int (*okfn)(struct sk_buff *),
162 int hook_thresh)
163{
164 struct list_head *elem;
165 unsigned int verdict;
166 int ret = 0;
167
Denis V. Lunev0be43f82008-03-24 15:32:09 -0700168#ifdef CONFIG_NET_NS
169 struct net *net;
170
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900171 net = indev == NULL ? dev_net(outdev) : dev_net(indev);
Denis V. Lunev0be43f82008-03-24 15:32:09 -0700172 if (net != &init_net)
173 return 1;
174#endif
175
Harald Weltef6ebe772005-08-09 20:21:49 -0700176 /* We may already have this, but read-locks nest anyway */
177 rcu_read_lock();
178
179 elem = &nf_hooks[pf][hook];
180next_hook:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700181 verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev,
Harald Weltef6ebe772005-08-09 20:21:49 -0700182 outdev, &elem, okfn, hook_thresh);
183 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
184 ret = 1;
185 goto unlock;
186 } else if (verdict == NF_DROP) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700187 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700188 ret = -EPERM;
Patrick McHardyf9c63992007-12-05 01:27:46 -0800189 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700190 if (!nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
Harald Weltef6ebe772005-08-09 20:21:49 -0700191 verdict >> NF_VERDICT_BITS))
192 goto next_hook;
193 }
194unlock:
195 rcu_read_unlock();
196 return ret;
197}
198EXPORT_SYMBOL(nf_hook_slow);
199
200
Herbert Xu37d41872007-10-14 00:39:18 -0700201int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
Harald Weltef6ebe772005-08-09 20:21:49 -0700202{
Herbert Xu37d41872007-10-14 00:39:18 -0700203 if (writable_len > skb->len)
Harald Weltef6ebe772005-08-09 20:21:49 -0700204 return 0;
205
206 /* Not exclusive use of packet? Must copy. */
Herbert Xu37d41872007-10-14 00:39:18 -0700207 if (!skb_cloned(skb)) {
208 if (writable_len <= skb_headlen(skb))
209 return 1;
210 } else if (skb_clone_writable(skb, writable_len))
211 return 1;
Harald Weltef6ebe772005-08-09 20:21:49 -0700212
Herbert Xu37d41872007-10-14 00:39:18 -0700213 if (writable_len <= skb_headlen(skb))
214 writable_len = 0;
215 else
216 writable_len -= skb_headlen(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700217
Herbert Xu37d41872007-10-14 00:39:18 -0700218 return !!__pskb_pull_tail(skb, writable_len);
Harald Weltef6ebe772005-08-09 20:21:49 -0700219}
220EXPORT_SYMBOL(skb_make_writable);
221
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700222#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Harald Weltef6ebe772005-08-09 20:21:49 -0700223/* This does not belong here, but locally generated errors need it if connection
224 tracking in use: without this, connection may not be in hash table, and hence
225 manufactured ICMP or RST packets will not be associated with it. */
226void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
227EXPORT_SYMBOL(ip_ct_attach);
228
229void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
230{
231 void (*attach)(struct sk_buff *, struct sk_buff *);
232
Patrick McHardyc3a47ab2007-02-12 11:09:19 -0800233 if (skb->nfct) {
234 rcu_read_lock();
235 attach = rcu_dereference(ip_ct_attach);
236 if (attach)
237 attach(new, skb);
238 rcu_read_unlock();
Harald Weltef6ebe772005-08-09 20:21:49 -0700239 }
240}
241EXPORT_SYMBOL(nf_ct_attach);
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700242
243void (*nf_ct_destroy)(struct nf_conntrack *);
244EXPORT_SYMBOL(nf_ct_destroy);
245
246void nf_conntrack_destroy(struct nf_conntrack *nfct)
247{
248 void (*destroy)(struct nf_conntrack *);
249
250 rcu_read_lock();
251 destroy = rcu_dereference(nf_ct_destroy);
252 BUG_ON(destroy == NULL);
253 destroy(nfct);
254 rcu_read_unlock();
255}
256EXPORT_SYMBOL(nf_conntrack_destroy);
257#endif /* CONFIG_NF_CONNTRACK */
Harald Weltef6ebe772005-08-09 20:21:49 -0700258
259#ifdef CONFIG_PROC_FS
260struct proc_dir_entry *proc_net_netfilter;
261EXPORT_SYMBOL(proc_net_netfilter);
262#endif
263
264void __init netfilter_init(void)
265{
266 int i, h;
267 for (i = 0; i < NPROTO; i++) {
268 for (h = 0; h < NF_MAX_HOOKS; h++)
269 INIT_LIST_HEAD(&nf_hooks[i][h]);
270 }
271
272#ifdef CONFIG_PROC_FS
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200273 proc_net_netfilter = proc_mkdir("netfilter", init_net.proc_net);
Harald Weltef6ebe772005-08-09 20:21:49 -0700274 if (!proc_net_netfilter)
275 panic("cannot create netfilter proc entry");
276#endif
277
278 if (netfilter_queue_init() < 0)
279 panic("cannot initialize nf_queue");
280 if (netfilter_log_init() < 0)
281 panic("cannot initialize nf_log");
282}
Patrick McHardy4f536522008-01-14 23:48:39 -0800283
284#ifdef CONFIG_SYSCTL
285struct ctl_path nf_net_netfilter_sysctl_path[] = {
286 { .procname = "net", .ctl_name = CTL_NET, },
287 { .procname = "netfilter", .ctl_name = NET_NETFILTER, },
288 { }
289};
290EXPORT_SYMBOL_GPL(nf_net_netfilter_sysctl_path);
291#endif /* CONFIG_SYSCTL */