blob: 8f014f22d132d17e7c79ce0aa727463e3dbd7941 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020023#include <net/net_namespace.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070024#include <net/sock.h>
25
26#include "nf_internals.h"
27
Patrick McHardyd486dd12007-02-12 11:09:55 -080028static DEFINE_MUTEX(afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070029
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020030const struct nf_afinfo *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
Patrick McHardybce80322006-04-06 14:18:09 -070031EXPORT_SYMBOL(nf_afinfo);
32
Patrick McHardy1e796fd2007-12-17 22:42:27 -080033int nf_register_afinfo(const struct nf_afinfo *afinfo)
Patrick McHardybce80322006-04-06 14:18:09 -070034{
Patrick McHardyd486dd12007-02-12 11:09:55 -080035 int err;
36
37 err = mutex_lock_interruptible(&afinfo_mutex);
38 if (err < 0)
39 return err;
Patrick McHardybce80322006-04-06 14:18:09 -070040 rcu_assign_pointer(nf_afinfo[afinfo->family], afinfo);
Patrick McHardyd486dd12007-02-12 11:09:55 -080041 mutex_unlock(&afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070042 return 0;
43}
44EXPORT_SYMBOL_GPL(nf_register_afinfo);
45
Patrick McHardy1e796fd2007-12-17 22:42:27 -080046void nf_unregister_afinfo(const struct nf_afinfo *afinfo)
Patrick McHardybce80322006-04-06 14:18:09 -070047{
Patrick McHardyd486dd12007-02-12 11:09:55 -080048 mutex_lock(&afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070049 rcu_assign_pointer(nf_afinfo[afinfo->family], NULL);
Patrick McHardyd486dd12007-02-12 11:09:55 -080050 mutex_unlock(&afinfo_mutex);
Patrick McHardybce80322006-04-06 14:18:09 -070051 synchronize_rcu();
52}
53EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
54
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020055struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS] __read_mostly;
Harald Weltef6ebe772005-08-09 20:21:49 -070056EXPORT_SYMBOL(nf_hooks);
Patrick McHardyfd706d62007-02-12 11:10:14 -080057static DEFINE_MUTEX(nf_hook_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070058
59int nf_register_hook(struct nf_hook_ops *reg)
60{
Li Zefan4c610972007-12-04 23:22:26 -080061 struct nf_hook_ops *elem;
Patrick McHardyfd706d62007-02-12 11:10:14 -080062 int err;
Harald Weltef6ebe772005-08-09 20:21:49 -070063
Patrick McHardyfd706d62007-02-12 11:10:14 -080064 err = mutex_lock_interruptible(&nf_hook_mutex);
65 if (err < 0)
66 return err;
Li Zefan4c610972007-12-04 23:22:26 -080067 list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {
68 if (reg->priority < elem->priority)
Harald Weltef6ebe772005-08-09 20:21:49 -070069 break;
70 }
Li Zefan4c610972007-12-04 23:22:26 -080071 list_add_rcu(&reg->list, elem->list.prev);
Patrick McHardyfd706d62007-02-12 11:10:14 -080072 mutex_unlock(&nf_hook_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070073 return 0;
74}
75EXPORT_SYMBOL(nf_register_hook);
76
77void nf_unregister_hook(struct nf_hook_ops *reg)
78{
Patrick McHardyfd706d62007-02-12 11:10:14 -080079 mutex_lock(&nf_hook_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070080 list_del_rcu(&reg->list);
Patrick McHardyfd706d62007-02-12 11:10:14 -080081 mutex_unlock(&nf_hook_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070082
83 synchronize_net();
84}
85EXPORT_SYMBOL(nf_unregister_hook);
86
Patrick McHardy972d1cb2006-04-06 14:09:12 -070087int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
88{
89 unsigned int i;
90 int err = 0;
91
92 for (i = 0; i < n; i++) {
93 err = nf_register_hook(&reg[i]);
94 if (err)
95 goto err;
96 }
97 return err;
98
99err:
100 if (i > 0)
101 nf_unregister_hooks(reg, i);
102 return err;
103}
104EXPORT_SYMBOL(nf_register_hooks);
105
106void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
107{
Changli Gaof68c5302010-10-04 22:24:12 +0200108 while (n-- > 0)
109 nf_unregister_hook(&reg[n]);
Patrick McHardy972d1cb2006-04-06 14:09:12 -0700110}
111EXPORT_SYMBOL(nf_unregister_hooks);
112
Harald Weltef6ebe772005-08-09 20:21:49 -0700113unsigned int nf_iterate(struct list_head *head,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700114 struct sk_buff *skb,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200115 unsigned int hook,
Harald Weltef6ebe772005-08-09 20:21:49 -0700116 const struct net_device *indev,
117 const struct net_device *outdev,
118 struct list_head **i,
119 int (*okfn)(struct sk_buff *),
120 int hook_thresh)
121{
122 unsigned int verdict;
123
124 /*
125 * The caller must not block between calls to this
126 * function because of risk of continuing from deleted element.
127 */
128 list_for_each_continue_rcu(*i, head) {
129 struct nf_hook_ops *elem = (struct nf_hook_ops *)*i;
130
131 if (hook_thresh > elem->priority)
132 continue;
133
134 /* Optimization: we don't need to hold module
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800135 reference here, since function can't sleep. --RR */
Harald Weltef6ebe772005-08-09 20:21:49 -0700136 verdict = elem->hook(hook, skb, indev, outdev, okfn);
137 if (verdict != NF_ACCEPT) {
138#ifdef CONFIG_NETFILTER_DEBUG
139 if (unlikely((verdict & NF_VERDICT_MASK)
140 > NF_MAX_VERDICT)) {
141 NFDEBUG("Evil return from %p(%u).\n",
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800142 elem->hook, hook);
Harald Weltef6ebe772005-08-09 20:21:49 -0700143 continue;
144 }
145#endif
146 if (verdict != NF_REPEAT)
147 return verdict;
148 *i = (*i)->prev;
149 }
150 }
151 return NF_ACCEPT;
152}
153
154
155/* Returns 1 if okfn() needs to be executed by the caller,
156 * -EPERM for NF_DROP, 0 otherwise. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200157int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
Harald Weltef6ebe772005-08-09 20:21:49 -0700158 struct net_device *indev,
159 struct net_device *outdev,
160 int (*okfn)(struct sk_buff *),
161 int hook_thresh)
162{
163 struct list_head *elem;
164 unsigned int verdict;
165 int ret = 0;
166
167 /* We may already have this, but read-locks nest anyway */
168 rcu_read_lock();
169
170 elem = &nf_hooks[pf][hook];
171next_hook:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700172 verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev,
Harald Weltef6ebe772005-08-09 20:21:49 -0700173 outdev, &elem, okfn, hook_thresh);
174 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
175 ret = 1;
Harald Weltef6ebe772005-08-09 20:21:49 -0700176 } else if (verdict == NF_DROP) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700177 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700178 ret = -EPERM;
Patrick McHardyf9c63992007-12-05 01:27:46 -0800179 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700180 if (!nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
Harald Weltef6ebe772005-08-09 20:21:49 -0700181 verdict >> NF_VERDICT_BITS))
182 goto next_hook;
183 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700184 rcu_read_unlock();
185 return ret;
186}
187EXPORT_SYMBOL(nf_hook_slow);
188
189
Herbert Xu37d41872007-10-14 00:39:18 -0700190int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
Harald Weltef6ebe772005-08-09 20:21:49 -0700191{
Herbert Xu37d41872007-10-14 00:39:18 -0700192 if (writable_len > skb->len)
Harald Weltef6ebe772005-08-09 20:21:49 -0700193 return 0;
194
195 /* Not exclusive use of packet? Must copy. */
Herbert Xu37d41872007-10-14 00:39:18 -0700196 if (!skb_cloned(skb)) {
197 if (writable_len <= skb_headlen(skb))
198 return 1;
199 } else if (skb_clone_writable(skb, writable_len))
200 return 1;
Harald Weltef6ebe772005-08-09 20:21:49 -0700201
Herbert Xu37d41872007-10-14 00:39:18 -0700202 if (writable_len <= skb_headlen(skb))
203 writable_len = 0;
204 else
205 writable_len -= skb_headlen(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700206
Herbert Xu37d41872007-10-14 00:39:18 -0700207 return !!__pskb_pull_tail(skb, writable_len);
Harald Weltef6ebe772005-08-09 20:21:49 -0700208}
209EXPORT_SYMBOL(skb_make_writable);
210
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700211#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Harald Weltef6ebe772005-08-09 20:21:49 -0700212/* This does not belong here, but locally generated errors need it if connection
213 tracking in use: without this, connection may not be in hash table, and hence
214 manufactured ICMP or RST packets will not be associated with it. */
215void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
216EXPORT_SYMBOL(ip_ct_attach);
217
218void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
219{
220 void (*attach)(struct sk_buff *, struct sk_buff *);
221
Patrick McHardyc3a47ab2007-02-12 11:09:19 -0800222 if (skb->nfct) {
223 rcu_read_lock();
224 attach = rcu_dereference(ip_ct_attach);
225 if (attach)
226 attach(new, skb);
227 rcu_read_unlock();
Harald Weltef6ebe772005-08-09 20:21:49 -0700228 }
229}
230EXPORT_SYMBOL(nf_ct_attach);
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700231
232void (*nf_ct_destroy)(struct nf_conntrack *);
233EXPORT_SYMBOL(nf_ct_destroy);
234
235void nf_conntrack_destroy(struct nf_conntrack *nfct)
236{
237 void (*destroy)(struct nf_conntrack *);
238
239 rcu_read_lock();
240 destroy = rcu_dereference(nf_ct_destroy);
241 BUG_ON(destroy == NULL);
242 destroy(nfct);
243 rcu_read_unlock();
244}
245EXPORT_SYMBOL(nf_conntrack_destroy);
246#endif /* CONFIG_NF_CONNTRACK */
Harald Weltef6ebe772005-08-09 20:21:49 -0700247
248#ifdef CONFIG_PROC_FS
249struct proc_dir_entry *proc_net_netfilter;
250EXPORT_SYMBOL(proc_net_netfilter);
251#endif
252
253void __init netfilter_init(void)
254{
255 int i, h;
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +0200256 for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) {
Harald Weltef6ebe772005-08-09 20:21:49 -0700257 for (h = 0; h < NF_MAX_HOOKS; h++)
258 INIT_LIST_HEAD(&nf_hooks[i][h]);
259 }
260
261#ifdef CONFIG_PROC_FS
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200262 proc_net_netfilter = proc_mkdir("netfilter", init_net.proc_net);
Harald Weltef6ebe772005-08-09 20:21:49 -0700263 if (!proc_net_netfilter)
264 panic("cannot create netfilter proc entry");
265#endif
266
267 if (netfilter_queue_init() < 0)
268 panic("cannot initialize nf_queue");
269 if (netfilter_log_init() < 0)
270 panic("cannot initialize nf_log");
271}
Patrick McHardy4f536522008-01-14 23:48:39 -0800272
273#ifdef CONFIG_SYSCTL
274struct ctl_path nf_net_netfilter_sysctl_path[] = {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800275 { .procname = "net", },
276 { .procname = "netfilter", },
Patrick McHardy4f536522008-01-14 23:48:39 -0800277 { }
278};
279EXPORT_SYMBOL_GPL(nf_net_netfilter_sysctl_path);
280#endif /* CONFIG_SYSCTL */