blob: aaa3f5c567611ab77168d0104ea0b61fd6671cba [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>
25#include <linux/security.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080026#include <linux/mutex.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020027#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <net/sock.h>
29#include <net/route.h>
30
31#define IPQ_QMAX_DEFAULT 1024
32#define IPQ_PROC_FS_NAME "ip_queue"
33#define NET_IPQ_QMAX 2088
34#define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036struct ipq_queue_entry {
37 struct list_head list;
38 struct nf_info *info;
39 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040};
41
42typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
43
Brian Haley1192e402006-09-20 12:03:46 -070044static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE;
Brian Haley94aec082006-09-18 00:05:22 -070045static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static DEFINE_RWLOCK(queue_lock);
Brian Haley1192e402006-09-20 12:03:46 -070047static int peer_pid __read_mostly;
48static unsigned int copy_range __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static unsigned int queue_total;
50static unsigned int queue_dropped = 0;
51static unsigned int queue_user_dropped = 0;
Brian Haley1192e402006-09-20 12:03:46 -070052static struct sock *ipqnl __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static LIST_HEAD(queue_list);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080054static DEFINE_MUTEX(ipqnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56static void
57ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
58{
Harald Welte9bb7bc92005-05-30 15:35:26 -070059 /* TCP input path (and probably other bits) assume to be called
60 * from softirq context, not from syscall, like ipq_issue_verdict is
61 * called. TCP input path deadlocks with locks taken from timer
62 * softirq, e.g. We therefore emulate this by local_bh_disable() */
63
64 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 nf_reinject(entry->skb, entry->info, verdict);
Harald Welte9bb7bc92005-05-30 15:35:26 -070066 local_bh_enable();
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 kfree(entry);
69}
70
71static inline void
72__ipq_enqueue_entry(struct ipq_queue_entry *entry)
73{
74 list_add(&entry->list, &queue_list);
75 queue_total++;
76}
77
78/*
79 * Find and return a queued entry matched by cmpfn, or return the last
80 * entry if cmpfn is NULL.
81 */
82static inline struct ipq_queue_entry *
83__ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
84{
85 struct list_head *p;
86
87 list_for_each_prev(p, &queue_list) {
88 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090089
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,
274 unsigned int queuenum, void *data)
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;
338 struct iphdr *user_iph = (struct iphdr *)v->payload;
339
340 if (v->data_len < sizeof(*user_iph))
341 return 0;
342 diff = v->data_len - e->skb->len;
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800343 if (diff < 0) {
344 if (pskb_trim(e->skb, v->data_len))
345 return -ENOMEM;
346 } else if (diff > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (v->data_len > 0xFFFF)
348 return -EINVAL;
349 if (diff > skb_tailroom(e->skb)) {
350 struct sk_buff *newskb;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 newskb = skb_copy_expand(e->skb,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900353 skb_headroom(e->skb),
354 diff,
355 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (newskb == NULL) {
357 printk(KERN_WARNING "ip_queue: OOM "
358 "in mangle, dropping packet\n");
359 return -ENOMEM;
360 }
361 if (e->skb->sk)
362 skb_set_owner_w(newskb, e->skb->sk);
363 kfree_skb(e->skb);
364 e->skb = newskb;
365 }
366 skb_put(e->skb, diff);
367 }
Harald Welte089af262005-08-09 19:37:23 -0700368 if (!skb_make_writable(&e->skb, v->data_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300370 skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
Patrick McHardy66a79a12005-08-23 10:10:35 -0700371 e->skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
374}
375
376static inline int
377id_cmp(struct ipq_queue_entry *e, unsigned long id)
378{
379 return (id == (unsigned long )e);
380}
381
382static int
383ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
384{
385 struct ipq_queue_entry *entry;
386
387 if (vmsg->value > NF_MAX_VERDICT)
388 return -EINVAL;
389
390 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
391 if (entry == NULL)
392 return -ENOENT;
393 else {
394 int verdict = vmsg->value;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (vmsg->data_len && vmsg->data_len == len)
397 if (ipq_mangle_ipv4(vmsg, entry) < 0)
398 verdict = NF_DROP;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 ipq_issue_verdict(entry, verdict);
401 return 0;
402 }
403}
404
405static int
406ipq_set_mode(unsigned char mode, unsigned int range)
407{
408 int status;
409
410 write_lock_bh(&queue_lock);
411 status = __ipq_set_mode(mode, range);
412 write_unlock_bh(&queue_lock);
413 return status;
414}
415
416static int
417ipq_receive_peer(struct ipq_peer_msg *pmsg,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900418 unsigned char type, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 int status = 0;
421
422 if (len < sizeof(*pmsg))
423 return -EINVAL;
424
425 switch (type) {
426 case IPQM_MODE:
427 status = ipq_set_mode(pmsg->msg.mode.value,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900428 pmsg->msg.mode.range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 case IPQM_VERDICT:
432 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
433 status = -EINVAL;
434 else
435 status = ipq_set_verdict(&pmsg->msg.verdict,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900436 len - sizeof(*pmsg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 break;
438 default:
439 status = -EINVAL;
440 }
441 return status;
442}
443
444static int
445dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
446{
447 if (entry->info->indev)
448 if (entry->info->indev->ifindex == ifindex)
449 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (entry->info->outdev)
451 if (entry->info->outdev->ifindex == ifindex)
452 return 1;
Patrick McHardyef47c6a72006-06-27 03:01:48 -0700453#ifdef CONFIG_BRIDGE_NETFILTER
454 if (entry->skb->nf_bridge) {
455 if (entry->skb->nf_bridge->physindev &&
456 entry->skb->nf_bridge->physindev->ifindex == ifindex)
457 return 1;
458 if (entry->skb->nf_bridge->physoutdev &&
459 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900460 return 1;
Patrick McHardyef47c6a72006-06-27 03:01:48 -0700461 }
462#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return 0;
464}
465
466static void
467ipq_dev_drop(int ifindex)
468{
469 struct ipq_queue_entry *entry;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
472 ipq_issue_verdict(entry, NF_DROP);
473}
474
475#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
476
477static inline void
478ipq_rcv_skb(struct sk_buff *skb)
479{
480 int status, type, pid, flags, nlmsglen, skblen;
481 struct nlmsghdr *nlh;
482
483 skblen = skb->len;
484 if (skblen < sizeof(*nlh))
485 return;
486
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700487 nlh = nlmsg_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 nlmsglen = nlh->nlmsg_len;
489 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
490 return;
491
492 pid = nlh->nlmsg_pid;
493 flags = nlh->nlmsg_flags;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
496 RCV_SKB_FAIL(-EINVAL);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (flags & MSG_TRUNC)
499 RCV_SKB_FAIL(-ECOMM);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 type = nlh->nlmsg_type;
502 if (type < NLMSG_NOOP || type >= IPQM_MAX)
503 RCV_SKB_FAIL(-EINVAL);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (type <= IPQM_BASE)
506 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900507
Darrel Goeddelc7bdb542006-06-27 13:26:11 -0700508 if (security_netlink_recv(skb, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 RCV_SKB_FAIL(-EPERM);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 write_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (peer_pid) {
514 if (peer_pid != pid) {
515 write_unlock_bh(&queue_lock);
516 RCV_SKB_FAIL(-EBUSY);
517 }
518 } else {
519 net_enable_timestamp();
520 peer_pid = pid;
521 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 write_unlock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900526 nlmsglen - NLMSG_LENGTH(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (status < 0)
528 RCV_SKB_FAIL(status);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (flags & NLM_F_ACK)
531 netlink_ack(skb, nlh, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900532 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535static void
536ipq_rcv_sk(struct sock *sk, int len)
537{
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700538 struct sk_buff *skb;
539 unsigned int qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800541 mutex_lock(&ipqnl_mutex);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900542
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700543 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
544 skb = skb_dequeue(&sk->sk_receive_queue);
545 ipq_rcv_skb(skb);
546 kfree_skb(skb);
547 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900548
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800549 mutex_unlock(&ipqnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552static int
553ipq_rcv_dev_event(struct notifier_block *this,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900554 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 struct net_device *dev = ptr;
557
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200558 if (dev->nd_net != &init_net)
559 return NOTIFY_DONE;
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* 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 Hideakie905a9e2007-02-09 23:24:47 +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_FIREWALL && n->pid) {
579 write_lock_bh(&queue_lock);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200580 if ((n->net == &init_net) && (n->pid == peer_pid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 __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 Hideakie905a9e2007-02-09 23:24:47 +0900602 { .ctl_name = 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603};
604
605static ctl_table ipq_dir_table[] = {
606 {
607 .ctl_name = NET_IPV4,
608 .procname = "ipv4",
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
625#ifdef CONFIG_PROC_FS
626static 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 Hideakie905a9e2007-02-09 23:24:47 +0900632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 len = sprintf(buffer,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +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 "Netlink dropped : %u\n",
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +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 Hideakie905a9e2007-02-09 23:24:47 +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}
659#endif /* CONFIG_PROC_FS */
660
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700661static struct nf_queue_handler nfqh = {
662 .name = "ip_queue",
663 .outfn = &ipq_enqueue_packet,
664};
665
Patrick McHardy32292a72006-04-06 14:11:30 -0700666static int __init ip_queue_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 int status = -ENOMEM;
669 struct proc_dir_entry *proc;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 netlink_register_notifier(&ipq_nl_notifier);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200672 ipqnl = netlink_kernel_create(&init_net, NETLINK_FIREWALL, 0,
673 ipq_rcv_sk, NULL, THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (ipqnl == NULL) {
675 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
676 goto cleanup_netlink_notifier;
677 }
678
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200679 proc = proc_net_create(&init_net, IPQ_PROC_FS_NAME, 0, ipq_get_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (proc)
681 proc->owner = THIS_MODULE;
682 else {
683 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
684 goto cleanup_ipqnl;
685 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +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 Hideakie905a9e2007-02-09 23:24:47 +0900689
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700690 status = nf_register_queue_handler(PF_INET, &nfqh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (status < 0) {
692 printk(KERN_ERR "ip_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);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200700 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701cleanup_ipqnl:
702 sock_release(ipqnl->sk_socket);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800703 mutex_lock(&ipqnl_mutex);
704 mutex_unlock(&ipqnl_mutex);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706cleanup_netlink_notifier:
707 netlink_unregister_notifier(&ipq_nl_notifier);
708 return status;
709}
710
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800711static void __exit ip_queue_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Patrick McHardy32292a72006-04-06 14:11:30 -0700713 nf_unregister_queue_handlers(&nfqh);
714 synchronize_net();
715 ipq_flush(NF_DROP);
716
717 unregister_sysctl_table(ipq_sysctl_header);
718 unregister_netdevice_notifier(&ipq_dev_notifier);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200719 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
Patrick McHardy32292a72006-04-06 14:11:30 -0700720
721 sock_release(ipqnl->sk_socket);
722 mutex_lock(&ipqnl_mutex);
723 mutex_unlock(&ipqnl_mutex);
724
725 netlink_unregister_notifier(&ipq_nl_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728MODULE_DESCRIPTION("IPv4 packet queue handler");
729MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
730MODULE_LICENSE("GPL");
731
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800732module_init(ip_queue_init);
733module_exit(ip_queue_fini);