blob: 17f7c988460c8bab02ab800302a477d7d1e5bbe2 [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.
11 *
12 * 2000-03-27: Simplified code (thanks to Andi Kleen for clues).
13 * 2000-05-20: Fixed notifier problems (following Miguel Freitas' report).
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090014 * 2000-06-19: Fixed so nfmark is copied to metadata (reported by Sebastian
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Zander).
16 * 2000-08-01: Added Nick Williams' MAC support.
17 * 2002-06-25: Code cleanup.
18 * 2005-01-10: Added /proc counter for dropped packets; fixed so
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090019 * packets aren't delivered to user space if they're going
20 * to be dropped.
Harald Welte9bb7bc92005-05-30 15:35:26 -070021 * 2005-05-26: local_bh_{disable,enable} around nf_reinject (Harald Welte)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *
23 */
24#include <linux/module.h>
25#include <linux/skbuff.h>
26#include <linux/init.h>
27#include <linux/ip.h>
28#include <linux/notifier.h>
29#include <linux/netdevice.h>
30#include <linux/netfilter.h>
31#include <linux/netfilter_ipv4/ip_queue.h>
32#include <linux/netfilter_ipv4/ip_tables.h>
33#include <linux/netlink.h>
34#include <linux/spinlock.h>
35#include <linux/sysctl.h>
36#include <linux/proc_fs.h>
37#include <linux/security.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080038#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <net/sock.h>
40#include <net/route.h>
41
42#define IPQ_QMAX_DEFAULT 1024
43#define IPQ_PROC_FS_NAME "ip_queue"
44#define NET_IPQ_QMAX 2088
45#define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047struct ipq_queue_entry {
48 struct list_head list;
49 struct nf_info *info;
50 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
53typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
54
Brian Haley1192e402006-09-20 12:03:46 -070055static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE;
Brian Haley94aec082006-09-18 00:05:22 -070056static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static DEFINE_RWLOCK(queue_lock);
Brian Haley1192e402006-09-20 12:03:46 -070058static int peer_pid __read_mostly;
59static unsigned int copy_range __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static unsigned int queue_total;
61static unsigned int queue_dropped = 0;
62static unsigned int queue_user_dropped = 0;
Brian Haley1192e402006-09-20 12:03:46 -070063static struct sock *ipqnl __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static LIST_HEAD(queue_list);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080065static DEFINE_MUTEX(ipqnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67static void
68ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
69{
Harald Welte9bb7bc92005-05-30 15:35:26 -070070 /* TCP input path (and probably other bits) assume to be called
71 * from softirq context, not from syscall, like ipq_issue_verdict is
72 * called. TCP input path deadlocks with locks taken from timer
73 * softirq, e.g. We therefore emulate this by local_bh_disable() */
74
75 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 nf_reinject(entry->skb, entry->info, verdict);
Harald Welte9bb7bc92005-05-30 15:35:26 -070077 local_bh_enable();
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 kfree(entry);
80}
81
82static inline void
83__ipq_enqueue_entry(struct ipq_queue_entry *entry)
84{
85 list_add(&entry->list, &queue_list);
86 queue_total++;
87}
88
89/*
90 * Find and return a queued entry matched by cmpfn, or return the last
91 * entry if cmpfn is NULL.
92 */
93static inline struct ipq_queue_entry *
94__ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
95{
96 struct list_head *p;
97
98 list_for_each_prev(p, &queue_list) {
99 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (!cmpfn || cmpfn(entry, data))
102 return entry;
103 }
104 return NULL;
105}
106
107static inline void
108__ipq_dequeue_entry(struct ipq_queue_entry *entry)
109{
110 list_del(&entry->list);
111 queue_total--;
112}
113
114static inline struct ipq_queue_entry *
115__ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
116{
117 struct ipq_queue_entry *entry;
118
119 entry = __ipq_find_entry(cmpfn, data);
120 if (entry == NULL)
121 return NULL;
122
123 __ipq_dequeue_entry(entry);
124 return entry;
125}
126
127
128static inline void
129__ipq_flush(int verdict)
130{
131 struct ipq_queue_entry *entry;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
134 ipq_issue_verdict(entry, verdict);
135}
136
137static inline int
138__ipq_set_mode(unsigned char mode, unsigned int range)
139{
140 int status = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 switch(mode) {
143 case IPQ_COPY_NONE:
144 case IPQ_COPY_META:
145 copy_mode = mode;
146 copy_range = 0;
147 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 case IPQ_COPY_PACKET:
150 copy_mode = mode;
151 copy_range = range;
152 if (copy_range > 0xFFFF)
153 copy_range = 0xFFFF;
154 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 default:
157 status = -EINVAL;
158
159 }
160 return status;
161}
162
163static inline void
164__ipq_reset(void)
165{
166 peer_pid = 0;
167 net_disable_timestamp();
168 __ipq_set_mode(IPQ_COPY_NONE, 0);
169 __ipq_flush(NF_DROP);
170}
171
172static struct ipq_queue_entry *
173ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
174{
175 struct ipq_queue_entry *entry;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 write_lock_bh(&queue_lock);
178 entry = __ipq_find_dequeue_entry(cmpfn, data);
179 write_unlock_bh(&queue_lock);
180 return entry;
181}
182
183static void
184ipq_flush(int verdict)
185{
186 write_lock_bh(&queue_lock);
187 __ipq_flush(verdict);
188 write_unlock_bh(&queue_lock);
189}
190
191static struct sk_buff *
192ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
193{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700194 sk_buff_data_t old_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 size_t size = 0;
196 size_t data_len = 0;
197 struct sk_buff *skb;
198 struct ipq_packet_msg *pmsg;
199 struct nlmsghdr *nlh;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700200 struct timeval tv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 read_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 switch (copy_mode) {
205 case IPQ_COPY_META:
206 case IPQ_COPY_NONE:
207 size = NLMSG_SPACE(sizeof(*pmsg));
208 data_len = 0;
209 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 case IPQ_COPY_PACKET:
Patrick McHardy84fa7932006-08-29 16:44:56 -0700212 if ((entry->skb->ip_summed == CHECKSUM_PARTIAL ||
213 entry->skb->ip_summed == CHECKSUM_COMPLETE) &&
214 (*errp = skb_checksum_help(entry->skb))) {
Patrick McHardy66a79a12005-08-23 10:10:35 -0700215 read_unlock_bh(&queue_lock);
216 return NULL;
217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (copy_range == 0 || copy_range > entry->skb->len)
219 data_len = entry->skb->len;
220 else
221 data_len = copy_range;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
224 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 default:
227 *errp = -EINVAL;
228 read_unlock_bh(&queue_lock);
229 return NULL;
230 }
231
232 read_unlock_bh(&queue_lock);
233
234 skb = alloc_skb(size, GFP_ATOMIC);
235 if (!skb)
236 goto nlmsg_failure;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900237
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700238 old_tail = skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
240 pmsg = NLMSG_DATA(nlh);
241 memset(pmsg, 0, sizeof(*pmsg));
242
243 pmsg->packet_id = (unsigned long )entry;
244 pmsg->data_len = data_len;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700245 tv = ktime_to_timeval(entry->skb->tstamp);
246 pmsg->timestamp_sec = tv.tv_sec;
247 pmsg->timestamp_usec = tv.tv_usec;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800248 pmsg->mark = entry->skb->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 pmsg->hook = entry->info->hook;
250 pmsg->hw_protocol = entry->skb->protocol;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (entry->info->indev)
253 strcpy(pmsg->indev_name, entry->info->indev->name);
254 else
255 pmsg->indev_name[0] = '\0';
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (entry->info->outdev)
258 strcpy(pmsg->outdev_name, entry->info->outdev->name);
259 else
260 pmsg->outdev_name[0] = '\0';
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (entry->info->indev && entry->skb->dev) {
263 pmsg->hw_type = entry->skb->dev->type;
264 if (entry->skb->dev->hard_header_parse)
265 pmsg->hw_addrlen =
266 entry->skb->dev->hard_header_parse(entry->skb,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900267 pmsg->hw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (data_len)
271 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
272 BUG();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 nlh->nlmsg_len = skb->tail - old_tail;
275 return skb;
276
277nlmsg_failure:
278 if (skb)
279 kfree_skb(skb);
280 *errp = -EINVAL;
281 printk(KERN_ERR "ip_queue: error creating packet message\n");
282 return NULL;
283}
284
285static int
Harald Welte0ab43f82005-08-09 19:43:44 -0700286ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
287 unsigned int queuenum, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 int status = -EINVAL;
290 struct sk_buff *nskb;
291 struct ipq_queue_entry *entry;
292
293 if (copy_mode == IPQ_COPY_NONE)
294 return -EAGAIN;
295
296 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
297 if (entry == NULL) {
298 printk(KERN_ERR "ip_queue: OOM in ipq_enqueue_packet()\n");
299 return -ENOMEM;
300 }
301
302 entry->info = info;
303 entry->skb = skb;
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 nskb = ipq_build_packet_message(entry, &status);
306 if (nskb == NULL)
307 goto err_out_free;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 write_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (!peer_pid)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900312 goto err_out_free_nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 if (queue_total >= queue_maxlen) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900315 queue_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 status = -ENOSPC;
317 if (net_ratelimit())
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900318 printk (KERN_WARNING "ip_queue: full at %d entries, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 "dropping packets(s). Dropped: %d\n", queue_total,
320 queue_dropped);
321 goto err_out_free_nskb;
322 }
323
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900324 /* netlink_unicast will either free the nskb or attach it to a socket */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
326 if (status < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900327 queue_user_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 goto err_out_unlock;
329 }
330
331 __ipq_enqueue_entry(entry);
332
333 write_unlock_bh(&queue_lock);
334 return status;
335
336err_out_free_nskb:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900337 kfree_skb(nskb);
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339err_out_unlock:
340 write_unlock_bh(&queue_lock);
341
342err_out_free:
343 kfree(entry);
344 return status;
345}
346
347static int
348ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
349{
350 int diff;
351 struct iphdr *user_iph = (struct iphdr *)v->payload;
352
353 if (v->data_len < sizeof(*user_iph))
354 return 0;
355 diff = v->data_len - e->skb->len;
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800356 if (diff < 0) {
357 if (pskb_trim(e->skb, v->data_len))
358 return -ENOMEM;
359 } else if (diff > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (v->data_len > 0xFFFF)
361 return -EINVAL;
362 if (diff > skb_tailroom(e->skb)) {
363 struct sk_buff *newskb;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 newskb = skb_copy_expand(e->skb,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900366 skb_headroom(e->skb),
367 diff,
368 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (newskb == NULL) {
370 printk(KERN_WARNING "ip_queue: OOM "
371 "in mangle, dropping packet\n");
372 return -ENOMEM;
373 }
374 if (e->skb->sk)
375 skb_set_owner_w(newskb, e->skb->sk);
376 kfree_skb(e->skb);
377 e->skb = newskb;
378 }
379 skb_put(e->skb, diff);
380 }
Harald Welte089af262005-08-09 19:37:23 -0700381 if (!skb_make_writable(&e->skb, v->data_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return -ENOMEM;
383 memcpy(e->skb->data, v->payload, v->data_len);
Patrick McHardy66a79a12005-08-23 10:10:35 -0700384 e->skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return 0;
387}
388
389static inline int
390id_cmp(struct ipq_queue_entry *e, unsigned long id)
391{
392 return (id == (unsigned long )e);
393}
394
395static int
396ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
397{
398 struct ipq_queue_entry *entry;
399
400 if (vmsg->value > NF_MAX_VERDICT)
401 return -EINVAL;
402
403 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
404 if (entry == NULL)
405 return -ENOENT;
406 else {
407 int verdict = vmsg->value;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (vmsg->data_len && vmsg->data_len == len)
410 if (ipq_mangle_ipv4(vmsg, entry) < 0)
411 verdict = NF_DROP;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 ipq_issue_verdict(entry, verdict);
414 return 0;
415 }
416}
417
418static int
419ipq_set_mode(unsigned char mode, unsigned int range)
420{
421 int status;
422
423 write_lock_bh(&queue_lock);
424 status = __ipq_set_mode(mode, range);
425 write_unlock_bh(&queue_lock);
426 return status;
427}
428
429static int
430ipq_receive_peer(struct ipq_peer_msg *pmsg,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900431 unsigned char type, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 int status = 0;
434
435 if (len < sizeof(*pmsg))
436 return -EINVAL;
437
438 switch (type) {
439 case IPQM_MODE:
440 status = ipq_set_mode(pmsg->msg.mode.value,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900441 pmsg->msg.mode.range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 case IPQM_VERDICT:
445 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
446 status = -EINVAL;
447 else
448 status = ipq_set_verdict(&pmsg->msg.verdict,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900449 len - sizeof(*pmsg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 break;
451 default:
452 status = -EINVAL;
453 }
454 return status;
455}
456
457static int
458dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
459{
460 if (entry->info->indev)
461 if (entry->info->indev->ifindex == ifindex)
462 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (entry->info->outdev)
464 if (entry->info->outdev->ifindex == ifindex)
465 return 1;
Patrick McHardyef47c6a72006-06-27 03:01:48 -0700466#ifdef CONFIG_BRIDGE_NETFILTER
467 if (entry->skb->nf_bridge) {
468 if (entry->skb->nf_bridge->physindev &&
469 entry->skb->nf_bridge->physindev->ifindex == ifindex)
470 return 1;
471 if (entry->skb->nf_bridge->physoutdev &&
472 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900473 return 1;
Patrick McHardyef47c6a72006-06-27 03:01:48 -0700474 }
475#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return 0;
477}
478
479static void
480ipq_dev_drop(int ifindex)
481{
482 struct ipq_queue_entry *entry;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
485 ipq_issue_verdict(entry, NF_DROP);
486}
487
488#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
489
490static inline void
491ipq_rcv_skb(struct sk_buff *skb)
492{
493 int status, type, pid, flags, nlmsglen, skblen;
494 struct nlmsghdr *nlh;
495
496 skblen = skb->len;
497 if (skblen < sizeof(*nlh))
498 return;
499
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700500 nlh = nlmsg_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 nlmsglen = nlh->nlmsg_len;
502 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
503 return;
504
505 pid = nlh->nlmsg_pid;
506 flags = nlh->nlmsg_flags;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
509 RCV_SKB_FAIL(-EINVAL);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (flags & MSG_TRUNC)
512 RCV_SKB_FAIL(-ECOMM);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 type = nlh->nlmsg_type;
515 if (type < NLMSG_NOOP || type >= IPQM_MAX)
516 RCV_SKB_FAIL(-EINVAL);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (type <= IPQM_BASE)
519 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900520
Darrel Goeddelc7bdb542006-06-27 13:26:11 -0700521 if (security_netlink_recv(skb, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 RCV_SKB_FAIL(-EPERM);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 write_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (peer_pid) {
527 if (peer_pid != pid) {
528 write_unlock_bh(&queue_lock);
529 RCV_SKB_FAIL(-EBUSY);
530 }
531 } else {
532 net_enable_timestamp();
533 peer_pid = pid;
534 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 write_unlock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900539 nlmsglen - NLMSG_LENGTH(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (status < 0)
541 RCV_SKB_FAIL(status);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (flags & NLM_F_ACK)
544 netlink_ack(skb, nlh, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900545 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
548static void
549ipq_rcv_sk(struct sock *sk, int len)
550{
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700551 struct sk_buff *skb;
552 unsigned int qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800554 mutex_lock(&ipqnl_mutex);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900555
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700556 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
557 skb = skb_dequeue(&sk->sk_receive_queue);
558 ipq_rcv_skb(skb);
559 kfree_skb(skb);
560 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900561
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800562 mutex_unlock(&ipqnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565static int
566ipq_rcv_dev_event(struct notifier_block *this,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900567 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 struct net_device *dev = ptr;
570
571 /* Drop any packets associated with the downed device */
572 if (event == NETDEV_DOWN)
573 ipq_dev_drop(dev->ifindex);
574 return NOTIFY_DONE;
575}
576
577static struct notifier_block ipq_dev_notifier = {
578 .notifier_call = ipq_rcv_dev_event,
579};
580
581static int
582ipq_rcv_nl_event(struct notifier_block *this,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900583 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 struct netlink_notify *n = ptr;
586
587 if (event == NETLINK_URELEASE &&
588 n->protocol == NETLINK_FIREWALL && n->pid) {
589 write_lock_bh(&queue_lock);
590 if (n->pid == peer_pid)
591 __ipq_reset();
592 write_unlock_bh(&queue_lock);
593 }
594 return NOTIFY_DONE;
595}
596
597static struct notifier_block ipq_nl_notifier = {
598 .notifier_call = ipq_rcv_nl_event,
599};
600
601static struct ctl_table_header *ipq_sysctl_header;
602
603static ctl_table ipq_table[] = {
604 {
605 .ctl_name = NET_IPQ_QMAX,
606 .procname = NET_IPQ_QMAX_NAME,
607 .data = &queue_maxlen,
608 .maxlen = sizeof(queue_maxlen),
609 .mode = 0644,
610 .proc_handler = proc_dointvec
611 },
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900612 { .ctl_name = 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613};
614
615static ctl_table ipq_dir_table[] = {
616 {
617 .ctl_name = NET_IPV4,
618 .procname = "ipv4",
619 .mode = 0555,
620 .child = ipq_table
621 },
622 { .ctl_name = 0 }
623};
624
625static ctl_table ipq_root_table[] = {
626 {
627 .ctl_name = CTL_NET,
628 .procname = "net",
629 .mode = 0555,
630 .child = ipq_dir_table
631 },
632 { .ctl_name = 0 }
633};
634
635#ifdef CONFIG_PROC_FS
636static int
637ipq_get_info(char *buffer, char **start, off_t offset, int length)
638{
639 int len;
640
641 read_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 len = sprintf(buffer,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900644 "Peer PID : %d\n"
645 "Copy mode : %hu\n"
646 "Copy range : %u\n"
647 "Queue length : %u\n"
648 "Queue max. length : %u\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 "Queue dropped : %u\n"
650 "Netlink dropped : %u\n",
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900651 peer_pid,
652 copy_mode,
653 copy_range,
654 queue_total,
655 queue_maxlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 queue_dropped,
657 queue_user_dropped);
658
659 read_unlock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 *start = buffer + offset;
662 len -= offset;
663 if (len > length)
664 len = length;
665 else if (len < 0)
666 len = 0;
667 return len;
668}
669#endif /* CONFIG_PROC_FS */
670
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700671static struct nf_queue_handler nfqh = {
672 .name = "ip_queue",
673 .outfn = &ipq_enqueue_packet,
674};
675
Patrick McHardy32292a72006-04-06 14:11:30 -0700676static int __init ip_queue_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 int status = -ENOMEM;
679 struct proc_dir_entry *proc;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 netlink_register_notifier(&ipq_nl_notifier);
Patrick McHardy06628602005-08-15 12:33:26 -0700682 ipqnl = netlink_kernel_create(NETLINK_FIREWALL, 0, ipq_rcv_sk,
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700683 THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (ipqnl == NULL) {
685 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
686 goto cleanup_netlink_notifier;
687 }
688
689 proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
690 if (proc)
691 proc->owner = THIS_MODULE;
692 else {
693 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
694 goto cleanup_ipqnl;
695 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 register_netdevice_notifier(&ipq_dev_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800698 ipq_sysctl_header = register_sysctl_table(ipq_root_table);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900699
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700700 status = nf_register_queue_handler(PF_INET, &nfqh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (status < 0) {
702 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
703 goto cleanup_sysctl;
704 }
705 return status;
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707cleanup_sysctl:
708 unregister_sysctl_table(ipq_sysctl_header);
709 unregister_netdevice_notifier(&ipq_dev_notifier);
710 proc_net_remove(IPQ_PROC_FS_NAME);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712cleanup_ipqnl:
713 sock_release(ipqnl->sk_socket);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800714 mutex_lock(&ipqnl_mutex);
715 mutex_unlock(&ipqnl_mutex);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717cleanup_netlink_notifier:
718 netlink_unregister_notifier(&ipq_nl_notifier);
719 return status;
720}
721
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800722static void __exit ip_queue_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Patrick McHardy32292a72006-04-06 14:11:30 -0700724 nf_unregister_queue_handlers(&nfqh);
725 synchronize_net();
726 ipq_flush(NF_DROP);
727
728 unregister_sysctl_table(ipq_sysctl_header);
729 unregister_netdevice_notifier(&ipq_dev_notifier);
730 proc_net_remove(IPQ_PROC_FS_NAME);
731
732 sock_release(ipqnl->sk_socket);
733 mutex_lock(&ipqnl_mutex);
734 mutex_unlock(&ipqnl_mutex);
735
736 netlink_unregister_notifier(&ipq_nl_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739MODULE_DESCRIPTION("IPv4 packet queue handler");
740MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
741MODULE_LICENSE("GPL");
742
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800743module_init(ip_queue_init);
744module_exit(ip_queue_fini);