blob: 9e72246ede25b4e5be1668bfa6384b80172d93a6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This is a module which is used for queueing IPv4 packets and
3 * communicating with userspace via netlink.
4 *
5 * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
Harald Welte9bb7bc92005-05-30 15:35:26 -07006 * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12#include <linux/module.h>
13#include <linux/skbuff.h>
14#include <linux/init.h>
15#include <linux/ip.h>
16#include <linux/notifier.h>
17#include <linux/netdevice.h>
18#include <linux/netfilter.h>
19#include <linux/netfilter_ipv4/ip_queue.h>
20#include <linux/netfilter_ipv4/ip_tables.h>
21#include <linux/netlink.h>
22#include <linux/spinlock.h>
23#include <linux/sysctl.h>
24#include <linux/proc_fs.h>
Alexey Dobriyan7351a222007-11-05 20:33:46 -080025#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/security.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080027#include <linux/mutex.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020028#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <net/sock.h>
30#include <net/route.h>
Patrick McHardyc01cd422007-12-05 01:24:48 -080031#include <net/netfilter/nf_queue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#define IPQ_QMAX_DEFAULT 1024
34#define IPQ_PROC_FS_NAME "ip_queue"
35#define NET_IPQ_QMAX 2088
36#define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038struct ipq_queue_entry {
39 struct list_head list;
40 struct nf_info *info;
41 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042};
43
44typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
45
Brian Haley1192e402006-09-20 12:03:46 -070046static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE;
Brian Haley94aec082006-09-18 00:05:22 -070047static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static DEFINE_RWLOCK(queue_lock);
Brian Haley1192e402006-09-20 12:03:46 -070049static int peer_pid __read_mostly;
50static unsigned int copy_range __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static unsigned int queue_total;
52static unsigned int queue_dropped = 0;
53static unsigned int queue_user_dropped = 0;
Brian Haley1192e402006-09-20 12:03:46 -070054static struct sock *ipqnl __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static LIST_HEAD(queue_list);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080056static DEFINE_MUTEX(ipqnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58static void
59ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
60{
Harald Welte9bb7bc92005-05-30 15:35:26 -070061 /* TCP input path (and probably other bits) assume to be called
62 * from softirq context, not from syscall, like ipq_issue_verdict is
63 * called. TCP input path deadlocks with locks taken from timer
64 * softirq, e.g. We therefore emulate this by local_bh_disable() */
65
66 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 nf_reinject(entry->skb, entry->info, verdict);
Harald Welte9bb7bc92005-05-30 15:35:26 -070068 local_bh_enable();
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 kfree(entry);
71}
72
73static inline void
74__ipq_enqueue_entry(struct ipq_queue_entry *entry)
75{
Patrick McHardy0ac41e82007-12-05 01:25:03 -080076 list_add_tail(&entry->list, &queue_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 queue_total++;
78}
79
80/*
81 * Find and return a queued entry matched by cmpfn, or return the last
82 * entry if cmpfn is NULL.
83 */
84static inline struct ipq_queue_entry *
85__ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
86{
Patrick McHardy0ac41e82007-12-05 01:25:03 -080087 struct ipq_queue_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Patrick McHardy0ac41e82007-12-05 01:25:03 -080089 list_for_each_entry(entry, &queue_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (!cmpfn || cmpfn(entry, data))
91 return entry;
92 }
93 return NULL;
94}
95
96static inline void
97__ipq_dequeue_entry(struct ipq_queue_entry *entry)
98{
99 list_del(&entry->list);
100 queue_total--;
101}
102
103static inline struct ipq_queue_entry *
104__ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
105{
106 struct ipq_queue_entry *entry;
107
108 entry = __ipq_find_entry(cmpfn, data);
109 if (entry == NULL)
110 return NULL;
111
112 __ipq_dequeue_entry(entry);
113 return entry;
114}
115
116
117static inline void
118__ipq_flush(int verdict)
119{
120 struct ipq_queue_entry *entry;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
123 ipq_issue_verdict(entry, verdict);
124}
125
126static inline int
127__ipq_set_mode(unsigned char mode, unsigned int range)
128{
129 int status = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 switch(mode) {
132 case IPQ_COPY_NONE:
133 case IPQ_COPY_META:
134 copy_mode = mode;
135 copy_range = 0;
136 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 case IPQ_COPY_PACKET:
139 copy_mode = mode;
140 copy_range = range;
141 if (copy_range > 0xFFFF)
142 copy_range = 0xFFFF;
143 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 default:
146 status = -EINVAL;
147
148 }
149 return status;
150}
151
152static inline void
153__ipq_reset(void)
154{
155 peer_pid = 0;
156 net_disable_timestamp();
157 __ipq_set_mode(IPQ_COPY_NONE, 0);
158 __ipq_flush(NF_DROP);
159}
160
161static struct ipq_queue_entry *
162ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
163{
164 struct ipq_queue_entry *entry;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 write_lock_bh(&queue_lock);
167 entry = __ipq_find_dequeue_entry(cmpfn, data);
168 write_unlock_bh(&queue_lock);
169 return entry;
170}
171
172static void
173ipq_flush(int verdict)
174{
175 write_lock_bh(&queue_lock);
176 __ipq_flush(verdict);
177 write_unlock_bh(&queue_lock);
178}
179
180static struct sk_buff *
181ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
182{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700183 sk_buff_data_t old_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 size_t size = 0;
185 size_t data_len = 0;
186 struct sk_buff *skb;
187 struct ipq_packet_msg *pmsg;
188 struct nlmsghdr *nlh;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700189 struct timeval tv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 read_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 switch (copy_mode) {
194 case IPQ_COPY_META:
195 case IPQ_COPY_NONE:
196 size = NLMSG_SPACE(sizeof(*pmsg));
197 data_len = 0;
198 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 case IPQ_COPY_PACKET:
Patrick McHardy84fa7932006-08-29 16:44:56 -0700201 if ((entry->skb->ip_summed == CHECKSUM_PARTIAL ||
202 entry->skb->ip_summed == CHECKSUM_COMPLETE) &&
203 (*errp = skb_checksum_help(entry->skb))) {
Patrick McHardy66a79a12005-08-23 10:10:35 -0700204 read_unlock_bh(&queue_lock);
205 return NULL;
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (copy_range == 0 || copy_range > entry->skb->len)
208 data_len = entry->skb->len;
209 else
210 data_len = copy_range;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
213 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 default:
216 *errp = -EINVAL;
217 read_unlock_bh(&queue_lock);
218 return NULL;
219 }
220
221 read_unlock_bh(&queue_lock);
222
223 skb = alloc_skb(size, GFP_ATOMIC);
224 if (!skb)
225 goto nlmsg_failure;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900226
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700227 old_tail = skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
229 pmsg = NLMSG_DATA(nlh);
230 memset(pmsg, 0, sizeof(*pmsg));
231
232 pmsg->packet_id = (unsigned long )entry;
233 pmsg->data_len = data_len;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700234 tv = ktime_to_timeval(entry->skb->tstamp);
235 pmsg->timestamp_sec = tv.tv_sec;
236 pmsg->timestamp_usec = tv.tv_usec;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800237 pmsg->mark = entry->skb->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 pmsg->hook = entry->info->hook;
239 pmsg->hw_protocol = entry->skb->protocol;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (entry->info->indev)
242 strcpy(pmsg->indev_name, entry->info->indev->name);
243 else
244 pmsg->indev_name[0] = '\0';
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (entry->info->outdev)
247 strcpy(pmsg->outdev_name, entry->info->outdev->name);
248 else
249 pmsg->outdev_name[0] = '\0';
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (entry->info->indev && entry->skb->dev) {
252 pmsg->hw_type = entry->skb->dev->type;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700253 pmsg->hw_addrlen = dev_parse_header(entry->skb,
254 pmsg->hw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (data_len)
258 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
259 BUG();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 nlh->nlmsg_len = skb->tail - old_tail;
262 return skb;
263
264nlmsg_failure:
265 if (skb)
266 kfree_skb(skb);
267 *errp = -EINVAL;
268 printk(KERN_ERR "ip_queue: error creating packet message\n");
269 return NULL;
270}
271
272static int
Harald Welte0ab43f82005-08-09 19:43:44 -0700273ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
Patrick McHardyf9d89282007-12-05 01:24:30 -0800274 unsigned int queuenum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 int status = -EINVAL;
277 struct sk_buff *nskb;
278 struct ipq_queue_entry *entry;
279
280 if (copy_mode == IPQ_COPY_NONE)
281 return -EAGAIN;
282
283 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
284 if (entry == NULL) {
285 printk(KERN_ERR "ip_queue: OOM in ipq_enqueue_packet()\n");
286 return -ENOMEM;
287 }
288
289 entry->info = info;
290 entry->skb = skb;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 nskb = ipq_build_packet_message(entry, &status);
293 if (nskb == NULL)
294 goto err_out_free;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 write_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (!peer_pid)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900299 goto err_out_free_nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 if (queue_total >= queue_maxlen) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900302 queue_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 status = -ENOSPC;
304 if (net_ratelimit())
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900305 printk (KERN_WARNING "ip_queue: full at %d entries, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 "dropping packets(s). Dropped: %d\n", queue_total,
307 queue_dropped);
308 goto err_out_free_nskb;
309 }
310
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900311 /* netlink_unicast will either free the nskb or attach it to a socket */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
313 if (status < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900314 queue_user_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 goto err_out_unlock;
316 }
317
318 __ipq_enqueue_entry(entry);
319
320 write_unlock_bh(&queue_lock);
321 return status;
322
323err_out_free_nskb:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900324 kfree_skb(nskb);
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326err_out_unlock:
327 write_unlock_bh(&queue_lock);
328
329err_out_free:
330 kfree(entry);
331 return status;
332}
333
334static int
335ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
336{
337 int diff;
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700338 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct iphdr *user_iph = (struct iphdr *)v->payload;
340
341 if (v->data_len < sizeof(*user_iph))
342 return 0;
343 diff = v->data_len - e->skb->len;
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800344 if (diff < 0) {
345 if (pskb_trim(e->skb, v->data_len))
346 return -ENOMEM;
347 } else if (diff > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (v->data_len > 0xFFFF)
349 return -EINVAL;
350 if (diff > skb_tailroom(e->skb)) {
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700351 err = pskb_expand_head(e->skb, 0,
352 diff - skb_tailroom(e->skb),
353 GFP_ATOMIC);
354 if (err) {
355 printk(KERN_WARNING "ip_queue: error "
356 "in mangle, dropping packet: %d\n", -err);
357 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360 skb_put(e->skb, diff);
361 }
Herbert Xu37d41872007-10-14 00:39:18 -0700362 if (!skb_make_writable(e->skb, v->data_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300364 skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
Patrick McHardy66a79a12005-08-23 10:10:35 -0700365 e->skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
368}
369
370static inline int
371id_cmp(struct ipq_queue_entry *e, unsigned long id)
372{
373 return (id == (unsigned long )e);
374}
375
376static int
377ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
378{
379 struct ipq_queue_entry *entry;
380
381 if (vmsg->value > NF_MAX_VERDICT)
382 return -EINVAL;
383
384 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
385 if (entry == NULL)
386 return -ENOENT;
387 else {
388 int verdict = vmsg->value;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (vmsg->data_len && vmsg->data_len == len)
391 if (ipq_mangle_ipv4(vmsg, entry) < 0)
392 verdict = NF_DROP;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 ipq_issue_verdict(entry, verdict);
395 return 0;
396 }
397}
398
399static int
400ipq_set_mode(unsigned char mode, unsigned int range)
401{
402 int status;
403
404 write_lock_bh(&queue_lock);
405 status = __ipq_set_mode(mode, range);
406 write_unlock_bh(&queue_lock);
407 return status;
408}
409
410static int
411ipq_receive_peer(struct ipq_peer_msg *pmsg,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900412 unsigned char type, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 int status = 0;
415
416 if (len < sizeof(*pmsg))
417 return -EINVAL;
418
419 switch (type) {
420 case IPQM_MODE:
421 status = ipq_set_mode(pmsg->msg.mode.value,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900422 pmsg->msg.mode.range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 case IPQM_VERDICT:
426 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
427 status = -EINVAL;
428 else
429 status = ipq_set_verdict(&pmsg->msg.verdict,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900430 len - sizeof(*pmsg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 break;
432 default:
433 status = -EINVAL;
434 }
435 return status;
436}
437
438static int
439dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
440{
441 if (entry->info->indev)
442 if (entry->info->indev->ifindex == ifindex)
443 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (entry->info->outdev)
445 if (entry->info->outdev->ifindex == ifindex)
446 return 1;
Patrick McHardyef47c6a72006-06-27 03:01:48 -0700447#ifdef CONFIG_BRIDGE_NETFILTER
448 if (entry->skb->nf_bridge) {
449 if (entry->skb->nf_bridge->physindev &&
450 entry->skb->nf_bridge->physindev->ifindex == ifindex)
451 return 1;
452 if (entry->skb->nf_bridge->physoutdev &&
453 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900454 return 1;
Patrick McHardyef47c6a72006-06-27 03:01:48 -0700455 }
456#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
458}
459
460static void
461ipq_dev_drop(int ifindex)
462{
463 struct ipq_queue_entry *entry;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
466 ipq_issue_verdict(entry, NF_DROP);
467}
468
469#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
470
471static inline void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700472__ipq_rcv_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 int status, type, pid, flags, nlmsglen, skblen;
475 struct nlmsghdr *nlh;
476
477 skblen = skb->len;
478 if (skblen < sizeof(*nlh))
479 return;
480
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700481 nlh = nlmsg_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 nlmsglen = nlh->nlmsg_len;
483 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
484 return;
485
486 pid = nlh->nlmsg_pid;
487 flags = nlh->nlmsg_flags;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
490 RCV_SKB_FAIL(-EINVAL);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (flags & MSG_TRUNC)
493 RCV_SKB_FAIL(-ECOMM);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 type = nlh->nlmsg_type;
496 if (type < NLMSG_NOOP || type >= IPQM_MAX)
497 RCV_SKB_FAIL(-EINVAL);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (type <= IPQM_BASE)
500 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900501
Darrel Goeddelc7bdb542006-06-27 13:26:11 -0700502 if (security_netlink_recv(skb, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 RCV_SKB_FAIL(-EPERM);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 write_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (peer_pid) {
508 if (peer_pid != pid) {
509 write_unlock_bh(&queue_lock);
510 RCV_SKB_FAIL(-EBUSY);
511 }
512 } else {
513 net_enable_timestamp();
514 peer_pid = pid;
515 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 write_unlock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900520 nlmsglen - NLMSG_LENGTH(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (status < 0)
522 RCV_SKB_FAIL(status);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (flags & NLM_F_ACK)
525 netlink_ack(skb, nlh, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900526 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700530ipq_rcv_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800532 mutex_lock(&ipqnl_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700533 __ipq_rcv_skb(skb);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800534 mutex_unlock(&ipqnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537static int
538ipq_rcv_dev_event(struct notifier_block *this,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900539 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 struct net_device *dev = ptr;
542
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200543 if (dev->nd_net != &init_net)
544 return NOTIFY_DONE;
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* Drop any packets associated with the downed device */
547 if (event == NETDEV_DOWN)
548 ipq_dev_drop(dev->ifindex);
549 return NOTIFY_DONE;
550}
551
552static struct notifier_block ipq_dev_notifier = {
553 .notifier_call = ipq_rcv_dev_event,
554};
555
556static int
557ipq_rcv_nl_event(struct notifier_block *this,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900558 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 struct netlink_notify *n = ptr;
561
562 if (event == NETLINK_URELEASE &&
563 n->protocol == NETLINK_FIREWALL && n->pid) {
564 write_lock_bh(&queue_lock);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200565 if ((n->net == &init_net) && (n->pid == peer_pid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 __ipq_reset();
567 write_unlock_bh(&queue_lock);
568 }
569 return NOTIFY_DONE;
570}
571
572static struct notifier_block ipq_nl_notifier = {
573 .notifier_call = ipq_rcv_nl_event,
574};
575
576static struct ctl_table_header *ipq_sysctl_header;
577
578static ctl_table ipq_table[] = {
579 {
580 .ctl_name = NET_IPQ_QMAX,
581 .procname = NET_IPQ_QMAX_NAME,
582 .data = &queue_maxlen,
583 .maxlen = sizeof(queue_maxlen),
584 .mode = 0644,
585 .proc_handler = proc_dointvec
586 },
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900587 { .ctl_name = 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588};
589
590static ctl_table ipq_dir_table[] = {
591 {
592 .ctl_name = NET_IPV4,
593 .procname = "ipv4",
594 .mode = 0555,
595 .child = ipq_table
596 },
597 { .ctl_name = 0 }
598};
599
600static ctl_table ipq_root_table[] = {
601 {
602 .ctl_name = CTL_NET,
603 .procname = "net",
604 .mode = 0555,
605 .child = ipq_dir_table
606 },
607 { .ctl_name = 0 }
608};
609
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800610static int ip_queue_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 read_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900613
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800614 seq_printf(m,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900615 "Peer PID : %d\n"
616 "Copy mode : %hu\n"
617 "Copy range : %u\n"
618 "Queue length : %u\n"
619 "Queue max. length : %u\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 "Queue dropped : %u\n"
621 "Netlink dropped : %u\n",
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900622 peer_pid,
623 copy_mode,
624 copy_range,
625 queue_total,
626 queue_maxlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 queue_dropped,
628 queue_user_dropped);
629
630 read_unlock_bh(&queue_lock);
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800631 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800633
634static int ip_queue_open(struct inode *inode, struct file *file)
635{
636 return single_open(file, ip_queue_show, NULL);
637}
638
639static const struct file_operations ip_queue_proc_fops = {
640 .open = ip_queue_open,
641 .read = seq_read,
642 .llseek = seq_lseek,
643 .release = single_release,
644 .owner = THIS_MODULE,
645};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Patrick McHardye3ac5292007-12-05 01:23:57 -0800647static const struct nf_queue_handler nfqh = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700648 .name = "ip_queue",
649 .outfn = &ipq_enqueue_packet,
650};
651
Patrick McHardy32292a72006-04-06 14:11:30 -0700652static int __init ip_queue_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 int status = -ENOMEM;
655 struct proc_dir_entry *proc;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 netlink_register_notifier(&ipq_nl_notifier);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200658 ipqnl = netlink_kernel_create(&init_net, NETLINK_FIREWALL, 0,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700659 ipq_rcv_skb, NULL, THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (ipqnl == NULL) {
661 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
662 goto cleanup_netlink_notifier;
663 }
664
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800665 proc = create_proc_entry(IPQ_PROC_FS_NAME, 0, init_net.proc_net);
666 if (proc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 proc->owner = THIS_MODULE;
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800668 proc->proc_fops = &ip_queue_proc_fops;
669 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
671 goto cleanup_ipqnl;
672 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 register_netdevice_notifier(&ipq_dev_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800675 ipq_sysctl_header = register_sysctl_table(ipq_root_table);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900676
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700677 status = nf_register_queue_handler(PF_INET, &nfqh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (status < 0) {
679 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
680 goto cleanup_sysctl;
681 }
682 return status;
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684cleanup_sysctl:
685 unregister_sysctl_table(ipq_sysctl_header);
686 unregister_netdevice_notifier(&ipq_dev_notifier);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200687 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688cleanup_ipqnl:
689 sock_release(ipqnl->sk_socket);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800690 mutex_lock(&ipqnl_mutex);
691 mutex_unlock(&ipqnl_mutex);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693cleanup_netlink_notifier:
694 netlink_unregister_notifier(&ipq_nl_notifier);
695 return status;
696}
697
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800698static void __exit ip_queue_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Patrick McHardy32292a72006-04-06 14:11:30 -0700700 nf_unregister_queue_handlers(&nfqh);
701 synchronize_net();
702 ipq_flush(NF_DROP);
703
704 unregister_sysctl_table(ipq_sysctl_header);
705 unregister_netdevice_notifier(&ipq_dev_notifier);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200706 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
Patrick McHardy32292a72006-04-06 14:11:30 -0700707
708 sock_release(ipqnl->sk_socket);
709 mutex_lock(&ipqnl_mutex);
710 mutex_unlock(&ipqnl_mutex);
711
712 netlink_unregister_notifier(&ipq_nl_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715MODULE_DESCRIPTION("IPv4 packet queue handler");
716MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
717MODULE_LICENSE("GPL");
718
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800719module_init(ip_queue_init);
720module_exit(ip_queue_fini);