]>
Commit | Line | Data |
---|---|---|
7af4cc3f HW |
1 | /* |
2 | * This is a module which is used for queueing packets and communicating with | |
67137f3c | 3 | * userspace via nfnetlink. |
7af4cc3f HW |
4 | * |
5 | * (C) 2005 by Harald Welte <laforge@netfilter.org> | |
4ad9d4fa | 6 | * (C) 2007 by Patrick McHardy <kaber@trash.net> |
7af4cc3f HW |
7 | * |
8 | * Based on the old ipv4-only ip_queue.c: | |
9 | * (C) 2000-2002 James Morris <jmorris@intercode.com.au> | |
10 | * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org> | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License version 2 as | |
14 | * published by the Free Software Foundation. | |
15 | * | |
16 | */ | |
17 | #include <linux/module.h> | |
18 | #include <linux/skbuff.h> | |
19 | #include <linux/init.h> | |
20 | #include <linux/spinlock.h> | |
5a0e3ad6 | 21 | #include <linux/slab.h> |
7af4cc3f HW |
22 | #include <linux/notifier.h> |
23 | #include <linux/netdevice.h> | |
24 | #include <linux/netfilter.h> | |
838ab636 | 25 | #include <linux/proc_fs.h> |
7af4cc3f HW |
26 | #include <linux/netfilter_ipv4.h> |
27 | #include <linux/netfilter_ipv6.h> | |
28 | #include <linux/netfilter/nfnetlink.h> | |
29 | #include <linux/netfilter/nfnetlink_queue.h> | |
30 | #include <linux/list.h> | |
31 | #include <net/sock.h> | |
c01cd429 | 32 | #include <net/netfilter/nf_queue.h> |
e8179610 | 33 | #include <net/netns/generic.h> |
7c622345 | 34 | #include <net/netfilter/nfnetlink_queue.h> |
7af4cc3f | 35 | |
60063497 | 36 | #include <linux/atomic.h> |
7af4cc3f | 37 | |
fbcd923c HW |
38 | #ifdef CONFIG_BRIDGE_NETFILTER |
39 | #include "../bridge/br_private.h" | |
40 | #endif | |
41 | ||
7af4cc3f HW |
42 | #define NFQNL_QMAX_DEFAULT 1024 |
43 | ||
9cefbbc9 FW |
44 | /* We're using struct nlattr which has 16bit nla_len. Note that nla_len |
45 | * includes the header length. Thus, the maximum packet length that we | |
46 | * support is 65531 bytes. We send truncated packets if the specified length | |
47 | * is larger than that. Userspace can check for presence of NFQA_CAP_LEN | |
48 | * attribute to detect truncation. | |
49 | */ | |
50 | #define NFQNL_MAX_COPY_RANGE (0xffff - NLA_HDRLEN) | |
51 | ||
7af4cc3f HW |
52 | struct nfqnl_instance { |
53 | struct hlist_node hlist; /* global list of queues */ | |
9872bec7 | 54 | struct rcu_head rcu; |
7af4cc3f | 55 | |
15e47304 | 56 | int peer_portid; |
7af4cc3f HW |
57 | unsigned int queue_maxlen; |
58 | unsigned int copy_range; | |
7af4cc3f HW |
59 | unsigned int queue_dropped; |
60 | unsigned int queue_user_dropped; | |
61 | ||
7af4cc3f HW |
62 | |
63 | u_int16_t queue_num; /* number of this queue */ | |
64 | u_int8_t copy_mode; | |
fdb694a0 | 65 | u_int32_t flags; /* Set using NFQA_CFG_FLAGS */ |
c463ac97 ED |
66 | /* |
67 | * Following fields are dirtied for each queued packet, | |
68 | * keep them in same cache line if possible. | |
69 | */ | |
70 | spinlock_t lock; | |
71 | unsigned int queue_total; | |
5863702a | 72 | unsigned int id_sequence; /* 'sequence' of pkt ids */ |
7af4cc3f HW |
73 | struct list_head queue_list; /* packets in queue */ |
74 | }; | |
75 | ||
02f014d8 | 76 | typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long); |
7af4cc3f | 77 | |
e8179610 | 78 | static int nfnl_queue_net_id __read_mostly; |
7af4cc3f | 79 | |
7af4cc3f | 80 | #define INSTANCE_BUCKETS 16 |
e8179610 G |
81 | struct nfnl_queue_net { |
82 | spinlock_t instances_lock; | |
83 | struct hlist_head instance_table[INSTANCE_BUCKETS]; | |
84 | }; | |
85 | ||
86 | static struct nfnl_queue_net *nfnl_queue_pernet(struct net *net) | |
87 | { | |
88 | return net_generic(net, nfnl_queue_net_id); | |
89 | } | |
7af4cc3f HW |
90 | |
91 | static inline u_int8_t instance_hashfn(u_int16_t queue_num) | |
92 | { | |
1cdb0905 | 93 | return ((queue_num >> 8) ^ queue_num) % INSTANCE_BUCKETS; |
7af4cc3f HW |
94 | } |
95 | ||
96 | static struct nfqnl_instance * | |
e8179610 | 97 | instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num) |
7af4cc3f HW |
98 | { |
99 | struct hlist_head *head; | |
7af4cc3f HW |
100 | struct nfqnl_instance *inst; |
101 | ||
e8179610 | 102 | head = &q->instance_table[instance_hashfn(queue_num)]; |
b67bfe0d | 103 | hlist_for_each_entry_rcu(inst, head, hlist) { |
7af4cc3f HW |
104 | if (inst->queue_num == queue_num) |
105 | return inst; | |
106 | } | |
107 | return NULL; | |
108 | } | |
109 | ||
7af4cc3f | 110 | static struct nfqnl_instance * |
e8179610 G |
111 | instance_create(struct nfnl_queue_net *q, u_int16_t queue_num, |
112 | int portid) | |
7af4cc3f | 113 | { |
baab2ce7 | 114 | struct nfqnl_instance *inst; |
9872bec7 | 115 | unsigned int h; |
baab2ce7 | 116 | int err; |
7af4cc3f | 117 | |
e8179610 G |
118 | spin_lock(&q->instances_lock); |
119 | if (instance_lookup(q, queue_num)) { | |
baab2ce7 | 120 | err = -EEXIST; |
7af4cc3f | 121 | goto out_unlock; |
baab2ce7 | 122 | } |
7af4cc3f | 123 | |
10dfdc69 | 124 | inst = kzalloc(sizeof(*inst), GFP_ATOMIC); |
baab2ce7 PM |
125 | if (!inst) { |
126 | err = -ENOMEM; | |
7af4cc3f | 127 | goto out_unlock; |
baab2ce7 | 128 | } |
7af4cc3f | 129 | |
7af4cc3f | 130 | inst->queue_num = queue_num; |
15e47304 | 131 | inst->peer_portid = portid; |
7af4cc3f | 132 | inst->queue_maxlen = NFQNL_QMAX_DEFAULT; |
9cefbbc9 | 133 | inst->copy_range = NFQNL_MAX_COPY_RANGE; |
7af4cc3f | 134 | inst->copy_mode = NFQNL_COPY_NONE; |
181a46a5 | 135 | spin_lock_init(&inst->lock); |
7af4cc3f HW |
136 | INIT_LIST_HEAD(&inst->queue_list); |
137 | ||
baab2ce7 PM |
138 | if (!try_module_get(THIS_MODULE)) { |
139 | err = -EAGAIN; | |
7af4cc3f | 140 | goto out_free; |
baab2ce7 | 141 | } |
7af4cc3f | 142 | |
9872bec7 | 143 | h = instance_hashfn(queue_num); |
e8179610 | 144 | hlist_add_head_rcu(&inst->hlist, &q->instance_table[h]); |
7af4cc3f | 145 | |
e8179610 | 146 | spin_unlock(&q->instances_lock); |
7af4cc3f | 147 | |
7af4cc3f HW |
148 | return inst; |
149 | ||
150 | out_free: | |
151 | kfree(inst); | |
152 | out_unlock: | |
e8179610 | 153 | spin_unlock(&q->instances_lock); |
baab2ce7 | 154 | return ERR_PTR(err); |
7af4cc3f HW |
155 | } |
156 | ||
b43d8d85 PM |
157 | static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, |
158 | unsigned long data); | |
7af4cc3f HW |
159 | |
160 | static void | |
9872bec7 | 161 | instance_destroy_rcu(struct rcu_head *head) |
7af4cc3f | 162 | { |
9872bec7 PM |
163 | struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance, |
164 | rcu); | |
7af4cc3f | 165 | |
b43d8d85 | 166 | nfqnl_flush(inst, NULL, 0); |
9872bec7 | 167 | kfree(inst); |
7af4cc3f HW |
168 | module_put(THIS_MODULE); |
169 | } | |
170 | ||
9872bec7 | 171 | static void |
7af4cc3f HW |
172 | __instance_destroy(struct nfqnl_instance *inst) |
173 | { | |
9872bec7 PM |
174 | hlist_del_rcu(&inst->hlist); |
175 | call_rcu(&inst->rcu, instance_destroy_rcu); | |
7af4cc3f HW |
176 | } |
177 | ||
9872bec7 | 178 | static void |
e8179610 | 179 | instance_destroy(struct nfnl_queue_net *q, struct nfqnl_instance *inst) |
7af4cc3f | 180 | { |
e8179610 | 181 | spin_lock(&q->instances_lock); |
9872bec7 | 182 | __instance_destroy(inst); |
e8179610 | 183 | spin_unlock(&q->instances_lock); |
7af4cc3f HW |
184 | } |
185 | ||
7af4cc3f | 186 | static inline void |
02f014d8 | 187 | __enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry) |
7af4cc3f | 188 | { |
0ac41e81 | 189 | list_add_tail(&entry->list, &queue->queue_list); |
7af4cc3f HW |
190 | queue->queue_total++; |
191 | } | |
192 | ||
97d32cf9 FW |
193 | static void |
194 | __dequeue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry) | |
195 | { | |
196 | list_del(&entry->list); | |
197 | queue->queue_total--; | |
198 | } | |
199 | ||
02f014d8 | 200 | static struct nf_queue_entry * |
b43d8d85 | 201 | find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id) |
7af4cc3f | 202 | { |
02f014d8 | 203 | struct nf_queue_entry *entry = NULL, *i; |
601e68e1 | 204 | |
7af4cc3f | 205 | spin_lock_bh(&queue->lock); |
b43d8d85 PM |
206 | |
207 | list_for_each_entry(i, &queue->queue_list, list) { | |
208 | if (i->id == id) { | |
209 | entry = i; | |
210 | break; | |
211 | } | |
212 | } | |
213 | ||
97d32cf9 FW |
214 | if (entry) |
215 | __dequeue_entry(queue, entry); | |
b43d8d85 | 216 | |
7af4cc3f HW |
217 | spin_unlock_bh(&queue->lock); |
218 | ||
219 | return entry; | |
220 | } | |
221 | ||
222 | static void | |
b43d8d85 | 223 | nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data) |
7af4cc3f | 224 | { |
02f014d8 | 225 | struct nf_queue_entry *entry, *next; |
b43d8d85 | 226 | |
7af4cc3f | 227 | spin_lock_bh(&queue->lock); |
b43d8d85 PM |
228 | list_for_each_entry_safe(entry, next, &queue->queue_list, list) { |
229 | if (!cmpfn || cmpfn(entry, data)) { | |
230 | list_del(&entry->list); | |
231 | queue->queue_total--; | |
4b3d15ef | 232 | nf_reinject(entry, NF_DROP); |
b43d8d85 PM |
233 | } |
234 | } | |
7af4cc3f HW |
235 | spin_unlock_bh(&queue->lock); |
236 | } | |
237 | ||
ae08ce00 ED |
238 | static void |
239 | nfqnl_zcopy(struct sk_buff *to, const struct sk_buff *from, int len, int hlen) | |
240 | { | |
241 | int i, j = 0; | |
242 | int plen = 0; /* length of skb->head fragment */ | |
243 | struct page *page; | |
244 | unsigned int offset; | |
245 | ||
246 | /* dont bother with small payloads */ | |
247 | if (len <= skb_tailroom(to)) { | |
248 | skb_copy_bits(from, 0, skb_put(to, len), len); | |
249 | return; | |
250 | } | |
251 | ||
252 | if (hlen) { | |
253 | skb_copy_bits(from, 0, skb_put(to, hlen), hlen); | |
254 | len -= hlen; | |
255 | } else { | |
256 | plen = min_t(int, skb_headlen(from), len); | |
257 | if (plen) { | |
258 | page = virt_to_head_page(from->head); | |
259 | offset = from->data - (unsigned char *)page_address(page); | |
260 | __skb_fill_page_desc(to, 0, page, offset, plen); | |
261 | get_page(page); | |
262 | j = 1; | |
263 | len -= plen; | |
264 | } | |
265 | } | |
266 | ||
267 | to->truesize += len + plen; | |
268 | to->len += len + plen; | |
269 | to->data_len += len + plen; | |
270 | ||
271 | for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { | |
272 | if (!len) | |
273 | break; | |
274 | skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; | |
275 | skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len); | |
276 | len -= skb_shinfo(to)->frags[j].size; | |
277 | skb_frag_ref(to, j); | |
278 | j++; | |
279 | } | |
280 | skb_shinfo(to)->nr_frags = j; | |
281 | } | |
282 | ||
496e4ae7 FW |
283 | static int |
284 | nfqnl_put_packet_info(struct sk_buff *nlskb, struct sk_buff *packet, | |
285 | bool csum_verify) | |
7237190d FW |
286 | { |
287 | __u32 flags = 0; | |
288 | ||
289 | if (packet->ip_summed == CHECKSUM_PARTIAL) | |
290 | flags = NFQA_SKB_CSUMNOTREADY; | |
496e4ae7 FW |
291 | else if (csum_verify) |
292 | flags = NFQA_SKB_CSUM_NOTVERIFIED; | |
293 | ||
7237190d FW |
294 | if (skb_is_gso(packet)) |
295 | flags |= NFQA_SKB_GSO; | |
296 | ||
297 | return flags ? nla_put_be32(nlskb, NFQA_SKB_INFO, htonl(flags)) : 0; | |
298 | } | |
299 | ||
7af4cc3f HW |
300 | static struct sk_buff * |
301 | nfqnl_build_packet_message(struct nfqnl_instance *queue, | |
5863702a ED |
302 | struct nf_queue_entry *entry, |
303 | __be32 **packet_id_ptr) | |
7af4cc3f | 304 | { |
7af4cc3f | 305 | size_t size; |
6ee584be | 306 | size_t data_len = 0, cap_len = 0; |
ae08ce00 | 307 | int hlen = 0; |
7af4cc3f | 308 | struct sk_buff *skb; |
5863702a ED |
309 | struct nlattr *nla; |
310 | struct nfqnl_msg_packet_hdr *pmsg; | |
7af4cc3f HW |
311 | struct nlmsghdr *nlh; |
312 | struct nfgenmsg *nfmsg; | |
3e4ead4f JJ |
313 | struct sk_buff *entskb = entry->skb; |
314 | struct net_device *indev; | |
315 | struct net_device *outdev; | |
9cb01766 PNA |
316 | struct nf_conn *ct = NULL; |
317 | enum ip_conntrack_info uninitialized_var(ctinfo); | |
496e4ae7 | 318 | bool csum_verify; |
7af4cc3f | 319 | |
573ce260 | 320 | size = nlmsg_total_size(sizeof(struct nfgenmsg)) |
df6fb868 PM |
321 | + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr)) |
322 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ | |
323 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ | |
fbcd923c | 324 | #ifdef CONFIG_BRIDGE_NETFILTER |
df6fb868 PM |
325 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ |
326 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ | |
fbcd923c | 327 | #endif |
df6fb868 PM |
328 | + nla_total_size(sizeof(u_int32_t)) /* mark */ |
329 | + nla_total_size(sizeof(struct nfqnl_msg_packet_hw)) | |
7237190d | 330 | + nla_total_size(sizeof(u_int32_t)) /* skbinfo */ |
ae08ce00 ED |
331 | + nla_total_size(sizeof(u_int32_t)); /* cap_len */ |
332 | ||
333 | if (entskb->tstamp.tv64) | |
334 | size += nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp)); | |
7af4cc3f | 335 | |
496e4ae7 FW |
336 | if (entry->hook <= NF_INET_FORWARD || |
337 | (entry->hook == NF_INET_POST_ROUTING && entskb->sk == NULL)) | |
338 | csum_verify = !skb_csum_unnecessary(entskb); | |
339 | else | |
340 | csum_verify = false; | |
341 | ||
02f014d8 | 342 | outdev = entry->outdev; |
3e4ead4f | 343 | |
c463ac97 | 344 | switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) { |
7af4cc3f HW |
345 | case NFQNL_COPY_META: |
346 | case NFQNL_COPY_NONE: | |
7af4cc3f | 347 | break; |
601e68e1 | 348 | |
7af4cc3f | 349 | case NFQNL_COPY_PACKET: |
00bd1cc2 FW |
350 | if (!(queue->flags & NFQA_CFG_F_GSO) && |
351 | entskb->ip_summed == CHECKSUM_PARTIAL && | |
c463ac97 | 352 | skb_checksum_help(entskb)) |
e7dfb09a | 353 | return NULL; |
c463ac97 ED |
354 | |
355 | data_len = ACCESS_ONCE(queue->copy_range); | |
9cefbbc9 | 356 | if (data_len > entskb->len) |
3e4ead4f | 357 | data_len = entskb->len; |
601e68e1 | 358 | |
ae08ce00 ED |
359 | if (!entskb->head_frag || |
360 | skb_headlen(entskb) < L1_CACHE_BYTES || | |
361 | skb_shinfo(entskb)->nr_frags >= MAX_SKB_FRAGS) | |
362 | hlen = skb_headlen(entskb); | |
363 | ||
364 | if (skb_has_frag_list(entskb)) | |
365 | hlen = entskb->len; | |
366 | hlen = min_t(int, data_len, hlen); | |
367 | size += sizeof(struct nlattr) + hlen; | |
6ee584be | 368 | cap_len = entskb->len; |
7af4cc3f | 369 | break; |
7af4cc3f HW |
370 | } |
371 | ||
7c622345 PNA |
372 | if (queue->flags & NFQA_CFG_F_CONNTRACK) |
373 | ct = nfqnl_ct_get(entskb, &size, &ctinfo); | |
7af4cc3f | 374 | |
3ab1f683 PM |
375 | skb = nfnetlink_alloc_skb(&init_net, size, queue->peer_portid, |
376 | GFP_ATOMIC); | |
7af4cc3f | 377 | if (!skb) |
3da07c0c | 378 | return NULL; |
601e68e1 | 379 | |
3da07c0c | 380 | nlh = nlmsg_put(skb, 0, 0, |
7af4cc3f | 381 | NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET, |
3da07c0c DM |
382 | sizeof(struct nfgenmsg), 0); |
383 | if (!nlh) { | |
384 | kfree_skb(skb); | |
385 | return NULL; | |
386 | } | |
387 | nfmsg = nlmsg_data(nlh); | |
02f014d8 | 388 | nfmsg->nfgen_family = entry->pf; |
7af4cc3f HW |
389 | nfmsg->version = NFNETLINK_V0; |
390 | nfmsg->res_id = htons(queue->queue_num); | |
391 | ||
5863702a ED |
392 | nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg)); |
393 | pmsg = nla_data(nla); | |
394 | pmsg->hw_protocol = entskb->protocol; | |
395 | pmsg->hook = entry->hook; | |
396 | *packet_id_ptr = &pmsg->packet_id; | |
7af4cc3f | 397 | |
02f014d8 | 398 | indev = entry->indev; |
3e4ead4f | 399 | if (indev) { |
fbcd923c | 400 | #ifndef CONFIG_BRIDGE_NETFILTER |
a447189e DM |
401 | if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex))) |
402 | goto nla_put_failure; | |
fbcd923c | 403 | #else |
02f014d8 | 404 | if (entry->pf == PF_BRIDGE) { |
fbcd923c | 405 | /* Case 1: indev is physical input device, we need to |
601e68e1 | 406 | * look for bridge group (when called from |
fbcd923c | 407 | * netfilter_bridge) */ |
a447189e DM |
408 | if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV, |
409 | htonl(indev->ifindex)) || | |
fbcd923c | 410 | /* this is the bridge group "brX" */ |
f350a0a8 | 411 | /* rcu_read_lock()ed by __nf_queue */ |
a447189e DM |
412 | nla_put_be32(skb, NFQA_IFINDEX_INDEV, |
413 | htonl(br_port_get_rcu(indev)->br->dev->ifindex))) | |
414 | goto nla_put_failure; | |
fbcd923c HW |
415 | } else { |
416 | /* Case 2: indev is bridge group, we need to look for | |
417 | * physical device (when called from ipv4) */ | |
a447189e DM |
418 | if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, |
419 | htonl(indev->ifindex))) | |
420 | goto nla_put_failure; | |
421 | if (entskb->nf_bridge && entskb->nf_bridge->physindev && | |
422 | nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV, | |
423 | htonl(entskb->nf_bridge->physindev->ifindex))) | |
424 | goto nla_put_failure; | |
fbcd923c HW |
425 | } |
426 | #endif | |
7af4cc3f HW |
427 | } |
428 | ||
3e4ead4f | 429 | if (outdev) { |
fbcd923c | 430 | #ifndef CONFIG_BRIDGE_NETFILTER |
a447189e DM |
431 | if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex))) |
432 | goto nla_put_failure; | |
fbcd923c | 433 | #else |
02f014d8 | 434 | if (entry->pf == PF_BRIDGE) { |
fbcd923c | 435 | /* Case 1: outdev is physical output device, we need to |
601e68e1 | 436 | * look for bridge group (when called from |
fbcd923c | 437 | * netfilter_bridge) */ |
a447189e DM |
438 | if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV, |
439 | htonl(outdev->ifindex)) || | |
fbcd923c | 440 | /* this is the bridge group "brX" */ |
f350a0a8 | 441 | /* rcu_read_lock()ed by __nf_queue */ |
a447189e DM |
442 | nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, |
443 | htonl(br_port_get_rcu(outdev)->br->dev->ifindex))) | |
444 | goto nla_put_failure; | |
fbcd923c HW |
445 | } else { |
446 | /* Case 2: outdev is bridge group, we need to look for | |
447 | * physical output device (when called from ipv4) */ | |
a447189e DM |
448 | if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, |
449 | htonl(outdev->ifindex))) | |
450 | goto nla_put_failure; | |
451 | if (entskb->nf_bridge && entskb->nf_bridge->physoutdev && | |
452 | nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV, | |
453 | htonl(entskb->nf_bridge->physoutdev->ifindex))) | |
454 | goto nla_put_failure; | |
fbcd923c HW |
455 | } |
456 | #endif | |
7af4cc3f HW |
457 | } |
458 | ||
a447189e DM |
459 | if (entskb->mark && |
460 | nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark))) | |
461 | goto nla_put_failure; | |
7af4cc3f | 462 | |
2c38de4c NC |
463 | if (indev && entskb->dev && |
464 | entskb->mac_header != entskb->network_header) { | |
7af4cc3f | 465 | struct nfqnl_msg_packet_hw phw; |
b95cce35 SH |
466 | int len = dev_parse_header(entskb, phw.hw_addr); |
467 | if (len) { | |
468 | phw.hw_addrlen = htons(len); | |
a447189e DM |
469 | if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw)) |
470 | goto nla_put_failure; | |
b95cce35 | 471 | } |
7af4cc3f HW |
472 | } |
473 | ||
b7aa0bf7 | 474 | if (entskb->tstamp.tv64) { |
7af4cc3f | 475 | struct nfqnl_msg_packet_timestamp ts; |
b7aa0bf7 ED |
476 | struct timeval tv = ktime_to_timeval(entskb->tstamp); |
477 | ts.sec = cpu_to_be64(tv.tv_sec); | |
478 | ts.usec = cpu_to_be64(tv.tv_usec); | |
7af4cc3f | 479 | |
a447189e DM |
480 | if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts)) |
481 | goto nla_put_failure; | |
7af4cc3f HW |
482 | } |
483 | ||
ae08ce00 ED |
484 | if (ct && nfqnl_ct_put(skb, ct, ctinfo) < 0) |
485 | goto nla_put_failure; | |
486 | ||
7f87712c FW |
487 | if (cap_len > data_len && |
488 | nla_put_be32(skb, NFQA_CAP_LEN, htonl(cap_len))) | |
ae08ce00 ED |
489 | goto nla_put_failure; |
490 | ||
496e4ae7 | 491 | if (nfqnl_put_packet_info(skb, entskb, csum_verify)) |
7237190d FW |
492 | goto nla_put_failure; |
493 | ||
7af4cc3f | 494 | if (data_len) { |
df6fb868 | 495 | struct nlattr *nla; |
7af4cc3f | 496 | |
ae08ce00 ED |
497 | if (skb_tailroom(skb) < sizeof(*nla) + hlen) |
498 | goto nla_put_failure; | |
7af4cc3f | 499 | |
ae08ce00 | 500 | nla = (struct nlattr *)skb_put(skb, sizeof(*nla)); |
df6fb868 | 501 | nla->nla_type = NFQA_PAYLOAD; |
ae08ce00 | 502 | nla->nla_len = nla_attr_size(data_len); |
7af4cc3f | 503 | |
ae08ce00 | 504 | nfqnl_zcopy(skb, entskb, data_len, hlen); |
7af4cc3f | 505 | } |
601e68e1 | 506 | |
ae08ce00 | 507 | nlh->nlmsg_len = skb->len; |
7af4cc3f HW |
508 | return skb; |
509 | ||
df6fb868 | 510 | nla_put_failure: |
a6729955 | 511 | kfree_skb(skb); |
e87cc472 | 512 | net_err_ratelimited("nf_queue: error creating packet message\n"); |
7af4cc3f HW |
513 | return NULL; |
514 | } | |
515 | ||
516 | static int | |
a5fedd43 FW |
517 | __nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue, |
518 | struct nf_queue_entry *entry) | |
7af4cc3f | 519 | { |
7af4cc3f | 520 | struct sk_buff *nskb; |
f1585086 | 521 | int err = -ENOBUFS; |
5863702a | 522 | __be32 *packet_id_ptr; |
fdb694a0 | 523 | int failopen = 0; |
7af4cc3f | 524 | |
5863702a | 525 | nskb = nfqnl_build_packet_message(queue, entry, &packet_id_ptr); |
f1585086 FW |
526 | if (nskb == NULL) { |
527 | err = -ENOMEM; | |
0ef0f465 | 528 | goto err_out; |
f1585086 | 529 | } |
7af4cc3f | 530 | spin_lock_bh(&queue->lock); |
601e68e1 | 531 | |
7af4cc3f | 532 | if (queue->queue_total >= queue->queue_maxlen) { |
fdb694a0 KK |
533 | if (queue->flags & NFQA_CFG_F_FAIL_OPEN) { |
534 | failopen = 1; | |
535 | err = 0; | |
536 | } else { | |
537 | queue->queue_dropped++; | |
538 | net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n", | |
539 | queue->queue_total); | |
540 | } | |
7af4cc3f HW |
541 | goto err_out_free_nskb; |
542 | } | |
5863702a ED |
543 | entry->id = ++queue->id_sequence; |
544 | *packet_id_ptr = htonl(entry->id); | |
7af4cc3f HW |
545 | |
546 | /* nfnetlink_unicast will either free the nskb or add it to a socket */ | |
e8179610 | 547 | err = nfnetlink_unicast(nskb, net, queue->peer_portid, MSG_DONTWAIT); |
0ef0f465 | 548 | if (err < 0) { |
601e68e1 | 549 | queue->queue_user_dropped++; |
7af4cc3f HW |
550 | goto err_out_unlock; |
551 | } | |
552 | ||
553 | __enqueue_entry(queue, entry); | |
554 | ||
555 | spin_unlock_bh(&queue->lock); | |
0ef0f465 | 556 | return 0; |
7af4cc3f HW |
557 | |
558 | err_out_free_nskb: | |
601e68e1 | 559 | kfree_skb(nskb); |
7af4cc3f HW |
560 | err_out_unlock: |
561 | spin_unlock_bh(&queue->lock); | |
fdb694a0 KK |
562 | if (failopen) |
563 | nf_reinject(entry, NF_ACCEPT); | |
0ef0f465 | 564 | err_out: |
f1585086 | 565 | return err; |
7af4cc3f HW |
566 | } |
567 | ||
a5fedd43 FW |
568 | static struct nf_queue_entry * |
569 | nf_queue_entry_dup(struct nf_queue_entry *e) | |
570 | { | |
571 | struct nf_queue_entry *entry = kmemdup(e, e->size, GFP_ATOMIC); | |
572 | if (entry) { | |
573 | if (nf_queue_entry_get_refs(entry)) | |
574 | return entry; | |
575 | kfree(entry); | |
576 | } | |
577 | return NULL; | |
578 | } | |
579 | ||
580 | #ifdef CONFIG_BRIDGE_NETFILTER | |
581 | /* When called from bridge netfilter, skb->data must point to MAC header | |
582 | * before calling skb_gso_segment(). Else, original MAC header is lost | |
583 | * and segmented skbs will be sent to wrong destination. | |
584 | */ | |
585 | static void nf_bridge_adjust_skb_data(struct sk_buff *skb) | |
586 | { | |
587 | if (skb->nf_bridge) | |
588 | __skb_push(skb, skb->network_header - skb->mac_header); | |
589 | } | |
590 | ||
591 | static void nf_bridge_adjust_segmented_data(struct sk_buff *skb) | |
592 | { | |
593 | if (skb->nf_bridge) | |
594 | __skb_pull(skb, skb->network_header - skb->mac_header); | |
595 | } | |
596 | #else | |
597 | #define nf_bridge_adjust_skb_data(s) do {} while (0) | |
598 | #define nf_bridge_adjust_segmented_data(s) do {} while (0) | |
599 | #endif | |
600 | ||
601 | static void free_entry(struct nf_queue_entry *entry) | |
602 | { | |
603 | nf_queue_entry_release_refs(entry); | |
604 | kfree(entry); | |
605 | } | |
606 | ||
607 | static int | |
608 | __nfqnl_enqueue_packet_gso(struct net *net, struct nfqnl_instance *queue, | |
609 | struct sk_buff *skb, struct nf_queue_entry *entry) | |
610 | { | |
611 | int ret = -ENOMEM; | |
612 | struct nf_queue_entry *entry_seg; | |
613 | ||
614 | nf_bridge_adjust_segmented_data(skb); | |
615 | ||
616 | if (skb->next == NULL) { /* last packet, no need to copy entry */ | |
617 | struct sk_buff *gso_skb = entry->skb; | |
618 | entry->skb = skb; | |
619 | ret = __nfqnl_enqueue_packet(net, queue, entry); | |
620 | if (ret) | |
621 | entry->skb = gso_skb; | |
622 | return ret; | |
623 | } | |
624 | ||
625 | skb->next = NULL; | |
626 | ||
627 | entry_seg = nf_queue_entry_dup(entry); | |
628 | if (entry_seg) { | |
629 | entry_seg->skb = skb; | |
630 | ret = __nfqnl_enqueue_packet(net, queue, entry_seg); | |
631 | if (ret) | |
632 | free_entry(entry_seg); | |
633 | } | |
634 | return ret; | |
635 | } | |
636 | ||
637 | static int | |
638 | nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum) | |
639 | { | |
640 | unsigned int queued; | |
641 | struct nfqnl_instance *queue; | |
642 | struct sk_buff *skb, *segs; | |
643 | int err = -ENOBUFS; | |
644 | struct net *net = dev_net(entry->indev ? | |
645 | entry->indev : entry->outdev); | |
646 | struct nfnl_queue_net *q = nfnl_queue_pernet(net); | |
647 | ||
648 | /* rcu_read_lock()ed by nf_hook_slow() */ | |
649 | queue = instance_lookup(q, queuenum); | |
650 | if (!queue) | |
651 | return -ESRCH; | |
652 | ||
653 | if (queue->copy_mode == NFQNL_COPY_NONE) | |
654 | return -EINVAL; | |
655 | ||
a5fedd43 FW |
656 | skb = entry->skb; |
657 | ||
658 | switch (entry->pf) { | |
659 | case NFPROTO_IPV4: | |
660 | skb->protocol = htons(ETH_P_IP); | |
661 | break; | |
662 | case NFPROTO_IPV6: | |
663 | skb->protocol = htons(ETH_P_IPV6); | |
664 | break; | |
665 | } | |
666 | ||
7b8dfe28 PNA |
667 | if ((queue->flags & NFQA_CFG_F_GSO) || !skb_is_gso(skb)) |
668 | return __nfqnl_enqueue_packet(net, queue, entry); | |
669 | ||
a5fedd43 FW |
670 | nf_bridge_adjust_skb_data(skb); |
671 | segs = skb_gso_segment(skb, 0); | |
672 | /* Does not use PTR_ERR to limit the number of error codes that can be | |
673 | * returned by nf_queue. For instance, callers rely on -ECANCELED to | |
674 | * mean 'ignore this hook'. | |
675 | */ | |
676 | if (IS_ERR(segs)) | |
677 | goto out_err; | |
678 | queued = 0; | |
679 | err = 0; | |
680 | do { | |
681 | struct sk_buff *nskb = segs->next; | |
682 | if (err == 0) | |
683 | err = __nfqnl_enqueue_packet_gso(net, queue, | |
684 | segs, entry); | |
685 | if (err == 0) | |
686 | queued++; | |
687 | else | |
688 | kfree_skb(segs); | |
689 | segs = nskb; | |
690 | } while (segs); | |
691 | ||
692 | if (queued) { | |
693 | if (err) /* some segments are already queued */ | |
694 | free_entry(entry); | |
695 | kfree_skb(skb); | |
696 | return 0; | |
697 | } | |
698 | out_err: | |
699 | nf_bridge_adjust_segmented_data(skb); | |
700 | return err; | |
701 | } | |
702 | ||
7af4cc3f | 703 | static int |
8c88f87c | 704 | nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e, int diff) |
7af4cc3f | 705 | { |
e2b58a67 | 706 | struct sk_buff *nskb; |
7af4cc3f | 707 | |
d8a585d7 PM |
708 | if (diff < 0) { |
709 | if (pskb_trim(e->skb, data_len)) | |
710 | return -ENOMEM; | |
711 | } else if (diff > 0) { | |
7af4cc3f HW |
712 | if (data_len > 0xFFFF) |
713 | return -EINVAL; | |
714 | if (diff > skb_tailroom(e->skb)) { | |
9a732ed6 AE |
715 | nskb = skb_copy_expand(e->skb, skb_headroom(e->skb), |
716 | diff, GFP_ATOMIC); | |
e2b58a67 | 717 | if (!nskb) { |
1158ba27 | 718 | printk(KERN_WARNING "nf_queue: OOM " |
7af4cc3f | 719 | "in mangle, dropping packet\n"); |
e2b58a67 | 720 | return -ENOMEM; |
7af4cc3f | 721 | } |
e2b58a67 PM |
722 | kfree_skb(e->skb); |
723 | e->skb = nskb; | |
7af4cc3f HW |
724 | } |
725 | skb_put(e->skb, diff); | |
726 | } | |
37d41879 | 727 | if (!skb_make_writable(e->skb, data_len)) |
7af4cc3f | 728 | return -ENOMEM; |
27d7ff46 | 729 | skb_copy_to_linear_data(e->skb, data, data_len); |
e7dfb09a | 730 | e->skb->ip_summed = CHECKSUM_NONE; |
7af4cc3f HW |
731 | return 0; |
732 | } | |
733 | ||
7af4cc3f HW |
734 | static int |
735 | nfqnl_set_mode(struct nfqnl_instance *queue, | |
736 | unsigned char mode, unsigned int range) | |
737 | { | |
c5de0dfd | 738 | int status = 0; |
7af4cc3f HW |
739 | |
740 | spin_lock_bh(&queue->lock); | |
c5de0dfd PM |
741 | switch (mode) { |
742 | case NFQNL_COPY_NONE: | |
743 | case NFQNL_COPY_META: | |
744 | queue->copy_mode = mode; | |
745 | queue->copy_range = 0; | |
746 | break; | |
747 | ||
748 | case NFQNL_COPY_PACKET: | |
749 | queue->copy_mode = mode; | |
9cefbbc9 FW |
750 | if (range == 0 || range > NFQNL_MAX_COPY_RANGE) |
751 | queue->copy_range = NFQNL_MAX_COPY_RANGE; | |
c5de0dfd PM |
752 | else |
753 | queue->copy_range = range; | |
754 | break; | |
755 | ||
756 | default: | |
757 | status = -EINVAL; | |
758 | ||
759 | } | |
7af4cc3f HW |
760 | spin_unlock_bh(&queue->lock); |
761 | ||
762 | return status; | |
763 | } | |
764 | ||
765 | static int | |
02f014d8 | 766 | dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex) |
7af4cc3f | 767 | { |
02f014d8 PM |
768 | if (entry->indev) |
769 | if (entry->indev->ifindex == ifindex) | |
7af4cc3f | 770 | return 1; |
02f014d8 PM |
771 | if (entry->outdev) |
772 | if (entry->outdev->ifindex == ifindex) | |
7af4cc3f | 773 | return 1; |
ef47c6a7 PM |
774 | #ifdef CONFIG_BRIDGE_NETFILTER |
775 | if (entry->skb->nf_bridge) { | |
776 | if (entry->skb->nf_bridge->physindev && | |
777 | entry->skb->nf_bridge->physindev->ifindex == ifindex) | |
778 | return 1; | |
779 | if (entry->skb->nf_bridge->physoutdev && | |
780 | entry->skb->nf_bridge->physoutdev->ifindex == ifindex) | |
781 | return 1; | |
782 | } | |
783 | #endif | |
7af4cc3f HW |
784 | return 0; |
785 | } | |
786 | ||
787 | /* drop all packets with either indev or outdev == ifindex from all queue | |
788 | * instances */ | |
789 | static void | |
e8179610 | 790 | nfqnl_dev_drop(struct net *net, int ifindex) |
7af4cc3f HW |
791 | { |
792 | int i; | |
e8179610 | 793 | struct nfnl_queue_net *q = nfnl_queue_pernet(net); |
601e68e1 | 794 | |
9872bec7 | 795 | rcu_read_lock(); |
7af4cc3f | 796 | |
9872bec7 | 797 | for (i = 0; i < INSTANCE_BUCKETS; i++) { |
7af4cc3f | 798 | struct nfqnl_instance *inst; |
e8179610 | 799 | struct hlist_head *head = &q->instance_table[i]; |
7af4cc3f | 800 | |
b67bfe0d | 801 | hlist_for_each_entry_rcu(inst, head, hlist) |
b43d8d85 | 802 | nfqnl_flush(inst, dev_cmp, ifindex); |
7af4cc3f HW |
803 | } |
804 | ||
9872bec7 | 805 | rcu_read_unlock(); |
7af4cc3f HW |
806 | } |
807 | ||
808 | #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0) | |
809 | ||
810 | static int | |
811 | nfqnl_rcv_dev_event(struct notifier_block *this, | |
812 | unsigned long event, void *ptr) | |
813 | { | |
351638e7 | 814 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
7af4cc3f HW |
815 | |
816 | /* Drop any packets associated with the downed device */ | |
817 | if (event == NETDEV_DOWN) | |
e8179610 | 818 | nfqnl_dev_drop(dev_net(dev), dev->ifindex); |
7af4cc3f HW |
819 | return NOTIFY_DONE; |
820 | } | |
821 | ||
822 | static struct notifier_block nfqnl_dev_notifier = { | |
823 | .notifier_call = nfqnl_rcv_dev_event, | |
824 | }; | |
825 | ||
826 | static int | |
827 | nfqnl_rcv_nl_event(struct notifier_block *this, | |
828 | unsigned long event, void *ptr) | |
829 | { | |
830 | struct netlink_notify *n = ptr; | |
e8179610 | 831 | struct nfnl_queue_net *q = nfnl_queue_pernet(n->net); |
7af4cc3f | 832 | |
dee5817e | 833 | if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) { |
7af4cc3f HW |
834 | int i; |
835 | ||
15e47304 | 836 | /* destroy all instances for this portid */ |
e8179610 | 837 | spin_lock(&q->instances_lock); |
9872bec7 | 838 | for (i = 0; i < INSTANCE_BUCKETS; i++) { |
b67bfe0d | 839 | struct hlist_node *t2; |
7af4cc3f | 840 | struct nfqnl_instance *inst; |
e8179610 | 841 | struct hlist_head *head = &q->instance_table[i]; |
7af4cc3f | 842 | |
b67bfe0d | 843 | hlist_for_each_entry_safe(inst, t2, head, hlist) { |
e8179610 | 844 | if (n->portid == inst->peer_portid) |
7af4cc3f HW |
845 | __instance_destroy(inst); |
846 | } | |
847 | } | |
e8179610 | 848 | spin_unlock(&q->instances_lock); |
7af4cc3f HW |
849 | } |
850 | return NOTIFY_DONE; | |
851 | } | |
852 | ||
853 | static struct notifier_block nfqnl_rtnl_notifier = { | |
854 | .notifier_call = nfqnl_rcv_nl_event, | |
855 | }; | |
856 | ||
5bf75853 PM |
857 | static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = { |
858 | [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) }, | |
859 | [NFQA_MARK] = { .type = NLA_U32 }, | |
860 | [NFQA_PAYLOAD] = { .type = NLA_UNSPEC }, | |
9cb01766 | 861 | [NFQA_CT] = { .type = NLA_UNSPEC }, |
838ab636 HW |
862 | }; |
863 | ||
97d32cf9 FW |
864 | static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = { |
865 | [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) }, | |
866 | [NFQA_MARK] = { .type = NLA_U32 }, | |
867 | }; | |
868 | ||
e8179610 G |
869 | static struct nfqnl_instance * |
870 | verdict_instance_lookup(struct nfnl_queue_net *q, u16 queue_num, int nlportid) | |
97d32cf9 FW |
871 | { |
872 | struct nfqnl_instance *queue; | |
873 | ||
e8179610 | 874 | queue = instance_lookup(q, queue_num); |
97d32cf9 FW |
875 | if (!queue) |
876 | return ERR_PTR(-ENODEV); | |
877 | ||
15e47304 | 878 | if (queue->peer_portid != nlportid) |
97d32cf9 FW |
879 | return ERR_PTR(-EPERM); |
880 | ||
881 | return queue; | |
882 | } | |
883 | ||
884 | static struct nfqnl_msg_verdict_hdr* | |
885 | verdicthdr_get(const struct nlattr * const nfqa[]) | |
886 | { | |
887 | struct nfqnl_msg_verdict_hdr *vhdr; | |
888 | unsigned int verdict; | |
889 | ||
890 | if (!nfqa[NFQA_VERDICT_HDR]) | |
891 | return NULL; | |
892 | ||
893 | vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]); | |
c6675233 FW |
894 | verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK; |
895 | if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN) | |
97d32cf9 FW |
896 | return NULL; |
897 | return vhdr; | |
898 | } | |
899 | ||
900 | static int nfq_id_after(unsigned int id, unsigned int max) | |
901 | { | |
902 | return (int)(id - max) > 0; | |
903 | } | |
904 | ||
905 | static int | |
906 | nfqnl_recv_verdict_batch(struct sock *ctnl, struct sk_buff *skb, | |
907 | const struct nlmsghdr *nlh, | |
908 | const struct nlattr * const nfqa[]) | |
909 | { | |
3da07c0c | 910 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
97d32cf9 FW |
911 | struct nf_queue_entry *entry, *tmp; |
912 | unsigned int verdict, maxid; | |
913 | struct nfqnl_msg_verdict_hdr *vhdr; | |
914 | struct nfqnl_instance *queue; | |
915 | LIST_HEAD(batch_list); | |
916 | u16 queue_num = ntohs(nfmsg->res_id); | |
917 | ||
e8179610 G |
918 | struct net *net = sock_net(ctnl); |
919 | struct nfnl_queue_net *q = nfnl_queue_pernet(net); | |
920 | ||
921 | queue = verdict_instance_lookup(q, queue_num, | |
922 | NETLINK_CB(skb).portid); | |
97d32cf9 FW |
923 | if (IS_ERR(queue)) |
924 | return PTR_ERR(queue); | |
925 | ||
926 | vhdr = verdicthdr_get(nfqa); | |
927 | if (!vhdr) | |
928 | return -EINVAL; | |
929 | ||
930 | verdict = ntohl(vhdr->verdict); | |
931 | maxid = ntohl(vhdr->id); | |
932 | ||
933 | spin_lock_bh(&queue->lock); | |
934 | ||
935 | list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) { | |
936 | if (nfq_id_after(entry->id, maxid)) | |
937 | break; | |
938 | __dequeue_entry(queue, entry); | |
939 | list_add_tail(&entry->list, &batch_list); | |
940 | } | |
941 | ||
942 | spin_unlock_bh(&queue->lock); | |
943 | ||
944 | if (list_empty(&batch_list)) | |
945 | return -ENOENT; | |
946 | ||
947 | list_for_each_entry_safe(entry, tmp, &batch_list, list) { | |
948 | if (nfqa[NFQA_MARK]) | |
949 | entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK])); | |
950 | nf_reinject(entry, verdict); | |
951 | } | |
952 | return 0; | |
953 | } | |
954 | ||
7af4cc3f HW |
955 | static int |
956 | nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb, | |
39938324 PM |
957 | const struct nlmsghdr *nlh, |
958 | const struct nlattr * const nfqa[]) | |
7af4cc3f | 959 | { |
3da07c0c | 960 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
7af4cc3f HW |
961 | u_int16_t queue_num = ntohs(nfmsg->res_id); |
962 | ||
963 | struct nfqnl_msg_verdict_hdr *vhdr; | |
964 | struct nfqnl_instance *queue; | |
965 | unsigned int verdict; | |
02f014d8 | 966 | struct nf_queue_entry *entry; |
8c88f87c PNA |
967 | enum ip_conntrack_info uninitialized_var(ctinfo); |
968 | struct nf_conn *ct = NULL; | |
7af4cc3f | 969 | |
e8179610 G |
970 | struct net *net = sock_net(ctnl); |
971 | struct nfnl_queue_net *q = nfnl_queue_pernet(net); | |
7af4cc3f | 972 | |
e8179610 G |
973 | queue = instance_lookup(q, queue_num); |
974 | if (!queue) | |
975 | queue = verdict_instance_lookup(q, queue_num, | |
976 | NETLINK_CB(skb).portid); | |
97d32cf9 FW |
977 | if (IS_ERR(queue)) |
978 | return PTR_ERR(queue); | |
7af4cc3f | 979 | |
97d32cf9 FW |
980 | vhdr = verdicthdr_get(nfqa); |
981 | if (!vhdr) | |
84a797dd | 982 | return -EINVAL; |
7af4cc3f | 983 | |
7af4cc3f HW |
984 | verdict = ntohl(vhdr->verdict); |
985 | ||
b43d8d85 | 986 | entry = find_dequeue_entry(queue, ntohl(vhdr->id)); |
84a797dd ED |
987 | if (entry == NULL) |
988 | return -ENOENT; | |
7af4cc3f | 989 | |
9cb01766 | 990 | rcu_read_lock(); |
7c622345 PNA |
991 | if (nfqa[NFQA_CT] && (queue->flags & NFQA_CFG_F_CONNTRACK)) |
992 | ct = nfqnl_ct_parse(entry->skb, nfqa[NFQA_CT], &ctinfo); | |
9cb01766 | 993 | |
df6fb868 | 994 | if (nfqa[NFQA_PAYLOAD]) { |
8c88f87c PNA |
995 | u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]); |
996 | int diff = payload_len - entry->skb->len; | |
997 | ||
df6fb868 | 998 | if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]), |
8c88f87c | 999 | payload_len, entry, diff) < 0) |
7af4cc3f | 1000 | verdict = NF_DROP; |
8c88f87c | 1001 | |
7c622345 PNA |
1002 | if (ct) |
1003 | nfqnl_ct_seq_adjust(skb, ct, ctinfo, diff); | |
7af4cc3f | 1004 | } |
8c88f87c | 1005 | rcu_read_unlock(); |
7af4cc3f | 1006 | |
df6fb868 | 1007 | if (nfqa[NFQA_MARK]) |
ea3a66ff | 1008 | entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK])); |
601e68e1 | 1009 | |
4b3d15ef | 1010 | nf_reinject(entry, verdict); |
7af4cc3f HW |
1011 | return 0; |
1012 | } | |
1013 | ||
1014 | static int | |
1015 | nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb, | |
39938324 PM |
1016 | const struct nlmsghdr *nlh, |
1017 | const struct nlattr * const nfqa[]) | |
7af4cc3f HW |
1018 | { |
1019 | return -ENOTSUPP; | |
1020 | } | |
1021 | ||
5bf75853 PM |
1022 | static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = { |
1023 | [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) }, | |
1024 | [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) }, | |
838ab636 HW |
1025 | }; |
1026 | ||
e3ac5298 | 1027 | static const struct nf_queue_handler nfqh = { |
bbd86b9f HW |
1028 | .outfn = &nfqnl_enqueue_packet, |
1029 | }; | |
1030 | ||
7af4cc3f HW |
1031 | static int |
1032 | nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb, | |
39938324 PM |
1033 | const struct nlmsghdr *nlh, |
1034 | const struct nlattr * const nfqa[]) | |
7af4cc3f | 1035 | { |
3da07c0c | 1036 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
7af4cc3f HW |
1037 | u_int16_t queue_num = ntohs(nfmsg->res_id); |
1038 | struct nfqnl_instance *queue; | |
9872bec7 | 1039 | struct nfqnl_msg_config_cmd *cmd = NULL; |
e8179610 G |
1040 | struct net *net = sock_net(ctnl); |
1041 | struct nfnl_queue_net *q = nfnl_queue_pernet(net); | |
838ab636 | 1042 | int ret = 0; |
7af4cc3f | 1043 | |
9872bec7 PM |
1044 | if (nfqa[NFQA_CFG_CMD]) { |
1045 | cmd = nla_data(nfqa[NFQA_CFG_CMD]); | |
1046 | ||
0360ae41 | 1047 | /* Obsolete commands without queue context */ |
9872bec7 | 1048 | switch (cmd->command) { |
0360ae41 FW |
1049 | case NFQNL_CFG_CMD_PF_BIND: return 0; |
1050 | case NFQNL_CFG_CMD_PF_UNBIND: return 0; | |
9872bec7 | 1051 | } |
9872bec7 PM |
1052 | } |
1053 | ||
1054 | rcu_read_lock(); | |
e8179610 | 1055 | queue = instance_lookup(q, queue_num); |
15e47304 | 1056 | if (queue && queue->peer_portid != NETLINK_CB(skb).portid) { |
a3c8e7fd | 1057 | ret = -EPERM; |
9872bec7 | 1058 | goto err_out_unlock; |
a3c8e7fd PM |
1059 | } |
1060 | ||
9872bec7 | 1061 | if (cmd != NULL) { |
7af4cc3f HW |
1062 | switch (cmd->command) { |
1063 | case NFQNL_CFG_CMD_BIND: | |
9872bec7 PM |
1064 | if (queue) { |
1065 | ret = -EBUSY; | |
1066 | goto err_out_unlock; | |
1067 | } | |
e8179610 G |
1068 | queue = instance_create(q, queue_num, |
1069 | NETLINK_CB(skb).portid); | |
baab2ce7 PM |
1070 | if (IS_ERR(queue)) { |
1071 | ret = PTR_ERR(queue); | |
9872bec7 PM |
1072 | goto err_out_unlock; |
1073 | } | |
7af4cc3f HW |
1074 | break; |
1075 | case NFQNL_CFG_CMD_UNBIND: | |
9872bec7 PM |
1076 | if (!queue) { |
1077 | ret = -ENODEV; | |
1078 | goto err_out_unlock; | |
1079 | } | |
e8179610 | 1080 | instance_destroy(q, queue); |
7af4cc3f HW |
1081 | break; |
1082 | case NFQNL_CFG_CMD_PF_BIND: | |
7af4cc3f | 1083 | case NFQNL_CFG_CMD_PF_UNBIND: |
7af4cc3f HW |
1084 | break; |
1085 | default: | |
cd21f0ac | 1086 | ret = -ENOTSUPP; |
838ab636 | 1087 | break; |
7af4cc3f | 1088 | } |
7af4cc3f HW |
1089 | } |
1090 | ||
df6fb868 | 1091 | if (nfqa[NFQA_CFG_PARAMS]) { |
7af4cc3f | 1092 | struct nfqnl_msg_config_params *params; |
7af4cc3f | 1093 | |
406dbfc9 | 1094 | if (!queue) { |
a3c8e7fd | 1095 | ret = -ENODEV; |
9872bec7 | 1096 | goto err_out_unlock; |
406dbfc9 | 1097 | } |
df6fb868 | 1098 | params = nla_data(nfqa[NFQA_CFG_PARAMS]); |
7af4cc3f HW |
1099 | nfqnl_set_mode(queue, params->copy_mode, |
1100 | ntohl(params->copy_range)); | |
1101 | } | |
1102 | ||
df6fb868 | 1103 | if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) { |
829e17a1 | 1104 | __be32 *queue_maxlen; |
a3c8e7fd PM |
1105 | |
1106 | if (!queue) { | |
1107 | ret = -ENODEV; | |
9872bec7 | 1108 | goto err_out_unlock; |
a3c8e7fd | 1109 | } |
df6fb868 | 1110 | queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]); |
829e17a1 EL |
1111 | spin_lock_bh(&queue->lock); |
1112 | queue->queue_maxlen = ntohl(*queue_maxlen); | |
1113 | spin_unlock_bh(&queue->lock); | |
1114 | } | |
1115 | ||
fdb694a0 KK |
1116 | if (nfqa[NFQA_CFG_FLAGS]) { |
1117 | __u32 flags, mask; | |
1118 | ||
1119 | if (!queue) { | |
1120 | ret = -ENODEV; | |
1121 | goto err_out_unlock; | |
1122 | } | |
1123 | ||
1124 | if (!nfqa[NFQA_CFG_MASK]) { | |
1125 | /* A mask is needed to specify which flags are being | |
1126 | * changed. | |
1127 | */ | |
1128 | ret = -EINVAL; | |
1129 | goto err_out_unlock; | |
1130 | } | |
1131 | ||
1132 | flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS])); | |
1133 | mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK])); | |
1134 | ||
46ba5a25 KK |
1135 | if (flags >= NFQA_CFG_F_MAX) { |
1136 | ret = -EOPNOTSUPP; | |
1137 | goto err_out_unlock; | |
1138 | } | |
1139 | ||
fdb694a0 KK |
1140 | spin_lock_bh(&queue->lock); |
1141 | queue->flags &= ~mask; | |
1142 | queue->flags |= flags & mask; | |
1143 | spin_unlock_bh(&queue->lock); | |
1144 | } | |
1145 | ||
9872bec7 PM |
1146 | err_out_unlock: |
1147 | rcu_read_unlock(); | |
838ab636 | 1148 | return ret; |
7af4cc3f HW |
1149 | } |
1150 | ||
7c8d4cb4 | 1151 | static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = { |
84a797dd | 1152 | [NFQNL_MSG_PACKET] = { .call_rcu = nfqnl_recv_unsupp, |
37d2e7a2 | 1153 | .attr_count = NFQA_MAX, }, |
84a797dd | 1154 | [NFQNL_MSG_VERDICT] = { .call_rcu = nfqnl_recv_verdict, |
5bf75853 PM |
1155 | .attr_count = NFQA_MAX, |
1156 | .policy = nfqa_verdict_policy }, | |
7af4cc3f | 1157 | [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config, |
5bf75853 PM |
1158 | .attr_count = NFQA_CFG_MAX, |
1159 | .policy = nfqa_cfg_policy }, | |
97d32cf9 FW |
1160 | [NFQNL_MSG_VERDICT_BATCH]={ .call_rcu = nfqnl_recv_verdict_batch, |
1161 | .attr_count = NFQA_MAX, | |
1162 | .policy = nfqa_verdict_batch_policy }, | |
7af4cc3f HW |
1163 | }; |
1164 | ||
7c8d4cb4 | 1165 | static const struct nfnetlink_subsystem nfqnl_subsys = { |
7af4cc3f HW |
1166 | .name = "nf_queue", |
1167 | .subsys_id = NFNL_SUBSYS_QUEUE, | |
1168 | .cb_count = NFQNL_MSG_MAX, | |
7af4cc3f HW |
1169 | .cb = nfqnl_cb, |
1170 | }; | |
1171 | ||
838ab636 HW |
1172 | #ifdef CONFIG_PROC_FS |
1173 | struct iter_state { | |
e8179610 | 1174 | struct seq_net_private p; |
838ab636 HW |
1175 | unsigned int bucket; |
1176 | }; | |
1177 | ||
1178 | static struct hlist_node *get_first(struct seq_file *seq) | |
1179 | { | |
1180 | struct iter_state *st = seq->private; | |
e8179610 G |
1181 | struct net *net; |
1182 | struct nfnl_queue_net *q; | |
838ab636 HW |
1183 | |
1184 | if (!st) | |
1185 | return NULL; | |
1186 | ||
e8179610 G |
1187 | net = seq_file_net(seq); |
1188 | q = nfnl_queue_pernet(net); | |
838ab636 | 1189 | for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) { |
e8179610 G |
1190 | if (!hlist_empty(&q->instance_table[st->bucket])) |
1191 | return q->instance_table[st->bucket].first; | |
838ab636 HW |
1192 | } |
1193 | return NULL; | |
1194 | } | |
1195 | ||
1196 | static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h) | |
1197 | { | |
1198 | struct iter_state *st = seq->private; | |
e8179610 | 1199 | struct net *net = seq_file_net(seq); |
838ab636 HW |
1200 | |
1201 | h = h->next; | |
1202 | while (!h) { | |
e8179610 G |
1203 | struct nfnl_queue_net *q; |
1204 | ||
838ab636 HW |
1205 | if (++st->bucket >= INSTANCE_BUCKETS) |
1206 | return NULL; | |
1207 | ||
e8179610 G |
1208 | q = nfnl_queue_pernet(net); |
1209 | h = q->instance_table[st->bucket].first; | |
838ab636 HW |
1210 | } |
1211 | return h; | |
1212 | } | |
1213 | ||
1214 | static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos) | |
1215 | { | |
1216 | struct hlist_node *head; | |
1217 | head = get_first(seq); | |
1218 | ||
1219 | if (head) | |
1220 | while (pos && (head = get_next(seq, head))) | |
1221 | pos--; | |
1222 | return pos ? NULL : head; | |
1223 | } | |
1224 | ||
e8179610 G |
1225 | static void *seq_start(struct seq_file *s, loff_t *pos) |
1226 | __acquires(nfnl_queue_pernet(seq_file_net(s))->instances_lock) | |
838ab636 | 1227 | { |
e8179610 G |
1228 | spin_lock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock); |
1229 | return get_idx(s, *pos); | |
838ab636 HW |
1230 | } |
1231 | ||
1232 | static void *seq_next(struct seq_file *s, void *v, loff_t *pos) | |
1233 | { | |
1234 | (*pos)++; | |
1235 | return get_next(s, v); | |
1236 | } | |
1237 | ||
1238 | static void seq_stop(struct seq_file *s, void *v) | |
e8179610 | 1239 | __releases(nfnl_queue_pernet(seq_file_net(s))->instances_lock) |
838ab636 | 1240 | { |
e8179610 | 1241 | spin_unlock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock); |
838ab636 HW |
1242 | } |
1243 | ||
1244 | static int seq_show(struct seq_file *s, void *v) | |
1245 | { | |
1246 | const struct nfqnl_instance *inst = v; | |
1247 | ||
1248 | return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n", | |
1249 | inst->queue_num, | |
15e47304 | 1250 | inst->peer_portid, inst->queue_total, |
838ab636 HW |
1251 | inst->copy_mode, inst->copy_range, |
1252 | inst->queue_dropped, inst->queue_user_dropped, | |
5863702a | 1253 | inst->id_sequence, 1); |
838ab636 HW |
1254 | } |
1255 | ||
56b3d975 | 1256 | static const struct seq_operations nfqnl_seq_ops = { |
838ab636 HW |
1257 | .start = seq_start, |
1258 | .next = seq_next, | |
1259 | .stop = seq_stop, | |
1260 | .show = seq_show, | |
1261 | }; | |
1262 | ||
1263 | static int nfqnl_open(struct inode *inode, struct file *file) | |
1264 | { | |
e8179610 | 1265 | return seq_open_net(inode, file, &nfqnl_seq_ops, |
e2da5913 | 1266 | sizeof(struct iter_state)); |
838ab636 HW |
1267 | } |
1268 | ||
da7071d7 | 1269 | static const struct file_operations nfqnl_file_ops = { |
838ab636 HW |
1270 | .owner = THIS_MODULE, |
1271 | .open = nfqnl_open, | |
1272 | .read = seq_read, | |
1273 | .llseek = seq_lseek, | |
e8179610 | 1274 | .release = seq_release_net, |
838ab636 HW |
1275 | }; |
1276 | ||
1277 | #endif /* PROC_FS */ | |
1278 | ||
e8179610 | 1279 | static int __net_init nfnl_queue_net_init(struct net *net) |
7af4cc3f | 1280 | { |
e8179610 G |
1281 | unsigned int i; |
1282 | struct nfnl_queue_net *q = nfnl_queue_pernet(net); | |
601e68e1 | 1283 | |
838ab636 | 1284 | for (i = 0; i < INSTANCE_BUCKETS; i++) |
e8179610 G |
1285 | INIT_HLIST_HEAD(&q->instance_table[i]); |
1286 | ||
1287 | spin_lock_init(&q->instances_lock); | |
1288 | ||
1289 | #ifdef CONFIG_PROC_FS | |
1290 | if (!proc_create("nfnetlink_queue", 0440, | |
1291 | net->nf.proc_netfilter, &nfqnl_file_ops)) | |
1292 | return -ENOMEM; | |
1293 | #endif | |
1294 | return 0; | |
1295 | } | |
1296 | ||
1297 | static void __net_exit nfnl_queue_net_exit(struct net *net) | |
1298 | { | |
e778f56e | 1299 | #ifdef CONFIG_PROC_FS |
e8179610 | 1300 | remove_proc_entry("nfnetlink_queue", net->nf.proc_netfilter); |
e778f56e | 1301 | #endif |
e8179610 G |
1302 | } |
1303 | ||
1304 | static struct pernet_operations nfnl_queue_net_ops = { | |
1305 | .init = nfnl_queue_net_init, | |
1306 | .exit = nfnl_queue_net_exit, | |
1307 | .id = &nfnl_queue_net_id, | |
1308 | .size = sizeof(struct nfnl_queue_net), | |
1309 | }; | |
1310 | ||
1311 | static int __init nfnetlink_queue_init(void) | |
1312 | { | |
1313 | int status = -ENOMEM; | |
838ab636 | 1314 | |
7af4cc3f HW |
1315 | netlink_register_notifier(&nfqnl_rtnl_notifier); |
1316 | status = nfnetlink_subsys_register(&nfqnl_subsys); | |
1317 | if (status < 0) { | |
e8179610 | 1318 | pr_err("nf_queue: failed to create netlink socket\n"); |
7af4cc3f HW |
1319 | goto cleanup_netlink_notifier; |
1320 | } | |
1321 | ||
e8179610 G |
1322 | status = register_pernet_subsys(&nfnl_queue_net_ops); |
1323 | if (status < 0) { | |
1324 | pr_err("nf_queue: failed to register pernet ops\n"); | |
838ab636 | 1325 | goto cleanup_subsys; |
e8179610 | 1326 | } |
7af4cc3f | 1327 | register_netdevice_notifier(&nfqnl_dev_notifier); |
0360ae41 | 1328 | nf_register_queue_handler(&nfqh); |
7af4cc3f HW |
1329 | return status; |
1330 | ||
838ab636 | 1331 | cleanup_subsys: |
7af4cc3f | 1332 | nfnetlink_subsys_unregister(&nfqnl_subsys); |
7af4cc3f HW |
1333 | cleanup_netlink_notifier: |
1334 | netlink_unregister_notifier(&nfqnl_rtnl_notifier); | |
1335 | return status; | |
1336 | } | |
1337 | ||
65b4b4e8 | 1338 | static void __exit nfnetlink_queue_fini(void) |
7af4cc3f | 1339 | { |
0360ae41 | 1340 | nf_unregister_queue_handler(); |
32292a7f | 1341 | unregister_netdevice_notifier(&nfqnl_dev_notifier); |
e8179610 | 1342 | unregister_pernet_subsys(&nfnl_queue_net_ops); |
32292a7f PM |
1343 | nfnetlink_subsys_unregister(&nfqnl_subsys); |
1344 | netlink_unregister_notifier(&nfqnl_rtnl_notifier); | |
67137f3c JDB |
1345 | |
1346 | rcu_barrier(); /* Wait for completion of call_rcu()'s */ | |
7af4cc3f HW |
1347 | } |
1348 | ||
1349 | MODULE_DESCRIPTION("netfilter packet queue handler"); | |
1350 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); | |
1351 | MODULE_LICENSE("GPL"); | |
1352 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE); | |
1353 | ||
65b4b4e8 AM |
1354 | module_init(nfnetlink_queue_init); |
1355 | module_exit(nfnetlink_queue_fini); |