Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1 | /* |
| 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 Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 23 | #include <linux/proc_fs.h> |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 24 | #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 McHardy | c01cd42 | 2007-12-05 01:24:48 -0800 | [diff] [blame] | 30 | #include <net/netfilter/nf_queue.h> |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 31 | |
| 32 | #include <asm/atomic.h> |
| 33 | |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 34 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 35 | #include "../bridge/br_private.h" |
| 36 | #endif |
| 37 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 38 | #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 Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 48 | struct nfqnl_instance { |
| 49 | struct hlist_node hlist; /* global list of queues */ |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 50 | atomic_t use; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 51 | |
| 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 McHardy | e48b9b2 | 2007-12-05 01:28:10 -0800 | [diff] [blame^] | 59 | unsigned int id_sequence; /* 'sequence' of pkt ids */ |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 60 | |
| 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 McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 69 | typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 70 | |
| 71 | static DEFINE_RWLOCK(instances_lock); |
| 72 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 73 | #define INSTANCE_BUCKETS 16 |
| 74 | static struct hlist_head instance_table[INSTANCE_BUCKETS]; |
| 75 | |
| 76 | static inline u_int8_t instance_hashfn(u_int16_t queue_num) |
| 77 | { |
| 78 | return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS; |
| 79 | } |
| 80 | |
| 81 | static struct nfqnl_instance * |
| 82 | __instance_lookup(u_int16_t queue_num) |
| 83 | { |
| 84 | struct hlist_head *head; |
| 85 | struct hlist_node *pos; |
| 86 | struct nfqnl_instance *inst; |
| 87 | |
| 88 | head = &instance_table[instance_hashfn(queue_num)]; |
| 89 | hlist_for_each_entry(inst, pos, head, hlist) { |
| 90 | if (inst->queue_num == queue_num) |
| 91 | return inst; |
| 92 | } |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | static struct nfqnl_instance * |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 97 | instance_lookup_get(u_int16_t queue_num) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 98 | { |
| 99 | struct nfqnl_instance *inst; |
| 100 | |
| 101 | read_lock_bh(&instances_lock); |
| 102 | inst = __instance_lookup(queue_num); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 103 | if (inst) |
| 104 | atomic_inc(&inst->use); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 105 | read_unlock_bh(&instances_lock); |
| 106 | |
| 107 | return inst; |
| 108 | } |
| 109 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 110 | static void |
| 111 | instance_put(struct nfqnl_instance *inst) |
| 112 | { |
| 113 | if (inst && atomic_dec_and_test(&inst->use)) { |
| 114 | QDEBUG("kfree(inst=%p)\n", inst); |
| 115 | kfree(inst); |
| 116 | } |
| 117 | } |
| 118 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 119 | static struct nfqnl_instance * |
| 120 | instance_create(u_int16_t queue_num, int pid) |
| 121 | { |
| 122 | struct nfqnl_instance *inst; |
| 123 | |
| 124 | QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num, pid); |
| 125 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 126 | write_lock_bh(&instances_lock); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 127 | if (__instance_lookup(queue_num)) { |
| 128 | inst = NULL; |
| 129 | QDEBUG("aborting, instance already exists\n"); |
| 130 | goto out_unlock; |
| 131 | } |
| 132 | |
Harald Welte | 10dfdc6 | 2005-11-03 19:20:07 +0100 | [diff] [blame] | 133 | inst = kzalloc(sizeof(*inst), GFP_ATOMIC); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 134 | if (!inst) |
| 135 | goto out_unlock; |
| 136 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 137 | inst->queue_num = queue_num; |
| 138 | inst->peer_pid = pid; |
| 139 | inst->queue_maxlen = NFQNL_QMAX_DEFAULT; |
| 140 | inst->copy_range = 0xfffff; |
| 141 | inst->copy_mode = NFQNL_COPY_NONE; |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 142 | /* needs to be two, since we _put() after creation */ |
| 143 | atomic_set(&inst->use, 2); |
YOSHIFUJI Hideaki | 181a46a | 2006-01-04 13:56:54 -0800 | [diff] [blame] | 144 | spin_lock_init(&inst->lock); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 145 | INIT_LIST_HEAD(&inst->queue_list); |
| 146 | |
| 147 | if (!try_module_get(THIS_MODULE)) |
| 148 | goto out_free; |
| 149 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 150 | hlist_add_head(&inst->hlist, |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 151 | &instance_table[instance_hashfn(queue_num)]); |
| 152 | |
| 153 | write_unlock_bh(&instances_lock); |
| 154 | |
| 155 | QDEBUG("successfully created new instance\n"); |
| 156 | |
| 157 | return inst; |
| 158 | |
| 159 | out_free: |
| 160 | kfree(inst); |
| 161 | out_unlock: |
| 162 | write_unlock_bh(&instances_lock); |
| 163 | return NULL; |
| 164 | } |
| 165 | |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 166 | static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, |
| 167 | unsigned long data); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 168 | |
| 169 | static void |
| 170 | _instance_destroy2(struct nfqnl_instance *inst, int lock) |
| 171 | { |
| 172 | /* first pull it out of the global list */ |
| 173 | if (lock) |
| 174 | write_lock_bh(&instances_lock); |
| 175 | |
| 176 | QDEBUG("removing instance %p (queuenum=%u) from hash\n", |
| 177 | inst, inst->queue_num); |
| 178 | hlist_del(&inst->hlist); |
| 179 | |
| 180 | if (lock) |
| 181 | write_unlock_bh(&instances_lock); |
| 182 | |
| 183 | /* then flush all pending skbs from the queue */ |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 184 | nfqnl_flush(inst, NULL, 0); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 185 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 186 | /* and finally put the refcount */ |
| 187 | instance_put(inst); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 188 | |
| 189 | module_put(THIS_MODULE); |
| 190 | } |
| 191 | |
| 192 | static inline void |
| 193 | __instance_destroy(struct nfqnl_instance *inst) |
| 194 | { |
| 195 | _instance_destroy2(inst, 0); |
| 196 | } |
| 197 | |
| 198 | static inline void |
| 199 | instance_destroy(struct nfqnl_instance *inst) |
| 200 | { |
| 201 | _instance_destroy2(inst, 1); |
| 202 | } |
| 203 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 204 | static inline void |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 205 | __enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 206 | { |
Patrick McHardy | 0ac41e8 | 2007-12-05 01:25:03 -0800 | [diff] [blame] | 207 | list_add_tail(&entry->list, &queue->queue_list); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 208 | queue->queue_total++; |
| 209 | } |
| 210 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 211 | static inline int |
| 212 | __nfqnl_set_mode(struct nfqnl_instance *queue, |
| 213 | unsigned char mode, unsigned int range) |
| 214 | { |
| 215 | int status = 0; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 216 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 217 | switch (mode) { |
| 218 | case NFQNL_COPY_NONE: |
| 219 | case NFQNL_COPY_META: |
| 220 | queue->copy_mode = mode; |
| 221 | queue->copy_range = 0; |
| 222 | break; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 223 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 224 | case NFQNL_COPY_PACKET: |
| 225 | queue->copy_mode = mode; |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 226 | /* we're using struct nlattr which has 16bit nla_len */ |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 227 | if (range > 0xffff) |
| 228 | queue->copy_range = 0xffff; |
| 229 | else |
| 230 | queue->copy_range = range; |
| 231 | break; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 232 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 233 | default: |
| 234 | status = -EINVAL; |
| 235 | |
| 236 | } |
| 237 | return status; |
| 238 | } |
| 239 | |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 240 | static struct nf_queue_entry * |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 241 | find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 242 | { |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 243 | struct nf_queue_entry *entry = NULL, *i; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 244 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 245 | spin_lock_bh(&queue->lock); |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 246 | |
| 247 | list_for_each_entry(i, &queue->queue_list, list) { |
| 248 | if (i->id == id) { |
| 249 | entry = i; |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | if (entry) { |
| 255 | list_del(&entry->list); |
| 256 | queue->queue_total--; |
| 257 | } |
| 258 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 259 | spin_unlock_bh(&queue->lock); |
| 260 | |
| 261 | return entry; |
| 262 | } |
| 263 | |
| 264 | static void |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 265 | nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 266 | { |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 267 | struct nf_queue_entry *entry, *next; |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 268 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 269 | spin_lock_bh(&queue->lock); |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 270 | list_for_each_entry_safe(entry, next, &queue->queue_list, list) { |
| 271 | if (!cmpfn || cmpfn(entry, data)) { |
| 272 | list_del(&entry->list); |
| 273 | queue->queue_total--; |
Patrick McHardy | 4b3d15e | 2007-12-05 01:27:02 -0800 | [diff] [blame] | 274 | nf_reinject(entry, NF_DROP); |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 275 | } |
| 276 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 277 | spin_unlock_bh(&queue->lock); |
| 278 | } |
| 279 | |
| 280 | static struct sk_buff * |
| 281 | nfqnl_build_packet_message(struct nfqnl_instance *queue, |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 282 | struct nf_queue_entry *entry, int *errp) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 283 | { |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 284 | sk_buff_data_t old_tail; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 285 | size_t size; |
| 286 | size_t data_len = 0; |
| 287 | struct sk_buff *skb; |
| 288 | struct nfqnl_msg_packet_hdr pmsg; |
| 289 | struct nlmsghdr *nlh; |
| 290 | struct nfgenmsg *nfmsg; |
Jesper Juhl | 3e4ead4 | 2006-01-05 12:15:58 -0800 | [diff] [blame] | 291 | struct sk_buff *entskb = entry->skb; |
| 292 | struct net_device *indev; |
| 293 | struct net_device *outdev; |
Al Viro | 98a4a86 | 2006-11-08 00:26:51 -0800 | [diff] [blame] | 294 | __be32 tmp_uint; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 295 | |
| 296 | QDEBUG("entered\n"); |
| 297 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 298 | size = NLMSG_ALIGN(sizeof(struct nfgenmsg)) |
| 299 | + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr)) |
| 300 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ |
| 301 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 302 | #ifdef CONFIG_BRIDGE_NETFILTER |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 303 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ |
| 304 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 305 | #endif |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 306 | + nla_total_size(sizeof(u_int32_t)) /* mark */ |
| 307 | + nla_total_size(sizeof(struct nfqnl_msg_packet_hw)) |
| 308 | + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp)); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 309 | |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 310 | outdev = entry->outdev; |
Jesper Juhl | 3e4ead4 | 2006-01-05 12:15:58 -0800 | [diff] [blame] | 311 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 312 | spin_lock_bh(&queue->lock); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 313 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 314 | switch (queue->copy_mode) { |
| 315 | case NFQNL_COPY_META: |
| 316 | case NFQNL_COPY_NONE: |
| 317 | data_len = 0; |
| 318 | break; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 319 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 320 | case NFQNL_COPY_PACKET: |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 321 | if ((entskb->ip_summed == CHECKSUM_PARTIAL || |
| 322 | entskb->ip_summed == CHECKSUM_COMPLETE) && |
| 323 | (*errp = skb_checksum_help(entskb))) { |
Patrick McHardy | e7dfb09 | 2005-09-06 15:10:00 -0700 | [diff] [blame] | 324 | spin_unlock_bh(&queue->lock); |
| 325 | return NULL; |
| 326 | } |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 327 | if (queue->copy_range == 0 |
Jesper Juhl | 3e4ead4 | 2006-01-05 12:15:58 -0800 | [diff] [blame] | 328 | || queue->copy_range > entskb->len) |
| 329 | data_len = entskb->len; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 330 | else |
| 331 | data_len = queue->copy_range; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 332 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 333 | size += nla_total_size(data_len); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 334 | break; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 335 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 336 | default: |
| 337 | *errp = -EINVAL; |
| 338 | spin_unlock_bh(&queue->lock); |
| 339 | return NULL; |
| 340 | } |
| 341 | |
Patrick McHardy | e48b9b2 | 2007-12-05 01:28:10 -0800 | [diff] [blame^] | 342 | entry->id = queue->id_sequence++; |
| 343 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 344 | spin_unlock_bh(&queue->lock); |
| 345 | |
| 346 | skb = alloc_skb(size, GFP_ATOMIC); |
| 347 | if (!skb) |
| 348 | goto nlmsg_failure; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 349 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 350 | old_tail = skb->tail; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 351 | nlh = NLMSG_PUT(skb, 0, 0, |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 352 | NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET, |
| 353 | sizeof(struct nfgenmsg)); |
| 354 | nfmsg = NLMSG_DATA(nlh); |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 355 | nfmsg->nfgen_family = entry->pf; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 356 | nfmsg->version = NFNETLINK_V0; |
| 357 | nfmsg->res_id = htons(queue->queue_num); |
| 358 | |
| 359 | pmsg.packet_id = htonl(entry->id); |
Al Viro | febf0a4 | 2006-11-03 00:59:17 -0800 | [diff] [blame] | 360 | pmsg.hw_protocol = entskb->protocol; |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 361 | pmsg.hook = entry->hook; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 362 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 363 | NLA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 364 | |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 365 | indev = entry->indev; |
Jesper Juhl | 3e4ead4 | 2006-01-05 12:15:58 -0800 | [diff] [blame] | 366 | if (indev) { |
| 367 | tmp_uint = htonl(indev->ifindex); |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 368 | #ifndef CONFIG_BRIDGE_NETFILTER |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 369 | NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint); |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 370 | #else |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 371 | if (entry->pf == PF_BRIDGE) { |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 372 | /* Case 1: indev is physical input device, we need to |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 373 | * look for bridge group (when called from |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 374 | * netfilter_bridge) */ |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 375 | NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint), |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 376 | &tmp_uint); |
| 377 | /* this is the bridge group "brX" */ |
Jesper Juhl | 3e4ead4 | 2006-01-05 12:15:58 -0800 | [diff] [blame] | 378 | tmp_uint = htonl(indev->br_port->br->dev->ifindex); |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 379 | NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 380 | &tmp_uint); |
| 381 | } else { |
| 382 | /* Case 2: indev is bridge group, we need to look for |
| 383 | * physical device (when called from ipv4) */ |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 384 | NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 385 | &tmp_uint); |
Jesper Juhl | 3e4ead4 | 2006-01-05 12:15:58 -0800 | [diff] [blame] | 386 | if (entskb->nf_bridge |
| 387 | && entskb->nf_bridge->physindev) { |
| 388 | tmp_uint = htonl(entskb->nf_bridge->physindev->ifindex); |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 389 | NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 390 | sizeof(tmp_uint), &tmp_uint); |
| 391 | } |
| 392 | } |
| 393 | #endif |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Jesper Juhl | 3e4ead4 | 2006-01-05 12:15:58 -0800 | [diff] [blame] | 396 | if (outdev) { |
| 397 | tmp_uint = htonl(outdev->ifindex); |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 398 | #ifndef CONFIG_BRIDGE_NETFILTER |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 399 | NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint); |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 400 | #else |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 401 | if (entry->pf == PF_BRIDGE) { |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 402 | /* Case 1: outdev is physical output device, we need to |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 403 | * look for bridge group (when called from |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 404 | * netfilter_bridge) */ |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 405 | NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint), |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 406 | &tmp_uint); |
| 407 | /* this is the bridge group "brX" */ |
Jesper Juhl | 3e4ead4 | 2006-01-05 12:15:58 -0800 | [diff] [blame] | 408 | tmp_uint = htonl(outdev->br_port->br->dev->ifindex); |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 409 | NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 410 | &tmp_uint); |
| 411 | } else { |
| 412 | /* Case 2: outdev is bridge group, we need to look for |
| 413 | * physical output device (when called from ipv4) */ |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 414 | NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 415 | &tmp_uint); |
Jesper Juhl | 3e4ead4 | 2006-01-05 12:15:58 -0800 | [diff] [blame] | 416 | if (entskb->nf_bridge |
| 417 | && entskb->nf_bridge->physoutdev) { |
| 418 | tmp_uint = htonl(entskb->nf_bridge->physoutdev->ifindex); |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 419 | NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 420 | sizeof(tmp_uint), &tmp_uint); |
| 421 | } |
| 422 | } |
| 423 | #endif |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 426 | if (entskb->mark) { |
| 427 | tmp_uint = htonl(entskb->mark); |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 428 | NLA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Stephen Hemminger | b95cce3 | 2007-09-26 22:13:38 -0700 | [diff] [blame] | 431 | if (indev && entskb->dev) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 432 | struct nfqnl_msg_packet_hw phw; |
Stephen Hemminger | b95cce3 | 2007-09-26 22:13:38 -0700 | [diff] [blame] | 433 | int len = dev_parse_header(entskb, phw.hw_addr); |
| 434 | if (len) { |
| 435 | phw.hw_addrlen = htons(len); |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 436 | NLA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw); |
Stephen Hemminger | b95cce3 | 2007-09-26 22:13:38 -0700 | [diff] [blame] | 437 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 440 | if (entskb->tstamp.tv64) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 441 | struct nfqnl_msg_packet_timestamp ts; |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 442 | struct timeval tv = ktime_to_timeval(entskb->tstamp); |
| 443 | ts.sec = cpu_to_be64(tv.tv_sec); |
| 444 | ts.usec = cpu_to_be64(tv.tv_usec); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 445 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 446 | NLA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | if (data_len) { |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 450 | struct nlattr *nla; |
| 451 | int size = nla_attr_size(data_len); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 452 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 453 | if (skb_tailroom(skb) < nla_total_size(data_len)) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 454 | printk(KERN_WARNING "nf_queue: no tailroom!\n"); |
| 455 | goto nlmsg_failure; |
| 456 | } |
| 457 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 458 | nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len)); |
| 459 | nla->nla_type = NFQA_PAYLOAD; |
| 460 | nla->nla_len = size; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 461 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 462 | if (skb_copy_bits(entskb, 0, nla_data(nla), data_len)) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 463 | BUG(); |
| 464 | } |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 465 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 466 | nlh->nlmsg_len = skb->tail - old_tail; |
| 467 | return skb; |
| 468 | |
| 469 | nlmsg_failure: |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 470 | nla_put_failure: |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 471 | if (skb) |
| 472 | kfree_skb(skb); |
| 473 | *errp = -EINVAL; |
| 474 | if (net_ratelimit()) |
| 475 | printk(KERN_ERR "nf_queue: error creating packet message\n"); |
| 476 | return NULL; |
| 477 | } |
| 478 | |
| 479 | static int |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 480 | nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 481 | { |
| 482 | int status = -EINVAL; |
| 483 | struct sk_buff *nskb; |
| 484 | struct nfqnl_instance *queue; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 485 | |
| 486 | QDEBUG("entered\n"); |
| 487 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 488 | queue = instance_lookup_get(queuenum); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 489 | if (!queue) { |
| 490 | QDEBUG("no queue instance matching\n"); |
| 491 | return -EINVAL; |
| 492 | } |
| 493 | |
| 494 | if (queue->copy_mode == NFQNL_COPY_NONE) { |
| 495 | QDEBUG("mode COPY_NONE, aborting\n"); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 496 | status = -EAGAIN; |
| 497 | goto err_out_put; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 500 | nskb = nfqnl_build_packet_message(queue, entry, &status); |
| 501 | if (nskb == NULL) |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 502 | goto err_out_put; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 503 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 504 | spin_lock_bh(&queue->lock); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 505 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 506 | if (!queue->peer_pid) |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 507 | goto err_out_free_nskb; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 508 | |
| 509 | if (queue->queue_total >= queue->queue_maxlen) { |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 510 | queue->queue_dropped++; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 511 | status = -ENOSPC; |
| 512 | if (net_ratelimit()) |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 513 | printk(KERN_WARNING "nf_queue: full at %d entries, " |
| 514 | "dropping packets(s). Dropped: %d\n", |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 515 | queue->queue_total, queue->queue_dropped); |
| 516 | goto err_out_free_nskb; |
| 517 | } |
| 518 | |
| 519 | /* nfnetlink_unicast will either free the nskb or add it to a socket */ |
| 520 | status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT); |
| 521 | if (status < 0) { |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 522 | queue->queue_user_dropped++; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 523 | goto err_out_unlock; |
| 524 | } |
| 525 | |
| 526 | __enqueue_entry(queue, entry); |
| 527 | |
| 528 | spin_unlock_bh(&queue->lock); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 529 | instance_put(queue); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 530 | return status; |
| 531 | |
| 532 | err_out_free_nskb: |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 533 | kfree_skb(nskb); |
| 534 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 535 | err_out_unlock: |
| 536 | spin_unlock_bh(&queue->lock); |
| 537 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 538 | err_out_put: |
| 539 | instance_put(queue); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 540 | return status; |
| 541 | } |
| 542 | |
| 543 | static int |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 544 | nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 545 | { |
| 546 | int diff; |
Herbert Xu | 2ca7b0a | 2007-10-14 00:39:55 -0700 | [diff] [blame] | 547 | int err; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 548 | |
| 549 | diff = data_len - e->skb->len; |
Patrick McHardy | d8a585d | 2006-11-14 19:48:09 -0800 | [diff] [blame] | 550 | if (diff < 0) { |
| 551 | if (pskb_trim(e->skb, data_len)) |
| 552 | return -ENOMEM; |
| 553 | } else if (diff > 0) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 554 | if (data_len > 0xFFFF) |
| 555 | return -EINVAL; |
| 556 | if (diff > skb_tailroom(e->skb)) { |
Herbert Xu | 2ca7b0a | 2007-10-14 00:39:55 -0700 | [diff] [blame] | 557 | err = pskb_expand_head(e->skb, 0, |
| 558 | diff - skb_tailroom(e->skb), |
| 559 | GFP_ATOMIC); |
| 560 | if (err) { |
Patrick McHardy | 1158ba2 | 2006-08-22 00:32:47 -0700 | [diff] [blame] | 561 | printk(KERN_WARNING "nf_queue: OOM " |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 562 | "in mangle, dropping packet\n"); |
Herbert Xu | 2ca7b0a | 2007-10-14 00:39:55 -0700 | [diff] [blame] | 563 | return err; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 564 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 565 | } |
| 566 | skb_put(e->skb, diff); |
| 567 | } |
Herbert Xu | 37d4187 | 2007-10-14 00:39:18 -0700 | [diff] [blame] | 568 | if (!skb_make_writable(e->skb, data_len)) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 569 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 27d7ff4 | 2007-03-31 11:55:19 -0300 | [diff] [blame] | 570 | skb_copy_to_linear_data(e->skb, data, data_len); |
Patrick McHardy | e7dfb09 | 2005-09-06 15:10:00 -0700 | [diff] [blame] | 571 | e->skb->ip_summed = CHECKSUM_NONE; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 572 | return 0; |
| 573 | } |
| 574 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 575 | static int |
| 576 | nfqnl_set_mode(struct nfqnl_instance *queue, |
| 577 | unsigned char mode, unsigned int range) |
| 578 | { |
| 579 | int status; |
| 580 | |
| 581 | spin_lock_bh(&queue->lock); |
| 582 | status = __nfqnl_set_mode(queue, mode, range); |
| 583 | spin_unlock_bh(&queue->lock); |
| 584 | |
| 585 | return status; |
| 586 | } |
| 587 | |
| 588 | static int |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 589 | dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 590 | { |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 591 | if (entry->indev) |
| 592 | if (entry->indev->ifindex == ifindex) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 593 | return 1; |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 594 | if (entry->outdev) |
| 595 | if (entry->outdev->ifindex == ifindex) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 596 | return 1; |
Patrick McHardy | ef47c6a7 | 2006-06-27 03:01:48 -0700 | [diff] [blame] | 597 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 598 | if (entry->skb->nf_bridge) { |
| 599 | if (entry->skb->nf_bridge->physindev && |
| 600 | entry->skb->nf_bridge->physindev->ifindex == ifindex) |
| 601 | return 1; |
| 602 | if (entry->skb->nf_bridge->physoutdev && |
| 603 | entry->skb->nf_bridge->physoutdev->ifindex == ifindex) |
| 604 | return 1; |
| 605 | } |
| 606 | #endif |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | /* drop all packets with either indev or outdev == ifindex from all queue |
| 611 | * instances */ |
| 612 | static void |
| 613 | nfqnl_dev_drop(int ifindex) |
| 614 | { |
| 615 | int i; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 616 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 617 | QDEBUG("entering for ifindex %u\n", ifindex); |
| 618 | |
| 619 | /* this only looks like we have to hold the readlock for a way too long |
| 620 | * time, issue_verdict(), nf_reinject(), ... - but we always only |
| 621 | * issue NF_DROP, which is processed directly in nf_reinject() */ |
| 622 | read_lock_bh(&instances_lock); |
| 623 | |
| 624 | for (i = 0; i < INSTANCE_BUCKETS; i++) { |
| 625 | struct hlist_node *tmp; |
| 626 | struct nfqnl_instance *inst; |
| 627 | struct hlist_head *head = &instance_table[i]; |
| 628 | |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 629 | hlist_for_each_entry(inst, tmp, head, hlist) |
| 630 | nfqnl_flush(inst, dev_cmp, ifindex); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | read_unlock_bh(&instances_lock); |
| 634 | } |
| 635 | |
| 636 | #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0) |
| 637 | |
| 638 | static int |
| 639 | nfqnl_rcv_dev_event(struct notifier_block *this, |
| 640 | unsigned long event, void *ptr) |
| 641 | { |
| 642 | struct net_device *dev = ptr; |
| 643 | |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 644 | if (dev->nd_net != &init_net) |
| 645 | return NOTIFY_DONE; |
| 646 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 647 | /* Drop any packets associated with the downed device */ |
| 648 | if (event == NETDEV_DOWN) |
| 649 | nfqnl_dev_drop(dev->ifindex); |
| 650 | return NOTIFY_DONE; |
| 651 | } |
| 652 | |
| 653 | static struct notifier_block nfqnl_dev_notifier = { |
| 654 | .notifier_call = nfqnl_rcv_dev_event, |
| 655 | }; |
| 656 | |
| 657 | static int |
| 658 | nfqnl_rcv_nl_event(struct notifier_block *this, |
| 659 | unsigned long event, void *ptr) |
| 660 | { |
| 661 | struct netlink_notify *n = ptr; |
| 662 | |
| 663 | if (event == NETLINK_URELEASE && |
| 664 | n->protocol == NETLINK_NETFILTER && n->pid) { |
| 665 | int i; |
| 666 | |
| 667 | /* destroy all instances for this pid */ |
| 668 | write_lock_bh(&instances_lock); |
| 669 | for (i = 0; i < INSTANCE_BUCKETS; i++) { |
| 670 | struct hlist_node *tmp, *t2; |
| 671 | struct nfqnl_instance *inst; |
| 672 | struct hlist_head *head = &instance_table[i]; |
| 673 | |
| 674 | hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) { |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 675 | if ((n->net == &init_net) && |
| 676 | (n->pid == inst->peer_pid)) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 677 | __instance_destroy(inst); |
| 678 | } |
| 679 | } |
| 680 | write_unlock_bh(&instances_lock); |
| 681 | } |
| 682 | return NOTIFY_DONE; |
| 683 | } |
| 684 | |
| 685 | static struct notifier_block nfqnl_rtnl_notifier = { |
| 686 | .notifier_call = nfqnl_rcv_nl_event, |
| 687 | }; |
| 688 | |
Patrick McHardy | 5bf7585 | 2007-09-28 14:39:26 -0700 | [diff] [blame] | 689 | static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = { |
| 690 | [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) }, |
| 691 | [NFQA_MARK] = { .type = NLA_U32 }, |
| 692 | [NFQA_PAYLOAD] = { .type = NLA_UNSPEC }, |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 693 | }; |
| 694 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 695 | static int |
| 696 | nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb, |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 697 | struct nlmsghdr *nlh, struct nlattr *nfqa[]) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 698 | { |
| 699 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); |
| 700 | u_int16_t queue_num = ntohs(nfmsg->res_id); |
| 701 | |
| 702 | struct nfqnl_msg_verdict_hdr *vhdr; |
| 703 | struct nfqnl_instance *queue; |
| 704 | unsigned int verdict; |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 705 | struct nf_queue_entry *entry; |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 706 | int err; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 707 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 708 | queue = instance_lookup_get(queue_num); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 709 | if (!queue) |
| 710 | return -ENODEV; |
| 711 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 712 | if (queue->peer_pid != NETLINK_CB(skb).pid) { |
| 713 | err = -EPERM; |
| 714 | goto err_out_put; |
| 715 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 716 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 717 | if (!nfqa[NFQA_VERDICT_HDR]) { |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 718 | err = -EINVAL; |
| 719 | goto err_out_put; |
| 720 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 721 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 722 | vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 723 | verdict = ntohl(vhdr->verdict); |
| 724 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 725 | if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) { |
| 726 | err = -EINVAL; |
| 727 | goto err_out_put; |
| 728 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 729 | |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 730 | entry = find_dequeue_entry(queue, ntohl(vhdr->id)); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 731 | if (entry == NULL) { |
| 732 | err = -ENOENT; |
| 733 | goto err_out_put; |
| 734 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 735 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 736 | if (nfqa[NFQA_PAYLOAD]) { |
| 737 | if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]), |
| 738 | nla_len(nfqa[NFQA_PAYLOAD]), entry) < 0) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 739 | verdict = NF_DROP; |
| 740 | } |
| 741 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 742 | if (nfqa[NFQA_MARK]) |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 743 | entry->skb->mark = ntohl(*(__be32 *) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 744 | nla_data(nfqa[NFQA_MARK])); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 745 | |
Patrick McHardy | 4b3d15e | 2007-12-05 01:27:02 -0800 | [diff] [blame] | 746 | nf_reinject(entry, verdict); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 747 | instance_put(queue); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 748 | return 0; |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 749 | |
| 750 | err_out_put: |
| 751 | instance_put(queue); |
| 752 | return err; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | static int |
| 756 | nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb, |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 757 | struct nlmsghdr *nlh, struct nlattr *nfqa[]) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 758 | { |
| 759 | return -ENOTSUPP; |
| 760 | } |
| 761 | |
Patrick McHardy | 5bf7585 | 2007-09-28 14:39:26 -0700 | [diff] [blame] | 762 | static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = { |
| 763 | [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) }, |
| 764 | [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) }, |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 765 | }; |
| 766 | |
Patrick McHardy | e3ac529 | 2007-12-05 01:23:57 -0800 | [diff] [blame] | 767 | static const struct nf_queue_handler nfqh = { |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 768 | .name = "nf_queue", |
| 769 | .outfn = &nfqnl_enqueue_packet, |
| 770 | }; |
| 771 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 772 | static int |
| 773 | nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb, |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 774 | struct nlmsghdr *nlh, struct nlattr *nfqa[]) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 775 | { |
| 776 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); |
| 777 | u_int16_t queue_num = ntohs(nfmsg->res_id); |
| 778 | struct nfqnl_instance *queue; |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 779 | int ret = 0; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 780 | |
| 781 | QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type)); |
| 782 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 783 | queue = instance_lookup_get(queue_num); |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 784 | if (nfqa[NFQA_CFG_CMD]) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 785 | struct nfqnl_msg_config_cmd *cmd; |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 786 | cmd = nla_data(nfqa[NFQA_CFG_CMD]); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 787 | QDEBUG("found CFG_CMD\n"); |
| 788 | |
| 789 | switch (cmd->command) { |
| 790 | case NFQNL_CFG_CMD_BIND: |
| 791 | if (queue) |
| 792 | return -EBUSY; |
| 793 | |
| 794 | queue = instance_create(queue_num, NETLINK_CB(skb).pid); |
| 795 | if (!queue) |
| 796 | return -EINVAL; |
| 797 | break; |
| 798 | case NFQNL_CFG_CMD_UNBIND: |
| 799 | if (!queue) |
| 800 | return -ENODEV; |
| 801 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 802 | if (queue->peer_pid != NETLINK_CB(skb).pid) { |
| 803 | ret = -EPERM; |
| 804 | goto out_put; |
| 805 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 806 | |
| 807 | instance_destroy(queue); |
| 808 | break; |
| 809 | case NFQNL_CFG_CMD_PF_BIND: |
| 810 | QDEBUG("registering queue handler for pf=%u\n", |
| 811 | ntohs(cmd->pf)); |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 812 | ret = nf_register_queue_handler(ntohs(cmd->pf), &nfqh); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 813 | break; |
| 814 | case NFQNL_CFG_CMD_PF_UNBIND: |
| 815 | QDEBUG("unregistering queue handler for pf=%u\n", |
| 816 | ntohs(cmd->pf)); |
Yasuyuki Kozakai | ce7663d | 2007-07-07 22:40:08 -0700 | [diff] [blame] | 817 | ret = nf_unregister_queue_handler(ntohs(cmd->pf), &nfqh); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 818 | break; |
| 819 | default: |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 820 | ret = -EINVAL; |
| 821 | break; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 822 | } |
| 823 | } else { |
| 824 | if (!queue) { |
| 825 | QDEBUG("no config command, and no instance ENOENT\n"); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 826 | ret = -ENOENT; |
| 827 | goto out_put; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | if (queue->peer_pid != NETLINK_CB(skb).pid) { |
| 831 | QDEBUG("no config command, and wrong pid\n"); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 832 | ret = -EPERM; |
| 833 | goto out_put; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 837 | if (nfqa[NFQA_CFG_PARAMS]) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 838 | struct nfqnl_msg_config_params *params; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 839 | |
Patrick McHardy | 406dbfc | 2006-03-12 20:32:47 -0800 | [diff] [blame] | 840 | if (!queue) { |
| 841 | ret = -ENOENT; |
| 842 | goto out_put; |
| 843 | } |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 844 | params = nla_data(nfqa[NFQA_CFG_PARAMS]); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 845 | nfqnl_set_mode(queue, params->copy_mode, |
| 846 | ntohl(params->copy_range)); |
| 847 | } |
| 848 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 849 | if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) { |
Eric Leblond | 829e17a | 2006-11-29 02:35:33 +0100 | [diff] [blame] | 850 | __be32 *queue_maxlen; |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 851 | queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]); |
Eric Leblond | 829e17a | 2006-11-29 02:35:33 +0100 | [diff] [blame] | 852 | spin_lock_bh(&queue->lock); |
| 853 | queue->queue_maxlen = ntohl(*queue_maxlen); |
| 854 | spin_unlock_bh(&queue->lock); |
| 855 | } |
| 856 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 857 | out_put: |
| 858 | instance_put(queue); |
| 859 | return ret; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Patrick McHardy | 7c8d4cb | 2007-09-28 14:15:45 -0700 | [diff] [blame] | 862 | static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 863 | [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp, |
Harald Welte | 37d2e7a | 2005-11-14 15:24:59 -0800 | [diff] [blame] | 864 | .attr_count = NFQA_MAX, }, |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 865 | [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict, |
Patrick McHardy | 5bf7585 | 2007-09-28 14:39:26 -0700 | [diff] [blame] | 866 | .attr_count = NFQA_MAX, |
| 867 | .policy = nfqa_verdict_policy }, |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 868 | [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config, |
Patrick McHardy | 5bf7585 | 2007-09-28 14:39:26 -0700 | [diff] [blame] | 869 | .attr_count = NFQA_CFG_MAX, |
| 870 | .policy = nfqa_cfg_policy }, |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 871 | }; |
| 872 | |
Patrick McHardy | 7c8d4cb | 2007-09-28 14:15:45 -0700 | [diff] [blame] | 873 | static const struct nfnetlink_subsystem nfqnl_subsys = { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 874 | .name = "nf_queue", |
| 875 | .subsys_id = NFNL_SUBSYS_QUEUE, |
| 876 | .cb_count = NFQNL_MSG_MAX, |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 877 | .cb = nfqnl_cb, |
| 878 | }; |
| 879 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 880 | #ifdef CONFIG_PROC_FS |
| 881 | struct iter_state { |
| 882 | unsigned int bucket; |
| 883 | }; |
| 884 | |
| 885 | static struct hlist_node *get_first(struct seq_file *seq) |
| 886 | { |
| 887 | struct iter_state *st = seq->private; |
| 888 | |
| 889 | if (!st) |
| 890 | return NULL; |
| 891 | |
| 892 | for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) { |
| 893 | if (!hlist_empty(&instance_table[st->bucket])) |
| 894 | return instance_table[st->bucket].first; |
| 895 | } |
| 896 | return NULL; |
| 897 | } |
| 898 | |
| 899 | static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h) |
| 900 | { |
| 901 | struct iter_state *st = seq->private; |
| 902 | |
| 903 | h = h->next; |
| 904 | while (!h) { |
| 905 | if (++st->bucket >= INSTANCE_BUCKETS) |
| 906 | return NULL; |
| 907 | |
| 908 | h = instance_table[st->bucket].first; |
| 909 | } |
| 910 | return h; |
| 911 | } |
| 912 | |
| 913 | static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos) |
| 914 | { |
| 915 | struct hlist_node *head; |
| 916 | head = get_first(seq); |
| 917 | |
| 918 | if (head) |
| 919 | while (pos && (head = get_next(seq, head))) |
| 920 | pos--; |
| 921 | return pos ? NULL : head; |
| 922 | } |
| 923 | |
| 924 | static void *seq_start(struct seq_file *seq, loff_t *pos) |
| 925 | { |
| 926 | read_lock_bh(&instances_lock); |
| 927 | return get_idx(seq, *pos); |
| 928 | } |
| 929 | |
| 930 | static void *seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 931 | { |
| 932 | (*pos)++; |
| 933 | return get_next(s, v); |
| 934 | } |
| 935 | |
| 936 | static void seq_stop(struct seq_file *s, void *v) |
| 937 | { |
| 938 | read_unlock_bh(&instances_lock); |
| 939 | } |
| 940 | |
| 941 | static int seq_show(struct seq_file *s, void *v) |
| 942 | { |
| 943 | const struct nfqnl_instance *inst = v; |
| 944 | |
| 945 | return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n", |
| 946 | inst->queue_num, |
| 947 | inst->peer_pid, inst->queue_total, |
| 948 | inst->copy_mode, inst->copy_range, |
| 949 | inst->queue_dropped, inst->queue_user_dropped, |
Patrick McHardy | e48b9b2 | 2007-12-05 01:28:10 -0800 | [diff] [blame^] | 950 | inst->id_sequence, |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 951 | atomic_read(&inst->use)); |
| 952 | } |
| 953 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 954 | static const struct seq_operations nfqnl_seq_ops = { |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 955 | .start = seq_start, |
| 956 | .next = seq_next, |
| 957 | .stop = seq_stop, |
| 958 | .show = seq_show, |
| 959 | }; |
| 960 | |
| 961 | static int nfqnl_open(struct inode *inode, struct file *file) |
| 962 | { |
Pavel Emelyanov | e2da591 | 2007-10-10 02:29:58 -0700 | [diff] [blame] | 963 | return seq_open_private(file, &nfqnl_seq_ops, |
| 964 | sizeof(struct iter_state)); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 967 | static const struct file_operations nfqnl_file_ops = { |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 968 | .owner = THIS_MODULE, |
| 969 | .open = nfqnl_open, |
| 970 | .read = seq_read, |
| 971 | .llseek = seq_lseek, |
| 972 | .release = seq_release_private, |
| 973 | }; |
| 974 | |
| 975 | #endif /* PROC_FS */ |
| 976 | |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 977 | static int __init nfnetlink_queue_init(void) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 978 | { |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 979 | int i, status = -ENOMEM; |
| 980 | #ifdef CONFIG_PROC_FS |
| 981 | struct proc_dir_entry *proc_nfqueue; |
| 982 | #endif |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 983 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 984 | for (i = 0; i < INSTANCE_BUCKETS; i++) |
| 985 | INIT_HLIST_HEAD(&instance_table[i]); |
| 986 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 987 | netlink_register_notifier(&nfqnl_rtnl_notifier); |
| 988 | status = nfnetlink_subsys_register(&nfqnl_subsys); |
| 989 | if (status < 0) { |
| 990 | printk(KERN_ERR "nf_queue: failed to create netlink socket\n"); |
| 991 | goto cleanup_netlink_notifier; |
| 992 | } |
| 993 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 994 | #ifdef CONFIG_PROC_FS |
| 995 | proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440, |
| 996 | proc_net_netfilter); |
| 997 | if (!proc_nfqueue) |
| 998 | goto cleanup_subsys; |
| 999 | proc_nfqueue->proc_fops = &nfqnl_file_ops; |
| 1000 | #endif |
| 1001 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1002 | register_netdevice_notifier(&nfqnl_dev_notifier); |
| 1003 | return status; |
| 1004 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1005 | #ifdef CONFIG_PROC_FS |
| 1006 | cleanup_subsys: |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1007 | nfnetlink_subsys_unregister(&nfqnl_subsys); |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 1008 | #endif |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1009 | cleanup_netlink_notifier: |
| 1010 | netlink_unregister_notifier(&nfqnl_rtnl_notifier); |
| 1011 | return status; |
| 1012 | } |
| 1013 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 1014 | static void __exit nfnetlink_queue_fini(void) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1015 | { |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 1016 | nf_unregister_queue_handlers(&nfqh); |
| 1017 | unregister_netdevice_notifier(&nfqnl_dev_notifier); |
| 1018 | #ifdef CONFIG_PROC_FS |
| 1019 | remove_proc_entry("nfnetlink_queue", proc_net_netfilter); |
| 1020 | #endif |
| 1021 | nfnetlink_subsys_unregister(&nfqnl_subsys); |
| 1022 | netlink_unregister_notifier(&nfqnl_rtnl_notifier); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | MODULE_DESCRIPTION("netfilter packet queue handler"); |
| 1026 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); |
| 1027 | MODULE_LICENSE("GPL"); |
| 1028 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE); |
| 1029 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 1030 | module_init(nfnetlink_queue_init); |
| 1031 | module_exit(nfnetlink_queue_fini); |