[NETFILTER]: nf_queue: clean up error paths
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netfilter / core.c
CommitLineData
601e68e1 1/* netfilter.c: look after the filters for various protocols.
f6ebe77f
HW
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.
f6ebe77f 8 */
f6ebe77f
HW
9#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>
d486dd1f 21#include <linux/mutex.h>
457c4cbc 22#include <net/net_namespace.h>
f6ebe77f
HW
23#include <net/sock.h>
24
25#include "nf_internals.h"
26
d486dd1f 27static DEFINE_MUTEX(afinfo_mutex);
bce8032e 28
e2b7606c 29struct nf_afinfo *nf_afinfo[NPROTO] __read_mostly;
bce8032e
PM
30EXPORT_SYMBOL(nf_afinfo);
31
32int nf_register_afinfo(struct nf_afinfo *afinfo)
33{
d486dd1f
PM
34 int err;
35
36 err = mutex_lock_interruptible(&afinfo_mutex);
37 if (err < 0)
38 return err;
bce8032e 39 rcu_assign_pointer(nf_afinfo[afinfo->family], afinfo);
d486dd1f 40 mutex_unlock(&afinfo_mutex);
bce8032e
PM
41 return 0;
42}
43EXPORT_SYMBOL_GPL(nf_register_afinfo);
44
45void nf_unregister_afinfo(struct nf_afinfo *afinfo)
46{
d486dd1f 47 mutex_lock(&afinfo_mutex);
bce8032e 48 rcu_assign_pointer(nf_afinfo[afinfo->family], NULL);
d486dd1f 49 mutex_unlock(&afinfo_mutex);
bce8032e
PM
50 synchronize_rcu();
51}
52EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
53
e2b7606c 54struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS] __read_mostly;
f6ebe77f 55EXPORT_SYMBOL(nf_hooks);
fd706d69 56static DEFINE_MUTEX(nf_hook_mutex);
f6ebe77f
HW
57
58int nf_register_hook(struct nf_hook_ops *reg)
59{
4c610979 60 struct nf_hook_ops *elem;
fd706d69 61 int err;
f6ebe77f 62
fd706d69
PM
63 err = mutex_lock_interruptible(&nf_hook_mutex);
64 if (err < 0)
65 return err;
4c610979
LZ
66 list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {
67 if (reg->priority < elem->priority)
f6ebe77f
HW
68 break;
69 }
4c610979 70 list_add_rcu(&reg->list, elem->list.prev);
fd706d69 71 mutex_unlock(&nf_hook_mutex);
f6ebe77f
HW
72 return 0;
73}
74EXPORT_SYMBOL(nf_register_hook);
75
76void nf_unregister_hook(struct nf_hook_ops *reg)
77{
fd706d69 78 mutex_lock(&nf_hook_mutex);
f6ebe77f 79 list_del_rcu(&reg->list);
fd706d69 80 mutex_unlock(&nf_hook_mutex);
f6ebe77f
HW
81
82 synchronize_net();
83}
84EXPORT_SYMBOL(nf_unregister_hook);
85
972d1cb1
PM
86int 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
f6ebe77f 114unsigned int nf_iterate(struct list_head *head,
3db05fea 115 struct sk_buff *skb,
f6ebe77f
HW
116 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
601e68e1 136 reference here, since function can't sleep. --RR */
f6ebe77f
HW
137 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",
601e68e1 143 elem->hook, hook);
f6ebe77f
HW
144 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. */
3db05fea 158int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb,
f6ebe77f
HW
159 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
168 /* We may already have this, but read-locks nest anyway */
169 rcu_read_lock();
170
171 elem = &nf_hooks[pf][hook];
172next_hook:
3db05fea 173 verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev,
f6ebe77f
HW
174 outdev, &elem, okfn, hook_thresh);
175 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
176 ret = 1;
177 goto unlock;
178 } else if (verdict == NF_DROP) {
3db05fea 179 kfree_skb(skb);
f6ebe77f
HW
180 ret = -EPERM;
181 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
182 NFDEBUG("nf_hook: Verdict = QUEUE.\n");
3db05fea 183 if (!nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
f6ebe77f
HW
184 verdict >> NF_VERDICT_BITS))
185 goto next_hook;
186 }
187unlock:
188 rcu_read_unlock();
189 return ret;
190}
191EXPORT_SYMBOL(nf_hook_slow);
192
193
37d41879 194int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
f6ebe77f 195{
37d41879 196 if (writable_len > skb->len)
f6ebe77f
HW
197 return 0;
198
199 /* Not exclusive use of packet? Must copy. */
37d41879
HX
200 if (!skb_cloned(skb)) {
201 if (writable_len <= skb_headlen(skb))
202 return 1;
203 } else if (skb_clone_writable(skb, writable_len))
204 return 1;
205
206 if (writable_len <= skb_headlen(skb))
207 writable_len = 0;
208 else
209 writable_len -= skb_headlen(skb);
210
211 return !!__pskb_pull_tail(skb, writable_len);
f6ebe77f
HW
212}
213EXPORT_SYMBOL(skb_make_writable);
214
5f79e0f9 215#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
f6ebe77f
HW
216/* This does not belong here, but locally generated errors need it if connection
217 tracking in use: without this, connection may not be in hash table, and hence
218 manufactured ICMP or RST packets will not be associated with it. */
219void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
220EXPORT_SYMBOL(ip_ct_attach);
221
222void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
223{
224 void (*attach)(struct sk_buff *, struct sk_buff *);
225
c3a47ab3
PM
226 if (skb->nfct) {
227 rcu_read_lock();
228 attach = rcu_dereference(ip_ct_attach);
229 if (attach)
230 attach(new, skb);
231 rcu_read_unlock();
f6ebe77f
HW
232 }
233}
234EXPORT_SYMBOL(nf_ct_attach);
de6e05c4
YK
235
236void (*nf_ct_destroy)(struct nf_conntrack *);
237EXPORT_SYMBOL(nf_ct_destroy);
238
239void nf_conntrack_destroy(struct nf_conntrack *nfct)
240{
241 void (*destroy)(struct nf_conntrack *);
242
243 rcu_read_lock();
244 destroy = rcu_dereference(nf_ct_destroy);
245 BUG_ON(destroy == NULL);
246 destroy(nfct);
247 rcu_read_unlock();
248}
249EXPORT_SYMBOL(nf_conntrack_destroy);
250#endif /* CONFIG_NF_CONNTRACK */
f6ebe77f
HW
251
252#ifdef CONFIG_PROC_FS
253struct proc_dir_entry *proc_net_netfilter;
254EXPORT_SYMBOL(proc_net_netfilter);
255#endif
256
257void __init netfilter_init(void)
258{
259 int i, h;
260 for (i = 0; i < NPROTO; i++) {
261 for (h = 0; h < NF_MAX_HOOKS; h++)
262 INIT_LIST_HEAD(&nf_hooks[i][h]);
263 }
264
265#ifdef CONFIG_PROC_FS
457c4cbc 266 proc_net_netfilter = proc_mkdir("netfilter", init_net.proc_net);
f6ebe77f
HW
267 if (!proc_net_netfilter)
268 panic("cannot create netfilter proc entry");
269#endif
270
271 if (netfilter_queue_init() < 0)
272 panic("cannot initialize nf_queue");
273 if (netfilter_log_init() < 0)
274 panic("cannot initialize nf_log");
275}