blob: 37b7655df910896735eb42f032878fda0e2ad31d [file] [log] [blame]
Harald Welte7af4cc32005-08-09 19:44:15 -07001/*
2 * This is a module which is used for queueing packets and communicating with
3 * userspace via nfetlink.
4 *
5 * (C) 2005 by Harald Welte <laforge@netfilter.org>
6 *
7 * Based on the old ipv4-only ip_queue.c:
8 * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
9 * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 */
16#include <linux/module.h>
17#include <linux/skbuff.h>
18#include <linux/init.h>
19#include <linux/spinlock.h>
20#include <linux/notifier.h>
21#include <linux/netdevice.h>
22#include <linux/netfilter.h>
Harald Welte838ab632005-08-09 19:50:45 -070023#include <linux/proc_fs.h>
Harald Welte7af4cc32005-08-09 19:44:15 -070024#include <linux/netfilter_ipv4.h>
25#include <linux/netfilter_ipv6.h>
26#include <linux/netfilter/nfnetlink.h>
27#include <linux/netfilter/nfnetlink_queue.h>
28#include <linux/list.h>
29#include <net/sock.h>
Patrick McHardyc01cd422007-12-05 01:24:48 -080030#include <net/netfilter/nf_queue.h>
Harald Welte7af4cc32005-08-09 19:44:15 -070031
32#include <asm/atomic.h>
33
Harald Weltefbcd9232005-08-09 20:22:10 -070034#ifdef CONFIG_BRIDGE_NETFILTER
35#include "../bridge/br_private.h"
36#endif
37
Harald Welte7af4cc32005-08-09 19:44:15 -070038#define NFQNL_QMAX_DEFAULT 1024
39
40#if 0
41#define QDEBUG(x, args ...) printk(KERN_DEBUG "%s(%d):%s(): " x, \
42 __FILE__, __LINE__, __FUNCTION__, \
43 ## args)
44#else
45#define QDEBUG(x, ...)
46#endif
47
Harald Welte7af4cc32005-08-09 19:44:15 -070048struct nfqnl_instance {
49 struct hlist_node hlist; /* global list of queues */
Patrick McHardy9872bec2007-12-05 01:28:50 -080050 struct rcu_head rcu;
Harald Welte7af4cc32005-08-09 19:44:15 -070051
52 int peer_pid;
53 unsigned int queue_maxlen;
54 unsigned int copy_range;
55 unsigned int queue_total;
56 unsigned int queue_dropped;
57 unsigned int queue_user_dropped;
58
Patrick McHardye48b9b22007-12-05 01:28:10 -080059 unsigned int id_sequence; /* 'sequence' of pkt ids */
Harald Welte7af4cc32005-08-09 19:44:15 -070060
61 u_int16_t queue_num; /* number of this queue */
62 u_int8_t copy_mode;
63
64 spinlock_t lock;
65
66 struct list_head queue_list; /* packets in queue */
67};
68
Patrick McHardy02f014d2007-12-05 01:26:33 -080069typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
Harald Welte7af4cc32005-08-09 19:44:15 -070070
Patrick McHardy9872bec2007-12-05 01:28:50 -080071static DEFINE_SPINLOCK(instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -070072
Harald Welte7af4cc32005-08-09 19:44:15 -070073#define INSTANCE_BUCKETS 16
74static struct hlist_head instance_table[INSTANCE_BUCKETS];
75
76static inline u_int8_t instance_hashfn(u_int16_t queue_num)
77{
78 return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
79}
80
81static struct nfqnl_instance *
Patrick McHardy9872bec2007-12-05 01:28:50 -080082instance_lookup(u_int16_t queue_num)
Harald Welte7af4cc32005-08-09 19:44:15 -070083{
84 struct hlist_head *head;
85 struct hlist_node *pos;
86 struct nfqnl_instance *inst;
87
88 head = &instance_table[instance_hashfn(queue_num)];
Patrick McHardy9872bec2007-12-05 01:28:50 -080089 hlist_for_each_entry_rcu(inst, pos, head, hlist) {
Harald Welte7af4cc32005-08-09 19:44:15 -070090 if (inst->queue_num == queue_num)
91 return inst;
92 }
93 return NULL;
94}
95
96static struct nfqnl_instance *
Harald Welte7af4cc32005-08-09 19:44:15 -070097instance_create(u_int16_t queue_num, int pid)
98{
99 struct nfqnl_instance *inst;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800100 unsigned int h;
Harald Welte7af4cc32005-08-09 19:44:15 -0700101
102 QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num, pid);
103
Patrick McHardy9872bec2007-12-05 01:28:50 -0800104 spin_lock(&instances_lock);
105 if (instance_lookup(queue_num)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700106 inst = NULL;
107 QDEBUG("aborting, instance already exists\n");
108 goto out_unlock;
109 }
110
Harald Welte10dfdc62005-11-03 19:20:07 +0100111 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
Harald Welte7af4cc32005-08-09 19:44:15 -0700112 if (!inst)
113 goto out_unlock;
114
Harald Welte7af4cc32005-08-09 19:44:15 -0700115 inst->queue_num = queue_num;
116 inst->peer_pid = pid;
117 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
118 inst->copy_range = 0xfffff;
119 inst->copy_mode = NFQNL_COPY_NONE;
YOSHIFUJI Hideaki181a46a2006-01-04 13:56:54 -0800120 spin_lock_init(&inst->lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700121 INIT_LIST_HEAD(&inst->queue_list);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800122 INIT_RCU_HEAD(&inst->rcu);
Harald Welte7af4cc32005-08-09 19:44:15 -0700123
124 if (!try_module_get(THIS_MODULE))
125 goto out_free;
126
Patrick McHardy9872bec2007-12-05 01:28:50 -0800127 h = instance_hashfn(queue_num);
128 hlist_add_head_rcu(&inst->hlist, &instance_table[h]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700129
Patrick McHardy9872bec2007-12-05 01:28:50 -0800130 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700131
132 QDEBUG("successfully created new instance\n");
133
134 return inst;
135
136out_free:
137 kfree(inst);
138out_unlock:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800139 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700140 return NULL;
141}
142
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800143static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
144 unsigned long data);
Harald Welte7af4cc32005-08-09 19:44:15 -0700145
146static void
Patrick McHardy9872bec2007-12-05 01:28:50 -0800147instance_destroy_rcu(struct rcu_head *head)
Harald Welte7af4cc32005-08-09 19:44:15 -0700148{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800149 struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
150 rcu);
Harald Welte7af4cc32005-08-09 19:44:15 -0700151
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800152 nfqnl_flush(inst, NULL, 0);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800153 kfree(inst);
Harald Welte7af4cc32005-08-09 19:44:15 -0700154 module_put(THIS_MODULE);
155}
156
Patrick McHardy9872bec2007-12-05 01:28:50 -0800157static void
Harald Welte7af4cc32005-08-09 19:44:15 -0700158__instance_destroy(struct nfqnl_instance *inst)
159{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800160 hlist_del_rcu(&inst->hlist);
161 call_rcu(&inst->rcu, instance_destroy_rcu);
Harald Welte7af4cc32005-08-09 19:44:15 -0700162}
163
Patrick McHardy9872bec2007-12-05 01:28:50 -0800164static void
Harald Welte7af4cc32005-08-09 19:44:15 -0700165instance_destroy(struct nfqnl_instance *inst)
166{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800167 spin_lock(&instances_lock);
168 __instance_destroy(inst);
169 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700170}
171
Harald Welte7af4cc32005-08-09 19:44:15 -0700172static inline void
Patrick McHardy02f014d2007-12-05 01:26:33 -0800173__enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
Harald Welte7af4cc32005-08-09 19:44:15 -0700174{
Patrick McHardy0ac41e82007-12-05 01:25:03 -0800175 list_add_tail(&entry->list, &queue->queue_list);
Harald Welte7af4cc32005-08-09 19:44:15 -0700176 queue->queue_total++;
177}
178
Patrick McHardy02f014d2007-12-05 01:26:33 -0800179static struct nf_queue_entry *
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800180find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
Harald Welte7af4cc32005-08-09 19:44:15 -0700181{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800182 struct nf_queue_entry *entry = NULL, *i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800183
Harald Welte7af4cc32005-08-09 19:44:15 -0700184 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800185
186 list_for_each_entry(i, &queue->queue_list, list) {
187 if (i->id == id) {
188 entry = i;
189 break;
190 }
191 }
192
193 if (entry) {
194 list_del(&entry->list);
195 queue->queue_total--;
196 }
197
Harald Welte7af4cc32005-08-09 19:44:15 -0700198 spin_unlock_bh(&queue->lock);
199
200 return entry;
201}
202
203static void
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800204nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
Harald Welte7af4cc32005-08-09 19:44:15 -0700205{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800206 struct nf_queue_entry *entry, *next;
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800207
Harald Welte7af4cc32005-08-09 19:44:15 -0700208 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800209 list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
210 if (!cmpfn || cmpfn(entry, data)) {
211 list_del(&entry->list);
212 queue->queue_total--;
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800213 nf_reinject(entry, NF_DROP);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800214 }
215 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700216 spin_unlock_bh(&queue->lock);
217}
218
219static struct sk_buff *
220nfqnl_build_packet_message(struct nfqnl_instance *queue,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800221 struct nf_queue_entry *entry, int *errp)
Harald Welte7af4cc32005-08-09 19:44:15 -0700222{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700223 sk_buff_data_t old_tail;
Harald Welte7af4cc32005-08-09 19:44:15 -0700224 size_t size;
225 size_t data_len = 0;
226 struct sk_buff *skb;
227 struct nfqnl_msg_packet_hdr pmsg;
228 struct nlmsghdr *nlh;
229 struct nfgenmsg *nfmsg;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800230 struct sk_buff *entskb = entry->skb;
231 struct net_device *indev;
232 struct net_device *outdev;
Al Viro98a4a862006-11-08 00:26:51 -0800233 __be32 tmp_uint;
Harald Welte7af4cc32005-08-09 19:44:15 -0700234
235 QDEBUG("entered\n");
236
Patrick McHardydf6fb862007-09-28 14:37:03 -0700237 size = NLMSG_ALIGN(sizeof(struct nfgenmsg))
238 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
239 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
240 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700241#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700242 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
243 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700244#endif
Patrick McHardydf6fb862007-09-28 14:37:03 -0700245 + nla_total_size(sizeof(u_int32_t)) /* mark */
246 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
247 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
Harald Welte7af4cc32005-08-09 19:44:15 -0700248
Patrick McHardy02f014d2007-12-05 01:26:33 -0800249 outdev = entry->outdev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800250
Harald Welte7af4cc32005-08-09 19:44:15 -0700251 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800252
Harald Welte7af4cc32005-08-09 19:44:15 -0700253 switch (queue->copy_mode) {
254 case NFQNL_COPY_META:
255 case NFQNL_COPY_NONE:
256 data_len = 0;
257 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800258
Harald Welte7af4cc32005-08-09 19:44:15 -0700259 case NFQNL_COPY_PACKET:
Patrick McHardy84fa7932006-08-29 16:44:56 -0700260 if ((entskb->ip_summed == CHECKSUM_PARTIAL ||
261 entskb->ip_summed == CHECKSUM_COMPLETE) &&
262 (*errp = skb_checksum_help(entskb))) {
Patrick McHardye7dfb092005-09-06 15:10:00 -0700263 spin_unlock_bh(&queue->lock);
264 return NULL;
265 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800266 if (queue->copy_range == 0
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800267 || queue->copy_range > entskb->len)
268 data_len = entskb->len;
Harald Welte7af4cc32005-08-09 19:44:15 -0700269 else
270 data_len = queue->copy_range;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800271
Patrick McHardydf6fb862007-09-28 14:37:03 -0700272 size += nla_total_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700273 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800274
Harald Welte7af4cc32005-08-09 19:44:15 -0700275 default:
276 *errp = -EINVAL;
277 spin_unlock_bh(&queue->lock);
278 return NULL;
279 }
280
Patrick McHardye48b9b22007-12-05 01:28:10 -0800281 entry->id = queue->id_sequence++;
282
Harald Welte7af4cc32005-08-09 19:44:15 -0700283 spin_unlock_bh(&queue->lock);
284
285 skb = alloc_skb(size, GFP_ATOMIC);
286 if (!skb)
287 goto nlmsg_failure;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800288
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700289 old_tail = skb->tail;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800290 nlh = NLMSG_PUT(skb, 0, 0,
Harald Welte7af4cc32005-08-09 19:44:15 -0700291 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
292 sizeof(struct nfgenmsg));
293 nfmsg = NLMSG_DATA(nlh);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800294 nfmsg->nfgen_family = entry->pf;
Harald Welte7af4cc32005-08-09 19:44:15 -0700295 nfmsg->version = NFNETLINK_V0;
296 nfmsg->res_id = htons(queue->queue_num);
297
298 pmsg.packet_id = htonl(entry->id);
Al Virofebf0a42006-11-03 00:59:17 -0800299 pmsg.hw_protocol = entskb->protocol;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800300 pmsg.hook = entry->hook;
Harald Welte7af4cc32005-08-09 19:44:15 -0700301
Patrick McHardydf6fb862007-09-28 14:37:03 -0700302 NLA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
Harald Welte7af4cc32005-08-09 19:44:15 -0700303
Patrick McHardy02f014d2007-12-05 01:26:33 -0800304 indev = entry->indev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800305 if (indev) {
306 tmp_uint = htonl(indev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700307#ifndef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700308 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint);
Harald Weltefbcd9232005-08-09 20:22:10 -0700309#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800310 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700311 /* Case 1: indev is physical input device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800312 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700313 * netfilter_bridge) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700314 NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700315 &tmp_uint);
316 /* this is the bridge group "brX" */
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800317 tmp_uint = htonl(indev->br_port->br->dev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700318 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700319 &tmp_uint);
320 } else {
321 /* Case 2: indev is bridge group, we need to look for
322 * physical device (when called from ipv4) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700323 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700324 &tmp_uint);
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800325 if (entskb->nf_bridge
326 && entskb->nf_bridge->physindev) {
327 tmp_uint = htonl(entskb->nf_bridge->physindev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700328 NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV,
Harald Weltefbcd9232005-08-09 20:22:10 -0700329 sizeof(tmp_uint), &tmp_uint);
330 }
331 }
332#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700333 }
334
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800335 if (outdev) {
336 tmp_uint = htonl(outdev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700337#ifndef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700338 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint);
Harald Weltefbcd9232005-08-09 20:22:10 -0700339#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800340 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700341 /* Case 1: outdev is physical output device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800342 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700343 * netfilter_bridge) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700344 NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700345 &tmp_uint);
346 /* this is the bridge group "brX" */
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800347 tmp_uint = htonl(outdev->br_port->br->dev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700348 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700349 &tmp_uint);
350 } else {
351 /* Case 2: outdev is bridge group, we need to look for
352 * physical output device (when called from ipv4) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700353 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700354 &tmp_uint);
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800355 if (entskb->nf_bridge
356 && entskb->nf_bridge->physoutdev) {
357 tmp_uint = htonl(entskb->nf_bridge->physoutdev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700358 NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV,
Harald Weltefbcd9232005-08-09 20:22:10 -0700359 sizeof(tmp_uint), &tmp_uint);
360 }
361 }
362#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700363 }
364
Thomas Graf82e91ff2006-11-09 15:19:14 -0800365 if (entskb->mark) {
366 tmp_uint = htonl(entskb->mark);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700367 NLA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint);
Harald Welte7af4cc32005-08-09 19:44:15 -0700368 }
369
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700370 if (indev && entskb->dev) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700371 struct nfqnl_msg_packet_hw phw;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700372 int len = dev_parse_header(entskb, phw.hw_addr);
373 if (len) {
374 phw.hw_addrlen = htons(len);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700375 NLA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700376 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700377 }
378
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700379 if (entskb->tstamp.tv64) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700380 struct nfqnl_msg_packet_timestamp ts;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700381 struct timeval tv = ktime_to_timeval(entskb->tstamp);
382 ts.sec = cpu_to_be64(tv.tv_sec);
383 ts.usec = cpu_to_be64(tv.tv_usec);
Harald Welte7af4cc32005-08-09 19:44:15 -0700384
Patrick McHardydf6fb862007-09-28 14:37:03 -0700385 NLA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
Harald Welte7af4cc32005-08-09 19:44:15 -0700386 }
387
388 if (data_len) {
Patrick McHardydf6fb862007-09-28 14:37:03 -0700389 struct nlattr *nla;
390 int size = nla_attr_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700391
Patrick McHardydf6fb862007-09-28 14:37:03 -0700392 if (skb_tailroom(skb) < nla_total_size(data_len)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700393 printk(KERN_WARNING "nf_queue: no tailroom!\n");
394 goto nlmsg_failure;
395 }
396
Patrick McHardydf6fb862007-09-28 14:37:03 -0700397 nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
398 nla->nla_type = NFQA_PAYLOAD;
399 nla->nla_len = size;
Harald Welte7af4cc32005-08-09 19:44:15 -0700400
Patrick McHardydf6fb862007-09-28 14:37:03 -0700401 if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700402 BUG();
403 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800404
Harald Welte7af4cc32005-08-09 19:44:15 -0700405 nlh->nlmsg_len = skb->tail - old_tail;
406 return skb;
407
408nlmsg_failure:
Patrick McHardydf6fb862007-09-28 14:37:03 -0700409nla_put_failure:
Harald Welte7af4cc32005-08-09 19:44:15 -0700410 if (skb)
411 kfree_skb(skb);
412 *errp = -EINVAL;
413 if (net_ratelimit())
414 printk(KERN_ERR "nf_queue: error creating packet message\n");
415 return NULL;
416}
417
418static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800419nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
Harald Welte7af4cc32005-08-09 19:44:15 -0700420{
421 int status = -EINVAL;
422 struct sk_buff *nskb;
423 struct nfqnl_instance *queue;
Harald Welte7af4cc32005-08-09 19:44:15 -0700424
425 QDEBUG("entered\n");
426
Patrick McHardy9872bec2007-12-05 01:28:50 -0800427 /* rcu_read_lock()ed by nf_hook_slow() */
428 queue = instance_lookup(queuenum);
Harald Welte7af4cc32005-08-09 19:44:15 -0700429 if (!queue) {
430 QDEBUG("no queue instance matching\n");
431 return -EINVAL;
432 }
433
434 if (queue->copy_mode == NFQNL_COPY_NONE) {
435 QDEBUG("mode COPY_NONE, aborting\n");
Patrick McHardy9872bec2007-12-05 01:28:50 -0800436 return -EAGAIN;
Harald Welte7af4cc32005-08-09 19:44:15 -0700437 }
438
Harald Welte7af4cc32005-08-09 19:44:15 -0700439 nskb = nfqnl_build_packet_message(queue, entry, &status);
440 if (nskb == NULL)
Patrick McHardy9872bec2007-12-05 01:28:50 -0800441 return status;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800442
Harald Welte7af4cc32005-08-09 19:44:15 -0700443 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800444
Harald Welte7af4cc32005-08-09 19:44:15 -0700445 if (!queue->peer_pid)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800446 goto err_out_free_nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700447
448 if (queue->queue_total >= queue->queue_maxlen) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800449 queue->queue_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700450 status = -ENOSPC;
451 if (net_ratelimit())
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800452 printk(KERN_WARNING "nf_queue: full at %d entries, "
453 "dropping packets(s). Dropped: %d\n",
Harald Welte7af4cc32005-08-09 19:44:15 -0700454 queue->queue_total, queue->queue_dropped);
455 goto err_out_free_nskb;
456 }
457
458 /* nfnetlink_unicast will either free the nskb or add it to a socket */
459 status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
460 if (status < 0) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800461 queue->queue_user_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700462 goto err_out_unlock;
463 }
464
465 __enqueue_entry(queue, entry);
466
467 spin_unlock_bh(&queue->lock);
468 return status;
469
470err_out_free_nskb:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800471 kfree_skb(nskb);
472
Harald Welte7af4cc32005-08-09 19:44:15 -0700473err_out_unlock:
474 spin_unlock_bh(&queue->lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700475 return status;
476}
477
478static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800479nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e)
Harald Welte7af4cc32005-08-09 19:44:15 -0700480{
481 int diff;
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700482 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700483
484 diff = data_len - e->skb->len;
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800485 if (diff < 0) {
486 if (pskb_trim(e->skb, data_len))
487 return -ENOMEM;
488 } else if (diff > 0) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700489 if (data_len > 0xFFFF)
490 return -EINVAL;
491 if (diff > skb_tailroom(e->skb)) {
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700492 err = pskb_expand_head(e->skb, 0,
493 diff - skb_tailroom(e->skb),
494 GFP_ATOMIC);
495 if (err) {
Patrick McHardy1158ba22006-08-22 00:32:47 -0700496 printk(KERN_WARNING "nf_queue: OOM "
Harald Welte7af4cc32005-08-09 19:44:15 -0700497 "in mangle, dropping packet\n");
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700498 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700499 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700500 }
501 skb_put(e->skb, diff);
502 }
Herbert Xu37d41872007-10-14 00:39:18 -0700503 if (!skb_make_writable(e->skb, data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700504 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300505 skb_copy_to_linear_data(e->skb, data, data_len);
Patrick McHardye7dfb092005-09-06 15:10:00 -0700506 e->skb->ip_summed = CHECKSUM_NONE;
Harald Welte7af4cc32005-08-09 19:44:15 -0700507 return 0;
508}
509
Harald Welte7af4cc32005-08-09 19:44:15 -0700510static int
511nfqnl_set_mode(struct nfqnl_instance *queue,
512 unsigned char mode, unsigned int range)
513{
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800514 int status = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700515
516 spin_lock_bh(&queue->lock);
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800517 switch (mode) {
518 case NFQNL_COPY_NONE:
519 case NFQNL_COPY_META:
520 queue->copy_mode = mode;
521 queue->copy_range = 0;
522 break;
523
524 case NFQNL_COPY_PACKET:
525 queue->copy_mode = mode;
526 /* we're using struct nlattr which has 16bit nla_len */
527 if (range > 0xffff)
528 queue->copy_range = 0xffff;
529 else
530 queue->copy_range = range;
531 break;
532
533 default:
534 status = -EINVAL;
535
536 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700537 spin_unlock_bh(&queue->lock);
538
539 return status;
540}
541
542static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800543dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700544{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800545 if (entry->indev)
546 if (entry->indev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700547 return 1;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800548 if (entry->outdev)
549 if (entry->outdev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700550 return 1;
Patrick McHardyef47c6a2006-06-27 03:01:48 -0700551#ifdef CONFIG_BRIDGE_NETFILTER
552 if (entry->skb->nf_bridge) {
553 if (entry->skb->nf_bridge->physindev &&
554 entry->skb->nf_bridge->physindev->ifindex == ifindex)
555 return 1;
556 if (entry->skb->nf_bridge->physoutdev &&
557 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
558 return 1;
559 }
560#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700561 return 0;
562}
563
564/* drop all packets with either indev or outdev == ifindex from all queue
565 * instances */
566static void
567nfqnl_dev_drop(int ifindex)
568{
569 int i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800570
Harald Welte7af4cc32005-08-09 19:44:15 -0700571 QDEBUG("entering for ifindex %u\n", ifindex);
572
Patrick McHardy9872bec2007-12-05 01:28:50 -0800573 rcu_read_lock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700574
Patrick McHardy9872bec2007-12-05 01:28:50 -0800575 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700576 struct hlist_node *tmp;
577 struct nfqnl_instance *inst;
578 struct hlist_head *head = &instance_table[i];
579
Patrick McHardy9872bec2007-12-05 01:28:50 -0800580 hlist_for_each_entry_rcu(inst, tmp, head, hlist)
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800581 nfqnl_flush(inst, dev_cmp, ifindex);
Harald Welte7af4cc32005-08-09 19:44:15 -0700582 }
583
Patrick McHardy9872bec2007-12-05 01:28:50 -0800584 rcu_read_unlock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700585}
586
587#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
588
589static int
590nfqnl_rcv_dev_event(struct notifier_block *this,
591 unsigned long event, void *ptr)
592{
593 struct net_device *dev = ptr;
594
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200595 if (dev->nd_net != &init_net)
596 return NOTIFY_DONE;
597
Harald Welte7af4cc32005-08-09 19:44:15 -0700598 /* Drop any packets associated with the downed device */
599 if (event == NETDEV_DOWN)
600 nfqnl_dev_drop(dev->ifindex);
601 return NOTIFY_DONE;
602}
603
604static struct notifier_block nfqnl_dev_notifier = {
605 .notifier_call = nfqnl_rcv_dev_event,
606};
607
608static int
609nfqnl_rcv_nl_event(struct notifier_block *this,
610 unsigned long event, void *ptr)
611{
612 struct netlink_notify *n = ptr;
613
614 if (event == NETLINK_URELEASE &&
615 n->protocol == NETLINK_NETFILTER && n->pid) {
616 int i;
617
618 /* destroy all instances for this pid */
Patrick McHardy9872bec2007-12-05 01:28:50 -0800619 spin_lock(&instances_lock);
620 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700621 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) {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200626 if ((n->net == &init_net) &&
627 (n->pid == inst->peer_pid))
Harald Welte7af4cc32005-08-09 19:44:15 -0700628 __instance_destroy(inst);
629 }
630 }
Patrick McHardy9872bec2007-12-05 01:28:50 -0800631 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700632 }
633 return NOTIFY_DONE;
634}
635
636static struct notifier_block nfqnl_rtnl_notifier = {
637 .notifier_call = nfqnl_rcv_nl_event,
638};
639
Patrick McHardy5bf75852007-09-28 14:39:26 -0700640static 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 },
Harald Welte838ab632005-08-09 19:50:45 -0700644};
645
Harald Welte7af4cc32005-08-09 19:44:15 -0700646static int
647nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700648 struct nlmsghdr *nlh, struct nlattr *nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700649{
650 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
651 u_int16_t queue_num = ntohs(nfmsg->res_id);
652
653 struct nfqnl_msg_verdict_hdr *vhdr;
654 struct nfqnl_instance *queue;
655 unsigned int verdict;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800656 struct nf_queue_entry *entry;
Harald Welte838ab632005-08-09 19:50:45 -0700657 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700658
Patrick McHardy9872bec2007-12-05 01:28:50 -0800659 rcu_read_lock();
660 queue = instance_lookup(queue_num);
661 if (!queue) {
662 err = -ENODEV;
663 goto err_out_unlock;
664 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700665
Harald Welte838ab632005-08-09 19:50:45 -0700666 if (queue->peer_pid != NETLINK_CB(skb).pid) {
667 err = -EPERM;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800668 goto err_out_unlock;
Harald Welte838ab632005-08-09 19:50:45 -0700669 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700670
Patrick McHardydf6fb862007-09-28 14:37:03 -0700671 if (!nfqa[NFQA_VERDICT_HDR]) {
Harald Welte838ab632005-08-09 19:50:45 -0700672 err = -EINVAL;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800673 goto err_out_unlock;
Harald Welte838ab632005-08-09 19:50:45 -0700674 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700675
Patrick McHardydf6fb862007-09-28 14:37:03 -0700676 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700677 verdict = ntohl(vhdr->verdict);
678
Harald Welte838ab632005-08-09 19:50:45 -0700679 if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
680 err = -EINVAL;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800681 goto err_out_unlock;
Harald Welte838ab632005-08-09 19:50:45 -0700682 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700683
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800684 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
Harald Welte838ab632005-08-09 19:50:45 -0700685 if (entry == NULL) {
686 err = -ENOENT;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800687 goto err_out_unlock;
Harald Welte838ab632005-08-09 19:50:45 -0700688 }
Patrick McHardy9872bec2007-12-05 01:28:50 -0800689 rcu_read_unlock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700690
Patrick McHardydf6fb862007-09-28 14:37:03 -0700691 if (nfqa[NFQA_PAYLOAD]) {
692 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
693 nla_len(nfqa[NFQA_PAYLOAD]), entry) < 0)
Harald Welte7af4cc32005-08-09 19:44:15 -0700694 verdict = NF_DROP;
695 }
696
Patrick McHardydf6fb862007-09-28 14:37:03 -0700697 if (nfqa[NFQA_MARK])
Thomas Graf82e91ff2006-11-09 15:19:14 -0800698 entry->skb->mark = ntohl(*(__be32 *)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700699 nla_data(nfqa[NFQA_MARK]));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800700
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800701 nf_reinject(entry, verdict);
Harald Welte7af4cc32005-08-09 19:44:15 -0700702 return 0;
Harald Welte838ab632005-08-09 19:50:45 -0700703
Patrick McHardy9872bec2007-12-05 01:28:50 -0800704err_out_unlock:
705 rcu_read_unlock();
Harald Welte838ab632005-08-09 19:50:45 -0700706 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700707}
708
709static int
710nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700711 struct nlmsghdr *nlh, struct nlattr *nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700712{
713 return -ENOTSUPP;
714}
715
Patrick McHardy5bf75852007-09-28 14:39:26 -0700716static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
717 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
718 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
Harald Welte838ab632005-08-09 19:50:45 -0700719};
720
Patrick McHardye3ac5292007-12-05 01:23:57 -0800721static const struct nf_queue_handler nfqh = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700722 .name = "nf_queue",
723 .outfn = &nfqnl_enqueue_packet,
724};
725
Harald Welte7af4cc32005-08-09 19:44:15 -0700726static int
727nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700728 struct nlmsghdr *nlh, struct nlattr *nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700729{
730 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
731 u_int16_t queue_num = ntohs(nfmsg->res_id);
732 struct nfqnl_instance *queue;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800733 struct nfqnl_msg_config_cmd *cmd = NULL;
Harald Welte838ab632005-08-09 19:50:45 -0700734 int ret = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700735
736 QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
737
Patrick McHardy9872bec2007-12-05 01:28:50 -0800738 if (nfqa[NFQA_CFG_CMD]) {
739 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
740
741 /* Commands without queue context - might sleep */
742 switch (cmd->command) {
743 case NFQNL_CFG_CMD_PF_BIND:
744 ret = nf_register_queue_handler(ntohs(cmd->pf),
745 &nfqh);
746 break;
747 case NFQNL_CFG_CMD_PF_UNBIND:
748 ret = nf_unregister_queue_handler(ntohs(cmd->pf),
749 &nfqh);
750 break;
751 default:
752 break;
753 }
754
755 if (ret < 0)
756 return ret;
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800757 }
758
Patrick McHardy9872bec2007-12-05 01:28:50 -0800759 rcu_read_lock();
760 queue = instance_lookup(queue_num);
761 if (queue && queue->peer_pid != NETLINK_CB(skb).pid) {
762 ret = -EPERM;
763 goto err_out_unlock;
764 }
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800765
Patrick McHardy9872bec2007-12-05 01:28:50 -0800766 if (cmd != NULL) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700767 switch (cmd->command) {
768 case NFQNL_CFG_CMD_BIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800769 if (queue) {
770 ret = -EBUSY;
771 goto err_out_unlock;
772 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700773 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800774 if (!queue) {
775 ret = -EINVAL;
776 goto err_out_unlock;
777 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700778 break;
779 case NFQNL_CFG_CMD_UNBIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800780 if (!queue) {
781 ret = -ENODEV;
782 goto err_out_unlock;
783 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700784 instance_destroy(queue);
785 break;
786 case NFQNL_CFG_CMD_PF_BIND:
Harald Welte7af4cc32005-08-09 19:44:15 -0700787 case NFQNL_CFG_CMD_PF_UNBIND:
Harald Welte7af4cc32005-08-09 19:44:15 -0700788 break;
789 default:
Harald Welte838ab632005-08-09 19:50:45 -0700790 ret = -EINVAL;
791 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700792 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700793 }
794
Patrick McHardydf6fb862007-09-28 14:37:03 -0700795 if (nfqa[NFQA_CFG_PARAMS]) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700796 struct nfqnl_msg_config_params *params;
Harald Welte7af4cc32005-08-09 19:44:15 -0700797
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800798 if (!queue) {
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800799 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800800 goto err_out_unlock;
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800801 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700802 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700803 nfqnl_set_mode(queue, params->copy_mode,
804 ntohl(params->copy_range));
805 }
806
Patrick McHardydf6fb862007-09-28 14:37:03 -0700807 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
Eric Leblond829e17a2006-11-29 02:35:33 +0100808 __be32 *queue_maxlen;
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800809
810 if (!queue) {
811 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800812 goto err_out_unlock;
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800813 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700814 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
Eric Leblond829e17a2006-11-29 02:35:33 +0100815 spin_lock_bh(&queue->lock);
816 queue->queue_maxlen = ntohl(*queue_maxlen);
817 spin_unlock_bh(&queue->lock);
818 }
819
Patrick McHardy9872bec2007-12-05 01:28:50 -0800820err_out_unlock:
821 rcu_read_unlock();
Harald Welte838ab632005-08-09 19:50:45 -0700822 return ret;
Harald Welte7af4cc32005-08-09 19:44:15 -0700823}
824
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700825static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700826 [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800827 .attr_count = NFQA_MAX, },
Harald Welte7af4cc32005-08-09 19:44:15 -0700828 [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700829 .attr_count = NFQA_MAX,
830 .policy = nfqa_verdict_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700831 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700832 .attr_count = NFQA_CFG_MAX,
833 .policy = nfqa_cfg_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700834};
835
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700836static const struct nfnetlink_subsystem nfqnl_subsys = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700837 .name = "nf_queue",
838 .subsys_id = NFNL_SUBSYS_QUEUE,
839 .cb_count = NFQNL_MSG_MAX,
Harald Welte7af4cc32005-08-09 19:44:15 -0700840 .cb = nfqnl_cb,
841};
842
Harald Welte838ab632005-08-09 19:50:45 -0700843#ifdef CONFIG_PROC_FS
844struct iter_state {
845 unsigned int bucket;
846};
847
848static struct hlist_node *get_first(struct seq_file *seq)
849{
850 struct iter_state *st = seq->private;
851
852 if (!st)
853 return NULL;
854
855 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
856 if (!hlist_empty(&instance_table[st->bucket]))
857 return instance_table[st->bucket].first;
858 }
859 return NULL;
860}
861
862static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
863{
864 struct iter_state *st = seq->private;
865
866 h = h->next;
867 while (!h) {
868 if (++st->bucket >= INSTANCE_BUCKETS)
869 return NULL;
870
871 h = instance_table[st->bucket].first;
872 }
873 return h;
874}
875
876static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
877{
878 struct hlist_node *head;
879 head = get_first(seq);
880
881 if (head)
882 while (pos && (head = get_next(seq, head)))
883 pos--;
884 return pos ? NULL : head;
885}
886
887static void *seq_start(struct seq_file *seq, loff_t *pos)
888{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800889 spin_lock(&instances_lock);
Harald Welte838ab632005-08-09 19:50:45 -0700890 return get_idx(seq, *pos);
891}
892
893static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
894{
895 (*pos)++;
896 return get_next(s, v);
897}
898
899static void seq_stop(struct seq_file *s, void *v)
900{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800901 spin_unlock(&instances_lock);
Harald Welte838ab632005-08-09 19:50:45 -0700902}
903
904static int seq_show(struct seq_file *s, void *v)
905{
906 const struct nfqnl_instance *inst = v;
907
908 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
909 inst->queue_num,
910 inst->peer_pid, inst->queue_total,
911 inst->copy_mode, inst->copy_range,
912 inst->queue_dropped, inst->queue_user_dropped,
Patrick McHardy9872bec2007-12-05 01:28:50 -0800913 inst->id_sequence, 1);
Harald Welte838ab632005-08-09 19:50:45 -0700914}
915
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700916static const struct seq_operations nfqnl_seq_ops = {
Harald Welte838ab632005-08-09 19:50:45 -0700917 .start = seq_start,
918 .next = seq_next,
919 .stop = seq_stop,
920 .show = seq_show,
921};
922
923static int nfqnl_open(struct inode *inode, struct file *file)
924{
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700925 return seq_open_private(file, &nfqnl_seq_ops,
926 sizeof(struct iter_state));
Harald Welte838ab632005-08-09 19:50:45 -0700927}
928
Arjan van de Venda7071d2007-02-12 00:55:36 -0800929static const struct file_operations nfqnl_file_ops = {
Harald Welte838ab632005-08-09 19:50:45 -0700930 .owner = THIS_MODULE,
931 .open = nfqnl_open,
932 .read = seq_read,
933 .llseek = seq_lseek,
934 .release = seq_release_private,
935};
936
937#endif /* PROC_FS */
938
Patrick McHardy32292a72006-04-06 14:11:30 -0700939static int __init nfnetlink_queue_init(void)
Harald Welte7af4cc32005-08-09 19:44:15 -0700940{
Harald Welte838ab632005-08-09 19:50:45 -0700941 int i, status = -ENOMEM;
942#ifdef CONFIG_PROC_FS
943 struct proc_dir_entry *proc_nfqueue;
944#endif
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800945
Harald Welte838ab632005-08-09 19:50:45 -0700946 for (i = 0; i < INSTANCE_BUCKETS; i++)
947 INIT_HLIST_HEAD(&instance_table[i]);
948
Harald Welte7af4cc32005-08-09 19:44:15 -0700949 netlink_register_notifier(&nfqnl_rtnl_notifier);
950 status = nfnetlink_subsys_register(&nfqnl_subsys);
951 if (status < 0) {
952 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
953 goto cleanup_netlink_notifier;
954 }
955
Harald Welte838ab632005-08-09 19:50:45 -0700956#ifdef CONFIG_PROC_FS
957 proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
958 proc_net_netfilter);
959 if (!proc_nfqueue)
960 goto cleanup_subsys;
961 proc_nfqueue->proc_fops = &nfqnl_file_ops;
962#endif
963
Harald Welte7af4cc32005-08-09 19:44:15 -0700964 register_netdevice_notifier(&nfqnl_dev_notifier);
965 return status;
966
Harald Welte838ab632005-08-09 19:50:45 -0700967#ifdef CONFIG_PROC_FS
968cleanup_subsys:
Harald Welte7af4cc32005-08-09 19:44:15 -0700969 nfnetlink_subsys_unregister(&nfqnl_subsys);
Patrick McHardy32292a72006-04-06 14:11:30 -0700970#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700971cleanup_netlink_notifier:
972 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
973 return status;
974}
975
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800976static void __exit nfnetlink_queue_fini(void)
Harald Welte7af4cc32005-08-09 19:44:15 -0700977{
Patrick McHardy32292a72006-04-06 14:11:30 -0700978 nf_unregister_queue_handlers(&nfqh);
979 unregister_netdevice_notifier(&nfqnl_dev_notifier);
980#ifdef CONFIG_PROC_FS
981 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
982#endif
983 nfnetlink_subsys_unregister(&nfqnl_subsys);
984 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
Harald Welte7af4cc32005-08-09 19:44:15 -0700985}
986
987MODULE_DESCRIPTION("netfilter packet queue handler");
988MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
989MODULE_LICENSE("GPL");
990MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
991
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800992module_init(nfnetlink_queue_init);
993module_exit(nfnetlink_queue_fini);