GNU Linux-libre 4.19.264-gnu1
[releases.git] / net / netfilter / nf_queue.c
1 /*
2  * Rusty Russell (C)2000 -- This code is GPL.
3  * Patrick McHardy (c) 2006-2012
4  */
5
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>
20 #include <net/dst.h>
21
22 #include "nf_internals.h"
23
24 /*
25  * Hook for nfnetlink_queue to register its queue handler.
26  * We do this so that most of the NFQUEUE code can be modular.
27  *
28  * Once the queue is registered it must reinject all packets it
29  * receives, no matter what.
30  */
31
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)
35 {
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);
39 }
40 EXPORT_SYMBOL(nf_register_queue_handler);
41
42 /* The caller must flush their queue before this */
43 void nf_unregister_queue_handler(struct net *net)
44 {
45         RCU_INIT_POINTER(net->nf.queue_handler, NULL);
46 }
47 EXPORT_SYMBOL(nf_unregister_queue_handler);
48
49 static void nf_queue_sock_put(struct sock *sk)
50 {
51 #ifdef CONFIG_INET
52         sock_gen_put(sk);
53 #else
54         sock_put(sk);
55 #endif
56 }
57
58 void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
59 {
60         struct nf_hook_state *state = &entry->state;
61
62         /* Release those devices we held, or Alexey will kill me. */
63         if (state->in)
64                 dev_put(state->in);
65         if (state->out)
66                 dev_put(state->out);
67         if (state->sk)
68                 nf_queue_sock_put(state->sk);
69 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
70         if (entry->skb->nf_bridge) {
71                 struct net_device *physdev;
72
73                 physdev = nf_bridge_get_physindev(entry->skb);
74                 if (physdev)
75                         dev_put(physdev);
76                 physdev = nf_bridge_get_physoutdev(entry->skb);
77                 if (physdev)
78                         dev_put(physdev);
79         }
80 #endif
81 }
82 EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
83
84 /* Bump dev refs so they don't vanish while packet is out */
85 bool nf_queue_entry_get_refs(struct nf_queue_entry *entry)
86 {
87         struct nf_hook_state *state = &entry->state;
88
89         if (state->sk && !refcount_inc_not_zero(&state->sk->sk_refcnt))
90                 return false;
91
92         if (state->in)
93                 dev_hold(state->in);
94         if (state->out)
95                 dev_hold(state->out);
96         if (state->sk)
97                 sock_hold(state->sk);
98 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
99         if (entry->skb->nf_bridge) {
100                 struct net_device *physdev;
101
102                 physdev = nf_bridge_get_physindev(entry->skb);
103                 if (physdev)
104                         dev_hold(physdev);
105                 physdev = nf_bridge_get_physoutdev(entry->skb);
106                 if (physdev)
107                         dev_hold(physdev);
108         }
109 #endif
110         return true;
111 }
112 EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
113
114 void nf_queue_nf_hook_drop(struct net *net)
115 {
116         const struct nf_queue_handler *qh;
117
118         rcu_read_lock();
119         qh = rcu_dereference(net->nf.queue_handler);
120         if (qh)
121                 qh->nf_hook_drop(net);
122         rcu_read_unlock();
123 }
124 EXPORT_SYMBOL_GPL(nf_queue_nf_hook_drop);
125
126 static void nf_ip_saveroute(const struct sk_buff *skb,
127                             struct nf_queue_entry *entry)
128 {
129         struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
130
131         if (entry->state.hook == NF_INET_LOCAL_OUT) {
132                 const struct iphdr *iph = ip_hdr(skb);
133
134                 rt_info->tos = iph->tos;
135                 rt_info->daddr = iph->daddr;
136                 rt_info->saddr = iph->saddr;
137                 rt_info->mark = skb->mark;
138         }
139 }
140
141 static void nf_ip6_saveroute(const struct sk_buff *skb,
142                              struct nf_queue_entry *entry)
143 {
144         struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
145
146         if (entry->state.hook == NF_INET_LOCAL_OUT) {
147                 const struct ipv6hdr *iph = ipv6_hdr(skb);
148
149                 rt_info->daddr = iph->daddr;
150                 rt_info->saddr = iph->saddr;
151                 rt_info->mark = skb->mark;
152         }
153 }
154
155 static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
156                       const struct nf_hook_entries *entries,
157                       unsigned int index, unsigned int queuenum)
158 {
159         int status = -ENOENT;
160         struct nf_queue_entry *entry = NULL;
161         const struct nf_queue_handler *qh;
162         struct net *net = state->net;
163         unsigned int route_key_size;
164
165         /* QUEUE == DROP if no one is waiting, to be safe. */
166         qh = rcu_dereference(net->nf.queue_handler);
167         if (!qh) {
168                 status = -ESRCH;
169                 goto err;
170         }
171
172         switch (state->pf) {
173         case AF_INET:
174                 route_key_size = sizeof(struct ip_rt_info);
175                 break;
176         case AF_INET6:
177                 route_key_size = sizeof(struct ip6_rt_info);
178                 break;
179         default:
180                 route_key_size = 0;
181                 break;
182         }
183
184         entry = kmalloc(sizeof(*entry) + route_key_size, GFP_ATOMIC);
185         if (!entry) {
186                 status = -ENOMEM;
187                 goto err;
188         }
189
190         if (skb_dst(skb) && !skb_dst_force(skb)) {
191                 status = -ENETDOWN;
192                 goto err;
193         }
194
195         *entry = (struct nf_queue_entry) {
196                 .skb    = skb,
197                 .state  = *state,
198                 .hook_index = index,
199                 .size   = sizeof(*entry) + route_key_size,
200         };
201
202         if (!nf_queue_entry_get_refs(entry)) {
203                 kfree(entry);
204                 return -ENOTCONN;
205         }
206
207         switch (entry->state.pf) {
208         case AF_INET:
209                 nf_ip_saveroute(skb, entry);
210                 break;
211         case AF_INET6:
212                 nf_ip6_saveroute(skb, entry);
213                 break;
214         }
215
216         status = qh->outfn(entry, queuenum);
217
218         if (status < 0) {
219                 nf_queue_entry_release_refs(entry);
220                 goto err;
221         }
222
223         return 0;
224
225 err:
226         kfree(entry);
227         return status;
228 }
229
230 /* Packets leaving via this function must come back through nf_reinject(). */
231 int nf_queue(struct sk_buff *skb, struct nf_hook_state *state,
232              const struct nf_hook_entries *entries, unsigned int index,
233              unsigned int verdict)
234 {
235         int ret;
236
237         ret = __nf_queue(skb, state, entries, index, verdict >> NF_VERDICT_QBITS);
238         if (ret < 0) {
239                 if (ret == -ESRCH &&
240                     (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
241                         return 1;
242                 kfree_skb(skb);
243         }
244
245         return 0;
246 }
247
248 static unsigned int nf_iterate(struct sk_buff *skb,
249                                struct nf_hook_state *state,
250                                const struct nf_hook_entries *hooks,
251                                unsigned int *index)
252 {
253         const struct nf_hook_entry *hook;
254         unsigned int verdict, i = *index;
255
256         while (i < hooks->num_hook_entries) {
257                 hook = &hooks->hooks[i];
258 repeat:
259                 verdict = nf_hook_entry_hookfn(hook, skb, state);
260                 if (verdict != NF_ACCEPT) {
261                         *index = i;
262                         if (verdict != NF_REPEAT)
263                                 return verdict;
264                         goto repeat;
265                 }
266                 i++;
267         }
268
269         *index = i;
270         return NF_ACCEPT;
271 }
272
273 static struct nf_hook_entries *nf_hook_entries_head(const struct net *net, u8 pf, u8 hooknum)
274 {
275         switch (pf) {
276 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
277         case NFPROTO_BRIDGE:
278                 return rcu_dereference(net->nf.hooks_bridge[hooknum]);
279 #endif
280         case NFPROTO_IPV4:
281                 return rcu_dereference(net->nf.hooks_ipv4[hooknum]);
282         case NFPROTO_IPV6:
283                 return rcu_dereference(net->nf.hooks_ipv6[hooknum]);
284         default:
285                 WARN_ON_ONCE(1);
286                 return NULL;
287         }
288
289         return NULL;
290 }
291
292 /* Caller must hold rcu read-side lock */
293 void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
294 {
295         const struct nf_hook_entry *hook_entry;
296         const struct nf_hook_entries *hooks;
297         struct sk_buff *skb = entry->skb;
298         const struct net *net;
299         unsigned int i;
300         int err;
301         u8 pf;
302
303         net = entry->state.net;
304         pf = entry->state.pf;
305
306         hooks = nf_hook_entries_head(net, pf, entry->state.hook);
307
308         nf_queue_entry_release_refs(entry);
309
310         i = entry->hook_index;
311         if (WARN_ON_ONCE(!hooks || i >= hooks->num_hook_entries)) {
312                 kfree_skb(skb);
313                 kfree(entry);
314                 return;
315         }
316
317         hook_entry = &hooks->hooks[i];
318
319         /* Continue traversal iff userspace said ok... */
320         if (verdict == NF_REPEAT)
321                 verdict = nf_hook_entry_hookfn(hook_entry, skb, &entry->state);
322
323         if (verdict == NF_ACCEPT) {
324                 if (nf_reroute(skb, entry) < 0)
325                         verdict = NF_DROP;
326         }
327
328         if (verdict == NF_ACCEPT) {
329 next_hook:
330                 ++i;
331                 verdict = nf_iterate(skb, &entry->state, hooks, &i);
332         }
333
334         switch (verdict & NF_VERDICT_MASK) {
335         case NF_ACCEPT:
336         case NF_STOP:
337                 local_bh_disable();
338                 entry->state.okfn(entry->state.net, entry->state.sk, skb);
339                 local_bh_enable();
340                 break;
341         case NF_QUEUE:
342                 err = nf_queue(skb, &entry->state, hooks, i, verdict);
343                 if (err == 1)
344                         goto next_hook;
345                 break;
346         case NF_STOLEN:
347                 break;
348         default:
349                 kfree_skb(skb);
350         }
351
352         kfree(entry);
353 }
354 EXPORT_SYMBOL(nf_reinject);