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