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