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> |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 33 | #include <net/netns/generic.h> |
Pablo Neira Ayuso | 7c62234 | 2012-06-19 02:10:57 +0200 | [diff] [blame] | 34 | #include <net/netfilter/nfnetlink_queue.h> |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 35 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 36 | #include <linux/atomic.h> |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 37 | |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 38 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 39 | #include "../bridge/br_private.h" |
| 40 | #endif |
| 41 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 42 | #define NFQNL_QMAX_DEFAULT 1024 |
| 43 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 44 | struct nfqnl_instance { |
| 45 | struct hlist_node hlist; /* global list of queues */ |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 46 | struct rcu_head rcu; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 47 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 48 | int peer_portid; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 49 | unsigned int queue_maxlen; |
| 50 | unsigned int copy_range; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 51 | unsigned int queue_dropped; |
| 52 | unsigned int queue_user_dropped; |
| 53 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 54 | |
| 55 | u_int16_t queue_num; /* number of this queue */ |
| 56 | u_int8_t copy_mode; |
Krishna Kumar | fdb694a | 2012-05-24 03:56:44 +0000 | [diff] [blame] | 57 | u_int32_t flags; /* Set using NFQA_CFG_FLAGS */ |
Eric Dumazet | c463ac9 | 2010-06-09 18:07:06 +0200 | [diff] [blame] | 58 | /* |
| 59 | * Following fields are dirtied for each queued packet, |
| 60 | * keep them in same cache line if possible. |
| 61 | */ |
| 62 | spinlock_t lock; |
| 63 | unsigned int queue_total; |
Eric Dumazet | 5863702 | 2011-07-19 11:44:17 +0200 | [diff] [blame] | 64 | unsigned int id_sequence; /* 'sequence' of pkt ids */ |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 65 | struct list_head queue_list; /* packets in queue */ |
| 66 | }; |
| 67 | |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 68 | typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 69 | |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 70 | static int nfnl_queue_net_id __read_mostly; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 71 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 72 | #define INSTANCE_BUCKETS 16 |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 73 | struct nfnl_queue_net { |
| 74 | spinlock_t instances_lock; |
| 75 | struct hlist_head instance_table[INSTANCE_BUCKETS]; |
| 76 | }; |
| 77 | |
| 78 | static struct nfnl_queue_net *nfnl_queue_pernet(struct net *net) |
| 79 | { |
| 80 | return net_generic(net, nfnl_queue_net_id); |
| 81 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 82 | |
| 83 | static inline u_int8_t instance_hashfn(u_int16_t queue_num) |
| 84 | { |
Pablo Neira Ayuso | 1cdb090 | 2013-03-14 06:03:17 +0000 | [diff] [blame] | 85 | return ((queue_num >> 8) ^ queue_num) % INSTANCE_BUCKETS; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static struct nfqnl_instance * |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 89 | instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 90 | { |
| 91 | struct hlist_head *head; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 92 | struct nfqnl_instance *inst; |
| 93 | |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 94 | head = &q->instance_table[instance_hashfn(queue_num)]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 95 | hlist_for_each_entry_rcu(inst, head, hlist) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 96 | if (inst->queue_num == queue_num) |
| 97 | return inst; |
| 98 | } |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | static struct nfqnl_instance * |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 103 | instance_create(struct nfnl_queue_net *q, u_int16_t queue_num, |
| 104 | int portid) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 105 | { |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 106 | struct nfqnl_instance *inst; |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 107 | unsigned int h; |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 108 | int err; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 109 | |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 110 | spin_lock(&q->instances_lock); |
| 111 | if (instance_lookup(q, queue_num)) { |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 112 | err = -EEXIST; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 113 | goto out_unlock; |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 114 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 115 | |
Harald Welte | 10dfdc6 | 2005-11-03 19:20:07 +0100 | [diff] [blame] | 116 | inst = kzalloc(sizeof(*inst), GFP_ATOMIC); |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 117 | if (!inst) { |
| 118 | err = -ENOMEM; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 119 | goto out_unlock; |
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 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 122 | inst->queue_num = queue_num; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 123 | inst->peer_portid = portid; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 124 | inst->queue_maxlen = NFQNL_QMAX_DEFAULT; |
Pablo Neira Ayuso | bae99f7 | 2013-03-14 06:03:18 +0000 | [diff] [blame] | 125 | inst->copy_range = 0xffff; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 126 | inst->copy_mode = NFQNL_COPY_NONE; |
YOSHIFUJI Hideaki | 181a46a | 2006-01-04 13:56:54 -0800 | [diff] [blame] | 127 | spin_lock_init(&inst->lock); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 128 | INIT_LIST_HEAD(&inst->queue_list); |
| 129 | |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 130 | if (!try_module_get(THIS_MODULE)) { |
| 131 | err = -EAGAIN; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 132 | goto out_free; |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 133 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 134 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 135 | h = instance_hashfn(queue_num); |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 136 | hlist_add_head_rcu(&inst->hlist, &q->instance_table[h]); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 137 | |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 138 | spin_unlock(&q->instances_lock); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 139 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 140 | return inst; |
| 141 | |
| 142 | out_free: |
| 143 | kfree(inst); |
| 144 | out_unlock: |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 145 | spin_unlock(&q->instances_lock); |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 146 | return ERR_PTR(err); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 149 | static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, |
| 150 | unsigned long data); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 151 | |
| 152 | static void |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 153 | instance_destroy_rcu(struct rcu_head *head) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 154 | { |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 155 | struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance, |
| 156 | rcu); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 157 | |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 158 | nfqnl_flush(inst, NULL, 0); |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 159 | kfree(inst); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 160 | module_put(THIS_MODULE); |
| 161 | } |
| 162 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 163 | static void |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 164 | __instance_destroy(struct nfqnl_instance *inst) |
| 165 | { |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 166 | hlist_del_rcu(&inst->hlist); |
| 167 | call_rcu(&inst->rcu, instance_destroy_rcu); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 170 | static void |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 171 | instance_destroy(struct nfnl_queue_net *q, struct nfqnl_instance *inst) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 172 | { |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 173 | spin_lock(&q->instances_lock); |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 174 | __instance_destroy(inst); |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 175 | spin_unlock(&q->instances_lock); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 178 | static inline void |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 179 | __enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 180 | { |
Patrick McHardy | 0ac41e8 | 2007-12-05 01:25:03 -0800 | [diff] [blame] | 181 | list_add_tail(&entry->list, &queue->queue_list); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 182 | queue->queue_total++; |
| 183 | } |
| 184 | |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 185 | static void |
| 186 | __dequeue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry) |
| 187 | { |
| 188 | list_del(&entry->list); |
| 189 | queue->queue_total--; |
| 190 | } |
| 191 | |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 192 | static struct nf_queue_entry * |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 193 | find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 194 | { |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 195 | struct nf_queue_entry *entry = NULL, *i; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 196 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 197 | spin_lock_bh(&queue->lock); |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 198 | |
| 199 | list_for_each_entry(i, &queue->queue_list, list) { |
| 200 | if (i->id == id) { |
| 201 | entry = i; |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 206 | if (entry) |
| 207 | __dequeue_entry(queue, entry); |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 208 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 209 | spin_unlock_bh(&queue->lock); |
| 210 | |
| 211 | return entry; |
| 212 | } |
| 213 | |
| 214 | static void |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 215 | nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 216 | { |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 217 | struct nf_queue_entry *entry, *next; |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 218 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 219 | spin_lock_bh(&queue->lock); |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 220 | list_for_each_entry_safe(entry, next, &queue->queue_list, list) { |
| 221 | if (!cmpfn || cmpfn(entry, data)) { |
| 222 | list_del(&entry->list); |
| 223 | queue->queue_total--; |
Patrick McHardy | 4b3d15e | 2007-12-05 01:27:02 -0800 | [diff] [blame] | 224 | nf_reinject(entry, NF_DROP); |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 225 | } |
| 226 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 227 | spin_unlock_bh(&queue->lock); |
| 228 | } |
| 229 | |
Eric Dumazet | ae08ce0 | 2013-03-17 17:15:55 +0000 | [diff] [blame] | 230 | static void |
| 231 | nfqnl_zcopy(struct sk_buff *to, const struct sk_buff *from, int len, int hlen) |
| 232 | { |
| 233 | int i, j = 0; |
| 234 | int plen = 0; /* length of skb->head fragment */ |
| 235 | struct page *page; |
| 236 | unsigned int offset; |
| 237 | |
| 238 | /* dont bother with small payloads */ |
| 239 | if (len <= skb_tailroom(to)) { |
| 240 | skb_copy_bits(from, 0, skb_put(to, len), len); |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | if (hlen) { |
| 245 | skb_copy_bits(from, 0, skb_put(to, hlen), hlen); |
| 246 | len -= hlen; |
| 247 | } else { |
| 248 | plen = min_t(int, skb_headlen(from), len); |
| 249 | if (plen) { |
| 250 | page = virt_to_head_page(from->head); |
| 251 | offset = from->data - (unsigned char *)page_address(page); |
| 252 | __skb_fill_page_desc(to, 0, page, offset, plen); |
| 253 | get_page(page); |
| 254 | j = 1; |
| 255 | len -= plen; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | to->truesize += len + plen; |
| 260 | to->len += len + plen; |
| 261 | to->data_len += len + plen; |
| 262 | |
| 263 | for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { |
| 264 | if (!len) |
| 265 | break; |
| 266 | skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; |
| 267 | skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len); |
| 268 | len -= skb_shinfo(to)->frags[j].size; |
| 269 | skb_frag_ref(to, j); |
| 270 | j++; |
| 271 | } |
| 272 | skb_shinfo(to)->nr_frags = j; |
| 273 | } |
| 274 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 275 | static struct sk_buff * |
| 276 | nfqnl_build_packet_message(struct nfqnl_instance *queue, |
Eric Dumazet | 5863702 | 2011-07-19 11:44:17 +0200 | [diff] [blame] | 277 | struct nf_queue_entry *entry, |
| 278 | __be32 **packet_id_ptr) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 279 | { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 280 | size_t size; |
Pablo Neira Ayuso | 6ee584b | 2012-09-24 14:52:12 +0200 | [diff] [blame] | 281 | size_t data_len = 0, cap_len = 0; |
Eric Dumazet | ae08ce0 | 2013-03-17 17:15:55 +0000 | [diff] [blame] | 282 | int hlen = 0; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 283 | struct sk_buff *skb; |
Eric Dumazet | 5863702 | 2011-07-19 11:44:17 +0200 | [diff] [blame] | 284 | struct nlattr *nla; |
| 285 | struct nfqnl_msg_packet_hdr *pmsg; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 286 | struct nlmsghdr *nlh; |
| 287 | struct nfgenmsg *nfmsg; |
Jesper Juhl | 3e4ead4 | 2006-01-05 12:15:58 -0800 | [diff] [blame] | 288 | struct sk_buff *entskb = entry->skb; |
| 289 | struct net_device *indev; |
| 290 | struct net_device *outdev; |
Pablo Neira Ayuso | 9cb0176 | 2012-06-07 12:13:39 +0200 | [diff] [blame] | 291 | struct nf_conn *ct = NULL; |
| 292 | enum ip_conntrack_info uninitialized_var(ctinfo); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 293 | |
Eric Leblond | cabaa9b | 2008-03-10 16:41:43 -0700 | [diff] [blame] | 294 | size = NLMSG_SPACE(sizeof(struct nfgenmsg)) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 295 | + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr)) |
| 296 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ |
| 297 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 298 | #ifdef CONFIG_BRIDGE_NETFILTER |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 299 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ |
| 300 | + nla_total_size(sizeof(u_int32_t)) /* ifindex */ |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 301 | #endif |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 302 | + nla_total_size(sizeof(u_int32_t)) /* mark */ |
| 303 | + nla_total_size(sizeof(struct nfqnl_msg_packet_hw)) |
Eric Dumazet | ae08ce0 | 2013-03-17 17:15:55 +0000 | [diff] [blame] | 304 | + nla_total_size(sizeof(u_int32_t)); /* cap_len */ |
| 305 | |
| 306 | if (entskb->tstamp.tv64) |
| 307 | size += nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp)); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 308 | |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 309 | outdev = entry->outdev; |
Jesper Juhl | 3e4ead4 | 2006-01-05 12:15:58 -0800 | [diff] [blame] | 310 | |
Eric Dumazet | c463ac9 | 2010-06-09 18:07:06 +0200 | [diff] [blame] | 311 | switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 312 | case NFQNL_COPY_META: |
| 313 | case NFQNL_COPY_NONE: |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 314 | break; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 315 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 316 | case NFQNL_COPY_PACKET: |
Herbert Xu | e9f13ca | 2010-04-08 14:54:35 +0200 | [diff] [blame] | 317 | if (entskb->ip_summed == CHECKSUM_PARTIAL && |
Eric Dumazet | c463ac9 | 2010-06-09 18:07:06 +0200 | [diff] [blame] | 318 | skb_checksum_help(entskb)) |
Patrick McHardy | e7dfb09 | 2005-09-06 15:10:00 -0700 | [diff] [blame] | 319 | return NULL; |
Eric Dumazet | c463ac9 | 2010-06-09 18:07:06 +0200 | [diff] [blame] | 320 | |
| 321 | data_len = ACCESS_ONCE(queue->copy_range); |
| 322 | if (data_len == 0 || data_len > entskb->len) |
Jesper Juhl | 3e4ead4 | 2006-01-05 12:15:58 -0800 | [diff] [blame] | 323 | data_len = entskb->len; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 324 | |
Eric Dumazet | ae08ce0 | 2013-03-17 17:15:55 +0000 | [diff] [blame] | 325 | |
| 326 | if (!entskb->head_frag || |
| 327 | skb_headlen(entskb) < L1_CACHE_BYTES || |
| 328 | skb_shinfo(entskb)->nr_frags >= MAX_SKB_FRAGS) |
| 329 | hlen = skb_headlen(entskb); |
| 330 | |
| 331 | if (skb_has_frag_list(entskb)) |
| 332 | hlen = entskb->len; |
| 333 | hlen = min_t(int, data_len, hlen); |
| 334 | size += sizeof(struct nlattr) + hlen; |
Pablo Neira Ayuso | 6ee584b | 2012-09-24 14:52:12 +0200 | [diff] [blame] | 335 | cap_len = entskb->len; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 336 | break; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Pablo Neira Ayuso | 7c62234 | 2012-06-19 02:10:57 +0200 | [diff] [blame] | 339 | if (queue->flags & NFQA_CFG_F_CONNTRACK) |
| 340 | ct = nfqnl_ct_get(entskb, &size, &ctinfo); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 341 | |
| 342 | skb = alloc_skb(size, GFP_ATOMIC); |
| 343 | if (!skb) |
David S. Miller | 3da07c0 | 2012-06-26 21:35:27 -0700 | [diff] [blame] | 344 | return NULL; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 345 | |
David S. Miller | 3da07c0 | 2012-06-26 21:35:27 -0700 | [diff] [blame] | 346 | nlh = nlmsg_put(skb, 0, 0, |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 347 | NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET, |
David S. Miller | 3da07c0 | 2012-06-26 21:35:27 -0700 | [diff] [blame] | 348 | sizeof(struct nfgenmsg), 0); |
| 349 | if (!nlh) { |
| 350 | kfree_skb(skb); |
| 351 | return NULL; |
| 352 | } |
| 353 | nfmsg = nlmsg_data(nlh); |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 354 | nfmsg->nfgen_family = entry->pf; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 355 | nfmsg->version = NFNETLINK_V0; |
| 356 | nfmsg->res_id = htons(queue->queue_num); |
| 357 | |
Eric Dumazet | 5863702 | 2011-07-19 11:44:17 +0200 | [diff] [blame] | 358 | nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg)); |
| 359 | pmsg = nla_data(nla); |
| 360 | pmsg->hw_protocol = entskb->protocol; |
| 361 | pmsg->hook = entry->hook; |
| 362 | *packet_id_ptr = &pmsg->packet_id; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 363 | |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 364 | indev = entry->indev; |
Jesper Juhl | 3e4ead4 | 2006-01-05 12:15:58 -0800 | [diff] [blame] | 365 | if (indev) { |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 366 | #ifndef CONFIG_BRIDGE_NETFILTER |
David S. Miller | a447189 | 2012-03-29 23:27:38 -0400 | [diff] [blame] | 367 | if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex))) |
| 368 | goto nla_put_failure; |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 369 | #else |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 370 | if (entry->pf == PF_BRIDGE) { |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 371 | /* Case 1: indev is physical input device, we need to |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 372 | * look for bridge group (when called from |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 373 | * netfilter_bridge) */ |
David S. Miller | a447189 | 2012-03-29 23:27:38 -0400 | [diff] [blame] | 374 | if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV, |
| 375 | htonl(indev->ifindex)) || |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 376 | /* this is the bridge group "brX" */ |
Jiri Pirko | f350a0a8 | 2010-06-15 06:50:45 +0000 | [diff] [blame] | 377 | /* rcu_read_lock()ed by __nf_queue */ |
David S. Miller | a447189 | 2012-03-29 23:27:38 -0400 | [diff] [blame] | 378 | nla_put_be32(skb, NFQA_IFINDEX_INDEV, |
| 379 | htonl(br_port_get_rcu(indev)->br->dev->ifindex))) |
| 380 | goto nla_put_failure; |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 381 | } else { |
| 382 | /* Case 2: indev is bridge group, we need to look for |
| 383 | * physical device (when called from ipv4) */ |
David S. Miller | a447189 | 2012-03-29 23:27:38 -0400 | [diff] [blame] | 384 | if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, |
| 385 | htonl(indev->ifindex))) |
| 386 | goto nla_put_failure; |
| 387 | if (entskb->nf_bridge && entskb->nf_bridge->physindev && |
| 388 | nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV, |
| 389 | htonl(entskb->nf_bridge->physindev->ifindex))) |
| 390 | goto nla_put_failure; |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 391 | } |
| 392 | #endif |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Jesper Juhl | 3e4ead4 | 2006-01-05 12:15:58 -0800 | [diff] [blame] | 395 | if (outdev) { |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 396 | #ifndef CONFIG_BRIDGE_NETFILTER |
David S. Miller | a447189 | 2012-03-29 23:27:38 -0400 | [diff] [blame] | 397 | if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex))) |
| 398 | goto nla_put_failure; |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 399 | #else |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 400 | if (entry->pf == PF_BRIDGE) { |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 401 | /* Case 1: outdev is physical output device, we need to |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 402 | * look for bridge group (when called from |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 403 | * netfilter_bridge) */ |
David S. Miller | a447189 | 2012-03-29 23:27:38 -0400 | [diff] [blame] | 404 | if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV, |
| 405 | htonl(outdev->ifindex)) || |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 406 | /* this is the bridge group "brX" */ |
Jiri Pirko | f350a0a8 | 2010-06-15 06:50:45 +0000 | [diff] [blame] | 407 | /* rcu_read_lock()ed by __nf_queue */ |
David S. Miller | a447189 | 2012-03-29 23:27:38 -0400 | [diff] [blame] | 408 | nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, |
| 409 | htonl(br_port_get_rcu(outdev)->br->dev->ifindex))) |
| 410 | goto nla_put_failure; |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 411 | } else { |
| 412 | /* Case 2: outdev is bridge group, we need to look for |
| 413 | * physical output device (when called from ipv4) */ |
David S. Miller | a447189 | 2012-03-29 23:27:38 -0400 | [diff] [blame] | 414 | if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, |
| 415 | htonl(outdev->ifindex))) |
| 416 | goto nla_put_failure; |
| 417 | if (entskb->nf_bridge && entskb->nf_bridge->physoutdev && |
| 418 | nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV, |
| 419 | htonl(entskb->nf_bridge->physoutdev->ifindex))) |
| 420 | goto nla_put_failure; |
Harald Welte | fbcd923 | 2005-08-09 20:22:10 -0700 | [diff] [blame] | 421 | } |
| 422 | #endif |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 423 | } |
| 424 | |
David S. Miller | a447189 | 2012-03-29 23:27:38 -0400 | [diff] [blame] | 425 | if (entskb->mark && |
| 426 | nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark))) |
| 427 | goto nla_put_failure; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 428 | |
Nicolas Cavallari | 2c38de4 | 2011-06-16 17:27:04 +0200 | [diff] [blame] | 429 | if (indev && entskb->dev && |
| 430 | entskb->mac_header != entskb->network_header) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 431 | struct nfqnl_msg_packet_hw phw; |
Stephen Hemminger | b95cce3 | 2007-09-26 22:13:38 -0700 | [diff] [blame] | 432 | int len = dev_parse_header(entskb, phw.hw_addr); |
| 433 | if (len) { |
| 434 | phw.hw_addrlen = htons(len); |
David S. Miller | a447189 | 2012-03-29 23:27:38 -0400 | [diff] [blame] | 435 | if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw)) |
| 436 | goto nla_put_failure; |
Stephen Hemminger | b95cce3 | 2007-09-26 22:13:38 -0700 | [diff] [blame] | 437 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 440 | if (entskb->tstamp.tv64) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 441 | struct nfqnl_msg_packet_timestamp ts; |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 442 | struct timeval tv = ktime_to_timeval(entskb->tstamp); |
| 443 | ts.sec = cpu_to_be64(tv.tv_sec); |
| 444 | ts.usec = cpu_to_be64(tv.tv_usec); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 445 | |
David S. Miller | a447189 | 2012-03-29 23:27:38 -0400 | [diff] [blame] | 446 | if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts)) |
| 447 | goto nla_put_failure; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Pablo Neira Ayuso | 7c62234 | 2012-06-19 02:10:57 +0200 | [diff] [blame] | 450 | if (ct && nfqnl_ct_put(skb, ct, ctinfo) < 0) |
| 451 | goto nla_put_failure; |
Pablo Neira Ayuso | 9cb0176 | 2012-06-07 12:13:39 +0200 | [diff] [blame] | 452 | |
Pablo Neira Ayuso | 6ee584b | 2012-09-24 14:52:12 +0200 | [diff] [blame] | 453 | if (cap_len > 0 && nla_put_be32(skb, NFQA_CAP_LEN, htonl(cap_len))) |
| 454 | goto nla_put_failure; |
| 455 | |
Eric Dumazet | ae08ce0 | 2013-03-17 17:15:55 +0000 | [diff] [blame] | 456 | if (data_len) { |
| 457 | struct nlattr *nla; |
| 458 | |
| 459 | if (skb_tailroom(skb) < sizeof(*nla) + hlen) |
| 460 | goto nla_put_failure; |
| 461 | |
| 462 | nla = (struct nlattr *)skb_put(skb, sizeof(*nla)); |
| 463 | nla->nla_type = NFQA_PAYLOAD; |
| 464 | nla->nla_len = nla_attr_size(data_len); |
| 465 | |
| 466 | nfqnl_zcopy(skb, entskb, data_len, hlen); |
| 467 | } |
| 468 | |
| 469 | nlh->nlmsg_len = skb->len; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 470 | return skb; |
| 471 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 472 | nla_put_failure: |
Wei Yongjun | a672995 | 2012-08-28 03:14:15 +0000 | [diff] [blame] | 473 | kfree_skb(skb); |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 474 | net_err_ratelimited("nf_queue: error creating packet message\n"); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 475 | return NULL; |
| 476 | } |
| 477 | |
| 478 | static int |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 479 | nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 480 | { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 481 | struct sk_buff *nskb; |
| 482 | struct nfqnl_instance *queue; |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 483 | int err = -ENOBUFS; |
Eric Dumazet | 5863702 | 2011-07-19 11:44:17 +0200 | [diff] [blame] | 484 | __be32 *packet_id_ptr; |
Krishna Kumar | fdb694a | 2012-05-24 03:56:44 +0000 | [diff] [blame] | 485 | int failopen = 0; |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 486 | struct net *net = dev_net(entry->indev ? |
| 487 | entry->indev : entry->outdev); |
| 488 | struct nfnl_queue_net *q = nfnl_queue_pernet(net); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 489 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 490 | /* rcu_read_lock()ed by nf_hook_slow() */ |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 491 | queue = instance_lookup(q, queuenum); |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 492 | if (!queue) { |
| 493 | err = -ESRCH; |
Patrick McHardy | 0ef0f46 | 2007-12-05 01:31:01 -0800 | [diff] [blame] | 494 | goto err_out; |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 495 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 496 | |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 497 | if (queue->copy_mode == NFQNL_COPY_NONE) { |
| 498 | err = -EINVAL; |
Patrick McHardy | 0ef0f46 | 2007-12-05 01:31:01 -0800 | [diff] [blame] | 499 | goto err_out; |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 500 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 501 | |
Eric Dumazet | 5863702 | 2011-07-19 11:44:17 +0200 | [diff] [blame] | 502 | nskb = nfqnl_build_packet_message(queue, entry, &packet_id_ptr); |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 503 | if (nskb == NULL) { |
| 504 | err = -ENOMEM; |
Patrick McHardy | 0ef0f46 | 2007-12-05 01:31:01 -0800 | [diff] [blame] | 505 | goto err_out; |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 506 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 507 | spin_lock_bh(&queue->lock); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 508 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 509 | if (!queue->peer_portid) { |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 510 | err = -EINVAL; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 511 | goto err_out_free_nskb; |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 512 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 513 | if (queue->queue_total >= queue->queue_maxlen) { |
Krishna Kumar | fdb694a | 2012-05-24 03:56:44 +0000 | [diff] [blame] | 514 | if (queue->flags & NFQA_CFG_F_FAIL_OPEN) { |
| 515 | failopen = 1; |
| 516 | err = 0; |
| 517 | } else { |
| 518 | queue->queue_dropped++; |
| 519 | net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n", |
| 520 | queue->queue_total); |
| 521 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 522 | goto err_out_free_nskb; |
| 523 | } |
Eric Dumazet | 5863702 | 2011-07-19 11:44:17 +0200 | [diff] [blame] | 524 | entry->id = ++queue->id_sequence; |
| 525 | *packet_id_ptr = htonl(entry->id); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 526 | |
| 527 | /* nfnetlink_unicast will either free the nskb or add it to a socket */ |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 528 | err = nfnetlink_unicast(nskb, net, queue->peer_portid, MSG_DONTWAIT); |
Patrick McHardy | 0ef0f46 | 2007-12-05 01:31:01 -0800 | [diff] [blame] | 529 | if (err < 0) { |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 530 | queue->queue_user_dropped++; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 531 | goto err_out_unlock; |
| 532 | } |
| 533 | |
| 534 | __enqueue_entry(queue, entry); |
| 535 | |
| 536 | spin_unlock_bh(&queue->lock); |
Patrick McHardy | 0ef0f46 | 2007-12-05 01:31:01 -0800 | [diff] [blame] | 537 | return 0; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 538 | |
| 539 | err_out_free_nskb: |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 540 | kfree_skb(nskb); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 541 | err_out_unlock: |
| 542 | spin_unlock_bh(&queue->lock); |
Krishna Kumar | fdb694a | 2012-05-24 03:56:44 +0000 | [diff] [blame] | 543 | if (failopen) |
| 544 | nf_reinject(entry, NF_ACCEPT); |
Patrick McHardy | 0ef0f46 | 2007-12-05 01:31:01 -0800 | [diff] [blame] | 545 | err_out: |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 546 | return err; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | static int |
Pablo Neira Ayuso | 8c88f87 | 2012-06-07 13:31:25 +0200 | [diff] [blame] | 550 | nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e, int diff) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 551 | { |
Patrick McHardy | e2b58a6 | 2008-02-19 17:17:52 -0800 | [diff] [blame] | 552 | struct sk_buff *nskb; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 553 | |
Patrick McHardy | d8a585d | 2006-11-14 19:48:09 -0800 | [diff] [blame] | 554 | if (diff < 0) { |
| 555 | if (pskb_trim(e->skb, data_len)) |
| 556 | return -ENOMEM; |
| 557 | } else if (diff > 0) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 558 | if (data_len > 0xFFFF) |
| 559 | return -EINVAL; |
| 560 | if (diff > skb_tailroom(e->skb)) { |
Arnaud Ebalard | 9a732ed | 2008-04-29 03:16:34 -0700 | [diff] [blame] | 561 | nskb = skb_copy_expand(e->skb, skb_headroom(e->skb), |
| 562 | diff, GFP_ATOMIC); |
Patrick McHardy | e2b58a6 | 2008-02-19 17:17:52 -0800 | [diff] [blame] | 563 | if (!nskb) { |
Patrick McHardy | 1158ba2 | 2006-08-22 00:32:47 -0700 | [diff] [blame] | 564 | printk(KERN_WARNING "nf_queue: OOM " |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 565 | "in mangle, dropping packet\n"); |
Patrick McHardy | e2b58a6 | 2008-02-19 17:17:52 -0800 | [diff] [blame] | 566 | return -ENOMEM; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 567 | } |
Patrick McHardy | e2b58a6 | 2008-02-19 17:17:52 -0800 | [diff] [blame] | 568 | kfree_skb(e->skb); |
| 569 | e->skb = nskb; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 570 | } |
| 571 | skb_put(e->skb, diff); |
| 572 | } |
Herbert Xu | 37d4187 | 2007-10-14 00:39:18 -0700 | [diff] [blame] | 573 | if (!skb_make_writable(e->skb, data_len)) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 574 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 27d7ff4 | 2007-03-31 11:55:19 -0300 | [diff] [blame] | 575 | skb_copy_to_linear_data(e->skb, data, data_len); |
Patrick McHardy | e7dfb09 | 2005-09-06 15:10:00 -0700 | [diff] [blame] | 576 | e->skb->ip_summed = CHECKSUM_NONE; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 577 | return 0; |
| 578 | } |
| 579 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 580 | static int |
| 581 | nfqnl_set_mode(struct nfqnl_instance *queue, |
| 582 | unsigned char mode, unsigned int range) |
| 583 | { |
Patrick McHardy | c5de0df | 2007-12-05 01:29:05 -0800 | [diff] [blame] | 584 | int status = 0; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 585 | |
| 586 | spin_lock_bh(&queue->lock); |
Patrick McHardy | c5de0df | 2007-12-05 01:29:05 -0800 | [diff] [blame] | 587 | switch (mode) { |
| 588 | case NFQNL_COPY_NONE: |
| 589 | case NFQNL_COPY_META: |
| 590 | queue->copy_mode = mode; |
| 591 | queue->copy_range = 0; |
| 592 | break; |
| 593 | |
| 594 | case NFQNL_COPY_PACKET: |
| 595 | queue->copy_mode = mode; |
Pablo Neira Ayuso | ba8d3b0 | 2012-09-06 17:09:26 +0200 | [diff] [blame] | 596 | /* We're using struct nlattr which has 16bit nla_len. Note that |
| 597 | * nla_len includes the header length. Thus, the maximum packet |
| 598 | * length that we support is 65531 bytes. We send truncated |
| 599 | * packets if the specified length is larger than that. |
| 600 | */ |
| 601 | if (range > 0xffff - NLA_HDRLEN) |
| 602 | queue->copy_range = 0xffff - NLA_HDRLEN; |
Patrick McHardy | c5de0df | 2007-12-05 01:29:05 -0800 | [diff] [blame] | 603 | else |
| 604 | queue->copy_range = range; |
| 605 | break; |
| 606 | |
| 607 | default: |
| 608 | status = -EINVAL; |
| 609 | |
| 610 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 611 | spin_unlock_bh(&queue->lock); |
| 612 | |
| 613 | return status; |
| 614 | } |
| 615 | |
| 616 | static int |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 617 | dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 618 | { |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 619 | if (entry->indev) |
| 620 | if (entry->indev->ifindex == ifindex) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 621 | return 1; |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 622 | if (entry->outdev) |
| 623 | if (entry->outdev->ifindex == ifindex) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 624 | return 1; |
Patrick McHardy | ef47c6a7 | 2006-06-27 03:01:48 -0700 | [diff] [blame] | 625 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 626 | if (entry->skb->nf_bridge) { |
| 627 | if (entry->skb->nf_bridge->physindev && |
| 628 | entry->skb->nf_bridge->physindev->ifindex == ifindex) |
| 629 | return 1; |
| 630 | if (entry->skb->nf_bridge->physoutdev && |
| 631 | entry->skb->nf_bridge->physoutdev->ifindex == ifindex) |
| 632 | return 1; |
| 633 | } |
| 634 | #endif |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | /* drop all packets with either indev or outdev == ifindex from all queue |
| 639 | * instances */ |
| 640 | static void |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 641 | nfqnl_dev_drop(struct net *net, int ifindex) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 642 | { |
| 643 | int i; |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 644 | struct nfnl_queue_net *q = nfnl_queue_pernet(net); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 645 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 646 | rcu_read_lock(); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 647 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 648 | for (i = 0; i < INSTANCE_BUCKETS; i++) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 649 | struct nfqnl_instance *inst; |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 650 | struct hlist_head *head = &q->instance_table[i]; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 651 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 652 | hlist_for_each_entry_rcu(inst, head, hlist) |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 653 | nfqnl_flush(inst, dev_cmp, ifindex); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 656 | rcu_read_unlock(); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0) |
| 660 | |
| 661 | static int |
| 662 | nfqnl_rcv_dev_event(struct notifier_block *this, |
| 663 | unsigned long event, void *ptr) |
| 664 | { |
| 665 | struct net_device *dev = ptr; |
| 666 | |
| 667 | /* Drop any packets associated with the downed device */ |
| 668 | if (event == NETDEV_DOWN) |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 669 | nfqnl_dev_drop(dev_net(dev), dev->ifindex); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 670 | return NOTIFY_DONE; |
| 671 | } |
| 672 | |
| 673 | static struct notifier_block nfqnl_dev_notifier = { |
| 674 | .notifier_call = nfqnl_rcv_dev_event, |
| 675 | }; |
| 676 | |
| 677 | static int |
| 678 | nfqnl_rcv_nl_event(struct notifier_block *this, |
| 679 | unsigned long event, void *ptr) |
| 680 | { |
| 681 | struct netlink_notify *n = ptr; |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 682 | struct nfnl_queue_net *q = nfnl_queue_pernet(n->net); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 683 | |
Patrick McHardy | dee5817 | 2009-11-06 17:04:00 +0100 | [diff] [blame] | 684 | if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 685 | int i; |
| 686 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 687 | /* destroy all instances for this portid */ |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 688 | spin_lock(&q->instances_lock); |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 689 | for (i = 0; i < INSTANCE_BUCKETS; i++) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 690 | struct hlist_node *t2; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 691 | struct nfqnl_instance *inst; |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 692 | struct hlist_head *head = &q->instance_table[i]; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 693 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 694 | hlist_for_each_entry_safe(inst, t2, head, hlist) { |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 695 | if (n->portid == inst->peer_portid) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 696 | __instance_destroy(inst); |
| 697 | } |
| 698 | } |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 699 | spin_unlock(&q->instances_lock); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 700 | } |
| 701 | return NOTIFY_DONE; |
| 702 | } |
| 703 | |
| 704 | static struct notifier_block nfqnl_rtnl_notifier = { |
| 705 | .notifier_call = nfqnl_rcv_nl_event, |
| 706 | }; |
| 707 | |
Patrick McHardy | 5bf7585 | 2007-09-28 14:39:26 -0700 | [diff] [blame] | 708 | static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = { |
| 709 | [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) }, |
| 710 | [NFQA_MARK] = { .type = NLA_U32 }, |
| 711 | [NFQA_PAYLOAD] = { .type = NLA_UNSPEC }, |
Pablo Neira Ayuso | 9cb0176 | 2012-06-07 12:13:39 +0200 | [diff] [blame] | 712 | [NFQA_CT] = { .type = NLA_UNSPEC }, |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 713 | }; |
| 714 | |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 715 | static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = { |
| 716 | [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) }, |
| 717 | [NFQA_MARK] = { .type = NLA_U32 }, |
| 718 | }; |
| 719 | |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 720 | static struct nfqnl_instance * |
| 721 | verdict_instance_lookup(struct nfnl_queue_net *q, u16 queue_num, int nlportid) |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 722 | { |
| 723 | struct nfqnl_instance *queue; |
| 724 | |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 725 | queue = instance_lookup(q, queue_num); |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 726 | if (!queue) |
| 727 | return ERR_PTR(-ENODEV); |
| 728 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 729 | if (queue->peer_portid != nlportid) |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 730 | return ERR_PTR(-EPERM); |
| 731 | |
| 732 | return queue; |
| 733 | } |
| 734 | |
| 735 | static struct nfqnl_msg_verdict_hdr* |
| 736 | verdicthdr_get(const struct nlattr * const nfqa[]) |
| 737 | { |
| 738 | struct nfqnl_msg_verdict_hdr *vhdr; |
| 739 | unsigned int verdict; |
| 740 | |
| 741 | if (!nfqa[NFQA_VERDICT_HDR]) |
| 742 | return NULL; |
| 743 | |
| 744 | vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]); |
Florian Westphal | c667523 | 2011-08-30 15:01:20 +0200 | [diff] [blame] | 745 | verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK; |
| 746 | if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN) |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 747 | return NULL; |
| 748 | return vhdr; |
| 749 | } |
| 750 | |
| 751 | static int nfq_id_after(unsigned int id, unsigned int max) |
| 752 | { |
| 753 | return (int)(id - max) > 0; |
| 754 | } |
| 755 | |
| 756 | static int |
| 757 | nfqnl_recv_verdict_batch(struct sock *ctnl, struct sk_buff *skb, |
| 758 | const struct nlmsghdr *nlh, |
| 759 | const struct nlattr * const nfqa[]) |
| 760 | { |
David S. Miller | 3da07c0 | 2012-06-26 21:35:27 -0700 | [diff] [blame] | 761 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 762 | struct nf_queue_entry *entry, *tmp; |
| 763 | unsigned int verdict, maxid; |
| 764 | struct nfqnl_msg_verdict_hdr *vhdr; |
| 765 | struct nfqnl_instance *queue; |
| 766 | LIST_HEAD(batch_list); |
| 767 | u16 queue_num = ntohs(nfmsg->res_id); |
| 768 | |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 769 | struct net *net = sock_net(ctnl); |
| 770 | struct nfnl_queue_net *q = nfnl_queue_pernet(net); |
| 771 | |
| 772 | queue = verdict_instance_lookup(q, queue_num, |
| 773 | NETLINK_CB(skb).portid); |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 774 | if (IS_ERR(queue)) |
| 775 | return PTR_ERR(queue); |
| 776 | |
| 777 | vhdr = verdicthdr_get(nfqa); |
| 778 | if (!vhdr) |
| 779 | return -EINVAL; |
| 780 | |
| 781 | verdict = ntohl(vhdr->verdict); |
| 782 | maxid = ntohl(vhdr->id); |
| 783 | |
| 784 | spin_lock_bh(&queue->lock); |
| 785 | |
| 786 | list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) { |
| 787 | if (nfq_id_after(entry->id, maxid)) |
| 788 | break; |
| 789 | __dequeue_entry(queue, entry); |
| 790 | list_add_tail(&entry->list, &batch_list); |
| 791 | } |
| 792 | |
| 793 | spin_unlock_bh(&queue->lock); |
| 794 | |
| 795 | if (list_empty(&batch_list)) |
| 796 | return -ENOENT; |
| 797 | |
| 798 | list_for_each_entry_safe(entry, tmp, &batch_list, list) { |
| 799 | if (nfqa[NFQA_MARK]) |
| 800 | entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK])); |
| 801 | nf_reinject(entry, verdict); |
| 802 | } |
| 803 | return 0; |
| 804 | } |
| 805 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 806 | static int |
| 807 | nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb, |
Patrick McHardy | 3993832 | 2009-08-25 16:07:58 +0200 | [diff] [blame] | 808 | const struct nlmsghdr *nlh, |
| 809 | const struct nlattr * const nfqa[]) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 810 | { |
David S. Miller | 3da07c0 | 2012-06-26 21:35:27 -0700 | [diff] [blame] | 811 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 812 | u_int16_t queue_num = ntohs(nfmsg->res_id); |
| 813 | |
| 814 | struct nfqnl_msg_verdict_hdr *vhdr; |
| 815 | struct nfqnl_instance *queue; |
| 816 | unsigned int verdict; |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 817 | struct nf_queue_entry *entry; |
Pablo Neira Ayuso | 8c88f87 | 2012-06-07 13:31:25 +0200 | [diff] [blame] | 818 | enum ip_conntrack_info uninitialized_var(ctinfo); |
| 819 | struct nf_conn *ct = NULL; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 820 | |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 821 | struct net *net = sock_net(ctnl); |
| 822 | struct nfnl_queue_net *q = nfnl_queue_pernet(net); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 823 | |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 824 | queue = instance_lookup(q, queue_num); |
| 825 | if (!queue) |
| 826 | queue = verdict_instance_lookup(q, queue_num, |
| 827 | NETLINK_CB(skb).portid); |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 828 | if (IS_ERR(queue)) |
| 829 | return PTR_ERR(queue); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 830 | |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 831 | vhdr = verdicthdr_get(nfqa); |
| 832 | if (!vhdr) |
Eric Dumazet | 84a797d | 2011-07-18 16:08:27 +0200 | [diff] [blame] | 833 | return -EINVAL; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 834 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 835 | verdict = ntohl(vhdr->verdict); |
| 836 | |
Patrick McHardy | b43d8d8 | 2007-12-05 01:25:30 -0800 | [diff] [blame] | 837 | entry = find_dequeue_entry(queue, ntohl(vhdr->id)); |
Eric Dumazet | 84a797d | 2011-07-18 16:08:27 +0200 | [diff] [blame] | 838 | if (entry == NULL) |
| 839 | return -ENOENT; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 840 | |
Pablo Neira Ayuso | 9cb0176 | 2012-06-07 12:13:39 +0200 | [diff] [blame] | 841 | rcu_read_lock(); |
Pablo Neira Ayuso | 7c62234 | 2012-06-19 02:10:57 +0200 | [diff] [blame] | 842 | if (nfqa[NFQA_CT] && (queue->flags & NFQA_CFG_F_CONNTRACK)) |
| 843 | ct = nfqnl_ct_parse(entry->skb, nfqa[NFQA_CT], &ctinfo); |
Pablo Neira Ayuso | 9cb0176 | 2012-06-07 12:13:39 +0200 | [diff] [blame] | 844 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 845 | if (nfqa[NFQA_PAYLOAD]) { |
Pablo Neira Ayuso | 8c88f87 | 2012-06-07 13:31:25 +0200 | [diff] [blame] | 846 | u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]); |
| 847 | int diff = payload_len - entry->skb->len; |
| 848 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 849 | if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]), |
Pablo Neira Ayuso | 8c88f87 | 2012-06-07 13:31:25 +0200 | [diff] [blame] | 850 | payload_len, entry, diff) < 0) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 851 | verdict = NF_DROP; |
Pablo Neira Ayuso | 8c88f87 | 2012-06-07 13:31:25 +0200 | [diff] [blame] | 852 | |
Pablo Neira Ayuso | 7c62234 | 2012-06-19 02:10:57 +0200 | [diff] [blame] | 853 | if (ct) |
| 854 | nfqnl_ct_seq_adjust(skb, ct, ctinfo, diff); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 855 | } |
Pablo Neira Ayuso | 8c88f87 | 2012-06-07 13:31:25 +0200 | [diff] [blame] | 856 | rcu_read_unlock(); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 857 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 858 | if (nfqa[NFQA_MARK]) |
Patrick McHardy | ea3a66f | 2007-12-05 01:30:02 -0800 | [diff] [blame] | 859 | entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK])); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 860 | |
Patrick McHardy | 4b3d15e | 2007-12-05 01:27:02 -0800 | [diff] [blame] | 861 | nf_reinject(entry, verdict); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | static int |
| 866 | nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb, |
Patrick McHardy | 3993832 | 2009-08-25 16:07:58 +0200 | [diff] [blame] | 867 | const struct nlmsghdr *nlh, |
| 868 | const struct nlattr * const nfqa[]) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 869 | { |
| 870 | return -ENOTSUPP; |
| 871 | } |
| 872 | |
Patrick McHardy | 5bf7585 | 2007-09-28 14:39:26 -0700 | [diff] [blame] | 873 | static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = { |
| 874 | [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) }, |
| 875 | [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) }, |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 876 | }; |
| 877 | |
Patrick McHardy | e3ac529 | 2007-12-05 01:23:57 -0800 | [diff] [blame] | 878 | static const struct nf_queue_handler nfqh = { |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 879 | .outfn = &nfqnl_enqueue_packet, |
| 880 | }; |
| 881 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 882 | static int |
| 883 | nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb, |
Patrick McHardy | 3993832 | 2009-08-25 16:07:58 +0200 | [diff] [blame] | 884 | const struct nlmsghdr *nlh, |
| 885 | const struct nlattr * const nfqa[]) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 886 | { |
David S. Miller | 3da07c0 | 2012-06-26 21:35:27 -0700 | [diff] [blame] | 887 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 888 | u_int16_t queue_num = ntohs(nfmsg->res_id); |
| 889 | struct nfqnl_instance *queue; |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 890 | struct nfqnl_msg_config_cmd *cmd = NULL; |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 891 | struct net *net = sock_net(ctnl); |
| 892 | struct nfnl_queue_net *q = nfnl_queue_pernet(net); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 893 | int ret = 0; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 894 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 895 | if (nfqa[NFQA_CFG_CMD]) { |
| 896 | cmd = nla_data(nfqa[NFQA_CFG_CMD]); |
| 897 | |
Florian Westphal | 0360ae4 | 2012-11-23 06:22:21 +0000 | [diff] [blame] | 898 | /* Obsolete commands without queue context */ |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 899 | switch (cmd->command) { |
Florian Westphal | 0360ae4 | 2012-11-23 06:22:21 +0000 | [diff] [blame] | 900 | case NFQNL_CFG_CMD_PF_BIND: return 0; |
| 901 | case NFQNL_CFG_CMD_PF_UNBIND: return 0; |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 902 | } |
Patrick McHardy | a3c8e7fd | 2007-12-05 01:28:30 -0800 | [diff] [blame] | 903 | } |
| 904 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 905 | rcu_read_lock(); |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 906 | queue = instance_lookup(q, queue_num); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 907 | if (queue && queue->peer_portid != NETLINK_CB(skb).portid) { |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 908 | ret = -EPERM; |
| 909 | goto err_out_unlock; |
| 910 | } |
Patrick McHardy | a3c8e7fd | 2007-12-05 01:28:30 -0800 | [diff] [blame] | 911 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 912 | if (cmd != NULL) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 913 | switch (cmd->command) { |
| 914 | case NFQNL_CFG_CMD_BIND: |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 915 | if (queue) { |
| 916 | ret = -EBUSY; |
| 917 | goto err_out_unlock; |
| 918 | } |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 919 | queue = instance_create(q, queue_num, |
| 920 | NETLINK_CB(skb).portid); |
Patrick McHardy | baab2ce | 2007-12-17 22:41:21 -0800 | [diff] [blame] | 921 | if (IS_ERR(queue)) { |
| 922 | ret = PTR_ERR(queue); |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 923 | goto err_out_unlock; |
| 924 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 925 | break; |
| 926 | case NFQNL_CFG_CMD_UNBIND: |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 927 | if (!queue) { |
| 928 | ret = -ENODEV; |
| 929 | goto err_out_unlock; |
| 930 | } |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 931 | instance_destroy(q, queue); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 932 | break; |
| 933 | case NFQNL_CFG_CMD_PF_BIND: |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 934 | case NFQNL_CFG_CMD_PF_UNBIND: |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 935 | break; |
| 936 | default: |
Patrick McHardy | cd21f0a | 2007-12-17 22:40:19 -0800 | [diff] [blame] | 937 | ret = -ENOTSUPP; |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 938 | break; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 939 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 940 | } |
| 941 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 942 | if (nfqa[NFQA_CFG_PARAMS]) { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 943 | struct nfqnl_msg_config_params *params; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 944 | |
Patrick McHardy | 406dbfc | 2006-03-12 20:32:47 -0800 | [diff] [blame] | 945 | if (!queue) { |
Patrick McHardy | a3c8e7fd | 2007-12-05 01:28:30 -0800 | [diff] [blame] | 946 | ret = -ENODEV; |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 947 | goto err_out_unlock; |
Patrick McHardy | 406dbfc | 2006-03-12 20:32:47 -0800 | [diff] [blame] | 948 | } |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 949 | params = nla_data(nfqa[NFQA_CFG_PARAMS]); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 950 | nfqnl_set_mode(queue, params->copy_mode, |
| 951 | ntohl(params->copy_range)); |
| 952 | } |
| 953 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 954 | if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) { |
Eric Leblond | 829e17a | 2006-11-29 02:35:33 +0100 | [diff] [blame] | 955 | __be32 *queue_maxlen; |
Patrick McHardy | a3c8e7fd | 2007-12-05 01:28:30 -0800 | [diff] [blame] | 956 | |
| 957 | if (!queue) { |
| 958 | ret = -ENODEV; |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 959 | goto err_out_unlock; |
Patrick McHardy | a3c8e7fd | 2007-12-05 01:28:30 -0800 | [diff] [blame] | 960 | } |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 961 | queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]); |
Eric Leblond | 829e17a | 2006-11-29 02:35:33 +0100 | [diff] [blame] | 962 | spin_lock_bh(&queue->lock); |
| 963 | queue->queue_maxlen = ntohl(*queue_maxlen); |
| 964 | spin_unlock_bh(&queue->lock); |
| 965 | } |
| 966 | |
Krishna Kumar | fdb694a | 2012-05-24 03:56:44 +0000 | [diff] [blame] | 967 | if (nfqa[NFQA_CFG_FLAGS]) { |
| 968 | __u32 flags, mask; |
| 969 | |
| 970 | if (!queue) { |
| 971 | ret = -ENODEV; |
| 972 | goto err_out_unlock; |
| 973 | } |
| 974 | |
| 975 | if (!nfqa[NFQA_CFG_MASK]) { |
| 976 | /* A mask is needed to specify which flags are being |
| 977 | * changed. |
| 978 | */ |
| 979 | ret = -EINVAL; |
| 980 | goto err_out_unlock; |
| 981 | } |
| 982 | |
| 983 | flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS])); |
| 984 | mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK])); |
| 985 | |
Krishna Kumar | 46ba5a2 | 2012-06-27 00:59:56 +0000 | [diff] [blame] | 986 | if (flags >= NFQA_CFG_F_MAX) { |
| 987 | ret = -EOPNOTSUPP; |
| 988 | goto err_out_unlock; |
| 989 | } |
| 990 | |
Krishna Kumar | fdb694a | 2012-05-24 03:56:44 +0000 | [diff] [blame] | 991 | spin_lock_bh(&queue->lock); |
| 992 | queue->flags &= ~mask; |
| 993 | queue->flags |= flags & mask; |
| 994 | spin_unlock_bh(&queue->lock); |
| 995 | } |
| 996 | |
Patrick McHardy | 9872bec | 2007-12-05 01:28:50 -0800 | [diff] [blame] | 997 | err_out_unlock: |
| 998 | rcu_read_unlock(); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 999 | return ret; |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Patrick McHardy | 7c8d4cb | 2007-09-28 14:15:45 -0700 | [diff] [blame] | 1002 | static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = { |
Eric Dumazet | 84a797d | 2011-07-18 16:08:27 +0200 | [diff] [blame] | 1003 | [NFQNL_MSG_PACKET] = { .call_rcu = nfqnl_recv_unsupp, |
Harald Welte | 37d2e7a | 2005-11-14 15:24:59 -0800 | [diff] [blame] | 1004 | .attr_count = NFQA_MAX, }, |
Eric Dumazet | 84a797d | 2011-07-18 16:08:27 +0200 | [diff] [blame] | 1005 | [NFQNL_MSG_VERDICT] = { .call_rcu = nfqnl_recv_verdict, |
Patrick McHardy | 5bf7585 | 2007-09-28 14:39:26 -0700 | [diff] [blame] | 1006 | .attr_count = NFQA_MAX, |
| 1007 | .policy = nfqa_verdict_policy }, |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1008 | [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config, |
Patrick McHardy | 5bf7585 | 2007-09-28 14:39:26 -0700 | [diff] [blame] | 1009 | .attr_count = NFQA_CFG_MAX, |
| 1010 | .policy = nfqa_cfg_policy }, |
Florian Westphal | 97d32cf | 2011-07-19 11:46:33 +0200 | [diff] [blame] | 1011 | [NFQNL_MSG_VERDICT_BATCH]={ .call_rcu = nfqnl_recv_verdict_batch, |
| 1012 | .attr_count = NFQA_MAX, |
| 1013 | .policy = nfqa_verdict_batch_policy }, |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1014 | }; |
| 1015 | |
Patrick McHardy | 7c8d4cb | 2007-09-28 14:15:45 -0700 | [diff] [blame] | 1016 | static const struct nfnetlink_subsystem nfqnl_subsys = { |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1017 | .name = "nf_queue", |
| 1018 | .subsys_id = NFNL_SUBSYS_QUEUE, |
| 1019 | .cb_count = NFQNL_MSG_MAX, |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1020 | .cb = nfqnl_cb, |
| 1021 | }; |
| 1022 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1023 | #ifdef CONFIG_PROC_FS |
| 1024 | struct iter_state { |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1025 | struct seq_net_private p; |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1026 | unsigned int bucket; |
| 1027 | }; |
| 1028 | |
| 1029 | static struct hlist_node *get_first(struct seq_file *seq) |
| 1030 | { |
| 1031 | struct iter_state *st = seq->private; |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1032 | struct net *net; |
| 1033 | struct nfnl_queue_net *q; |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1034 | |
| 1035 | if (!st) |
| 1036 | return NULL; |
| 1037 | |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1038 | net = seq_file_net(seq); |
| 1039 | q = nfnl_queue_pernet(net); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1040 | for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) { |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1041 | if (!hlist_empty(&q->instance_table[st->bucket])) |
| 1042 | return q->instance_table[st->bucket].first; |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1043 | } |
| 1044 | return NULL; |
| 1045 | } |
| 1046 | |
| 1047 | static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h) |
| 1048 | { |
| 1049 | struct iter_state *st = seq->private; |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1050 | struct net *net = seq_file_net(seq); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1051 | |
| 1052 | h = h->next; |
| 1053 | while (!h) { |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1054 | struct nfnl_queue_net *q; |
| 1055 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1056 | if (++st->bucket >= INSTANCE_BUCKETS) |
| 1057 | return NULL; |
| 1058 | |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1059 | q = nfnl_queue_pernet(net); |
| 1060 | h = q->instance_table[st->bucket].first; |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1061 | } |
| 1062 | return h; |
| 1063 | } |
| 1064 | |
| 1065 | static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos) |
| 1066 | { |
| 1067 | struct hlist_node *head; |
| 1068 | head = get_first(seq); |
| 1069 | |
| 1070 | if (head) |
| 1071 | while (pos && (head = get_next(seq, head))) |
| 1072 | pos--; |
| 1073 | return pos ? NULL : head; |
| 1074 | } |
| 1075 | |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1076 | static void *seq_start(struct seq_file *s, loff_t *pos) |
| 1077 | __acquires(nfnl_queue_pernet(seq_file_net(s))->instances_lock) |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1078 | { |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1079 | spin_lock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock); |
| 1080 | return get_idx(s, *pos); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | static void *seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 1084 | { |
| 1085 | (*pos)++; |
| 1086 | return get_next(s, v); |
| 1087 | } |
| 1088 | |
| 1089 | static void seq_stop(struct seq_file *s, void *v) |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1090 | __releases(nfnl_queue_pernet(seq_file_net(s))->instances_lock) |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1091 | { |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1092 | spin_unlock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | static int seq_show(struct seq_file *s, void *v) |
| 1096 | { |
| 1097 | const struct nfqnl_instance *inst = v; |
| 1098 | |
| 1099 | return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n", |
| 1100 | inst->queue_num, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1101 | inst->peer_portid, inst->queue_total, |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1102 | inst->copy_mode, inst->copy_range, |
| 1103 | inst->queue_dropped, inst->queue_user_dropped, |
Eric Dumazet | 5863702 | 2011-07-19 11:44:17 +0200 | [diff] [blame] | 1104 | inst->id_sequence, 1); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 1107 | static const struct seq_operations nfqnl_seq_ops = { |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1108 | .start = seq_start, |
| 1109 | .next = seq_next, |
| 1110 | .stop = seq_stop, |
| 1111 | .show = seq_show, |
| 1112 | }; |
| 1113 | |
| 1114 | static int nfqnl_open(struct inode *inode, struct file *file) |
| 1115 | { |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1116 | return seq_open_net(inode, file, &nfqnl_seq_ops, |
Pavel Emelyanov | e2da591 | 2007-10-10 02:29:58 -0700 | [diff] [blame] | 1117 | sizeof(struct iter_state)); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 1120 | static const struct file_operations nfqnl_file_ops = { |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1121 | .owner = THIS_MODULE, |
| 1122 | .open = nfqnl_open, |
| 1123 | .read = seq_read, |
| 1124 | .llseek = seq_lseek, |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1125 | .release = seq_release_net, |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1126 | }; |
| 1127 | |
| 1128 | #endif /* PROC_FS */ |
| 1129 | |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1130 | static int __net_init nfnl_queue_net_init(struct net *net) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1131 | { |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1132 | unsigned int i; |
| 1133 | struct nfnl_queue_net *q = nfnl_queue_pernet(net); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1134 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1135 | for (i = 0; i < INSTANCE_BUCKETS; i++) |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1136 | INIT_HLIST_HEAD(&q->instance_table[i]); |
| 1137 | |
| 1138 | spin_lock_init(&q->instances_lock); |
| 1139 | |
| 1140 | #ifdef CONFIG_PROC_FS |
| 1141 | if (!proc_create("nfnetlink_queue", 0440, |
| 1142 | net->nf.proc_netfilter, &nfqnl_file_ops)) |
| 1143 | return -ENOMEM; |
| 1144 | #endif |
| 1145 | return 0; |
| 1146 | } |
| 1147 | |
| 1148 | static void __net_exit nfnl_queue_net_exit(struct net *net) |
| 1149 | { |
| 1150 | remove_proc_entry("nfnetlink_queue", net->nf.proc_netfilter); |
| 1151 | } |
| 1152 | |
| 1153 | static struct pernet_operations nfnl_queue_net_ops = { |
| 1154 | .init = nfnl_queue_net_init, |
| 1155 | .exit = nfnl_queue_net_exit, |
| 1156 | .id = &nfnl_queue_net_id, |
| 1157 | .size = sizeof(struct nfnl_queue_net), |
| 1158 | }; |
| 1159 | |
| 1160 | static int __init nfnetlink_queue_init(void) |
| 1161 | { |
| 1162 | int status = -ENOMEM; |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1163 | |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1164 | netlink_register_notifier(&nfqnl_rtnl_notifier); |
| 1165 | status = nfnetlink_subsys_register(&nfqnl_subsys); |
| 1166 | if (status < 0) { |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1167 | pr_err("nf_queue: failed to create netlink socket\n"); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1168 | goto cleanup_netlink_notifier; |
| 1169 | } |
| 1170 | |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1171 | status = register_pernet_subsys(&nfnl_queue_net_ops); |
| 1172 | if (status < 0) { |
| 1173 | pr_err("nf_queue: failed to register pernet ops\n"); |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1174 | goto cleanup_subsys; |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1175 | } |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1176 | register_netdevice_notifier(&nfqnl_dev_notifier); |
Florian Westphal | 0360ae4 | 2012-11-23 06:22:21 +0000 | [diff] [blame] | 1177 | nf_register_queue_handler(&nfqh); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1178 | return status; |
| 1179 | |
Harald Welte | 838ab63 | 2005-08-09 19:50:45 -0700 | [diff] [blame] | 1180 | cleanup_subsys: |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1181 | nfnetlink_subsys_unregister(&nfqnl_subsys); |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1182 | cleanup_netlink_notifier: |
| 1183 | netlink_unregister_notifier(&nfqnl_rtnl_notifier); |
| 1184 | return status; |
| 1185 | } |
| 1186 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 1187 | static void __exit nfnetlink_queue_fini(void) |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1188 | { |
Florian Westphal | 0360ae4 | 2012-11-23 06:22:21 +0000 | [diff] [blame] | 1189 | nf_unregister_queue_handler(); |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 1190 | unregister_netdevice_notifier(&nfqnl_dev_notifier); |
Gao feng | e817961 | 2013-03-24 23:50:47 +0000 | [diff] [blame^] | 1191 | unregister_pernet_subsys(&nfnl_queue_net_ops); |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 1192 | nfnetlink_subsys_unregister(&nfqnl_subsys); |
| 1193 | netlink_unregister_notifier(&nfqnl_rtnl_notifier); |
Jesper Dangaard Brouer | 67137f3 | 2009-06-08 03:11:33 +0000 | [diff] [blame] | 1194 | |
| 1195 | rcu_barrier(); /* Wait for completion of call_rcu()'s */ |
Harald Welte | 7af4cc3 | 2005-08-09 19:44:15 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | MODULE_DESCRIPTION("netfilter packet queue handler"); |
| 1199 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); |
| 1200 | MODULE_LICENSE("GPL"); |
| 1201 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE); |
| 1202 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 1203 | module_init(nfnetlink_queue_init); |
| 1204 | module_exit(nfnetlink_queue_fini); |