1/*
2 * Rusty Russell (C)2000 -- This code is GPL.
3 * Patrick McHardy (c) 2006-2012
4 */
5
6#include <linux/kernel.h>
7#include <linux/slab.h>
8#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>
13#include <linux/netfilter_bridge.h>
14#include <linux/seq_file.h>
15#include <linux/rcupdate.h>
16#include <net/protocol.h>
17#include <net/netfilter/nf_queue.h>
18#include <net/dst.h>
19
20#include "nf_internals.h"
21
22/*
23 * Hook for nfnetlink_queue to register its queue handler.
24 * We do this so that most of the NFQUEUE code can be modular.
25 *
26 * Once the queue is registered it must reinject all packets it
27 * receives, no matter what.
28 */
29static const struct nf_queue_handler __rcu *queue_handler __read_mostly;
30
31/* return EBUSY when somebody else is registered, return EEXIST if the
32 * same handler is registered, return 0 in case of success. */
33void nf_register_queue_handler(const struct nf_queue_handler *qh)
34{
35	/* should never happen, we only have one queueing backend in kernel */
36	WARN_ON(rcu_access_pointer(queue_handler));
37	rcu_assign_pointer(queue_handler, qh);
38}
39EXPORT_SYMBOL(nf_register_queue_handler);
40
41/* The caller must flush their queue before this */
42void nf_unregister_queue_handler(void)
43{
44	RCU_INIT_POINTER(queue_handler, NULL);
45	synchronize_rcu();
46}
47EXPORT_SYMBOL(nf_unregister_queue_handler);
48
49void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
50{
51	struct nf_hook_state *state = &entry->state;
52
53	/* Release those devices we held, or Alexey will kill me. */
54	if (state->in)
55		dev_put(state->in);
56	if (state->out)
57		dev_put(state->out);
58	if (state->sk)
59		sock_put(state->sk);
60#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
61	if (entry->skb->nf_bridge) {
62		struct net_device *physdev;
63
64		physdev = nf_bridge_get_physindev(entry->skb);
65		if (physdev)
66			dev_put(physdev);
67		physdev = nf_bridge_get_physoutdev(entry->skb);
68		if (physdev)
69			dev_put(physdev);
70	}
71#endif
72	/* Drop reference to owner of hook which queued us. */
73	module_put(entry->elem->owner);
74}
75EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
76
77/* Bump dev refs so they don't vanish while packet is out */
78bool nf_queue_entry_get_refs(struct nf_queue_entry *entry)
79{
80	struct nf_hook_state *state = &entry->state;
81
82	if (!try_module_get(entry->elem->owner))
83		return false;
84
85	if (state->in)
86		dev_hold(state->in);
87	if (state->out)
88		dev_hold(state->out);
89	if (state->sk)
90		sock_hold(state->sk);
91#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
92	if (entry->skb->nf_bridge) {
93		struct net_device *physdev;
94
95		physdev = nf_bridge_get_physindev(entry->skb);
96		if (physdev)
97			dev_hold(physdev);
98		physdev = nf_bridge_get_physoutdev(entry->skb);
99		if (physdev)
100			dev_hold(physdev);
101	}
102#endif
103
104	return true;
105}
106EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
107
108void nf_queue_nf_hook_drop(struct nf_hook_ops *ops)
109{
110	const struct nf_queue_handler *qh;
111	struct net *net;
112
113	rtnl_lock();
114	rcu_read_lock();
115	qh = rcu_dereference(queue_handler);
116	if (qh) {
117		for_each_net(net) {
118			qh->nf_hook_drop(net, ops);
119		}
120	}
121	rcu_read_unlock();
122	rtnl_unlock();
123}
124
125/*
126 * Any packet that leaves via this function must come back
127 * through nf_reinject().
128 */
129int nf_queue(struct sk_buff *skb,
130	     struct nf_hook_ops *elem,
131	     struct nf_hook_state *state,
132	     unsigned int queuenum)
133{
134	int status = -ENOENT;
135	struct nf_queue_entry *entry = NULL;
136	const struct nf_afinfo *afinfo;
137	const struct nf_queue_handler *qh;
138
139	/* QUEUE == DROP if no one is waiting, to be safe. */
140	rcu_read_lock();
141
142	qh = rcu_dereference(queue_handler);
143	if (!qh) {
144		status = -ESRCH;
145		goto err_unlock;
146	}
147
148	afinfo = nf_get_afinfo(state->pf);
149	if (!afinfo)
150		goto err_unlock;
151
152	entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC);
153	if (!entry) {
154		status = -ENOMEM;
155		goto err_unlock;
156	}
157
158	*entry = (struct nf_queue_entry) {
159		.skb	= skb,
160		.elem	= elem,
161		.state	= *state,
162		.size	= sizeof(*entry) + afinfo->route_key_size,
163	};
164
165	if (!nf_queue_entry_get_refs(entry)) {
166		status = -ECANCELED;
167		goto err_unlock;
168	}
169	skb_dst_force(skb);
170	afinfo->saveroute(skb, entry);
171	status = qh->outfn(entry, queuenum);
172
173	rcu_read_unlock();
174
175	if (status < 0) {
176		nf_queue_entry_release_refs(entry);
177		goto err;
178	}
179
180	return 0;
181
182err_unlock:
183	rcu_read_unlock();
184err:
185	kfree(entry);
186	return status;
187}
188
189void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
190{
191	struct sk_buff *skb = entry->skb;
192	struct nf_hook_ops *elem = entry->elem;
193	const struct nf_afinfo *afinfo;
194	int err;
195
196	rcu_read_lock();
197
198	nf_queue_entry_release_refs(entry);
199
200	/* Continue traversal iff userspace said ok... */
201	if (verdict == NF_REPEAT) {
202		elem = list_entry(elem->list.prev, struct nf_hook_ops, list);
203		verdict = NF_ACCEPT;
204	}
205
206	if (verdict == NF_ACCEPT) {
207		afinfo = nf_get_afinfo(entry->state.pf);
208		if (!afinfo || afinfo->reroute(skb, entry) < 0)
209			verdict = NF_DROP;
210	}
211
212	entry->state.thresh = INT_MIN;
213
214	if (verdict == NF_ACCEPT) {
215	next_hook:
216		verdict = nf_iterate(&nf_hooks[entry->state.pf][entry->state.hook],
217				     skb, &entry->state, &elem);
218	}
219
220	switch (verdict & NF_VERDICT_MASK) {
221	case NF_ACCEPT:
222	case NF_STOP:
223		local_bh_disable();
224		entry->state.okfn(entry->state.sk, skb);
225		local_bh_enable();
226		break;
227	case NF_QUEUE:
228		err = nf_queue(skb, elem, &entry->state,
229			       verdict >> NF_VERDICT_QBITS);
230		if (err < 0) {
231			if (err == -ECANCELED)
232				goto next_hook;
233			if (err == -ESRCH &&
234			   (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
235				goto next_hook;
236			kfree_skb(skb);
237		}
238		break;
239	case NF_STOLEN:
240		break;
241	default:
242		kfree_skb(skb);
243	}
244	rcu_read_unlock();
245	kfree(entry);
246}
247EXPORT_SYMBOL(nf_reinject);
248