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