1 #include <linux/kernel.h>
2 #include <linux/slab.h>
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include <linux/proc_fs.h>
6 #include <linux/skbuff.h>
7 #include <linux/netfilter.h>
8 #include <linux/seq_file.h>
9 #include <linux/rcupdate.h>
10 #include <net/protocol.h>
11 #include <net/netfilter/nf_queue.h>
14 #include "nf_internals.h"
17 * A queue handler may be registered for each protocol. Each is protected by
18 * long term mutex. The handler must provide an an outfn() to accept packets
19 * for queueing and must reinject all packets it receives, no matter what.
21 static const struct nf_queue_handler
*queue_handler
[NFPROTO_NUMPROTO
] __read_mostly
;
23 static DEFINE_MUTEX(queue_handler_mutex
);
25 /* return EBUSY when somebody else is registered, return EEXIST if the
26 * same handler is registered, return 0 in case of success. */
27 int nf_register_queue_handler(u_int8_t pf
, const struct nf_queue_handler
*qh
)
31 if (pf
>= ARRAY_SIZE(queue_handler
))
34 mutex_lock(&queue_handler_mutex
);
35 if (queue_handler
[pf
] == qh
)
37 else if (queue_handler
[pf
])
40 rcu_assign_pointer(queue_handler
[pf
], qh
);
43 mutex_unlock(&queue_handler_mutex
);
47 EXPORT_SYMBOL(nf_register_queue_handler
);
49 /* The caller must flush their queue before this */
50 int nf_unregister_queue_handler(u_int8_t pf
, const struct nf_queue_handler
*qh
)
52 if (pf
>= ARRAY_SIZE(queue_handler
))
55 mutex_lock(&queue_handler_mutex
);
56 if (queue_handler
[pf
] && queue_handler
[pf
] != qh
) {
57 mutex_unlock(&queue_handler_mutex
);
61 rcu_assign_pointer(queue_handler
[pf
], NULL
);
62 mutex_unlock(&queue_handler_mutex
);
68 EXPORT_SYMBOL(nf_unregister_queue_handler
);
70 void nf_unregister_queue_handlers(const struct nf_queue_handler
*qh
)
74 mutex_lock(&queue_handler_mutex
);
75 for (pf
= 0; pf
< ARRAY_SIZE(queue_handler
); pf
++) {
76 if (queue_handler
[pf
] == qh
)
77 rcu_assign_pointer(queue_handler
[pf
], NULL
);
79 mutex_unlock(&queue_handler_mutex
);
83 EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers
);
85 static void nf_queue_entry_release_refs(struct nf_queue_entry
*entry
)
87 /* Release those devices we held, or Alexey will kill me. */
89 dev_put(entry
->indev
);
91 dev_put(entry
->outdev
);
92 #ifdef CONFIG_BRIDGE_NETFILTER
93 if (entry
->skb
->nf_bridge
) {
94 struct nf_bridge_info
*nf_bridge
= entry
->skb
->nf_bridge
;
96 if (nf_bridge
->physindev
)
97 dev_put(nf_bridge
->physindev
);
98 if (nf_bridge
->physoutdev
)
99 dev_put(nf_bridge
->physoutdev
);
102 /* Drop reference to owner of hook which queued us. */
103 module_put(entry
->elem
->owner
);
107 * Any packet that leaves via this function must come back
108 * through nf_reinject().
110 static int __nf_queue(struct sk_buff
*skb
,
111 struct list_head
*elem
,
112 u_int8_t pf
, unsigned int hook
,
113 struct net_device
*indev
,
114 struct net_device
*outdev
,
115 int (*okfn
)(struct sk_buff
*),
116 unsigned int queuenum
)
118 int status
= -ENOENT
;
119 struct nf_queue_entry
*entry
= NULL
;
120 #ifdef CONFIG_BRIDGE_NETFILTER
121 struct net_device
*physindev
;
122 struct net_device
*physoutdev
;
124 const struct nf_afinfo
*afinfo
;
125 const struct nf_queue_handler
*qh
;
127 /* QUEUE == DROP if noone is waiting, to be safe. */
130 qh
= rcu_dereference(queue_handler
[pf
]);
136 afinfo
= nf_get_afinfo(pf
);
140 entry
= kmalloc(sizeof(*entry
) + afinfo
->route_key_size
, GFP_ATOMIC
);
146 *entry
= (struct nf_queue_entry
) {
148 .elem
= list_entry(elem
, struct nf_hook_ops
, list
),
156 /* If it's going away, ignore hook. */
157 if (!try_module_get(entry
->elem
->owner
)) {
162 /* Bump dev refs so they don't vanish while packet is out */
167 #ifdef CONFIG_BRIDGE_NETFILTER
168 if (skb
->nf_bridge
) {
169 physindev
= skb
->nf_bridge
->physindev
;
172 physoutdev
= skb
->nf_bridge
->physoutdev
;
174 dev_hold(physoutdev
);
178 afinfo
->saveroute(skb
, entry
);
179 status
= qh
->outfn(entry
, queuenum
);
184 nf_queue_entry_release_refs(entry
);
197 int nf_queue(struct sk_buff
*skb
,
198 struct list_head
*elem
,
199 u_int8_t pf
, unsigned int hook
,
200 struct net_device
*indev
,
201 struct net_device
*outdev
,
202 int (*okfn
)(struct sk_buff
*),
203 unsigned int queuenum
)
205 struct sk_buff
*segs
;
209 if (!skb_is_gso(skb
))
210 return __nf_queue(skb
, elem
, pf
, hook
, indev
, outdev
, okfn
,
215 skb
->protocol
= htons(ETH_P_IP
);
218 skb
->protocol
= htons(ETH_P_IPV6
);
222 /* Set packet's nf flag to indicate non-optimized segmentation */
225 segs
= skb_gso_segment(skb
, 0);
226 /* Does not use PTR_ERR to limit the number of error codes that can be
227 * returned by nf_queue. For instance, callers rely on -ECANCELED to mean
228 * 'ignore this hook'.
236 struct sk_buff
*nskb
= segs
->next
;
240 err
= __nf_queue(segs
, elem
, pf
, hook
, indev
,
241 outdev
, okfn
, queuenum
);
249 /* also free orig skb if only some segments were queued */
250 if (unlikely(err
&& queued
))
257 void nf_reinject(struct nf_queue_entry
*entry
, unsigned int verdict
)
259 struct sk_buff
*skb
= entry
->skb
;
260 struct list_head
*elem
= &entry
->elem
->list
;
261 const struct nf_afinfo
*afinfo
;
266 nf_queue_entry_release_refs(entry
);
268 /* Continue traversal iff userspace said ok... */
269 if (verdict
== NF_REPEAT
) {
274 if (verdict
== NF_ACCEPT
) {
275 afinfo
= nf_get_afinfo(entry
->pf
);
276 if (!afinfo
|| afinfo
->reroute(skb
, entry
) < 0)
280 if (verdict
== NF_ACCEPT
) {
282 verdict
= nf_iterate(&nf_hooks
[entry
->pf
][entry
->hook
],
284 entry
->indev
, entry
->outdev
, &elem
,
285 entry
->okfn
, INT_MIN
);
288 switch (verdict
& NF_VERDICT_MASK
) {
296 err
= __nf_queue(skb
, elem
, entry
->pf
, entry
->hook
,
297 entry
->indev
, entry
->outdev
, entry
->okfn
,
298 verdict
>> NF_VERDICT_QBITS
);
300 if (err
== -ECANCELED
)
303 (verdict
& NF_VERDICT_FLAG_QUEUE_BYPASS
))
315 EXPORT_SYMBOL(nf_reinject
);
317 #ifdef CONFIG_PROC_FS
318 static void *seq_start(struct seq_file
*seq
, loff_t
*pos
)
320 if (*pos
>= ARRAY_SIZE(queue_handler
))
326 static void *seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
330 if (*pos
>= ARRAY_SIZE(queue_handler
))
336 static void seq_stop(struct seq_file
*s
, void *v
)
341 static int seq_show(struct seq_file
*s
, void *v
)
345 const struct nf_queue_handler
*qh
;
348 qh
= rcu_dereference(queue_handler
[*pos
]);
350 ret
= seq_printf(s
, "%2lld NONE\n", *pos
);
352 ret
= seq_printf(s
, "%2lld %s\n", *pos
, qh
->name
);
358 static const struct seq_operations nfqueue_seq_ops
= {
365 static int nfqueue_open(struct inode
*inode
, struct file
*file
)
367 return seq_open(file
, &nfqueue_seq_ops
);
370 static const struct file_operations nfqueue_file_ops
= {
371 .owner
= THIS_MODULE
,
372 .open
= nfqueue_open
,
375 .release
= seq_release
,
380 int __init
netfilter_queue_init(void)
382 #ifdef CONFIG_PROC_FS
383 if (!proc_create("nf_queue", S_IRUGO
,
384 proc_net_netfilter
, &nfqueue_file_ops
))