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
)
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
]);
134 afinfo
= nf_get_afinfo(pf
);
138 entry
= kmalloc(sizeof(*entry
) + afinfo
->route_key_size
, GFP_ATOMIC
);
142 *entry
= (struct nf_queue_entry
) {
144 .elem
= list_entry(elem
, struct nf_hook_ops
, list
),
152 /* If it's going away, ignore hook. */
153 if (!try_module_get(entry
->elem
->owner
)) {
159 /* Bump dev refs so they don't vanish while packet is out */
164 #ifdef CONFIG_BRIDGE_NETFILTER
165 if (skb
->nf_bridge
) {
166 physindev
= skb
->nf_bridge
->physindev
;
169 physoutdev
= skb
->nf_bridge
->physoutdev
;
171 dev_hold(physoutdev
);
175 afinfo
->saveroute(skb
, entry
);
176 status
= qh
->outfn(entry
, queuenum
);
181 nf_queue_entry_release_refs(entry
);
195 int nf_queue(struct sk_buff
*skb
,
196 struct list_head
*elem
,
197 u_int8_t pf
, unsigned int hook
,
198 struct net_device
*indev
,
199 struct net_device
*outdev
,
200 int (*okfn
)(struct sk_buff
*),
201 unsigned int queuenum
)
203 struct sk_buff
*segs
;
205 if (!skb_is_gso(skb
))
206 return __nf_queue(skb
, elem
, pf
, hook
, indev
, outdev
, okfn
,
211 skb
->protocol
= htons(ETH_P_IP
);
214 skb
->protocol
= htons(ETH_P_IPV6
);
218 segs
= skb_gso_segment(skb
, 0);
224 struct sk_buff
*nskb
= segs
->next
;
227 if (!__nf_queue(segs
, elem
, pf
, hook
, indev
, outdev
, okfn
,
235 void nf_reinject(struct nf_queue_entry
*entry
, unsigned int verdict
)
237 struct sk_buff
*skb
= entry
->skb
;
238 struct list_head
*elem
= &entry
->elem
->list
;
239 const struct nf_afinfo
*afinfo
;
243 nf_queue_entry_release_refs(entry
);
245 /* Continue traversal iff userspace said ok... */
246 if (verdict
== NF_REPEAT
) {
251 if (verdict
== NF_ACCEPT
) {
252 afinfo
= nf_get_afinfo(entry
->pf
);
253 if (!afinfo
|| afinfo
->reroute(skb
, entry
) < 0)
257 if (verdict
== NF_ACCEPT
) {
259 verdict
= nf_iterate(&nf_hooks
[entry
->pf
][entry
->hook
],
261 entry
->indev
, entry
->outdev
, &elem
,
262 entry
->okfn
, INT_MIN
);
265 switch (verdict
& NF_VERDICT_MASK
) {
273 if (!__nf_queue(skb
, elem
, entry
->pf
, entry
->hook
,
274 entry
->indev
, entry
->outdev
, entry
->okfn
,
275 verdict
>> NF_VERDICT_BITS
))
285 EXPORT_SYMBOL(nf_reinject
);
287 #ifdef CONFIG_PROC_FS
288 static void *seq_start(struct seq_file
*seq
, loff_t
*pos
)
290 if (*pos
>= ARRAY_SIZE(queue_handler
))
296 static void *seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
300 if (*pos
>= ARRAY_SIZE(queue_handler
))
306 static void seq_stop(struct seq_file
*s
, void *v
)
311 static int seq_show(struct seq_file
*s
, void *v
)
315 const struct nf_queue_handler
*qh
;
318 qh
= rcu_dereference(queue_handler
[*pos
]);
320 ret
= seq_printf(s
, "%2lld NONE\n", *pos
);
322 ret
= seq_printf(s
, "%2lld %s\n", *pos
, qh
->name
);
328 static const struct seq_operations nfqueue_seq_ops
= {
335 static int nfqueue_open(struct inode
*inode
, struct file
*file
)
337 return seq_open(file
, &nfqueue_seq_ops
);
340 static const struct file_operations nfqueue_file_ops
= {
341 .owner
= THIS_MODULE
,
342 .open
= nfqueue_open
,
345 .release
= seq_release
,
350 int __init
netfilter_queue_init(void)
352 #ifdef CONFIG_PROC_FS
353 if (!proc_create("nf_queue", S_IRUGO
,
354 proc_net_netfilter
, &nfqueue_file_ops
))