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_ipv4.h>
14 #include <linux/netfilter_ipv6.h>
15 #include <linux/netfilter_bridge.h>
16 #include <linux/seq_file.h>
17 #include <linux/rcupdate.h>
18 #include <net/protocol.h>
19 #include <net/netfilter/nf_queue.h>
22 #include "nf_internals.h"
25 * Hook for nfnetlink_queue to register its queue handler.
26 * We do this so that most of the NFQUEUE code can be modular.
28 * Once the queue is registered it must reinject all packets it
29 * receives, no matter what.
32 /* return EBUSY when somebody else is registered, return EEXIST if the
33 * same handler is registered, return 0 in case of success. */
34 void nf_register_queue_handler(struct net
*net
, const struct nf_queue_handler
*qh
)
36 /* should never happen, we only have one queueing backend in kernel */
37 WARN_ON(rcu_access_pointer(net
->nf
.queue_handler
));
38 rcu_assign_pointer(net
->nf
.queue_handler
, qh
);
40 EXPORT_SYMBOL(nf_register_queue_handler
);
42 /* The caller must flush their queue before this */
43 void nf_unregister_queue_handler(struct net
*net
)
45 RCU_INIT_POINTER(net
->nf
.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
)
103 const struct nf_queue_handler
*qh
;
106 qh
= rcu_dereference(net
->nf
.queue_handler
);
108 qh
->nf_hook_drop(net
);
111 EXPORT_SYMBOL_GPL(nf_queue_nf_hook_drop
);
113 static void nf_ip_saveroute(const struct sk_buff
*skb
,
114 struct nf_queue_entry
*entry
)
116 struct ip_rt_info
*rt_info
= nf_queue_entry_reroute(entry
);
118 if (entry
->state
.hook
== NF_INET_LOCAL_OUT
) {
119 const struct iphdr
*iph
= ip_hdr(skb
);
121 rt_info
->tos
= iph
->tos
;
122 rt_info
->daddr
= iph
->daddr
;
123 rt_info
->saddr
= iph
->saddr
;
124 rt_info
->mark
= skb
->mark
;
128 static void nf_ip6_saveroute(const struct sk_buff
*skb
,
129 struct nf_queue_entry
*entry
)
131 struct ip6_rt_info
*rt_info
= nf_queue_entry_reroute(entry
);
133 if (entry
->state
.hook
== NF_INET_LOCAL_OUT
) {
134 const struct ipv6hdr
*iph
= ipv6_hdr(skb
);
136 rt_info
->daddr
= iph
->daddr
;
137 rt_info
->saddr
= iph
->saddr
;
138 rt_info
->mark
= skb
->mark
;
142 static int __nf_queue(struct sk_buff
*skb
, const struct nf_hook_state
*state
,
143 const struct nf_hook_entries
*entries
,
144 unsigned int index
, unsigned int queuenum
)
146 int status
= -ENOENT
;
147 struct nf_queue_entry
*entry
= NULL
;
148 const struct nf_queue_handler
*qh
;
149 struct net
*net
= state
->net
;
150 unsigned int route_key_size
;
152 /* QUEUE == DROP if no one is waiting, to be safe. */
153 qh
= rcu_dereference(net
->nf
.queue_handler
);
161 route_key_size
= sizeof(struct ip_rt_info
);
164 route_key_size
= sizeof(struct ip6_rt_info
);
171 entry
= kmalloc(sizeof(*entry
) + route_key_size
, GFP_ATOMIC
);
177 if (skb_dst(skb
) && !skb_dst_force(skb
)) {
182 *entry
= (struct nf_queue_entry
) {
186 .size
= sizeof(*entry
) + route_key_size
,
189 nf_queue_entry_get_refs(entry
);
191 switch (entry
->state
.pf
) {
193 nf_ip_saveroute(skb
, entry
);
196 nf_ip6_saveroute(skb
, entry
);
200 status
= qh
->outfn(entry
, queuenum
);
203 nf_queue_entry_release_refs(entry
);
214 /* Packets leaving via this function must come back through nf_reinject(). */
215 int nf_queue(struct sk_buff
*skb
, struct nf_hook_state
*state
,
216 const struct nf_hook_entries
*entries
, unsigned int index
,
217 unsigned int verdict
)
221 ret
= __nf_queue(skb
, state
, entries
, index
, verdict
>> NF_VERDICT_QBITS
);
224 (verdict
& NF_VERDICT_FLAG_QUEUE_BYPASS
))
232 static unsigned int nf_iterate(struct sk_buff
*skb
,
233 struct nf_hook_state
*state
,
234 const struct nf_hook_entries
*hooks
,
237 const struct nf_hook_entry
*hook
;
238 unsigned int verdict
, i
= *index
;
240 while (i
< hooks
->num_hook_entries
) {
241 hook
= &hooks
->hooks
[i
];
243 verdict
= nf_hook_entry_hookfn(hook
, skb
, state
);
244 if (verdict
!= NF_ACCEPT
) {
246 if (verdict
!= NF_REPEAT
)
257 static struct nf_hook_entries
*nf_hook_entries_head(const struct net
*net
, u8 pf
, u8 hooknum
)
260 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
262 return rcu_dereference(net
->nf
.hooks_bridge
[hooknum
]);
265 return rcu_dereference(net
->nf
.hooks_ipv4
[hooknum
]);
267 return rcu_dereference(net
->nf
.hooks_ipv6
[hooknum
]);
276 /* Caller must hold rcu read-side lock */
277 void nf_reinject(struct nf_queue_entry
*entry
, unsigned int verdict
)
279 const struct nf_hook_entry
*hook_entry
;
280 const struct nf_hook_entries
*hooks
;
281 struct sk_buff
*skb
= entry
->skb
;
282 const struct net
*net
;
287 net
= entry
->state
.net
;
288 pf
= entry
->state
.pf
;
290 hooks
= nf_hook_entries_head(net
, pf
, entry
->state
.hook
);
292 nf_queue_entry_release_refs(entry
);
294 i
= entry
->hook_index
;
295 if (WARN_ON_ONCE(!hooks
|| i
>= hooks
->num_hook_entries
)) {
301 hook_entry
= &hooks
->hooks
[i
];
303 /* Continue traversal iff userspace said ok... */
304 if (verdict
== NF_REPEAT
)
305 verdict
= nf_hook_entry_hookfn(hook_entry
, skb
, &entry
->state
);
307 if (verdict
== NF_ACCEPT
) {
308 if (nf_reroute(skb
, entry
) < 0)
312 if (verdict
== NF_ACCEPT
) {
315 verdict
= nf_iterate(skb
, &entry
->state
, hooks
, &i
);
318 switch (verdict
& NF_VERDICT_MASK
) {
322 entry
->state
.okfn(entry
->state
.net
, entry
->state
.sk
, skb
);
326 err
= nf_queue(skb
, &entry
->state
, hooks
, i
, verdict
);
338 EXPORT_SYMBOL(nf_reinject
);