blob: df2957c5bcb43da0b268aeaeaedb9479310c2257 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This is a module which is used for queueing IPv4 packets and
3 * communicating with userspace via netlink.
4 *
5 * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
Harald Welte9bb7bc92005-05-30 15:35:26 -07006 * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12#include <linux/module.h>
13#include <linux/skbuff.h>
14#include <linux/init.h>
15#include <linux/ip.h>
16#include <linux/notifier.h>
17#include <linux/netdevice.h>
18#include <linux/netfilter.h>
19#include <linux/netfilter_ipv4/ip_queue.h>
20#include <linux/netfilter_ipv4/ip_tables.h>
21#include <linux/netlink.h>
22#include <linux/spinlock.h>
23#include <linux/sysctl.h>
24#include <linux/proc_fs.h>
Alexey Dobriyan7351a222007-11-05 20:33:46 -080025#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/security.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080027#include <linux/mutex.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020028#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <net/sock.h>
30#include <net/route.h>
Patrick McHardyc01cd422007-12-05 01:24:48 -080031#include <net/netfilter/nf_queue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#define IPQ_QMAX_DEFAULT 1024
34#define IPQ_PROC_FS_NAME "ip_queue"
35#define NET_IPQ_QMAX 2088
36#define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038struct ipq_queue_entry {
39 struct list_head list;
40 struct nf_info *info;
41 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042};
43
44typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
45
Brian Haley1192e402006-09-20 12:03:46 -070046static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE;
Brian Haley94aec082006-09-18 00:05:22 -070047static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static DEFINE_RWLOCK(queue_lock);
Brian Haley1192e402006-09-20 12:03:46 -070049static int peer_pid __read_mostly;
50static unsigned int copy_range __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static unsigned int queue_total;
52static unsigned int queue_dropped = 0;
53static unsigned int queue_user_dropped = 0;
Brian Haley1192e402006-09-20 12:03:46 -070054static struct sock *ipqnl __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static LIST_HEAD(queue_list);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080056static DEFINE_MUTEX(ipqnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58static void
59ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
60{
Harald Welte9bb7bc92005-05-30 15:35:26 -070061 /* TCP input path (and probably other bits) assume to be called
62 * from softirq context, not from syscall, like ipq_issue_verdict is
63 * called. TCP input path deadlocks with locks taken from timer
64 * softirq, e.g. We therefore emulate this by local_bh_disable() */
65
66 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 nf_reinject(entry->skb, entry->info, verdict);
Harald Welte9bb7bc92005-05-30 15:35:26 -070068 local_bh_enable();
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 kfree(entry);
71}
72
73static inline void
74__ipq_enqueue_entry(struct ipq_queue_entry *entry)
75{
Patrick McHardy0ac41e82007-12-05 01:25:03 -080076 list_add_tail(&entry->list, &queue_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 queue_total++;
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static inline int
81__ipq_set_mode(unsigned char mode, unsigned int range)
82{
83 int status = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 switch(mode) {
86 case IPQ_COPY_NONE:
87 case IPQ_COPY_META:
88 copy_mode = mode;
89 copy_range = 0;
90 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 case IPQ_COPY_PACKET:
93 copy_mode = mode;
94 copy_range = range;
95 if (copy_range > 0xFFFF)
96 copy_range = 0xFFFF;
97 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 default:
100 status = -EINVAL;
101
102 }
103 return status;
104}
105
Patrick McHardy95214092007-12-05 01:25:46 -0800106static void __ipq_flush(ipq_cmpfn cmpfn, unsigned long data);
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static inline void
109__ipq_reset(void)
110{
111 peer_pid = 0;
112 net_disable_timestamp();
113 __ipq_set_mode(IPQ_COPY_NONE, 0);
Patrick McHardy95214092007-12-05 01:25:46 -0800114 __ipq_flush(NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117static struct ipq_queue_entry *
Patrick McHardy95214092007-12-05 01:25:46 -0800118ipq_find_dequeue_entry(unsigned long id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Patrick McHardy95214092007-12-05 01:25:46 -0800120 struct ipq_queue_entry *entry = NULL, *i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 write_lock_bh(&queue_lock);
Patrick McHardy95214092007-12-05 01:25:46 -0800123
124 list_for_each_entry(i, &queue_list, list) {
125 if ((unsigned long)i == id) {
126 entry = i;
127 break;
128 }
129 }
130
131 if (entry) {
132 list_del(&entry->list);
133 queue_total--;
134 }
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 write_unlock_bh(&queue_lock);
137 return entry;
138}
139
140static void
Patrick McHardy95214092007-12-05 01:25:46 -0800141__ipq_flush(ipq_cmpfn cmpfn, unsigned long data)
142{
143 struct ipq_queue_entry *entry, *next;
144
145 list_for_each_entry_safe(entry, next, &queue_list, list) {
146 if (!cmpfn || cmpfn(entry, data)) {
147 list_del(&entry->list);
148 queue_total--;
149 ipq_issue_verdict(entry, NF_DROP);
150 }
151 }
152}
153
154static void
155ipq_flush(ipq_cmpfn cmpfn, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 write_lock_bh(&queue_lock);
Patrick McHardy95214092007-12-05 01:25:46 -0800158 __ipq_flush(cmpfn, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 write_unlock_bh(&queue_lock);
160}
161
162static struct sk_buff *
163ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
164{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700165 sk_buff_data_t old_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 size_t size = 0;
167 size_t data_len = 0;
168 struct sk_buff *skb;
169 struct ipq_packet_msg *pmsg;
170 struct nlmsghdr *nlh;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700171 struct timeval tv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 read_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 switch (copy_mode) {
176 case IPQ_COPY_META:
177 case IPQ_COPY_NONE:
178 size = NLMSG_SPACE(sizeof(*pmsg));
179 data_len = 0;
180 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 case IPQ_COPY_PACKET:
Patrick McHardy84fa7932006-08-29 16:44:56 -0700183 if ((entry->skb->ip_summed == CHECKSUM_PARTIAL ||
184 entry->skb->ip_summed == CHECKSUM_COMPLETE) &&
185 (*errp = skb_checksum_help(entry->skb))) {
Patrick McHardy66a79a12005-08-23 10:10:35 -0700186 read_unlock_bh(&queue_lock);
187 return NULL;
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (copy_range == 0 || copy_range > entry->skb->len)
190 data_len = entry->skb->len;
191 else
192 data_len = copy_range;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
195 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 default:
198 *errp = -EINVAL;
199 read_unlock_bh(&queue_lock);
200 return NULL;
201 }
202
203 read_unlock_bh(&queue_lock);
204
205 skb = alloc_skb(size, GFP_ATOMIC);
206 if (!skb)
207 goto nlmsg_failure;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900208
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700209 old_tail = skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
211 pmsg = NLMSG_DATA(nlh);
212 memset(pmsg, 0, sizeof(*pmsg));
213
214 pmsg->packet_id = (unsigned long )entry;
215 pmsg->data_len = data_len;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700216 tv = ktime_to_timeval(entry->skb->tstamp);
217 pmsg->timestamp_sec = tv.tv_sec;
218 pmsg->timestamp_usec = tv.tv_usec;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800219 pmsg->mark = entry->skb->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 pmsg->hook = entry->info->hook;
221 pmsg->hw_protocol = entry->skb->protocol;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (entry->info->indev)
224 strcpy(pmsg->indev_name, entry->info->indev->name);
225 else
226 pmsg->indev_name[0] = '\0';
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (entry->info->outdev)
229 strcpy(pmsg->outdev_name, entry->info->outdev->name);
230 else
231 pmsg->outdev_name[0] = '\0';
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (entry->info->indev && entry->skb->dev) {
234 pmsg->hw_type = entry->skb->dev->type;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700235 pmsg->hw_addrlen = dev_parse_header(entry->skb,
236 pmsg->hw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (data_len)
240 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
241 BUG();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 nlh->nlmsg_len = skb->tail - old_tail;
244 return skb;
245
246nlmsg_failure:
247 if (skb)
248 kfree_skb(skb);
249 *errp = -EINVAL;
250 printk(KERN_ERR "ip_queue: error creating packet message\n");
251 return NULL;
252}
253
254static int
Harald Welte0ab43f82005-08-09 19:43:44 -0700255ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
Patrick McHardyf9d89282007-12-05 01:24:30 -0800256 unsigned int queuenum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 int status = -EINVAL;
259 struct sk_buff *nskb;
260 struct ipq_queue_entry *entry;
261
262 if (copy_mode == IPQ_COPY_NONE)
263 return -EAGAIN;
264
265 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
266 if (entry == NULL) {
267 printk(KERN_ERR "ip_queue: OOM in ipq_enqueue_packet()\n");
268 return -ENOMEM;
269 }
270
271 entry->info = info;
272 entry->skb = skb;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 nskb = ipq_build_packet_message(entry, &status);
275 if (nskb == NULL)
276 goto err_out_free;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 write_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (!peer_pid)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900281 goto err_out_free_nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if (queue_total >= queue_maxlen) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900284 queue_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 status = -ENOSPC;
286 if (net_ratelimit())
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900287 printk (KERN_WARNING "ip_queue: full at %d entries, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 "dropping packets(s). Dropped: %d\n", queue_total,
289 queue_dropped);
290 goto err_out_free_nskb;
291 }
292
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900293 /* netlink_unicast will either free the nskb or attach it to a socket */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
295 if (status < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900296 queue_user_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 goto err_out_unlock;
298 }
299
300 __ipq_enqueue_entry(entry);
301
302 write_unlock_bh(&queue_lock);
303 return status;
304
305err_out_free_nskb:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900306 kfree_skb(nskb);
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308err_out_unlock:
309 write_unlock_bh(&queue_lock);
310
311err_out_free:
312 kfree(entry);
313 return status;
314}
315
316static int
317ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
318{
319 int diff;
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700320 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 struct iphdr *user_iph = (struct iphdr *)v->payload;
322
323 if (v->data_len < sizeof(*user_iph))
324 return 0;
325 diff = v->data_len - e->skb->len;
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800326 if (diff < 0) {
327 if (pskb_trim(e->skb, v->data_len))
328 return -ENOMEM;
329 } else if (diff > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (v->data_len > 0xFFFF)
331 return -EINVAL;
332 if (diff > skb_tailroom(e->skb)) {
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700333 err = pskb_expand_head(e->skb, 0,
334 diff - skb_tailroom(e->skb),
335 GFP_ATOMIC);
336 if (err) {
337 printk(KERN_WARNING "ip_queue: error "
338 "in mangle, dropping packet: %d\n", -err);
339 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 skb_put(e->skb, diff);
343 }
Herbert Xu37d41872007-10-14 00:39:18 -0700344 if (!skb_make_writable(e->skb, v->data_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300346 skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
Patrick McHardy66a79a12005-08-23 10:10:35 -0700347 e->skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return 0;
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352static int
353ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
354{
355 struct ipq_queue_entry *entry;
356
357 if (vmsg->value > NF_MAX_VERDICT)
358 return -EINVAL;
359
Patrick McHardy95214092007-12-05 01:25:46 -0800360 entry = ipq_find_dequeue_entry(vmsg->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (entry == NULL)
362 return -ENOENT;
363 else {
364 int verdict = vmsg->value;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (vmsg->data_len && vmsg->data_len == len)
367 if (ipq_mangle_ipv4(vmsg, entry) < 0)
368 verdict = NF_DROP;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 ipq_issue_verdict(entry, verdict);
371 return 0;
372 }
373}
374
375static int
376ipq_set_mode(unsigned char mode, unsigned int range)
377{
378 int status;
379
380 write_lock_bh(&queue_lock);
381 status = __ipq_set_mode(mode, range);
382 write_unlock_bh(&queue_lock);
383 return status;
384}
385
386static int
387ipq_receive_peer(struct ipq_peer_msg *pmsg,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900388 unsigned char type, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 int status = 0;
391
392 if (len < sizeof(*pmsg))
393 return -EINVAL;
394
395 switch (type) {
396 case IPQM_MODE:
397 status = ipq_set_mode(pmsg->msg.mode.value,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900398 pmsg->msg.mode.range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 case IPQM_VERDICT:
402 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
403 status = -EINVAL;
404 else
405 status = ipq_set_verdict(&pmsg->msg.verdict,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900406 len - sizeof(*pmsg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 break;
408 default:
409 status = -EINVAL;
410 }
411 return status;
412}
413
414static int
415dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
416{
417 if (entry->info->indev)
418 if (entry->info->indev->ifindex == ifindex)
419 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (entry->info->outdev)
421 if (entry->info->outdev->ifindex == ifindex)
422 return 1;
Patrick McHardyef47c6a2006-06-27 03:01:48 -0700423#ifdef CONFIG_BRIDGE_NETFILTER
424 if (entry->skb->nf_bridge) {
425 if (entry->skb->nf_bridge->physindev &&
426 entry->skb->nf_bridge->physindev->ifindex == ifindex)
427 return 1;
428 if (entry->skb->nf_bridge->physoutdev &&
429 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900430 return 1;
Patrick McHardyef47c6a2006-06-27 03:01:48 -0700431 }
432#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return 0;
434}
435
436static void
437ipq_dev_drop(int ifindex)
438{
Patrick McHardy95214092007-12-05 01:25:46 -0800439 ipq_flush(dev_cmp, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
443
444static inline void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700445__ipq_rcv_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 int status, type, pid, flags, nlmsglen, skblen;
448 struct nlmsghdr *nlh;
449
450 skblen = skb->len;
451 if (skblen < sizeof(*nlh))
452 return;
453
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700454 nlh = nlmsg_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 nlmsglen = nlh->nlmsg_len;
456 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
457 return;
458
459 pid = nlh->nlmsg_pid;
460 flags = nlh->nlmsg_flags;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
463 RCV_SKB_FAIL(-EINVAL);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (flags & MSG_TRUNC)
466 RCV_SKB_FAIL(-ECOMM);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 type = nlh->nlmsg_type;
469 if (type < NLMSG_NOOP || type >= IPQM_MAX)
470 RCV_SKB_FAIL(-EINVAL);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (type <= IPQM_BASE)
473 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900474
Darrel Goeddelc7bdb542006-06-27 13:26:11 -0700475 if (security_netlink_recv(skb, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 RCV_SKB_FAIL(-EPERM);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 write_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (peer_pid) {
481 if (peer_pid != pid) {
482 write_unlock_bh(&queue_lock);
483 RCV_SKB_FAIL(-EBUSY);
484 }
485 } else {
486 net_enable_timestamp();
487 peer_pid = pid;
488 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 write_unlock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900493 nlmsglen - NLMSG_LENGTH(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (status < 0)
495 RCV_SKB_FAIL(status);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (flags & NLM_F_ACK)
498 netlink_ack(skb, nlh, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900499 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700503ipq_rcv_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800505 mutex_lock(&ipqnl_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700506 __ipq_rcv_skb(skb);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800507 mutex_unlock(&ipqnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
510static int
511ipq_rcv_dev_event(struct notifier_block *this,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900512 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 struct net_device *dev = ptr;
515
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200516 if (dev->nd_net != &init_net)
517 return NOTIFY_DONE;
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /* Drop any packets associated with the downed device */
520 if (event == NETDEV_DOWN)
521 ipq_dev_drop(dev->ifindex);
522 return NOTIFY_DONE;
523}
524
525static struct notifier_block ipq_dev_notifier = {
526 .notifier_call = ipq_rcv_dev_event,
527};
528
529static int
530ipq_rcv_nl_event(struct notifier_block *this,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900531 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 struct netlink_notify *n = ptr;
534
535 if (event == NETLINK_URELEASE &&
536 n->protocol == NETLINK_FIREWALL && n->pid) {
537 write_lock_bh(&queue_lock);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200538 if ((n->net == &init_net) && (n->pid == peer_pid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 __ipq_reset();
540 write_unlock_bh(&queue_lock);
541 }
542 return NOTIFY_DONE;
543}
544
545static struct notifier_block ipq_nl_notifier = {
546 .notifier_call = ipq_rcv_nl_event,
547};
548
549static struct ctl_table_header *ipq_sysctl_header;
550
551static ctl_table ipq_table[] = {
552 {
553 .ctl_name = NET_IPQ_QMAX,
554 .procname = NET_IPQ_QMAX_NAME,
555 .data = &queue_maxlen,
556 .maxlen = sizeof(queue_maxlen),
557 .mode = 0644,
558 .proc_handler = proc_dointvec
559 },
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900560 { .ctl_name = 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561};
562
563static ctl_table ipq_dir_table[] = {
564 {
565 .ctl_name = NET_IPV4,
566 .procname = "ipv4",
567 .mode = 0555,
568 .child = ipq_table
569 },
570 { .ctl_name = 0 }
571};
572
573static ctl_table ipq_root_table[] = {
574 {
575 .ctl_name = CTL_NET,
576 .procname = "net",
577 .mode = 0555,
578 .child = ipq_dir_table
579 },
580 { .ctl_name = 0 }
581};
582
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800583static int ip_queue_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 read_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900586
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800587 seq_printf(m,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900588 "Peer PID : %d\n"
589 "Copy mode : %hu\n"
590 "Copy range : %u\n"
591 "Queue length : %u\n"
592 "Queue max. length : %u\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 "Queue dropped : %u\n"
594 "Netlink dropped : %u\n",
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900595 peer_pid,
596 copy_mode,
597 copy_range,
598 queue_total,
599 queue_maxlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 queue_dropped,
601 queue_user_dropped);
602
603 read_unlock_bh(&queue_lock);
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800604 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800606
607static int ip_queue_open(struct inode *inode, struct file *file)
608{
609 return single_open(file, ip_queue_show, NULL);
610}
611
612static const struct file_operations ip_queue_proc_fops = {
613 .open = ip_queue_open,
614 .read = seq_read,
615 .llseek = seq_lseek,
616 .release = single_release,
617 .owner = THIS_MODULE,
618};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Patrick McHardye3ac5292007-12-05 01:23:57 -0800620static const struct nf_queue_handler nfqh = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700621 .name = "ip_queue",
622 .outfn = &ipq_enqueue_packet,
623};
624
Patrick McHardy32292a72006-04-06 14:11:30 -0700625static int __init ip_queue_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 int status = -ENOMEM;
628 struct proc_dir_entry *proc;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 netlink_register_notifier(&ipq_nl_notifier);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200631 ipqnl = netlink_kernel_create(&init_net, NETLINK_FIREWALL, 0,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700632 ipq_rcv_skb, NULL, THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (ipqnl == NULL) {
634 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
635 goto cleanup_netlink_notifier;
636 }
637
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800638 proc = create_proc_entry(IPQ_PROC_FS_NAME, 0, init_net.proc_net);
639 if (proc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 proc->owner = THIS_MODULE;
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800641 proc->proc_fops = &ip_queue_proc_fops;
642 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
644 goto cleanup_ipqnl;
645 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 register_netdevice_notifier(&ipq_dev_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800648 ipq_sysctl_header = register_sysctl_table(ipq_root_table);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900649
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700650 status = nf_register_queue_handler(PF_INET, &nfqh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (status < 0) {
652 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
653 goto cleanup_sysctl;
654 }
655 return status;
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657cleanup_sysctl:
658 unregister_sysctl_table(ipq_sysctl_header);
659 unregister_netdevice_notifier(&ipq_dev_notifier);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200660 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661cleanup_ipqnl:
662 sock_release(ipqnl->sk_socket);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800663 mutex_lock(&ipqnl_mutex);
664 mutex_unlock(&ipqnl_mutex);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666cleanup_netlink_notifier:
667 netlink_unregister_notifier(&ipq_nl_notifier);
668 return status;
669}
670
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800671static void __exit ip_queue_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Patrick McHardy32292a72006-04-06 14:11:30 -0700673 nf_unregister_queue_handlers(&nfqh);
674 synchronize_net();
Patrick McHardy95214092007-12-05 01:25:46 -0800675 ipq_flush(NULL, 0);
Patrick McHardy32292a72006-04-06 14:11:30 -0700676
677 unregister_sysctl_table(ipq_sysctl_header);
678 unregister_netdevice_notifier(&ipq_dev_notifier);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200679 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
Patrick McHardy32292a72006-04-06 14:11:30 -0700680
681 sock_release(ipqnl->sk_socket);
682 mutex_lock(&ipqnl_mutex);
683 mutex_unlock(&ipqnl_mutex);
684
685 netlink_unregister_notifier(&ipq_nl_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688MODULE_DESCRIPTION("IPv4 packet queue handler");
689MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
690MODULE_LICENSE("GPL");
691
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800692module_init(ip_queue_init);
693module_exit(ip_queue_fini);