blob: 14d64a383db1a7223ed321e5dedfbc1942af1095 [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>
31
32#define IPQ_QMAX_DEFAULT 1024
33#define IPQ_PROC_FS_NAME "ip_queue"
34#define NET_IPQ_QMAX 2088
35#define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037struct ipq_queue_entry {
38 struct list_head list;
39 struct nf_info *info;
40 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041};
42
43typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
44
Brian Haley1192e402006-09-20 12:03:46 -070045static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE;
Brian Haley94aec082006-09-18 00:05:22 -070046static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static DEFINE_RWLOCK(queue_lock);
Brian Haley1192e402006-09-20 12:03:46 -070048static int peer_pid __read_mostly;
49static unsigned int copy_range __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static unsigned int queue_total;
51static unsigned int queue_dropped = 0;
52static unsigned int queue_user_dropped = 0;
Brian Haley1192e402006-09-20 12:03:46 -070053static struct sock *ipqnl __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static LIST_HEAD(queue_list);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080055static DEFINE_MUTEX(ipqnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static void
58ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
59{
Harald Welte9bb7bc92005-05-30 15:35:26 -070060 /* TCP input path (and probably other bits) assume to be called
61 * from softirq context, not from syscall, like ipq_issue_verdict is
62 * called. TCP input path deadlocks with locks taken from timer
63 * softirq, e.g. We therefore emulate this by local_bh_disable() */
64
65 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 nf_reinject(entry->skb, entry->info, verdict);
Harald Welte9bb7bc92005-05-30 15:35:26 -070067 local_bh_enable();
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 kfree(entry);
70}
71
72static inline void
73__ipq_enqueue_entry(struct ipq_queue_entry *entry)
74{
75 list_add(&entry->list, &queue_list);
76 queue_total++;
77}
78
79/*
80 * Find and return a queued entry matched by cmpfn, or return the last
81 * entry if cmpfn is NULL.
82 */
83static inline struct ipq_queue_entry *
84__ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
85{
86 struct list_head *p;
87
88 list_for_each_prev(p, &queue_list) {
89 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (!cmpfn || cmpfn(entry, data))
92 return entry;
93 }
94 return NULL;
95}
96
97static inline void
98__ipq_dequeue_entry(struct ipq_queue_entry *entry)
99{
100 list_del(&entry->list);
101 queue_total--;
102}
103
104static inline struct ipq_queue_entry *
105__ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
106{
107 struct ipq_queue_entry *entry;
108
109 entry = __ipq_find_entry(cmpfn, data);
110 if (entry == NULL)
111 return NULL;
112
113 __ipq_dequeue_entry(entry);
114 return entry;
115}
116
117
118static inline void
119__ipq_flush(int verdict)
120{
121 struct ipq_queue_entry *entry;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
124 ipq_issue_verdict(entry, verdict);
125}
126
127static inline int
128__ipq_set_mode(unsigned char mode, unsigned int range)
129{
130 int status = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 switch(mode) {
133 case IPQ_COPY_NONE:
134 case IPQ_COPY_META:
135 copy_mode = mode;
136 copy_range = 0;
137 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 case IPQ_COPY_PACKET:
140 copy_mode = mode;
141 copy_range = range;
142 if (copy_range > 0xFFFF)
143 copy_range = 0xFFFF;
144 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 default:
147 status = -EINVAL;
148
149 }
150 return status;
151}
152
153static inline void
154__ipq_reset(void)
155{
156 peer_pid = 0;
157 net_disable_timestamp();
158 __ipq_set_mode(IPQ_COPY_NONE, 0);
159 __ipq_flush(NF_DROP);
160}
161
162static struct ipq_queue_entry *
163ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
164{
165 struct ipq_queue_entry *entry;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 write_lock_bh(&queue_lock);
168 entry = __ipq_find_dequeue_entry(cmpfn, data);
169 write_unlock_bh(&queue_lock);
170 return entry;
171}
172
173static void
174ipq_flush(int verdict)
175{
176 write_lock_bh(&queue_lock);
177 __ipq_flush(verdict);
178 write_unlock_bh(&queue_lock);
179}
180
181static struct sk_buff *
182ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
183{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700184 sk_buff_data_t old_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 size_t size = 0;
186 size_t data_len = 0;
187 struct sk_buff *skb;
188 struct ipq_packet_msg *pmsg;
189 struct nlmsghdr *nlh;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700190 struct timeval tv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 read_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 switch (copy_mode) {
195 case IPQ_COPY_META:
196 case IPQ_COPY_NONE:
197 size = NLMSG_SPACE(sizeof(*pmsg));
198 data_len = 0;
199 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 case IPQ_COPY_PACKET:
Patrick McHardy84fa7932006-08-29 16:44:56 -0700202 if ((entry->skb->ip_summed == CHECKSUM_PARTIAL ||
203 entry->skb->ip_summed == CHECKSUM_COMPLETE) &&
204 (*errp = skb_checksum_help(entry->skb))) {
Patrick McHardy66a79a12005-08-23 10:10:35 -0700205 read_unlock_bh(&queue_lock);
206 return NULL;
207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (copy_range == 0 || copy_range > entry->skb->len)
209 data_len = entry->skb->len;
210 else
211 data_len = copy_range;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
214 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 default:
217 *errp = -EINVAL;
218 read_unlock_bh(&queue_lock);
219 return NULL;
220 }
221
222 read_unlock_bh(&queue_lock);
223
224 skb = alloc_skb(size, GFP_ATOMIC);
225 if (!skb)
226 goto nlmsg_failure;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900227
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700228 old_tail = skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
230 pmsg = NLMSG_DATA(nlh);
231 memset(pmsg, 0, sizeof(*pmsg));
232
233 pmsg->packet_id = (unsigned long )entry;
234 pmsg->data_len = data_len;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700235 tv = ktime_to_timeval(entry->skb->tstamp);
236 pmsg->timestamp_sec = tv.tv_sec;
237 pmsg->timestamp_usec = tv.tv_usec;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800238 pmsg->mark = entry->skb->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 pmsg->hook = entry->info->hook;
240 pmsg->hw_protocol = entry->skb->protocol;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (entry->info->indev)
243 strcpy(pmsg->indev_name, entry->info->indev->name);
244 else
245 pmsg->indev_name[0] = '\0';
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (entry->info->outdev)
248 strcpy(pmsg->outdev_name, entry->info->outdev->name);
249 else
250 pmsg->outdev_name[0] = '\0';
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (entry->info->indev && entry->skb->dev) {
253 pmsg->hw_type = entry->skb->dev->type;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700254 pmsg->hw_addrlen = dev_parse_header(entry->skb,
255 pmsg->hw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (data_len)
259 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
260 BUG();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 nlh->nlmsg_len = skb->tail - old_tail;
263 return skb;
264
265nlmsg_failure:
266 if (skb)
267 kfree_skb(skb);
268 *errp = -EINVAL;
269 printk(KERN_ERR "ip_queue: error creating packet message\n");
270 return NULL;
271}
272
273static int
Harald Welte0ab43f82005-08-09 19:43:44 -0700274ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
275 unsigned int queuenum, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 int status = -EINVAL;
278 struct sk_buff *nskb;
279 struct ipq_queue_entry *entry;
280
281 if (copy_mode == IPQ_COPY_NONE)
282 return -EAGAIN;
283
284 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
285 if (entry == NULL) {
286 printk(KERN_ERR "ip_queue: OOM in ipq_enqueue_packet()\n");
287 return -ENOMEM;
288 }
289
290 entry->info = info;
291 entry->skb = skb;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 nskb = ipq_build_packet_message(entry, &status);
294 if (nskb == NULL)
295 goto err_out_free;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 write_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (!peer_pid)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900300 goto err_out_free_nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (queue_total >= queue_maxlen) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900303 queue_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 status = -ENOSPC;
305 if (net_ratelimit())
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900306 printk (KERN_WARNING "ip_queue: full at %d entries, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 "dropping packets(s). Dropped: %d\n", queue_total,
308 queue_dropped);
309 goto err_out_free_nskb;
310 }
311
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900312 /* netlink_unicast will either free the nskb or attach it to a socket */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
314 if (status < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900315 queue_user_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 goto err_out_unlock;
317 }
318
319 __ipq_enqueue_entry(entry);
320
321 write_unlock_bh(&queue_lock);
322 return status;
323
324err_out_free_nskb:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900325 kfree_skb(nskb);
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327err_out_unlock:
328 write_unlock_bh(&queue_lock);
329
330err_out_free:
331 kfree(entry);
332 return status;
333}
334
335static int
336ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
337{
338 int diff;
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700339 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct iphdr *user_iph = (struct iphdr *)v->payload;
341
342 if (v->data_len < sizeof(*user_iph))
343 return 0;
344 diff = v->data_len - e->skb->len;
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800345 if (diff < 0) {
346 if (pskb_trim(e->skb, v->data_len))
347 return -ENOMEM;
348 } else if (diff > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (v->data_len > 0xFFFF)
350 return -EINVAL;
351 if (diff > skb_tailroom(e->skb)) {
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700352 err = pskb_expand_head(e->skb, 0,
353 diff - skb_tailroom(e->skb),
354 GFP_ATOMIC);
355 if (err) {
356 printk(KERN_WARNING "ip_queue: error "
357 "in mangle, dropping packet: %d\n", -err);
358 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361 skb_put(e->skb, diff);
362 }
Herbert Xu37d41872007-10-14 00:39:18 -0700363 if (!skb_make_writable(e->skb, v->data_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300365 skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
Patrick McHardy66a79a12005-08-23 10:10:35 -0700366 e->skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
369}
370
371static inline int
372id_cmp(struct ipq_queue_entry *e, unsigned long id)
373{
374 return (id == (unsigned long )e);
375}
376
377static int
378ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
379{
380 struct ipq_queue_entry *entry;
381
382 if (vmsg->value > NF_MAX_VERDICT)
383 return -EINVAL;
384
385 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
386 if (entry == NULL)
387 return -ENOENT;
388 else {
389 int verdict = vmsg->value;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (vmsg->data_len && vmsg->data_len == len)
392 if (ipq_mangle_ipv4(vmsg, entry) < 0)
393 verdict = NF_DROP;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 ipq_issue_verdict(entry, verdict);
396 return 0;
397 }
398}
399
400static int
401ipq_set_mode(unsigned char mode, unsigned int range)
402{
403 int status;
404
405 write_lock_bh(&queue_lock);
406 status = __ipq_set_mode(mode, range);
407 write_unlock_bh(&queue_lock);
408 return status;
409}
410
411static int
412ipq_receive_peer(struct ipq_peer_msg *pmsg,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900413 unsigned char type, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 int status = 0;
416
417 if (len < sizeof(*pmsg))
418 return -EINVAL;
419
420 switch (type) {
421 case IPQM_MODE:
422 status = ipq_set_mode(pmsg->msg.mode.value,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900423 pmsg->msg.mode.range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 case IPQM_VERDICT:
427 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
428 status = -EINVAL;
429 else
430 status = ipq_set_verdict(&pmsg->msg.verdict,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900431 len - sizeof(*pmsg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 break;
433 default:
434 status = -EINVAL;
435 }
436 return status;
437}
438
439static int
440dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
441{
442 if (entry->info->indev)
443 if (entry->info->indev->ifindex == ifindex)
444 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (entry->info->outdev)
446 if (entry->info->outdev->ifindex == ifindex)
447 return 1;
Patrick McHardyef47c6a2006-06-27 03:01:48 -0700448#ifdef CONFIG_BRIDGE_NETFILTER
449 if (entry->skb->nf_bridge) {
450 if (entry->skb->nf_bridge->physindev &&
451 entry->skb->nf_bridge->physindev->ifindex == ifindex)
452 return 1;
453 if (entry->skb->nf_bridge->physoutdev &&
454 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900455 return 1;
Patrick McHardyef47c6a2006-06-27 03:01:48 -0700456 }
457#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return 0;
459}
460
461static void
462ipq_dev_drop(int ifindex)
463{
464 struct ipq_queue_entry *entry;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
467 ipq_issue_verdict(entry, NF_DROP);
468}
469
470#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
471
472static inline void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700473__ipq_rcv_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 int status, type, pid, flags, nlmsglen, skblen;
476 struct nlmsghdr *nlh;
477
478 skblen = skb->len;
479 if (skblen < sizeof(*nlh))
480 return;
481
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700482 nlh = nlmsg_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 nlmsglen = nlh->nlmsg_len;
484 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
485 return;
486
487 pid = nlh->nlmsg_pid;
488 flags = nlh->nlmsg_flags;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
491 RCV_SKB_FAIL(-EINVAL);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (flags & MSG_TRUNC)
494 RCV_SKB_FAIL(-ECOMM);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 type = nlh->nlmsg_type;
497 if (type < NLMSG_NOOP || type >= IPQM_MAX)
498 RCV_SKB_FAIL(-EINVAL);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (type <= IPQM_BASE)
501 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900502
Darrel Goeddelc7bdb542006-06-27 13:26:11 -0700503 if (security_netlink_recv(skb, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 RCV_SKB_FAIL(-EPERM);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 write_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (peer_pid) {
509 if (peer_pid != pid) {
510 write_unlock_bh(&queue_lock);
511 RCV_SKB_FAIL(-EBUSY);
512 }
513 } else {
514 net_enable_timestamp();
515 peer_pid = pid;
516 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 write_unlock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900521 nlmsglen - NLMSG_LENGTH(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (status < 0)
523 RCV_SKB_FAIL(status);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (flags & NLM_F_ACK)
526 netlink_ack(skb, nlh, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900527 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700531ipq_rcv_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800533 mutex_lock(&ipqnl_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700534 __ipq_rcv_skb(skb);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800535 mutex_unlock(&ipqnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538static int
539ipq_rcv_dev_event(struct notifier_block *this,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900540 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct net_device *dev = ptr;
543
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200544 if (dev->nd_net != &init_net)
545 return NOTIFY_DONE;
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* Drop any packets associated with the downed device */
548 if (event == NETDEV_DOWN)
549 ipq_dev_drop(dev->ifindex);
550 return NOTIFY_DONE;
551}
552
553static struct notifier_block ipq_dev_notifier = {
554 .notifier_call = ipq_rcv_dev_event,
555};
556
557static int
558ipq_rcv_nl_event(struct notifier_block *this,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900559 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 struct netlink_notify *n = ptr;
562
563 if (event == NETLINK_URELEASE &&
564 n->protocol == NETLINK_FIREWALL && n->pid) {
565 write_lock_bh(&queue_lock);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200566 if ((n->net == &init_net) && (n->pid == peer_pid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 __ipq_reset();
568 write_unlock_bh(&queue_lock);
569 }
570 return NOTIFY_DONE;
571}
572
573static struct notifier_block ipq_nl_notifier = {
574 .notifier_call = ipq_rcv_nl_event,
575};
576
577static struct ctl_table_header *ipq_sysctl_header;
578
579static ctl_table ipq_table[] = {
580 {
581 .ctl_name = NET_IPQ_QMAX,
582 .procname = NET_IPQ_QMAX_NAME,
583 .data = &queue_maxlen,
584 .maxlen = sizeof(queue_maxlen),
585 .mode = 0644,
586 .proc_handler = proc_dointvec
587 },
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900588 { .ctl_name = 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589};
590
591static ctl_table ipq_dir_table[] = {
592 {
593 .ctl_name = NET_IPV4,
594 .procname = "ipv4",
595 .mode = 0555,
596 .child = ipq_table
597 },
598 { .ctl_name = 0 }
599};
600
601static ctl_table ipq_root_table[] = {
602 {
603 .ctl_name = CTL_NET,
604 .procname = "net",
605 .mode = 0555,
606 .child = ipq_dir_table
607 },
608 { .ctl_name = 0 }
609};
610
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800611static int ip_queue_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 read_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900614
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800615 seq_printf(m,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900616 "Peer PID : %d\n"
617 "Copy mode : %hu\n"
618 "Copy range : %u\n"
619 "Queue length : %u\n"
620 "Queue max. length : %u\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 "Queue dropped : %u\n"
622 "Netlink dropped : %u\n",
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900623 peer_pid,
624 copy_mode,
625 copy_range,
626 queue_total,
627 queue_maxlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 queue_dropped,
629 queue_user_dropped);
630
631 read_unlock_bh(&queue_lock);
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800632 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800634
635static int ip_queue_open(struct inode *inode, struct file *file)
636{
637 return single_open(file, ip_queue_show, NULL);
638}
639
640static const struct file_operations ip_queue_proc_fops = {
641 .open = ip_queue_open,
642 .read = seq_read,
643 .llseek = seq_lseek,
644 .release = single_release,
645 .owner = THIS_MODULE,
646};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700648static struct nf_queue_handler nfqh = {
649 .name = "ip_queue",
650 .outfn = &ipq_enqueue_packet,
651};
652
Patrick McHardy32292a72006-04-06 14:11:30 -0700653static int __init ip_queue_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 int status = -ENOMEM;
656 struct proc_dir_entry *proc;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 netlink_register_notifier(&ipq_nl_notifier);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200659 ipqnl = netlink_kernel_create(&init_net, NETLINK_FIREWALL, 0,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700660 ipq_rcv_skb, NULL, THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (ipqnl == NULL) {
662 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
663 goto cleanup_netlink_notifier;
664 }
665
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800666 proc = create_proc_entry(IPQ_PROC_FS_NAME, 0, init_net.proc_net);
667 if (proc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 proc->owner = THIS_MODULE;
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800669 proc->proc_fops = &ip_queue_proc_fops;
670 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
672 goto cleanup_ipqnl;
673 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 register_netdevice_notifier(&ipq_dev_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800676 ipq_sysctl_header = register_sysctl_table(ipq_root_table);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900677
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700678 status = nf_register_queue_handler(PF_INET, &nfqh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (status < 0) {
680 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
681 goto cleanup_sysctl;
682 }
683 return status;
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685cleanup_sysctl:
686 unregister_sysctl_table(ipq_sysctl_header);
687 unregister_netdevice_notifier(&ipq_dev_notifier);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200688 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689cleanup_ipqnl:
690 sock_release(ipqnl->sk_socket);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800691 mutex_lock(&ipqnl_mutex);
692 mutex_unlock(&ipqnl_mutex);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694cleanup_netlink_notifier:
695 netlink_unregister_notifier(&ipq_nl_notifier);
696 return status;
697}
698
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800699static void __exit ip_queue_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Patrick McHardy32292a72006-04-06 14:11:30 -0700701 nf_unregister_queue_handlers(&nfqh);
702 synchronize_net();
703 ipq_flush(NF_DROP);
704
705 unregister_sysctl_table(ipq_sysctl_header);
706 unregister_netdevice_notifier(&ipq_dev_notifier);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200707 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
Patrick McHardy32292a72006-04-06 14:11:30 -0700708
709 sock_release(ipqnl->sk_socket);
710 mutex_lock(&ipqnl_mutex);
711 mutex_unlock(&ipqnl_mutex);
712
713 netlink_unregister_notifier(&ipq_nl_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716MODULE_DESCRIPTION("IPv4 packet queue handler");
717MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
718MODULE_LICENSE("GPL");
719
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800720module_init(ip_queue_init);
721module_exit(ip_queue_fini);