blob: d3cd37edca182707fb587c89fd54610ca5c93a8a [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);
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +020057#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Patrick McHardydaaa8be2007-12-05 01:27:19 -080058 if (entry->skb->nf_bridge) {
59 struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge;
60
61 if (nf_bridge->physindev)
62 dev_put(nf_bridge->physindev);
63 if (nf_bridge->physoutdev)
64 dev_put(nf_bridge->physoutdev);
65 }
66#endif
67 /* Drop reference to owner of hook which queued us. */
68 module_put(entry->elem->owner);
69}
Florian Westphala5fedd432013-04-19 04:58:25 +000070EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
Patrick McHardydaaa8be2007-12-05 01:27:19 -080071
Florian Westphal4bd60442013-04-19 04:58:23 +000072/* Bump dev refs so they don't vanish while packet is out */
Florian Westphala5fedd432013-04-19 04:58:25 +000073bool nf_queue_entry_get_refs(struct nf_queue_entry *entry)
Florian Westphal4bd60442013-04-19 04:58:23 +000074{
David S. Miller1d1de892015-04-03 16:31:01 -040075 struct nf_hook_state *state = &entry->state;
76
Florian Westphal4bd60442013-04-19 04:58:23 +000077 if (!try_module_get(entry->elem->owner))
78 return false;
79
David S. Miller1d1de892015-04-03 16:31:01 -040080 if (state->in)
81 dev_hold(state->in);
82 if (state->out)
83 dev_hold(state->out);
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +020084#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Florian Westphal4bd60442013-04-19 04:58:23 +000085 if (entry->skb->nf_bridge) {
86 struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge;
87 struct net_device *physdev;
88
89 physdev = nf_bridge->physindev;
90 if (physdev)
91 dev_hold(physdev);
92 physdev = nf_bridge->physoutdev;
93 if (physdev)
94 dev_hold(physdev);
95 }
96#endif
97
98 return true;
99}
Florian Westphala5fedd432013-04-19 04:58:25 +0000100EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
Florian Westphal4bd60442013-04-19 04:58:23 +0000101
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800102/*
103 * Any packet that leaves via this function must come back
Harald Weltef6ebe772005-08-09 20:21:49 -0700104 * through nf_reinject().
105 */
Florian Westphala5fedd432013-04-19 04:58:25 +0000106int nf_queue(struct sk_buff *skb,
David S. Millercfdfab32015-04-03 16:23:58 -0400107 struct nf_hook_ops *elem,
108 struct nf_hook_state *state,
109 unsigned int queuenum)
Harald Weltef6ebe772005-08-09 20:21:49 -0700110{
Florian Westphalf1585082011-01-18 15:27:28 +0100111 int status = -ENOENT;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800112 struct nf_queue_entry *entry = NULL;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800113 const struct nf_afinfo *afinfo;
Patrick McHardye3ac5292007-12-05 01:23:57 -0800114 const struct nf_queue_handler *qh;
Harald Weltef6ebe772005-08-09 20:21:49 -0700115
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300116 /* QUEUE == DROP if no one is waiting, to be safe. */
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700117 rcu_read_lock();
118
Florian Westphal0360ae42012-11-23 06:22:21 +0000119 qh = rcu_dereference(queue_handler);
Florian Westphal94b27cc2011-01-18 16:08:30 +0100120 if (!qh) {
121 status = -ESRCH;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800122 goto err_unlock;
Florian Westphal94b27cc2011-01-18 16:08:30 +0100123 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700124
David S. Millercfdfab32015-04-03 16:23:58 -0400125 afinfo = nf_get_afinfo(state->pf);
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800126 if (!afinfo)
127 goto err_unlock;
Patrick McHardybce80322006-04-06 14:18:09 -0700128
Patrick McHardy02f014d2007-12-05 01:26:33 -0800129 entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC);
Florian Westphalf1585082011-01-18 15:27:28 +0100130 if (!entry) {
131 status = -ENOMEM;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800132 goto err_unlock;
Florian Westphalf1585082011-01-18 15:27:28 +0100133 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700134
Patrick McHardy02f014d2007-12-05 01:26:33 -0800135 *entry = (struct nf_queue_entry) {
136 .skb = skb,
Michael Wang1c15b672012-08-22 20:00:06 +0000137 .elem = elem,
David S. Miller1d1de892015-04-03 16:31:01 -0400138 .state = *state,
Florian Westphala5fedd432013-04-19 04:58:25 +0000139 .size = sizeof(*entry) + afinfo->route_key_size,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800140 };
Harald Weltef6ebe772005-08-09 20:21:49 -0700141
Florian Westphal4bd60442013-04-19 04:58:23 +0000142 if (!nf_queue_entry_get_refs(entry)) {
Florian Westphal06cdb632011-01-18 15:28:38 +0100143 status = -ECANCELED;
144 goto err_unlock;
Harald Weltef6ebe772005-08-09 20:21:49 -0700145 }
Eric Dumazet7fee2262010-05-11 23:19:48 +0000146 skb_dst_force(skb);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800147 afinfo->saveroute(skb, entry);
148 status = qh->outfn(entry, queuenum);
Harald Weltef6ebe772005-08-09 20:21:49 -0700149
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700150 rcu_read_unlock();
Harald Weltef6ebe772005-08-09 20:21:49 -0700151
152 if (status < 0) {
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800153 nf_queue_entry_release_refs(entry);
154 goto err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700155 }
156
Florian Westphalf1585082011-01-18 15:27:28 +0100157 return 0;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800158
159err_unlock:
160 rcu_read_unlock();
161err:
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800162 kfree(entry);
Florian Westphalf1585082011-01-18 15:27:28 +0100163 return status;
Harald Weltef6ebe772005-08-09 20:21:49 -0700164}
165
Patrick McHardy02f014d2007-12-05 01:26:33 -0800166void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
Harald Weltef6ebe772005-08-09 20:21:49 -0700167{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800168 struct sk_buff *skb = entry->skb;
Michael Wang2a6decf2012-08-22 19:59:57 +0000169 struct nf_hook_ops *elem = entry->elem;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800170 const struct nf_afinfo *afinfo;
Florian Westphalf1585082011-01-18 15:27:28 +0100171 int err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700172
173 rcu_read_lock();
174
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800175 nf_queue_entry_release_refs(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700176
Harald Weltef6ebe772005-08-09 20:21:49 -0700177 /* Continue traversal iff userspace said ok... */
178 if (verdict == NF_REPEAT) {
Michael Wang2a6decf2012-08-22 19:59:57 +0000179 elem = list_entry(elem->list.prev, struct nf_hook_ops, list);
Harald Weltef6ebe772005-08-09 20:21:49 -0700180 verdict = NF_ACCEPT;
181 }
182
183 if (verdict == NF_ACCEPT) {
David S. Miller1d1de892015-04-03 16:31:01 -0400184 afinfo = nf_get_afinfo(entry->state.pf);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800185 if (!afinfo || afinfo->reroute(skb, entry) < 0)
Patrick McHardy7a11b982006-02-27 13:03:24 -0800186 verdict = NF_DROP;
187 }
188
David S. Miller1d1de892015-04-03 16:31:01 -0400189 entry->state.thresh = INT_MIN;
David S. Millercfdfab32015-04-03 16:23:58 -0400190
Patrick McHardy7a11b982006-02-27 13:03:24 -0800191 if (verdict == NF_ACCEPT) {
Harald Weltef6ebe772005-08-09 20:21:49 -0700192 next_hook:
David S. Miller1d1de892015-04-03 16:31:01 -0400193 verdict = nf_iterate(&nf_hooks[entry->state.pf][entry->state.hook],
194 skb, &entry->state, &elem);
Harald Weltef6ebe772005-08-09 20:21:49 -0700195 }
196
197 switch (verdict & NF_VERDICT_MASK) {
198 case NF_ACCEPT:
Patrick McHardy3bc38712006-07-24 22:52:47 -0700199 case NF_STOP:
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800200 local_bh_disable();
David S. Miller1d1de892015-04-03 16:31:01 -0400201 entry->state.okfn(skb);
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800202 local_bh_enable();
Harald Weltef6ebe772005-08-09 20:21:49 -0700203 break;
Harald Weltef6ebe772005-08-09 20:21:49 -0700204 case NF_QUEUE:
David S. Miller1d1de892015-04-03 16:31:01 -0400205 err = nf_queue(skb, elem, &entry->state,
David S. Millercfdfab32015-04-03 16:23:58 -0400206 verdict >> NF_VERDICT_QBITS);
Florian Westphal06cdb632011-01-18 15:28:38 +0100207 if (err < 0) {
208 if (err == -ECANCELED)
209 goto next_hook;
Florian Westphal94b27cc2011-01-18 16:08:30 +0100210 if (err == -ESRCH &&
211 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
212 goto next_hook;
Florian Westphal06cdb632011-01-18 15:28:38 +0100213 kfree_skb(skb);
214 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700215 break;
Eric Dumazet64507fd2010-02-19 15:28:38 +0100216 case NF_STOLEN:
Julian Anastasovfad54442011-08-05 00:36:28 +0000217 break;
Patrick McHardy3bc38712006-07-24 22:52:47 -0700218 default:
219 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700220 }
221 rcu_read_unlock();
Patrick McHardy02f014d2007-12-05 01:26:33 -0800222 kfree(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700223}
224EXPORT_SYMBOL(nf_reinject);