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