blob: 3f3ac57b2998f20a901410d4f997b2b2f794e3fb [file] [log] [blame]
Patrick McHardyf229f6c2013-04-06 15:24:29 +02001/*
2 * Rusty Russell (C)2000 -- This code is GPL.
3 * Patrick McHardy (c) 2006-2012
4 */
5
Harald Weltef6ebe772005-08-09 20:21:49 -07006#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Harald Weltef6ebe772005-08-09 20:21:49 -07008#include <linux/init.h>
9#include <linux/module.h>
10#include <linux/proc_fs.h>
11#include <linux/skbuff.h>
12#include <linux/netfilter.h>
Harald Weltebbd86b9f2005-08-09 20:23:11 -070013#include <linux/seq_file.h>
Patrick McHardy7a11b982006-02-27 13:03:24 -080014#include <linux/rcupdate.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070015#include <net/protocol.h>
Patrick McHardyc01cd422007-12-05 01:24:48 -080016#include <net/netfilter/nf_queue.h>
Eric Dumazet7fee2262010-05-11 23:19:48 +000017#include <net/dst.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070018
19#include "nf_internals.h"
20
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080021/*
Florian Westphal0360ae42012-11-23 06:22:21 +000022 * Hook for nfnetlink_queue to register its queue handler.
23 * We do this so that most of the NFQUEUE code can be modular.
24 *
25 * Once the queue is registered it must reinject all packets it
26 * receives, no matter what.
Harald Weltef6ebe772005-08-09 20:21:49 -070027 */
Florian Westphal0360ae42012-11-23 06:22:21 +000028static const struct nf_queue_handler __rcu *queue_handler __read_mostly;
Harald Weltef6ebe772005-08-09 20:21:49 -070029
Harald Welted72367b2005-08-09 20:23:36 -070030/* return EBUSY when somebody else is registered, return EEXIST if the
31 * same handler is registered, return 0 in case of success. */
Florian Westphal0360ae42012-11-23 06:22:21 +000032void nf_register_queue_handler(const struct nf_queue_handler *qh)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080033{
Florian Westphal0360ae42012-11-23 06:22:21 +000034 /* should never happen, we only have one queueing backend in kernel */
35 WARN_ON(rcu_access_pointer(queue_handler));
36 rcu_assign_pointer(queue_handler, qh);
Harald Weltef6ebe772005-08-09 20:21:49 -070037}
38EXPORT_SYMBOL(nf_register_queue_handler);
39
40/* The caller must flush their queue before this */
Florian Westphal0360ae42012-11-23 06:22:21 +000041void nf_unregister_queue_handler(void)
Harald Weltef6ebe772005-08-09 20:21:49 -070042{
Florian Westphal0360ae42012-11-23 06:22:21 +000043 RCU_INIT_POINTER(queue_handler, NULL);
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070044 synchronize_rcu();
Harald Weltef6ebe772005-08-09 20:21:49 -070045}
46EXPORT_SYMBOL(nf_unregister_queue_handler);
47
Florian Westphala5fedd432013-04-19 04:58:25 +000048void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
Patrick McHardydaaa8be2007-12-05 01:27:19 -080049{
David S. Miller1d1de892015-04-03 16:31:01 -040050 struct nf_hook_state *state = &entry->state;
51
Patrick McHardydaaa8be2007-12-05 01:27:19 -080052 /* Release those devices we held, or Alexey will kill me. */
David S. Miller1d1de892015-04-03 16:31:01 -040053 if (state->in)
54 dev_put(state->in);
55 if (state->out)
56 dev_put(state->out);
David Miller1c984f82015-04-05 22:19:00 -040057 if (state->sk)
58 sock_put(state->sk);
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +020059#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Patrick McHardydaaa8be2007-12-05 01:27:19 -080060 if (entry->skb->nf_bridge) {
61 struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge;
62
63 if (nf_bridge->physindev)
64 dev_put(nf_bridge->physindev);
65 if (nf_bridge->physoutdev)
66 dev_put(nf_bridge->physoutdev);
67 }
68#endif
69 /* Drop reference to owner of hook which queued us. */
70 module_put(entry->elem->owner);
71}
Florian Westphala5fedd432013-04-19 04:58:25 +000072EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
Patrick McHardydaaa8be2007-12-05 01:27:19 -080073
Florian Westphal4bd60442013-04-19 04:58:23 +000074/* Bump dev refs so they don't vanish while packet is out */
Florian Westphala5fedd432013-04-19 04:58:25 +000075bool nf_queue_entry_get_refs(struct nf_queue_entry *entry)
Florian Westphal4bd60442013-04-19 04:58:23 +000076{
David S. Miller1d1de892015-04-03 16:31:01 -040077 struct nf_hook_state *state = &entry->state;
78
Florian Westphal4bd60442013-04-19 04:58:23 +000079 if (!try_module_get(entry->elem->owner))
80 return false;
81
David S. Miller1d1de892015-04-03 16:31:01 -040082 if (state->in)
83 dev_hold(state->in);
84 if (state->out)
85 dev_hold(state->out);
David Miller1c984f82015-04-05 22:19:00 -040086 if (state->sk)
87 sock_hold(state->sk);
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +020088#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Florian Westphal4bd60442013-04-19 04:58:23 +000089 if (entry->skb->nf_bridge) {
90 struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge;
91 struct net_device *physdev;
92
93 physdev = nf_bridge->physindev;
94 if (physdev)
95 dev_hold(physdev);
96 physdev = nf_bridge->physoutdev;
97 if (physdev)
98 dev_hold(physdev);
99 }
100#endif
101
102 return true;
103}
Florian Westphala5fedd432013-04-19 04:58:25 +0000104EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
Florian Westphal4bd60442013-04-19 04:58:23 +0000105
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800106/*
107 * Any packet that leaves via this function must come back
Harald Weltef6ebe772005-08-09 20:21:49 -0700108 * through nf_reinject().
109 */
Florian Westphala5fedd432013-04-19 04:58:25 +0000110int nf_queue(struct sk_buff *skb,
David S. Millercfdfab32015-04-03 16:23:58 -0400111 struct nf_hook_ops *elem,
112 struct nf_hook_state *state,
113 unsigned int queuenum)
Harald Weltef6ebe772005-08-09 20:21:49 -0700114{
Florian Westphalf1585082011-01-18 15:27:28 +0100115 int status = -ENOENT;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800116 struct nf_queue_entry *entry = NULL;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800117 const struct nf_afinfo *afinfo;
Patrick McHardye3ac5292007-12-05 01:23:57 -0800118 const struct nf_queue_handler *qh;
Harald Weltef6ebe772005-08-09 20:21:49 -0700119
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300120 /* QUEUE == DROP if no one is waiting, to be safe. */
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700121 rcu_read_lock();
122
Florian Westphal0360ae42012-11-23 06:22:21 +0000123 qh = rcu_dereference(queue_handler);
Florian Westphal94b27cc2011-01-18 16:08:30 +0100124 if (!qh) {
125 status = -ESRCH;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800126 goto err_unlock;
Florian Westphal94b27cc2011-01-18 16:08:30 +0100127 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700128
David S. Millercfdfab32015-04-03 16:23:58 -0400129 afinfo = nf_get_afinfo(state->pf);
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800130 if (!afinfo)
131 goto err_unlock;
Patrick McHardybce80322006-04-06 14:18:09 -0700132
Patrick McHardy02f014d2007-12-05 01:26:33 -0800133 entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC);
Florian Westphalf1585082011-01-18 15:27:28 +0100134 if (!entry) {
135 status = -ENOMEM;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800136 goto err_unlock;
Florian Westphalf1585082011-01-18 15:27:28 +0100137 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700138
Patrick McHardy02f014d2007-12-05 01:26:33 -0800139 *entry = (struct nf_queue_entry) {
140 .skb = skb,
Michael Wang1c15b672012-08-22 20:00:06 +0000141 .elem = elem,
David S. Miller1d1de892015-04-03 16:31:01 -0400142 .state = *state,
Florian Westphala5fedd432013-04-19 04:58:25 +0000143 .size = sizeof(*entry) + afinfo->route_key_size,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800144 };
Harald Weltef6ebe772005-08-09 20:21:49 -0700145
Florian Westphal4bd60442013-04-19 04:58:23 +0000146 if (!nf_queue_entry_get_refs(entry)) {
Florian Westphal06cdb632011-01-18 15:28:38 +0100147 status = -ECANCELED;
148 goto err_unlock;
Harald Weltef6ebe772005-08-09 20:21:49 -0700149 }
Eric Dumazet7fee2262010-05-11 23:19:48 +0000150 skb_dst_force(skb);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800151 afinfo->saveroute(skb, entry);
152 status = qh->outfn(entry, queuenum);
Harald Weltef6ebe772005-08-09 20:21:49 -0700153
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700154 rcu_read_unlock();
Harald Weltef6ebe772005-08-09 20:21:49 -0700155
156 if (status < 0) {
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800157 nf_queue_entry_release_refs(entry);
158 goto err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700159 }
160
Florian Westphalf1585082011-01-18 15:27:28 +0100161 return 0;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800162
163err_unlock:
164 rcu_read_unlock();
165err:
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800166 kfree(entry);
Florian Westphalf1585082011-01-18 15:27:28 +0100167 return status;
Harald Weltef6ebe772005-08-09 20:21:49 -0700168}
169
Patrick McHardy02f014d2007-12-05 01:26:33 -0800170void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
Harald Weltef6ebe772005-08-09 20:21:49 -0700171{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800172 struct sk_buff *skb = entry->skb;
Michael Wang2a6decf2012-08-22 19:59:57 +0000173 struct nf_hook_ops *elem = entry->elem;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800174 const struct nf_afinfo *afinfo;
Florian Westphalf1585082011-01-18 15:27:28 +0100175 int err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700176
177 rcu_read_lock();
178
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800179 nf_queue_entry_release_refs(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700180
Harald Weltef6ebe772005-08-09 20:21:49 -0700181 /* Continue traversal iff userspace said ok... */
182 if (verdict == NF_REPEAT) {
Michael Wang2a6decf2012-08-22 19:59:57 +0000183 elem = list_entry(elem->list.prev, struct nf_hook_ops, list);
Harald Weltef6ebe772005-08-09 20:21:49 -0700184 verdict = NF_ACCEPT;
185 }
186
187 if (verdict == NF_ACCEPT) {
David S. Miller1d1de892015-04-03 16:31:01 -0400188 afinfo = nf_get_afinfo(entry->state.pf);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800189 if (!afinfo || afinfo->reroute(skb, entry) < 0)
Patrick McHardy7a11b982006-02-27 13:03:24 -0800190 verdict = NF_DROP;
191 }
192
David S. Miller1d1de892015-04-03 16:31:01 -0400193 entry->state.thresh = INT_MIN;
David S. Millercfdfab32015-04-03 16:23:58 -0400194
Patrick McHardy7a11b982006-02-27 13:03:24 -0800195 if (verdict == NF_ACCEPT) {
Harald Weltef6ebe772005-08-09 20:21:49 -0700196 next_hook:
David S. Miller1d1de892015-04-03 16:31:01 -0400197 verdict = nf_iterate(&nf_hooks[entry->state.pf][entry->state.hook],
198 skb, &entry->state, &elem);
Harald Weltef6ebe772005-08-09 20:21:49 -0700199 }
200
201 switch (verdict & NF_VERDICT_MASK) {
202 case NF_ACCEPT:
Patrick McHardy3bc38712006-07-24 22:52:47 -0700203 case NF_STOP:
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800204 local_bh_disable();
David Miller7026b1d2015-04-05 22:19:04 -0400205 entry->state.okfn(entry->state.sk, skb);
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800206 local_bh_enable();
Harald Weltef6ebe772005-08-09 20:21:49 -0700207 break;
Harald Weltef6ebe772005-08-09 20:21:49 -0700208 case NF_QUEUE:
David S. Miller1d1de892015-04-03 16:31:01 -0400209 err = nf_queue(skb, elem, &entry->state,
David S. Millercfdfab32015-04-03 16:23:58 -0400210 verdict >> NF_VERDICT_QBITS);
Florian Westphal06cdb632011-01-18 15:28:38 +0100211 if (err < 0) {
212 if (err == -ECANCELED)
213 goto next_hook;
Florian Westphal94b27cc2011-01-18 16:08:30 +0100214 if (err == -ESRCH &&
215 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
216 goto next_hook;
Florian Westphal06cdb632011-01-18 15:28:38 +0100217 kfree_skb(skb);
218 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700219 break;
Eric Dumazet64507fd2010-02-19 15:28:38 +0100220 case NF_STOLEN:
Julian Anastasovfad54442011-08-05 00:36:28 +0000221 break;
Patrick McHardy3bc38712006-07-24 22:52:47 -0700222 default:
223 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700224 }
225 rcu_read_unlock();
Patrick McHardy02f014d2007-12-05 01:26:33 -0800226 kfree(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700227}
228EXPORT_SYMBOL(nf_reinject);