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