Commit | Line | Data |
---|---|---|
f229f6ce PM |
1 | /* |
2 | * Rusty Russell (C)2000 -- This code is GPL. | |
3 | * Patrick McHardy (c) 2006-2012 | |
4 | */ | |
5 | ||
f6ebe77f | 6 | #include <linux/kernel.h> |
5a0e3ad6 | 7 | #include <linux/slab.h> |
f6ebe77f HW |
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> | |
7db9a51e PNA |
13 | #include <linux/netfilter_ipv4.h> |
14 | #include <linux/netfilter_ipv6.h> | |
c737b7c4 | 15 | #include <linux/netfilter_bridge.h> |
bbd86b9f | 16 | #include <linux/seq_file.h> |
7a11b984 | 17 | #include <linux/rcupdate.h> |
f6ebe77f | 18 | #include <net/protocol.h> |
c01cd429 | 19 | #include <net/netfilter/nf_queue.h> |
7fee226a | 20 | #include <net/dst.h> |
f6ebe77f HW |
21 | |
22 | #include "nf_internals.h" | |
23 | ||
87029970 FW |
24 | static const struct nf_queue_handler __rcu *nf_queue_handler; |
25 | ||
601e68e1 | 26 | /* |
0360ae41 FW |
27 | * Hook for nfnetlink_queue to register its queue handler. |
28 | * We do this so that most of the NFQUEUE code can be modular. | |
29 | * | |
30 | * Once the queue is registered it must reinject all packets it | |
31 | * receives, no matter what. | |
f6ebe77f | 32 | */ |
f6ebe77f | 33 | |
87029970 | 34 | void nf_register_queue_handler(const struct nf_queue_handler *qh) |
601e68e1 | 35 | { |
0360ae41 | 36 | /* should never happen, we only have one queueing backend in kernel */ |
87029970 FW |
37 | WARN_ON(rcu_access_pointer(nf_queue_handler)); |
38 | rcu_assign_pointer(nf_queue_handler, qh); | |
f6ebe77f HW |
39 | } |
40 | EXPORT_SYMBOL(nf_register_queue_handler); | |
41 | ||
42 | /* The caller must flush their queue before this */ | |
87029970 | 43 | void nf_unregister_queue_handler(void) |
f6ebe77f | 44 | { |
87029970 | 45 | RCU_INIT_POINTER(nf_queue_handler, NULL); |
f6ebe77f HW |
46 | } |
47 | EXPORT_SYMBOL(nf_unregister_queue_handler); | |
48 | ||
747670fd FW |
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 | ||
dd3cc111 | 58 | static void nf_queue_entry_release_refs(struct nf_queue_entry *entry) |
daaa8be2 | 59 | { |
1d1de89b DM |
60 | struct nf_hook_state *state = &entry->state; |
61 | ||
daaa8be2 | 62 | /* Release those devices we held, or Alexey will kill me. */ |
1160dfa1 YD |
63 | dev_put(state->in); |
64 | dev_put(state->out); | |
1c984f8a | 65 | if (state->sk) |
747670fd | 66 | nf_queue_sock_put(state->sk); |
c4b0e771 | 67 | |
119e52e6 | 68 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
1160dfa1 YD |
69 | dev_put(entry->physin); |
70 | dev_put(entry->physout); | |
119e52e6 | 71 | #endif |
c4b0e771 | 72 | } |
dd3cc111 FW |
73 | |
74 | void nf_queue_entry_free(struct nf_queue_entry *entry) | |
75 | { | |
76 | nf_queue_entry_release_refs(entry); | |
77 | kfree(entry); | |
78 | } | |
79 | EXPORT_SYMBOL_GPL(nf_queue_entry_free); | |
c4b0e771 | 80 | |
119e52e6 | 81 | static void __nf_queue_entry_init_physdevs(struct nf_queue_entry *entry) |
c4b0e771 | 82 | { |
1109a90c | 83 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
119e52e6 | 84 | const struct sk_buff *skb = entry->skb; |
c4b0e771 | 85 | |
aeaa4407 | 86 | if (nf_bridge_info_exists(skb)) { |
a54e7219 | 87 | entry->physin = nf_bridge_get_physindev(skb, entry->state.net); |
119e52e6 FW |
88 | entry->physout = nf_bridge_get_physoutdev(skb); |
89 | } else { | |
90 | entry->physin = NULL; | |
91 | entry->physout = NULL; | |
daaa8be2 PM |
92 | } |
93 | #endif | |
daaa8be2 PM |
94 | } |
95 | ||
4bd60443 | 96 | /* Bump dev refs so they don't vanish while packet is out */ |
c3873070 | 97 | bool nf_queue_entry_get_refs(struct nf_queue_entry *entry) |
4bd60443 | 98 | { |
1d1de89b DM |
99 | struct nf_hook_state *state = &entry->state; |
100 | ||
c3873070 FW |
101 | if (state->sk && !refcount_inc_not_zero(&state->sk->sk_refcnt)) |
102 | return false; | |
103 | ||
1160dfa1 YD |
104 | dev_hold(state->in); |
105 | dev_hold(state->out); | |
4bd60443 | 106 | |
119e52e6 | 107 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
1160dfa1 YD |
108 | dev_hold(entry->physin); |
109 | dev_hold(entry->physout); | |
119e52e6 | 110 | #endif |
c3873070 | 111 | return true; |
4bd60443 | 112 | } |
a5fedd43 | 113 | EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs); |
4bd60443 | 114 | |
26888dfd | 115 | void nf_queue_nf_hook_drop(struct net *net) |
8405a8ff EB |
116 | { |
117 | const struct nf_queue_handler *qh; | |
8405a8ff | 118 | |
8405a8ff | 119 | rcu_read_lock(); |
87029970 | 120 | qh = rcu_dereference(nf_queue_handler); |
2385eb0c | 121 | if (qh) |
26888dfd | 122 | qh->nf_hook_drop(net); |
8405a8ff | 123 | rcu_read_unlock(); |
8405a8ff | 124 | } |
e2a75007 | 125 | EXPORT_SYMBOL_GPL(nf_queue_nf_hook_drop); |
8405a8ff | 126 | |
7db9a51e PNA |
127 | static void nf_ip_saveroute(const struct sk_buff *skb, |
128 | struct nf_queue_entry *entry) | |
129 | { | |
130 | struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry); | |
131 | ||
132 | if (entry->state.hook == NF_INET_LOCAL_OUT) { | |
133 | const struct iphdr *iph = ip_hdr(skb); | |
134 | ||
135 | rt_info->tos = iph->tos; | |
136 | rt_info->daddr = iph->daddr; | |
137 | rt_info->saddr = iph->saddr; | |
138 | rt_info->mark = skb->mark; | |
139 | } | |
140 | } | |
141 | ||
142 | static void nf_ip6_saveroute(const struct sk_buff *skb, | |
143 | struct nf_queue_entry *entry) | |
144 | { | |
145 | struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry); | |
146 | ||
147 | if (entry->state.hook == NF_INET_LOCAL_OUT) { | |
148 | const struct ipv6hdr *iph = ipv6_hdr(skb); | |
149 | ||
150 | rt_info->daddr = iph->daddr; | |
151 | rt_info->saddr = iph->saddr; | |
152 | rt_info->mark = skb->mark; | |
153 | } | |
154 | } | |
155 | ||
7034b566 | 156 | static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state, |
960632ec | 157 | unsigned int index, unsigned int queuenum) |
f6ebe77f | 158 | { |
daaa8be2 | 159 | struct nf_queue_entry *entry = NULL; |
e3ac5298 | 160 | const struct nf_queue_handler *qh; |
46435623 | 161 | unsigned int route_key_size; |
28f715b9 | 162 | int status; |
f6ebe77f | 163 | |
25985edc | 164 | /* QUEUE == DROP if no one is waiting, to be safe. */ |
87029970 | 165 | qh = rcu_dereference(nf_queue_handler); |
28f715b9 FW |
166 | if (!qh) |
167 | return -ESRCH; | |
f6ebe77f | 168 | |
46435623 PNA |
169 | switch (state->pf) { |
170 | case AF_INET: | |
171 | route_key_size = sizeof(struct ip_rt_info); | |
172 | break; | |
173 | case AF_INET6: | |
174 | route_key_size = sizeof(struct ip6_rt_info); | |
175 | break; | |
176 | default: | |
177 | route_key_size = 0; | |
178 | break; | |
179 | } | |
bce8032e | 180 | |
3b836da4 FW |
181 | if (skb_sk_is_prefetched(skb)) { |
182 | struct sock *sk = skb->sk; | |
183 | ||
184 | if (!sk_is_refcounted(sk)) { | |
185 | if (!refcount_inc_not_zero(&sk->sk_refcnt)) | |
186 | return -ENOTCONN; | |
187 | ||
188 | /* drop refcount on skb_orphan */ | |
189 | skb->destructor = sock_edemux; | |
190 | } | |
191 | } | |
192 | ||
46435623 | 193 | entry = kmalloc(sizeof(*entry) + route_key_size, GFP_ATOMIC); |
28f715b9 FW |
194 | if (!entry) |
195 | return -ENOMEM; | |
f6ebe77f | 196 | |
0b9173f4 | 197 | if (skb_dst(skb) && !skb_dst_force(skb)) { |
28f715b9 FW |
198 | kfree(entry); |
199 | return -ENETDOWN; | |
b60a7738 FW |
200 | } |
201 | ||
02f014d8 PM |
202 | *entry = (struct nf_queue_entry) { |
203 | .skb = skb, | |
1d1de89b | 204 | .state = *state, |
960632ec | 205 | .hook_index = index, |
46435623 | 206 | .size = sizeof(*entry) + route_key_size, |
02f014d8 | 207 | }; |
f6ebe77f | 208 | |
119e52e6 FW |
209 | __nf_queue_entry_init_physdevs(entry); |
210 | ||
c3873070 FW |
211 | if (!nf_queue_entry_get_refs(entry)) { |
212 | kfree(entry); | |
213 | return -ENOTCONN; | |
214 | } | |
7db9a51e PNA |
215 | |
216 | switch (entry->state.pf) { | |
217 | case AF_INET: | |
218 | nf_ip_saveroute(skb, entry); | |
219 | break; | |
220 | case AF_INET6: | |
221 | nf_ip6_saveroute(skb, entry); | |
222 | break; | |
223 | } | |
224 | ||
02f014d8 | 225 | status = qh->outfn(entry, queuenum); |
f6ebe77f | 226 | if (status < 0) { |
28f715b9 FW |
227 | nf_queue_entry_free(entry); |
228 | return status; | |
f6ebe77f HW |
229 | } |
230 | ||
f1585086 | 231 | return 0; |
f6ebe77f HW |
232 | } |
233 | ||
7034b566 PNA |
234 | /* Packets leaving via this function must come back through nf_reinject(). */ |
235 | int nf_queue(struct sk_buff *skb, struct nf_hook_state *state, | |
0d9cb300 | 236 | unsigned int index, unsigned int verdict) |
7034b566 | 237 | { |
7034b566 PNA |
238 | int ret; |
239 | ||
0d9cb300 | 240 | ret = __nf_queue(skb, state, index, verdict >> NF_VERDICT_QBITS); |
7034b566 PNA |
241 | if (ret < 0) { |
242 | if (ret == -ESRCH && | |
960632ec | 243 | (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS)) |
7034b566 | 244 | return 1; |
7034b566 PNA |
245 | kfree_skb(skb); |
246 | } | |
247 | ||
248 | return 0; | |
249 | } | |
971502d7 | 250 | EXPORT_SYMBOL_GPL(nf_queue); |