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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 22 | #include <linux/notifier.h> |
| 23 | #include <linux/netdevice.h> |
| 24 | #include <linux/netfilter.h> |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 25 | #include <linux/proc_fs.h> |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 26 | #include <linux/netfilter_ipv4.h> |
| 27 | #include <linux/netfilter_ipv6.h> |
| 28 | #include <linux/netfilter/nfnetlink.h> |
| 29 | #include <linux/netfilter/nfnetlink_queue.h> |
| 30 | #include <linux/list.h> |
| 31 | #include <net/sock.h> |
Patrick McHardy | c01cd42 | 2007-12-05 01:24:48 -0800 | [diff] [blame] | 32 | #include <net/netfilter/nf_queue.h> |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 33 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 34 | #include <linux/atomic.h> |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 35 | |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 36 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 37 | #include "../bridge/br_private.h" |
| 38 | #endif |
| 39 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 40 | #define NFQNL_QMAX_DEFAULT 1024 |
| 41 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 42 | struct nfqnl_instance { |
| 43 | struct hlist_node hlist; /* global list of queues */ |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 44 | struct rcu_head rcu; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 45 | |
| 46 | int peer_pid; |
| 47 | unsigned int queue_maxlen; |
| 48 | unsigned int copy_range; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 49 | unsigned int queue_dropped; |
| 50 | unsigned int queue_user_dropped; |
| 51 | |
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; |
Eric Dumazet | c463ac9 | 2010-06-09 18:07:06 +0200 | [diff] [blame] | 55 | /* |
| 56 | * Following fields are dirtied for each queued packet, |
| 57 | * keep them in same cache line if possible. |
| 58 | */ |
| 59 | spinlock_t lock; |
| 60 | unsigned int queue_total; |
Eric Dumazet | 5863702a | 2011-07-19 11:44:17 +0200 | [diff] [blame] | 61 | unsigned int id_sequence; /* 'sequence' of pkt ids */ |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 62 | struct list_head queue_list; /* packets in queue */ |
| 63 | }; |
| 64 | |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 65 | typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 66 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 67 | static DEFINE_SPINLOCK(instances_lock); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 68 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 69 | #define INSTANCE_BUCKETS 16 |
Patrick McHardy | 9d6023a | 2007-12-05 01:29:38 -0800 | [diff] [blame] | 70 | static struct hlist_head instance_table[INSTANCE_BUCKETS] __read_mostly; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 71 | |
| 72 | static inline u_int8_t instance_hashfn(u_int16_t queue_num) |
| 73 | { |
| 74 | return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS; |
| 75 | } |
| 76 | |
| 77 | static struct nfqnl_instance * |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 78 | instance_lookup(u_int16_t queue_num) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 79 | { |
| 80 | struct hlist_head *head; |
| 81 | struct hlist_node *pos; |
| 82 | struct nfqnl_instance *inst; |
| 83 | |
| 84 | head = &instance_table[instance_hashfn(queue_num)]; |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 85 | hlist_for_each_entry_rcu(inst, pos, head, hlist) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 86 | if (inst->queue_num == queue_num) |
| 87 | return inst; |
| 88 | } |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | static struct nfqnl_instance * |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 93 | instance_create(u_int16_t queue_num, int pid) |
| 94 | { |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 95 | struct nfqnl_instance *inst; |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 96 | unsigned int h; |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 97 | int err; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 98 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 99 | spin_lock(&instances_lock); |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 100 | if (instance_lookup(queue_num)) { |
| 101 | err = -EEXIST; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 102 | goto out_unlock; |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 103 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 104 | |
Harald Welte | 10dfdc6 | 2005-11-03 19:20:07 +0100 | [diff] [blame] | 105 | inst = kzalloc(sizeof(*inst), GFP_ATOMIC); |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 106 | if (!inst) { |
| 107 | err = -ENOMEM; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 108 | goto out_unlock; |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 109 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 110 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 111 | inst->queue_num = queue_num; |
| 112 | inst->peer_pid = pid; |
| 113 | inst->queue_maxlen = NFQNL_QMAX_DEFAULT; |
| 114 | inst->copy_range = 0xfffff; |
| 115 | inst->copy_mode = NFQNL_COPY_NONE; |
YOSHIFUJI Hideaki | 181a46a | 2006-01-04 13:56:54 -0800 | [diff] [blame] | 116 | spin_lock_init(&inst->lock); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 117 | INIT_LIST_HEAD(&inst->queue_list); |
| 118 | |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 119 | if (!try_module_get(THIS_MODULE)) { |
| 120 | err = -EAGAIN; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 121 | goto out_free; |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 122 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 123 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 124 | h = instance_hashfn(queue_num); |
| 125 | hlist_add_head_rcu(&inst->hlist, &instance_table[h]); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 126 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 127 | spin_unlock(&instances_lock); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 128 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 129 | return inst; |
| 130 | |
| 131 | out_free: |
| 132 | kfree(inst); |
| 133 | out_unlock: |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 134 | spin_unlock(&instances_lock); |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 135 | return ERR_PTR(err); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 138 | static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, |
| 139 | unsigned long data); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 140 | |
| 141 | static void |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 142 | instance_destroy_rcu(struct rcu_head *head) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 143 | { |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 144 | struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance, |
| 145 | rcu); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 146 | |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 147 | nfqnl_flush(inst, NULL, 0); |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 148 | kfree(inst); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 149 | module_put(THIS_MODULE); |
| 150 | } |
| 151 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 152 | static void |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 153 | __instance_destroy(struct nfqnl_instance *inst) |
| 154 | { |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 155 | hlist_del_rcu(&inst->hlist); |
| 156 | call_rcu(&inst->rcu, instance_destroy_rcu); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 159 | static void |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 160 | instance_destroy(struct nfqnl_instance *inst) |
| 161 | { |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 162 | spin_lock(&instances_lock); |
| 163 | __instance_destroy(inst); |
| 164 | spin_unlock(&instances_lock); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 167 | static inline void |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 168 | __enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 169 | { |
Patrick McHardy | 0ac41e8 | 2007-12-05 01:25:03 -0800 | [diff] [blame] | 170 | list_add_tail(&entry->list, &queue->queue_list); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 171 | queue->queue_total++; |
| 172 | } |
| 173 | |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 174 | static void |
| 175 | __dequeue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry) |
| 176 | { |
| 177 | list_del(&entry->list); |
| 178 | queue->queue_total--; |
| 179 | } |
| 180 | |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 181 | static struct nf_queue_entry * |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 182 | find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 183 | { |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 184 | struct nf_queue_entry *entry = NULL, *i; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 185 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 186 | spin_lock_bh(&queue->lock); |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 187 | |
| 188 | list_for_each_entry(i, &queue->queue_list, list) { |
| 189 | if (i->id == id) { |
| 190 | entry = i; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 195 | if (entry) |
| 196 | __dequeue_entry(queue, entry); |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 197 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 198 | spin_unlock_bh(&queue->lock); |
| 199 | |
| 200 | return entry; |
| 201 | } |
| 202 | |
| 203 | static void |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 204 | nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 205 | { |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 206 | struct nf_queue_entry *entry, *next; |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 207 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 208 | spin_lock_bh(&queue->lock); |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 209 | list_for_each_entry_safe(entry, next, &queue->queue_list, list) { |
| 210 | if (!cmpfn || cmpfn(entry, data)) { |
| 211 | list_del(&entry->list); |
| 212 | queue->queue_total--; |
Patrick McHardy | 4b3d15e | 2007-12-05 01:27:02 -0800 | [diff] [blame] | 213 | nf_reinject(entry, NF_DROP); |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 214 | } |
| 215 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 216 | spin_unlock_bh(&queue->lock); |
| 217 | } |
| 218 | |
| 219 | static struct sk_buff * |
| 220 | nfqnl_build_packet_message(struct nfqnl_instance *queue, |
Eric Dumazet | 5863702a | 2011-07-19 11:44:17 +0200 | [diff] [blame] | 221 | struct nf_queue_entry *entry, |
| 222 | __be32 **packet_id_ptr) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 223 | { |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 224 | sk_buff_data_t old_tail; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 225 | size_t size; |
| 226 | size_t data_len = 0; |
| 227 | struct sk_buff *skb; |
Eric Dumazet | 5863702a | 2011-07-19 11:44:17 +0200 | [diff] [blame] | 228 | struct nlattr *nla; |
| 229 | struct nfqnl_msg_packet_hdr *pmsg; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 230 | struct nlmsghdr *nlh; |
| 231 | struct nfgenmsg *nfmsg; |
Jesper Juhl | 3e4ead4 | 2006-01-05 12:15:58 -0800 | [diff] [blame] | 232 | struct sk_buff *entskb = entry->skb; |
| 233 | struct net_device *indev; |
| 234 | struct net_device *outdev; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 235 | |
Eric Leblond | cabaa9b | 2008-03-10 16:41:43 -0700 | [diff] [blame] | 236 | size = NLMSG_SPACE(sizeof(struct nfgenmsg)) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 237 | + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr)) |
| 238 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ |
| 239 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 240 | #ifdef CONFIG_BRIDGE_NETFILTER |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 241 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ |
| 242 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 243 | #endif |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 244 | + nla_total_size(sizeof(u_int32_t)) /* mark */ |
| 245 | + nla_total_size(sizeof(struct nfqnl_msg_packet_hw)) |
| 246 | + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp)); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 247 | |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 248 | outdev = entry->outdev; |
Jesper Juhl | 3e4ead4 | 2006-01-05 12:15:58 -0800 | [diff] [blame] | 249 | |
Eric Dumazet | c463ac9 | 2010-06-09 18:07:06 +0200 | [diff] [blame] | 250 | switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 251 | case NFQNL_COPY_META: |
| 252 | case NFQNL_COPY_NONE: |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 253 | break; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 254 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 255 | case NFQNL_COPY_PACKET: |
Herbert Xu | e9f13ca | 2010-04-08 14:54:35 +0200 | [diff] [blame] | 256 | if (entskb->ip_summed == CHECKSUM_PARTIAL && |
Eric Dumazet | c463ac9 | 2010-06-09 18:07:06 +0200 | [diff] [blame] | 257 | skb_checksum_help(entskb)) |
Patrick McHardy | e7dfb09 | 2005-09-06 15:10:00 -0700 | [diff] [blame] | 258 | return NULL; |
Eric Dumazet | c463ac9 | 2010-06-09 18:07:06 +0200 | [diff] [blame] | 259 | |
| 260 | data_len = ACCESS_ONCE(queue->copy_range); |
| 261 | if (data_len == 0 || data_len > entskb->len) |
Jesper Juhl | 3e4ead4 | 2006-01-05 12:15:58 -0800 | [diff] [blame] | 262 | data_len = entskb->len; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 263 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 264 | size += nla_total_size(data_len); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 265 | break; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 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 | |
Eric Dumazet | 5863702a | 2011-07-19 11:44:17 +0200 | [diff] [blame] | 282 | nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg)); |
| 283 | pmsg = nla_data(nla); |
| 284 | pmsg->hw_protocol = entskb->protocol; |
| 285 | pmsg->hook = entry->hook; |
| 286 | *packet_id_ptr = &pmsg->packet_id; |
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" */ |
Jiri Pirko | f350a0a8 | 2010-06-15 06:50:45 +0000 | [diff] [blame] | 300 | /* rcu_read_lock()ed by __nf_queue */ |
Patrick McHardy | ea3a66f | 2007-12-05 01:30:02 -0800 | [diff] [blame] | 301 | NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV, |
Jiri Pirko | f350a0a8 | 2010-06-15 06:50:45 +0000 | [diff] [blame] | 302 | htonl(br_port_get_rcu(indev)->br->dev->ifindex)); |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 303 | } else { |
| 304 | /* Case 2: indev is bridge group, we need to look for |
| 305 | * physical device (when called from ipv4) */ |
Patrick McHardy | ea3a66f | 2007-12-05 01:30:02 -0800 | [diff] [blame] | 306 | NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV, |
| 307 | htonl(indev->ifindex)); |
| 308 | if (entskb->nf_bridge && entskb->nf_bridge->physindev) |
| 309 | NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSINDEV, |
| 310 | htonl(entskb->nf_bridge->physindev->ifindex)); |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 311 | } |
| 312 | #endif |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Jesper Juhl | 3e4ead4 | 2006-01-05 12:15:58 -0800 | [diff] [blame] | 315 | if (outdev) { |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 316 | #ifndef CONFIG_BRIDGE_NETFILTER |
Patrick McHardy | ea3a66f | 2007-12-05 01:30:02 -0800 | [diff] [blame] | 317 | NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)); |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 318 | #else |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 319 | if (entry->pf == PF_BRIDGE) { |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 320 | /* Case 1: outdev is physical output device, we need to |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 321 | * look for bridge group (when called from |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 322 | * netfilter_bridge) */ |
Patrick McHardy | ea3a66f | 2007-12-05 01:30:02 -0800 | [diff] [blame] | 323 | NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSOUTDEV, |
| 324 | htonl(outdev->ifindex)); |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 325 | /* this is the bridge group "brX" */ |
Jiri Pirko | f350a0a8 | 2010-06-15 06:50:45 +0000 | [diff] [blame] | 326 | /* rcu_read_lock()ed by __nf_queue */ |
Patrick McHardy | ea3a66f | 2007-12-05 01:30:02 -0800 | [diff] [blame] | 327 | NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV, |
Jiri Pirko | f350a0a8 | 2010-06-15 06:50:45 +0000 | [diff] [blame] | 328 | htonl(br_port_get_rcu(outdev)->br->dev->ifindex)); |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 329 | } else { |
| 330 | /* Case 2: outdev is bridge group, we need to look for |
| 331 | * physical output device (when called from ipv4) */ |
Patrick McHardy | ea3a66f | 2007-12-05 01:30:02 -0800 | [diff] [blame] | 332 | NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV, |
| 333 | htonl(outdev->ifindex)); |
| 334 | if (entskb->nf_bridge && entskb->nf_bridge->physoutdev) |
| 335 | NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSOUTDEV, |
| 336 | htonl(entskb->nf_bridge->physoutdev->ifindex)); |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 337 | } |
| 338 | #endif |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Patrick McHardy | ea3a66f | 2007-12-05 01:30:02 -0800 | [diff] [blame] | 341 | if (entskb->mark) |
| 342 | NLA_PUT_BE32(skb, NFQA_MARK, htonl(entskb->mark)); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 343 | |
Nicolas Cavallari | 2c38de4 | 2011-06-16 17:27:04 +0200 | [diff] [blame] | 344 | if (indev && entskb->dev && |
| 345 | entskb->mac_header != entskb->network_header) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 346 | struct nfqnl_msg_packet_hw phw; |
Stephen Hemminger | b95cce3 | 2007-09-26 22:13:38 -0700 | [diff] [blame] | 347 | int len = dev_parse_header(entskb, phw.hw_addr); |
| 348 | if (len) { |
| 349 | phw.hw_addrlen = htons(len); |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 350 | NLA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw); |
Stephen Hemminger | b95cce3 | 2007-09-26 22:13:38 -0700 | [diff] [blame] | 351 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 354 | if (entskb->tstamp.tv64) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 355 | struct nfqnl_msg_packet_timestamp ts; |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 356 | struct timeval tv = ktime_to_timeval(entskb->tstamp); |
| 357 | ts.sec = cpu_to_be64(tv.tv_sec); |
| 358 | ts.usec = cpu_to_be64(tv.tv_usec); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 359 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 360 | NLA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | if (data_len) { |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 364 | struct nlattr *nla; |
Eric Dumazet | ca7c48c | 2008-01-31 03:53:27 -0800 | [diff] [blame] | 365 | int sz = nla_attr_size(data_len); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 366 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 367 | if (skb_tailroom(skb) < nla_total_size(data_len)) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 368 | printk(KERN_WARNING "nf_queue: no tailroom!\n"); |
| 369 | goto nlmsg_failure; |
| 370 | } |
| 371 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 372 | nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len)); |
| 373 | nla->nla_type = NFQA_PAYLOAD; |
Eric Dumazet | ca7c48c | 2008-01-31 03:53:27 -0800 | [diff] [blame] | 374 | nla->nla_len = sz; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 375 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 376 | if (skb_copy_bits(entskb, 0, nla_data(nla), data_len)) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 377 | BUG(); |
| 378 | } |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 379 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 380 | nlh->nlmsg_len = skb->tail - old_tail; |
| 381 | return skb; |
| 382 | |
| 383 | nlmsg_failure: |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 384 | nla_put_failure: |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 385 | if (skb) |
| 386 | kfree_skb(skb); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 387 | if (net_ratelimit()) |
| 388 | printk(KERN_ERR "nf_queue: error creating packet message\n"); |
| 389 | return NULL; |
| 390 | } |
| 391 | |
| 392 | static int |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 393 | nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 394 | { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 395 | struct sk_buff *nskb; |
| 396 | struct nfqnl_instance *queue; |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 397 | int err = -ENOBUFS; |
Eric Dumazet | 5863702a | 2011-07-19 11:44:17 +0200 | [diff] [blame] | 398 | __be32 *packet_id_ptr; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 399 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 400 | /* rcu_read_lock()ed by nf_hook_slow() */ |
| 401 | queue = instance_lookup(queuenum); |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 402 | if (!queue) { |
| 403 | err = -ESRCH; |
Patrick McHardy | 0ef0f46 | 2007-12-05 01:31:01 -0800 | [diff] [blame] | 404 | goto err_out; |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 405 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 406 | |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 407 | if (queue->copy_mode == NFQNL_COPY_NONE) { |
| 408 | err = -EINVAL; |
Patrick McHardy | 0ef0f46 | 2007-12-05 01:31:01 -0800 | [diff] [blame] | 409 | goto err_out; |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 410 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 411 | |
Eric Dumazet | 5863702a | 2011-07-19 11:44:17 +0200 | [diff] [blame] | 412 | nskb = nfqnl_build_packet_message(queue, entry, &packet_id_ptr); |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 413 | if (nskb == NULL) { |
| 414 | err = -ENOMEM; |
Patrick McHardy | 0ef0f46 | 2007-12-05 01:31:01 -0800 | [diff] [blame] | 415 | goto err_out; |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 416 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 417 | spin_lock_bh(&queue->lock); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 418 | |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 419 | if (!queue->peer_pid) { |
| 420 | err = -EINVAL; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 421 | goto err_out_free_nskb; |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 422 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 423 | if (queue->queue_total >= queue->queue_maxlen) { |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 424 | queue->queue_dropped++; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 425 | if (net_ratelimit()) |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 426 | printk(KERN_WARNING "nf_queue: full at %d entries, " |
Eric Leblond | a5d896ad | 2010-01-18 09:44:39 +0100 | [diff] [blame] | 427 | "dropping packets(s).\n", |
| 428 | queue->queue_total); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 429 | goto err_out_free_nskb; |
| 430 | } |
Eric Dumazet | 5863702a | 2011-07-19 11:44:17 +0200 | [diff] [blame] | 431 | entry->id = ++queue->id_sequence; |
| 432 | *packet_id_ptr = htonl(entry->id); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 433 | |
| 434 | /* nfnetlink_unicast will either free the nskb or add it to a socket */ |
Alexey Dobriyan | cd8c20b | 2010-01-13 16:02:14 +0100 | [diff] [blame] | 435 | err = nfnetlink_unicast(nskb, &init_net, queue->peer_pid, MSG_DONTWAIT); |
Patrick McHardy | 0ef0f46 | 2007-12-05 01:31:01 -0800 | [diff] [blame] | 436 | if (err < 0) { |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 437 | queue->queue_user_dropped++; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 438 | goto err_out_unlock; |
| 439 | } |
| 440 | |
| 441 | __enqueue_entry(queue, entry); |
| 442 | |
| 443 | spin_unlock_bh(&queue->lock); |
Patrick McHardy | 0ef0f46 | 2007-12-05 01:31:01 -0800 | [diff] [blame] | 444 | return 0; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 445 | |
| 446 | err_out_free_nskb: |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 447 | kfree_skb(nskb); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 448 | err_out_unlock: |
| 449 | spin_unlock_bh(&queue->lock); |
Patrick McHardy | 0ef0f46 | 2007-12-05 01:31:01 -0800 | [diff] [blame] | 450 | err_out: |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 451 | return err; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static int |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 455 | nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 456 | { |
Patrick McHardy | e2b58a6 | 2008-02-19 17:17:52 -0800 | [diff] [blame] | 457 | struct sk_buff *nskb; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 458 | int diff; |
| 459 | |
| 460 | diff = data_len - e->skb->len; |
Patrick McHardy | d8a585d | 2006-11-14 19:48:09 -0800 | [diff] [blame] | 461 | if (diff < 0) { |
| 462 | if (pskb_trim(e->skb, data_len)) |
| 463 | return -ENOMEM; |
| 464 | } else if (diff > 0) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 465 | if (data_len > 0xFFFF) |
| 466 | return -EINVAL; |
| 467 | if (diff > skb_tailroom(e->skb)) { |
Arnaud Ebalard | 9a732ed | 2008-04-29 03:16:34 -0700 | [diff] [blame] | 468 | nskb = skb_copy_expand(e->skb, skb_headroom(e->skb), |
| 469 | diff, GFP_ATOMIC); |
Patrick McHardy | e2b58a6 | 2008-02-19 17:17:52 -0800 | [diff] [blame] | 470 | if (!nskb) { |
Patrick McHardy | 1158ba2 | 2006-08-22 00:32:47 -0700 | [diff] [blame] | 471 | printk(KERN_WARNING "nf_queue: OOM " |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 472 | "in mangle, dropping packet\n"); |
Patrick McHardy | e2b58a6 | 2008-02-19 17:17:52 -0800 | [diff] [blame] | 473 | return -ENOMEM; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 474 | } |
Patrick McHardy | e2b58a6 | 2008-02-19 17:17:52 -0800 | [diff] [blame] | 475 | kfree_skb(e->skb); |
| 476 | e->skb = nskb; |
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 | ef47c6a | 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 | |
YOSHIFUJI Hideaki | 721499e | 2008-07-19 22:34:43 -0700 | [diff] [blame] | 570 | if (!net_eq(dev_net(dev), &init_net)) |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 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 | |
Patrick McHardy | dee5817 | 2009-11-06 17:04:00 +0100 | [diff] [blame] | 589 | if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 590 | int i; |
| 591 | |
| 592 | /* destroy all instances for this pid */ |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 593 | spin_lock(&instances_lock); |
| 594 | for (i = 0; i < INSTANCE_BUCKETS; i++) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 595 | struct hlist_node *tmp, *t2; |
| 596 | struct nfqnl_instance *inst; |
| 597 | struct hlist_head *head = &instance_table[i]; |
| 598 | |
| 599 | hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) { |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 600 | if ((n->net == &init_net) && |
| 601 | (n->pid == inst->peer_pid)) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 602 | __instance_destroy(inst); |
| 603 | } |
| 604 | } |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 605 | spin_unlock(&instances_lock); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 606 | } |
| 607 | return NOTIFY_DONE; |
| 608 | } |
| 609 | |
| 610 | static struct notifier_block nfqnl_rtnl_notifier = { |
| 611 | .notifier_call = nfqnl_rcv_nl_event, |
| 612 | }; |
| 613 | |
Patrick McHardy | 5bf7585 | 2007-09-28 14:39:26 -0700 | [diff] [blame] | 614 | static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = { |
| 615 | [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) }, |
| 616 | [NFQA_MARK] = { .type = NLA_U32 }, |
| 617 | [NFQA_PAYLOAD] = { .type = NLA_UNSPEC }, |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 618 | }; |
| 619 | |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 620 | static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = { |
| 621 | [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) }, |
| 622 | [NFQA_MARK] = { .type = NLA_U32 }, |
| 623 | }; |
| 624 | |
| 625 | static struct nfqnl_instance *verdict_instance_lookup(u16 queue_num, int nlpid) |
| 626 | { |
| 627 | struct nfqnl_instance *queue; |
| 628 | |
| 629 | queue = instance_lookup(queue_num); |
| 630 | if (!queue) |
| 631 | return ERR_PTR(-ENODEV); |
| 632 | |
| 633 | if (queue->peer_pid != nlpid) |
| 634 | return ERR_PTR(-EPERM); |
| 635 | |
| 636 | return queue; |
| 637 | } |
| 638 | |
| 639 | static struct nfqnl_msg_verdict_hdr* |
| 640 | verdicthdr_get(const struct nlattr * const nfqa[]) |
| 641 | { |
| 642 | struct nfqnl_msg_verdict_hdr *vhdr; |
| 643 | unsigned int verdict; |
| 644 | |
| 645 | if (!nfqa[NFQA_VERDICT_HDR]) |
| 646 | return NULL; |
| 647 | |
| 648 | vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]); |
Florian Westphal | c667523 | 2011-08-30 15:01:20 +0200 | [diff] [blame] | 649 | verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK; |
| 650 | if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN) |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 651 | return NULL; |
| 652 | return vhdr; |
| 653 | } |
| 654 | |
| 655 | static int nfq_id_after(unsigned int id, unsigned int max) |
| 656 | { |
| 657 | return (int)(id - max) > 0; |
| 658 | } |
| 659 | |
| 660 | static int |
| 661 | nfqnl_recv_verdict_batch(struct sock *ctnl, struct sk_buff *skb, |
| 662 | const struct nlmsghdr *nlh, |
| 663 | const struct nlattr * const nfqa[]) |
| 664 | { |
| 665 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); |
| 666 | struct nf_queue_entry *entry, *tmp; |
| 667 | unsigned int verdict, maxid; |
| 668 | struct nfqnl_msg_verdict_hdr *vhdr; |
| 669 | struct nfqnl_instance *queue; |
| 670 | LIST_HEAD(batch_list); |
| 671 | u16 queue_num = ntohs(nfmsg->res_id); |
| 672 | |
| 673 | queue = verdict_instance_lookup(queue_num, NETLINK_CB(skb).pid); |
| 674 | if (IS_ERR(queue)) |
| 675 | return PTR_ERR(queue); |
| 676 | |
| 677 | vhdr = verdicthdr_get(nfqa); |
| 678 | if (!vhdr) |
| 679 | return -EINVAL; |
| 680 | |
| 681 | verdict = ntohl(vhdr->verdict); |
| 682 | maxid = ntohl(vhdr->id); |
| 683 | |
| 684 | spin_lock_bh(&queue->lock); |
| 685 | |
| 686 | list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) { |
| 687 | if (nfq_id_after(entry->id, maxid)) |
| 688 | break; |
| 689 | __dequeue_entry(queue, entry); |
| 690 | list_add_tail(&entry->list, &batch_list); |
| 691 | } |
| 692 | |
| 693 | spin_unlock_bh(&queue->lock); |
| 694 | |
| 695 | if (list_empty(&batch_list)) |
| 696 | return -ENOENT; |
| 697 | |
| 698 | list_for_each_entry_safe(entry, tmp, &batch_list, list) { |
| 699 | if (nfqa[NFQA_MARK]) |
| 700 | entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK])); |
| 701 | nf_reinject(entry, verdict); |
| 702 | } |
| 703 | return 0; |
| 704 | } |
| 705 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 706 | static int |
| 707 | nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb, |
Patrick McHardy | 3993832 | 2009-08-25 16:07:58 +0200 | [diff] [blame] | 708 | const struct nlmsghdr *nlh, |
| 709 | const struct nlattr * const nfqa[]) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 710 | { |
| 711 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); |
| 712 | u_int16_t queue_num = ntohs(nfmsg->res_id); |
| 713 | |
| 714 | struct nfqnl_msg_verdict_hdr *vhdr; |
| 715 | struct nfqnl_instance *queue; |
| 716 | unsigned int verdict; |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 717 | struct nf_queue_entry *entry; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 718 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 719 | queue = instance_lookup(queue_num); |
Eric Dumazet | 84a797d | 2011-07-18 16:08:27 +0200 | [diff] [blame] | 720 | if (!queue) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 721 | |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 722 | queue = verdict_instance_lookup(queue_num, NETLINK_CB(skb).pid); |
| 723 | if (IS_ERR(queue)) |
| 724 | return PTR_ERR(queue); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 725 | |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 726 | vhdr = verdicthdr_get(nfqa); |
| 727 | if (!vhdr) |
Eric Dumazet | 84a797d | 2011-07-18 16:08:27 +0200 | [diff] [blame] | 728 | return -EINVAL; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 729 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 730 | verdict = ntohl(vhdr->verdict); |
| 731 | |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 732 | entry = find_dequeue_entry(queue, ntohl(vhdr->id)); |
Eric Dumazet | 84a797d | 2011-07-18 16:08:27 +0200 | [diff] [blame] | 733 | if (entry == NULL) |
| 734 | return -ENOENT; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 735 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 736 | if (nfqa[NFQA_PAYLOAD]) { |
| 737 | if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]), |
| 738 | nla_len(nfqa[NFQA_PAYLOAD]), entry) < 0) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 739 | verdict = NF_DROP; |
| 740 | } |
| 741 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 742 | if (nfqa[NFQA_MARK]) |
Patrick McHardy | ea3a66f | 2007-12-05 01:30:02 -0800 | [diff] [blame] | 743 | entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK])); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 744 | |
Patrick McHardy | 4b3d15e | 2007-12-05 01:27:02 -0800 | [diff] [blame] | 745 | nf_reinject(entry, verdict); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | static int |
| 750 | nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb, |
Patrick McHardy | 3993832 | 2009-08-25 16:07:58 +0200 | [diff] [blame] | 751 | const struct nlmsghdr *nlh, |
| 752 | const struct nlattr * const nfqa[]) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 753 | { |
| 754 | return -ENOTSUPP; |
| 755 | } |
| 756 | |
Patrick McHardy | 5bf7585 | 2007-09-28 14:39:26 -0700 | [diff] [blame] | 757 | static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = { |
| 758 | [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) }, |
| 759 | [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) }, |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 760 | }; |
| 761 | |
Patrick McHardy | e3ac529 | 2007-12-05 01:23:57 -0800 | [diff] [blame] | 762 | static const struct nf_queue_handler nfqh = { |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 763 | .name = "nf_queue", |
| 764 | .outfn = &nfqnl_enqueue_packet, |
| 765 | }; |
| 766 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 767 | static int |
| 768 | nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb, |
Patrick McHardy | 3993832 | 2009-08-25 16:07:58 +0200 | [diff] [blame] | 769 | const struct nlmsghdr *nlh, |
| 770 | const struct nlattr * const nfqa[]) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 771 | { |
| 772 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); |
| 773 | u_int16_t queue_num = ntohs(nfmsg->res_id); |
| 774 | struct nfqnl_instance *queue; |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 775 | struct nfqnl_msg_config_cmd *cmd = NULL; |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 776 | int ret = 0; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 777 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 778 | if (nfqa[NFQA_CFG_CMD]) { |
| 779 | cmd = nla_data(nfqa[NFQA_CFG_CMD]); |
| 780 | |
| 781 | /* Commands without queue context - might sleep */ |
| 782 | switch (cmd->command) { |
| 783 | case NFQNL_CFG_CMD_PF_BIND: |
Patrick McHardy | 914afea | 2008-03-10 16:44:36 -0700 | [diff] [blame] | 784 | return nf_register_queue_handler(ntohs(cmd->pf), |
| 785 | &nfqh); |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 786 | case NFQNL_CFG_CMD_PF_UNBIND: |
Patrick McHardy | 914afea | 2008-03-10 16:44:36 -0700 | [diff] [blame] | 787 | return nf_unregister_queue_handler(ntohs(cmd->pf), |
| 788 | &nfqh); |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 789 | } |
Patrick McHardy | a3c8e7f | 2007-12-05 01:28:30 -0800 | [diff] [blame] | 790 | } |
| 791 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 792 | rcu_read_lock(); |
| 793 | queue = instance_lookup(queue_num); |
| 794 | if (queue && queue->peer_pid != NETLINK_CB(skb).pid) { |
| 795 | ret = -EPERM; |
| 796 | goto err_out_unlock; |
| 797 | } |
Patrick McHardy | a3c8e7f | 2007-12-05 01:28:30 -0800 | [diff] [blame] | 798 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 799 | if (cmd != NULL) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 800 | switch (cmd->command) { |
| 801 | case NFQNL_CFG_CMD_BIND: |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 802 | if (queue) { |
| 803 | ret = -EBUSY; |
| 804 | goto err_out_unlock; |
| 805 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 806 | queue = instance_create(queue_num, NETLINK_CB(skb).pid); |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 807 | if (IS_ERR(queue)) { |
| 808 | ret = PTR_ERR(queue); |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 809 | goto err_out_unlock; |
| 810 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 811 | break; |
| 812 | case NFQNL_CFG_CMD_UNBIND: |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 813 | if (!queue) { |
| 814 | ret = -ENODEV; |
| 815 | goto err_out_unlock; |
| 816 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 817 | instance_destroy(queue); |
| 818 | break; |
| 819 | case NFQNL_CFG_CMD_PF_BIND: |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 820 | case NFQNL_CFG_CMD_PF_UNBIND: |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 821 | break; |
| 822 | default: |
Patrick McHardy | cd21f0a | 2007-12-17 22:40:19 -0800 | [diff] [blame] | 823 | ret = -ENOTSUPP; |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 824 | break; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 825 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 826 | } |
| 827 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 828 | if (nfqa[NFQA_CFG_PARAMS]) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 829 | struct nfqnl_msg_config_params *params; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 830 | |
Patrick McHardy | 406dbfc | 2006-03-12 20:32:47 -0800 | [diff] [blame] | 831 | if (!queue) { |
Patrick McHardy | a3c8e7f | 2007-12-05 01:28:30 -0800 | [diff] [blame] | 832 | ret = -ENODEV; |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 833 | goto err_out_unlock; |
Patrick McHardy | 406dbfc | 2006-03-12 20:32:47 -0800 | [diff] [blame] | 834 | } |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 835 | params = nla_data(nfqa[NFQA_CFG_PARAMS]); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 836 | nfqnl_set_mode(queue, params->copy_mode, |
| 837 | ntohl(params->copy_range)); |
| 838 | } |
| 839 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 840 | if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) { |
Eric Leblond | 829e17a | 2006-11-29 02:35:33 +0100 | [diff] [blame] | 841 | __be32 *queue_maxlen; |
Patrick McHardy | a3c8e7f | 2007-12-05 01:28:30 -0800 | [diff] [blame] | 842 | |
| 843 | if (!queue) { |
| 844 | ret = -ENODEV; |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 845 | goto err_out_unlock; |
Patrick McHardy | a3c8e7f | 2007-12-05 01:28:30 -0800 | [diff] [blame] | 846 | } |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 847 | queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]); |
Eric Leblond | 829e17a | 2006-11-29 02:35:33 +0100 | [diff] [blame] | 848 | spin_lock_bh(&queue->lock); |
| 849 | queue->queue_maxlen = ntohl(*queue_maxlen); |
| 850 | spin_unlock_bh(&queue->lock); |
| 851 | } |
| 852 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 853 | err_out_unlock: |
| 854 | rcu_read_unlock(); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 855 | return ret; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Patrick McHardy | 7c8d4cb | 2007-09-28 14:15:45 -0700 | [diff] [blame] | 858 | static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = { |
Eric Dumazet | 84a797d | 2011-07-18 16:08:27 +0200 | [diff] [blame] | 859 | [NFQNL_MSG_PACKET] = { .call_rcu = nfqnl_recv_unsupp, |
Harald Welte | 37d2e7a | 2005-11-14 15:24:59 -0800 | [diff] [blame] | 860 | .attr_count = NFQA_MAX, }, |
Eric Dumazet | 84a797d | 2011-07-18 16:08:27 +0200 | [diff] [blame] | 861 | [NFQNL_MSG_VERDICT] = { .call_rcu = nfqnl_recv_verdict, |
Patrick McHardy | 5bf7585 | 2007-09-28 14:39:26 -0700 | [diff] [blame] | 862 | .attr_count = NFQA_MAX, |
| 863 | .policy = nfqa_verdict_policy }, |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 864 | [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config, |
Patrick McHardy | 5bf7585 | 2007-09-28 14:39:26 -0700 | [diff] [blame] | 865 | .attr_count = NFQA_CFG_MAX, |
| 866 | .policy = nfqa_cfg_policy }, |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 867 | [NFQNL_MSG_VERDICT_BATCH]={ .call_rcu = nfqnl_recv_verdict_batch, |
| 868 | .attr_count = NFQA_MAX, |
| 869 | .policy = nfqa_verdict_batch_policy }, |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 870 | }; |
| 871 | |
Patrick McHardy | 7c8d4cb | 2007-09-28 14:15:45 -0700 | [diff] [blame] | 872 | static const struct nfnetlink_subsystem nfqnl_subsys = { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 873 | .name = "nf_queue", |
| 874 | .subsys_id = NFNL_SUBSYS_QUEUE, |
| 875 | .cb_count = NFQNL_MSG_MAX, |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 876 | .cb = nfqnl_cb, |
| 877 | }; |
| 878 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 879 | #ifdef CONFIG_PROC_FS |
| 880 | struct iter_state { |
| 881 | unsigned int bucket; |
| 882 | }; |
| 883 | |
| 884 | static struct hlist_node *get_first(struct seq_file *seq) |
| 885 | { |
| 886 | struct iter_state *st = seq->private; |
| 887 | |
| 888 | if (!st) |
| 889 | return NULL; |
| 890 | |
| 891 | for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) { |
| 892 | if (!hlist_empty(&instance_table[st->bucket])) |
| 893 | return instance_table[st->bucket].first; |
| 894 | } |
| 895 | return NULL; |
| 896 | } |
| 897 | |
| 898 | static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h) |
| 899 | { |
| 900 | struct iter_state *st = seq->private; |
| 901 | |
| 902 | h = h->next; |
| 903 | while (!h) { |
| 904 | if (++st->bucket >= INSTANCE_BUCKETS) |
| 905 | return NULL; |
| 906 | |
| 907 | h = instance_table[st->bucket].first; |
| 908 | } |
| 909 | return h; |
| 910 | } |
| 911 | |
| 912 | static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos) |
| 913 | { |
| 914 | struct hlist_node *head; |
| 915 | head = get_first(seq); |
| 916 | |
| 917 | if (head) |
| 918 | while (pos && (head = get_next(seq, head))) |
| 919 | pos--; |
| 920 | return pos ? NULL : head; |
| 921 | } |
| 922 | |
| 923 | static void *seq_start(struct seq_file *seq, loff_t *pos) |
Eric Dumazet | ca7c48c | 2008-01-31 03:53:27 -0800 | [diff] [blame] | 924 | __acquires(instances_lock) |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 925 | { |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 926 | spin_lock(&instances_lock); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 927 | return get_idx(seq, *pos); |
| 928 | } |
| 929 | |
| 930 | static void *seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 931 | { |
| 932 | (*pos)++; |
| 933 | return get_next(s, v); |
| 934 | } |
| 935 | |
| 936 | static void seq_stop(struct seq_file *s, void *v) |
Eric Dumazet | ca7c48c | 2008-01-31 03:53:27 -0800 | [diff] [blame] | 937 | __releases(instances_lock) |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 938 | { |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 939 | spin_unlock(&instances_lock); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | static int seq_show(struct seq_file *s, void *v) |
| 943 | { |
| 944 | const struct nfqnl_instance *inst = v; |
| 945 | |
| 946 | return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n", |
| 947 | inst->queue_num, |
| 948 | inst->peer_pid, inst->queue_total, |
| 949 | inst->copy_mode, inst->copy_range, |
| 950 | inst->queue_dropped, inst->queue_user_dropped, |
Eric Dumazet | 5863702a | 2011-07-19 11:44:17 +0200 | [diff] [blame] | 951 | inst->id_sequence, 1); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 954 | static const struct seq_operations nfqnl_seq_ops = { |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 955 | .start = seq_start, |
| 956 | .next = seq_next, |
| 957 | .stop = seq_stop, |
| 958 | .show = seq_show, |
| 959 | }; |
| 960 | |
| 961 | static int nfqnl_open(struct inode *inode, struct file *file) |
| 962 | { |
Pavel Emelyanov | e2da591 | 2007-10-10 02:29:58 -0700 | [diff] [blame] | 963 | return seq_open_private(file, &nfqnl_seq_ops, |
| 964 | sizeof(struct iter_state)); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 967 | static const struct file_operations nfqnl_file_ops = { |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 968 | .owner = THIS_MODULE, |
| 969 | .open = nfqnl_open, |
| 970 | .read = seq_read, |
| 971 | .llseek = seq_lseek, |
| 972 | .release = seq_release_private, |
| 973 | }; |
| 974 | |
| 975 | #endif /* PROC_FS */ |
| 976 | |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 977 | static int __init nfnetlink_queue_init(void) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 978 | { |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 979 | int i, status = -ENOMEM; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 980 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 981 | for (i = 0; i < INSTANCE_BUCKETS; i++) |
| 982 | INIT_HLIST_HEAD(&instance_table[i]); |
| 983 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 984 | netlink_register_notifier(&nfqnl_rtnl_notifier); |
| 985 | status = nfnetlink_subsys_register(&nfqnl_subsys); |
| 986 | if (status < 0) { |
| 987 | printk(KERN_ERR "nf_queue: failed to create netlink socket\n"); |
| 988 | goto cleanup_netlink_notifier; |
| 989 | } |
| 990 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 991 | #ifdef CONFIG_PROC_FS |
Denis V. Lunev | 8eeee8b | 2008-03-27 16:55:53 -0700 | [diff] [blame] | 992 | if (!proc_create("nfnetlink_queue", 0440, |
| 993 | proc_net_netfilter, &nfqnl_file_ops)) |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 994 | goto cleanup_subsys; |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 995 | #endif |
| 996 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 997 | register_netdevice_notifier(&nfqnl_dev_notifier); |
| 998 | return status; |
| 999 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1000 | #ifdef CONFIG_PROC_FS |
| 1001 | cleanup_subsys: |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1002 | nfnetlink_subsys_unregister(&nfqnl_subsys); |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 1003 | #endif |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1004 | cleanup_netlink_notifier: |
| 1005 | netlink_unregister_notifier(&nfqnl_rtnl_notifier); |
| 1006 | return status; |
| 1007 | } |
| 1008 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 1009 | static void __exit nfnetlink_queue_fini(void) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1010 | { |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 1011 | nf_unregister_queue_handlers(&nfqh); |
| 1012 | unregister_netdevice_notifier(&nfqnl_dev_notifier); |
| 1013 | #ifdef CONFIG_PROC_FS |
| 1014 | remove_proc_entry("nfnetlink_queue", proc_net_netfilter); |
| 1015 | #endif |
| 1016 | nfnetlink_subsys_unregister(&nfqnl_subsys); |
| 1017 | netlink_unregister_notifier(&nfqnl_rtnl_notifier); |
Jesper Dangaard Brouer | 67137f3 | 2009-06-08 03:11:33 +0000 | [diff] [blame] | 1018 | |
| 1019 | rcu_barrier(); /* Wait for completion of call_rcu()'s */ |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | MODULE_DESCRIPTION("netfilter packet queue handler"); |
| 1023 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); |
| 1024 | MODULE_LICENSE("GPL"); |
| 1025 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE); |
| 1026 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 1027 | module_init(nfnetlink_queue_init); |
| 1028 | module_exit(nfnetlink_queue_fini); |