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.
29 static const struct nf_queue_handler __rcu
*queue_handler __read_mostly
;
31 /* return EBUSY when somebody else is registered, return EEXIST if the
32 * same handler is registered, return 0 in case of success. */
33 void nf_register_queue_handler(const struct nf_queue_handler
*qh
)
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
);
39 EXPORT_SYMBOL(nf_register_queue_handler
);
41 /* The caller must flush their queue before this */
42 void nf_unregister_queue_handler(void)
44 RCU_INIT_POINTER(queue_handler
, NULL
);
47 EXPORT_SYMBOL(nf_unregister_queue_handler
);
49 void nf_queue_entry_release_refs(struct nf_queue_entry
*entry
)
51 struct nf_hook_state
*state
= &entry
->state
;
53 /* Release those devices we held, or Alexey will kill me. */
60 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
61 if (entry
->skb
->nf_bridge
) {
62 struct net_device
*physdev
;
64 physdev
= nf_bridge_get_physindev(entry
->skb
);
67 physdev
= nf_bridge_get_physoutdev(entry
->skb
);
73 EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs
);
75 /* Bump dev refs so they don't vanish while packet is out */
76 void nf_queue_entry_get_refs(struct nf_queue_entry
*entry
)
78 struct nf_hook_state
*state
= &entry
->state
;
86 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
87 if (entry
->skb
->nf_bridge
) {
88 struct net_device
*physdev
;
90 physdev
= nf_bridge_get_physindev(entry
->skb
);
93 physdev
= nf_bridge_get_physoutdev(entry
->skb
);
99 EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs
);
101 void nf_queue_nf_hook_drop(struct net
*net
, struct nf_hook_ops
*ops
)
103 const struct nf_queue_handler
*qh
;
106 qh
= rcu_dereference(queue_handler
);
108 qh
->nf_hook_drop(net
, ops
);
113 * Any packet that leaves via this function must come back
114 * through nf_reinject().
116 int nf_queue(struct sk_buff
*skb
,
117 struct nf_hook_ops
*elem
,
118 struct nf_hook_state
*state
,
119 unsigned int queuenum
)
121 int status
= -ENOENT
;
122 struct nf_queue_entry
*entry
= NULL
;
123 const struct nf_afinfo
*afinfo
;
124 const struct nf_queue_handler
*qh
;
126 /* QUEUE == DROP if no one is waiting, to be safe. */
127 qh
= rcu_dereference(queue_handler
);
133 afinfo
= nf_get_afinfo(state
->pf
);
137 entry
= kmalloc(sizeof(*entry
) + afinfo
->route_key_size
, GFP_ATOMIC
);
143 *entry
= (struct nf_queue_entry
) {
147 .size
= sizeof(*entry
) + afinfo
->route_key_size
,
150 nf_queue_entry_get_refs(entry
);
152 afinfo
->saveroute(skb
, entry
);
153 status
= qh
->outfn(entry
, queuenum
);
156 nf_queue_entry_release_refs(entry
);
167 void nf_reinject(struct nf_queue_entry
*entry
, unsigned int verdict
)
169 struct sk_buff
*skb
= entry
->skb
;
170 struct nf_hook_ops
*elem
= entry
->elem
;
171 const struct nf_afinfo
*afinfo
;
174 nf_queue_entry_release_refs(entry
);
176 /* Continue traversal iff userspace said ok... */
177 if (verdict
== NF_REPEAT
)
178 verdict
= elem
->hook(elem
->priv
, skb
, &entry
->state
);
180 if (verdict
== NF_ACCEPT
) {
181 afinfo
= nf_get_afinfo(entry
->state
.pf
);
182 if (!afinfo
|| afinfo
->reroute(entry
->state
.net
, skb
, entry
) < 0)
186 entry
->state
.thresh
= INT_MIN
;
188 if (verdict
== NF_ACCEPT
) {
190 verdict
= nf_iterate(entry
->state
.hook_list
,
191 skb
, &entry
->state
, &elem
);
194 switch (verdict
& NF_VERDICT_MASK
) {
198 entry
->state
.okfn(entry
->state
.net
, entry
->state
.sk
, skb
);
202 err
= nf_queue(skb
, elem
, &entry
->state
,
203 verdict
>> NF_VERDICT_QBITS
);
206 (verdict
& NF_VERDICT_FLAG_QUEUE_BYPASS
))
219 EXPORT_SYMBOL(nf_reinject
);