Commit | Line | Data |
---|---|---|
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 | 30 | static DEFINE_MUTEX(afinfo_mutex); |
bce8032e | 31 | |
0906a372 | 32 | const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly; |
bce8032e | 33 | EXPORT_SYMBOL(nf_afinfo); |
2a7851bf FW |
34 | const struct nf_ipv6_ops __rcu *nf_ipv6_ops __read_mostly; |
35 | EXPORT_SYMBOL_GPL(nf_ipv6_ops); | |
bce8032e | 36 | |
e7c8899f FW |
37 | DEFINE_PER_CPU(bool, nf_skb_duplicated); |
38 | EXPORT_SYMBOL_GPL(nf_skb_duplicated); | |
39 | ||
1e796fda | 40 | int nf_register_afinfo(const struct nf_afinfo *afinfo) |
bce8032e | 41 | { |
7926dbfa | 42 | mutex_lock(&afinfo_mutex); |
a9b3cd7f | 43 | RCU_INIT_POINTER(nf_afinfo[afinfo->family], afinfo); |
d486dd1f | 44 | mutex_unlock(&afinfo_mutex); |
bce8032e PM |
45 | return 0; |
46 | } | |
47 | EXPORT_SYMBOL_GPL(nf_register_afinfo); | |
48 | ||
1e796fda | 49 | void 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 | } | |
56 | EXPORT_SYMBOL_GPL(nf_unregister_afinfo); | |
57 | ||
d1c85c2e | 58 | #ifdef HAVE_JUMP_LABEL |
c5905afb | 59 | struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS]; |
a2d7ec58 ED |
60 | EXPORT_SYMBOL(nf_hooks_needed); |
61 | #endif | |
62 | ||
fd706d69 | 63 | static DEFINE_MUTEX(nf_hook_mutex); |
f6ebe77f | 64 | |
3bbd14e0 | 65 | static struct list_head *nf_find_hook_list(struct net *net, |
085db2c0 | 66 | const struct nf_hook_ops *reg) |
f6ebe77f | 67 | { |
3bbd14e0 | 68 | struct list_head *hook_list = NULL; |
f6ebe77f | 69 | |
0edcf282 | 70 | if (reg->pf != NFPROTO_NETDEV) |
3bbd14e0 | 71 | hook_list = &net->nf.hooks[reg->pf][reg->hooknum]; |
0edcf282 | 72 | else if (reg->hooknum == NF_NETDEV_INGRESS) { |
e687ad60 | 73 | #ifdef CONFIG_NETFILTER_INGRESS |
085db2c0 | 74 | if (reg->dev && dev_net(reg->dev) == net) |
3bbd14e0 | 75 | hook_list = ®->dev->nf_hooks_ingress; |
e687ad60 | 76 | #endif |
e687ad60 | 77 | } |
3bbd14e0 | 78 | return hook_list; |
0edcf282 | 79 | } |
e687ad60 | 80 | |
7181ebaf PNA |
81 | struct nf_hook_entry { |
82 | const struct nf_hook_ops *orig_ops; | |
83 | struct nf_hook_ops ops; | |
84 | }; | |
85 | ||
085db2c0 | 86 | int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg) |
0edcf282 | 87 | { |
3bbd14e0 | 88 | struct list_head *hook_list; |
7181ebaf PNA |
89 | struct nf_hook_entry *entry; |
90 | struct nf_hook_ops *elem; | |
085db2c0 | 91 | |
7181ebaf PNA |
92 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
93 | if (!entry) | |
085db2c0 | 94 | return -ENOMEM; |
0edcf282 | 95 | |
7181ebaf PNA |
96 | entry->orig_ops = reg; |
97 | entry->ops = *reg; | |
085db2c0 | 98 | |
3bbd14e0 PNA |
99 | hook_list = nf_find_hook_list(net, reg); |
100 | if (!hook_list) { | |
7181ebaf | 101 | kfree(entry); |
0edcf282 | 102 | return -ENOENT; |
e317fa50 | 103 | } |
0edcf282 EB |
104 | |
105 | mutex_lock(&nf_hook_mutex); | |
3bbd14e0 | 106 | list_for_each_entry(elem, hook_list, list) { |
4c610979 | 107 | if (reg->priority < elem->priority) |
f6ebe77f HW |
108 | break; |
109 | } | |
7181ebaf | 110 | list_add_rcu(&entry->ops.list, elem->list.prev); |
fd706d69 | 111 | mutex_unlock(&nf_hook_mutex); |
4c091156 EB |
112 | #ifdef CONFIG_NETFILTER_INGRESS |
113 | if (reg->pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_INGRESS) | |
114 | net_inc_ingress_queue(); | |
115 | #endif | |
d1c85c2e | 116 | #ifdef HAVE_JUMP_LABEL |
c5905afb | 117 | static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]); |
a2d7ec58 | 118 | #endif |
f6ebe77f HW |
119 | return 0; |
120 | } | |
085db2c0 | 121 | EXPORT_SYMBOL(nf_register_net_hook); |
f6ebe77f | 122 | |
085db2c0 | 123 | void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg) |
f6ebe77f | 124 | { |
3bbd14e0 | 125 | struct list_head *hook_list; |
7181ebaf | 126 | struct nf_hook_entry *entry; |
085db2c0 EB |
127 | struct nf_hook_ops *elem; |
128 | ||
3bbd14e0 PNA |
129 | hook_list = nf_find_hook_list(net, reg); |
130 | if (!hook_list) | |
085db2c0 EB |
131 | return; |
132 | ||
fd706d69 | 133 | mutex_lock(&nf_hook_mutex); |
3bbd14e0 | 134 | list_for_each_entry(elem, hook_list, list) { |
7181ebaf PNA |
135 | entry = container_of(elem, struct nf_hook_entry, ops); |
136 | if (entry->orig_ops == reg) { | |
137 | list_del_rcu(&entry->ops.list); | |
085db2c0 EB |
138 | break; |
139 | } | |
140 | } | |
fd706d69 | 141 | mutex_unlock(&nf_hook_mutex); |
3bbd14e0 | 142 | if (&elem->list == hook_list) { |
085db2c0 EB |
143 | WARN(1, "nf_unregister_net_hook: hook not found!\n"); |
144 | return; | |
145 | } | |
e687ad60 | 146 | #ifdef CONFIG_NETFILTER_INGRESS |
4c091156 EB |
147 | if (reg->pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_INGRESS) |
148 | net_dec_ingress_queue(); | |
e687ad60 | 149 | #endif |
d1c85c2e | 150 | #ifdef HAVE_JUMP_LABEL |
c5905afb | 151 | static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]); |
a2d7ec58 | 152 | #endif |
f6ebe77f | 153 | synchronize_net(); |
7181ebaf | 154 | nf_queue_nf_hook_drop(net, &entry->ops); |
514ed62e FW |
155 | /* other cpu might still process nfqueue verdict that used reg */ |
156 | synchronize_net(); | |
7181ebaf | 157 | kfree(entry); |
085db2c0 EB |
158 | } |
159 | EXPORT_SYMBOL(nf_unregister_net_hook); | |
160 | ||
161 | int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg, | |
162 | unsigned int n) | |
163 | { | |
164 | unsigned int i; | |
165 | int err = 0; | |
166 | ||
167 | for (i = 0; i < n; i++) { | |
168 | err = nf_register_net_hook(net, ®[i]); | |
169 | if (err) | |
170 | goto err; | |
171 | } | |
172 | return err; | |
173 | ||
174 | err: | |
175 | if (i > 0) | |
176 | nf_unregister_net_hooks(net, reg, i); | |
177 | return err; | |
178 | } | |
179 | EXPORT_SYMBOL(nf_register_net_hooks); | |
180 | ||
181 | void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg, | |
182 | unsigned int n) | |
183 | { | |
184 | while (n-- > 0) | |
185 | nf_unregister_net_hook(net, ®[n]); | |
186 | } | |
187 | EXPORT_SYMBOL(nf_unregister_net_hooks); | |
188 | ||
189 | static LIST_HEAD(nf_hook_list); | |
190 | ||
191 | int nf_register_hook(struct nf_hook_ops *reg) | |
192 | { | |
193 | struct net *net, *last; | |
194 | int ret; | |
195 | ||
196 | rtnl_lock(); | |
197 | for_each_net(net) { | |
198 | ret = nf_register_net_hook(net, reg); | |
199 | if (ret && ret != -ENOENT) | |
200 | goto rollback; | |
201 | } | |
202 | list_add_tail(®->list, &nf_hook_list); | |
203 | rtnl_unlock(); | |
204 | ||
205 | return 0; | |
206 | rollback: | |
207 | last = net; | |
208 | for_each_net(net) { | |
209 | if (net == last) | |
210 | break; | |
211 | nf_unregister_net_hook(net, reg); | |
212 | } | |
213 | rtnl_unlock(); | |
214 | return ret; | |
215 | } | |
216 | EXPORT_SYMBOL(nf_register_hook); | |
217 | ||
218 | void nf_unregister_hook(struct nf_hook_ops *reg) | |
219 | { | |
220 | struct net *net; | |
221 | ||
222 | rtnl_lock(); | |
223 | list_del(®->list); | |
224 | for_each_net(net) | |
225 | nf_unregister_net_hook(net, reg); | |
226 | rtnl_unlock(); | |
f6ebe77f HW |
227 | } |
228 | EXPORT_SYMBOL(nf_unregister_hook); | |
229 | ||
972d1cb1 PM |
230 | int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n) |
231 | { | |
232 | unsigned int i; | |
233 | int err = 0; | |
234 | ||
235 | for (i = 0; i < n; i++) { | |
236 | err = nf_register_hook(®[i]); | |
237 | if (err) | |
238 | goto err; | |
239 | } | |
240 | return err; | |
241 | ||
242 | err: | |
243 | if (i > 0) | |
244 | nf_unregister_hooks(reg, i); | |
245 | return err; | |
246 | } | |
247 | EXPORT_SYMBOL(nf_register_hooks); | |
248 | ||
249 | void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n) | |
250 | { | |
f68c5301 CG |
251 | while (n-- > 0) |
252 | nf_unregister_hook(®[n]); | |
972d1cb1 PM |
253 | } |
254 | EXPORT_SYMBOL(nf_unregister_hooks); | |
255 | ||
f6ebe77f | 256 | unsigned int nf_iterate(struct list_head *head, |
3db05fea | 257 | struct sk_buff *skb, |
cfdfab31 DM |
258 | struct nf_hook_state *state, |
259 | struct nf_hook_ops **elemp) | |
f6ebe77f HW |
260 | { |
261 | unsigned int verdict; | |
262 | ||
263 | /* | |
264 | * The caller must not block between calls to this | |
265 | * function because of risk of continuing from deleted element. | |
266 | */ | |
2a6decfd | 267 | list_for_each_entry_continue_rcu((*elemp), head, list) { |
cfdfab31 | 268 | if (state->thresh > (*elemp)->priority) |
f6ebe77f HW |
269 | continue; |
270 | ||
271 | /* Optimization: we don't need to hold module | |
601e68e1 | 272 | reference here, since function can't sleep. --RR */ |
de9963f0 | 273 | repeat: |
06198b34 | 274 | verdict = (*elemp)->hook((*elemp)->priv, skb, state); |
f6ebe77f HW |
275 | if (verdict != NF_ACCEPT) { |
276 | #ifdef CONFIG_NETFILTER_DEBUG | |
277 | if (unlikely((verdict & NF_VERDICT_MASK) | |
278 | > NF_MAX_VERDICT)) { | |
279 | NFDEBUG("Evil return from %p(%u).\n", | |
cfdfab31 | 280 | (*elemp)->hook, state->hook); |
f6ebe77f HW |
281 | continue; |
282 | } | |
283 | #endif | |
2a6decfd | 284 | if (verdict != NF_REPEAT) |
f6ebe77f | 285 | return verdict; |
de9963f0 | 286 | goto repeat; |
f6ebe77f HW |
287 | } |
288 | } | |
289 | return NF_ACCEPT; | |
290 | } | |
291 | ||
292 | ||
293 | /* Returns 1 if okfn() needs to be executed by the caller, | |
294 | * -EPERM for NF_DROP, 0 otherwise. */ | |
cfdfab31 | 295 | int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state) |
f6ebe77f | 296 | { |
2a6decfd | 297 | struct nf_hook_ops *elem; |
f6ebe77f HW |
298 | unsigned int verdict; |
299 | int ret = 0; | |
300 | ||
301 | /* We may already have this, but read-locks nest anyway */ | |
302 | rcu_read_lock(); | |
303 | ||
f7191483 | 304 | elem = list_entry_rcu(state->hook_list, struct nf_hook_ops, list); |
f6ebe77f | 305 | next_hook: |
f7191483 | 306 | verdict = nf_iterate(state->hook_list, skb, state, &elem); |
f6ebe77f HW |
307 | if (verdict == NF_ACCEPT || verdict == NF_STOP) { |
308 | ret = 1; | |
da683650 | 309 | } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) { |
3db05fea | 310 | kfree_skb(skb); |
f615df76 | 311 | ret = NF_DROP_GETERR(verdict); |
da683650 EP |
312 | if (ret == 0) |
313 | ret = -EPERM; | |
f9c63990 | 314 | } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) { |
cfdfab31 DM |
315 | int err = nf_queue(skb, elem, state, |
316 | verdict >> NF_VERDICT_QBITS); | |
563e1232 | 317 | if (err < 0) { |
563e1232 | 318 | if (err == -ESRCH && |
94b27cc3 FW |
319 | (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS)) |
320 | goto next_hook; | |
06cdb634 FW |
321 | kfree_skb(skb); |
322 | } | |
f6ebe77f | 323 | } |
f6ebe77f HW |
324 | rcu_read_unlock(); |
325 | return ret; | |
326 | } | |
327 | EXPORT_SYMBOL(nf_hook_slow); | |
328 | ||
329 | ||
37d41879 | 330 | int skb_make_writable(struct sk_buff *skb, unsigned int writable_len) |
f6ebe77f | 331 | { |
37d41879 | 332 | if (writable_len > skb->len) |
f6ebe77f HW |
333 | return 0; |
334 | ||
335 | /* Not exclusive use of packet? Must copy. */ | |
37d41879 HX |
336 | if (!skb_cloned(skb)) { |
337 | if (writable_len <= skb_headlen(skb)) | |
338 | return 1; | |
339 | } else if (skb_clone_writable(skb, writable_len)) | |
340 | return 1; | |
341 | ||
342 | if (writable_len <= skb_headlen(skb)) | |
343 | writable_len = 0; | |
344 | else | |
345 | writable_len -= skb_headlen(skb); | |
346 | ||
347 | return !!__pskb_pull_tail(skb, writable_len); | |
f6ebe77f HW |
348 | } |
349 | EXPORT_SYMBOL(skb_make_writable); | |
350 | ||
b7bd1809 PNA |
351 | /* This needs to be compiled in any case to avoid dependencies between the |
352 | * nfnetlink_queue code and nf_conntrack. | |
353 | */ | |
a4b4766c KM |
354 | struct nfnl_ct_hook __rcu *nfnl_ct_hook __read_mostly; |
355 | EXPORT_SYMBOL_GPL(nfnl_ct_hook); | |
b7bd1809 | 356 | |
c0cd1156 | 357 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
f6ebe77f HW |
358 | /* This does not belong here, but locally generated errors need it if connection |
359 | tracking in use: without this, connection may not be in hash table, and hence | |
360 | manufactured ICMP or RST packets will not be associated with it. */ | |
312a0c16 PM |
361 | void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) |
362 | __rcu __read_mostly; | |
f6ebe77f HW |
363 | EXPORT_SYMBOL(ip_ct_attach); |
364 | ||
312a0c16 | 365 | void nf_ct_attach(struct sk_buff *new, const struct sk_buff *skb) |
f6ebe77f | 366 | { |
312a0c16 | 367 | void (*attach)(struct sk_buff *, const struct sk_buff *); |
f6ebe77f | 368 | |
c3a47ab3 PM |
369 | if (skb->nfct) { |
370 | rcu_read_lock(); | |
371 | attach = rcu_dereference(ip_ct_attach); | |
372 | if (attach) | |
373 | attach(new, skb); | |
374 | rcu_read_unlock(); | |
f6ebe77f HW |
375 | } |
376 | } | |
377 | EXPORT_SYMBOL(nf_ct_attach); | |
de6e05c4 | 378 | |
0e60ebe0 | 379 | void (*nf_ct_destroy)(struct nf_conntrack *) __rcu __read_mostly; |
de6e05c4 YK |
380 | EXPORT_SYMBOL(nf_ct_destroy); |
381 | ||
382 | void nf_conntrack_destroy(struct nf_conntrack *nfct) | |
383 | { | |
384 | void (*destroy)(struct nf_conntrack *); | |
385 | ||
386 | rcu_read_lock(); | |
387 | destroy = rcu_dereference(nf_ct_destroy); | |
388 | BUG_ON(destroy == NULL); | |
389 | destroy(nfct); | |
390 | rcu_read_unlock(); | |
391 | } | |
392 | EXPORT_SYMBOL(nf_conntrack_destroy); | |
9cb01766 | 393 | |
62da9865 DB |
394 | /* Built-in default zone used e.g. by modules. */ |
395 | const struct nf_conntrack_zone nf_ct_zone_dflt = { | |
396 | .id = NF_CT_DEFAULT_ZONE_ID, | |
397 | .dir = NF_CT_DEFAULT_ZONE_DIR, | |
398 | }; | |
399 | EXPORT_SYMBOL_GPL(nf_ct_zone_dflt); | |
de6e05c4 | 400 | #endif /* CONFIG_NF_CONNTRACK */ |
f6ebe77f | 401 | |
c7232c99 PM |
402 | #ifdef CONFIG_NF_NAT_NEEDED |
403 | void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *); | |
404 | EXPORT_SYMBOL(nf_nat_decode_session_hook); | |
405 | #endif | |
406 | ||
085db2c0 EB |
407 | static int nf_register_hook_list(struct net *net) |
408 | { | |
409 | struct nf_hook_ops *elem; | |
410 | int ret; | |
411 | ||
412 | rtnl_lock(); | |
413 | list_for_each_entry(elem, &nf_hook_list, list) { | |
414 | ret = nf_register_net_hook(net, elem); | |
415 | if (ret && ret != -ENOENT) | |
416 | goto out_undo; | |
417 | } | |
418 | rtnl_unlock(); | |
419 | return 0; | |
420 | ||
421 | out_undo: | |
422 | list_for_each_entry_continue_reverse(elem, &nf_hook_list, list) | |
423 | nf_unregister_net_hook(net, elem); | |
424 | rtnl_unlock(); | |
425 | return ret; | |
426 | } | |
427 | ||
428 | static void nf_unregister_hook_list(struct net *net) | |
429 | { | |
430 | struct nf_hook_ops *elem; | |
431 | ||
432 | rtnl_lock(); | |
433 | list_for_each_entry(elem, &nf_hook_list, list) | |
434 | nf_unregister_net_hook(net, elem); | |
435 | rtnl_unlock(); | |
436 | } | |
437 | ||
f3c1a44a G |
438 | static int __net_init netfilter_net_init(struct net *net) |
439 | { | |
085db2c0 EB |
440 | int i, h, ret; |
441 | ||
442 | for (i = 0; i < ARRAY_SIZE(net->nf.hooks); i++) { | |
443 | for (h = 0; h < NF_MAX_HOOKS; h++) | |
444 | INIT_LIST_HEAD(&net->nf.hooks[i][h]); | |
445 | } | |
446 | ||
f3c1a44a G |
447 | #ifdef CONFIG_PROC_FS |
448 | net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter", | |
449 | net->proc_net); | |
12202fa7 PNA |
450 | if (!net->nf.proc_netfilter) { |
451 | if (!net_eq(net, &init_net)) | |
452 | pr_err("cannot create netfilter proc entry"); | |
453 | ||
f3c1a44a G |
454 | return -ENOMEM; |
455 | } | |
456 | #endif | |
085db2c0 EB |
457 | ret = nf_register_hook_list(net); |
458 | if (ret) | |
459 | remove_proc_entry("netfilter", net->proc_net); | |
460 | ||
461 | return ret; | |
f3c1a44a G |
462 | } |
463 | ||
464 | static void __net_exit netfilter_net_exit(struct net *net) | |
465 | { | |
085db2c0 | 466 | nf_unregister_hook_list(net); |
f3c1a44a G |
467 | remove_proc_entry("netfilter", net->proc_net); |
468 | } | |
469 | ||
470 | static struct pernet_operations netfilter_net_ops = { | |
471 | .init = netfilter_net_init, | |
472 | .exit = netfilter_net_exit, | |
473 | }; | |
474 | ||
6d11cfdb | 475 | int __init netfilter_init(void) |
f6ebe77f | 476 | { |
085db2c0 | 477 | int ret; |
f6ebe77f | 478 | |
6d11cfdb PNA |
479 | ret = register_pernet_subsys(&netfilter_net_ops); |
480 | if (ret < 0) | |
481 | goto err; | |
482 | ||
483 | ret = netfilter_log_init(); | |
484 | if (ret < 0) | |
485 | goto err_pernet; | |
f6ebe77f | 486 | |
6d11cfdb PNA |
487 | return 0; |
488 | err_pernet: | |
489 | unregister_pernet_subsys(&netfilter_net_ops); | |
490 | err: | |
491 | return ret; | |
f6ebe77f | 492 | } |