blob: 66a2c41352515fb64eaadf8e7761e20030fac08e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This is a module which is used for queueing IPv6 packets and
3 * communicating with userspace via netlink.
4 *
5 * (C) 2001 Fernando Anton, this code is GPL.
6 * IPv64 Project - Work based in IPv64 draft by Arturo Azcorra.
7 * Universidad Carlos III de Madrid - Leganes (Madrid) - Spain
8 * Universidad Politecnica de Alcala de Henares - Alcala de H. (Madrid) - Spain
9 * email: fanton@it.uc3m.es
10 *
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
13 * published by the Free Software Foundation.
14 *
15 * 2001-11-06: First try. Working with ip_queue.c for IPv4 and trying
16 * to adapt it to IPv6
17 * HEAVILY based in ipqueue.c by James Morris. It's just
18 * a little modified version of it, so he's nearly the
19 * real coder of this.
20 * Few changes needed, mainly the hard_routing code and
21 * the netlink socket protocol (we're NETLINK_IP6_FW).
22 * 2002-06-25: Code cleanup. [JM: ported cleanup over from ip_queue.c]
23 * 2005-02-04: Added /proc counter for dropped packets; fixed so
24 * packets aren't delivered to user space if they're going
25 * to be dropped.
26 */
27#include <linux/module.h>
28#include <linux/skbuff.h>
29#include <linux/init.h>
30#include <linux/ipv6.h>
31#include <linux/notifier.h>
32#include <linux/netdevice.h>
33#include <linux/netfilter.h>
34#include <linux/netlink.h>
35#include <linux/spinlock.h>
36#include <linux/sysctl.h>
37#include <linux/proc_fs.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/ipv6.h>
41#include <net/ip6_route.h>
42#include <linux/netfilter_ipv4/ip_queue.h>
43#include <linux/netfilter_ipv4/ip_tables.h>
44#include <linux/netfilter_ipv6/ip6_tables.h>
45
46#define IPQ_QMAX_DEFAULT 1024
47#define IPQ_PROC_FS_NAME "ip6_queue"
48#define NET_IPQ_QMAX 2088
49#define NET_IPQ_QMAX_NAME "ip6_queue_maxlen"
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051struct ipq_queue_entry {
52 struct list_head list;
53 struct nf_info *info;
54 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56
57typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
58
Brian Haley1192e402006-09-20 12:03:46 -070059static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE;
Brian Haley94aec082006-09-18 00:05:22 -070060static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static DEFINE_RWLOCK(queue_lock);
Brian Haley1192e402006-09-20 12:03:46 -070062static int peer_pid __read_mostly;
63static unsigned int copy_range __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static unsigned int queue_total;
65static unsigned int queue_dropped = 0;
66static unsigned int queue_user_dropped = 0;
Brian Haley1192e402006-09-20 12:03:46 -070067static struct sock *ipqnl __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static LIST_HEAD(queue_list);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080069static DEFINE_MUTEX(ipqnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71static void
72ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
73{
Patrick McHardy4c1217d2005-07-22 12:49:30 -070074 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 nf_reinject(entry->skb, entry->info, verdict);
Patrick McHardy4c1217d2005-07-22 12:49:30 -070076 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 kfree(entry);
78}
79
80static inline void
81__ipq_enqueue_entry(struct ipq_queue_entry *entry)
82{
83 list_add(&entry->list, &queue_list);
84 queue_total++;
85}
86
87/*
88 * Find and return a queued entry matched by cmpfn, or return the last
89 * entry if cmpfn is NULL.
90 */
91static inline struct ipq_queue_entry *
92__ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
93{
94 struct list_head *p;
95
96 list_for_each_prev(p, &queue_list) {
97 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (!cmpfn || cmpfn(entry, data))
100 return entry;
101 }
102 return NULL;
103}
104
105static inline void
106__ipq_dequeue_entry(struct ipq_queue_entry *entry)
107{
108 list_del(&entry->list);
109 queue_total--;
110}
111
112static inline struct ipq_queue_entry *
113__ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
114{
115 struct ipq_queue_entry *entry;
116
117 entry = __ipq_find_entry(cmpfn, data);
118 if (entry == NULL)
119 return NULL;
120
121 __ipq_dequeue_entry(entry);
122 return entry;
123}
124
125
126static inline void
127__ipq_flush(int verdict)
128{
129 struct ipq_queue_entry *entry;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
132 ipq_issue_verdict(entry, verdict);
133}
134
135static inline int
136__ipq_set_mode(unsigned char mode, unsigned int range)
137{
138 int status = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 switch(mode) {
141 case IPQ_COPY_NONE:
142 case IPQ_COPY_META:
143 copy_mode = mode;
144 copy_range = 0;
145 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 case IPQ_COPY_PACKET:
148 copy_mode = mode;
149 copy_range = range;
150 if (copy_range > 0xFFFF)
151 copy_range = 0xFFFF;
152 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 default:
155 status = -EINVAL;
156
157 }
158 return status;
159}
160
161static inline void
162__ipq_reset(void)
163{
164 peer_pid = 0;
165 net_disable_timestamp();
166 __ipq_set_mode(IPQ_COPY_NONE, 0);
167 __ipq_flush(NF_DROP);
168}
169
170static struct ipq_queue_entry *
171ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
172{
173 struct ipq_queue_entry *entry;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 write_lock_bh(&queue_lock);
176 entry = __ipq_find_dequeue_entry(cmpfn, data);
177 write_unlock_bh(&queue_lock);
178 return entry;
179}
180
181static void
182ipq_flush(int verdict)
183{
184 write_lock_bh(&queue_lock);
185 __ipq_flush(verdict);
186 write_unlock_bh(&queue_lock);
187}
188
189static struct sk_buff *
190ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
191{
192 unsigned char *old_tail;
193 size_t size = 0;
194 size_t data_len = 0;
195 struct sk_buff *skb;
196 struct ipq_packet_msg *pmsg;
197 struct nlmsghdr *nlh;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700198 struct timeval tv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 read_lock_bh(&queue_lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 switch (copy_mode) {
203 case IPQ_COPY_META:
204 case IPQ_COPY_NONE:
205 size = NLMSG_SPACE(sizeof(*pmsg));
206 data_len = 0;
207 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 case IPQ_COPY_PACKET:
Patrick McHardy84fa7932006-08-29 16:44:56 -0700210 if ((entry->skb->ip_summed == CHECKSUM_PARTIAL ||
211 entry->skb->ip_summed == CHECKSUM_COMPLETE) &&
212 (*errp = skb_checksum_help(entry->skb))) {
Patrick McHardy66a79a12005-08-23 10:10:35 -0700213 read_unlock_bh(&queue_lock);
214 return NULL;
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (copy_range == 0 || copy_range > entry->skb->len)
217 data_len = entry->skb->len;
218 else
219 data_len = copy_range;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
222 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 default:
225 *errp = -EINVAL;
226 read_unlock_bh(&queue_lock);
227 return NULL;
228 }
229
230 read_unlock_bh(&queue_lock);
231
232 skb = alloc_skb(size, GFP_ATOMIC);
233 if (!skb)
234 goto nlmsg_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 old_tail= skb->tail;
237 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
238 pmsg = NLMSG_DATA(nlh);
239 memset(pmsg, 0, sizeof(*pmsg));
240
241 pmsg->packet_id = (unsigned long )entry;
242 pmsg->data_len = data_len;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700243 tv = ktime_to_timeval(entry->skb->tstamp);
244 pmsg->timestamp_sec = tv.tv_sec;
245 pmsg->timestamp_usec = tv.tv_usec;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800246 pmsg->mark = entry->skb->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 pmsg->hook = entry->info->hook;
248 pmsg->hw_protocol = entry->skb->protocol;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (entry->info->indev)
251 strcpy(pmsg->indev_name, entry->info->indev->name);
252 else
253 pmsg->indev_name[0] = '\0';
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (entry->info->outdev)
256 strcpy(pmsg->outdev_name, entry->info->outdev->name);
257 else
258 pmsg->outdev_name[0] = '\0';
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (entry->info->indev && entry->skb->dev) {
261 pmsg->hw_type = entry->skb->dev->type;
262 if (entry->skb->dev->hard_header_parse)
263 pmsg->hw_addrlen =
264 entry->skb->dev->hard_header_parse(entry->skb,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900265 pmsg->hw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (data_len)
269 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
270 BUG();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 nlh->nlmsg_len = skb->tail - old_tail;
273 return skb;
274
275nlmsg_failure:
276 if (skb)
277 kfree_skb(skb);
278 *errp = -EINVAL;
279 printk(KERN_ERR "ip6_queue: error creating packet message\n");
280 return NULL;
281}
282
283static int
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900284ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
Harald Welte0ab43f82005-08-09 19:43:44 -0700285 unsigned int queuenum, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 int status = -EINVAL;
288 struct sk_buff *nskb;
289 struct ipq_queue_entry *entry;
290
291 if (copy_mode == IPQ_COPY_NONE)
292 return -EAGAIN;
293
294 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
295 if (entry == NULL) {
296 printk(KERN_ERR "ip6_queue: OOM in ipq_enqueue_packet()\n");
297 return -ENOMEM;
298 }
299
300 entry->info = info;
301 entry->skb = skb;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 nskb = ipq_build_packet_message(entry, &status);
304 if (nskb == NULL)
305 goto err_out_free;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 write_lock_bh(&queue_lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (!peer_pid)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900310 goto err_out_free_nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 if (queue_total >= queue_maxlen) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900313 queue_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 status = -ENOSPC;
315 if (net_ratelimit())
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900316 printk (KERN_WARNING "ip6_queue: fill at %d entries, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 "dropping packet(s). Dropped: %d\n", queue_total,
318 queue_dropped);
319 goto err_out_free_nskb;
320 }
321
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900322 /* netlink_unicast will either free the nskb or attach it to a socket */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
324 if (status < 0) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900325 queue_user_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 goto err_out_unlock;
327 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 __ipq_enqueue_entry(entry);
330
331 write_unlock_bh(&queue_lock);
332 return status;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334err_out_free_nskb:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900335 kfree_skb(nskb);
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337err_out_unlock:
338 write_unlock_bh(&queue_lock);
339
340err_out_free:
341 kfree(entry);
342 return status;
343}
344
345static int
346ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
347{
348 int diff;
349 struct ipv6hdr *user_iph = (struct ipv6hdr *)v->payload;
350
351 if (v->data_len < sizeof(*user_iph))
352 return 0;
353 diff = v->data_len - e->skb->len;
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800354 if (diff < 0) {
355 if (pskb_trim(e->skb, v->data_len))
356 return -ENOMEM;
357 } else if (diff > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (v->data_len > 0xFFFF)
359 return -EINVAL;
360 if (diff > skb_tailroom(e->skb)) {
361 struct sk_buff *newskb;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 newskb = skb_copy_expand(e->skb,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900364 skb_headroom(e->skb),
365 diff,
366 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (newskb == NULL) {
368 printk(KERN_WARNING "ip6_queue: OOM "
369 "in mangle, dropping packet\n");
370 return -ENOMEM;
371 }
372 if (e->skb->sk)
373 skb_set_owner_w(newskb, e->skb->sk);
374 kfree_skb(e->skb);
375 e->skb = newskb;
376 }
377 skb_put(e->skb, diff);
378 }
Harald Welte089af262005-08-09 19:37:23 -0700379 if (!skb_make_writable(&e->skb, v->data_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return -ENOMEM;
381 memcpy(e->skb->data, v->payload, v->data_len);
Patrick McHardy66a79a12005-08-23 10:10:35 -0700382 e->skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return 0;
385}
386
387static inline int
388id_cmp(struct ipq_queue_entry *e, unsigned long id)
389{
390 return (id == (unsigned long )e);
391}
392
393static int
394ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
395{
396 struct ipq_queue_entry *entry;
397
398 if (vmsg->value > NF_MAX_VERDICT)
399 return -EINVAL;
400
401 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
402 if (entry == NULL)
403 return -ENOENT;
404 else {
405 int verdict = vmsg->value;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (vmsg->data_len && vmsg->data_len == len)
408 if (ipq_mangle_ipv6(vmsg, entry) < 0)
409 verdict = NF_DROP;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 ipq_issue_verdict(entry, verdict);
412 return 0;
413 }
414}
415
416static int
417ipq_set_mode(unsigned char mode, unsigned int range)
418{
419 int status;
420
421 write_lock_bh(&queue_lock);
422 status = __ipq_set_mode(mode, range);
423 write_unlock_bh(&queue_lock);
424 return status;
425}
426
427static int
428ipq_receive_peer(struct ipq_peer_msg *pmsg,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900429 unsigned char type, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 int status = 0;
432
433 if (len < sizeof(*pmsg))
434 return -EINVAL;
435
436 switch (type) {
437 case IPQM_MODE:
438 status = ipq_set_mode(pmsg->msg.mode.value,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900439 pmsg->msg.mode.range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 case IPQM_VERDICT:
443 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
444 status = -EINVAL;
445 else
446 status = ipq_set_verdict(&pmsg->msg.verdict,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900447 len - sizeof(*pmsg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 break;
449 default:
450 status = -EINVAL;
451 }
452 return status;
453}
454
455static int
456dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
457{
458 if (entry->info->indev)
459 if (entry->info->indev->ifindex == ifindex)
460 return 1;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (entry->info->outdev)
463 if (entry->info->outdev->ifindex == ifindex)
464 return 1;
465
466 return 0;
467}
468
469static void
470ipq_dev_drop(int ifindex)
471{
472 struct ipq_queue_entry *entry;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
475 ipq_issue_verdict(entry, NF_DROP);
476}
477
478#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
479
480static inline void
481ipq_rcv_skb(struct sk_buff *skb)
482{
483 int status, type, pid, flags, nlmsglen, skblen;
484 struct nlmsghdr *nlh;
485
486 skblen = skb->len;
487 if (skblen < sizeof(*nlh))
488 return;
489
490 nlh = (struct nlmsghdr *)skb->data;
491 nlmsglen = nlh->nlmsg_len;
492 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
493 return;
494
495 pid = nlh->nlmsg_pid;
496 flags = nlh->nlmsg_flags;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
499 RCV_SKB_FAIL(-EINVAL);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (flags & MSG_TRUNC)
502 RCV_SKB_FAIL(-ECOMM);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 type = nlh->nlmsg_type;
505 if (type < NLMSG_NOOP || type >= IPQM_MAX)
506 RCV_SKB_FAIL(-EINVAL);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (type <= IPQM_BASE)
509 return;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900510
Darrel Goeddelc7bdb542006-06-27 13:26:11 -0700511 if (security_netlink_recv(skb, CAP_NET_ADMIN))
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900512 RCV_SKB_FAIL(-EPERM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 write_lock_bh(&queue_lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (peer_pid) {
517 if (peer_pid != pid) {
518 write_unlock_bh(&queue_lock);
519 RCV_SKB_FAIL(-EBUSY);
520 }
521 } else {
522 net_enable_timestamp();
523 peer_pid = pid;
524 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 write_unlock_bh(&queue_lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900529 nlmsglen - NLMSG_LENGTH(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (status < 0)
531 RCV_SKB_FAIL(status);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (flags & NLM_F_ACK)
534 netlink_ack(skb, nlh, 0);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900535 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538static void
539ipq_rcv_sk(struct sock *sk, int len)
540{
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700541 struct sk_buff *skb;
542 unsigned int qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800544 mutex_lock(&ipqnl_mutex);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900545
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700546 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
547 skb = skb_dequeue(&sk->sk_receive_queue);
548 ipq_rcv_skb(skb);
549 kfree_skb(skb);
550 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900551
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800552 mutex_unlock(&ipqnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555static int
556ipq_rcv_dev_event(struct notifier_block *this,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900557 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 struct net_device *dev = ptr;
560
561 /* Drop any packets associated with the downed device */
562 if (event == NETDEV_DOWN)
563 ipq_dev_drop(dev->ifindex);
564 return NOTIFY_DONE;
565}
566
567static struct notifier_block ipq_dev_notifier = {
568 .notifier_call = ipq_rcv_dev_event,
569};
570
571static int
572ipq_rcv_nl_event(struct notifier_block *this,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900573 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 struct netlink_notify *n = ptr;
576
577 if (event == NETLINK_URELEASE &&
578 n->protocol == NETLINK_IP6_FW && n->pid) {
579 write_lock_bh(&queue_lock);
580 if (n->pid == peer_pid)
581 __ipq_reset();
582 write_unlock_bh(&queue_lock);
583 }
584 return NOTIFY_DONE;
585}
586
587static struct notifier_block ipq_nl_notifier = {
588 .notifier_call = ipq_rcv_nl_event,
589};
590
591static struct ctl_table_header *ipq_sysctl_header;
592
593static ctl_table ipq_table[] = {
594 {
595 .ctl_name = NET_IPQ_QMAX,
596 .procname = NET_IPQ_QMAX_NAME,
597 .data = &queue_maxlen,
598 .maxlen = sizeof(queue_maxlen),
599 .mode = 0644,
600 .proc_handler = proc_dointvec
601 },
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900602 { .ctl_name = 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603};
604
605static ctl_table ipq_dir_table[] = {
606 {
607 .ctl_name = NET_IPV6,
608 .procname = "ipv6",
609 .mode = 0555,
610 .child = ipq_table
611 },
612 { .ctl_name = 0 }
613};
614
615static ctl_table ipq_root_table[] = {
616 {
617 .ctl_name = CTL_NET,
618 .procname = "net",
619 .mode = 0555,
620 .child = ipq_dir_table
621 },
622 { .ctl_name = 0 }
623};
624
Patrick McHardy76592582006-11-29 02:35:42 +0100625#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626static int
627ipq_get_info(char *buffer, char **start, off_t offset, int length)
628{
629 int len;
630
631 read_lock_bh(&queue_lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 len = sprintf(buffer,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900634 "Peer PID : %d\n"
635 "Copy mode : %hu\n"
636 "Copy range : %u\n"
637 "Queue length : %u\n"
638 "Queue max. length : %u\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 "Queue dropped : %u\n"
640 "Netfilter dropped : %u\n",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900641 peer_pid,
642 copy_mode,
643 copy_range,
644 queue_total,
645 queue_maxlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 queue_dropped,
647 queue_user_dropped);
648
649 read_unlock_bh(&queue_lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 *start = buffer + offset;
652 len -= offset;
653 if (len > length)
654 len = length;
655 else if (len < 0)
656 len = 0;
657 return len;
658}
Patrick McHardy76592582006-11-29 02:35:42 +0100659#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700661static struct nf_queue_handler nfqh = {
662 .name = "ip6_queue",
663 .outfn = &ipq_enqueue_packet,
664};
665
Patrick McHardy32292a72006-04-06 14:11:30 -0700666static int __init ip6_queue_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 int status = -ENOMEM;
669 struct proc_dir_entry *proc;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 netlink_register_notifier(&ipq_nl_notifier);
Patrick McHardy06628602005-08-15 12:33:26 -0700672 ipqnl = netlink_kernel_create(NETLINK_IP6_FW, 0, ipq_rcv_sk,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900673 THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (ipqnl == NULL) {
675 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
676 goto cleanup_netlink_notifier;
677 }
678
679 proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
680 if (proc)
681 proc->owner = THIS_MODULE;
682 else {
683 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
684 goto cleanup_ipqnl;
685 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 register_netdevice_notifier(&ipq_dev_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800688 ipq_sysctl_header = register_sysctl_table(ipq_root_table);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900689
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700690 status = nf_register_queue_handler(PF_INET6, &nfqh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (status < 0) {
692 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
693 goto cleanup_sysctl;
694 }
695 return status;
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697cleanup_sysctl:
698 unregister_sysctl_table(ipq_sysctl_header);
699 unregister_netdevice_notifier(&ipq_dev_notifier);
700 proc_net_remove(IPQ_PROC_FS_NAME);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702cleanup_ipqnl:
703 sock_release(ipqnl->sk_socket);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800704 mutex_lock(&ipqnl_mutex);
705 mutex_unlock(&ipqnl_mutex);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707cleanup_netlink_notifier:
708 netlink_unregister_notifier(&ipq_nl_notifier);
709 return status;
710}
711
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800712static void __exit ip6_queue_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Patrick McHardy32292a72006-04-06 14:11:30 -0700714 nf_unregister_queue_handlers(&nfqh);
715 synchronize_net();
716 ipq_flush(NF_DROP);
717
718 unregister_sysctl_table(ipq_sysctl_header);
719 unregister_netdevice_notifier(&ipq_dev_notifier);
720 proc_net_remove(IPQ_PROC_FS_NAME);
721
722 sock_release(ipqnl->sk_socket);
723 mutex_lock(&ipqnl_mutex);
724 mutex_unlock(&ipqnl_mutex);
725
726 netlink_unregister_notifier(&ipq_nl_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729MODULE_DESCRIPTION("IPv6 packet queue handler");
730MODULE_LICENSE("GPL");
731
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800732module_init(ip6_queue_init);
733module_exit(ip6_queue_fini);