import PULS_20160108
[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 const struct nf_ipv6_ops __rcu *nf_ipv6_ops __read_mostly;
34 EXPORT_SYMBOL_GPL(nf_ipv6_ops);
35
36 int nf_register_afinfo(const struct nf_afinfo *afinfo)
37 {
38 int err;
39
40 err = mutex_lock_interruptible(&afinfo_mutex);
41 if (err < 0)
42 return err;
43 RCU_INIT_POINTER(nf_afinfo[afinfo->family], afinfo);
44 mutex_unlock(&afinfo_mutex);
45 return 0;
46 }
47 EXPORT_SYMBOL_GPL(nf_register_afinfo);
48
49 void nf_unregister_afinfo(const struct nf_afinfo *afinfo)
50 {
51 mutex_lock(&afinfo_mutex);
52 RCU_INIT_POINTER(nf_afinfo[afinfo->family], NULL);
53 mutex_unlock(&afinfo_mutex);
54 synchronize_rcu();
55 }
56 EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
57
58 struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS] __read_mostly;
59 EXPORT_SYMBOL(nf_hooks);
60
61 #if defined(CONFIG_JUMP_LABEL)
62 struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
63 EXPORT_SYMBOL(nf_hooks_needed);
64 #endif
65
66 static DEFINE_MUTEX(nf_hook_mutex);
67
68 int nf_register_hook(struct nf_hook_ops *reg)
69 {
70 struct nf_hook_ops *elem;
71 int err;
72
73 err = mutex_lock_interruptible(&nf_hook_mutex);
74 if (err < 0)
75 return err;
76 list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {
77 if (reg->priority < elem->priority)
78 break;
79 }
80 list_add_rcu(&reg->list, elem->list.prev);
81 mutex_unlock(&nf_hook_mutex);
82 #if defined(CONFIG_JUMP_LABEL)
83 static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]);
84 #endif
85 return 0;
86 }
87 EXPORT_SYMBOL(nf_register_hook);
88
89 void nf_unregister_hook(struct nf_hook_ops *reg)
90 {
91 mutex_lock(&nf_hook_mutex);
92 list_del_rcu(&reg->list);
93 mutex_unlock(&nf_hook_mutex);
94 #if defined(CONFIG_JUMP_LABEL)
95 static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
96 #endif
97 synchronize_net();
98 }
99 EXPORT_SYMBOL(nf_unregister_hook);
100
101 int 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
113 err:
114 if (i > 0)
115 nf_unregister_hooks(reg, i);
116 return err;
117 }
118 EXPORT_SYMBOL(nf_register_hooks);
119
120 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
121 {
122 while (n-- > 0)
123 nf_unregister_hook(&reg[n]);
124 }
125 EXPORT_SYMBOL(nf_unregister_hooks);
126
127 unsigned int nf_iterate(struct list_head *head,
128 struct sk_buff *skb,
129 unsigned int hook,
130 const struct net_device *indev,
131 const struct net_device *outdev,
132 struct nf_hook_ops **elemp,
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 */
142 list_for_each_entry_continue_rcu((*elemp), head, list) {
143 if (hook_thresh > (*elemp)->priority)
144 continue;
145
146 /* Optimization: we don't need to hold module
147 reference here, since function can't sleep. --RR */
148 repeat:
149 verdict = (*elemp)->hook(hook, skb, indev, outdev, okfn);
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",
155 (*elemp)->hook, hook);
156 continue;
157 }
158 #endif
159 if (verdict != NF_REPEAT)
160 return verdict;
161 goto repeat;
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. */
170 int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
171 struct net_device *indev,
172 struct net_device *outdev,
173 int (*okfn)(struct sk_buff *),
174 int hook_thresh)
175 {
176 struct nf_hook_ops *elem;
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
183 elem = list_entry_rcu(&nf_hooks[pf][hook], struct nf_hook_ops, list);
184 next_hook:
185 verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev,
186 outdev, &elem, okfn, hook_thresh);
187 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
188 ret = 1;
189 } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) {
190 #ifdef CONFIG_MTK_NET_LOGGING
191 printk(KERN_INFO "[mtk_net][core]nf_hook_slow drop! "
192 "len = %d, pf =%d, hook = %d, func = %pS, dev = %s\n",
193 skb->len, pf, hook, ((struct nf_hook_ops *)elem)->hook, skb->dev->name);
194 #endif
195 kfree_skb(skb);
196 ret = NF_DROP_GETERR(verdict);
197 if (ret == 0)
198 ret = -EPERM;
199 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
200 int err = nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
201 verdict >> NF_VERDICT_QBITS);
202 if (err < 0) {
203 if (err == -ECANCELED)
204 goto next_hook;
205 if (err == -ESRCH &&
206 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
207 goto next_hook;
208 kfree_skb(skb);
209 }
210 }
211 rcu_read_unlock();
212 return ret;
213 }
214 EXPORT_SYMBOL(nf_hook_slow);
215
216
217 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
218 {
219 if (writable_len > skb->len)
220 return 0;
221
222 /* Not exclusive use of packet? Must copy. */
223 if (!skb_cloned(skb)) {
224 if (writable_len <= skb_headlen(skb))
225 return 1;
226 } else if (skb_clone_writable(skb, writable_len))
227 return 1;
228
229 if (writable_len <= skb_headlen(skb))
230 writable_len = 0;
231 else
232 writable_len -= skb_headlen(skb);
233
234 return !!__pskb_pull_tail(skb, writable_len);
235 }
236 EXPORT_SYMBOL(skb_make_writable);
237
238 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
239 /* This does not belong here, but locally generated errors need it if connection
240 tracking in use: without this, connection may not be in hash table, and hence
241 manufactured ICMP or RST packets will not be associated with it. */
242 void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *) __rcu __read_mostly;
243 EXPORT_SYMBOL(ip_ct_attach);
244
245 void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
246 {
247 void (*attach)(struct sk_buff *, struct sk_buff *);
248
249 if (skb->nfct) {
250 rcu_read_lock();
251 attach = rcu_dereference(ip_ct_attach);
252 if (attach)
253 attach(new, skb);
254 rcu_read_unlock();
255 }
256 }
257 EXPORT_SYMBOL(nf_ct_attach);
258
259 void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly;
260 EXPORT_SYMBOL(nf_ct_destroy);
261
262 void nf_conntrack_destroy(struct nf_conntrack *nfct)
263 {
264 void (*destroy)(struct nf_conntrack *);
265
266 rcu_read_lock();
267 destroy = rcu_dereference(nf_ct_destroy);
268 BUG_ON(destroy == NULL);
269 destroy(nfct);
270 rcu_read_unlock();
271 }
272 EXPORT_SYMBOL(nf_conntrack_destroy);
273
274 struct nfq_ct_hook __rcu *nfq_ct_hook __read_mostly;
275 EXPORT_SYMBOL_GPL(nfq_ct_hook);
276
277 struct nfq_ct_nat_hook __rcu *nfq_ct_nat_hook __read_mostly;
278 EXPORT_SYMBOL_GPL(nfq_ct_nat_hook);
279
280 #endif /* CONFIG_NF_CONNTRACK */
281
282 #ifdef CONFIG_NF_NAT_NEEDED
283 void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
284 EXPORT_SYMBOL(nf_nat_decode_session_hook);
285 #endif
286
287 static int __net_init netfilter_net_init(struct net *net)
288 {
289 #ifdef CONFIG_PROC_FS
290 net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter",
291 net->proc_net);
292 if (!net->nf.proc_netfilter) {
293 if (!net_eq(net, &init_net))
294 pr_err("cannot create netfilter proc entry");
295
296 return -ENOMEM;
297 }
298 #endif
299 return 0;
300 }
301
302 static void __net_exit netfilter_net_exit(struct net *net)
303 {
304 remove_proc_entry("netfilter", net->proc_net);
305 }
306
307 static struct pernet_operations netfilter_net_ops = {
308 .init = netfilter_net_init,
309 .exit = netfilter_net_exit,
310 };
311
312 void __init netfilter_init(void)
313 {
314 int i, h;
315 for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) {
316 for (h = 0; h < NF_MAX_HOOKS; h++)
317 INIT_LIST_HEAD(&nf_hooks[i][h]);
318 }
319
320 if (register_pernet_subsys(&netfilter_net_ops) < 0)
321 panic("cannot create netfilter proc entry");
322
323 if (netfilter_log_init() < 0)
324 panic("cannot initialize nf_log");
325 }