Commit aa07ca5793414ce257ad98809147bd1dbb7a3837
Committed by
David S. Miller
1 parent
506e7beb74
Exists in
master
and in
7 other branches
[NETFILTER] remove bogus hand-coded htonll() from nenetlink_queue
htonll() is nothing else than cpu_to_be64(), so we'd rather call the latter. Signed-off-by: Harald Welte <laforge@netfilter.org> Signed-off-by: David S. Miller <davem@davemloft.net>
Showing 1 changed file with 2 additions and 13 deletions Inline Diff
net/netfilter/nfnetlink_queue.c
1 | /* | 1 | /* |
2 | * This is a module which is used for queueing packets and communicating with | 2 | * This is a module which is used for queueing packets and communicating with |
3 | * userspace via nfetlink. | 3 | * userspace via nfetlink. |
4 | * | 4 | * |
5 | * (C) 2005 by Harald Welte <laforge@netfilter.org> | 5 | * (C) 2005 by Harald Welte <laforge@netfilter.org> |
6 | * | 6 | * |
7 | * Based on the old ipv4-only ip_queue.c: | 7 | * Based on the old ipv4-only ip_queue.c: |
8 | * (C) 2000-2002 James Morris <jmorris@intercode.com.au> | 8 | * (C) 2000-2002 James Morris <jmorris@intercode.com.au> |
9 | * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org> | 9 | * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org> |
10 | * | 10 | * |
11 | * This program is free software; you can redistribute it and/or modify | 11 | * This program is free software; you can redistribute it and/or modify |
12 | * it under the terms of the GNU General Public License version 2 as | 12 | * it under the terms of the GNU General Public License version 2 as |
13 | * published by the Free Software Foundation. | 13 | * published by the Free Software Foundation. |
14 | * | 14 | * |
15 | */ | 15 | */ |
16 | #include <linux/module.h> | 16 | #include <linux/module.h> |
17 | #include <linux/skbuff.h> | 17 | #include <linux/skbuff.h> |
18 | #include <linux/init.h> | 18 | #include <linux/init.h> |
19 | #include <linux/spinlock.h> | 19 | #include <linux/spinlock.h> |
20 | #include <linux/notifier.h> | 20 | #include <linux/notifier.h> |
21 | #include <linux/netdevice.h> | 21 | #include <linux/netdevice.h> |
22 | #include <linux/netfilter.h> | 22 | #include <linux/netfilter.h> |
23 | #include <linux/proc_fs.h> | 23 | #include <linux/proc_fs.h> |
24 | #include <linux/netfilter_ipv4.h> | 24 | #include <linux/netfilter_ipv4.h> |
25 | #include <linux/netfilter_ipv6.h> | 25 | #include <linux/netfilter_ipv6.h> |
26 | #include <linux/netfilter/nfnetlink.h> | 26 | #include <linux/netfilter/nfnetlink.h> |
27 | #include <linux/netfilter/nfnetlink_queue.h> | 27 | #include <linux/netfilter/nfnetlink_queue.h> |
28 | #include <linux/list.h> | 28 | #include <linux/list.h> |
29 | #include <net/sock.h> | 29 | #include <net/sock.h> |
30 | 30 | ||
31 | #include <asm/atomic.h> | 31 | #include <asm/atomic.h> |
32 | 32 | ||
33 | #ifdef CONFIG_BRIDGE_NETFILTER | 33 | #ifdef CONFIG_BRIDGE_NETFILTER |
34 | #include "../bridge/br_private.h" | 34 | #include "../bridge/br_private.h" |
35 | #endif | 35 | #endif |
36 | 36 | ||
37 | #define NFQNL_QMAX_DEFAULT 1024 | 37 | #define NFQNL_QMAX_DEFAULT 1024 |
38 | 38 | ||
39 | #if 0 | 39 | #if 0 |
40 | #define QDEBUG(x, args ...) printk(KERN_DEBUG "%s(%d):%s(): " x, \ | 40 | #define QDEBUG(x, args ...) printk(KERN_DEBUG "%s(%d):%s(): " x, \ |
41 | __FILE__, __LINE__, __FUNCTION__, \ | 41 | __FILE__, __LINE__, __FUNCTION__, \ |
42 | ## args) | 42 | ## args) |
43 | #else | 43 | #else |
44 | #define QDEBUG(x, ...) | 44 | #define QDEBUG(x, ...) |
45 | #endif | 45 | #endif |
46 | 46 | ||
47 | struct nfqnl_queue_entry { | 47 | struct nfqnl_queue_entry { |
48 | struct list_head list; | 48 | struct list_head list; |
49 | struct nf_info *info; | 49 | struct nf_info *info; |
50 | struct sk_buff *skb; | 50 | struct sk_buff *skb; |
51 | unsigned int id; | 51 | unsigned int id; |
52 | }; | 52 | }; |
53 | 53 | ||
54 | struct nfqnl_instance { | 54 | struct nfqnl_instance { |
55 | struct hlist_node hlist; /* global list of queues */ | 55 | struct hlist_node hlist; /* global list of queues */ |
56 | atomic_t use; | 56 | atomic_t use; |
57 | 57 | ||
58 | int peer_pid; | 58 | int peer_pid; |
59 | unsigned int queue_maxlen; | 59 | unsigned int queue_maxlen; |
60 | unsigned int copy_range; | 60 | unsigned int copy_range; |
61 | unsigned int queue_total; | 61 | unsigned int queue_total; |
62 | unsigned int queue_dropped; | 62 | unsigned int queue_dropped; |
63 | unsigned int queue_user_dropped; | 63 | unsigned int queue_user_dropped; |
64 | 64 | ||
65 | atomic_t id_sequence; /* 'sequence' of pkt ids */ | 65 | atomic_t id_sequence; /* 'sequence' of pkt ids */ |
66 | 66 | ||
67 | u_int16_t queue_num; /* number of this queue */ | 67 | u_int16_t queue_num; /* number of this queue */ |
68 | u_int8_t copy_mode; | 68 | u_int8_t copy_mode; |
69 | 69 | ||
70 | spinlock_t lock; | 70 | spinlock_t lock; |
71 | 71 | ||
72 | struct list_head queue_list; /* packets in queue */ | 72 | struct list_head queue_list; /* packets in queue */ |
73 | }; | 73 | }; |
74 | 74 | ||
75 | typedef int (*nfqnl_cmpfn)(struct nfqnl_queue_entry *, unsigned long); | 75 | typedef int (*nfqnl_cmpfn)(struct nfqnl_queue_entry *, unsigned long); |
76 | 76 | ||
77 | static DEFINE_RWLOCK(instances_lock); | 77 | static DEFINE_RWLOCK(instances_lock); |
78 | 78 | ||
79 | static u_int64_t htonll(u_int64_t in) | ||
80 | { | ||
81 | u_int64_t out; | ||
82 | int i; | ||
83 | |||
84 | for (i = 0; i < sizeof(u_int64_t); i++) | ||
85 | ((u_int8_t *)&out)[sizeof(u_int64_t)-1] = ((u_int8_t *)&in)[i]; | ||
86 | |||
87 | return out; | ||
88 | } | ||
89 | |||
90 | #define INSTANCE_BUCKETS 16 | 79 | #define INSTANCE_BUCKETS 16 |
91 | static struct hlist_head instance_table[INSTANCE_BUCKETS]; | 80 | static struct hlist_head instance_table[INSTANCE_BUCKETS]; |
92 | 81 | ||
93 | static inline u_int8_t instance_hashfn(u_int16_t queue_num) | 82 | static inline u_int8_t instance_hashfn(u_int16_t queue_num) |
94 | { | 83 | { |
95 | return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS; | 84 | return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS; |
96 | } | 85 | } |
97 | 86 | ||
98 | static struct nfqnl_instance * | 87 | static struct nfqnl_instance * |
99 | __instance_lookup(u_int16_t queue_num) | 88 | __instance_lookup(u_int16_t queue_num) |
100 | { | 89 | { |
101 | struct hlist_head *head; | 90 | struct hlist_head *head; |
102 | struct hlist_node *pos; | 91 | struct hlist_node *pos; |
103 | struct nfqnl_instance *inst; | 92 | struct nfqnl_instance *inst; |
104 | 93 | ||
105 | head = &instance_table[instance_hashfn(queue_num)]; | 94 | head = &instance_table[instance_hashfn(queue_num)]; |
106 | hlist_for_each_entry(inst, pos, head, hlist) { | 95 | hlist_for_each_entry(inst, pos, head, hlist) { |
107 | if (inst->queue_num == queue_num) | 96 | if (inst->queue_num == queue_num) |
108 | return inst; | 97 | return inst; |
109 | } | 98 | } |
110 | return NULL; | 99 | return NULL; |
111 | } | 100 | } |
112 | 101 | ||
113 | static struct nfqnl_instance * | 102 | static struct nfqnl_instance * |
114 | instance_lookup_get(u_int16_t queue_num) | 103 | instance_lookup_get(u_int16_t queue_num) |
115 | { | 104 | { |
116 | struct nfqnl_instance *inst; | 105 | struct nfqnl_instance *inst; |
117 | 106 | ||
118 | read_lock_bh(&instances_lock); | 107 | read_lock_bh(&instances_lock); |
119 | inst = __instance_lookup(queue_num); | 108 | inst = __instance_lookup(queue_num); |
120 | if (inst) | 109 | if (inst) |
121 | atomic_inc(&inst->use); | 110 | atomic_inc(&inst->use); |
122 | read_unlock_bh(&instances_lock); | 111 | read_unlock_bh(&instances_lock); |
123 | 112 | ||
124 | return inst; | 113 | return inst; |
125 | } | 114 | } |
126 | 115 | ||
127 | static void | 116 | static void |
128 | instance_put(struct nfqnl_instance *inst) | 117 | instance_put(struct nfqnl_instance *inst) |
129 | { | 118 | { |
130 | if (inst && atomic_dec_and_test(&inst->use)) { | 119 | if (inst && atomic_dec_and_test(&inst->use)) { |
131 | QDEBUG("kfree(inst=%p)\n", inst); | 120 | QDEBUG("kfree(inst=%p)\n", inst); |
132 | kfree(inst); | 121 | kfree(inst); |
133 | } | 122 | } |
134 | } | 123 | } |
135 | 124 | ||
136 | static struct nfqnl_instance * | 125 | static struct nfqnl_instance * |
137 | instance_create(u_int16_t queue_num, int pid) | 126 | instance_create(u_int16_t queue_num, int pid) |
138 | { | 127 | { |
139 | struct nfqnl_instance *inst; | 128 | struct nfqnl_instance *inst; |
140 | 129 | ||
141 | QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num, pid); | 130 | QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num, pid); |
142 | 131 | ||
143 | write_lock_bh(&instances_lock); | 132 | write_lock_bh(&instances_lock); |
144 | if (__instance_lookup(queue_num)) { | 133 | if (__instance_lookup(queue_num)) { |
145 | inst = NULL; | 134 | inst = NULL; |
146 | QDEBUG("aborting, instance already exists\n"); | 135 | QDEBUG("aborting, instance already exists\n"); |
147 | goto out_unlock; | 136 | goto out_unlock; |
148 | } | 137 | } |
149 | 138 | ||
150 | inst = kmalloc(sizeof(*inst), GFP_ATOMIC); | 139 | inst = kmalloc(sizeof(*inst), GFP_ATOMIC); |
151 | if (!inst) | 140 | if (!inst) |
152 | goto out_unlock; | 141 | goto out_unlock; |
153 | 142 | ||
154 | memset(inst, 0, sizeof(*inst)); | 143 | memset(inst, 0, sizeof(*inst)); |
155 | inst->queue_num = queue_num; | 144 | inst->queue_num = queue_num; |
156 | inst->peer_pid = pid; | 145 | inst->peer_pid = pid; |
157 | inst->queue_maxlen = NFQNL_QMAX_DEFAULT; | 146 | inst->queue_maxlen = NFQNL_QMAX_DEFAULT; |
158 | inst->copy_range = 0xfffff; | 147 | inst->copy_range = 0xfffff; |
159 | inst->copy_mode = NFQNL_COPY_NONE; | 148 | inst->copy_mode = NFQNL_COPY_NONE; |
160 | atomic_set(&inst->id_sequence, 0); | 149 | atomic_set(&inst->id_sequence, 0); |
161 | /* needs to be two, since we _put() after creation */ | 150 | /* needs to be two, since we _put() after creation */ |
162 | atomic_set(&inst->use, 2); | 151 | atomic_set(&inst->use, 2); |
163 | inst->lock = SPIN_LOCK_UNLOCKED; | 152 | inst->lock = SPIN_LOCK_UNLOCKED; |
164 | INIT_LIST_HEAD(&inst->queue_list); | 153 | INIT_LIST_HEAD(&inst->queue_list); |
165 | 154 | ||
166 | if (!try_module_get(THIS_MODULE)) | 155 | if (!try_module_get(THIS_MODULE)) |
167 | goto out_free; | 156 | goto out_free; |
168 | 157 | ||
169 | hlist_add_head(&inst->hlist, | 158 | hlist_add_head(&inst->hlist, |
170 | &instance_table[instance_hashfn(queue_num)]); | 159 | &instance_table[instance_hashfn(queue_num)]); |
171 | 160 | ||
172 | write_unlock_bh(&instances_lock); | 161 | write_unlock_bh(&instances_lock); |
173 | 162 | ||
174 | QDEBUG("successfully created new instance\n"); | 163 | QDEBUG("successfully created new instance\n"); |
175 | 164 | ||
176 | return inst; | 165 | return inst; |
177 | 166 | ||
178 | out_free: | 167 | out_free: |
179 | kfree(inst); | 168 | kfree(inst); |
180 | out_unlock: | 169 | out_unlock: |
181 | write_unlock_bh(&instances_lock); | 170 | write_unlock_bh(&instances_lock); |
182 | return NULL; | 171 | return NULL; |
183 | } | 172 | } |
184 | 173 | ||
185 | static void nfqnl_flush(struct nfqnl_instance *queue, int verdict); | 174 | static void nfqnl_flush(struct nfqnl_instance *queue, int verdict); |
186 | 175 | ||
187 | static void | 176 | static void |
188 | _instance_destroy2(struct nfqnl_instance *inst, int lock) | 177 | _instance_destroy2(struct nfqnl_instance *inst, int lock) |
189 | { | 178 | { |
190 | /* first pull it out of the global list */ | 179 | /* first pull it out of the global list */ |
191 | if (lock) | 180 | if (lock) |
192 | write_lock_bh(&instances_lock); | 181 | write_lock_bh(&instances_lock); |
193 | 182 | ||
194 | QDEBUG("removing instance %p (queuenum=%u) from hash\n", | 183 | QDEBUG("removing instance %p (queuenum=%u) from hash\n", |
195 | inst, inst->queue_num); | 184 | inst, inst->queue_num); |
196 | hlist_del(&inst->hlist); | 185 | hlist_del(&inst->hlist); |
197 | 186 | ||
198 | if (lock) | 187 | if (lock) |
199 | write_unlock_bh(&instances_lock); | 188 | write_unlock_bh(&instances_lock); |
200 | 189 | ||
201 | /* then flush all pending skbs from the queue */ | 190 | /* then flush all pending skbs from the queue */ |
202 | nfqnl_flush(inst, NF_DROP); | 191 | nfqnl_flush(inst, NF_DROP); |
203 | 192 | ||
204 | /* and finally put the refcount */ | 193 | /* and finally put the refcount */ |
205 | instance_put(inst); | 194 | instance_put(inst); |
206 | 195 | ||
207 | module_put(THIS_MODULE); | 196 | module_put(THIS_MODULE); |
208 | } | 197 | } |
209 | 198 | ||
210 | static inline void | 199 | static inline void |
211 | __instance_destroy(struct nfqnl_instance *inst) | 200 | __instance_destroy(struct nfqnl_instance *inst) |
212 | { | 201 | { |
213 | _instance_destroy2(inst, 0); | 202 | _instance_destroy2(inst, 0); |
214 | } | 203 | } |
215 | 204 | ||
216 | static inline void | 205 | static inline void |
217 | instance_destroy(struct nfqnl_instance *inst) | 206 | instance_destroy(struct nfqnl_instance *inst) |
218 | { | 207 | { |
219 | _instance_destroy2(inst, 1); | 208 | _instance_destroy2(inst, 1); |
220 | } | 209 | } |
221 | 210 | ||
222 | 211 | ||
223 | 212 | ||
224 | static void | 213 | static void |
225 | issue_verdict(struct nfqnl_queue_entry *entry, int verdict) | 214 | issue_verdict(struct nfqnl_queue_entry *entry, int verdict) |
226 | { | 215 | { |
227 | QDEBUG("entering for entry %p, verdict %u\n", entry, verdict); | 216 | QDEBUG("entering for entry %p, verdict %u\n", entry, verdict); |
228 | 217 | ||
229 | /* TCP input path (and probably other bits) assume to be called | 218 | /* TCP input path (and probably other bits) assume to be called |
230 | * from softirq context, not from syscall, like issue_verdict is | 219 | * from softirq context, not from syscall, like issue_verdict is |
231 | * called. TCP input path deadlocks with locks taken from timer | 220 | * called. TCP input path deadlocks with locks taken from timer |
232 | * softirq, e.g. We therefore emulate this by local_bh_disable() */ | 221 | * softirq, e.g. We therefore emulate this by local_bh_disable() */ |
233 | 222 | ||
234 | local_bh_disable(); | 223 | local_bh_disable(); |
235 | nf_reinject(entry->skb, entry->info, verdict); | 224 | nf_reinject(entry->skb, entry->info, verdict); |
236 | local_bh_enable(); | 225 | local_bh_enable(); |
237 | 226 | ||
238 | kfree(entry); | 227 | kfree(entry); |
239 | } | 228 | } |
240 | 229 | ||
241 | static inline void | 230 | static inline void |
242 | __enqueue_entry(struct nfqnl_instance *queue, | 231 | __enqueue_entry(struct nfqnl_instance *queue, |
243 | struct nfqnl_queue_entry *entry) | 232 | struct nfqnl_queue_entry *entry) |
244 | { | 233 | { |
245 | list_add(&entry->list, &queue->queue_list); | 234 | list_add(&entry->list, &queue->queue_list); |
246 | queue->queue_total++; | 235 | queue->queue_total++; |
247 | } | 236 | } |
248 | 237 | ||
249 | /* | 238 | /* |
250 | * Find and return a queued entry matched by cmpfn, or return the last | 239 | * Find and return a queued entry matched by cmpfn, or return the last |
251 | * entry if cmpfn is NULL. | 240 | * entry if cmpfn is NULL. |
252 | */ | 241 | */ |
253 | static inline struct nfqnl_queue_entry * | 242 | static inline struct nfqnl_queue_entry * |
254 | __find_entry(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, | 243 | __find_entry(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, |
255 | unsigned long data) | 244 | unsigned long data) |
256 | { | 245 | { |
257 | struct list_head *p; | 246 | struct list_head *p; |
258 | 247 | ||
259 | list_for_each_prev(p, &queue->queue_list) { | 248 | list_for_each_prev(p, &queue->queue_list) { |
260 | struct nfqnl_queue_entry *entry = (struct nfqnl_queue_entry *)p; | 249 | struct nfqnl_queue_entry *entry = (struct nfqnl_queue_entry *)p; |
261 | 250 | ||
262 | if (!cmpfn || cmpfn(entry, data)) | 251 | if (!cmpfn || cmpfn(entry, data)) |
263 | return entry; | 252 | return entry; |
264 | } | 253 | } |
265 | return NULL; | 254 | return NULL; |
266 | } | 255 | } |
267 | 256 | ||
268 | static inline void | 257 | static inline void |
269 | __dequeue_entry(struct nfqnl_instance *q, struct nfqnl_queue_entry *entry) | 258 | __dequeue_entry(struct nfqnl_instance *q, struct nfqnl_queue_entry *entry) |
270 | { | 259 | { |
271 | list_del(&entry->list); | 260 | list_del(&entry->list); |
272 | q->queue_total--; | 261 | q->queue_total--; |
273 | } | 262 | } |
274 | 263 | ||
275 | static inline struct nfqnl_queue_entry * | 264 | static inline struct nfqnl_queue_entry * |
276 | __find_dequeue_entry(struct nfqnl_instance *queue, | 265 | __find_dequeue_entry(struct nfqnl_instance *queue, |
277 | nfqnl_cmpfn cmpfn, unsigned long data) | 266 | nfqnl_cmpfn cmpfn, unsigned long data) |
278 | { | 267 | { |
279 | struct nfqnl_queue_entry *entry; | 268 | struct nfqnl_queue_entry *entry; |
280 | 269 | ||
281 | entry = __find_entry(queue, cmpfn, data); | 270 | entry = __find_entry(queue, cmpfn, data); |
282 | if (entry == NULL) | 271 | if (entry == NULL) |
283 | return NULL; | 272 | return NULL; |
284 | 273 | ||
285 | __dequeue_entry(queue, entry); | 274 | __dequeue_entry(queue, entry); |
286 | return entry; | 275 | return entry; |
287 | } | 276 | } |
288 | 277 | ||
289 | 278 | ||
290 | static inline void | 279 | static inline void |
291 | __nfqnl_flush(struct nfqnl_instance *queue, int verdict) | 280 | __nfqnl_flush(struct nfqnl_instance *queue, int verdict) |
292 | { | 281 | { |
293 | struct nfqnl_queue_entry *entry; | 282 | struct nfqnl_queue_entry *entry; |
294 | 283 | ||
295 | while ((entry = __find_dequeue_entry(queue, NULL, 0))) | 284 | while ((entry = __find_dequeue_entry(queue, NULL, 0))) |
296 | issue_verdict(entry, verdict); | 285 | issue_verdict(entry, verdict); |
297 | } | 286 | } |
298 | 287 | ||
299 | static inline int | 288 | static inline int |
300 | __nfqnl_set_mode(struct nfqnl_instance *queue, | 289 | __nfqnl_set_mode(struct nfqnl_instance *queue, |
301 | unsigned char mode, unsigned int range) | 290 | unsigned char mode, unsigned int range) |
302 | { | 291 | { |
303 | int status = 0; | 292 | int status = 0; |
304 | 293 | ||
305 | switch (mode) { | 294 | switch (mode) { |
306 | case NFQNL_COPY_NONE: | 295 | case NFQNL_COPY_NONE: |
307 | case NFQNL_COPY_META: | 296 | case NFQNL_COPY_META: |
308 | queue->copy_mode = mode; | 297 | queue->copy_mode = mode; |
309 | queue->copy_range = 0; | 298 | queue->copy_range = 0; |
310 | break; | 299 | break; |
311 | 300 | ||
312 | case NFQNL_COPY_PACKET: | 301 | case NFQNL_COPY_PACKET: |
313 | queue->copy_mode = mode; | 302 | queue->copy_mode = mode; |
314 | /* we're using struct nfattr which has 16bit nfa_len */ | 303 | /* we're using struct nfattr which has 16bit nfa_len */ |
315 | if (range > 0xffff) | 304 | if (range > 0xffff) |
316 | queue->copy_range = 0xffff; | 305 | queue->copy_range = 0xffff; |
317 | else | 306 | else |
318 | queue->copy_range = range; | 307 | queue->copy_range = range; |
319 | break; | 308 | break; |
320 | 309 | ||
321 | default: | 310 | default: |
322 | status = -EINVAL; | 311 | status = -EINVAL; |
323 | 312 | ||
324 | } | 313 | } |
325 | return status; | 314 | return status; |
326 | } | 315 | } |
327 | 316 | ||
328 | static struct nfqnl_queue_entry * | 317 | static struct nfqnl_queue_entry * |
329 | find_dequeue_entry(struct nfqnl_instance *queue, | 318 | find_dequeue_entry(struct nfqnl_instance *queue, |
330 | nfqnl_cmpfn cmpfn, unsigned long data) | 319 | nfqnl_cmpfn cmpfn, unsigned long data) |
331 | { | 320 | { |
332 | struct nfqnl_queue_entry *entry; | 321 | struct nfqnl_queue_entry *entry; |
333 | 322 | ||
334 | spin_lock_bh(&queue->lock); | 323 | spin_lock_bh(&queue->lock); |
335 | entry = __find_dequeue_entry(queue, cmpfn, data); | 324 | entry = __find_dequeue_entry(queue, cmpfn, data); |
336 | spin_unlock_bh(&queue->lock); | 325 | spin_unlock_bh(&queue->lock); |
337 | 326 | ||
338 | return entry; | 327 | return entry; |
339 | } | 328 | } |
340 | 329 | ||
341 | static void | 330 | static void |
342 | nfqnl_flush(struct nfqnl_instance *queue, int verdict) | 331 | nfqnl_flush(struct nfqnl_instance *queue, int verdict) |
343 | { | 332 | { |
344 | spin_lock_bh(&queue->lock); | 333 | spin_lock_bh(&queue->lock); |
345 | __nfqnl_flush(queue, verdict); | 334 | __nfqnl_flush(queue, verdict); |
346 | spin_unlock_bh(&queue->lock); | 335 | spin_unlock_bh(&queue->lock); |
347 | } | 336 | } |
348 | 337 | ||
349 | static struct sk_buff * | 338 | static struct sk_buff * |
350 | nfqnl_build_packet_message(struct nfqnl_instance *queue, | 339 | nfqnl_build_packet_message(struct nfqnl_instance *queue, |
351 | struct nfqnl_queue_entry *entry, int *errp) | 340 | struct nfqnl_queue_entry *entry, int *errp) |
352 | { | 341 | { |
353 | unsigned char *old_tail; | 342 | unsigned char *old_tail; |
354 | size_t size; | 343 | size_t size; |
355 | size_t data_len = 0; | 344 | size_t data_len = 0; |
356 | struct sk_buff *skb; | 345 | struct sk_buff *skb; |
357 | struct nfqnl_msg_packet_hdr pmsg; | 346 | struct nfqnl_msg_packet_hdr pmsg; |
358 | struct nlmsghdr *nlh; | 347 | struct nlmsghdr *nlh; |
359 | struct nfgenmsg *nfmsg; | 348 | struct nfgenmsg *nfmsg; |
360 | unsigned int tmp_uint; | 349 | unsigned int tmp_uint; |
361 | 350 | ||
362 | QDEBUG("entered\n"); | 351 | QDEBUG("entered\n"); |
363 | 352 | ||
364 | /* all macros expand to constant values at compile time */ | 353 | /* all macros expand to constant values at compile time */ |
365 | size = NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_hdr)) | 354 | size = NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_hdr)) |
366 | + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */ | 355 | + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */ |
367 | + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */ | 356 | + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */ |
368 | #ifdef CONFIG_BRIDGE_NETFILTER | 357 | #ifdef CONFIG_BRIDGE_NETFILTER |
369 | + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */ | 358 | + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */ |
370 | + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */ | 359 | + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */ |
371 | #endif | 360 | #endif |
372 | + NLMSG_SPACE(sizeof(u_int32_t)) /* mark */ | 361 | + NLMSG_SPACE(sizeof(u_int32_t)) /* mark */ |
373 | + NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_hw)) | 362 | + NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_hw)) |
374 | + NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_timestamp)); | 363 | + NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_timestamp)); |
375 | 364 | ||
376 | spin_lock_bh(&queue->lock); | 365 | spin_lock_bh(&queue->lock); |
377 | 366 | ||
378 | switch (queue->copy_mode) { | 367 | switch (queue->copy_mode) { |
379 | case NFQNL_COPY_META: | 368 | case NFQNL_COPY_META: |
380 | case NFQNL_COPY_NONE: | 369 | case NFQNL_COPY_NONE: |
381 | data_len = 0; | 370 | data_len = 0; |
382 | break; | 371 | break; |
383 | 372 | ||
384 | case NFQNL_COPY_PACKET: | 373 | case NFQNL_COPY_PACKET: |
385 | if (queue->copy_range == 0 | 374 | if (queue->copy_range == 0 |
386 | || queue->copy_range > entry->skb->len) | 375 | || queue->copy_range > entry->skb->len) |
387 | data_len = entry->skb->len; | 376 | data_len = entry->skb->len; |
388 | else | 377 | else |
389 | data_len = queue->copy_range; | 378 | data_len = queue->copy_range; |
390 | 379 | ||
391 | size += NLMSG_SPACE(data_len); | 380 | size += NLMSG_SPACE(data_len); |
392 | break; | 381 | break; |
393 | 382 | ||
394 | default: | 383 | default: |
395 | *errp = -EINVAL; | 384 | *errp = -EINVAL; |
396 | spin_unlock_bh(&queue->lock); | 385 | spin_unlock_bh(&queue->lock); |
397 | return NULL; | 386 | return NULL; |
398 | } | 387 | } |
399 | 388 | ||
400 | spin_unlock_bh(&queue->lock); | 389 | spin_unlock_bh(&queue->lock); |
401 | 390 | ||
402 | skb = alloc_skb(size, GFP_ATOMIC); | 391 | skb = alloc_skb(size, GFP_ATOMIC); |
403 | if (!skb) | 392 | if (!skb) |
404 | goto nlmsg_failure; | 393 | goto nlmsg_failure; |
405 | 394 | ||
406 | old_tail= skb->tail; | 395 | old_tail= skb->tail; |
407 | nlh = NLMSG_PUT(skb, 0, 0, | 396 | nlh = NLMSG_PUT(skb, 0, 0, |
408 | NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET, | 397 | NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET, |
409 | sizeof(struct nfgenmsg)); | 398 | sizeof(struct nfgenmsg)); |
410 | nfmsg = NLMSG_DATA(nlh); | 399 | nfmsg = NLMSG_DATA(nlh); |
411 | nfmsg->nfgen_family = entry->info->pf; | 400 | nfmsg->nfgen_family = entry->info->pf; |
412 | nfmsg->version = NFNETLINK_V0; | 401 | nfmsg->version = NFNETLINK_V0; |
413 | nfmsg->res_id = htons(queue->queue_num); | 402 | nfmsg->res_id = htons(queue->queue_num); |
414 | 403 | ||
415 | pmsg.packet_id = htonl(entry->id); | 404 | pmsg.packet_id = htonl(entry->id); |
416 | pmsg.hw_protocol = htons(entry->skb->protocol); | 405 | pmsg.hw_protocol = htons(entry->skb->protocol); |
417 | pmsg.hook = entry->info->hook; | 406 | pmsg.hook = entry->info->hook; |
418 | 407 | ||
419 | NFA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg); | 408 | NFA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg); |
420 | 409 | ||
421 | if (entry->info->indev) { | 410 | if (entry->info->indev) { |
422 | tmp_uint = htonl(entry->info->indev->ifindex); | 411 | tmp_uint = htonl(entry->info->indev->ifindex); |
423 | #ifndef CONFIG_BRIDGE_NETFILTER | 412 | #ifndef CONFIG_BRIDGE_NETFILTER |
424 | NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint); | 413 | NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint); |
425 | #else | 414 | #else |
426 | if (entry->info->pf == PF_BRIDGE) { | 415 | if (entry->info->pf == PF_BRIDGE) { |
427 | /* Case 1: indev is physical input device, we need to | 416 | /* Case 1: indev is physical input device, we need to |
428 | * look for bridge group (when called from | 417 | * look for bridge group (when called from |
429 | * netfilter_bridge) */ | 418 | * netfilter_bridge) */ |
430 | NFA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint), | 419 | NFA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint), |
431 | &tmp_uint); | 420 | &tmp_uint); |
432 | /* this is the bridge group "brX" */ | 421 | /* this is the bridge group "brX" */ |
433 | tmp_uint = htonl(entry->info->indev->br_port->br->dev->ifindex); | 422 | tmp_uint = htonl(entry->info->indev->br_port->br->dev->ifindex); |
434 | NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), | 423 | NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), |
435 | &tmp_uint); | 424 | &tmp_uint); |
436 | } else { | 425 | } else { |
437 | /* Case 2: indev is bridge group, we need to look for | 426 | /* Case 2: indev is bridge group, we need to look for |
438 | * physical device (when called from ipv4) */ | 427 | * physical device (when called from ipv4) */ |
439 | NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), | 428 | NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), |
440 | &tmp_uint); | 429 | &tmp_uint); |
441 | if (entry->skb->nf_bridge | 430 | if (entry->skb->nf_bridge |
442 | && entry->skb->nf_bridge->physindev) { | 431 | && entry->skb->nf_bridge->physindev) { |
443 | tmp_uint = htonl(entry->skb->nf_bridge->physindev->ifindex); | 432 | tmp_uint = htonl(entry->skb->nf_bridge->physindev->ifindex); |
444 | NFA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, | 433 | NFA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, |
445 | sizeof(tmp_uint), &tmp_uint); | 434 | sizeof(tmp_uint), &tmp_uint); |
446 | } | 435 | } |
447 | } | 436 | } |
448 | #endif | 437 | #endif |
449 | } | 438 | } |
450 | 439 | ||
451 | if (entry->info->outdev) { | 440 | if (entry->info->outdev) { |
452 | tmp_uint = htonl(entry->info->outdev->ifindex); | 441 | tmp_uint = htonl(entry->info->outdev->ifindex); |
453 | #ifndef CONFIG_BRIDGE_NETFILTER | 442 | #ifndef CONFIG_BRIDGE_NETFILTER |
454 | NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint); | 443 | NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint); |
455 | #else | 444 | #else |
456 | if (entry->info->pf == PF_BRIDGE) { | 445 | if (entry->info->pf == PF_BRIDGE) { |
457 | /* Case 1: outdev is physical output device, we need to | 446 | /* Case 1: outdev is physical output device, we need to |
458 | * look for bridge group (when called from | 447 | * look for bridge group (when called from |
459 | * netfilter_bridge) */ | 448 | * netfilter_bridge) */ |
460 | NFA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint), | 449 | NFA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint), |
461 | &tmp_uint); | 450 | &tmp_uint); |
462 | /* this is the bridge group "brX" */ | 451 | /* this is the bridge group "brX" */ |
463 | tmp_uint = htonl(entry->info->outdev->br_port->br->dev->ifindex); | 452 | tmp_uint = htonl(entry->info->outdev->br_port->br->dev->ifindex); |
464 | NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), | 453 | NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), |
465 | &tmp_uint); | 454 | &tmp_uint); |
466 | } else { | 455 | } else { |
467 | /* Case 2: outdev is bridge group, we need to look for | 456 | /* Case 2: outdev is bridge group, we need to look for |
468 | * physical output device (when called from ipv4) */ | 457 | * physical output device (when called from ipv4) */ |
469 | NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), | 458 | NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), |
470 | &tmp_uint); | 459 | &tmp_uint); |
471 | if (entry->skb->nf_bridge | 460 | if (entry->skb->nf_bridge |
472 | && entry->skb->nf_bridge->physoutdev) { | 461 | && entry->skb->nf_bridge->physoutdev) { |
473 | tmp_uint = htonl(entry->skb->nf_bridge->physoutdev->ifindex); | 462 | tmp_uint = htonl(entry->skb->nf_bridge->physoutdev->ifindex); |
474 | NFA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, | 463 | NFA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, |
475 | sizeof(tmp_uint), &tmp_uint); | 464 | sizeof(tmp_uint), &tmp_uint); |
476 | } | 465 | } |
477 | } | 466 | } |
478 | #endif | 467 | #endif |
479 | } | 468 | } |
480 | 469 | ||
481 | if (entry->skb->nfmark) { | 470 | if (entry->skb->nfmark) { |
482 | tmp_uint = htonl(entry->skb->nfmark); | 471 | tmp_uint = htonl(entry->skb->nfmark); |
483 | NFA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint); | 472 | NFA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint); |
484 | } | 473 | } |
485 | 474 | ||
486 | if (entry->info->indev && entry->skb->dev | 475 | if (entry->info->indev && entry->skb->dev |
487 | && entry->skb->dev->hard_header_parse) { | 476 | && entry->skb->dev->hard_header_parse) { |
488 | struct nfqnl_msg_packet_hw phw; | 477 | struct nfqnl_msg_packet_hw phw; |
489 | 478 | ||
490 | phw.hw_addrlen = | 479 | phw.hw_addrlen = |
491 | entry->skb->dev->hard_header_parse(entry->skb, | 480 | entry->skb->dev->hard_header_parse(entry->skb, |
492 | phw.hw_addr); | 481 | phw.hw_addr); |
493 | phw.hw_addrlen = htons(phw.hw_addrlen); | 482 | phw.hw_addrlen = htons(phw.hw_addrlen); |
494 | NFA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw); | 483 | NFA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw); |
495 | } | 484 | } |
496 | 485 | ||
497 | if (entry->skb->tstamp.off_sec) { | 486 | if (entry->skb->tstamp.off_sec) { |
498 | struct nfqnl_msg_packet_timestamp ts; | 487 | struct nfqnl_msg_packet_timestamp ts; |
499 | 488 | ||
500 | ts.sec = htonll(skb_tv_base.tv_sec + entry->skb->tstamp.off_sec); | 489 | ts.sec = cpu_to_be64(skb_tv_base.tv_sec + entry->skb->tstamp.off_sec); |
501 | ts.usec = htonll(skb_tv_base.tv_usec + entry->skb->tstamp.off_usec); | 490 | ts.usec = cpu_to_be64(skb_tv_base.tv_usec + entry->skb->tstamp.off_usec); |
502 | 491 | ||
503 | NFA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts); | 492 | NFA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts); |
504 | } | 493 | } |
505 | 494 | ||
506 | if (data_len) { | 495 | if (data_len) { |
507 | struct nfattr *nfa; | 496 | struct nfattr *nfa; |
508 | int size = NFA_LENGTH(data_len); | 497 | int size = NFA_LENGTH(data_len); |
509 | 498 | ||
510 | if (skb_tailroom(skb) < (int)NFA_SPACE(data_len)) { | 499 | if (skb_tailroom(skb) < (int)NFA_SPACE(data_len)) { |
511 | printk(KERN_WARNING "nf_queue: no tailroom!\n"); | 500 | printk(KERN_WARNING "nf_queue: no tailroom!\n"); |
512 | goto nlmsg_failure; | 501 | goto nlmsg_failure; |
513 | } | 502 | } |
514 | 503 | ||
515 | nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size)); | 504 | nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size)); |
516 | nfa->nfa_type = NFQA_PAYLOAD; | 505 | nfa->nfa_type = NFQA_PAYLOAD; |
517 | nfa->nfa_len = size; | 506 | nfa->nfa_len = size; |
518 | 507 | ||
519 | if (skb_copy_bits(entry->skb, 0, NFA_DATA(nfa), data_len)) | 508 | if (skb_copy_bits(entry->skb, 0, NFA_DATA(nfa), data_len)) |
520 | BUG(); | 509 | BUG(); |
521 | } | 510 | } |
522 | 511 | ||
523 | nlh->nlmsg_len = skb->tail - old_tail; | 512 | nlh->nlmsg_len = skb->tail - old_tail; |
524 | return skb; | 513 | return skb; |
525 | 514 | ||
526 | nlmsg_failure: | 515 | nlmsg_failure: |
527 | nfattr_failure: | 516 | nfattr_failure: |
528 | if (skb) | 517 | if (skb) |
529 | kfree_skb(skb); | 518 | kfree_skb(skb); |
530 | *errp = -EINVAL; | 519 | *errp = -EINVAL; |
531 | if (net_ratelimit()) | 520 | if (net_ratelimit()) |
532 | printk(KERN_ERR "nf_queue: error creating packet message\n"); | 521 | printk(KERN_ERR "nf_queue: error creating packet message\n"); |
533 | return NULL; | 522 | return NULL; |
534 | } | 523 | } |
535 | 524 | ||
536 | static int | 525 | static int |
537 | nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info, | 526 | nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info, |
538 | unsigned int queuenum, void *data) | 527 | unsigned int queuenum, void *data) |
539 | { | 528 | { |
540 | int status = -EINVAL; | 529 | int status = -EINVAL; |
541 | struct sk_buff *nskb; | 530 | struct sk_buff *nskb; |
542 | struct nfqnl_instance *queue; | 531 | struct nfqnl_instance *queue; |
543 | struct nfqnl_queue_entry *entry; | 532 | struct nfqnl_queue_entry *entry; |
544 | 533 | ||
545 | QDEBUG("entered\n"); | 534 | QDEBUG("entered\n"); |
546 | 535 | ||
547 | queue = instance_lookup_get(queuenum); | 536 | queue = instance_lookup_get(queuenum); |
548 | if (!queue) { | 537 | if (!queue) { |
549 | QDEBUG("no queue instance matching\n"); | 538 | QDEBUG("no queue instance matching\n"); |
550 | return -EINVAL; | 539 | return -EINVAL; |
551 | } | 540 | } |
552 | 541 | ||
553 | if (queue->copy_mode == NFQNL_COPY_NONE) { | 542 | if (queue->copy_mode == NFQNL_COPY_NONE) { |
554 | QDEBUG("mode COPY_NONE, aborting\n"); | 543 | QDEBUG("mode COPY_NONE, aborting\n"); |
555 | status = -EAGAIN; | 544 | status = -EAGAIN; |
556 | goto err_out_put; | 545 | goto err_out_put; |
557 | } | 546 | } |
558 | 547 | ||
559 | entry = kmalloc(sizeof(*entry), GFP_ATOMIC); | 548 | entry = kmalloc(sizeof(*entry), GFP_ATOMIC); |
560 | if (entry == NULL) { | 549 | if (entry == NULL) { |
561 | if (net_ratelimit()) | 550 | if (net_ratelimit()) |
562 | printk(KERN_ERR | 551 | printk(KERN_ERR |
563 | "nf_queue: OOM in nfqnl_enqueue_packet()\n"); | 552 | "nf_queue: OOM in nfqnl_enqueue_packet()\n"); |
564 | status = -ENOMEM; | 553 | status = -ENOMEM; |
565 | goto err_out_put; | 554 | goto err_out_put; |
566 | } | 555 | } |
567 | 556 | ||
568 | entry->info = info; | 557 | entry->info = info; |
569 | entry->skb = skb; | 558 | entry->skb = skb; |
570 | entry->id = atomic_inc_return(&queue->id_sequence); | 559 | entry->id = atomic_inc_return(&queue->id_sequence); |
571 | 560 | ||
572 | nskb = nfqnl_build_packet_message(queue, entry, &status); | 561 | nskb = nfqnl_build_packet_message(queue, entry, &status); |
573 | if (nskb == NULL) | 562 | if (nskb == NULL) |
574 | goto err_out_free; | 563 | goto err_out_free; |
575 | 564 | ||
576 | spin_lock_bh(&queue->lock); | 565 | spin_lock_bh(&queue->lock); |
577 | 566 | ||
578 | if (!queue->peer_pid) | 567 | if (!queue->peer_pid) |
579 | goto err_out_free_nskb; | 568 | goto err_out_free_nskb; |
580 | 569 | ||
581 | if (queue->queue_total >= queue->queue_maxlen) { | 570 | if (queue->queue_total >= queue->queue_maxlen) { |
582 | queue->queue_dropped++; | 571 | queue->queue_dropped++; |
583 | status = -ENOSPC; | 572 | status = -ENOSPC; |
584 | if (net_ratelimit()) | 573 | if (net_ratelimit()) |
585 | printk(KERN_WARNING "ip_queue: full at %d entries, " | 574 | printk(KERN_WARNING "ip_queue: full at %d entries, " |
586 | "dropping packets(s). Dropped: %d\n", | 575 | "dropping packets(s). Dropped: %d\n", |
587 | queue->queue_total, queue->queue_dropped); | 576 | queue->queue_total, queue->queue_dropped); |
588 | goto err_out_free_nskb; | 577 | goto err_out_free_nskb; |
589 | } | 578 | } |
590 | 579 | ||
591 | /* nfnetlink_unicast will either free the nskb or add it to a socket */ | 580 | /* nfnetlink_unicast will either free the nskb or add it to a socket */ |
592 | status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT); | 581 | status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT); |
593 | if (status < 0) { | 582 | if (status < 0) { |
594 | queue->queue_user_dropped++; | 583 | queue->queue_user_dropped++; |
595 | goto err_out_unlock; | 584 | goto err_out_unlock; |
596 | } | 585 | } |
597 | 586 | ||
598 | __enqueue_entry(queue, entry); | 587 | __enqueue_entry(queue, entry); |
599 | 588 | ||
600 | spin_unlock_bh(&queue->lock); | 589 | spin_unlock_bh(&queue->lock); |
601 | instance_put(queue); | 590 | instance_put(queue); |
602 | return status; | 591 | return status; |
603 | 592 | ||
604 | err_out_free_nskb: | 593 | err_out_free_nskb: |
605 | kfree_skb(nskb); | 594 | kfree_skb(nskb); |
606 | 595 | ||
607 | err_out_unlock: | 596 | err_out_unlock: |
608 | spin_unlock_bh(&queue->lock); | 597 | spin_unlock_bh(&queue->lock); |
609 | 598 | ||
610 | err_out_free: | 599 | err_out_free: |
611 | kfree(entry); | 600 | kfree(entry); |
612 | err_out_put: | 601 | err_out_put: |
613 | instance_put(queue); | 602 | instance_put(queue); |
614 | return status; | 603 | return status; |
615 | } | 604 | } |
616 | 605 | ||
617 | static int | 606 | static int |
618 | nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e) | 607 | nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e) |
619 | { | 608 | { |
620 | int diff; | 609 | int diff; |
621 | 610 | ||
622 | diff = data_len - e->skb->len; | 611 | diff = data_len - e->skb->len; |
623 | if (diff < 0) | 612 | if (diff < 0) |
624 | skb_trim(e->skb, data_len); | 613 | skb_trim(e->skb, data_len); |
625 | else if (diff > 0) { | 614 | else if (diff > 0) { |
626 | if (data_len > 0xFFFF) | 615 | if (data_len > 0xFFFF) |
627 | return -EINVAL; | 616 | return -EINVAL; |
628 | if (diff > skb_tailroom(e->skb)) { | 617 | if (diff > skb_tailroom(e->skb)) { |
629 | struct sk_buff *newskb; | 618 | struct sk_buff *newskb; |
630 | 619 | ||
631 | newskb = skb_copy_expand(e->skb, | 620 | newskb = skb_copy_expand(e->skb, |
632 | skb_headroom(e->skb), | 621 | skb_headroom(e->skb), |
633 | diff, | 622 | diff, |
634 | GFP_ATOMIC); | 623 | GFP_ATOMIC); |
635 | if (newskb == NULL) { | 624 | if (newskb == NULL) { |
636 | printk(KERN_WARNING "ip_queue: OOM " | 625 | printk(KERN_WARNING "ip_queue: OOM " |
637 | "in mangle, dropping packet\n"); | 626 | "in mangle, dropping packet\n"); |
638 | return -ENOMEM; | 627 | return -ENOMEM; |
639 | } | 628 | } |
640 | if (e->skb->sk) | 629 | if (e->skb->sk) |
641 | skb_set_owner_w(newskb, e->skb->sk); | 630 | skb_set_owner_w(newskb, e->skb->sk); |
642 | kfree_skb(e->skb); | 631 | kfree_skb(e->skb); |
643 | e->skb = newskb; | 632 | e->skb = newskb; |
644 | } | 633 | } |
645 | skb_put(e->skb, diff); | 634 | skb_put(e->skb, diff); |
646 | } | 635 | } |
647 | if (!skb_make_writable(&e->skb, data_len)) | 636 | if (!skb_make_writable(&e->skb, data_len)) |
648 | return -ENOMEM; | 637 | return -ENOMEM; |
649 | memcpy(e->skb->data, data, data_len); | 638 | memcpy(e->skb->data, data, data_len); |
650 | 639 | ||
651 | return 0; | 640 | return 0; |
652 | } | 641 | } |
653 | 642 | ||
654 | static inline int | 643 | static inline int |
655 | id_cmp(struct nfqnl_queue_entry *e, unsigned long id) | 644 | id_cmp(struct nfqnl_queue_entry *e, unsigned long id) |
656 | { | 645 | { |
657 | return (id == e->id); | 646 | return (id == e->id); |
658 | } | 647 | } |
659 | 648 | ||
660 | static int | 649 | static int |
661 | nfqnl_set_mode(struct nfqnl_instance *queue, | 650 | nfqnl_set_mode(struct nfqnl_instance *queue, |
662 | unsigned char mode, unsigned int range) | 651 | unsigned char mode, unsigned int range) |
663 | { | 652 | { |
664 | int status; | 653 | int status; |
665 | 654 | ||
666 | spin_lock_bh(&queue->lock); | 655 | spin_lock_bh(&queue->lock); |
667 | status = __nfqnl_set_mode(queue, mode, range); | 656 | status = __nfqnl_set_mode(queue, mode, range); |
668 | spin_unlock_bh(&queue->lock); | 657 | spin_unlock_bh(&queue->lock); |
669 | 658 | ||
670 | return status; | 659 | return status; |
671 | } | 660 | } |
672 | 661 | ||
673 | static int | 662 | static int |
674 | dev_cmp(struct nfqnl_queue_entry *entry, unsigned long ifindex) | 663 | dev_cmp(struct nfqnl_queue_entry *entry, unsigned long ifindex) |
675 | { | 664 | { |
676 | if (entry->info->indev) | 665 | if (entry->info->indev) |
677 | if (entry->info->indev->ifindex == ifindex) | 666 | if (entry->info->indev->ifindex == ifindex) |
678 | return 1; | 667 | return 1; |
679 | 668 | ||
680 | if (entry->info->outdev) | 669 | if (entry->info->outdev) |
681 | if (entry->info->outdev->ifindex == ifindex) | 670 | if (entry->info->outdev->ifindex == ifindex) |
682 | return 1; | 671 | return 1; |
683 | 672 | ||
684 | return 0; | 673 | return 0; |
685 | } | 674 | } |
686 | 675 | ||
687 | /* drop all packets with either indev or outdev == ifindex from all queue | 676 | /* drop all packets with either indev or outdev == ifindex from all queue |
688 | * instances */ | 677 | * instances */ |
689 | static void | 678 | static void |
690 | nfqnl_dev_drop(int ifindex) | 679 | nfqnl_dev_drop(int ifindex) |
691 | { | 680 | { |
692 | int i; | 681 | int i; |
693 | 682 | ||
694 | QDEBUG("entering for ifindex %u\n", ifindex); | 683 | QDEBUG("entering for ifindex %u\n", ifindex); |
695 | 684 | ||
696 | /* this only looks like we have to hold the readlock for a way too long | 685 | /* this only looks like we have to hold the readlock for a way too long |
697 | * time, issue_verdict(), nf_reinject(), ... - but we always only | 686 | * time, issue_verdict(), nf_reinject(), ... - but we always only |
698 | * issue NF_DROP, which is processed directly in nf_reinject() */ | 687 | * issue NF_DROP, which is processed directly in nf_reinject() */ |
699 | read_lock_bh(&instances_lock); | 688 | read_lock_bh(&instances_lock); |
700 | 689 | ||
701 | for (i = 0; i < INSTANCE_BUCKETS; i++) { | 690 | for (i = 0; i < INSTANCE_BUCKETS; i++) { |
702 | struct hlist_node *tmp; | 691 | struct hlist_node *tmp; |
703 | struct nfqnl_instance *inst; | 692 | struct nfqnl_instance *inst; |
704 | struct hlist_head *head = &instance_table[i]; | 693 | struct hlist_head *head = &instance_table[i]; |
705 | 694 | ||
706 | hlist_for_each_entry(inst, tmp, head, hlist) { | 695 | hlist_for_each_entry(inst, tmp, head, hlist) { |
707 | struct nfqnl_queue_entry *entry; | 696 | struct nfqnl_queue_entry *entry; |
708 | while ((entry = find_dequeue_entry(inst, dev_cmp, | 697 | while ((entry = find_dequeue_entry(inst, dev_cmp, |
709 | ifindex)) != NULL) | 698 | ifindex)) != NULL) |
710 | issue_verdict(entry, NF_DROP); | 699 | issue_verdict(entry, NF_DROP); |
711 | } | 700 | } |
712 | } | 701 | } |
713 | 702 | ||
714 | read_unlock_bh(&instances_lock); | 703 | read_unlock_bh(&instances_lock); |
715 | } | 704 | } |
716 | 705 | ||
717 | #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0) | 706 | #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0) |
718 | 707 | ||
719 | static int | 708 | static int |
720 | nfqnl_rcv_dev_event(struct notifier_block *this, | 709 | nfqnl_rcv_dev_event(struct notifier_block *this, |
721 | unsigned long event, void *ptr) | 710 | unsigned long event, void *ptr) |
722 | { | 711 | { |
723 | struct net_device *dev = ptr; | 712 | struct net_device *dev = ptr; |
724 | 713 | ||
725 | /* Drop any packets associated with the downed device */ | 714 | /* Drop any packets associated with the downed device */ |
726 | if (event == NETDEV_DOWN) | 715 | if (event == NETDEV_DOWN) |
727 | nfqnl_dev_drop(dev->ifindex); | 716 | nfqnl_dev_drop(dev->ifindex); |
728 | return NOTIFY_DONE; | 717 | return NOTIFY_DONE; |
729 | } | 718 | } |
730 | 719 | ||
731 | static struct notifier_block nfqnl_dev_notifier = { | 720 | static struct notifier_block nfqnl_dev_notifier = { |
732 | .notifier_call = nfqnl_rcv_dev_event, | 721 | .notifier_call = nfqnl_rcv_dev_event, |
733 | }; | 722 | }; |
734 | 723 | ||
735 | static int | 724 | static int |
736 | nfqnl_rcv_nl_event(struct notifier_block *this, | 725 | nfqnl_rcv_nl_event(struct notifier_block *this, |
737 | unsigned long event, void *ptr) | 726 | unsigned long event, void *ptr) |
738 | { | 727 | { |
739 | struct netlink_notify *n = ptr; | 728 | struct netlink_notify *n = ptr; |
740 | 729 | ||
741 | if (event == NETLINK_URELEASE && | 730 | if (event == NETLINK_URELEASE && |
742 | n->protocol == NETLINK_NETFILTER && n->pid) { | 731 | n->protocol == NETLINK_NETFILTER && n->pid) { |
743 | int i; | 732 | int i; |
744 | 733 | ||
745 | /* destroy all instances for this pid */ | 734 | /* destroy all instances for this pid */ |
746 | write_lock_bh(&instances_lock); | 735 | write_lock_bh(&instances_lock); |
747 | for (i = 0; i < INSTANCE_BUCKETS; i++) { | 736 | for (i = 0; i < INSTANCE_BUCKETS; i++) { |
748 | struct hlist_node *tmp, *t2; | 737 | struct hlist_node *tmp, *t2; |
749 | struct nfqnl_instance *inst; | 738 | struct nfqnl_instance *inst; |
750 | struct hlist_head *head = &instance_table[i]; | 739 | struct hlist_head *head = &instance_table[i]; |
751 | 740 | ||
752 | hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) { | 741 | hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) { |
753 | if (n->pid == inst->peer_pid) | 742 | if (n->pid == inst->peer_pid) |
754 | __instance_destroy(inst); | 743 | __instance_destroy(inst); |
755 | } | 744 | } |
756 | } | 745 | } |
757 | write_unlock_bh(&instances_lock); | 746 | write_unlock_bh(&instances_lock); |
758 | } | 747 | } |
759 | return NOTIFY_DONE; | 748 | return NOTIFY_DONE; |
760 | } | 749 | } |
761 | 750 | ||
762 | static struct notifier_block nfqnl_rtnl_notifier = { | 751 | static struct notifier_block nfqnl_rtnl_notifier = { |
763 | .notifier_call = nfqnl_rcv_nl_event, | 752 | .notifier_call = nfqnl_rcv_nl_event, |
764 | }; | 753 | }; |
765 | 754 | ||
766 | static const int nfqa_verdict_min[NFQA_MAX] = { | 755 | static const int nfqa_verdict_min[NFQA_MAX] = { |
767 | [NFQA_VERDICT_HDR-1] = sizeof(struct nfqnl_msg_verdict_hdr), | 756 | [NFQA_VERDICT_HDR-1] = sizeof(struct nfqnl_msg_verdict_hdr), |
768 | [NFQA_MARK-1] = sizeof(u_int32_t), | 757 | [NFQA_MARK-1] = sizeof(u_int32_t), |
769 | [NFQA_PAYLOAD-1] = 0, | 758 | [NFQA_PAYLOAD-1] = 0, |
770 | }; | 759 | }; |
771 | 760 | ||
772 | static int | 761 | static int |
773 | nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb, | 762 | nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb, |
774 | struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp) | 763 | struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp) |
775 | { | 764 | { |
776 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); | 765 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); |
777 | u_int16_t queue_num = ntohs(nfmsg->res_id); | 766 | u_int16_t queue_num = ntohs(nfmsg->res_id); |
778 | 767 | ||
779 | struct nfqnl_msg_verdict_hdr *vhdr; | 768 | struct nfqnl_msg_verdict_hdr *vhdr; |
780 | struct nfqnl_instance *queue; | 769 | struct nfqnl_instance *queue; |
781 | unsigned int verdict; | 770 | unsigned int verdict; |
782 | struct nfqnl_queue_entry *entry; | 771 | struct nfqnl_queue_entry *entry; |
783 | int err; | 772 | int err; |
784 | 773 | ||
785 | if (nfattr_bad_size(nfqa, NFQA_MAX, nfqa_verdict_min)) { | 774 | if (nfattr_bad_size(nfqa, NFQA_MAX, nfqa_verdict_min)) { |
786 | QDEBUG("bad attribute size\n"); | 775 | QDEBUG("bad attribute size\n"); |
787 | return -EINVAL; | 776 | return -EINVAL; |
788 | } | 777 | } |
789 | 778 | ||
790 | queue = instance_lookup_get(queue_num); | 779 | queue = instance_lookup_get(queue_num); |
791 | if (!queue) | 780 | if (!queue) |
792 | return -ENODEV; | 781 | return -ENODEV; |
793 | 782 | ||
794 | if (queue->peer_pid != NETLINK_CB(skb).pid) { | 783 | if (queue->peer_pid != NETLINK_CB(skb).pid) { |
795 | err = -EPERM; | 784 | err = -EPERM; |
796 | goto err_out_put; | 785 | goto err_out_put; |
797 | } | 786 | } |
798 | 787 | ||
799 | if (!nfqa[NFQA_VERDICT_HDR-1]) { | 788 | if (!nfqa[NFQA_VERDICT_HDR-1]) { |
800 | err = -EINVAL; | 789 | err = -EINVAL; |
801 | goto err_out_put; | 790 | goto err_out_put; |
802 | } | 791 | } |
803 | 792 | ||
804 | vhdr = NFA_DATA(nfqa[NFQA_VERDICT_HDR-1]); | 793 | vhdr = NFA_DATA(nfqa[NFQA_VERDICT_HDR-1]); |
805 | verdict = ntohl(vhdr->verdict); | 794 | verdict = ntohl(vhdr->verdict); |
806 | 795 | ||
807 | if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) { | 796 | if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) { |
808 | err = -EINVAL; | 797 | err = -EINVAL; |
809 | goto err_out_put; | 798 | goto err_out_put; |
810 | } | 799 | } |
811 | 800 | ||
812 | entry = find_dequeue_entry(queue, id_cmp, ntohl(vhdr->id)); | 801 | entry = find_dequeue_entry(queue, id_cmp, ntohl(vhdr->id)); |
813 | if (entry == NULL) { | 802 | if (entry == NULL) { |
814 | err = -ENOENT; | 803 | err = -ENOENT; |
815 | goto err_out_put; | 804 | goto err_out_put; |
816 | } | 805 | } |
817 | 806 | ||
818 | if (nfqa[NFQA_PAYLOAD-1]) { | 807 | if (nfqa[NFQA_PAYLOAD-1]) { |
819 | if (nfqnl_mangle(NFA_DATA(nfqa[NFQA_PAYLOAD-1]), | 808 | if (nfqnl_mangle(NFA_DATA(nfqa[NFQA_PAYLOAD-1]), |
820 | NFA_PAYLOAD(nfqa[NFQA_PAYLOAD-1]), entry) < 0) | 809 | NFA_PAYLOAD(nfqa[NFQA_PAYLOAD-1]), entry) < 0) |
821 | verdict = NF_DROP; | 810 | verdict = NF_DROP; |
822 | } | 811 | } |
823 | 812 | ||
824 | if (nfqa[NFQA_MARK-1]) | 813 | if (nfqa[NFQA_MARK-1]) |
825 | skb->nfmark = ntohl(*(u_int32_t *)NFA_DATA(nfqa[NFQA_MARK-1])); | 814 | skb->nfmark = ntohl(*(u_int32_t *)NFA_DATA(nfqa[NFQA_MARK-1])); |
826 | 815 | ||
827 | issue_verdict(entry, verdict); | 816 | issue_verdict(entry, verdict); |
828 | instance_put(queue); | 817 | instance_put(queue); |
829 | return 0; | 818 | return 0; |
830 | 819 | ||
831 | err_out_put: | 820 | err_out_put: |
832 | instance_put(queue); | 821 | instance_put(queue); |
833 | return err; | 822 | return err; |
834 | } | 823 | } |
835 | 824 | ||
836 | static int | 825 | static int |
837 | nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb, | 826 | nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb, |
838 | struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp) | 827 | struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp) |
839 | { | 828 | { |
840 | return -ENOTSUPP; | 829 | return -ENOTSUPP; |
841 | } | 830 | } |
842 | 831 | ||
843 | static const int nfqa_cfg_min[NFQA_CFG_MAX] = { | 832 | static const int nfqa_cfg_min[NFQA_CFG_MAX] = { |
844 | [NFQA_CFG_CMD-1] = sizeof(struct nfqnl_msg_config_cmd), | 833 | [NFQA_CFG_CMD-1] = sizeof(struct nfqnl_msg_config_cmd), |
845 | [NFQA_CFG_PARAMS-1] = sizeof(struct nfqnl_msg_config_params), | 834 | [NFQA_CFG_PARAMS-1] = sizeof(struct nfqnl_msg_config_params), |
846 | }; | 835 | }; |
847 | 836 | ||
848 | static struct nf_queue_handler nfqh = { | 837 | static struct nf_queue_handler nfqh = { |
849 | .name = "nf_queue", | 838 | .name = "nf_queue", |
850 | .outfn = &nfqnl_enqueue_packet, | 839 | .outfn = &nfqnl_enqueue_packet, |
851 | }; | 840 | }; |
852 | 841 | ||
853 | static int | 842 | static int |
854 | nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb, | 843 | nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb, |
855 | struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp) | 844 | struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp) |
856 | { | 845 | { |
857 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); | 846 | struct nfgenmsg *nfmsg = NLMSG_DATA(nlh); |
858 | u_int16_t queue_num = ntohs(nfmsg->res_id); | 847 | u_int16_t queue_num = ntohs(nfmsg->res_id); |
859 | struct nfqnl_instance *queue; | 848 | struct nfqnl_instance *queue; |
860 | int ret = 0; | 849 | int ret = 0; |
861 | 850 | ||
862 | QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type)); | 851 | QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type)); |
863 | 852 | ||
864 | if (nfattr_bad_size(nfqa, NFQA_CFG_MAX, nfqa_cfg_min)) { | 853 | if (nfattr_bad_size(nfqa, NFQA_CFG_MAX, nfqa_cfg_min)) { |
865 | QDEBUG("bad attribute size\n"); | 854 | QDEBUG("bad attribute size\n"); |
866 | return -EINVAL; | 855 | return -EINVAL; |
867 | } | 856 | } |
868 | 857 | ||
869 | queue = instance_lookup_get(queue_num); | 858 | queue = instance_lookup_get(queue_num); |
870 | if (nfqa[NFQA_CFG_CMD-1]) { | 859 | if (nfqa[NFQA_CFG_CMD-1]) { |
871 | struct nfqnl_msg_config_cmd *cmd; | 860 | struct nfqnl_msg_config_cmd *cmd; |
872 | cmd = NFA_DATA(nfqa[NFQA_CFG_CMD-1]); | 861 | cmd = NFA_DATA(nfqa[NFQA_CFG_CMD-1]); |
873 | QDEBUG("found CFG_CMD\n"); | 862 | QDEBUG("found CFG_CMD\n"); |
874 | 863 | ||
875 | switch (cmd->command) { | 864 | switch (cmd->command) { |
876 | case NFQNL_CFG_CMD_BIND: | 865 | case NFQNL_CFG_CMD_BIND: |
877 | if (queue) | 866 | if (queue) |
878 | return -EBUSY; | 867 | return -EBUSY; |
879 | 868 | ||
880 | queue = instance_create(queue_num, NETLINK_CB(skb).pid); | 869 | queue = instance_create(queue_num, NETLINK_CB(skb).pid); |
881 | if (!queue) | 870 | if (!queue) |
882 | return -EINVAL; | 871 | return -EINVAL; |
883 | break; | 872 | break; |
884 | case NFQNL_CFG_CMD_UNBIND: | 873 | case NFQNL_CFG_CMD_UNBIND: |
885 | if (!queue) | 874 | if (!queue) |
886 | return -ENODEV; | 875 | return -ENODEV; |
887 | 876 | ||
888 | if (queue->peer_pid != NETLINK_CB(skb).pid) { | 877 | if (queue->peer_pid != NETLINK_CB(skb).pid) { |
889 | ret = -EPERM; | 878 | ret = -EPERM; |
890 | goto out_put; | 879 | goto out_put; |
891 | } | 880 | } |
892 | 881 | ||
893 | instance_destroy(queue); | 882 | instance_destroy(queue); |
894 | break; | 883 | break; |
895 | case NFQNL_CFG_CMD_PF_BIND: | 884 | case NFQNL_CFG_CMD_PF_BIND: |
896 | QDEBUG("registering queue handler for pf=%u\n", | 885 | QDEBUG("registering queue handler for pf=%u\n", |
897 | ntohs(cmd->pf)); | 886 | ntohs(cmd->pf)); |
898 | ret = nf_register_queue_handler(ntohs(cmd->pf), &nfqh); | 887 | ret = nf_register_queue_handler(ntohs(cmd->pf), &nfqh); |
899 | break; | 888 | break; |
900 | case NFQNL_CFG_CMD_PF_UNBIND: | 889 | case NFQNL_CFG_CMD_PF_UNBIND: |
901 | QDEBUG("unregistering queue handler for pf=%u\n", | 890 | QDEBUG("unregistering queue handler for pf=%u\n", |
902 | ntohs(cmd->pf)); | 891 | ntohs(cmd->pf)); |
903 | /* This is a bug and a feature. We can unregister | 892 | /* This is a bug and a feature. We can unregister |
904 | * other handlers(!) */ | 893 | * other handlers(!) */ |
905 | ret = nf_unregister_queue_handler(ntohs(cmd->pf)); | 894 | ret = nf_unregister_queue_handler(ntohs(cmd->pf)); |
906 | break; | 895 | break; |
907 | default: | 896 | default: |
908 | ret = -EINVAL; | 897 | ret = -EINVAL; |
909 | break; | 898 | break; |
910 | } | 899 | } |
911 | } else { | 900 | } else { |
912 | if (!queue) { | 901 | if (!queue) { |
913 | QDEBUG("no config command, and no instance ENOENT\n"); | 902 | QDEBUG("no config command, and no instance ENOENT\n"); |
914 | ret = -ENOENT; | 903 | ret = -ENOENT; |
915 | goto out_put; | 904 | goto out_put; |
916 | } | 905 | } |
917 | 906 | ||
918 | if (queue->peer_pid != NETLINK_CB(skb).pid) { | 907 | if (queue->peer_pid != NETLINK_CB(skb).pid) { |
919 | QDEBUG("no config command, and wrong pid\n"); | 908 | QDEBUG("no config command, and wrong pid\n"); |
920 | ret = -EPERM; | 909 | ret = -EPERM; |
921 | goto out_put; | 910 | goto out_put; |
922 | } | 911 | } |
923 | } | 912 | } |
924 | 913 | ||
925 | if (nfqa[NFQA_CFG_PARAMS-1]) { | 914 | if (nfqa[NFQA_CFG_PARAMS-1]) { |
926 | struct nfqnl_msg_config_params *params; | 915 | struct nfqnl_msg_config_params *params; |
927 | params = NFA_DATA(nfqa[NFQA_CFG_PARAMS-1]); | 916 | params = NFA_DATA(nfqa[NFQA_CFG_PARAMS-1]); |
928 | 917 | ||
929 | nfqnl_set_mode(queue, params->copy_mode, | 918 | nfqnl_set_mode(queue, params->copy_mode, |
930 | ntohl(params->copy_range)); | 919 | ntohl(params->copy_range)); |
931 | } | 920 | } |
932 | 921 | ||
933 | out_put: | 922 | out_put: |
934 | instance_put(queue); | 923 | instance_put(queue); |
935 | return ret; | 924 | return ret; |
936 | } | 925 | } |
937 | 926 | ||
938 | static struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = { | 927 | static struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = { |
939 | [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp, | 928 | [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp, |
940 | .attr_count = NFQA_MAX, | 929 | .attr_count = NFQA_MAX, |
941 | .cap_required = CAP_NET_ADMIN }, | 930 | .cap_required = CAP_NET_ADMIN }, |
942 | [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict, | 931 | [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict, |
943 | .attr_count = NFQA_MAX, | 932 | .attr_count = NFQA_MAX, |
944 | .cap_required = CAP_NET_ADMIN }, | 933 | .cap_required = CAP_NET_ADMIN }, |
945 | [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config, | 934 | [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config, |
946 | .attr_count = NFQA_CFG_MAX, | 935 | .attr_count = NFQA_CFG_MAX, |
947 | .cap_required = CAP_NET_ADMIN }, | 936 | .cap_required = CAP_NET_ADMIN }, |
948 | }; | 937 | }; |
949 | 938 | ||
950 | static struct nfnetlink_subsystem nfqnl_subsys = { | 939 | static struct nfnetlink_subsystem nfqnl_subsys = { |
951 | .name = "nf_queue", | 940 | .name = "nf_queue", |
952 | .subsys_id = NFNL_SUBSYS_QUEUE, | 941 | .subsys_id = NFNL_SUBSYS_QUEUE, |
953 | .cb_count = NFQNL_MSG_MAX, | 942 | .cb_count = NFQNL_MSG_MAX, |
954 | .cb = nfqnl_cb, | 943 | .cb = nfqnl_cb, |
955 | }; | 944 | }; |
956 | 945 | ||
957 | #ifdef CONFIG_PROC_FS | 946 | #ifdef CONFIG_PROC_FS |
958 | struct iter_state { | 947 | struct iter_state { |
959 | unsigned int bucket; | 948 | unsigned int bucket; |
960 | }; | 949 | }; |
961 | 950 | ||
962 | static struct hlist_node *get_first(struct seq_file *seq) | 951 | static struct hlist_node *get_first(struct seq_file *seq) |
963 | { | 952 | { |
964 | struct iter_state *st = seq->private; | 953 | struct iter_state *st = seq->private; |
965 | 954 | ||
966 | if (!st) | 955 | if (!st) |
967 | return NULL; | 956 | return NULL; |
968 | 957 | ||
969 | for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) { | 958 | for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) { |
970 | if (!hlist_empty(&instance_table[st->bucket])) | 959 | if (!hlist_empty(&instance_table[st->bucket])) |
971 | return instance_table[st->bucket].first; | 960 | return instance_table[st->bucket].first; |
972 | } | 961 | } |
973 | return NULL; | 962 | return NULL; |
974 | } | 963 | } |
975 | 964 | ||
976 | static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h) | 965 | static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h) |
977 | { | 966 | { |
978 | struct iter_state *st = seq->private; | 967 | struct iter_state *st = seq->private; |
979 | 968 | ||
980 | h = h->next; | 969 | h = h->next; |
981 | while (!h) { | 970 | while (!h) { |
982 | if (++st->bucket >= INSTANCE_BUCKETS) | 971 | if (++st->bucket >= INSTANCE_BUCKETS) |
983 | return NULL; | 972 | return NULL; |
984 | 973 | ||
985 | h = instance_table[st->bucket].first; | 974 | h = instance_table[st->bucket].first; |
986 | } | 975 | } |
987 | return h; | 976 | return h; |
988 | } | 977 | } |
989 | 978 | ||
990 | static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos) | 979 | static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos) |
991 | { | 980 | { |
992 | struct hlist_node *head; | 981 | struct hlist_node *head; |
993 | head = get_first(seq); | 982 | head = get_first(seq); |
994 | 983 | ||
995 | if (head) | 984 | if (head) |
996 | while (pos && (head = get_next(seq, head))) | 985 | while (pos && (head = get_next(seq, head))) |
997 | pos--; | 986 | pos--; |
998 | return pos ? NULL : head; | 987 | return pos ? NULL : head; |
999 | } | 988 | } |
1000 | 989 | ||
1001 | static void *seq_start(struct seq_file *seq, loff_t *pos) | 990 | static void *seq_start(struct seq_file *seq, loff_t *pos) |
1002 | { | 991 | { |
1003 | read_lock_bh(&instances_lock); | 992 | read_lock_bh(&instances_lock); |
1004 | return get_idx(seq, *pos); | 993 | return get_idx(seq, *pos); |
1005 | } | 994 | } |
1006 | 995 | ||
1007 | static void *seq_next(struct seq_file *s, void *v, loff_t *pos) | 996 | static void *seq_next(struct seq_file *s, void *v, loff_t *pos) |
1008 | { | 997 | { |
1009 | (*pos)++; | 998 | (*pos)++; |
1010 | return get_next(s, v); | 999 | return get_next(s, v); |
1011 | } | 1000 | } |
1012 | 1001 | ||
1013 | static void seq_stop(struct seq_file *s, void *v) | 1002 | static void seq_stop(struct seq_file *s, void *v) |
1014 | { | 1003 | { |
1015 | read_unlock_bh(&instances_lock); | 1004 | read_unlock_bh(&instances_lock); |
1016 | } | 1005 | } |
1017 | 1006 | ||
1018 | static int seq_show(struct seq_file *s, void *v) | 1007 | static int seq_show(struct seq_file *s, void *v) |
1019 | { | 1008 | { |
1020 | const struct nfqnl_instance *inst = v; | 1009 | const struct nfqnl_instance *inst = v; |
1021 | 1010 | ||
1022 | return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n", | 1011 | return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n", |
1023 | inst->queue_num, | 1012 | inst->queue_num, |
1024 | inst->peer_pid, inst->queue_total, | 1013 | inst->peer_pid, inst->queue_total, |
1025 | inst->copy_mode, inst->copy_range, | 1014 | inst->copy_mode, inst->copy_range, |
1026 | inst->queue_dropped, inst->queue_user_dropped, | 1015 | inst->queue_dropped, inst->queue_user_dropped, |
1027 | atomic_read(&inst->id_sequence), | 1016 | atomic_read(&inst->id_sequence), |
1028 | atomic_read(&inst->use)); | 1017 | atomic_read(&inst->use)); |
1029 | } | 1018 | } |
1030 | 1019 | ||
1031 | static struct seq_operations nfqnl_seq_ops = { | 1020 | static struct seq_operations nfqnl_seq_ops = { |
1032 | .start = seq_start, | 1021 | .start = seq_start, |
1033 | .next = seq_next, | 1022 | .next = seq_next, |
1034 | .stop = seq_stop, | 1023 | .stop = seq_stop, |
1035 | .show = seq_show, | 1024 | .show = seq_show, |
1036 | }; | 1025 | }; |
1037 | 1026 | ||
1038 | static int nfqnl_open(struct inode *inode, struct file *file) | 1027 | static int nfqnl_open(struct inode *inode, struct file *file) |
1039 | { | 1028 | { |
1040 | struct seq_file *seq; | 1029 | struct seq_file *seq; |
1041 | struct iter_state *is; | 1030 | struct iter_state *is; |
1042 | int ret; | 1031 | int ret; |
1043 | 1032 | ||
1044 | is = kmalloc(sizeof(*is), GFP_KERNEL); | 1033 | is = kmalloc(sizeof(*is), GFP_KERNEL); |
1045 | if (!is) | 1034 | if (!is) |
1046 | return -ENOMEM; | 1035 | return -ENOMEM; |
1047 | memset(is, 0, sizeof(*is)); | 1036 | memset(is, 0, sizeof(*is)); |
1048 | ret = seq_open(file, &nfqnl_seq_ops); | 1037 | ret = seq_open(file, &nfqnl_seq_ops); |
1049 | if (ret < 0) | 1038 | if (ret < 0) |
1050 | goto out_free; | 1039 | goto out_free; |
1051 | seq = file->private_data; | 1040 | seq = file->private_data; |
1052 | seq->private = is; | 1041 | seq->private = is; |
1053 | return ret; | 1042 | return ret; |
1054 | out_free: | 1043 | out_free: |
1055 | kfree(is); | 1044 | kfree(is); |
1056 | return ret; | 1045 | return ret; |
1057 | } | 1046 | } |
1058 | 1047 | ||
1059 | static struct file_operations nfqnl_file_ops = { | 1048 | static struct file_operations nfqnl_file_ops = { |
1060 | .owner = THIS_MODULE, | 1049 | .owner = THIS_MODULE, |
1061 | .open = nfqnl_open, | 1050 | .open = nfqnl_open, |
1062 | .read = seq_read, | 1051 | .read = seq_read, |
1063 | .llseek = seq_lseek, | 1052 | .llseek = seq_lseek, |
1064 | .release = seq_release_private, | 1053 | .release = seq_release_private, |
1065 | }; | 1054 | }; |
1066 | 1055 | ||
1067 | #endif /* PROC_FS */ | 1056 | #endif /* PROC_FS */ |
1068 | 1057 | ||
1069 | static int | 1058 | static int |
1070 | init_or_cleanup(int init) | 1059 | init_or_cleanup(int init) |
1071 | { | 1060 | { |
1072 | int i, status = -ENOMEM; | 1061 | int i, status = -ENOMEM; |
1073 | #ifdef CONFIG_PROC_FS | 1062 | #ifdef CONFIG_PROC_FS |
1074 | struct proc_dir_entry *proc_nfqueue; | 1063 | struct proc_dir_entry *proc_nfqueue; |
1075 | #endif | 1064 | #endif |
1076 | 1065 | ||
1077 | if (!init) | 1066 | if (!init) |
1078 | goto cleanup; | 1067 | goto cleanup; |
1079 | 1068 | ||
1080 | for (i = 0; i < INSTANCE_BUCKETS; i++) | 1069 | for (i = 0; i < INSTANCE_BUCKETS; i++) |
1081 | INIT_HLIST_HEAD(&instance_table[i]); | 1070 | INIT_HLIST_HEAD(&instance_table[i]); |
1082 | 1071 | ||
1083 | netlink_register_notifier(&nfqnl_rtnl_notifier); | 1072 | netlink_register_notifier(&nfqnl_rtnl_notifier); |
1084 | status = nfnetlink_subsys_register(&nfqnl_subsys); | 1073 | status = nfnetlink_subsys_register(&nfqnl_subsys); |
1085 | if (status < 0) { | 1074 | if (status < 0) { |
1086 | printk(KERN_ERR "nf_queue: failed to create netlink socket\n"); | 1075 | printk(KERN_ERR "nf_queue: failed to create netlink socket\n"); |
1087 | goto cleanup_netlink_notifier; | 1076 | goto cleanup_netlink_notifier; |
1088 | } | 1077 | } |
1089 | 1078 | ||
1090 | #ifdef CONFIG_PROC_FS | 1079 | #ifdef CONFIG_PROC_FS |
1091 | proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440, | 1080 | proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440, |
1092 | proc_net_netfilter); | 1081 | proc_net_netfilter); |
1093 | if (!proc_nfqueue) | 1082 | if (!proc_nfqueue) |
1094 | goto cleanup_subsys; | 1083 | goto cleanup_subsys; |
1095 | proc_nfqueue->proc_fops = &nfqnl_file_ops; | 1084 | proc_nfqueue->proc_fops = &nfqnl_file_ops; |
1096 | #endif | 1085 | #endif |
1097 | 1086 | ||
1098 | register_netdevice_notifier(&nfqnl_dev_notifier); | 1087 | register_netdevice_notifier(&nfqnl_dev_notifier); |
1099 | 1088 | ||
1100 | return status; | 1089 | return status; |
1101 | 1090 | ||
1102 | cleanup: | 1091 | cleanup: |
1103 | nf_unregister_queue_handlers(&nfqh); | 1092 | nf_unregister_queue_handlers(&nfqh); |
1104 | unregister_netdevice_notifier(&nfqnl_dev_notifier); | 1093 | unregister_netdevice_notifier(&nfqnl_dev_notifier); |
1105 | #ifdef CONFIG_PROC_FS | 1094 | #ifdef CONFIG_PROC_FS |
1106 | remove_proc_entry("nfnetlink_queue", proc_net_netfilter); | 1095 | remove_proc_entry("nfnetlink_queue", proc_net_netfilter); |
1107 | cleanup_subsys: | 1096 | cleanup_subsys: |
1108 | #endif | 1097 | #endif |
1109 | nfnetlink_subsys_unregister(&nfqnl_subsys); | 1098 | nfnetlink_subsys_unregister(&nfqnl_subsys); |
1110 | cleanup_netlink_notifier: | 1099 | cleanup_netlink_notifier: |
1111 | netlink_unregister_notifier(&nfqnl_rtnl_notifier); | 1100 | netlink_unregister_notifier(&nfqnl_rtnl_notifier); |
1112 | return status; | 1101 | return status; |
1113 | } | 1102 | } |
1114 | 1103 | ||
1115 | static int __init init(void) | 1104 | static int __init init(void) |
1116 | { | 1105 | { |
1117 | 1106 | ||
1118 | return init_or_cleanup(1); | 1107 | return init_or_cleanup(1); |
1119 | } | 1108 | } |
1120 | 1109 | ||
1121 | static void __exit fini(void) | 1110 | static void __exit fini(void) |
1122 | { | 1111 | { |
1123 | init_or_cleanup(0); | 1112 | init_or_cleanup(0); |
1124 | } | 1113 | } |
1125 | 1114 | ||
1126 | MODULE_DESCRIPTION("netfilter packet queue handler"); | 1115 | MODULE_DESCRIPTION("netfilter packet queue handler"); |
1127 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); | 1116 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); |
1128 | MODULE_LICENSE("GPL"); | 1117 | MODULE_LICENSE("GPL"); |
1129 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE); | 1118 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE); |
1130 | 1119 | ||
1131 | module_init(init); | 1120 | module_init(init); |
1132 | module_exit(fini); | 1121 | module_exit(fini); |
1133 | 1122 |