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