netfilter: pass 'nf_hook_ops' instead of 'list_head' to nf_iterate()
[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>
5a0e3ad6 22#include <linux/slab.h>
457c4cbc 23#include <net/net_namespace.h>
f6ebe77f
HW
24#include <net/sock.h>
25
26#include "nf_internals.h"
27
d486dd1f 28static DEFINE_MUTEX(afinfo_mutex);
bce8032e 29
0906a372 30const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
bce8032e
PM
31EXPORT_SYMBOL(nf_afinfo);
32
1e796fda 33int nf_register_afinfo(const struct nf_afinfo *afinfo)
bce8032e 34{
d486dd1f
PM
35 int err;
36
37 err = mutex_lock_interruptible(&afinfo_mutex);
38 if (err < 0)
39 return err;
a9b3cd7f 40 RCU_INIT_POINTER(nf_afinfo[afinfo->family], afinfo);
d486dd1f 41 mutex_unlock(&afinfo_mutex);
bce8032e
PM
42 return 0;
43}
44EXPORT_SYMBOL_GPL(nf_register_afinfo);
45
1e796fda 46void nf_unregister_afinfo(const struct nf_afinfo *afinfo)
bce8032e 47{
d486dd1f 48 mutex_lock(&afinfo_mutex);
a9b3cd7f 49 RCU_INIT_POINTER(nf_afinfo[afinfo->family], NULL);
d486dd1f 50 mutex_unlock(&afinfo_mutex);
bce8032e
PM
51 synchronize_rcu();
52}
53EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
54
7e9c6eeb 55struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS] __read_mostly;
f6ebe77f 56EXPORT_SYMBOL(nf_hooks);
a2d7ec58
ED
57
58#if defined(CONFIG_JUMP_LABEL)
c5905afb 59struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
a2d7ec58
ED
60EXPORT_SYMBOL(nf_hooks_needed);
61#endif
62
fd706d69 63static DEFINE_MUTEX(nf_hook_mutex);
f6ebe77f
HW
64
65int nf_register_hook(struct nf_hook_ops *reg)
66{
4c610979 67 struct nf_hook_ops *elem;
fd706d69 68 int err;
f6ebe77f 69
fd706d69
PM
70 err = mutex_lock_interruptible(&nf_hook_mutex);
71 if (err < 0)
72 return err;
4c610979
LZ
73 list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {
74 if (reg->priority < elem->priority)
f6ebe77f
HW
75 break;
76 }
4c610979 77 list_add_rcu(&reg->list, elem->list.prev);
fd706d69 78 mutex_unlock(&nf_hook_mutex);
a2d7ec58 79#if defined(CONFIG_JUMP_LABEL)
c5905afb 80 static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]);
a2d7ec58 81#endif
f6ebe77f
HW
82 return 0;
83}
84EXPORT_SYMBOL(nf_register_hook);
85
86void nf_unregister_hook(struct nf_hook_ops *reg)
87{
fd706d69 88 mutex_lock(&nf_hook_mutex);
f6ebe77f 89 list_del_rcu(&reg->list);
fd706d69 90 mutex_unlock(&nf_hook_mutex);
a2d7ec58 91#if defined(CONFIG_JUMP_LABEL)
c5905afb 92 static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
a2d7ec58 93#endif
f6ebe77f
HW
94 synchronize_net();
95}
96EXPORT_SYMBOL(nf_unregister_hook);
97
972d1cb1
PM
98int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
99{
100 unsigned int i;
101 int err = 0;
102
103 for (i = 0; i < n; i++) {
104 err = nf_register_hook(&reg[i]);
105 if (err)
106 goto err;
107 }
108 return err;
109
110err:
111 if (i > 0)
112 nf_unregister_hooks(reg, i);
113 return err;
114}
115EXPORT_SYMBOL(nf_register_hooks);
116
117void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
118{
f68c5301
CG
119 while (n-- > 0)
120 nf_unregister_hook(&reg[n]);
972d1cb1
PM
121}
122EXPORT_SYMBOL(nf_unregister_hooks);
123
f6ebe77f 124unsigned int nf_iterate(struct list_head *head,
3db05fea 125 struct sk_buff *skb,
76108cea 126 unsigned int hook,
f6ebe77f
HW
127 const struct net_device *indev,
128 const struct net_device *outdev,
2a6decfd 129 struct nf_hook_ops **elemp,
f6ebe77f
HW
130 int (*okfn)(struct sk_buff *),
131 int hook_thresh)
132{
133 unsigned int verdict;
134
135 /*
136 * The caller must not block between calls to this
137 * function because of risk of continuing from deleted element.
138 */
2a6decfd
MW
139 list_for_each_entry_continue_rcu((*elemp), head, list) {
140 if (hook_thresh > (*elemp)->priority)
f6ebe77f
HW
141 continue;
142
143 /* Optimization: we don't need to hold module
601e68e1 144 reference here, since function can't sleep. --RR */
de9963f0 145repeat:
2a6decfd 146 verdict = (*elemp)->hook(hook, skb, indev, outdev, okfn);
f6ebe77f
HW
147 if (verdict != NF_ACCEPT) {
148#ifdef CONFIG_NETFILTER_DEBUG
149 if (unlikely((verdict & NF_VERDICT_MASK)
150 > NF_MAX_VERDICT)) {
151 NFDEBUG("Evil return from %p(%u).\n",
2a6decfd 152 (*elemp)->hook, hook);
f6ebe77f
HW
153 continue;
154 }
155#endif
2a6decfd 156 if (verdict != NF_REPEAT)
f6ebe77f 157 return verdict;
de9963f0 158 goto repeat;
f6ebe77f
HW
159 }
160 }
161 return NF_ACCEPT;
162}
163
164
165/* Returns 1 if okfn() needs to be executed by the caller,
166 * -EPERM for NF_DROP, 0 otherwise. */
76108cea 167int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
f6ebe77f
HW
168 struct net_device *indev,
169 struct net_device *outdev,
170 int (*okfn)(struct sk_buff *),
171 int hook_thresh)
172{
2a6decfd 173 struct nf_hook_ops *elem;
f6ebe77f
HW
174 unsigned int verdict;
175 int ret = 0;
176
177 /* We may already have this, but read-locks nest anyway */
178 rcu_read_lock();
179
2a6decfd 180 elem = list_entry_rcu(&nf_hooks[pf][hook], struct nf_hook_ops, list);
f6ebe77f 181next_hook:
3db05fea 182 verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev,
f6ebe77f
HW
183 outdev, &elem, okfn, hook_thresh);
184 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
185 ret = 1;
da683650 186 } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) {
3db05fea 187 kfree_skb(skb);
f615df76 188 ret = NF_DROP_GETERR(verdict);
da683650
EP
189 if (ret == 0)
190 ret = -EPERM;
f9c63990 191 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
2a6decfd
MW
192 int err = nf_queue(skb, &elem->list, pf, hook, indev, outdev,
193 okfn, verdict >> NF_VERDICT_QBITS);
563e1232
FW
194 if (err < 0) {
195 if (err == -ECANCELED)
06cdb634 196 goto next_hook;
563e1232 197 if (err == -ESRCH &&
94b27cc3
FW
198 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
199 goto next_hook;
06cdb634
FW
200 kfree_skb(skb);
201 }
f6ebe77f 202 }
f6ebe77f
HW
203 rcu_read_unlock();
204 return ret;
205}
206EXPORT_SYMBOL(nf_hook_slow);
207
208
37d41879 209int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
f6ebe77f 210{
37d41879 211 if (writable_len > skb->len)
f6ebe77f
HW
212 return 0;
213
214 /* Not exclusive use of packet? Must copy. */
37d41879
HX
215 if (!skb_cloned(skb)) {
216 if (writable_len <= skb_headlen(skb))
217 return 1;
218 } else if (skb_clone_writable(skb, writable_len))
219 return 1;
220
221 if (writable_len <= skb_headlen(skb))
222 writable_len = 0;
223 else
224 writable_len -= skb_headlen(skb);
225
226 return !!__pskb_pull_tail(skb, writable_len);
f6ebe77f
HW
227}
228EXPORT_SYMBOL(skb_make_writable);
229
c0cd1156 230#if IS_ENABLED(CONFIG_NF_CONNTRACK)
f6ebe77f
HW
231/* This does not belong here, but locally generated errors need it if connection
232 tracking in use: without this, connection may not be in hash table, and hence
233 manufactured ICMP or RST packets will not be associated with it. */
0e60ebe0 234void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *) __rcu __read_mostly;
f6ebe77f
HW
235EXPORT_SYMBOL(ip_ct_attach);
236
237void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
238{
239 void (*attach)(struct sk_buff *, struct sk_buff *);
240
c3a47ab3
PM
241 if (skb->nfct) {
242 rcu_read_lock();
243 attach = rcu_dereference(ip_ct_attach);
244 if (attach)
245 attach(new, skb);
246 rcu_read_unlock();
f6ebe77f
HW
247 }
248}
249EXPORT_SYMBOL(nf_ct_attach);
de6e05c4 250
0e60ebe0 251void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly;
de6e05c4
YK
252EXPORT_SYMBOL(nf_ct_destroy);
253
254void nf_conntrack_destroy(struct nf_conntrack *nfct)
255{
256 void (*destroy)(struct nf_conntrack *);
257
258 rcu_read_lock();
259 destroy = rcu_dereference(nf_ct_destroy);
260 BUG_ON(destroy == NULL);
261 destroy(nfct);
262 rcu_read_unlock();
263}
264EXPORT_SYMBOL(nf_conntrack_destroy);
9cb01766 265
5a05fae5 266struct nfq_ct_hook __rcu *nfq_ct_hook __read_mostly;
9cb01766
PNA
267EXPORT_SYMBOL_GPL(nfq_ct_hook);
268
d584a61a
PNA
269struct nfq_ct_nat_hook __rcu *nfq_ct_nat_hook __read_mostly;
270EXPORT_SYMBOL_GPL(nfq_ct_nat_hook);
271
de6e05c4 272#endif /* CONFIG_NF_CONNTRACK */
f6ebe77f 273
c7232c99
PM
274#ifdef CONFIG_NF_NAT_NEEDED
275void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
276EXPORT_SYMBOL(nf_nat_decode_session_hook);
277#endif
278
f6ebe77f
HW
279#ifdef CONFIG_PROC_FS
280struct proc_dir_entry *proc_net_netfilter;
281EXPORT_SYMBOL(proc_net_netfilter);
282#endif
283
284void __init netfilter_init(void)
285{
286 int i, h;
7e9c6eeb 287 for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) {
f6ebe77f
HW
288 for (h = 0; h < NF_MAX_HOOKS; h++)
289 INIT_LIST_HEAD(&nf_hooks[i][h]);
290 }
291
292#ifdef CONFIG_PROC_FS
457c4cbc 293 proc_net_netfilter = proc_mkdir("netfilter", init_net.proc_net);
f6ebe77f
HW
294 if (!proc_net_netfilter)
295 panic("cannot create netfilter proc entry");
296#endif
297
298 if (netfilter_queue_init() < 0)
299 panic("cannot initialize nf_queue");
300 if (netfilter_log_init() < 0)
301 panic("cannot initialize nf_log");
302}