2 * Rusty Russell (C)2000 -- This code is GPL.
3 * Patrick McHardy (c) 2006-2012
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>
20 #include "nf_internals.h"
23 * Hook for nfnetlink_queue to register its queue handler.
24 * We do this so that most of the NFQUEUE code can be modular.
26 * Once the queue is registered it must reinject all packets it
27 * receives, no matter what.
30 /* return EBUSY when somebody else is registered, return EEXIST if the
31 * same handler is registered, return 0 in case of success. */
32 void nf_register_queue_handler(struct net
*net
, const struct nf_queue_handler
*qh
)
34 /* should never happen, we only have one queueing backend in kernel */
35 WARN_ON(rcu_access_pointer(net
->nf
.queue_handler
));
36 rcu_assign_pointer(net
->nf
.queue_handler
, qh
);
38 EXPORT_SYMBOL(nf_register_queue_handler
);
40 /* The caller must flush their queue before this */
41 void nf_unregister_queue_handler(struct net
*net
)
43 RCU_INIT_POINTER(net
->nf
.queue_handler
, NULL
);
45 EXPORT_SYMBOL(nf_unregister_queue_handler
);
47 void nf_queue_entry_release_refs(struct nf_queue_entry
*entry
)
49 struct nf_hook_state
*state
= &entry
->state
;
51 /* Release those devices we held, or Alexey will kill me. */
58 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
59 if (entry
->skb
->nf_bridge
) {
60 struct net_device
*physdev
;
62 physdev
= nf_bridge_get_physindev(entry
->skb
);
65 physdev
= nf_bridge_get_physoutdev(entry
->skb
);
71 EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs
);
73 /* Bump dev refs so they don't vanish while packet is out */
74 void nf_queue_entry_get_refs(struct nf_queue_entry
*entry
)
76 struct nf_hook_state
*state
= &entry
->state
;
84 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
85 if (entry
->skb
->nf_bridge
) {
86 struct net_device
*physdev
;
88 physdev
= nf_bridge_get_physindev(entry
->skb
);
91 physdev
= nf_bridge_get_physoutdev(entry
->skb
);
97 EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs
);
99 void nf_queue_nf_hook_drop(struct net
*net
, struct nf_hook_ops
*ops
)
101 const struct nf_queue_handler
*qh
;
104 qh
= rcu_dereference(net
->nf
.queue_handler
);
106 qh
->nf_hook_drop(net
, ops
);
111 * Any packet that leaves via this function must come back
112 * through nf_reinject().
114 int nf_queue(struct sk_buff
*skb
,
115 struct nf_hook_ops
*elem
,
116 struct nf_hook_state
*state
,
117 unsigned int queuenum
)
119 int status
= -ENOENT
;
120 struct nf_queue_entry
*entry
= NULL
;
121 const struct nf_afinfo
*afinfo
;
122 const struct nf_queue_handler
*qh
;
123 struct net
*net
= state
->net
;
125 /* QUEUE == DROP if no one is waiting, to be safe. */
126 qh
= rcu_dereference(net
->nf
.queue_handler
);
132 afinfo
= nf_get_afinfo(state
->pf
);
136 entry
= kmalloc(sizeof(*entry
) + afinfo
->route_key_size
, GFP_ATOMIC
);
142 *entry
= (struct nf_queue_entry
) {
146 .size
= sizeof(*entry
) + afinfo
->route_key_size
,
149 nf_queue_entry_get_refs(entry
);
151 afinfo
->saveroute(skb
, entry
);
152 status
= qh
->outfn(entry
, queuenum
);
155 nf_queue_entry_release_refs(entry
);
166 void nf_reinject(struct nf_queue_entry
*entry
, unsigned int verdict
)
168 struct sk_buff
*skb
= entry
->skb
;
169 struct nf_hook_ops
*elem
= entry
->elem
;
170 const struct nf_afinfo
*afinfo
;
173 nf_queue_entry_release_refs(entry
);
175 /* Continue traversal iff userspace said ok... */
176 if (verdict
== NF_REPEAT
)
177 verdict
= elem
->hook(elem
->priv
, skb
, &entry
->state
);
179 if (verdict
== NF_ACCEPT
) {
180 afinfo
= nf_get_afinfo(entry
->state
.pf
);
181 if (!afinfo
|| afinfo
->reroute(entry
->state
.net
, skb
, entry
) < 0)
185 entry
->state
.thresh
= INT_MIN
;
187 if (verdict
== NF_ACCEPT
) {
189 verdict
= nf_iterate(entry
->state
.hook_list
,
190 skb
, &entry
->state
, &elem
);
193 switch (verdict
& NF_VERDICT_MASK
) {
197 entry
->state
.okfn(entry
->state
.net
, entry
->state
.sk
, skb
);
201 err
= nf_queue(skb
, elem
, &entry
->state
,
202 verdict
>> NF_VERDICT_QBITS
);
205 (verdict
& NF_VERDICT_FLAG_QUEUE_BYPASS
))
218 EXPORT_SYMBOL(nf_reinject
);