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