netfilter: Use nf_hook_state in nf_queue_entry.
[deliverable/linux.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.
f229f6ce 8 * Patrick McHardy (c) 2006-2012
f6ebe77f 9 */
f6ebe77f
HW
10#include <linux/kernel.h>
11#include <linux/netfilter.h>
12#include <net/protocol.h>
13#include <linux/init.h>
14#include <linux/skbuff.h>
15#include <linux/wait.h>
16#include <linux/module.h>
17#include <linux/interrupt.h>
18#include <linux/if.h>
19#include <linux/netdevice.h>
56768644 20#include <linux/netfilter_ipv6.h>
f6ebe77f
HW
21#include <linux/inetdevice.h>
22#include <linux/proc_fs.h>
d486dd1f 23#include <linux/mutex.h>
5a0e3ad6 24#include <linux/slab.h>
457c4cbc 25#include <net/net_namespace.h>
f6ebe77f
HW
26#include <net/sock.h>
27
28#include "nf_internals.h"
29
d486dd1f 30static DEFINE_MUTEX(afinfo_mutex);
bce8032e 31
0906a372 32const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
bce8032e 33EXPORT_SYMBOL(nf_afinfo);
2a7851bf
FW
34const struct nf_ipv6_ops __rcu *nf_ipv6_ops __read_mostly;
35EXPORT_SYMBOL_GPL(nf_ipv6_ops);
bce8032e 36
1e796fda 37int nf_register_afinfo(const struct nf_afinfo *afinfo)
bce8032e 38{
7926dbfa 39 mutex_lock(&afinfo_mutex);
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 57
d1c85c2e 58#ifdef HAVE_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;
f6ebe77f 68
7926dbfa 69 mutex_lock(&nf_hook_mutex);
4c610979
LZ
70 list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {
71 if (reg->priority < elem->priority)
f6ebe77f
HW
72 break;
73 }
4c610979 74 list_add_rcu(&reg->list, elem->list.prev);
fd706d69 75 mutex_unlock(&nf_hook_mutex);
d1c85c2e 76#ifdef HAVE_JUMP_LABEL
c5905afb 77 static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]);
a2d7ec58 78#endif
f6ebe77f
HW
79 return 0;
80}
81EXPORT_SYMBOL(nf_register_hook);
82
83void nf_unregister_hook(struct nf_hook_ops *reg)
84{
fd706d69 85 mutex_lock(&nf_hook_mutex);
f6ebe77f 86 list_del_rcu(&reg->list);
fd706d69 87 mutex_unlock(&nf_hook_mutex);
d1c85c2e 88#ifdef HAVE_JUMP_LABEL
c5905afb 89 static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
a2d7ec58 90#endif
f6ebe77f
HW
91 synchronize_net();
92}
93EXPORT_SYMBOL(nf_unregister_hook);
94
972d1cb1
PM
95int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
96{
97 unsigned int i;
98 int err = 0;
99
100 for (i = 0; i < n; i++) {
101 err = nf_register_hook(&reg[i]);
102 if (err)
103 goto err;
104 }
105 return err;
106
107err:
108 if (i > 0)
109 nf_unregister_hooks(reg, i);
110 return err;
111}
112EXPORT_SYMBOL(nf_register_hooks);
113
114void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
115{
f68c5301
CG
116 while (n-- > 0)
117 nf_unregister_hook(&reg[n]);
972d1cb1
PM
118}
119EXPORT_SYMBOL(nf_unregister_hooks);
120
f6ebe77f 121unsigned int nf_iterate(struct list_head *head,
3db05fea 122 struct sk_buff *skb,
cfdfab31
DM
123 struct nf_hook_state *state,
124 struct nf_hook_ops **elemp)
f6ebe77f
HW
125{
126 unsigned int verdict;
127
128 /*
129 * The caller must not block between calls to this
130 * function because of risk of continuing from deleted element.
131 */
2a6decfd 132 list_for_each_entry_continue_rcu((*elemp), head, list) {
cfdfab31 133 if (state->thresh > (*elemp)->priority)
f6ebe77f
HW
134 continue;
135
136 /* Optimization: we don't need to hold module
601e68e1 137 reference here, since function can't sleep. --RR */
de9963f0 138repeat:
cfdfab31
DM
139 verdict = (*elemp)->hook(*elemp, skb, state->in, state->out,
140 state->okfn);
f6ebe77f
HW
141 if (verdict != NF_ACCEPT) {
142#ifdef CONFIG_NETFILTER_DEBUG
143 if (unlikely((verdict & NF_VERDICT_MASK)
144 > NF_MAX_VERDICT)) {
145 NFDEBUG("Evil return from %p(%u).\n",
cfdfab31 146 (*elemp)->hook, state->hook);
f6ebe77f
HW
147 continue;
148 }
149#endif
2a6decfd 150 if (verdict != NF_REPEAT)
f6ebe77f 151 return verdict;
de9963f0 152 goto repeat;
f6ebe77f
HW
153 }
154 }
155 return NF_ACCEPT;
156}
157
158
159/* Returns 1 if okfn() needs to be executed by the caller,
160 * -EPERM for NF_DROP, 0 otherwise. */
cfdfab31 161int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state)
f6ebe77f 162{
2a6decfd 163 struct nf_hook_ops *elem;
f6ebe77f
HW
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
cfdfab31
DM
170 elem = list_entry_rcu(&nf_hooks[state->pf][state->hook],
171 struct nf_hook_ops, list);
f6ebe77f 172next_hook:
cfdfab31
DM
173 verdict = nf_iterate(&nf_hooks[state->pf][state->hook], skb, state,
174 &elem);
f6ebe77f
HW
175 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
176 ret = 1;
da683650 177 } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) {
3db05fea 178 kfree_skb(skb);
f615df76 179 ret = NF_DROP_GETERR(verdict);
da683650
EP
180 if (ret == 0)
181 ret = -EPERM;
f9c63990 182 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
cfdfab31
DM
183 int err = nf_queue(skb, elem, state,
184 verdict >> NF_VERDICT_QBITS);
563e1232
FW
185 if (err < 0) {
186 if (err == -ECANCELED)
06cdb634 187 goto next_hook;
563e1232 188 if (err == -ESRCH &&
94b27cc3
FW
189 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
190 goto next_hook;
06cdb634
FW
191 kfree_skb(skb);
192 }
f6ebe77f 193 }
f6ebe77f
HW
194 rcu_read_unlock();
195 return ret;
196}
197EXPORT_SYMBOL(nf_hook_slow);
198
199
37d41879 200int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
f6ebe77f 201{
37d41879 202 if (writable_len > skb->len)
f6ebe77f
HW
203 return 0;
204
205 /* Not exclusive use of packet? Must copy. */
37d41879
HX
206 if (!skb_cloned(skb)) {
207 if (writable_len <= skb_headlen(skb))
208 return 1;
209 } else if (skb_clone_writable(skb, writable_len))
210 return 1;
211
212 if (writable_len <= skb_headlen(skb))
213 writable_len = 0;
214 else
215 writable_len -= skb_headlen(skb);
216
217 return !!__pskb_pull_tail(skb, writable_len);
f6ebe77f
HW
218}
219EXPORT_SYMBOL(skb_make_writable);
220
c0cd1156 221#if IS_ENABLED(CONFIG_NF_CONNTRACK)
f6ebe77f
HW
222/* This does not belong here, but locally generated errors need it if connection
223 tracking in use: without this, connection may not be in hash table, and hence
224 manufactured ICMP or RST packets will not be associated with it. */
312a0c16
PM
225void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *)
226 __rcu __read_mostly;
f6ebe77f
HW
227EXPORT_SYMBOL(ip_ct_attach);
228
312a0c16 229void nf_ct_attach(struct sk_buff *new, const struct sk_buff *skb)
f6ebe77f 230{
312a0c16 231 void (*attach)(struct sk_buff *, const struct sk_buff *);
f6ebe77f 232
c3a47ab3
PM
233 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();
f6ebe77f
HW
239 }
240}
241EXPORT_SYMBOL(nf_ct_attach);
de6e05c4 242
0e60ebe0 243void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly;
de6e05c4
YK
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);
9cb01766 257
5a05fae5 258struct nfq_ct_hook __rcu *nfq_ct_hook __read_mostly;
9cb01766
PNA
259EXPORT_SYMBOL_GPL(nfq_ct_hook);
260
d584a61a
PNA
261struct nfq_ct_nat_hook __rcu *nfq_ct_nat_hook __read_mostly;
262EXPORT_SYMBOL_GPL(nfq_ct_nat_hook);
263
de6e05c4 264#endif /* CONFIG_NF_CONNTRACK */
f6ebe77f 265
c7232c99
PM
266#ifdef CONFIG_NF_NAT_NEEDED
267void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
268EXPORT_SYMBOL(nf_nat_decode_session_hook);
269#endif
270
f3c1a44a
G
271static int __net_init netfilter_net_init(struct net *net)
272{
273#ifdef CONFIG_PROC_FS
274 net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter",
275 net->proc_net);
12202fa7
PNA
276 if (!net->nf.proc_netfilter) {
277 if (!net_eq(net, &init_net))
278 pr_err("cannot create netfilter proc entry");
279
f3c1a44a
G
280 return -ENOMEM;
281 }
282#endif
283 return 0;
284}
285
286static void __net_exit netfilter_net_exit(struct net *net)
287{
288 remove_proc_entry("netfilter", net->proc_net);
289}
290
291static struct pernet_operations netfilter_net_ops = {
292 .init = netfilter_net_init,
293 .exit = netfilter_net_exit,
294};
295
6d11cfdb 296int __init netfilter_init(void)
f6ebe77f 297{
6d11cfdb
PNA
298 int i, h, ret;
299
7e9c6eeb 300 for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) {
f6ebe77f
HW
301 for (h = 0; h < NF_MAX_HOOKS; h++)
302 INIT_LIST_HEAD(&nf_hooks[i][h]);
303 }
304
6d11cfdb
PNA
305 ret = register_pernet_subsys(&netfilter_net_ops);
306 if (ret < 0)
307 goto err;
308
309 ret = netfilter_log_init();
310 if (ret < 0)
311 goto err_pernet;
f6ebe77f 312
6d11cfdb
PNA
313 return 0;
314err_pernet:
315 unregister_pernet_subsys(&netfilter_net_ops);
316err:
317 return ret;
f6ebe77f 318}
This page took 0.739899 seconds and 5 git commands to generate.