]>
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> |
7af4cc3f HW |
33 | |
34 | #include <asm/atomic.h> | |
35 | ||
fbcd923c HW |
36 | #ifdef CONFIG_BRIDGE_NETFILTER |
37 | #include "../bridge/br_private.h" | |
38 | #endif | |
39 | ||
7af4cc3f HW |
40 | #define NFQNL_QMAX_DEFAULT 1024 |
41 | ||
7af4cc3f HW |
42 | struct nfqnl_instance { |
43 | struct hlist_node hlist; /* global list of queues */ | |
9872bec7 | 44 | struct rcu_head rcu; |
7af4cc3f HW |
45 | |
46 | int peer_pid; | |
47 | unsigned int queue_maxlen; | |
48 | unsigned int copy_range; | |
7af4cc3f HW |
49 | unsigned int queue_dropped; |
50 | unsigned int queue_user_dropped; | |
51 | ||
7af4cc3f HW |
52 | |
53 | u_int16_t queue_num; /* number of this queue */ | |
54 | u_int8_t copy_mode; | |
c463ac97 ED |
55 | /* |
56 | * Following fields are dirtied for each queued packet, | |
57 | * keep them in same cache line if possible. | |
58 | */ | |
59 | spinlock_t lock; | |
60 | unsigned int queue_total; | |
5863702a | 61 | unsigned int id_sequence; /* 'sequence' of pkt ids */ |
7af4cc3f HW |
62 | struct list_head queue_list; /* packets in queue */ |
63 | }; | |
64 | ||
02f014d8 | 65 | typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long); |
7af4cc3f | 66 | |
9872bec7 | 67 | static DEFINE_SPINLOCK(instances_lock); |
7af4cc3f | 68 | |
7af4cc3f | 69 | #define INSTANCE_BUCKETS 16 |
9d6023ab | 70 | static struct hlist_head instance_table[INSTANCE_BUCKETS] __read_mostly; |
7af4cc3f HW |
71 | |
72 | static inline u_int8_t instance_hashfn(u_int16_t queue_num) | |
73 | { | |
74 | return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS; | |
75 | } | |
76 | ||
77 | static struct nfqnl_instance * | |
9872bec7 | 78 | instance_lookup(u_int16_t queue_num) |
7af4cc3f HW |
79 | { |
80 | struct hlist_head *head; | |
81 | struct hlist_node *pos; | |
82 | struct nfqnl_instance *inst; | |
83 | ||
84 | head = &instance_table[instance_hashfn(queue_num)]; | |
9872bec7 | 85 | hlist_for_each_entry_rcu(inst, pos, head, hlist) { |
7af4cc3f HW |
86 | if (inst->queue_num == queue_num) |
87 | return inst; | |
88 | } | |
89 | return NULL; | |
90 | } | |
91 | ||
7af4cc3f HW |
92 | static struct nfqnl_instance * |
93 | instance_create(u_int16_t queue_num, int pid) | |
94 | { | |
baab2ce7 | 95 | struct nfqnl_instance *inst; |
9872bec7 | 96 | unsigned int h; |
baab2ce7 | 97 | int err; |
7af4cc3f | 98 | |
9872bec7 | 99 | spin_lock(&instances_lock); |
baab2ce7 PM |
100 | if (instance_lookup(queue_num)) { |
101 | err = -EEXIST; | |
7af4cc3f | 102 | goto out_unlock; |
baab2ce7 | 103 | } |
7af4cc3f | 104 | |
10dfdc69 | 105 | inst = kzalloc(sizeof(*inst), GFP_ATOMIC); |
baab2ce7 PM |
106 | if (!inst) { |
107 | err = -ENOMEM; | |
7af4cc3f | 108 | goto out_unlock; |
baab2ce7 | 109 | } |
7af4cc3f | 110 | |
7af4cc3f HW |
111 | inst->queue_num = queue_num; |
112 | inst->peer_pid = pid; | |
113 | inst->queue_maxlen = NFQNL_QMAX_DEFAULT; | |
114 | inst->copy_range = 0xfffff; | |
115 | inst->copy_mode = NFQNL_COPY_NONE; | |
181a46a5 | 116 | spin_lock_init(&inst->lock); |
7af4cc3f HW |
117 | INIT_LIST_HEAD(&inst->queue_list); |
118 | ||
baab2ce7 PM |
119 | if (!try_module_get(THIS_MODULE)) { |
120 | err = -EAGAIN; | |
7af4cc3f | 121 | goto out_free; |
baab2ce7 | 122 | } |
7af4cc3f | 123 | |
9872bec7 PM |
124 | h = instance_hashfn(queue_num); |
125 | hlist_add_head_rcu(&inst->hlist, &instance_table[h]); | |
7af4cc3f | 126 | |
9872bec7 | 127 | spin_unlock(&instances_lock); |
7af4cc3f | 128 | |
7af4cc3f HW |
129 | return inst; |
130 | ||
131 | out_free: | |
132 | kfree(inst); | |
133 | out_unlock: | |
9872bec7 | 134 | spin_unlock(&instances_lock); |
baab2ce7 | 135 | return ERR_PTR(err); |
7af4cc3f HW |
136 | } |
137 | ||
b43d8d85 PM |
138 | static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, |
139 | unsigned long data); | |
7af4cc3f HW |
140 | |
141 | static void | |
9872bec7 | 142 | instance_destroy_rcu(struct rcu_head *head) |
7af4cc3f | 143 | { |
9872bec7 PM |
144 | struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance, |
145 | rcu); | |
7af4cc3f | 146 | |
b43d8d85 | 147 | nfqnl_flush(inst, NULL, 0); |
9872bec7 | 148 | kfree(inst); |
7af4cc3f HW |
149 | module_put(THIS_MODULE); |
150 | } | |
151 | ||
9872bec7 | 152 | static void |
7af4cc3f HW |
153 | __instance_destroy(struct nfqnl_instance *inst) |
154 | { | |
9872bec7 PM |
155 | hlist_del_rcu(&inst->hlist); |
156 | call_rcu(&inst->rcu, instance_destroy_rcu); | |
7af4cc3f HW |
157 | } |
158 | ||
9872bec7 | 159 | static void |
7af4cc3f HW |
160 | instance_destroy(struct nfqnl_instance *inst) |
161 | { | |
9872bec7 PM |
162 | spin_lock(&instances_lock); |
163 | __instance_destroy(inst); | |
164 | spin_unlock(&instances_lock); | |
7af4cc3f HW |
165 | } |
166 | ||
7af4cc3f | 167 | static inline void |
02f014d8 | 168 | __enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry) |
7af4cc3f | 169 | { |
0ac41e81 | 170 | list_add_tail(&entry->list, &queue->queue_list); |
7af4cc3f HW |
171 | queue->queue_total++; |
172 | } | |
173 | ||
02f014d8 | 174 | static struct nf_queue_entry * |
b43d8d85 | 175 | find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id) |
7af4cc3f | 176 | { |
02f014d8 | 177 | struct nf_queue_entry *entry = NULL, *i; |
601e68e1 | 178 | |
7af4cc3f | 179 | spin_lock_bh(&queue->lock); |
b43d8d85 PM |
180 | |
181 | list_for_each_entry(i, &queue->queue_list, list) { | |
182 | if (i->id == id) { | |
183 | entry = i; | |
184 | break; | |
185 | } | |
186 | } | |
187 | ||
188 | if (entry) { | |
189 | list_del(&entry->list); | |
190 | queue->queue_total--; | |
191 | } | |
192 | ||
7af4cc3f HW |
193 | spin_unlock_bh(&queue->lock); |
194 | ||
195 | return entry; | |
196 | } | |
197 | ||
198 | static void | |
b43d8d85 | 199 | nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data) |
7af4cc3f | 200 | { |
02f014d8 | 201 | struct nf_queue_entry *entry, *next; |
b43d8d85 | 202 | |
7af4cc3f | 203 | spin_lock_bh(&queue->lock); |
b43d8d85 PM |
204 | list_for_each_entry_safe(entry, next, &queue->queue_list, list) { |
205 | if (!cmpfn || cmpfn(entry, data)) { | |
206 | list_del(&entry->list); | |
207 | queue->queue_total--; | |
4b3d15ef | 208 | nf_reinject(entry, NF_DROP); |
b43d8d85 PM |
209 | } |
210 | } | |
7af4cc3f HW |
211 | spin_unlock_bh(&queue->lock); |
212 | } | |
213 | ||
214 | static struct sk_buff * | |
215 | nfqnl_build_packet_message(struct nfqnl_instance *queue, | |
5863702a ED |
216 | struct nf_queue_entry *entry, |
217 | __be32 **packet_id_ptr) | |
7af4cc3f | 218 | { |
27a884dc | 219 | sk_buff_data_t old_tail; |
7af4cc3f HW |
220 | size_t size; |
221 | size_t data_len = 0; | |
222 | struct sk_buff *skb; | |
5863702a ED |
223 | struct nlattr *nla; |
224 | struct nfqnl_msg_packet_hdr *pmsg; | |
7af4cc3f HW |
225 | struct nlmsghdr *nlh; |
226 | struct nfgenmsg *nfmsg; | |
3e4ead4f JJ |
227 | struct sk_buff *entskb = entry->skb; |
228 | struct net_device *indev; | |
229 | struct net_device *outdev; | |
7af4cc3f | 230 | |
cabaa9bf | 231 | size = NLMSG_SPACE(sizeof(struct nfgenmsg)) |
df6fb868 PM |
232 | + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr)) |
233 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ | |
234 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ | |
fbcd923c | 235 | #ifdef CONFIG_BRIDGE_NETFILTER |
df6fb868 PM |
236 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ |
237 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ | |
fbcd923c | 238 | #endif |
df6fb868 PM |
239 | + nla_total_size(sizeof(u_int32_t)) /* mark */ |
240 | + nla_total_size(sizeof(struct nfqnl_msg_packet_hw)) | |
241 | + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp)); | |
7af4cc3f | 242 | |
02f014d8 | 243 | outdev = entry->outdev; |
3e4ead4f | 244 | |
c463ac97 | 245 | switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) { |
7af4cc3f HW |
246 | case NFQNL_COPY_META: |
247 | case NFQNL_COPY_NONE: | |
7af4cc3f | 248 | break; |
601e68e1 | 249 | |
7af4cc3f | 250 | case NFQNL_COPY_PACKET: |
e9f13cab | 251 | if (entskb->ip_summed == CHECKSUM_PARTIAL && |
c463ac97 | 252 | skb_checksum_help(entskb)) |
e7dfb09a | 253 | return NULL; |
c463ac97 ED |
254 | |
255 | data_len = ACCESS_ONCE(queue->copy_range); | |
256 | if (data_len == 0 || data_len > entskb->len) | |
3e4ead4f | 257 | data_len = entskb->len; |
601e68e1 | 258 | |
df6fb868 | 259 | size += nla_total_size(data_len); |
7af4cc3f | 260 | break; |
7af4cc3f HW |
261 | } |
262 | ||
7af4cc3f HW |
263 | |
264 | skb = alloc_skb(size, GFP_ATOMIC); | |
265 | if (!skb) | |
266 | goto nlmsg_failure; | |
601e68e1 | 267 | |
27a884dc | 268 | old_tail = skb->tail; |
601e68e1 | 269 | nlh = NLMSG_PUT(skb, 0, 0, |
7af4cc3f HW |
270 | NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET, |
271 | sizeof(struct nfgenmsg)); | |
272 | nfmsg = NLMSG_DATA(nlh); | |
02f014d8 | 273 | nfmsg->nfgen_family = entry->pf; |
7af4cc3f HW |
274 | nfmsg->version = NFNETLINK_V0; |
275 | nfmsg->res_id = htons(queue->queue_num); | |
276 | ||
5863702a ED |
277 | nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg)); |
278 | pmsg = nla_data(nla); | |
279 | pmsg->hw_protocol = entskb->protocol; | |
280 | pmsg->hook = entry->hook; | |
281 | *packet_id_ptr = &pmsg->packet_id; | |
7af4cc3f | 282 | |
02f014d8 | 283 | indev = entry->indev; |
3e4ead4f | 284 | if (indev) { |
fbcd923c | 285 | #ifndef CONFIG_BRIDGE_NETFILTER |
ea3a66ff | 286 | NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex)); |
fbcd923c | 287 | #else |
02f014d8 | 288 | if (entry->pf == PF_BRIDGE) { |
fbcd923c | 289 | /* Case 1: indev is physical input device, we need to |
601e68e1 | 290 | * look for bridge group (when called from |
fbcd923c | 291 | * netfilter_bridge) */ |
ea3a66ff PM |
292 | NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSINDEV, |
293 | htonl(indev->ifindex)); | |
fbcd923c | 294 | /* this is the bridge group "brX" */ |
f350a0a8 | 295 | /* rcu_read_lock()ed by __nf_queue */ |
ea3a66ff | 296 | NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV, |
f350a0a8 | 297 | htonl(br_port_get_rcu(indev)->br->dev->ifindex)); |
fbcd923c HW |
298 | } else { |
299 | /* Case 2: indev is bridge group, we need to look for | |
300 | * physical device (when called from ipv4) */ | |
ea3a66ff PM |
301 | NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV, |
302 | htonl(indev->ifindex)); | |
303 | if (entskb->nf_bridge && entskb->nf_bridge->physindev) | |
304 | NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSINDEV, | |
305 | htonl(entskb->nf_bridge->physindev->ifindex)); | |
fbcd923c HW |
306 | } |
307 | #endif | |
7af4cc3f HW |
308 | } |
309 | ||
3e4ead4f | 310 | if (outdev) { |
fbcd923c | 311 | #ifndef CONFIG_BRIDGE_NETFILTER |
ea3a66ff | 312 | NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)); |
fbcd923c | 313 | #else |
02f014d8 | 314 | if (entry->pf == PF_BRIDGE) { |
fbcd923c | 315 | /* Case 1: outdev is physical output device, we need to |
601e68e1 | 316 | * look for bridge group (when called from |
fbcd923c | 317 | * netfilter_bridge) */ |
ea3a66ff PM |
318 | NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSOUTDEV, |
319 | htonl(outdev->ifindex)); | |
fbcd923c | 320 | /* this is the bridge group "brX" */ |
f350a0a8 | 321 | /* rcu_read_lock()ed by __nf_queue */ |
ea3a66ff | 322 | NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV, |
f350a0a8 | 323 | htonl(br_port_get_rcu(outdev)->br->dev->ifindex)); |
fbcd923c HW |
324 | } else { |
325 | /* Case 2: outdev is bridge group, we need to look for | |
326 | * physical output device (when called from ipv4) */ | |
ea3a66ff PM |
327 | NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV, |
328 | htonl(outdev->ifindex)); | |
329 | if (entskb->nf_bridge && entskb->nf_bridge->physoutdev) | |
330 | NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSOUTDEV, | |
331 | htonl(entskb->nf_bridge->physoutdev->ifindex)); | |
fbcd923c HW |
332 | } |
333 | #endif | |
7af4cc3f HW |
334 | } |
335 | ||
ea3a66ff PM |
336 | if (entskb->mark) |
337 | NLA_PUT_BE32(skb, NFQA_MARK, htonl(entskb->mark)); | |
7af4cc3f | 338 | |
b95cce35 | 339 | if (indev && entskb->dev) { |
7af4cc3f | 340 | struct nfqnl_msg_packet_hw phw; |
b95cce35 SH |
341 | int len = dev_parse_header(entskb, phw.hw_addr); |
342 | if (len) { | |
343 | phw.hw_addrlen = htons(len); | |
df6fb868 | 344 | NLA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw); |
b95cce35 | 345 | } |
7af4cc3f HW |
346 | } |
347 | ||
b7aa0bf7 | 348 | if (entskb->tstamp.tv64) { |
7af4cc3f | 349 | struct nfqnl_msg_packet_timestamp ts; |
b7aa0bf7 ED |
350 | struct timeval tv = ktime_to_timeval(entskb->tstamp); |
351 | ts.sec = cpu_to_be64(tv.tv_sec); | |
352 | ts.usec = cpu_to_be64(tv.tv_usec); | |
7af4cc3f | 353 | |
df6fb868 | 354 | NLA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts); |
7af4cc3f HW |
355 | } |
356 | ||
357 | if (data_len) { | |
df6fb868 | 358 | struct nlattr *nla; |
ca7c48ca | 359 | int sz = nla_attr_size(data_len); |
7af4cc3f | 360 | |
df6fb868 | 361 | if (skb_tailroom(skb) < nla_total_size(data_len)) { |
7af4cc3f HW |
362 | printk(KERN_WARNING "nf_queue: no tailroom!\n"); |
363 | goto nlmsg_failure; | |
364 | } | |
365 | ||
df6fb868 PM |
366 | nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len)); |
367 | nla->nla_type = NFQA_PAYLOAD; | |
ca7c48ca | 368 | nla->nla_len = sz; |
7af4cc3f | 369 | |
df6fb868 | 370 | if (skb_copy_bits(entskb, 0, nla_data(nla), data_len)) |
7af4cc3f HW |
371 | BUG(); |
372 | } | |
601e68e1 | 373 | |
7af4cc3f HW |
374 | nlh->nlmsg_len = skb->tail - old_tail; |
375 | return skb; | |
376 | ||
377 | nlmsg_failure: | |
df6fb868 | 378 | nla_put_failure: |
7af4cc3f HW |
379 | if (skb) |
380 | kfree_skb(skb); | |
7af4cc3f HW |
381 | if (net_ratelimit()) |
382 | printk(KERN_ERR "nf_queue: error creating packet message\n"); | |
383 | return NULL; | |
384 | } | |
385 | ||
386 | static int | |
02f014d8 | 387 | nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum) |
7af4cc3f | 388 | { |
7af4cc3f HW |
389 | struct sk_buff *nskb; |
390 | struct nfqnl_instance *queue; | |
f1585086 | 391 | int err = -ENOBUFS; |
5863702a | 392 | __be32 *packet_id_ptr; |
7af4cc3f | 393 | |
9872bec7 PM |
394 | /* rcu_read_lock()ed by nf_hook_slow() */ |
395 | queue = instance_lookup(queuenum); | |
f1585086 FW |
396 | if (!queue) { |
397 | err = -ESRCH; | |
0ef0f465 | 398 | goto err_out; |
f1585086 | 399 | } |
7af4cc3f | 400 | |
f1585086 FW |
401 | if (queue->copy_mode == NFQNL_COPY_NONE) { |
402 | err = -EINVAL; | |
0ef0f465 | 403 | goto err_out; |
f1585086 | 404 | } |
7af4cc3f | 405 | |
5863702a | 406 | nskb = nfqnl_build_packet_message(queue, entry, &packet_id_ptr); |
f1585086 FW |
407 | if (nskb == NULL) { |
408 | err = -ENOMEM; | |
0ef0f465 | 409 | goto err_out; |
f1585086 | 410 | } |
7af4cc3f | 411 | spin_lock_bh(&queue->lock); |
601e68e1 | 412 | |
f1585086 FW |
413 | if (!queue->peer_pid) { |
414 | err = -EINVAL; | |
601e68e1 | 415 | goto err_out_free_nskb; |
f1585086 | 416 | } |
7af4cc3f | 417 | if (queue->queue_total >= queue->queue_maxlen) { |
601e68e1 | 418 | queue->queue_dropped++; |
7af4cc3f | 419 | if (net_ratelimit()) |
601e68e1 | 420 | printk(KERN_WARNING "nf_queue: full at %d entries, " |
a5d896ad EL |
421 | "dropping packets(s).\n", |
422 | queue->queue_total); | |
7af4cc3f HW |
423 | goto err_out_free_nskb; |
424 | } | |
5863702a ED |
425 | entry->id = ++queue->id_sequence; |
426 | *packet_id_ptr = htonl(entry->id); | |
7af4cc3f HW |
427 | |
428 | /* nfnetlink_unicast will either free the nskb or add it to a socket */ | |
cd8c20b6 | 429 | err = nfnetlink_unicast(nskb, &init_net, queue->peer_pid, MSG_DONTWAIT); |
0ef0f465 | 430 | if (err < 0) { |
601e68e1 | 431 | queue->queue_user_dropped++; |
7af4cc3f HW |
432 | goto err_out_unlock; |
433 | } | |
434 | ||
435 | __enqueue_entry(queue, entry); | |
436 | ||
437 | spin_unlock_bh(&queue->lock); | |
0ef0f465 | 438 | return 0; |
7af4cc3f HW |
439 | |
440 | err_out_free_nskb: | |
601e68e1 | 441 | kfree_skb(nskb); |
7af4cc3f HW |
442 | err_out_unlock: |
443 | spin_unlock_bh(&queue->lock); | |
0ef0f465 | 444 | err_out: |
f1585086 | 445 | return err; |
7af4cc3f HW |
446 | } |
447 | ||
448 | static int | |
02f014d8 | 449 | nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e) |
7af4cc3f | 450 | { |
e2b58a67 | 451 | struct sk_buff *nskb; |
7af4cc3f HW |
452 | int diff; |
453 | ||
454 | diff = data_len - e->skb->len; | |
d8a585d7 PM |
455 | if (diff < 0) { |
456 | if (pskb_trim(e->skb, data_len)) | |
457 | return -ENOMEM; | |
458 | } else if (diff > 0) { | |
7af4cc3f HW |
459 | if (data_len > 0xFFFF) |
460 | return -EINVAL; | |
461 | if (diff > skb_tailroom(e->skb)) { | |
9a732ed6 AE |
462 | nskb = skb_copy_expand(e->skb, skb_headroom(e->skb), |
463 | diff, GFP_ATOMIC); | |
e2b58a67 | 464 | if (!nskb) { |
1158ba27 | 465 | printk(KERN_WARNING "nf_queue: OOM " |
7af4cc3f | 466 | "in mangle, dropping packet\n"); |
e2b58a67 | 467 | return -ENOMEM; |
7af4cc3f | 468 | } |
e2b58a67 PM |
469 | kfree_skb(e->skb); |
470 | e->skb = nskb; | |
7af4cc3f HW |
471 | } |
472 | skb_put(e->skb, diff); | |
473 | } | |
37d41879 | 474 | if (!skb_make_writable(e->skb, data_len)) |
7af4cc3f | 475 | return -ENOMEM; |
27d7ff46 | 476 | skb_copy_to_linear_data(e->skb, data, data_len); |
e7dfb09a | 477 | e->skb->ip_summed = CHECKSUM_NONE; |
7af4cc3f HW |
478 | return 0; |
479 | } | |
480 | ||
7af4cc3f HW |
481 | static int |
482 | nfqnl_set_mode(struct nfqnl_instance *queue, | |
483 | unsigned char mode, unsigned int range) | |
484 | { | |
c5de0dfd | 485 | int status = 0; |
7af4cc3f HW |
486 | |
487 | spin_lock_bh(&queue->lock); | |
c5de0dfd PM |
488 | switch (mode) { |
489 | case NFQNL_COPY_NONE: | |
490 | case NFQNL_COPY_META: | |
491 | queue->copy_mode = mode; | |
492 | queue->copy_range = 0; | |
493 | break; | |
494 | ||
495 | case NFQNL_COPY_PACKET: | |
496 | queue->copy_mode = mode; | |
497 | /* we're using struct nlattr which has 16bit nla_len */ | |
498 | if (range > 0xffff) | |
499 | queue->copy_range = 0xffff; | |
500 | else | |
501 | queue->copy_range = range; | |
502 | break; | |
503 | ||
504 | default: | |
505 | status = -EINVAL; | |
506 | ||
507 | } | |
7af4cc3f HW |
508 | spin_unlock_bh(&queue->lock); |
509 | ||
510 | return status; | |
511 | } | |
512 | ||
513 | static int | |
02f014d8 | 514 | dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex) |
7af4cc3f | 515 | { |
02f014d8 PM |
516 | if (entry->indev) |
517 | if (entry->indev->ifindex == ifindex) | |
7af4cc3f | 518 | return 1; |
02f014d8 PM |
519 | if (entry->outdev) |
520 | if (entry->outdev->ifindex == ifindex) | |
7af4cc3f | 521 | return 1; |
ef47c6a7 PM |
522 | #ifdef CONFIG_BRIDGE_NETFILTER |
523 | if (entry->skb->nf_bridge) { | |
524 | if (entry->skb->nf_bridge->physindev && | |
525 | entry->skb->nf_bridge->physindev->ifindex == ifindex) | |
526 | return 1; | |
527 | if (entry->skb->nf_bridge->physoutdev && | |
528 | entry->skb->nf_bridge->physoutdev->ifindex == ifindex) | |
529 | return 1; | |
530 | } | |
531 | #endif | |
7af4cc3f HW |
532 | return 0; |
533 | } | |
534 | ||
535 | /* drop all packets with either indev or outdev == ifindex from all queue | |
536 | * instances */ | |
537 | static void | |
538 | nfqnl_dev_drop(int ifindex) | |
539 | { | |
540 | int i; | |
601e68e1 | 541 | |
9872bec7 | 542 | rcu_read_lock(); |
7af4cc3f | 543 | |
9872bec7 | 544 | for (i = 0; i < INSTANCE_BUCKETS; i++) { |
7af4cc3f HW |
545 | struct hlist_node *tmp; |
546 | struct nfqnl_instance *inst; | |
547 | struct hlist_head *head = &instance_table[i]; | |
548 | ||
9872bec7 | 549 | hlist_for_each_entry_rcu(inst, tmp, head, hlist) |
b43d8d85 | 550 | nfqnl_flush(inst, dev_cmp, ifindex); |
7af4cc3f HW |
551 | } |
552 | ||
9872bec7 | 553 | rcu_read_unlock(); |
7af4cc3f HW |
554 | } |
555 | ||
556 | #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0) | |
557 | ||
558 | static int | |
559 | nfqnl_rcv_dev_event(struct notifier_block *this, | |
560 | unsigned long event, void *ptr) | |
561 | { | |
562 | struct net_device *dev = ptr; | |
563 | ||
721499e8 | 564 | if (!net_eq(dev_net(dev), &init_net)) |
e9dc8653 EB |
565 | return NOTIFY_DONE; |
566 | ||
7af4cc3f HW |
567 | /* Drop any packets associated with the downed device */ |
568 | if (event == NETDEV_DOWN) | |
569 | nfqnl_dev_drop(dev->ifindex); | |
570 | return NOTIFY_DONE; | |
571 | } | |
572 | ||
573 | static struct notifier_block nfqnl_dev_notifier = { | |
574 | .notifier_call = nfqnl_rcv_dev_event, | |
575 | }; | |
576 | ||
577 | static int | |
578 | nfqnl_rcv_nl_event(struct notifier_block *this, | |
579 | unsigned long event, void *ptr) | |
580 | { | |
581 | struct netlink_notify *n = ptr; | |
582 | ||
dee5817e | 583 | if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) { |
7af4cc3f HW |
584 | int i; |
585 | ||
586 | /* destroy all instances for this pid */ | |
9872bec7 PM |
587 | spin_lock(&instances_lock); |
588 | for (i = 0; i < INSTANCE_BUCKETS; i++) { | |
7af4cc3f HW |
589 | struct hlist_node *tmp, *t2; |
590 | struct nfqnl_instance *inst; | |
591 | struct hlist_head *head = &instance_table[i]; | |
592 | ||
593 | hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) { | |
b4b51029 EB |
594 | if ((n->net == &init_net) && |
595 | (n->pid == inst->peer_pid)) | |
7af4cc3f HW |
596 | __instance_destroy(inst); |
597 | } | |
598 | } | |
9872bec7 | 599 | spin_unlock(&instances_lock); |
7af4cc3f HW |
600 | } |
601 | return NOTIFY_DONE; | |
602 | } | |
603 | ||
604 | static struct notifier_block nfqnl_rtnl_notifier = { | |
605 | .notifier_call = nfqnl_rcv_nl_event, | |
606 | }; | |
607 | ||
5bf75853 PM |
608 | static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = { |
609 | [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) }, | |
610 | [NFQA_MARK] = { .type = NLA_U32 }, | |
611 | [NFQA_PAYLOAD] = { .type = NLA_UNSPEC }, | |
838ab636 HW |
612 | }; |
613 | ||
7af4cc3f HW |
614 | static int |
615 | nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb, | |
39938324 PM |
616 | const struct nlmsghdr *nlh, |
617 | const struct nlattr * const nfqa[]) | |
7af4cc3f HW |
618 | { |
619 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); | |
620 | u_int16_t queue_num = ntohs(nfmsg->res_id); | |
621 | ||
622 | struct nfqnl_msg_verdict_hdr *vhdr; | |
623 | struct nfqnl_instance *queue; | |
624 | unsigned int verdict; | |
02f014d8 | 625 | struct nf_queue_entry *entry; |
7af4cc3f | 626 | |
9872bec7 | 627 | queue = instance_lookup(queue_num); |
84a797dd ED |
628 | if (!queue) |
629 | return -ENODEV; | |
7af4cc3f | 630 | |
84a797dd ED |
631 | if (queue->peer_pid != NETLINK_CB(skb).pid) |
632 | return -EPERM; | |
7af4cc3f | 633 | |
84a797dd ED |
634 | if (!nfqa[NFQA_VERDICT_HDR]) |
635 | return -EINVAL; | |
7af4cc3f | 636 | |
df6fb868 | 637 | vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]); |
7af4cc3f HW |
638 | verdict = ntohl(vhdr->verdict); |
639 | ||
84a797dd ED |
640 | if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) |
641 | return -EINVAL; | |
7af4cc3f | 642 | |
b43d8d85 | 643 | entry = find_dequeue_entry(queue, ntohl(vhdr->id)); |
84a797dd ED |
644 | if (entry == NULL) |
645 | return -ENOENT; | |
7af4cc3f | 646 | |
df6fb868 PM |
647 | if (nfqa[NFQA_PAYLOAD]) { |
648 | if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]), | |
649 | nla_len(nfqa[NFQA_PAYLOAD]), entry) < 0) | |
7af4cc3f HW |
650 | verdict = NF_DROP; |
651 | } | |
652 | ||
df6fb868 | 653 | if (nfqa[NFQA_MARK]) |
ea3a66ff | 654 | entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK])); |
601e68e1 | 655 | |
4b3d15ef | 656 | nf_reinject(entry, verdict); |
7af4cc3f HW |
657 | return 0; |
658 | } | |
659 | ||
660 | static int | |
661 | nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb, | |
39938324 PM |
662 | const struct nlmsghdr *nlh, |
663 | const struct nlattr * const nfqa[]) | |
7af4cc3f HW |
664 | { |
665 | return -ENOTSUPP; | |
666 | } | |
667 | ||
5bf75853 PM |
668 | static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = { |
669 | [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) }, | |
670 | [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) }, | |
838ab636 HW |
671 | }; |
672 | ||
e3ac5298 | 673 | static const struct nf_queue_handler nfqh = { |
bbd86b9f HW |
674 | .name = "nf_queue", |
675 | .outfn = &nfqnl_enqueue_packet, | |
676 | }; | |
677 | ||
7af4cc3f HW |
678 | static int |
679 | nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb, | |
39938324 PM |
680 | const struct nlmsghdr *nlh, |
681 | const struct nlattr * const nfqa[]) | |
7af4cc3f HW |
682 | { |
683 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); | |
684 | u_int16_t queue_num = ntohs(nfmsg->res_id); | |
685 | struct nfqnl_instance *queue; | |
9872bec7 | 686 | struct nfqnl_msg_config_cmd *cmd = NULL; |
838ab636 | 687 | int ret = 0; |
7af4cc3f | 688 | |
9872bec7 PM |
689 | if (nfqa[NFQA_CFG_CMD]) { |
690 | cmd = nla_data(nfqa[NFQA_CFG_CMD]); | |
691 | ||
692 | /* Commands without queue context - might sleep */ | |
693 | switch (cmd->command) { | |
694 | case NFQNL_CFG_CMD_PF_BIND: | |
914afea8 PM |
695 | return nf_register_queue_handler(ntohs(cmd->pf), |
696 | &nfqh); | |
9872bec7 | 697 | case NFQNL_CFG_CMD_PF_UNBIND: |
914afea8 PM |
698 | return nf_unregister_queue_handler(ntohs(cmd->pf), |
699 | &nfqh); | |
9872bec7 | 700 | } |
9872bec7 PM |
701 | } |
702 | ||
703 | rcu_read_lock(); | |
704 | queue = instance_lookup(queue_num); | |
a3c8e7fd PM |
705 | if (queue && queue->peer_pid != NETLINK_CB(skb).pid) { |
706 | ret = -EPERM; | |
9872bec7 | 707 | goto err_out_unlock; |
a3c8e7fd PM |
708 | } |
709 | ||
9872bec7 | 710 | if (cmd != NULL) { |
7af4cc3f HW |
711 | switch (cmd->command) { |
712 | case NFQNL_CFG_CMD_BIND: | |
9872bec7 PM |
713 | if (queue) { |
714 | ret = -EBUSY; | |
715 | goto err_out_unlock; | |
716 | } | |
7af4cc3f | 717 | queue = instance_create(queue_num, NETLINK_CB(skb).pid); |
baab2ce7 PM |
718 | if (IS_ERR(queue)) { |
719 | ret = PTR_ERR(queue); | |
9872bec7 PM |
720 | goto err_out_unlock; |
721 | } | |
7af4cc3f HW |
722 | break; |
723 | case NFQNL_CFG_CMD_UNBIND: | |
9872bec7 PM |
724 | if (!queue) { |
725 | ret = -ENODEV; | |
726 | goto err_out_unlock; | |
727 | } | |
7af4cc3f HW |
728 | instance_destroy(queue); |
729 | break; | |
730 | case NFQNL_CFG_CMD_PF_BIND: | |
7af4cc3f | 731 | case NFQNL_CFG_CMD_PF_UNBIND: |
7af4cc3f HW |
732 | break; |
733 | default: | |
cd21f0ac | 734 | ret = -ENOTSUPP; |
838ab636 | 735 | break; |
7af4cc3f | 736 | } |
7af4cc3f HW |
737 | } |
738 | ||
df6fb868 | 739 | if (nfqa[NFQA_CFG_PARAMS]) { |
7af4cc3f | 740 | struct nfqnl_msg_config_params *params; |
7af4cc3f | 741 | |
406dbfc9 | 742 | if (!queue) { |
a3c8e7fd | 743 | ret = -ENODEV; |
9872bec7 | 744 | goto err_out_unlock; |
406dbfc9 | 745 | } |
df6fb868 | 746 | params = nla_data(nfqa[NFQA_CFG_PARAMS]); |
7af4cc3f HW |
747 | nfqnl_set_mode(queue, params->copy_mode, |
748 | ntohl(params->copy_range)); | |
749 | } | |
750 | ||
df6fb868 | 751 | if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) { |
829e17a1 | 752 | __be32 *queue_maxlen; |
a3c8e7fd PM |
753 | |
754 | if (!queue) { | |
755 | ret = -ENODEV; | |
9872bec7 | 756 | goto err_out_unlock; |
a3c8e7fd | 757 | } |
df6fb868 | 758 | queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]); |
829e17a1 EL |
759 | spin_lock_bh(&queue->lock); |
760 | queue->queue_maxlen = ntohl(*queue_maxlen); | |
761 | spin_unlock_bh(&queue->lock); | |
762 | } | |
763 | ||
9872bec7 PM |
764 | err_out_unlock: |
765 | rcu_read_unlock(); | |
838ab636 | 766 | return ret; |
7af4cc3f HW |
767 | } |
768 | ||
7c8d4cb4 | 769 | static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = { |
84a797dd | 770 | [NFQNL_MSG_PACKET] = { .call_rcu = nfqnl_recv_unsupp, |
37d2e7a2 | 771 | .attr_count = NFQA_MAX, }, |
84a797dd | 772 | [NFQNL_MSG_VERDICT] = { .call_rcu = nfqnl_recv_verdict, |
5bf75853 PM |
773 | .attr_count = NFQA_MAX, |
774 | .policy = nfqa_verdict_policy }, | |
7af4cc3f | 775 | [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config, |
5bf75853 PM |
776 | .attr_count = NFQA_CFG_MAX, |
777 | .policy = nfqa_cfg_policy }, | |
7af4cc3f HW |
778 | }; |
779 | ||
7c8d4cb4 | 780 | static const struct nfnetlink_subsystem nfqnl_subsys = { |
7af4cc3f HW |
781 | .name = "nf_queue", |
782 | .subsys_id = NFNL_SUBSYS_QUEUE, | |
783 | .cb_count = NFQNL_MSG_MAX, | |
7af4cc3f HW |
784 | .cb = nfqnl_cb, |
785 | }; | |
786 | ||
838ab636 HW |
787 | #ifdef CONFIG_PROC_FS |
788 | struct iter_state { | |
789 | unsigned int bucket; | |
790 | }; | |
791 | ||
792 | static struct hlist_node *get_first(struct seq_file *seq) | |
793 | { | |
794 | struct iter_state *st = seq->private; | |
795 | ||
796 | if (!st) | |
797 | return NULL; | |
798 | ||
799 | for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) { | |
800 | if (!hlist_empty(&instance_table[st->bucket])) | |
801 | return instance_table[st->bucket].first; | |
802 | } | |
803 | return NULL; | |
804 | } | |
805 | ||
806 | static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h) | |
807 | { | |
808 | struct iter_state *st = seq->private; | |
809 | ||
810 | h = h->next; | |
811 | while (!h) { | |
812 | if (++st->bucket >= INSTANCE_BUCKETS) | |
813 | return NULL; | |
814 | ||
815 | h = instance_table[st->bucket].first; | |
816 | } | |
817 | return h; | |
818 | } | |
819 | ||
820 | static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos) | |
821 | { | |
822 | struct hlist_node *head; | |
823 | head = get_first(seq); | |
824 | ||
825 | if (head) | |
826 | while (pos && (head = get_next(seq, head))) | |
827 | pos--; | |
828 | return pos ? NULL : head; | |
829 | } | |
830 | ||
831 | static void *seq_start(struct seq_file *seq, loff_t *pos) | |
ca7c48ca | 832 | __acquires(instances_lock) |
838ab636 | 833 | { |
9872bec7 | 834 | spin_lock(&instances_lock); |
838ab636 HW |
835 | return get_idx(seq, *pos); |
836 | } | |
837 | ||
838 | static void *seq_next(struct seq_file *s, void *v, loff_t *pos) | |
839 | { | |
840 | (*pos)++; | |
841 | return get_next(s, v); | |
842 | } | |
843 | ||
844 | static void seq_stop(struct seq_file *s, void *v) | |
ca7c48ca | 845 | __releases(instances_lock) |
838ab636 | 846 | { |
9872bec7 | 847 | spin_unlock(&instances_lock); |
838ab636 HW |
848 | } |
849 | ||
850 | static int seq_show(struct seq_file *s, void *v) | |
851 | { | |
852 | const struct nfqnl_instance *inst = v; | |
853 | ||
854 | return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n", | |
855 | inst->queue_num, | |
856 | inst->peer_pid, inst->queue_total, | |
857 | inst->copy_mode, inst->copy_range, | |
858 | inst->queue_dropped, inst->queue_user_dropped, | |
5863702a | 859 | inst->id_sequence, 1); |
838ab636 HW |
860 | } |
861 | ||
56b3d975 | 862 | static const struct seq_operations nfqnl_seq_ops = { |
838ab636 HW |
863 | .start = seq_start, |
864 | .next = seq_next, | |
865 | .stop = seq_stop, | |
866 | .show = seq_show, | |
867 | }; | |
868 | ||
869 | static int nfqnl_open(struct inode *inode, struct file *file) | |
870 | { | |
e2da5913 PE |
871 | return seq_open_private(file, &nfqnl_seq_ops, |
872 | sizeof(struct iter_state)); | |
838ab636 HW |
873 | } |
874 | ||
da7071d7 | 875 | static const struct file_operations nfqnl_file_ops = { |
838ab636 HW |
876 | .owner = THIS_MODULE, |
877 | .open = nfqnl_open, | |
878 | .read = seq_read, | |
879 | .llseek = seq_lseek, | |
880 | .release = seq_release_private, | |
881 | }; | |
882 | ||
883 | #endif /* PROC_FS */ | |
884 | ||
32292a7f | 885 | static int __init nfnetlink_queue_init(void) |
7af4cc3f | 886 | { |
838ab636 | 887 | int i, status = -ENOMEM; |
601e68e1 | 888 | |
838ab636 HW |
889 | for (i = 0; i < INSTANCE_BUCKETS; i++) |
890 | INIT_HLIST_HEAD(&instance_table[i]); | |
891 | ||
7af4cc3f HW |
892 | netlink_register_notifier(&nfqnl_rtnl_notifier); |
893 | status = nfnetlink_subsys_register(&nfqnl_subsys); | |
894 | if (status < 0) { | |
895 | printk(KERN_ERR "nf_queue: failed to create netlink socket\n"); | |
896 | goto cleanup_netlink_notifier; | |
897 | } | |
898 | ||
838ab636 | 899 | #ifdef CONFIG_PROC_FS |
8eeee8b1 DL |
900 | if (!proc_create("nfnetlink_queue", 0440, |
901 | proc_net_netfilter, &nfqnl_file_ops)) | |
838ab636 | 902 | goto cleanup_subsys; |
838ab636 HW |
903 | #endif |
904 | ||
7af4cc3f HW |
905 | register_netdevice_notifier(&nfqnl_dev_notifier); |
906 | return status; | |
907 | ||
838ab636 HW |
908 | #ifdef CONFIG_PROC_FS |
909 | cleanup_subsys: | |
7af4cc3f | 910 | nfnetlink_subsys_unregister(&nfqnl_subsys); |
32292a7f | 911 | #endif |
7af4cc3f HW |
912 | cleanup_netlink_notifier: |
913 | netlink_unregister_notifier(&nfqnl_rtnl_notifier); | |
914 | return status; | |
915 | } | |
916 | ||
65b4b4e8 | 917 | static void __exit nfnetlink_queue_fini(void) |
7af4cc3f | 918 | { |
32292a7f PM |
919 | nf_unregister_queue_handlers(&nfqh); |
920 | unregister_netdevice_notifier(&nfqnl_dev_notifier); | |
921 | #ifdef CONFIG_PROC_FS | |
922 | remove_proc_entry("nfnetlink_queue", proc_net_netfilter); | |
923 | #endif | |
924 | nfnetlink_subsys_unregister(&nfqnl_subsys); | |
925 | netlink_unregister_notifier(&nfqnl_rtnl_notifier); | |
67137f3c JDB |
926 | |
927 | rcu_barrier(); /* Wait for completion of call_rcu()'s */ | |
7af4cc3f HW |
928 | } |
929 | ||
930 | MODULE_DESCRIPTION("netfilter packet queue handler"); | |
931 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); | |
932 | MODULE_LICENSE("GPL"); | |
933 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE); | |
934 | ||
65b4b4e8 AM |
935 | module_init(nfnetlink_queue_init); |
936 | module_exit(nfnetlink_queue_fini); |