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